1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /* -*- mode: c; c-basic-offset: 8; -*-
3*4882a593Smuzhiyun * vim: noexpandtab sw=8 ts=8 sts=0:
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * xattr.c
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Copyright (C) 2004, 2008 Oracle. All rights reserved.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * CREDITS:
10*4882a593Smuzhiyun * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
11*4882a593Smuzhiyun * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
12*4882a593Smuzhiyun */
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <linux/capability.h>
15*4882a593Smuzhiyun #include <linux/fs.h>
16*4882a593Smuzhiyun #include <linux/types.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun #include <linux/highmem.h>
19*4882a593Smuzhiyun #include <linux/pagemap.h>
20*4882a593Smuzhiyun #include <linux/uio.h>
21*4882a593Smuzhiyun #include <linux/sched.h>
22*4882a593Smuzhiyun #include <linux/splice.h>
23*4882a593Smuzhiyun #include <linux/mount.h>
24*4882a593Smuzhiyun #include <linux/writeback.h>
25*4882a593Smuzhiyun #include <linux/falloc.h>
26*4882a593Smuzhiyun #include <linux/sort.h>
27*4882a593Smuzhiyun #include <linux/init.h>
28*4882a593Smuzhiyun #include <linux/module.h>
29*4882a593Smuzhiyun #include <linux/string.h>
30*4882a593Smuzhiyun #include <linux/security.h>
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #include <cluster/masklog.h>
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #include "ocfs2.h"
35*4882a593Smuzhiyun #include "alloc.h"
36*4882a593Smuzhiyun #include "blockcheck.h"
37*4882a593Smuzhiyun #include "dlmglue.h"
38*4882a593Smuzhiyun #include "file.h"
39*4882a593Smuzhiyun #include "symlink.h"
40*4882a593Smuzhiyun #include "sysfile.h"
41*4882a593Smuzhiyun #include "inode.h"
42*4882a593Smuzhiyun #include "journal.h"
43*4882a593Smuzhiyun #include "ocfs2_fs.h"
44*4882a593Smuzhiyun #include "suballoc.h"
45*4882a593Smuzhiyun #include "uptodate.h"
46*4882a593Smuzhiyun #include "buffer_head_io.h"
47*4882a593Smuzhiyun #include "super.h"
48*4882a593Smuzhiyun #include "xattr.h"
49*4882a593Smuzhiyun #include "refcounttree.h"
50*4882a593Smuzhiyun #include "acl.h"
51*4882a593Smuzhiyun #include "ocfs2_trace.h"
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun struct ocfs2_xattr_def_value_root {
54*4882a593Smuzhiyun struct ocfs2_xattr_value_root xv;
55*4882a593Smuzhiyun struct ocfs2_extent_rec er;
56*4882a593Smuzhiyun };
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun struct ocfs2_xattr_bucket {
59*4882a593Smuzhiyun /* The inode these xattrs are associated with */
60*4882a593Smuzhiyun struct inode *bu_inode;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /* The actual buffers that make up the bucket */
63*4882a593Smuzhiyun struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /* How many blocks make up one bucket for this filesystem */
66*4882a593Smuzhiyun int bu_blocks;
67*4882a593Smuzhiyun };
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt {
70*4882a593Smuzhiyun handle_t *handle;
71*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac;
72*4882a593Smuzhiyun struct ocfs2_alloc_context *data_ac;
73*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt dealloc;
74*4882a593Smuzhiyun int set_abort;
75*4882a593Smuzhiyun };
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
78*4882a593Smuzhiyun #define OCFS2_XATTR_INLINE_SIZE 80
79*4882a593Smuzhiyun #define OCFS2_XATTR_HEADER_GAP 4
80*4882a593Smuzhiyun #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
81*4882a593Smuzhiyun - sizeof(struct ocfs2_xattr_header) \
82*4882a593Smuzhiyun - OCFS2_XATTR_HEADER_GAP)
83*4882a593Smuzhiyun #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
84*4882a593Smuzhiyun - sizeof(struct ocfs2_xattr_block) \
85*4882a593Smuzhiyun - sizeof(struct ocfs2_xattr_header) \
86*4882a593Smuzhiyun - OCFS2_XATTR_HEADER_GAP)
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun static struct ocfs2_xattr_def_value_root def_xv = {
89*4882a593Smuzhiyun .xv.xr_list.l_count = cpu_to_le16(1),
90*4882a593Smuzhiyun };
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun const struct xattr_handler *ocfs2_xattr_handlers[] = {
93*4882a593Smuzhiyun &ocfs2_xattr_user_handler,
94*4882a593Smuzhiyun &posix_acl_access_xattr_handler,
95*4882a593Smuzhiyun &posix_acl_default_xattr_handler,
96*4882a593Smuzhiyun &ocfs2_xattr_trusted_handler,
97*4882a593Smuzhiyun &ocfs2_xattr_security_handler,
98*4882a593Smuzhiyun NULL
99*4882a593Smuzhiyun };
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun static const struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
102*4882a593Smuzhiyun [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
103*4882a593Smuzhiyun [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
104*4882a593Smuzhiyun = &posix_acl_access_xattr_handler,
105*4882a593Smuzhiyun [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
106*4882a593Smuzhiyun = &posix_acl_default_xattr_handler,
107*4882a593Smuzhiyun [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
108*4882a593Smuzhiyun [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
109*4882a593Smuzhiyun };
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun struct ocfs2_xattr_info {
112*4882a593Smuzhiyun int xi_name_index;
113*4882a593Smuzhiyun const char *xi_name;
114*4882a593Smuzhiyun int xi_name_len;
115*4882a593Smuzhiyun const void *xi_value;
116*4882a593Smuzhiyun size_t xi_value_len;
117*4882a593Smuzhiyun };
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun struct ocfs2_xattr_search {
120*4882a593Smuzhiyun struct buffer_head *inode_bh;
121*4882a593Smuzhiyun /*
122*4882a593Smuzhiyun * xattr_bh point to the block buffer head which has extended attribute
123*4882a593Smuzhiyun * when extended attribute in inode, xattr_bh is equal to inode_bh.
124*4882a593Smuzhiyun */
125*4882a593Smuzhiyun struct buffer_head *xattr_bh;
126*4882a593Smuzhiyun struct ocfs2_xattr_header *header;
127*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket;
128*4882a593Smuzhiyun void *base;
129*4882a593Smuzhiyun void *end;
130*4882a593Smuzhiyun struct ocfs2_xattr_entry *here;
131*4882a593Smuzhiyun int not_found;
132*4882a593Smuzhiyun };
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun /* Operations on struct ocfs2_xa_entry */
135*4882a593Smuzhiyun struct ocfs2_xa_loc;
136*4882a593Smuzhiyun struct ocfs2_xa_loc_operations {
137*4882a593Smuzhiyun /*
138*4882a593Smuzhiyun * Journal functions
139*4882a593Smuzhiyun */
140*4882a593Smuzhiyun int (*xlo_journal_access)(handle_t *handle, struct ocfs2_xa_loc *loc,
141*4882a593Smuzhiyun int type);
142*4882a593Smuzhiyun void (*xlo_journal_dirty)(handle_t *handle, struct ocfs2_xa_loc *loc);
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun /*
145*4882a593Smuzhiyun * Return a pointer to the appropriate buffer in loc->xl_storage
146*4882a593Smuzhiyun * at the given offset from loc->xl_header.
147*4882a593Smuzhiyun */
148*4882a593Smuzhiyun void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun /* Can we reuse the existing entry for the new value? */
151*4882a593Smuzhiyun int (*xlo_can_reuse)(struct ocfs2_xa_loc *loc,
152*4882a593Smuzhiyun struct ocfs2_xattr_info *xi);
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun /* How much space is needed for the new value? */
155*4882a593Smuzhiyun int (*xlo_check_space)(struct ocfs2_xa_loc *loc,
156*4882a593Smuzhiyun struct ocfs2_xattr_info *xi);
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /*
159*4882a593Smuzhiyun * Return the offset of the first name+value pair. This is
160*4882a593Smuzhiyun * the start of our downward-filling free space.
161*4882a593Smuzhiyun */
162*4882a593Smuzhiyun int (*xlo_get_free_start)(struct ocfs2_xa_loc *loc);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun /*
165*4882a593Smuzhiyun * Remove the name+value at this location. Do whatever is
166*4882a593Smuzhiyun * appropriate with the remaining name+value pairs.
167*4882a593Smuzhiyun */
168*4882a593Smuzhiyun void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /* Fill xl_entry with a new entry */
171*4882a593Smuzhiyun void (*xlo_add_entry)(struct ocfs2_xa_loc *loc, u32 name_hash);
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun /* Add name+value storage to an entry */
174*4882a593Smuzhiyun void (*xlo_add_namevalue)(struct ocfs2_xa_loc *loc, int size);
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun /*
177*4882a593Smuzhiyun * Initialize the value buf's access and bh fields for this entry.
178*4882a593Smuzhiyun * ocfs2_xa_fill_value_buf() will handle the xv pointer.
179*4882a593Smuzhiyun */
180*4882a593Smuzhiyun void (*xlo_fill_value_buf)(struct ocfs2_xa_loc *loc,
181*4882a593Smuzhiyun struct ocfs2_xattr_value_buf *vb);
182*4882a593Smuzhiyun };
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun /*
185*4882a593Smuzhiyun * Describes an xattr entry location. This is a memory structure
186*4882a593Smuzhiyun * tracking the on-disk structure.
187*4882a593Smuzhiyun */
188*4882a593Smuzhiyun struct ocfs2_xa_loc {
189*4882a593Smuzhiyun /* This xattr belongs to this inode */
190*4882a593Smuzhiyun struct inode *xl_inode;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
193*4882a593Smuzhiyun struct ocfs2_xattr_header *xl_header;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun /* Bytes from xl_header to the end of the storage */
196*4882a593Smuzhiyun int xl_size;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun /*
199*4882a593Smuzhiyun * The ocfs2_xattr_entry this location describes. If this is
200*4882a593Smuzhiyun * NULL, this location describes the on-disk structure where it
201*4882a593Smuzhiyun * would have been.
202*4882a593Smuzhiyun */
203*4882a593Smuzhiyun struct ocfs2_xattr_entry *xl_entry;
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun /*
206*4882a593Smuzhiyun * Internal housekeeping
207*4882a593Smuzhiyun */
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun /* Buffer(s) containing this entry */
210*4882a593Smuzhiyun void *xl_storage;
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun /* Operations on the storage backing this location */
213*4882a593Smuzhiyun const struct ocfs2_xa_loc_operations *xl_ops;
214*4882a593Smuzhiyun };
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun * Convenience functions to calculate how much space is needed for a
218*4882a593Smuzhiyun * given name+value pair
219*4882a593Smuzhiyun */
namevalue_size(int name_len,uint64_t value_len)220*4882a593Smuzhiyun static int namevalue_size(int name_len, uint64_t value_len)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun if (value_len > OCFS2_XATTR_INLINE_SIZE)
223*4882a593Smuzhiyun return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
224*4882a593Smuzhiyun else
225*4882a593Smuzhiyun return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun
namevalue_size_xi(struct ocfs2_xattr_info * xi)228*4882a593Smuzhiyun static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun return namevalue_size(xi->xi_name_len, xi->xi_value_len);
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun
namevalue_size_xe(struct ocfs2_xattr_entry * xe)233*4882a593Smuzhiyun static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun u64 value_len = le64_to_cpu(xe->xe_value_size);
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
238*4882a593Smuzhiyun ocfs2_xattr_is_local(xe));
239*4882a593Smuzhiyun return namevalue_size(xe->xe_name_len, value_len);
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
244*4882a593Smuzhiyun struct ocfs2_xattr_header *xh,
245*4882a593Smuzhiyun int index,
246*4882a593Smuzhiyun int *block_off,
247*4882a593Smuzhiyun int *new_offset);
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun static int ocfs2_xattr_block_find(struct inode *inode,
250*4882a593Smuzhiyun int name_index,
251*4882a593Smuzhiyun const char *name,
252*4882a593Smuzhiyun struct ocfs2_xattr_search *xs);
253*4882a593Smuzhiyun static int ocfs2_xattr_index_block_find(struct inode *inode,
254*4882a593Smuzhiyun struct buffer_head *root_bh,
255*4882a593Smuzhiyun int name_index,
256*4882a593Smuzhiyun const char *name,
257*4882a593Smuzhiyun struct ocfs2_xattr_search *xs);
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
260*4882a593Smuzhiyun struct buffer_head *blk_bh,
261*4882a593Smuzhiyun char *buffer,
262*4882a593Smuzhiyun size_t buffer_size);
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun static int ocfs2_xattr_create_index_block(struct inode *inode,
265*4882a593Smuzhiyun struct ocfs2_xattr_search *xs,
266*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt);
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
269*4882a593Smuzhiyun struct ocfs2_xattr_info *xi,
270*4882a593Smuzhiyun struct ocfs2_xattr_search *xs,
271*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt);
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun typedef int (xattr_tree_rec_func)(struct inode *inode,
274*4882a593Smuzhiyun struct buffer_head *root_bh,
275*4882a593Smuzhiyun u64 blkno, u32 cpos, u32 len, void *para);
276*4882a593Smuzhiyun static int ocfs2_iterate_xattr_index_block(struct inode *inode,
277*4882a593Smuzhiyun struct buffer_head *root_bh,
278*4882a593Smuzhiyun xattr_tree_rec_func *rec_func,
279*4882a593Smuzhiyun void *para);
280*4882a593Smuzhiyun static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
281*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket,
282*4882a593Smuzhiyun void *para);
283*4882a593Smuzhiyun static int ocfs2_rm_xattr_cluster(struct inode *inode,
284*4882a593Smuzhiyun struct buffer_head *root_bh,
285*4882a593Smuzhiyun u64 blkno,
286*4882a593Smuzhiyun u32 cpos,
287*4882a593Smuzhiyun u32 len,
288*4882a593Smuzhiyun void *para);
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
291*4882a593Smuzhiyun u64 src_blk, u64 last_blk, u64 to_blk,
292*4882a593Smuzhiyun unsigned int start_bucket,
293*4882a593Smuzhiyun u32 *first_hash);
294*4882a593Smuzhiyun static int ocfs2_prepare_refcount_xattr(struct inode *inode,
295*4882a593Smuzhiyun struct ocfs2_dinode *di,
296*4882a593Smuzhiyun struct ocfs2_xattr_info *xi,
297*4882a593Smuzhiyun struct ocfs2_xattr_search *xis,
298*4882a593Smuzhiyun struct ocfs2_xattr_search *xbs,
299*4882a593Smuzhiyun struct ocfs2_refcount_tree **ref_tree,
300*4882a593Smuzhiyun int *meta_need,
301*4882a593Smuzhiyun int *credits);
302*4882a593Smuzhiyun static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
303*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket,
304*4882a593Smuzhiyun int offset,
305*4882a593Smuzhiyun struct ocfs2_xattr_value_root **xv,
306*4882a593Smuzhiyun struct buffer_head **bh);
307*4882a593Smuzhiyun
ocfs2_xattr_buckets_per_cluster(struct ocfs2_super * osb)308*4882a593Smuzhiyun static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
309*4882a593Smuzhiyun {
310*4882a593Smuzhiyun return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun
ocfs2_blocks_per_xattr_bucket(struct super_block * sb)313*4882a593Smuzhiyun static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
319*4882a593Smuzhiyun #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
320*4882a593Smuzhiyun #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
321*4882a593Smuzhiyun
ocfs2_xattr_bucket_new(struct inode * inode)322*4882a593Smuzhiyun static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket;
325*4882a593Smuzhiyun int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
330*4882a593Smuzhiyun if (bucket) {
331*4882a593Smuzhiyun bucket->bu_inode = inode;
332*4882a593Smuzhiyun bucket->bu_blocks = blks;
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun return bucket;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun
ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket * bucket)338*4882a593Smuzhiyun static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun int i;
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun for (i = 0; i < bucket->bu_blocks; i++) {
343*4882a593Smuzhiyun brelse(bucket->bu_bhs[i]);
344*4882a593Smuzhiyun bucket->bu_bhs[i] = NULL;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun }
347*4882a593Smuzhiyun
ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket * bucket)348*4882a593Smuzhiyun static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun if (bucket) {
351*4882a593Smuzhiyun ocfs2_xattr_bucket_relse(bucket);
352*4882a593Smuzhiyun bucket->bu_inode = NULL;
353*4882a593Smuzhiyun kfree(bucket);
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun /*
358*4882a593Smuzhiyun * A bucket that has never been written to disk doesn't need to be
359*4882a593Smuzhiyun * read. We just need the buffer_heads. Don't call this for
360*4882a593Smuzhiyun * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
361*4882a593Smuzhiyun * them fully.
362*4882a593Smuzhiyun */
ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket * bucket,u64 xb_blkno,int new)363*4882a593Smuzhiyun static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
364*4882a593Smuzhiyun u64 xb_blkno, int new)
365*4882a593Smuzhiyun {
366*4882a593Smuzhiyun int i, rc = 0;
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun for (i = 0; i < bucket->bu_blocks; i++) {
369*4882a593Smuzhiyun bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
370*4882a593Smuzhiyun xb_blkno + i);
371*4882a593Smuzhiyun if (!bucket->bu_bhs[i]) {
372*4882a593Smuzhiyun rc = -ENOMEM;
373*4882a593Smuzhiyun mlog_errno(rc);
374*4882a593Smuzhiyun break;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
378*4882a593Smuzhiyun bucket->bu_bhs[i])) {
379*4882a593Smuzhiyun if (new)
380*4882a593Smuzhiyun ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
381*4882a593Smuzhiyun bucket->bu_bhs[i]);
382*4882a593Smuzhiyun else {
383*4882a593Smuzhiyun set_buffer_uptodate(bucket->bu_bhs[i]);
384*4882a593Smuzhiyun ocfs2_set_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
385*4882a593Smuzhiyun bucket->bu_bhs[i]);
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun if (rc)
391*4882a593Smuzhiyun ocfs2_xattr_bucket_relse(bucket);
392*4882a593Smuzhiyun return rc;
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun /* Read the xattr bucket at xb_blkno */
ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket * bucket,u64 xb_blkno)396*4882a593Smuzhiyun static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
397*4882a593Smuzhiyun u64 xb_blkno)
398*4882a593Smuzhiyun {
399*4882a593Smuzhiyun int rc;
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
402*4882a593Smuzhiyun bucket->bu_blocks, bucket->bu_bhs, 0,
403*4882a593Smuzhiyun NULL);
404*4882a593Smuzhiyun if (!rc) {
405*4882a593Smuzhiyun spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
406*4882a593Smuzhiyun rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
407*4882a593Smuzhiyun bucket->bu_bhs,
408*4882a593Smuzhiyun bucket->bu_blocks,
409*4882a593Smuzhiyun &bucket_xh(bucket)->xh_check);
410*4882a593Smuzhiyun spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
411*4882a593Smuzhiyun if (rc)
412*4882a593Smuzhiyun mlog_errno(rc);
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun if (rc)
416*4882a593Smuzhiyun ocfs2_xattr_bucket_relse(bucket);
417*4882a593Smuzhiyun return rc;
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun
ocfs2_xattr_bucket_journal_access(handle_t * handle,struct ocfs2_xattr_bucket * bucket,int type)420*4882a593Smuzhiyun static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
421*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket,
422*4882a593Smuzhiyun int type)
423*4882a593Smuzhiyun {
424*4882a593Smuzhiyun int i, rc = 0;
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun for (i = 0; i < bucket->bu_blocks; i++) {
427*4882a593Smuzhiyun rc = ocfs2_journal_access(handle,
428*4882a593Smuzhiyun INODE_CACHE(bucket->bu_inode),
429*4882a593Smuzhiyun bucket->bu_bhs[i], type);
430*4882a593Smuzhiyun if (rc) {
431*4882a593Smuzhiyun mlog_errno(rc);
432*4882a593Smuzhiyun break;
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun return rc;
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun
ocfs2_xattr_bucket_journal_dirty(handle_t * handle,struct ocfs2_xattr_bucket * bucket)439*4882a593Smuzhiyun static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
440*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket)
441*4882a593Smuzhiyun {
442*4882a593Smuzhiyun int i;
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
445*4882a593Smuzhiyun ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
446*4882a593Smuzhiyun bucket->bu_bhs, bucket->bu_blocks,
447*4882a593Smuzhiyun &bucket_xh(bucket)->xh_check);
448*4882a593Smuzhiyun spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun for (i = 0; i < bucket->bu_blocks; i++)
451*4882a593Smuzhiyun ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun
ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket * dest,struct ocfs2_xattr_bucket * src)454*4882a593Smuzhiyun static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
455*4882a593Smuzhiyun struct ocfs2_xattr_bucket *src)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun int i;
458*4882a593Smuzhiyun int blocksize = src->bu_inode->i_sb->s_blocksize;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun BUG_ON(dest->bu_blocks != src->bu_blocks);
461*4882a593Smuzhiyun BUG_ON(dest->bu_inode != src->bu_inode);
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun for (i = 0; i < src->bu_blocks; i++) {
464*4882a593Smuzhiyun memcpy(bucket_block(dest, i), bucket_block(src, i),
465*4882a593Smuzhiyun blocksize);
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun
ocfs2_validate_xattr_block(struct super_block * sb,struct buffer_head * bh)469*4882a593Smuzhiyun static int ocfs2_validate_xattr_block(struct super_block *sb,
470*4882a593Smuzhiyun struct buffer_head *bh)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun int rc;
473*4882a593Smuzhiyun struct ocfs2_xattr_block *xb =
474*4882a593Smuzhiyun (struct ocfs2_xattr_block *)bh->b_data;
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun trace_ocfs2_validate_xattr_block((unsigned long long)bh->b_blocknr);
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun BUG_ON(!buffer_uptodate(bh));
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun /*
481*4882a593Smuzhiyun * If the ecc fails, we return the error but otherwise
482*4882a593Smuzhiyun * leave the filesystem running. We know any error is
483*4882a593Smuzhiyun * local to this block.
484*4882a593Smuzhiyun */
485*4882a593Smuzhiyun rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
486*4882a593Smuzhiyun if (rc)
487*4882a593Smuzhiyun return rc;
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun /*
490*4882a593Smuzhiyun * Errors after here are fatal
491*4882a593Smuzhiyun */
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
494*4882a593Smuzhiyun return ocfs2_error(sb,
495*4882a593Smuzhiyun "Extended attribute block #%llu has bad signature %.*s\n",
496*4882a593Smuzhiyun (unsigned long long)bh->b_blocknr, 7,
497*4882a593Smuzhiyun xb->xb_signature);
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
501*4882a593Smuzhiyun return ocfs2_error(sb,
502*4882a593Smuzhiyun "Extended attribute block #%llu has an invalid xb_blkno of %llu\n",
503*4882a593Smuzhiyun (unsigned long long)bh->b_blocknr,
504*4882a593Smuzhiyun (unsigned long long)le64_to_cpu(xb->xb_blkno));
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
508*4882a593Smuzhiyun return ocfs2_error(sb,
509*4882a593Smuzhiyun "Extended attribute block #%llu has an invalid xb_fs_generation of #%u\n",
510*4882a593Smuzhiyun (unsigned long long)bh->b_blocknr,
511*4882a593Smuzhiyun le32_to_cpu(xb->xb_fs_generation));
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun return 0;
515*4882a593Smuzhiyun }
516*4882a593Smuzhiyun
ocfs2_read_xattr_block(struct inode * inode,u64 xb_blkno,struct buffer_head ** bh)517*4882a593Smuzhiyun static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
518*4882a593Smuzhiyun struct buffer_head **bh)
519*4882a593Smuzhiyun {
520*4882a593Smuzhiyun int rc;
521*4882a593Smuzhiyun struct buffer_head *tmp = *bh;
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
524*4882a593Smuzhiyun ocfs2_validate_xattr_block);
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun /* If ocfs2_read_block() got us a new bh, pass it up. */
527*4882a593Smuzhiyun if (!rc && !*bh)
528*4882a593Smuzhiyun *bh = tmp;
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun return rc;
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun
ocfs2_xattr_prefix(int name_index)533*4882a593Smuzhiyun static inline const char *ocfs2_xattr_prefix(int name_index)
534*4882a593Smuzhiyun {
535*4882a593Smuzhiyun const struct xattr_handler *handler = NULL;
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
538*4882a593Smuzhiyun handler = ocfs2_xattr_handler_map[name_index];
539*4882a593Smuzhiyun return handler ? xattr_prefix(handler) : NULL;
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun
ocfs2_xattr_name_hash(struct inode * inode,const char * name,int name_len)542*4882a593Smuzhiyun static u32 ocfs2_xattr_name_hash(struct inode *inode,
543*4882a593Smuzhiyun const char *name,
544*4882a593Smuzhiyun int name_len)
545*4882a593Smuzhiyun {
546*4882a593Smuzhiyun /* Get hash value of uuid from super block */
547*4882a593Smuzhiyun u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
548*4882a593Smuzhiyun int i;
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun /* hash extended attribute name */
551*4882a593Smuzhiyun for (i = 0; i < name_len; i++) {
552*4882a593Smuzhiyun hash = (hash << OCFS2_HASH_SHIFT) ^
553*4882a593Smuzhiyun (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
554*4882a593Smuzhiyun *name++;
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun return hash;
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun
ocfs2_xattr_entry_real_size(int name_len,size_t value_len)560*4882a593Smuzhiyun static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun return namevalue_size(name_len, value_len) +
563*4882a593Smuzhiyun sizeof(struct ocfs2_xattr_entry);
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun
ocfs2_xi_entry_usage(struct ocfs2_xattr_info * xi)566*4882a593Smuzhiyun static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
567*4882a593Smuzhiyun {
568*4882a593Smuzhiyun return namevalue_size_xi(xi) +
569*4882a593Smuzhiyun sizeof(struct ocfs2_xattr_entry);
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun
ocfs2_xe_entry_usage(struct ocfs2_xattr_entry * xe)572*4882a593Smuzhiyun static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
573*4882a593Smuzhiyun {
574*4882a593Smuzhiyun return namevalue_size_xe(xe) +
575*4882a593Smuzhiyun sizeof(struct ocfs2_xattr_entry);
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun
ocfs2_calc_security_init(struct inode * dir,struct ocfs2_security_xattr_info * si,int * want_clusters,int * xattr_credits,struct ocfs2_alloc_context ** xattr_ac)578*4882a593Smuzhiyun int ocfs2_calc_security_init(struct inode *dir,
579*4882a593Smuzhiyun struct ocfs2_security_xattr_info *si,
580*4882a593Smuzhiyun int *want_clusters,
581*4882a593Smuzhiyun int *xattr_credits,
582*4882a593Smuzhiyun struct ocfs2_alloc_context **xattr_ac)
583*4882a593Smuzhiyun {
584*4882a593Smuzhiyun int ret = 0;
585*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
586*4882a593Smuzhiyun int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
587*4882a593Smuzhiyun si->value_len);
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun /*
590*4882a593Smuzhiyun * The max space of security xattr taken inline is
591*4882a593Smuzhiyun * 256(name) + 80(value) + 16(entry) = 352 bytes,
592*4882a593Smuzhiyun * So reserve one metadata block for it is ok.
593*4882a593Smuzhiyun */
594*4882a593Smuzhiyun if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
595*4882a593Smuzhiyun s_size > OCFS2_XATTR_FREE_IN_IBODY) {
596*4882a593Smuzhiyun ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
597*4882a593Smuzhiyun if (ret) {
598*4882a593Smuzhiyun mlog_errno(ret);
599*4882a593Smuzhiyun return ret;
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun /* reserve clusters for xattr value which will be set in B tree*/
605*4882a593Smuzhiyun if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
606*4882a593Smuzhiyun int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
607*4882a593Smuzhiyun si->value_len);
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
610*4882a593Smuzhiyun new_clusters);
611*4882a593Smuzhiyun *want_clusters += new_clusters;
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun return ret;
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun
ocfs2_calc_xattr_init(struct inode * dir,struct buffer_head * dir_bh,umode_t mode,struct ocfs2_security_xattr_info * si,int * want_clusters,int * xattr_credits,int * want_meta)616*4882a593Smuzhiyun int ocfs2_calc_xattr_init(struct inode *dir,
617*4882a593Smuzhiyun struct buffer_head *dir_bh,
618*4882a593Smuzhiyun umode_t mode,
619*4882a593Smuzhiyun struct ocfs2_security_xattr_info *si,
620*4882a593Smuzhiyun int *want_clusters,
621*4882a593Smuzhiyun int *xattr_credits,
622*4882a593Smuzhiyun int *want_meta)
623*4882a593Smuzhiyun {
624*4882a593Smuzhiyun int ret = 0;
625*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
626*4882a593Smuzhiyun int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun if (si->enable)
629*4882a593Smuzhiyun s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
630*4882a593Smuzhiyun si->value_len);
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
633*4882a593Smuzhiyun down_read(&OCFS2_I(dir)->ip_xattr_sem);
634*4882a593Smuzhiyun acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
635*4882a593Smuzhiyun OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
636*4882a593Smuzhiyun "", NULL, 0);
637*4882a593Smuzhiyun up_read(&OCFS2_I(dir)->ip_xattr_sem);
638*4882a593Smuzhiyun if (acl_len > 0) {
639*4882a593Smuzhiyun a_size = ocfs2_xattr_entry_real_size(0, acl_len);
640*4882a593Smuzhiyun if (S_ISDIR(mode))
641*4882a593Smuzhiyun a_size <<= 1;
642*4882a593Smuzhiyun } else if (acl_len != 0 && acl_len != -ENODATA) {
643*4882a593Smuzhiyun ret = acl_len;
644*4882a593Smuzhiyun mlog_errno(ret);
645*4882a593Smuzhiyun return ret;
646*4882a593Smuzhiyun }
647*4882a593Smuzhiyun }
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun if (!(s_size + a_size))
650*4882a593Smuzhiyun return ret;
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun /*
653*4882a593Smuzhiyun * The max space of security xattr taken inline is
654*4882a593Smuzhiyun * 256(name) + 80(value) + 16(entry) = 352 bytes,
655*4882a593Smuzhiyun * The max space of acl xattr taken inline is
656*4882a593Smuzhiyun * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
657*4882a593Smuzhiyun * when blocksize = 512, may reserve one more cluser for
658*4882a593Smuzhiyun * xattr bucket, otherwise reserve one metadata block
659*4882a593Smuzhiyun * for them is ok.
660*4882a593Smuzhiyun * If this is a new directory with inline data,
661*4882a593Smuzhiyun * we choose to reserve the entire inline area for
662*4882a593Smuzhiyun * directory contents and force an external xattr block.
663*4882a593Smuzhiyun */
664*4882a593Smuzhiyun if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
665*4882a593Smuzhiyun (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
666*4882a593Smuzhiyun (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
667*4882a593Smuzhiyun *want_meta = *want_meta + 1;
668*4882a593Smuzhiyun *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
672*4882a593Smuzhiyun (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
673*4882a593Smuzhiyun *want_clusters += 1;
674*4882a593Smuzhiyun *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun /*
678*4882a593Smuzhiyun * reserve credits and clusters for xattrs which has large value
679*4882a593Smuzhiyun * and have to be set outside
680*4882a593Smuzhiyun */
681*4882a593Smuzhiyun if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
682*4882a593Smuzhiyun new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
683*4882a593Smuzhiyun si->value_len);
684*4882a593Smuzhiyun *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
685*4882a593Smuzhiyun new_clusters);
686*4882a593Smuzhiyun *want_clusters += new_clusters;
687*4882a593Smuzhiyun }
688*4882a593Smuzhiyun if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
689*4882a593Smuzhiyun acl_len > OCFS2_XATTR_INLINE_SIZE) {
690*4882a593Smuzhiyun /* for directory, it has DEFAULT and ACCESS two types of acls */
691*4882a593Smuzhiyun new_clusters = (S_ISDIR(mode) ? 2 : 1) *
692*4882a593Smuzhiyun ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
693*4882a593Smuzhiyun *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
694*4882a593Smuzhiyun new_clusters);
695*4882a593Smuzhiyun *want_clusters += new_clusters;
696*4882a593Smuzhiyun }
697*4882a593Smuzhiyun
698*4882a593Smuzhiyun return ret;
699*4882a593Smuzhiyun }
700*4882a593Smuzhiyun
ocfs2_xattr_extend_allocation(struct inode * inode,u32 clusters_to_add,struct ocfs2_xattr_value_buf * vb,struct ocfs2_xattr_set_ctxt * ctxt)701*4882a593Smuzhiyun static int ocfs2_xattr_extend_allocation(struct inode *inode,
702*4882a593Smuzhiyun u32 clusters_to_add,
703*4882a593Smuzhiyun struct ocfs2_xattr_value_buf *vb,
704*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
705*4882a593Smuzhiyun {
706*4882a593Smuzhiyun int status = 0, credits;
707*4882a593Smuzhiyun handle_t *handle = ctxt->handle;
708*4882a593Smuzhiyun enum ocfs2_alloc_restarted why;
709*4882a593Smuzhiyun u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
710*4882a593Smuzhiyun struct ocfs2_extent_tree et;
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun while (clusters_to_add) {
715*4882a593Smuzhiyun trace_ocfs2_xattr_extend_allocation(clusters_to_add);
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
718*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
719*4882a593Smuzhiyun if (status < 0) {
720*4882a593Smuzhiyun mlog_errno(status);
721*4882a593Smuzhiyun break;
722*4882a593Smuzhiyun }
723*4882a593Smuzhiyun
724*4882a593Smuzhiyun prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
725*4882a593Smuzhiyun status = ocfs2_add_clusters_in_btree(handle,
726*4882a593Smuzhiyun &et,
727*4882a593Smuzhiyun &logical_start,
728*4882a593Smuzhiyun clusters_to_add,
729*4882a593Smuzhiyun 0,
730*4882a593Smuzhiyun ctxt->data_ac,
731*4882a593Smuzhiyun ctxt->meta_ac,
732*4882a593Smuzhiyun &why);
733*4882a593Smuzhiyun if ((status < 0) && (status != -EAGAIN)) {
734*4882a593Smuzhiyun if (status != -ENOSPC)
735*4882a593Smuzhiyun mlog_errno(status);
736*4882a593Smuzhiyun break;
737*4882a593Smuzhiyun }
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun ocfs2_journal_dirty(handle, vb->vb_bh);
740*4882a593Smuzhiyun
741*4882a593Smuzhiyun clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) -
742*4882a593Smuzhiyun prev_clusters;
743*4882a593Smuzhiyun
744*4882a593Smuzhiyun if (why != RESTART_NONE && clusters_to_add) {
745*4882a593Smuzhiyun /*
746*4882a593Smuzhiyun * We can only fail in case the alloc file doesn't give
747*4882a593Smuzhiyun * up enough clusters.
748*4882a593Smuzhiyun */
749*4882a593Smuzhiyun BUG_ON(why == RESTART_META);
750*4882a593Smuzhiyun
751*4882a593Smuzhiyun credits = ocfs2_calc_extend_credits(inode->i_sb,
752*4882a593Smuzhiyun &vb->vb_xv->xr_list);
753*4882a593Smuzhiyun status = ocfs2_extend_trans(handle, credits);
754*4882a593Smuzhiyun if (status < 0) {
755*4882a593Smuzhiyun status = -ENOMEM;
756*4882a593Smuzhiyun mlog_errno(status);
757*4882a593Smuzhiyun break;
758*4882a593Smuzhiyun }
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun }
761*4882a593Smuzhiyun
762*4882a593Smuzhiyun return status;
763*4882a593Smuzhiyun }
764*4882a593Smuzhiyun
__ocfs2_remove_xattr_range(struct inode * inode,struct ocfs2_xattr_value_buf * vb,u32 cpos,u32 phys_cpos,u32 len,unsigned int ext_flags,struct ocfs2_xattr_set_ctxt * ctxt)765*4882a593Smuzhiyun static int __ocfs2_remove_xattr_range(struct inode *inode,
766*4882a593Smuzhiyun struct ocfs2_xattr_value_buf *vb,
767*4882a593Smuzhiyun u32 cpos, u32 phys_cpos, u32 len,
768*4882a593Smuzhiyun unsigned int ext_flags,
769*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
770*4882a593Smuzhiyun {
771*4882a593Smuzhiyun int ret;
772*4882a593Smuzhiyun u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
773*4882a593Smuzhiyun handle_t *handle = ctxt->handle;
774*4882a593Smuzhiyun struct ocfs2_extent_tree et;
775*4882a593Smuzhiyun
776*4882a593Smuzhiyun ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
777*4882a593Smuzhiyun
778*4882a593Smuzhiyun ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
779*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
780*4882a593Smuzhiyun if (ret) {
781*4882a593Smuzhiyun mlog_errno(ret);
782*4882a593Smuzhiyun goto out;
783*4882a593Smuzhiyun }
784*4882a593Smuzhiyun
785*4882a593Smuzhiyun ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
786*4882a593Smuzhiyun &ctxt->dealloc);
787*4882a593Smuzhiyun if (ret) {
788*4882a593Smuzhiyun mlog_errno(ret);
789*4882a593Smuzhiyun goto out;
790*4882a593Smuzhiyun }
791*4882a593Smuzhiyun
792*4882a593Smuzhiyun le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
793*4882a593Smuzhiyun ocfs2_journal_dirty(handle, vb->vb_bh);
794*4882a593Smuzhiyun
795*4882a593Smuzhiyun if (ext_flags & OCFS2_EXT_REFCOUNTED)
796*4882a593Smuzhiyun ret = ocfs2_decrease_refcount(inode, handle,
797*4882a593Smuzhiyun ocfs2_blocks_to_clusters(inode->i_sb,
798*4882a593Smuzhiyun phys_blkno),
799*4882a593Smuzhiyun len, ctxt->meta_ac, &ctxt->dealloc, 1);
800*4882a593Smuzhiyun else
801*4882a593Smuzhiyun ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
802*4882a593Smuzhiyun phys_blkno, len);
803*4882a593Smuzhiyun if (ret)
804*4882a593Smuzhiyun mlog_errno(ret);
805*4882a593Smuzhiyun
806*4882a593Smuzhiyun out:
807*4882a593Smuzhiyun return ret;
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun
ocfs2_xattr_shrink_size(struct inode * inode,u32 old_clusters,u32 new_clusters,struct ocfs2_xattr_value_buf * vb,struct ocfs2_xattr_set_ctxt * ctxt)810*4882a593Smuzhiyun static int ocfs2_xattr_shrink_size(struct inode *inode,
811*4882a593Smuzhiyun u32 old_clusters,
812*4882a593Smuzhiyun u32 new_clusters,
813*4882a593Smuzhiyun struct ocfs2_xattr_value_buf *vb,
814*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
815*4882a593Smuzhiyun {
816*4882a593Smuzhiyun int ret = 0;
817*4882a593Smuzhiyun unsigned int ext_flags;
818*4882a593Smuzhiyun u32 trunc_len, cpos, phys_cpos, alloc_size;
819*4882a593Smuzhiyun u64 block;
820*4882a593Smuzhiyun
821*4882a593Smuzhiyun if (old_clusters <= new_clusters)
822*4882a593Smuzhiyun return 0;
823*4882a593Smuzhiyun
824*4882a593Smuzhiyun cpos = new_clusters;
825*4882a593Smuzhiyun trunc_len = old_clusters - new_clusters;
826*4882a593Smuzhiyun while (trunc_len) {
827*4882a593Smuzhiyun ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
828*4882a593Smuzhiyun &alloc_size,
829*4882a593Smuzhiyun &vb->vb_xv->xr_list, &ext_flags);
830*4882a593Smuzhiyun if (ret) {
831*4882a593Smuzhiyun mlog_errno(ret);
832*4882a593Smuzhiyun goto out;
833*4882a593Smuzhiyun }
834*4882a593Smuzhiyun
835*4882a593Smuzhiyun if (alloc_size > trunc_len)
836*4882a593Smuzhiyun alloc_size = trunc_len;
837*4882a593Smuzhiyun
838*4882a593Smuzhiyun ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
839*4882a593Smuzhiyun phys_cpos, alloc_size,
840*4882a593Smuzhiyun ext_flags, ctxt);
841*4882a593Smuzhiyun if (ret) {
842*4882a593Smuzhiyun mlog_errno(ret);
843*4882a593Smuzhiyun goto out;
844*4882a593Smuzhiyun }
845*4882a593Smuzhiyun
846*4882a593Smuzhiyun block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
847*4882a593Smuzhiyun ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
848*4882a593Smuzhiyun block, alloc_size);
849*4882a593Smuzhiyun cpos += alloc_size;
850*4882a593Smuzhiyun trunc_len -= alloc_size;
851*4882a593Smuzhiyun }
852*4882a593Smuzhiyun
853*4882a593Smuzhiyun out:
854*4882a593Smuzhiyun return ret;
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun
ocfs2_xattr_value_truncate(struct inode * inode,struct ocfs2_xattr_value_buf * vb,int len,struct ocfs2_xattr_set_ctxt * ctxt)857*4882a593Smuzhiyun static int ocfs2_xattr_value_truncate(struct inode *inode,
858*4882a593Smuzhiyun struct ocfs2_xattr_value_buf *vb,
859*4882a593Smuzhiyun int len,
860*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
861*4882a593Smuzhiyun {
862*4882a593Smuzhiyun int ret;
863*4882a593Smuzhiyun u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
864*4882a593Smuzhiyun u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun if (new_clusters == old_clusters)
867*4882a593Smuzhiyun return 0;
868*4882a593Smuzhiyun
869*4882a593Smuzhiyun if (new_clusters > old_clusters)
870*4882a593Smuzhiyun ret = ocfs2_xattr_extend_allocation(inode,
871*4882a593Smuzhiyun new_clusters - old_clusters,
872*4882a593Smuzhiyun vb, ctxt);
873*4882a593Smuzhiyun else
874*4882a593Smuzhiyun ret = ocfs2_xattr_shrink_size(inode,
875*4882a593Smuzhiyun old_clusters, new_clusters,
876*4882a593Smuzhiyun vb, ctxt);
877*4882a593Smuzhiyun
878*4882a593Smuzhiyun return ret;
879*4882a593Smuzhiyun }
880*4882a593Smuzhiyun
ocfs2_xattr_list_entry(struct super_block * sb,char * buffer,size_t size,size_t * result,int type,const char * name,int name_len)881*4882a593Smuzhiyun static int ocfs2_xattr_list_entry(struct super_block *sb,
882*4882a593Smuzhiyun char *buffer, size_t size,
883*4882a593Smuzhiyun size_t *result, int type,
884*4882a593Smuzhiyun const char *name, int name_len)
885*4882a593Smuzhiyun {
886*4882a593Smuzhiyun char *p = buffer + *result;
887*4882a593Smuzhiyun const char *prefix;
888*4882a593Smuzhiyun int prefix_len;
889*4882a593Smuzhiyun int total_len;
890*4882a593Smuzhiyun
891*4882a593Smuzhiyun switch(type) {
892*4882a593Smuzhiyun case OCFS2_XATTR_INDEX_USER:
893*4882a593Smuzhiyun if (OCFS2_SB(sb)->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
894*4882a593Smuzhiyun return 0;
895*4882a593Smuzhiyun break;
896*4882a593Smuzhiyun
897*4882a593Smuzhiyun case OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS:
898*4882a593Smuzhiyun case OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT:
899*4882a593Smuzhiyun if (!(sb->s_flags & SB_POSIXACL))
900*4882a593Smuzhiyun return 0;
901*4882a593Smuzhiyun break;
902*4882a593Smuzhiyun
903*4882a593Smuzhiyun case OCFS2_XATTR_INDEX_TRUSTED:
904*4882a593Smuzhiyun if (!capable(CAP_SYS_ADMIN))
905*4882a593Smuzhiyun return 0;
906*4882a593Smuzhiyun break;
907*4882a593Smuzhiyun }
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun prefix = ocfs2_xattr_prefix(type);
910*4882a593Smuzhiyun if (!prefix)
911*4882a593Smuzhiyun return 0;
912*4882a593Smuzhiyun prefix_len = strlen(prefix);
913*4882a593Smuzhiyun total_len = prefix_len + name_len + 1;
914*4882a593Smuzhiyun *result += total_len;
915*4882a593Smuzhiyun
916*4882a593Smuzhiyun /* we are just looking for how big our buffer needs to be */
917*4882a593Smuzhiyun if (!size)
918*4882a593Smuzhiyun return 0;
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun if (*result > size)
921*4882a593Smuzhiyun return -ERANGE;
922*4882a593Smuzhiyun
923*4882a593Smuzhiyun memcpy(p, prefix, prefix_len);
924*4882a593Smuzhiyun memcpy(p + prefix_len, name, name_len);
925*4882a593Smuzhiyun p[prefix_len + name_len] = '\0';
926*4882a593Smuzhiyun
927*4882a593Smuzhiyun return 0;
928*4882a593Smuzhiyun }
929*4882a593Smuzhiyun
ocfs2_xattr_list_entries(struct inode * inode,struct ocfs2_xattr_header * header,char * buffer,size_t buffer_size)930*4882a593Smuzhiyun static int ocfs2_xattr_list_entries(struct inode *inode,
931*4882a593Smuzhiyun struct ocfs2_xattr_header *header,
932*4882a593Smuzhiyun char *buffer, size_t buffer_size)
933*4882a593Smuzhiyun {
934*4882a593Smuzhiyun size_t result = 0;
935*4882a593Smuzhiyun int i, type, ret;
936*4882a593Smuzhiyun const char *name;
937*4882a593Smuzhiyun
938*4882a593Smuzhiyun for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
939*4882a593Smuzhiyun struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
940*4882a593Smuzhiyun type = ocfs2_xattr_get_type(entry);
941*4882a593Smuzhiyun name = (const char *)header +
942*4882a593Smuzhiyun le16_to_cpu(entry->xe_name_offset);
943*4882a593Smuzhiyun
944*4882a593Smuzhiyun ret = ocfs2_xattr_list_entry(inode->i_sb,
945*4882a593Smuzhiyun buffer, buffer_size,
946*4882a593Smuzhiyun &result, type, name,
947*4882a593Smuzhiyun entry->xe_name_len);
948*4882a593Smuzhiyun if (ret)
949*4882a593Smuzhiyun return ret;
950*4882a593Smuzhiyun }
951*4882a593Smuzhiyun
952*4882a593Smuzhiyun return result;
953*4882a593Smuzhiyun }
954*4882a593Smuzhiyun
ocfs2_has_inline_xattr_value_outside(struct inode * inode,struct ocfs2_dinode * di)955*4882a593Smuzhiyun int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
956*4882a593Smuzhiyun struct ocfs2_dinode *di)
957*4882a593Smuzhiyun {
958*4882a593Smuzhiyun struct ocfs2_xattr_header *xh;
959*4882a593Smuzhiyun int i;
960*4882a593Smuzhiyun
961*4882a593Smuzhiyun xh = (struct ocfs2_xattr_header *)
962*4882a593Smuzhiyun ((void *)di + inode->i_sb->s_blocksize -
963*4882a593Smuzhiyun le16_to_cpu(di->i_xattr_inline_size));
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
966*4882a593Smuzhiyun if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
967*4882a593Smuzhiyun return 1;
968*4882a593Smuzhiyun
969*4882a593Smuzhiyun return 0;
970*4882a593Smuzhiyun }
971*4882a593Smuzhiyun
ocfs2_xattr_ibody_list(struct inode * inode,struct ocfs2_dinode * di,char * buffer,size_t buffer_size)972*4882a593Smuzhiyun static int ocfs2_xattr_ibody_list(struct inode *inode,
973*4882a593Smuzhiyun struct ocfs2_dinode *di,
974*4882a593Smuzhiyun char *buffer,
975*4882a593Smuzhiyun size_t buffer_size)
976*4882a593Smuzhiyun {
977*4882a593Smuzhiyun struct ocfs2_xattr_header *header = NULL;
978*4882a593Smuzhiyun struct ocfs2_inode_info *oi = OCFS2_I(inode);
979*4882a593Smuzhiyun int ret = 0;
980*4882a593Smuzhiyun
981*4882a593Smuzhiyun if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
982*4882a593Smuzhiyun return ret;
983*4882a593Smuzhiyun
984*4882a593Smuzhiyun header = (struct ocfs2_xattr_header *)
985*4882a593Smuzhiyun ((void *)di + inode->i_sb->s_blocksize -
986*4882a593Smuzhiyun le16_to_cpu(di->i_xattr_inline_size));
987*4882a593Smuzhiyun
988*4882a593Smuzhiyun ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun return ret;
991*4882a593Smuzhiyun }
992*4882a593Smuzhiyun
ocfs2_xattr_block_list(struct inode * inode,struct ocfs2_dinode * di,char * buffer,size_t buffer_size)993*4882a593Smuzhiyun static int ocfs2_xattr_block_list(struct inode *inode,
994*4882a593Smuzhiyun struct ocfs2_dinode *di,
995*4882a593Smuzhiyun char *buffer,
996*4882a593Smuzhiyun size_t buffer_size)
997*4882a593Smuzhiyun {
998*4882a593Smuzhiyun struct buffer_head *blk_bh = NULL;
999*4882a593Smuzhiyun struct ocfs2_xattr_block *xb;
1000*4882a593Smuzhiyun int ret = 0;
1001*4882a593Smuzhiyun
1002*4882a593Smuzhiyun if (!di->i_xattr_loc)
1003*4882a593Smuzhiyun return ret;
1004*4882a593Smuzhiyun
1005*4882a593Smuzhiyun ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
1006*4882a593Smuzhiyun &blk_bh);
1007*4882a593Smuzhiyun if (ret < 0) {
1008*4882a593Smuzhiyun mlog_errno(ret);
1009*4882a593Smuzhiyun return ret;
1010*4882a593Smuzhiyun }
1011*4882a593Smuzhiyun
1012*4882a593Smuzhiyun xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1013*4882a593Smuzhiyun if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1014*4882a593Smuzhiyun struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
1015*4882a593Smuzhiyun ret = ocfs2_xattr_list_entries(inode, header,
1016*4882a593Smuzhiyun buffer, buffer_size);
1017*4882a593Smuzhiyun } else
1018*4882a593Smuzhiyun ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
1019*4882a593Smuzhiyun buffer, buffer_size);
1020*4882a593Smuzhiyun
1021*4882a593Smuzhiyun brelse(blk_bh);
1022*4882a593Smuzhiyun
1023*4882a593Smuzhiyun return ret;
1024*4882a593Smuzhiyun }
1025*4882a593Smuzhiyun
ocfs2_listxattr(struct dentry * dentry,char * buffer,size_t size)1026*4882a593Smuzhiyun ssize_t ocfs2_listxattr(struct dentry *dentry,
1027*4882a593Smuzhiyun char *buffer,
1028*4882a593Smuzhiyun size_t size)
1029*4882a593Smuzhiyun {
1030*4882a593Smuzhiyun int ret = 0, i_ret = 0, b_ret = 0;
1031*4882a593Smuzhiyun struct buffer_head *di_bh = NULL;
1032*4882a593Smuzhiyun struct ocfs2_dinode *di = NULL;
1033*4882a593Smuzhiyun struct ocfs2_inode_info *oi = OCFS2_I(d_inode(dentry));
1034*4882a593Smuzhiyun
1035*4882a593Smuzhiyun if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1036*4882a593Smuzhiyun return -EOPNOTSUPP;
1037*4882a593Smuzhiyun
1038*4882a593Smuzhiyun if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1039*4882a593Smuzhiyun return ret;
1040*4882a593Smuzhiyun
1041*4882a593Smuzhiyun ret = ocfs2_inode_lock(d_inode(dentry), &di_bh, 0);
1042*4882a593Smuzhiyun if (ret < 0) {
1043*4882a593Smuzhiyun mlog_errno(ret);
1044*4882a593Smuzhiyun return ret;
1045*4882a593Smuzhiyun }
1046*4882a593Smuzhiyun
1047*4882a593Smuzhiyun di = (struct ocfs2_dinode *)di_bh->b_data;
1048*4882a593Smuzhiyun
1049*4882a593Smuzhiyun down_read(&oi->ip_xattr_sem);
1050*4882a593Smuzhiyun i_ret = ocfs2_xattr_ibody_list(d_inode(dentry), di, buffer, size);
1051*4882a593Smuzhiyun if (i_ret < 0)
1052*4882a593Smuzhiyun b_ret = 0;
1053*4882a593Smuzhiyun else {
1054*4882a593Smuzhiyun if (buffer) {
1055*4882a593Smuzhiyun buffer += i_ret;
1056*4882a593Smuzhiyun size -= i_ret;
1057*4882a593Smuzhiyun }
1058*4882a593Smuzhiyun b_ret = ocfs2_xattr_block_list(d_inode(dentry), di,
1059*4882a593Smuzhiyun buffer, size);
1060*4882a593Smuzhiyun if (b_ret < 0)
1061*4882a593Smuzhiyun i_ret = 0;
1062*4882a593Smuzhiyun }
1063*4882a593Smuzhiyun up_read(&oi->ip_xattr_sem);
1064*4882a593Smuzhiyun ocfs2_inode_unlock(d_inode(dentry), 0);
1065*4882a593Smuzhiyun
1066*4882a593Smuzhiyun brelse(di_bh);
1067*4882a593Smuzhiyun
1068*4882a593Smuzhiyun return i_ret + b_ret;
1069*4882a593Smuzhiyun }
1070*4882a593Smuzhiyun
ocfs2_xattr_find_entry(int name_index,const char * name,struct ocfs2_xattr_search * xs)1071*4882a593Smuzhiyun static int ocfs2_xattr_find_entry(int name_index,
1072*4882a593Smuzhiyun const char *name,
1073*4882a593Smuzhiyun struct ocfs2_xattr_search *xs)
1074*4882a593Smuzhiyun {
1075*4882a593Smuzhiyun struct ocfs2_xattr_entry *entry;
1076*4882a593Smuzhiyun size_t name_len;
1077*4882a593Smuzhiyun int i, cmp = 1;
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun if (name == NULL)
1080*4882a593Smuzhiyun return -EINVAL;
1081*4882a593Smuzhiyun
1082*4882a593Smuzhiyun name_len = strlen(name);
1083*4882a593Smuzhiyun entry = xs->here;
1084*4882a593Smuzhiyun for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1085*4882a593Smuzhiyun cmp = name_index - ocfs2_xattr_get_type(entry);
1086*4882a593Smuzhiyun if (!cmp)
1087*4882a593Smuzhiyun cmp = name_len - entry->xe_name_len;
1088*4882a593Smuzhiyun if (!cmp)
1089*4882a593Smuzhiyun cmp = memcmp(name, (xs->base +
1090*4882a593Smuzhiyun le16_to_cpu(entry->xe_name_offset)),
1091*4882a593Smuzhiyun name_len);
1092*4882a593Smuzhiyun if (cmp == 0)
1093*4882a593Smuzhiyun break;
1094*4882a593Smuzhiyun entry += 1;
1095*4882a593Smuzhiyun }
1096*4882a593Smuzhiyun xs->here = entry;
1097*4882a593Smuzhiyun
1098*4882a593Smuzhiyun return cmp ? -ENODATA : 0;
1099*4882a593Smuzhiyun }
1100*4882a593Smuzhiyun
ocfs2_xattr_get_value_outside(struct inode * inode,struct ocfs2_xattr_value_root * xv,void * buffer,size_t len)1101*4882a593Smuzhiyun static int ocfs2_xattr_get_value_outside(struct inode *inode,
1102*4882a593Smuzhiyun struct ocfs2_xattr_value_root *xv,
1103*4882a593Smuzhiyun void *buffer,
1104*4882a593Smuzhiyun size_t len)
1105*4882a593Smuzhiyun {
1106*4882a593Smuzhiyun u32 cpos, p_cluster, num_clusters, bpc, clusters;
1107*4882a593Smuzhiyun u64 blkno;
1108*4882a593Smuzhiyun int i, ret = 0;
1109*4882a593Smuzhiyun size_t cplen, blocksize;
1110*4882a593Smuzhiyun struct buffer_head *bh = NULL;
1111*4882a593Smuzhiyun struct ocfs2_extent_list *el;
1112*4882a593Smuzhiyun
1113*4882a593Smuzhiyun el = &xv->xr_list;
1114*4882a593Smuzhiyun clusters = le32_to_cpu(xv->xr_clusters);
1115*4882a593Smuzhiyun bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1116*4882a593Smuzhiyun blocksize = inode->i_sb->s_blocksize;
1117*4882a593Smuzhiyun
1118*4882a593Smuzhiyun cpos = 0;
1119*4882a593Smuzhiyun while (cpos < clusters) {
1120*4882a593Smuzhiyun ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1121*4882a593Smuzhiyun &num_clusters, el, NULL);
1122*4882a593Smuzhiyun if (ret) {
1123*4882a593Smuzhiyun mlog_errno(ret);
1124*4882a593Smuzhiyun goto out;
1125*4882a593Smuzhiyun }
1126*4882a593Smuzhiyun
1127*4882a593Smuzhiyun blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1128*4882a593Smuzhiyun /* Copy ocfs2_xattr_value */
1129*4882a593Smuzhiyun for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1130*4882a593Smuzhiyun ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1131*4882a593Smuzhiyun &bh, NULL);
1132*4882a593Smuzhiyun if (ret) {
1133*4882a593Smuzhiyun mlog_errno(ret);
1134*4882a593Smuzhiyun goto out;
1135*4882a593Smuzhiyun }
1136*4882a593Smuzhiyun
1137*4882a593Smuzhiyun cplen = len >= blocksize ? blocksize : len;
1138*4882a593Smuzhiyun memcpy(buffer, bh->b_data, cplen);
1139*4882a593Smuzhiyun len -= cplen;
1140*4882a593Smuzhiyun buffer += cplen;
1141*4882a593Smuzhiyun
1142*4882a593Smuzhiyun brelse(bh);
1143*4882a593Smuzhiyun bh = NULL;
1144*4882a593Smuzhiyun if (len == 0)
1145*4882a593Smuzhiyun break;
1146*4882a593Smuzhiyun }
1147*4882a593Smuzhiyun cpos += num_clusters;
1148*4882a593Smuzhiyun }
1149*4882a593Smuzhiyun out:
1150*4882a593Smuzhiyun return ret;
1151*4882a593Smuzhiyun }
1152*4882a593Smuzhiyun
ocfs2_xattr_ibody_get(struct inode * inode,int name_index,const char * name,void * buffer,size_t buffer_size,struct ocfs2_xattr_search * xs)1153*4882a593Smuzhiyun static int ocfs2_xattr_ibody_get(struct inode *inode,
1154*4882a593Smuzhiyun int name_index,
1155*4882a593Smuzhiyun const char *name,
1156*4882a593Smuzhiyun void *buffer,
1157*4882a593Smuzhiyun size_t buffer_size,
1158*4882a593Smuzhiyun struct ocfs2_xattr_search *xs)
1159*4882a593Smuzhiyun {
1160*4882a593Smuzhiyun struct ocfs2_inode_info *oi = OCFS2_I(inode);
1161*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1162*4882a593Smuzhiyun struct ocfs2_xattr_value_root *xv;
1163*4882a593Smuzhiyun size_t size;
1164*4882a593Smuzhiyun int ret = 0;
1165*4882a593Smuzhiyun
1166*4882a593Smuzhiyun if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1167*4882a593Smuzhiyun return -ENODATA;
1168*4882a593Smuzhiyun
1169*4882a593Smuzhiyun xs->end = (void *)di + inode->i_sb->s_blocksize;
1170*4882a593Smuzhiyun xs->header = (struct ocfs2_xattr_header *)
1171*4882a593Smuzhiyun (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1172*4882a593Smuzhiyun xs->base = (void *)xs->header;
1173*4882a593Smuzhiyun xs->here = xs->header->xh_entries;
1174*4882a593Smuzhiyun
1175*4882a593Smuzhiyun ret = ocfs2_xattr_find_entry(name_index, name, xs);
1176*4882a593Smuzhiyun if (ret)
1177*4882a593Smuzhiyun return ret;
1178*4882a593Smuzhiyun size = le64_to_cpu(xs->here->xe_value_size);
1179*4882a593Smuzhiyun if (buffer) {
1180*4882a593Smuzhiyun if (size > buffer_size)
1181*4882a593Smuzhiyun return -ERANGE;
1182*4882a593Smuzhiyun if (ocfs2_xattr_is_local(xs->here)) {
1183*4882a593Smuzhiyun memcpy(buffer, (void *)xs->base +
1184*4882a593Smuzhiyun le16_to_cpu(xs->here->xe_name_offset) +
1185*4882a593Smuzhiyun OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1186*4882a593Smuzhiyun } else {
1187*4882a593Smuzhiyun xv = (struct ocfs2_xattr_value_root *)
1188*4882a593Smuzhiyun (xs->base + le16_to_cpu(
1189*4882a593Smuzhiyun xs->here->xe_name_offset) +
1190*4882a593Smuzhiyun OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1191*4882a593Smuzhiyun ret = ocfs2_xattr_get_value_outside(inode, xv,
1192*4882a593Smuzhiyun buffer, size);
1193*4882a593Smuzhiyun if (ret < 0) {
1194*4882a593Smuzhiyun mlog_errno(ret);
1195*4882a593Smuzhiyun return ret;
1196*4882a593Smuzhiyun }
1197*4882a593Smuzhiyun }
1198*4882a593Smuzhiyun }
1199*4882a593Smuzhiyun
1200*4882a593Smuzhiyun return size;
1201*4882a593Smuzhiyun }
1202*4882a593Smuzhiyun
ocfs2_xattr_block_get(struct inode * inode,int name_index,const char * name,void * buffer,size_t buffer_size,struct ocfs2_xattr_search * xs)1203*4882a593Smuzhiyun static int ocfs2_xattr_block_get(struct inode *inode,
1204*4882a593Smuzhiyun int name_index,
1205*4882a593Smuzhiyun const char *name,
1206*4882a593Smuzhiyun void *buffer,
1207*4882a593Smuzhiyun size_t buffer_size,
1208*4882a593Smuzhiyun struct ocfs2_xattr_search *xs)
1209*4882a593Smuzhiyun {
1210*4882a593Smuzhiyun struct ocfs2_xattr_block *xb;
1211*4882a593Smuzhiyun struct ocfs2_xattr_value_root *xv;
1212*4882a593Smuzhiyun size_t size;
1213*4882a593Smuzhiyun int ret = -ENODATA, name_offset, name_len, i;
1214*4882a593Smuzhiyun int block_off;
1215*4882a593Smuzhiyun
1216*4882a593Smuzhiyun xs->bucket = ocfs2_xattr_bucket_new(inode);
1217*4882a593Smuzhiyun if (!xs->bucket) {
1218*4882a593Smuzhiyun ret = -ENOMEM;
1219*4882a593Smuzhiyun mlog_errno(ret);
1220*4882a593Smuzhiyun goto cleanup;
1221*4882a593Smuzhiyun }
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1224*4882a593Smuzhiyun if (ret) {
1225*4882a593Smuzhiyun mlog_errno(ret);
1226*4882a593Smuzhiyun goto cleanup;
1227*4882a593Smuzhiyun }
1228*4882a593Smuzhiyun
1229*4882a593Smuzhiyun if (xs->not_found) {
1230*4882a593Smuzhiyun ret = -ENODATA;
1231*4882a593Smuzhiyun goto cleanup;
1232*4882a593Smuzhiyun }
1233*4882a593Smuzhiyun
1234*4882a593Smuzhiyun xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1235*4882a593Smuzhiyun size = le64_to_cpu(xs->here->xe_value_size);
1236*4882a593Smuzhiyun if (buffer) {
1237*4882a593Smuzhiyun ret = -ERANGE;
1238*4882a593Smuzhiyun if (size > buffer_size)
1239*4882a593Smuzhiyun goto cleanup;
1240*4882a593Smuzhiyun
1241*4882a593Smuzhiyun name_offset = le16_to_cpu(xs->here->xe_name_offset);
1242*4882a593Smuzhiyun name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1243*4882a593Smuzhiyun i = xs->here - xs->header->xh_entries;
1244*4882a593Smuzhiyun
1245*4882a593Smuzhiyun if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1246*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1247*4882a593Smuzhiyun bucket_xh(xs->bucket),
1248*4882a593Smuzhiyun i,
1249*4882a593Smuzhiyun &block_off,
1250*4882a593Smuzhiyun &name_offset);
1251*4882a593Smuzhiyun if (ret) {
1252*4882a593Smuzhiyun mlog_errno(ret);
1253*4882a593Smuzhiyun goto cleanup;
1254*4882a593Smuzhiyun }
1255*4882a593Smuzhiyun xs->base = bucket_block(xs->bucket, block_off);
1256*4882a593Smuzhiyun }
1257*4882a593Smuzhiyun if (ocfs2_xattr_is_local(xs->here)) {
1258*4882a593Smuzhiyun memcpy(buffer, (void *)xs->base +
1259*4882a593Smuzhiyun name_offset + name_len, size);
1260*4882a593Smuzhiyun } else {
1261*4882a593Smuzhiyun xv = (struct ocfs2_xattr_value_root *)
1262*4882a593Smuzhiyun (xs->base + name_offset + name_len);
1263*4882a593Smuzhiyun ret = ocfs2_xattr_get_value_outside(inode, xv,
1264*4882a593Smuzhiyun buffer, size);
1265*4882a593Smuzhiyun if (ret < 0) {
1266*4882a593Smuzhiyun mlog_errno(ret);
1267*4882a593Smuzhiyun goto cleanup;
1268*4882a593Smuzhiyun }
1269*4882a593Smuzhiyun }
1270*4882a593Smuzhiyun }
1271*4882a593Smuzhiyun ret = size;
1272*4882a593Smuzhiyun cleanup:
1273*4882a593Smuzhiyun ocfs2_xattr_bucket_free(xs->bucket);
1274*4882a593Smuzhiyun
1275*4882a593Smuzhiyun brelse(xs->xattr_bh);
1276*4882a593Smuzhiyun xs->xattr_bh = NULL;
1277*4882a593Smuzhiyun return ret;
1278*4882a593Smuzhiyun }
1279*4882a593Smuzhiyun
ocfs2_xattr_get_nolock(struct inode * inode,struct buffer_head * di_bh,int name_index,const char * name,void * buffer,size_t buffer_size)1280*4882a593Smuzhiyun int ocfs2_xattr_get_nolock(struct inode *inode,
1281*4882a593Smuzhiyun struct buffer_head *di_bh,
1282*4882a593Smuzhiyun int name_index,
1283*4882a593Smuzhiyun const char *name,
1284*4882a593Smuzhiyun void *buffer,
1285*4882a593Smuzhiyun size_t buffer_size)
1286*4882a593Smuzhiyun {
1287*4882a593Smuzhiyun int ret;
1288*4882a593Smuzhiyun struct ocfs2_dinode *di = NULL;
1289*4882a593Smuzhiyun struct ocfs2_inode_info *oi = OCFS2_I(inode);
1290*4882a593Smuzhiyun struct ocfs2_xattr_search xis = {
1291*4882a593Smuzhiyun .not_found = -ENODATA,
1292*4882a593Smuzhiyun };
1293*4882a593Smuzhiyun struct ocfs2_xattr_search xbs = {
1294*4882a593Smuzhiyun .not_found = -ENODATA,
1295*4882a593Smuzhiyun };
1296*4882a593Smuzhiyun
1297*4882a593Smuzhiyun if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1298*4882a593Smuzhiyun return -EOPNOTSUPP;
1299*4882a593Smuzhiyun
1300*4882a593Smuzhiyun if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1301*4882a593Smuzhiyun return -ENODATA;
1302*4882a593Smuzhiyun
1303*4882a593Smuzhiyun xis.inode_bh = xbs.inode_bh = di_bh;
1304*4882a593Smuzhiyun di = (struct ocfs2_dinode *)di_bh->b_data;
1305*4882a593Smuzhiyun
1306*4882a593Smuzhiyun ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1307*4882a593Smuzhiyun buffer_size, &xis);
1308*4882a593Smuzhiyun if (ret == -ENODATA && di->i_xattr_loc)
1309*4882a593Smuzhiyun ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1310*4882a593Smuzhiyun buffer_size, &xbs);
1311*4882a593Smuzhiyun
1312*4882a593Smuzhiyun return ret;
1313*4882a593Smuzhiyun }
1314*4882a593Smuzhiyun
1315*4882a593Smuzhiyun /* ocfs2_xattr_get()
1316*4882a593Smuzhiyun *
1317*4882a593Smuzhiyun * Copy an extended attribute into the buffer provided.
1318*4882a593Smuzhiyun * Buffer is NULL to compute the size of buffer required.
1319*4882a593Smuzhiyun */
ocfs2_xattr_get(struct inode * inode,int name_index,const char * name,void * buffer,size_t buffer_size)1320*4882a593Smuzhiyun static int ocfs2_xattr_get(struct inode *inode,
1321*4882a593Smuzhiyun int name_index,
1322*4882a593Smuzhiyun const char *name,
1323*4882a593Smuzhiyun void *buffer,
1324*4882a593Smuzhiyun size_t buffer_size)
1325*4882a593Smuzhiyun {
1326*4882a593Smuzhiyun int ret, had_lock;
1327*4882a593Smuzhiyun struct buffer_head *di_bh = NULL;
1328*4882a593Smuzhiyun struct ocfs2_lock_holder oh;
1329*4882a593Smuzhiyun
1330*4882a593Smuzhiyun had_lock = ocfs2_inode_lock_tracker(inode, &di_bh, 0, &oh);
1331*4882a593Smuzhiyun if (had_lock < 0) {
1332*4882a593Smuzhiyun mlog_errno(had_lock);
1333*4882a593Smuzhiyun return had_lock;
1334*4882a593Smuzhiyun }
1335*4882a593Smuzhiyun down_read(&OCFS2_I(inode)->ip_xattr_sem);
1336*4882a593Smuzhiyun ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1337*4882a593Smuzhiyun name, buffer, buffer_size);
1338*4882a593Smuzhiyun up_read(&OCFS2_I(inode)->ip_xattr_sem);
1339*4882a593Smuzhiyun
1340*4882a593Smuzhiyun ocfs2_inode_unlock_tracker(inode, 0, &oh, had_lock);
1341*4882a593Smuzhiyun
1342*4882a593Smuzhiyun brelse(di_bh);
1343*4882a593Smuzhiyun
1344*4882a593Smuzhiyun return ret;
1345*4882a593Smuzhiyun }
1346*4882a593Smuzhiyun
__ocfs2_xattr_set_value_outside(struct inode * inode,handle_t * handle,struct ocfs2_xattr_value_buf * vb,const void * value,int value_len)1347*4882a593Smuzhiyun static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1348*4882a593Smuzhiyun handle_t *handle,
1349*4882a593Smuzhiyun struct ocfs2_xattr_value_buf *vb,
1350*4882a593Smuzhiyun const void *value,
1351*4882a593Smuzhiyun int value_len)
1352*4882a593Smuzhiyun {
1353*4882a593Smuzhiyun int ret = 0, i, cp_len;
1354*4882a593Smuzhiyun u16 blocksize = inode->i_sb->s_blocksize;
1355*4882a593Smuzhiyun u32 p_cluster, num_clusters;
1356*4882a593Smuzhiyun u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1357*4882a593Smuzhiyun u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1358*4882a593Smuzhiyun u64 blkno;
1359*4882a593Smuzhiyun struct buffer_head *bh = NULL;
1360*4882a593Smuzhiyun unsigned int ext_flags;
1361*4882a593Smuzhiyun struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1362*4882a593Smuzhiyun
1363*4882a593Smuzhiyun BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1364*4882a593Smuzhiyun
1365*4882a593Smuzhiyun while (cpos < clusters) {
1366*4882a593Smuzhiyun ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1367*4882a593Smuzhiyun &num_clusters, &xv->xr_list,
1368*4882a593Smuzhiyun &ext_flags);
1369*4882a593Smuzhiyun if (ret) {
1370*4882a593Smuzhiyun mlog_errno(ret);
1371*4882a593Smuzhiyun goto out;
1372*4882a593Smuzhiyun }
1373*4882a593Smuzhiyun
1374*4882a593Smuzhiyun BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1375*4882a593Smuzhiyun
1376*4882a593Smuzhiyun blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1377*4882a593Smuzhiyun
1378*4882a593Smuzhiyun for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1379*4882a593Smuzhiyun ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1380*4882a593Smuzhiyun &bh, NULL);
1381*4882a593Smuzhiyun if (ret) {
1382*4882a593Smuzhiyun mlog_errno(ret);
1383*4882a593Smuzhiyun goto out;
1384*4882a593Smuzhiyun }
1385*4882a593Smuzhiyun
1386*4882a593Smuzhiyun ret = ocfs2_journal_access(handle,
1387*4882a593Smuzhiyun INODE_CACHE(inode),
1388*4882a593Smuzhiyun bh,
1389*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
1390*4882a593Smuzhiyun if (ret < 0) {
1391*4882a593Smuzhiyun mlog_errno(ret);
1392*4882a593Smuzhiyun goto out;
1393*4882a593Smuzhiyun }
1394*4882a593Smuzhiyun
1395*4882a593Smuzhiyun cp_len = value_len > blocksize ? blocksize : value_len;
1396*4882a593Smuzhiyun memcpy(bh->b_data, value, cp_len);
1397*4882a593Smuzhiyun value_len -= cp_len;
1398*4882a593Smuzhiyun value += cp_len;
1399*4882a593Smuzhiyun if (cp_len < blocksize)
1400*4882a593Smuzhiyun memset(bh->b_data + cp_len, 0,
1401*4882a593Smuzhiyun blocksize - cp_len);
1402*4882a593Smuzhiyun
1403*4882a593Smuzhiyun ocfs2_journal_dirty(handle, bh);
1404*4882a593Smuzhiyun brelse(bh);
1405*4882a593Smuzhiyun bh = NULL;
1406*4882a593Smuzhiyun
1407*4882a593Smuzhiyun /*
1408*4882a593Smuzhiyun * XXX: do we need to empty all the following
1409*4882a593Smuzhiyun * blocks in this cluster?
1410*4882a593Smuzhiyun */
1411*4882a593Smuzhiyun if (!value_len)
1412*4882a593Smuzhiyun break;
1413*4882a593Smuzhiyun }
1414*4882a593Smuzhiyun cpos += num_clusters;
1415*4882a593Smuzhiyun }
1416*4882a593Smuzhiyun out:
1417*4882a593Smuzhiyun brelse(bh);
1418*4882a593Smuzhiyun
1419*4882a593Smuzhiyun return ret;
1420*4882a593Smuzhiyun }
1421*4882a593Smuzhiyun
ocfs2_xa_check_space_helper(int needed_space,int free_start,int num_entries)1422*4882a593Smuzhiyun static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1423*4882a593Smuzhiyun int num_entries)
1424*4882a593Smuzhiyun {
1425*4882a593Smuzhiyun int free_space;
1426*4882a593Smuzhiyun
1427*4882a593Smuzhiyun if (!needed_space)
1428*4882a593Smuzhiyun return 0;
1429*4882a593Smuzhiyun
1430*4882a593Smuzhiyun free_space = free_start -
1431*4882a593Smuzhiyun sizeof(struct ocfs2_xattr_header) -
1432*4882a593Smuzhiyun (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1433*4882a593Smuzhiyun OCFS2_XATTR_HEADER_GAP;
1434*4882a593Smuzhiyun if (free_space < 0)
1435*4882a593Smuzhiyun return -EIO;
1436*4882a593Smuzhiyun if (free_space < needed_space)
1437*4882a593Smuzhiyun return -ENOSPC;
1438*4882a593Smuzhiyun
1439*4882a593Smuzhiyun return 0;
1440*4882a593Smuzhiyun }
1441*4882a593Smuzhiyun
ocfs2_xa_journal_access(handle_t * handle,struct ocfs2_xa_loc * loc,int type)1442*4882a593Smuzhiyun static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1443*4882a593Smuzhiyun int type)
1444*4882a593Smuzhiyun {
1445*4882a593Smuzhiyun return loc->xl_ops->xlo_journal_access(handle, loc, type);
1446*4882a593Smuzhiyun }
1447*4882a593Smuzhiyun
ocfs2_xa_journal_dirty(handle_t * handle,struct ocfs2_xa_loc * loc)1448*4882a593Smuzhiyun static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1449*4882a593Smuzhiyun {
1450*4882a593Smuzhiyun loc->xl_ops->xlo_journal_dirty(handle, loc);
1451*4882a593Smuzhiyun }
1452*4882a593Smuzhiyun
1453*4882a593Smuzhiyun /* Give a pointer into the storage for the given offset */
ocfs2_xa_offset_pointer(struct ocfs2_xa_loc * loc,int offset)1454*4882a593Smuzhiyun static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1455*4882a593Smuzhiyun {
1456*4882a593Smuzhiyun BUG_ON(offset >= loc->xl_size);
1457*4882a593Smuzhiyun return loc->xl_ops->xlo_offset_pointer(loc, offset);
1458*4882a593Smuzhiyun }
1459*4882a593Smuzhiyun
1460*4882a593Smuzhiyun /*
1461*4882a593Smuzhiyun * Wipe the name+value pair and allow the storage to reclaim it. This
1462*4882a593Smuzhiyun * must be followed by either removal of the entry or a call to
1463*4882a593Smuzhiyun * ocfs2_xa_add_namevalue().
1464*4882a593Smuzhiyun */
ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc * loc)1465*4882a593Smuzhiyun static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1466*4882a593Smuzhiyun {
1467*4882a593Smuzhiyun loc->xl_ops->xlo_wipe_namevalue(loc);
1468*4882a593Smuzhiyun }
1469*4882a593Smuzhiyun
1470*4882a593Smuzhiyun /*
1471*4882a593Smuzhiyun * Find lowest offset to a name+value pair. This is the start of our
1472*4882a593Smuzhiyun * downward-growing free space.
1473*4882a593Smuzhiyun */
ocfs2_xa_get_free_start(struct ocfs2_xa_loc * loc)1474*4882a593Smuzhiyun static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1475*4882a593Smuzhiyun {
1476*4882a593Smuzhiyun return loc->xl_ops->xlo_get_free_start(loc);
1477*4882a593Smuzhiyun }
1478*4882a593Smuzhiyun
1479*4882a593Smuzhiyun /* Can we reuse loc->xl_entry for xi? */
ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi)1480*4882a593Smuzhiyun static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1481*4882a593Smuzhiyun struct ocfs2_xattr_info *xi)
1482*4882a593Smuzhiyun {
1483*4882a593Smuzhiyun return loc->xl_ops->xlo_can_reuse(loc, xi);
1484*4882a593Smuzhiyun }
1485*4882a593Smuzhiyun
1486*4882a593Smuzhiyun /* How much free space is needed to set the new value */
ocfs2_xa_check_space(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi)1487*4882a593Smuzhiyun static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1488*4882a593Smuzhiyun struct ocfs2_xattr_info *xi)
1489*4882a593Smuzhiyun {
1490*4882a593Smuzhiyun return loc->xl_ops->xlo_check_space(loc, xi);
1491*4882a593Smuzhiyun }
1492*4882a593Smuzhiyun
ocfs2_xa_add_entry(struct ocfs2_xa_loc * loc,u32 name_hash)1493*4882a593Smuzhiyun static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1494*4882a593Smuzhiyun {
1495*4882a593Smuzhiyun loc->xl_ops->xlo_add_entry(loc, name_hash);
1496*4882a593Smuzhiyun loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1497*4882a593Smuzhiyun /*
1498*4882a593Smuzhiyun * We can't leave the new entry's xe_name_offset at zero or
1499*4882a593Smuzhiyun * add_namevalue() will go nuts. We set it to the size of our
1500*4882a593Smuzhiyun * storage so that it can never be less than any other entry.
1501*4882a593Smuzhiyun */
1502*4882a593Smuzhiyun loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1503*4882a593Smuzhiyun }
1504*4882a593Smuzhiyun
ocfs2_xa_add_namevalue(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi)1505*4882a593Smuzhiyun static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1506*4882a593Smuzhiyun struct ocfs2_xattr_info *xi)
1507*4882a593Smuzhiyun {
1508*4882a593Smuzhiyun int size = namevalue_size_xi(xi);
1509*4882a593Smuzhiyun int nameval_offset;
1510*4882a593Smuzhiyun char *nameval_buf;
1511*4882a593Smuzhiyun
1512*4882a593Smuzhiyun loc->xl_ops->xlo_add_namevalue(loc, size);
1513*4882a593Smuzhiyun loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1514*4882a593Smuzhiyun loc->xl_entry->xe_name_len = xi->xi_name_len;
1515*4882a593Smuzhiyun ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1516*4882a593Smuzhiyun ocfs2_xattr_set_local(loc->xl_entry,
1517*4882a593Smuzhiyun xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1518*4882a593Smuzhiyun
1519*4882a593Smuzhiyun nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1520*4882a593Smuzhiyun nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1521*4882a593Smuzhiyun memset(nameval_buf, 0, size);
1522*4882a593Smuzhiyun memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1523*4882a593Smuzhiyun }
1524*4882a593Smuzhiyun
ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_value_buf * vb)1525*4882a593Smuzhiyun static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1526*4882a593Smuzhiyun struct ocfs2_xattr_value_buf *vb)
1527*4882a593Smuzhiyun {
1528*4882a593Smuzhiyun int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1529*4882a593Smuzhiyun int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1530*4882a593Smuzhiyun
1531*4882a593Smuzhiyun /* Value bufs are for value trees */
1532*4882a593Smuzhiyun BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
1533*4882a593Smuzhiyun BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1534*4882a593Smuzhiyun (name_size + OCFS2_XATTR_ROOT_SIZE));
1535*4882a593Smuzhiyun
1536*4882a593Smuzhiyun loc->xl_ops->xlo_fill_value_buf(loc, vb);
1537*4882a593Smuzhiyun vb->vb_xv =
1538*4882a593Smuzhiyun (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1539*4882a593Smuzhiyun nameval_offset +
1540*4882a593Smuzhiyun name_size);
1541*4882a593Smuzhiyun }
1542*4882a593Smuzhiyun
ocfs2_xa_block_journal_access(handle_t * handle,struct ocfs2_xa_loc * loc,int type)1543*4882a593Smuzhiyun static int ocfs2_xa_block_journal_access(handle_t *handle,
1544*4882a593Smuzhiyun struct ocfs2_xa_loc *loc, int type)
1545*4882a593Smuzhiyun {
1546*4882a593Smuzhiyun struct buffer_head *bh = loc->xl_storage;
1547*4882a593Smuzhiyun ocfs2_journal_access_func access;
1548*4882a593Smuzhiyun
1549*4882a593Smuzhiyun if (loc->xl_size == (bh->b_size -
1550*4882a593Smuzhiyun offsetof(struct ocfs2_xattr_block,
1551*4882a593Smuzhiyun xb_attrs.xb_header)))
1552*4882a593Smuzhiyun access = ocfs2_journal_access_xb;
1553*4882a593Smuzhiyun else
1554*4882a593Smuzhiyun access = ocfs2_journal_access_di;
1555*4882a593Smuzhiyun return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1556*4882a593Smuzhiyun }
1557*4882a593Smuzhiyun
ocfs2_xa_block_journal_dirty(handle_t * handle,struct ocfs2_xa_loc * loc)1558*4882a593Smuzhiyun static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1559*4882a593Smuzhiyun struct ocfs2_xa_loc *loc)
1560*4882a593Smuzhiyun {
1561*4882a593Smuzhiyun struct buffer_head *bh = loc->xl_storage;
1562*4882a593Smuzhiyun
1563*4882a593Smuzhiyun ocfs2_journal_dirty(handle, bh);
1564*4882a593Smuzhiyun }
1565*4882a593Smuzhiyun
ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc * loc,int offset)1566*4882a593Smuzhiyun static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1567*4882a593Smuzhiyun int offset)
1568*4882a593Smuzhiyun {
1569*4882a593Smuzhiyun return (char *)loc->xl_header + offset;
1570*4882a593Smuzhiyun }
1571*4882a593Smuzhiyun
ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi)1572*4882a593Smuzhiyun static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1573*4882a593Smuzhiyun struct ocfs2_xattr_info *xi)
1574*4882a593Smuzhiyun {
1575*4882a593Smuzhiyun /*
1576*4882a593Smuzhiyun * Block storage is strict. If the sizes aren't exact, we will
1577*4882a593Smuzhiyun * remove the old one and reinsert the new.
1578*4882a593Smuzhiyun */
1579*4882a593Smuzhiyun return namevalue_size_xe(loc->xl_entry) ==
1580*4882a593Smuzhiyun namevalue_size_xi(xi);
1581*4882a593Smuzhiyun }
1582*4882a593Smuzhiyun
ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc * loc)1583*4882a593Smuzhiyun static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1584*4882a593Smuzhiyun {
1585*4882a593Smuzhiyun struct ocfs2_xattr_header *xh = loc->xl_header;
1586*4882a593Smuzhiyun int i, count = le16_to_cpu(xh->xh_count);
1587*4882a593Smuzhiyun int offset, free_start = loc->xl_size;
1588*4882a593Smuzhiyun
1589*4882a593Smuzhiyun for (i = 0; i < count; i++) {
1590*4882a593Smuzhiyun offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1591*4882a593Smuzhiyun if (offset < free_start)
1592*4882a593Smuzhiyun free_start = offset;
1593*4882a593Smuzhiyun }
1594*4882a593Smuzhiyun
1595*4882a593Smuzhiyun return free_start;
1596*4882a593Smuzhiyun }
1597*4882a593Smuzhiyun
ocfs2_xa_block_check_space(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi)1598*4882a593Smuzhiyun static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1599*4882a593Smuzhiyun struct ocfs2_xattr_info *xi)
1600*4882a593Smuzhiyun {
1601*4882a593Smuzhiyun int count = le16_to_cpu(loc->xl_header->xh_count);
1602*4882a593Smuzhiyun int free_start = ocfs2_xa_get_free_start(loc);
1603*4882a593Smuzhiyun int needed_space = ocfs2_xi_entry_usage(xi);
1604*4882a593Smuzhiyun
1605*4882a593Smuzhiyun /*
1606*4882a593Smuzhiyun * Block storage will reclaim the original entry before inserting
1607*4882a593Smuzhiyun * the new value, so we only need the difference. If the new
1608*4882a593Smuzhiyun * entry is smaller than the old one, we don't need anything.
1609*4882a593Smuzhiyun */
1610*4882a593Smuzhiyun if (loc->xl_entry) {
1611*4882a593Smuzhiyun /* Don't need space if we're reusing! */
1612*4882a593Smuzhiyun if (ocfs2_xa_can_reuse_entry(loc, xi))
1613*4882a593Smuzhiyun needed_space = 0;
1614*4882a593Smuzhiyun else
1615*4882a593Smuzhiyun needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1616*4882a593Smuzhiyun }
1617*4882a593Smuzhiyun if (needed_space < 0)
1618*4882a593Smuzhiyun needed_space = 0;
1619*4882a593Smuzhiyun return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1620*4882a593Smuzhiyun }
1621*4882a593Smuzhiyun
1622*4882a593Smuzhiyun /*
1623*4882a593Smuzhiyun * Block storage for xattrs keeps the name+value pairs compacted. When
1624*4882a593Smuzhiyun * we remove one, we have to shift any that preceded it towards the end.
1625*4882a593Smuzhiyun */
ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc * loc)1626*4882a593Smuzhiyun static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1627*4882a593Smuzhiyun {
1628*4882a593Smuzhiyun int i, offset;
1629*4882a593Smuzhiyun int namevalue_offset, first_namevalue_offset, namevalue_size;
1630*4882a593Smuzhiyun struct ocfs2_xattr_entry *entry = loc->xl_entry;
1631*4882a593Smuzhiyun struct ocfs2_xattr_header *xh = loc->xl_header;
1632*4882a593Smuzhiyun int count = le16_to_cpu(xh->xh_count);
1633*4882a593Smuzhiyun
1634*4882a593Smuzhiyun namevalue_offset = le16_to_cpu(entry->xe_name_offset);
1635*4882a593Smuzhiyun namevalue_size = namevalue_size_xe(entry);
1636*4882a593Smuzhiyun first_namevalue_offset = ocfs2_xa_get_free_start(loc);
1637*4882a593Smuzhiyun
1638*4882a593Smuzhiyun /* Shift the name+value pairs */
1639*4882a593Smuzhiyun memmove((char *)xh + first_namevalue_offset + namevalue_size,
1640*4882a593Smuzhiyun (char *)xh + first_namevalue_offset,
1641*4882a593Smuzhiyun namevalue_offset - first_namevalue_offset);
1642*4882a593Smuzhiyun memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1643*4882a593Smuzhiyun
1644*4882a593Smuzhiyun /* Now tell xh->xh_entries about it */
1645*4882a593Smuzhiyun for (i = 0; i < count; i++) {
1646*4882a593Smuzhiyun offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1647*4882a593Smuzhiyun if (offset <= namevalue_offset)
1648*4882a593Smuzhiyun le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1649*4882a593Smuzhiyun namevalue_size);
1650*4882a593Smuzhiyun }
1651*4882a593Smuzhiyun
1652*4882a593Smuzhiyun /*
1653*4882a593Smuzhiyun * Note that we don't update xh_free_start or xh_name_value_len
1654*4882a593Smuzhiyun * because they're not used in block-stored xattrs.
1655*4882a593Smuzhiyun */
1656*4882a593Smuzhiyun }
1657*4882a593Smuzhiyun
ocfs2_xa_block_add_entry(struct ocfs2_xa_loc * loc,u32 name_hash)1658*4882a593Smuzhiyun static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1659*4882a593Smuzhiyun {
1660*4882a593Smuzhiyun int count = le16_to_cpu(loc->xl_header->xh_count);
1661*4882a593Smuzhiyun loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1662*4882a593Smuzhiyun le16_add_cpu(&loc->xl_header->xh_count, 1);
1663*4882a593Smuzhiyun memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1664*4882a593Smuzhiyun }
1665*4882a593Smuzhiyun
ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc * loc,int size)1666*4882a593Smuzhiyun static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1667*4882a593Smuzhiyun {
1668*4882a593Smuzhiyun int free_start = ocfs2_xa_get_free_start(loc);
1669*4882a593Smuzhiyun
1670*4882a593Smuzhiyun loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1671*4882a593Smuzhiyun }
1672*4882a593Smuzhiyun
ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_value_buf * vb)1673*4882a593Smuzhiyun static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1674*4882a593Smuzhiyun struct ocfs2_xattr_value_buf *vb)
1675*4882a593Smuzhiyun {
1676*4882a593Smuzhiyun struct buffer_head *bh = loc->xl_storage;
1677*4882a593Smuzhiyun
1678*4882a593Smuzhiyun if (loc->xl_size == (bh->b_size -
1679*4882a593Smuzhiyun offsetof(struct ocfs2_xattr_block,
1680*4882a593Smuzhiyun xb_attrs.xb_header)))
1681*4882a593Smuzhiyun vb->vb_access = ocfs2_journal_access_xb;
1682*4882a593Smuzhiyun else
1683*4882a593Smuzhiyun vb->vb_access = ocfs2_journal_access_di;
1684*4882a593Smuzhiyun vb->vb_bh = bh;
1685*4882a593Smuzhiyun }
1686*4882a593Smuzhiyun
1687*4882a593Smuzhiyun /*
1688*4882a593Smuzhiyun * Operations for xattrs stored in blocks. This includes inline inode
1689*4882a593Smuzhiyun * storage and unindexed ocfs2_xattr_blocks.
1690*4882a593Smuzhiyun */
1691*4882a593Smuzhiyun static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
1692*4882a593Smuzhiyun .xlo_journal_access = ocfs2_xa_block_journal_access,
1693*4882a593Smuzhiyun .xlo_journal_dirty = ocfs2_xa_block_journal_dirty,
1694*4882a593Smuzhiyun .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
1695*4882a593Smuzhiyun .xlo_check_space = ocfs2_xa_block_check_space,
1696*4882a593Smuzhiyun .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1697*4882a593Smuzhiyun .xlo_get_free_start = ocfs2_xa_block_get_free_start,
1698*4882a593Smuzhiyun .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
1699*4882a593Smuzhiyun .xlo_add_entry = ocfs2_xa_block_add_entry,
1700*4882a593Smuzhiyun .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
1701*4882a593Smuzhiyun .xlo_fill_value_buf = ocfs2_xa_block_fill_value_buf,
1702*4882a593Smuzhiyun };
1703*4882a593Smuzhiyun
ocfs2_xa_bucket_journal_access(handle_t * handle,struct ocfs2_xa_loc * loc,int type)1704*4882a593Smuzhiyun static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1705*4882a593Smuzhiyun struct ocfs2_xa_loc *loc, int type)
1706*4882a593Smuzhiyun {
1707*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1708*4882a593Smuzhiyun
1709*4882a593Smuzhiyun return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1710*4882a593Smuzhiyun }
1711*4882a593Smuzhiyun
ocfs2_xa_bucket_journal_dirty(handle_t * handle,struct ocfs2_xa_loc * loc)1712*4882a593Smuzhiyun static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1713*4882a593Smuzhiyun struct ocfs2_xa_loc *loc)
1714*4882a593Smuzhiyun {
1715*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1716*4882a593Smuzhiyun
1717*4882a593Smuzhiyun ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1718*4882a593Smuzhiyun }
1719*4882a593Smuzhiyun
ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc * loc,int offset)1720*4882a593Smuzhiyun static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1721*4882a593Smuzhiyun int offset)
1722*4882a593Smuzhiyun {
1723*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1724*4882a593Smuzhiyun int block, block_offset;
1725*4882a593Smuzhiyun
1726*4882a593Smuzhiyun /* The header is at the front of the bucket */
1727*4882a593Smuzhiyun block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1728*4882a593Smuzhiyun block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
1729*4882a593Smuzhiyun
1730*4882a593Smuzhiyun return bucket_block(bucket, block) + block_offset;
1731*4882a593Smuzhiyun }
1732*4882a593Smuzhiyun
ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi)1733*4882a593Smuzhiyun static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1734*4882a593Smuzhiyun struct ocfs2_xattr_info *xi)
1735*4882a593Smuzhiyun {
1736*4882a593Smuzhiyun return namevalue_size_xe(loc->xl_entry) >=
1737*4882a593Smuzhiyun namevalue_size_xi(xi);
1738*4882a593Smuzhiyun }
1739*4882a593Smuzhiyun
ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc * loc)1740*4882a593Smuzhiyun static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1741*4882a593Smuzhiyun {
1742*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1743*4882a593Smuzhiyun return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1744*4882a593Smuzhiyun }
1745*4882a593Smuzhiyun
ocfs2_bucket_align_free_start(struct super_block * sb,int free_start,int size)1746*4882a593Smuzhiyun static int ocfs2_bucket_align_free_start(struct super_block *sb,
1747*4882a593Smuzhiyun int free_start, int size)
1748*4882a593Smuzhiyun {
1749*4882a593Smuzhiyun /*
1750*4882a593Smuzhiyun * We need to make sure that the name+value pair fits within
1751*4882a593Smuzhiyun * one block.
1752*4882a593Smuzhiyun */
1753*4882a593Smuzhiyun if (((free_start - size) >> sb->s_blocksize_bits) !=
1754*4882a593Smuzhiyun ((free_start - 1) >> sb->s_blocksize_bits))
1755*4882a593Smuzhiyun free_start -= free_start % sb->s_blocksize;
1756*4882a593Smuzhiyun
1757*4882a593Smuzhiyun return free_start;
1758*4882a593Smuzhiyun }
1759*4882a593Smuzhiyun
ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi)1760*4882a593Smuzhiyun static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1761*4882a593Smuzhiyun struct ocfs2_xattr_info *xi)
1762*4882a593Smuzhiyun {
1763*4882a593Smuzhiyun int rc;
1764*4882a593Smuzhiyun int count = le16_to_cpu(loc->xl_header->xh_count);
1765*4882a593Smuzhiyun int free_start = ocfs2_xa_get_free_start(loc);
1766*4882a593Smuzhiyun int needed_space = ocfs2_xi_entry_usage(xi);
1767*4882a593Smuzhiyun int size = namevalue_size_xi(xi);
1768*4882a593Smuzhiyun struct super_block *sb = loc->xl_inode->i_sb;
1769*4882a593Smuzhiyun
1770*4882a593Smuzhiyun /*
1771*4882a593Smuzhiyun * Bucket storage does not reclaim name+value pairs it cannot
1772*4882a593Smuzhiyun * reuse. They live as holes until the bucket fills, and then
1773*4882a593Smuzhiyun * the bucket is defragmented. However, the bucket can reclaim
1774*4882a593Smuzhiyun * the ocfs2_xattr_entry.
1775*4882a593Smuzhiyun */
1776*4882a593Smuzhiyun if (loc->xl_entry) {
1777*4882a593Smuzhiyun /* Don't need space if we're reusing! */
1778*4882a593Smuzhiyun if (ocfs2_xa_can_reuse_entry(loc, xi))
1779*4882a593Smuzhiyun needed_space = 0;
1780*4882a593Smuzhiyun else
1781*4882a593Smuzhiyun needed_space -= sizeof(struct ocfs2_xattr_entry);
1782*4882a593Smuzhiyun }
1783*4882a593Smuzhiyun BUG_ON(needed_space < 0);
1784*4882a593Smuzhiyun
1785*4882a593Smuzhiyun if (free_start < size) {
1786*4882a593Smuzhiyun if (needed_space)
1787*4882a593Smuzhiyun return -ENOSPC;
1788*4882a593Smuzhiyun } else {
1789*4882a593Smuzhiyun /*
1790*4882a593Smuzhiyun * First we check if it would fit in the first place.
1791*4882a593Smuzhiyun * Below, we align the free start to a block. This may
1792*4882a593Smuzhiyun * slide us below the minimum gap. By checking unaligned
1793*4882a593Smuzhiyun * first, we avoid that error.
1794*4882a593Smuzhiyun */
1795*4882a593Smuzhiyun rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1796*4882a593Smuzhiyun count);
1797*4882a593Smuzhiyun if (rc)
1798*4882a593Smuzhiyun return rc;
1799*4882a593Smuzhiyun free_start = ocfs2_bucket_align_free_start(sb, free_start,
1800*4882a593Smuzhiyun size);
1801*4882a593Smuzhiyun }
1802*4882a593Smuzhiyun return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1803*4882a593Smuzhiyun }
1804*4882a593Smuzhiyun
ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc * loc)1805*4882a593Smuzhiyun static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1806*4882a593Smuzhiyun {
1807*4882a593Smuzhiyun le16_add_cpu(&loc->xl_header->xh_name_value_len,
1808*4882a593Smuzhiyun -namevalue_size_xe(loc->xl_entry));
1809*4882a593Smuzhiyun }
1810*4882a593Smuzhiyun
ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc * loc,u32 name_hash)1811*4882a593Smuzhiyun static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1812*4882a593Smuzhiyun {
1813*4882a593Smuzhiyun struct ocfs2_xattr_header *xh = loc->xl_header;
1814*4882a593Smuzhiyun int count = le16_to_cpu(xh->xh_count);
1815*4882a593Smuzhiyun int low = 0, high = count - 1, tmp;
1816*4882a593Smuzhiyun struct ocfs2_xattr_entry *tmp_xe;
1817*4882a593Smuzhiyun
1818*4882a593Smuzhiyun /*
1819*4882a593Smuzhiyun * We keep buckets sorted by name_hash, so we need to find
1820*4882a593Smuzhiyun * our insert place.
1821*4882a593Smuzhiyun */
1822*4882a593Smuzhiyun while (low <= high && count) {
1823*4882a593Smuzhiyun tmp = (low + high) / 2;
1824*4882a593Smuzhiyun tmp_xe = &xh->xh_entries[tmp];
1825*4882a593Smuzhiyun
1826*4882a593Smuzhiyun if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1827*4882a593Smuzhiyun low = tmp + 1;
1828*4882a593Smuzhiyun else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1829*4882a593Smuzhiyun high = tmp - 1;
1830*4882a593Smuzhiyun else {
1831*4882a593Smuzhiyun low = tmp;
1832*4882a593Smuzhiyun break;
1833*4882a593Smuzhiyun }
1834*4882a593Smuzhiyun }
1835*4882a593Smuzhiyun
1836*4882a593Smuzhiyun if (low != count)
1837*4882a593Smuzhiyun memmove(&xh->xh_entries[low + 1],
1838*4882a593Smuzhiyun &xh->xh_entries[low],
1839*4882a593Smuzhiyun ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1840*4882a593Smuzhiyun
1841*4882a593Smuzhiyun le16_add_cpu(&xh->xh_count, 1);
1842*4882a593Smuzhiyun loc->xl_entry = &xh->xh_entries[low];
1843*4882a593Smuzhiyun memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1844*4882a593Smuzhiyun }
1845*4882a593Smuzhiyun
ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc * loc,int size)1846*4882a593Smuzhiyun static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1847*4882a593Smuzhiyun {
1848*4882a593Smuzhiyun int free_start = ocfs2_xa_get_free_start(loc);
1849*4882a593Smuzhiyun struct ocfs2_xattr_header *xh = loc->xl_header;
1850*4882a593Smuzhiyun struct super_block *sb = loc->xl_inode->i_sb;
1851*4882a593Smuzhiyun int nameval_offset;
1852*4882a593Smuzhiyun
1853*4882a593Smuzhiyun free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1854*4882a593Smuzhiyun nameval_offset = free_start - size;
1855*4882a593Smuzhiyun loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1856*4882a593Smuzhiyun xh->xh_free_start = cpu_to_le16(nameval_offset);
1857*4882a593Smuzhiyun le16_add_cpu(&xh->xh_name_value_len, size);
1858*4882a593Smuzhiyun
1859*4882a593Smuzhiyun }
1860*4882a593Smuzhiyun
ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_value_buf * vb)1861*4882a593Smuzhiyun static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1862*4882a593Smuzhiyun struct ocfs2_xattr_value_buf *vb)
1863*4882a593Smuzhiyun {
1864*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1865*4882a593Smuzhiyun struct super_block *sb = loc->xl_inode->i_sb;
1866*4882a593Smuzhiyun int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1867*4882a593Smuzhiyun int size = namevalue_size_xe(loc->xl_entry);
1868*4882a593Smuzhiyun int block_offset = nameval_offset >> sb->s_blocksize_bits;
1869*4882a593Smuzhiyun
1870*4882a593Smuzhiyun /* Values are not allowed to straddle block boundaries */
1871*4882a593Smuzhiyun BUG_ON(block_offset !=
1872*4882a593Smuzhiyun ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1873*4882a593Smuzhiyun /* We expect the bucket to be filled in */
1874*4882a593Smuzhiyun BUG_ON(!bucket->bu_bhs[block_offset]);
1875*4882a593Smuzhiyun
1876*4882a593Smuzhiyun vb->vb_access = ocfs2_journal_access;
1877*4882a593Smuzhiyun vb->vb_bh = bucket->bu_bhs[block_offset];
1878*4882a593Smuzhiyun }
1879*4882a593Smuzhiyun
1880*4882a593Smuzhiyun /* Operations for xattrs stored in buckets. */
1881*4882a593Smuzhiyun static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
1882*4882a593Smuzhiyun .xlo_journal_access = ocfs2_xa_bucket_journal_access,
1883*4882a593Smuzhiyun .xlo_journal_dirty = ocfs2_xa_bucket_journal_dirty,
1884*4882a593Smuzhiyun .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
1885*4882a593Smuzhiyun .xlo_check_space = ocfs2_xa_bucket_check_space,
1886*4882a593Smuzhiyun .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1887*4882a593Smuzhiyun .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
1888*4882a593Smuzhiyun .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
1889*4882a593Smuzhiyun .xlo_add_entry = ocfs2_xa_bucket_add_entry,
1890*4882a593Smuzhiyun .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
1891*4882a593Smuzhiyun .xlo_fill_value_buf = ocfs2_xa_bucket_fill_value_buf,
1892*4882a593Smuzhiyun };
1893*4882a593Smuzhiyun
ocfs2_xa_value_clusters(struct ocfs2_xa_loc * loc)1894*4882a593Smuzhiyun static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc *loc)
1895*4882a593Smuzhiyun {
1896*4882a593Smuzhiyun struct ocfs2_xattr_value_buf vb;
1897*4882a593Smuzhiyun
1898*4882a593Smuzhiyun if (ocfs2_xattr_is_local(loc->xl_entry))
1899*4882a593Smuzhiyun return 0;
1900*4882a593Smuzhiyun
1901*4882a593Smuzhiyun ocfs2_xa_fill_value_buf(loc, &vb);
1902*4882a593Smuzhiyun return le32_to_cpu(vb.vb_xv->xr_clusters);
1903*4882a593Smuzhiyun }
1904*4882a593Smuzhiyun
ocfs2_xa_value_truncate(struct ocfs2_xa_loc * loc,u64 bytes,struct ocfs2_xattr_set_ctxt * ctxt)1905*4882a593Smuzhiyun static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
1906*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
1907*4882a593Smuzhiyun {
1908*4882a593Smuzhiyun int trunc_rc, access_rc;
1909*4882a593Smuzhiyun struct ocfs2_xattr_value_buf vb;
1910*4882a593Smuzhiyun
1911*4882a593Smuzhiyun ocfs2_xa_fill_value_buf(loc, &vb);
1912*4882a593Smuzhiyun trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
1913*4882a593Smuzhiyun ctxt);
1914*4882a593Smuzhiyun
1915*4882a593Smuzhiyun /*
1916*4882a593Smuzhiyun * The caller of ocfs2_xa_value_truncate() has already called
1917*4882a593Smuzhiyun * ocfs2_xa_journal_access on the loc. However, The truncate code
1918*4882a593Smuzhiyun * calls ocfs2_extend_trans(). This may commit the previous
1919*4882a593Smuzhiyun * transaction and open a new one. If this is a bucket, truncate
1920*4882a593Smuzhiyun * could leave only vb->vb_bh set up for journaling. Meanwhile,
1921*4882a593Smuzhiyun * the caller is expecting to dirty the entire bucket. So we must
1922*4882a593Smuzhiyun * reset the journal work. We do this even if truncate has failed,
1923*4882a593Smuzhiyun * as it could have failed after committing the extend.
1924*4882a593Smuzhiyun */
1925*4882a593Smuzhiyun access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
1926*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
1927*4882a593Smuzhiyun
1928*4882a593Smuzhiyun /* Errors in truncate take precedence */
1929*4882a593Smuzhiyun return trunc_rc ? trunc_rc : access_rc;
1930*4882a593Smuzhiyun }
1931*4882a593Smuzhiyun
ocfs2_xa_remove_entry(struct ocfs2_xa_loc * loc)1932*4882a593Smuzhiyun static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1933*4882a593Smuzhiyun {
1934*4882a593Smuzhiyun int index, count;
1935*4882a593Smuzhiyun struct ocfs2_xattr_header *xh = loc->xl_header;
1936*4882a593Smuzhiyun struct ocfs2_xattr_entry *entry = loc->xl_entry;
1937*4882a593Smuzhiyun
1938*4882a593Smuzhiyun ocfs2_xa_wipe_namevalue(loc);
1939*4882a593Smuzhiyun loc->xl_entry = NULL;
1940*4882a593Smuzhiyun
1941*4882a593Smuzhiyun le16_add_cpu(&xh->xh_count, -1);
1942*4882a593Smuzhiyun count = le16_to_cpu(xh->xh_count);
1943*4882a593Smuzhiyun
1944*4882a593Smuzhiyun /*
1945*4882a593Smuzhiyun * Only zero out the entry if there are more remaining. This is
1946*4882a593Smuzhiyun * important for an empty bucket, as it keeps track of the
1947*4882a593Smuzhiyun * bucket's hash value. It doesn't hurt empty block storage.
1948*4882a593Smuzhiyun */
1949*4882a593Smuzhiyun if (count) {
1950*4882a593Smuzhiyun index = ((char *)entry - (char *)&xh->xh_entries) /
1951*4882a593Smuzhiyun sizeof(struct ocfs2_xattr_entry);
1952*4882a593Smuzhiyun memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1953*4882a593Smuzhiyun (count - index) * sizeof(struct ocfs2_xattr_entry));
1954*4882a593Smuzhiyun memset(&xh->xh_entries[count], 0,
1955*4882a593Smuzhiyun sizeof(struct ocfs2_xattr_entry));
1956*4882a593Smuzhiyun }
1957*4882a593Smuzhiyun }
1958*4882a593Smuzhiyun
1959*4882a593Smuzhiyun /*
1960*4882a593Smuzhiyun * If we have a problem adjusting the size of an external value during
1961*4882a593Smuzhiyun * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1962*4882a593Smuzhiyun * in an intermediate state. For example, the value may be partially
1963*4882a593Smuzhiyun * truncated.
1964*4882a593Smuzhiyun *
1965*4882a593Smuzhiyun * If the value tree hasn't changed, the extend/truncate went nowhere.
1966*4882a593Smuzhiyun * We have nothing to do. The caller can treat it as a straight error.
1967*4882a593Smuzhiyun *
1968*4882a593Smuzhiyun * If the value tree got partially truncated, we now have a corrupted
1969*4882a593Smuzhiyun * extended attribute. We're going to wipe its entry and leak the
1970*4882a593Smuzhiyun * clusters. Better to leak some storage than leave a corrupt entry.
1971*4882a593Smuzhiyun *
1972*4882a593Smuzhiyun * If the value tree grew, it obviously didn't grow enough for the
1973*4882a593Smuzhiyun * new entry. We're not going to try and reclaim those clusters either.
1974*4882a593Smuzhiyun * If there was already an external value there (orig_clusters != 0),
1975*4882a593Smuzhiyun * the new clusters are attached safely and we can just leave the old
1976*4882a593Smuzhiyun * value in place. If there was no external value there, we remove
1977*4882a593Smuzhiyun * the entry.
1978*4882a593Smuzhiyun *
1979*4882a593Smuzhiyun * This way, the xattr block we store in the journal will be consistent.
1980*4882a593Smuzhiyun * If the size change broke because of the journal, no changes will hit
1981*4882a593Smuzhiyun * disk anyway.
1982*4882a593Smuzhiyun */
ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc * loc,const char * what,unsigned int orig_clusters)1983*4882a593Smuzhiyun static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc *loc,
1984*4882a593Smuzhiyun const char *what,
1985*4882a593Smuzhiyun unsigned int orig_clusters)
1986*4882a593Smuzhiyun {
1987*4882a593Smuzhiyun unsigned int new_clusters = ocfs2_xa_value_clusters(loc);
1988*4882a593Smuzhiyun char *nameval_buf = ocfs2_xa_offset_pointer(loc,
1989*4882a593Smuzhiyun le16_to_cpu(loc->xl_entry->xe_name_offset));
1990*4882a593Smuzhiyun
1991*4882a593Smuzhiyun if (new_clusters < orig_clusters) {
1992*4882a593Smuzhiyun mlog(ML_ERROR,
1993*4882a593Smuzhiyun "Partial truncate while %s xattr %.*s. Leaking "
1994*4882a593Smuzhiyun "%u clusters and removing the entry\n",
1995*4882a593Smuzhiyun what, loc->xl_entry->xe_name_len, nameval_buf,
1996*4882a593Smuzhiyun orig_clusters - new_clusters);
1997*4882a593Smuzhiyun ocfs2_xa_remove_entry(loc);
1998*4882a593Smuzhiyun } else if (!orig_clusters) {
1999*4882a593Smuzhiyun mlog(ML_ERROR,
2000*4882a593Smuzhiyun "Unable to allocate an external value for xattr "
2001*4882a593Smuzhiyun "%.*s safely. Leaking %u clusters and removing the "
2002*4882a593Smuzhiyun "entry\n",
2003*4882a593Smuzhiyun loc->xl_entry->xe_name_len, nameval_buf,
2004*4882a593Smuzhiyun new_clusters - orig_clusters);
2005*4882a593Smuzhiyun ocfs2_xa_remove_entry(loc);
2006*4882a593Smuzhiyun } else if (new_clusters > orig_clusters)
2007*4882a593Smuzhiyun mlog(ML_ERROR,
2008*4882a593Smuzhiyun "Unable to grow xattr %.*s safely. %u new clusters "
2009*4882a593Smuzhiyun "have been added, but the value will not be "
2010*4882a593Smuzhiyun "modified\n",
2011*4882a593Smuzhiyun loc->xl_entry->xe_name_len, nameval_buf,
2012*4882a593Smuzhiyun new_clusters - orig_clusters);
2013*4882a593Smuzhiyun }
2014*4882a593Smuzhiyun
ocfs2_xa_remove(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_set_ctxt * ctxt)2015*4882a593Smuzhiyun static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
2016*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
2017*4882a593Smuzhiyun {
2018*4882a593Smuzhiyun int rc = 0;
2019*4882a593Smuzhiyun unsigned int orig_clusters;
2020*4882a593Smuzhiyun
2021*4882a593Smuzhiyun if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2022*4882a593Smuzhiyun orig_clusters = ocfs2_xa_value_clusters(loc);
2023*4882a593Smuzhiyun rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2024*4882a593Smuzhiyun if (rc) {
2025*4882a593Smuzhiyun mlog_errno(rc);
2026*4882a593Smuzhiyun /*
2027*4882a593Smuzhiyun * Since this is remove, we can return 0 if
2028*4882a593Smuzhiyun * ocfs2_xa_cleanup_value_truncate() is going to
2029*4882a593Smuzhiyun * wipe the entry anyway. So we check the
2030*4882a593Smuzhiyun * cluster count as well.
2031*4882a593Smuzhiyun */
2032*4882a593Smuzhiyun if (orig_clusters != ocfs2_xa_value_clusters(loc))
2033*4882a593Smuzhiyun rc = 0;
2034*4882a593Smuzhiyun ocfs2_xa_cleanup_value_truncate(loc, "removing",
2035*4882a593Smuzhiyun orig_clusters);
2036*4882a593Smuzhiyun if (rc)
2037*4882a593Smuzhiyun goto out;
2038*4882a593Smuzhiyun }
2039*4882a593Smuzhiyun }
2040*4882a593Smuzhiyun
2041*4882a593Smuzhiyun ocfs2_xa_remove_entry(loc);
2042*4882a593Smuzhiyun
2043*4882a593Smuzhiyun out:
2044*4882a593Smuzhiyun return rc;
2045*4882a593Smuzhiyun }
2046*4882a593Smuzhiyun
ocfs2_xa_install_value_root(struct ocfs2_xa_loc * loc)2047*4882a593Smuzhiyun static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
2048*4882a593Smuzhiyun {
2049*4882a593Smuzhiyun int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
2050*4882a593Smuzhiyun char *nameval_buf;
2051*4882a593Smuzhiyun
2052*4882a593Smuzhiyun nameval_buf = ocfs2_xa_offset_pointer(loc,
2053*4882a593Smuzhiyun le16_to_cpu(loc->xl_entry->xe_name_offset));
2054*4882a593Smuzhiyun memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
2055*4882a593Smuzhiyun }
2056*4882a593Smuzhiyun
2057*4882a593Smuzhiyun /*
2058*4882a593Smuzhiyun * Take an existing entry and make it ready for the new value. This
2059*4882a593Smuzhiyun * won't allocate space, but it may free space. It should be ready for
2060*4882a593Smuzhiyun * ocfs2_xa_prepare_entry() to finish the work.
2061*4882a593Smuzhiyun */
ocfs2_xa_reuse_entry(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_set_ctxt * ctxt)2062*4882a593Smuzhiyun static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
2063*4882a593Smuzhiyun struct ocfs2_xattr_info *xi,
2064*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
2065*4882a593Smuzhiyun {
2066*4882a593Smuzhiyun int rc = 0;
2067*4882a593Smuzhiyun int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2068*4882a593Smuzhiyun unsigned int orig_clusters;
2069*4882a593Smuzhiyun char *nameval_buf;
2070*4882a593Smuzhiyun int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
2071*4882a593Smuzhiyun int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
2072*4882a593Smuzhiyun
2073*4882a593Smuzhiyun BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
2074*4882a593Smuzhiyun name_size);
2075*4882a593Smuzhiyun
2076*4882a593Smuzhiyun nameval_buf = ocfs2_xa_offset_pointer(loc,
2077*4882a593Smuzhiyun le16_to_cpu(loc->xl_entry->xe_name_offset));
2078*4882a593Smuzhiyun if (xe_local) {
2079*4882a593Smuzhiyun memset(nameval_buf + name_size, 0,
2080*4882a593Smuzhiyun namevalue_size_xe(loc->xl_entry) - name_size);
2081*4882a593Smuzhiyun if (!xi_local)
2082*4882a593Smuzhiyun ocfs2_xa_install_value_root(loc);
2083*4882a593Smuzhiyun } else {
2084*4882a593Smuzhiyun orig_clusters = ocfs2_xa_value_clusters(loc);
2085*4882a593Smuzhiyun if (xi_local) {
2086*4882a593Smuzhiyun rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2087*4882a593Smuzhiyun if (rc < 0)
2088*4882a593Smuzhiyun mlog_errno(rc);
2089*4882a593Smuzhiyun else
2090*4882a593Smuzhiyun memset(nameval_buf + name_size, 0,
2091*4882a593Smuzhiyun namevalue_size_xe(loc->xl_entry) -
2092*4882a593Smuzhiyun name_size);
2093*4882a593Smuzhiyun } else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
2094*4882a593Smuzhiyun xi->xi_value_len) {
2095*4882a593Smuzhiyun rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
2096*4882a593Smuzhiyun ctxt);
2097*4882a593Smuzhiyun if (rc < 0)
2098*4882a593Smuzhiyun mlog_errno(rc);
2099*4882a593Smuzhiyun }
2100*4882a593Smuzhiyun
2101*4882a593Smuzhiyun if (rc) {
2102*4882a593Smuzhiyun ocfs2_xa_cleanup_value_truncate(loc, "reusing",
2103*4882a593Smuzhiyun orig_clusters);
2104*4882a593Smuzhiyun goto out;
2105*4882a593Smuzhiyun }
2106*4882a593Smuzhiyun }
2107*4882a593Smuzhiyun
2108*4882a593Smuzhiyun loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
2109*4882a593Smuzhiyun ocfs2_xattr_set_local(loc->xl_entry, xi_local);
2110*4882a593Smuzhiyun
2111*4882a593Smuzhiyun out:
2112*4882a593Smuzhiyun return rc;
2113*4882a593Smuzhiyun }
2114*4882a593Smuzhiyun
2115*4882a593Smuzhiyun /*
2116*4882a593Smuzhiyun * Prepares loc->xl_entry to receive the new xattr. This includes
2117*4882a593Smuzhiyun * properly setting up the name+value pair region. If loc->xl_entry
2118*4882a593Smuzhiyun * already exists, it will take care of modifying it appropriately.
2119*4882a593Smuzhiyun *
2120*4882a593Smuzhiyun * Note that this modifies the data. You did journal_access already,
2121*4882a593Smuzhiyun * right?
2122*4882a593Smuzhiyun */
ocfs2_xa_prepare_entry(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi,u32 name_hash,struct ocfs2_xattr_set_ctxt * ctxt)2123*4882a593Smuzhiyun static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
2124*4882a593Smuzhiyun struct ocfs2_xattr_info *xi,
2125*4882a593Smuzhiyun u32 name_hash,
2126*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
2127*4882a593Smuzhiyun {
2128*4882a593Smuzhiyun int rc = 0;
2129*4882a593Smuzhiyun unsigned int orig_clusters;
2130*4882a593Smuzhiyun __le64 orig_value_size = 0;
2131*4882a593Smuzhiyun
2132*4882a593Smuzhiyun rc = ocfs2_xa_check_space(loc, xi);
2133*4882a593Smuzhiyun if (rc)
2134*4882a593Smuzhiyun goto out;
2135*4882a593Smuzhiyun
2136*4882a593Smuzhiyun if (loc->xl_entry) {
2137*4882a593Smuzhiyun if (ocfs2_xa_can_reuse_entry(loc, xi)) {
2138*4882a593Smuzhiyun orig_value_size = loc->xl_entry->xe_value_size;
2139*4882a593Smuzhiyun rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
2140*4882a593Smuzhiyun if (rc)
2141*4882a593Smuzhiyun goto out;
2142*4882a593Smuzhiyun goto alloc_value;
2143*4882a593Smuzhiyun }
2144*4882a593Smuzhiyun
2145*4882a593Smuzhiyun if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2146*4882a593Smuzhiyun orig_clusters = ocfs2_xa_value_clusters(loc);
2147*4882a593Smuzhiyun rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2148*4882a593Smuzhiyun if (rc) {
2149*4882a593Smuzhiyun mlog_errno(rc);
2150*4882a593Smuzhiyun ocfs2_xa_cleanup_value_truncate(loc,
2151*4882a593Smuzhiyun "overwriting",
2152*4882a593Smuzhiyun orig_clusters);
2153*4882a593Smuzhiyun goto out;
2154*4882a593Smuzhiyun }
2155*4882a593Smuzhiyun }
2156*4882a593Smuzhiyun ocfs2_xa_wipe_namevalue(loc);
2157*4882a593Smuzhiyun } else
2158*4882a593Smuzhiyun ocfs2_xa_add_entry(loc, name_hash);
2159*4882a593Smuzhiyun
2160*4882a593Smuzhiyun /*
2161*4882a593Smuzhiyun * If we get here, we have a blank entry. Fill it. We grow our
2162*4882a593Smuzhiyun * name+value pair back from the end.
2163*4882a593Smuzhiyun */
2164*4882a593Smuzhiyun ocfs2_xa_add_namevalue(loc, xi);
2165*4882a593Smuzhiyun if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2166*4882a593Smuzhiyun ocfs2_xa_install_value_root(loc);
2167*4882a593Smuzhiyun
2168*4882a593Smuzhiyun alloc_value:
2169*4882a593Smuzhiyun if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2170*4882a593Smuzhiyun orig_clusters = ocfs2_xa_value_clusters(loc);
2171*4882a593Smuzhiyun rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
2172*4882a593Smuzhiyun if (rc < 0) {
2173*4882a593Smuzhiyun ctxt->set_abort = 1;
2174*4882a593Smuzhiyun ocfs2_xa_cleanup_value_truncate(loc, "growing",
2175*4882a593Smuzhiyun orig_clusters);
2176*4882a593Smuzhiyun /*
2177*4882a593Smuzhiyun * If we were growing an existing value,
2178*4882a593Smuzhiyun * ocfs2_xa_cleanup_value_truncate() won't remove
2179*4882a593Smuzhiyun * the entry. We need to restore the original value
2180*4882a593Smuzhiyun * size.
2181*4882a593Smuzhiyun */
2182*4882a593Smuzhiyun if (loc->xl_entry) {
2183*4882a593Smuzhiyun BUG_ON(!orig_value_size);
2184*4882a593Smuzhiyun loc->xl_entry->xe_value_size = orig_value_size;
2185*4882a593Smuzhiyun }
2186*4882a593Smuzhiyun mlog_errno(rc);
2187*4882a593Smuzhiyun }
2188*4882a593Smuzhiyun }
2189*4882a593Smuzhiyun
2190*4882a593Smuzhiyun out:
2191*4882a593Smuzhiyun return rc;
2192*4882a593Smuzhiyun }
2193*4882a593Smuzhiyun
2194*4882a593Smuzhiyun /*
2195*4882a593Smuzhiyun * Store the value portion of the name+value pair. This will skip
2196*4882a593Smuzhiyun * values that are stored externally. Their tree roots were set up
2197*4882a593Smuzhiyun * by ocfs2_xa_prepare_entry().
2198*4882a593Smuzhiyun */
ocfs2_xa_store_value(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_set_ctxt * ctxt)2199*4882a593Smuzhiyun static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
2200*4882a593Smuzhiyun struct ocfs2_xattr_info *xi,
2201*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
2202*4882a593Smuzhiyun {
2203*4882a593Smuzhiyun int rc = 0;
2204*4882a593Smuzhiyun int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2205*4882a593Smuzhiyun int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2206*4882a593Smuzhiyun char *nameval_buf;
2207*4882a593Smuzhiyun struct ocfs2_xattr_value_buf vb;
2208*4882a593Smuzhiyun
2209*4882a593Smuzhiyun nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
2210*4882a593Smuzhiyun if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2211*4882a593Smuzhiyun ocfs2_xa_fill_value_buf(loc, &vb);
2212*4882a593Smuzhiyun rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
2213*4882a593Smuzhiyun ctxt->handle, &vb,
2214*4882a593Smuzhiyun xi->xi_value,
2215*4882a593Smuzhiyun xi->xi_value_len);
2216*4882a593Smuzhiyun } else
2217*4882a593Smuzhiyun memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
2218*4882a593Smuzhiyun
2219*4882a593Smuzhiyun return rc;
2220*4882a593Smuzhiyun }
2221*4882a593Smuzhiyun
ocfs2_xa_set(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_set_ctxt * ctxt)2222*4882a593Smuzhiyun static int ocfs2_xa_set(struct ocfs2_xa_loc *loc,
2223*4882a593Smuzhiyun struct ocfs2_xattr_info *xi,
2224*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
2225*4882a593Smuzhiyun {
2226*4882a593Smuzhiyun int ret;
2227*4882a593Smuzhiyun u32 name_hash = ocfs2_xattr_name_hash(loc->xl_inode, xi->xi_name,
2228*4882a593Smuzhiyun xi->xi_name_len);
2229*4882a593Smuzhiyun
2230*4882a593Smuzhiyun ret = ocfs2_xa_journal_access(ctxt->handle, loc,
2231*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
2232*4882a593Smuzhiyun if (ret) {
2233*4882a593Smuzhiyun mlog_errno(ret);
2234*4882a593Smuzhiyun goto out;
2235*4882a593Smuzhiyun }
2236*4882a593Smuzhiyun
2237*4882a593Smuzhiyun /*
2238*4882a593Smuzhiyun * From here on out, everything is going to modify the buffer a
2239*4882a593Smuzhiyun * little. Errors are going to leave the xattr header in a
2240*4882a593Smuzhiyun * sane state. Thus, even with errors we dirty the sucker.
2241*4882a593Smuzhiyun */
2242*4882a593Smuzhiyun
2243*4882a593Smuzhiyun /* Don't worry, we are never called with !xi_value and !xl_entry */
2244*4882a593Smuzhiyun if (!xi->xi_value) {
2245*4882a593Smuzhiyun ret = ocfs2_xa_remove(loc, ctxt);
2246*4882a593Smuzhiyun goto out_dirty;
2247*4882a593Smuzhiyun }
2248*4882a593Smuzhiyun
2249*4882a593Smuzhiyun ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt);
2250*4882a593Smuzhiyun if (ret) {
2251*4882a593Smuzhiyun if (ret != -ENOSPC)
2252*4882a593Smuzhiyun mlog_errno(ret);
2253*4882a593Smuzhiyun goto out_dirty;
2254*4882a593Smuzhiyun }
2255*4882a593Smuzhiyun
2256*4882a593Smuzhiyun ret = ocfs2_xa_store_value(loc, xi, ctxt);
2257*4882a593Smuzhiyun if (ret)
2258*4882a593Smuzhiyun mlog_errno(ret);
2259*4882a593Smuzhiyun
2260*4882a593Smuzhiyun out_dirty:
2261*4882a593Smuzhiyun ocfs2_xa_journal_dirty(ctxt->handle, loc);
2262*4882a593Smuzhiyun
2263*4882a593Smuzhiyun out:
2264*4882a593Smuzhiyun return ret;
2265*4882a593Smuzhiyun }
2266*4882a593Smuzhiyun
ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc * loc,struct inode * inode,struct buffer_head * bh,struct ocfs2_xattr_entry * entry)2267*4882a593Smuzhiyun static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2268*4882a593Smuzhiyun struct inode *inode,
2269*4882a593Smuzhiyun struct buffer_head *bh,
2270*4882a593Smuzhiyun struct ocfs2_xattr_entry *entry)
2271*4882a593Smuzhiyun {
2272*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2273*4882a593Smuzhiyun
2274*4882a593Smuzhiyun BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL));
2275*4882a593Smuzhiyun
2276*4882a593Smuzhiyun loc->xl_inode = inode;
2277*4882a593Smuzhiyun loc->xl_ops = &ocfs2_xa_block_loc_ops;
2278*4882a593Smuzhiyun loc->xl_storage = bh;
2279*4882a593Smuzhiyun loc->xl_entry = entry;
2280*4882a593Smuzhiyun loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
2281*4882a593Smuzhiyun loc->xl_header =
2282*4882a593Smuzhiyun (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2283*4882a593Smuzhiyun loc->xl_size);
2284*4882a593Smuzhiyun }
2285*4882a593Smuzhiyun
ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc * loc,struct inode * inode,struct buffer_head * bh,struct ocfs2_xattr_entry * entry)2286*4882a593Smuzhiyun static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
2287*4882a593Smuzhiyun struct inode *inode,
2288*4882a593Smuzhiyun struct buffer_head *bh,
2289*4882a593Smuzhiyun struct ocfs2_xattr_entry *entry)
2290*4882a593Smuzhiyun {
2291*4882a593Smuzhiyun struct ocfs2_xattr_block *xb =
2292*4882a593Smuzhiyun (struct ocfs2_xattr_block *)bh->b_data;
2293*4882a593Smuzhiyun
2294*4882a593Smuzhiyun BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2295*4882a593Smuzhiyun
2296*4882a593Smuzhiyun loc->xl_inode = inode;
2297*4882a593Smuzhiyun loc->xl_ops = &ocfs2_xa_block_loc_ops;
2298*4882a593Smuzhiyun loc->xl_storage = bh;
2299*4882a593Smuzhiyun loc->xl_header = &(xb->xb_attrs.xb_header);
2300*4882a593Smuzhiyun loc->xl_entry = entry;
2301*4882a593Smuzhiyun loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2302*4882a593Smuzhiyun xb_attrs.xb_header);
2303*4882a593Smuzhiyun }
2304*4882a593Smuzhiyun
ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc * loc,struct ocfs2_xattr_bucket * bucket,struct ocfs2_xattr_entry * entry)2305*4882a593Smuzhiyun static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2306*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket,
2307*4882a593Smuzhiyun struct ocfs2_xattr_entry *entry)
2308*4882a593Smuzhiyun {
2309*4882a593Smuzhiyun loc->xl_inode = bucket->bu_inode;
2310*4882a593Smuzhiyun loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2311*4882a593Smuzhiyun loc->xl_storage = bucket;
2312*4882a593Smuzhiyun loc->xl_header = bucket_xh(bucket);
2313*4882a593Smuzhiyun loc->xl_entry = entry;
2314*4882a593Smuzhiyun loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2315*4882a593Smuzhiyun }
2316*4882a593Smuzhiyun
2317*4882a593Smuzhiyun /*
2318*4882a593Smuzhiyun * In xattr remove, if it is stored outside and refcounted, we may have
2319*4882a593Smuzhiyun * the chance to split the refcount tree. So need the allocators.
2320*4882a593Smuzhiyun */
ocfs2_lock_xattr_remove_allocators(struct inode * inode,struct ocfs2_xattr_value_root * xv,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,struct ocfs2_alloc_context ** meta_ac,int * ref_credits)2321*4882a593Smuzhiyun static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2322*4882a593Smuzhiyun struct ocfs2_xattr_value_root *xv,
2323*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci,
2324*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
2325*4882a593Smuzhiyun struct ocfs2_alloc_context **meta_ac,
2326*4882a593Smuzhiyun int *ref_credits)
2327*4882a593Smuzhiyun {
2328*4882a593Smuzhiyun int ret, meta_add = 0;
2329*4882a593Smuzhiyun u32 p_cluster, num_clusters;
2330*4882a593Smuzhiyun unsigned int ext_flags;
2331*4882a593Smuzhiyun
2332*4882a593Smuzhiyun *ref_credits = 0;
2333*4882a593Smuzhiyun ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2334*4882a593Smuzhiyun &num_clusters,
2335*4882a593Smuzhiyun &xv->xr_list,
2336*4882a593Smuzhiyun &ext_flags);
2337*4882a593Smuzhiyun if (ret) {
2338*4882a593Smuzhiyun mlog_errno(ret);
2339*4882a593Smuzhiyun goto out;
2340*4882a593Smuzhiyun }
2341*4882a593Smuzhiyun
2342*4882a593Smuzhiyun if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2343*4882a593Smuzhiyun goto out;
2344*4882a593Smuzhiyun
2345*4882a593Smuzhiyun ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2346*4882a593Smuzhiyun ref_root_bh, xv,
2347*4882a593Smuzhiyun &meta_add, ref_credits);
2348*4882a593Smuzhiyun if (ret) {
2349*4882a593Smuzhiyun mlog_errno(ret);
2350*4882a593Smuzhiyun goto out;
2351*4882a593Smuzhiyun }
2352*4882a593Smuzhiyun
2353*4882a593Smuzhiyun ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2354*4882a593Smuzhiyun meta_add, meta_ac);
2355*4882a593Smuzhiyun if (ret)
2356*4882a593Smuzhiyun mlog_errno(ret);
2357*4882a593Smuzhiyun
2358*4882a593Smuzhiyun out:
2359*4882a593Smuzhiyun return ret;
2360*4882a593Smuzhiyun }
2361*4882a593Smuzhiyun
ocfs2_remove_value_outside(struct inode * inode,struct ocfs2_xattr_value_buf * vb,struct ocfs2_xattr_header * header,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh)2362*4882a593Smuzhiyun static int ocfs2_remove_value_outside(struct inode*inode,
2363*4882a593Smuzhiyun struct ocfs2_xattr_value_buf *vb,
2364*4882a593Smuzhiyun struct ocfs2_xattr_header *header,
2365*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci,
2366*4882a593Smuzhiyun struct buffer_head *ref_root_bh)
2367*4882a593Smuzhiyun {
2368*4882a593Smuzhiyun int ret = 0, i, ref_credits;
2369*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2370*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2371*4882a593Smuzhiyun void *val;
2372*4882a593Smuzhiyun
2373*4882a593Smuzhiyun ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2374*4882a593Smuzhiyun
2375*4882a593Smuzhiyun for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2376*4882a593Smuzhiyun struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2377*4882a593Smuzhiyun
2378*4882a593Smuzhiyun if (ocfs2_xattr_is_local(entry))
2379*4882a593Smuzhiyun continue;
2380*4882a593Smuzhiyun
2381*4882a593Smuzhiyun val = (void *)header +
2382*4882a593Smuzhiyun le16_to_cpu(entry->xe_name_offset);
2383*4882a593Smuzhiyun vb->vb_xv = (struct ocfs2_xattr_value_root *)
2384*4882a593Smuzhiyun (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2385*4882a593Smuzhiyun
2386*4882a593Smuzhiyun ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2387*4882a593Smuzhiyun ref_ci, ref_root_bh,
2388*4882a593Smuzhiyun &ctxt.meta_ac,
2389*4882a593Smuzhiyun &ref_credits);
2390*4882a593Smuzhiyun
2391*4882a593Smuzhiyun ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2392*4882a593Smuzhiyun ocfs2_remove_extent_credits(osb->sb));
2393*4882a593Smuzhiyun if (IS_ERR(ctxt.handle)) {
2394*4882a593Smuzhiyun ret = PTR_ERR(ctxt.handle);
2395*4882a593Smuzhiyun mlog_errno(ret);
2396*4882a593Smuzhiyun break;
2397*4882a593Smuzhiyun }
2398*4882a593Smuzhiyun
2399*4882a593Smuzhiyun ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2400*4882a593Smuzhiyun
2401*4882a593Smuzhiyun ocfs2_commit_trans(osb, ctxt.handle);
2402*4882a593Smuzhiyun if (ctxt.meta_ac) {
2403*4882a593Smuzhiyun ocfs2_free_alloc_context(ctxt.meta_ac);
2404*4882a593Smuzhiyun ctxt.meta_ac = NULL;
2405*4882a593Smuzhiyun }
2406*4882a593Smuzhiyun
2407*4882a593Smuzhiyun if (ret < 0) {
2408*4882a593Smuzhiyun mlog_errno(ret);
2409*4882a593Smuzhiyun break;
2410*4882a593Smuzhiyun }
2411*4882a593Smuzhiyun
2412*4882a593Smuzhiyun }
2413*4882a593Smuzhiyun
2414*4882a593Smuzhiyun if (ctxt.meta_ac)
2415*4882a593Smuzhiyun ocfs2_free_alloc_context(ctxt.meta_ac);
2416*4882a593Smuzhiyun ocfs2_schedule_truncate_log_flush(osb, 1);
2417*4882a593Smuzhiyun ocfs2_run_deallocs(osb, &ctxt.dealloc);
2418*4882a593Smuzhiyun return ret;
2419*4882a593Smuzhiyun }
2420*4882a593Smuzhiyun
ocfs2_xattr_ibody_remove(struct inode * inode,struct buffer_head * di_bh,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh)2421*4882a593Smuzhiyun static int ocfs2_xattr_ibody_remove(struct inode *inode,
2422*4882a593Smuzhiyun struct buffer_head *di_bh,
2423*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci,
2424*4882a593Smuzhiyun struct buffer_head *ref_root_bh)
2425*4882a593Smuzhiyun {
2426*4882a593Smuzhiyun
2427*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2428*4882a593Smuzhiyun struct ocfs2_xattr_header *header;
2429*4882a593Smuzhiyun int ret;
2430*4882a593Smuzhiyun struct ocfs2_xattr_value_buf vb = {
2431*4882a593Smuzhiyun .vb_bh = di_bh,
2432*4882a593Smuzhiyun .vb_access = ocfs2_journal_access_di,
2433*4882a593Smuzhiyun };
2434*4882a593Smuzhiyun
2435*4882a593Smuzhiyun header = (struct ocfs2_xattr_header *)
2436*4882a593Smuzhiyun ((void *)di + inode->i_sb->s_blocksize -
2437*4882a593Smuzhiyun le16_to_cpu(di->i_xattr_inline_size));
2438*4882a593Smuzhiyun
2439*4882a593Smuzhiyun ret = ocfs2_remove_value_outside(inode, &vb, header,
2440*4882a593Smuzhiyun ref_ci, ref_root_bh);
2441*4882a593Smuzhiyun
2442*4882a593Smuzhiyun return ret;
2443*4882a593Smuzhiyun }
2444*4882a593Smuzhiyun
2445*4882a593Smuzhiyun struct ocfs2_rm_xattr_bucket_para {
2446*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci;
2447*4882a593Smuzhiyun struct buffer_head *ref_root_bh;
2448*4882a593Smuzhiyun };
2449*4882a593Smuzhiyun
ocfs2_xattr_block_remove(struct inode * inode,struct buffer_head * blk_bh,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh)2450*4882a593Smuzhiyun static int ocfs2_xattr_block_remove(struct inode *inode,
2451*4882a593Smuzhiyun struct buffer_head *blk_bh,
2452*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci,
2453*4882a593Smuzhiyun struct buffer_head *ref_root_bh)
2454*4882a593Smuzhiyun {
2455*4882a593Smuzhiyun struct ocfs2_xattr_block *xb;
2456*4882a593Smuzhiyun int ret = 0;
2457*4882a593Smuzhiyun struct ocfs2_xattr_value_buf vb = {
2458*4882a593Smuzhiyun .vb_bh = blk_bh,
2459*4882a593Smuzhiyun .vb_access = ocfs2_journal_access_xb,
2460*4882a593Smuzhiyun };
2461*4882a593Smuzhiyun struct ocfs2_rm_xattr_bucket_para args = {
2462*4882a593Smuzhiyun .ref_ci = ref_ci,
2463*4882a593Smuzhiyun .ref_root_bh = ref_root_bh,
2464*4882a593Smuzhiyun };
2465*4882a593Smuzhiyun
2466*4882a593Smuzhiyun xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2467*4882a593Smuzhiyun if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2468*4882a593Smuzhiyun struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
2469*4882a593Smuzhiyun ret = ocfs2_remove_value_outside(inode, &vb, header,
2470*4882a593Smuzhiyun ref_ci, ref_root_bh);
2471*4882a593Smuzhiyun } else
2472*4882a593Smuzhiyun ret = ocfs2_iterate_xattr_index_block(inode,
2473*4882a593Smuzhiyun blk_bh,
2474*4882a593Smuzhiyun ocfs2_rm_xattr_cluster,
2475*4882a593Smuzhiyun &args);
2476*4882a593Smuzhiyun
2477*4882a593Smuzhiyun return ret;
2478*4882a593Smuzhiyun }
2479*4882a593Smuzhiyun
ocfs2_xattr_free_block(struct inode * inode,u64 block,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh)2480*4882a593Smuzhiyun static int ocfs2_xattr_free_block(struct inode *inode,
2481*4882a593Smuzhiyun u64 block,
2482*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci,
2483*4882a593Smuzhiyun struct buffer_head *ref_root_bh)
2484*4882a593Smuzhiyun {
2485*4882a593Smuzhiyun struct inode *xb_alloc_inode;
2486*4882a593Smuzhiyun struct buffer_head *xb_alloc_bh = NULL;
2487*4882a593Smuzhiyun struct buffer_head *blk_bh = NULL;
2488*4882a593Smuzhiyun struct ocfs2_xattr_block *xb;
2489*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2490*4882a593Smuzhiyun handle_t *handle;
2491*4882a593Smuzhiyun int ret = 0;
2492*4882a593Smuzhiyun u64 blk, bg_blkno;
2493*4882a593Smuzhiyun u16 bit;
2494*4882a593Smuzhiyun
2495*4882a593Smuzhiyun ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
2496*4882a593Smuzhiyun if (ret < 0) {
2497*4882a593Smuzhiyun mlog_errno(ret);
2498*4882a593Smuzhiyun goto out;
2499*4882a593Smuzhiyun }
2500*4882a593Smuzhiyun
2501*4882a593Smuzhiyun ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
2502*4882a593Smuzhiyun if (ret < 0) {
2503*4882a593Smuzhiyun mlog_errno(ret);
2504*4882a593Smuzhiyun goto out;
2505*4882a593Smuzhiyun }
2506*4882a593Smuzhiyun
2507*4882a593Smuzhiyun xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2508*4882a593Smuzhiyun blk = le64_to_cpu(xb->xb_blkno);
2509*4882a593Smuzhiyun bit = le16_to_cpu(xb->xb_suballoc_bit);
2510*4882a593Smuzhiyun if (xb->xb_suballoc_loc)
2511*4882a593Smuzhiyun bg_blkno = le64_to_cpu(xb->xb_suballoc_loc);
2512*4882a593Smuzhiyun else
2513*4882a593Smuzhiyun bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2514*4882a593Smuzhiyun
2515*4882a593Smuzhiyun xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2516*4882a593Smuzhiyun EXTENT_ALLOC_SYSTEM_INODE,
2517*4882a593Smuzhiyun le16_to_cpu(xb->xb_suballoc_slot));
2518*4882a593Smuzhiyun if (!xb_alloc_inode) {
2519*4882a593Smuzhiyun ret = -ENOMEM;
2520*4882a593Smuzhiyun mlog_errno(ret);
2521*4882a593Smuzhiyun goto out;
2522*4882a593Smuzhiyun }
2523*4882a593Smuzhiyun inode_lock(xb_alloc_inode);
2524*4882a593Smuzhiyun
2525*4882a593Smuzhiyun ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2526*4882a593Smuzhiyun if (ret < 0) {
2527*4882a593Smuzhiyun mlog_errno(ret);
2528*4882a593Smuzhiyun goto out_mutex;
2529*4882a593Smuzhiyun }
2530*4882a593Smuzhiyun
2531*4882a593Smuzhiyun handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2532*4882a593Smuzhiyun if (IS_ERR(handle)) {
2533*4882a593Smuzhiyun ret = PTR_ERR(handle);
2534*4882a593Smuzhiyun mlog_errno(ret);
2535*4882a593Smuzhiyun goto out_unlock;
2536*4882a593Smuzhiyun }
2537*4882a593Smuzhiyun
2538*4882a593Smuzhiyun ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2539*4882a593Smuzhiyun bit, bg_blkno, 1);
2540*4882a593Smuzhiyun if (ret < 0)
2541*4882a593Smuzhiyun mlog_errno(ret);
2542*4882a593Smuzhiyun
2543*4882a593Smuzhiyun ocfs2_commit_trans(osb, handle);
2544*4882a593Smuzhiyun out_unlock:
2545*4882a593Smuzhiyun ocfs2_inode_unlock(xb_alloc_inode, 1);
2546*4882a593Smuzhiyun brelse(xb_alloc_bh);
2547*4882a593Smuzhiyun out_mutex:
2548*4882a593Smuzhiyun inode_unlock(xb_alloc_inode);
2549*4882a593Smuzhiyun iput(xb_alloc_inode);
2550*4882a593Smuzhiyun out:
2551*4882a593Smuzhiyun brelse(blk_bh);
2552*4882a593Smuzhiyun return ret;
2553*4882a593Smuzhiyun }
2554*4882a593Smuzhiyun
2555*4882a593Smuzhiyun /*
2556*4882a593Smuzhiyun * ocfs2_xattr_remove()
2557*4882a593Smuzhiyun *
2558*4882a593Smuzhiyun * Free extended attribute resources associated with this inode.
2559*4882a593Smuzhiyun */
ocfs2_xattr_remove(struct inode * inode,struct buffer_head * di_bh)2560*4882a593Smuzhiyun int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2561*4882a593Smuzhiyun {
2562*4882a593Smuzhiyun struct ocfs2_inode_info *oi = OCFS2_I(inode);
2563*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2564*4882a593Smuzhiyun struct ocfs2_refcount_tree *ref_tree = NULL;
2565*4882a593Smuzhiyun struct buffer_head *ref_root_bh = NULL;
2566*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci = NULL;
2567*4882a593Smuzhiyun handle_t *handle;
2568*4882a593Smuzhiyun int ret;
2569*4882a593Smuzhiyun
2570*4882a593Smuzhiyun if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2571*4882a593Smuzhiyun return 0;
2572*4882a593Smuzhiyun
2573*4882a593Smuzhiyun if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2574*4882a593Smuzhiyun return 0;
2575*4882a593Smuzhiyun
2576*4882a593Smuzhiyun if (ocfs2_is_refcount_inode(inode)) {
2577*4882a593Smuzhiyun ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2578*4882a593Smuzhiyun le64_to_cpu(di->i_refcount_loc),
2579*4882a593Smuzhiyun 1, &ref_tree, &ref_root_bh);
2580*4882a593Smuzhiyun if (ret) {
2581*4882a593Smuzhiyun mlog_errno(ret);
2582*4882a593Smuzhiyun goto out;
2583*4882a593Smuzhiyun }
2584*4882a593Smuzhiyun ref_ci = &ref_tree->rf_ci;
2585*4882a593Smuzhiyun
2586*4882a593Smuzhiyun }
2587*4882a593Smuzhiyun
2588*4882a593Smuzhiyun if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2589*4882a593Smuzhiyun ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2590*4882a593Smuzhiyun ref_ci, ref_root_bh);
2591*4882a593Smuzhiyun if (ret < 0) {
2592*4882a593Smuzhiyun mlog_errno(ret);
2593*4882a593Smuzhiyun goto out;
2594*4882a593Smuzhiyun }
2595*4882a593Smuzhiyun }
2596*4882a593Smuzhiyun
2597*4882a593Smuzhiyun if (di->i_xattr_loc) {
2598*4882a593Smuzhiyun ret = ocfs2_xattr_free_block(inode,
2599*4882a593Smuzhiyun le64_to_cpu(di->i_xattr_loc),
2600*4882a593Smuzhiyun ref_ci, ref_root_bh);
2601*4882a593Smuzhiyun if (ret < 0) {
2602*4882a593Smuzhiyun mlog_errno(ret);
2603*4882a593Smuzhiyun goto out;
2604*4882a593Smuzhiyun }
2605*4882a593Smuzhiyun }
2606*4882a593Smuzhiyun
2607*4882a593Smuzhiyun handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2608*4882a593Smuzhiyun OCFS2_INODE_UPDATE_CREDITS);
2609*4882a593Smuzhiyun if (IS_ERR(handle)) {
2610*4882a593Smuzhiyun ret = PTR_ERR(handle);
2611*4882a593Smuzhiyun mlog_errno(ret);
2612*4882a593Smuzhiyun goto out;
2613*4882a593Smuzhiyun }
2614*4882a593Smuzhiyun ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2615*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
2616*4882a593Smuzhiyun if (ret) {
2617*4882a593Smuzhiyun mlog_errno(ret);
2618*4882a593Smuzhiyun goto out_commit;
2619*4882a593Smuzhiyun }
2620*4882a593Smuzhiyun
2621*4882a593Smuzhiyun di->i_xattr_loc = 0;
2622*4882a593Smuzhiyun
2623*4882a593Smuzhiyun spin_lock(&oi->ip_lock);
2624*4882a593Smuzhiyun oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2625*4882a593Smuzhiyun di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2626*4882a593Smuzhiyun spin_unlock(&oi->ip_lock);
2627*4882a593Smuzhiyun ocfs2_update_inode_fsync_trans(handle, inode, 0);
2628*4882a593Smuzhiyun
2629*4882a593Smuzhiyun ocfs2_journal_dirty(handle, di_bh);
2630*4882a593Smuzhiyun out_commit:
2631*4882a593Smuzhiyun ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2632*4882a593Smuzhiyun out:
2633*4882a593Smuzhiyun if (ref_tree)
2634*4882a593Smuzhiyun ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2635*4882a593Smuzhiyun brelse(ref_root_bh);
2636*4882a593Smuzhiyun return ret;
2637*4882a593Smuzhiyun }
2638*4882a593Smuzhiyun
ocfs2_xattr_has_space_inline(struct inode * inode,struct ocfs2_dinode * di)2639*4882a593Smuzhiyun static int ocfs2_xattr_has_space_inline(struct inode *inode,
2640*4882a593Smuzhiyun struct ocfs2_dinode *di)
2641*4882a593Smuzhiyun {
2642*4882a593Smuzhiyun struct ocfs2_inode_info *oi = OCFS2_I(inode);
2643*4882a593Smuzhiyun unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2644*4882a593Smuzhiyun int free;
2645*4882a593Smuzhiyun
2646*4882a593Smuzhiyun if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2647*4882a593Smuzhiyun return 0;
2648*4882a593Smuzhiyun
2649*4882a593Smuzhiyun if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2650*4882a593Smuzhiyun struct ocfs2_inline_data *idata = &di->id2.i_data;
2651*4882a593Smuzhiyun free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2652*4882a593Smuzhiyun } else if (ocfs2_inode_is_fast_symlink(inode)) {
2653*4882a593Smuzhiyun free = ocfs2_fast_symlink_chars(inode->i_sb) -
2654*4882a593Smuzhiyun le64_to_cpu(di->i_size);
2655*4882a593Smuzhiyun } else {
2656*4882a593Smuzhiyun struct ocfs2_extent_list *el = &di->id2.i_list;
2657*4882a593Smuzhiyun free = (le16_to_cpu(el->l_count) -
2658*4882a593Smuzhiyun le16_to_cpu(el->l_next_free_rec)) *
2659*4882a593Smuzhiyun sizeof(struct ocfs2_extent_rec);
2660*4882a593Smuzhiyun }
2661*4882a593Smuzhiyun if (free >= xattrsize)
2662*4882a593Smuzhiyun return 1;
2663*4882a593Smuzhiyun
2664*4882a593Smuzhiyun return 0;
2665*4882a593Smuzhiyun }
2666*4882a593Smuzhiyun
2667*4882a593Smuzhiyun /*
2668*4882a593Smuzhiyun * ocfs2_xattr_ibody_find()
2669*4882a593Smuzhiyun *
2670*4882a593Smuzhiyun * Find extended attribute in inode block and
2671*4882a593Smuzhiyun * fill search info into struct ocfs2_xattr_search.
2672*4882a593Smuzhiyun */
ocfs2_xattr_ibody_find(struct inode * inode,int name_index,const char * name,struct ocfs2_xattr_search * xs)2673*4882a593Smuzhiyun static int ocfs2_xattr_ibody_find(struct inode *inode,
2674*4882a593Smuzhiyun int name_index,
2675*4882a593Smuzhiyun const char *name,
2676*4882a593Smuzhiyun struct ocfs2_xattr_search *xs)
2677*4882a593Smuzhiyun {
2678*4882a593Smuzhiyun struct ocfs2_inode_info *oi = OCFS2_I(inode);
2679*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2680*4882a593Smuzhiyun int ret;
2681*4882a593Smuzhiyun int has_space = 0;
2682*4882a593Smuzhiyun
2683*4882a593Smuzhiyun if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2684*4882a593Smuzhiyun return 0;
2685*4882a593Smuzhiyun
2686*4882a593Smuzhiyun if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2687*4882a593Smuzhiyun down_read(&oi->ip_alloc_sem);
2688*4882a593Smuzhiyun has_space = ocfs2_xattr_has_space_inline(inode, di);
2689*4882a593Smuzhiyun up_read(&oi->ip_alloc_sem);
2690*4882a593Smuzhiyun if (!has_space)
2691*4882a593Smuzhiyun return 0;
2692*4882a593Smuzhiyun }
2693*4882a593Smuzhiyun
2694*4882a593Smuzhiyun xs->xattr_bh = xs->inode_bh;
2695*4882a593Smuzhiyun xs->end = (void *)di + inode->i_sb->s_blocksize;
2696*4882a593Smuzhiyun if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2697*4882a593Smuzhiyun xs->header = (struct ocfs2_xattr_header *)
2698*4882a593Smuzhiyun (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2699*4882a593Smuzhiyun else
2700*4882a593Smuzhiyun xs->header = (struct ocfs2_xattr_header *)
2701*4882a593Smuzhiyun (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2702*4882a593Smuzhiyun xs->base = (void *)xs->header;
2703*4882a593Smuzhiyun xs->here = xs->header->xh_entries;
2704*4882a593Smuzhiyun
2705*4882a593Smuzhiyun /* Find the named attribute. */
2706*4882a593Smuzhiyun if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2707*4882a593Smuzhiyun ret = ocfs2_xattr_find_entry(name_index, name, xs);
2708*4882a593Smuzhiyun if (ret && ret != -ENODATA)
2709*4882a593Smuzhiyun return ret;
2710*4882a593Smuzhiyun xs->not_found = ret;
2711*4882a593Smuzhiyun }
2712*4882a593Smuzhiyun
2713*4882a593Smuzhiyun return 0;
2714*4882a593Smuzhiyun }
2715*4882a593Smuzhiyun
ocfs2_xattr_ibody_init(struct inode * inode,struct buffer_head * di_bh,struct ocfs2_xattr_set_ctxt * ctxt)2716*4882a593Smuzhiyun static int ocfs2_xattr_ibody_init(struct inode *inode,
2717*4882a593Smuzhiyun struct buffer_head *di_bh,
2718*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
2719*4882a593Smuzhiyun {
2720*4882a593Smuzhiyun int ret;
2721*4882a593Smuzhiyun struct ocfs2_inode_info *oi = OCFS2_I(inode);
2722*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2723*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2724*4882a593Smuzhiyun unsigned int xattrsize = osb->s_xattr_inline_size;
2725*4882a593Smuzhiyun
2726*4882a593Smuzhiyun if (!ocfs2_xattr_has_space_inline(inode, di)) {
2727*4882a593Smuzhiyun ret = -ENOSPC;
2728*4882a593Smuzhiyun goto out;
2729*4882a593Smuzhiyun }
2730*4882a593Smuzhiyun
2731*4882a593Smuzhiyun ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), di_bh,
2732*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
2733*4882a593Smuzhiyun if (ret) {
2734*4882a593Smuzhiyun mlog_errno(ret);
2735*4882a593Smuzhiyun goto out;
2736*4882a593Smuzhiyun }
2737*4882a593Smuzhiyun
2738*4882a593Smuzhiyun /*
2739*4882a593Smuzhiyun * Adjust extent record count or inline data size
2740*4882a593Smuzhiyun * to reserve space for extended attribute.
2741*4882a593Smuzhiyun */
2742*4882a593Smuzhiyun if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2743*4882a593Smuzhiyun struct ocfs2_inline_data *idata = &di->id2.i_data;
2744*4882a593Smuzhiyun le16_add_cpu(&idata->id_count, -xattrsize);
2745*4882a593Smuzhiyun } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2746*4882a593Smuzhiyun struct ocfs2_extent_list *el = &di->id2.i_list;
2747*4882a593Smuzhiyun le16_add_cpu(&el->l_count, -(xattrsize /
2748*4882a593Smuzhiyun sizeof(struct ocfs2_extent_rec)));
2749*4882a593Smuzhiyun }
2750*4882a593Smuzhiyun di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2751*4882a593Smuzhiyun
2752*4882a593Smuzhiyun spin_lock(&oi->ip_lock);
2753*4882a593Smuzhiyun oi->ip_dyn_features |= OCFS2_INLINE_XATTR_FL|OCFS2_HAS_XATTR_FL;
2754*4882a593Smuzhiyun di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2755*4882a593Smuzhiyun spin_unlock(&oi->ip_lock);
2756*4882a593Smuzhiyun
2757*4882a593Smuzhiyun ocfs2_journal_dirty(ctxt->handle, di_bh);
2758*4882a593Smuzhiyun
2759*4882a593Smuzhiyun out:
2760*4882a593Smuzhiyun return ret;
2761*4882a593Smuzhiyun }
2762*4882a593Smuzhiyun
2763*4882a593Smuzhiyun /*
2764*4882a593Smuzhiyun * ocfs2_xattr_ibody_set()
2765*4882a593Smuzhiyun *
2766*4882a593Smuzhiyun * Set, replace or remove an extended attribute into inode block.
2767*4882a593Smuzhiyun *
2768*4882a593Smuzhiyun */
ocfs2_xattr_ibody_set(struct inode * inode,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_search * xs,struct ocfs2_xattr_set_ctxt * ctxt)2769*4882a593Smuzhiyun static int ocfs2_xattr_ibody_set(struct inode *inode,
2770*4882a593Smuzhiyun struct ocfs2_xattr_info *xi,
2771*4882a593Smuzhiyun struct ocfs2_xattr_search *xs,
2772*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
2773*4882a593Smuzhiyun {
2774*4882a593Smuzhiyun int ret;
2775*4882a593Smuzhiyun struct ocfs2_inode_info *oi = OCFS2_I(inode);
2776*4882a593Smuzhiyun struct ocfs2_xa_loc loc;
2777*4882a593Smuzhiyun
2778*4882a593Smuzhiyun if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2779*4882a593Smuzhiyun return -ENOSPC;
2780*4882a593Smuzhiyun
2781*4882a593Smuzhiyun down_write(&oi->ip_alloc_sem);
2782*4882a593Smuzhiyun if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2783*4882a593Smuzhiyun ret = ocfs2_xattr_ibody_init(inode, xs->inode_bh, ctxt);
2784*4882a593Smuzhiyun if (ret) {
2785*4882a593Smuzhiyun if (ret != -ENOSPC)
2786*4882a593Smuzhiyun mlog_errno(ret);
2787*4882a593Smuzhiyun goto out;
2788*4882a593Smuzhiyun }
2789*4882a593Smuzhiyun }
2790*4882a593Smuzhiyun
2791*4882a593Smuzhiyun ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2792*4882a593Smuzhiyun xs->not_found ? NULL : xs->here);
2793*4882a593Smuzhiyun ret = ocfs2_xa_set(&loc, xi, ctxt);
2794*4882a593Smuzhiyun if (ret) {
2795*4882a593Smuzhiyun if (ret != -ENOSPC)
2796*4882a593Smuzhiyun mlog_errno(ret);
2797*4882a593Smuzhiyun goto out;
2798*4882a593Smuzhiyun }
2799*4882a593Smuzhiyun xs->here = loc.xl_entry;
2800*4882a593Smuzhiyun
2801*4882a593Smuzhiyun out:
2802*4882a593Smuzhiyun up_write(&oi->ip_alloc_sem);
2803*4882a593Smuzhiyun
2804*4882a593Smuzhiyun return ret;
2805*4882a593Smuzhiyun }
2806*4882a593Smuzhiyun
2807*4882a593Smuzhiyun /*
2808*4882a593Smuzhiyun * ocfs2_xattr_block_find()
2809*4882a593Smuzhiyun *
2810*4882a593Smuzhiyun * Find extended attribute in external block and
2811*4882a593Smuzhiyun * fill search info into struct ocfs2_xattr_search.
2812*4882a593Smuzhiyun */
ocfs2_xattr_block_find(struct inode * inode,int name_index,const char * name,struct ocfs2_xattr_search * xs)2813*4882a593Smuzhiyun static int ocfs2_xattr_block_find(struct inode *inode,
2814*4882a593Smuzhiyun int name_index,
2815*4882a593Smuzhiyun const char *name,
2816*4882a593Smuzhiyun struct ocfs2_xattr_search *xs)
2817*4882a593Smuzhiyun {
2818*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2819*4882a593Smuzhiyun struct buffer_head *blk_bh = NULL;
2820*4882a593Smuzhiyun struct ocfs2_xattr_block *xb;
2821*4882a593Smuzhiyun int ret = 0;
2822*4882a593Smuzhiyun
2823*4882a593Smuzhiyun if (!di->i_xattr_loc)
2824*4882a593Smuzhiyun return ret;
2825*4882a593Smuzhiyun
2826*4882a593Smuzhiyun ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2827*4882a593Smuzhiyun &blk_bh);
2828*4882a593Smuzhiyun if (ret < 0) {
2829*4882a593Smuzhiyun mlog_errno(ret);
2830*4882a593Smuzhiyun return ret;
2831*4882a593Smuzhiyun }
2832*4882a593Smuzhiyun
2833*4882a593Smuzhiyun xs->xattr_bh = blk_bh;
2834*4882a593Smuzhiyun xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2835*4882a593Smuzhiyun
2836*4882a593Smuzhiyun if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2837*4882a593Smuzhiyun xs->header = &xb->xb_attrs.xb_header;
2838*4882a593Smuzhiyun xs->base = (void *)xs->header;
2839*4882a593Smuzhiyun xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2840*4882a593Smuzhiyun xs->here = xs->header->xh_entries;
2841*4882a593Smuzhiyun
2842*4882a593Smuzhiyun ret = ocfs2_xattr_find_entry(name_index, name, xs);
2843*4882a593Smuzhiyun } else
2844*4882a593Smuzhiyun ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2845*4882a593Smuzhiyun name_index,
2846*4882a593Smuzhiyun name, xs);
2847*4882a593Smuzhiyun
2848*4882a593Smuzhiyun if (ret && ret != -ENODATA) {
2849*4882a593Smuzhiyun xs->xattr_bh = NULL;
2850*4882a593Smuzhiyun goto cleanup;
2851*4882a593Smuzhiyun }
2852*4882a593Smuzhiyun xs->not_found = ret;
2853*4882a593Smuzhiyun return 0;
2854*4882a593Smuzhiyun cleanup:
2855*4882a593Smuzhiyun brelse(blk_bh);
2856*4882a593Smuzhiyun
2857*4882a593Smuzhiyun return ret;
2858*4882a593Smuzhiyun }
2859*4882a593Smuzhiyun
ocfs2_create_xattr_block(struct inode * inode,struct buffer_head * inode_bh,struct ocfs2_xattr_set_ctxt * ctxt,int indexed,struct buffer_head ** ret_bh)2860*4882a593Smuzhiyun static int ocfs2_create_xattr_block(struct inode *inode,
2861*4882a593Smuzhiyun struct buffer_head *inode_bh,
2862*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt,
2863*4882a593Smuzhiyun int indexed,
2864*4882a593Smuzhiyun struct buffer_head **ret_bh)
2865*4882a593Smuzhiyun {
2866*4882a593Smuzhiyun int ret;
2867*4882a593Smuzhiyun u16 suballoc_bit_start;
2868*4882a593Smuzhiyun u32 num_got;
2869*4882a593Smuzhiyun u64 suballoc_loc, first_blkno;
2870*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2871*4882a593Smuzhiyun struct buffer_head *new_bh = NULL;
2872*4882a593Smuzhiyun struct ocfs2_xattr_block *xblk;
2873*4882a593Smuzhiyun
2874*4882a593Smuzhiyun ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2875*4882a593Smuzhiyun inode_bh, OCFS2_JOURNAL_ACCESS_CREATE);
2876*4882a593Smuzhiyun if (ret < 0) {
2877*4882a593Smuzhiyun mlog_errno(ret);
2878*4882a593Smuzhiyun goto end;
2879*4882a593Smuzhiyun }
2880*4882a593Smuzhiyun
2881*4882a593Smuzhiyun ret = ocfs2_claim_metadata(ctxt->handle, ctxt->meta_ac, 1,
2882*4882a593Smuzhiyun &suballoc_loc, &suballoc_bit_start,
2883*4882a593Smuzhiyun &num_got, &first_blkno);
2884*4882a593Smuzhiyun if (ret < 0) {
2885*4882a593Smuzhiyun mlog_errno(ret);
2886*4882a593Smuzhiyun goto end;
2887*4882a593Smuzhiyun }
2888*4882a593Smuzhiyun
2889*4882a593Smuzhiyun new_bh = sb_getblk(inode->i_sb, first_blkno);
2890*4882a593Smuzhiyun if (!new_bh) {
2891*4882a593Smuzhiyun ret = -ENOMEM;
2892*4882a593Smuzhiyun mlog_errno(ret);
2893*4882a593Smuzhiyun goto end;
2894*4882a593Smuzhiyun }
2895*4882a593Smuzhiyun
2896*4882a593Smuzhiyun ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2897*4882a593Smuzhiyun
2898*4882a593Smuzhiyun ret = ocfs2_journal_access_xb(ctxt->handle, INODE_CACHE(inode),
2899*4882a593Smuzhiyun new_bh,
2900*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_CREATE);
2901*4882a593Smuzhiyun if (ret < 0) {
2902*4882a593Smuzhiyun mlog_errno(ret);
2903*4882a593Smuzhiyun goto end;
2904*4882a593Smuzhiyun }
2905*4882a593Smuzhiyun
2906*4882a593Smuzhiyun /* Initialize ocfs2_xattr_block */
2907*4882a593Smuzhiyun xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2908*4882a593Smuzhiyun memset(xblk, 0, inode->i_sb->s_blocksize);
2909*4882a593Smuzhiyun strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2910*4882a593Smuzhiyun xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot);
2911*4882a593Smuzhiyun xblk->xb_suballoc_loc = cpu_to_le64(suballoc_loc);
2912*4882a593Smuzhiyun xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2913*4882a593Smuzhiyun xblk->xb_fs_generation =
2914*4882a593Smuzhiyun cpu_to_le32(OCFS2_SB(inode->i_sb)->fs_generation);
2915*4882a593Smuzhiyun xblk->xb_blkno = cpu_to_le64(first_blkno);
2916*4882a593Smuzhiyun if (indexed) {
2917*4882a593Smuzhiyun struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2918*4882a593Smuzhiyun xr->xt_clusters = cpu_to_le32(1);
2919*4882a593Smuzhiyun xr->xt_last_eb_blk = 0;
2920*4882a593Smuzhiyun xr->xt_list.l_tree_depth = 0;
2921*4882a593Smuzhiyun xr->xt_list.l_count = cpu_to_le16(
2922*4882a593Smuzhiyun ocfs2_xattr_recs_per_xb(inode->i_sb));
2923*4882a593Smuzhiyun xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2924*4882a593Smuzhiyun xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2925*4882a593Smuzhiyun }
2926*4882a593Smuzhiyun ocfs2_journal_dirty(ctxt->handle, new_bh);
2927*4882a593Smuzhiyun
2928*4882a593Smuzhiyun /* Add it to the inode */
2929*4882a593Smuzhiyun di->i_xattr_loc = cpu_to_le64(first_blkno);
2930*4882a593Smuzhiyun
2931*4882a593Smuzhiyun spin_lock(&OCFS2_I(inode)->ip_lock);
2932*4882a593Smuzhiyun OCFS2_I(inode)->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
2933*4882a593Smuzhiyun di->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
2934*4882a593Smuzhiyun spin_unlock(&OCFS2_I(inode)->ip_lock);
2935*4882a593Smuzhiyun
2936*4882a593Smuzhiyun ocfs2_journal_dirty(ctxt->handle, inode_bh);
2937*4882a593Smuzhiyun
2938*4882a593Smuzhiyun *ret_bh = new_bh;
2939*4882a593Smuzhiyun new_bh = NULL;
2940*4882a593Smuzhiyun
2941*4882a593Smuzhiyun end:
2942*4882a593Smuzhiyun brelse(new_bh);
2943*4882a593Smuzhiyun return ret;
2944*4882a593Smuzhiyun }
2945*4882a593Smuzhiyun
2946*4882a593Smuzhiyun /*
2947*4882a593Smuzhiyun * ocfs2_xattr_block_set()
2948*4882a593Smuzhiyun *
2949*4882a593Smuzhiyun * Set, replace or remove an extended attribute into external block.
2950*4882a593Smuzhiyun *
2951*4882a593Smuzhiyun */
ocfs2_xattr_block_set(struct inode * inode,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_search * xs,struct ocfs2_xattr_set_ctxt * ctxt)2952*4882a593Smuzhiyun static int ocfs2_xattr_block_set(struct inode *inode,
2953*4882a593Smuzhiyun struct ocfs2_xattr_info *xi,
2954*4882a593Smuzhiyun struct ocfs2_xattr_search *xs,
2955*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
2956*4882a593Smuzhiyun {
2957*4882a593Smuzhiyun struct buffer_head *new_bh = NULL;
2958*4882a593Smuzhiyun struct ocfs2_xattr_block *xblk = NULL;
2959*4882a593Smuzhiyun int ret;
2960*4882a593Smuzhiyun struct ocfs2_xa_loc loc;
2961*4882a593Smuzhiyun
2962*4882a593Smuzhiyun if (!xs->xattr_bh) {
2963*4882a593Smuzhiyun ret = ocfs2_create_xattr_block(inode, xs->inode_bh, ctxt,
2964*4882a593Smuzhiyun 0, &new_bh);
2965*4882a593Smuzhiyun if (ret) {
2966*4882a593Smuzhiyun mlog_errno(ret);
2967*4882a593Smuzhiyun goto end;
2968*4882a593Smuzhiyun }
2969*4882a593Smuzhiyun
2970*4882a593Smuzhiyun xs->xattr_bh = new_bh;
2971*4882a593Smuzhiyun xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2972*4882a593Smuzhiyun xs->header = &xblk->xb_attrs.xb_header;
2973*4882a593Smuzhiyun xs->base = (void *)xs->header;
2974*4882a593Smuzhiyun xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2975*4882a593Smuzhiyun xs->here = xs->header->xh_entries;
2976*4882a593Smuzhiyun } else
2977*4882a593Smuzhiyun xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2978*4882a593Smuzhiyun
2979*4882a593Smuzhiyun if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2980*4882a593Smuzhiyun ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
2981*4882a593Smuzhiyun xs->not_found ? NULL : xs->here);
2982*4882a593Smuzhiyun
2983*4882a593Smuzhiyun ret = ocfs2_xa_set(&loc, xi, ctxt);
2984*4882a593Smuzhiyun if (!ret)
2985*4882a593Smuzhiyun xs->here = loc.xl_entry;
2986*4882a593Smuzhiyun else if ((ret != -ENOSPC) || ctxt->set_abort)
2987*4882a593Smuzhiyun goto end;
2988*4882a593Smuzhiyun else {
2989*4882a593Smuzhiyun ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2990*4882a593Smuzhiyun if (ret)
2991*4882a593Smuzhiyun goto end;
2992*4882a593Smuzhiyun }
2993*4882a593Smuzhiyun }
2994*4882a593Smuzhiyun
2995*4882a593Smuzhiyun if (le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)
2996*4882a593Smuzhiyun ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2997*4882a593Smuzhiyun
2998*4882a593Smuzhiyun end:
2999*4882a593Smuzhiyun return ret;
3000*4882a593Smuzhiyun }
3001*4882a593Smuzhiyun
3002*4882a593Smuzhiyun /* Check whether the new xattr can be inserted into the inode. */
ocfs2_xattr_can_be_in_inode(struct inode * inode,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_search * xs)3003*4882a593Smuzhiyun static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
3004*4882a593Smuzhiyun struct ocfs2_xattr_info *xi,
3005*4882a593Smuzhiyun struct ocfs2_xattr_search *xs)
3006*4882a593Smuzhiyun {
3007*4882a593Smuzhiyun struct ocfs2_xattr_entry *last;
3008*4882a593Smuzhiyun int free, i;
3009*4882a593Smuzhiyun size_t min_offs = xs->end - xs->base;
3010*4882a593Smuzhiyun
3011*4882a593Smuzhiyun if (!xs->header)
3012*4882a593Smuzhiyun return 0;
3013*4882a593Smuzhiyun
3014*4882a593Smuzhiyun last = xs->header->xh_entries;
3015*4882a593Smuzhiyun
3016*4882a593Smuzhiyun for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
3017*4882a593Smuzhiyun size_t offs = le16_to_cpu(last->xe_name_offset);
3018*4882a593Smuzhiyun if (offs < min_offs)
3019*4882a593Smuzhiyun min_offs = offs;
3020*4882a593Smuzhiyun last += 1;
3021*4882a593Smuzhiyun }
3022*4882a593Smuzhiyun
3023*4882a593Smuzhiyun free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
3024*4882a593Smuzhiyun if (free < 0)
3025*4882a593Smuzhiyun return 0;
3026*4882a593Smuzhiyun
3027*4882a593Smuzhiyun BUG_ON(!xs->not_found);
3028*4882a593Smuzhiyun
3029*4882a593Smuzhiyun if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
3030*4882a593Smuzhiyun return 1;
3031*4882a593Smuzhiyun
3032*4882a593Smuzhiyun return 0;
3033*4882a593Smuzhiyun }
3034*4882a593Smuzhiyun
ocfs2_calc_xattr_set_need(struct inode * inode,struct ocfs2_dinode * di,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_search * xis,struct ocfs2_xattr_search * xbs,int * clusters_need,int * meta_need,int * credits_need)3035*4882a593Smuzhiyun static int ocfs2_calc_xattr_set_need(struct inode *inode,
3036*4882a593Smuzhiyun struct ocfs2_dinode *di,
3037*4882a593Smuzhiyun struct ocfs2_xattr_info *xi,
3038*4882a593Smuzhiyun struct ocfs2_xattr_search *xis,
3039*4882a593Smuzhiyun struct ocfs2_xattr_search *xbs,
3040*4882a593Smuzhiyun int *clusters_need,
3041*4882a593Smuzhiyun int *meta_need,
3042*4882a593Smuzhiyun int *credits_need)
3043*4882a593Smuzhiyun {
3044*4882a593Smuzhiyun int ret = 0, old_in_xb = 0;
3045*4882a593Smuzhiyun int clusters_add = 0, meta_add = 0, credits = 0;
3046*4882a593Smuzhiyun struct buffer_head *bh = NULL;
3047*4882a593Smuzhiyun struct ocfs2_xattr_block *xb = NULL;
3048*4882a593Smuzhiyun struct ocfs2_xattr_entry *xe = NULL;
3049*4882a593Smuzhiyun struct ocfs2_xattr_value_root *xv = NULL;
3050*4882a593Smuzhiyun char *base = NULL;
3051*4882a593Smuzhiyun int name_offset, name_len = 0;
3052*4882a593Smuzhiyun u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3053*4882a593Smuzhiyun xi->xi_value_len);
3054*4882a593Smuzhiyun u64 value_size;
3055*4882a593Smuzhiyun
3056*4882a593Smuzhiyun /*
3057*4882a593Smuzhiyun * Calculate the clusters we need to write.
3058*4882a593Smuzhiyun * No matter whether we replace an old one or add a new one,
3059*4882a593Smuzhiyun * we need this for writing.
3060*4882a593Smuzhiyun */
3061*4882a593Smuzhiyun if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
3062*4882a593Smuzhiyun credits += new_clusters *
3063*4882a593Smuzhiyun ocfs2_clusters_to_blocks(inode->i_sb, 1);
3064*4882a593Smuzhiyun
3065*4882a593Smuzhiyun if (xis->not_found && xbs->not_found) {
3066*4882a593Smuzhiyun credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3067*4882a593Smuzhiyun
3068*4882a593Smuzhiyun if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3069*4882a593Smuzhiyun clusters_add += new_clusters;
3070*4882a593Smuzhiyun credits += ocfs2_calc_extend_credits(inode->i_sb,
3071*4882a593Smuzhiyun &def_xv.xv.xr_list);
3072*4882a593Smuzhiyun }
3073*4882a593Smuzhiyun
3074*4882a593Smuzhiyun goto meta_guess;
3075*4882a593Smuzhiyun }
3076*4882a593Smuzhiyun
3077*4882a593Smuzhiyun if (!xis->not_found) {
3078*4882a593Smuzhiyun xe = xis->here;
3079*4882a593Smuzhiyun name_offset = le16_to_cpu(xe->xe_name_offset);
3080*4882a593Smuzhiyun name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3081*4882a593Smuzhiyun base = xis->base;
3082*4882a593Smuzhiyun credits += OCFS2_INODE_UPDATE_CREDITS;
3083*4882a593Smuzhiyun } else {
3084*4882a593Smuzhiyun int i, block_off = 0;
3085*4882a593Smuzhiyun xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3086*4882a593Smuzhiyun xe = xbs->here;
3087*4882a593Smuzhiyun name_offset = le16_to_cpu(xe->xe_name_offset);
3088*4882a593Smuzhiyun name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3089*4882a593Smuzhiyun i = xbs->here - xbs->header->xh_entries;
3090*4882a593Smuzhiyun old_in_xb = 1;
3091*4882a593Smuzhiyun
3092*4882a593Smuzhiyun if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3093*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3094*4882a593Smuzhiyun bucket_xh(xbs->bucket),
3095*4882a593Smuzhiyun i, &block_off,
3096*4882a593Smuzhiyun &name_offset);
3097*4882a593Smuzhiyun base = bucket_block(xbs->bucket, block_off);
3098*4882a593Smuzhiyun credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3099*4882a593Smuzhiyun } else {
3100*4882a593Smuzhiyun base = xbs->base;
3101*4882a593Smuzhiyun credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
3102*4882a593Smuzhiyun }
3103*4882a593Smuzhiyun }
3104*4882a593Smuzhiyun
3105*4882a593Smuzhiyun /*
3106*4882a593Smuzhiyun * delete a xattr doesn't need metadata and cluster allocation.
3107*4882a593Smuzhiyun * so just calculate the credits and return.
3108*4882a593Smuzhiyun *
3109*4882a593Smuzhiyun * The credits for removing the value tree will be extended
3110*4882a593Smuzhiyun * by ocfs2_remove_extent itself.
3111*4882a593Smuzhiyun */
3112*4882a593Smuzhiyun if (!xi->xi_value) {
3113*4882a593Smuzhiyun if (!ocfs2_xattr_is_local(xe))
3114*4882a593Smuzhiyun credits += ocfs2_remove_extent_credits(inode->i_sb);
3115*4882a593Smuzhiyun
3116*4882a593Smuzhiyun goto out;
3117*4882a593Smuzhiyun }
3118*4882a593Smuzhiyun
3119*4882a593Smuzhiyun /* do cluster allocation guess first. */
3120*4882a593Smuzhiyun value_size = le64_to_cpu(xe->xe_value_size);
3121*4882a593Smuzhiyun
3122*4882a593Smuzhiyun if (old_in_xb) {
3123*4882a593Smuzhiyun /*
3124*4882a593Smuzhiyun * In xattr set, we always try to set the xe in inode first,
3125*4882a593Smuzhiyun * so if it can be inserted into inode successfully, the old
3126*4882a593Smuzhiyun * one will be removed from the xattr block, and this xattr
3127*4882a593Smuzhiyun * will be inserted into inode as a new xattr in inode.
3128*4882a593Smuzhiyun */
3129*4882a593Smuzhiyun if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3130*4882a593Smuzhiyun clusters_add += new_clusters;
3131*4882a593Smuzhiyun credits += ocfs2_remove_extent_credits(inode->i_sb) +
3132*4882a593Smuzhiyun OCFS2_INODE_UPDATE_CREDITS;
3133*4882a593Smuzhiyun if (!ocfs2_xattr_is_local(xe))
3134*4882a593Smuzhiyun credits += ocfs2_calc_extend_credits(
3135*4882a593Smuzhiyun inode->i_sb,
3136*4882a593Smuzhiyun &def_xv.xv.xr_list);
3137*4882a593Smuzhiyun goto out;
3138*4882a593Smuzhiyun }
3139*4882a593Smuzhiyun }
3140*4882a593Smuzhiyun
3141*4882a593Smuzhiyun if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3142*4882a593Smuzhiyun /* the new values will be stored outside. */
3143*4882a593Smuzhiyun u32 old_clusters = 0;
3144*4882a593Smuzhiyun
3145*4882a593Smuzhiyun if (!ocfs2_xattr_is_local(xe)) {
3146*4882a593Smuzhiyun old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3147*4882a593Smuzhiyun value_size);
3148*4882a593Smuzhiyun xv = (struct ocfs2_xattr_value_root *)
3149*4882a593Smuzhiyun (base + name_offset + name_len);
3150*4882a593Smuzhiyun value_size = OCFS2_XATTR_ROOT_SIZE;
3151*4882a593Smuzhiyun } else
3152*4882a593Smuzhiyun xv = &def_xv.xv;
3153*4882a593Smuzhiyun
3154*4882a593Smuzhiyun if (old_clusters >= new_clusters) {
3155*4882a593Smuzhiyun credits += ocfs2_remove_extent_credits(inode->i_sb);
3156*4882a593Smuzhiyun goto out;
3157*4882a593Smuzhiyun } else {
3158*4882a593Smuzhiyun meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3159*4882a593Smuzhiyun clusters_add += new_clusters - old_clusters;
3160*4882a593Smuzhiyun credits += ocfs2_calc_extend_credits(inode->i_sb,
3161*4882a593Smuzhiyun &xv->xr_list);
3162*4882a593Smuzhiyun if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3163*4882a593Smuzhiyun goto out;
3164*4882a593Smuzhiyun }
3165*4882a593Smuzhiyun } else {
3166*4882a593Smuzhiyun /*
3167*4882a593Smuzhiyun * Now the new value will be stored inside. So if the new
3168*4882a593Smuzhiyun * value is smaller than the size of value root or the old
3169*4882a593Smuzhiyun * value, we don't need any allocation, otherwise we have
3170*4882a593Smuzhiyun * to guess metadata allocation.
3171*4882a593Smuzhiyun */
3172*4882a593Smuzhiyun if ((ocfs2_xattr_is_local(xe) &&
3173*4882a593Smuzhiyun (value_size >= xi->xi_value_len)) ||
3174*4882a593Smuzhiyun (!ocfs2_xattr_is_local(xe) &&
3175*4882a593Smuzhiyun OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
3176*4882a593Smuzhiyun goto out;
3177*4882a593Smuzhiyun }
3178*4882a593Smuzhiyun
3179*4882a593Smuzhiyun meta_guess:
3180*4882a593Smuzhiyun /* calculate metadata allocation. */
3181*4882a593Smuzhiyun if (di->i_xattr_loc) {
3182*4882a593Smuzhiyun if (!xbs->xattr_bh) {
3183*4882a593Smuzhiyun ret = ocfs2_read_xattr_block(inode,
3184*4882a593Smuzhiyun le64_to_cpu(di->i_xattr_loc),
3185*4882a593Smuzhiyun &bh);
3186*4882a593Smuzhiyun if (ret) {
3187*4882a593Smuzhiyun mlog_errno(ret);
3188*4882a593Smuzhiyun goto out;
3189*4882a593Smuzhiyun }
3190*4882a593Smuzhiyun
3191*4882a593Smuzhiyun xb = (struct ocfs2_xattr_block *)bh->b_data;
3192*4882a593Smuzhiyun } else
3193*4882a593Smuzhiyun xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3194*4882a593Smuzhiyun
3195*4882a593Smuzhiyun /*
3196*4882a593Smuzhiyun * If there is already an xattr tree, good, we can calculate
3197*4882a593Smuzhiyun * like other b-trees. Otherwise we may have the chance of
3198*4882a593Smuzhiyun * create a tree, the credit calculation is borrowed from
3199*4882a593Smuzhiyun * ocfs2_calc_extend_credits with root_el = NULL. And the
3200*4882a593Smuzhiyun * new tree will be cluster based, so no meta is needed.
3201*4882a593Smuzhiyun */
3202*4882a593Smuzhiyun if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3203*4882a593Smuzhiyun struct ocfs2_extent_list *el =
3204*4882a593Smuzhiyun &xb->xb_attrs.xb_root.xt_list;
3205*4882a593Smuzhiyun meta_add += ocfs2_extend_meta_needed(el);
3206*4882a593Smuzhiyun credits += ocfs2_calc_extend_credits(inode->i_sb,
3207*4882a593Smuzhiyun el);
3208*4882a593Smuzhiyun } else
3209*4882a593Smuzhiyun credits += OCFS2_SUBALLOC_ALLOC + 1;
3210*4882a593Smuzhiyun
3211*4882a593Smuzhiyun /*
3212*4882a593Smuzhiyun * This cluster will be used either for new bucket or for
3213*4882a593Smuzhiyun * new xattr block.
3214*4882a593Smuzhiyun * If the cluster size is the same as the bucket size, one
3215*4882a593Smuzhiyun * more is needed since we may need to extend the bucket
3216*4882a593Smuzhiyun * also.
3217*4882a593Smuzhiyun */
3218*4882a593Smuzhiyun clusters_add += 1;
3219*4882a593Smuzhiyun credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3220*4882a593Smuzhiyun if (OCFS2_XATTR_BUCKET_SIZE ==
3221*4882a593Smuzhiyun OCFS2_SB(inode->i_sb)->s_clustersize) {
3222*4882a593Smuzhiyun credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3223*4882a593Smuzhiyun clusters_add += 1;
3224*4882a593Smuzhiyun }
3225*4882a593Smuzhiyun } else {
3226*4882a593Smuzhiyun credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3227*4882a593Smuzhiyun if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3228*4882a593Smuzhiyun struct ocfs2_extent_list *el = &def_xv.xv.xr_list;
3229*4882a593Smuzhiyun meta_add += ocfs2_extend_meta_needed(el);
3230*4882a593Smuzhiyun credits += ocfs2_calc_extend_credits(inode->i_sb,
3231*4882a593Smuzhiyun el);
3232*4882a593Smuzhiyun } else {
3233*4882a593Smuzhiyun meta_add += 1;
3234*4882a593Smuzhiyun }
3235*4882a593Smuzhiyun }
3236*4882a593Smuzhiyun out:
3237*4882a593Smuzhiyun if (clusters_need)
3238*4882a593Smuzhiyun *clusters_need = clusters_add;
3239*4882a593Smuzhiyun if (meta_need)
3240*4882a593Smuzhiyun *meta_need = meta_add;
3241*4882a593Smuzhiyun if (credits_need)
3242*4882a593Smuzhiyun *credits_need = credits;
3243*4882a593Smuzhiyun brelse(bh);
3244*4882a593Smuzhiyun return ret;
3245*4882a593Smuzhiyun }
3246*4882a593Smuzhiyun
ocfs2_init_xattr_set_ctxt(struct inode * inode,struct ocfs2_dinode * di,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_search * xis,struct ocfs2_xattr_search * xbs,struct ocfs2_xattr_set_ctxt * ctxt,int extra_meta,int * credits)3247*4882a593Smuzhiyun static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3248*4882a593Smuzhiyun struct ocfs2_dinode *di,
3249*4882a593Smuzhiyun struct ocfs2_xattr_info *xi,
3250*4882a593Smuzhiyun struct ocfs2_xattr_search *xis,
3251*4882a593Smuzhiyun struct ocfs2_xattr_search *xbs,
3252*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt,
3253*4882a593Smuzhiyun int extra_meta,
3254*4882a593Smuzhiyun int *credits)
3255*4882a593Smuzhiyun {
3256*4882a593Smuzhiyun int clusters_add, meta_add, ret;
3257*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3258*4882a593Smuzhiyun
3259*4882a593Smuzhiyun memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3260*4882a593Smuzhiyun
3261*4882a593Smuzhiyun ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3262*4882a593Smuzhiyun
3263*4882a593Smuzhiyun ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
3264*4882a593Smuzhiyun &clusters_add, &meta_add, credits);
3265*4882a593Smuzhiyun if (ret) {
3266*4882a593Smuzhiyun mlog_errno(ret);
3267*4882a593Smuzhiyun return ret;
3268*4882a593Smuzhiyun }
3269*4882a593Smuzhiyun
3270*4882a593Smuzhiyun meta_add += extra_meta;
3271*4882a593Smuzhiyun trace_ocfs2_init_xattr_set_ctxt(xi->xi_name, meta_add,
3272*4882a593Smuzhiyun clusters_add, *credits);
3273*4882a593Smuzhiyun
3274*4882a593Smuzhiyun if (meta_add) {
3275*4882a593Smuzhiyun ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3276*4882a593Smuzhiyun &ctxt->meta_ac);
3277*4882a593Smuzhiyun if (ret) {
3278*4882a593Smuzhiyun mlog_errno(ret);
3279*4882a593Smuzhiyun goto out;
3280*4882a593Smuzhiyun }
3281*4882a593Smuzhiyun }
3282*4882a593Smuzhiyun
3283*4882a593Smuzhiyun if (clusters_add) {
3284*4882a593Smuzhiyun ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3285*4882a593Smuzhiyun if (ret)
3286*4882a593Smuzhiyun mlog_errno(ret);
3287*4882a593Smuzhiyun }
3288*4882a593Smuzhiyun out:
3289*4882a593Smuzhiyun if (ret) {
3290*4882a593Smuzhiyun if (ctxt->meta_ac) {
3291*4882a593Smuzhiyun ocfs2_free_alloc_context(ctxt->meta_ac);
3292*4882a593Smuzhiyun ctxt->meta_ac = NULL;
3293*4882a593Smuzhiyun }
3294*4882a593Smuzhiyun
3295*4882a593Smuzhiyun /*
3296*4882a593Smuzhiyun * We cannot have an error and a non null ctxt->data_ac.
3297*4882a593Smuzhiyun */
3298*4882a593Smuzhiyun }
3299*4882a593Smuzhiyun
3300*4882a593Smuzhiyun return ret;
3301*4882a593Smuzhiyun }
3302*4882a593Smuzhiyun
__ocfs2_xattr_set_handle(struct inode * inode,struct ocfs2_dinode * di,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_search * xis,struct ocfs2_xattr_search * xbs,struct ocfs2_xattr_set_ctxt * ctxt)3303*4882a593Smuzhiyun static int __ocfs2_xattr_set_handle(struct inode *inode,
3304*4882a593Smuzhiyun struct ocfs2_dinode *di,
3305*4882a593Smuzhiyun struct ocfs2_xattr_info *xi,
3306*4882a593Smuzhiyun struct ocfs2_xattr_search *xis,
3307*4882a593Smuzhiyun struct ocfs2_xattr_search *xbs,
3308*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
3309*4882a593Smuzhiyun {
3310*4882a593Smuzhiyun int ret = 0, credits, old_found;
3311*4882a593Smuzhiyun
3312*4882a593Smuzhiyun if (!xi->xi_value) {
3313*4882a593Smuzhiyun /* Remove existing extended attribute */
3314*4882a593Smuzhiyun if (!xis->not_found)
3315*4882a593Smuzhiyun ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3316*4882a593Smuzhiyun else if (!xbs->not_found)
3317*4882a593Smuzhiyun ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3318*4882a593Smuzhiyun } else {
3319*4882a593Smuzhiyun /* We always try to set extended attribute into inode first*/
3320*4882a593Smuzhiyun ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3321*4882a593Smuzhiyun if (!ret && !xbs->not_found) {
3322*4882a593Smuzhiyun /*
3323*4882a593Smuzhiyun * If succeed and that extended attribute existing in
3324*4882a593Smuzhiyun * external block, then we will remove it.
3325*4882a593Smuzhiyun */
3326*4882a593Smuzhiyun xi->xi_value = NULL;
3327*4882a593Smuzhiyun xi->xi_value_len = 0;
3328*4882a593Smuzhiyun
3329*4882a593Smuzhiyun old_found = xis->not_found;
3330*4882a593Smuzhiyun xis->not_found = -ENODATA;
3331*4882a593Smuzhiyun ret = ocfs2_calc_xattr_set_need(inode,
3332*4882a593Smuzhiyun di,
3333*4882a593Smuzhiyun xi,
3334*4882a593Smuzhiyun xis,
3335*4882a593Smuzhiyun xbs,
3336*4882a593Smuzhiyun NULL,
3337*4882a593Smuzhiyun NULL,
3338*4882a593Smuzhiyun &credits);
3339*4882a593Smuzhiyun xis->not_found = old_found;
3340*4882a593Smuzhiyun if (ret) {
3341*4882a593Smuzhiyun mlog_errno(ret);
3342*4882a593Smuzhiyun goto out;
3343*4882a593Smuzhiyun }
3344*4882a593Smuzhiyun
3345*4882a593Smuzhiyun ret = ocfs2_extend_trans(ctxt->handle, credits);
3346*4882a593Smuzhiyun if (ret) {
3347*4882a593Smuzhiyun mlog_errno(ret);
3348*4882a593Smuzhiyun goto out;
3349*4882a593Smuzhiyun }
3350*4882a593Smuzhiyun ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3351*4882a593Smuzhiyun } else if ((ret == -ENOSPC) && !ctxt->set_abort) {
3352*4882a593Smuzhiyun if (di->i_xattr_loc && !xbs->xattr_bh) {
3353*4882a593Smuzhiyun ret = ocfs2_xattr_block_find(inode,
3354*4882a593Smuzhiyun xi->xi_name_index,
3355*4882a593Smuzhiyun xi->xi_name, xbs);
3356*4882a593Smuzhiyun if (ret)
3357*4882a593Smuzhiyun goto out;
3358*4882a593Smuzhiyun
3359*4882a593Smuzhiyun old_found = xis->not_found;
3360*4882a593Smuzhiyun xis->not_found = -ENODATA;
3361*4882a593Smuzhiyun ret = ocfs2_calc_xattr_set_need(inode,
3362*4882a593Smuzhiyun di,
3363*4882a593Smuzhiyun xi,
3364*4882a593Smuzhiyun xis,
3365*4882a593Smuzhiyun xbs,
3366*4882a593Smuzhiyun NULL,
3367*4882a593Smuzhiyun NULL,
3368*4882a593Smuzhiyun &credits);
3369*4882a593Smuzhiyun xis->not_found = old_found;
3370*4882a593Smuzhiyun if (ret) {
3371*4882a593Smuzhiyun mlog_errno(ret);
3372*4882a593Smuzhiyun goto out;
3373*4882a593Smuzhiyun }
3374*4882a593Smuzhiyun
3375*4882a593Smuzhiyun ret = ocfs2_extend_trans(ctxt->handle, credits);
3376*4882a593Smuzhiyun if (ret) {
3377*4882a593Smuzhiyun mlog_errno(ret);
3378*4882a593Smuzhiyun goto out;
3379*4882a593Smuzhiyun }
3380*4882a593Smuzhiyun }
3381*4882a593Smuzhiyun /*
3382*4882a593Smuzhiyun * If no space in inode, we will set extended attribute
3383*4882a593Smuzhiyun * into external block.
3384*4882a593Smuzhiyun */
3385*4882a593Smuzhiyun ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3386*4882a593Smuzhiyun if (ret)
3387*4882a593Smuzhiyun goto out;
3388*4882a593Smuzhiyun if (!xis->not_found) {
3389*4882a593Smuzhiyun /*
3390*4882a593Smuzhiyun * If succeed and that extended attribute
3391*4882a593Smuzhiyun * existing in inode, we will remove it.
3392*4882a593Smuzhiyun */
3393*4882a593Smuzhiyun xi->xi_value = NULL;
3394*4882a593Smuzhiyun xi->xi_value_len = 0;
3395*4882a593Smuzhiyun xbs->not_found = -ENODATA;
3396*4882a593Smuzhiyun ret = ocfs2_calc_xattr_set_need(inode,
3397*4882a593Smuzhiyun di,
3398*4882a593Smuzhiyun xi,
3399*4882a593Smuzhiyun xis,
3400*4882a593Smuzhiyun xbs,
3401*4882a593Smuzhiyun NULL,
3402*4882a593Smuzhiyun NULL,
3403*4882a593Smuzhiyun &credits);
3404*4882a593Smuzhiyun if (ret) {
3405*4882a593Smuzhiyun mlog_errno(ret);
3406*4882a593Smuzhiyun goto out;
3407*4882a593Smuzhiyun }
3408*4882a593Smuzhiyun
3409*4882a593Smuzhiyun ret = ocfs2_extend_trans(ctxt->handle, credits);
3410*4882a593Smuzhiyun if (ret) {
3411*4882a593Smuzhiyun mlog_errno(ret);
3412*4882a593Smuzhiyun goto out;
3413*4882a593Smuzhiyun }
3414*4882a593Smuzhiyun ret = ocfs2_xattr_ibody_set(inode, xi,
3415*4882a593Smuzhiyun xis, ctxt);
3416*4882a593Smuzhiyun }
3417*4882a593Smuzhiyun }
3418*4882a593Smuzhiyun }
3419*4882a593Smuzhiyun
3420*4882a593Smuzhiyun if (!ret) {
3421*4882a593Smuzhiyun /* Update inode ctime. */
3422*4882a593Smuzhiyun ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
3423*4882a593Smuzhiyun xis->inode_bh,
3424*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
3425*4882a593Smuzhiyun if (ret) {
3426*4882a593Smuzhiyun mlog_errno(ret);
3427*4882a593Smuzhiyun goto out;
3428*4882a593Smuzhiyun }
3429*4882a593Smuzhiyun
3430*4882a593Smuzhiyun inode->i_ctime = current_time(inode);
3431*4882a593Smuzhiyun di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3432*4882a593Smuzhiyun di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3433*4882a593Smuzhiyun ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3434*4882a593Smuzhiyun }
3435*4882a593Smuzhiyun out:
3436*4882a593Smuzhiyun return ret;
3437*4882a593Smuzhiyun }
3438*4882a593Smuzhiyun
3439*4882a593Smuzhiyun /*
3440*4882a593Smuzhiyun * This function only called duing creating inode
3441*4882a593Smuzhiyun * for init security/acl xattrs of the new inode.
3442*4882a593Smuzhiyun * All transanction credits have been reserved in mknod.
3443*4882a593Smuzhiyun */
ocfs2_xattr_set_handle(handle_t * handle,struct inode * inode,struct buffer_head * di_bh,int name_index,const char * name,const void * value,size_t value_len,int flags,struct ocfs2_alloc_context * meta_ac,struct ocfs2_alloc_context * data_ac)3444*4882a593Smuzhiyun int ocfs2_xattr_set_handle(handle_t *handle,
3445*4882a593Smuzhiyun struct inode *inode,
3446*4882a593Smuzhiyun struct buffer_head *di_bh,
3447*4882a593Smuzhiyun int name_index,
3448*4882a593Smuzhiyun const char *name,
3449*4882a593Smuzhiyun const void *value,
3450*4882a593Smuzhiyun size_t value_len,
3451*4882a593Smuzhiyun int flags,
3452*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac,
3453*4882a593Smuzhiyun struct ocfs2_alloc_context *data_ac)
3454*4882a593Smuzhiyun {
3455*4882a593Smuzhiyun struct ocfs2_dinode *di;
3456*4882a593Smuzhiyun int ret;
3457*4882a593Smuzhiyun
3458*4882a593Smuzhiyun struct ocfs2_xattr_info xi = {
3459*4882a593Smuzhiyun .xi_name_index = name_index,
3460*4882a593Smuzhiyun .xi_name = name,
3461*4882a593Smuzhiyun .xi_name_len = strlen(name),
3462*4882a593Smuzhiyun .xi_value = value,
3463*4882a593Smuzhiyun .xi_value_len = value_len,
3464*4882a593Smuzhiyun };
3465*4882a593Smuzhiyun
3466*4882a593Smuzhiyun struct ocfs2_xattr_search xis = {
3467*4882a593Smuzhiyun .not_found = -ENODATA,
3468*4882a593Smuzhiyun };
3469*4882a593Smuzhiyun
3470*4882a593Smuzhiyun struct ocfs2_xattr_search xbs = {
3471*4882a593Smuzhiyun .not_found = -ENODATA,
3472*4882a593Smuzhiyun };
3473*4882a593Smuzhiyun
3474*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt ctxt = {
3475*4882a593Smuzhiyun .handle = handle,
3476*4882a593Smuzhiyun .meta_ac = meta_ac,
3477*4882a593Smuzhiyun .data_ac = data_ac,
3478*4882a593Smuzhiyun };
3479*4882a593Smuzhiyun
3480*4882a593Smuzhiyun if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3481*4882a593Smuzhiyun return -EOPNOTSUPP;
3482*4882a593Smuzhiyun
3483*4882a593Smuzhiyun /*
3484*4882a593Smuzhiyun * In extreme situation, may need xattr bucket when
3485*4882a593Smuzhiyun * block size is too small. And we have already reserved
3486*4882a593Smuzhiyun * the credits for bucket in mknod.
3487*4882a593Smuzhiyun */
3488*4882a593Smuzhiyun if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3489*4882a593Smuzhiyun xbs.bucket = ocfs2_xattr_bucket_new(inode);
3490*4882a593Smuzhiyun if (!xbs.bucket) {
3491*4882a593Smuzhiyun mlog_errno(-ENOMEM);
3492*4882a593Smuzhiyun return -ENOMEM;
3493*4882a593Smuzhiyun }
3494*4882a593Smuzhiyun }
3495*4882a593Smuzhiyun
3496*4882a593Smuzhiyun xis.inode_bh = xbs.inode_bh = di_bh;
3497*4882a593Smuzhiyun di = (struct ocfs2_dinode *)di_bh->b_data;
3498*4882a593Smuzhiyun
3499*4882a593Smuzhiyun down_write(&OCFS2_I(inode)->ip_xattr_sem);
3500*4882a593Smuzhiyun
3501*4882a593Smuzhiyun ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3502*4882a593Smuzhiyun if (ret)
3503*4882a593Smuzhiyun goto cleanup;
3504*4882a593Smuzhiyun if (xis.not_found) {
3505*4882a593Smuzhiyun ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3506*4882a593Smuzhiyun if (ret)
3507*4882a593Smuzhiyun goto cleanup;
3508*4882a593Smuzhiyun }
3509*4882a593Smuzhiyun
3510*4882a593Smuzhiyun ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3511*4882a593Smuzhiyun
3512*4882a593Smuzhiyun cleanup:
3513*4882a593Smuzhiyun up_write(&OCFS2_I(inode)->ip_xattr_sem);
3514*4882a593Smuzhiyun brelse(xbs.xattr_bh);
3515*4882a593Smuzhiyun ocfs2_xattr_bucket_free(xbs.bucket);
3516*4882a593Smuzhiyun
3517*4882a593Smuzhiyun return ret;
3518*4882a593Smuzhiyun }
3519*4882a593Smuzhiyun
3520*4882a593Smuzhiyun /*
3521*4882a593Smuzhiyun * ocfs2_xattr_set()
3522*4882a593Smuzhiyun *
3523*4882a593Smuzhiyun * Set, replace or remove an extended attribute for this inode.
3524*4882a593Smuzhiyun * value is NULL to remove an existing extended attribute, else either
3525*4882a593Smuzhiyun * create or replace an extended attribute.
3526*4882a593Smuzhiyun */
ocfs2_xattr_set(struct inode * inode,int name_index,const char * name,const void * value,size_t value_len,int flags)3527*4882a593Smuzhiyun int ocfs2_xattr_set(struct inode *inode,
3528*4882a593Smuzhiyun int name_index,
3529*4882a593Smuzhiyun const char *name,
3530*4882a593Smuzhiyun const void *value,
3531*4882a593Smuzhiyun size_t value_len,
3532*4882a593Smuzhiyun int flags)
3533*4882a593Smuzhiyun {
3534*4882a593Smuzhiyun struct buffer_head *di_bh = NULL;
3535*4882a593Smuzhiyun struct ocfs2_dinode *di;
3536*4882a593Smuzhiyun int ret, credits, had_lock, ref_meta = 0, ref_credits = 0;
3537*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3538*4882a593Smuzhiyun struct inode *tl_inode = osb->osb_tl_inode;
3539*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, NULL, };
3540*4882a593Smuzhiyun struct ocfs2_refcount_tree *ref_tree = NULL;
3541*4882a593Smuzhiyun struct ocfs2_lock_holder oh;
3542*4882a593Smuzhiyun
3543*4882a593Smuzhiyun struct ocfs2_xattr_info xi = {
3544*4882a593Smuzhiyun .xi_name_index = name_index,
3545*4882a593Smuzhiyun .xi_name = name,
3546*4882a593Smuzhiyun .xi_name_len = strlen(name),
3547*4882a593Smuzhiyun .xi_value = value,
3548*4882a593Smuzhiyun .xi_value_len = value_len,
3549*4882a593Smuzhiyun };
3550*4882a593Smuzhiyun
3551*4882a593Smuzhiyun struct ocfs2_xattr_search xis = {
3552*4882a593Smuzhiyun .not_found = -ENODATA,
3553*4882a593Smuzhiyun };
3554*4882a593Smuzhiyun
3555*4882a593Smuzhiyun struct ocfs2_xattr_search xbs = {
3556*4882a593Smuzhiyun .not_found = -ENODATA,
3557*4882a593Smuzhiyun };
3558*4882a593Smuzhiyun
3559*4882a593Smuzhiyun if (!ocfs2_supports_xattr(osb))
3560*4882a593Smuzhiyun return -EOPNOTSUPP;
3561*4882a593Smuzhiyun
3562*4882a593Smuzhiyun /*
3563*4882a593Smuzhiyun * Only xbs will be used on indexed trees. xis doesn't need a
3564*4882a593Smuzhiyun * bucket.
3565*4882a593Smuzhiyun */
3566*4882a593Smuzhiyun xbs.bucket = ocfs2_xattr_bucket_new(inode);
3567*4882a593Smuzhiyun if (!xbs.bucket) {
3568*4882a593Smuzhiyun mlog_errno(-ENOMEM);
3569*4882a593Smuzhiyun return -ENOMEM;
3570*4882a593Smuzhiyun }
3571*4882a593Smuzhiyun
3572*4882a593Smuzhiyun had_lock = ocfs2_inode_lock_tracker(inode, &di_bh, 1, &oh);
3573*4882a593Smuzhiyun if (had_lock < 0) {
3574*4882a593Smuzhiyun ret = had_lock;
3575*4882a593Smuzhiyun mlog_errno(ret);
3576*4882a593Smuzhiyun goto cleanup_nolock;
3577*4882a593Smuzhiyun }
3578*4882a593Smuzhiyun xis.inode_bh = xbs.inode_bh = di_bh;
3579*4882a593Smuzhiyun di = (struct ocfs2_dinode *)di_bh->b_data;
3580*4882a593Smuzhiyun
3581*4882a593Smuzhiyun down_write(&OCFS2_I(inode)->ip_xattr_sem);
3582*4882a593Smuzhiyun /*
3583*4882a593Smuzhiyun * Scan inode and external block to find the same name
3584*4882a593Smuzhiyun * extended attribute and collect search information.
3585*4882a593Smuzhiyun */
3586*4882a593Smuzhiyun ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3587*4882a593Smuzhiyun if (ret)
3588*4882a593Smuzhiyun goto cleanup;
3589*4882a593Smuzhiyun if (xis.not_found) {
3590*4882a593Smuzhiyun ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3591*4882a593Smuzhiyun if (ret)
3592*4882a593Smuzhiyun goto cleanup;
3593*4882a593Smuzhiyun }
3594*4882a593Smuzhiyun
3595*4882a593Smuzhiyun if (xis.not_found && xbs.not_found) {
3596*4882a593Smuzhiyun ret = -ENODATA;
3597*4882a593Smuzhiyun if (flags & XATTR_REPLACE)
3598*4882a593Smuzhiyun goto cleanup;
3599*4882a593Smuzhiyun ret = 0;
3600*4882a593Smuzhiyun if (!value)
3601*4882a593Smuzhiyun goto cleanup;
3602*4882a593Smuzhiyun } else {
3603*4882a593Smuzhiyun ret = -EEXIST;
3604*4882a593Smuzhiyun if (flags & XATTR_CREATE)
3605*4882a593Smuzhiyun goto cleanup;
3606*4882a593Smuzhiyun }
3607*4882a593Smuzhiyun
3608*4882a593Smuzhiyun /* Check whether the value is refcounted and do some preparation. */
3609*4882a593Smuzhiyun if (ocfs2_is_refcount_inode(inode) &&
3610*4882a593Smuzhiyun (!xis.not_found || !xbs.not_found)) {
3611*4882a593Smuzhiyun ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3612*4882a593Smuzhiyun &xis, &xbs, &ref_tree,
3613*4882a593Smuzhiyun &ref_meta, &ref_credits);
3614*4882a593Smuzhiyun if (ret) {
3615*4882a593Smuzhiyun mlog_errno(ret);
3616*4882a593Smuzhiyun goto cleanup;
3617*4882a593Smuzhiyun }
3618*4882a593Smuzhiyun }
3619*4882a593Smuzhiyun
3620*4882a593Smuzhiyun inode_lock(tl_inode);
3621*4882a593Smuzhiyun
3622*4882a593Smuzhiyun if (ocfs2_truncate_log_needs_flush(osb)) {
3623*4882a593Smuzhiyun ret = __ocfs2_flush_truncate_log(osb);
3624*4882a593Smuzhiyun if (ret < 0) {
3625*4882a593Smuzhiyun inode_unlock(tl_inode);
3626*4882a593Smuzhiyun mlog_errno(ret);
3627*4882a593Smuzhiyun goto cleanup;
3628*4882a593Smuzhiyun }
3629*4882a593Smuzhiyun }
3630*4882a593Smuzhiyun inode_unlock(tl_inode);
3631*4882a593Smuzhiyun
3632*4882a593Smuzhiyun ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3633*4882a593Smuzhiyun &xbs, &ctxt, ref_meta, &credits);
3634*4882a593Smuzhiyun if (ret) {
3635*4882a593Smuzhiyun mlog_errno(ret);
3636*4882a593Smuzhiyun goto cleanup;
3637*4882a593Smuzhiyun }
3638*4882a593Smuzhiyun
3639*4882a593Smuzhiyun /* we need to update inode's ctime field, so add credit for it. */
3640*4882a593Smuzhiyun credits += OCFS2_INODE_UPDATE_CREDITS;
3641*4882a593Smuzhiyun ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3642*4882a593Smuzhiyun if (IS_ERR(ctxt.handle)) {
3643*4882a593Smuzhiyun ret = PTR_ERR(ctxt.handle);
3644*4882a593Smuzhiyun mlog_errno(ret);
3645*4882a593Smuzhiyun goto out_free_ac;
3646*4882a593Smuzhiyun }
3647*4882a593Smuzhiyun
3648*4882a593Smuzhiyun ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3649*4882a593Smuzhiyun ocfs2_update_inode_fsync_trans(ctxt.handle, inode, 0);
3650*4882a593Smuzhiyun
3651*4882a593Smuzhiyun ocfs2_commit_trans(osb, ctxt.handle);
3652*4882a593Smuzhiyun
3653*4882a593Smuzhiyun out_free_ac:
3654*4882a593Smuzhiyun if (ctxt.data_ac)
3655*4882a593Smuzhiyun ocfs2_free_alloc_context(ctxt.data_ac);
3656*4882a593Smuzhiyun if (ctxt.meta_ac)
3657*4882a593Smuzhiyun ocfs2_free_alloc_context(ctxt.meta_ac);
3658*4882a593Smuzhiyun if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3659*4882a593Smuzhiyun ocfs2_schedule_truncate_log_flush(osb, 1);
3660*4882a593Smuzhiyun ocfs2_run_deallocs(osb, &ctxt.dealloc);
3661*4882a593Smuzhiyun
3662*4882a593Smuzhiyun cleanup:
3663*4882a593Smuzhiyun if (ref_tree)
3664*4882a593Smuzhiyun ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3665*4882a593Smuzhiyun up_write(&OCFS2_I(inode)->ip_xattr_sem);
3666*4882a593Smuzhiyun if (!value && !ret) {
3667*4882a593Smuzhiyun ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3668*4882a593Smuzhiyun if (ret)
3669*4882a593Smuzhiyun mlog_errno(ret);
3670*4882a593Smuzhiyun }
3671*4882a593Smuzhiyun ocfs2_inode_unlock_tracker(inode, 1, &oh, had_lock);
3672*4882a593Smuzhiyun cleanup_nolock:
3673*4882a593Smuzhiyun brelse(di_bh);
3674*4882a593Smuzhiyun brelse(xbs.xattr_bh);
3675*4882a593Smuzhiyun ocfs2_xattr_bucket_free(xbs.bucket);
3676*4882a593Smuzhiyun
3677*4882a593Smuzhiyun return ret;
3678*4882a593Smuzhiyun }
3679*4882a593Smuzhiyun
3680*4882a593Smuzhiyun /*
3681*4882a593Smuzhiyun * Find the xattr extent rec which may contains name_hash.
3682*4882a593Smuzhiyun * e_cpos will be the first name hash of the xattr rec.
3683*4882a593Smuzhiyun * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3684*4882a593Smuzhiyun */
ocfs2_xattr_get_rec(struct inode * inode,u32 name_hash,u64 * p_blkno,u32 * e_cpos,u32 * num_clusters,struct ocfs2_extent_list * el)3685*4882a593Smuzhiyun static int ocfs2_xattr_get_rec(struct inode *inode,
3686*4882a593Smuzhiyun u32 name_hash,
3687*4882a593Smuzhiyun u64 *p_blkno,
3688*4882a593Smuzhiyun u32 *e_cpos,
3689*4882a593Smuzhiyun u32 *num_clusters,
3690*4882a593Smuzhiyun struct ocfs2_extent_list *el)
3691*4882a593Smuzhiyun {
3692*4882a593Smuzhiyun int ret = 0, i;
3693*4882a593Smuzhiyun struct buffer_head *eb_bh = NULL;
3694*4882a593Smuzhiyun struct ocfs2_extent_block *eb;
3695*4882a593Smuzhiyun struct ocfs2_extent_rec *rec = NULL;
3696*4882a593Smuzhiyun u64 e_blkno = 0;
3697*4882a593Smuzhiyun
3698*4882a593Smuzhiyun if (el->l_tree_depth) {
3699*4882a593Smuzhiyun ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3700*4882a593Smuzhiyun &eb_bh);
3701*4882a593Smuzhiyun if (ret) {
3702*4882a593Smuzhiyun mlog_errno(ret);
3703*4882a593Smuzhiyun goto out;
3704*4882a593Smuzhiyun }
3705*4882a593Smuzhiyun
3706*4882a593Smuzhiyun eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3707*4882a593Smuzhiyun el = &eb->h_list;
3708*4882a593Smuzhiyun
3709*4882a593Smuzhiyun if (el->l_tree_depth) {
3710*4882a593Smuzhiyun ret = ocfs2_error(inode->i_sb,
3711*4882a593Smuzhiyun "Inode %lu has non zero tree depth in xattr tree block %llu\n",
3712*4882a593Smuzhiyun inode->i_ino,
3713*4882a593Smuzhiyun (unsigned long long)eb_bh->b_blocknr);
3714*4882a593Smuzhiyun goto out;
3715*4882a593Smuzhiyun }
3716*4882a593Smuzhiyun }
3717*4882a593Smuzhiyun
3718*4882a593Smuzhiyun for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3719*4882a593Smuzhiyun rec = &el->l_recs[i];
3720*4882a593Smuzhiyun
3721*4882a593Smuzhiyun if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3722*4882a593Smuzhiyun e_blkno = le64_to_cpu(rec->e_blkno);
3723*4882a593Smuzhiyun break;
3724*4882a593Smuzhiyun }
3725*4882a593Smuzhiyun }
3726*4882a593Smuzhiyun
3727*4882a593Smuzhiyun if (!e_blkno) {
3728*4882a593Smuzhiyun ret = ocfs2_error(inode->i_sb, "Inode %lu has bad extent record (%u, %u, 0) in xattr\n",
3729*4882a593Smuzhiyun inode->i_ino,
3730*4882a593Smuzhiyun le32_to_cpu(rec->e_cpos),
3731*4882a593Smuzhiyun ocfs2_rec_clusters(el, rec));
3732*4882a593Smuzhiyun goto out;
3733*4882a593Smuzhiyun }
3734*4882a593Smuzhiyun
3735*4882a593Smuzhiyun *p_blkno = le64_to_cpu(rec->e_blkno);
3736*4882a593Smuzhiyun *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3737*4882a593Smuzhiyun if (e_cpos)
3738*4882a593Smuzhiyun *e_cpos = le32_to_cpu(rec->e_cpos);
3739*4882a593Smuzhiyun out:
3740*4882a593Smuzhiyun brelse(eb_bh);
3741*4882a593Smuzhiyun return ret;
3742*4882a593Smuzhiyun }
3743*4882a593Smuzhiyun
3744*4882a593Smuzhiyun typedef int (xattr_bucket_func)(struct inode *inode,
3745*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket,
3746*4882a593Smuzhiyun void *para);
3747*4882a593Smuzhiyun
ocfs2_find_xe_in_bucket(struct inode * inode,struct ocfs2_xattr_bucket * bucket,int name_index,const char * name,u32 name_hash,u16 * xe_index,int * found)3748*4882a593Smuzhiyun static int ocfs2_find_xe_in_bucket(struct inode *inode,
3749*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket,
3750*4882a593Smuzhiyun int name_index,
3751*4882a593Smuzhiyun const char *name,
3752*4882a593Smuzhiyun u32 name_hash,
3753*4882a593Smuzhiyun u16 *xe_index,
3754*4882a593Smuzhiyun int *found)
3755*4882a593Smuzhiyun {
3756*4882a593Smuzhiyun int i, ret = 0, cmp = 1, block_off, new_offset;
3757*4882a593Smuzhiyun struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3758*4882a593Smuzhiyun size_t name_len = strlen(name);
3759*4882a593Smuzhiyun struct ocfs2_xattr_entry *xe = NULL;
3760*4882a593Smuzhiyun char *xe_name;
3761*4882a593Smuzhiyun
3762*4882a593Smuzhiyun /*
3763*4882a593Smuzhiyun * We don't use binary search in the bucket because there
3764*4882a593Smuzhiyun * may be multiple entries with the same name hash.
3765*4882a593Smuzhiyun */
3766*4882a593Smuzhiyun for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3767*4882a593Smuzhiyun xe = &xh->xh_entries[i];
3768*4882a593Smuzhiyun
3769*4882a593Smuzhiyun if (name_hash > le32_to_cpu(xe->xe_name_hash))
3770*4882a593Smuzhiyun continue;
3771*4882a593Smuzhiyun else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3772*4882a593Smuzhiyun break;
3773*4882a593Smuzhiyun
3774*4882a593Smuzhiyun cmp = name_index - ocfs2_xattr_get_type(xe);
3775*4882a593Smuzhiyun if (!cmp)
3776*4882a593Smuzhiyun cmp = name_len - xe->xe_name_len;
3777*4882a593Smuzhiyun if (cmp)
3778*4882a593Smuzhiyun continue;
3779*4882a593Smuzhiyun
3780*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3781*4882a593Smuzhiyun xh,
3782*4882a593Smuzhiyun i,
3783*4882a593Smuzhiyun &block_off,
3784*4882a593Smuzhiyun &new_offset);
3785*4882a593Smuzhiyun if (ret) {
3786*4882a593Smuzhiyun mlog_errno(ret);
3787*4882a593Smuzhiyun break;
3788*4882a593Smuzhiyun }
3789*4882a593Smuzhiyun
3790*4882a593Smuzhiyun
3791*4882a593Smuzhiyun xe_name = bucket_block(bucket, block_off) + new_offset;
3792*4882a593Smuzhiyun if (!memcmp(name, xe_name, name_len)) {
3793*4882a593Smuzhiyun *xe_index = i;
3794*4882a593Smuzhiyun *found = 1;
3795*4882a593Smuzhiyun ret = 0;
3796*4882a593Smuzhiyun break;
3797*4882a593Smuzhiyun }
3798*4882a593Smuzhiyun }
3799*4882a593Smuzhiyun
3800*4882a593Smuzhiyun return ret;
3801*4882a593Smuzhiyun }
3802*4882a593Smuzhiyun
3803*4882a593Smuzhiyun /*
3804*4882a593Smuzhiyun * Find the specified xattr entry in a series of buckets.
3805*4882a593Smuzhiyun * This series start from p_blkno and last for num_clusters.
3806*4882a593Smuzhiyun * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3807*4882a593Smuzhiyun * the num of the valid buckets.
3808*4882a593Smuzhiyun *
3809*4882a593Smuzhiyun * Return the buffer_head this xattr should reside in. And if the xattr's
3810*4882a593Smuzhiyun * hash is in the gap of 2 buckets, return the lower bucket.
3811*4882a593Smuzhiyun */
ocfs2_xattr_bucket_find(struct inode * inode,int name_index,const char * name,u32 name_hash,u64 p_blkno,u32 first_hash,u32 num_clusters,struct ocfs2_xattr_search * xs)3812*4882a593Smuzhiyun static int ocfs2_xattr_bucket_find(struct inode *inode,
3813*4882a593Smuzhiyun int name_index,
3814*4882a593Smuzhiyun const char *name,
3815*4882a593Smuzhiyun u32 name_hash,
3816*4882a593Smuzhiyun u64 p_blkno,
3817*4882a593Smuzhiyun u32 first_hash,
3818*4882a593Smuzhiyun u32 num_clusters,
3819*4882a593Smuzhiyun struct ocfs2_xattr_search *xs)
3820*4882a593Smuzhiyun {
3821*4882a593Smuzhiyun int ret, found = 0;
3822*4882a593Smuzhiyun struct ocfs2_xattr_header *xh = NULL;
3823*4882a593Smuzhiyun struct ocfs2_xattr_entry *xe = NULL;
3824*4882a593Smuzhiyun u16 index = 0;
3825*4882a593Smuzhiyun u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3826*4882a593Smuzhiyun int low_bucket = 0, bucket, high_bucket;
3827*4882a593Smuzhiyun struct ocfs2_xattr_bucket *search;
3828*4882a593Smuzhiyun u64 blkno, lower_blkno = 0;
3829*4882a593Smuzhiyun
3830*4882a593Smuzhiyun search = ocfs2_xattr_bucket_new(inode);
3831*4882a593Smuzhiyun if (!search) {
3832*4882a593Smuzhiyun ret = -ENOMEM;
3833*4882a593Smuzhiyun mlog_errno(ret);
3834*4882a593Smuzhiyun goto out;
3835*4882a593Smuzhiyun }
3836*4882a593Smuzhiyun
3837*4882a593Smuzhiyun ret = ocfs2_read_xattr_bucket(search, p_blkno);
3838*4882a593Smuzhiyun if (ret) {
3839*4882a593Smuzhiyun mlog_errno(ret);
3840*4882a593Smuzhiyun goto out;
3841*4882a593Smuzhiyun }
3842*4882a593Smuzhiyun
3843*4882a593Smuzhiyun xh = bucket_xh(search);
3844*4882a593Smuzhiyun high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
3845*4882a593Smuzhiyun while (low_bucket <= high_bucket) {
3846*4882a593Smuzhiyun ocfs2_xattr_bucket_relse(search);
3847*4882a593Smuzhiyun
3848*4882a593Smuzhiyun bucket = (low_bucket + high_bucket) / 2;
3849*4882a593Smuzhiyun blkno = p_blkno + bucket * blk_per_bucket;
3850*4882a593Smuzhiyun ret = ocfs2_read_xattr_bucket(search, blkno);
3851*4882a593Smuzhiyun if (ret) {
3852*4882a593Smuzhiyun mlog_errno(ret);
3853*4882a593Smuzhiyun goto out;
3854*4882a593Smuzhiyun }
3855*4882a593Smuzhiyun
3856*4882a593Smuzhiyun xh = bucket_xh(search);
3857*4882a593Smuzhiyun xe = &xh->xh_entries[0];
3858*4882a593Smuzhiyun if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3859*4882a593Smuzhiyun high_bucket = bucket - 1;
3860*4882a593Smuzhiyun continue;
3861*4882a593Smuzhiyun }
3862*4882a593Smuzhiyun
3863*4882a593Smuzhiyun /*
3864*4882a593Smuzhiyun * Check whether the hash of the last entry in our
3865*4882a593Smuzhiyun * bucket is larger than the search one. for an empty
3866*4882a593Smuzhiyun * bucket, the last one is also the first one.
3867*4882a593Smuzhiyun */
3868*4882a593Smuzhiyun if (xh->xh_count)
3869*4882a593Smuzhiyun xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3870*4882a593Smuzhiyun
3871*4882a593Smuzhiyun /* record lower_blkno which may be the insert place. */
3872*4882a593Smuzhiyun lower_blkno = blkno;
3873*4882a593Smuzhiyun
3874*4882a593Smuzhiyun if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3875*4882a593Smuzhiyun low_bucket = bucket + 1;
3876*4882a593Smuzhiyun continue;
3877*4882a593Smuzhiyun }
3878*4882a593Smuzhiyun
3879*4882a593Smuzhiyun /* the searched xattr should reside in this bucket if exists. */
3880*4882a593Smuzhiyun ret = ocfs2_find_xe_in_bucket(inode, search,
3881*4882a593Smuzhiyun name_index, name, name_hash,
3882*4882a593Smuzhiyun &index, &found);
3883*4882a593Smuzhiyun if (ret) {
3884*4882a593Smuzhiyun mlog_errno(ret);
3885*4882a593Smuzhiyun goto out;
3886*4882a593Smuzhiyun }
3887*4882a593Smuzhiyun break;
3888*4882a593Smuzhiyun }
3889*4882a593Smuzhiyun
3890*4882a593Smuzhiyun /*
3891*4882a593Smuzhiyun * Record the bucket we have found.
3892*4882a593Smuzhiyun * When the xattr's hash value is in the gap of 2 buckets, we will
3893*4882a593Smuzhiyun * always set it to the previous bucket.
3894*4882a593Smuzhiyun */
3895*4882a593Smuzhiyun if (!lower_blkno)
3896*4882a593Smuzhiyun lower_blkno = p_blkno;
3897*4882a593Smuzhiyun
3898*4882a593Smuzhiyun /* This should be in cache - we just read it during the search */
3899*4882a593Smuzhiyun ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3900*4882a593Smuzhiyun if (ret) {
3901*4882a593Smuzhiyun mlog_errno(ret);
3902*4882a593Smuzhiyun goto out;
3903*4882a593Smuzhiyun }
3904*4882a593Smuzhiyun
3905*4882a593Smuzhiyun xs->header = bucket_xh(xs->bucket);
3906*4882a593Smuzhiyun xs->base = bucket_block(xs->bucket, 0);
3907*4882a593Smuzhiyun xs->end = xs->base + inode->i_sb->s_blocksize;
3908*4882a593Smuzhiyun
3909*4882a593Smuzhiyun if (found) {
3910*4882a593Smuzhiyun xs->here = &xs->header->xh_entries[index];
3911*4882a593Smuzhiyun trace_ocfs2_xattr_bucket_find(OCFS2_I(inode)->ip_blkno,
3912*4882a593Smuzhiyun name, name_index, name_hash,
3913*4882a593Smuzhiyun (unsigned long long)bucket_blkno(xs->bucket),
3914*4882a593Smuzhiyun index);
3915*4882a593Smuzhiyun } else
3916*4882a593Smuzhiyun ret = -ENODATA;
3917*4882a593Smuzhiyun
3918*4882a593Smuzhiyun out:
3919*4882a593Smuzhiyun ocfs2_xattr_bucket_free(search);
3920*4882a593Smuzhiyun return ret;
3921*4882a593Smuzhiyun }
3922*4882a593Smuzhiyun
ocfs2_xattr_index_block_find(struct inode * inode,struct buffer_head * root_bh,int name_index,const char * name,struct ocfs2_xattr_search * xs)3923*4882a593Smuzhiyun static int ocfs2_xattr_index_block_find(struct inode *inode,
3924*4882a593Smuzhiyun struct buffer_head *root_bh,
3925*4882a593Smuzhiyun int name_index,
3926*4882a593Smuzhiyun const char *name,
3927*4882a593Smuzhiyun struct ocfs2_xattr_search *xs)
3928*4882a593Smuzhiyun {
3929*4882a593Smuzhiyun int ret;
3930*4882a593Smuzhiyun struct ocfs2_xattr_block *xb =
3931*4882a593Smuzhiyun (struct ocfs2_xattr_block *)root_bh->b_data;
3932*4882a593Smuzhiyun struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3933*4882a593Smuzhiyun struct ocfs2_extent_list *el = &xb_root->xt_list;
3934*4882a593Smuzhiyun u64 p_blkno = 0;
3935*4882a593Smuzhiyun u32 first_hash, num_clusters = 0;
3936*4882a593Smuzhiyun u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3937*4882a593Smuzhiyun
3938*4882a593Smuzhiyun if (le16_to_cpu(el->l_next_free_rec) == 0)
3939*4882a593Smuzhiyun return -ENODATA;
3940*4882a593Smuzhiyun
3941*4882a593Smuzhiyun trace_ocfs2_xattr_index_block_find(OCFS2_I(inode)->ip_blkno,
3942*4882a593Smuzhiyun name, name_index, name_hash,
3943*4882a593Smuzhiyun (unsigned long long)root_bh->b_blocknr,
3944*4882a593Smuzhiyun -1);
3945*4882a593Smuzhiyun
3946*4882a593Smuzhiyun ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3947*4882a593Smuzhiyun &num_clusters, el);
3948*4882a593Smuzhiyun if (ret) {
3949*4882a593Smuzhiyun mlog_errno(ret);
3950*4882a593Smuzhiyun goto out;
3951*4882a593Smuzhiyun }
3952*4882a593Smuzhiyun
3953*4882a593Smuzhiyun BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3954*4882a593Smuzhiyun
3955*4882a593Smuzhiyun trace_ocfs2_xattr_index_block_find_rec(OCFS2_I(inode)->ip_blkno,
3956*4882a593Smuzhiyun name, name_index, first_hash,
3957*4882a593Smuzhiyun (unsigned long long)p_blkno,
3958*4882a593Smuzhiyun num_clusters);
3959*4882a593Smuzhiyun
3960*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3961*4882a593Smuzhiyun p_blkno, first_hash, num_clusters, xs);
3962*4882a593Smuzhiyun
3963*4882a593Smuzhiyun out:
3964*4882a593Smuzhiyun return ret;
3965*4882a593Smuzhiyun }
3966*4882a593Smuzhiyun
ocfs2_iterate_xattr_buckets(struct inode * inode,u64 blkno,u32 clusters,xattr_bucket_func * func,void * para)3967*4882a593Smuzhiyun static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3968*4882a593Smuzhiyun u64 blkno,
3969*4882a593Smuzhiyun u32 clusters,
3970*4882a593Smuzhiyun xattr_bucket_func *func,
3971*4882a593Smuzhiyun void *para)
3972*4882a593Smuzhiyun {
3973*4882a593Smuzhiyun int i, ret = 0;
3974*4882a593Smuzhiyun u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3975*4882a593Smuzhiyun u32 num_buckets = clusters * bpc;
3976*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket;
3977*4882a593Smuzhiyun
3978*4882a593Smuzhiyun bucket = ocfs2_xattr_bucket_new(inode);
3979*4882a593Smuzhiyun if (!bucket) {
3980*4882a593Smuzhiyun mlog_errno(-ENOMEM);
3981*4882a593Smuzhiyun return -ENOMEM;
3982*4882a593Smuzhiyun }
3983*4882a593Smuzhiyun
3984*4882a593Smuzhiyun trace_ocfs2_iterate_xattr_buckets(
3985*4882a593Smuzhiyun (unsigned long long)OCFS2_I(inode)->ip_blkno,
3986*4882a593Smuzhiyun (unsigned long long)blkno, clusters);
3987*4882a593Smuzhiyun
3988*4882a593Smuzhiyun for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3989*4882a593Smuzhiyun ret = ocfs2_read_xattr_bucket(bucket, blkno);
3990*4882a593Smuzhiyun if (ret) {
3991*4882a593Smuzhiyun mlog_errno(ret);
3992*4882a593Smuzhiyun break;
3993*4882a593Smuzhiyun }
3994*4882a593Smuzhiyun
3995*4882a593Smuzhiyun /*
3996*4882a593Smuzhiyun * The real bucket num in this series of blocks is stored
3997*4882a593Smuzhiyun * in the 1st bucket.
3998*4882a593Smuzhiyun */
3999*4882a593Smuzhiyun if (i == 0)
4000*4882a593Smuzhiyun num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
4001*4882a593Smuzhiyun
4002*4882a593Smuzhiyun trace_ocfs2_iterate_xattr_bucket((unsigned long long)blkno,
4003*4882a593Smuzhiyun le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
4004*4882a593Smuzhiyun if (func) {
4005*4882a593Smuzhiyun ret = func(inode, bucket, para);
4006*4882a593Smuzhiyun if (ret && ret != -ERANGE)
4007*4882a593Smuzhiyun mlog_errno(ret);
4008*4882a593Smuzhiyun /* Fall through to bucket_relse() */
4009*4882a593Smuzhiyun }
4010*4882a593Smuzhiyun
4011*4882a593Smuzhiyun ocfs2_xattr_bucket_relse(bucket);
4012*4882a593Smuzhiyun if (ret)
4013*4882a593Smuzhiyun break;
4014*4882a593Smuzhiyun }
4015*4882a593Smuzhiyun
4016*4882a593Smuzhiyun ocfs2_xattr_bucket_free(bucket);
4017*4882a593Smuzhiyun return ret;
4018*4882a593Smuzhiyun }
4019*4882a593Smuzhiyun
4020*4882a593Smuzhiyun struct ocfs2_xattr_tree_list {
4021*4882a593Smuzhiyun char *buffer;
4022*4882a593Smuzhiyun size_t buffer_size;
4023*4882a593Smuzhiyun size_t result;
4024*4882a593Smuzhiyun };
4025*4882a593Smuzhiyun
ocfs2_xattr_bucket_get_name_value(struct super_block * sb,struct ocfs2_xattr_header * xh,int index,int * block_off,int * new_offset)4026*4882a593Smuzhiyun static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
4027*4882a593Smuzhiyun struct ocfs2_xattr_header *xh,
4028*4882a593Smuzhiyun int index,
4029*4882a593Smuzhiyun int *block_off,
4030*4882a593Smuzhiyun int *new_offset)
4031*4882a593Smuzhiyun {
4032*4882a593Smuzhiyun u16 name_offset;
4033*4882a593Smuzhiyun
4034*4882a593Smuzhiyun if (index < 0 || index >= le16_to_cpu(xh->xh_count))
4035*4882a593Smuzhiyun return -EINVAL;
4036*4882a593Smuzhiyun
4037*4882a593Smuzhiyun name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
4038*4882a593Smuzhiyun
4039*4882a593Smuzhiyun *block_off = name_offset >> sb->s_blocksize_bits;
4040*4882a593Smuzhiyun *new_offset = name_offset % sb->s_blocksize;
4041*4882a593Smuzhiyun
4042*4882a593Smuzhiyun return 0;
4043*4882a593Smuzhiyun }
4044*4882a593Smuzhiyun
ocfs2_list_xattr_bucket(struct inode * inode,struct ocfs2_xattr_bucket * bucket,void * para)4045*4882a593Smuzhiyun static int ocfs2_list_xattr_bucket(struct inode *inode,
4046*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket,
4047*4882a593Smuzhiyun void *para)
4048*4882a593Smuzhiyun {
4049*4882a593Smuzhiyun int ret = 0, type;
4050*4882a593Smuzhiyun struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
4051*4882a593Smuzhiyun int i, block_off, new_offset;
4052*4882a593Smuzhiyun const char *name;
4053*4882a593Smuzhiyun
4054*4882a593Smuzhiyun for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
4055*4882a593Smuzhiyun struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
4056*4882a593Smuzhiyun type = ocfs2_xattr_get_type(entry);
4057*4882a593Smuzhiyun
4058*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
4059*4882a593Smuzhiyun bucket_xh(bucket),
4060*4882a593Smuzhiyun i,
4061*4882a593Smuzhiyun &block_off,
4062*4882a593Smuzhiyun &new_offset);
4063*4882a593Smuzhiyun if (ret)
4064*4882a593Smuzhiyun break;
4065*4882a593Smuzhiyun
4066*4882a593Smuzhiyun name = (const char *)bucket_block(bucket, block_off) +
4067*4882a593Smuzhiyun new_offset;
4068*4882a593Smuzhiyun ret = ocfs2_xattr_list_entry(inode->i_sb,
4069*4882a593Smuzhiyun xl->buffer,
4070*4882a593Smuzhiyun xl->buffer_size,
4071*4882a593Smuzhiyun &xl->result,
4072*4882a593Smuzhiyun type, name,
4073*4882a593Smuzhiyun entry->xe_name_len);
4074*4882a593Smuzhiyun if (ret)
4075*4882a593Smuzhiyun break;
4076*4882a593Smuzhiyun }
4077*4882a593Smuzhiyun
4078*4882a593Smuzhiyun return ret;
4079*4882a593Smuzhiyun }
4080*4882a593Smuzhiyun
ocfs2_iterate_xattr_index_block(struct inode * inode,struct buffer_head * blk_bh,xattr_tree_rec_func * rec_func,void * para)4081*4882a593Smuzhiyun static int ocfs2_iterate_xattr_index_block(struct inode *inode,
4082*4882a593Smuzhiyun struct buffer_head *blk_bh,
4083*4882a593Smuzhiyun xattr_tree_rec_func *rec_func,
4084*4882a593Smuzhiyun void *para)
4085*4882a593Smuzhiyun {
4086*4882a593Smuzhiyun struct ocfs2_xattr_block *xb =
4087*4882a593Smuzhiyun (struct ocfs2_xattr_block *)blk_bh->b_data;
4088*4882a593Smuzhiyun struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4089*4882a593Smuzhiyun int ret = 0;
4090*4882a593Smuzhiyun u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
4091*4882a593Smuzhiyun u64 p_blkno = 0;
4092*4882a593Smuzhiyun
4093*4882a593Smuzhiyun if (!el->l_next_free_rec || !rec_func)
4094*4882a593Smuzhiyun return 0;
4095*4882a593Smuzhiyun
4096*4882a593Smuzhiyun while (name_hash > 0) {
4097*4882a593Smuzhiyun ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4098*4882a593Smuzhiyun &e_cpos, &num_clusters, el);
4099*4882a593Smuzhiyun if (ret) {
4100*4882a593Smuzhiyun mlog_errno(ret);
4101*4882a593Smuzhiyun break;
4102*4882a593Smuzhiyun }
4103*4882a593Smuzhiyun
4104*4882a593Smuzhiyun ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
4105*4882a593Smuzhiyun num_clusters, para);
4106*4882a593Smuzhiyun if (ret) {
4107*4882a593Smuzhiyun if (ret != -ERANGE)
4108*4882a593Smuzhiyun mlog_errno(ret);
4109*4882a593Smuzhiyun break;
4110*4882a593Smuzhiyun }
4111*4882a593Smuzhiyun
4112*4882a593Smuzhiyun if (e_cpos == 0)
4113*4882a593Smuzhiyun break;
4114*4882a593Smuzhiyun
4115*4882a593Smuzhiyun name_hash = e_cpos - 1;
4116*4882a593Smuzhiyun }
4117*4882a593Smuzhiyun
4118*4882a593Smuzhiyun return ret;
4119*4882a593Smuzhiyun
4120*4882a593Smuzhiyun }
4121*4882a593Smuzhiyun
ocfs2_list_xattr_tree_rec(struct inode * inode,struct buffer_head * root_bh,u64 blkno,u32 cpos,u32 len,void * para)4122*4882a593Smuzhiyun static int ocfs2_list_xattr_tree_rec(struct inode *inode,
4123*4882a593Smuzhiyun struct buffer_head *root_bh,
4124*4882a593Smuzhiyun u64 blkno, u32 cpos, u32 len, void *para)
4125*4882a593Smuzhiyun {
4126*4882a593Smuzhiyun return ocfs2_iterate_xattr_buckets(inode, blkno, len,
4127*4882a593Smuzhiyun ocfs2_list_xattr_bucket, para);
4128*4882a593Smuzhiyun }
4129*4882a593Smuzhiyun
ocfs2_xattr_tree_list_index_block(struct inode * inode,struct buffer_head * blk_bh,char * buffer,size_t buffer_size)4130*4882a593Smuzhiyun static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4131*4882a593Smuzhiyun struct buffer_head *blk_bh,
4132*4882a593Smuzhiyun char *buffer,
4133*4882a593Smuzhiyun size_t buffer_size)
4134*4882a593Smuzhiyun {
4135*4882a593Smuzhiyun int ret;
4136*4882a593Smuzhiyun struct ocfs2_xattr_tree_list xl = {
4137*4882a593Smuzhiyun .buffer = buffer,
4138*4882a593Smuzhiyun .buffer_size = buffer_size,
4139*4882a593Smuzhiyun .result = 0,
4140*4882a593Smuzhiyun };
4141*4882a593Smuzhiyun
4142*4882a593Smuzhiyun ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4143*4882a593Smuzhiyun ocfs2_list_xattr_tree_rec, &xl);
4144*4882a593Smuzhiyun if (ret) {
4145*4882a593Smuzhiyun mlog_errno(ret);
4146*4882a593Smuzhiyun goto out;
4147*4882a593Smuzhiyun }
4148*4882a593Smuzhiyun
4149*4882a593Smuzhiyun ret = xl.result;
4150*4882a593Smuzhiyun out:
4151*4882a593Smuzhiyun return ret;
4152*4882a593Smuzhiyun }
4153*4882a593Smuzhiyun
cmp_xe(const void * a,const void * b)4154*4882a593Smuzhiyun static int cmp_xe(const void *a, const void *b)
4155*4882a593Smuzhiyun {
4156*4882a593Smuzhiyun const struct ocfs2_xattr_entry *l = a, *r = b;
4157*4882a593Smuzhiyun u32 l_hash = le32_to_cpu(l->xe_name_hash);
4158*4882a593Smuzhiyun u32 r_hash = le32_to_cpu(r->xe_name_hash);
4159*4882a593Smuzhiyun
4160*4882a593Smuzhiyun if (l_hash > r_hash)
4161*4882a593Smuzhiyun return 1;
4162*4882a593Smuzhiyun if (l_hash < r_hash)
4163*4882a593Smuzhiyun return -1;
4164*4882a593Smuzhiyun return 0;
4165*4882a593Smuzhiyun }
4166*4882a593Smuzhiyun
swap_xe(void * a,void * b,int size)4167*4882a593Smuzhiyun static void swap_xe(void *a, void *b, int size)
4168*4882a593Smuzhiyun {
4169*4882a593Smuzhiyun struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4170*4882a593Smuzhiyun
4171*4882a593Smuzhiyun tmp = *l;
4172*4882a593Smuzhiyun memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4173*4882a593Smuzhiyun memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4174*4882a593Smuzhiyun }
4175*4882a593Smuzhiyun
4176*4882a593Smuzhiyun /*
4177*4882a593Smuzhiyun * When the ocfs2_xattr_block is filled up, new bucket will be created
4178*4882a593Smuzhiyun * and all the xattr entries will be moved to the new bucket.
4179*4882a593Smuzhiyun * The header goes at the start of the bucket, and the names+values are
4180*4882a593Smuzhiyun * filled from the end. This is why *target starts as the last buffer.
4181*4882a593Smuzhiyun * Note: we need to sort the entries since they are not saved in order
4182*4882a593Smuzhiyun * in the ocfs2_xattr_block.
4183*4882a593Smuzhiyun */
ocfs2_cp_xattr_block_to_bucket(struct inode * inode,struct buffer_head * xb_bh,struct ocfs2_xattr_bucket * bucket)4184*4882a593Smuzhiyun static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4185*4882a593Smuzhiyun struct buffer_head *xb_bh,
4186*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket)
4187*4882a593Smuzhiyun {
4188*4882a593Smuzhiyun int i, blocksize = inode->i_sb->s_blocksize;
4189*4882a593Smuzhiyun int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4190*4882a593Smuzhiyun u16 offset, size, off_change;
4191*4882a593Smuzhiyun struct ocfs2_xattr_entry *xe;
4192*4882a593Smuzhiyun struct ocfs2_xattr_block *xb =
4193*4882a593Smuzhiyun (struct ocfs2_xattr_block *)xb_bh->b_data;
4194*4882a593Smuzhiyun struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
4195*4882a593Smuzhiyun struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4196*4882a593Smuzhiyun u16 count = le16_to_cpu(xb_xh->xh_count);
4197*4882a593Smuzhiyun char *src = xb_bh->b_data;
4198*4882a593Smuzhiyun char *target = bucket_block(bucket, blks - 1);
4199*4882a593Smuzhiyun
4200*4882a593Smuzhiyun trace_ocfs2_cp_xattr_block_to_bucket_begin(
4201*4882a593Smuzhiyun (unsigned long long)xb_bh->b_blocknr,
4202*4882a593Smuzhiyun (unsigned long long)bucket_blkno(bucket));
4203*4882a593Smuzhiyun
4204*4882a593Smuzhiyun for (i = 0; i < blks; i++)
4205*4882a593Smuzhiyun memset(bucket_block(bucket, i), 0, blocksize);
4206*4882a593Smuzhiyun
4207*4882a593Smuzhiyun /*
4208*4882a593Smuzhiyun * Since the xe_name_offset is based on ocfs2_xattr_header,
4209*4882a593Smuzhiyun * there is a offset change corresponding to the change of
4210*4882a593Smuzhiyun * ocfs2_xattr_header's position.
4211*4882a593Smuzhiyun */
4212*4882a593Smuzhiyun off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4213*4882a593Smuzhiyun xe = &xb_xh->xh_entries[count - 1];
4214*4882a593Smuzhiyun offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4215*4882a593Smuzhiyun size = blocksize - offset;
4216*4882a593Smuzhiyun
4217*4882a593Smuzhiyun /* copy all the names and values. */
4218*4882a593Smuzhiyun memcpy(target + offset, src + offset, size);
4219*4882a593Smuzhiyun
4220*4882a593Smuzhiyun /* Init new header now. */
4221*4882a593Smuzhiyun xh->xh_count = xb_xh->xh_count;
4222*4882a593Smuzhiyun xh->xh_num_buckets = cpu_to_le16(1);
4223*4882a593Smuzhiyun xh->xh_name_value_len = cpu_to_le16(size);
4224*4882a593Smuzhiyun xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4225*4882a593Smuzhiyun
4226*4882a593Smuzhiyun /* copy all the entries. */
4227*4882a593Smuzhiyun target = bucket_block(bucket, 0);
4228*4882a593Smuzhiyun offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4229*4882a593Smuzhiyun size = count * sizeof(struct ocfs2_xattr_entry);
4230*4882a593Smuzhiyun memcpy(target + offset, (char *)xb_xh + offset, size);
4231*4882a593Smuzhiyun
4232*4882a593Smuzhiyun /* Change the xe offset for all the xe because of the move. */
4233*4882a593Smuzhiyun off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4234*4882a593Smuzhiyun offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4235*4882a593Smuzhiyun for (i = 0; i < count; i++)
4236*4882a593Smuzhiyun le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4237*4882a593Smuzhiyun
4238*4882a593Smuzhiyun trace_ocfs2_cp_xattr_block_to_bucket_end(offset, size, off_change);
4239*4882a593Smuzhiyun
4240*4882a593Smuzhiyun sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4241*4882a593Smuzhiyun cmp_xe, swap_xe);
4242*4882a593Smuzhiyun }
4243*4882a593Smuzhiyun
4244*4882a593Smuzhiyun /*
4245*4882a593Smuzhiyun * After we move xattr from block to index btree, we have to
4246*4882a593Smuzhiyun * update ocfs2_xattr_search to the new xe and base.
4247*4882a593Smuzhiyun *
4248*4882a593Smuzhiyun * When the entry is in xattr block, xattr_bh indicates the storage place.
4249*4882a593Smuzhiyun * While if the entry is in index b-tree, "bucket" indicates the
4250*4882a593Smuzhiyun * real place of the xattr.
4251*4882a593Smuzhiyun */
ocfs2_xattr_update_xattr_search(struct inode * inode,struct ocfs2_xattr_search * xs,struct buffer_head * old_bh)4252*4882a593Smuzhiyun static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4253*4882a593Smuzhiyun struct ocfs2_xattr_search *xs,
4254*4882a593Smuzhiyun struct buffer_head *old_bh)
4255*4882a593Smuzhiyun {
4256*4882a593Smuzhiyun char *buf = old_bh->b_data;
4257*4882a593Smuzhiyun struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4258*4882a593Smuzhiyun struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
4259*4882a593Smuzhiyun int i;
4260*4882a593Smuzhiyun
4261*4882a593Smuzhiyun xs->header = bucket_xh(xs->bucket);
4262*4882a593Smuzhiyun xs->base = bucket_block(xs->bucket, 0);
4263*4882a593Smuzhiyun xs->end = xs->base + inode->i_sb->s_blocksize;
4264*4882a593Smuzhiyun
4265*4882a593Smuzhiyun if (xs->not_found)
4266*4882a593Smuzhiyun return;
4267*4882a593Smuzhiyun
4268*4882a593Smuzhiyun i = xs->here - old_xh->xh_entries;
4269*4882a593Smuzhiyun xs->here = &xs->header->xh_entries[i];
4270*4882a593Smuzhiyun }
4271*4882a593Smuzhiyun
ocfs2_xattr_create_index_block(struct inode * inode,struct ocfs2_xattr_search * xs,struct ocfs2_xattr_set_ctxt * ctxt)4272*4882a593Smuzhiyun static int ocfs2_xattr_create_index_block(struct inode *inode,
4273*4882a593Smuzhiyun struct ocfs2_xattr_search *xs,
4274*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
4275*4882a593Smuzhiyun {
4276*4882a593Smuzhiyun int ret;
4277*4882a593Smuzhiyun u32 bit_off, len;
4278*4882a593Smuzhiyun u64 blkno;
4279*4882a593Smuzhiyun handle_t *handle = ctxt->handle;
4280*4882a593Smuzhiyun struct ocfs2_inode_info *oi = OCFS2_I(inode);
4281*4882a593Smuzhiyun struct buffer_head *xb_bh = xs->xattr_bh;
4282*4882a593Smuzhiyun struct ocfs2_xattr_block *xb =
4283*4882a593Smuzhiyun (struct ocfs2_xattr_block *)xb_bh->b_data;
4284*4882a593Smuzhiyun struct ocfs2_xattr_tree_root *xr;
4285*4882a593Smuzhiyun u16 xb_flags = le16_to_cpu(xb->xb_flags);
4286*4882a593Smuzhiyun
4287*4882a593Smuzhiyun trace_ocfs2_xattr_create_index_block_begin(
4288*4882a593Smuzhiyun (unsigned long long)xb_bh->b_blocknr);
4289*4882a593Smuzhiyun
4290*4882a593Smuzhiyun BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
4291*4882a593Smuzhiyun BUG_ON(!xs->bucket);
4292*4882a593Smuzhiyun
4293*4882a593Smuzhiyun /*
4294*4882a593Smuzhiyun * XXX:
4295*4882a593Smuzhiyun * We can use this lock for now, and maybe move to a dedicated mutex
4296*4882a593Smuzhiyun * if performance becomes a problem later.
4297*4882a593Smuzhiyun */
4298*4882a593Smuzhiyun down_write(&oi->ip_alloc_sem);
4299*4882a593Smuzhiyun
4300*4882a593Smuzhiyun ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
4301*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
4302*4882a593Smuzhiyun if (ret) {
4303*4882a593Smuzhiyun mlog_errno(ret);
4304*4882a593Smuzhiyun goto out;
4305*4882a593Smuzhiyun }
4306*4882a593Smuzhiyun
4307*4882a593Smuzhiyun ret = __ocfs2_claim_clusters(handle, ctxt->data_ac,
4308*4882a593Smuzhiyun 1, 1, &bit_off, &len);
4309*4882a593Smuzhiyun if (ret) {
4310*4882a593Smuzhiyun mlog_errno(ret);
4311*4882a593Smuzhiyun goto out;
4312*4882a593Smuzhiyun }
4313*4882a593Smuzhiyun
4314*4882a593Smuzhiyun /*
4315*4882a593Smuzhiyun * The bucket may spread in many blocks, and
4316*4882a593Smuzhiyun * we will only touch the 1st block and the last block
4317*4882a593Smuzhiyun * in the whole bucket(one for entry and one for data).
4318*4882a593Smuzhiyun */
4319*4882a593Smuzhiyun blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4320*4882a593Smuzhiyun
4321*4882a593Smuzhiyun trace_ocfs2_xattr_create_index_block((unsigned long long)blkno);
4322*4882a593Smuzhiyun
4323*4882a593Smuzhiyun ret = ocfs2_init_xattr_bucket(xs->bucket, blkno, 1);
4324*4882a593Smuzhiyun if (ret) {
4325*4882a593Smuzhiyun mlog_errno(ret);
4326*4882a593Smuzhiyun goto out;
4327*4882a593Smuzhiyun }
4328*4882a593Smuzhiyun
4329*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4330*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_CREATE);
4331*4882a593Smuzhiyun if (ret) {
4332*4882a593Smuzhiyun mlog_errno(ret);
4333*4882a593Smuzhiyun goto out;
4334*4882a593Smuzhiyun }
4335*4882a593Smuzhiyun
4336*4882a593Smuzhiyun ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4337*4882a593Smuzhiyun ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4338*4882a593Smuzhiyun
4339*4882a593Smuzhiyun ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4340*4882a593Smuzhiyun
4341*4882a593Smuzhiyun /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4342*4882a593Smuzhiyun memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4343*4882a593Smuzhiyun offsetof(struct ocfs2_xattr_block, xb_attrs));
4344*4882a593Smuzhiyun
4345*4882a593Smuzhiyun xr = &xb->xb_attrs.xb_root;
4346*4882a593Smuzhiyun xr->xt_clusters = cpu_to_le32(1);
4347*4882a593Smuzhiyun xr->xt_last_eb_blk = 0;
4348*4882a593Smuzhiyun xr->xt_list.l_tree_depth = 0;
4349*4882a593Smuzhiyun xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4350*4882a593Smuzhiyun xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4351*4882a593Smuzhiyun
4352*4882a593Smuzhiyun xr->xt_list.l_recs[0].e_cpos = 0;
4353*4882a593Smuzhiyun xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4354*4882a593Smuzhiyun xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4355*4882a593Smuzhiyun
4356*4882a593Smuzhiyun xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4357*4882a593Smuzhiyun
4358*4882a593Smuzhiyun ocfs2_journal_dirty(handle, xb_bh);
4359*4882a593Smuzhiyun
4360*4882a593Smuzhiyun out:
4361*4882a593Smuzhiyun up_write(&oi->ip_alloc_sem);
4362*4882a593Smuzhiyun
4363*4882a593Smuzhiyun return ret;
4364*4882a593Smuzhiyun }
4365*4882a593Smuzhiyun
cmp_xe_offset(const void * a,const void * b)4366*4882a593Smuzhiyun static int cmp_xe_offset(const void *a, const void *b)
4367*4882a593Smuzhiyun {
4368*4882a593Smuzhiyun const struct ocfs2_xattr_entry *l = a, *r = b;
4369*4882a593Smuzhiyun u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4370*4882a593Smuzhiyun u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4371*4882a593Smuzhiyun
4372*4882a593Smuzhiyun if (l_name_offset < r_name_offset)
4373*4882a593Smuzhiyun return 1;
4374*4882a593Smuzhiyun if (l_name_offset > r_name_offset)
4375*4882a593Smuzhiyun return -1;
4376*4882a593Smuzhiyun return 0;
4377*4882a593Smuzhiyun }
4378*4882a593Smuzhiyun
4379*4882a593Smuzhiyun /*
4380*4882a593Smuzhiyun * defrag a xattr bucket if we find that the bucket has some
4381*4882a593Smuzhiyun * holes beteen name/value pairs.
4382*4882a593Smuzhiyun * We will move all the name/value pairs to the end of the bucket
4383*4882a593Smuzhiyun * so that we can spare some space for insertion.
4384*4882a593Smuzhiyun */
ocfs2_defrag_xattr_bucket(struct inode * inode,handle_t * handle,struct ocfs2_xattr_bucket * bucket)4385*4882a593Smuzhiyun static int ocfs2_defrag_xattr_bucket(struct inode *inode,
4386*4882a593Smuzhiyun handle_t *handle,
4387*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket)
4388*4882a593Smuzhiyun {
4389*4882a593Smuzhiyun int ret, i;
4390*4882a593Smuzhiyun size_t end, offset, len;
4391*4882a593Smuzhiyun struct ocfs2_xattr_header *xh;
4392*4882a593Smuzhiyun char *entries, *buf, *bucket_buf = NULL;
4393*4882a593Smuzhiyun u64 blkno = bucket_blkno(bucket);
4394*4882a593Smuzhiyun u16 xh_free_start;
4395*4882a593Smuzhiyun size_t blocksize = inode->i_sb->s_blocksize;
4396*4882a593Smuzhiyun struct ocfs2_xattr_entry *xe;
4397*4882a593Smuzhiyun
4398*4882a593Smuzhiyun /*
4399*4882a593Smuzhiyun * In order to make the operation more efficient and generic,
4400*4882a593Smuzhiyun * we copy all the blocks into a contiguous memory and do the
4401*4882a593Smuzhiyun * defragment there, so if anything is error, we will not touch
4402*4882a593Smuzhiyun * the real block.
4403*4882a593Smuzhiyun */
4404*4882a593Smuzhiyun bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4405*4882a593Smuzhiyun if (!bucket_buf) {
4406*4882a593Smuzhiyun ret = -EIO;
4407*4882a593Smuzhiyun goto out;
4408*4882a593Smuzhiyun }
4409*4882a593Smuzhiyun
4410*4882a593Smuzhiyun buf = bucket_buf;
4411*4882a593Smuzhiyun for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4412*4882a593Smuzhiyun memcpy(buf, bucket_block(bucket, i), blocksize);
4413*4882a593Smuzhiyun
4414*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
4415*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
4416*4882a593Smuzhiyun if (ret < 0) {
4417*4882a593Smuzhiyun mlog_errno(ret);
4418*4882a593Smuzhiyun goto out;
4419*4882a593Smuzhiyun }
4420*4882a593Smuzhiyun
4421*4882a593Smuzhiyun xh = (struct ocfs2_xattr_header *)bucket_buf;
4422*4882a593Smuzhiyun entries = (char *)xh->xh_entries;
4423*4882a593Smuzhiyun xh_free_start = le16_to_cpu(xh->xh_free_start);
4424*4882a593Smuzhiyun
4425*4882a593Smuzhiyun trace_ocfs2_defrag_xattr_bucket(
4426*4882a593Smuzhiyun (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4427*4882a593Smuzhiyun xh_free_start, le16_to_cpu(xh->xh_name_value_len));
4428*4882a593Smuzhiyun
4429*4882a593Smuzhiyun /*
4430*4882a593Smuzhiyun * sort all the entries by their offset.
4431*4882a593Smuzhiyun * the largest will be the first, so that we can
4432*4882a593Smuzhiyun * move them to the end one by one.
4433*4882a593Smuzhiyun */
4434*4882a593Smuzhiyun sort(entries, le16_to_cpu(xh->xh_count),
4435*4882a593Smuzhiyun sizeof(struct ocfs2_xattr_entry),
4436*4882a593Smuzhiyun cmp_xe_offset, swap_xe);
4437*4882a593Smuzhiyun
4438*4882a593Smuzhiyun /* Move all name/values to the end of the bucket. */
4439*4882a593Smuzhiyun xe = xh->xh_entries;
4440*4882a593Smuzhiyun end = OCFS2_XATTR_BUCKET_SIZE;
4441*4882a593Smuzhiyun for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4442*4882a593Smuzhiyun offset = le16_to_cpu(xe->xe_name_offset);
4443*4882a593Smuzhiyun len = namevalue_size_xe(xe);
4444*4882a593Smuzhiyun
4445*4882a593Smuzhiyun /*
4446*4882a593Smuzhiyun * We must make sure that the name/value pair
4447*4882a593Smuzhiyun * exist in the same block. So adjust end to
4448*4882a593Smuzhiyun * the previous block end if needed.
4449*4882a593Smuzhiyun */
4450*4882a593Smuzhiyun if (((end - len) / blocksize !=
4451*4882a593Smuzhiyun (end - 1) / blocksize))
4452*4882a593Smuzhiyun end = end - end % blocksize;
4453*4882a593Smuzhiyun
4454*4882a593Smuzhiyun if (end > offset + len) {
4455*4882a593Smuzhiyun memmove(bucket_buf + end - len,
4456*4882a593Smuzhiyun bucket_buf + offset, len);
4457*4882a593Smuzhiyun xe->xe_name_offset = cpu_to_le16(end - len);
4458*4882a593Smuzhiyun }
4459*4882a593Smuzhiyun
4460*4882a593Smuzhiyun mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4461*4882a593Smuzhiyun "bucket %llu\n", (unsigned long long)blkno);
4462*4882a593Smuzhiyun
4463*4882a593Smuzhiyun end -= len;
4464*4882a593Smuzhiyun }
4465*4882a593Smuzhiyun
4466*4882a593Smuzhiyun mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4467*4882a593Smuzhiyun "bucket %llu\n", (unsigned long long)blkno);
4468*4882a593Smuzhiyun
4469*4882a593Smuzhiyun if (xh_free_start == end)
4470*4882a593Smuzhiyun goto out;
4471*4882a593Smuzhiyun
4472*4882a593Smuzhiyun memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4473*4882a593Smuzhiyun xh->xh_free_start = cpu_to_le16(end);
4474*4882a593Smuzhiyun
4475*4882a593Smuzhiyun /* sort the entries by their name_hash. */
4476*4882a593Smuzhiyun sort(entries, le16_to_cpu(xh->xh_count),
4477*4882a593Smuzhiyun sizeof(struct ocfs2_xattr_entry),
4478*4882a593Smuzhiyun cmp_xe, swap_xe);
4479*4882a593Smuzhiyun
4480*4882a593Smuzhiyun buf = bucket_buf;
4481*4882a593Smuzhiyun for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4482*4882a593Smuzhiyun memcpy(bucket_block(bucket, i), buf, blocksize);
4483*4882a593Smuzhiyun ocfs2_xattr_bucket_journal_dirty(handle, bucket);
4484*4882a593Smuzhiyun
4485*4882a593Smuzhiyun out:
4486*4882a593Smuzhiyun kfree(bucket_buf);
4487*4882a593Smuzhiyun return ret;
4488*4882a593Smuzhiyun }
4489*4882a593Smuzhiyun
4490*4882a593Smuzhiyun /*
4491*4882a593Smuzhiyun * prev_blkno points to the start of an existing extent. new_blkno
4492*4882a593Smuzhiyun * points to a newly allocated extent. Because we know each of our
4493*4882a593Smuzhiyun * clusters contains more than bucket, we can easily split one cluster
4494*4882a593Smuzhiyun * at a bucket boundary. So we take the last cluster of the existing
4495*4882a593Smuzhiyun * extent and split it down the middle. We move the last half of the
4496*4882a593Smuzhiyun * buckets in the last cluster of the existing extent over to the new
4497*4882a593Smuzhiyun * extent.
4498*4882a593Smuzhiyun *
4499*4882a593Smuzhiyun * first_bh is the buffer at prev_blkno so we can update the existing
4500*4882a593Smuzhiyun * extent's bucket count. header_bh is the bucket were we were hoping
4501*4882a593Smuzhiyun * to insert our xattr. If the bucket move places the target in the new
4502*4882a593Smuzhiyun * extent, we'll update first_bh and header_bh after modifying the old
4503*4882a593Smuzhiyun * extent.
4504*4882a593Smuzhiyun *
4505*4882a593Smuzhiyun * first_hash will be set as the 1st xe's name_hash in the new extent.
4506*4882a593Smuzhiyun */
ocfs2_mv_xattr_bucket_cross_cluster(struct inode * inode,handle_t * handle,struct ocfs2_xattr_bucket * first,struct ocfs2_xattr_bucket * target,u64 new_blkno,u32 num_clusters,u32 * first_hash)4507*4882a593Smuzhiyun static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4508*4882a593Smuzhiyun handle_t *handle,
4509*4882a593Smuzhiyun struct ocfs2_xattr_bucket *first,
4510*4882a593Smuzhiyun struct ocfs2_xattr_bucket *target,
4511*4882a593Smuzhiyun u64 new_blkno,
4512*4882a593Smuzhiyun u32 num_clusters,
4513*4882a593Smuzhiyun u32 *first_hash)
4514*4882a593Smuzhiyun {
4515*4882a593Smuzhiyun int ret;
4516*4882a593Smuzhiyun struct super_block *sb = inode->i_sb;
4517*4882a593Smuzhiyun int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4518*4882a593Smuzhiyun int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
4519*4882a593Smuzhiyun int to_move = num_buckets / 2;
4520*4882a593Smuzhiyun u64 src_blkno;
4521*4882a593Smuzhiyun u64 last_cluster_blkno = bucket_blkno(first) +
4522*4882a593Smuzhiyun ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
4523*4882a593Smuzhiyun
4524*4882a593Smuzhiyun BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4525*4882a593Smuzhiyun BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
4526*4882a593Smuzhiyun
4527*4882a593Smuzhiyun trace_ocfs2_mv_xattr_bucket_cross_cluster(
4528*4882a593Smuzhiyun (unsigned long long)last_cluster_blkno,
4529*4882a593Smuzhiyun (unsigned long long)new_blkno);
4530*4882a593Smuzhiyun
4531*4882a593Smuzhiyun ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
4532*4882a593Smuzhiyun last_cluster_blkno, new_blkno,
4533*4882a593Smuzhiyun to_move, first_hash);
4534*4882a593Smuzhiyun if (ret) {
4535*4882a593Smuzhiyun mlog_errno(ret);
4536*4882a593Smuzhiyun goto out;
4537*4882a593Smuzhiyun }
4538*4882a593Smuzhiyun
4539*4882a593Smuzhiyun /* This is the first bucket that got moved */
4540*4882a593Smuzhiyun src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4541*4882a593Smuzhiyun
4542*4882a593Smuzhiyun /*
4543*4882a593Smuzhiyun * If the target bucket was part of the moved buckets, we need to
4544*4882a593Smuzhiyun * update first and target.
4545*4882a593Smuzhiyun */
4546*4882a593Smuzhiyun if (bucket_blkno(target) >= src_blkno) {
4547*4882a593Smuzhiyun /* Find the block for the new target bucket */
4548*4882a593Smuzhiyun src_blkno = new_blkno +
4549*4882a593Smuzhiyun (bucket_blkno(target) - src_blkno);
4550*4882a593Smuzhiyun
4551*4882a593Smuzhiyun ocfs2_xattr_bucket_relse(first);
4552*4882a593Smuzhiyun ocfs2_xattr_bucket_relse(target);
4553*4882a593Smuzhiyun
4554*4882a593Smuzhiyun /*
4555*4882a593Smuzhiyun * These shouldn't fail - the buffers are in the
4556*4882a593Smuzhiyun * journal from ocfs2_cp_xattr_bucket().
4557*4882a593Smuzhiyun */
4558*4882a593Smuzhiyun ret = ocfs2_read_xattr_bucket(first, new_blkno);
4559*4882a593Smuzhiyun if (ret) {
4560*4882a593Smuzhiyun mlog_errno(ret);
4561*4882a593Smuzhiyun goto out;
4562*4882a593Smuzhiyun }
4563*4882a593Smuzhiyun ret = ocfs2_read_xattr_bucket(target, src_blkno);
4564*4882a593Smuzhiyun if (ret)
4565*4882a593Smuzhiyun mlog_errno(ret);
4566*4882a593Smuzhiyun
4567*4882a593Smuzhiyun }
4568*4882a593Smuzhiyun
4569*4882a593Smuzhiyun out:
4570*4882a593Smuzhiyun return ret;
4571*4882a593Smuzhiyun }
4572*4882a593Smuzhiyun
4573*4882a593Smuzhiyun /*
4574*4882a593Smuzhiyun * Find the suitable pos when we divide a bucket into 2.
4575*4882a593Smuzhiyun * We have to make sure the xattrs with the same hash value exist
4576*4882a593Smuzhiyun * in the same bucket.
4577*4882a593Smuzhiyun *
4578*4882a593Smuzhiyun * If this ocfs2_xattr_header covers more than one hash value, find a
4579*4882a593Smuzhiyun * place where the hash value changes. Try to find the most even split.
4580*4882a593Smuzhiyun * The most common case is that all entries have different hash values,
4581*4882a593Smuzhiyun * and the first check we make will find a place to split.
4582*4882a593Smuzhiyun */
ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header * xh)4583*4882a593Smuzhiyun static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4584*4882a593Smuzhiyun {
4585*4882a593Smuzhiyun struct ocfs2_xattr_entry *entries = xh->xh_entries;
4586*4882a593Smuzhiyun int count = le16_to_cpu(xh->xh_count);
4587*4882a593Smuzhiyun int delta, middle = count / 2;
4588*4882a593Smuzhiyun
4589*4882a593Smuzhiyun /*
4590*4882a593Smuzhiyun * We start at the middle. Each step gets farther away in both
4591*4882a593Smuzhiyun * directions. We therefore hit the change in hash value
4592*4882a593Smuzhiyun * nearest to the middle. Note that this loop does not execute for
4593*4882a593Smuzhiyun * count < 2.
4594*4882a593Smuzhiyun */
4595*4882a593Smuzhiyun for (delta = 0; delta < middle; delta++) {
4596*4882a593Smuzhiyun /* Let's check delta earlier than middle */
4597*4882a593Smuzhiyun if (cmp_xe(&entries[middle - delta - 1],
4598*4882a593Smuzhiyun &entries[middle - delta]))
4599*4882a593Smuzhiyun return middle - delta;
4600*4882a593Smuzhiyun
4601*4882a593Smuzhiyun /* For even counts, don't walk off the end */
4602*4882a593Smuzhiyun if ((middle + delta + 1) == count)
4603*4882a593Smuzhiyun continue;
4604*4882a593Smuzhiyun
4605*4882a593Smuzhiyun /* Now try delta past middle */
4606*4882a593Smuzhiyun if (cmp_xe(&entries[middle + delta],
4607*4882a593Smuzhiyun &entries[middle + delta + 1]))
4608*4882a593Smuzhiyun return middle + delta + 1;
4609*4882a593Smuzhiyun }
4610*4882a593Smuzhiyun
4611*4882a593Smuzhiyun /* Every entry had the same hash */
4612*4882a593Smuzhiyun return count;
4613*4882a593Smuzhiyun }
4614*4882a593Smuzhiyun
4615*4882a593Smuzhiyun /*
4616*4882a593Smuzhiyun * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4617*4882a593Smuzhiyun * first_hash will record the 1st hash of the new bucket.
4618*4882a593Smuzhiyun *
4619*4882a593Smuzhiyun * Normally half of the xattrs will be moved. But we have to make
4620*4882a593Smuzhiyun * sure that the xattrs with the same hash value are stored in the
4621*4882a593Smuzhiyun * same bucket. If all the xattrs in this bucket have the same hash
4622*4882a593Smuzhiyun * value, the new bucket will be initialized as an empty one and the
4623*4882a593Smuzhiyun * first_hash will be initialized as (hash_value+1).
4624*4882a593Smuzhiyun */
ocfs2_divide_xattr_bucket(struct inode * inode,handle_t * handle,u64 blk,u64 new_blk,u32 * first_hash,int new_bucket_head)4625*4882a593Smuzhiyun static int ocfs2_divide_xattr_bucket(struct inode *inode,
4626*4882a593Smuzhiyun handle_t *handle,
4627*4882a593Smuzhiyun u64 blk,
4628*4882a593Smuzhiyun u64 new_blk,
4629*4882a593Smuzhiyun u32 *first_hash,
4630*4882a593Smuzhiyun int new_bucket_head)
4631*4882a593Smuzhiyun {
4632*4882a593Smuzhiyun int ret, i;
4633*4882a593Smuzhiyun int count, start, len, name_value_len = 0, name_offset = 0;
4634*4882a593Smuzhiyun struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4635*4882a593Smuzhiyun struct ocfs2_xattr_header *xh;
4636*4882a593Smuzhiyun struct ocfs2_xattr_entry *xe;
4637*4882a593Smuzhiyun int blocksize = inode->i_sb->s_blocksize;
4638*4882a593Smuzhiyun
4639*4882a593Smuzhiyun trace_ocfs2_divide_xattr_bucket_begin((unsigned long long)blk,
4640*4882a593Smuzhiyun (unsigned long long)new_blk);
4641*4882a593Smuzhiyun
4642*4882a593Smuzhiyun s_bucket = ocfs2_xattr_bucket_new(inode);
4643*4882a593Smuzhiyun t_bucket = ocfs2_xattr_bucket_new(inode);
4644*4882a593Smuzhiyun if (!s_bucket || !t_bucket) {
4645*4882a593Smuzhiyun ret = -ENOMEM;
4646*4882a593Smuzhiyun mlog_errno(ret);
4647*4882a593Smuzhiyun goto out;
4648*4882a593Smuzhiyun }
4649*4882a593Smuzhiyun
4650*4882a593Smuzhiyun ret = ocfs2_read_xattr_bucket(s_bucket, blk);
4651*4882a593Smuzhiyun if (ret) {
4652*4882a593Smuzhiyun mlog_errno(ret);
4653*4882a593Smuzhiyun goto out;
4654*4882a593Smuzhiyun }
4655*4882a593Smuzhiyun
4656*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
4657*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
4658*4882a593Smuzhiyun if (ret) {
4659*4882a593Smuzhiyun mlog_errno(ret);
4660*4882a593Smuzhiyun goto out;
4661*4882a593Smuzhiyun }
4662*4882a593Smuzhiyun
4663*4882a593Smuzhiyun /*
4664*4882a593Smuzhiyun * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4665*4882a593Smuzhiyun * there's no need to read it.
4666*4882a593Smuzhiyun */
4667*4882a593Smuzhiyun ret = ocfs2_init_xattr_bucket(t_bucket, new_blk, new_bucket_head);
4668*4882a593Smuzhiyun if (ret) {
4669*4882a593Smuzhiyun mlog_errno(ret);
4670*4882a593Smuzhiyun goto out;
4671*4882a593Smuzhiyun }
4672*4882a593Smuzhiyun
4673*4882a593Smuzhiyun /*
4674*4882a593Smuzhiyun * Hey, if we're overwriting t_bucket, what difference does
4675*4882a593Smuzhiyun * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4676*4882a593Smuzhiyun * same part of ocfs2_cp_xattr_bucket().
4677*4882a593Smuzhiyun */
4678*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4679*4882a593Smuzhiyun new_bucket_head ?
4680*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_CREATE :
4681*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
4682*4882a593Smuzhiyun if (ret) {
4683*4882a593Smuzhiyun mlog_errno(ret);
4684*4882a593Smuzhiyun goto out;
4685*4882a593Smuzhiyun }
4686*4882a593Smuzhiyun
4687*4882a593Smuzhiyun xh = bucket_xh(s_bucket);
4688*4882a593Smuzhiyun count = le16_to_cpu(xh->xh_count);
4689*4882a593Smuzhiyun start = ocfs2_xattr_find_divide_pos(xh);
4690*4882a593Smuzhiyun
4691*4882a593Smuzhiyun if (start == count) {
4692*4882a593Smuzhiyun xe = &xh->xh_entries[start-1];
4693*4882a593Smuzhiyun
4694*4882a593Smuzhiyun /*
4695*4882a593Smuzhiyun * initialized a new empty bucket here.
4696*4882a593Smuzhiyun * The hash value is set as one larger than
4697*4882a593Smuzhiyun * that of the last entry in the previous bucket.
4698*4882a593Smuzhiyun */
4699*4882a593Smuzhiyun for (i = 0; i < t_bucket->bu_blocks; i++)
4700*4882a593Smuzhiyun memset(bucket_block(t_bucket, i), 0, blocksize);
4701*4882a593Smuzhiyun
4702*4882a593Smuzhiyun xh = bucket_xh(t_bucket);
4703*4882a593Smuzhiyun xh->xh_free_start = cpu_to_le16(blocksize);
4704*4882a593Smuzhiyun xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4705*4882a593Smuzhiyun le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4706*4882a593Smuzhiyun
4707*4882a593Smuzhiyun goto set_num_buckets;
4708*4882a593Smuzhiyun }
4709*4882a593Smuzhiyun
4710*4882a593Smuzhiyun /* copy the whole bucket to the new first. */
4711*4882a593Smuzhiyun ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4712*4882a593Smuzhiyun
4713*4882a593Smuzhiyun /* update the new bucket. */
4714*4882a593Smuzhiyun xh = bucket_xh(t_bucket);
4715*4882a593Smuzhiyun
4716*4882a593Smuzhiyun /*
4717*4882a593Smuzhiyun * Calculate the total name/value len and xh_free_start for
4718*4882a593Smuzhiyun * the old bucket first.
4719*4882a593Smuzhiyun */
4720*4882a593Smuzhiyun name_offset = OCFS2_XATTR_BUCKET_SIZE;
4721*4882a593Smuzhiyun name_value_len = 0;
4722*4882a593Smuzhiyun for (i = 0; i < start; i++) {
4723*4882a593Smuzhiyun xe = &xh->xh_entries[i];
4724*4882a593Smuzhiyun name_value_len += namevalue_size_xe(xe);
4725*4882a593Smuzhiyun if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4726*4882a593Smuzhiyun name_offset = le16_to_cpu(xe->xe_name_offset);
4727*4882a593Smuzhiyun }
4728*4882a593Smuzhiyun
4729*4882a593Smuzhiyun /*
4730*4882a593Smuzhiyun * Now begin the modification to the new bucket.
4731*4882a593Smuzhiyun *
4732*4882a593Smuzhiyun * In the new bucket, We just move the xattr entry to the beginning
4733*4882a593Smuzhiyun * and don't touch the name/value. So there will be some holes in the
4734*4882a593Smuzhiyun * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4735*4882a593Smuzhiyun * called.
4736*4882a593Smuzhiyun */
4737*4882a593Smuzhiyun xe = &xh->xh_entries[start];
4738*4882a593Smuzhiyun len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4739*4882a593Smuzhiyun trace_ocfs2_divide_xattr_bucket_move(len,
4740*4882a593Smuzhiyun (int)((char *)xe - (char *)xh),
4741*4882a593Smuzhiyun (int)((char *)xh->xh_entries - (char *)xh));
4742*4882a593Smuzhiyun memmove((char *)xh->xh_entries, (char *)xe, len);
4743*4882a593Smuzhiyun xe = &xh->xh_entries[count - start];
4744*4882a593Smuzhiyun len = sizeof(struct ocfs2_xattr_entry) * start;
4745*4882a593Smuzhiyun memset((char *)xe, 0, len);
4746*4882a593Smuzhiyun
4747*4882a593Smuzhiyun le16_add_cpu(&xh->xh_count, -start);
4748*4882a593Smuzhiyun le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4749*4882a593Smuzhiyun
4750*4882a593Smuzhiyun /* Calculate xh_free_start for the new bucket. */
4751*4882a593Smuzhiyun xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4752*4882a593Smuzhiyun for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4753*4882a593Smuzhiyun xe = &xh->xh_entries[i];
4754*4882a593Smuzhiyun if (le16_to_cpu(xe->xe_name_offset) <
4755*4882a593Smuzhiyun le16_to_cpu(xh->xh_free_start))
4756*4882a593Smuzhiyun xh->xh_free_start = xe->xe_name_offset;
4757*4882a593Smuzhiyun }
4758*4882a593Smuzhiyun
4759*4882a593Smuzhiyun set_num_buckets:
4760*4882a593Smuzhiyun /* set xh->xh_num_buckets for the new xh. */
4761*4882a593Smuzhiyun if (new_bucket_head)
4762*4882a593Smuzhiyun xh->xh_num_buckets = cpu_to_le16(1);
4763*4882a593Smuzhiyun else
4764*4882a593Smuzhiyun xh->xh_num_buckets = 0;
4765*4882a593Smuzhiyun
4766*4882a593Smuzhiyun ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4767*4882a593Smuzhiyun
4768*4882a593Smuzhiyun /* store the first_hash of the new bucket. */
4769*4882a593Smuzhiyun if (first_hash)
4770*4882a593Smuzhiyun *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4771*4882a593Smuzhiyun
4772*4882a593Smuzhiyun /*
4773*4882a593Smuzhiyun * Now only update the 1st block of the old bucket. If we
4774*4882a593Smuzhiyun * just added a new empty bucket, there is no need to modify
4775*4882a593Smuzhiyun * it.
4776*4882a593Smuzhiyun */
4777*4882a593Smuzhiyun if (start == count)
4778*4882a593Smuzhiyun goto out;
4779*4882a593Smuzhiyun
4780*4882a593Smuzhiyun xh = bucket_xh(s_bucket);
4781*4882a593Smuzhiyun memset(&xh->xh_entries[start], 0,
4782*4882a593Smuzhiyun sizeof(struct ocfs2_xattr_entry) * (count - start));
4783*4882a593Smuzhiyun xh->xh_count = cpu_to_le16(start);
4784*4882a593Smuzhiyun xh->xh_free_start = cpu_to_le16(name_offset);
4785*4882a593Smuzhiyun xh->xh_name_value_len = cpu_to_le16(name_value_len);
4786*4882a593Smuzhiyun
4787*4882a593Smuzhiyun ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
4788*4882a593Smuzhiyun
4789*4882a593Smuzhiyun out:
4790*4882a593Smuzhiyun ocfs2_xattr_bucket_free(s_bucket);
4791*4882a593Smuzhiyun ocfs2_xattr_bucket_free(t_bucket);
4792*4882a593Smuzhiyun
4793*4882a593Smuzhiyun return ret;
4794*4882a593Smuzhiyun }
4795*4882a593Smuzhiyun
4796*4882a593Smuzhiyun /*
4797*4882a593Smuzhiyun * Copy xattr from one bucket to another bucket.
4798*4882a593Smuzhiyun *
4799*4882a593Smuzhiyun * The caller must make sure that the journal transaction
4800*4882a593Smuzhiyun * has enough space for journaling.
4801*4882a593Smuzhiyun */
ocfs2_cp_xattr_bucket(struct inode * inode,handle_t * handle,u64 s_blkno,u64 t_blkno,int t_is_new)4802*4882a593Smuzhiyun static int ocfs2_cp_xattr_bucket(struct inode *inode,
4803*4882a593Smuzhiyun handle_t *handle,
4804*4882a593Smuzhiyun u64 s_blkno,
4805*4882a593Smuzhiyun u64 t_blkno,
4806*4882a593Smuzhiyun int t_is_new)
4807*4882a593Smuzhiyun {
4808*4882a593Smuzhiyun int ret;
4809*4882a593Smuzhiyun struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4810*4882a593Smuzhiyun
4811*4882a593Smuzhiyun BUG_ON(s_blkno == t_blkno);
4812*4882a593Smuzhiyun
4813*4882a593Smuzhiyun trace_ocfs2_cp_xattr_bucket((unsigned long long)s_blkno,
4814*4882a593Smuzhiyun (unsigned long long)t_blkno,
4815*4882a593Smuzhiyun t_is_new);
4816*4882a593Smuzhiyun
4817*4882a593Smuzhiyun s_bucket = ocfs2_xattr_bucket_new(inode);
4818*4882a593Smuzhiyun t_bucket = ocfs2_xattr_bucket_new(inode);
4819*4882a593Smuzhiyun if (!s_bucket || !t_bucket) {
4820*4882a593Smuzhiyun ret = -ENOMEM;
4821*4882a593Smuzhiyun mlog_errno(ret);
4822*4882a593Smuzhiyun goto out;
4823*4882a593Smuzhiyun }
4824*4882a593Smuzhiyun
4825*4882a593Smuzhiyun ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
4826*4882a593Smuzhiyun if (ret)
4827*4882a593Smuzhiyun goto out;
4828*4882a593Smuzhiyun
4829*4882a593Smuzhiyun /*
4830*4882a593Smuzhiyun * Even if !t_is_new, we're overwriting t_bucket. Thus,
4831*4882a593Smuzhiyun * there's no need to read it.
4832*4882a593Smuzhiyun */
4833*4882a593Smuzhiyun ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno, t_is_new);
4834*4882a593Smuzhiyun if (ret)
4835*4882a593Smuzhiyun goto out;
4836*4882a593Smuzhiyun
4837*4882a593Smuzhiyun /*
4838*4882a593Smuzhiyun * Hey, if we're overwriting t_bucket, what difference does
4839*4882a593Smuzhiyun * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
4840*4882a593Smuzhiyun * cluster to fill, we came here from
4841*4882a593Smuzhiyun * ocfs2_mv_xattr_buckets(), and it is really new -
4842*4882a593Smuzhiyun * ACCESS_CREATE is required. But we also might have moved data
4843*4882a593Smuzhiyun * out of t_bucket before extending back into it.
4844*4882a593Smuzhiyun * ocfs2_add_new_xattr_bucket() can do this - its call to
4845*4882a593Smuzhiyun * ocfs2_add_new_xattr_cluster() may have created a new extent
4846*4882a593Smuzhiyun * and copied out the end of the old extent. Then it re-extends
4847*4882a593Smuzhiyun * the old extent back to create space for new xattrs. That's
4848*4882a593Smuzhiyun * how we get here, and the bucket isn't really new.
4849*4882a593Smuzhiyun */
4850*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4851*4882a593Smuzhiyun t_is_new ?
4852*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_CREATE :
4853*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
4854*4882a593Smuzhiyun if (ret)
4855*4882a593Smuzhiyun goto out;
4856*4882a593Smuzhiyun
4857*4882a593Smuzhiyun ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4858*4882a593Smuzhiyun ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4859*4882a593Smuzhiyun
4860*4882a593Smuzhiyun out:
4861*4882a593Smuzhiyun ocfs2_xattr_bucket_free(t_bucket);
4862*4882a593Smuzhiyun ocfs2_xattr_bucket_free(s_bucket);
4863*4882a593Smuzhiyun
4864*4882a593Smuzhiyun return ret;
4865*4882a593Smuzhiyun }
4866*4882a593Smuzhiyun
4867*4882a593Smuzhiyun /*
4868*4882a593Smuzhiyun * src_blk points to the start of an existing extent. last_blk points to
4869*4882a593Smuzhiyun * last cluster in that extent. to_blk points to a newly allocated
4870*4882a593Smuzhiyun * extent. We copy the buckets from the cluster at last_blk to the new
4871*4882a593Smuzhiyun * extent. If start_bucket is non-zero, we skip that many buckets before
4872*4882a593Smuzhiyun * we start copying. The new extent's xh_num_buckets gets set to the
4873*4882a593Smuzhiyun * number of buckets we copied. The old extent's xh_num_buckets shrinks
4874*4882a593Smuzhiyun * by the same amount.
4875*4882a593Smuzhiyun */
ocfs2_mv_xattr_buckets(struct inode * inode,handle_t * handle,u64 src_blk,u64 last_blk,u64 to_blk,unsigned int start_bucket,u32 * first_hash)4876*4882a593Smuzhiyun static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4877*4882a593Smuzhiyun u64 src_blk, u64 last_blk, u64 to_blk,
4878*4882a593Smuzhiyun unsigned int start_bucket,
4879*4882a593Smuzhiyun u32 *first_hash)
4880*4882a593Smuzhiyun {
4881*4882a593Smuzhiyun int i, ret, credits;
4882*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4883*4882a593Smuzhiyun int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4884*4882a593Smuzhiyun int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
4885*4882a593Smuzhiyun struct ocfs2_xattr_bucket *old_first, *new_first;
4886*4882a593Smuzhiyun
4887*4882a593Smuzhiyun trace_ocfs2_mv_xattr_buckets((unsigned long long)last_blk,
4888*4882a593Smuzhiyun (unsigned long long)to_blk);
4889*4882a593Smuzhiyun
4890*4882a593Smuzhiyun BUG_ON(start_bucket >= num_buckets);
4891*4882a593Smuzhiyun if (start_bucket) {
4892*4882a593Smuzhiyun num_buckets -= start_bucket;
4893*4882a593Smuzhiyun last_blk += (start_bucket * blks_per_bucket);
4894*4882a593Smuzhiyun }
4895*4882a593Smuzhiyun
4896*4882a593Smuzhiyun /* The first bucket of the original extent */
4897*4882a593Smuzhiyun old_first = ocfs2_xattr_bucket_new(inode);
4898*4882a593Smuzhiyun /* The first bucket of the new extent */
4899*4882a593Smuzhiyun new_first = ocfs2_xattr_bucket_new(inode);
4900*4882a593Smuzhiyun if (!old_first || !new_first) {
4901*4882a593Smuzhiyun ret = -ENOMEM;
4902*4882a593Smuzhiyun mlog_errno(ret);
4903*4882a593Smuzhiyun goto out;
4904*4882a593Smuzhiyun }
4905*4882a593Smuzhiyun
4906*4882a593Smuzhiyun ret = ocfs2_read_xattr_bucket(old_first, src_blk);
4907*4882a593Smuzhiyun if (ret) {
4908*4882a593Smuzhiyun mlog_errno(ret);
4909*4882a593Smuzhiyun goto out;
4910*4882a593Smuzhiyun }
4911*4882a593Smuzhiyun
4912*4882a593Smuzhiyun /*
4913*4882a593Smuzhiyun * We need to update the first bucket of the old extent and all
4914*4882a593Smuzhiyun * the buckets going to the new extent.
4915*4882a593Smuzhiyun */
4916*4882a593Smuzhiyun credits = ((num_buckets + 1) * blks_per_bucket);
4917*4882a593Smuzhiyun ret = ocfs2_extend_trans(handle, credits);
4918*4882a593Smuzhiyun if (ret) {
4919*4882a593Smuzhiyun mlog_errno(ret);
4920*4882a593Smuzhiyun goto out;
4921*4882a593Smuzhiyun }
4922*4882a593Smuzhiyun
4923*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4924*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
4925*4882a593Smuzhiyun if (ret) {
4926*4882a593Smuzhiyun mlog_errno(ret);
4927*4882a593Smuzhiyun goto out;
4928*4882a593Smuzhiyun }
4929*4882a593Smuzhiyun
4930*4882a593Smuzhiyun for (i = 0; i < num_buckets; i++) {
4931*4882a593Smuzhiyun ret = ocfs2_cp_xattr_bucket(inode, handle,
4932*4882a593Smuzhiyun last_blk + (i * blks_per_bucket),
4933*4882a593Smuzhiyun to_blk + (i * blks_per_bucket),
4934*4882a593Smuzhiyun 1);
4935*4882a593Smuzhiyun if (ret) {
4936*4882a593Smuzhiyun mlog_errno(ret);
4937*4882a593Smuzhiyun goto out;
4938*4882a593Smuzhiyun }
4939*4882a593Smuzhiyun }
4940*4882a593Smuzhiyun
4941*4882a593Smuzhiyun /*
4942*4882a593Smuzhiyun * Get the new bucket ready before we dirty anything
4943*4882a593Smuzhiyun * (This actually shouldn't fail, because we already dirtied
4944*4882a593Smuzhiyun * it once in ocfs2_cp_xattr_bucket()).
4945*4882a593Smuzhiyun */
4946*4882a593Smuzhiyun ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4947*4882a593Smuzhiyun if (ret) {
4948*4882a593Smuzhiyun mlog_errno(ret);
4949*4882a593Smuzhiyun goto out;
4950*4882a593Smuzhiyun }
4951*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4952*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
4953*4882a593Smuzhiyun if (ret) {
4954*4882a593Smuzhiyun mlog_errno(ret);
4955*4882a593Smuzhiyun goto out;
4956*4882a593Smuzhiyun }
4957*4882a593Smuzhiyun
4958*4882a593Smuzhiyun /* Now update the headers */
4959*4882a593Smuzhiyun le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4960*4882a593Smuzhiyun ocfs2_xattr_bucket_journal_dirty(handle, old_first);
4961*4882a593Smuzhiyun
4962*4882a593Smuzhiyun bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4963*4882a593Smuzhiyun ocfs2_xattr_bucket_journal_dirty(handle, new_first);
4964*4882a593Smuzhiyun
4965*4882a593Smuzhiyun if (first_hash)
4966*4882a593Smuzhiyun *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4967*4882a593Smuzhiyun
4968*4882a593Smuzhiyun out:
4969*4882a593Smuzhiyun ocfs2_xattr_bucket_free(new_first);
4970*4882a593Smuzhiyun ocfs2_xattr_bucket_free(old_first);
4971*4882a593Smuzhiyun return ret;
4972*4882a593Smuzhiyun }
4973*4882a593Smuzhiyun
4974*4882a593Smuzhiyun /*
4975*4882a593Smuzhiyun * Move some xattrs in this cluster to the new cluster.
4976*4882a593Smuzhiyun * This function should only be called when bucket size == cluster size.
4977*4882a593Smuzhiyun * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4978*4882a593Smuzhiyun */
ocfs2_divide_xattr_cluster(struct inode * inode,handle_t * handle,u64 prev_blk,u64 new_blk,u32 * first_hash)4979*4882a593Smuzhiyun static int ocfs2_divide_xattr_cluster(struct inode *inode,
4980*4882a593Smuzhiyun handle_t *handle,
4981*4882a593Smuzhiyun u64 prev_blk,
4982*4882a593Smuzhiyun u64 new_blk,
4983*4882a593Smuzhiyun u32 *first_hash)
4984*4882a593Smuzhiyun {
4985*4882a593Smuzhiyun u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4986*4882a593Smuzhiyun int ret, credits = 2 * blk_per_bucket;
4987*4882a593Smuzhiyun
4988*4882a593Smuzhiyun BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4989*4882a593Smuzhiyun
4990*4882a593Smuzhiyun ret = ocfs2_extend_trans(handle, credits);
4991*4882a593Smuzhiyun if (ret) {
4992*4882a593Smuzhiyun mlog_errno(ret);
4993*4882a593Smuzhiyun return ret;
4994*4882a593Smuzhiyun }
4995*4882a593Smuzhiyun
4996*4882a593Smuzhiyun /* Move half of the xattr in start_blk to the next bucket. */
4997*4882a593Smuzhiyun return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4998*4882a593Smuzhiyun new_blk, first_hash, 1);
4999*4882a593Smuzhiyun }
5000*4882a593Smuzhiyun
5001*4882a593Smuzhiyun /*
5002*4882a593Smuzhiyun * Move some xattrs from the old cluster to the new one since they are not
5003*4882a593Smuzhiyun * contiguous in ocfs2 xattr tree.
5004*4882a593Smuzhiyun *
5005*4882a593Smuzhiyun * new_blk starts a new separate cluster, and we will move some xattrs from
5006*4882a593Smuzhiyun * prev_blk to it. v_start will be set as the first name hash value in this
5007*4882a593Smuzhiyun * new cluster so that it can be used as e_cpos during tree insertion and
5008*4882a593Smuzhiyun * don't collide with our original b-tree operations. first_bh and header_bh
5009*4882a593Smuzhiyun * will also be updated since they will be used in ocfs2_extend_xattr_bucket
5010*4882a593Smuzhiyun * to extend the insert bucket.
5011*4882a593Smuzhiyun *
5012*4882a593Smuzhiyun * The problem is how much xattr should we move to the new one and when should
5013*4882a593Smuzhiyun * we update first_bh and header_bh?
5014*4882a593Smuzhiyun * 1. If cluster size > bucket size, that means the previous cluster has more
5015*4882a593Smuzhiyun * than 1 bucket, so just move half nums of bucket into the new cluster and
5016*4882a593Smuzhiyun * update the first_bh and header_bh if the insert bucket has been moved
5017*4882a593Smuzhiyun * to the new cluster.
5018*4882a593Smuzhiyun * 2. If cluster_size == bucket_size:
5019*4882a593Smuzhiyun * a) If the previous extent rec has more than one cluster and the insert
5020*4882a593Smuzhiyun * place isn't in the last cluster, copy the entire last cluster to the
5021*4882a593Smuzhiyun * new one. This time, we don't need to upate the first_bh and header_bh
5022*4882a593Smuzhiyun * since they will not be moved into the new cluster.
5023*4882a593Smuzhiyun * b) Otherwise, move the bottom half of the xattrs in the last cluster into
5024*4882a593Smuzhiyun * the new one. And we set the extend flag to zero if the insert place is
5025*4882a593Smuzhiyun * moved into the new allocated cluster since no extend is needed.
5026*4882a593Smuzhiyun */
ocfs2_adjust_xattr_cross_cluster(struct inode * inode,handle_t * handle,struct ocfs2_xattr_bucket * first,struct ocfs2_xattr_bucket * target,u64 new_blk,u32 prev_clusters,u32 * v_start,int * extend)5027*4882a593Smuzhiyun static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
5028*4882a593Smuzhiyun handle_t *handle,
5029*4882a593Smuzhiyun struct ocfs2_xattr_bucket *first,
5030*4882a593Smuzhiyun struct ocfs2_xattr_bucket *target,
5031*4882a593Smuzhiyun u64 new_blk,
5032*4882a593Smuzhiyun u32 prev_clusters,
5033*4882a593Smuzhiyun u32 *v_start,
5034*4882a593Smuzhiyun int *extend)
5035*4882a593Smuzhiyun {
5036*4882a593Smuzhiyun int ret;
5037*4882a593Smuzhiyun
5038*4882a593Smuzhiyun trace_ocfs2_adjust_xattr_cross_cluster(
5039*4882a593Smuzhiyun (unsigned long long)bucket_blkno(first),
5040*4882a593Smuzhiyun (unsigned long long)new_blk, prev_clusters);
5041*4882a593Smuzhiyun
5042*4882a593Smuzhiyun if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
5043*4882a593Smuzhiyun ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
5044*4882a593Smuzhiyun handle,
5045*4882a593Smuzhiyun first, target,
5046*4882a593Smuzhiyun new_blk,
5047*4882a593Smuzhiyun prev_clusters,
5048*4882a593Smuzhiyun v_start);
5049*4882a593Smuzhiyun if (ret)
5050*4882a593Smuzhiyun mlog_errno(ret);
5051*4882a593Smuzhiyun } else {
5052*4882a593Smuzhiyun /* The start of the last cluster in the first extent */
5053*4882a593Smuzhiyun u64 last_blk = bucket_blkno(first) +
5054*4882a593Smuzhiyun ((prev_clusters - 1) *
5055*4882a593Smuzhiyun ocfs2_clusters_to_blocks(inode->i_sb, 1));
5056*4882a593Smuzhiyun
5057*4882a593Smuzhiyun if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
5058*4882a593Smuzhiyun ret = ocfs2_mv_xattr_buckets(inode, handle,
5059*4882a593Smuzhiyun bucket_blkno(first),
5060*4882a593Smuzhiyun last_blk, new_blk, 0,
5061*4882a593Smuzhiyun v_start);
5062*4882a593Smuzhiyun if (ret)
5063*4882a593Smuzhiyun mlog_errno(ret);
5064*4882a593Smuzhiyun } else {
5065*4882a593Smuzhiyun ret = ocfs2_divide_xattr_cluster(inode, handle,
5066*4882a593Smuzhiyun last_blk, new_blk,
5067*4882a593Smuzhiyun v_start);
5068*4882a593Smuzhiyun if (ret)
5069*4882a593Smuzhiyun mlog_errno(ret);
5070*4882a593Smuzhiyun
5071*4882a593Smuzhiyun if ((bucket_blkno(target) == last_blk) && extend)
5072*4882a593Smuzhiyun *extend = 0;
5073*4882a593Smuzhiyun }
5074*4882a593Smuzhiyun }
5075*4882a593Smuzhiyun
5076*4882a593Smuzhiyun return ret;
5077*4882a593Smuzhiyun }
5078*4882a593Smuzhiyun
5079*4882a593Smuzhiyun /*
5080*4882a593Smuzhiyun * Add a new cluster for xattr storage.
5081*4882a593Smuzhiyun *
5082*4882a593Smuzhiyun * If the new cluster is contiguous with the previous one, it will be
5083*4882a593Smuzhiyun * appended to the same extent record, and num_clusters will be updated.
5084*4882a593Smuzhiyun * If not, we will insert a new extent for it and move some xattrs in
5085*4882a593Smuzhiyun * the last cluster into the new allocated one.
5086*4882a593Smuzhiyun * We also need to limit the maximum size of a btree leaf, otherwise we'll
5087*4882a593Smuzhiyun * lose the benefits of hashing because we'll have to search large leaves.
5088*4882a593Smuzhiyun * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
5089*4882a593Smuzhiyun * if it's bigger).
5090*4882a593Smuzhiyun *
5091*4882a593Smuzhiyun * first_bh is the first block of the previous extent rec and header_bh
5092*4882a593Smuzhiyun * indicates the bucket we will insert the new xattrs. They will be updated
5093*4882a593Smuzhiyun * when the header_bh is moved into the new cluster.
5094*4882a593Smuzhiyun */
ocfs2_add_new_xattr_cluster(struct inode * inode,struct buffer_head * root_bh,struct ocfs2_xattr_bucket * first,struct ocfs2_xattr_bucket * target,u32 * num_clusters,u32 prev_cpos,int * extend,struct ocfs2_xattr_set_ctxt * ctxt)5095*4882a593Smuzhiyun static int ocfs2_add_new_xattr_cluster(struct inode *inode,
5096*4882a593Smuzhiyun struct buffer_head *root_bh,
5097*4882a593Smuzhiyun struct ocfs2_xattr_bucket *first,
5098*4882a593Smuzhiyun struct ocfs2_xattr_bucket *target,
5099*4882a593Smuzhiyun u32 *num_clusters,
5100*4882a593Smuzhiyun u32 prev_cpos,
5101*4882a593Smuzhiyun int *extend,
5102*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
5103*4882a593Smuzhiyun {
5104*4882a593Smuzhiyun int ret;
5105*4882a593Smuzhiyun u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
5106*4882a593Smuzhiyun u32 prev_clusters = *num_clusters;
5107*4882a593Smuzhiyun u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
5108*4882a593Smuzhiyun u64 block;
5109*4882a593Smuzhiyun handle_t *handle = ctxt->handle;
5110*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5111*4882a593Smuzhiyun struct ocfs2_extent_tree et;
5112*4882a593Smuzhiyun
5113*4882a593Smuzhiyun trace_ocfs2_add_new_xattr_cluster_begin(
5114*4882a593Smuzhiyun (unsigned long long)OCFS2_I(inode)->ip_blkno,
5115*4882a593Smuzhiyun (unsigned long long)bucket_blkno(first),
5116*4882a593Smuzhiyun prev_cpos, prev_clusters);
5117*4882a593Smuzhiyun
5118*4882a593Smuzhiyun ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5119*4882a593Smuzhiyun
5120*4882a593Smuzhiyun ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5121*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
5122*4882a593Smuzhiyun if (ret < 0) {
5123*4882a593Smuzhiyun mlog_errno(ret);
5124*4882a593Smuzhiyun goto leave;
5125*4882a593Smuzhiyun }
5126*4882a593Smuzhiyun
5127*4882a593Smuzhiyun ret = __ocfs2_claim_clusters(handle, ctxt->data_ac, 1,
5128*4882a593Smuzhiyun clusters_to_add, &bit_off, &num_bits);
5129*4882a593Smuzhiyun if (ret < 0) {
5130*4882a593Smuzhiyun if (ret != -ENOSPC)
5131*4882a593Smuzhiyun mlog_errno(ret);
5132*4882a593Smuzhiyun goto leave;
5133*4882a593Smuzhiyun }
5134*4882a593Smuzhiyun
5135*4882a593Smuzhiyun BUG_ON(num_bits > clusters_to_add);
5136*4882a593Smuzhiyun
5137*4882a593Smuzhiyun block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5138*4882a593Smuzhiyun trace_ocfs2_add_new_xattr_cluster((unsigned long long)block, num_bits);
5139*4882a593Smuzhiyun
5140*4882a593Smuzhiyun if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
5141*4882a593Smuzhiyun (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5142*4882a593Smuzhiyun OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5143*4882a593Smuzhiyun /*
5144*4882a593Smuzhiyun * If this cluster is contiguous with the old one and
5145*4882a593Smuzhiyun * adding this new cluster, we don't surpass the limit of
5146*4882a593Smuzhiyun * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5147*4882a593Smuzhiyun * initialized and used like other buckets in the previous
5148*4882a593Smuzhiyun * cluster.
5149*4882a593Smuzhiyun * So add it as a contiguous one. The caller will handle
5150*4882a593Smuzhiyun * its init process.
5151*4882a593Smuzhiyun */
5152*4882a593Smuzhiyun v_start = prev_cpos + prev_clusters;
5153*4882a593Smuzhiyun *num_clusters = prev_clusters + num_bits;
5154*4882a593Smuzhiyun } else {
5155*4882a593Smuzhiyun ret = ocfs2_adjust_xattr_cross_cluster(inode,
5156*4882a593Smuzhiyun handle,
5157*4882a593Smuzhiyun first,
5158*4882a593Smuzhiyun target,
5159*4882a593Smuzhiyun block,
5160*4882a593Smuzhiyun prev_clusters,
5161*4882a593Smuzhiyun &v_start,
5162*4882a593Smuzhiyun extend);
5163*4882a593Smuzhiyun if (ret) {
5164*4882a593Smuzhiyun mlog_errno(ret);
5165*4882a593Smuzhiyun goto leave;
5166*4882a593Smuzhiyun }
5167*4882a593Smuzhiyun }
5168*4882a593Smuzhiyun
5169*4882a593Smuzhiyun trace_ocfs2_add_new_xattr_cluster_insert((unsigned long long)block,
5170*4882a593Smuzhiyun v_start, num_bits);
5171*4882a593Smuzhiyun ret = ocfs2_insert_extent(handle, &et, v_start, block,
5172*4882a593Smuzhiyun num_bits, 0, ctxt->meta_ac);
5173*4882a593Smuzhiyun if (ret < 0) {
5174*4882a593Smuzhiyun mlog_errno(ret);
5175*4882a593Smuzhiyun goto leave;
5176*4882a593Smuzhiyun }
5177*4882a593Smuzhiyun
5178*4882a593Smuzhiyun ocfs2_journal_dirty(handle, root_bh);
5179*4882a593Smuzhiyun
5180*4882a593Smuzhiyun leave:
5181*4882a593Smuzhiyun return ret;
5182*4882a593Smuzhiyun }
5183*4882a593Smuzhiyun
5184*4882a593Smuzhiyun /*
5185*4882a593Smuzhiyun * We are given an extent. 'first' is the bucket at the very front of
5186*4882a593Smuzhiyun * the extent. The extent has space for an additional bucket past
5187*4882a593Smuzhiyun * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5188*4882a593Smuzhiyun * of the target bucket. We wish to shift every bucket past the target
5189*4882a593Smuzhiyun * down one, filling in that additional space. When we get back to the
5190*4882a593Smuzhiyun * target, we split the target between itself and the now-empty bucket
5191*4882a593Smuzhiyun * at target+1 (aka, target_blkno + blks_per_bucket).
5192*4882a593Smuzhiyun */
ocfs2_extend_xattr_bucket(struct inode * inode,handle_t * handle,struct ocfs2_xattr_bucket * first,u64 target_blk,u32 num_clusters)5193*4882a593Smuzhiyun static int ocfs2_extend_xattr_bucket(struct inode *inode,
5194*4882a593Smuzhiyun handle_t *handle,
5195*4882a593Smuzhiyun struct ocfs2_xattr_bucket *first,
5196*4882a593Smuzhiyun u64 target_blk,
5197*4882a593Smuzhiyun u32 num_clusters)
5198*4882a593Smuzhiyun {
5199*4882a593Smuzhiyun int ret, credits;
5200*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5201*4882a593Smuzhiyun u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5202*4882a593Smuzhiyun u64 end_blk;
5203*4882a593Smuzhiyun u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
5204*4882a593Smuzhiyun
5205*4882a593Smuzhiyun trace_ocfs2_extend_xattr_bucket((unsigned long long)target_blk,
5206*4882a593Smuzhiyun (unsigned long long)bucket_blkno(first),
5207*4882a593Smuzhiyun num_clusters, new_bucket);
5208*4882a593Smuzhiyun
5209*4882a593Smuzhiyun /* The extent must have room for an additional bucket */
5210*4882a593Smuzhiyun BUG_ON(new_bucket >=
5211*4882a593Smuzhiyun (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
5212*4882a593Smuzhiyun
5213*4882a593Smuzhiyun /* end_blk points to the last existing bucket */
5214*4882a593Smuzhiyun end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
5215*4882a593Smuzhiyun
5216*4882a593Smuzhiyun /*
5217*4882a593Smuzhiyun * end_blk is the start of the last existing bucket.
5218*4882a593Smuzhiyun * Thus, (end_blk - target_blk) covers the target bucket and
5219*4882a593Smuzhiyun * every bucket after it up to, but not including, the last
5220*4882a593Smuzhiyun * existing bucket. Then we add the last existing bucket, the
5221*4882a593Smuzhiyun * new bucket, and the first bucket (3 * blk_per_bucket).
5222*4882a593Smuzhiyun */
5223*4882a593Smuzhiyun credits = (end_blk - target_blk) + (3 * blk_per_bucket);
5224*4882a593Smuzhiyun ret = ocfs2_extend_trans(handle, credits);
5225*4882a593Smuzhiyun if (ret) {
5226*4882a593Smuzhiyun mlog_errno(ret);
5227*4882a593Smuzhiyun goto out;
5228*4882a593Smuzhiyun }
5229*4882a593Smuzhiyun
5230*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_journal_access(handle, first,
5231*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
5232*4882a593Smuzhiyun if (ret) {
5233*4882a593Smuzhiyun mlog_errno(ret);
5234*4882a593Smuzhiyun goto out;
5235*4882a593Smuzhiyun }
5236*4882a593Smuzhiyun
5237*4882a593Smuzhiyun while (end_blk != target_blk) {
5238*4882a593Smuzhiyun ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5239*4882a593Smuzhiyun end_blk + blk_per_bucket, 0);
5240*4882a593Smuzhiyun if (ret)
5241*4882a593Smuzhiyun goto out;
5242*4882a593Smuzhiyun end_blk -= blk_per_bucket;
5243*4882a593Smuzhiyun }
5244*4882a593Smuzhiyun
5245*4882a593Smuzhiyun /* Move half of the xattr in target_blkno to the next bucket. */
5246*4882a593Smuzhiyun ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5247*4882a593Smuzhiyun target_blk + blk_per_bucket, NULL, 0);
5248*4882a593Smuzhiyun
5249*4882a593Smuzhiyun le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5250*4882a593Smuzhiyun ocfs2_xattr_bucket_journal_dirty(handle, first);
5251*4882a593Smuzhiyun
5252*4882a593Smuzhiyun out:
5253*4882a593Smuzhiyun return ret;
5254*4882a593Smuzhiyun }
5255*4882a593Smuzhiyun
5256*4882a593Smuzhiyun /*
5257*4882a593Smuzhiyun * Add new xattr bucket in an extent record and adjust the buckets
5258*4882a593Smuzhiyun * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5259*4882a593Smuzhiyun * bucket we want to insert into.
5260*4882a593Smuzhiyun *
5261*4882a593Smuzhiyun * In the easy case, we will move all the buckets after target down by
5262*4882a593Smuzhiyun * one. Half of target's xattrs will be moved to the next bucket.
5263*4882a593Smuzhiyun *
5264*4882a593Smuzhiyun * If current cluster is full, we'll allocate a new one. This may not
5265*4882a593Smuzhiyun * be contiguous. The underlying calls will make sure that there is
5266*4882a593Smuzhiyun * space for the insert, shifting buckets around if necessary.
5267*4882a593Smuzhiyun * 'target' may be moved by those calls.
5268*4882a593Smuzhiyun */
ocfs2_add_new_xattr_bucket(struct inode * inode,struct buffer_head * xb_bh,struct ocfs2_xattr_bucket * target,struct ocfs2_xattr_set_ctxt * ctxt)5269*4882a593Smuzhiyun static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5270*4882a593Smuzhiyun struct buffer_head *xb_bh,
5271*4882a593Smuzhiyun struct ocfs2_xattr_bucket *target,
5272*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
5273*4882a593Smuzhiyun {
5274*4882a593Smuzhiyun struct ocfs2_xattr_block *xb =
5275*4882a593Smuzhiyun (struct ocfs2_xattr_block *)xb_bh->b_data;
5276*4882a593Smuzhiyun struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5277*4882a593Smuzhiyun struct ocfs2_extent_list *el = &xb_root->xt_list;
5278*4882a593Smuzhiyun u32 name_hash =
5279*4882a593Smuzhiyun le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
5280*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5281*4882a593Smuzhiyun int ret, num_buckets, extend = 1;
5282*4882a593Smuzhiyun u64 p_blkno;
5283*4882a593Smuzhiyun u32 e_cpos, num_clusters;
5284*4882a593Smuzhiyun /* The bucket at the front of the extent */
5285*4882a593Smuzhiyun struct ocfs2_xattr_bucket *first;
5286*4882a593Smuzhiyun
5287*4882a593Smuzhiyun trace_ocfs2_add_new_xattr_bucket(
5288*4882a593Smuzhiyun (unsigned long long)bucket_blkno(target));
5289*4882a593Smuzhiyun
5290*4882a593Smuzhiyun /* The first bucket of the original extent */
5291*4882a593Smuzhiyun first = ocfs2_xattr_bucket_new(inode);
5292*4882a593Smuzhiyun if (!first) {
5293*4882a593Smuzhiyun ret = -ENOMEM;
5294*4882a593Smuzhiyun mlog_errno(ret);
5295*4882a593Smuzhiyun goto out;
5296*4882a593Smuzhiyun }
5297*4882a593Smuzhiyun
5298*4882a593Smuzhiyun ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5299*4882a593Smuzhiyun &num_clusters, el);
5300*4882a593Smuzhiyun if (ret) {
5301*4882a593Smuzhiyun mlog_errno(ret);
5302*4882a593Smuzhiyun goto out;
5303*4882a593Smuzhiyun }
5304*4882a593Smuzhiyun
5305*4882a593Smuzhiyun ret = ocfs2_read_xattr_bucket(first, p_blkno);
5306*4882a593Smuzhiyun if (ret) {
5307*4882a593Smuzhiyun mlog_errno(ret);
5308*4882a593Smuzhiyun goto out;
5309*4882a593Smuzhiyun }
5310*4882a593Smuzhiyun
5311*4882a593Smuzhiyun num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
5312*4882a593Smuzhiyun if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5313*4882a593Smuzhiyun /*
5314*4882a593Smuzhiyun * This can move first+target if the target bucket moves
5315*4882a593Smuzhiyun * to the new extent.
5316*4882a593Smuzhiyun */
5317*4882a593Smuzhiyun ret = ocfs2_add_new_xattr_cluster(inode,
5318*4882a593Smuzhiyun xb_bh,
5319*4882a593Smuzhiyun first,
5320*4882a593Smuzhiyun target,
5321*4882a593Smuzhiyun &num_clusters,
5322*4882a593Smuzhiyun e_cpos,
5323*4882a593Smuzhiyun &extend,
5324*4882a593Smuzhiyun ctxt);
5325*4882a593Smuzhiyun if (ret) {
5326*4882a593Smuzhiyun mlog_errno(ret);
5327*4882a593Smuzhiyun goto out;
5328*4882a593Smuzhiyun }
5329*4882a593Smuzhiyun }
5330*4882a593Smuzhiyun
5331*4882a593Smuzhiyun if (extend) {
5332*4882a593Smuzhiyun ret = ocfs2_extend_xattr_bucket(inode,
5333*4882a593Smuzhiyun ctxt->handle,
5334*4882a593Smuzhiyun first,
5335*4882a593Smuzhiyun bucket_blkno(target),
5336*4882a593Smuzhiyun num_clusters);
5337*4882a593Smuzhiyun if (ret)
5338*4882a593Smuzhiyun mlog_errno(ret);
5339*4882a593Smuzhiyun }
5340*4882a593Smuzhiyun
5341*4882a593Smuzhiyun out:
5342*4882a593Smuzhiyun ocfs2_xattr_bucket_free(first);
5343*4882a593Smuzhiyun
5344*4882a593Smuzhiyun return ret;
5345*4882a593Smuzhiyun }
5346*4882a593Smuzhiyun
5347*4882a593Smuzhiyun /*
5348*4882a593Smuzhiyun * Truncate the specified xe_off entry in xattr bucket.
5349*4882a593Smuzhiyun * bucket is indicated by header_bh and len is the new length.
5350*4882a593Smuzhiyun * Both the ocfs2_xattr_value_root and the entry will be updated here.
5351*4882a593Smuzhiyun *
5352*4882a593Smuzhiyun * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5353*4882a593Smuzhiyun */
ocfs2_xattr_bucket_value_truncate(struct inode * inode,struct ocfs2_xattr_bucket * bucket,int xe_off,int len,struct ocfs2_xattr_set_ctxt * ctxt)5354*4882a593Smuzhiyun static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
5355*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket,
5356*4882a593Smuzhiyun int xe_off,
5357*4882a593Smuzhiyun int len,
5358*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
5359*4882a593Smuzhiyun {
5360*4882a593Smuzhiyun int ret, offset;
5361*4882a593Smuzhiyun u64 value_blk;
5362*4882a593Smuzhiyun struct ocfs2_xattr_entry *xe;
5363*4882a593Smuzhiyun struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5364*4882a593Smuzhiyun size_t blocksize = inode->i_sb->s_blocksize;
5365*4882a593Smuzhiyun struct ocfs2_xattr_value_buf vb = {
5366*4882a593Smuzhiyun .vb_access = ocfs2_journal_access,
5367*4882a593Smuzhiyun };
5368*4882a593Smuzhiyun
5369*4882a593Smuzhiyun xe = &xh->xh_entries[xe_off];
5370*4882a593Smuzhiyun
5371*4882a593Smuzhiyun BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5372*4882a593Smuzhiyun
5373*4882a593Smuzhiyun offset = le16_to_cpu(xe->xe_name_offset) +
5374*4882a593Smuzhiyun OCFS2_XATTR_SIZE(xe->xe_name_len);
5375*4882a593Smuzhiyun
5376*4882a593Smuzhiyun value_blk = offset / blocksize;
5377*4882a593Smuzhiyun
5378*4882a593Smuzhiyun /* We don't allow ocfs2_xattr_value to be stored in different block. */
5379*4882a593Smuzhiyun BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
5380*4882a593Smuzhiyun
5381*4882a593Smuzhiyun vb.vb_bh = bucket->bu_bhs[value_blk];
5382*4882a593Smuzhiyun BUG_ON(!vb.vb_bh);
5383*4882a593Smuzhiyun
5384*4882a593Smuzhiyun vb.vb_xv = (struct ocfs2_xattr_value_root *)
5385*4882a593Smuzhiyun (vb.vb_bh->b_data + offset % blocksize);
5386*4882a593Smuzhiyun
5387*4882a593Smuzhiyun /*
5388*4882a593Smuzhiyun * From here on out we have to dirty the bucket. The generic
5389*4882a593Smuzhiyun * value calls only modify one of the bucket's bhs, but we need
5390*4882a593Smuzhiyun * to send the bucket at once. So if they error, they *could* have
5391*4882a593Smuzhiyun * modified something. We have to assume they did, and dirty
5392*4882a593Smuzhiyun * the whole bucket. This leaves us in a consistent state.
5393*4882a593Smuzhiyun */
5394*4882a593Smuzhiyun trace_ocfs2_xattr_bucket_value_truncate(
5395*4882a593Smuzhiyun (unsigned long long)bucket_blkno(bucket), xe_off, len);
5396*4882a593Smuzhiyun ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
5397*4882a593Smuzhiyun if (ret) {
5398*4882a593Smuzhiyun mlog_errno(ret);
5399*4882a593Smuzhiyun goto out;
5400*4882a593Smuzhiyun }
5401*4882a593Smuzhiyun
5402*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5403*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
5404*4882a593Smuzhiyun if (ret) {
5405*4882a593Smuzhiyun mlog_errno(ret);
5406*4882a593Smuzhiyun goto out;
5407*4882a593Smuzhiyun }
5408*4882a593Smuzhiyun
5409*4882a593Smuzhiyun xe->xe_value_size = cpu_to_le64(len);
5410*4882a593Smuzhiyun
5411*4882a593Smuzhiyun ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
5412*4882a593Smuzhiyun
5413*4882a593Smuzhiyun out:
5414*4882a593Smuzhiyun return ret;
5415*4882a593Smuzhiyun }
5416*4882a593Smuzhiyun
ocfs2_rm_xattr_cluster(struct inode * inode,struct buffer_head * root_bh,u64 blkno,u32 cpos,u32 len,void * para)5417*4882a593Smuzhiyun static int ocfs2_rm_xattr_cluster(struct inode *inode,
5418*4882a593Smuzhiyun struct buffer_head *root_bh,
5419*4882a593Smuzhiyun u64 blkno,
5420*4882a593Smuzhiyun u32 cpos,
5421*4882a593Smuzhiyun u32 len,
5422*4882a593Smuzhiyun void *para)
5423*4882a593Smuzhiyun {
5424*4882a593Smuzhiyun int ret;
5425*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5426*4882a593Smuzhiyun struct inode *tl_inode = osb->osb_tl_inode;
5427*4882a593Smuzhiyun handle_t *handle;
5428*4882a593Smuzhiyun struct ocfs2_xattr_block *xb =
5429*4882a593Smuzhiyun (struct ocfs2_xattr_block *)root_bh->b_data;
5430*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac = NULL;
5431*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt dealloc;
5432*4882a593Smuzhiyun struct ocfs2_extent_tree et;
5433*4882a593Smuzhiyun
5434*4882a593Smuzhiyun ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
5435*4882a593Smuzhiyun ocfs2_delete_xattr_in_bucket, para);
5436*4882a593Smuzhiyun if (ret) {
5437*4882a593Smuzhiyun mlog_errno(ret);
5438*4882a593Smuzhiyun return ret;
5439*4882a593Smuzhiyun }
5440*4882a593Smuzhiyun
5441*4882a593Smuzhiyun ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5442*4882a593Smuzhiyun
5443*4882a593Smuzhiyun ocfs2_init_dealloc_ctxt(&dealloc);
5444*4882a593Smuzhiyun
5445*4882a593Smuzhiyun trace_ocfs2_rm_xattr_cluster(
5446*4882a593Smuzhiyun (unsigned long long)OCFS2_I(inode)->ip_blkno,
5447*4882a593Smuzhiyun (unsigned long long)blkno, cpos, len);
5448*4882a593Smuzhiyun
5449*4882a593Smuzhiyun ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5450*4882a593Smuzhiyun len);
5451*4882a593Smuzhiyun
5452*4882a593Smuzhiyun ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
5453*4882a593Smuzhiyun if (ret) {
5454*4882a593Smuzhiyun mlog_errno(ret);
5455*4882a593Smuzhiyun return ret;
5456*4882a593Smuzhiyun }
5457*4882a593Smuzhiyun
5458*4882a593Smuzhiyun inode_lock(tl_inode);
5459*4882a593Smuzhiyun
5460*4882a593Smuzhiyun if (ocfs2_truncate_log_needs_flush(osb)) {
5461*4882a593Smuzhiyun ret = __ocfs2_flush_truncate_log(osb);
5462*4882a593Smuzhiyun if (ret < 0) {
5463*4882a593Smuzhiyun mlog_errno(ret);
5464*4882a593Smuzhiyun goto out;
5465*4882a593Smuzhiyun }
5466*4882a593Smuzhiyun }
5467*4882a593Smuzhiyun
5468*4882a593Smuzhiyun handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5469*4882a593Smuzhiyun if (IS_ERR(handle)) {
5470*4882a593Smuzhiyun ret = -ENOMEM;
5471*4882a593Smuzhiyun mlog_errno(ret);
5472*4882a593Smuzhiyun goto out;
5473*4882a593Smuzhiyun }
5474*4882a593Smuzhiyun
5475*4882a593Smuzhiyun ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5476*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
5477*4882a593Smuzhiyun if (ret) {
5478*4882a593Smuzhiyun mlog_errno(ret);
5479*4882a593Smuzhiyun goto out_commit;
5480*4882a593Smuzhiyun }
5481*4882a593Smuzhiyun
5482*4882a593Smuzhiyun ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
5483*4882a593Smuzhiyun &dealloc);
5484*4882a593Smuzhiyun if (ret) {
5485*4882a593Smuzhiyun mlog_errno(ret);
5486*4882a593Smuzhiyun goto out_commit;
5487*4882a593Smuzhiyun }
5488*4882a593Smuzhiyun
5489*4882a593Smuzhiyun le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5490*4882a593Smuzhiyun ocfs2_journal_dirty(handle, root_bh);
5491*4882a593Smuzhiyun
5492*4882a593Smuzhiyun ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5493*4882a593Smuzhiyun if (ret)
5494*4882a593Smuzhiyun mlog_errno(ret);
5495*4882a593Smuzhiyun ocfs2_update_inode_fsync_trans(handle, inode, 0);
5496*4882a593Smuzhiyun
5497*4882a593Smuzhiyun out_commit:
5498*4882a593Smuzhiyun ocfs2_commit_trans(osb, handle);
5499*4882a593Smuzhiyun out:
5500*4882a593Smuzhiyun ocfs2_schedule_truncate_log_flush(osb, 1);
5501*4882a593Smuzhiyun
5502*4882a593Smuzhiyun inode_unlock(tl_inode);
5503*4882a593Smuzhiyun
5504*4882a593Smuzhiyun if (meta_ac)
5505*4882a593Smuzhiyun ocfs2_free_alloc_context(meta_ac);
5506*4882a593Smuzhiyun
5507*4882a593Smuzhiyun ocfs2_run_deallocs(osb, &dealloc);
5508*4882a593Smuzhiyun
5509*4882a593Smuzhiyun return ret;
5510*4882a593Smuzhiyun }
5511*4882a593Smuzhiyun
5512*4882a593Smuzhiyun /*
5513*4882a593Smuzhiyun * check whether the xattr bucket is filled up with the same hash value.
5514*4882a593Smuzhiyun * If we want to insert the xattr with the same hash, return -ENOSPC.
5515*4882a593Smuzhiyun * If we want to insert a xattr with different hash value, go ahead
5516*4882a593Smuzhiyun * and ocfs2_divide_xattr_bucket will handle this.
5517*4882a593Smuzhiyun */
ocfs2_check_xattr_bucket_collision(struct inode * inode,struct ocfs2_xattr_bucket * bucket,const char * name)5518*4882a593Smuzhiyun static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
5519*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket,
5520*4882a593Smuzhiyun const char *name)
5521*4882a593Smuzhiyun {
5522*4882a593Smuzhiyun struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5523*4882a593Smuzhiyun u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5524*4882a593Smuzhiyun
5525*4882a593Smuzhiyun if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5526*4882a593Smuzhiyun return 0;
5527*4882a593Smuzhiyun
5528*4882a593Smuzhiyun if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5529*4882a593Smuzhiyun xh->xh_entries[0].xe_name_hash) {
5530*4882a593Smuzhiyun mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5531*4882a593Smuzhiyun "hash = %u\n",
5532*4882a593Smuzhiyun (unsigned long long)bucket_blkno(bucket),
5533*4882a593Smuzhiyun le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5534*4882a593Smuzhiyun return -ENOSPC;
5535*4882a593Smuzhiyun }
5536*4882a593Smuzhiyun
5537*4882a593Smuzhiyun return 0;
5538*4882a593Smuzhiyun }
5539*4882a593Smuzhiyun
5540*4882a593Smuzhiyun /*
5541*4882a593Smuzhiyun * Try to set the entry in the current bucket. If we fail, the caller
5542*4882a593Smuzhiyun * will handle getting us another bucket.
5543*4882a593Smuzhiyun */
ocfs2_xattr_set_entry_bucket(struct inode * inode,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_search * xs,struct ocfs2_xattr_set_ctxt * ctxt)5544*4882a593Smuzhiyun static int ocfs2_xattr_set_entry_bucket(struct inode *inode,
5545*4882a593Smuzhiyun struct ocfs2_xattr_info *xi,
5546*4882a593Smuzhiyun struct ocfs2_xattr_search *xs,
5547*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
5548*4882a593Smuzhiyun {
5549*4882a593Smuzhiyun int ret;
5550*4882a593Smuzhiyun struct ocfs2_xa_loc loc;
5551*4882a593Smuzhiyun
5552*4882a593Smuzhiyun trace_ocfs2_xattr_set_entry_bucket(xi->xi_name);
5553*4882a593Smuzhiyun
5554*4882a593Smuzhiyun ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5555*4882a593Smuzhiyun xs->not_found ? NULL : xs->here);
5556*4882a593Smuzhiyun ret = ocfs2_xa_set(&loc, xi, ctxt);
5557*4882a593Smuzhiyun if (!ret) {
5558*4882a593Smuzhiyun xs->here = loc.xl_entry;
5559*4882a593Smuzhiyun goto out;
5560*4882a593Smuzhiyun }
5561*4882a593Smuzhiyun if (ret != -ENOSPC) {
5562*4882a593Smuzhiyun mlog_errno(ret);
5563*4882a593Smuzhiyun goto out;
5564*4882a593Smuzhiyun }
5565*4882a593Smuzhiyun
5566*4882a593Smuzhiyun /* Ok, we need space. Let's try defragmenting the bucket. */
5567*4882a593Smuzhiyun ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5568*4882a593Smuzhiyun xs->bucket);
5569*4882a593Smuzhiyun if (ret) {
5570*4882a593Smuzhiyun mlog_errno(ret);
5571*4882a593Smuzhiyun goto out;
5572*4882a593Smuzhiyun }
5573*4882a593Smuzhiyun
5574*4882a593Smuzhiyun ret = ocfs2_xa_set(&loc, xi, ctxt);
5575*4882a593Smuzhiyun if (!ret) {
5576*4882a593Smuzhiyun xs->here = loc.xl_entry;
5577*4882a593Smuzhiyun goto out;
5578*4882a593Smuzhiyun }
5579*4882a593Smuzhiyun if (ret != -ENOSPC)
5580*4882a593Smuzhiyun mlog_errno(ret);
5581*4882a593Smuzhiyun
5582*4882a593Smuzhiyun
5583*4882a593Smuzhiyun out:
5584*4882a593Smuzhiyun return ret;
5585*4882a593Smuzhiyun }
5586*4882a593Smuzhiyun
ocfs2_xattr_set_entry_index_block(struct inode * inode,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_search * xs,struct ocfs2_xattr_set_ctxt * ctxt)5587*4882a593Smuzhiyun static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5588*4882a593Smuzhiyun struct ocfs2_xattr_info *xi,
5589*4882a593Smuzhiyun struct ocfs2_xattr_search *xs,
5590*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt *ctxt)
5591*4882a593Smuzhiyun {
5592*4882a593Smuzhiyun int ret;
5593*4882a593Smuzhiyun
5594*4882a593Smuzhiyun trace_ocfs2_xattr_set_entry_index_block(xi->xi_name);
5595*4882a593Smuzhiyun
5596*4882a593Smuzhiyun ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5597*4882a593Smuzhiyun if (!ret)
5598*4882a593Smuzhiyun goto out;
5599*4882a593Smuzhiyun if (ret != -ENOSPC) {
5600*4882a593Smuzhiyun mlog_errno(ret);
5601*4882a593Smuzhiyun goto out;
5602*4882a593Smuzhiyun }
5603*4882a593Smuzhiyun
5604*4882a593Smuzhiyun /* Ack, need more space. Let's try to get another bucket! */
5605*4882a593Smuzhiyun
5606*4882a593Smuzhiyun /*
5607*4882a593Smuzhiyun * We do not allow for overlapping ranges between buckets. And
5608*4882a593Smuzhiyun * the maximum number of collisions we will allow for then is
5609*4882a593Smuzhiyun * one bucket's worth, so check it here whether we need to
5610*4882a593Smuzhiyun * add a new bucket for the insert.
5611*4882a593Smuzhiyun */
5612*4882a593Smuzhiyun ret = ocfs2_check_xattr_bucket_collision(inode,
5613*4882a593Smuzhiyun xs->bucket,
5614*4882a593Smuzhiyun xi->xi_name);
5615*4882a593Smuzhiyun if (ret) {
5616*4882a593Smuzhiyun mlog_errno(ret);
5617*4882a593Smuzhiyun goto out;
5618*4882a593Smuzhiyun }
5619*4882a593Smuzhiyun
5620*4882a593Smuzhiyun ret = ocfs2_add_new_xattr_bucket(inode,
5621*4882a593Smuzhiyun xs->xattr_bh,
5622*4882a593Smuzhiyun xs->bucket,
5623*4882a593Smuzhiyun ctxt);
5624*4882a593Smuzhiyun if (ret) {
5625*4882a593Smuzhiyun mlog_errno(ret);
5626*4882a593Smuzhiyun goto out;
5627*4882a593Smuzhiyun }
5628*4882a593Smuzhiyun
5629*4882a593Smuzhiyun /*
5630*4882a593Smuzhiyun * ocfs2_add_new_xattr_bucket() will have updated
5631*4882a593Smuzhiyun * xs->bucket if it moved, but it will not have updated
5632*4882a593Smuzhiyun * any of the other search fields. Thus, we drop it and
5633*4882a593Smuzhiyun * re-search. Everything should be cached, so it'll be
5634*4882a593Smuzhiyun * quick.
5635*4882a593Smuzhiyun */
5636*4882a593Smuzhiyun ocfs2_xattr_bucket_relse(xs->bucket);
5637*4882a593Smuzhiyun ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5638*4882a593Smuzhiyun xi->xi_name_index,
5639*4882a593Smuzhiyun xi->xi_name, xs);
5640*4882a593Smuzhiyun if (ret && ret != -ENODATA)
5641*4882a593Smuzhiyun goto out;
5642*4882a593Smuzhiyun xs->not_found = ret;
5643*4882a593Smuzhiyun
5644*4882a593Smuzhiyun /* Ok, we have a new bucket, let's try again */
5645*4882a593Smuzhiyun ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5646*4882a593Smuzhiyun if (ret && (ret != -ENOSPC))
5647*4882a593Smuzhiyun mlog_errno(ret);
5648*4882a593Smuzhiyun
5649*4882a593Smuzhiyun out:
5650*4882a593Smuzhiyun return ret;
5651*4882a593Smuzhiyun }
5652*4882a593Smuzhiyun
ocfs2_delete_xattr_in_bucket(struct inode * inode,struct ocfs2_xattr_bucket * bucket,void * para)5653*4882a593Smuzhiyun static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5654*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket,
5655*4882a593Smuzhiyun void *para)
5656*4882a593Smuzhiyun {
5657*4882a593Smuzhiyun int ret = 0, ref_credits;
5658*4882a593Smuzhiyun struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5659*4882a593Smuzhiyun u16 i;
5660*4882a593Smuzhiyun struct ocfs2_xattr_entry *xe;
5661*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5662*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5663*4882a593Smuzhiyun int credits = ocfs2_remove_extent_credits(osb->sb) +
5664*4882a593Smuzhiyun ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5665*4882a593Smuzhiyun struct ocfs2_xattr_value_root *xv;
5666*4882a593Smuzhiyun struct ocfs2_rm_xattr_bucket_para *args =
5667*4882a593Smuzhiyun (struct ocfs2_rm_xattr_bucket_para *)para;
5668*4882a593Smuzhiyun
5669*4882a593Smuzhiyun ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5670*4882a593Smuzhiyun
5671*4882a593Smuzhiyun for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5672*4882a593Smuzhiyun xe = &xh->xh_entries[i];
5673*4882a593Smuzhiyun if (ocfs2_xattr_is_local(xe))
5674*4882a593Smuzhiyun continue;
5675*4882a593Smuzhiyun
5676*4882a593Smuzhiyun ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5677*4882a593Smuzhiyun i, &xv, NULL);
5678*4882a593Smuzhiyun if (ret) {
5679*4882a593Smuzhiyun mlog_errno(ret);
5680*4882a593Smuzhiyun break;
5681*4882a593Smuzhiyun }
5682*4882a593Smuzhiyun
5683*4882a593Smuzhiyun ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5684*4882a593Smuzhiyun args->ref_ci,
5685*4882a593Smuzhiyun args->ref_root_bh,
5686*4882a593Smuzhiyun &ctxt.meta_ac,
5687*4882a593Smuzhiyun &ref_credits);
5688*4882a593Smuzhiyun
5689*4882a593Smuzhiyun ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
5690*4882a593Smuzhiyun if (IS_ERR(ctxt.handle)) {
5691*4882a593Smuzhiyun ret = PTR_ERR(ctxt.handle);
5692*4882a593Smuzhiyun mlog_errno(ret);
5693*4882a593Smuzhiyun break;
5694*4882a593Smuzhiyun }
5695*4882a593Smuzhiyun
5696*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
5697*4882a593Smuzhiyun i, 0, &ctxt);
5698*4882a593Smuzhiyun
5699*4882a593Smuzhiyun ocfs2_commit_trans(osb, ctxt.handle);
5700*4882a593Smuzhiyun if (ctxt.meta_ac) {
5701*4882a593Smuzhiyun ocfs2_free_alloc_context(ctxt.meta_ac);
5702*4882a593Smuzhiyun ctxt.meta_ac = NULL;
5703*4882a593Smuzhiyun }
5704*4882a593Smuzhiyun if (ret) {
5705*4882a593Smuzhiyun mlog_errno(ret);
5706*4882a593Smuzhiyun break;
5707*4882a593Smuzhiyun }
5708*4882a593Smuzhiyun }
5709*4882a593Smuzhiyun
5710*4882a593Smuzhiyun if (ctxt.meta_ac)
5711*4882a593Smuzhiyun ocfs2_free_alloc_context(ctxt.meta_ac);
5712*4882a593Smuzhiyun ocfs2_schedule_truncate_log_flush(osb, 1);
5713*4882a593Smuzhiyun ocfs2_run_deallocs(osb, &ctxt.dealloc);
5714*4882a593Smuzhiyun return ret;
5715*4882a593Smuzhiyun }
5716*4882a593Smuzhiyun
5717*4882a593Smuzhiyun /*
5718*4882a593Smuzhiyun * Whenever we modify a xattr value root in the bucket(e.g, CoW
5719*4882a593Smuzhiyun * or change the extent record flag), we need to recalculate
5720*4882a593Smuzhiyun * the metaecc for the whole bucket. So it is done here.
5721*4882a593Smuzhiyun *
5722*4882a593Smuzhiyun * Note:
5723*4882a593Smuzhiyun * We have to give the extra credits for the caller.
5724*4882a593Smuzhiyun */
ocfs2_xattr_bucket_post_refcount(struct inode * inode,handle_t * handle,void * para)5725*4882a593Smuzhiyun static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5726*4882a593Smuzhiyun handle_t *handle,
5727*4882a593Smuzhiyun void *para)
5728*4882a593Smuzhiyun {
5729*4882a593Smuzhiyun int ret;
5730*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket =
5731*4882a593Smuzhiyun (struct ocfs2_xattr_bucket *)para;
5732*4882a593Smuzhiyun
5733*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5734*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
5735*4882a593Smuzhiyun if (ret) {
5736*4882a593Smuzhiyun mlog_errno(ret);
5737*4882a593Smuzhiyun return ret;
5738*4882a593Smuzhiyun }
5739*4882a593Smuzhiyun
5740*4882a593Smuzhiyun ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5741*4882a593Smuzhiyun
5742*4882a593Smuzhiyun return 0;
5743*4882a593Smuzhiyun }
5744*4882a593Smuzhiyun
5745*4882a593Smuzhiyun /*
5746*4882a593Smuzhiyun * Special action we need if the xattr value is refcounted.
5747*4882a593Smuzhiyun *
5748*4882a593Smuzhiyun * 1. If the xattr is refcounted, lock the tree.
5749*4882a593Smuzhiyun * 2. CoW the xattr if we are setting the new value and the value
5750*4882a593Smuzhiyun * will be stored outside.
5751*4882a593Smuzhiyun * 3. In other case, decrease_refcount will work for us, so just
5752*4882a593Smuzhiyun * lock the refcount tree, calculate the meta and credits is OK.
5753*4882a593Smuzhiyun *
5754*4882a593Smuzhiyun * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5755*4882a593Smuzhiyun * currently CoW is a completed transaction, while this function
5756*4882a593Smuzhiyun * will also lock the allocators and let us deadlock. So we will
5757*4882a593Smuzhiyun * CoW the whole xattr value.
5758*4882a593Smuzhiyun */
ocfs2_prepare_refcount_xattr(struct inode * inode,struct ocfs2_dinode * di,struct ocfs2_xattr_info * xi,struct ocfs2_xattr_search * xis,struct ocfs2_xattr_search * xbs,struct ocfs2_refcount_tree ** ref_tree,int * meta_add,int * credits)5759*4882a593Smuzhiyun static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5760*4882a593Smuzhiyun struct ocfs2_dinode *di,
5761*4882a593Smuzhiyun struct ocfs2_xattr_info *xi,
5762*4882a593Smuzhiyun struct ocfs2_xattr_search *xis,
5763*4882a593Smuzhiyun struct ocfs2_xattr_search *xbs,
5764*4882a593Smuzhiyun struct ocfs2_refcount_tree **ref_tree,
5765*4882a593Smuzhiyun int *meta_add,
5766*4882a593Smuzhiyun int *credits)
5767*4882a593Smuzhiyun {
5768*4882a593Smuzhiyun int ret = 0;
5769*4882a593Smuzhiyun struct ocfs2_xattr_block *xb;
5770*4882a593Smuzhiyun struct ocfs2_xattr_entry *xe;
5771*4882a593Smuzhiyun char *base;
5772*4882a593Smuzhiyun u32 p_cluster, num_clusters;
5773*4882a593Smuzhiyun unsigned int ext_flags;
5774*4882a593Smuzhiyun int name_offset, name_len;
5775*4882a593Smuzhiyun struct ocfs2_xattr_value_buf vb;
5776*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket = NULL;
5777*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5778*4882a593Smuzhiyun struct ocfs2_post_refcount refcount;
5779*4882a593Smuzhiyun struct ocfs2_post_refcount *p = NULL;
5780*4882a593Smuzhiyun struct buffer_head *ref_root_bh = NULL;
5781*4882a593Smuzhiyun
5782*4882a593Smuzhiyun if (!xis->not_found) {
5783*4882a593Smuzhiyun xe = xis->here;
5784*4882a593Smuzhiyun name_offset = le16_to_cpu(xe->xe_name_offset);
5785*4882a593Smuzhiyun name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5786*4882a593Smuzhiyun base = xis->base;
5787*4882a593Smuzhiyun vb.vb_bh = xis->inode_bh;
5788*4882a593Smuzhiyun vb.vb_access = ocfs2_journal_access_di;
5789*4882a593Smuzhiyun } else {
5790*4882a593Smuzhiyun int i, block_off = 0;
5791*4882a593Smuzhiyun xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5792*4882a593Smuzhiyun xe = xbs->here;
5793*4882a593Smuzhiyun name_offset = le16_to_cpu(xe->xe_name_offset);
5794*4882a593Smuzhiyun name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5795*4882a593Smuzhiyun i = xbs->here - xbs->header->xh_entries;
5796*4882a593Smuzhiyun
5797*4882a593Smuzhiyun if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
5798*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
5799*4882a593Smuzhiyun bucket_xh(xbs->bucket),
5800*4882a593Smuzhiyun i, &block_off,
5801*4882a593Smuzhiyun &name_offset);
5802*4882a593Smuzhiyun if (ret) {
5803*4882a593Smuzhiyun mlog_errno(ret);
5804*4882a593Smuzhiyun goto out;
5805*4882a593Smuzhiyun }
5806*4882a593Smuzhiyun base = bucket_block(xbs->bucket, block_off);
5807*4882a593Smuzhiyun vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5808*4882a593Smuzhiyun vb.vb_access = ocfs2_journal_access;
5809*4882a593Smuzhiyun
5810*4882a593Smuzhiyun if (ocfs2_meta_ecc(osb)) {
5811*4882a593Smuzhiyun /*create parameters for ocfs2_post_refcount. */
5812*4882a593Smuzhiyun bucket = xbs->bucket;
5813*4882a593Smuzhiyun refcount.credits = bucket->bu_blocks;
5814*4882a593Smuzhiyun refcount.para = bucket;
5815*4882a593Smuzhiyun refcount.func =
5816*4882a593Smuzhiyun ocfs2_xattr_bucket_post_refcount;
5817*4882a593Smuzhiyun p = &refcount;
5818*4882a593Smuzhiyun }
5819*4882a593Smuzhiyun } else {
5820*4882a593Smuzhiyun base = xbs->base;
5821*4882a593Smuzhiyun vb.vb_bh = xbs->xattr_bh;
5822*4882a593Smuzhiyun vb.vb_access = ocfs2_journal_access_xb;
5823*4882a593Smuzhiyun }
5824*4882a593Smuzhiyun }
5825*4882a593Smuzhiyun
5826*4882a593Smuzhiyun if (ocfs2_xattr_is_local(xe))
5827*4882a593Smuzhiyun goto out;
5828*4882a593Smuzhiyun
5829*4882a593Smuzhiyun vb.vb_xv = (struct ocfs2_xattr_value_root *)
5830*4882a593Smuzhiyun (base + name_offset + name_len);
5831*4882a593Smuzhiyun
5832*4882a593Smuzhiyun ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5833*4882a593Smuzhiyun &num_clusters, &vb.vb_xv->xr_list,
5834*4882a593Smuzhiyun &ext_flags);
5835*4882a593Smuzhiyun if (ret) {
5836*4882a593Smuzhiyun mlog_errno(ret);
5837*4882a593Smuzhiyun goto out;
5838*4882a593Smuzhiyun }
5839*4882a593Smuzhiyun
5840*4882a593Smuzhiyun /*
5841*4882a593Smuzhiyun * We just need to check the 1st extent record, since we always
5842*4882a593Smuzhiyun * CoW the whole xattr. So there shouldn't be a xattr with
5843*4882a593Smuzhiyun * some REFCOUNT extent recs after the 1st one.
5844*4882a593Smuzhiyun */
5845*4882a593Smuzhiyun if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5846*4882a593Smuzhiyun goto out;
5847*4882a593Smuzhiyun
5848*4882a593Smuzhiyun ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5849*4882a593Smuzhiyun 1, ref_tree, &ref_root_bh);
5850*4882a593Smuzhiyun if (ret) {
5851*4882a593Smuzhiyun mlog_errno(ret);
5852*4882a593Smuzhiyun goto out;
5853*4882a593Smuzhiyun }
5854*4882a593Smuzhiyun
5855*4882a593Smuzhiyun /*
5856*4882a593Smuzhiyun * If we are deleting the xattr or the new size will be stored inside,
5857*4882a593Smuzhiyun * cool, leave it there, the xattr truncate process will remove them
5858*4882a593Smuzhiyun * for us(it still needs the refcount tree lock and the meta, credits).
5859*4882a593Smuzhiyun * And the worse case is that every cluster truncate will split the
5860*4882a593Smuzhiyun * refcount tree, and make the original extent become 3. So we will need
5861*4882a593Smuzhiyun * 2 * cluster more extent recs at most.
5862*4882a593Smuzhiyun */
5863*4882a593Smuzhiyun if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
5864*4882a593Smuzhiyun
5865*4882a593Smuzhiyun ret = ocfs2_refcounted_xattr_delete_need(inode,
5866*4882a593Smuzhiyun &(*ref_tree)->rf_ci,
5867*4882a593Smuzhiyun ref_root_bh, vb.vb_xv,
5868*4882a593Smuzhiyun meta_add, credits);
5869*4882a593Smuzhiyun if (ret)
5870*4882a593Smuzhiyun mlog_errno(ret);
5871*4882a593Smuzhiyun goto out;
5872*4882a593Smuzhiyun }
5873*4882a593Smuzhiyun
5874*4882a593Smuzhiyun ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5875*4882a593Smuzhiyun *ref_tree, ref_root_bh, 0,
5876*4882a593Smuzhiyun le32_to_cpu(vb.vb_xv->xr_clusters), p);
5877*4882a593Smuzhiyun if (ret)
5878*4882a593Smuzhiyun mlog_errno(ret);
5879*4882a593Smuzhiyun
5880*4882a593Smuzhiyun out:
5881*4882a593Smuzhiyun brelse(ref_root_bh);
5882*4882a593Smuzhiyun return ret;
5883*4882a593Smuzhiyun }
5884*4882a593Smuzhiyun
5885*4882a593Smuzhiyun /*
5886*4882a593Smuzhiyun * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5887*4882a593Smuzhiyun * The physical clusters will be added to refcount tree.
5888*4882a593Smuzhiyun */
ocfs2_xattr_value_attach_refcount(struct inode * inode,struct ocfs2_xattr_value_root * xv,struct ocfs2_extent_tree * value_et,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,struct ocfs2_cached_dealloc_ctxt * dealloc,struct ocfs2_post_refcount * refcount)5889*4882a593Smuzhiyun static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5890*4882a593Smuzhiyun struct ocfs2_xattr_value_root *xv,
5891*4882a593Smuzhiyun struct ocfs2_extent_tree *value_et,
5892*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci,
5893*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
5894*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc,
5895*4882a593Smuzhiyun struct ocfs2_post_refcount *refcount)
5896*4882a593Smuzhiyun {
5897*4882a593Smuzhiyun int ret = 0;
5898*4882a593Smuzhiyun u32 clusters = le32_to_cpu(xv->xr_clusters);
5899*4882a593Smuzhiyun u32 cpos, p_cluster, num_clusters;
5900*4882a593Smuzhiyun struct ocfs2_extent_list *el = &xv->xr_list;
5901*4882a593Smuzhiyun unsigned int ext_flags;
5902*4882a593Smuzhiyun
5903*4882a593Smuzhiyun cpos = 0;
5904*4882a593Smuzhiyun while (cpos < clusters) {
5905*4882a593Smuzhiyun ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5906*4882a593Smuzhiyun &num_clusters, el, &ext_flags);
5907*4882a593Smuzhiyun if (ret) {
5908*4882a593Smuzhiyun mlog_errno(ret);
5909*4882a593Smuzhiyun break;
5910*4882a593Smuzhiyun }
5911*4882a593Smuzhiyun
5912*4882a593Smuzhiyun cpos += num_clusters;
5913*4882a593Smuzhiyun if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5914*4882a593Smuzhiyun continue;
5915*4882a593Smuzhiyun
5916*4882a593Smuzhiyun BUG_ON(!p_cluster);
5917*4882a593Smuzhiyun
5918*4882a593Smuzhiyun ret = ocfs2_add_refcount_flag(inode, value_et,
5919*4882a593Smuzhiyun ref_ci, ref_root_bh,
5920*4882a593Smuzhiyun cpos - num_clusters,
5921*4882a593Smuzhiyun p_cluster, num_clusters,
5922*4882a593Smuzhiyun dealloc, refcount);
5923*4882a593Smuzhiyun if (ret) {
5924*4882a593Smuzhiyun mlog_errno(ret);
5925*4882a593Smuzhiyun break;
5926*4882a593Smuzhiyun }
5927*4882a593Smuzhiyun }
5928*4882a593Smuzhiyun
5929*4882a593Smuzhiyun return ret;
5930*4882a593Smuzhiyun }
5931*4882a593Smuzhiyun
5932*4882a593Smuzhiyun /*
5933*4882a593Smuzhiyun * Given a normal ocfs2_xattr_header, refcount all the entries which
5934*4882a593Smuzhiyun * have value stored outside.
5935*4882a593Smuzhiyun * Used for xattrs stored in inode and ocfs2_xattr_block.
5936*4882a593Smuzhiyun */
ocfs2_xattr_attach_refcount_normal(struct inode * inode,struct ocfs2_xattr_value_buf * vb,struct ocfs2_xattr_header * header,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,struct ocfs2_cached_dealloc_ctxt * dealloc)5937*4882a593Smuzhiyun static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5938*4882a593Smuzhiyun struct ocfs2_xattr_value_buf *vb,
5939*4882a593Smuzhiyun struct ocfs2_xattr_header *header,
5940*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci,
5941*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
5942*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc)
5943*4882a593Smuzhiyun {
5944*4882a593Smuzhiyun
5945*4882a593Smuzhiyun struct ocfs2_xattr_entry *xe;
5946*4882a593Smuzhiyun struct ocfs2_xattr_value_root *xv;
5947*4882a593Smuzhiyun struct ocfs2_extent_tree et;
5948*4882a593Smuzhiyun int i, ret = 0;
5949*4882a593Smuzhiyun
5950*4882a593Smuzhiyun for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5951*4882a593Smuzhiyun xe = &header->xh_entries[i];
5952*4882a593Smuzhiyun
5953*4882a593Smuzhiyun if (ocfs2_xattr_is_local(xe))
5954*4882a593Smuzhiyun continue;
5955*4882a593Smuzhiyun
5956*4882a593Smuzhiyun xv = (struct ocfs2_xattr_value_root *)((void *)header +
5957*4882a593Smuzhiyun le16_to_cpu(xe->xe_name_offset) +
5958*4882a593Smuzhiyun OCFS2_XATTR_SIZE(xe->xe_name_len));
5959*4882a593Smuzhiyun
5960*4882a593Smuzhiyun vb->vb_xv = xv;
5961*4882a593Smuzhiyun ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5962*4882a593Smuzhiyun
5963*4882a593Smuzhiyun ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5964*4882a593Smuzhiyun ref_ci, ref_root_bh,
5965*4882a593Smuzhiyun dealloc, NULL);
5966*4882a593Smuzhiyun if (ret) {
5967*4882a593Smuzhiyun mlog_errno(ret);
5968*4882a593Smuzhiyun break;
5969*4882a593Smuzhiyun }
5970*4882a593Smuzhiyun }
5971*4882a593Smuzhiyun
5972*4882a593Smuzhiyun return ret;
5973*4882a593Smuzhiyun }
5974*4882a593Smuzhiyun
ocfs2_xattr_inline_attach_refcount(struct inode * inode,struct buffer_head * fe_bh,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,struct ocfs2_cached_dealloc_ctxt * dealloc)5975*4882a593Smuzhiyun static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5976*4882a593Smuzhiyun struct buffer_head *fe_bh,
5977*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci,
5978*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
5979*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc)
5980*4882a593Smuzhiyun {
5981*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5982*4882a593Smuzhiyun struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5983*4882a593Smuzhiyun (fe_bh->b_data + inode->i_sb->s_blocksize -
5984*4882a593Smuzhiyun le16_to_cpu(di->i_xattr_inline_size));
5985*4882a593Smuzhiyun struct ocfs2_xattr_value_buf vb = {
5986*4882a593Smuzhiyun .vb_bh = fe_bh,
5987*4882a593Smuzhiyun .vb_access = ocfs2_journal_access_di,
5988*4882a593Smuzhiyun };
5989*4882a593Smuzhiyun
5990*4882a593Smuzhiyun return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5991*4882a593Smuzhiyun ref_ci, ref_root_bh, dealloc);
5992*4882a593Smuzhiyun }
5993*4882a593Smuzhiyun
5994*4882a593Smuzhiyun struct ocfs2_xattr_tree_value_refcount_para {
5995*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci;
5996*4882a593Smuzhiyun struct buffer_head *ref_root_bh;
5997*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc;
5998*4882a593Smuzhiyun };
5999*4882a593Smuzhiyun
ocfs2_get_xattr_tree_value_root(struct super_block * sb,struct ocfs2_xattr_bucket * bucket,int offset,struct ocfs2_xattr_value_root ** xv,struct buffer_head ** bh)6000*4882a593Smuzhiyun static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
6001*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket,
6002*4882a593Smuzhiyun int offset,
6003*4882a593Smuzhiyun struct ocfs2_xattr_value_root **xv,
6004*4882a593Smuzhiyun struct buffer_head **bh)
6005*4882a593Smuzhiyun {
6006*4882a593Smuzhiyun int ret, block_off, name_offset;
6007*4882a593Smuzhiyun struct ocfs2_xattr_header *xh = bucket_xh(bucket);
6008*4882a593Smuzhiyun struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6009*4882a593Smuzhiyun void *base;
6010*4882a593Smuzhiyun
6011*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_get_name_value(sb,
6012*4882a593Smuzhiyun bucket_xh(bucket),
6013*4882a593Smuzhiyun offset,
6014*4882a593Smuzhiyun &block_off,
6015*4882a593Smuzhiyun &name_offset);
6016*4882a593Smuzhiyun if (ret) {
6017*4882a593Smuzhiyun mlog_errno(ret);
6018*4882a593Smuzhiyun goto out;
6019*4882a593Smuzhiyun }
6020*4882a593Smuzhiyun
6021*4882a593Smuzhiyun base = bucket_block(bucket, block_off);
6022*4882a593Smuzhiyun
6023*4882a593Smuzhiyun *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
6024*4882a593Smuzhiyun OCFS2_XATTR_SIZE(xe->xe_name_len));
6025*4882a593Smuzhiyun
6026*4882a593Smuzhiyun if (bh)
6027*4882a593Smuzhiyun *bh = bucket->bu_bhs[block_off];
6028*4882a593Smuzhiyun out:
6029*4882a593Smuzhiyun return ret;
6030*4882a593Smuzhiyun }
6031*4882a593Smuzhiyun
6032*4882a593Smuzhiyun /*
6033*4882a593Smuzhiyun * For a given xattr bucket, refcount all the entries which
6034*4882a593Smuzhiyun * have value stored outside.
6035*4882a593Smuzhiyun */
ocfs2_xattr_bucket_value_refcount(struct inode * inode,struct ocfs2_xattr_bucket * bucket,void * para)6036*4882a593Smuzhiyun static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
6037*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket,
6038*4882a593Smuzhiyun void *para)
6039*4882a593Smuzhiyun {
6040*4882a593Smuzhiyun int i, ret = 0;
6041*4882a593Smuzhiyun struct ocfs2_extent_tree et;
6042*4882a593Smuzhiyun struct ocfs2_xattr_tree_value_refcount_para *ref =
6043*4882a593Smuzhiyun (struct ocfs2_xattr_tree_value_refcount_para *)para;
6044*4882a593Smuzhiyun struct ocfs2_xattr_header *xh =
6045*4882a593Smuzhiyun (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6046*4882a593Smuzhiyun struct ocfs2_xattr_entry *xe;
6047*4882a593Smuzhiyun struct ocfs2_xattr_value_buf vb = {
6048*4882a593Smuzhiyun .vb_access = ocfs2_journal_access,
6049*4882a593Smuzhiyun };
6050*4882a593Smuzhiyun struct ocfs2_post_refcount refcount = {
6051*4882a593Smuzhiyun .credits = bucket->bu_blocks,
6052*4882a593Smuzhiyun .para = bucket,
6053*4882a593Smuzhiyun .func = ocfs2_xattr_bucket_post_refcount,
6054*4882a593Smuzhiyun };
6055*4882a593Smuzhiyun struct ocfs2_post_refcount *p = NULL;
6056*4882a593Smuzhiyun
6057*4882a593Smuzhiyun /* We only need post_refcount if we support metaecc. */
6058*4882a593Smuzhiyun if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6059*4882a593Smuzhiyun p = &refcount;
6060*4882a593Smuzhiyun
6061*4882a593Smuzhiyun trace_ocfs2_xattr_bucket_value_refcount(
6062*4882a593Smuzhiyun (unsigned long long)bucket_blkno(bucket),
6063*4882a593Smuzhiyun le16_to_cpu(xh->xh_count));
6064*4882a593Smuzhiyun for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6065*4882a593Smuzhiyun xe = &xh->xh_entries[i];
6066*4882a593Smuzhiyun
6067*4882a593Smuzhiyun if (ocfs2_xattr_is_local(xe))
6068*4882a593Smuzhiyun continue;
6069*4882a593Smuzhiyun
6070*4882a593Smuzhiyun ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6071*4882a593Smuzhiyun &vb.vb_xv, &vb.vb_bh);
6072*4882a593Smuzhiyun if (ret) {
6073*4882a593Smuzhiyun mlog_errno(ret);
6074*4882a593Smuzhiyun break;
6075*4882a593Smuzhiyun }
6076*4882a593Smuzhiyun
6077*4882a593Smuzhiyun ocfs2_init_xattr_value_extent_tree(&et,
6078*4882a593Smuzhiyun INODE_CACHE(inode), &vb);
6079*4882a593Smuzhiyun
6080*4882a593Smuzhiyun ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6081*4882a593Smuzhiyun &et, ref->ref_ci,
6082*4882a593Smuzhiyun ref->ref_root_bh,
6083*4882a593Smuzhiyun ref->dealloc, p);
6084*4882a593Smuzhiyun if (ret) {
6085*4882a593Smuzhiyun mlog_errno(ret);
6086*4882a593Smuzhiyun break;
6087*4882a593Smuzhiyun }
6088*4882a593Smuzhiyun }
6089*4882a593Smuzhiyun
6090*4882a593Smuzhiyun return ret;
6091*4882a593Smuzhiyun
6092*4882a593Smuzhiyun }
6093*4882a593Smuzhiyun
ocfs2_refcount_xattr_tree_rec(struct inode * inode,struct buffer_head * root_bh,u64 blkno,u32 cpos,u32 len,void * para)6094*4882a593Smuzhiyun static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6095*4882a593Smuzhiyun struct buffer_head *root_bh,
6096*4882a593Smuzhiyun u64 blkno, u32 cpos, u32 len, void *para)
6097*4882a593Smuzhiyun {
6098*4882a593Smuzhiyun return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6099*4882a593Smuzhiyun ocfs2_xattr_bucket_value_refcount,
6100*4882a593Smuzhiyun para);
6101*4882a593Smuzhiyun }
6102*4882a593Smuzhiyun
ocfs2_xattr_block_attach_refcount(struct inode * inode,struct buffer_head * blk_bh,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,struct ocfs2_cached_dealloc_ctxt * dealloc)6103*4882a593Smuzhiyun static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6104*4882a593Smuzhiyun struct buffer_head *blk_bh,
6105*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci,
6106*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
6107*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc)
6108*4882a593Smuzhiyun {
6109*4882a593Smuzhiyun int ret = 0;
6110*4882a593Smuzhiyun struct ocfs2_xattr_block *xb =
6111*4882a593Smuzhiyun (struct ocfs2_xattr_block *)blk_bh->b_data;
6112*4882a593Smuzhiyun
6113*4882a593Smuzhiyun if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6114*4882a593Smuzhiyun struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6115*4882a593Smuzhiyun struct ocfs2_xattr_value_buf vb = {
6116*4882a593Smuzhiyun .vb_bh = blk_bh,
6117*4882a593Smuzhiyun .vb_access = ocfs2_journal_access_xb,
6118*4882a593Smuzhiyun };
6119*4882a593Smuzhiyun
6120*4882a593Smuzhiyun ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6121*4882a593Smuzhiyun ref_ci, ref_root_bh,
6122*4882a593Smuzhiyun dealloc);
6123*4882a593Smuzhiyun } else {
6124*4882a593Smuzhiyun struct ocfs2_xattr_tree_value_refcount_para para = {
6125*4882a593Smuzhiyun .ref_ci = ref_ci,
6126*4882a593Smuzhiyun .ref_root_bh = ref_root_bh,
6127*4882a593Smuzhiyun .dealloc = dealloc,
6128*4882a593Smuzhiyun };
6129*4882a593Smuzhiyun
6130*4882a593Smuzhiyun ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6131*4882a593Smuzhiyun ocfs2_refcount_xattr_tree_rec,
6132*4882a593Smuzhiyun ¶);
6133*4882a593Smuzhiyun }
6134*4882a593Smuzhiyun
6135*4882a593Smuzhiyun return ret;
6136*4882a593Smuzhiyun }
6137*4882a593Smuzhiyun
ocfs2_xattr_attach_refcount_tree(struct inode * inode,struct buffer_head * fe_bh,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,struct ocfs2_cached_dealloc_ctxt * dealloc)6138*4882a593Smuzhiyun int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6139*4882a593Smuzhiyun struct buffer_head *fe_bh,
6140*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci,
6141*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
6142*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc)
6143*4882a593Smuzhiyun {
6144*4882a593Smuzhiyun int ret = 0;
6145*4882a593Smuzhiyun struct ocfs2_inode_info *oi = OCFS2_I(inode);
6146*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6147*4882a593Smuzhiyun struct buffer_head *blk_bh = NULL;
6148*4882a593Smuzhiyun
6149*4882a593Smuzhiyun if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6150*4882a593Smuzhiyun ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6151*4882a593Smuzhiyun ref_ci, ref_root_bh,
6152*4882a593Smuzhiyun dealloc);
6153*4882a593Smuzhiyun if (ret) {
6154*4882a593Smuzhiyun mlog_errno(ret);
6155*4882a593Smuzhiyun goto out;
6156*4882a593Smuzhiyun }
6157*4882a593Smuzhiyun }
6158*4882a593Smuzhiyun
6159*4882a593Smuzhiyun if (!di->i_xattr_loc)
6160*4882a593Smuzhiyun goto out;
6161*4882a593Smuzhiyun
6162*4882a593Smuzhiyun ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6163*4882a593Smuzhiyun &blk_bh);
6164*4882a593Smuzhiyun if (ret < 0) {
6165*4882a593Smuzhiyun mlog_errno(ret);
6166*4882a593Smuzhiyun goto out;
6167*4882a593Smuzhiyun }
6168*4882a593Smuzhiyun
6169*4882a593Smuzhiyun ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6170*4882a593Smuzhiyun ref_root_bh, dealloc);
6171*4882a593Smuzhiyun if (ret)
6172*4882a593Smuzhiyun mlog_errno(ret);
6173*4882a593Smuzhiyun
6174*4882a593Smuzhiyun brelse(blk_bh);
6175*4882a593Smuzhiyun out:
6176*4882a593Smuzhiyun
6177*4882a593Smuzhiyun return ret;
6178*4882a593Smuzhiyun }
6179*4882a593Smuzhiyun
6180*4882a593Smuzhiyun typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
6181*4882a593Smuzhiyun /*
6182*4882a593Smuzhiyun * Store the information we need in xattr reflink.
6183*4882a593Smuzhiyun * old_bh and new_bh are inode bh for the old and new inode.
6184*4882a593Smuzhiyun */
6185*4882a593Smuzhiyun struct ocfs2_xattr_reflink {
6186*4882a593Smuzhiyun struct inode *old_inode;
6187*4882a593Smuzhiyun struct inode *new_inode;
6188*4882a593Smuzhiyun struct buffer_head *old_bh;
6189*4882a593Smuzhiyun struct buffer_head *new_bh;
6190*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci;
6191*4882a593Smuzhiyun struct buffer_head *ref_root_bh;
6192*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc;
6193*4882a593Smuzhiyun should_xattr_reflinked *xattr_reflinked;
6194*4882a593Smuzhiyun };
6195*4882a593Smuzhiyun
6196*4882a593Smuzhiyun /*
6197*4882a593Smuzhiyun * Given a xattr header and xe offset,
6198*4882a593Smuzhiyun * return the proper xv and the corresponding bh.
6199*4882a593Smuzhiyun * xattr in inode, block and xattr tree have different implementaions.
6200*4882a593Smuzhiyun */
6201*4882a593Smuzhiyun typedef int (get_xattr_value_root)(struct super_block *sb,
6202*4882a593Smuzhiyun struct buffer_head *bh,
6203*4882a593Smuzhiyun struct ocfs2_xattr_header *xh,
6204*4882a593Smuzhiyun int offset,
6205*4882a593Smuzhiyun struct ocfs2_xattr_value_root **xv,
6206*4882a593Smuzhiyun struct buffer_head **ret_bh,
6207*4882a593Smuzhiyun void *para);
6208*4882a593Smuzhiyun
6209*4882a593Smuzhiyun /*
6210*4882a593Smuzhiyun * Calculate all the xattr value root metadata stored in this xattr header and
6211*4882a593Smuzhiyun * credits we need if we create them from the scratch.
6212*4882a593Smuzhiyun * We use get_xattr_value_root so that all types of xattr container can use it.
6213*4882a593Smuzhiyun */
ocfs2_value_metas_in_xattr_header(struct super_block * sb,struct buffer_head * bh,struct ocfs2_xattr_header * xh,int * metas,int * credits,int * num_recs,get_xattr_value_root * func,void * para)6214*4882a593Smuzhiyun static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6215*4882a593Smuzhiyun struct buffer_head *bh,
6216*4882a593Smuzhiyun struct ocfs2_xattr_header *xh,
6217*4882a593Smuzhiyun int *metas, int *credits,
6218*4882a593Smuzhiyun int *num_recs,
6219*4882a593Smuzhiyun get_xattr_value_root *func,
6220*4882a593Smuzhiyun void *para)
6221*4882a593Smuzhiyun {
6222*4882a593Smuzhiyun int i, ret = 0;
6223*4882a593Smuzhiyun struct ocfs2_xattr_value_root *xv;
6224*4882a593Smuzhiyun struct ocfs2_xattr_entry *xe;
6225*4882a593Smuzhiyun
6226*4882a593Smuzhiyun for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6227*4882a593Smuzhiyun xe = &xh->xh_entries[i];
6228*4882a593Smuzhiyun if (ocfs2_xattr_is_local(xe))
6229*4882a593Smuzhiyun continue;
6230*4882a593Smuzhiyun
6231*4882a593Smuzhiyun ret = func(sb, bh, xh, i, &xv, NULL, para);
6232*4882a593Smuzhiyun if (ret) {
6233*4882a593Smuzhiyun mlog_errno(ret);
6234*4882a593Smuzhiyun break;
6235*4882a593Smuzhiyun }
6236*4882a593Smuzhiyun
6237*4882a593Smuzhiyun *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6238*4882a593Smuzhiyun le16_to_cpu(xv->xr_list.l_next_free_rec);
6239*4882a593Smuzhiyun
6240*4882a593Smuzhiyun *credits += ocfs2_calc_extend_credits(sb,
6241*4882a593Smuzhiyun &def_xv.xv.xr_list);
6242*4882a593Smuzhiyun
6243*4882a593Smuzhiyun /*
6244*4882a593Smuzhiyun * If the value is a tree with depth > 1, We don't go deep
6245*4882a593Smuzhiyun * to the extent block, so just calculate a maximum record num.
6246*4882a593Smuzhiyun */
6247*4882a593Smuzhiyun if (!xv->xr_list.l_tree_depth)
6248*4882a593Smuzhiyun *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
6249*4882a593Smuzhiyun else
6250*4882a593Smuzhiyun *num_recs += ocfs2_clusters_for_bytes(sb,
6251*4882a593Smuzhiyun XATTR_SIZE_MAX);
6252*4882a593Smuzhiyun }
6253*4882a593Smuzhiyun
6254*4882a593Smuzhiyun return ret;
6255*4882a593Smuzhiyun }
6256*4882a593Smuzhiyun
6257*4882a593Smuzhiyun /* Used by xattr inode and block to return the right xv and buffer_head. */
ocfs2_get_xattr_value_root(struct super_block * sb,struct buffer_head * bh,struct ocfs2_xattr_header * xh,int offset,struct ocfs2_xattr_value_root ** xv,struct buffer_head ** ret_bh,void * para)6258*4882a593Smuzhiyun static int ocfs2_get_xattr_value_root(struct super_block *sb,
6259*4882a593Smuzhiyun struct buffer_head *bh,
6260*4882a593Smuzhiyun struct ocfs2_xattr_header *xh,
6261*4882a593Smuzhiyun int offset,
6262*4882a593Smuzhiyun struct ocfs2_xattr_value_root **xv,
6263*4882a593Smuzhiyun struct buffer_head **ret_bh,
6264*4882a593Smuzhiyun void *para)
6265*4882a593Smuzhiyun {
6266*4882a593Smuzhiyun struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6267*4882a593Smuzhiyun
6268*4882a593Smuzhiyun *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6269*4882a593Smuzhiyun le16_to_cpu(xe->xe_name_offset) +
6270*4882a593Smuzhiyun OCFS2_XATTR_SIZE(xe->xe_name_len));
6271*4882a593Smuzhiyun
6272*4882a593Smuzhiyun if (ret_bh)
6273*4882a593Smuzhiyun *ret_bh = bh;
6274*4882a593Smuzhiyun
6275*4882a593Smuzhiyun return 0;
6276*4882a593Smuzhiyun }
6277*4882a593Smuzhiyun
6278*4882a593Smuzhiyun /*
6279*4882a593Smuzhiyun * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6280*4882a593Smuzhiyun * It is only used for inline xattr and xattr block.
6281*4882a593Smuzhiyun */
ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super * osb,struct ocfs2_xattr_header * xh,struct buffer_head * ref_root_bh,int * credits,struct ocfs2_alloc_context ** meta_ac)6282*4882a593Smuzhiyun static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6283*4882a593Smuzhiyun struct ocfs2_xattr_header *xh,
6284*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
6285*4882a593Smuzhiyun int *credits,
6286*4882a593Smuzhiyun struct ocfs2_alloc_context **meta_ac)
6287*4882a593Smuzhiyun {
6288*4882a593Smuzhiyun int ret, meta_add = 0, num_recs = 0;
6289*4882a593Smuzhiyun struct ocfs2_refcount_block *rb =
6290*4882a593Smuzhiyun (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6291*4882a593Smuzhiyun
6292*4882a593Smuzhiyun *credits = 0;
6293*4882a593Smuzhiyun
6294*4882a593Smuzhiyun ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6295*4882a593Smuzhiyun &meta_add, credits, &num_recs,
6296*4882a593Smuzhiyun ocfs2_get_xattr_value_root,
6297*4882a593Smuzhiyun NULL);
6298*4882a593Smuzhiyun if (ret) {
6299*4882a593Smuzhiyun mlog_errno(ret);
6300*4882a593Smuzhiyun goto out;
6301*4882a593Smuzhiyun }
6302*4882a593Smuzhiyun
6303*4882a593Smuzhiyun /*
6304*4882a593Smuzhiyun * We need to add/modify num_recs in refcount tree, so just calculate
6305*4882a593Smuzhiyun * an approximate number we need for refcount tree change.
6306*4882a593Smuzhiyun * Sometimes we need to split the tree, and after split, half recs
6307*4882a593Smuzhiyun * will be moved to the new block, and a new block can only provide
6308*4882a593Smuzhiyun * half number of recs. So we multiple new blocks by 2.
6309*4882a593Smuzhiyun */
6310*4882a593Smuzhiyun num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6311*4882a593Smuzhiyun meta_add += num_recs;
6312*4882a593Smuzhiyun *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6313*4882a593Smuzhiyun if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6314*4882a593Smuzhiyun *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6315*4882a593Smuzhiyun le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6316*4882a593Smuzhiyun else
6317*4882a593Smuzhiyun *credits += 1;
6318*4882a593Smuzhiyun
6319*4882a593Smuzhiyun ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6320*4882a593Smuzhiyun if (ret)
6321*4882a593Smuzhiyun mlog_errno(ret);
6322*4882a593Smuzhiyun
6323*4882a593Smuzhiyun out:
6324*4882a593Smuzhiyun return ret;
6325*4882a593Smuzhiyun }
6326*4882a593Smuzhiyun
6327*4882a593Smuzhiyun /*
6328*4882a593Smuzhiyun * Given a xattr header, reflink all the xattrs in this container.
6329*4882a593Smuzhiyun * It can be used for inode, block and bucket.
6330*4882a593Smuzhiyun *
6331*4882a593Smuzhiyun * NOTE:
6332*4882a593Smuzhiyun * Before we call this function, the caller has memcpy the xattr in
6333*4882a593Smuzhiyun * old_xh to the new_xh.
6334*4882a593Smuzhiyun *
6335*4882a593Smuzhiyun * If args.xattr_reflinked is set, call it to decide whether the xe should
6336*4882a593Smuzhiyun * be reflinked or not. If not, remove it from the new xattr header.
6337*4882a593Smuzhiyun */
ocfs2_reflink_xattr_header(handle_t * handle,struct ocfs2_xattr_reflink * args,struct buffer_head * old_bh,struct ocfs2_xattr_header * xh,struct buffer_head * new_bh,struct ocfs2_xattr_header * new_xh,struct ocfs2_xattr_value_buf * vb,struct ocfs2_alloc_context * meta_ac,get_xattr_value_root * func,void * para)6338*4882a593Smuzhiyun static int ocfs2_reflink_xattr_header(handle_t *handle,
6339*4882a593Smuzhiyun struct ocfs2_xattr_reflink *args,
6340*4882a593Smuzhiyun struct buffer_head *old_bh,
6341*4882a593Smuzhiyun struct ocfs2_xattr_header *xh,
6342*4882a593Smuzhiyun struct buffer_head *new_bh,
6343*4882a593Smuzhiyun struct ocfs2_xattr_header *new_xh,
6344*4882a593Smuzhiyun struct ocfs2_xattr_value_buf *vb,
6345*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac,
6346*4882a593Smuzhiyun get_xattr_value_root *func,
6347*4882a593Smuzhiyun void *para)
6348*4882a593Smuzhiyun {
6349*4882a593Smuzhiyun int ret = 0, i, j;
6350*4882a593Smuzhiyun struct super_block *sb = args->old_inode->i_sb;
6351*4882a593Smuzhiyun struct buffer_head *value_bh;
6352*4882a593Smuzhiyun struct ocfs2_xattr_entry *xe, *last;
6353*4882a593Smuzhiyun struct ocfs2_xattr_value_root *xv, *new_xv;
6354*4882a593Smuzhiyun struct ocfs2_extent_tree data_et;
6355*4882a593Smuzhiyun u32 clusters, cpos, p_cluster, num_clusters;
6356*4882a593Smuzhiyun unsigned int ext_flags = 0;
6357*4882a593Smuzhiyun
6358*4882a593Smuzhiyun trace_ocfs2_reflink_xattr_header((unsigned long long)old_bh->b_blocknr,
6359*4882a593Smuzhiyun le16_to_cpu(xh->xh_count));
6360*4882a593Smuzhiyun
6361*4882a593Smuzhiyun last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6362*4882a593Smuzhiyun for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
6363*4882a593Smuzhiyun xe = &xh->xh_entries[i];
6364*4882a593Smuzhiyun
6365*4882a593Smuzhiyun if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6366*4882a593Smuzhiyun xe = &new_xh->xh_entries[j];
6367*4882a593Smuzhiyun
6368*4882a593Smuzhiyun le16_add_cpu(&new_xh->xh_count, -1);
6369*4882a593Smuzhiyun if (new_xh->xh_count) {
6370*4882a593Smuzhiyun memmove(xe, xe + 1,
6371*4882a593Smuzhiyun (void *)last - (void *)xe);
6372*4882a593Smuzhiyun memset(last, 0,
6373*4882a593Smuzhiyun sizeof(struct ocfs2_xattr_entry));
6374*4882a593Smuzhiyun }
6375*4882a593Smuzhiyun
6376*4882a593Smuzhiyun /*
6377*4882a593Smuzhiyun * We don't want j to increase in the next round since
6378*4882a593Smuzhiyun * it is already moved ahead.
6379*4882a593Smuzhiyun */
6380*4882a593Smuzhiyun j--;
6381*4882a593Smuzhiyun continue;
6382*4882a593Smuzhiyun }
6383*4882a593Smuzhiyun
6384*4882a593Smuzhiyun if (ocfs2_xattr_is_local(xe))
6385*4882a593Smuzhiyun continue;
6386*4882a593Smuzhiyun
6387*4882a593Smuzhiyun ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6388*4882a593Smuzhiyun if (ret) {
6389*4882a593Smuzhiyun mlog_errno(ret);
6390*4882a593Smuzhiyun break;
6391*4882a593Smuzhiyun }
6392*4882a593Smuzhiyun
6393*4882a593Smuzhiyun ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
6394*4882a593Smuzhiyun if (ret) {
6395*4882a593Smuzhiyun mlog_errno(ret);
6396*4882a593Smuzhiyun break;
6397*4882a593Smuzhiyun }
6398*4882a593Smuzhiyun
6399*4882a593Smuzhiyun /*
6400*4882a593Smuzhiyun * For the xattr which has l_tree_depth = 0, all the extent
6401*4882a593Smuzhiyun * recs have already be copied to the new xh with the
6402*4882a593Smuzhiyun * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6403*4882a593Smuzhiyun * increase the refount count int the refcount tree.
6404*4882a593Smuzhiyun *
6405*4882a593Smuzhiyun * For the xattr which has l_tree_depth > 0, we need
6406*4882a593Smuzhiyun * to initialize it to the empty default value root,
6407*4882a593Smuzhiyun * and then insert the extents one by one.
6408*4882a593Smuzhiyun */
6409*4882a593Smuzhiyun if (xv->xr_list.l_tree_depth) {
6410*4882a593Smuzhiyun memcpy(new_xv, &def_xv, OCFS2_XATTR_ROOT_SIZE);
6411*4882a593Smuzhiyun vb->vb_xv = new_xv;
6412*4882a593Smuzhiyun vb->vb_bh = value_bh;
6413*4882a593Smuzhiyun ocfs2_init_xattr_value_extent_tree(&data_et,
6414*4882a593Smuzhiyun INODE_CACHE(args->new_inode), vb);
6415*4882a593Smuzhiyun }
6416*4882a593Smuzhiyun
6417*4882a593Smuzhiyun clusters = le32_to_cpu(xv->xr_clusters);
6418*4882a593Smuzhiyun cpos = 0;
6419*4882a593Smuzhiyun while (cpos < clusters) {
6420*4882a593Smuzhiyun ret = ocfs2_xattr_get_clusters(args->old_inode,
6421*4882a593Smuzhiyun cpos,
6422*4882a593Smuzhiyun &p_cluster,
6423*4882a593Smuzhiyun &num_clusters,
6424*4882a593Smuzhiyun &xv->xr_list,
6425*4882a593Smuzhiyun &ext_flags);
6426*4882a593Smuzhiyun if (ret) {
6427*4882a593Smuzhiyun mlog_errno(ret);
6428*4882a593Smuzhiyun goto out;
6429*4882a593Smuzhiyun }
6430*4882a593Smuzhiyun
6431*4882a593Smuzhiyun BUG_ON(!p_cluster);
6432*4882a593Smuzhiyun
6433*4882a593Smuzhiyun if (xv->xr_list.l_tree_depth) {
6434*4882a593Smuzhiyun ret = ocfs2_insert_extent(handle,
6435*4882a593Smuzhiyun &data_et, cpos,
6436*4882a593Smuzhiyun ocfs2_clusters_to_blocks(
6437*4882a593Smuzhiyun args->old_inode->i_sb,
6438*4882a593Smuzhiyun p_cluster),
6439*4882a593Smuzhiyun num_clusters, ext_flags,
6440*4882a593Smuzhiyun meta_ac);
6441*4882a593Smuzhiyun if (ret) {
6442*4882a593Smuzhiyun mlog_errno(ret);
6443*4882a593Smuzhiyun goto out;
6444*4882a593Smuzhiyun }
6445*4882a593Smuzhiyun }
6446*4882a593Smuzhiyun
6447*4882a593Smuzhiyun ret = ocfs2_increase_refcount(handle, args->ref_ci,
6448*4882a593Smuzhiyun args->ref_root_bh,
6449*4882a593Smuzhiyun p_cluster, num_clusters,
6450*4882a593Smuzhiyun meta_ac, args->dealloc);
6451*4882a593Smuzhiyun if (ret) {
6452*4882a593Smuzhiyun mlog_errno(ret);
6453*4882a593Smuzhiyun goto out;
6454*4882a593Smuzhiyun }
6455*4882a593Smuzhiyun
6456*4882a593Smuzhiyun cpos += num_clusters;
6457*4882a593Smuzhiyun }
6458*4882a593Smuzhiyun }
6459*4882a593Smuzhiyun
6460*4882a593Smuzhiyun out:
6461*4882a593Smuzhiyun return ret;
6462*4882a593Smuzhiyun }
6463*4882a593Smuzhiyun
ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink * args)6464*4882a593Smuzhiyun static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6465*4882a593Smuzhiyun {
6466*4882a593Smuzhiyun int ret = 0, credits = 0;
6467*4882a593Smuzhiyun handle_t *handle;
6468*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6469*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6470*4882a593Smuzhiyun int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6471*4882a593Smuzhiyun int header_off = osb->sb->s_blocksize - inline_size;
6472*4882a593Smuzhiyun struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6473*4882a593Smuzhiyun (args->old_bh->b_data + header_off);
6474*4882a593Smuzhiyun struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6475*4882a593Smuzhiyun (args->new_bh->b_data + header_off);
6476*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac = NULL;
6477*4882a593Smuzhiyun struct ocfs2_inode_info *new_oi;
6478*4882a593Smuzhiyun struct ocfs2_dinode *new_di;
6479*4882a593Smuzhiyun struct ocfs2_xattr_value_buf vb = {
6480*4882a593Smuzhiyun .vb_bh = args->new_bh,
6481*4882a593Smuzhiyun .vb_access = ocfs2_journal_access_di,
6482*4882a593Smuzhiyun };
6483*4882a593Smuzhiyun
6484*4882a593Smuzhiyun ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6485*4882a593Smuzhiyun &credits, &meta_ac);
6486*4882a593Smuzhiyun if (ret) {
6487*4882a593Smuzhiyun mlog_errno(ret);
6488*4882a593Smuzhiyun goto out;
6489*4882a593Smuzhiyun }
6490*4882a593Smuzhiyun
6491*4882a593Smuzhiyun handle = ocfs2_start_trans(osb, credits);
6492*4882a593Smuzhiyun if (IS_ERR(handle)) {
6493*4882a593Smuzhiyun ret = PTR_ERR(handle);
6494*4882a593Smuzhiyun mlog_errno(ret);
6495*4882a593Smuzhiyun goto out;
6496*4882a593Smuzhiyun }
6497*4882a593Smuzhiyun
6498*4882a593Smuzhiyun ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6499*4882a593Smuzhiyun args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6500*4882a593Smuzhiyun if (ret) {
6501*4882a593Smuzhiyun mlog_errno(ret);
6502*4882a593Smuzhiyun goto out_commit;
6503*4882a593Smuzhiyun }
6504*4882a593Smuzhiyun
6505*4882a593Smuzhiyun memcpy(args->new_bh->b_data + header_off,
6506*4882a593Smuzhiyun args->old_bh->b_data + header_off, inline_size);
6507*4882a593Smuzhiyun
6508*4882a593Smuzhiyun new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6509*4882a593Smuzhiyun new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6510*4882a593Smuzhiyun
6511*4882a593Smuzhiyun ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6512*4882a593Smuzhiyun args->new_bh, new_xh, &vb, meta_ac,
6513*4882a593Smuzhiyun ocfs2_get_xattr_value_root, NULL);
6514*4882a593Smuzhiyun if (ret) {
6515*4882a593Smuzhiyun mlog_errno(ret);
6516*4882a593Smuzhiyun goto out_commit;
6517*4882a593Smuzhiyun }
6518*4882a593Smuzhiyun
6519*4882a593Smuzhiyun new_oi = OCFS2_I(args->new_inode);
6520*4882a593Smuzhiyun /*
6521*4882a593Smuzhiyun * Adjust extent record count to reserve space for extended attribute.
6522*4882a593Smuzhiyun * Inline data count had been adjusted in ocfs2_duplicate_inline_data().
6523*4882a593Smuzhiyun */
6524*4882a593Smuzhiyun if (!(new_oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) &&
6525*4882a593Smuzhiyun !(ocfs2_inode_is_fast_symlink(args->new_inode))) {
6526*4882a593Smuzhiyun struct ocfs2_extent_list *el = &new_di->id2.i_list;
6527*4882a593Smuzhiyun le16_add_cpu(&el->l_count, -(inline_size /
6528*4882a593Smuzhiyun sizeof(struct ocfs2_extent_rec)));
6529*4882a593Smuzhiyun }
6530*4882a593Smuzhiyun spin_lock(&new_oi->ip_lock);
6531*4882a593Smuzhiyun new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6532*4882a593Smuzhiyun new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6533*4882a593Smuzhiyun spin_unlock(&new_oi->ip_lock);
6534*4882a593Smuzhiyun
6535*4882a593Smuzhiyun ocfs2_journal_dirty(handle, args->new_bh);
6536*4882a593Smuzhiyun
6537*4882a593Smuzhiyun out_commit:
6538*4882a593Smuzhiyun ocfs2_commit_trans(osb, handle);
6539*4882a593Smuzhiyun
6540*4882a593Smuzhiyun out:
6541*4882a593Smuzhiyun if (meta_ac)
6542*4882a593Smuzhiyun ocfs2_free_alloc_context(meta_ac);
6543*4882a593Smuzhiyun return ret;
6544*4882a593Smuzhiyun }
6545*4882a593Smuzhiyun
ocfs2_create_empty_xattr_block(struct inode * inode,struct buffer_head * fe_bh,struct buffer_head ** ret_bh,int indexed)6546*4882a593Smuzhiyun static int ocfs2_create_empty_xattr_block(struct inode *inode,
6547*4882a593Smuzhiyun struct buffer_head *fe_bh,
6548*4882a593Smuzhiyun struct buffer_head **ret_bh,
6549*4882a593Smuzhiyun int indexed)
6550*4882a593Smuzhiyun {
6551*4882a593Smuzhiyun int ret;
6552*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6553*4882a593Smuzhiyun struct ocfs2_xattr_set_ctxt ctxt;
6554*4882a593Smuzhiyun
6555*4882a593Smuzhiyun memset(&ctxt, 0, sizeof(ctxt));
6556*4882a593Smuzhiyun ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &ctxt.meta_ac);
6557*4882a593Smuzhiyun if (ret < 0) {
6558*4882a593Smuzhiyun mlog_errno(ret);
6559*4882a593Smuzhiyun return ret;
6560*4882a593Smuzhiyun }
6561*4882a593Smuzhiyun
6562*4882a593Smuzhiyun ctxt.handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6563*4882a593Smuzhiyun if (IS_ERR(ctxt.handle)) {
6564*4882a593Smuzhiyun ret = PTR_ERR(ctxt.handle);
6565*4882a593Smuzhiyun mlog_errno(ret);
6566*4882a593Smuzhiyun goto out;
6567*4882a593Smuzhiyun }
6568*4882a593Smuzhiyun
6569*4882a593Smuzhiyun trace_ocfs2_create_empty_xattr_block(
6570*4882a593Smuzhiyun (unsigned long long)fe_bh->b_blocknr, indexed);
6571*4882a593Smuzhiyun ret = ocfs2_create_xattr_block(inode, fe_bh, &ctxt, indexed,
6572*4882a593Smuzhiyun ret_bh);
6573*4882a593Smuzhiyun if (ret)
6574*4882a593Smuzhiyun mlog_errno(ret);
6575*4882a593Smuzhiyun
6576*4882a593Smuzhiyun ocfs2_commit_trans(osb, ctxt.handle);
6577*4882a593Smuzhiyun out:
6578*4882a593Smuzhiyun ocfs2_free_alloc_context(ctxt.meta_ac);
6579*4882a593Smuzhiyun return ret;
6580*4882a593Smuzhiyun }
6581*4882a593Smuzhiyun
ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink * args,struct buffer_head * blk_bh,struct buffer_head * new_blk_bh)6582*4882a593Smuzhiyun static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6583*4882a593Smuzhiyun struct buffer_head *blk_bh,
6584*4882a593Smuzhiyun struct buffer_head *new_blk_bh)
6585*4882a593Smuzhiyun {
6586*4882a593Smuzhiyun int ret = 0, credits = 0;
6587*4882a593Smuzhiyun handle_t *handle;
6588*4882a593Smuzhiyun struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6589*4882a593Smuzhiyun struct ocfs2_dinode *new_di;
6590*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6591*4882a593Smuzhiyun int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6592*4882a593Smuzhiyun struct ocfs2_xattr_block *xb =
6593*4882a593Smuzhiyun (struct ocfs2_xattr_block *)blk_bh->b_data;
6594*4882a593Smuzhiyun struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6595*4882a593Smuzhiyun struct ocfs2_xattr_block *new_xb =
6596*4882a593Smuzhiyun (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6597*4882a593Smuzhiyun struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6598*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac;
6599*4882a593Smuzhiyun struct ocfs2_xattr_value_buf vb = {
6600*4882a593Smuzhiyun .vb_bh = new_blk_bh,
6601*4882a593Smuzhiyun .vb_access = ocfs2_journal_access_xb,
6602*4882a593Smuzhiyun };
6603*4882a593Smuzhiyun
6604*4882a593Smuzhiyun ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6605*4882a593Smuzhiyun &credits, &meta_ac);
6606*4882a593Smuzhiyun if (ret) {
6607*4882a593Smuzhiyun mlog_errno(ret);
6608*4882a593Smuzhiyun return ret;
6609*4882a593Smuzhiyun }
6610*4882a593Smuzhiyun
6611*4882a593Smuzhiyun /* One more credits in case we need to add xattr flags in new inode. */
6612*4882a593Smuzhiyun handle = ocfs2_start_trans(osb, credits + 1);
6613*4882a593Smuzhiyun if (IS_ERR(handle)) {
6614*4882a593Smuzhiyun ret = PTR_ERR(handle);
6615*4882a593Smuzhiyun mlog_errno(ret);
6616*4882a593Smuzhiyun goto out;
6617*4882a593Smuzhiyun }
6618*4882a593Smuzhiyun
6619*4882a593Smuzhiyun if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6620*4882a593Smuzhiyun ret = ocfs2_journal_access_di(handle,
6621*4882a593Smuzhiyun INODE_CACHE(args->new_inode),
6622*4882a593Smuzhiyun args->new_bh,
6623*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
6624*4882a593Smuzhiyun if (ret) {
6625*4882a593Smuzhiyun mlog_errno(ret);
6626*4882a593Smuzhiyun goto out_commit;
6627*4882a593Smuzhiyun }
6628*4882a593Smuzhiyun }
6629*4882a593Smuzhiyun
6630*4882a593Smuzhiyun ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6631*4882a593Smuzhiyun new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6632*4882a593Smuzhiyun if (ret) {
6633*4882a593Smuzhiyun mlog_errno(ret);
6634*4882a593Smuzhiyun goto out_commit;
6635*4882a593Smuzhiyun }
6636*4882a593Smuzhiyun
6637*4882a593Smuzhiyun memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6638*4882a593Smuzhiyun osb->sb->s_blocksize - header_off);
6639*4882a593Smuzhiyun
6640*4882a593Smuzhiyun ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6641*4882a593Smuzhiyun new_blk_bh, new_xh, &vb, meta_ac,
6642*4882a593Smuzhiyun ocfs2_get_xattr_value_root, NULL);
6643*4882a593Smuzhiyun if (ret) {
6644*4882a593Smuzhiyun mlog_errno(ret);
6645*4882a593Smuzhiyun goto out_commit;
6646*4882a593Smuzhiyun }
6647*4882a593Smuzhiyun
6648*4882a593Smuzhiyun ocfs2_journal_dirty(handle, new_blk_bh);
6649*4882a593Smuzhiyun
6650*4882a593Smuzhiyun if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6651*4882a593Smuzhiyun new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6652*4882a593Smuzhiyun spin_lock(&new_oi->ip_lock);
6653*4882a593Smuzhiyun new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6654*4882a593Smuzhiyun new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6655*4882a593Smuzhiyun spin_unlock(&new_oi->ip_lock);
6656*4882a593Smuzhiyun
6657*4882a593Smuzhiyun ocfs2_journal_dirty(handle, args->new_bh);
6658*4882a593Smuzhiyun }
6659*4882a593Smuzhiyun
6660*4882a593Smuzhiyun out_commit:
6661*4882a593Smuzhiyun ocfs2_commit_trans(osb, handle);
6662*4882a593Smuzhiyun
6663*4882a593Smuzhiyun out:
6664*4882a593Smuzhiyun ocfs2_free_alloc_context(meta_ac);
6665*4882a593Smuzhiyun return ret;
6666*4882a593Smuzhiyun }
6667*4882a593Smuzhiyun
6668*4882a593Smuzhiyun struct ocfs2_reflink_xattr_tree_args {
6669*4882a593Smuzhiyun struct ocfs2_xattr_reflink *reflink;
6670*4882a593Smuzhiyun struct buffer_head *old_blk_bh;
6671*4882a593Smuzhiyun struct buffer_head *new_blk_bh;
6672*4882a593Smuzhiyun struct ocfs2_xattr_bucket *old_bucket;
6673*4882a593Smuzhiyun struct ocfs2_xattr_bucket *new_bucket;
6674*4882a593Smuzhiyun };
6675*4882a593Smuzhiyun
6676*4882a593Smuzhiyun /*
6677*4882a593Smuzhiyun * NOTE:
6678*4882a593Smuzhiyun * We have to handle the case that both old bucket and new bucket
6679*4882a593Smuzhiyun * will call this function to get the right ret_bh.
6680*4882a593Smuzhiyun * So The caller must give us the right bh.
6681*4882a593Smuzhiyun */
ocfs2_get_reflink_xattr_value_root(struct super_block * sb,struct buffer_head * bh,struct ocfs2_xattr_header * xh,int offset,struct ocfs2_xattr_value_root ** xv,struct buffer_head ** ret_bh,void * para)6682*4882a593Smuzhiyun static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6683*4882a593Smuzhiyun struct buffer_head *bh,
6684*4882a593Smuzhiyun struct ocfs2_xattr_header *xh,
6685*4882a593Smuzhiyun int offset,
6686*4882a593Smuzhiyun struct ocfs2_xattr_value_root **xv,
6687*4882a593Smuzhiyun struct buffer_head **ret_bh,
6688*4882a593Smuzhiyun void *para)
6689*4882a593Smuzhiyun {
6690*4882a593Smuzhiyun struct ocfs2_reflink_xattr_tree_args *args =
6691*4882a593Smuzhiyun (struct ocfs2_reflink_xattr_tree_args *)para;
6692*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket;
6693*4882a593Smuzhiyun
6694*4882a593Smuzhiyun if (bh == args->old_bucket->bu_bhs[0])
6695*4882a593Smuzhiyun bucket = args->old_bucket;
6696*4882a593Smuzhiyun else
6697*4882a593Smuzhiyun bucket = args->new_bucket;
6698*4882a593Smuzhiyun
6699*4882a593Smuzhiyun return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6700*4882a593Smuzhiyun xv, ret_bh);
6701*4882a593Smuzhiyun }
6702*4882a593Smuzhiyun
6703*4882a593Smuzhiyun struct ocfs2_value_tree_metas {
6704*4882a593Smuzhiyun int num_metas;
6705*4882a593Smuzhiyun int credits;
6706*4882a593Smuzhiyun int num_recs;
6707*4882a593Smuzhiyun };
6708*4882a593Smuzhiyun
ocfs2_value_tree_metas_in_bucket(struct super_block * sb,struct buffer_head * bh,struct ocfs2_xattr_header * xh,int offset,struct ocfs2_xattr_value_root ** xv,struct buffer_head ** ret_bh,void * para)6709*4882a593Smuzhiyun static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6710*4882a593Smuzhiyun struct buffer_head *bh,
6711*4882a593Smuzhiyun struct ocfs2_xattr_header *xh,
6712*4882a593Smuzhiyun int offset,
6713*4882a593Smuzhiyun struct ocfs2_xattr_value_root **xv,
6714*4882a593Smuzhiyun struct buffer_head **ret_bh,
6715*4882a593Smuzhiyun void *para)
6716*4882a593Smuzhiyun {
6717*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket =
6718*4882a593Smuzhiyun (struct ocfs2_xattr_bucket *)para;
6719*4882a593Smuzhiyun
6720*4882a593Smuzhiyun return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6721*4882a593Smuzhiyun xv, ret_bh);
6722*4882a593Smuzhiyun }
6723*4882a593Smuzhiyun
ocfs2_calc_value_tree_metas(struct inode * inode,struct ocfs2_xattr_bucket * bucket,void * para)6724*4882a593Smuzhiyun static int ocfs2_calc_value_tree_metas(struct inode *inode,
6725*4882a593Smuzhiyun struct ocfs2_xattr_bucket *bucket,
6726*4882a593Smuzhiyun void *para)
6727*4882a593Smuzhiyun {
6728*4882a593Smuzhiyun struct ocfs2_value_tree_metas *metas =
6729*4882a593Smuzhiyun (struct ocfs2_value_tree_metas *)para;
6730*4882a593Smuzhiyun struct ocfs2_xattr_header *xh =
6731*4882a593Smuzhiyun (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6732*4882a593Smuzhiyun
6733*4882a593Smuzhiyun /* Add the credits for this bucket first. */
6734*4882a593Smuzhiyun metas->credits += bucket->bu_blocks;
6735*4882a593Smuzhiyun return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6736*4882a593Smuzhiyun xh, &metas->num_metas,
6737*4882a593Smuzhiyun &metas->credits, &metas->num_recs,
6738*4882a593Smuzhiyun ocfs2_value_tree_metas_in_bucket,
6739*4882a593Smuzhiyun bucket);
6740*4882a593Smuzhiyun }
6741*4882a593Smuzhiyun
6742*4882a593Smuzhiyun /*
6743*4882a593Smuzhiyun * Given a xattr extent rec starting from blkno and having len clusters,
6744*4882a593Smuzhiyun * iterate all the buckets calculate how much metadata we need for reflinking
6745*4882a593Smuzhiyun * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6746*4882a593Smuzhiyun */
ocfs2_lock_reflink_xattr_rec_allocators(struct ocfs2_reflink_xattr_tree_args * args,struct ocfs2_extent_tree * xt_et,u64 blkno,u32 len,int * credits,struct ocfs2_alloc_context ** meta_ac,struct ocfs2_alloc_context ** data_ac)6747*4882a593Smuzhiyun static int ocfs2_lock_reflink_xattr_rec_allocators(
6748*4882a593Smuzhiyun struct ocfs2_reflink_xattr_tree_args *args,
6749*4882a593Smuzhiyun struct ocfs2_extent_tree *xt_et,
6750*4882a593Smuzhiyun u64 blkno, u32 len, int *credits,
6751*4882a593Smuzhiyun struct ocfs2_alloc_context **meta_ac,
6752*4882a593Smuzhiyun struct ocfs2_alloc_context **data_ac)
6753*4882a593Smuzhiyun {
6754*4882a593Smuzhiyun int ret, num_free_extents;
6755*4882a593Smuzhiyun struct ocfs2_value_tree_metas metas;
6756*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6757*4882a593Smuzhiyun struct ocfs2_refcount_block *rb;
6758*4882a593Smuzhiyun
6759*4882a593Smuzhiyun memset(&metas, 0, sizeof(metas));
6760*4882a593Smuzhiyun
6761*4882a593Smuzhiyun ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6762*4882a593Smuzhiyun ocfs2_calc_value_tree_metas, &metas);
6763*4882a593Smuzhiyun if (ret) {
6764*4882a593Smuzhiyun mlog_errno(ret);
6765*4882a593Smuzhiyun goto out;
6766*4882a593Smuzhiyun }
6767*4882a593Smuzhiyun
6768*4882a593Smuzhiyun *credits = metas.credits;
6769*4882a593Smuzhiyun
6770*4882a593Smuzhiyun /*
6771*4882a593Smuzhiyun * Calculate we need for refcount tree change.
6772*4882a593Smuzhiyun *
6773*4882a593Smuzhiyun * We need to add/modify num_recs in refcount tree, so just calculate
6774*4882a593Smuzhiyun * an approximate number we need for refcount tree change.
6775*4882a593Smuzhiyun * Sometimes we need to split the tree, and after split, half recs
6776*4882a593Smuzhiyun * will be moved to the new block, and a new block can only provide
6777*4882a593Smuzhiyun * half number of recs. So we multiple new blocks by 2.
6778*4882a593Smuzhiyun * In the end, we have to add credits for modifying the already
6779*4882a593Smuzhiyun * existed refcount block.
6780*4882a593Smuzhiyun */
6781*4882a593Smuzhiyun rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6782*4882a593Smuzhiyun metas.num_recs =
6783*4882a593Smuzhiyun (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6784*4882a593Smuzhiyun ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6785*4882a593Smuzhiyun metas.num_metas += metas.num_recs;
6786*4882a593Smuzhiyun *credits += metas.num_recs +
6787*4882a593Smuzhiyun metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6788*4882a593Smuzhiyun if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6789*4882a593Smuzhiyun *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6790*4882a593Smuzhiyun le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6791*4882a593Smuzhiyun else
6792*4882a593Smuzhiyun *credits += 1;
6793*4882a593Smuzhiyun
6794*4882a593Smuzhiyun /* count in the xattr tree change. */
6795*4882a593Smuzhiyun num_free_extents = ocfs2_num_free_extents(xt_et);
6796*4882a593Smuzhiyun if (num_free_extents < 0) {
6797*4882a593Smuzhiyun ret = num_free_extents;
6798*4882a593Smuzhiyun mlog_errno(ret);
6799*4882a593Smuzhiyun goto out;
6800*4882a593Smuzhiyun }
6801*4882a593Smuzhiyun
6802*4882a593Smuzhiyun if (num_free_extents < len)
6803*4882a593Smuzhiyun metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6804*4882a593Smuzhiyun
6805*4882a593Smuzhiyun *credits += ocfs2_calc_extend_credits(osb->sb,
6806*4882a593Smuzhiyun xt_et->et_root_el);
6807*4882a593Smuzhiyun
6808*4882a593Smuzhiyun if (metas.num_metas) {
6809*4882a593Smuzhiyun ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6810*4882a593Smuzhiyun meta_ac);
6811*4882a593Smuzhiyun if (ret) {
6812*4882a593Smuzhiyun mlog_errno(ret);
6813*4882a593Smuzhiyun goto out;
6814*4882a593Smuzhiyun }
6815*4882a593Smuzhiyun }
6816*4882a593Smuzhiyun
6817*4882a593Smuzhiyun if (len) {
6818*4882a593Smuzhiyun ret = ocfs2_reserve_clusters(osb, len, data_ac);
6819*4882a593Smuzhiyun if (ret)
6820*4882a593Smuzhiyun mlog_errno(ret);
6821*4882a593Smuzhiyun }
6822*4882a593Smuzhiyun out:
6823*4882a593Smuzhiyun if (ret) {
6824*4882a593Smuzhiyun if (*meta_ac) {
6825*4882a593Smuzhiyun ocfs2_free_alloc_context(*meta_ac);
6826*4882a593Smuzhiyun *meta_ac = NULL;
6827*4882a593Smuzhiyun }
6828*4882a593Smuzhiyun }
6829*4882a593Smuzhiyun
6830*4882a593Smuzhiyun return ret;
6831*4882a593Smuzhiyun }
6832*4882a593Smuzhiyun
ocfs2_reflink_xattr_bucket(handle_t * handle,u64 blkno,u64 new_blkno,u32 clusters,u32 * cpos,int num_buckets,struct ocfs2_alloc_context * meta_ac,struct ocfs2_alloc_context * data_ac,struct ocfs2_reflink_xattr_tree_args * args)6833*4882a593Smuzhiyun static int ocfs2_reflink_xattr_bucket(handle_t *handle,
6834*4882a593Smuzhiyun u64 blkno, u64 new_blkno, u32 clusters,
6835*4882a593Smuzhiyun u32 *cpos, int num_buckets,
6836*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac,
6837*4882a593Smuzhiyun struct ocfs2_alloc_context *data_ac,
6838*4882a593Smuzhiyun struct ocfs2_reflink_xattr_tree_args *args)
6839*4882a593Smuzhiyun {
6840*4882a593Smuzhiyun int i, j, ret = 0;
6841*4882a593Smuzhiyun struct super_block *sb = args->reflink->old_inode->i_sb;
6842*4882a593Smuzhiyun int bpb = args->old_bucket->bu_blocks;
6843*4882a593Smuzhiyun struct ocfs2_xattr_value_buf vb = {
6844*4882a593Smuzhiyun .vb_access = ocfs2_journal_access,
6845*4882a593Smuzhiyun };
6846*4882a593Smuzhiyun
6847*4882a593Smuzhiyun for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6848*4882a593Smuzhiyun ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6849*4882a593Smuzhiyun if (ret) {
6850*4882a593Smuzhiyun mlog_errno(ret);
6851*4882a593Smuzhiyun break;
6852*4882a593Smuzhiyun }
6853*4882a593Smuzhiyun
6854*4882a593Smuzhiyun ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno, 1);
6855*4882a593Smuzhiyun if (ret) {
6856*4882a593Smuzhiyun mlog_errno(ret);
6857*4882a593Smuzhiyun break;
6858*4882a593Smuzhiyun }
6859*4882a593Smuzhiyun
6860*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_journal_access(handle,
6861*4882a593Smuzhiyun args->new_bucket,
6862*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_CREATE);
6863*4882a593Smuzhiyun if (ret) {
6864*4882a593Smuzhiyun mlog_errno(ret);
6865*4882a593Smuzhiyun break;
6866*4882a593Smuzhiyun }
6867*4882a593Smuzhiyun
6868*4882a593Smuzhiyun for (j = 0; j < bpb; j++)
6869*4882a593Smuzhiyun memcpy(bucket_block(args->new_bucket, j),
6870*4882a593Smuzhiyun bucket_block(args->old_bucket, j),
6871*4882a593Smuzhiyun sb->s_blocksize);
6872*4882a593Smuzhiyun
6873*4882a593Smuzhiyun /*
6874*4882a593Smuzhiyun * Record the start cpos so that we can use it to initialize
6875*4882a593Smuzhiyun * our xattr tree we also set the xh_num_bucket for the new
6876*4882a593Smuzhiyun * bucket.
6877*4882a593Smuzhiyun */
6878*4882a593Smuzhiyun if (i == 0) {
6879*4882a593Smuzhiyun *cpos = le32_to_cpu(bucket_xh(args->new_bucket)->
6880*4882a593Smuzhiyun xh_entries[0].xe_name_hash);
6881*4882a593Smuzhiyun bucket_xh(args->new_bucket)->xh_num_buckets =
6882*4882a593Smuzhiyun cpu_to_le16(num_buckets);
6883*4882a593Smuzhiyun }
6884*4882a593Smuzhiyun
6885*4882a593Smuzhiyun ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6886*4882a593Smuzhiyun
6887*4882a593Smuzhiyun ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6888*4882a593Smuzhiyun args->old_bucket->bu_bhs[0],
6889*4882a593Smuzhiyun bucket_xh(args->old_bucket),
6890*4882a593Smuzhiyun args->new_bucket->bu_bhs[0],
6891*4882a593Smuzhiyun bucket_xh(args->new_bucket),
6892*4882a593Smuzhiyun &vb, meta_ac,
6893*4882a593Smuzhiyun ocfs2_get_reflink_xattr_value_root,
6894*4882a593Smuzhiyun args);
6895*4882a593Smuzhiyun if (ret) {
6896*4882a593Smuzhiyun mlog_errno(ret);
6897*4882a593Smuzhiyun break;
6898*4882a593Smuzhiyun }
6899*4882a593Smuzhiyun
6900*4882a593Smuzhiyun /*
6901*4882a593Smuzhiyun * Re-access and dirty the bucket to calculate metaecc.
6902*4882a593Smuzhiyun * Because we may extend the transaction in reflink_xattr_header
6903*4882a593Smuzhiyun * which will let the already accessed block gone.
6904*4882a593Smuzhiyun */
6905*4882a593Smuzhiyun ret = ocfs2_xattr_bucket_journal_access(handle,
6906*4882a593Smuzhiyun args->new_bucket,
6907*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
6908*4882a593Smuzhiyun if (ret) {
6909*4882a593Smuzhiyun mlog_errno(ret);
6910*4882a593Smuzhiyun break;
6911*4882a593Smuzhiyun }
6912*4882a593Smuzhiyun
6913*4882a593Smuzhiyun ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6914*4882a593Smuzhiyun
6915*4882a593Smuzhiyun ocfs2_xattr_bucket_relse(args->old_bucket);
6916*4882a593Smuzhiyun ocfs2_xattr_bucket_relse(args->new_bucket);
6917*4882a593Smuzhiyun }
6918*4882a593Smuzhiyun
6919*4882a593Smuzhiyun ocfs2_xattr_bucket_relse(args->old_bucket);
6920*4882a593Smuzhiyun ocfs2_xattr_bucket_relse(args->new_bucket);
6921*4882a593Smuzhiyun return ret;
6922*4882a593Smuzhiyun }
6923*4882a593Smuzhiyun
ocfs2_reflink_xattr_buckets(handle_t * handle,struct inode * inode,struct ocfs2_reflink_xattr_tree_args * args,struct ocfs2_extent_tree * et,struct ocfs2_alloc_context * meta_ac,struct ocfs2_alloc_context * data_ac,u64 blkno,u32 cpos,u32 len)6924*4882a593Smuzhiyun static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6925*4882a593Smuzhiyun struct inode *inode,
6926*4882a593Smuzhiyun struct ocfs2_reflink_xattr_tree_args *args,
6927*4882a593Smuzhiyun struct ocfs2_extent_tree *et,
6928*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac,
6929*4882a593Smuzhiyun struct ocfs2_alloc_context *data_ac,
6930*4882a593Smuzhiyun u64 blkno, u32 cpos, u32 len)
6931*4882a593Smuzhiyun {
6932*4882a593Smuzhiyun int ret, first_inserted = 0;
6933*4882a593Smuzhiyun u32 p_cluster, num_clusters, reflink_cpos = 0;
6934*4882a593Smuzhiyun u64 new_blkno;
6935*4882a593Smuzhiyun unsigned int num_buckets, reflink_buckets;
6936*4882a593Smuzhiyun unsigned int bpc =
6937*4882a593Smuzhiyun ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
6938*4882a593Smuzhiyun
6939*4882a593Smuzhiyun ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6940*4882a593Smuzhiyun if (ret) {
6941*4882a593Smuzhiyun mlog_errno(ret);
6942*4882a593Smuzhiyun goto out;
6943*4882a593Smuzhiyun }
6944*4882a593Smuzhiyun num_buckets = le16_to_cpu(bucket_xh(args->old_bucket)->xh_num_buckets);
6945*4882a593Smuzhiyun ocfs2_xattr_bucket_relse(args->old_bucket);
6946*4882a593Smuzhiyun
6947*4882a593Smuzhiyun while (len && num_buckets) {
6948*4882a593Smuzhiyun ret = ocfs2_claim_clusters(handle, data_ac,
6949*4882a593Smuzhiyun 1, &p_cluster, &num_clusters);
6950*4882a593Smuzhiyun if (ret) {
6951*4882a593Smuzhiyun mlog_errno(ret);
6952*4882a593Smuzhiyun goto out;
6953*4882a593Smuzhiyun }
6954*4882a593Smuzhiyun
6955*4882a593Smuzhiyun new_blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
6956*4882a593Smuzhiyun reflink_buckets = min(num_buckets, bpc * num_clusters);
6957*4882a593Smuzhiyun
6958*4882a593Smuzhiyun ret = ocfs2_reflink_xattr_bucket(handle, blkno,
6959*4882a593Smuzhiyun new_blkno, num_clusters,
6960*4882a593Smuzhiyun &reflink_cpos, reflink_buckets,
6961*4882a593Smuzhiyun meta_ac, data_ac, args);
6962*4882a593Smuzhiyun if (ret) {
6963*4882a593Smuzhiyun mlog_errno(ret);
6964*4882a593Smuzhiyun goto out;
6965*4882a593Smuzhiyun }
6966*4882a593Smuzhiyun
6967*4882a593Smuzhiyun /*
6968*4882a593Smuzhiyun * For the 1st allocated cluster, we make it use the same cpos
6969*4882a593Smuzhiyun * so that the xattr tree looks the same as the original one
6970*4882a593Smuzhiyun * in the most case.
6971*4882a593Smuzhiyun */
6972*4882a593Smuzhiyun if (!first_inserted) {
6973*4882a593Smuzhiyun reflink_cpos = cpos;
6974*4882a593Smuzhiyun first_inserted = 1;
6975*4882a593Smuzhiyun }
6976*4882a593Smuzhiyun ret = ocfs2_insert_extent(handle, et, reflink_cpos, new_blkno,
6977*4882a593Smuzhiyun num_clusters, 0, meta_ac);
6978*4882a593Smuzhiyun if (ret)
6979*4882a593Smuzhiyun mlog_errno(ret);
6980*4882a593Smuzhiyun
6981*4882a593Smuzhiyun trace_ocfs2_reflink_xattr_buckets((unsigned long long)new_blkno,
6982*4882a593Smuzhiyun num_clusters, reflink_cpos);
6983*4882a593Smuzhiyun
6984*4882a593Smuzhiyun len -= num_clusters;
6985*4882a593Smuzhiyun blkno += ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
6986*4882a593Smuzhiyun num_buckets -= reflink_buckets;
6987*4882a593Smuzhiyun }
6988*4882a593Smuzhiyun out:
6989*4882a593Smuzhiyun return ret;
6990*4882a593Smuzhiyun }
6991*4882a593Smuzhiyun
6992*4882a593Smuzhiyun /*
6993*4882a593Smuzhiyun * Create the same xattr extent record in the new inode's xattr tree.
6994*4882a593Smuzhiyun */
ocfs2_reflink_xattr_rec(struct inode * inode,struct buffer_head * root_bh,u64 blkno,u32 cpos,u32 len,void * para)6995*4882a593Smuzhiyun static int ocfs2_reflink_xattr_rec(struct inode *inode,
6996*4882a593Smuzhiyun struct buffer_head *root_bh,
6997*4882a593Smuzhiyun u64 blkno,
6998*4882a593Smuzhiyun u32 cpos,
6999*4882a593Smuzhiyun u32 len,
7000*4882a593Smuzhiyun void *para)
7001*4882a593Smuzhiyun {
7002*4882a593Smuzhiyun int ret, credits = 0;
7003*4882a593Smuzhiyun handle_t *handle;
7004*4882a593Smuzhiyun struct ocfs2_reflink_xattr_tree_args *args =
7005*4882a593Smuzhiyun (struct ocfs2_reflink_xattr_tree_args *)para;
7006*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7007*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac = NULL;
7008*4882a593Smuzhiyun struct ocfs2_alloc_context *data_ac = NULL;
7009*4882a593Smuzhiyun struct ocfs2_extent_tree et;
7010*4882a593Smuzhiyun
7011*4882a593Smuzhiyun trace_ocfs2_reflink_xattr_rec((unsigned long long)blkno, len);
7012*4882a593Smuzhiyun
7013*4882a593Smuzhiyun ocfs2_init_xattr_tree_extent_tree(&et,
7014*4882a593Smuzhiyun INODE_CACHE(args->reflink->new_inode),
7015*4882a593Smuzhiyun args->new_blk_bh);
7016*4882a593Smuzhiyun
7017*4882a593Smuzhiyun ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
7018*4882a593Smuzhiyun len, &credits,
7019*4882a593Smuzhiyun &meta_ac, &data_ac);
7020*4882a593Smuzhiyun if (ret) {
7021*4882a593Smuzhiyun mlog_errno(ret);
7022*4882a593Smuzhiyun goto out;
7023*4882a593Smuzhiyun }
7024*4882a593Smuzhiyun
7025*4882a593Smuzhiyun handle = ocfs2_start_trans(osb, credits);
7026*4882a593Smuzhiyun if (IS_ERR(handle)) {
7027*4882a593Smuzhiyun ret = PTR_ERR(handle);
7028*4882a593Smuzhiyun mlog_errno(ret);
7029*4882a593Smuzhiyun goto out;
7030*4882a593Smuzhiyun }
7031*4882a593Smuzhiyun
7032*4882a593Smuzhiyun ret = ocfs2_reflink_xattr_buckets(handle, inode, args, &et,
7033*4882a593Smuzhiyun meta_ac, data_ac,
7034*4882a593Smuzhiyun blkno, cpos, len);
7035*4882a593Smuzhiyun if (ret)
7036*4882a593Smuzhiyun mlog_errno(ret);
7037*4882a593Smuzhiyun
7038*4882a593Smuzhiyun ocfs2_commit_trans(osb, handle);
7039*4882a593Smuzhiyun
7040*4882a593Smuzhiyun out:
7041*4882a593Smuzhiyun if (meta_ac)
7042*4882a593Smuzhiyun ocfs2_free_alloc_context(meta_ac);
7043*4882a593Smuzhiyun if (data_ac)
7044*4882a593Smuzhiyun ocfs2_free_alloc_context(data_ac);
7045*4882a593Smuzhiyun return ret;
7046*4882a593Smuzhiyun }
7047*4882a593Smuzhiyun
7048*4882a593Smuzhiyun /*
7049*4882a593Smuzhiyun * Create reflinked xattr buckets.
7050*4882a593Smuzhiyun * We will add bucket one by one, and refcount all the xattrs in the bucket
7051*4882a593Smuzhiyun * if they are stored outside.
7052*4882a593Smuzhiyun */
ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink * args,struct buffer_head * blk_bh,struct buffer_head * new_blk_bh)7053*4882a593Smuzhiyun static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
7054*4882a593Smuzhiyun struct buffer_head *blk_bh,
7055*4882a593Smuzhiyun struct buffer_head *new_blk_bh)
7056*4882a593Smuzhiyun {
7057*4882a593Smuzhiyun int ret;
7058*4882a593Smuzhiyun struct ocfs2_reflink_xattr_tree_args para;
7059*4882a593Smuzhiyun
7060*4882a593Smuzhiyun memset(¶, 0, sizeof(para));
7061*4882a593Smuzhiyun para.reflink = args;
7062*4882a593Smuzhiyun para.old_blk_bh = blk_bh;
7063*4882a593Smuzhiyun para.new_blk_bh = new_blk_bh;
7064*4882a593Smuzhiyun
7065*4882a593Smuzhiyun para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
7066*4882a593Smuzhiyun if (!para.old_bucket) {
7067*4882a593Smuzhiyun mlog_errno(-ENOMEM);
7068*4882a593Smuzhiyun return -ENOMEM;
7069*4882a593Smuzhiyun }
7070*4882a593Smuzhiyun
7071*4882a593Smuzhiyun para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
7072*4882a593Smuzhiyun if (!para.new_bucket) {
7073*4882a593Smuzhiyun ret = -ENOMEM;
7074*4882a593Smuzhiyun mlog_errno(ret);
7075*4882a593Smuzhiyun goto out;
7076*4882a593Smuzhiyun }
7077*4882a593Smuzhiyun
7078*4882a593Smuzhiyun ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
7079*4882a593Smuzhiyun ocfs2_reflink_xattr_rec,
7080*4882a593Smuzhiyun ¶);
7081*4882a593Smuzhiyun if (ret)
7082*4882a593Smuzhiyun mlog_errno(ret);
7083*4882a593Smuzhiyun
7084*4882a593Smuzhiyun out:
7085*4882a593Smuzhiyun ocfs2_xattr_bucket_free(para.old_bucket);
7086*4882a593Smuzhiyun ocfs2_xattr_bucket_free(para.new_bucket);
7087*4882a593Smuzhiyun return ret;
7088*4882a593Smuzhiyun }
7089*4882a593Smuzhiyun
ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink * args,struct buffer_head * blk_bh)7090*4882a593Smuzhiyun static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
7091*4882a593Smuzhiyun struct buffer_head *blk_bh)
7092*4882a593Smuzhiyun {
7093*4882a593Smuzhiyun int ret, indexed = 0;
7094*4882a593Smuzhiyun struct buffer_head *new_blk_bh = NULL;
7095*4882a593Smuzhiyun struct ocfs2_xattr_block *xb =
7096*4882a593Smuzhiyun (struct ocfs2_xattr_block *)blk_bh->b_data;
7097*4882a593Smuzhiyun
7098*4882a593Smuzhiyun
7099*4882a593Smuzhiyun if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
7100*4882a593Smuzhiyun indexed = 1;
7101*4882a593Smuzhiyun
7102*4882a593Smuzhiyun ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
7103*4882a593Smuzhiyun &new_blk_bh, indexed);
7104*4882a593Smuzhiyun if (ret) {
7105*4882a593Smuzhiyun mlog_errno(ret);
7106*4882a593Smuzhiyun goto out;
7107*4882a593Smuzhiyun }
7108*4882a593Smuzhiyun
7109*4882a593Smuzhiyun if (!indexed)
7110*4882a593Smuzhiyun ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7111*4882a593Smuzhiyun else
7112*4882a593Smuzhiyun ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7113*4882a593Smuzhiyun if (ret)
7114*4882a593Smuzhiyun mlog_errno(ret);
7115*4882a593Smuzhiyun
7116*4882a593Smuzhiyun out:
7117*4882a593Smuzhiyun brelse(new_blk_bh);
7118*4882a593Smuzhiyun return ret;
7119*4882a593Smuzhiyun }
7120*4882a593Smuzhiyun
ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry * xe)7121*4882a593Smuzhiyun static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7122*4882a593Smuzhiyun {
7123*4882a593Smuzhiyun int type = ocfs2_xattr_get_type(xe);
7124*4882a593Smuzhiyun
7125*4882a593Smuzhiyun return type != OCFS2_XATTR_INDEX_SECURITY &&
7126*4882a593Smuzhiyun type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7127*4882a593Smuzhiyun type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7128*4882a593Smuzhiyun }
7129*4882a593Smuzhiyun
ocfs2_reflink_xattrs(struct inode * old_inode,struct buffer_head * old_bh,struct inode * new_inode,struct buffer_head * new_bh,bool preserve_security)7130*4882a593Smuzhiyun int ocfs2_reflink_xattrs(struct inode *old_inode,
7131*4882a593Smuzhiyun struct buffer_head *old_bh,
7132*4882a593Smuzhiyun struct inode *new_inode,
7133*4882a593Smuzhiyun struct buffer_head *new_bh,
7134*4882a593Smuzhiyun bool preserve_security)
7135*4882a593Smuzhiyun {
7136*4882a593Smuzhiyun int ret;
7137*4882a593Smuzhiyun struct ocfs2_xattr_reflink args;
7138*4882a593Smuzhiyun struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7139*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7140*4882a593Smuzhiyun struct buffer_head *blk_bh = NULL;
7141*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt dealloc;
7142*4882a593Smuzhiyun struct ocfs2_refcount_tree *ref_tree;
7143*4882a593Smuzhiyun struct buffer_head *ref_root_bh = NULL;
7144*4882a593Smuzhiyun
7145*4882a593Smuzhiyun ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7146*4882a593Smuzhiyun le64_to_cpu(di->i_refcount_loc),
7147*4882a593Smuzhiyun 1, &ref_tree, &ref_root_bh);
7148*4882a593Smuzhiyun if (ret) {
7149*4882a593Smuzhiyun mlog_errno(ret);
7150*4882a593Smuzhiyun goto out;
7151*4882a593Smuzhiyun }
7152*4882a593Smuzhiyun
7153*4882a593Smuzhiyun ocfs2_init_dealloc_ctxt(&dealloc);
7154*4882a593Smuzhiyun
7155*4882a593Smuzhiyun args.old_inode = old_inode;
7156*4882a593Smuzhiyun args.new_inode = new_inode;
7157*4882a593Smuzhiyun args.old_bh = old_bh;
7158*4882a593Smuzhiyun args.new_bh = new_bh;
7159*4882a593Smuzhiyun args.ref_ci = &ref_tree->rf_ci;
7160*4882a593Smuzhiyun args.ref_root_bh = ref_root_bh;
7161*4882a593Smuzhiyun args.dealloc = &dealloc;
7162*4882a593Smuzhiyun if (preserve_security)
7163*4882a593Smuzhiyun args.xattr_reflinked = NULL;
7164*4882a593Smuzhiyun else
7165*4882a593Smuzhiyun args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
7166*4882a593Smuzhiyun
7167*4882a593Smuzhiyun if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7168*4882a593Smuzhiyun ret = ocfs2_reflink_xattr_inline(&args);
7169*4882a593Smuzhiyun if (ret) {
7170*4882a593Smuzhiyun mlog_errno(ret);
7171*4882a593Smuzhiyun goto out_unlock;
7172*4882a593Smuzhiyun }
7173*4882a593Smuzhiyun }
7174*4882a593Smuzhiyun
7175*4882a593Smuzhiyun if (!di->i_xattr_loc)
7176*4882a593Smuzhiyun goto out_unlock;
7177*4882a593Smuzhiyun
7178*4882a593Smuzhiyun ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7179*4882a593Smuzhiyun &blk_bh);
7180*4882a593Smuzhiyun if (ret < 0) {
7181*4882a593Smuzhiyun mlog_errno(ret);
7182*4882a593Smuzhiyun goto out_unlock;
7183*4882a593Smuzhiyun }
7184*4882a593Smuzhiyun
7185*4882a593Smuzhiyun ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7186*4882a593Smuzhiyun if (ret)
7187*4882a593Smuzhiyun mlog_errno(ret);
7188*4882a593Smuzhiyun
7189*4882a593Smuzhiyun brelse(blk_bh);
7190*4882a593Smuzhiyun
7191*4882a593Smuzhiyun out_unlock:
7192*4882a593Smuzhiyun ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7193*4882a593Smuzhiyun ref_tree, 1);
7194*4882a593Smuzhiyun brelse(ref_root_bh);
7195*4882a593Smuzhiyun
7196*4882a593Smuzhiyun if (ocfs2_dealloc_has_cluster(&dealloc)) {
7197*4882a593Smuzhiyun ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7198*4882a593Smuzhiyun ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7199*4882a593Smuzhiyun }
7200*4882a593Smuzhiyun
7201*4882a593Smuzhiyun out:
7202*4882a593Smuzhiyun return ret;
7203*4882a593Smuzhiyun }
7204*4882a593Smuzhiyun
7205*4882a593Smuzhiyun /*
7206*4882a593Smuzhiyun * Initialize security and acl for a already created inode.
7207*4882a593Smuzhiyun * Used for reflink a non-preserve-security file.
7208*4882a593Smuzhiyun *
7209*4882a593Smuzhiyun * It uses common api like ocfs2_xattr_set, so the caller
7210*4882a593Smuzhiyun * must not hold any lock expect i_mutex.
7211*4882a593Smuzhiyun */
ocfs2_init_security_and_acl(struct inode * dir,struct inode * inode,const struct qstr * qstr)7212*4882a593Smuzhiyun int ocfs2_init_security_and_acl(struct inode *dir,
7213*4882a593Smuzhiyun struct inode *inode,
7214*4882a593Smuzhiyun const struct qstr *qstr)
7215*4882a593Smuzhiyun {
7216*4882a593Smuzhiyun int ret = 0;
7217*4882a593Smuzhiyun struct buffer_head *dir_bh = NULL;
7218*4882a593Smuzhiyun
7219*4882a593Smuzhiyun ret = ocfs2_init_security_get(inode, dir, qstr, NULL);
7220*4882a593Smuzhiyun if (ret) {
7221*4882a593Smuzhiyun mlog_errno(ret);
7222*4882a593Smuzhiyun goto leave;
7223*4882a593Smuzhiyun }
7224*4882a593Smuzhiyun
7225*4882a593Smuzhiyun ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7226*4882a593Smuzhiyun if (ret) {
7227*4882a593Smuzhiyun mlog_errno(ret);
7228*4882a593Smuzhiyun goto leave;
7229*4882a593Smuzhiyun }
7230*4882a593Smuzhiyun ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7231*4882a593Smuzhiyun if (ret)
7232*4882a593Smuzhiyun mlog_errno(ret);
7233*4882a593Smuzhiyun
7234*4882a593Smuzhiyun ocfs2_inode_unlock(dir, 0);
7235*4882a593Smuzhiyun brelse(dir_bh);
7236*4882a593Smuzhiyun leave:
7237*4882a593Smuzhiyun return ret;
7238*4882a593Smuzhiyun }
7239*4882a593Smuzhiyun
7240*4882a593Smuzhiyun /*
7241*4882a593Smuzhiyun * 'security' attributes support
7242*4882a593Smuzhiyun */
ocfs2_xattr_security_get(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * buffer,size_t size,int flags)7243*4882a593Smuzhiyun static int ocfs2_xattr_security_get(const struct xattr_handler *handler,
7244*4882a593Smuzhiyun struct dentry *unused, struct inode *inode,
7245*4882a593Smuzhiyun const char *name, void *buffer, size_t size,
7246*4882a593Smuzhiyun int flags)
7247*4882a593Smuzhiyun {
7248*4882a593Smuzhiyun return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY,
7249*4882a593Smuzhiyun name, buffer, size);
7250*4882a593Smuzhiyun }
7251*4882a593Smuzhiyun
ocfs2_xattr_security_set(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,const void * value,size_t size,int flags)7252*4882a593Smuzhiyun static int ocfs2_xattr_security_set(const struct xattr_handler *handler,
7253*4882a593Smuzhiyun struct dentry *unused, struct inode *inode,
7254*4882a593Smuzhiyun const char *name, const void *value,
7255*4882a593Smuzhiyun size_t size, int flags)
7256*4882a593Smuzhiyun {
7257*4882a593Smuzhiyun return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7258*4882a593Smuzhiyun name, value, size, flags);
7259*4882a593Smuzhiyun }
7260*4882a593Smuzhiyun
ocfs2_initxattrs(struct inode * inode,const struct xattr * xattr_array,void * fs_info)7261*4882a593Smuzhiyun static int ocfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
7262*4882a593Smuzhiyun void *fs_info)
7263*4882a593Smuzhiyun {
7264*4882a593Smuzhiyun const struct xattr *xattr;
7265*4882a593Smuzhiyun int err = 0;
7266*4882a593Smuzhiyun
7267*4882a593Smuzhiyun for (xattr = xattr_array; xattr->name != NULL; xattr++) {
7268*4882a593Smuzhiyun err = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7269*4882a593Smuzhiyun xattr->name, xattr->value,
7270*4882a593Smuzhiyun xattr->value_len, XATTR_CREATE);
7271*4882a593Smuzhiyun if (err)
7272*4882a593Smuzhiyun break;
7273*4882a593Smuzhiyun }
7274*4882a593Smuzhiyun return err;
7275*4882a593Smuzhiyun }
7276*4882a593Smuzhiyun
ocfs2_init_security_get(struct inode * inode,struct inode * dir,const struct qstr * qstr,struct ocfs2_security_xattr_info * si)7277*4882a593Smuzhiyun int ocfs2_init_security_get(struct inode *inode,
7278*4882a593Smuzhiyun struct inode *dir,
7279*4882a593Smuzhiyun const struct qstr *qstr,
7280*4882a593Smuzhiyun struct ocfs2_security_xattr_info *si)
7281*4882a593Smuzhiyun {
7282*4882a593Smuzhiyun /* check whether ocfs2 support feature xattr */
7283*4882a593Smuzhiyun if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7284*4882a593Smuzhiyun return -EOPNOTSUPP;
7285*4882a593Smuzhiyun if (si)
7286*4882a593Smuzhiyun return security_old_inode_init_security(inode, dir, qstr,
7287*4882a593Smuzhiyun &si->name, &si->value,
7288*4882a593Smuzhiyun &si->value_len);
7289*4882a593Smuzhiyun
7290*4882a593Smuzhiyun return security_inode_init_security(inode, dir, qstr,
7291*4882a593Smuzhiyun &ocfs2_initxattrs, NULL);
7292*4882a593Smuzhiyun }
7293*4882a593Smuzhiyun
ocfs2_init_security_set(handle_t * handle,struct inode * inode,struct buffer_head * di_bh,struct ocfs2_security_xattr_info * si,struct ocfs2_alloc_context * xattr_ac,struct ocfs2_alloc_context * data_ac)7294*4882a593Smuzhiyun int ocfs2_init_security_set(handle_t *handle,
7295*4882a593Smuzhiyun struct inode *inode,
7296*4882a593Smuzhiyun struct buffer_head *di_bh,
7297*4882a593Smuzhiyun struct ocfs2_security_xattr_info *si,
7298*4882a593Smuzhiyun struct ocfs2_alloc_context *xattr_ac,
7299*4882a593Smuzhiyun struct ocfs2_alloc_context *data_ac)
7300*4882a593Smuzhiyun {
7301*4882a593Smuzhiyun return ocfs2_xattr_set_handle(handle, inode, di_bh,
7302*4882a593Smuzhiyun OCFS2_XATTR_INDEX_SECURITY,
7303*4882a593Smuzhiyun si->name, si->value, si->value_len, 0,
7304*4882a593Smuzhiyun xattr_ac, data_ac);
7305*4882a593Smuzhiyun }
7306*4882a593Smuzhiyun
7307*4882a593Smuzhiyun const struct xattr_handler ocfs2_xattr_security_handler = {
7308*4882a593Smuzhiyun .prefix = XATTR_SECURITY_PREFIX,
7309*4882a593Smuzhiyun .get = ocfs2_xattr_security_get,
7310*4882a593Smuzhiyun .set = ocfs2_xattr_security_set,
7311*4882a593Smuzhiyun };
7312*4882a593Smuzhiyun
7313*4882a593Smuzhiyun /*
7314*4882a593Smuzhiyun * 'trusted' attributes support
7315*4882a593Smuzhiyun */
ocfs2_xattr_trusted_get(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * buffer,size_t size,int flags)7316*4882a593Smuzhiyun static int ocfs2_xattr_trusted_get(const struct xattr_handler *handler,
7317*4882a593Smuzhiyun struct dentry *unused, struct inode *inode,
7318*4882a593Smuzhiyun const char *name, void *buffer, size_t size,
7319*4882a593Smuzhiyun int flags)
7320*4882a593Smuzhiyun {
7321*4882a593Smuzhiyun return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED,
7322*4882a593Smuzhiyun name, buffer, size);
7323*4882a593Smuzhiyun }
7324*4882a593Smuzhiyun
ocfs2_xattr_trusted_set(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,const void * value,size_t size,int flags)7325*4882a593Smuzhiyun static int ocfs2_xattr_trusted_set(const struct xattr_handler *handler,
7326*4882a593Smuzhiyun struct dentry *unused, struct inode *inode,
7327*4882a593Smuzhiyun const char *name, const void *value,
7328*4882a593Smuzhiyun size_t size, int flags)
7329*4882a593Smuzhiyun {
7330*4882a593Smuzhiyun return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED,
7331*4882a593Smuzhiyun name, value, size, flags);
7332*4882a593Smuzhiyun }
7333*4882a593Smuzhiyun
7334*4882a593Smuzhiyun const struct xattr_handler ocfs2_xattr_trusted_handler = {
7335*4882a593Smuzhiyun .prefix = XATTR_TRUSTED_PREFIX,
7336*4882a593Smuzhiyun .get = ocfs2_xattr_trusted_get,
7337*4882a593Smuzhiyun .set = ocfs2_xattr_trusted_set,
7338*4882a593Smuzhiyun };
7339*4882a593Smuzhiyun
7340*4882a593Smuzhiyun /*
7341*4882a593Smuzhiyun * 'user' attributes support
7342*4882a593Smuzhiyun */
ocfs2_xattr_user_get(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * buffer,size_t size,int flags)7343*4882a593Smuzhiyun static int ocfs2_xattr_user_get(const struct xattr_handler *handler,
7344*4882a593Smuzhiyun struct dentry *unused, struct inode *inode,
7345*4882a593Smuzhiyun const char *name, void *buffer, size_t size,
7346*4882a593Smuzhiyun int flags)
7347*4882a593Smuzhiyun {
7348*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7349*4882a593Smuzhiyun
7350*4882a593Smuzhiyun if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7351*4882a593Smuzhiyun return -EOPNOTSUPP;
7352*4882a593Smuzhiyun return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
7353*4882a593Smuzhiyun buffer, size);
7354*4882a593Smuzhiyun }
7355*4882a593Smuzhiyun
ocfs2_xattr_user_set(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,const void * value,size_t size,int flags)7356*4882a593Smuzhiyun static int ocfs2_xattr_user_set(const struct xattr_handler *handler,
7357*4882a593Smuzhiyun struct dentry *unused, struct inode *inode,
7358*4882a593Smuzhiyun const char *name, const void *value,
7359*4882a593Smuzhiyun size_t size, int flags)
7360*4882a593Smuzhiyun {
7361*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7362*4882a593Smuzhiyun
7363*4882a593Smuzhiyun if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7364*4882a593Smuzhiyun return -EOPNOTSUPP;
7365*4882a593Smuzhiyun
7366*4882a593Smuzhiyun return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER,
7367*4882a593Smuzhiyun name, value, size, flags);
7368*4882a593Smuzhiyun }
7369*4882a593Smuzhiyun
7370*4882a593Smuzhiyun const struct xattr_handler ocfs2_xattr_user_handler = {
7371*4882a593Smuzhiyun .prefix = XATTR_USER_PREFIX,
7372*4882a593Smuzhiyun .get = ocfs2_xattr_user_get,
7373*4882a593Smuzhiyun .set = ocfs2_xattr_user_set,
7374*4882a593Smuzhiyun };
7375