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" |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 59 | |
| 60 | struct ocfs2_xattr_def_value_root { |
| 61 | struct ocfs2_xattr_value_root xv; |
| 62 | struct ocfs2_extent_rec er; |
| 63 | }; |
| 64 | |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 65 | struct ocfs2_xattr_bucket { |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 66 | /* The inode these xattrs are associated with */ |
| 67 | struct inode *bu_inode; |
| 68 | |
| 69 | /* The actual buffers that make up the bucket */ |
Joel Becker | 4ac6032 | 2008-10-18 19:11:42 -0700 | [diff] [blame] | 70 | struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET]; |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 71 | |
| 72 | /* How many blocks make up one bucket for this filesystem */ |
| 73 | int bu_blocks; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 74 | }; |
| 75 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 76 | struct ocfs2_xattr_set_ctxt { |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 77 | handle_t *handle; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 78 | struct ocfs2_alloc_context *meta_ac; |
| 79 | struct ocfs2_alloc_context *data_ac; |
| 80 | struct ocfs2_cached_dealloc_ctxt dealloc; |
| 81 | }; |
| 82 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 83 | #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root)) |
| 84 | #define OCFS2_XATTR_INLINE_SIZE 80 |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 85 | #define OCFS2_XATTR_HEADER_GAP 4 |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 86 | #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \ |
| 87 | - sizeof(struct ocfs2_xattr_header) \ |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 88 | - OCFS2_XATTR_HEADER_GAP) |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 89 | #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \ |
| 90 | - sizeof(struct ocfs2_xattr_block) \ |
| 91 | - sizeof(struct ocfs2_xattr_header) \ |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 92 | - OCFS2_XATTR_HEADER_GAP) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 93 | |
| 94 | static struct ocfs2_xattr_def_value_root def_xv = { |
| 95 | .xv.xr_list.l_count = cpu_to_le16(1), |
| 96 | }; |
| 97 | |
| 98 | struct xattr_handler *ocfs2_xattr_handlers[] = { |
| 99 | &ocfs2_xattr_user_handler, |
Tiger Yang | 929fb01 | 2008-11-14 11:17:04 +0800 | [diff] [blame] | 100 | #ifdef CONFIG_OCFS2_FS_POSIX_ACL |
| 101 | &ocfs2_xattr_acl_access_handler, |
| 102 | &ocfs2_xattr_acl_default_handler, |
| 103 | #endif |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 104 | &ocfs2_xattr_trusted_handler, |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 105 | &ocfs2_xattr_security_handler, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 106 | NULL |
| 107 | }; |
| 108 | |
Tiger Yang | c988fd0 | 2008-10-23 16:34:44 +0800 | [diff] [blame] | 109 | static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = { |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 110 | [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler, |
Tiger Yang | 929fb01 | 2008-11-14 11:17:04 +0800 | [diff] [blame] | 111 | #ifdef CONFIG_OCFS2_FS_POSIX_ACL |
| 112 | [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS] |
| 113 | = &ocfs2_xattr_acl_access_handler, |
| 114 | [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT] |
| 115 | = &ocfs2_xattr_acl_default_handler, |
| 116 | #endif |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 117 | [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler, |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 118 | [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | struct ocfs2_xattr_info { |
| 122 | int name_index; |
| 123 | const char *name; |
| 124 | const void *value; |
| 125 | size_t value_len; |
| 126 | }; |
| 127 | |
| 128 | struct ocfs2_xattr_search { |
| 129 | struct buffer_head *inode_bh; |
| 130 | /* |
| 131 | * xattr_bh point to the block buffer head which has extended attribute |
| 132 | * when extended attribute in inode, xattr_bh is equal to inode_bh. |
| 133 | */ |
| 134 | struct buffer_head *xattr_bh; |
| 135 | struct ocfs2_xattr_header *header; |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 136 | struct ocfs2_xattr_bucket *bucket; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 137 | void *base; |
| 138 | void *end; |
| 139 | struct ocfs2_xattr_entry *here; |
| 140 | int not_found; |
| 141 | }; |
| 142 | |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 143 | static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb, |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 144 | struct ocfs2_xattr_header *xh, |
| 145 | int index, |
| 146 | int *block_off, |
| 147 | int *new_offset); |
| 148 | |
Joel Becker | 54f443f | 2008-10-20 18:43:07 -0700 | [diff] [blame] | 149 | static int ocfs2_xattr_block_find(struct inode *inode, |
| 150 | int name_index, |
| 151 | const char *name, |
| 152 | struct ocfs2_xattr_search *xs); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 153 | static int ocfs2_xattr_index_block_find(struct inode *inode, |
| 154 | struct buffer_head *root_bh, |
| 155 | int name_index, |
| 156 | const char *name, |
| 157 | struct ocfs2_xattr_search *xs); |
| 158 | |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 159 | static int ocfs2_xattr_tree_list_index_block(struct inode *inode, |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame^] | 160 | struct buffer_head *blk_bh, |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 161 | char *buffer, |
| 162 | size_t buffer_size); |
| 163 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 164 | static int ocfs2_xattr_create_index_block(struct inode *inode, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 165 | struct ocfs2_xattr_search *xs, |
| 166 | struct ocfs2_xattr_set_ctxt *ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 167 | |
| 168 | static int ocfs2_xattr_set_entry_index_block(struct inode *inode, |
| 169 | struct ocfs2_xattr_info *xi, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 170 | struct ocfs2_xattr_search *xs, |
| 171 | struct ocfs2_xattr_set_ctxt *ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 172 | |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame^] | 173 | typedef int (xattr_tree_rec_func)(struct inode *inode, |
| 174 | struct buffer_head *root_bh, |
| 175 | u64 blkno, u32 cpos, u32 len, void *para); |
| 176 | static int ocfs2_iterate_xattr_index_block(struct inode *inode, |
| 177 | struct buffer_head *root_bh, |
| 178 | xattr_tree_rec_func *rec_func, |
| 179 | void *para); |
| 180 | static int ocfs2_delete_xattr_in_bucket(struct inode *inode, |
| 181 | struct ocfs2_xattr_bucket *bucket, |
| 182 | void *para); |
| 183 | static int ocfs2_rm_xattr_cluster(struct inode *inode, |
| 184 | struct buffer_head *root_bh, |
| 185 | u64 blkno, |
| 186 | u32 cpos, |
| 187 | u32 len, |
| 188 | void *para); |
| 189 | |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 190 | static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle, |
| 191 | u64 src_blk, u64 last_blk, u64 to_blk, |
| 192 | unsigned int start_bucket, |
| 193 | u32 *first_hash); |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 194 | static int ocfs2_prepare_refcount_xattr(struct inode *inode, |
| 195 | struct ocfs2_dinode *di, |
| 196 | struct ocfs2_xattr_info *xi, |
| 197 | struct ocfs2_xattr_search *xis, |
| 198 | struct ocfs2_xattr_search *xbs, |
| 199 | struct ocfs2_refcount_tree **ref_tree, |
| 200 | int *meta_need, |
| 201 | int *credits); |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 202 | |
Tiger Yang | 0030e00 | 2008-10-23 16:33:33 +0800 | [diff] [blame] | 203 | static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb) |
| 204 | { |
| 205 | return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE; |
| 206 | } |
| 207 | |
| 208 | static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb) |
| 209 | { |
| 210 | return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits); |
| 211 | } |
| 212 | |
| 213 | static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb) |
| 214 | { |
| 215 | u16 len = sb->s_blocksize - |
| 216 | offsetof(struct ocfs2_xattr_header, xh_entries); |
| 217 | |
| 218 | return len / sizeof(struct ocfs2_xattr_entry); |
| 219 | } |
| 220 | |
Joel Becker | 9c7759a | 2008-10-24 16:21:03 -0700 | [diff] [blame] | 221 | #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr) |
Joel Becker | 51def39 | 2008-10-24 16:57:21 -0700 | [diff] [blame] | 222 | #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data) |
Joel Becker | 3e63294 | 2008-10-24 17:04:49 -0700 | [diff] [blame] | 223 | #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0)) |
Joel Becker | 9c7759a | 2008-10-24 16:21:03 -0700 | [diff] [blame] | 224 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 225 | static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode) |
Joel Becker | 6dde41d | 2008-10-24 17:16:48 -0700 | [diff] [blame] | 226 | { |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 227 | struct ocfs2_xattr_bucket *bucket; |
| 228 | int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Joel Becker | 6dde41d | 2008-10-24 17:16:48 -0700 | [diff] [blame] | 229 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 230 | BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET); |
| 231 | |
| 232 | bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS); |
| 233 | if (bucket) { |
| 234 | bucket->bu_inode = inode; |
| 235 | bucket->bu_blocks = blks; |
| 236 | } |
| 237 | |
| 238 | return bucket; |
| 239 | } |
| 240 | |
| 241 | static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket) |
| 242 | { |
| 243 | int i; |
| 244 | |
| 245 | for (i = 0; i < bucket->bu_blocks; i++) { |
Joel Becker | 6dde41d | 2008-10-24 17:16:48 -0700 | [diff] [blame] | 246 | brelse(bucket->bu_bhs[i]); |
| 247 | bucket->bu_bhs[i] = NULL; |
| 248 | } |
| 249 | } |
| 250 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 251 | static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket) |
| 252 | { |
| 253 | if (bucket) { |
| 254 | ocfs2_xattr_bucket_relse(bucket); |
| 255 | bucket->bu_inode = NULL; |
| 256 | kfree(bucket); |
| 257 | } |
| 258 | } |
| 259 | |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 260 | /* |
| 261 | * A bucket that has never been written to disk doesn't need to be |
| 262 | * read. We just need the buffer_heads. Don't call this for |
| 263 | * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes |
| 264 | * them fully. |
| 265 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 266 | static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket, |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 267 | u64 xb_blkno) |
| 268 | { |
| 269 | int i, rc = 0; |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 270 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 271 | for (i = 0; i < bucket->bu_blocks; i++) { |
| 272 | bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb, |
| 273 | xb_blkno + i); |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 274 | if (!bucket->bu_bhs[i]) { |
| 275 | rc = -EIO; |
| 276 | mlog_errno(rc); |
| 277 | break; |
| 278 | } |
| 279 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 280 | if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode), |
Tao Ma | 757055a | 2008-11-06 08:10:48 +0800 | [diff] [blame] | 281 | bucket->bu_bhs[i])) |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 282 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode), |
Tao Ma | 757055a | 2008-11-06 08:10:48 +0800 | [diff] [blame] | 283 | bucket->bu_bhs[i]); |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | if (rc) |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 287 | ocfs2_xattr_bucket_relse(bucket); |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 288 | return rc; |
| 289 | } |
| 290 | |
| 291 | /* Read the xattr bucket at xb_blkno */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 292 | static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket, |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 293 | u64 xb_blkno) |
| 294 | { |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 295 | int rc; |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 296 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 297 | rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno, |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 298 | bucket->bu_blocks, bucket->bu_bhs, 0, |
| 299 | NULL); |
Joel Becker | 4d0e214 | 2008-12-05 11:19:37 -0800 | [diff] [blame] | 300 | if (!rc) { |
Tao Ma | c8b9cf9 | 2009-02-24 17:40:26 -0800 | [diff] [blame] | 301 | spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock); |
Joel Becker | 4d0e214 | 2008-12-05 11:19:37 -0800 | [diff] [blame] | 302 | rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb, |
| 303 | bucket->bu_bhs, |
| 304 | bucket->bu_blocks, |
| 305 | &bucket_xh(bucket)->xh_check); |
Tao Ma | c8b9cf9 | 2009-02-24 17:40:26 -0800 | [diff] [blame] | 306 | spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock); |
Joel Becker | 4d0e214 | 2008-12-05 11:19:37 -0800 | [diff] [blame] | 307 | if (rc) |
| 308 | mlog_errno(rc); |
| 309 | } |
| 310 | |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 311 | if (rc) |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 312 | ocfs2_xattr_bucket_relse(bucket); |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 313 | return rc; |
| 314 | } |
| 315 | |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 316 | static int ocfs2_xattr_bucket_journal_access(handle_t *handle, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 317 | struct ocfs2_xattr_bucket *bucket, |
| 318 | int type) |
| 319 | { |
| 320 | int i, rc = 0; |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 321 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 322 | for (i = 0; i < bucket->bu_blocks; i++) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 323 | rc = ocfs2_journal_access(handle, |
| 324 | INODE_CACHE(bucket->bu_inode), |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 325 | bucket->bu_bhs[i], type); |
| 326 | if (rc) { |
| 327 | mlog_errno(rc); |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | return rc; |
| 333 | } |
| 334 | |
| 335 | static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 336 | struct ocfs2_xattr_bucket *bucket) |
| 337 | { |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 338 | int i; |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 339 | |
Tao Ma | c8b9cf9 | 2009-02-24 17:40:26 -0800 | [diff] [blame] | 340 | spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock); |
Joel Becker | 4d0e214 | 2008-12-05 11:19:37 -0800 | [diff] [blame] | 341 | ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb, |
| 342 | bucket->bu_bhs, bucket->bu_blocks, |
| 343 | &bucket_xh(bucket)->xh_check); |
Tao Ma | c8b9cf9 | 2009-02-24 17:40:26 -0800 | [diff] [blame] | 344 | spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock); |
Joel Becker | 4d0e214 | 2008-12-05 11:19:37 -0800 | [diff] [blame] | 345 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 346 | for (i = 0; i < bucket->bu_blocks; i++) |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 347 | ocfs2_journal_dirty(handle, bucket->bu_bhs[i]); |
| 348 | } |
| 349 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 350 | static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest, |
Joel Becker | 4980c6d | 2008-10-24 18:54:43 -0700 | [diff] [blame] | 351 | struct ocfs2_xattr_bucket *src) |
| 352 | { |
| 353 | int i; |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 354 | int blocksize = src->bu_inode->i_sb->s_blocksize; |
Joel Becker | 4980c6d | 2008-10-24 18:54:43 -0700 | [diff] [blame] | 355 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 356 | BUG_ON(dest->bu_blocks != src->bu_blocks); |
| 357 | BUG_ON(dest->bu_inode != src->bu_inode); |
| 358 | |
| 359 | for (i = 0; i < src->bu_blocks; i++) { |
Joel Becker | 4980c6d | 2008-10-24 18:54:43 -0700 | [diff] [blame] | 360 | memcpy(bucket_block(dest, i), bucket_block(src, i), |
| 361 | blocksize); |
| 362 | } |
| 363 | } |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 364 | |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 365 | static int ocfs2_validate_xattr_block(struct super_block *sb, |
| 366 | struct buffer_head *bh) |
| 367 | { |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 368 | int rc; |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 369 | struct ocfs2_xattr_block *xb = |
| 370 | (struct ocfs2_xattr_block *)bh->b_data; |
| 371 | |
| 372 | mlog(0, "Validating xattr block %llu\n", |
| 373 | (unsigned long long)bh->b_blocknr); |
| 374 | |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 375 | BUG_ON(!buffer_uptodate(bh)); |
| 376 | |
| 377 | /* |
| 378 | * If the ecc fails, we return the error but otherwise |
| 379 | * leave the filesystem running. We know any error is |
| 380 | * local to this block. |
| 381 | */ |
| 382 | rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check); |
| 383 | if (rc) |
| 384 | return rc; |
| 385 | |
| 386 | /* |
| 387 | * Errors after here are fatal |
| 388 | */ |
| 389 | |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 390 | if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) { |
| 391 | ocfs2_error(sb, |
| 392 | "Extended attribute block #%llu has bad " |
| 393 | "signature %.*s", |
| 394 | (unsigned long long)bh->b_blocknr, 7, |
| 395 | xb->xb_signature); |
| 396 | return -EINVAL; |
| 397 | } |
| 398 | |
| 399 | if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) { |
| 400 | ocfs2_error(sb, |
| 401 | "Extended attribute block #%llu has an " |
| 402 | "invalid xb_blkno of %llu", |
| 403 | (unsigned long long)bh->b_blocknr, |
| 404 | (unsigned long long)le64_to_cpu(xb->xb_blkno)); |
| 405 | return -EINVAL; |
| 406 | } |
| 407 | |
| 408 | if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) { |
| 409 | ocfs2_error(sb, |
| 410 | "Extended attribute block #%llu has an invalid " |
| 411 | "xb_fs_generation of #%u", |
| 412 | (unsigned long long)bh->b_blocknr, |
| 413 | le32_to_cpu(xb->xb_fs_generation)); |
| 414 | return -EINVAL; |
| 415 | } |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno, |
| 421 | struct buffer_head **bh) |
| 422 | { |
| 423 | int rc; |
| 424 | struct buffer_head *tmp = *bh; |
| 425 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 426 | rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp, |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 427 | ocfs2_validate_xattr_block); |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 428 | |
| 429 | /* If ocfs2_read_block() got us a new bh, pass it up. */ |
| 430 | if (!rc && !*bh) |
| 431 | *bh = tmp; |
| 432 | |
| 433 | return rc; |
| 434 | } |
| 435 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 436 | static inline const char *ocfs2_xattr_prefix(int name_index) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 437 | { |
| 438 | struct xattr_handler *handler = NULL; |
| 439 | |
| 440 | if (name_index > 0 && name_index < OCFS2_XATTR_MAX) |
| 441 | handler = ocfs2_xattr_handler_map[name_index]; |
| 442 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 443 | return handler ? handler->prefix : NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 444 | } |
| 445 | |
Mark Fasheh | 40daa16 | 2008-10-07 14:31:42 -0700 | [diff] [blame] | 446 | static u32 ocfs2_xattr_name_hash(struct inode *inode, |
Tao Ma | 2057e5c | 2008-10-09 23:06:13 +0800 | [diff] [blame] | 447 | const char *name, |
Mark Fasheh | 40daa16 | 2008-10-07 14:31:42 -0700 | [diff] [blame] | 448 | int name_len) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 449 | { |
| 450 | /* Get hash value of uuid from super block */ |
| 451 | u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash; |
| 452 | int i; |
| 453 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 454 | /* hash extended attribute name */ |
| 455 | for (i = 0; i < name_len; i++) { |
| 456 | hash = (hash << OCFS2_HASH_SHIFT) ^ |
| 457 | (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^ |
| 458 | *name++; |
| 459 | } |
| 460 | |
| 461 | return hash; |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * ocfs2_xattr_hash_entry() |
| 466 | * |
| 467 | * Compute the hash of an extended attribute. |
| 468 | */ |
| 469 | static void ocfs2_xattr_hash_entry(struct inode *inode, |
| 470 | struct ocfs2_xattr_header *header, |
| 471 | struct ocfs2_xattr_entry *entry) |
| 472 | { |
| 473 | u32 hash = 0; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 474 | char *name = (char *)header + le16_to_cpu(entry->xe_name_offset); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 475 | |
Tao Ma | 2057e5c | 2008-10-09 23:06:13 +0800 | [diff] [blame] | 476 | hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 477 | entry->xe_name_hash = cpu_to_le32(hash); |
| 478 | |
| 479 | return; |
| 480 | } |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 481 | |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 482 | static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len) |
| 483 | { |
| 484 | int size = 0; |
| 485 | |
| 486 | if (value_len <= OCFS2_XATTR_INLINE_SIZE) |
| 487 | size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len); |
| 488 | else |
| 489 | size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE; |
| 490 | size += sizeof(struct ocfs2_xattr_entry); |
| 491 | |
| 492 | return size; |
| 493 | } |
| 494 | |
| 495 | int ocfs2_calc_security_init(struct inode *dir, |
| 496 | struct ocfs2_security_xattr_info *si, |
| 497 | int *want_clusters, |
| 498 | int *xattr_credits, |
| 499 | struct ocfs2_alloc_context **xattr_ac) |
| 500 | { |
| 501 | int ret = 0; |
| 502 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
| 503 | int s_size = ocfs2_xattr_entry_real_size(strlen(si->name), |
| 504 | si->value_len); |
| 505 | |
| 506 | /* |
| 507 | * The max space of security xattr taken inline is |
| 508 | * 256(name) + 80(value) + 16(entry) = 352 bytes, |
| 509 | * So reserve one metadata block for it is ok. |
| 510 | */ |
| 511 | if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE || |
| 512 | s_size > OCFS2_XATTR_FREE_IN_IBODY) { |
| 513 | ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac); |
| 514 | if (ret) { |
| 515 | mlog_errno(ret); |
| 516 | return ret; |
| 517 | } |
| 518 | *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS; |
| 519 | } |
| 520 | |
| 521 | /* reserve clusters for xattr value which will be set in B tree*/ |
Tiger Yang | 0e445b6 | 2008-12-09 16:42:51 +0800 | [diff] [blame] | 522 | if (si->value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 523 | int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb, |
| 524 | si->value_len); |
| 525 | |
| 526 | *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb, |
| 527 | new_clusters); |
| 528 | *want_clusters += new_clusters; |
| 529 | } |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 530 | return ret; |
| 531 | } |
| 532 | |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 533 | int ocfs2_calc_xattr_init(struct inode *dir, |
| 534 | struct buffer_head *dir_bh, |
| 535 | int mode, |
| 536 | struct ocfs2_security_xattr_info *si, |
| 537 | int *want_clusters, |
| 538 | int *xattr_credits, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 539 | int *want_meta) |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 540 | { |
| 541 | int ret = 0; |
| 542 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
Tiger Yang | 0e445b6 | 2008-12-09 16:42:51 +0800 | [diff] [blame] | 543 | int s_size = 0, a_size = 0, acl_len = 0, new_clusters; |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 544 | |
| 545 | if (si->enable) |
| 546 | s_size = ocfs2_xattr_entry_real_size(strlen(si->name), |
| 547 | si->value_len); |
| 548 | |
| 549 | if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) { |
| 550 | acl_len = ocfs2_xattr_get_nolock(dir, dir_bh, |
| 551 | OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT, |
| 552 | "", NULL, 0); |
| 553 | if (acl_len > 0) { |
| 554 | a_size = ocfs2_xattr_entry_real_size(0, acl_len); |
| 555 | if (S_ISDIR(mode)) |
| 556 | a_size <<= 1; |
| 557 | } else if (acl_len != 0 && acl_len != -ENODATA) { |
| 558 | mlog_errno(ret); |
| 559 | return ret; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | if (!(s_size + a_size)) |
| 564 | return ret; |
| 565 | |
| 566 | /* |
| 567 | * The max space of security xattr taken inline is |
| 568 | * 256(name) + 80(value) + 16(entry) = 352 bytes, |
| 569 | * The max space of acl xattr taken inline is |
| 570 | * 80(value) + 16(entry) * 2(if directory) = 192 bytes, |
| 571 | * when blocksize = 512, may reserve one more cluser for |
| 572 | * xattr bucket, otherwise reserve one metadata block |
| 573 | * for them is ok. |
Tiger Yang | 6c9fd1d | 2009-03-06 10:19:30 +0800 | [diff] [blame] | 574 | * If this is a new directory with inline data, |
| 575 | * we choose to reserve the entire inline area for |
| 576 | * directory contents and force an external xattr block. |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 577 | */ |
| 578 | if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE || |
Tiger Yang | 6c9fd1d | 2009-03-06 10:19:30 +0800 | [diff] [blame] | 579 | (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) || |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 580 | (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) { |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 581 | *want_meta = *want_meta + 1; |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 582 | *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS; |
| 583 | } |
| 584 | |
| 585 | if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE && |
| 586 | (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) { |
| 587 | *want_clusters += 1; |
| 588 | *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb); |
| 589 | } |
| 590 | |
Tiger Yang | 0e445b6 | 2008-12-09 16:42:51 +0800 | [diff] [blame] | 591 | /* |
| 592 | * reserve credits and clusters for xattrs which has large value |
| 593 | * and have to be set outside |
| 594 | */ |
| 595 | if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 596 | new_clusters = ocfs2_clusters_for_bytes(dir->i_sb, |
| 597 | si->value_len); |
| 598 | *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb, |
| 599 | new_clusters); |
| 600 | *want_clusters += new_clusters; |
| 601 | } |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 602 | if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL && |
| 603 | acl_len > OCFS2_XATTR_INLINE_SIZE) { |
Tiger Yang | 0e445b6 | 2008-12-09 16:42:51 +0800 | [diff] [blame] | 604 | /* for directory, it has DEFAULT and ACCESS two types of acls */ |
| 605 | new_clusters = (S_ISDIR(mode) ? 2 : 1) * |
| 606 | ocfs2_clusters_for_bytes(dir->i_sb, acl_len); |
| 607 | *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb, |
| 608 | new_clusters); |
| 609 | *want_clusters += new_clusters; |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | return ret; |
| 613 | } |
| 614 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 615 | static int ocfs2_xattr_extend_allocation(struct inode *inode, |
| 616 | u32 clusters_to_add, |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 617 | struct ocfs2_xattr_value_buf *vb, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 618 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 619 | { |
| 620 | int status = 0; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 621 | handle_t *handle = ctxt->handle; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 622 | enum ocfs2_alloc_restarted why; |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 623 | 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] | 624 | struct ocfs2_extent_tree et; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 625 | |
| 626 | mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add); |
| 627 | |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 628 | ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 629 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 630 | status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh, |
Joel Becker | 2a50a74 | 2008-12-09 14:24:33 -0800 | [diff] [blame] | 631 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 632 | if (status < 0) { |
| 633 | mlog_errno(status); |
| 634 | goto leave; |
| 635 | } |
| 636 | |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 637 | prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters); |
Joel Becker | cbee7e1 | 2009-02-13 03:34:15 -0800 | [diff] [blame] | 638 | status = ocfs2_add_clusters_in_btree(handle, |
| 639 | &et, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 640 | &logical_start, |
| 641 | clusters_to_add, |
| 642 | 0, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 643 | ctxt->data_ac, |
| 644 | ctxt->meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 645 | &why); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 646 | if (status < 0) { |
| 647 | mlog_errno(status); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 648 | goto leave; |
| 649 | } |
| 650 | |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 651 | status = ocfs2_journal_dirty(handle, vb->vb_bh); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 652 | if (status < 0) { |
| 653 | mlog_errno(status); |
| 654 | goto leave; |
| 655 | } |
| 656 | |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 657 | 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] | 658 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 659 | /* |
| 660 | * We should have already allocated enough space before the transaction, |
| 661 | * so no need to restart. |
| 662 | */ |
| 663 | BUG_ON(why != RESTART_NONE || clusters_to_add); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 664 | |
| 665 | leave: |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 666 | |
| 667 | return status; |
| 668 | } |
| 669 | |
| 670 | static int __ocfs2_remove_xattr_range(struct inode *inode, |
Joel Becker | d72cc72 | 2008-12-09 14:30:41 -0800 | [diff] [blame] | 671 | struct ocfs2_xattr_value_buf *vb, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 672 | u32 cpos, u32 phys_cpos, u32 len, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 673 | unsigned int ext_flags, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 674 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 675 | { |
| 676 | int ret; |
| 677 | u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 678 | handle_t *handle = ctxt->handle; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 679 | struct ocfs2_extent_tree et; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 680 | |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 681 | ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 682 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 683 | ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh, |
Joel Becker | d72cc72 | 2008-12-09 14:30:41 -0800 | [diff] [blame] | 684 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 685 | if (ret) { |
| 686 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 687 | goto out; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 688 | } |
| 689 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 690 | ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 691 | &ctxt->dealloc); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 692 | if (ret) { |
| 693 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 694 | goto out; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 695 | } |
| 696 | |
Joel Becker | d72cc72 | 2008-12-09 14:30:41 -0800 | [diff] [blame] | 697 | le32_add_cpu(&vb->vb_xv->xr_clusters, -len); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 698 | |
Joel Becker | d72cc72 | 2008-12-09 14:30:41 -0800 | [diff] [blame] | 699 | ret = ocfs2_journal_dirty(handle, vb->vb_bh); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 700 | if (ret) { |
| 701 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 702 | goto out; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 703 | } |
| 704 | |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 705 | if (ext_flags & OCFS2_EXT_REFCOUNTED) |
| 706 | ret = ocfs2_decrease_refcount(inode, handle, |
| 707 | ocfs2_blocks_to_clusters(inode->i_sb, |
| 708 | phys_blkno), |
| 709 | len, ctxt->meta_ac, &ctxt->dealloc, 1); |
| 710 | else |
| 711 | ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc, |
| 712 | phys_blkno, len); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 713 | if (ret) |
| 714 | mlog_errno(ret); |
| 715 | |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 716 | out: |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 717 | return ret; |
| 718 | } |
| 719 | |
| 720 | static int ocfs2_xattr_shrink_size(struct inode *inode, |
| 721 | u32 old_clusters, |
| 722 | u32 new_clusters, |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 723 | struct ocfs2_xattr_value_buf *vb, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 724 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 725 | { |
| 726 | int ret = 0; |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 727 | unsigned int ext_flags; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 728 | u32 trunc_len, cpos, phys_cpos, alloc_size; |
| 729 | u64 block; |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 730 | |
| 731 | if (old_clusters <= new_clusters) |
| 732 | return 0; |
| 733 | |
| 734 | cpos = new_clusters; |
| 735 | trunc_len = old_clusters - new_clusters; |
| 736 | while (trunc_len) { |
| 737 | ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos, |
Joel Becker | d72cc72 | 2008-12-09 14:30:41 -0800 | [diff] [blame] | 738 | &alloc_size, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 739 | &vb->vb_xv->xr_list, &ext_flags); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 740 | if (ret) { |
| 741 | mlog_errno(ret); |
| 742 | goto out; |
| 743 | } |
| 744 | |
| 745 | if (alloc_size > trunc_len) |
| 746 | alloc_size = trunc_len; |
| 747 | |
Joel Becker | 19b801f | 2008-12-09 14:36:50 -0800 | [diff] [blame] | 748 | ret = __ocfs2_remove_xattr_range(inode, vb, cpos, |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 749 | phys_cpos, alloc_size, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 750 | ext_flags, ctxt); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 751 | if (ret) { |
| 752 | mlog_errno(ret); |
| 753 | goto out; |
| 754 | } |
| 755 | |
| 756 | block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 757 | ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), |
| 758 | block, alloc_size); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 759 | cpos += alloc_size; |
| 760 | trunc_len -= alloc_size; |
| 761 | } |
| 762 | |
| 763 | out: |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | static int ocfs2_xattr_value_truncate(struct inode *inode, |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 768 | struct ocfs2_xattr_value_buf *vb, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 769 | int len, |
| 770 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 771 | { |
| 772 | int ret; |
| 773 | u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len); |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 774 | u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 775 | |
| 776 | if (new_clusters == old_clusters) |
| 777 | return 0; |
| 778 | |
| 779 | if (new_clusters > old_clusters) |
| 780 | ret = ocfs2_xattr_extend_allocation(inode, |
| 781 | new_clusters - old_clusters, |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 782 | vb, ctxt); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 783 | else |
| 784 | ret = ocfs2_xattr_shrink_size(inode, |
| 785 | old_clusters, new_clusters, |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 786 | vb, ctxt); |
Tao Ma | f56654c | 2008-08-18 17:38:48 +0800 | [diff] [blame] | 787 | |
| 788 | return ret; |
| 789 | } |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 790 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 791 | static int ocfs2_xattr_list_entry(char *buffer, size_t size, |
| 792 | size_t *result, const char *prefix, |
| 793 | const char *name, int name_len) |
| 794 | { |
| 795 | char *p = buffer + *result; |
| 796 | int prefix_len = strlen(prefix); |
| 797 | int total_len = prefix_len + name_len + 1; |
| 798 | |
| 799 | *result += total_len; |
| 800 | |
| 801 | /* we are just looking for how big our buffer needs to be */ |
| 802 | if (!size) |
| 803 | return 0; |
| 804 | |
| 805 | if (*result > size) |
| 806 | return -ERANGE; |
| 807 | |
| 808 | memcpy(p, prefix, prefix_len); |
| 809 | memcpy(p + prefix_len, name, name_len); |
| 810 | p[prefix_len + name_len] = '\0'; |
| 811 | |
| 812 | return 0; |
| 813 | } |
| 814 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 815 | static int ocfs2_xattr_list_entries(struct inode *inode, |
| 816 | struct ocfs2_xattr_header *header, |
| 817 | char *buffer, size_t buffer_size) |
| 818 | { |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 819 | size_t result = 0; |
| 820 | int i, type, ret; |
| 821 | const char *prefix, *name; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 822 | |
| 823 | for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) { |
| 824 | struct ocfs2_xattr_entry *entry = &header->xh_entries[i]; |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 825 | type = ocfs2_xattr_get_type(entry); |
| 826 | prefix = ocfs2_xattr_prefix(type); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 827 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 828 | if (prefix) { |
| 829 | name = (const char *)header + |
| 830 | le16_to_cpu(entry->xe_name_offset); |
| 831 | |
| 832 | ret = ocfs2_xattr_list_entry(buffer, buffer_size, |
| 833 | &result, prefix, name, |
| 834 | entry->xe_name_len); |
| 835 | if (ret) |
| 836 | return ret; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 840 | return result; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | static int ocfs2_xattr_ibody_list(struct inode *inode, |
| 844 | struct ocfs2_dinode *di, |
| 845 | char *buffer, |
| 846 | size_t buffer_size) |
| 847 | { |
| 848 | struct ocfs2_xattr_header *header = NULL; |
| 849 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 850 | int ret = 0; |
| 851 | |
| 852 | if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) |
| 853 | return ret; |
| 854 | |
| 855 | header = (struct ocfs2_xattr_header *) |
| 856 | ((void *)di + inode->i_sb->s_blocksize - |
| 857 | le16_to_cpu(di->i_xattr_inline_size)); |
| 858 | |
| 859 | ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size); |
| 860 | |
| 861 | return ret; |
| 862 | } |
| 863 | |
| 864 | static int ocfs2_xattr_block_list(struct inode *inode, |
| 865 | struct ocfs2_dinode *di, |
| 866 | char *buffer, |
| 867 | size_t buffer_size) |
| 868 | { |
| 869 | struct buffer_head *blk_bh = NULL; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 870 | struct ocfs2_xattr_block *xb; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 871 | int ret = 0; |
| 872 | |
| 873 | if (!di->i_xattr_loc) |
| 874 | return ret; |
| 875 | |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 876 | ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc), |
| 877 | &blk_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 878 | if (ret < 0) { |
| 879 | mlog_errno(ret); |
| 880 | return ret; |
| 881 | } |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 882 | |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 883 | xb = (struct ocfs2_xattr_block *)blk_bh->b_data; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 884 | if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { |
| 885 | struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header; |
| 886 | ret = ocfs2_xattr_list_entries(inode, header, |
| 887 | buffer, buffer_size); |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame^] | 888 | } else |
| 889 | ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh, |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 890 | buffer, buffer_size); |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 891 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 892 | brelse(blk_bh); |
| 893 | |
| 894 | return ret; |
| 895 | } |
| 896 | |
| 897 | ssize_t ocfs2_listxattr(struct dentry *dentry, |
| 898 | char *buffer, |
| 899 | size_t size) |
| 900 | { |
| 901 | int ret = 0, i_ret = 0, b_ret = 0; |
| 902 | struct buffer_head *di_bh = NULL; |
| 903 | struct ocfs2_dinode *di = NULL; |
| 904 | struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode); |
| 905 | |
Tiger Yang | 8154da3 | 2008-08-18 17:11:46 +0800 | [diff] [blame] | 906 | if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb))) |
| 907 | return -EOPNOTSUPP; |
| 908 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 909 | if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) |
| 910 | return ret; |
| 911 | |
| 912 | ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0); |
| 913 | if (ret < 0) { |
| 914 | mlog_errno(ret); |
| 915 | return ret; |
| 916 | } |
| 917 | |
| 918 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 919 | |
| 920 | down_read(&oi->ip_xattr_sem); |
| 921 | i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size); |
| 922 | if (i_ret < 0) |
| 923 | b_ret = 0; |
| 924 | else { |
| 925 | if (buffer) { |
| 926 | buffer += i_ret; |
| 927 | size -= i_ret; |
| 928 | } |
| 929 | b_ret = ocfs2_xattr_block_list(dentry->d_inode, di, |
| 930 | buffer, size); |
| 931 | if (b_ret < 0) |
| 932 | i_ret = 0; |
| 933 | } |
| 934 | up_read(&oi->ip_xattr_sem); |
| 935 | ocfs2_inode_unlock(dentry->d_inode, 0); |
| 936 | |
| 937 | brelse(di_bh); |
| 938 | |
| 939 | return i_ret + b_ret; |
| 940 | } |
| 941 | |
| 942 | static int ocfs2_xattr_find_entry(int name_index, |
| 943 | const char *name, |
| 944 | struct ocfs2_xattr_search *xs) |
| 945 | { |
| 946 | struct ocfs2_xattr_entry *entry; |
| 947 | size_t name_len; |
| 948 | int i, cmp = 1; |
| 949 | |
| 950 | if (name == NULL) |
| 951 | return -EINVAL; |
| 952 | |
| 953 | name_len = strlen(name); |
| 954 | entry = xs->here; |
| 955 | for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) { |
| 956 | cmp = name_index - ocfs2_xattr_get_type(entry); |
| 957 | if (!cmp) |
| 958 | cmp = name_len - entry->xe_name_len; |
| 959 | if (!cmp) |
| 960 | cmp = memcmp(name, (xs->base + |
| 961 | le16_to_cpu(entry->xe_name_offset)), |
| 962 | name_len); |
| 963 | if (cmp == 0) |
| 964 | break; |
| 965 | entry += 1; |
| 966 | } |
| 967 | xs->here = entry; |
| 968 | |
| 969 | return cmp ? -ENODATA : 0; |
| 970 | } |
| 971 | |
| 972 | static int ocfs2_xattr_get_value_outside(struct inode *inode, |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 973 | struct ocfs2_xattr_value_root *xv, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 974 | void *buffer, |
| 975 | size_t len) |
| 976 | { |
| 977 | u32 cpos, p_cluster, num_clusters, bpc, clusters; |
| 978 | u64 blkno; |
| 979 | int i, ret = 0; |
| 980 | size_t cplen, blocksize; |
| 981 | struct buffer_head *bh = NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 982 | struct ocfs2_extent_list *el; |
| 983 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 984 | el = &xv->xr_list; |
| 985 | clusters = le32_to_cpu(xv->xr_clusters); |
| 986 | bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); |
| 987 | blocksize = inode->i_sb->s_blocksize; |
| 988 | |
| 989 | cpos = 0; |
| 990 | while (cpos < clusters) { |
| 991 | ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster, |
Tao Ma | 1061f9c | 2009-08-18 11:41:57 +0800 | [diff] [blame] | 992 | &num_clusters, el, NULL); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 993 | if (ret) { |
| 994 | mlog_errno(ret); |
| 995 | goto out; |
| 996 | } |
| 997 | |
| 998 | blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster); |
| 999 | /* Copy ocfs2_xattr_value */ |
| 1000 | for (i = 0; i < num_clusters * bpc; i++, blkno++) { |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 1001 | ret = ocfs2_read_block(INODE_CACHE(inode), blkno, |
| 1002 | &bh, NULL); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1003 | if (ret) { |
| 1004 | mlog_errno(ret); |
| 1005 | goto out; |
| 1006 | } |
| 1007 | |
| 1008 | cplen = len >= blocksize ? blocksize : len; |
| 1009 | memcpy(buffer, bh->b_data, cplen); |
| 1010 | len -= cplen; |
| 1011 | buffer += cplen; |
| 1012 | |
| 1013 | brelse(bh); |
| 1014 | bh = NULL; |
| 1015 | if (len == 0) |
| 1016 | break; |
| 1017 | } |
| 1018 | cpos += num_clusters; |
| 1019 | } |
| 1020 | out: |
| 1021 | return ret; |
| 1022 | } |
| 1023 | |
| 1024 | static int ocfs2_xattr_ibody_get(struct inode *inode, |
| 1025 | int name_index, |
| 1026 | const char *name, |
| 1027 | void *buffer, |
| 1028 | size_t buffer_size, |
| 1029 | struct ocfs2_xattr_search *xs) |
| 1030 | { |
| 1031 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 1032 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1033 | struct ocfs2_xattr_value_root *xv; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1034 | size_t size; |
| 1035 | int ret = 0; |
| 1036 | |
| 1037 | if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) |
| 1038 | return -ENODATA; |
| 1039 | |
| 1040 | xs->end = (void *)di + inode->i_sb->s_blocksize; |
| 1041 | xs->header = (struct ocfs2_xattr_header *) |
| 1042 | (xs->end - le16_to_cpu(di->i_xattr_inline_size)); |
| 1043 | xs->base = (void *)xs->header; |
| 1044 | xs->here = xs->header->xh_entries; |
| 1045 | |
| 1046 | ret = ocfs2_xattr_find_entry(name_index, name, xs); |
| 1047 | if (ret) |
| 1048 | return ret; |
| 1049 | size = le64_to_cpu(xs->here->xe_value_size); |
| 1050 | if (buffer) { |
| 1051 | if (size > buffer_size) |
| 1052 | return -ERANGE; |
| 1053 | if (ocfs2_xattr_is_local(xs->here)) { |
| 1054 | memcpy(buffer, (void *)xs->base + |
| 1055 | le16_to_cpu(xs->here->xe_name_offset) + |
| 1056 | OCFS2_XATTR_SIZE(xs->here->xe_name_len), size); |
| 1057 | } else { |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1058 | xv = (struct ocfs2_xattr_value_root *) |
| 1059 | (xs->base + le16_to_cpu( |
| 1060 | xs->here->xe_name_offset) + |
| 1061 | OCFS2_XATTR_SIZE(xs->here->xe_name_len)); |
| 1062 | ret = ocfs2_xattr_get_value_outside(inode, xv, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1063 | buffer, size); |
| 1064 | if (ret < 0) { |
| 1065 | mlog_errno(ret); |
| 1066 | return ret; |
| 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | return size; |
| 1072 | } |
| 1073 | |
| 1074 | static int ocfs2_xattr_block_get(struct inode *inode, |
| 1075 | int name_index, |
| 1076 | const char *name, |
| 1077 | void *buffer, |
| 1078 | size_t buffer_size, |
| 1079 | struct ocfs2_xattr_search *xs) |
| 1080 | { |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1081 | struct ocfs2_xattr_block *xb; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1082 | struct ocfs2_xattr_value_root *xv; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1083 | size_t size; |
Subrata Modak | 44d8e4e | 2009-07-14 01:19:31 +0530 | [diff] [blame] | 1084 | int ret = -ENODATA, name_offset, name_len, i; |
| 1085 | int uninitialized_var(block_off); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1086 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 1087 | xs->bucket = ocfs2_xattr_bucket_new(inode); |
| 1088 | if (!xs->bucket) { |
| 1089 | ret = -ENOMEM; |
| 1090 | mlog_errno(ret); |
| 1091 | goto cleanup; |
| 1092 | } |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1093 | |
Joel Becker | 54f443f | 2008-10-20 18:43:07 -0700 | [diff] [blame] | 1094 | ret = ocfs2_xattr_block_find(inode, name_index, name, xs); |
| 1095 | if (ret) { |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1096 | mlog_errno(ret); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1097 | goto cleanup; |
| 1098 | } |
| 1099 | |
Tiger Yang | 6c1e183 | 2008-11-02 19:04:21 +0800 | [diff] [blame] | 1100 | if (xs->not_found) { |
| 1101 | ret = -ENODATA; |
| 1102 | goto cleanup; |
| 1103 | } |
| 1104 | |
Joel Becker | 54f443f | 2008-10-20 18:43:07 -0700 | [diff] [blame] | 1105 | xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1106 | size = le64_to_cpu(xs->here->xe_value_size); |
| 1107 | if (buffer) { |
| 1108 | ret = -ERANGE; |
| 1109 | if (size > buffer_size) |
| 1110 | goto cleanup; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1111 | |
| 1112 | name_offset = le16_to_cpu(xs->here->xe_name_offset); |
| 1113 | name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len); |
| 1114 | i = xs->here - xs->header->xh_entries; |
| 1115 | |
| 1116 | if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) { |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 1117 | ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 1118 | bucket_xh(xs->bucket), |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1119 | i, |
| 1120 | &block_off, |
| 1121 | &name_offset); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 1122 | xs->base = bucket_block(xs->bucket, block_off); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1123 | } |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1124 | if (ocfs2_xattr_is_local(xs->here)) { |
| 1125 | memcpy(buffer, (void *)xs->base + |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1126 | name_offset + name_len, size); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1127 | } else { |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 1128 | xv = (struct ocfs2_xattr_value_root *) |
| 1129 | (xs->base + name_offset + name_len); |
| 1130 | ret = ocfs2_xattr_get_value_outside(inode, xv, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1131 | buffer, size); |
| 1132 | if (ret < 0) { |
| 1133 | mlog_errno(ret); |
| 1134 | goto cleanup; |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | ret = size; |
| 1139 | cleanup: |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 1140 | ocfs2_xattr_bucket_free(xs->bucket); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1141 | |
Joel Becker | 54f443f | 2008-10-20 18:43:07 -0700 | [diff] [blame] | 1142 | brelse(xs->xattr_bh); |
| 1143 | xs->xattr_bh = NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1144 | return ret; |
| 1145 | } |
| 1146 | |
Tiger Yang | 4e3e9d0 | 2008-11-14 11:16:53 +0800 | [diff] [blame] | 1147 | int ocfs2_xattr_get_nolock(struct inode *inode, |
| 1148 | struct buffer_head *di_bh, |
Tiger Yang | 0030e00 | 2008-10-23 16:33:33 +0800 | [diff] [blame] | 1149 | int name_index, |
| 1150 | const char *name, |
| 1151 | void *buffer, |
| 1152 | size_t buffer_size) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1153 | { |
| 1154 | int ret; |
| 1155 | struct ocfs2_dinode *di = NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1156 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 1157 | struct ocfs2_xattr_search xis = { |
| 1158 | .not_found = -ENODATA, |
| 1159 | }; |
| 1160 | struct ocfs2_xattr_search xbs = { |
| 1161 | .not_found = -ENODATA, |
| 1162 | }; |
| 1163 | |
Tiger Yang | 8154da3 | 2008-08-18 17:11:46 +0800 | [diff] [blame] | 1164 | if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) |
| 1165 | return -EOPNOTSUPP; |
| 1166 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1167 | if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) |
| 1168 | ret = -ENODATA; |
| 1169 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1170 | xis.inode_bh = xbs.inode_bh = di_bh; |
| 1171 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 1172 | |
| 1173 | down_read(&oi->ip_xattr_sem); |
| 1174 | ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer, |
| 1175 | buffer_size, &xis); |
Tiger Yang | 6c1e183 | 2008-11-02 19:04:21 +0800 | [diff] [blame] | 1176 | if (ret == -ENODATA && di->i_xattr_loc) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1177 | ret = ocfs2_xattr_block_get(inode, name_index, name, buffer, |
| 1178 | buffer_size, &xbs); |
| 1179 | up_read(&oi->ip_xattr_sem); |
Tiger Yang | 4e3e9d0 | 2008-11-14 11:16:53 +0800 | [diff] [blame] | 1180 | |
| 1181 | return ret; |
| 1182 | } |
| 1183 | |
| 1184 | /* ocfs2_xattr_get() |
| 1185 | * |
| 1186 | * Copy an extended attribute into the buffer provided. |
| 1187 | * Buffer is NULL to compute the size of buffer required. |
| 1188 | */ |
| 1189 | static int ocfs2_xattr_get(struct inode *inode, |
| 1190 | int name_index, |
| 1191 | const char *name, |
| 1192 | void *buffer, |
| 1193 | size_t buffer_size) |
| 1194 | { |
| 1195 | int ret; |
| 1196 | struct buffer_head *di_bh = NULL; |
| 1197 | |
| 1198 | ret = ocfs2_inode_lock(inode, &di_bh, 0); |
| 1199 | if (ret < 0) { |
| 1200 | mlog_errno(ret); |
| 1201 | return ret; |
| 1202 | } |
| 1203 | ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index, |
| 1204 | name, buffer, buffer_size); |
| 1205 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1206 | ocfs2_inode_unlock(inode, 0); |
| 1207 | |
| 1208 | brelse(di_bh); |
| 1209 | |
| 1210 | return ret; |
| 1211 | } |
| 1212 | |
| 1213 | static int __ocfs2_xattr_set_value_outside(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1214 | handle_t *handle, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 1215 | struct ocfs2_xattr_value_buf *vb, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1216 | const void *value, |
| 1217 | int value_len) |
| 1218 | { |
Tao Ma | 71d548a | 2008-12-05 06:20:54 +0800 | [diff] [blame] | 1219 | int ret = 0, i, cp_len; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1220 | u16 blocksize = inode->i_sb->s_blocksize; |
| 1221 | u32 p_cluster, num_clusters; |
| 1222 | u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); |
| 1223 | u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len); |
| 1224 | u64 blkno; |
| 1225 | struct buffer_head *bh = NULL; |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 1226 | unsigned int ext_flags; |
| 1227 | struct ocfs2_xattr_value_root *xv = vb->vb_xv; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1228 | |
| 1229 | BUG_ON(clusters > le32_to_cpu(xv->xr_clusters)); |
| 1230 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1231 | while (cpos < clusters) { |
| 1232 | ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster, |
Tao Ma | 1061f9c | 2009-08-18 11:41:57 +0800 | [diff] [blame] | 1233 | &num_clusters, &xv->xr_list, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 1234 | &ext_flags); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1235 | if (ret) { |
| 1236 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1237 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1238 | } |
| 1239 | |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 1240 | BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED); |
| 1241 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1242 | blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster); |
| 1243 | |
| 1244 | for (i = 0; i < num_clusters * bpc; i++, blkno++) { |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 1245 | ret = ocfs2_read_block(INODE_CACHE(inode), blkno, |
| 1246 | &bh, NULL); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1247 | if (ret) { |
| 1248 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1249 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | ret = ocfs2_journal_access(handle, |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1253 | INODE_CACHE(inode), |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1254 | bh, |
| 1255 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1256 | if (ret < 0) { |
| 1257 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1258 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | cp_len = value_len > blocksize ? blocksize : value_len; |
| 1262 | memcpy(bh->b_data, value, cp_len); |
| 1263 | value_len -= cp_len; |
| 1264 | value += cp_len; |
| 1265 | if (cp_len < blocksize) |
| 1266 | memset(bh->b_data + cp_len, 0, |
| 1267 | blocksize - cp_len); |
| 1268 | |
| 1269 | ret = ocfs2_journal_dirty(handle, bh); |
| 1270 | if (ret < 0) { |
| 1271 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1272 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1273 | } |
| 1274 | brelse(bh); |
| 1275 | bh = NULL; |
| 1276 | |
| 1277 | /* |
| 1278 | * XXX: do we need to empty all the following |
| 1279 | * blocks in this cluster? |
| 1280 | */ |
| 1281 | if (!value_len) |
| 1282 | break; |
| 1283 | } |
| 1284 | cpos += num_clusters; |
| 1285 | } |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1286 | out: |
| 1287 | brelse(bh); |
| 1288 | |
| 1289 | return ret; |
| 1290 | } |
| 1291 | |
| 1292 | static int ocfs2_xattr_cleanup(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1293 | handle_t *handle, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1294 | struct ocfs2_xattr_info *xi, |
| 1295 | struct ocfs2_xattr_search *xs, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1296 | struct ocfs2_xattr_value_buf *vb, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1297 | size_t offs) |
| 1298 | { |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1299 | int ret = 0; |
| 1300 | size_t name_len = strlen(xi->name); |
| 1301 | void *val = xs->base + offs; |
| 1302 | size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE; |
| 1303 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1304 | ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1305 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1306 | if (ret) { |
| 1307 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1308 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1309 | } |
| 1310 | /* Decrease xattr count */ |
| 1311 | le16_add_cpu(&xs->header->xh_count, -1); |
| 1312 | /* Remove the xattr entry and tree root which has already be set*/ |
| 1313 | memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry)); |
| 1314 | memset(val, 0, size); |
| 1315 | |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1316 | ret = ocfs2_journal_dirty(handle, vb->vb_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1317 | if (ret < 0) |
| 1318 | mlog_errno(ret); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1319 | out: |
| 1320 | return ret; |
| 1321 | } |
| 1322 | |
| 1323 | static int ocfs2_xattr_update_entry(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1324 | handle_t *handle, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1325 | struct ocfs2_xattr_info *xi, |
| 1326 | struct ocfs2_xattr_search *xs, |
Joel Becker | 0c748e9 | 2008-12-09 15:46:15 -0800 | [diff] [blame] | 1327 | struct ocfs2_xattr_value_buf *vb, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1328 | size_t offs) |
| 1329 | { |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1330 | int ret; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1331 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1332 | ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh, |
Joel Becker | 0c748e9 | 2008-12-09 15:46:15 -0800 | [diff] [blame] | 1333 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1334 | if (ret) { |
| 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 | |
| 1339 | xs->here->xe_name_offset = cpu_to_le16(offs); |
| 1340 | xs->here->xe_value_size = cpu_to_le64(xi->value_len); |
| 1341 | if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE) |
| 1342 | ocfs2_xattr_set_local(xs->here, 1); |
| 1343 | else |
| 1344 | ocfs2_xattr_set_local(xs->here, 0); |
| 1345 | ocfs2_xattr_hash_entry(inode, xs->header, xs->here); |
| 1346 | |
Joel Becker | 0c748e9 | 2008-12-09 15:46:15 -0800 | [diff] [blame] | 1347 | ret = ocfs2_journal_dirty(handle, vb->vb_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1348 | if (ret < 0) |
| 1349 | mlog_errno(ret); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1350 | out: |
| 1351 | return ret; |
| 1352 | } |
| 1353 | |
| 1354 | /* |
| 1355 | * ocfs2_xattr_set_value_outside() |
| 1356 | * |
| 1357 | * Set large size value in B tree. |
| 1358 | */ |
| 1359 | static int ocfs2_xattr_set_value_outside(struct inode *inode, |
| 1360 | struct ocfs2_xattr_info *xi, |
| 1361 | struct ocfs2_xattr_search *xs, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 1362 | struct ocfs2_xattr_set_ctxt *ctxt, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1363 | struct ocfs2_xattr_value_buf *vb, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1364 | size_t offs) |
| 1365 | { |
| 1366 | size_t name_len = strlen(xi->name); |
| 1367 | void *val = xs->base + offs; |
| 1368 | struct ocfs2_xattr_value_root *xv = NULL; |
| 1369 | size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE; |
| 1370 | int ret = 0; |
| 1371 | |
| 1372 | memset(val, 0, size); |
| 1373 | memcpy(val, xi->name, name_len); |
| 1374 | xv = (struct ocfs2_xattr_value_root *) |
| 1375 | (val + OCFS2_XATTR_SIZE(name_len)); |
| 1376 | xv->xr_clusters = 0; |
| 1377 | xv->xr_last_eb_blk = 0; |
| 1378 | xv->xr_list.l_tree_depth = 0; |
| 1379 | xv->xr_list.l_count = cpu_to_le16(1); |
| 1380 | xv->xr_list.l_next_free_rec = 0; |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1381 | vb->vb_xv = xv; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1382 | |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1383 | ret = ocfs2_xattr_value_truncate(inode, vb, xi->value_len, ctxt); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1384 | if (ret < 0) { |
| 1385 | mlog_errno(ret); |
| 1386 | return ret; |
| 1387 | } |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1388 | ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1389 | if (ret < 0) { |
| 1390 | mlog_errno(ret); |
| 1391 | return ret; |
| 1392 | } |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 1393 | ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1394 | xi->value, xi->value_len); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1395 | if (ret < 0) |
| 1396 | mlog_errno(ret); |
| 1397 | |
| 1398 | return ret; |
| 1399 | } |
| 1400 | |
| 1401 | /* |
| 1402 | * ocfs2_xattr_set_entry_local() |
| 1403 | * |
| 1404 | * Set, replace or remove extended attribute in local. |
| 1405 | */ |
| 1406 | static void ocfs2_xattr_set_entry_local(struct inode *inode, |
| 1407 | struct ocfs2_xattr_info *xi, |
| 1408 | struct ocfs2_xattr_search *xs, |
| 1409 | struct ocfs2_xattr_entry *last, |
| 1410 | size_t min_offs) |
| 1411 | { |
| 1412 | size_t name_len = strlen(xi->name); |
| 1413 | int i; |
| 1414 | |
| 1415 | if (xi->value && xs->not_found) { |
| 1416 | /* Insert the new xattr entry. */ |
| 1417 | le16_add_cpu(&xs->header->xh_count, 1); |
| 1418 | ocfs2_xattr_set_type(last, xi->name_index); |
| 1419 | ocfs2_xattr_set_local(last, 1); |
| 1420 | last->xe_name_len = name_len; |
| 1421 | } else { |
| 1422 | void *first_val; |
| 1423 | void *val; |
| 1424 | size_t offs, size; |
| 1425 | |
| 1426 | first_val = xs->base + min_offs; |
| 1427 | offs = le16_to_cpu(xs->here->xe_name_offset); |
| 1428 | val = xs->base + offs; |
| 1429 | |
| 1430 | if (le64_to_cpu(xs->here->xe_value_size) > |
| 1431 | OCFS2_XATTR_INLINE_SIZE) |
| 1432 | size = OCFS2_XATTR_SIZE(name_len) + |
| 1433 | OCFS2_XATTR_ROOT_SIZE; |
| 1434 | else |
| 1435 | size = OCFS2_XATTR_SIZE(name_len) + |
| 1436 | OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size)); |
| 1437 | |
| 1438 | if (xi->value && size == OCFS2_XATTR_SIZE(name_len) + |
| 1439 | OCFS2_XATTR_SIZE(xi->value_len)) { |
| 1440 | /* The old and the new value have the |
| 1441 | same size. Just replace the value. */ |
| 1442 | ocfs2_xattr_set_local(xs->here, 1); |
| 1443 | xs->here->xe_value_size = cpu_to_le64(xi->value_len); |
| 1444 | /* Clear value bytes. */ |
| 1445 | memset(val + OCFS2_XATTR_SIZE(name_len), |
| 1446 | 0, |
| 1447 | OCFS2_XATTR_SIZE(xi->value_len)); |
| 1448 | memcpy(val + OCFS2_XATTR_SIZE(name_len), |
| 1449 | xi->value, |
| 1450 | xi->value_len); |
| 1451 | return; |
| 1452 | } |
| 1453 | /* Remove the old name+value. */ |
| 1454 | memmove(first_val + size, first_val, val - first_val); |
| 1455 | memset(first_val, 0, size); |
| 1456 | xs->here->xe_name_hash = 0; |
| 1457 | xs->here->xe_name_offset = 0; |
| 1458 | ocfs2_xattr_set_local(xs->here, 1); |
| 1459 | xs->here->xe_value_size = 0; |
| 1460 | |
| 1461 | min_offs += size; |
| 1462 | |
| 1463 | /* Adjust all value offsets. */ |
| 1464 | last = xs->header->xh_entries; |
| 1465 | for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) { |
| 1466 | size_t o = le16_to_cpu(last->xe_name_offset); |
| 1467 | |
| 1468 | if (o < offs) |
| 1469 | last->xe_name_offset = cpu_to_le16(o + size); |
| 1470 | last += 1; |
| 1471 | } |
| 1472 | |
| 1473 | if (!xi->value) { |
| 1474 | /* Remove the old entry. */ |
| 1475 | last -= 1; |
| 1476 | memmove(xs->here, xs->here + 1, |
| 1477 | (void *)last - (void *)xs->here); |
| 1478 | memset(last, 0, sizeof(struct ocfs2_xattr_entry)); |
| 1479 | le16_add_cpu(&xs->header->xh_count, -1); |
| 1480 | } |
| 1481 | } |
| 1482 | if (xi->value) { |
| 1483 | /* Insert the new name+value. */ |
| 1484 | size_t size = OCFS2_XATTR_SIZE(name_len) + |
| 1485 | OCFS2_XATTR_SIZE(xi->value_len); |
| 1486 | void *val = xs->base + min_offs - size; |
| 1487 | |
| 1488 | xs->here->xe_name_offset = cpu_to_le16(min_offs - size); |
| 1489 | memset(val, 0, size); |
| 1490 | memcpy(val, xi->name, name_len); |
| 1491 | memcpy(val + OCFS2_XATTR_SIZE(name_len), |
| 1492 | xi->value, |
| 1493 | xi->value_len); |
| 1494 | xs->here->xe_value_size = cpu_to_le64(xi->value_len); |
| 1495 | ocfs2_xattr_set_local(xs->here, 1); |
| 1496 | ocfs2_xattr_hash_entry(inode, xs->header, xs->here); |
| 1497 | } |
| 1498 | |
| 1499 | return; |
| 1500 | } |
| 1501 | |
| 1502 | /* |
| 1503 | * ocfs2_xattr_set_entry() |
| 1504 | * |
| 1505 | * Set extended attribute entry into inode or block. |
| 1506 | * |
| 1507 | * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE, |
| 1508 | * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(), |
| 1509 | * then set value in B tree with set_value_outside(). |
| 1510 | */ |
| 1511 | static int ocfs2_xattr_set_entry(struct inode *inode, |
| 1512 | struct ocfs2_xattr_info *xi, |
| 1513 | struct ocfs2_xattr_search *xs, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 1514 | struct ocfs2_xattr_set_ctxt *ctxt, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1515 | int flag) |
| 1516 | { |
| 1517 | struct ocfs2_xattr_entry *last; |
| 1518 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 1519 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; |
| 1520 | size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name); |
| 1521 | size_t size_l = 0; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1522 | handle_t *handle = ctxt->handle; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1523 | int free, i, ret; |
| 1524 | struct ocfs2_xattr_info xi_l = { |
| 1525 | .name_index = xi->name_index, |
| 1526 | .name = xi->name, |
| 1527 | .value = xi->value, |
| 1528 | .value_len = xi->value_len, |
| 1529 | }; |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1530 | struct ocfs2_xattr_value_buf vb = { |
| 1531 | .vb_bh = xs->xattr_bh, |
| 1532 | .vb_access = ocfs2_journal_access_di, |
| 1533 | }; |
| 1534 | |
| 1535 | if (!(flag & OCFS2_INLINE_XATTR_FL)) { |
| 1536 | BUG_ON(xs->xattr_bh == xs->inode_bh); |
| 1537 | vb.vb_access = ocfs2_journal_access_xb; |
| 1538 | } else |
| 1539 | BUG_ON(xs->xattr_bh != xs->inode_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1540 | |
| 1541 | /* Compute min_offs, last and free space. */ |
| 1542 | last = xs->header->xh_entries; |
| 1543 | |
| 1544 | for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) { |
| 1545 | size_t offs = le16_to_cpu(last->xe_name_offset); |
| 1546 | if (offs < min_offs) |
| 1547 | min_offs = offs; |
| 1548 | last += 1; |
| 1549 | } |
| 1550 | |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 1551 | free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1552 | if (free < 0) |
Joel Becker | b37c4d8 | 2008-10-20 18:24:03 -0700 | [diff] [blame] | 1553 | return -EIO; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1554 | |
| 1555 | if (!xs->not_found) { |
| 1556 | size_t size = 0; |
| 1557 | if (ocfs2_xattr_is_local(xs->here)) |
| 1558 | size = OCFS2_XATTR_SIZE(name_len) + |
| 1559 | OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size)); |
| 1560 | else |
| 1561 | size = OCFS2_XATTR_SIZE(name_len) + |
| 1562 | OCFS2_XATTR_ROOT_SIZE; |
| 1563 | free += (size + sizeof(struct ocfs2_xattr_entry)); |
| 1564 | } |
| 1565 | /* Check free space in inode or block */ |
| 1566 | if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 1567 | if (free < sizeof(struct ocfs2_xattr_entry) + |
| 1568 | OCFS2_XATTR_SIZE(name_len) + |
| 1569 | OCFS2_XATTR_ROOT_SIZE) { |
| 1570 | ret = -ENOSPC; |
| 1571 | goto out; |
| 1572 | } |
| 1573 | size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE; |
| 1574 | xi_l.value = (void *)&def_xv; |
| 1575 | xi_l.value_len = OCFS2_XATTR_ROOT_SIZE; |
| 1576 | } else if (xi->value) { |
| 1577 | if (free < sizeof(struct ocfs2_xattr_entry) + |
| 1578 | OCFS2_XATTR_SIZE(name_len) + |
| 1579 | OCFS2_XATTR_SIZE(xi->value_len)) { |
| 1580 | ret = -ENOSPC; |
| 1581 | goto out; |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | if (!xs->not_found) { |
| 1586 | /* For existing extended attribute */ |
| 1587 | size_t size = OCFS2_XATTR_SIZE(name_len) + |
| 1588 | OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size)); |
| 1589 | size_t offs = le16_to_cpu(xs->here->xe_name_offset); |
| 1590 | void *val = xs->base + offs; |
| 1591 | |
| 1592 | if (ocfs2_xattr_is_local(xs->here) && size == size_l) { |
| 1593 | /* Replace existing local xattr with tree root */ |
| 1594 | ret = ocfs2_xattr_set_value_outside(inode, xi, xs, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1595 | ctxt, &vb, offs); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1596 | if (ret < 0) |
| 1597 | mlog_errno(ret); |
| 1598 | goto out; |
| 1599 | } else if (!ocfs2_xattr_is_local(xs->here)) { |
| 1600 | /* For existing xattr which has value outside */ |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1601 | vb.vb_xv = (struct ocfs2_xattr_value_root *) |
| 1602 | (val + OCFS2_XATTR_SIZE(name_len)); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1603 | |
| 1604 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 1605 | /* |
| 1606 | * If new value need set outside also, |
| 1607 | * first truncate old value to new value, |
| 1608 | * then set new value with set_value_outside(). |
| 1609 | */ |
| 1610 | ret = ocfs2_xattr_value_truncate(inode, |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 1611 | &vb, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 1612 | xi->value_len, |
| 1613 | ctxt); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1614 | if (ret < 0) { |
| 1615 | mlog_errno(ret); |
| 1616 | goto out; |
| 1617 | } |
| 1618 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1619 | ret = ocfs2_xattr_update_entry(inode, |
| 1620 | handle, |
| 1621 | xi, |
| 1622 | xs, |
Joel Becker | 0c748e9 | 2008-12-09 15:46:15 -0800 | [diff] [blame] | 1623 | &vb, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1624 | offs); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1625 | if (ret < 0) { |
| 1626 | mlog_errno(ret); |
| 1627 | goto out; |
| 1628 | } |
| 1629 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1630 | ret = __ocfs2_xattr_set_value_outside(inode, |
| 1631 | handle, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 1632 | &vb, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1633 | xi->value, |
| 1634 | xi->value_len); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1635 | if (ret < 0) |
| 1636 | mlog_errno(ret); |
| 1637 | goto out; |
| 1638 | } else { |
| 1639 | /* |
| 1640 | * If new value need set in local, |
| 1641 | * just trucate old value to zero. |
| 1642 | */ |
| 1643 | ret = ocfs2_xattr_value_truncate(inode, |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 1644 | &vb, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1645 | 0, |
| 1646 | ctxt); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1647 | if (ret < 0) |
| 1648 | mlog_errno(ret); |
| 1649 | } |
| 1650 | } |
| 1651 | } |
| 1652 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1653 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1654 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1655 | if (ret) { |
| 1656 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1657 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | if (!(flag & OCFS2_INLINE_XATTR_FL)) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1661 | ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1662 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1663 | if (ret) { |
| 1664 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1665 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | /* |
| 1670 | * Set value in local, include set tree root in local. |
| 1671 | * This is the first step for value size >INLINE_SIZE. |
| 1672 | */ |
| 1673 | ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs); |
| 1674 | |
| 1675 | if (!(flag & OCFS2_INLINE_XATTR_FL)) { |
| 1676 | ret = ocfs2_journal_dirty(handle, xs->xattr_bh); |
| 1677 | if (ret < 0) { |
| 1678 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1679 | goto out; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) && |
| 1684 | (flag & OCFS2_INLINE_XATTR_FL)) { |
| 1685 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 1686 | unsigned int xattrsize = osb->s_xattr_inline_size; |
| 1687 | |
| 1688 | /* |
| 1689 | * Adjust extent record count or inline data size |
| 1690 | * to reserve space for extended attribute. |
| 1691 | */ |
| 1692 | if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
| 1693 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 1694 | le16_add_cpu(&idata->id_count, -xattrsize); |
| 1695 | } else if (!(ocfs2_inode_is_fast_symlink(inode))) { |
| 1696 | struct ocfs2_extent_list *el = &di->id2.i_list; |
| 1697 | le16_add_cpu(&el->l_count, -(xattrsize / |
| 1698 | sizeof(struct ocfs2_extent_rec))); |
| 1699 | } |
| 1700 | di->i_xattr_inline_size = cpu_to_le16(xattrsize); |
| 1701 | } |
| 1702 | /* Update xattr flag */ |
| 1703 | spin_lock(&oi->ip_lock); |
| 1704 | oi->ip_dyn_features |= flag; |
| 1705 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 1706 | spin_unlock(&oi->ip_lock); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1707 | |
| 1708 | ret = ocfs2_journal_dirty(handle, xs->inode_bh); |
| 1709 | if (ret < 0) |
| 1710 | mlog_errno(ret); |
| 1711 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1712 | if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 1713 | /* |
| 1714 | * Set value outside in B tree. |
| 1715 | * This is the second step for value size > INLINE_SIZE. |
| 1716 | */ |
| 1717 | size_t offs = le16_to_cpu(xs->here->xe_name_offset); |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1718 | ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt, |
| 1719 | &vb, offs); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1720 | if (ret < 0) { |
| 1721 | int ret2; |
| 1722 | |
| 1723 | mlog_errno(ret); |
| 1724 | /* |
| 1725 | * If set value outside failed, we have to clean |
| 1726 | * the junk tree root we have already set in local. |
| 1727 | */ |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1728 | ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle, |
Joel Becker | 512620f | 2008-12-09 15:58:35 -0800 | [diff] [blame] | 1729 | xi, xs, &vb, offs); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1730 | if (ret2 < 0) |
| 1731 | mlog_errno(ret2); |
| 1732 | } |
| 1733 | } |
| 1734 | out: |
| 1735 | return ret; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1736 | } |
| 1737 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1738 | static int ocfs2_remove_value_outside(struct inode*inode, |
Joel Becker | 4311901 | 2008-12-09 16:24:43 -0800 | [diff] [blame] | 1739 | struct ocfs2_xattr_value_buf *vb, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1740 | struct ocfs2_xattr_header *header) |
| 1741 | { |
| 1742 | int ret = 0, i; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 1743 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 1744 | struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, }; |
| 1745 | |
| 1746 | ocfs2_init_dealloc_ctxt(&ctxt.dealloc); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1747 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1748 | ctxt.handle = ocfs2_start_trans(osb, |
| 1749 | ocfs2_remove_extent_credits(osb->sb)); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1750 | if (IS_ERR(ctxt.handle)) { |
| 1751 | ret = PTR_ERR(ctxt.handle); |
| 1752 | mlog_errno(ret); |
| 1753 | goto out; |
| 1754 | } |
| 1755 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1756 | for (i = 0; i < le16_to_cpu(header->xh_count); i++) { |
| 1757 | struct ocfs2_xattr_entry *entry = &header->xh_entries[i]; |
| 1758 | |
| 1759 | if (!ocfs2_xattr_is_local(entry)) { |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1760 | void *val; |
| 1761 | |
| 1762 | val = (void *)header + |
| 1763 | le16_to_cpu(entry->xe_name_offset); |
Joel Becker | 4311901 | 2008-12-09 16:24:43 -0800 | [diff] [blame] | 1764 | vb->vb_xv = (struct ocfs2_xattr_value_root *) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1765 | (val + OCFS2_XATTR_SIZE(entry->xe_name_len)); |
Joel Becker | 4311901 | 2008-12-09 16:24:43 -0800 | [diff] [blame] | 1766 | ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1767 | if (ret < 0) { |
| 1768 | mlog_errno(ret); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 1769 | break; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1770 | } |
| 1771 | } |
| 1772 | } |
| 1773 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1774 | ocfs2_commit_trans(osb, ctxt.handle); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 1775 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 1776 | ocfs2_run_deallocs(osb, &ctxt.dealloc); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 1777 | out: |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1778 | return ret; |
| 1779 | } |
| 1780 | |
| 1781 | static int ocfs2_xattr_ibody_remove(struct inode *inode, |
| 1782 | struct buffer_head *di_bh) |
| 1783 | { |
| 1784 | |
| 1785 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 1786 | struct ocfs2_xattr_header *header; |
| 1787 | int ret; |
Joel Becker | 4311901 | 2008-12-09 16:24:43 -0800 | [diff] [blame] | 1788 | struct ocfs2_xattr_value_buf vb = { |
| 1789 | .vb_bh = di_bh, |
| 1790 | .vb_access = ocfs2_journal_access_di, |
| 1791 | }; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1792 | |
| 1793 | header = (struct ocfs2_xattr_header *) |
| 1794 | ((void *)di + inode->i_sb->s_blocksize - |
| 1795 | le16_to_cpu(di->i_xattr_inline_size)); |
| 1796 | |
Joel Becker | 4311901 | 2008-12-09 16:24:43 -0800 | [diff] [blame] | 1797 | ret = ocfs2_remove_value_outside(inode, &vb, header); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1798 | |
| 1799 | return ret; |
| 1800 | } |
| 1801 | |
| 1802 | static int ocfs2_xattr_block_remove(struct inode *inode, |
| 1803 | struct buffer_head *blk_bh) |
| 1804 | { |
| 1805 | struct ocfs2_xattr_block *xb; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1806 | int ret = 0; |
Joel Becker | 4311901 | 2008-12-09 16:24:43 -0800 | [diff] [blame] | 1807 | struct ocfs2_xattr_value_buf vb = { |
| 1808 | .vb_bh = blk_bh, |
| 1809 | .vb_access = ocfs2_journal_access_xb, |
| 1810 | }; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1811 | |
| 1812 | xb = (struct ocfs2_xattr_block *)blk_bh->b_data; |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 1813 | if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { |
| 1814 | struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header); |
Joel Becker | 4311901 | 2008-12-09 16:24:43 -0800 | [diff] [blame] | 1815 | ret = ocfs2_remove_value_outside(inode, &vb, header); |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 1816 | } else |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame^] | 1817 | ret = ocfs2_iterate_xattr_index_block(inode, |
| 1818 | blk_bh, |
| 1819 | ocfs2_rm_xattr_cluster, |
| 1820 | NULL); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1821 | |
| 1822 | return ret; |
| 1823 | } |
| 1824 | |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 1825 | static int ocfs2_xattr_free_block(struct inode *inode, |
| 1826 | u64 block) |
| 1827 | { |
| 1828 | struct inode *xb_alloc_inode; |
| 1829 | struct buffer_head *xb_alloc_bh = NULL; |
| 1830 | struct buffer_head *blk_bh = NULL; |
| 1831 | struct ocfs2_xattr_block *xb; |
| 1832 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 1833 | handle_t *handle; |
| 1834 | int ret = 0; |
| 1835 | u64 blk, bg_blkno; |
| 1836 | u16 bit; |
| 1837 | |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 1838 | ret = ocfs2_read_xattr_block(inode, block, &blk_bh); |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 1839 | if (ret < 0) { |
| 1840 | mlog_errno(ret); |
| 1841 | goto out; |
| 1842 | } |
| 1843 | |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 1844 | ret = ocfs2_xattr_block_remove(inode, blk_bh); |
| 1845 | if (ret < 0) { |
| 1846 | mlog_errno(ret); |
| 1847 | goto out; |
| 1848 | } |
| 1849 | |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 1850 | xb = (struct ocfs2_xattr_block *)blk_bh->b_data; |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 1851 | blk = le64_to_cpu(xb->xb_blkno); |
| 1852 | bit = le16_to_cpu(xb->xb_suballoc_bit); |
| 1853 | bg_blkno = ocfs2_which_suballoc_group(blk, bit); |
| 1854 | |
| 1855 | xb_alloc_inode = ocfs2_get_system_file_inode(osb, |
| 1856 | EXTENT_ALLOC_SYSTEM_INODE, |
| 1857 | le16_to_cpu(xb->xb_suballoc_slot)); |
| 1858 | if (!xb_alloc_inode) { |
| 1859 | ret = -ENOMEM; |
| 1860 | mlog_errno(ret); |
| 1861 | goto out; |
| 1862 | } |
| 1863 | mutex_lock(&xb_alloc_inode->i_mutex); |
| 1864 | |
| 1865 | ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1); |
| 1866 | if (ret < 0) { |
| 1867 | mlog_errno(ret); |
| 1868 | goto out_mutex; |
| 1869 | } |
| 1870 | |
| 1871 | handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE); |
| 1872 | if (IS_ERR(handle)) { |
| 1873 | ret = PTR_ERR(handle); |
| 1874 | mlog_errno(ret); |
| 1875 | goto out_unlock; |
| 1876 | } |
| 1877 | |
| 1878 | ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh, |
| 1879 | bit, bg_blkno, 1); |
| 1880 | if (ret < 0) |
| 1881 | mlog_errno(ret); |
| 1882 | |
| 1883 | ocfs2_commit_trans(osb, handle); |
| 1884 | out_unlock: |
| 1885 | ocfs2_inode_unlock(xb_alloc_inode, 1); |
| 1886 | brelse(xb_alloc_bh); |
| 1887 | out_mutex: |
| 1888 | mutex_unlock(&xb_alloc_inode->i_mutex); |
| 1889 | iput(xb_alloc_inode); |
| 1890 | out: |
| 1891 | brelse(blk_bh); |
| 1892 | return ret; |
| 1893 | } |
| 1894 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1895 | /* |
| 1896 | * ocfs2_xattr_remove() |
| 1897 | * |
| 1898 | * Free extended attribute resources associated with this inode. |
| 1899 | */ |
| 1900 | int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh) |
| 1901 | { |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1902 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 1903 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 1904 | handle_t *handle; |
| 1905 | int ret; |
| 1906 | |
Tiger Yang | 8154da3 | 2008-08-18 17:11:46 +0800 | [diff] [blame] | 1907 | if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) |
| 1908 | return 0; |
| 1909 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1910 | if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) |
| 1911 | return 0; |
| 1912 | |
| 1913 | if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) { |
| 1914 | ret = ocfs2_xattr_ibody_remove(inode, di_bh); |
| 1915 | if (ret < 0) { |
| 1916 | mlog_errno(ret); |
| 1917 | goto out; |
| 1918 | } |
| 1919 | } |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1920 | |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 1921 | if (di->i_xattr_loc) { |
| 1922 | ret = ocfs2_xattr_free_block(inode, |
| 1923 | le64_to_cpu(di->i_xattr_loc)); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1924 | if (ret < 0) { |
| 1925 | mlog_errno(ret); |
| 1926 | goto out; |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), |
| 1931 | OCFS2_INODE_UPDATE_CREDITS); |
| 1932 | if (IS_ERR(handle)) { |
| 1933 | ret = PTR_ERR(handle); |
| 1934 | mlog_errno(ret); |
| 1935 | goto out; |
| 1936 | } |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1937 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 8400897 | 2008-12-09 16:11:49 -0800 | [diff] [blame] | 1938 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1939 | if (ret) { |
| 1940 | mlog_errno(ret); |
| 1941 | goto out_commit; |
| 1942 | } |
| 1943 | |
Tao Ma | 0841389 | 2008-08-29 09:00:19 +0800 | [diff] [blame] | 1944 | di->i_xattr_loc = 0; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1945 | |
| 1946 | spin_lock(&oi->ip_lock); |
| 1947 | oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL); |
| 1948 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 1949 | spin_unlock(&oi->ip_lock); |
| 1950 | |
| 1951 | ret = ocfs2_journal_dirty(handle, di_bh); |
| 1952 | if (ret < 0) |
| 1953 | mlog_errno(ret); |
| 1954 | out_commit: |
| 1955 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); |
| 1956 | out: |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 1957 | return ret; |
| 1958 | } |
| 1959 | |
| 1960 | static int ocfs2_xattr_has_space_inline(struct inode *inode, |
| 1961 | struct ocfs2_dinode *di) |
| 1962 | { |
| 1963 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 1964 | unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size; |
| 1965 | int free; |
| 1966 | |
| 1967 | if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE) |
| 1968 | return 0; |
| 1969 | |
| 1970 | if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
| 1971 | struct ocfs2_inline_data *idata = &di->id2.i_data; |
| 1972 | free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size); |
| 1973 | } else if (ocfs2_inode_is_fast_symlink(inode)) { |
| 1974 | free = ocfs2_fast_symlink_chars(inode->i_sb) - |
| 1975 | le64_to_cpu(di->i_size); |
| 1976 | } else { |
| 1977 | struct ocfs2_extent_list *el = &di->id2.i_list; |
| 1978 | free = (le16_to_cpu(el->l_count) - |
| 1979 | le16_to_cpu(el->l_next_free_rec)) * |
| 1980 | sizeof(struct ocfs2_extent_rec); |
| 1981 | } |
| 1982 | if (free >= xattrsize) |
| 1983 | return 1; |
| 1984 | |
| 1985 | return 0; |
| 1986 | } |
| 1987 | |
| 1988 | /* |
| 1989 | * ocfs2_xattr_ibody_find() |
| 1990 | * |
| 1991 | * Find extended attribute in inode block and |
| 1992 | * fill search info into struct ocfs2_xattr_search. |
| 1993 | */ |
| 1994 | static int ocfs2_xattr_ibody_find(struct inode *inode, |
| 1995 | int name_index, |
| 1996 | const char *name, |
| 1997 | struct ocfs2_xattr_search *xs) |
| 1998 | { |
| 1999 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 2000 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; |
| 2001 | int ret; |
| 2002 | int has_space = 0; |
| 2003 | |
| 2004 | if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) |
| 2005 | return 0; |
| 2006 | |
| 2007 | if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) { |
| 2008 | down_read(&oi->ip_alloc_sem); |
| 2009 | has_space = ocfs2_xattr_has_space_inline(inode, di); |
| 2010 | up_read(&oi->ip_alloc_sem); |
| 2011 | if (!has_space) |
| 2012 | return 0; |
| 2013 | } |
| 2014 | |
| 2015 | xs->xattr_bh = xs->inode_bh; |
| 2016 | xs->end = (void *)di + inode->i_sb->s_blocksize; |
| 2017 | if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) |
| 2018 | xs->header = (struct ocfs2_xattr_header *) |
| 2019 | (xs->end - le16_to_cpu(di->i_xattr_inline_size)); |
| 2020 | else |
| 2021 | xs->header = (struct ocfs2_xattr_header *) |
| 2022 | (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size); |
| 2023 | xs->base = (void *)xs->header; |
| 2024 | xs->here = xs->header->xh_entries; |
| 2025 | |
| 2026 | /* Find the named attribute. */ |
| 2027 | if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) { |
| 2028 | ret = ocfs2_xattr_find_entry(name_index, name, xs); |
| 2029 | if (ret && ret != -ENODATA) |
| 2030 | return ret; |
| 2031 | xs->not_found = ret; |
| 2032 | } |
| 2033 | |
| 2034 | return 0; |
| 2035 | } |
| 2036 | |
| 2037 | /* |
| 2038 | * ocfs2_xattr_ibody_set() |
| 2039 | * |
| 2040 | * Set, replace or remove an extended attribute into inode block. |
| 2041 | * |
| 2042 | */ |
| 2043 | static int ocfs2_xattr_ibody_set(struct inode *inode, |
| 2044 | struct ocfs2_xattr_info *xi, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2045 | struct ocfs2_xattr_search *xs, |
| 2046 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2047 | { |
| 2048 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 2049 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; |
| 2050 | int ret; |
| 2051 | |
| 2052 | if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) |
| 2053 | return -ENOSPC; |
| 2054 | |
| 2055 | down_write(&oi->ip_alloc_sem); |
| 2056 | if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) { |
| 2057 | if (!ocfs2_xattr_has_space_inline(inode, di)) { |
| 2058 | ret = -ENOSPC; |
| 2059 | goto out; |
| 2060 | } |
| 2061 | } |
| 2062 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2063 | ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2064 | (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL)); |
| 2065 | out: |
| 2066 | up_write(&oi->ip_alloc_sem); |
| 2067 | |
| 2068 | return ret; |
| 2069 | } |
| 2070 | |
| 2071 | /* |
| 2072 | * ocfs2_xattr_block_find() |
| 2073 | * |
| 2074 | * Find extended attribute in external block and |
| 2075 | * fill search info into struct ocfs2_xattr_search. |
| 2076 | */ |
| 2077 | static int ocfs2_xattr_block_find(struct inode *inode, |
| 2078 | int name_index, |
| 2079 | const char *name, |
| 2080 | struct ocfs2_xattr_search *xs) |
| 2081 | { |
| 2082 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; |
| 2083 | struct buffer_head *blk_bh = NULL; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 2084 | struct ocfs2_xattr_block *xb; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2085 | int ret = 0; |
| 2086 | |
| 2087 | if (!di->i_xattr_loc) |
| 2088 | return ret; |
| 2089 | |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 2090 | ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc), |
| 2091 | &blk_bh); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2092 | if (ret < 0) { |
| 2093 | mlog_errno(ret); |
| 2094 | return ret; |
| 2095 | } |
Joel Becker | f6087fb | 2008-10-20 18:20:43 -0700 | [diff] [blame] | 2096 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2097 | xs->xattr_bh = blk_bh; |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 2098 | xb = (struct ocfs2_xattr_block *)blk_bh->b_data; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2099 | |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 2100 | if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { |
| 2101 | xs->header = &xb->xb_attrs.xb_header; |
| 2102 | xs->base = (void *)xs->header; |
| 2103 | xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size; |
| 2104 | xs->here = xs->header->xh_entries; |
| 2105 | |
| 2106 | ret = ocfs2_xattr_find_entry(name_index, name, xs); |
| 2107 | } else |
| 2108 | ret = ocfs2_xattr_index_block_find(inode, blk_bh, |
| 2109 | name_index, |
| 2110 | name, xs); |
| 2111 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2112 | if (ret && ret != -ENODATA) { |
| 2113 | xs->xattr_bh = NULL; |
| 2114 | goto cleanup; |
| 2115 | } |
| 2116 | xs->not_found = ret; |
| 2117 | return 0; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2118 | cleanup: |
| 2119 | brelse(blk_bh); |
| 2120 | |
| 2121 | return ret; |
| 2122 | } |
| 2123 | |
Tao Ma | 5aea1f0 | 2009-08-18 11:43:24 +0800 | [diff] [blame] | 2124 | static int ocfs2_create_xattr_block(handle_t *handle, |
| 2125 | struct inode *inode, |
| 2126 | struct buffer_head *inode_bh, |
| 2127 | struct ocfs2_alloc_context *meta_ac, |
| 2128 | struct buffer_head **ret_bh) |
| 2129 | { |
| 2130 | int ret; |
| 2131 | u16 suballoc_bit_start; |
| 2132 | u32 num_got; |
| 2133 | u64 first_blkno; |
| 2134 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data; |
| 2135 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 2136 | struct buffer_head *new_bh = NULL; |
| 2137 | struct ocfs2_xattr_block *xblk; |
| 2138 | |
| 2139 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh, |
| 2140 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 2141 | if (ret < 0) { |
| 2142 | mlog_errno(ret); |
| 2143 | goto end; |
| 2144 | } |
| 2145 | |
| 2146 | ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1, |
| 2147 | &suballoc_bit_start, &num_got, |
| 2148 | &first_blkno); |
| 2149 | if (ret < 0) { |
| 2150 | mlog_errno(ret); |
| 2151 | goto end; |
| 2152 | } |
| 2153 | |
| 2154 | new_bh = sb_getblk(inode->i_sb, first_blkno); |
| 2155 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh); |
| 2156 | |
| 2157 | ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), |
| 2158 | new_bh, |
| 2159 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 2160 | if (ret < 0) { |
| 2161 | mlog_errno(ret); |
| 2162 | goto end; |
| 2163 | } |
| 2164 | |
| 2165 | /* Initialize ocfs2_xattr_block */ |
| 2166 | xblk = (struct ocfs2_xattr_block *)new_bh->b_data; |
| 2167 | memset(xblk, 0, inode->i_sb->s_blocksize); |
| 2168 | strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE); |
| 2169 | xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num); |
| 2170 | xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start); |
| 2171 | xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation); |
| 2172 | xblk->xb_blkno = cpu_to_le64(first_blkno); |
| 2173 | |
| 2174 | ret = ocfs2_journal_dirty(handle, new_bh); |
| 2175 | if (ret < 0) { |
| 2176 | mlog_errno(ret); |
| 2177 | goto end; |
| 2178 | } |
| 2179 | di->i_xattr_loc = cpu_to_le64(first_blkno); |
| 2180 | ocfs2_journal_dirty(handle, inode_bh); |
| 2181 | |
| 2182 | *ret_bh = new_bh; |
| 2183 | new_bh = NULL; |
| 2184 | |
| 2185 | end: |
| 2186 | brelse(new_bh); |
| 2187 | return ret; |
| 2188 | } |
| 2189 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2190 | /* |
| 2191 | * ocfs2_xattr_block_set() |
| 2192 | * |
| 2193 | * Set, replace or remove an extended attribute into external block. |
| 2194 | * |
| 2195 | */ |
| 2196 | static int ocfs2_xattr_block_set(struct inode *inode, |
| 2197 | struct ocfs2_xattr_info *xi, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2198 | struct ocfs2_xattr_search *xs, |
| 2199 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2200 | { |
| 2201 | struct buffer_head *new_bh = NULL; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2202 | handle_t *handle = ctxt->handle; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2203 | struct ocfs2_xattr_block *xblk = NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2204 | int ret; |
| 2205 | |
| 2206 | if (!xs->xattr_bh) { |
Tao Ma | 5aea1f0 | 2009-08-18 11:43:24 +0800 | [diff] [blame] | 2207 | ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh, |
| 2208 | ctxt->meta_ac, &new_bh); |
| 2209 | if (ret) { |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2210 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2211 | goto end; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2212 | } |
| 2213 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2214 | xs->xattr_bh = new_bh; |
Tao Ma | 5aea1f0 | 2009-08-18 11:43:24 +0800 | [diff] [blame] | 2215 | xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2216 | xs->header = &xblk->xb_attrs.xb_header; |
| 2217 | xs->base = (void *)xs->header; |
| 2218 | xs->end = (void *)xblk + inode->i_sb->s_blocksize; |
| 2219 | xs->here = xs->header->xh_entries; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 2220 | } else |
| 2221 | xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; |
| 2222 | |
| 2223 | if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) { |
| 2224 | /* Set extended attribute into external block */ |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2225 | ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt, |
| 2226 | OCFS2_HAS_XATTR_FL); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 2227 | if (!ret || ret != -ENOSPC) |
| 2228 | goto end; |
| 2229 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2230 | ret = ocfs2_xattr_create_index_block(inode, xs, ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 2231 | if (ret) |
| 2232 | goto end; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2233 | } |
| 2234 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2235 | ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 2236 | |
| 2237 | end: |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2238 | |
| 2239 | return ret; |
| 2240 | } |
| 2241 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2242 | /* Check whether the new xattr can be inserted into the inode. */ |
| 2243 | static int ocfs2_xattr_can_be_in_inode(struct inode *inode, |
| 2244 | struct ocfs2_xattr_info *xi, |
| 2245 | struct ocfs2_xattr_search *xs) |
| 2246 | { |
| 2247 | u64 value_size; |
| 2248 | struct ocfs2_xattr_entry *last; |
| 2249 | int free, i; |
| 2250 | size_t min_offs = xs->end - xs->base; |
| 2251 | |
| 2252 | if (!xs->header) |
| 2253 | return 0; |
| 2254 | |
| 2255 | last = xs->header->xh_entries; |
| 2256 | |
| 2257 | for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) { |
| 2258 | size_t offs = le16_to_cpu(last->xe_name_offset); |
| 2259 | if (offs < min_offs) |
| 2260 | min_offs = offs; |
| 2261 | last += 1; |
| 2262 | } |
| 2263 | |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 2264 | free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2265 | if (free < 0) |
| 2266 | return 0; |
| 2267 | |
| 2268 | BUG_ON(!xs->not_found); |
| 2269 | |
| 2270 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) |
| 2271 | value_size = OCFS2_XATTR_ROOT_SIZE; |
| 2272 | else |
| 2273 | value_size = OCFS2_XATTR_SIZE(xi->value_len); |
| 2274 | |
| 2275 | if (free >= sizeof(struct ocfs2_xattr_entry) + |
| 2276 | OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size) |
| 2277 | return 1; |
| 2278 | |
| 2279 | return 0; |
| 2280 | } |
| 2281 | |
| 2282 | static int ocfs2_calc_xattr_set_need(struct inode *inode, |
| 2283 | struct ocfs2_dinode *di, |
| 2284 | struct ocfs2_xattr_info *xi, |
| 2285 | struct ocfs2_xattr_search *xis, |
| 2286 | struct ocfs2_xattr_search *xbs, |
| 2287 | int *clusters_need, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2288 | int *meta_need, |
| 2289 | int *credits_need) |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2290 | { |
| 2291 | int ret = 0, old_in_xb = 0; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2292 | int clusters_add = 0, meta_add = 0, credits = 0; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2293 | struct buffer_head *bh = NULL; |
| 2294 | struct ocfs2_xattr_block *xb = NULL; |
| 2295 | struct ocfs2_xattr_entry *xe = NULL; |
| 2296 | struct ocfs2_xattr_value_root *xv = NULL; |
| 2297 | char *base = NULL; |
| 2298 | int name_offset, name_len = 0; |
| 2299 | u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, |
| 2300 | xi->value_len); |
| 2301 | u64 value_size; |
| 2302 | |
Tao Ma | 71d548a | 2008-12-05 06:20:54 +0800 | [diff] [blame] | 2303 | /* |
| 2304 | * Calculate the clusters we need to write. |
| 2305 | * No matter whether we replace an old one or add a new one, |
| 2306 | * we need this for writing. |
| 2307 | */ |
| 2308 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) |
| 2309 | credits += new_clusters * |
| 2310 | ocfs2_clusters_to_blocks(inode->i_sb, 1); |
| 2311 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2312 | if (xis->not_found && xbs->not_found) { |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2313 | credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
| 2314 | |
| 2315 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) { |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2316 | clusters_add += new_clusters; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2317 | credits += ocfs2_calc_extend_credits(inode->i_sb, |
| 2318 | &def_xv.xv.xr_list, |
| 2319 | new_clusters); |
| 2320 | } |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2321 | |
| 2322 | goto meta_guess; |
| 2323 | } |
| 2324 | |
| 2325 | if (!xis->not_found) { |
| 2326 | xe = xis->here; |
| 2327 | name_offset = le16_to_cpu(xe->xe_name_offset); |
| 2328 | name_len = OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 2329 | base = xis->base; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2330 | credits += OCFS2_INODE_UPDATE_CREDITS; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2331 | } else { |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 2332 | int i, block_off = 0; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2333 | xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data; |
| 2334 | xe = xbs->here; |
| 2335 | name_offset = le16_to_cpu(xe->xe_name_offset); |
| 2336 | name_len = OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 2337 | i = xbs->here - xbs->header->xh_entries; |
| 2338 | old_in_xb = 1; |
| 2339 | |
| 2340 | if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) { |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 2341 | ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2342 | bucket_xh(xbs->bucket), |
| 2343 | i, &block_off, |
| 2344 | &name_offset); |
| 2345 | base = bucket_block(xbs->bucket, block_off); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2346 | credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
| 2347 | } else { |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2348 | base = xbs->base; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2349 | credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS; |
| 2350 | } |
| 2351 | } |
| 2352 | |
| 2353 | /* |
| 2354 | * delete a xattr doesn't need metadata and cluster allocation. |
| 2355 | * so just calculate the credits and return. |
| 2356 | * |
| 2357 | * The credits for removing the value tree will be extended |
| 2358 | * by ocfs2_remove_extent itself. |
| 2359 | */ |
| 2360 | if (!xi->value) { |
| 2361 | if (!ocfs2_xattr_is_local(xe)) |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 2362 | credits += ocfs2_remove_extent_credits(inode->i_sb); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2363 | |
| 2364 | goto out; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | /* do cluster allocation guess first. */ |
| 2368 | value_size = le64_to_cpu(xe->xe_value_size); |
| 2369 | |
| 2370 | if (old_in_xb) { |
| 2371 | /* |
| 2372 | * In xattr set, we always try to set the xe in inode first, |
| 2373 | * so if it can be inserted into inode successfully, the old |
| 2374 | * one will be removed from the xattr block, and this xattr |
| 2375 | * will be inserted into inode as a new xattr in inode. |
| 2376 | */ |
| 2377 | if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) { |
| 2378 | clusters_add += new_clusters; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 2379 | credits += ocfs2_remove_extent_credits(inode->i_sb) + |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2380 | OCFS2_INODE_UPDATE_CREDITS; |
| 2381 | if (!ocfs2_xattr_is_local(xe)) |
| 2382 | credits += ocfs2_calc_extend_credits( |
| 2383 | inode->i_sb, |
| 2384 | &def_xv.xv.xr_list, |
| 2385 | new_clusters); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2386 | goto out; |
| 2387 | } |
| 2388 | } |
| 2389 | |
| 2390 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 2391 | /* the new values will be stored outside. */ |
| 2392 | u32 old_clusters = 0; |
| 2393 | |
| 2394 | if (!ocfs2_xattr_is_local(xe)) { |
| 2395 | old_clusters = ocfs2_clusters_for_bytes(inode->i_sb, |
| 2396 | value_size); |
| 2397 | xv = (struct ocfs2_xattr_value_root *) |
| 2398 | (base + name_offset + name_len); |
Tao Ma | 97aff52 | 2008-11-19 16:48:41 +0800 | [diff] [blame] | 2399 | value_size = OCFS2_XATTR_ROOT_SIZE; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2400 | } else |
| 2401 | xv = &def_xv.xv; |
| 2402 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2403 | if (old_clusters >= new_clusters) { |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 2404 | credits += ocfs2_remove_extent_credits(inode->i_sb); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2405 | goto out; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2406 | } else { |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2407 | meta_add += ocfs2_extend_meta_needed(&xv->xr_list); |
| 2408 | clusters_add += new_clusters - old_clusters; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2409 | credits += ocfs2_calc_extend_credits(inode->i_sb, |
| 2410 | &xv->xr_list, |
| 2411 | new_clusters - |
| 2412 | old_clusters); |
Tao Ma | 97aff52 | 2008-11-19 16:48:41 +0800 | [diff] [blame] | 2413 | if (value_size >= OCFS2_XATTR_ROOT_SIZE) |
| 2414 | goto out; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2415 | } |
| 2416 | } else { |
| 2417 | /* |
| 2418 | * Now the new value will be stored inside. So if the new |
| 2419 | * value is smaller than the size of value root or the old |
| 2420 | * value, we don't need any allocation, otherwise we have |
| 2421 | * to guess metadata allocation. |
| 2422 | */ |
| 2423 | if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) || |
| 2424 | (!ocfs2_xattr_is_local(xe) && |
| 2425 | OCFS2_XATTR_ROOT_SIZE >= xi->value_len)) |
| 2426 | goto out; |
| 2427 | } |
| 2428 | |
| 2429 | meta_guess: |
| 2430 | /* calculate metadata allocation. */ |
| 2431 | if (di->i_xattr_loc) { |
| 2432 | if (!xbs->xattr_bh) { |
Joel Becker | 4ae1d69 | 2008-11-13 14:49:18 -0800 | [diff] [blame] | 2433 | ret = ocfs2_read_xattr_block(inode, |
| 2434 | le64_to_cpu(di->i_xattr_loc), |
| 2435 | &bh); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2436 | if (ret) { |
| 2437 | mlog_errno(ret); |
| 2438 | goto out; |
| 2439 | } |
| 2440 | |
| 2441 | xb = (struct ocfs2_xattr_block *)bh->b_data; |
| 2442 | } else |
| 2443 | xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data; |
| 2444 | |
Tao Ma | 90cb546 | 2008-12-05 06:20:56 +0800 | [diff] [blame] | 2445 | /* |
| 2446 | * If there is already an xattr tree, good, we can calculate |
| 2447 | * like other b-trees. Otherwise we may have the chance of |
| 2448 | * create a tree, the credit calculation is borrowed from |
| 2449 | * ocfs2_calc_extend_credits with root_el = NULL. And the |
| 2450 | * new tree will be cluster based, so no meta is needed. |
| 2451 | */ |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2452 | if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) { |
| 2453 | struct ocfs2_extent_list *el = |
| 2454 | &xb->xb_attrs.xb_root.xt_list; |
| 2455 | meta_add += ocfs2_extend_meta_needed(el); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2456 | credits += ocfs2_calc_extend_credits(inode->i_sb, |
| 2457 | el, 1); |
Tao Ma | 90cb546 | 2008-12-05 06:20:56 +0800 | [diff] [blame] | 2458 | } else |
| 2459 | credits += OCFS2_SUBALLOC_ALLOC + 1; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2460 | |
| 2461 | /* |
| 2462 | * This cluster will be used either for new bucket or for |
| 2463 | * new xattr block. |
| 2464 | * If the cluster size is the same as the bucket size, one |
| 2465 | * more is needed since we may need to extend the bucket |
| 2466 | * also. |
| 2467 | */ |
| 2468 | clusters_add += 1; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2469 | credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2470 | if (OCFS2_XATTR_BUCKET_SIZE == |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2471 | OCFS2_SB(inode->i_sb)->s_clustersize) { |
| 2472 | credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2473 | clusters_add += 1; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2474 | } |
| 2475 | } else { |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2476 | meta_add += 1; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2477 | credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS; |
| 2478 | } |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2479 | out: |
| 2480 | if (clusters_need) |
| 2481 | *clusters_need = clusters_add; |
| 2482 | if (meta_need) |
| 2483 | *meta_need = meta_add; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2484 | if (credits_need) |
| 2485 | *credits_need = credits; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2486 | brelse(bh); |
| 2487 | return ret; |
| 2488 | } |
| 2489 | |
| 2490 | static int ocfs2_init_xattr_set_ctxt(struct inode *inode, |
| 2491 | struct ocfs2_dinode *di, |
| 2492 | struct ocfs2_xattr_info *xi, |
| 2493 | struct ocfs2_xattr_search *xis, |
| 2494 | struct ocfs2_xattr_search *xbs, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2495 | struct ocfs2_xattr_set_ctxt *ctxt, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 2496 | int extra_meta, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2497 | int *credits) |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2498 | { |
| 2499 | int clusters_add, meta_add, ret; |
| 2500 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 2501 | |
| 2502 | memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt)); |
| 2503 | |
| 2504 | ocfs2_init_dealloc_ctxt(&ctxt->dealloc); |
| 2505 | |
| 2506 | ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2507 | &clusters_add, &meta_add, credits); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2508 | if (ret) { |
| 2509 | mlog_errno(ret); |
| 2510 | return ret; |
| 2511 | } |
| 2512 | |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 2513 | meta_add += extra_meta; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2514 | mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, " |
| 2515 | "credits = %d\n", xi->name, meta_add, clusters_add, *credits); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2516 | |
| 2517 | if (meta_add) { |
| 2518 | ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, |
| 2519 | &ctxt->meta_ac); |
| 2520 | if (ret) { |
| 2521 | mlog_errno(ret); |
| 2522 | goto out; |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | if (clusters_add) { |
| 2527 | ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac); |
| 2528 | if (ret) |
| 2529 | mlog_errno(ret); |
| 2530 | } |
| 2531 | out: |
| 2532 | if (ret) { |
| 2533 | if (ctxt->meta_ac) { |
| 2534 | ocfs2_free_alloc_context(ctxt->meta_ac); |
| 2535 | ctxt->meta_ac = NULL; |
| 2536 | } |
| 2537 | |
| 2538 | /* |
| 2539 | * We cannot have an error and a non null ctxt->data_ac. |
| 2540 | */ |
| 2541 | } |
| 2542 | |
| 2543 | return ret; |
| 2544 | } |
| 2545 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2546 | static int __ocfs2_xattr_set_handle(struct inode *inode, |
| 2547 | struct ocfs2_dinode *di, |
| 2548 | struct ocfs2_xattr_info *xi, |
| 2549 | struct ocfs2_xattr_search *xis, |
| 2550 | struct ocfs2_xattr_search *xbs, |
| 2551 | struct ocfs2_xattr_set_ctxt *ctxt) |
| 2552 | { |
Tao Ma | 9f868f1 | 2008-11-19 16:48:42 +0800 | [diff] [blame] | 2553 | int ret = 0, credits, old_found; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2554 | |
| 2555 | if (!xi->value) { |
| 2556 | /* Remove existing extended attribute */ |
| 2557 | if (!xis->not_found) |
| 2558 | ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt); |
| 2559 | else if (!xbs->not_found) |
| 2560 | ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt); |
| 2561 | } else { |
| 2562 | /* We always try to set extended attribute into inode first*/ |
| 2563 | ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt); |
| 2564 | if (!ret && !xbs->not_found) { |
| 2565 | /* |
| 2566 | * If succeed and that extended attribute existing in |
| 2567 | * external block, then we will remove it. |
| 2568 | */ |
| 2569 | xi->value = NULL; |
| 2570 | xi->value_len = 0; |
| 2571 | |
Tao Ma | 9f868f1 | 2008-11-19 16:48:42 +0800 | [diff] [blame] | 2572 | old_found = xis->not_found; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2573 | xis->not_found = -ENODATA; |
| 2574 | ret = ocfs2_calc_xattr_set_need(inode, |
| 2575 | di, |
| 2576 | xi, |
| 2577 | xis, |
| 2578 | xbs, |
| 2579 | NULL, |
| 2580 | NULL, |
| 2581 | &credits); |
Tao Ma | 9f868f1 | 2008-11-19 16:48:42 +0800 | [diff] [blame] | 2582 | xis->not_found = old_found; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2583 | if (ret) { |
| 2584 | mlog_errno(ret); |
| 2585 | goto out; |
| 2586 | } |
| 2587 | |
| 2588 | ret = ocfs2_extend_trans(ctxt->handle, credits + |
| 2589 | ctxt->handle->h_buffer_credits); |
| 2590 | if (ret) { |
| 2591 | mlog_errno(ret); |
| 2592 | goto out; |
| 2593 | } |
| 2594 | ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt); |
| 2595 | } else if (ret == -ENOSPC) { |
| 2596 | if (di->i_xattr_loc && !xbs->xattr_bh) { |
| 2597 | ret = ocfs2_xattr_block_find(inode, |
| 2598 | xi->name_index, |
| 2599 | xi->name, xbs); |
| 2600 | if (ret) |
| 2601 | goto out; |
| 2602 | |
Tao Ma | 9f868f1 | 2008-11-19 16:48:42 +0800 | [diff] [blame] | 2603 | old_found = xis->not_found; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2604 | xis->not_found = -ENODATA; |
| 2605 | ret = ocfs2_calc_xattr_set_need(inode, |
| 2606 | di, |
| 2607 | xi, |
| 2608 | xis, |
| 2609 | xbs, |
| 2610 | NULL, |
| 2611 | NULL, |
| 2612 | &credits); |
Tao Ma | 9f868f1 | 2008-11-19 16:48:42 +0800 | [diff] [blame] | 2613 | xis->not_found = old_found; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2614 | if (ret) { |
| 2615 | mlog_errno(ret); |
| 2616 | goto out; |
| 2617 | } |
| 2618 | |
| 2619 | ret = ocfs2_extend_trans(ctxt->handle, credits + |
| 2620 | ctxt->handle->h_buffer_credits); |
| 2621 | if (ret) { |
| 2622 | mlog_errno(ret); |
| 2623 | goto out; |
| 2624 | } |
| 2625 | } |
| 2626 | /* |
| 2627 | * If no space in inode, we will set extended attribute |
| 2628 | * into external block. |
| 2629 | */ |
| 2630 | ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt); |
| 2631 | if (ret) |
| 2632 | goto out; |
| 2633 | if (!xis->not_found) { |
| 2634 | /* |
| 2635 | * If succeed and that extended attribute |
| 2636 | * existing in inode, we will remove it. |
| 2637 | */ |
| 2638 | xi->value = NULL; |
| 2639 | xi->value_len = 0; |
| 2640 | xbs->not_found = -ENODATA; |
| 2641 | ret = ocfs2_calc_xattr_set_need(inode, |
| 2642 | di, |
| 2643 | xi, |
| 2644 | xis, |
| 2645 | xbs, |
| 2646 | NULL, |
| 2647 | NULL, |
| 2648 | &credits); |
| 2649 | if (ret) { |
| 2650 | mlog_errno(ret); |
| 2651 | goto out; |
| 2652 | } |
| 2653 | |
| 2654 | ret = ocfs2_extend_trans(ctxt->handle, credits + |
| 2655 | ctxt->handle->h_buffer_credits); |
| 2656 | if (ret) { |
| 2657 | mlog_errno(ret); |
| 2658 | goto out; |
| 2659 | } |
| 2660 | ret = ocfs2_xattr_ibody_set(inode, xi, |
| 2661 | xis, ctxt); |
| 2662 | } |
| 2663 | } |
| 2664 | } |
| 2665 | |
Tao Ma | 4b3f620 | 2008-12-05 06:20:55 +0800 | [diff] [blame] | 2666 | if (!ret) { |
| 2667 | /* Update inode ctime. */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2668 | ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), |
Tao Ma | 89a907a | 2009-02-17 04:39:28 +0800 | [diff] [blame] | 2669 | xis->inode_bh, |
| 2670 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 4b3f620 | 2008-12-05 06:20:55 +0800 | [diff] [blame] | 2671 | if (ret) { |
| 2672 | mlog_errno(ret); |
| 2673 | goto out; |
| 2674 | } |
| 2675 | |
| 2676 | inode->i_ctime = CURRENT_TIME; |
| 2677 | di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 2678 | di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 2679 | ocfs2_journal_dirty(ctxt->handle, xis->inode_bh); |
| 2680 | } |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2681 | out: |
| 2682 | return ret; |
| 2683 | } |
| 2684 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2685 | /* |
Tiger Yang | 6c3faba | 2008-11-14 11:16:03 +0800 | [diff] [blame] | 2686 | * This function only called duing creating inode |
| 2687 | * for init security/acl xattrs of the new inode. |
Tiger Yang | 008aafa | 2008-12-09 16:43:08 +0800 | [diff] [blame] | 2688 | * All transanction credits have been reserved in mknod. |
Tiger Yang | 6c3faba | 2008-11-14 11:16:03 +0800 | [diff] [blame] | 2689 | */ |
| 2690 | int ocfs2_xattr_set_handle(handle_t *handle, |
| 2691 | struct inode *inode, |
| 2692 | struct buffer_head *di_bh, |
| 2693 | int name_index, |
| 2694 | const char *name, |
| 2695 | const void *value, |
| 2696 | size_t value_len, |
| 2697 | int flags, |
| 2698 | struct ocfs2_alloc_context *meta_ac, |
| 2699 | struct ocfs2_alloc_context *data_ac) |
| 2700 | { |
| 2701 | struct ocfs2_dinode *di; |
| 2702 | int ret; |
| 2703 | |
| 2704 | struct ocfs2_xattr_info xi = { |
| 2705 | .name_index = name_index, |
| 2706 | .name = name, |
| 2707 | .value = value, |
| 2708 | .value_len = value_len, |
| 2709 | }; |
| 2710 | |
| 2711 | struct ocfs2_xattr_search xis = { |
| 2712 | .not_found = -ENODATA, |
| 2713 | }; |
| 2714 | |
| 2715 | struct ocfs2_xattr_search xbs = { |
| 2716 | .not_found = -ENODATA, |
| 2717 | }; |
| 2718 | |
| 2719 | struct ocfs2_xattr_set_ctxt ctxt = { |
| 2720 | .handle = handle, |
| 2721 | .meta_ac = meta_ac, |
| 2722 | .data_ac = data_ac, |
| 2723 | }; |
| 2724 | |
| 2725 | if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) |
| 2726 | return -EOPNOTSUPP; |
| 2727 | |
Tiger Yang | 008aafa | 2008-12-09 16:43:08 +0800 | [diff] [blame] | 2728 | /* |
| 2729 | * In extreme situation, may need xattr bucket when |
| 2730 | * block size is too small. And we have already reserved |
| 2731 | * the credits for bucket in mknod. |
| 2732 | */ |
| 2733 | if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) { |
| 2734 | xbs.bucket = ocfs2_xattr_bucket_new(inode); |
| 2735 | if (!xbs.bucket) { |
| 2736 | mlog_errno(-ENOMEM); |
| 2737 | return -ENOMEM; |
| 2738 | } |
| 2739 | } |
| 2740 | |
Tiger Yang | 6c3faba | 2008-11-14 11:16:03 +0800 | [diff] [blame] | 2741 | xis.inode_bh = xbs.inode_bh = di_bh; |
| 2742 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 2743 | |
| 2744 | down_write(&OCFS2_I(inode)->ip_xattr_sem); |
| 2745 | |
| 2746 | ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis); |
| 2747 | if (ret) |
| 2748 | goto cleanup; |
| 2749 | if (xis.not_found) { |
| 2750 | ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs); |
| 2751 | if (ret) |
| 2752 | goto cleanup; |
| 2753 | } |
| 2754 | |
| 2755 | ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt); |
| 2756 | |
| 2757 | cleanup: |
| 2758 | up_write(&OCFS2_I(inode)->ip_xattr_sem); |
| 2759 | brelse(xbs.xattr_bh); |
Tiger Yang | 008aafa | 2008-12-09 16:43:08 +0800 | [diff] [blame] | 2760 | ocfs2_xattr_bucket_free(xbs.bucket); |
Tiger Yang | 6c3faba | 2008-11-14 11:16:03 +0800 | [diff] [blame] | 2761 | |
| 2762 | return ret; |
| 2763 | } |
| 2764 | |
| 2765 | /* |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2766 | * ocfs2_xattr_set() |
| 2767 | * |
| 2768 | * Set, replace or remove an extended attribute for this inode. |
| 2769 | * value is NULL to remove an existing extended attribute, else either |
| 2770 | * create or replace an extended attribute. |
| 2771 | */ |
| 2772 | int ocfs2_xattr_set(struct inode *inode, |
| 2773 | int name_index, |
| 2774 | const char *name, |
| 2775 | const void *value, |
| 2776 | size_t value_len, |
| 2777 | int flags) |
| 2778 | { |
| 2779 | struct buffer_head *di_bh = NULL; |
| 2780 | struct ocfs2_dinode *di; |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 2781 | int ret, credits, ref_meta = 0, ref_credits = 0; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2782 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2783 | struct inode *tl_inode = osb->osb_tl_inode; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2784 | struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, }; |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 2785 | struct ocfs2_refcount_tree *ref_tree = NULL; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2786 | |
| 2787 | struct ocfs2_xattr_info xi = { |
| 2788 | .name_index = name_index, |
| 2789 | .name = name, |
| 2790 | .value = value, |
| 2791 | .value_len = value_len, |
| 2792 | }; |
| 2793 | |
| 2794 | struct ocfs2_xattr_search xis = { |
| 2795 | .not_found = -ENODATA, |
| 2796 | }; |
| 2797 | |
| 2798 | struct ocfs2_xattr_search xbs = { |
| 2799 | .not_found = -ENODATA, |
| 2800 | }; |
| 2801 | |
Tiger Yang | 8154da3 | 2008-08-18 17:11:46 +0800 | [diff] [blame] | 2802 | if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) |
| 2803 | return -EOPNOTSUPP; |
| 2804 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 2805 | /* |
| 2806 | * Only xbs will be used on indexed trees. xis doesn't need a |
| 2807 | * bucket. |
| 2808 | */ |
| 2809 | xbs.bucket = ocfs2_xattr_bucket_new(inode); |
| 2810 | if (!xbs.bucket) { |
| 2811 | mlog_errno(-ENOMEM); |
| 2812 | return -ENOMEM; |
| 2813 | } |
| 2814 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2815 | ret = ocfs2_inode_lock(inode, &di_bh, 1); |
| 2816 | if (ret < 0) { |
| 2817 | mlog_errno(ret); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 2818 | goto cleanup_nolock; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2819 | } |
| 2820 | xis.inode_bh = xbs.inode_bh = di_bh; |
| 2821 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 2822 | |
| 2823 | down_write(&OCFS2_I(inode)->ip_xattr_sem); |
| 2824 | /* |
| 2825 | * Scan inode and external block to find the same name |
| 2826 | * extended attribute and collect search infomation. |
| 2827 | */ |
| 2828 | ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis); |
| 2829 | if (ret) |
| 2830 | goto cleanup; |
| 2831 | if (xis.not_found) { |
| 2832 | ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs); |
| 2833 | if (ret) |
| 2834 | goto cleanup; |
| 2835 | } |
| 2836 | |
| 2837 | if (xis.not_found && xbs.not_found) { |
| 2838 | ret = -ENODATA; |
| 2839 | if (flags & XATTR_REPLACE) |
| 2840 | goto cleanup; |
| 2841 | ret = 0; |
| 2842 | if (!value) |
| 2843 | goto cleanup; |
| 2844 | } else { |
| 2845 | ret = -EEXIST; |
| 2846 | if (flags & XATTR_CREATE) |
| 2847 | goto cleanup; |
| 2848 | } |
| 2849 | |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 2850 | /* Check whether the value is refcounted and do some prepartion. */ |
| 2851 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL && |
| 2852 | (!xis.not_found || !xbs.not_found)) { |
| 2853 | ret = ocfs2_prepare_refcount_xattr(inode, di, &xi, |
| 2854 | &xis, &xbs, &ref_tree, |
| 2855 | &ref_meta, &ref_credits); |
| 2856 | if (ret) { |
| 2857 | mlog_errno(ret); |
| 2858 | goto cleanup; |
| 2859 | } |
| 2860 | } |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2861 | |
| 2862 | mutex_lock(&tl_inode->i_mutex); |
| 2863 | |
| 2864 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 2865 | ret = __ocfs2_flush_truncate_log(osb); |
| 2866 | if (ret < 0) { |
| 2867 | mutex_unlock(&tl_inode->i_mutex); |
| 2868 | mlog_errno(ret); |
| 2869 | goto cleanup; |
| 2870 | } |
| 2871 | } |
| 2872 | mutex_unlock(&tl_inode->i_mutex); |
| 2873 | |
| 2874 | ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 2875 | &xbs, &ctxt, ref_meta, &credits); |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2876 | if (ret) { |
| 2877 | mlog_errno(ret); |
| 2878 | goto cleanup; |
| 2879 | } |
| 2880 | |
Tao Ma | 4b3f620 | 2008-12-05 06:20:55 +0800 | [diff] [blame] | 2881 | /* we need to update inode's ctime field, so add credit for it. */ |
| 2882 | credits += OCFS2_INODE_UPDATE_CREDITS; |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 2883 | ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2884 | if (IS_ERR(ctxt.handle)) { |
| 2885 | ret = PTR_ERR(ctxt.handle); |
| 2886 | mlog_errno(ret); |
| 2887 | goto cleanup; |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2888 | } |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 2889 | |
| 2890 | ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt); |
| 2891 | |
| 2892 | ocfs2_commit_trans(osb, ctxt.handle); |
| 2893 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 2894 | if (ctxt.data_ac) |
| 2895 | ocfs2_free_alloc_context(ctxt.data_ac); |
| 2896 | if (ctxt.meta_ac) |
| 2897 | ocfs2_free_alloc_context(ctxt.meta_ac); |
| 2898 | if (ocfs2_dealloc_has_cluster(&ctxt.dealloc)) |
| 2899 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 2900 | ocfs2_run_deallocs(osb, &ctxt.dealloc); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2901 | cleanup: |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 2902 | if (ref_tree) |
| 2903 | ocfs2_unlock_refcount_tree(osb, ref_tree, 1); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2904 | up_write(&OCFS2_I(inode)->ip_xattr_sem); |
| 2905 | ocfs2_inode_unlock(inode, 1); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 2906 | cleanup_nolock: |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2907 | brelse(di_bh); |
| 2908 | brelse(xbs.xattr_bh); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 2909 | ocfs2_xattr_bucket_free(xbs.bucket); |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2910 | |
| 2911 | return ret; |
| 2912 | } |
| 2913 | |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 2914 | /* |
| 2915 | * Find the xattr extent rec which may contains name_hash. |
| 2916 | * e_cpos will be the first name hash of the xattr rec. |
| 2917 | * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list. |
| 2918 | */ |
| 2919 | static int ocfs2_xattr_get_rec(struct inode *inode, |
| 2920 | u32 name_hash, |
| 2921 | u64 *p_blkno, |
| 2922 | u32 *e_cpos, |
| 2923 | u32 *num_clusters, |
| 2924 | struct ocfs2_extent_list *el) |
| 2925 | { |
| 2926 | int ret = 0, i; |
| 2927 | struct buffer_head *eb_bh = NULL; |
| 2928 | struct ocfs2_extent_block *eb; |
| 2929 | struct ocfs2_extent_rec *rec = NULL; |
| 2930 | u64 e_blkno = 0; |
| 2931 | |
| 2932 | if (el->l_tree_depth) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 2933 | ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash, |
| 2934 | &eb_bh); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 2935 | if (ret) { |
| 2936 | mlog_errno(ret); |
| 2937 | goto out; |
| 2938 | } |
| 2939 | |
| 2940 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 2941 | el = &eb->h_list; |
| 2942 | |
| 2943 | if (el->l_tree_depth) { |
| 2944 | ocfs2_error(inode->i_sb, |
| 2945 | "Inode %lu has non zero tree depth in " |
| 2946 | "xattr tree block %llu\n", inode->i_ino, |
| 2947 | (unsigned long long)eb_bh->b_blocknr); |
| 2948 | ret = -EROFS; |
| 2949 | goto out; |
| 2950 | } |
| 2951 | } |
| 2952 | |
| 2953 | for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) { |
| 2954 | rec = &el->l_recs[i]; |
| 2955 | |
| 2956 | if (le32_to_cpu(rec->e_cpos) <= name_hash) { |
| 2957 | e_blkno = le64_to_cpu(rec->e_blkno); |
| 2958 | break; |
| 2959 | } |
| 2960 | } |
| 2961 | |
| 2962 | if (!e_blkno) { |
| 2963 | ocfs2_error(inode->i_sb, "Inode %lu has bad extent " |
| 2964 | "record (%u, %u, 0) in xattr", inode->i_ino, |
| 2965 | le32_to_cpu(rec->e_cpos), |
| 2966 | ocfs2_rec_clusters(el, rec)); |
| 2967 | ret = -EROFS; |
| 2968 | goto out; |
| 2969 | } |
| 2970 | |
| 2971 | *p_blkno = le64_to_cpu(rec->e_blkno); |
| 2972 | *num_clusters = le16_to_cpu(rec->e_leaf_clusters); |
| 2973 | if (e_cpos) |
| 2974 | *e_cpos = le32_to_cpu(rec->e_cpos); |
| 2975 | out: |
| 2976 | brelse(eb_bh); |
| 2977 | return ret; |
| 2978 | } |
| 2979 | |
| 2980 | typedef int (xattr_bucket_func)(struct inode *inode, |
| 2981 | struct ocfs2_xattr_bucket *bucket, |
| 2982 | void *para); |
| 2983 | |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 2984 | static int ocfs2_find_xe_in_bucket(struct inode *inode, |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 2985 | struct ocfs2_xattr_bucket *bucket, |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 2986 | int name_index, |
| 2987 | const char *name, |
| 2988 | u32 name_hash, |
| 2989 | u16 *xe_index, |
| 2990 | int *found) |
| 2991 | { |
| 2992 | int i, ret = 0, cmp = 1, block_off, new_offset; |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 2993 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 2994 | size_t name_len = strlen(name); |
| 2995 | struct ocfs2_xattr_entry *xe = NULL; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 2996 | char *xe_name; |
| 2997 | |
| 2998 | /* |
| 2999 | * We don't use binary search in the bucket because there |
| 3000 | * may be multiple entries with the same name hash. |
| 3001 | */ |
| 3002 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { |
| 3003 | xe = &xh->xh_entries[i]; |
| 3004 | |
| 3005 | if (name_hash > le32_to_cpu(xe->xe_name_hash)) |
| 3006 | continue; |
| 3007 | else if (name_hash < le32_to_cpu(xe->xe_name_hash)) |
| 3008 | break; |
| 3009 | |
| 3010 | cmp = name_index - ocfs2_xattr_get_type(xe); |
| 3011 | if (!cmp) |
| 3012 | cmp = name_len - xe->xe_name_len; |
| 3013 | if (cmp) |
| 3014 | continue; |
| 3015 | |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 3016 | ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3017 | xh, |
| 3018 | i, |
| 3019 | &block_off, |
| 3020 | &new_offset); |
| 3021 | if (ret) { |
| 3022 | mlog_errno(ret); |
| 3023 | break; |
| 3024 | } |
| 3025 | |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 3026 | |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3027 | xe_name = bucket_block(bucket, block_off) + new_offset; |
| 3028 | if (!memcmp(name, xe_name, name_len)) { |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3029 | *xe_index = i; |
| 3030 | *found = 1; |
| 3031 | ret = 0; |
| 3032 | break; |
| 3033 | } |
| 3034 | } |
| 3035 | |
| 3036 | return ret; |
| 3037 | } |
| 3038 | |
| 3039 | /* |
| 3040 | * Find the specified xattr entry in a series of buckets. |
| 3041 | * This series start from p_blkno and last for num_clusters. |
| 3042 | * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains |
| 3043 | * the num of the valid buckets. |
| 3044 | * |
| 3045 | * Return the buffer_head this xattr should reside in. And if the xattr's |
| 3046 | * hash is in the gap of 2 buckets, return the lower bucket. |
| 3047 | */ |
| 3048 | static int ocfs2_xattr_bucket_find(struct inode *inode, |
| 3049 | int name_index, |
| 3050 | const char *name, |
| 3051 | u32 name_hash, |
| 3052 | u64 p_blkno, |
| 3053 | u32 first_hash, |
| 3054 | u32 num_clusters, |
| 3055 | struct ocfs2_xattr_search *xs) |
| 3056 | { |
| 3057 | int ret, found = 0; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3058 | struct ocfs2_xattr_header *xh = NULL; |
| 3059 | struct ocfs2_xattr_entry *xe = NULL; |
| 3060 | u16 index = 0; |
| 3061 | u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
| 3062 | int low_bucket = 0, bucket, high_bucket; |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3063 | struct ocfs2_xattr_bucket *search; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3064 | u32 last_hash; |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3065 | u64 blkno, lower_blkno = 0; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3066 | |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3067 | search = ocfs2_xattr_bucket_new(inode); |
| 3068 | if (!search) { |
| 3069 | ret = -ENOMEM; |
| 3070 | mlog_errno(ret); |
| 3071 | goto out; |
| 3072 | } |
| 3073 | |
| 3074 | ret = ocfs2_read_xattr_bucket(search, p_blkno); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3075 | if (ret) { |
| 3076 | mlog_errno(ret); |
| 3077 | goto out; |
| 3078 | } |
| 3079 | |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3080 | xh = bucket_xh(search); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3081 | high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3082 | while (low_bucket <= high_bucket) { |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3083 | ocfs2_xattr_bucket_relse(search); |
| 3084 | |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3085 | bucket = (low_bucket + high_bucket) / 2; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3086 | blkno = p_blkno + bucket * blk_per_bucket; |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3087 | ret = ocfs2_read_xattr_bucket(search, blkno); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3088 | if (ret) { |
| 3089 | mlog_errno(ret); |
| 3090 | goto out; |
| 3091 | } |
| 3092 | |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3093 | xh = bucket_xh(search); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3094 | xe = &xh->xh_entries[0]; |
| 3095 | if (name_hash < le32_to_cpu(xe->xe_name_hash)) { |
| 3096 | high_bucket = bucket - 1; |
| 3097 | continue; |
| 3098 | } |
| 3099 | |
| 3100 | /* |
| 3101 | * Check whether the hash of the last entry in our |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 3102 | * bucket is larger than the search one. for an empty |
| 3103 | * bucket, the last one is also the first one. |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3104 | */ |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 3105 | if (xh->xh_count) |
| 3106 | xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1]; |
| 3107 | |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3108 | last_hash = le32_to_cpu(xe->xe_name_hash); |
| 3109 | |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3110 | /* record lower_blkno which may be the insert place. */ |
| 3111 | lower_blkno = blkno; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3112 | |
| 3113 | if (name_hash > le32_to_cpu(xe->xe_name_hash)) { |
| 3114 | low_bucket = bucket + 1; |
| 3115 | continue; |
| 3116 | } |
| 3117 | |
| 3118 | /* the searched xattr should reside in this bucket if exists. */ |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3119 | ret = ocfs2_find_xe_in_bucket(inode, search, |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3120 | name_index, name, name_hash, |
| 3121 | &index, &found); |
| 3122 | if (ret) { |
| 3123 | mlog_errno(ret); |
| 3124 | goto out; |
| 3125 | } |
| 3126 | break; |
| 3127 | } |
| 3128 | |
| 3129 | /* |
| 3130 | * Record the bucket we have found. |
| 3131 | * When the xattr's hash value is in the gap of 2 buckets, we will |
| 3132 | * always set it to the previous bucket. |
| 3133 | */ |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3134 | if (!lower_blkno) |
| 3135 | lower_blkno = p_blkno; |
| 3136 | |
| 3137 | /* This should be in cache - we just read it during the search */ |
| 3138 | ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno); |
| 3139 | if (ret) { |
| 3140 | mlog_errno(ret); |
| 3141 | goto out; |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3142 | } |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3143 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3144 | xs->header = bucket_xh(xs->bucket); |
| 3145 | xs->base = bucket_block(xs->bucket, 0); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3146 | xs->end = xs->base + inode->i_sb->s_blocksize; |
| 3147 | |
| 3148 | if (found) { |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3149 | xs->here = &xs->header->xh_entries[index]; |
| 3150 | mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3151 | (unsigned long long)bucket_blkno(xs->bucket), index); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3152 | } else |
| 3153 | ret = -ENODATA; |
| 3154 | |
| 3155 | out: |
Joel Becker | e2356a3 | 2008-10-27 15:01:54 -0700 | [diff] [blame] | 3156 | ocfs2_xattr_bucket_free(search); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3157 | return ret; |
| 3158 | } |
| 3159 | |
| 3160 | static int ocfs2_xattr_index_block_find(struct inode *inode, |
| 3161 | struct buffer_head *root_bh, |
| 3162 | int name_index, |
| 3163 | const char *name, |
| 3164 | struct ocfs2_xattr_search *xs) |
| 3165 | { |
| 3166 | int ret; |
| 3167 | struct ocfs2_xattr_block *xb = |
| 3168 | (struct ocfs2_xattr_block *)root_bh->b_data; |
| 3169 | struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root; |
| 3170 | struct ocfs2_extent_list *el = &xb_root->xt_list; |
| 3171 | u64 p_blkno = 0; |
| 3172 | u32 first_hash, num_clusters = 0; |
Tao Ma | 2057e5c | 2008-10-09 23:06:13 +0800 | [diff] [blame] | 3173 | u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name)); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3174 | |
| 3175 | if (le16_to_cpu(el->l_next_free_rec) == 0) |
| 3176 | return -ENODATA; |
| 3177 | |
| 3178 | mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n", |
| 3179 | name, name_hash, name_index); |
| 3180 | |
| 3181 | ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash, |
| 3182 | &num_clusters, el); |
| 3183 | if (ret) { |
| 3184 | mlog_errno(ret); |
| 3185 | goto out; |
| 3186 | } |
| 3187 | |
| 3188 | BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash); |
| 3189 | |
| 3190 | 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] | 3191 | "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno, |
| 3192 | first_hash); |
Tao Ma | 589dc26 | 2008-08-18 17:38:51 +0800 | [diff] [blame] | 3193 | |
| 3194 | ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash, |
| 3195 | p_blkno, first_hash, num_clusters, xs); |
| 3196 | |
| 3197 | out: |
| 3198 | return ret; |
| 3199 | } |
| 3200 | |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3201 | static int ocfs2_iterate_xattr_buckets(struct inode *inode, |
| 3202 | u64 blkno, |
| 3203 | u32 clusters, |
| 3204 | xattr_bucket_func *func, |
| 3205 | void *para) |
| 3206 | { |
Joel Becker | 6dde41d | 2008-10-24 17:16:48 -0700 | [diff] [blame] | 3207 | int i, ret = 0; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3208 | u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)); |
| 3209 | u32 num_buckets = clusters * bpc; |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3210 | struct ocfs2_xattr_bucket *bucket; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3211 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3212 | bucket = ocfs2_xattr_bucket_new(inode); |
| 3213 | if (!bucket) { |
| 3214 | mlog_errno(-ENOMEM); |
| 3215 | return -ENOMEM; |
| 3216 | } |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3217 | |
| 3218 | mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n", |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 3219 | clusters, (unsigned long long)blkno); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3220 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3221 | for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) { |
| 3222 | ret = ocfs2_read_xattr_bucket(bucket, blkno); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3223 | if (ret) { |
| 3224 | mlog_errno(ret); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3225 | break; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3226 | } |
| 3227 | |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3228 | /* |
| 3229 | * The real bucket num in this series of blocks is stored |
| 3230 | * in the 1st bucket. |
| 3231 | */ |
| 3232 | if (i == 0) |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3233 | num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3234 | |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 3235 | mlog(0, "iterating xattr bucket %llu, first hash %u\n", |
| 3236 | (unsigned long long)blkno, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3237 | le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash)); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3238 | if (func) { |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3239 | ret = func(inode, bucket, para); |
Tao Ma | a46fa68 | 2009-05-04 05:18:09 +0800 | [diff] [blame] | 3240 | if (ret && ret != -ERANGE) |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3241 | mlog_errno(ret); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3242 | /* Fall through to bucket_relse() */ |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3243 | } |
| 3244 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3245 | ocfs2_xattr_bucket_relse(bucket); |
| 3246 | if (ret) |
| 3247 | break; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3248 | } |
| 3249 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3250 | ocfs2_xattr_bucket_free(bucket); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3251 | return ret; |
| 3252 | } |
| 3253 | |
| 3254 | struct ocfs2_xattr_tree_list { |
| 3255 | char *buffer; |
| 3256 | size_t buffer_size; |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3257 | size_t result; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3258 | }; |
| 3259 | |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 3260 | static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb, |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3261 | struct ocfs2_xattr_header *xh, |
| 3262 | int index, |
| 3263 | int *block_off, |
| 3264 | int *new_offset) |
| 3265 | { |
| 3266 | u16 name_offset; |
| 3267 | |
| 3268 | if (index < 0 || index >= le16_to_cpu(xh->xh_count)) |
| 3269 | return -EINVAL; |
| 3270 | |
| 3271 | name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset); |
| 3272 | |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 3273 | *block_off = name_offset >> sb->s_blocksize_bits; |
| 3274 | *new_offset = name_offset % sb->s_blocksize; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3275 | |
| 3276 | return 0; |
| 3277 | } |
| 3278 | |
| 3279 | static int ocfs2_list_xattr_bucket(struct inode *inode, |
| 3280 | struct ocfs2_xattr_bucket *bucket, |
| 3281 | void *para) |
| 3282 | { |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3283 | int ret = 0, type; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3284 | struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3285 | int i, block_off, new_offset; |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3286 | const char *prefix, *name; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3287 | |
Joel Becker | 3e63294 | 2008-10-24 17:04:49 -0700 | [diff] [blame] | 3288 | for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) { |
| 3289 | struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i]; |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3290 | type = ocfs2_xattr_get_type(entry); |
| 3291 | prefix = ocfs2_xattr_prefix(type); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3292 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3293 | if (prefix) { |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 3294 | ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, |
Joel Becker | 3e63294 | 2008-10-24 17:04:49 -0700 | [diff] [blame] | 3295 | bucket_xh(bucket), |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3296 | i, |
| 3297 | &block_off, |
| 3298 | &new_offset); |
| 3299 | if (ret) |
| 3300 | break; |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3301 | |
Joel Becker | 51def39 | 2008-10-24 16:57:21 -0700 | [diff] [blame] | 3302 | name = (const char *)bucket_block(bucket, block_off) + |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3303 | new_offset; |
| 3304 | ret = ocfs2_xattr_list_entry(xl->buffer, |
| 3305 | xl->buffer_size, |
| 3306 | &xl->result, |
| 3307 | prefix, name, |
| 3308 | entry->xe_name_len); |
| 3309 | if (ret) |
| 3310 | break; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3311 | } |
| 3312 | } |
| 3313 | |
| 3314 | return ret; |
| 3315 | } |
| 3316 | |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame^] | 3317 | static int ocfs2_iterate_xattr_index_block(struct inode *inode, |
| 3318 | struct buffer_head *blk_bh, |
| 3319 | xattr_tree_rec_func *rec_func, |
| 3320 | void *para) |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3321 | { |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame^] | 3322 | struct ocfs2_xattr_block *xb = |
| 3323 | (struct ocfs2_xattr_block *)blk_bh->b_data; |
| 3324 | struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3325 | int ret = 0; |
| 3326 | u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0; |
| 3327 | u64 p_blkno = 0; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3328 | |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame^] | 3329 | if (!el->l_next_free_rec || !rec_func) |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3330 | return 0; |
| 3331 | |
| 3332 | while (name_hash > 0) { |
| 3333 | ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, |
| 3334 | &e_cpos, &num_clusters, el); |
| 3335 | if (ret) { |
| 3336 | mlog_errno(ret); |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame^] | 3337 | break; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3338 | } |
| 3339 | |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame^] | 3340 | ret = rec_func(inode, blk_bh, p_blkno, e_cpos, |
| 3341 | num_clusters, para); |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3342 | if (ret) { |
Tao Ma | a46fa68 | 2009-05-04 05:18:09 +0800 | [diff] [blame] | 3343 | if (ret != -ERANGE) |
| 3344 | mlog_errno(ret); |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame^] | 3345 | break; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3346 | } |
| 3347 | |
| 3348 | if (e_cpos == 0) |
| 3349 | break; |
| 3350 | |
| 3351 | name_hash = e_cpos - 1; |
| 3352 | } |
| 3353 | |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame^] | 3354 | return ret; |
| 3355 | |
| 3356 | } |
| 3357 | |
| 3358 | static int ocfs2_list_xattr_tree_rec(struct inode *inode, |
| 3359 | struct buffer_head *root_bh, |
| 3360 | u64 blkno, u32 cpos, u32 len, void *para) |
| 3361 | { |
| 3362 | return ocfs2_iterate_xattr_buckets(inode, blkno, len, |
| 3363 | ocfs2_list_xattr_bucket, para); |
| 3364 | } |
| 3365 | |
| 3366 | static int ocfs2_xattr_tree_list_index_block(struct inode *inode, |
| 3367 | struct buffer_head *blk_bh, |
| 3368 | char *buffer, |
| 3369 | size_t buffer_size) |
| 3370 | { |
| 3371 | int ret; |
| 3372 | struct ocfs2_xattr_tree_list xl = { |
| 3373 | .buffer = buffer, |
| 3374 | .buffer_size = buffer_size, |
| 3375 | .result = 0, |
| 3376 | }; |
| 3377 | |
| 3378 | ret = ocfs2_iterate_xattr_index_block(inode, blk_bh, |
| 3379 | ocfs2_list_xattr_tree_rec, &xl); |
| 3380 | if (ret) { |
| 3381 | mlog_errno(ret); |
| 3382 | goto out; |
| 3383 | } |
| 3384 | |
Tao Ma | 936b883 | 2008-10-09 23:06:14 +0800 | [diff] [blame] | 3385 | ret = xl.result; |
Tao Ma | 0c044f0 | 2008-08-18 17:38:50 +0800 | [diff] [blame] | 3386 | out: |
| 3387 | return ret; |
| 3388 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3389 | |
| 3390 | static int cmp_xe(const void *a, const void *b) |
| 3391 | { |
| 3392 | const struct ocfs2_xattr_entry *l = a, *r = b; |
| 3393 | u32 l_hash = le32_to_cpu(l->xe_name_hash); |
| 3394 | u32 r_hash = le32_to_cpu(r->xe_name_hash); |
| 3395 | |
| 3396 | if (l_hash > r_hash) |
| 3397 | return 1; |
| 3398 | if (l_hash < r_hash) |
| 3399 | return -1; |
| 3400 | return 0; |
| 3401 | } |
| 3402 | |
| 3403 | static void swap_xe(void *a, void *b, int size) |
| 3404 | { |
| 3405 | struct ocfs2_xattr_entry *l = a, *r = b, tmp; |
| 3406 | |
| 3407 | tmp = *l; |
| 3408 | memcpy(l, r, sizeof(struct ocfs2_xattr_entry)); |
| 3409 | memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry)); |
| 3410 | } |
| 3411 | |
| 3412 | /* |
| 3413 | * When the ocfs2_xattr_block is filled up, new bucket will be created |
| 3414 | * and all the xattr entries will be moved to the new bucket. |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3415 | * The header goes at the start of the bucket, and the names+values are |
| 3416 | * 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] | 3417 | * Note: we need to sort the entries since they are not saved in order |
| 3418 | * in the ocfs2_xattr_block. |
| 3419 | */ |
| 3420 | static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode, |
| 3421 | struct buffer_head *xb_bh, |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3422 | struct ocfs2_xattr_bucket *bucket) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3423 | { |
| 3424 | int i, blocksize = inode->i_sb->s_blocksize; |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3425 | int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3426 | u16 offset, size, off_change; |
| 3427 | struct ocfs2_xattr_entry *xe; |
| 3428 | struct ocfs2_xattr_block *xb = |
| 3429 | (struct ocfs2_xattr_block *)xb_bh->b_data; |
| 3430 | struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header; |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3431 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3432 | u16 count = le16_to_cpu(xb_xh->xh_count); |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3433 | char *src = xb_bh->b_data; |
| 3434 | char *target = bucket_block(bucket, blks - 1); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3435 | |
| 3436 | mlog(0, "cp xattr from block %llu to bucket %llu\n", |
| 3437 | (unsigned long long)xb_bh->b_blocknr, |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3438 | (unsigned long long)bucket_blkno(bucket)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3439 | |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3440 | for (i = 0; i < blks; i++) |
| 3441 | memset(bucket_block(bucket, i), 0, blocksize); |
| 3442 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3443 | /* |
| 3444 | * Since the xe_name_offset is based on ocfs2_xattr_header, |
| 3445 | * there is a offset change corresponding to the change of |
| 3446 | * ocfs2_xattr_header's position. |
| 3447 | */ |
| 3448 | off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); |
| 3449 | xe = &xb_xh->xh_entries[count - 1]; |
| 3450 | offset = le16_to_cpu(xe->xe_name_offset) + off_change; |
| 3451 | size = blocksize - offset; |
| 3452 | |
| 3453 | /* copy all the names and values. */ |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3454 | memcpy(target + offset, src + offset, size); |
| 3455 | |
| 3456 | /* Init new header now. */ |
| 3457 | xh->xh_count = xb_xh->xh_count; |
| 3458 | xh->xh_num_buckets = cpu_to_le16(1); |
| 3459 | xh->xh_name_value_len = cpu_to_le16(size); |
| 3460 | xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size); |
| 3461 | |
| 3462 | /* copy all the entries. */ |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3463 | target = bucket_block(bucket, 0); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3464 | offset = offsetof(struct ocfs2_xattr_header, xh_entries); |
| 3465 | size = count * sizeof(struct ocfs2_xattr_entry); |
| 3466 | memcpy(target + offset, (char *)xb_xh + offset, size); |
| 3467 | |
| 3468 | /* Change the xe offset for all the xe because of the move. */ |
| 3469 | off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize + |
| 3470 | offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); |
| 3471 | for (i = 0; i < count; i++) |
| 3472 | le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change); |
| 3473 | |
| 3474 | mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n", |
| 3475 | offset, size, off_change); |
| 3476 | |
| 3477 | sort(target + offset, count, sizeof(struct ocfs2_xattr_entry), |
| 3478 | cmp_xe, swap_xe); |
| 3479 | } |
| 3480 | |
| 3481 | /* |
| 3482 | * After we move xattr from block to index btree, we have to |
| 3483 | * update ocfs2_xattr_search to the new xe and base. |
| 3484 | * |
| 3485 | * When the entry is in xattr block, xattr_bh indicates the storage place. |
| 3486 | * While if the entry is in index b-tree, "bucket" indicates the |
| 3487 | * real place of the xattr. |
| 3488 | */ |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3489 | static void ocfs2_xattr_update_xattr_search(struct inode *inode, |
| 3490 | struct ocfs2_xattr_search *xs, |
| 3491 | struct buffer_head *old_bh) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3492 | { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3493 | char *buf = old_bh->b_data; |
| 3494 | struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf; |
| 3495 | struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header; |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3496 | int i; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3497 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3498 | xs->header = bucket_xh(xs->bucket); |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3499 | xs->base = bucket_block(xs->bucket, 0); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3500 | xs->end = xs->base + inode->i_sb->s_blocksize; |
| 3501 | |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3502 | if (xs->not_found) |
| 3503 | return; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3504 | |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3505 | i = xs->here - old_xh->xh_entries; |
| 3506 | xs->here = &xs->header->xh_entries[i]; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3507 | } |
| 3508 | |
| 3509 | static int ocfs2_xattr_create_index_block(struct inode *inode, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 3510 | struct ocfs2_xattr_search *xs, |
| 3511 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3512 | { |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3513 | int ret; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3514 | u32 bit_off, len; |
| 3515 | u64 blkno; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3516 | handle_t *handle = ctxt->handle; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3517 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 3518 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3519 | struct buffer_head *xb_bh = xs->xattr_bh; |
| 3520 | struct ocfs2_xattr_block *xb = |
| 3521 | (struct ocfs2_xattr_block *)xb_bh->b_data; |
| 3522 | struct ocfs2_xattr_tree_root *xr; |
| 3523 | u16 xb_flags = le16_to_cpu(xb->xb_flags); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3524 | |
| 3525 | mlog(0, "create xattr index block for %llu\n", |
| 3526 | (unsigned long long)xb_bh->b_blocknr); |
| 3527 | |
| 3528 | BUG_ON(xb_flags & OCFS2_XATTR_INDEXED); |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3529 | BUG_ON(!xs->bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3530 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3531 | /* |
| 3532 | * XXX: |
| 3533 | * We can use this lock for now, and maybe move to a dedicated mutex |
| 3534 | * if performance becomes a problem later. |
| 3535 | */ |
| 3536 | down_write(&oi->ip_alloc_sem); |
| 3537 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 3538 | ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh, |
Joel Becker | 8400897 | 2008-12-09 16:11:49 -0800 | [diff] [blame] | 3539 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3540 | if (ret) { |
| 3541 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3542 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3543 | } |
| 3544 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 3545 | ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, |
| 3546 | 1, 1, &bit_off, &len); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3547 | if (ret) { |
| 3548 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3549 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3550 | } |
| 3551 | |
| 3552 | /* |
| 3553 | * The bucket may spread in many blocks, and |
| 3554 | * we will only touch the 1st block and the last block |
| 3555 | * in the whole bucket(one for entry and one for data). |
| 3556 | */ |
| 3557 | blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off); |
| 3558 | |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 3559 | mlog(0, "allocate 1 cluster from %llu to xattr block\n", |
| 3560 | (unsigned long long)blkno); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3561 | |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3562 | ret = ocfs2_init_xattr_bucket(xs->bucket, blkno); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3563 | if (ret) { |
| 3564 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3565 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3566 | } |
| 3567 | |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3568 | ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket, |
| 3569 | OCFS2_JOURNAL_ACCESS_CREATE); |
Joel Becker | bd60bd3 | 2008-10-20 18:25:56 -0700 | [diff] [blame] | 3570 | if (ret) { |
| 3571 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3572 | goto out; |
Joel Becker | bd60bd3 | 2008-10-20 18:25:56 -0700 | [diff] [blame] | 3573 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3574 | |
Joel Becker | 178eeac | 2008-10-27 15:18:29 -0700 | [diff] [blame] | 3575 | ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket); |
| 3576 | ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket); |
| 3577 | |
| 3578 | ocfs2_xattr_update_xattr_search(inode, xs, xb_bh); |
| 3579 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3580 | /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */ |
| 3581 | memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize - |
| 3582 | offsetof(struct ocfs2_xattr_block, xb_attrs)); |
| 3583 | |
| 3584 | xr = &xb->xb_attrs.xb_root; |
| 3585 | xr->xt_clusters = cpu_to_le32(1); |
| 3586 | xr->xt_last_eb_blk = 0; |
| 3587 | xr->xt_list.l_tree_depth = 0; |
| 3588 | xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb)); |
| 3589 | xr->xt_list.l_next_free_rec = cpu_to_le16(1); |
| 3590 | |
| 3591 | xr->xt_list.l_recs[0].e_cpos = 0; |
| 3592 | xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno); |
| 3593 | xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1); |
| 3594 | |
| 3595 | xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED); |
| 3596 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3597 | ocfs2_journal_dirty(handle, xb_bh); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3598 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3599 | out: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3600 | up_write(&oi->ip_alloc_sem); |
| 3601 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3602 | return ret; |
| 3603 | } |
| 3604 | |
| 3605 | static int cmp_xe_offset(const void *a, const void *b) |
| 3606 | { |
| 3607 | const struct ocfs2_xattr_entry *l = a, *r = b; |
| 3608 | u32 l_name_offset = le16_to_cpu(l->xe_name_offset); |
| 3609 | u32 r_name_offset = le16_to_cpu(r->xe_name_offset); |
| 3610 | |
| 3611 | if (l_name_offset < r_name_offset) |
| 3612 | return 1; |
| 3613 | if (l_name_offset > r_name_offset) |
| 3614 | return -1; |
| 3615 | return 0; |
| 3616 | } |
| 3617 | |
| 3618 | /* |
| 3619 | * defrag a xattr bucket if we find that the bucket has some |
| 3620 | * holes beteen name/value pairs. |
| 3621 | * We will move all the name/value pairs to the end of the bucket |
| 3622 | * so that we can spare some space for insertion. |
| 3623 | */ |
| 3624 | static int ocfs2_defrag_xattr_bucket(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3625 | handle_t *handle, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3626 | struct ocfs2_xattr_bucket *bucket) |
| 3627 | { |
| 3628 | int ret, i; |
| 3629 | size_t end, offset, len, value_len; |
| 3630 | struct ocfs2_xattr_header *xh; |
| 3631 | char *entries, *buf, *bucket_buf = NULL; |
Joel Becker | 9c7759a | 2008-10-24 16:21:03 -0700 | [diff] [blame] | 3632 | u64 blkno = bucket_blkno(bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3633 | u16 xh_free_start; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3634 | size_t blocksize = inode->i_sb->s_blocksize; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3635 | struct ocfs2_xattr_entry *xe; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3636 | |
| 3637 | /* |
| 3638 | * In order to make the operation more efficient and generic, |
| 3639 | * we copy all the blocks into a contiguous memory and do the |
| 3640 | * defragment there, so if anything is error, we will not touch |
| 3641 | * the real block. |
| 3642 | */ |
| 3643 | bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS); |
| 3644 | if (!bucket_buf) { |
| 3645 | ret = -EIO; |
| 3646 | goto out; |
| 3647 | } |
| 3648 | |
Joel Becker | 161d6f3 | 2008-10-27 15:25:18 -0700 | [diff] [blame] | 3649 | buf = bucket_buf; |
Tao Ma | 1c32a2f | 2008-11-06 08:10:47 +0800 | [diff] [blame] | 3650 | for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize) |
| 3651 | memcpy(buf, bucket_block(bucket, i), blocksize); |
Joel Becker | 161d6f3 | 2008-10-27 15:25:18 -0700 | [diff] [blame] | 3652 | |
Tao Ma | 1c32a2f | 2008-11-06 08:10:47 +0800 | [diff] [blame] | 3653 | ret = ocfs2_xattr_bucket_journal_access(handle, bucket, |
Joel Becker | 161d6f3 | 2008-10-27 15:25:18 -0700 | [diff] [blame] | 3654 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 3655 | if (ret < 0) { |
| 3656 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3657 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3658 | } |
| 3659 | |
| 3660 | xh = (struct ocfs2_xattr_header *)bucket_buf; |
| 3661 | entries = (char *)xh->xh_entries; |
| 3662 | xh_free_start = le16_to_cpu(xh->xh_free_start); |
| 3663 | |
| 3664 | mlog(0, "adjust xattr bucket in %llu, count = %u, " |
| 3665 | "xh_free_start = %u, xh_name_value_len = %u.\n", |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 3666 | (unsigned long long)blkno, le16_to_cpu(xh->xh_count), |
| 3667 | xh_free_start, le16_to_cpu(xh->xh_name_value_len)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3668 | |
| 3669 | /* |
| 3670 | * sort all the entries by their offset. |
| 3671 | * the largest will be the first, so that we can |
| 3672 | * move them to the end one by one. |
| 3673 | */ |
| 3674 | sort(entries, le16_to_cpu(xh->xh_count), |
| 3675 | sizeof(struct ocfs2_xattr_entry), |
| 3676 | cmp_xe_offset, swap_xe); |
| 3677 | |
| 3678 | /* Move all name/values to the end of the bucket. */ |
| 3679 | xe = xh->xh_entries; |
| 3680 | end = OCFS2_XATTR_BUCKET_SIZE; |
| 3681 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) { |
| 3682 | offset = le16_to_cpu(xe->xe_name_offset); |
| 3683 | if (ocfs2_xattr_is_local(xe)) |
| 3684 | value_len = OCFS2_XATTR_SIZE( |
| 3685 | le64_to_cpu(xe->xe_value_size)); |
| 3686 | else |
| 3687 | value_len = OCFS2_XATTR_ROOT_SIZE; |
| 3688 | len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len; |
| 3689 | |
| 3690 | /* |
| 3691 | * We must make sure that the name/value pair |
| 3692 | * exist in the same block. So adjust end to |
| 3693 | * the previous block end if needed. |
| 3694 | */ |
| 3695 | if (((end - len) / blocksize != |
| 3696 | (end - 1) / blocksize)) |
| 3697 | end = end - end % blocksize; |
| 3698 | |
| 3699 | if (end > offset + len) { |
| 3700 | memmove(bucket_buf + end - len, |
| 3701 | bucket_buf + offset, len); |
| 3702 | xe->xe_name_offset = cpu_to_le16(end - len); |
| 3703 | } |
| 3704 | |
| 3705 | mlog_bug_on_msg(end < offset + len, "Defrag check failed for " |
| 3706 | "bucket %llu\n", (unsigned long long)blkno); |
| 3707 | |
| 3708 | end -= len; |
| 3709 | } |
| 3710 | |
| 3711 | mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for " |
| 3712 | "bucket %llu\n", (unsigned long long)blkno); |
| 3713 | |
| 3714 | if (xh_free_start == end) |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 3715 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3716 | |
| 3717 | memset(bucket_buf + xh_free_start, 0, end - xh_free_start); |
| 3718 | xh->xh_free_start = cpu_to_le16(end); |
| 3719 | |
| 3720 | /* sort the entries by their name_hash. */ |
| 3721 | sort(entries, le16_to_cpu(xh->xh_count), |
| 3722 | sizeof(struct ocfs2_xattr_entry), |
| 3723 | cmp_xe, swap_xe); |
| 3724 | |
| 3725 | buf = bucket_buf; |
Tao Ma | 1c32a2f | 2008-11-06 08:10:47 +0800 | [diff] [blame] | 3726 | for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize) |
| 3727 | memcpy(bucket_block(bucket, i), buf, blocksize); |
| 3728 | ocfs2_xattr_bucket_journal_dirty(handle, bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3729 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3730 | out: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3731 | kfree(bucket_buf); |
| 3732 | return ret; |
| 3733 | } |
| 3734 | |
| 3735 | /* |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3736 | * prev_blkno points to the start of an existing extent. new_blkno |
| 3737 | * points to a newly allocated extent. Because we know each of our |
| 3738 | * clusters contains more than bucket, we can easily split one cluster |
| 3739 | * at a bucket boundary. So we take the last cluster of the existing |
| 3740 | * extent and split it down the middle. We move the last half of the |
| 3741 | * buckets in the last cluster of the existing extent over to the new |
| 3742 | * extent. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3743 | * |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3744 | * first_bh is the buffer at prev_blkno so we can update the existing |
| 3745 | * extent's bucket count. header_bh is the bucket were we were hoping |
| 3746 | * to insert our xattr. If the bucket move places the target in the new |
| 3747 | * extent, we'll update first_bh and header_bh after modifying the old |
| 3748 | * extent. |
| 3749 | * |
| 3750 | * 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] | 3751 | */ |
| 3752 | static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode, |
| 3753 | handle_t *handle, |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3754 | struct ocfs2_xattr_bucket *first, |
| 3755 | struct ocfs2_xattr_bucket *target, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3756 | u64 new_blkno, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3757 | u32 num_clusters, |
| 3758 | u32 *first_hash) |
| 3759 | { |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 3760 | int ret; |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3761 | struct super_block *sb = inode->i_sb; |
| 3762 | int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb); |
| 3763 | int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb)); |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3764 | int to_move = num_buckets / 2; |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 3765 | u64 src_blkno; |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3766 | u64 last_cluster_blkno = bucket_blkno(first) + |
| 3767 | ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3768 | |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3769 | BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets); |
| 3770 | BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3771 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3772 | mlog(0, "move half of xattrs in cluster %llu to %llu\n", |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 3773 | (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3774 | |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3775 | ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first), |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 3776 | last_cluster_blkno, new_blkno, |
| 3777 | to_move, first_hash); |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3778 | if (ret) { |
| 3779 | mlog_errno(ret); |
| 3780 | goto out; |
| 3781 | } |
| 3782 | |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 3783 | /* This is the first bucket that got moved */ |
| 3784 | src_blkno = last_cluster_blkno + (to_move * blks_per_bucket); |
| 3785 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3786 | /* |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 3787 | * 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] | 3788 | * update first and target. |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3789 | */ |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3790 | if (bucket_blkno(target) >= src_blkno) { |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3791 | /* Find the block for the new target bucket */ |
| 3792 | src_blkno = new_blkno + |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3793 | (bucket_blkno(target) - src_blkno); |
| 3794 | |
| 3795 | ocfs2_xattr_bucket_relse(first); |
| 3796 | ocfs2_xattr_bucket_relse(target); |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3797 | |
| 3798 | /* |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 3799 | * These shouldn't fail - the buffers are in the |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3800 | * journal from ocfs2_cp_xattr_bucket(). |
| 3801 | */ |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3802 | ret = ocfs2_read_xattr_bucket(first, new_blkno); |
Joel Becker | c58b603 | 2008-11-26 13:36:24 -0800 | [diff] [blame] | 3803 | if (ret) { |
| 3804 | mlog_errno(ret); |
| 3805 | goto out; |
| 3806 | } |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 3807 | ret = ocfs2_read_xattr_bucket(target, src_blkno); |
| 3808 | if (ret) |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3809 | mlog_errno(ret); |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3810 | |
Joel Becker | b5c03e7 | 2008-11-25 19:58:16 -0800 | [diff] [blame] | 3811 | } |
| 3812 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3813 | out: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3814 | return ret; |
| 3815 | } |
| 3816 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3817 | /* |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 3818 | * Find the suitable pos when we divide a bucket into 2. |
| 3819 | * We have to make sure the xattrs with the same hash value exist |
| 3820 | * in the same bucket. |
| 3821 | * |
| 3822 | * If this ocfs2_xattr_header covers more than one hash value, find a |
| 3823 | * place where the hash value changes. Try to find the most even split. |
| 3824 | * The most common case is that all entries have different hash values, |
| 3825 | * and the first check we make will find a place to split. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3826 | */ |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 3827 | static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh) |
| 3828 | { |
| 3829 | struct ocfs2_xattr_entry *entries = xh->xh_entries; |
| 3830 | int count = le16_to_cpu(xh->xh_count); |
| 3831 | int delta, middle = count / 2; |
| 3832 | |
| 3833 | /* |
| 3834 | * We start at the middle. Each step gets farther away in both |
| 3835 | * directions. We therefore hit the change in hash value |
| 3836 | * nearest to the middle. Note that this loop does not execute for |
| 3837 | * count < 2. |
| 3838 | */ |
| 3839 | for (delta = 0; delta < middle; delta++) { |
| 3840 | /* Let's check delta earlier than middle */ |
| 3841 | if (cmp_xe(&entries[middle - delta - 1], |
| 3842 | &entries[middle - delta])) |
| 3843 | return middle - delta; |
| 3844 | |
| 3845 | /* For even counts, don't walk off the end */ |
| 3846 | if ((middle + delta + 1) == count) |
| 3847 | continue; |
| 3848 | |
| 3849 | /* Now try delta past middle */ |
| 3850 | if (cmp_xe(&entries[middle + delta], |
| 3851 | &entries[middle + delta + 1])) |
| 3852 | return middle + delta + 1; |
| 3853 | } |
| 3854 | |
| 3855 | /* Every entry had the same hash */ |
| 3856 | return count; |
| 3857 | } |
| 3858 | |
| 3859 | /* |
| 3860 | * Move some xattrs in old bucket(blk) to new bucket(new_blk). |
| 3861 | * first_hash will record the 1st hash of the new bucket. |
| 3862 | * |
| 3863 | * Normally half of the xattrs will be moved. But we have to make |
| 3864 | * sure that the xattrs with the same hash value are stored in the |
| 3865 | * same bucket. If all the xattrs in this bucket have the same hash |
| 3866 | * value, the new bucket will be initialized as an empty one and the |
| 3867 | * first_hash will be initialized as (hash_value+1). |
| 3868 | */ |
| 3869 | static int ocfs2_divide_xattr_bucket(struct inode *inode, |
| 3870 | handle_t *handle, |
| 3871 | u64 blk, |
| 3872 | u64 new_blk, |
| 3873 | u32 *first_hash, |
| 3874 | int new_bucket_head) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3875 | { |
| 3876 | int ret, i; |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 3877 | 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] | 3878 | struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3879 | struct ocfs2_xattr_header *xh; |
| 3880 | struct ocfs2_xattr_entry *xe; |
| 3881 | int blocksize = inode->i_sb->s_blocksize; |
| 3882 | |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 3883 | mlog(0, "move some of xattrs from bucket %llu to %llu\n", |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 3884 | (unsigned long long)blk, (unsigned long long)new_blk); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3885 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3886 | s_bucket = ocfs2_xattr_bucket_new(inode); |
| 3887 | t_bucket = ocfs2_xattr_bucket_new(inode); |
| 3888 | if (!s_bucket || !t_bucket) { |
| 3889 | ret = -ENOMEM; |
| 3890 | mlog_errno(ret); |
| 3891 | goto out; |
| 3892 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3893 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3894 | ret = ocfs2_read_xattr_bucket(s_bucket, blk); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3895 | if (ret) { |
| 3896 | mlog_errno(ret); |
| 3897 | goto out; |
| 3898 | } |
| 3899 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3900 | ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 3901 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3902 | if (ret) { |
| 3903 | mlog_errno(ret); |
| 3904 | goto out; |
| 3905 | } |
| 3906 | |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 3907 | /* |
| 3908 | * Even if !new_bucket_head, we're overwriting t_bucket. Thus, |
| 3909 | * there's no need to read it. |
| 3910 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3911 | ret = ocfs2_init_xattr_bucket(t_bucket, new_blk); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3912 | if (ret) { |
| 3913 | mlog_errno(ret); |
| 3914 | goto out; |
| 3915 | } |
| 3916 | |
Joel Becker | 2b656c1 | 2008-11-25 19:00:15 -0800 | [diff] [blame] | 3917 | /* |
| 3918 | * Hey, if we're overwriting t_bucket, what difference does |
| 3919 | * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the |
| 3920 | * same part of ocfs2_cp_xattr_bucket(). |
| 3921 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3922 | ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 3923 | new_bucket_head ? |
| 3924 | OCFS2_JOURNAL_ACCESS_CREATE : |
| 3925 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 3926 | if (ret) { |
| 3927 | mlog_errno(ret); |
| 3928 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3929 | } |
| 3930 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3931 | xh = bucket_xh(s_bucket); |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 3932 | count = le16_to_cpu(xh->xh_count); |
| 3933 | start = ocfs2_xattr_find_divide_pos(xh); |
| 3934 | |
| 3935 | if (start == count) { |
| 3936 | xe = &xh->xh_entries[start-1]; |
| 3937 | |
| 3938 | /* |
| 3939 | * initialized a new empty bucket here. |
| 3940 | * The hash value is set as one larger than |
| 3941 | * that of the last entry in the previous bucket. |
| 3942 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3943 | for (i = 0; i < t_bucket->bu_blocks; i++) |
| 3944 | memset(bucket_block(t_bucket, i), 0, blocksize); |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 3945 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3946 | xh = bucket_xh(t_bucket); |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 3947 | xh->xh_free_start = cpu_to_le16(blocksize); |
| 3948 | xh->xh_entries[0].xe_name_hash = xe->xe_name_hash; |
| 3949 | le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1); |
| 3950 | |
| 3951 | goto set_num_buckets; |
| 3952 | } |
| 3953 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3954 | /* copy the whole bucket to the new first. */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3955 | ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3956 | |
| 3957 | /* update the new bucket. */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 3958 | xh = bucket_xh(t_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3959 | |
| 3960 | /* |
| 3961 | * Calculate the total name/value len and xh_free_start for |
| 3962 | * the old bucket first. |
| 3963 | */ |
| 3964 | name_offset = OCFS2_XATTR_BUCKET_SIZE; |
| 3965 | name_value_len = 0; |
| 3966 | for (i = 0; i < start; i++) { |
| 3967 | xe = &xh->xh_entries[i]; |
| 3968 | xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 3969 | if (ocfs2_xattr_is_local(xe)) |
| 3970 | xe_len += |
| 3971 | OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); |
| 3972 | else |
| 3973 | xe_len += OCFS2_XATTR_ROOT_SIZE; |
| 3974 | name_value_len += xe_len; |
| 3975 | if (le16_to_cpu(xe->xe_name_offset) < name_offset) |
| 3976 | name_offset = le16_to_cpu(xe->xe_name_offset); |
| 3977 | } |
| 3978 | |
| 3979 | /* |
| 3980 | * Now begin the modification to the new bucket. |
| 3981 | * |
| 3982 | * In the new bucket, We just move the xattr entry to the beginning |
| 3983 | * and don't touch the name/value. So there will be some holes in the |
| 3984 | * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is |
| 3985 | * called. |
| 3986 | */ |
| 3987 | xe = &xh->xh_entries[start]; |
| 3988 | len = sizeof(struct ocfs2_xattr_entry) * (count - start); |
| 3989 | 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] | 3990 | (int)((char *)xe - (char *)xh), |
| 3991 | (int)((char *)xh->xh_entries - (char *)xh)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 3992 | memmove((char *)xh->xh_entries, (char *)xe, len); |
| 3993 | xe = &xh->xh_entries[count - start]; |
| 3994 | len = sizeof(struct ocfs2_xattr_entry) * start; |
| 3995 | memset((char *)xe, 0, len); |
| 3996 | |
| 3997 | le16_add_cpu(&xh->xh_count, -start); |
| 3998 | le16_add_cpu(&xh->xh_name_value_len, -name_value_len); |
| 3999 | |
| 4000 | /* Calculate xh_free_start for the new bucket. */ |
| 4001 | xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE); |
| 4002 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { |
| 4003 | xe = &xh->xh_entries[i]; |
| 4004 | xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 4005 | if (ocfs2_xattr_is_local(xe)) |
| 4006 | xe_len += |
| 4007 | OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); |
| 4008 | else |
| 4009 | xe_len += OCFS2_XATTR_ROOT_SIZE; |
| 4010 | if (le16_to_cpu(xe->xe_name_offset) < |
| 4011 | le16_to_cpu(xh->xh_free_start)) |
| 4012 | xh->xh_free_start = xe->xe_name_offset; |
| 4013 | } |
| 4014 | |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4015 | set_num_buckets: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4016 | /* set xh->xh_num_buckets for the new xh. */ |
| 4017 | if (new_bucket_head) |
| 4018 | xh->xh_num_buckets = cpu_to_le16(1); |
| 4019 | else |
| 4020 | xh->xh_num_buckets = 0; |
| 4021 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4022 | ocfs2_xattr_bucket_journal_dirty(handle, t_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4023 | |
| 4024 | /* store the first_hash of the new bucket. */ |
| 4025 | if (first_hash) |
| 4026 | *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash); |
| 4027 | |
| 4028 | /* |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4029 | * Now only update the 1st block of the old bucket. If we |
| 4030 | * just added a new empty bucket, there is no need to modify |
| 4031 | * it. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4032 | */ |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4033 | if (start == count) |
| 4034 | goto out; |
| 4035 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4036 | xh = bucket_xh(s_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4037 | memset(&xh->xh_entries[start], 0, |
| 4038 | sizeof(struct ocfs2_xattr_entry) * (count - start)); |
| 4039 | xh->xh_count = cpu_to_le16(start); |
| 4040 | xh->xh_free_start = cpu_to_le16(name_offset); |
| 4041 | xh->xh_name_value_len = cpu_to_le16(name_value_len); |
| 4042 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4043 | ocfs2_xattr_bucket_journal_dirty(handle, s_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4044 | |
| 4045 | out: |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4046 | ocfs2_xattr_bucket_free(s_bucket); |
| 4047 | ocfs2_xattr_bucket_free(t_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4048 | |
| 4049 | return ret; |
| 4050 | } |
| 4051 | |
| 4052 | /* |
| 4053 | * Copy xattr from one bucket to another bucket. |
| 4054 | * |
| 4055 | * The caller must make sure that the journal transaction |
| 4056 | * has enough space for journaling. |
| 4057 | */ |
| 4058 | static int ocfs2_cp_xattr_bucket(struct inode *inode, |
| 4059 | handle_t *handle, |
| 4060 | u64 s_blkno, |
| 4061 | u64 t_blkno, |
| 4062 | int t_is_new) |
| 4063 | { |
Joel Becker | 4980c6d | 2008-10-24 18:54:43 -0700 | [diff] [blame] | 4064 | int ret; |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4065 | struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4066 | |
| 4067 | BUG_ON(s_blkno == t_blkno); |
| 4068 | |
| 4069 | mlog(0, "cp bucket %llu to %llu, target is %d\n", |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 4070 | (unsigned long long)s_blkno, (unsigned long long)t_blkno, |
| 4071 | t_is_new); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4072 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4073 | s_bucket = ocfs2_xattr_bucket_new(inode); |
| 4074 | t_bucket = ocfs2_xattr_bucket_new(inode); |
| 4075 | if (!s_bucket || !t_bucket) { |
| 4076 | ret = -ENOMEM; |
| 4077 | mlog_errno(ret); |
| 4078 | goto out; |
| 4079 | } |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4080 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4081 | ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4082 | if (ret) |
| 4083 | goto out; |
| 4084 | |
Joel Becker | 784b816 | 2008-10-24 17:33:40 -0700 | [diff] [blame] | 4085 | /* |
| 4086 | * Even if !t_is_new, we're overwriting t_bucket. Thus, |
| 4087 | * there's no need to read it. |
| 4088 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4089 | ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4090 | if (ret) |
| 4091 | goto out; |
| 4092 | |
Joel Becker | 2b656c1 | 2008-11-25 19:00:15 -0800 | [diff] [blame] | 4093 | /* |
| 4094 | * Hey, if we're overwriting t_bucket, what difference does |
| 4095 | * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new |
Joel Becker | 874d65a | 2008-11-26 13:02:18 -0800 | [diff] [blame] | 4096 | * cluster to fill, we came here from |
| 4097 | * ocfs2_mv_xattr_buckets(), and it is really new - |
| 4098 | * ACCESS_CREATE is required. But we also might have moved data |
| 4099 | * out of t_bucket before extending back into it. |
| 4100 | * ocfs2_add_new_xattr_bucket() can do this - its call to |
| 4101 | * ocfs2_add_new_xattr_cluster() may have created a new extent |
Joel Becker | 2b656c1 | 2008-11-25 19:00:15 -0800 | [diff] [blame] | 4102 | * and copied out the end of the old extent. Then it re-extends |
| 4103 | * the old extent back to create space for new xattrs. That's |
| 4104 | * how we get here, and the bucket isn't really new. |
| 4105 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4106 | ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 4107 | t_is_new ? |
| 4108 | OCFS2_JOURNAL_ACCESS_CREATE : |
| 4109 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 4110 | if (ret) |
| 4111 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4112 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4113 | ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket); |
| 4114 | ocfs2_xattr_bucket_journal_dirty(handle, t_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4115 | |
| 4116 | out: |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4117 | ocfs2_xattr_bucket_free(t_bucket); |
| 4118 | ocfs2_xattr_bucket_free(s_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4119 | |
| 4120 | return ret; |
| 4121 | } |
| 4122 | |
| 4123 | /* |
Joel Becker | 874d65a | 2008-11-26 13:02:18 -0800 | [diff] [blame] | 4124 | * src_blk points to the start of an existing extent. last_blk points to |
| 4125 | * last cluster in that extent. to_blk points to a newly allocated |
Joel Becker | 54ecb6b | 2008-11-26 13:18:31 -0800 | [diff] [blame] | 4126 | * extent. We copy the buckets from the cluster at last_blk to the new |
| 4127 | * extent. If start_bucket is non-zero, we skip that many buckets before |
| 4128 | * we start copying. The new extent's xh_num_buckets gets set to the |
| 4129 | * number of buckets we copied. The old extent's xh_num_buckets shrinks |
| 4130 | * by the same amount. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4131 | */ |
Joel Becker | 54ecb6b | 2008-11-26 13:18:31 -0800 | [diff] [blame] | 4132 | static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle, |
| 4133 | u64 src_blk, u64 last_blk, u64 to_blk, |
| 4134 | unsigned int start_bucket, |
| 4135 | u32 *first_hash) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4136 | { |
| 4137 | int i, ret, credits; |
| 4138 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4139 | int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4140 | int num_buckets = ocfs2_xattr_buckets_per_cluster(osb); |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4141 | struct ocfs2_xattr_bucket *old_first, *new_first; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4142 | |
Joel Becker | 874d65a | 2008-11-26 13:02:18 -0800 | [diff] [blame] | 4143 | mlog(0, "mv xattrs from cluster %llu to %llu\n", |
| 4144 | (unsigned long long)last_blk, (unsigned long long)to_blk); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4145 | |
Joel Becker | 54ecb6b | 2008-11-26 13:18:31 -0800 | [diff] [blame] | 4146 | BUG_ON(start_bucket >= num_buckets); |
| 4147 | if (start_bucket) { |
| 4148 | num_buckets -= start_bucket; |
| 4149 | last_blk += (start_bucket * blks_per_bucket); |
| 4150 | } |
| 4151 | |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4152 | /* The first bucket of the original extent */ |
| 4153 | old_first = ocfs2_xattr_bucket_new(inode); |
| 4154 | /* The first bucket of the new extent */ |
| 4155 | new_first = ocfs2_xattr_bucket_new(inode); |
| 4156 | if (!old_first || !new_first) { |
| 4157 | ret = -ENOMEM; |
| 4158 | mlog_errno(ret); |
| 4159 | goto out; |
| 4160 | } |
| 4161 | |
Joel Becker | 874d65a | 2008-11-26 13:02:18 -0800 | [diff] [blame] | 4162 | ret = ocfs2_read_xattr_bucket(old_first, src_blk); |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4163 | if (ret) { |
| 4164 | mlog_errno(ret); |
| 4165 | goto out; |
| 4166 | } |
| 4167 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4168 | /* |
Joel Becker | 54ecb6b | 2008-11-26 13:18:31 -0800 | [diff] [blame] | 4169 | * We need to update the first bucket of the old extent and all |
| 4170 | * the buckets going to the new extent. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4171 | */ |
Joel Becker | 54ecb6b | 2008-11-26 13:18:31 -0800 | [diff] [blame] | 4172 | credits = ((num_buckets + 1) * blks_per_bucket) + |
| 4173 | handle->h_buffer_credits; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4174 | ret = ocfs2_extend_trans(handle, credits); |
| 4175 | if (ret) { |
| 4176 | mlog_errno(ret); |
| 4177 | goto out; |
| 4178 | } |
| 4179 | |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4180 | ret = ocfs2_xattr_bucket_journal_access(handle, old_first, |
| 4181 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4182 | if (ret) { |
| 4183 | mlog_errno(ret); |
| 4184 | goto out; |
| 4185 | } |
| 4186 | |
| 4187 | for (i = 0; i < num_buckets; i++) { |
| 4188 | ret = ocfs2_cp_xattr_bucket(inode, handle, |
Joel Becker | 874d65a | 2008-11-26 13:02:18 -0800 | [diff] [blame] | 4189 | last_blk + (i * blks_per_bucket), |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4190 | to_blk + (i * blks_per_bucket), |
| 4191 | 1); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4192 | if (ret) { |
| 4193 | mlog_errno(ret); |
| 4194 | goto out; |
| 4195 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4196 | } |
| 4197 | |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4198 | /* |
| 4199 | * Get the new bucket ready before we dirty anything |
| 4200 | * (This actually shouldn't fail, because we already dirtied |
| 4201 | * it once in ocfs2_cp_xattr_bucket()). |
| 4202 | */ |
| 4203 | ret = ocfs2_read_xattr_bucket(new_first, to_blk); |
| 4204 | if (ret) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4205 | mlog_errno(ret); |
| 4206 | goto out; |
| 4207 | } |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4208 | ret = ocfs2_xattr_bucket_journal_access(handle, new_first, |
| 4209 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4210 | if (ret) { |
| 4211 | mlog_errno(ret); |
| 4212 | goto out; |
| 4213 | } |
| 4214 | |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4215 | /* Now update the headers */ |
| 4216 | le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets); |
| 4217 | ocfs2_xattr_bucket_journal_dirty(handle, old_first); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4218 | |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4219 | bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets); |
| 4220 | ocfs2_xattr_bucket_journal_dirty(handle, new_first); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4221 | |
| 4222 | if (first_hash) |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4223 | *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash); |
| 4224 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4225 | out: |
Joel Becker | 15d6092 | 2008-11-25 18:36:42 -0800 | [diff] [blame] | 4226 | ocfs2_xattr_bucket_free(new_first); |
| 4227 | ocfs2_xattr_bucket_free(old_first); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4228 | return ret; |
| 4229 | } |
| 4230 | |
| 4231 | /* |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4232 | * Move some xattrs in this cluster to the new cluster. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4233 | * This function should only be called when bucket size == cluster size. |
| 4234 | * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead. |
| 4235 | */ |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4236 | static int ocfs2_divide_xattr_cluster(struct inode *inode, |
| 4237 | handle_t *handle, |
| 4238 | u64 prev_blk, |
| 4239 | u64 new_blk, |
| 4240 | u32 *first_hash) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4241 | { |
| 4242 | u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4243 | int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4244 | |
| 4245 | BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize); |
| 4246 | |
| 4247 | ret = ocfs2_extend_trans(handle, credits); |
| 4248 | if (ret) { |
| 4249 | mlog_errno(ret); |
| 4250 | return ret; |
| 4251 | } |
| 4252 | |
| 4253 | /* Move half of the xattr in start_blk to the next bucket. */ |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4254 | return ocfs2_divide_xattr_bucket(inode, handle, prev_blk, |
| 4255 | new_blk, first_hash, 1); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4256 | } |
| 4257 | |
| 4258 | /* |
| 4259 | * Move some xattrs from the old cluster to the new one since they are not |
| 4260 | * contiguous in ocfs2 xattr tree. |
| 4261 | * |
| 4262 | * new_blk starts a new separate cluster, and we will move some xattrs from |
| 4263 | * prev_blk to it. v_start will be set as the first name hash value in this |
| 4264 | * new cluster so that it can be used as e_cpos during tree insertion and |
| 4265 | * don't collide with our original b-tree operations. first_bh and header_bh |
| 4266 | * will also be updated since they will be used in ocfs2_extend_xattr_bucket |
| 4267 | * to extend the insert bucket. |
| 4268 | * |
| 4269 | * The problem is how much xattr should we move to the new one and when should |
| 4270 | * we update first_bh and header_bh? |
| 4271 | * 1. If cluster size > bucket size, that means the previous cluster has more |
| 4272 | * than 1 bucket, so just move half nums of bucket into the new cluster and |
| 4273 | * update the first_bh and header_bh if the insert bucket has been moved |
| 4274 | * to the new cluster. |
| 4275 | * 2. If cluster_size == bucket_size: |
| 4276 | * a) If the previous extent rec has more than one cluster and the insert |
| 4277 | * place isn't in the last cluster, copy the entire last cluster to the |
| 4278 | * new one. This time, we don't need to upate the first_bh and header_bh |
| 4279 | * since they will not be moved into the new cluster. |
| 4280 | * b) Otherwise, move the bottom half of the xattrs in the last cluster into |
| 4281 | * the new one. And we set the extend flag to zero if the insert place is |
| 4282 | * moved into the new allocated cluster since no extend is needed. |
| 4283 | */ |
| 4284 | static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode, |
| 4285 | handle_t *handle, |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4286 | struct ocfs2_xattr_bucket *first, |
| 4287 | struct ocfs2_xattr_bucket *target, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4288 | u64 new_blk, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4289 | u32 prev_clusters, |
| 4290 | u32 *v_start, |
| 4291 | int *extend) |
| 4292 | { |
Joel Becker | 92cf3ad | 2008-11-26 14:12:09 -0800 | [diff] [blame] | 4293 | int ret; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4294 | |
| 4295 | mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n", |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4296 | (unsigned long long)bucket_blkno(first), prev_clusters, |
Mark Fasheh | de29c08 | 2008-10-29 14:45:30 -0700 | [diff] [blame] | 4297 | (unsigned long long)new_blk); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4298 | |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4299 | if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4300 | ret = ocfs2_mv_xattr_bucket_cross_cluster(inode, |
| 4301 | handle, |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4302 | first, target, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4303 | new_blk, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4304 | prev_clusters, |
| 4305 | v_start); |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4306 | if (ret) |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4307 | mlog_errno(ret); |
Joel Becker | 41cb814 | 2008-11-26 14:25:21 -0800 | [diff] [blame] | 4308 | } else { |
Joel Becker | 92cf3ad | 2008-11-26 14:12:09 -0800 | [diff] [blame] | 4309 | /* The start of the last cluster in the first extent */ |
| 4310 | u64 last_blk = bucket_blkno(first) + |
| 4311 | ((prev_clusters - 1) * |
| 4312 | ocfs2_clusters_to_blocks(inode->i_sb, 1)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4313 | |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4314 | if (prev_clusters > 1 && bucket_blkno(target) != last_blk) { |
Joel Becker | 874d65a | 2008-11-26 13:02:18 -0800 | [diff] [blame] | 4315 | ret = ocfs2_mv_xattr_buckets(inode, handle, |
Joel Becker | 92cf3ad | 2008-11-26 14:12:09 -0800 | [diff] [blame] | 4316 | bucket_blkno(first), |
Joel Becker | 54ecb6b | 2008-11-26 13:18:31 -0800 | [diff] [blame] | 4317 | last_blk, new_blk, 0, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4318 | v_start); |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4319 | if (ret) |
| 4320 | mlog_errno(ret); |
| 4321 | } else { |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 4322 | ret = ocfs2_divide_xattr_cluster(inode, handle, |
| 4323 | last_blk, new_blk, |
| 4324 | v_start); |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4325 | if (ret) |
| 4326 | mlog_errno(ret); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4327 | |
Joel Becker | 92cf3ad | 2008-11-26 14:12:09 -0800 | [diff] [blame] | 4328 | if ((bucket_blkno(target) == last_blk) && extend) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4329 | *extend = 0; |
| 4330 | } |
| 4331 | } |
| 4332 | |
| 4333 | return ret; |
| 4334 | } |
| 4335 | |
| 4336 | /* |
| 4337 | * Add a new cluster for xattr storage. |
| 4338 | * |
| 4339 | * If the new cluster is contiguous with the previous one, it will be |
| 4340 | * appended to the same extent record, and num_clusters will be updated. |
| 4341 | * If not, we will insert a new extent for it and move some xattrs in |
| 4342 | * the last cluster into the new allocated one. |
| 4343 | * We also need to limit the maximum size of a btree leaf, otherwise we'll |
| 4344 | * lose the benefits of hashing because we'll have to search large leaves. |
| 4345 | * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize, |
| 4346 | * if it's bigger). |
| 4347 | * |
| 4348 | * first_bh is the first block of the previous extent rec and header_bh |
| 4349 | * indicates the bucket we will insert the new xattrs. They will be updated |
| 4350 | * when the header_bh is moved into the new cluster. |
| 4351 | */ |
| 4352 | static int ocfs2_add_new_xattr_cluster(struct inode *inode, |
| 4353 | struct buffer_head *root_bh, |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4354 | struct ocfs2_xattr_bucket *first, |
| 4355 | struct ocfs2_xattr_bucket *target, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4356 | u32 *num_clusters, |
| 4357 | u32 prev_cpos, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4358 | int *extend, |
| 4359 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4360 | { |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4361 | int ret; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4362 | u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); |
| 4363 | u32 prev_clusters = *num_clusters; |
| 4364 | u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0; |
| 4365 | u64 block; |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4366 | handle_t *handle = ctxt->handle; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4367 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4368 | struct ocfs2_extent_tree et; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4369 | |
| 4370 | mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, " |
| 4371 | "previous xattr blkno = %llu\n", |
| 4372 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4373 | prev_cpos, (unsigned long long)bucket_blkno(first)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4374 | |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 4375 | ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4376 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 4377 | ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh, |
Joel Becker | 8400897 | 2008-12-09 16:11:49 -0800 | [diff] [blame] | 4378 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4379 | if (ret < 0) { |
| 4380 | mlog_errno(ret); |
| 4381 | goto leave; |
| 4382 | } |
| 4383 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4384 | ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4385 | clusters_to_add, &bit_off, &num_bits); |
| 4386 | if (ret < 0) { |
| 4387 | if (ret != -ENOSPC) |
| 4388 | mlog_errno(ret); |
| 4389 | goto leave; |
| 4390 | } |
| 4391 | |
| 4392 | BUG_ON(num_bits > clusters_to_add); |
| 4393 | |
| 4394 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); |
| 4395 | mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n", |
| 4396 | num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 4397 | |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4398 | if (bucket_blkno(first) + (prev_clusters * bpc) == block && |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4399 | (prev_clusters + num_bits) << osb->s_clustersize_bits <= |
| 4400 | OCFS2_MAX_XATTR_TREE_LEAF_SIZE) { |
| 4401 | /* |
| 4402 | * If this cluster is contiguous with the old one and |
| 4403 | * adding this new cluster, we don't surpass the limit of |
| 4404 | * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be |
| 4405 | * initialized and used like other buckets in the previous |
| 4406 | * cluster. |
| 4407 | * So add it as a contiguous one. The caller will handle |
| 4408 | * its init process. |
| 4409 | */ |
| 4410 | v_start = prev_cpos + prev_clusters; |
| 4411 | *num_clusters = prev_clusters + num_bits; |
| 4412 | mlog(0, "Add contiguous %u clusters to previous extent rec.\n", |
| 4413 | num_bits); |
| 4414 | } else { |
| 4415 | ret = ocfs2_adjust_xattr_cross_cluster(inode, |
| 4416 | handle, |
Joel Becker | 012ee91 | 2008-11-26 14:43:31 -0800 | [diff] [blame] | 4417 | first, |
| 4418 | target, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4419 | block, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4420 | prev_clusters, |
| 4421 | &v_start, |
| 4422 | extend); |
| 4423 | if (ret) { |
| 4424 | mlog_errno(ret); |
| 4425 | goto leave; |
| 4426 | } |
| 4427 | } |
| 4428 | |
| 4429 | 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] | 4430 | num_bits, (unsigned long long)block, v_start); |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4431 | ret = ocfs2_insert_extent(handle, &et, v_start, block, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4432 | num_bits, 0, ctxt->meta_ac); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4433 | if (ret < 0) { |
| 4434 | mlog_errno(ret); |
| 4435 | goto leave; |
| 4436 | } |
| 4437 | |
| 4438 | ret = ocfs2_journal_dirty(handle, root_bh); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4439 | if (ret < 0) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4440 | mlog_errno(ret); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4441 | |
| 4442 | leave: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4443 | return ret; |
| 4444 | } |
| 4445 | |
| 4446 | /* |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4447 | * We are given an extent. 'first' is the bucket at the very front of |
| 4448 | * the extent. The extent has space for an additional bucket past |
| 4449 | * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number |
| 4450 | * of the target bucket. We wish to shift every bucket past the target |
| 4451 | * down one, filling in that additional space. When we get back to the |
| 4452 | * target, we split the target between itself and the now-empty bucket |
| 4453 | * at target+1 (aka, target_blkno + blks_per_bucket). |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4454 | */ |
| 4455 | static int ocfs2_extend_xattr_bucket(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4456 | handle_t *handle, |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4457 | struct ocfs2_xattr_bucket *first, |
| 4458 | u64 target_blk, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4459 | u32 num_clusters) |
| 4460 | { |
| 4461 | int ret, credits; |
| 4462 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 4463 | u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4464 | u64 end_blk; |
| 4465 | u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4466 | |
| 4467 | mlog(0, "extend xattr bucket in %llu, xattr extend rec starting " |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4468 | "from %llu, len = %u\n", (unsigned long long)target_blk, |
| 4469 | (unsigned long long)bucket_blkno(first), num_clusters); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4470 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4471 | /* The extent must have room for an additional bucket */ |
| 4472 | BUG_ON(new_bucket >= |
| 4473 | (num_clusters * ocfs2_xattr_buckets_per_cluster(osb))); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4474 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4475 | /* end_blk points to the last existing bucket */ |
| 4476 | end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4477 | |
| 4478 | /* |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4479 | * end_blk is the start of the last existing bucket. |
| 4480 | * Thus, (end_blk - target_blk) covers the target bucket and |
| 4481 | * every bucket after it up to, but not including, the last |
| 4482 | * existing bucket. Then we add the last existing bucket, the |
| 4483 | * new bucket, and the first bucket (3 * blk_per_bucket). |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4484 | */ |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4485 | credits = (end_blk - target_blk) + (3 * blk_per_bucket) + |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4486 | handle->h_buffer_credits; |
| 4487 | ret = ocfs2_extend_trans(handle, credits); |
| 4488 | if (ret) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4489 | mlog_errno(ret); |
| 4490 | goto out; |
| 4491 | } |
| 4492 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4493 | ret = ocfs2_xattr_bucket_journal_access(handle, first, |
| 4494 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4495 | if (ret) { |
| 4496 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4497 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4498 | } |
| 4499 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4500 | while (end_blk != target_blk) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4501 | ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk, |
| 4502 | end_blk + blk_per_bucket, 0); |
| 4503 | if (ret) |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4504 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4505 | end_blk -= blk_per_bucket; |
| 4506 | } |
| 4507 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4508 | /* Move half of the xattr in target_blkno to the next bucket. */ |
| 4509 | ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk, |
| 4510 | target_blk + blk_per_bucket, NULL, 0); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4511 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4512 | le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1); |
| 4513 | ocfs2_xattr_bucket_journal_dirty(handle, first); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4514 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4515 | out: |
| 4516 | return ret; |
| 4517 | } |
| 4518 | |
| 4519 | /* |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4520 | * Add new xattr bucket in an extent record and adjust the buckets |
| 4521 | * accordingly. xb_bh is the ocfs2_xattr_block, and target is the |
| 4522 | * bucket we want to insert into. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4523 | * |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4524 | * In the easy case, we will move all the buckets after target down by |
| 4525 | * one. Half of target's xattrs will be moved to the next bucket. |
| 4526 | * |
| 4527 | * If current cluster is full, we'll allocate a new one. This may not |
| 4528 | * be contiguous. The underlying calls will make sure that there is |
| 4529 | * space for the insert, shifting buckets around if necessary. |
| 4530 | * 'target' may be moved by those calls. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4531 | */ |
| 4532 | static int ocfs2_add_new_xattr_bucket(struct inode *inode, |
| 4533 | struct buffer_head *xb_bh, |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4534 | struct ocfs2_xattr_bucket *target, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4535 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4536 | { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4537 | struct ocfs2_xattr_block *xb = |
| 4538 | (struct ocfs2_xattr_block *)xb_bh->b_data; |
| 4539 | struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root; |
| 4540 | struct ocfs2_extent_list *el = &xb_root->xt_list; |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4541 | u32 name_hash = |
| 4542 | le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash); |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4543 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4544 | int ret, num_buckets, extend = 1; |
| 4545 | u64 p_blkno; |
| 4546 | u32 e_cpos, num_clusters; |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4547 | /* The bucket at the front of the extent */ |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4548 | struct ocfs2_xattr_bucket *first; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4549 | |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4550 | mlog(0, "Add new xattr bucket starting from %llu\n", |
| 4551 | (unsigned long long)bucket_blkno(target)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4552 | |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4553 | /* The first bucket of the original extent */ |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4554 | first = ocfs2_xattr_bucket_new(inode); |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 4555 | if (!first) { |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4556 | ret = -ENOMEM; |
| 4557 | mlog_errno(ret); |
| 4558 | goto out; |
| 4559 | } |
| 4560 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4561 | ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos, |
| 4562 | &num_clusters, el); |
| 4563 | if (ret) { |
| 4564 | mlog_errno(ret); |
| 4565 | goto out; |
| 4566 | } |
| 4567 | |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4568 | ret = ocfs2_read_xattr_bucket(first, p_blkno); |
| 4569 | if (ret) { |
| 4570 | mlog_errno(ret); |
| 4571 | goto out; |
| 4572 | } |
| 4573 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4574 | num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters; |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4575 | if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) { |
| 4576 | /* |
| 4577 | * This can move first+target if the target bucket moves |
| 4578 | * to the new extent. |
| 4579 | */ |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4580 | ret = ocfs2_add_new_xattr_cluster(inode, |
| 4581 | xb_bh, |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4582 | first, |
| 4583 | target, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4584 | &num_clusters, |
| 4585 | e_cpos, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4586 | &extend, |
| 4587 | ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4588 | if (ret) { |
| 4589 | mlog_errno(ret); |
| 4590 | goto out; |
| 4591 | } |
| 4592 | } |
| 4593 | |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4594 | if (extend) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4595 | ret = ocfs2_extend_xattr_bucket(inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4596 | ctxt->handle, |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4597 | first, |
| 4598 | bucket_blkno(target), |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4599 | num_clusters); |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4600 | if (ret) |
| 4601 | mlog_errno(ret); |
| 4602 | } |
| 4603 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4604 | out: |
Joel Becker | 92de109 | 2008-11-25 17:06:40 -0800 | [diff] [blame] | 4605 | ocfs2_xattr_bucket_free(first); |
Joel Becker | ed29c0c | 2008-11-26 15:08:44 -0800 | [diff] [blame] | 4606 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4607 | return ret; |
| 4608 | } |
| 4609 | |
| 4610 | static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode, |
| 4611 | struct ocfs2_xattr_bucket *bucket, |
| 4612 | int offs) |
| 4613 | { |
| 4614 | int block_off = offs >> inode->i_sb->s_blocksize_bits; |
| 4615 | |
| 4616 | offs = offs % inode->i_sb->s_blocksize; |
Joel Becker | 51def39 | 2008-10-24 16:57:21 -0700 | [diff] [blame] | 4617 | return bucket_block(bucket, block_off) + offs; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4618 | } |
| 4619 | |
| 4620 | /* |
| 4621 | * Handle the normal xattr set, including replace, delete and new. |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4622 | * |
| 4623 | * Note: "local" indicates the real data's locality. So we can't |
| 4624 | * just its bucket locality by its length. |
| 4625 | */ |
| 4626 | static void ocfs2_xattr_set_entry_normal(struct inode *inode, |
| 4627 | struct ocfs2_xattr_info *xi, |
| 4628 | struct ocfs2_xattr_search *xs, |
| 4629 | u32 name_hash, |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 4630 | int local) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4631 | { |
| 4632 | struct ocfs2_xattr_entry *last, *xe; |
| 4633 | int name_len = strlen(xi->name); |
| 4634 | struct ocfs2_xattr_header *xh = xs->header; |
| 4635 | u16 count = le16_to_cpu(xh->xh_count), start; |
| 4636 | size_t blocksize = inode->i_sb->s_blocksize; |
| 4637 | char *val; |
| 4638 | size_t offs, size, new_size; |
| 4639 | |
| 4640 | last = &xh->xh_entries[count]; |
| 4641 | if (!xs->not_found) { |
| 4642 | xe = xs->here; |
| 4643 | offs = le16_to_cpu(xe->xe_name_offset); |
| 4644 | if (ocfs2_xattr_is_local(xe)) |
| 4645 | size = OCFS2_XATTR_SIZE(name_len) + |
| 4646 | OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); |
| 4647 | else |
| 4648 | size = OCFS2_XATTR_SIZE(name_len) + |
| 4649 | OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE); |
| 4650 | |
| 4651 | /* |
| 4652 | * If the new value will be stored outside, xi->value has been |
| 4653 | * initalized as an empty ocfs2_xattr_value_root, and the same |
| 4654 | * goes with xi->value_len, so we can set new_size safely here. |
| 4655 | * See ocfs2_xattr_set_in_bucket. |
| 4656 | */ |
| 4657 | new_size = OCFS2_XATTR_SIZE(name_len) + |
| 4658 | OCFS2_XATTR_SIZE(xi->value_len); |
| 4659 | |
| 4660 | le16_add_cpu(&xh->xh_name_value_len, -size); |
| 4661 | if (xi->value) { |
| 4662 | if (new_size > size) |
| 4663 | goto set_new_name_value; |
| 4664 | |
| 4665 | /* Now replace the old value with new one. */ |
| 4666 | if (local) |
| 4667 | xe->xe_value_size = cpu_to_le64(xi->value_len); |
| 4668 | else |
| 4669 | xe->xe_value_size = 0; |
| 4670 | |
| 4671 | val = ocfs2_xattr_bucket_get_val(inode, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4672 | xs->bucket, offs); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4673 | memset(val + OCFS2_XATTR_SIZE(name_len), 0, |
| 4674 | size - OCFS2_XATTR_SIZE(name_len)); |
| 4675 | if (OCFS2_XATTR_SIZE(xi->value_len) > 0) |
| 4676 | memcpy(val + OCFS2_XATTR_SIZE(name_len), |
| 4677 | xi->value, xi->value_len); |
| 4678 | |
| 4679 | le16_add_cpu(&xh->xh_name_value_len, new_size); |
| 4680 | ocfs2_xattr_set_local(xe, local); |
| 4681 | return; |
| 4682 | } else { |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 4683 | /* |
| 4684 | * Remove the old entry if there is more than one. |
| 4685 | * We don't remove the last entry so that we can |
| 4686 | * use it to indicate the hash value of the empty |
| 4687 | * bucket. |
| 4688 | */ |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4689 | last -= 1; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4690 | le16_add_cpu(&xh->xh_count, -1); |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 4691 | if (xh->xh_count) { |
| 4692 | memmove(xe, xe + 1, |
| 4693 | (void *)last - (void *)xe); |
| 4694 | memset(last, 0, |
| 4695 | sizeof(struct ocfs2_xattr_entry)); |
| 4696 | } else |
| 4697 | xh->xh_free_start = |
| 4698 | cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE); |
| 4699 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4700 | return; |
| 4701 | } |
| 4702 | } else { |
| 4703 | /* find a new entry for insert. */ |
| 4704 | int low = 0, high = count - 1, tmp; |
| 4705 | struct ocfs2_xattr_entry *tmp_xe; |
| 4706 | |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 4707 | while (low <= high && count) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4708 | tmp = (low + high) / 2; |
| 4709 | tmp_xe = &xh->xh_entries[tmp]; |
| 4710 | |
| 4711 | if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash)) |
| 4712 | low = tmp + 1; |
| 4713 | else if (name_hash < |
| 4714 | le32_to_cpu(tmp_xe->xe_name_hash)) |
| 4715 | high = tmp - 1; |
Tao Ma | 06b240d | 2008-09-19 22:16:34 +0800 | [diff] [blame] | 4716 | else { |
| 4717 | low = tmp; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4718 | break; |
Tao Ma | 06b240d | 2008-09-19 22:16:34 +0800 | [diff] [blame] | 4719 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4720 | } |
| 4721 | |
| 4722 | xe = &xh->xh_entries[low]; |
| 4723 | if (low != count) |
| 4724 | memmove(xe + 1, xe, (void *)last - (void *)xe); |
| 4725 | |
| 4726 | le16_add_cpu(&xh->xh_count, 1); |
| 4727 | memset(xe, 0, sizeof(struct ocfs2_xattr_entry)); |
| 4728 | xe->xe_name_hash = cpu_to_le32(name_hash); |
| 4729 | xe->xe_name_len = name_len; |
| 4730 | ocfs2_xattr_set_type(xe, xi->name_index); |
| 4731 | } |
| 4732 | |
| 4733 | set_new_name_value: |
| 4734 | /* Insert the new name+value. */ |
| 4735 | size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len); |
| 4736 | |
| 4737 | /* |
| 4738 | * We must make sure that the name/value pair |
| 4739 | * exists in the same block. |
| 4740 | */ |
| 4741 | offs = le16_to_cpu(xh->xh_free_start); |
| 4742 | start = offs - size; |
| 4743 | |
| 4744 | if (start >> inode->i_sb->s_blocksize_bits != |
| 4745 | (offs - 1) >> inode->i_sb->s_blocksize_bits) { |
| 4746 | offs = offs - offs % blocksize; |
| 4747 | xh->xh_free_start = cpu_to_le16(offs); |
| 4748 | } |
| 4749 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4750 | val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4751 | xe->xe_name_offset = cpu_to_le16(offs - size); |
| 4752 | |
| 4753 | memset(val, 0, size); |
| 4754 | memcpy(val, xi->name, name_len); |
| 4755 | memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len); |
| 4756 | |
| 4757 | xe->xe_value_size = cpu_to_le64(xi->value_len); |
| 4758 | ocfs2_xattr_set_local(xe, local); |
| 4759 | xs->here = xe; |
| 4760 | le16_add_cpu(&xh->xh_free_start, -size); |
| 4761 | le16_add_cpu(&xh->xh_name_value_len, size); |
| 4762 | |
| 4763 | return; |
| 4764 | } |
| 4765 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4766 | /* |
| 4767 | * Set the xattr entry in the specified bucket. |
| 4768 | * The bucket is indicated by xs->bucket and it should have the enough |
| 4769 | * space for the xattr insertion. |
| 4770 | */ |
| 4771 | static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4772 | handle_t *handle, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4773 | struct ocfs2_xattr_info *xi, |
| 4774 | struct ocfs2_xattr_search *xs, |
| 4775 | u32 name_hash, |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 4776 | int local) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4777 | { |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 4778 | int ret; |
Joel Becker | 02dbf38 | 2008-10-27 18:07:45 -0700 | [diff] [blame] | 4779 | u64 blkno; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4780 | |
Mark Fasheh | ff1ec20 | 2008-08-19 10:54:29 -0700 | [diff] [blame] | 4781 | mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n", |
| 4782 | (unsigned long)xi->value_len, xi->name_index, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4783 | (unsigned long long)bucket_blkno(xs->bucket)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4784 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4785 | if (!xs->bucket->bu_bhs[1]) { |
Joel Becker | 02dbf38 | 2008-10-27 18:07:45 -0700 | [diff] [blame] | 4786 | blkno = bucket_blkno(xs->bucket); |
| 4787 | ocfs2_xattr_bucket_relse(xs->bucket); |
| 4788 | ret = ocfs2_read_xattr_bucket(xs->bucket, blkno); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4789 | if (ret) { |
| 4790 | mlog_errno(ret); |
| 4791 | goto out; |
| 4792 | } |
| 4793 | } |
| 4794 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4795 | ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 4796 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 4797 | if (ret < 0) { |
| 4798 | mlog_errno(ret); |
| 4799 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4800 | } |
| 4801 | |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 4802 | ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local); |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4803 | ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4804 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4805 | out: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4806 | return ret; |
| 4807 | } |
| 4808 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4809 | /* |
| 4810 | * Truncate the specified xe_off entry in xattr bucket. |
| 4811 | * bucket is indicated by header_bh and len is the new length. |
| 4812 | * Both the ocfs2_xattr_value_root and the entry will be updated here. |
| 4813 | * |
| 4814 | * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed. |
| 4815 | */ |
| 4816 | static int ocfs2_xattr_bucket_value_truncate(struct inode *inode, |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 4817 | struct ocfs2_xattr_bucket *bucket, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4818 | int xe_off, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4819 | int len, |
| 4820 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4821 | { |
| 4822 | int ret, offset; |
| 4823 | u64 value_blk; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4824 | struct ocfs2_xattr_entry *xe; |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 4825 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4826 | size_t blocksize = inode->i_sb->s_blocksize; |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 4827 | struct ocfs2_xattr_value_buf vb = { |
| 4828 | .vb_access = ocfs2_journal_access, |
| 4829 | }; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4830 | |
| 4831 | xe = &xh->xh_entries[xe_off]; |
| 4832 | |
| 4833 | BUG_ON(!xe || ocfs2_xattr_is_local(xe)); |
| 4834 | |
| 4835 | offset = le16_to_cpu(xe->xe_name_offset) + |
| 4836 | OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 4837 | |
| 4838 | value_blk = offset / blocksize; |
| 4839 | |
| 4840 | /* We don't allow ocfs2_xattr_value to be stored in different block. */ |
| 4841 | BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4842 | |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 4843 | vb.vb_bh = bucket->bu_bhs[value_blk]; |
| 4844 | BUG_ON(!vb.vb_bh); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4845 | |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 4846 | vb.vb_xv = (struct ocfs2_xattr_value_root *) |
| 4847 | (vb.vb_bh->b_data + offset % blocksize); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4848 | |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 4849 | /* |
| 4850 | * From here on out we have to dirty the bucket. The generic |
| 4851 | * value calls only modify one of the bucket's bhs, but we need |
| 4852 | * to send the bucket at once. So if they error, they *could* have |
| 4853 | * modified something. We have to assume they did, and dirty |
| 4854 | * the whole bucket. This leaves us in a consistent state. |
| 4855 | */ |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4856 | mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n", |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 4857 | xe_off, (unsigned long long)bucket_blkno(bucket), len); |
Joel Becker | b3e5d37 | 2008-12-09 15:01:04 -0800 | [diff] [blame] | 4858 | ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4859 | if (ret) { |
| 4860 | mlog_errno(ret); |
Tao Ma | 554e7f9 | 2009-01-08 08:21:43 +0800 | [diff] [blame] | 4861 | goto out; |
| 4862 | } |
| 4863 | |
| 4864 | ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket, |
| 4865 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 4866 | if (ret) { |
| 4867 | mlog_errno(ret); |
| 4868 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4869 | } |
| 4870 | |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 4871 | xe->xe_value_size = cpu_to_le64(len); |
| 4872 | |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 4873 | ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4874 | |
| 4875 | out: |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4876 | return ret; |
| 4877 | } |
| 4878 | |
| 4879 | static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4880 | struct ocfs2_xattr_search *xs, |
| 4881 | int len, |
| 4882 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4883 | { |
| 4884 | int ret, offset; |
| 4885 | struct ocfs2_xattr_entry *xe = xs->here; |
| 4886 | struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base; |
| 4887 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 4888 | 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] | 4889 | |
| 4890 | offset = xe - xh->xh_entries; |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 4891 | ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 4892 | offset, len, ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4893 | if (ret) |
| 4894 | mlog_errno(ret); |
| 4895 | |
| 4896 | return ret; |
| 4897 | } |
| 4898 | |
| 4899 | static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 4900 | handle_t *handle, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4901 | struct ocfs2_xattr_search *xs, |
| 4902 | char *val, |
| 4903 | int value_len) |
| 4904 | { |
Tao Ma | 712e53e | 2009-03-12 08:37:34 +0800 | [diff] [blame] | 4905 | int ret, offset, block_off; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4906 | struct ocfs2_xattr_value_root *xv; |
| 4907 | struct ocfs2_xattr_entry *xe = xs->here; |
Tao Ma | 712e53e | 2009-03-12 08:37:34 +0800 | [diff] [blame] | 4908 | struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket); |
| 4909 | void *base; |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 4910 | struct ocfs2_xattr_value_buf vb = { |
| 4911 | .vb_access = ocfs2_journal_access, |
| 4912 | }; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4913 | |
| 4914 | BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe)); |
| 4915 | |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 4916 | ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, xh, |
Tao Ma | 712e53e | 2009-03-12 08:37:34 +0800 | [diff] [blame] | 4917 | xe - xh->xh_entries, |
| 4918 | &block_off, |
| 4919 | &offset); |
| 4920 | if (ret) { |
| 4921 | mlog_errno(ret); |
| 4922 | goto out; |
| 4923 | } |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4924 | |
Tao Ma | 712e53e | 2009-03-12 08:37:34 +0800 | [diff] [blame] | 4925 | base = bucket_block(xs->bucket, block_off); |
| 4926 | xv = (struct ocfs2_xattr_value_root *)(base + offset + |
| 4927 | OCFS2_XATTR_SIZE(xe->xe_name_len)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4928 | |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 4929 | vb.vb_xv = xv; |
| 4930 | vb.vb_bh = xs->bucket->bu_bhs[block_off]; |
Tao Ma | 712e53e | 2009-03-12 08:37:34 +0800 | [diff] [blame] | 4931 | ret = __ocfs2_xattr_set_value_outside(inode, handle, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 4932 | &vb, val, value_len); |
Tao Ma | 712e53e | 2009-03-12 08:37:34 +0800 | [diff] [blame] | 4933 | if (ret) |
| 4934 | mlog_errno(ret); |
| 4935 | out: |
| 4936 | return ret; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4937 | } |
| 4938 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4939 | static int ocfs2_rm_xattr_cluster(struct inode *inode, |
| 4940 | struct buffer_head *root_bh, |
| 4941 | u64 blkno, |
| 4942 | u32 cpos, |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame^] | 4943 | u32 len, |
| 4944 | void *para) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4945 | { |
| 4946 | int ret; |
| 4947 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 4948 | struct inode *tl_inode = osb->osb_tl_inode; |
| 4949 | handle_t *handle; |
| 4950 | struct ocfs2_xattr_block *xb = |
| 4951 | (struct ocfs2_xattr_block *)root_bh->b_data; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4952 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 4953 | struct ocfs2_cached_dealloc_ctxt dealloc; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4954 | struct ocfs2_extent_tree et; |
| 4955 | |
Tao Ma | 47bca49 | 2009-08-18 11:43:42 +0800 | [diff] [blame^] | 4956 | ret = ocfs2_iterate_xattr_buckets(inode, blkno, len, |
| 4957 | ocfs2_delete_xattr_in_bucket, NULL); |
| 4958 | if (ret) { |
| 4959 | mlog_errno(ret); |
| 4960 | return ret; |
| 4961 | } |
| 4962 | |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 4963 | ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4964 | |
| 4965 | ocfs2_init_dealloc_ctxt(&dealloc); |
| 4966 | |
| 4967 | mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n", |
| 4968 | cpos, len, (unsigned long long)blkno); |
| 4969 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 4970 | ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno, |
| 4971 | len); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4972 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 4973 | ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4974 | if (ret) { |
| 4975 | mlog_errno(ret); |
| 4976 | return ret; |
| 4977 | } |
| 4978 | |
| 4979 | mutex_lock(&tl_inode->i_mutex); |
| 4980 | |
| 4981 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 4982 | ret = __ocfs2_flush_truncate_log(osb); |
| 4983 | if (ret < 0) { |
| 4984 | mlog_errno(ret); |
| 4985 | goto out; |
| 4986 | } |
| 4987 | } |
| 4988 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 4989 | handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb)); |
Tao Ma | d326479 | 2008-10-24 07:57:28 +0800 | [diff] [blame] | 4990 | if (IS_ERR(handle)) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4991 | ret = -ENOMEM; |
| 4992 | mlog_errno(ret); |
| 4993 | goto out; |
| 4994 | } |
| 4995 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 4996 | ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh, |
Joel Becker | 8400897 | 2008-12-09 16:11:49 -0800 | [diff] [blame] | 4997 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 4998 | if (ret) { |
| 4999 | mlog_errno(ret); |
| 5000 | goto out_commit; |
| 5001 | } |
| 5002 | |
Joel Becker | dbdcf6a | 2009-02-13 03:41:26 -0800 | [diff] [blame] | 5003 | ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 5004 | &dealloc); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5005 | if (ret) { |
| 5006 | mlog_errno(ret); |
| 5007 | goto out_commit; |
| 5008 | } |
| 5009 | |
| 5010 | le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len); |
| 5011 | |
| 5012 | ret = ocfs2_journal_dirty(handle, root_bh); |
| 5013 | if (ret) { |
| 5014 | mlog_errno(ret); |
| 5015 | goto out_commit; |
| 5016 | } |
| 5017 | |
| 5018 | ret = ocfs2_truncate_log_append(osb, handle, blkno, len); |
| 5019 | if (ret) |
| 5020 | mlog_errno(ret); |
| 5021 | |
| 5022 | out_commit: |
| 5023 | ocfs2_commit_trans(osb, handle); |
| 5024 | out: |
| 5025 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 5026 | |
| 5027 | mutex_unlock(&tl_inode->i_mutex); |
| 5028 | |
| 5029 | if (meta_ac) |
| 5030 | ocfs2_free_alloc_context(meta_ac); |
| 5031 | |
| 5032 | ocfs2_run_deallocs(osb, &dealloc); |
| 5033 | |
| 5034 | return ret; |
| 5035 | } |
| 5036 | |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5037 | static void ocfs2_xattr_bucket_remove_xs(struct inode *inode, |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5038 | handle_t *handle, |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5039 | struct ocfs2_xattr_search *xs) |
| 5040 | { |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5041 | struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5042 | struct ocfs2_xattr_entry *last = &xh->xh_entries[ |
| 5043 | le16_to_cpu(xh->xh_count) - 1]; |
| 5044 | int ret = 0; |
| 5045 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5046 | ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket, |
Joel Becker | 1224be0 | 2008-10-24 18:47:33 -0700 | [diff] [blame] | 5047 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5048 | if (ret) { |
| 5049 | mlog_errno(ret); |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5050 | return; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5051 | } |
| 5052 | |
| 5053 | /* Remove the old entry. */ |
| 5054 | memmove(xs->here, xs->here + 1, |
| 5055 | (void *)last - (void *)xs->here); |
| 5056 | memset(last, 0, sizeof(struct ocfs2_xattr_entry)); |
| 5057 | le16_add_cpu(&xh->xh_count, -1); |
| 5058 | |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5059 | ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5060 | } |
| 5061 | |
| 5062 | /* |
| 5063 | * Set the xattr name/value in the bucket specified in xs. |
| 5064 | * |
| 5065 | * As the new value in xi may be stored in the bucket or in an outside cluster, |
| 5066 | * we divide the whole process into 3 steps: |
| 5067 | * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket) |
| 5068 | * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs) |
| 5069 | * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside) |
| 5070 | * 4. If the clusters for the new outside value can't be allocated, we need |
| 5071 | * to free the xattr we allocated in set. |
| 5072 | */ |
| 5073 | static int ocfs2_xattr_set_in_bucket(struct inode *inode, |
| 5074 | struct ocfs2_xattr_info *xi, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5075 | struct ocfs2_xattr_search *xs, |
| 5076 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5077 | { |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 5078 | int ret, local = 1; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5079 | size_t value_len; |
| 5080 | char *val = (char *)xi->value; |
| 5081 | struct ocfs2_xattr_entry *xe = xs->here; |
Tao Ma | 2057e5c | 2008-10-09 23:06:13 +0800 | [diff] [blame] | 5082 | u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name, |
| 5083 | strlen(xi->name)); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5084 | |
| 5085 | if (!xs->not_found && !ocfs2_xattr_is_local(xe)) { |
| 5086 | /* |
| 5087 | * We need to truncate the xattr storage first. |
| 5088 | * |
| 5089 | * If both the old and new value are stored to |
| 5090 | * outside block, we only need to truncate |
| 5091 | * the storage and then set the value outside. |
| 5092 | * |
| 5093 | * If the new value should be stored within block, |
| 5094 | * we should free all the outside block first and |
| 5095 | * the modification to the xattr block will be done |
| 5096 | * by following steps. |
| 5097 | */ |
| 5098 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) |
| 5099 | value_len = xi->value_len; |
| 5100 | else |
| 5101 | value_len = 0; |
| 5102 | |
| 5103 | ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5104 | value_len, |
| 5105 | ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5106 | if (ret) |
| 5107 | goto out; |
| 5108 | |
| 5109 | if (value_len) |
| 5110 | goto set_value_outside; |
| 5111 | } |
| 5112 | |
| 5113 | value_len = xi->value_len; |
| 5114 | /* So we have to handle the inside block change now. */ |
| 5115 | if (value_len > OCFS2_XATTR_INLINE_SIZE) { |
| 5116 | /* |
| 5117 | * If the new value will be stored outside of block, |
| 5118 | * initalize a new empty value root and insert it first. |
| 5119 | */ |
| 5120 | local = 0; |
| 5121 | xi->value = &def_xv; |
| 5122 | xi->value_len = OCFS2_XATTR_ROOT_SIZE; |
| 5123 | } |
| 5124 | |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5125 | ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs, |
| 5126 | name_hash, local); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5127 | if (ret) { |
| 5128 | mlog_errno(ret); |
| 5129 | goto out; |
| 5130 | } |
| 5131 | |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 5132 | if (value_len <= OCFS2_XATTR_INLINE_SIZE) |
| 5133 | goto out; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5134 | |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 5135 | /* allocate the space now for the outside block storage. */ |
| 5136 | ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5137 | value_len, ctxt); |
Tao Ma | 5a09561 | 2008-09-19 22:17:41 +0800 | [diff] [blame] | 5138 | if (ret) { |
| 5139 | mlog_errno(ret); |
| 5140 | |
| 5141 | if (xs->not_found) { |
| 5142 | /* |
| 5143 | * We can't allocate enough clusters for outside |
| 5144 | * storage and we have allocated xattr already, |
| 5145 | * so need to remove it. |
| 5146 | */ |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5147 | ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs); |
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 | goto out; |
| 5150 | } |
| 5151 | |
| 5152 | set_value_outside: |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5153 | ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle, |
| 5154 | xs, val, value_len); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5155 | out: |
| 5156 | return ret; |
| 5157 | } |
| 5158 | |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 5159 | /* |
| 5160 | * check whether the xattr bucket is filled up with the same hash value. |
| 5161 | * If we want to insert the xattr with the same hash, return -ENOSPC. |
| 5162 | * If we want to insert a xattr with different hash value, go ahead |
| 5163 | * and ocfs2_divide_xattr_bucket will handle this. |
| 5164 | */ |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5165 | static int ocfs2_check_xattr_bucket_collision(struct inode *inode, |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 5166 | struct ocfs2_xattr_bucket *bucket, |
| 5167 | const char *name) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5168 | { |
Joel Becker | 3e63294 | 2008-10-24 17:04:49 -0700 | [diff] [blame] | 5169 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 5170 | u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name)); |
| 5171 | |
| 5172 | if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash)) |
| 5173 | return 0; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5174 | |
| 5175 | if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash == |
| 5176 | xh->xh_entries[0].xe_name_hash) { |
| 5177 | mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, " |
| 5178 | "hash = %u\n", |
Joel Becker | 9c7759a | 2008-10-24 16:21:03 -0700 | [diff] [blame] | 5179 | (unsigned long long)bucket_blkno(bucket), |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5180 | le32_to_cpu(xh->xh_entries[0].xe_name_hash)); |
| 5181 | return -ENOSPC; |
| 5182 | } |
| 5183 | |
| 5184 | return 0; |
| 5185 | } |
| 5186 | |
| 5187 | static int ocfs2_xattr_set_entry_index_block(struct inode *inode, |
| 5188 | struct ocfs2_xattr_info *xi, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5189 | struct ocfs2_xattr_search *xs, |
| 5190 | struct ocfs2_xattr_set_ctxt *ctxt) |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5191 | { |
| 5192 | struct ocfs2_xattr_header *xh; |
| 5193 | struct ocfs2_xattr_entry *xe; |
| 5194 | u16 count, header_size, xh_free_start; |
Joel Becker | 6dde41d | 2008-10-24 17:16:48 -0700 | [diff] [blame] | 5195 | int free, max_free, need, old; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5196 | size_t value_size = 0, name_len = strlen(xi->name); |
| 5197 | size_t blocksize = inode->i_sb->s_blocksize; |
| 5198 | int ret, allocation = 0; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5199 | |
| 5200 | mlog_entry("Set xattr %s in xattr index block\n", xi->name); |
| 5201 | |
| 5202 | try_again: |
| 5203 | xh = xs->header; |
| 5204 | count = le16_to_cpu(xh->xh_count); |
| 5205 | xh_free_start = le16_to_cpu(xh->xh_free_start); |
| 5206 | header_size = sizeof(struct ocfs2_xattr_header) + |
| 5207 | count * sizeof(struct ocfs2_xattr_entry); |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 5208 | max_free = OCFS2_XATTR_BUCKET_SIZE - header_size - |
| 5209 | le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5210 | |
| 5211 | mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size " |
| 5212 | "of %u which exceed block size\n", |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5213 | (unsigned long long)bucket_blkno(xs->bucket), |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5214 | header_size); |
| 5215 | |
| 5216 | if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) |
| 5217 | value_size = OCFS2_XATTR_ROOT_SIZE; |
| 5218 | else if (xi->value) |
| 5219 | value_size = OCFS2_XATTR_SIZE(xi->value_len); |
| 5220 | |
| 5221 | if (xs->not_found) |
| 5222 | need = sizeof(struct ocfs2_xattr_entry) + |
| 5223 | OCFS2_XATTR_SIZE(name_len) + value_size; |
| 5224 | else { |
| 5225 | need = value_size + OCFS2_XATTR_SIZE(name_len); |
| 5226 | |
| 5227 | /* |
| 5228 | * We only replace the old value if the new length is smaller |
| 5229 | * than the old one. Otherwise we will allocate new space in the |
| 5230 | * bucket to store it. |
| 5231 | */ |
| 5232 | xe = xs->here; |
| 5233 | if (ocfs2_xattr_is_local(xe)) |
| 5234 | old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); |
| 5235 | else |
| 5236 | old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE); |
| 5237 | |
| 5238 | if (old >= value_size) |
| 5239 | need = 0; |
| 5240 | } |
| 5241 | |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 5242 | free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5243 | /* |
| 5244 | * We need to make sure the new name/value pair |
| 5245 | * can exist in the same block. |
| 5246 | */ |
| 5247 | if (xh_free_start % blocksize < need) |
| 5248 | free -= xh_free_start % blocksize; |
| 5249 | |
| 5250 | mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, " |
| 5251 | "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len =" |
| 5252 | " %u\n", xs->not_found, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5253 | (unsigned long long)bucket_blkno(xs->bucket), |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5254 | free, need, max_free, le16_to_cpu(xh->xh_free_start), |
| 5255 | le16_to_cpu(xh->xh_name_value_len)); |
| 5256 | |
Tao Ma | 976331d | 2008-11-12 08:26:57 +0800 | [diff] [blame] | 5257 | if (free < need || |
| 5258 | (xs->not_found && |
| 5259 | count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) { |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5260 | if (need <= max_free && |
| 5261 | count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) { |
| 5262 | /* |
| 5263 | * We can create the space by defragment. Since only the |
| 5264 | * name/value will be moved, the xe shouldn't be changed |
| 5265 | * in xs. |
| 5266 | */ |
Tao Ma | 85db90e | 2008-11-12 08:27:01 +0800 | [diff] [blame] | 5267 | ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle, |
| 5268 | xs->bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5269 | if (ret) { |
| 5270 | mlog_errno(ret); |
| 5271 | goto out; |
| 5272 | } |
| 5273 | |
| 5274 | xh_free_start = le16_to_cpu(xh->xh_free_start); |
Tiger Yang | 4442f51 | 2009-02-20 11:11:50 +0800 | [diff] [blame] | 5275 | free = xh_free_start - header_size |
| 5276 | - OCFS2_XATTR_HEADER_GAP; |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5277 | if (xh_free_start % blocksize < need) |
| 5278 | free -= xh_free_start % blocksize; |
| 5279 | |
| 5280 | if (free >= need) |
| 5281 | goto xattr_set; |
| 5282 | |
| 5283 | mlog(0, "Can't get enough space for xattr insert by " |
| 5284 | "defragment. Need %u bytes, but we have %d, so " |
| 5285 | "allocate new bucket for it.\n", need, free); |
| 5286 | } |
| 5287 | |
| 5288 | /* |
| 5289 | * We have to add new buckets or clusters and one |
| 5290 | * allocation should leave us enough space for insert. |
| 5291 | */ |
| 5292 | BUG_ON(allocation); |
| 5293 | |
| 5294 | /* |
| 5295 | * We do not allow for overlapping ranges between buckets. And |
| 5296 | * the maximum number of collisions we will allow for then is |
| 5297 | * one bucket's worth, so check it here whether we need to |
| 5298 | * add a new bucket for the insert. |
| 5299 | */ |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 5300 | ret = ocfs2_check_xattr_bucket_collision(inode, |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5301 | xs->bucket, |
Tao Ma | 80bcaf3 | 2008-10-27 06:06:24 +0800 | [diff] [blame] | 5302 | xi->name); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5303 | if (ret) { |
| 5304 | mlog_errno(ret); |
| 5305 | goto out; |
| 5306 | } |
| 5307 | |
| 5308 | ret = ocfs2_add_new_xattr_bucket(inode, |
| 5309 | xs->xattr_bh, |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 5310 | xs->bucket, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5311 | ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5312 | if (ret) { |
| 5313 | mlog_errno(ret); |
| 5314 | goto out; |
| 5315 | } |
| 5316 | |
Joel Becker | 91f2033 | 2008-11-26 15:25:41 -0800 | [diff] [blame] | 5317 | /* |
| 5318 | * ocfs2_add_new_xattr_bucket() will have updated |
| 5319 | * xs->bucket if it moved, but it will not have updated |
| 5320 | * any of the other search fields. Thus, we drop it and |
| 5321 | * re-search. Everything should be cached, so it'll be |
| 5322 | * quick. |
| 5323 | */ |
Joel Becker | ba93712 | 2008-10-24 19:13:20 -0700 | [diff] [blame] | 5324 | ocfs2_xattr_bucket_relse(xs->bucket); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5325 | ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh, |
| 5326 | xi->name_index, |
| 5327 | xi->name, xs); |
| 5328 | if (ret && ret != -ENODATA) |
| 5329 | goto out; |
| 5330 | xs->not_found = ret; |
| 5331 | allocation = 1; |
| 5332 | goto try_again; |
| 5333 | } |
| 5334 | |
| 5335 | xattr_set: |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5336 | ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt); |
Tao Ma | 0122559 | 2008-08-18 17:38:53 +0800 | [diff] [blame] | 5337 | out: |
| 5338 | mlog_exit(ret); |
| 5339 | return ret; |
| 5340 | } |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 5341 | |
| 5342 | static int ocfs2_delete_xattr_in_bucket(struct inode *inode, |
| 5343 | struct ocfs2_xattr_bucket *bucket, |
| 5344 | void *para) |
| 5345 | { |
| 5346 | int ret = 0; |
Joel Becker | 3e63294 | 2008-10-24 17:04:49 -0700 | [diff] [blame] | 5347 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 5348 | u16 i; |
| 5349 | struct ocfs2_xattr_entry *xe; |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5350 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5351 | struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,}; |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 5352 | int credits = ocfs2_remove_extent_credits(osb->sb) + |
| 5353 | ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
| 5354 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5355 | |
| 5356 | ocfs2_init_dealloc_ctxt(&ctxt.dealloc); |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 5357 | |
| 5358 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { |
| 5359 | xe = &xh->xh_entries[i]; |
| 5360 | if (ocfs2_xattr_is_local(xe)) |
| 5361 | continue; |
| 5362 | |
Tao Ma | 88c3b06 | 2008-12-11 08:54:11 +0800 | [diff] [blame] | 5363 | ctxt.handle = ocfs2_start_trans(osb, credits); |
| 5364 | if (IS_ERR(ctxt.handle)) { |
| 5365 | ret = PTR_ERR(ctxt.handle); |
| 5366 | mlog_errno(ret); |
| 5367 | break; |
| 5368 | } |
| 5369 | |
Joel Becker | 548b0f2 | 2008-11-24 19:32:13 -0800 | [diff] [blame] | 5370 | ret = ocfs2_xattr_bucket_value_truncate(inode, bucket, |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5371 | i, 0, &ctxt); |
Tao Ma | 88c3b06 | 2008-12-11 08:54:11 +0800 | [diff] [blame] | 5372 | |
| 5373 | ocfs2_commit_trans(osb, ctxt.handle); |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 5374 | if (ret) { |
| 5375 | mlog_errno(ret); |
| 5376 | break; |
| 5377 | } |
| 5378 | } |
| 5379 | |
Tao Ma | 78f30c3 | 2008-11-12 08:27:00 +0800 | [diff] [blame] | 5380 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 5381 | ocfs2_run_deallocs(osb, &ctxt.dealloc); |
Tao Ma | a394425 | 2008-08-18 17:38:54 +0800 | [diff] [blame] | 5382 | return ret; |
| 5383 | } |
| 5384 | |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 5385 | /* |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 5386 | * Whenever we modify a xattr value root in the bucket(e.g, CoW |
| 5387 | * or change the extent record flag), we need to recalculate |
| 5388 | * the metaecc for the whole bucket. So it is done here. |
| 5389 | * |
| 5390 | * Note: |
| 5391 | * We have to give the extra credits for the caller. |
| 5392 | */ |
| 5393 | static int ocfs2_xattr_bucket_post_refcount(struct inode *inode, |
| 5394 | handle_t *handle, |
| 5395 | void *para) |
| 5396 | { |
| 5397 | int ret; |
| 5398 | struct ocfs2_xattr_bucket *bucket = |
| 5399 | (struct ocfs2_xattr_bucket *)para; |
| 5400 | |
| 5401 | ret = ocfs2_xattr_bucket_journal_access(handle, bucket, |
| 5402 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 5403 | if (ret) { |
| 5404 | mlog_errno(ret); |
| 5405 | return ret; |
| 5406 | } |
| 5407 | |
| 5408 | ocfs2_xattr_bucket_journal_dirty(handle, bucket); |
| 5409 | |
| 5410 | return 0; |
| 5411 | } |
| 5412 | |
| 5413 | /* |
| 5414 | * Special action we need if the xattr value is refcounted. |
| 5415 | * |
| 5416 | * 1. If the xattr is refcounted, lock the tree. |
| 5417 | * 2. CoW the xattr if we are setting the new value and the value |
| 5418 | * will be stored outside. |
| 5419 | * 3. In other case, decrease_refcount will work for us, so just |
| 5420 | * lock the refcount tree, calculate the meta and credits is OK. |
| 5421 | * |
| 5422 | * We have to do CoW before ocfs2_init_xattr_set_ctxt since |
| 5423 | * currently CoW is a completed transaction, while this function |
| 5424 | * will also lock the allocators and let us deadlock. So we will |
| 5425 | * CoW the whole xattr value. |
| 5426 | */ |
| 5427 | static int ocfs2_prepare_refcount_xattr(struct inode *inode, |
| 5428 | struct ocfs2_dinode *di, |
| 5429 | struct ocfs2_xattr_info *xi, |
| 5430 | struct ocfs2_xattr_search *xis, |
| 5431 | struct ocfs2_xattr_search *xbs, |
| 5432 | struct ocfs2_refcount_tree **ref_tree, |
| 5433 | int *meta_add, |
| 5434 | int *credits) |
| 5435 | { |
| 5436 | int ret = 0; |
| 5437 | struct ocfs2_xattr_block *xb; |
| 5438 | struct ocfs2_xattr_entry *xe; |
| 5439 | char *base; |
| 5440 | u32 p_cluster, num_clusters; |
| 5441 | unsigned int ext_flags; |
| 5442 | int name_offset, name_len; |
| 5443 | struct ocfs2_xattr_value_buf vb; |
| 5444 | struct ocfs2_xattr_bucket *bucket = NULL; |
| 5445 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5446 | struct ocfs2_post_refcount refcount; |
| 5447 | struct ocfs2_post_refcount *p = NULL; |
| 5448 | struct buffer_head *ref_root_bh = NULL; |
| 5449 | |
| 5450 | if (!xis->not_found) { |
| 5451 | xe = xis->here; |
| 5452 | name_offset = le16_to_cpu(xe->xe_name_offset); |
| 5453 | name_len = OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 5454 | base = xis->base; |
| 5455 | vb.vb_bh = xis->inode_bh; |
| 5456 | vb.vb_access = ocfs2_journal_access_di; |
| 5457 | } else { |
| 5458 | int i, block_off = 0; |
| 5459 | xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data; |
| 5460 | xe = xbs->here; |
| 5461 | name_offset = le16_to_cpu(xe->xe_name_offset); |
| 5462 | name_len = OCFS2_XATTR_SIZE(xe->xe_name_len); |
| 5463 | i = xbs->here - xbs->header->xh_entries; |
| 5464 | |
| 5465 | if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) { |
Tao Ma | fd68a89 | 2009-08-18 11:43:21 +0800 | [diff] [blame] | 5466 | ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, |
Tao Ma | 492a8a3 | 2009-08-18 11:43:17 +0800 | [diff] [blame] | 5467 | bucket_xh(xbs->bucket), |
| 5468 | i, &block_off, |
| 5469 | &name_offset); |
| 5470 | if (ret) { |
| 5471 | mlog_errno(ret); |
| 5472 | goto out; |
| 5473 | } |
| 5474 | base = bucket_block(xbs->bucket, block_off); |
| 5475 | vb.vb_bh = xbs->bucket->bu_bhs[block_off]; |
| 5476 | vb.vb_access = ocfs2_journal_access; |
| 5477 | |
| 5478 | if (ocfs2_meta_ecc(osb)) { |
| 5479 | /*create parameters for ocfs2_post_refcount. */ |
| 5480 | bucket = xbs->bucket; |
| 5481 | refcount.credits = bucket->bu_blocks; |
| 5482 | refcount.para = bucket; |
| 5483 | refcount.func = |
| 5484 | ocfs2_xattr_bucket_post_refcount; |
| 5485 | p = &refcount; |
| 5486 | } |
| 5487 | } else { |
| 5488 | base = xbs->base; |
| 5489 | vb.vb_bh = xbs->xattr_bh; |
| 5490 | vb.vb_access = ocfs2_journal_access_xb; |
| 5491 | } |
| 5492 | } |
| 5493 | |
| 5494 | if (ocfs2_xattr_is_local(xe)) |
| 5495 | goto out; |
| 5496 | |
| 5497 | vb.vb_xv = (struct ocfs2_xattr_value_root *) |
| 5498 | (base + name_offset + name_len); |
| 5499 | |
| 5500 | ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster, |
| 5501 | &num_clusters, &vb.vb_xv->xr_list, |
| 5502 | &ext_flags); |
| 5503 | if (ret) { |
| 5504 | mlog_errno(ret); |
| 5505 | goto out; |
| 5506 | } |
| 5507 | |
| 5508 | /* |
| 5509 | * We just need to check the 1st extent record, since we always |
| 5510 | * CoW the whole xattr. So there shouldn't be a xattr with |
| 5511 | * some REFCOUNT extent recs after the 1st one. |
| 5512 | */ |
| 5513 | if (!(ext_flags & OCFS2_EXT_REFCOUNTED)) |
| 5514 | goto out; |
| 5515 | |
| 5516 | ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc), |
| 5517 | 1, ref_tree, &ref_root_bh); |
| 5518 | if (ret) { |
| 5519 | mlog_errno(ret); |
| 5520 | goto out; |
| 5521 | } |
| 5522 | |
| 5523 | /* |
| 5524 | * If we are deleting the xattr or the new size will be stored inside, |
| 5525 | * cool, leave it there, the xattr truncate process will remove them |
| 5526 | * for us(it still needs the refcount tree lock and the meta, credits). |
| 5527 | * And the worse case is that every cluster truncate will split the |
| 5528 | * refcount tree, and make the original extent become 3. So we will need |
| 5529 | * 2 * cluster more extent recs at most. |
| 5530 | */ |
| 5531 | if (!xi->value || xi->value_len <= OCFS2_XATTR_INLINE_SIZE) { |
| 5532 | |
| 5533 | ret = ocfs2_refcounted_xattr_delete_need(inode, |
| 5534 | &(*ref_tree)->rf_ci, |
| 5535 | ref_root_bh, vb.vb_xv, |
| 5536 | meta_add, credits); |
| 5537 | if (ret) |
| 5538 | mlog_errno(ret); |
| 5539 | goto out; |
| 5540 | } |
| 5541 | |
| 5542 | ret = ocfs2_refcount_cow_xattr(inode, di, &vb, |
| 5543 | *ref_tree, ref_root_bh, 0, |
| 5544 | le32_to_cpu(vb.vb_xv->xr_clusters), p); |
| 5545 | if (ret) |
| 5546 | mlog_errno(ret); |
| 5547 | |
| 5548 | out: |
| 5549 | brelse(ref_root_bh); |
| 5550 | return ret; |
| 5551 | } |
| 5552 | |
| 5553 | /* |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 5554 | * 'security' attributes support |
| 5555 | */ |
| 5556 | static size_t ocfs2_xattr_security_list(struct inode *inode, char *list, |
| 5557 | size_t list_size, const char *name, |
| 5558 | size_t name_len) |
| 5559 | { |
| 5560 | const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN; |
| 5561 | const size_t total_len = prefix_len + name_len + 1; |
| 5562 | |
| 5563 | if (list && total_len <= list_size) { |
| 5564 | memcpy(list, XATTR_SECURITY_PREFIX, prefix_len); |
| 5565 | memcpy(list + prefix_len, name, name_len); |
| 5566 | list[prefix_len + name_len] = '\0'; |
| 5567 | } |
| 5568 | return total_len; |
| 5569 | } |
| 5570 | |
| 5571 | static int ocfs2_xattr_security_get(struct inode *inode, const char *name, |
| 5572 | void *buffer, size_t size) |
| 5573 | { |
| 5574 | if (strcmp(name, "") == 0) |
| 5575 | return -EINVAL; |
| 5576 | return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, name, |
| 5577 | buffer, size); |
| 5578 | } |
| 5579 | |
| 5580 | static int ocfs2_xattr_security_set(struct inode *inode, const char *name, |
| 5581 | const void *value, size_t size, int flags) |
| 5582 | { |
| 5583 | if (strcmp(name, "") == 0) |
| 5584 | return -EINVAL; |
| 5585 | |
| 5586 | return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, name, value, |
| 5587 | size, flags); |
| 5588 | } |
| 5589 | |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 5590 | int ocfs2_init_security_get(struct inode *inode, |
| 5591 | struct inode *dir, |
| 5592 | struct ocfs2_security_xattr_info *si) |
| 5593 | { |
Tiger Yang | 38d59ef | 2008-12-17 10:22:56 +0800 | [diff] [blame] | 5594 | /* check whether ocfs2 support feature xattr */ |
| 5595 | if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb))) |
| 5596 | return -EOPNOTSUPP; |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 5597 | return security_inode_init_security(inode, dir, &si->name, &si->value, |
| 5598 | &si->value_len); |
| 5599 | } |
| 5600 | |
| 5601 | int ocfs2_init_security_set(handle_t *handle, |
| 5602 | struct inode *inode, |
| 5603 | struct buffer_head *di_bh, |
| 5604 | struct ocfs2_security_xattr_info *si, |
| 5605 | struct ocfs2_alloc_context *xattr_ac, |
| 5606 | struct ocfs2_alloc_context *data_ac) |
| 5607 | { |
| 5608 | return ocfs2_xattr_set_handle(handle, inode, di_bh, |
| 5609 | OCFS2_XATTR_INDEX_SECURITY, |
| 5610 | si->name, si->value, si->value_len, 0, |
| 5611 | xattr_ac, data_ac); |
| 5612 | } |
| 5613 | |
Tiger Yang | 923f7f3 | 2008-11-14 11:16:27 +0800 | [diff] [blame] | 5614 | struct xattr_handler ocfs2_xattr_security_handler = { |
| 5615 | .prefix = XATTR_SECURITY_PREFIX, |
| 5616 | .list = ocfs2_xattr_security_list, |
| 5617 | .get = ocfs2_xattr_security_get, |
| 5618 | .set = ocfs2_xattr_security_set, |
| 5619 | }; |
| 5620 | |
| 5621 | /* |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 5622 | * 'trusted' attributes support |
| 5623 | */ |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 5624 | static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list, |
| 5625 | size_t list_size, const char *name, |
| 5626 | size_t name_len) |
| 5627 | { |
Tiger Yang | ceb1eba | 2008-10-23 16:34:13 +0800 | [diff] [blame] | 5628 | const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN; |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 5629 | const size_t total_len = prefix_len + name_len + 1; |
| 5630 | |
| 5631 | if (list && total_len <= list_size) { |
| 5632 | memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len); |
| 5633 | memcpy(list + prefix_len, name, name_len); |
| 5634 | list[prefix_len + name_len] = '\0'; |
| 5635 | } |
| 5636 | return total_len; |
| 5637 | } |
| 5638 | |
| 5639 | static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name, |
| 5640 | void *buffer, size_t size) |
| 5641 | { |
| 5642 | if (strcmp(name, "") == 0) |
| 5643 | return -EINVAL; |
| 5644 | return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name, |
| 5645 | buffer, size); |
| 5646 | } |
| 5647 | |
| 5648 | static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name, |
| 5649 | const void *value, size_t size, int flags) |
| 5650 | { |
| 5651 | if (strcmp(name, "") == 0) |
| 5652 | return -EINVAL; |
| 5653 | |
| 5654 | return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value, |
| 5655 | size, flags); |
| 5656 | } |
| 5657 | |
| 5658 | struct xattr_handler ocfs2_xattr_trusted_handler = { |
| 5659 | .prefix = XATTR_TRUSTED_PREFIX, |
| 5660 | .list = ocfs2_xattr_trusted_list, |
| 5661 | .get = ocfs2_xattr_trusted_get, |
| 5662 | .set = ocfs2_xattr_trusted_set, |
| 5663 | }; |
| 5664 | |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 5665 | /* |
| 5666 | * 'user' attributes support |
| 5667 | */ |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 5668 | static size_t ocfs2_xattr_user_list(struct inode *inode, char *list, |
| 5669 | size_t list_size, const char *name, |
| 5670 | size_t name_len) |
| 5671 | { |
Tiger Yang | ceb1eba | 2008-10-23 16:34:13 +0800 | [diff] [blame] | 5672 | const size_t prefix_len = XATTR_USER_PREFIX_LEN; |
Mark Fasheh | 99219ae | 2008-10-07 14:52:59 -0700 | [diff] [blame] | 5673 | const size_t total_len = prefix_len + name_len + 1; |
| 5674 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5675 | |
| 5676 | if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) |
| 5677 | return 0; |
| 5678 | |
| 5679 | if (list && total_len <= list_size) { |
| 5680 | memcpy(list, XATTR_USER_PREFIX, prefix_len); |
| 5681 | memcpy(list + prefix_len, name, name_len); |
| 5682 | list[prefix_len + name_len] = '\0'; |
| 5683 | } |
| 5684 | return total_len; |
| 5685 | } |
| 5686 | |
| 5687 | static int ocfs2_xattr_user_get(struct inode *inode, const char *name, |
| 5688 | void *buffer, size_t size) |
| 5689 | { |
| 5690 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5691 | |
| 5692 | if (strcmp(name, "") == 0) |
| 5693 | return -EINVAL; |
| 5694 | if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) |
| 5695 | return -EOPNOTSUPP; |
| 5696 | return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name, |
| 5697 | buffer, size); |
| 5698 | } |
| 5699 | |
| 5700 | static int ocfs2_xattr_user_set(struct inode *inode, const char *name, |
| 5701 | const void *value, size_t size, int flags) |
| 5702 | { |
| 5703 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 5704 | |
| 5705 | if (strcmp(name, "") == 0) |
| 5706 | return -EINVAL; |
| 5707 | if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) |
| 5708 | return -EOPNOTSUPP; |
| 5709 | |
| 5710 | return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value, |
| 5711 | size, flags); |
| 5712 | } |
| 5713 | |
| 5714 | struct xattr_handler ocfs2_xattr_user_handler = { |
| 5715 | .prefix = XATTR_USER_PREFIX, |
| 5716 | .list = ocfs2_xattr_user_list, |
| 5717 | .get = ocfs2_xattr_user_get, |
| 5718 | .set = ocfs2_xattr_user_set, |
| 5719 | }; |