1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
4 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
7 #include <linux/slab.h>
8 #include <linux/spinlock.h>
9 #include <linux/completion.h>
10 #include <linux/buffer_head.h>
11 #include <linux/xattr.h>
12 #include <linux/gfs2_ondisk.h>
13 #include <linux/posix_acl_xattr.h>
14 #include <linux/uaccess.h>
30 * ea_calc_size - returns the acutal number of bytes the request will take up
31 * (not counting any unstuffed data blocks)
36 * Returns: 1 if the EA should be stuffed
39 static int ea_calc_size(struct gfs2_sbd
*sdp
, unsigned int nsize
, size_t dsize
,
42 unsigned int jbsize
= sdp
->sd_jbsize
;
45 *size
= ALIGN(sizeof(struct gfs2_ea_header
) + nsize
+ dsize
, 8);
51 *size
= ALIGN(sizeof(struct gfs2_ea_header
) + nsize
+
52 (sizeof(__be64
) * DIV_ROUND_UP(dsize
, jbsize
)), 8);
57 static int ea_check_size(struct gfs2_sbd
*sdp
, unsigned int nsize
, size_t dsize
)
61 if (dsize
> GFS2_EA_MAX_DATA_LEN
)
64 ea_calc_size(sdp
, nsize
, dsize
, &size
);
66 /* This can only happen with 512 byte blocks */
67 if (size
> sdp
->sd_jbsize
)
73 typedef int (*ea_call_t
) (struct gfs2_inode
*ip
, struct buffer_head
*bh
,
74 struct gfs2_ea_header
*ea
,
75 struct gfs2_ea_header
*prev
, void *private);
77 static int ea_foreach_i(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
78 ea_call_t ea_call
, void *data
)
80 struct gfs2_ea_header
*ea
, *prev
= NULL
;
83 if (gfs2_metatype_check(GFS2_SB(&ip
->i_inode
), bh
, GFS2_METATYPE_EA
))
86 for (ea
= GFS2_EA_BH2FIRST(bh
);; prev
= ea
, ea
= GFS2_EA2NEXT(ea
)) {
87 if (!GFS2_EA_REC_LEN(ea
))
89 if (!(bh
->b_data
<= (char *)ea
&& (char *)GFS2_EA2NEXT(ea
) <=
90 bh
->b_data
+ bh
->b_size
))
92 if (!GFS2_EATYPE_VALID(ea
->ea_type
))
95 error
= ea_call(ip
, bh
, ea
, prev
, data
);
99 if (GFS2_EA_IS_LAST(ea
)) {
100 if ((char *)GFS2_EA2NEXT(ea
) !=
101 bh
->b_data
+ bh
->b_size
)
110 gfs2_consist_inode(ip
);
114 static int ea_foreach(struct gfs2_inode
*ip
, ea_call_t ea_call
, void *data
)
116 struct buffer_head
*bh
, *eabh
;
120 error
= gfs2_meta_read(ip
->i_gl
, ip
->i_eattr
, DIO_WAIT
, 0, &bh
);
124 if (!(ip
->i_diskflags
& GFS2_DIF_EA_INDIRECT
)) {
125 error
= ea_foreach_i(ip
, bh
, ea_call
, data
);
129 if (gfs2_metatype_check(GFS2_SB(&ip
->i_inode
), bh
, GFS2_METATYPE_IN
)) {
134 eablk
= (__be64
*)(bh
->b_data
+ sizeof(struct gfs2_meta_header
));
135 end
= eablk
+ GFS2_SB(&ip
->i_inode
)->sd_inptrs
;
137 for (; eablk
< end
; eablk
++) {
142 bn
= be64_to_cpu(*eablk
);
144 error
= gfs2_meta_read(ip
->i_gl
, bn
, DIO_WAIT
, 0, &eabh
);
147 error
= ea_foreach_i(ip
, eabh
, ea_call
, data
);
161 struct gfs2_ea_location
*ef_el
;
164 static int ea_find_i(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
165 struct gfs2_ea_header
*ea
, struct gfs2_ea_header
*prev
,
168 struct ea_find
*ef
= private;
170 if (ea
->ea_type
== GFS2_EATYPE_UNUSED
)
173 if (ea
->ea_type
== ef
->type
) {
174 if (ea
->ea_name_len
== ef
->namel
&&
175 !memcmp(GFS2_EA2NAME(ea
), ef
->name
, ea
->ea_name_len
)) {
176 struct gfs2_ea_location
*el
= ef
->ef_el
;
188 static int gfs2_ea_find(struct gfs2_inode
*ip
, int type
, const char *name
,
189 struct gfs2_ea_location
*el
)
196 ef
.namel
= strlen(name
);
199 memset(el
, 0, sizeof(struct gfs2_ea_location
));
201 error
= ea_foreach(ip
, ea_find_i
, &ef
);
209 * ea_dealloc_unstuffed -
216 * Take advantage of the fact that all unstuffed blocks are
217 * allocated from the same RG. But watch, this may not always
223 static int ea_dealloc_unstuffed(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
224 struct gfs2_ea_header
*ea
,
225 struct gfs2_ea_header
*prev
, void *private)
227 int *leave
= private;
228 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
229 struct gfs2_rgrpd
*rgd
;
230 struct gfs2_holder rg_gh
;
234 unsigned int blen
= 0;
235 unsigned int blks
= 0;
239 error
= gfs2_rindex_update(sdp
);
243 if (GFS2_EA_IS_STUFFED(ea
))
246 dataptrs
= GFS2_EA2DATAPTRS(ea
);
247 for (x
= 0; x
< ea
->ea_num_ptrs
; x
++, dataptrs
++) {
250 bn
= be64_to_cpu(*dataptrs
);
256 rgd
= gfs2_blk2rgrpd(sdp
, bn
, 1);
258 gfs2_consist_inode(ip
);
262 error
= gfs2_glock_nq_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, &rg_gh
);
266 error
= gfs2_trans_begin(sdp
, rgd
->rd_length
+ RES_DINODE
+
267 RES_EATTR
+ RES_STATFS
+ RES_QUOTA
, blks
);
271 gfs2_trans_add_meta(ip
->i_gl
, bh
);
273 dataptrs
= GFS2_EA2DATAPTRS(ea
);
274 for (x
= 0; x
< ea
->ea_num_ptrs
; x
++, dataptrs
++) {
277 bn
= be64_to_cpu(*dataptrs
);
279 if (bstart
+ blen
== bn
)
283 gfs2_free_meta(ip
, rgd
, bstart
, blen
);
289 gfs2_add_inode_blocks(&ip
->i_inode
, -1);
292 gfs2_free_meta(ip
, rgd
, bstart
, blen
);
294 if (prev
&& !leave
) {
297 len
= GFS2_EA_REC_LEN(prev
) + GFS2_EA_REC_LEN(ea
);
298 prev
->ea_rec_len
= cpu_to_be32(len
);
300 if (GFS2_EA_IS_LAST(ea
))
301 prev
->ea_flags
|= GFS2_EAFLAG_LAST
;
303 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
307 ip
->i_inode
.i_ctime
= current_time(&ip
->i_inode
);
308 __mark_inode_dirty(&ip
->i_inode
, I_DIRTY_DATASYNC
);
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
)
323 error
= gfs2_rindex_update(GFS2_SB(&ip
->i_inode
));
327 error
= gfs2_quota_hold(ip
, NO_UID_QUOTA_CHANGE
, NO_GID_QUOTA_CHANGE
);
331 error
= ea_dealloc_unstuffed(ip
, bh
, ea
, prev
, (leave
) ? &error
: NULL
);
333 gfs2_quota_unhold(ip
);
339 struct gfs2_ea_request
*ei_er
;
340 unsigned int ei_size
;
343 static int ea_list_i(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
344 struct gfs2_ea_header
*ea
, struct gfs2_ea_header
*prev
,
347 struct ea_list
*ei
= private;
348 struct gfs2_ea_request
*er
= ei
->ei_er
;
349 unsigned int ea_size
;
353 if (ea
->ea_type
== GFS2_EATYPE_UNUSED
)
356 switch (ea
->ea_type
) {
357 case GFS2_EATYPE_USR
:
361 case GFS2_EATYPE_SYS
:
365 case GFS2_EATYPE_SECURITY
:
366 prefix
= "security.";
373 ea_size
= l
+ ea
->ea_name_len
+ 1;
374 if (er
->er_data_len
) {
375 if (ei
->ei_size
+ ea_size
> er
->er_data_len
)
378 memcpy(er
->er_data
+ ei
->ei_size
, prefix
, l
);
379 memcpy(er
->er_data
+ ei
->ei_size
+ l
, GFS2_EA2NAME(ea
),
381 er
->er_data
[ei
->ei_size
+ ea_size
- 1] = 0;
384 ei
->ei_size
+= ea_size
;
390 * gfs2_listxattr - List gfs2 extended attributes
391 * @dentry: The dentry whose inode we are interested in
392 * @buffer: The buffer to write the results
393 * @size: The size of the buffer
395 * Returns: actual size of data on success, -errno on error
398 ssize_t
gfs2_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
400 struct gfs2_inode
*ip
= GFS2_I(d_inode(dentry
));
401 struct gfs2_ea_request er
;
402 struct gfs2_holder i_gh
;
405 memset(&er
, 0, sizeof(struct gfs2_ea_request
));
408 er
.er_data_len
= size
;
411 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &i_gh
);
416 struct ea_list ei
= { .ei_er
= &er
, .ei_size
= 0 };
418 error
= ea_foreach(ip
, ea_list_i
, &ei
);
423 gfs2_glock_dq_uninit(&i_gh
);
429 * ea_iter_unstuffed - copies the unstuffed xattr data to/from the
431 * @ip: The GFS2 inode
432 * @ea: The extended attribute header structure
433 * @din: The data to be copied in
434 * @dout: The data to be copied out (one of din,dout will be NULL)
439 static int gfs2_iter_unstuffed(struct gfs2_inode
*ip
, struct gfs2_ea_header
*ea
,
440 const char *din
, char *dout
)
442 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
443 struct buffer_head
**bh
;
444 unsigned int amount
= GFS2_EA_DATA_LEN(ea
);
445 unsigned int nptrs
= DIV_ROUND_UP(amount
, sdp
->sd_jbsize
);
446 __be64
*dataptrs
= GFS2_EA2DATAPTRS(ea
);
452 bh
= kcalloc(nptrs
, sizeof(struct buffer_head
*), GFP_NOFS
);
456 for (x
= 0; x
< nptrs
; x
++) {
457 error
= gfs2_meta_read(ip
->i_gl
, be64_to_cpu(*dataptrs
), 0, 0,
467 for (x
= 0; x
< nptrs
; x
++) {
468 error
= gfs2_meta_wait(sdp
, bh
[x
]);
470 for (; x
< nptrs
; x
++)
474 if (gfs2_metatype_check(sdp
, bh
[x
], GFS2_METATYPE_ED
)) {
475 for (; x
< nptrs
; x
++)
481 pos
= bh
[x
]->b_data
+ sizeof(struct gfs2_meta_header
);
482 cp_size
= (sdp
->sd_jbsize
> amount
) ? amount
: sdp
->sd_jbsize
;
485 memcpy(dout
, pos
, cp_size
);
486 dout
+= sdp
->sd_jbsize
;
490 gfs2_trans_add_meta(ip
->i_gl
, bh
[x
]);
491 memcpy(pos
, din
, cp_size
);
492 din
+= sdp
->sd_jbsize
;
495 amount
-= sdp
->sd_jbsize
;
504 static int gfs2_ea_get_copy(struct gfs2_inode
*ip
, struct gfs2_ea_location
*el
,
505 char *data
, size_t size
)
508 size_t len
= GFS2_EA_DATA_LEN(el
->el_ea
);
512 if (GFS2_EA_IS_STUFFED(el
->el_ea
)) {
513 memcpy(data
, GFS2_EA2DATA(el
->el_ea
), len
);
516 ret
= gfs2_iter_unstuffed(ip
, el
->el_ea
, NULL
, data
);
522 int gfs2_xattr_acl_get(struct gfs2_inode
*ip
, const char *name
, char **ppdata
)
524 struct gfs2_ea_location el
;
529 error
= gfs2_ea_find(ip
, GFS2_EATYPE_SYS
, name
, &el
);
534 if (!GFS2_EA_DATA_LEN(el
.el_ea
))
537 len
= GFS2_EA_DATA_LEN(el
.el_ea
);
538 data
= kmalloc(len
, GFP_NOFS
);
543 error
= gfs2_ea_get_copy(ip
, &el
, data
, len
);
554 * gfs2_xattr_get - Get a GFS2 extended attribute
556 * @name: The name of the extended attribute
557 * @buffer: The buffer to write the result into
558 * @size: The size of the buffer
559 * @type: The type of extended attribute
561 * Returns: actual size of data on success, -errno on error
563 static int __gfs2_xattr_get(struct inode
*inode
, const char *name
,
564 void *buffer
, size_t size
, int type
)
566 struct gfs2_inode
*ip
= GFS2_I(inode
);
567 struct gfs2_ea_location el
;
572 if (strlen(name
) > GFS2_EA_MAX_NAME_LEN
)
575 error
= gfs2_ea_find(ip
, type
, name
, &el
);
581 error
= gfs2_ea_get_copy(ip
, &el
, buffer
, size
);
583 error
= GFS2_EA_DATA_LEN(el
.el_ea
);
589 static int gfs2_xattr_get(const struct xattr_handler
*handler
,
590 struct dentry
*unused
, struct inode
*inode
,
591 const char *name
, void *buffer
, size_t size
)
593 struct gfs2_inode
*ip
= GFS2_I(inode
);
594 struct gfs2_holder gh
;
597 /* During lookup, SELinux calls this function with the glock locked. */
599 if (!gfs2_glock_is_locked_by_me(ip
->i_gl
)) {
600 ret
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &gh
);
604 gfs2_holder_mark_uninitialized(&gh
);
606 ret
= __gfs2_xattr_get(inode
, name
, buffer
, size
, handler
->flags
);
607 if (gfs2_holder_initialized(&gh
))
608 gfs2_glock_dq_uninit(&gh
);
613 * ea_alloc_blk - allocates a new block for extended attributes.
614 * @ip: A pointer to the inode that's getting extended attributes
615 * @bhp: Pointer to pointer to a struct buffer_head
620 static int ea_alloc_blk(struct gfs2_inode
*ip
, struct buffer_head
**bhp
)
622 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
623 struct gfs2_ea_header
*ea
;
628 error
= gfs2_alloc_blocks(ip
, &block
, &n
, 0, NULL
);
631 gfs2_trans_remove_revoke(sdp
, block
, 1);
632 *bhp
= gfs2_meta_new(ip
->i_gl
, block
);
633 gfs2_trans_add_meta(ip
->i_gl
, *bhp
);
634 gfs2_metatype_set(*bhp
, GFS2_METATYPE_EA
, GFS2_FORMAT_EA
);
635 gfs2_buffer_clear_tail(*bhp
, sizeof(struct gfs2_meta_header
));
637 ea
= GFS2_EA_BH2FIRST(*bhp
);
638 ea
->ea_rec_len
= cpu_to_be32(sdp
->sd_jbsize
);
639 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
640 ea
->ea_flags
= GFS2_EAFLAG_LAST
;
643 gfs2_add_inode_blocks(&ip
->i_inode
, 1);
649 * ea_write - writes the request info to an ea, creating new blocks if
651 * @ip: inode that is being modified
652 * @ea: the location of the new ea in a block
653 * @er: the write request
655 * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
660 static int ea_write(struct gfs2_inode
*ip
, struct gfs2_ea_header
*ea
,
661 struct gfs2_ea_request
*er
)
663 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
666 ea
->ea_data_len
= cpu_to_be32(er
->er_data_len
);
667 ea
->ea_name_len
= er
->er_name_len
;
668 ea
->ea_type
= er
->er_type
;
671 memcpy(GFS2_EA2NAME(ea
), er
->er_name
, er
->er_name_len
);
673 if (GFS2_EAREQ_SIZE_STUFFED(er
) <= sdp
->sd_jbsize
) {
675 memcpy(GFS2_EA2DATA(ea
), er
->er_data
, er
->er_data_len
);
677 __be64
*dataptr
= GFS2_EA2DATAPTRS(ea
);
678 const char *data
= er
->er_data
;
679 unsigned int data_len
= er
->er_data_len
;
683 ea
->ea_num_ptrs
= DIV_ROUND_UP(er
->er_data_len
, sdp
->sd_jbsize
);
684 for (x
= 0; x
< ea
->ea_num_ptrs
; x
++) {
685 struct buffer_head
*bh
;
687 int mh_size
= sizeof(struct gfs2_meta_header
);
690 error
= gfs2_alloc_blocks(ip
, &block
, &n
, 0, NULL
);
693 gfs2_trans_remove_revoke(sdp
, block
, 1);
694 bh
= gfs2_meta_new(ip
->i_gl
, block
);
695 gfs2_trans_add_meta(ip
->i_gl
, bh
);
696 gfs2_metatype_set(bh
, GFS2_METATYPE_ED
, GFS2_FORMAT_ED
);
698 gfs2_add_inode_blocks(&ip
->i_inode
, 1);
700 copy
= data_len
> sdp
->sd_jbsize
? sdp
->sd_jbsize
:
702 memcpy(bh
->b_data
+ mh_size
, data
, copy
);
703 if (copy
< sdp
->sd_jbsize
)
704 memset(bh
->b_data
+ mh_size
+ copy
, 0,
705 sdp
->sd_jbsize
- copy
);
707 *dataptr
++ = cpu_to_be64(bh
->b_blocknr
);
714 gfs2_assert_withdraw(sdp
, !data_len
);
720 typedef int (*ea_skeleton_call_t
) (struct gfs2_inode
*ip
,
721 struct gfs2_ea_request
*er
, void *private);
723 static int ea_alloc_skeleton(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
,
725 ea_skeleton_call_t skeleton_call
, void *private)
727 struct gfs2_alloc_parms ap
= { .target
= blks
};
730 error
= gfs2_rindex_update(GFS2_SB(&ip
->i_inode
));
734 error
= gfs2_quota_lock_check(ip
, &ap
);
738 error
= gfs2_inplace_reserve(ip
, &ap
);
742 error
= gfs2_trans_begin(GFS2_SB(&ip
->i_inode
),
743 blks
+ gfs2_rg_blocks(ip
, blks
) +
744 RES_DINODE
+ RES_STATFS
+ RES_QUOTA
, 0);
748 error
= skeleton_call(ip
, er
, private);
752 ip
->i_inode
.i_ctime
= current_time(&ip
->i_inode
);
753 __mark_inode_dirty(&ip
->i_inode
, I_DIRTY_DATASYNC
);
756 gfs2_trans_end(GFS2_SB(&ip
->i_inode
));
758 gfs2_inplace_release(ip
);
760 gfs2_quota_unlock(ip
);
764 static int ea_init_i(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
,
767 struct buffer_head
*bh
;
770 error
= ea_alloc_blk(ip
, &bh
);
774 ip
->i_eattr
= bh
->b_blocknr
;
775 error
= ea_write(ip
, GFS2_EA_BH2FIRST(bh
), er
);
783 * ea_init - initializes a new eattr block
790 static int ea_init(struct gfs2_inode
*ip
, int type
, const char *name
,
791 const void *data
, size_t size
)
793 struct gfs2_ea_request er
;
794 unsigned int jbsize
= GFS2_SB(&ip
->i_inode
)->sd_jbsize
;
795 unsigned int blks
= 1;
799 er
.er_name_len
= strlen(name
);
800 er
.er_data
= (void *)data
;
801 er
.er_data_len
= size
;
803 if (GFS2_EAREQ_SIZE_STUFFED(&er
) > jbsize
)
804 blks
+= DIV_ROUND_UP(er
.er_data_len
, jbsize
);
806 return ea_alloc_skeleton(ip
, &er
, blks
, ea_init_i
, NULL
);
809 static struct gfs2_ea_header
*ea_split_ea(struct gfs2_ea_header
*ea
)
811 u32 ea_size
= GFS2_EA_SIZE(ea
);
812 struct gfs2_ea_header
*new = (struct gfs2_ea_header
*)((char *)ea
+
814 u32 new_size
= GFS2_EA_REC_LEN(ea
) - ea_size
;
815 int last
= ea
->ea_flags
& GFS2_EAFLAG_LAST
;
817 ea
->ea_rec_len
= cpu_to_be32(ea_size
);
818 ea
->ea_flags
^= last
;
820 new->ea_rec_len
= cpu_to_be32(new_size
);
821 new->ea_flags
= last
;
826 static void ea_set_remove_stuffed(struct gfs2_inode
*ip
,
827 struct gfs2_ea_location
*el
)
829 struct gfs2_ea_header
*ea
= el
->el_ea
;
830 struct gfs2_ea_header
*prev
= el
->el_prev
;
833 gfs2_trans_add_meta(ip
->i_gl
, el
->el_bh
);
835 if (!prev
|| !GFS2_EA_IS_STUFFED(ea
)) {
836 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
838 } else if (GFS2_EA2NEXT(prev
) != ea
) {
839 prev
= GFS2_EA2NEXT(prev
);
840 gfs2_assert_withdraw(GFS2_SB(&ip
->i_inode
), GFS2_EA2NEXT(prev
) == ea
);
843 len
= GFS2_EA_REC_LEN(prev
) + GFS2_EA_REC_LEN(ea
);
844 prev
->ea_rec_len
= cpu_to_be32(len
);
846 if (GFS2_EA_IS_LAST(ea
))
847 prev
->ea_flags
|= GFS2_EAFLAG_LAST
;
853 struct gfs2_ea_request
*es_er
;
854 struct gfs2_ea_location
*es_el
;
856 struct buffer_head
*es_bh
;
857 struct gfs2_ea_header
*es_ea
;
860 static int ea_set_simple_noalloc(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
861 struct gfs2_ea_header
*ea
, struct ea_set
*es
)
863 struct gfs2_ea_request
*er
= es
->es_er
;
866 error
= gfs2_trans_begin(GFS2_SB(&ip
->i_inode
), RES_DINODE
+ 2 * RES_EATTR
, 0);
870 gfs2_trans_add_meta(ip
->i_gl
, bh
);
873 ea
= ea_split_ea(ea
);
875 ea_write(ip
, ea
, er
);
878 ea_set_remove_stuffed(ip
, es
->es_el
);
880 ip
->i_inode
.i_ctime
= current_time(&ip
->i_inode
);
881 __mark_inode_dirty(&ip
->i_inode
, I_DIRTY_DATASYNC
);
883 gfs2_trans_end(GFS2_SB(&ip
->i_inode
));
887 static int ea_set_simple_alloc(struct gfs2_inode
*ip
,
888 struct gfs2_ea_request
*er
, void *private)
890 struct ea_set
*es
= private;
891 struct gfs2_ea_header
*ea
= es
->es_ea
;
894 gfs2_trans_add_meta(ip
->i_gl
, es
->es_bh
);
897 ea
= ea_split_ea(ea
);
899 error
= ea_write(ip
, ea
, er
);
904 ea_set_remove_stuffed(ip
, es
->es_el
);
909 static int ea_set_simple(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
910 struct gfs2_ea_header
*ea
, struct gfs2_ea_header
*prev
,
913 struct ea_set
*es
= private;
918 stuffed
= ea_calc_size(GFS2_SB(&ip
->i_inode
), es
->es_er
->er_name_len
,
919 es
->es_er
->er_data_len
, &size
);
921 if (ea
->ea_type
== GFS2_EATYPE_UNUSED
) {
922 if (GFS2_EA_REC_LEN(ea
) < size
)
924 if (!GFS2_EA_IS_STUFFED(ea
)) {
925 error
= ea_remove_unstuffed(ip
, bh
, ea
, prev
, 1);
930 } else if (GFS2_EA_REC_LEN(ea
) - GFS2_EA_SIZE(ea
) >= size
)
936 error
= ea_set_simple_noalloc(ip
, bh
, ea
, es
);
944 blks
= 2 + DIV_ROUND_UP(es
->es_er
->er_data_len
,
945 GFS2_SB(&ip
->i_inode
)->sd_jbsize
);
947 error
= ea_alloc_skeleton(ip
, es
->es_er
, blks
,
948 ea_set_simple_alloc
, es
);
956 static int ea_set_block(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
,
959 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
960 struct buffer_head
*indbh
, *newbh
;
963 int mh_size
= sizeof(struct gfs2_meta_header
);
965 if (ip
->i_diskflags
& GFS2_DIF_EA_INDIRECT
) {
968 error
= gfs2_meta_read(ip
->i_gl
, ip
->i_eattr
, DIO_WAIT
, 0,
973 if (gfs2_metatype_check(sdp
, indbh
, GFS2_METATYPE_IN
)) {
978 eablk
= (__be64
*)(indbh
->b_data
+ mh_size
);
979 end
= eablk
+ sdp
->sd_inptrs
;
981 for (; eablk
< end
; eablk
++)
990 gfs2_trans_add_meta(ip
->i_gl
, indbh
);
994 error
= gfs2_alloc_blocks(ip
, &blk
, &n
, 0, NULL
);
997 gfs2_trans_remove_revoke(sdp
, blk
, 1);
998 indbh
= gfs2_meta_new(ip
->i_gl
, blk
);
999 gfs2_trans_add_meta(ip
->i_gl
, indbh
);
1000 gfs2_metatype_set(indbh
, GFS2_METATYPE_IN
, GFS2_FORMAT_IN
);
1001 gfs2_buffer_clear_tail(indbh
, mh_size
);
1003 eablk
= (__be64
*)(indbh
->b_data
+ mh_size
);
1004 *eablk
= cpu_to_be64(ip
->i_eattr
);
1006 ip
->i_diskflags
|= GFS2_DIF_EA_INDIRECT
;
1007 gfs2_add_inode_blocks(&ip
->i_inode
, 1);
1012 error
= ea_alloc_blk(ip
, &newbh
);
1016 *eablk
= cpu_to_be64((u64
)newbh
->b_blocknr
);
1017 error
= ea_write(ip
, GFS2_EA_BH2FIRST(newbh
), er
);
1023 ea_set_remove_stuffed(ip
, private);
1030 static int ea_set_i(struct gfs2_inode
*ip
, int type
, const char *name
,
1031 const void *value
, size_t size
, struct gfs2_ea_location
*el
)
1033 struct gfs2_ea_request er
;
1035 unsigned int blks
= 2;
1040 er
.er_data
= (void *)value
;
1041 er
.er_name_len
= strlen(name
);
1042 er
.er_data_len
= size
;
1044 memset(&es
, 0, sizeof(struct ea_set
));
1048 error
= ea_foreach(ip
, ea_set_simple
, &es
);
1054 if (!(ip
->i_diskflags
& GFS2_DIF_EA_INDIRECT
))
1056 if (GFS2_EAREQ_SIZE_STUFFED(&er
) > GFS2_SB(&ip
->i_inode
)->sd_jbsize
)
1057 blks
+= DIV_ROUND_UP(er
.er_data_len
, GFS2_SB(&ip
->i_inode
)->sd_jbsize
);
1059 return ea_alloc_skeleton(ip
, &er
, blks
, ea_set_block
, el
);
1062 static int ea_set_remove_unstuffed(struct gfs2_inode
*ip
,
1063 struct gfs2_ea_location
*el
)
1065 if (el
->el_prev
&& GFS2_EA2NEXT(el
->el_prev
) != el
->el_ea
) {
1066 el
->el_prev
= GFS2_EA2NEXT(el
->el_prev
);
1067 gfs2_assert_withdraw(GFS2_SB(&ip
->i_inode
),
1068 GFS2_EA2NEXT(el
->el_prev
) == el
->el_ea
);
1071 return ea_remove_unstuffed(ip
, el
->el_bh
, el
->el_ea
, el
->el_prev
, 0);
1074 static int ea_remove_stuffed(struct gfs2_inode
*ip
, struct gfs2_ea_location
*el
)
1076 struct gfs2_ea_header
*ea
= el
->el_ea
;
1077 struct gfs2_ea_header
*prev
= el
->el_prev
;
1080 error
= gfs2_trans_begin(GFS2_SB(&ip
->i_inode
), RES_DINODE
+ RES_EATTR
, 0);
1084 gfs2_trans_add_meta(ip
->i_gl
, el
->el_bh
);
1089 len
= GFS2_EA_REC_LEN(prev
) + GFS2_EA_REC_LEN(ea
);
1090 prev
->ea_rec_len
= cpu_to_be32(len
);
1092 if (GFS2_EA_IS_LAST(ea
))
1093 prev
->ea_flags
|= GFS2_EAFLAG_LAST
;
1095 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
1098 ip
->i_inode
.i_ctime
= current_time(&ip
->i_inode
);
1099 __mark_inode_dirty(&ip
->i_inode
, I_DIRTY_DATASYNC
);
1101 gfs2_trans_end(GFS2_SB(&ip
->i_inode
));
1107 * gfs2_xattr_remove - Remove a GFS2 extended attribute
1109 * @type: The type of the extended attribute
1110 * @name: The name of the extended attribute
1112 * This is not called directly by the VFS since we use the (common)
1113 * scheme of making a "set with NULL data" mean a remove request. Note
1114 * that this is different from a set with zero length data.
1116 * Returns: 0, or errno on failure
1119 static int gfs2_xattr_remove(struct gfs2_inode
*ip
, int type
, const char *name
)
1121 struct gfs2_ea_location el
;
1127 error
= gfs2_ea_find(ip
, type
, name
, &el
);
1133 if (GFS2_EA_IS_STUFFED(el
.el_ea
))
1134 error
= ea_remove_stuffed(ip
, &el
);
1136 error
= ea_remove_unstuffed(ip
, el
.el_bh
, el
.el_ea
, el
.el_prev
, 0);
1144 * __gfs2_xattr_set - Set (or remove) a GFS2 extended attribute
1146 * @name: The name of the extended attribute
1147 * @value: The value of the extended attribute (NULL for remove)
1148 * @size: The size of the @value argument
1149 * @flags: Create or Replace
1150 * @type: The type of the extended attribute
1152 * See gfs2_xattr_remove() for details of the removal of xattrs.
1154 * Returns: 0 or errno on failure
1157 int __gfs2_xattr_set(struct inode
*inode
, const char *name
,
1158 const void *value
, size_t size
, int flags
, int type
)
1160 struct gfs2_inode
*ip
= GFS2_I(inode
);
1161 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1162 struct gfs2_ea_location el
;
1163 unsigned int namel
= strlen(name
);
1166 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
1168 if (namel
> GFS2_EA_MAX_NAME_LEN
)
1171 if (value
== NULL
) {
1172 error
= gfs2_xattr_remove(ip
, type
, name
);
1173 if (error
== -ENODATA
&& !(flags
& XATTR_REPLACE
))
1178 if (ea_check_size(sdp
, namel
, size
))
1182 if (flags
& XATTR_REPLACE
)
1184 return ea_init(ip
, type
, name
, value
, size
);
1187 error
= gfs2_ea_find(ip
, type
, name
, &el
);
1192 if (ip
->i_diskflags
& GFS2_DIF_APPENDONLY
) {
1198 if (!(flags
& XATTR_CREATE
)) {
1199 int unstuffed
= !GFS2_EA_IS_STUFFED(el
.el_ea
);
1200 error
= ea_set_i(ip
, type
, name
, value
, size
, &el
);
1201 if (!error
&& unstuffed
)
1202 ea_set_remove_unstuffed(ip
, &el
);
1210 if (!(flags
& XATTR_REPLACE
))
1211 error
= ea_set_i(ip
, type
, name
, value
, size
, NULL
);
1216 static int gfs2_xattr_set(const struct xattr_handler
*handler
,
1217 struct dentry
*unused
, struct inode
*inode
,
1218 const char *name
, const void *value
,
1219 size_t size
, int flags
)
1221 struct gfs2_inode
*ip
= GFS2_I(inode
);
1222 struct gfs2_holder gh
;
1225 ret
= gfs2_qa_get(ip
);
1229 /* May be called from gfs_setattr with the glock locked. */
1231 if (!gfs2_glock_is_locked_by_me(ip
->i_gl
)) {
1232 ret
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &gh
);
1236 if (WARN_ON_ONCE(ip
->i_gl
->gl_state
!= LM_ST_EXCLUSIVE
)) {
1240 gfs2_holder_mark_uninitialized(&gh
);
1242 ret
= __gfs2_xattr_set(inode
, name
, value
, size
, flags
, handler
->flags
);
1243 if (gfs2_holder_initialized(&gh
))
1244 gfs2_glock_dq_uninit(&gh
);
1250 static int ea_dealloc_indirect(struct gfs2_inode
*ip
)
1252 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1253 struct gfs2_rgrp_list rlist
;
1254 struct gfs2_rgrpd
*rgd
;
1255 struct buffer_head
*indbh
, *dibh
;
1256 __be64
*eablk
, *end
;
1257 unsigned int rg_blocks
= 0;
1259 unsigned int blen
= 0;
1260 unsigned int blks
= 0;
1264 error
= gfs2_rindex_update(sdp
);
1268 memset(&rlist
, 0, sizeof(struct gfs2_rgrp_list
));
1270 error
= gfs2_meta_read(ip
->i_gl
, ip
->i_eattr
, DIO_WAIT
, 0, &indbh
);
1274 if (gfs2_metatype_check(sdp
, indbh
, GFS2_METATYPE_IN
)) {
1279 eablk
= (__be64
*)(indbh
->b_data
+ sizeof(struct gfs2_meta_header
));
1280 end
= eablk
+ sdp
->sd_inptrs
;
1282 for (; eablk
< end
; eablk
++) {
1287 bn
= be64_to_cpu(*eablk
);
1289 if (bstart
+ blen
== bn
)
1293 gfs2_rlist_add(ip
, &rlist
, bstart
);
1300 gfs2_rlist_add(ip
, &rlist
, bstart
);
1304 gfs2_rlist_alloc(&rlist
);
1306 for (x
= 0; x
< rlist
.rl_rgrps
; x
++) {
1307 rgd
= gfs2_glock2rgrp(rlist
.rl_ghs
[x
].gh_gl
);
1308 rg_blocks
+= rgd
->rd_length
;
1311 error
= gfs2_glock_nq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1313 goto out_rlist_free
;
1315 error
= gfs2_trans_begin(sdp
, rg_blocks
+ RES_DINODE
+ RES_INDIRECT
+
1316 RES_STATFS
+ RES_QUOTA
, blks
);
1320 gfs2_trans_add_meta(ip
->i_gl
, indbh
);
1322 eablk
= (__be64
*)(indbh
->b_data
+ sizeof(struct gfs2_meta_header
));
1327 for (; eablk
< end
; eablk
++) {
1332 bn
= be64_to_cpu(*eablk
);
1334 if (bstart
+ blen
== bn
)
1338 gfs2_free_meta(ip
, rgd
, bstart
, blen
);
1340 rgd
= gfs2_blk2rgrpd(sdp
, bstart
, true);
1345 gfs2_add_inode_blocks(&ip
->i_inode
, -1);
1348 gfs2_free_meta(ip
, rgd
, bstart
, blen
);
1350 ip
->i_diskflags
&= ~GFS2_DIF_EA_INDIRECT
;
1352 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1354 gfs2_trans_add_meta(ip
->i_gl
, dibh
);
1355 gfs2_dinode_out(ip
, dibh
->b_data
);
1359 gfs2_trans_end(sdp
);
1362 gfs2_glock_dq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1364 gfs2_rlist_free(&rlist
);
1370 static int ea_dealloc_block(struct gfs2_inode
*ip
)
1372 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1373 struct gfs2_rgrpd
*rgd
;
1374 struct buffer_head
*dibh
;
1375 struct gfs2_holder gh
;
1378 error
= gfs2_rindex_update(sdp
);
1382 rgd
= gfs2_blk2rgrpd(sdp
, ip
->i_eattr
, 1);
1384 gfs2_consist_inode(ip
);
1388 error
= gfs2_glock_nq_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, &gh
);
1392 error
= gfs2_trans_begin(sdp
, RES_RG_BIT
+ RES_DINODE
+ RES_STATFS
+
1397 gfs2_free_meta(ip
, rgd
, ip
->i_eattr
, 1);
1400 gfs2_add_inode_blocks(&ip
->i_inode
, -1);
1402 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1404 gfs2_trans_add_meta(ip
->i_gl
, dibh
);
1405 gfs2_dinode_out(ip
, dibh
->b_data
);
1409 gfs2_trans_end(sdp
);
1412 gfs2_glock_dq_uninit(&gh
);
1417 * gfs2_ea_dealloc - deallocate the extended attribute fork
1423 int gfs2_ea_dealloc(struct gfs2_inode
*ip
)
1427 error
= gfs2_rindex_update(GFS2_SB(&ip
->i_inode
));
1431 error
= gfs2_quota_hold(ip
, NO_UID_QUOTA_CHANGE
, NO_GID_QUOTA_CHANGE
);
1435 error
= ea_foreach(ip
, ea_dealloc_unstuffed
, NULL
);
1439 if (ip
->i_diskflags
& GFS2_DIF_EA_INDIRECT
) {
1440 error
= ea_dealloc_indirect(ip
);
1445 error
= ea_dealloc_block(ip
);
1448 gfs2_quota_unhold(ip
);
1452 static const struct xattr_handler gfs2_xattr_user_handler
= {
1453 .prefix
= XATTR_USER_PREFIX
,
1454 .flags
= GFS2_EATYPE_USR
,
1455 .get
= gfs2_xattr_get
,
1456 .set
= gfs2_xattr_set
,
1459 static const struct xattr_handler gfs2_xattr_security_handler
= {
1460 .prefix
= XATTR_SECURITY_PREFIX
,
1461 .flags
= GFS2_EATYPE_SECURITY
,
1462 .get
= gfs2_xattr_get
,
1463 .set
= gfs2_xattr_set
,
1466 const struct xattr_handler
*gfs2_xattr_handlers
[] = {
1467 &gfs2_xattr_user_handler
,
1468 &gfs2_xattr_security_handler
,
1469 &posix_acl_access_xattr_handler
,
1470 &posix_acl_default_xattr_handler
,