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 | |
Joel Becker | 11179f2 | 2009-08-14 16:07:44 -0700 | [diff] [blame^] | 140 | /* Operations on struct ocfs2_xa_entry */ |
| 141 | struct ocfs2_xa_loc; |
| 142 | struct ocfs2_xa_loc_operations { |
| 143 | /* |
| 144 | * Return a pointer to the appropriate buffer in loc->xl_storage |
| 145 | * at the given offset from loc->xl_header. |
| 146 | */ |
| 147 | void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset); |
| 148 | |
| 149 | /* |
| 150 | * Remove the name+value at this location. Do whatever is |
| 151 | * appropriate with the remaining name+value pairs. |
| 152 | */ |
| 153 | void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc); |
| 154 | }; |
| 155 | |
| 156 | /* |
| 157 | * Describes an xattr entry location. This is a memory structure |
| 158 | * tracking the on-disk structure. |
| 159 | */ |
| 160 | struct ocfs2_xa_loc { |
| 161 | /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */ |
| 162 | struct ocfs2_xattr_header *xl_header; |
| 163 | |
| 164 | /* Bytes from xl_header to the end of the storage */ |
| 165 | int xl_size; |
| 166 | |
| 167 | /* |
| 168 | * The ocfs2_xattr_entry this location describes. If this is |
| 169 | * NULL, this location describes the on-disk structure where it |
| 170 | * would have been. |
| 171 | */ |
| 172 | struct ocfs2_xattr_entry *xl_entry; |
| 173 | |
| 174 | /* |
| 175 | * Internal housekeeping |
| 176 | */ |
| 177 | |
| 178 | /* Buffer(s) containing this entry */ |
| 179 | void *xl_storage; |
| 180 | |
| 181 | /* Operations on the storage backing this location */ |
| 182 | const struct ocfs2_xa_loc_operations *xl_ops; |
| 183 | }; |
| 184 | |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 185 | static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb, |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 186 | struct ocfs2_xattr_header *xh, |
| 187 | int index, |
| 188 | int *block_off, |
| 189 | int *new_offset); |
| 190 | |
Joel Becker | 54f443f | 2008-10-20 18:43:07 -0700 | [diff] [blame] | 191 | static int ocfs2_xattr_block_find(struct inode *inode, |
| 192 | int name_index, |
| 193 | const char *name, |
| 194 | struct ocfs2_xattr_search *xs); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 195 | static int ocfs2_xattr_index_block_find(struct inode *inode, |
| 196 | struct buffer_head *root_bh, |
| 197 | int name_index, |
| 198 | const char *name, |
| 199 | struct ocfs2_xattr_search *xs); |
| 200 | |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 201 | static int ocfs2_xattr_tree_list_index_block(struct inode *inode, |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 202 | struct buffer_head *blk_bh, |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 203 | char *buffer, |
| 204 | size_t buffer_size); |
| 205 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 206 | static int ocfs2_xattr_create_index_block(struct inode *inode, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 207 | struct ocfs2_xattr_search *xs, |
| 208 | struct ocfs2_xattr_set_ctxt *ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 209 | |
| 210 | static int ocfs2_xattr_set_entry_index_block(struct inode *inode, |
| 211 | struct ocfs2_xattr_info *xi, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 212 | struct ocfs2_xattr_search *xs, |
| 213 | struct ocfs2_xattr_set_ctxt *ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 214 | |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 215 | typedef int (xattr_tree_rec_func)(struct inode *inode, |
| 216 | struct buffer_head *root_bh, |
| 217 | u64 blkno, u32 cpos, u32 len, void *para); |
| 218 | static int ocfs2_iterate_xattr_index_block(struct inode *inode, |
| 219 | struct buffer_head *root_bh, |
| 220 | xattr_tree_rec_func *rec_func, |
| 221 | void *para); |
| 222 | static int ocfs2_delete_xattr_in_bucket(struct inode *inode, |
| 223 | struct ocfs2_xattr_bucket *bucket, |
| 224 | void *para); |
| 225 | static int ocfs2_rm_xattr_cluster(struct inode *inode, |
| 226 | struct buffer_head *root_bh, |
| 227 | u64 blkno, |
| 228 | u32 cpos, |
| 229 | u32 len, |
| 230 | void *para); |
| 231 | |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 232 | static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle, |
| 233 | u64 src_blk, u64 last_blk, u64 to_blk, |
| 234 | unsigned int start_bucket, |
| 235 | u32 *first_hash); |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 236 | static int ocfs2_prepare_refcount_xattr(struct inode *inode, |
| 237 | struct ocfs2_dinode *di, |
| 238 | struct ocfs2_xattr_info *xi, |
| 239 | struct ocfs2_xattr_search *xis, |
| 240 | struct ocfs2_xattr_search *xbs, |
| 241 | struct ocfs2_refcount_tree **ref_tree, |
| 242 | int *meta_need, |
| 243 | int *credits); |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 244 | static int ocfs2_get_xattr_tree_value_root(struct super_block *sb, |
| 245 | struct ocfs2_xattr_bucket *bucket, |
| 246 | int offset, |
| 247 | struct ocfs2_xattr_value_root **xv, |
| 248 | struct buffer_head **bh); |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 249 | |
Tiger Yang | 0030e00 | 2008-10-23 16:33:33 +0800 | [diff] [blame] | 250 | static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb) |
| 251 | { |
| 252 | return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE; |
| 253 | } |
| 254 | |
| 255 | static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb) |
| 256 | { |
| 257 | return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits); |
| 258 | } |
| 259 | |
| 260 | static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb) |
| 261 | { |
| 262 | u16 len = sb->s_blocksize - |
| 263 | offsetof(struct ocfs2_xattr_header, xh_entries); |
| 264 | |
| 265 | return len / sizeof(struct ocfs2_xattr_entry); |
| 266 | } |
| 267 | |
Joel Becker | 9c7759a | 2008-10-24 16:21:03 -0700 | [diff] [blame] | 268 | #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr) |
Joel Becker | 51def39 | 2008-10-24 16:57:21 -0700 | [diff] [blame] | 269 | #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data) |
Joel Becker | 3e63294 | 2008-10-24 17:04:49 -0700 | [diff] [blame] | 270 | #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0)) |
Joel Becker | 9c7759a | 2008-10-24 16:21:03 -0700 | [diff] [blame] | 271 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 272 | static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode) |
Joel Becker | 6dde41d | 2008-10-24 17:16:48 -0700 | [diff] [blame] | 273 | { |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 274 | struct ocfs2_xattr_bucket *bucket; |
| 275 | int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Joel Becker | 6dde41d | 2008-10-24 17:16:48 -0700 | [diff] [blame] | 276 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 277 | BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET); |
| 278 | |
| 279 | bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS); |
| 280 | if (bucket) { |
| 281 | bucket->bu_inode = inode; |
| 282 | bucket->bu_blocks = blks; |
| 283 | } |
| 284 | |
| 285 | return bucket; |
| 286 | } |
| 287 | |
| 288 | static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket) |
| 289 | { |
| 290 | int i; |
| 291 | |
| 292 | for (i = 0; i < bucket->bu_blocks; i++) { |
Joel Becker | 6dde41d | 2008-10-24 17:16:48 -0700 | [diff] [blame] | 293 | brelse(bucket->bu_bhs[i]); |
| 294 | bucket->bu_bhs[i] = NULL; |
| 295 | } |
| 296 | } |
| 297 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 298 | static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket) |
| 299 | { |
| 300 | if (bucket) { |
| 301 | ocfs2_xattr_bucket_relse(bucket); |
| 302 | bucket->bu_inode = NULL; |
| 303 | kfree(bucket); |
| 304 | } |
| 305 | } |
| 306 | |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 307 | /* |
| 308 | * A bucket that has never been written to disk doesn't need to be |
| 309 | * read. We just need the buffer_heads. Don't call this for |
| 310 | * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes |
| 311 | * them fully. |
| 312 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 313 | static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket, |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 314 | u64 xb_blkno) |
| 315 | { |
| 316 | int i, rc = 0; |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 317 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 318 | for (i = 0; i < bucket->bu_blocks; i++) { |
| 319 | bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb, |
| 320 | xb_blkno + i); |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 321 | if (!bucket->bu_bhs[i]) { |
| 322 | rc = -EIO; |
| 323 | mlog_errno(rc); |
| 324 | break; |
| 325 | } |
| 326 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 327 | if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode), |
Tao Ma | 757055a | 2008-11-06 08:10:48 +0800 | [diff] [blame] | 328 | bucket->bu_bhs[i])) |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 329 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode), |
Tao Ma | 757055a | 2008-11-06 08:10:48 +0800 | [diff] [blame] | 330 | bucket->bu_bhs[i]); |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | if (rc) |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 334 | ocfs2_xattr_bucket_relse(bucket); |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 335 | return rc; |
| 336 | } |
| 337 | |
| 338 | /* Read the xattr bucket at xb_blkno */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 339 | static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket, |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 340 | u64 xb_blkno) |
| 341 | { |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 342 | int rc; |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 343 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 344 | rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno, |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 345 | bucket->bu_blocks, bucket->bu_bhs, 0, |
| 346 | NULL); |
Joel Becker | 4d0e214 | 2008-12-05 11:19:37 -0800 | [diff] [blame] | 347 | if (!rc) { |
Tao Ma | c8b9cf9 | 2009-02-24 17:40:26 -0800 | [diff] [blame] | 348 | spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock); |
Joel Becker | 4d0e214 | 2008-12-05 11:19:37 -0800 | [diff] [blame] | 349 | rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb, |
| 350 | bucket->bu_bhs, |
| 351 | bucket->bu_blocks, |
| 352 | &bucket_xh(bucket)->xh_check); |
Tao Ma | c8b9cf9 | 2009-02-24 17:40:26 -0800 | [diff] [blame] | 353 | spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock); |
Joel Becker | 4d0e214 | 2008-12-05 11:19:37 -0800 | [diff] [blame] | 354 | if (rc) |
| 355 | mlog_errno(rc); |
| 356 | } |
| 357 | |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 358 | if (rc) |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 359 | ocfs2_xattr_bucket_relse(bucket); |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 360 | return rc; |
| 361 | } |
| 362 | |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 363 | static int ocfs2_xattr_bucket_journal_access(handle_t *handle, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 364 | struct ocfs2_xattr_bucket *bucket, |
| 365 | int type) |
| 366 | { |
| 367 | int i, rc = 0; |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 368 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 369 | for (i = 0; i < bucket->bu_blocks; i++) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 370 | rc = ocfs2_journal_access(handle, |
| 371 | INODE_CACHE(bucket->bu_inode), |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 372 | bucket->bu_bhs[i], type); |
| 373 | if (rc) { |
| 374 | mlog_errno(rc); |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | return rc; |
| 380 | } |
| 381 | |
| 382 | static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 383 | struct ocfs2_xattr_bucket *bucket) |
| 384 | { |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 385 | int i; |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 386 | |
Tao Ma | c8b9cf9 | 2009-02-24 17:40:26 -0800 | [diff] [blame] | 387 | spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock); |
Joel Becker | 4d0e214 | 2008-12-05 11:19:37 -0800 | [diff] [blame] | 388 | ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb, |
| 389 | bucket->bu_bhs, bucket->bu_blocks, |
| 390 | &bucket_xh(bucket)->xh_check); |
Tao Ma | c8b9cf9 | 2009-02-24 17:40:26 -0800 | [diff] [blame] | 391 | spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock); |
Joel Becker | 4d0e214 | 2008-12-05 11:19:37 -0800 | [diff] [blame] | 392 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 393 | for (i = 0; i < bucket->bu_blocks; i++) |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 394 | ocfs2_journal_dirty(handle, bucket->bu_bhs[i]); |
| 395 | } |
| 396 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 397 | static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest, |
Joel Becker | 4980c6d | 2008-10-24 18:54:43 -0700 | [diff] [blame] | 398 | struct ocfs2_xattr_bucket *src) |
| 399 | { |
| 400 | int i; |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 401 | int blocksize = src->bu_inode->i_sb->s_blocksize; |
Joel Becker | 4980c6d | 2008-10-24 18:54:43 -0700 | [diff] [blame] | 402 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 403 | BUG_ON(dest->bu_blocks != src->bu_blocks); |
| 404 | BUG_ON(dest->bu_inode != src->bu_inode); |
| 405 | |
| 406 | for (i = 0; i < src->bu_blocks; i++) { |
Joel Becker | 4980c6d | 2008-10-24 18:54:43 -0700 | [diff] [blame] | 407 | memcpy(bucket_block(dest, i), bucket_block(src, i), |
| 408 | blocksize); |
| 409 | } |
| 410 | } |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 411 | |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 412 | static int ocfs2_validate_xattr_block(struct super_block *sb, |
| 413 | struct buffer_head *bh) |
| 414 | { |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 415 | int rc; |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 416 | struct ocfs2_xattr_block *xb = |
| 417 | (struct ocfs2_xattr_block *)bh->b_data; |
| 418 | |
| 419 | mlog(0, "Validating xattr block %llu\n", |
| 420 | (unsigned long long)bh->b_blocknr); |
| 421 | |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 422 | BUG_ON(!buffer_uptodate(bh)); |
| 423 | |
| 424 | /* |
| 425 | * If the ecc fails, we return the error but otherwise |
| 426 | * leave the filesystem running. We know any error is |
| 427 | * local to this block. |
| 428 | */ |
| 429 | rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check); |
| 430 | if (rc) |
| 431 | return rc; |
| 432 | |
| 433 | /* |
| 434 | * Errors after here are fatal |
| 435 | */ |
| 436 | |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 437 | if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) { |
| 438 | ocfs2_error(sb, |
| 439 | "Extended attribute block #%llu has bad " |
| 440 | "signature %.*s", |
| 441 | (unsigned long long)bh->b_blocknr, 7, |
| 442 | xb->xb_signature); |
| 443 | return -EINVAL; |
| 444 | } |
| 445 | |
| 446 | if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) { |
| 447 | ocfs2_error(sb, |
| 448 | "Extended attribute block #%llu has an " |
| 449 | "invalid xb_blkno of %llu", |
| 450 | (unsigned long long)bh->b_blocknr, |
| 451 | (unsigned long long)le64_to_cpu(xb->xb_blkno)); |
| 452 | return -EINVAL; |
| 453 | } |
| 454 | |
| 455 | if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) { |
| 456 | ocfs2_error(sb, |
| 457 | "Extended attribute block #%llu has an invalid " |
| 458 | "xb_fs_generation of #%u", |
| 459 | (unsigned long long)bh->b_blocknr, |
| 460 | le32_to_cpu(xb->xb_fs_generation)); |
| 461 | return -EINVAL; |
| 462 | } |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno, |
| 468 | struct buffer_head **bh) |
| 469 | { |
| 470 | int rc; |
| 471 | struct buffer_head *tmp = *bh; |
| 472 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 473 | rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp, |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 474 | ocfs2_validate_xattr_block); |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 475 | |
| 476 | /* If ocfs2_read_block() got us a new bh, pass it up. */ |
| 477 | if (!rc && !*bh) |
| 478 | *bh = tmp; |
| 479 | |
| 480 | return rc; |
| 481 | } |
| 482 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 483 | static inline const char *ocfs2_xattr_prefix(int name_index) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 484 | { |
| 485 | struct xattr_handler *handler = NULL; |
| 486 | |
| 487 | if (name_index > 0 && name_index < OCFS2_XATTR_MAX) |
| 488 | handler = ocfs2_xattr_handler_map[name_index]; |
| 489 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 490 | return handler ? handler->prefix : NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 491 | } |
| 492 | |
Mark Fasheh | 40daa16 | 2008-10-07 14:31:42 -0700 | [diff] [blame] | 493 | static u32 ocfs2_xattr_name_hash(struct inode *inode, |
Tao Ma | 2057e5c | 2008-10-09 23:06:13 +0800 | [diff] [blame] | 494 | const char *name, |
Mark Fasheh | 40daa16 | 2008-10-07 14:31:42 -0700 | [diff] [blame] | 495 | int name_len) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 496 | { |
| 497 | /* Get hash value of uuid from super block */ |
| 498 | u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash; |
| 499 | int i; |
| 500 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 501 | /* hash extended attribute name */ |
| 502 | for (i = 0; i < name_len; i++) { |
| 503 | hash = (hash << OCFS2_HASH_SHIFT) ^ |
| 504 | (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^ |
| 505 | *name++; |
| 506 | } |
| 507 | |
| 508 | return hash; |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | * ocfs2_xattr_hash_entry() |
| 513 | * |
| 514 | * Compute the hash of an extended attribute. |
| 515 | */ |
| 516 | static void ocfs2_xattr_hash_entry(struct inode *inode, |
| 517 | struct ocfs2_xattr_header *header, |
| 518 | struct ocfs2_xattr_entry *entry) |
| 519 | { |
| 520 | u32 hash = 0; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 521 | char *name = (char *)header + le16_to_cpu(entry->xe_name_offset); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 522 | |
Tao Ma | 2057e5c | 2008-10-09 23:06:13 +0800 | [diff] [blame] | 523 | hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 524 | entry->xe_name_hash = cpu_to_le32(hash); |
| 525 | |
| 526 | return; |
| 527 | } |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 528 | |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 529 | static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len) |
| 530 | { |
| 531 | int size = 0; |
| 532 | |
| 533 | if (value_len <= OCFS2_XATTR_INLINE_SIZE) |
| 534 | size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len); |
| 535 | else |
| 536 | size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE; |
| 537 | size += sizeof(struct ocfs2_xattr_entry); |
| 538 | |
| 539 | return size; |
| 540 | } |
| 541 | |
| 542 | int ocfs2_calc_security_init(struct inode *dir, |
| 543 | struct ocfs2_security_xattr_info *si, |
| 544 | int *want_clusters, |
| 545 | int *xattr_credits, |
| 546 | struct ocfs2_alloc_context **xattr_ac) |
| 547 | { |
| 548 | int ret = 0; |
| 549 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
| 550 | int s_size = ocfs2_xattr_entry_real_size(strlen(si->name), |
| 551 | si->value_len); |
| 552 | |
| 553 | /* |
| 554 | * The max space of security xattr taken inline is |
| 555 | * 256(name) + 80(value) + 16(entry) = 352 bytes, |
| 556 | * So reserve one metadata block for it is ok. |
| 557 | */ |
| 558 | if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE || |
| 559 | s_size > OCFS2_XATTR_FREE_IN_IBODY) { |
| 560 | ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac); |
| 561 | if (ret) { |
| 562 | mlog_errno(ret); |
| 563 | return ret; |
| 564 | } |
| 565 | *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS; |
| 566 | } |
| 567 | |
| 568 | /* reserve clusters for xattr value which will be set in B tree*/ |
Tiger Yang | 0e445b6 | 2008-12-09 16:42:51 +0800 | [diff] [blame] | 569 | if (si->value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 570 | int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb, |
| 571 | si->value_len); |
| 572 | |
| 573 | *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb, |
| 574 | new_clusters); |
| 575 | *want_clusters += new_clusters; |
| 576 | } |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 577 | return ret; |
| 578 | } |
| 579 | |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 580 | int ocfs2_calc_xattr_init(struct inode *dir, |
| 581 | struct buffer_head *dir_bh, |
| 582 | int mode, |
| 583 | struct ocfs2_security_xattr_info *si, |
| 584 | int *want_clusters, |
| 585 | int *xattr_credits, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 586 | int *want_meta) |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 587 | { |
| 588 | int ret = 0; |
| 589 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
Tiger Yang | 0e445b6 | 2008-12-09 16:42:51 +0800 | [diff] [blame] | 590 | int s_size = 0, a_size = 0, acl_len = 0, new_clusters; |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 591 | |
| 592 | if (si->enable) |
| 593 | s_size = ocfs2_xattr_entry_real_size(strlen(si->name), |
| 594 | si->value_len); |
| 595 | |
| 596 | if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) { |
| 597 | acl_len = ocfs2_xattr_get_nolock(dir, dir_bh, |
| 598 | OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT, |
| 599 | "", NULL, 0); |
| 600 | if (acl_len > 0) { |
| 601 | a_size = ocfs2_xattr_entry_real_size(0, acl_len); |
| 602 | if (S_ISDIR(mode)) |
| 603 | a_size <<= 1; |
| 604 | } else if (acl_len != 0 && acl_len != -ENODATA) { |
| 605 | mlog_errno(ret); |
| 606 | return ret; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | if (!(s_size + a_size)) |
| 611 | return ret; |
| 612 | |
| 613 | /* |
| 614 | * The max space of security xattr taken inline is |
| 615 | * 256(name) + 80(value) + 16(entry) = 352 bytes, |
| 616 | * The max space of acl xattr taken inline is |
| 617 | * 80(value) + 16(entry) * 2(if directory) = 192 bytes, |
| 618 | * when blocksize = 512, may reserve one more cluser for |
| 619 | * xattr bucket, otherwise reserve one metadata block |
| 620 | * for them is ok. |
Tiger Yang | 6c9fd1d | 2009-03-06 10:19:30 +0800 | [diff] [blame] | 621 | * If this is a new directory with inline data, |
| 622 | * we choose to reserve the entire inline area for |
| 623 | * directory contents and force an external xattr block. |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 624 | */ |
| 625 | if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE || |
Tiger Yang | 6c9fd1d | 2009-03-06 10:19:30 +0800 | [diff] [blame] | 626 | (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) || |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 627 | (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) { |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 628 | *want_meta = *want_meta + 1; |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 629 | *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS; |
| 630 | } |
| 631 | |
| 632 | if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE && |
| 633 | (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) { |
| 634 | *want_clusters += 1; |
| 635 | *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb); |
| 636 | } |
| 637 | |
Tiger Yang | 0e445b6 | 2008-12-09 16:42:51 +0800 | [diff] [blame] | 638 | /* |
| 639 | * reserve credits and clusters for xattrs which has large value |
| 640 | * and have to be set outside |
| 641 | */ |
| 642 | if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 643 | new_clusters = ocfs2_clusters_for_bytes(dir->i_sb, |
| 644 | si->value_len); |
| 645 | *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb, |
| 646 | new_clusters); |
| 647 | *want_clusters += new_clusters; |
| 648 | } |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 649 | if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL && |
| 650 | acl_len > OCFS2_XATTR_INLINE_SIZE) { |
Tiger Yang | 0e445b6 | 2008-12-09 16:42:51 +0800 | [diff] [blame] | 651 | /* for directory, it has DEFAULT and ACCESS two types of acls */ |
| 652 | new_clusters = (S_ISDIR(mode) ? 2 : 1) * |
| 653 | ocfs2_clusters_for_bytes(dir->i_sb, acl_len); |
| 654 | *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb, |
| 655 | new_clusters); |
| 656 | *want_clusters += new_clusters; |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | return ret; |
| 660 | } |
| 661 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 662 | static int ocfs2_xattr_extend_allocation(struct inode *inode, |
| 663 | u32 clusters_to_add, |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 664 | struct ocfs2_xattr_value_buf *vb, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 665 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 666 | { |
| 667 | int status = 0; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 668 | handle_t *handle = ctxt->handle; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 669 | enum ocfs2_alloc_restarted why; |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 670 | 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] | 671 | struct ocfs2_extent_tree et; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 672 | |
| 673 | mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add); |
| 674 | |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 675 | ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 676 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 677 | status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh, |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 678 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 679 | if (status < 0) { |
| 680 | mlog_errno(status); |
| 681 | goto leave; |
| 682 | } |
| 683 | |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 684 | prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters); |
Joel Becker | cbee7e1 | 2009-02-13 03:34:15 -0800 | [diff] [blame] | 685 | status = ocfs2_add_clusters_in_btree(handle, |
| 686 | &et, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 687 | &logical_start, |
| 688 | clusters_to_add, |
| 689 | 0, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 690 | ctxt->data_ac, |
| 691 | ctxt->meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 692 | &why); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 693 | if (status < 0) { |
| 694 | mlog_errno(status); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 695 | goto leave; |
| 696 | } |
| 697 | |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 698 | status = ocfs2_journal_dirty(handle, vb->vb_bh); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 699 | if (status < 0) { |
| 700 | mlog_errno(status); |
| 701 | goto leave; |
| 702 | } |
| 703 | |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 704 | 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] | 705 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 706 | /* |
| 707 | * We should have already allocated enough space before the transaction, |
| 708 | * so no need to restart. |
| 709 | */ |
| 710 | BUG_ON(why != RESTART_NONE || clusters_to_add); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 711 | |
| 712 | leave: |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 713 | |
| 714 | return status; |
| 715 | } |
| 716 | |
| 717 | static int __ocfs2_remove_xattr_range(struct inode *inode, |
Joel Becker | d72cc72 | 2008-12-09 14:30:41 -0800 | [diff] [blame] | 718 | struct ocfs2_xattr_value_buf *vb, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 719 | u32 cpos, u32 phys_cpos, u32 len, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 720 | unsigned int ext_flags, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 721 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 722 | { |
| 723 | int ret; |
| 724 | u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 725 | handle_t *handle = ctxt->handle; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 726 | struct ocfs2_extent_tree et; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 727 | |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 728 | ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 729 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 730 | ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh, |
Joel Becker | d72cc72 | 2008-12-09 14:30:41 -0800 | [diff] [blame] | 731 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 732 | if (ret) { |
| 733 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 734 | goto out; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 735 | } |
| 736 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 737 | ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 738 | &ctxt->dealloc); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 739 | if (ret) { |
| 740 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 741 | goto out; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 742 | } |
| 743 | |
Joel Becker | d72cc72 | 2008-12-09 14:30:41 -0800 | [diff] [blame] | 744 | le32_add_cpu(&vb->vb_xv->xr_clusters, -len); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 745 | |
Joel Becker | d72cc72 | 2008-12-09 14:30:41 -0800 | [diff] [blame] | 746 | ret = ocfs2_journal_dirty(handle, vb->vb_bh); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 747 | if (ret) { |
| 748 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 749 | goto out; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 750 | } |
| 751 | |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 752 | if (ext_flags & OCFS2_EXT_REFCOUNTED) |
| 753 | ret = ocfs2_decrease_refcount(inode, handle, |
| 754 | ocfs2_blocks_to_clusters(inode->i_sb, |
| 755 | phys_blkno), |
| 756 | len, ctxt->meta_ac, &ctxt->dealloc, 1); |
| 757 | else |
| 758 | ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc, |
| 759 | phys_blkno, len); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 760 | if (ret) |
| 761 | mlog_errno(ret); |
| 762 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 763 | out: |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | static int ocfs2_xattr_shrink_size(struct inode *inode, |
| 768 | u32 old_clusters, |
| 769 | u32 new_clusters, |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 770 | struct ocfs2_xattr_value_buf *vb, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 771 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 772 | { |
| 773 | int ret = 0; |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 774 | unsigned int ext_flags; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 775 | u32 trunc_len, cpos, phys_cpos, alloc_size; |
| 776 | u64 block; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 777 | |
| 778 | if (old_clusters <= new_clusters) |
| 779 | return 0; |
| 780 | |
| 781 | cpos = new_clusters; |
| 782 | trunc_len = old_clusters - new_clusters; |
| 783 | while (trunc_len) { |
| 784 | ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos, |
Joel Becker | d72cc72 | 2008-12-09 14:30:41 -0800 | [diff] [blame] | 785 | &alloc_size, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 786 | &vb->vb_xv->xr_list, &ext_flags); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 787 | if (ret) { |
| 788 | mlog_errno(ret); |
| 789 | goto out; |
| 790 | } |
| 791 | |
| 792 | if (alloc_size > trunc_len) |
| 793 | alloc_size = trunc_len; |
| 794 | |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 795 | ret = __ocfs2_remove_xattr_range(inode, vb, cpos, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 796 | phys_cpos, alloc_size, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 797 | ext_flags, ctxt); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 798 | if (ret) { |
| 799 | mlog_errno(ret); |
| 800 | goto out; |
| 801 | } |
| 802 | |
| 803 | block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 804 | ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), |
| 805 | block, alloc_size); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 806 | cpos += alloc_size; |
| 807 | trunc_len -= alloc_size; |
| 808 | } |
| 809 | |
| 810 | out: |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 811 | return ret; |
| 812 | } |
| 813 | |
| 814 | static int ocfs2_xattr_value_truncate(struct inode *inode, |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 815 | struct ocfs2_xattr_value_buf *vb, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 816 | int len, |
| 817 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 818 | { |
| 819 | int ret; |
| 820 | u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len); |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 821 | u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 822 | |
| 823 | if (new_clusters == old_clusters) |
| 824 | return 0; |
| 825 | |
| 826 | if (new_clusters > old_clusters) |
| 827 | ret = ocfs2_xattr_extend_allocation(inode, |
| 828 | new_clusters - old_clusters, |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 829 | vb, ctxt); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 830 | else |
| 831 | ret = ocfs2_xattr_shrink_size(inode, |
| 832 | old_clusters, new_clusters, |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 833 | vb, ctxt); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 834 | |
| 835 | return ret; |
| 836 | } |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 837 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 838 | static int ocfs2_xattr_list_entry(char *buffer, size_t size, |
| 839 | size_t *result, const char *prefix, |
| 840 | const char *name, int name_len) |
| 841 | { |
| 842 | char *p = buffer + *result; |
| 843 | int prefix_len = strlen(prefix); |
| 844 | int total_len = prefix_len + name_len + 1; |
| 845 | |
| 846 | *result += total_len; |
| 847 | |
| 848 | /* we are just looking for how big our buffer needs to be */ |
| 849 | if (!size) |
| 850 | return 0; |
| 851 | |
| 852 | if (*result > size) |
| 853 | return -ERANGE; |
| 854 | |
| 855 | memcpy(p, prefix, prefix_len); |
| 856 | memcpy(p + prefix_len, name, name_len); |
| 857 | p[prefix_len + name_len] = '\0'; |
| 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 862 | static int ocfs2_xattr_list_entries(struct inode *inode, |
| 863 | struct ocfs2_xattr_header *header, |
| 864 | char *buffer, size_t buffer_size) |
| 865 | { |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 866 | size_t result = 0; |
| 867 | int i, type, ret; |
| 868 | const char *prefix, *name; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 869 | |
| 870 | for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) { |
| 871 | struct ocfs2_xattr_entry *entry = &header->xh_entries[i]; |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 872 | type = ocfs2_xattr_get_type(entry); |
| 873 | prefix = ocfs2_xattr_prefix(type); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 874 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 875 | if (prefix) { |
| 876 | name = (const char *)header + |
| 877 | le16_to_cpu(entry->xe_name_offset); |
| 878 | |
| 879 | ret = ocfs2_xattr_list_entry(buffer, buffer_size, |
| 880 | &result, prefix, name, |
| 881 | entry->xe_name_len); |
| 882 | if (ret) |
| 883 | return ret; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 887 | return result; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 888 | } |
| 889 | |
Tao Ma | 8b2c0db | 2009-08-18 11:43:49 +0800 | [diff] [blame] | 890 | int ocfs2_has_inline_xattr_value_outside(struct inode *inode, |
| 891 | struct ocfs2_dinode *di) |
| 892 | { |
| 893 | struct ocfs2_xattr_header *xh; |
| 894 | int i; |
| 895 | |
| 896 | xh = (struct ocfs2_xattr_header *) |
| 897 | ((void *)di + inode->i_sb->s_blocksize - |
| 898 | le16_to_cpu(di->i_xattr_inline_size)); |
| 899 | |
| 900 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) |
| 901 | if (!ocfs2_xattr_is_local(&xh->xh_entries[i])) |
| 902 | return 1; |
| 903 | |
| 904 | return 0; |
| 905 | } |
| 906 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 907 | static int ocfs2_xattr_ibody_list(struct inode *inode, |
| 908 | struct ocfs2_dinode *di, |
| 909 | char *buffer, |
| 910 | size_t buffer_size) |
| 911 | { |
| 912 | struct ocfs2_xattr_header *header = NULL; |
| 913 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 914 | int ret = 0; |
| 915 | |
| 916 | if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) |
| 917 | return ret; |
| 918 | |
| 919 | header = (struct ocfs2_xattr_header *) |
| 920 | ((void *)di + inode->i_sb->s_blocksize - |
| 921 | le16_to_cpu(di->i_xattr_inline_size)); |
| 922 | |
| 923 | ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size); |
| 924 | |
| 925 | return ret; |
| 926 | } |
| 927 | |
| 928 | static int ocfs2_xattr_block_list(struct inode *inode, |
| 929 | struct ocfs2_dinode *di, |
| 930 | char *buffer, |
| 931 | size_t buffer_size) |
| 932 | { |
| 933 | struct buffer_head *blk_bh = NULL; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 934 | struct ocfs2_xattr_block *xb; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 935 | int ret = 0; |
| 936 | |
| 937 | if (!di->i_xattr_loc) |
| 938 | return ret; |
| 939 | |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 940 | ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc), |
| 941 | &blk_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 942 | if (ret < 0) { |
| 943 | mlog_errno(ret); |
| 944 | return ret; |
| 945 | } |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 946 | |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 947 | xb = (struct ocfs2_xattr_block *)blk_bh->b_data; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 948 | if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { |
| 949 | struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header; |
| 950 | ret = ocfs2_xattr_list_entries(inode, header, |
| 951 | buffer, buffer_size); |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 952 | } else |
| 953 | ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh, |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 954 | buffer, buffer_size); |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 955 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 956 | brelse(blk_bh); |
| 957 | |
| 958 | return ret; |
| 959 | } |
| 960 | |
| 961 | ssize_t ocfs2_listxattr(struct dentry *dentry, |
| 962 | char *buffer, |
| 963 | size_t size) |
| 964 | { |
| 965 | int ret = 0, i_ret = 0, b_ret = 0; |
| 966 | struct buffer_head *di_bh = NULL; |
| 967 | struct ocfs2_dinode *di = NULL; |
| 968 | struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode); |
| 969 | |
Tiger Yang | 8154da3 | 2008-08-18 17:11:46 +0800 | [diff] [blame] | 970 | if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb))) |
| 971 | return -EOPNOTSUPP; |
| 972 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 973 | if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) |
| 974 | return ret; |
| 975 | |
| 976 | ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0); |
| 977 | if (ret < 0) { |
| 978 | mlog_errno(ret); |
| 979 | return ret; |
| 980 | } |
| 981 | |
| 982 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 983 | |
| 984 | down_read(&oi->ip_xattr_sem); |
| 985 | i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size); |
| 986 | if (i_ret < 0) |
| 987 | b_ret = 0; |
| 988 | else { |
| 989 | if (buffer) { |
| 990 | buffer += i_ret; |
| 991 | size -= i_ret; |
| 992 | } |
| 993 | b_ret = ocfs2_xattr_block_list(dentry->d_inode, di, |
| 994 | buffer, size); |
| 995 | if (b_ret < 0) |
| 996 | i_ret = 0; |
| 997 | } |
| 998 | up_read(&oi->ip_xattr_sem); |
| 999 | ocfs2_inode_unlock(dentry->d_inode, 0); |
| 1000 | |
| 1001 | brelse(di_bh); |
| 1002 | |
| 1003 | return i_ret + b_ret; |
| 1004 | } |
| 1005 | |
| 1006 | static int ocfs2_xattr_find_entry(int name_index, |
| 1007 | const char *name, |
| 1008 | struct ocfs2_xattr_search *xs) |
| 1009 | { |
| 1010 | struct ocfs2_xattr_entry *entry; |
| 1011 | size_t name_len; |
| 1012 | int i, cmp = 1; |
| 1013 | |
| 1014 | if (name == NULL) |
| 1015 | return -EINVAL; |
| 1016 | |
| 1017 | name_len = strlen(name); |
| 1018 | entry = xs->here; |
| 1019 | for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) { |
| 1020 | cmp = name_index - ocfs2_xattr_get_type(entry); |
| 1021 | if (!cmp) |
| 1022 | cmp = name_len - entry->xe_name_len; |
| 1023 | if (!cmp) |
| 1024 | cmp = memcmp(name, (xs->base + |
| 1025 | le16_to_cpu(entry->xe_name_offset)), |
| 1026 | name_len); |
| 1027 | if (cmp == 0) |
| 1028 | break; |
| 1029 | entry += 1; |
| 1030 | } |
| 1031 | xs->here = entry; |
| 1032 | |
| 1033 | return cmp ? -ENODATA : 0; |
| 1034 | } |
| 1035 | |
| 1036 | static int ocfs2_xattr_get_value_outside(struct inode *inode, |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1037 | struct ocfs2_xattr_value_root *xv, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1038 | void *buffer, |
| 1039 | size_t len) |
| 1040 | { |
| 1041 | u32 cpos, p_cluster, num_clusters, bpc, clusters; |
| 1042 | u64 blkno; |
| 1043 | int i, ret = 0; |
| 1044 | size_t cplen, blocksize; |
| 1045 | struct buffer_head *bh = NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1046 | struct ocfs2_extent_list *el; |
| 1047 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1048 | el = &xv->xr_list; |
| 1049 | clusters = le32_to_cpu(xv->xr_clusters); |
| 1050 | bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); |
| 1051 | blocksize = inode->i_sb->s_blocksize; |
| 1052 | |
| 1053 | cpos = 0; |
| 1054 | while (cpos < clusters) { |
| 1055 | ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster, |
Tao Ma | 1061f9c | 2009-08-18 11:41:57 +0800 | [diff] [blame] | 1056 | &num_clusters, el, NULL); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1057 | if (ret) { |
| 1058 | mlog_errno(ret); |
| 1059 | goto out; |
| 1060 | } |
| 1061 | |
| 1062 | blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster); |
| 1063 | /* Copy ocfs2_xattr_value */ |
| 1064 | for (i = 0; i < num_clusters * bpc; i++, blkno++) { |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 1065 | ret = ocfs2_read_block(INODE_CACHE(inode), blkno, |
| 1066 | &bh, NULL); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1067 | if (ret) { |
| 1068 | mlog_errno(ret); |
| 1069 | goto out; |
| 1070 | } |
| 1071 | |
| 1072 | cplen = len >= blocksize ? blocksize : len; |
| 1073 | memcpy(buffer, bh->b_data, cplen); |
| 1074 | len -= cplen; |
| 1075 | buffer += cplen; |
| 1076 | |
| 1077 | brelse(bh); |
| 1078 | bh = NULL; |
| 1079 | if (len == 0) |
| 1080 | break; |
| 1081 | } |
| 1082 | cpos += num_clusters; |
| 1083 | } |
| 1084 | out: |
| 1085 | return ret; |
| 1086 | } |
| 1087 | |
| 1088 | static int ocfs2_xattr_ibody_get(struct inode *inode, |
| 1089 | int name_index, |
| 1090 | const char *name, |
| 1091 | void *buffer, |
| 1092 | size_t buffer_size, |
| 1093 | struct ocfs2_xattr_search *xs) |
| 1094 | { |
| 1095 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 1096 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1097 | struct ocfs2_xattr_value_root *xv; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1098 | size_t size; |
| 1099 | int ret = 0; |
| 1100 | |
| 1101 | if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) |
| 1102 | return -ENODATA; |
| 1103 | |
| 1104 | xs->end = (void *)di + inode->i_sb->s_blocksize; |
| 1105 | xs->header = (struct ocfs2_xattr_header *) |
| 1106 | (xs->end - le16_to_cpu(di->i_xattr_inline_size)); |
| 1107 | xs->base = (void *)xs->header; |
| 1108 | xs->here = xs->header->xh_entries; |
| 1109 | |
| 1110 | ret = ocfs2_xattr_find_entry(name_index, name, xs); |
| 1111 | if (ret) |
| 1112 | return ret; |
| 1113 | size = le64_to_cpu(xs->here->xe_value_size); |
| 1114 | if (buffer) { |
| 1115 | if (size > buffer_size) |
| 1116 | return -ERANGE; |
| 1117 | if (ocfs2_xattr_is_local(xs->here)) { |
| 1118 | memcpy(buffer, (void *)xs->base + |
| 1119 | le16_to_cpu(xs->here->xe_name_offset) + |
| 1120 | OCFS2_XATTR_SIZE(xs->here->xe_name_len), size); |
| 1121 | } else { |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1122 | xv = (struct ocfs2_xattr_value_root *) |
| 1123 | (xs->base + le16_to_cpu( |
| 1124 | xs->here->xe_name_offset) + |
| 1125 | OCFS2_XATTR_SIZE(xs->here->xe_name_len)); |
| 1126 | ret = ocfs2_xattr_get_value_outside(inode, xv, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1127 | buffer, size); |
| 1128 | if (ret < 0) { |
| 1129 | mlog_errno(ret); |
| 1130 | return ret; |
| 1131 | } |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | return size; |
| 1136 | } |
| 1137 | |
| 1138 | static int ocfs2_xattr_block_get(struct inode *inode, |
| 1139 | int name_index, |
| 1140 | const char *name, |
| 1141 | void *buffer, |
| 1142 | size_t buffer_size, |
| 1143 | struct ocfs2_xattr_search *xs) |
| 1144 | { |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1145 | struct ocfs2_xattr_block *xb; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1146 | struct ocfs2_xattr_value_root *xv; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1147 | size_t size; |
Subrata Modak | 44d8e4e | 2009-07-14 01:19:31 +0530 | [diff] [blame] | 1148 | int ret = -ENODATA, name_offset, name_len, i; |
| 1149 | int uninitialized_var(block_off); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1150 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 1151 | xs->bucket = ocfs2_xattr_bucket_new(inode); |
| 1152 | if (!xs->bucket) { |
| 1153 | ret = -ENOMEM; |
| 1154 | mlog_errno(ret); |
| 1155 | goto cleanup; |
| 1156 | } |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1157 | |
Joel Becker | 54f443f | 2008-10-20 18:43:07 -0700 | [diff] [blame] | 1158 | ret = ocfs2_xattr_block_find(inode, name_index, name, xs); |
| 1159 | if (ret) { |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1160 | mlog_errno(ret); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1161 | goto cleanup; |
| 1162 | } |
| 1163 | |
Tiger Yang | 6c1e183 | 2008-11-02 19:04:21 +0800 | [diff] [blame] | 1164 | if (xs->not_found) { |
| 1165 | ret = -ENODATA; |
| 1166 | goto cleanup; |
| 1167 | } |
| 1168 | |
Joel Becker | 54f443f | 2008-10-20 18:43:07 -0700 | [diff] [blame] | 1169 | xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1170 | size = le64_to_cpu(xs->here->xe_value_size); |
| 1171 | if (buffer) { |
| 1172 | ret = -ERANGE; |
| 1173 | if (size > buffer_size) |
| 1174 | goto cleanup; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1175 | |
| 1176 | name_offset = le16_to_cpu(xs->here->xe_name_offset); |
| 1177 | name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len); |
| 1178 | i = xs->here - xs->header->xh_entries; |
| 1179 | |
| 1180 | if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) { |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 1181 | ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 1182 | bucket_xh(xs->bucket), |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1183 | i, |
| 1184 | &block_off, |
| 1185 | &name_offset); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 1186 | xs->base = bucket_block(xs->bucket, block_off); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1187 | } |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1188 | if (ocfs2_xattr_is_local(xs->here)) { |
| 1189 | memcpy(buffer, (void *)xs->base + |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1190 | name_offset + name_len, size); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1191 | } else { |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1192 | xv = (struct ocfs2_xattr_value_root *) |
| 1193 | (xs->base + name_offset + name_len); |
| 1194 | ret = ocfs2_xattr_get_value_outside(inode, xv, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1195 | buffer, size); |
| 1196 | if (ret < 0) { |
| 1197 | mlog_errno(ret); |
| 1198 | goto cleanup; |
| 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | ret = size; |
| 1203 | cleanup: |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 1204 | ocfs2_xattr_bucket_free(xs->bucket); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1205 | |
Joel Becker | 54f443f | 2008-10-20 18:43:07 -0700 | [diff] [blame] | 1206 | brelse(xs->xattr_bh); |
| 1207 | xs->xattr_bh = NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1208 | return ret; |
| 1209 | } |
| 1210 | |
Tiger Yang | 4e3e9d0 | 2008-11-14 11:16:53 +0800 | [diff] [blame] | 1211 | int ocfs2_xattr_get_nolock(struct inode *inode, |
| 1212 | struct buffer_head *di_bh, |
Tiger Yang | 0030e00 | 2008-10-23 16:33:33 +0800 | [diff] [blame] | 1213 | int name_index, |
| 1214 | const char *name, |
| 1215 | void *buffer, |
| 1216 | size_t buffer_size) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1217 | { |
| 1218 | int ret; |
| 1219 | struct ocfs2_dinode *di = NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1220 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 1221 | struct ocfs2_xattr_search xis = { |
| 1222 | .not_found = -ENODATA, |
| 1223 | }; |
| 1224 | struct ocfs2_xattr_search xbs = { |
| 1225 | .not_found = -ENODATA, |
| 1226 | }; |
| 1227 | |
Tiger Yang | 8154da3 | 2008-08-18 17:11:46 +0800 | [diff] [blame] | 1228 | if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) |
| 1229 | return -EOPNOTSUPP; |
| 1230 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1231 | if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) |
| 1232 | ret = -ENODATA; |
| 1233 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1234 | xis.inode_bh = xbs.inode_bh = di_bh; |
| 1235 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 1236 | |
| 1237 | down_read(&oi->ip_xattr_sem); |
| 1238 | ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer, |
| 1239 | buffer_size, &xis); |
Tiger Yang | 6c1e183 | 2008-11-02 19:04:21 +0800 | [diff] [blame] | 1240 | if (ret == -ENODATA && di->i_xattr_loc) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1241 | ret = ocfs2_xattr_block_get(inode, name_index, name, buffer, |
| 1242 | buffer_size, &xbs); |
| 1243 | up_read(&oi->ip_xattr_sem); |
Tiger Yang | 4e3e9d0 | 2008-11-14 11:16:53 +0800 | [diff] [blame] | 1244 | |
| 1245 | return ret; |
| 1246 | } |
| 1247 | |
| 1248 | /* ocfs2_xattr_get() |
| 1249 | * |
| 1250 | * Copy an extended attribute into the buffer provided. |
| 1251 | * Buffer is NULL to compute the size of buffer required. |
| 1252 | */ |
| 1253 | static int ocfs2_xattr_get(struct inode *inode, |
| 1254 | int name_index, |
| 1255 | const char *name, |
| 1256 | void *buffer, |
| 1257 | size_t buffer_size) |
| 1258 | { |
| 1259 | int ret; |
| 1260 | struct buffer_head *di_bh = NULL; |
| 1261 | |
| 1262 | ret = ocfs2_inode_lock(inode, &di_bh, 0); |
| 1263 | if (ret < 0) { |
| 1264 | mlog_errno(ret); |
| 1265 | return ret; |
| 1266 | } |
| 1267 | ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index, |
| 1268 | name, buffer, buffer_size); |
| 1269 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1270 | ocfs2_inode_unlock(inode, 0); |
| 1271 | |
| 1272 | brelse(di_bh); |
| 1273 | |
| 1274 | return ret; |
| 1275 | } |
| 1276 | |
| 1277 | static int __ocfs2_xattr_set_value_outside(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1278 | handle_t *handle, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 1279 | struct ocfs2_xattr_value_buf *vb, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1280 | const void *value, |
| 1281 | int value_len) |
| 1282 | { |
Tao Ma | 71d548a | 2008-12-05 06:20:54 +0800 | [diff] [blame] | 1283 | int ret = 0, i, cp_len; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1284 | u16 blocksize = inode->i_sb->s_blocksize; |
| 1285 | u32 p_cluster, num_clusters; |
| 1286 | u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); |
| 1287 | u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len); |
| 1288 | u64 blkno; |
| 1289 | struct buffer_head *bh = NULL; |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 1290 | unsigned int ext_flags; |
| 1291 | struct ocfs2_xattr_value_root *xv = vb->vb_xv; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1292 | |
| 1293 | BUG_ON(clusters > le32_to_cpu(xv->xr_clusters)); |
| 1294 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1295 | while (cpos < clusters) { |
| 1296 | ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster, |
Tao Ma | 1061f9c | 2009-08-18 11:41:57 +0800 | [diff] [blame] | 1297 | &num_clusters, &xv->xr_list, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 1298 | &ext_flags); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1299 | if (ret) { |
| 1300 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1301 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1302 | } |
| 1303 | |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 1304 | BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED); |
| 1305 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1306 | blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster); |
| 1307 | |
| 1308 | for (i = 0; i < num_clusters * bpc; i++, blkno++) { |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 1309 | ret = ocfs2_read_block(INODE_CACHE(inode), blkno, |
| 1310 | &bh, NULL); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1311 | if (ret) { |
| 1312 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1313 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | ret = ocfs2_journal_access(handle, |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1317 | INODE_CACHE(inode), |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1318 | bh, |
| 1319 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1320 | if (ret < 0) { |
| 1321 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1322 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | cp_len = value_len > blocksize ? blocksize : value_len; |
| 1326 | memcpy(bh->b_data, value, cp_len); |
| 1327 | value_len -= cp_len; |
| 1328 | value += cp_len; |
| 1329 | if (cp_len < blocksize) |
| 1330 | memset(bh->b_data + cp_len, 0, |
| 1331 | blocksize - cp_len); |
| 1332 | |
| 1333 | ret = ocfs2_journal_dirty(handle, bh); |
| 1334 | if (ret < 0) { |
| 1335 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1336 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1337 | } |
| 1338 | brelse(bh); |
| 1339 | bh = NULL; |
| 1340 | |
| 1341 | /* |
| 1342 | * XXX: do we need to empty all the following |
| 1343 | * blocks in this cluster? |
| 1344 | */ |
| 1345 | if (!value_len) |
| 1346 | break; |
| 1347 | } |
| 1348 | cpos += num_clusters; |
| 1349 | } |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1350 | out: |
| 1351 | brelse(bh); |
| 1352 | |
| 1353 | return ret; |
| 1354 | } |
| 1355 | |
| 1356 | static int ocfs2_xattr_cleanup(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1357 | handle_t *handle, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1358 | struct ocfs2_xattr_info *xi, |
| 1359 | struct ocfs2_xattr_search *xs, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1360 | struct ocfs2_xattr_value_buf *vb, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1361 | size_t offs) |
| 1362 | { |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1363 | int ret = 0; |
| 1364 | size_t name_len = strlen(xi->name); |
| 1365 | void *val = xs->base + offs; |
| 1366 | size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE; |
| 1367 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1368 | ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1369 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1370 | if (ret) { |
| 1371 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1372 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1373 | } |
| 1374 | /* Decrease xattr count */ |
| 1375 | le16_add_cpu(&xs->header->xh_count, -1); |
| 1376 | /* Remove the xattr entry and tree root which has already be set*/ |
| 1377 | memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry)); |
| 1378 | memset(val, 0, size); |
| 1379 | |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1380 | ret = ocfs2_journal_dirty(handle, vb->vb_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1381 | if (ret < 0) |
| 1382 | mlog_errno(ret); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1383 | out: |
| 1384 | return ret; |
| 1385 | } |
| 1386 | |
| 1387 | static int ocfs2_xattr_update_entry(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1388 | handle_t *handle, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1389 | struct ocfs2_xattr_info *xi, |
| 1390 | struct ocfs2_xattr_search *xs, |
Joel Becker | 0c748e9 | 2008-12-09 15:46:15 -0800 | [diff] [blame] | 1391 | struct ocfs2_xattr_value_buf *vb, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1392 | size_t offs) |
| 1393 | { |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1394 | int ret; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1395 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1396 | ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh, |
Joel Becker | 0c748e9 | 2008-12-09 15:46:15 -0800 | [diff] [blame] | 1397 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1398 | if (ret) { |
| 1399 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1400 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | xs->here->xe_name_offset = cpu_to_le16(offs); |
| 1404 | xs->here->xe_value_size = cpu_to_le64(xi->value_len); |
| 1405 | if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE) |
| 1406 | ocfs2_xattr_set_local(xs->here, 1); |
| 1407 | else |
| 1408 | ocfs2_xattr_set_local(xs->here, 0); |
| 1409 | ocfs2_xattr_hash_entry(inode, xs->header, xs->here); |
| 1410 | |
Joel Becker | 0c748e9 | 2008-12-09 15:46:15 -0800 | [diff] [blame] | 1411 | ret = ocfs2_journal_dirty(handle, vb->vb_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1412 | if (ret < 0) |
| 1413 | mlog_errno(ret); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1414 | out: |
| 1415 | return ret; |
| 1416 | } |
| 1417 | |
| 1418 | /* |
| 1419 | * ocfs2_xattr_set_value_outside() |
| 1420 | * |
| 1421 | * Set large size value in B tree. |
| 1422 | */ |
| 1423 | static int ocfs2_xattr_set_value_outside(struct inode *inode, |
| 1424 | struct ocfs2_xattr_info *xi, |
| 1425 | struct ocfs2_xattr_search *xs, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 1426 | struct ocfs2_xattr_set_ctxt *ctxt, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1427 | struct ocfs2_xattr_value_buf *vb, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1428 | size_t offs) |
| 1429 | { |
| 1430 | size_t name_len = strlen(xi->name); |
| 1431 | void *val = xs->base + offs; |
| 1432 | struct ocfs2_xattr_value_root *xv = NULL; |
| 1433 | size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE; |
| 1434 | int ret = 0; |
| 1435 | |
| 1436 | memset(val, 0, size); |
| 1437 | memcpy(val, xi->name, name_len); |
| 1438 | xv = (struct ocfs2_xattr_value_root *) |
| 1439 | (val + OCFS2_XATTR_SIZE(name_len)); |
| 1440 | xv->xr_clusters = 0; |
| 1441 | xv->xr_last_eb_blk = 0; |
| 1442 | xv->xr_list.l_tree_depth = 0; |
| 1443 | xv->xr_list.l_count = cpu_to_le16(1); |
| 1444 | xv->xr_list.l_next_free_rec = 0; |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1445 | vb->vb_xv = xv; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1446 | |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1447 | ret = ocfs2_xattr_value_truncate(inode, vb, xi->value_len, ctxt); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1448 | if (ret < 0) { |
| 1449 | mlog_errno(ret); |
| 1450 | return ret; |
| 1451 | } |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1452 | ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1453 | if (ret < 0) { |
| 1454 | mlog_errno(ret); |
| 1455 | return ret; |
| 1456 | } |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 1457 | ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1458 | xi->value, xi->value_len); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1459 | if (ret < 0) |
| 1460 | mlog_errno(ret); |
| 1461 | |
| 1462 | return ret; |
| 1463 | } |
| 1464 | |
| 1465 | /* |
Joel Becker | 11179f2 | 2009-08-14 16:07:44 -0700 | [diff] [blame^] | 1466 | * Wipe the name+value pair and allow the storage to reclaim it. This |
| 1467 | * must be followed by either removal of the entry or a call to |
| 1468 | * ocfs2_xa_add_namevalue(). |
| 1469 | */ |
| 1470 | static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc) |
| 1471 | { |
| 1472 | loc->xl_ops->xlo_wipe_namevalue(loc); |
| 1473 | } |
| 1474 | |
| 1475 | static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc, |
| 1476 | int offset) |
| 1477 | { |
| 1478 | BUG_ON(offset >= loc->xl_size); |
| 1479 | return (char *)loc->xl_header + offset; |
| 1480 | } |
| 1481 | |
| 1482 | /* |
| 1483 | * Block storage for xattrs keeps the name+value pairs compacted. When |
| 1484 | * we remove one, we have to shift any that preceded it towards the end. |
| 1485 | */ |
| 1486 | static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc) |
| 1487 | { |
| 1488 | int i, offset; |
| 1489 | int namevalue_offset, first_namevalue_offset, namevalue_size; |
| 1490 | struct ocfs2_xattr_entry *entry = loc->xl_entry; |
| 1491 | struct ocfs2_xattr_header *xh = loc->xl_header; |
| 1492 | u64 value_size = le64_to_cpu(entry->xe_value_size); |
| 1493 | int count = le16_to_cpu(xh->xh_count); |
| 1494 | |
| 1495 | namevalue_offset = le16_to_cpu(entry->xe_name_offset); |
| 1496 | namevalue_size = OCFS2_XATTR_SIZE(entry->xe_name_len); |
| 1497 | if (value_size > OCFS2_XATTR_INLINE_SIZE) |
| 1498 | namevalue_size += OCFS2_XATTR_ROOT_SIZE; |
| 1499 | else |
| 1500 | namevalue_size += OCFS2_XATTR_SIZE(value_size); |
| 1501 | |
| 1502 | for (i = 0, first_namevalue_offset = loc->xl_size; |
| 1503 | i < count; i++) { |
| 1504 | offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset); |
| 1505 | if (offset < first_namevalue_offset) |
| 1506 | first_namevalue_offset = offset; |
| 1507 | } |
| 1508 | |
| 1509 | /* Shift the name+value pairs */ |
| 1510 | memmove((char *)xh + first_namevalue_offset + namevalue_size, |
| 1511 | (char *)xh + first_namevalue_offset, |
| 1512 | namevalue_offset - first_namevalue_offset); |
| 1513 | memset((char *)xh + first_namevalue_offset, 0, namevalue_size); |
| 1514 | |
| 1515 | /* Now tell xh->xh_entries about it */ |
| 1516 | for (i = 0; i < count; i++) { |
| 1517 | offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset); |
| 1518 | if (offset < namevalue_offset) |
| 1519 | le16_add_cpu(&xh->xh_entries[i].xe_name_offset, |
| 1520 | namevalue_size); |
| 1521 | } |
| 1522 | |
| 1523 | /* |
| 1524 | * Note that we don't update xh_free_start or xh_name_value_len |
| 1525 | * because they're not used in block-stored xattrs. |
| 1526 | */ |
| 1527 | } |
| 1528 | |
| 1529 | /* |
| 1530 | * Operations for xattrs stored in blocks. This includes inline inode |
| 1531 | * storage and unindexed ocfs2_xattr_blocks. |
| 1532 | */ |
| 1533 | static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = { |
| 1534 | .xlo_offset_pointer = ocfs2_xa_block_offset_pointer, |
| 1535 | .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue, |
| 1536 | }; |
| 1537 | |
| 1538 | static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc, |
| 1539 | int offset) |
| 1540 | { |
| 1541 | struct ocfs2_xattr_bucket *bucket = loc->xl_storage; |
| 1542 | int block, block_offset; |
| 1543 | |
| 1544 | BUG_ON(offset >= OCFS2_XATTR_BUCKET_SIZE); |
| 1545 | |
| 1546 | /* The header is at the front of the bucket */ |
| 1547 | block = offset >> bucket->bu_inode->i_sb->s_blocksize_bits; |
| 1548 | block_offset = offset % bucket->bu_inode->i_sb->s_blocksize; |
| 1549 | |
| 1550 | return bucket_block(bucket, block) + block_offset; |
| 1551 | } |
| 1552 | |
| 1553 | static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc) |
| 1554 | { |
| 1555 | int namevalue_size; |
| 1556 | struct ocfs2_xattr_entry *entry = loc->xl_entry; |
| 1557 | u64 value_size = le64_to_cpu(entry->xe_value_size); |
| 1558 | |
| 1559 | namevalue_size = OCFS2_XATTR_SIZE(entry->xe_name_len); |
| 1560 | if (value_size > OCFS2_XATTR_INLINE_SIZE) |
| 1561 | namevalue_size += OCFS2_XATTR_ROOT_SIZE; |
| 1562 | else |
| 1563 | namevalue_size += OCFS2_XATTR_SIZE(value_size); |
| 1564 | |
| 1565 | le16_add_cpu(&loc->xl_header->xh_name_value_len, -namevalue_size); |
| 1566 | } |
| 1567 | |
| 1568 | /* Operations for xattrs stored in buckets. */ |
| 1569 | static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = { |
| 1570 | .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer, |
| 1571 | .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue, |
| 1572 | }; |
| 1573 | |
| 1574 | static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc) |
| 1575 | { |
| 1576 | ocfs2_xa_wipe_namevalue(loc); |
| 1577 | } |
| 1578 | |
| 1579 | static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc, |
| 1580 | struct inode *inode, |
| 1581 | struct buffer_head *bh, |
| 1582 | struct ocfs2_xattr_entry *entry) |
| 1583 | { |
| 1584 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data; |
| 1585 | |
| 1586 | loc->xl_ops = &ocfs2_xa_block_loc_ops; |
| 1587 | loc->xl_storage = bh; |
| 1588 | loc->xl_entry = entry; |
| 1589 | |
| 1590 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL) |
| 1591 | loc->xl_size = le16_to_cpu(di->i_xattr_inline_size); |
| 1592 | else { |
| 1593 | BUG_ON(entry); |
| 1594 | loc->xl_size = OCFS2_SB(inode->i_sb)->s_xattr_inline_size; |
| 1595 | } |
| 1596 | loc->xl_header = |
| 1597 | (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size - |
| 1598 | loc->xl_size); |
| 1599 | } |
| 1600 | |
| 1601 | static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc, |
| 1602 | struct buffer_head *bh, |
| 1603 | struct ocfs2_xattr_entry *entry) |
| 1604 | { |
| 1605 | struct ocfs2_xattr_block *xb = |
| 1606 | (struct ocfs2_xattr_block *)bh->b_data; |
| 1607 | |
| 1608 | BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED); |
| 1609 | |
| 1610 | loc->xl_ops = &ocfs2_xa_block_loc_ops; |
| 1611 | loc->xl_storage = bh; |
| 1612 | loc->xl_header = &(xb->xb_attrs.xb_header); |
| 1613 | loc->xl_entry = entry; |
| 1614 | loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block, |
| 1615 | xb_attrs.xb_header); |
| 1616 | } |
| 1617 | |
| 1618 | static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc, |
| 1619 | struct ocfs2_xattr_bucket *bucket, |
| 1620 | struct ocfs2_xattr_entry *entry) |
| 1621 | { |
| 1622 | loc->xl_ops = &ocfs2_xa_bucket_loc_ops; |
| 1623 | loc->xl_storage = bucket; |
| 1624 | loc->xl_header = bucket_xh(bucket); |
| 1625 | loc->xl_entry = entry; |
| 1626 | loc->xl_size = OCFS2_XATTR_BUCKET_SIZE; |
| 1627 | } |
| 1628 | |
| 1629 | /* |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1630 | * ocfs2_xattr_set_entry_local() |
| 1631 | * |
| 1632 | * Set, replace or remove extended attribute in local. |
| 1633 | */ |
| 1634 | static void ocfs2_xattr_set_entry_local(struct inode *inode, |
| 1635 | struct ocfs2_xattr_info *xi, |
| 1636 | struct ocfs2_xattr_search *xs, |
| 1637 | struct ocfs2_xattr_entry *last, |
| 1638 | size_t min_offs) |
| 1639 | { |
| 1640 | size_t name_len = strlen(xi->name); |
| 1641 | int i; |
Joel Becker | 11179f2 | 2009-08-14 16:07:44 -0700 | [diff] [blame^] | 1642 | struct ocfs2_xa_loc loc; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1643 | |
Joel Becker | 11179f2 | 2009-08-14 16:07:44 -0700 | [diff] [blame^] | 1644 | if (xs->xattr_bh == xs->inode_bh) |
| 1645 | ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh, |
| 1646 | xs->not_found ? NULL : xs->here); |
| 1647 | else |
| 1648 | ocfs2_init_xattr_block_xa_loc(&loc, xs->xattr_bh, |
| 1649 | xs->not_found ? NULL : xs->here); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1650 | if (xi->value && xs->not_found) { |
| 1651 | /* Insert the new xattr entry. */ |
| 1652 | le16_add_cpu(&xs->header->xh_count, 1); |
| 1653 | ocfs2_xattr_set_type(last, xi->name_index); |
| 1654 | ocfs2_xattr_set_local(last, 1); |
| 1655 | last->xe_name_len = name_len; |
| 1656 | } else { |
| 1657 | void *first_val; |
| 1658 | void *val; |
| 1659 | size_t offs, size; |
| 1660 | |
| 1661 | first_val = xs->base + min_offs; |
| 1662 | offs = le16_to_cpu(xs->here->xe_name_offset); |
| 1663 | val = xs->base + offs; |
| 1664 | |
| 1665 | if (le64_to_cpu(xs->here->xe_value_size) > |
| 1666 | OCFS2_XATTR_INLINE_SIZE) |
| 1667 | size = OCFS2_XATTR_SIZE(name_len) + |
| 1668 | OCFS2_XATTR_ROOT_SIZE; |
| 1669 | else |
| 1670 | size = OCFS2_XATTR_SIZE(name_len) + |
| 1671 | OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size)); |
| 1672 | |
| 1673 | if (xi->value && size == OCFS2_XATTR_SIZE(name_len) + |
| 1674 | OCFS2_XATTR_SIZE(xi->value_len)) { |
| 1675 | /* The old and the new value have the |
| 1676 | same size. Just replace the value. */ |
| 1677 | ocfs2_xattr_set_local(xs->here, 1); |
| 1678 | xs->here->xe_value_size = cpu_to_le64(xi->value_len); |
| 1679 | /* Clear value bytes. */ |
| 1680 | memset(val + OCFS2_XATTR_SIZE(name_len), |
| 1681 | 0, |
| 1682 | OCFS2_XATTR_SIZE(xi->value_len)); |
| 1683 | memcpy(val + OCFS2_XATTR_SIZE(name_len), |
| 1684 | xi->value, |
| 1685 | xi->value_len); |
| 1686 | return; |
| 1687 | } |
Joel Becker | 11179f2 | 2009-08-14 16:07:44 -0700 | [diff] [blame^] | 1688 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1689 | /* Remove the old name+value. */ |
Joel Becker | 11179f2 | 2009-08-14 16:07:44 -0700 | [diff] [blame^] | 1690 | ocfs2_xa_wipe_namevalue(&loc); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1691 | xs->here->xe_name_hash = 0; |
| 1692 | xs->here->xe_name_offset = 0; |
| 1693 | ocfs2_xattr_set_local(xs->here, 1); |
| 1694 | xs->here->xe_value_size = 0; |
| 1695 | |
| 1696 | min_offs += size; |
| 1697 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1698 | if (!xi->value) { |
| 1699 | /* Remove the old entry. */ |
Joel Becker | 11179f2 | 2009-08-14 16:07:44 -0700 | [diff] [blame^] | 1700 | i = le16_to_cpu(xs->header->xh_count) - 1; |
| 1701 | last = &xs->header->xh_entries[i]; |
| 1702 | xs->header->xh_count = cpu_to_le16(i); |
| 1703 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1704 | memmove(xs->here, xs->here + 1, |
| 1705 | (void *)last - (void *)xs->here); |
| 1706 | memset(last, 0, sizeof(struct ocfs2_xattr_entry)); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1707 | } |
| 1708 | } |
| 1709 | if (xi->value) { |
| 1710 | /* Insert the new name+value. */ |
| 1711 | size_t size = OCFS2_XATTR_SIZE(name_len) + |
| 1712 | OCFS2_XATTR_SIZE(xi->value_len); |
| 1713 | void *val = xs->base + min_offs - size; |
| 1714 | |
| 1715 | xs->here->xe_name_offset = cpu_to_le16(min_offs - size); |
| 1716 | memset(val, 0, size); |
| 1717 | memcpy(val, xi->name, name_len); |
| 1718 | memcpy(val + OCFS2_XATTR_SIZE(name_len), |
| 1719 | xi->value, |
| 1720 | xi->value_len); |
| 1721 | xs->here->xe_value_size = cpu_to_le64(xi->value_len); |
| 1722 | ocfs2_xattr_set_local(xs->here, 1); |
| 1723 | ocfs2_xattr_hash_entry(inode, xs->header, xs->here); |
| 1724 | } |
| 1725 | |
| 1726 | return; |
| 1727 | } |
| 1728 | |
| 1729 | /* |
| 1730 | * ocfs2_xattr_set_entry() |
| 1731 | * |
| 1732 | * Set extended attribute entry into inode or block. |
| 1733 | * |
| 1734 | * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE, |
| 1735 | * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(), |
| 1736 | * then set value in B tree with set_value_outside(). |
| 1737 | */ |
| 1738 | static int ocfs2_xattr_set_entry(struct inode *inode, |
| 1739 | struct ocfs2_xattr_info *xi, |
| 1740 | struct ocfs2_xattr_search *xs, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 1741 | struct ocfs2_xattr_set_ctxt *ctxt, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1742 | int flag) |
| 1743 | { |
| 1744 | struct ocfs2_xattr_entry *last; |
| 1745 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 1746 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; |
| 1747 | size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name); |
| 1748 | size_t size_l = 0; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1749 | handle_t *handle = ctxt->handle; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1750 | int free, i, ret; |
| 1751 | struct ocfs2_xattr_info xi_l = { |
| 1752 | .name_index = xi->name_index, |
| 1753 | .name = xi->name, |
| 1754 | .value = xi->value, |
| 1755 | .value_len = xi->value_len, |
| 1756 | }; |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1757 | struct ocfs2_xattr_value_buf vb = { |
| 1758 | .vb_bh = xs->xattr_bh, |
| 1759 | .vb_access = ocfs2_journal_access_di, |
| 1760 | }; |
| 1761 | |
| 1762 | if (!(flag & OCFS2_INLINE_XATTR_FL)) { |
| 1763 | BUG_ON(xs->xattr_bh == xs->inode_bh); |
| 1764 | vb.vb_access = ocfs2_journal_access_xb; |
| 1765 | } else |
| 1766 | BUG_ON(xs->xattr_bh != xs->inode_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1767 | |
| 1768 | /* Compute min_offs, last and free space. */ |
| 1769 | last = xs->header->xh_entries; |
| 1770 | |
| 1771 | for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) { |
| 1772 | size_t offs = le16_to_cpu(last->xe_name_offset); |
| 1773 | if (offs < min_offs) |
| 1774 | min_offs = offs; |
| 1775 | last += 1; |
| 1776 | } |
| 1777 | |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 1778 | free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1779 | if (free < 0) |
Joel Becker | b37c4d8 | 2008-10-20 18:24:03 -0700 | [diff] [blame] | 1780 | return -EIO; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1781 | |
| 1782 | if (!xs->not_found) { |
| 1783 | size_t size = 0; |
| 1784 | if (ocfs2_xattr_is_local(xs->here)) |
| 1785 | size = OCFS2_XATTR_SIZE(name_len) + |
| 1786 | OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size)); |
| 1787 | else |
| 1788 | size = OCFS2_XATTR_SIZE(name_len) + |
| 1789 | OCFS2_XATTR_ROOT_SIZE; |
| 1790 | free += (size + sizeof(struct ocfs2_xattr_entry)); |
| 1791 | } |
| 1792 | /* Check free space in inode or block */ |
| 1793 | if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 1794 | if (free < sizeof(struct ocfs2_xattr_entry) + |
| 1795 | OCFS2_XATTR_SIZE(name_len) + |
| 1796 | OCFS2_XATTR_ROOT_SIZE) { |
| 1797 | ret = -ENOSPC; |
| 1798 | goto out; |
| 1799 | } |
| 1800 | size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE; |
| 1801 | xi_l.value = (void *)&def_xv; |
| 1802 | xi_l.value_len = OCFS2_XATTR_ROOT_SIZE; |
| 1803 | } else if (xi->value) { |
| 1804 | if (free < sizeof(struct ocfs2_xattr_entry) + |
| 1805 | OCFS2_XATTR_SIZE(name_len) + |
| 1806 | OCFS2_XATTR_SIZE(xi->value_len)) { |
| 1807 | ret = -ENOSPC; |
| 1808 | goto out; |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | if (!xs->not_found) { |
| 1813 | /* For existing extended attribute */ |
| 1814 | size_t size = OCFS2_XATTR_SIZE(name_len) + |
| 1815 | OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size)); |
| 1816 | size_t offs = le16_to_cpu(xs->here->xe_name_offset); |
| 1817 | void *val = xs->base + offs; |
| 1818 | |
| 1819 | if (ocfs2_xattr_is_local(xs->here) && size == size_l) { |
| 1820 | /* Replace existing local xattr with tree root */ |
| 1821 | ret = ocfs2_xattr_set_value_outside(inode, xi, xs, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1822 | ctxt, &vb, offs); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1823 | if (ret < 0) |
| 1824 | mlog_errno(ret); |
| 1825 | goto out; |
| 1826 | } else if (!ocfs2_xattr_is_local(xs->here)) { |
| 1827 | /* For existing xattr which has value outside */ |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1828 | vb.vb_xv = (struct ocfs2_xattr_value_root *) |
| 1829 | (val + OCFS2_XATTR_SIZE(name_len)); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1830 | |
| 1831 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 1832 | /* |
| 1833 | * If new value need set outside also, |
| 1834 | * first truncate old value to new value, |
| 1835 | * then set new value with set_value_outside(). |
| 1836 | */ |
| 1837 | ret = ocfs2_xattr_value_truncate(inode, |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 1838 | &vb, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 1839 | xi->value_len, |
| 1840 | ctxt); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1841 | if (ret < 0) { |
| 1842 | mlog_errno(ret); |
| 1843 | goto out; |
| 1844 | } |
| 1845 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1846 | ret = ocfs2_xattr_update_entry(inode, |
| 1847 | handle, |
| 1848 | xi, |
| 1849 | xs, |
Joel Becker | 0c748e9 | 2008-12-09 15:46:15 -0800 | [diff] [blame] | 1850 | &vb, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1851 | offs); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1852 | if (ret < 0) { |
| 1853 | mlog_errno(ret); |
| 1854 | goto out; |
| 1855 | } |
| 1856 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1857 | ret = __ocfs2_xattr_set_value_outside(inode, |
| 1858 | handle, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 1859 | &vb, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1860 | xi->value, |
| 1861 | xi->value_len); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1862 | if (ret < 0) |
| 1863 | mlog_errno(ret); |
| 1864 | goto out; |
| 1865 | } else { |
| 1866 | /* |
| 1867 | * If new value need set in local, |
| 1868 | * just trucate old value to zero. |
| 1869 | */ |
| 1870 | ret = ocfs2_xattr_value_truncate(inode, |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 1871 | &vb, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1872 | 0, |
| 1873 | ctxt); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1874 | if (ret < 0) |
| 1875 | mlog_errno(ret); |
| 1876 | } |
| 1877 | } |
| 1878 | } |
| 1879 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1880 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1881 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1882 | if (ret) { |
| 1883 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1884 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | if (!(flag & OCFS2_INLINE_XATTR_FL)) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1888 | ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1889 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1890 | if (ret) { |
| 1891 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1892 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | /* |
| 1897 | * Set value in local, include set tree root in local. |
| 1898 | * This is the first step for value size >INLINE_SIZE. |
| 1899 | */ |
| 1900 | ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs); |
| 1901 | |
| 1902 | if (!(flag & OCFS2_INLINE_XATTR_FL)) { |
| 1903 | ret = ocfs2_journal_dirty(handle, xs->xattr_bh); |
| 1904 | if (ret < 0) { |
| 1905 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1906 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1907 | } |
| 1908 | } |
| 1909 | |
| 1910 | if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) && |
| 1911 | (flag & OCFS2_INLINE_XATTR_FL)) { |
| 1912 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 1913 | unsigned int xattrsize = osb->s_xattr_inline_size; |
| 1914 | |
| 1915 | /* |
| 1916 | * Adjust extent record count or inline data size |
| 1917 | * to reserve space for extended attribute. |
| 1918 | */ |
| 1919 | if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
| 1920 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 1921 | le16_add_cpu(&idata->id_count, -xattrsize); |
| 1922 | } else if (!(ocfs2_inode_is_fast_symlink(inode))) { |
| 1923 | struct ocfs2_extent_list *el = &di->id2.i_list; |
| 1924 | le16_add_cpu(&el->l_count, -(xattrsize / |
| 1925 | sizeof(struct ocfs2_extent_rec))); |
| 1926 | } |
| 1927 | di->i_xattr_inline_size = cpu_to_le16(xattrsize); |
| 1928 | } |
| 1929 | /* Update xattr flag */ |
| 1930 | spin_lock(&oi->ip_lock); |
| 1931 | oi->ip_dyn_features |= flag; |
| 1932 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 1933 | spin_unlock(&oi->ip_lock); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1934 | |
| 1935 | ret = ocfs2_journal_dirty(handle, xs->inode_bh); |
| 1936 | if (ret < 0) |
| 1937 | mlog_errno(ret); |
| 1938 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1939 | if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 1940 | /* |
| 1941 | * Set value outside in B tree. |
| 1942 | * This is the second step for value size > INLINE_SIZE. |
| 1943 | */ |
| 1944 | size_t offs = le16_to_cpu(xs->here->xe_name_offset); |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1945 | ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt, |
| 1946 | &vb, offs); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1947 | if (ret < 0) { |
| 1948 | int ret2; |
| 1949 | |
| 1950 | mlog_errno(ret); |
| 1951 | /* |
| 1952 | * If set value outside failed, we have to clean |
| 1953 | * the junk tree root we have already set in local. |
| 1954 | */ |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1955 | ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1956 | xi, xs, &vb, offs); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1957 | if (ret2 < 0) |
| 1958 | mlog_errno(ret2); |
| 1959 | } |
| 1960 | } |
| 1961 | out: |
| 1962 | return ret; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1963 | } |
| 1964 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1965 | /* |
| 1966 | * In xattr remove, if it is stored outside and refcounted, we may have |
| 1967 | * the chance to split the refcount tree. So need the allocators. |
| 1968 | */ |
| 1969 | static int ocfs2_lock_xattr_remove_allocators(struct inode *inode, |
| 1970 | struct ocfs2_xattr_value_root *xv, |
| 1971 | struct ocfs2_caching_info *ref_ci, |
| 1972 | struct buffer_head *ref_root_bh, |
| 1973 | struct ocfs2_alloc_context **meta_ac, |
| 1974 | int *ref_credits) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1975 | { |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1976 | int ret, meta_add = 0; |
| 1977 | u32 p_cluster, num_clusters; |
| 1978 | unsigned int ext_flags; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 1979 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1980 | *ref_credits = 0; |
| 1981 | ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster, |
| 1982 | &num_clusters, |
| 1983 | &xv->xr_list, |
| 1984 | &ext_flags); |
| 1985 | if (ret) { |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1986 | mlog_errno(ret); |
| 1987 | goto out; |
| 1988 | } |
| 1989 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1990 | if (!(ext_flags & OCFS2_EXT_REFCOUNTED)) |
| 1991 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1992 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 1993 | ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci, |
| 1994 | ref_root_bh, xv, |
| 1995 | &meta_add, ref_credits); |
| 1996 | if (ret) { |
| 1997 | mlog_errno(ret); |
| 1998 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1999 | } |
| 2000 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2001 | ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb), |
| 2002 | meta_add, meta_ac); |
| 2003 | if (ret) |
| 2004 | mlog_errno(ret); |
| 2005 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2006 | out: |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2007 | return ret; |
| 2008 | } |
| 2009 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2010 | static int ocfs2_remove_value_outside(struct inode*inode, |
| 2011 | struct ocfs2_xattr_value_buf *vb, |
| 2012 | struct ocfs2_xattr_header *header, |
| 2013 | struct ocfs2_caching_info *ref_ci, |
| 2014 | struct buffer_head *ref_root_bh) |
| 2015 | { |
| 2016 | int ret = 0, i, ref_credits; |
| 2017 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 2018 | struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, }; |
| 2019 | void *val; |
| 2020 | |
| 2021 | ocfs2_init_dealloc_ctxt(&ctxt.dealloc); |
| 2022 | |
| 2023 | for (i = 0; i < le16_to_cpu(header->xh_count); i++) { |
| 2024 | struct ocfs2_xattr_entry *entry = &header->xh_entries[i]; |
| 2025 | |
| 2026 | if (ocfs2_xattr_is_local(entry)) |
| 2027 | continue; |
| 2028 | |
| 2029 | val = (void *)header + |
| 2030 | le16_to_cpu(entry->xe_name_offset); |
| 2031 | vb->vb_xv = (struct ocfs2_xattr_value_root *) |
| 2032 | (val + OCFS2_XATTR_SIZE(entry->xe_name_len)); |
| 2033 | |
| 2034 | ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv, |
| 2035 | ref_ci, ref_root_bh, |
| 2036 | &ctxt.meta_ac, |
| 2037 | &ref_credits); |
| 2038 | |
| 2039 | ctxt.handle = ocfs2_start_trans(osb, ref_credits + |
| 2040 | ocfs2_remove_extent_credits(osb->sb)); |
| 2041 | if (IS_ERR(ctxt.handle)) { |
| 2042 | ret = PTR_ERR(ctxt.handle); |
| 2043 | mlog_errno(ret); |
| 2044 | break; |
| 2045 | } |
| 2046 | |
| 2047 | ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt); |
| 2048 | if (ret < 0) { |
| 2049 | mlog_errno(ret); |
| 2050 | break; |
| 2051 | } |
| 2052 | |
| 2053 | ocfs2_commit_trans(osb, ctxt.handle); |
| 2054 | if (ctxt.meta_ac) { |
| 2055 | ocfs2_free_alloc_context(ctxt.meta_ac); |
| 2056 | ctxt.meta_ac = NULL; |
| 2057 | } |
| 2058 | } |
| 2059 | |
| 2060 | if (ctxt.meta_ac) |
| 2061 | ocfs2_free_alloc_context(ctxt.meta_ac); |
| 2062 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 2063 | ocfs2_run_deallocs(osb, &ctxt.dealloc); |
| 2064 | return ret; |
| 2065 | } |
| 2066 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2067 | static int ocfs2_xattr_ibody_remove(struct inode *inode, |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2068 | struct buffer_head *di_bh, |
| 2069 | struct ocfs2_caching_info *ref_ci, |
| 2070 | struct buffer_head *ref_root_bh) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2071 | { |
| 2072 | |
| 2073 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 2074 | struct ocfs2_xattr_header *header; |
| 2075 | int ret; |
Joel Becker | 4311901 | 2008-12-09 16:24:43 -0800 | [diff] [blame] | 2076 | struct ocfs2_xattr_value_buf vb = { |
| 2077 | .vb_bh = di_bh, |
| 2078 | .vb_access = ocfs2_journal_access_di, |
| 2079 | }; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2080 | |
| 2081 | header = (struct ocfs2_xattr_header *) |
| 2082 | ((void *)di + inode->i_sb->s_blocksize - |
| 2083 | le16_to_cpu(di->i_xattr_inline_size)); |
| 2084 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2085 | ret = ocfs2_remove_value_outside(inode, &vb, header, |
| 2086 | ref_ci, ref_root_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2087 | |
| 2088 | return ret; |
| 2089 | } |
| 2090 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2091 | struct ocfs2_rm_xattr_bucket_para { |
| 2092 | struct ocfs2_caching_info *ref_ci; |
| 2093 | struct buffer_head *ref_root_bh; |
| 2094 | }; |
| 2095 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2096 | static int ocfs2_xattr_block_remove(struct inode *inode, |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2097 | struct buffer_head *blk_bh, |
| 2098 | struct ocfs2_caching_info *ref_ci, |
| 2099 | struct buffer_head *ref_root_bh) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2100 | { |
| 2101 | struct ocfs2_xattr_block *xb; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2102 | int ret = 0; |
Joel Becker | 4311901 | 2008-12-09 16:24:43 -0800 | [diff] [blame] | 2103 | struct ocfs2_xattr_value_buf vb = { |
| 2104 | .vb_bh = blk_bh, |
| 2105 | .vb_access = ocfs2_journal_access_xb, |
| 2106 | }; |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2107 | struct ocfs2_rm_xattr_bucket_para args = { |
| 2108 | .ref_ci = ref_ci, |
| 2109 | .ref_root_bh = ref_root_bh, |
| 2110 | }; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2111 | |
| 2112 | xb = (struct ocfs2_xattr_block *)blk_bh->b_data; |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 2113 | if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { |
| 2114 | struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header); |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2115 | ret = ocfs2_remove_value_outside(inode, &vb, header, |
| 2116 | ref_ci, ref_root_bh); |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 2117 | } else |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 2118 | ret = ocfs2_iterate_xattr_index_block(inode, |
| 2119 | blk_bh, |
| 2120 | ocfs2_rm_xattr_cluster, |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2121 | &args); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2122 | |
| 2123 | return ret; |
| 2124 | } |
| 2125 | |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 2126 | static int ocfs2_xattr_free_block(struct inode *inode, |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2127 | u64 block, |
| 2128 | struct ocfs2_caching_info *ref_ci, |
| 2129 | struct buffer_head *ref_root_bh) |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 2130 | { |
| 2131 | struct inode *xb_alloc_inode; |
| 2132 | struct buffer_head *xb_alloc_bh = NULL; |
| 2133 | struct buffer_head *blk_bh = NULL; |
| 2134 | struct ocfs2_xattr_block *xb; |
| 2135 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 2136 | handle_t *handle; |
| 2137 | int ret = 0; |
| 2138 | u64 blk, bg_blkno; |
| 2139 | u16 bit; |
| 2140 | |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 2141 | ret = ocfs2_read_xattr_block(inode, block, &blk_bh); |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 2142 | if (ret < 0) { |
| 2143 | mlog_errno(ret); |
| 2144 | goto out; |
| 2145 | } |
| 2146 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2147 | 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] | 2148 | if (ret < 0) { |
| 2149 | mlog_errno(ret); |
| 2150 | goto out; |
| 2151 | } |
| 2152 | |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 2153 | xb = (struct ocfs2_xattr_block *)blk_bh->b_data; |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 2154 | blk = le64_to_cpu(xb->xb_blkno); |
| 2155 | bit = le16_to_cpu(xb->xb_suballoc_bit); |
| 2156 | bg_blkno = ocfs2_which_suballoc_group(blk, bit); |
| 2157 | |
| 2158 | xb_alloc_inode = ocfs2_get_system_file_inode(osb, |
| 2159 | EXTENT_ALLOC_SYSTEM_INODE, |
| 2160 | le16_to_cpu(xb->xb_suballoc_slot)); |
| 2161 | if (!xb_alloc_inode) { |
| 2162 | ret = -ENOMEM; |
| 2163 | mlog_errno(ret); |
| 2164 | goto out; |
| 2165 | } |
| 2166 | mutex_lock(&xb_alloc_inode->i_mutex); |
| 2167 | |
| 2168 | ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1); |
| 2169 | if (ret < 0) { |
| 2170 | mlog_errno(ret); |
| 2171 | goto out_mutex; |
| 2172 | } |
| 2173 | |
| 2174 | handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE); |
| 2175 | if (IS_ERR(handle)) { |
| 2176 | ret = PTR_ERR(handle); |
| 2177 | mlog_errno(ret); |
| 2178 | goto out_unlock; |
| 2179 | } |
| 2180 | |
| 2181 | ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh, |
| 2182 | bit, bg_blkno, 1); |
| 2183 | if (ret < 0) |
| 2184 | mlog_errno(ret); |
| 2185 | |
| 2186 | ocfs2_commit_trans(osb, handle); |
| 2187 | out_unlock: |
| 2188 | ocfs2_inode_unlock(xb_alloc_inode, 1); |
| 2189 | brelse(xb_alloc_bh); |
| 2190 | out_mutex: |
| 2191 | mutex_unlock(&xb_alloc_inode->i_mutex); |
| 2192 | iput(xb_alloc_inode); |
| 2193 | out: |
| 2194 | brelse(blk_bh); |
| 2195 | return ret; |
| 2196 | } |
| 2197 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2198 | /* |
| 2199 | * ocfs2_xattr_remove() |
| 2200 | * |
| 2201 | * Free extended attribute resources associated with this inode. |
| 2202 | */ |
| 2203 | int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh) |
| 2204 | { |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2205 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 2206 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2207 | struct ocfs2_refcount_tree *ref_tree = NULL; |
| 2208 | struct buffer_head *ref_root_bh = NULL; |
| 2209 | struct ocfs2_caching_info *ref_ci = NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2210 | handle_t *handle; |
| 2211 | int ret; |
| 2212 | |
Tiger Yang | 8154da3 | 2008-08-18 17:11:46 +0800 | [diff] [blame] | 2213 | if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) |
| 2214 | return 0; |
| 2215 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2216 | if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) |
| 2217 | return 0; |
| 2218 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2219 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) { |
| 2220 | ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb), |
| 2221 | le64_to_cpu(di->i_refcount_loc), |
| 2222 | 1, &ref_tree, &ref_root_bh); |
| 2223 | if (ret) { |
| 2224 | mlog_errno(ret); |
| 2225 | goto out; |
| 2226 | } |
| 2227 | ref_ci = &ref_tree->rf_ci; |
| 2228 | |
| 2229 | } |
| 2230 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2231 | if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) { |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2232 | ret = ocfs2_xattr_ibody_remove(inode, di_bh, |
| 2233 | ref_ci, ref_root_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2234 | if (ret < 0) { |
| 2235 | mlog_errno(ret); |
| 2236 | goto out; |
| 2237 | } |
| 2238 | } |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2239 | |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 2240 | if (di->i_xattr_loc) { |
| 2241 | ret = ocfs2_xattr_free_block(inode, |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2242 | le64_to_cpu(di->i_xattr_loc), |
| 2243 | ref_ci, ref_root_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2244 | if (ret < 0) { |
| 2245 | mlog_errno(ret); |
| 2246 | goto out; |
| 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), |
| 2251 | OCFS2_INODE_UPDATE_CREDITS); |
| 2252 | if (IS_ERR(handle)) { |
| 2253 | ret = PTR_ERR(handle); |
| 2254 | mlog_errno(ret); |
| 2255 | goto out; |
| 2256 | } |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2257 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 8400897 | 2008-12-09 16:11:49 -0800 | [diff] [blame] | 2258 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2259 | if (ret) { |
| 2260 | mlog_errno(ret); |
| 2261 | goto out_commit; |
| 2262 | } |
| 2263 | |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 2264 | di->i_xattr_loc = 0; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2265 | |
| 2266 | spin_lock(&oi->ip_lock); |
| 2267 | oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL); |
| 2268 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 2269 | spin_unlock(&oi->ip_lock); |
| 2270 | |
| 2271 | ret = ocfs2_journal_dirty(handle, di_bh); |
| 2272 | if (ret < 0) |
| 2273 | mlog_errno(ret); |
| 2274 | out_commit: |
| 2275 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); |
| 2276 | out: |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 2277 | if (ref_tree) |
| 2278 | ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1); |
| 2279 | brelse(ref_root_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2280 | return ret; |
| 2281 | } |
| 2282 | |
| 2283 | static int ocfs2_xattr_has_space_inline(struct inode *inode, |
| 2284 | struct ocfs2_dinode *di) |
| 2285 | { |
| 2286 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 2287 | unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size; |
| 2288 | int free; |
| 2289 | |
| 2290 | if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE) |
| 2291 | return 0; |
| 2292 | |
| 2293 | if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
| 2294 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 2295 | free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size); |
| 2296 | } else if (ocfs2_inode_is_fast_symlink(inode)) { |
| 2297 | free = ocfs2_fast_symlink_chars(inode->i_sb) - |
| 2298 | le64_to_cpu(di->i_size); |
| 2299 | } else { |
| 2300 | struct ocfs2_extent_list *el = &di->id2.i_list; |
| 2301 | free = (le16_to_cpu(el->l_count) - |
| 2302 | le16_to_cpu(el->l_next_free_rec)) * |
| 2303 | sizeof(struct ocfs2_extent_rec); |
| 2304 | } |
| 2305 | if (free >= xattrsize) |
| 2306 | return 1; |
| 2307 | |
| 2308 | return 0; |
| 2309 | } |
| 2310 | |
| 2311 | /* |
| 2312 | * ocfs2_xattr_ibody_find() |
| 2313 | * |
| 2314 | * Find extended attribute in inode block and |
| 2315 | * fill search info into struct ocfs2_xattr_search. |
| 2316 | */ |
| 2317 | static int ocfs2_xattr_ibody_find(struct inode *inode, |
| 2318 | int name_index, |
| 2319 | const char *name, |
| 2320 | struct ocfs2_xattr_search *xs) |
| 2321 | { |
| 2322 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 2323 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; |
| 2324 | int ret; |
| 2325 | int has_space = 0; |
| 2326 | |
| 2327 | if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) |
| 2328 | return 0; |
| 2329 | |
| 2330 | if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) { |
| 2331 | down_read(&oi->ip_alloc_sem); |
| 2332 | has_space = ocfs2_xattr_has_space_inline(inode, di); |
| 2333 | up_read(&oi->ip_alloc_sem); |
| 2334 | if (!has_space) |
| 2335 | return 0; |
| 2336 | } |
| 2337 | |
| 2338 | xs->xattr_bh = xs->inode_bh; |
| 2339 | xs->end = (void *)di + inode->i_sb->s_blocksize; |
| 2340 | if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) |
| 2341 | xs->header = (struct ocfs2_xattr_header *) |
| 2342 | (xs->end - le16_to_cpu(di->i_xattr_inline_size)); |
| 2343 | else |
| 2344 | xs->header = (struct ocfs2_xattr_header *) |
| 2345 | (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size); |
| 2346 | xs->base = (void *)xs->header; |
| 2347 | xs->here = xs->header->xh_entries; |
| 2348 | |
| 2349 | /* Find the named attribute. */ |
| 2350 | if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) { |
| 2351 | ret = ocfs2_xattr_find_entry(name_index, name, xs); |
| 2352 | if (ret && ret != -ENODATA) |
| 2353 | return ret; |
| 2354 | xs->not_found = ret; |
| 2355 | } |
| 2356 | |
| 2357 | return 0; |
| 2358 | } |
| 2359 | |
| 2360 | /* |
| 2361 | * ocfs2_xattr_ibody_set() |
| 2362 | * |
| 2363 | * Set, replace or remove an extended attribute into inode block. |
| 2364 | * |
| 2365 | */ |
| 2366 | static int ocfs2_xattr_ibody_set(struct inode *inode, |
| 2367 | struct ocfs2_xattr_info *xi, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2368 | struct ocfs2_xattr_search *xs, |
| 2369 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2370 | { |
| 2371 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 2372 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; |
| 2373 | int ret; |
| 2374 | |
| 2375 | if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) |
| 2376 | return -ENOSPC; |
| 2377 | |
| 2378 | down_write(&oi->ip_alloc_sem); |
| 2379 | if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) { |
| 2380 | if (!ocfs2_xattr_has_space_inline(inode, di)) { |
| 2381 | ret = -ENOSPC; |
| 2382 | goto out; |
| 2383 | } |
| 2384 | } |
| 2385 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2386 | ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2387 | (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL)); |
| 2388 | out: |
| 2389 | up_write(&oi->ip_alloc_sem); |
| 2390 | |
| 2391 | return ret; |
| 2392 | } |
| 2393 | |
| 2394 | /* |
| 2395 | * ocfs2_xattr_block_find() |
| 2396 | * |
| 2397 | * Find extended attribute in external block and |
| 2398 | * fill search info into struct ocfs2_xattr_search. |
| 2399 | */ |
| 2400 | static int ocfs2_xattr_block_find(struct inode *inode, |
| 2401 | int name_index, |
| 2402 | const char *name, |
| 2403 | struct ocfs2_xattr_search *xs) |
| 2404 | { |
| 2405 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; |
| 2406 | struct buffer_head *blk_bh = NULL; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 2407 | struct ocfs2_xattr_block *xb; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2408 | int ret = 0; |
| 2409 | |
| 2410 | if (!di->i_xattr_loc) |
| 2411 | return ret; |
| 2412 | |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 2413 | ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc), |
| 2414 | &blk_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2415 | if (ret < 0) { |
| 2416 | mlog_errno(ret); |
| 2417 | return ret; |
| 2418 | } |
Joel Becker | f6087fb | 2008-10-20 18:20:43 -0700 | [diff] [blame] | 2419 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2420 | xs->xattr_bh = blk_bh; |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 2421 | xb = (struct ocfs2_xattr_block *)blk_bh->b_data; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2422 | |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 2423 | if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { |
| 2424 | xs->header = &xb->xb_attrs.xb_header; |
| 2425 | xs->base = (void *)xs->header; |
| 2426 | xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size; |
| 2427 | xs->here = xs->header->xh_entries; |
| 2428 | |
| 2429 | ret = ocfs2_xattr_find_entry(name_index, name, xs); |
| 2430 | } else |
| 2431 | ret = ocfs2_xattr_index_block_find(inode, blk_bh, |
| 2432 | name_index, |
| 2433 | name, xs); |
| 2434 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2435 | if (ret && ret != -ENODATA) { |
| 2436 | xs->xattr_bh = NULL; |
| 2437 | goto cleanup; |
| 2438 | } |
| 2439 | xs->not_found = ret; |
| 2440 | return 0; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2441 | cleanup: |
| 2442 | brelse(blk_bh); |
| 2443 | |
| 2444 | return ret; |
| 2445 | } |
| 2446 | |
Tao Ma | 5aea1f0 | 2009-08-18 11:43:24 +0800 | [diff] [blame] | 2447 | static int ocfs2_create_xattr_block(handle_t *handle, |
| 2448 | struct inode *inode, |
| 2449 | struct buffer_head *inode_bh, |
| 2450 | struct ocfs2_alloc_context *meta_ac, |
Tao Ma | a7fe7a3 | 2009-08-18 11:43:52 +0800 | [diff] [blame] | 2451 | struct buffer_head **ret_bh, |
| 2452 | int indexed) |
Tao Ma | 5aea1f0 | 2009-08-18 11:43:24 +0800 | [diff] [blame] | 2453 | { |
| 2454 | int ret; |
| 2455 | u16 suballoc_bit_start; |
| 2456 | u32 num_got; |
| 2457 | u64 first_blkno; |
| 2458 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data; |
| 2459 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 2460 | struct buffer_head *new_bh = NULL; |
| 2461 | struct ocfs2_xattr_block *xblk; |
| 2462 | |
| 2463 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh, |
| 2464 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 2465 | if (ret < 0) { |
| 2466 | mlog_errno(ret); |
| 2467 | goto end; |
| 2468 | } |
| 2469 | |
| 2470 | ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1, |
| 2471 | &suballoc_bit_start, &num_got, |
| 2472 | &first_blkno); |
| 2473 | if (ret < 0) { |
| 2474 | mlog_errno(ret); |
| 2475 | goto end; |
| 2476 | } |
| 2477 | |
| 2478 | new_bh = sb_getblk(inode->i_sb, first_blkno); |
| 2479 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh); |
| 2480 | |
| 2481 | ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), |
| 2482 | new_bh, |
| 2483 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 2484 | if (ret < 0) { |
| 2485 | mlog_errno(ret); |
| 2486 | goto end; |
| 2487 | } |
| 2488 | |
| 2489 | /* Initialize ocfs2_xattr_block */ |
| 2490 | xblk = (struct ocfs2_xattr_block *)new_bh->b_data; |
| 2491 | memset(xblk, 0, inode->i_sb->s_blocksize); |
| 2492 | strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE); |
Tiger Yang | b89c542 | 2010-01-25 14:11:06 +0800 | [diff] [blame] | 2493 | xblk->xb_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot); |
Tao Ma | 5aea1f0 | 2009-08-18 11:43:24 +0800 | [diff] [blame] | 2494 | xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start); |
| 2495 | xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation); |
| 2496 | xblk->xb_blkno = cpu_to_le64(first_blkno); |
| 2497 | |
Tao Ma | a7fe7a3 | 2009-08-18 11:43:52 +0800 | [diff] [blame] | 2498 | if (indexed) { |
| 2499 | struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root; |
| 2500 | xr->xt_clusters = cpu_to_le32(1); |
| 2501 | xr->xt_last_eb_blk = 0; |
| 2502 | xr->xt_list.l_tree_depth = 0; |
| 2503 | xr->xt_list.l_count = cpu_to_le16( |
| 2504 | ocfs2_xattr_recs_per_xb(inode->i_sb)); |
| 2505 | xr->xt_list.l_next_free_rec = cpu_to_le16(1); |
| 2506 | xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED); |
| 2507 | } |
| 2508 | |
Tao Ma | 5aea1f0 | 2009-08-18 11:43:24 +0800 | [diff] [blame] | 2509 | ret = ocfs2_journal_dirty(handle, new_bh); |
| 2510 | if (ret < 0) { |
| 2511 | mlog_errno(ret); |
| 2512 | goto end; |
| 2513 | } |
| 2514 | di->i_xattr_loc = cpu_to_le64(first_blkno); |
| 2515 | ocfs2_journal_dirty(handle, inode_bh); |
| 2516 | |
| 2517 | *ret_bh = new_bh; |
| 2518 | new_bh = NULL; |
| 2519 | |
| 2520 | end: |
| 2521 | brelse(new_bh); |
| 2522 | return ret; |
| 2523 | } |
| 2524 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2525 | /* |
| 2526 | * ocfs2_xattr_block_set() |
| 2527 | * |
| 2528 | * Set, replace or remove an extended attribute into external block. |
| 2529 | * |
| 2530 | */ |
| 2531 | static int ocfs2_xattr_block_set(struct inode *inode, |
| 2532 | struct ocfs2_xattr_info *xi, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2533 | struct ocfs2_xattr_search *xs, |
| 2534 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2535 | { |
| 2536 | struct buffer_head *new_bh = NULL; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2537 | handle_t *handle = ctxt->handle; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2538 | struct ocfs2_xattr_block *xblk = NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2539 | int ret; |
| 2540 | |
| 2541 | if (!xs->xattr_bh) { |
Tao Ma | 5aea1f0 | 2009-08-18 11:43:24 +0800 | [diff] [blame] | 2542 | ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh, |
Tao Ma | a7fe7a3 | 2009-08-18 11:43:52 +0800 | [diff] [blame] | 2543 | ctxt->meta_ac, &new_bh, 0); |
Tao Ma | 5aea1f0 | 2009-08-18 11:43:24 +0800 | [diff] [blame] | 2544 | if (ret) { |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2545 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2546 | goto end; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2547 | } |
| 2548 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2549 | xs->xattr_bh = new_bh; |
Tao Ma | 5aea1f0 | 2009-08-18 11:43:24 +0800 | [diff] [blame] | 2550 | xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2551 | xs->header = &xblk->xb_attrs.xb_header; |
| 2552 | xs->base = (void *)xs->header; |
| 2553 | xs->end = (void *)xblk + inode->i_sb->s_blocksize; |
| 2554 | xs->here = xs->header->xh_entries; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 2555 | } else |
| 2556 | xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; |
| 2557 | |
| 2558 | if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) { |
| 2559 | /* Set extended attribute into external block */ |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2560 | ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt, |
| 2561 | OCFS2_HAS_XATTR_FL); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 2562 | if (!ret || ret != -ENOSPC) |
| 2563 | goto end; |
| 2564 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2565 | ret = ocfs2_xattr_create_index_block(inode, xs, ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 2566 | if (ret) |
| 2567 | goto end; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2568 | } |
| 2569 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2570 | ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 2571 | |
| 2572 | end: |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2573 | |
| 2574 | return ret; |
| 2575 | } |
| 2576 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2577 | /* Check whether the new xattr can be inserted into the inode. */ |
| 2578 | static int ocfs2_xattr_can_be_in_inode(struct inode *inode, |
| 2579 | struct ocfs2_xattr_info *xi, |
| 2580 | struct ocfs2_xattr_search *xs) |
| 2581 | { |
| 2582 | u64 value_size; |
| 2583 | struct ocfs2_xattr_entry *last; |
| 2584 | int free, i; |
| 2585 | size_t min_offs = xs->end - xs->base; |
| 2586 | |
| 2587 | if (!xs->header) |
| 2588 | return 0; |
| 2589 | |
| 2590 | last = xs->header->xh_entries; |
| 2591 | |
| 2592 | for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) { |
| 2593 | size_t offs = le16_to_cpu(last->xe_name_offset); |
| 2594 | if (offs < min_offs) |
| 2595 | min_offs = offs; |
| 2596 | last += 1; |
| 2597 | } |
| 2598 | |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 2599 | free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2600 | if (free < 0) |
| 2601 | return 0; |
| 2602 | |
| 2603 | BUG_ON(!xs->not_found); |
| 2604 | |
| 2605 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) |
| 2606 | value_size = OCFS2_XATTR_ROOT_SIZE; |
| 2607 | else |
| 2608 | value_size = OCFS2_XATTR_SIZE(xi->value_len); |
| 2609 | |
| 2610 | if (free >= sizeof(struct ocfs2_xattr_entry) + |
| 2611 | OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size) |
| 2612 | return 1; |
| 2613 | |
| 2614 | return 0; |
| 2615 | } |
| 2616 | |
| 2617 | static int ocfs2_calc_xattr_set_need(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, |
| 2622 | int *clusters_need, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2623 | int *meta_need, |
| 2624 | int *credits_need) |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2625 | { |
| 2626 | int ret = 0, old_in_xb = 0; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2627 | int clusters_add = 0, meta_add = 0, credits = 0; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2628 | struct buffer_head *bh = NULL; |
| 2629 | struct ocfs2_xattr_block *xb = NULL; |
| 2630 | struct ocfs2_xattr_entry *xe = NULL; |
| 2631 | struct ocfs2_xattr_value_root *xv = NULL; |
| 2632 | char *base = NULL; |
| 2633 | int name_offset, name_len = 0; |
| 2634 | u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, |
| 2635 | xi->value_len); |
| 2636 | u64 value_size; |
| 2637 | |
Tao Ma | 71d548a | 2008-12-05 06:20:54 +0800 | [diff] [blame] | 2638 | /* |
| 2639 | * Calculate the clusters we need to write. |
| 2640 | * No matter whether we replace an old one or add a new one, |
| 2641 | * we need this for writing. |
| 2642 | */ |
| 2643 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) |
| 2644 | credits += new_clusters * |
| 2645 | ocfs2_clusters_to_blocks(inode->i_sb, 1); |
| 2646 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2647 | if (xis->not_found && xbs->not_found) { |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2648 | credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
| 2649 | |
| 2650 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) { |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2651 | clusters_add += new_clusters; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2652 | credits += ocfs2_calc_extend_credits(inode->i_sb, |
| 2653 | &def_xv.xv.xr_list, |
| 2654 | new_clusters); |
| 2655 | } |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2656 | |
| 2657 | goto meta_guess; |
| 2658 | } |
| 2659 | |
| 2660 | if (!xis->not_found) { |
| 2661 | xe = xis->here; |
| 2662 | name_offset = le16_to_cpu(xe->xe_name_offset); |
| 2663 | name_len = OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 2664 | base = xis->base; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2665 | credits += OCFS2_INODE_UPDATE_CREDITS; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2666 | } else { |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 2667 | int i, block_off = 0; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2668 | xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data; |
| 2669 | xe = xbs->here; |
| 2670 | name_offset = le16_to_cpu(xe->xe_name_offset); |
| 2671 | name_len = OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 2672 | i = xbs->here - xbs->header->xh_entries; |
| 2673 | old_in_xb = 1; |
| 2674 | |
| 2675 | if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) { |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 2676 | ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2677 | bucket_xh(xbs->bucket), |
| 2678 | i, &block_off, |
| 2679 | &name_offset); |
| 2680 | base = bucket_block(xbs->bucket, block_off); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2681 | credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
| 2682 | } else { |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2683 | base = xbs->base; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2684 | credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS; |
| 2685 | } |
| 2686 | } |
| 2687 | |
| 2688 | /* |
| 2689 | * delete a xattr doesn't need metadata and cluster allocation. |
| 2690 | * so just calculate the credits and return. |
| 2691 | * |
| 2692 | * The credits for removing the value tree will be extended |
| 2693 | * by ocfs2_remove_extent itself. |
| 2694 | */ |
| 2695 | if (!xi->value) { |
| 2696 | if (!ocfs2_xattr_is_local(xe)) |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 2697 | credits += ocfs2_remove_extent_credits(inode->i_sb); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2698 | |
| 2699 | goto out; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2700 | } |
| 2701 | |
| 2702 | /* do cluster allocation guess first. */ |
| 2703 | value_size = le64_to_cpu(xe->xe_value_size); |
| 2704 | |
| 2705 | if (old_in_xb) { |
| 2706 | /* |
| 2707 | * In xattr set, we always try to set the xe in inode first, |
| 2708 | * so if it can be inserted into inode successfully, the old |
| 2709 | * one will be removed from the xattr block, and this xattr |
| 2710 | * will be inserted into inode as a new xattr in inode. |
| 2711 | */ |
| 2712 | if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) { |
| 2713 | clusters_add += new_clusters; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 2714 | credits += ocfs2_remove_extent_credits(inode->i_sb) + |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2715 | OCFS2_INODE_UPDATE_CREDITS; |
| 2716 | if (!ocfs2_xattr_is_local(xe)) |
| 2717 | credits += ocfs2_calc_extend_credits( |
| 2718 | inode->i_sb, |
| 2719 | &def_xv.xv.xr_list, |
| 2720 | new_clusters); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2721 | goto out; |
| 2722 | } |
| 2723 | } |
| 2724 | |
| 2725 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 2726 | /* the new values will be stored outside. */ |
| 2727 | u32 old_clusters = 0; |
| 2728 | |
| 2729 | if (!ocfs2_xattr_is_local(xe)) { |
| 2730 | old_clusters = ocfs2_clusters_for_bytes(inode->i_sb, |
| 2731 | value_size); |
| 2732 | xv = (struct ocfs2_xattr_value_root *) |
| 2733 | (base + name_offset + name_len); |
Tao Ma | 97aff52 | 2008-11-19 16:48:41 +0800 | [diff] [blame] | 2734 | value_size = OCFS2_XATTR_ROOT_SIZE; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2735 | } else |
| 2736 | xv = &def_xv.xv; |
| 2737 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2738 | if (old_clusters >= new_clusters) { |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 2739 | credits += ocfs2_remove_extent_credits(inode->i_sb); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2740 | goto out; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2741 | } else { |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2742 | meta_add += ocfs2_extend_meta_needed(&xv->xr_list); |
| 2743 | clusters_add += new_clusters - old_clusters; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2744 | credits += ocfs2_calc_extend_credits(inode->i_sb, |
| 2745 | &xv->xr_list, |
| 2746 | new_clusters - |
| 2747 | old_clusters); |
Tao Ma | 97aff52 | 2008-11-19 16:48:41 +0800 | [diff] [blame] | 2748 | if (value_size >= OCFS2_XATTR_ROOT_SIZE) |
| 2749 | goto out; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2750 | } |
| 2751 | } else { |
| 2752 | /* |
| 2753 | * Now the new value will be stored inside. So if the new |
| 2754 | * value is smaller than the size of value root or the old |
| 2755 | * value, we don't need any allocation, otherwise we have |
| 2756 | * to guess metadata allocation. |
| 2757 | */ |
| 2758 | if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) || |
| 2759 | (!ocfs2_xattr_is_local(xe) && |
| 2760 | OCFS2_XATTR_ROOT_SIZE >= xi->value_len)) |
| 2761 | goto out; |
| 2762 | } |
| 2763 | |
| 2764 | meta_guess: |
| 2765 | /* calculate metadata allocation. */ |
| 2766 | if (di->i_xattr_loc) { |
| 2767 | if (!xbs->xattr_bh) { |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 2768 | ret = ocfs2_read_xattr_block(inode, |
| 2769 | le64_to_cpu(di->i_xattr_loc), |
| 2770 | &bh); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2771 | if (ret) { |
| 2772 | mlog_errno(ret); |
| 2773 | goto out; |
| 2774 | } |
| 2775 | |
| 2776 | xb = (struct ocfs2_xattr_block *)bh->b_data; |
| 2777 | } else |
| 2778 | xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data; |
| 2779 | |
Tao Ma | 90cb546 | 2008-12-05 06:20:56 +0800 | [diff] [blame] | 2780 | /* |
| 2781 | * If there is already an xattr tree, good, we can calculate |
| 2782 | * like other b-trees. Otherwise we may have the chance of |
| 2783 | * create a tree, the credit calculation is borrowed from |
| 2784 | * ocfs2_calc_extend_credits with root_el = NULL. And the |
| 2785 | * new tree will be cluster based, so no meta is needed. |
| 2786 | */ |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2787 | if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) { |
| 2788 | struct ocfs2_extent_list *el = |
| 2789 | &xb->xb_attrs.xb_root.xt_list; |
| 2790 | meta_add += ocfs2_extend_meta_needed(el); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2791 | credits += ocfs2_calc_extend_credits(inode->i_sb, |
| 2792 | el, 1); |
Tao Ma | 90cb546 | 2008-12-05 06:20:56 +0800 | [diff] [blame] | 2793 | } else |
| 2794 | credits += OCFS2_SUBALLOC_ALLOC + 1; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2795 | |
| 2796 | /* |
| 2797 | * This cluster will be used either for new bucket or for |
| 2798 | * new xattr block. |
| 2799 | * If the cluster size is the same as the bucket size, one |
| 2800 | * more is needed since we may need to extend the bucket |
| 2801 | * also. |
| 2802 | */ |
| 2803 | clusters_add += 1; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2804 | credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2805 | if (OCFS2_XATTR_BUCKET_SIZE == |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2806 | OCFS2_SB(inode->i_sb)->s_clustersize) { |
| 2807 | credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2808 | clusters_add += 1; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2809 | } |
| 2810 | } else { |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2811 | meta_add += 1; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2812 | credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS; |
| 2813 | } |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2814 | out: |
| 2815 | if (clusters_need) |
| 2816 | *clusters_need = clusters_add; |
| 2817 | if (meta_need) |
| 2818 | *meta_need = meta_add; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2819 | if (credits_need) |
| 2820 | *credits_need = credits; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2821 | brelse(bh); |
| 2822 | return ret; |
| 2823 | } |
| 2824 | |
| 2825 | static int ocfs2_init_xattr_set_ctxt(struct inode *inode, |
| 2826 | struct ocfs2_dinode *di, |
| 2827 | struct ocfs2_xattr_info *xi, |
| 2828 | struct ocfs2_xattr_search *xis, |
| 2829 | struct ocfs2_xattr_search *xbs, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2830 | struct ocfs2_xattr_set_ctxt *ctxt, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 2831 | int extra_meta, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2832 | int *credits) |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2833 | { |
| 2834 | int clusters_add, meta_add, ret; |
| 2835 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 2836 | |
| 2837 | memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt)); |
| 2838 | |
| 2839 | ocfs2_init_dealloc_ctxt(&ctxt->dealloc); |
| 2840 | |
| 2841 | ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2842 | &clusters_add, &meta_add, credits); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2843 | if (ret) { |
| 2844 | mlog_errno(ret); |
| 2845 | return ret; |
| 2846 | } |
| 2847 | |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 2848 | meta_add += extra_meta; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2849 | mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, " |
| 2850 | "credits = %d\n", xi->name, meta_add, clusters_add, *credits); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2851 | |
| 2852 | if (meta_add) { |
| 2853 | ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, |
| 2854 | &ctxt->meta_ac); |
| 2855 | if (ret) { |
| 2856 | mlog_errno(ret); |
| 2857 | goto out; |
| 2858 | } |
| 2859 | } |
| 2860 | |
| 2861 | if (clusters_add) { |
| 2862 | ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac); |
| 2863 | if (ret) |
| 2864 | mlog_errno(ret); |
| 2865 | } |
| 2866 | out: |
| 2867 | if (ret) { |
| 2868 | if (ctxt->meta_ac) { |
| 2869 | ocfs2_free_alloc_context(ctxt->meta_ac); |
| 2870 | ctxt->meta_ac = NULL; |
| 2871 | } |
| 2872 | |
| 2873 | /* |
| 2874 | * We cannot have an error and a non null ctxt->data_ac. |
| 2875 | */ |
| 2876 | } |
| 2877 | |
| 2878 | return ret; |
| 2879 | } |
| 2880 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2881 | static int __ocfs2_xattr_set_handle(struct inode *inode, |
| 2882 | struct ocfs2_dinode *di, |
| 2883 | struct ocfs2_xattr_info *xi, |
| 2884 | struct ocfs2_xattr_search *xis, |
| 2885 | struct ocfs2_xattr_search *xbs, |
| 2886 | struct ocfs2_xattr_set_ctxt *ctxt) |
| 2887 | { |
Tao Ma | 9f868f1 | 2008-11-19 16:48:42 +0800 | [diff] [blame] | 2888 | int ret = 0, credits, old_found; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2889 | |
| 2890 | if (!xi->value) { |
| 2891 | /* Remove existing extended attribute */ |
| 2892 | if (!xis->not_found) |
| 2893 | ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt); |
| 2894 | else if (!xbs->not_found) |
| 2895 | ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt); |
| 2896 | } else { |
| 2897 | /* We always try to set extended attribute into inode first*/ |
| 2898 | ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt); |
| 2899 | if (!ret && !xbs->not_found) { |
| 2900 | /* |
| 2901 | * If succeed and that extended attribute existing in |
| 2902 | * external block, then we will remove it. |
| 2903 | */ |
| 2904 | xi->value = NULL; |
| 2905 | xi->value_len = 0; |
| 2906 | |
Tao Ma | 9f868f1 | 2008-11-19 16:48:42 +0800 | [diff] [blame] | 2907 | old_found = xis->not_found; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2908 | xis->not_found = -ENODATA; |
| 2909 | ret = ocfs2_calc_xattr_set_need(inode, |
| 2910 | di, |
| 2911 | xi, |
| 2912 | xis, |
| 2913 | xbs, |
| 2914 | NULL, |
| 2915 | NULL, |
| 2916 | &credits); |
Tao Ma | 9f868f1 | 2008-11-19 16:48:42 +0800 | [diff] [blame] | 2917 | xis->not_found = old_found; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2918 | if (ret) { |
| 2919 | mlog_errno(ret); |
| 2920 | goto out; |
| 2921 | } |
| 2922 | |
| 2923 | ret = ocfs2_extend_trans(ctxt->handle, credits + |
| 2924 | ctxt->handle->h_buffer_credits); |
| 2925 | if (ret) { |
| 2926 | mlog_errno(ret); |
| 2927 | goto out; |
| 2928 | } |
| 2929 | ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt); |
| 2930 | } else if (ret == -ENOSPC) { |
| 2931 | if (di->i_xattr_loc && !xbs->xattr_bh) { |
| 2932 | ret = ocfs2_xattr_block_find(inode, |
| 2933 | xi->name_index, |
| 2934 | xi->name, xbs); |
| 2935 | if (ret) |
| 2936 | goto out; |
| 2937 | |
Tao Ma | 9f868f1 | 2008-11-19 16:48:42 +0800 | [diff] [blame] | 2938 | old_found = xis->not_found; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2939 | xis->not_found = -ENODATA; |
| 2940 | ret = ocfs2_calc_xattr_set_need(inode, |
| 2941 | di, |
| 2942 | xi, |
| 2943 | xis, |
| 2944 | xbs, |
| 2945 | NULL, |
| 2946 | NULL, |
| 2947 | &credits); |
Tao Ma | 9f868f1 | 2008-11-19 16:48:42 +0800 | [diff] [blame] | 2948 | xis->not_found = old_found; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2949 | if (ret) { |
| 2950 | mlog_errno(ret); |
| 2951 | goto out; |
| 2952 | } |
| 2953 | |
| 2954 | ret = ocfs2_extend_trans(ctxt->handle, credits + |
| 2955 | ctxt->handle->h_buffer_credits); |
| 2956 | if (ret) { |
| 2957 | mlog_errno(ret); |
| 2958 | goto out; |
| 2959 | } |
| 2960 | } |
| 2961 | /* |
| 2962 | * If no space in inode, we will set extended attribute |
| 2963 | * into external block. |
| 2964 | */ |
| 2965 | ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt); |
| 2966 | if (ret) |
| 2967 | goto out; |
| 2968 | if (!xis->not_found) { |
| 2969 | /* |
| 2970 | * If succeed and that extended attribute |
| 2971 | * existing in inode, we will remove it. |
| 2972 | */ |
| 2973 | xi->value = NULL; |
| 2974 | xi->value_len = 0; |
| 2975 | xbs->not_found = -ENODATA; |
| 2976 | ret = ocfs2_calc_xattr_set_need(inode, |
| 2977 | di, |
| 2978 | xi, |
| 2979 | xis, |
| 2980 | xbs, |
| 2981 | NULL, |
| 2982 | NULL, |
| 2983 | &credits); |
| 2984 | if (ret) { |
| 2985 | mlog_errno(ret); |
| 2986 | goto out; |
| 2987 | } |
| 2988 | |
| 2989 | ret = ocfs2_extend_trans(ctxt->handle, credits + |
| 2990 | ctxt->handle->h_buffer_credits); |
| 2991 | if (ret) { |
| 2992 | mlog_errno(ret); |
| 2993 | goto out; |
| 2994 | } |
| 2995 | ret = ocfs2_xattr_ibody_set(inode, xi, |
| 2996 | xis, ctxt); |
| 2997 | } |
| 2998 | } |
| 2999 | } |
| 3000 | |
Tao Ma | 4b3f620 | 2008-12-05 06:20:55 +0800 | [diff] [blame] | 3001 | if (!ret) { |
| 3002 | /* Update inode ctime. */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 3003 | ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), |
Tao Ma | 89a907a | 2009-02-17 04:39:28 +0800 | [diff] [blame] | 3004 | xis->inode_bh, |
| 3005 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 4b3f620 | 2008-12-05 06:20:55 +0800 | [diff] [blame] | 3006 | if (ret) { |
| 3007 | mlog_errno(ret); |
| 3008 | goto out; |
| 3009 | } |
| 3010 | |
| 3011 | inode->i_ctime = CURRENT_TIME; |
| 3012 | di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 3013 | di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 3014 | ocfs2_journal_dirty(ctxt->handle, xis->inode_bh); |
| 3015 | } |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3016 | out: |
| 3017 | return ret; |
| 3018 | } |
| 3019 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 3020 | /* |
Tiger Yang | 6c3faba | 2008-11-14 11:16:03 +0800 | [diff] [blame] | 3021 | * This function only called duing creating inode |
| 3022 | * for init security/acl xattrs of the new inode. |
Tiger Yang | 008aafa | 2008-12-09 16:43:08 +0800 | [diff] [blame] | 3023 | * All transanction credits have been reserved in mknod. |
Tiger Yang | 6c3faba | 2008-11-14 11:16:03 +0800 | [diff] [blame] | 3024 | */ |
| 3025 | int ocfs2_xattr_set_handle(handle_t *handle, |
| 3026 | struct inode *inode, |
| 3027 | struct buffer_head *di_bh, |
| 3028 | int name_index, |
| 3029 | const char *name, |
| 3030 | const void *value, |
| 3031 | size_t value_len, |
| 3032 | int flags, |
| 3033 | struct ocfs2_alloc_context *meta_ac, |
| 3034 | struct ocfs2_alloc_context *data_ac) |
| 3035 | { |
| 3036 | struct ocfs2_dinode *di; |
| 3037 | int ret; |
| 3038 | |
| 3039 | struct ocfs2_xattr_info xi = { |
| 3040 | .name_index = name_index, |
| 3041 | .name = name, |
| 3042 | .value = value, |
| 3043 | .value_len = value_len, |
| 3044 | }; |
| 3045 | |
| 3046 | struct ocfs2_xattr_search xis = { |
| 3047 | .not_found = -ENODATA, |
| 3048 | }; |
| 3049 | |
| 3050 | struct ocfs2_xattr_search xbs = { |
| 3051 | .not_found = -ENODATA, |
| 3052 | }; |
| 3053 | |
| 3054 | struct ocfs2_xattr_set_ctxt ctxt = { |
| 3055 | .handle = handle, |
| 3056 | .meta_ac = meta_ac, |
| 3057 | .data_ac = data_ac, |
| 3058 | }; |
| 3059 | |
| 3060 | if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) |
| 3061 | return -EOPNOTSUPP; |
| 3062 | |
Tiger Yang | 008aafa | 2008-12-09 16:43:08 +0800 | [diff] [blame] | 3063 | /* |
| 3064 | * In extreme situation, may need xattr bucket when |
| 3065 | * block size is too small. And we have already reserved |
| 3066 | * the credits for bucket in mknod. |
| 3067 | */ |
| 3068 | if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) { |
| 3069 | xbs.bucket = ocfs2_xattr_bucket_new(inode); |
| 3070 | if (!xbs.bucket) { |
| 3071 | mlog_errno(-ENOMEM); |
| 3072 | return -ENOMEM; |
| 3073 | } |
| 3074 | } |
| 3075 | |
Tiger Yang | 6c3faba | 2008-11-14 11:16:03 +0800 | [diff] [blame] | 3076 | xis.inode_bh = xbs.inode_bh = di_bh; |
| 3077 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 3078 | |
| 3079 | down_write(&OCFS2_I(inode)->ip_xattr_sem); |
| 3080 | |
| 3081 | ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis); |
| 3082 | if (ret) |
| 3083 | goto cleanup; |
| 3084 | if (xis.not_found) { |
| 3085 | ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs); |
| 3086 | if (ret) |
| 3087 | goto cleanup; |
| 3088 | } |
| 3089 | |
| 3090 | ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt); |
| 3091 | |
| 3092 | cleanup: |
| 3093 | up_write(&OCFS2_I(inode)->ip_xattr_sem); |
| 3094 | brelse(xbs.xattr_bh); |
Tiger Yang | 008aafa | 2008-12-09 16:43:08 +0800 | [diff] [blame] | 3095 | ocfs2_xattr_bucket_free(xbs.bucket); |
Tiger Yang | 6c3faba | 2008-11-14 11:16:03 +0800 | [diff] [blame] | 3096 | |
| 3097 | return ret; |
| 3098 | } |
| 3099 | |
| 3100 | /* |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 3101 | * ocfs2_xattr_set() |
| 3102 | * |
| 3103 | * Set, replace or remove an extended attribute for this inode. |
| 3104 | * value is NULL to remove an existing extended attribute, else either |
| 3105 | * create or replace an extended attribute. |
| 3106 | */ |
| 3107 | int ocfs2_xattr_set(struct inode *inode, |
| 3108 | int name_index, |
| 3109 | const char *name, |
| 3110 | const void *value, |
| 3111 | size_t value_len, |
| 3112 | int flags) |
| 3113 | { |
| 3114 | struct buffer_head *di_bh = NULL; |
| 3115 | struct ocfs2_dinode *di; |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 3116 | int ret, credits, ref_meta = 0, ref_credits = 0; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 3117 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3118 | struct inode *tl_inode = osb->osb_tl_inode; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 3119 | struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, }; |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 3120 | struct ocfs2_refcount_tree *ref_tree = NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 3121 | |
| 3122 | struct ocfs2_xattr_info xi = { |
| 3123 | .name_index = name_index, |
| 3124 | .name = name, |
| 3125 | .value = value, |
| 3126 | .value_len = value_len, |
| 3127 | }; |
| 3128 | |
| 3129 | struct ocfs2_xattr_search xis = { |
| 3130 | .not_found = -ENODATA, |
| 3131 | }; |
| 3132 | |
| 3133 | struct ocfs2_xattr_search xbs = { |
| 3134 | .not_found = -ENODATA, |
| 3135 | }; |
| 3136 | |
Tiger Yang | 8154da3 | 2008-08-18 17:11:46 +0800 | [diff] [blame] | 3137 | if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) |
| 3138 | return -EOPNOTSUPP; |
| 3139 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3140 | /* |
| 3141 | * Only xbs will be used on indexed trees. xis doesn't need a |
| 3142 | * bucket. |
| 3143 | */ |
| 3144 | xbs.bucket = ocfs2_xattr_bucket_new(inode); |
| 3145 | if (!xbs.bucket) { |
| 3146 | mlog_errno(-ENOMEM); |
| 3147 | return -ENOMEM; |
| 3148 | } |
| 3149 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 3150 | ret = ocfs2_inode_lock(inode, &di_bh, 1); |
| 3151 | if (ret < 0) { |
| 3152 | mlog_errno(ret); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3153 | goto cleanup_nolock; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 3154 | } |
| 3155 | xis.inode_bh = xbs.inode_bh = di_bh; |
| 3156 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 3157 | |
| 3158 | down_write(&OCFS2_I(inode)->ip_xattr_sem); |
| 3159 | /* |
| 3160 | * Scan inode and external block to find the same name |
| 3161 | * extended attribute and collect search infomation. |
| 3162 | */ |
| 3163 | ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis); |
| 3164 | if (ret) |
| 3165 | goto cleanup; |
| 3166 | if (xis.not_found) { |
| 3167 | ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs); |
| 3168 | if (ret) |
| 3169 | goto cleanup; |
| 3170 | } |
| 3171 | |
| 3172 | if (xis.not_found && xbs.not_found) { |
| 3173 | ret = -ENODATA; |
| 3174 | if (flags & XATTR_REPLACE) |
| 3175 | goto cleanup; |
| 3176 | ret = 0; |
| 3177 | if (!value) |
| 3178 | goto cleanup; |
| 3179 | } else { |
| 3180 | ret = -EEXIST; |
| 3181 | if (flags & XATTR_CREATE) |
| 3182 | goto cleanup; |
| 3183 | } |
| 3184 | |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 3185 | /* Check whether the value is refcounted and do some prepartion. */ |
| 3186 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL && |
| 3187 | (!xis.not_found || !xbs.not_found)) { |
| 3188 | ret = ocfs2_prepare_refcount_xattr(inode, di, &xi, |
| 3189 | &xis, &xbs, &ref_tree, |
| 3190 | &ref_meta, &ref_credits); |
| 3191 | if (ret) { |
| 3192 | mlog_errno(ret); |
| 3193 | goto cleanup; |
| 3194 | } |
| 3195 | } |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3196 | |
| 3197 | mutex_lock(&tl_inode->i_mutex); |
| 3198 | |
| 3199 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 3200 | ret = __ocfs2_flush_truncate_log(osb); |
| 3201 | if (ret < 0) { |
| 3202 | mutex_unlock(&tl_inode->i_mutex); |
| 3203 | mlog_errno(ret); |
| 3204 | goto cleanup; |
| 3205 | } |
| 3206 | } |
| 3207 | mutex_unlock(&tl_inode->i_mutex); |
| 3208 | |
| 3209 | ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 3210 | &xbs, &ctxt, ref_meta, &credits); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 3211 | if (ret) { |
| 3212 | mlog_errno(ret); |
| 3213 | goto cleanup; |
| 3214 | } |
| 3215 | |
Tao Ma | 4b3f620 | 2008-12-05 06:20:55 +0800 | [diff] [blame] | 3216 | /* we need to update inode's ctime field, so add credit for it. */ |
| 3217 | credits += OCFS2_INODE_UPDATE_CREDITS; |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 3218 | ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3219 | if (IS_ERR(ctxt.handle)) { |
| 3220 | ret = PTR_ERR(ctxt.handle); |
| 3221 | mlog_errno(ret); |
| 3222 | goto cleanup; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 3223 | } |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3224 | |
| 3225 | ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt); |
| 3226 | |
| 3227 | ocfs2_commit_trans(osb, ctxt.handle); |
| 3228 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 3229 | if (ctxt.data_ac) |
| 3230 | ocfs2_free_alloc_context(ctxt.data_ac); |
| 3231 | if (ctxt.meta_ac) |
| 3232 | ocfs2_free_alloc_context(ctxt.meta_ac); |
| 3233 | if (ocfs2_dealloc_has_cluster(&ctxt.dealloc)) |
| 3234 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 3235 | ocfs2_run_deallocs(osb, &ctxt.dealloc); |
Tao Ma | 8b2c0db | 2009-08-18 11:43:49 +0800 | [diff] [blame] | 3236 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 3237 | cleanup: |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 3238 | if (ref_tree) |
| 3239 | ocfs2_unlock_refcount_tree(osb, ref_tree, 1); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 3240 | up_write(&OCFS2_I(inode)->ip_xattr_sem); |
Tao Ma | 8b2c0db | 2009-08-18 11:43:49 +0800 | [diff] [blame] | 3241 | if (!value && !ret) { |
| 3242 | ret = ocfs2_try_remove_refcount_tree(inode, di_bh); |
| 3243 | if (ret) |
| 3244 | mlog_errno(ret); |
| 3245 | } |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 3246 | ocfs2_inode_unlock(inode, 1); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3247 | cleanup_nolock: |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 3248 | brelse(di_bh); |
| 3249 | brelse(xbs.xattr_bh); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3250 | ocfs2_xattr_bucket_free(xbs.bucket); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 3251 | |
| 3252 | return ret; |
| 3253 | } |
| 3254 | |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3255 | /* |
| 3256 | * Find the xattr extent rec which may contains name_hash. |
| 3257 | * e_cpos will be the first name hash of the xattr rec. |
| 3258 | * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list. |
| 3259 | */ |
| 3260 | static int ocfs2_xattr_get_rec(struct inode *inode, |
| 3261 | u32 name_hash, |
| 3262 | u64 *p_blkno, |
| 3263 | u32 *e_cpos, |
| 3264 | u32 *num_clusters, |
| 3265 | struct ocfs2_extent_list *el) |
| 3266 | { |
| 3267 | int ret = 0, i; |
| 3268 | struct buffer_head *eb_bh = NULL; |
| 3269 | struct ocfs2_extent_block *eb; |
| 3270 | struct ocfs2_extent_rec *rec = NULL; |
| 3271 | u64 e_blkno = 0; |
| 3272 | |
| 3273 | if (el->l_tree_depth) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 3274 | ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash, |
| 3275 | &eb_bh); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3276 | if (ret) { |
| 3277 | mlog_errno(ret); |
| 3278 | goto out; |
| 3279 | } |
| 3280 | |
| 3281 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 3282 | el = &eb->h_list; |
| 3283 | |
| 3284 | if (el->l_tree_depth) { |
| 3285 | ocfs2_error(inode->i_sb, |
| 3286 | "Inode %lu has non zero tree depth in " |
| 3287 | "xattr tree block %llu\n", inode->i_ino, |
| 3288 | (unsigned long long)eb_bh->b_blocknr); |
| 3289 | ret = -EROFS; |
| 3290 | goto out; |
| 3291 | } |
| 3292 | } |
| 3293 | |
| 3294 | for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) { |
| 3295 | rec = &el->l_recs[i]; |
| 3296 | |
| 3297 | if (le32_to_cpu(rec->e_cpos) <= name_hash) { |
| 3298 | e_blkno = le64_to_cpu(rec->e_blkno); |
| 3299 | break; |
| 3300 | } |
| 3301 | } |
| 3302 | |
| 3303 | if (!e_blkno) { |
| 3304 | ocfs2_error(inode->i_sb, "Inode %lu has bad extent " |
| 3305 | "record (%u, %u, 0) in xattr", inode->i_ino, |
| 3306 | le32_to_cpu(rec->e_cpos), |
| 3307 | ocfs2_rec_clusters(el, rec)); |
| 3308 | ret = -EROFS; |
| 3309 | goto out; |
| 3310 | } |
| 3311 | |
| 3312 | *p_blkno = le64_to_cpu(rec->e_blkno); |
| 3313 | *num_clusters = le16_to_cpu(rec->e_leaf_clusters); |
| 3314 | if (e_cpos) |
| 3315 | *e_cpos = le32_to_cpu(rec->e_cpos); |
| 3316 | out: |
| 3317 | brelse(eb_bh); |
| 3318 | return ret; |
| 3319 | } |
| 3320 | |
| 3321 | typedef int (xattr_bucket_func)(struct inode *inode, |
| 3322 | struct ocfs2_xattr_bucket *bucket, |
| 3323 | void *para); |
| 3324 | |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3325 | static int ocfs2_find_xe_in_bucket(struct inode *inode, |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3326 | struct ocfs2_xattr_bucket *bucket, |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3327 | int name_index, |
| 3328 | const char *name, |
| 3329 | u32 name_hash, |
| 3330 | u16 *xe_index, |
| 3331 | int *found) |
| 3332 | { |
| 3333 | int i, ret = 0, cmp = 1, block_off, new_offset; |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3334 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3335 | size_t name_len = strlen(name); |
| 3336 | struct ocfs2_xattr_entry *xe = NULL; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3337 | char *xe_name; |
| 3338 | |
| 3339 | /* |
| 3340 | * We don't use binary search in the bucket because there |
| 3341 | * may be multiple entries with the same name hash. |
| 3342 | */ |
| 3343 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { |
| 3344 | xe = &xh->xh_entries[i]; |
| 3345 | |
| 3346 | if (name_hash > le32_to_cpu(xe->xe_name_hash)) |
| 3347 | continue; |
| 3348 | else if (name_hash < le32_to_cpu(xe->xe_name_hash)) |
| 3349 | break; |
| 3350 | |
| 3351 | cmp = name_index - ocfs2_xattr_get_type(xe); |
| 3352 | if (!cmp) |
| 3353 | cmp = name_len - xe->xe_name_len; |
| 3354 | if (cmp) |
| 3355 | continue; |
| 3356 | |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 3357 | ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3358 | xh, |
| 3359 | i, |
| 3360 | &block_off, |
| 3361 | &new_offset); |
| 3362 | if (ret) { |
| 3363 | mlog_errno(ret); |
| 3364 | break; |
| 3365 | } |
| 3366 | |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 3367 | |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3368 | xe_name = bucket_block(bucket, block_off) + new_offset; |
| 3369 | if (!memcmp(name, xe_name, name_len)) { |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3370 | *xe_index = i; |
| 3371 | *found = 1; |
| 3372 | ret = 0; |
| 3373 | break; |
| 3374 | } |
| 3375 | } |
| 3376 | |
| 3377 | return ret; |
| 3378 | } |
| 3379 | |
| 3380 | /* |
| 3381 | * Find the specified xattr entry in a series of buckets. |
| 3382 | * This series start from p_blkno and last for num_clusters. |
| 3383 | * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains |
| 3384 | * the num of the valid buckets. |
| 3385 | * |
| 3386 | * Return the buffer_head this xattr should reside in. And if the xattr's |
| 3387 | * hash is in the gap of 2 buckets, return the lower bucket. |
| 3388 | */ |
| 3389 | static int ocfs2_xattr_bucket_find(struct inode *inode, |
| 3390 | int name_index, |
| 3391 | const char *name, |
| 3392 | u32 name_hash, |
| 3393 | u64 p_blkno, |
| 3394 | u32 first_hash, |
| 3395 | u32 num_clusters, |
| 3396 | struct ocfs2_xattr_search *xs) |
| 3397 | { |
| 3398 | int ret, found = 0; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3399 | struct ocfs2_xattr_header *xh = NULL; |
| 3400 | struct ocfs2_xattr_entry *xe = NULL; |
| 3401 | u16 index = 0; |
| 3402 | u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
| 3403 | int low_bucket = 0, bucket, high_bucket; |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3404 | struct ocfs2_xattr_bucket *search; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3405 | u32 last_hash; |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3406 | u64 blkno, lower_blkno = 0; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3407 | |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3408 | search = ocfs2_xattr_bucket_new(inode); |
| 3409 | if (!search) { |
| 3410 | ret = -ENOMEM; |
| 3411 | mlog_errno(ret); |
| 3412 | goto out; |
| 3413 | } |
| 3414 | |
| 3415 | ret = ocfs2_read_xattr_bucket(search, p_blkno); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3416 | if (ret) { |
| 3417 | mlog_errno(ret); |
| 3418 | goto out; |
| 3419 | } |
| 3420 | |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3421 | xh = bucket_xh(search); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3422 | high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3423 | while (low_bucket <= high_bucket) { |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3424 | ocfs2_xattr_bucket_relse(search); |
| 3425 | |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3426 | bucket = (low_bucket + high_bucket) / 2; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3427 | blkno = p_blkno + bucket * blk_per_bucket; |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3428 | ret = ocfs2_read_xattr_bucket(search, blkno); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3429 | if (ret) { |
| 3430 | mlog_errno(ret); |
| 3431 | goto out; |
| 3432 | } |
| 3433 | |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3434 | xh = bucket_xh(search); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3435 | xe = &xh->xh_entries[0]; |
| 3436 | if (name_hash < le32_to_cpu(xe->xe_name_hash)) { |
| 3437 | high_bucket = bucket - 1; |
| 3438 | continue; |
| 3439 | } |
| 3440 | |
| 3441 | /* |
| 3442 | * Check whether the hash of the last entry in our |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 3443 | * bucket is larger than the search one. for an empty |
| 3444 | * bucket, the last one is also the first one. |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3445 | */ |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 3446 | if (xh->xh_count) |
| 3447 | xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1]; |
| 3448 | |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3449 | last_hash = le32_to_cpu(xe->xe_name_hash); |
| 3450 | |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3451 | /* record lower_blkno which may be the insert place. */ |
| 3452 | lower_blkno = blkno; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3453 | |
| 3454 | if (name_hash > le32_to_cpu(xe->xe_name_hash)) { |
| 3455 | low_bucket = bucket + 1; |
| 3456 | continue; |
| 3457 | } |
| 3458 | |
| 3459 | /* the searched xattr should reside in this bucket if exists. */ |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3460 | ret = ocfs2_find_xe_in_bucket(inode, search, |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3461 | name_index, name, name_hash, |
| 3462 | &index, &found); |
| 3463 | if (ret) { |
| 3464 | mlog_errno(ret); |
| 3465 | goto out; |
| 3466 | } |
| 3467 | break; |
| 3468 | } |
| 3469 | |
| 3470 | /* |
| 3471 | * Record the bucket we have found. |
| 3472 | * When the xattr's hash value is in the gap of 2 buckets, we will |
| 3473 | * always set it to the previous bucket. |
| 3474 | */ |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3475 | if (!lower_blkno) |
| 3476 | lower_blkno = p_blkno; |
| 3477 | |
| 3478 | /* This should be in cache - we just read it during the search */ |
| 3479 | ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno); |
| 3480 | if (ret) { |
| 3481 | mlog_errno(ret); |
| 3482 | goto out; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3483 | } |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3484 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3485 | xs->header = bucket_xh(xs->bucket); |
| 3486 | xs->base = bucket_block(xs->bucket, 0); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3487 | xs->end = xs->base + inode->i_sb->s_blocksize; |
| 3488 | |
| 3489 | if (found) { |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3490 | xs->here = &xs->header->xh_entries[index]; |
| 3491 | mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3492 | (unsigned long long)bucket_blkno(xs->bucket), index); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3493 | } else |
| 3494 | ret = -ENODATA; |
| 3495 | |
| 3496 | out: |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3497 | ocfs2_xattr_bucket_free(search); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3498 | return ret; |
| 3499 | } |
| 3500 | |
| 3501 | static int ocfs2_xattr_index_block_find(struct inode *inode, |
| 3502 | struct buffer_head *root_bh, |
| 3503 | int name_index, |
| 3504 | const char *name, |
| 3505 | struct ocfs2_xattr_search *xs) |
| 3506 | { |
| 3507 | int ret; |
| 3508 | struct ocfs2_xattr_block *xb = |
| 3509 | (struct ocfs2_xattr_block *)root_bh->b_data; |
| 3510 | struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root; |
| 3511 | struct ocfs2_extent_list *el = &xb_root->xt_list; |
| 3512 | u64 p_blkno = 0; |
| 3513 | u32 first_hash, num_clusters = 0; |
Tao Ma | 2057e5c | 2008-10-09 23:06:13 +0800 | [diff] [blame] | 3514 | u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name)); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3515 | |
| 3516 | if (le16_to_cpu(el->l_next_free_rec) == 0) |
| 3517 | return -ENODATA; |
| 3518 | |
| 3519 | mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n", |
| 3520 | name, name_hash, name_index); |
| 3521 | |
| 3522 | ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash, |
| 3523 | &num_clusters, el); |
| 3524 | if (ret) { |
| 3525 | mlog_errno(ret); |
| 3526 | goto out; |
| 3527 | } |
| 3528 | |
| 3529 | BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash); |
| 3530 | |
| 3531 | 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] | 3532 | "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno, |
| 3533 | first_hash); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3534 | |
| 3535 | ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash, |
| 3536 | p_blkno, first_hash, num_clusters, xs); |
| 3537 | |
| 3538 | out: |
| 3539 | return ret; |
| 3540 | } |
| 3541 | |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3542 | static int ocfs2_iterate_xattr_buckets(struct inode *inode, |
| 3543 | u64 blkno, |
| 3544 | u32 clusters, |
| 3545 | xattr_bucket_func *func, |
| 3546 | void *para) |
| 3547 | { |
Joel Becker | 6dde41d | 2008-10-24 17:16:48 -0700 | [diff] [blame] | 3548 | int i, ret = 0; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3549 | u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)); |
| 3550 | u32 num_buckets = clusters * bpc; |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3551 | struct ocfs2_xattr_bucket *bucket; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3552 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3553 | bucket = ocfs2_xattr_bucket_new(inode); |
| 3554 | if (!bucket) { |
| 3555 | mlog_errno(-ENOMEM); |
| 3556 | return -ENOMEM; |
| 3557 | } |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3558 | |
| 3559 | mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n", |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 3560 | clusters, (unsigned long long)blkno); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3561 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3562 | for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) { |
| 3563 | ret = ocfs2_read_xattr_bucket(bucket, blkno); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3564 | if (ret) { |
| 3565 | mlog_errno(ret); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3566 | break; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3567 | } |
| 3568 | |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3569 | /* |
| 3570 | * The real bucket num in this series of blocks is stored |
| 3571 | * in the 1st bucket. |
| 3572 | */ |
| 3573 | if (i == 0) |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3574 | num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3575 | |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 3576 | mlog(0, "iterating xattr bucket %llu, first hash %u\n", |
| 3577 | (unsigned long long)blkno, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3578 | le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash)); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3579 | if (func) { |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3580 | ret = func(inode, bucket, para); |
Tao Ma | a46fa68 | 2009-05-04 05:18:09 +0800 | [diff] [blame] | 3581 | if (ret && ret != -ERANGE) |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3582 | mlog_errno(ret); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3583 | /* Fall through to bucket_relse() */ |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3584 | } |
| 3585 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3586 | ocfs2_xattr_bucket_relse(bucket); |
| 3587 | if (ret) |
| 3588 | break; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3589 | } |
| 3590 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3591 | ocfs2_xattr_bucket_free(bucket); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3592 | return ret; |
| 3593 | } |
| 3594 | |
| 3595 | struct ocfs2_xattr_tree_list { |
| 3596 | char *buffer; |
| 3597 | size_t buffer_size; |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3598 | size_t result; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3599 | }; |
| 3600 | |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 3601 | static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb, |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3602 | struct ocfs2_xattr_header *xh, |
| 3603 | int index, |
| 3604 | int *block_off, |
| 3605 | int *new_offset) |
| 3606 | { |
| 3607 | u16 name_offset; |
| 3608 | |
| 3609 | if (index < 0 || index >= le16_to_cpu(xh->xh_count)) |
| 3610 | return -EINVAL; |
| 3611 | |
| 3612 | name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset); |
| 3613 | |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 3614 | *block_off = name_offset >> sb->s_blocksize_bits; |
| 3615 | *new_offset = name_offset % sb->s_blocksize; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3616 | |
| 3617 | return 0; |
| 3618 | } |
| 3619 | |
| 3620 | static int ocfs2_list_xattr_bucket(struct inode *inode, |
| 3621 | struct ocfs2_xattr_bucket *bucket, |
| 3622 | void *para) |
| 3623 | { |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3624 | int ret = 0, type; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3625 | struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3626 | int i, block_off, new_offset; |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3627 | const char *prefix, *name; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3628 | |
Joel Becker | 3e63294 | 2008-10-24 17:04:49 -0700 | [diff] [blame] | 3629 | for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) { |
| 3630 | struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i]; |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3631 | type = ocfs2_xattr_get_type(entry); |
| 3632 | prefix = ocfs2_xattr_prefix(type); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3633 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3634 | if (prefix) { |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 3635 | ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, |
Joel Becker | 3e63294 | 2008-10-24 17:04:49 -0700 | [diff] [blame] | 3636 | bucket_xh(bucket), |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3637 | i, |
| 3638 | &block_off, |
| 3639 | &new_offset); |
| 3640 | if (ret) |
| 3641 | break; |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3642 | |
Joel Becker | 51def39 | 2008-10-24 16:57:21 -0700 | [diff] [blame] | 3643 | name = (const char *)bucket_block(bucket, block_off) + |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3644 | new_offset; |
| 3645 | ret = ocfs2_xattr_list_entry(xl->buffer, |
| 3646 | xl->buffer_size, |
| 3647 | &xl->result, |
| 3648 | prefix, name, |
| 3649 | entry->xe_name_len); |
| 3650 | if (ret) |
| 3651 | break; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3652 | } |
| 3653 | } |
| 3654 | |
| 3655 | return ret; |
| 3656 | } |
| 3657 | |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 3658 | static int ocfs2_iterate_xattr_index_block(struct inode *inode, |
| 3659 | struct buffer_head *blk_bh, |
| 3660 | xattr_tree_rec_func *rec_func, |
| 3661 | void *para) |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3662 | { |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 3663 | struct ocfs2_xattr_block *xb = |
| 3664 | (struct ocfs2_xattr_block *)blk_bh->b_data; |
| 3665 | struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3666 | int ret = 0; |
| 3667 | u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0; |
| 3668 | u64 p_blkno = 0; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3669 | |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 3670 | if (!el->l_next_free_rec || !rec_func) |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3671 | return 0; |
| 3672 | |
| 3673 | while (name_hash > 0) { |
| 3674 | ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, |
| 3675 | &e_cpos, &num_clusters, el); |
| 3676 | if (ret) { |
| 3677 | mlog_errno(ret); |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 3678 | break; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3679 | } |
| 3680 | |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 3681 | ret = rec_func(inode, blk_bh, p_blkno, e_cpos, |
| 3682 | num_clusters, para); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3683 | if (ret) { |
Tao Ma | a46fa68 | 2009-05-04 05:18:09 +0800 | [diff] [blame] | 3684 | if (ret != -ERANGE) |
| 3685 | mlog_errno(ret); |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 3686 | break; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3687 | } |
| 3688 | |
| 3689 | if (e_cpos == 0) |
| 3690 | break; |
| 3691 | |
| 3692 | name_hash = e_cpos - 1; |
| 3693 | } |
| 3694 | |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 3695 | return ret; |
| 3696 | |
| 3697 | } |
| 3698 | |
| 3699 | static int ocfs2_list_xattr_tree_rec(struct inode *inode, |
| 3700 | struct buffer_head *root_bh, |
| 3701 | u64 blkno, u32 cpos, u32 len, void *para) |
| 3702 | { |
| 3703 | return ocfs2_iterate_xattr_buckets(inode, blkno, len, |
| 3704 | ocfs2_list_xattr_bucket, para); |
| 3705 | } |
| 3706 | |
| 3707 | static int ocfs2_xattr_tree_list_index_block(struct inode *inode, |
| 3708 | struct buffer_head *blk_bh, |
| 3709 | char *buffer, |
| 3710 | size_t buffer_size) |
| 3711 | { |
| 3712 | int ret; |
| 3713 | struct ocfs2_xattr_tree_list xl = { |
| 3714 | .buffer = buffer, |
| 3715 | .buffer_size = buffer_size, |
| 3716 | .result = 0, |
| 3717 | }; |
| 3718 | |
| 3719 | ret = ocfs2_iterate_xattr_index_block(inode, blk_bh, |
| 3720 | ocfs2_list_xattr_tree_rec, &xl); |
| 3721 | if (ret) { |
| 3722 | mlog_errno(ret); |
| 3723 | goto out; |
| 3724 | } |
| 3725 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3726 | ret = xl.result; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3727 | out: |
| 3728 | return ret; |
| 3729 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3730 | |
| 3731 | static int cmp_xe(const void *a, const void *b) |
| 3732 | { |
| 3733 | const struct ocfs2_xattr_entry *l = a, *r = b; |
| 3734 | u32 l_hash = le32_to_cpu(l->xe_name_hash); |
| 3735 | u32 r_hash = le32_to_cpu(r->xe_name_hash); |
| 3736 | |
| 3737 | if (l_hash > r_hash) |
| 3738 | return 1; |
| 3739 | if (l_hash < r_hash) |
| 3740 | return -1; |
| 3741 | return 0; |
| 3742 | } |
| 3743 | |
| 3744 | static void swap_xe(void *a, void *b, int size) |
| 3745 | { |
| 3746 | struct ocfs2_xattr_entry *l = a, *r = b, tmp; |
| 3747 | |
| 3748 | tmp = *l; |
| 3749 | memcpy(l, r, sizeof(struct ocfs2_xattr_entry)); |
| 3750 | memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry)); |
| 3751 | } |
| 3752 | |
| 3753 | /* |
| 3754 | * When the ocfs2_xattr_block is filled up, new bucket will be created |
| 3755 | * and all the xattr entries will be moved to the new bucket. |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3756 | * The header goes at the start of the bucket, and the names+values are |
| 3757 | * 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] | 3758 | * Note: we need to sort the entries since they are not saved in order |
| 3759 | * in the ocfs2_xattr_block. |
| 3760 | */ |
| 3761 | static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode, |
| 3762 | struct buffer_head *xb_bh, |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3763 | struct ocfs2_xattr_bucket *bucket) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3764 | { |
| 3765 | int i, blocksize = inode->i_sb->s_blocksize; |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3766 | int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3767 | u16 offset, size, off_change; |
| 3768 | struct ocfs2_xattr_entry *xe; |
| 3769 | struct ocfs2_xattr_block *xb = |
| 3770 | (struct ocfs2_xattr_block *)xb_bh->b_data; |
| 3771 | struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header; |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3772 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3773 | u16 count = le16_to_cpu(xb_xh->xh_count); |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3774 | char *src = xb_bh->b_data; |
| 3775 | char *target = bucket_block(bucket, blks - 1); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3776 | |
| 3777 | mlog(0, "cp xattr from block %llu to bucket %llu\n", |
| 3778 | (unsigned long long)xb_bh->b_blocknr, |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3779 | (unsigned long long)bucket_blkno(bucket)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3780 | |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3781 | for (i = 0; i < blks; i++) |
| 3782 | memset(bucket_block(bucket, i), 0, blocksize); |
| 3783 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3784 | /* |
| 3785 | * Since the xe_name_offset is based on ocfs2_xattr_header, |
| 3786 | * there is a offset change corresponding to the change of |
| 3787 | * ocfs2_xattr_header's position. |
| 3788 | */ |
| 3789 | off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); |
| 3790 | xe = &xb_xh->xh_entries[count - 1]; |
| 3791 | offset = le16_to_cpu(xe->xe_name_offset) + off_change; |
| 3792 | size = blocksize - offset; |
| 3793 | |
| 3794 | /* copy all the names and values. */ |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3795 | memcpy(target + offset, src + offset, size); |
| 3796 | |
| 3797 | /* Init new header now. */ |
| 3798 | xh->xh_count = xb_xh->xh_count; |
| 3799 | xh->xh_num_buckets = cpu_to_le16(1); |
| 3800 | xh->xh_name_value_len = cpu_to_le16(size); |
| 3801 | xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size); |
| 3802 | |
| 3803 | /* copy all the entries. */ |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3804 | target = bucket_block(bucket, 0); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3805 | offset = offsetof(struct ocfs2_xattr_header, xh_entries); |
| 3806 | size = count * sizeof(struct ocfs2_xattr_entry); |
| 3807 | memcpy(target + offset, (char *)xb_xh + offset, size); |
| 3808 | |
| 3809 | /* Change the xe offset for all the xe because of the move. */ |
| 3810 | off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize + |
| 3811 | offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); |
| 3812 | for (i = 0; i < count; i++) |
| 3813 | le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change); |
| 3814 | |
| 3815 | mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n", |
| 3816 | offset, size, off_change); |
| 3817 | |
| 3818 | sort(target + offset, count, sizeof(struct ocfs2_xattr_entry), |
| 3819 | cmp_xe, swap_xe); |
| 3820 | } |
| 3821 | |
| 3822 | /* |
| 3823 | * After we move xattr from block to index btree, we have to |
| 3824 | * update ocfs2_xattr_search to the new xe and base. |
| 3825 | * |
| 3826 | * When the entry is in xattr block, xattr_bh indicates the storage place. |
| 3827 | * While if the entry is in index b-tree, "bucket" indicates the |
| 3828 | * real place of the xattr. |
| 3829 | */ |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3830 | static void ocfs2_xattr_update_xattr_search(struct inode *inode, |
| 3831 | struct ocfs2_xattr_search *xs, |
| 3832 | struct buffer_head *old_bh) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3833 | { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3834 | char *buf = old_bh->b_data; |
| 3835 | struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf; |
| 3836 | struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header; |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3837 | int i; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3838 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3839 | xs->header = bucket_xh(xs->bucket); |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3840 | xs->base = bucket_block(xs->bucket, 0); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3841 | xs->end = xs->base + inode->i_sb->s_blocksize; |
| 3842 | |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3843 | if (xs->not_found) |
| 3844 | return; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3845 | |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3846 | i = xs->here - old_xh->xh_entries; |
| 3847 | xs->here = &xs->header->xh_entries[i]; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3848 | } |
| 3849 | |
| 3850 | static int ocfs2_xattr_create_index_block(struct inode *inode, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 3851 | struct ocfs2_xattr_search *xs, |
| 3852 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3853 | { |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3854 | int ret; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3855 | u32 bit_off, len; |
| 3856 | u64 blkno; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3857 | handle_t *handle = ctxt->handle; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3858 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 3859 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3860 | struct buffer_head *xb_bh = xs->xattr_bh; |
| 3861 | struct ocfs2_xattr_block *xb = |
| 3862 | (struct ocfs2_xattr_block *)xb_bh->b_data; |
| 3863 | struct ocfs2_xattr_tree_root *xr; |
| 3864 | u16 xb_flags = le16_to_cpu(xb->xb_flags); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3865 | |
| 3866 | mlog(0, "create xattr index block for %llu\n", |
| 3867 | (unsigned long long)xb_bh->b_blocknr); |
| 3868 | |
| 3869 | BUG_ON(xb_flags & OCFS2_XATTR_INDEXED); |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3870 | BUG_ON(!xs->bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3871 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3872 | /* |
| 3873 | * XXX: |
| 3874 | * We can use this lock for now, and maybe move to a dedicated mutex |
| 3875 | * if performance becomes a problem later. |
| 3876 | */ |
| 3877 | down_write(&oi->ip_alloc_sem); |
| 3878 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 3879 | ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh, |
Joel Becker | 8400897 | 2008-12-09 16:11:49 -0800 | [diff] [blame] | 3880 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3881 | if (ret) { |
| 3882 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3883 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3884 | } |
| 3885 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 3886 | ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, |
| 3887 | 1, 1, &bit_off, &len); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3888 | if (ret) { |
| 3889 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3890 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3891 | } |
| 3892 | |
| 3893 | /* |
| 3894 | * The bucket may spread in many blocks, and |
| 3895 | * we will only touch the 1st block and the last block |
| 3896 | * in the whole bucket(one for entry and one for data). |
| 3897 | */ |
| 3898 | blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off); |
| 3899 | |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 3900 | mlog(0, "allocate 1 cluster from %llu to xattr block\n", |
| 3901 | (unsigned long long)blkno); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3902 | |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3903 | ret = ocfs2_init_xattr_bucket(xs->bucket, blkno); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3904 | if (ret) { |
| 3905 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3906 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3907 | } |
| 3908 | |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3909 | ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket, |
| 3910 | OCFS2_JOURNAL_ACCESS_CREATE); |
Joel Becker | bd60bd3 | 2008-10-20 18:25:56 -0700 | [diff] [blame] | 3911 | if (ret) { |
| 3912 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3913 | goto out; |
Joel Becker | bd60bd3 | 2008-10-20 18:25:56 -0700 | [diff] [blame] | 3914 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3915 | |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3916 | ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket); |
| 3917 | ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket); |
| 3918 | |
| 3919 | ocfs2_xattr_update_xattr_search(inode, xs, xb_bh); |
| 3920 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3921 | /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */ |
| 3922 | memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize - |
| 3923 | offsetof(struct ocfs2_xattr_block, xb_attrs)); |
| 3924 | |
| 3925 | xr = &xb->xb_attrs.xb_root; |
| 3926 | xr->xt_clusters = cpu_to_le32(1); |
| 3927 | xr->xt_last_eb_blk = 0; |
| 3928 | xr->xt_list.l_tree_depth = 0; |
| 3929 | xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb)); |
| 3930 | xr->xt_list.l_next_free_rec = cpu_to_le16(1); |
| 3931 | |
| 3932 | xr->xt_list.l_recs[0].e_cpos = 0; |
| 3933 | xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno); |
| 3934 | xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1); |
| 3935 | |
| 3936 | xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED); |
| 3937 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3938 | ocfs2_journal_dirty(handle, xb_bh); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3939 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3940 | out: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3941 | up_write(&oi->ip_alloc_sem); |
| 3942 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3943 | return ret; |
| 3944 | } |
| 3945 | |
| 3946 | static int cmp_xe_offset(const void *a, const void *b) |
| 3947 | { |
| 3948 | const struct ocfs2_xattr_entry *l = a, *r = b; |
| 3949 | u32 l_name_offset = le16_to_cpu(l->xe_name_offset); |
| 3950 | u32 r_name_offset = le16_to_cpu(r->xe_name_offset); |
| 3951 | |
| 3952 | if (l_name_offset < r_name_offset) |
| 3953 | return 1; |
| 3954 | if (l_name_offset > r_name_offset) |
| 3955 | return -1; |
| 3956 | return 0; |
| 3957 | } |
| 3958 | |
| 3959 | /* |
| 3960 | * defrag a xattr bucket if we find that the bucket has some |
| 3961 | * holes beteen name/value pairs. |
| 3962 | * We will move all the name/value pairs to the end of the bucket |
| 3963 | * so that we can spare some space for insertion. |
| 3964 | */ |
| 3965 | static int ocfs2_defrag_xattr_bucket(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3966 | handle_t *handle, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3967 | struct ocfs2_xattr_bucket *bucket) |
| 3968 | { |
| 3969 | int ret, i; |
| 3970 | size_t end, offset, len, value_len; |
| 3971 | struct ocfs2_xattr_header *xh; |
| 3972 | char *entries, *buf, *bucket_buf = NULL; |
Joel Becker | 9c7759a | 2008-10-24 16:21:03 -0700 | [diff] [blame] | 3973 | u64 blkno = bucket_blkno(bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3974 | u16 xh_free_start; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3975 | size_t blocksize = inode->i_sb->s_blocksize; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3976 | struct ocfs2_xattr_entry *xe; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3977 | |
| 3978 | /* |
| 3979 | * In order to make the operation more efficient and generic, |
| 3980 | * we copy all the blocks into a contiguous memory and do the |
| 3981 | * defragment there, so if anything is error, we will not touch |
| 3982 | * the real block. |
| 3983 | */ |
| 3984 | bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS); |
| 3985 | if (!bucket_buf) { |
| 3986 | ret = -EIO; |
| 3987 | goto out; |
| 3988 | } |
| 3989 | |
Joel Becker | 161d6f3 | 2008-10-27 15:25:18 -0700 | [diff] [blame] | 3990 | buf = bucket_buf; |
Tao Ma | 1c32a2f | 2008-11-06 08:10:47 +0800 | [diff] [blame] | 3991 | for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize) |
| 3992 | memcpy(buf, bucket_block(bucket, i), blocksize); |
Joel Becker | 161d6f3 | 2008-10-27 15:25:18 -0700 | [diff] [blame] | 3993 | |
Tao Ma | 1c32a2f | 2008-11-06 08:10:47 +0800 | [diff] [blame] | 3994 | ret = ocfs2_xattr_bucket_journal_access(handle, bucket, |
Joel Becker | 161d6f3 | 2008-10-27 15:25:18 -0700 | [diff] [blame] | 3995 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 3996 | if (ret < 0) { |
| 3997 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3998 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3999 | } |
| 4000 | |
| 4001 | xh = (struct ocfs2_xattr_header *)bucket_buf; |
| 4002 | entries = (char *)xh->xh_entries; |
| 4003 | xh_free_start = le16_to_cpu(xh->xh_free_start); |
| 4004 | |
| 4005 | mlog(0, "adjust xattr bucket in %llu, count = %u, " |
| 4006 | "xh_free_start = %u, xh_name_value_len = %u.\n", |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 4007 | (unsigned long long)blkno, le16_to_cpu(xh->xh_count), |
| 4008 | xh_free_start, le16_to_cpu(xh->xh_name_value_len)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4009 | |
| 4010 | /* |
| 4011 | * sort all the entries by their offset. |
| 4012 | * the largest will be the first, so that we can |
| 4013 | * move them to the end one by one. |
| 4014 | */ |
| 4015 | sort(entries, le16_to_cpu(xh->xh_count), |
| 4016 | sizeof(struct ocfs2_xattr_entry), |
| 4017 | cmp_xe_offset, swap_xe); |
| 4018 | |
| 4019 | /* Move all name/values to the end of the bucket. */ |
| 4020 | xe = xh->xh_entries; |
| 4021 | end = OCFS2_XATTR_BUCKET_SIZE; |
| 4022 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) { |
| 4023 | offset = le16_to_cpu(xe->xe_name_offset); |
| 4024 | if (ocfs2_xattr_is_local(xe)) |
| 4025 | value_len = OCFS2_XATTR_SIZE( |
| 4026 | le64_to_cpu(xe->xe_value_size)); |
| 4027 | else |
| 4028 | value_len = OCFS2_XATTR_ROOT_SIZE; |
| 4029 | len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len; |
| 4030 | |
| 4031 | /* |
| 4032 | * We must make sure that the name/value pair |
| 4033 | * exist in the same block. So adjust end to |
| 4034 | * the previous block end if needed. |
| 4035 | */ |
| 4036 | if (((end - len) / blocksize != |
| 4037 | (end - 1) / blocksize)) |
| 4038 | end = end - end % blocksize; |
| 4039 | |
| 4040 | if (end > offset + len) { |
| 4041 | memmove(bucket_buf + end - len, |
| 4042 | bucket_buf + offset, len); |
| 4043 | xe->xe_name_offset = cpu_to_le16(end - len); |
| 4044 | } |
| 4045 | |
| 4046 | mlog_bug_on_msg(end < offset + len, "Defrag check failed for " |
| 4047 | "bucket %llu\n", (unsigned long long)blkno); |
| 4048 | |
| 4049 | end -= len; |
| 4050 | } |
| 4051 | |
| 4052 | mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for " |
| 4053 | "bucket %llu\n", (unsigned long long)blkno); |
| 4054 | |
| 4055 | if (xh_free_start == end) |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4056 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4057 | |
| 4058 | memset(bucket_buf + xh_free_start, 0, end - xh_free_start); |
| 4059 | xh->xh_free_start = cpu_to_le16(end); |
| 4060 | |
| 4061 | /* sort the entries by their name_hash. */ |
| 4062 | sort(entries, le16_to_cpu(xh->xh_count), |
| 4063 | sizeof(struct ocfs2_xattr_entry), |
| 4064 | cmp_xe, swap_xe); |
| 4065 | |
| 4066 | buf = bucket_buf; |
Tao Ma | 1c32a2f | 2008-11-06 08:10:47 +0800 | [diff] [blame] | 4067 | for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize) |
| 4068 | memcpy(bucket_block(bucket, i), buf, blocksize); |
| 4069 | ocfs2_xattr_bucket_journal_dirty(handle, bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4070 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4071 | out: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4072 | kfree(bucket_buf); |
| 4073 | return ret; |
| 4074 | } |
| 4075 | |
| 4076 | /* |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 4077 | * prev_blkno points to the start of an existing extent. new_blkno |
| 4078 | * points to a newly allocated extent. Because we know each of our |
| 4079 | * clusters contains more than bucket, we can easily split one cluster |
| 4080 | * at a bucket boundary. So we take the last cluster of the existing |
| 4081 | * extent and split it down the middle. We move the last half of the |
| 4082 | * buckets in the last cluster of the existing extent over to the new |
| 4083 | * extent. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4084 | * |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 4085 | * first_bh is the buffer at prev_blkno so we can update the existing |
| 4086 | * extent's bucket count. header_bh is the bucket were we were hoping |
| 4087 | * to insert our xattr. If the bucket move places the target in the new |
| 4088 | * extent, we'll update first_bh and header_bh after modifying the old |
| 4089 | * extent. |
| 4090 | * |
| 4091 | * 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] | 4092 | */ |
| 4093 | static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode, |
| 4094 | handle_t *handle, |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4095 | struct ocfs2_xattr_bucket *first, |
| 4096 | struct ocfs2_xattr_bucket *target, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4097 | u64 new_blkno, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4098 | u32 num_clusters, |
| 4099 | u32 *first_hash) |
| 4100 | { |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 4101 | int ret; |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4102 | struct super_block *sb = inode->i_sb; |
| 4103 | int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb); |
| 4104 | int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb)); |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 4105 | int to_move = num_buckets / 2; |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 4106 | u64 src_blkno; |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4107 | u64 last_cluster_blkno = bucket_blkno(first) + |
| 4108 | ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4109 | |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4110 | BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets); |
| 4111 | BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4112 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4113 | mlog(0, "move half of xattrs in cluster %llu to %llu\n", |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 4114 | (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4115 | |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4116 | ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first), |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 4117 | last_cluster_blkno, new_blkno, |
| 4118 | to_move, first_hash); |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 4119 | if (ret) { |
| 4120 | mlog_errno(ret); |
| 4121 | goto out; |
| 4122 | } |
| 4123 | |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 4124 | /* This is the first bucket that got moved */ |
| 4125 | src_blkno = last_cluster_blkno + (to_move * blks_per_bucket); |
| 4126 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4127 | /* |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 4128 | * 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] | 4129 | * update first and target. |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 4130 | */ |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4131 | if (bucket_blkno(target) >= src_blkno) { |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 4132 | /* Find the block for the new target bucket */ |
| 4133 | src_blkno = new_blkno + |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4134 | (bucket_blkno(target) - src_blkno); |
| 4135 | |
| 4136 | ocfs2_xattr_bucket_relse(first); |
| 4137 | ocfs2_xattr_bucket_relse(target); |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 4138 | |
| 4139 | /* |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 4140 | * These shouldn't fail - the buffers are in the |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 4141 | * journal from ocfs2_cp_xattr_bucket(). |
| 4142 | */ |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4143 | ret = ocfs2_read_xattr_bucket(first, new_blkno); |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 4144 | if (ret) { |
| 4145 | mlog_errno(ret); |
| 4146 | goto out; |
| 4147 | } |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4148 | ret = ocfs2_read_xattr_bucket(target, src_blkno); |
| 4149 | if (ret) |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 4150 | mlog_errno(ret); |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 4151 | |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 4152 | } |
| 4153 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4154 | out: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4155 | return ret; |
| 4156 | } |
| 4157 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4158 | /* |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4159 | * Find the suitable pos when we divide a bucket into 2. |
| 4160 | * We have to make sure the xattrs with the same hash value exist |
| 4161 | * in the same bucket. |
| 4162 | * |
| 4163 | * If this ocfs2_xattr_header covers more than one hash value, find a |
| 4164 | * place where the hash value changes. Try to find the most even split. |
| 4165 | * The most common case is that all entries have different hash values, |
| 4166 | * and the first check we make will find a place to split. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4167 | */ |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4168 | static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh) |
| 4169 | { |
| 4170 | struct ocfs2_xattr_entry *entries = xh->xh_entries; |
| 4171 | int count = le16_to_cpu(xh->xh_count); |
| 4172 | int delta, middle = count / 2; |
| 4173 | |
| 4174 | /* |
| 4175 | * We start at the middle. Each step gets farther away in both |
| 4176 | * directions. We therefore hit the change in hash value |
| 4177 | * nearest to the middle. Note that this loop does not execute for |
| 4178 | * count < 2. |
| 4179 | */ |
| 4180 | for (delta = 0; delta < middle; delta++) { |
| 4181 | /* Let's check delta earlier than middle */ |
| 4182 | if (cmp_xe(&entries[middle - delta - 1], |
| 4183 | &entries[middle - delta])) |
| 4184 | return middle - delta; |
| 4185 | |
| 4186 | /* For even counts, don't walk off the end */ |
| 4187 | if ((middle + delta + 1) == count) |
| 4188 | continue; |
| 4189 | |
| 4190 | /* Now try delta past middle */ |
| 4191 | if (cmp_xe(&entries[middle + delta], |
| 4192 | &entries[middle + delta + 1])) |
| 4193 | return middle + delta + 1; |
| 4194 | } |
| 4195 | |
| 4196 | /* Every entry had the same hash */ |
| 4197 | return count; |
| 4198 | } |
| 4199 | |
| 4200 | /* |
| 4201 | * Move some xattrs in old bucket(blk) to new bucket(new_blk). |
| 4202 | * first_hash will record the 1st hash of the new bucket. |
| 4203 | * |
| 4204 | * Normally half of the xattrs will be moved. But we have to make |
| 4205 | * sure that the xattrs with the same hash value are stored in the |
| 4206 | * same bucket. If all the xattrs in this bucket have the same hash |
| 4207 | * value, the new bucket will be initialized as an empty one and the |
| 4208 | * first_hash will be initialized as (hash_value+1). |
| 4209 | */ |
| 4210 | static int ocfs2_divide_xattr_bucket(struct inode *inode, |
| 4211 | handle_t *handle, |
| 4212 | u64 blk, |
| 4213 | u64 new_blk, |
| 4214 | u32 *first_hash, |
| 4215 | int new_bucket_head) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4216 | { |
| 4217 | int ret, i; |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4218 | 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] | 4219 | struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4220 | struct ocfs2_xattr_header *xh; |
| 4221 | struct ocfs2_xattr_entry *xe; |
| 4222 | int blocksize = inode->i_sb->s_blocksize; |
| 4223 | |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4224 | mlog(0, "move some of xattrs from bucket %llu to %llu\n", |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 4225 | (unsigned long long)blk, (unsigned long long)new_blk); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4226 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4227 | s_bucket = ocfs2_xattr_bucket_new(inode); |
| 4228 | t_bucket = ocfs2_xattr_bucket_new(inode); |
| 4229 | if (!s_bucket || !t_bucket) { |
| 4230 | ret = -ENOMEM; |
| 4231 | mlog_errno(ret); |
| 4232 | goto out; |
| 4233 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4234 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4235 | ret = ocfs2_read_xattr_bucket(s_bucket, blk); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4236 | if (ret) { |
| 4237 | mlog_errno(ret); |
| 4238 | goto out; |
| 4239 | } |
| 4240 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4241 | ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 4242 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4243 | if (ret) { |
| 4244 | mlog_errno(ret); |
| 4245 | goto out; |
| 4246 | } |
| 4247 | |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 4248 | /* |
| 4249 | * Even if !new_bucket_head, we're overwriting t_bucket. Thus, |
| 4250 | * there's no need to read it. |
| 4251 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4252 | ret = ocfs2_init_xattr_bucket(t_bucket, new_blk); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4253 | if (ret) { |
| 4254 | mlog_errno(ret); |
| 4255 | goto out; |
| 4256 | } |
| 4257 | |
Joel Becker | 2b656c1 | 2008-11-25 19:00:15 -0800 | [diff] [blame] | 4258 | /* |
| 4259 | * Hey, if we're overwriting t_bucket, what difference does |
| 4260 | * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the |
| 4261 | * same part of ocfs2_cp_xattr_bucket(). |
| 4262 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4263 | ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 4264 | new_bucket_head ? |
| 4265 | OCFS2_JOURNAL_ACCESS_CREATE : |
| 4266 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 4267 | if (ret) { |
| 4268 | mlog_errno(ret); |
| 4269 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4270 | } |
| 4271 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4272 | xh = bucket_xh(s_bucket); |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4273 | count = le16_to_cpu(xh->xh_count); |
| 4274 | start = ocfs2_xattr_find_divide_pos(xh); |
| 4275 | |
| 4276 | if (start == count) { |
| 4277 | xe = &xh->xh_entries[start-1]; |
| 4278 | |
| 4279 | /* |
| 4280 | * initialized a new empty bucket here. |
| 4281 | * The hash value is set as one larger than |
| 4282 | * that of the last entry in the previous bucket. |
| 4283 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4284 | for (i = 0; i < t_bucket->bu_blocks; i++) |
| 4285 | memset(bucket_block(t_bucket, i), 0, blocksize); |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4286 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4287 | xh = bucket_xh(t_bucket); |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4288 | xh->xh_free_start = cpu_to_le16(blocksize); |
| 4289 | xh->xh_entries[0].xe_name_hash = xe->xe_name_hash; |
| 4290 | le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1); |
| 4291 | |
| 4292 | goto set_num_buckets; |
| 4293 | } |
| 4294 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4295 | /* copy the whole bucket to the new first. */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4296 | ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4297 | |
| 4298 | /* update the new bucket. */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4299 | xh = bucket_xh(t_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4300 | |
| 4301 | /* |
| 4302 | * Calculate the total name/value len and xh_free_start for |
| 4303 | * the old bucket first. |
| 4304 | */ |
| 4305 | name_offset = OCFS2_XATTR_BUCKET_SIZE; |
| 4306 | name_value_len = 0; |
| 4307 | for (i = 0; i < start; i++) { |
| 4308 | xe = &xh->xh_entries[i]; |
| 4309 | xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 4310 | if (ocfs2_xattr_is_local(xe)) |
| 4311 | xe_len += |
| 4312 | OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); |
| 4313 | else |
| 4314 | xe_len += OCFS2_XATTR_ROOT_SIZE; |
| 4315 | name_value_len += xe_len; |
| 4316 | if (le16_to_cpu(xe->xe_name_offset) < name_offset) |
| 4317 | name_offset = le16_to_cpu(xe->xe_name_offset); |
| 4318 | } |
| 4319 | |
| 4320 | /* |
| 4321 | * Now begin the modification to the new bucket. |
| 4322 | * |
| 4323 | * In the new bucket, We just move the xattr entry to the beginning |
| 4324 | * and don't touch the name/value. So there will be some holes in the |
| 4325 | * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is |
| 4326 | * called. |
| 4327 | */ |
| 4328 | xe = &xh->xh_entries[start]; |
| 4329 | len = sizeof(struct ocfs2_xattr_entry) * (count - start); |
| 4330 | 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] | 4331 | (int)((char *)xe - (char *)xh), |
| 4332 | (int)((char *)xh->xh_entries - (char *)xh)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4333 | memmove((char *)xh->xh_entries, (char *)xe, len); |
| 4334 | xe = &xh->xh_entries[count - start]; |
| 4335 | len = sizeof(struct ocfs2_xattr_entry) * start; |
| 4336 | memset((char *)xe, 0, len); |
| 4337 | |
| 4338 | le16_add_cpu(&xh->xh_count, -start); |
| 4339 | le16_add_cpu(&xh->xh_name_value_len, -name_value_len); |
| 4340 | |
| 4341 | /* Calculate xh_free_start for the new bucket. */ |
| 4342 | xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE); |
| 4343 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { |
| 4344 | xe = &xh->xh_entries[i]; |
| 4345 | xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 4346 | if (ocfs2_xattr_is_local(xe)) |
| 4347 | xe_len += |
| 4348 | OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); |
| 4349 | else |
| 4350 | xe_len += OCFS2_XATTR_ROOT_SIZE; |
| 4351 | if (le16_to_cpu(xe->xe_name_offset) < |
| 4352 | le16_to_cpu(xh->xh_free_start)) |
| 4353 | xh->xh_free_start = xe->xe_name_offset; |
| 4354 | } |
| 4355 | |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4356 | set_num_buckets: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4357 | /* set xh->xh_num_buckets for the new xh. */ |
| 4358 | if (new_bucket_head) |
| 4359 | xh->xh_num_buckets = cpu_to_le16(1); |
| 4360 | else |
| 4361 | xh->xh_num_buckets = 0; |
| 4362 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4363 | ocfs2_xattr_bucket_journal_dirty(handle, t_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4364 | |
| 4365 | /* store the first_hash of the new bucket. */ |
| 4366 | if (first_hash) |
| 4367 | *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash); |
| 4368 | |
| 4369 | /* |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4370 | * Now only update the 1st block of the old bucket. If we |
| 4371 | * just added a new empty bucket, there is no need to modify |
| 4372 | * it. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4373 | */ |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4374 | if (start == count) |
| 4375 | goto out; |
| 4376 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4377 | xh = bucket_xh(s_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4378 | memset(&xh->xh_entries[start], 0, |
| 4379 | sizeof(struct ocfs2_xattr_entry) * (count - start)); |
| 4380 | xh->xh_count = cpu_to_le16(start); |
| 4381 | xh->xh_free_start = cpu_to_le16(name_offset); |
| 4382 | xh->xh_name_value_len = cpu_to_le16(name_value_len); |
| 4383 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4384 | ocfs2_xattr_bucket_journal_dirty(handle, s_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4385 | |
| 4386 | out: |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4387 | ocfs2_xattr_bucket_free(s_bucket); |
| 4388 | ocfs2_xattr_bucket_free(t_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4389 | |
| 4390 | return ret; |
| 4391 | } |
| 4392 | |
| 4393 | /* |
| 4394 | * Copy xattr from one bucket to another bucket. |
| 4395 | * |
| 4396 | * The caller must make sure that the journal transaction |
| 4397 | * has enough space for journaling. |
| 4398 | */ |
| 4399 | static int ocfs2_cp_xattr_bucket(struct inode *inode, |
| 4400 | handle_t *handle, |
| 4401 | u64 s_blkno, |
| 4402 | u64 t_blkno, |
| 4403 | int t_is_new) |
| 4404 | { |
Joel Becker | 4980c6d | 2008-10-24 18:54:43 -0700 | [diff] [blame] | 4405 | int ret; |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4406 | struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4407 | |
| 4408 | BUG_ON(s_blkno == t_blkno); |
| 4409 | |
| 4410 | mlog(0, "cp bucket %llu to %llu, target is %d\n", |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 4411 | (unsigned long long)s_blkno, (unsigned long long)t_blkno, |
| 4412 | t_is_new); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4413 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4414 | s_bucket = ocfs2_xattr_bucket_new(inode); |
| 4415 | t_bucket = ocfs2_xattr_bucket_new(inode); |
| 4416 | if (!s_bucket || !t_bucket) { |
| 4417 | ret = -ENOMEM; |
| 4418 | mlog_errno(ret); |
| 4419 | goto out; |
| 4420 | } |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4421 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4422 | ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4423 | if (ret) |
| 4424 | goto out; |
| 4425 | |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 4426 | /* |
| 4427 | * Even if !t_is_new, we're overwriting t_bucket. Thus, |
| 4428 | * there's no need to read it. |
| 4429 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4430 | ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4431 | if (ret) |
| 4432 | goto out; |
| 4433 | |
Joel Becker | 2b656c1 | 2008-11-25 19:00:15 -0800 | [diff] [blame] | 4434 | /* |
| 4435 | * Hey, if we're overwriting t_bucket, what difference does |
| 4436 | * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new |
Joel Becker | 874d65a | 2008-11-26 13:02:18 -0800 | [diff] [blame] | 4437 | * cluster to fill, we came here from |
| 4438 | * ocfs2_mv_xattr_buckets(), and it is really new - |
| 4439 | * ACCESS_CREATE is required. But we also might have moved data |
| 4440 | * out of t_bucket before extending back into it. |
| 4441 | * ocfs2_add_new_xattr_bucket() can do this - its call to |
| 4442 | * ocfs2_add_new_xattr_cluster() may have created a new extent |
Joel Becker | 2b656c1 | 2008-11-25 19:00:15 -0800 | [diff] [blame] | 4443 | * and copied out the end of the old extent. Then it re-extends |
| 4444 | * the old extent back to create space for new xattrs. That's |
| 4445 | * how we get here, and the bucket isn't really new. |
| 4446 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4447 | ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 4448 | t_is_new ? |
| 4449 | OCFS2_JOURNAL_ACCESS_CREATE : |
| 4450 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 4451 | if (ret) |
| 4452 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4453 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4454 | ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket); |
| 4455 | ocfs2_xattr_bucket_journal_dirty(handle, t_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4456 | |
| 4457 | out: |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4458 | ocfs2_xattr_bucket_free(t_bucket); |
| 4459 | ocfs2_xattr_bucket_free(s_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4460 | |
| 4461 | return ret; |
| 4462 | } |
| 4463 | |
| 4464 | /* |
Joel Becker | 874d65a | 2008-11-26 13:02:18 -0800 | [diff] [blame] | 4465 | * src_blk points to the start of an existing extent. last_blk points to |
| 4466 | * last cluster in that extent. to_blk points to a newly allocated |
Joel Becker | 54ecb6b | 2008-11-26 13:18:31 -0800 | [diff] [blame] | 4467 | * extent. We copy the buckets from the cluster at last_blk to the new |
| 4468 | * extent. If start_bucket is non-zero, we skip that many buckets before |
| 4469 | * we start copying. The new extent's xh_num_buckets gets set to the |
| 4470 | * number of buckets we copied. The old extent's xh_num_buckets shrinks |
| 4471 | * by the same amount. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4472 | */ |
Joel Becker | 54ecb6b | 2008-11-26 13:18:31 -0800 | [diff] [blame] | 4473 | static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle, |
| 4474 | u64 src_blk, u64 last_blk, u64 to_blk, |
| 4475 | unsigned int start_bucket, |
| 4476 | u32 *first_hash) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4477 | { |
| 4478 | int i, ret, credits; |
| 4479 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4480 | int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4481 | int num_buckets = ocfs2_xattr_buckets_per_cluster(osb); |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4482 | struct ocfs2_xattr_bucket *old_first, *new_first; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4483 | |
Joel Becker | 874d65a | 2008-11-26 13:02:18 -0800 | [diff] [blame] | 4484 | mlog(0, "mv xattrs from cluster %llu to %llu\n", |
| 4485 | (unsigned long long)last_blk, (unsigned long long)to_blk); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4486 | |
Joel Becker | 54ecb6b | 2008-11-26 13:18:31 -0800 | [diff] [blame] | 4487 | BUG_ON(start_bucket >= num_buckets); |
| 4488 | if (start_bucket) { |
| 4489 | num_buckets -= start_bucket; |
| 4490 | last_blk += (start_bucket * blks_per_bucket); |
| 4491 | } |
| 4492 | |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4493 | /* The first bucket of the original extent */ |
| 4494 | old_first = ocfs2_xattr_bucket_new(inode); |
| 4495 | /* The first bucket of the new extent */ |
| 4496 | new_first = ocfs2_xattr_bucket_new(inode); |
| 4497 | if (!old_first || !new_first) { |
| 4498 | ret = -ENOMEM; |
| 4499 | mlog_errno(ret); |
| 4500 | goto out; |
| 4501 | } |
| 4502 | |
Joel Becker | 874d65a | 2008-11-26 13:02:18 -0800 | [diff] [blame] | 4503 | ret = ocfs2_read_xattr_bucket(old_first, src_blk); |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4504 | if (ret) { |
| 4505 | mlog_errno(ret); |
| 4506 | goto out; |
| 4507 | } |
| 4508 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4509 | /* |
Joel Becker | 54ecb6b | 2008-11-26 13:18:31 -0800 | [diff] [blame] | 4510 | * We need to update the first bucket of the old extent and all |
| 4511 | * the buckets going to the new extent. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4512 | */ |
Joel Becker | 54ecb6b | 2008-11-26 13:18:31 -0800 | [diff] [blame] | 4513 | credits = ((num_buckets + 1) * blks_per_bucket) + |
| 4514 | handle->h_buffer_credits; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4515 | ret = ocfs2_extend_trans(handle, credits); |
| 4516 | if (ret) { |
| 4517 | mlog_errno(ret); |
| 4518 | goto out; |
| 4519 | } |
| 4520 | |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4521 | ret = ocfs2_xattr_bucket_journal_access(handle, old_first, |
| 4522 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4523 | if (ret) { |
| 4524 | mlog_errno(ret); |
| 4525 | goto out; |
| 4526 | } |
| 4527 | |
| 4528 | for (i = 0; i < num_buckets; i++) { |
| 4529 | ret = ocfs2_cp_xattr_bucket(inode, handle, |
Joel Becker | 874d65a | 2008-11-26 13:02:18 -0800 | [diff] [blame] | 4530 | last_blk + (i * blks_per_bucket), |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4531 | to_blk + (i * blks_per_bucket), |
| 4532 | 1); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4533 | if (ret) { |
| 4534 | mlog_errno(ret); |
| 4535 | goto out; |
| 4536 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4537 | } |
| 4538 | |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4539 | /* |
| 4540 | * Get the new bucket ready before we dirty anything |
| 4541 | * (This actually shouldn't fail, because we already dirtied |
| 4542 | * it once in ocfs2_cp_xattr_bucket()). |
| 4543 | */ |
| 4544 | ret = ocfs2_read_xattr_bucket(new_first, to_blk); |
| 4545 | if (ret) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4546 | mlog_errno(ret); |
| 4547 | goto out; |
| 4548 | } |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4549 | ret = ocfs2_xattr_bucket_journal_access(handle, new_first, |
| 4550 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4551 | if (ret) { |
| 4552 | mlog_errno(ret); |
| 4553 | goto out; |
| 4554 | } |
| 4555 | |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4556 | /* Now update the headers */ |
| 4557 | le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets); |
| 4558 | ocfs2_xattr_bucket_journal_dirty(handle, old_first); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4559 | |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4560 | bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets); |
| 4561 | ocfs2_xattr_bucket_journal_dirty(handle, new_first); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4562 | |
| 4563 | if (first_hash) |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4564 | *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash); |
| 4565 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4566 | out: |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4567 | ocfs2_xattr_bucket_free(new_first); |
| 4568 | ocfs2_xattr_bucket_free(old_first); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4569 | return ret; |
| 4570 | } |
| 4571 | |
| 4572 | /* |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4573 | * Move some xattrs in this cluster to the new cluster. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4574 | * This function should only be called when bucket size == cluster size. |
| 4575 | * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead. |
| 4576 | */ |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4577 | static int ocfs2_divide_xattr_cluster(struct inode *inode, |
| 4578 | handle_t *handle, |
| 4579 | u64 prev_blk, |
| 4580 | u64 new_blk, |
| 4581 | u32 *first_hash) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4582 | { |
| 4583 | u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4584 | int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4585 | |
| 4586 | BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize); |
| 4587 | |
| 4588 | ret = ocfs2_extend_trans(handle, credits); |
| 4589 | if (ret) { |
| 4590 | mlog_errno(ret); |
| 4591 | return ret; |
| 4592 | } |
| 4593 | |
| 4594 | /* Move half of the xattr in start_blk to the next bucket. */ |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4595 | return ocfs2_divide_xattr_bucket(inode, handle, prev_blk, |
| 4596 | new_blk, first_hash, 1); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4597 | } |
| 4598 | |
| 4599 | /* |
| 4600 | * Move some xattrs from the old cluster to the new one since they are not |
| 4601 | * contiguous in ocfs2 xattr tree. |
| 4602 | * |
| 4603 | * new_blk starts a new separate cluster, and we will move some xattrs from |
| 4604 | * prev_blk to it. v_start will be set as the first name hash value in this |
| 4605 | * new cluster so that it can be used as e_cpos during tree insertion and |
| 4606 | * don't collide with our original b-tree operations. first_bh and header_bh |
| 4607 | * will also be updated since they will be used in ocfs2_extend_xattr_bucket |
| 4608 | * to extend the insert bucket. |
| 4609 | * |
| 4610 | * The problem is how much xattr should we move to the new one and when should |
| 4611 | * we update first_bh and header_bh? |
| 4612 | * 1. If cluster size > bucket size, that means the previous cluster has more |
| 4613 | * than 1 bucket, so just move half nums of bucket into the new cluster and |
| 4614 | * update the first_bh and header_bh if the insert bucket has been moved |
| 4615 | * to the new cluster. |
| 4616 | * 2. If cluster_size == bucket_size: |
| 4617 | * a) If the previous extent rec has more than one cluster and the insert |
| 4618 | * place isn't in the last cluster, copy the entire last cluster to the |
| 4619 | * new one. This time, we don't need to upate the first_bh and header_bh |
| 4620 | * since they will not be moved into the new cluster. |
| 4621 | * b) Otherwise, move the bottom half of the xattrs in the last cluster into |
| 4622 | * the new one. And we set the extend flag to zero if the insert place is |
| 4623 | * moved into the new allocated cluster since no extend is needed. |
| 4624 | */ |
| 4625 | static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode, |
| 4626 | handle_t *handle, |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4627 | struct ocfs2_xattr_bucket *first, |
| 4628 | struct ocfs2_xattr_bucket *target, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4629 | u64 new_blk, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4630 | u32 prev_clusters, |
| 4631 | u32 *v_start, |
| 4632 | int *extend) |
| 4633 | { |
Joel Becker | 92cf3ad | 2008-11-26 14:12:09 -0800 | [diff] [blame] | 4634 | int ret; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4635 | |
| 4636 | mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n", |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4637 | (unsigned long long)bucket_blkno(first), prev_clusters, |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 4638 | (unsigned long long)new_blk); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4639 | |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4640 | if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4641 | ret = ocfs2_mv_xattr_bucket_cross_cluster(inode, |
| 4642 | handle, |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4643 | first, target, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4644 | new_blk, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4645 | prev_clusters, |
| 4646 | v_start); |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4647 | if (ret) |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4648 | mlog_errno(ret); |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4649 | } else { |
Joel Becker | 92cf3ad | 2008-11-26 14:12:09 -0800 | [diff] [blame] | 4650 | /* The start of the last cluster in the first extent */ |
| 4651 | u64 last_blk = bucket_blkno(first) + |
| 4652 | ((prev_clusters - 1) * |
| 4653 | ocfs2_clusters_to_blocks(inode->i_sb, 1)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4654 | |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4655 | if (prev_clusters > 1 && bucket_blkno(target) != last_blk) { |
Joel Becker | 874d65a | 2008-11-26 13:02:18 -0800 | [diff] [blame] | 4656 | ret = ocfs2_mv_xattr_buckets(inode, handle, |
Joel Becker | 92cf3ad | 2008-11-26 14:12:09 -0800 | [diff] [blame] | 4657 | bucket_blkno(first), |
Joel Becker | 54ecb6b | 2008-11-26 13:18:31 -0800 | [diff] [blame] | 4658 | last_blk, new_blk, 0, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4659 | v_start); |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4660 | if (ret) |
| 4661 | mlog_errno(ret); |
| 4662 | } else { |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4663 | ret = ocfs2_divide_xattr_cluster(inode, handle, |
| 4664 | last_blk, new_blk, |
| 4665 | v_start); |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4666 | if (ret) |
| 4667 | mlog_errno(ret); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4668 | |
Joel Becker | 92cf3ad | 2008-11-26 14:12:09 -0800 | [diff] [blame] | 4669 | if ((bucket_blkno(target) == last_blk) && extend) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4670 | *extend = 0; |
| 4671 | } |
| 4672 | } |
| 4673 | |
| 4674 | return ret; |
| 4675 | } |
| 4676 | |
| 4677 | /* |
| 4678 | * Add a new cluster for xattr storage. |
| 4679 | * |
| 4680 | * If the new cluster is contiguous with the previous one, it will be |
| 4681 | * appended to the same extent record, and num_clusters will be updated. |
| 4682 | * If not, we will insert a new extent for it and move some xattrs in |
| 4683 | * the last cluster into the new allocated one. |
| 4684 | * We also need to limit the maximum size of a btree leaf, otherwise we'll |
| 4685 | * lose the benefits of hashing because we'll have to search large leaves. |
| 4686 | * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize, |
| 4687 | * if it's bigger). |
| 4688 | * |
| 4689 | * first_bh is the first block of the previous extent rec and header_bh |
| 4690 | * indicates the bucket we will insert the new xattrs. They will be updated |
| 4691 | * when the header_bh is moved into the new cluster. |
| 4692 | */ |
| 4693 | static int ocfs2_add_new_xattr_cluster(struct inode *inode, |
| 4694 | struct buffer_head *root_bh, |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4695 | struct ocfs2_xattr_bucket *first, |
| 4696 | struct ocfs2_xattr_bucket *target, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4697 | u32 *num_clusters, |
| 4698 | u32 prev_cpos, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4699 | int *extend, |
| 4700 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4701 | { |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4702 | int ret; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4703 | u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); |
| 4704 | u32 prev_clusters = *num_clusters; |
| 4705 | u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0; |
| 4706 | u64 block; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4707 | handle_t *handle = ctxt->handle; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4708 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4709 | struct ocfs2_extent_tree et; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4710 | |
| 4711 | mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, " |
| 4712 | "previous xattr blkno = %llu\n", |
| 4713 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4714 | prev_cpos, (unsigned long long)bucket_blkno(first)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4715 | |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 4716 | ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4717 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 4718 | ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh, |
Joel Becker | 8400897 | 2008-12-09 16:11:49 -0800 | [diff] [blame] | 4719 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4720 | if (ret < 0) { |
| 4721 | mlog_errno(ret); |
| 4722 | goto leave; |
| 4723 | } |
| 4724 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4725 | ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4726 | clusters_to_add, &bit_off, &num_bits); |
| 4727 | if (ret < 0) { |
| 4728 | if (ret != -ENOSPC) |
| 4729 | mlog_errno(ret); |
| 4730 | goto leave; |
| 4731 | } |
| 4732 | |
| 4733 | BUG_ON(num_bits > clusters_to_add); |
| 4734 | |
| 4735 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); |
| 4736 | mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n", |
| 4737 | num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 4738 | |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4739 | if (bucket_blkno(first) + (prev_clusters * bpc) == block && |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4740 | (prev_clusters + num_bits) << osb->s_clustersize_bits <= |
| 4741 | OCFS2_MAX_XATTR_TREE_LEAF_SIZE) { |
| 4742 | /* |
| 4743 | * If this cluster is contiguous with the old one and |
| 4744 | * adding this new cluster, we don't surpass the limit of |
| 4745 | * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be |
| 4746 | * initialized and used like other buckets in the previous |
| 4747 | * cluster. |
| 4748 | * So add it as a contiguous one. The caller will handle |
| 4749 | * its init process. |
| 4750 | */ |
| 4751 | v_start = prev_cpos + prev_clusters; |
| 4752 | *num_clusters = prev_clusters + num_bits; |
| 4753 | mlog(0, "Add contiguous %u clusters to previous extent rec.\n", |
| 4754 | num_bits); |
| 4755 | } else { |
| 4756 | ret = ocfs2_adjust_xattr_cross_cluster(inode, |
| 4757 | handle, |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4758 | first, |
| 4759 | target, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4760 | block, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4761 | prev_clusters, |
| 4762 | &v_start, |
| 4763 | extend); |
| 4764 | if (ret) { |
| 4765 | mlog_errno(ret); |
| 4766 | goto leave; |
| 4767 | } |
| 4768 | } |
| 4769 | |
| 4770 | 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] | 4771 | num_bits, (unsigned long long)block, v_start); |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4772 | ret = ocfs2_insert_extent(handle, &et, v_start, block, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4773 | num_bits, 0, ctxt->meta_ac); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4774 | if (ret < 0) { |
| 4775 | mlog_errno(ret); |
| 4776 | goto leave; |
| 4777 | } |
| 4778 | |
| 4779 | ret = ocfs2_journal_dirty(handle, root_bh); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4780 | if (ret < 0) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4781 | mlog_errno(ret); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4782 | |
| 4783 | leave: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4784 | return ret; |
| 4785 | } |
| 4786 | |
| 4787 | /* |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4788 | * We are given an extent. 'first' is the bucket at the very front of |
| 4789 | * the extent. The extent has space for an additional bucket past |
| 4790 | * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number |
| 4791 | * of the target bucket. We wish to shift every bucket past the target |
| 4792 | * down one, filling in that additional space. When we get back to the |
| 4793 | * target, we split the target between itself and the now-empty bucket |
| 4794 | * at target+1 (aka, target_blkno + blks_per_bucket). |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4795 | */ |
| 4796 | static int ocfs2_extend_xattr_bucket(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4797 | handle_t *handle, |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4798 | struct ocfs2_xattr_bucket *first, |
| 4799 | u64 target_blk, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4800 | u32 num_clusters) |
| 4801 | { |
| 4802 | int ret, credits; |
| 4803 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 4804 | u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4805 | u64 end_blk; |
| 4806 | u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4807 | |
| 4808 | mlog(0, "extend xattr bucket in %llu, xattr extend rec starting " |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4809 | "from %llu, len = %u\n", (unsigned long long)target_blk, |
| 4810 | (unsigned long long)bucket_blkno(first), num_clusters); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4811 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4812 | /* The extent must have room for an additional bucket */ |
| 4813 | BUG_ON(new_bucket >= |
| 4814 | (num_clusters * ocfs2_xattr_buckets_per_cluster(osb))); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4815 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4816 | /* end_blk points to the last existing bucket */ |
| 4817 | end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4818 | |
| 4819 | /* |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4820 | * end_blk is the start of the last existing bucket. |
| 4821 | * Thus, (end_blk - target_blk) covers the target bucket and |
| 4822 | * every bucket after it up to, but not including, the last |
| 4823 | * existing bucket. Then we add the last existing bucket, the |
| 4824 | * new bucket, and the first bucket (3 * blk_per_bucket). |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4825 | */ |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4826 | credits = (end_blk - target_blk) + (3 * blk_per_bucket) + |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4827 | handle->h_buffer_credits; |
| 4828 | ret = ocfs2_extend_trans(handle, credits); |
| 4829 | if (ret) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4830 | mlog_errno(ret); |
| 4831 | goto out; |
| 4832 | } |
| 4833 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4834 | ret = ocfs2_xattr_bucket_journal_access(handle, first, |
| 4835 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4836 | if (ret) { |
| 4837 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4838 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4839 | } |
| 4840 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4841 | while (end_blk != target_blk) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4842 | ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk, |
| 4843 | end_blk + blk_per_bucket, 0); |
| 4844 | if (ret) |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4845 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4846 | end_blk -= blk_per_bucket; |
| 4847 | } |
| 4848 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4849 | /* Move half of the xattr in target_blkno to the next bucket. */ |
| 4850 | ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk, |
| 4851 | target_blk + blk_per_bucket, NULL, 0); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4852 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4853 | le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1); |
| 4854 | ocfs2_xattr_bucket_journal_dirty(handle, first); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4855 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4856 | out: |
| 4857 | return ret; |
| 4858 | } |
| 4859 | |
| 4860 | /* |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4861 | * Add new xattr bucket in an extent record and adjust the buckets |
| 4862 | * accordingly. xb_bh is the ocfs2_xattr_block, and target is the |
| 4863 | * bucket we want to insert into. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4864 | * |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4865 | * In the easy case, we will move all the buckets after target down by |
| 4866 | * one. Half of target's xattrs will be moved to the next bucket. |
| 4867 | * |
| 4868 | * If current cluster is full, we'll allocate a new one. This may not |
| 4869 | * be contiguous. The underlying calls will make sure that there is |
| 4870 | * space for the insert, shifting buckets around if necessary. |
| 4871 | * 'target' may be moved by those calls. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4872 | */ |
| 4873 | static int ocfs2_add_new_xattr_bucket(struct inode *inode, |
| 4874 | struct buffer_head *xb_bh, |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4875 | struct ocfs2_xattr_bucket *target, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4876 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4877 | { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4878 | struct ocfs2_xattr_block *xb = |
| 4879 | (struct ocfs2_xattr_block *)xb_bh->b_data; |
| 4880 | struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root; |
| 4881 | struct ocfs2_extent_list *el = &xb_root->xt_list; |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4882 | u32 name_hash = |
| 4883 | le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash); |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4884 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4885 | int ret, num_buckets, extend = 1; |
| 4886 | u64 p_blkno; |
| 4887 | u32 e_cpos, num_clusters; |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4888 | /* The bucket at the front of the extent */ |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4889 | struct ocfs2_xattr_bucket *first; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4890 | |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4891 | mlog(0, "Add new xattr bucket starting from %llu\n", |
| 4892 | (unsigned long long)bucket_blkno(target)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4893 | |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4894 | /* The first bucket of the original extent */ |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4895 | first = ocfs2_xattr_bucket_new(inode); |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4896 | if (!first) { |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4897 | ret = -ENOMEM; |
| 4898 | mlog_errno(ret); |
| 4899 | goto out; |
| 4900 | } |
| 4901 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4902 | ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos, |
| 4903 | &num_clusters, el); |
| 4904 | if (ret) { |
| 4905 | mlog_errno(ret); |
| 4906 | goto out; |
| 4907 | } |
| 4908 | |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4909 | ret = ocfs2_read_xattr_bucket(first, p_blkno); |
| 4910 | if (ret) { |
| 4911 | mlog_errno(ret); |
| 4912 | goto out; |
| 4913 | } |
| 4914 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4915 | num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters; |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4916 | if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) { |
| 4917 | /* |
| 4918 | * This can move first+target if the target bucket moves |
| 4919 | * to the new extent. |
| 4920 | */ |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4921 | ret = ocfs2_add_new_xattr_cluster(inode, |
| 4922 | xb_bh, |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4923 | first, |
| 4924 | target, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4925 | &num_clusters, |
| 4926 | e_cpos, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4927 | &extend, |
| 4928 | ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4929 | if (ret) { |
| 4930 | mlog_errno(ret); |
| 4931 | goto out; |
| 4932 | } |
| 4933 | } |
| 4934 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4935 | if (extend) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4936 | ret = ocfs2_extend_xattr_bucket(inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4937 | ctxt->handle, |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4938 | first, |
| 4939 | bucket_blkno(target), |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4940 | num_clusters); |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4941 | if (ret) |
| 4942 | mlog_errno(ret); |
| 4943 | } |
| 4944 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4945 | out: |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4946 | ocfs2_xattr_bucket_free(first); |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4947 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4948 | return ret; |
| 4949 | } |
| 4950 | |
| 4951 | static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode, |
| 4952 | struct ocfs2_xattr_bucket *bucket, |
| 4953 | int offs) |
| 4954 | { |
| 4955 | int block_off = offs >> inode->i_sb->s_blocksize_bits; |
| 4956 | |
| 4957 | offs = offs % inode->i_sb->s_blocksize; |
Joel Becker | 51def39 | 2008-10-24 16:57:21 -0700 | [diff] [blame] | 4958 | return bucket_block(bucket, block_off) + offs; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4959 | } |
| 4960 | |
| 4961 | /* |
| 4962 | * Handle the normal xattr set, including replace, delete and new. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4963 | * |
| 4964 | * Note: "local" indicates the real data's locality. So we can't |
| 4965 | * just its bucket locality by its length. |
| 4966 | */ |
| 4967 | static void ocfs2_xattr_set_entry_normal(struct inode *inode, |
| 4968 | struct ocfs2_xattr_info *xi, |
| 4969 | struct ocfs2_xattr_search *xs, |
| 4970 | u32 name_hash, |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 4971 | int local) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4972 | { |
| 4973 | struct ocfs2_xattr_entry *last, *xe; |
| 4974 | int name_len = strlen(xi->name); |
| 4975 | struct ocfs2_xattr_header *xh = xs->header; |
| 4976 | u16 count = le16_to_cpu(xh->xh_count), start; |
| 4977 | size_t blocksize = inode->i_sb->s_blocksize; |
| 4978 | char *val; |
| 4979 | size_t offs, size, new_size; |
Joel Becker | 11179f2 | 2009-08-14 16:07:44 -0700 | [diff] [blame^] | 4980 | struct ocfs2_xa_loc loc; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4981 | |
Joel Becker | 11179f2 | 2009-08-14 16:07:44 -0700 | [diff] [blame^] | 4982 | ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket, |
| 4983 | xs->not_found ? NULL : xs->here); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4984 | last = &xh->xh_entries[count]; |
| 4985 | if (!xs->not_found) { |
| 4986 | xe = xs->here; |
| 4987 | offs = le16_to_cpu(xe->xe_name_offset); |
| 4988 | if (ocfs2_xattr_is_local(xe)) |
| 4989 | size = OCFS2_XATTR_SIZE(name_len) + |
| 4990 | OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); |
| 4991 | else |
| 4992 | size = OCFS2_XATTR_SIZE(name_len) + |
| 4993 | OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE); |
| 4994 | |
| 4995 | /* |
| 4996 | * If the new value will be stored outside, xi->value has been |
| 4997 | * initalized as an empty ocfs2_xattr_value_root, and the same |
| 4998 | * goes with xi->value_len, so we can set new_size safely here. |
| 4999 | * See ocfs2_xattr_set_in_bucket. |
| 5000 | */ |
| 5001 | new_size = OCFS2_XATTR_SIZE(name_len) + |
| 5002 | OCFS2_XATTR_SIZE(xi->value_len); |
| 5003 | |
Joel Becker | 11179f2 | 2009-08-14 16:07:44 -0700 | [diff] [blame^] | 5004 | ocfs2_xa_wipe_namevalue(&loc); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5005 | if (xi->value) { |
| 5006 | if (new_size > size) |
| 5007 | goto set_new_name_value; |
| 5008 | |
| 5009 | /* Now replace the old value with new one. */ |
| 5010 | if (local) |
| 5011 | xe->xe_value_size = cpu_to_le64(xi->value_len); |
| 5012 | else |
| 5013 | xe->xe_value_size = 0; |
| 5014 | |
| 5015 | val = ocfs2_xattr_bucket_get_val(inode, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5016 | xs->bucket, offs); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5017 | memset(val + OCFS2_XATTR_SIZE(name_len), 0, |
| 5018 | size - OCFS2_XATTR_SIZE(name_len)); |
| 5019 | if (OCFS2_XATTR_SIZE(xi->value_len) > 0) |
| 5020 | memcpy(val + OCFS2_XATTR_SIZE(name_len), |
| 5021 | xi->value, xi->value_len); |
| 5022 | |
| 5023 | le16_add_cpu(&xh->xh_name_value_len, new_size); |
| 5024 | ocfs2_xattr_set_local(xe, local); |
| 5025 | return; |
| 5026 | } else { |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 5027 | /* |
| 5028 | * Remove the old entry if there is more than one. |
| 5029 | * We don't remove the last entry so that we can |
| 5030 | * use it to indicate the hash value of the empty |
| 5031 | * bucket. |
| 5032 | */ |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5033 | last -= 1; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5034 | le16_add_cpu(&xh->xh_count, -1); |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 5035 | if (xh->xh_count) { |
| 5036 | memmove(xe, xe + 1, |
| 5037 | (void *)last - (void *)xe); |
| 5038 | memset(last, 0, |
| 5039 | sizeof(struct ocfs2_xattr_entry)); |
| 5040 | } else |
| 5041 | xh->xh_free_start = |
| 5042 | cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE); |
| 5043 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5044 | return; |
| 5045 | } |
| 5046 | } else { |
| 5047 | /* find a new entry for insert. */ |
| 5048 | int low = 0, high = count - 1, tmp; |
| 5049 | struct ocfs2_xattr_entry *tmp_xe; |
| 5050 | |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 5051 | while (low <= high && count) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5052 | tmp = (low + high) / 2; |
| 5053 | tmp_xe = &xh->xh_entries[tmp]; |
| 5054 | |
| 5055 | if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash)) |
| 5056 | low = tmp + 1; |
| 5057 | else if (name_hash < |
| 5058 | le32_to_cpu(tmp_xe->xe_name_hash)) |
| 5059 | high = tmp - 1; |
Tao Ma | 06b240d | 2008-09-19 22:16:34 +0800 | [diff] [blame] | 5060 | else { |
| 5061 | low = tmp; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5062 | break; |
Tao Ma | 06b240d | 2008-09-19 22:16:34 +0800 | [diff] [blame] | 5063 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5064 | } |
| 5065 | |
| 5066 | xe = &xh->xh_entries[low]; |
| 5067 | if (low != count) |
| 5068 | memmove(xe + 1, xe, (void *)last - (void *)xe); |
| 5069 | |
| 5070 | le16_add_cpu(&xh->xh_count, 1); |
| 5071 | memset(xe, 0, sizeof(struct ocfs2_xattr_entry)); |
| 5072 | xe->xe_name_hash = cpu_to_le32(name_hash); |
| 5073 | xe->xe_name_len = name_len; |
| 5074 | ocfs2_xattr_set_type(xe, xi->name_index); |
| 5075 | } |
| 5076 | |
| 5077 | set_new_name_value: |
| 5078 | /* Insert the new name+value. */ |
| 5079 | size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len); |
| 5080 | |
| 5081 | /* |
| 5082 | * We must make sure that the name/value pair |
| 5083 | * exists in the same block. |
| 5084 | */ |
| 5085 | offs = le16_to_cpu(xh->xh_free_start); |
| 5086 | start = offs - size; |
| 5087 | |
| 5088 | if (start >> inode->i_sb->s_blocksize_bits != |
| 5089 | (offs - 1) >> inode->i_sb->s_blocksize_bits) { |
| 5090 | offs = offs - offs % blocksize; |
| 5091 | xh->xh_free_start = cpu_to_le16(offs); |
| 5092 | } |
| 5093 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5094 | val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5095 | xe->xe_name_offset = cpu_to_le16(offs - size); |
| 5096 | |
| 5097 | memset(val, 0, size); |
| 5098 | memcpy(val, xi->name, name_len); |
| 5099 | memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len); |
| 5100 | |
| 5101 | xe->xe_value_size = cpu_to_le64(xi->value_len); |
| 5102 | ocfs2_xattr_set_local(xe, local); |
| 5103 | xs->here = xe; |
| 5104 | le16_add_cpu(&xh->xh_free_start, -size); |
| 5105 | le16_add_cpu(&xh->xh_name_value_len, size); |
| 5106 | |
| 5107 | return; |
| 5108 | } |
| 5109 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5110 | /* |
| 5111 | * Set the xattr entry in the specified bucket. |
| 5112 | * The bucket is indicated by xs->bucket and it should have the enough |
| 5113 | * space for the xattr insertion. |
| 5114 | */ |
| 5115 | static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5116 | handle_t *handle, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5117 | struct ocfs2_xattr_info *xi, |
| 5118 | struct ocfs2_xattr_search *xs, |
| 5119 | u32 name_hash, |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 5120 | int local) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5121 | { |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 5122 | int ret; |
Joel Becker | 02dbf38 | 2008-10-27 18:07:45 -0700 | [diff] [blame] | 5123 | u64 blkno; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5124 | |
Mark Fasheh | ff1ec20 | 2008-08-19 10:54:29 -0700 | [diff] [blame] | 5125 | mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n", |
| 5126 | (unsigned long)xi->value_len, xi->name_index, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5127 | (unsigned long long)bucket_blkno(xs->bucket)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5128 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5129 | if (!xs->bucket->bu_bhs[1]) { |
Joel Becker | 02dbf38 | 2008-10-27 18:07:45 -0700 | [diff] [blame] | 5130 | blkno = bucket_blkno(xs->bucket); |
| 5131 | ocfs2_xattr_bucket_relse(xs->bucket); |
| 5132 | ret = ocfs2_read_xattr_bucket(xs->bucket, blkno); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5133 | if (ret) { |
| 5134 | mlog_errno(ret); |
| 5135 | goto out; |
| 5136 | } |
| 5137 | } |
| 5138 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5139 | ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 5140 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 5141 | if (ret < 0) { |
| 5142 | mlog_errno(ret); |
| 5143 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5144 | } |
| 5145 | |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 5146 | ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5147 | ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5148 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5149 | out: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5150 | return ret; |
| 5151 | } |
| 5152 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5153 | /* |
| 5154 | * Truncate the specified xe_off entry in xattr bucket. |
| 5155 | * bucket is indicated by header_bh and len is the new length. |
| 5156 | * Both the ocfs2_xattr_value_root and the entry will be updated here. |
| 5157 | * |
| 5158 | * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed. |
| 5159 | */ |
| 5160 | static int ocfs2_xattr_bucket_value_truncate(struct inode *inode, |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 5161 | struct ocfs2_xattr_bucket *bucket, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5162 | int xe_off, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5163 | int len, |
| 5164 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5165 | { |
| 5166 | int ret, offset; |
| 5167 | u64 value_blk; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5168 | struct ocfs2_xattr_entry *xe; |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 5169 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5170 | size_t blocksize = inode->i_sb->s_blocksize; |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 5171 | struct ocfs2_xattr_value_buf vb = { |
| 5172 | .vb_access = ocfs2_journal_access, |
| 5173 | }; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5174 | |
| 5175 | xe = &xh->xh_entries[xe_off]; |
| 5176 | |
| 5177 | BUG_ON(!xe || ocfs2_xattr_is_local(xe)); |
| 5178 | |
| 5179 | offset = le16_to_cpu(xe->xe_name_offset) + |
| 5180 | OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 5181 | |
| 5182 | value_blk = offset / blocksize; |
| 5183 | |
| 5184 | /* We don't allow ocfs2_xattr_value to be stored in different block. */ |
| 5185 | BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5186 | |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 5187 | vb.vb_bh = bucket->bu_bhs[value_blk]; |
| 5188 | BUG_ON(!vb.vb_bh); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5189 | |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 5190 | vb.vb_xv = (struct ocfs2_xattr_value_root *) |
| 5191 | (vb.vb_bh->b_data + offset % blocksize); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5192 | |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 5193 | /* |
| 5194 | * From here on out we have to dirty the bucket. The generic |
| 5195 | * value calls only modify one of the bucket's bhs, but we need |
| 5196 | * to send the bucket at once. So if they error, they *could* have |
| 5197 | * modified something. We have to assume they did, and dirty |
| 5198 | * the whole bucket. This leaves us in a consistent state. |
| 5199 | */ |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5200 | mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n", |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 5201 | xe_off, (unsigned long long)bucket_blkno(bucket), len); |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 5202 | ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5203 | if (ret) { |
| 5204 | mlog_errno(ret); |
Tao Ma | 554e7f9 | 2009-01-08 08:21:43 +0800 | [diff] [blame] | 5205 | goto out; |
| 5206 | } |
| 5207 | |
| 5208 | ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket, |
| 5209 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 5210 | if (ret) { |
| 5211 | mlog_errno(ret); |
| 5212 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5213 | } |
| 5214 | |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 5215 | xe->xe_value_size = cpu_to_le64(len); |
| 5216 | |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 5217 | ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5218 | |
| 5219 | out: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5220 | return ret; |
| 5221 | } |
| 5222 | |
| 5223 | static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5224 | struct ocfs2_xattr_search *xs, |
| 5225 | int len, |
| 5226 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5227 | { |
| 5228 | int ret, offset; |
| 5229 | struct ocfs2_xattr_entry *xe = xs->here; |
| 5230 | struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base; |
| 5231 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5232 | 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] | 5233 | |
| 5234 | offset = xe - xh->xh_entries; |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 5235 | ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5236 | offset, len, ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5237 | if (ret) |
| 5238 | mlog_errno(ret); |
| 5239 | |
| 5240 | return ret; |
| 5241 | } |
| 5242 | |
| 5243 | static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5244 | handle_t *handle, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5245 | struct ocfs2_xattr_search *xs, |
| 5246 | char *val, |
| 5247 | int value_len) |
| 5248 | { |
Tao Ma | 712e53e | 2009-03-12 08:37:34 +0800 | [diff] [blame] | 5249 | int ret, offset, block_off; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5250 | struct ocfs2_xattr_value_root *xv; |
| 5251 | struct ocfs2_xattr_entry *xe = xs->here; |
Tao Ma | 712e53e | 2009-03-12 08:37:34 +0800 | [diff] [blame] | 5252 | struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket); |
| 5253 | void *base; |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 5254 | struct ocfs2_xattr_value_buf vb = { |
| 5255 | .vb_access = ocfs2_journal_access, |
| 5256 | }; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5257 | |
| 5258 | BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe)); |
| 5259 | |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 5260 | ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, xh, |
Tao Ma | 712e53e | 2009-03-12 08:37:34 +0800 | [diff] [blame] | 5261 | xe - xh->xh_entries, |
| 5262 | &block_off, |
| 5263 | &offset); |
| 5264 | if (ret) { |
| 5265 | mlog_errno(ret); |
| 5266 | goto out; |
| 5267 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5268 | |
Tao Ma | 712e53e | 2009-03-12 08:37:34 +0800 | [diff] [blame] | 5269 | base = bucket_block(xs->bucket, block_off); |
| 5270 | xv = (struct ocfs2_xattr_value_root *)(base + offset + |
| 5271 | OCFS2_XATTR_SIZE(xe->xe_name_len)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5272 | |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 5273 | vb.vb_xv = xv; |
| 5274 | vb.vb_bh = xs->bucket->bu_bhs[block_off]; |
Tao Ma | 712e53e | 2009-03-12 08:37:34 +0800 | [diff] [blame] | 5275 | ret = __ocfs2_xattr_set_value_outside(inode, handle, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 5276 | &vb, val, value_len); |
Tao Ma | 712e53e | 2009-03-12 08:37:34 +0800 | [diff] [blame] | 5277 | if (ret) |
| 5278 | mlog_errno(ret); |
| 5279 | out: |
| 5280 | return ret; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5281 | } |
| 5282 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5283 | static int ocfs2_rm_xattr_cluster(struct inode *inode, |
| 5284 | struct buffer_head *root_bh, |
| 5285 | u64 blkno, |
| 5286 | u32 cpos, |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 5287 | u32 len, |
| 5288 | void *para) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5289 | { |
| 5290 | int ret; |
| 5291 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5292 | struct inode *tl_inode = osb->osb_tl_inode; |
| 5293 | handle_t *handle; |
| 5294 | struct ocfs2_xattr_block *xb = |
| 5295 | (struct ocfs2_xattr_block *)root_bh->b_data; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5296 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 5297 | struct ocfs2_cached_dealloc_ctxt dealloc; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5298 | struct ocfs2_extent_tree et; |
| 5299 | |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 5300 | ret = ocfs2_iterate_xattr_buckets(inode, blkno, len, |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 5301 | ocfs2_delete_xattr_in_bucket, para); |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame] | 5302 | if (ret) { |
| 5303 | mlog_errno(ret); |
| 5304 | return ret; |
| 5305 | } |
| 5306 | |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 5307 | ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5308 | |
| 5309 | ocfs2_init_dealloc_ctxt(&dealloc); |
| 5310 | |
| 5311 | mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n", |
| 5312 | cpos, len, (unsigned long long)blkno); |
| 5313 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 5314 | ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno, |
| 5315 | len); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5316 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5317 | ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5318 | if (ret) { |
| 5319 | mlog_errno(ret); |
| 5320 | return ret; |
| 5321 | } |
| 5322 | |
| 5323 | mutex_lock(&tl_inode->i_mutex); |
| 5324 | |
| 5325 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 5326 | ret = __ocfs2_flush_truncate_log(osb); |
| 5327 | if (ret < 0) { |
| 5328 | mlog_errno(ret); |
| 5329 | goto out; |
| 5330 | } |
| 5331 | } |
| 5332 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 5333 | handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb)); |
Tao Ma | d326479 | 2008-10-24 07:57:28 +0800 | [diff] [blame] | 5334 | if (IS_ERR(handle)) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5335 | ret = -ENOMEM; |
| 5336 | mlog_errno(ret); |
| 5337 | goto out; |
| 5338 | } |
| 5339 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 5340 | ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh, |
Joel Becker | 8400897 | 2008-12-09 16:11:49 -0800 | [diff] [blame] | 5341 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5342 | if (ret) { |
| 5343 | mlog_errno(ret); |
| 5344 | goto out_commit; |
| 5345 | } |
| 5346 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5347 | ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5348 | &dealloc); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5349 | if (ret) { |
| 5350 | mlog_errno(ret); |
| 5351 | goto out_commit; |
| 5352 | } |
| 5353 | |
| 5354 | le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len); |
| 5355 | |
| 5356 | ret = ocfs2_journal_dirty(handle, root_bh); |
| 5357 | if (ret) { |
| 5358 | mlog_errno(ret); |
| 5359 | goto out_commit; |
| 5360 | } |
| 5361 | |
| 5362 | ret = ocfs2_truncate_log_append(osb, handle, blkno, len); |
| 5363 | if (ret) |
| 5364 | mlog_errno(ret); |
| 5365 | |
| 5366 | out_commit: |
| 5367 | ocfs2_commit_trans(osb, handle); |
| 5368 | out: |
| 5369 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 5370 | |
| 5371 | mutex_unlock(&tl_inode->i_mutex); |
| 5372 | |
| 5373 | if (meta_ac) |
| 5374 | ocfs2_free_alloc_context(meta_ac); |
| 5375 | |
| 5376 | ocfs2_run_deallocs(osb, &dealloc); |
| 5377 | |
| 5378 | return ret; |
| 5379 | } |
| 5380 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5381 | static void ocfs2_xattr_bucket_remove_xs(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5382 | handle_t *handle, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5383 | struct ocfs2_xattr_search *xs) |
| 5384 | { |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5385 | struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5386 | struct ocfs2_xattr_entry *last = &xh->xh_entries[ |
| 5387 | le16_to_cpu(xh->xh_count) - 1]; |
| 5388 | int ret = 0; |
| 5389 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5390 | ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 5391 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5392 | if (ret) { |
| 5393 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5394 | return; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5395 | } |
| 5396 | |
| 5397 | /* Remove the old entry. */ |
| 5398 | memmove(xs->here, xs->here + 1, |
| 5399 | (void *)last - (void *)xs->here); |
| 5400 | memset(last, 0, sizeof(struct ocfs2_xattr_entry)); |
| 5401 | le16_add_cpu(&xh->xh_count, -1); |
| 5402 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5403 | ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5404 | } |
| 5405 | |
| 5406 | /* |
| 5407 | * Set the xattr name/value in the bucket specified in xs. |
| 5408 | * |
| 5409 | * As the new value in xi may be stored in the bucket or in an outside cluster, |
| 5410 | * we divide the whole process into 3 steps: |
| 5411 | * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket) |
| 5412 | * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs) |
| 5413 | * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside) |
| 5414 | * 4. If the clusters for the new outside value can't be allocated, we need |
| 5415 | * to free the xattr we allocated in set. |
| 5416 | */ |
| 5417 | static int ocfs2_xattr_set_in_bucket(struct inode *inode, |
| 5418 | struct ocfs2_xattr_info *xi, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5419 | struct ocfs2_xattr_search *xs, |
| 5420 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5421 | { |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 5422 | int ret, local = 1; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5423 | size_t value_len; |
| 5424 | char *val = (char *)xi->value; |
| 5425 | struct ocfs2_xattr_entry *xe = xs->here; |
Tao Ma | 2057e5c | 2008-10-09 23:06:13 +0800 | [diff] [blame] | 5426 | u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name, |
| 5427 | strlen(xi->name)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5428 | |
| 5429 | if (!xs->not_found && !ocfs2_xattr_is_local(xe)) { |
| 5430 | /* |
| 5431 | * We need to truncate the xattr storage first. |
| 5432 | * |
| 5433 | * If both the old and new value are stored to |
| 5434 | * outside block, we only need to truncate |
| 5435 | * the storage and then set the value outside. |
| 5436 | * |
| 5437 | * If the new value should be stored within block, |
| 5438 | * we should free all the outside block first and |
| 5439 | * the modification to the xattr block will be done |
| 5440 | * by following steps. |
| 5441 | */ |
| 5442 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) |
| 5443 | value_len = xi->value_len; |
| 5444 | else |
| 5445 | value_len = 0; |
| 5446 | |
| 5447 | ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5448 | value_len, |
| 5449 | ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5450 | if (ret) |
| 5451 | goto out; |
| 5452 | |
| 5453 | if (value_len) |
| 5454 | goto set_value_outside; |
| 5455 | } |
| 5456 | |
| 5457 | value_len = xi->value_len; |
| 5458 | /* So we have to handle the inside block change now. */ |
| 5459 | if (value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 5460 | /* |
| 5461 | * If the new value will be stored outside of block, |
| 5462 | * initalize a new empty value root and insert it first. |
| 5463 | */ |
| 5464 | local = 0; |
| 5465 | xi->value = &def_xv; |
| 5466 | xi->value_len = OCFS2_XATTR_ROOT_SIZE; |
| 5467 | } |
| 5468 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5469 | ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs, |
| 5470 | name_hash, local); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5471 | if (ret) { |
| 5472 | mlog_errno(ret); |
| 5473 | goto out; |
| 5474 | } |
| 5475 | |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 5476 | if (value_len <= OCFS2_XATTR_INLINE_SIZE) |
| 5477 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5478 | |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 5479 | /* allocate the space now for the outside block storage. */ |
| 5480 | ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5481 | value_len, ctxt); |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 5482 | if (ret) { |
| 5483 | mlog_errno(ret); |
| 5484 | |
| 5485 | if (xs->not_found) { |
| 5486 | /* |
| 5487 | * We can't allocate enough clusters for outside |
| 5488 | * storage and we have allocated xattr already, |
| 5489 | * so need to remove it. |
| 5490 | */ |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5491 | ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5492 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5493 | goto out; |
| 5494 | } |
| 5495 | |
| 5496 | set_value_outside: |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5497 | ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle, |
| 5498 | xs, val, value_len); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5499 | out: |
| 5500 | return ret; |
| 5501 | } |
| 5502 | |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 5503 | /* |
| 5504 | * check whether the xattr bucket is filled up with the same hash value. |
| 5505 | * If we want to insert the xattr with the same hash, return -ENOSPC. |
| 5506 | * If we want to insert a xattr with different hash value, go ahead |
| 5507 | * and ocfs2_divide_xattr_bucket will handle this. |
| 5508 | */ |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5509 | static int ocfs2_check_xattr_bucket_collision(struct inode *inode, |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 5510 | struct ocfs2_xattr_bucket *bucket, |
| 5511 | const char *name) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5512 | { |
Joel Becker | 3e63294 | 2008-10-24 17:04:49 -0700 | [diff] [blame] | 5513 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 5514 | u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name)); |
| 5515 | |
| 5516 | if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash)) |
| 5517 | return 0; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5518 | |
| 5519 | if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash == |
| 5520 | xh->xh_entries[0].xe_name_hash) { |
| 5521 | mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, " |
| 5522 | "hash = %u\n", |
Joel Becker | 9c7759a | 2008-10-24 16:21:03 -0700 | [diff] [blame] | 5523 | (unsigned long long)bucket_blkno(bucket), |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5524 | le32_to_cpu(xh->xh_entries[0].xe_name_hash)); |
| 5525 | return -ENOSPC; |
| 5526 | } |
| 5527 | |
| 5528 | return 0; |
| 5529 | } |
| 5530 | |
| 5531 | static int ocfs2_xattr_set_entry_index_block(struct inode *inode, |
| 5532 | struct ocfs2_xattr_info *xi, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5533 | struct ocfs2_xattr_search *xs, |
| 5534 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5535 | { |
| 5536 | struct ocfs2_xattr_header *xh; |
| 5537 | struct ocfs2_xattr_entry *xe; |
| 5538 | u16 count, header_size, xh_free_start; |
Joel Becker | 6dde41d | 2008-10-24 17:16:48 -0700 | [diff] [blame] | 5539 | int free, max_free, need, old; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5540 | size_t value_size = 0, name_len = strlen(xi->name); |
| 5541 | size_t blocksize = inode->i_sb->s_blocksize; |
| 5542 | int ret, allocation = 0; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5543 | |
| 5544 | mlog_entry("Set xattr %s in xattr index block\n", xi->name); |
| 5545 | |
| 5546 | try_again: |
| 5547 | xh = xs->header; |
| 5548 | count = le16_to_cpu(xh->xh_count); |
| 5549 | xh_free_start = le16_to_cpu(xh->xh_free_start); |
| 5550 | header_size = sizeof(struct ocfs2_xattr_header) + |
| 5551 | count * sizeof(struct ocfs2_xattr_entry); |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 5552 | max_free = OCFS2_XATTR_BUCKET_SIZE - header_size - |
| 5553 | le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5554 | |
| 5555 | mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size " |
| 5556 | "of %u which exceed block size\n", |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5557 | (unsigned long long)bucket_blkno(xs->bucket), |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5558 | header_size); |
| 5559 | |
| 5560 | if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) |
| 5561 | value_size = OCFS2_XATTR_ROOT_SIZE; |
| 5562 | else if (xi->value) |
| 5563 | value_size = OCFS2_XATTR_SIZE(xi->value_len); |
| 5564 | |
| 5565 | if (xs->not_found) |
| 5566 | need = sizeof(struct ocfs2_xattr_entry) + |
| 5567 | OCFS2_XATTR_SIZE(name_len) + value_size; |
| 5568 | else { |
| 5569 | need = value_size + OCFS2_XATTR_SIZE(name_len); |
| 5570 | |
| 5571 | /* |
| 5572 | * We only replace the old value if the new length is smaller |
| 5573 | * than the old one. Otherwise we will allocate new space in the |
| 5574 | * bucket to store it. |
| 5575 | */ |
| 5576 | xe = xs->here; |
| 5577 | if (ocfs2_xattr_is_local(xe)) |
| 5578 | old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); |
| 5579 | else |
| 5580 | old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE); |
| 5581 | |
| 5582 | if (old >= value_size) |
| 5583 | need = 0; |
| 5584 | } |
| 5585 | |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 5586 | free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5587 | /* |
| 5588 | * We need to make sure the new name/value pair |
| 5589 | * can exist in the same block. |
| 5590 | */ |
| 5591 | if (xh_free_start % blocksize < need) |
| 5592 | free -= xh_free_start % blocksize; |
| 5593 | |
| 5594 | mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, " |
| 5595 | "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len =" |
| 5596 | " %u\n", xs->not_found, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5597 | (unsigned long long)bucket_blkno(xs->bucket), |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5598 | free, need, max_free, le16_to_cpu(xh->xh_free_start), |
| 5599 | le16_to_cpu(xh->xh_name_value_len)); |
| 5600 | |
Tao Ma | 976331d | 2008-11-12 08:26:57 +0800 | [diff] [blame] | 5601 | if (free < need || |
| 5602 | (xs->not_found && |
| 5603 | count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5604 | if (need <= max_free && |
| 5605 | count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) { |
| 5606 | /* |
| 5607 | * We can create the space by defragment. Since only the |
| 5608 | * name/value will be moved, the xe shouldn't be changed |
| 5609 | * in xs. |
| 5610 | */ |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5611 | ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle, |
| 5612 | xs->bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5613 | if (ret) { |
| 5614 | mlog_errno(ret); |
| 5615 | goto out; |
| 5616 | } |
| 5617 | |
| 5618 | xh_free_start = le16_to_cpu(xh->xh_free_start); |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 5619 | free = xh_free_start - header_size |
| 5620 | - OCFS2_XATTR_HEADER_GAP; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5621 | if (xh_free_start % blocksize < need) |
| 5622 | free -= xh_free_start % blocksize; |
| 5623 | |
| 5624 | if (free >= need) |
| 5625 | goto xattr_set; |
| 5626 | |
| 5627 | mlog(0, "Can't get enough space for xattr insert by " |
| 5628 | "defragment. Need %u bytes, but we have %d, so " |
| 5629 | "allocate new bucket for it.\n", need, free); |
| 5630 | } |
| 5631 | |
| 5632 | /* |
| 5633 | * We have to add new buckets or clusters and one |
| 5634 | * allocation should leave us enough space for insert. |
| 5635 | */ |
| 5636 | BUG_ON(allocation); |
| 5637 | |
| 5638 | /* |
| 5639 | * We do not allow for overlapping ranges between buckets. And |
| 5640 | * the maximum number of collisions we will allow for then is |
| 5641 | * one bucket's worth, so check it here whether we need to |
| 5642 | * add a new bucket for the insert. |
| 5643 | */ |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 5644 | ret = ocfs2_check_xattr_bucket_collision(inode, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5645 | xs->bucket, |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 5646 | xi->name); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5647 | if (ret) { |
| 5648 | mlog_errno(ret); |
| 5649 | goto out; |
| 5650 | } |
| 5651 | |
| 5652 | ret = ocfs2_add_new_xattr_bucket(inode, |
| 5653 | xs->xattr_bh, |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 5654 | xs->bucket, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5655 | ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5656 | if (ret) { |
| 5657 | mlog_errno(ret); |
| 5658 | goto out; |
| 5659 | } |
| 5660 | |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 5661 | /* |
| 5662 | * ocfs2_add_new_xattr_bucket() will have updated |
| 5663 | * xs->bucket if it moved, but it will not have updated |
| 5664 | * any of the other search fields. Thus, we drop it and |
| 5665 | * re-search. Everything should be cached, so it'll be |
| 5666 | * quick. |
| 5667 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5668 | ocfs2_xattr_bucket_relse(xs->bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5669 | ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh, |
| 5670 | xi->name_index, |
| 5671 | xi->name, xs); |
| 5672 | if (ret && ret != -ENODATA) |
| 5673 | goto out; |
| 5674 | xs->not_found = ret; |
| 5675 | allocation = 1; |
| 5676 | goto try_again; |
| 5677 | } |
| 5678 | |
| 5679 | xattr_set: |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5680 | ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5681 | out: |
| 5682 | mlog_exit(ret); |
| 5683 | return ret; |
| 5684 | } |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 5685 | |
| 5686 | static int ocfs2_delete_xattr_in_bucket(struct inode *inode, |
| 5687 | struct ocfs2_xattr_bucket *bucket, |
| 5688 | void *para) |
| 5689 | { |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 5690 | int ret = 0, ref_credits; |
Joel Becker | 3e63294 | 2008-10-24 17:04:49 -0700 | [diff] [blame] | 5691 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 5692 | u16 i; |
| 5693 | struct ocfs2_xattr_entry *xe; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5694 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5695 | struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,}; |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 5696 | int credits = ocfs2_remove_extent_credits(osb->sb) + |
| 5697 | ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 5698 | struct ocfs2_xattr_value_root *xv; |
| 5699 | struct ocfs2_rm_xattr_bucket_para *args = |
| 5700 | (struct ocfs2_rm_xattr_bucket_para *)para; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5701 | |
| 5702 | ocfs2_init_dealloc_ctxt(&ctxt.dealloc); |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 5703 | |
| 5704 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { |
| 5705 | xe = &xh->xh_entries[i]; |
| 5706 | if (ocfs2_xattr_is_local(xe)) |
| 5707 | continue; |
| 5708 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 5709 | ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, |
| 5710 | i, &xv, NULL); |
| 5711 | |
| 5712 | ret = ocfs2_lock_xattr_remove_allocators(inode, xv, |
| 5713 | args->ref_ci, |
| 5714 | args->ref_root_bh, |
| 5715 | &ctxt.meta_ac, |
| 5716 | &ref_credits); |
| 5717 | |
| 5718 | ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits); |
Tao Ma | 88c3b06 | 2008-12-11 08:54:11 +0800 | [diff] [blame] | 5719 | if (IS_ERR(ctxt.handle)) { |
| 5720 | ret = PTR_ERR(ctxt.handle); |
| 5721 | mlog_errno(ret); |
| 5722 | break; |
| 5723 | } |
| 5724 | |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 5725 | ret = ocfs2_xattr_bucket_value_truncate(inode, bucket, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5726 | i, 0, &ctxt); |
Tao Ma | 88c3b06 | 2008-12-11 08:54:11 +0800 | [diff] [blame] | 5727 | |
| 5728 | ocfs2_commit_trans(osb, ctxt.handle); |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 5729 | if (ctxt.meta_ac) { |
| 5730 | ocfs2_free_alloc_context(ctxt.meta_ac); |
| 5731 | ctxt.meta_ac = NULL; |
| 5732 | } |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 5733 | if (ret) { |
| 5734 | mlog_errno(ret); |
| 5735 | break; |
| 5736 | } |
| 5737 | } |
| 5738 | |
Tao Ma | ce9c5a5 | 2009-08-18 11:43:59 +0800 | [diff] [blame] | 5739 | if (ctxt.meta_ac) |
| 5740 | ocfs2_free_alloc_context(ctxt.meta_ac); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5741 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 5742 | ocfs2_run_deallocs(osb, &ctxt.dealloc); |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 5743 | return ret; |
| 5744 | } |
| 5745 | |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 5746 | /* |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 5747 | * Whenever we modify a xattr value root in the bucket(e.g, CoW |
| 5748 | * or change the extent record flag), we need to recalculate |
| 5749 | * the metaecc for the whole bucket. So it is done here. |
| 5750 | * |
| 5751 | * Note: |
| 5752 | * We have to give the extra credits for the caller. |
| 5753 | */ |
| 5754 | static int ocfs2_xattr_bucket_post_refcount(struct inode *inode, |
| 5755 | handle_t *handle, |
| 5756 | void *para) |
| 5757 | { |
| 5758 | int ret; |
| 5759 | struct ocfs2_xattr_bucket *bucket = |
| 5760 | (struct ocfs2_xattr_bucket *)para; |
| 5761 | |
| 5762 | ret = ocfs2_xattr_bucket_journal_access(handle, bucket, |
| 5763 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 5764 | if (ret) { |
| 5765 | mlog_errno(ret); |
| 5766 | return ret; |
| 5767 | } |
| 5768 | |
| 5769 | ocfs2_xattr_bucket_journal_dirty(handle, bucket); |
| 5770 | |
| 5771 | return 0; |
| 5772 | } |
| 5773 | |
| 5774 | /* |
| 5775 | * Special action we need if the xattr value is refcounted. |
| 5776 | * |
| 5777 | * 1. If the xattr is refcounted, lock the tree. |
| 5778 | * 2. CoW the xattr if we are setting the new value and the value |
| 5779 | * will be stored outside. |
| 5780 | * 3. In other case, decrease_refcount will work for us, so just |
| 5781 | * lock the refcount tree, calculate the meta and credits is OK. |
| 5782 | * |
| 5783 | * We have to do CoW before ocfs2_init_xattr_set_ctxt since |
| 5784 | * currently CoW is a completed transaction, while this function |
| 5785 | * will also lock the allocators and let us deadlock. So we will |
| 5786 | * CoW the whole xattr value. |
| 5787 | */ |
| 5788 | static int ocfs2_prepare_refcount_xattr(struct inode *inode, |
| 5789 | struct ocfs2_dinode *di, |
| 5790 | struct ocfs2_xattr_info *xi, |
| 5791 | struct ocfs2_xattr_search *xis, |
| 5792 | struct ocfs2_xattr_search *xbs, |
| 5793 | struct ocfs2_refcount_tree **ref_tree, |
| 5794 | int *meta_add, |
| 5795 | int *credits) |
| 5796 | { |
| 5797 | int ret = 0; |
| 5798 | struct ocfs2_xattr_block *xb; |
| 5799 | struct ocfs2_xattr_entry *xe; |
| 5800 | char *base; |
| 5801 | u32 p_cluster, num_clusters; |
| 5802 | unsigned int ext_flags; |
| 5803 | int name_offset, name_len; |
| 5804 | struct ocfs2_xattr_value_buf vb; |
| 5805 | struct ocfs2_xattr_bucket *bucket = NULL; |
| 5806 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5807 | struct ocfs2_post_refcount refcount; |
| 5808 | struct ocfs2_post_refcount *p = NULL; |
| 5809 | struct buffer_head *ref_root_bh = NULL; |
| 5810 | |
| 5811 | if (!xis->not_found) { |
| 5812 | xe = xis->here; |
| 5813 | name_offset = le16_to_cpu(xe->xe_name_offset); |
| 5814 | name_len = OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 5815 | base = xis->base; |
| 5816 | vb.vb_bh = xis->inode_bh; |
| 5817 | vb.vb_access = ocfs2_journal_access_di; |
| 5818 | } else { |
| 5819 | int i, block_off = 0; |
| 5820 | xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data; |
| 5821 | xe = xbs->here; |
| 5822 | name_offset = le16_to_cpu(xe->xe_name_offset); |
| 5823 | name_len = OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 5824 | i = xbs->here - xbs->header->xh_entries; |
| 5825 | |
| 5826 | if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) { |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 5827 | ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 5828 | bucket_xh(xbs->bucket), |
| 5829 | i, &block_off, |
| 5830 | &name_offset); |
| 5831 | if (ret) { |
| 5832 | mlog_errno(ret); |
| 5833 | goto out; |
| 5834 | } |
| 5835 | base = bucket_block(xbs->bucket, block_off); |
| 5836 | vb.vb_bh = xbs->bucket->bu_bhs[block_off]; |
| 5837 | vb.vb_access = ocfs2_journal_access; |
| 5838 | |
| 5839 | if (ocfs2_meta_ecc(osb)) { |
| 5840 | /*create parameters for ocfs2_post_refcount. */ |
| 5841 | bucket = xbs->bucket; |
| 5842 | refcount.credits = bucket->bu_blocks; |
| 5843 | refcount.para = bucket; |
| 5844 | refcount.func = |
| 5845 | ocfs2_xattr_bucket_post_refcount; |
| 5846 | p = &refcount; |
| 5847 | } |
| 5848 | } else { |
| 5849 | base = xbs->base; |
| 5850 | vb.vb_bh = xbs->xattr_bh; |
| 5851 | vb.vb_access = ocfs2_journal_access_xb; |
| 5852 | } |
| 5853 | } |
| 5854 | |
| 5855 | if (ocfs2_xattr_is_local(xe)) |
| 5856 | goto out; |
| 5857 | |
| 5858 | vb.vb_xv = (struct ocfs2_xattr_value_root *) |
| 5859 | (base + name_offset + name_len); |
| 5860 | |
| 5861 | ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster, |
| 5862 | &num_clusters, &vb.vb_xv->xr_list, |
| 5863 | &ext_flags); |
| 5864 | if (ret) { |
| 5865 | mlog_errno(ret); |
| 5866 | goto out; |
| 5867 | } |
| 5868 | |
| 5869 | /* |
| 5870 | * We just need to check the 1st extent record, since we always |
| 5871 | * CoW the whole xattr. So there shouldn't be a xattr with |
| 5872 | * some REFCOUNT extent recs after the 1st one. |
| 5873 | */ |
| 5874 | if (!(ext_flags & OCFS2_EXT_REFCOUNTED)) |
| 5875 | goto out; |
| 5876 | |
| 5877 | ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc), |
| 5878 | 1, ref_tree, &ref_root_bh); |
| 5879 | if (ret) { |
| 5880 | mlog_errno(ret); |
| 5881 | goto out; |
| 5882 | } |
| 5883 | |
| 5884 | /* |
| 5885 | * If we are deleting the xattr or the new size will be stored inside, |
| 5886 | * cool, leave it there, the xattr truncate process will remove them |
| 5887 | * for us(it still needs the refcount tree lock and the meta, credits). |
| 5888 | * And the worse case is that every cluster truncate will split the |
| 5889 | * refcount tree, and make the original extent become 3. So we will need |
| 5890 | * 2 * cluster more extent recs at most. |
| 5891 | */ |
| 5892 | if (!xi->value || xi->value_len <= OCFS2_XATTR_INLINE_SIZE) { |
| 5893 | |
| 5894 | ret = ocfs2_refcounted_xattr_delete_need(inode, |
| 5895 | &(*ref_tree)->rf_ci, |
| 5896 | ref_root_bh, vb.vb_xv, |
| 5897 | meta_add, credits); |
| 5898 | if (ret) |
| 5899 | mlog_errno(ret); |
| 5900 | goto out; |
| 5901 | } |
| 5902 | |
| 5903 | ret = ocfs2_refcount_cow_xattr(inode, di, &vb, |
| 5904 | *ref_tree, ref_root_bh, 0, |
| 5905 | le32_to_cpu(vb.vb_xv->xr_clusters), p); |
| 5906 | if (ret) |
| 5907 | mlog_errno(ret); |
| 5908 | |
| 5909 | out: |
| 5910 | brelse(ref_root_bh); |
| 5911 | return ret; |
| 5912 | } |
| 5913 | |
| 5914 | /* |
Tao Ma | 0129241 | 2009-09-21 13:04:19 +0800 | [diff] [blame] | 5915 | * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root. |
| 5916 | * The physical clusters will be added to refcount tree. |
| 5917 | */ |
| 5918 | static int ocfs2_xattr_value_attach_refcount(struct inode *inode, |
| 5919 | struct ocfs2_xattr_value_root *xv, |
| 5920 | struct ocfs2_extent_tree *value_et, |
| 5921 | struct ocfs2_caching_info *ref_ci, |
| 5922 | struct buffer_head *ref_root_bh, |
| 5923 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 5924 | struct ocfs2_post_refcount *refcount) |
| 5925 | { |
| 5926 | int ret = 0; |
| 5927 | u32 clusters = le32_to_cpu(xv->xr_clusters); |
| 5928 | u32 cpos, p_cluster, num_clusters; |
| 5929 | struct ocfs2_extent_list *el = &xv->xr_list; |
| 5930 | unsigned int ext_flags; |
| 5931 | |
| 5932 | cpos = 0; |
| 5933 | while (cpos < clusters) { |
| 5934 | ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster, |
| 5935 | &num_clusters, el, &ext_flags); |
| 5936 | |
| 5937 | cpos += num_clusters; |
| 5938 | if ((ext_flags & OCFS2_EXT_REFCOUNTED)) |
| 5939 | continue; |
| 5940 | |
| 5941 | BUG_ON(!p_cluster); |
| 5942 | |
| 5943 | ret = ocfs2_add_refcount_flag(inode, value_et, |
| 5944 | ref_ci, ref_root_bh, |
| 5945 | cpos - num_clusters, |
| 5946 | p_cluster, num_clusters, |
| 5947 | dealloc, refcount); |
| 5948 | if (ret) { |
| 5949 | mlog_errno(ret); |
| 5950 | break; |
| 5951 | } |
| 5952 | } |
| 5953 | |
| 5954 | return ret; |
| 5955 | } |
| 5956 | |
| 5957 | /* |
| 5958 | * Given a normal ocfs2_xattr_header, refcount all the entries which |
| 5959 | * have value stored outside. |
| 5960 | * Used for xattrs stored in inode and ocfs2_xattr_block. |
| 5961 | */ |
| 5962 | static int ocfs2_xattr_attach_refcount_normal(struct inode *inode, |
| 5963 | struct ocfs2_xattr_value_buf *vb, |
| 5964 | struct ocfs2_xattr_header *header, |
| 5965 | struct ocfs2_caching_info *ref_ci, |
| 5966 | struct buffer_head *ref_root_bh, |
| 5967 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 5968 | { |
| 5969 | |
| 5970 | struct ocfs2_xattr_entry *xe; |
| 5971 | struct ocfs2_xattr_value_root *xv; |
| 5972 | struct ocfs2_extent_tree et; |
| 5973 | int i, ret = 0; |
| 5974 | |
| 5975 | for (i = 0; i < le16_to_cpu(header->xh_count); i++) { |
| 5976 | xe = &header->xh_entries[i]; |
| 5977 | |
| 5978 | if (ocfs2_xattr_is_local(xe)) |
| 5979 | continue; |
| 5980 | |
| 5981 | xv = (struct ocfs2_xattr_value_root *)((void *)header + |
| 5982 | le16_to_cpu(xe->xe_name_offset) + |
| 5983 | OCFS2_XATTR_SIZE(xe->xe_name_len)); |
| 5984 | |
| 5985 | vb->vb_xv = xv; |
| 5986 | ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb); |
| 5987 | |
| 5988 | ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et, |
| 5989 | ref_ci, ref_root_bh, |
| 5990 | dealloc, NULL); |
| 5991 | if (ret) { |
| 5992 | mlog_errno(ret); |
| 5993 | break; |
| 5994 | } |
| 5995 | } |
| 5996 | |
| 5997 | return ret; |
| 5998 | } |
| 5999 | |
| 6000 | static int ocfs2_xattr_inline_attach_refcount(struct inode *inode, |
| 6001 | struct buffer_head *fe_bh, |
| 6002 | struct ocfs2_caching_info *ref_ci, |
| 6003 | struct buffer_head *ref_root_bh, |
| 6004 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 6005 | { |
| 6006 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data; |
| 6007 | struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *) |
| 6008 | (fe_bh->b_data + inode->i_sb->s_blocksize - |
| 6009 | le16_to_cpu(di->i_xattr_inline_size)); |
| 6010 | struct ocfs2_xattr_value_buf vb = { |
| 6011 | .vb_bh = fe_bh, |
| 6012 | .vb_access = ocfs2_journal_access_di, |
| 6013 | }; |
| 6014 | |
| 6015 | return ocfs2_xattr_attach_refcount_normal(inode, &vb, header, |
| 6016 | ref_ci, ref_root_bh, dealloc); |
| 6017 | } |
| 6018 | |
| 6019 | struct ocfs2_xattr_tree_value_refcount_para { |
| 6020 | struct ocfs2_caching_info *ref_ci; |
| 6021 | struct buffer_head *ref_root_bh; |
| 6022 | struct ocfs2_cached_dealloc_ctxt *dealloc; |
| 6023 | }; |
| 6024 | |
| 6025 | static int ocfs2_get_xattr_tree_value_root(struct super_block *sb, |
| 6026 | struct ocfs2_xattr_bucket *bucket, |
| 6027 | int offset, |
| 6028 | struct ocfs2_xattr_value_root **xv, |
| 6029 | struct buffer_head **bh) |
| 6030 | { |
| 6031 | int ret, block_off, name_offset; |
| 6032 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
| 6033 | struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset]; |
| 6034 | void *base; |
| 6035 | |
| 6036 | ret = ocfs2_xattr_bucket_get_name_value(sb, |
| 6037 | bucket_xh(bucket), |
| 6038 | offset, |
| 6039 | &block_off, |
| 6040 | &name_offset); |
| 6041 | if (ret) { |
| 6042 | mlog_errno(ret); |
| 6043 | goto out; |
| 6044 | } |
| 6045 | |
| 6046 | base = bucket_block(bucket, block_off); |
| 6047 | |
| 6048 | *xv = (struct ocfs2_xattr_value_root *)(base + name_offset + |
| 6049 | OCFS2_XATTR_SIZE(xe->xe_name_len)); |
| 6050 | |
| 6051 | if (bh) |
| 6052 | *bh = bucket->bu_bhs[block_off]; |
| 6053 | out: |
| 6054 | return ret; |
| 6055 | } |
| 6056 | |
| 6057 | /* |
| 6058 | * For a given xattr bucket, refcount all the entries which |
| 6059 | * have value stored outside. |
| 6060 | */ |
| 6061 | static int ocfs2_xattr_bucket_value_refcount(struct inode *inode, |
| 6062 | struct ocfs2_xattr_bucket *bucket, |
| 6063 | void *para) |
| 6064 | { |
| 6065 | int i, ret = 0; |
| 6066 | struct ocfs2_extent_tree et; |
| 6067 | struct ocfs2_xattr_tree_value_refcount_para *ref = |
| 6068 | (struct ocfs2_xattr_tree_value_refcount_para *)para; |
| 6069 | struct ocfs2_xattr_header *xh = |
| 6070 | (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data; |
| 6071 | struct ocfs2_xattr_entry *xe; |
| 6072 | struct ocfs2_xattr_value_buf vb = { |
| 6073 | .vb_access = ocfs2_journal_access, |
| 6074 | }; |
| 6075 | struct ocfs2_post_refcount refcount = { |
| 6076 | .credits = bucket->bu_blocks, |
| 6077 | .para = bucket, |
| 6078 | .func = ocfs2_xattr_bucket_post_refcount, |
| 6079 | }; |
| 6080 | struct ocfs2_post_refcount *p = NULL; |
| 6081 | |
| 6082 | /* We only need post_refcount if we support metaecc. */ |
| 6083 | if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb))) |
| 6084 | p = &refcount; |
| 6085 | |
| 6086 | mlog(0, "refcount bucket %llu, count = %u\n", |
| 6087 | (unsigned long long)bucket_blkno(bucket), |
| 6088 | le16_to_cpu(xh->xh_count)); |
| 6089 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { |
| 6090 | xe = &xh->xh_entries[i]; |
| 6091 | |
| 6092 | if (ocfs2_xattr_is_local(xe)) |
| 6093 | continue; |
| 6094 | |
| 6095 | ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i, |
| 6096 | &vb.vb_xv, &vb.vb_bh); |
| 6097 | if (ret) { |
| 6098 | mlog_errno(ret); |
| 6099 | break; |
| 6100 | } |
| 6101 | |
| 6102 | ocfs2_init_xattr_value_extent_tree(&et, |
| 6103 | INODE_CACHE(inode), &vb); |
| 6104 | |
| 6105 | ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv, |
| 6106 | &et, ref->ref_ci, |
| 6107 | ref->ref_root_bh, |
| 6108 | ref->dealloc, p); |
| 6109 | if (ret) { |
| 6110 | mlog_errno(ret); |
| 6111 | break; |
| 6112 | } |
| 6113 | } |
| 6114 | |
| 6115 | return ret; |
| 6116 | |
| 6117 | } |
| 6118 | |
| 6119 | static int ocfs2_refcount_xattr_tree_rec(struct inode *inode, |
| 6120 | struct buffer_head *root_bh, |
| 6121 | u64 blkno, u32 cpos, u32 len, void *para) |
| 6122 | { |
| 6123 | return ocfs2_iterate_xattr_buckets(inode, blkno, len, |
| 6124 | ocfs2_xattr_bucket_value_refcount, |
| 6125 | para); |
| 6126 | } |
| 6127 | |
| 6128 | static int ocfs2_xattr_block_attach_refcount(struct inode *inode, |
| 6129 | struct buffer_head *blk_bh, |
| 6130 | struct ocfs2_caching_info *ref_ci, |
| 6131 | struct buffer_head *ref_root_bh, |
| 6132 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 6133 | { |
| 6134 | int ret = 0; |
| 6135 | struct ocfs2_xattr_block *xb = |
| 6136 | (struct ocfs2_xattr_block *)blk_bh->b_data; |
| 6137 | |
| 6138 | if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { |
| 6139 | struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header; |
| 6140 | struct ocfs2_xattr_value_buf vb = { |
| 6141 | .vb_bh = blk_bh, |
| 6142 | .vb_access = ocfs2_journal_access_xb, |
| 6143 | }; |
| 6144 | |
| 6145 | ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header, |
| 6146 | ref_ci, ref_root_bh, |
| 6147 | dealloc); |
| 6148 | } else { |
| 6149 | struct ocfs2_xattr_tree_value_refcount_para para = { |
| 6150 | .ref_ci = ref_ci, |
| 6151 | .ref_root_bh = ref_root_bh, |
| 6152 | .dealloc = dealloc, |
| 6153 | }; |
| 6154 | |
| 6155 | ret = ocfs2_iterate_xattr_index_block(inode, blk_bh, |
| 6156 | ocfs2_refcount_xattr_tree_rec, |
| 6157 | ¶); |
| 6158 | } |
| 6159 | |
| 6160 | return ret; |
| 6161 | } |
| 6162 | |
| 6163 | int ocfs2_xattr_attach_refcount_tree(struct inode *inode, |
| 6164 | struct buffer_head *fe_bh, |
| 6165 | struct ocfs2_caching_info *ref_ci, |
| 6166 | struct buffer_head *ref_root_bh, |
| 6167 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 6168 | { |
| 6169 | int ret = 0; |
| 6170 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 6171 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data; |
| 6172 | struct buffer_head *blk_bh = NULL; |
| 6173 | |
| 6174 | if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) { |
| 6175 | ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh, |
| 6176 | ref_ci, ref_root_bh, |
| 6177 | dealloc); |
| 6178 | if (ret) { |
| 6179 | mlog_errno(ret); |
| 6180 | goto out; |
| 6181 | } |
| 6182 | } |
| 6183 | |
| 6184 | if (!di->i_xattr_loc) |
| 6185 | goto out; |
| 6186 | |
| 6187 | ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc), |
| 6188 | &blk_bh); |
| 6189 | if (ret < 0) { |
| 6190 | mlog_errno(ret); |
| 6191 | goto out; |
| 6192 | } |
| 6193 | |
| 6194 | ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci, |
| 6195 | ref_root_bh, dealloc); |
| 6196 | if (ret) |
| 6197 | mlog_errno(ret); |
| 6198 | |
| 6199 | brelse(blk_bh); |
| 6200 | out: |
| 6201 | |
| 6202 | return ret; |
| 6203 | } |
| 6204 | |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6205 | typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe); |
Tao Ma | 0129241 | 2009-09-21 13:04:19 +0800 | [diff] [blame] | 6206 | /* |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 6207 | * Store the information we need in xattr reflink. |
| 6208 | * old_bh and new_bh are inode bh for the old and new inode. |
| 6209 | */ |
| 6210 | struct ocfs2_xattr_reflink { |
| 6211 | struct inode *old_inode; |
| 6212 | struct inode *new_inode; |
| 6213 | struct buffer_head *old_bh; |
| 6214 | struct buffer_head *new_bh; |
| 6215 | struct ocfs2_caching_info *ref_ci; |
| 6216 | struct buffer_head *ref_root_bh; |
| 6217 | struct ocfs2_cached_dealloc_ctxt *dealloc; |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6218 | should_xattr_reflinked *xattr_reflinked; |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 6219 | }; |
| 6220 | |
| 6221 | /* |
| 6222 | * Given a xattr header and xe offset, |
| 6223 | * return the proper xv and the corresponding bh. |
| 6224 | * xattr in inode, block and xattr tree have different implementaions. |
| 6225 | */ |
| 6226 | typedef int (get_xattr_value_root)(struct super_block *sb, |
| 6227 | struct buffer_head *bh, |
| 6228 | struct ocfs2_xattr_header *xh, |
| 6229 | int offset, |
| 6230 | struct ocfs2_xattr_value_root **xv, |
| 6231 | struct buffer_head **ret_bh, |
| 6232 | void *para); |
| 6233 | |
| 6234 | /* |
| 6235 | * Calculate all the xattr value root metadata stored in this xattr header and |
| 6236 | * credits we need if we create them from the scratch. |
| 6237 | * We use get_xattr_value_root so that all types of xattr container can use it. |
| 6238 | */ |
| 6239 | static int ocfs2_value_metas_in_xattr_header(struct super_block *sb, |
| 6240 | struct buffer_head *bh, |
| 6241 | struct ocfs2_xattr_header *xh, |
| 6242 | int *metas, int *credits, |
| 6243 | int *num_recs, |
| 6244 | get_xattr_value_root *func, |
| 6245 | void *para) |
| 6246 | { |
| 6247 | int i, ret = 0; |
| 6248 | struct ocfs2_xattr_value_root *xv; |
| 6249 | struct ocfs2_xattr_entry *xe; |
| 6250 | |
| 6251 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { |
| 6252 | xe = &xh->xh_entries[i]; |
| 6253 | if (ocfs2_xattr_is_local(xe)) |
| 6254 | continue; |
| 6255 | |
| 6256 | ret = func(sb, bh, xh, i, &xv, NULL, para); |
| 6257 | if (ret) { |
| 6258 | mlog_errno(ret); |
| 6259 | break; |
| 6260 | } |
| 6261 | |
| 6262 | *metas += le16_to_cpu(xv->xr_list.l_tree_depth) * |
| 6263 | le16_to_cpu(xv->xr_list.l_next_free_rec); |
| 6264 | |
| 6265 | *credits += ocfs2_calc_extend_credits(sb, |
| 6266 | &def_xv.xv.xr_list, |
| 6267 | le32_to_cpu(xv->xr_clusters)); |
| 6268 | |
| 6269 | /* |
| 6270 | * If the value is a tree with depth > 1, We don't go deep |
| 6271 | * to the extent block, so just calculate a maximum record num. |
| 6272 | */ |
| 6273 | if (!xv->xr_list.l_tree_depth) |
Tao Ma | 8ff6af8 | 2009-12-23 14:31:15 +0800 | [diff] [blame] | 6274 | *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec); |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 6275 | else |
| 6276 | *num_recs += ocfs2_clusters_for_bytes(sb, |
| 6277 | XATTR_SIZE_MAX); |
| 6278 | } |
| 6279 | |
| 6280 | return ret; |
| 6281 | } |
| 6282 | |
| 6283 | /* Used by xattr inode and block to return the right xv and buffer_head. */ |
| 6284 | static int ocfs2_get_xattr_value_root(struct super_block *sb, |
| 6285 | struct buffer_head *bh, |
| 6286 | struct ocfs2_xattr_header *xh, |
| 6287 | int offset, |
| 6288 | struct ocfs2_xattr_value_root **xv, |
| 6289 | struct buffer_head **ret_bh, |
| 6290 | void *para) |
| 6291 | { |
| 6292 | struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset]; |
| 6293 | |
| 6294 | *xv = (struct ocfs2_xattr_value_root *)((void *)xh + |
| 6295 | le16_to_cpu(xe->xe_name_offset) + |
| 6296 | OCFS2_XATTR_SIZE(xe->xe_name_len)); |
| 6297 | |
| 6298 | if (ret_bh) |
| 6299 | *ret_bh = bh; |
| 6300 | |
| 6301 | return 0; |
| 6302 | } |
| 6303 | |
| 6304 | /* |
| 6305 | * Lock the meta_ac and caculate how much credits we need for reflink xattrs. |
| 6306 | * It is only used for inline xattr and xattr block. |
| 6307 | */ |
| 6308 | static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb, |
| 6309 | struct ocfs2_xattr_header *xh, |
| 6310 | struct buffer_head *ref_root_bh, |
| 6311 | int *credits, |
| 6312 | struct ocfs2_alloc_context **meta_ac) |
| 6313 | { |
| 6314 | int ret, meta_add = 0, num_recs = 0; |
| 6315 | struct ocfs2_refcount_block *rb = |
| 6316 | (struct ocfs2_refcount_block *)ref_root_bh->b_data; |
| 6317 | |
| 6318 | *credits = 0; |
| 6319 | |
| 6320 | ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh, |
| 6321 | &meta_add, credits, &num_recs, |
| 6322 | ocfs2_get_xattr_value_root, |
| 6323 | NULL); |
| 6324 | if (ret) { |
| 6325 | mlog_errno(ret); |
| 6326 | goto out; |
| 6327 | } |
| 6328 | |
| 6329 | /* |
| 6330 | * We need to add/modify num_recs in refcount tree, so just calculate |
| 6331 | * an approximate number we need for refcount tree change. |
| 6332 | * Sometimes we need to split the tree, and after split, half recs |
| 6333 | * will be moved to the new block, and a new block can only provide |
| 6334 | * half number of recs. So we multiple new blocks by 2. |
| 6335 | */ |
| 6336 | num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2; |
| 6337 | meta_add += num_recs; |
| 6338 | *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS; |
| 6339 | if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL) |
| 6340 | *credits += le16_to_cpu(rb->rf_list.l_tree_depth) * |
| 6341 | le16_to_cpu(rb->rf_list.l_next_free_rec) + 1; |
| 6342 | else |
| 6343 | *credits += 1; |
| 6344 | |
| 6345 | ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac); |
| 6346 | if (ret) |
| 6347 | mlog_errno(ret); |
| 6348 | |
| 6349 | out: |
| 6350 | return ret; |
| 6351 | } |
| 6352 | |
| 6353 | /* |
| 6354 | * Given a xattr header, reflink all the xattrs in this container. |
| 6355 | * It can be used for inode, block and bucket. |
| 6356 | * |
| 6357 | * NOTE: |
| 6358 | * Before we call this function, the caller has memcpy the xattr in |
| 6359 | * old_xh to the new_xh. |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6360 | * |
| 6361 | * If args.xattr_reflinked is set, call it to decide whether the xe should |
| 6362 | * 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] | 6363 | */ |
| 6364 | static int ocfs2_reflink_xattr_header(handle_t *handle, |
| 6365 | struct ocfs2_xattr_reflink *args, |
| 6366 | struct buffer_head *old_bh, |
| 6367 | struct ocfs2_xattr_header *xh, |
| 6368 | struct buffer_head *new_bh, |
| 6369 | struct ocfs2_xattr_header *new_xh, |
| 6370 | struct ocfs2_xattr_value_buf *vb, |
| 6371 | struct ocfs2_alloc_context *meta_ac, |
| 6372 | get_xattr_value_root *func, |
| 6373 | void *para) |
| 6374 | { |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6375 | int ret = 0, i, j; |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 6376 | struct super_block *sb = args->old_inode->i_sb; |
| 6377 | struct buffer_head *value_bh; |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6378 | struct ocfs2_xattr_entry *xe, *last; |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 6379 | struct ocfs2_xattr_value_root *xv, *new_xv; |
| 6380 | struct ocfs2_extent_tree data_et; |
| 6381 | u32 clusters, cpos, p_cluster, num_clusters; |
| 6382 | unsigned int ext_flags = 0; |
| 6383 | |
| 6384 | mlog(0, "reflink xattr in container %llu, count = %u\n", |
| 6385 | (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] | 6386 | |
| 6387 | last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)]; |
| 6388 | 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] | 6389 | xe = &xh->xh_entries[i]; |
| 6390 | |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6391 | if (args->xattr_reflinked && !args->xattr_reflinked(xe)) { |
| 6392 | xe = &new_xh->xh_entries[j]; |
| 6393 | |
| 6394 | le16_add_cpu(&new_xh->xh_count, -1); |
| 6395 | if (new_xh->xh_count) { |
| 6396 | memmove(xe, xe + 1, |
| 6397 | (void *)last - (void *)xe); |
| 6398 | memset(last, 0, |
| 6399 | sizeof(struct ocfs2_xattr_entry)); |
| 6400 | } |
| 6401 | |
| 6402 | /* |
| 6403 | * We don't want j to increase in the next round since |
| 6404 | * it is already moved ahead. |
| 6405 | */ |
| 6406 | j--; |
| 6407 | continue; |
| 6408 | } |
| 6409 | |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 6410 | if (ocfs2_xattr_is_local(xe)) |
| 6411 | continue; |
| 6412 | |
| 6413 | ret = func(sb, old_bh, xh, i, &xv, NULL, para); |
| 6414 | if (ret) { |
| 6415 | mlog_errno(ret); |
| 6416 | break; |
| 6417 | } |
| 6418 | |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 6419 | 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] | 6420 | if (ret) { |
| 6421 | mlog_errno(ret); |
| 6422 | break; |
| 6423 | } |
| 6424 | |
| 6425 | /* |
| 6426 | * For the xattr which has l_tree_depth = 0, all the extent |
| 6427 | * recs have already be copied to the new xh with the |
| 6428 | * propriate OCFS2_EXT_REFCOUNTED flag we just need to |
| 6429 | * increase the refount count int the refcount tree. |
| 6430 | * |
| 6431 | * For the xattr which has l_tree_depth > 0, we need |
| 6432 | * to initialize it to the empty default value root, |
| 6433 | * and then insert the extents one by one. |
| 6434 | */ |
| 6435 | if (xv->xr_list.l_tree_depth) { |
| 6436 | memcpy(new_xv, &def_xv, sizeof(def_xv)); |
| 6437 | vb->vb_xv = new_xv; |
| 6438 | vb->vb_bh = value_bh; |
| 6439 | ocfs2_init_xattr_value_extent_tree(&data_et, |
| 6440 | INODE_CACHE(args->new_inode), vb); |
| 6441 | } |
| 6442 | |
| 6443 | clusters = le32_to_cpu(xv->xr_clusters); |
| 6444 | cpos = 0; |
| 6445 | while (cpos < clusters) { |
| 6446 | ret = ocfs2_xattr_get_clusters(args->old_inode, |
| 6447 | cpos, |
| 6448 | &p_cluster, |
| 6449 | &num_clusters, |
| 6450 | &xv->xr_list, |
| 6451 | &ext_flags); |
| 6452 | if (ret) { |
| 6453 | mlog_errno(ret); |
| 6454 | goto out; |
| 6455 | } |
| 6456 | |
| 6457 | BUG_ON(!p_cluster); |
| 6458 | |
| 6459 | if (xv->xr_list.l_tree_depth) { |
| 6460 | ret = ocfs2_insert_extent(handle, |
| 6461 | &data_et, cpos, |
| 6462 | ocfs2_clusters_to_blocks( |
| 6463 | args->old_inode->i_sb, |
| 6464 | p_cluster), |
| 6465 | num_clusters, ext_flags, |
| 6466 | meta_ac); |
| 6467 | if (ret) { |
| 6468 | mlog_errno(ret); |
| 6469 | goto out; |
| 6470 | } |
| 6471 | } |
| 6472 | |
| 6473 | ret = ocfs2_increase_refcount(handle, args->ref_ci, |
| 6474 | args->ref_root_bh, |
| 6475 | p_cluster, num_clusters, |
| 6476 | meta_ac, args->dealloc); |
| 6477 | if (ret) { |
| 6478 | mlog_errno(ret); |
| 6479 | goto out; |
| 6480 | } |
| 6481 | |
| 6482 | cpos += num_clusters; |
| 6483 | } |
| 6484 | } |
| 6485 | |
| 6486 | out: |
| 6487 | return ret; |
| 6488 | } |
| 6489 | |
| 6490 | static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args) |
| 6491 | { |
| 6492 | int ret = 0, credits = 0; |
| 6493 | handle_t *handle; |
| 6494 | struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb); |
| 6495 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data; |
| 6496 | int inline_size = le16_to_cpu(di->i_xattr_inline_size); |
| 6497 | int header_off = osb->sb->s_blocksize - inline_size; |
| 6498 | struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *) |
| 6499 | (args->old_bh->b_data + header_off); |
| 6500 | struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *) |
| 6501 | (args->new_bh->b_data + header_off); |
| 6502 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 6503 | struct ocfs2_inode_info *new_oi; |
| 6504 | struct ocfs2_dinode *new_di; |
| 6505 | struct ocfs2_xattr_value_buf vb = { |
| 6506 | .vb_bh = args->new_bh, |
| 6507 | .vb_access = ocfs2_journal_access_di, |
| 6508 | }; |
| 6509 | |
| 6510 | ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh, |
| 6511 | &credits, &meta_ac); |
| 6512 | if (ret) { |
| 6513 | mlog_errno(ret); |
| 6514 | goto out; |
| 6515 | } |
| 6516 | |
| 6517 | handle = ocfs2_start_trans(osb, credits); |
| 6518 | if (IS_ERR(handle)) { |
| 6519 | ret = PTR_ERR(handle); |
| 6520 | mlog_errno(ret); |
| 6521 | goto out; |
| 6522 | } |
| 6523 | |
| 6524 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode), |
| 6525 | args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE); |
| 6526 | if (ret) { |
| 6527 | mlog_errno(ret); |
| 6528 | goto out_commit; |
| 6529 | } |
| 6530 | |
| 6531 | memcpy(args->new_bh->b_data + header_off, |
| 6532 | args->old_bh->b_data + header_off, inline_size); |
| 6533 | |
| 6534 | new_di = (struct ocfs2_dinode *)args->new_bh->b_data; |
| 6535 | new_di->i_xattr_inline_size = cpu_to_le16(inline_size); |
| 6536 | |
| 6537 | ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh, |
| 6538 | args->new_bh, new_xh, &vb, meta_ac, |
| 6539 | ocfs2_get_xattr_value_root, NULL); |
| 6540 | if (ret) { |
| 6541 | mlog_errno(ret); |
| 6542 | goto out_commit; |
| 6543 | } |
| 6544 | |
| 6545 | new_oi = OCFS2_I(args->new_inode); |
| 6546 | spin_lock(&new_oi->ip_lock); |
| 6547 | new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL; |
| 6548 | new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features); |
| 6549 | spin_unlock(&new_oi->ip_lock); |
| 6550 | |
| 6551 | ocfs2_journal_dirty(handle, args->new_bh); |
| 6552 | |
| 6553 | out_commit: |
| 6554 | ocfs2_commit_trans(osb, handle); |
| 6555 | |
| 6556 | out: |
| 6557 | if (meta_ac) |
| 6558 | ocfs2_free_alloc_context(meta_ac); |
| 6559 | return ret; |
| 6560 | } |
| 6561 | |
| 6562 | static int ocfs2_create_empty_xattr_block(struct inode *inode, |
| 6563 | struct buffer_head *fe_bh, |
| 6564 | struct buffer_head **ret_bh, |
| 6565 | int indexed) |
| 6566 | { |
| 6567 | int ret; |
| 6568 | handle_t *handle; |
| 6569 | struct ocfs2_alloc_context *meta_ac; |
| 6570 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 6571 | |
| 6572 | ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac); |
| 6573 | if (ret < 0) { |
| 6574 | mlog_errno(ret); |
| 6575 | return ret; |
| 6576 | } |
| 6577 | |
| 6578 | handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS); |
| 6579 | if (IS_ERR(handle)) { |
| 6580 | ret = PTR_ERR(handle); |
| 6581 | mlog_errno(ret); |
| 6582 | goto out; |
| 6583 | } |
| 6584 | |
| 6585 | mlog(0, "create new xattr block for inode %llu, index = %d\n", |
| 6586 | (unsigned long long)fe_bh->b_blocknr, indexed); |
| 6587 | ret = ocfs2_create_xattr_block(handle, inode, fe_bh, |
| 6588 | meta_ac, ret_bh, indexed); |
| 6589 | if (ret) |
| 6590 | mlog_errno(ret); |
| 6591 | |
| 6592 | ocfs2_commit_trans(osb, handle); |
| 6593 | out: |
| 6594 | ocfs2_free_alloc_context(meta_ac); |
| 6595 | return ret; |
| 6596 | } |
| 6597 | |
| 6598 | static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args, |
| 6599 | struct buffer_head *blk_bh, |
| 6600 | struct buffer_head *new_blk_bh) |
| 6601 | { |
| 6602 | int ret = 0, credits = 0; |
| 6603 | handle_t *handle; |
| 6604 | struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode); |
| 6605 | struct ocfs2_dinode *new_di; |
| 6606 | struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb); |
| 6607 | int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); |
| 6608 | struct ocfs2_xattr_block *xb = |
| 6609 | (struct ocfs2_xattr_block *)blk_bh->b_data; |
| 6610 | struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header; |
| 6611 | struct ocfs2_xattr_block *new_xb = |
| 6612 | (struct ocfs2_xattr_block *)new_blk_bh->b_data; |
| 6613 | struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header; |
| 6614 | struct ocfs2_alloc_context *meta_ac; |
| 6615 | struct ocfs2_xattr_value_buf vb = { |
| 6616 | .vb_bh = new_blk_bh, |
| 6617 | .vb_access = ocfs2_journal_access_xb, |
| 6618 | }; |
| 6619 | |
| 6620 | ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh, |
| 6621 | &credits, &meta_ac); |
| 6622 | if (ret) { |
| 6623 | mlog_errno(ret); |
| 6624 | return ret; |
| 6625 | } |
| 6626 | |
| 6627 | /* One more credits in case we need to add xattr flags in new inode. */ |
| 6628 | handle = ocfs2_start_trans(osb, credits + 1); |
| 6629 | if (IS_ERR(handle)) { |
| 6630 | ret = PTR_ERR(handle); |
| 6631 | mlog_errno(ret); |
| 6632 | goto out; |
| 6633 | } |
| 6634 | |
| 6635 | if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) { |
| 6636 | ret = ocfs2_journal_access_di(handle, |
| 6637 | INODE_CACHE(args->new_inode), |
| 6638 | args->new_bh, |
| 6639 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 6640 | if (ret) { |
| 6641 | mlog_errno(ret); |
| 6642 | goto out_commit; |
| 6643 | } |
| 6644 | } |
| 6645 | |
| 6646 | ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode), |
| 6647 | new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE); |
| 6648 | if (ret) { |
| 6649 | mlog_errno(ret); |
| 6650 | goto out_commit; |
| 6651 | } |
| 6652 | |
| 6653 | memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off, |
| 6654 | osb->sb->s_blocksize - header_off); |
| 6655 | |
| 6656 | ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh, |
| 6657 | new_blk_bh, new_xh, &vb, meta_ac, |
| 6658 | ocfs2_get_xattr_value_root, NULL); |
| 6659 | if (ret) { |
| 6660 | mlog_errno(ret); |
| 6661 | goto out_commit; |
| 6662 | } |
| 6663 | |
| 6664 | ocfs2_journal_dirty(handle, new_blk_bh); |
| 6665 | |
| 6666 | if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) { |
| 6667 | new_di = (struct ocfs2_dinode *)args->new_bh->b_data; |
| 6668 | spin_lock(&new_oi->ip_lock); |
| 6669 | new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL; |
| 6670 | new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features); |
| 6671 | spin_unlock(&new_oi->ip_lock); |
| 6672 | |
| 6673 | ocfs2_journal_dirty(handle, args->new_bh); |
| 6674 | } |
| 6675 | |
| 6676 | out_commit: |
| 6677 | ocfs2_commit_trans(osb, handle); |
| 6678 | |
| 6679 | out: |
| 6680 | ocfs2_free_alloc_context(meta_ac); |
| 6681 | return ret; |
| 6682 | } |
| 6683 | |
| 6684 | struct ocfs2_reflink_xattr_tree_args { |
| 6685 | struct ocfs2_xattr_reflink *reflink; |
| 6686 | struct buffer_head *old_blk_bh; |
| 6687 | struct buffer_head *new_blk_bh; |
| 6688 | struct ocfs2_xattr_bucket *old_bucket; |
| 6689 | struct ocfs2_xattr_bucket *new_bucket; |
| 6690 | }; |
| 6691 | |
| 6692 | /* |
| 6693 | * NOTE: |
| 6694 | * We have to handle the case that both old bucket and new bucket |
| 6695 | * will call this function to get the right ret_bh. |
| 6696 | * So The caller must give us the right bh. |
| 6697 | */ |
| 6698 | static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb, |
| 6699 | struct buffer_head *bh, |
| 6700 | struct ocfs2_xattr_header *xh, |
| 6701 | int offset, |
| 6702 | struct ocfs2_xattr_value_root **xv, |
| 6703 | struct buffer_head **ret_bh, |
| 6704 | void *para) |
| 6705 | { |
| 6706 | struct ocfs2_reflink_xattr_tree_args *args = |
| 6707 | (struct ocfs2_reflink_xattr_tree_args *)para; |
| 6708 | struct ocfs2_xattr_bucket *bucket; |
| 6709 | |
| 6710 | if (bh == args->old_bucket->bu_bhs[0]) |
| 6711 | bucket = args->old_bucket; |
| 6712 | else |
| 6713 | bucket = args->new_bucket; |
| 6714 | |
| 6715 | return ocfs2_get_xattr_tree_value_root(sb, bucket, offset, |
| 6716 | xv, ret_bh); |
| 6717 | } |
| 6718 | |
| 6719 | struct ocfs2_value_tree_metas { |
| 6720 | int num_metas; |
| 6721 | int credits; |
| 6722 | int num_recs; |
| 6723 | }; |
| 6724 | |
| 6725 | static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb, |
| 6726 | struct buffer_head *bh, |
| 6727 | struct ocfs2_xattr_header *xh, |
| 6728 | int offset, |
| 6729 | struct ocfs2_xattr_value_root **xv, |
| 6730 | struct buffer_head **ret_bh, |
| 6731 | void *para) |
| 6732 | { |
| 6733 | struct ocfs2_xattr_bucket *bucket = |
| 6734 | (struct ocfs2_xattr_bucket *)para; |
| 6735 | |
| 6736 | return ocfs2_get_xattr_tree_value_root(sb, bucket, offset, |
| 6737 | xv, ret_bh); |
| 6738 | } |
| 6739 | |
| 6740 | static int ocfs2_calc_value_tree_metas(struct inode *inode, |
| 6741 | struct ocfs2_xattr_bucket *bucket, |
| 6742 | void *para) |
| 6743 | { |
| 6744 | struct ocfs2_value_tree_metas *metas = |
| 6745 | (struct ocfs2_value_tree_metas *)para; |
| 6746 | struct ocfs2_xattr_header *xh = |
| 6747 | (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data; |
| 6748 | |
| 6749 | /* Add the credits for this bucket first. */ |
| 6750 | metas->credits += bucket->bu_blocks; |
| 6751 | return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0], |
| 6752 | xh, &metas->num_metas, |
| 6753 | &metas->credits, &metas->num_recs, |
| 6754 | ocfs2_value_tree_metas_in_bucket, |
| 6755 | bucket); |
| 6756 | } |
| 6757 | |
| 6758 | /* |
| 6759 | * Given a xattr extent rec starting from blkno and having len clusters, |
| 6760 | * iterate all the buckets calculate how much metadata we need for reflinking |
| 6761 | * all the ocfs2_xattr_value_root and lock the allocators accordingly. |
| 6762 | */ |
| 6763 | static int ocfs2_lock_reflink_xattr_rec_allocators( |
| 6764 | struct ocfs2_reflink_xattr_tree_args *args, |
| 6765 | struct ocfs2_extent_tree *xt_et, |
| 6766 | u64 blkno, u32 len, int *credits, |
| 6767 | struct ocfs2_alloc_context **meta_ac, |
| 6768 | struct ocfs2_alloc_context **data_ac) |
| 6769 | { |
| 6770 | int ret, num_free_extents; |
| 6771 | struct ocfs2_value_tree_metas metas; |
| 6772 | struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb); |
| 6773 | struct ocfs2_refcount_block *rb; |
| 6774 | |
| 6775 | memset(&metas, 0, sizeof(metas)); |
| 6776 | |
| 6777 | ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len, |
| 6778 | ocfs2_calc_value_tree_metas, &metas); |
| 6779 | if (ret) { |
| 6780 | mlog_errno(ret); |
| 6781 | goto out; |
| 6782 | } |
| 6783 | |
| 6784 | *credits = metas.credits; |
| 6785 | |
| 6786 | /* |
| 6787 | * Calculate we need for refcount tree change. |
| 6788 | * |
| 6789 | * We need to add/modify num_recs in refcount tree, so just calculate |
| 6790 | * an approximate number we need for refcount tree change. |
| 6791 | * Sometimes we need to split the tree, and after split, half recs |
| 6792 | * will be moved to the new block, and a new block can only provide |
| 6793 | * half number of recs. So we multiple new blocks by 2. |
| 6794 | * In the end, we have to add credits for modifying the already |
| 6795 | * existed refcount block. |
| 6796 | */ |
| 6797 | rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data; |
| 6798 | metas.num_recs = |
| 6799 | (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) / |
| 6800 | ocfs2_refcount_recs_per_rb(osb->sb) * 2; |
| 6801 | metas.num_metas += metas.num_recs; |
| 6802 | *credits += metas.num_recs + |
| 6803 | metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS; |
| 6804 | if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL) |
| 6805 | *credits += le16_to_cpu(rb->rf_list.l_tree_depth) * |
| 6806 | le16_to_cpu(rb->rf_list.l_next_free_rec) + 1; |
| 6807 | else |
| 6808 | *credits += 1; |
| 6809 | |
| 6810 | /* count in the xattr tree change. */ |
| 6811 | num_free_extents = ocfs2_num_free_extents(osb, xt_et); |
| 6812 | if (num_free_extents < 0) { |
| 6813 | ret = num_free_extents; |
| 6814 | mlog_errno(ret); |
| 6815 | goto out; |
| 6816 | } |
| 6817 | |
| 6818 | if (num_free_extents < len) |
| 6819 | metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el); |
| 6820 | |
| 6821 | *credits += ocfs2_calc_extend_credits(osb->sb, |
| 6822 | xt_et->et_root_el, len); |
| 6823 | |
| 6824 | if (metas.num_metas) { |
| 6825 | ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas, |
| 6826 | meta_ac); |
| 6827 | if (ret) { |
| 6828 | mlog_errno(ret); |
| 6829 | goto out; |
| 6830 | } |
| 6831 | } |
| 6832 | |
| 6833 | if (len) { |
| 6834 | ret = ocfs2_reserve_clusters(osb, len, data_ac); |
| 6835 | if (ret) |
| 6836 | mlog_errno(ret); |
| 6837 | } |
| 6838 | out: |
| 6839 | if (ret) { |
| 6840 | if (*meta_ac) { |
| 6841 | ocfs2_free_alloc_context(*meta_ac); |
| 6842 | meta_ac = NULL; |
| 6843 | } |
| 6844 | } |
| 6845 | |
| 6846 | return ret; |
| 6847 | } |
| 6848 | |
| 6849 | static int ocfs2_reflink_xattr_buckets(handle_t *handle, |
| 6850 | u64 blkno, u64 new_blkno, u32 clusters, |
| 6851 | struct ocfs2_alloc_context *meta_ac, |
| 6852 | struct ocfs2_alloc_context *data_ac, |
| 6853 | struct ocfs2_reflink_xattr_tree_args *args) |
| 6854 | { |
| 6855 | int i, j, ret = 0; |
| 6856 | struct super_block *sb = args->reflink->old_inode->i_sb; |
| 6857 | u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb)); |
| 6858 | u32 num_buckets = clusters * bpc; |
| 6859 | int bpb = args->old_bucket->bu_blocks; |
| 6860 | struct ocfs2_xattr_value_buf vb = { |
| 6861 | .vb_access = ocfs2_journal_access, |
| 6862 | }; |
| 6863 | |
| 6864 | for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) { |
| 6865 | ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno); |
| 6866 | if (ret) { |
| 6867 | mlog_errno(ret); |
| 6868 | break; |
| 6869 | } |
| 6870 | |
| 6871 | ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno); |
| 6872 | if (ret) { |
| 6873 | mlog_errno(ret); |
| 6874 | break; |
| 6875 | } |
| 6876 | |
| 6877 | /* |
| 6878 | * The real bucket num in this series of blocks is stored |
| 6879 | * in the 1st bucket. |
| 6880 | */ |
| 6881 | if (i == 0) |
| 6882 | num_buckets = le16_to_cpu( |
| 6883 | bucket_xh(args->old_bucket)->xh_num_buckets); |
| 6884 | |
| 6885 | ret = ocfs2_xattr_bucket_journal_access(handle, |
| 6886 | args->new_bucket, |
| 6887 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 6888 | if (ret) { |
| 6889 | mlog_errno(ret); |
| 6890 | break; |
| 6891 | } |
| 6892 | |
| 6893 | for (j = 0; j < bpb; j++) |
| 6894 | memcpy(bucket_block(args->new_bucket, j), |
| 6895 | bucket_block(args->old_bucket, j), |
| 6896 | sb->s_blocksize); |
| 6897 | |
| 6898 | ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket); |
| 6899 | |
| 6900 | ret = ocfs2_reflink_xattr_header(handle, args->reflink, |
| 6901 | args->old_bucket->bu_bhs[0], |
| 6902 | bucket_xh(args->old_bucket), |
| 6903 | args->new_bucket->bu_bhs[0], |
| 6904 | bucket_xh(args->new_bucket), |
| 6905 | &vb, meta_ac, |
| 6906 | ocfs2_get_reflink_xattr_value_root, |
| 6907 | args); |
| 6908 | if (ret) { |
| 6909 | mlog_errno(ret); |
| 6910 | break; |
| 6911 | } |
| 6912 | |
| 6913 | /* |
| 6914 | * Re-access and dirty the bucket to calculate metaecc. |
| 6915 | * Because we may extend the transaction in reflink_xattr_header |
| 6916 | * which will let the already accessed block gone. |
| 6917 | */ |
| 6918 | ret = ocfs2_xattr_bucket_journal_access(handle, |
| 6919 | args->new_bucket, |
| 6920 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 6921 | if (ret) { |
| 6922 | mlog_errno(ret); |
| 6923 | break; |
| 6924 | } |
| 6925 | |
| 6926 | ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket); |
| 6927 | ocfs2_xattr_bucket_relse(args->old_bucket); |
| 6928 | ocfs2_xattr_bucket_relse(args->new_bucket); |
| 6929 | } |
| 6930 | |
| 6931 | ocfs2_xattr_bucket_relse(args->old_bucket); |
| 6932 | ocfs2_xattr_bucket_relse(args->new_bucket); |
| 6933 | return ret; |
| 6934 | } |
| 6935 | /* |
| 6936 | * Create the same xattr extent record in the new inode's xattr tree. |
| 6937 | */ |
| 6938 | static int ocfs2_reflink_xattr_rec(struct inode *inode, |
| 6939 | struct buffer_head *root_bh, |
| 6940 | u64 blkno, |
| 6941 | u32 cpos, |
| 6942 | u32 len, |
| 6943 | void *para) |
| 6944 | { |
| 6945 | int ret, credits = 0; |
| 6946 | u32 p_cluster, num_clusters; |
| 6947 | u64 new_blkno; |
| 6948 | handle_t *handle; |
| 6949 | struct ocfs2_reflink_xattr_tree_args *args = |
| 6950 | (struct ocfs2_reflink_xattr_tree_args *)para; |
| 6951 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 6952 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 6953 | struct ocfs2_alloc_context *data_ac = NULL; |
| 6954 | struct ocfs2_extent_tree et; |
| 6955 | |
| 6956 | ocfs2_init_xattr_tree_extent_tree(&et, |
| 6957 | INODE_CACHE(args->reflink->new_inode), |
| 6958 | args->new_blk_bh); |
| 6959 | |
| 6960 | ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno, |
| 6961 | len, &credits, |
| 6962 | &meta_ac, &data_ac); |
| 6963 | if (ret) { |
| 6964 | mlog_errno(ret); |
| 6965 | goto out; |
| 6966 | } |
| 6967 | |
| 6968 | handle = ocfs2_start_trans(osb, credits); |
| 6969 | if (IS_ERR(handle)) { |
| 6970 | ret = PTR_ERR(handle); |
| 6971 | mlog_errno(ret); |
| 6972 | goto out; |
| 6973 | } |
| 6974 | |
| 6975 | ret = ocfs2_claim_clusters(osb, handle, data_ac, |
| 6976 | len, &p_cluster, &num_clusters); |
| 6977 | if (ret) { |
| 6978 | mlog_errno(ret); |
| 6979 | goto out_commit; |
| 6980 | } |
| 6981 | |
| 6982 | new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster); |
| 6983 | |
| 6984 | mlog(0, "reflink xattr buckets %llu to %llu, len %u\n", |
| 6985 | (unsigned long long)blkno, (unsigned long long)new_blkno, len); |
| 6986 | ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len, |
| 6987 | meta_ac, data_ac, args); |
| 6988 | if (ret) { |
| 6989 | mlog_errno(ret); |
| 6990 | goto out_commit; |
| 6991 | } |
| 6992 | |
| 6993 | mlog(0, "insert new xattr extent rec start %llu len %u to %u\n", |
| 6994 | (unsigned long long)new_blkno, len, cpos); |
| 6995 | ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno, |
| 6996 | len, 0, meta_ac); |
| 6997 | if (ret) |
| 6998 | mlog_errno(ret); |
| 6999 | |
| 7000 | out_commit: |
| 7001 | ocfs2_commit_trans(osb, handle); |
| 7002 | |
| 7003 | out: |
| 7004 | if (meta_ac) |
| 7005 | ocfs2_free_alloc_context(meta_ac); |
| 7006 | if (data_ac) |
| 7007 | ocfs2_free_alloc_context(data_ac); |
| 7008 | return ret; |
| 7009 | } |
| 7010 | |
| 7011 | /* |
| 7012 | * Create reflinked xattr buckets. |
| 7013 | * We will add bucket one by one, and refcount all the xattrs in the bucket |
| 7014 | * if they are stored outside. |
| 7015 | */ |
| 7016 | static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args, |
| 7017 | struct buffer_head *blk_bh, |
| 7018 | struct buffer_head *new_blk_bh) |
| 7019 | { |
| 7020 | int ret; |
| 7021 | struct ocfs2_reflink_xattr_tree_args para; |
| 7022 | |
| 7023 | memset(¶, 0, sizeof(para)); |
| 7024 | para.reflink = args; |
| 7025 | para.old_blk_bh = blk_bh; |
| 7026 | para.new_blk_bh = new_blk_bh; |
| 7027 | |
| 7028 | para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode); |
| 7029 | if (!para.old_bucket) { |
| 7030 | mlog_errno(-ENOMEM); |
| 7031 | return -ENOMEM; |
| 7032 | } |
| 7033 | |
| 7034 | para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode); |
| 7035 | if (!para.new_bucket) { |
| 7036 | ret = -ENOMEM; |
| 7037 | mlog_errno(ret); |
| 7038 | goto out; |
| 7039 | } |
| 7040 | |
| 7041 | ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh, |
| 7042 | ocfs2_reflink_xattr_rec, |
| 7043 | ¶); |
| 7044 | if (ret) |
| 7045 | mlog_errno(ret); |
| 7046 | |
| 7047 | out: |
| 7048 | ocfs2_xattr_bucket_free(para.old_bucket); |
| 7049 | ocfs2_xattr_bucket_free(para.new_bucket); |
| 7050 | return ret; |
| 7051 | } |
| 7052 | |
| 7053 | static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args, |
| 7054 | struct buffer_head *blk_bh) |
| 7055 | { |
| 7056 | int ret, indexed = 0; |
| 7057 | struct buffer_head *new_blk_bh = NULL; |
| 7058 | struct ocfs2_xattr_block *xb = |
| 7059 | (struct ocfs2_xattr_block *)blk_bh->b_data; |
| 7060 | |
| 7061 | |
| 7062 | if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) |
| 7063 | indexed = 1; |
| 7064 | |
| 7065 | ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh, |
| 7066 | &new_blk_bh, indexed); |
| 7067 | if (ret) { |
| 7068 | mlog_errno(ret); |
| 7069 | goto out; |
| 7070 | } |
| 7071 | |
| 7072 | if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) |
| 7073 | ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh); |
| 7074 | else |
| 7075 | ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh); |
| 7076 | if (ret) |
| 7077 | mlog_errno(ret); |
| 7078 | |
| 7079 | out: |
| 7080 | brelse(new_blk_bh); |
| 7081 | return ret; |
| 7082 | } |
| 7083 | |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 7084 | static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe) |
| 7085 | { |
| 7086 | int type = ocfs2_xattr_get_type(xe); |
| 7087 | |
| 7088 | return type != OCFS2_XATTR_INDEX_SECURITY && |
| 7089 | type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS && |
| 7090 | type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT; |
| 7091 | } |
| 7092 | |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 7093 | int ocfs2_reflink_xattrs(struct inode *old_inode, |
| 7094 | struct buffer_head *old_bh, |
| 7095 | struct inode *new_inode, |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 7096 | struct buffer_head *new_bh, |
| 7097 | bool preserve_security) |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 7098 | { |
| 7099 | int ret; |
| 7100 | struct ocfs2_xattr_reflink args; |
| 7101 | struct ocfs2_inode_info *oi = OCFS2_I(old_inode); |
| 7102 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data; |
| 7103 | struct buffer_head *blk_bh = NULL; |
| 7104 | struct ocfs2_cached_dealloc_ctxt dealloc; |
| 7105 | struct ocfs2_refcount_tree *ref_tree; |
| 7106 | struct buffer_head *ref_root_bh = NULL; |
| 7107 | |
| 7108 | ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb), |
| 7109 | le64_to_cpu(di->i_refcount_loc), |
| 7110 | 1, &ref_tree, &ref_root_bh); |
| 7111 | if (ret) { |
| 7112 | mlog_errno(ret); |
| 7113 | goto out; |
| 7114 | } |
| 7115 | |
| 7116 | ocfs2_init_dealloc_ctxt(&dealloc); |
| 7117 | |
| 7118 | args.old_inode = old_inode; |
| 7119 | args.new_inode = new_inode; |
| 7120 | args.old_bh = old_bh; |
| 7121 | args.new_bh = new_bh; |
| 7122 | args.ref_ci = &ref_tree->rf_ci; |
| 7123 | args.ref_root_bh = ref_root_bh; |
| 7124 | args.dealloc = &dealloc; |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 7125 | if (preserve_security) |
| 7126 | args.xattr_reflinked = NULL; |
| 7127 | else |
| 7128 | args.xattr_reflinked = ocfs2_reflink_xattr_no_security; |
Tao Ma | 2999d12 | 2009-08-18 11:43:55 +0800 | [diff] [blame] | 7129 | |
| 7130 | if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) { |
| 7131 | ret = ocfs2_reflink_xattr_inline(&args); |
| 7132 | if (ret) { |
| 7133 | mlog_errno(ret); |
| 7134 | goto out_unlock; |
| 7135 | } |
| 7136 | } |
| 7137 | |
| 7138 | if (!di->i_xattr_loc) |
| 7139 | goto out_unlock; |
| 7140 | |
| 7141 | ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc), |
| 7142 | &blk_bh); |
| 7143 | if (ret < 0) { |
| 7144 | mlog_errno(ret); |
| 7145 | goto out_unlock; |
| 7146 | } |
| 7147 | |
| 7148 | ret = ocfs2_reflink_xattr_in_block(&args, blk_bh); |
| 7149 | if (ret) |
| 7150 | mlog_errno(ret); |
| 7151 | |
| 7152 | brelse(blk_bh); |
| 7153 | |
| 7154 | out_unlock: |
| 7155 | ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb), |
| 7156 | ref_tree, 1); |
| 7157 | brelse(ref_root_bh); |
| 7158 | |
| 7159 | if (ocfs2_dealloc_has_cluster(&dealloc)) { |
| 7160 | ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1); |
| 7161 | ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc); |
| 7162 | } |
| 7163 | |
| 7164 | out: |
| 7165 | return ret; |
| 7166 | } |
| 7167 | |
| 7168 | /* |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 7169 | * Initialize security and acl for a already created inode. |
| 7170 | * Used for reflink a non-preserve-security file. |
| 7171 | * |
| 7172 | * It uses common api like ocfs2_xattr_set, so the caller |
| 7173 | * must not hold any lock expect i_mutex. |
| 7174 | */ |
| 7175 | int ocfs2_init_security_and_acl(struct inode *dir, |
| 7176 | struct inode *inode) |
| 7177 | { |
| 7178 | int ret = 0; |
| 7179 | struct buffer_head *dir_bh = NULL; |
| 7180 | struct ocfs2_security_xattr_info si = { |
| 7181 | .enable = 1, |
| 7182 | }; |
| 7183 | |
| 7184 | ret = ocfs2_init_security_get(inode, dir, &si); |
| 7185 | if (!ret) { |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7186 | ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, |
| 7187 | si.name, si.value, si.value_len, |
| 7188 | XATTR_CREATE); |
Tao Ma | 0fe9b66 | 2009-08-18 11:47:56 +0800 | [diff] [blame] | 7189 | if (ret) { |
| 7190 | mlog_errno(ret); |
| 7191 | goto leave; |
| 7192 | } |
| 7193 | } else if (ret != -EOPNOTSUPP) { |
| 7194 | mlog_errno(ret); |
| 7195 | goto leave; |
| 7196 | } |
| 7197 | |
| 7198 | ret = ocfs2_inode_lock(dir, &dir_bh, 0); |
| 7199 | if (ret) { |
| 7200 | mlog_errno(ret); |
| 7201 | goto leave; |
| 7202 | } |
| 7203 | |
| 7204 | ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL); |
| 7205 | if (ret) |
| 7206 | mlog_errno(ret); |
| 7207 | |
| 7208 | ocfs2_inode_unlock(dir, 0); |
| 7209 | brelse(dir_bh); |
| 7210 | leave: |
| 7211 | return ret; |
| 7212 | } |
| 7213 | /* |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 7214 | * 'security' attributes support |
| 7215 | */ |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7216 | static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list, |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 7217 | size_t list_size, const char *name, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7218 | size_t name_len, int type) |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 7219 | { |
| 7220 | const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN; |
| 7221 | const size_t total_len = prefix_len + name_len + 1; |
| 7222 | |
| 7223 | if (list && total_len <= list_size) { |
| 7224 | memcpy(list, XATTR_SECURITY_PREFIX, prefix_len); |
| 7225 | memcpy(list + prefix_len, name, name_len); |
| 7226 | list[prefix_len + name_len] = '\0'; |
| 7227 | } |
| 7228 | return total_len; |
| 7229 | } |
| 7230 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7231 | static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name, |
| 7232 | void *buffer, size_t size, int type) |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 7233 | { |
| 7234 | if (strcmp(name, "") == 0) |
| 7235 | return -EINVAL; |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7236 | return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY, |
| 7237 | name, buffer, size); |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 7238 | } |
| 7239 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7240 | static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name, |
| 7241 | const void *value, size_t size, int flags, int type) |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 7242 | { |
| 7243 | if (strcmp(name, "") == 0) |
| 7244 | return -EINVAL; |
| 7245 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7246 | return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY, |
| 7247 | name, value, size, flags); |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 7248 | } |
| 7249 | |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 7250 | int ocfs2_init_security_get(struct inode *inode, |
| 7251 | struct inode *dir, |
| 7252 | struct ocfs2_security_xattr_info *si) |
| 7253 | { |
Tiger Yang | 38d59ef | 2008-12-17 10:22:56 +0800 | [diff] [blame] | 7254 | /* check whether ocfs2 support feature xattr */ |
| 7255 | if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb))) |
| 7256 | return -EOPNOTSUPP; |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 7257 | return security_inode_init_security(inode, dir, &si->name, &si->value, |
| 7258 | &si->value_len); |
| 7259 | } |
| 7260 | |
| 7261 | int ocfs2_init_security_set(handle_t *handle, |
| 7262 | struct inode *inode, |
| 7263 | struct buffer_head *di_bh, |
| 7264 | struct ocfs2_security_xattr_info *si, |
| 7265 | struct ocfs2_alloc_context *xattr_ac, |
| 7266 | struct ocfs2_alloc_context *data_ac) |
| 7267 | { |
| 7268 | return ocfs2_xattr_set_handle(handle, inode, di_bh, |
| 7269 | OCFS2_XATTR_INDEX_SECURITY, |
| 7270 | si->name, si->value, si->value_len, 0, |
| 7271 | xattr_ac, data_ac); |
| 7272 | } |
| 7273 | |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 7274 | struct xattr_handler ocfs2_xattr_security_handler = { |
| 7275 | .prefix = XATTR_SECURITY_PREFIX, |
| 7276 | .list = ocfs2_xattr_security_list, |
| 7277 | .get = ocfs2_xattr_security_get, |
| 7278 | .set = ocfs2_xattr_security_set, |
| 7279 | }; |
| 7280 | |
| 7281 | /* |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7282 | * 'trusted' attributes support |
| 7283 | */ |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7284 | static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list, |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7285 | size_t list_size, const char *name, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7286 | size_t name_len, int type) |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7287 | { |
Tiger Yang | ceb1eba | 2008-10-23 16:34:13 +0800 | [diff] [blame] | 7288 | const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN; |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7289 | const size_t total_len = prefix_len + name_len + 1; |
| 7290 | |
| 7291 | if (list && total_len <= list_size) { |
| 7292 | memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len); |
| 7293 | memcpy(list + prefix_len, name, name_len); |
| 7294 | list[prefix_len + name_len] = '\0'; |
| 7295 | } |
| 7296 | return total_len; |
| 7297 | } |
| 7298 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7299 | static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name, |
| 7300 | void *buffer, size_t size, int type) |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7301 | { |
| 7302 | if (strcmp(name, "") == 0) |
| 7303 | return -EINVAL; |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7304 | return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED, |
| 7305 | name, buffer, size); |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7306 | } |
| 7307 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7308 | static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name, |
| 7309 | const void *value, size_t size, int flags, int type) |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7310 | { |
| 7311 | if (strcmp(name, "") == 0) |
| 7312 | return -EINVAL; |
| 7313 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7314 | return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED, |
| 7315 | name, value, size, flags); |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7316 | } |
| 7317 | |
| 7318 | struct xattr_handler ocfs2_xattr_trusted_handler = { |
| 7319 | .prefix = XATTR_TRUSTED_PREFIX, |
| 7320 | .list = ocfs2_xattr_trusted_list, |
| 7321 | .get = ocfs2_xattr_trusted_get, |
| 7322 | .set = ocfs2_xattr_trusted_set, |
| 7323 | }; |
| 7324 | |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7325 | /* |
| 7326 | * 'user' attributes support |
| 7327 | */ |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7328 | static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list, |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7329 | size_t list_size, const char *name, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7330 | size_t name_len, int type) |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7331 | { |
Tiger Yang | ceb1eba | 2008-10-23 16:34:13 +0800 | [diff] [blame] | 7332 | const size_t prefix_len = XATTR_USER_PREFIX_LEN; |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7333 | const size_t total_len = prefix_len + name_len + 1; |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7334 | struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb); |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7335 | |
| 7336 | if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) |
| 7337 | return 0; |
| 7338 | |
| 7339 | if (list && total_len <= list_size) { |
| 7340 | memcpy(list, XATTR_USER_PREFIX, prefix_len); |
| 7341 | memcpy(list + prefix_len, name, name_len); |
| 7342 | list[prefix_len + name_len] = '\0'; |
| 7343 | } |
| 7344 | return total_len; |
| 7345 | } |
| 7346 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7347 | static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name, |
| 7348 | void *buffer, size_t size, int type) |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7349 | { |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7350 | struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb); |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7351 | |
| 7352 | if (strcmp(name, "") == 0) |
| 7353 | return -EINVAL; |
| 7354 | if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) |
| 7355 | return -EOPNOTSUPP; |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7356 | return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name, |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7357 | buffer, size); |
| 7358 | } |
| 7359 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7360 | static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name, |
| 7361 | const void *value, size_t size, int flags, int type) |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7362 | { |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7363 | struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb); |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7364 | |
| 7365 | if (strcmp(name, "") == 0) |
| 7366 | return -EINVAL; |
| 7367 | if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) |
| 7368 | return -EOPNOTSUPP; |
| 7369 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 7370 | return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER, |
| 7371 | name, value, size, flags); |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 7372 | } |
| 7373 | |
| 7374 | struct xattr_handler ocfs2_xattr_user_handler = { |
| 7375 | .prefix = XATTR_USER_PREFIX, |
| 7376 | .list = ocfs2_xattr_user_list, |
| 7377 | .get = ocfs2_xattr_user_get, |
| 7378 | .set = ocfs2_xattr_user_set, |
| 7379 | }; |