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