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 <asm/uaccess.h>
31 * ea_calc_size - returns the acutal number of bytes the request will take up
32 * (not counting any unstuffed data blocks)
37 * Returns: 1 if the EA should be stuffed
40 static int ea_calc_size(struct gfs2_sbd
*sdp
, unsigned int nsize
, size_t dsize
,
43 unsigned int jbsize
= sdp
->sd_jbsize
;
46 *size
= ALIGN(sizeof(struct gfs2_ea_header
) + nsize
+ dsize
, 8);
52 *size
= ALIGN(sizeof(struct gfs2_ea_header
) + nsize
+
53 (sizeof(__be64
) * DIV_ROUND_UP(dsize
, jbsize
)), 8);
58 static int ea_check_size(struct gfs2_sbd
*sdp
, unsigned int nsize
, size_t dsize
)
62 if (dsize
> GFS2_EA_MAX_DATA_LEN
)
65 ea_calc_size(sdp
, nsize
, dsize
, &size
);
67 /* This can only happen with 512 byte blocks */
68 if (size
> sdp
->sd_jbsize
)
74 typedef int (*ea_call_t
) (struct gfs2_inode
*ip
, struct buffer_head
*bh
,
75 struct gfs2_ea_header
*ea
,
76 struct gfs2_ea_header
*prev
, void *private);
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(GFS2_SB(&ip
->i_inode
), 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
&& (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_eattr
, DIO_WAIT
, &bh
);
125 if (!(ip
->i_diskflags
& GFS2_DIF_EA_INDIRECT
)) {
126 error
= ea_foreach_i(ip
, bh
, ea_call
, data
);
130 if (gfs2_metatype_check(GFS2_SB(&ip
->i_inode
), bh
, GFS2_METATYPE_IN
)) {
135 eablk
= (__be64
*)(bh
->b_data
+ sizeof(struct gfs2_meta_header
));
136 end
= eablk
+ GFS2_SB(&ip
->i_inode
)->sd_inptrs
;
138 for (; eablk
< end
; eablk
++) {
143 bn
= be64_to_cpu(*eablk
);
145 error
= gfs2_meta_read(ip
->i_gl
, bn
, DIO_WAIT
, &eabh
);
148 error
= ea_foreach_i(ip
, eabh
, ea_call
, data
);
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;
171 if (ea
->ea_type
== GFS2_EATYPE_UNUSED
)
174 if (ea
->ea_type
== ef
->type
) {
175 if (ea
->ea_name_len
== ef
->namel
&&
176 !memcmp(GFS2_EA2NAME(ea
), ef
->name
, ea
->ea_name_len
)) {
177 struct gfs2_ea_location
*el
= ef
->ef_el
;
189 int gfs2_ea_find(struct gfs2_inode
*ip
, int type
, const char *name
,
190 struct gfs2_ea_location
*el
)
197 ef
.namel
= strlen(name
);
200 memset(el
, 0, sizeof(struct gfs2_ea_location
));
202 error
= ea_foreach(ip
, ea_find_i
, &ef
);
210 * ea_dealloc_unstuffed -
217 * Take advantage of the fact that all unstuffed blocks are
218 * allocated from the same RG. But watch, this may not always
224 static int ea_dealloc_unstuffed(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
225 struct gfs2_ea_header
*ea
,
226 struct gfs2_ea_header
*prev
, void *private)
228 int *leave
= private;
229 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
230 struct gfs2_rgrpd
*rgd
;
231 struct gfs2_holder rg_gh
;
232 struct buffer_head
*dibh
;
236 unsigned int blen
= 0;
237 unsigned int blks
= 0;
241 if (GFS2_EA_IS_STUFFED(ea
))
244 dataptrs
= GFS2_EA2DATAPTRS(ea
);
245 for (x
= 0; x
< ea
->ea_num_ptrs
; x
++, dataptrs
++) {
248 bn
= be64_to_cpu(*dataptrs
);
254 rgd
= gfs2_blk2rgrpd(sdp
, bn
);
256 gfs2_consist_inode(ip
);
260 error
= gfs2_glock_nq_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, &rg_gh
);
264 error
= gfs2_trans_begin(sdp
, rgd
->rd_length
+ RES_DINODE
+
265 RES_EATTR
+ RES_STATFS
+ RES_QUOTA
, blks
);
269 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
271 dataptrs
= GFS2_EA2DATAPTRS(ea
);
272 for (x
= 0; x
< ea
->ea_num_ptrs
; x
++, dataptrs
++) {
275 bn
= be64_to_cpu(*dataptrs
);
277 if (bstart
+ blen
== bn
)
281 gfs2_free_meta(ip
, bstart
, blen
);
287 gfs2_add_inode_blocks(&ip
->i_inode
, -1);
290 gfs2_free_meta(ip
, bstart
, blen
);
292 if (prev
&& !leave
) {
295 len
= GFS2_EA_REC_LEN(prev
) + GFS2_EA_REC_LEN(ea
);
296 prev
->ea_rec_len
= cpu_to_be32(len
);
298 if (GFS2_EA_IS_LAST(ea
))
299 prev
->ea_flags
|= GFS2_EAFLAG_LAST
;
301 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
305 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
307 ip
->i_inode
.i_ctime
= CURRENT_TIME
;
308 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
309 gfs2_dinode_out(ip
, dibh
->b_data
);
316 gfs2_glock_dq_uninit(&rg_gh
);
320 static int ea_remove_unstuffed(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
321 struct gfs2_ea_header
*ea
,
322 struct gfs2_ea_header
*prev
, int leave
)
324 struct gfs2_alloc
*al
;
327 al
= gfs2_alloc_get(ip
);
331 error
= gfs2_quota_hold(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
335 error
= gfs2_rindex_hold(GFS2_SB(&ip
->i_inode
), &al
->al_ri_gh
);
339 error
= ea_dealloc_unstuffed(ip
, bh
, ea
, prev
, (leave
) ? &error
: NULL
);
341 gfs2_glock_dq_uninit(&al
->al_ri_gh
);
344 gfs2_quota_unhold(ip
);
351 struct gfs2_ea_request
*ei_er
;
352 unsigned int ei_size
;
355 static inline unsigned int gfs2_ea_strlen(struct gfs2_ea_header
*ea
)
357 switch (ea
->ea_type
) {
358 case GFS2_EATYPE_USR
:
359 return 5 + ea
->ea_name_len
+ 1;
360 case GFS2_EATYPE_SYS
:
361 return 7 + ea
->ea_name_len
+ 1;
362 case GFS2_EATYPE_SECURITY
:
363 return 9 + ea
->ea_name_len
+ 1;
369 static int ea_list_i(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
370 struct gfs2_ea_header
*ea
, struct gfs2_ea_header
*prev
,
373 struct ea_list
*ei
= private;
374 struct gfs2_ea_request
*er
= ei
->ei_er
;
375 unsigned int ea_size
= gfs2_ea_strlen(ea
);
377 if (ea
->ea_type
== GFS2_EATYPE_UNUSED
)
380 if (er
->er_data_len
) {
385 if (ei
->ei_size
+ ea_size
> er
->er_data_len
)
388 switch (ea
->ea_type
) {
389 case GFS2_EATYPE_USR
:
393 case GFS2_EATYPE_SYS
:
397 case GFS2_EATYPE_SECURITY
:
398 prefix
= "security.";
405 memcpy(er
->er_data
+ ei
->ei_size
, prefix
, l
);
406 memcpy(er
->er_data
+ ei
->ei_size
+ l
, GFS2_EA2NAME(ea
),
408 memcpy(er
->er_data
+ ei
->ei_size
+ ea_size
- 1, &c
, 1);
411 ei
->ei_size
+= ea_size
;
417 * gfs2_listxattr - List gfs2 extended attributes
418 * @dentry: The dentry whose inode we are interested in
419 * @buffer: The buffer to write the results
420 * @size: The size of the buffer
422 * Returns: actual size of data on success, -errno on error
425 ssize_t
gfs2_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
427 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
428 struct gfs2_ea_request er
;
429 struct gfs2_holder i_gh
;
432 memset(&er
, 0, sizeof(struct gfs2_ea_request
));
435 er
.er_data_len
= size
;
438 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &i_gh
);
443 struct ea_list ei
= { .ei_er
= &er
, .ei_size
= 0 };
445 error
= ea_foreach(ip
, ea_list_i
, &ei
);
450 gfs2_glock_dq_uninit(&i_gh
);
456 * ea_get_unstuffed - actually copies the unstuffed data into the
458 * @ip: The GFS2 inode
459 * @ea: The extended attribute header structure
460 * @data: The data to be copied
465 static int ea_get_unstuffed(struct gfs2_inode
*ip
, struct gfs2_ea_header
*ea
,
468 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
469 struct buffer_head
**bh
;
470 unsigned int amount
= GFS2_EA_DATA_LEN(ea
);
471 unsigned int nptrs
= DIV_ROUND_UP(amount
, sdp
->sd_jbsize
);
472 __be64
*dataptrs
= GFS2_EA2DATAPTRS(ea
);
476 bh
= kcalloc(nptrs
, sizeof(struct buffer_head
*), GFP_NOFS
);
480 for (x
= 0; x
< nptrs
; x
++) {
481 error
= gfs2_meta_read(ip
->i_gl
, be64_to_cpu(*dataptrs
), 0,
491 for (x
= 0; x
< nptrs
; x
++) {
492 error
= gfs2_meta_wait(sdp
, bh
[x
]);
494 for (; x
< nptrs
; x
++)
498 if (gfs2_metatype_check(sdp
, bh
[x
], GFS2_METATYPE_ED
)) {
499 for (; x
< nptrs
; x
++)
505 memcpy(data
, bh
[x
]->b_data
+ sizeof(struct gfs2_meta_header
),
506 (sdp
->sd_jbsize
> amount
) ? amount
: sdp
->sd_jbsize
);
508 amount
-= sdp
->sd_jbsize
;
509 data
+= sdp
->sd_jbsize
;
519 int gfs2_ea_get_copy(struct gfs2_inode
*ip
, struct gfs2_ea_location
*el
,
520 char *data
, size_t size
)
523 size_t len
= GFS2_EA_DATA_LEN(el
->el_ea
);
527 if (GFS2_EA_IS_STUFFED(el
->el_ea
)) {
528 memcpy(data
, GFS2_EA2DATA(el
->el_ea
), len
);
531 ret
= ea_get_unstuffed(ip
, el
->el_ea
, data
);
538 * gfs2_xattr_get - Get a GFS2 extended attribute
540 * @type: The type of extended attribute
541 * @name: The name of the extended attribute
542 * @buffer: The buffer to write the result into
543 * @size: The size of the buffer
545 * Returns: actual size of data on success, -errno on error
548 int gfs2_xattr_get(struct inode
*inode
, int type
, const char *name
,
549 void *buffer
, size_t size
)
551 struct gfs2_inode
*ip
= GFS2_I(inode
);
552 struct gfs2_ea_location el
;
557 if (strlen(name
) > GFS2_EA_MAX_NAME_LEN
)
560 error
= gfs2_ea_find(ip
, type
, name
, &el
);
566 error
= gfs2_ea_get_copy(ip
, &el
, buffer
, size
);
568 error
= GFS2_EA_DATA_LEN(el
.el_ea
);
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
;
590 error
= gfs2_alloc_block(ip
, &block
, &n
);
593 gfs2_trans_add_unrevoke(sdp
, block
, 1);
594 *bhp
= gfs2_meta_new(ip
->i_gl
, block
);
595 gfs2_trans_add_bh(ip
->i_gl
, *bhp
, 1);
596 gfs2_metatype_set(*bhp
, GFS2_METATYPE_EA
, GFS2_FORMAT_EA
);
597 gfs2_buffer_clear_tail(*bhp
, sizeof(struct gfs2_meta_header
));
599 ea
= GFS2_EA_BH2FIRST(*bhp
);
600 ea
->ea_rec_len
= cpu_to_be32(sdp
->sd_jbsize
);
601 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
602 ea
->ea_flags
= GFS2_EAFLAG_LAST
;
605 gfs2_add_inode_blocks(&ip
->i_inode
, 1);
611 * ea_write - writes the request info to an ea, creating new blocks if
613 * @ip: inode that is being modified
614 * @ea: the location of the new ea in a block
615 * @er: the write request
617 * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
622 static int ea_write(struct gfs2_inode
*ip
, struct gfs2_ea_header
*ea
,
623 struct gfs2_ea_request
*er
)
625 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
628 ea
->ea_data_len
= cpu_to_be32(er
->er_data_len
);
629 ea
->ea_name_len
= er
->er_name_len
;
630 ea
->ea_type
= er
->er_type
;
633 memcpy(GFS2_EA2NAME(ea
), er
->er_name
, er
->er_name_len
);
635 if (GFS2_EAREQ_SIZE_STUFFED(er
) <= sdp
->sd_jbsize
) {
637 memcpy(GFS2_EA2DATA(ea
), er
->er_data
, er
->er_data_len
);
639 __be64
*dataptr
= GFS2_EA2DATAPTRS(ea
);
640 const char *data
= er
->er_data
;
641 unsigned int data_len
= er
->er_data_len
;
645 ea
->ea_num_ptrs
= DIV_ROUND_UP(er
->er_data_len
, sdp
->sd_jbsize
);
646 for (x
= 0; x
< ea
->ea_num_ptrs
; x
++) {
647 struct buffer_head
*bh
;
649 int mh_size
= sizeof(struct gfs2_meta_header
);
652 error
= gfs2_alloc_block(ip
, &block
, &n
);
655 gfs2_trans_add_unrevoke(sdp
, block
, 1);
656 bh
= gfs2_meta_new(ip
->i_gl
, block
);
657 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
658 gfs2_metatype_set(bh
, GFS2_METATYPE_ED
, GFS2_FORMAT_ED
);
660 gfs2_add_inode_blocks(&ip
->i_inode
, 1);
662 copy
= data_len
> sdp
->sd_jbsize
? sdp
->sd_jbsize
:
664 memcpy(bh
->b_data
+ mh_size
, data
, copy
);
665 if (copy
< sdp
->sd_jbsize
)
666 memset(bh
->b_data
+ mh_size
+ copy
, 0,
667 sdp
->sd_jbsize
- copy
);
669 *dataptr
++ = cpu_to_be64(bh
->b_blocknr
);
676 gfs2_assert_withdraw(sdp
, !data_len
);
682 typedef int (*ea_skeleton_call_t
) (struct gfs2_inode
*ip
,
683 struct gfs2_ea_request
*er
, void *private);
685 static int ea_alloc_skeleton(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
,
687 ea_skeleton_call_t skeleton_call
, void *private)
689 struct gfs2_alloc
*al
;
690 struct buffer_head
*dibh
;
693 al
= gfs2_alloc_get(ip
);
697 error
= gfs2_quota_lock_check(ip
);
701 al
->al_requested
= blks
;
703 error
= gfs2_inplace_reserve(ip
);
707 error
= gfs2_trans_begin(GFS2_SB(&ip
->i_inode
),
708 blks
+ al
->al_rgd
->rd_length
+
709 RES_DINODE
+ RES_STATFS
+ RES_QUOTA
, 0);
713 error
= skeleton_call(ip
, er
, private);
717 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
719 ip
->i_inode
.i_ctime
= CURRENT_TIME
;
720 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
721 gfs2_dinode_out(ip
, dibh
->b_data
);
726 gfs2_trans_end(GFS2_SB(&ip
->i_inode
));
728 gfs2_inplace_release(ip
);
730 gfs2_quota_unlock(ip
);
736 static int ea_init_i(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
,
739 struct buffer_head
*bh
;
742 error
= ea_alloc_blk(ip
, &bh
);
746 ip
->i_eattr
= bh
->b_blocknr
;
747 error
= ea_write(ip
, GFS2_EA_BH2FIRST(bh
), er
);
755 * ea_init - initializes a new eattr block
762 static int ea_init(struct gfs2_inode
*ip
, int type
, const char *name
,
763 const void *data
, size_t size
)
765 struct gfs2_ea_request er
;
766 unsigned int jbsize
= GFS2_SB(&ip
->i_inode
)->sd_jbsize
;
767 unsigned int blks
= 1;
771 er
.er_name_len
= strlen(name
);
772 er
.er_data
= (void *)data
;
773 er
.er_data_len
= size
;
775 if (GFS2_EAREQ_SIZE_STUFFED(&er
) > jbsize
)
776 blks
+= DIV_ROUND_UP(er
.er_data_len
, jbsize
);
778 return ea_alloc_skeleton(ip
, &er
, blks
, ea_init_i
, NULL
);
781 static struct gfs2_ea_header
*ea_split_ea(struct gfs2_ea_header
*ea
)
783 u32 ea_size
= GFS2_EA_SIZE(ea
);
784 struct gfs2_ea_header
*new = (struct gfs2_ea_header
*)((char *)ea
+
786 u32 new_size
= GFS2_EA_REC_LEN(ea
) - ea_size
;
787 int last
= ea
->ea_flags
& GFS2_EAFLAG_LAST
;
789 ea
->ea_rec_len
= cpu_to_be32(ea_size
);
790 ea
->ea_flags
^= last
;
792 new->ea_rec_len
= cpu_to_be32(new_size
);
793 new->ea_flags
= last
;
798 static void ea_set_remove_stuffed(struct gfs2_inode
*ip
,
799 struct gfs2_ea_location
*el
)
801 struct gfs2_ea_header
*ea
= el
->el_ea
;
802 struct gfs2_ea_header
*prev
= el
->el_prev
;
805 gfs2_trans_add_bh(ip
->i_gl
, el
->el_bh
, 1);
807 if (!prev
|| !GFS2_EA_IS_STUFFED(ea
)) {
808 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
810 } else if (GFS2_EA2NEXT(prev
) != ea
) {
811 prev
= GFS2_EA2NEXT(prev
);
812 gfs2_assert_withdraw(GFS2_SB(&ip
->i_inode
), GFS2_EA2NEXT(prev
) == ea
);
815 len
= GFS2_EA_REC_LEN(prev
) + GFS2_EA_REC_LEN(ea
);
816 prev
->ea_rec_len
= cpu_to_be32(len
);
818 if (GFS2_EA_IS_LAST(ea
))
819 prev
->ea_flags
|= GFS2_EAFLAG_LAST
;
825 struct gfs2_ea_request
*es_er
;
826 struct gfs2_ea_location
*es_el
;
828 struct buffer_head
*es_bh
;
829 struct gfs2_ea_header
*es_ea
;
832 static int ea_set_simple_noalloc(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
833 struct gfs2_ea_header
*ea
, struct ea_set
*es
)
835 struct gfs2_ea_request
*er
= es
->es_er
;
836 struct buffer_head
*dibh
;
839 error
= gfs2_trans_begin(GFS2_SB(&ip
->i_inode
), RES_DINODE
+ 2 * RES_EATTR
, 0);
843 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
846 ea
= ea_split_ea(ea
);
848 ea_write(ip
, ea
, er
);
851 ea_set_remove_stuffed(ip
, es
->es_el
);
853 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
856 ip
->i_inode
.i_ctime
= CURRENT_TIME
;
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
->er_name_len
,
897 es
->es_er
->er_data_len
, &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_diskflags
& GFS2_DIF_EA_INDIRECT
) {
946 error
= gfs2_meta_read(ip
->i_gl
, ip
->i_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 error
= gfs2_alloc_block(ip
, &blk
, &n
);
975 gfs2_trans_add_unrevoke(sdp
, blk
, 1);
976 indbh
= gfs2_meta_new(ip
->i_gl
, blk
);
977 gfs2_trans_add_bh(ip
->i_gl
, indbh
, 1);
978 gfs2_metatype_set(indbh
, GFS2_METATYPE_IN
, GFS2_FORMAT_IN
);
979 gfs2_buffer_clear_tail(indbh
, mh_size
);
981 eablk
= (__be64
*)(indbh
->b_data
+ mh_size
);
982 *eablk
= cpu_to_be64(ip
->i_eattr
);
984 ip
->i_diskflags
|= GFS2_DIF_EA_INDIRECT
;
985 gfs2_add_inode_blocks(&ip
->i_inode
, 1);
990 error
= ea_alloc_blk(ip
, &newbh
);
994 *eablk
= cpu_to_be64((u64
)newbh
->b_blocknr
);
995 error
= ea_write(ip
, GFS2_EA_BH2FIRST(newbh
), er
);
1001 ea_set_remove_stuffed(ip
, private);
1008 static int ea_set_i(struct gfs2_inode
*ip
, int type
, const char *name
,
1009 const void *value
, size_t size
, struct gfs2_ea_location
*el
)
1011 struct gfs2_ea_request er
;
1013 unsigned int blks
= 2;
1018 er
.er_data
= (void *)value
;
1019 er
.er_name_len
= strlen(name
);
1020 er
.er_data_len
= size
;
1022 memset(&es
, 0, sizeof(struct ea_set
));
1026 error
= ea_foreach(ip
, ea_set_simple
, &es
);
1032 if (!(ip
->i_diskflags
& GFS2_DIF_EA_INDIRECT
))
1034 if (GFS2_EAREQ_SIZE_STUFFED(&er
) > GFS2_SB(&ip
->i_inode
)->sd_jbsize
)
1035 blks
+= DIV_ROUND_UP(er
.er_data_len
, GFS2_SB(&ip
->i_inode
)->sd_jbsize
);
1037 return ea_alloc_skeleton(ip
, &er
, blks
, ea_set_block
, el
);
1040 static int ea_set_remove_unstuffed(struct gfs2_inode
*ip
,
1041 struct gfs2_ea_location
*el
)
1043 if (el
->el_prev
&& GFS2_EA2NEXT(el
->el_prev
) != el
->el_ea
) {
1044 el
->el_prev
= GFS2_EA2NEXT(el
->el_prev
);
1045 gfs2_assert_withdraw(GFS2_SB(&ip
->i_inode
),
1046 GFS2_EA2NEXT(el
->el_prev
) == el
->el_ea
);
1049 return ea_remove_unstuffed(ip
, el
->el_bh
, el
->el_ea
, el
->el_prev
, 0);
1052 static int ea_remove_stuffed(struct gfs2_inode
*ip
, struct gfs2_ea_location
*el
)
1054 struct gfs2_ea_header
*ea
= el
->el_ea
;
1055 struct gfs2_ea_header
*prev
= el
->el_prev
;
1056 struct buffer_head
*dibh
;
1059 error
= gfs2_trans_begin(GFS2_SB(&ip
->i_inode
), RES_DINODE
+ RES_EATTR
, 0);
1063 gfs2_trans_add_bh(ip
->i_gl
, el
->el_bh
, 1);
1068 len
= GFS2_EA_REC_LEN(prev
) + GFS2_EA_REC_LEN(ea
);
1069 prev
->ea_rec_len
= cpu_to_be32(len
);
1071 if (GFS2_EA_IS_LAST(ea
))
1072 prev
->ea_flags
|= GFS2_EAFLAG_LAST
;
1074 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
1077 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1079 ip
->i_inode
.i_ctime
= CURRENT_TIME
;
1080 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1081 gfs2_dinode_out(ip
, dibh
->b_data
);
1085 gfs2_trans_end(GFS2_SB(&ip
->i_inode
));
1091 * gfs2_xattr_remove - Remove a GFS2 extended attribute
1093 * @type: The type of the extended attribute
1094 * @name: The name of the extended attribute
1096 * This is not called directly by the VFS since we use the (common)
1097 * scheme of making a "set with NULL data" mean a remove request. Note
1098 * that this is different from a set with zero length data.
1100 * Returns: 0, or errno on failure
1103 static int gfs2_xattr_remove(struct inode
*inode
, int type
, const char *name
)
1105 struct gfs2_inode
*ip
= GFS2_I(inode
);
1106 struct gfs2_ea_location el
;
1112 error
= gfs2_ea_find(ip
, type
, name
, &el
);
1118 if (GFS2_EA_IS_STUFFED(el
.el_ea
))
1119 error
= ea_remove_stuffed(ip
, &el
);
1121 error
= ea_remove_unstuffed(ip
, el
.el_bh
, el
.el_ea
, el
.el_prev
, 0);
1129 * gfs2_xattr_set - Set (or remove) a GFS2 extended attribute
1131 * @type: The type of the extended attribute
1132 * @name: The name of the extended attribute
1133 * @value: The value of the extended attribute (NULL for remove)
1134 * @size: The size of the @value argument
1135 * @flags: Create or Replace
1137 * See gfs2_xattr_remove() for details of the removal of xattrs.
1139 * Returns: 0 or errno on failure
1142 int gfs2_xattr_set(struct inode
*inode
, int type
, const char *name
,
1143 const void *value
, size_t size
, int flags
)
1145 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1146 struct gfs2_inode
*ip
= GFS2_I(inode
);
1147 struct gfs2_ea_location el
;
1148 unsigned int namel
= strlen(name
);
1151 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
1153 if (namel
> GFS2_EA_MAX_NAME_LEN
)
1157 return gfs2_xattr_remove(inode
, type
, name
);
1159 if (ea_check_size(sdp
, namel
, size
))
1163 if (flags
& XATTR_REPLACE
)
1165 return ea_init(ip
, type
, name
, value
, size
);
1168 error
= gfs2_ea_find(ip
, type
, name
, &el
);
1173 if (ip
->i_diskflags
& GFS2_DIF_APPENDONLY
) {
1179 if (!(flags
& XATTR_CREATE
)) {
1180 int unstuffed
= !GFS2_EA_IS_STUFFED(el
.el_ea
);
1181 error
= ea_set_i(ip
, type
, name
, value
, size
, &el
);
1182 if (!error
&& unstuffed
)
1183 ea_set_remove_unstuffed(ip
, &el
);
1191 if (!(flags
& XATTR_REPLACE
))
1192 error
= ea_set_i(ip
, type
, name
, value
, size
, NULL
);
1197 static int ea_acl_chmod_unstuffed(struct gfs2_inode
*ip
,
1198 struct gfs2_ea_header
*ea
, char *data
)
1200 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1201 struct buffer_head
**bh
;
1202 unsigned int amount
= GFS2_EA_DATA_LEN(ea
);
1203 unsigned int nptrs
= DIV_ROUND_UP(amount
, sdp
->sd_jbsize
);
1204 __be64
*dataptrs
= GFS2_EA2DATAPTRS(ea
);
1208 bh
= kcalloc(nptrs
, sizeof(struct buffer_head
*), GFP_NOFS
);
1212 error
= gfs2_trans_begin(sdp
, nptrs
+ RES_DINODE
, 0);
1216 for (x
= 0; x
< nptrs
; x
++) {
1217 error
= gfs2_meta_read(ip
->i_gl
, be64_to_cpu(*dataptrs
), 0,
1227 for (x
= 0; x
< nptrs
; x
++) {
1228 error
= gfs2_meta_wait(sdp
, bh
[x
]);
1230 for (; x
< nptrs
; x
++)
1234 if (gfs2_metatype_check(sdp
, bh
[x
], GFS2_METATYPE_ED
)) {
1235 for (; x
< nptrs
; x
++)
1241 gfs2_trans_add_bh(ip
->i_gl
, bh
[x
], 1);
1243 memcpy(bh
[x
]->b_data
+ sizeof(struct gfs2_meta_header
), data
,
1244 (sdp
->sd_jbsize
> amount
) ? amount
: sdp
->sd_jbsize
);
1246 amount
-= sdp
->sd_jbsize
;
1247 data
+= sdp
->sd_jbsize
;
1257 gfs2_trans_end(sdp
);
1262 int gfs2_ea_acl_chmod(struct gfs2_inode
*ip
, struct gfs2_ea_location
*el
,
1263 struct iattr
*attr
, char *data
)
1265 struct buffer_head
*dibh
;
1268 if (GFS2_EA_IS_STUFFED(el
->el_ea
)) {
1269 error
= gfs2_trans_begin(GFS2_SB(&ip
->i_inode
), RES_DINODE
+ RES_EATTR
, 0);
1273 gfs2_trans_add_bh(ip
->i_gl
, el
->el_bh
, 1);
1274 memcpy(GFS2_EA2DATA(el
->el_ea
), data
,
1275 GFS2_EA_DATA_LEN(el
->el_ea
));
1277 error
= ea_acl_chmod_unstuffed(ip
, el
->el_ea
, data
);
1282 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1284 error
= inode_setattr(&ip
->i_inode
, attr
);
1285 gfs2_assert_warn(GFS2_SB(&ip
->i_inode
), !error
);
1286 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1287 gfs2_dinode_out(ip
, dibh
->b_data
);
1291 gfs2_trans_end(GFS2_SB(&ip
->i_inode
));
1296 static int ea_dealloc_indirect(struct gfs2_inode
*ip
)
1298 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1299 struct gfs2_rgrp_list rlist
;
1300 struct buffer_head
*indbh
, *dibh
;
1301 __be64
*eablk
, *end
;
1302 unsigned int rg_blocks
= 0;
1304 unsigned int blen
= 0;
1305 unsigned int blks
= 0;
1309 memset(&rlist
, 0, sizeof(struct gfs2_rgrp_list
));
1311 error
= gfs2_meta_read(ip
->i_gl
, ip
->i_eattr
, DIO_WAIT
, &indbh
);
1315 if (gfs2_metatype_check(sdp
, indbh
, GFS2_METATYPE_IN
)) {
1320 eablk
= (__be64
*)(indbh
->b_data
+ sizeof(struct gfs2_meta_header
));
1321 end
= eablk
+ sdp
->sd_inptrs
;
1323 for (; eablk
< end
; eablk
++) {
1328 bn
= be64_to_cpu(*eablk
);
1330 if (bstart
+ blen
== bn
)
1334 gfs2_rlist_add(sdp
, &rlist
, bstart
);
1341 gfs2_rlist_add(sdp
, &rlist
, bstart
);
1345 gfs2_rlist_alloc(&rlist
, LM_ST_EXCLUSIVE
);
1347 for (x
= 0; x
< rlist
.rl_rgrps
; x
++) {
1348 struct gfs2_rgrpd
*rgd
;
1349 rgd
= rlist
.rl_ghs
[x
].gh_gl
->gl_object
;
1350 rg_blocks
+= rgd
->rd_length
;
1353 error
= gfs2_glock_nq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1355 goto out_rlist_free
;
1357 error
= gfs2_trans_begin(sdp
, rg_blocks
+ RES_DINODE
+ RES_INDIRECT
+
1358 RES_STATFS
+ RES_QUOTA
, blks
);
1362 gfs2_trans_add_bh(ip
->i_gl
, indbh
, 1);
1364 eablk
= (__be64
*)(indbh
->b_data
+ sizeof(struct gfs2_meta_header
));
1368 for (; eablk
< end
; eablk
++) {
1373 bn
= be64_to_cpu(*eablk
);
1375 if (bstart
+ blen
== bn
)
1379 gfs2_free_meta(ip
, bstart
, blen
);
1385 gfs2_add_inode_blocks(&ip
->i_inode
, -1);
1388 gfs2_free_meta(ip
, bstart
, blen
);
1390 ip
->i_diskflags
&= ~GFS2_DIF_EA_INDIRECT
;
1392 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1394 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1395 gfs2_dinode_out(ip
, dibh
->b_data
);
1399 gfs2_trans_end(sdp
);
1402 gfs2_glock_dq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1404 gfs2_rlist_free(&rlist
);
1410 static int ea_dealloc_block(struct gfs2_inode
*ip
)
1412 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1413 struct gfs2_alloc
*al
= ip
->i_alloc
;
1414 struct gfs2_rgrpd
*rgd
;
1415 struct buffer_head
*dibh
;
1418 rgd
= gfs2_blk2rgrpd(sdp
, ip
->i_eattr
);
1420 gfs2_consist_inode(ip
);
1424 error
= gfs2_glock_nq_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0,
1429 error
= gfs2_trans_begin(sdp
, RES_RG_BIT
+ RES_DINODE
+ RES_STATFS
+
1434 gfs2_free_meta(ip
, ip
->i_eattr
, 1);
1437 gfs2_add_inode_blocks(&ip
->i_inode
, -1);
1439 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1441 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1442 gfs2_dinode_out(ip
, dibh
->b_data
);
1446 gfs2_trans_end(sdp
);
1449 gfs2_glock_dq_uninit(&al
->al_rgd_gh
);
1454 * gfs2_ea_dealloc - deallocate the extended attribute fork
1460 int gfs2_ea_dealloc(struct gfs2_inode
*ip
)
1462 struct gfs2_alloc
*al
;
1465 al
= gfs2_alloc_get(ip
);
1469 error
= gfs2_quota_hold(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
1473 error
= gfs2_rindex_hold(GFS2_SB(&ip
->i_inode
), &al
->al_ri_gh
);
1477 error
= ea_foreach(ip
, ea_dealloc_unstuffed
, NULL
);
1481 if (ip
->i_diskflags
& GFS2_DIF_EA_INDIRECT
) {
1482 error
= ea_dealloc_indirect(ip
);
1487 error
= ea_dealloc_block(ip
);
1490 gfs2_glock_dq_uninit(&al
->al_ri_gh
);
1492 gfs2_quota_unhold(ip
);
1498 static int gfs2_xattr_user_get(struct inode
*inode
, const char *name
,
1499 void *buffer
, size_t size
)
1501 return gfs2_xattr_get(inode
, GFS2_EATYPE_USR
, name
, buffer
, size
);
1504 static int gfs2_xattr_user_set(struct inode
*inode
, const char *name
,
1505 const void *value
, size_t size
, int flags
)
1507 return gfs2_xattr_set(inode
, GFS2_EATYPE_USR
, name
, value
, size
, flags
);
1510 static int gfs2_xattr_system_get(struct inode
*inode
, const char *name
,
1511 void *buffer
, size_t size
)
1513 return gfs2_xattr_get(inode
, GFS2_EATYPE_SYS
, name
, buffer
, size
);
1516 static int gfs2_xattr_system_set(struct inode
*inode
, const char *name
,
1517 const void *value
, size_t size
, int flags
)
1519 return gfs2_xattr_set(inode
, GFS2_EATYPE_SYS
, name
, value
, size
, flags
);
1522 static int gfs2_xattr_security_get(struct inode
*inode
, const char *name
,
1523 void *buffer
, size_t size
)
1525 return gfs2_xattr_get(inode
, GFS2_EATYPE_SECURITY
, name
, buffer
, size
);
1528 static int gfs2_xattr_security_set(struct inode
*inode
, const char *name
,
1529 const void *value
, size_t size
, int flags
)
1531 return gfs2_xattr_set(inode
, GFS2_EATYPE_SECURITY
, name
, value
, size
, flags
);
1534 static struct xattr_handler gfs2_xattr_user_handler
= {
1535 .prefix
= XATTR_USER_PREFIX
,
1536 .get
= gfs2_xattr_user_get
,
1537 .set
= gfs2_xattr_user_set
,
1540 static struct xattr_handler gfs2_xattr_security_handler
= {
1541 .prefix
= XATTR_SECURITY_PREFIX
,
1542 .get
= gfs2_xattr_security_get
,
1543 .set
= gfs2_xattr_security_set
,
1546 static struct xattr_handler gfs2_xattr_system_handler
= {
1547 .prefix
= XATTR_SYSTEM_PREFIX
,
1548 .get
= gfs2_xattr_system_get
,
1549 .set
= gfs2_xattr_system_set
,
1552 struct xattr_handler
*gfs2_xattr_handlers
[] = {
1553 &gfs2_xattr_user_handler
,
1554 &gfs2_xattr_security_handler
,
1555 &gfs2_xattr_system_handler
,