Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 1 | /* -*- mode: c; c-basic-offset: 8; -*- |
| 2 | * vim: noexpandtab sw=8 ts=8 sts=0: |
| 3 | * |
| 4 | * xattr.c |
| 5 | * |
Tiger Yang | c3cb682 | 2008-10-23 16:33:03 +0800 | [diff] [blame] | 6 | * Copyright (C) 2004, 2008 Oracle. All rights reserved. |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 7 | * |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 8 | * CREDITS: |
Tiger Yang | c3cb682 | 2008-10-23 16:33:03 +0800 | [diff] [blame] | 9 | * 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 Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 11 | * |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public |
Tiger Yang | c3cb682 | 2008-10-23 16:33:03 +0800 | [diff] [blame] | 14 | * License version 2 as published by the Free Software Foundation. |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 15 | * |
| 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 Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 20 | */ |
| 21 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 22 | #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 Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 34 | #include <linux/sort.h> |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 35 | #include <linux/init.h> |
| 36 | #include <linux/module.h> |
| 37 | #include <linux/string.h> |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 38 | #include <linux/security.h> |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 39 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 40 | #define MLOG_MASK_PREFIX ML_XATTR |
| 41 | #include <cluster/masklog.h> |
| 42 | |
| 43 | #include "ocfs2.h" |
| 44 | #include "alloc.h" |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 45 | #include "blockcheck.h" |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 46 | #include "dlmglue.h" |
| 47 | #include "file.h" |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 48 | #include "symlink.h" |
| 49 | #include "sysfile.h" |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 50 | #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 Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 56 | #include "super.h" |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 57 | #include "xattr.h" |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 58 | #include "refcounttree.h" |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 59 | #include "acl.h" |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 60 | |
| 61 | struct ocfs2_xattr_def_value_root { |
| 62 | struct ocfs2_xattr_value_root xv; |
| 63 | struct ocfs2_extent_rec er; |
| 64 | }; |
| 65 | |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 66 | struct ocfs2_xattr_bucket { |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 67 | /* The inode these xattrs are associated with */ |
| 68 | struct inode *bu_inode; |
| 69 | |
| 70 | /* The actual buffers that make up the bucket */ |
Joel Becker | 4ac6032 | 2008-10-18 19:11:42 -0700 | [diff] [blame] | 71 | struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET]; |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 72 | |
| 73 | /* How many blocks make up one bucket for this filesystem */ |
| 74 | int bu_blocks; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 75 | }; |
| 76 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 77 | struct ocfs2_xattr_set_ctxt { |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 78 | handle_t *handle; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 79 | struct ocfs2_alloc_context *meta_ac; |
| 80 | struct ocfs2_alloc_context *data_ac; |
| 81 | struct ocfs2_cached_dealloc_ctxt dealloc; |
| 82 | }; |
| 83 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 84 | #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root)) |
| 85 | #define OCFS2_XATTR_INLINE_SIZE 80 |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 86 | #define OCFS2_XATTR_HEADER_GAP 4 |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 87 | #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \ |
| 88 | - sizeof(struct ocfs2_xattr_header) \ |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 89 | - OCFS2_XATTR_HEADER_GAP) |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 90 | #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 Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 93 | - OCFS2_XATTR_HEADER_GAP) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 94 | |
| 95 | static struct ocfs2_xattr_def_value_root def_xv = { |
| 96 | .xv.xr_list.l_count = cpu_to_le16(1), |
| 97 | }; |
| 98 | |
| 99 | struct xattr_handler *ocfs2_xattr_handlers[] = { |
| 100 | &ocfs2_xattr_user_handler, |
Tiger Yang | 929fb01 | 2008-11-14 11:17:04 +0800 | [diff] [blame] | 101 | &ocfs2_xattr_acl_access_handler, |
| 102 | &ocfs2_xattr_acl_default_handler, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 103 | &ocfs2_xattr_trusted_handler, |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 104 | &ocfs2_xattr_security_handler, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 105 | NULL |
| 106 | }; |
| 107 | |
Tiger Yang | c988fd0 | 2008-10-23 16:34:44 +0800 | [diff] [blame] | 108 | static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = { |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 109 | [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler, |
Tiger Yang | 929fb01 | 2008-11-14 11:17:04 +0800 | [diff] [blame] | 110 | [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS] |
| 111 | = &ocfs2_xattr_acl_access_handler, |
| 112 | [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT] |
| 113 | = &ocfs2_xattr_acl_default_handler, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 114 | [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler, |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 115 | [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | struct ocfs2_xattr_info { |
| 119 | int name_index; |
| 120 | const char *name; |
| 121 | const void *value; |
| 122 | size_t value_len; |
| 123 | }; |
| 124 | |
| 125 | struct ocfs2_xattr_search { |
| 126 | struct buffer_head *inode_bh; |
| 127 | /* |
| 128 | * xattr_bh point to the block buffer head which has extended attribute |
| 129 | * when extended attribute in inode, xattr_bh is equal to inode_bh. |
| 130 | */ |
| 131 | struct buffer_head *xattr_bh; |
| 132 | struct ocfs2_xattr_header *header; |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 133 | struct ocfs2_xattr_bucket *bucket; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 134 | void *base; |
| 135 | void *end; |
| 136 | struct ocfs2_xattr_entry *here; |
| 137 | int not_found; |
| 138 | }; |
| 139 | |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 140 | static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb, |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 141 | struct ocfs2_xattr_header *xh, |
| 142 | int index, |
| 143 | int *block_off, |
| 144 | int *new_offset); |
| 145 | |
Joel Becker | 54f443f | 2008-10-20 18:43:07 -0700 | [diff] [blame] | 146 | static int ocfs2_xattr_block_find(struct inode *inode, |
| 147 | int name_index, |
| 148 | const char *name, |
| 149 | struct ocfs2_xattr_search *xs); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 150 | static int ocfs2_xattr_index_block_find(struct inode *inode, |
| 151 | struct buffer_head *root_bh, |
| 152 | int name_index, |
| 153 | const char *name, |
| 154 | struct ocfs2_xattr_search *xs); |
| 155 | |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 156 | static int ocfs2_xattr_tree_list_index_block(struct inode *inode, |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 157 | struct buffer_head *blk_bh, |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 158 | char *buffer, |
| 159 | size_t buffer_size); |
| 160 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 161 | static int ocfs2_xattr_create_index_block(struct inode *inode, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 162 | struct ocfs2_xattr_search *xs, |
| 163 | struct ocfs2_xattr_set_ctxt *ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 164 | |
| 165 | static int ocfs2_xattr_set_entry_index_block(struct inode *inode, |
| 166 | struct ocfs2_xattr_info *xi, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 167 | struct ocfs2_xattr_search *xs, |
| 168 | struct ocfs2_xattr_set_ctxt *ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 169 | |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 170 | typedef int (xattr_tree_rec_func)(struct inode *inode, |
| 171 | struct buffer_head *root_bh, |
| 172 | u64 blkno, u32 cpos, u32 len, void *para); |
| 173 | static int ocfs2_iterate_xattr_index_block(struct inode *inode, |
| 174 | struct buffer_head *root_bh, |
| 175 | xattr_tree_rec_func *rec_func, |
| 176 | void *para); |
| 177 | static int ocfs2_delete_xattr_in_bucket(struct inode *inode, |
| 178 | struct ocfs2_xattr_bucket *bucket, |
| 179 | void *para); |
| 180 | static int ocfs2_rm_xattr_cluster(struct inode *inode, |
| 181 | struct buffer_head *root_bh, |
| 182 | u64 blkno, |
| 183 | u32 cpos, |
| 184 | u32 len, |
| 185 | void *para); |
| 186 | |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 187 | static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle, |
| 188 | u64 src_blk, u64 last_blk, u64 to_blk, |
| 189 | unsigned int start_bucket, |
| 190 | u32 *first_hash); |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 191 | static int ocfs2_prepare_refcount_xattr(struct inode *inode, |
| 192 | struct ocfs2_dinode *di, |
| 193 | struct ocfs2_xattr_info *xi, |
| 194 | struct ocfs2_xattr_search *xis, |
| 195 | struct ocfs2_xattr_search *xbs, |
| 196 | struct ocfs2_refcount_tree **ref_tree, |
| 197 | int *meta_need, |
| 198 | int *credits); |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 199 | static int ocfs2_get_xattr_tree_value_root(struct super_block *sb, |
| 200 | struct ocfs2_xattr_bucket *bucket, |
| 201 | int offset, |
| 202 | struct ocfs2_xattr_value_root **xv, |
| 203 | struct buffer_head **bh); |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 204 | |
Tiger Yang | 0030e00 | 2008-10-23 16:33:33 +0800 | [diff] [blame] | 205 | static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb) |
| 206 | { |
| 207 | return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE; |
| 208 | } |
| 209 | |
| 210 | static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb) |
| 211 | { |
| 212 | return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits); |
| 213 | } |
| 214 | |
| 215 | static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb) |
| 216 | { |
| 217 | u16 len = sb->s_blocksize - |
| 218 | offsetof(struct ocfs2_xattr_header, xh_entries); |
| 219 | |
| 220 | return len / sizeof(struct ocfs2_xattr_entry); |
| 221 | } |
| 222 | |
Joel Becker | 9c7759a | 2008-10-24 16:21:03 -0700 | [diff] [blame] | 223 | #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr) |
Joel Becker | 51def39 | 2008-10-24 16:57:21 -0700 | [diff] [blame] | 224 | #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data) |
Joel Becker | 3e63294 | 2008-10-24 17:04:49 -0700 | [diff] [blame] | 225 | #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0)) |
Joel Becker | 9c7759a | 2008-10-24 16:21:03 -0700 | [diff] [blame] | 226 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 227 | static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode) |
Joel Becker | 6dde41d | 2008-10-24 17:16:48 -0700 | [diff] [blame] | 228 | { |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 229 | struct ocfs2_xattr_bucket *bucket; |
| 230 | int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Joel Becker | 6dde41d | 2008-10-24 17:16:48 -0700 | [diff] [blame] | 231 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 232 | BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET); |
| 233 | |
| 234 | bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS); |
| 235 | if (bucket) { |
| 236 | bucket->bu_inode = inode; |
| 237 | bucket->bu_blocks = blks; |
| 238 | } |
| 239 | |
| 240 | return bucket; |
| 241 | } |
| 242 | |
| 243 | static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket) |
| 244 | { |
| 245 | int i; |
| 246 | |
| 247 | for (i = 0; i < bucket->bu_blocks; i++) { |
Joel Becker | 6dde41d | 2008-10-24 17:16:48 -0700 | [diff] [blame] | 248 | brelse(bucket->bu_bhs[i]); |
| 249 | bucket->bu_bhs[i] = NULL; |
| 250 | } |
| 251 | } |
| 252 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 253 | static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket) |
| 254 | { |
| 255 | if (bucket) { |
| 256 | ocfs2_xattr_bucket_relse(bucket); |
| 257 | bucket->bu_inode = NULL; |
| 258 | kfree(bucket); |
| 259 | } |
| 260 | } |
| 261 | |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 262 | /* |
| 263 | * A bucket that has never been written to disk doesn't need to be |
| 264 | * read. We just need the buffer_heads. Don't call this for |
| 265 | * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes |
| 266 | * them fully. |
| 267 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 268 | static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket, |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 269 | u64 xb_blkno) |
| 270 | { |
| 271 | int i, rc = 0; |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 272 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 273 | for (i = 0; i < bucket->bu_blocks; i++) { |
| 274 | bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb, |
| 275 | xb_blkno + i); |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 276 | if (!bucket->bu_bhs[i]) { |
| 277 | rc = -EIO; |
| 278 | mlog_errno(rc); |
| 279 | break; |
| 280 | } |
| 281 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 282 | if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode), |
Tao Ma | 757055a | 2008-11-06 08:10:48 +0800 | [diff] [blame] | 283 | bucket->bu_bhs[i])) |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 284 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode), |
Tao Ma | 757055a | 2008-11-06 08:10:48 +0800 | [diff] [blame] | 285 | bucket->bu_bhs[i]); |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | if (rc) |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 289 | ocfs2_xattr_bucket_relse(bucket); |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 290 | return rc; |
| 291 | } |
| 292 | |
| 293 | /* Read the xattr bucket at xb_blkno */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 294 | static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket, |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 295 | u64 xb_blkno) |
| 296 | { |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 297 | int rc; |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 298 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 299 | rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno, |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 300 | bucket->bu_blocks, bucket->bu_bhs, 0, |
| 301 | NULL); |
Joel Becker | 4d0e214 | 2008-12-05 11:19:37 -0800 | [diff] [blame] | 302 | if (!rc) { |
Tao Ma | c8b9cf9 | 2009-02-24 17:40:26 -0800 | [diff] [blame] | 303 | spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock); |
Joel Becker | 4d0e214 | 2008-12-05 11:19:37 -0800 | [diff] [blame] | 304 | rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb, |
| 305 | bucket->bu_bhs, |
| 306 | bucket->bu_blocks, |
| 307 | &bucket_xh(bucket)->xh_check); |
Tao Ma | c8b9cf9 | 2009-02-24 17:40:26 -0800 | [diff] [blame] | 308 | spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock); |
Joel Becker | 4d0e214 | 2008-12-05 11:19:37 -0800 | [diff] [blame] | 309 | if (rc) |
| 310 | mlog_errno(rc); |
| 311 | } |
| 312 | |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 313 | if (rc) |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 314 | ocfs2_xattr_bucket_relse(bucket); |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 315 | return rc; |
| 316 | } |
| 317 | |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 318 | static int ocfs2_xattr_bucket_journal_access(handle_t *handle, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 319 | struct ocfs2_xattr_bucket *bucket, |
| 320 | int type) |
| 321 | { |
| 322 | int i, rc = 0; |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 323 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 324 | for (i = 0; i < bucket->bu_blocks; i++) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 325 | rc = ocfs2_journal_access(handle, |
| 326 | INODE_CACHE(bucket->bu_inode), |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 327 | bucket->bu_bhs[i], type); |
| 328 | if (rc) { |
| 329 | mlog_errno(rc); |
| 330 | break; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | return rc; |
| 335 | } |
| 336 | |
| 337 | static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 338 | struct ocfs2_xattr_bucket *bucket) |
| 339 | { |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 340 | int i; |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 341 | |
Tao Ma | c8b9cf9 | 2009-02-24 17:40:26 -0800 | [diff] [blame] | 342 | spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock); |
Joel Becker | 4d0e214 | 2008-12-05 11:19:37 -0800 | [diff] [blame] | 343 | ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb, |
| 344 | bucket->bu_bhs, bucket->bu_blocks, |
| 345 | &bucket_xh(bucket)->xh_check); |
Tao Ma | c8b9cf9 | 2009-02-24 17:40:26 -0800 | [diff] [blame] | 346 | spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock); |
Joel Becker | 4d0e214 | 2008-12-05 11:19:37 -0800 | [diff] [blame] | 347 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 348 | for (i = 0; i < bucket->bu_blocks; i++) |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 349 | ocfs2_journal_dirty(handle, bucket->bu_bhs[i]); |
| 350 | } |
| 351 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 352 | static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest, |
Joel Becker | 4980c6d | 2008-10-24 18:54:43 -0700 | [diff] [blame] | 353 | struct ocfs2_xattr_bucket *src) |
| 354 | { |
| 355 | int i; |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 356 | int blocksize = src->bu_inode->i_sb->s_blocksize; |
Joel Becker | 4980c6d | 2008-10-24 18:54:43 -0700 | [diff] [blame] | 357 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 358 | BUG_ON(dest->bu_blocks != src->bu_blocks); |
| 359 | BUG_ON(dest->bu_inode != src->bu_inode); |
| 360 | |
| 361 | for (i = 0; i < src->bu_blocks; i++) { |
Joel Becker | 4980c6d | 2008-10-24 18:54:43 -0700 | [diff] [blame] | 362 | memcpy(bucket_block(dest, i), bucket_block(src, i), |
| 363 | blocksize); |
| 364 | } |
| 365 | } |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 366 | |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 367 | static int ocfs2_validate_xattr_block(struct super_block *sb, |
| 368 | struct buffer_head *bh) |
| 369 | { |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 370 | int rc; |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 371 | struct ocfs2_xattr_block *xb = |
| 372 | (struct ocfs2_xattr_block *)bh->b_data; |
| 373 | |
| 374 | mlog(0, "Validating xattr block %llu\n", |
| 375 | (unsigned long long)bh->b_blocknr); |
| 376 | |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 377 | BUG_ON(!buffer_uptodate(bh)); |
| 378 | |
| 379 | /* |
| 380 | * If the ecc fails, we return the error but otherwise |
| 381 | * leave the filesystem running. We know any error is |
| 382 | * local to this block. |
| 383 | */ |
| 384 | rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check); |
| 385 | if (rc) |
| 386 | return rc; |
| 387 | |
| 388 | /* |
| 389 | * Errors after here are fatal |
| 390 | */ |
| 391 | |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 392 | if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) { |
| 393 | ocfs2_error(sb, |
| 394 | "Extended attribute block #%llu has bad " |
| 395 | "signature %.*s", |
| 396 | (unsigned long long)bh->b_blocknr, 7, |
| 397 | xb->xb_signature); |
| 398 | return -EINVAL; |
| 399 | } |
| 400 | |
| 401 | if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) { |
| 402 | ocfs2_error(sb, |
| 403 | "Extended attribute block #%llu has an " |
| 404 | "invalid xb_blkno of %llu", |
| 405 | (unsigned long long)bh->b_blocknr, |
| 406 | (unsigned long long)le64_to_cpu(xb->xb_blkno)); |
| 407 | return -EINVAL; |
| 408 | } |
| 409 | |
| 410 | if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) { |
| 411 | ocfs2_error(sb, |
| 412 | "Extended attribute block #%llu has an invalid " |
| 413 | "xb_fs_generation of #%u", |
| 414 | (unsigned long long)bh->b_blocknr, |
| 415 | le32_to_cpu(xb->xb_fs_generation)); |
| 416 | return -EINVAL; |
| 417 | } |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno, |
| 423 | struct buffer_head **bh) |
| 424 | { |
| 425 | int rc; |
| 426 | struct buffer_head *tmp = *bh; |
| 427 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 428 | rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp, |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 429 | ocfs2_validate_xattr_block); |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 430 | |
| 431 | /* If ocfs2_read_block() got us a new bh, pass it up. */ |
| 432 | if (!rc && !*bh) |
| 433 | *bh = tmp; |
| 434 | |
| 435 | return rc; |
| 436 | } |
| 437 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 438 | static inline const char *ocfs2_xattr_prefix(int name_index) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 439 | { |
| 440 | struct xattr_handler *handler = NULL; |
| 441 | |
| 442 | if (name_index > 0 && name_index < OCFS2_XATTR_MAX) |
| 443 | handler = ocfs2_xattr_handler_map[name_index]; |
| 444 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 445 | return handler ? handler->prefix : NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 446 | } |
| 447 | |
Mark Fasheh | 40daa16 | 2008-10-07 14:31:42 -0700 | [diff] [blame] | 448 | static u32 ocfs2_xattr_name_hash(struct inode *inode, |
Tao Ma | 2057e5c | 2008-10-09 23:06:13 +0800 | [diff] [blame] | 449 | const char *name, |
Mark Fasheh | 40daa16 | 2008-10-07 14:31:42 -0700 | [diff] [blame] | 450 | int name_len) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 451 | { |
| 452 | /* Get hash value of uuid from super block */ |
| 453 | u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash; |
| 454 | int i; |
| 455 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 456 | /* hash extended attribute name */ |
| 457 | for (i = 0; i < name_len; i++) { |
| 458 | hash = (hash << OCFS2_HASH_SHIFT) ^ |
| 459 | (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^ |
| 460 | *name++; |
| 461 | } |
| 462 | |
| 463 | return hash; |
| 464 | } |
| 465 | |
| 466 | /* |
| 467 | * ocfs2_xattr_hash_entry() |
| 468 | * |
| 469 | * Compute the hash of an extended attribute. |
| 470 | */ |
| 471 | static void ocfs2_xattr_hash_entry(struct inode *inode, |
| 472 | struct ocfs2_xattr_header *header, |
| 473 | struct ocfs2_xattr_entry *entry) |
| 474 | { |
| 475 | u32 hash = 0; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 476 | char *name = (char *)header + le16_to_cpu(entry->xe_name_offset); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 477 | |
Tao Ma | 2057e5c | 2008-10-09 23:06:13 +0800 | [diff] [blame] | 478 | hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 479 | entry->xe_name_hash = cpu_to_le32(hash); |
| 480 | |
| 481 | return; |
| 482 | } |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 483 | |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 484 | static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len) |
| 485 | { |
| 486 | int size = 0; |
| 487 | |
| 488 | if (value_len <= OCFS2_XATTR_INLINE_SIZE) |
| 489 | size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len); |
| 490 | else |
| 491 | size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE; |
| 492 | size += sizeof(struct ocfs2_xattr_entry); |
| 493 | |
| 494 | return size; |
| 495 | } |
| 496 | |
| 497 | int ocfs2_calc_security_init(struct inode *dir, |
| 498 | struct ocfs2_security_xattr_info *si, |
| 499 | int *want_clusters, |
| 500 | int *xattr_credits, |
| 501 | struct ocfs2_alloc_context **xattr_ac) |
| 502 | { |
| 503 | int ret = 0; |
| 504 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
| 505 | int s_size = ocfs2_xattr_entry_real_size(strlen(si->name), |
| 506 | si->value_len); |
| 507 | |
| 508 | /* |
| 509 | * The max space of security xattr taken inline is |
| 510 | * 256(name) + 80(value) + 16(entry) = 352 bytes, |
| 511 | * So reserve one metadata block for it is ok. |
| 512 | */ |
| 513 | if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE || |
| 514 | s_size > OCFS2_XATTR_FREE_IN_IBODY) { |
| 515 | ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac); |
| 516 | if (ret) { |
| 517 | mlog_errno(ret); |
| 518 | return ret; |
| 519 | } |
| 520 | *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS; |
| 521 | } |
| 522 | |
| 523 | /* reserve clusters for xattr value which will be set in B tree*/ |
Tiger Yang | 0e445b6 | 2008-12-09 16:42:51 +0800 | [diff] [blame] | 524 | if (si->value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 525 | int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb, |
| 526 | si->value_len); |
| 527 | |
| 528 | *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb, |
| 529 | new_clusters); |
| 530 | *want_clusters += new_clusters; |
| 531 | } |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 532 | return ret; |
| 533 | } |
| 534 | |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 535 | int ocfs2_calc_xattr_init(struct inode *dir, |
| 536 | struct buffer_head *dir_bh, |
| 537 | int mode, |
| 538 | struct ocfs2_security_xattr_info *si, |
| 539 | int *want_clusters, |
| 540 | int *xattr_credits, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 541 | int *want_meta) |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 542 | { |
| 543 | int ret = 0; |
| 544 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
Tiger Yang | 0e445b6 | 2008-12-09 16:42:51 +0800 | [diff] [blame] | 545 | int s_size = 0, a_size = 0, acl_len = 0, new_clusters; |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 546 | |
| 547 | if (si->enable) |
| 548 | s_size = ocfs2_xattr_entry_real_size(strlen(si->name), |
| 549 | si->value_len); |
| 550 | |
| 551 | if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) { |
| 552 | acl_len = ocfs2_xattr_get_nolock(dir, dir_bh, |
| 553 | OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT, |
| 554 | "", NULL, 0); |
| 555 | if (acl_len > 0) { |
| 556 | a_size = ocfs2_xattr_entry_real_size(0, acl_len); |
| 557 | if (S_ISDIR(mode)) |
| 558 | a_size <<= 1; |
| 559 | } else if (acl_len != 0 && acl_len != -ENODATA) { |
| 560 | mlog_errno(ret); |
| 561 | return ret; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | if (!(s_size + a_size)) |
| 566 | return ret; |
| 567 | |
| 568 | /* |
| 569 | * The max space of security xattr taken inline is |
| 570 | * 256(name) + 80(value) + 16(entry) = 352 bytes, |
| 571 | * The max space of acl xattr taken inline is |
| 572 | * 80(value) + 16(entry) * 2(if directory) = 192 bytes, |
| 573 | * when blocksize = 512, may reserve one more cluser for |
| 574 | * xattr bucket, otherwise reserve one metadata block |
| 575 | * for them is ok. |
Tiger Yang | 6c9fd1d | 2009-03-06 10:19:30 +0800 | [diff] [blame] | 576 | * If this is a new directory with inline data, |
| 577 | * we choose to reserve the entire inline area for |
| 578 | * directory contents and force an external xattr block. |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 579 | */ |
| 580 | if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE || |
Tiger Yang | 6c9fd1d | 2009-03-06 10:19:30 +0800 | [diff] [blame] | 581 | (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) || |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 582 | (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) { |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 583 | *want_meta = *want_meta + 1; |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 584 | *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS; |
| 585 | } |
| 586 | |
| 587 | if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE && |
| 588 | (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) { |
| 589 | *want_clusters += 1; |
| 590 | *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb); |
| 591 | } |
| 592 | |
Tiger Yang | 0e445b6 | 2008-12-09 16:42:51 +0800 | [diff] [blame] | 593 | /* |
| 594 | * reserve credits and clusters for xattrs which has large value |
| 595 | * and have to be set outside |
| 596 | */ |
| 597 | if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 598 | new_clusters = ocfs2_clusters_for_bytes(dir->i_sb, |
| 599 | si->value_len); |
| 600 | *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb, |
| 601 | new_clusters); |
| 602 | *want_clusters += new_clusters; |
| 603 | } |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 604 | if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL && |
| 605 | acl_len > OCFS2_XATTR_INLINE_SIZE) { |
Tiger Yang | 0e445b6 | 2008-12-09 16:42:51 +0800 | [diff] [blame] | 606 | /* for directory, it has DEFAULT and ACCESS two types of acls */ |
| 607 | new_clusters = (S_ISDIR(mode) ? 2 : 1) * |
| 608 | ocfs2_clusters_for_bytes(dir->i_sb, acl_len); |
| 609 | *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb, |
| 610 | new_clusters); |
| 611 | *want_clusters += new_clusters; |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | return ret; |
| 615 | } |
| 616 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 617 | static int ocfs2_xattr_extend_allocation(struct inode *inode, |
| 618 | u32 clusters_to_add, |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 619 | struct ocfs2_xattr_value_buf *vb, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 620 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 621 | { |
| 622 | int status = 0; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 623 | handle_t *handle = ctxt->handle; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 624 | enum ocfs2_alloc_restarted why; |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 625 | u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 626 | struct ocfs2_extent_tree et; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 627 | |
| 628 | mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add); |
| 629 | |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 630 | ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 631 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 632 | status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh, |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 633 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 634 | if (status < 0) { |
| 635 | mlog_errno(status); |
| 636 | goto leave; |
| 637 | } |
| 638 | |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 639 | prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters); |
Joel Becker | cbee7e1 | 2009-02-13 03:34:15 -0800 | [diff] [blame] | 640 | status = ocfs2_add_clusters_in_btree(handle, |
| 641 | &et, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 642 | &logical_start, |
| 643 | clusters_to_add, |
| 644 | 0, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 645 | ctxt->data_ac, |
| 646 | ctxt->meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 647 | &why); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 648 | if (status < 0) { |
| 649 | mlog_errno(status); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 650 | goto leave; |
| 651 | } |
| 652 | |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 653 | status = ocfs2_journal_dirty(handle, vb->vb_bh); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 654 | if (status < 0) { |
| 655 | mlog_errno(status); |
| 656 | goto leave; |
| 657 | } |
| 658 | |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 659 | clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 660 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 661 | /* |
| 662 | * We should have already allocated enough space before the transaction, |
| 663 | * so no need to restart. |
| 664 | */ |
| 665 | BUG_ON(why != RESTART_NONE || clusters_to_add); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 666 | |
| 667 | leave: |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 668 | |
| 669 | return status; |
| 670 | } |
| 671 | |
| 672 | static int __ocfs2_remove_xattr_range(struct inode *inode, |
Joel Becker | d72cc72 | 2008-12-09 14:30:41 -0800 | [diff] [blame] | 673 | struct ocfs2_xattr_value_buf *vb, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 674 | u32 cpos, u32 phys_cpos, u32 len, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 675 | unsigned int ext_flags, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 676 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 677 | { |
| 678 | int ret; |
| 679 | u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 680 | handle_t *handle = ctxt->handle; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 681 | struct ocfs2_extent_tree et; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 682 | |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 683 | ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 684 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 685 | ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh, |
Joel Becker | d72cc72 | 2008-12-09 14:30:41 -0800 | [diff] [blame] | 686 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 687 | if (ret) { |
| 688 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 689 | goto out; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 690 | } |
| 691 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 692 | ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 693 | &ctxt->dealloc); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 694 | if (ret) { |
| 695 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 696 | goto out; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 697 | } |
| 698 | |
Joel Becker | d72cc72 | 2008-12-09 14:30:41 -0800 | [diff] [blame] | 699 | le32_add_cpu(&vb->vb_xv->xr_clusters, -len); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 700 | |
Joel Becker | d72cc72 | 2008-12-09 14:30:41 -0800 | [diff] [blame] | 701 | ret = ocfs2_journal_dirty(handle, vb->vb_bh); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 702 | if (ret) { |
| 703 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 704 | goto out; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 705 | } |
| 706 | |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 707 | if (ext_flags & OCFS2_EXT_REFCOUNTED) |
| 708 | ret = ocfs2_decrease_refcount(inode, handle, |
| 709 | ocfs2_blocks_to_clusters(inode->i_sb, |
| 710 | phys_blkno), |
| 711 | len, ctxt->meta_ac, &ctxt->dealloc, 1); |
| 712 | else |
| 713 | ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc, |
| 714 | phys_blkno, len); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 715 | if (ret) |
| 716 | mlog_errno(ret); |
| 717 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 718 | out: |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 719 | return ret; |
| 720 | } |
| 721 | |
| 722 | static int ocfs2_xattr_shrink_size(struct inode *inode, |
| 723 | u32 old_clusters, |
| 724 | u32 new_clusters, |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 725 | struct ocfs2_xattr_value_buf *vb, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 726 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 727 | { |
| 728 | int ret = 0; |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 729 | unsigned int ext_flags; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 730 | u32 trunc_len, cpos, phys_cpos, alloc_size; |
| 731 | u64 block; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 732 | |
| 733 | if (old_clusters <= new_clusters) |
| 734 | return 0; |
| 735 | |
| 736 | cpos = new_clusters; |
| 737 | trunc_len = old_clusters - new_clusters; |
| 738 | while (trunc_len) { |
| 739 | ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos, |
Joel Becker | d72cc72 | 2008-12-09 14:30:41 -0800 | [diff] [blame] | 740 | &alloc_size, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 741 | &vb->vb_xv->xr_list, &ext_flags); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 742 | if (ret) { |
| 743 | mlog_errno(ret); |
| 744 | goto out; |
| 745 | } |
| 746 | |
| 747 | if (alloc_size > trunc_len) |
| 748 | alloc_size = trunc_len; |
| 749 | |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 750 | ret = __ocfs2_remove_xattr_range(inode, vb, cpos, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 751 | phys_cpos, alloc_size, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 752 | ext_flags, ctxt); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 753 | if (ret) { |
| 754 | mlog_errno(ret); |
| 755 | goto out; |
| 756 | } |
| 757 | |
| 758 | block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 759 | ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), |
| 760 | block, alloc_size); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 761 | cpos += alloc_size; |
| 762 | trunc_len -= alloc_size; |
| 763 | } |
| 764 | |
| 765 | out: |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 766 | return ret; |
| 767 | } |
| 768 | |
| 769 | static int ocfs2_xattr_value_truncate(struct inode *inode, |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 770 | struct ocfs2_xattr_value_buf *vb, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 771 | int len, |
| 772 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 773 | { |
| 774 | int ret; |
| 775 | u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len); |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 776 | u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 777 | |
| 778 | if (new_clusters == old_clusters) |
| 779 | return 0; |
| 780 | |
| 781 | if (new_clusters > old_clusters) |
| 782 | ret = ocfs2_xattr_extend_allocation(inode, |
| 783 | new_clusters - old_clusters, |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 784 | vb, ctxt); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 785 | else |
| 786 | ret = ocfs2_xattr_shrink_size(inode, |
| 787 | old_clusters, new_clusters, |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 788 | vb, ctxt); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 789 | |
| 790 | return ret; |
| 791 | } |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 792 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 793 | static int ocfs2_xattr_list_entry(char *buffer, size_t size, |
| 794 | size_t *result, const char *prefix, |
| 795 | const char *name, int name_len) |
| 796 | { |
| 797 | char *p = buffer + *result; |
| 798 | int prefix_len = strlen(prefix); |
| 799 | int total_len = prefix_len + name_len + 1; |
| 800 | |
| 801 | *result += total_len; |
| 802 | |
| 803 | /* we are just looking for how big our buffer needs to be */ |
| 804 | if (!size) |
| 805 | return 0; |
| 806 | |
| 807 | if (*result > size) |
| 808 | return -ERANGE; |
| 809 | |
| 810 | memcpy(p, prefix, prefix_len); |
| 811 | memcpy(p + prefix_len, name, name_len); |
| 812 | p[prefix_len + name_len] = '\0'; |
| 813 | |
| 814 | return 0; |
| 815 | } |
| 816 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 817 | static int ocfs2_xattr_list_entries(struct inode *inode, |
| 818 | struct ocfs2_xattr_header *header, |
| 819 | char *buffer, size_t buffer_size) |
| 820 | { |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 821 | size_t result = 0; |
| 822 | int i, type, ret; |
| 823 | const char *prefix, *name; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 824 | |
| 825 | for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) { |
| 826 | struct ocfs2_xattr_entry *entry = &header->xh_entries[i]; |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 827 | type = ocfs2_xattr_get_type(entry); |
| 828 | prefix = ocfs2_xattr_prefix(type); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 829 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 830 | if (prefix) { |
| 831 | name = (const char *)header + |
| 832 | le16_to_cpu(entry->xe_name_offset); |
| 833 | |
| 834 | ret = ocfs2_xattr_list_entry(buffer, buffer_size, |
| 835 | &result, prefix, name, |
| 836 | entry->xe_name_len); |
| 837 | if (ret) |
| 838 | return ret; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 842 | return result; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 843 | } |
| 844 | |
Tao Ma | 8b2c0db | 2009-08-18 11:43:49 +0800 | [diff] [blame] | 845 | int ocfs2_has_inline_xattr_value_outside(struct inode *inode, |
| 846 | struct ocfs2_dinode *di) |
| 847 | { |
| 848 | struct ocfs2_xattr_header *xh; |
| 849 | int i; |
| 850 | |
| 851 | xh = (struct ocfs2_xattr_header *) |
| 852 | ((void *)di + inode->i_sb->s_blocksize - |
| 853 | le16_to_cpu(di->i_xattr_inline_size)); |
| 854 | |
| 855 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) |
| 856 | if (!ocfs2_xattr_is_local(&xh->xh_entries[i])) |
| 857 | return 1; |
| 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 862 | static int ocfs2_xattr_ibody_list(struct inode *inode, |
| 863 | struct ocfs2_dinode *di, |
| 864 | char *buffer, |
| 865 | size_t buffer_size) |
| 866 | { |
| 867 | struct ocfs2_xattr_header *header = NULL; |
| 868 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 869 | int ret = 0; |
| 870 | |
| 871 | if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) |
| 872 | return ret; |
| 873 | |
| 874 | header = (struct ocfs2_xattr_header *) |
| 875 | ((void *)di + inode->i_sb->s_blocksize - |
| 876 | le16_to_cpu(di->i_xattr_inline_size)); |
| 877 | |
| 878 | ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size); |
| 879 | |
| 880 | return ret; |
| 881 | } |
| 882 | |
| 883 | static int ocfs2_xattr_block_list(struct inode *inode, |
| 884 | struct ocfs2_dinode *di, |
| 885 | char *buffer, |
| 886 | size_t buffer_size) |
| 887 | { |
| 888 | struct buffer_head *blk_bh = NULL; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 889 | struct ocfs2_xattr_block *xb; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 890 | int ret = 0; |
| 891 | |
| 892 | if (!di->i_xattr_loc) |
| 893 | return ret; |
| 894 | |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 895 | ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc), |
| 896 | &blk_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 897 | if (ret < 0) { |
| 898 | mlog_errno(ret); |
| 899 | return ret; |
| 900 | } |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 901 | |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 902 | xb = (struct ocfs2_xattr_block *)blk_bh->b_data; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 903 | if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { |
| 904 | struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header; |
| 905 | ret = ocfs2_xattr_list_entries(inode, header, |
| 906 | buffer, buffer_size); |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 907 | } else |
| 908 | ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh, |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 909 | buffer, buffer_size); |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 910 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 911 | brelse(blk_bh); |
| 912 | |
| 913 | return ret; |
| 914 | } |
| 915 | |
| 916 | ssize_t ocfs2_listxattr(struct dentry *dentry, |
| 917 | char *buffer, |
| 918 | size_t size) |
| 919 | { |
| 920 | int ret = 0, i_ret = 0, b_ret = 0; |
| 921 | struct buffer_head *di_bh = NULL; |
| 922 | struct ocfs2_dinode *di = NULL; |
| 923 | struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode); |
| 924 | |
Tiger Yang | 8154da3 | 2008-08-18 17:11:46 +0800 | [diff] [blame] | 925 | if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb))) |
| 926 | return -EOPNOTSUPP; |
| 927 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 928 | if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) |
| 929 | return ret; |
| 930 | |
| 931 | ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0); |
| 932 | if (ret < 0) { |
| 933 | mlog_errno(ret); |
| 934 | return ret; |
| 935 | } |
| 936 | |
| 937 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 938 | |
| 939 | down_read(&oi->ip_xattr_sem); |
| 940 | i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size); |
| 941 | if (i_ret < 0) |
| 942 | b_ret = 0; |
| 943 | else { |
| 944 | if (buffer) { |
| 945 | buffer += i_ret; |
| 946 | size -= i_ret; |
| 947 | } |
| 948 | b_ret = ocfs2_xattr_block_list(dentry->d_inode, di, |
| 949 | buffer, size); |
| 950 | if (b_ret < 0) |
| 951 | i_ret = 0; |
| 952 | } |
| 953 | up_read(&oi->ip_xattr_sem); |
| 954 | ocfs2_inode_unlock(dentry->d_inode, 0); |
| 955 | |
| 956 | brelse(di_bh); |
| 957 | |
| 958 | return i_ret + b_ret; |
| 959 | } |
| 960 | |
| 961 | static int ocfs2_xattr_find_entry(int name_index, |
| 962 | const char *name, |
| 963 | struct ocfs2_xattr_search *xs) |
| 964 | { |
| 965 | struct ocfs2_xattr_entry *entry; |
| 966 | size_t name_len; |
| 967 | int i, cmp = 1; |
| 968 | |
| 969 | if (name == NULL) |
| 970 | return -EINVAL; |
| 971 | |
| 972 | name_len = strlen(name); |
| 973 | entry = xs->here; |
| 974 | for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) { |
| 975 | cmp = name_index - ocfs2_xattr_get_type(entry); |
| 976 | if (!cmp) |
| 977 | cmp = name_len - entry->xe_name_len; |
| 978 | if (!cmp) |
| 979 | cmp = memcmp(name, (xs->base + |
| 980 | le16_to_cpu(entry->xe_name_offset)), |
| 981 | name_len); |
| 982 | if (cmp == 0) |
| 983 | break; |
| 984 | entry += 1; |
| 985 | } |
| 986 | xs->here = entry; |
| 987 | |
| 988 | return cmp ? -ENODATA : 0; |
| 989 | } |
| 990 | |
| 991 | static int ocfs2_xattr_get_value_outside(struct inode *inode, |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 992 | struct ocfs2_xattr_value_root *xv, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 993 | void *buffer, |
| 994 | size_t len) |
| 995 | { |
| 996 | u32 cpos, p_cluster, num_clusters, bpc, clusters; |
| 997 | u64 blkno; |
| 998 | int i, ret = 0; |
| 999 | size_t cplen, blocksize; |
| 1000 | struct buffer_head *bh = NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1001 | struct ocfs2_extent_list *el; |
| 1002 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1003 | el = &xv->xr_list; |
| 1004 | clusters = le32_to_cpu(xv->xr_clusters); |
| 1005 | bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); |
| 1006 | blocksize = inode->i_sb->s_blocksize; |
| 1007 | |
| 1008 | cpos = 0; |
| 1009 | while (cpos < clusters) { |
| 1010 | ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster, |
Tao Ma | 1061f9c | 2009-08-18 11:41:57 +0800 | [diff] [blame] | 1011 | &num_clusters, el, NULL); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1012 | if (ret) { |
| 1013 | mlog_errno(ret); |
| 1014 | goto out; |
| 1015 | } |
| 1016 | |
| 1017 | blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster); |
| 1018 | /* Copy ocfs2_xattr_value */ |
| 1019 | for (i = 0; i < num_clusters * bpc; i++, blkno++) { |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 1020 | ret = ocfs2_read_block(INODE_CACHE(inode), blkno, |
| 1021 | &bh, NULL); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1022 | if (ret) { |
| 1023 | mlog_errno(ret); |
| 1024 | goto out; |
| 1025 | } |
| 1026 | |
| 1027 | cplen = len >= blocksize ? blocksize : len; |
| 1028 | memcpy(buffer, bh->b_data, cplen); |
| 1029 | len -= cplen; |
| 1030 | buffer += cplen; |
| 1031 | |
| 1032 | brelse(bh); |
| 1033 | bh = NULL; |
| 1034 | if (len == 0) |
| 1035 | break; |
| 1036 | } |
| 1037 | cpos += num_clusters; |
| 1038 | } |
| 1039 | out: |
| 1040 | return ret; |
| 1041 | } |
| 1042 | |
| 1043 | static int ocfs2_xattr_ibody_get(struct inode *inode, |
| 1044 | int name_index, |
| 1045 | const char *name, |
| 1046 | void *buffer, |
| 1047 | size_t buffer_size, |
| 1048 | struct ocfs2_xattr_search *xs) |
| 1049 | { |
| 1050 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 1051 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1052 | struct ocfs2_xattr_value_root *xv; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1053 | size_t size; |
| 1054 | int ret = 0; |
| 1055 | |
| 1056 | if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) |
| 1057 | return -ENODATA; |
| 1058 | |
| 1059 | xs->end = (void *)di + inode->i_sb->s_blocksize; |
| 1060 | xs->header = (struct ocfs2_xattr_header *) |
| 1061 | (xs->end - le16_to_cpu(di->i_xattr_inline_size)); |
| 1062 | xs->base = (void *)xs->header; |
| 1063 | xs->here = xs->header->xh_entries; |
| 1064 | |
| 1065 | ret = ocfs2_xattr_find_entry(name_index, name, xs); |
| 1066 | if (ret) |
| 1067 | return ret; |
| 1068 | size = le64_to_cpu(xs->here->xe_value_size); |
| 1069 | if (buffer) { |
| 1070 | if (size > buffer_size) |
| 1071 | return -ERANGE; |
| 1072 | if (ocfs2_xattr_is_local(xs->here)) { |
| 1073 | memcpy(buffer, (void *)xs->base + |
| 1074 | le16_to_cpu(xs->here->xe_name_offset) + |
| 1075 | OCFS2_XATTR_SIZE(xs->here->xe_name_len), size); |
| 1076 | } else { |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1077 | xv = (struct ocfs2_xattr_value_root *) |
| 1078 | (xs->base + le16_to_cpu( |
| 1079 | xs->here->xe_name_offset) + |
| 1080 | OCFS2_XATTR_SIZE(xs->here->xe_name_len)); |
| 1081 | ret = ocfs2_xattr_get_value_outside(inode, xv, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1082 | buffer, size); |
| 1083 | if (ret < 0) { |
| 1084 | mlog_errno(ret); |
| 1085 | return ret; |
| 1086 | } |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | return size; |
| 1091 | } |
| 1092 | |
| 1093 | static int ocfs2_xattr_block_get(struct inode *inode, |
| 1094 | int name_index, |
| 1095 | const char *name, |
| 1096 | void *buffer, |
| 1097 | size_t buffer_size, |
| 1098 | struct ocfs2_xattr_search *xs) |
| 1099 | { |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1100 | struct ocfs2_xattr_block *xb; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1101 | struct ocfs2_xattr_value_root *xv; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1102 | size_t size; |
Subrata Modak | 44d8e4e | 2009-07-14 01:19:31 +0530 | [diff] [blame] | 1103 | int ret = -ENODATA, name_offset, name_len, i; |
| 1104 | int uninitialized_var(block_off); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1105 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 1106 | xs->bucket = ocfs2_xattr_bucket_new(inode); |
| 1107 | if (!xs->bucket) { |
| 1108 | ret = -ENOMEM; |
| 1109 | mlog_errno(ret); |
| 1110 | goto cleanup; |
| 1111 | } |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1112 | |
Joel Becker | 54f443f | 2008-10-20 18:43:07 -0700 | [diff] [blame] | 1113 | ret = ocfs2_xattr_block_find(inode, name_index, name, xs); |
| 1114 | if (ret) { |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1115 | mlog_errno(ret); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1116 | goto cleanup; |
| 1117 | } |
| 1118 | |
Tiger Yang | 6c1e183 | 2008-11-02 19:04:21 +0800 | [diff] [blame] | 1119 | if (xs->not_found) { |
| 1120 | ret = -ENODATA; |
| 1121 | goto cleanup; |
| 1122 | } |
| 1123 | |
Joel Becker | 54f443f | 2008-10-20 18:43:07 -0700 | [diff] [blame] | 1124 | xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1125 | size = le64_to_cpu(xs->here->xe_value_size); |
| 1126 | if (buffer) { |
| 1127 | ret = -ERANGE; |
| 1128 | if (size > buffer_size) |
| 1129 | goto cleanup; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1130 | |
| 1131 | name_offset = le16_to_cpu(xs->here->xe_name_offset); |
| 1132 | name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len); |
| 1133 | i = xs->here - xs->header->xh_entries; |
| 1134 | |
| 1135 | if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) { |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 1136 | ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 1137 | bucket_xh(xs->bucket), |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1138 | i, |
| 1139 | &block_off, |
| 1140 | &name_offset); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 1141 | xs->base = bucket_block(xs->bucket, block_off); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1142 | } |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1143 | if (ocfs2_xattr_is_local(xs->here)) { |
| 1144 | memcpy(buffer, (void *)xs->base + |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1145 | name_offset + name_len, size); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1146 | } else { |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1147 | xv = (struct ocfs2_xattr_value_root *) |
| 1148 | (xs->base + name_offset + name_len); |
| 1149 | ret = ocfs2_xattr_get_value_outside(inode, xv, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1150 | buffer, size); |
| 1151 | if (ret < 0) { |
| 1152 | mlog_errno(ret); |
| 1153 | goto cleanup; |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | ret = size; |
| 1158 | cleanup: |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 1159 | ocfs2_xattr_bucket_free(xs->bucket); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1160 | |
Joel Becker | 54f443f | 2008-10-20 18:43:07 -0700 | [diff] [blame] | 1161 | brelse(xs->xattr_bh); |
| 1162 | xs->xattr_bh = NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1163 | return ret; |
| 1164 | } |
| 1165 | |
Tiger Yang | 4e3e9d0 | 2008-11-14 11:16:53 +0800 | [diff] [blame] | 1166 | int ocfs2_xattr_get_nolock(struct inode *inode, |
| 1167 | struct buffer_head *di_bh, |
Tiger Yang | 0030e00 | 2008-10-23 16:33:33 +0800 | [diff] [blame] | 1168 | int name_index, |
| 1169 | const char *name, |
| 1170 | void *buffer, |
| 1171 | size_t buffer_size) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1172 | { |
| 1173 | int ret; |
| 1174 | struct ocfs2_dinode *di = NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1175 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 1176 | struct ocfs2_xattr_search xis = { |
| 1177 | .not_found = -ENODATA, |
| 1178 | }; |
| 1179 | struct ocfs2_xattr_search xbs = { |
| 1180 | .not_found = -ENODATA, |
| 1181 | }; |
| 1182 | |
Tiger Yang | 8154da3 | 2008-08-18 17:11:46 +0800 | [diff] [blame] | 1183 | if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) |
| 1184 | return -EOPNOTSUPP; |
| 1185 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1186 | if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) |
| 1187 | ret = -ENODATA; |
| 1188 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1189 | xis.inode_bh = xbs.inode_bh = di_bh; |
| 1190 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 1191 | |
| 1192 | down_read(&oi->ip_xattr_sem); |
| 1193 | ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer, |
| 1194 | buffer_size, &xis); |
Tiger Yang | 6c1e183 | 2008-11-02 19:04:21 +0800 | [diff] [blame] | 1195 | if (ret == -ENODATA && di->i_xattr_loc) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1196 | ret = ocfs2_xattr_block_get(inode, name_index, name, buffer, |
| 1197 | buffer_size, &xbs); |
| 1198 | up_read(&oi->ip_xattr_sem); |
Tiger Yang | 4e3e9d0 | 2008-11-14 11:16:53 +0800 | [diff] [blame] | 1199 | |
| 1200 | return ret; |
| 1201 | } |
| 1202 | |
| 1203 | /* ocfs2_xattr_get() |
| 1204 | * |
| 1205 | * Copy an extended attribute into the buffer provided. |
| 1206 | * Buffer is NULL to compute the size of buffer required. |
| 1207 | */ |
| 1208 | static int ocfs2_xattr_get(struct inode *inode, |
| 1209 | int name_index, |
| 1210 | const char *name, |
| 1211 | void *buffer, |
| 1212 | size_t buffer_size) |
| 1213 | { |
| 1214 | int ret; |
| 1215 | struct buffer_head *di_bh = NULL; |
| 1216 | |
| 1217 | ret = ocfs2_inode_lock(inode, &di_bh, 0); |
| 1218 | if (ret < 0) { |
| 1219 | mlog_errno(ret); |
| 1220 | return ret; |
| 1221 | } |
| 1222 | ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index, |
| 1223 | name, buffer, buffer_size); |
| 1224 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1225 | ocfs2_inode_unlock(inode, 0); |
| 1226 | |
| 1227 | brelse(di_bh); |
| 1228 | |
| 1229 | return ret; |
| 1230 | } |
| 1231 | |
| 1232 | static int __ocfs2_xattr_set_value_outside(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1233 | handle_t *handle, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 1234 | struct ocfs2_xattr_value_buf *vb, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1235 | const void *value, |
| 1236 | int value_len) |
| 1237 | { |
Tao Ma | 71d548a | 2008-12-05 06:20:54 +0800 | [diff] [blame] | 1238 | int ret = 0, i, cp_len; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1239 | u16 blocksize = inode->i_sb->s_blocksize; |
| 1240 | u32 p_cluster, num_clusters; |
| 1241 | u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); |
| 1242 | u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len); |
| 1243 | u64 blkno; |
| 1244 | struct buffer_head *bh = NULL; |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 1245 | unsigned int ext_flags; |
| 1246 | struct ocfs2_xattr_value_root *xv = vb->vb_xv; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1247 | |
| 1248 | BUG_ON(clusters > le32_to_cpu(xv->xr_clusters)); |
| 1249 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1250 | while (cpos < clusters) { |
| 1251 | ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster, |
Tao Ma | 1061f9c | 2009-08-18 11:41:57 +0800 | [diff] [blame] | 1252 | &num_clusters, &xv->xr_list, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 1253 | &ext_flags); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1254 | if (ret) { |
| 1255 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1256 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1257 | } |
| 1258 | |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 1259 | BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED); |
| 1260 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1261 | blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster); |
| 1262 | |
| 1263 | for (i = 0; i < num_clusters * bpc; i++, blkno++) { |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 1264 | ret = ocfs2_read_block(INODE_CACHE(inode), blkno, |
| 1265 | &bh, NULL); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1266 | if (ret) { |
| 1267 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1268 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | ret = ocfs2_journal_access(handle, |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1272 | INODE_CACHE(inode), |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1273 | bh, |
| 1274 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1275 | if (ret < 0) { |
| 1276 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1277 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | cp_len = value_len > blocksize ? blocksize : value_len; |
| 1281 | memcpy(bh->b_data, value, cp_len); |
| 1282 | value_len -= cp_len; |
| 1283 | value += cp_len; |
| 1284 | if (cp_len < blocksize) |
| 1285 | memset(bh->b_data + cp_len, 0, |
| 1286 | blocksize - cp_len); |
| 1287 | |
| 1288 | ret = ocfs2_journal_dirty(handle, bh); |
| 1289 | if (ret < 0) { |
| 1290 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1291 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1292 | } |
| 1293 | brelse(bh); |
| 1294 | bh = NULL; |
| 1295 | |
| 1296 | /* |
| 1297 | * XXX: do we need to empty all the following |
| 1298 | * blocks in this cluster? |
| 1299 | */ |
| 1300 | if (!value_len) |
| 1301 | break; |
| 1302 | } |
| 1303 | cpos += num_clusters; |
| 1304 | } |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1305 | out: |
| 1306 | brelse(bh); |
| 1307 | |
| 1308 | return ret; |
| 1309 | } |
| 1310 | |
| 1311 | static int ocfs2_xattr_cleanup(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1312 | handle_t *handle, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1313 | struct ocfs2_xattr_info *xi, |
| 1314 | struct ocfs2_xattr_search *xs, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1315 | struct ocfs2_xattr_value_buf *vb, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1316 | size_t offs) |
| 1317 | { |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1318 | int ret = 0; |
| 1319 | size_t name_len = strlen(xi->name); |
| 1320 | void *val = xs->base + offs; |
| 1321 | size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE; |
| 1322 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1323 | ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1324 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1325 | if (ret) { |
| 1326 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1327 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1328 | } |
| 1329 | /* Decrease xattr count */ |
| 1330 | le16_add_cpu(&xs->header->xh_count, -1); |
| 1331 | /* Remove the xattr entry and tree root which has already be set*/ |
| 1332 | memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry)); |
| 1333 | memset(val, 0, size); |
| 1334 | |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1335 | ret = ocfs2_journal_dirty(handle, vb->vb_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1336 | if (ret < 0) |
| 1337 | mlog_errno(ret); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1338 | out: |
| 1339 | return ret; |
| 1340 | } |
| 1341 | |
| 1342 | static int ocfs2_xattr_update_entry(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1343 | handle_t *handle, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1344 | struct ocfs2_xattr_info *xi, |
| 1345 | struct ocfs2_xattr_search *xs, |
Joel Becker | 0c748e9 | 2008-12-09 15:46:15 -0800 | [diff] [blame] | 1346 | struct ocfs2_xattr_value_buf *vb, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1347 | size_t offs) |
| 1348 | { |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1349 | int ret; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1350 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1351 | ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh, |
Joel Becker | 0c748e9 | 2008-12-09 15:46:15 -0800 | [diff] [blame] | 1352 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1353 | if (ret) { |
| 1354 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1355 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | xs->here->xe_name_offset = cpu_to_le16(offs); |
| 1359 | xs->here->xe_value_size = cpu_to_le64(xi->value_len); |
| 1360 | if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE) |
| 1361 | ocfs2_xattr_set_local(xs->here, 1); |
| 1362 | else |
| 1363 | ocfs2_xattr_set_local(xs->here, 0); |
| 1364 | ocfs2_xattr_hash_entry(inode, xs->header, xs->here); |
| 1365 | |
Joel Becker | 0c748e9 | 2008-12-09 15:46:15 -0800 | [diff] [blame] | 1366 | ret = ocfs2_journal_dirty(handle, vb->vb_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1367 | if (ret < 0) |
| 1368 | mlog_errno(ret); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1369 | out: |
| 1370 | return ret; |
| 1371 | } |
| 1372 | |
| 1373 | /* |
| 1374 | * ocfs2_xattr_set_value_outside() |
| 1375 | * |
| 1376 | * Set large size value in B tree. |
| 1377 | */ |
| 1378 | static int ocfs2_xattr_set_value_outside(struct inode *inode, |
| 1379 | struct ocfs2_xattr_info *xi, |
| 1380 | struct ocfs2_xattr_search *xs, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 1381 | struct ocfs2_xattr_set_ctxt *ctxt, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1382 | struct ocfs2_xattr_value_buf *vb, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1383 | size_t offs) |
| 1384 | { |
| 1385 | size_t name_len = strlen(xi->name); |
| 1386 | void *val = xs->base + offs; |
| 1387 | struct ocfs2_xattr_value_root *xv = NULL; |
| 1388 | size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE; |
| 1389 | int ret = 0; |
| 1390 | |
| 1391 | memset(val, 0, size); |
| 1392 | memcpy(val, xi->name, name_len); |
| 1393 | xv = (struct ocfs2_xattr_value_root *) |
| 1394 | (val + OCFS2_XATTR_SIZE(name_len)); |
| 1395 | xv->xr_clusters = 0; |
| 1396 | xv->xr_last_eb_blk = 0; |
| 1397 | xv->xr_list.l_tree_depth = 0; |
| 1398 | xv->xr_list.l_count = cpu_to_le16(1); |
| 1399 | xv->xr_list.l_next_free_rec = 0; |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1400 | vb->vb_xv = xv; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1401 | |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1402 | ret = ocfs2_xattr_value_truncate(inode, vb, xi->value_len, ctxt); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1403 | if (ret < 0) { |
| 1404 | mlog_errno(ret); |
| 1405 | return ret; |
| 1406 | } |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1407 | ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1408 | if (ret < 0) { |
| 1409 | mlog_errno(ret); |
| 1410 | return ret; |
| 1411 | } |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 1412 | ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1413 | xi->value, xi->value_len); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1414 | if (ret < 0) |
| 1415 | mlog_errno(ret); |
| 1416 | |
| 1417 | return ret; |
| 1418 | } |
| 1419 | |
| 1420 | /* |
| 1421 | * ocfs2_xattr_set_entry_local() |
| 1422 | * |
| 1423 | * Set, replace or remove extended attribute in local. |
| 1424 | */ |
| 1425 | static void ocfs2_xattr_set_entry_local(struct inode *inode, |
| 1426 | struct ocfs2_xattr_info *xi, |
| 1427 | struct ocfs2_xattr_search *xs, |
| 1428 | struct ocfs2_xattr_entry *last, |
| 1429 | size_t min_offs) |
| 1430 | { |
| 1431 | size_t name_len = strlen(xi->name); |
| 1432 | int i; |
| 1433 | |
| 1434 | if (xi->value && xs->not_found) { |
| 1435 | /* Insert the new xattr entry. */ |
| 1436 | le16_add_cpu(&xs->header->xh_count, 1); |
| 1437 | ocfs2_xattr_set_type(last, xi->name_index); |
| 1438 | ocfs2_xattr_set_local(last, 1); |
| 1439 | last->xe_name_len = name_len; |
| 1440 | } else { |
| 1441 | void *first_val; |
| 1442 | void *val; |
| 1443 | size_t offs, size; |
| 1444 | |
| 1445 | first_val = xs->base + min_offs; |
| 1446 | offs = le16_to_cpu(xs->here->xe_name_offset); |
| 1447 | val = xs->base + offs; |
| 1448 | |
| 1449 | if (le64_to_cpu(xs->here->xe_value_size) > |
| 1450 | OCFS2_XATTR_INLINE_SIZE) |
| 1451 | size = OCFS2_XATTR_SIZE(name_len) + |
| 1452 | OCFS2_XATTR_ROOT_SIZE; |
| 1453 | else |
| 1454 | size = OCFS2_XATTR_SIZE(name_len) + |
| 1455 | OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size)); |
| 1456 | |
| 1457 | if (xi->value && size == OCFS2_XATTR_SIZE(name_len) + |
| 1458 | OCFS2_XATTR_SIZE(xi->value_len)) { |
| 1459 | /* The old and the new value have the |
| 1460 | same size. Just replace the value. */ |
| 1461 | ocfs2_xattr_set_local(xs->here, 1); |
| 1462 | xs->here->xe_value_size = cpu_to_le64(xi->value_len); |
| 1463 | /* Clear value bytes. */ |
| 1464 | memset(val + OCFS2_XATTR_SIZE(name_len), |
| 1465 | 0, |
| 1466 | OCFS2_XATTR_SIZE(xi->value_len)); |
| 1467 | memcpy(val + OCFS2_XATTR_SIZE(name_len), |
| 1468 | xi->value, |
| 1469 | xi->value_len); |
| 1470 | return; |
| 1471 | } |
| 1472 | /* Remove the old name+value. */ |
| 1473 | memmove(first_val + size, first_val, val - first_val); |
| 1474 | memset(first_val, 0, size); |
| 1475 | xs->here->xe_name_hash = 0; |
| 1476 | xs->here->xe_name_offset = 0; |
| 1477 | ocfs2_xattr_set_local(xs->here, 1); |
| 1478 | xs->here->xe_value_size = 0; |
| 1479 | |
| 1480 | min_offs += size; |
| 1481 | |
| 1482 | /* Adjust all value offsets. */ |
| 1483 | last = xs->header->xh_entries; |
| 1484 | for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) { |
| 1485 | size_t o = le16_to_cpu(last->xe_name_offset); |
| 1486 | |
| 1487 | if (o < offs) |
| 1488 | last->xe_name_offset = cpu_to_le16(o + size); |
| 1489 | last += 1; |
| 1490 | } |
| 1491 | |
| 1492 | if (!xi->value) { |
| 1493 | /* Remove the old entry. */ |
| 1494 | last -= 1; |
| 1495 | memmove(xs->here, xs->here + 1, |
| 1496 | (void *)last - (void *)xs->here); |
| 1497 | memset(last, 0, sizeof(struct ocfs2_xattr_entry)); |
| 1498 | le16_add_cpu(&xs->header->xh_count, -1); |
| 1499 | } |
| 1500 | } |
| 1501 | if (xi->value) { |
| 1502 | /* Insert the new name+value. */ |
| 1503 | size_t size = OCFS2_XATTR_SIZE(name_len) + |
| 1504 | OCFS2_XATTR_SIZE(xi->value_len); |
| 1505 | void *val = xs->base + min_offs - size; |
| 1506 | |
| 1507 | xs->here->xe_name_offset = cpu_to_le16(min_offs - size); |
| 1508 | memset(val, 0, size); |
| 1509 | memcpy(val, xi->name, name_len); |
| 1510 | memcpy(val + OCFS2_XATTR_SIZE(name_len), |
| 1511 | xi->value, |
| 1512 | xi->value_len); |
| 1513 | xs->here->xe_value_size = cpu_to_le64(xi->value_len); |
| 1514 | ocfs2_xattr_set_local(xs->here, 1); |
| 1515 | ocfs2_xattr_hash_entry(inode, xs->header, xs->here); |
| 1516 | } |
| 1517 | |
| 1518 | return; |
| 1519 | } |
| 1520 | |
| 1521 | /* |
| 1522 | * ocfs2_xattr_set_entry() |
| 1523 | * |
| 1524 | * Set extended attribute entry into inode or block. |
| 1525 | * |
| 1526 | * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE, |
| 1527 | * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(), |
| 1528 | * then set value in B tree with set_value_outside(). |
| 1529 | */ |
| 1530 | static int ocfs2_xattr_set_entry(struct inode *inode, |
| 1531 | struct ocfs2_xattr_info *xi, |
| 1532 | struct ocfs2_xattr_search *xs, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 1533 | struct ocfs2_xattr_set_ctxt *ctxt, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1534 | int flag) |
| 1535 | { |
| 1536 | struct ocfs2_xattr_entry *last; |
| 1537 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 1538 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; |
| 1539 | size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name); |
| 1540 | size_t size_l = 0; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1541 | handle_t *handle = ctxt->handle; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1542 | int free, i, ret; |
| 1543 | struct ocfs2_xattr_info xi_l = { |
| 1544 | .name_index = xi->name_index, |
| 1545 | .name = xi->name, |
| 1546 | .value = xi->value, |
| 1547 | .value_len = xi->value_len, |
| 1548 | }; |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1549 | struct ocfs2_xattr_value_buf vb = { |
| 1550 | .vb_bh = xs->xattr_bh, |
| 1551 | .vb_access = ocfs2_journal_access_di, |
| 1552 | }; |
| 1553 | |
| 1554 | if (!(flag & OCFS2_INLINE_XATTR_FL)) { |
| 1555 | BUG_ON(xs->xattr_bh == xs->inode_bh); |
| 1556 | vb.vb_access = ocfs2_journal_access_xb; |
| 1557 | } else |
| 1558 | BUG_ON(xs->xattr_bh != xs->inode_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1559 | |
| 1560 | /* Compute min_offs, last and free space. */ |
| 1561 | last = xs->header->xh_entries; |
| 1562 | |
| 1563 | for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) { |
| 1564 | size_t offs = le16_to_cpu(last->xe_name_offset); |
| 1565 | if (offs < min_offs) |
| 1566 | min_offs = offs; |
| 1567 | last += 1; |
| 1568 | } |
| 1569 | |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 1570 | free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1571 | if (free < 0) |
Joel Becker | b37c4d8 | 2008-10-20 18:24:03 -0700 | [diff] [blame] | 1572 | return -EIO; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1573 | |
| 1574 | if (!xs->not_found) { |
| 1575 | size_t size = 0; |
| 1576 | if (ocfs2_xattr_is_local(xs->here)) |
| 1577 | size = OCFS2_XATTR_SIZE(name_len) + |
| 1578 | OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size)); |
| 1579 | else |
| 1580 | size = OCFS2_XATTR_SIZE(name_len) + |
| 1581 | OCFS2_XATTR_ROOT_SIZE; |
| 1582 | free += (size + sizeof(struct ocfs2_xattr_entry)); |
| 1583 | } |
| 1584 | /* Check free space in inode or block */ |
| 1585 | if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 1586 | if (free < sizeof(struct ocfs2_xattr_entry) + |
| 1587 | OCFS2_XATTR_SIZE(name_len) + |
| 1588 | OCFS2_XATTR_ROOT_SIZE) { |
| 1589 | ret = -ENOSPC; |
| 1590 | goto out; |
| 1591 | } |
| 1592 | size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE; |
| 1593 | xi_l.value = (void *)&def_xv; |
| 1594 | xi_l.value_len = OCFS2_XATTR_ROOT_SIZE; |
| 1595 | } else if (xi->value) { |
| 1596 | if (free < sizeof(struct ocfs2_xattr_entry) + |
| 1597 | OCFS2_XATTR_SIZE(name_len) + |
| 1598 | OCFS2_XATTR_SIZE(xi->value_len)) { |
| 1599 | ret = -ENOSPC; |
| 1600 | goto out; |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | if (!xs->not_found) { |
| 1605 | /* For existing extended attribute */ |
| 1606 | size_t size = OCFS2_XATTR_SIZE(name_len) + |
| 1607 | OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size)); |
| 1608 | size_t offs = le16_to_cpu(xs->here->xe_name_offset); |
| 1609 | void *val = xs->base + offs; |
| 1610 | |
| 1611 | if (ocfs2_xattr_is_local(xs->here) && size == size_l) { |
| 1612 | /* Replace existing local xattr with tree root */ |
| 1613 | ret = ocfs2_xattr_set_value_outside(inode, xi, xs, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1614 | ctxt, &vb, offs); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1615 | if (ret < 0) |
| 1616 | mlog_errno(ret); |
| 1617 | goto out; |
| 1618 | } else if (!ocfs2_xattr_is_local(xs->here)) { |
| 1619 | /* For existing xattr which has value outside */ |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1620 | vb.vb_xv = (struct ocfs2_xattr_value_root *) |
| 1621 | (val + OCFS2_XATTR_SIZE(name_len)); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1622 | |
| 1623 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 1624 | /* |
| 1625 | * If new value need set outside also, |
| 1626 | * first truncate old value to new value, |
| 1627 | * then set new value with set_value_outside(). |
| 1628 | */ |
| 1629 | ret = ocfs2_xattr_value_truncate(inode, |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 1630 | &vb, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 1631 | xi->value_len, |
| 1632 | ctxt); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1633 | if (ret < 0) { |
| 1634 | mlog_errno(ret); |
| 1635 | goto out; |
| 1636 | } |
| 1637 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1638 | ret = ocfs2_xattr_update_entry(inode, |
| 1639 | handle, |
| 1640 | xi, |
| 1641 | xs, |
Joel Becker | 0c748e9 | 2008-12-09 15:46:15 -0800 | [diff] [blame] | 1642 | &vb, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1643 | offs); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1644 | if (ret < 0) { |
| 1645 | mlog_errno(ret); |
| 1646 | goto out; |
| 1647 | } |
| 1648 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1649 | ret = __ocfs2_xattr_set_value_outside(inode, |
| 1650 | handle, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 1651 | &vb, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1652 | xi->value, |
| 1653 | xi->value_len); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1654 | if (ret < 0) |
| 1655 | mlog_errno(ret); |
| 1656 | goto out; |
| 1657 | } else { |
| 1658 | /* |
| 1659 | * If new value need set in local, |
| 1660 | * just trucate old value to zero. |
| 1661 | */ |
| 1662 | ret = ocfs2_xattr_value_truncate(inode, |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 1663 | &vb, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1664 | 0, |
| 1665 | ctxt); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1666 | if (ret < 0) |
| 1667 | mlog_errno(ret); |
| 1668 | } |
| 1669 | } |
| 1670 | } |
| 1671 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1672 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1673 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1674 | if (ret) { |
| 1675 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1676 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | if (!(flag & OCFS2_INLINE_XATTR_FL)) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1680 | ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1681 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1682 | if (ret) { |
| 1683 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1684 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | /* |
| 1689 | * Set value in local, include set tree root in local. |
| 1690 | * This is the first step for value size >INLINE_SIZE. |
| 1691 | */ |
| 1692 | ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs); |
| 1693 | |
| 1694 | if (!(flag & OCFS2_INLINE_XATTR_FL)) { |
| 1695 | ret = ocfs2_journal_dirty(handle, xs->xattr_bh); |
| 1696 | if (ret < 0) { |
| 1697 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1698 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) && |
| 1703 | (flag & OCFS2_INLINE_XATTR_FL)) { |
| 1704 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 1705 | unsigned int xattrsize = osb->s_xattr_inline_size; |
| 1706 | |
| 1707 | /* |
| 1708 | * Adjust extent record count or inline data size |
| 1709 | * to reserve space for extended attribute. |
| 1710 | */ |
| 1711 | if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
| 1712 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 1713 | le16_add_cpu(&idata->id_count, -xattrsize); |
| 1714 | } else if (!(ocfs2_inode_is_fast_symlink(inode))) { |
| 1715 | struct ocfs2_extent_list *el = &di->id2.i_list; |
| 1716 | le16_add_cpu(&el->l_count, -(xattrsize / |
| 1717 | sizeof(struct ocfs2_extent_rec))); |
| 1718 | } |
| 1719 | di->i_xattr_inline_size = cpu_to_le16(xattrsize); |
| 1720 | } |
| 1721 | /* Update xattr flag */ |
| 1722 | spin_lock(&oi->ip_lock); |
| 1723 | oi->ip_dyn_features |= flag; |
| 1724 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 1725 | spin_unlock(&oi->ip_lock); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1726 | |
| 1727 | ret = ocfs2_journal_dirty(handle, xs->inode_bh); |
| 1728 | if (ret < 0) |
| 1729 | mlog_errno(ret); |
| 1730 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1731 | if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 1732 | /* |
| 1733 | * Set value outside in B tree. |
| 1734 | * This is the second step for value size > INLINE_SIZE. |
| 1735 | */ |
| 1736 | size_t offs = le16_to_cpu(xs->here->xe_name_offset); |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1737 | ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt, |
| 1738 | &vb, offs); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1739 | if (ret < 0) { |
| 1740 | int ret2; |
| 1741 | |
| 1742 | mlog_errno(ret); |
| 1743 | /* |
| 1744 | * If set value outside failed, we have to clean |
| 1745 | * the junk tree root we have already set in local. |
| 1746 | */ |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1747 | ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1748 | xi, xs, &vb, offs); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1749 | if (ret2 < 0) |
| 1750 | mlog_errno(ret2); |
| 1751 | } |
| 1752 | } |
| 1753 | out: |
| 1754 | return ret; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1755 | } |
| 1756 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1757 | /* |
| 1758 | * In xattr remove, if it is stored outside and refcounted, we may have |
| 1759 | * the chance to split the refcount tree. So need the allocators. |
| 1760 | */ |
| 1761 | static int ocfs2_lock_xattr_remove_allocators(struct inode *inode, |
| 1762 | struct ocfs2_xattr_value_root *xv, |
| 1763 | struct ocfs2_caching_info *ref_ci, |
| 1764 | struct buffer_head *ref_root_bh, |
| 1765 | struct ocfs2_alloc_context **meta_ac, |
| 1766 | int *ref_credits) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1767 | { |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1768 | int ret, meta_add = 0; |
| 1769 | u32 p_cluster, num_clusters; |
| 1770 | unsigned int ext_flags; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 1771 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1772 | *ref_credits = 0; |
| 1773 | ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster, |
| 1774 | &num_clusters, |
| 1775 | &xv->xr_list, |
| 1776 | &ext_flags); |
| 1777 | if (ret) { |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1778 | mlog_errno(ret); |
| 1779 | goto out; |
| 1780 | } |
| 1781 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1782 | if (!(ext_flags & OCFS2_EXT_REFCOUNTED)) |
| 1783 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1784 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1785 | ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci, |
| 1786 | ref_root_bh, xv, |
| 1787 | &meta_add, ref_credits); |
| 1788 | if (ret) { |
| 1789 | mlog_errno(ret); |
| 1790 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1791 | } |
| 1792 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1793 | ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb), |
| 1794 | meta_add, meta_ac); |
| 1795 | if (ret) |
| 1796 | mlog_errno(ret); |
| 1797 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1798 | out: |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1799 | return ret; |
| 1800 | } |
| 1801 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1802 | static int ocfs2_remove_value_outside(struct inode*inode, |
| 1803 | struct ocfs2_xattr_value_buf *vb, |
| 1804 | struct ocfs2_xattr_header *header, |
| 1805 | struct ocfs2_caching_info *ref_ci, |
| 1806 | struct buffer_head *ref_root_bh) |
| 1807 | { |
| 1808 | int ret = 0, i, ref_credits; |
| 1809 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 1810 | struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, }; |
| 1811 | void *val; |
| 1812 | |
| 1813 | ocfs2_init_dealloc_ctxt(&ctxt.dealloc); |
| 1814 | |
| 1815 | for (i = 0; i < le16_to_cpu(header->xh_count); i++) { |
| 1816 | struct ocfs2_xattr_entry *entry = &header->xh_entries[i]; |
| 1817 | |
| 1818 | if (ocfs2_xattr_is_local(entry)) |
| 1819 | continue; |
| 1820 | |
| 1821 | val = (void *)header + |
| 1822 | le16_to_cpu(entry->xe_name_offset); |
| 1823 | vb->vb_xv = (struct ocfs2_xattr_value_root *) |
| 1824 | (val + OCFS2_XATTR_SIZE(entry->xe_name_len)); |
| 1825 | |
| 1826 | ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv, |
| 1827 | ref_ci, ref_root_bh, |
| 1828 | &ctxt.meta_ac, |
| 1829 | &ref_credits); |
| 1830 | |
| 1831 | ctxt.handle = ocfs2_start_trans(osb, ref_credits + |
| 1832 | ocfs2_remove_extent_credits(osb->sb)); |
| 1833 | if (IS_ERR(ctxt.handle)) { |
| 1834 | ret = PTR_ERR(ctxt.handle); |
| 1835 | mlog_errno(ret); |
| 1836 | break; |
| 1837 | } |
| 1838 | |
| 1839 | ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt); |
| 1840 | if (ret < 0) { |
| 1841 | mlog_errno(ret); |
| 1842 | break; |
| 1843 | } |
| 1844 | |
| 1845 | ocfs2_commit_trans(osb, ctxt.handle); |
| 1846 | if (ctxt.meta_ac) { |
| 1847 | ocfs2_free_alloc_context(ctxt.meta_ac); |
| 1848 | ctxt.meta_ac = NULL; |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | if (ctxt.meta_ac) |
| 1853 | ocfs2_free_alloc_context(ctxt.meta_ac); |
| 1854 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 1855 | ocfs2_run_deallocs(osb, &ctxt.dealloc); |
| 1856 | return ret; |
| 1857 | } |
| 1858 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1859 | static int ocfs2_xattr_ibody_remove(struct inode *inode, |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1860 | struct buffer_head *di_bh, |
| 1861 | struct ocfs2_caching_info *ref_ci, |
| 1862 | struct buffer_head *ref_root_bh) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1863 | { |
| 1864 | |
| 1865 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 1866 | struct ocfs2_xattr_header *header; |
| 1867 | int ret; |
Joel Becker | 4311901 | 2008-12-09 16:24:43 -0800 | [diff] [blame] | 1868 | struct ocfs2_xattr_value_buf vb = { |
| 1869 | .vb_bh = di_bh, |
| 1870 | .vb_access = ocfs2_journal_access_di, |
| 1871 | }; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1872 | |
| 1873 | header = (struct ocfs2_xattr_header *) |
| 1874 | ((void *)di + inode->i_sb->s_blocksize - |
| 1875 | le16_to_cpu(di->i_xattr_inline_size)); |
| 1876 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1877 | ret = ocfs2_remove_value_outside(inode, &vb, header, |
| 1878 | ref_ci, ref_root_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1879 | |
| 1880 | return ret; |
| 1881 | } |
| 1882 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1883 | struct ocfs2_rm_xattr_bucket_para { |
| 1884 | struct ocfs2_caching_info *ref_ci; |
| 1885 | struct buffer_head *ref_root_bh; |
| 1886 | }; |
| 1887 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1888 | static int ocfs2_xattr_block_remove(struct inode *inode, |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1889 | struct buffer_head *blk_bh, |
| 1890 | struct ocfs2_caching_info *ref_ci, |
| 1891 | struct buffer_head *ref_root_bh) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1892 | { |
| 1893 | struct ocfs2_xattr_block *xb; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1894 | int ret = 0; |
Joel Becker | 4311901 | 2008-12-09 16:24:43 -0800 | [diff] [blame] | 1895 | struct ocfs2_xattr_value_buf vb = { |
| 1896 | .vb_bh = blk_bh, |
| 1897 | .vb_access = ocfs2_journal_access_xb, |
| 1898 | }; |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1899 | struct ocfs2_rm_xattr_bucket_para args = { |
| 1900 | .ref_ci = ref_ci, |
| 1901 | .ref_root_bh = ref_root_bh, |
| 1902 | }; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1903 | |
| 1904 | xb = (struct ocfs2_xattr_block *)blk_bh->b_data; |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 1905 | if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { |
| 1906 | struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header); |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1907 | ret = ocfs2_remove_value_outside(inode, &vb, header, |
| 1908 | ref_ci, ref_root_bh); |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 1909 | } else |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 1910 | ret = ocfs2_iterate_xattr_index_block(inode, |
| 1911 | blk_bh, |
| 1912 | ocfs2_rm_xattr_cluster, |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1913 | &args); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1914 | |
| 1915 | return ret; |
| 1916 | } |
| 1917 | |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 1918 | static int ocfs2_xattr_free_block(struct inode *inode, |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1919 | u64 block, |
| 1920 | struct ocfs2_caching_info *ref_ci, |
| 1921 | struct buffer_head *ref_root_bh) |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 1922 | { |
| 1923 | struct inode *xb_alloc_inode; |
| 1924 | struct buffer_head *xb_alloc_bh = NULL; |
| 1925 | struct buffer_head *blk_bh = NULL; |
| 1926 | struct ocfs2_xattr_block *xb; |
| 1927 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 1928 | handle_t *handle; |
| 1929 | int ret = 0; |
| 1930 | u64 blk, bg_blkno; |
| 1931 | u16 bit; |
| 1932 | |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 1933 | ret = ocfs2_read_xattr_block(inode, block, &blk_bh); |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 1934 | if (ret < 0) { |
| 1935 | mlog_errno(ret); |
| 1936 | goto out; |
| 1937 | } |
| 1938 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1939 | ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh); |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 1940 | if (ret < 0) { |
| 1941 | mlog_errno(ret); |
| 1942 | goto out; |
| 1943 | } |
| 1944 | |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 1945 | xb = (struct ocfs2_xattr_block *)blk_bh->b_data; |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 1946 | blk = le64_to_cpu(xb->xb_blkno); |
| 1947 | bit = le16_to_cpu(xb->xb_suballoc_bit); |
| 1948 | bg_blkno = ocfs2_which_suballoc_group(blk, bit); |
| 1949 | |
| 1950 | xb_alloc_inode = ocfs2_get_system_file_inode(osb, |
| 1951 | EXTENT_ALLOC_SYSTEM_INODE, |
| 1952 | le16_to_cpu(xb->xb_suballoc_slot)); |
| 1953 | if (!xb_alloc_inode) { |
| 1954 | ret = -ENOMEM; |
| 1955 | mlog_errno(ret); |
| 1956 | goto out; |
| 1957 | } |
| 1958 | mutex_lock(&xb_alloc_inode->i_mutex); |
| 1959 | |
| 1960 | ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1); |
| 1961 | if (ret < 0) { |
| 1962 | mlog_errno(ret); |
| 1963 | goto out_mutex; |
| 1964 | } |
| 1965 | |
| 1966 | handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE); |
| 1967 | if (IS_ERR(handle)) { |
| 1968 | ret = PTR_ERR(handle); |
| 1969 | mlog_errno(ret); |
| 1970 | goto out_unlock; |
| 1971 | } |
| 1972 | |
| 1973 | ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh, |
| 1974 | bit, bg_blkno, 1); |
| 1975 | if (ret < 0) |
| 1976 | mlog_errno(ret); |
| 1977 | |
| 1978 | ocfs2_commit_trans(osb, handle); |
| 1979 | out_unlock: |
| 1980 | ocfs2_inode_unlock(xb_alloc_inode, 1); |
| 1981 | brelse(xb_alloc_bh); |
| 1982 | out_mutex: |
| 1983 | mutex_unlock(&xb_alloc_inode->i_mutex); |
| 1984 | iput(xb_alloc_inode); |
| 1985 | out: |
| 1986 | brelse(blk_bh); |
| 1987 | return ret; |
| 1988 | } |
| 1989 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1990 | /* |
| 1991 | * ocfs2_xattr_remove() |
| 1992 | * |
| 1993 | * Free extended attribute resources associated with this inode. |
| 1994 | */ |
| 1995 | int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh) |
| 1996 | { |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1997 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 1998 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1999 | struct ocfs2_refcount_tree *ref_tree = NULL; |
| 2000 | struct buffer_head *ref_root_bh = NULL; |
| 2001 | struct ocfs2_caching_info *ref_ci = NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2002 | handle_t *handle; |
| 2003 | int ret; |
| 2004 | |
Tiger Yang | 8154da3 | 2008-08-18 17:11:46 +0800 | [diff] [blame] | 2005 | if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) |
| 2006 | return 0; |
| 2007 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2008 | if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) |
| 2009 | return 0; |
| 2010 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2011 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) { |
| 2012 | ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb), |
| 2013 | le64_to_cpu(di->i_refcount_loc), |
| 2014 | 1, &ref_tree, &ref_root_bh); |
| 2015 | if (ret) { |
| 2016 | mlog_errno(ret); |
| 2017 | goto out; |
| 2018 | } |
| 2019 | ref_ci = &ref_tree->rf_ci; |
| 2020 | |
| 2021 | } |
| 2022 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2023 | if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) { |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2024 | ret = ocfs2_xattr_ibody_remove(inode, di_bh, |
| 2025 | ref_ci, ref_root_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2026 | if (ret < 0) { |
| 2027 | mlog_errno(ret); |
| 2028 | goto out; |
| 2029 | } |
| 2030 | } |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2031 | |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 2032 | if (di->i_xattr_loc) { |
| 2033 | ret = ocfs2_xattr_free_block(inode, |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2034 | le64_to_cpu(di->i_xattr_loc), |
| 2035 | ref_ci, ref_root_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2036 | if (ret < 0) { |
| 2037 | mlog_errno(ret); |
| 2038 | goto out; |
| 2039 | } |
| 2040 | } |
| 2041 | |
| 2042 | handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), |
| 2043 | OCFS2_INODE_UPDATE_CREDITS); |
| 2044 | if (IS_ERR(handle)) { |
| 2045 | ret = PTR_ERR(handle); |
| 2046 | mlog_errno(ret); |
| 2047 | goto out; |
| 2048 | } |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2049 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 8400897 | 2008-12-09 16:11:49 -0800 | [diff] [blame] | 2050 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2051 | if (ret) { |
| 2052 | mlog_errno(ret); |
| 2053 | goto out_commit; |
| 2054 | } |
| 2055 | |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 2056 | di->i_xattr_loc = 0; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2057 | |
| 2058 | spin_lock(&oi->ip_lock); |
| 2059 | oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL); |
| 2060 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 2061 | spin_unlock(&oi->ip_lock); |
| 2062 | |
| 2063 | ret = ocfs2_journal_dirty(handle, di_bh); |
| 2064 | if (ret < 0) |
| 2065 | mlog_errno(ret); |
| 2066 | out_commit: |
| 2067 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); |
| 2068 | out: |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2069 | if (ref_tree) |
| 2070 | ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1); |
| 2071 | brelse(ref_root_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2072 | return ret; |
| 2073 | } |
| 2074 | |
| 2075 | static int ocfs2_xattr_has_space_inline(struct inode *inode, |
| 2076 | struct ocfs2_dinode *di) |
| 2077 | { |
| 2078 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 2079 | unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size; |
| 2080 | int free; |
| 2081 | |
| 2082 | if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE) |
| 2083 | return 0; |
| 2084 | |
| 2085 | if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
| 2086 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 2087 | free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size); |
| 2088 | } else if (ocfs2_inode_is_fast_symlink(inode)) { |
| 2089 | free = ocfs2_fast_symlink_chars(inode->i_sb) - |
| 2090 | le64_to_cpu(di->i_size); |
| 2091 | } else { |
| 2092 | struct ocfs2_extent_list *el = &di->id2.i_list; |
| 2093 | free = (le16_to_cpu(el->l_count) - |
| 2094 | le16_to_cpu(el->l_next_free_rec)) * |
| 2095 | sizeof(struct ocfs2_extent_rec); |
| 2096 | } |
| 2097 | if (free >= xattrsize) |
| 2098 | return 1; |
| 2099 | |
| 2100 | return 0; |
| 2101 | } |
| 2102 | |
| 2103 | /* |
| 2104 | * ocfs2_xattr_ibody_find() |
| 2105 | * |
| 2106 | * Find extended attribute in inode block and |
| 2107 | * fill search info into struct ocfs2_xattr_search. |
| 2108 | */ |
| 2109 | static int ocfs2_xattr_ibody_find(struct inode *inode, |
| 2110 | int name_index, |
| 2111 | const char *name, |
| 2112 | struct ocfs2_xattr_search *xs) |
| 2113 | { |
| 2114 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 2115 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; |
| 2116 | int ret; |
| 2117 | int has_space = 0; |
| 2118 | |
| 2119 | if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) |
| 2120 | return 0; |
| 2121 | |
| 2122 | if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) { |
| 2123 | down_read(&oi->ip_alloc_sem); |
| 2124 | has_space = ocfs2_xattr_has_space_inline(inode, di); |
| 2125 | up_read(&oi->ip_alloc_sem); |
| 2126 | if (!has_space) |
| 2127 | return 0; |
| 2128 | } |
| 2129 | |
| 2130 | xs->xattr_bh = xs->inode_bh; |
| 2131 | xs->end = (void *)di + inode->i_sb->s_blocksize; |
| 2132 | if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) |
| 2133 | xs->header = (struct ocfs2_xattr_header *) |
| 2134 | (xs->end - le16_to_cpu(di->i_xattr_inline_size)); |
| 2135 | else |
| 2136 | xs->header = (struct ocfs2_xattr_header *) |
| 2137 | (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size); |
| 2138 | xs->base = (void *)xs->header; |
| 2139 | xs->here = xs->header->xh_entries; |
| 2140 | |
| 2141 | /* Find the named attribute. */ |
| 2142 | if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) { |
| 2143 | ret = ocfs2_xattr_find_entry(name_index, name, xs); |
| 2144 | if (ret && ret != -ENODATA) |
| 2145 | return ret; |
| 2146 | xs->not_found = ret; |
| 2147 | } |
| 2148 | |
| 2149 | return 0; |
| 2150 | } |
| 2151 | |
| 2152 | /* |
| 2153 | * ocfs2_xattr_ibody_set() |
| 2154 | * |
| 2155 | * Set, replace or remove an extended attribute into inode block. |
| 2156 | * |
| 2157 | */ |
| 2158 | static int ocfs2_xattr_ibody_set(struct inode *inode, |
| 2159 | struct ocfs2_xattr_info *xi, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2160 | struct ocfs2_xattr_search *xs, |
| 2161 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2162 | { |
| 2163 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 2164 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; |
| 2165 | int ret; |
| 2166 | |
| 2167 | if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) |
| 2168 | return -ENOSPC; |
| 2169 | |
| 2170 | down_write(&oi->ip_alloc_sem); |
| 2171 | if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) { |
| 2172 | if (!ocfs2_xattr_has_space_inline(inode, di)) { |
| 2173 | ret = -ENOSPC; |
| 2174 | goto out; |
| 2175 | } |
| 2176 | } |
| 2177 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2178 | ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2179 | (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL)); |
| 2180 | out: |
| 2181 | up_write(&oi->ip_alloc_sem); |
| 2182 | |
| 2183 | return ret; |
| 2184 | } |
| 2185 | |
| 2186 | /* |
| 2187 | * ocfs2_xattr_block_find() |
| 2188 | * |
| 2189 | * Find extended attribute in external block and |
| 2190 | * fill search info into struct ocfs2_xattr_search. |
| 2191 | */ |
| 2192 | static int ocfs2_xattr_block_find(struct inode *inode, |
| 2193 | int name_index, |
| 2194 | const char *name, |
| 2195 | struct ocfs2_xattr_search *xs) |
| 2196 | { |
| 2197 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; |
| 2198 | struct buffer_head *blk_bh = NULL; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 2199 | struct ocfs2_xattr_block *xb; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2200 | int ret = 0; |
| 2201 | |
| 2202 | if (!di->i_xattr_loc) |
| 2203 | return ret; |
| 2204 | |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 2205 | ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc), |
| 2206 | &blk_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2207 | if (ret < 0) { |
| 2208 | mlog_errno(ret); |
| 2209 | return ret; |
| 2210 | } |
Joel Becker | f6087fb | 2008-10-20 18:20:43 -0700 | [diff] [blame] | 2211 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2212 | xs->xattr_bh = blk_bh; |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 2213 | xb = (struct ocfs2_xattr_block *)blk_bh->b_data; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2214 | |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 2215 | if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { |
| 2216 | xs->header = &xb->xb_attrs.xb_header; |
| 2217 | xs->base = (void *)xs->header; |
| 2218 | xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size; |
| 2219 | xs->here = xs->header->xh_entries; |
| 2220 | |
| 2221 | ret = ocfs2_xattr_find_entry(name_index, name, xs); |
| 2222 | } else |
| 2223 | ret = ocfs2_xattr_index_block_find(inode, blk_bh, |
| 2224 | name_index, |
| 2225 | name, xs); |
| 2226 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2227 | if (ret && ret != -ENODATA) { |
| 2228 | xs->xattr_bh = NULL; |
| 2229 | goto cleanup; |
| 2230 | } |
| 2231 | xs->not_found = ret; |
| 2232 | return 0; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2233 | cleanup: |
| 2234 | brelse(blk_bh); |
| 2235 | |
| 2236 | return ret; |
| 2237 | } |
| 2238 | |
Tao Ma | 5aea1f0 | 2009-08-18 11:43:24 +0800 | [diff] [blame] | 2239 | static int ocfs2_create_xattr_block(handle_t *handle, |
| 2240 | struct inode *inode, |
| 2241 | struct buffer_head *inode_bh, |
| 2242 | struct ocfs2_alloc_context *meta_ac, |
Tao Ma | a7fe7a3 | 2009-08-18 11:43:52 +0800 | [diff] [blame] | 2243 | struct buffer_head **ret_bh, |
| 2244 | int indexed) |
Tao Ma | 5aea1f0 | 2009-08-18 11:43:24 +0800 | [diff] [blame] | 2245 | { |
| 2246 | int ret; |
| 2247 | u16 suballoc_bit_start; |
| 2248 | u32 num_got; |
| 2249 | u64 first_blkno; |
| 2250 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data; |
| 2251 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 2252 | struct buffer_head *new_bh = NULL; |
| 2253 | struct ocfs2_xattr_block *xblk; |
| 2254 | |
| 2255 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh, |
| 2256 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 2257 | if (ret < 0) { |
| 2258 | mlog_errno(ret); |
| 2259 | goto end; |
| 2260 | } |
| 2261 | |
| 2262 | ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1, |
| 2263 | &suballoc_bit_start, &num_got, |
| 2264 | &first_blkno); |
| 2265 | if (ret < 0) { |
| 2266 | mlog_errno(ret); |
| 2267 | goto end; |
| 2268 | } |
| 2269 | |
| 2270 | new_bh = sb_getblk(inode->i_sb, first_blkno); |
| 2271 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh); |
| 2272 | |
| 2273 | ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), |
| 2274 | new_bh, |
| 2275 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 2276 | if (ret < 0) { |
| 2277 | mlog_errno(ret); |
| 2278 | goto end; |
| 2279 | } |
| 2280 | |
| 2281 | /* Initialize ocfs2_xattr_block */ |
| 2282 | xblk = (struct ocfs2_xattr_block *)new_bh->b_data; |
| 2283 | memset(xblk, 0, inode->i_sb->s_blocksize); |
| 2284 | strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE); |
| 2285 | xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num); |
| 2286 | xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start); |
| 2287 | xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation); |
| 2288 | xblk->xb_blkno = cpu_to_le64(first_blkno); |
| 2289 | |
Tao Ma | a7fe7a3 | 2009-08-18 11:43:52 +0800 | [diff] [blame] | 2290 | if (indexed) { |
| 2291 | struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root; |
| 2292 | xr->xt_clusters = cpu_to_le32(1); |
| 2293 | xr->xt_last_eb_blk = 0; |
| 2294 | xr->xt_list.l_tree_depth = 0; |
| 2295 | xr->xt_list.l_count = cpu_to_le16( |
| 2296 | ocfs2_xattr_recs_per_xb(inode->i_sb)); |
| 2297 | xr->xt_list.l_next_free_rec = cpu_to_le16(1); |
| 2298 | xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED); |
| 2299 | } |
| 2300 | |
Tao Ma | 5aea1f0 | 2009-08-18 11:43:24 +0800 | [diff] [blame] | 2301 | ret = ocfs2_journal_dirty(handle, new_bh); |
| 2302 | if (ret < 0) { |
| 2303 | mlog_errno(ret); |
| 2304 | goto end; |
| 2305 | } |
| 2306 | di->i_xattr_loc = cpu_to_le64(first_blkno); |
| 2307 | ocfs2_journal_dirty(handle, inode_bh); |
| 2308 | |
| 2309 | *ret_bh = new_bh; |
| 2310 | new_bh = NULL; |
| 2311 | |
| 2312 | end: |
| 2313 | brelse(new_bh); |
| 2314 | return ret; |
| 2315 | } |
| 2316 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2317 | /* |
| 2318 | * ocfs2_xattr_block_set() |
| 2319 | * |
| 2320 | * Set, replace or remove an extended attribute into external block. |
| 2321 | * |
| 2322 | */ |
| 2323 | static int ocfs2_xattr_block_set(struct inode *inode, |
| 2324 | struct ocfs2_xattr_info *xi, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2325 | struct ocfs2_xattr_search *xs, |
| 2326 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2327 | { |
| 2328 | struct buffer_head *new_bh = NULL; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2329 | handle_t *handle = ctxt->handle; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2330 | struct ocfs2_xattr_block *xblk = NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2331 | int ret; |
| 2332 | |
| 2333 | if (!xs->xattr_bh) { |
Tao Ma | 5aea1f0 | 2009-08-18 11:43:24 +0800 | [diff] [blame] | 2334 | ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh, |
Tao Ma | a7fe7a3 | 2009-08-18 11:43:52 +0800 | [diff] [blame] | 2335 | ctxt->meta_ac, &new_bh, 0); |
Tao Ma | 5aea1f0 | 2009-08-18 11:43:24 +0800 | [diff] [blame] | 2336 | if (ret) { |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2337 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2338 | goto end; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2339 | } |
| 2340 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2341 | xs->xattr_bh = new_bh; |
Tao Ma | 5aea1f0 | 2009-08-18 11:43:24 +0800 | [diff] [blame] | 2342 | xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2343 | xs->header = &xblk->xb_attrs.xb_header; |
| 2344 | xs->base = (void *)xs->header; |
| 2345 | xs->end = (void *)xblk + inode->i_sb->s_blocksize; |
| 2346 | xs->here = xs->header->xh_entries; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 2347 | } else |
| 2348 | xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; |
| 2349 | |
| 2350 | if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) { |
| 2351 | /* Set extended attribute into external block */ |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2352 | ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt, |
| 2353 | OCFS2_HAS_XATTR_FL); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 2354 | if (!ret || ret != -ENOSPC) |
| 2355 | goto end; |
| 2356 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2357 | ret = ocfs2_xattr_create_index_block(inode, xs, ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 2358 | if (ret) |
| 2359 | goto end; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2360 | } |
| 2361 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2362 | ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 2363 | |
| 2364 | end: |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2365 | |
| 2366 | return ret; |
| 2367 | } |
| 2368 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2369 | /* Check whether the new xattr can be inserted into the inode. */ |
| 2370 | static int ocfs2_xattr_can_be_in_inode(struct inode *inode, |
| 2371 | struct ocfs2_xattr_info *xi, |
| 2372 | struct ocfs2_xattr_search *xs) |
| 2373 | { |
| 2374 | u64 value_size; |
| 2375 | struct ocfs2_xattr_entry *last; |
| 2376 | int free, i; |
| 2377 | size_t min_offs = xs->end - xs->base; |
| 2378 | |
| 2379 | if (!xs->header) |
| 2380 | return 0; |
| 2381 | |
| 2382 | last = xs->header->xh_entries; |
| 2383 | |
| 2384 | for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) { |
| 2385 | size_t offs = le16_to_cpu(last->xe_name_offset); |
| 2386 | if (offs < min_offs) |
| 2387 | min_offs = offs; |
| 2388 | last += 1; |
| 2389 | } |
| 2390 | |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 2391 | free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2392 | if (free < 0) |
| 2393 | return 0; |
| 2394 | |
| 2395 | BUG_ON(!xs->not_found); |
| 2396 | |
| 2397 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) |
| 2398 | value_size = OCFS2_XATTR_ROOT_SIZE; |
| 2399 | else |
| 2400 | value_size = OCFS2_XATTR_SIZE(xi->value_len); |
| 2401 | |
| 2402 | if (free >= sizeof(struct ocfs2_xattr_entry) + |
| 2403 | OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size) |
| 2404 | return 1; |
| 2405 | |
| 2406 | return 0; |
| 2407 | } |
| 2408 | |
| 2409 | static int ocfs2_calc_xattr_set_need(struct inode *inode, |
| 2410 | struct ocfs2_dinode *di, |
| 2411 | struct ocfs2_xattr_info *xi, |
| 2412 | struct ocfs2_xattr_search *xis, |
| 2413 | struct ocfs2_xattr_search *xbs, |
| 2414 | int *clusters_need, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2415 | int *meta_need, |
| 2416 | int *credits_need) |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2417 | { |
| 2418 | int ret = 0, old_in_xb = 0; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2419 | int clusters_add = 0, meta_add = 0, credits = 0; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2420 | struct buffer_head *bh = NULL; |
| 2421 | struct ocfs2_xattr_block *xb = NULL; |
| 2422 | struct ocfs2_xattr_entry *xe = NULL; |
| 2423 | struct ocfs2_xattr_value_root *xv = NULL; |
| 2424 | char *base = NULL; |
| 2425 | int name_offset, name_len = 0; |
| 2426 | u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, |
| 2427 | xi->value_len); |
| 2428 | u64 value_size; |
| 2429 | |
Tao Ma | 71d548a | 2008-12-05 06:20:54 +0800 | [diff] [blame] | 2430 | /* |
| 2431 | * Calculate the clusters we need to write. |
| 2432 | * No matter whether we replace an old one or add a new one, |
| 2433 | * we need this for writing. |
| 2434 | */ |
| 2435 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) |
| 2436 | credits += new_clusters * |
| 2437 | ocfs2_clusters_to_blocks(inode->i_sb, 1); |
| 2438 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2439 | if (xis->not_found && xbs->not_found) { |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2440 | credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
| 2441 | |
| 2442 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) { |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2443 | clusters_add += new_clusters; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2444 | credits += ocfs2_calc_extend_credits(inode->i_sb, |
| 2445 | &def_xv.xv.xr_list, |
| 2446 | new_clusters); |
| 2447 | } |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2448 | |
| 2449 | goto meta_guess; |
| 2450 | } |
| 2451 | |
| 2452 | if (!xis->not_found) { |
| 2453 | xe = xis->here; |
| 2454 | name_offset = le16_to_cpu(xe->xe_name_offset); |
| 2455 | name_len = OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 2456 | base = xis->base; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2457 | credits += OCFS2_INODE_UPDATE_CREDITS; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2458 | } else { |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 2459 | int i, block_off = 0; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2460 | xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data; |
| 2461 | xe = xbs->here; |
| 2462 | name_offset = le16_to_cpu(xe->xe_name_offset); |
| 2463 | name_len = OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 2464 | i = xbs->here - xbs->header->xh_entries; |
| 2465 | old_in_xb = 1; |
| 2466 | |
| 2467 | if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) { |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 2468 | ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2469 | bucket_xh(xbs->bucket), |
| 2470 | i, &block_off, |
| 2471 | &name_offset); |
| 2472 | base = bucket_block(xbs->bucket, block_off); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2473 | credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
| 2474 | } else { |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2475 | base = xbs->base; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2476 | credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS; |
| 2477 | } |
| 2478 | } |
| 2479 | |
| 2480 | /* |
| 2481 | * delete a xattr doesn't need metadata and cluster allocation. |
| 2482 | * so just calculate the credits and return. |
| 2483 | * |
| 2484 | * The credits for removing the value tree will be extended |
| 2485 | * by ocfs2_remove_extent itself. |
| 2486 | */ |
| 2487 | if (!xi->value) { |
| 2488 | if (!ocfs2_xattr_is_local(xe)) |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 2489 | credits += ocfs2_remove_extent_credits(inode->i_sb); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2490 | |
| 2491 | goto out; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2492 | } |
| 2493 | |
| 2494 | /* do cluster allocation guess first. */ |
| 2495 | value_size = le64_to_cpu(xe->xe_value_size); |
| 2496 | |
| 2497 | if (old_in_xb) { |
| 2498 | /* |
| 2499 | * In xattr set, we always try to set the xe in inode first, |
| 2500 | * so if it can be inserted into inode successfully, the old |
| 2501 | * one will be removed from the xattr block, and this xattr |
| 2502 | * will be inserted into inode as a new xattr in inode. |
| 2503 | */ |
| 2504 | if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) { |
| 2505 | clusters_add += new_clusters; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 2506 | credits += ocfs2_remove_extent_credits(inode->i_sb) + |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2507 | OCFS2_INODE_UPDATE_CREDITS; |
| 2508 | if (!ocfs2_xattr_is_local(xe)) |
| 2509 | credits += ocfs2_calc_extend_credits( |
| 2510 | inode->i_sb, |
| 2511 | &def_xv.xv.xr_list, |
| 2512 | new_clusters); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2513 | goto out; |
| 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 2518 | /* the new values will be stored outside. */ |
| 2519 | u32 old_clusters = 0; |
| 2520 | |
| 2521 | if (!ocfs2_xattr_is_local(xe)) { |
| 2522 | old_clusters = ocfs2_clusters_for_bytes(inode->i_sb, |
| 2523 | value_size); |
| 2524 | xv = (struct ocfs2_xattr_value_root *) |
| 2525 | (base + name_offset + name_len); |
Tao Ma | 97aff52 | 2008-11-19 16:48:41 +0800 | [diff] [blame] | 2526 | value_size = OCFS2_XATTR_ROOT_SIZE; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2527 | } else |
| 2528 | xv = &def_xv.xv; |
| 2529 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2530 | if (old_clusters >= new_clusters) { |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 2531 | credits += ocfs2_remove_extent_credits(inode->i_sb); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2532 | goto out; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2533 | } else { |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2534 | meta_add += ocfs2_extend_meta_needed(&xv->xr_list); |
| 2535 | clusters_add += new_clusters - old_clusters; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2536 | credits += ocfs2_calc_extend_credits(inode->i_sb, |
| 2537 | &xv->xr_list, |
| 2538 | new_clusters - |
| 2539 | old_clusters); |
Tao Ma | 97aff52 | 2008-11-19 16:48:41 +0800 | [diff] [blame] | 2540 | if (value_size >= OCFS2_XATTR_ROOT_SIZE) |
| 2541 | goto out; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2542 | } |
| 2543 | } else { |
| 2544 | /* |
| 2545 | * Now the new value will be stored inside. So if the new |
| 2546 | * value is smaller than the size of value root or the old |
| 2547 | * value, we don't need any allocation, otherwise we have |
| 2548 | * to guess metadata allocation. |
| 2549 | */ |
| 2550 | if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) || |
| 2551 | (!ocfs2_xattr_is_local(xe) && |
| 2552 | OCFS2_XATTR_ROOT_SIZE >= xi->value_len)) |
| 2553 | goto out; |
| 2554 | } |
| 2555 | |
| 2556 | meta_guess: |
| 2557 | /* calculate metadata allocation. */ |
| 2558 | if (di->i_xattr_loc) { |
| 2559 | if (!xbs->xattr_bh) { |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 2560 | ret = ocfs2_read_xattr_block(inode, |
| 2561 | le64_to_cpu(di->i_xattr_loc), |
| 2562 | &bh); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2563 | if (ret) { |
| 2564 | mlog_errno(ret); |
| 2565 | goto out; |
| 2566 | } |
| 2567 | |
| 2568 | xb = (struct ocfs2_xattr_block *)bh->b_data; |
| 2569 | } else |
| 2570 | xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data; |
| 2571 | |
Tao Ma | 90cb546 | 2008-12-05 06:20:56 +0800 | [diff] [blame] | 2572 | /* |
| 2573 | * If there is already an xattr tree, good, we can calculate |
| 2574 | * like other b-trees. Otherwise we may have the chance of |
| 2575 | * create a tree, the credit calculation is borrowed from |
| 2576 | * ocfs2_calc_extend_credits with root_el = NULL. And the |
| 2577 | * new tree will be cluster based, so no meta is needed. |
| 2578 | */ |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2579 | if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) { |
| 2580 | struct ocfs2_extent_list *el = |
| 2581 | &xb->xb_attrs.xb_root.xt_list; |
| 2582 | meta_add += ocfs2_extend_meta_needed(el); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2583 | credits += ocfs2_calc_extend_credits(inode->i_sb, |
| 2584 | el, 1); |
Tao Ma | 90cb546 | 2008-12-05 06:20:56 +0800 | [diff] [blame] | 2585 | } else |
| 2586 | credits += OCFS2_SUBALLOC_ALLOC + 1; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2587 | |
| 2588 | /* |
| 2589 | * This cluster will be used either for new bucket or for |
| 2590 | * new xattr block. |
| 2591 | * If the cluster size is the same as the bucket size, one |
| 2592 | * more is needed since we may need to extend the bucket |
| 2593 | * also. |
| 2594 | */ |
| 2595 | clusters_add += 1; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2596 | credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2597 | if (OCFS2_XATTR_BUCKET_SIZE == |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2598 | OCFS2_SB(inode->i_sb)->s_clustersize) { |
| 2599 | credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2600 | clusters_add += 1; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2601 | } |
| 2602 | } else { |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2603 | meta_add += 1; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2604 | credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS; |
| 2605 | } |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2606 | out: |
| 2607 | if (clusters_need) |
| 2608 | *clusters_need = clusters_add; |
| 2609 | if (meta_need) |
| 2610 | *meta_need = meta_add; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2611 | if (credits_need) |
| 2612 | *credits_need = credits; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2613 | brelse(bh); |
| 2614 | return ret; |
| 2615 | } |
| 2616 | |
| 2617 | static int ocfs2_init_xattr_set_ctxt(struct inode *inode, |
| 2618 | struct ocfs2_dinode *di, |
| 2619 | struct ocfs2_xattr_info *xi, |
| 2620 | struct ocfs2_xattr_search *xis, |
| 2621 | struct ocfs2_xattr_search *xbs, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2622 | struct ocfs2_xattr_set_ctxt *ctxt, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 2623 | int extra_meta, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2624 | int *credits) |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2625 | { |
| 2626 | int clusters_add, meta_add, ret; |
| 2627 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 2628 | |
| 2629 | memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt)); |
| 2630 | |
| 2631 | ocfs2_init_dealloc_ctxt(&ctxt->dealloc); |
| 2632 | |
| 2633 | ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2634 | &clusters_add, &meta_add, credits); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2635 | if (ret) { |
| 2636 | mlog_errno(ret); |
| 2637 | return ret; |
| 2638 | } |
| 2639 | |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 2640 | meta_add += extra_meta; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2641 | mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, " |
| 2642 | "credits = %d\n", xi->name, meta_add, clusters_add, *credits); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2643 | |
| 2644 | if (meta_add) { |
| 2645 | ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, |
| 2646 | &ctxt->meta_ac); |
| 2647 | if (ret) { |
| 2648 | mlog_errno(ret); |
| 2649 | goto out; |
| 2650 | } |
| 2651 | } |
| 2652 | |
| 2653 | if (clusters_add) { |
| 2654 | ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac); |
| 2655 | if (ret) |
| 2656 | mlog_errno(ret); |
| 2657 | } |
| 2658 | out: |
| 2659 | if (ret) { |
| 2660 | if (ctxt->meta_ac) { |
| 2661 | ocfs2_free_alloc_context(ctxt->meta_ac); |
| 2662 | ctxt->meta_ac = NULL; |
| 2663 | } |
| 2664 | |
| 2665 | /* |
| 2666 | * We cannot have an error and a non null ctxt->data_ac. |
| 2667 | */ |
| 2668 | } |
| 2669 | |
| 2670 | return ret; |
| 2671 | } |
| 2672 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2673 | static int __ocfs2_xattr_set_handle(struct inode *inode, |
| 2674 | struct ocfs2_dinode *di, |
| 2675 | struct ocfs2_xattr_info *xi, |
| 2676 | struct ocfs2_xattr_search *xis, |
| 2677 | struct ocfs2_xattr_search *xbs, |
| 2678 | struct ocfs2_xattr_set_ctxt *ctxt) |
| 2679 | { |
Tao Ma | 9f868f1 | 2008-11-19 16:48:42 +0800 | [diff] [blame] | 2680 | int ret = 0, credits, old_found; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2681 | |
| 2682 | if (!xi->value) { |
| 2683 | /* Remove existing extended attribute */ |
| 2684 | if (!xis->not_found) |
| 2685 | ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt); |
| 2686 | else if (!xbs->not_found) |
| 2687 | ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt); |
| 2688 | } else { |
| 2689 | /* We always try to set extended attribute into inode first*/ |
| 2690 | ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt); |
| 2691 | if (!ret && !xbs->not_found) { |
| 2692 | /* |
| 2693 | * If succeed and that extended attribute existing in |
| 2694 | * external block, then we will remove it. |
| 2695 | */ |
| 2696 | xi->value = NULL; |
| 2697 | xi->value_len = 0; |
| 2698 | |
Tao Ma | 9f868f1 | 2008-11-19 16:48:42 +0800 | [diff] [blame] | 2699 | old_found = xis->not_found; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2700 | xis->not_found = -ENODATA; |
| 2701 | ret = ocfs2_calc_xattr_set_need(inode, |
| 2702 | di, |
| 2703 | xi, |
| 2704 | xis, |
| 2705 | xbs, |
| 2706 | NULL, |
| 2707 | NULL, |
| 2708 | &credits); |
Tao Ma | 9f868f1 | 2008-11-19 16:48:42 +0800 | [diff] [blame] | 2709 | xis->not_found = old_found; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2710 | if (ret) { |
| 2711 | mlog_errno(ret); |
| 2712 | goto out; |
| 2713 | } |
| 2714 | |
| 2715 | ret = ocfs2_extend_trans(ctxt->handle, credits + |
| 2716 | ctxt->handle->h_buffer_credits); |
| 2717 | if (ret) { |
| 2718 | mlog_errno(ret); |
| 2719 | goto out; |
| 2720 | } |
| 2721 | ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt); |
| 2722 | } else if (ret == -ENOSPC) { |
| 2723 | if (di->i_xattr_loc && !xbs->xattr_bh) { |
| 2724 | ret = ocfs2_xattr_block_find(inode, |
| 2725 | xi->name_index, |
| 2726 | xi->name, xbs); |
| 2727 | if (ret) |
| 2728 | goto out; |
| 2729 | |
Tao Ma | 9f868f1 | 2008-11-19 16:48:42 +0800 | [diff] [blame] | 2730 | old_found = xis->not_found; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2731 | xis->not_found = -ENODATA; |
| 2732 | ret = ocfs2_calc_xattr_set_need(inode, |
| 2733 | di, |
| 2734 | xi, |
| 2735 | xis, |
| 2736 | xbs, |
| 2737 | NULL, |
| 2738 | NULL, |
| 2739 | &credits); |
Tao Ma | 9f868f1 | 2008-11-19 16:48:42 +0800 | [diff] [blame] | 2740 | xis->not_found = old_found; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2741 | if (ret) { |
| 2742 | mlog_errno(ret); |
| 2743 | goto out; |
| 2744 | } |
| 2745 | |
| 2746 | ret = ocfs2_extend_trans(ctxt->handle, credits + |
| 2747 | ctxt->handle->h_buffer_credits); |
| 2748 | if (ret) { |
| 2749 | mlog_errno(ret); |
| 2750 | goto out; |
| 2751 | } |
| 2752 | } |
| 2753 | /* |
| 2754 | * If no space in inode, we will set extended attribute |
| 2755 | * into external block. |
| 2756 | */ |
| 2757 | ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt); |
| 2758 | if (ret) |
| 2759 | goto out; |
| 2760 | if (!xis->not_found) { |
| 2761 | /* |
| 2762 | * If succeed and that extended attribute |
| 2763 | * existing in inode, we will remove it. |
| 2764 | */ |
| 2765 | xi->value = NULL; |
| 2766 | xi->value_len = 0; |
| 2767 | xbs->not_found = -ENODATA; |
| 2768 | ret = ocfs2_calc_xattr_set_need(inode, |
| 2769 | di, |
| 2770 | xi, |
| 2771 | xis, |
| 2772 | xbs, |
| 2773 | NULL, |
| 2774 | NULL, |
| 2775 | &credits); |
| 2776 | if (ret) { |
| 2777 | mlog_errno(ret); |
| 2778 | goto out; |
| 2779 | } |
| 2780 | |
| 2781 | ret = ocfs2_extend_trans(ctxt->handle, credits + |
| 2782 | ctxt->handle->h_buffer_credits); |
| 2783 | if (ret) { |
| 2784 | mlog_errno(ret); |
| 2785 | goto out; |
| 2786 | } |
| 2787 | ret = ocfs2_xattr_ibody_set(inode, xi, |
| 2788 | xis, ctxt); |
| 2789 | } |
| 2790 | } |
| 2791 | } |
| 2792 | |
Tao Ma | 4b3f620 | 2008-12-05 06:20:55 +0800 | [diff] [blame] | 2793 | if (!ret) { |
| 2794 | /* Update inode ctime. */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2795 | ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), |
Tao Ma | 89a907a | 2009-02-17 04:39:28 +0800 | [diff] [blame] | 2796 | xis->inode_bh, |
| 2797 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 4b3f620 | 2008-12-05 06:20:55 +0800 | [diff] [blame] | 2798 | if (ret) { |
| 2799 | mlog_errno(ret); |
| 2800 | goto out; |
| 2801 | } |
| 2802 | |
| 2803 | inode->i_ctime = CURRENT_TIME; |
| 2804 | di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 2805 | di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 2806 | ocfs2_journal_dirty(ctxt->handle, xis->inode_bh); |
| 2807 | } |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2808 | out: |
| 2809 | return ret; |
| 2810 | } |
| 2811 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2812 | /* |
Tiger Yang | 6c3faba | 2008-11-14 11:16:03 +0800 | [diff] [blame] | 2813 | * This function only called duing creating inode |
| 2814 | * for init security/acl xattrs of the new inode. |
Tiger Yang | 008aafa | 2008-12-09 16:43:08 +0800 | [diff] [blame] | 2815 | * All transanction credits have been reserved in mknod. |
Tiger Yang | 6c3faba | 2008-11-14 11:16:03 +0800 | [diff] [blame] | 2816 | */ |
| 2817 | int ocfs2_xattr_set_handle(handle_t *handle, |
| 2818 | struct inode *inode, |
| 2819 | struct buffer_head *di_bh, |
| 2820 | int name_index, |
| 2821 | const char *name, |
| 2822 | const void *value, |
| 2823 | size_t value_len, |
| 2824 | int flags, |
| 2825 | struct ocfs2_alloc_context *meta_ac, |
| 2826 | struct ocfs2_alloc_context *data_ac) |
| 2827 | { |
| 2828 | struct ocfs2_dinode *di; |
| 2829 | int ret; |
| 2830 | |
| 2831 | struct ocfs2_xattr_info xi = { |
| 2832 | .name_index = name_index, |
| 2833 | .name = name, |
| 2834 | .value = value, |
| 2835 | .value_len = value_len, |
| 2836 | }; |
| 2837 | |
| 2838 | struct ocfs2_xattr_search xis = { |
| 2839 | .not_found = -ENODATA, |
| 2840 | }; |
| 2841 | |
| 2842 | struct ocfs2_xattr_search xbs = { |
| 2843 | .not_found = -ENODATA, |
| 2844 | }; |
| 2845 | |
| 2846 | struct ocfs2_xattr_set_ctxt ctxt = { |
| 2847 | .handle = handle, |
| 2848 | .meta_ac = meta_ac, |
| 2849 | .data_ac = data_ac, |
| 2850 | }; |
| 2851 | |
| 2852 | if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) |
| 2853 | return -EOPNOTSUPP; |
| 2854 | |
Tiger Yang | 008aafa | 2008-12-09 16:43:08 +0800 | [diff] [blame] | 2855 | /* |
| 2856 | * In extreme situation, may need xattr bucket when |
| 2857 | * block size is too small. And we have already reserved |
| 2858 | * the credits for bucket in mknod. |
| 2859 | */ |
| 2860 | if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) { |
| 2861 | xbs.bucket = ocfs2_xattr_bucket_new(inode); |
| 2862 | if (!xbs.bucket) { |
| 2863 | mlog_errno(-ENOMEM); |
| 2864 | return -ENOMEM; |
| 2865 | } |
| 2866 | } |
| 2867 | |
Tiger Yang | 6c3faba | 2008-11-14 11:16:03 +0800 | [diff] [blame] | 2868 | xis.inode_bh = xbs.inode_bh = di_bh; |
| 2869 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 2870 | |
| 2871 | down_write(&OCFS2_I(inode)->ip_xattr_sem); |
| 2872 | |
| 2873 | ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis); |
| 2874 | if (ret) |
| 2875 | goto cleanup; |
| 2876 | if (xis.not_found) { |
| 2877 | ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs); |
| 2878 | if (ret) |
| 2879 | goto cleanup; |
| 2880 | } |
| 2881 | |
| 2882 | ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt); |
| 2883 | |
| 2884 | cleanup: |
| 2885 | up_write(&OCFS2_I(inode)->ip_xattr_sem); |
| 2886 | brelse(xbs.xattr_bh); |
Tiger Yang | 008aafa | 2008-12-09 16:43:08 +0800 | [diff] [blame] | 2887 | ocfs2_xattr_bucket_free(xbs.bucket); |
Tiger Yang | 6c3faba | 2008-11-14 11:16:03 +0800 | [diff] [blame] | 2888 | |
| 2889 | return ret; |
| 2890 | } |
| 2891 | |
| 2892 | /* |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2893 | * ocfs2_xattr_set() |
| 2894 | * |
| 2895 | * Set, replace or remove an extended attribute for this inode. |
| 2896 | * value is NULL to remove an existing extended attribute, else either |
| 2897 | * create or replace an extended attribute. |
| 2898 | */ |
| 2899 | int ocfs2_xattr_set(struct inode *inode, |
| 2900 | int name_index, |
| 2901 | const char *name, |
| 2902 | const void *value, |
| 2903 | size_t value_len, |
| 2904 | int flags) |
| 2905 | { |
| 2906 | struct buffer_head *di_bh = NULL; |
| 2907 | struct ocfs2_dinode *di; |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 2908 | int ret, credits, ref_meta = 0, ref_credits = 0; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2909 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2910 | struct inode *tl_inode = osb->osb_tl_inode; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2911 | struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, }; |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 2912 | struct ocfs2_refcount_tree *ref_tree = NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2913 | |
| 2914 | struct ocfs2_xattr_info xi = { |
| 2915 | .name_index = name_index, |
| 2916 | .name = name, |
| 2917 | .value = value, |
| 2918 | .value_len = value_len, |
| 2919 | }; |
| 2920 | |
| 2921 | struct ocfs2_xattr_search xis = { |
| 2922 | .not_found = -ENODATA, |
| 2923 | }; |
| 2924 | |
| 2925 | struct ocfs2_xattr_search xbs = { |
| 2926 | .not_found = -ENODATA, |
| 2927 | }; |
| 2928 | |
Tiger Yang | 8154da3 | 2008-08-18 17:11:46 +0800 | [diff] [blame] | 2929 | if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) |
| 2930 | return -EOPNOTSUPP; |
| 2931 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 2932 | /* |
| 2933 | * Only xbs will be used on indexed trees. xis doesn't need a |
| 2934 | * bucket. |
| 2935 | */ |
| 2936 | xbs.bucket = ocfs2_xattr_bucket_new(inode); |
| 2937 | if (!xbs.bucket) { |
| 2938 | mlog_errno(-ENOMEM); |
| 2939 | return -ENOMEM; |
| 2940 | } |
| 2941 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2942 | ret = ocfs2_inode_lock(inode, &di_bh, 1); |
| 2943 | if (ret < 0) { |
| 2944 | mlog_errno(ret); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 2945 | goto cleanup_nolock; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2946 | } |
| 2947 | xis.inode_bh = xbs.inode_bh = di_bh; |
| 2948 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 2949 | |
| 2950 | down_write(&OCFS2_I(inode)->ip_xattr_sem); |
| 2951 | /* |
| 2952 | * Scan inode and external block to find the same name |
| 2953 | * extended attribute and collect search infomation. |
| 2954 | */ |
| 2955 | ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis); |
| 2956 | if (ret) |
| 2957 | goto cleanup; |
| 2958 | if (xis.not_found) { |
| 2959 | ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs); |
| 2960 | if (ret) |
| 2961 | goto cleanup; |
| 2962 | } |
| 2963 | |
| 2964 | if (xis.not_found && xbs.not_found) { |
| 2965 | ret = -ENODATA; |
| 2966 | if (flags & XATTR_REPLACE) |
| 2967 | goto cleanup; |
| 2968 | ret = 0; |
| 2969 | if (!value) |
| 2970 | goto cleanup; |
| 2971 | } else { |
| 2972 | ret = -EEXIST; |
| 2973 | if (flags & XATTR_CREATE) |
| 2974 | goto cleanup; |
| 2975 | } |
| 2976 | |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 2977 | /* Check whether the value is refcounted and do some prepartion. */ |
| 2978 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL && |
| 2979 | (!xis.not_found || !xbs.not_found)) { |
| 2980 | ret = ocfs2_prepare_refcount_xattr(inode, di, &xi, |
| 2981 | &xis, &xbs, &ref_tree, |
| 2982 | &ref_meta, &ref_credits); |
| 2983 | if (ret) { |
| 2984 | mlog_errno(ret); |
| 2985 | goto cleanup; |
| 2986 | } |
| 2987 | } |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2988 | |
| 2989 | mutex_lock(&tl_inode->i_mutex); |
| 2990 | |
| 2991 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 2992 | ret = __ocfs2_flush_truncate_log(osb); |
| 2993 | if (ret < 0) { |
| 2994 | mutex_unlock(&tl_inode->i_mutex); |
| 2995 | mlog_errno(ret); |
| 2996 | goto cleanup; |
| 2997 | } |
| 2998 | } |
| 2999 | mutex_unlock(&tl_inode->i_mutex); |
| 3000 | |
| 3001 | ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 3002 | &xbs, &ctxt, ref_meta, &credits); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 3003 | if (ret) { |
| 3004 | mlog_errno(ret); |
| 3005 | goto cleanup; |
| 3006 | } |
| 3007 | |
Tao Ma | 4b3f620 | 2008-12-05 06:20:55 +0800 | [diff] [blame] | 3008 | /* we need to update inode's ctime field, so add credit for it. */ |
| 3009 | credits += OCFS2_INODE_UPDATE_CREDITS; |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 3010 | ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3011 | if (IS_ERR(ctxt.handle)) { |
| 3012 | ret = PTR_ERR(ctxt.handle); |
| 3013 | mlog_errno(ret); |
| 3014 | goto cleanup; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 3015 | } |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3016 | |
| 3017 | ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt); |
| 3018 | |
| 3019 | ocfs2_commit_trans(osb, ctxt.handle); |
| 3020 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 3021 | if (ctxt.data_ac) |
| 3022 | ocfs2_free_alloc_context(ctxt.data_ac); |
| 3023 | if (ctxt.meta_ac) |
| 3024 | ocfs2_free_alloc_context(ctxt.meta_ac); |
| 3025 | if (ocfs2_dealloc_has_cluster(&ctxt.dealloc)) |
| 3026 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 3027 | ocfs2_run_deallocs(osb, &ctxt.dealloc); |
Tao Ma | 8b2c0db | 2009-08-18 11:43:49 +0800 | [diff] [blame] | 3028 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 3029 | cleanup: |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 3030 | if (ref_tree) |
| 3031 | ocfs2_unlock_refcount_tree(osb, ref_tree, 1); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 3032 | up_write(&OCFS2_I(inode)->ip_xattr_sem); |
Tao Ma | 8b2c0db | 2009-08-18 11:43:49 +0800 | [diff] [blame] | 3033 | if (!value && !ret) { |
| 3034 | ret = ocfs2_try_remove_refcount_tree(inode, di_bh); |
| 3035 | if (ret) |
| 3036 | mlog_errno(ret); |
| 3037 | } |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 3038 | ocfs2_inode_unlock(inode, 1); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3039 | cleanup_nolock: |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 3040 | brelse(di_bh); |
| 3041 | brelse(xbs.xattr_bh); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3042 | ocfs2_xattr_bucket_free(xbs.bucket); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 3043 | |
| 3044 | return ret; |
| 3045 | } |
| 3046 | |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3047 | /* |
| 3048 | * Find the xattr extent rec which may contains name_hash. |
| 3049 | * e_cpos will be the first name hash of the xattr rec. |
| 3050 | * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list. |
| 3051 | */ |
| 3052 | static int ocfs2_xattr_get_rec(struct inode *inode, |
| 3053 | u32 name_hash, |
| 3054 | u64 *p_blkno, |
| 3055 | u32 *e_cpos, |
| 3056 | u32 *num_clusters, |
| 3057 | struct ocfs2_extent_list *el) |
| 3058 | { |
| 3059 | int ret = 0, i; |
| 3060 | struct buffer_head *eb_bh = NULL; |
| 3061 | struct ocfs2_extent_block *eb; |
| 3062 | struct ocfs2_extent_rec *rec = NULL; |
| 3063 | u64 e_blkno = 0; |
| 3064 | |
| 3065 | if (el->l_tree_depth) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 3066 | ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash, |
| 3067 | &eb_bh); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3068 | if (ret) { |
| 3069 | mlog_errno(ret); |
| 3070 | goto out; |
| 3071 | } |
| 3072 | |
| 3073 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 3074 | el = &eb->h_list; |
| 3075 | |
| 3076 | if (el->l_tree_depth) { |
| 3077 | ocfs2_error(inode->i_sb, |
| 3078 | "Inode %lu has non zero tree depth in " |
| 3079 | "xattr tree block %llu\n", inode->i_ino, |
| 3080 | (unsigned long long)eb_bh->b_blocknr); |
| 3081 | ret = -EROFS; |
| 3082 | goto out; |
| 3083 | } |
| 3084 | } |
| 3085 | |
| 3086 | for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) { |
| 3087 | rec = &el->l_recs[i]; |
| 3088 | |
| 3089 | if (le32_to_cpu(rec->e_cpos) <= name_hash) { |
| 3090 | e_blkno = le64_to_cpu(rec->e_blkno); |
| 3091 | break; |
| 3092 | } |
| 3093 | } |
| 3094 | |
| 3095 | if (!e_blkno) { |
| 3096 | ocfs2_error(inode->i_sb, "Inode %lu has bad extent " |
| 3097 | "record (%u, %u, 0) in xattr", inode->i_ino, |
| 3098 | le32_to_cpu(rec->e_cpos), |
| 3099 | ocfs2_rec_clusters(el, rec)); |
| 3100 | ret = -EROFS; |
| 3101 | goto out; |
| 3102 | } |
| 3103 | |
| 3104 | *p_blkno = le64_to_cpu(rec->e_blkno); |
| 3105 | *num_clusters = le16_to_cpu(rec->e_leaf_clusters); |
| 3106 | if (e_cpos) |
| 3107 | *e_cpos = le32_to_cpu(rec->e_cpos); |
| 3108 | out: |
| 3109 | brelse(eb_bh); |
| 3110 | return ret; |
| 3111 | } |
| 3112 | |
| 3113 | typedef int (xattr_bucket_func)(struct inode *inode, |
| 3114 | struct ocfs2_xattr_bucket *bucket, |
| 3115 | void *para); |
| 3116 | |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3117 | static int ocfs2_find_xe_in_bucket(struct inode *inode, |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3118 | struct ocfs2_xattr_bucket *bucket, |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3119 | int name_index, |
| 3120 | const char *name, |
| 3121 | u32 name_hash, |
| 3122 | u16 *xe_index, |
| 3123 | int *found) |
| 3124 | { |
| 3125 | int i, ret = 0, cmp = 1, block_off, new_offset; |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3126 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3127 | size_t name_len = strlen(name); |
| 3128 | struct ocfs2_xattr_entry *xe = NULL; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3129 | char *xe_name; |
| 3130 | |
| 3131 | /* |
| 3132 | * We don't use binary search in the bucket because there |
| 3133 | * may be multiple entries with the same name hash. |
| 3134 | */ |
| 3135 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { |
| 3136 | xe = &xh->xh_entries[i]; |
| 3137 | |
| 3138 | if (name_hash > le32_to_cpu(xe->xe_name_hash)) |
| 3139 | continue; |
| 3140 | else if (name_hash < le32_to_cpu(xe->xe_name_hash)) |
| 3141 | break; |
| 3142 | |
| 3143 | cmp = name_index - ocfs2_xattr_get_type(xe); |
| 3144 | if (!cmp) |
| 3145 | cmp = name_len - xe->xe_name_len; |
| 3146 | if (cmp) |
| 3147 | continue; |
| 3148 | |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 3149 | ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3150 | xh, |
| 3151 | i, |
| 3152 | &block_off, |
| 3153 | &new_offset); |
| 3154 | if (ret) { |
| 3155 | mlog_errno(ret); |
| 3156 | break; |
| 3157 | } |
| 3158 | |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 3159 | |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3160 | xe_name = bucket_block(bucket, block_off) + new_offset; |
| 3161 | if (!memcmp(name, xe_name, name_len)) { |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3162 | *xe_index = i; |
| 3163 | *found = 1; |
| 3164 | ret = 0; |
| 3165 | break; |
| 3166 | } |
| 3167 | } |
| 3168 | |
| 3169 | return ret; |
| 3170 | } |
| 3171 | |
| 3172 | /* |
| 3173 | * Find the specified xattr entry in a series of buckets. |
| 3174 | * This series start from p_blkno and last for num_clusters. |
| 3175 | * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains |
| 3176 | * the num of the valid buckets. |
| 3177 | * |
| 3178 | * Return the buffer_head this xattr should reside in. And if the xattr's |
| 3179 | * hash is in the gap of 2 buckets, return the lower bucket. |
| 3180 | */ |
| 3181 | static int ocfs2_xattr_bucket_find(struct inode *inode, |
| 3182 | int name_index, |
| 3183 | const char *name, |
| 3184 | u32 name_hash, |
| 3185 | u64 p_blkno, |
| 3186 | u32 first_hash, |
| 3187 | u32 num_clusters, |
| 3188 | struct ocfs2_xattr_search *xs) |
| 3189 | { |
| 3190 | int ret, found = 0; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3191 | struct ocfs2_xattr_header *xh = NULL; |
| 3192 | struct ocfs2_xattr_entry *xe = NULL; |
| 3193 | u16 index = 0; |
| 3194 | u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
| 3195 | int low_bucket = 0, bucket, high_bucket; |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3196 | struct ocfs2_xattr_bucket *search; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3197 | u32 last_hash; |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3198 | u64 blkno, lower_blkno = 0; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3199 | |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3200 | search = ocfs2_xattr_bucket_new(inode); |
| 3201 | if (!search) { |
| 3202 | ret = -ENOMEM; |
| 3203 | mlog_errno(ret); |
| 3204 | goto out; |
| 3205 | } |
| 3206 | |
| 3207 | ret = ocfs2_read_xattr_bucket(search, p_blkno); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3208 | if (ret) { |
| 3209 | mlog_errno(ret); |
| 3210 | goto out; |
| 3211 | } |
| 3212 | |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3213 | xh = bucket_xh(search); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3214 | high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3215 | while (low_bucket <= high_bucket) { |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3216 | ocfs2_xattr_bucket_relse(search); |
| 3217 | |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3218 | bucket = (low_bucket + high_bucket) / 2; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3219 | blkno = p_blkno + bucket * blk_per_bucket; |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3220 | ret = ocfs2_read_xattr_bucket(search, blkno); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3221 | if (ret) { |
| 3222 | mlog_errno(ret); |
| 3223 | goto out; |
| 3224 | } |
| 3225 | |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3226 | xh = bucket_xh(search); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3227 | xe = &xh->xh_entries[0]; |
| 3228 | if (name_hash < le32_to_cpu(xe->xe_name_hash)) { |
| 3229 | high_bucket = bucket - 1; |
| 3230 | continue; |
| 3231 | } |
| 3232 | |
| 3233 | /* |
| 3234 | * Check whether the hash of the last entry in our |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 3235 | * bucket is larger than the search one. for an empty |
| 3236 | * bucket, the last one is also the first one. |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3237 | */ |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 3238 | if (xh->xh_count) |
| 3239 | xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1]; |
| 3240 | |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3241 | last_hash = le32_to_cpu(xe->xe_name_hash); |
| 3242 | |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3243 | /* record lower_blkno which may be the insert place. */ |
| 3244 | lower_blkno = blkno; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3245 | |
| 3246 | if (name_hash > le32_to_cpu(xe->xe_name_hash)) { |
| 3247 | low_bucket = bucket + 1; |
| 3248 | continue; |
| 3249 | } |
| 3250 | |
| 3251 | /* the searched xattr should reside in this bucket if exists. */ |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3252 | ret = ocfs2_find_xe_in_bucket(inode, search, |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3253 | name_index, name, name_hash, |
| 3254 | &index, &found); |
| 3255 | if (ret) { |
| 3256 | mlog_errno(ret); |
| 3257 | goto out; |
| 3258 | } |
| 3259 | break; |
| 3260 | } |
| 3261 | |
| 3262 | /* |
| 3263 | * Record the bucket we have found. |
| 3264 | * When the xattr's hash value is in the gap of 2 buckets, we will |
| 3265 | * always set it to the previous bucket. |
| 3266 | */ |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3267 | if (!lower_blkno) |
| 3268 | lower_blkno = p_blkno; |
| 3269 | |
| 3270 | /* This should be in cache - we just read it during the search */ |
| 3271 | ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno); |
| 3272 | if (ret) { |
| 3273 | mlog_errno(ret); |
| 3274 | goto out; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3275 | } |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3276 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3277 | xs->header = bucket_xh(xs->bucket); |
| 3278 | xs->base = bucket_block(xs->bucket, 0); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3279 | xs->end = xs->base + inode->i_sb->s_blocksize; |
| 3280 | |
| 3281 | if (found) { |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3282 | xs->here = &xs->header->xh_entries[index]; |
| 3283 | mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3284 | (unsigned long long)bucket_blkno(xs->bucket), index); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3285 | } else |
| 3286 | ret = -ENODATA; |
| 3287 | |
| 3288 | out: |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3289 | ocfs2_xattr_bucket_free(search); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3290 | return ret; |
| 3291 | } |
| 3292 | |
| 3293 | static int ocfs2_xattr_index_block_find(struct inode *inode, |
| 3294 | struct buffer_head *root_bh, |
| 3295 | int name_index, |
| 3296 | const char *name, |
| 3297 | struct ocfs2_xattr_search *xs) |
| 3298 | { |
| 3299 | int ret; |
| 3300 | struct ocfs2_xattr_block *xb = |
| 3301 | (struct ocfs2_xattr_block *)root_bh->b_data; |
| 3302 | struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root; |
| 3303 | struct ocfs2_extent_list *el = &xb_root->xt_list; |
| 3304 | u64 p_blkno = 0; |
| 3305 | u32 first_hash, num_clusters = 0; |
Tao Ma | 2057e5c | 2008-10-09 23:06:13 +0800 | [diff] [blame] | 3306 | u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name)); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3307 | |
| 3308 | if (le16_to_cpu(el->l_next_free_rec) == 0) |
| 3309 | return -ENODATA; |
| 3310 | |
| 3311 | mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n", |
| 3312 | name, name_hash, name_index); |
| 3313 | |
| 3314 | ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash, |
| 3315 | &num_clusters, el); |
| 3316 | if (ret) { |
| 3317 | mlog_errno(ret); |
| 3318 | goto out; |
| 3319 | } |
| 3320 | |
| 3321 | BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash); |
| 3322 | |
| 3323 | mlog(0, "find xattr extent rec %u clusters from %llu, the first hash " |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 3324 | "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno, |
| 3325 | first_hash); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3326 | |
| 3327 | ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash, |
| 3328 | p_blkno, first_hash, num_clusters, xs); |
| 3329 | |
| 3330 | out: |
| 3331 | return ret; |
| 3332 | } |
| 3333 | |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3334 | static int ocfs2_iterate_xattr_buckets(struct inode *inode, |
| 3335 | u64 blkno, |
| 3336 | u32 clusters, |
| 3337 | xattr_bucket_func *func, |
| 3338 | void *para) |
| 3339 | { |
Joel Becker | 6dde41d | 2008-10-24 17:16:48 -0700 | [diff] [blame] | 3340 | int i, ret = 0; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3341 | u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)); |
| 3342 | u32 num_buckets = clusters * bpc; |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3343 | struct ocfs2_xattr_bucket *bucket; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3344 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3345 | bucket = ocfs2_xattr_bucket_new(inode); |
| 3346 | if (!bucket) { |
| 3347 | mlog_errno(-ENOMEM); |
| 3348 | return -ENOMEM; |
| 3349 | } |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3350 | |
| 3351 | mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n", |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 3352 | clusters, (unsigned long long)blkno); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3353 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3354 | for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) { |
| 3355 | ret = ocfs2_read_xattr_bucket(bucket, blkno); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3356 | if (ret) { |
| 3357 | mlog_errno(ret); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3358 | break; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3359 | } |
| 3360 | |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3361 | /* |
| 3362 | * The real bucket num in this series of blocks is stored |
| 3363 | * in the 1st bucket. |
| 3364 | */ |
| 3365 | if (i == 0) |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3366 | num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3367 | |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 3368 | mlog(0, "iterating xattr bucket %llu, first hash %u\n", |
| 3369 | (unsigned long long)blkno, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3370 | le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash)); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3371 | if (func) { |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3372 | ret = func(inode, bucket, para); |
Tao Ma | a46fa68 | 2009-05-04 05:18:09 +0800 | [diff] [blame] | 3373 | if (ret && ret != -ERANGE) |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3374 | mlog_errno(ret); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3375 | /* Fall through to bucket_relse() */ |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3376 | } |
| 3377 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3378 | ocfs2_xattr_bucket_relse(bucket); |
| 3379 | if (ret) |
| 3380 | break; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3381 | } |
| 3382 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3383 | ocfs2_xattr_bucket_free(bucket); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3384 | return ret; |
| 3385 | } |
| 3386 | |
| 3387 | struct ocfs2_xattr_tree_list { |
| 3388 | char *buffer; |
| 3389 | size_t buffer_size; |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3390 | size_t result; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3391 | }; |
| 3392 | |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 3393 | static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb, |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3394 | struct ocfs2_xattr_header *xh, |
| 3395 | int index, |
| 3396 | int *block_off, |
| 3397 | int *new_offset) |
| 3398 | { |
| 3399 | u16 name_offset; |
| 3400 | |
| 3401 | if (index < 0 || index >= le16_to_cpu(xh->xh_count)) |
| 3402 | return -EINVAL; |
| 3403 | |
| 3404 | name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset); |
| 3405 | |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 3406 | *block_off = name_offset >> sb->s_blocksize_bits; |
| 3407 | *new_offset = name_offset % sb->s_blocksize; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3408 | |
| 3409 | return 0; |
| 3410 | } |
| 3411 | |
| 3412 | static int ocfs2_list_xattr_bucket(struct inode *inode, |
| 3413 | struct ocfs2_xattr_bucket *bucket, |
| 3414 | void *para) |
| 3415 | { |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3416 | int ret = 0, type; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3417 | struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3418 | int i, block_off, new_offset; |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3419 | const char *prefix, *name; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3420 | |
Joel Becker | 3e63294 | 2008-10-24 17:04:49 -0700 | [diff] [blame] | 3421 | for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) { |
| 3422 | struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i]; |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3423 | type = ocfs2_xattr_get_type(entry); |
| 3424 | prefix = ocfs2_xattr_prefix(type); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3425 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3426 | if (prefix) { |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 3427 | ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, |
Joel Becker | 3e63294 | 2008-10-24 17:04:49 -0700 | [diff] [blame] | 3428 | bucket_xh(bucket), |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3429 | i, |
| 3430 | &block_off, |
| 3431 | &new_offset); |
| 3432 | if (ret) |
| 3433 | break; |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3434 | |
Joel Becker | 51def39 | 2008-10-24 16:57:21 -0700 | [diff] [blame] | 3435 | name = (const char *)bucket_block(bucket, block_off) + |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3436 | new_offset; |
| 3437 | ret = ocfs2_xattr_list_entry(xl->buffer, |
| 3438 | xl->buffer_size, |
| 3439 | &xl->result, |
| 3440 | prefix, name, |
| 3441 | entry->xe_name_len); |
| 3442 | if (ret) |
| 3443 | break; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3444 | } |
| 3445 | } |
| 3446 | |
| 3447 | return ret; |
| 3448 | } |
| 3449 | |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 3450 | static int ocfs2_iterate_xattr_index_block(struct inode *inode, |
| 3451 | struct buffer_head *blk_bh, |
| 3452 | xattr_tree_rec_func *rec_func, |
| 3453 | void *para) |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3454 | { |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 3455 | struct ocfs2_xattr_block *xb = |
| 3456 | (struct ocfs2_xattr_block *)blk_bh->b_data; |
| 3457 | struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3458 | int ret = 0; |
| 3459 | u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0; |
| 3460 | u64 p_blkno = 0; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3461 | |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 3462 | if (!el->l_next_free_rec || !rec_func) |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3463 | return 0; |
| 3464 | |
| 3465 | while (name_hash > 0) { |
| 3466 | ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, |
| 3467 | &e_cpos, &num_clusters, el); |
| 3468 | if (ret) { |
| 3469 | mlog_errno(ret); |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 3470 | break; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3471 | } |
| 3472 | |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 3473 | ret = rec_func(inode, blk_bh, p_blkno, e_cpos, |
| 3474 | num_clusters, para); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3475 | if (ret) { |
Tao Ma | a46fa68 | 2009-05-04 05:18:09 +0800 | [diff] [blame] | 3476 | if (ret != -ERANGE) |
| 3477 | mlog_errno(ret); |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 3478 | break; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3479 | } |
| 3480 | |
| 3481 | if (e_cpos == 0) |
| 3482 | break; |
| 3483 | |
| 3484 | name_hash = e_cpos - 1; |
| 3485 | } |
| 3486 | |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 3487 | return ret; |
| 3488 | |
| 3489 | } |
| 3490 | |
| 3491 | static int ocfs2_list_xattr_tree_rec(struct inode *inode, |
| 3492 | struct buffer_head *root_bh, |
| 3493 | u64 blkno, u32 cpos, u32 len, void *para) |
| 3494 | { |
| 3495 | return ocfs2_iterate_xattr_buckets(inode, blkno, len, |
| 3496 | ocfs2_list_xattr_bucket, para); |
| 3497 | } |
| 3498 | |
| 3499 | static int ocfs2_xattr_tree_list_index_block(struct inode *inode, |
| 3500 | struct buffer_head *blk_bh, |
| 3501 | char *buffer, |
| 3502 | size_t buffer_size) |
| 3503 | { |
| 3504 | int ret; |
| 3505 | struct ocfs2_xattr_tree_list xl = { |
| 3506 | .buffer = buffer, |
| 3507 | .buffer_size = buffer_size, |
| 3508 | .result = 0, |
| 3509 | }; |
| 3510 | |
| 3511 | ret = ocfs2_iterate_xattr_index_block(inode, blk_bh, |
| 3512 | ocfs2_list_xattr_tree_rec, &xl); |
| 3513 | if (ret) { |
| 3514 | mlog_errno(ret); |
| 3515 | goto out; |
| 3516 | } |
| 3517 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3518 | ret = xl.result; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3519 | out: |
| 3520 | return ret; |
| 3521 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3522 | |
| 3523 | static int cmp_xe(const void *a, const void *b) |
| 3524 | { |
| 3525 | const struct ocfs2_xattr_entry *l = a, *r = b; |
| 3526 | u32 l_hash = le32_to_cpu(l->xe_name_hash); |
| 3527 | u32 r_hash = le32_to_cpu(r->xe_name_hash); |
| 3528 | |
| 3529 | if (l_hash > r_hash) |
| 3530 | return 1; |
| 3531 | if (l_hash < r_hash) |
| 3532 | return -1; |
| 3533 | return 0; |
| 3534 | } |
| 3535 | |
| 3536 | static void swap_xe(void *a, void *b, int size) |
| 3537 | { |
| 3538 | struct ocfs2_xattr_entry *l = a, *r = b, tmp; |
| 3539 | |
| 3540 | tmp = *l; |
| 3541 | memcpy(l, r, sizeof(struct ocfs2_xattr_entry)); |
| 3542 | memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry)); |
| 3543 | } |
| 3544 | |
| 3545 | /* |
| 3546 | * When the ocfs2_xattr_block is filled up, new bucket will be created |
| 3547 | * and all the xattr entries will be moved to the new bucket. |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3548 | * The header goes at the start of the bucket, and the names+values are |
| 3549 | * filled from the end. This is why *target starts as the last buffer. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3550 | * Note: we need to sort the entries since they are not saved in order |
| 3551 | * in the ocfs2_xattr_block. |
| 3552 | */ |
| 3553 | static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode, |
| 3554 | struct buffer_head *xb_bh, |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3555 | struct ocfs2_xattr_bucket *bucket) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3556 | { |
| 3557 | int i, blocksize = inode->i_sb->s_blocksize; |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3558 | int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3559 | u16 offset, size, off_change; |
| 3560 | struct ocfs2_xattr_entry *xe; |
| 3561 | struct ocfs2_xattr_block *xb = |
| 3562 | (struct ocfs2_xattr_block *)xb_bh->b_data; |
| 3563 | struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header; |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3564 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3565 | u16 count = le16_to_cpu(xb_xh->xh_count); |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3566 | char *src = xb_bh->b_data; |
| 3567 | char *target = bucket_block(bucket, blks - 1); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3568 | |
| 3569 | mlog(0, "cp xattr from block %llu to bucket %llu\n", |
| 3570 | (unsigned long long)xb_bh->b_blocknr, |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3571 | (unsigned long long)bucket_blkno(bucket)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3572 | |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3573 | for (i = 0; i < blks; i++) |
| 3574 | memset(bucket_block(bucket, i), 0, blocksize); |
| 3575 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3576 | /* |
| 3577 | * Since the xe_name_offset is based on ocfs2_xattr_header, |
| 3578 | * there is a offset change corresponding to the change of |
| 3579 | * ocfs2_xattr_header's position. |
| 3580 | */ |
| 3581 | off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); |
| 3582 | xe = &xb_xh->xh_entries[count - 1]; |
| 3583 | offset = le16_to_cpu(xe->xe_name_offset) + off_change; |
| 3584 | size = blocksize - offset; |
| 3585 | |
| 3586 | /* copy all the names and values. */ |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3587 | memcpy(target + offset, src + offset, size); |
| 3588 | |
| 3589 | /* Init new header now. */ |
| 3590 | xh->xh_count = xb_xh->xh_count; |
| 3591 | xh->xh_num_buckets = cpu_to_le16(1); |
| 3592 | xh->xh_name_value_len = cpu_to_le16(size); |
| 3593 | xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size); |
| 3594 | |
| 3595 | /* copy all the entries. */ |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3596 | target = bucket_block(bucket, 0); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3597 | offset = offsetof(struct ocfs2_xattr_header, xh_entries); |
| 3598 | size = count * sizeof(struct ocfs2_xattr_entry); |
| 3599 | memcpy(target + offset, (char *)xb_xh + offset, size); |
| 3600 | |
| 3601 | /* Change the xe offset for all the xe because of the move. */ |
| 3602 | off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize + |
| 3603 | offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); |
| 3604 | for (i = 0; i < count; i++) |
| 3605 | le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change); |
| 3606 | |
| 3607 | mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n", |
| 3608 | offset, size, off_change); |
| 3609 | |
| 3610 | sort(target + offset, count, sizeof(struct ocfs2_xattr_entry), |
| 3611 | cmp_xe, swap_xe); |
| 3612 | } |
| 3613 | |
| 3614 | /* |
| 3615 | * After we move xattr from block to index btree, we have to |
| 3616 | * update ocfs2_xattr_search to the new xe and base. |
| 3617 | * |
| 3618 | * When the entry is in xattr block, xattr_bh indicates the storage place. |
| 3619 | * While if the entry is in index b-tree, "bucket" indicates the |
| 3620 | * real place of the xattr. |
| 3621 | */ |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3622 | static void ocfs2_xattr_update_xattr_search(struct inode *inode, |
| 3623 | struct ocfs2_xattr_search *xs, |
| 3624 | struct buffer_head *old_bh) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3625 | { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3626 | char *buf = old_bh->b_data; |
| 3627 | struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf; |
| 3628 | struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header; |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3629 | int i; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3630 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3631 | xs->header = bucket_xh(xs->bucket); |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3632 | xs->base = bucket_block(xs->bucket, 0); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3633 | xs->end = xs->base + inode->i_sb->s_blocksize; |
| 3634 | |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3635 | if (xs->not_found) |
| 3636 | return; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3637 | |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3638 | i = xs->here - old_xh->xh_entries; |
| 3639 | xs->here = &xs->header->xh_entries[i]; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3640 | } |
| 3641 | |
| 3642 | static int ocfs2_xattr_create_index_block(struct inode *inode, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 3643 | struct ocfs2_xattr_search *xs, |
| 3644 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3645 | { |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3646 | int ret; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3647 | u32 bit_off, len; |
| 3648 | u64 blkno; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3649 | handle_t *handle = ctxt->handle; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3650 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 3651 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3652 | struct buffer_head *xb_bh = xs->xattr_bh; |
| 3653 | struct ocfs2_xattr_block *xb = |
| 3654 | (struct ocfs2_xattr_block *)xb_bh->b_data; |
| 3655 | struct ocfs2_xattr_tree_root *xr; |
| 3656 | u16 xb_flags = le16_to_cpu(xb->xb_flags); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3657 | |
| 3658 | mlog(0, "create xattr index block for %llu\n", |
| 3659 | (unsigned long long)xb_bh->b_blocknr); |
| 3660 | |
| 3661 | BUG_ON(xb_flags & OCFS2_XATTR_INDEXED); |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3662 | BUG_ON(!xs->bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3663 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3664 | /* |
| 3665 | * XXX: |
| 3666 | * We can use this lock for now, and maybe move to a dedicated mutex |
| 3667 | * if performance becomes a problem later. |
| 3668 | */ |
| 3669 | down_write(&oi->ip_alloc_sem); |
| 3670 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 3671 | ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh, |
Joel Becker | 8400897 | 2008-12-09 16:11:49 -0800 | [diff] [blame] | 3672 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3673 | if (ret) { |
| 3674 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3675 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3676 | } |
| 3677 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 3678 | ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, |
| 3679 | 1, 1, &bit_off, &len); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3680 | if (ret) { |
| 3681 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3682 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3683 | } |
| 3684 | |
| 3685 | /* |
| 3686 | * The bucket may spread in many blocks, and |
| 3687 | * we will only touch the 1st block and the last block |
| 3688 | * in the whole bucket(one for entry and one for data). |
| 3689 | */ |
| 3690 | blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off); |
| 3691 | |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 3692 | mlog(0, "allocate 1 cluster from %llu to xattr block\n", |
| 3693 | (unsigned long long)blkno); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3694 | |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3695 | ret = ocfs2_init_xattr_bucket(xs->bucket, blkno); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3696 | if (ret) { |
| 3697 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3698 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3699 | } |
| 3700 | |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3701 | ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket, |
| 3702 | OCFS2_JOURNAL_ACCESS_CREATE); |
Joel Becker | bd60bd3 | 2008-10-20 18:25:56 -0700 | [diff] [blame] | 3703 | if (ret) { |
| 3704 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3705 | goto out; |
Joel Becker | bd60bd3 | 2008-10-20 18:25:56 -0700 | [diff] [blame] | 3706 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3707 | |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3708 | ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket); |
| 3709 | ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket); |
| 3710 | |
| 3711 | ocfs2_xattr_update_xattr_search(inode, xs, xb_bh); |
| 3712 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3713 | /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */ |
| 3714 | memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize - |
| 3715 | offsetof(struct ocfs2_xattr_block, xb_attrs)); |
| 3716 | |
| 3717 | xr = &xb->xb_attrs.xb_root; |
| 3718 | xr->xt_clusters = cpu_to_le32(1); |
| 3719 | xr->xt_last_eb_blk = 0; |
| 3720 | xr->xt_list.l_tree_depth = 0; |
| 3721 | xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb)); |
| 3722 | xr->xt_list.l_next_free_rec = cpu_to_le16(1); |
| 3723 | |
| 3724 | xr->xt_list.l_recs[0].e_cpos = 0; |
| 3725 | xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno); |
| 3726 | xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1); |
| 3727 | |
| 3728 | xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED); |
| 3729 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3730 | ocfs2_journal_dirty(handle, xb_bh); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3731 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3732 | out: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3733 | up_write(&oi->ip_alloc_sem); |
| 3734 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3735 | return ret; |
| 3736 | } |
| 3737 | |
| 3738 | static int cmp_xe_offset(const void *a, const void *b) |
| 3739 | { |
| 3740 | const struct ocfs2_xattr_entry *l = a, *r = b; |
| 3741 | u32 l_name_offset = le16_to_cpu(l->xe_name_offset); |
| 3742 | u32 r_name_offset = le16_to_cpu(r->xe_name_offset); |
| 3743 | |
| 3744 | if (l_name_offset < r_name_offset) |
| 3745 | return 1; |
| 3746 | if (l_name_offset > r_name_offset) |
| 3747 | return -1; |
| 3748 | return 0; |
| 3749 | } |
| 3750 | |
| 3751 | /* |
| 3752 | * defrag a xattr bucket if we find that the bucket has some |
| 3753 | * holes beteen name/value pairs. |
| 3754 | * We will move all the name/value pairs to the end of the bucket |
| 3755 | * so that we can spare some space for insertion. |
| 3756 | */ |
| 3757 | static int ocfs2_defrag_xattr_bucket(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3758 | handle_t *handle, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3759 | struct ocfs2_xattr_bucket *bucket) |
| 3760 | { |
| 3761 | int ret, i; |
| 3762 | size_t end, offset, len, value_len; |
| 3763 | struct ocfs2_xattr_header *xh; |
| 3764 | char *entries, *buf, *bucket_buf = NULL; |
Joel Becker | 9c7759a | 2008-10-24 16:21:03 -0700 | [diff] [blame] | 3765 | u64 blkno = bucket_blkno(bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3766 | u16 xh_free_start; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3767 | size_t blocksize = inode->i_sb->s_blocksize; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3768 | struct ocfs2_xattr_entry *xe; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3769 | |
| 3770 | /* |
| 3771 | * In order to make the operation more efficient and generic, |
| 3772 | * we copy all the blocks into a contiguous memory and do the |
| 3773 | * defragment there, so if anything is error, we will not touch |
| 3774 | * the real block. |
| 3775 | */ |
| 3776 | bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS); |
| 3777 | if (!bucket_buf) { |
| 3778 | ret = -EIO; |
| 3779 | goto out; |
| 3780 | } |
| 3781 | |
Joel Becker | 161d6f3 | 2008-10-27 15:25:18 -0700 | [diff] [blame] | 3782 | buf = bucket_buf; |
Tao Ma | 1c32a2f | 2008-11-06 08:10:47 +0800 | [diff] [blame] | 3783 | for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize) |
| 3784 | memcpy(buf, bucket_block(bucket, i), blocksize); |
Joel Becker | 161d6f3 | 2008-10-27 15:25:18 -0700 | [diff] [blame] | 3785 | |
Tao Ma | 1c32a2f | 2008-11-06 08:10:47 +0800 | [diff] [blame] | 3786 | ret = ocfs2_xattr_bucket_journal_access(handle, bucket, |
Joel Becker | 161d6f3 | 2008-10-27 15:25:18 -0700 | [diff] [blame] | 3787 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 3788 | if (ret < 0) { |
| 3789 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3790 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3791 | } |
| 3792 | |
| 3793 | xh = (struct ocfs2_xattr_header *)bucket_buf; |
| 3794 | entries = (char *)xh->xh_entries; |
| 3795 | xh_free_start = le16_to_cpu(xh->xh_free_start); |
| 3796 | |
| 3797 | mlog(0, "adjust xattr bucket in %llu, count = %u, " |
| 3798 | "xh_free_start = %u, xh_name_value_len = %u.\n", |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 3799 | (unsigned long long)blkno, le16_to_cpu(xh->xh_count), |
| 3800 | xh_free_start, le16_to_cpu(xh->xh_name_value_len)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3801 | |
| 3802 | /* |
| 3803 | * sort all the entries by their offset. |
| 3804 | * the largest will be the first, so that we can |
| 3805 | * move them to the end one by one. |
| 3806 | */ |
| 3807 | sort(entries, le16_to_cpu(xh->xh_count), |
| 3808 | sizeof(struct ocfs2_xattr_entry), |
| 3809 | cmp_xe_offset, swap_xe); |
| 3810 | |
| 3811 | /* Move all name/values to the end of the bucket. */ |
| 3812 | xe = xh->xh_entries; |
| 3813 | end = OCFS2_XATTR_BUCKET_SIZE; |
| 3814 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) { |
| 3815 | offset = le16_to_cpu(xe->xe_name_offset); |
| 3816 | if (ocfs2_xattr_is_local(xe)) |
| 3817 | value_len = OCFS2_XATTR_SIZE( |
| 3818 | le64_to_cpu(xe->xe_value_size)); |
| 3819 | else |
| 3820 | value_len = OCFS2_XATTR_ROOT_SIZE; |
| 3821 | len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len; |
| 3822 | |
| 3823 | /* |
| 3824 | * We must make sure that the name/value pair |
| 3825 | * exist in the same block. So adjust end to |
| 3826 | * the previous block end if needed. |
| 3827 | */ |
| 3828 | if (((end - len) / blocksize != |
| 3829 | (end - 1) / blocksize)) |
| 3830 | end = end - end % blocksize; |
| 3831 | |
| 3832 | if (end > offset + len) { |
| 3833 | memmove(bucket_buf + end - len, |
| 3834 | bucket_buf + offset, len); |
| 3835 | xe->xe_name_offset = cpu_to_le16(end - len); |
| 3836 | } |
| 3837 | |
| 3838 | mlog_bug_on_msg(end < offset + len, "Defrag check failed for " |
| 3839 | "bucket %llu\n", (unsigned long long)blkno); |
| 3840 | |
| 3841 | end -= len; |
| 3842 | } |
| 3843 | |
| 3844 | mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for " |
| 3845 | "bucket %llu\n", (unsigned long long)blkno); |
| 3846 | |
| 3847 | if (xh_free_start == end) |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3848 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3849 | |
| 3850 | memset(bucket_buf + xh_free_start, 0, end - xh_free_start); |
| 3851 | xh->xh_free_start = cpu_to_le16(end); |
| 3852 | |
| 3853 | /* sort the entries by their name_hash. */ |
| 3854 | sort(entries, le16_to_cpu(xh->xh_count), |
| 3855 | sizeof(struct ocfs2_xattr_entry), |
| 3856 | cmp_xe, swap_xe); |
| 3857 | |
| 3858 | buf = bucket_buf; |
Tao Ma | 1c32a2f | 2008-11-06 08:10:47 +0800 | [diff] [blame] | 3859 | for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize) |
| 3860 | memcpy(bucket_block(bucket, i), buf, blocksize); |
| 3861 | ocfs2_xattr_bucket_journal_dirty(handle, bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3862 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3863 | out: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3864 | kfree(bucket_buf); |
| 3865 | return ret; |
| 3866 | } |
| 3867 | |
| 3868 | /* |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3869 | * prev_blkno points to the start of an existing extent. new_blkno |
| 3870 | * points to a newly allocated extent. Because we know each of our |
| 3871 | * clusters contains more than bucket, we can easily split one cluster |
| 3872 | * at a bucket boundary. So we take the last cluster of the existing |
| 3873 | * extent and split it down the middle. We move the last half of the |
| 3874 | * buckets in the last cluster of the existing extent over to the new |
| 3875 | * extent. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3876 | * |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3877 | * first_bh is the buffer at prev_blkno so we can update the existing |
| 3878 | * extent's bucket count. header_bh is the bucket were we were hoping |
| 3879 | * to insert our xattr. If the bucket move places the target in the new |
| 3880 | * extent, we'll update first_bh and header_bh after modifying the old |
| 3881 | * extent. |
| 3882 | * |
| 3883 | * first_hash will be set as the 1st xe's name_hash in the new extent. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3884 | */ |
| 3885 | static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode, |
| 3886 | handle_t *handle, |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3887 | struct ocfs2_xattr_bucket *first, |
| 3888 | struct ocfs2_xattr_bucket *target, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3889 | u64 new_blkno, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3890 | u32 num_clusters, |
| 3891 | u32 *first_hash) |
| 3892 | { |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 3893 | int ret; |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3894 | struct super_block *sb = inode->i_sb; |
| 3895 | int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb); |
| 3896 | int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb)); |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3897 | int to_move = num_buckets / 2; |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 3898 | u64 src_blkno; |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3899 | u64 last_cluster_blkno = bucket_blkno(first) + |
| 3900 | ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3901 | |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3902 | BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets); |
| 3903 | BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3904 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3905 | mlog(0, "move half of xattrs in cluster %llu to %llu\n", |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 3906 | (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3907 | |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3908 | ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first), |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 3909 | last_cluster_blkno, new_blkno, |
| 3910 | to_move, first_hash); |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3911 | if (ret) { |
| 3912 | mlog_errno(ret); |
| 3913 | goto out; |
| 3914 | } |
| 3915 | |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 3916 | /* This is the first bucket that got moved */ |
| 3917 | src_blkno = last_cluster_blkno + (to_move * blks_per_bucket); |
| 3918 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3919 | /* |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 3920 | * If the target bucket was part of the moved buckets, we need to |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3921 | * update first and target. |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3922 | */ |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3923 | if (bucket_blkno(target) >= src_blkno) { |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3924 | /* Find the block for the new target bucket */ |
| 3925 | src_blkno = new_blkno + |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3926 | (bucket_blkno(target) - src_blkno); |
| 3927 | |
| 3928 | ocfs2_xattr_bucket_relse(first); |
| 3929 | ocfs2_xattr_bucket_relse(target); |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3930 | |
| 3931 | /* |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 3932 | * These shouldn't fail - the buffers are in the |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3933 | * journal from ocfs2_cp_xattr_bucket(). |
| 3934 | */ |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3935 | ret = ocfs2_read_xattr_bucket(first, new_blkno); |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 3936 | if (ret) { |
| 3937 | mlog_errno(ret); |
| 3938 | goto out; |
| 3939 | } |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3940 | ret = ocfs2_read_xattr_bucket(target, src_blkno); |
| 3941 | if (ret) |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3942 | mlog_errno(ret); |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3943 | |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3944 | } |
| 3945 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3946 | out: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3947 | return ret; |
| 3948 | } |
| 3949 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3950 | /* |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 3951 | * Find the suitable pos when we divide a bucket into 2. |
| 3952 | * We have to make sure the xattrs with the same hash value exist |
| 3953 | * in the same bucket. |
| 3954 | * |
| 3955 | * If this ocfs2_xattr_header covers more than one hash value, find a |
| 3956 | * place where the hash value changes. Try to find the most even split. |
| 3957 | * The most common case is that all entries have different hash values, |
| 3958 | * and the first check we make will find a place to split. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3959 | */ |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 3960 | static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh) |
| 3961 | { |
| 3962 | struct ocfs2_xattr_entry *entries = xh->xh_entries; |
| 3963 | int count = le16_to_cpu(xh->xh_count); |
| 3964 | int delta, middle = count / 2; |
| 3965 | |
| 3966 | /* |
| 3967 | * We start at the middle. Each step gets farther away in both |
| 3968 | * directions. We therefore hit the change in hash value |
| 3969 | * nearest to the middle. Note that this loop does not execute for |
| 3970 | * count < 2. |
| 3971 | */ |
| 3972 | for (delta = 0; delta < middle; delta++) { |
| 3973 | /* Let's check delta earlier than middle */ |
| 3974 | if (cmp_xe(&entries[middle - delta - 1], |
| 3975 | &entries[middle - delta])) |
| 3976 | return middle - delta; |
| 3977 | |
| 3978 | /* For even counts, don't walk off the end */ |
| 3979 | if ((middle + delta + 1) == count) |
| 3980 | continue; |
| 3981 | |
| 3982 | /* Now try delta past middle */ |
| 3983 | if (cmp_xe(&entries[middle + delta], |
| 3984 | &entries[middle + delta + 1])) |
| 3985 | return middle + delta + 1; |
| 3986 | } |
| 3987 | |
| 3988 | /* Every entry had the same hash */ |
| 3989 | return count; |
| 3990 | } |
| 3991 | |
| 3992 | /* |
| 3993 | * Move some xattrs in old bucket(blk) to new bucket(new_blk). |
| 3994 | * first_hash will record the 1st hash of the new bucket. |
| 3995 | * |
| 3996 | * Normally half of the xattrs will be moved. But we have to make |
| 3997 | * sure that the xattrs with the same hash value are stored in the |
| 3998 | * same bucket. If all the xattrs in this bucket have the same hash |
| 3999 | * value, the new bucket will be initialized as an empty one and the |
| 4000 | * first_hash will be initialized as (hash_value+1). |
| 4001 | */ |
| 4002 | static int ocfs2_divide_xattr_bucket(struct inode *inode, |
| 4003 | handle_t *handle, |
| 4004 | u64 blk, |
| 4005 | u64 new_blk, |
| 4006 | u32 *first_hash, |
| 4007 | int new_bucket_head) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4008 | { |
| 4009 | int ret, i; |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4010 | int count, start, len, name_value_len = 0, xe_len, name_offset = 0; |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4011 | struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4012 | struct ocfs2_xattr_header *xh; |
| 4013 | struct ocfs2_xattr_entry *xe; |
| 4014 | int blocksize = inode->i_sb->s_blocksize; |
| 4015 | |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4016 | mlog(0, "move some of xattrs from bucket %llu to %llu\n", |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 4017 | (unsigned long long)blk, (unsigned long long)new_blk); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4018 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4019 | s_bucket = ocfs2_xattr_bucket_new(inode); |
| 4020 | t_bucket = ocfs2_xattr_bucket_new(inode); |
| 4021 | if (!s_bucket || !t_bucket) { |
| 4022 | ret = -ENOMEM; |
| 4023 | mlog_errno(ret); |
| 4024 | goto out; |
| 4025 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4026 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4027 | ret = ocfs2_read_xattr_bucket(s_bucket, blk); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4028 | if (ret) { |
| 4029 | mlog_errno(ret); |
| 4030 | goto out; |
| 4031 | } |
| 4032 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4033 | ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 4034 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4035 | if (ret) { |
| 4036 | mlog_errno(ret); |
| 4037 | goto out; |
| 4038 | } |
| 4039 | |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 4040 | /* |
| 4041 | * Even if !new_bucket_head, we're overwriting t_bucket. Thus, |
| 4042 | * there's no need to read it. |
| 4043 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4044 | ret = ocfs2_init_xattr_bucket(t_bucket, new_blk); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4045 | if (ret) { |
| 4046 | mlog_errno(ret); |
| 4047 | goto out; |
| 4048 | } |
| 4049 | |
Joel Becker | 2b656c1 | 2008-11-25 19:00:15 -0800 | [diff] [blame] | 4050 | /* |
| 4051 | * Hey, if we're overwriting t_bucket, what difference does |
| 4052 | * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the |
| 4053 | * same part of ocfs2_cp_xattr_bucket(). |
| 4054 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4055 | ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 4056 | new_bucket_head ? |
| 4057 | OCFS2_JOURNAL_ACCESS_CREATE : |
| 4058 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 4059 | if (ret) { |
| 4060 | mlog_errno(ret); |
| 4061 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4062 | } |
| 4063 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4064 | xh = bucket_xh(s_bucket); |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4065 | count = le16_to_cpu(xh->xh_count); |
| 4066 | start = ocfs2_xattr_find_divide_pos(xh); |
| 4067 | |
| 4068 | if (start == count) { |
| 4069 | xe = &xh->xh_entries[start-1]; |
| 4070 | |
| 4071 | /* |
| 4072 | * initialized a new empty bucket here. |
| 4073 | * The hash value is set as one larger than |
| 4074 | * that of the last entry in the previous bucket. |
| 4075 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4076 | for (i = 0; i < t_bucket->bu_blocks; i++) |
| 4077 | memset(bucket_block(t_bucket, i), 0, blocksize); |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4078 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4079 | xh = bucket_xh(t_bucket); |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4080 | xh->xh_free_start = cpu_to_le16(blocksize); |
| 4081 | xh->xh_entries[0].xe_name_hash = xe->xe_name_hash; |
| 4082 | le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1); |
| 4083 | |
| 4084 | goto set_num_buckets; |
| 4085 | } |
| 4086 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4087 | /* copy the whole bucket to the new first. */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4088 | ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4089 | |
| 4090 | /* update the new bucket. */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4091 | xh = bucket_xh(t_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4092 | |
| 4093 | /* |
| 4094 | * Calculate the total name/value len and xh_free_start for |
| 4095 | * the old bucket first. |
| 4096 | */ |
| 4097 | name_offset = OCFS2_XATTR_BUCKET_SIZE; |
| 4098 | name_value_len = 0; |
| 4099 | for (i = 0; i < start; i++) { |
| 4100 | xe = &xh->xh_entries[i]; |
| 4101 | xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 4102 | if (ocfs2_xattr_is_local(xe)) |
| 4103 | xe_len += |
| 4104 | OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); |
| 4105 | else |
| 4106 | xe_len += OCFS2_XATTR_ROOT_SIZE; |
| 4107 | name_value_len += xe_len; |
| 4108 | if (le16_to_cpu(xe->xe_name_offset) < name_offset) |
| 4109 | name_offset = le16_to_cpu(xe->xe_name_offset); |
| 4110 | } |
| 4111 | |
| 4112 | /* |
| 4113 | * Now begin the modification to the new bucket. |
| 4114 | * |
| 4115 | * In the new bucket, We just move the xattr entry to the beginning |
| 4116 | * and don't touch the name/value. So there will be some holes in the |
| 4117 | * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is |
| 4118 | * called. |
| 4119 | */ |
| 4120 | xe = &xh->xh_entries[start]; |
| 4121 | len = sizeof(struct ocfs2_xattr_entry) * (count - start); |
| 4122 | mlog(0, "mv xattr entry len %d from %d to %d\n", len, |
Mark Fasheh | ff1ec20 | 2008-08-19 10:54:29 -0700 | [diff] [blame] | 4123 | (int)((char *)xe - (char *)xh), |
| 4124 | (int)((char *)xh->xh_entries - (char *)xh)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4125 | memmove((char *)xh->xh_entries, (char *)xe, len); |
| 4126 | xe = &xh->xh_entries[count - start]; |
| 4127 | len = sizeof(struct ocfs2_xattr_entry) * start; |
| 4128 | memset((char *)xe, 0, len); |
| 4129 | |
| 4130 | le16_add_cpu(&xh->xh_count, -start); |
| 4131 | le16_add_cpu(&xh->xh_name_value_len, -name_value_len); |
| 4132 | |
| 4133 | /* Calculate xh_free_start for the new bucket. */ |
| 4134 | xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE); |
| 4135 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { |
| 4136 | xe = &xh->xh_entries[i]; |
| 4137 | xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 4138 | if (ocfs2_xattr_is_local(xe)) |
| 4139 | xe_len += |
| 4140 | OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); |
| 4141 | else |
| 4142 | xe_len += OCFS2_XATTR_ROOT_SIZE; |
| 4143 | if (le16_to_cpu(xe->xe_name_offset) < |
| 4144 | le16_to_cpu(xh->xh_free_start)) |
| 4145 | xh->xh_free_start = xe->xe_name_offset; |
| 4146 | } |
| 4147 | |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4148 | set_num_buckets: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4149 | /* set xh->xh_num_buckets for the new xh. */ |
| 4150 | if (new_bucket_head) |
| 4151 | xh->xh_num_buckets = cpu_to_le16(1); |
| 4152 | else |
| 4153 | xh->xh_num_buckets = 0; |
| 4154 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4155 | ocfs2_xattr_bucket_journal_dirty(handle, t_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4156 | |
| 4157 | /* store the first_hash of the new bucket. */ |
| 4158 | if (first_hash) |
| 4159 | *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash); |
| 4160 | |
| 4161 | /* |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4162 | * Now only update the 1st block of the old bucket. If we |
| 4163 | * just added a new empty bucket, there is no need to modify |
| 4164 | * it. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4165 | */ |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4166 | if (start == count) |
| 4167 | goto out; |
| 4168 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4169 | xh = bucket_xh(s_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4170 | memset(&xh->xh_entries[start], 0, |
| 4171 | sizeof(struct ocfs2_xattr_entry) * (count - start)); |
| 4172 | xh->xh_count = cpu_to_le16(start); |
| 4173 | xh->xh_free_start = cpu_to_le16(name_offset); |
| 4174 | xh->xh_name_value_len = cpu_to_le16(name_value_len); |
| 4175 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4176 | ocfs2_xattr_bucket_journal_dirty(handle, s_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4177 | |
| 4178 | out: |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4179 | ocfs2_xattr_bucket_free(s_bucket); |
| 4180 | ocfs2_xattr_bucket_free(t_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4181 | |
| 4182 | return ret; |
| 4183 | } |
| 4184 | |
| 4185 | /* |
| 4186 | * Copy xattr from one bucket to another bucket. |
| 4187 | * |
| 4188 | * The caller must make sure that the journal transaction |
| 4189 | * has enough space for journaling. |
| 4190 | */ |
| 4191 | static int ocfs2_cp_xattr_bucket(struct inode *inode, |
| 4192 | handle_t *handle, |
| 4193 | u64 s_blkno, |
| 4194 | u64 t_blkno, |
| 4195 | int t_is_new) |
| 4196 | { |
Joel Becker | 4980c6d | 2008-10-24 18:54:43 -0700 | [diff] [blame] | 4197 | int ret; |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4198 | struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4199 | |
| 4200 | BUG_ON(s_blkno == t_blkno); |
| 4201 | |
| 4202 | mlog(0, "cp bucket %llu to %llu, target is %d\n", |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 4203 | (unsigned long long)s_blkno, (unsigned long long)t_blkno, |
| 4204 | t_is_new); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4205 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4206 | s_bucket = ocfs2_xattr_bucket_new(inode); |
| 4207 | t_bucket = ocfs2_xattr_bucket_new(inode); |
| 4208 | if (!s_bucket || !t_bucket) { |
| 4209 | ret = -ENOMEM; |
| 4210 | mlog_errno(ret); |
| 4211 | goto out; |
| 4212 | } |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4213 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4214 | ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4215 | if (ret) |
| 4216 | goto out; |
| 4217 | |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 4218 | /* |
| 4219 | * Even if !t_is_new, we're overwriting t_bucket. Thus, |
| 4220 | * there's no need to read it. |
| 4221 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4222 | ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4223 | if (ret) |
| 4224 | goto out; |
| 4225 | |
Joel Becker | 2b656c1 | 2008-11-25 19:00:15 -0800 | [diff] [blame] | 4226 | /* |
| 4227 | * Hey, if we're overwriting t_bucket, what difference does |
| 4228 | * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new |
Joel Becker | 874d65a | 2008-11-26 13:02:18 -0800 | [diff] [blame] | 4229 | * cluster to fill, we came here from |
| 4230 | * ocfs2_mv_xattr_buckets(), and it is really new - |
| 4231 | * ACCESS_CREATE is required. But we also might have moved data |
| 4232 | * out of t_bucket before extending back into it. |
| 4233 | * ocfs2_add_new_xattr_bucket() can do this - its call to |
| 4234 | * ocfs2_add_new_xattr_cluster() may have created a new extent |
Joel Becker | 2b656c1 | 2008-11-25 19:00:15 -0800 | [diff] [blame] | 4235 | * and copied out the end of the old extent. Then it re-extends |
| 4236 | * the old extent back to create space for new xattrs. That's |
| 4237 | * how we get here, and the bucket isn't really new. |
| 4238 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4239 | ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 4240 | t_is_new ? |
| 4241 | OCFS2_JOURNAL_ACCESS_CREATE : |
| 4242 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 4243 | if (ret) |
| 4244 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4245 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4246 | ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket); |
| 4247 | ocfs2_xattr_bucket_journal_dirty(handle, t_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4248 | |
| 4249 | out: |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4250 | ocfs2_xattr_bucket_free(t_bucket); |
| 4251 | ocfs2_xattr_bucket_free(s_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4252 | |
| 4253 | return ret; |
| 4254 | } |
| 4255 | |
| 4256 | /* |
Joel Becker | 874d65a | 2008-11-26 13:02:18 -0800 | [diff] [blame] | 4257 | * src_blk points to the start of an existing extent. last_blk points to |
| 4258 | * last cluster in that extent. to_blk points to a newly allocated |
Joel Becker | 54ecb6b | 2008-11-26 13:18:31 -0800 | [diff] [blame] | 4259 | * extent. We copy the buckets from the cluster at last_blk to the new |
| 4260 | * extent. If start_bucket is non-zero, we skip that many buckets before |
| 4261 | * we start copying. The new extent's xh_num_buckets gets set to the |
| 4262 | * number of buckets we copied. The old extent's xh_num_buckets shrinks |
| 4263 | * by the same amount. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4264 | */ |
Joel Becker | 54ecb6b | 2008-11-26 13:18:31 -0800 | [diff] [blame] | 4265 | static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle, |
| 4266 | u64 src_blk, u64 last_blk, u64 to_blk, |
| 4267 | unsigned int start_bucket, |
| 4268 | u32 *first_hash) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4269 | { |
| 4270 | int i, ret, credits; |
| 4271 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4272 | int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4273 | int num_buckets = ocfs2_xattr_buckets_per_cluster(osb); |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4274 | struct ocfs2_xattr_bucket *old_first, *new_first; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4275 | |
Joel Becker | 874d65a | 2008-11-26 13:02:18 -0800 | [diff] [blame] | 4276 | mlog(0, "mv xattrs from cluster %llu to %llu\n", |
| 4277 | (unsigned long long)last_blk, (unsigned long long)to_blk); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4278 | |
Joel Becker | 54ecb6b | 2008-11-26 13:18:31 -0800 | [diff] [blame] | 4279 | BUG_ON(start_bucket >= num_buckets); |
| 4280 | if (start_bucket) { |
| 4281 | num_buckets -= start_bucket; |
| 4282 | last_blk += (start_bucket * blks_per_bucket); |
| 4283 | } |
| 4284 | |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4285 | /* The first bucket of the original extent */ |
| 4286 | old_first = ocfs2_xattr_bucket_new(inode); |
| 4287 | /* The first bucket of the new extent */ |
| 4288 | new_first = ocfs2_xattr_bucket_new(inode); |
| 4289 | if (!old_first || !new_first) { |
| 4290 | ret = -ENOMEM; |
| 4291 | mlog_errno(ret); |
| 4292 | goto out; |
| 4293 | } |
| 4294 | |
Joel Becker | 874d65a | 2008-11-26 13:02:18 -0800 | [diff] [blame] | 4295 | ret = ocfs2_read_xattr_bucket(old_first, src_blk); |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4296 | if (ret) { |
| 4297 | mlog_errno(ret); |
| 4298 | goto out; |
| 4299 | } |
| 4300 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4301 | /* |
Joel Becker | 54ecb6b | 2008-11-26 13:18:31 -0800 | [diff] [blame] | 4302 | * We need to update the first bucket of the old extent and all |
| 4303 | * the buckets going to the new extent. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4304 | */ |
Joel Becker | 54ecb6b | 2008-11-26 13:18:31 -0800 | [diff] [blame] | 4305 | credits = ((num_buckets + 1) * blks_per_bucket) + |
| 4306 | handle->h_buffer_credits; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4307 | ret = ocfs2_extend_trans(handle, credits); |
| 4308 | if (ret) { |
| 4309 | mlog_errno(ret); |
| 4310 | goto out; |
| 4311 | } |
| 4312 | |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4313 | ret = ocfs2_xattr_bucket_journal_access(handle, old_first, |
| 4314 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4315 | if (ret) { |
| 4316 | mlog_errno(ret); |
| 4317 | goto out; |
| 4318 | } |
| 4319 | |
| 4320 | for (i = 0; i < num_buckets; i++) { |
| 4321 | ret = ocfs2_cp_xattr_bucket(inode, handle, |
Joel Becker | 874d65a | 2008-11-26 13:02:18 -0800 | [diff] [blame] | 4322 | last_blk + (i * blks_per_bucket), |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4323 | to_blk + (i * blks_per_bucket), |
| 4324 | 1); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4325 | if (ret) { |
| 4326 | mlog_errno(ret); |
| 4327 | goto out; |
| 4328 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4329 | } |
| 4330 | |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4331 | /* |
| 4332 | * Get the new bucket ready before we dirty anything |
| 4333 | * (This actually shouldn't fail, because we already dirtied |
| 4334 | * it once in ocfs2_cp_xattr_bucket()). |
| 4335 | */ |
| 4336 | ret = ocfs2_read_xattr_bucket(new_first, to_blk); |
| 4337 | if (ret) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4338 | mlog_errno(ret); |
| 4339 | goto out; |
| 4340 | } |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4341 | ret = ocfs2_xattr_bucket_journal_access(handle, new_first, |
| 4342 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4343 | if (ret) { |
| 4344 | mlog_errno(ret); |
| 4345 | goto out; |
| 4346 | } |
| 4347 | |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4348 | /* Now update the headers */ |
| 4349 | le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets); |
| 4350 | ocfs2_xattr_bucket_journal_dirty(handle, old_first); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4351 | |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4352 | bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets); |
| 4353 | ocfs2_xattr_bucket_journal_dirty(handle, new_first); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4354 | |
| 4355 | if (first_hash) |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4356 | *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash); |
| 4357 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4358 | out: |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4359 | ocfs2_xattr_bucket_free(new_first); |
| 4360 | ocfs2_xattr_bucket_free(old_first); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4361 | return ret; |
| 4362 | } |
| 4363 | |
| 4364 | /* |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4365 | * Move some xattrs in this cluster to the new cluster. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4366 | * This function should only be called when bucket size == cluster size. |
| 4367 | * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead. |
| 4368 | */ |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4369 | static int ocfs2_divide_xattr_cluster(struct inode *inode, |
| 4370 | handle_t *handle, |
| 4371 | u64 prev_blk, |
| 4372 | u64 new_blk, |
| 4373 | u32 *first_hash) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4374 | { |
| 4375 | u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4376 | int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4377 | |
| 4378 | BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize); |
| 4379 | |
| 4380 | ret = ocfs2_extend_trans(handle, credits); |
| 4381 | if (ret) { |
| 4382 | mlog_errno(ret); |
| 4383 | return ret; |
| 4384 | } |
| 4385 | |
| 4386 | /* Move half of the xattr in start_blk to the next bucket. */ |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4387 | return ocfs2_divide_xattr_bucket(inode, handle, prev_blk, |
| 4388 | new_blk, first_hash, 1); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4389 | } |
| 4390 | |
| 4391 | /* |
| 4392 | * Move some xattrs from the old cluster to the new one since they are not |
| 4393 | * contiguous in ocfs2 xattr tree. |
| 4394 | * |
| 4395 | * new_blk starts a new separate cluster, and we will move some xattrs from |
| 4396 | * prev_blk to it. v_start will be set as the first name hash value in this |
| 4397 | * new cluster so that it can be used as e_cpos during tree insertion and |
| 4398 | * don't collide with our original b-tree operations. first_bh and header_bh |
| 4399 | * will also be updated since they will be used in ocfs2_extend_xattr_bucket |
| 4400 | * to extend the insert bucket. |
| 4401 | * |
| 4402 | * The problem is how much xattr should we move to the new one and when should |
| 4403 | * we update first_bh and header_bh? |
| 4404 | * 1. If cluster size > bucket size, that means the previous cluster has more |
| 4405 | * than 1 bucket, so just move half nums of bucket into the new cluster and |
| 4406 | * update the first_bh and header_bh if the insert bucket has been moved |
| 4407 | * to the new cluster. |
| 4408 | * 2. If cluster_size == bucket_size: |
| 4409 | * a) If the previous extent rec has more than one cluster and the insert |
| 4410 | * place isn't in the last cluster, copy the entire last cluster to the |
| 4411 | * new one. This time, we don't need to upate the first_bh and header_bh |
| 4412 | * since they will not be moved into the new cluster. |
| 4413 | * b) Otherwise, move the bottom half of the xattrs in the last cluster into |
| 4414 | * the new one. And we set the extend flag to zero if the insert place is |
| 4415 | * moved into the new allocated cluster since no extend is needed. |
| 4416 | */ |
| 4417 | static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode, |
| 4418 | handle_t *handle, |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4419 | struct ocfs2_xattr_bucket *first, |
| 4420 | struct ocfs2_xattr_bucket *target, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4421 | u64 new_blk, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4422 | u32 prev_clusters, |
| 4423 | u32 *v_start, |
| 4424 | int *extend) |
| 4425 | { |
Joel Becker | 92cf3ad | 2008-11-26 14:12:09 -0800 | [diff] [blame] | 4426 | int ret; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4427 | |
| 4428 | mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n", |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4429 | (unsigned long long)bucket_blkno(first), prev_clusters, |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 4430 | (unsigned long long)new_blk); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4431 | |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4432 | if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4433 | ret = ocfs2_mv_xattr_bucket_cross_cluster(inode, |
| 4434 | handle, |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4435 | first, target, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4436 | new_blk, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4437 | prev_clusters, |
| 4438 | v_start); |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4439 | if (ret) |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4440 | mlog_errno(ret); |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4441 | } else { |
Joel Becker | 92cf3ad | 2008-11-26 14:12:09 -0800 | [diff] [blame] | 4442 | /* The start of the last cluster in the first extent */ |
| 4443 | u64 last_blk = bucket_blkno(first) + |
| 4444 | ((prev_clusters - 1) * |
| 4445 | ocfs2_clusters_to_blocks(inode->i_sb, 1)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4446 | |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4447 | if (prev_clusters > 1 && bucket_blkno(target) != last_blk) { |
Joel Becker | 874d65a | 2008-11-26 13:02:18 -0800 | [diff] [blame] | 4448 | ret = ocfs2_mv_xattr_buckets(inode, handle, |
Joel Becker | 92cf3ad | 2008-11-26 14:12:09 -0800 | [diff] [blame] | 4449 | bucket_blkno(first), |
Joel Becker | 54ecb6b | 2008-11-26 13:18:31 -0800 | [diff] [blame] | 4450 | last_blk, new_blk, 0, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4451 | v_start); |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4452 | if (ret) |
| 4453 | mlog_errno(ret); |
| 4454 | } else { |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4455 | ret = ocfs2_divide_xattr_cluster(inode, handle, |
| 4456 | last_blk, new_blk, |
| 4457 | v_start); |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4458 | if (ret) |
| 4459 | mlog_errno(ret); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4460 | |
Joel Becker | 92cf3ad | 2008-11-26 14:12:09 -0800 | [diff] [blame] | 4461 | if ((bucket_blkno(target) == last_blk) && extend) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4462 | *extend = 0; |
| 4463 | } |
| 4464 | } |
| 4465 | |
| 4466 | return ret; |
| 4467 | } |
| 4468 | |
| 4469 | /* |
| 4470 | * Add a new cluster for xattr storage. |
| 4471 | * |
| 4472 | * If the new cluster is contiguous with the previous one, it will be |
| 4473 | * appended to the same extent record, and num_clusters will be updated. |
| 4474 | * If not, we will insert a new extent for it and move some xattrs in |
| 4475 | * the last cluster into the new allocated one. |
| 4476 | * We also need to limit the maximum size of a btree leaf, otherwise we'll |
| 4477 | * lose the benefits of hashing because we'll have to search large leaves. |
| 4478 | * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize, |
| 4479 | * if it's bigger). |
| 4480 | * |
| 4481 | * first_bh is the first block of the previous extent rec and header_bh |
| 4482 | * indicates the bucket we will insert the new xattrs. They will be updated |
| 4483 | * when the header_bh is moved into the new cluster. |
| 4484 | */ |
| 4485 | static int ocfs2_add_new_xattr_cluster(struct inode *inode, |
| 4486 | struct buffer_head *root_bh, |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4487 | struct ocfs2_xattr_bucket *first, |
| 4488 | struct ocfs2_xattr_bucket *target, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4489 | u32 *num_clusters, |
| 4490 | u32 prev_cpos, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4491 | int *extend, |
| 4492 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4493 | { |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4494 | int ret; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4495 | u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); |
| 4496 | u32 prev_clusters = *num_clusters; |
| 4497 | u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0; |
| 4498 | u64 block; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4499 | handle_t *handle = ctxt->handle; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4500 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4501 | struct ocfs2_extent_tree et; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4502 | |
| 4503 | mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, " |
| 4504 | "previous xattr blkno = %llu\n", |
| 4505 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4506 | prev_cpos, (unsigned long long)bucket_blkno(first)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4507 | |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 4508 | ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4509 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 4510 | ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh, |
Joel Becker | 8400897 | 2008-12-09 16:11:49 -0800 | [diff] [blame] | 4511 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4512 | if (ret < 0) { |
| 4513 | mlog_errno(ret); |
| 4514 | goto leave; |
| 4515 | } |
| 4516 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4517 | ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4518 | clusters_to_add, &bit_off, &num_bits); |
| 4519 | if (ret < 0) { |
| 4520 | if (ret != -ENOSPC) |
| 4521 | mlog_errno(ret); |
| 4522 | goto leave; |
| 4523 | } |
| 4524 | |
| 4525 | BUG_ON(num_bits > clusters_to_add); |
| 4526 | |
| 4527 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); |
| 4528 | mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n", |
| 4529 | num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 4530 | |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4531 | if (bucket_blkno(first) + (prev_clusters * bpc) == block && |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4532 | (prev_clusters + num_bits) << osb->s_clustersize_bits <= |
| 4533 | OCFS2_MAX_XATTR_TREE_LEAF_SIZE) { |
| 4534 | /* |
| 4535 | * If this cluster is contiguous with the old one and |
| 4536 | * adding this new cluster, we don't surpass the limit of |
| 4537 | * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be |
| 4538 | * initialized and used like other buckets in the previous |
| 4539 | * cluster. |
| 4540 | * So add it as a contiguous one. The caller will handle |
| 4541 | * its init process. |
| 4542 | */ |
| 4543 | v_start = prev_cpos + prev_clusters; |
| 4544 | *num_clusters = prev_clusters + num_bits; |
| 4545 | mlog(0, "Add contiguous %u clusters to previous extent rec.\n", |
| 4546 | num_bits); |
| 4547 | } else { |
| 4548 | ret = ocfs2_adjust_xattr_cross_cluster(inode, |
| 4549 | handle, |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4550 | first, |
| 4551 | target, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4552 | block, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4553 | prev_clusters, |
| 4554 | &v_start, |
| 4555 | extend); |
| 4556 | if (ret) { |
| 4557 | mlog_errno(ret); |
| 4558 | goto leave; |
| 4559 | } |
| 4560 | } |
| 4561 | |
| 4562 | mlog(0, "Insert %u clusters at block %llu for xattr at %u\n", |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 4563 | num_bits, (unsigned long long)block, v_start); |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4564 | ret = ocfs2_insert_extent(handle, &et, v_start, block, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4565 | num_bits, 0, ctxt->meta_ac); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4566 | if (ret < 0) { |
| 4567 | mlog_errno(ret); |
| 4568 | goto leave; |
| 4569 | } |
| 4570 | |
| 4571 | ret = ocfs2_journal_dirty(handle, root_bh); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4572 | if (ret < 0) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4573 | mlog_errno(ret); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4574 | |
| 4575 | leave: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4576 | return ret; |
| 4577 | } |
| 4578 | |
| 4579 | /* |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4580 | * We are given an extent. 'first' is the bucket at the very front of |
| 4581 | * the extent. The extent has space for an additional bucket past |
| 4582 | * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number |
| 4583 | * of the target bucket. We wish to shift every bucket past the target |
| 4584 | * down one, filling in that additional space. When we get back to the |
| 4585 | * target, we split the target between itself and the now-empty bucket |
| 4586 | * at target+1 (aka, target_blkno + blks_per_bucket). |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4587 | */ |
| 4588 | static int ocfs2_extend_xattr_bucket(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4589 | handle_t *handle, |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4590 | struct ocfs2_xattr_bucket *first, |
| 4591 | u64 target_blk, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4592 | u32 num_clusters) |
| 4593 | { |
| 4594 | int ret, credits; |
| 4595 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 4596 | u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4597 | u64 end_blk; |
| 4598 | u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4599 | |
| 4600 | mlog(0, "extend xattr bucket in %llu, xattr extend rec starting " |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4601 | "from %llu, len = %u\n", (unsigned long long)target_blk, |
| 4602 | (unsigned long long)bucket_blkno(first), num_clusters); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4603 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4604 | /* The extent must have room for an additional bucket */ |
| 4605 | BUG_ON(new_bucket >= |
| 4606 | (num_clusters * ocfs2_xattr_buckets_per_cluster(osb))); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4607 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4608 | /* end_blk points to the last existing bucket */ |
| 4609 | end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4610 | |
| 4611 | /* |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4612 | * end_blk is the start of the last existing bucket. |
| 4613 | * Thus, (end_blk - target_blk) covers the target bucket and |
| 4614 | * every bucket after it up to, but not including, the last |
| 4615 | * existing bucket. Then we add the last existing bucket, the |
| 4616 | * new bucket, and the first bucket (3 * blk_per_bucket). |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4617 | */ |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4618 | credits = (end_blk - target_blk) + (3 * blk_per_bucket) + |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4619 | handle->h_buffer_credits; |
| 4620 | ret = ocfs2_extend_trans(handle, credits); |
| 4621 | if (ret) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4622 | mlog_errno(ret); |
| 4623 | goto out; |
| 4624 | } |
| 4625 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4626 | ret = ocfs2_xattr_bucket_journal_access(handle, first, |
| 4627 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4628 | if (ret) { |
| 4629 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4630 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4631 | } |
| 4632 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4633 | while (end_blk != target_blk) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4634 | ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk, |
| 4635 | end_blk + blk_per_bucket, 0); |
| 4636 | if (ret) |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4637 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4638 | end_blk -= blk_per_bucket; |
| 4639 | } |
| 4640 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4641 | /* Move half of the xattr in target_blkno to the next bucket. */ |
| 4642 | ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk, |
| 4643 | target_blk + blk_per_bucket, NULL, 0); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4644 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4645 | le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1); |
| 4646 | ocfs2_xattr_bucket_journal_dirty(handle, first); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4647 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4648 | out: |
| 4649 | return ret; |
| 4650 | } |
| 4651 | |
| 4652 | /* |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4653 | * Add new xattr bucket in an extent record and adjust the buckets |
| 4654 | * accordingly. xb_bh is the ocfs2_xattr_block, and target is the |
| 4655 | * bucket we want to insert into. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4656 | * |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4657 | * In the easy case, we will move all the buckets after target down by |
| 4658 | * one. Half of target's xattrs will be moved to the next bucket. |
| 4659 | * |
| 4660 | * If current cluster is full, we'll allocate a new one. This may not |
| 4661 | * be contiguous. The underlying calls will make sure that there is |
| 4662 | * space for the insert, shifting buckets around if necessary. |
| 4663 | * 'target' may be moved by those calls. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4664 | */ |
| 4665 | static int ocfs2_add_new_xattr_bucket(struct inode *inode, |
| 4666 | struct buffer_head *xb_bh, |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4667 | struct ocfs2_xattr_bucket *target, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4668 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4669 | { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4670 | struct ocfs2_xattr_block *xb = |
| 4671 | (struct ocfs2_xattr_block *)xb_bh->b_data; |
| 4672 | struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root; |
| 4673 | struct ocfs2_extent_list *el = &xb_root->xt_list; |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4674 | u32 name_hash = |
| 4675 | le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash); |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4676 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4677 | int ret, num_buckets, extend = 1; |
| 4678 | u64 p_blkno; |
| 4679 | u32 e_cpos, num_clusters; |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4680 | /* The bucket at the front of the extent */ |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4681 | struct ocfs2_xattr_bucket *first; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4682 | |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4683 | mlog(0, "Add new xattr bucket starting from %llu\n", |
| 4684 | (unsigned long long)bucket_blkno(target)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4685 | |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4686 | /* The first bucket of the original extent */ |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4687 | first = ocfs2_xattr_bucket_new(inode); |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4688 | if (!first) { |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4689 | ret = -ENOMEM; |
| 4690 | mlog_errno(ret); |
| 4691 | goto out; |
| 4692 | } |
| 4693 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4694 | ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos, |
| 4695 | &num_clusters, el); |
| 4696 | if (ret) { |
| 4697 | mlog_errno(ret); |
| 4698 | goto out; |
| 4699 | } |
| 4700 | |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4701 | ret = ocfs2_read_xattr_bucket(first, p_blkno); |
| 4702 | if (ret) { |
| 4703 | mlog_errno(ret); |
| 4704 | goto out; |
| 4705 | } |
| 4706 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4707 | num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters; |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4708 | if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) { |
| 4709 | /* |
| 4710 | * This can move first+target if the target bucket moves |
| 4711 | * to the new extent. |
| 4712 | */ |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4713 | ret = ocfs2_add_new_xattr_cluster(inode, |
| 4714 | xb_bh, |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4715 | first, |
| 4716 | target, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4717 | &num_clusters, |
| 4718 | e_cpos, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4719 | &extend, |
| 4720 | ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4721 | if (ret) { |
| 4722 | mlog_errno(ret); |
| 4723 | goto out; |
| 4724 | } |
| 4725 | } |
| 4726 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4727 | if (extend) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4728 | ret = ocfs2_extend_xattr_bucket(inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4729 | ctxt->handle, |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4730 | first, |
| 4731 | bucket_blkno(target), |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4732 | num_clusters); |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4733 | if (ret) |
| 4734 | mlog_errno(ret); |
| 4735 | } |
| 4736 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4737 | out: |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4738 | ocfs2_xattr_bucket_free(first); |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4739 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4740 | return ret; |
| 4741 | } |
| 4742 | |
| 4743 | static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode, |
| 4744 | struct ocfs2_xattr_bucket *bucket, |
| 4745 | int offs) |
| 4746 | { |
| 4747 | int block_off = offs >> inode->i_sb->s_blocksize_bits; |
| 4748 | |
| 4749 | offs = offs % inode->i_sb->s_blocksize; |
Joel Becker | 51def39 | 2008-10-24 16:57:21 -0700 | [diff] [blame] | 4750 | return bucket_block(bucket, block_off) + offs; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4751 | } |
| 4752 | |
| 4753 | /* |
| 4754 | * Handle the normal xattr set, including replace, delete and new. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4755 | * |
| 4756 | * Note: "local" indicates the real data's locality. So we can't |
| 4757 | * just its bucket locality by its length. |
| 4758 | */ |
| 4759 | static void ocfs2_xattr_set_entry_normal(struct inode *inode, |
| 4760 | struct ocfs2_xattr_info *xi, |
| 4761 | struct ocfs2_xattr_search *xs, |
| 4762 | u32 name_hash, |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 4763 | int local) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4764 | { |
| 4765 | struct ocfs2_xattr_entry *last, *xe; |
| 4766 | int name_len = strlen(xi->name); |
| 4767 | struct ocfs2_xattr_header *xh = xs->header; |
| 4768 | u16 count = le16_to_cpu(xh->xh_count), start; |
| 4769 | size_t blocksize = inode->i_sb->s_blocksize; |
| 4770 | char *val; |
| 4771 | size_t offs, size, new_size; |
| 4772 | |
| 4773 | last = &xh->xh_entries[count]; |
| 4774 | if (!xs->not_found) { |
| 4775 | xe = xs->here; |
| 4776 | offs = le16_to_cpu(xe->xe_name_offset); |
| 4777 | if (ocfs2_xattr_is_local(xe)) |
| 4778 | size = OCFS2_XATTR_SIZE(name_len) + |
| 4779 | OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); |
| 4780 | else |
| 4781 | size = OCFS2_XATTR_SIZE(name_len) + |
| 4782 | OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE); |
| 4783 | |
| 4784 | /* |
| 4785 | * If the new value will be stored outside, xi->value has been |
| 4786 | * initalized as an empty ocfs2_xattr_value_root, and the same |
| 4787 | * goes with xi->value_len, so we can set new_size safely here. |
| 4788 | * See ocfs2_xattr_set_in_bucket. |
| 4789 | */ |
| 4790 | new_size = OCFS2_XATTR_SIZE(name_len) + |
| 4791 | OCFS2_XATTR_SIZE(xi->value_len); |
| 4792 | |
| 4793 | le16_add_cpu(&xh->xh_name_value_len, -size); |
| 4794 | if (xi->value) { |
| 4795 | if (new_size > size) |
| 4796 | goto set_new_name_value; |
| 4797 | |
| 4798 | /* Now replace the old value with new one. */ |
| 4799 | if (local) |
| 4800 | xe->xe_value_size = cpu_to_le64(xi->value_len); |
| 4801 | else |
| 4802 | xe->xe_value_size = 0; |
| 4803 | |
| 4804 | val = ocfs2_xattr_bucket_get_val(inode, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4805 | xs->bucket, offs); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4806 | memset(val + OCFS2_XATTR_SIZE(name_len), 0, |
| 4807 | size - OCFS2_XATTR_SIZE(name_len)); |
| 4808 | if (OCFS2_XATTR_SIZE(xi->value_len) > 0) |
| 4809 | memcpy(val + OCFS2_XATTR_SIZE(name_len), |
| 4810 | xi->value, xi->value_len); |
| 4811 | |
| 4812 | le16_add_cpu(&xh->xh_name_value_len, new_size); |
| 4813 | ocfs2_xattr_set_local(xe, local); |
| 4814 | return; |
| 4815 | } else { |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 4816 | /* |
| 4817 | * Remove the old entry if there is more than one. |
| 4818 | * We don't remove the last entry so that we can |
| 4819 | * use it to indicate the hash value of the empty |
| 4820 | * bucket. |
| 4821 | */ |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4822 | last -= 1; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4823 | le16_add_cpu(&xh->xh_count, -1); |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 4824 | if (xh->xh_count) { |
| 4825 | memmove(xe, xe + 1, |
| 4826 | (void *)last - (void *)xe); |
| 4827 | memset(last, 0, |
| 4828 | sizeof(struct ocfs2_xattr_entry)); |
| 4829 | } else |
| 4830 | xh->xh_free_start = |
| 4831 | cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE); |
| 4832 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4833 | return; |
| 4834 | } |
| 4835 | } else { |
| 4836 | /* find a new entry for insert. */ |
| 4837 | int low = 0, high = count - 1, tmp; |
| 4838 | struct ocfs2_xattr_entry *tmp_xe; |
| 4839 | |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 4840 | while (low <= high && count) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4841 | tmp = (low + high) / 2; |
| 4842 | tmp_xe = &xh->xh_entries[tmp]; |
| 4843 | |
| 4844 | if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash)) |
| 4845 | low = tmp + 1; |
| 4846 | else if (name_hash < |
| 4847 | le32_to_cpu(tmp_xe->xe_name_hash)) |
| 4848 | high = tmp - 1; |
Tao Ma | 06b240d | 2008-09-19 22:16:34 +0800 | [diff] [blame] | 4849 | else { |
| 4850 | low = tmp; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4851 | break; |
Tao Ma | 06b240d | 2008-09-19 22:16:34 +0800 | [diff] [blame] | 4852 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4853 | } |
| 4854 | |
| 4855 | xe = &xh->xh_entries[low]; |
| 4856 | if (low != count) |
| 4857 | memmove(xe + 1, xe, (void *)last - (void *)xe); |
| 4858 | |
| 4859 | le16_add_cpu(&xh->xh_count, 1); |
| 4860 | memset(xe, 0, sizeof(struct ocfs2_xattr_entry)); |
| 4861 | xe->xe_name_hash = cpu_to_le32(name_hash); |
| 4862 | xe->xe_name_len = name_len; |
| 4863 | ocfs2_xattr_set_type(xe, xi->name_index); |
| 4864 | } |
| 4865 | |
| 4866 | set_new_name_value: |
| 4867 | /* Insert the new name+value. */ |
| 4868 | size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len); |
| 4869 | |
| 4870 | /* |
| 4871 | * We must make sure that the name/value pair |
| 4872 | * exists in the same block. |
| 4873 | */ |
| 4874 | offs = le16_to_cpu(xh->xh_free_start); |
| 4875 | start = offs - size; |
| 4876 | |
| 4877 | if (start >> inode->i_sb->s_blocksize_bits != |
| 4878 | (offs - 1) >> inode->i_sb->s_blocksize_bits) { |
| 4879 | offs = offs - offs % blocksize; |
| 4880 | xh->xh_free_start = cpu_to_le16(offs); |
| 4881 | } |
| 4882 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4883 | val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4884 | xe->xe_name_offset = cpu_to_le16(offs - size); |
| 4885 | |
| 4886 | memset(val, 0, size); |
| 4887 | memcpy(val, xi->name, name_len); |
| 4888 | memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len); |
| 4889 | |
| 4890 | xe->xe_value_size = cpu_to_le64(xi->value_len); |
| 4891 | ocfs2_xattr_set_local(xe, local); |
| 4892 | xs->here = xe; |
| 4893 | le16_add_cpu(&xh->xh_free_start, -size); |
| 4894 | le16_add_cpu(&xh->xh_name_value_len, size); |
| 4895 | |
| 4896 | return; |
| 4897 | } |
| 4898 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4899 | /* |
| 4900 | * Set the xattr entry in the specified bucket. |
| 4901 | * The bucket is indicated by xs->bucket and it should have the enough |
| 4902 | * space for the xattr insertion. |
| 4903 | */ |
| 4904 | static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4905 | handle_t *handle, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4906 | struct ocfs2_xattr_info *xi, |
| 4907 | struct ocfs2_xattr_search *xs, |
| 4908 | u32 name_hash, |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 4909 | int local) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4910 | { |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 4911 | int ret; |
Joel Becker | 02dbf38 | 2008-10-27 18:07:45 -0700 | [diff] [blame] | 4912 | u64 blkno; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4913 | |
Mark Fasheh | ff1ec20 | 2008-08-19 10:54:29 -0700 | [diff] [blame] | 4914 | mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n", |
| 4915 | (unsigned long)xi->value_len, xi->name_index, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4916 | (unsigned long long)bucket_blkno(xs->bucket)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4917 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4918 | if (!xs->bucket->bu_bhs[1]) { |
Joel Becker | 02dbf38 | 2008-10-27 18:07:45 -0700 | [diff] [blame] | 4919 | blkno = bucket_blkno(xs->bucket); |
| 4920 | ocfs2_xattr_bucket_relse(xs->bucket); |
| 4921 | ret = ocfs2_read_xattr_bucket(xs->bucket, blkno); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4922 | if (ret) { |
| 4923 | mlog_errno(ret); |
| 4924 | goto out; |
| 4925 | } |
| 4926 | } |
| 4927 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4928 | ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 4929 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 4930 | if (ret < 0) { |
| 4931 | mlog_errno(ret); |
| 4932 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4933 | } |
| 4934 | |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 4935 | ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4936 | ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4937 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4938 | out: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4939 | return ret; |
| 4940 | } |
| 4941 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4942 | /* |
| 4943 | * Truncate the specified xe_off entry in xattr bucket. |
| 4944 | * bucket is indicated by header_bh and len is the new length. |
| 4945 | * Both the ocfs2_xattr_value_root and the entry will be updated here. |
| 4946 | * |
| 4947 | * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed. |
| 4948 | */ |
| 4949 | static int ocfs2_xattr_bucket_value_truncate(struct inode *inode, |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 4950 | struct ocfs2_xattr_bucket *bucket, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4951 | int xe_off, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4952 | int len, |
| 4953 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4954 | { |
| 4955 | int ret, offset; |
| 4956 | u64 value_blk; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4957 | struct ocfs2_xattr_entry *xe; |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 4958 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4959 | size_t blocksize = inode->i_sb->s_blocksize; |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 4960 | struct ocfs2_xattr_value_buf vb = { |
| 4961 | .vb_access = ocfs2_journal_access, |
| 4962 | }; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4963 | |
| 4964 | xe = &xh->xh_entries[xe_off]; |
| 4965 | |
| 4966 | BUG_ON(!xe || ocfs2_xattr_is_local(xe)); |
| 4967 | |
| 4968 | offset = le16_to_cpu(xe->xe_name_offset) + |
| 4969 | OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 4970 | |
| 4971 | value_blk = offset / blocksize; |
| 4972 | |
| 4973 | /* We don't allow ocfs2_xattr_value to be stored in different block. */ |
| 4974 | BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4975 | |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 4976 | vb.vb_bh = bucket->bu_bhs[value_blk]; |
| 4977 | BUG_ON(!vb.vb_bh); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4978 | |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 4979 | vb.vb_xv = (struct ocfs2_xattr_value_root *) |
| 4980 | (vb.vb_bh->b_data + offset % blocksize); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4981 | |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 4982 | /* |
| 4983 | * From here on out we have to dirty the bucket. The generic |
| 4984 | * value calls only modify one of the bucket's bhs, but we need |
| 4985 | * to send the bucket at once. So if they error, they *could* have |
| 4986 | * modified something. We have to assume they did, and dirty |
| 4987 | * the whole bucket. This leaves us in a consistent state. |
| 4988 | */ |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4989 | mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n", |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 4990 | xe_off, (unsigned long long)bucket_blkno(bucket), len); |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 4991 | ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4992 | if (ret) { |
| 4993 | mlog_errno(ret); |
Tao Ma | 554e7f9 | 2009-01-08 08:21:43 +0800 | [diff] [blame] | 4994 | goto out; |
| 4995 | } |
| 4996 | |
| 4997 | ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket, |
| 4998 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 4999 | if (ret) { |
| 5000 | mlog_errno(ret); |
| 5001 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5002 | } |
| 5003 | |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 5004 | xe->xe_value_size = cpu_to_le64(len); |
| 5005 | |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 5006 | ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5007 | |
| 5008 | out: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5009 | return ret; |
| 5010 | } |
| 5011 | |
| 5012 | static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5013 | struct ocfs2_xattr_search *xs, |
| 5014 | int len, |
| 5015 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5016 | { |
| 5017 | int ret, offset; |
| 5018 | struct ocfs2_xattr_entry *xe = xs->here; |
| 5019 | struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base; |
| 5020 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5021 | BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5022 | |
| 5023 | offset = xe - xh->xh_entries; |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 5024 | ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5025 | offset, len, ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5026 | if (ret) |
| 5027 | mlog_errno(ret); |
| 5028 | |
| 5029 | return ret; |
| 5030 | } |
| 5031 | |
| 5032 | static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5033 | handle_t *handle, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5034 | struct ocfs2_xattr_search *xs, |
| 5035 | char *val, |
| 5036 | int value_len) |
| 5037 | { |
Tao Ma | 712e53e | 2009-03-12 08:37:34 +0800 | [diff] [blame] | 5038 | int ret, offset, block_off; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5039 | struct ocfs2_xattr_value_root *xv; |
| 5040 | struct ocfs2_xattr_entry *xe = xs->here; |
Tao Ma | 712e53e | 2009-03-12 08:37:34 +0800 | [diff] [blame] | 5041 | struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket); |
| 5042 | void *base; |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 5043 | struct ocfs2_xattr_value_buf vb = { |
| 5044 | .vb_access = ocfs2_journal_access, |
| 5045 | }; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5046 | |
| 5047 | BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe)); |
| 5048 | |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 5049 | ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, xh, |
Tao Ma | 712e53e | 2009-03-12 08:37:34 +0800 | [diff] [blame] | 5050 | xe - xh->xh_entries, |
| 5051 | &block_off, |
| 5052 | &offset); |
| 5053 | if (ret) { |
| 5054 | mlog_errno(ret); |
| 5055 | goto out; |
| 5056 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5057 | |
Tao Ma | 712e53e | 2009-03-12 08:37:34 +0800 | [diff] [blame] | 5058 | base = bucket_block(xs->bucket, block_off); |
| 5059 | xv = (struct ocfs2_xattr_value_root *)(base + offset + |
| 5060 | OCFS2_XATTR_SIZE(xe->xe_name_len)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5061 | |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 5062 | vb.vb_xv = xv; |
| 5063 | vb.vb_bh = xs->bucket->bu_bhs[block_off]; |
Tao Ma | 712e53e | 2009-03-12 08:37:34 +0800 | [diff] [blame] | 5064 | ret = __ocfs2_xattr_set_value_outside(inode, handle, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 5065 | &vb, val, value_len); |
Tao Ma | 712e53e | 2009-03-12 08:37:34 +0800 | [diff] [blame] | 5066 | if (ret) |
| 5067 | mlog_errno(ret); |
| 5068 | out: |
| 5069 | return ret; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5070 | } |
| 5071 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5072 | static int ocfs2_rm_xattr_cluster(struct inode *inode, |
| 5073 | struct buffer_head *root_bh, |
| 5074 | u64 blkno, |
| 5075 | u32 cpos, |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 5076 | u32 len, |
| 5077 | void *para) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5078 | { |
| 5079 | int ret; |
| 5080 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5081 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5082 | handle_t *handle; |
| 5083 | struct ocfs2_xattr_block *xb = |
| 5084 | (struct ocfs2_xattr_block *)root_bh->b_data; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5085 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 5086 | struct ocfs2_cached_dealloc_ctxt dealloc; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5087 | struct ocfs2_extent_tree et; |
| 5088 | |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 5089 | ret = ocfs2_iterate_xattr_buckets(inode, blkno, len, |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 5090 | ocfs2_delete_xattr_in_bucket, para); |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 5091 | if (ret) { |
| 5092 | mlog_errno(ret); |
| 5093 | return ret; |
| 5094 | } |
| 5095 | |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 5096 | ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5097 | |
| 5098 | ocfs2_init_dealloc_ctxt(&dealloc); |
| 5099 | |
| 5100 | mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n", |
| 5101 | cpos, len, (unsigned long long)blkno); |
| 5102 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 5103 | ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno, |
| 5104 | len); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5105 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5106 | ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5107 | if (ret) { |
| 5108 | mlog_errno(ret); |
| 5109 | return ret; |
| 5110 | } |
| 5111 | |
| 5112 | mutex_lock(&tl_inode->i_mutex); |
| 5113 | |
| 5114 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 5115 | ret = __ocfs2_flush_truncate_log(osb); |
| 5116 | if (ret < 0) { |
| 5117 | mlog_errno(ret); |
| 5118 | goto out; |
| 5119 | } |
| 5120 | } |
| 5121 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 5122 | handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb)); |
Tao Ma | d326479 | 2008-10-24 07:57:28 +0800 | [diff] [blame] | 5123 | if (IS_ERR(handle)) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5124 | ret = -ENOMEM; |
| 5125 | mlog_errno(ret); |
| 5126 | goto out; |
| 5127 | } |
| 5128 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 5129 | ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh, |
Joel Becker | 8400897 | 2008-12-09 16:11:49 -0800 | [diff] [blame] | 5130 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5131 | if (ret) { |
| 5132 | mlog_errno(ret); |
| 5133 | goto out_commit; |
| 5134 | } |
| 5135 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5136 | ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5137 | &dealloc); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5138 | if (ret) { |
| 5139 | mlog_errno(ret); |
| 5140 | goto out_commit; |
| 5141 | } |
| 5142 | |
| 5143 | le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len); |
| 5144 | |
| 5145 | ret = ocfs2_journal_dirty(handle, root_bh); |
| 5146 | if (ret) { |
| 5147 | mlog_errno(ret); |
| 5148 | goto out_commit; |
| 5149 | } |
| 5150 | |
| 5151 | ret = ocfs2_truncate_log_append(osb, handle, blkno, len); |
| 5152 | if (ret) |
| 5153 | mlog_errno(ret); |
| 5154 | |
| 5155 | out_commit: |
| 5156 | ocfs2_commit_trans(osb, handle); |
| 5157 | out: |
| 5158 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 5159 | |
| 5160 | mutex_unlock(&tl_inode->i_mutex); |
| 5161 | |
| 5162 | if (meta_ac) |
| 5163 | ocfs2_free_alloc_context(meta_ac); |
| 5164 | |
| 5165 | ocfs2_run_deallocs(osb, &dealloc); |
| 5166 | |
| 5167 | return ret; |
| 5168 | } |
| 5169 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5170 | static void ocfs2_xattr_bucket_remove_xs(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5171 | handle_t *handle, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5172 | struct ocfs2_xattr_search *xs) |
| 5173 | { |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5174 | struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5175 | struct ocfs2_xattr_entry *last = &xh->xh_entries[ |
| 5176 | le16_to_cpu(xh->xh_count) - 1]; |
| 5177 | int ret = 0; |
| 5178 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5179 | ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 5180 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5181 | if (ret) { |
| 5182 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5183 | return; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5184 | } |
| 5185 | |
| 5186 | /* Remove the old entry. */ |
| 5187 | memmove(xs->here, xs->here + 1, |
| 5188 | (void *)last - (void *)xs->here); |
| 5189 | memset(last, 0, sizeof(struct ocfs2_xattr_entry)); |
| 5190 | le16_add_cpu(&xh->xh_count, -1); |
| 5191 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5192 | ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5193 | } |
| 5194 | |
| 5195 | /* |
| 5196 | * Set the xattr name/value in the bucket specified in xs. |
| 5197 | * |
| 5198 | * As the new value in xi may be stored in the bucket or in an outside cluster, |
| 5199 | * we divide the whole process into 3 steps: |
| 5200 | * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket) |
| 5201 | * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs) |
| 5202 | * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside) |
| 5203 | * 4. If the clusters for the new outside value can't be allocated, we need |
| 5204 | * to free the xattr we allocated in set. |
| 5205 | */ |
| 5206 | static int ocfs2_xattr_set_in_bucket(struct inode *inode, |
| 5207 | struct ocfs2_xattr_info *xi, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5208 | struct ocfs2_xattr_search *xs, |
| 5209 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5210 | { |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 5211 | int ret, local = 1; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5212 | size_t value_len; |
| 5213 | char *val = (char *)xi->value; |
| 5214 | struct ocfs2_xattr_entry *xe = xs->here; |
Tao Ma | 2057e5c | 2008-10-09 23:06:13 +0800 | [diff] [blame] | 5215 | u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name, |
| 5216 | strlen(xi->name)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5217 | |
| 5218 | if (!xs->not_found && !ocfs2_xattr_is_local(xe)) { |
| 5219 | /* |
| 5220 | * We need to truncate the xattr storage first. |
| 5221 | * |
| 5222 | * If both the old and new value are stored to |
| 5223 | * outside block, we only need to truncate |
| 5224 | * the storage and then set the value outside. |
| 5225 | * |
| 5226 | * If the new value should be stored within block, |
| 5227 | * we should free all the outside block first and |
| 5228 | * the modification to the xattr block will be done |
| 5229 | * by following steps. |
| 5230 | */ |
| 5231 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) |
| 5232 | value_len = xi->value_len; |
| 5233 | else |
| 5234 | value_len = 0; |
| 5235 | |
| 5236 | ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5237 | value_len, |
| 5238 | ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5239 | if (ret) |
| 5240 | goto out; |
| 5241 | |
| 5242 | if (value_len) |
| 5243 | goto set_value_outside; |
| 5244 | } |
| 5245 | |
| 5246 | value_len = xi->value_len; |
| 5247 | /* So we have to handle the inside block change now. */ |
| 5248 | if (value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 5249 | /* |
| 5250 | * If the new value will be stored outside of block, |
| 5251 | * initalize a new empty value root and insert it first. |
| 5252 | */ |
| 5253 | local = 0; |
| 5254 | xi->value = &def_xv; |
| 5255 | xi->value_len = OCFS2_XATTR_ROOT_SIZE; |
| 5256 | } |
| 5257 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5258 | ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs, |
| 5259 | name_hash, local); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5260 | if (ret) { |
| 5261 | mlog_errno(ret); |
| 5262 | goto out; |
| 5263 | } |
| 5264 | |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 5265 | if (value_len <= OCFS2_XATTR_INLINE_SIZE) |
| 5266 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5267 | |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 5268 | /* allocate the space now for the outside block storage. */ |
| 5269 | ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5270 | value_len, ctxt); |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 5271 | if (ret) { |
| 5272 | mlog_errno(ret); |
| 5273 | |
| 5274 | if (xs->not_found) { |
| 5275 | /* |
| 5276 | * We can't allocate enough clusters for outside |
| 5277 | * storage and we have allocated xattr already, |
| 5278 | * so need to remove it. |
| 5279 | */ |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5280 | ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5281 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5282 | goto out; |
| 5283 | } |
| 5284 | |
| 5285 | set_value_outside: |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5286 | ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle, |
| 5287 | xs, val, value_len); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5288 | out: |
| 5289 | return ret; |
| 5290 | } |
| 5291 | |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 5292 | /* |
| 5293 | * check whether the xattr bucket is filled up with the same hash value. |
| 5294 | * If we want to insert the xattr with the same hash, return -ENOSPC. |
| 5295 | * If we want to insert a xattr with different hash value, go ahead |
| 5296 | * and ocfs2_divide_xattr_bucket will handle this. |
| 5297 | */ |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5298 | static int ocfs2_check_xattr_bucket_collision(struct inode *inode, |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 5299 | struct ocfs2_xattr_bucket *bucket, |
| 5300 | const char *name) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5301 | { |
Joel Becker | 3e63294 | 2008-10-24 17:04:49 -0700 | [diff] [blame] | 5302 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 5303 | u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name)); |
| 5304 | |
| 5305 | if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash)) |
| 5306 | return 0; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5307 | |
| 5308 | if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash == |
| 5309 | xh->xh_entries[0].xe_name_hash) { |
| 5310 | mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, " |
| 5311 | "hash = %u\n", |
Joel Becker | 9c7759a | 2008-10-24 16:21:03 -0700 | [diff] [blame] | 5312 | (unsigned long long)bucket_blkno(bucket), |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5313 | le32_to_cpu(xh->xh_entries[0].xe_name_hash)); |
| 5314 | return -ENOSPC; |
| 5315 | } |
| 5316 | |
| 5317 | return 0; |
| 5318 | } |
| 5319 | |
| 5320 | static int ocfs2_xattr_set_entry_index_block(struct inode *inode, |
| 5321 | struct ocfs2_xattr_info *xi, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5322 | struct ocfs2_xattr_search *xs, |
| 5323 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5324 | { |
| 5325 | struct ocfs2_xattr_header *xh; |
| 5326 | struct ocfs2_xattr_entry *xe; |
| 5327 | u16 count, header_size, xh_free_start; |
Joel Becker | 6dde41d | 2008-10-24 17:16:48 -0700 | [diff] [blame] | 5328 | int free, max_free, need, old; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5329 | size_t value_size = 0, name_len = strlen(xi->name); |
| 5330 | size_t blocksize = inode->i_sb->s_blocksize; |
| 5331 | int ret, allocation = 0; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5332 | |
| 5333 | mlog_entry("Set xattr %s in xattr index block\n", xi->name); |
| 5334 | |
| 5335 | try_again: |
| 5336 | xh = xs->header; |
| 5337 | count = le16_to_cpu(xh->xh_count); |
| 5338 | xh_free_start = le16_to_cpu(xh->xh_free_start); |
| 5339 | header_size = sizeof(struct ocfs2_xattr_header) + |
| 5340 | count * sizeof(struct ocfs2_xattr_entry); |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 5341 | max_free = OCFS2_XATTR_BUCKET_SIZE - header_size - |
| 5342 | le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5343 | |
| 5344 | mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size " |
| 5345 | "of %u which exceed block size\n", |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5346 | (unsigned long long)bucket_blkno(xs->bucket), |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5347 | header_size); |
| 5348 | |
| 5349 | if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) |
| 5350 | value_size = OCFS2_XATTR_ROOT_SIZE; |
| 5351 | else if (xi->value) |
| 5352 | value_size = OCFS2_XATTR_SIZE(xi->value_len); |
| 5353 | |
| 5354 | if (xs->not_found) |
| 5355 | need = sizeof(struct ocfs2_xattr_entry) + |
| 5356 | OCFS2_XATTR_SIZE(name_len) + value_size; |
| 5357 | else { |
| 5358 | need = value_size + OCFS2_XATTR_SIZE(name_len); |
| 5359 | |
| 5360 | /* |
| 5361 | * We only replace the old value if the new length is smaller |
| 5362 | * than the old one. Otherwise we will allocate new space in the |
| 5363 | * bucket to store it. |
| 5364 | */ |
| 5365 | xe = xs->here; |
| 5366 | if (ocfs2_xattr_is_local(xe)) |
| 5367 | old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); |
| 5368 | else |
| 5369 | old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE); |
| 5370 | |
| 5371 | if (old >= value_size) |
| 5372 | need = 0; |
| 5373 | } |
| 5374 | |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 5375 | free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5376 | /* |
| 5377 | * We need to make sure the new name/value pair |
| 5378 | * can exist in the same block. |
| 5379 | */ |
| 5380 | if (xh_free_start % blocksize < need) |
| 5381 | free -= xh_free_start % blocksize; |
| 5382 | |
| 5383 | mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, " |
| 5384 | "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len =" |
| 5385 | " %u\n", xs->not_found, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5386 | (unsigned long long)bucket_blkno(xs->bucket), |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5387 | free, need, max_free, le16_to_cpu(xh->xh_free_start), |
| 5388 | le16_to_cpu(xh->xh_name_value_len)); |
| 5389 | |
Tao Ma | 976331d | 2008-11-12 08:26:57 +0800 | [diff] [blame] | 5390 | if (free < need || |
| 5391 | (xs->not_found && |
| 5392 | count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5393 | if (need <= max_free && |
| 5394 | count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) { |
| 5395 | /* |
| 5396 | * We can create the space by defragment. Since only the |
| 5397 | * name/value will be moved, the xe shouldn't be changed |
| 5398 | * in xs. |
| 5399 | */ |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5400 | ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle, |
| 5401 | xs->bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5402 | if (ret) { |
| 5403 | mlog_errno(ret); |
| 5404 | goto out; |
| 5405 | } |
| 5406 | |
| 5407 | xh_free_start = le16_to_cpu(xh->xh_free_start); |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 5408 | free = xh_free_start - header_size |
| 5409 | - OCFS2_XATTR_HEADER_GAP; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5410 | if (xh_free_start % blocksize < need) |
| 5411 | free -= xh_free_start % blocksize; |
| 5412 | |
| 5413 | if (free >= need) |
| 5414 | goto xattr_set; |
| 5415 | |
| 5416 | mlog(0, "Can't get enough space for xattr insert by " |
| 5417 | "defragment. Need %u bytes, but we have %d, so " |
| 5418 | "allocate new bucket for it.\n", need, free); |
| 5419 | } |
| 5420 | |
| 5421 | /* |
| 5422 | * We have to add new buckets or clusters and one |
| 5423 | * allocation should leave us enough space for insert. |
| 5424 | */ |
| 5425 | BUG_ON(allocation); |
| 5426 | |
| 5427 | /* |
| 5428 | * We do not allow for overlapping ranges between buckets. And |
| 5429 | * the maximum number of collisions we will allow for then is |
| 5430 | * one bucket's worth, so check it here whether we need to |
| 5431 | * add a new bucket for the insert. |
| 5432 | */ |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 5433 | ret = ocfs2_check_xattr_bucket_collision(inode, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5434 | xs->bucket, |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 5435 | xi->name); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5436 | if (ret) { |
| 5437 | mlog_errno(ret); |
| 5438 | goto out; |
| 5439 | } |
| 5440 | |
| 5441 | ret = ocfs2_add_new_xattr_bucket(inode, |
| 5442 | xs->xattr_bh, |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 5443 | xs->bucket, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5444 | ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5445 | if (ret) { |
| 5446 | mlog_errno(ret); |
| 5447 | goto out; |
| 5448 | } |
| 5449 | |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 5450 | /* |
| 5451 | * ocfs2_add_new_xattr_bucket() will have updated |
| 5452 | * xs->bucket if it moved, but it will not have updated |
| 5453 | * any of the other search fields. Thus, we drop it and |
| 5454 | * re-search. Everything should be cached, so it'll be |
| 5455 | * quick. |
| 5456 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5457 | ocfs2_xattr_bucket_relse(xs->bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5458 | ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh, |
| 5459 | xi->name_index, |
| 5460 | xi->name, xs); |
| 5461 | if (ret && ret != -ENODATA) |
| 5462 | goto out; |
| 5463 | xs->not_found = ret; |
| 5464 | allocation = 1; |
| 5465 | goto try_again; |
| 5466 | } |
| 5467 | |
| 5468 | xattr_set: |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5469 | ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5470 | out: |
| 5471 | mlog_exit(ret); |
| 5472 | return ret; |
| 5473 | } |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 5474 | |
| 5475 | static int ocfs2_delete_xattr_in_bucket(struct inode *inode, |
| 5476 | struct ocfs2_xattr_bucket *bucket, |
| 5477 | void *para) |
| 5478 | { |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 5479 | int ret = 0, ref_credits; |
Joel Becker | 3e63294 | 2008-10-24 17:04:49 -0700 | [diff] [blame] | 5480 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 5481 | u16 i; |
| 5482 | struct ocfs2_xattr_entry *xe; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5483 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5484 | struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,}; |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 5485 | int credits = ocfs2_remove_extent_credits(osb->sb) + |
| 5486 | ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 5487 | struct ocfs2_xattr_value_root *xv; |
| 5488 | struct ocfs2_rm_xattr_bucket_para *args = |
| 5489 | (struct ocfs2_rm_xattr_bucket_para *)para; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5490 | |
| 5491 | ocfs2_init_dealloc_ctxt(&ctxt.dealloc); |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 5492 | |
| 5493 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { |
| 5494 | xe = &xh->xh_entries[i]; |
| 5495 | if (ocfs2_xattr_is_local(xe)) |
| 5496 | continue; |
| 5497 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 5498 | ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, |
| 5499 | i, &xv, NULL); |
| 5500 | |
| 5501 | ret = ocfs2_lock_xattr_remove_allocators(inode, xv, |
| 5502 | args->ref_ci, |
| 5503 | args->ref_root_bh, |
| 5504 | &ctxt.meta_ac, |
| 5505 | &ref_credits); |
| 5506 | |
| 5507 | ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits); |
Tao Ma | 88c3b06 | 2008-12-11 08:54:11 +0800 | [diff] [blame] | 5508 | if (IS_ERR(ctxt.handle)) { |
| 5509 | ret = PTR_ERR(ctxt.handle); |
| 5510 | mlog_errno(ret); |
| 5511 | break; |
| 5512 | } |
| 5513 | |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 5514 | ret = ocfs2_xattr_bucket_value_truncate(inode, bucket, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5515 | i, 0, &ctxt); |
Tao Ma | 88c3b06 | 2008-12-11 08:54:11 +0800 | [diff] [blame] | 5516 | |
| 5517 | ocfs2_commit_trans(osb, ctxt.handle); |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 5518 | if (ctxt.meta_ac) { |
| 5519 | ocfs2_free_alloc_context(ctxt.meta_ac); |
| 5520 | ctxt.meta_ac = NULL; |
| 5521 | } |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 5522 | if (ret) { |
| 5523 | mlog_errno(ret); |
| 5524 | break; |
| 5525 | } |
| 5526 | } |
| 5527 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 5528 | if (ctxt.meta_ac) |
| 5529 | ocfs2_free_alloc_context(ctxt.meta_ac); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5530 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 5531 | ocfs2_run_deallocs(osb, &ctxt.dealloc); |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 5532 | return ret; |
| 5533 | } |
| 5534 | |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 5535 | /* |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 5536 | * Whenever we modify a xattr value root in the bucket(e.g, CoW |
| 5537 | * or change the extent record flag), we need to recalculate |
| 5538 | * the metaecc for the whole bucket. So it is done here. |
| 5539 | * |
| 5540 | * Note: |
| 5541 | * We have to give the extra credits for the caller. |
| 5542 | */ |
| 5543 | static int ocfs2_xattr_bucket_post_refcount(struct inode *inode, |
| 5544 | handle_t *handle, |
| 5545 | void *para) |
| 5546 | { |
| 5547 | int ret; |
| 5548 | struct ocfs2_xattr_bucket *bucket = |
| 5549 | (struct ocfs2_xattr_bucket *)para; |
| 5550 | |
| 5551 | ret = ocfs2_xattr_bucket_journal_access(handle, bucket, |
| 5552 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 5553 | if (ret) { |
| 5554 | mlog_errno(ret); |
| 5555 | return ret; |
| 5556 | } |
| 5557 | |
| 5558 | ocfs2_xattr_bucket_journal_dirty(handle, bucket); |
| 5559 | |
| 5560 | return 0; |
| 5561 | } |
| 5562 | |
| 5563 | /* |
| 5564 | * Special action we need if the xattr value is refcounted. |
| 5565 | * |
| 5566 | * 1. If the xattr is refcounted, lock the tree. |
| 5567 | * 2. CoW the xattr if we are setting the new value and the value |
| 5568 | * will be stored outside. |
| 5569 | * 3. In other case, decrease_refcount will work for us, so just |
| 5570 | * lock the refcount tree, calculate the meta and credits is OK. |
| 5571 | * |
| 5572 | * We have to do CoW before ocfs2_init_xattr_set_ctxt since |
| 5573 | * currently CoW is a completed transaction, while this function |
| 5574 | * will also lock the allocators and let us deadlock. So we will |
| 5575 | * CoW the whole xattr value. |
| 5576 | */ |
| 5577 | static int ocfs2_prepare_refcount_xattr(struct inode *inode, |
| 5578 | struct ocfs2_dinode *di, |
| 5579 | struct ocfs2_xattr_info *xi, |
| 5580 | struct ocfs2_xattr_search *xis, |
| 5581 | struct ocfs2_xattr_search *xbs, |
| 5582 | struct ocfs2_refcount_tree **ref_tree, |
| 5583 | int *meta_add, |
| 5584 | int *credits) |
| 5585 | { |
| 5586 | int ret = 0; |
| 5587 | struct ocfs2_xattr_block *xb; |
| 5588 | struct ocfs2_xattr_entry *xe; |
| 5589 | char *base; |
| 5590 | u32 p_cluster, num_clusters; |
| 5591 | unsigned int ext_flags; |
| 5592 | int name_offset, name_len; |
| 5593 | struct ocfs2_xattr_value_buf vb; |
| 5594 | struct ocfs2_xattr_bucket *bucket = NULL; |
| 5595 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5596 | struct ocfs2_post_refcount refcount; |
| 5597 | struct ocfs2_post_refcount *p = NULL; |
| 5598 | struct buffer_head *ref_root_bh = NULL; |
| 5599 | |
| 5600 | if (!xis->not_found) { |
| 5601 | xe = xis->here; |
| 5602 | name_offset = le16_to_cpu(xe->xe_name_offset); |
| 5603 | name_len = OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 5604 | base = xis->base; |
| 5605 | vb.vb_bh = xis->inode_bh; |
| 5606 | vb.vb_access = ocfs2_journal_access_di; |
| 5607 | } else { |
| 5608 | int i, block_off = 0; |
| 5609 | xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data; |
| 5610 | xe = xbs->here; |
| 5611 | name_offset = le16_to_cpu(xe->xe_name_offset); |
| 5612 | name_len = OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 5613 | i = xbs->here - xbs->header->xh_entries; |
| 5614 | |
| 5615 | if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) { |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 5616 | ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 5617 | bucket_xh(xbs->bucket), |
| 5618 | i, &block_off, |
| 5619 | &name_offset); |
| 5620 | if (ret) { |
| 5621 | mlog_errno(ret); |
| 5622 | goto out; |
| 5623 | } |
| 5624 | base = bucket_block(xbs->bucket, block_off); |
| 5625 | vb.vb_bh = xbs->bucket->bu_bhs[block_off]; |
| 5626 | vb.vb_access = ocfs2_journal_access; |
| 5627 | |
| 5628 | if (ocfs2_meta_ecc(osb)) { |
| 5629 | /*create parameters for ocfs2_post_refcount. */ |
| 5630 | bucket = xbs->bucket; |
| 5631 | refcount.credits = bucket->bu_blocks; |
| 5632 | refcount.para = bucket; |
| 5633 | refcount.func = |
| 5634 | ocfs2_xattr_bucket_post_refcount; |
| 5635 | p = &refcount; |
| 5636 | } |
| 5637 | } else { |
| 5638 | base = xbs->base; |
| 5639 | vb.vb_bh = xbs->xattr_bh; |
| 5640 | vb.vb_access = ocfs2_journal_access_xb; |
| 5641 | } |
| 5642 | } |
| 5643 | |
| 5644 | if (ocfs2_xattr_is_local(xe)) |
| 5645 | goto out; |
| 5646 | |
| 5647 | vb.vb_xv = (struct ocfs2_xattr_value_root *) |
| 5648 | (base + name_offset + name_len); |
| 5649 | |
| 5650 | ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster, |
| 5651 | &num_clusters, &vb.vb_xv->xr_list, |
| 5652 | &ext_flags); |
| 5653 | if (ret) { |
| 5654 | mlog_errno(ret); |
| 5655 | goto out; |
| 5656 | } |
| 5657 | |
| 5658 | /* |
| 5659 | * We just need to check the 1st extent record, since we always |
| 5660 | * CoW the whole xattr. So there shouldn't be a xattr with |
| 5661 | * some REFCOUNT extent recs after the 1st one. |
| 5662 | */ |
| 5663 | if (!(ext_flags & OCFS2_EXT_REFCOUNTED)) |
| 5664 | goto out; |
| 5665 | |
| 5666 | ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc), |
| 5667 | 1, ref_tree, &ref_root_bh); |
| 5668 | if (ret) { |
| 5669 | mlog_errno(ret); |
| 5670 | goto out; |
| 5671 | } |
| 5672 | |
| 5673 | /* |
| 5674 | * If we are deleting the xattr or the new size will be stored inside, |
| 5675 | * cool, leave it there, the xattr truncate process will remove them |
| 5676 | * for us(it still needs the refcount tree lock and the meta, credits). |
| 5677 | * And the worse case is that every cluster truncate will split the |
| 5678 | * refcount tree, and make the original extent become 3. So we will need |
| 5679 | * 2 * cluster more extent recs at most. |
| 5680 | */ |
| 5681 | if (!xi->value || xi->value_len <= OCFS2_XATTR_INLINE_SIZE) { |
| 5682 | |
| 5683 | ret = ocfs2_refcounted_xattr_delete_need(inode, |
| 5684 | &(*ref_tree)->rf_ci, |
| 5685 | ref_root_bh, vb.vb_xv, |
| 5686 | meta_add, credits); |
| 5687 | if (ret) |
| 5688 | mlog_errno(ret); |
| 5689 | goto out; |
| 5690 | } |
| 5691 | |
| 5692 | ret = ocfs2_refcount_cow_xattr(inode, di, &vb, |
| 5693 | *ref_tree, ref_root_bh, 0, |
| 5694 | le32_to_cpu(vb.vb_xv->xr_clusters), p); |
| 5695 | if (ret) |
| 5696 | mlog_errno(ret); |
| 5697 | |
| 5698 | out: |
| 5699 | brelse(ref_root_bh); |
| 5700 | return ret; |
| 5701 | } |
| 5702 | |
| 5703 | /* |
Tao Ma | 0129241 | 2009-09-21 13:04:19 +0800 | [diff] [blame] | 5704 | * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root. |
| 5705 | * The physical clusters will be added to refcount tree. |
| 5706 | */ |
| 5707 | static int ocfs2_xattr_value_attach_refcount(struct inode *inode, |
| 5708 | struct ocfs2_xattr_value_root *xv, |
| 5709 | struct ocfs2_extent_tree *value_et, |
| 5710 | struct ocfs2_caching_info *ref_ci, |
| 5711 | struct buffer_head *ref_root_bh, |
| 5712 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 5713 | struct ocfs2_post_refcount *refcount) |
| 5714 | { |
| 5715 | int ret = 0; |
| 5716 | u32 clusters = le32_to_cpu(xv->xr_clusters); |
| 5717 | u32 cpos, p_cluster, num_clusters; |
| 5718 | struct ocfs2_extent_list *el = &xv->xr_list; |
| 5719 | unsigned int ext_flags; |
| 5720 | |
| 5721 | cpos = 0; |
| 5722 | while (cpos < clusters) { |
| 5723 | ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster, |
| 5724 | &num_clusters, el, &ext_flags); |
| 5725 | |
| 5726 | cpos += num_clusters; |
| 5727 | if ((ext_flags & OCFS2_EXT_REFCOUNTED)) |
| 5728 | continue; |
| 5729 | |
| 5730 | BUG_ON(!p_cluster); |
| 5731 | |
| 5732 | ret = ocfs2_add_refcount_flag(inode, value_et, |
| 5733 | ref_ci, ref_root_bh, |
| 5734 | cpos - num_clusters, |
| 5735 | p_cluster, num_clusters, |
| 5736 | dealloc, refcount); |
| 5737 | if (ret) { |
| 5738 | mlog_errno(ret); |
| 5739 | break; |
| 5740 | } |
| 5741 | } |
| 5742 | |
| 5743 | return ret; |
| 5744 | } |
| 5745 | |
| 5746 | /* |
| 5747 | * Given a normal ocfs2_xattr_header, refcount all the entries which |
| 5748 | * have value stored outside. |
| 5749 | * Used for xattrs stored in inode and ocfs2_xattr_block. |
| 5750 | */ |
| 5751 | static int ocfs2_xattr_attach_refcount_normal(struct inode *inode, |
| 5752 | struct ocfs2_xattr_value_buf *vb, |
| 5753 | struct ocfs2_xattr_header *header, |
| 5754 | struct ocfs2_caching_info *ref_ci, |
| 5755 | struct buffer_head *ref_root_bh, |
| 5756 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 5757 | { |
| 5758 | |
| 5759 | struct ocfs2_xattr_entry *xe; |
| 5760 | struct ocfs2_xattr_value_root *xv; |
| 5761 | struct ocfs2_extent_tree et; |
| 5762 | int i, ret = 0; |
| 5763 | |
| 5764 | for (i = 0; i < le16_to_cpu(header->xh_count); i++) { |
| 5765 | xe = &header->xh_entries[i]; |
| 5766 | |
| 5767 | if (ocfs2_xattr_is_local(xe)) |
| 5768 | continue; |
| 5769 | |
| 5770 | xv = (struct ocfs2_xattr_value_root *)((void *)header + |
| 5771 | le16_to_cpu(xe->xe_name_offset) + |
| 5772 | OCFS2_XATTR_SIZE(xe->xe_name_len)); |
| 5773 | |
| 5774 | vb->vb_xv = xv; |
| 5775 | ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb); |
| 5776 | |
| 5777 | ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et, |
| 5778 | ref_ci, ref_root_bh, |
| 5779 | dealloc, NULL); |
| 5780 | if (ret) { |
| 5781 | mlog_errno(ret); |
| 5782 | break; |
| 5783 | } |
| 5784 | } |
| 5785 | |
| 5786 | return ret; |
| 5787 | } |
| 5788 | |
| 5789 | static int ocfs2_xattr_inline_attach_refcount(struct inode *inode, |
| 5790 | struct buffer_head *fe_bh, |
| 5791 | struct ocfs2_caching_info *ref_ci, |
| 5792 | struct buffer_head *ref_root_bh, |
| 5793 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 5794 | { |
| 5795 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data; |
| 5796 | struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *) |
| 5797 | (fe_bh->b_data + inode->i_sb->s_blocksize - |
| 5798 | le16_to_cpu(di->i_xattr_inline_size)); |
| 5799 | struct ocfs2_xattr_value_buf vb = { |
| 5800 | .vb_bh = fe_bh, |
| 5801 | .vb_access = ocfs2_journal_access_di, |
| 5802 | }; |
| 5803 | |
| 5804 | return ocfs2_xattr_attach_refcount_normal(inode, &vb, header, |
| 5805 | ref_ci, ref_root_bh, dealloc); |
| 5806 | } |
| 5807 | |
| 5808 | struct ocfs2_xattr_tree_value_refcount_para { |
| 5809 | struct ocfs2_caching_info *ref_ci; |
| 5810 | struct buffer_head *ref_root_bh; |
| 5811 | struct ocfs2_cached_dealloc_ctxt *dealloc; |
| 5812 | }; |
| 5813 | |
| 5814 | static int ocfs2_get_xattr_tree_value_root(struct super_block *sb, |
| 5815 | struct ocfs2_xattr_bucket *bucket, |
| 5816 | int offset, |
| 5817 | struct ocfs2_xattr_value_root **xv, |
| 5818 | struct buffer_head **bh) |
| 5819 | { |
| 5820 | int ret, block_off, name_offset; |
| 5821 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
| 5822 | struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset]; |
| 5823 | void *base; |
| 5824 | |
| 5825 | ret = ocfs2_xattr_bucket_get_name_value(sb, |
| 5826 | bucket_xh(bucket), |
| 5827 | offset, |
| 5828 | &block_off, |
| 5829 | &name_offset); |
| 5830 | if (ret) { |
| 5831 | mlog_errno(ret); |
| 5832 | goto out; |
| 5833 | } |
| 5834 | |
| 5835 | base = bucket_block(bucket, block_off); |
| 5836 | |
| 5837 | *xv = (struct ocfs2_xattr_value_root *)(base + name_offset + |
| 5838 | OCFS2_XATTR_SIZE(xe->xe_name_len)); |
| 5839 | |
| 5840 | if (bh) |
| 5841 | *bh = bucket->bu_bhs[block_off]; |
| 5842 | out: |
| 5843 | return ret; |
| 5844 | } |
| 5845 | |
| 5846 | /* |
| 5847 | * For a given xattr bucket, refcount all the entries which |
| 5848 | * have value stored outside. |
| 5849 | */ |
| 5850 | static int ocfs2_xattr_bucket_value_refcount(struct inode *inode, |
| 5851 | struct ocfs2_xattr_bucket *bucket, |
| 5852 | void *para) |
| 5853 | { |
| 5854 | int i, ret = 0; |
| 5855 | struct ocfs2_extent_tree et; |
| 5856 | struct ocfs2_xattr_tree_value_refcount_para *ref = |
| 5857 | (struct ocfs2_xattr_tree_value_refcount_para *)para; |
| 5858 | struct ocfs2_xattr_header *xh = |
| 5859 | (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data; |
| 5860 | struct ocfs2_xattr_entry *xe; |
| 5861 | struct ocfs2_xattr_value_buf vb = { |
| 5862 | .vb_access = ocfs2_journal_access, |
| 5863 | }; |
| 5864 | struct ocfs2_post_refcount refcount = { |
| 5865 | .credits = bucket->bu_blocks, |
| 5866 | .para = bucket, |
| 5867 | .func = ocfs2_xattr_bucket_post_refcount, |
| 5868 | }; |
| 5869 | struct ocfs2_post_refcount *p = NULL; |
| 5870 | |
| 5871 | /* We only need post_refcount if we support metaecc. */ |
| 5872 | if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb))) |
| 5873 | p = &refcount; |
| 5874 | |
| 5875 | mlog(0, "refcount bucket %llu, count = %u\n", |
| 5876 | (unsigned long long)bucket_blkno(bucket), |
| 5877 | le16_to_cpu(xh->xh_count)); |
| 5878 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { |
| 5879 | xe = &xh->xh_entries[i]; |
| 5880 | |
| 5881 | if (ocfs2_xattr_is_local(xe)) |
| 5882 | continue; |
| 5883 | |
| 5884 | ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i, |
| 5885 | &vb.vb_xv, &vb.vb_bh); |
| 5886 | if (ret) { |
| 5887 | mlog_errno(ret); |
| 5888 | break; |
| 5889 | } |
| 5890 | |
| 5891 | ocfs2_init_xattr_value_extent_tree(&et, |
| 5892 | INODE_CACHE(inode), &vb); |
| 5893 | |
| 5894 | ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv, |
| 5895 | &et, ref->ref_ci, |
| 5896 | ref->ref_root_bh, |
| 5897 | ref->dealloc, p); |
| 5898 | if (ret) { |
| 5899 | mlog_errno(ret); |
| 5900 | break; |
| 5901 | } |
| 5902 | } |
| 5903 | |
| 5904 | return ret; |
| 5905 | |
| 5906 | } |
| 5907 | |
| 5908 | static int ocfs2_refcount_xattr_tree_rec(struct inode *inode, |
| 5909 | struct buffer_head *root_bh, |
| 5910 | u64 blkno, u32 cpos, u32 len, void *para) |
| 5911 | { |
| 5912 | return ocfs2_iterate_xattr_buckets(inode, blkno, len, |
| 5913 | ocfs2_xattr_bucket_value_refcount, |
| 5914 | para); |
| 5915 | } |
| 5916 | |
| 5917 | static int ocfs2_xattr_block_attach_refcount(struct inode *inode, |
| 5918 | struct buffer_head *blk_bh, |
| 5919 | struct ocfs2_caching_info *ref_ci, |
| 5920 | struct buffer_head *ref_root_bh, |
| 5921 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 5922 | { |
| 5923 | int ret = 0; |
| 5924 | struct ocfs2_xattr_block *xb = |
| 5925 | (struct ocfs2_xattr_block *)blk_bh->b_data; |
| 5926 | |
| 5927 | if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { |
| 5928 | struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header; |
| 5929 | struct ocfs2_xattr_value_buf vb = { |
| 5930 | .vb_bh = blk_bh, |
| 5931 | .vb_access = ocfs2_journal_access_xb, |
| 5932 | }; |
| 5933 | |
| 5934 | ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header, |
| 5935 | ref_ci, ref_root_bh, |
| 5936 | dealloc); |
| 5937 | } else { |
| 5938 | struct ocfs2_xattr_tree_value_refcount_para para = { |
| 5939 | .ref_ci = ref_ci, |
| 5940 | .ref_root_bh = ref_root_bh, |
| 5941 | .dealloc = dealloc, |
| 5942 | }; |
| 5943 | |
| 5944 | ret = ocfs2_iterate_xattr_index_block(inode, blk_bh, |
| 5945 | ocfs2_refcount_xattr_tree_rec, |
| 5946 | ¶); |
| 5947 | } |
| 5948 | |
| 5949 | return ret; |
| 5950 | } |
| 5951 | |
| 5952 | int ocfs2_xattr_attach_refcount_tree(struct inode *inode, |
| 5953 | struct buffer_head *fe_bh, |
| 5954 | struct ocfs2_caching_info *ref_ci, |
| 5955 | struct buffer_head *ref_root_bh, |
| 5956 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 5957 | { |
| 5958 | int ret = 0; |
| 5959 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 5960 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data; |
| 5961 | struct buffer_head *blk_bh = NULL; |
| 5962 | |
| 5963 | if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) { |
| 5964 | ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh, |
| 5965 | ref_ci, ref_root_bh, |
| 5966 | dealloc); |
| 5967 | if (ret) { |
| 5968 | mlog_errno(ret); |
| 5969 | goto out; |
| 5970 | } |
| 5971 | } |
| 5972 | |
| 5973 | if (!di->i_xattr_loc) |
| 5974 | goto out; |
| 5975 | |
| 5976 | ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc), |
| 5977 | &blk_bh); |
| 5978 | if (ret < 0) { |
| 5979 | mlog_errno(ret); |
| 5980 | goto out; |
| 5981 | } |
| 5982 | |
| 5983 | ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci, |
| 5984 | ref_root_bh, dealloc); |
| 5985 | if (ret) |
| 5986 | mlog_errno(ret); |
| 5987 | |
| 5988 | brelse(blk_bh); |
| 5989 | out: |
| 5990 | |
| 5991 | return ret; |
| 5992 | } |
| 5993 | |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 5994 | typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe); |
Tao Ma | 0129241 | 2009-09-21 13:04:19 +0800 | [diff] [blame] | 5995 | /* |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 5996 | * Store the information we need in xattr reflink. |
| 5997 | * old_bh and new_bh are inode bh for the old and new inode. |
| 5998 | */ |
| 5999 | struct ocfs2_xattr_reflink { |
| 6000 | struct inode *old_inode; |
| 6001 | struct inode *new_inode; |
| 6002 | struct buffer_head *old_bh; |
| 6003 | struct buffer_head *new_bh; |
| 6004 | struct ocfs2_caching_info *ref_ci; |
| 6005 | struct buffer_head *ref_root_bh; |
| 6006 | struct ocfs2_cached_dealloc_ctxt *dealloc; |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6007 | should_xattr_reflinked *xattr_reflinked; |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 6008 | }; |
| 6009 | |
| 6010 | /* |
| 6011 | * Given a xattr header and xe offset, |
| 6012 | * return the proper xv and the corresponding bh. |
| 6013 | * xattr in inode, block and xattr tree have different implementaions. |
| 6014 | */ |
| 6015 | typedef int (get_xattr_value_root)(struct super_block *sb, |
| 6016 | struct buffer_head *bh, |
| 6017 | struct ocfs2_xattr_header *xh, |
| 6018 | int offset, |
| 6019 | struct ocfs2_xattr_value_root **xv, |
| 6020 | struct buffer_head **ret_bh, |
| 6021 | void *para); |
| 6022 | |
| 6023 | /* |
| 6024 | * Calculate all the xattr value root metadata stored in this xattr header and |
| 6025 | * credits we need if we create them from the scratch. |
| 6026 | * We use get_xattr_value_root so that all types of xattr container can use it. |
| 6027 | */ |
| 6028 | static int ocfs2_value_metas_in_xattr_header(struct super_block *sb, |
| 6029 | struct buffer_head *bh, |
| 6030 | struct ocfs2_xattr_header *xh, |
| 6031 | int *metas, int *credits, |
| 6032 | int *num_recs, |
| 6033 | get_xattr_value_root *func, |
| 6034 | void *para) |
| 6035 | { |
| 6036 | int i, ret = 0; |
| 6037 | struct ocfs2_xattr_value_root *xv; |
| 6038 | struct ocfs2_xattr_entry *xe; |
| 6039 | |
| 6040 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { |
| 6041 | xe = &xh->xh_entries[i]; |
| 6042 | if (ocfs2_xattr_is_local(xe)) |
| 6043 | continue; |
| 6044 | |
| 6045 | ret = func(sb, bh, xh, i, &xv, NULL, para); |
| 6046 | if (ret) { |
| 6047 | mlog_errno(ret); |
| 6048 | break; |
| 6049 | } |
| 6050 | |
| 6051 | *metas += le16_to_cpu(xv->xr_list.l_tree_depth) * |
| 6052 | le16_to_cpu(xv->xr_list.l_next_free_rec); |
| 6053 | |
| 6054 | *credits += ocfs2_calc_extend_credits(sb, |
| 6055 | &def_xv.xv.xr_list, |
| 6056 | le32_to_cpu(xv->xr_clusters)); |
| 6057 | |
| 6058 | /* |
| 6059 | * If the value is a tree with depth > 1, We don't go deep |
| 6060 | * to the extent block, so just calculate a maximum record num. |
| 6061 | */ |
| 6062 | if (!xv->xr_list.l_tree_depth) |
Tao Ma | 8ff6af8 | 2009-12-23 14:31:15 +0800 | [diff] [blame] | 6063 | *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec); |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 6064 | else |
| 6065 | *num_recs += ocfs2_clusters_for_bytes(sb, |
| 6066 | XATTR_SIZE_MAX); |
| 6067 | } |
| 6068 | |
| 6069 | return ret; |
| 6070 | } |
| 6071 | |
| 6072 | /* Used by xattr inode and block to return the right xv and buffer_head. */ |
| 6073 | static int ocfs2_get_xattr_value_root(struct super_block *sb, |
| 6074 | struct buffer_head *bh, |
| 6075 | struct ocfs2_xattr_header *xh, |
| 6076 | int offset, |
| 6077 | struct ocfs2_xattr_value_root **xv, |
| 6078 | struct buffer_head **ret_bh, |
| 6079 | void *para) |
| 6080 | { |
| 6081 | struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset]; |
| 6082 | |
| 6083 | *xv = (struct ocfs2_xattr_value_root *)((void *)xh + |
| 6084 | le16_to_cpu(xe->xe_name_offset) + |
| 6085 | OCFS2_XATTR_SIZE(xe->xe_name_len)); |
| 6086 | |
| 6087 | if (ret_bh) |
| 6088 | *ret_bh = bh; |
| 6089 | |
| 6090 | return 0; |
| 6091 | } |
| 6092 | |
| 6093 | /* |
| 6094 | * Lock the meta_ac and caculate how much credits we need for reflink xattrs. |
| 6095 | * It is only used for inline xattr and xattr block. |
| 6096 | */ |
| 6097 | static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb, |
| 6098 | struct ocfs2_xattr_header *xh, |
| 6099 | struct buffer_head *ref_root_bh, |
| 6100 | int *credits, |
| 6101 | struct ocfs2_alloc_context **meta_ac) |
| 6102 | { |
| 6103 | int ret, meta_add = 0, num_recs = 0; |
| 6104 | struct ocfs2_refcount_block *rb = |
| 6105 | (struct ocfs2_refcount_block *)ref_root_bh->b_data; |
| 6106 | |
| 6107 | *credits = 0; |
| 6108 | |
| 6109 | ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh, |
| 6110 | &meta_add, credits, &num_recs, |
| 6111 | ocfs2_get_xattr_value_root, |
| 6112 | NULL); |
| 6113 | if (ret) { |
| 6114 | mlog_errno(ret); |
| 6115 | goto out; |
| 6116 | } |
| 6117 | |
| 6118 | /* |
| 6119 | * We need to add/modify num_recs in refcount tree, so just calculate |
| 6120 | * an approximate number we need for refcount tree change. |
| 6121 | * Sometimes we need to split the tree, and after split, half recs |
| 6122 | * will be moved to the new block, and a new block can only provide |
| 6123 | * half number of recs. So we multiple new blocks by 2. |
| 6124 | */ |
| 6125 | num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2; |
| 6126 | meta_add += num_recs; |
| 6127 | *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS; |
| 6128 | if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL) |
| 6129 | *credits += le16_to_cpu(rb->rf_list.l_tree_depth) * |
| 6130 | le16_to_cpu(rb->rf_list.l_next_free_rec) + 1; |
| 6131 | else |
| 6132 | *credits += 1; |
| 6133 | |
| 6134 | ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac); |
| 6135 | if (ret) |
| 6136 | mlog_errno(ret); |
| 6137 | |
| 6138 | out: |
| 6139 | return ret; |
| 6140 | } |
| 6141 | |
| 6142 | /* |
| 6143 | * Given a xattr header, reflink all the xattrs in this container. |
| 6144 | * It can be used for inode, block and bucket. |
| 6145 | * |
| 6146 | * NOTE: |
| 6147 | * Before we call this function, the caller has memcpy the xattr in |
| 6148 | * old_xh to the new_xh. |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6149 | * |
| 6150 | * If args.xattr_reflinked is set, call it to decide whether the xe should |
| 6151 | * be reflinked or not. If not, remove it from the new xattr header. |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 6152 | */ |
| 6153 | static int ocfs2_reflink_xattr_header(handle_t *handle, |
| 6154 | struct ocfs2_xattr_reflink *args, |
| 6155 | struct buffer_head *old_bh, |
| 6156 | struct ocfs2_xattr_header *xh, |
| 6157 | struct buffer_head *new_bh, |
| 6158 | struct ocfs2_xattr_header *new_xh, |
| 6159 | struct ocfs2_xattr_value_buf *vb, |
| 6160 | struct ocfs2_alloc_context *meta_ac, |
| 6161 | get_xattr_value_root *func, |
| 6162 | void *para) |
| 6163 | { |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6164 | int ret = 0, i, j; |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 6165 | struct super_block *sb = args->old_inode->i_sb; |
| 6166 | struct buffer_head *value_bh; |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6167 | struct ocfs2_xattr_entry *xe, *last; |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 6168 | struct ocfs2_xattr_value_root *xv, *new_xv; |
| 6169 | struct ocfs2_extent_tree data_et; |
| 6170 | u32 clusters, cpos, p_cluster, num_clusters; |
| 6171 | unsigned int ext_flags = 0; |
| 6172 | |
| 6173 | mlog(0, "reflink xattr in container %llu, count = %u\n", |
| 6174 | (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count)); |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6175 | |
| 6176 | last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)]; |
| 6177 | for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) { |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 6178 | xe = &xh->xh_entries[i]; |
| 6179 | |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6180 | if (args->xattr_reflinked && !args->xattr_reflinked(xe)) { |
| 6181 | xe = &new_xh->xh_entries[j]; |
| 6182 | |
| 6183 | le16_add_cpu(&new_xh->xh_count, -1); |
| 6184 | if (new_xh->xh_count) { |
| 6185 | memmove(xe, xe + 1, |
| 6186 | (void *)last - (void *)xe); |
| 6187 | memset(last, 0, |
| 6188 | sizeof(struct ocfs2_xattr_entry)); |
| 6189 | } |
| 6190 | |
| 6191 | /* |
| 6192 | * We don't want j to increase in the next round since |
| 6193 | * it is already moved ahead. |
| 6194 | */ |
| 6195 | j--; |
| 6196 | continue; |
| 6197 | } |
| 6198 | |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 6199 | if (ocfs2_xattr_is_local(xe)) |
| 6200 | continue; |
| 6201 | |
| 6202 | ret = func(sb, old_bh, xh, i, &xv, NULL, para); |
| 6203 | if (ret) { |
| 6204 | mlog_errno(ret); |
| 6205 | break; |
| 6206 | } |
| 6207 | |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6208 | ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para); |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 6209 | if (ret) { |
| 6210 | mlog_errno(ret); |
| 6211 | break; |
| 6212 | } |
| 6213 | |
| 6214 | /* |
| 6215 | * For the xattr which has l_tree_depth = 0, all the extent |
| 6216 | * recs have already be copied to the new xh with the |
| 6217 | * propriate OCFS2_EXT_REFCOUNTED flag we just need to |
| 6218 | * increase the refount count int the refcount tree. |
| 6219 | * |
| 6220 | * For the xattr which has l_tree_depth > 0, we need |
| 6221 | * to initialize it to the empty default value root, |
| 6222 | * and then insert the extents one by one. |
| 6223 | */ |
| 6224 | if (xv->xr_list.l_tree_depth) { |
| 6225 | memcpy(new_xv, &def_xv, sizeof(def_xv)); |
| 6226 | vb->vb_xv = new_xv; |
| 6227 | vb->vb_bh = value_bh; |
| 6228 | ocfs2_init_xattr_value_extent_tree(&data_et, |
| 6229 | INODE_CACHE(args->new_inode), vb); |
| 6230 | } |
| 6231 | |
| 6232 | clusters = le32_to_cpu(xv->xr_clusters); |
| 6233 | cpos = 0; |
| 6234 | while (cpos < clusters) { |
| 6235 | ret = ocfs2_xattr_get_clusters(args->old_inode, |
| 6236 | cpos, |
| 6237 | &p_cluster, |
| 6238 | &num_clusters, |
| 6239 | &xv->xr_list, |
| 6240 | &ext_flags); |
| 6241 | if (ret) { |
| 6242 | mlog_errno(ret); |
| 6243 | goto out; |
| 6244 | } |
| 6245 | |
| 6246 | BUG_ON(!p_cluster); |
| 6247 | |
| 6248 | if (xv->xr_list.l_tree_depth) { |
| 6249 | ret = ocfs2_insert_extent(handle, |
| 6250 | &data_et, cpos, |
| 6251 | ocfs2_clusters_to_blocks( |
| 6252 | args->old_inode->i_sb, |
| 6253 | p_cluster), |
| 6254 | num_clusters, ext_flags, |
| 6255 | meta_ac); |
| 6256 | if (ret) { |
| 6257 | mlog_errno(ret); |
| 6258 | goto out; |
| 6259 | } |
| 6260 | } |
| 6261 | |
| 6262 | ret = ocfs2_increase_refcount(handle, args->ref_ci, |
| 6263 | args->ref_root_bh, |
| 6264 | p_cluster, num_clusters, |
| 6265 | meta_ac, args->dealloc); |
| 6266 | if (ret) { |
| 6267 | mlog_errno(ret); |
| 6268 | goto out; |
| 6269 | } |
| 6270 | |
| 6271 | cpos += num_clusters; |
| 6272 | } |
| 6273 | } |
| 6274 | |
| 6275 | out: |
| 6276 | return ret; |
| 6277 | } |
| 6278 | |
| 6279 | static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args) |
| 6280 | { |
| 6281 | int ret = 0, credits = 0; |
| 6282 | handle_t *handle; |
| 6283 | struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb); |
| 6284 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data; |
| 6285 | int inline_size = le16_to_cpu(di->i_xattr_inline_size); |
| 6286 | int header_off = osb->sb->s_blocksize - inline_size; |
| 6287 | struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *) |
| 6288 | (args->old_bh->b_data + header_off); |
| 6289 | struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *) |
| 6290 | (args->new_bh->b_data + header_off); |
| 6291 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 6292 | struct ocfs2_inode_info *new_oi; |
| 6293 | struct ocfs2_dinode *new_di; |
| 6294 | struct ocfs2_xattr_value_buf vb = { |
| 6295 | .vb_bh = args->new_bh, |
| 6296 | .vb_access = ocfs2_journal_access_di, |
| 6297 | }; |
| 6298 | |
| 6299 | ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh, |
| 6300 | &credits, &meta_ac); |
| 6301 | if (ret) { |
| 6302 | mlog_errno(ret); |
| 6303 | goto out; |
| 6304 | } |
| 6305 | |
| 6306 | handle = ocfs2_start_trans(osb, credits); |
| 6307 | if (IS_ERR(handle)) { |
| 6308 | ret = PTR_ERR(handle); |
| 6309 | mlog_errno(ret); |
| 6310 | goto out; |
| 6311 | } |
| 6312 | |
| 6313 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode), |
| 6314 | args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE); |
| 6315 | if (ret) { |
| 6316 | mlog_errno(ret); |
| 6317 | goto out_commit; |
| 6318 | } |
| 6319 | |
| 6320 | memcpy(args->new_bh->b_data + header_off, |
| 6321 | args->old_bh->b_data + header_off, inline_size); |
| 6322 | |
| 6323 | new_di = (struct ocfs2_dinode *)args->new_bh->b_data; |
| 6324 | new_di->i_xattr_inline_size = cpu_to_le16(inline_size); |
| 6325 | |
| 6326 | ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh, |
| 6327 | args->new_bh, new_xh, &vb, meta_ac, |
| 6328 | ocfs2_get_xattr_value_root, NULL); |
| 6329 | if (ret) { |
| 6330 | mlog_errno(ret); |
| 6331 | goto out_commit; |
| 6332 | } |
| 6333 | |
| 6334 | new_oi = OCFS2_I(args->new_inode); |
| 6335 | spin_lock(&new_oi->ip_lock); |
| 6336 | new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL; |
| 6337 | new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features); |
| 6338 | spin_unlock(&new_oi->ip_lock); |
| 6339 | |
| 6340 | ocfs2_journal_dirty(handle, args->new_bh); |
| 6341 | |
| 6342 | out_commit: |
| 6343 | ocfs2_commit_trans(osb, handle); |
| 6344 | |
| 6345 | out: |
| 6346 | if (meta_ac) |
| 6347 | ocfs2_free_alloc_context(meta_ac); |
| 6348 | return ret; |
| 6349 | } |
| 6350 | |
| 6351 | static int ocfs2_create_empty_xattr_block(struct inode *inode, |
| 6352 | struct buffer_head *fe_bh, |
| 6353 | struct buffer_head **ret_bh, |
| 6354 | int indexed) |
| 6355 | { |
| 6356 | int ret; |
| 6357 | handle_t *handle; |
| 6358 | struct ocfs2_alloc_context *meta_ac; |
| 6359 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 6360 | |
| 6361 | ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac); |
| 6362 | if (ret < 0) { |
| 6363 | mlog_errno(ret); |
| 6364 | return ret; |
| 6365 | } |
| 6366 | |
| 6367 | handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS); |
| 6368 | if (IS_ERR(handle)) { |
| 6369 | ret = PTR_ERR(handle); |
| 6370 | mlog_errno(ret); |
| 6371 | goto out; |
| 6372 | } |
| 6373 | |
| 6374 | mlog(0, "create new xattr block for inode %llu, index = %d\n", |
| 6375 | (unsigned long long)fe_bh->b_blocknr, indexed); |
| 6376 | ret = ocfs2_create_xattr_block(handle, inode, fe_bh, |
| 6377 | meta_ac, ret_bh, indexed); |
| 6378 | if (ret) |
| 6379 | mlog_errno(ret); |
| 6380 | |
| 6381 | ocfs2_commit_trans(osb, handle); |
| 6382 | out: |
| 6383 | ocfs2_free_alloc_context(meta_ac); |
| 6384 | return ret; |
| 6385 | } |
| 6386 | |
| 6387 | static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args, |
| 6388 | struct buffer_head *blk_bh, |
| 6389 | struct buffer_head *new_blk_bh) |
| 6390 | { |
| 6391 | int ret = 0, credits = 0; |
| 6392 | handle_t *handle; |
| 6393 | struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode); |
| 6394 | struct ocfs2_dinode *new_di; |
| 6395 | struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb); |
| 6396 | int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); |
| 6397 | struct ocfs2_xattr_block *xb = |
| 6398 | (struct ocfs2_xattr_block *)blk_bh->b_data; |
| 6399 | struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header; |
| 6400 | struct ocfs2_xattr_block *new_xb = |
| 6401 | (struct ocfs2_xattr_block *)new_blk_bh->b_data; |
| 6402 | struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header; |
| 6403 | struct ocfs2_alloc_context *meta_ac; |
| 6404 | struct ocfs2_xattr_value_buf vb = { |
| 6405 | .vb_bh = new_blk_bh, |
| 6406 | .vb_access = ocfs2_journal_access_xb, |
| 6407 | }; |
| 6408 | |
| 6409 | ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh, |
| 6410 | &credits, &meta_ac); |
| 6411 | if (ret) { |
| 6412 | mlog_errno(ret); |
| 6413 | return ret; |
| 6414 | } |
| 6415 | |
| 6416 | /* One more credits in case we need to add xattr flags in new inode. */ |
| 6417 | handle = ocfs2_start_trans(osb, credits + 1); |
| 6418 | if (IS_ERR(handle)) { |
| 6419 | ret = PTR_ERR(handle); |
| 6420 | mlog_errno(ret); |
| 6421 | goto out; |
| 6422 | } |
| 6423 | |
| 6424 | if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) { |
| 6425 | ret = ocfs2_journal_access_di(handle, |
| 6426 | INODE_CACHE(args->new_inode), |
| 6427 | args->new_bh, |
| 6428 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 6429 | if (ret) { |
| 6430 | mlog_errno(ret); |
| 6431 | goto out_commit; |
| 6432 | } |
| 6433 | } |
| 6434 | |
| 6435 | ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode), |
| 6436 | new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE); |
| 6437 | if (ret) { |
| 6438 | mlog_errno(ret); |
| 6439 | goto out_commit; |
| 6440 | } |
| 6441 | |
| 6442 | memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off, |
| 6443 | osb->sb->s_blocksize - header_off); |
| 6444 | |
| 6445 | ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh, |
| 6446 | new_blk_bh, new_xh, &vb, meta_ac, |
| 6447 | ocfs2_get_xattr_value_root, NULL); |
| 6448 | if (ret) { |
| 6449 | mlog_errno(ret); |
| 6450 | goto out_commit; |
| 6451 | } |
| 6452 | |
| 6453 | ocfs2_journal_dirty(handle, new_blk_bh); |
| 6454 | |
| 6455 | if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) { |
| 6456 | new_di = (struct ocfs2_dinode *)args->new_bh->b_data; |
| 6457 | spin_lock(&new_oi->ip_lock); |
| 6458 | new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL; |
| 6459 | new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features); |
| 6460 | spin_unlock(&new_oi->ip_lock); |
| 6461 | |
| 6462 | ocfs2_journal_dirty(handle, args->new_bh); |
| 6463 | } |
| 6464 | |
| 6465 | out_commit: |
| 6466 | ocfs2_commit_trans(osb, handle); |
| 6467 | |
| 6468 | out: |
| 6469 | ocfs2_free_alloc_context(meta_ac); |
| 6470 | return ret; |
| 6471 | } |
| 6472 | |
| 6473 | struct ocfs2_reflink_xattr_tree_args { |
| 6474 | struct ocfs2_xattr_reflink *reflink; |
| 6475 | struct buffer_head *old_blk_bh; |
| 6476 | struct buffer_head *new_blk_bh; |
| 6477 | struct ocfs2_xattr_bucket *old_bucket; |
| 6478 | struct ocfs2_xattr_bucket *new_bucket; |
| 6479 | }; |
| 6480 | |
| 6481 | /* |
| 6482 | * NOTE: |
| 6483 | * We have to handle the case that both old bucket and new bucket |
| 6484 | * will call this function to get the right ret_bh. |
| 6485 | * So The caller must give us the right bh. |
| 6486 | */ |
| 6487 | static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb, |
| 6488 | struct buffer_head *bh, |
| 6489 | struct ocfs2_xattr_header *xh, |
| 6490 | int offset, |
| 6491 | struct ocfs2_xattr_value_root **xv, |
| 6492 | struct buffer_head **ret_bh, |
| 6493 | void *para) |
| 6494 | { |
| 6495 | struct ocfs2_reflink_xattr_tree_args *args = |
| 6496 | (struct ocfs2_reflink_xattr_tree_args *)para; |
| 6497 | struct ocfs2_xattr_bucket *bucket; |
| 6498 | |
| 6499 | if (bh == args->old_bucket->bu_bhs[0]) |
| 6500 | bucket = args->old_bucket; |
| 6501 | else |
| 6502 | bucket = args->new_bucket; |
| 6503 | |
| 6504 | return ocfs2_get_xattr_tree_value_root(sb, bucket, offset, |
| 6505 | xv, ret_bh); |
| 6506 | } |
| 6507 | |
| 6508 | struct ocfs2_value_tree_metas { |
| 6509 | int num_metas; |
| 6510 | int credits; |
| 6511 | int num_recs; |
| 6512 | }; |
| 6513 | |
| 6514 | static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb, |
| 6515 | struct buffer_head *bh, |
| 6516 | struct ocfs2_xattr_header *xh, |
| 6517 | int offset, |
| 6518 | struct ocfs2_xattr_value_root **xv, |
| 6519 | struct buffer_head **ret_bh, |
| 6520 | void *para) |
| 6521 | { |
| 6522 | struct ocfs2_xattr_bucket *bucket = |
| 6523 | (struct ocfs2_xattr_bucket *)para; |
| 6524 | |
| 6525 | return ocfs2_get_xattr_tree_value_root(sb, bucket, offset, |
| 6526 | xv, ret_bh); |
| 6527 | } |
| 6528 | |
| 6529 | static int ocfs2_calc_value_tree_metas(struct inode *inode, |
| 6530 | struct ocfs2_xattr_bucket *bucket, |
| 6531 | void *para) |
| 6532 | { |
| 6533 | struct ocfs2_value_tree_metas *metas = |
| 6534 | (struct ocfs2_value_tree_metas *)para; |
| 6535 | struct ocfs2_xattr_header *xh = |
| 6536 | (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data; |
| 6537 | |
| 6538 | /* Add the credits for this bucket first. */ |
| 6539 | metas->credits += bucket->bu_blocks; |
| 6540 | return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0], |
| 6541 | xh, &metas->num_metas, |
| 6542 | &metas->credits, &metas->num_recs, |
| 6543 | ocfs2_value_tree_metas_in_bucket, |
| 6544 | bucket); |
| 6545 | } |
| 6546 | |
| 6547 | /* |
| 6548 | * Given a xattr extent rec starting from blkno and having len clusters, |
| 6549 | * iterate all the buckets calculate how much metadata we need for reflinking |
| 6550 | * all the ocfs2_xattr_value_root and lock the allocators accordingly. |
| 6551 | */ |
| 6552 | static int ocfs2_lock_reflink_xattr_rec_allocators( |
| 6553 | struct ocfs2_reflink_xattr_tree_args *args, |
| 6554 | struct ocfs2_extent_tree *xt_et, |
| 6555 | u64 blkno, u32 len, int *credits, |
| 6556 | struct ocfs2_alloc_context **meta_ac, |
| 6557 | struct ocfs2_alloc_context **data_ac) |
| 6558 | { |
| 6559 | int ret, num_free_extents; |
| 6560 | struct ocfs2_value_tree_metas metas; |
| 6561 | struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb); |
| 6562 | struct ocfs2_refcount_block *rb; |
| 6563 | |
| 6564 | memset(&metas, 0, sizeof(metas)); |
| 6565 | |
| 6566 | ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len, |
| 6567 | ocfs2_calc_value_tree_metas, &metas); |
| 6568 | if (ret) { |
| 6569 | mlog_errno(ret); |
| 6570 | goto out; |
| 6571 | } |
| 6572 | |
| 6573 | *credits = metas.credits; |
| 6574 | |
| 6575 | /* |
| 6576 | * Calculate we need for refcount tree change. |
| 6577 | * |
| 6578 | * We need to add/modify num_recs in refcount tree, so just calculate |
| 6579 | * an approximate number we need for refcount tree change. |
| 6580 | * Sometimes we need to split the tree, and after split, half recs |
| 6581 | * will be moved to the new block, and a new block can only provide |
| 6582 | * half number of recs. So we multiple new blocks by 2. |
| 6583 | * In the end, we have to add credits for modifying the already |
| 6584 | * existed refcount block. |
| 6585 | */ |
| 6586 | rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data; |
| 6587 | metas.num_recs = |
| 6588 | (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) / |
| 6589 | ocfs2_refcount_recs_per_rb(osb->sb) * 2; |
| 6590 | metas.num_metas += metas.num_recs; |
| 6591 | *credits += metas.num_recs + |
| 6592 | metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS; |
| 6593 | if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL) |
| 6594 | *credits += le16_to_cpu(rb->rf_list.l_tree_depth) * |
| 6595 | le16_to_cpu(rb->rf_list.l_next_free_rec) + 1; |
| 6596 | else |
| 6597 | *credits += 1; |
| 6598 | |
| 6599 | /* count in the xattr tree change. */ |
| 6600 | num_free_extents = ocfs2_num_free_extents(osb, xt_et); |
| 6601 | if (num_free_extents < 0) { |
| 6602 | ret = num_free_extents; |
| 6603 | mlog_errno(ret); |
| 6604 | goto out; |
| 6605 | } |
| 6606 | |
| 6607 | if (num_free_extents < len) |
| 6608 | metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el); |
| 6609 | |
| 6610 | *credits += ocfs2_calc_extend_credits(osb->sb, |
| 6611 | xt_et->et_root_el, len); |
| 6612 | |
| 6613 | if (metas.num_metas) { |
| 6614 | ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas, |
| 6615 | meta_ac); |
| 6616 | if (ret) { |
| 6617 | mlog_errno(ret); |
| 6618 | goto out; |
| 6619 | } |
| 6620 | } |
| 6621 | |
| 6622 | if (len) { |
| 6623 | ret = ocfs2_reserve_clusters(osb, len, data_ac); |
| 6624 | if (ret) |
| 6625 | mlog_errno(ret); |
| 6626 | } |
| 6627 | out: |
| 6628 | if (ret) { |
| 6629 | if (*meta_ac) { |
| 6630 | ocfs2_free_alloc_context(*meta_ac); |
| 6631 | meta_ac = NULL; |
| 6632 | } |
| 6633 | } |
| 6634 | |
| 6635 | return ret; |
| 6636 | } |
| 6637 | |
| 6638 | static int ocfs2_reflink_xattr_buckets(handle_t *handle, |
| 6639 | u64 blkno, u64 new_blkno, u32 clusters, |
| 6640 | struct ocfs2_alloc_context *meta_ac, |
| 6641 | struct ocfs2_alloc_context *data_ac, |
| 6642 | struct ocfs2_reflink_xattr_tree_args *args) |
| 6643 | { |
| 6644 | int i, j, ret = 0; |
| 6645 | struct super_block *sb = args->reflink->old_inode->i_sb; |
| 6646 | u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb)); |
| 6647 | u32 num_buckets = clusters * bpc; |
| 6648 | int bpb = args->old_bucket->bu_blocks; |
| 6649 | struct ocfs2_xattr_value_buf vb = { |
| 6650 | .vb_access = ocfs2_journal_access, |
| 6651 | }; |
| 6652 | |
| 6653 | for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) { |
| 6654 | ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno); |
| 6655 | if (ret) { |
| 6656 | mlog_errno(ret); |
| 6657 | break; |
| 6658 | } |
| 6659 | |
| 6660 | ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno); |
| 6661 | if (ret) { |
| 6662 | mlog_errno(ret); |
| 6663 | break; |
| 6664 | } |
| 6665 | |
| 6666 | /* |
| 6667 | * The real bucket num in this series of blocks is stored |
| 6668 | * in the 1st bucket. |
| 6669 | */ |
| 6670 | if (i == 0) |
| 6671 | num_buckets = le16_to_cpu( |
| 6672 | bucket_xh(args->old_bucket)->xh_num_buckets); |
| 6673 | |
| 6674 | ret = ocfs2_xattr_bucket_journal_access(handle, |
| 6675 | args->new_bucket, |
| 6676 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 6677 | if (ret) { |
| 6678 | mlog_errno(ret); |
| 6679 | break; |
| 6680 | } |
| 6681 | |
| 6682 | for (j = 0; j < bpb; j++) |
| 6683 | memcpy(bucket_block(args->new_bucket, j), |
| 6684 | bucket_block(args->old_bucket, j), |
| 6685 | sb->s_blocksize); |
| 6686 | |
| 6687 | ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket); |
| 6688 | |
| 6689 | ret = ocfs2_reflink_xattr_header(handle, args->reflink, |
| 6690 | args->old_bucket->bu_bhs[0], |
| 6691 | bucket_xh(args->old_bucket), |
| 6692 | args->new_bucket->bu_bhs[0], |
| 6693 | bucket_xh(args->new_bucket), |
| 6694 | &vb, meta_ac, |
| 6695 | ocfs2_get_reflink_xattr_value_root, |
| 6696 | args); |
| 6697 | if (ret) { |
| 6698 | mlog_errno(ret); |
| 6699 | break; |
| 6700 | } |
| 6701 | |
| 6702 | /* |
| 6703 | * Re-access and dirty the bucket to calculate metaecc. |
| 6704 | * Because we may extend the transaction in reflink_xattr_header |
| 6705 | * which will let the already accessed block gone. |
| 6706 | */ |
| 6707 | ret = ocfs2_xattr_bucket_journal_access(handle, |
| 6708 | args->new_bucket, |
| 6709 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 6710 | if (ret) { |
| 6711 | mlog_errno(ret); |
| 6712 | break; |
| 6713 | } |
| 6714 | |
| 6715 | ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket); |
| 6716 | ocfs2_xattr_bucket_relse(args->old_bucket); |
| 6717 | ocfs2_xattr_bucket_relse(args->new_bucket); |
| 6718 | } |
| 6719 | |
| 6720 | ocfs2_xattr_bucket_relse(args->old_bucket); |
| 6721 | ocfs2_xattr_bucket_relse(args->new_bucket); |
| 6722 | return ret; |
| 6723 | } |
| 6724 | /* |
| 6725 | * Create the same xattr extent record in the new inode's xattr tree. |
| 6726 | */ |
| 6727 | static int ocfs2_reflink_xattr_rec(struct inode *inode, |
| 6728 | struct buffer_head *root_bh, |
| 6729 | u64 blkno, |
| 6730 | u32 cpos, |
| 6731 | u32 len, |
| 6732 | void *para) |
| 6733 | { |
| 6734 | int ret, credits = 0; |
| 6735 | u32 p_cluster, num_clusters; |
| 6736 | u64 new_blkno; |
| 6737 | handle_t *handle; |
| 6738 | struct ocfs2_reflink_xattr_tree_args *args = |
| 6739 | (struct ocfs2_reflink_xattr_tree_args *)para; |
| 6740 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 6741 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 6742 | struct ocfs2_alloc_context *data_ac = NULL; |
| 6743 | struct ocfs2_extent_tree et; |
| 6744 | |
| 6745 | ocfs2_init_xattr_tree_extent_tree(&et, |
| 6746 | INODE_CACHE(args->reflink->new_inode), |
| 6747 | args->new_blk_bh); |
| 6748 | |
| 6749 | ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno, |
| 6750 | len, &credits, |
| 6751 | &meta_ac, &data_ac); |
| 6752 | if (ret) { |
| 6753 | mlog_errno(ret); |
| 6754 | goto out; |
| 6755 | } |
| 6756 | |
| 6757 | handle = ocfs2_start_trans(osb, credits); |
| 6758 | if (IS_ERR(handle)) { |
| 6759 | ret = PTR_ERR(handle); |
| 6760 | mlog_errno(ret); |
| 6761 | goto out; |
| 6762 | } |
| 6763 | |
| 6764 | ret = ocfs2_claim_clusters(osb, handle, data_ac, |
| 6765 | len, &p_cluster, &num_clusters); |
| 6766 | if (ret) { |
| 6767 | mlog_errno(ret); |
| 6768 | goto out_commit; |
| 6769 | } |
| 6770 | |
| 6771 | new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster); |
| 6772 | |
| 6773 | mlog(0, "reflink xattr buckets %llu to %llu, len %u\n", |
| 6774 | (unsigned long long)blkno, (unsigned long long)new_blkno, len); |
| 6775 | ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len, |
| 6776 | meta_ac, data_ac, args); |
| 6777 | if (ret) { |
| 6778 | mlog_errno(ret); |
| 6779 | goto out_commit; |
| 6780 | } |
| 6781 | |
| 6782 | mlog(0, "insert new xattr extent rec start %llu len %u to %u\n", |
| 6783 | (unsigned long long)new_blkno, len, cpos); |
| 6784 | ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno, |
| 6785 | len, 0, meta_ac); |
| 6786 | if (ret) |
| 6787 | mlog_errno(ret); |
| 6788 | |
| 6789 | out_commit: |
| 6790 | ocfs2_commit_trans(osb, handle); |
| 6791 | |
| 6792 | out: |
| 6793 | if (meta_ac) |
| 6794 | ocfs2_free_alloc_context(meta_ac); |
| 6795 | if (data_ac) |
| 6796 | ocfs2_free_alloc_context(data_ac); |
| 6797 | return ret; |
| 6798 | } |
| 6799 | |
| 6800 | /* |
| 6801 | * Create reflinked xattr buckets. |
| 6802 | * We will add bucket one by one, and refcount all the xattrs in the bucket |
| 6803 | * if they are stored outside. |
| 6804 | */ |
| 6805 | static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args, |
| 6806 | struct buffer_head *blk_bh, |
| 6807 | struct buffer_head *new_blk_bh) |
| 6808 | { |
| 6809 | int ret; |
| 6810 | struct ocfs2_reflink_xattr_tree_args para; |
| 6811 | |
| 6812 | memset(¶, 0, sizeof(para)); |
| 6813 | para.reflink = args; |
| 6814 | para.old_blk_bh = blk_bh; |
| 6815 | para.new_blk_bh = new_blk_bh; |
| 6816 | |
| 6817 | para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode); |
| 6818 | if (!para.old_bucket) { |
| 6819 | mlog_errno(-ENOMEM); |
| 6820 | return -ENOMEM; |
| 6821 | } |
| 6822 | |
| 6823 | para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode); |
| 6824 | if (!para.new_bucket) { |
| 6825 | ret = -ENOMEM; |
| 6826 | mlog_errno(ret); |
| 6827 | goto out; |
| 6828 | } |
| 6829 | |
| 6830 | ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh, |
| 6831 | ocfs2_reflink_xattr_rec, |
| 6832 | ¶); |
| 6833 | if (ret) |
| 6834 | mlog_errno(ret); |
| 6835 | |
| 6836 | out: |
| 6837 | ocfs2_xattr_bucket_free(para.old_bucket); |
| 6838 | ocfs2_xattr_bucket_free(para.new_bucket); |
| 6839 | return ret; |
| 6840 | } |
| 6841 | |
| 6842 | static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args, |
| 6843 | struct buffer_head *blk_bh) |
| 6844 | { |
| 6845 | int ret, indexed = 0; |
| 6846 | struct buffer_head *new_blk_bh = NULL; |
| 6847 | struct ocfs2_xattr_block *xb = |
| 6848 | (struct ocfs2_xattr_block *)blk_bh->b_data; |
| 6849 | |
| 6850 | |
| 6851 | if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) |
| 6852 | indexed = 1; |
| 6853 | |
| 6854 | ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh, |
| 6855 | &new_blk_bh, indexed); |
| 6856 | if (ret) { |
| 6857 | mlog_errno(ret); |
| 6858 | goto out; |
| 6859 | } |
| 6860 | |
| 6861 | if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) |
| 6862 | ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh); |
| 6863 | else |
| 6864 | ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh); |
| 6865 | if (ret) |
| 6866 | mlog_errno(ret); |
| 6867 | |
| 6868 | out: |
| 6869 | brelse(new_blk_bh); |
| 6870 | return ret; |
| 6871 | } |
| 6872 | |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6873 | static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe) |
| 6874 | { |
| 6875 | int type = ocfs2_xattr_get_type(xe); |
| 6876 | |
| 6877 | return type != OCFS2_XATTR_INDEX_SECURITY && |
| 6878 | type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS && |
| 6879 | type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT; |
| 6880 | } |
| 6881 | |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 6882 | int ocfs2_reflink_xattrs(struct inode *old_inode, |
| 6883 | struct buffer_head *old_bh, |
| 6884 | struct inode *new_inode, |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6885 | struct buffer_head *new_bh, |
| 6886 | bool preserve_security) |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 6887 | { |
| 6888 | int ret; |
| 6889 | struct ocfs2_xattr_reflink args; |
| 6890 | struct ocfs2_inode_info *oi = OCFS2_I(old_inode); |
| 6891 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data; |
| 6892 | struct buffer_head *blk_bh = NULL; |
| 6893 | struct ocfs2_cached_dealloc_ctxt dealloc; |
| 6894 | struct ocfs2_refcount_tree *ref_tree; |
| 6895 | struct buffer_head *ref_root_bh = NULL; |
| 6896 | |
| 6897 | ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb), |
| 6898 | le64_to_cpu(di->i_refcount_loc), |
| 6899 | 1, &ref_tree, &ref_root_bh); |
| 6900 | if (ret) { |
| 6901 | mlog_errno(ret); |
| 6902 | goto out; |
| 6903 | } |
| 6904 | |
| 6905 | ocfs2_init_dealloc_ctxt(&dealloc); |
| 6906 | |
| 6907 | args.old_inode = old_inode; |
| 6908 | args.new_inode = new_inode; |
| 6909 | args.old_bh = old_bh; |
| 6910 | args.new_bh = new_bh; |
| 6911 | args.ref_ci = &ref_tree->rf_ci; |
| 6912 | args.ref_root_bh = ref_root_bh; |
| 6913 | args.dealloc = &dealloc; |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6914 | if (preserve_security) |
| 6915 | args.xattr_reflinked = NULL; |
| 6916 | else |
| 6917 | args.xattr_reflinked = ocfs2_reflink_xattr_no_security; |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 6918 | |
| 6919 | if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) { |
| 6920 | ret = ocfs2_reflink_xattr_inline(&args); |
| 6921 | if (ret) { |
| 6922 | mlog_errno(ret); |
| 6923 | goto out_unlock; |
| 6924 | } |
| 6925 | } |
| 6926 | |
| 6927 | if (!di->i_xattr_loc) |
| 6928 | goto out_unlock; |
| 6929 | |
| 6930 | ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc), |
| 6931 | &blk_bh); |
| 6932 | if (ret < 0) { |
| 6933 | mlog_errno(ret); |
| 6934 | goto out_unlock; |
| 6935 | } |
| 6936 | |
| 6937 | ret = ocfs2_reflink_xattr_in_block(&args, blk_bh); |
| 6938 | if (ret) |
| 6939 | mlog_errno(ret); |
| 6940 | |
| 6941 | brelse(blk_bh); |
| 6942 | |
| 6943 | out_unlock: |
| 6944 | ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb), |
| 6945 | ref_tree, 1); |
| 6946 | brelse(ref_root_bh); |
| 6947 | |
| 6948 | if (ocfs2_dealloc_has_cluster(&dealloc)) { |
| 6949 | ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1); |
| 6950 | ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc); |
| 6951 | } |
| 6952 | |
| 6953 | out: |
| 6954 | return ret; |
| 6955 | } |
| 6956 | |
| 6957 | /* |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6958 | * Initialize security and acl for a already created inode. |
| 6959 | * Used for reflink a non-preserve-security file. |
| 6960 | * |
| 6961 | * It uses common api like ocfs2_xattr_set, so the caller |
| 6962 | * must not hold any lock expect i_mutex. |
| 6963 | */ |
| 6964 | int ocfs2_init_security_and_acl(struct inode *dir, |
| 6965 | struct inode *inode) |
| 6966 | { |
| 6967 | int ret = 0; |
| 6968 | struct buffer_head *dir_bh = NULL; |
| 6969 | struct ocfs2_security_xattr_info si = { |
| 6970 | .enable = 1, |
| 6971 | }; |
| 6972 | |
| 6973 | ret = ocfs2_init_security_get(inode, dir, &si); |
| 6974 | if (!ret) { |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 6975 | ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, |
| 6976 | si.name, si.value, si.value_len, |
| 6977 | XATTR_CREATE); |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6978 | if (ret) { |
| 6979 | mlog_errno(ret); |
| 6980 | goto leave; |
| 6981 | } |
| 6982 | } else if (ret != -EOPNOTSUPP) { |
| 6983 | mlog_errno(ret); |
| 6984 | goto leave; |
| 6985 | } |
| 6986 | |
| 6987 | ret = ocfs2_inode_lock(dir, &dir_bh, 0); |
| 6988 | if (ret) { |
| 6989 | mlog_errno(ret); |
| 6990 | goto leave; |
| 6991 | } |
| 6992 | |
| 6993 | ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL); |
| 6994 | if (ret) |
| 6995 | mlog_errno(ret); |
| 6996 | |
| 6997 | ocfs2_inode_unlock(dir, 0); |
| 6998 | brelse(dir_bh); |
| 6999 | leave: |
| 7000 | return ret; |
| 7001 | } |
| 7002 | /* |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 7003 | * 'security' attributes support |
| 7004 | */ |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7005 | static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list, |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 7006 | size_t list_size, const char *name, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7007 | size_t name_len, int type) |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 7008 | { |
| 7009 | const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN; |
| 7010 | const size_t total_len = prefix_len + name_len + 1; |
| 7011 | |
| 7012 | if (list && total_len <= list_size) { |
| 7013 | memcpy(list, XATTR_SECURITY_PREFIX, prefix_len); |
| 7014 | memcpy(list + prefix_len, name, name_len); |
| 7015 | list[prefix_len + name_len] = '\0'; |
| 7016 | } |
| 7017 | return total_len; |
| 7018 | } |
| 7019 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7020 | static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name, |
| 7021 | void *buffer, size_t size, int type) |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 7022 | { |
| 7023 | if (strcmp(name, "") == 0) |
| 7024 | return -EINVAL; |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7025 | return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY, |
| 7026 | name, buffer, size); |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 7027 | } |
| 7028 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7029 | static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name, |
| 7030 | const void *value, size_t size, int flags, int type) |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 7031 | { |
| 7032 | if (strcmp(name, "") == 0) |
| 7033 | return -EINVAL; |
| 7034 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7035 | return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY, |
| 7036 | name, value, size, flags); |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 7037 | } |
| 7038 | |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 7039 | int ocfs2_init_security_get(struct inode *inode, |
| 7040 | struct inode *dir, |
| 7041 | struct ocfs2_security_xattr_info *si) |
| 7042 | { |
Tiger Yang | 38d59ef | 2008-12-17 10:22:56 +0800 | [diff] [blame] | 7043 | /* check whether ocfs2 support feature xattr */ |
| 7044 | if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb))) |
| 7045 | return -EOPNOTSUPP; |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 7046 | return security_inode_init_security(inode, dir, &si->name, &si->value, |
| 7047 | &si->value_len); |
| 7048 | } |
| 7049 | |
| 7050 | int ocfs2_init_security_set(handle_t *handle, |
| 7051 | struct inode *inode, |
| 7052 | struct buffer_head *di_bh, |
| 7053 | struct ocfs2_security_xattr_info *si, |
| 7054 | struct ocfs2_alloc_context *xattr_ac, |
| 7055 | struct ocfs2_alloc_context *data_ac) |
| 7056 | { |
| 7057 | return ocfs2_xattr_set_handle(handle, inode, di_bh, |
| 7058 | OCFS2_XATTR_INDEX_SECURITY, |
| 7059 | si->name, si->value, si->value_len, 0, |
| 7060 | xattr_ac, data_ac); |
| 7061 | } |
| 7062 | |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 7063 | struct xattr_handler ocfs2_xattr_security_handler = { |
| 7064 | .prefix = XATTR_SECURITY_PREFIX, |
| 7065 | .list = ocfs2_xattr_security_list, |
| 7066 | .get = ocfs2_xattr_security_get, |
| 7067 | .set = ocfs2_xattr_security_set, |
| 7068 | }; |
| 7069 | |
| 7070 | /* |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7071 | * 'trusted' attributes support |
| 7072 | */ |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7073 | static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list, |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7074 | size_t list_size, const char *name, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7075 | size_t name_len, int type) |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7076 | { |
Tiger Yang | ceb1eba | 2008-10-23 16:34:13 +0800 | [diff] [blame] | 7077 | const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN; |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7078 | const size_t total_len = prefix_len + name_len + 1; |
| 7079 | |
| 7080 | if (list && total_len <= list_size) { |
| 7081 | memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len); |
| 7082 | memcpy(list + prefix_len, name, name_len); |
| 7083 | list[prefix_len + name_len] = '\0'; |
| 7084 | } |
| 7085 | return total_len; |
| 7086 | } |
| 7087 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7088 | static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name, |
| 7089 | void *buffer, size_t size, int type) |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7090 | { |
| 7091 | if (strcmp(name, "") == 0) |
| 7092 | return -EINVAL; |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7093 | return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED, |
| 7094 | name, buffer, size); |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7095 | } |
| 7096 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7097 | static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name, |
| 7098 | const void *value, size_t size, int flags, int type) |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7099 | { |
| 7100 | if (strcmp(name, "") == 0) |
| 7101 | return -EINVAL; |
| 7102 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7103 | return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED, |
| 7104 | name, value, size, flags); |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7105 | } |
| 7106 | |
| 7107 | struct xattr_handler ocfs2_xattr_trusted_handler = { |
| 7108 | .prefix = XATTR_TRUSTED_PREFIX, |
| 7109 | .list = ocfs2_xattr_trusted_list, |
| 7110 | .get = ocfs2_xattr_trusted_get, |
| 7111 | .set = ocfs2_xattr_trusted_set, |
| 7112 | }; |
| 7113 | |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7114 | /* |
| 7115 | * 'user' attributes support |
| 7116 | */ |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7117 | static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list, |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7118 | size_t list_size, const char *name, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7119 | size_t name_len, int type) |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7120 | { |
Tiger Yang | ceb1eba | 2008-10-23 16:34:13 +0800 | [diff] [blame] | 7121 | const size_t prefix_len = XATTR_USER_PREFIX_LEN; |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7122 | const size_t total_len = prefix_len + name_len + 1; |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7123 | struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb); |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7124 | |
| 7125 | if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) |
| 7126 | return 0; |
| 7127 | |
| 7128 | if (list && total_len <= list_size) { |
| 7129 | memcpy(list, XATTR_USER_PREFIX, prefix_len); |
| 7130 | memcpy(list + prefix_len, name, name_len); |
| 7131 | list[prefix_len + name_len] = '\0'; |
| 7132 | } |
| 7133 | return total_len; |
| 7134 | } |
| 7135 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7136 | static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name, |
| 7137 | void *buffer, size_t size, int type) |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7138 | { |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7139 | struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb); |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7140 | |
| 7141 | if (strcmp(name, "") == 0) |
| 7142 | return -EINVAL; |
| 7143 | if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) |
| 7144 | return -EOPNOTSUPP; |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7145 | return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name, |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7146 | buffer, size); |
| 7147 | } |
| 7148 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7149 | static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name, |
| 7150 | const void *value, size_t size, int flags, int type) |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7151 | { |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7152 | struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb); |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7153 | |
| 7154 | if (strcmp(name, "") == 0) |
| 7155 | return -EINVAL; |
| 7156 | if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) |
| 7157 | return -EOPNOTSUPP; |
| 7158 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7159 | return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER, |
| 7160 | name, value, size, flags); |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7161 | } |
| 7162 | |
| 7163 | struct xattr_handler ocfs2_xattr_user_handler = { |
| 7164 | .prefix = XATTR_USER_PREFIX, |
| 7165 | .list = ocfs2_xattr_user_list, |
| 7166 | .get = ocfs2_xattr_user_get, |
| 7167 | .set = ocfs2_xattr_user_set, |
| 7168 | }; |