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/posix_acl_xattr.h>
17 #include <linux/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
, unsigned int nsize
, size_t dsize
,
45 unsigned int jbsize
= sdp
->sd_jbsize
;
48 *size
= ALIGN(sizeof(struct gfs2_ea_header
) + nsize
+ dsize
, 8);
54 *size
= ALIGN(sizeof(struct gfs2_ea_header
) + nsize
+
55 (sizeof(__be64
) * DIV_ROUND_UP(dsize
, jbsize
)), 8);
60 static int ea_check_size(struct gfs2_sbd
*sdp
, unsigned int nsize
, size_t dsize
)
64 if (dsize
> GFS2_EA_MAX_DATA_LEN
)
67 ea_calc_size(sdp
, nsize
, dsize
, &size
);
69 /* This can only happen with 512 byte blocks */
70 if (size
> sdp
->sd_jbsize
)
76 typedef int (*ea_call_t
) (struct gfs2_inode
*ip
, struct buffer_head
*bh
,
77 struct gfs2_ea_header
*ea
,
78 struct gfs2_ea_header
*prev
, void *private);
80 static int ea_foreach_i(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
81 ea_call_t ea_call
, void *data
)
83 struct gfs2_ea_header
*ea
, *prev
= NULL
;
86 if (gfs2_metatype_check(GFS2_SB(&ip
->i_inode
), bh
, GFS2_METATYPE_EA
))
89 for (ea
= GFS2_EA_BH2FIRST(bh
);; prev
= ea
, ea
= GFS2_EA2NEXT(ea
)) {
90 if (!GFS2_EA_REC_LEN(ea
))
92 if (!(bh
->b_data
<= (char *)ea
&& (char *)GFS2_EA2NEXT(ea
) <=
93 bh
->b_data
+ bh
->b_size
))
95 if (!GFS2_EATYPE_VALID(ea
->ea_type
))
98 error
= ea_call(ip
, bh
, ea
, prev
, data
);
102 if (GFS2_EA_IS_LAST(ea
)) {
103 if ((char *)GFS2_EA2NEXT(ea
) !=
104 bh
->b_data
+ bh
->b_size
)
113 gfs2_consist_inode(ip
);
117 static int ea_foreach(struct gfs2_inode
*ip
, ea_call_t ea_call
, void *data
)
119 struct buffer_head
*bh
, *eabh
;
123 error
= gfs2_meta_read(ip
->i_gl
, ip
->i_eattr
, DIO_WAIT
, 0, &bh
);
127 if (!(ip
->i_diskflags
& GFS2_DIF_EA_INDIRECT
)) {
128 error
= ea_foreach_i(ip
, bh
, ea_call
, data
);
132 if (gfs2_metatype_check(GFS2_SB(&ip
->i_inode
), bh
, GFS2_METATYPE_IN
)) {
137 eablk
= (__be64
*)(bh
->b_data
+ sizeof(struct gfs2_meta_header
));
138 end
= eablk
+ GFS2_SB(&ip
->i_inode
)->sd_inptrs
;
140 for (; eablk
< end
; eablk
++) {
145 bn
= be64_to_cpu(*eablk
);
147 error
= gfs2_meta_read(ip
->i_gl
, bn
, DIO_WAIT
, 0, &eabh
);
150 error
= ea_foreach_i(ip
, eabh
, ea_call
, data
);
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;
173 if (ea
->ea_type
== GFS2_EATYPE_UNUSED
)
176 if (ea
->ea_type
== ef
->type
) {
177 if (ea
->ea_name_len
== ef
->namel
&&
178 !memcmp(GFS2_EA2NAME(ea
), ef
->name
, ea
->ea_name_len
)) {
179 struct gfs2_ea_location
*el
= ef
->ef_el
;
191 static int gfs2_ea_find(struct gfs2_inode
*ip
, int type
, const char *name
,
192 struct gfs2_ea_location
*el
)
199 ef
.namel
= strlen(name
);
202 memset(el
, 0, sizeof(struct gfs2_ea_location
));
204 error
= ea_foreach(ip
, ea_find_i
, &ef
);
212 * ea_dealloc_unstuffed -
219 * Take advantage of the fact that all unstuffed blocks are
220 * allocated from the same RG. But watch, this may not always
226 static int ea_dealloc_unstuffed(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
227 struct gfs2_ea_header
*ea
,
228 struct gfs2_ea_header
*prev
, void *private)
230 int *leave
= private;
231 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
232 struct gfs2_rgrpd
*rgd
;
233 struct gfs2_holder rg_gh
;
234 struct buffer_head
*dibh
;
238 unsigned int blen
= 0;
239 unsigned int blks
= 0;
243 error
= gfs2_rindex_update(sdp
);
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
);
260 rgd
= gfs2_blk2rgrpd(sdp
, bn
, 1);
262 gfs2_consist_inode(ip
);
266 error
= gfs2_glock_nq_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, &rg_gh
);
270 error
= gfs2_trans_begin(sdp
, rgd
->rd_length
+ RES_DINODE
+
271 RES_EATTR
+ RES_STATFS
+ RES_QUOTA
, blks
);
275 gfs2_trans_add_meta(ip
->i_gl
, bh
);
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 gfs2_add_inode_blocks(&ip
->i_inode
, -1);
296 gfs2_free_meta(ip
, bstart
, blen
);
298 if (prev
&& !leave
) {
301 len
= GFS2_EA_REC_LEN(prev
) + GFS2_EA_REC_LEN(ea
);
302 prev
->ea_rec_len
= cpu_to_be32(len
);
304 if (GFS2_EA_IS_LAST(ea
))
305 prev
->ea_flags
|= GFS2_EAFLAG_LAST
;
307 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
311 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
313 ip
->i_inode
.i_ctime
= current_time(&ip
->i_inode
);
314 gfs2_trans_add_meta(ip
->i_gl
, dibh
);
315 gfs2_dinode_out(ip
, dibh
->b_data
);
322 gfs2_glock_dq_uninit(&rg_gh
);
326 static int ea_remove_unstuffed(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
327 struct gfs2_ea_header
*ea
,
328 struct gfs2_ea_header
*prev
, int leave
)
332 error
= gfs2_rindex_update(GFS2_SB(&ip
->i_inode
));
336 error
= gfs2_quota_hold(ip
, NO_UID_QUOTA_CHANGE
, NO_GID_QUOTA_CHANGE
);
340 error
= ea_dealloc_unstuffed(ip
, bh
, ea
, prev
, (leave
) ? &error
: NULL
);
342 gfs2_quota_unhold(ip
);
348 struct gfs2_ea_request
*ei_er
;
349 unsigned int ei_size
;
352 static inline unsigned int gfs2_ea_strlen(struct gfs2_ea_header
*ea
)
354 switch (ea
->ea_type
) {
355 case GFS2_EATYPE_USR
:
356 return 5 + ea
->ea_name_len
+ 1;
357 case GFS2_EATYPE_SYS
:
358 return 7 + ea
->ea_name_len
+ 1;
359 case GFS2_EATYPE_SECURITY
:
360 return 9 + ea
->ea_name_len
+ 1;
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 switch (ea
->ea_type
) {
386 case GFS2_EATYPE_USR
:
390 case GFS2_EATYPE_SYS
:
394 case GFS2_EATYPE_SECURITY
:
395 prefix
= "security.";
402 memcpy(er
->er_data
+ ei
->ei_size
, prefix
, l
);
403 memcpy(er
->er_data
+ ei
->ei_size
+ l
, GFS2_EA2NAME(ea
),
405 memcpy(er
->er_data
+ ei
->ei_size
+ ea_size
- 1, &c
, 1);
408 ei
->ei_size
+= ea_size
;
414 * gfs2_listxattr - List gfs2 extended attributes
415 * @dentry: The dentry whose inode we are interested in
416 * @buffer: The buffer to write the results
417 * @size: The size of the buffer
419 * Returns: actual size of data on success, -errno on error
422 ssize_t
gfs2_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
424 struct gfs2_inode
*ip
= GFS2_I(d_inode(dentry
));
425 struct gfs2_ea_request er
;
426 struct gfs2_holder i_gh
;
429 memset(&er
, 0, sizeof(struct gfs2_ea_request
));
432 er
.er_data_len
= size
;
435 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &i_gh
);
440 struct ea_list ei
= { .ei_er
= &er
, .ei_size
= 0 };
442 error
= ea_foreach(ip
, ea_list_i
, &ei
);
447 gfs2_glock_dq_uninit(&i_gh
);
453 * ea_iter_unstuffed - copies the unstuffed xattr data to/from the
455 * @ip: The GFS2 inode
456 * @ea: The extended attribute header structure
457 * @din: The data to be copied in
458 * @dout: The data to be copied out (one of din,dout will be NULL)
463 static int gfs2_iter_unstuffed(struct gfs2_inode
*ip
, struct gfs2_ea_header
*ea
,
464 const char *din
, char *dout
)
466 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
467 struct buffer_head
**bh
;
468 unsigned int amount
= GFS2_EA_DATA_LEN(ea
);
469 unsigned int nptrs
= DIV_ROUND_UP(amount
, sdp
->sd_jbsize
);
470 __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, 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 pos
= bh
[x
]->b_data
+ sizeof(struct gfs2_meta_header
);
506 cp_size
= (sdp
->sd_jbsize
> amount
) ? amount
: sdp
->sd_jbsize
;
509 memcpy(dout
, pos
, cp_size
);
510 dout
+= sdp
->sd_jbsize
;
514 gfs2_trans_add_meta(ip
->i_gl
, bh
[x
]);
515 memcpy(pos
, din
, cp_size
);
516 din
+= sdp
->sd_jbsize
;
519 amount
-= sdp
->sd_jbsize
;
528 static int gfs2_ea_get_copy(struct gfs2_inode
*ip
, struct gfs2_ea_location
*el
,
529 char *data
, size_t size
)
532 size_t len
= GFS2_EA_DATA_LEN(el
->el_ea
);
536 if (GFS2_EA_IS_STUFFED(el
->el_ea
)) {
537 memcpy(data
, GFS2_EA2DATA(el
->el_ea
), len
);
540 ret
= gfs2_iter_unstuffed(ip
, el
->el_ea
, NULL
, data
);
546 int gfs2_xattr_acl_get(struct gfs2_inode
*ip
, const char *name
, char **ppdata
)
548 struct gfs2_ea_location el
;
553 error
= gfs2_ea_find(ip
, GFS2_EATYPE_SYS
, name
, &el
);
558 if (!GFS2_EA_DATA_LEN(el
.el_ea
))
561 len
= GFS2_EA_DATA_LEN(el
.el_ea
);
562 data
= kmalloc(len
, GFP_NOFS
);
567 error
= gfs2_ea_get_copy(ip
, &el
, data
, len
);
578 * gfs2_xattr_get - Get a GFS2 extended attribute
580 * @name: The name of the extended attribute
581 * @buffer: The buffer to write the result into
582 * @size: The size of the buffer
583 * @type: The type of extended attribute
585 * Returns: actual size of data on success, -errno on error
587 static int __gfs2_xattr_get(struct inode
*inode
, const char *name
,
588 void *buffer
, size_t size
, int type
)
590 struct gfs2_inode
*ip
= GFS2_I(inode
);
591 struct gfs2_ea_location el
;
596 if (strlen(name
) > GFS2_EA_MAX_NAME_LEN
)
599 error
= gfs2_ea_find(ip
, type
, name
, &el
);
605 error
= gfs2_ea_get_copy(ip
, &el
, buffer
, size
);
607 error
= GFS2_EA_DATA_LEN(el
.el_ea
);
613 static int gfs2_xattr_get(const struct xattr_handler
*handler
,
614 struct dentry
*unused
, struct inode
*inode
,
615 const char *name
, void *buffer
, size_t size
)
617 struct gfs2_inode
*ip
= GFS2_I(inode
);
618 struct gfs2_holder gh
;
619 bool need_unlock
= false;
622 /* During lookup, SELinux calls this function with the glock locked. */
624 if (!gfs2_glock_is_locked_by_me(ip
->i_gl
)) {
625 ret
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &gh
);
630 ret
= __gfs2_xattr_get(inode
, name
, buffer
, size
, handler
->flags
);
632 gfs2_glock_dq_uninit(&gh
);
637 * ea_alloc_blk - allocates a new block for extended attributes.
638 * @ip: A pointer to the inode that's getting extended attributes
639 * @bhp: Pointer to pointer to a struct buffer_head
644 static int ea_alloc_blk(struct gfs2_inode
*ip
, struct buffer_head
**bhp
)
646 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
647 struct gfs2_ea_header
*ea
;
652 error
= gfs2_alloc_blocks(ip
, &block
, &n
, 0, NULL
);
655 gfs2_trans_add_unrevoke(sdp
, block
, 1);
656 *bhp
= gfs2_meta_new(ip
->i_gl
, block
);
657 gfs2_trans_add_meta(ip
->i_gl
, *bhp
);
658 gfs2_metatype_set(*bhp
, GFS2_METATYPE_EA
, GFS2_FORMAT_EA
);
659 gfs2_buffer_clear_tail(*bhp
, sizeof(struct gfs2_meta_header
));
661 ea
= GFS2_EA_BH2FIRST(*bhp
);
662 ea
->ea_rec_len
= cpu_to_be32(sdp
->sd_jbsize
);
663 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
664 ea
->ea_flags
= GFS2_EAFLAG_LAST
;
667 gfs2_add_inode_blocks(&ip
->i_inode
, 1);
673 * ea_write - writes the request info to an ea, creating new blocks if
675 * @ip: inode that is being modified
676 * @ea: the location of the new ea in a block
677 * @er: the write request
679 * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
684 static int ea_write(struct gfs2_inode
*ip
, struct gfs2_ea_header
*ea
,
685 struct gfs2_ea_request
*er
)
687 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
690 ea
->ea_data_len
= cpu_to_be32(er
->er_data_len
);
691 ea
->ea_name_len
= er
->er_name_len
;
692 ea
->ea_type
= er
->er_type
;
695 memcpy(GFS2_EA2NAME(ea
), er
->er_name
, er
->er_name_len
);
697 if (GFS2_EAREQ_SIZE_STUFFED(er
) <= sdp
->sd_jbsize
) {
699 memcpy(GFS2_EA2DATA(ea
), er
->er_data
, er
->er_data_len
);
701 __be64
*dataptr
= GFS2_EA2DATAPTRS(ea
);
702 const char *data
= er
->er_data
;
703 unsigned int data_len
= er
->er_data_len
;
707 ea
->ea_num_ptrs
= DIV_ROUND_UP(er
->er_data_len
, sdp
->sd_jbsize
);
708 for (x
= 0; x
< ea
->ea_num_ptrs
; x
++) {
709 struct buffer_head
*bh
;
711 int mh_size
= sizeof(struct gfs2_meta_header
);
714 error
= gfs2_alloc_blocks(ip
, &block
, &n
, 0, NULL
);
717 gfs2_trans_add_unrevoke(sdp
, block
, 1);
718 bh
= gfs2_meta_new(ip
->i_gl
, block
);
719 gfs2_trans_add_meta(ip
->i_gl
, bh
);
720 gfs2_metatype_set(bh
, GFS2_METATYPE_ED
, GFS2_FORMAT_ED
);
722 gfs2_add_inode_blocks(&ip
->i_inode
, 1);
724 copy
= data_len
> sdp
->sd_jbsize
? sdp
->sd_jbsize
:
726 memcpy(bh
->b_data
+ mh_size
, data
, copy
);
727 if (copy
< sdp
->sd_jbsize
)
728 memset(bh
->b_data
+ mh_size
+ copy
, 0,
729 sdp
->sd_jbsize
- copy
);
731 *dataptr
++ = cpu_to_be64(bh
->b_blocknr
);
738 gfs2_assert_withdraw(sdp
, !data_len
);
744 typedef int (*ea_skeleton_call_t
) (struct gfs2_inode
*ip
,
745 struct gfs2_ea_request
*er
, void *private);
747 static int ea_alloc_skeleton(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
,
749 ea_skeleton_call_t skeleton_call
, void *private)
751 struct gfs2_alloc_parms ap
= { .target
= blks
};
752 struct buffer_head
*dibh
;
755 error
= gfs2_rindex_update(GFS2_SB(&ip
->i_inode
));
759 error
= gfs2_quota_lock_check(ip
, &ap
);
763 error
= gfs2_inplace_reserve(ip
, &ap
);
767 error
= gfs2_trans_begin(GFS2_SB(&ip
->i_inode
),
768 blks
+ gfs2_rg_blocks(ip
, blks
) +
769 RES_DINODE
+ RES_STATFS
+ RES_QUOTA
, 0);
773 error
= skeleton_call(ip
, er
, private);
777 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
779 ip
->i_inode
.i_ctime
= current_time(&ip
->i_inode
);
780 gfs2_trans_add_meta(ip
->i_gl
, dibh
);
781 gfs2_dinode_out(ip
, dibh
->b_data
);
786 gfs2_trans_end(GFS2_SB(&ip
->i_inode
));
788 gfs2_inplace_release(ip
);
790 gfs2_quota_unlock(ip
);
794 static int ea_init_i(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
,
797 struct buffer_head
*bh
;
800 error
= ea_alloc_blk(ip
, &bh
);
804 ip
->i_eattr
= bh
->b_blocknr
;
805 error
= ea_write(ip
, GFS2_EA_BH2FIRST(bh
), er
);
813 * ea_init - initializes a new eattr block
820 static int ea_init(struct gfs2_inode
*ip
, int type
, const char *name
,
821 const void *data
, size_t size
)
823 struct gfs2_ea_request er
;
824 unsigned int jbsize
= GFS2_SB(&ip
->i_inode
)->sd_jbsize
;
825 unsigned int blks
= 1;
829 er
.er_name_len
= strlen(name
);
830 er
.er_data
= (void *)data
;
831 er
.er_data_len
= size
;
833 if (GFS2_EAREQ_SIZE_STUFFED(&er
) > jbsize
)
834 blks
+= DIV_ROUND_UP(er
.er_data_len
, jbsize
);
836 return ea_alloc_skeleton(ip
, &er
, blks
, ea_init_i
, NULL
);
839 static struct gfs2_ea_header
*ea_split_ea(struct gfs2_ea_header
*ea
)
841 u32 ea_size
= GFS2_EA_SIZE(ea
);
842 struct gfs2_ea_header
*new = (struct gfs2_ea_header
*)((char *)ea
+
844 u32 new_size
= GFS2_EA_REC_LEN(ea
) - ea_size
;
845 int last
= ea
->ea_flags
& GFS2_EAFLAG_LAST
;
847 ea
->ea_rec_len
= cpu_to_be32(ea_size
);
848 ea
->ea_flags
^= last
;
850 new->ea_rec_len
= cpu_to_be32(new_size
);
851 new->ea_flags
= last
;
856 static void ea_set_remove_stuffed(struct gfs2_inode
*ip
,
857 struct gfs2_ea_location
*el
)
859 struct gfs2_ea_header
*ea
= el
->el_ea
;
860 struct gfs2_ea_header
*prev
= el
->el_prev
;
863 gfs2_trans_add_meta(ip
->i_gl
, el
->el_bh
);
865 if (!prev
|| !GFS2_EA_IS_STUFFED(ea
)) {
866 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
868 } else if (GFS2_EA2NEXT(prev
) != ea
) {
869 prev
= GFS2_EA2NEXT(prev
);
870 gfs2_assert_withdraw(GFS2_SB(&ip
->i_inode
), GFS2_EA2NEXT(prev
) == ea
);
873 len
= GFS2_EA_REC_LEN(prev
) + GFS2_EA_REC_LEN(ea
);
874 prev
->ea_rec_len
= cpu_to_be32(len
);
876 if (GFS2_EA_IS_LAST(ea
))
877 prev
->ea_flags
|= GFS2_EAFLAG_LAST
;
883 struct gfs2_ea_request
*es_er
;
884 struct gfs2_ea_location
*es_el
;
886 struct buffer_head
*es_bh
;
887 struct gfs2_ea_header
*es_ea
;
890 static int ea_set_simple_noalloc(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
891 struct gfs2_ea_header
*ea
, struct ea_set
*es
)
893 struct gfs2_ea_request
*er
= es
->es_er
;
894 struct buffer_head
*dibh
;
897 error
= gfs2_trans_begin(GFS2_SB(&ip
->i_inode
), RES_DINODE
+ 2 * RES_EATTR
, 0);
901 gfs2_trans_add_meta(ip
->i_gl
, bh
);
904 ea
= ea_split_ea(ea
);
906 ea_write(ip
, ea
, er
);
909 ea_set_remove_stuffed(ip
, es
->es_el
);
911 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
914 ip
->i_inode
.i_ctime
= current_time(&ip
->i_inode
);
915 gfs2_trans_add_meta(ip
->i_gl
, dibh
);
916 gfs2_dinode_out(ip
, dibh
->b_data
);
919 gfs2_trans_end(GFS2_SB(&ip
->i_inode
));
923 static int ea_set_simple_alloc(struct gfs2_inode
*ip
,
924 struct gfs2_ea_request
*er
, void *private)
926 struct ea_set
*es
= private;
927 struct gfs2_ea_header
*ea
= es
->es_ea
;
930 gfs2_trans_add_meta(ip
->i_gl
, es
->es_bh
);
933 ea
= ea_split_ea(ea
);
935 error
= ea_write(ip
, ea
, er
);
940 ea_set_remove_stuffed(ip
, es
->es_el
);
945 static int ea_set_simple(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
946 struct gfs2_ea_header
*ea
, struct gfs2_ea_header
*prev
,
949 struct ea_set
*es
= private;
954 stuffed
= ea_calc_size(GFS2_SB(&ip
->i_inode
), es
->es_er
->er_name_len
,
955 es
->es_er
->er_data_len
, &size
);
957 if (ea
->ea_type
== GFS2_EATYPE_UNUSED
) {
958 if (GFS2_EA_REC_LEN(ea
) < size
)
960 if (!GFS2_EA_IS_STUFFED(ea
)) {
961 error
= ea_remove_unstuffed(ip
, bh
, ea
, prev
, 1);
966 } else if (GFS2_EA_REC_LEN(ea
) - GFS2_EA_SIZE(ea
) >= size
)
972 error
= ea_set_simple_noalloc(ip
, bh
, ea
, es
);
980 blks
= 2 + DIV_ROUND_UP(es
->es_er
->er_data_len
,
981 GFS2_SB(&ip
->i_inode
)->sd_jbsize
);
983 error
= ea_alloc_skeleton(ip
, es
->es_er
, blks
,
984 ea_set_simple_alloc
, es
);
992 static int ea_set_block(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
,
995 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
996 struct buffer_head
*indbh
, *newbh
;
999 int mh_size
= sizeof(struct gfs2_meta_header
);
1001 if (ip
->i_diskflags
& GFS2_DIF_EA_INDIRECT
) {
1004 error
= gfs2_meta_read(ip
->i_gl
, ip
->i_eattr
, DIO_WAIT
, 0,
1009 if (gfs2_metatype_check(sdp
, indbh
, GFS2_METATYPE_IN
)) {
1014 eablk
= (__be64
*)(indbh
->b_data
+ mh_size
);
1015 end
= eablk
+ sdp
->sd_inptrs
;
1017 for (; eablk
< end
; eablk
++)
1026 gfs2_trans_add_meta(ip
->i_gl
, indbh
);
1030 error
= gfs2_alloc_blocks(ip
, &blk
, &n
, 0, NULL
);
1033 gfs2_trans_add_unrevoke(sdp
, blk
, 1);
1034 indbh
= gfs2_meta_new(ip
->i_gl
, blk
);
1035 gfs2_trans_add_meta(ip
->i_gl
, indbh
);
1036 gfs2_metatype_set(indbh
, GFS2_METATYPE_IN
, GFS2_FORMAT_IN
);
1037 gfs2_buffer_clear_tail(indbh
, mh_size
);
1039 eablk
= (__be64
*)(indbh
->b_data
+ mh_size
);
1040 *eablk
= cpu_to_be64(ip
->i_eattr
);
1042 ip
->i_diskflags
|= GFS2_DIF_EA_INDIRECT
;
1043 gfs2_add_inode_blocks(&ip
->i_inode
, 1);
1048 error
= ea_alloc_blk(ip
, &newbh
);
1052 *eablk
= cpu_to_be64((u64
)newbh
->b_blocknr
);
1053 error
= ea_write(ip
, GFS2_EA_BH2FIRST(newbh
), er
);
1059 ea_set_remove_stuffed(ip
, private);
1066 static int ea_set_i(struct gfs2_inode
*ip
, int type
, const char *name
,
1067 const void *value
, size_t size
, struct gfs2_ea_location
*el
)
1069 struct gfs2_ea_request er
;
1071 unsigned int blks
= 2;
1076 er
.er_data
= (void *)value
;
1077 er
.er_name_len
= strlen(name
);
1078 er
.er_data_len
= size
;
1080 memset(&es
, 0, sizeof(struct ea_set
));
1084 error
= ea_foreach(ip
, ea_set_simple
, &es
);
1090 if (!(ip
->i_diskflags
& GFS2_DIF_EA_INDIRECT
))
1092 if (GFS2_EAREQ_SIZE_STUFFED(&er
) > GFS2_SB(&ip
->i_inode
)->sd_jbsize
)
1093 blks
+= DIV_ROUND_UP(er
.er_data_len
, GFS2_SB(&ip
->i_inode
)->sd_jbsize
);
1095 return ea_alloc_skeleton(ip
, &er
, blks
, ea_set_block
, el
);
1098 static int ea_set_remove_unstuffed(struct gfs2_inode
*ip
,
1099 struct gfs2_ea_location
*el
)
1101 if (el
->el_prev
&& GFS2_EA2NEXT(el
->el_prev
) != el
->el_ea
) {
1102 el
->el_prev
= GFS2_EA2NEXT(el
->el_prev
);
1103 gfs2_assert_withdraw(GFS2_SB(&ip
->i_inode
),
1104 GFS2_EA2NEXT(el
->el_prev
) == el
->el_ea
);
1107 return ea_remove_unstuffed(ip
, el
->el_bh
, el
->el_ea
, el
->el_prev
, 0);
1110 static int ea_remove_stuffed(struct gfs2_inode
*ip
, struct gfs2_ea_location
*el
)
1112 struct gfs2_ea_header
*ea
= el
->el_ea
;
1113 struct gfs2_ea_header
*prev
= el
->el_prev
;
1114 struct buffer_head
*dibh
;
1117 error
= gfs2_trans_begin(GFS2_SB(&ip
->i_inode
), RES_DINODE
+ RES_EATTR
, 0);
1121 gfs2_trans_add_meta(ip
->i_gl
, el
->el_bh
);
1126 len
= GFS2_EA_REC_LEN(prev
) + GFS2_EA_REC_LEN(ea
);
1127 prev
->ea_rec_len
= cpu_to_be32(len
);
1129 if (GFS2_EA_IS_LAST(ea
))
1130 prev
->ea_flags
|= GFS2_EAFLAG_LAST
;
1132 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
1135 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1137 ip
->i_inode
.i_ctime
= current_time(&ip
->i_inode
);
1138 gfs2_trans_add_meta(ip
->i_gl
, dibh
);
1139 gfs2_dinode_out(ip
, dibh
->b_data
);
1143 gfs2_trans_end(GFS2_SB(&ip
->i_inode
));
1149 * gfs2_xattr_remove - Remove a GFS2 extended attribute
1151 * @type: The type of the extended attribute
1152 * @name: The name of the extended attribute
1154 * This is not called directly by the VFS since we use the (common)
1155 * scheme of making a "set with NULL data" mean a remove request. Note
1156 * that this is different from a set with zero length data.
1158 * Returns: 0, or errno on failure
1161 static int gfs2_xattr_remove(struct gfs2_inode
*ip
, int type
, const char *name
)
1163 struct gfs2_ea_location el
;
1169 error
= gfs2_ea_find(ip
, type
, name
, &el
);
1175 if (GFS2_EA_IS_STUFFED(el
.el_ea
))
1176 error
= ea_remove_stuffed(ip
, &el
);
1178 error
= ea_remove_unstuffed(ip
, el
.el_bh
, el
.el_ea
, el
.el_prev
, 0);
1186 * __gfs2_xattr_set - Set (or remove) a GFS2 extended attribute
1188 * @name: The name of the extended attribute
1189 * @value: The value of the extended attribute (NULL for remove)
1190 * @size: The size of the @value argument
1191 * @flags: Create or Replace
1192 * @type: The type of the extended attribute
1194 * See gfs2_xattr_remove() for details of the removal of xattrs.
1196 * Returns: 0 or errno on failure
1199 int __gfs2_xattr_set(struct inode
*inode
, const char *name
,
1200 const void *value
, size_t size
, int flags
, int type
)
1202 struct gfs2_inode
*ip
= GFS2_I(inode
);
1203 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1204 struct gfs2_ea_location el
;
1205 unsigned int namel
= strlen(name
);
1208 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
1210 if (namel
> GFS2_EA_MAX_NAME_LEN
)
1213 if (value
== NULL
) {
1214 error
= gfs2_xattr_remove(ip
, type
, name
);
1215 if (error
== -ENODATA
&& !(flags
& XATTR_REPLACE
))
1220 if (ea_check_size(sdp
, namel
, size
))
1224 if (flags
& XATTR_REPLACE
)
1226 return ea_init(ip
, type
, name
, value
, size
);
1229 error
= gfs2_ea_find(ip
, type
, name
, &el
);
1234 if (ip
->i_diskflags
& GFS2_DIF_APPENDONLY
) {
1240 if (!(flags
& XATTR_CREATE
)) {
1241 int unstuffed
= !GFS2_EA_IS_STUFFED(el
.el_ea
);
1242 error
= ea_set_i(ip
, type
, name
, value
, size
, &el
);
1243 if (!error
&& unstuffed
)
1244 ea_set_remove_unstuffed(ip
, &el
);
1252 if (!(flags
& XATTR_REPLACE
))
1253 error
= ea_set_i(ip
, type
, name
, value
, size
, NULL
);
1258 static int gfs2_xattr_set(const struct xattr_handler
*handler
,
1259 struct dentry
*unused
, struct inode
*inode
,
1260 const char *name
, const void *value
,
1261 size_t size
, int flags
)
1263 struct gfs2_inode
*ip
= GFS2_I(inode
);
1264 struct gfs2_holder gh
;
1267 ret
= gfs2_rsqa_alloc(ip
);
1271 ret
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &gh
);
1274 ret
= __gfs2_xattr_set(inode
, name
, value
, size
, flags
, handler
->flags
);
1275 gfs2_glock_dq_uninit(&gh
);
1279 static int ea_dealloc_indirect(struct gfs2_inode
*ip
)
1281 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1282 struct gfs2_rgrp_list rlist
;
1283 struct buffer_head
*indbh
, *dibh
;
1284 __be64
*eablk
, *end
;
1285 unsigned int rg_blocks
= 0;
1287 unsigned int blen
= 0;
1288 unsigned int blks
= 0;
1292 error
= gfs2_rindex_update(sdp
);
1296 memset(&rlist
, 0, sizeof(struct gfs2_rgrp_list
));
1298 error
= gfs2_meta_read(ip
->i_gl
, ip
->i_eattr
, DIO_WAIT
, 0, &indbh
);
1302 if (gfs2_metatype_check(sdp
, indbh
, GFS2_METATYPE_IN
)) {
1307 eablk
= (__be64
*)(indbh
->b_data
+ sizeof(struct gfs2_meta_header
));
1308 end
= eablk
+ sdp
->sd_inptrs
;
1310 for (; eablk
< end
; eablk
++) {
1315 bn
= be64_to_cpu(*eablk
);
1317 if (bstart
+ blen
== bn
)
1321 gfs2_rlist_add(ip
, &rlist
, bstart
);
1328 gfs2_rlist_add(ip
, &rlist
, bstart
);
1332 gfs2_rlist_alloc(&rlist
, LM_ST_EXCLUSIVE
);
1334 for (x
= 0; x
< rlist
.rl_rgrps
; x
++) {
1335 struct gfs2_rgrpd
*rgd
= gfs2_glock2rgrp(rlist
.rl_ghs
[x
].gh_gl
);
1337 rg_blocks
+= rgd
->rd_length
;
1340 error
= gfs2_glock_nq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1342 goto out_rlist_free
;
1344 error
= gfs2_trans_begin(sdp
, rg_blocks
+ RES_DINODE
+ RES_INDIRECT
+
1345 RES_STATFS
+ RES_QUOTA
, blks
);
1349 gfs2_trans_add_meta(ip
->i_gl
, indbh
);
1351 eablk
= (__be64
*)(indbh
->b_data
+ sizeof(struct gfs2_meta_header
));
1355 for (; eablk
< end
; eablk
++) {
1360 bn
= be64_to_cpu(*eablk
);
1362 if (bstart
+ blen
== bn
)
1366 gfs2_free_meta(ip
, bstart
, blen
);
1372 gfs2_add_inode_blocks(&ip
->i_inode
, -1);
1375 gfs2_free_meta(ip
, bstart
, blen
);
1377 ip
->i_diskflags
&= ~GFS2_DIF_EA_INDIRECT
;
1379 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1381 gfs2_trans_add_meta(ip
->i_gl
, dibh
);
1382 gfs2_dinode_out(ip
, dibh
->b_data
);
1386 gfs2_trans_end(sdp
);
1389 gfs2_glock_dq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1391 gfs2_rlist_free(&rlist
);
1397 static int ea_dealloc_block(struct gfs2_inode
*ip
)
1399 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1400 struct gfs2_rgrpd
*rgd
;
1401 struct buffer_head
*dibh
;
1402 struct gfs2_holder gh
;
1405 error
= gfs2_rindex_update(sdp
);
1409 rgd
= gfs2_blk2rgrpd(sdp
, ip
->i_eattr
, 1);
1411 gfs2_consist_inode(ip
);
1415 error
= gfs2_glock_nq_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, &gh
);
1419 error
= gfs2_trans_begin(sdp
, RES_RG_BIT
+ RES_DINODE
+ RES_STATFS
+
1424 gfs2_free_meta(ip
, ip
->i_eattr
, 1);
1427 gfs2_add_inode_blocks(&ip
->i_inode
, -1);
1429 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1431 gfs2_trans_add_meta(ip
->i_gl
, dibh
);
1432 gfs2_dinode_out(ip
, dibh
->b_data
);
1436 gfs2_trans_end(sdp
);
1439 gfs2_glock_dq_uninit(&gh
);
1444 * gfs2_ea_dealloc - deallocate the extended attribute fork
1450 int gfs2_ea_dealloc(struct gfs2_inode
*ip
)
1454 error
= gfs2_rindex_update(GFS2_SB(&ip
->i_inode
));
1458 error
= gfs2_quota_hold(ip
, NO_UID_QUOTA_CHANGE
, NO_GID_QUOTA_CHANGE
);
1462 error
= ea_foreach(ip
, ea_dealloc_unstuffed
, NULL
);
1466 if (ip
->i_diskflags
& GFS2_DIF_EA_INDIRECT
) {
1467 error
= ea_dealloc_indirect(ip
);
1472 error
= ea_dealloc_block(ip
);
1475 gfs2_quota_unhold(ip
);
1479 static const struct xattr_handler gfs2_xattr_user_handler
= {
1480 .prefix
= XATTR_USER_PREFIX
,
1481 .flags
= GFS2_EATYPE_USR
,
1482 .get
= gfs2_xattr_get
,
1483 .set
= gfs2_xattr_set
,
1486 static const struct xattr_handler gfs2_xattr_security_handler
= {
1487 .prefix
= XATTR_SECURITY_PREFIX
,
1488 .flags
= GFS2_EATYPE_SECURITY
,
1489 .get
= gfs2_xattr_get
,
1490 .set
= gfs2_xattr_set
,
1493 const struct xattr_handler
*gfs2_xattr_handlers
[] = {
1494 &gfs2_xattr_user_handler
,
1495 &gfs2_xattr_security_handler
,
1496 &posix_acl_access_xattr_handler
,
1497 &posix_acl_default_xattr_handler
,