blob: fe3419068df2efa5ed33ff7cdc424327d04431f0 [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#define MLOG_MASK_PREFIX ML_XATTR
41#include <cluster/masklog.h>
42
43#include "ocfs2.h"
44#include "alloc.h"
Joel Beckerd6b32bb2008-10-17 14:55:01 -070045#include "blockcheck.h"
Tao Maf56654c2008-08-18 17:38:48 +080046#include "dlmglue.h"
47#include "file.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080048#include "symlink.h"
49#include "sysfile.h"
Tao Maf56654c2008-08-18 17:38:48 +080050#include "inode.h"
51#include "journal.h"
52#include "ocfs2_fs.h"
53#include "suballoc.h"
54#include "uptodate.h"
55#include "buffer_head_io.h"
Tao Ma0c044f02008-08-18 17:38:50 +080056#include "super.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080057#include "xattr.h"
Tao Ma492a8a32009-08-18 11:43:17 +080058#include "refcounttree.h"
Tao Ma0fe9b662009-08-18 11:47:56 +080059#include "acl.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;
82};
83
Tiger Yangcf1d6c72008-08-18 17:11:00 +080084#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
85#define OCFS2_XATTR_INLINE_SIZE 80
Tiger Yang4442f512009-02-20 11:11:50 +080086#define OCFS2_XATTR_HEADER_GAP 4
Tiger Yang534eadd2008-11-14 11:16:41 +080087#define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
88 - sizeof(struct ocfs2_xattr_header) \
Tiger Yang4442f512009-02-20 11:11:50 +080089 - OCFS2_XATTR_HEADER_GAP)
Tiger Yang89c38bd2008-11-14 11:17:41 +080090#define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
91 - sizeof(struct ocfs2_xattr_block) \
92 - sizeof(struct ocfs2_xattr_header) \
Tiger Yang4442f512009-02-20 11:11:50 +080093 - OCFS2_XATTR_HEADER_GAP)
Tiger Yangcf1d6c72008-08-18 17:11:00 +080094
95static struct ocfs2_xattr_def_value_root def_xv = {
96 .xv.xr_list.l_count = cpu_to_le16(1),
97};
98
99struct xattr_handler *ocfs2_xattr_handlers[] = {
100 &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +0800101#ifdef CONFIG_OCFS2_FS_POSIX_ACL
102 &ocfs2_xattr_acl_access_handler,
103 &ocfs2_xattr_acl_default_handler,
104#endif
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800105 &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800106 &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800107 NULL
108};
109
Tiger Yangc988fd02008-10-23 16:34:44 +0800110static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800111 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +0800112#ifdef CONFIG_OCFS2_FS_POSIX_ACL
113 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
114 = &ocfs2_xattr_acl_access_handler,
115 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
116 = &ocfs2_xattr_acl_default_handler,
117#endif
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800118 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800119 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800120};
121
122struct ocfs2_xattr_info {
123 int name_index;
124 const char *name;
125 const void *value;
126 size_t value_len;
127};
128
129struct ocfs2_xattr_search {
130 struct buffer_head *inode_bh;
131 /*
132 * xattr_bh point to the block buffer head which has extended attribute
133 * when extended attribute in inode, xattr_bh is equal to inode_bh.
134 */
135 struct buffer_head *xattr_bh;
136 struct ocfs2_xattr_header *header;
Joel Beckerba937122008-10-24 19:13:20 -0700137 struct ocfs2_xattr_bucket *bucket;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800138 void *base;
139 void *end;
140 struct ocfs2_xattr_entry *here;
141 int not_found;
142};
143
Tao Mafd68a892009-08-18 11:43:21 +0800144static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
Tao Ma589dc262008-08-18 17:38:51 +0800145 struct ocfs2_xattr_header *xh,
146 int index,
147 int *block_off,
148 int *new_offset);
149
Joel Becker54f443f2008-10-20 18:43:07 -0700150static int ocfs2_xattr_block_find(struct inode *inode,
151 int name_index,
152 const char *name,
153 struct ocfs2_xattr_search *xs);
Tao Ma589dc262008-08-18 17:38:51 +0800154static int ocfs2_xattr_index_block_find(struct inode *inode,
155 struct buffer_head *root_bh,
156 int name_index,
157 const char *name,
158 struct ocfs2_xattr_search *xs);
159
Tao Ma0c044f02008-08-18 17:38:50 +0800160static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
Tao Ma47bca492009-08-18 11:43:42 +0800161 struct buffer_head *blk_bh,
Tao Ma0c044f02008-08-18 17:38:50 +0800162 char *buffer,
163 size_t buffer_size);
164
Tao Ma01225592008-08-18 17:38:53 +0800165static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +0800166 struct ocfs2_xattr_search *xs,
167 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800168
169static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
170 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +0800171 struct ocfs2_xattr_search *xs,
172 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800173
Tao Ma47bca492009-08-18 11:43:42 +0800174typedef int (xattr_tree_rec_func)(struct inode *inode,
175 struct buffer_head *root_bh,
176 u64 blkno, u32 cpos, u32 len, void *para);
177static int ocfs2_iterate_xattr_index_block(struct inode *inode,
178 struct buffer_head *root_bh,
179 xattr_tree_rec_func *rec_func,
180 void *para);
181static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
182 struct ocfs2_xattr_bucket *bucket,
183 void *para);
184static int ocfs2_rm_xattr_cluster(struct inode *inode,
185 struct buffer_head *root_bh,
186 u64 blkno,
187 u32 cpos,
188 u32 len,
189 void *para);
190
Joel Beckerc58b6032008-11-26 13:36:24 -0800191static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
192 u64 src_blk, u64 last_blk, u64 to_blk,
193 unsigned int start_bucket,
194 u32 *first_hash);
Tao Ma492a8a32009-08-18 11:43:17 +0800195static int ocfs2_prepare_refcount_xattr(struct inode *inode,
196 struct ocfs2_dinode *di,
197 struct ocfs2_xattr_info *xi,
198 struct ocfs2_xattr_search *xis,
199 struct ocfs2_xattr_search *xbs,
200 struct ocfs2_refcount_tree **ref_tree,
201 int *meta_need,
202 int *credits);
Tao Mace9c5a52009-08-18 11:43:59 +0800203static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
204 struct ocfs2_xattr_bucket *bucket,
205 int offset,
206 struct ocfs2_xattr_value_root **xv,
207 struct buffer_head **bh);
Tao Ma0fe9b662009-08-18 11:47:56 +0800208static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
209 const void *value, size_t size, int flags);
Tao Maa3944252008-08-18 17:38:54 +0800210
Tiger Yang0030e002008-10-23 16:33:33 +0800211static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
212{
213 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
214}
215
216static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
217{
218 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
219}
220
221static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
222{
223 u16 len = sb->s_blocksize -
224 offsetof(struct ocfs2_xattr_header, xh_entries);
225
226 return len / sizeof(struct ocfs2_xattr_entry);
227}
228
Joel Becker9c7759a2008-10-24 16:21:03 -0700229#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
Joel Becker51def392008-10-24 16:57:21 -0700230#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
Joel Becker3e632942008-10-24 17:04:49 -0700231#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
Joel Becker9c7759a2008-10-24 16:21:03 -0700232
Joel Beckerba937122008-10-24 19:13:20 -0700233static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
Joel Becker6dde41d2008-10-24 17:16:48 -0700234{
Joel Beckerba937122008-10-24 19:13:20 -0700235 struct ocfs2_xattr_bucket *bucket;
236 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker6dde41d2008-10-24 17:16:48 -0700237
Joel Beckerba937122008-10-24 19:13:20 -0700238 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
239
240 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
241 if (bucket) {
242 bucket->bu_inode = inode;
243 bucket->bu_blocks = blks;
244 }
245
246 return bucket;
247}
248
249static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
250{
251 int i;
252
253 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker6dde41d2008-10-24 17:16:48 -0700254 brelse(bucket->bu_bhs[i]);
255 bucket->bu_bhs[i] = NULL;
256 }
257}
258
Joel Beckerba937122008-10-24 19:13:20 -0700259static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
260{
261 if (bucket) {
262 ocfs2_xattr_bucket_relse(bucket);
263 bucket->bu_inode = NULL;
264 kfree(bucket);
265 }
266}
267
Joel Becker784b8162008-10-24 17:33:40 -0700268/*
269 * A bucket that has never been written to disk doesn't need to be
270 * read. We just need the buffer_heads. Don't call this for
271 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
272 * them fully.
273 */
Joel Beckerba937122008-10-24 19:13:20 -0700274static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700275 u64 xb_blkno)
276{
277 int i, rc = 0;
Joel Becker784b8162008-10-24 17:33:40 -0700278
Joel Beckerba937122008-10-24 19:13:20 -0700279 for (i = 0; i < bucket->bu_blocks; i++) {
280 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
281 xb_blkno + i);
Joel Becker784b8162008-10-24 17:33:40 -0700282 if (!bucket->bu_bhs[i]) {
283 rc = -EIO;
284 mlog_errno(rc);
285 break;
286 }
287
Joel Becker8cb471e2009-02-10 20:00:41 -0800288 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
Tao Ma757055a2008-11-06 08:10:48 +0800289 bucket->bu_bhs[i]))
Joel Becker8cb471e2009-02-10 20:00:41 -0800290 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
Tao Ma757055a2008-11-06 08:10:48 +0800291 bucket->bu_bhs[i]);
Joel Becker784b8162008-10-24 17:33:40 -0700292 }
293
294 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700295 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700296 return rc;
297}
298
299/* Read the xattr bucket at xb_blkno */
Joel Beckerba937122008-10-24 19:13:20 -0700300static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700301 u64 xb_blkno)
302{
Joel Beckerba937122008-10-24 19:13:20 -0700303 int rc;
Joel Becker784b8162008-10-24 17:33:40 -0700304
Joel Becker8cb471e2009-02-10 20:00:41 -0800305 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
Joel Becker970e4932008-11-13 14:49:19 -0800306 bucket->bu_blocks, bucket->bu_bhs, 0,
307 NULL);
Joel Becker4d0e2142008-12-05 11:19:37 -0800308 if (!rc) {
Tao Mac8b9cf92009-02-24 17:40:26 -0800309 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800310 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
311 bucket->bu_bhs,
312 bucket->bu_blocks,
313 &bucket_xh(bucket)->xh_check);
Tao Mac8b9cf92009-02-24 17:40:26 -0800314 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800315 if (rc)
316 mlog_errno(rc);
317 }
318
Joel Becker784b8162008-10-24 17:33:40 -0700319 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700320 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700321 return rc;
322}
323
Joel Becker1224be02008-10-24 18:47:33 -0700324static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700325 struct ocfs2_xattr_bucket *bucket,
326 int type)
327{
328 int i, rc = 0;
Joel Becker1224be02008-10-24 18:47:33 -0700329
Joel Beckerba937122008-10-24 19:13:20 -0700330 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800331 rc = ocfs2_journal_access(handle,
332 INODE_CACHE(bucket->bu_inode),
Joel Becker1224be02008-10-24 18:47:33 -0700333 bucket->bu_bhs[i], type);
334 if (rc) {
335 mlog_errno(rc);
336 break;
337 }
338 }
339
340 return rc;
341}
342
343static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700344 struct ocfs2_xattr_bucket *bucket)
345{
Joel Beckerba937122008-10-24 19:13:20 -0700346 int i;
Joel Becker1224be02008-10-24 18:47:33 -0700347
Tao Mac8b9cf92009-02-24 17:40:26 -0800348 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800349 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
350 bucket->bu_bhs, bucket->bu_blocks,
351 &bucket_xh(bucket)->xh_check);
Tao Mac8b9cf92009-02-24 17:40:26 -0800352 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800353
Joel Beckerba937122008-10-24 19:13:20 -0700354 for (i = 0; i < bucket->bu_blocks; i++)
Joel Becker1224be02008-10-24 18:47:33 -0700355 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
356}
357
Joel Beckerba937122008-10-24 19:13:20 -0700358static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
Joel Becker4980c6d2008-10-24 18:54:43 -0700359 struct ocfs2_xattr_bucket *src)
360{
361 int i;
Joel Beckerba937122008-10-24 19:13:20 -0700362 int blocksize = src->bu_inode->i_sb->s_blocksize;
Joel Becker4980c6d2008-10-24 18:54:43 -0700363
Joel Beckerba937122008-10-24 19:13:20 -0700364 BUG_ON(dest->bu_blocks != src->bu_blocks);
365 BUG_ON(dest->bu_inode != src->bu_inode);
366
367 for (i = 0; i < src->bu_blocks; i++) {
Joel Becker4980c6d2008-10-24 18:54:43 -0700368 memcpy(bucket_block(dest, i), bucket_block(src, i),
369 blocksize);
370 }
371}
Joel Becker1224be02008-10-24 18:47:33 -0700372
Joel Becker4ae1d692008-11-13 14:49:18 -0800373static int ocfs2_validate_xattr_block(struct super_block *sb,
374 struct buffer_head *bh)
375{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700376 int rc;
Joel Becker4ae1d692008-11-13 14:49:18 -0800377 struct ocfs2_xattr_block *xb =
378 (struct ocfs2_xattr_block *)bh->b_data;
379
380 mlog(0, "Validating xattr block %llu\n",
381 (unsigned long long)bh->b_blocknr);
382
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700383 BUG_ON(!buffer_uptodate(bh));
384
385 /*
386 * If the ecc fails, we return the error but otherwise
387 * leave the filesystem running. We know any error is
388 * local to this block.
389 */
390 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
391 if (rc)
392 return rc;
393
394 /*
395 * Errors after here are fatal
396 */
397
Joel Becker4ae1d692008-11-13 14:49:18 -0800398 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
399 ocfs2_error(sb,
400 "Extended attribute block #%llu has bad "
401 "signature %.*s",
402 (unsigned long long)bh->b_blocknr, 7,
403 xb->xb_signature);
404 return -EINVAL;
405 }
406
407 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
408 ocfs2_error(sb,
409 "Extended attribute block #%llu has an "
410 "invalid xb_blkno of %llu",
411 (unsigned long long)bh->b_blocknr,
412 (unsigned long long)le64_to_cpu(xb->xb_blkno));
413 return -EINVAL;
414 }
415
416 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
417 ocfs2_error(sb,
418 "Extended attribute block #%llu has an invalid "
419 "xb_fs_generation of #%u",
420 (unsigned long long)bh->b_blocknr,
421 le32_to_cpu(xb->xb_fs_generation));
422 return -EINVAL;
423 }
424
425 return 0;
426}
427
428static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
429 struct buffer_head **bh)
430{
431 int rc;
432 struct buffer_head *tmp = *bh;
433
Joel Becker8cb471e2009-02-10 20:00:41 -0800434 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800435 ocfs2_validate_xattr_block);
Joel Becker4ae1d692008-11-13 14:49:18 -0800436
437 /* If ocfs2_read_block() got us a new bh, pass it up. */
438 if (!rc && !*bh)
439 *bh = tmp;
440
441 return rc;
442}
443
Tao Ma936b8832008-10-09 23:06:14 +0800444static inline const char *ocfs2_xattr_prefix(int name_index)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800445{
446 struct xattr_handler *handler = NULL;
447
448 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
449 handler = ocfs2_xattr_handler_map[name_index];
450
Tao Ma936b8832008-10-09 23:06:14 +0800451 return handler ? handler->prefix : NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800452}
453
Mark Fasheh40daa162008-10-07 14:31:42 -0700454static u32 ocfs2_xattr_name_hash(struct inode *inode,
Tao Ma2057e5c2008-10-09 23:06:13 +0800455 const char *name,
Mark Fasheh40daa162008-10-07 14:31:42 -0700456 int name_len)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800457{
458 /* Get hash value of uuid from super block */
459 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
460 int i;
461
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800462 /* hash extended attribute name */
463 for (i = 0; i < name_len; i++) {
464 hash = (hash << OCFS2_HASH_SHIFT) ^
465 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
466 *name++;
467 }
468
469 return hash;
470}
471
472/*
473 * ocfs2_xattr_hash_entry()
474 *
475 * Compute the hash of an extended attribute.
476 */
477static void ocfs2_xattr_hash_entry(struct inode *inode,
478 struct ocfs2_xattr_header *header,
479 struct ocfs2_xattr_entry *entry)
480{
481 u32 hash = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800482 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800483
Tao Ma2057e5c2008-10-09 23:06:13 +0800484 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800485 entry->xe_name_hash = cpu_to_le32(hash);
486
487 return;
488}
Tao Maf56654c2008-08-18 17:38:48 +0800489
Tiger Yang534eadd2008-11-14 11:16:41 +0800490static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
491{
492 int size = 0;
493
494 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
495 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
496 else
497 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
498 size += sizeof(struct ocfs2_xattr_entry);
499
500 return size;
501}
502
503int ocfs2_calc_security_init(struct inode *dir,
504 struct ocfs2_security_xattr_info *si,
505 int *want_clusters,
506 int *xattr_credits,
507 struct ocfs2_alloc_context **xattr_ac)
508{
509 int ret = 0;
510 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
511 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
512 si->value_len);
513
514 /*
515 * The max space of security xattr taken inline is
516 * 256(name) + 80(value) + 16(entry) = 352 bytes,
517 * So reserve one metadata block for it is ok.
518 */
519 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
520 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
521 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
522 if (ret) {
523 mlog_errno(ret);
524 return ret;
525 }
526 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
527 }
528
529 /* reserve clusters for xattr value which will be set in B tree*/
Tiger Yang0e445b62008-12-09 16:42:51 +0800530 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
531 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
532 si->value_len);
533
534 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
535 new_clusters);
536 *want_clusters += new_clusters;
537 }
Tiger Yang534eadd2008-11-14 11:16:41 +0800538 return ret;
539}
540
Tiger Yang89c38bd2008-11-14 11:17:41 +0800541int ocfs2_calc_xattr_init(struct inode *dir,
542 struct buffer_head *dir_bh,
543 int mode,
544 struct ocfs2_security_xattr_info *si,
545 int *want_clusters,
546 int *xattr_credits,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800547 int *want_meta)
Tiger Yang89c38bd2008-11-14 11:17:41 +0800548{
549 int ret = 0;
550 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
Tiger Yang0e445b62008-12-09 16:42:51 +0800551 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800552
553 if (si->enable)
554 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
555 si->value_len);
556
557 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
558 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
559 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
560 "", NULL, 0);
561 if (acl_len > 0) {
562 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
563 if (S_ISDIR(mode))
564 a_size <<= 1;
565 } else if (acl_len != 0 && acl_len != -ENODATA) {
566 mlog_errno(ret);
567 return ret;
568 }
569 }
570
571 if (!(s_size + a_size))
572 return ret;
573
574 /*
575 * The max space of security xattr taken inline is
576 * 256(name) + 80(value) + 16(entry) = 352 bytes,
577 * The max space of acl xattr taken inline is
578 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
579 * when blocksize = 512, may reserve one more cluser for
580 * xattr bucket, otherwise reserve one metadata block
581 * for them is ok.
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800582 * If this is a new directory with inline data,
583 * we choose to reserve the entire inline area for
584 * directory contents and force an external xattr block.
Tiger Yang89c38bd2008-11-14 11:17:41 +0800585 */
586 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800587 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
Tiger Yang89c38bd2008-11-14 11:17:41 +0800588 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800589 *want_meta = *want_meta + 1;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800590 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
591 }
592
593 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
594 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
595 *want_clusters += 1;
596 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
597 }
598
Tiger Yang0e445b62008-12-09 16:42:51 +0800599 /*
600 * reserve credits and clusters for xattrs which has large value
601 * and have to be set outside
602 */
603 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
604 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
605 si->value_len);
606 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
607 new_clusters);
608 *want_clusters += new_clusters;
609 }
Tiger Yang89c38bd2008-11-14 11:17:41 +0800610 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
611 acl_len > OCFS2_XATTR_INLINE_SIZE) {
Tiger Yang0e445b62008-12-09 16:42:51 +0800612 /* for directory, it has DEFAULT and ACCESS two types of acls */
613 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
614 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
615 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
616 new_clusters);
617 *want_clusters += new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800618 }
619
620 return ret;
621}
622
Tao Maf56654c2008-08-18 17:38:48 +0800623static int ocfs2_xattr_extend_allocation(struct inode *inode,
624 u32 clusters_to_add,
Joel Becker19b801f2008-12-09 14:36:50 -0800625 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800626 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800627{
628 int status = 0;
Tao Ma85db90e2008-11-12 08:27:01 +0800629 handle_t *handle = ctxt->handle;
Tao Maf56654c2008-08-18 17:38:48 +0800630 enum ocfs2_alloc_restarted why;
Joel Becker19b801f2008-12-09 14:36:50 -0800631 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700632 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800633
634 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
635
Joel Becker5e404e92009-02-13 03:54:22 -0800636 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700637
Joel Becker0cf2f762009-02-12 16:41:25 -0800638 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker2a50a742008-12-09 14:24:33 -0800639 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800640 if (status < 0) {
641 mlog_errno(status);
642 goto leave;
643 }
644
Joel Becker19b801f2008-12-09 14:36:50 -0800645 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckercbee7e12009-02-13 03:34:15 -0800646 status = ocfs2_add_clusters_in_btree(handle,
647 &et,
Tao Maf56654c2008-08-18 17:38:48 +0800648 &logical_start,
649 clusters_to_add,
650 0,
Tao Ma78f30c32008-11-12 08:27:00 +0800651 ctxt->data_ac,
652 ctxt->meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700653 &why);
Tao Ma85db90e2008-11-12 08:27:01 +0800654 if (status < 0) {
655 mlog_errno(status);
Tao Maf56654c2008-08-18 17:38:48 +0800656 goto leave;
657 }
658
Joel Becker19b801f2008-12-09 14:36:50 -0800659 status = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800660 if (status < 0) {
661 mlog_errno(status);
662 goto leave;
663 }
664
Joel Becker19b801f2008-12-09 14:36:50 -0800665 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
Tao Maf56654c2008-08-18 17:38:48 +0800666
Tao Ma85db90e2008-11-12 08:27:01 +0800667 /*
668 * We should have already allocated enough space before the transaction,
669 * so no need to restart.
670 */
671 BUG_ON(why != RESTART_NONE || clusters_to_add);
Tao Maf56654c2008-08-18 17:38:48 +0800672
673leave:
Tao Maf56654c2008-08-18 17:38:48 +0800674
675 return status;
676}
677
678static int __ocfs2_remove_xattr_range(struct inode *inode,
Joel Beckerd72cc722008-12-09 14:30:41 -0800679 struct ocfs2_xattr_value_buf *vb,
Tao Maf56654c2008-08-18 17:38:48 +0800680 u32 cpos, u32 phys_cpos, u32 len,
Tao Ma492a8a32009-08-18 11:43:17 +0800681 unsigned int ext_flags,
Tao Ma78f30c32008-11-12 08:27:00 +0800682 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800683{
684 int ret;
685 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Tao Ma85db90e2008-11-12 08:27:01 +0800686 handle_t *handle = ctxt->handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700687 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800688
Joel Becker5e404e92009-02-13 03:54:22 -0800689 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700690
Joel Becker0cf2f762009-02-12 16:41:25 -0800691 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Beckerd72cc722008-12-09 14:30:41 -0800692 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800693 if (ret) {
694 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800695 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800696 }
697
Joel Beckerdbdcf6a2009-02-13 03:41:26 -0800698 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
Tao Ma78f30c32008-11-12 08:27:00 +0800699 &ctxt->dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800700 if (ret) {
701 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800702 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800703 }
704
Joel Beckerd72cc722008-12-09 14:30:41 -0800705 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
Tao Maf56654c2008-08-18 17:38:48 +0800706
Joel Beckerd72cc722008-12-09 14:30:41 -0800707 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800708 if (ret) {
709 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800710 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800711 }
712
Tao Ma492a8a32009-08-18 11:43:17 +0800713 if (ext_flags & OCFS2_EXT_REFCOUNTED)
714 ret = ocfs2_decrease_refcount(inode, handle,
715 ocfs2_blocks_to_clusters(inode->i_sb,
716 phys_blkno),
717 len, ctxt->meta_ac, &ctxt->dealloc, 1);
718 else
719 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
720 phys_blkno, len);
Tao Maf56654c2008-08-18 17:38:48 +0800721 if (ret)
722 mlog_errno(ret);
723
Tao Maf56654c2008-08-18 17:38:48 +0800724out:
Tao Maf56654c2008-08-18 17:38:48 +0800725 return ret;
726}
727
728static int ocfs2_xattr_shrink_size(struct inode *inode,
729 u32 old_clusters,
730 u32 new_clusters,
Joel Becker19b801f2008-12-09 14:36:50 -0800731 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800732 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800733{
734 int ret = 0;
Tao Ma492a8a32009-08-18 11:43:17 +0800735 unsigned int ext_flags;
Tao Maf56654c2008-08-18 17:38:48 +0800736 u32 trunc_len, cpos, phys_cpos, alloc_size;
737 u64 block;
Tao Maf56654c2008-08-18 17:38:48 +0800738
739 if (old_clusters <= new_clusters)
740 return 0;
741
742 cpos = new_clusters;
743 trunc_len = old_clusters - new_clusters;
744 while (trunc_len) {
745 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
Joel Beckerd72cc722008-12-09 14:30:41 -0800746 &alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800747 &vb->vb_xv->xr_list, &ext_flags);
Tao Maf56654c2008-08-18 17:38:48 +0800748 if (ret) {
749 mlog_errno(ret);
750 goto out;
751 }
752
753 if (alloc_size > trunc_len)
754 alloc_size = trunc_len;
755
Joel Becker19b801f2008-12-09 14:36:50 -0800756 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
Tao Maf56654c2008-08-18 17:38:48 +0800757 phys_cpos, alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800758 ext_flags, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800759 if (ret) {
760 mlog_errno(ret);
761 goto out;
762 }
763
764 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Joel Becker8cb471e2009-02-10 20:00:41 -0800765 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
766 block, alloc_size);
Tao Maf56654c2008-08-18 17:38:48 +0800767 cpos += alloc_size;
768 trunc_len -= alloc_size;
769 }
770
771out:
Tao Maf56654c2008-08-18 17:38:48 +0800772 return ret;
773}
774
775static int ocfs2_xattr_value_truncate(struct inode *inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800776 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800777 int len,
778 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800779{
780 int ret;
781 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
Joel Beckerb3e5d372008-12-09 15:01:04 -0800782 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800783
784 if (new_clusters == old_clusters)
785 return 0;
786
787 if (new_clusters > old_clusters)
788 ret = ocfs2_xattr_extend_allocation(inode,
789 new_clusters - old_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800790 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800791 else
792 ret = ocfs2_xattr_shrink_size(inode,
793 old_clusters, new_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800794 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800795
796 return ret;
797}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800798
Tao Ma936b8832008-10-09 23:06:14 +0800799static int ocfs2_xattr_list_entry(char *buffer, size_t size,
800 size_t *result, const char *prefix,
801 const char *name, int name_len)
802{
803 char *p = buffer + *result;
804 int prefix_len = strlen(prefix);
805 int total_len = prefix_len + name_len + 1;
806
807 *result += total_len;
808
809 /* we are just looking for how big our buffer needs to be */
810 if (!size)
811 return 0;
812
813 if (*result > size)
814 return -ERANGE;
815
816 memcpy(p, prefix, prefix_len);
817 memcpy(p + prefix_len, name, name_len);
818 p[prefix_len + name_len] = '\0';
819
820 return 0;
821}
822
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800823static int ocfs2_xattr_list_entries(struct inode *inode,
824 struct ocfs2_xattr_header *header,
825 char *buffer, size_t buffer_size)
826{
Tao Ma936b8832008-10-09 23:06:14 +0800827 size_t result = 0;
828 int i, type, ret;
829 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800830
831 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
832 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800833 type = ocfs2_xattr_get_type(entry);
834 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800835
Tao Ma936b8832008-10-09 23:06:14 +0800836 if (prefix) {
837 name = (const char *)header +
838 le16_to_cpu(entry->xe_name_offset);
839
840 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
841 &result, prefix, name,
842 entry->xe_name_len);
843 if (ret)
844 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800845 }
846 }
847
Tao Ma936b8832008-10-09 23:06:14 +0800848 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800849}
850
Tao Ma8b2c0db2009-08-18 11:43:49 +0800851int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
852 struct ocfs2_dinode *di)
853{
854 struct ocfs2_xattr_header *xh;
855 int i;
856
857 xh = (struct ocfs2_xattr_header *)
858 ((void *)di + inode->i_sb->s_blocksize -
859 le16_to_cpu(di->i_xattr_inline_size));
860
861 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
862 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
863 return 1;
864
865 return 0;
866}
867
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800868static int ocfs2_xattr_ibody_list(struct inode *inode,
869 struct ocfs2_dinode *di,
870 char *buffer,
871 size_t buffer_size)
872{
873 struct ocfs2_xattr_header *header = NULL;
874 struct ocfs2_inode_info *oi = OCFS2_I(inode);
875 int ret = 0;
876
877 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
878 return ret;
879
880 header = (struct ocfs2_xattr_header *)
881 ((void *)di + inode->i_sb->s_blocksize -
882 le16_to_cpu(di->i_xattr_inline_size));
883
884 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
885
886 return ret;
887}
888
889static int ocfs2_xattr_block_list(struct inode *inode,
890 struct ocfs2_dinode *di,
891 char *buffer,
892 size_t buffer_size)
893{
894 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800895 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800896 int ret = 0;
897
898 if (!di->i_xattr_loc)
899 return ret;
900
Joel Becker4ae1d692008-11-13 14:49:18 -0800901 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
902 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800903 if (ret < 0) {
904 mlog_errno(ret);
905 return ret;
906 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800907
Tao Ma0c044f02008-08-18 17:38:50 +0800908 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma0c044f02008-08-18 17:38:50 +0800909 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
910 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
911 ret = ocfs2_xattr_list_entries(inode, header,
912 buffer, buffer_size);
Tao Ma47bca492009-08-18 11:43:42 +0800913 } else
914 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
Tao Ma0c044f02008-08-18 17:38:50 +0800915 buffer, buffer_size);
Joel Becker4ae1d692008-11-13 14:49:18 -0800916
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800917 brelse(blk_bh);
918
919 return ret;
920}
921
922ssize_t ocfs2_listxattr(struct dentry *dentry,
923 char *buffer,
924 size_t size)
925{
926 int ret = 0, i_ret = 0, b_ret = 0;
927 struct buffer_head *di_bh = NULL;
928 struct ocfs2_dinode *di = NULL;
929 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
930
Tiger Yang8154da32008-08-18 17:11:46 +0800931 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
932 return -EOPNOTSUPP;
933
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800934 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
935 return ret;
936
937 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
938 if (ret < 0) {
939 mlog_errno(ret);
940 return ret;
941 }
942
943 di = (struct ocfs2_dinode *)di_bh->b_data;
944
945 down_read(&oi->ip_xattr_sem);
946 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
947 if (i_ret < 0)
948 b_ret = 0;
949 else {
950 if (buffer) {
951 buffer += i_ret;
952 size -= i_ret;
953 }
954 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
955 buffer, size);
956 if (b_ret < 0)
957 i_ret = 0;
958 }
959 up_read(&oi->ip_xattr_sem);
960 ocfs2_inode_unlock(dentry->d_inode, 0);
961
962 brelse(di_bh);
963
964 return i_ret + b_ret;
965}
966
967static int ocfs2_xattr_find_entry(int name_index,
968 const char *name,
969 struct ocfs2_xattr_search *xs)
970{
971 struct ocfs2_xattr_entry *entry;
972 size_t name_len;
973 int i, cmp = 1;
974
975 if (name == NULL)
976 return -EINVAL;
977
978 name_len = strlen(name);
979 entry = xs->here;
980 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
981 cmp = name_index - ocfs2_xattr_get_type(entry);
982 if (!cmp)
983 cmp = name_len - entry->xe_name_len;
984 if (!cmp)
985 cmp = memcmp(name, (xs->base +
986 le16_to_cpu(entry->xe_name_offset)),
987 name_len);
988 if (cmp == 0)
989 break;
990 entry += 1;
991 }
992 xs->here = entry;
993
994 return cmp ? -ENODATA : 0;
995}
996
997static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +0800998 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800999 void *buffer,
1000 size_t len)
1001{
1002 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1003 u64 blkno;
1004 int i, ret = 0;
1005 size_t cplen, blocksize;
1006 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001007 struct ocfs2_extent_list *el;
1008
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001009 el = &xv->xr_list;
1010 clusters = le32_to_cpu(xv->xr_clusters);
1011 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1012 blocksize = inode->i_sb->s_blocksize;
1013
1014 cpos = 0;
1015 while (cpos < clusters) {
1016 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001017 &num_clusters, el, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001018 if (ret) {
1019 mlog_errno(ret);
1020 goto out;
1021 }
1022
1023 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1024 /* Copy ocfs2_xattr_value */
1025 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001026 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1027 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001028 if (ret) {
1029 mlog_errno(ret);
1030 goto out;
1031 }
1032
1033 cplen = len >= blocksize ? blocksize : len;
1034 memcpy(buffer, bh->b_data, cplen);
1035 len -= cplen;
1036 buffer += cplen;
1037
1038 brelse(bh);
1039 bh = NULL;
1040 if (len == 0)
1041 break;
1042 }
1043 cpos += num_clusters;
1044 }
1045out:
1046 return ret;
1047}
1048
1049static int ocfs2_xattr_ibody_get(struct inode *inode,
1050 int name_index,
1051 const char *name,
1052 void *buffer,
1053 size_t buffer_size,
1054 struct ocfs2_xattr_search *xs)
1055{
1056 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1057 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +08001058 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001059 size_t size;
1060 int ret = 0;
1061
1062 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1063 return -ENODATA;
1064
1065 xs->end = (void *)di + inode->i_sb->s_blocksize;
1066 xs->header = (struct ocfs2_xattr_header *)
1067 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1068 xs->base = (void *)xs->header;
1069 xs->here = xs->header->xh_entries;
1070
1071 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1072 if (ret)
1073 return ret;
1074 size = le64_to_cpu(xs->here->xe_value_size);
1075 if (buffer) {
1076 if (size > buffer_size)
1077 return -ERANGE;
1078 if (ocfs2_xattr_is_local(xs->here)) {
1079 memcpy(buffer, (void *)xs->base +
1080 le16_to_cpu(xs->here->xe_name_offset) +
1081 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1082 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001083 xv = (struct ocfs2_xattr_value_root *)
1084 (xs->base + le16_to_cpu(
1085 xs->here->xe_name_offset) +
1086 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1087 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001088 buffer, size);
1089 if (ret < 0) {
1090 mlog_errno(ret);
1091 return ret;
1092 }
1093 }
1094 }
1095
1096 return size;
1097}
1098
1099static int ocfs2_xattr_block_get(struct inode *inode,
1100 int name_index,
1101 const char *name,
1102 void *buffer,
1103 size_t buffer_size,
1104 struct ocfs2_xattr_search *xs)
1105{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001106 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +08001107 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001108 size_t size;
Subrata Modak44d8e4e2009-07-14 01:19:31 +05301109 int ret = -ENODATA, name_offset, name_len, i;
1110 int uninitialized_var(block_off);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001111
Joel Beckerba937122008-10-24 19:13:20 -07001112 xs->bucket = ocfs2_xattr_bucket_new(inode);
1113 if (!xs->bucket) {
1114 ret = -ENOMEM;
1115 mlog_errno(ret);
1116 goto cleanup;
1117 }
Tao Ma589dc262008-08-18 17:38:51 +08001118
Joel Becker54f443f2008-10-20 18:43:07 -07001119 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1120 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001121 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001122 goto cleanup;
1123 }
1124
Tiger Yang6c1e1832008-11-02 19:04:21 +08001125 if (xs->not_found) {
1126 ret = -ENODATA;
1127 goto cleanup;
1128 }
1129
Joel Becker54f443f2008-10-20 18:43:07 -07001130 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001131 size = le64_to_cpu(xs->here->xe_value_size);
1132 if (buffer) {
1133 ret = -ERANGE;
1134 if (size > buffer_size)
1135 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +08001136
1137 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1138 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1139 i = xs->here - xs->header->xh_entries;
1140
1141 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08001142 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Beckerba937122008-10-24 19:13:20 -07001143 bucket_xh(xs->bucket),
Tao Ma589dc262008-08-18 17:38:51 +08001144 i,
1145 &block_off,
1146 &name_offset);
Joel Beckerba937122008-10-24 19:13:20 -07001147 xs->base = bucket_block(xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +08001148 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001149 if (ocfs2_xattr_is_local(xs->here)) {
1150 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +08001151 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001152 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001153 xv = (struct ocfs2_xattr_value_root *)
1154 (xs->base + name_offset + name_len);
1155 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001156 buffer, size);
1157 if (ret < 0) {
1158 mlog_errno(ret);
1159 goto cleanup;
1160 }
1161 }
1162 }
1163 ret = size;
1164cleanup:
Joel Beckerba937122008-10-24 19:13:20 -07001165 ocfs2_xattr_bucket_free(xs->bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001166
Joel Becker54f443f2008-10-20 18:43:07 -07001167 brelse(xs->xattr_bh);
1168 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001169 return ret;
1170}
1171
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001172int ocfs2_xattr_get_nolock(struct inode *inode,
1173 struct buffer_head *di_bh,
Tiger Yang0030e002008-10-23 16:33:33 +08001174 int name_index,
1175 const char *name,
1176 void *buffer,
1177 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001178{
1179 int ret;
1180 struct ocfs2_dinode *di = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001181 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1182 struct ocfs2_xattr_search xis = {
1183 .not_found = -ENODATA,
1184 };
1185 struct ocfs2_xattr_search xbs = {
1186 .not_found = -ENODATA,
1187 };
1188
Tiger Yang8154da32008-08-18 17:11:46 +08001189 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1190 return -EOPNOTSUPP;
1191
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001192 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1193 ret = -ENODATA;
1194
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001195 xis.inode_bh = xbs.inode_bh = di_bh;
1196 di = (struct ocfs2_dinode *)di_bh->b_data;
1197
1198 down_read(&oi->ip_xattr_sem);
1199 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1200 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +08001201 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001202 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1203 buffer_size, &xbs);
1204 up_read(&oi->ip_xattr_sem);
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001205
1206 return ret;
1207}
1208
1209/* ocfs2_xattr_get()
1210 *
1211 * Copy an extended attribute into the buffer provided.
1212 * Buffer is NULL to compute the size of buffer required.
1213 */
1214static int ocfs2_xattr_get(struct inode *inode,
1215 int name_index,
1216 const char *name,
1217 void *buffer,
1218 size_t buffer_size)
1219{
1220 int ret;
1221 struct buffer_head *di_bh = NULL;
1222
1223 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1224 if (ret < 0) {
1225 mlog_errno(ret);
1226 return ret;
1227 }
1228 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1229 name, buffer, buffer_size);
1230
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001231 ocfs2_inode_unlock(inode, 0);
1232
1233 brelse(di_bh);
1234
1235 return ret;
1236}
1237
1238static int __ocfs2_xattr_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001239 handle_t *handle,
Tao Ma492a8a32009-08-18 11:43:17 +08001240 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001241 const void *value,
1242 int value_len)
1243{
Tao Ma71d548a2008-12-05 06:20:54 +08001244 int ret = 0, i, cp_len;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001245 u16 blocksize = inode->i_sb->s_blocksize;
1246 u32 p_cluster, num_clusters;
1247 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1248 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1249 u64 blkno;
1250 struct buffer_head *bh = NULL;
Tao Ma492a8a32009-08-18 11:43:17 +08001251 unsigned int ext_flags;
1252 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001253
1254 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1255
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001256 while (cpos < clusters) {
1257 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001258 &num_clusters, &xv->xr_list,
Tao Ma492a8a32009-08-18 11:43:17 +08001259 &ext_flags);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001260 if (ret) {
1261 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001262 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001263 }
1264
Tao Ma492a8a32009-08-18 11:43:17 +08001265 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1266
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001267 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1268
1269 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001270 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1271 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001272 if (ret) {
1273 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001274 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001275 }
1276
1277 ret = ocfs2_journal_access(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001278 INODE_CACHE(inode),
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001279 bh,
1280 OCFS2_JOURNAL_ACCESS_WRITE);
1281 if (ret < 0) {
1282 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001283 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001284 }
1285
1286 cp_len = value_len > blocksize ? blocksize : value_len;
1287 memcpy(bh->b_data, value, cp_len);
1288 value_len -= cp_len;
1289 value += cp_len;
1290 if (cp_len < blocksize)
1291 memset(bh->b_data + cp_len, 0,
1292 blocksize - cp_len);
1293
1294 ret = ocfs2_journal_dirty(handle, bh);
1295 if (ret < 0) {
1296 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001297 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001298 }
1299 brelse(bh);
1300 bh = NULL;
1301
1302 /*
1303 * XXX: do we need to empty all the following
1304 * blocks in this cluster?
1305 */
1306 if (!value_len)
1307 break;
1308 }
1309 cpos += num_clusters;
1310 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001311out:
1312 brelse(bh);
1313
1314 return ret;
1315}
1316
1317static int ocfs2_xattr_cleanup(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001318 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001319 struct ocfs2_xattr_info *xi,
1320 struct ocfs2_xattr_search *xs,
Joel Becker512620f2008-12-09 15:58:35 -08001321 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001322 size_t offs)
1323{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001324 int ret = 0;
1325 size_t name_len = strlen(xi->name);
1326 void *val = xs->base + offs;
1327 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1328
Joel Becker0cf2f762009-02-12 16:41:25 -08001329 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker512620f2008-12-09 15:58:35 -08001330 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001331 if (ret) {
1332 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001333 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001334 }
1335 /* Decrease xattr count */
1336 le16_add_cpu(&xs->header->xh_count, -1);
1337 /* Remove the xattr entry and tree root which has already be set*/
1338 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1339 memset(val, 0, size);
1340
Joel Becker512620f2008-12-09 15:58:35 -08001341 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001342 if (ret < 0)
1343 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001344out:
1345 return ret;
1346}
1347
1348static int ocfs2_xattr_update_entry(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001349 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001350 struct ocfs2_xattr_info *xi,
1351 struct ocfs2_xattr_search *xs,
Joel Becker0c748e92008-12-09 15:46:15 -08001352 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001353 size_t offs)
1354{
Tao Ma85db90e2008-11-12 08:27:01 +08001355 int ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001356
Joel Becker0cf2f762009-02-12 16:41:25 -08001357 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker0c748e92008-12-09 15:46:15 -08001358 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001359 if (ret) {
1360 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001361 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001362 }
1363
1364 xs->here->xe_name_offset = cpu_to_le16(offs);
1365 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1366 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1367 ocfs2_xattr_set_local(xs->here, 1);
1368 else
1369 ocfs2_xattr_set_local(xs->here, 0);
1370 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1371
Joel Becker0c748e92008-12-09 15:46:15 -08001372 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001373 if (ret < 0)
1374 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001375out:
1376 return ret;
1377}
1378
1379/*
1380 * ocfs2_xattr_set_value_outside()
1381 *
1382 * Set large size value in B tree.
1383 */
1384static int ocfs2_xattr_set_value_outside(struct inode *inode,
1385 struct ocfs2_xattr_info *xi,
1386 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001387 struct ocfs2_xattr_set_ctxt *ctxt,
Joel Becker512620f2008-12-09 15:58:35 -08001388 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001389 size_t offs)
1390{
1391 size_t name_len = strlen(xi->name);
1392 void *val = xs->base + offs;
1393 struct ocfs2_xattr_value_root *xv = NULL;
1394 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1395 int ret = 0;
1396
1397 memset(val, 0, size);
1398 memcpy(val, xi->name, name_len);
1399 xv = (struct ocfs2_xattr_value_root *)
1400 (val + OCFS2_XATTR_SIZE(name_len));
1401 xv->xr_clusters = 0;
1402 xv->xr_last_eb_blk = 0;
1403 xv->xr_list.l_tree_depth = 0;
1404 xv->xr_list.l_count = cpu_to_le16(1);
1405 xv->xr_list.l_next_free_rec = 0;
Joel Becker512620f2008-12-09 15:58:35 -08001406 vb->vb_xv = xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001407
Joel Becker512620f2008-12-09 15:58:35 -08001408 ret = ocfs2_xattr_value_truncate(inode, vb, xi->value_len, ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001409 if (ret < 0) {
1410 mlog_errno(ret);
1411 return ret;
1412 }
Joel Becker512620f2008-12-09 15:58:35 -08001413 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001414 if (ret < 0) {
1415 mlog_errno(ret);
1416 return ret;
1417 }
Tao Ma492a8a32009-08-18 11:43:17 +08001418 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb,
Tao Ma85db90e2008-11-12 08:27:01 +08001419 xi->value, xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001420 if (ret < 0)
1421 mlog_errno(ret);
1422
1423 return ret;
1424}
1425
1426/*
1427 * ocfs2_xattr_set_entry_local()
1428 *
1429 * Set, replace or remove extended attribute in local.
1430 */
1431static void ocfs2_xattr_set_entry_local(struct inode *inode,
1432 struct ocfs2_xattr_info *xi,
1433 struct ocfs2_xattr_search *xs,
1434 struct ocfs2_xattr_entry *last,
1435 size_t min_offs)
1436{
1437 size_t name_len = strlen(xi->name);
1438 int i;
1439
1440 if (xi->value && xs->not_found) {
1441 /* Insert the new xattr entry. */
1442 le16_add_cpu(&xs->header->xh_count, 1);
1443 ocfs2_xattr_set_type(last, xi->name_index);
1444 ocfs2_xattr_set_local(last, 1);
1445 last->xe_name_len = name_len;
1446 } else {
1447 void *first_val;
1448 void *val;
1449 size_t offs, size;
1450
1451 first_val = xs->base + min_offs;
1452 offs = le16_to_cpu(xs->here->xe_name_offset);
1453 val = xs->base + offs;
1454
1455 if (le64_to_cpu(xs->here->xe_value_size) >
1456 OCFS2_XATTR_INLINE_SIZE)
1457 size = OCFS2_XATTR_SIZE(name_len) +
1458 OCFS2_XATTR_ROOT_SIZE;
1459 else
1460 size = OCFS2_XATTR_SIZE(name_len) +
1461 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1462
1463 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1464 OCFS2_XATTR_SIZE(xi->value_len)) {
1465 /* The old and the new value have the
1466 same size. Just replace the value. */
1467 ocfs2_xattr_set_local(xs->here, 1);
1468 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1469 /* Clear value bytes. */
1470 memset(val + OCFS2_XATTR_SIZE(name_len),
1471 0,
1472 OCFS2_XATTR_SIZE(xi->value_len));
1473 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1474 xi->value,
1475 xi->value_len);
1476 return;
1477 }
1478 /* Remove the old name+value. */
1479 memmove(first_val + size, first_val, val - first_val);
1480 memset(first_val, 0, size);
1481 xs->here->xe_name_hash = 0;
1482 xs->here->xe_name_offset = 0;
1483 ocfs2_xattr_set_local(xs->here, 1);
1484 xs->here->xe_value_size = 0;
1485
1486 min_offs += size;
1487
1488 /* Adjust all value offsets. */
1489 last = xs->header->xh_entries;
1490 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1491 size_t o = le16_to_cpu(last->xe_name_offset);
1492
1493 if (o < offs)
1494 last->xe_name_offset = cpu_to_le16(o + size);
1495 last += 1;
1496 }
1497
1498 if (!xi->value) {
1499 /* Remove the old entry. */
1500 last -= 1;
1501 memmove(xs->here, xs->here + 1,
1502 (void *)last - (void *)xs->here);
1503 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1504 le16_add_cpu(&xs->header->xh_count, -1);
1505 }
1506 }
1507 if (xi->value) {
1508 /* Insert the new name+value. */
1509 size_t size = OCFS2_XATTR_SIZE(name_len) +
1510 OCFS2_XATTR_SIZE(xi->value_len);
1511 void *val = xs->base + min_offs - size;
1512
1513 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1514 memset(val, 0, size);
1515 memcpy(val, xi->name, name_len);
1516 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1517 xi->value,
1518 xi->value_len);
1519 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1520 ocfs2_xattr_set_local(xs->here, 1);
1521 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1522 }
1523
1524 return;
1525}
1526
1527/*
1528 * ocfs2_xattr_set_entry()
1529 *
1530 * Set extended attribute entry into inode or block.
1531 *
1532 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1533 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1534 * then set value in B tree with set_value_outside().
1535 */
1536static int ocfs2_xattr_set_entry(struct inode *inode,
1537 struct ocfs2_xattr_info *xi,
1538 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001539 struct ocfs2_xattr_set_ctxt *ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001540 int flag)
1541{
1542 struct ocfs2_xattr_entry *last;
1543 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1544 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1545 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1546 size_t size_l = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08001547 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001548 int free, i, ret;
1549 struct ocfs2_xattr_info xi_l = {
1550 .name_index = xi->name_index,
1551 .name = xi->name,
1552 .value = xi->value,
1553 .value_len = xi->value_len,
1554 };
Joel Becker512620f2008-12-09 15:58:35 -08001555 struct ocfs2_xattr_value_buf vb = {
1556 .vb_bh = xs->xattr_bh,
1557 .vb_access = ocfs2_journal_access_di,
1558 };
1559
1560 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1561 BUG_ON(xs->xattr_bh == xs->inode_bh);
1562 vb.vb_access = ocfs2_journal_access_xb;
1563 } else
1564 BUG_ON(xs->xattr_bh != xs->inode_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001565
1566 /* Compute min_offs, last and free space. */
1567 last = xs->header->xh_entries;
1568
1569 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1570 size_t offs = le16_to_cpu(last->xe_name_offset);
1571 if (offs < min_offs)
1572 min_offs = offs;
1573 last += 1;
1574 }
1575
Tiger Yang4442f512009-02-20 11:11:50 +08001576 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001577 if (free < 0)
Joel Beckerb37c4d82008-10-20 18:24:03 -07001578 return -EIO;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001579
1580 if (!xs->not_found) {
1581 size_t size = 0;
1582 if (ocfs2_xattr_is_local(xs->here))
1583 size = OCFS2_XATTR_SIZE(name_len) +
1584 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1585 else
1586 size = OCFS2_XATTR_SIZE(name_len) +
1587 OCFS2_XATTR_ROOT_SIZE;
1588 free += (size + sizeof(struct ocfs2_xattr_entry));
1589 }
1590 /* Check free space in inode or block */
1591 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1592 if (free < sizeof(struct ocfs2_xattr_entry) +
1593 OCFS2_XATTR_SIZE(name_len) +
1594 OCFS2_XATTR_ROOT_SIZE) {
1595 ret = -ENOSPC;
1596 goto out;
1597 }
1598 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1599 xi_l.value = (void *)&def_xv;
1600 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1601 } else if (xi->value) {
1602 if (free < sizeof(struct ocfs2_xattr_entry) +
1603 OCFS2_XATTR_SIZE(name_len) +
1604 OCFS2_XATTR_SIZE(xi->value_len)) {
1605 ret = -ENOSPC;
1606 goto out;
1607 }
1608 }
1609
1610 if (!xs->not_found) {
1611 /* For existing extended attribute */
1612 size_t size = OCFS2_XATTR_SIZE(name_len) +
1613 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1614 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1615 void *val = xs->base + offs;
1616
1617 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1618 /* Replace existing local xattr with tree root */
1619 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
Joel Becker512620f2008-12-09 15:58:35 -08001620 ctxt, &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001621 if (ret < 0)
1622 mlog_errno(ret);
1623 goto out;
1624 } else if (!ocfs2_xattr_is_local(xs->here)) {
1625 /* For existing xattr which has value outside */
Joel Becker512620f2008-12-09 15:58:35 -08001626 vb.vb_xv = (struct ocfs2_xattr_value_root *)
1627 (val + OCFS2_XATTR_SIZE(name_len));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001628
1629 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1630 /*
1631 * If new value need set outside also,
1632 * first truncate old value to new value,
1633 * then set new value with set_value_outside().
1634 */
1635 ret = ocfs2_xattr_value_truncate(inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -08001636 &vb,
Tao Ma78f30c32008-11-12 08:27:00 +08001637 xi->value_len,
1638 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001639 if (ret < 0) {
1640 mlog_errno(ret);
1641 goto out;
1642 }
1643
Tao Ma85db90e2008-11-12 08:27:01 +08001644 ret = ocfs2_xattr_update_entry(inode,
1645 handle,
1646 xi,
1647 xs,
Joel Becker0c748e92008-12-09 15:46:15 -08001648 &vb,
Tao Ma85db90e2008-11-12 08:27:01 +08001649 offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001650 if (ret < 0) {
1651 mlog_errno(ret);
1652 goto out;
1653 }
1654
Tao Ma85db90e2008-11-12 08:27:01 +08001655 ret = __ocfs2_xattr_set_value_outside(inode,
1656 handle,
Tao Ma492a8a32009-08-18 11:43:17 +08001657 &vb,
Tao Ma85db90e2008-11-12 08:27:01 +08001658 xi->value,
1659 xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001660 if (ret < 0)
1661 mlog_errno(ret);
1662 goto out;
1663 } else {
1664 /*
1665 * If new value need set in local,
1666 * just trucate old value to zero.
1667 */
1668 ret = ocfs2_xattr_value_truncate(inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -08001669 &vb,
Tao Ma85db90e2008-11-12 08:27:01 +08001670 0,
1671 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001672 if (ret < 0)
1673 mlog_errno(ret);
1674 }
1675 }
1676 }
1677
Joel Becker0cf2f762009-02-12 16:41:25 -08001678 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
Joel Becker512620f2008-12-09 15:58:35 -08001679 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001680 if (ret) {
1681 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001682 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001683 }
1684
1685 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
Joel Becker0cf2f762009-02-12 16:41:25 -08001686 ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
Joel Becker512620f2008-12-09 15:58:35 -08001687 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001688 if (ret) {
1689 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001690 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001691 }
1692 }
1693
1694 /*
1695 * Set value in local, include set tree root in local.
1696 * This is the first step for value size >INLINE_SIZE.
1697 */
1698 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1699
1700 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1701 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1702 if (ret < 0) {
1703 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001704 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001705 }
1706 }
1707
1708 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1709 (flag & OCFS2_INLINE_XATTR_FL)) {
1710 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1711 unsigned int xattrsize = osb->s_xattr_inline_size;
1712
1713 /*
1714 * Adjust extent record count or inline data size
1715 * to reserve space for extended attribute.
1716 */
1717 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1718 struct ocfs2_inline_data *idata = &di->id2.i_data;
1719 le16_add_cpu(&idata->id_count, -xattrsize);
1720 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1721 struct ocfs2_extent_list *el = &di->id2.i_list;
1722 le16_add_cpu(&el->l_count, -(xattrsize /
1723 sizeof(struct ocfs2_extent_rec)));
1724 }
1725 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1726 }
1727 /* Update xattr flag */
1728 spin_lock(&oi->ip_lock);
1729 oi->ip_dyn_features |= flag;
1730 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1731 spin_unlock(&oi->ip_lock);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001732
1733 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1734 if (ret < 0)
1735 mlog_errno(ret);
1736
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001737 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1738 /*
1739 * Set value outside in B tree.
1740 * This is the second step for value size > INLINE_SIZE.
1741 */
1742 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
Joel Becker512620f2008-12-09 15:58:35 -08001743 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
1744 &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001745 if (ret < 0) {
1746 int ret2;
1747
1748 mlog_errno(ret);
1749 /*
1750 * If set value outside failed, we have to clean
1751 * the junk tree root we have already set in local.
1752 */
Tao Ma85db90e2008-11-12 08:27:01 +08001753 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
Joel Becker512620f2008-12-09 15:58:35 -08001754 xi, xs, &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001755 if (ret2 < 0)
1756 mlog_errno(ret2);
1757 }
1758 }
1759out:
1760 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001761}
1762
Tao Mace9c5a52009-08-18 11:43:59 +08001763/*
1764 * In xattr remove, if it is stored outside and refcounted, we may have
1765 * the chance to split the refcount tree. So need the allocators.
1766 */
1767static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
1768 struct ocfs2_xattr_value_root *xv,
1769 struct ocfs2_caching_info *ref_ci,
1770 struct buffer_head *ref_root_bh,
1771 struct ocfs2_alloc_context **meta_ac,
1772 int *ref_credits)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001773{
Tao Mace9c5a52009-08-18 11:43:59 +08001774 int ret, meta_add = 0;
1775 u32 p_cluster, num_clusters;
1776 unsigned int ext_flags;
Tao Ma78f30c32008-11-12 08:27:00 +08001777
Tao Mace9c5a52009-08-18 11:43:59 +08001778 *ref_credits = 0;
1779 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
1780 &num_clusters,
1781 &xv->xr_list,
1782 &ext_flags);
1783 if (ret) {
Tao Ma85db90e2008-11-12 08:27:01 +08001784 mlog_errno(ret);
1785 goto out;
1786 }
1787
Tao Mace9c5a52009-08-18 11:43:59 +08001788 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
1789 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001790
Tao Mace9c5a52009-08-18 11:43:59 +08001791 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
1792 ref_root_bh, xv,
1793 &meta_add, ref_credits);
1794 if (ret) {
1795 mlog_errno(ret);
1796 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001797 }
1798
Tao Mace9c5a52009-08-18 11:43:59 +08001799 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
1800 meta_add, meta_ac);
1801 if (ret)
1802 mlog_errno(ret);
1803
Tao Ma85db90e2008-11-12 08:27:01 +08001804out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001805 return ret;
1806}
1807
Tao Mace9c5a52009-08-18 11:43:59 +08001808static int ocfs2_remove_value_outside(struct inode*inode,
1809 struct ocfs2_xattr_value_buf *vb,
1810 struct ocfs2_xattr_header *header,
1811 struct ocfs2_caching_info *ref_ci,
1812 struct buffer_head *ref_root_bh)
1813{
1814 int ret = 0, i, ref_credits;
1815 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1816 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1817 void *val;
1818
1819 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
1820
1821 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1822 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1823
1824 if (ocfs2_xattr_is_local(entry))
1825 continue;
1826
1827 val = (void *)header +
1828 le16_to_cpu(entry->xe_name_offset);
1829 vb->vb_xv = (struct ocfs2_xattr_value_root *)
1830 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1831
1832 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
1833 ref_ci, ref_root_bh,
1834 &ctxt.meta_ac,
1835 &ref_credits);
1836
1837 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
1838 ocfs2_remove_extent_credits(osb->sb));
1839 if (IS_ERR(ctxt.handle)) {
1840 ret = PTR_ERR(ctxt.handle);
1841 mlog_errno(ret);
1842 break;
1843 }
1844
1845 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
1846 if (ret < 0) {
1847 mlog_errno(ret);
1848 break;
1849 }
1850
1851 ocfs2_commit_trans(osb, ctxt.handle);
1852 if (ctxt.meta_ac) {
1853 ocfs2_free_alloc_context(ctxt.meta_ac);
1854 ctxt.meta_ac = NULL;
1855 }
1856 }
1857
1858 if (ctxt.meta_ac)
1859 ocfs2_free_alloc_context(ctxt.meta_ac);
1860 ocfs2_schedule_truncate_log_flush(osb, 1);
1861 ocfs2_run_deallocs(osb, &ctxt.dealloc);
1862 return ret;
1863}
1864
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001865static int ocfs2_xattr_ibody_remove(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08001866 struct buffer_head *di_bh,
1867 struct ocfs2_caching_info *ref_ci,
1868 struct buffer_head *ref_root_bh)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001869{
1870
1871 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1872 struct ocfs2_xattr_header *header;
1873 int ret;
Joel Becker43119012008-12-09 16:24:43 -08001874 struct ocfs2_xattr_value_buf vb = {
1875 .vb_bh = di_bh,
1876 .vb_access = ocfs2_journal_access_di,
1877 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001878
1879 header = (struct ocfs2_xattr_header *)
1880 ((void *)di + inode->i_sb->s_blocksize -
1881 le16_to_cpu(di->i_xattr_inline_size));
1882
Tao Mace9c5a52009-08-18 11:43:59 +08001883 ret = ocfs2_remove_value_outside(inode, &vb, header,
1884 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001885
1886 return ret;
1887}
1888
Tao Mace9c5a52009-08-18 11:43:59 +08001889struct ocfs2_rm_xattr_bucket_para {
1890 struct ocfs2_caching_info *ref_ci;
1891 struct buffer_head *ref_root_bh;
1892};
1893
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001894static int ocfs2_xattr_block_remove(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08001895 struct buffer_head *blk_bh,
1896 struct ocfs2_caching_info *ref_ci,
1897 struct buffer_head *ref_root_bh)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001898{
1899 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001900 int ret = 0;
Joel Becker43119012008-12-09 16:24:43 -08001901 struct ocfs2_xattr_value_buf vb = {
1902 .vb_bh = blk_bh,
1903 .vb_access = ocfs2_journal_access_xb,
1904 };
Tao Mace9c5a52009-08-18 11:43:59 +08001905 struct ocfs2_rm_xattr_bucket_para args = {
1906 .ref_ci = ref_ci,
1907 .ref_root_bh = ref_root_bh,
1908 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001909
1910 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08001911 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1912 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
Tao Mace9c5a52009-08-18 11:43:59 +08001913 ret = ocfs2_remove_value_outside(inode, &vb, header,
1914 ref_ci, ref_root_bh);
Tao Maa3944252008-08-18 17:38:54 +08001915 } else
Tao Ma47bca492009-08-18 11:43:42 +08001916 ret = ocfs2_iterate_xattr_index_block(inode,
1917 blk_bh,
1918 ocfs2_rm_xattr_cluster,
Tao Mace9c5a52009-08-18 11:43:59 +08001919 &args);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001920
1921 return ret;
1922}
1923
Tao Ma08413892008-08-29 09:00:19 +08001924static int ocfs2_xattr_free_block(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08001925 u64 block,
1926 struct ocfs2_caching_info *ref_ci,
1927 struct buffer_head *ref_root_bh)
Tao Ma08413892008-08-29 09:00:19 +08001928{
1929 struct inode *xb_alloc_inode;
1930 struct buffer_head *xb_alloc_bh = NULL;
1931 struct buffer_head *blk_bh = NULL;
1932 struct ocfs2_xattr_block *xb;
1933 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1934 handle_t *handle;
1935 int ret = 0;
1936 u64 blk, bg_blkno;
1937 u16 bit;
1938
Joel Becker4ae1d692008-11-13 14:49:18 -08001939 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08001940 if (ret < 0) {
1941 mlog_errno(ret);
1942 goto out;
1943 }
1944
Tao Mace9c5a52009-08-18 11:43:59 +08001945 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
Tao Ma08413892008-08-29 09:00:19 +08001946 if (ret < 0) {
1947 mlog_errno(ret);
1948 goto out;
1949 }
1950
Joel Becker4ae1d692008-11-13 14:49:18 -08001951 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma08413892008-08-29 09:00:19 +08001952 blk = le64_to_cpu(xb->xb_blkno);
1953 bit = le16_to_cpu(xb->xb_suballoc_bit);
1954 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1955
1956 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1957 EXTENT_ALLOC_SYSTEM_INODE,
1958 le16_to_cpu(xb->xb_suballoc_slot));
1959 if (!xb_alloc_inode) {
1960 ret = -ENOMEM;
1961 mlog_errno(ret);
1962 goto out;
1963 }
1964 mutex_lock(&xb_alloc_inode->i_mutex);
1965
1966 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1967 if (ret < 0) {
1968 mlog_errno(ret);
1969 goto out_mutex;
1970 }
1971
1972 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1973 if (IS_ERR(handle)) {
1974 ret = PTR_ERR(handle);
1975 mlog_errno(ret);
1976 goto out_unlock;
1977 }
1978
1979 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1980 bit, bg_blkno, 1);
1981 if (ret < 0)
1982 mlog_errno(ret);
1983
1984 ocfs2_commit_trans(osb, handle);
1985out_unlock:
1986 ocfs2_inode_unlock(xb_alloc_inode, 1);
1987 brelse(xb_alloc_bh);
1988out_mutex:
1989 mutex_unlock(&xb_alloc_inode->i_mutex);
1990 iput(xb_alloc_inode);
1991out:
1992 brelse(blk_bh);
1993 return ret;
1994}
1995
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001996/*
1997 * ocfs2_xattr_remove()
1998 *
1999 * Free extended attribute resources associated with this inode.
2000 */
2001int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2002{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002003 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2004 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Tao Mace9c5a52009-08-18 11:43:59 +08002005 struct ocfs2_refcount_tree *ref_tree = NULL;
2006 struct buffer_head *ref_root_bh = NULL;
2007 struct ocfs2_caching_info *ref_ci = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002008 handle_t *handle;
2009 int ret;
2010
Tiger Yang8154da32008-08-18 17:11:46 +08002011 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2012 return 0;
2013
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002014 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2015 return 0;
2016
Tao Mace9c5a52009-08-18 11:43:59 +08002017 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2018 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2019 le64_to_cpu(di->i_refcount_loc),
2020 1, &ref_tree, &ref_root_bh);
2021 if (ret) {
2022 mlog_errno(ret);
2023 goto out;
2024 }
2025 ref_ci = &ref_tree->rf_ci;
2026
2027 }
2028
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002029 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
Tao Mace9c5a52009-08-18 11:43:59 +08002030 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2031 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002032 if (ret < 0) {
2033 mlog_errno(ret);
2034 goto out;
2035 }
2036 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002037
Tao Ma08413892008-08-29 09:00:19 +08002038 if (di->i_xattr_loc) {
2039 ret = ocfs2_xattr_free_block(inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002040 le64_to_cpu(di->i_xattr_loc),
2041 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002042 if (ret < 0) {
2043 mlog_errno(ret);
2044 goto out;
2045 }
2046 }
2047
2048 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2049 OCFS2_INODE_UPDATE_CREDITS);
2050 if (IS_ERR(handle)) {
2051 ret = PTR_ERR(handle);
2052 mlog_errno(ret);
2053 goto out;
2054 }
Joel Becker0cf2f762009-02-12 16:41:25 -08002055 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker84008972008-12-09 16:11:49 -08002056 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002057 if (ret) {
2058 mlog_errno(ret);
2059 goto out_commit;
2060 }
2061
Tao Ma08413892008-08-29 09:00:19 +08002062 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002063
2064 spin_lock(&oi->ip_lock);
2065 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2066 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2067 spin_unlock(&oi->ip_lock);
2068
2069 ret = ocfs2_journal_dirty(handle, di_bh);
2070 if (ret < 0)
2071 mlog_errno(ret);
2072out_commit:
2073 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2074out:
Tao Mace9c5a52009-08-18 11:43:59 +08002075 if (ref_tree)
2076 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2077 brelse(ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002078 return ret;
2079}
2080
2081static int ocfs2_xattr_has_space_inline(struct inode *inode,
2082 struct ocfs2_dinode *di)
2083{
2084 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2085 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2086 int free;
2087
2088 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2089 return 0;
2090
2091 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2092 struct ocfs2_inline_data *idata = &di->id2.i_data;
2093 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2094 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2095 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2096 le64_to_cpu(di->i_size);
2097 } else {
2098 struct ocfs2_extent_list *el = &di->id2.i_list;
2099 free = (le16_to_cpu(el->l_count) -
2100 le16_to_cpu(el->l_next_free_rec)) *
2101 sizeof(struct ocfs2_extent_rec);
2102 }
2103 if (free >= xattrsize)
2104 return 1;
2105
2106 return 0;
2107}
2108
2109/*
2110 * ocfs2_xattr_ibody_find()
2111 *
2112 * Find extended attribute in inode block and
2113 * fill search info into struct ocfs2_xattr_search.
2114 */
2115static int ocfs2_xattr_ibody_find(struct inode *inode,
2116 int name_index,
2117 const char *name,
2118 struct ocfs2_xattr_search *xs)
2119{
2120 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2121 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2122 int ret;
2123 int has_space = 0;
2124
2125 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2126 return 0;
2127
2128 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2129 down_read(&oi->ip_alloc_sem);
2130 has_space = ocfs2_xattr_has_space_inline(inode, di);
2131 up_read(&oi->ip_alloc_sem);
2132 if (!has_space)
2133 return 0;
2134 }
2135
2136 xs->xattr_bh = xs->inode_bh;
2137 xs->end = (void *)di + inode->i_sb->s_blocksize;
2138 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2139 xs->header = (struct ocfs2_xattr_header *)
2140 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2141 else
2142 xs->header = (struct ocfs2_xattr_header *)
2143 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2144 xs->base = (void *)xs->header;
2145 xs->here = xs->header->xh_entries;
2146
2147 /* Find the named attribute. */
2148 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2149 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2150 if (ret && ret != -ENODATA)
2151 return ret;
2152 xs->not_found = ret;
2153 }
2154
2155 return 0;
2156}
2157
2158/*
2159 * ocfs2_xattr_ibody_set()
2160 *
2161 * Set, replace or remove an extended attribute into inode block.
2162 *
2163 */
2164static int ocfs2_xattr_ibody_set(struct inode *inode,
2165 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002166 struct ocfs2_xattr_search *xs,
2167 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002168{
2169 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2170 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2171 int ret;
2172
2173 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2174 return -ENOSPC;
2175
2176 down_write(&oi->ip_alloc_sem);
2177 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2178 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2179 ret = -ENOSPC;
2180 goto out;
2181 }
2182 }
2183
Tao Ma78f30c32008-11-12 08:27:00 +08002184 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002185 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2186out:
2187 up_write(&oi->ip_alloc_sem);
2188
2189 return ret;
2190}
2191
2192/*
2193 * ocfs2_xattr_block_find()
2194 *
2195 * Find extended attribute in external block and
2196 * fill search info into struct ocfs2_xattr_search.
2197 */
2198static int ocfs2_xattr_block_find(struct inode *inode,
2199 int name_index,
2200 const char *name,
2201 struct ocfs2_xattr_search *xs)
2202{
2203 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2204 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002205 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002206 int ret = 0;
2207
2208 if (!di->i_xattr_loc)
2209 return ret;
2210
Joel Becker4ae1d692008-11-13 14:49:18 -08002211 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2212 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002213 if (ret < 0) {
2214 mlog_errno(ret);
2215 return ret;
2216 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07002217
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002218 xs->xattr_bh = blk_bh;
Joel Becker4ae1d692008-11-13 14:49:18 -08002219 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002220
Tao Ma589dc262008-08-18 17:38:51 +08002221 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2222 xs->header = &xb->xb_attrs.xb_header;
2223 xs->base = (void *)xs->header;
2224 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2225 xs->here = xs->header->xh_entries;
2226
2227 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2228 } else
2229 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2230 name_index,
2231 name, xs);
2232
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002233 if (ret && ret != -ENODATA) {
2234 xs->xattr_bh = NULL;
2235 goto cleanup;
2236 }
2237 xs->not_found = ret;
2238 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002239cleanup:
2240 brelse(blk_bh);
2241
2242 return ret;
2243}
2244
Tao Ma5aea1f02009-08-18 11:43:24 +08002245static int ocfs2_create_xattr_block(handle_t *handle,
2246 struct inode *inode,
2247 struct buffer_head *inode_bh,
2248 struct ocfs2_alloc_context *meta_ac,
Tao Maa7fe7a32009-08-18 11:43:52 +08002249 struct buffer_head **ret_bh,
2250 int indexed)
Tao Ma5aea1f02009-08-18 11:43:24 +08002251{
2252 int ret;
2253 u16 suballoc_bit_start;
2254 u32 num_got;
2255 u64 first_blkno;
2256 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2257 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2258 struct buffer_head *new_bh = NULL;
2259 struct ocfs2_xattr_block *xblk;
2260
2261 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
2262 OCFS2_JOURNAL_ACCESS_CREATE);
2263 if (ret < 0) {
2264 mlog_errno(ret);
2265 goto end;
2266 }
2267
2268 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
2269 &suballoc_bit_start, &num_got,
2270 &first_blkno);
2271 if (ret < 0) {
2272 mlog_errno(ret);
2273 goto end;
2274 }
2275
2276 new_bh = sb_getblk(inode->i_sb, first_blkno);
2277 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2278
2279 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
2280 new_bh,
2281 OCFS2_JOURNAL_ACCESS_CREATE);
2282 if (ret < 0) {
2283 mlog_errno(ret);
2284 goto end;
2285 }
2286
2287 /* Initialize ocfs2_xattr_block */
2288 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2289 memset(xblk, 0, inode->i_sb->s_blocksize);
2290 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2291 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2292 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2293 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2294 xblk->xb_blkno = cpu_to_le64(first_blkno);
2295
Tao Maa7fe7a32009-08-18 11:43:52 +08002296 if (indexed) {
2297 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2298 xr->xt_clusters = cpu_to_le32(1);
2299 xr->xt_last_eb_blk = 0;
2300 xr->xt_list.l_tree_depth = 0;
2301 xr->xt_list.l_count = cpu_to_le16(
2302 ocfs2_xattr_recs_per_xb(inode->i_sb));
2303 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2304 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2305 }
2306
Tao Ma5aea1f02009-08-18 11:43:24 +08002307 ret = ocfs2_journal_dirty(handle, new_bh);
2308 if (ret < 0) {
2309 mlog_errno(ret);
2310 goto end;
2311 }
2312 di->i_xattr_loc = cpu_to_le64(first_blkno);
2313 ocfs2_journal_dirty(handle, inode_bh);
2314
2315 *ret_bh = new_bh;
2316 new_bh = NULL;
2317
2318end:
2319 brelse(new_bh);
2320 return ret;
2321}
2322
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002323/*
2324 * ocfs2_xattr_block_set()
2325 *
2326 * Set, replace or remove an extended attribute into external block.
2327 *
2328 */
2329static int ocfs2_xattr_block_set(struct inode *inode,
2330 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002331 struct ocfs2_xattr_search *xs,
2332 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002333{
2334 struct buffer_head *new_bh = NULL;
Tao Ma85db90e2008-11-12 08:27:01 +08002335 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002336 struct ocfs2_xattr_block *xblk = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002337 int ret;
2338
2339 if (!xs->xattr_bh) {
Tao Ma5aea1f02009-08-18 11:43:24 +08002340 ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
Tao Maa7fe7a32009-08-18 11:43:52 +08002341 ctxt->meta_ac, &new_bh, 0);
Tao Ma5aea1f02009-08-18 11:43:24 +08002342 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002343 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002344 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002345 }
2346
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002347 xs->xattr_bh = new_bh;
Tao Ma5aea1f02009-08-18 11:43:24 +08002348 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002349 xs->header = &xblk->xb_attrs.xb_header;
2350 xs->base = (void *)xs->header;
2351 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2352 xs->here = xs->header->xh_entries;
Tao Ma01225592008-08-18 17:38:53 +08002353 } else
2354 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2355
2356 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2357 /* Set extended attribute into external block */
Tao Ma78f30c32008-11-12 08:27:00 +08002358 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2359 OCFS2_HAS_XATTR_FL);
Tao Ma01225592008-08-18 17:38:53 +08002360 if (!ret || ret != -ENOSPC)
2361 goto end;
2362
Tao Ma78f30c32008-11-12 08:27:00 +08002363 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002364 if (ret)
2365 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002366 }
2367
Tao Ma78f30c32008-11-12 08:27:00 +08002368 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002369
2370end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002371
2372 return ret;
2373}
2374
Tao Ma78f30c32008-11-12 08:27:00 +08002375/* Check whether the new xattr can be inserted into the inode. */
2376static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2377 struct ocfs2_xattr_info *xi,
2378 struct ocfs2_xattr_search *xs)
2379{
2380 u64 value_size;
2381 struct ocfs2_xattr_entry *last;
2382 int free, i;
2383 size_t min_offs = xs->end - xs->base;
2384
2385 if (!xs->header)
2386 return 0;
2387
2388 last = xs->header->xh_entries;
2389
2390 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2391 size_t offs = le16_to_cpu(last->xe_name_offset);
2392 if (offs < min_offs)
2393 min_offs = offs;
2394 last += 1;
2395 }
2396
Tiger Yang4442f512009-02-20 11:11:50 +08002397 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
Tao Ma78f30c32008-11-12 08:27:00 +08002398 if (free < 0)
2399 return 0;
2400
2401 BUG_ON(!xs->not_found);
2402
2403 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2404 value_size = OCFS2_XATTR_ROOT_SIZE;
2405 else
2406 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2407
2408 if (free >= sizeof(struct ocfs2_xattr_entry) +
2409 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2410 return 1;
2411
2412 return 0;
2413}
2414
2415static int ocfs2_calc_xattr_set_need(struct inode *inode,
2416 struct ocfs2_dinode *di,
2417 struct ocfs2_xattr_info *xi,
2418 struct ocfs2_xattr_search *xis,
2419 struct ocfs2_xattr_search *xbs,
2420 int *clusters_need,
Tao Ma85db90e2008-11-12 08:27:01 +08002421 int *meta_need,
2422 int *credits_need)
Tao Ma78f30c32008-11-12 08:27:00 +08002423{
2424 int ret = 0, old_in_xb = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08002425 int clusters_add = 0, meta_add = 0, credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002426 struct buffer_head *bh = NULL;
2427 struct ocfs2_xattr_block *xb = NULL;
2428 struct ocfs2_xattr_entry *xe = NULL;
2429 struct ocfs2_xattr_value_root *xv = NULL;
2430 char *base = NULL;
2431 int name_offset, name_len = 0;
2432 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2433 xi->value_len);
2434 u64 value_size;
2435
Tao Ma71d548a2008-12-05 06:20:54 +08002436 /*
2437 * Calculate the clusters we need to write.
2438 * No matter whether we replace an old one or add a new one,
2439 * we need this for writing.
2440 */
2441 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2442 credits += new_clusters *
2443 ocfs2_clusters_to_blocks(inode->i_sb, 1);
2444
Tao Ma78f30c32008-11-12 08:27:00 +08002445 if (xis->not_found && xbs->not_found) {
Tao Ma85db90e2008-11-12 08:27:01 +08002446 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2447
2448 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08002449 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002450 credits += ocfs2_calc_extend_credits(inode->i_sb,
2451 &def_xv.xv.xr_list,
2452 new_clusters);
2453 }
Tao Ma78f30c32008-11-12 08:27:00 +08002454
2455 goto meta_guess;
2456 }
2457
2458 if (!xis->not_found) {
2459 xe = xis->here;
2460 name_offset = le16_to_cpu(xe->xe_name_offset);
2461 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2462 base = xis->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002463 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08002464 } else {
Joel Becker970e4932008-11-13 14:49:19 -08002465 int i, block_off = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002466 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2467 xe = xbs->here;
2468 name_offset = le16_to_cpu(xe->xe_name_offset);
2469 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2470 i = xbs->here - xbs->header->xh_entries;
2471 old_in_xb = 1;
2472
2473 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08002474 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma78f30c32008-11-12 08:27:00 +08002475 bucket_xh(xbs->bucket),
2476 i, &block_off,
2477 &name_offset);
2478 base = bucket_block(xbs->bucket, block_off);
Tao Ma85db90e2008-11-12 08:27:01 +08002479 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2480 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002481 base = xbs->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002482 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2483 }
2484 }
2485
2486 /*
2487 * delete a xattr doesn't need metadata and cluster allocation.
2488 * so just calculate the credits and return.
2489 *
2490 * The credits for removing the value tree will be extended
2491 * by ocfs2_remove_extent itself.
2492 */
2493 if (!xi->value) {
2494 if (!ocfs2_xattr_is_local(xe))
Jan Karaa90714c2008-10-09 19:38:40 +02002495 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002496
2497 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002498 }
2499
2500 /* do cluster allocation guess first. */
2501 value_size = le64_to_cpu(xe->xe_value_size);
2502
2503 if (old_in_xb) {
2504 /*
2505 * In xattr set, we always try to set the xe in inode first,
2506 * so if it can be inserted into inode successfully, the old
2507 * one will be removed from the xattr block, and this xattr
2508 * will be inserted into inode as a new xattr in inode.
2509 */
2510 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2511 clusters_add += new_clusters;
Jan Karaa90714c2008-10-09 19:38:40 +02002512 credits += ocfs2_remove_extent_credits(inode->i_sb) +
Tao Ma85db90e2008-11-12 08:27:01 +08002513 OCFS2_INODE_UPDATE_CREDITS;
2514 if (!ocfs2_xattr_is_local(xe))
2515 credits += ocfs2_calc_extend_credits(
2516 inode->i_sb,
2517 &def_xv.xv.xr_list,
2518 new_clusters);
Tao Ma78f30c32008-11-12 08:27:00 +08002519 goto out;
2520 }
2521 }
2522
2523 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2524 /* the new values will be stored outside. */
2525 u32 old_clusters = 0;
2526
2527 if (!ocfs2_xattr_is_local(xe)) {
2528 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2529 value_size);
2530 xv = (struct ocfs2_xattr_value_root *)
2531 (base + name_offset + name_len);
Tao Ma97aff522008-11-19 16:48:41 +08002532 value_size = OCFS2_XATTR_ROOT_SIZE;
Tao Ma78f30c32008-11-12 08:27:00 +08002533 } else
2534 xv = &def_xv.xv;
2535
Tao Ma85db90e2008-11-12 08:27:01 +08002536 if (old_clusters >= new_clusters) {
Jan Karaa90714c2008-10-09 19:38:40 +02002537 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002538 goto out;
Tao Ma85db90e2008-11-12 08:27:01 +08002539 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002540 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2541 clusters_add += new_clusters - old_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002542 credits += ocfs2_calc_extend_credits(inode->i_sb,
2543 &xv->xr_list,
2544 new_clusters -
2545 old_clusters);
Tao Ma97aff522008-11-19 16:48:41 +08002546 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
2547 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002548 }
2549 } else {
2550 /*
2551 * Now the new value will be stored inside. So if the new
2552 * value is smaller than the size of value root or the old
2553 * value, we don't need any allocation, otherwise we have
2554 * to guess metadata allocation.
2555 */
2556 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2557 (!ocfs2_xattr_is_local(xe) &&
2558 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2559 goto out;
2560 }
2561
2562meta_guess:
2563 /* calculate metadata allocation. */
2564 if (di->i_xattr_loc) {
2565 if (!xbs->xattr_bh) {
Joel Becker4ae1d692008-11-13 14:49:18 -08002566 ret = ocfs2_read_xattr_block(inode,
2567 le64_to_cpu(di->i_xattr_loc),
2568 &bh);
Tao Ma78f30c32008-11-12 08:27:00 +08002569 if (ret) {
2570 mlog_errno(ret);
2571 goto out;
2572 }
2573
2574 xb = (struct ocfs2_xattr_block *)bh->b_data;
2575 } else
2576 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2577
Tao Ma90cb5462008-12-05 06:20:56 +08002578 /*
2579 * If there is already an xattr tree, good, we can calculate
2580 * like other b-trees. Otherwise we may have the chance of
2581 * create a tree, the credit calculation is borrowed from
2582 * ocfs2_calc_extend_credits with root_el = NULL. And the
2583 * new tree will be cluster based, so no meta is needed.
2584 */
Tao Ma78f30c32008-11-12 08:27:00 +08002585 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2586 struct ocfs2_extent_list *el =
2587 &xb->xb_attrs.xb_root.xt_list;
2588 meta_add += ocfs2_extend_meta_needed(el);
Tao Ma85db90e2008-11-12 08:27:01 +08002589 credits += ocfs2_calc_extend_credits(inode->i_sb,
2590 el, 1);
Tao Ma90cb5462008-12-05 06:20:56 +08002591 } else
2592 credits += OCFS2_SUBALLOC_ALLOC + 1;
Tao Ma78f30c32008-11-12 08:27:00 +08002593
2594 /*
2595 * This cluster will be used either for new bucket or for
2596 * new xattr block.
2597 * If the cluster size is the same as the bucket size, one
2598 * more is needed since we may need to extend the bucket
2599 * also.
2600 */
2601 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002602 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002603 if (OCFS2_XATTR_BUCKET_SIZE ==
Tao Ma85db90e2008-11-12 08:27:01 +08002604 OCFS2_SB(inode->i_sb)->s_clustersize) {
2605 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002606 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002607 }
2608 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002609 meta_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002610 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2611 }
Tao Ma78f30c32008-11-12 08:27:00 +08002612out:
2613 if (clusters_need)
2614 *clusters_need = clusters_add;
2615 if (meta_need)
2616 *meta_need = meta_add;
Tao Ma85db90e2008-11-12 08:27:01 +08002617 if (credits_need)
2618 *credits_need = credits;
Tao Ma78f30c32008-11-12 08:27:00 +08002619 brelse(bh);
2620 return ret;
2621}
2622
2623static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2624 struct ocfs2_dinode *di,
2625 struct ocfs2_xattr_info *xi,
2626 struct ocfs2_xattr_search *xis,
2627 struct ocfs2_xattr_search *xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002628 struct ocfs2_xattr_set_ctxt *ctxt,
Tao Ma492a8a32009-08-18 11:43:17 +08002629 int extra_meta,
Tao Ma85db90e2008-11-12 08:27:01 +08002630 int *credits)
Tao Ma78f30c32008-11-12 08:27:00 +08002631{
2632 int clusters_add, meta_add, ret;
2633 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2634
2635 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2636
2637 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2638
2639 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002640 &clusters_add, &meta_add, credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002641 if (ret) {
2642 mlog_errno(ret);
2643 return ret;
2644 }
2645
Tao Ma492a8a32009-08-18 11:43:17 +08002646 meta_add += extra_meta;
Tao Ma85db90e2008-11-12 08:27:01 +08002647 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2648 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002649
2650 if (meta_add) {
2651 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2652 &ctxt->meta_ac);
2653 if (ret) {
2654 mlog_errno(ret);
2655 goto out;
2656 }
2657 }
2658
2659 if (clusters_add) {
2660 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2661 if (ret)
2662 mlog_errno(ret);
2663 }
2664out:
2665 if (ret) {
2666 if (ctxt->meta_ac) {
2667 ocfs2_free_alloc_context(ctxt->meta_ac);
2668 ctxt->meta_ac = NULL;
2669 }
2670
2671 /*
2672 * We cannot have an error and a non null ctxt->data_ac.
2673 */
2674 }
2675
2676 return ret;
2677}
2678
Tao Ma85db90e2008-11-12 08:27:01 +08002679static int __ocfs2_xattr_set_handle(struct inode *inode,
2680 struct ocfs2_dinode *di,
2681 struct ocfs2_xattr_info *xi,
2682 struct ocfs2_xattr_search *xis,
2683 struct ocfs2_xattr_search *xbs,
2684 struct ocfs2_xattr_set_ctxt *ctxt)
2685{
Tao Ma9f868f12008-11-19 16:48:42 +08002686 int ret = 0, credits, old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002687
2688 if (!xi->value) {
2689 /* Remove existing extended attribute */
2690 if (!xis->not_found)
2691 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2692 else if (!xbs->not_found)
2693 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2694 } else {
2695 /* We always try to set extended attribute into inode first*/
2696 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2697 if (!ret && !xbs->not_found) {
2698 /*
2699 * If succeed and that extended attribute existing in
2700 * external block, then we will remove it.
2701 */
2702 xi->value = NULL;
2703 xi->value_len = 0;
2704
Tao Ma9f868f12008-11-19 16:48:42 +08002705 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002706 xis->not_found = -ENODATA;
2707 ret = ocfs2_calc_xattr_set_need(inode,
2708 di,
2709 xi,
2710 xis,
2711 xbs,
2712 NULL,
2713 NULL,
2714 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08002715 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002716 if (ret) {
2717 mlog_errno(ret);
2718 goto out;
2719 }
2720
2721 ret = ocfs2_extend_trans(ctxt->handle, credits +
2722 ctxt->handle->h_buffer_credits);
2723 if (ret) {
2724 mlog_errno(ret);
2725 goto out;
2726 }
2727 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2728 } else if (ret == -ENOSPC) {
2729 if (di->i_xattr_loc && !xbs->xattr_bh) {
2730 ret = ocfs2_xattr_block_find(inode,
2731 xi->name_index,
2732 xi->name, xbs);
2733 if (ret)
2734 goto out;
2735
Tao Ma9f868f12008-11-19 16:48:42 +08002736 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002737 xis->not_found = -ENODATA;
2738 ret = ocfs2_calc_xattr_set_need(inode,
2739 di,
2740 xi,
2741 xis,
2742 xbs,
2743 NULL,
2744 NULL,
2745 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08002746 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002747 if (ret) {
2748 mlog_errno(ret);
2749 goto out;
2750 }
2751
2752 ret = ocfs2_extend_trans(ctxt->handle, credits +
2753 ctxt->handle->h_buffer_credits);
2754 if (ret) {
2755 mlog_errno(ret);
2756 goto out;
2757 }
2758 }
2759 /*
2760 * If no space in inode, we will set extended attribute
2761 * into external block.
2762 */
2763 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2764 if (ret)
2765 goto out;
2766 if (!xis->not_found) {
2767 /*
2768 * If succeed and that extended attribute
2769 * existing in inode, we will remove it.
2770 */
2771 xi->value = NULL;
2772 xi->value_len = 0;
2773 xbs->not_found = -ENODATA;
2774 ret = ocfs2_calc_xattr_set_need(inode,
2775 di,
2776 xi,
2777 xis,
2778 xbs,
2779 NULL,
2780 NULL,
2781 &credits);
2782 if (ret) {
2783 mlog_errno(ret);
2784 goto out;
2785 }
2786
2787 ret = ocfs2_extend_trans(ctxt->handle, credits +
2788 ctxt->handle->h_buffer_credits);
2789 if (ret) {
2790 mlog_errno(ret);
2791 goto out;
2792 }
2793 ret = ocfs2_xattr_ibody_set(inode, xi,
2794 xis, ctxt);
2795 }
2796 }
2797 }
2798
Tao Ma4b3f6202008-12-05 06:20:55 +08002799 if (!ret) {
2800 /* Update inode ctime. */
Joel Becker0cf2f762009-02-12 16:41:25 -08002801 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
Tao Ma89a907a2009-02-17 04:39:28 +08002802 xis->inode_bh,
2803 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma4b3f6202008-12-05 06:20:55 +08002804 if (ret) {
2805 mlog_errno(ret);
2806 goto out;
2807 }
2808
2809 inode->i_ctime = CURRENT_TIME;
2810 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
2811 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
2812 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
2813 }
Tao Ma85db90e2008-11-12 08:27:01 +08002814out:
2815 return ret;
2816}
2817
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002818/*
Tiger Yang6c3faba2008-11-14 11:16:03 +08002819 * This function only called duing creating inode
2820 * for init security/acl xattrs of the new inode.
Tiger Yang008aafa2008-12-09 16:43:08 +08002821 * All transanction credits have been reserved in mknod.
Tiger Yang6c3faba2008-11-14 11:16:03 +08002822 */
2823int ocfs2_xattr_set_handle(handle_t *handle,
2824 struct inode *inode,
2825 struct buffer_head *di_bh,
2826 int name_index,
2827 const char *name,
2828 const void *value,
2829 size_t value_len,
2830 int flags,
2831 struct ocfs2_alloc_context *meta_ac,
2832 struct ocfs2_alloc_context *data_ac)
2833{
2834 struct ocfs2_dinode *di;
2835 int ret;
2836
2837 struct ocfs2_xattr_info xi = {
2838 .name_index = name_index,
2839 .name = name,
2840 .value = value,
2841 .value_len = value_len,
2842 };
2843
2844 struct ocfs2_xattr_search xis = {
2845 .not_found = -ENODATA,
2846 };
2847
2848 struct ocfs2_xattr_search xbs = {
2849 .not_found = -ENODATA,
2850 };
2851
2852 struct ocfs2_xattr_set_ctxt ctxt = {
2853 .handle = handle,
2854 .meta_ac = meta_ac,
2855 .data_ac = data_ac,
2856 };
2857
2858 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2859 return -EOPNOTSUPP;
2860
Tiger Yang008aafa2008-12-09 16:43:08 +08002861 /*
2862 * In extreme situation, may need xattr bucket when
2863 * block size is too small. And we have already reserved
2864 * the credits for bucket in mknod.
2865 */
2866 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
2867 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2868 if (!xbs.bucket) {
2869 mlog_errno(-ENOMEM);
2870 return -ENOMEM;
2871 }
2872 }
2873
Tiger Yang6c3faba2008-11-14 11:16:03 +08002874 xis.inode_bh = xbs.inode_bh = di_bh;
2875 di = (struct ocfs2_dinode *)di_bh->b_data;
2876
2877 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2878
2879 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2880 if (ret)
2881 goto cleanup;
2882 if (xis.not_found) {
2883 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2884 if (ret)
2885 goto cleanup;
2886 }
2887
2888 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2889
2890cleanup:
2891 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2892 brelse(xbs.xattr_bh);
Tiger Yang008aafa2008-12-09 16:43:08 +08002893 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yang6c3faba2008-11-14 11:16:03 +08002894
2895 return ret;
2896}
2897
2898/*
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002899 * ocfs2_xattr_set()
2900 *
2901 * Set, replace or remove an extended attribute for this inode.
2902 * value is NULL to remove an existing extended attribute, else either
2903 * create or replace an extended attribute.
2904 */
2905int ocfs2_xattr_set(struct inode *inode,
2906 int name_index,
2907 const char *name,
2908 const void *value,
2909 size_t value_len,
2910 int flags)
2911{
2912 struct buffer_head *di_bh = NULL;
2913 struct ocfs2_dinode *di;
Tao Ma492a8a32009-08-18 11:43:17 +08002914 int ret, credits, ref_meta = 0, ref_credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002915 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002916 struct inode *tl_inode = osb->osb_tl_inode;
Tao Ma78f30c32008-11-12 08:27:00 +08002917 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
Tao Ma492a8a32009-08-18 11:43:17 +08002918 struct ocfs2_refcount_tree *ref_tree = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002919
2920 struct ocfs2_xattr_info xi = {
2921 .name_index = name_index,
2922 .name = name,
2923 .value = value,
2924 .value_len = value_len,
2925 };
2926
2927 struct ocfs2_xattr_search xis = {
2928 .not_found = -ENODATA,
2929 };
2930
2931 struct ocfs2_xattr_search xbs = {
2932 .not_found = -ENODATA,
2933 };
2934
Tiger Yang8154da32008-08-18 17:11:46 +08002935 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2936 return -EOPNOTSUPP;
2937
Joel Beckerba937122008-10-24 19:13:20 -07002938 /*
2939 * Only xbs will be used on indexed trees. xis doesn't need a
2940 * bucket.
2941 */
2942 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2943 if (!xbs.bucket) {
2944 mlog_errno(-ENOMEM);
2945 return -ENOMEM;
2946 }
2947
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002948 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2949 if (ret < 0) {
2950 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002951 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002952 }
2953 xis.inode_bh = xbs.inode_bh = di_bh;
2954 di = (struct ocfs2_dinode *)di_bh->b_data;
2955
2956 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2957 /*
2958 * Scan inode and external block to find the same name
2959 * extended attribute and collect search infomation.
2960 */
2961 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2962 if (ret)
2963 goto cleanup;
2964 if (xis.not_found) {
2965 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2966 if (ret)
2967 goto cleanup;
2968 }
2969
2970 if (xis.not_found && xbs.not_found) {
2971 ret = -ENODATA;
2972 if (flags & XATTR_REPLACE)
2973 goto cleanup;
2974 ret = 0;
2975 if (!value)
2976 goto cleanup;
2977 } else {
2978 ret = -EEXIST;
2979 if (flags & XATTR_CREATE)
2980 goto cleanup;
2981 }
2982
Tao Ma492a8a32009-08-18 11:43:17 +08002983 /* Check whether the value is refcounted and do some prepartion. */
2984 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
2985 (!xis.not_found || !xbs.not_found)) {
2986 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
2987 &xis, &xbs, &ref_tree,
2988 &ref_meta, &ref_credits);
2989 if (ret) {
2990 mlog_errno(ret);
2991 goto cleanup;
2992 }
2993 }
Tao Ma85db90e2008-11-12 08:27:01 +08002994
2995 mutex_lock(&tl_inode->i_mutex);
2996
2997 if (ocfs2_truncate_log_needs_flush(osb)) {
2998 ret = __ocfs2_flush_truncate_log(osb);
2999 if (ret < 0) {
3000 mutex_unlock(&tl_inode->i_mutex);
3001 mlog_errno(ret);
3002 goto cleanup;
3003 }
3004 }
3005 mutex_unlock(&tl_inode->i_mutex);
3006
3007 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
Tao Ma492a8a32009-08-18 11:43:17 +08003008 &xbs, &ctxt, ref_meta, &credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003009 if (ret) {
3010 mlog_errno(ret);
3011 goto cleanup;
3012 }
3013
Tao Ma4b3f6202008-12-05 06:20:55 +08003014 /* we need to update inode's ctime field, so add credit for it. */
3015 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma492a8a32009-08-18 11:43:17 +08003016 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma85db90e2008-11-12 08:27:01 +08003017 if (IS_ERR(ctxt.handle)) {
3018 ret = PTR_ERR(ctxt.handle);
3019 mlog_errno(ret);
3020 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003021 }
Tao Ma85db90e2008-11-12 08:27:01 +08003022
3023 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3024
3025 ocfs2_commit_trans(osb, ctxt.handle);
3026
Tao Ma78f30c32008-11-12 08:27:00 +08003027 if (ctxt.data_ac)
3028 ocfs2_free_alloc_context(ctxt.data_ac);
3029 if (ctxt.meta_ac)
3030 ocfs2_free_alloc_context(ctxt.meta_ac);
3031 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3032 ocfs2_schedule_truncate_log_flush(osb, 1);
3033 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma8b2c0db2009-08-18 11:43:49 +08003034
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003035cleanup:
Tao Ma492a8a32009-08-18 11:43:17 +08003036 if (ref_tree)
3037 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003038 up_write(&OCFS2_I(inode)->ip_xattr_sem);
Tao Ma8b2c0db2009-08-18 11:43:49 +08003039 if (!value && !ret) {
3040 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3041 if (ret)
3042 mlog_errno(ret);
3043 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003044 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07003045cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003046 brelse(di_bh);
3047 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07003048 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003049
3050 return ret;
3051}
3052
Tao Ma0c044f02008-08-18 17:38:50 +08003053/*
3054 * Find the xattr extent rec which may contains name_hash.
3055 * e_cpos will be the first name hash of the xattr rec.
3056 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3057 */
3058static int ocfs2_xattr_get_rec(struct inode *inode,
3059 u32 name_hash,
3060 u64 *p_blkno,
3061 u32 *e_cpos,
3062 u32 *num_clusters,
3063 struct ocfs2_extent_list *el)
3064{
3065 int ret = 0, i;
3066 struct buffer_head *eb_bh = NULL;
3067 struct ocfs2_extent_block *eb;
3068 struct ocfs2_extent_rec *rec = NULL;
3069 u64 e_blkno = 0;
3070
3071 if (el->l_tree_depth) {
Joel Beckerfacdb772009-02-12 18:08:48 -08003072 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3073 &eb_bh);
Tao Ma0c044f02008-08-18 17:38:50 +08003074 if (ret) {
3075 mlog_errno(ret);
3076 goto out;
3077 }
3078
3079 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3080 el = &eb->h_list;
3081
3082 if (el->l_tree_depth) {
3083 ocfs2_error(inode->i_sb,
3084 "Inode %lu has non zero tree depth in "
3085 "xattr tree block %llu\n", inode->i_ino,
3086 (unsigned long long)eb_bh->b_blocknr);
3087 ret = -EROFS;
3088 goto out;
3089 }
3090 }
3091
3092 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3093 rec = &el->l_recs[i];
3094
3095 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3096 e_blkno = le64_to_cpu(rec->e_blkno);
3097 break;
3098 }
3099 }
3100
3101 if (!e_blkno) {
3102 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3103 "record (%u, %u, 0) in xattr", inode->i_ino,
3104 le32_to_cpu(rec->e_cpos),
3105 ocfs2_rec_clusters(el, rec));
3106 ret = -EROFS;
3107 goto out;
3108 }
3109
3110 *p_blkno = le64_to_cpu(rec->e_blkno);
3111 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3112 if (e_cpos)
3113 *e_cpos = le32_to_cpu(rec->e_cpos);
3114out:
3115 brelse(eb_bh);
3116 return ret;
3117}
3118
3119typedef int (xattr_bucket_func)(struct inode *inode,
3120 struct ocfs2_xattr_bucket *bucket,
3121 void *para);
3122
Tao Ma589dc262008-08-18 17:38:51 +08003123static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07003124 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08003125 int name_index,
3126 const char *name,
3127 u32 name_hash,
3128 u16 *xe_index,
3129 int *found)
3130{
3131 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07003132 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08003133 size_t name_len = strlen(name);
3134 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08003135 char *xe_name;
3136
3137 /*
3138 * We don't use binary search in the bucket because there
3139 * may be multiple entries with the same name hash.
3140 */
3141 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3142 xe = &xh->xh_entries[i];
3143
3144 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3145 continue;
3146 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3147 break;
3148
3149 cmp = name_index - ocfs2_xattr_get_type(xe);
3150 if (!cmp)
3151 cmp = name_len - xe->xe_name_len;
3152 if (cmp)
3153 continue;
3154
Tao Mafd68a892009-08-18 11:43:21 +08003155 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma589dc262008-08-18 17:38:51 +08003156 xh,
3157 i,
3158 &block_off,
3159 &new_offset);
3160 if (ret) {
3161 mlog_errno(ret);
3162 break;
3163 }
3164
Joel Becker970e4932008-11-13 14:49:19 -08003165
Joel Beckere2356a32008-10-27 15:01:54 -07003166 xe_name = bucket_block(bucket, block_off) + new_offset;
3167 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08003168 *xe_index = i;
3169 *found = 1;
3170 ret = 0;
3171 break;
3172 }
3173 }
3174
3175 return ret;
3176}
3177
3178/*
3179 * Find the specified xattr entry in a series of buckets.
3180 * This series start from p_blkno and last for num_clusters.
3181 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3182 * the num of the valid buckets.
3183 *
3184 * Return the buffer_head this xattr should reside in. And if the xattr's
3185 * hash is in the gap of 2 buckets, return the lower bucket.
3186 */
3187static int ocfs2_xattr_bucket_find(struct inode *inode,
3188 int name_index,
3189 const char *name,
3190 u32 name_hash,
3191 u64 p_blkno,
3192 u32 first_hash,
3193 u32 num_clusters,
3194 struct ocfs2_xattr_search *xs)
3195{
3196 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003197 struct ocfs2_xattr_header *xh = NULL;
3198 struct ocfs2_xattr_entry *xe = NULL;
3199 u16 index = 0;
3200 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3201 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003202 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08003203 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07003204 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003205
Joel Beckere2356a32008-10-27 15:01:54 -07003206 search = ocfs2_xattr_bucket_new(inode);
3207 if (!search) {
3208 ret = -ENOMEM;
3209 mlog_errno(ret);
3210 goto out;
3211 }
3212
3213 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003214 if (ret) {
3215 mlog_errno(ret);
3216 goto out;
3217 }
3218
Joel Beckere2356a32008-10-27 15:01:54 -07003219 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003220 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08003221 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07003222 ocfs2_xattr_bucket_relse(search);
3223
Tao Ma589dc262008-08-18 17:38:51 +08003224 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08003225 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003226 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003227 if (ret) {
3228 mlog_errno(ret);
3229 goto out;
3230 }
3231
Joel Beckere2356a32008-10-27 15:01:54 -07003232 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003233 xe = &xh->xh_entries[0];
3234 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3235 high_bucket = bucket - 1;
3236 continue;
3237 }
3238
3239 /*
3240 * Check whether the hash of the last entry in our
Tao Ma5a095612008-09-19 22:17:41 +08003241 * bucket is larger than the search one. for an empty
3242 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08003243 */
Tao Ma5a095612008-09-19 22:17:41 +08003244 if (xh->xh_count)
3245 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3246
Tao Ma589dc262008-08-18 17:38:51 +08003247 last_hash = le32_to_cpu(xe->xe_name_hash);
3248
Joel Beckere2356a32008-10-27 15:01:54 -07003249 /* record lower_blkno which may be the insert place. */
3250 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08003251
3252 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3253 low_bucket = bucket + 1;
3254 continue;
3255 }
3256
3257 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07003258 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08003259 name_index, name, name_hash,
3260 &index, &found);
3261 if (ret) {
3262 mlog_errno(ret);
3263 goto out;
3264 }
3265 break;
3266 }
3267
3268 /*
3269 * Record the bucket we have found.
3270 * When the xattr's hash value is in the gap of 2 buckets, we will
3271 * always set it to the previous bucket.
3272 */
Joel Beckere2356a32008-10-27 15:01:54 -07003273 if (!lower_blkno)
3274 lower_blkno = p_blkno;
3275
3276 /* This should be in cache - we just read it during the search */
3277 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3278 if (ret) {
3279 mlog_errno(ret);
3280 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08003281 }
Tao Ma589dc262008-08-18 17:38:51 +08003282
Joel Beckerba937122008-10-24 19:13:20 -07003283 xs->header = bucket_xh(xs->bucket);
3284 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08003285 xs->end = xs->base + inode->i_sb->s_blocksize;
3286
3287 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08003288 xs->here = &xs->header->xh_entries[index];
3289 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Beckerba937122008-10-24 19:13:20 -07003290 (unsigned long long)bucket_blkno(xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08003291 } else
3292 ret = -ENODATA;
3293
3294out:
Joel Beckere2356a32008-10-27 15:01:54 -07003295 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08003296 return ret;
3297}
3298
3299static int ocfs2_xattr_index_block_find(struct inode *inode,
3300 struct buffer_head *root_bh,
3301 int name_index,
3302 const char *name,
3303 struct ocfs2_xattr_search *xs)
3304{
3305 int ret;
3306 struct ocfs2_xattr_block *xb =
3307 (struct ocfs2_xattr_block *)root_bh->b_data;
3308 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3309 struct ocfs2_extent_list *el = &xb_root->xt_list;
3310 u64 p_blkno = 0;
3311 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08003312 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08003313
3314 if (le16_to_cpu(el->l_next_free_rec) == 0)
3315 return -ENODATA;
3316
3317 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3318 name, name_hash, name_index);
3319
3320 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3321 &num_clusters, el);
3322 if (ret) {
3323 mlog_errno(ret);
3324 goto out;
3325 }
3326
3327 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3328
3329 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07003330 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3331 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08003332
3333 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3334 p_blkno, first_hash, num_clusters, xs);
3335
3336out:
3337 return ret;
3338}
3339
Tao Ma0c044f02008-08-18 17:38:50 +08003340static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3341 u64 blkno,
3342 u32 clusters,
3343 xattr_bucket_func *func,
3344 void *para)
3345{
Joel Becker6dde41d2008-10-24 17:16:48 -07003346 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003347 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3348 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07003349 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08003350
Joel Beckerba937122008-10-24 19:13:20 -07003351 bucket = ocfs2_xattr_bucket_new(inode);
3352 if (!bucket) {
3353 mlog_errno(-ENOMEM);
3354 return -ENOMEM;
3355 }
Tao Ma0c044f02008-08-18 17:38:50 +08003356
3357 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003358 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003359
Joel Beckerba937122008-10-24 19:13:20 -07003360 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3361 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003362 if (ret) {
3363 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003364 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003365 }
3366
Tao Ma0c044f02008-08-18 17:38:50 +08003367 /*
3368 * The real bucket num in this series of blocks is stored
3369 * in the 1st bucket.
3370 */
3371 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07003372 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08003373
Mark Fashehde29c082008-10-29 14:45:30 -07003374 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3375 (unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07003376 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08003377 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07003378 ret = func(inode, bucket, para);
Tao Maa46fa682009-05-04 05:18:09 +08003379 if (ret && ret != -ERANGE)
Tao Ma0c044f02008-08-18 17:38:50 +08003380 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003381 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08003382 }
3383
Joel Beckerba937122008-10-24 19:13:20 -07003384 ocfs2_xattr_bucket_relse(bucket);
3385 if (ret)
3386 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003387 }
3388
Joel Beckerba937122008-10-24 19:13:20 -07003389 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08003390 return ret;
3391}
3392
3393struct ocfs2_xattr_tree_list {
3394 char *buffer;
3395 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08003396 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08003397};
3398
Tao Mafd68a892009-08-18 11:43:21 +08003399static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
Tao Ma0c044f02008-08-18 17:38:50 +08003400 struct ocfs2_xattr_header *xh,
3401 int index,
3402 int *block_off,
3403 int *new_offset)
3404{
3405 u16 name_offset;
3406
3407 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3408 return -EINVAL;
3409
3410 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3411
Tao Mafd68a892009-08-18 11:43:21 +08003412 *block_off = name_offset >> sb->s_blocksize_bits;
3413 *new_offset = name_offset % sb->s_blocksize;
Tao Ma0c044f02008-08-18 17:38:50 +08003414
3415 return 0;
3416}
3417
3418static int ocfs2_list_xattr_bucket(struct inode *inode,
3419 struct ocfs2_xattr_bucket *bucket,
3420 void *para)
3421{
Tao Ma936b8832008-10-09 23:06:14 +08003422 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08003423 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08003424 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08003425 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08003426
Joel Becker3e632942008-10-24 17:04:49 -07003427 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3428 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08003429 type = ocfs2_xattr_get_type(entry);
3430 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08003431
Tao Ma936b8832008-10-09 23:06:14 +08003432 if (prefix) {
Tao Mafd68a892009-08-18 11:43:21 +08003433 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Becker3e632942008-10-24 17:04:49 -07003434 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08003435 i,
3436 &block_off,
3437 &new_offset);
3438 if (ret)
3439 break;
Tao Ma936b8832008-10-09 23:06:14 +08003440
Joel Becker51def392008-10-24 16:57:21 -07003441 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08003442 new_offset;
3443 ret = ocfs2_xattr_list_entry(xl->buffer,
3444 xl->buffer_size,
3445 &xl->result,
3446 prefix, name,
3447 entry->xe_name_len);
3448 if (ret)
3449 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003450 }
3451 }
3452
3453 return ret;
3454}
3455
Tao Ma47bca492009-08-18 11:43:42 +08003456static int ocfs2_iterate_xattr_index_block(struct inode *inode,
3457 struct buffer_head *blk_bh,
3458 xattr_tree_rec_func *rec_func,
3459 void *para)
Tao Ma0c044f02008-08-18 17:38:50 +08003460{
Tao Ma47bca492009-08-18 11:43:42 +08003461 struct ocfs2_xattr_block *xb =
3462 (struct ocfs2_xattr_block *)blk_bh->b_data;
3463 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
Tao Ma0c044f02008-08-18 17:38:50 +08003464 int ret = 0;
3465 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3466 u64 p_blkno = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003467
Tao Ma47bca492009-08-18 11:43:42 +08003468 if (!el->l_next_free_rec || !rec_func)
Tao Ma0c044f02008-08-18 17:38:50 +08003469 return 0;
3470
3471 while (name_hash > 0) {
3472 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3473 &e_cpos, &num_clusters, el);
3474 if (ret) {
3475 mlog_errno(ret);
Tao Ma47bca492009-08-18 11:43:42 +08003476 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003477 }
3478
Tao Ma47bca492009-08-18 11:43:42 +08003479 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
3480 num_clusters, para);
Tao Ma0c044f02008-08-18 17:38:50 +08003481 if (ret) {
Tao Maa46fa682009-05-04 05:18:09 +08003482 if (ret != -ERANGE)
3483 mlog_errno(ret);
Tao Ma47bca492009-08-18 11:43:42 +08003484 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003485 }
3486
3487 if (e_cpos == 0)
3488 break;
3489
3490 name_hash = e_cpos - 1;
3491 }
3492
Tao Ma47bca492009-08-18 11:43:42 +08003493 return ret;
3494
3495}
3496
3497static int ocfs2_list_xattr_tree_rec(struct inode *inode,
3498 struct buffer_head *root_bh,
3499 u64 blkno, u32 cpos, u32 len, void *para)
3500{
3501 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
3502 ocfs2_list_xattr_bucket, para);
3503}
3504
3505static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
3506 struct buffer_head *blk_bh,
3507 char *buffer,
3508 size_t buffer_size)
3509{
3510 int ret;
3511 struct ocfs2_xattr_tree_list xl = {
3512 .buffer = buffer,
3513 .buffer_size = buffer_size,
3514 .result = 0,
3515 };
3516
3517 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
3518 ocfs2_list_xattr_tree_rec, &xl);
3519 if (ret) {
3520 mlog_errno(ret);
3521 goto out;
3522 }
3523
Tao Ma936b8832008-10-09 23:06:14 +08003524 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08003525out:
3526 return ret;
3527}
Tao Ma01225592008-08-18 17:38:53 +08003528
3529static int cmp_xe(const void *a, const void *b)
3530{
3531 const struct ocfs2_xattr_entry *l = a, *r = b;
3532 u32 l_hash = le32_to_cpu(l->xe_name_hash);
3533 u32 r_hash = le32_to_cpu(r->xe_name_hash);
3534
3535 if (l_hash > r_hash)
3536 return 1;
3537 if (l_hash < r_hash)
3538 return -1;
3539 return 0;
3540}
3541
3542static void swap_xe(void *a, void *b, int size)
3543{
3544 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
3545
3546 tmp = *l;
3547 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
3548 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
3549}
3550
3551/*
3552 * When the ocfs2_xattr_block is filled up, new bucket will be created
3553 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07003554 * The header goes at the start of the bucket, and the names+values are
3555 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08003556 * Note: we need to sort the entries since they are not saved in order
3557 * in the ocfs2_xattr_block.
3558 */
3559static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
3560 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07003561 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08003562{
3563 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07003564 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08003565 u16 offset, size, off_change;
3566 struct ocfs2_xattr_entry *xe;
3567 struct ocfs2_xattr_block *xb =
3568 (struct ocfs2_xattr_block *)xb_bh->b_data;
3569 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003570 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003571 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07003572 char *src = xb_bh->b_data;
3573 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08003574
3575 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3576 (unsigned long long)xb_bh->b_blocknr,
Joel Becker178eeac2008-10-27 15:18:29 -07003577 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08003578
Joel Becker178eeac2008-10-27 15:18:29 -07003579 for (i = 0; i < blks; i++)
3580 memset(bucket_block(bucket, i), 0, blocksize);
3581
Tao Ma01225592008-08-18 17:38:53 +08003582 /*
3583 * Since the xe_name_offset is based on ocfs2_xattr_header,
3584 * there is a offset change corresponding to the change of
3585 * ocfs2_xattr_header's position.
3586 */
3587 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3588 xe = &xb_xh->xh_entries[count - 1];
3589 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
3590 size = blocksize - offset;
3591
3592 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08003593 memcpy(target + offset, src + offset, size);
3594
3595 /* Init new header now. */
3596 xh->xh_count = xb_xh->xh_count;
3597 xh->xh_num_buckets = cpu_to_le16(1);
3598 xh->xh_name_value_len = cpu_to_le16(size);
3599 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
3600
3601 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07003602 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003603 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
3604 size = count * sizeof(struct ocfs2_xattr_entry);
3605 memcpy(target + offset, (char *)xb_xh + offset, size);
3606
3607 /* Change the xe offset for all the xe because of the move. */
3608 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
3609 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3610 for (i = 0; i < count; i++)
3611 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
3612
3613 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3614 offset, size, off_change);
3615
3616 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
3617 cmp_xe, swap_xe);
3618}
3619
3620/*
3621 * After we move xattr from block to index btree, we have to
3622 * update ocfs2_xattr_search to the new xe and base.
3623 *
3624 * When the entry is in xattr block, xattr_bh indicates the storage place.
3625 * While if the entry is in index b-tree, "bucket" indicates the
3626 * real place of the xattr.
3627 */
Joel Becker178eeac2008-10-27 15:18:29 -07003628static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3629 struct ocfs2_xattr_search *xs,
3630 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08003631{
Tao Ma01225592008-08-18 17:38:53 +08003632 char *buf = old_bh->b_data;
3633 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3634 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003635 int i;
Tao Ma01225592008-08-18 17:38:53 +08003636
Joel Beckerba937122008-10-24 19:13:20 -07003637 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07003638 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003639 xs->end = xs->base + inode->i_sb->s_blocksize;
3640
Joel Becker178eeac2008-10-27 15:18:29 -07003641 if (xs->not_found)
3642 return;
Tao Ma01225592008-08-18 17:38:53 +08003643
Joel Becker178eeac2008-10-27 15:18:29 -07003644 i = xs->here - old_xh->xh_entries;
3645 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08003646}
3647
3648static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08003649 struct ocfs2_xattr_search *xs,
3650 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08003651{
Tao Ma85db90e2008-11-12 08:27:01 +08003652 int ret;
Tao Ma01225592008-08-18 17:38:53 +08003653 u32 bit_off, len;
3654 u64 blkno;
Tao Ma85db90e2008-11-12 08:27:01 +08003655 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08003656 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3657 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Tao Ma01225592008-08-18 17:38:53 +08003658 struct buffer_head *xb_bh = xs->xattr_bh;
3659 struct ocfs2_xattr_block *xb =
3660 (struct ocfs2_xattr_block *)xb_bh->b_data;
3661 struct ocfs2_xattr_tree_root *xr;
3662 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08003663
3664 mlog(0, "create xattr index block for %llu\n",
3665 (unsigned long long)xb_bh->b_blocknr);
3666
3667 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07003668 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08003669
Tao Ma01225592008-08-18 17:38:53 +08003670 /*
3671 * XXX:
3672 * We can use this lock for now, and maybe move to a dedicated mutex
3673 * if performance becomes a problem later.
3674 */
3675 down_write(&oi->ip_alloc_sem);
3676
Joel Becker0cf2f762009-02-12 16:41:25 -08003677 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
Joel Becker84008972008-12-09 16:11:49 -08003678 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08003679 if (ret) {
3680 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003681 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003682 }
3683
Tao Ma78f30c32008-11-12 08:27:00 +08003684 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3685 1, 1, &bit_off, &len);
Tao Ma01225592008-08-18 17:38:53 +08003686 if (ret) {
3687 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003688 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003689 }
3690
3691 /*
3692 * The bucket may spread in many blocks, and
3693 * we will only touch the 1st block and the last block
3694 * in the whole bucket(one for entry and one for data).
3695 */
3696 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3697
Mark Fashehde29c082008-10-29 14:45:30 -07003698 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3699 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08003700
Joel Becker178eeac2008-10-27 15:18:29 -07003701 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08003702 if (ret) {
3703 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003704 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003705 }
3706
Joel Becker178eeac2008-10-27 15:18:29 -07003707 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3708 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07003709 if (ret) {
3710 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003711 goto out;
Joel Beckerbd60bd32008-10-20 18:25:56 -07003712 }
Tao Ma01225592008-08-18 17:38:53 +08003713
Joel Becker178eeac2008-10-27 15:18:29 -07003714 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3715 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3716
3717 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3718
Tao Ma01225592008-08-18 17:38:53 +08003719 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3720 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3721 offsetof(struct ocfs2_xattr_block, xb_attrs));
3722
3723 xr = &xb->xb_attrs.xb_root;
3724 xr->xt_clusters = cpu_to_le32(1);
3725 xr->xt_last_eb_blk = 0;
3726 xr->xt_list.l_tree_depth = 0;
3727 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3728 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3729
3730 xr->xt_list.l_recs[0].e_cpos = 0;
3731 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3732 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3733
3734 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3735
Tao Ma85db90e2008-11-12 08:27:01 +08003736 ocfs2_journal_dirty(handle, xb_bh);
Tao Ma01225592008-08-18 17:38:53 +08003737
Tao Ma85db90e2008-11-12 08:27:01 +08003738out:
Tao Ma01225592008-08-18 17:38:53 +08003739 up_write(&oi->ip_alloc_sem);
3740
Tao Ma01225592008-08-18 17:38:53 +08003741 return ret;
3742}
3743
3744static int cmp_xe_offset(const void *a, const void *b)
3745{
3746 const struct ocfs2_xattr_entry *l = a, *r = b;
3747 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3748 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3749
3750 if (l_name_offset < r_name_offset)
3751 return 1;
3752 if (l_name_offset > r_name_offset)
3753 return -1;
3754 return 0;
3755}
3756
3757/*
3758 * defrag a xattr bucket if we find that the bucket has some
3759 * holes beteen name/value pairs.
3760 * We will move all the name/value pairs to the end of the bucket
3761 * so that we can spare some space for insertion.
3762 */
3763static int ocfs2_defrag_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08003764 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08003765 struct ocfs2_xattr_bucket *bucket)
3766{
3767 int ret, i;
3768 size_t end, offset, len, value_len;
3769 struct ocfs2_xattr_header *xh;
3770 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07003771 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003772 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08003773 size_t blocksize = inode->i_sb->s_blocksize;
Tao Ma01225592008-08-18 17:38:53 +08003774 struct ocfs2_xattr_entry *xe;
Tao Ma01225592008-08-18 17:38:53 +08003775
3776 /*
3777 * In order to make the operation more efficient and generic,
3778 * we copy all the blocks into a contiguous memory and do the
3779 * defragment there, so if anything is error, we will not touch
3780 * the real block.
3781 */
3782 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3783 if (!bucket_buf) {
3784 ret = -EIO;
3785 goto out;
3786 }
3787
Joel Becker161d6f32008-10-27 15:25:18 -07003788 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003789 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3790 memcpy(buf, bucket_block(bucket, i), blocksize);
Joel Becker161d6f32008-10-27 15:25:18 -07003791
Tao Ma1c32a2f2008-11-06 08:10:47 +08003792 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
Joel Becker161d6f32008-10-27 15:25:18 -07003793 OCFS2_JOURNAL_ACCESS_WRITE);
3794 if (ret < 0) {
3795 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003796 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003797 }
3798
3799 xh = (struct ocfs2_xattr_header *)bucket_buf;
3800 entries = (char *)xh->xh_entries;
3801 xh_free_start = le16_to_cpu(xh->xh_free_start);
3802
3803 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3804 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003805 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
3806 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08003807
3808 /*
3809 * sort all the entries by their offset.
3810 * the largest will be the first, so that we can
3811 * move them to the end one by one.
3812 */
3813 sort(entries, le16_to_cpu(xh->xh_count),
3814 sizeof(struct ocfs2_xattr_entry),
3815 cmp_xe_offset, swap_xe);
3816
3817 /* Move all name/values to the end of the bucket. */
3818 xe = xh->xh_entries;
3819 end = OCFS2_XATTR_BUCKET_SIZE;
3820 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3821 offset = le16_to_cpu(xe->xe_name_offset);
3822 if (ocfs2_xattr_is_local(xe))
3823 value_len = OCFS2_XATTR_SIZE(
3824 le64_to_cpu(xe->xe_value_size));
3825 else
3826 value_len = OCFS2_XATTR_ROOT_SIZE;
3827 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3828
3829 /*
3830 * We must make sure that the name/value pair
3831 * exist in the same block. So adjust end to
3832 * the previous block end if needed.
3833 */
3834 if (((end - len) / blocksize !=
3835 (end - 1) / blocksize))
3836 end = end - end % blocksize;
3837
3838 if (end > offset + len) {
3839 memmove(bucket_buf + end - len,
3840 bucket_buf + offset, len);
3841 xe->xe_name_offset = cpu_to_le16(end - len);
3842 }
3843
3844 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3845 "bucket %llu\n", (unsigned long long)blkno);
3846
3847 end -= len;
3848 }
3849
3850 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3851 "bucket %llu\n", (unsigned long long)blkno);
3852
3853 if (xh_free_start == end)
Tao Ma85db90e2008-11-12 08:27:01 +08003854 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003855
3856 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3857 xh->xh_free_start = cpu_to_le16(end);
3858
3859 /* sort the entries by their name_hash. */
3860 sort(entries, le16_to_cpu(xh->xh_count),
3861 sizeof(struct ocfs2_xattr_entry),
3862 cmp_xe, swap_xe);
3863
3864 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003865 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3866 memcpy(bucket_block(bucket, i), buf, blocksize);
3867 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08003868
Tao Ma01225592008-08-18 17:38:53 +08003869out:
Tao Ma01225592008-08-18 17:38:53 +08003870 kfree(bucket_buf);
3871 return ret;
3872}
3873
3874/*
Joel Beckerb5c03e72008-11-25 19:58:16 -08003875 * prev_blkno points to the start of an existing extent. new_blkno
3876 * points to a newly allocated extent. Because we know each of our
3877 * clusters contains more than bucket, we can easily split one cluster
3878 * at a bucket boundary. So we take the last cluster of the existing
3879 * extent and split it down the middle. We move the last half of the
3880 * buckets in the last cluster of the existing extent over to the new
3881 * extent.
Tao Ma01225592008-08-18 17:38:53 +08003882 *
Joel Beckerb5c03e72008-11-25 19:58:16 -08003883 * first_bh is the buffer at prev_blkno so we can update the existing
3884 * extent's bucket count. header_bh is the bucket were we were hoping
3885 * to insert our xattr. If the bucket move places the target in the new
3886 * extent, we'll update first_bh and header_bh after modifying the old
3887 * extent.
3888 *
3889 * first_hash will be set as the 1st xe's name_hash in the new extent.
Tao Ma01225592008-08-18 17:38:53 +08003890 */
3891static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3892 handle_t *handle,
Joel Becker41cb8142008-11-26 14:25:21 -08003893 struct ocfs2_xattr_bucket *first,
3894 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08003895 u64 new_blkno,
Tao Ma01225592008-08-18 17:38:53 +08003896 u32 num_clusters,
3897 u32 *first_hash)
3898{
Joel Beckerc58b6032008-11-26 13:36:24 -08003899 int ret;
Joel Becker41cb8142008-11-26 14:25:21 -08003900 struct super_block *sb = inode->i_sb;
3901 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
3902 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
Joel Beckerb5c03e72008-11-25 19:58:16 -08003903 int to_move = num_buckets / 2;
Joel Beckerc58b6032008-11-26 13:36:24 -08003904 u64 src_blkno;
Joel Becker41cb8142008-11-26 14:25:21 -08003905 u64 last_cluster_blkno = bucket_blkno(first) +
3906 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08003907
Joel Becker41cb8142008-11-26 14:25:21 -08003908 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
3909 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
Tao Ma01225592008-08-18 17:38:53 +08003910
Tao Ma01225592008-08-18 17:38:53 +08003911 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Joel Beckerc58b6032008-11-26 13:36:24 -08003912 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003913
Joel Becker41cb8142008-11-26 14:25:21 -08003914 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
Joel Beckerc58b6032008-11-26 13:36:24 -08003915 last_cluster_blkno, new_blkno,
3916 to_move, first_hash);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003917 if (ret) {
3918 mlog_errno(ret);
3919 goto out;
3920 }
3921
Joel Beckerc58b6032008-11-26 13:36:24 -08003922 /* This is the first bucket that got moved */
3923 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
3924
Tao Ma01225592008-08-18 17:38:53 +08003925 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08003926 * If the target bucket was part of the moved buckets, we need to
Joel Becker41cb8142008-11-26 14:25:21 -08003927 * update first and target.
Joel Beckerb5c03e72008-11-25 19:58:16 -08003928 */
Joel Becker41cb8142008-11-26 14:25:21 -08003929 if (bucket_blkno(target) >= src_blkno) {
Joel Beckerb5c03e72008-11-25 19:58:16 -08003930 /* Find the block for the new target bucket */
3931 src_blkno = new_blkno +
Joel Becker41cb8142008-11-26 14:25:21 -08003932 (bucket_blkno(target) - src_blkno);
3933
3934 ocfs2_xattr_bucket_relse(first);
3935 ocfs2_xattr_bucket_relse(target);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003936
3937 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08003938 * These shouldn't fail - the buffers are in the
Joel Beckerb5c03e72008-11-25 19:58:16 -08003939 * journal from ocfs2_cp_xattr_bucket().
3940 */
Joel Becker41cb8142008-11-26 14:25:21 -08003941 ret = ocfs2_read_xattr_bucket(first, new_blkno);
Joel Beckerc58b6032008-11-26 13:36:24 -08003942 if (ret) {
3943 mlog_errno(ret);
3944 goto out;
3945 }
Joel Becker41cb8142008-11-26 14:25:21 -08003946 ret = ocfs2_read_xattr_bucket(target, src_blkno);
3947 if (ret)
Joel Beckerb5c03e72008-11-25 19:58:16 -08003948 mlog_errno(ret);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003949
Joel Beckerb5c03e72008-11-25 19:58:16 -08003950 }
3951
Tao Ma01225592008-08-18 17:38:53 +08003952out:
Tao Ma01225592008-08-18 17:38:53 +08003953 return ret;
3954}
3955
Tao Ma01225592008-08-18 17:38:53 +08003956/*
Tao Ma80bcaf32008-10-27 06:06:24 +08003957 * Find the suitable pos when we divide a bucket into 2.
3958 * We have to make sure the xattrs with the same hash value exist
3959 * in the same bucket.
3960 *
3961 * If this ocfs2_xattr_header covers more than one hash value, find a
3962 * place where the hash value changes. Try to find the most even split.
3963 * The most common case is that all entries have different hash values,
3964 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08003965 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003966static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3967{
3968 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3969 int count = le16_to_cpu(xh->xh_count);
3970 int delta, middle = count / 2;
3971
3972 /*
3973 * We start at the middle. Each step gets farther away in both
3974 * directions. We therefore hit the change in hash value
3975 * nearest to the middle. Note that this loop does not execute for
3976 * count < 2.
3977 */
3978 for (delta = 0; delta < middle; delta++) {
3979 /* Let's check delta earlier than middle */
3980 if (cmp_xe(&entries[middle - delta - 1],
3981 &entries[middle - delta]))
3982 return middle - delta;
3983
3984 /* For even counts, don't walk off the end */
3985 if ((middle + delta + 1) == count)
3986 continue;
3987
3988 /* Now try delta past middle */
3989 if (cmp_xe(&entries[middle + delta],
3990 &entries[middle + delta + 1]))
3991 return middle + delta + 1;
3992 }
3993
3994 /* Every entry had the same hash */
3995 return count;
3996}
3997
3998/*
3999 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4000 * first_hash will record the 1st hash of the new bucket.
4001 *
4002 * Normally half of the xattrs will be moved. But we have to make
4003 * sure that the xattrs with the same hash value are stored in the
4004 * same bucket. If all the xattrs in this bucket have the same hash
4005 * value, the new bucket will be initialized as an empty one and the
4006 * first_hash will be initialized as (hash_value+1).
4007 */
4008static int ocfs2_divide_xattr_bucket(struct inode *inode,
4009 handle_t *handle,
4010 u64 blk,
4011 u64 new_blk,
4012 u32 *first_hash,
4013 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08004014{
4015 int ret, i;
Tao Ma80bcaf32008-10-27 06:06:24 +08004016 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07004017 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004018 struct ocfs2_xattr_header *xh;
4019 struct ocfs2_xattr_entry *xe;
4020 int blocksize = inode->i_sb->s_blocksize;
4021
Tao Ma80bcaf32008-10-27 06:06:24 +08004022 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004023 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004024
Joel Beckerba937122008-10-24 19:13:20 -07004025 s_bucket = ocfs2_xattr_bucket_new(inode);
4026 t_bucket = ocfs2_xattr_bucket_new(inode);
4027 if (!s_bucket || !t_bucket) {
4028 ret = -ENOMEM;
4029 mlog_errno(ret);
4030 goto out;
4031 }
Tao Ma01225592008-08-18 17:38:53 +08004032
Joel Beckerba937122008-10-24 19:13:20 -07004033 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08004034 if (ret) {
4035 mlog_errno(ret);
4036 goto out;
4037 }
4038
Joel Beckerba937122008-10-24 19:13:20 -07004039 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004040 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004041 if (ret) {
4042 mlog_errno(ret);
4043 goto out;
4044 }
4045
Joel Becker784b8162008-10-24 17:33:40 -07004046 /*
4047 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4048 * there's no need to read it.
4049 */
Joel Beckerba937122008-10-24 19:13:20 -07004050 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004051 if (ret) {
4052 mlog_errno(ret);
4053 goto out;
4054 }
4055
Joel Becker2b656c12008-11-25 19:00:15 -08004056 /*
4057 * Hey, if we're overwriting t_bucket, what difference does
4058 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4059 * same part of ocfs2_cp_xattr_bucket().
4060 */
Joel Beckerba937122008-10-24 19:13:20 -07004061 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004062 new_bucket_head ?
4063 OCFS2_JOURNAL_ACCESS_CREATE :
4064 OCFS2_JOURNAL_ACCESS_WRITE);
4065 if (ret) {
4066 mlog_errno(ret);
4067 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004068 }
4069
Joel Beckerba937122008-10-24 19:13:20 -07004070 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004071 count = le16_to_cpu(xh->xh_count);
4072 start = ocfs2_xattr_find_divide_pos(xh);
4073
4074 if (start == count) {
4075 xe = &xh->xh_entries[start-1];
4076
4077 /*
4078 * initialized a new empty bucket here.
4079 * The hash value is set as one larger than
4080 * that of the last entry in the previous bucket.
4081 */
Joel Beckerba937122008-10-24 19:13:20 -07004082 for (i = 0; i < t_bucket->bu_blocks; i++)
4083 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08004084
Joel Beckerba937122008-10-24 19:13:20 -07004085 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004086 xh->xh_free_start = cpu_to_le16(blocksize);
4087 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4088 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4089
4090 goto set_num_buckets;
4091 }
4092
Tao Ma01225592008-08-18 17:38:53 +08004093 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07004094 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004095
4096 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07004097 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004098
4099 /*
4100 * Calculate the total name/value len and xh_free_start for
4101 * the old bucket first.
4102 */
4103 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4104 name_value_len = 0;
4105 for (i = 0; i < start; i++) {
4106 xe = &xh->xh_entries[i];
4107 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
4108 if (ocfs2_xattr_is_local(xe))
4109 xe_len +=
4110 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4111 else
4112 xe_len += OCFS2_XATTR_ROOT_SIZE;
4113 name_value_len += xe_len;
4114 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4115 name_offset = le16_to_cpu(xe->xe_name_offset);
4116 }
4117
4118 /*
4119 * Now begin the modification to the new bucket.
4120 *
4121 * In the new bucket, We just move the xattr entry to the beginning
4122 * and don't touch the name/value. So there will be some holes in the
4123 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4124 * called.
4125 */
4126 xe = &xh->xh_entries[start];
4127 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4128 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07004129 (int)((char *)xe - (char *)xh),
4130 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08004131 memmove((char *)xh->xh_entries, (char *)xe, len);
4132 xe = &xh->xh_entries[count - start];
4133 len = sizeof(struct ocfs2_xattr_entry) * start;
4134 memset((char *)xe, 0, len);
4135
4136 le16_add_cpu(&xh->xh_count, -start);
4137 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4138
4139 /* Calculate xh_free_start for the new bucket. */
4140 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4141 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4142 xe = &xh->xh_entries[i];
4143 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
4144 if (ocfs2_xattr_is_local(xe))
4145 xe_len +=
4146 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4147 else
4148 xe_len += OCFS2_XATTR_ROOT_SIZE;
4149 if (le16_to_cpu(xe->xe_name_offset) <
4150 le16_to_cpu(xh->xh_free_start))
4151 xh->xh_free_start = xe->xe_name_offset;
4152 }
4153
Tao Ma80bcaf32008-10-27 06:06:24 +08004154set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08004155 /* set xh->xh_num_buckets for the new xh. */
4156 if (new_bucket_head)
4157 xh->xh_num_buckets = cpu_to_le16(1);
4158 else
4159 xh->xh_num_buckets = 0;
4160
Joel Beckerba937122008-10-24 19:13:20 -07004161 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004162
4163 /* store the first_hash of the new bucket. */
4164 if (first_hash)
4165 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4166
4167 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08004168 * Now only update the 1st block of the old bucket. If we
4169 * just added a new empty bucket, there is no need to modify
4170 * it.
Tao Ma01225592008-08-18 17:38:53 +08004171 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004172 if (start == count)
4173 goto out;
4174
Joel Beckerba937122008-10-24 19:13:20 -07004175 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004176 memset(&xh->xh_entries[start], 0,
4177 sizeof(struct ocfs2_xattr_entry) * (count - start));
4178 xh->xh_count = cpu_to_le16(start);
4179 xh->xh_free_start = cpu_to_le16(name_offset);
4180 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4181
Joel Beckerba937122008-10-24 19:13:20 -07004182 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004183
4184out:
Joel Beckerba937122008-10-24 19:13:20 -07004185 ocfs2_xattr_bucket_free(s_bucket);
4186 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004187
4188 return ret;
4189}
4190
4191/*
4192 * Copy xattr from one bucket to another bucket.
4193 *
4194 * The caller must make sure that the journal transaction
4195 * has enough space for journaling.
4196 */
4197static int ocfs2_cp_xattr_bucket(struct inode *inode,
4198 handle_t *handle,
4199 u64 s_blkno,
4200 u64 t_blkno,
4201 int t_is_new)
4202{
Joel Becker4980c6d2008-10-24 18:54:43 -07004203 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07004204 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004205
4206 BUG_ON(s_blkno == t_blkno);
4207
4208 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004209 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4210 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08004211
Joel Beckerba937122008-10-24 19:13:20 -07004212 s_bucket = ocfs2_xattr_bucket_new(inode);
4213 t_bucket = ocfs2_xattr_bucket_new(inode);
4214 if (!s_bucket || !t_bucket) {
4215 ret = -ENOMEM;
4216 mlog_errno(ret);
4217 goto out;
4218 }
Joel Becker92de1092008-11-25 17:06:40 -08004219
Joel Beckerba937122008-10-24 19:13:20 -07004220 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004221 if (ret)
4222 goto out;
4223
Joel Becker784b8162008-10-24 17:33:40 -07004224 /*
4225 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4226 * there's no need to read it.
4227 */
Joel Beckerba937122008-10-24 19:13:20 -07004228 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004229 if (ret)
4230 goto out;
4231
Joel Becker2b656c12008-11-25 19:00:15 -08004232 /*
4233 * Hey, if we're overwriting t_bucket, what difference does
4234 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
Joel Becker874d65a2008-11-26 13:02:18 -08004235 * cluster to fill, we came here from
4236 * ocfs2_mv_xattr_buckets(), and it is really new -
4237 * ACCESS_CREATE is required. But we also might have moved data
4238 * out of t_bucket before extending back into it.
4239 * ocfs2_add_new_xattr_bucket() can do this - its call to
4240 * ocfs2_add_new_xattr_cluster() may have created a new extent
Joel Becker2b656c12008-11-25 19:00:15 -08004241 * and copied out the end of the old extent. Then it re-extends
4242 * the old extent back to create space for new xattrs. That's
4243 * how we get here, and the bucket isn't really new.
4244 */
Joel Beckerba937122008-10-24 19:13:20 -07004245 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004246 t_is_new ?
4247 OCFS2_JOURNAL_ACCESS_CREATE :
4248 OCFS2_JOURNAL_ACCESS_WRITE);
4249 if (ret)
4250 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004251
Joel Beckerba937122008-10-24 19:13:20 -07004252 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4253 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004254
4255out:
Joel Beckerba937122008-10-24 19:13:20 -07004256 ocfs2_xattr_bucket_free(t_bucket);
4257 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004258
4259 return ret;
4260}
4261
4262/*
Joel Becker874d65a2008-11-26 13:02:18 -08004263 * src_blk points to the start of an existing extent. last_blk points to
4264 * last cluster in that extent. to_blk points to a newly allocated
Joel Becker54ecb6b2008-11-26 13:18:31 -08004265 * extent. We copy the buckets from the cluster at last_blk to the new
4266 * extent. If start_bucket is non-zero, we skip that many buckets before
4267 * we start copying. The new extent's xh_num_buckets gets set to the
4268 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4269 * by the same amount.
Tao Ma01225592008-08-18 17:38:53 +08004270 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004271static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4272 u64 src_blk, u64 last_blk, u64 to_blk,
4273 unsigned int start_bucket,
4274 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004275{
4276 int i, ret, credits;
4277 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Becker15d60922008-11-25 18:36:42 -08004278 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004279 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
Joel Becker15d60922008-11-25 18:36:42 -08004280 struct ocfs2_xattr_bucket *old_first, *new_first;
Tao Ma01225592008-08-18 17:38:53 +08004281
Joel Becker874d65a2008-11-26 13:02:18 -08004282 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4283 (unsigned long long)last_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08004284
Joel Becker54ecb6b2008-11-26 13:18:31 -08004285 BUG_ON(start_bucket >= num_buckets);
4286 if (start_bucket) {
4287 num_buckets -= start_bucket;
4288 last_blk += (start_bucket * blks_per_bucket);
4289 }
4290
Joel Becker15d60922008-11-25 18:36:42 -08004291 /* The first bucket of the original extent */
4292 old_first = ocfs2_xattr_bucket_new(inode);
4293 /* The first bucket of the new extent */
4294 new_first = ocfs2_xattr_bucket_new(inode);
4295 if (!old_first || !new_first) {
4296 ret = -ENOMEM;
4297 mlog_errno(ret);
4298 goto out;
4299 }
4300
Joel Becker874d65a2008-11-26 13:02:18 -08004301 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
Joel Becker15d60922008-11-25 18:36:42 -08004302 if (ret) {
4303 mlog_errno(ret);
4304 goto out;
4305 }
4306
Tao Ma01225592008-08-18 17:38:53 +08004307 /*
Joel Becker54ecb6b2008-11-26 13:18:31 -08004308 * We need to update the first bucket of the old extent and all
4309 * the buckets going to the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004310 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004311 credits = ((num_buckets + 1) * blks_per_bucket) +
4312 handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004313 ret = ocfs2_extend_trans(handle, credits);
4314 if (ret) {
4315 mlog_errno(ret);
4316 goto out;
4317 }
4318
Joel Becker15d60922008-11-25 18:36:42 -08004319 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4320 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004321 if (ret) {
4322 mlog_errno(ret);
4323 goto out;
4324 }
4325
4326 for (i = 0; i < num_buckets; i++) {
4327 ret = ocfs2_cp_xattr_bucket(inode, handle,
Joel Becker874d65a2008-11-26 13:02:18 -08004328 last_blk + (i * blks_per_bucket),
Joel Becker15d60922008-11-25 18:36:42 -08004329 to_blk + (i * blks_per_bucket),
4330 1);
Tao Ma01225592008-08-18 17:38:53 +08004331 if (ret) {
4332 mlog_errno(ret);
4333 goto out;
4334 }
Tao Ma01225592008-08-18 17:38:53 +08004335 }
4336
Joel Becker15d60922008-11-25 18:36:42 -08004337 /*
4338 * Get the new bucket ready before we dirty anything
4339 * (This actually shouldn't fail, because we already dirtied
4340 * it once in ocfs2_cp_xattr_bucket()).
4341 */
4342 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4343 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004344 mlog_errno(ret);
4345 goto out;
4346 }
Joel Becker15d60922008-11-25 18:36:42 -08004347 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4348 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004349 if (ret) {
4350 mlog_errno(ret);
4351 goto out;
4352 }
4353
Joel Becker15d60922008-11-25 18:36:42 -08004354 /* Now update the headers */
4355 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4356 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
Tao Ma01225592008-08-18 17:38:53 +08004357
Joel Becker15d60922008-11-25 18:36:42 -08004358 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4359 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
Tao Ma01225592008-08-18 17:38:53 +08004360
4361 if (first_hash)
Joel Becker15d60922008-11-25 18:36:42 -08004362 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4363
Tao Ma01225592008-08-18 17:38:53 +08004364out:
Joel Becker15d60922008-11-25 18:36:42 -08004365 ocfs2_xattr_bucket_free(new_first);
4366 ocfs2_xattr_bucket_free(old_first);
Tao Ma01225592008-08-18 17:38:53 +08004367 return ret;
4368}
4369
4370/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004371 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08004372 * This function should only be called when bucket size == cluster size.
4373 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4374 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004375static int ocfs2_divide_xattr_cluster(struct inode *inode,
4376 handle_t *handle,
4377 u64 prev_blk,
4378 u64 new_blk,
4379 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004380{
4381 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08004382 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004383
4384 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4385
4386 ret = ocfs2_extend_trans(handle, credits);
4387 if (ret) {
4388 mlog_errno(ret);
4389 return ret;
4390 }
4391
4392 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08004393 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4394 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08004395}
4396
4397/*
4398 * Move some xattrs from the old cluster to the new one since they are not
4399 * contiguous in ocfs2 xattr tree.
4400 *
4401 * new_blk starts a new separate cluster, and we will move some xattrs from
4402 * prev_blk to it. v_start will be set as the first name hash value in this
4403 * new cluster so that it can be used as e_cpos during tree insertion and
4404 * don't collide with our original b-tree operations. first_bh and header_bh
4405 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4406 * to extend the insert bucket.
4407 *
4408 * The problem is how much xattr should we move to the new one and when should
4409 * we update first_bh and header_bh?
4410 * 1. If cluster size > bucket size, that means the previous cluster has more
4411 * than 1 bucket, so just move half nums of bucket into the new cluster and
4412 * update the first_bh and header_bh if the insert bucket has been moved
4413 * to the new cluster.
4414 * 2. If cluster_size == bucket_size:
4415 * a) If the previous extent rec has more than one cluster and the insert
4416 * place isn't in the last cluster, copy the entire last cluster to the
4417 * new one. This time, we don't need to upate the first_bh and header_bh
4418 * since they will not be moved into the new cluster.
4419 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4420 * the new one. And we set the extend flag to zero if the insert place is
4421 * moved into the new allocated cluster since no extend is needed.
4422 */
4423static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4424 handle_t *handle,
Joel Becker012ee912008-11-26 14:43:31 -08004425 struct ocfs2_xattr_bucket *first,
4426 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004427 u64 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004428 u32 prev_clusters,
4429 u32 *v_start,
4430 int *extend)
4431{
Joel Becker92cf3ad2008-11-26 14:12:09 -08004432 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004433
4434 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Joel Becker012ee912008-11-26 14:43:31 -08004435 (unsigned long long)bucket_blkno(first), prev_clusters,
Mark Fashehde29c082008-10-29 14:45:30 -07004436 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004437
Joel Becker41cb8142008-11-26 14:25:21 -08004438 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
Tao Ma01225592008-08-18 17:38:53 +08004439 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4440 handle,
Joel Becker41cb8142008-11-26 14:25:21 -08004441 first, target,
Tao Ma01225592008-08-18 17:38:53 +08004442 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004443 prev_clusters,
4444 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004445 if (ret)
Joel Becker41cb8142008-11-26 14:25:21 -08004446 mlog_errno(ret);
Joel Becker41cb8142008-11-26 14:25:21 -08004447 } else {
Joel Becker92cf3ad2008-11-26 14:12:09 -08004448 /* The start of the last cluster in the first extent */
4449 u64 last_blk = bucket_blkno(first) +
4450 ((prev_clusters - 1) *
4451 ocfs2_clusters_to_blocks(inode->i_sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08004452
Joel Becker012ee912008-11-26 14:43:31 -08004453 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
Joel Becker874d65a2008-11-26 13:02:18 -08004454 ret = ocfs2_mv_xattr_buckets(inode, handle,
Joel Becker92cf3ad2008-11-26 14:12:09 -08004455 bucket_blkno(first),
Joel Becker54ecb6b2008-11-26 13:18:31 -08004456 last_blk, new_blk, 0,
Tao Ma01225592008-08-18 17:38:53 +08004457 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004458 if (ret)
4459 mlog_errno(ret);
4460 } else {
Tao Ma80bcaf32008-10-27 06:06:24 +08004461 ret = ocfs2_divide_xattr_cluster(inode, handle,
4462 last_blk, new_blk,
4463 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004464 if (ret)
4465 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004466
Joel Becker92cf3ad2008-11-26 14:12:09 -08004467 if ((bucket_blkno(target) == last_blk) && extend)
Tao Ma01225592008-08-18 17:38:53 +08004468 *extend = 0;
4469 }
4470 }
4471
4472 return ret;
4473}
4474
4475/*
4476 * Add a new cluster for xattr storage.
4477 *
4478 * If the new cluster is contiguous with the previous one, it will be
4479 * appended to the same extent record, and num_clusters will be updated.
4480 * If not, we will insert a new extent for it and move some xattrs in
4481 * the last cluster into the new allocated one.
4482 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4483 * lose the benefits of hashing because we'll have to search large leaves.
4484 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4485 * if it's bigger).
4486 *
4487 * first_bh is the first block of the previous extent rec and header_bh
4488 * indicates the bucket we will insert the new xattrs. They will be updated
4489 * when the header_bh is moved into the new cluster.
4490 */
4491static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4492 struct buffer_head *root_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08004493 struct ocfs2_xattr_bucket *first,
4494 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004495 u32 *num_clusters,
4496 u32 prev_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08004497 int *extend,
4498 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004499{
Tao Ma85db90e2008-11-12 08:27:01 +08004500 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004501 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4502 u32 prev_clusters = *num_clusters;
4503 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4504 u64 block;
Tao Ma85db90e2008-11-12 08:27:01 +08004505 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004506 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004507 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08004508
4509 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4510 "previous xattr blkno = %llu\n",
4511 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Joel Beckered29c0c2008-11-26 15:08:44 -08004512 prev_cpos, (unsigned long long)bucket_blkno(first));
Tao Ma01225592008-08-18 17:38:53 +08004513
Joel Becker5e404e92009-02-13 03:54:22 -08004514 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004515
Joel Becker0cf2f762009-02-12 16:41:25 -08004516 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08004517 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004518 if (ret < 0) {
4519 mlog_errno(ret);
4520 goto leave;
4521 }
4522
Tao Ma78f30c32008-11-12 08:27:00 +08004523 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
Tao Ma01225592008-08-18 17:38:53 +08004524 clusters_to_add, &bit_off, &num_bits);
4525 if (ret < 0) {
4526 if (ret != -ENOSPC)
4527 mlog_errno(ret);
4528 goto leave;
4529 }
4530
4531 BUG_ON(num_bits > clusters_to_add);
4532
4533 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4534 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4535 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4536
Joel Beckered29c0c2008-11-26 15:08:44 -08004537 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
Tao Ma01225592008-08-18 17:38:53 +08004538 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
4539 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
4540 /*
4541 * If this cluster is contiguous with the old one and
4542 * adding this new cluster, we don't surpass the limit of
4543 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4544 * initialized and used like other buckets in the previous
4545 * cluster.
4546 * So add it as a contiguous one. The caller will handle
4547 * its init process.
4548 */
4549 v_start = prev_cpos + prev_clusters;
4550 *num_clusters = prev_clusters + num_bits;
4551 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4552 num_bits);
4553 } else {
4554 ret = ocfs2_adjust_xattr_cross_cluster(inode,
4555 handle,
Joel Becker012ee912008-11-26 14:43:31 -08004556 first,
4557 target,
Tao Ma01225592008-08-18 17:38:53 +08004558 block,
Tao Ma01225592008-08-18 17:38:53 +08004559 prev_clusters,
4560 &v_start,
4561 extend);
4562 if (ret) {
4563 mlog_errno(ret);
4564 goto leave;
4565 }
4566 }
4567
4568 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004569 num_bits, (unsigned long long)block, v_start);
Joel Beckercc79d8c2009-02-13 03:24:43 -08004570 ret = ocfs2_insert_extent(handle, &et, v_start, block,
Tao Ma78f30c32008-11-12 08:27:00 +08004571 num_bits, 0, ctxt->meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004572 if (ret < 0) {
4573 mlog_errno(ret);
4574 goto leave;
4575 }
4576
4577 ret = ocfs2_journal_dirty(handle, root_bh);
Tao Ma85db90e2008-11-12 08:27:01 +08004578 if (ret < 0)
Tao Ma01225592008-08-18 17:38:53 +08004579 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004580
4581leave:
Tao Ma01225592008-08-18 17:38:53 +08004582 return ret;
4583}
4584
4585/*
Joel Becker92de1092008-11-25 17:06:40 -08004586 * We are given an extent. 'first' is the bucket at the very front of
4587 * the extent. The extent has space for an additional bucket past
4588 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
4589 * of the target bucket. We wish to shift every bucket past the target
4590 * down one, filling in that additional space. When we get back to the
4591 * target, we split the target between itself and the now-empty bucket
4592 * at target+1 (aka, target_blkno + blks_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08004593 */
4594static int ocfs2_extend_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004595 handle_t *handle,
Joel Becker92de1092008-11-25 17:06:40 -08004596 struct ocfs2_xattr_bucket *first,
4597 u64 target_blk,
Tao Ma01225592008-08-18 17:38:53 +08004598 u32 num_clusters)
4599{
4600 int ret, credits;
4601 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4602 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker92de1092008-11-25 17:06:40 -08004603 u64 end_blk;
4604 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
Tao Ma01225592008-08-18 17:38:53 +08004605
4606 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Joel Becker92de1092008-11-25 17:06:40 -08004607 "from %llu, len = %u\n", (unsigned long long)target_blk,
4608 (unsigned long long)bucket_blkno(first), num_clusters);
Tao Ma01225592008-08-18 17:38:53 +08004609
Joel Becker92de1092008-11-25 17:06:40 -08004610 /* The extent must have room for an additional bucket */
4611 BUG_ON(new_bucket >=
4612 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
Tao Ma01225592008-08-18 17:38:53 +08004613
Joel Becker92de1092008-11-25 17:06:40 -08004614 /* end_blk points to the last existing bucket */
4615 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004616
4617 /*
Joel Becker92de1092008-11-25 17:06:40 -08004618 * end_blk is the start of the last existing bucket.
4619 * Thus, (end_blk - target_blk) covers the target bucket and
4620 * every bucket after it up to, but not including, the last
4621 * existing bucket. Then we add the last existing bucket, the
4622 * new bucket, and the first bucket (3 * blk_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08004623 */
Joel Becker92de1092008-11-25 17:06:40 -08004624 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
Tao Ma85db90e2008-11-12 08:27:01 +08004625 handle->h_buffer_credits;
4626 ret = ocfs2_extend_trans(handle, credits);
4627 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004628 mlog_errno(ret);
4629 goto out;
4630 }
4631
Joel Becker92de1092008-11-25 17:06:40 -08004632 ret = ocfs2_xattr_bucket_journal_access(handle, first,
4633 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004634 if (ret) {
4635 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004636 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004637 }
4638
Joel Becker92de1092008-11-25 17:06:40 -08004639 while (end_blk != target_blk) {
Tao Ma01225592008-08-18 17:38:53 +08004640 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4641 end_blk + blk_per_bucket, 0);
4642 if (ret)
Tao Ma85db90e2008-11-12 08:27:01 +08004643 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004644 end_blk -= blk_per_bucket;
4645 }
4646
Joel Becker92de1092008-11-25 17:06:40 -08004647 /* Move half of the xattr in target_blkno to the next bucket. */
4648 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
4649 target_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08004650
Joel Becker92de1092008-11-25 17:06:40 -08004651 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
4652 ocfs2_xattr_bucket_journal_dirty(handle, first);
Tao Ma01225592008-08-18 17:38:53 +08004653
Tao Ma01225592008-08-18 17:38:53 +08004654out:
4655 return ret;
4656}
4657
4658/*
Joel Becker91f20332008-11-26 15:25:41 -08004659 * Add new xattr bucket in an extent record and adjust the buckets
4660 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
4661 * bucket we want to insert into.
Tao Ma01225592008-08-18 17:38:53 +08004662 *
Joel Becker91f20332008-11-26 15:25:41 -08004663 * In the easy case, we will move all the buckets after target down by
4664 * one. Half of target's xattrs will be moved to the next bucket.
4665 *
4666 * If current cluster is full, we'll allocate a new one. This may not
4667 * be contiguous. The underlying calls will make sure that there is
4668 * space for the insert, shifting buckets around if necessary.
4669 * 'target' may be moved by those calls.
Tao Ma01225592008-08-18 17:38:53 +08004670 */
4671static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4672 struct buffer_head *xb_bh,
Joel Becker91f20332008-11-26 15:25:41 -08004673 struct ocfs2_xattr_bucket *target,
Tao Ma78f30c32008-11-12 08:27:00 +08004674 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004675{
Tao Ma01225592008-08-18 17:38:53 +08004676 struct ocfs2_xattr_block *xb =
4677 (struct ocfs2_xattr_block *)xb_bh->b_data;
4678 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4679 struct ocfs2_extent_list *el = &xb_root->xt_list;
Joel Becker91f20332008-11-26 15:25:41 -08004680 u32 name_hash =
4681 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
Joel Beckered29c0c2008-11-26 15:08:44 -08004682 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004683 int ret, num_buckets, extend = 1;
4684 u64 p_blkno;
4685 u32 e_cpos, num_clusters;
Joel Becker92de1092008-11-25 17:06:40 -08004686 /* The bucket at the front of the extent */
Joel Becker91f20332008-11-26 15:25:41 -08004687 struct ocfs2_xattr_bucket *first;
Tao Ma01225592008-08-18 17:38:53 +08004688
Joel Becker91f20332008-11-26 15:25:41 -08004689 mlog(0, "Add new xattr bucket starting from %llu\n",
4690 (unsigned long long)bucket_blkno(target));
Tao Ma01225592008-08-18 17:38:53 +08004691
Joel Beckered29c0c2008-11-26 15:08:44 -08004692 /* The first bucket of the original extent */
Joel Becker92de1092008-11-25 17:06:40 -08004693 first = ocfs2_xattr_bucket_new(inode);
Joel Becker91f20332008-11-26 15:25:41 -08004694 if (!first) {
Joel Becker92de1092008-11-25 17:06:40 -08004695 ret = -ENOMEM;
4696 mlog_errno(ret);
4697 goto out;
4698 }
4699
Tao Ma01225592008-08-18 17:38:53 +08004700 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4701 &num_clusters, el);
4702 if (ret) {
4703 mlog_errno(ret);
4704 goto out;
4705 }
4706
Joel Beckered29c0c2008-11-26 15:08:44 -08004707 ret = ocfs2_read_xattr_bucket(first, p_blkno);
4708 if (ret) {
4709 mlog_errno(ret);
4710 goto out;
4711 }
4712
Tao Ma01225592008-08-18 17:38:53 +08004713 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
Joel Beckered29c0c2008-11-26 15:08:44 -08004714 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
4715 /*
4716 * This can move first+target if the target bucket moves
4717 * to the new extent.
4718 */
Tao Ma01225592008-08-18 17:38:53 +08004719 ret = ocfs2_add_new_xattr_cluster(inode,
4720 xb_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08004721 first,
4722 target,
Tao Ma01225592008-08-18 17:38:53 +08004723 &num_clusters,
4724 e_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08004725 &extend,
4726 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004727 if (ret) {
4728 mlog_errno(ret);
4729 goto out;
4730 }
4731 }
4732
Joel Becker92de1092008-11-25 17:06:40 -08004733 if (extend) {
Tao Ma01225592008-08-18 17:38:53 +08004734 ret = ocfs2_extend_xattr_bucket(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004735 ctxt->handle,
Joel Beckered29c0c2008-11-26 15:08:44 -08004736 first,
4737 bucket_blkno(target),
Tao Ma01225592008-08-18 17:38:53 +08004738 num_clusters);
Joel Becker92de1092008-11-25 17:06:40 -08004739 if (ret)
4740 mlog_errno(ret);
4741 }
4742
Tao Ma01225592008-08-18 17:38:53 +08004743out:
Joel Becker92de1092008-11-25 17:06:40 -08004744 ocfs2_xattr_bucket_free(first);
Joel Beckered29c0c2008-11-26 15:08:44 -08004745
Tao Ma01225592008-08-18 17:38:53 +08004746 return ret;
4747}
4748
4749static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4750 struct ocfs2_xattr_bucket *bucket,
4751 int offs)
4752{
4753 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4754
4755 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07004756 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08004757}
4758
4759/*
4760 * Handle the normal xattr set, including replace, delete and new.
Tao Ma01225592008-08-18 17:38:53 +08004761 *
4762 * Note: "local" indicates the real data's locality. So we can't
4763 * just its bucket locality by its length.
4764 */
4765static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4766 struct ocfs2_xattr_info *xi,
4767 struct ocfs2_xattr_search *xs,
4768 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004769 int local)
Tao Ma01225592008-08-18 17:38:53 +08004770{
4771 struct ocfs2_xattr_entry *last, *xe;
4772 int name_len = strlen(xi->name);
4773 struct ocfs2_xattr_header *xh = xs->header;
4774 u16 count = le16_to_cpu(xh->xh_count), start;
4775 size_t blocksize = inode->i_sb->s_blocksize;
4776 char *val;
4777 size_t offs, size, new_size;
4778
4779 last = &xh->xh_entries[count];
4780 if (!xs->not_found) {
4781 xe = xs->here;
4782 offs = le16_to_cpu(xe->xe_name_offset);
4783 if (ocfs2_xattr_is_local(xe))
4784 size = OCFS2_XATTR_SIZE(name_len) +
4785 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4786 else
4787 size = OCFS2_XATTR_SIZE(name_len) +
4788 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4789
4790 /*
4791 * If the new value will be stored outside, xi->value has been
4792 * initalized as an empty ocfs2_xattr_value_root, and the same
4793 * goes with xi->value_len, so we can set new_size safely here.
4794 * See ocfs2_xattr_set_in_bucket.
4795 */
4796 new_size = OCFS2_XATTR_SIZE(name_len) +
4797 OCFS2_XATTR_SIZE(xi->value_len);
4798
4799 le16_add_cpu(&xh->xh_name_value_len, -size);
4800 if (xi->value) {
4801 if (new_size > size)
4802 goto set_new_name_value;
4803
4804 /* Now replace the old value with new one. */
4805 if (local)
4806 xe->xe_value_size = cpu_to_le64(xi->value_len);
4807 else
4808 xe->xe_value_size = 0;
4809
4810 val = ocfs2_xattr_bucket_get_val(inode,
Joel Beckerba937122008-10-24 19:13:20 -07004811 xs->bucket, offs);
Tao Ma01225592008-08-18 17:38:53 +08004812 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4813 size - OCFS2_XATTR_SIZE(name_len));
4814 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4815 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4816 xi->value, xi->value_len);
4817
4818 le16_add_cpu(&xh->xh_name_value_len, new_size);
4819 ocfs2_xattr_set_local(xe, local);
4820 return;
4821 } else {
Tao Ma5a095612008-09-19 22:17:41 +08004822 /*
4823 * Remove the old entry if there is more than one.
4824 * We don't remove the last entry so that we can
4825 * use it to indicate the hash value of the empty
4826 * bucket.
4827 */
Tao Ma01225592008-08-18 17:38:53 +08004828 last -= 1;
Tao Ma01225592008-08-18 17:38:53 +08004829 le16_add_cpu(&xh->xh_count, -1);
Tao Ma5a095612008-09-19 22:17:41 +08004830 if (xh->xh_count) {
4831 memmove(xe, xe + 1,
4832 (void *)last - (void *)xe);
4833 memset(last, 0,
4834 sizeof(struct ocfs2_xattr_entry));
4835 } else
4836 xh->xh_free_start =
4837 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4838
Tao Ma01225592008-08-18 17:38:53 +08004839 return;
4840 }
4841 } else {
4842 /* find a new entry for insert. */
4843 int low = 0, high = count - 1, tmp;
4844 struct ocfs2_xattr_entry *tmp_xe;
4845
Tao Ma5a095612008-09-19 22:17:41 +08004846 while (low <= high && count) {
Tao Ma01225592008-08-18 17:38:53 +08004847 tmp = (low + high) / 2;
4848 tmp_xe = &xh->xh_entries[tmp];
4849
4850 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4851 low = tmp + 1;
4852 else if (name_hash <
4853 le32_to_cpu(tmp_xe->xe_name_hash))
4854 high = tmp - 1;
Tao Ma06b240d2008-09-19 22:16:34 +08004855 else {
4856 low = tmp;
Tao Ma01225592008-08-18 17:38:53 +08004857 break;
Tao Ma06b240d2008-09-19 22:16:34 +08004858 }
Tao Ma01225592008-08-18 17:38:53 +08004859 }
4860
4861 xe = &xh->xh_entries[low];
4862 if (low != count)
4863 memmove(xe + 1, xe, (void *)last - (void *)xe);
4864
4865 le16_add_cpu(&xh->xh_count, 1);
4866 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4867 xe->xe_name_hash = cpu_to_le32(name_hash);
4868 xe->xe_name_len = name_len;
4869 ocfs2_xattr_set_type(xe, xi->name_index);
4870 }
4871
4872set_new_name_value:
4873 /* Insert the new name+value. */
4874 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4875
4876 /*
4877 * We must make sure that the name/value pair
4878 * exists in the same block.
4879 */
4880 offs = le16_to_cpu(xh->xh_free_start);
4881 start = offs - size;
4882
4883 if (start >> inode->i_sb->s_blocksize_bits !=
4884 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4885 offs = offs - offs % blocksize;
4886 xh->xh_free_start = cpu_to_le16(offs);
4887 }
4888
Joel Beckerba937122008-10-24 19:13:20 -07004889 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
Tao Ma01225592008-08-18 17:38:53 +08004890 xe->xe_name_offset = cpu_to_le16(offs - size);
4891
4892 memset(val, 0, size);
4893 memcpy(val, xi->name, name_len);
4894 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4895
4896 xe->xe_value_size = cpu_to_le64(xi->value_len);
4897 ocfs2_xattr_set_local(xe, local);
4898 xs->here = xe;
4899 le16_add_cpu(&xh->xh_free_start, -size);
4900 le16_add_cpu(&xh->xh_name_value_len, size);
4901
4902 return;
4903}
4904
Tao Ma01225592008-08-18 17:38:53 +08004905/*
4906 * Set the xattr entry in the specified bucket.
4907 * The bucket is indicated by xs->bucket and it should have the enough
4908 * space for the xattr insertion.
4909 */
4910static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004911 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004912 struct ocfs2_xattr_info *xi,
4913 struct ocfs2_xattr_search *xs,
4914 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004915 int local)
Tao Ma01225592008-08-18 17:38:53 +08004916{
Joel Becker1224be02008-10-24 18:47:33 -07004917 int ret;
Joel Becker02dbf382008-10-27 18:07:45 -07004918 u64 blkno;
Tao Ma01225592008-08-18 17:38:53 +08004919
Mark Fashehff1ec202008-08-19 10:54:29 -07004920 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4921 (unsigned long)xi->value_len, xi->name_index,
Joel Beckerba937122008-10-24 19:13:20 -07004922 (unsigned long long)bucket_blkno(xs->bucket));
Tao Ma01225592008-08-18 17:38:53 +08004923
Joel Beckerba937122008-10-24 19:13:20 -07004924 if (!xs->bucket->bu_bhs[1]) {
Joel Becker02dbf382008-10-27 18:07:45 -07004925 blkno = bucket_blkno(xs->bucket);
4926 ocfs2_xattr_bucket_relse(xs->bucket);
4927 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08004928 if (ret) {
4929 mlog_errno(ret);
4930 goto out;
4931 }
4932 }
4933
Joel Beckerba937122008-10-24 19:13:20 -07004934 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004935 OCFS2_JOURNAL_ACCESS_WRITE);
4936 if (ret < 0) {
4937 mlog_errno(ret);
4938 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004939 }
4940
Tao Ma5a095612008-09-19 22:17:41 +08004941 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
Joel Beckerba937122008-10-24 19:13:20 -07004942 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004943
Tao Ma01225592008-08-18 17:38:53 +08004944out:
Tao Ma01225592008-08-18 17:38:53 +08004945 return ret;
4946}
4947
Tao Ma01225592008-08-18 17:38:53 +08004948/*
4949 * Truncate the specified xe_off entry in xattr bucket.
4950 * bucket is indicated by header_bh and len is the new length.
4951 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4952 *
4953 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4954 */
4955static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
Joel Becker548b0f22008-11-24 19:32:13 -08004956 struct ocfs2_xattr_bucket *bucket,
Tao Ma01225592008-08-18 17:38:53 +08004957 int xe_off,
Tao Ma78f30c32008-11-12 08:27:00 +08004958 int len,
4959 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004960{
4961 int ret, offset;
4962 u64 value_blk;
Tao Ma01225592008-08-18 17:38:53 +08004963 struct ocfs2_xattr_entry *xe;
Joel Becker548b0f22008-11-24 19:32:13 -08004964 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004965 size_t blocksize = inode->i_sb->s_blocksize;
Joel Beckerb3e5d372008-12-09 15:01:04 -08004966 struct ocfs2_xattr_value_buf vb = {
4967 .vb_access = ocfs2_journal_access,
4968 };
Tao Ma01225592008-08-18 17:38:53 +08004969
4970 xe = &xh->xh_entries[xe_off];
4971
4972 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4973
4974 offset = le16_to_cpu(xe->xe_name_offset) +
4975 OCFS2_XATTR_SIZE(xe->xe_name_len);
4976
4977 value_blk = offset / blocksize;
4978
4979 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4980 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
Tao Ma01225592008-08-18 17:38:53 +08004981
Joel Beckerb3e5d372008-12-09 15:01:04 -08004982 vb.vb_bh = bucket->bu_bhs[value_blk];
4983 BUG_ON(!vb.vb_bh);
Tao Ma01225592008-08-18 17:38:53 +08004984
Joel Beckerb3e5d372008-12-09 15:01:04 -08004985 vb.vb_xv = (struct ocfs2_xattr_value_root *)
4986 (vb.vb_bh->b_data + offset % blocksize);
Tao Ma01225592008-08-18 17:38:53 +08004987
Joel Becker548b0f22008-11-24 19:32:13 -08004988 /*
4989 * From here on out we have to dirty the bucket. The generic
4990 * value calls only modify one of the bucket's bhs, but we need
4991 * to send the bucket at once. So if they error, they *could* have
4992 * modified something. We have to assume they did, and dirty
4993 * the whole bucket. This leaves us in a consistent state.
4994 */
Tao Ma01225592008-08-18 17:38:53 +08004995 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
Joel Becker548b0f22008-11-24 19:32:13 -08004996 xe_off, (unsigned long long)bucket_blkno(bucket), len);
Joel Beckerb3e5d372008-12-09 15:01:04 -08004997 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004998 if (ret) {
4999 mlog_errno(ret);
Tao Ma554e7f92009-01-08 08:21:43 +08005000 goto out;
5001 }
5002
5003 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5004 OCFS2_JOURNAL_ACCESS_WRITE);
5005 if (ret) {
5006 mlog_errno(ret);
5007 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005008 }
5009
Joel Becker548b0f22008-11-24 19:32:13 -08005010 xe->xe_value_size = cpu_to_le64(len);
5011
Joel Becker548b0f22008-11-24 19:32:13 -08005012 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08005013
5014out:
Tao Ma01225592008-08-18 17:38:53 +08005015 return ret;
5016}
5017
5018static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08005019 struct ocfs2_xattr_search *xs,
5020 int len,
5021 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005022{
5023 int ret, offset;
5024 struct ocfs2_xattr_entry *xe = xs->here;
5025 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
5026
Joel Beckerba937122008-10-24 19:13:20 -07005027 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
Tao Ma01225592008-08-18 17:38:53 +08005028
5029 offset = xe - xh->xh_entries;
Joel Becker548b0f22008-11-24 19:32:13 -08005030 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005031 offset, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005032 if (ret)
5033 mlog_errno(ret);
5034
5035 return ret;
5036}
5037
5038static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005039 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08005040 struct ocfs2_xattr_search *xs,
5041 char *val,
5042 int value_len)
5043{
Tao Ma712e53e2009-03-12 08:37:34 +08005044 int ret, offset, block_off;
Tao Ma01225592008-08-18 17:38:53 +08005045 struct ocfs2_xattr_value_root *xv;
5046 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma712e53e2009-03-12 08:37:34 +08005047 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
5048 void *base;
Tao Ma492a8a32009-08-18 11:43:17 +08005049 struct ocfs2_xattr_value_buf vb = {
5050 .vb_access = ocfs2_journal_access,
5051 };
Tao Ma01225592008-08-18 17:38:53 +08005052
5053 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
5054
Tao Mafd68a892009-08-18 11:43:21 +08005055 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, xh,
Tao Ma712e53e2009-03-12 08:37:34 +08005056 xe - xh->xh_entries,
5057 &block_off,
5058 &offset);
5059 if (ret) {
5060 mlog_errno(ret);
5061 goto out;
5062 }
Tao Ma01225592008-08-18 17:38:53 +08005063
Tao Ma712e53e2009-03-12 08:37:34 +08005064 base = bucket_block(xs->bucket, block_off);
5065 xv = (struct ocfs2_xattr_value_root *)(base + offset +
5066 OCFS2_XATTR_SIZE(xe->xe_name_len));
Tao Ma01225592008-08-18 17:38:53 +08005067
Tao Ma492a8a32009-08-18 11:43:17 +08005068 vb.vb_xv = xv;
5069 vb.vb_bh = xs->bucket->bu_bhs[block_off];
Tao Ma712e53e2009-03-12 08:37:34 +08005070 ret = __ocfs2_xattr_set_value_outside(inode, handle,
Tao Ma492a8a32009-08-18 11:43:17 +08005071 &vb, val, value_len);
Tao Ma712e53e2009-03-12 08:37:34 +08005072 if (ret)
5073 mlog_errno(ret);
5074out:
5075 return ret;
Tao Ma01225592008-08-18 17:38:53 +08005076}
5077
Tao Ma01225592008-08-18 17:38:53 +08005078static int ocfs2_rm_xattr_cluster(struct inode *inode,
5079 struct buffer_head *root_bh,
5080 u64 blkno,
5081 u32 cpos,
Tao Ma47bca492009-08-18 11:43:42 +08005082 u32 len,
5083 void *para)
Tao Ma01225592008-08-18 17:38:53 +08005084{
5085 int ret;
5086 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5087 struct inode *tl_inode = osb->osb_tl_inode;
5088 handle_t *handle;
5089 struct ocfs2_xattr_block *xb =
5090 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08005091 struct ocfs2_alloc_context *meta_ac = NULL;
5092 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07005093 struct ocfs2_extent_tree et;
5094
Tao Ma47bca492009-08-18 11:43:42 +08005095 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
Tao Mace9c5a52009-08-18 11:43:59 +08005096 ocfs2_delete_xattr_in_bucket, para);
Tao Ma47bca492009-08-18 11:43:42 +08005097 if (ret) {
5098 mlog_errno(ret);
5099 return ret;
5100 }
5101
Joel Becker5e404e92009-02-13 03:54:22 -08005102 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Tao Ma01225592008-08-18 17:38:53 +08005103
5104 ocfs2_init_dealloc_ctxt(&dealloc);
5105
5106 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5107 cpos, len, (unsigned long long)blkno);
5108
Joel Becker8cb471e2009-02-10 20:00:41 -08005109 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5110 len);
Tao Ma01225592008-08-18 17:38:53 +08005111
Joel Beckerf99b9b72008-08-20 19:36:33 -07005112 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08005113 if (ret) {
5114 mlog_errno(ret);
5115 return ret;
5116 }
5117
5118 mutex_lock(&tl_inode->i_mutex);
5119
5120 if (ocfs2_truncate_log_needs_flush(osb)) {
5121 ret = __ocfs2_flush_truncate_log(osb);
5122 if (ret < 0) {
5123 mlog_errno(ret);
5124 goto out;
5125 }
5126 }
5127
Jan Karaa90714c2008-10-09 19:38:40 +02005128 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Tao Mad3264792008-10-24 07:57:28 +08005129 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08005130 ret = -ENOMEM;
5131 mlog_errno(ret);
5132 goto out;
5133 }
5134
Joel Becker0cf2f762009-02-12 16:41:25 -08005135 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08005136 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005137 if (ret) {
5138 mlog_errno(ret);
5139 goto out_commit;
5140 }
5141
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005142 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005143 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08005144 if (ret) {
5145 mlog_errno(ret);
5146 goto out_commit;
5147 }
5148
5149 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5150
5151 ret = ocfs2_journal_dirty(handle, root_bh);
5152 if (ret) {
5153 mlog_errno(ret);
5154 goto out_commit;
5155 }
5156
5157 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5158 if (ret)
5159 mlog_errno(ret);
5160
5161out_commit:
5162 ocfs2_commit_trans(osb, handle);
5163out:
5164 ocfs2_schedule_truncate_log_flush(osb, 1);
5165
5166 mutex_unlock(&tl_inode->i_mutex);
5167
5168 if (meta_ac)
5169 ocfs2_free_alloc_context(meta_ac);
5170
5171 ocfs2_run_deallocs(osb, &dealloc);
5172
5173 return ret;
5174}
5175
Tao Ma01225592008-08-18 17:38:53 +08005176static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005177 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08005178 struct ocfs2_xattr_search *xs)
5179{
Joel Beckerba937122008-10-24 19:13:20 -07005180 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005181 struct ocfs2_xattr_entry *last = &xh->xh_entries[
5182 le16_to_cpu(xh->xh_count) - 1];
5183 int ret = 0;
5184
Joel Beckerba937122008-10-24 19:13:20 -07005185 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07005186 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005187 if (ret) {
5188 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08005189 return;
Tao Ma01225592008-08-18 17:38:53 +08005190 }
5191
5192 /* Remove the old entry. */
5193 memmove(xs->here, xs->here + 1,
5194 (void *)last - (void *)xs->here);
5195 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
5196 le16_add_cpu(&xh->xh_count, -1);
5197
Joel Beckerba937122008-10-24 19:13:20 -07005198 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005199}
5200
5201/*
5202 * Set the xattr name/value in the bucket specified in xs.
5203 *
5204 * As the new value in xi may be stored in the bucket or in an outside cluster,
5205 * we divide the whole process into 3 steps:
5206 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
5207 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
5208 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
5209 * 4. If the clusters for the new outside value can't be allocated, we need
5210 * to free the xattr we allocated in set.
5211 */
5212static int ocfs2_xattr_set_in_bucket(struct inode *inode,
5213 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08005214 struct ocfs2_xattr_search *xs,
5215 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005216{
Tao Ma5a095612008-09-19 22:17:41 +08005217 int ret, local = 1;
Tao Ma01225592008-08-18 17:38:53 +08005218 size_t value_len;
5219 char *val = (char *)xi->value;
5220 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma2057e5c2008-10-09 23:06:13 +08005221 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
5222 strlen(xi->name));
Tao Ma01225592008-08-18 17:38:53 +08005223
5224 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
5225 /*
5226 * We need to truncate the xattr storage first.
5227 *
5228 * If both the old and new value are stored to
5229 * outside block, we only need to truncate
5230 * the storage and then set the value outside.
5231 *
5232 * If the new value should be stored within block,
5233 * we should free all the outside block first and
5234 * the modification to the xattr block will be done
5235 * by following steps.
5236 */
5237 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
5238 value_len = xi->value_len;
5239 else
5240 value_len = 0;
5241
5242 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08005243 value_len,
5244 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005245 if (ret)
5246 goto out;
5247
5248 if (value_len)
5249 goto set_value_outside;
5250 }
5251
5252 value_len = xi->value_len;
5253 /* So we have to handle the inside block change now. */
5254 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
5255 /*
5256 * If the new value will be stored outside of block,
5257 * initalize a new empty value root and insert it first.
5258 */
5259 local = 0;
5260 xi->value = &def_xv;
5261 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
5262 }
5263
Tao Ma85db90e2008-11-12 08:27:01 +08005264 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
5265 name_hash, local);
Tao Ma01225592008-08-18 17:38:53 +08005266 if (ret) {
5267 mlog_errno(ret);
5268 goto out;
5269 }
5270
Tao Ma5a095612008-09-19 22:17:41 +08005271 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
5272 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005273
Tao Ma5a095612008-09-19 22:17:41 +08005274 /* allocate the space now for the outside block storage. */
5275 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08005276 value_len, ctxt);
Tao Ma5a095612008-09-19 22:17:41 +08005277 if (ret) {
5278 mlog_errno(ret);
5279
5280 if (xs->not_found) {
5281 /*
5282 * We can't allocate enough clusters for outside
5283 * storage and we have allocated xattr already,
5284 * so need to remove it.
5285 */
Tao Ma85db90e2008-11-12 08:27:01 +08005286 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
Tao Ma01225592008-08-18 17:38:53 +08005287 }
Tao Ma01225592008-08-18 17:38:53 +08005288 goto out;
5289 }
5290
5291set_value_outside:
Tao Ma85db90e2008-11-12 08:27:01 +08005292 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
5293 xs, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08005294out:
5295 return ret;
5296}
5297
Tao Ma80bcaf32008-10-27 06:06:24 +08005298/*
5299 * check whether the xattr bucket is filled up with the same hash value.
5300 * If we want to insert the xattr with the same hash, return -ENOSPC.
5301 * If we want to insert a xattr with different hash value, go ahead
5302 * and ocfs2_divide_xattr_bucket will handle this.
5303 */
Tao Ma01225592008-08-18 17:38:53 +08005304static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08005305 struct ocfs2_xattr_bucket *bucket,
5306 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08005307{
Joel Becker3e632942008-10-24 17:04:49 -07005308 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08005309 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5310
5311 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5312 return 0;
Tao Ma01225592008-08-18 17:38:53 +08005313
5314 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5315 xh->xh_entries[0].xe_name_hash) {
5316 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5317 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07005318 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08005319 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5320 return -ENOSPC;
5321 }
5322
5323 return 0;
5324}
5325
5326static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5327 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08005328 struct ocfs2_xattr_search *xs,
5329 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005330{
5331 struct ocfs2_xattr_header *xh;
5332 struct ocfs2_xattr_entry *xe;
5333 u16 count, header_size, xh_free_start;
Joel Becker6dde41d2008-10-24 17:16:48 -07005334 int free, max_free, need, old;
Tao Ma01225592008-08-18 17:38:53 +08005335 size_t value_size = 0, name_len = strlen(xi->name);
5336 size_t blocksize = inode->i_sb->s_blocksize;
5337 int ret, allocation = 0;
Tao Ma01225592008-08-18 17:38:53 +08005338
5339 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
5340
5341try_again:
5342 xh = xs->header;
5343 count = le16_to_cpu(xh->xh_count);
5344 xh_free_start = le16_to_cpu(xh->xh_free_start);
5345 header_size = sizeof(struct ocfs2_xattr_header) +
5346 count * sizeof(struct ocfs2_xattr_entry);
Tiger Yang4442f512009-02-20 11:11:50 +08005347 max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
5348 le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005349
5350 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5351 "of %u which exceed block size\n",
Joel Beckerba937122008-10-24 19:13:20 -07005352 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005353 header_size);
5354
5355 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
5356 value_size = OCFS2_XATTR_ROOT_SIZE;
5357 else if (xi->value)
5358 value_size = OCFS2_XATTR_SIZE(xi->value_len);
5359
5360 if (xs->not_found)
5361 need = sizeof(struct ocfs2_xattr_entry) +
5362 OCFS2_XATTR_SIZE(name_len) + value_size;
5363 else {
5364 need = value_size + OCFS2_XATTR_SIZE(name_len);
5365
5366 /*
5367 * We only replace the old value if the new length is smaller
5368 * than the old one. Otherwise we will allocate new space in the
5369 * bucket to store it.
5370 */
5371 xe = xs->here;
5372 if (ocfs2_xattr_is_local(xe))
5373 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5374 else
5375 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5376
5377 if (old >= value_size)
5378 need = 0;
5379 }
5380
Tiger Yang4442f512009-02-20 11:11:50 +08005381 free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005382 /*
5383 * We need to make sure the new name/value pair
5384 * can exist in the same block.
5385 */
5386 if (xh_free_start % blocksize < need)
5387 free -= xh_free_start % blocksize;
5388
5389 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5390 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5391 " %u\n", xs->not_found,
Joel Beckerba937122008-10-24 19:13:20 -07005392 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005393 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5394 le16_to_cpu(xh->xh_name_value_len));
5395
Tao Ma976331d2008-11-12 08:26:57 +08005396 if (free < need ||
5397 (xs->not_found &&
5398 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
Tao Ma01225592008-08-18 17:38:53 +08005399 if (need <= max_free &&
5400 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5401 /*
5402 * We can create the space by defragment. Since only the
5403 * name/value will be moved, the xe shouldn't be changed
5404 * in xs.
5405 */
Tao Ma85db90e2008-11-12 08:27:01 +08005406 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5407 xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005408 if (ret) {
5409 mlog_errno(ret);
5410 goto out;
5411 }
5412
5413 xh_free_start = le16_to_cpu(xh->xh_free_start);
Tiger Yang4442f512009-02-20 11:11:50 +08005414 free = xh_free_start - header_size
5415 - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005416 if (xh_free_start % blocksize < need)
5417 free -= xh_free_start % blocksize;
5418
5419 if (free >= need)
5420 goto xattr_set;
5421
5422 mlog(0, "Can't get enough space for xattr insert by "
5423 "defragment. Need %u bytes, but we have %d, so "
5424 "allocate new bucket for it.\n", need, free);
5425 }
5426
5427 /*
5428 * We have to add new buckets or clusters and one
5429 * allocation should leave us enough space for insert.
5430 */
5431 BUG_ON(allocation);
5432
5433 /*
5434 * We do not allow for overlapping ranges between buckets. And
5435 * the maximum number of collisions we will allow for then is
5436 * one bucket's worth, so check it here whether we need to
5437 * add a new bucket for the insert.
5438 */
Tao Ma80bcaf32008-10-27 06:06:24 +08005439 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Beckerba937122008-10-24 19:13:20 -07005440 xs->bucket,
Tao Ma80bcaf32008-10-27 06:06:24 +08005441 xi->name);
Tao Ma01225592008-08-18 17:38:53 +08005442 if (ret) {
5443 mlog_errno(ret);
5444 goto out;
5445 }
5446
5447 ret = ocfs2_add_new_xattr_bucket(inode,
5448 xs->xattr_bh,
Joel Becker91f20332008-11-26 15:25:41 -08005449 xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005450 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005451 if (ret) {
5452 mlog_errno(ret);
5453 goto out;
5454 }
5455
Joel Becker91f20332008-11-26 15:25:41 -08005456 /*
5457 * ocfs2_add_new_xattr_bucket() will have updated
5458 * xs->bucket if it moved, but it will not have updated
5459 * any of the other search fields. Thus, we drop it and
5460 * re-search. Everything should be cached, so it'll be
5461 * quick.
5462 */
Joel Beckerba937122008-10-24 19:13:20 -07005463 ocfs2_xattr_bucket_relse(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005464 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5465 xi->name_index,
5466 xi->name, xs);
5467 if (ret && ret != -ENODATA)
5468 goto out;
5469 xs->not_found = ret;
5470 allocation = 1;
5471 goto try_again;
5472 }
5473
5474xattr_set:
Tao Ma78f30c32008-11-12 08:27:00 +08005475 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005476out:
5477 mlog_exit(ret);
5478 return ret;
5479}
Tao Maa3944252008-08-18 17:38:54 +08005480
5481static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5482 struct ocfs2_xattr_bucket *bucket,
5483 void *para)
5484{
Tao Mace9c5a52009-08-18 11:43:59 +08005485 int ret = 0, ref_credits;
Joel Becker3e632942008-10-24 17:04:49 -07005486 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08005487 u16 i;
5488 struct ocfs2_xattr_entry *xe;
Tao Ma78f30c32008-11-12 08:27:00 +08005489 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5490 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
Joel Becker548b0f22008-11-24 19:32:13 -08005491 int credits = ocfs2_remove_extent_credits(osb->sb) +
5492 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Mace9c5a52009-08-18 11:43:59 +08005493 struct ocfs2_xattr_value_root *xv;
5494 struct ocfs2_rm_xattr_bucket_para *args =
5495 (struct ocfs2_rm_xattr_bucket_para *)para;
Tao Ma78f30c32008-11-12 08:27:00 +08005496
5497 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005498
5499 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5500 xe = &xh->xh_entries[i];
5501 if (ocfs2_xattr_is_local(xe))
5502 continue;
5503
Tao Mace9c5a52009-08-18 11:43:59 +08005504 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5505 i, &xv, NULL);
5506
5507 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5508 args->ref_ci,
5509 args->ref_root_bh,
5510 &ctxt.meta_ac,
5511 &ref_credits);
5512
5513 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma88c3b062008-12-11 08:54:11 +08005514 if (IS_ERR(ctxt.handle)) {
5515 ret = PTR_ERR(ctxt.handle);
5516 mlog_errno(ret);
5517 break;
5518 }
5519
Joel Becker548b0f22008-11-24 19:32:13 -08005520 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005521 i, 0, &ctxt);
Tao Ma88c3b062008-12-11 08:54:11 +08005522
5523 ocfs2_commit_trans(osb, ctxt.handle);
Tao Mace9c5a52009-08-18 11:43:59 +08005524 if (ctxt.meta_ac) {
5525 ocfs2_free_alloc_context(ctxt.meta_ac);
5526 ctxt.meta_ac = NULL;
5527 }
Tao Maa3944252008-08-18 17:38:54 +08005528 if (ret) {
5529 mlog_errno(ret);
5530 break;
5531 }
5532 }
5533
Tao Mace9c5a52009-08-18 11:43:59 +08005534 if (ctxt.meta_ac)
5535 ocfs2_free_alloc_context(ctxt.meta_ac);
Tao Ma78f30c32008-11-12 08:27:00 +08005536 ocfs2_schedule_truncate_log_flush(osb, 1);
5537 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005538 return ret;
5539}
5540
Mark Fasheh99219ae2008-10-07 14:52:59 -07005541/*
Tao Ma492a8a32009-08-18 11:43:17 +08005542 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5543 * or change the extent record flag), we need to recalculate
5544 * the metaecc for the whole bucket. So it is done here.
5545 *
5546 * Note:
5547 * We have to give the extra credits for the caller.
5548 */
5549static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5550 handle_t *handle,
5551 void *para)
5552{
5553 int ret;
5554 struct ocfs2_xattr_bucket *bucket =
5555 (struct ocfs2_xattr_bucket *)para;
5556
5557 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5558 OCFS2_JOURNAL_ACCESS_WRITE);
5559 if (ret) {
5560 mlog_errno(ret);
5561 return ret;
5562 }
5563
5564 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5565
5566 return 0;
5567}
5568
5569/*
5570 * Special action we need if the xattr value is refcounted.
5571 *
5572 * 1. If the xattr is refcounted, lock the tree.
5573 * 2. CoW the xattr if we are setting the new value and the value
5574 * will be stored outside.
5575 * 3. In other case, decrease_refcount will work for us, so just
5576 * lock the refcount tree, calculate the meta and credits is OK.
5577 *
5578 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5579 * currently CoW is a completed transaction, while this function
5580 * will also lock the allocators and let us deadlock. So we will
5581 * CoW the whole xattr value.
5582 */
5583static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5584 struct ocfs2_dinode *di,
5585 struct ocfs2_xattr_info *xi,
5586 struct ocfs2_xattr_search *xis,
5587 struct ocfs2_xattr_search *xbs,
5588 struct ocfs2_refcount_tree **ref_tree,
5589 int *meta_add,
5590 int *credits)
5591{
5592 int ret = 0;
5593 struct ocfs2_xattr_block *xb;
5594 struct ocfs2_xattr_entry *xe;
5595 char *base;
5596 u32 p_cluster, num_clusters;
5597 unsigned int ext_flags;
5598 int name_offset, name_len;
5599 struct ocfs2_xattr_value_buf vb;
5600 struct ocfs2_xattr_bucket *bucket = NULL;
5601 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5602 struct ocfs2_post_refcount refcount;
5603 struct ocfs2_post_refcount *p = NULL;
5604 struct buffer_head *ref_root_bh = NULL;
5605
5606 if (!xis->not_found) {
5607 xe = xis->here;
5608 name_offset = le16_to_cpu(xe->xe_name_offset);
5609 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5610 base = xis->base;
5611 vb.vb_bh = xis->inode_bh;
5612 vb.vb_access = ocfs2_journal_access_di;
5613 } else {
5614 int i, block_off = 0;
5615 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5616 xe = xbs->here;
5617 name_offset = le16_to_cpu(xe->xe_name_offset);
5618 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5619 i = xbs->here - xbs->header->xh_entries;
5620
5621 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08005622 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma492a8a32009-08-18 11:43:17 +08005623 bucket_xh(xbs->bucket),
5624 i, &block_off,
5625 &name_offset);
5626 if (ret) {
5627 mlog_errno(ret);
5628 goto out;
5629 }
5630 base = bucket_block(xbs->bucket, block_off);
5631 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5632 vb.vb_access = ocfs2_journal_access;
5633
5634 if (ocfs2_meta_ecc(osb)) {
5635 /*create parameters for ocfs2_post_refcount. */
5636 bucket = xbs->bucket;
5637 refcount.credits = bucket->bu_blocks;
5638 refcount.para = bucket;
5639 refcount.func =
5640 ocfs2_xattr_bucket_post_refcount;
5641 p = &refcount;
5642 }
5643 } else {
5644 base = xbs->base;
5645 vb.vb_bh = xbs->xattr_bh;
5646 vb.vb_access = ocfs2_journal_access_xb;
5647 }
5648 }
5649
5650 if (ocfs2_xattr_is_local(xe))
5651 goto out;
5652
5653 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5654 (base + name_offset + name_len);
5655
5656 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5657 &num_clusters, &vb.vb_xv->xr_list,
5658 &ext_flags);
5659 if (ret) {
5660 mlog_errno(ret);
5661 goto out;
5662 }
5663
5664 /*
5665 * We just need to check the 1st extent record, since we always
5666 * CoW the whole xattr. So there shouldn't be a xattr with
5667 * some REFCOUNT extent recs after the 1st one.
5668 */
5669 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5670 goto out;
5671
5672 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5673 1, ref_tree, &ref_root_bh);
5674 if (ret) {
5675 mlog_errno(ret);
5676 goto out;
5677 }
5678
5679 /*
5680 * If we are deleting the xattr or the new size will be stored inside,
5681 * cool, leave it there, the xattr truncate process will remove them
5682 * for us(it still needs the refcount tree lock and the meta, credits).
5683 * And the worse case is that every cluster truncate will split the
5684 * refcount tree, and make the original extent become 3. So we will need
5685 * 2 * cluster more extent recs at most.
5686 */
5687 if (!xi->value || xi->value_len <= OCFS2_XATTR_INLINE_SIZE) {
5688
5689 ret = ocfs2_refcounted_xattr_delete_need(inode,
5690 &(*ref_tree)->rf_ci,
5691 ref_root_bh, vb.vb_xv,
5692 meta_add, credits);
5693 if (ret)
5694 mlog_errno(ret);
5695 goto out;
5696 }
5697
5698 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5699 *ref_tree, ref_root_bh, 0,
5700 le32_to_cpu(vb.vb_xv->xr_clusters), p);
5701 if (ret)
5702 mlog_errno(ret);
5703
5704out:
5705 brelse(ref_root_bh);
5706 return ret;
5707}
5708
5709/*
Tao Ma01292412009-09-21 13:04:19 +08005710 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5711 * The physical clusters will be added to refcount tree.
5712 */
5713static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5714 struct ocfs2_xattr_value_root *xv,
5715 struct ocfs2_extent_tree *value_et,
5716 struct ocfs2_caching_info *ref_ci,
5717 struct buffer_head *ref_root_bh,
5718 struct ocfs2_cached_dealloc_ctxt *dealloc,
5719 struct ocfs2_post_refcount *refcount)
5720{
5721 int ret = 0;
5722 u32 clusters = le32_to_cpu(xv->xr_clusters);
5723 u32 cpos, p_cluster, num_clusters;
5724 struct ocfs2_extent_list *el = &xv->xr_list;
5725 unsigned int ext_flags;
5726
5727 cpos = 0;
5728 while (cpos < clusters) {
5729 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5730 &num_clusters, el, &ext_flags);
5731
5732 cpos += num_clusters;
5733 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5734 continue;
5735
5736 BUG_ON(!p_cluster);
5737
5738 ret = ocfs2_add_refcount_flag(inode, value_et,
5739 ref_ci, ref_root_bh,
5740 cpos - num_clusters,
5741 p_cluster, num_clusters,
5742 dealloc, refcount);
5743 if (ret) {
5744 mlog_errno(ret);
5745 break;
5746 }
5747 }
5748
5749 return ret;
5750}
5751
5752/*
5753 * Given a normal ocfs2_xattr_header, refcount all the entries which
5754 * have value stored outside.
5755 * Used for xattrs stored in inode and ocfs2_xattr_block.
5756 */
5757static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5758 struct ocfs2_xattr_value_buf *vb,
5759 struct ocfs2_xattr_header *header,
5760 struct ocfs2_caching_info *ref_ci,
5761 struct buffer_head *ref_root_bh,
5762 struct ocfs2_cached_dealloc_ctxt *dealloc)
5763{
5764
5765 struct ocfs2_xattr_entry *xe;
5766 struct ocfs2_xattr_value_root *xv;
5767 struct ocfs2_extent_tree et;
5768 int i, ret = 0;
5769
5770 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5771 xe = &header->xh_entries[i];
5772
5773 if (ocfs2_xattr_is_local(xe))
5774 continue;
5775
5776 xv = (struct ocfs2_xattr_value_root *)((void *)header +
5777 le16_to_cpu(xe->xe_name_offset) +
5778 OCFS2_XATTR_SIZE(xe->xe_name_len));
5779
5780 vb->vb_xv = xv;
5781 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5782
5783 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5784 ref_ci, ref_root_bh,
5785 dealloc, NULL);
5786 if (ret) {
5787 mlog_errno(ret);
5788 break;
5789 }
5790 }
5791
5792 return ret;
5793}
5794
5795static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5796 struct buffer_head *fe_bh,
5797 struct ocfs2_caching_info *ref_ci,
5798 struct buffer_head *ref_root_bh,
5799 struct ocfs2_cached_dealloc_ctxt *dealloc)
5800{
5801 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5802 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5803 (fe_bh->b_data + inode->i_sb->s_blocksize -
5804 le16_to_cpu(di->i_xattr_inline_size));
5805 struct ocfs2_xattr_value_buf vb = {
5806 .vb_bh = fe_bh,
5807 .vb_access = ocfs2_journal_access_di,
5808 };
5809
5810 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5811 ref_ci, ref_root_bh, dealloc);
5812}
5813
5814struct ocfs2_xattr_tree_value_refcount_para {
5815 struct ocfs2_caching_info *ref_ci;
5816 struct buffer_head *ref_root_bh;
5817 struct ocfs2_cached_dealloc_ctxt *dealloc;
5818};
5819
5820static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
5821 struct ocfs2_xattr_bucket *bucket,
5822 int offset,
5823 struct ocfs2_xattr_value_root **xv,
5824 struct buffer_head **bh)
5825{
5826 int ret, block_off, name_offset;
5827 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5828 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
5829 void *base;
5830
5831 ret = ocfs2_xattr_bucket_get_name_value(sb,
5832 bucket_xh(bucket),
5833 offset,
5834 &block_off,
5835 &name_offset);
5836 if (ret) {
5837 mlog_errno(ret);
5838 goto out;
5839 }
5840
5841 base = bucket_block(bucket, block_off);
5842
5843 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
5844 OCFS2_XATTR_SIZE(xe->xe_name_len));
5845
5846 if (bh)
5847 *bh = bucket->bu_bhs[block_off];
5848out:
5849 return ret;
5850}
5851
5852/*
5853 * For a given xattr bucket, refcount all the entries which
5854 * have value stored outside.
5855 */
5856static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
5857 struct ocfs2_xattr_bucket *bucket,
5858 void *para)
5859{
5860 int i, ret = 0;
5861 struct ocfs2_extent_tree et;
5862 struct ocfs2_xattr_tree_value_refcount_para *ref =
5863 (struct ocfs2_xattr_tree_value_refcount_para *)para;
5864 struct ocfs2_xattr_header *xh =
5865 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
5866 struct ocfs2_xattr_entry *xe;
5867 struct ocfs2_xattr_value_buf vb = {
5868 .vb_access = ocfs2_journal_access,
5869 };
5870 struct ocfs2_post_refcount refcount = {
5871 .credits = bucket->bu_blocks,
5872 .para = bucket,
5873 .func = ocfs2_xattr_bucket_post_refcount,
5874 };
5875 struct ocfs2_post_refcount *p = NULL;
5876
5877 /* We only need post_refcount if we support metaecc. */
5878 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
5879 p = &refcount;
5880
5881 mlog(0, "refcount bucket %llu, count = %u\n",
5882 (unsigned long long)bucket_blkno(bucket),
5883 le16_to_cpu(xh->xh_count));
5884 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5885 xe = &xh->xh_entries[i];
5886
5887 if (ocfs2_xattr_is_local(xe))
5888 continue;
5889
5890 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
5891 &vb.vb_xv, &vb.vb_bh);
5892 if (ret) {
5893 mlog_errno(ret);
5894 break;
5895 }
5896
5897 ocfs2_init_xattr_value_extent_tree(&et,
5898 INODE_CACHE(inode), &vb);
5899
5900 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
5901 &et, ref->ref_ci,
5902 ref->ref_root_bh,
5903 ref->dealloc, p);
5904 if (ret) {
5905 mlog_errno(ret);
5906 break;
5907 }
5908 }
5909
5910 return ret;
5911
5912}
5913
5914static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
5915 struct buffer_head *root_bh,
5916 u64 blkno, u32 cpos, u32 len, void *para)
5917{
5918 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
5919 ocfs2_xattr_bucket_value_refcount,
5920 para);
5921}
5922
5923static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
5924 struct buffer_head *blk_bh,
5925 struct ocfs2_caching_info *ref_ci,
5926 struct buffer_head *ref_root_bh,
5927 struct ocfs2_cached_dealloc_ctxt *dealloc)
5928{
5929 int ret = 0;
5930 struct ocfs2_xattr_block *xb =
5931 (struct ocfs2_xattr_block *)blk_bh->b_data;
5932
5933 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
5934 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
5935 struct ocfs2_xattr_value_buf vb = {
5936 .vb_bh = blk_bh,
5937 .vb_access = ocfs2_journal_access_xb,
5938 };
5939
5940 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5941 ref_ci, ref_root_bh,
5942 dealloc);
5943 } else {
5944 struct ocfs2_xattr_tree_value_refcount_para para = {
5945 .ref_ci = ref_ci,
5946 .ref_root_bh = ref_root_bh,
5947 .dealloc = dealloc,
5948 };
5949
5950 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
5951 ocfs2_refcount_xattr_tree_rec,
5952 &para);
5953 }
5954
5955 return ret;
5956}
5957
5958int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
5959 struct buffer_head *fe_bh,
5960 struct ocfs2_caching_info *ref_ci,
5961 struct buffer_head *ref_root_bh,
5962 struct ocfs2_cached_dealloc_ctxt *dealloc)
5963{
5964 int ret = 0;
5965 struct ocfs2_inode_info *oi = OCFS2_I(inode);
5966 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5967 struct buffer_head *blk_bh = NULL;
5968
5969 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
5970 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
5971 ref_ci, ref_root_bh,
5972 dealloc);
5973 if (ret) {
5974 mlog_errno(ret);
5975 goto out;
5976 }
5977 }
5978
5979 if (!di->i_xattr_loc)
5980 goto out;
5981
5982 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
5983 &blk_bh);
5984 if (ret < 0) {
5985 mlog_errno(ret);
5986 goto out;
5987 }
5988
5989 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
5990 ref_root_bh, dealloc);
5991 if (ret)
5992 mlog_errno(ret);
5993
5994 brelse(blk_bh);
5995out:
5996
5997 return ret;
5998}
5999
Tao Ma0fe9b662009-08-18 11:47:56 +08006000typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
Tao Ma01292412009-09-21 13:04:19 +08006001/*
Tao Ma2999d122009-08-18 11:43:55 +08006002 * Store the information we need in xattr reflink.
6003 * old_bh and new_bh are inode bh for the old and new inode.
6004 */
6005struct ocfs2_xattr_reflink {
6006 struct inode *old_inode;
6007 struct inode *new_inode;
6008 struct buffer_head *old_bh;
6009 struct buffer_head *new_bh;
6010 struct ocfs2_caching_info *ref_ci;
6011 struct buffer_head *ref_root_bh;
6012 struct ocfs2_cached_dealloc_ctxt *dealloc;
Tao Ma0fe9b662009-08-18 11:47:56 +08006013 should_xattr_reflinked *xattr_reflinked;
Tao Ma2999d122009-08-18 11:43:55 +08006014};
6015
6016/*
6017 * Given a xattr header and xe offset,
6018 * return the proper xv and the corresponding bh.
6019 * xattr in inode, block and xattr tree have different implementaions.
6020 */
6021typedef int (get_xattr_value_root)(struct super_block *sb,
6022 struct buffer_head *bh,
6023 struct ocfs2_xattr_header *xh,
6024 int offset,
6025 struct ocfs2_xattr_value_root **xv,
6026 struct buffer_head **ret_bh,
6027 void *para);
6028
6029/*
6030 * Calculate all the xattr value root metadata stored in this xattr header and
6031 * credits we need if we create them from the scratch.
6032 * We use get_xattr_value_root so that all types of xattr container can use it.
6033 */
6034static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6035 struct buffer_head *bh,
6036 struct ocfs2_xattr_header *xh,
6037 int *metas, int *credits,
6038 int *num_recs,
6039 get_xattr_value_root *func,
6040 void *para)
6041{
6042 int i, ret = 0;
6043 struct ocfs2_xattr_value_root *xv;
6044 struct ocfs2_xattr_entry *xe;
6045
6046 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6047 xe = &xh->xh_entries[i];
6048 if (ocfs2_xattr_is_local(xe))
6049 continue;
6050
6051 ret = func(sb, bh, xh, i, &xv, NULL, para);
6052 if (ret) {
6053 mlog_errno(ret);
6054 break;
6055 }
6056
6057 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6058 le16_to_cpu(xv->xr_list.l_next_free_rec);
6059
6060 *credits += ocfs2_calc_extend_credits(sb,
6061 &def_xv.xv.xr_list,
6062 le32_to_cpu(xv->xr_clusters));
6063
6064 /*
6065 * If the value is a tree with depth > 1, We don't go deep
6066 * to the extent block, so just calculate a maximum record num.
6067 */
6068 if (!xv->xr_list.l_tree_depth)
6069 *num_recs += xv->xr_list.l_next_free_rec;
6070 else
6071 *num_recs += ocfs2_clusters_for_bytes(sb,
6072 XATTR_SIZE_MAX);
6073 }
6074
6075 return ret;
6076}
6077
6078/* Used by xattr inode and block to return the right xv and buffer_head. */
6079static int ocfs2_get_xattr_value_root(struct super_block *sb,
6080 struct buffer_head *bh,
6081 struct ocfs2_xattr_header *xh,
6082 int offset,
6083 struct ocfs2_xattr_value_root **xv,
6084 struct buffer_head **ret_bh,
6085 void *para)
6086{
6087 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6088
6089 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6090 le16_to_cpu(xe->xe_name_offset) +
6091 OCFS2_XATTR_SIZE(xe->xe_name_len));
6092
6093 if (ret_bh)
6094 *ret_bh = bh;
6095
6096 return 0;
6097}
6098
6099/*
6100 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6101 * It is only used for inline xattr and xattr block.
6102 */
6103static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6104 struct ocfs2_xattr_header *xh,
6105 struct buffer_head *ref_root_bh,
6106 int *credits,
6107 struct ocfs2_alloc_context **meta_ac)
6108{
6109 int ret, meta_add = 0, num_recs = 0;
6110 struct ocfs2_refcount_block *rb =
6111 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6112
6113 *credits = 0;
6114
6115 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6116 &meta_add, credits, &num_recs,
6117 ocfs2_get_xattr_value_root,
6118 NULL);
6119 if (ret) {
6120 mlog_errno(ret);
6121 goto out;
6122 }
6123
6124 /*
6125 * We need to add/modify num_recs in refcount tree, so just calculate
6126 * an approximate number we need for refcount tree change.
6127 * Sometimes we need to split the tree, and after split, half recs
6128 * will be moved to the new block, and a new block can only provide
6129 * half number of recs. So we multiple new blocks by 2.
6130 */
6131 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6132 meta_add += num_recs;
6133 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6134 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6135 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6136 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6137 else
6138 *credits += 1;
6139
6140 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6141 if (ret)
6142 mlog_errno(ret);
6143
6144out:
6145 return ret;
6146}
6147
6148/*
6149 * Given a xattr header, reflink all the xattrs in this container.
6150 * It can be used for inode, block and bucket.
6151 *
6152 * NOTE:
6153 * Before we call this function, the caller has memcpy the xattr in
6154 * old_xh to the new_xh.
Tao Ma0fe9b662009-08-18 11:47:56 +08006155 *
6156 * If args.xattr_reflinked is set, call it to decide whether the xe should
6157 * be reflinked or not. If not, remove it from the new xattr header.
Tao Ma2999d122009-08-18 11:43:55 +08006158 */
6159static int ocfs2_reflink_xattr_header(handle_t *handle,
6160 struct ocfs2_xattr_reflink *args,
6161 struct buffer_head *old_bh,
6162 struct ocfs2_xattr_header *xh,
6163 struct buffer_head *new_bh,
6164 struct ocfs2_xattr_header *new_xh,
6165 struct ocfs2_xattr_value_buf *vb,
6166 struct ocfs2_alloc_context *meta_ac,
6167 get_xattr_value_root *func,
6168 void *para)
6169{
Tao Ma0fe9b662009-08-18 11:47:56 +08006170 int ret = 0, i, j;
Tao Ma2999d122009-08-18 11:43:55 +08006171 struct super_block *sb = args->old_inode->i_sb;
6172 struct buffer_head *value_bh;
Tao Ma0fe9b662009-08-18 11:47:56 +08006173 struct ocfs2_xattr_entry *xe, *last;
Tao Ma2999d122009-08-18 11:43:55 +08006174 struct ocfs2_xattr_value_root *xv, *new_xv;
6175 struct ocfs2_extent_tree data_et;
6176 u32 clusters, cpos, p_cluster, num_clusters;
6177 unsigned int ext_flags = 0;
6178
6179 mlog(0, "reflink xattr in container %llu, count = %u\n",
6180 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
Tao Ma0fe9b662009-08-18 11:47:56 +08006181
6182 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6183 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
Tao Ma2999d122009-08-18 11:43:55 +08006184 xe = &xh->xh_entries[i];
6185
Tao Ma0fe9b662009-08-18 11:47:56 +08006186 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6187 xe = &new_xh->xh_entries[j];
6188
6189 le16_add_cpu(&new_xh->xh_count, -1);
6190 if (new_xh->xh_count) {
6191 memmove(xe, xe + 1,
6192 (void *)last - (void *)xe);
6193 memset(last, 0,
6194 sizeof(struct ocfs2_xattr_entry));
6195 }
6196
6197 /*
6198 * We don't want j to increase in the next round since
6199 * it is already moved ahead.
6200 */
6201 j--;
6202 continue;
6203 }
6204
Tao Ma2999d122009-08-18 11:43:55 +08006205 if (ocfs2_xattr_is_local(xe))
6206 continue;
6207
6208 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6209 if (ret) {
6210 mlog_errno(ret);
6211 break;
6212 }
6213
Tao Ma0fe9b662009-08-18 11:47:56 +08006214 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
Tao Ma2999d122009-08-18 11:43:55 +08006215 if (ret) {
6216 mlog_errno(ret);
6217 break;
6218 }
6219
6220 /*
6221 * For the xattr which has l_tree_depth = 0, all the extent
6222 * recs have already be copied to the new xh with the
6223 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6224 * increase the refount count int the refcount tree.
6225 *
6226 * For the xattr which has l_tree_depth > 0, we need
6227 * to initialize it to the empty default value root,
6228 * and then insert the extents one by one.
6229 */
6230 if (xv->xr_list.l_tree_depth) {
6231 memcpy(new_xv, &def_xv, sizeof(def_xv));
6232 vb->vb_xv = new_xv;
6233 vb->vb_bh = value_bh;
6234 ocfs2_init_xattr_value_extent_tree(&data_et,
6235 INODE_CACHE(args->new_inode), vb);
6236 }
6237
6238 clusters = le32_to_cpu(xv->xr_clusters);
6239 cpos = 0;
6240 while (cpos < clusters) {
6241 ret = ocfs2_xattr_get_clusters(args->old_inode,
6242 cpos,
6243 &p_cluster,
6244 &num_clusters,
6245 &xv->xr_list,
6246 &ext_flags);
6247 if (ret) {
6248 mlog_errno(ret);
6249 goto out;
6250 }
6251
6252 BUG_ON(!p_cluster);
6253
6254 if (xv->xr_list.l_tree_depth) {
6255 ret = ocfs2_insert_extent(handle,
6256 &data_et, cpos,
6257 ocfs2_clusters_to_blocks(
6258 args->old_inode->i_sb,
6259 p_cluster),
6260 num_clusters, ext_flags,
6261 meta_ac);
6262 if (ret) {
6263 mlog_errno(ret);
6264 goto out;
6265 }
6266 }
6267
6268 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6269 args->ref_root_bh,
6270 p_cluster, num_clusters,
6271 meta_ac, args->dealloc);
6272 if (ret) {
6273 mlog_errno(ret);
6274 goto out;
6275 }
6276
6277 cpos += num_clusters;
6278 }
6279 }
6280
6281out:
6282 return ret;
6283}
6284
6285static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6286{
6287 int ret = 0, credits = 0;
6288 handle_t *handle;
6289 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6290 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6291 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6292 int header_off = osb->sb->s_blocksize - inline_size;
6293 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6294 (args->old_bh->b_data + header_off);
6295 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6296 (args->new_bh->b_data + header_off);
6297 struct ocfs2_alloc_context *meta_ac = NULL;
6298 struct ocfs2_inode_info *new_oi;
6299 struct ocfs2_dinode *new_di;
6300 struct ocfs2_xattr_value_buf vb = {
6301 .vb_bh = args->new_bh,
6302 .vb_access = ocfs2_journal_access_di,
6303 };
6304
6305 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6306 &credits, &meta_ac);
6307 if (ret) {
6308 mlog_errno(ret);
6309 goto out;
6310 }
6311
6312 handle = ocfs2_start_trans(osb, credits);
6313 if (IS_ERR(handle)) {
6314 ret = PTR_ERR(handle);
6315 mlog_errno(ret);
6316 goto out;
6317 }
6318
6319 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6320 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6321 if (ret) {
6322 mlog_errno(ret);
6323 goto out_commit;
6324 }
6325
6326 memcpy(args->new_bh->b_data + header_off,
6327 args->old_bh->b_data + header_off, inline_size);
6328
6329 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6330 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6331
6332 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6333 args->new_bh, new_xh, &vb, meta_ac,
6334 ocfs2_get_xattr_value_root, NULL);
6335 if (ret) {
6336 mlog_errno(ret);
6337 goto out_commit;
6338 }
6339
6340 new_oi = OCFS2_I(args->new_inode);
6341 spin_lock(&new_oi->ip_lock);
6342 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6343 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6344 spin_unlock(&new_oi->ip_lock);
6345
6346 ocfs2_journal_dirty(handle, args->new_bh);
6347
6348out_commit:
6349 ocfs2_commit_trans(osb, handle);
6350
6351out:
6352 if (meta_ac)
6353 ocfs2_free_alloc_context(meta_ac);
6354 return ret;
6355}
6356
6357static int ocfs2_create_empty_xattr_block(struct inode *inode,
6358 struct buffer_head *fe_bh,
6359 struct buffer_head **ret_bh,
6360 int indexed)
6361{
6362 int ret;
6363 handle_t *handle;
6364 struct ocfs2_alloc_context *meta_ac;
6365 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6366
6367 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
6368 if (ret < 0) {
6369 mlog_errno(ret);
6370 return ret;
6371 }
6372
6373 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6374 if (IS_ERR(handle)) {
6375 ret = PTR_ERR(handle);
6376 mlog_errno(ret);
6377 goto out;
6378 }
6379
6380 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6381 (unsigned long long)fe_bh->b_blocknr, indexed);
6382 ret = ocfs2_create_xattr_block(handle, inode, fe_bh,
6383 meta_ac, ret_bh, indexed);
6384 if (ret)
6385 mlog_errno(ret);
6386
6387 ocfs2_commit_trans(osb, handle);
6388out:
6389 ocfs2_free_alloc_context(meta_ac);
6390 return ret;
6391}
6392
6393static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6394 struct buffer_head *blk_bh,
6395 struct buffer_head *new_blk_bh)
6396{
6397 int ret = 0, credits = 0;
6398 handle_t *handle;
6399 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6400 struct ocfs2_dinode *new_di;
6401 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6402 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6403 struct ocfs2_xattr_block *xb =
6404 (struct ocfs2_xattr_block *)blk_bh->b_data;
6405 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6406 struct ocfs2_xattr_block *new_xb =
6407 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6408 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6409 struct ocfs2_alloc_context *meta_ac;
6410 struct ocfs2_xattr_value_buf vb = {
6411 .vb_bh = new_blk_bh,
6412 .vb_access = ocfs2_journal_access_xb,
6413 };
6414
6415 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6416 &credits, &meta_ac);
6417 if (ret) {
6418 mlog_errno(ret);
6419 return ret;
6420 }
6421
6422 /* One more credits in case we need to add xattr flags in new inode. */
6423 handle = ocfs2_start_trans(osb, credits + 1);
6424 if (IS_ERR(handle)) {
6425 ret = PTR_ERR(handle);
6426 mlog_errno(ret);
6427 goto out;
6428 }
6429
6430 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6431 ret = ocfs2_journal_access_di(handle,
6432 INODE_CACHE(args->new_inode),
6433 args->new_bh,
6434 OCFS2_JOURNAL_ACCESS_WRITE);
6435 if (ret) {
6436 mlog_errno(ret);
6437 goto out_commit;
6438 }
6439 }
6440
6441 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6442 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6443 if (ret) {
6444 mlog_errno(ret);
6445 goto out_commit;
6446 }
6447
6448 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6449 osb->sb->s_blocksize - header_off);
6450
6451 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6452 new_blk_bh, new_xh, &vb, meta_ac,
6453 ocfs2_get_xattr_value_root, NULL);
6454 if (ret) {
6455 mlog_errno(ret);
6456 goto out_commit;
6457 }
6458
6459 ocfs2_journal_dirty(handle, new_blk_bh);
6460
6461 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6462 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6463 spin_lock(&new_oi->ip_lock);
6464 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6465 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6466 spin_unlock(&new_oi->ip_lock);
6467
6468 ocfs2_journal_dirty(handle, args->new_bh);
6469 }
6470
6471out_commit:
6472 ocfs2_commit_trans(osb, handle);
6473
6474out:
6475 ocfs2_free_alloc_context(meta_ac);
6476 return ret;
6477}
6478
6479struct ocfs2_reflink_xattr_tree_args {
6480 struct ocfs2_xattr_reflink *reflink;
6481 struct buffer_head *old_blk_bh;
6482 struct buffer_head *new_blk_bh;
6483 struct ocfs2_xattr_bucket *old_bucket;
6484 struct ocfs2_xattr_bucket *new_bucket;
6485};
6486
6487/*
6488 * NOTE:
6489 * We have to handle the case that both old bucket and new bucket
6490 * will call this function to get the right ret_bh.
6491 * So The caller must give us the right bh.
6492 */
6493static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6494 struct buffer_head *bh,
6495 struct ocfs2_xattr_header *xh,
6496 int offset,
6497 struct ocfs2_xattr_value_root **xv,
6498 struct buffer_head **ret_bh,
6499 void *para)
6500{
6501 struct ocfs2_reflink_xattr_tree_args *args =
6502 (struct ocfs2_reflink_xattr_tree_args *)para;
6503 struct ocfs2_xattr_bucket *bucket;
6504
6505 if (bh == args->old_bucket->bu_bhs[0])
6506 bucket = args->old_bucket;
6507 else
6508 bucket = args->new_bucket;
6509
6510 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6511 xv, ret_bh);
6512}
6513
6514struct ocfs2_value_tree_metas {
6515 int num_metas;
6516 int credits;
6517 int num_recs;
6518};
6519
6520static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6521 struct buffer_head *bh,
6522 struct ocfs2_xattr_header *xh,
6523 int offset,
6524 struct ocfs2_xattr_value_root **xv,
6525 struct buffer_head **ret_bh,
6526 void *para)
6527{
6528 struct ocfs2_xattr_bucket *bucket =
6529 (struct ocfs2_xattr_bucket *)para;
6530
6531 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6532 xv, ret_bh);
6533}
6534
6535static int ocfs2_calc_value_tree_metas(struct inode *inode,
6536 struct ocfs2_xattr_bucket *bucket,
6537 void *para)
6538{
6539 struct ocfs2_value_tree_metas *metas =
6540 (struct ocfs2_value_tree_metas *)para;
6541 struct ocfs2_xattr_header *xh =
6542 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6543
6544 /* Add the credits for this bucket first. */
6545 metas->credits += bucket->bu_blocks;
6546 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6547 xh, &metas->num_metas,
6548 &metas->credits, &metas->num_recs,
6549 ocfs2_value_tree_metas_in_bucket,
6550 bucket);
6551}
6552
6553/*
6554 * Given a xattr extent rec starting from blkno and having len clusters,
6555 * iterate all the buckets calculate how much metadata we need for reflinking
6556 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6557 */
6558static int ocfs2_lock_reflink_xattr_rec_allocators(
6559 struct ocfs2_reflink_xattr_tree_args *args,
6560 struct ocfs2_extent_tree *xt_et,
6561 u64 blkno, u32 len, int *credits,
6562 struct ocfs2_alloc_context **meta_ac,
6563 struct ocfs2_alloc_context **data_ac)
6564{
6565 int ret, num_free_extents;
6566 struct ocfs2_value_tree_metas metas;
6567 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6568 struct ocfs2_refcount_block *rb;
6569
6570 memset(&metas, 0, sizeof(metas));
6571
6572 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6573 ocfs2_calc_value_tree_metas, &metas);
6574 if (ret) {
6575 mlog_errno(ret);
6576 goto out;
6577 }
6578
6579 *credits = metas.credits;
6580
6581 /*
6582 * Calculate we need for refcount tree change.
6583 *
6584 * We need to add/modify num_recs in refcount tree, so just calculate
6585 * an approximate number we need for refcount tree change.
6586 * Sometimes we need to split the tree, and after split, half recs
6587 * will be moved to the new block, and a new block can only provide
6588 * half number of recs. So we multiple new blocks by 2.
6589 * In the end, we have to add credits for modifying the already
6590 * existed refcount block.
6591 */
6592 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6593 metas.num_recs =
6594 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6595 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6596 metas.num_metas += metas.num_recs;
6597 *credits += metas.num_recs +
6598 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6599 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6600 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6601 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6602 else
6603 *credits += 1;
6604
6605 /* count in the xattr tree change. */
6606 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6607 if (num_free_extents < 0) {
6608 ret = num_free_extents;
6609 mlog_errno(ret);
6610 goto out;
6611 }
6612
6613 if (num_free_extents < len)
6614 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6615
6616 *credits += ocfs2_calc_extend_credits(osb->sb,
6617 xt_et->et_root_el, len);
6618
6619 if (metas.num_metas) {
6620 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6621 meta_ac);
6622 if (ret) {
6623 mlog_errno(ret);
6624 goto out;
6625 }
6626 }
6627
6628 if (len) {
6629 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6630 if (ret)
6631 mlog_errno(ret);
6632 }
6633out:
6634 if (ret) {
6635 if (*meta_ac) {
6636 ocfs2_free_alloc_context(*meta_ac);
6637 meta_ac = NULL;
6638 }
6639 }
6640
6641 return ret;
6642}
6643
6644static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6645 u64 blkno, u64 new_blkno, u32 clusters,
6646 struct ocfs2_alloc_context *meta_ac,
6647 struct ocfs2_alloc_context *data_ac,
6648 struct ocfs2_reflink_xattr_tree_args *args)
6649{
6650 int i, j, ret = 0;
6651 struct super_block *sb = args->reflink->old_inode->i_sb;
6652 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
6653 u32 num_buckets = clusters * bpc;
6654 int bpb = args->old_bucket->bu_blocks;
6655 struct ocfs2_xattr_value_buf vb = {
6656 .vb_access = ocfs2_journal_access,
6657 };
6658
6659 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6660 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6661 if (ret) {
6662 mlog_errno(ret);
6663 break;
6664 }
6665
6666 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
6667 if (ret) {
6668 mlog_errno(ret);
6669 break;
6670 }
6671
6672 /*
6673 * The real bucket num in this series of blocks is stored
6674 * in the 1st bucket.
6675 */
6676 if (i == 0)
6677 num_buckets = le16_to_cpu(
6678 bucket_xh(args->old_bucket)->xh_num_buckets);
6679
6680 ret = ocfs2_xattr_bucket_journal_access(handle,
6681 args->new_bucket,
6682 OCFS2_JOURNAL_ACCESS_CREATE);
6683 if (ret) {
6684 mlog_errno(ret);
6685 break;
6686 }
6687
6688 for (j = 0; j < bpb; j++)
6689 memcpy(bucket_block(args->new_bucket, j),
6690 bucket_block(args->old_bucket, j),
6691 sb->s_blocksize);
6692
6693 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6694
6695 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6696 args->old_bucket->bu_bhs[0],
6697 bucket_xh(args->old_bucket),
6698 args->new_bucket->bu_bhs[0],
6699 bucket_xh(args->new_bucket),
6700 &vb, meta_ac,
6701 ocfs2_get_reflink_xattr_value_root,
6702 args);
6703 if (ret) {
6704 mlog_errno(ret);
6705 break;
6706 }
6707
6708 /*
6709 * Re-access and dirty the bucket to calculate metaecc.
6710 * Because we may extend the transaction in reflink_xattr_header
6711 * which will let the already accessed block gone.
6712 */
6713 ret = ocfs2_xattr_bucket_journal_access(handle,
6714 args->new_bucket,
6715 OCFS2_JOURNAL_ACCESS_WRITE);
6716 if (ret) {
6717 mlog_errno(ret);
6718 break;
6719 }
6720
6721 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6722 ocfs2_xattr_bucket_relse(args->old_bucket);
6723 ocfs2_xattr_bucket_relse(args->new_bucket);
6724 }
6725
6726 ocfs2_xattr_bucket_relse(args->old_bucket);
6727 ocfs2_xattr_bucket_relse(args->new_bucket);
6728 return ret;
6729}
6730/*
6731 * Create the same xattr extent record in the new inode's xattr tree.
6732 */
6733static int ocfs2_reflink_xattr_rec(struct inode *inode,
6734 struct buffer_head *root_bh,
6735 u64 blkno,
6736 u32 cpos,
6737 u32 len,
6738 void *para)
6739{
6740 int ret, credits = 0;
6741 u32 p_cluster, num_clusters;
6742 u64 new_blkno;
6743 handle_t *handle;
6744 struct ocfs2_reflink_xattr_tree_args *args =
6745 (struct ocfs2_reflink_xattr_tree_args *)para;
6746 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6747 struct ocfs2_alloc_context *meta_ac = NULL;
6748 struct ocfs2_alloc_context *data_ac = NULL;
6749 struct ocfs2_extent_tree et;
6750
6751 ocfs2_init_xattr_tree_extent_tree(&et,
6752 INODE_CACHE(args->reflink->new_inode),
6753 args->new_blk_bh);
6754
6755 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6756 len, &credits,
6757 &meta_ac, &data_ac);
6758 if (ret) {
6759 mlog_errno(ret);
6760 goto out;
6761 }
6762
6763 handle = ocfs2_start_trans(osb, credits);
6764 if (IS_ERR(handle)) {
6765 ret = PTR_ERR(handle);
6766 mlog_errno(ret);
6767 goto out;
6768 }
6769
6770 ret = ocfs2_claim_clusters(osb, handle, data_ac,
6771 len, &p_cluster, &num_clusters);
6772 if (ret) {
6773 mlog_errno(ret);
6774 goto out_commit;
6775 }
6776
6777 new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
6778
6779 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
6780 (unsigned long long)blkno, (unsigned long long)new_blkno, len);
6781 ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
6782 meta_ac, data_ac, args);
6783 if (ret) {
6784 mlog_errno(ret);
6785 goto out_commit;
6786 }
6787
6788 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6789 (unsigned long long)new_blkno, len, cpos);
6790 ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
6791 len, 0, meta_ac);
6792 if (ret)
6793 mlog_errno(ret);
6794
6795out_commit:
6796 ocfs2_commit_trans(osb, handle);
6797
6798out:
6799 if (meta_ac)
6800 ocfs2_free_alloc_context(meta_ac);
6801 if (data_ac)
6802 ocfs2_free_alloc_context(data_ac);
6803 return ret;
6804}
6805
6806/*
6807 * Create reflinked xattr buckets.
6808 * We will add bucket one by one, and refcount all the xattrs in the bucket
6809 * if they are stored outside.
6810 */
6811static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
6812 struct buffer_head *blk_bh,
6813 struct buffer_head *new_blk_bh)
6814{
6815 int ret;
6816 struct ocfs2_reflink_xattr_tree_args para;
6817
6818 memset(&para, 0, sizeof(para));
6819 para.reflink = args;
6820 para.old_blk_bh = blk_bh;
6821 para.new_blk_bh = new_blk_bh;
6822
6823 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
6824 if (!para.old_bucket) {
6825 mlog_errno(-ENOMEM);
6826 return -ENOMEM;
6827 }
6828
6829 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
6830 if (!para.new_bucket) {
6831 ret = -ENOMEM;
6832 mlog_errno(ret);
6833 goto out;
6834 }
6835
6836 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
6837 ocfs2_reflink_xattr_rec,
6838 &para);
6839 if (ret)
6840 mlog_errno(ret);
6841
6842out:
6843 ocfs2_xattr_bucket_free(para.old_bucket);
6844 ocfs2_xattr_bucket_free(para.new_bucket);
6845 return ret;
6846}
6847
6848static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
6849 struct buffer_head *blk_bh)
6850{
6851 int ret, indexed = 0;
6852 struct buffer_head *new_blk_bh = NULL;
6853 struct ocfs2_xattr_block *xb =
6854 (struct ocfs2_xattr_block *)blk_bh->b_data;
6855
6856
6857 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
6858 indexed = 1;
6859
6860 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
6861 &new_blk_bh, indexed);
6862 if (ret) {
6863 mlog_errno(ret);
6864 goto out;
6865 }
6866
6867 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
6868 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
6869 else
6870 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
6871 if (ret)
6872 mlog_errno(ret);
6873
6874out:
6875 brelse(new_blk_bh);
6876 return ret;
6877}
6878
Tao Ma0fe9b662009-08-18 11:47:56 +08006879static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
6880{
6881 int type = ocfs2_xattr_get_type(xe);
6882
6883 return type != OCFS2_XATTR_INDEX_SECURITY &&
6884 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
6885 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
6886}
6887
Tao Ma2999d122009-08-18 11:43:55 +08006888int ocfs2_reflink_xattrs(struct inode *old_inode,
6889 struct buffer_head *old_bh,
6890 struct inode *new_inode,
Tao Ma0fe9b662009-08-18 11:47:56 +08006891 struct buffer_head *new_bh,
6892 bool preserve_security)
Tao Ma2999d122009-08-18 11:43:55 +08006893{
6894 int ret;
6895 struct ocfs2_xattr_reflink args;
6896 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
6897 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
6898 struct buffer_head *blk_bh = NULL;
6899 struct ocfs2_cached_dealloc_ctxt dealloc;
6900 struct ocfs2_refcount_tree *ref_tree;
6901 struct buffer_head *ref_root_bh = NULL;
6902
6903 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
6904 le64_to_cpu(di->i_refcount_loc),
6905 1, &ref_tree, &ref_root_bh);
6906 if (ret) {
6907 mlog_errno(ret);
6908 goto out;
6909 }
6910
6911 ocfs2_init_dealloc_ctxt(&dealloc);
6912
6913 args.old_inode = old_inode;
6914 args.new_inode = new_inode;
6915 args.old_bh = old_bh;
6916 args.new_bh = new_bh;
6917 args.ref_ci = &ref_tree->rf_ci;
6918 args.ref_root_bh = ref_root_bh;
6919 args.dealloc = &dealloc;
Tao Ma0fe9b662009-08-18 11:47:56 +08006920 if (preserve_security)
6921 args.xattr_reflinked = NULL;
6922 else
6923 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
Tao Ma2999d122009-08-18 11:43:55 +08006924
6925 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6926 ret = ocfs2_reflink_xattr_inline(&args);
6927 if (ret) {
6928 mlog_errno(ret);
6929 goto out_unlock;
6930 }
6931 }
6932
6933 if (!di->i_xattr_loc)
6934 goto out_unlock;
6935
6936 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
6937 &blk_bh);
6938 if (ret < 0) {
6939 mlog_errno(ret);
6940 goto out_unlock;
6941 }
6942
6943 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
6944 if (ret)
6945 mlog_errno(ret);
6946
6947 brelse(blk_bh);
6948
6949out_unlock:
6950 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
6951 ref_tree, 1);
6952 brelse(ref_root_bh);
6953
6954 if (ocfs2_dealloc_has_cluster(&dealloc)) {
6955 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
6956 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
6957 }
6958
6959out:
6960 return ret;
6961}
6962
6963/*
Tao Ma0fe9b662009-08-18 11:47:56 +08006964 * Initialize security and acl for a already created inode.
6965 * Used for reflink a non-preserve-security file.
6966 *
6967 * It uses common api like ocfs2_xattr_set, so the caller
6968 * must not hold any lock expect i_mutex.
6969 */
6970int ocfs2_init_security_and_acl(struct inode *dir,
6971 struct inode *inode)
6972{
6973 int ret = 0;
6974 struct buffer_head *dir_bh = NULL;
6975 struct ocfs2_security_xattr_info si = {
6976 .enable = 1,
6977 };
6978
6979 ret = ocfs2_init_security_get(inode, dir, &si);
6980 if (!ret) {
6981 ret = ocfs2_xattr_security_set(inode, si.name,
6982 si.value, si.value_len,
6983 XATTR_CREATE);
6984 if (ret) {
6985 mlog_errno(ret);
6986 goto leave;
6987 }
6988 } else if (ret != -EOPNOTSUPP) {
6989 mlog_errno(ret);
6990 goto leave;
6991 }
6992
6993 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
6994 if (ret) {
6995 mlog_errno(ret);
6996 goto leave;
6997 }
6998
6999 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7000 if (ret)
7001 mlog_errno(ret);
7002
7003 ocfs2_inode_unlock(dir, 0);
7004 brelse(dir_bh);
7005leave:
7006 return ret;
7007}
7008/*
Tiger Yang923f7f32008-11-14 11:16:27 +08007009 * 'security' attributes support
7010 */
7011static size_t ocfs2_xattr_security_list(struct inode *inode, char *list,
7012 size_t list_size, const char *name,
7013 size_t name_len)
7014{
7015 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7016 const size_t total_len = prefix_len + name_len + 1;
7017
7018 if (list && total_len <= list_size) {
7019 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7020 memcpy(list + prefix_len, name, name_len);
7021 list[prefix_len + name_len] = '\0';
7022 }
7023 return total_len;
7024}
7025
7026static int ocfs2_xattr_security_get(struct inode *inode, const char *name,
7027 void *buffer, size_t size)
7028{
7029 if (strcmp(name, "") == 0)
7030 return -EINVAL;
7031 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, name,
7032 buffer, size);
7033}
7034
7035static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
7036 const void *value, size_t size, int flags)
7037{
7038 if (strcmp(name, "") == 0)
7039 return -EINVAL;
7040
7041 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, name, value,
7042 size, flags);
7043}
7044
Tiger Yang534eadd2008-11-14 11:16:41 +08007045int ocfs2_init_security_get(struct inode *inode,
7046 struct inode *dir,
7047 struct ocfs2_security_xattr_info *si)
7048{
Tiger Yang38d59ef2008-12-17 10:22:56 +08007049 /* check whether ocfs2 support feature xattr */
7050 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7051 return -EOPNOTSUPP;
Tiger Yang534eadd2008-11-14 11:16:41 +08007052 return security_inode_init_security(inode, dir, &si->name, &si->value,
7053 &si->value_len);
7054}
7055
7056int ocfs2_init_security_set(handle_t *handle,
7057 struct inode *inode,
7058 struct buffer_head *di_bh,
7059 struct ocfs2_security_xattr_info *si,
7060 struct ocfs2_alloc_context *xattr_ac,
7061 struct ocfs2_alloc_context *data_ac)
7062{
7063 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7064 OCFS2_XATTR_INDEX_SECURITY,
7065 si->name, si->value, si->value_len, 0,
7066 xattr_ac, data_ac);
7067}
7068
Tiger Yang923f7f32008-11-14 11:16:27 +08007069struct xattr_handler ocfs2_xattr_security_handler = {
7070 .prefix = XATTR_SECURITY_PREFIX,
7071 .list = ocfs2_xattr_security_list,
7072 .get = ocfs2_xattr_security_get,
7073 .set = ocfs2_xattr_security_set,
7074};
7075
7076/*
Mark Fasheh99219ae2008-10-07 14:52:59 -07007077 * 'trusted' attributes support
7078 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07007079static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
7080 size_t list_size, const char *name,
7081 size_t name_len)
7082{
Tiger Yangceb1eba2008-10-23 16:34:13 +08007083 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07007084 const size_t total_len = prefix_len + name_len + 1;
7085
7086 if (list && total_len <= list_size) {
7087 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7088 memcpy(list + prefix_len, name, name_len);
7089 list[prefix_len + name_len] = '\0';
7090 }
7091 return total_len;
7092}
7093
7094static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
7095 void *buffer, size_t size)
7096{
7097 if (strcmp(name, "") == 0)
7098 return -EINVAL;
7099 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
7100 buffer, size);
7101}
7102
7103static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
7104 const void *value, size_t size, int flags)
7105{
7106 if (strcmp(name, "") == 0)
7107 return -EINVAL;
7108
7109 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
7110 size, flags);
7111}
7112
7113struct xattr_handler ocfs2_xattr_trusted_handler = {
7114 .prefix = XATTR_TRUSTED_PREFIX,
7115 .list = ocfs2_xattr_trusted_list,
7116 .get = ocfs2_xattr_trusted_get,
7117 .set = ocfs2_xattr_trusted_set,
7118};
7119
Mark Fasheh99219ae2008-10-07 14:52:59 -07007120/*
7121 * 'user' attributes support
7122 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07007123static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
7124 size_t list_size, const char *name,
7125 size_t name_len)
7126{
Tiger Yangceb1eba2008-10-23 16:34:13 +08007127 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07007128 const size_t total_len = prefix_len + name_len + 1;
7129 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7130
7131 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7132 return 0;
7133
7134 if (list && total_len <= list_size) {
7135 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7136 memcpy(list + prefix_len, name, name_len);
7137 list[prefix_len + name_len] = '\0';
7138 }
7139 return total_len;
7140}
7141
7142static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
7143 void *buffer, size_t size)
7144{
7145 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7146
7147 if (strcmp(name, "") == 0)
7148 return -EINVAL;
7149 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7150 return -EOPNOTSUPP;
7151 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
7152 buffer, size);
7153}
7154
7155static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
7156 const void *value, size_t size, int flags)
7157{
7158 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7159
7160 if (strcmp(name, "") == 0)
7161 return -EINVAL;
7162 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7163 return -EOPNOTSUPP;
7164
7165 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
7166 size, flags);
7167}
7168
7169struct xattr_handler ocfs2_xattr_user_handler = {
7170 .prefix = XATTR_USER_PREFIX,
7171 .list = ocfs2_xattr_user_list,
7172 .get = ocfs2_xattr_user_get,
7173 .set = ocfs2_xattr_user_set,
7174};