blob: ebfdea78659b1bda5a93674837e8f818c2503d44 [file] [log] [blame]
Tao Maf56654c2008-08-18 17:38:48 +08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * xattr.c
5 *
Tiger Yangc3cb6822008-10-23 16:33:03 +08006 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
Tao Maf56654c2008-08-18 17:38:48 +08007 *
Tiger Yangcf1d6c72008-08-18 17:11:00 +08008 * CREDITS:
Tiger Yangc3cb6822008-10-23 16:33:03 +08009 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
Tiger Yangcf1d6c72008-08-18 17:11:00 +080011 *
Tao Maf56654c2008-08-18 17:38:48 +080012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
Tiger Yangc3cb6822008-10-23 16:33:03 +080014 * License version 2 as published by the Free Software Foundation.
Tao Maf56654c2008-08-18 17:38:48 +080015 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
Tao Maf56654c2008-08-18 17:38:48 +080020 */
21
Tiger Yangcf1d6c72008-08-18 17:11:00 +080022#include <linux/capability.h>
23#include <linux/fs.h>
24#include <linux/types.h>
25#include <linux/slab.h>
26#include <linux/highmem.h>
27#include <linux/pagemap.h>
28#include <linux/uio.h>
29#include <linux/sched.h>
30#include <linux/splice.h>
31#include <linux/mount.h>
32#include <linux/writeback.h>
33#include <linux/falloc.h>
Tao Ma01225592008-08-18 17:38:53 +080034#include <linux/sort.h>
Mark Fasheh99219ae2008-10-07 14:52:59 -070035#include <linux/init.h>
36#include <linux/module.h>
37#include <linux/string.h>
Tiger Yang923f7f32008-11-14 11:16:27 +080038#include <linux/security.h>
Tiger Yangcf1d6c72008-08-18 17:11:00 +080039
Tao Maf56654c2008-08-18 17:38:48 +080040#include <cluster/masklog.h>
41
42#include "ocfs2.h"
43#include "alloc.h"
Joel Beckerd6b32bb2008-10-17 14:55:01 -070044#include "blockcheck.h"
Tao Maf56654c2008-08-18 17:38:48 +080045#include "dlmglue.h"
46#include "file.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080047#include "symlink.h"
48#include "sysfile.h"
Tao Maf56654c2008-08-18 17:38:48 +080049#include "inode.h"
50#include "journal.h"
51#include "ocfs2_fs.h"
52#include "suballoc.h"
53#include "uptodate.h"
54#include "buffer_head_io.h"
Tao Ma0c044f02008-08-18 17:38:50 +080055#include "super.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080056#include "xattr.h"
Tao Ma492a8a32009-08-18 11:43:17 +080057#include "refcounttree.h"
Tao Ma0fe9b662009-08-18 11:47:56 +080058#include "acl.h"
Tao Ma402b4182011-02-23 22:01:17 +080059#include "ocfs2_trace.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080060
61struct ocfs2_xattr_def_value_root {
62 struct ocfs2_xattr_value_root xv;
63 struct ocfs2_extent_rec er;
64};
65
Tao Ma0c044f02008-08-18 17:38:50 +080066struct ocfs2_xattr_bucket {
Joel Beckerba937122008-10-24 19:13:20 -070067 /* The inode these xattrs are associated with */
68 struct inode *bu_inode;
69
70 /* The actual buffers that make up the bucket */
Joel Becker4ac60322008-10-18 19:11:42 -070071 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
Joel Beckerba937122008-10-24 19:13:20 -070072
73 /* How many blocks make up one bucket for this filesystem */
74 int bu_blocks;
Tao Ma0c044f02008-08-18 17:38:50 +080075};
76
Tao Ma78f30c32008-11-12 08:27:00 +080077struct ocfs2_xattr_set_ctxt {
Tao Ma85db90e2008-11-12 08:27:01 +080078 handle_t *handle;
Tao Ma78f30c32008-11-12 08:27:00 +080079 struct ocfs2_alloc_context *meta_ac;
80 struct ocfs2_alloc_context *data_ac;
81 struct ocfs2_cached_dealloc_ctxt dealloc;
Tao Ma5f5261a2010-05-13 22:49:05 +080082 int set_abort;
Tao Ma78f30c32008-11-12 08:27:00 +080083};
84
Tiger Yangcf1d6c72008-08-18 17:11:00 +080085#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
86#define OCFS2_XATTR_INLINE_SIZE 80
Tiger Yang4442f512009-02-20 11:11:50 +080087#define OCFS2_XATTR_HEADER_GAP 4
Tiger Yang534eadd2008-11-14 11:16:41 +080088#define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
89 - sizeof(struct ocfs2_xattr_header) \
Tiger Yang4442f512009-02-20 11:11:50 +080090 - OCFS2_XATTR_HEADER_GAP)
Tiger Yang89c38bd2008-11-14 11:17:41 +080091#define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
92 - sizeof(struct ocfs2_xattr_block) \
93 - sizeof(struct ocfs2_xattr_header) \
Tiger Yang4442f512009-02-20 11:11:50 +080094 - OCFS2_XATTR_HEADER_GAP)
Tiger Yangcf1d6c72008-08-18 17:11:00 +080095
96static struct ocfs2_xattr_def_value_root def_xv = {
97 .xv.xr_list.l_count = cpu_to_le16(1),
98};
99
Stephen Hemminger537d81c2010-05-13 17:53:22 -0700100const struct xattr_handler *ocfs2_xattr_handlers[] = {
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800101 &ocfs2_xattr_user_handler,
Christoph Hellwig702e5bc2013-12-20 05:16:48 -0800102 &posix_acl_access_xattr_handler,
103 &posix_acl_default_xattr_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800104 &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800105 &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800106 NULL
107};
108
Stephen Hemminger537d81c2010-05-13 17:53:22 -0700109static const struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800110 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +0800111 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
Christoph Hellwig702e5bc2013-12-20 05:16:48 -0800112 = &posix_acl_access_xattr_handler,
Tiger Yang929fb012008-11-14 11:17:04 +0800113 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
Christoph Hellwig702e5bc2013-12-20 05:16:48 -0800114 = &posix_acl_default_xattr_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800115 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800116 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800117};
118
119struct ocfs2_xattr_info {
Joel Becker6b240ff2009-08-14 18:02:52 -0700120 int xi_name_index;
121 const char *xi_name;
Joel Becker18853b92009-08-14 18:17:07 -0700122 int xi_name_len;
Joel Becker6b240ff2009-08-14 18:02:52 -0700123 const void *xi_value;
124 size_t xi_value_len;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800125};
126
127struct ocfs2_xattr_search {
128 struct buffer_head *inode_bh;
129 /*
130 * xattr_bh point to the block buffer head which has extended attribute
131 * when extended attribute in inode, xattr_bh is equal to inode_bh.
132 */
133 struct buffer_head *xattr_bh;
134 struct ocfs2_xattr_header *header;
Joel Beckerba937122008-10-24 19:13:20 -0700135 struct ocfs2_xattr_bucket *bucket;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800136 void *base;
137 void *end;
138 struct ocfs2_xattr_entry *here;
139 int not_found;
140};
141
Joel Becker11179f22009-08-14 16:07:44 -0700142/* Operations on struct ocfs2_xa_entry */
143struct ocfs2_xa_loc;
144struct ocfs2_xa_loc_operations {
145 /*
Joel Beckercf2bc802009-08-18 13:52:38 -0700146 * Journal functions
147 */
148 int (*xlo_journal_access)(handle_t *handle, struct ocfs2_xa_loc *loc,
149 int type);
150 void (*xlo_journal_dirty)(handle_t *handle, struct ocfs2_xa_loc *loc);
151
152 /*
Joel Becker11179f22009-08-14 16:07:44 -0700153 * Return a pointer to the appropriate buffer in loc->xl_storage
154 * at the given offset from loc->xl_header.
155 */
156 void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
157
Joel Becker69a3e532009-08-17 12:24:39 -0700158 /* Can we reuse the existing entry for the new value? */
159 int (*xlo_can_reuse)(struct ocfs2_xa_loc *loc,
160 struct ocfs2_xattr_info *xi);
161
162 /* How much space is needed for the new value? */
163 int (*xlo_check_space)(struct ocfs2_xa_loc *loc,
164 struct ocfs2_xattr_info *xi);
165
166 /*
167 * Return the offset of the first name+value pair. This is
168 * the start of our downward-filling free space.
169 */
170 int (*xlo_get_free_start)(struct ocfs2_xa_loc *loc);
171
Joel Becker11179f22009-08-14 16:07:44 -0700172 /*
173 * Remove the name+value at this location. Do whatever is
174 * appropriate with the remaining name+value pairs.
175 */
176 void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
Joel Becker69a3e532009-08-17 12:24:39 -0700177
178 /* Fill xl_entry with a new entry */
179 void (*xlo_add_entry)(struct ocfs2_xa_loc *loc, u32 name_hash);
180
181 /* Add name+value storage to an entry */
182 void (*xlo_add_namevalue)(struct ocfs2_xa_loc *loc, int size);
Joel Becker3fc12af2009-08-18 13:20:27 -0700183
184 /*
185 * Initialize the value buf's access and bh fields for this entry.
186 * ocfs2_xa_fill_value_buf() will handle the xv pointer.
187 */
188 void (*xlo_fill_value_buf)(struct ocfs2_xa_loc *loc,
189 struct ocfs2_xattr_value_buf *vb);
Joel Becker11179f22009-08-14 16:07:44 -0700190};
191
192/*
193 * Describes an xattr entry location. This is a memory structure
194 * tracking the on-disk structure.
195 */
196struct ocfs2_xa_loc {
Joel Beckercf2bc802009-08-18 13:52:38 -0700197 /* This xattr belongs to this inode */
198 struct inode *xl_inode;
199
Joel Becker11179f22009-08-14 16:07:44 -0700200 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
201 struct ocfs2_xattr_header *xl_header;
202
203 /* Bytes from xl_header to the end of the storage */
204 int xl_size;
205
206 /*
207 * The ocfs2_xattr_entry this location describes. If this is
208 * NULL, this location describes the on-disk structure where it
209 * would have been.
210 */
211 struct ocfs2_xattr_entry *xl_entry;
212
213 /*
214 * Internal housekeeping
215 */
216
217 /* Buffer(s) containing this entry */
218 void *xl_storage;
219
220 /* Operations on the storage backing this location */
221 const struct ocfs2_xa_loc_operations *xl_ops;
222};
223
Joel Becker199799a2009-08-14 19:04:15 -0700224/*
225 * Convenience functions to calculate how much space is needed for a
226 * given name+value pair
227 */
228static int namevalue_size(int name_len, uint64_t value_len)
229{
230 if (value_len > OCFS2_XATTR_INLINE_SIZE)
231 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
232 else
233 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
234}
235
236static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
237{
238 return namevalue_size(xi->xi_name_len, xi->xi_value_len);
239}
240
241static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
242{
243 u64 value_len = le64_to_cpu(xe->xe_value_size);
244
245 BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
246 ocfs2_xattr_is_local(xe));
247 return namevalue_size(xe->xe_name_len, value_len);
248}
249
250
Tao Mafd68a892009-08-18 11:43:21 +0800251static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
Tao Ma589dc262008-08-18 17:38:51 +0800252 struct ocfs2_xattr_header *xh,
253 int index,
254 int *block_off,
255 int *new_offset);
256
Joel Becker54f443f2008-10-20 18:43:07 -0700257static int ocfs2_xattr_block_find(struct inode *inode,
258 int name_index,
259 const char *name,
260 struct ocfs2_xattr_search *xs);
Tao Ma589dc262008-08-18 17:38:51 +0800261static int ocfs2_xattr_index_block_find(struct inode *inode,
262 struct buffer_head *root_bh,
263 int name_index,
264 const char *name,
265 struct ocfs2_xattr_search *xs);
266
Tao Ma0c044f02008-08-18 17:38:50 +0800267static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
Tao Ma47bca492009-08-18 11:43:42 +0800268 struct buffer_head *blk_bh,
Tao Ma0c044f02008-08-18 17:38:50 +0800269 char *buffer,
270 size_t buffer_size);
271
Tao Ma01225592008-08-18 17:38:53 +0800272static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +0800273 struct ocfs2_xattr_search *xs,
274 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800275
276static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
277 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +0800278 struct ocfs2_xattr_search *xs,
279 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800280
Tao Ma47bca492009-08-18 11:43:42 +0800281typedef int (xattr_tree_rec_func)(struct inode *inode,
282 struct buffer_head *root_bh,
283 u64 blkno, u32 cpos, u32 len, void *para);
284static int ocfs2_iterate_xattr_index_block(struct inode *inode,
285 struct buffer_head *root_bh,
286 xattr_tree_rec_func *rec_func,
287 void *para);
288static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
289 struct ocfs2_xattr_bucket *bucket,
290 void *para);
291static int ocfs2_rm_xattr_cluster(struct inode *inode,
292 struct buffer_head *root_bh,
293 u64 blkno,
294 u32 cpos,
295 u32 len,
296 void *para);
297
Joel Beckerc58b6032008-11-26 13:36:24 -0800298static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
299 u64 src_blk, u64 last_blk, u64 to_blk,
300 unsigned int start_bucket,
301 u32 *first_hash);
Tao Ma492a8a32009-08-18 11:43:17 +0800302static int ocfs2_prepare_refcount_xattr(struct inode *inode,
303 struct ocfs2_dinode *di,
304 struct ocfs2_xattr_info *xi,
305 struct ocfs2_xattr_search *xis,
306 struct ocfs2_xattr_search *xbs,
307 struct ocfs2_refcount_tree **ref_tree,
308 int *meta_need,
309 int *credits);
Tao Mace9c5a52009-08-18 11:43:59 +0800310static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
311 struct ocfs2_xattr_bucket *bucket,
312 int offset,
313 struct ocfs2_xattr_value_root **xv,
314 struct buffer_head **bh);
Tao Maa3944252008-08-18 17:38:54 +0800315
Tiger Yang0030e002008-10-23 16:33:33 +0800316static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
317{
318 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
319}
320
321static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
322{
323 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
324}
325
Joel Becker9c7759a2008-10-24 16:21:03 -0700326#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
Joel Becker51def392008-10-24 16:57:21 -0700327#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
Joel Becker3e632942008-10-24 17:04:49 -0700328#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
Joel Becker9c7759a2008-10-24 16:21:03 -0700329
Joel Beckerba937122008-10-24 19:13:20 -0700330static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
Joel Becker6dde41d2008-10-24 17:16:48 -0700331{
Joel Beckerba937122008-10-24 19:13:20 -0700332 struct ocfs2_xattr_bucket *bucket;
333 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker6dde41d2008-10-24 17:16:48 -0700334
Joel Beckerba937122008-10-24 19:13:20 -0700335 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
336
337 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
338 if (bucket) {
339 bucket->bu_inode = inode;
340 bucket->bu_blocks = blks;
341 }
342
343 return bucket;
344}
345
346static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
347{
348 int i;
349
350 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker6dde41d2008-10-24 17:16:48 -0700351 brelse(bucket->bu_bhs[i]);
352 bucket->bu_bhs[i] = NULL;
353 }
354}
355
Joel Beckerba937122008-10-24 19:13:20 -0700356static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
357{
358 if (bucket) {
359 ocfs2_xattr_bucket_relse(bucket);
360 bucket->bu_inode = NULL;
361 kfree(bucket);
362 }
363}
364
Joel Becker784b8162008-10-24 17:33:40 -0700365/*
366 * A bucket that has never been written to disk doesn't need to be
367 * read. We just need the buffer_heads. Don't call this for
368 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
369 * them fully.
370 */
Joel Beckerba937122008-10-24 19:13:20 -0700371static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Wengang Wang9c339252014-04-03 14:47:15 -0700372 u64 xb_blkno, int new)
Joel Becker784b8162008-10-24 17:33:40 -0700373{
374 int i, rc = 0;
Joel Becker784b8162008-10-24 17:33:40 -0700375
Joel Beckerba937122008-10-24 19:13:20 -0700376 for (i = 0; i < bucket->bu_blocks; i++) {
377 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
378 xb_blkno + i);
Joel Becker784b8162008-10-24 17:33:40 -0700379 if (!bucket->bu_bhs[i]) {
Rui Xiang7391a292013-11-12 15:06:54 -0800380 rc = -ENOMEM;
Joel Becker784b8162008-10-24 17:33:40 -0700381 mlog_errno(rc);
382 break;
383 }
384
Joel Becker8cb471e2009-02-10 20:00:41 -0800385 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
Wengang Wang9c339252014-04-03 14:47:15 -0700386 bucket->bu_bhs[i])) {
387 if (new)
388 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
389 bucket->bu_bhs[i]);
390 else {
391 set_buffer_uptodate(bucket->bu_bhs[i]);
392 ocfs2_set_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
393 bucket->bu_bhs[i]);
394 }
395 }
Joel Becker784b8162008-10-24 17:33:40 -0700396 }
397
398 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700399 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700400 return rc;
401}
402
403/* Read the xattr bucket at xb_blkno */
Joel Beckerba937122008-10-24 19:13:20 -0700404static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700405 u64 xb_blkno)
406{
Joel Beckerba937122008-10-24 19:13:20 -0700407 int rc;
Joel Becker784b8162008-10-24 17:33:40 -0700408
Joel Becker8cb471e2009-02-10 20:00:41 -0800409 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
Joel Becker970e4932008-11-13 14:49:19 -0800410 bucket->bu_blocks, bucket->bu_bhs, 0,
411 NULL);
Joel Becker4d0e2142008-12-05 11:19:37 -0800412 if (!rc) {
Tao Mac8b9cf92009-02-24 17:40:26 -0800413 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800414 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
415 bucket->bu_bhs,
416 bucket->bu_blocks,
417 &bucket_xh(bucket)->xh_check);
Tao Mac8b9cf92009-02-24 17:40:26 -0800418 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800419 if (rc)
420 mlog_errno(rc);
421 }
422
Joel Becker784b8162008-10-24 17:33:40 -0700423 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700424 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700425 return rc;
426}
427
Joel Becker1224be02008-10-24 18:47:33 -0700428static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700429 struct ocfs2_xattr_bucket *bucket,
430 int type)
431{
432 int i, rc = 0;
Joel Becker1224be02008-10-24 18:47:33 -0700433
Joel Beckerba937122008-10-24 19:13:20 -0700434 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800435 rc = ocfs2_journal_access(handle,
436 INODE_CACHE(bucket->bu_inode),
Joel Becker1224be02008-10-24 18:47:33 -0700437 bucket->bu_bhs[i], type);
438 if (rc) {
439 mlog_errno(rc);
440 break;
441 }
442 }
443
444 return rc;
445}
446
447static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700448 struct ocfs2_xattr_bucket *bucket)
449{
Joel Beckerba937122008-10-24 19:13:20 -0700450 int i;
Joel Becker1224be02008-10-24 18:47:33 -0700451
Tao Mac8b9cf92009-02-24 17:40:26 -0800452 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800453 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
454 bucket->bu_bhs, bucket->bu_blocks,
455 &bucket_xh(bucket)->xh_check);
Tao Mac8b9cf92009-02-24 17:40:26 -0800456 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800457
Joel Beckerba937122008-10-24 19:13:20 -0700458 for (i = 0; i < bucket->bu_blocks; i++)
Joel Becker1224be02008-10-24 18:47:33 -0700459 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
460}
461
Joel Beckerba937122008-10-24 19:13:20 -0700462static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
Joel Becker4980c6d2008-10-24 18:54:43 -0700463 struct ocfs2_xattr_bucket *src)
464{
465 int i;
Joel Beckerba937122008-10-24 19:13:20 -0700466 int blocksize = src->bu_inode->i_sb->s_blocksize;
Joel Becker4980c6d2008-10-24 18:54:43 -0700467
Joel Beckerba937122008-10-24 19:13:20 -0700468 BUG_ON(dest->bu_blocks != src->bu_blocks);
469 BUG_ON(dest->bu_inode != src->bu_inode);
470
471 for (i = 0; i < src->bu_blocks; i++) {
Joel Becker4980c6d2008-10-24 18:54:43 -0700472 memcpy(bucket_block(dest, i), bucket_block(src, i),
473 blocksize);
474 }
475}
Joel Becker1224be02008-10-24 18:47:33 -0700476
Joel Becker4ae1d692008-11-13 14:49:18 -0800477static int ocfs2_validate_xattr_block(struct super_block *sb,
478 struct buffer_head *bh)
479{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700480 int rc;
Joel Becker4ae1d692008-11-13 14:49:18 -0800481 struct ocfs2_xattr_block *xb =
482 (struct ocfs2_xattr_block *)bh->b_data;
483
Tao Ma402b4182011-02-23 22:01:17 +0800484 trace_ocfs2_validate_xattr_block((unsigned long long)bh->b_blocknr);
Joel Becker4ae1d692008-11-13 14:49:18 -0800485
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700486 BUG_ON(!buffer_uptodate(bh));
487
488 /*
489 * If the ecc fails, we return the error but otherwise
490 * leave the filesystem running. We know any error is
491 * local to this block.
492 */
493 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
494 if (rc)
495 return rc;
496
497 /*
498 * Errors after here are fatal
499 */
500
Joel Becker4ae1d692008-11-13 14:49:18 -0800501 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700502 return ocfs2_error(sb,
Joe Perches7ecef142015-09-04 15:44:51 -0700503 "Extended attribute block #%llu has bad signature %.*s\n",
504 (unsigned long long)bh->b_blocknr, 7,
505 xb->xb_signature);
Joel Becker4ae1d692008-11-13 14:49:18 -0800506 }
507
508 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700509 return ocfs2_error(sb,
Joe Perches7ecef142015-09-04 15:44:51 -0700510 "Extended attribute block #%llu has an invalid xb_blkno of %llu\n",
511 (unsigned long long)bh->b_blocknr,
512 (unsigned long long)le64_to_cpu(xb->xb_blkno));
Joel Becker4ae1d692008-11-13 14:49:18 -0800513 }
514
515 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700516 return ocfs2_error(sb,
Joe Perches7ecef142015-09-04 15:44:51 -0700517 "Extended attribute block #%llu has an invalid xb_fs_generation of #%u\n",
518 (unsigned long long)bh->b_blocknr,
519 le32_to_cpu(xb->xb_fs_generation));
Joel Becker4ae1d692008-11-13 14:49:18 -0800520 }
521
522 return 0;
523}
524
525static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
526 struct buffer_head **bh)
527{
528 int rc;
529 struct buffer_head *tmp = *bh;
530
Joel Becker8cb471e2009-02-10 20:00:41 -0800531 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800532 ocfs2_validate_xattr_block);
Joel Becker4ae1d692008-11-13 14:49:18 -0800533
534 /* If ocfs2_read_block() got us a new bh, pass it up. */
535 if (!rc && !*bh)
536 *bh = tmp;
537
538 return rc;
539}
540
Tao Ma936b8832008-10-09 23:06:14 +0800541static inline const char *ocfs2_xattr_prefix(int name_index)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800542{
Stephen Hemminger537d81c2010-05-13 17:53:22 -0700543 const struct xattr_handler *handler = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800544
545 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
546 handler = ocfs2_xattr_handler_map[name_index];
547
Tao Ma936b8832008-10-09 23:06:14 +0800548 return handler ? handler->prefix : NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800549}
550
Mark Fasheh40daa162008-10-07 14:31:42 -0700551static u32 ocfs2_xattr_name_hash(struct inode *inode,
Tao Ma2057e5c2008-10-09 23:06:13 +0800552 const char *name,
Mark Fasheh40daa162008-10-07 14:31:42 -0700553 int name_len)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800554{
555 /* Get hash value of uuid from super block */
556 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
557 int i;
558
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800559 /* hash extended attribute name */
560 for (i = 0; i < name_len; i++) {
561 hash = (hash << OCFS2_HASH_SHIFT) ^
562 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
563 *name++;
564 }
565
566 return hash;
567}
568
Tiger Yang534eadd2008-11-14 11:16:41 +0800569static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
570{
Joel Becker199799a2009-08-14 19:04:15 -0700571 return namevalue_size(name_len, value_len) +
572 sizeof(struct ocfs2_xattr_entry);
573}
Tiger Yang534eadd2008-11-14 11:16:41 +0800574
Joel Becker199799a2009-08-14 19:04:15 -0700575static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
576{
577 return namevalue_size_xi(xi) +
578 sizeof(struct ocfs2_xattr_entry);
579}
Tiger Yang534eadd2008-11-14 11:16:41 +0800580
Joel Becker199799a2009-08-14 19:04:15 -0700581static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
582{
583 return namevalue_size_xe(xe) +
584 sizeof(struct ocfs2_xattr_entry);
Tiger Yang534eadd2008-11-14 11:16:41 +0800585}
586
587int ocfs2_calc_security_init(struct inode *dir,
588 struct ocfs2_security_xattr_info *si,
589 int *want_clusters,
590 int *xattr_credits,
591 struct ocfs2_alloc_context **xattr_ac)
592{
593 int ret = 0;
594 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
595 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
596 si->value_len);
597
598 /*
599 * The max space of security xattr taken inline is
600 * 256(name) + 80(value) + 16(entry) = 352 bytes,
601 * So reserve one metadata block for it is ok.
602 */
603 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
604 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
605 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
606 if (ret) {
607 mlog_errno(ret);
608 return ret;
609 }
610 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
611 }
612
613 /* reserve clusters for xattr value which will be set in B tree*/
Tiger Yang0e445b62008-12-09 16:42:51 +0800614 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
615 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
616 si->value_len);
617
618 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
619 new_clusters);
620 *want_clusters += new_clusters;
621 }
Tiger Yang534eadd2008-11-14 11:16:41 +0800622 return ret;
623}
624
Tiger Yang89c38bd2008-11-14 11:17:41 +0800625int ocfs2_calc_xattr_init(struct inode *dir,
626 struct buffer_head *dir_bh,
Al Viro67697cb2011-07-26 02:55:32 -0400627 umode_t mode,
Tiger Yang89c38bd2008-11-14 11:17:41 +0800628 struct ocfs2_security_xattr_info *si,
629 int *want_clusters,
630 int *xattr_credits,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800631 int *want_meta)
Tiger Yang89c38bd2008-11-14 11:17:41 +0800632{
633 int ret = 0;
634 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
Tiger Yang0e445b62008-12-09 16:42:51 +0800635 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800636
637 if (si->enable)
638 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
639 si->value_len);
640
641 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
642 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
643 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
644 "", NULL, 0);
645 if (acl_len > 0) {
646 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
647 if (S_ISDIR(mode))
648 a_size <<= 1;
649 } else if (acl_len != 0 && acl_len != -ENODATA) {
650 mlog_errno(ret);
651 return ret;
652 }
653 }
654
655 if (!(s_size + a_size))
656 return ret;
657
658 /*
659 * The max space of security xattr taken inline is
660 * 256(name) + 80(value) + 16(entry) = 352 bytes,
661 * The max space of acl xattr taken inline is
662 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
663 * when blocksize = 512, may reserve one more cluser for
664 * xattr bucket, otherwise reserve one metadata block
665 * for them is ok.
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800666 * If this is a new directory with inline data,
667 * we choose to reserve the entire inline area for
668 * directory contents and force an external xattr block.
Tiger Yang89c38bd2008-11-14 11:17:41 +0800669 */
670 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800671 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
Tiger Yang89c38bd2008-11-14 11:17:41 +0800672 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800673 *want_meta = *want_meta + 1;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800674 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
675 }
676
677 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
678 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
679 *want_clusters += 1;
680 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
681 }
682
Tiger Yang0e445b62008-12-09 16:42:51 +0800683 /*
684 * reserve credits and clusters for xattrs which has large value
685 * and have to be set outside
686 */
687 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
688 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
689 si->value_len);
690 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
691 new_clusters);
692 *want_clusters += new_clusters;
693 }
Tiger Yang89c38bd2008-11-14 11:17:41 +0800694 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
695 acl_len > OCFS2_XATTR_INLINE_SIZE) {
Tiger Yang0e445b62008-12-09 16:42:51 +0800696 /* for directory, it has DEFAULT and ACCESS two types of acls */
697 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
698 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
699 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
700 new_clusters);
701 *want_clusters += new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800702 }
703
704 return ret;
705}
706
Tao Maf56654c2008-08-18 17:38:48 +0800707static int ocfs2_xattr_extend_allocation(struct inode *inode,
708 u32 clusters_to_add,
Joel Becker19b801f2008-12-09 14:36:50 -0800709 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800710 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800711{
Tao Maa78f9f42010-07-09 14:53:11 +0800712 int status = 0, credits;
Tao Ma85db90e2008-11-12 08:27:01 +0800713 handle_t *handle = ctxt->handle;
Tao Maf56654c2008-08-18 17:38:48 +0800714 enum ocfs2_alloc_restarted why;
Joel Becker19b801f2008-12-09 14:36:50 -0800715 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700716 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800717
Joel Becker5e404e92009-02-13 03:54:22 -0800718 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700719
Tao Maa78f9f42010-07-09 14:53:11 +0800720 while (clusters_to_add) {
Tao Ma402b4182011-02-23 22:01:17 +0800721 trace_ocfs2_xattr_extend_allocation(clusters_to_add);
722
Tao Maa78f9f42010-07-09 14:53:11 +0800723 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
724 OCFS2_JOURNAL_ACCESS_WRITE);
725 if (status < 0) {
726 mlog_errno(status);
727 break;
728 }
729
730 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
731 status = ocfs2_add_clusters_in_btree(handle,
732 &et,
733 &logical_start,
734 clusters_to_add,
735 0,
736 ctxt->data_ac,
737 ctxt->meta_ac,
738 &why);
739 if ((status < 0) && (status != -EAGAIN)) {
740 if (status != -ENOSPC)
741 mlog_errno(status);
742 break;
743 }
744
745 ocfs2_journal_dirty(handle, vb->vb_bh);
746
747 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) -
748 prev_clusters;
749
750 if (why != RESTART_NONE && clusters_to_add) {
751 /*
752 * We can only fail in case the alloc file doesn't give
753 * up enough clusters.
754 */
755 BUG_ON(why == RESTART_META);
756
Tao Maa78f9f42010-07-09 14:53:11 +0800757 credits = ocfs2_calc_extend_credits(inode->i_sb,
Goldwyn Rodrigues06f9da62013-11-12 15:06:52 -0800758 &vb->vb_xv->xr_list);
Tao Maa78f9f42010-07-09 14:53:11 +0800759 status = ocfs2_extend_trans(handle, credits);
760 if (status < 0) {
761 status = -ENOMEM;
762 mlog_errno(status);
763 break;
764 }
765 }
Tao Maf56654c2008-08-18 17:38:48 +0800766 }
767
Tao Maf56654c2008-08-18 17:38:48 +0800768 return status;
769}
770
771static int __ocfs2_remove_xattr_range(struct inode *inode,
Joel Beckerd72cc722008-12-09 14:30:41 -0800772 struct ocfs2_xattr_value_buf *vb,
Tao Maf56654c2008-08-18 17:38:48 +0800773 u32 cpos, u32 phys_cpos, u32 len,
Tao Ma492a8a32009-08-18 11:43:17 +0800774 unsigned int ext_flags,
Tao Ma78f30c32008-11-12 08:27:00 +0800775 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800776{
777 int ret;
778 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Tao Ma85db90e2008-11-12 08:27:01 +0800779 handle_t *handle = ctxt->handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700780 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800781
Joel Becker5e404e92009-02-13 03:54:22 -0800782 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700783
Joel Becker0cf2f762009-02-12 16:41:25 -0800784 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Beckerd72cc722008-12-09 14:30:41 -0800785 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800786 if (ret) {
787 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800788 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800789 }
790
Joel Beckerdbdcf6a2009-02-13 03:41:26 -0800791 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
Tao Ma78f30c32008-11-12 08:27:00 +0800792 &ctxt->dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800793 if (ret) {
794 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800795 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800796 }
797
Joel Beckerd72cc722008-12-09 14:30:41 -0800798 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
Joel Beckerec20cec2010-03-19 14:13:52 -0700799 ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800800
Tao Ma492a8a32009-08-18 11:43:17 +0800801 if (ext_flags & OCFS2_EXT_REFCOUNTED)
802 ret = ocfs2_decrease_refcount(inode, handle,
803 ocfs2_blocks_to_clusters(inode->i_sb,
804 phys_blkno),
805 len, ctxt->meta_ac, &ctxt->dealloc, 1);
806 else
807 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
808 phys_blkno, len);
Tao Maf56654c2008-08-18 17:38:48 +0800809 if (ret)
810 mlog_errno(ret);
811
Tao Maf56654c2008-08-18 17:38:48 +0800812out:
Tao Maf56654c2008-08-18 17:38:48 +0800813 return ret;
814}
815
816static int ocfs2_xattr_shrink_size(struct inode *inode,
817 u32 old_clusters,
818 u32 new_clusters,
Joel Becker19b801f2008-12-09 14:36:50 -0800819 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800820 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800821{
822 int ret = 0;
Tao Ma492a8a32009-08-18 11:43:17 +0800823 unsigned int ext_flags;
Tao Maf56654c2008-08-18 17:38:48 +0800824 u32 trunc_len, cpos, phys_cpos, alloc_size;
825 u64 block;
Tao Maf56654c2008-08-18 17:38:48 +0800826
827 if (old_clusters <= new_clusters)
828 return 0;
829
830 cpos = new_clusters;
831 trunc_len = old_clusters - new_clusters;
832 while (trunc_len) {
833 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
Joel Beckerd72cc722008-12-09 14:30:41 -0800834 &alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800835 &vb->vb_xv->xr_list, &ext_flags);
Tao Maf56654c2008-08-18 17:38:48 +0800836 if (ret) {
837 mlog_errno(ret);
838 goto out;
839 }
840
841 if (alloc_size > trunc_len)
842 alloc_size = trunc_len;
843
Joel Becker19b801f2008-12-09 14:36:50 -0800844 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
Tao Maf56654c2008-08-18 17:38:48 +0800845 phys_cpos, alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800846 ext_flags, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800847 if (ret) {
848 mlog_errno(ret);
849 goto out;
850 }
851
852 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Joel Becker8cb471e2009-02-10 20:00:41 -0800853 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
854 block, alloc_size);
Tao Maf56654c2008-08-18 17:38:48 +0800855 cpos += alloc_size;
856 trunc_len -= alloc_size;
857 }
858
859out:
Tao Maf56654c2008-08-18 17:38:48 +0800860 return ret;
861}
862
863static int ocfs2_xattr_value_truncate(struct inode *inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800864 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800865 int len,
866 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800867{
868 int ret;
869 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
Joel Beckerb3e5d372008-12-09 15:01:04 -0800870 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800871
872 if (new_clusters == old_clusters)
873 return 0;
874
875 if (new_clusters > old_clusters)
876 ret = ocfs2_xattr_extend_allocation(inode,
877 new_clusters - old_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800878 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800879 else
880 ret = ocfs2_xattr_shrink_size(inode,
881 old_clusters, new_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800882 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800883
884 return ret;
885}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800886
Tao Ma936b8832008-10-09 23:06:14 +0800887static int ocfs2_xattr_list_entry(char *buffer, size_t size,
888 size_t *result, const char *prefix,
889 const char *name, int name_len)
890{
891 char *p = buffer + *result;
892 int prefix_len = strlen(prefix);
893 int total_len = prefix_len + name_len + 1;
894
895 *result += total_len;
896
897 /* we are just looking for how big our buffer needs to be */
898 if (!size)
899 return 0;
900
901 if (*result > size)
902 return -ERANGE;
903
904 memcpy(p, prefix, prefix_len);
905 memcpy(p + prefix_len, name, name_len);
906 p[prefix_len + name_len] = '\0';
907
908 return 0;
909}
910
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800911static int ocfs2_xattr_list_entries(struct inode *inode,
912 struct ocfs2_xattr_header *header,
913 char *buffer, size_t buffer_size)
914{
Tao Ma936b8832008-10-09 23:06:14 +0800915 size_t result = 0;
916 int i, type, ret;
917 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800918
919 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
920 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800921 type = ocfs2_xattr_get_type(entry);
922 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800923
Tao Ma936b8832008-10-09 23:06:14 +0800924 if (prefix) {
925 name = (const char *)header +
926 le16_to_cpu(entry->xe_name_offset);
927
928 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
929 &result, prefix, name,
930 entry->xe_name_len);
931 if (ret)
932 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800933 }
934 }
935
Tao Ma936b8832008-10-09 23:06:14 +0800936 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800937}
938
Tao Ma8b2c0db2009-08-18 11:43:49 +0800939int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
940 struct ocfs2_dinode *di)
941{
942 struct ocfs2_xattr_header *xh;
943 int i;
944
945 xh = (struct ocfs2_xattr_header *)
946 ((void *)di + inode->i_sb->s_blocksize -
947 le16_to_cpu(di->i_xattr_inline_size));
948
949 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
950 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
951 return 1;
952
953 return 0;
954}
955
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800956static int ocfs2_xattr_ibody_list(struct inode *inode,
957 struct ocfs2_dinode *di,
958 char *buffer,
959 size_t buffer_size)
960{
961 struct ocfs2_xattr_header *header = NULL;
962 struct ocfs2_inode_info *oi = OCFS2_I(inode);
963 int ret = 0;
964
965 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
966 return ret;
967
968 header = (struct ocfs2_xattr_header *)
969 ((void *)di + inode->i_sb->s_blocksize -
970 le16_to_cpu(di->i_xattr_inline_size));
971
972 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
973
974 return ret;
975}
976
977static int ocfs2_xattr_block_list(struct inode *inode,
978 struct ocfs2_dinode *di,
979 char *buffer,
980 size_t buffer_size)
981{
982 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800983 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800984 int ret = 0;
985
986 if (!di->i_xattr_loc)
987 return ret;
988
Joel Becker4ae1d692008-11-13 14:49:18 -0800989 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
990 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800991 if (ret < 0) {
992 mlog_errno(ret);
993 return ret;
994 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800995
Tao Ma0c044f02008-08-18 17:38:50 +0800996 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma0c044f02008-08-18 17:38:50 +0800997 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
998 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
999 ret = ocfs2_xattr_list_entries(inode, header,
1000 buffer, buffer_size);
Tao Ma47bca492009-08-18 11:43:42 +08001001 } else
1002 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
Tao Ma0c044f02008-08-18 17:38:50 +08001003 buffer, buffer_size);
Joel Becker4ae1d692008-11-13 14:49:18 -08001004
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001005 brelse(blk_bh);
1006
1007 return ret;
1008}
1009
1010ssize_t ocfs2_listxattr(struct dentry *dentry,
1011 char *buffer,
1012 size_t size)
1013{
1014 int ret = 0, i_ret = 0, b_ret = 0;
1015 struct buffer_head *di_bh = NULL;
1016 struct ocfs2_dinode *di = NULL;
David Howells2b0143b2015-03-17 22:25:59 +00001017 struct ocfs2_inode_info *oi = OCFS2_I(d_inode(dentry));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001018
Tiger Yang8154da32008-08-18 17:11:46 +08001019 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1020 return -EOPNOTSUPP;
1021
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001022 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1023 return ret;
1024
David Howells2b0143b2015-03-17 22:25:59 +00001025 ret = ocfs2_inode_lock(d_inode(dentry), &di_bh, 0);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001026 if (ret < 0) {
1027 mlog_errno(ret);
1028 return ret;
1029 }
1030
1031 di = (struct ocfs2_dinode *)di_bh->b_data;
1032
1033 down_read(&oi->ip_xattr_sem);
David Howells2b0143b2015-03-17 22:25:59 +00001034 i_ret = ocfs2_xattr_ibody_list(d_inode(dentry), di, buffer, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001035 if (i_ret < 0)
1036 b_ret = 0;
1037 else {
1038 if (buffer) {
1039 buffer += i_ret;
1040 size -= i_ret;
1041 }
David Howells2b0143b2015-03-17 22:25:59 +00001042 b_ret = ocfs2_xattr_block_list(d_inode(dentry), di,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001043 buffer, size);
1044 if (b_ret < 0)
1045 i_ret = 0;
1046 }
1047 up_read(&oi->ip_xattr_sem);
David Howells2b0143b2015-03-17 22:25:59 +00001048 ocfs2_inode_unlock(d_inode(dentry), 0);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001049
1050 brelse(di_bh);
1051
1052 return i_ret + b_ret;
1053}
1054
1055static int ocfs2_xattr_find_entry(int name_index,
1056 const char *name,
1057 struct ocfs2_xattr_search *xs)
1058{
1059 struct ocfs2_xattr_entry *entry;
1060 size_t name_len;
1061 int i, cmp = 1;
1062
1063 if (name == NULL)
1064 return -EINVAL;
1065
1066 name_len = strlen(name);
1067 entry = xs->here;
1068 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1069 cmp = name_index - ocfs2_xattr_get_type(entry);
1070 if (!cmp)
1071 cmp = name_len - entry->xe_name_len;
1072 if (!cmp)
1073 cmp = memcmp(name, (xs->base +
1074 le16_to_cpu(entry->xe_name_offset)),
1075 name_len);
1076 if (cmp == 0)
1077 break;
1078 entry += 1;
1079 }
1080 xs->here = entry;
1081
1082 return cmp ? -ENODATA : 0;
1083}
1084
1085static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +08001086 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001087 void *buffer,
1088 size_t len)
1089{
1090 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1091 u64 blkno;
1092 int i, ret = 0;
1093 size_t cplen, blocksize;
1094 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001095 struct ocfs2_extent_list *el;
1096
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001097 el = &xv->xr_list;
1098 clusters = le32_to_cpu(xv->xr_clusters);
1099 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1100 blocksize = inode->i_sb->s_blocksize;
1101
1102 cpos = 0;
1103 while (cpos < clusters) {
1104 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001105 &num_clusters, el, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001106 if (ret) {
1107 mlog_errno(ret);
1108 goto out;
1109 }
1110
1111 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1112 /* Copy ocfs2_xattr_value */
1113 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001114 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1115 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001116 if (ret) {
1117 mlog_errno(ret);
1118 goto out;
1119 }
1120
1121 cplen = len >= blocksize ? blocksize : len;
1122 memcpy(buffer, bh->b_data, cplen);
1123 len -= cplen;
1124 buffer += cplen;
1125
1126 brelse(bh);
1127 bh = NULL;
1128 if (len == 0)
1129 break;
1130 }
1131 cpos += num_clusters;
1132 }
1133out:
1134 return ret;
1135}
1136
1137static int ocfs2_xattr_ibody_get(struct inode *inode,
1138 int name_index,
1139 const char *name,
1140 void *buffer,
1141 size_t buffer_size,
1142 struct ocfs2_xattr_search *xs)
1143{
1144 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1145 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +08001146 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001147 size_t size;
1148 int ret = 0;
1149
1150 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1151 return -ENODATA;
1152
1153 xs->end = (void *)di + inode->i_sb->s_blocksize;
1154 xs->header = (struct ocfs2_xattr_header *)
1155 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1156 xs->base = (void *)xs->header;
1157 xs->here = xs->header->xh_entries;
1158
1159 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1160 if (ret)
1161 return ret;
1162 size = le64_to_cpu(xs->here->xe_value_size);
1163 if (buffer) {
1164 if (size > buffer_size)
1165 return -ERANGE;
1166 if (ocfs2_xattr_is_local(xs->here)) {
1167 memcpy(buffer, (void *)xs->base +
1168 le16_to_cpu(xs->here->xe_name_offset) +
1169 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1170 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001171 xv = (struct ocfs2_xattr_value_root *)
1172 (xs->base + le16_to_cpu(
1173 xs->here->xe_name_offset) +
1174 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1175 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001176 buffer, size);
1177 if (ret < 0) {
1178 mlog_errno(ret);
1179 return ret;
1180 }
1181 }
1182 }
1183
1184 return size;
1185}
1186
1187static int ocfs2_xattr_block_get(struct inode *inode,
1188 int name_index,
1189 const char *name,
1190 void *buffer,
1191 size_t buffer_size,
1192 struct ocfs2_xattr_search *xs)
1193{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001194 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +08001195 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001196 size_t size;
Subrata Modak44d8e4e2009-07-14 01:19:31 +05301197 int ret = -ENODATA, name_offset, name_len, i;
1198 int uninitialized_var(block_off);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001199
Joel Beckerba937122008-10-24 19:13:20 -07001200 xs->bucket = ocfs2_xattr_bucket_new(inode);
1201 if (!xs->bucket) {
1202 ret = -ENOMEM;
1203 mlog_errno(ret);
1204 goto cleanup;
1205 }
Tao Ma589dc262008-08-18 17:38:51 +08001206
Joel Becker54f443f2008-10-20 18:43:07 -07001207 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1208 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001209 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001210 goto cleanup;
1211 }
1212
Tiger Yang6c1e1832008-11-02 19:04:21 +08001213 if (xs->not_found) {
1214 ret = -ENODATA;
1215 goto cleanup;
1216 }
1217
Joel Becker54f443f2008-10-20 18:43:07 -07001218 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001219 size = le64_to_cpu(xs->here->xe_value_size);
1220 if (buffer) {
1221 ret = -ERANGE;
1222 if (size > buffer_size)
1223 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +08001224
1225 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1226 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1227 i = xs->here - xs->header->xh_entries;
1228
1229 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08001230 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Beckerba937122008-10-24 19:13:20 -07001231 bucket_xh(xs->bucket),
Tao Ma589dc262008-08-18 17:38:51 +08001232 i,
1233 &block_off,
1234 &name_offset);
Joseph Qi023d4ea2015-04-14 15:43:33 -07001235 if (ret) {
1236 mlog_errno(ret);
1237 goto cleanup;
1238 }
Joel Beckerba937122008-10-24 19:13:20 -07001239 xs->base = bucket_block(xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +08001240 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001241 if (ocfs2_xattr_is_local(xs->here)) {
1242 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +08001243 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001244 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001245 xv = (struct ocfs2_xattr_value_root *)
1246 (xs->base + name_offset + name_len);
1247 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001248 buffer, size);
1249 if (ret < 0) {
1250 mlog_errno(ret);
1251 goto cleanup;
1252 }
1253 }
1254 }
1255 ret = size;
1256cleanup:
Joel Beckerba937122008-10-24 19:13:20 -07001257 ocfs2_xattr_bucket_free(xs->bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001258
Joel Becker54f443f2008-10-20 18:43:07 -07001259 brelse(xs->xattr_bh);
1260 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001261 return ret;
1262}
1263
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001264int ocfs2_xattr_get_nolock(struct inode *inode,
1265 struct buffer_head *di_bh,
Tiger Yang0030e002008-10-23 16:33:33 +08001266 int name_index,
1267 const char *name,
1268 void *buffer,
1269 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001270{
1271 int ret;
1272 struct ocfs2_dinode *di = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001273 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1274 struct ocfs2_xattr_search xis = {
1275 .not_found = -ENODATA,
1276 };
1277 struct ocfs2_xattr_search xbs = {
1278 .not_found = -ENODATA,
1279 };
1280
Tiger Yang8154da32008-08-18 17:11:46 +08001281 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1282 return -EOPNOTSUPP;
1283
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001284 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
Jan Kara2b693002014-12-10 15:41:40 -08001285 return -ENODATA;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001286
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001287 xis.inode_bh = xbs.inode_bh = di_bh;
1288 di = (struct ocfs2_dinode *)di_bh->b_data;
1289
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001290 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1291 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +08001292 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001293 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1294 buffer_size, &xbs);
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001295
1296 return ret;
1297}
1298
1299/* ocfs2_xattr_get()
1300 *
1301 * Copy an extended attribute into the buffer provided.
1302 * Buffer is NULL to compute the size of buffer required.
1303 */
1304static int ocfs2_xattr_get(struct inode *inode,
1305 int name_index,
1306 const char *name,
1307 void *buffer,
1308 size_t buffer_size)
1309{
1310 int ret;
1311 struct buffer_head *di_bh = NULL;
1312
1313 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1314 if (ret < 0) {
1315 mlog_errno(ret);
1316 return ret;
1317 }
Tao Ma5e64b0d2010-09-07 13:30:05 +08001318 down_read(&OCFS2_I(inode)->ip_xattr_sem);
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001319 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1320 name, buffer, buffer_size);
Tao Ma5e64b0d2010-09-07 13:30:05 +08001321 up_read(&OCFS2_I(inode)->ip_xattr_sem);
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001322
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001323 ocfs2_inode_unlock(inode, 0);
1324
1325 brelse(di_bh);
1326
1327 return ret;
1328}
1329
1330static int __ocfs2_xattr_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001331 handle_t *handle,
Tao Ma492a8a32009-08-18 11:43:17 +08001332 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001333 const void *value,
1334 int value_len)
1335{
Tao Ma71d548a2008-12-05 06:20:54 +08001336 int ret = 0, i, cp_len;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001337 u16 blocksize = inode->i_sb->s_blocksize;
1338 u32 p_cluster, num_clusters;
1339 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1340 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1341 u64 blkno;
1342 struct buffer_head *bh = NULL;
Tao Ma492a8a32009-08-18 11:43:17 +08001343 unsigned int ext_flags;
1344 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001345
1346 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1347
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001348 while (cpos < clusters) {
1349 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001350 &num_clusters, &xv->xr_list,
Tao Ma492a8a32009-08-18 11:43:17 +08001351 &ext_flags);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001352 if (ret) {
1353 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001354 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001355 }
1356
Tao Ma492a8a32009-08-18 11:43:17 +08001357 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1358
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001359 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1360
1361 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001362 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1363 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001364 if (ret) {
1365 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001366 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001367 }
1368
1369 ret = ocfs2_journal_access(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001370 INODE_CACHE(inode),
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001371 bh,
1372 OCFS2_JOURNAL_ACCESS_WRITE);
1373 if (ret < 0) {
1374 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001375 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001376 }
1377
1378 cp_len = value_len > blocksize ? blocksize : value_len;
1379 memcpy(bh->b_data, value, cp_len);
1380 value_len -= cp_len;
1381 value += cp_len;
1382 if (cp_len < blocksize)
1383 memset(bh->b_data + cp_len, 0,
1384 blocksize - cp_len);
1385
Joel Beckerec20cec2010-03-19 14:13:52 -07001386 ocfs2_journal_dirty(handle, bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001387 brelse(bh);
1388 bh = NULL;
1389
1390 /*
1391 * XXX: do we need to empty all the following
1392 * blocks in this cluster?
1393 */
1394 if (!value_len)
1395 break;
1396 }
1397 cpos += num_clusters;
1398 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001399out:
1400 brelse(bh);
1401
1402 return ret;
1403}
1404
Joel Becker69a3e532009-08-17 12:24:39 -07001405static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1406 int num_entries)
1407{
1408 int free_space;
1409
1410 if (!needed_space)
1411 return 0;
1412
1413 free_space = free_start -
1414 sizeof(struct ocfs2_xattr_header) -
1415 (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1416 OCFS2_XATTR_HEADER_GAP;
1417 if (free_space < 0)
1418 return -EIO;
1419 if (free_space < needed_space)
1420 return -ENOSPC;
1421
1422 return 0;
1423}
1424
Joel Beckercf2bc802009-08-18 13:52:38 -07001425static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1426 int type)
1427{
1428 return loc->xl_ops->xlo_journal_access(handle, loc, type);
1429}
1430
1431static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1432{
1433 loc->xl_ops->xlo_journal_dirty(handle, loc);
1434}
1435
Joel Becker69a3e532009-08-17 12:24:39 -07001436/* Give a pointer into the storage for the given offset */
1437static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1438{
1439 BUG_ON(offset >= loc->xl_size);
1440 return loc->xl_ops->xlo_offset_pointer(loc, offset);
1441}
1442
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001443/*
Joel Becker11179f22009-08-14 16:07:44 -07001444 * Wipe the name+value pair and allow the storage to reclaim it. This
1445 * must be followed by either removal of the entry or a call to
1446 * ocfs2_xa_add_namevalue().
1447 */
1448static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1449{
1450 loc->xl_ops->xlo_wipe_namevalue(loc);
1451}
1452
Joel Becker69a3e532009-08-17 12:24:39 -07001453/*
1454 * Find lowest offset to a name+value pair. This is the start of our
1455 * downward-growing free space.
1456 */
1457static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1458{
1459 return loc->xl_ops->xlo_get_free_start(loc);
1460}
1461
1462/* Can we reuse loc->xl_entry for xi? */
1463static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1464 struct ocfs2_xattr_info *xi)
1465{
1466 return loc->xl_ops->xlo_can_reuse(loc, xi);
1467}
1468
1469/* How much free space is needed to set the new value */
1470static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1471 struct ocfs2_xattr_info *xi)
1472{
1473 return loc->xl_ops->xlo_check_space(loc, xi);
1474}
1475
1476static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1477{
1478 loc->xl_ops->xlo_add_entry(loc, name_hash);
1479 loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1480 /*
1481 * We can't leave the new entry's xe_name_offset at zero or
1482 * add_namevalue() will go nuts. We set it to the size of our
1483 * storage so that it can never be less than any other entry.
1484 */
1485 loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1486}
1487
1488static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1489 struct ocfs2_xattr_info *xi)
1490{
1491 int size = namevalue_size_xi(xi);
1492 int nameval_offset;
1493 char *nameval_buf;
1494
1495 loc->xl_ops->xlo_add_namevalue(loc, size);
1496 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1497 loc->xl_entry->xe_name_len = xi->xi_name_len;
1498 ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1499 ocfs2_xattr_set_local(loc->xl_entry,
1500 xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1501
1502 nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1503 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1504 memset(nameval_buf, 0, size);
1505 memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1506}
1507
Joel Becker3fc12af2009-08-18 13:20:27 -07001508static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1509 struct ocfs2_xattr_value_buf *vb)
1510{
1511 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1512 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1513
1514 /* Value bufs are for value trees */
Joel Becker73857ee2009-08-18 20:26:41 -07001515 BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
Joel Becker3fc12af2009-08-18 13:20:27 -07001516 BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1517 (name_size + OCFS2_XATTR_ROOT_SIZE));
1518
1519 loc->xl_ops->xlo_fill_value_buf(loc, vb);
1520 vb->vb_xv =
1521 (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1522 nameval_offset +
1523 name_size);
1524}
1525
Joel Beckercf2bc802009-08-18 13:52:38 -07001526static int ocfs2_xa_block_journal_access(handle_t *handle,
1527 struct ocfs2_xa_loc *loc, int type)
1528{
1529 struct buffer_head *bh = loc->xl_storage;
1530 ocfs2_journal_access_func access;
1531
1532 if (loc->xl_size == (bh->b_size -
1533 offsetof(struct ocfs2_xattr_block,
1534 xb_attrs.xb_header)))
1535 access = ocfs2_journal_access_xb;
1536 else
1537 access = ocfs2_journal_access_di;
1538 return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1539}
1540
1541static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1542 struct ocfs2_xa_loc *loc)
1543{
1544 struct buffer_head *bh = loc->xl_storage;
1545
1546 ocfs2_journal_dirty(handle, bh);
1547}
1548
Joel Becker11179f22009-08-14 16:07:44 -07001549static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1550 int offset)
1551{
Joel Becker11179f22009-08-14 16:07:44 -07001552 return (char *)loc->xl_header + offset;
1553}
1554
Joel Becker69a3e532009-08-17 12:24:39 -07001555static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1556 struct ocfs2_xattr_info *xi)
1557{
1558 /*
1559 * Block storage is strict. If the sizes aren't exact, we will
1560 * remove the old one and reinsert the new.
1561 */
1562 return namevalue_size_xe(loc->xl_entry) ==
1563 namevalue_size_xi(xi);
1564}
1565
1566static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1567{
1568 struct ocfs2_xattr_header *xh = loc->xl_header;
1569 int i, count = le16_to_cpu(xh->xh_count);
1570 int offset, free_start = loc->xl_size;
1571
1572 for (i = 0; i < count; i++) {
1573 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1574 if (offset < free_start)
1575 free_start = offset;
1576 }
1577
1578 return free_start;
1579}
1580
1581static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1582 struct ocfs2_xattr_info *xi)
1583{
1584 int count = le16_to_cpu(loc->xl_header->xh_count);
1585 int free_start = ocfs2_xa_get_free_start(loc);
1586 int needed_space = ocfs2_xi_entry_usage(xi);
1587
1588 /*
1589 * Block storage will reclaim the original entry before inserting
1590 * the new value, so we only need the difference. If the new
1591 * entry is smaller than the old one, we don't need anything.
1592 */
1593 if (loc->xl_entry) {
1594 /* Don't need space if we're reusing! */
1595 if (ocfs2_xa_can_reuse_entry(loc, xi))
1596 needed_space = 0;
1597 else
1598 needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1599 }
1600 if (needed_space < 0)
1601 needed_space = 0;
1602 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1603}
1604
Joel Becker11179f22009-08-14 16:07:44 -07001605/*
1606 * Block storage for xattrs keeps the name+value pairs compacted. When
1607 * we remove one, we have to shift any that preceded it towards the end.
1608 */
1609static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1610{
1611 int i, offset;
1612 int namevalue_offset, first_namevalue_offset, namevalue_size;
1613 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1614 struct ocfs2_xattr_header *xh = loc->xl_header;
Joel Becker11179f22009-08-14 16:07:44 -07001615 int count = le16_to_cpu(xh->xh_count);
1616
1617 namevalue_offset = le16_to_cpu(entry->xe_name_offset);
Joel Becker199799a2009-08-14 19:04:15 -07001618 namevalue_size = namevalue_size_xe(entry);
Joel Becker69a3e532009-08-17 12:24:39 -07001619 first_namevalue_offset = ocfs2_xa_get_free_start(loc);
Joel Becker11179f22009-08-14 16:07:44 -07001620
1621 /* Shift the name+value pairs */
1622 memmove((char *)xh + first_namevalue_offset + namevalue_size,
1623 (char *)xh + first_namevalue_offset,
1624 namevalue_offset - first_namevalue_offset);
1625 memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1626
1627 /* Now tell xh->xh_entries about it */
1628 for (i = 0; i < count; i++) {
1629 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
Tao Madfe4d3d2010-03-19 15:04:23 +08001630 if (offset <= namevalue_offset)
Joel Becker11179f22009-08-14 16:07:44 -07001631 le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1632 namevalue_size);
1633 }
1634
1635 /*
1636 * Note that we don't update xh_free_start or xh_name_value_len
1637 * because they're not used in block-stored xattrs.
1638 */
1639}
1640
Joel Becker69a3e532009-08-17 12:24:39 -07001641static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1642{
1643 int count = le16_to_cpu(loc->xl_header->xh_count);
1644 loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1645 le16_add_cpu(&loc->xl_header->xh_count, 1);
1646 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1647}
1648
1649static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1650{
1651 int free_start = ocfs2_xa_get_free_start(loc);
1652
1653 loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1654}
1655
Joel Becker3fc12af2009-08-18 13:20:27 -07001656static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1657 struct ocfs2_xattr_value_buf *vb)
1658{
1659 struct buffer_head *bh = loc->xl_storage;
1660
1661 if (loc->xl_size == (bh->b_size -
1662 offsetof(struct ocfs2_xattr_block,
1663 xb_attrs.xb_header)))
1664 vb->vb_access = ocfs2_journal_access_xb;
1665 else
1666 vb->vb_access = ocfs2_journal_access_di;
1667 vb->vb_bh = bh;
1668}
1669
Joel Becker11179f22009-08-14 16:07:44 -07001670/*
1671 * Operations for xattrs stored in blocks. This includes inline inode
1672 * storage and unindexed ocfs2_xattr_blocks.
1673 */
1674static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
Joel Beckercf2bc802009-08-18 13:52:38 -07001675 .xlo_journal_access = ocfs2_xa_block_journal_access,
1676 .xlo_journal_dirty = ocfs2_xa_block_journal_dirty,
Joel Becker11179f22009-08-14 16:07:44 -07001677 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
Joel Becker69a3e532009-08-17 12:24:39 -07001678 .xlo_check_space = ocfs2_xa_block_check_space,
1679 .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1680 .xlo_get_free_start = ocfs2_xa_block_get_free_start,
Joel Becker11179f22009-08-14 16:07:44 -07001681 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
Joel Becker69a3e532009-08-17 12:24:39 -07001682 .xlo_add_entry = ocfs2_xa_block_add_entry,
1683 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
Joel Becker3fc12af2009-08-18 13:20:27 -07001684 .xlo_fill_value_buf = ocfs2_xa_block_fill_value_buf,
Joel Becker11179f22009-08-14 16:07:44 -07001685};
1686
Joel Beckercf2bc802009-08-18 13:52:38 -07001687static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1688 struct ocfs2_xa_loc *loc, int type)
1689{
1690 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1691
1692 return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1693}
1694
1695static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1696 struct ocfs2_xa_loc *loc)
1697{
1698 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1699
1700 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1701}
1702
Joel Becker11179f22009-08-14 16:07:44 -07001703static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1704 int offset)
1705{
1706 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1707 int block, block_offset;
1708
Joel Becker11179f22009-08-14 16:07:44 -07001709 /* The header is at the front of the bucket */
Joel Beckercf2bc802009-08-18 13:52:38 -07001710 block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1711 block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
Joel Becker11179f22009-08-14 16:07:44 -07001712
1713 return bucket_block(bucket, block) + block_offset;
1714}
1715
Joel Becker69a3e532009-08-17 12:24:39 -07001716static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1717 struct ocfs2_xattr_info *xi)
1718{
1719 return namevalue_size_xe(loc->xl_entry) >=
1720 namevalue_size_xi(xi);
1721}
1722
1723static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1724{
1725 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1726 return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1727}
1728
1729static int ocfs2_bucket_align_free_start(struct super_block *sb,
1730 int free_start, int size)
1731{
1732 /*
1733 * We need to make sure that the name+value pair fits within
1734 * one block.
1735 */
1736 if (((free_start - size) >> sb->s_blocksize_bits) !=
1737 ((free_start - 1) >> sb->s_blocksize_bits))
1738 free_start -= free_start % sb->s_blocksize;
1739
1740 return free_start;
1741}
1742
1743static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1744 struct ocfs2_xattr_info *xi)
1745{
1746 int rc;
1747 int count = le16_to_cpu(loc->xl_header->xh_count);
1748 int free_start = ocfs2_xa_get_free_start(loc);
1749 int needed_space = ocfs2_xi_entry_usage(xi);
1750 int size = namevalue_size_xi(xi);
Joel Beckercf2bc802009-08-18 13:52:38 -07001751 struct super_block *sb = loc->xl_inode->i_sb;
Joel Becker69a3e532009-08-17 12:24:39 -07001752
1753 /*
1754 * Bucket storage does not reclaim name+value pairs it cannot
1755 * reuse. They live as holes until the bucket fills, and then
1756 * the bucket is defragmented. However, the bucket can reclaim
1757 * the ocfs2_xattr_entry.
1758 */
1759 if (loc->xl_entry) {
1760 /* Don't need space if we're reusing! */
1761 if (ocfs2_xa_can_reuse_entry(loc, xi))
1762 needed_space = 0;
1763 else
1764 needed_space -= sizeof(struct ocfs2_xattr_entry);
1765 }
1766 BUG_ON(needed_space < 0);
1767
1768 if (free_start < size) {
1769 if (needed_space)
1770 return -ENOSPC;
1771 } else {
1772 /*
1773 * First we check if it would fit in the first place.
1774 * Below, we align the free start to a block. This may
1775 * slide us below the minimum gap. By checking unaligned
1776 * first, we avoid that error.
1777 */
1778 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1779 count);
1780 if (rc)
1781 return rc;
1782 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1783 size);
1784 }
1785 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1786}
1787
Joel Becker11179f22009-08-14 16:07:44 -07001788static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1789{
Joel Becker199799a2009-08-14 19:04:15 -07001790 le16_add_cpu(&loc->xl_header->xh_name_value_len,
1791 -namevalue_size_xe(loc->xl_entry));
Joel Becker11179f22009-08-14 16:07:44 -07001792}
1793
Joel Becker69a3e532009-08-17 12:24:39 -07001794static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1795{
1796 struct ocfs2_xattr_header *xh = loc->xl_header;
1797 int count = le16_to_cpu(xh->xh_count);
1798 int low = 0, high = count - 1, tmp;
1799 struct ocfs2_xattr_entry *tmp_xe;
1800
1801 /*
1802 * We keep buckets sorted by name_hash, so we need to find
1803 * our insert place.
1804 */
1805 while (low <= high && count) {
1806 tmp = (low + high) / 2;
1807 tmp_xe = &xh->xh_entries[tmp];
1808
1809 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1810 low = tmp + 1;
1811 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1812 high = tmp - 1;
1813 else {
1814 low = tmp;
1815 break;
1816 }
1817 }
1818
1819 if (low != count)
1820 memmove(&xh->xh_entries[low + 1],
1821 &xh->xh_entries[low],
1822 ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1823
1824 le16_add_cpu(&xh->xh_count, 1);
1825 loc->xl_entry = &xh->xh_entries[low];
1826 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1827}
1828
1829static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1830{
1831 int free_start = ocfs2_xa_get_free_start(loc);
1832 struct ocfs2_xattr_header *xh = loc->xl_header;
Joel Beckercf2bc802009-08-18 13:52:38 -07001833 struct super_block *sb = loc->xl_inode->i_sb;
Joel Becker69a3e532009-08-17 12:24:39 -07001834 int nameval_offset;
1835
1836 free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1837 nameval_offset = free_start - size;
1838 loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1839 xh->xh_free_start = cpu_to_le16(nameval_offset);
1840 le16_add_cpu(&xh->xh_name_value_len, size);
1841
1842}
1843
Joel Becker3fc12af2009-08-18 13:20:27 -07001844static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1845 struct ocfs2_xattr_value_buf *vb)
1846{
1847 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
Joel Beckercf2bc802009-08-18 13:52:38 -07001848 struct super_block *sb = loc->xl_inode->i_sb;
Joel Becker3fc12af2009-08-18 13:20:27 -07001849 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1850 int size = namevalue_size_xe(loc->xl_entry);
1851 int block_offset = nameval_offset >> sb->s_blocksize_bits;
1852
1853 /* Values are not allowed to straddle block boundaries */
1854 BUG_ON(block_offset !=
1855 ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1856 /* We expect the bucket to be filled in */
1857 BUG_ON(!bucket->bu_bhs[block_offset]);
1858
1859 vb->vb_access = ocfs2_journal_access;
1860 vb->vb_bh = bucket->bu_bhs[block_offset];
1861}
1862
Joel Becker11179f22009-08-14 16:07:44 -07001863/* Operations for xattrs stored in buckets. */
1864static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
Joel Beckercf2bc802009-08-18 13:52:38 -07001865 .xlo_journal_access = ocfs2_xa_bucket_journal_access,
1866 .xlo_journal_dirty = ocfs2_xa_bucket_journal_dirty,
Joel Becker11179f22009-08-14 16:07:44 -07001867 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
Joel Becker69a3e532009-08-17 12:24:39 -07001868 .xlo_check_space = ocfs2_xa_bucket_check_space,
1869 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1870 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
Joel Becker11179f22009-08-14 16:07:44 -07001871 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
Joel Becker69a3e532009-08-17 12:24:39 -07001872 .xlo_add_entry = ocfs2_xa_bucket_add_entry,
1873 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
Joel Becker3fc12af2009-08-18 13:20:27 -07001874 .xlo_fill_value_buf = ocfs2_xa_bucket_fill_value_buf,
Joel Becker11179f22009-08-14 16:07:44 -07001875};
1876
Joel Becker399ff3a72009-09-01 18:38:27 -07001877static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc *loc)
1878{
1879 struct ocfs2_xattr_value_buf vb;
1880
1881 if (ocfs2_xattr_is_local(loc->xl_entry))
1882 return 0;
1883
1884 ocfs2_xa_fill_value_buf(loc, &vb);
1885 return le32_to_cpu(vb.vb_xv->xr_clusters);
1886}
1887
Joel Becker73857ee2009-08-18 20:26:41 -07001888static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
1889 struct ocfs2_xattr_set_ctxt *ctxt)
1890{
1891 int trunc_rc, access_rc;
1892 struct ocfs2_xattr_value_buf vb;
1893
1894 ocfs2_xa_fill_value_buf(loc, &vb);
1895 trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
1896 ctxt);
1897
1898 /*
1899 * The caller of ocfs2_xa_value_truncate() has already called
1900 * ocfs2_xa_journal_access on the loc. However, The truncate code
1901 * calls ocfs2_extend_trans(). This may commit the previous
1902 * transaction and open a new one. If this is a bucket, truncate
1903 * could leave only vb->vb_bh set up for journaling. Meanwhile,
1904 * the caller is expecting to dirty the entire bucket. So we must
1905 * reset the journal work. We do this even if truncate has failed,
1906 * as it could have failed after committing the extend.
1907 */
1908 access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
1909 OCFS2_JOURNAL_ACCESS_WRITE);
1910
1911 /* Errors in truncate take precedence */
1912 return trunc_rc ? trunc_rc : access_rc;
1913}
1914
Joel Becker11179f22009-08-14 16:07:44 -07001915static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1916{
Joel Beckerbde1e542009-08-14 16:58:38 -07001917 int index, count;
1918 struct ocfs2_xattr_header *xh = loc->xl_header;
1919 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1920
Joel Becker11179f22009-08-14 16:07:44 -07001921 ocfs2_xa_wipe_namevalue(loc);
Joel Beckerbde1e542009-08-14 16:58:38 -07001922 loc->xl_entry = NULL;
1923
1924 le16_add_cpu(&xh->xh_count, -1);
1925 count = le16_to_cpu(xh->xh_count);
1926
1927 /*
1928 * Only zero out the entry if there are more remaining. This is
1929 * important for an empty bucket, as it keeps track of the
1930 * bucket's hash value. It doesn't hurt empty block storage.
1931 */
1932 if (count) {
1933 index = ((char *)entry - (char *)&xh->xh_entries) /
1934 sizeof(struct ocfs2_xattr_entry);
1935 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1936 (count - index) * sizeof(struct ocfs2_xattr_entry));
1937 memset(&xh->xh_entries[count], 0,
1938 sizeof(struct ocfs2_xattr_entry));
1939 }
Joel Becker11179f22009-08-14 16:07:44 -07001940}
1941
Joel Becker399ff3a72009-09-01 18:38:27 -07001942/*
1943 * If we have a problem adjusting the size of an external value during
1944 * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1945 * in an intermediate state. For example, the value may be partially
1946 * truncated.
1947 *
1948 * If the value tree hasn't changed, the extend/truncate went nowhere.
1949 * We have nothing to do. The caller can treat it as a straight error.
1950 *
1951 * If the value tree got partially truncated, we now have a corrupted
1952 * extended attribute. We're going to wipe its entry and leak the
1953 * clusters. Better to leak some storage than leave a corrupt entry.
1954 *
1955 * If the value tree grew, it obviously didn't grow enough for the
1956 * new entry. We're not going to try and reclaim those clusters either.
1957 * If there was already an external value there (orig_clusters != 0),
1958 * the new clusters are attached safely and we can just leave the old
1959 * value in place. If there was no external value there, we remove
1960 * the entry.
1961 *
1962 * This way, the xattr block we store in the journal will be consistent.
1963 * If the size change broke because of the journal, no changes will hit
1964 * disk anyway.
1965 */
1966static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc *loc,
1967 const char *what,
1968 unsigned int orig_clusters)
1969{
1970 unsigned int new_clusters = ocfs2_xa_value_clusters(loc);
1971 char *nameval_buf = ocfs2_xa_offset_pointer(loc,
1972 le16_to_cpu(loc->xl_entry->xe_name_offset));
1973
1974 if (new_clusters < orig_clusters) {
1975 mlog(ML_ERROR,
1976 "Partial truncate while %s xattr %.*s. Leaking "
1977 "%u clusters and removing the entry\n",
1978 what, loc->xl_entry->xe_name_len, nameval_buf,
1979 orig_clusters - new_clusters);
1980 ocfs2_xa_remove_entry(loc);
1981 } else if (!orig_clusters) {
1982 mlog(ML_ERROR,
1983 "Unable to allocate an external value for xattr "
1984 "%.*s safely. Leaking %u clusters and removing the "
1985 "entry\n",
1986 loc->xl_entry->xe_name_len, nameval_buf,
1987 new_clusters - orig_clusters);
1988 ocfs2_xa_remove_entry(loc);
1989 } else if (new_clusters > orig_clusters)
1990 mlog(ML_ERROR,
1991 "Unable to grow xattr %.*s safely. %u new clusters "
1992 "have been added, but the value will not be "
1993 "modified\n",
1994 loc->xl_entry->xe_name_len, nameval_buf,
1995 new_clusters - orig_clusters);
1996}
1997
Joel Becker73857ee2009-08-18 20:26:41 -07001998static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
1999 struct ocfs2_xattr_set_ctxt *ctxt)
2000{
2001 int rc = 0;
Joel Becker399ff3a72009-09-01 18:38:27 -07002002 unsigned int orig_clusters;
Joel Becker73857ee2009-08-18 20:26:41 -07002003
2004 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
Joel Becker399ff3a72009-09-01 18:38:27 -07002005 orig_clusters = ocfs2_xa_value_clusters(loc);
Joel Becker73857ee2009-08-18 20:26:41 -07002006 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2007 if (rc) {
2008 mlog_errno(rc);
Joel Becker399ff3a72009-09-01 18:38:27 -07002009 /*
2010 * Since this is remove, we can return 0 if
2011 * ocfs2_xa_cleanup_value_truncate() is going to
2012 * wipe the entry anyway. So we check the
2013 * cluster count as well.
2014 */
2015 if (orig_clusters != ocfs2_xa_value_clusters(loc))
2016 rc = 0;
2017 ocfs2_xa_cleanup_value_truncate(loc, "removing",
2018 orig_clusters);
2019 if (rc)
2020 goto out;
Joel Becker73857ee2009-08-18 20:26:41 -07002021 }
2022 }
2023
2024 ocfs2_xa_remove_entry(loc);
2025
2026out:
2027 return rc;
2028}
2029
2030static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
2031{
2032 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
2033 char *nameval_buf;
2034
2035 nameval_buf = ocfs2_xa_offset_pointer(loc,
2036 le16_to_cpu(loc->xl_entry->xe_name_offset));
2037 memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
2038}
2039
2040/*
2041 * Take an existing entry and make it ready for the new value. This
2042 * won't allocate space, but it may free space. It should be ready for
2043 * ocfs2_xa_prepare_entry() to finish the work.
2044 */
2045static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
2046 struct ocfs2_xattr_info *xi,
2047 struct ocfs2_xattr_set_ctxt *ctxt)
2048{
2049 int rc = 0;
2050 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
Joel Becker399ff3a72009-09-01 18:38:27 -07002051 unsigned int orig_clusters;
Joel Becker73857ee2009-08-18 20:26:41 -07002052 char *nameval_buf;
2053 int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
2054 int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
2055
2056 BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
2057 name_size);
2058
2059 nameval_buf = ocfs2_xa_offset_pointer(loc,
2060 le16_to_cpu(loc->xl_entry->xe_name_offset));
2061 if (xe_local) {
2062 memset(nameval_buf + name_size, 0,
2063 namevalue_size_xe(loc->xl_entry) - name_size);
2064 if (!xi_local)
2065 ocfs2_xa_install_value_root(loc);
2066 } else {
Joel Becker399ff3a72009-09-01 18:38:27 -07002067 orig_clusters = ocfs2_xa_value_clusters(loc);
Joel Becker73857ee2009-08-18 20:26:41 -07002068 if (xi_local) {
2069 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
Joel Becker399ff3a72009-09-01 18:38:27 -07002070 if (rc < 0)
Joel Becker73857ee2009-08-18 20:26:41 -07002071 mlog_errno(rc);
Joel Becker399ff3a72009-09-01 18:38:27 -07002072 else
2073 memset(nameval_buf + name_size, 0,
2074 namevalue_size_xe(loc->xl_entry) -
2075 name_size);
Joel Becker73857ee2009-08-18 20:26:41 -07002076 } else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
2077 xi->xi_value_len) {
2078 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
2079 ctxt);
Joel Becker399ff3a72009-09-01 18:38:27 -07002080 if (rc < 0)
Joel Becker73857ee2009-08-18 20:26:41 -07002081 mlog_errno(rc);
Joel Becker399ff3a72009-09-01 18:38:27 -07002082 }
2083
2084 if (rc) {
2085 ocfs2_xa_cleanup_value_truncate(loc, "reusing",
2086 orig_clusters);
2087 goto out;
Joel Becker73857ee2009-08-18 20:26:41 -07002088 }
2089 }
2090
2091 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
2092 ocfs2_xattr_set_local(loc->xl_entry, xi_local);
2093
2094out:
2095 return rc;
2096}
2097
Joel Becker69a3e532009-08-17 12:24:39 -07002098/*
2099 * Prepares loc->xl_entry to receive the new xattr. This includes
2100 * properly setting up the name+value pair region. If loc->xl_entry
2101 * already exists, it will take care of modifying it appropriately.
Joel Becker69a3e532009-08-17 12:24:39 -07002102 *
2103 * Note that this modifies the data. You did journal_access already,
2104 * right?
2105 */
2106static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
2107 struct ocfs2_xattr_info *xi,
Joel Becker73857ee2009-08-18 20:26:41 -07002108 u32 name_hash,
2109 struct ocfs2_xattr_set_ctxt *ctxt)
Joel Becker69a3e532009-08-17 12:24:39 -07002110{
2111 int rc = 0;
Joel Becker399ff3a72009-09-01 18:38:27 -07002112 unsigned int orig_clusters;
2113 __le64 orig_value_size = 0;
Joel Becker69a3e532009-08-17 12:24:39 -07002114
Joel Becker69a3e532009-08-17 12:24:39 -07002115 rc = ocfs2_xa_check_space(loc, xi);
2116 if (rc)
2117 goto out;
2118
2119 if (loc->xl_entry) {
2120 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
Joel Becker399ff3a72009-09-01 18:38:27 -07002121 orig_value_size = loc->xl_entry->xe_value_size;
Joel Becker73857ee2009-08-18 20:26:41 -07002122 rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
2123 if (rc)
2124 goto out;
2125 goto alloc_value;
Joel Becker69a3e532009-08-17 12:24:39 -07002126 }
2127
Joel Becker73857ee2009-08-18 20:26:41 -07002128 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
Joel Becker399ff3a72009-09-01 18:38:27 -07002129 orig_clusters = ocfs2_xa_value_clusters(loc);
Joel Becker73857ee2009-08-18 20:26:41 -07002130 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2131 if (rc) {
2132 mlog_errno(rc);
Joel Becker399ff3a72009-09-01 18:38:27 -07002133 ocfs2_xa_cleanup_value_truncate(loc,
2134 "overwriting",
2135 orig_clusters);
Joel Becker73857ee2009-08-18 20:26:41 -07002136 goto out;
2137 }
2138 }
Joel Becker69a3e532009-08-17 12:24:39 -07002139 ocfs2_xa_wipe_namevalue(loc);
2140 } else
2141 ocfs2_xa_add_entry(loc, name_hash);
2142
2143 /*
2144 * If we get here, we have a blank entry. Fill it. We grow our
2145 * name+value pair back from the end.
2146 */
2147 ocfs2_xa_add_namevalue(loc, xi);
Joel Becker73857ee2009-08-18 20:26:41 -07002148 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2149 ocfs2_xa_install_value_root(loc);
2150
2151alloc_value:
2152 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Joel Becker399ff3a72009-09-01 18:38:27 -07002153 orig_clusters = ocfs2_xa_value_clusters(loc);
Joel Becker73857ee2009-08-18 20:26:41 -07002154 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
Joel Becker399ff3a72009-09-01 18:38:27 -07002155 if (rc < 0) {
Tao Ma5f5261a2010-05-13 22:49:05 +08002156 ctxt->set_abort = 1;
Joel Becker399ff3a72009-09-01 18:38:27 -07002157 ocfs2_xa_cleanup_value_truncate(loc, "growing",
2158 orig_clusters);
Tao Mad5a7df02010-05-10 18:09:47 +08002159 /*
2160 * If we were growing an existing value,
2161 * ocfs2_xa_cleanup_value_truncate() won't remove
2162 * the entry. We need to restore the original value
2163 * size.
2164 */
2165 if (loc->xl_entry) {
2166 BUG_ON(!orig_value_size);
2167 loc->xl_entry->xe_value_size = orig_value_size;
2168 }
Joel Becker73857ee2009-08-18 20:26:41 -07002169 mlog_errno(rc);
Joel Becker399ff3a72009-09-01 18:38:27 -07002170 }
Joel Becker73857ee2009-08-18 20:26:41 -07002171 }
Joel Becker69a3e532009-08-17 12:24:39 -07002172
2173out:
2174 return rc;
2175}
2176
2177/*
Joel Becker73857ee2009-08-18 20:26:41 -07002178 * Store the value portion of the name+value pair. This will skip
2179 * values that are stored externally. Their tree roots were set up
2180 * by ocfs2_xa_prepare_entry().
Joel Becker69a3e532009-08-17 12:24:39 -07002181 */
Joel Becker73857ee2009-08-18 20:26:41 -07002182static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
2183 struct ocfs2_xattr_info *xi,
2184 struct ocfs2_xattr_set_ctxt *ctxt)
Joel Becker69a3e532009-08-17 12:24:39 -07002185{
Joel Becker73857ee2009-08-18 20:26:41 -07002186 int rc = 0;
Joel Becker69a3e532009-08-17 12:24:39 -07002187 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2188 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
Joel Becker69a3e532009-08-17 12:24:39 -07002189 char *nameval_buf;
Joel Becker73857ee2009-08-18 20:26:41 -07002190 struct ocfs2_xattr_value_buf vb;
Joel Becker69a3e532009-08-17 12:24:39 -07002191
Joel Becker69a3e532009-08-17 12:24:39 -07002192 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
Joel Becker73857ee2009-08-18 20:26:41 -07002193 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2194 ocfs2_xa_fill_value_buf(loc, &vb);
2195 rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
2196 ctxt->handle, &vb,
2197 xi->xi_value,
2198 xi->xi_value_len);
2199 } else
2200 memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
2201
Joel Becker73857ee2009-08-18 20:26:41 -07002202 return rc;
Joel Becker69a3e532009-08-17 12:24:39 -07002203}
2204
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002205static int ocfs2_xa_set(struct ocfs2_xa_loc *loc,
2206 struct ocfs2_xattr_info *xi,
2207 struct ocfs2_xattr_set_ctxt *ctxt)
2208{
2209 int ret;
2210 u32 name_hash = ocfs2_xattr_name_hash(loc->xl_inode, xi->xi_name,
2211 xi->xi_name_len);
2212
2213 ret = ocfs2_xa_journal_access(ctxt->handle, loc,
2214 OCFS2_JOURNAL_ACCESS_WRITE);
2215 if (ret) {
2216 mlog_errno(ret);
2217 goto out;
2218 }
2219
Joel Becker399ff3a72009-09-01 18:38:27 -07002220 /*
2221 * From here on out, everything is going to modify the buffer a
2222 * little. Errors are going to leave the xattr header in a
2223 * sane state. Thus, even with errors we dirty the sucker.
2224 */
2225
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002226 /* Don't worry, we are never called with !xi_value and !xl_entry */
2227 if (!xi->xi_value) {
2228 ret = ocfs2_xa_remove(loc, ctxt);
Joel Becker399ff3a72009-09-01 18:38:27 -07002229 goto out_dirty;
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002230 }
2231
2232 ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt);
2233 if (ret) {
2234 if (ret != -ENOSPC)
2235 mlog_errno(ret);
Joel Becker399ff3a72009-09-01 18:38:27 -07002236 goto out_dirty;
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002237 }
2238
2239 ret = ocfs2_xa_store_value(loc, xi, ctxt);
Joel Becker399ff3a72009-09-01 18:38:27 -07002240 if (ret)
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002241 mlog_errno(ret);
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002242
Joel Becker399ff3a72009-09-01 18:38:27 -07002243out_dirty:
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002244 ocfs2_xa_journal_dirty(ctxt->handle, loc);
2245
2246out:
2247 return ret;
2248}
2249
Joel Becker11179f22009-08-14 16:07:44 -07002250static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2251 struct inode *inode,
2252 struct buffer_head *bh,
2253 struct ocfs2_xattr_entry *entry)
2254{
2255 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2256
Joel Becker139ffac2009-08-19 11:09:17 -07002257 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL));
2258
Joel Beckercf2bc802009-08-18 13:52:38 -07002259 loc->xl_inode = inode;
Joel Becker11179f22009-08-14 16:07:44 -07002260 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2261 loc->xl_storage = bh;
2262 loc->xl_entry = entry;
Joel Becker139ffac2009-08-19 11:09:17 -07002263 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
Joel Becker11179f22009-08-14 16:07:44 -07002264 loc->xl_header =
2265 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2266 loc->xl_size);
2267}
2268
2269static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
Joel Beckercf2bc802009-08-18 13:52:38 -07002270 struct inode *inode,
Joel Becker11179f22009-08-14 16:07:44 -07002271 struct buffer_head *bh,
2272 struct ocfs2_xattr_entry *entry)
2273{
2274 struct ocfs2_xattr_block *xb =
2275 (struct ocfs2_xattr_block *)bh->b_data;
2276
2277 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2278
Joel Beckercf2bc802009-08-18 13:52:38 -07002279 loc->xl_inode = inode;
Joel Becker11179f22009-08-14 16:07:44 -07002280 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2281 loc->xl_storage = bh;
2282 loc->xl_header = &(xb->xb_attrs.xb_header);
2283 loc->xl_entry = entry;
2284 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2285 xb_attrs.xb_header);
2286}
2287
2288static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2289 struct ocfs2_xattr_bucket *bucket,
2290 struct ocfs2_xattr_entry *entry)
2291{
Joel Beckercf2bc802009-08-18 13:52:38 -07002292 loc->xl_inode = bucket->bu_inode;
Joel Becker11179f22009-08-14 16:07:44 -07002293 loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2294 loc->xl_storage = bucket;
2295 loc->xl_header = bucket_xh(bucket);
2296 loc->xl_entry = entry;
2297 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2298}
2299
Tao Mace9c5a52009-08-18 11:43:59 +08002300/*
2301 * In xattr remove, if it is stored outside and refcounted, we may have
2302 * the chance to split the refcount tree. So need the allocators.
2303 */
2304static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2305 struct ocfs2_xattr_value_root *xv,
2306 struct ocfs2_caching_info *ref_ci,
2307 struct buffer_head *ref_root_bh,
2308 struct ocfs2_alloc_context **meta_ac,
2309 int *ref_credits)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002310{
Tao Mace9c5a52009-08-18 11:43:59 +08002311 int ret, meta_add = 0;
2312 u32 p_cluster, num_clusters;
2313 unsigned int ext_flags;
Tao Ma78f30c32008-11-12 08:27:00 +08002314
Tao Mace9c5a52009-08-18 11:43:59 +08002315 *ref_credits = 0;
2316 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2317 &num_clusters,
2318 &xv->xr_list,
2319 &ext_flags);
2320 if (ret) {
Tao Ma85db90e2008-11-12 08:27:01 +08002321 mlog_errno(ret);
2322 goto out;
2323 }
2324
Tao Mace9c5a52009-08-18 11:43:59 +08002325 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2326 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002327
Tao Mace9c5a52009-08-18 11:43:59 +08002328 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2329 ref_root_bh, xv,
2330 &meta_add, ref_credits);
2331 if (ret) {
2332 mlog_errno(ret);
2333 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002334 }
2335
Tao Mace9c5a52009-08-18 11:43:59 +08002336 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2337 meta_add, meta_ac);
2338 if (ret)
2339 mlog_errno(ret);
2340
Tao Ma85db90e2008-11-12 08:27:01 +08002341out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002342 return ret;
2343}
2344
Tao Mace9c5a52009-08-18 11:43:59 +08002345static int ocfs2_remove_value_outside(struct inode*inode,
2346 struct ocfs2_xattr_value_buf *vb,
2347 struct ocfs2_xattr_header *header,
2348 struct ocfs2_caching_info *ref_ci,
2349 struct buffer_head *ref_root_bh)
2350{
2351 int ret = 0, i, ref_credits;
2352 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2353 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2354 void *val;
2355
2356 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2357
2358 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2359 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2360
2361 if (ocfs2_xattr_is_local(entry))
2362 continue;
2363
2364 val = (void *)header +
2365 le16_to_cpu(entry->xe_name_offset);
2366 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2367 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2368
2369 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2370 ref_ci, ref_root_bh,
2371 &ctxt.meta_ac,
2372 &ref_credits);
2373
2374 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2375 ocfs2_remove_extent_credits(osb->sb));
2376 if (IS_ERR(ctxt.handle)) {
2377 ret = PTR_ERR(ctxt.handle);
2378 mlog_errno(ret);
2379 break;
2380 }
2381
2382 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
Tao Mace9c5a52009-08-18 11:43:59 +08002383
2384 ocfs2_commit_trans(osb, ctxt.handle);
2385 if (ctxt.meta_ac) {
2386 ocfs2_free_alloc_context(ctxt.meta_ac);
2387 ctxt.meta_ac = NULL;
2388 }
Wengang Wangb8a0ae52011-10-12 15:22:15 +08002389
2390 if (ret < 0) {
2391 mlog_errno(ret);
2392 break;
2393 }
2394
Tao Mace9c5a52009-08-18 11:43:59 +08002395 }
2396
2397 if (ctxt.meta_ac)
2398 ocfs2_free_alloc_context(ctxt.meta_ac);
2399 ocfs2_schedule_truncate_log_flush(osb, 1);
2400 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2401 return ret;
2402}
2403
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002404static int ocfs2_xattr_ibody_remove(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002405 struct buffer_head *di_bh,
2406 struct ocfs2_caching_info *ref_ci,
2407 struct buffer_head *ref_root_bh)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002408{
2409
2410 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2411 struct ocfs2_xattr_header *header;
2412 int ret;
Joel Becker43119012008-12-09 16:24:43 -08002413 struct ocfs2_xattr_value_buf vb = {
2414 .vb_bh = di_bh,
2415 .vb_access = ocfs2_journal_access_di,
2416 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002417
2418 header = (struct ocfs2_xattr_header *)
2419 ((void *)di + inode->i_sb->s_blocksize -
2420 le16_to_cpu(di->i_xattr_inline_size));
2421
Tao Mace9c5a52009-08-18 11:43:59 +08002422 ret = ocfs2_remove_value_outside(inode, &vb, header,
2423 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002424
2425 return ret;
2426}
2427
Tao Mace9c5a52009-08-18 11:43:59 +08002428struct ocfs2_rm_xattr_bucket_para {
2429 struct ocfs2_caching_info *ref_ci;
2430 struct buffer_head *ref_root_bh;
2431};
2432
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002433static int ocfs2_xattr_block_remove(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002434 struct buffer_head *blk_bh,
2435 struct ocfs2_caching_info *ref_ci,
2436 struct buffer_head *ref_root_bh)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002437{
2438 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002439 int ret = 0;
Joel Becker43119012008-12-09 16:24:43 -08002440 struct ocfs2_xattr_value_buf vb = {
2441 .vb_bh = blk_bh,
2442 .vb_access = ocfs2_journal_access_xb,
2443 };
Tao Mace9c5a52009-08-18 11:43:59 +08002444 struct ocfs2_rm_xattr_bucket_para args = {
2445 .ref_ci = ref_ci,
2446 .ref_root_bh = ref_root_bh,
2447 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002448
2449 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08002450 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2451 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
Tao Mace9c5a52009-08-18 11:43:59 +08002452 ret = ocfs2_remove_value_outside(inode, &vb, header,
2453 ref_ci, ref_root_bh);
Tao Maa3944252008-08-18 17:38:54 +08002454 } else
Tao Ma47bca492009-08-18 11:43:42 +08002455 ret = ocfs2_iterate_xattr_index_block(inode,
2456 blk_bh,
2457 ocfs2_rm_xattr_cluster,
Tao Mace9c5a52009-08-18 11:43:59 +08002458 &args);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002459
2460 return ret;
2461}
2462
Tao Ma08413892008-08-29 09:00:19 +08002463static int ocfs2_xattr_free_block(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002464 u64 block,
2465 struct ocfs2_caching_info *ref_ci,
2466 struct buffer_head *ref_root_bh)
Tao Ma08413892008-08-29 09:00:19 +08002467{
2468 struct inode *xb_alloc_inode;
2469 struct buffer_head *xb_alloc_bh = NULL;
2470 struct buffer_head *blk_bh = NULL;
2471 struct ocfs2_xattr_block *xb;
2472 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2473 handle_t *handle;
2474 int ret = 0;
2475 u64 blk, bg_blkno;
2476 u16 bit;
2477
Joel Becker4ae1d692008-11-13 14:49:18 -08002478 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08002479 if (ret < 0) {
2480 mlog_errno(ret);
2481 goto out;
2482 }
2483
Tao Mace9c5a52009-08-18 11:43:59 +08002484 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
Tao Ma08413892008-08-29 09:00:19 +08002485 if (ret < 0) {
2486 mlog_errno(ret);
2487 goto out;
2488 }
2489
Joel Becker4ae1d692008-11-13 14:49:18 -08002490 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma08413892008-08-29 09:00:19 +08002491 blk = le64_to_cpu(xb->xb_blkno);
2492 bit = le16_to_cpu(xb->xb_suballoc_bit);
Tao Ma74380c42010-03-22 14:20:18 +08002493 if (xb->xb_suballoc_loc)
2494 bg_blkno = le64_to_cpu(xb->xb_suballoc_loc);
2495 else
2496 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
Tao Ma08413892008-08-29 09:00:19 +08002497
2498 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2499 EXTENT_ALLOC_SYSTEM_INODE,
2500 le16_to_cpu(xb->xb_suballoc_slot));
2501 if (!xb_alloc_inode) {
2502 ret = -ENOMEM;
2503 mlog_errno(ret);
2504 goto out;
2505 }
2506 mutex_lock(&xb_alloc_inode->i_mutex);
2507
2508 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2509 if (ret < 0) {
2510 mlog_errno(ret);
2511 goto out_mutex;
2512 }
2513
2514 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2515 if (IS_ERR(handle)) {
2516 ret = PTR_ERR(handle);
2517 mlog_errno(ret);
2518 goto out_unlock;
2519 }
2520
2521 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2522 bit, bg_blkno, 1);
2523 if (ret < 0)
2524 mlog_errno(ret);
2525
2526 ocfs2_commit_trans(osb, handle);
2527out_unlock:
2528 ocfs2_inode_unlock(xb_alloc_inode, 1);
2529 brelse(xb_alloc_bh);
2530out_mutex:
2531 mutex_unlock(&xb_alloc_inode->i_mutex);
2532 iput(xb_alloc_inode);
2533out:
2534 brelse(blk_bh);
2535 return ret;
2536}
2537
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002538/*
2539 * ocfs2_xattr_remove()
2540 *
2541 * Free extended attribute resources associated with this inode.
2542 */
2543int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2544{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002545 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2546 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Tao Mace9c5a52009-08-18 11:43:59 +08002547 struct ocfs2_refcount_tree *ref_tree = NULL;
2548 struct buffer_head *ref_root_bh = NULL;
2549 struct ocfs2_caching_info *ref_ci = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002550 handle_t *handle;
2551 int ret;
2552
Tiger Yang8154da32008-08-18 17:11:46 +08002553 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2554 return 0;
2555
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002556 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2557 return 0;
2558
Tao Mace9c5a52009-08-18 11:43:59 +08002559 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2560 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2561 le64_to_cpu(di->i_refcount_loc),
2562 1, &ref_tree, &ref_root_bh);
2563 if (ret) {
2564 mlog_errno(ret);
2565 goto out;
2566 }
2567 ref_ci = &ref_tree->rf_ci;
2568
2569 }
2570
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002571 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
Tao Mace9c5a52009-08-18 11:43:59 +08002572 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2573 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002574 if (ret < 0) {
2575 mlog_errno(ret);
2576 goto out;
2577 }
2578 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002579
Tao Ma08413892008-08-29 09:00:19 +08002580 if (di->i_xattr_loc) {
2581 ret = ocfs2_xattr_free_block(inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002582 le64_to_cpu(di->i_xattr_loc),
2583 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002584 if (ret < 0) {
2585 mlog_errno(ret);
2586 goto out;
2587 }
2588 }
2589
2590 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2591 OCFS2_INODE_UPDATE_CREDITS);
2592 if (IS_ERR(handle)) {
2593 ret = PTR_ERR(handle);
2594 mlog_errno(ret);
2595 goto out;
2596 }
Joel Becker0cf2f762009-02-12 16:41:25 -08002597 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker84008972008-12-09 16:11:49 -08002598 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002599 if (ret) {
2600 mlog_errno(ret);
2601 goto out_commit;
2602 }
2603
Tao Ma08413892008-08-29 09:00:19 +08002604 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002605
2606 spin_lock(&oi->ip_lock);
2607 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2608 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2609 spin_unlock(&oi->ip_lock);
Darrick J. Wong6fdb7022014-04-03 14:47:08 -07002610 ocfs2_update_inode_fsync_trans(handle, inode, 0);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002611
Joel Beckerec20cec2010-03-19 14:13:52 -07002612 ocfs2_journal_dirty(handle, di_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002613out_commit:
2614 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2615out:
Tao Mace9c5a52009-08-18 11:43:59 +08002616 if (ref_tree)
2617 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2618 brelse(ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002619 return ret;
2620}
2621
2622static int ocfs2_xattr_has_space_inline(struct inode *inode,
2623 struct ocfs2_dinode *di)
2624{
2625 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2626 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2627 int free;
2628
2629 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2630 return 0;
2631
2632 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2633 struct ocfs2_inline_data *idata = &di->id2.i_data;
2634 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2635 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2636 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2637 le64_to_cpu(di->i_size);
2638 } else {
2639 struct ocfs2_extent_list *el = &di->id2.i_list;
2640 free = (le16_to_cpu(el->l_count) -
2641 le16_to_cpu(el->l_next_free_rec)) *
2642 sizeof(struct ocfs2_extent_rec);
2643 }
2644 if (free >= xattrsize)
2645 return 1;
2646
2647 return 0;
2648}
2649
2650/*
2651 * ocfs2_xattr_ibody_find()
2652 *
2653 * Find extended attribute in inode block and
2654 * fill search info into struct ocfs2_xattr_search.
2655 */
2656static int ocfs2_xattr_ibody_find(struct inode *inode,
2657 int name_index,
2658 const char *name,
2659 struct ocfs2_xattr_search *xs)
2660{
2661 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2662 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2663 int ret;
2664 int has_space = 0;
2665
2666 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2667 return 0;
2668
2669 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2670 down_read(&oi->ip_alloc_sem);
2671 has_space = ocfs2_xattr_has_space_inline(inode, di);
2672 up_read(&oi->ip_alloc_sem);
2673 if (!has_space)
2674 return 0;
2675 }
2676
2677 xs->xattr_bh = xs->inode_bh;
2678 xs->end = (void *)di + inode->i_sb->s_blocksize;
2679 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2680 xs->header = (struct ocfs2_xattr_header *)
2681 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2682 else
2683 xs->header = (struct ocfs2_xattr_header *)
2684 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2685 xs->base = (void *)xs->header;
2686 xs->here = xs->header->xh_entries;
2687
2688 /* Find the named attribute. */
2689 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2690 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2691 if (ret && ret != -ENODATA)
2692 return ret;
2693 xs->not_found = ret;
2694 }
2695
2696 return 0;
2697}
2698
Joel Becker139ffac2009-08-19 11:09:17 -07002699static int ocfs2_xattr_ibody_init(struct inode *inode,
2700 struct buffer_head *di_bh,
2701 struct ocfs2_xattr_set_ctxt *ctxt)
2702{
2703 int ret;
2704 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2705 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2706 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2707 unsigned int xattrsize = osb->s_xattr_inline_size;
2708
2709 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2710 ret = -ENOSPC;
2711 goto out;
2712 }
2713
2714 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), di_bh,
2715 OCFS2_JOURNAL_ACCESS_WRITE);
2716 if (ret) {
2717 mlog_errno(ret);
2718 goto out;
2719 }
2720
2721 /*
2722 * Adjust extent record count or inline data size
2723 * to reserve space for extended attribute.
2724 */
2725 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2726 struct ocfs2_inline_data *idata = &di->id2.i_data;
2727 le16_add_cpu(&idata->id_count, -xattrsize);
2728 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2729 struct ocfs2_extent_list *el = &di->id2.i_list;
2730 le16_add_cpu(&el->l_count, -(xattrsize /
2731 sizeof(struct ocfs2_extent_rec)));
2732 }
2733 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2734
2735 spin_lock(&oi->ip_lock);
2736 oi->ip_dyn_features |= OCFS2_INLINE_XATTR_FL|OCFS2_HAS_XATTR_FL;
2737 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2738 spin_unlock(&oi->ip_lock);
2739
Joel Beckerec20cec2010-03-19 14:13:52 -07002740 ocfs2_journal_dirty(ctxt->handle, di_bh);
Joel Becker139ffac2009-08-19 11:09:17 -07002741
2742out:
2743 return ret;
2744}
2745
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002746/*
2747 * ocfs2_xattr_ibody_set()
2748 *
2749 * Set, replace or remove an extended attribute into inode block.
2750 *
2751 */
2752static int ocfs2_xattr_ibody_set(struct inode *inode,
2753 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002754 struct ocfs2_xattr_search *xs,
2755 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002756{
Joel Becker139ffac2009-08-19 11:09:17 -07002757 int ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002758 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Joel Becker139ffac2009-08-19 11:09:17 -07002759 struct ocfs2_xa_loc loc;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002760
2761 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2762 return -ENOSPC;
2763
2764 down_write(&oi->ip_alloc_sem);
2765 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
Joel Becker139ffac2009-08-19 11:09:17 -07002766 ret = ocfs2_xattr_ibody_init(inode, xs->inode_bh, ctxt);
2767 if (ret) {
2768 if (ret != -ENOSPC)
2769 mlog_errno(ret);
2770 goto out;
2771 }
2772 }
2773
2774 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2775 xs->not_found ? NULL : xs->here);
2776 ret = ocfs2_xa_set(&loc, xi, ctxt);
2777 if (ret) {
2778 if (ret != -ENOSPC)
2779 mlog_errno(ret);
2780 goto out;
2781 }
2782 xs->here = loc.xl_entry;
2783
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002784out:
2785 up_write(&oi->ip_alloc_sem);
2786
2787 return ret;
2788}
2789
2790/*
2791 * ocfs2_xattr_block_find()
2792 *
2793 * Find extended attribute in external block and
2794 * fill search info into struct ocfs2_xattr_search.
2795 */
2796static int ocfs2_xattr_block_find(struct inode *inode,
2797 int name_index,
2798 const char *name,
2799 struct ocfs2_xattr_search *xs)
2800{
2801 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2802 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002803 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002804 int ret = 0;
2805
2806 if (!di->i_xattr_loc)
2807 return ret;
2808
Joel Becker4ae1d692008-11-13 14:49:18 -08002809 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2810 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002811 if (ret < 0) {
2812 mlog_errno(ret);
2813 return ret;
2814 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07002815
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002816 xs->xattr_bh = blk_bh;
Joel Becker4ae1d692008-11-13 14:49:18 -08002817 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002818
Tao Ma589dc262008-08-18 17:38:51 +08002819 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2820 xs->header = &xb->xb_attrs.xb_header;
2821 xs->base = (void *)xs->header;
2822 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2823 xs->here = xs->header->xh_entries;
2824
2825 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2826 } else
2827 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2828 name_index,
2829 name, xs);
2830
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002831 if (ret && ret != -ENODATA) {
2832 xs->xattr_bh = NULL;
2833 goto cleanup;
2834 }
2835 xs->not_found = ret;
2836 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002837cleanup:
2838 brelse(blk_bh);
2839
2840 return ret;
2841}
2842
Joel Beckerd3981542009-08-19 02:13:50 -07002843static int ocfs2_create_xattr_block(struct inode *inode,
Tao Ma5aea1f02009-08-18 11:43:24 +08002844 struct buffer_head *inode_bh,
Joel Beckerd3981542009-08-19 02:13:50 -07002845 struct ocfs2_xattr_set_ctxt *ctxt,
2846 int indexed,
2847 struct buffer_head **ret_bh)
Tao Ma5aea1f02009-08-18 11:43:24 +08002848{
2849 int ret;
2850 u16 suballoc_bit_start;
2851 u32 num_got;
Joel Becker2b6cb572010-03-26 10:09:15 +08002852 u64 suballoc_loc, first_blkno;
Tao Ma5aea1f02009-08-18 11:43:24 +08002853 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
Tao Ma5aea1f02009-08-18 11:43:24 +08002854 struct buffer_head *new_bh = NULL;
2855 struct ocfs2_xattr_block *xblk;
2856
Joel Beckerd3981542009-08-19 02:13:50 -07002857 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2858 inode_bh, OCFS2_JOURNAL_ACCESS_CREATE);
Tao Ma5aea1f02009-08-18 11:43:24 +08002859 if (ret < 0) {
2860 mlog_errno(ret);
2861 goto end;
2862 }
2863
Joel Becker1ed9b772010-05-06 13:59:06 +08002864 ret = ocfs2_claim_metadata(ctxt->handle, ctxt->meta_ac, 1,
Joel Becker2b6cb572010-03-26 10:09:15 +08002865 &suballoc_loc, &suballoc_bit_start,
2866 &num_got, &first_blkno);
Tao Ma5aea1f02009-08-18 11:43:24 +08002867 if (ret < 0) {
2868 mlog_errno(ret);
2869 goto end;
2870 }
2871
2872 new_bh = sb_getblk(inode->i_sb, first_blkno);
Rui Xiang58796202013-11-12 15:06:55 -08002873 if (!new_bh) {
2874 ret = -ENOMEM;
2875 mlog_errno(ret);
2876 goto end;
2877 }
2878
Tao Ma5aea1f02009-08-18 11:43:24 +08002879 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2880
Joel Beckerd3981542009-08-19 02:13:50 -07002881 ret = ocfs2_journal_access_xb(ctxt->handle, INODE_CACHE(inode),
Tao Ma5aea1f02009-08-18 11:43:24 +08002882 new_bh,
2883 OCFS2_JOURNAL_ACCESS_CREATE);
2884 if (ret < 0) {
2885 mlog_errno(ret);
2886 goto end;
2887 }
2888
2889 /* Initialize ocfs2_xattr_block */
2890 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2891 memset(xblk, 0, inode->i_sb->s_blocksize);
2892 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
Joel Beckerd3981542009-08-19 02:13:50 -07002893 xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot);
Joel Becker2b6cb572010-03-26 10:09:15 +08002894 xblk->xb_suballoc_loc = cpu_to_le64(suballoc_loc);
Tao Ma5aea1f02009-08-18 11:43:24 +08002895 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
Joel Becker1ed9b772010-05-06 13:59:06 +08002896 xblk->xb_fs_generation =
2897 cpu_to_le32(OCFS2_SB(inode->i_sb)->fs_generation);
Tao Ma5aea1f02009-08-18 11:43:24 +08002898 xblk->xb_blkno = cpu_to_le64(first_blkno);
Tao Maa7fe7a32009-08-18 11:43:52 +08002899 if (indexed) {
2900 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2901 xr->xt_clusters = cpu_to_le32(1);
2902 xr->xt_last_eb_blk = 0;
2903 xr->xt_list.l_tree_depth = 0;
2904 xr->xt_list.l_count = cpu_to_le16(
2905 ocfs2_xattr_recs_per_xb(inode->i_sb));
2906 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2907 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2908 }
Joel Beckerd3981542009-08-19 02:13:50 -07002909 ocfs2_journal_dirty(ctxt->handle, new_bh);
Tao Maa7fe7a32009-08-18 11:43:52 +08002910
Joel Beckerd3981542009-08-19 02:13:50 -07002911 /* Add it to the inode */
Tao Ma5aea1f02009-08-18 11:43:24 +08002912 di->i_xattr_loc = cpu_to_le64(first_blkno);
Joel Beckerd3981542009-08-19 02:13:50 -07002913
2914 spin_lock(&OCFS2_I(inode)->ip_lock);
2915 OCFS2_I(inode)->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
2916 di->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
2917 spin_unlock(&OCFS2_I(inode)->ip_lock);
2918
2919 ocfs2_journal_dirty(ctxt->handle, inode_bh);
Tao Ma5aea1f02009-08-18 11:43:24 +08002920
2921 *ret_bh = new_bh;
2922 new_bh = NULL;
2923
2924end:
2925 brelse(new_bh);
2926 return ret;
2927}
2928
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002929/*
2930 * ocfs2_xattr_block_set()
2931 *
2932 * Set, replace or remove an extended attribute into external block.
2933 *
2934 */
2935static int ocfs2_xattr_block_set(struct inode *inode,
2936 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002937 struct ocfs2_xattr_search *xs,
2938 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002939{
2940 struct buffer_head *new_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002941 struct ocfs2_xattr_block *xblk = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002942 int ret;
Joel Beckerd3981542009-08-19 02:13:50 -07002943 struct ocfs2_xa_loc loc;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002944
2945 if (!xs->xattr_bh) {
Joel Beckerd3981542009-08-19 02:13:50 -07002946 ret = ocfs2_create_xattr_block(inode, xs->inode_bh, ctxt,
2947 0, &new_bh);
Tao Ma5aea1f02009-08-18 11:43:24 +08002948 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002949 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002950 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002951 }
2952
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002953 xs->xattr_bh = new_bh;
Tao Ma5aea1f02009-08-18 11:43:24 +08002954 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002955 xs->header = &xblk->xb_attrs.xb_header;
2956 xs->base = (void *)xs->header;
2957 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2958 xs->here = xs->header->xh_entries;
Tao Ma01225592008-08-18 17:38:53 +08002959 } else
2960 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2961
2962 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
Joel Beckerd3981542009-08-19 02:13:50 -07002963 ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
2964 xs->not_found ? NULL : xs->here);
Tao Ma01225592008-08-18 17:38:53 +08002965
Joel Beckerd3981542009-08-19 02:13:50 -07002966 ret = ocfs2_xa_set(&loc, xi, ctxt);
2967 if (!ret)
2968 xs->here = loc.xl_entry;
Tao Ma5f5261a2010-05-13 22:49:05 +08002969 else if ((ret != -ENOSPC) || ctxt->set_abort)
Tao Ma01225592008-08-18 17:38:53 +08002970 goto end;
Joel Beckerd3981542009-08-19 02:13:50 -07002971 else {
2972 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2973 if (ret)
2974 goto end;
2975 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002976 }
2977
Joel Beckerd3981542009-08-19 02:13:50 -07002978 if (le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)
2979 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002980
2981end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002982 return ret;
2983}
2984
Tao Ma78f30c32008-11-12 08:27:00 +08002985/* Check whether the new xattr can be inserted into the inode. */
2986static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2987 struct ocfs2_xattr_info *xi,
2988 struct ocfs2_xattr_search *xs)
2989{
Tao Ma78f30c32008-11-12 08:27:00 +08002990 struct ocfs2_xattr_entry *last;
2991 int free, i;
2992 size_t min_offs = xs->end - xs->base;
2993
2994 if (!xs->header)
2995 return 0;
2996
2997 last = xs->header->xh_entries;
2998
2999 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
3000 size_t offs = le16_to_cpu(last->xe_name_offset);
3001 if (offs < min_offs)
3002 min_offs = offs;
3003 last += 1;
3004 }
3005
Tiger Yang4442f512009-02-20 11:11:50 +08003006 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
Tao Ma78f30c32008-11-12 08:27:00 +08003007 if (free < 0)
3008 return 0;
3009
3010 BUG_ON(!xs->not_found);
3011
Joel Becker199799a2009-08-14 19:04:15 -07003012 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
Tao Ma78f30c32008-11-12 08:27:00 +08003013 return 1;
3014
3015 return 0;
3016}
3017
3018static int ocfs2_calc_xattr_set_need(struct inode *inode,
3019 struct ocfs2_dinode *di,
3020 struct ocfs2_xattr_info *xi,
3021 struct ocfs2_xattr_search *xis,
3022 struct ocfs2_xattr_search *xbs,
3023 int *clusters_need,
Tao Ma85db90e2008-11-12 08:27:01 +08003024 int *meta_need,
3025 int *credits_need)
Tao Ma78f30c32008-11-12 08:27:00 +08003026{
3027 int ret = 0, old_in_xb = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08003028 int clusters_add = 0, meta_add = 0, credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08003029 struct buffer_head *bh = NULL;
3030 struct ocfs2_xattr_block *xb = NULL;
3031 struct ocfs2_xattr_entry *xe = NULL;
3032 struct ocfs2_xattr_value_root *xv = NULL;
3033 char *base = NULL;
3034 int name_offset, name_len = 0;
3035 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
Joel Becker6b240ff2009-08-14 18:02:52 -07003036 xi->xi_value_len);
Tao Ma78f30c32008-11-12 08:27:00 +08003037 u64 value_size;
3038
Tao Ma71d548a2008-12-05 06:20:54 +08003039 /*
3040 * Calculate the clusters we need to write.
3041 * No matter whether we replace an old one or add a new one,
3042 * we need this for writing.
3043 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003044 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
Tao Ma71d548a2008-12-05 06:20:54 +08003045 credits += new_clusters *
3046 ocfs2_clusters_to_blocks(inode->i_sb, 1);
3047
Tao Ma78f30c32008-11-12 08:27:00 +08003048 if (xis->not_found && xbs->not_found) {
Tao Ma85db90e2008-11-12 08:27:01 +08003049 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3050
Joel Becker6b240ff2009-08-14 18:02:52 -07003051 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08003052 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08003053 credits += ocfs2_calc_extend_credits(inode->i_sb,
Goldwyn Rodrigues06f9da62013-11-12 15:06:52 -08003054 &def_xv.xv.xr_list);
Tao Ma85db90e2008-11-12 08:27:01 +08003055 }
Tao Ma78f30c32008-11-12 08:27:00 +08003056
3057 goto meta_guess;
3058 }
3059
3060 if (!xis->not_found) {
3061 xe = xis->here;
3062 name_offset = le16_to_cpu(xe->xe_name_offset);
3063 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3064 base = xis->base;
Tao Ma85db90e2008-11-12 08:27:01 +08003065 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08003066 } else {
Joel Becker970e4932008-11-13 14:49:19 -08003067 int i, block_off = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08003068 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3069 xe = xbs->here;
3070 name_offset = le16_to_cpu(xe->xe_name_offset);
3071 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3072 i = xbs->here - xbs->header->xh_entries;
3073 old_in_xb = 1;
3074
3075 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08003076 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma78f30c32008-11-12 08:27:00 +08003077 bucket_xh(xbs->bucket),
3078 i, &block_off,
3079 &name_offset);
3080 base = bucket_block(xbs->bucket, block_off);
Tao Ma85db90e2008-11-12 08:27:01 +08003081 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3082 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08003083 base = xbs->base;
Tao Ma85db90e2008-11-12 08:27:01 +08003084 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
3085 }
3086 }
3087
3088 /*
3089 * delete a xattr doesn't need metadata and cluster allocation.
3090 * so just calculate the credits and return.
3091 *
3092 * The credits for removing the value tree will be extended
3093 * by ocfs2_remove_extent itself.
3094 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003095 if (!xi->xi_value) {
Tao Ma85db90e2008-11-12 08:27:01 +08003096 if (!ocfs2_xattr_is_local(xe))
Jan Karaa90714c2008-10-09 19:38:40 +02003097 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08003098
3099 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08003100 }
3101
3102 /* do cluster allocation guess first. */
3103 value_size = le64_to_cpu(xe->xe_value_size);
3104
3105 if (old_in_xb) {
3106 /*
3107 * In xattr set, we always try to set the xe in inode first,
3108 * so if it can be inserted into inode successfully, the old
3109 * one will be removed from the xattr block, and this xattr
3110 * will be inserted into inode as a new xattr in inode.
3111 */
3112 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3113 clusters_add += new_clusters;
Jan Karaa90714c2008-10-09 19:38:40 +02003114 credits += ocfs2_remove_extent_credits(inode->i_sb) +
Tao Ma85db90e2008-11-12 08:27:01 +08003115 OCFS2_INODE_UPDATE_CREDITS;
3116 if (!ocfs2_xattr_is_local(xe))
3117 credits += ocfs2_calc_extend_credits(
3118 inode->i_sb,
Goldwyn Rodrigues06f9da62013-11-12 15:06:52 -08003119 &def_xv.xv.xr_list);
Tao Ma78f30c32008-11-12 08:27:00 +08003120 goto out;
3121 }
3122 }
3123
Joel Becker6b240ff2009-08-14 18:02:52 -07003124 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08003125 /* the new values will be stored outside. */
3126 u32 old_clusters = 0;
3127
3128 if (!ocfs2_xattr_is_local(xe)) {
3129 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3130 value_size);
3131 xv = (struct ocfs2_xattr_value_root *)
3132 (base + name_offset + name_len);
Tao Ma97aff522008-11-19 16:48:41 +08003133 value_size = OCFS2_XATTR_ROOT_SIZE;
Tao Ma78f30c32008-11-12 08:27:00 +08003134 } else
3135 xv = &def_xv.xv;
3136
Tao Ma85db90e2008-11-12 08:27:01 +08003137 if (old_clusters >= new_clusters) {
Jan Karaa90714c2008-10-09 19:38:40 +02003138 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003139 goto out;
Tao Ma85db90e2008-11-12 08:27:01 +08003140 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08003141 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3142 clusters_add += new_clusters - old_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08003143 credits += ocfs2_calc_extend_credits(inode->i_sb,
Goldwyn Rodrigues06f9da62013-11-12 15:06:52 -08003144 &xv->xr_list);
Tao Ma97aff522008-11-19 16:48:41 +08003145 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3146 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08003147 }
3148 } else {
3149 /*
3150 * Now the new value will be stored inside. So if the new
3151 * value is smaller than the size of value root or the old
3152 * value, we don't need any allocation, otherwise we have
3153 * to guess metadata allocation.
3154 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003155 if ((ocfs2_xattr_is_local(xe) &&
3156 (value_size >= xi->xi_value_len)) ||
Tao Ma78f30c32008-11-12 08:27:00 +08003157 (!ocfs2_xattr_is_local(xe) &&
Joel Becker6b240ff2009-08-14 18:02:52 -07003158 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
Tao Ma78f30c32008-11-12 08:27:00 +08003159 goto out;
3160 }
3161
3162meta_guess:
3163 /* calculate metadata allocation. */
3164 if (di->i_xattr_loc) {
3165 if (!xbs->xattr_bh) {
Joel Becker4ae1d692008-11-13 14:49:18 -08003166 ret = ocfs2_read_xattr_block(inode,
3167 le64_to_cpu(di->i_xattr_loc),
3168 &bh);
Tao Ma78f30c32008-11-12 08:27:00 +08003169 if (ret) {
3170 mlog_errno(ret);
3171 goto out;
3172 }
3173
3174 xb = (struct ocfs2_xattr_block *)bh->b_data;
3175 } else
3176 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3177
Tao Ma90cb5462008-12-05 06:20:56 +08003178 /*
3179 * If there is already an xattr tree, good, we can calculate
3180 * like other b-trees. Otherwise we may have the chance of
3181 * create a tree, the credit calculation is borrowed from
3182 * ocfs2_calc_extend_credits with root_el = NULL. And the
3183 * new tree will be cluster based, so no meta is needed.
3184 */
Tao Ma78f30c32008-11-12 08:27:00 +08003185 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3186 struct ocfs2_extent_list *el =
3187 &xb->xb_attrs.xb_root.xt_list;
3188 meta_add += ocfs2_extend_meta_needed(el);
Tao Ma85db90e2008-11-12 08:27:01 +08003189 credits += ocfs2_calc_extend_credits(inode->i_sb,
Goldwyn Rodrigues06f9da62013-11-12 15:06:52 -08003190 el);
Tao Ma90cb5462008-12-05 06:20:56 +08003191 } else
3192 credits += OCFS2_SUBALLOC_ALLOC + 1;
Tao Ma78f30c32008-11-12 08:27:00 +08003193
3194 /*
3195 * This cluster will be used either for new bucket or for
3196 * new xattr block.
3197 * If the cluster size is the same as the bucket size, one
3198 * more is needed since we may need to extend the bucket
3199 * also.
3200 */
3201 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003202 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003203 if (OCFS2_XATTR_BUCKET_SIZE ==
Tao Ma85db90e2008-11-12 08:27:01 +08003204 OCFS2_SB(inode->i_sb)->s_clustersize) {
3205 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003206 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003207 }
3208 } else {
Tao Ma85db90e2008-11-12 08:27:01 +08003209 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
Tariq Saeed3ed2be72014-04-03 14:47:03 -07003210 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3211 struct ocfs2_extent_list *el = &def_xv.xv.xr_list;
3212 meta_add += ocfs2_extend_meta_needed(el);
3213 credits += ocfs2_calc_extend_credits(inode->i_sb,
3214 el);
3215 } else {
3216 meta_add += 1;
3217 }
Tao Ma85db90e2008-11-12 08:27:01 +08003218 }
Tao Ma78f30c32008-11-12 08:27:00 +08003219out:
3220 if (clusters_need)
3221 *clusters_need = clusters_add;
3222 if (meta_need)
3223 *meta_need = meta_add;
Tao Ma85db90e2008-11-12 08:27:01 +08003224 if (credits_need)
3225 *credits_need = credits;
Tao Ma78f30c32008-11-12 08:27:00 +08003226 brelse(bh);
3227 return ret;
3228}
3229
3230static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3231 struct ocfs2_dinode *di,
3232 struct ocfs2_xattr_info *xi,
3233 struct ocfs2_xattr_search *xis,
3234 struct ocfs2_xattr_search *xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08003235 struct ocfs2_xattr_set_ctxt *ctxt,
Tao Ma492a8a32009-08-18 11:43:17 +08003236 int extra_meta,
Tao Ma85db90e2008-11-12 08:27:01 +08003237 int *credits)
Tao Ma78f30c32008-11-12 08:27:00 +08003238{
3239 int clusters_add, meta_add, ret;
3240 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3241
3242 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3243
3244 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3245
3246 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08003247 &clusters_add, &meta_add, credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003248 if (ret) {
3249 mlog_errno(ret);
3250 return ret;
3251 }
3252
Tao Ma492a8a32009-08-18 11:43:17 +08003253 meta_add += extra_meta;
Tao Ma402b4182011-02-23 22:01:17 +08003254 trace_ocfs2_init_xattr_set_ctxt(xi->xi_name, meta_add,
3255 clusters_add, *credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003256
3257 if (meta_add) {
3258 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3259 &ctxt->meta_ac);
3260 if (ret) {
3261 mlog_errno(ret);
3262 goto out;
3263 }
3264 }
3265
3266 if (clusters_add) {
3267 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3268 if (ret)
3269 mlog_errno(ret);
3270 }
3271out:
3272 if (ret) {
3273 if (ctxt->meta_ac) {
3274 ocfs2_free_alloc_context(ctxt->meta_ac);
3275 ctxt->meta_ac = NULL;
3276 }
3277
3278 /*
3279 * We cannot have an error and a non null ctxt->data_ac.
3280 */
3281 }
3282
3283 return ret;
3284}
3285
Tao Ma85db90e2008-11-12 08:27:01 +08003286static int __ocfs2_xattr_set_handle(struct inode *inode,
3287 struct ocfs2_dinode *di,
3288 struct ocfs2_xattr_info *xi,
3289 struct ocfs2_xattr_search *xis,
3290 struct ocfs2_xattr_search *xbs,
3291 struct ocfs2_xattr_set_ctxt *ctxt)
3292{
Tao Ma9f868f12008-11-19 16:48:42 +08003293 int ret = 0, credits, old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003294
Joel Becker6b240ff2009-08-14 18:02:52 -07003295 if (!xi->xi_value) {
Tao Ma85db90e2008-11-12 08:27:01 +08003296 /* Remove existing extended attribute */
3297 if (!xis->not_found)
3298 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3299 else if (!xbs->not_found)
3300 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3301 } else {
3302 /* We always try to set extended attribute into inode first*/
3303 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3304 if (!ret && !xbs->not_found) {
3305 /*
3306 * If succeed and that extended attribute existing in
3307 * external block, then we will remove it.
3308 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003309 xi->xi_value = NULL;
3310 xi->xi_value_len = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08003311
Tao Ma9f868f12008-11-19 16:48:42 +08003312 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003313 xis->not_found = -ENODATA;
3314 ret = ocfs2_calc_xattr_set_need(inode,
3315 di,
3316 xi,
3317 xis,
3318 xbs,
3319 NULL,
3320 NULL,
3321 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08003322 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003323 if (ret) {
3324 mlog_errno(ret);
3325 goto out;
3326 }
3327
Tao Mac901fb02010-04-26 14:34:57 +08003328 ret = ocfs2_extend_trans(ctxt->handle, credits);
Tao Ma85db90e2008-11-12 08:27:01 +08003329 if (ret) {
3330 mlog_errno(ret);
3331 goto out;
3332 }
3333 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
Tao Ma5f5261a2010-05-13 22:49:05 +08003334 } else if ((ret == -ENOSPC) && !ctxt->set_abort) {
Tao Ma85db90e2008-11-12 08:27:01 +08003335 if (di->i_xattr_loc && !xbs->xattr_bh) {
3336 ret = ocfs2_xattr_block_find(inode,
Joel Becker6b240ff2009-08-14 18:02:52 -07003337 xi->xi_name_index,
3338 xi->xi_name, xbs);
Tao Ma85db90e2008-11-12 08:27:01 +08003339 if (ret)
3340 goto out;
3341
Tao Ma9f868f12008-11-19 16:48:42 +08003342 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003343 xis->not_found = -ENODATA;
3344 ret = ocfs2_calc_xattr_set_need(inode,
3345 di,
3346 xi,
3347 xis,
3348 xbs,
3349 NULL,
3350 NULL,
3351 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08003352 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003353 if (ret) {
3354 mlog_errno(ret);
3355 goto out;
3356 }
3357
Tao Mac901fb02010-04-26 14:34:57 +08003358 ret = ocfs2_extend_trans(ctxt->handle, credits);
Tao Ma85db90e2008-11-12 08:27:01 +08003359 if (ret) {
3360 mlog_errno(ret);
3361 goto out;
3362 }
3363 }
3364 /*
3365 * If no space in inode, we will set extended attribute
3366 * into external block.
3367 */
3368 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3369 if (ret)
3370 goto out;
3371 if (!xis->not_found) {
3372 /*
3373 * If succeed and that extended attribute
3374 * existing in inode, we will remove it.
3375 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003376 xi->xi_value = NULL;
3377 xi->xi_value_len = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08003378 xbs->not_found = -ENODATA;
3379 ret = ocfs2_calc_xattr_set_need(inode,
3380 di,
3381 xi,
3382 xis,
3383 xbs,
3384 NULL,
3385 NULL,
3386 &credits);
3387 if (ret) {
3388 mlog_errno(ret);
3389 goto out;
3390 }
3391
Tao Mac901fb02010-04-26 14:34:57 +08003392 ret = ocfs2_extend_trans(ctxt->handle, credits);
Tao Ma85db90e2008-11-12 08:27:01 +08003393 if (ret) {
3394 mlog_errno(ret);
3395 goto out;
3396 }
3397 ret = ocfs2_xattr_ibody_set(inode, xi,
3398 xis, ctxt);
3399 }
3400 }
3401 }
3402
Tao Ma4b3f6202008-12-05 06:20:55 +08003403 if (!ret) {
3404 /* Update inode ctime. */
Joel Becker0cf2f762009-02-12 16:41:25 -08003405 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
Tao Ma89a907a2009-02-17 04:39:28 +08003406 xis->inode_bh,
3407 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma4b3f6202008-12-05 06:20:55 +08003408 if (ret) {
3409 mlog_errno(ret);
3410 goto out;
3411 }
3412
3413 inode->i_ctime = CURRENT_TIME;
3414 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3415 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3416 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3417 }
Tao Ma85db90e2008-11-12 08:27:01 +08003418out:
3419 return ret;
3420}
3421
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003422/*
Tiger Yang6c3faba2008-11-14 11:16:03 +08003423 * This function only called duing creating inode
3424 * for init security/acl xattrs of the new inode.
Tiger Yang008aafa2008-12-09 16:43:08 +08003425 * All transanction credits have been reserved in mknod.
Tiger Yang6c3faba2008-11-14 11:16:03 +08003426 */
3427int ocfs2_xattr_set_handle(handle_t *handle,
3428 struct inode *inode,
3429 struct buffer_head *di_bh,
3430 int name_index,
3431 const char *name,
3432 const void *value,
3433 size_t value_len,
3434 int flags,
3435 struct ocfs2_alloc_context *meta_ac,
3436 struct ocfs2_alloc_context *data_ac)
3437{
3438 struct ocfs2_dinode *di;
3439 int ret;
3440
3441 struct ocfs2_xattr_info xi = {
Joel Becker6b240ff2009-08-14 18:02:52 -07003442 .xi_name_index = name_index,
3443 .xi_name = name,
Joel Becker18853b92009-08-14 18:17:07 -07003444 .xi_name_len = strlen(name),
Joel Becker6b240ff2009-08-14 18:02:52 -07003445 .xi_value = value,
3446 .xi_value_len = value_len,
Tiger Yang6c3faba2008-11-14 11:16:03 +08003447 };
3448
3449 struct ocfs2_xattr_search xis = {
3450 .not_found = -ENODATA,
3451 };
3452
3453 struct ocfs2_xattr_search xbs = {
3454 .not_found = -ENODATA,
3455 };
3456
3457 struct ocfs2_xattr_set_ctxt ctxt = {
3458 .handle = handle,
3459 .meta_ac = meta_ac,
3460 .data_ac = data_ac,
3461 };
3462
3463 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3464 return -EOPNOTSUPP;
3465
Tiger Yang008aafa2008-12-09 16:43:08 +08003466 /*
3467 * In extreme situation, may need xattr bucket when
3468 * block size is too small. And we have already reserved
3469 * the credits for bucket in mknod.
3470 */
3471 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3472 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3473 if (!xbs.bucket) {
3474 mlog_errno(-ENOMEM);
3475 return -ENOMEM;
3476 }
3477 }
3478
Tiger Yang6c3faba2008-11-14 11:16:03 +08003479 xis.inode_bh = xbs.inode_bh = di_bh;
3480 di = (struct ocfs2_dinode *)di_bh->b_data;
3481
3482 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3483
3484 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3485 if (ret)
3486 goto cleanup;
3487 if (xis.not_found) {
3488 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3489 if (ret)
3490 goto cleanup;
3491 }
3492
3493 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3494
3495cleanup:
3496 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3497 brelse(xbs.xattr_bh);
Tiger Yang008aafa2008-12-09 16:43:08 +08003498 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yang6c3faba2008-11-14 11:16:03 +08003499
3500 return ret;
3501}
3502
3503/*
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003504 * ocfs2_xattr_set()
3505 *
3506 * Set, replace or remove an extended attribute for this inode.
3507 * value is NULL to remove an existing extended attribute, else either
3508 * create or replace an extended attribute.
3509 */
3510int ocfs2_xattr_set(struct inode *inode,
3511 int name_index,
3512 const char *name,
3513 const void *value,
3514 size_t value_len,
3515 int flags)
3516{
3517 struct buffer_head *di_bh = NULL;
3518 struct ocfs2_dinode *di;
Tao Ma492a8a32009-08-18 11:43:17 +08003519 int ret, credits, ref_meta = 0, ref_credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08003520 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08003521 struct inode *tl_inode = osb->osb_tl_inode;
Younger Liu6ea437a2013-09-11 14:19:56 -07003522 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, NULL, };
Tao Ma492a8a32009-08-18 11:43:17 +08003523 struct ocfs2_refcount_tree *ref_tree = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003524
3525 struct ocfs2_xattr_info xi = {
Joel Becker6b240ff2009-08-14 18:02:52 -07003526 .xi_name_index = name_index,
3527 .xi_name = name,
Joel Becker18853b92009-08-14 18:17:07 -07003528 .xi_name_len = strlen(name),
Joel Becker6b240ff2009-08-14 18:02:52 -07003529 .xi_value = value,
3530 .xi_value_len = value_len,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003531 };
3532
3533 struct ocfs2_xattr_search xis = {
3534 .not_found = -ENODATA,
3535 };
3536
3537 struct ocfs2_xattr_search xbs = {
3538 .not_found = -ENODATA,
3539 };
3540
Tiger Yang8154da32008-08-18 17:11:46 +08003541 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3542 return -EOPNOTSUPP;
3543
Joel Beckerba937122008-10-24 19:13:20 -07003544 /*
3545 * Only xbs will be used on indexed trees. xis doesn't need a
3546 * bucket.
3547 */
3548 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3549 if (!xbs.bucket) {
3550 mlog_errno(-ENOMEM);
3551 return -ENOMEM;
3552 }
3553
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003554 ret = ocfs2_inode_lock(inode, &di_bh, 1);
3555 if (ret < 0) {
3556 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003557 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003558 }
3559 xis.inode_bh = xbs.inode_bh = di_bh;
3560 di = (struct ocfs2_dinode *)di_bh->b_data;
3561
3562 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3563 /*
3564 * Scan inode and external block to find the same name
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003565 * extended attribute and collect search information.
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003566 */
3567 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3568 if (ret)
3569 goto cleanup;
3570 if (xis.not_found) {
3571 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3572 if (ret)
3573 goto cleanup;
3574 }
3575
3576 if (xis.not_found && xbs.not_found) {
3577 ret = -ENODATA;
3578 if (flags & XATTR_REPLACE)
3579 goto cleanup;
3580 ret = 0;
3581 if (!value)
3582 goto cleanup;
3583 } else {
3584 ret = -EEXIST;
3585 if (flags & XATTR_CREATE)
3586 goto cleanup;
3587 }
3588
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003589 /* Check whether the value is refcounted and do some preparation. */
Tao Ma492a8a32009-08-18 11:43:17 +08003590 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3591 (!xis.not_found || !xbs.not_found)) {
3592 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3593 &xis, &xbs, &ref_tree,
3594 &ref_meta, &ref_credits);
3595 if (ret) {
3596 mlog_errno(ret);
3597 goto cleanup;
3598 }
3599 }
Tao Ma85db90e2008-11-12 08:27:01 +08003600
3601 mutex_lock(&tl_inode->i_mutex);
3602
3603 if (ocfs2_truncate_log_needs_flush(osb)) {
3604 ret = __ocfs2_flush_truncate_log(osb);
3605 if (ret < 0) {
3606 mutex_unlock(&tl_inode->i_mutex);
3607 mlog_errno(ret);
3608 goto cleanup;
3609 }
3610 }
3611 mutex_unlock(&tl_inode->i_mutex);
3612
3613 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
Tao Ma492a8a32009-08-18 11:43:17 +08003614 &xbs, &ctxt, ref_meta, &credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003615 if (ret) {
3616 mlog_errno(ret);
3617 goto cleanup;
3618 }
3619
Tao Ma4b3f6202008-12-05 06:20:55 +08003620 /* we need to update inode's ctime field, so add credit for it. */
3621 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma492a8a32009-08-18 11:43:17 +08003622 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma85db90e2008-11-12 08:27:01 +08003623 if (IS_ERR(ctxt.handle)) {
3624 ret = PTR_ERR(ctxt.handle);
3625 mlog_errno(ret);
Younger Liu6ea437a2013-09-11 14:19:56 -07003626 goto out_free_ac;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003627 }
Tao Ma85db90e2008-11-12 08:27:01 +08003628
3629 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
Darrick J. Wong6fdb7022014-04-03 14:47:08 -07003630 ocfs2_update_inode_fsync_trans(ctxt.handle, inode, 0);
Tao Ma85db90e2008-11-12 08:27:01 +08003631
3632 ocfs2_commit_trans(osb, ctxt.handle);
3633
Younger Liu6ea437a2013-09-11 14:19:56 -07003634out_free_ac:
Tao Ma78f30c32008-11-12 08:27:00 +08003635 if (ctxt.data_ac)
3636 ocfs2_free_alloc_context(ctxt.data_ac);
3637 if (ctxt.meta_ac)
3638 ocfs2_free_alloc_context(ctxt.meta_ac);
3639 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3640 ocfs2_schedule_truncate_log_flush(osb, 1);
3641 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma8b2c0db2009-08-18 11:43:49 +08003642
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003643cleanup:
Tao Ma492a8a32009-08-18 11:43:17 +08003644 if (ref_tree)
3645 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003646 up_write(&OCFS2_I(inode)->ip_xattr_sem);
Tao Ma8b2c0db2009-08-18 11:43:49 +08003647 if (!value && !ret) {
3648 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3649 if (ret)
3650 mlog_errno(ret);
3651 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003652 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07003653cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003654 brelse(di_bh);
3655 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07003656 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003657
3658 return ret;
3659}
3660
Tao Ma0c044f02008-08-18 17:38:50 +08003661/*
3662 * Find the xattr extent rec which may contains name_hash.
3663 * e_cpos will be the first name hash of the xattr rec.
3664 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3665 */
3666static int ocfs2_xattr_get_rec(struct inode *inode,
3667 u32 name_hash,
3668 u64 *p_blkno,
3669 u32 *e_cpos,
3670 u32 *num_clusters,
3671 struct ocfs2_extent_list *el)
3672{
3673 int ret = 0, i;
3674 struct buffer_head *eb_bh = NULL;
3675 struct ocfs2_extent_block *eb;
3676 struct ocfs2_extent_rec *rec = NULL;
3677 u64 e_blkno = 0;
3678
3679 if (el->l_tree_depth) {
Joel Beckerfacdb772009-02-12 18:08:48 -08003680 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3681 &eb_bh);
Tao Ma0c044f02008-08-18 17:38:50 +08003682 if (ret) {
3683 mlog_errno(ret);
3684 goto out;
3685 }
3686
3687 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3688 el = &eb->h_list;
3689
3690 if (el->l_tree_depth) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -07003691 ret = ocfs2_error(inode->i_sb,
Joe Perches7ecef142015-09-04 15:44:51 -07003692 "Inode %lu has non zero tree depth in xattr tree block %llu\n",
3693 inode->i_ino,
3694 (unsigned long long)eb_bh->b_blocknr);
Tao Ma0c044f02008-08-18 17:38:50 +08003695 goto out;
3696 }
3697 }
3698
3699 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3700 rec = &el->l_recs[i];
3701
3702 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3703 e_blkno = le64_to_cpu(rec->e_blkno);
3704 break;
3705 }
3706 }
3707
3708 if (!e_blkno) {
Joe Perches7ecef142015-09-04 15:44:51 -07003709 ret = ocfs2_error(inode->i_sb, "Inode %lu has bad extent record (%u, %u, 0) in xattr\n",
3710 inode->i_ino,
3711 le32_to_cpu(rec->e_cpos),
3712 ocfs2_rec_clusters(el, rec));
Tao Ma0c044f02008-08-18 17:38:50 +08003713 goto out;
3714 }
3715
3716 *p_blkno = le64_to_cpu(rec->e_blkno);
3717 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3718 if (e_cpos)
3719 *e_cpos = le32_to_cpu(rec->e_cpos);
3720out:
3721 brelse(eb_bh);
3722 return ret;
3723}
3724
3725typedef int (xattr_bucket_func)(struct inode *inode,
3726 struct ocfs2_xattr_bucket *bucket,
3727 void *para);
3728
Tao Ma589dc262008-08-18 17:38:51 +08003729static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07003730 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08003731 int name_index,
3732 const char *name,
3733 u32 name_hash,
3734 u16 *xe_index,
3735 int *found)
3736{
3737 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07003738 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08003739 size_t name_len = strlen(name);
3740 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08003741 char *xe_name;
3742
3743 /*
3744 * We don't use binary search in the bucket because there
3745 * may be multiple entries with the same name hash.
3746 */
3747 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3748 xe = &xh->xh_entries[i];
3749
3750 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3751 continue;
3752 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3753 break;
3754
3755 cmp = name_index - ocfs2_xattr_get_type(xe);
3756 if (!cmp)
3757 cmp = name_len - xe->xe_name_len;
3758 if (cmp)
3759 continue;
3760
Tao Mafd68a892009-08-18 11:43:21 +08003761 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma589dc262008-08-18 17:38:51 +08003762 xh,
3763 i,
3764 &block_off,
3765 &new_offset);
3766 if (ret) {
3767 mlog_errno(ret);
3768 break;
3769 }
3770
Joel Becker970e4932008-11-13 14:49:19 -08003771
Joel Beckere2356a32008-10-27 15:01:54 -07003772 xe_name = bucket_block(bucket, block_off) + new_offset;
3773 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08003774 *xe_index = i;
3775 *found = 1;
3776 ret = 0;
3777 break;
3778 }
3779 }
3780
3781 return ret;
3782}
3783
3784/*
3785 * Find the specified xattr entry in a series of buckets.
3786 * This series start from p_blkno and last for num_clusters.
3787 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3788 * the num of the valid buckets.
3789 *
3790 * Return the buffer_head this xattr should reside in. And if the xattr's
3791 * hash is in the gap of 2 buckets, return the lower bucket.
3792 */
3793static int ocfs2_xattr_bucket_find(struct inode *inode,
3794 int name_index,
3795 const char *name,
3796 u32 name_hash,
3797 u64 p_blkno,
3798 u32 first_hash,
3799 u32 num_clusters,
3800 struct ocfs2_xattr_search *xs)
3801{
3802 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003803 struct ocfs2_xattr_header *xh = NULL;
3804 struct ocfs2_xattr_entry *xe = NULL;
3805 u16 index = 0;
3806 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3807 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003808 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08003809 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07003810 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003811
Joel Beckere2356a32008-10-27 15:01:54 -07003812 search = ocfs2_xattr_bucket_new(inode);
3813 if (!search) {
3814 ret = -ENOMEM;
3815 mlog_errno(ret);
3816 goto out;
3817 }
3818
3819 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003820 if (ret) {
3821 mlog_errno(ret);
3822 goto out;
3823 }
3824
Joel Beckere2356a32008-10-27 15:01:54 -07003825 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003826 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08003827 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07003828 ocfs2_xattr_bucket_relse(search);
3829
Tao Ma589dc262008-08-18 17:38:51 +08003830 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08003831 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003832 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003833 if (ret) {
3834 mlog_errno(ret);
3835 goto out;
3836 }
3837
Joel Beckere2356a32008-10-27 15:01:54 -07003838 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003839 xe = &xh->xh_entries[0];
3840 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3841 high_bucket = bucket - 1;
3842 continue;
3843 }
3844
3845 /*
3846 * Check whether the hash of the last entry in our
Tao Ma5a095612008-09-19 22:17:41 +08003847 * bucket is larger than the search one. for an empty
3848 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08003849 */
Tao Ma5a095612008-09-19 22:17:41 +08003850 if (xh->xh_count)
3851 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3852
Tao Ma589dc262008-08-18 17:38:51 +08003853 last_hash = le32_to_cpu(xe->xe_name_hash);
3854
Joel Beckere2356a32008-10-27 15:01:54 -07003855 /* record lower_blkno which may be the insert place. */
3856 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08003857
3858 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3859 low_bucket = bucket + 1;
3860 continue;
3861 }
3862
3863 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07003864 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08003865 name_index, name, name_hash,
3866 &index, &found);
3867 if (ret) {
3868 mlog_errno(ret);
3869 goto out;
3870 }
3871 break;
3872 }
3873
3874 /*
3875 * Record the bucket we have found.
3876 * When the xattr's hash value is in the gap of 2 buckets, we will
3877 * always set it to the previous bucket.
3878 */
Joel Beckere2356a32008-10-27 15:01:54 -07003879 if (!lower_blkno)
3880 lower_blkno = p_blkno;
3881
3882 /* This should be in cache - we just read it during the search */
3883 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3884 if (ret) {
3885 mlog_errno(ret);
3886 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08003887 }
Tao Ma589dc262008-08-18 17:38:51 +08003888
Joel Beckerba937122008-10-24 19:13:20 -07003889 xs->header = bucket_xh(xs->bucket);
3890 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08003891 xs->end = xs->base + inode->i_sb->s_blocksize;
3892
3893 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08003894 xs->here = &xs->header->xh_entries[index];
Tao Ma402b4182011-02-23 22:01:17 +08003895 trace_ocfs2_xattr_bucket_find(OCFS2_I(inode)->ip_blkno,
3896 name, name_index, name_hash,
3897 (unsigned long long)bucket_blkno(xs->bucket),
3898 index);
Tao Ma589dc262008-08-18 17:38:51 +08003899 } else
3900 ret = -ENODATA;
3901
3902out:
Joel Beckere2356a32008-10-27 15:01:54 -07003903 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08003904 return ret;
3905}
3906
3907static int ocfs2_xattr_index_block_find(struct inode *inode,
3908 struct buffer_head *root_bh,
3909 int name_index,
3910 const char *name,
3911 struct ocfs2_xattr_search *xs)
3912{
3913 int ret;
3914 struct ocfs2_xattr_block *xb =
3915 (struct ocfs2_xattr_block *)root_bh->b_data;
3916 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3917 struct ocfs2_extent_list *el = &xb_root->xt_list;
3918 u64 p_blkno = 0;
3919 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08003920 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08003921
3922 if (le16_to_cpu(el->l_next_free_rec) == 0)
3923 return -ENODATA;
3924
Tao Ma402b4182011-02-23 22:01:17 +08003925 trace_ocfs2_xattr_index_block_find(OCFS2_I(inode)->ip_blkno,
3926 name, name_index, name_hash,
3927 (unsigned long long)root_bh->b_blocknr,
3928 -1);
Tao Ma589dc262008-08-18 17:38:51 +08003929
3930 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3931 &num_clusters, el);
3932 if (ret) {
3933 mlog_errno(ret);
3934 goto out;
3935 }
3936
3937 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3938
Tao Ma402b4182011-02-23 22:01:17 +08003939 trace_ocfs2_xattr_index_block_find_rec(OCFS2_I(inode)->ip_blkno,
3940 name, name_index, first_hash,
3941 (unsigned long long)p_blkno,
3942 num_clusters);
Tao Ma589dc262008-08-18 17:38:51 +08003943
3944 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3945 p_blkno, first_hash, num_clusters, xs);
3946
3947out:
3948 return ret;
3949}
3950
Tao Ma0c044f02008-08-18 17:38:50 +08003951static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3952 u64 blkno,
3953 u32 clusters,
3954 xattr_bucket_func *func,
3955 void *para)
3956{
Joel Becker6dde41d2008-10-24 17:16:48 -07003957 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003958 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3959 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07003960 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08003961
Joel Beckerba937122008-10-24 19:13:20 -07003962 bucket = ocfs2_xattr_bucket_new(inode);
3963 if (!bucket) {
3964 mlog_errno(-ENOMEM);
3965 return -ENOMEM;
3966 }
Tao Ma0c044f02008-08-18 17:38:50 +08003967
Tao Ma402b4182011-02-23 22:01:17 +08003968 trace_ocfs2_iterate_xattr_buckets(
3969 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3970 (unsigned long long)blkno, clusters);
Tao Ma0c044f02008-08-18 17:38:50 +08003971
Joel Beckerba937122008-10-24 19:13:20 -07003972 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3973 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003974 if (ret) {
3975 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003976 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003977 }
3978
Tao Ma0c044f02008-08-18 17:38:50 +08003979 /*
3980 * The real bucket num in this series of blocks is stored
3981 * in the 1st bucket.
3982 */
3983 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07003984 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08003985
Tao Ma402b4182011-02-23 22:01:17 +08003986 trace_ocfs2_iterate_xattr_bucket((unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07003987 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08003988 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07003989 ret = func(inode, bucket, para);
Tao Maa46fa682009-05-04 05:18:09 +08003990 if (ret && ret != -ERANGE)
Tao Ma0c044f02008-08-18 17:38:50 +08003991 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003992 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08003993 }
3994
Joel Beckerba937122008-10-24 19:13:20 -07003995 ocfs2_xattr_bucket_relse(bucket);
3996 if (ret)
3997 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003998 }
3999
Joel Beckerba937122008-10-24 19:13:20 -07004000 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08004001 return ret;
4002}
4003
4004struct ocfs2_xattr_tree_list {
4005 char *buffer;
4006 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08004007 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08004008};
4009
Tao Mafd68a892009-08-18 11:43:21 +08004010static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
Tao Ma0c044f02008-08-18 17:38:50 +08004011 struct ocfs2_xattr_header *xh,
4012 int index,
4013 int *block_off,
4014 int *new_offset)
4015{
4016 u16 name_offset;
4017
4018 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
4019 return -EINVAL;
4020
4021 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
4022
Tao Mafd68a892009-08-18 11:43:21 +08004023 *block_off = name_offset >> sb->s_blocksize_bits;
4024 *new_offset = name_offset % sb->s_blocksize;
Tao Ma0c044f02008-08-18 17:38:50 +08004025
4026 return 0;
4027}
4028
4029static int ocfs2_list_xattr_bucket(struct inode *inode,
4030 struct ocfs2_xattr_bucket *bucket,
4031 void *para)
4032{
Tao Ma936b8832008-10-09 23:06:14 +08004033 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08004034 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08004035 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08004036 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08004037
Joel Becker3e632942008-10-24 17:04:49 -07004038 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
4039 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08004040 type = ocfs2_xattr_get_type(entry);
4041 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08004042
Tao Ma936b8832008-10-09 23:06:14 +08004043 if (prefix) {
Tao Mafd68a892009-08-18 11:43:21 +08004044 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Becker3e632942008-10-24 17:04:49 -07004045 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08004046 i,
4047 &block_off,
4048 &new_offset);
4049 if (ret)
4050 break;
Tao Ma936b8832008-10-09 23:06:14 +08004051
Joel Becker51def392008-10-24 16:57:21 -07004052 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08004053 new_offset;
4054 ret = ocfs2_xattr_list_entry(xl->buffer,
4055 xl->buffer_size,
4056 &xl->result,
4057 prefix, name,
4058 entry->xe_name_len);
4059 if (ret)
4060 break;
Tao Ma0c044f02008-08-18 17:38:50 +08004061 }
4062 }
4063
4064 return ret;
4065}
4066
Tao Ma47bca492009-08-18 11:43:42 +08004067static int ocfs2_iterate_xattr_index_block(struct inode *inode,
4068 struct buffer_head *blk_bh,
4069 xattr_tree_rec_func *rec_func,
4070 void *para)
Tao Ma0c044f02008-08-18 17:38:50 +08004071{
Tao Ma47bca492009-08-18 11:43:42 +08004072 struct ocfs2_xattr_block *xb =
4073 (struct ocfs2_xattr_block *)blk_bh->b_data;
4074 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
Tao Ma0c044f02008-08-18 17:38:50 +08004075 int ret = 0;
4076 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
4077 u64 p_blkno = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08004078
Tao Ma47bca492009-08-18 11:43:42 +08004079 if (!el->l_next_free_rec || !rec_func)
Tao Ma0c044f02008-08-18 17:38:50 +08004080 return 0;
4081
4082 while (name_hash > 0) {
4083 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4084 &e_cpos, &num_clusters, el);
4085 if (ret) {
4086 mlog_errno(ret);
Tao Ma47bca492009-08-18 11:43:42 +08004087 break;
Tao Ma0c044f02008-08-18 17:38:50 +08004088 }
4089
Tao Ma47bca492009-08-18 11:43:42 +08004090 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
4091 num_clusters, para);
Tao Ma0c044f02008-08-18 17:38:50 +08004092 if (ret) {
Tao Maa46fa682009-05-04 05:18:09 +08004093 if (ret != -ERANGE)
4094 mlog_errno(ret);
Tao Ma47bca492009-08-18 11:43:42 +08004095 break;
Tao Ma0c044f02008-08-18 17:38:50 +08004096 }
4097
4098 if (e_cpos == 0)
4099 break;
4100
4101 name_hash = e_cpos - 1;
4102 }
4103
Tao Ma47bca492009-08-18 11:43:42 +08004104 return ret;
4105
4106}
4107
4108static int ocfs2_list_xattr_tree_rec(struct inode *inode,
4109 struct buffer_head *root_bh,
4110 u64 blkno, u32 cpos, u32 len, void *para)
4111{
4112 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
4113 ocfs2_list_xattr_bucket, para);
4114}
4115
4116static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4117 struct buffer_head *blk_bh,
4118 char *buffer,
4119 size_t buffer_size)
4120{
4121 int ret;
4122 struct ocfs2_xattr_tree_list xl = {
4123 .buffer = buffer,
4124 .buffer_size = buffer_size,
4125 .result = 0,
4126 };
4127
4128 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4129 ocfs2_list_xattr_tree_rec, &xl);
4130 if (ret) {
4131 mlog_errno(ret);
4132 goto out;
4133 }
4134
Tao Ma936b8832008-10-09 23:06:14 +08004135 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08004136out:
4137 return ret;
4138}
Tao Ma01225592008-08-18 17:38:53 +08004139
4140static int cmp_xe(const void *a, const void *b)
4141{
4142 const struct ocfs2_xattr_entry *l = a, *r = b;
4143 u32 l_hash = le32_to_cpu(l->xe_name_hash);
4144 u32 r_hash = le32_to_cpu(r->xe_name_hash);
4145
4146 if (l_hash > r_hash)
4147 return 1;
4148 if (l_hash < r_hash)
4149 return -1;
4150 return 0;
4151}
4152
4153static void swap_xe(void *a, void *b, int size)
4154{
4155 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4156
4157 tmp = *l;
4158 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4159 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4160}
4161
4162/*
4163 * When the ocfs2_xattr_block is filled up, new bucket will be created
4164 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07004165 * The header goes at the start of the bucket, and the names+values are
4166 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08004167 * Note: we need to sort the entries since they are not saved in order
4168 * in the ocfs2_xattr_block.
4169 */
4170static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4171 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07004172 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08004173{
4174 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07004175 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004176 u16 offset, size, off_change;
4177 struct ocfs2_xattr_entry *xe;
4178 struct ocfs2_xattr_block *xb =
4179 (struct ocfs2_xattr_block *)xb_bh->b_data;
4180 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07004181 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004182 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07004183 char *src = xb_bh->b_data;
4184 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08004185
Tao Ma402b4182011-02-23 22:01:17 +08004186 trace_ocfs2_cp_xattr_block_to_bucket_begin(
4187 (unsigned long long)xb_bh->b_blocknr,
4188 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08004189
Joel Becker178eeac2008-10-27 15:18:29 -07004190 for (i = 0; i < blks; i++)
4191 memset(bucket_block(bucket, i), 0, blocksize);
4192
Tao Ma01225592008-08-18 17:38:53 +08004193 /*
4194 * Since the xe_name_offset is based on ocfs2_xattr_header,
4195 * there is a offset change corresponding to the change of
4196 * ocfs2_xattr_header's position.
4197 */
4198 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4199 xe = &xb_xh->xh_entries[count - 1];
4200 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4201 size = blocksize - offset;
4202
4203 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08004204 memcpy(target + offset, src + offset, size);
4205
4206 /* Init new header now. */
4207 xh->xh_count = xb_xh->xh_count;
4208 xh->xh_num_buckets = cpu_to_le16(1);
4209 xh->xh_name_value_len = cpu_to_le16(size);
4210 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4211
4212 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07004213 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08004214 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4215 size = count * sizeof(struct ocfs2_xattr_entry);
4216 memcpy(target + offset, (char *)xb_xh + offset, size);
4217
4218 /* Change the xe offset for all the xe because of the move. */
4219 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4220 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4221 for (i = 0; i < count; i++)
4222 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4223
Tao Ma402b4182011-02-23 22:01:17 +08004224 trace_ocfs2_cp_xattr_block_to_bucket_end(offset, size, off_change);
Tao Ma01225592008-08-18 17:38:53 +08004225
4226 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4227 cmp_xe, swap_xe);
4228}
4229
4230/*
4231 * After we move xattr from block to index btree, we have to
4232 * update ocfs2_xattr_search to the new xe and base.
4233 *
4234 * When the entry is in xattr block, xattr_bh indicates the storage place.
4235 * While if the entry is in index b-tree, "bucket" indicates the
4236 * real place of the xattr.
4237 */
Joel Becker178eeac2008-10-27 15:18:29 -07004238static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4239 struct ocfs2_xattr_search *xs,
4240 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08004241{
Tao Ma01225592008-08-18 17:38:53 +08004242 char *buf = old_bh->b_data;
4243 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4244 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07004245 int i;
Tao Ma01225592008-08-18 17:38:53 +08004246
Joel Beckerba937122008-10-24 19:13:20 -07004247 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07004248 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08004249 xs->end = xs->base + inode->i_sb->s_blocksize;
4250
Joel Becker178eeac2008-10-27 15:18:29 -07004251 if (xs->not_found)
4252 return;
Tao Ma01225592008-08-18 17:38:53 +08004253
Joel Becker178eeac2008-10-27 15:18:29 -07004254 i = xs->here - old_xh->xh_entries;
4255 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08004256}
4257
4258static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08004259 struct ocfs2_xattr_search *xs,
4260 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004261{
Tao Ma85db90e2008-11-12 08:27:01 +08004262 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004263 u32 bit_off, len;
4264 u64 blkno;
Tao Ma85db90e2008-11-12 08:27:01 +08004265 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004266 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Tao Ma01225592008-08-18 17:38:53 +08004267 struct buffer_head *xb_bh = xs->xattr_bh;
4268 struct ocfs2_xattr_block *xb =
4269 (struct ocfs2_xattr_block *)xb_bh->b_data;
4270 struct ocfs2_xattr_tree_root *xr;
4271 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08004272
Tao Ma402b4182011-02-23 22:01:17 +08004273 trace_ocfs2_xattr_create_index_block_begin(
4274 (unsigned long long)xb_bh->b_blocknr);
Tao Ma01225592008-08-18 17:38:53 +08004275
4276 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07004277 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004278
Tao Ma01225592008-08-18 17:38:53 +08004279 /*
4280 * XXX:
4281 * We can use this lock for now, and maybe move to a dedicated mutex
4282 * if performance becomes a problem later.
4283 */
4284 down_write(&oi->ip_alloc_sem);
4285
Joel Becker0cf2f762009-02-12 16:41:25 -08004286 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
Joel Becker84008972008-12-09 16:11:49 -08004287 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004288 if (ret) {
4289 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004290 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004291 }
4292
Joel Becker1ed9b772010-05-06 13:59:06 +08004293 ret = __ocfs2_claim_clusters(handle, ctxt->data_ac,
Tao Ma78f30c32008-11-12 08:27:00 +08004294 1, 1, &bit_off, &len);
Tao Ma01225592008-08-18 17:38:53 +08004295 if (ret) {
4296 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004297 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004298 }
4299
4300 /*
4301 * The bucket may spread in many blocks, and
4302 * we will only touch the 1st block and the last block
4303 * in the whole bucket(one for entry and one for data).
4304 */
4305 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4306
Tao Ma402b4182011-02-23 22:01:17 +08004307 trace_ocfs2_xattr_create_index_block((unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08004308
Wengang Wang9c339252014-04-03 14:47:15 -07004309 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno, 1);
Tao Ma01225592008-08-18 17:38:53 +08004310 if (ret) {
4311 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004312 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004313 }
4314
Joel Becker178eeac2008-10-27 15:18:29 -07004315 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4316 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07004317 if (ret) {
4318 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004319 goto out;
Joel Beckerbd60bd32008-10-20 18:25:56 -07004320 }
Tao Ma01225592008-08-18 17:38:53 +08004321
Joel Becker178eeac2008-10-27 15:18:29 -07004322 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4323 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4324
4325 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4326
Tao Ma01225592008-08-18 17:38:53 +08004327 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4328 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4329 offsetof(struct ocfs2_xattr_block, xb_attrs));
4330
4331 xr = &xb->xb_attrs.xb_root;
4332 xr->xt_clusters = cpu_to_le32(1);
4333 xr->xt_last_eb_blk = 0;
4334 xr->xt_list.l_tree_depth = 0;
4335 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4336 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4337
4338 xr->xt_list.l_recs[0].e_cpos = 0;
4339 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4340 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4341
4342 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4343
Tao Ma85db90e2008-11-12 08:27:01 +08004344 ocfs2_journal_dirty(handle, xb_bh);
Tao Ma01225592008-08-18 17:38:53 +08004345
Tao Ma85db90e2008-11-12 08:27:01 +08004346out:
Tao Ma01225592008-08-18 17:38:53 +08004347 up_write(&oi->ip_alloc_sem);
4348
Tao Ma01225592008-08-18 17:38:53 +08004349 return ret;
4350}
4351
4352static int cmp_xe_offset(const void *a, const void *b)
4353{
4354 const struct ocfs2_xattr_entry *l = a, *r = b;
4355 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4356 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4357
4358 if (l_name_offset < r_name_offset)
4359 return 1;
4360 if (l_name_offset > r_name_offset)
4361 return -1;
4362 return 0;
4363}
4364
4365/*
4366 * defrag a xattr bucket if we find that the bucket has some
4367 * holes beteen name/value pairs.
4368 * We will move all the name/value pairs to the end of the bucket
4369 * so that we can spare some space for insertion.
4370 */
4371static int ocfs2_defrag_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004372 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004373 struct ocfs2_xattr_bucket *bucket)
4374{
4375 int ret, i;
Joel Becker199799a2009-08-14 19:04:15 -07004376 size_t end, offset, len;
Tao Ma01225592008-08-18 17:38:53 +08004377 struct ocfs2_xattr_header *xh;
4378 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07004379 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004380 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08004381 size_t blocksize = inode->i_sb->s_blocksize;
Tao Ma01225592008-08-18 17:38:53 +08004382 struct ocfs2_xattr_entry *xe;
Tao Ma01225592008-08-18 17:38:53 +08004383
4384 /*
4385 * In order to make the operation more efficient and generic,
4386 * we copy all the blocks into a contiguous memory and do the
4387 * defragment there, so if anything is error, we will not touch
4388 * the real block.
4389 */
4390 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4391 if (!bucket_buf) {
4392 ret = -EIO;
4393 goto out;
4394 }
4395
Joel Becker161d6f32008-10-27 15:25:18 -07004396 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08004397 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4398 memcpy(buf, bucket_block(bucket, i), blocksize);
Joel Becker161d6f32008-10-27 15:25:18 -07004399
Tao Ma1c32a2f2008-11-06 08:10:47 +08004400 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
Joel Becker161d6f32008-10-27 15:25:18 -07004401 OCFS2_JOURNAL_ACCESS_WRITE);
4402 if (ret < 0) {
4403 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004404 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004405 }
4406
4407 xh = (struct ocfs2_xattr_header *)bucket_buf;
4408 entries = (char *)xh->xh_entries;
4409 xh_free_start = le16_to_cpu(xh->xh_free_start);
4410
Tao Ma402b4182011-02-23 22:01:17 +08004411 trace_ocfs2_defrag_xattr_bucket(
Mark Fashehde29c082008-10-29 14:45:30 -07004412 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4413 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08004414
4415 /*
4416 * sort all the entries by their offset.
4417 * the largest will be the first, so that we can
4418 * move them to the end one by one.
4419 */
4420 sort(entries, le16_to_cpu(xh->xh_count),
4421 sizeof(struct ocfs2_xattr_entry),
4422 cmp_xe_offset, swap_xe);
4423
4424 /* Move all name/values to the end of the bucket. */
4425 xe = xh->xh_entries;
4426 end = OCFS2_XATTR_BUCKET_SIZE;
4427 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4428 offset = le16_to_cpu(xe->xe_name_offset);
Joel Becker199799a2009-08-14 19:04:15 -07004429 len = namevalue_size_xe(xe);
Tao Ma01225592008-08-18 17:38:53 +08004430
4431 /*
4432 * We must make sure that the name/value pair
4433 * exist in the same block. So adjust end to
4434 * the previous block end if needed.
4435 */
4436 if (((end - len) / blocksize !=
4437 (end - 1) / blocksize))
4438 end = end - end % blocksize;
4439
4440 if (end > offset + len) {
4441 memmove(bucket_buf + end - len,
4442 bucket_buf + offset, len);
4443 xe->xe_name_offset = cpu_to_le16(end - len);
4444 }
4445
4446 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4447 "bucket %llu\n", (unsigned long long)blkno);
4448
4449 end -= len;
4450 }
4451
4452 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4453 "bucket %llu\n", (unsigned long long)blkno);
4454
4455 if (xh_free_start == end)
Tao Ma85db90e2008-11-12 08:27:01 +08004456 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004457
4458 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4459 xh->xh_free_start = cpu_to_le16(end);
4460
4461 /* sort the entries by their name_hash. */
4462 sort(entries, le16_to_cpu(xh->xh_count),
4463 sizeof(struct ocfs2_xattr_entry),
4464 cmp_xe, swap_xe);
4465
4466 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08004467 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4468 memcpy(bucket_block(bucket, i), buf, blocksize);
4469 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08004470
Tao Ma01225592008-08-18 17:38:53 +08004471out:
Tao Ma01225592008-08-18 17:38:53 +08004472 kfree(bucket_buf);
4473 return ret;
4474}
4475
4476/*
Joel Beckerb5c03e72008-11-25 19:58:16 -08004477 * prev_blkno points to the start of an existing extent. new_blkno
4478 * points to a newly allocated extent. Because we know each of our
4479 * clusters contains more than bucket, we can easily split one cluster
4480 * at a bucket boundary. So we take the last cluster of the existing
4481 * extent and split it down the middle. We move the last half of the
4482 * buckets in the last cluster of the existing extent over to the new
4483 * extent.
Tao Ma01225592008-08-18 17:38:53 +08004484 *
Joel Beckerb5c03e72008-11-25 19:58:16 -08004485 * first_bh is the buffer at prev_blkno so we can update the existing
4486 * extent's bucket count. header_bh is the bucket were we were hoping
4487 * to insert our xattr. If the bucket move places the target in the new
4488 * extent, we'll update first_bh and header_bh after modifying the old
4489 * extent.
4490 *
4491 * first_hash will be set as the 1st xe's name_hash in the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004492 */
4493static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4494 handle_t *handle,
Joel Becker41cb8142008-11-26 14:25:21 -08004495 struct ocfs2_xattr_bucket *first,
4496 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004497 u64 new_blkno,
Tao Ma01225592008-08-18 17:38:53 +08004498 u32 num_clusters,
4499 u32 *first_hash)
4500{
Joel Beckerc58b6032008-11-26 13:36:24 -08004501 int ret;
Joel Becker41cb8142008-11-26 14:25:21 -08004502 struct super_block *sb = inode->i_sb;
4503 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4504 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
Joel Beckerb5c03e72008-11-25 19:58:16 -08004505 int to_move = num_buckets / 2;
Joel Beckerc58b6032008-11-26 13:36:24 -08004506 u64 src_blkno;
Joel Becker41cb8142008-11-26 14:25:21 -08004507 u64 last_cluster_blkno = bucket_blkno(first) +
4508 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08004509
Joel Becker41cb8142008-11-26 14:25:21 -08004510 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4511 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
Tao Ma01225592008-08-18 17:38:53 +08004512
Tao Ma402b4182011-02-23 22:01:17 +08004513 trace_ocfs2_mv_xattr_bucket_cross_cluster(
4514 (unsigned long long)last_cluster_blkno,
4515 (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004516
Joel Becker41cb8142008-11-26 14:25:21 -08004517 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
Joel Beckerc58b6032008-11-26 13:36:24 -08004518 last_cluster_blkno, new_blkno,
4519 to_move, first_hash);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004520 if (ret) {
4521 mlog_errno(ret);
4522 goto out;
4523 }
4524
Joel Beckerc58b6032008-11-26 13:36:24 -08004525 /* This is the first bucket that got moved */
4526 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4527
Tao Ma01225592008-08-18 17:38:53 +08004528 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08004529 * If the target bucket was part of the moved buckets, we need to
Joel Becker41cb8142008-11-26 14:25:21 -08004530 * update first and target.
Joel Beckerb5c03e72008-11-25 19:58:16 -08004531 */
Joel Becker41cb8142008-11-26 14:25:21 -08004532 if (bucket_blkno(target) >= src_blkno) {
Joel Beckerb5c03e72008-11-25 19:58:16 -08004533 /* Find the block for the new target bucket */
4534 src_blkno = new_blkno +
Joel Becker41cb8142008-11-26 14:25:21 -08004535 (bucket_blkno(target) - src_blkno);
4536
4537 ocfs2_xattr_bucket_relse(first);
4538 ocfs2_xattr_bucket_relse(target);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004539
4540 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08004541 * These shouldn't fail - the buffers are in the
Joel Beckerb5c03e72008-11-25 19:58:16 -08004542 * journal from ocfs2_cp_xattr_bucket().
4543 */
Joel Becker41cb8142008-11-26 14:25:21 -08004544 ret = ocfs2_read_xattr_bucket(first, new_blkno);
Joel Beckerc58b6032008-11-26 13:36:24 -08004545 if (ret) {
4546 mlog_errno(ret);
4547 goto out;
4548 }
Joel Becker41cb8142008-11-26 14:25:21 -08004549 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4550 if (ret)
Joel Beckerb5c03e72008-11-25 19:58:16 -08004551 mlog_errno(ret);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004552
Joel Beckerb5c03e72008-11-25 19:58:16 -08004553 }
4554
Tao Ma01225592008-08-18 17:38:53 +08004555out:
Tao Ma01225592008-08-18 17:38:53 +08004556 return ret;
4557}
4558
Tao Ma01225592008-08-18 17:38:53 +08004559/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004560 * Find the suitable pos when we divide a bucket into 2.
4561 * We have to make sure the xattrs with the same hash value exist
4562 * in the same bucket.
4563 *
4564 * If this ocfs2_xattr_header covers more than one hash value, find a
4565 * place where the hash value changes. Try to find the most even split.
4566 * The most common case is that all entries have different hash values,
4567 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08004568 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004569static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4570{
4571 struct ocfs2_xattr_entry *entries = xh->xh_entries;
4572 int count = le16_to_cpu(xh->xh_count);
4573 int delta, middle = count / 2;
4574
4575 /*
4576 * We start at the middle. Each step gets farther away in both
4577 * directions. We therefore hit the change in hash value
4578 * nearest to the middle. Note that this loop does not execute for
4579 * count < 2.
4580 */
4581 for (delta = 0; delta < middle; delta++) {
4582 /* Let's check delta earlier than middle */
4583 if (cmp_xe(&entries[middle - delta - 1],
4584 &entries[middle - delta]))
4585 return middle - delta;
4586
4587 /* For even counts, don't walk off the end */
4588 if ((middle + delta + 1) == count)
4589 continue;
4590
4591 /* Now try delta past middle */
4592 if (cmp_xe(&entries[middle + delta],
4593 &entries[middle + delta + 1]))
4594 return middle + delta + 1;
4595 }
4596
4597 /* Every entry had the same hash */
4598 return count;
4599}
4600
4601/*
4602 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4603 * first_hash will record the 1st hash of the new bucket.
4604 *
4605 * Normally half of the xattrs will be moved. But we have to make
4606 * sure that the xattrs with the same hash value are stored in the
4607 * same bucket. If all the xattrs in this bucket have the same hash
4608 * value, the new bucket will be initialized as an empty one and the
4609 * first_hash will be initialized as (hash_value+1).
4610 */
4611static int ocfs2_divide_xattr_bucket(struct inode *inode,
4612 handle_t *handle,
4613 u64 blk,
4614 u64 new_blk,
4615 u32 *first_hash,
4616 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08004617{
4618 int ret, i;
Joel Becker199799a2009-08-14 19:04:15 -07004619 int count, start, len, name_value_len = 0, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07004620 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004621 struct ocfs2_xattr_header *xh;
4622 struct ocfs2_xattr_entry *xe;
4623 int blocksize = inode->i_sb->s_blocksize;
4624
Tao Ma402b4182011-02-23 22:01:17 +08004625 trace_ocfs2_divide_xattr_bucket_begin((unsigned long long)blk,
4626 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004627
Joel Beckerba937122008-10-24 19:13:20 -07004628 s_bucket = ocfs2_xattr_bucket_new(inode);
4629 t_bucket = ocfs2_xattr_bucket_new(inode);
4630 if (!s_bucket || !t_bucket) {
4631 ret = -ENOMEM;
4632 mlog_errno(ret);
4633 goto out;
4634 }
Tao Ma01225592008-08-18 17:38:53 +08004635
Joel Beckerba937122008-10-24 19:13:20 -07004636 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08004637 if (ret) {
4638 mlog_errno(ret);
4639 goto out;
4640 }
4641
Joel Beckerba937122008-10-24 19:13:20 -07004642 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004643 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004644 if (ret) {
4645 mlog_errno(ret);
4646 goto out;
4647 }
4648
Joel Becker784b8162008-10-24 17:33:40 -07004649 /*
4650 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4651 * there's no need to read it.
4652 */
Wengang Wang9c339252014-04-03 14:47:15 -07004653 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk, new_bucket_head);
Tao Ma01225592008-08-18 17:38:53 +08004654 if (ret) {
4655 mlog_errno(ret);
4656 goto out;
4657 }
4658
Joel Becker2b656c12008-11-25 19:00:15 -08004659 /*
4660 * Hey, if we're overwriting t_bucket, what difference does
4661 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4662 * same part of ocfs2_cp_xattr_bucket().
4663 */
Joel Beckerba937122008-10-24 19:13:20 -07004664 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004665 new_bucket_head ?
4666 OCFS2_JOURNAL_ACCESS_CREATE :
4667 OCFS2_JOURNAL_ACCESS_WRITE);
4668 if (ret) {
4669 mlog_errno(ret);
4670 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004671 }
4672
Joel Beckerba937122008-10-24 19:13:20 -07004673 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004674 count = le16_to_cpu(xh->xh_count);
4675 start = ocfs2_xattr_find_divide_pos(xh);
4676
4677 if (start == count) {
4678 xe = &xh->xh_entries[start-1];
4679
4680 /*
4681 * initialized a new empty bucket here.
4682 * The hash value is set as one larger than
4683 * that of the last entry in the previous bucket.
4684 */
Joel Beckerba937122008-10-24 19:13:20 -07004685 for (i = 0; i < t_bucket->bu_blocks; i++)
4686 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08004687
Joel Beckerba937122008-10-24 19:13:20 -07004688 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004689 xh->xh_free_start = cpu_to_le16(blocksize);
4690 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4691 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4692
4693 goto set_num_buckets;
4694 }
4695
Tao Ma01225592008-08-18 17:38:53 +08004696 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07004697 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004698
4699 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07004700 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004701
4702 /*
4703 * Calculate the total name/value len and xh_free_start for
4704 * the old bucket first.
4705 */
4706 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4707 name_value_len = 0;
4708 for (i = 0; i < start; i++) {
4709 xe = &xh->xh_entries[i];
Joel Becker199799a2009-08-14 19:04:15 -07004710 name_value_len += namevalue_size_xe(xe);
Tao Ma01225592008-08-18 17:38:53 +08004711 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4712 name_offset = le16_to_cpu(xe->xe_name_offset);
4713 }
4714
4715 /*
4716 * Now begin the modification to the new bucket.
4717 *
4718 * In the new bucket, We just move the xattr entry to the beginning
4719 * and don't touch the name/value. So there will be some holes in the
4720 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4721 * called.
4722 */
4723 xe = &xh->xh_entries[start];
4724 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
Tao Ma402b4182011-02-23 22:01:17 +08004725 trace_ocfs2_divide_xattr_bucket_move(len,
4726 (int)((char *)xe - (char *)xh),
4727 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08004728 memmove((char *)xh->xh_entries, (char *)xe, len);
4729 xe = &xh->xh_entries[count - start];
4730 len = sizeof(struct ocfs2_xattr_entry) * start;
4731 memset((char *)xe, 0, len);
4732
4733 le16_add_cpu(&xh->xh_count, -start);
4734 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4735
4736 /* Calculate xh_free_start for the new bucket. */
4737 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4738 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4739 xe = &xh->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08004740 if (le16_to_cpu(xe->xe_name_offset) <
4741 le16_to_cpu(xh->xh_free_start))
4742 xh->xh_free_start = xe->xe_name_offset;
4743 }
4744
Tao Ma80bcaf32008-10-27 06:06:24 +08004745set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08004746 /* set xh->xh_num_buckets for the new xh. */
4747 if (new_bucket_head)
4748 xh->xh_num_buckets = cpu_to_le16(1);
4749 else
4750 xh->xh_num_buckets = 0;
4751
Joel Beckerba937122008-10-24 19:13:20 -07004752 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004753
4754 /* store the first_hash of the new bucket. */
4755 if (first_hash)
4756 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4757
4758 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08004759 * Now only update the 1st block of the old bucket. If we
4760 * just added a new empty bucket, there is no need to modify
4761 * it.
Tao Ma01225592008-08-18 17:38:53 +08004762 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004763 if (start == count)
4764 goto out;
4765
Joel Beckerba937122008-10-24 19:13:20 -07004766 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004767 memset(&xh->xh_entries[start], 0,
4768 sizeof(struct ocfs2_xattr_entry) * (count - start));
4769 xh->xh_count = cpu_to_le16(start);
4770 xh->xh_free_start = cpu_to_le16(name_offset);
4771 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4772
Joel Beckerba937122008-10-24 19:13:20 -07004773 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004774
4775out:
Joel Beckerba937122008-10-24 19:13:20 -07004776 ocfs2_xattr_bucket_free(s_bucket);
4777 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004778
4779 return ret;
4780}
4781
4782/*
4783 * Copy xattr from one bucket to another bucket.
4784 *
4785 * The caller must make sure that the journal transaction
4786 * has enough space for journaling.
4787 */
4788static int ocfs2_cp_xattr_bucket(struct inode *inode,
4789 handle_t *handle,
4790 u64 s_blkno,
4791 u64 t_blkno,
4792 int t_is_new)
4793{
Joel Becker4980c6d2008-10-24 18:54:43 -07004794 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07004795 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004796
4797 BUG_ON(s_blkno == t_blkno);
4798
Tao Ma402b4182011-02-23 22:01:17 +08004799 trace_ocfs2_cp_xattr_bucket((unsigned long long)s_blkno,
4800 (unsigned long long)t_blkno,
4801 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08004802
Joel Beckerba937122008-10-24 19:13:20 -07004803 s_bucket = ocfs2_xattr_bucket_new(inode);
4804 t_bucket = ocfs2_xattr_bucket_new(inode);
4805 if (!s_bucket || !t_bucket) {
4806 ret = -ENOMEM;
4807 mlog_errno(ret);
4808 goto out;
4809 }
Joel Becker92de1092008-11-25 17:06:40 -08004810
Joel Beckerba937122008-10-24 19:13:20 -07004811 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004812 if (ret)
4813 goto out;
4814
Joel Becker784b8162008-10-24 17:33:40 -07004815 /*
4816 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4817 * there's no need to read it.
4818 */
Wengang Wang9c339252014-04-03 14:47:15 -07004819 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno, t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08004820 if (ret)
4821 goto out;
4822
Joel Becker2b656c12008-11-25 19:00:15 -08004823 /*
4824 * Hey, if we're overwriting t_bucket, what difference does
4825 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
Joel Becker874d65a2008-11-26 13:02:18 -08004826 * cluster to fill, we came here from
4827 * ocfs2_mv_xattr_buckets(), and it is really new -
4828 * ACCESS_CREATE is required. But we also might have moved data
4829 * out of t_bucket before extending back into it.
4830 * ocfs2_add_new_xattr_bucket() can do this - its call to
4831 * ocfs2_add_new_xattr_cluster() may have created a new extent
Joel Becker2b656c12008-11-25 19:00:15 -08004832 * and copied out the end of the old extent. Then it re-extends
4833 * the old extent back to create space for new xattrs. That's
4834 * how we get here, and the bucket isn't really new.
4835 */
Joel Beckerba937122008-10-24 19:13:20 -07004836 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004837 t_is_new ?
4838 OCFS2_JOURNAL_ACCESS_CREATE :
4839 OCFS2_JOURNAL_ACCESS_WRITE);
4840 if (ret)
4841 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004842
Joel Beckerba937122008-10-24 19:13:20 -07004843 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4844 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004845
4846out:
Joel Beckerba937122008-10-24 19:13:20 -07004847 ocfs2_xattr_bucket_free(t_bucket);
4848 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004849
4850 return ret;
4851}
4852
4853/*
Joel Becker874d65a2008-11-26 13:02:18 -08004854 * src_blk points to the start of an existing extent. last_blk points to
4855 * last cluster in that extent. to_blk points to a newly allocated
Joel Becker54ecb6b2008-11-26 13:18:31 -08004856 * extent. We copy the buckets from the cluster at last_blk to the new
4857 * extent. If start_bucket is non-zero, we skip that many buckets before
4858 * we start copying. The new extent's xh_num_buckets gets set to the
4859 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4860 * by the same amount.
Tao Ma01225592008-08-18 17:38:53 +08004861 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004862static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4863 u64 src_blk, u64 last_blk, u64 to_blk,
4864 unsigned int start_bucket,
4865 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004866{
4867 int i, ret, credits;
4868 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Becker15d60922008-11-25 18:36:42 -08004869 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004870 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
Joel Becker15d60922008-11-25 18:36:42 -08004871 struct ocfs2_xattr_bucket *old_first, *new_first;
Tao Ma01225592008-08-18 17:38:53 +08004872
Tao Ma402b4182011-02-23 22:01:17 +08004873 trace_ocfs2_mv_xattr_buckets((unsigned long long)last_blk,
4874 (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08004875
Joel Becker54ecb6b2008-11-26 13:18:31 -08004876 BUG_ON(start_bucket >= num_buckets);
4877 if (start_bucket) {
4878 num_buckets -= start_bucket;
4879 last_blk += (start_bucket * blks_per_bucket);
4880 }
4881
Joel Becker15d60922008-11-25 18:36:42 -08004882 /* The first bucket of the original extent */
4883 old_first = ocfs2_xattr_bucket_new(inode);
4884 /* The first bucket of the new extent */
4885 new_first = ocfs2_xattr_bucket_new(inode);
4886 if (!old_first || !new_first) {
4887 ret = -ENOMEM;
4888 mlog_errno(ret);
4889 goto out;
4890 }
4891
Joel Becker874d65a2008-11-26 13:02:18 -08004892 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
Joel Becker15d60922008-11-25 18:36:42 -08004893 if (ret) {
4894 mlog_errno(ret);
4895 goto out;
4896 }
4897
Tao Ma01225592008-08-18 17:38:53 +08004898 /*
Joel Becker54ecb6b2008-11-26 13:18:31 -08004899 * We need to update the first bucket of the old extent and all
4900 * the buckets going to the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004901 */
Tao Mac901fb02010-04-26 14:34:57 +08004902 credits = ((num_buckets + 1) * blks_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004903 ret = ocfs2_extend_trans(handle, credits);
4904 if (ret) {
4905 mlog_errno(ret);
4906 goto out;
4907 }
4908
Joel Becker15d60922008-11-25 18:36:42 -08004909 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4910 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004911 if (ret) {
4912 mlog_errno(ret);
4913 goto out;
4914 }
4915
4916 for (i = 0; i < num_buckets; i++) {
4917 ret = ocfs2_cp_xattr_bucket(inode, handle,
Joel Becker874d65a2008-11-26 13:02:18 -08004918 last_blk + (i * blks_per_bucket),
Joel Becker15d60922008-11-25 18:36:42 -08004919 to_blk + (i * blks_per_bucket),
4920 1);
Tao Ma01225592008-08-18 17:38:53 +08004921 if (ret) {
4922 mlog_errno(ret);
4923 goto out;
4924 }
Tao Ma01225592008-08-18 17:38:53 +08004925 }
4926
Joel Becker15d60922008-11-25 18:36:42 -08004927 /*
4928 * Get the new bucket ready before we dirty anything
4929 * (This actually shouldn't fail, because we already dirtied
4930 * it once in ocfs2_cp_xattr_bucket()).
4931 */
4932 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4933 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004934 mlog_errno(ret);
4935 goto out;
4936 }
Joel Becker15d60922008-11-25 18:36:42 -08004937 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4938 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004939 if (ret) {
4940 mlog_errno(ret);
4941 goto out;
4942 }
4943
Joel Becker15d60922008-11-25 18:36:42 -08004944 /* Now update the headers */
4945 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4946 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
Tao Ma01225592008-08-18 17:38:53 +08004947
Joel Becker15d60922008-11-25 18:36:42 -08004948 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4949 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
Tao Ma01225592008-08-18 17:38:53 +08004950
4951 if (first_hash)
Joel Becker15d60922008-11-25 18:36:42 -08004952 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4953
Tao Ma01225592008-08-18 17:38:53 +08004954out:
Joel Becker15d60922008-11-25 18:36:42 -08004955 ocfs2_xattr_bucket_free(new_first);
4956 ocfs2_xattr_bucket_free(old_first);
Tao Ma01225592008-08-18 17:38:53 +08004957 return ret;
4958}
4959
4960/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004961 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08004962 * This function should only be called when bucket size == cluster size.
4963 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4964 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004965static int ocfs2_divide_xattr_cluster(struct inode *inode,
4966 handle_t *handle,
4967 u64 prev_blk,
4968 u64 new_blk,
4969 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004970{
4971 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Mac901fb02010-04-26 14:34:57 +08004972 int ret, credits = 2 * blk_per_bucket;
Tao Ma01225592008-08-18 17:38:53 +08004973
4974 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4975
4976 ret = ocfs2_extend_trans(handle, credits);
4977 if (ret) {
4978 mlog_errno(ret);
4979 return ret;
4980 }
4981
4982 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08004983 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4984 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08004985}
4986
4987/*
4988 * Move some xattrs from the old cluster to the new one since they are not
4989 * contiguous in ocfs2 xattr tree.
4990 *
4991 * new_blk starts a new separate cluster, and we will move some xattrs from
4992 * prev_blk to it. v_start will be set as the first name hash value in this
4993 * new cluster so that it can be used as e_cpos during tree insertion and
4994 * don't collide with our original b-tree operations. first_bh and header_bh
4995 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4996 * to extend the insert bucket.
4997 *
4998 * The problem is how much xattr should we move to the new one and when should
4999 * we update first_bh and header_bh?
5000 * 1. If cluster size > bucket size, that means the previous cluster has more
5001 * than 1 bucket, so just move half nums of bucket into the new cluster and
5002 * update the first_bh and header_bh if the insert bucket has been moved
5003 * to the new cluster.
5004 * 2. If cluster_size == bucket_size:
5005 * a) If the previous extent rec has more than one cluster and the insert
5006 * place isn't in the last cluster, copy the entire last cluster to the
5007 * new one. This time, we don't need to upate the first_bh and header_bh
5008 * since they will not be moved into the new cluster.
5009 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
5010 * the new one. And we set the extend flag to zero if the insert place is
5011 * moved into the new allocated cluster since no extend is needed.
5012 */
5013static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
5014 handle_t *handle,
Joel Becker012ee912008-11-26 14:43:31 -08005015 struct ocfs2_xattr_bucket *first,
5016 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08005017 u64 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08005018 u32 prev_clusters,
5019 u32 *v_start,
5020 int *extend)
5021{
Joel Becker92cf3ad2008-11-26 14:12:09 -08005022 int ret;
Tao Ma01225592008-08-18 17:38:53 +08005023
Tao Ma402b4182011-02-23 22:01:17 +08005024 trace_ocfs2_adjust_xattr_cross_cluster(
5025 (unsigned long long)bucket_blkno(first),
5026 (unsigned long long)new_blk, prev_clusters);
Tao Ma01225592008-08-18 17:38:53 +08005027
Joel Becker41cb8142008-11-26 14:25:21 -08005028 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
Tao Ma01225592008-08-18 17:38:53 +08005029 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
5030 handle,
Joel Becker41cb8142008-11-26 14:25:21 -08005031 first, target,
Tao Ma01225592008-08-18 17:38:53 +08005032 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08005033 prev_clusters,
5034 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08005035 if (ret)
Joel Becker41cb8142008-11-26 14:25:21 -08005036 mlog_errno(ret);
Joel Becker41cb8142008-11-26 14:25:21 -08005037 } else {
Joel Becker92cf3ad2008-11-26 14:12:09 -08005038 /* The start of the last cluster in the first extent */
5039 u64 last_blk = bucket_blkno(first) +
5040 ((prev_clusters - 1) *
5041 ocfs2_clusters_to_blocks(inode->i_sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08005042
Joel Becker012ee912008-11-26 14:43:31 -08005043 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
Joel Becker874d65a2008-11-26 13:02:18 -08005044 ret = ocfs2_mv_xattr_buckets(inode, handle,
Joel Becker92cf3ad2008-11-26 14:12:09 -08005045 bucket_blkno(first),
Joel Becker54ecb6b2008-11-26 13:18:31 -08005046 last_blk, new_blk, 0,
Tao Ma01225592008-08-18 17:38:53 +08005047 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08005048 if (ret)
5049 mlog_errno(ret);
5050 } else {
Tao Ma80bcaf32008-10-27 06:06:24 +08005051 ret = ocfs2_divide_xattr_cluster(inode, handle,
5052 last_blk, new_blk,
5053 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08005054 if (ret)
5055 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08005056
Joel Becker92cf3ad2008-11-26 14:12:09 -08005057 if ((bucket_blkno(target) == last_blk) && extend)
Tao Ma01225592008-08-18 17:38:53 +08005058 *extend = 0;
5059 }
5060 }
5061
5062 return ret;
5063}
5064
5065/*
5066 * Add a new cluster for xattr storage.
5067 *
5068 * If the new cluster is contiguous with the previous one, it will be
5069 * appended to the same extent record, and num_clusters will be updated.
5070 * If not, we will insert a new extent for it and move some xattrs in
5071 * the last cluster into the new allocated one.
5072 * We also need to limit the maximum size of a btree leaf, otherwise we'll
5073 * lose the benefits of hashing because we'll have to search large leaves.
5074 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
5075 * if it's bigger).
5076 *
5077 * first_bh is the first block of the previous extent rec and header_bh
5078 * indicates the bucket we will insert the new xattrs. They will be updated
5079 * when the header_bh is moved into the new cluster.
5080 */
5081static int ocfs2_add_new_xattr_cluster(struct inode *inode,
5082 struct buffer_head *root_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08005083 struct ocfs2_xattr_bucket *first,
5084 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08005085 u32 *num_clusters,
5086 u32 prev_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08005087 int *extend,
5088 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005089{
Tao Ma85db90e2008-11-12 08:27:01 +08005090 int ret;
Tao Ma01225592008-08-18 17:38:53 +08005091 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
5092 u32 prev_clusters = *num_clusters;
5093 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
5094 u64 block;
Tao Ma85db90e2008-11-12 08:27:01 +08005095 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08005096 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07005097 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08005098
Tao Ma402b4182011-02-23 22:01:17 +08005099 trace_ocfs2_add_new_xattr_cluster_begin(
5100 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5101 (unsigned long long)bucket_blkno(first),
5102 prev_cpos, prev_clusters);
Tao Ma01225592008-08-18 17:38:53 +08005103
Joel Becker5e404e92009-02-13 03:54:22 -08005104 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07005105
Joel Becker0cf2f762009-02-12 16:41:25 -08005106 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08005107 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005108 if (ret < 0) {
5109 mlog_errno(ret);
5110 goto leave;
5111 }
5112
Joel Becker1ed9b772010-05-06 13:59:06 +08005113 ret = __ocfs2_claim_clusters(handle, ctxt->data_ac, 1,
Tao Ma01225592008-08-18 17:38:53 +08005114 clusters_to_add, &bit_off, &num_bits);
5115 if (ret < 0) {
5116 if (ret != -ENOSPC)
5117 mlog_errno(ret);
5118 goto leave;
5119 }
5120
5121 BUG_ON(num_bits > clusters_to_add);
5122
5123 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Tao Ma402b4182011-02-23 22:01:17 +08005124 trace_ocfs2_add_new_xattr_cluster((unsigned long long)block, num_bits);
Tao Ma01225592008-08-18 17:38:53 +08005125
Joel Beckered29c0c2008-11-26 15:08:44 -08005126 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
Tao Ma01225592008-08-18 17:38:53 +08005127 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5128 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5129 /*
5130 * If this cluster is contiguous with the old one and
5131 * adding this new cluster, we don't surpass the limit of
5132 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5133 * initialized and used like other buckets in the previous
5134 * cluster.
5135 * So add it as a contiguous one. The caller will handle
5136 * its init process.
5137 */
5138 v_start = prev_cpos + prev_clusters;
5139 *num_clusters = prev_clusters + num_bits;
Tao Ma01225592008-08-18 17:38:53 +08005140 } else {
5141 ret = ocfs2_adjust_xattr_cross_cluster(inode,
5142 handle,
Joel Becker012ee912008-11-26 14:43:31 -08005143 first,
5144 target,
Tao Ma01225592008-08-18 17:38:53 +08005145 block,
Tao Ma01225592008-08-18 17:38:53 +08005146 prev_clusters,
5147 &v_start,
5148 extend);
5149 if (ret) {
5150 mlog_errno(ret);
5151 goto leave;
5152 }
5153 }
5154
Tao Ma402b4182011-02-23 22:01:17 +08005155 trace_ocfs2_add_new_xattr_cluster_insert((unsigned long long)block,
5156 v_start, num_bits);
Joel Beckercc79d8c2009-02-13 03:24:43 -08005157 ret = ocfs2_insert_extent(handle, &et, v_start, block,
Tao Ma78f30c32008-11-12 08:27:00 +08005158 num_bits, 0, ctxt->meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08005159 if (ret < 0) {
5160 mlog_errno(ret);
5161 goto leave;
5162 }
5163
Joel Beckerec20cec2010-03-19 14:13:52 -07005164 ocfs2_journal_dirty(handle, root_bh);
Tao Ma01225592008-08-18 17:38:53 +08005165
5166leave:
Tao Ma01225592008-08-18 17:38:53 +08005167 return ret;
5168}
5169
5170/*
Joel Becker92de1092008-11-25 17:06:40 -08005171 * We are given an extent. 'first' is the bucket at the very front of
5172 * the extent. The extent has space for an additional bucket past
5173 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5174 * of the target bucket. We wish to shift every bucket past the target
5175 * down one, filling in that additional space. When we get back to the
5176 * target, we split the target between itself and the now-empty bucket
5177 * at target+1 (aka, target_blkno + blks_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08005178 */
5179static int ocfs2_extend_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005180 handle_t *handle,
Joel Becker92de1092008-11-25 17:06:40 -08005181 struct ocfs2_xattr_bucket *first,
5182 u64 target_blk,
Tao Ma01225592008-08-18 17:38:53 +08005183 u32 num_clusters)
5184{
5185 int ret, credits;
5186 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5187 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker92de1092008-11-25 17:06:40 -08005188 u64 end_blk;
5189 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
Tao Ma01225592008-08-18 17:38:53 +08005190
Tao Ma402b4182011-02-23 22:01:17 +08005191 trace_ocfs2_extend_xattr_bucket((unsigned long long)target_blk,
5192 (unsigned long long)bucket_blkno(first),
5193 num_clusters, new_bucket);
Tao Ma01225592008-08-18 17:38:53 +08005194
Joel Becker92de1092008-11-25 17:06:40 -08005195 /* The extent must have room for an additional bucket */
5196 BUG_ON(new_bucket >=
5197 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
Tao Ma01225592008-08-18 17:38:53 +08005198
Joel Becker92de1092008-11-25 17:06:40 -08005199 /* end_blk points to the last existing bucket */
5200 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08005201
5202 /*
Joel Becker92de1092008-11-25 17:06:40 -08005203 * end_blk is the start of the last existing bucket.
5204 * Thus, (end_blk - target_blk) covers the target bucket and
5205 * every bucket after it up to, but not including, the last
5206 * existing bucket. Then we add the last existing bucket, the
5207 * new bucket, and the first bucket (3 * blk_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08005208 */
Tao Mac901fb02010-04-26 14:34:57 +08005209 credits = (end_blk - target_blk) + (3 * blk_per_bucket);
Tao Ma85db90e2008-11-12 08:27:01 +08005210 ret = ocfs2_extend_trans(handle, credits);
5211 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08005212 mlog_errno(ret);
5213 goto out;
5214 }
5215
Joel Becker92de1092008-11-25 17:06:40 -08005216 ret = ocfs2_xattr_bucket_journal_access(handle, first,
5217 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005218 if (ret) {
5219 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08005220 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005221 }
5222
Joel Becker92de1092008-11-25 17:06:40 -08005223 while (end_blk != target_blk) {
Tao Ma01225592008-08-18 17:38:53 +08005224 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5225 end_blk + blk_per_bucket, 0);
5226 if (ret)
Tao Ma85db90e2008-11-12 08:27:01 +08005227 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005228 end_blk -= blk_per_bucket;
5229 }
5230
Joel Becker92de1092008-11-25 17:06:40 -08005231 /* Move half of the xattr in target_blkno to the next bucket. */
5232 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5233 target_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08005234
Joel Becker92de1092008-11-25 17:06:40 -08005235 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5236 ocfs2_xattr_bucket_journal_dirty(handle, first);
Tao Ma01225592008-08-18 17:38:53 +08005237
Tao Ma01225592008-08-18 17:38:53 +08005238out:
5239 return ret;
5240}
5241
5242/*
Joel Becker91f20332008-11-26 15:25:41 -08005243 * Add new xattr bucket in an extent record and adjust the buckets
5244 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5245 * bucket we want to insert into.
Tao Ma01225592008-08-18 17:38:53 +08005246 *
Joel Becker91f20332008-11-26 15:25:41 -08005247 * In the easy case, we will move all the buckets after target down by
5248 * one. Half of target's xattrs will be moved to the next bucket.
5249 *
5250 * If current cluster is full, we'll allocate a new one. This may not
5251 * be contiguous. The underlying calls will make sure that there is
5252 * space for the insert, shifting buckets around if necessary.
5253 * 'target' may be moved by those calls.
Tao Ma01225592008-08-18 17:38:53 +08005254 */
5255static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5256 struct buffer_head *xb_bh,
Joel Becker91f20332008-11-26 15:25:41 -08005257 struct ocfs2_xattr_bucket *target,
Tao Ma78f30c32008-11-12 08:27:00 +08005258 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005259{
Tao Ma01225592008-08-18 17:38:53 +08005260 struct ocfs2_xattr_block *xb =
5261 (struct ocfs2_xattr_block *)xb_bh->b_data;
5262 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5263 struct ocfs2_extent_list *el = &xb_root->xt_list;
Joel Becker91f20332008-11-26 15:25:41 -08005264 u32 name_hash =
5265 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
Joel Beckered29c0c2008-11-26 15:08:44 -08005266 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08005267 int ret, num_buckets, extend = 1;
5268 u64 p_blkno;
5269 u32 e_cpos, num_clusters;
Joel Becker92de1092008-11-25 17:06:40 -08005270 /* The bucket at the front of the extent */
Joel Becker91f20332008-11-26 15:25:41 -08005271 struct ocfs2_xattr_bucket *first;
Tao Ma01225592008-08-18 17:38:53 +08005272
Tao Ma402b4182011-02-23 22:01:17 +08005273 trace_ocfs2_add_new_xattr_bucket(
5274 (unsigned long long)bucket_blkno(target));
Tao Ma01225592008-08-18 17:38:53 +08005275
Joel Beckered29c0c2008-11-26 15:08:44 -08005276 /* The first bucket of the original extent */
Joel Becker92de1092008-11-25 17:06:40 -08005277 first = ocfs2_xattr_bucket_new(inode);
Joel Becker91f20332008-11-26 15:25:41 -08005278 if (!first) {
Joel Becker92de1092008-11-25 17:06:40 -08005279 ret = -ENOMEM;
5280 mlog_errno(ret);
5281 goto out;
5282 }
5283
Tao Ma01225592008-08-18 17:38:53 +08005284 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5285 &num_clusters, el);
5286 if (ret) {
5287 mlog_errno(ret);
5288 goto out;
5289 }
5290
Joel Beckered29c0c2008-11-26 15:08:44 -08005291 ret = ocfs2_read_xattr_bucket(first, p_blkno);
5292 if (ret) {
5293 mlog_errno(ret);
5294 goto out;
5295 }
5296
Tao Ma01225592008-08-18 17:38:53 +08005297 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
Joel Beckered29c0c2008-11-26 15:08:44 -08005298 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5299 /*
5300 * This can move first+target if the target bucket moves
5301 * to the new extent.
5302 */
Tao Ma01225592008-08-18 17:38:53 +08005303 ret = ocfs2_add_new_xattr_cluster(inode,
5304 xb_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08005305 first,
5306 target,
Tao Ma01225592008-08-18 17:38:53 +08005307 &num_clusters,
5308 e_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08005309 &extend,
5310 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005311 if (ret) {
5312 mlog_errno(ret);
5313 goto out;
5314 }
5315 }
5316
Joel Becker92de1092008-11-25 17:06:40 -08005317 if (extend) {
Tao Ma01225592008-08-18 17:38:53 +08005318 ret = ocfs2_extend_xattr_bucket(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005319 ctxt->handle,
Joel Beckered29c0c2008-11-26 15:08:44 -08005320 first,
5321 bucket_blkno(target),
Tao Ma01225592008-08-18 17:38:53 +08005322 num_clusters);
Joel Becker92de1092008-11-25 17:06:40 -08005323 if (ret)
5324 mlog_errno(ret);
5325 }
5326
Tao Ma01225592008-08-18 17:38:53 +08005327out:
Joel Becker92de1092008-11-25 17:06:40 -08005328 ocfs2_xattr_bucket_free(first);
Joel Beckered29c0c2008-11-26 15:08:44 -08005329
Tao Ma01225592008-08-18 17:38:53 +08005330 return ret;
5331}
5332
Tao Ma01225592008-08-18 17:38:53 +08005333/*
Tao Ma01225592008-08-18 17:38:53 +08005334 * Truncate the specified xe_off entry in xattr bucket.
5335 * bucket is indicated by header_bh and len is the new length.
5336 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5337 *
5338 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5339 */
5340static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
Joel Becker548b0f22008-11-24 19:32:13 -08005341 struct ocfs2_xattr_bucket *bucket,
Tao Ma01225592008-08-18 17:38:53 +08005342 int xe_off,
Tao Ma78f30c32008-11-12 08:27:00 +08005343 int len,
5344 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005345{
5346 int ret, offset;
5347 u64 value_blk;
Tao Ma01225592008-08-18 17:38:53 +08005348 struct ocfs2_xattr_entry *xe;
Joel Becker548b0f22008-11-24 19:32:13 -08005349 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08005350 size_t blocksize = inode->i_sb->s_blocksize;
Joel Beckerb3e5d372008-12-09 15:01:04 -08005351 struct ocfs2_xattr_value_buf vb = {
5352 .vb_access = ocfs2_journal_access,
5353 };
Tao Ma01225592008-08-18 17:38:53 +08005354
5355 xe = &xh->xh_entries[xe_off];
5356
5357 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5358
5359 offset = le16_to_cpu(xe->xe_name_offset) +
5360 OCFS2_XATTR_SIZE(xe->xe_name_len);
5361
5362 value_blk = offset / blocksize;
5363
5364 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5365 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
Tao Ma01225592008-08-18 17:38:53 +08005366
Joel Beckerb3e5d372008-12-09 15:01:04 -08005367 vb.vb_bh = bucket->bu_bhs[value_blk];
5368 BUG_ON(!vb.vb_bh);
Tao Ma01225592008-08-18 17:38:53 +08005369
Joel Beckerb3e5d372008-12-09 15:01:04 -08005370 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5371 (vb.vb_bh->b_data + offset % blocksize);
Tao Ma01225592008-08-18 17:38:53 +08005372
Joel Becker548b0f22008-11-24 19:32:13 -08005373 /*
5374 * From here on out we have to dirty the bucket. The generic
5375 * value calls only modify one of the bucket's bhs, but we need
5376 * to send the bucket at once. So if they error, they *could* have
5377 * modified something. We have to assume they did, and dirty
5378 * the whole bucket. This leaves us in a consistent state.
5379 */
Tao Ma402b4182011-02-23 22:01:17 +08005380 trace_ocfs2_xattr_bucket_value_truncate(
5381 (unsigned long long)bucket_blkno(bucket), xe_off, len);
Joel Beckerb3e5d372008-12-09 15:01:04 -08005382 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005383 if (ret) {
5384 mlog_errno(ret);
Tao Ma554e7f92009-01-08 08:21:43 +08005385 goto out;
5386 }
5387
5388 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5389 OCFS2_JOURNAL_ACCESS_WRITE);
5390 if (ret) {
5391 mlog_errno(ret);
5392 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005393 }
5394
Joel Becker548b0f22008-11-24 19:32:13 -08005395 xe->xe_value_size = cpu_to_le64(len);
5396
Joel Becker548b0f22008-11-24 19:32:13 -08005397 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08005398
5399out:
Tao Ma01225592008-08-18 17:38:53 +08005400 return ret;
5401}
5402
Tao Ma01225592008-08-18 17:38:53 +08005403static int ocfs2_rm_xattr_cluster(struct inode *inode,
5404 struct buffer_head *root_bh,
5405 u64 blkno,
5406 u32 cpos,
Tao Ma47bca492009-08-18 11:43:42 +08005407 u32 len,
5408 void *para)
Tao Ma01225592008-08-18 17:38:53 +08005409{
5410 int ret;
5411 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5412 struct inode *tl_inode = osb->osb_tl_inode;
5413 handle_t *handle;
5414 struct ocfs2_xattr_block *xb =
5415 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08005416 struct ocfs2_alloc_context *meta_ac = NULL;
5417 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07005418 struct ocfs2_extent_tree et;
5419
Tao Ma47bca492009-08-18 11:43:42 +08005420 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
Tao Mace9c5a52009-08-18 11:43:59 +08005421 ocfs2_delete_xattr_in_bucket, para);
Tao Ma47bca492009-08-18 11:43:42 +08005422 if (ret) {
5423 mlog_errno(ret);
5424 return ret;
5425 }
5426
Joel Becker5e404e92009-02-13 03:54:22 -08005427 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Tao Ma01225592008-08-18 17:38:53 +08005428
5429 ocfs2_init_dealloc_ctxt(&dealloc);
5430
Tao Ma402b4182011-02-23 22:01:17 +08005431 trace_ocfs2_rm_xattr_cluster(
5432 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5433 (unsigned long long)blkno, cpos, len);
Tao Ma01225592008-08-18 17:38:53 +08005434
Joel Becker8cb471e2009-02-10 20:00:41 -08005435 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5436 len);
Tao Ma01225592008-08-18 17:38:53 +08005437
Joel Beckerf99b9b72008-08-20 19:36:33 -07005438 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08005439 if (ret) {
5440 mlog_errno(ret);
5441 return ret;
5442 }
5443
5444 mutex_lock(&tl_inode->i_mutex);
5445
5446 if (ocfs2_truncate_log_needs_flush(osb)) {
5447 ret = __ocfs2_flush_truncate_log(osb);
5448 if (ret < 0) {
5449 mlog_errno(ret);
5450 goto out;
5451 }
5452 }
5453
Jan Karaa90714c2008-10-09 19:38:40 +02005454 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Tao Mad3264792008-10-24 07:57:28 +08005455 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08005456 ret = -ENOMEM;
5457 mlog_errno(ret);
5458 goto out;
5459 }
5460
Joel Becker0cf2f762009-02-12 16:41:25 -08005461 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08005462 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005463 if (ret) {
5464 mlog_errno(ret);
5465 goto out_commit;
5466 }
5467
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005468 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005469 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08005470 if (ret) {
5471 mlog_errno(ret);
5472 goto out_commit;
5473 }
5474
5475 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
Joel Beckerec20cec2010-03-19 14:13:52 -07005476 ocfs2_journal_dirty(handle, root_bh);
Tao Ma01225592008-08-18 17:38:53 +08005477
5478 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5479 if (ret)
5480 mlog_errno(ret);
Darrick J. Wong6fdb7022014-04-03 14:47:08 -07005481 ocfs2_update_inode_fsync_trans(handle, inode, 0);
Tao Ma01225592008-08-18 17:38:53 +08005482
5483out_commit:
5484 ocfs2_commit_trans(osb, handle);
5485out:
5486 ocfs2_schedule_truncate_log_flush(osb, 1);
5487
5488 mutex_unlock(&tl_inode->i_mutex);
5489
5490 if (meta_ac)
5491 ocfs2_free_alloc_context(meta_ac);
5492
5493 ocfs2_run_deallocs(osb, &dealloc);
5494
5495 return ret;
5496}
5497
Tao Ma01225592008-08-18 17:38:53 +08005498/*
Tao Ma80bcaf32008-10-27 06:06:24 +08005499 * check whether the xattr bucket is filled up with the same hash value.
5500 * If we want to insert the xattr with the same hash, return -ENOSPC.
5501 * If we want to insert a xattr with different hash value, go ahead
5502 * and ocfs2_divide_xattr_bucket will handle this.
5503 */
Tao Ma01225592008-08-18 17:38:53 +08005504static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08005505 struct ocfs2_xattr_bucket *bucket,
5506 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08005507{
Joel Becker3e632942008-10-24 17:04:49 -07005508 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08005509 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5510
5511 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5512 return 0;
Tao Ma01225592008-08-18 17:38:53 +08005513
5514 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5515 xh->xh_entries[0].xe_name_hash) {
5516 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5517 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07005518 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08005519 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5520 return -ENOSPC;
5521 }
5522
5523 return 0;
5524}
5525
Joel Beckerc5d95df2009-08-18 21:03:24 -07005526/*
5527 * Try to set the entry in the current bucket. If we fail, the caller
5528 * will handle getting us another bucket.
5529 */
5530static int ocfs2_xattr_set_entry_bucket(struct inode *inode,
5531 struct ocfs2_xattr_info *xi,
5532 struct ocfs2_xattr_search *xs,
5533 struct ocfs2_xattr_set_ctxt *ctxt)
5534{
5535 int ret;
5536 struct ocfs2_xa_loc loc;
5537
Tao Ma402b4182011-02-23 22:01:17 +08005538 trace_ocfs2_xattr_set_entry_bucket(xi->xi_name);
Joel Beckerc5d95df2009-08-18 21:03:24 -07005539
5540 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5541 xs->not_found ? NULL : xs->here);
5542 ret = ocfs2_xa_set(&loc, xi, ctxt);
5543 if (!ret) {
5544 xs->here = loc.xl_entry;
5545 goto out;
5546 }
5547 if (ret != -ENOSPC) {
5548 mlog_errno(ret);
5549 goto out;
5550 }
5551
5552 /* Ok, we need space. Let's try defragmenting the bucket. */
5553 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5554 xs->bucket);
5555 if (ret) {
5556 mlog_errno(ret);
5557 goto out;
5558 }
5559
5560 ret = ocfs2_xa_set(&loc, xi, ctxt);
5561 if (!ret) {
5562 xs->here = loc.xl_entry;
5563 goto out;
5564 }
5565 if (ret != -ENOSPC)
5566 mlog_errno(ret);
5567
5568
5569out:
Joel Beckerc5d95df2009-08-18 21:03:24 -07005570 return ret;
5571}
5572
Tao Ma01225592008-08-18 17:38:53 +08005573static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5574 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08005575 struct ocfs2_xattr_search *xs,
5576 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005577{
Joel Beckerc5d95df2009-08-18 21:03:24 -07005578 int ret;
Tao Ma01225592008-08-18 17:38:53 +08005579
Tao Ma402b4182011-02-23 22:01:17 +08005580 trace_ocfs2_xattr_set_entry_index_block(xi->xi_name);
Tao Ma01225592008-08-18 17:38:53 +08005581
Joel Beckerc5d95df2009-08-18 21:03:24 -07005582 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5583 if (!ret)
5584 goto out;
5585 if (ret != -ENOSPC) {
5586 mlog_errno(ret);
5587 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005588 }
5589
Joel Beckerc5d95df2009-08-18 21:03:24 -07005590 /* Ack, need more space. Let's try to get another bucket! */
5591
Tao Ma01225592008-08-18 17:38:53 +08005592 /*
Joel Beckerc5d95df2009-08-18 21:03:24 -07005593 * We do not allow for overlapping ranges between buckets. And
5594 * the maximum number of collisions we will allow for then is
5595 * one bucket's worth, so check it here whether we need to
5596 * add a new bucket for the insert.
Tao Ma01225592008-08-18 17:38:53 +08005597 */
Joel Beckerc5d95df2009-08-18 21:03:24 -07005598 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Becker91f20332008-11-26 15:25:41 -08005599 xs->bucket,
Joel Beckerc5d95df2009-08-18 21:03:24 -07005600 xi->xi_name);
5601 if (ret) {
5602 mlog_errno(ret);
5603 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005604 }
5605
Joel Beckerc5d95df2009-08-18 21:03:24 -07005606 ret = ocfs2_add_new_xattr_bucket(inode,
5607 xs->xattr_bh,
5608 xs->bucket,
5609 ctxt);
5610 if (ret) {
5611 mlog_errno(ret);
5612 goto out;
5613 }
5614
5615 /*
5616 * ocfs2_add_new_xattr_bucket() will have updated
5617 * xs->bucket if it moved, but it will not have updated
5618 * any of the other search fields. Thus, we drop it and
5619 * re-search. Everything should be cached, so it'll be
5620 * quick.
5621 */
5622 ocfs2_xattr_bucket_relse(xs->bucket);
5623 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5624 xi->xi_name_index,
5625 xi->xi_name, xs);
5626 if (ret && ret != -ENODATA)
5627 goto out;
5628 xs->not_found = ret;
5629
5630 /* Ok, we have a new bucket, let's try again */
5631 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5632 if (ret && (ret != -ENOSPC))
5633 mlog_errno(ret);
5634
Tao Ma01225592008-08-18 17:38:53 +08005635out:
Tao Ma01225592008-08-18 17:38:53 +08005636 return ret;
5637}
Tao Maa3944252008-08-18 17:38:54 +08005638
5639static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5640 struct ocfs2_xattr_bucket *bucket,
5641 void *para)
5642{
Tao Mace9c5a52009-08-18 11:43:59 +08005643 int ret = 0, ref_credits;
Joel Becker3e632942008-10-24 17:04:49 -07005644 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08005645 u16 i;
5646 struct ocfs2_xattr_entry *xe;
Tao Ma78f30c32008-11-12 08:27:00 +08005647 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5648 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
Joel Becker548b0f22008-11-24 19:32:13 -08005649 int credits = ocfs2_remove_extent_credits(osb->sb) +
5650 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Mace9c5a52009-08-18 11:43:59 +08005651 struct ocfs2_xattr_value_root *xv;
5652 struct ocfs2_rm_xattr_bucket_para *args =
5653 (struct ocfs2_rm_xattr_bucket_para *)para;
Tao Ma78f30c32008-11-12 08:27:00 +08005654
5655 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005656
5657 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5658 xe = &xh->xh_entries[i];
5659 if (ocfs2_xattr_is_local(xe))
5660 continue;
5661
Tao Mace9c5a52009-08-18 11:43:59 +08005662 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5663 i, &xv, NULL);
Joseph Qi023d4ea2015-04-14 15:43:33 -07005664 if (ret) {
5665 mlog_errno(ret);
5666 break;
5667 }
Tao Mace9c5a52009-08-18 11:43:59 +08005668
5669 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5670 args->ref_ci,
5671 args->ref_root_bh,
5672 &ctxt.meta_ac,
5673 &ref_credits);
5674
5675 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma88c3b062008-12-11 08:54:11 +08005676 if (IS_ERR(ctxt.handle)) {
5677 ret = PTR_ERR(ctxt.handle);
5678 mlog_errno(ret);
5679 break;
5680 }
5681
Joel Becker548b0f22008-11-24 19:32:13 -08005682 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005683 i, 0, &ctxt);
Tao Ma88c3b062008-12-11 08:54:11 +08005684
5685 ocfs2_commit_trans(osb, ctxt.handle);
Tao Mace9c5a52009-08-18 11:43:59 +08005686 if (ctxt.meta_ac) {
5687 ocfs2_free_alloc_context(ctxt.meta_ac);
5688 ctxt.meta_ac = NULL;
5689 }
Tao Maa3944252008-08-18 17:38:54 +08005690 if (ret) {
5691 mlog_errno(ret);
5692 break;
5693 }
5694 }
5695
Tao Mace9c5a52009-08-18 11:43:59 +08005696 if (ctxt.meta_ac)
5697 ocfs2_free_alloc_context(ctxt.meta_ac);
Tao Ma78f30c32008-11-12 08:27:00 +08005698 ocfs2_schedule_truncate_log_flush(osb, 1);
5699 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005700 return ret;
5701}
5702
Mark Fasheh99219ae2008-10-07 14:52:59 -07005703/*
Tao Ma492a8a32009-08-18 11:43:17 +08005704 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5705 * or change the extent record flag), we need to recalculate
5706 * the metaecc for the whole bucket. So it is done here.
5707 *
5708 * Note:
5709 * We have to give the extra credits for the caller.
5710 */
5711static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5712 handle_t *handle,
5713 void *para)
5714{
5715 int ret;
5716 struct ocfs2_xattr_bucket *bucket =
5717 (struct ocfs2_xattr_bucket *)para;
5718
5719 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5720 OCFS2_JOURNAL_ACCESS_WRITE);
5721 if (ret) {
5722 mlog_errno(ret);
5723 return ret;
5724 }
5725
5726 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5727
5728 return 0;
5729}
5730
5731/*
5732 * Special action we need if the xattr value is refcounted.
5733 *
5734 * 1. If the xattr is refcounted, lock the tree.
5735 * 2. CoW the xattr if we are setting the new value and the value
5736 * will be stored outside.
5737 * 3. In other case, decrease_refcount will work for us, so just
5738 * lock the refcount tree, calculate the meta and credits is OK.
5739 *
5740 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5741 * currently CoW is a completed transaction, while this function
5742 * will also lock the allocators and let us deadlock. So we will
5743 * CoW the whole xattr value.
5744 */
5745static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5746 struct ocfs2_dinode *di,
5747 struct ocfs2_xattr_info *xi,
5748 struct ocfs2_xattr_search *xis,
5749 struct ocfs2_xattr_search *xbs,
5750 struct ocfs2_refcount_tree **ref_tree,
5751 int *meta_add,
5752 int *credits)
5753{
5754 int ret = 0;
5755 struct ocfs2_xattr_block *xb;
5756 struct ocfs2_xattr_entry *xe;
5757 char *base;
5758 u32 p_cluster, num_clusters;
5759 unsigned int ext_flags;
5760 int name_offset, name_len;
5761 struct ocfs2_xattr_value_buf vb;
5762 struct ocfs2_xattr_bucket *bucket = NULL;
5763 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5764 struct ocfs2_post_refcount refcount;
5765 struct ocfs2_post_refcount *p = NULL;
5766 struct buffer_head *ref_root_bh = NULL;
5767
5768 if (!xis->not_found) {
5769 xe = xis->here;
5770 name_offset = le16_to_cpu(xe->xe_name_offset);
5771 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5772 base = xis->base;
5773 vb.vb_bh = xis->inode_bh;
5774 vb.vb_access = ocfs2_journal_access_di;
5775 } else {
5776 int i, block_off = 0;
5777 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5778 xe = xbs->here;
5779 name_offset = le16_to_cpu(xe->xe_name_offset);
5780 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5781 i = xbs->here - xbs->header->xh_entries;
5782
5783 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08005784 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma492a8a32009-08-18 11:43:17 +08005785 bucket_xh(xbs->bucket),
5786 i, &block_off,
5787 &name_offset);
5788 if (ret) {
5789 mlog_errno(ret);
5790 goto out;
5791 }
5792 base = bucket_block(xbs->bucket, block_off);
5793 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5794 vb.vb_access = ocfs2_journal_access;
5795
5796 if (ocfs2_meta_ecc(osb)) {
5797 /*create parameters for ocfs2_post_refcount. */
5798 bucket = xbs->bucket;
5799 refcount.credits = bucket->bu_blocks;
5800 refcount.para = bucket;
5801 refcount.func =
5802 ocfs2_xattr_bucket_post_refcount;
5803 p = &refcount;
5804 }
5805 } else {
5806 base = xbs->base;
5807 vb.vb_bh = xbs->xattr_bh;
5808 vb.vb_access = ocfs2_journal_access_xb;
5809 }
5810 }
5811
5812 if (ocfs2_xattr_is_local(xe))
5813 goto out;
5814
5815 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5816 (base + name_offset + name_len);
5817
5818 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5819 &num_clusters, &vb.vb_xv->xr_list,
5820 &ext_flags);
5821 if (ret) {
5822 mlog_errno(ret);
5823 goto out;
5824 }
5825
5826 /*
5827 * We just need to check the 1st extent record, since we always
5828 * CoW the whole xattr. So there shouldn't be a xattr with
5829 * some REFCOUNT extent recs after the 1st one.
5830 */
5831 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5832 goto out;
5833
5834 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5835 1, ref_tree, &ref_root_bh);
5836 if (ret) {
5837 mlog_errno(ret);
5838 goto out;
5839 }
5840
5841 /*
5842 * If we are deleting the xattr or the new size will be stored inside,
5843 * cool, leave it there, the xattr truncate process will remove them
5844 * for us(it still needs the refcount tree lock and the meta, credits).
5845 * And the worse case is that every cluster truncate will split the
5846 * refcount tree, and make the original extent become 3. So we will need
5847 * 2 * cluster more extent recs at most.
5848 */
Joel Becker6b240ff2009-08-14 18:02:52 -07005849 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
Tao Ma492a8a32009-08-18 11:43:17 +08005850
5851 ret = ocfs2_refcounted_xattr_delete_need(inode,
5852 &(*ref_tree)->rf_ci,
5853 ref_root_bh, vb.vb_xv,
5854 meta_add, credits);
5855 if (ret)
5856 mlog_errno(ret);
5857 goto out;
5858 }
5859
5860 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5861 *ref_tree, ref_root_bh, 0,
5862 le32_to_cpu(vb.vb_xv->xr_clusters), p);
5863 if (ret)
5864 mlog_errno(ret);
5865
5866out:
5867 brelse(ref_root_bh);
5868 return ret;
5869}
5870
5871/*
Tao Ma01292412009-09-21 13:04:19 +08005872 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5873 * The physical clusters will be added to refcount tree.
5874 */
5875static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5876 struct ocfs2_xattr_value_root *xv,
5877 struct ocfs2_extent_tree *value_et,
5878 struct ocfs2_caching_info *ref_ci,
5879 struct buffer_head *ref_root_bh,
5880 struct ocfs2_cached_dealloc_ctxt *dealloc,
5881 struct ocfs2_post_refcount *refcount)
5882{
5883 int ret = 0;
5884 u32 clusters = le32_to_cpu(xv->xr_clusters);
5885 u32 cpos, p_cluster, num_clusters;
5886 struct ocfs2_extent_list *el = &xv->xr_list;
5887 unsigned int ext_flags;
5888
5889 cpos = 0;
5890 while (cpos < clusters) {
5891 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5892 &num_clusters, el, &ext_flags);
Joseph Qi17caf952013-09-11 14:19:55 -07005893 if (ret) {
5894 mlog_errno(ret);
5895 break;
5896 }
Tao Ma01292412009-09-21 13:04:19 +08005897
5898 cpos += num_clusters;
5899 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5900 continue;
5901
5902 BUG_ON(!p_cluster);
5903
5904 ret = ocfs2_add_refcount_flag(inode, value_et,
5905 ref_ci, ref_root_bh,
5906 cpos - num_clusters,
5907 p_cluster, num_clusters,
5908 dealloc, refcount);
5909 if (ret) {
5910 mlog_errno(ret);
5911 break;
5912 }
5913 }
5914
5915 return ret;
5916}
5917
5918/*
5919 * Given a normal ocfs2_xattr_header, refcount all the entries which
5920 * have value stored outside.
5921 * Used for xattrs stored in inode and ocfs2_xattr_block.
5922 */
5923static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5924 struct ocfs2_xattr_value_buf *vb,
5925 struct ocfs2_xattr_header *header,
5926 struct ocfs2_caching_info *ref_ci,
5927 struct buffer_head *ref_root_bh,
5928 struct ocfs2_cached_dealloc_ctxt *dealloc)
5929{
5930
5931 struct ocfs2_xattr_entry *xe;
5932 struct ocfs2_xattr_value_root *xv;
5933 struct ocfs2_extent_tree et;
5934 int i, ret = 0;
5935
5936 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5937 xe = &header->xh_entries[i];
5938
5939 if (ocfs2_xattr_is_local(xe))
5940 continue;
5941
5942 xv = (struct ocfs2_xattr_value_root *)((void *)header +
5943 le16_to_cpu(xe->xe_name_offset) +
5944 OCFS2_XATTR_SIZE(xe->xe_name_len));
5945
5946 vb->vb_xv = xv;
5947 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5948
5949 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5950 ref_ci, ref_root_bh,
5951 dealloc, NULL);
5952 if (ret) {
5953 mlog_errno(ret);
5954 break;
5955 }
5956 }
5957
5958 return ret;
5959}
5960
5961static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5962 struct buffer_head *fe_bh,
5963 struct ocfs2_caching_info *ref_ci,
5964 struct buffer_head *ref_root_bh,
5965 struct ocfs2_cached_dealloc_ctxt *dealloc)
5966{
5967 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5968 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5969 (fe_bh->b_data + inode->i_sb->s_blocksize -
5970 le16_to_cpu(di->i_xattr_inline_size));
5971 struct ocfs2_xattr_value_buf vb = {
5972 .vb_bh = fe_bh,
5973 .vb_access = ocfs2_journal_access_di,
5974 };
5975
5976 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5977 ref_ci, ref_root_bh, dealloc);
5978}
5979
5980struct ocfs2_xattr_tree_value_refcount_para {
5981 struct ocfs2_caching_info *ref_ci;
5982 struct buffer_head *ref_root_bh;
5983 struct ocfs2_cached_dealloc_ctxt *dealloc;
5984};
5985
5986static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
5987 struct ocfs2_xattr_bucket *bucket,
5988 int offset,
5989 struct ocfs2_xattr_value_root **xv,
5990 struct buffer_head **bh)
5991{
5992 int ret, block_off, name_offset;
5993 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5994 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
5995 void *base;
5996
5997 ret = ocfs2_xattr_bucket_get_name_value(sb,
5998 bucket_xh(bucket),
5999 offset,
6000 &block_off,
6001 &name_offset);
6002 if (ret) {
6003 mlog_errno(ret);
6004 goto out;
6005 }
6006
6007 base = bucket_block(bucket, block_off);
6008
6009 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
6010 OCFS2_XATTR_SIZE(xe->xe_name_len));
6011
6012 if (bh)
6013 *bh = bucket->bu_bhs[block_off];
6014out:
6015 return ret;
6016}
6017
6018/*
6019 * For a given xattr bucket, refcount all the entries which
6020 * have value stored outside.
6021 */
6022static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
6023 struct ocfs2_xattr_bucket *bucket,
6024 void *para)
6025{
6026 int i, ret = 0;
6027 struct ocfs2_extent_tree et;
6028 struct ocfs2_xattr_tree_value_refcount_para *ref =
6029 (struct ocfs2_xattr_tree_value_refcount_para *)para;
6030 struct ocfs2_xattr_header *xh =
6031 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6032 struct ocfs2_xattr_entry *xe;
6033 struct ocfs2_xattr_value_buf vb = {
6034 .vb_access = ocfs2_journal_access,
6035 };
6036 struct ocfs2_post_refcount refcount = {
6037 .credits = bucket->bu_blocks,
6038 .para = bucket,
6039 .func = ocfs2_xattr_bucket_post_refcount,
6040 };
6041 struct ocfs2_post_refcount *p = NULL;
6042
6043 /* We only need post_refcount if we support metaecc. */
6044 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6045 p = &refcount;
6046
Tao Ma402b4182011-02-23 22:01:17 +08006047 trace_ocfs2_xattr_bucket_value_refcount(
6048 (unsigned long long)bucket_blkno(bucket),
6049 le16_to_cpu(xh->xh_count));
Tao Ma01292412009-09-21 13:04:19 +08006050 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6051 xe = &xh->xh_entries[i];
6052
6053 if (ocfs2_xattr_is_local(xe))
6054 continue;
6055
6056 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6057 &vb.vb_xv, &vb.vb_bh);
6058 if (ret) {
6059 mlog_errno(ret);
6060 break;
6061 }
6062
6063 ocfs2_init_xattr_value_extent_tree(&et,
6064 INODE_CACHE(inode), &vb);
6065
6066 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6067 &et, ref->ref_ci,
6068 ref->ref_root_bh,
6069 ref->dealloc, p);
6070 if (ret) {
6071 mlog_errno(ret);
6072 break;
6073 }
6074 }
6075
6076 return ret;
6077
6078}
6079
6080static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6081 struct buffer_head *root_bh,
6082 u64 blkno, u32 cpos, u32 len, void *para)
6083{
6084 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6085 ocfs2_xattr_bucket_value_refcount,
6086 para);
6087}
6088
6089static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6090 struct buffer_head *blk_bh,
6091 struct ocfs2_caching_info *ref_ci,
6092 struct buffer_head *ref_root_bh,
6093 struct ocfs2_cached_dealloc_ctxt *dealloc)
6094{
6095 int ret = 0;
6096 struct ocfs2_xattr_block *xb =
6097 (struct ocfs2_xattr_block *)blk_bh->b_data;
6098
6099 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6100 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6101 struct ocfs2_xattr_value_buf vb = {
6102 .vb_bh = blk_bh,
6103 .vb_access = ocfs2_journal_access_xb,
6104 };
6105
6106 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6107 ref_ci, ref_root_bh,
6108 dealloc);
6109 } else {
6110 struct ocfs2_xattr_tree_value_refcount_para para = {
6111 .ref_ci = ref_ci,
6112 .ref_root_bh = ref_root_bh,
6113 .dealloc = dealloc,
6114 };
6115
6116 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6117 ocfs2_refcount_xattr_tree_rec,
6118 &para);
6119 }
6120
6121 return ret;
6122}
6123
6124int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6125 struct buffer_head *fe_bh,
6126 struct ocfs2_caching_info *ref_ci,
6127 struct buffer_head *ref_root_bh,
6128 struct ocfs2_cached_dealloc_ctxt *dealloc)
6129{
6130 int ret = 0;
6131 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6132 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6133 struct buffer_head *blk_bh = NULL;
6134
6135 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6136 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6137 ref_ci, ref_root_bh,
6138 dealloc);
6139 if (ret) {
6140 mlog_errno(ret);
6141 goto out;
6142 }
6143 }
6144
6145 if (!di->i_xattr_loc)
6146 goto out;
6147
6148 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6149 &blk_bh);
6150 if (ret < 0) {
6151 mlog_errno(ret);
6152 goto out;
6153 }
6154
6155 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6156 ref_root_bh, dealloc);
6157 if (ret)
6158 mlog_errno(ret);
6159
6160 brelse(blk_bh);
6161out:
6162
6163 return ret;
6164}
6165
Tao Ma0fe9b662009-08-18 11:47:56 +08006166typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
Tao Ma01292412009-09-21 13:04:19 +08006167/*
Tao Ma2999d122009-08-18 11:43:55 +08006168 * Store the information we need in xattr reflink.
6169 * old_bh and new_bh are inode bh for the old and new inode.
6170 */
6171struct ocfs2_xattr_reflink {
6172 struct inode *old_inode;
6173 struct inode *new_inode;
6174 struct buffer_head *old_bh;
6175 struct buffer_head *new_bh;
6176 struct ocfs2_caching_info *ref_ci;
6177 struct buffer_head *ref_root_bh;
6178 struct ocfs2_cached_dealloc_ctxt *dealloc;
Tao Ma0fe9b662009-08-18 11:47:56 +08006179 should_xattr_reflinked *xattr_reflinked;
Tao Ma2999d122009-08-18 11:43:55 +08006180};
6181
6182/*
6183 * Given a xattr header and xe offset,
6184 * return the proper xv and the corresponding bh.
6185 * xattr in inode, block and xattr tree have different implementaions.
6186 */
6187typedef int (get_xattr_value_root)(struct super_block *sb,
6188 struct buffer_head *bh,
6189 struct ocfs2_xattr_header *xh,
6190 int offset,
6191 struct ocfs2_xattr_value_root **xv,
6192 struct buffer_head **ret_bh,
6193 void *para);
6194
6195/*
6196 * Calculate all the xattr value root metadata stored in this xattr header and
6197 * credits we need if we create them from the scratch.
6198 * We use get_xattr_value_root so that all types of xattr container can use it.
6199 */
6200static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6201 struct buffer_head *bh,
6202 struct ocfs2_xattr_header *xh,
6203 int *metas, int *credits,
6204 int *num_recs,
6205 get_xattr_value_root *func,
6206 void *para)
6207{
6208 int i, ret = 0;
6209 struct ocfs2_xattr_value_root *xv;
6210 struct ocfs2_xattr_entry *xe;
6211
6212 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6213 xe = &xh->xh_entries[i];
6214 if (ocfs2_xattr_is_local(xe))
6215 continue;
6216
6217 ret = func(sb, bh, xh, i, &xv, NULL, para);
6218 if (ret) {
6219 mlog_errno(ret);
6220 break;
6221 }
6222
6223 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6224 le16_to_cpu(xv->xr_list.l_next_free_rec);
6225
6226 *credits += ocfs2_calc_extend_credits(sb,
Goldwyn Rodrigues06f9da62013-11-12 15:06:52 -08006227 &def_xv.xv.xr_list);
Tao Ma2999d122009-08-18 11:43:55 +08006228
6229 /*
6230 * If the value is a tree with depth > 1, We don't go deep
6231 * to the extent block, so just calculate a maximum record num.
6232 */
6233 if (!xv->xr_list.l_tree_depth)
Tao Ma8ff6af82009-12-23 14:31:15 +08006234 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
Tao Ma2999d122009-08-18 11:43:55 +08006235 else
6236 *num_recs += ocfs2_clusters_for_bytes(sb,
6237 XATTR_SIZE_MAX);
6238 }
6239
6240 return ret;
6241}
6242
6243/* Used by xattr inode and block to return the right xv and buffer_head. */
6244static int ocfs2_get_xattr_value_root(struct super_block *sb,
6245 struct buffer_head *bh,
6246 struct ocfs2_xattr_header *xh,
6247 int offset,
6248 struct ocfs2_xattr_value_root **xv,
6249 struct buffer_head **ret_bh,
6250 void *para)
6251{
6252 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6253
6254 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6255 le16_to_cpu(xe->xe_name_offset) +
6256 OCFS2_XATTR_SIZE(xe->xe_name_len));
6257
6258 if (ret_bh)
6259 *ret_bh = bh;
6260
6261 return 0;
6262}
6263
6264/*
6265 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6266 * It is only used for inline xattr and xattr block.
6267 */
6268static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6269 struct ocfs2_xattr_header *xh,
6270 struct buffer_head *ref_root_bh,
6271 int *credits,
6272 struct ocfs2_alloc_context **meta_ac)
6273{
6274 int ret, meta_add = 0, num_recs = 0;
6275 struct ocfs2_refcount_block *rb =
6276 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6277
6278 *credits = 0;
6279
6280 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6281 &meta_add, credits, &num_recs,
6282 ocfs2_get_xattr_value_root,
6283 NULL);
6284 if (ret) {
6285 mlog_errno(ret);
6286 goto out;
6287 }
6288
6289 /*
6290 * We need to add/modify num_recs in refcount tree, so just calculate
6291 * an approximate number we need for refcount tree change.
6292 * Sometimes we need to split the tree, and after split, half recs
6293 * will be moved to the new block, and a new block can only provide
6294 * half number of recs. So we multiple new blocks by 2.
6295 */
6296 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6297 meta_add += num_recs;
6298 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6299 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6300 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6301 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6302 else
6303 *credits += 1;
6304
6305 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6306 if (ret)
6307 mlog_errno(ret);
6308
6309out:
6310 return ret;
6311}
6312
6313/*
6314 * Given a xattr header, reflink all the xattrs in this container.
6315 * It can be used for inode, block and bucket.
6316 *
6317 * NOTE:
6318 * Before we call this function, the caller has memcpy the xattr in
6319 * old_xh to the new_xh.
Tao Ma0fe9b662009-08-18 11:47:56 +08006320 *
6321 * If args.xattr_reflinked is set, call it to decide whether the xe should
6322 * be reflinked or not. If not, remove it from the new xattr header.
Tao Ma2999d122009-08-18 11:43:55 +08006323 */
6324static int ocfs2_reflink_xattr_header(handle_t *handle,
6325 struct ocfs2_xattr_reflink *args,
6326 struct buffer_head *old_bh,
6327 struct ocfs2_xattr_header *xh,
6328 struct buffer_head *new_bh,
6329 struct ocfs2_xattr_header *new_xh,
6330 struct ocfs2_xattr_value_buf *vb,
6331 struct ocfs2_alloc_context *meta_ac,
6332 get_xattr_value_root *func,
6333 void *para)
6334{
Tao Ma0fe9b662009-08-18 11:47:56 +08006335 int ret = 0, i, j;
Tao Ma2999d122009-08-18 11:43:55 +08006336 struct super_block *sb = args->old_inode->i_sb;
6337 struct buffer_head *value_bh;
Tao Ma0fe9b662009-08-18 11:47:56 +08006338 struct ocfs2_xattr_entry *xe, *last;
Tao Ma2999d122009-08-18 11:43:55 +08006339 struct ocfs2_xattr_value_root *xv, *new_xv;
6340 struct ocfs2_extent_tree data_et;
6341 u32 clusters, cpos, p_cluster, num_clusters;
6342 unsigned int ext_flags = 0;
6343
Tao Ma402b4182011-02-23 22:01:17 +08006344 trace_ocfs2_reflink_xattr_header((unsigned long long)old_bh->b_blocknr,
6345 le16_to_cpu(xh->xh_count));
Tao Ma0fe9b662009-08-18 11:47:56 +08006346
6347 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6348 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
Tao Ma2999d122009-08-18 11:43:55 +08006349 xe = &xh->xh_entries[i];
6350
Tao Ma0fe9b662009-08-18 11:47:56 +08006351 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6352 xe = &new_xh->xh_entries[j];
6353
6354 le16_add_cpu(&new_xh->xh_count, -1);
6355 if (new_xh->xh_count) {
6356 memmove(xe, xe + 1,
6357 (void *)last - (void *)xe);
6358 memset(last, 0,
6359 sizeof(struct ocfs2_xattr_entry));
6360 }
6361
6362 /*
6363 * We don't want j to increase in the next round since
6364 * it is already moved ahead.
6365 */
6366 j--;
6367 continue;
6368 }
6369
Tao Ma2999d122009-08-18 11:43:55 +08006370 if (ocfs2_xattr_is_local(xe))
6371 continue;
6372
6373 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6374 if (ret) {
6375 mlog_errno(ret);
6376 break;
6377 }
6378
Tao Ma0fe9b662009-08-18 11:47:56 +08006379 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
Tao Ma2999d122009-08-18 11:43:55 +08006380 if (ret) {
6381 mlog_errno(ret);
6382 break;
6383 }
6384
6385 /*
6386 * For the xattr which has l_tree_depth = 0, all the extent
6387 * recs have already be copied to the new xh with the
6388 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6389 * increase the refount count int the refcount tree.
6390 *
6391 * For the xattr which has l_tree_depth > 0, we need
6392 * to initialize it to the empty default value root,
6393 * and then insert the extents one by one.
6394 */
6395 if (xv->xr_list.l_tree_depth) {
6396 memcpy(new_xv, &def_xv, sizeof(def_xv));
6397 vb->vb_xv = new_xv;
6398 vb->vb_bh = value_bh;
6399 ocfs2_init_xattr_value_extent_tree(&data_et,
6400 INODE_CACHE(args->new_inode), vb);
6401 }
6402
6403 clusters = le32_to_cpu(xv->xr_clusters);
6404 cpos = 0;
6405 while (cpos < clusters) {
6406 ret = ocfs2_xattr_get_clusters(args->old_inode,
6407 cpos,
6408 &p_cluster,
6409 &num_clusters,
6410 &xv->xr_list,
6411 &ext_flags);
6412 if (ret) {
6413 mlog_errno(ret);
6414 goto out;
6415 }
6416
6417 BUG_ON(!p_cluster);
6418
6419 if (xv->xr_list.l_tree_depth) {
6420 ret = ocfs2_insert_extent(handle,
6421 &data_et, cpos,
6422 ocfs2_clusters_to_blocks(
6423 args->old_inode->i_sb,
6424 p_cluster),
6425 num_clusters, ext_flags,
6426 meta_ac);
6427 if (ret) {
6428 mlog_errno(ret);
6429 goto out;
6430 }
6431 }
6432
6433 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6434 args->ref_root_bh,
6435 p_cluster, num_clusters,
6436 meta_ac, args->dealloc);
6437 if (ret) {
6438 mlog_errno(ret);
6439 goto out;
6440 }
6441
6442 cpos += num_clusters;
6443 }
6444 }
6445
6446out:
6447 return ret;
6448}
6449
6450static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6451{
6452 int ret = 0, credits = 0;
6453 handle_t *handle;
6454 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6455 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6456 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6457 int header_off = osb->sb->s_blocksize - inline_size;
6458 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6459 (args->old_bh->b_data + header_off);
6460 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6461 (args->new_bh->b_data + header_off);
6462 struct ocfs2_alloc_context *meta_ac = NULL;
6463 struct ocfs2_inode_info *new_oi;
6464 struct ocfs2_dinode *new_di;
6465 struct ocfs2_xattr_value_buf vb = {
6466 .vb_bh = args->new_bh,
6467 .vb_access = ocfs2_journal_access_di,
6468 };
6469
6470 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6471 &credits, &meta_ac);
6472 if (ret) {
6473 mlog_errno(ret);
6474 goto out;
6475 }
6476
6477 handle = ocfs2_start_trans(osb, credits);
6478 if (IS_ERR(handle)) {
6479 ret = PTR_ERR(handle);
6480 mlog_errno(ret);
6481 goto out;
6482 }
6483
6484 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6485 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6486 if (ret) {
6487 mlog_errno(ret);
6488 goto out_commit;
6489 }
6490
6491 memcpy(args->new_bh->b_data + header_off,
6492 args->old_bh->b_data + header_off, inline_size);
6493
6494 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6495 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6496
6497 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6498 args->new_bh, new_xh, &vb, meta_ac,
6499 ocfs2_get_xattr_value_root, NULL);
6500 if (ret) {
6501 mlog_errno(ret);
6502 goto out_commit;
6503 }
6504
6505 new_oi = OCFS2_I(args->new_inode);
Junxiao Bief962df2013-07-03 15:01:03 -07006506 /*
6507 * Adjust extent record count to reserve space for extended attribute.
6508 * Inline data count had been adjusted in ocfs2_duplicate_inline_data().
6509 */
6510 if (!(new_oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) &&
6511 !(ocfs2_inode_is_fast_symlink(args->new_inode))) {
6512 struct ocfs2_extent_list *el = &new_di->id2.i_list;
6513 le16_add_cpu(&el->l_count, -(inline_size /
6514 sizeof(struct ocfs2_extent_rec)));
6515 }
Tao Ma2999d122009-08-18 11:43:55 +08006516 spin_lock(&new_oi->ip_lock);
6517 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6518 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6519 spin_unlock(&new_oi->ip_lock);
6520
6521 ocfs2_journal_dirty(handle, args->new_bh);
6522
6523out_commit:
6524 ocfs2_commit_trans(osb, handle);
6525
6526out:
6527 if (meta_ac)
6528 ocfs2_free_alloc_context(meta_ac);
6529 return ret;
6530}
6531
6532static int ocfs2_create_empty_xattr_block(struct inode *inode,
6533 struct buffer_head *fe_bh,
6534 struct buffer_head **ret_bh,
6535 int indexed)
6536{
6537 int ret;
Tao Ma2999d122009-08-18 11:43:55 +08006538 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Mab2317962010-03-19 15:04:24 +08006539 struct ocfs2_xattr_set_ctxt ctxt;
Tao Ma2999d122009-08-18 11:43:55 +08006540
Tao Mab2317962010-03-19 15:04:24 +08006541 memset(&ctxt, 0, sizeof(ctxt));
6542 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &ctxt.meta_ac);
Tao Ma2999d122009-08-18 11:43:55 +08006543 if (ret < 0) {
6544 mlog_errno(ret);
6545 return ret;
6546 }
6547
Joel Beckerd3981542009-08-19 02:13:50 -07006548 ctxt.handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6549 if (IS_ERR(ctxt.handle)) {
6550 ret = PTR_ERR(ctxt.handle);
Tao Ma2999d122009-08-18 11:43:55 +08006551 mlog_errno(ret);
6552 goto out;
6553 }
6554
Tao Ma402b4182011-02-23 22:01:17 +08006555 trace_ocfs2_create_empty_xattr_block(
6556 (unsigned long long)fe_bh->b_blocknr, indexed);
Joel Beckerd3981542009-08-19 02:13:50 -07006557 ret = ocfs2_create_xattr_block(inode, fe_bh, &ctxt, indexed,
6558 ret_bh);
Tao Ma2999d122009-08-18 11:43:55 +08006559 if (ret)
6560 mlog_errno(ret);
6561
Joel Beckerd3981542009-08-19 02:13:50 -07006562 ocfs2_commit_trans(osb, ctxt.handle);
Tao Ma2999d122009-08-18 11:43:55 +08006563out:
Tao Mab2317962010-03-19 15:04:24 +08006564 ocfs2_free_alloc_context(ctxt.meta_ac);
Tao Ma2999d122009-08-18 11:43:55 +08006565 return ret;
6566}
6567
6568static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6569 struct buffer_head *blk_bh,
6570 struct buffer_head *new_blk_bh)
6571{
6572 int ret = 0, credits = 0;
6573 handle_t *handle;
6574 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6575 struct ocfs2_dinode *new_di;
6576 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6577 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6578 struct ocfs2_xattr_block *xb =
6579 (struct ocfs2_xattr_block *)blk_bh->b_data;
6580 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6581 struct ocfs2_xattr_block *new_xb =
6582 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6583 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6584 struct ocfs2_alloc_context *meta_ac;
6585 struct ocfs2_xattr_value_buf vb = {
6586 .vb_bh = new_blk_bh,
6587 .vb_access = ocfs2_journal_access_xb,
6588 };
6589
6590 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6591 &credits, &meta_ac);
6592 if (ret) {
6593 mlog_errno(ret);
6594 return ret;
6595 }
6596
6597 /* One more credits in case we need to add xattr flags in new inode. */
6598 handle = ocfs2_start_trans(osb, credits + 1);
6599 if (IS_ERR(handle)) {
6600 ret = PTR_ERR(handle);
6601 mlog_errno(ret);
6602 goto out;
6603 }
6604
6605 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6606 ret = ocfs2_journal_access_di(handle,
6607 INODE_CACHE(args->new_inode),
6608 args->new_bh,
6609 OCFS2_JOURNAL_ACCESS_WRITE);
6610 if (ret) {
6611 mlog_errno(ret);
6612 goto out_commit;
6613 }
6614 }
6615
6616 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6617 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6618 if (ret) {
6619 mlog_errno(ret);
6620 goto out_commit;
6621 }
6622
6623 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6624 osb->sb->s_blocksize - header_off);
6625
6626 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6627 new_blk_bh, new_xh, &vb, meta_ac,
6628 ocfs2_get_xattr_value_root, NULL);
6629 if (ret) {
6630 mlog_errno(ret);
6631 goto out_commit;
6632 }
6633
6634 ocfs2_journal_dirty(handle, new_blk_bh);
6635
6636 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6637 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6638 spin_lock(&new_oi->ip_lock);
6639 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6640 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6641 spin_unlock(&new_oi->ip_lock);
6642
6643 ocfs2_journal_dirty(handle, args->new_bh);
6644 }
6645
6646out_commit:
6647 ocfs2_commit_trans(osb, handle);
6648
6649out:
6650 ocfs2_free_alloc_context(meta_ac);
6651 return ret;
6652}
6653
6654struct ocfs2_reflink_xattr_tree_args {
6655 struct ocfs2_xattr_reflink *reflink;
6656 struct buffer_head *old_blk_bh;
6657 struct buffer_head *new_blk_bh;
6658 struct ocfs2_xattr_bucket *old_bucket;
6659 struct ocfs2_xattr_bucket *new_bucket;
6660};
6661
6662/*
6663 * NOTE:
6664 * We have to handle the case that both old bucket and new bucket
6665 * will call this function to get the right ret_bh.
6666 * So The caller must give us the right bh.
6667 */
6668static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6669 struct buffer_head *bh,
6670 struct ocfs2_xattr_header *xh,
6671 int offset,
6672 struct ocfs2_xattr_value_root **xv,
6673 struct buffer_head **ret_bh,
6674 void *para)
6675{
6676 struct ocfs2_reflink_xattr_tree_args *args =
6677 (struct ocfs2_reflink_xattr_tree_args *)para;
6678 struct ocfs2_xattr_bucket *bucket;
6679
6680 if (bh == args->old_bucket->bu_bhs[0])
6681 bucket = args->old_bucket;
6682 else
6683 bucket = args->new_bucket;
6684
6685 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6686 xv, ret_bh);
6687}
6688
6689struct ocfs2_value_tree_metas {
6690 int num_metas;
6691 int credits;
6692 int num_recs;
6693};
6694
6695static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6696 struct buffer_head *bh,
6697 struct ocfs2_xattr_header *xh,
6698 int offset,
6699 struct ocfs2_xattr_value_root **xv,
6700 struct buffer_head **ret_bh,
6701 void *para)
6702{
6703 struct ocfs2_xattr_bucket *bucket =
6704 (struct ocfs2_xattr_bucket *)para;
6705
6706 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6707 xv, ret_bh);
6708}
6709
6710static int ocfs2_calc_value_tree_metas(struct inode *inode,
6711 struct ocfs2_xattr_bucket *bucket,
6712 void *para)
6713{
6714 struct ocfs2_value_tree_metas *metas =
6715 (struct ocfs2_value_tree_metas *)para;
6716 struct ocfs2_xattr_header *xh =
6717 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6718
6719 /* Add the credits for this bucket first. */
6720 metas->credits += bucket->bu_blocks;
6721 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6722 xh, &metas->num_metas,
6723 &metas->credits, &metas->num_recs,
6724 ocfs2_value_tree_metas_in_bucket,
6725 bucket);
6726}
6727
6728/*
6729 * Given a xattr extent rec starting from blkno and having len clusters,
6730 * iterate all the buckets calculate how much metadata we need for reflinking
6731 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6732 */
6733static int ocfs2_lock_reflink_xattr_rec_allocators(
6734 struct ocfs2_reflink_xattr_tree_args *args,
6735 struct ocfs2_extent_tree *xt_et,
6736 u64 blkno, u32 len, int *credits,
6737 struct ocfs2_alloc_context **meta_ac,
6738 struct ocfs2_alloc_context **data_ac)
6739{
6740 int ret, num_free_extents;
6741 struct ocfs2_value_tree_metas metas;
6742 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6743 struct ocfs2_refcount_block *rb;
6744
6745 memset(&metas, 0, sizeof(metas));
6746
6747 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6748 ocfs2_calc_value_tree_metas, &metas);
6749 if (ret) {
6750 mlog_errno(ret);
6751 goto out;
6752 }
6753
6754 *credits = metas.credits;
6755
6756 /*
6757 * Calculate we need for refcount tree change.
6758 *
6759 * We need to add/modify num_recs in refcount tree, so just calculate
6760 * an approximate number we need for refcount tree change.
6761 * Sometimes we need to split the tree, and after split, half recs
6762 * will be moved to the new block, and a new block can only provide
6763 * half number of recs. So we multiple new blocks by 2.
6764 * In the end, we have to add credits for modifying the already
6765 * existed refcount block.
6766 */
6767 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6768 metas.num_recs =
6769 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6770 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6771 metas.num_metas += metas.num_recs;
6772 *credits += metas.num_recs +
6773 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6774 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6775 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6776 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6777 else
6778 *credits += 1;
6779
6780 /* count in the xattr tree change. */
6781 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6782 if (num_free_extents < 0) {
6783 ret = num_free_extents;
6784 mlog_errno(ret);
6785 goto out;
6786 }
6787
6788 if (num_free_extents < len)
6789 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6790
6791 *credits += ocfs2_calc_extend_credits(osb->sb,
Goldwyn Rodrigues06f9da62013-11-12 15:06:52 -08006792 xt_et->et_root_el);
Tao Ma2999d122009-08-18 11:43:55 +08006793
6794 if (metas.num_metas) {
6795 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6796 meta_ac);
6797 if (ret) {
6798 mlog_errno(ret);
6799 goto out;
6800 }
6801 }
6802
6803 if (len) {
6804 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6805 if (ret)
6806 mlog_errno(ret);
6807 }
6808out:
6809 if (ret) {
6810 if (*meta_ac) {
6811 ocfs2_free_alloc_context(*meta_ac);
Joseph Qi6cae6d32013-09-11 14:19:58 -07006812 *meta_ac = NULL;
Tao Ma2999d122009-08-18 11:43:55 +08006813 }
6814 }
6815
6816 return ret;
6817}
6818
Tao Ma121a39b2010-07-09 14:53:12 +08006819static int ocfs2_reflink_xattr_bucket(handle_t *handle,
Tao Ma2999d122009-08-18 11:43:55 +08006820 u64 blkno, u64 new_blkno, u32 clusters,
Tao Ma121a39b2010-07-09 14:53:12 +08006821 u32 *cpos, int num_buckets,
Tao Ma2999d122009-08-18 11:43:55 +08006822 struct ocfs2_alloc_context *meta_ac,
6823 struct ocfs2_alloc_context *data_ac,
6824 struct ocfs2_reflink_xattr_tree_args *args)
6825{
6826 int i, j, ret = 0;
6827 struct super_block *sb = args->reflink->old_inode->i_sb;
Tao Ma2999d122009-08-18 11:43:55 +08006828 int bpb = args->old_bucket->bu_blocks;
6829 struct ocfs2_xattr_value_buf vb = {
6830 .vb_access = ocfs2_journal_access,
6831 };
6832
6833 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6834 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6835 if (ret) {
6836 mlog_errno(ret);
6837 break;
6838 }
6839
Wengang Wang9c339252014-04-03 14:47:15 -07006840 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno, 1);
Tao Ma2999d122009-08-18 11:43:55 +08006841 if (ret) {
6842 mlog_errno(ret);
6843 break;
6844 }
6845
Tao Ma2999d122009-08-18 11:43:55 +08006846 ret = ocfs2_xattr_bucket_journal_access(handle,
6847 args->new_bucket,
6848 OCFS2_JOURNAL_ACCESS_CREATE);
6849 if (ret) {
6850 mlog_errno(ret);
6851 break;
6852 }
6853
6854 for (j = 0; j < bpb; j++)
6855 memcpy(bucket_block(args->new_bucket, j),
6856 bucket_block(args->old_bucket, j),
6857 sb->s_blocksize);
6858
Tao Ma121a39b2010-07-09 14:53:12 +08006859 /*
6860 * Record the start cpos so that we can use it to initialize
6861 * our xattr tree we also set the xh_num_bucket for the new
6862 * bucket.
6863 */
6864 if (i == 0) {
6865 *cpos = le32_to_cpu(bucket_xh(args->new_bucket)->
6866 xh_entries[0].xe_name_hash);
6867 bucket_xh(args->new_bucket)->xh_num_buckets =
6868 cpu_to_le16(num_buckets);
6869 }
6870
Tao Ma2999d122009-08-18 11:43:55 +08006871 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6872
6873 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6874 args->old_bucket->bu_bhs[0],
6875 bucket_xh(args->old_bucket),
6876 args->new_bucket->bu_bhs[0],
6877 bucket_xh(args->new_bucket),
6878 &vb, meta_ac,
6879 ocfs2_get_reflink_xattr_value_root,
6880 args);
6881 if (ret) {
6882 mlog_errno(ret);
6883 break;
6884 }
6885
6886 /*
6887 * Re-access and dirty the bucket to calculate metaecc.
6888 * Because we may extend the transaction in reflink_xattr_header
6889 * which will let the already accessed block gone.
6890 */
6891 ret = ocfs2_xattr_bucket_journal_access(handle,
6892 args->new_bucket,
6893 OCFS2_JOURNAL_ACCESS_WRITE);
6894 if (ret) {
6895 mlog_errno(ret);
6896 break;
6897 }
6898
6899 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
Tao Ma121a39b2010-07-09 14:53:12 +08006900
Tao Ma2999d122009-08-18 11:43:55 +08006901 ocfs2_xattr_bucket_relse(args->old_bucket);
6902 ocfs2_xattr_bucket_relse(args->new_bucket);
6903 }
6904
6905 ocfs2_xattr_bucket_relse(args->old_bucket);
6906 ocfs2_xattr_bucket_relse(args->new_bucket);
6907 return ret;
6908}
Tao Ma121a39b2010-07-09 14:53:12 +08006909
6910static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6911 struct inode *inode,
6912 struct ocfs2_reflink_xattr_tree_args *args,
6913 struct ocfs2_extent_tree *et,
6914 struct ocfs2_alloc_context *meta_ac,
6915 struct ocfs2_alloc_context *data_ac,
6916 u64 blkno, u32 cpos, u32 len)
6917{
6918 int ret, first_inserted = 0;
6919 u32 p_cluster, num_clusters, reflink_cpos = 0;
6920 u64 new_blkno;
6921 unsigned int num_buckets, reflink_buckets;
6922 unsigned int bpc =
6923 ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
6924
6925 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6926 if (ret) {
6927 mlog_errno(ret);
6928 goto out;
6929 }
6930 num_buckets = le16_to_cpu(bucket_xh(args->old_bucket)->xh_num_buckets);
6931 ocfs2_xattr_bucket_relse(args->old_bucket);
6932
6933 while (len && num_buckets) {
6934 ret = ocfs2_claim_clusters(handle, data_ac,
6935 1, &p_cluster, &num_clusters);
6936 if (ret) {
6937 mlog_errno(ret);
6938 goto out;
6939 }
6940
6941 new_blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
6942 reflink_buckets = min(num_buckets, bpc * num_clusters);
6943
6944 ret = ocfs2_reflink_xattr_bucket(handle, blkno,
6945 new_blkno, num_clusters,
6946 &reflink_cpos, reflink_buckets,
6947 meta_ac, data_ac, args);
6948 if (ret) {
6949 mlog_errno(ret);
6950 goto out;
6951 }
6952
6953 /*
6954 * For the 1st allocated cluster, we make it use the same cpos
6955 * so that the xattr tree looks the same as the original one
6956 * in the most case.
6957 */
6958 if (!first_inserted) {
6959 reflink_cpos = cpos;
6960 first_inserted = 1;
6961 }
6962 ret = ocfs2_insert_extent(handle, et, reflink_cpos, new_blkno,
6963 num_clusters, 0, meta_ac);
6964 if (ret)
6965 mlog_errno(ret);
6966
Tao Ma402b4182011-02-23 22:01:17 +08006967 trace_ocfs2_reflink_xattr_buckets((unsigned long long)new_blkno,
6968 num_clusters, reflink_cpos);
Tao Ma121a39b2010-07-09 14:53:12 +08006969
6970 len -= num_clusters;
6971 blkno += ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
6972 num_buckets -= reflink_buckets;
6973 }
6974out:
6975 return ret;
6976}
6977
Tao Ma2999d122009-08-18 11:43:55 +08006978/*
6979 * Create the same xattr extent record in the new inode's xattr tree.
6980 */
6981static int ocfs2_reflink_xattr_rec(struct inode *inode,
6982 struct buffer_head *root_bh,
6983 u64 blkno,
6984 u32 cpos,
6985 u32 len,
6986 void *para)
6987{
6988 int ret, credits = 0;
Tao Ma2999d122009-08-18 11:43:55 +08006989 handle_t *handle;
6990 struct ocfs2_reflink_xattr_tree_args *args =
6991 (struct ocfs2_reflink_xattr_tree_args *)para;
6992 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6993 struct ocfs2_alloc_context *meta_ac = NULL;
6994 struct ocfs2_alloc_context *data_ac = NULL;
6995 struct ocfs2_extent_tree et;
6996
Tao Ma402b4182011-02-23 22:01:17 +08006997 trace_ocfs2_reflink_xattr_rec((unsigned long long)blkno, len);
Tao Ma121a39b2010-07-09 14:53:12 +08006998
Tao Ma2999d122009-08-18 11:43:55 +08006999 ocfs2_init_xattr_tree_extent_tree(&et,
7000 INODE_CACHE(args->reflink->new_inode),
7001 args->new_blk_bh);
7002
7003 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
7004 len, &credits,
7005 &meta_ac, &data_ac);
7006 if (ret) {
7007 mlog_errno(ret);
7008 goto out;
7009 }
7010
7011 handle = ocfs2_start_trans(osb, credits);
7012 if (IS_ERR(handle)) {
7013 ret = PTR_ERR(handle);
7014 mlog_errno(ret);
7015 goto out;
7016 }
7017
Tao Ma121a39b2010-07-09 14:53:12 +08007018 ret = ocfs2_reflink_xattr_buckets(handle, inode, args, &et,
7019 meta_ac, data_ac,
7020 blkno, cpos, len);
Tao Ma2999d122009-08-18 11:43:55 +08007021 if (ret)
7022 mlog_errno(ret);
7023
Tao Ma2999d122009-08-18 11:43:55 +08007024 ocfs2_commit_trans(osb, handle);
7025
7026out:
7027 if (meta_ac)
7028 ocfs2_free_alloc_context(meta_ac);
7029 if (data_ac)
7030 ocfs2_free_alloc_context(data_ac);
7031 return ret;
7032}
7033
7034/*
7035 * Create reflinked xattr buckets.
7036 * We will add bucket one by one, and refcount all the xattrs in the bucket
7037 * if they are stored outside.
7038 */
7039static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
7040 struct buffer_head *blk_bh,
7041 struct buffer_head *new_blk_bh)
7042{
7043 int ret;
7044 struct ocfs2_reflink_xattr_tree_args para;
7045
7046 memset(&para, 0, sizeof(para));
7047 para.reflink = args;
7048 para.old_blk_bh = blk_bh;
7049 para.new_blk_bh = new_blk_bh;
7050
7051 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
7052 if (!para.old_bucket) {
7053 mlog_errno(-ENOMEM);
7054 return -ENOMEM;
7055 }
7056
7057 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
7058 if (!para.new_bucket) {
7059 ret = -ENOMEM;
7060 mlog_errno(ret);
7061 goto out;
7062 }
7063
7064 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
7065 ocfs2_reflink_xattr_rec,
7066 &para);
7067 if (ret)
7068 mlog_errno(ret);
7069
7070out:
7071 ocfs2_xattr_bucket_free(para.old_bucket);
7072 ocfs2_xattr_bucket_free(para.new_bucket);
7073 return ret;
7074}
7075
7076static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
7077 struct buffer_head *blk_bh)
7078{
7079 int ret, indexed = 0;
7080 struct buffer_head *new_blk_bh = NULL;
7081 struct ocfs2_xattr_block *xb =
7082 (struct ocfs2_xattr_block *)blk_bh->b_data;
7083
7084
7085 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
7086 indexed = 1;
7087
7088 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
7089 &new_blk_bh, indexed);
7090 if (ret) {
7091 mlog_errno(ret);
7092 goto out;
7093 }
7094
Jeff Liu2decd652010-10-12 11:18:18 +08007095 if (!indexed)
Tao Ma2999d122009-08-18 11:43:55 +08007096 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7097 else
7098 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7099 if (ret)
7100 mlog_errno(ret);
7101
7102out:
7103 brelse(new_blk_bh);
7104 return ret;
7105}
7106
Tao Ma0fe9b662009-08-18 11:47:56 +08007107static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7108{
7109 int type = ocfs2_xattr_get_type(xe);
7110
7111 return type != OCFS2_XATTR_INDEX_SECURITY &&
7112 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7113 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7114}
7115
Tao Ma2999d122009-08-18 11:43:55 +08007116int ocfs2_reflink_xattrs(struct inode *old_inode,
7117 struct buffer_head *old_bh,
7118 struct inode *new_inode,
Tao Ma0fe9b662009-08-18 11:47:56 +08007119 struct buffer_head *new_bh,
7120 bool preserve_security)
Tao Ma2999d122009-08-18 11:43:55 +08007121{
7122 int ret;
7123 struct ocfs2_xattr_reflink args;
7124 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7125 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7126 struct buffer_head *blk_bh = NULL;
7127 struct ocfs2_cached_dealloc_ctxt dealloc;
7128 struct ocfs2_refcount_tree *ref_tree;
7129 struct buffer_head *ref_root_bh = NULL;
7130
7131 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7132 le64_to_cpu(di->i_refcount_loc),
7133 1, &ref_tree, &ref_root_bh);
7134 if (ret) {
7135 mlog_errno(ret);
7136 goto out;
7137 }
7138
7139 ocfs2_init_dealloc_ctxt(&dealloc);
7140
7141 args.old_inode = old_inode;
7142 args.new_inode = new_inode;
7143 args.old_bh = old_bh;
7144 args.new_bh = new_bh;
7145 args.ref_ci = &ref_tree->rf_ci;
7146 args.ref_root_bh = ref_root_bh;
7147 args.dealloc = &dealloc;
Tao Ma0fe9b662009-08-18 11:47:56 +08007148 if (preserve_security)
7149 args.xattr_reflinked = NULL;
7150 else
7151 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
Tao Ma2999d122009-08-18 11:43:55 +08007152
7153 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7154 ret = ocfs2_reflink_xattr_inline(&args);
7155 if (ret) {
7156 mlog_errno(ret);
7157 goto out_unlock;
7158 }
7159 }
7160
7161 if (!di->i_xattr_loc)
7162 goto out_unlock;
7163
7164 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7165 &blk_bh);
7166 if (ret < 0) {
7167 mlog_errno(ret);
7168 goto out_unlock;
7169 }
7170
7171 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7172 if (ret)
7173 mlog_errno(ret);
7174
7175 brelse(blk_bh);
7176
7177out_unlock:
7178 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7179 ref_tree, 1);
7180 brelse(ref_root_bh);
7181
7182 if (ocfs2_dealloc_has_cluster(&dealloc)) {
7183 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7184 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7185 }
7186
7187out:
7188 return ret;
7189}
7190
7191/*
Tao Ma0fe9b662009-08-18 11:47:56 +08007192 * Initialize security and acl for a already created inode.
7193 * Used for reflink a non-preserve-security file.
7194 *
7195 * It uses common api like ocfs2_xattr_set, so the caller
7196 * must not hold any lock expect i_mutex.
7197 */
7198int ocfs2_init_security_and_acl(struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -05007199 struct inode *inode,
Christoph Hellwig702e5bc2013-12-20 05:16:48 -08007200 const struct qstr *qstr,
7201 struct posix_acl *default_acl,
7202 struct posix_acl *acl)
Tao Ma0fe9b662009-08-18 11:47:56 +08007203{
Tao Ma0fe9b662009-08-18 11:47:56 +08007204 struct buffer_head *dir_bh = NULL;
Christoph Hellwig702e5bc2013-12-20 05:16:48 -08007205 int ret = 0;
Tao Ma0fe9b662009-08-18 11:47:56 +08007206
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04007207 ret = ocfs2_init_security_get(inode, dir, qstr, NULL);
Jeff Liu32918dd2013-02-27 17:02:48 -08007208 if (ret) {
Tao Ma0fe9b662009-08-18 11:47:56 +08007209 mlog_errno(ret);
7210 goto leave;
7211 }
7212
7213 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7214 if (ret) {
7215 mlog_errno(ret);
7216 goto leave;
7217 }
7218
Christoph Hellwig702e5bc2013-12-20 05:16:48 -08007219 if (!ret && default_acl)
7220 ret = ocfs2_iop_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
7221 if (!ret && acl)
7222 ret = ocfs2_iop_set_acl(inode, acl, ACL_TYPE_ACCESS);
Tao Ma0fe9b662009-08-18 11:47:56 +08007223
7224 ocfs2_inode_unlock(dir, 0);
7225 brelse(dir_bh);
7226leave:
7227 return ret;
7228}
7229/*
Tiger Yang923f7f32008-11-14 11:16:27 +08007230 * 'security' attributes support
7231 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007232static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
Tiger Yang923f7f32008-11-14 11:16:27 +08007233 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007234 size_t name_len, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007235{
7236 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7237 const size_t total_len = prefix_len + name_len + 1;
7238
7239 if (list && total_len <= list_size) {
7240 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7241 memcpy(list + prefix_len, name, name_len);
7242 list[prefix_len + name_len] = '\0';
7243 }
7244 return total_len;
7245}
7246
Christoph Hellwig431547b2009-11-13 09:52:56 +00007247static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7248 void *buffer, size_t size, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007249{
7250 if (strcmp(name, "") == 0)
7251 return -EINVAL;
David Howells2b0143b2015-03-17 22:25:59 +00007252 return ocfs2_xattr_get(d_inode(dentry), OCFS2_XATTR_INDEX_SECURITY,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007253 name, buffer, size);
Tiger Yang923f7f32008-11-14 11:16:27 +08007254}
7255
Christoph Hellwig431547b2009-11-13 09:52:56 +00007256static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7257 const void *value, size_t size, int flags, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007258{
7259 if (strcmp(name, "") == 0)
7260 return -EINVAL;
7261
David Howells2b0143b2015-03-17 22:25:59 +00007262 return ocfs2_xattr_set(d_inode(dentry), OCFS2_XATTR_INDEX_SECURITY,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007263 name, value, size, flags);
Tiger Yang923f7f32008-11-14 11:16:27 +08007264}
7265
Joseph Qib519ea62015-06-24 16:55:34 -07007266static int ocfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04007267 void *fs_info)
7268{
7269 const struct xattr *xattr;
7270 int err = 0;
7271
7272 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
7273 err = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7274 xattr->name, xattr->value,
7275 xattr->value_len, XATTR_CREATE);
7276 if (err)
7277 break;
7278 }
7279 return err;
7280}
7281
Tiger Yang534eadd2008-11-14 11:16:41 +08007282int ocfs2_init_security_get(struct inode *inode,
7283 struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -05007284 const struct qstr *qstr,
Tiger Yang534eadd2008-11-14 11:16:41 +08007285 struct ocfs2_security_xattr_info *si)
7286{
Tiger Yang38d59ef2008-12-17 10:22:56 +08007287 /* check whether ocfs2 support feature xattr */
7288 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7289 return -EOPNOTSUPP;
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04007290 if (si)
7291 return security_old_inode_init_security(inode, dir, qstr,
7292 &si->name, &si->value,
7293 &si->value_len);
7294
7295 return security_inode_init_security(inode, dir, qstr,
7296 &ocfs2_initxattrs, NULL);
Tiger Yang534eadd2008-11-14 11:16:41 +08007297}
7298
7299int ocfs2_init_security_set(handle_t *handle,
7300 struct inode *inode,
7301 struct buffer_head *di_bh,
7302 struct ocfs2_security_xattr_info *si,
7303 struct ocfs2_alloc_context *xattr_ac,
7304 struct ocfs2_alloc_context *data_ac)
7305{
7306 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7307 OCFS2_XATTR_INDEX_SECURITY,
7308 si->name, si->value, si->value_len, 0,
7309 xattr_ac, data_ac);
7310}
7311
Stephen Hemminger537d81c2010-05-13 17:53:22 -07007312const struct xattr_handler ocfs2_xattr_security_handler = {
Tiger Yang923f7f32008-11-14 11:16:27 +08007313 .prefix = XATTR_SECURITY_PREFIX,
7314 .list = ocfs2_xattr_security_list,
7315 .get = ocfs2_xattr_security_get,
7316 .set = ocfs2_xattr_security_set,
7317};
7318
7319/*
Mark Fasheh99219ae2008-10-07 14:52:59 -07007320 * 'trusted' attributes support
7321 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007322static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007323 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007324 size_t name_len, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007325{
Tiger Yangceb1eba2008-10-23 16:34:13 +08007326 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07007327 const size_t total_len = prefix_len + name_len + 1;
7328
Sanidhya Kashyap0f5e7b42015-09-04 15:44:08 -07007329 if (!capable(CAP_SYS_ADMIN))
7330 return 0;
7331
Mark Fasheh99219ae2008-10-07 14:52:59 -07007332 if (list && total_len <= list_size) {
7333 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7334 memcpy(list + prefix_len, name, name_len);
7335 list[prefix_len + name_len] = '\0';
7336 }
7337 return total_len;
7338}
7339
Christoph Hellwig431547b2009-11-13 09:52:56 +00007340static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7341 void *buffer, size_t size, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007342{
7343 if (strcmp(name, "") == 0)
7344 return -EINVAL;
David Howells2b0143b2015-03-17 22:25:59 +00007345 return ocfs2_xattr_get(d_inode(dentry), OCFS2_XATTR_INDEX_TRUSTED,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007346 name, buffer, size);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007347}
7348
Christoph Hellwig431547b2009-11-13 09:52:56 +00007349static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7350 const void *value, size_t size, int flags, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007351{
7352 if (strcmp(name, "") == 0)
7353 return -EINVAL;
7354
David Howells2b0143b2015-03-17 22:25:59 +00007355 return ocfs2_xattr_set(d_inode(dentry), OCFS2_XATTR_INDEX_TRUSTED,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007356 name, value, size, flags);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007357}
7358
Stephen Hemminger537d81c2010-05-13 17:53:22 -07007359const struct xattr_handler ocfs2_xattr_trusted_handler = {
Mark Fasheh99219ae2008-10-07 14:52:59 -07007360 .prefix = XATTR_TRUSTED_PREFIX,
7361 .list = ocfs2_xattr_trusted_list,
7362 .get = ocfs2_xattr_trusted_get,
7363 .set = ocfs2_xattr_trusted_set,
7364};
7365
Mark Fasheh99219ae2008-10-07 14:52:59 -07007366/*
7367 * 'user' attributes support
7368 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007369static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007370 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007371 size_t name_len, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007372{
Tiger Yangceb1eba2008-10-23 16:34:13 +08007373 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07007374 const size_t total_len = prefix_len + name_len + 1;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007375 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007376
7377 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7378 return 0;
7379
7380 if (list && total_len <= list_size) {
7381 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7382 memcpy(list + prefix_len, name, name_len);
7383 list[prefix_len + name_len] = '\0';
7384 }
7385 return total_len;
7386}
7387
Christoph Hellwig431547b2009-11-13 09:52:56 +00007388static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7389 void *buffer, size_t size, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007390{
Christoph Hellwig431547b2009-11-13 09:52:56 +00007391 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007392
7393 if (strcmp(name, "") == 0)
7394 return -EINVAL;
7395 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7396 return -EOPNOTSUPP;
David Howells2b0143b2015-03-17 22:25:59 +00007397 return ocfs2_xattr_get(d_inode(dentry), OCFS2_XATTR_INDEX_USER, name,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007398 buffer, size);
7399}
7400
Christoph Hellwig431547b2009-11-13 09:52:56 +00007401static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7402 const void *value, size_t size, int flags, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007403{
Christoph Hellwig431547b2009-11-13 09:52:56 +00007404 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007405
7406 if (strcmp(name, "") == 0)
7407 return -EINVAL;
7408 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7409 return -EOPNOTSUPP;
7410
David Howells2b0143b2015-03-17 22:25:59 +00007411 return ocfs2_xattr_set(d_inode(dentry), OCFS2_XATTR_INDEX_USER,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007412 name, value, size, flags);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007413}
7414
Stephen Hemminger537d81c2010-05-13 17:53:22 -07007415const struct xattr_handler ocfs2_xattr_user_handler = {
Mark Fasheh99219ae2008-10-07 14:52:59 -07007416 .prefix = XATTR_USER_PREFIX,
7417 .list = ocfs2_xattr_user_list,
7418 .get = ocfs2_xattr_user_get,
7419 .set = ocfs2_xattr_user_set,
7420};