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