1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * xattr.c 4 * 5 * Copyright (C) 2004, 2008 Oracle. All rights reserved. 6 * 7 * CREDITS: 8 * Lots of code in this file is copy from linux/fs/ext3/xattr.c. 9 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> 10 */ 11 12 #include <linux/capability.h> 13 #include <linux/fs.h> 14 #include <linux/types.h> 15 #include <linux/slab.h> 16 #include <linux/highmem.h> 17 #include <linux/pagemap.h> 18 #include <linux/uio.h> 19 #include <linux/sched.h> 20 #include <linux/splice.h> 21 #include <linux/mount.h> 22 #include <linux/writeback.h> 23 #include <linux/falloc.h> 24 #include <linux/sort.h> 25 #include <linux/init.h> 26 #include <linux/module.h> 27 #include <linux/string.h> 28 #include <linux/security.h> 29 30 #include <cluster/masklog.h> 31 32 #include "ocfs2.h" 33 #include "alloc.h" 34 #include "blockcheck.h" 35 #include "dlmglue.h" 36 #include "file.h" 37 #include "symlink.h" 38 #include "sysfile.h" 39 #include "inode.h" 40 #include "journal.h" 41 #include "ocfs2_fs.h" 42 #include "suballoc.h" 43 #include "uptodate.h" 44 #include "buffer_head_io.h" 45 #include "super.h" 46 #include "xattr.h" 47 #include "refcounttree.h" 48 #include "acl.h" 49 #include "ocfs2_trace.h" 50 51 struct ocfs2_xattr_def_value_root { 52 struct ocfs2_xattr_value_root xv; 53 struct ocfs2_extent_rec er; 54 }; 55 56 struct ocfs2_xattr_bucket { 57 /* The inode these xattrs are associated with */ 58 struct inode *bu_inode; 59 60 /* The actual buffers that make up the bucket */ 61 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET]; 62 63 /* How many blocks make up one bucket for this filesystem */ 64 int bu_blocks; 65 }; 66 67 struct ocfs2_xattr_set_ctxt { 68 handle_t *handle; 69 struct ocfs2_alloc_context *meta_ac; 70 struct ocfs2_alloc_context *data_ac; 71 struct ocfs2_cached_dealloc_ctxt dealloc; 72 int set_abort; 73 }; 74 75 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root)) 76 #define OCFS2_XATTR_INLINE_SIZE 80 77 #define OCFS2_XATTR_HEADER_GAP 4 78 #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \ 79 - sizeof(struct ocfs2_xattr_header) \ 80 - OCFS2_XATTR_HEADER_GAP) 81 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \ 82 - sizeof(struct ocfs2_xattr_block) \ 83 - sizeof(struct ocfs2_xattr_header) \ 84 - OCFS2_XATTR_HEADER_GAP) 85 86 static struct ocfs2_xattr_def_value_root def_xv = { 87 .xv.xr_list.l_count = cpu_to_le16(1), 88 }; 89 90 const struct xattr_handler *ocfs2_xattr_handlers[] = { 91 &ocfs2_xattr_user_handler, 92 &ocfs2_xattr_trusted_handler, 93 &ocfs2_xattr_security_handler, 94 NULL 95 }; 96 97 static const struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = { 98 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler, 99 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS] 100 = &posix_acl_access_xattr_handler, 101 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT] 102 = &posix_acl_default_xattr_handler, 103 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler, 104 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler, 105 }; 106 107 struct ocfs2_xattr_info { 108 int xi_name_index; 109 const char *xi_name; 110 int xi_name_len; 111 const void *xi_value; 112 size_t xi_value_len; 113 }; 114 115 struct ocfs2_xattr_search { 116 struct buffer_head *inode_bh; 117 /* 118 * xattr_bh point to the block buffer head which has extended attribute 119 * when extended attribute in inode, xattr_bh is equal to inode_bh. 120 */ 121 struct buffer_head *xattr_bh; 122 struct ocfs2_xattr_header *header; 123 struct ocfs2_xattr_bucket *bucket; 124 void *base; 125 void *end; 126 struct ocfs2_xattr_entry *here; 127 int not_found; 128 }; 129 130 /* Operations on struct ocfs2_xa_entry */ 131 struct ocfs2_xa_loc; 132 struct ocfs2_xa_loc_operations { 133 /* 134 * Journal functions 135 */ 136 int (*xlo_journal_access)(handle_t *handle, struct ocfs2_xa_loc *loc, 137 int type); 138 void (*xlo_journal_dirty)(handle_t *handle, struct ocfs2_xa_loc *loc); 139 140 /* 141 * Return a pointer to the appropriate buffer in loc->xl_storage 142 * at the given offset from loc->xl_header. 143 */ 144 void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset); 145 146 /* Can we reuse the existing entry for the new value? */ 147 int (*xlo_can_reuse)(struct ocfs2_xa_loc *loc, 148 struct ocfs2_xattr_info *xi); 149 150 /* How much space is needed for the new value? */ 151 int (*xlo_check_space)(struct ocfs2_xa_loc *loc, 152 struct ocfs2_xattr_info *xi); 153 154 /* 155 * Return the offset of the first name+value pair. This is 156 * the start of our downward-filling free space. 157 */ 158 int (*xlo_get_free_start)(struct ocfs2_xa_loc *loc); 159 160 /* 161 * Remove the name+value at this location. Do whatever is 162 * appropriate with the remaining name+value pairs. 163 */ 164 void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc); 165 166 /* Fill xl_entry with a new entry */ 167 void (*xlo_add_entry)(struct ocfs2_xa_loc *loc, u32 name_hash); 168 169 /* Add name+value storage to an entry */ 170 void (*xlo_add_namevalue)(struct ocfs2_xa_loc *loc, int size); 171 172 /* 173 * Initialize the value buf's access and bh fields for this entry. 174 * ocfs2_xa_fill_value_buf() will handle the xv pointer. 175 */ 176 void (*xlo_fill_value_buf)(struct ocfs2_xa_loc *loc, 177 struct ocfs2_xattr_value_buf *vb); 178 }; 179 180 /* 181 * Describes an xattr entry location. This is a memory structure 182 * tracking the on-disk structure. 183 */ 184 struct ocfs2_xa_loc { 185 /* This xattr belongs to this inode */ 186 struct inode *xl_inode; 187 188 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */ 189 struct ocfs2_xattr_header *xl_header; 190 191 /* Bytes from xl_header to the end of the storage */ 192 int xl_size; 193 194 /* 195 * The ocfs2_xattr_entry this location describes. If this is 196 * NULL, this location describes the on-disk structure where it 197 * would have been. 198 */ 199 struct ocfs2_xattr_entry *xl_entry; 200 201 /* 202 * Internal housekeeping 203 */ 204 205 /* Buffer(s) containing this entry */ 206 void *xl_storage; 207 208 /* Operations on the storage backing this location */ 209 const struct ocfs2_xa_loc_operations *xl_ops; 210 }; 211 212 /* 213 * Convenience functions to calculate how much space is needed for a 214 * given name+value pair 215 */ 216 static int namevalue_size(int name_len, uint64_t value_len) 217 { 218 if (value_len > OCFS2_XATTR_INLINE_SIZE) 219 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE; 220 else 221 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len); 222 } 223 224 static int namevalue_size_xi(struct ocfs2_xattr_info *xi) 225 { 226 return namevalue_size(xi->xi_name_len, xi->xi_value_len); 227 } 228 229 static int namevalue_size_xe(struct ocfs2_xattr_entry *xe) 230 { 231 u64 value_len = le64_to_cpu(xe->xe_value_size); 232 233 BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) && 234 ocfs2_xattr_is_local(xe)); 235 return namevalue_size(xe->xe_name_len, value_len); 236 } 237 238 239 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb, 240 struct ocfs2_xattr_header *xh, 241 int index, 242 int *block_off, 243 int *new_offset); 244 245 static int ocfs2_xattr_block_find(struct inode *inode, 246 int name_index, 247 const char *name, 248 struct ocfs2_xattr_search *xs); 249 static int ocfs2_xattr_index_block_find(struct inode *inode, 250 struct buffer_head *root_bh, 251 int name_index, 252 const char *name, 253 struct ocfs2_xattr_search *xs); 254 255 static int ocfs2_xattr_tree_list_index_block(struct inode *inode, 256 struct buffer_head *blk_bh, 257 char *buffer, 258 size_t buffer_size); 259 260 static int ocfs2_xattr_create_index_block(struct inode *inode, 261 struct ocfs2_xattr_search *xs, 262 struct ocfs2_xattr_set_ctxt *ctxt); 263 264 static int ocfs2_xattr_set_entry_index_block(struct inode *inode, 265 struct ocfs2_xattr_info *xi, 266 struct ocfs2_xattr_search *xs, 267 struct ocfs2_xattr_set_ctxt *ctxt); 268 269 typedef int (xattr_tree_rec_func)(struct inode *inode, 270 struct buffer_head *root_bh, 271 u64 blkno, u32 cpos, u32 len, void *para); 272 static int ocfs2_iterate_xattr_index_block(struct inode *inode, 273 struct buffer_head *root_bh, 274 xattr_tree_rec_func *rec_func, 275 void *para); 276 static int ocfs2_delete_xattr_in_bucket(struct inode *inode, 277 struct ocfs2_xattr_bucket *bucket, 278 void *para); 279 static int ocfs2_rm_xattr_cluster(struct inode *inode, 280 struct buffer_head *root_bh, 281 u64 blkno, 282 u32 cpos, 283 u32 len, 284 void *para); 285 286 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle, 287 u64 src_blk, u64 last_blk, u64 to_blk, 288 unsigned int start_bucket, 289 u32 *first_hash); 290 static int ocfs2_prepare_refcount_xattr(struct inode *inode, 291 struct ocfs2_dinode *di, 292 struct ocfs2_xattr_info *xi, 293 struct ocfs2_xattr_search *xis, 294 struct ocfs2_xattr_search *xbs, 295 struct ocfs2_refcount_tree **ref_tree, 296 int *meta_need, 297 int *credits); 298 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb, 299 struct ocfs2_xattr_bucket *bucket, 300 int offset, 301 struct ocfs2_xattr_value_root **xv, 302 struct buffer_head **bh); 303 304 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb) 305 { 306 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE; 307 } 308 309 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb) 310 { 311 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits); 312 } 313 314 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr) 315 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data) 316 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0)) 317 318 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode) 319 { 320 struct ocfs2_xattr_bucket *bucket; 321 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb); 322 323 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET); 324 325 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS); 326 if (bucket) { 327 bucket->bu_inode = inode; 328 bucket->bu_blocks = blks; 329 } 330 331 return bucket; 332 } 333 334 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket) 335 { 336 int i; 337 338 for (i = 0; i < bucket->bu_blocks; i++) { 339 brelse(bucket->bu_bhs[i]); 340 bucket->bu_bhs[i] = NULL; 341 } 342 } 343 344 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket) 345 { 346 if (bucket) { 347 ocfs2_xattr_bucket_relse(bucket); 348 bucket->bu_inode = NULL; 349 kfree(bucket); 350 } 351 } 352 353 /* 354 * A bucket that has never been written to disk doesn't need to be 355 * read. We just need the buffer_heads. Don't call this for 356 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes 357 * them fully. 358 */ 359 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket, 360 u64 xb_blkno, int new) 361 { 362 int i, rc = 0; 363 364 for (i = 0; i < bucket->bu_blocks; i++) { 365 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb, 366 xb_blkno + i); 367 if (!bucket->bu_bhs[i]) { 368 rc = -ENOMEM; 369 mlog_errno(rc); 370 break; 371 } 372 373 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode), 374 bucket->bu_bhs[i])) { 375 if (new) 376 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode), 377 bucket->bu_bhs[i]); 378 else { 379 set_buffer_uptodate(bucket->bu_bhs[i]); 380 ocfs2_set_buffer_uptodate(INODE_CACHE(bucket->bu_inode), 381 bucket->bu_bhs[i]); 382 } 383 } 384 } 385 386 if (rc) 387 ocfs2_xattr_bucket_relse(bucket); 388 return rc; 389 } 390 391 /* Read the xattr bucket at xb_blkno */ 392 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket, 393 u64 xb_blkno) 394 { 395 int rc; 396 397 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno, 398 bucket->bu_blocks, bucket->bu_bhs, 0, 399 NULL); 400 if (!rc) { 401 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock); 402 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb, 403 bucket->bu_bhs, 404 bucket->bu_blocks, 405 &bucket_xh(bucket)->xh_check); 406 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock); 407 if (rc) 408 mlog_errno(rc); 409 } 410 411 if (rc) 412 ocfs2_xattr_bucket_relse(bucket); 413 return rc; 414 } 415 416 static int ocfs2_xattr_bucket_journal_access(handle_t *handle, 417 struct ocfs2_xattr_bucket *bucket, 418 int type) 419 { 420 int i, rc = 0; 421 422 for (i = 0; i < bucket->bu_blocks; i++) { 423 rc = ocfs2_journal_access(handle, 424 INODE_CACHE(bucket->bu_inode), 425 bucket->bu_bhs[i], type); 426 if (rc) { 427 mlog_errno(rc); 428 break; 429 } 430 } 431 432 return rc; 433 } 434 435 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle, 436 struct ocfs2_xattr_bucket *bucket) 437 { 438 int i; 439 440 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock); 441 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb, 442 bucket->bu_bhs, bucket->bu_blocks, 443 &bucket_xh(bucket)->xh_check); 444 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock); 445 446 for (i = 0; i < bucket->bu_blocks; i++) 447 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]); 448 } 449 450 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest, 451 struct ocfs2_xattr_bucket *src) 452 { 453 int i; 454 int blocksize = src->bu_inode->i_sb->s_blocksize; 455 456 BUG_ON(dest->bu_blocks != src->bu_blocks); 457 BUG_ON(dest->bu_inode != src->bu_inode); 458 459 for (i = 0; i < src->bu_blocks; i++) { 460 memcpy(bucket_block(dest, i), bucket_block(src, i), 461 blocksize); 462 } 463 } 464 465 static int ocfs2_validate_xattr_block(struct super_block *sb, 466 struct buffer_head *bh) 467 { 468 int rc; 469 struct ocfs2_xattr_block *xb = 470 (struct ocfs2_xattr_block *)bh->b_data; 471 472 trace_ocfs2_validate_xattr_block((unsigned long long)bh->b_blocknr); 473 474 BUG_ON(!buffer_uptodate(bh)); 475 476 /* 477 * If the ecc fails, we return the error but otherwise 478 * leave the filesystem running. We know any error is 479 * local to this block. 480 */ 481 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check); 482 if (rc) 483 return rc; 484 485 /* 486 * Errors after here are fatal 487 */ 488 489 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) { 490 return ocfs2_error(sb, 491 "Extended attribute block #%llu has bad signature %.*s\n", 492 (unsigned long long)bh->b_blocknr, 7, 493 xb->xb_signature); 494 } 495 496 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) { 497 return ocfs2_error(sb, 498 "Extended attribute block #%llu has an invalid xb_blkno of %llu\n", 499 (unsigned long long)bh->b_blocknr, 500 (unsigned long long)le64_to_cpu(xb->xb_blkno)); 501 } 502 503 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) { 504 return ocfs2_error(sb, 505 "Extended attribute block #%llu has an invalid xb_fs_generation of #%u\n", 506 (unsigned long long)bh->b_blocknr, 507 le32_to_cpu(xb->xb_fs_generation)); 508 } 509 510 return 0; 511 } 512 513 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno, 514 struct buffer_head **bh) 515 { 516 int rc; 517 struct buffer_head *tmp = *bh; 518 519 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp, 520 ocfs2_validate_xattr_block); 521 522 /* If ocfs2_read_block() got us a new bh, pass it up. */ 523 if (!rc && !*bh) 524 *bh = tmp; 525 526 return rc; 527 } 528 529 static inline const char *ocfs2_xattr_prefix(int name_index) 530 { 531 const struct xattr_handler *handler = NULL; 532 533 if (name_index > 0 && name_index < OCFS2_XATTR_MAX) 534 handler = ocfs2_xattr_handler_map[name_index]; 535 return handler ? xattr_prefix(handler) : NULL; 536 } 537 538 static u32 ocfs2_xattr_name_hash(struct inode *inode, 539 const char *name, 540 int name_len) 541 { 542 /* Get hash value of uuid from super block */ 543 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash; 544 int i; 545 546 /* hash extended attribute name */ 547 for (i = 0; i < name_len; i++) { 548 hash = (hash << OCFS2_HASH_SHIFT) ^ 549 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^ 550 *name++; 551 } 552 553 return hash; 554 } 555 556 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len) 557 { 558 return namevalue_size(name_len, value_len) + 559 sizeof(struct ocfs2_xattr_entry); 560 } 561 562 static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi) 563 { 564 return namevalue_size_xi(xi) + 565 sizeof(struct ocfs2_xattr_entry); 566 } 567 568 static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe) 569 { 570 return namevalue_size_xe(xe) + 571 sizeof(struct ocfs2_xattr_entry); 572 } 573 574 int ocfs2_calc_security_init(struct inode *dir, 575 struct ocfs2_security_xattr_info *si, 576 int *want_clusters, 577 int *xattr_credits, 578 struct ocfs2_alloc_context **xattr_ac) 579 { 580 int ret = 0; 581 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); 582 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name), 583 si->value_len); 584 585 /* 586 * The max space of security xattr taken inline is 587 * 256(name) + 80(value) + 16(entry) = 352 bytes, 588 * So reserve one metadata block for it is ok. 589 */ 590 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE || 591 s_size > OCFS2_XATTR_FREE_IN_IBODY) { 592 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac); 593 if (ret) { 594 mlog_errno(ret); 595 return ret; 596 } 597 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS; 598 } 599 600 /* reserve clusters for xattr value which will be set in B tree*/ 601 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) { 602 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb, 603 si->value_len); 604 605 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb, 606 new_clusters); 607 *want_clusters += new_clusters; 608 } 609 return ret; 610 } 611 612 int ocfs2_calc_xattr_init(struct inode *dir, 613 struct buffer_head *dir_bh, 614 umode_t mode, 615 struct ocfs2_security_xattr_info *si, 616 int *want_clusters, 617 int *xattr_credits, 618 int *want_meta) 619 { 620 int ret = 0; 621 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); 622 int s_size = 0, a_size = 0, acl_len = 0, new_clusters; 623 624 if (si->enable) 625 s_size = ocfs2_xattr_entry_real_size(strlen(si->name), 626 si->value_len); 627 628 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) { 629 down_read(&OCFS2_I(dir)->ip_xattr_sem); 630 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh, 631 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT, 632 "", NULL, 0); 633 up_read(&OCFS2_I(dir)->ip_xattr_sem); 634 if (acl_len > 0) { 635 a_size = ocfs2_xattr_entry_real_size(0, acl_len); 636 if (S_ISDIR(mode)) 637 a_size <<= 1; 638 } else if (acl_len != 0 && acl_len != -ENODATA) { 639 ret = acl_len; 640 mlog_errno(ret); 641 return ret; 642 } 643 } 644 645 if (!(s_size + a_size)) 646 return ret; 647 648 /* 649 * The max space of security xattr taken inline is 650 * 256(name) + 80(value) + 16(entry) = 352 bytes, 651 * The max space of acl xattr taken inline is 652 * 80(value) + 16(entry) * 2(if directory) = 192 bytes, 653 * when blocksize = 512, may reserve one more cluser for 654 * xattr bucket, otherwise reserve one metadata block 655 * for them is ok. 656 * If this is a new directory with inline data, 657 * we choose to reserve the entire inline area for 658 * directory contents and force an external xattr block. 659 */ 660 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE || 661 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) || 662 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) { 663 *want_meta = *want_meta + 1; 664 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS; 665 } 666 667 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE && 668 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) { 669 *want_clusters += 1; 670 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb); 671 } 672 673 /* 674 * reserve credits and clusters for xattrs which has large value 675 * and have to be set outside 676 */ 677 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) { 678 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb, 679 si->value_len); 680 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb, 681 new_clusters); 682 *want_clusters += new_clusters; 683 } 684 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL && 685 acl_len > OCFS2_XATTR_INLINE_SIZE) { 686 /* for directory, it has DEFAULT and ACCESS two types of acls */ 687 new_clusters = (S_ISDIR(mode) ? 2 : 1) * 688 ocfs2_clusters_for_bytes(dir->i_sb, acl_len); 689 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb, 690 new_clusters); 691 *want_clusters += new_clusters; 692 } 693 694 return ret; 695 } 696 697 static int ocfs2_xattr_extend_allocation(struct inode *inode, 698 u32 clusters_to_add, 699 struct ocfs2_xattr_value_buf *vb, 700 struct ocfs2_xattr_set_ctxt *ctxt) 701 { 702 int status = 0, credits; 703 handle_t *handle = ctxt->handle; 704 enum ocfs2_alloc_restarted why; 705 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters); 706 struct ocfs2_extent_tree et; 707 708 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb); 709 710 while (clusters_to_add) { 711 trace_ocfs2_xattr_extend_allocation(clusters_to_add); 712 713 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh, 714 OCFS2_JOURNAL_ACCESS_WRITE); 715 if (status < 0) { 716 mlog_errno(status); 717 break; 718 } 719 720 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters); 721 status = ocfs2_add_clusters_in_btree(handle, 722 &et, 723 &logical_start, 724 clusters_to_add, 725 0, 726 ctxt->data_ac, 727 ctxt->meta_ac, 728 &why); 729 if ((status < 0) && (status != -EAGAIN)) { 730 if (status != -ENOSPC) 731 mlog_errno(status); 732 break; 733 } 734 735 ocfs2_journal_dirty(handle, vb->vb_bh); 736 737 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - 738 prev_clusters; 739 740 if (why != RESTART_NONE && clusters_to_add) { 741 /* 742 * We can only fail in case the alloc file doesn't give 743 * up enough clusters. 744 */ 745 BUG_ON(why == RESTART_META); 746 747 credits = ocfs2_calc_extend_credits(inode->i_sb, 748 &vb->vb_xv->xr_list); 749 status = ocfs2_extend_trans(handle, credits); 750 if (status < 0) { 751 status = -ENOMEM; 752 mlog_errno(status); 753 break; 754 } 755 } 756 } 757 758 return status; 759 } 760 761 static int __ocfs2_remove_xattr_range(struct inode *inode, 762 struct ocfs2_xattr_value_buf *vb, 763 u32 cpos, u32 phys_cpos, u32 len, 764 unsigned int ext_flags, 765 struct ocfs2_xattr_set_ctxt *ctxt) 766 { 767 int ret; 768 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); 769 handle_t *handle = ctxt->handle; 770 struct ocfs2_extent_tree et; 771 772 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb); 773 774 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh, 775 OCFS2_JOURNAL_ACCESS_WRITE); 776 if (ret) { 777 mlog_errno(ret); 778 goto out; 779 } 780 781 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac, 782 &ctxt->dealloc); 783 if (ret) { 784 mlog_errno(ret); 785 goto out; 786 } 787 788 le32_add_cpu(&vb->vb_xv->xr_clusters, -len); 789 ocfs2_journal_dirty(handle, vb->vb_bh); 790 791 if (ext_flags & OCFS2_EXT_REFCOUNTED) 792 ret = ocfs2_decrease_refcount(inode, handle, 793 ocfs2_blocks_to_clusters(inode->i_sb, 794 phys_blkno), 795 len, ctxt->meta_ac, &ctxt->dealloc, 1); 796 else 797 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc, 798 phys_blkno, len); 799 if (ret) 800 mlog_errno(ret); 801 802 out: 803 return ret; 804 } 805 806 static int ocfs2_xattr_shrink_size(struct inode *inode, 807 u32 old_clusters, 808 u32 new_clusters, 809 struct ocfs2_xattr_value_buf *vb, 810 struct ocfs2_xattr_set_ctxt *ctxt) 811 { 812 int ret = 0; 813 unsigned int ext_flags; 814 u32 trunc_len, cpos, phys_cpos, alloc_size; 815 u64 block; 816 817 if (old_clusters <= new_clusters) 818 return 0; 819 820 cpos = new_clusters; 821 trunc_len = old_clusters - new_clusters; 822 while (trunc_len) { 823 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos, 824 &alloc_size, 825 &vb->vb_xv->xr_list, &ext_flags); 826 if (ret) { 827 mlog_errno(ret); 828 goto out; 829 } 830 831 if (alloc_size > trunc_len) 832 alloc_size = trunc_len; 833 834 ret = __ocfs2_remove_xattr_range(inode, vb, cpos, 835 phys_cpos, alloc_size, 836 ext_flags, ctxt); 837 if (ret) { 838 mlog_errno(ret); 839 goto out; 840 } 841 842 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); 843 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), 844 block, alloc_size); 845 cpos += alloc_size; 846 trunc_len -= alloc_size; 847 } 848 849 out: 850 return ret; 851 } 852 853 static int ocfs2_xattr_value_truncate(struct inode *inode, 854 struct ocfs2_xattr_value_buf *vb, 855 int len, 856 struct ocfs2_xattr_set_ctxt *ctxt) 857 { 858 int ret; 859 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len); 860 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters); 861 862 if (new_clusters == old_clusters) 863 return 0; 864 865 if (new_clusters > old_clusters) 866 ret = ocfs2_xattr_extend_allocation(inode, 867 new_clusters - old_clusters, 868 vb, ctxt); 869 else 870 ret = ocfs2_xattr_shrink_size(inode, 871 old_clusters, new_clusters, 872 vb, ctxt); 873 874 return ret; 875 } 876 877 static int ocfs2_xattr_list_entry(struct super_block *sb, 878 char *buffer, size_t size, 879 size_t *result, int type, 880 const char *name, int name_len) 881 { 882 char *p = buffer + *result; 883 const char *prefix; 884 int prefix_len; 885 int total_len; 886 887 switch(type) { 888 case OCFS2_XATTR_INDEX_USER: 889 if (OCFS2_SB(sb)->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) 890 return 0; 891 break; 892 893 case OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS: 894 case OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT: 895 if (!(sb->s_flags & SB_POSIXACL)) 896 return 0; 897 break; 898 899 case OCFS2_XATTR_INDEX_TRUSTED: 900 if (!capable(CAP_SYS_ADMIN)) 901 return 0; 902 break; 903 } 904 905 prefix = ocfs2_xattr_prefix(type); 906 if (!prefix) 907 return 0; 908 prefix_len = strlen(prefix); 909 total_len = prefix_len + name_len + 1; 910 *result += total_len; 911 912 /* we are just looking for how big our buffer needs to be */ 913 if (!size) 914 return 0; 915 916 if (*result > size) 917 return -ERANGE; 918 919 memcpy(p, prefix, prefix_len); 920 memcpy(p + prefix_len, name, name_len); 921 p[prefix_len + name_len] = '\0'; 922 923 return 0; 924 } 925 926 static int ocfs2_xattr_list_entries(struct inode *inode, 927 struct ocfs2_xattr_header *header, 928 char *buffer, size_t buffer_size) 929 { 930 size_t result = 0; 931 int i, type, ret; 932 const char *name; 933 934 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) { 935 struct ocfs2_xattr_entry *entry = &header->xh_entries[i]; 936 type = ocfs2_xattr_get_type(entry); 937 name = (const char *)header + 938 le16_to_cpu(entry->xe_name_offset); 939 940 ret = ocfs2_xattr_list_entry(inode->i_sb, 941 buffer, buffer_size, 942 &result, type, name, 943 entry->xe_name_len); 944 if (ret) 945 return ret; 946 } 947 948 return result; 949 } 950 951 int ocfs2_has_inline_xattr_value_outside(struct inode *inode, 952 struct ocfs2_dinode *di) 953 { 954 struct ocfs2_xattr_header *xh; 955 int i; 956 957 xh = (struct ocfs2_xattr_header *) 958 ((void *)di + inode->i_sb->s_blocksize - 959 le16_to_cpu(di->i_xattr_inline_size)); 960 961 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) 962 if (!ocfs2_xattr_is_local(&xh->xh_entries[i])) 963 return 1; 964 965 return 0; 966 } 967 968 static int ocfs2_xattr_ibody_list(struct inode *inode, 969 struct ocfs2_dinode *di, 970 char *buffer, 971 size_t buffer_size) 972 { 973 struct ocfs2_xattr_header *header = NULL; 974 struct ocfs2_inode_info *oi = OCFS2_I(inode); 975 int ret = 0; 976 977 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) 978 return ret; 979 980 header = (struct ocfs2_xattr_header *) 981 ((void *)di + inode->i_sb->s_blocksize - 982 le16_to_cpu(di->i_xattr_inline_size)); 983 984 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size); 985 986 return ret; 987 } 988 989 static int ocfs2_xattr_block_list(struct inode *inode, 990 struct ocfs2_dinode *di, 991 char *buffer, 992 size_t buffer_size) 993 { 994 struct buffer_head *blk_bh = NULL; 995 struct ocfs2_xattr_block *xb; 996 int ret = 0; 997 998 if (!di->i_xattr_loc) 999 return ret; 1000 1001 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc), 1002 &blk_bh); 1003 if (ret < 0) { 1004 mlog_errno(ret); 1005 return ret; 1006 } 1007 1008 xb = (struct ocfs2_xattr_block *)blk_bh->b_data; 1009 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { 1010 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header; 1011 ret = ocfs2_xattr_list_entries(inode, header, 1012 buffer, buffer_size); 1013 } else 1014 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh, 1015 buffer, buffer_size); 1016 1017 brelse(blk_bh); 1018 1019 return ret; 1020 } 1021 1022 ssize_t ocfs2_listxattr(struct dentry *dentry, 1023 char *buffer, 1024 size_t size) 1025 { 1026 int ret = 0, i_ret = 0, b_ret = 0; 1027 struct buffer_head *di_bh = NULL; 1028 struct ocfs2_dinode *di = NULL; 1029 struct ocfs2_inode_info *oi = OCFS2_I(d_inode(dentry)); 1030 1031 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb))) 1032 return -EOPNOTSUPP; 1033 1034 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) 1035 return ret; 1036 1037 ret = ocfs2_inode_lock(d_inode(dentry), &di_bh, 0); 1038 if (ret < 0) { 1039 mlog_errno(ret); 1040 return ret; 1041 } 1042 1043 di = (struct ocfs2_dinode *)di_bh->b_data; 1044 1045 down_read(&oi->ip_xattr_sem); 1046 i_ret = ocfs2_xattr_ibody_list(d_inode(dentry), di, buffer, size); 1047 if (i_ret < 0) 1048 b_ret = 0; 1049 else { 1050 if (buffer) { 1051 buffer += i_ret; 1052 size -= i_ret; 1053 } 1054 b_ret = ocfs2_xattr_block_list(d_inode(dentry), di, 1055 buffer, size); 1056 if (b_ret < 0) 1057 i_ret = 0; 1058 } 1059 up_read(&oi->ip_xattr_sem); 1060 ocfs2_inode_unlock(d_inode(dentry), 0); 1061 1062 brelse(di_bh); 1063 1064 return i_ret + b_ret; 1065 } 1066 1067 static int ocfs2_xattr_find_entry(int name_index, 1068 const char *name, 1069 struct ocfs2_xattr_search *xs) 1070 { 1071 struct ocfs2_xattr_entry *entry; 1072 size_t name_len; 1073 int i, cmp = 1; 1074 1075 if (name == NULL) 1076 return -EINVAL; 1077 1078 name_len = strlen(name); 1079 entry = xs->here; 1080 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) { 1081 cmp = name_index - ocfs2_xattr_get_type(entry); 1082 if (!cmp) 1083 cmp = name_len - entry->xe_name_len; 1084 if (!cmp) 1085 cmp = memcmp(name, (xs->base + 1086 le16_to_cpu(entry->xe_name_offset)), 1087 name_len); 1088 if (cmp == 0) 1089 break; 1090 entry += 1; 1091 } 1092 xs->here = entry; 1093 1094 return cmp ? -ENODATA : 0; 1095 } 1096 1097 static int ocfs2_xattr_get_value_outside(struct inode *inode, 1098 struct ocfs2_xattr_value_root *xv, 1099 void *buffer, 1100 size_t len) 1101 { 1102 u32 cpos, p_cluster, num_clusters, bpc, clusters; 1103 u64 blkno; 1104 int i, ret = 0; 1105 size_t cplen, blocksize; 1106 struct buffer_head *bh = NULL; 1107 struct ocfs2_extent_list *el; 1108 1109 el = &xv->xr_list; 1110 clusters = le32_to_cpu(xv->xr_clusters); 1111 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); 1112 blocksize = inode->i_sb->s_blocksize; 1113 1114 cpos = 0; 1115 while (cpos < clusters) { 1116 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster, 1117 &num_clusters, el, NULL); 1118 if (ret) { 1119 mlog_errno(ret); 1120 goto out; 1121 } 1122 1123 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster); 1124 /* Copy ocfs2_xattr_value */ 1125 for (i = 0; i < num_clusters * bpc; i++, blkno++) { 1126 ret = ocfs2_read_block(INODE_CACHE(inode), blkno, 1127 &bh, NULL); 1128 if (ret) { 1129 mlog_errno(ret); 1130 goto out; 1131 } 1132 1133 cplen = len >= blocksize ? blocksize : len; 1134 memcpy(buffer, bh->b_data, cplen); 1135 len -= cplen; 1136 buffer += cplen; 1137 1138 brelse(bh); 1139 bh = NULL; 1140 if (len == 0) 1141 break; 1142 } 1143 cpos += num_clusters; 1144 } 1145 out: 1146 return ret; 1147 } 1148 1149 static int ocfs2_xattr_ibody_get(struct inode *inode, 1150 int name_index, 1151 const char *name, 1152 void *buffer, 1153 size_t buffer_size, 1154 struct ocfs2_xattr_search *xs) 1155 { 1156 struct ocfs2_inode_info *oi = OCFS2_I(inode); 1157 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; 1158 struct ocfs2_xattr_value_root *xv; 1159 size_t size; 1160 int ret = 0; 1161 1162 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) 1163 return -ENODATA; 1164 1165 xs->end = (void *)di + inode->i_sb->s_blocksize; 1166 xs->header = (struct ocfs2_xattr_header *) 1167 (xs->end - le16_to_cpu(di->i_xattr_inline_size)); 1168 xs->base = (void *)xs->header; 1169 xs->here = xs->header->xh_entries; 1170 1171 ret = ocfs2_xattr_find_entry(name_index, name, xs); 1172 if (ret) 1173 return ret; 1174 size = le64_to_cpu(xs->here->xe_value_size); 1175 if (buffer) { 1176 if (size > buffer_size) 1177 return -ERANGE; 1178 if (ocfs2_xattr_is_local(xs->here)) { 1179 memcpy(buffer, (void *)xs->base + 1180 le16_to_cpu(xs->here->xe_name_offset) + 1181 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size); 1182 } else { 1183 xv = (struct ocfs2_xattr_value_root *) 1184 (xs->base + le16_to_cpu( 1185 xs->here->xe_name_offset) + 1186 OCFS2_XATTR_SIZE(xs->here->xe_name_len)); 1187 ret = ocfs2_xattr_get_value_outside(inode, xv, 1188 buffer, size); 1189 if (ret < 0) { 1190 mlog_errno(ret); 1191 return ret; 1192 } 1193 } 1194 } 1195 1196 return size; 1197 } 1198 1199 static int ocfs2_xattr_block_get(struct inode *inode, 1200 int name_index, 1201 const char *name, 1202 void *buffer, 1203 size_t buffer_size, 1204 struct ocfs2_xattr_search *xs) 1205 { 1206 struct ocfs2_xattr_block *xb; 1207 struct ocfs2_xattr_value_root *xv; 1208 size_t size; 1209 int ret = -ENODATA, name_offset, name_len, i; 1210 int block_off; 1211 1212 xs->bucket = ocfs2_xattr_bucket_new(inode); 1213 if (!xs->bucket) { 1214 ret = -ENOMEM; 1215 mlog_errno(ret); 1216 goto cleanup; 1217 } 1218 1219 ret = ocfs2_xattr_block_find(inode, name_index, name, xs); 1220 if (ret) { 1221 mlog_errno(ret); 1222 goto cleanup; 1223 } 1224 1225 if (xs->not_found) { 1226 ret = -ENODATA; 1227 goto cleanup; 1228 } 1229 1230 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; 1231 size = le64_to_cpu(xs->here->xe_value_size); 1232 if (buffer) { 1233 ret = -ERANGE; 1234 if (size > buffer_size) 1235 goto cleanup; 1236 1237 name_offset = le16_to_cpu(xs->here->xe_name_offset); 1238 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len); 1239 i = xs->here - xs->header->xh_entries; 1240 1241 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) { 1242 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, 1243 bucket_xh(xs->bucket), 1244 i, 1245 &block_off, 1246 &name_offset); 1247 if (ret) { 1248 mlog_errno(ret); 1249 goto cleanup; 1250 } 1251 xs->base = bucket_block(xs->bucket, block_off); 1252 } 1253 if (ocfs2_xattr_is_local(xs->here)) { 1254 memcpy(buffer, (void *)xs->base + 1255 name_offset + name_len, size); 1256 } else { 1257 xv = (struct ocfs2_xattr_value_root *) 1258 (xs->base + name_offset + name_len); 1259 ret = ocfs2_xattr_get_value_outside(inode, xv, 1260 buffer, size); 1261 if (ret < 0) { 1262 mlog_errno(ret); 1263 goto cleanup; 1264 } 1265 } 1266 } 1267 ret = size; 1268 cleanup: 1269 ocfs2_xattr_bucket_free(xs->bucket); 1270 1271 brelse(xs->xattr_bh); 1272 xs->xattr_bh = NULL; 1273 return ret; 1274 } 1275 1276 int ocfs2_xattr_get_nolock(struct inode *inode, 1277 struct buffer_head *di_bh, 1278 int name_index, 1279 const char *name, 1280 void *buffer, 1281 size_t buffer_size) 1282 { 1283 int ret; 1284 struct ocfs2_dinode *di = NULL; 1285 struct ocfs2_inode_info *oi = OCFS2_I(inode); 1286 struct ocfs2_xattr_search xis = { 1287 .not_found = -ENODATA, 1288 }; 1289 struct ocfs2_xattr_search xbs = { 1290 .not_found = -ENODATA, 1291 }; 1292 1293 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) 1294 return -EOPNOTSUPP; 1295 1296 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) 1297 return -ENODATA; 1298 1299 xis.inode_bh = xbs.inode_bh = di_bh; 1300 di = (struct ocfs2_dinode *)di_bh->b_data; 1301 1302 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer, 1303 buffer_size, &xis); 1304 if (ret == -ENODATA && di->i_xattr_loc) 1305 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer, 1306 buffer_size, &xbs); 1307 1308 return ret; 1309 } 1310 1311 /* ocfs2_xattr_get() 1312 * 1313 * Copy an extended attribute into the buffer provided. 1314 * Buffer is NULL to compute the size of buffer required. 1315 */ 1316 static int ocfs2_xattr_get(struct inode *inode, 1317 int name_index, 1318 const char *name, 1319 void *buffer, 1320 size_t buffer_size) 1321 { 1322 int ret, had_lock; 1323 struct buffer_head *di_bh = NULL; 1324 struct ocfs2_lock_holder oh; 1325 1326 had_lock = ocfs2_inode_lock_tracker(inode, &di_bh, 0, &oh); 1327 if (had_lock < 0) { 1328 mlog_errno(had_lock); 1329 return had_lock; 1330 } 1331 down_read(&OCFS2_I(inode)->ip_xattr_sem); 1332 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index, 1333 name, buffer, buffer_size); 1334 up_read(&OCFS2_I(inode)->ip_xattr_sem); 1335 1336 ocfs2_inode_unlock_tracker(inode, 0, &oh, had_lock); 1337 1338 brelse(di_bh); 1339 1340 return ret; 1341 } 1342 1343 static int __ocfs2_xattr_set_value_outside(struct inode *inode, 1344 handle_t *handle, 1345 struct ocfs2_xattr_value_buf *vb, 1346 const void *value, 1347 int value_len) 1348 { 1349 int ret = 0, i, cp_len; 1350 u16 blocksize = inode->i_sb->s_blocksize; 1351 u32 p_cluster, num_clusters; 1352 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); 1353 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len); 1354 u64 blkno; 1355 struct buffer_head *bh = NULL; 1356 unsigned int ext_flags; 1357 struct ocfs2_xattr_value_root *xv = vb->vb_xv; 1358 1359 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters)); 1360 1361 while (cpos < clusters) { 1362 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster, 1363 &num_clusters, &xv->xr_list, 1364 &ext_flags); 1365 if (ret) { 1366 mlog_errno(ret); 1367 goto out; 1368 } 1369 1370 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED); 1371 1372 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster); 1373 1374 for (i = 0; i < num_clusters * bpc; i++, blkno++) { 1375 ret = ocfs2_read_block(INODE_CACHE(inode), blkno, 1376 &bh, NULL); 1377 if (ret) { 1378 mlog_errno(ret); 1379 goto out; 1380 } 1381 1382 ret = ocfs2_journal_access(handle, 1383 INODE_CACHE(inode), 1384 bh, 1385 OCFS2_JOURNAL_ACCESS_WRITE); 1386 if (ret < 0) { 1387 mlog_errno(ret); 1388 goto out; 1389 } 1390 1391 cp_len = value_len > blocksize ? blocksize : value_len; 1392 memcpy(bh->b_data, value, cp_len); 1393 value_len -= cp_len; 1394 value += cp_len; 1395 if (cp_len < blocksize) 1396 memset(bh->b_data + cp_len, 0, 1397 blocksize - cp_len); 1398 1399 ocfs2_journal_dirty(handle, bh); 1400 brelse(bh); 1401 bh = NULL; 1402 1403 /* 1404 * XXX: do we need to empty all the following 1405 * blocks in this cluster? 1406 */ 1407 if (!value_len) 1408 break; 1409 } 1410 cpos += num_clusters; 1411 } 1412 out: 1413 brelse(bh); 1414 1415 return ret; 1416 } 1417 1418 static int ocfs2_xa_check_space_helper(int needed_space, int free_start, 1419 int num_entries) 1420 { 1421 int free_space; 1422 1423 if (!needed_space) 1424 return 0; 1425 1426 free_space = free_start - 1427 sizeof(struct ocfs2_xattr_header) - 1428 (num_entries * sizeof(struct ocfs2_xattr_entry)) - 1429 OCFS2_XATTR_HEADER_GAP; 1430 if (free_space < 0) 1431 return -EIO; 1432 if (free_space < needed_space) 1433 return -ENOSPC; 1434 1435 return 0; 1436 } 1437 1438 static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc, 1439 int type) 1440 { 1441 return loc->xl_ops->xlo_journal_access(handle, loc, type); 1442 } 1443 1444 static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc) 1445 { 1446 loc->xl_ops->xlo_journal_dirty(handle, loc); 1447 } 1448 1449 /* Give a pointer into the storage for the given offset */ 1450 static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset) 1451 { 1452 BUG_ON(offset >= loc->xl_size); 1453 return loc->xl_ops->xlo_offset_pointer(loc, offset); 1454 } 1455 1456 /* 1457 * Wipe the name+value pair and allow the storage to reclaim it. This 1458 * must be followed by either removal of the entry or a call to 1459 * ocfs2_xa_add_namevalue(). 1460 */ 1461 static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc) 1462 { 1463 loc->xl_ops->xlo_wipe_namevalue(loc); 1464 } 1465 1466 /* 1467 * Find lowest offset to a name+value pair. This is the start of our 1468 * downward-growing free space. 1469 */ 1470 static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc) 1471 { 1472 return loc->xl_ops->xlo_get_free_start(loc); 1473 } 1474 1475 /* Can we reuse loc->xl_entry for xi? */ 1476 static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc, 1477 struct ocfs2_xattr_info *xi) 1478 { 1479 return loc->xl_ops->xlo_can_reuse(loc, xi); 1480 } 1481 1482 /* How much free space is needed to set the new value */ 1483 static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc, 1484 struct ocfs2_xattr_info *xi) 1485 { 1486 return loc->xl_ops->xlo_check_space(loc, xi); 1487 } 1488 1489 static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash) 1490 { 1491 loc->xl_ops->xlo_add_entry(loc, name_hash); 1492 loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash); 1493 /* 1494 * We can't leave the new entry's xe_name_offset at zero or 1495 * add_namevalue() will go nuts. We set it to the size of our 1496 * storage so that it can never be less than any other entry. 1497 */ 1498 loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size); 1499 } 1500 1501 static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc, 1502 struct ocfs2_xattr_info *xi) 1503 { 1504 int size = namevalue_size_xi(xi); 1505 int nameval_offset; 1506 char *nameval_buf; 1507 1508 loc->xl_ops->xlo_add_namevalue(loc, size); 1509 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len); 1510 loc->xl_entry->xe_name_len = xi->xi_name_len; 1511 ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index); 1512 ocfs2_xattr_set_local(loc->xl_entry, 1513 xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE); 1514 1515 nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset); 1516 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset); 1517 memset(nameval_buf, 0, size); 1518 memcpy(nameval_buf, xi->xi_name, xi->xi_name_len); 1519 } 1520 1521 static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc, 1522 struct ocfs2_xattr_value_buf *vb) 1523 { 1524 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset); 1525 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len); 1526 1527 /* Value bufs are for value trees */ 1528 BUG_ON(ocfs2_xattr_is_local(loc->xl_entry)); 1529 BUG_ON(namevalue_size_xe(loc->xl_entry) != 1530 (name_size + OCFS2_XATTR_ROOT_SIZE)); 1531 1532 loc->xl_ops->xlo_fill_value_buf(loc, vb); 1533 vb->vb_xv = 1534 (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc, 1535 nameval_offset + 1536 name_size); 1537 } 1538 1539 static int ocfs2_xa_block_journal_access(handle_t *handle, 1540 struct ocfs2_xa_loc *loc, int type) 1541 { 1542 struct buffer_head *bh = loc->xl_storage; 1543 ocfs2_journal_access_func access; 1544 1545 if (loc->xl_size == (bh->b_size - 1546 offsetof(struct ocfs2_xattr_block, 1547 xb_attrs.xb_header))) 1548 access = ocfs2_journal_access_xb; 1549 else 1550 access = ocfs2_journal_access_di; 1551 return access(handle, INODE_CACHE(loc->xl_inode), bh, type); 1552 } 1553 1554 static void ocfs2_xa_block_journal_dirty(handle_t *handle, 1555 struct ocfs2_xa_loc *loc) 1556 { 1557 struct buffer_head *bh = loc->xl_storage; 1558 1559 ocfs2_journal_dirty(handle, bh); 1560 } 1561 1562 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc, 1563 int offset) 1564 { 1565 return (char *)loc->xl_header + offset; 1566 } 1567 1568 static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc, 1569 struct ocfs2_xattr_info *xi) 1570 { 1571 /* 1572 * Block storage is strict. If the sizes aren't exact, we will 1573 * remove the old one and reinsert the new. 1574 */ 1575 return namevalue_size_xe(loc->xl_entry) == 1576 namevalue_size_xi(xi); 1577 } 1578 1579 static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc) 1580 { 1581 struct ocfs2_xattr_header *xh = loc->xl_header; 1582 int i, count = le16_to_cpu(xh->xh_count); 1583 int offset, free_start = loc->xl_size; 1584 1585 for (i = 0; i < count; i++) { 1586 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset); 1587 if (offset < free_start) 1588 free_start = offset; 1589 } 1590 1591 return free_start; 1592 } 1593 1594 static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc, 1595 struct ocfs2_xattr_info *xi) 1596 { 1597 int count = le16_to_cpu(loc->xl_header->xh_count); 1598 int free_start = ocfs2_xa_get_free_start(loc); 1599 int needed_space = ocfs2_xi_entry_usage(xi); 1600 1601 /* 1602 * Block storage will reclaim the original entry before inserting 1603 * the new value, so we only need the difference. If the new 1604 * entry is smaller than the old one, we don't need anything. 1605 */ 1606 if (loc->xl_entry) { 1607 /* Don't need space if we're reusing! */ 1608 if (ocfs2_xa_can_reuse_entry(loc, xi)) 1609 needed_space = 0; 1610 else 1611 needed_space -= ocfs2_xe_entry_usage(loc->xl_entry); 1612 } 1613 if (needed_space < 0) 1614 needed_space = 0; 1615 return ocfs2_xa_check_space_helper(needed_space, free_start, count); 1616 } 1617 1618 /* 1619 * Block storage for xattrs keeps the name+value pairs compacted. When 1620 * we remove one, we have to shift any that preceded it towards the end. 1621 */ 1622 static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc) 1623 { 1624 int i, offset; 1625 int namevalue_offset, first_namevalue_offset, namevalue_size; 1626 struct ocfs2_xattr_entry *entry = loc->xl_entry; 1627 struct ocfs2_xattr_header *xh = loc->xl_header; 1628 int count = le16_to_cpu(xh->xh_count); 1629 1630 namevalue_offset = le16_to_cpu(entry->xe_name_offset); 1631 namevalue_size = namevalue_size_xe(entry); 1632 first_namevalue_offset = ocfs2_xa_get_free_start(loc); 1633 1634 /* Shift the name+value pairs */ 1635 memmove((char *)xh + first_namevalue_offset + namevalue_size, 1636 (char *)xh + first_namevalue_offset, 1637 namevalue_offset - first_namevalue_offset); 1638 memset((char *)xh + first_namevalue_offset, 0, namevalue_size); 1639 1640 /* Now tell xh->xh_entries about it */ 1641 for (i = 0; i < count; i++) { 1642 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset); 1643 if (offset <= namevalue_offset) 1644 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, 1645 namevalue_size); 1646 } 1647 1648 /* 1649 * Note that we don't update xh_free_start or xh_name_value_len 1650 * because they're not used in block-stored xattrs. 1651 */ 1652 } 1653 1654 static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash) 1655 { 1656 int count = le16_to_cpu(loc->xl_header->xh_count); 1657 loc->xl_entry = &(loc->xl_header->xh_entries[count]); 1658 le16_add_cpu(&loc->xl_header->xh_count, 1); 1659 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry)); 1660 } 1661 1662 static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size) 1663 { 1664 int free_start = ocfs2_xa_get_free_start(loc); 1665 1666 loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size); 1667 } 1668 1669 static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc, 1670 struct ocfs2_xattr_value_buf *vb) 1671 { 1672 struct buffer_head *bh = loc->xl_storage; 1673 1674 if (loc->xl_size == (bh->b_size - 1675 offsetof(struct ocfs2_xattr_block, 1676 xb_attrs.xb_header))) 1677 vb->vb_access = ocfs2_journal_access_xb; 1678 else 1679 vb->vb_access = ocfs2_journal_access_di; 1680 vb->vb_bh = bh; 1681 } 1682 1683 /* 1684 * Operations for xattrs stored in blocks. This includes inline inode 1685 * storage and unindexed ocfs2_xattr_blocks. 1686 */ 1687 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = { 1688 .xlo_journal_access = ocfs2_xa_block_journal_access, 1689 .xlo_journal_dirty = ocfs2_xa_block_journal_dirty, 1690 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer, 1691 .xlo_check_space = ocfs2_xa_block_check_space, 1692 .xlo_can_reuse = ocfs2_xa_block_can_reuse, 1693 .xlo_get_free_start = ocfs2_xa_block_get_free_start, 1694 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue, 1695 .xlo_add_entry = ocfs2_xa_block_add_entry, 1696 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue, 1697 .xlo_fill_value_buf = ocfs2_xa_block_fill_value_buf, 1698 }; 1699 1700 static int ocfs2_xa_bucket_journal_access(handle_t *handle, 1701 struct ocfs2_xa_loc *loc, int type) 1702 { 1703 struct ocfs2_xattr_bucket *bucket = loc->xl_storage; 1704 1705 return ocfs2_xattr_bucket_journal_access(handle, bucket, type); 1706 } 1707 1708 static void ocfs2_xa_bucket_journal_dirty(handle_t *handle, 1709 struct ocfs2_xa_loc *loc) 1710 { 1711 struct ocfs2_xattr_bucket *bucket = loc->xl_storage; 1712 1713 ocfs2_xattr_bucket_journal_dirty(handle, bucket); 1714 } 1715 1716 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc, 1717 int offset) 1718 { 1719 struct ocfs2_xattr_bucket *bucket = loc->xl_storage; 1720 int block, block_offset; 1721 1722 /* The header is at the front of the bucket */ 1723 block = offset >> loc->xl_inode->i_sb->s_blocksize_bits; 1724 block_offset = offset % loc->xl_inode->i_sb->s_blocksize; 1725 1726 return bucket_block(bucket, block) + block_offset; 1727 } 1728 1729 static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc, 1730 struct ocfs2_xattr_info *xi) 1731 { 1732 return namevalue_size_xe(loc->xl_entry) >= 1733 namevalue_size_xi(xi); 1734 } 1735 1736 static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc) 1737 { 1738 struct ocfs2_xattr_bucket *bucket = loc->xl_storage; 1739 return le16_to_cpu(bucket_xh(bucket)->xh_free_start); 1740 } 1741 1742 static int ocfs2_bucket_align_free_start(struct super_block *sb, 1743 int free_start, int size) 1744 { 1745 /* 1746 * We need to make sure that the name+value pair fits within 1747 * one block. 1748 */ 1749 if (((free_start - size) >> sb->s_blocksize_bits) != 1750 ((free_start - 1) >> sb->s_blocksize_bits)) 1751 free_start -= free_start % sb->s_blocksize; 1752 1753 return free_start; 1754 } 1755 1756 static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc, 1757 struct ocfs2_xattr_info *xi) 1758 { 1759 int rc; 1760 int count = le16_to_cpu(loc->xl_header->xh_count); 1761 int free_start = ocfs2_xa_get_free_start(loc); 1762 int needed_space = ocfs2_xi_entry_usage(xi); 1763 int size = namevalue_size_xi(xi); 1764 struct super_block *sb = loc->xl_inode->i_sb; 1765 1766 /* 1767 * Bucket storage does not reclaim name+value pairs it cannot 1768 * reuse. They live as holes until the bucket fills, and then 1769 * the bucket is defragmented. However, the bucket can reclaim 1770 * the ocfs2_xattr_entry. 1771 */ 1772 if (loc->xl_entry) { 1773 /* Don't need space if we're reusing! */ 1774 if (ocfs2_xa_can_reuse_entry(loc, xi)) 1775 needed_space = 0; 1776 else 1777 needed_space -= sizeof(struct ocfs2_xattr_entry); 1778 } 1779 BUG_ON(needed_space < 0); 1780 1781 if (free_start < size) { 1782 if (needed_space) 1783 return -ENOSPC; 1784 } else { 1785 /* 1786 * First we check if it would fit in the first place. 1787 * Below, we align the free start to a block. This may 1788 * slide us below the minimum gap. By checking unaligned 1789 * first, we avoid that error. 1790 */ 1791 rc = ocfs2_xa_check_space_helper(needed_space, free_start, 1792 count); 1793 if (rc) 1794 return rc; 1795 free_start = ocfs2_bucket_align_free_start(sb, free_start, 1796 size); 1797 } 1798 return ocfs2_xa_check_space_helper(needed_space, free_start, count); 1799 } 1800 1801 static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc) 1802 { 1803 le16_add_cpu(&loc->xl_header->xh_name_value_len, 1804 -namevalue_size_xe(loc->xl_entry)); 1805 } 1806 1807 static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash) 1808 { 1809 struct ocfs2_xattr_header *xh = loc->xl_header; 1810 int count = le16_to_cpu(xh->xh_count); 1811 int low = 0, high = count - 1, tmp; 1812 struct ocfs2_xattr_entry *tmp_xe; 1813 1814 /* 1815 * We keep buckets sorted by name_hash, so we need to find 1816 * our insert place. 1817 */ 1818 while (low <= high && count) { 1819 tmp = (low + high) / 2; 1820 tmp_xe = &xh->xh_entries[tmp]; 1821 1822 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash)) 1823 low = tmp + 1; 1824 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash)) 1825 high = tmp - 1; 1826 else { 1827 low = tmp; 1828 break; 1829 } 1830 } 1831 1832 if (low != count) 1833 memmove(&xh->xh_entries[low + 1], 1834 &xh->xh_entries[low], 1835 ((count - low) * sizeof(struct ocfs2_xattr_entry))); 1836 1837 le16_add_cpu(&xh->xh_count, 1); 1838 loc->xl_entry = &xh->xh_entries[low]; 1839 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry)); 1840 } 1841 1842 static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size) 1843 { 1844 int free_start = ocfs2_xa_get_free_start(loc); 1845 struct ocfs2_xattr_header *xh = loc->xl_header; 1846 struct super_block *sb = loc->xl_inode->i_sb; 1847 int nameval_offset; 1848 1849 free_start = ocfs2_bucket_align_free_start(sb, free_start, size); 1850 nameval_offset = free_start - size; 1851 loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset); 1852 xh->xh_free_start = cpu_to_le16(nameval_offset); 1853 le16_add_cpu(&xh->xh_name_value_len, size); 1854 1855 } 1856 1857 static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc, 1858 struct ocfs2_xattr_value_buf *vb) 1859 { 1860 struct ocfs2_xattr_bucket *bucket = loc->xl_storage; 1861 struct super_block *sb = loc->xl_inode->i_sb; 1862 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset); 1863 int size = namevalue_size_xe(loc->xl_entry); 1864 int block_offset = nameval_offset >> sb->s_blocksize_bits; 1865 1866 /* Values are not allowed to straddle block boundaries */ 1867 BUG_ON(block_offset != 1868 ((nameval_offset + size - 1) >> sb->s_blocksize_bits)); 1869 /* We expect the bucket to be filled in */ 1870 BUG_ON(!bucket->bu_bhs[block_offset]); 1871 1872 vb->vb_access = ocfs2_journal_access; 1873 vb->vb_bh = bucket->bu_bhs[block_offset]; 1874 } 1875 1876 /* Operations for xattrs stored in buckets. */ 1877 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = { 1878 .xlo_journal_access = ocfs2_xa_bucket_journal_access, 1879 .xlo_journal_dirty = ocfs2_xa_bucket_journal_dirty, 1880 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer, 1881 .xlo_check_space = ocfs2_xa_bucket_check_space, 1882 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse, 1883 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start, 1884 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue, 1885 .xlo_add_entry = ocfs2_xa_bucket_add_entry, 1886 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue, 1887 .xlo_fill_value_buf = ocfs2_xa_bucket_fill_value_buf, 1888 }; 1889 1890 static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc *loc) 1891 { 1892 struct ocfs2_xattr_value_buf vb; 1893 1894 if (ocfs2_xattr_is_local(loc->xl_entry)) 1895 return 0; 1896 1897 ocfs2_xa_fill_value_buf(loc, &vb); 1898 return le32_to_cpu(vb.vb_xv->xr_clusters); 1899 } 1900 1901 static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes, 1902 struct ocfs2_xattr_set_ctxt *ctxt) 1903 { 1904 int trunc_rc, access_rc; 1905 struct ocfs2_xattr_value_buf vb; 1906 1907 ocfs2_xa_fill_value_buf(loc, &vb); 1908 trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes, 1909 ctxt); 1910 1911 /* 1912 * The caller of ocfs2_xa_value_truncate() has already called 1913 * ocfs2_xa_journal_access on the loc. However, The truncate code 1914 * calls ocfs2_extend_trans(). This may commit the previous 1915 * transaction and open a new one. If this is a bucket, truncate 1916 * could leave only vb->vb_bh set up for journaling. Meanwhile, 1917 * the caller is expecting to dirty the entire bucket. So we must 1918 * reset the journal work. We do this even if truncate has failed, 1919 * as it could have failed after committing the extend. 1920 */ 1921 access_rc = ocfs2_xa_journal_access(ctxt->handle, loc, 1922 OCFS2_JOURNAL_ACCESS_WRITE); 1923 1924 /* Errors in truncate take precedence */ 1925 return trunc_rc ? trunc_rc : access_rc; 1926 } 1927 1928 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc) 1929 { 1930 int index, count; 1931 struct ocfs2_xattr_header *xh = loc->xl_header; 1932 struct ocfs2_xattr_entry *entry = loc->xl_entry; 1933 1934 ocfs2_xa_wipe_namevalue(loc); 1935 loc->xl_entry = NULL; 1936 1937 le16_add_cpu(&xh->xh_count, -1); 1938 count = le16_to_cpu(xh->xh_count); 1939 1940 /* 1941 * Only zero out the entry if there are more remaining. This is 1942 * important for an empty bucket, as it keeps track of the 1943 * bucket's hash value. It doesn't hurt empty block storage. 1944 */ 1945 if (count) { 1946 index = ((char *)entry - (char *)&xh->xh_entries) / 1947 sizeof(struct ocfs2_xattr_entry); 1948 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1], 1949 (count - index) * sizeof(struct ocfs2_xattr_entry)); 1950 memset(&xh->xh_entries[count], 0, 1951 sizeof(struct ocfs2_xattr_entry)); 1952 } 1953 } 1954 1955 /* 1956 * If we have a problem adjusting the size of an external value during 1957 * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr 1958 * in an intermediate state. For example, the value may be partially 1959 * truncated. 1960 * 1961 * If the value tree hasn't changed, the extend/truncate went nowhere. 1962 * We have nothing to do. The caller can treat it as a straight error. 1963 * 1964 * If the value tree got partially truncated, we now have a corrupted 1965 * extended attribute. We're going to wipe its entry and leak the 1966 * clusters. Better to leak some storage than leave a corrupt entry. 1967 * 1968 * If the value tree grew, it obviously didn't grow enough for the 1969 * new entry. We're not going to try and reclaim those clusters either. 1970 * If there was already an external value there (orig_clusters != 0), 1971 * the new clusters are attached safely and we can just leave the old 1972 * value in place. If there was no external value there, we remove 1973 * the entry. 1974 * 1975 * This way, the xattr block we store in the journal will be consistent. 1976 * If the size change broke because of the journal, no changes will hit 1977 * disk anyway. 1978 */ 1979 static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc *loc, 1980 const char *what, 1981 unsigned int orig_clusters) 1982 { 1983 unsigned int new_clusters = ocfs2_xa_value_clusters(loc); 1984 char *nameval_buf = ocfs2_xa_offset_pointer(loc, 1985 le16_to_cpu(loc->xl_entry->xe_name_offset)); 1986 1987 if (new_clusters < orig_clusters) { 1988 mlog(ML_ERROR, 1989 "Partial truncate while %s xattr %.*s. Leaking " 1990 "%u clusters and removing the entry\n", 1991 what, loc->xl_entry->xe_name_len, nameval_buf, 1992 orig_clusters - new_clusters); 1993 ocfs2_xa_remove_entry(loc); 1994 } else if (!orig_clusters) { 1995 mlog(ML_ERROR, 1996 "Unable to allocate an external value for xattr " 1997 "%.*s safely. Leaking %u clusters and removing the " 1998 "entry\n", 1999 loc->xl_entry->xe_name_len, nameval_buf, 2000 new_clusters - orig_clusters); 2001 ocfs2_xa_remove_entry(loc); 2002 } else if (new_clusters > orig_clusters) 2003 mlog(ML_ERROR, 2004 "Unable to grow xattr %.*s safely. %u new clusters " 2005 "have been added, but the value will not be " 2006 "modified\n", 2007 loc->xl_entry->xe_name_len, nameval_buf, 2008 new_clusters - orig_clusters); 2009 } 2010 2011 static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc, 2012 struct ocfs2_xattr_set_ctxt *ctxt) 2013 { 2014 int rc = 0; 2015 unsigned int orig_clusters; 2016 2017 if (!ocfs2_xattr_is_local(loc->xl_entry)) { 2018 orig_clusters = ocfs2_xa_value_clusters(loc); 2019 rc = ocfs2_xa_value_truncate(loc, 0, ctxt); 2020 if (rc) { 2021 mlog_errno(rc); 2022 /* 2023 * Since this is remove, we can return 0 if 2024 * ocfs2_xa_cleanup_value_truncate() is going to 2025 * wipe the entry anyway. So we check the 2026 * cluster count as well. 2027 */ 2028 if (orig_clusters != ocfs2_xa_value_clusters(loc)) 2029 rc = 0; 2030 ocfs2_xa_cleanup_value_truncate(loc, "removing", 2031 orig_clusters); 2032 if (rc) 2033 goto out; 2034 } 2035 } 2036 2037 ocfs2_xa_remove_entry(loc); 2038 2039 out: 2040 return rc; 2041 } 2042 2043 static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc) 2044 { 2045 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len); 2046 char *nameval_buf; 2047 2048 nameval_buf = ocfs2_xa_offset_pointer(loc, 2049 le16_to_cpu(loc->xl_entry->xe_name_offset)); 2050 memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE); 2051 } 2052 2053 /* 2054 * Take an existing entry and make it ready for the new value. This 2055 * won't allocate space, but it may free space. It should be ready for 2056 * ocfs2_xa_prepare_entry() to finish the work. 2057 */ 2058 static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc, 2059 struct ocfs2_xattr_info *xi, 2060 struct ocfs2_xattr_set_ctxt *ctxt) 2061 { 2062 int rc = 0; 2063 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len); 2064 unsigned int orig_clusters; 2065 char *nameval_buf; 2066 int xe_local = ocfs2_xattr_is_local(loc->xl_entry); 2067 int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE; 2068 2069 BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) != 2070 name_size); 2071 2072 nameval_buf = ocfs2_xa_offset_pointer(loc, 2073 le16_to_cpu(loc->xl_entry->xe_name_offset)); 2074 if (xe_local) { 2075 memset(nameval_buf + name_size, 0, 2076 namevalue_size_xe(loc->xl_entry) - name_size); 2077 if (!xi_local) 2078 ocfs2_xa_install_value_root(loc); 2079 } else { 2080 orig_clusters = ocfs2_xa_value_clusters(loc); 2081 if (xi_local) { 2082 rc = ocfs2_xa_value_truncate(loc, 0, ctxt); 2083 if (rc < 0) 2084 mlog_errno(rc); 2085 else 2086 memset(nameval_buf + name_size, 0, 2087 namevalue_size_xe(loc->xl_entry) - 2088 name_size); 2089 } else if (le64_to_cpu(loc->xl_entry->xe_value_size) > 2090 xi->xi_value_len) { 2091 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, 2092 ctxt); 2093 if (rc < 0) 2094 mlog_errno(rc); 2095 } 2096 2097 if (rc) { 2098 ocfs2_xa_cleanup_value_truncate(loc, "reusing", 2099 orig_clusters); 2100 goto out; 2101 } 2102 } 2103 2104 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len); 2105 ocfs2_xattr_set_local(loc->xl_entry, xi_local); 2106 2107 out: 2108 return rc; 2109 } 2110 2111 /* 2112 * Prepares loc->xl_entry to receive the new xattr. This includes 2113 * properly setting up the name+value pair region. If loc->xl_entry 2114 * already exists, it will take care of modifying it appropriately. 2115 * 2116 * Note that this modifies the data. You did journal_access already, 2117 * right? 2118 */ 2119 static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc, 2120 struct ocfs2_xattr_info *xi, 2121 u32 name_hash, 2122 struct ocfs2_xattr_set_ctxt *ctxt) 2123 { 2124 int rc = 0; 2125 unsigned int orig_clusters; 2126 __le64 orig_value_size = 0; 2127 2128 rc = ocfs2_xa_check_space(loc, xi); 2129 if (rc) 2130 goto out; 2131 2132 if (loc->xl_entry) { 2133 if (ocfs2_xa_can_reuse_entry(loc, xi)) { 2134 orig_value_size = loc->xl_entry->xe_value_size; 2135 rc = ocfs2_xa_reuse_entry(loc, xi, ctxt); 2136 if (rc) 2137 goto out; 2138 goto alloc_value; 2139 } 2140 2141 if (!ocfs2_xattr_is_local(loc->xl_entry)) { 2142 orig_clusters = ocfs2_xa_value_clusters(loc); 2143 rc = ocfs2_xa_value_truncate(loc, 0, ctxt); 2144 if (rc) { 2145 mlog_errno(rc); 2146 ocfs2_xa_cleanup_value_truncate(loc, 2147 "overwriting", 2148 orig_clusters); 2149 goto out; 2150 } 2151 } 2152 ocfs2_xa_wipe_namevalue(loc); 2153 } else 2154 ocfs2_xa_add_entry(loc, name_hash); 2155 2156 /* 2157 * If we get here, we have a blank entry. Fill it. We grow our 2158 * name+value pair back from the end. 2159 */ 2160 ocfs2_xa_add_namevalue(loc, xi); 2161 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) 2162 ocfs2_xa_install_value_root(loc); 2163 2164 alloc_value: 2165 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) { 2166 orig_clusters = ocfs2_xa_value_clusters(loc); 2167 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt); 2168 if (rc < 0) { 2169 ctxt->set_abort = 1; 2170 ocfs2_xa_cleanup_value_truncate(loc, "growing", 2171 orig_clusters); 2172 /* 2173 * If we were growing an existing value, 2174 * ocfs2_xa_cleanup_value_truncate() won't remove 2175 * the entry. We need to restore the original value 2176 * size. 2177 */ 2178 if (loc->xl_entry) { 2179 BUG_ON(!orig_value_size); 2180 loc->xl_entry->xe_value_size = orig_value_size; 2181 } 2182 mlog_errno(rc); 2183 } 2184 } 2185 2186 out: 2187 return rc; 2188 } 2189 2190 /* 2191 * Store the value portion of the name+value pair. This will skip 2192 * values that are stored externally. Their tree roots were set up 2193 * by ocfs2_xa_prepare_entry(). 2194 */ 2195 static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc, 2196 struct ocfs2_xattr_info *xi, 2197 struct ocfs2_xattr_set_ctxt *ctxt) 2198 { 2199 int rc = 0; 2200 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset); 2201 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len); 2202 char *nameval_buf; 2203 struct ocfs2_xattr_value_buf vb; 2204 2205 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset); 2206 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) { 2207 ocfs2_xa_fill_value_buf(loc, &vb); 2208 rc = __ocfs2_xattr_set_value_outside(loc->xl_inode, 2209 ctxt->handle, &vb, 2210 xi->xi_value, 2211 xi->xi_value_len); 2212 } else 2213 memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len); 2214 2215 return rc; 2216 } 2217 2218 static int ocfs2_xa_set(struct ocfs2_xa_loc *loc, 2219 struct ocfs2_xattr_info *xi, 2220 struct ocfs2_xattr_set_ctxt *ctxt) 2221 { 2222 int ret; 2223 u32 name_hash = ocfs2_xattr_name_hash(loc->xl_inode, xi->xi_name, 2224 xi->xi_name_len); 2225 2226 ret = ocfs2_xa_journal_access(ctxt->handle, loc, 2227 OCFS2_JOURNAL_ACCESS_WRITE); 2228 if (ret) { 2229 mlog_errno(ret); 2230 goto out; 2231 } 2232 2233 /* 2234 * From here on out, everything is going to modify the buffer a 2235 * little. Errors are going to leave the xattr header in a 2236 * sane state. Thus, even with errors we dirty the sucker. 2237 */ 2238 2239 /* Don't worry, we are never called with !xi_value and !xl_entry */ 2240 if (!xi->xi_value) { 2241 ret = ocfs2_xa_remove(loc, ctxt); 2242 goto out_dirty; 2243 } 2244 2245 ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt); 2246 if (ret) { 2247 if (ret != -ENOSPC) 2248 mlog_errno(ret); 2249 goto out_dirty; 2250 } 2251 2252 ret = ocfs2_xa_store_value(loc, xi, ctxt); 2253 if (ret) 2254 mlog_errno(ret); 2255 2256 out_dirty: 2257 ocfs2_xa_journal_dirty(ctxt->handle, loc); 2258 2259 out: 2260 return ret; 2261 } 2262 2263 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc, 2264 struct inode *inode, 2265 struct buffer_head *bh, 2266 struct ocfs2_xattr_entry *entry) 2267 { 2268 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data; 2269 2270 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL)); 2271 2272 loc->xl_inode = inode; 2273 loc->xl_ops = &ocfs2_xa_block_loc_ops; 2274 loc->xl_storage = bh; 2275 loc->xl_entry = entry; 2276 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size); 2277 loc->xl_header = 2278 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size - 2279 loc->xl_size); 2280 } 2281 2282 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc, 2283 struct inode *inode, 2284 struct buffer_head *bh, 2285 struct ocfs2_xattr_entry *entry) 2286 { 2287 struct ocfs2_xattr_block *xb = 2288 (struct ocfs2_xattr_block *)bh->b_data; 2289 2290 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED); 2291 2292 loc->xl_inode = inode; 2293 loc->xl_ops = &ocfs2_xa_block_loc_ops; 2294 loc->xl_storage = bh; 2295 loc->xl_header = &(xb->xb_attrs.xb_header); 2296 loc->xl_entry = entry; 2297 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block, 2298 xb_attrs.xb_header); 2299 } 2300 2301 static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc, 2302 struct ocfs2_xattr_bucket *bucket, 2303 struct ocfs2_xattr_entry *entry) 2304 { 2305 loc->xl_inode = bucket->bu_inode; 2306 loc->xl_ops = &ocfs2_xa_bucket_loc_ops; 2307 loc->xl_storage = bucket; 2308 loc->xl_header = bucket_xh(bucket); 2309 loc->xl_entry = entry; 2310 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE; 2311 } 2312 2313 /* 2314 * In xattr remove, if it is stored outside and refcounted, we may have 2315 * the chance to split the refcount tree. So need the allocators. 2316 */ 2317 static int ocfs2_lock_xattr_remove_allocators(struct inode *inode, 2318 struct ocfs2_xattr_value_root *xv, 2319 struct ocfs2_caching_info *ref_ci, 2320 struct buffer_head *ref_root_bh, 2321 struct ocfs2_alloc_context **meta_ac, 2322 int *ref_credits) 2323 { 2324 int ret, meta_add = 0; 2325 u32 p_cluster, num_clusters; 2326 unsigned int ext_flags; 2327 2328 *ref_credits = 0; 2329 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster, 2330 &num_clusters, 2331 &xv->xr_list, 2332 &ext_flags); 2333 if (ret) { 2334 mlog_errno(ret); 2335 goto out; 2336 } 2337 2338 if (!(ext_flags & OCFS2_EXT_REFCOUNTED)) 2339 goto out; 2340 2341 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci, 2342 ref_root_bh, xv, 2343 &meta_add, ref_credits); 2344 if (ret) { 2345 mlog_errno(ret); 2346 goto out; 2347 } 2348 2349 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb), 2350 meta_add, meta_ac); 2351 if (ret) 2352 mlog_errno(ret); 2353 2354 out: 2355 return ret; 2356 } 2357 2358 static int ocfs2_remove_value_outside(struct inode*inode, 2359 struct ocfs2_xattr_value_buf *vb, 2360 struct ocfs2_xattr_header *header, 2361 struct ocfs2_caching_info *ref_ci, 2362 struct buffer_head *ref_root_bh) 2363 { 2364 int ret = 0, i, ref_credits; 2365 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2366 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, }; 2367 void *val; 2368 2369 ocfs2_init_dealloc_ctxt(&ctxt.dealloc); 2370 2371 for (i = 0; i < le16_to_cpu(header->xh_count); i++) { 2372 struct ocfs2_xattr_entry *entry = &header->xh_entries[i]; 2373 2374 if (ocfs2_xattr_is_local(entry)) 2375 continue; 2376 2377 val = (void *)header + 2378 le16_to_cpu(entry->xe_name_offset); 2379 vb->vb_xv = (struct ocfs2_xattr_value_root *) 2380 (val + OCFS2_XATTR_SIZE(entry->xe_name_len)); 2381 2382 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv, 2383 ref_ci, ref_root_bh, 2384 &ctxt.meta_ac, 2385 &ref_credits); 2386 2387 ctxt.handle = ocfs2_start_trans(osb, ref_credits + 2388 ocfs2_remove_extent_credits(osb->sb)); 2389 if (IS_ERR(ctxt.handle)) { 2390 ret = PTR_ERR(ctxt.handle); 2391 mlog_errno(ret); 2392 break; 2393 } 2394 2395 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt); 2396 2397 ocfs2_commit_trans(osb, ctxt.handle); 2398 if (ctxt.meta_ac) { 2399 ocfs2_free_alloc_context(ctxt.meta_ac); 2400 ctxt.meta_ac = NULL; 2401 } 2402 2403 if (ret < 0) { 2404 mlog_errno(ret); 2405 break; 2406 } 2407 2408 } 2409 2410 if (ctxt.meta_ac) 2411 ocfs2_free_alloc_context(ctxt.meta_ac); 2412 ocfs2_schedule_truncate_log_flush(osb, 1); 2413 ocfs2_run_deallocs(osb, &ctxt.dealloc); 2414 return ret; 2415 } 2416 2417 static int ocfs2_xattr_ibody_remove(struct inode *inode, 2418 struct buffer_head *di_bh, 2419 struct ocfs2_caching_info *ref_ci, 2420 struct buffer_head *ref_root_bh) 2421 { 2422 2423 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 2424 struct ocfs2_xattr_header *header; 2425 int ret; 2426 struct ocfs2_xattr_value_buf vb = { 2427 .vb_bh = di_bh, 2428 .vb_access = ocfs2_journal_access_di, 2429 }; 2430 2431 header = (struct ocfs2_xattr_header *) 2432 ((void *)di + inode->i_sb->s_blocksize - 2433 le16_to_cpu(di->i_xattr_inline_size)); 2434 2435 ret = ocfs2_remove_value_outside(inode, &vb, header, 2436 ref_ci, ref_root_bh); 2437 2438 return ret; 2439 } 2440 2441 struct ocfs2_rm_xattr_bucket_para { 2442 struct ocfs2_caching_info *ref_ci; 2443 struct buffer_head *ref_root_bh; 2444 }; 2445 2446 static int ocfs2_xattr_block_remove(struct inode *inode, 2447 struct buffer_head *blk_bh, 2448 struct ocfs2_caching_info *ref_ci, 2449 struct buffer_head *ref_root_bh) 2450 { 2451 struct ocfs2_xattr_block *xb; 2452 int ret = 0; 2453 struct ocfs2_xattr_value_buf vb = { 2454 .vb_bh = blk_bh, 2455 .vb_access = ocfs2_journal_access_xb, 2456 }; 2457 struct ocfs2_rm_xattr_bucket_para args = { 2458 .ref_ci = ref_ci, 2459 .ref_root_bh = ref_root_bh, 2460 }; 2461 2462 xb = (struct ocfs2_xattr_block *)blk_bh->b_data; 2463 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { 2464 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header); 2465 ret = ocfs2_remove_value_outside(inode, &vb, header, 2466 ref_ci, ref_root_bh); 2467 } else 2468 ret = ocfs2_iterate_xattr_index_block(inode, 2469 blk_bh, 2470 ocfs2_rm_xattr_cluster, 2471 &args); 2472 2473 return ret; 2474 } 2475 2476 static int ocfs2_xattr_free_block(struct inode *inode, 2477 u64 block, 2478 struct ocfs2_caching_info *ref_ci, 2479 struct buffer_head *ref_root_bh) 2480 { 2481 struct inode *xb_alloc_inode; 2482 struct buffer_head *xb_alloc_bh = NULL; 2483 struct buffer_head *blk_bh = NULL; 2484 struct ocfs2_xattr_block *xb; 2485 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2486 handle_t *handle; 2487 int ret = 0; 2488 u64 blk, bg_blkno; 2489 u16 bit; 2490 2491 ret = ocfs2_read_xattr_block(inode, block, &blk_bh); 2492 if (ret < 0) { 2493 mlog_errno(ret); 2494 goto out; 2495 } 2496 2497 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh); 2498 if (ret < 0) { 2499 mlog_errno(ret); 2500 goto out; 2501 } 2502 2503 xb = (struct ocfs2_xattr_block *)blk_bh->b_data; 2504 blk = le64_to_cpu(xb->xb_blkno); 2505 bit = le16_to_cpu(xb->xb_suballoc_bit); 2506 if (xb->xb_suballoc_loc) 2507 bg_blkno = le64_to_cpu(xb->xb_suballoc_loc); 2508 else 2509 bg_blkno = ocfs2_which_suballoc_group(blk, bit); 2510 2511 xb_alloc_inode = ocfs2_get_system_file_inode(osb, 2512 EXTENT_ALLOC_SYSTEM_INODE, 2513 le16_to_cpu(xb->xb_suballoc_slot)); 2514 if (!xb_alloc_inode) { 2515 ret = -ENOMEM; 2516 mlog_errno(ret); 2517 goto out; 2518 } 2519 inode_lock(xb_alloc_inode); 2520 2521 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1); 2522 if (ret < 0) { 2523 mlog_errno(ret); 2524 goto out_mutex; 2525 } 2526 2527 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE); 2528 if (IS_ERR(handle)) { 2529 ret = PTR_ERR(handle); 2530 mlog_errno(ret); 2531 goto out_unlock; 2532 } 2533 2534 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh, 2535 bit, bg_blkno, 1); 2536 if (ret < 0) 2537 mlog_errno(ret); 2538 2539 ocfs2_commit_trans(osb, handle); 2540 out_unlock: 2541 ocfs2_inode_unlock(xb_alloc_inode, 1); 2542 brelse(xb_alloc_bh); 2543 out_mutex: 2544 inode_unlock(xb_alloc_inode); 2545 iput(xb_alloc_inode); 2546 out: 2547 brelse(blk_bh); 2548 return ret; 2549 } 2550 2551 /* 2552 * ocfs2_xattr_remove() 2553 * 2554 * Free extended attribute resources associated with this inode. 2555 */ 2556 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh) 2557 { 2558 struct ocfs2_inode_info *oi = OCFS2_I(inode); 2559 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 2560 struct ocfs2_refcount_tree *ref_tree = NULL; 2561 struct buffer_head *ref_root_bh = NULL; 2562 struct ocfs2_caching_info *ref_ci = NULL; 2563 handle_t *handle; 2564 int ret; 2565 2566 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) 2567 return 0; 2568 2569 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) 2570 return 0; 2571 2572 if (ocfs2_is_refcount_inode(inode)) { 2573 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb), 2574 le64_to_cpu(di->i_refcount_loc), 2575 1, &ref_tree, &ref_root_bh); 2576 if (ret) { 2577 mlog_errno(ret); 2578 goto out; 2579 } 2580 ref_ci = &ref_tree->rf_ci; 2581 2582 } 2583 2584 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) { 2585 ret = ocfs2_xattr_ibody_remove(inode, di_bh, 2586 ref_ci, ref_root_bh); 2587 if (ret < 0) { 2588 mlog_errno(ret); 2589 goto out; 2590 } 2591 } 2592 2593 if (di->i_xattr_loc) { 2594 ret = ocfs2_xattr_free_block(inode, 2595 le64_to_cpu(di->i_xattr_loc), 2596 ref_ci, ref_root_bh); 2597 if (ret < 0) { 2598 mlog_errno(ret); 2599 goto out; 2600 } 2601 } 2602 2603 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 2604 OCFS2_INODE_UPDATE_CREDITS); 2605 if (IS_ERR(handle)) { 2606 ret = PTR_ERR(handle); 2607 mlog_errno(ret); 2608 goto out; 2609 } 2610 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, 2611 OCFS2_JOURNAL_ACCESS_WRITE); 2612 if (ret) { 2613 mlog_errno(ret); 2614 goto out_commit; 2615 } 2616 2617 di->i_xattr_loc = 0; 2618 2619 spin_lock(&oi->ip_lock); 2620 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL); 2621 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); 2622 spin_unlock(&oi->ip_lock); 2623 ocfs2_update_inode_fsync_trans(handle, inode, 0); 2624 2625 ocfs2_journal_dirty(handle, di_bh); 2626 out_commit: 2627 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); 2628 out: 2629 if (ref_tree) 2630 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1); 2631 brelse(ref_root_bh); 2632 return ret; 2633 } 2634 2635 static int ocfs2_xattr_has_space_inline(struct inode *inode, 2636 struct ocfs2_dinode *di) 2637 { 2638 struct ocfs2_inode_info *oi = OCFS2_I(inode); 2639 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size; 2640 int free; 2641 2642 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE) 2643 return 0; 2644 2645 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) { 2646 struct ocfs2_inline_data *idata = &di->id2.i_data; 2647 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size); 2648 } else if (ocfs2_inode_is_fast_symlink(inode)) { 2649 free = ocfs2_fast_symlink_chars(inode->i_sb) - 2650 le64_to_cpu(di->i_size); 2651 } else { 2652 struct ocfs2_extent_list *el = &di->id2.i_list; 2653 free = (le16_to_cpu(el->l_count) - 2654 le16_to_cpu(el->l_next_free_rec)) * 2655 sizeof(struct ocfs2_extent_rec); 2656 } 2657 if (free >= xattrsize) 2658 return 1; 2659 2660 return 0; 2661 } 2662 2663 /* 2664 * ocfs2_xattr_ibody_find() 2665 * 2666 * Find extended attribute in inode block and 2667 * fill search info into struct ocfs2_xattr_search. 2668 */ 2669 static int ocfs2_xattr_ibody_find(struct inode *inode, 2670 int name_index, 2671 const char *name, 2672 struct ocfs2_xattr_search *xs) 2673 { 2674 struct ocfs2_inode_info *oi = OCFS2_I(inode); 2675 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; 2676 int ret; 2677 int has_space = 0; 2678 2679 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) 2680 return 0; 2681 2682 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) { 2683 down_read(&oi->ip_alloc_sem); 2684 has_space = ocfs2_xattr_has_space_inline(inode, di); 2685 up_read(&oi->ip_alloc_sem); 2686 if (!has_space) 2687 return 0; 2688 } 2689 2690 xs->xattr_bh = xs->inode_bh; 2691 xs->end = (void *)di + inode->i_sb->s_blocksize; 2692 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) 2693 xs->header = (struct ocfs2_xattr_header *) 2694 (xs->end - le16_to_cpu(di->i_xattr_inline_size)); 2695 else 2696 xs->header = (struct ocfs2_xattr_header *) 2697 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size); 2698 xs->base = (void *)xs->header; 2699 xs->here = xs->header->xh_entries; 2700 2701 /* Find the named attribute. */ 2702 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) { 2703 ret = ocfs2_xattr_find_entry(name_index, name, xs); 2704 if (ret && ret != -ENODATA) 2705 return ret; 2706 xs->not_found = ret; 2707 } 2708 2709 return 0; 2710 } 2711 2712 static int ocfs2_xattr_ibody_init(struct inode *inode, 2713 struct buffer_head *di_bh, 2714 struct ocfs2_xattr_set_ctxt *ctxt) 2715 { 2716 int ret; 2717 struct ocfs2_inode_info *oi = OCFS2_I(inode); 2718 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 2719 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2720 unsigned int xattrsize = osb->s_xattr_inline_size; 2721 2722 if (!ocfs2_xattr_has_space_inline(inode, di)) { 2723 ret = -ENOSPC; 2724 goto out; 2725 } 2726 2727 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), di_bh, 2728 OCFS2_JOURNAL_ACCESS_WRITE); 2729 if (ret) { 2730 mlog_errno(ret); 2731 goto out; 2732 } 2733 2734 /* 2735 * Adjust extent record count or inline data size 2736 * to reserve space for extended attribute. 2737 */ 2738 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) { 2739 struct ocfs2_inline_data *idata = &di->id2.i_data; 2740 le16_add_cpu(&idata->id_count, -xattrsize); 2741 } else if (!(ocfs2_inode_is_fast_symlink(inode))) { 2742 struct ocfs2_extent_list *el = &di->id2.i_list; 2743 le16_add_cpu(&el->l_count, -(xattrsize / 2744 sizeof(struct ocfs2_extent_rec))); 2745 } 2746 di->i_xattr_inline_size = cpu_to_le16(xattrsize); 2747 2748 spin_lock(&oi->ip_lock); 2749 oi->ip_dyn_features |= OCFS2_INLINE_XATTR_FL|OCFS2_HAS_XATTR_FL; 2750 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); 2751 spin_unlock(&oi->ip_lock); 2752 2753 ocfs2_journal_dirty(ctxt->handle, di_bh); 2754 2755 out: 2756 return ret; 2757 } 2758 2759 /* 2760 * ocfs2_xattr_ibody_set() 2761 * 2762 * Set, replace or remove an extended attribute into inode block. 2763 * 2764 */ 2765 static int ocfs2_xattr_ibody_set(struct inode *inode, 2766 struct ocfs2_xattr_info *xi, 2767 struct ocfs2_xattr_search *xs, 2768 struct ocfs2_xattr_set_ctxt *ctxt) 2769 { 2770 int ret; 2771 struct ocfs2_inode_info *oi = OCFS2_I(inode); 2772 struct ocfs2_xa_loc loc; 2773 2774 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) 2775 return -ENOSPC; 2776 2777 down_write(&oi->ip_alloc_sem); 2778 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) { 2779 ret = ocfs2_xattr_ibody_init(inode, xs->inode_bh, ctxt); 2780 if (ret) { 2781 if (ret != -ENOSPC) 2782 mlog_errno(ret); 2783 goto out; 2784 } 2785 } 2786 2787 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh, 2788 xs->not_found ? NULL : xs->here); 2789 ret = ocfs2_xa_set(&loc, xi, ctxt); 2790 if (ret) { 2791 if (ret != -ENOSPC) 2792 mlog_errno(ret); 2793 goto out; 2794 } 2795 xs->here = loc.xl_entry; 2796 2797 out: 2798 up_write(&oi->ip_alloc_sem); 2799 2800 return ret; 2801 } 2802 2803 /* 2804 * ocfs2_xattr_block_find() 2805 * 2806 * Find extended attribute in external block and 2807 * fill search info into struct ocfs2_xattr_search. 2808 */ 2809 static int ocfs2_xattr_block_find(struct inode *inode, 2810 int name_index, 2811 const char *name, 2812 struct ocfs2_xattr_search *xs) 2813 { 2814 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; 2815 struct buffer_head *blk_bh = NULL; 2816 struct ocfs2_xattr_block *xb; 2817 int ret = 0; 2818 2819 if (!di->i_xattr_loc) 2820 return ret; 2821 2822 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc), 2823 &blk_bh); 2824 if (ret < 0) { 2825 mlog_errno(ret); 2826 return ret; 2827 } 2828 2829 xs->xattr_bh = blk_bh; 2830 xb = (struct ocfs2_xattr_block *)blk_bh->b_data; 2831 2832 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { 2833 xs->header = &xb->xb_attrs.xb_header; 2834 xs->base = (void *)xs->header; 2835 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size; 2836 xs->here = xs->header->xh_entries; 2837 2838 ret = ocfs2_xattr_find_entry(name_index, name, xs); 2839 } else 2840 ret = ocfs2_xattr_index_block_find(inode, blk_bh, 2841 name_index, 2842 name, xs); 2843 2844 if (ret && ret != -ENODATA) { 2845 xs->xattr_bh = NULL; 2846 goto cleanup; 2847 } 2848 xs->not_found = ret; 2849 return 0; 2850 cleanup: 2851 brelse(blk_bh); 2852 2853 return ret; 2854 } 2855 2856 static int ocfs2_create_xattr_block(struct inode *inode, 2857 struct buffer_head *inode_bh, 2858 struct ocfs2_xattr_set_ctxt *ctxt, 2859 int indexed, 2860 struct buffer_head **ret_bh) 2861 { 2862 int ret; 2863 u16 suballoc_bit_start; 2864 u32 num_got; 2865 u64 suballoc_loc, first_blkno; 2866 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data; 2867 struct buffer_head *new_bh = NULL; 2868 struct ocfs2_xattr_block *xblk; 2869 2870 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), 2871 inode_bh, OCFS2_JOURNAL_ACCESS_CREATE); 2872 if (ret < 0) { 2873 mlog_errno(ret); 2874 goto end; 2875 } 2876 2877 ret = ocfs2_claim_metadata(ctxt->handle, ctxt->meta_ac, 1, 2878 &suballoc_loc, &suballoc_bit_start, 2879 &num_got, &first_blkno); 2880 if (ret < 0) { 2881 mlog_errno(ret); 2882 goto end; 2883 } 2884 2885 new_bh = sb_getblk(inode->i_sb, first_blkno); 2886 if (!new_bh) { 2887 ret = -ENOMEM; 2888 mlog_errno(ret); 2889 goto end; 2890 } 2891 2892 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh); 2893 2894 ret = ocfs2_journal_access_xb(ctxt->handle, INODE_CACHE(inode), 2895 new_bh, 2896 OCFS2_JOURNAL_ACCESS_CREATE); 2897 if (ret < 0) { 2898 mlog_errno(ret); 2899 goto end; 2900 } 2901 2902 /* Initialize ocfs2_xattr_block */ 2903 xblk = (struct ocfs2_xattr_block *)new_bh->b_data; 2904 memset(xblk, 0, inode->i_sb->s_blocksize); 2905 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE); 2906 xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot); 2907 xblk->xb_suballoc_loc = cpu_to_le64(suballoc_loc); 2908 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start); 2909 xblk->xb_fs_generation = 2910 cpu_to_le32(OCFS2_SB(inode->i_sb)->fs_generation); 2911 xblk->xb_blkno = cpu_to_le64(first_blkno); 2912 if (indexed) { 2913 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root; 2914 xr->xt_clusters = cpu_to_le32(1); 2915 xr->xt_last_eb_blk = 0; 2916 xr->xt_list.l_tree_depth = 0; 2917 xr->xt_list.l_count = cpu_to_le16( 2918 ocfs2_xattr_recs_per_xb(inode->i_sb)); 2919 xr->xt_list.l_next_free_rec = cpu_to_le16(1); 2920 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED); 2921 } 2922 ocfs2_journal_dirty(ctxt->handle, new_bh); 2923 2924 /* Add it to the inode */ 2925 di->i_xattr_loc = cpu_to_le64(first_blkno); 2926 2927 spin_lock(&OCFS2_I(inode)->ip_lock); 2928 OCFS2_I(inode)->ip_dyn_features |= OCFS2_HAS_XATTR_FL; 2929 di->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features); 2930 spin_unlock(&OCFS2_I(inode)->ip_lock); 2931 2932 ocfs2_journal_dirty(ctxt->handle, inode_bh); 2933 2934 *ret_bh = new_bh; 2935 new_bh = NULL; 2936 2937 end: 2938 brelse(new_bh); 2939 return ret; 2940 } 2941 2942 /* 2943 * ocfs2_xattr_block_set() 2944 * 2945 * Set, replace or remove an extended attribute into external block. 2946 * 2947 */ 2948 static int ocfs2_xattr_block_set(struct inode *inode, 2949 struct ocfs2_xattr_info *xi, 2950 struct ocfs2_xattr_search *xs, 2951 struct ocfs2_xattr_set_ctxt *ctxt) 2952 { 2953 struct buffer_head *new_bh = NULL; 2954 struct ocfs2_xattr_block *xblk = NULL; 2955 int ret; 2956 struct ocfs2_xa_loc loc; 2957 2958 if (!xs->xattr_bh) { 2959 ret = ocfs2_create_xattr_block(inode, xs->inode_bh, ctxt, 2960 0, &new_bh); 2961 if (ret) { 2962 mlog_errno(ret); 2963 goto end; 2964 } 2965 2966 xs->xattr_bh = new_bh; 2967 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; 2968 xs->header = &xblk->xb_attrs.xb_header; 2969 xs->base = (void *)xs->header; 2970 xs->end = (void *)xblk + inode->i_sb->s_blocksize; 2971 xs->here = xs->header->xh_entries; 2972 } else 2973 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; 2974 2975 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) { 2976 ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh, 2977 xs->not_found ? NULL : xs->here); 2978 2979 ret = ocfs2_xa_set(&loc, xi, ctxt); 2980 if (!ret) 2981 xs->here = loc.xl_entry; 2982 else if ((ret != -ENOSPC) || ctxt->set_abort) 2983 goto end; 2984 else { 2985 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt); 2986 if (ret) 2987 goto end; 2988 } 2989 } 2990 2991 if (le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED) 2992 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt); 2993 2994 end: 2995 return ret; 2996 } 2997 2998 /* Check whether the new xattr can be inserted into the inode. */ 2999 static int ocfs2_xattr_can_be_in_inode(struct inode *inode, 3000 struct ocfs2_xattr_info *xi, 3001 struct ocfs2_xattr_search *xs) 3002 { 3003 struct ocfs2_xattr_entry *last; 3004 int free, i; 3005 size_t min_offs = xs->end - xs->base; 3006 3007 if (!xs->header) 3008 return 0; 3009 3010 last = xs->header->xh_entries; 3011 3012 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) { 3013 size_t offs = le16_to_cpu(last->xe_name_offset); 3014 if (offs < min_offs) 3015 min_offs = offs; 3016 last += 1; 3017 } 3018 3019 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP; 3020 if (free < 0) 3021 return 0; 3022 3023 BUG_ON(!xs->not_found); 3024 3025 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi))) 3026 return 1; 3027 3028 return 0; 3029 } 3030 3031 static int ocfs2_calc_xattr_set_need(struct inode *inode, 3032 struct ocfs2_dinode *di, 3033 struct ocfs2_xattr_info *xi, 3034 struct ocfs2_xattr_search *xis, 3035 struct ocfs2_xattr_search *xbs, 3036 int *clusters_need, 3037 int *meta_need, 3038 int *credits_need) 3039 { 3040 int ret = 0, old_in_xb = 0; 3041 int clusters_add = 0, meta_add = 0, credits = 0; 3042 struct buffer_head *bh = NULL; 3043 struct ocfs2_xattr_block *xb = NULL; 3044 struct ocfs2_xattr_entry *xe = NULL; 3045 struct ocfs2_xattr_value_root *xv = NULL; 3046 char *base = NULL; 3047 int name_offset, name_len = 0; 3048 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, 3049 xi->xi_value_len); 3050 u64 value_size; 3051 3052 /* 3053 * Calculate the clusters we need to write. 3054 * No matter whether we replace an old one or add a new one, 3055 * we need this for writing. 3056 */ 3057 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) 3058 credits += new_clusters * 3059 ocfs2_clusters_to_blocks(inode->i_sb, 1); 3060 3061 if (xis->not_found && xbs->not_found) { 3062 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb); 3063 3064 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) { 3065 clusters_add += new_clusters; 3066 credits += ocfs2_calc_extend_credits(inode->i_sb, 3067 &def_xv.xv.xr_list); 3068 } 3069 3070 goto meta_guess; 3071 } 3072 3073 if (!xis->not_found) { 3074 xe = xis->here; 3075 name_offset = le16_to_cpu(xe->xe_name_offset); 3076 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len); 3077 base = xis->base; 3078 credits += OCFS2_INODE_UPDATE_CREDITS; 3079 } else { 3080 int i, block_off = 0; 3081 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data; 3082 xe = xbs->here; 3083 name_offset = le16_to_cpu(xe->xe_name_offset); 3084 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len); 3085 i = xbs->here - xbs->header->xh_entries; 3086 old_in_xb = 1; 3087 3088 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) { 3089 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, 3090 bucket_xh(xbs->bucket), 3091 i, &block_off, 3092 &name_offset); 3093 base = bucket_block(xbs->bucket, block_off); 3094 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb); 3095 } else { 3096 base = xbs->base; 3097 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS; 3098 } 3099 } 3100 3101 /* 3102 * delete a xattr doesn't need metadata and cluster allocation. 3103 * so just calculate the credits and return. 3104 * 3105 * The credits for removing the value tree will be extended 3106 * by ocfs2_remove_extent itself. 3107 */ 3108 if (!xi->xi_value) { 3109 if (!ocfs2_xattr_is_local(xe)) 3110 credits += ocfs2_remove_extent_credits(inode->i_sb); 3111 3112 goto out; 3113 } 3114 3115 /* do cluster allocation guess first. */ 3116 value_size = le64_to_cpu(xe->xe_value_size); 3117 3118 if (old_in_xb) { 3119 /* 3120 * In xattr set, we always try to set the xe in inode first, 3121 * so if it can be inserted into inode successfully, the old 3122 * one will be removed from the xattr block, and this xattr 3123 * will be inserted into inode as a new xattr in inode. 3124 */ 3125 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) { 3126 clusters_add += new_clusters; 3127 credits += ocfs2_remove_extent_credits(inode->i_sb) + 3128 OCFS2_INODE_UPDATE_CREDITS; 3129 if (!ocfs2_xattr_is_local(xe)) 3130 credits += ocfs2_calc_extend_credits( 3131 inode->i_sb, 3132 &def_xv.xv.xr_list); 3133 goto out; 3134 } 3135 } 3136 3137 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) { 3138 /* the new values will be stored outside. */ 3139 u32 old_clusters = 0; 3140 3141 if (!ocfs2_xattr_is_local(xe)) { 3142 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb, 3143 value_size); 3144 xv = (struct ocfs2_xattr_value_root *) 3145 (base + name_offset + name_len); 3146 value_size = OCFS2_XATTR_ROOT_SIZE; 3147 } else 3148 xv = &def_xv.xv; 3149 3150 if (old_clusters >= new_clusters) { 3151 credits += ocfs2_remove_extent_credits(inode->i_sb); 3152 goto out; 3153 } else { 3154 meta_add += ocfs2_extend_meta_needed(&xv->xr_list); 3155 clusters_add += new_clusters - old_clusters; 3156 credits += ocfs2_calc_extend_credits(inode->i_sb, 3157 &xv->xr_list); 3158 if (value_size >= OCFS2_XATTR_ROOT_SIZE) 3159 goto out; 3160 } 3161 } else { 3162 /* 3163 * Now the new value will be stored inside. So if the new 3164 * value is smaller than the size of value root or the old 3165 * value, we don't need any allocation, otherwise we have 3166 * to guess metadata allocation. 3167 */ 3168 if ((ocfs2_xattr_is_local(xe) && 3169 (value_size >= xi->xi_value_len)) || 3170 (!ocfs2_xattr_is_local(xe) && 3171 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len)) 3172 goto out; 3173 } 3174 3175 meta_guess: 3176 /* calculate metadata allocation. */ 3177 if (di->i_xattr_loc) { 3178 if (!xbs->xattr_bh) { 3179 ret = ocfs2_read_xattr_block(inode, 3180 le64_to_cpu(di->i_xattr_loc), 3181 &bh); 3182 if (ret) { 3183 mlog_errno(ret); 3184 goto out; 3185 } 3186 3187 xb = (struct ocfs2_xattr_block *)bh->b_data; 3188 } else 3189 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data; 3190 3191 /* 3192 * If there is already an xattr tree, good, we can calculate 3193 * like other b-trees. Otherwise we may have the chance of 3194 * create a tree, the credit calculation is borrowed from 3195 * ocfs2_calc_extend_credits with root_el = NULL. And the 3196 * new tree will be cluster based, so no meta is needed. 3197 */ 3198 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) { 3199 struct ocfs2_extent_list *el = 3200 &xb->xb_attrs.xb_root.xt_list; 3201 meta_add += ocfs2_extend_meta_needed(el); 3202 credits += ocfs2_calc_extend_credits(inode->i_sb, 3203 el); 3204 } else 3205 credits += OCFS2_SUBALLOC_ALLOC + 1; 3206 3207 /* 3208 * This cluster will be used either for new bucket or for 3209 * new xattr block. 3210 * If the cluster size is the same as the bucket size, one 3211 * more is needed since we may need to extend the bucket 3212 * also. 3213 */ 3214 clusters_add += 1; 3215 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb); 3216 if (OCFS2_XATTR_BUCKET_SIZE == 3217 OCFS2_SB(inode->i_sb)->s_clustersize) { 3218 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb); 3219 clusters_add += 1; 3220 } 3221 } else { 3222 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS; 3223 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) { 3224 struct ocfs2_extent_list *el = &def_xv.xv.xr_list; 3225 meta_add += ocfs2_extend_meta_needed(el); 3226 credits += ocfs2_calc_extend_credits(inode->i_sb, 3227 el); 3228 } else { 3229 meta_add += 1; 3230 } 3231 } 3232 out: 3233 if (clusters_need) 3234 *clusters_need = clusters_add; 3235 if (meta_need) 3236 *meta_need = meta_add; 3237 if (credits_need) 3238 *credits_need = credits; 3239 brelse(bh); 3240 return ret; 3241 } 3242 3243 static int ocfs2_init_xattr_set_ctxt(struct inode *inode, 3244 struct ocfs2_dinode *di, 3245 struct ocfs2_xattr_info *xi, 3246 struct ocfs2_xattr_search *xis, 3247 struct ocfs2_xattr_search *xbs, 3248 struct ocfs2_xattr_set_ctxt *ctxt, 3249 int extra_meta, 3250 int *credits) 3251 { 3252 int clusters_add, meta_add, ret; 3253 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 3254 3255 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt)); 3256 3257 ocfs2_init_dealloc_ctxt(&ctxt->dealloc); 3258 3259 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs, 3260 &clusters_add, &meta_add, credits); 3261 if (ret) { 3262 mlog_errno(ret); 3263 return ret; 3264 } 3265 3266 meta_add += extra_meta; 3267 trace_ocfs2_init_xattr_set_ctxt(xi->xi_name, meta_add, 3268 clusters_add, *credits); 3269 3270 if (meta_add) { 3271 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, 3272 &ctxt->meta_ac); 3273 if (ret) { 3274 mlog_errno(ret); 3275 goto out; 3276 } 3277 } 3278 3279 if (clusters_add) { 3280 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac); 3281 if (ret) 3282 mlog_errno(ret); 3283 } 3284 out: 3285 if (ret) { 3286 if (ctxt->meta_ac) { 3287 ocfs2_free_alloc_context(ctxt->meta_ac); 3288 ctxt->meta_ac = NULL; 3289 } 3290 3291 /* 3292 * We cannot have an error and a non null ctxt->data_ac. 3293 */ 3294 } 3295 3296 return ret; 3297 } 3298 3299 static int __ocfs2_xattr_set_handle(struct inode *inode, 3300 struct ocfs2_dinode *di, 3301 struct ocfs2_xattr_info *xi, 3302 struct ocfs2_xattr_search *xis, 3303 struct ocfs2_xattr_search *xbs, 3304 struct ocfs2_xattr_set_ctxt *ctxt) 3305 { 3306 int ret = 0, credits, old_found; 3307 3308 if (!xi->xi_value) { 3309 /* Remove existing extended attribute */ 3310 if (!xis->not_found) 3311 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt); 3312 else if (!xbs->not_found) 3313 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt); 3314 } else { 3315 /* We always try to set extended attribute into inode first*/ 3316 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt); 3317 if (!ret && !xbs->not_found) { 3318 /* 3319 * If succeed and that extended attribute existing in 3320 * external block, then we will remove it. 3321 */ 3322 xi->xi_value = NULL; 3323 xi->xi_value_len = 0; 3324 3325 old_found = xis->not_found; 3326 xis->not_found = -ENODATA; 3327 ret = ocfs2_calc_xattr_set_need(inode, 3328 di, 3329 xi, 3330 xis, 3331 xbs, 3332 NULL, 3333 NULL, 3334 &credits); 3335 xis->not_found = old_found; 3336 if (ret) { 3337 mlog_errno(ret); 3338 goto out; 3339 } 3340 3341 ret = ocfs2_extend_trans(ctxt->handle, credits); 3342 if (ret) { 3343 mlog_errno(ret); 3344 goto out; 3345 } 3346 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt); 3347 } else if ((ret == -ENOSPC) && !ctxt->set_abort) { 3348 if (di->i_xattr_loc && !xbs->xattr_bh) { 3349 ret = ocfs2_xattr_block_find(inode, 3350 xi->xi_name_index, 3351 xi->xi_name, xbs); 3352 if (ret) 3353 goto out; 3354 3355 old_found = xis->not_found; 3356 xis->not_found = -ENODATA; 3357 ret = ocfs2_calc_xattr_set_need(inode, 3358 di, 3359 xi, 3360 xis, 3361 xbs, 3362 NULL, 3363 NULL, 3364 &credits); 3365 xis->not_found = old_found; 3366 if (ret) { 3367 mlog_errno(ret); 3368 goto out; 3369 } 3370 3371 ret = ocfs2_extend_trans(ctxt->handle, credits); 3372 if (ret) { 3373 mlog_errno(ret); 3374 goto out; 3375 } 3376 } 3377 /* 3378 * If no space in inode, we will set extended attribute 3379 * into external block. 3380 */ 3381 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt); 3382 if (ret) 3383 goto out; 3384 if (!xis->not_found) { 3385 /* 3386 * If succeed and that extended attribute 3387 * existing in inode, we will remove it. 3388 */ 3389 xi->xi_value = NULL; 3390 xi->xi_value_len = 0; 3391 xbs->not_found = -ENODATA; 3392 ret = ocfs2_calc_xattr_set_need(inode, 3393 di, 3394 xi, 3395 xis, 3396 xbs, 3397 NULL, 3398 NULL, 3399 &credits); 3400 if (ret) { 3401 mlog_errno(ret); 3402 goto out; 3403 } 3404 3405 ret = ocfs2_extend_trans(ctxt->handle, credits); 3406 if (ret) { 3407 mlog_errno(ret); 3408 goto out; 3409 } 3410 ret = ocfs2_xattr_ibody_set(inode, xi, 3411 xis, ctxt); 3412 } 3413 } 3414 } 3415 3416 if (!ret) { 3417 /* Update inode ctime. */ 3418 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), 3419 xis->inode_bh, 3420 OCFS2_JOURNAL_ACCESS_WRITE); 3421 if (ret) { 3422 mlog_errno(ret); 3423 goto out; 3424 } 3425 3426 inode->i_ctime = current_time(inode); 3427 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); 3428 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); 3429 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh); 3430 } 3431 out: 3432 return ret; 3433 } 3434 3435 /* 3436 * This function only called duing creating inode 3437 * for init security/acl xattrs of the new inode. 3438 * All transanction credits have been reserved in mknod. 3439 */ 3440 int ocfs2_xattr_set_handle(handle_t *handle, 3441 struct inode *inode, 3442 struct buffer_head *di_bh, 3443 int name_index, 3444 const char *name, 3445 const void *value, 3446 size_t value_len, 3447 int flags, 3448 struct ocfs2_alloc_context *meta_ac, 3449 struct ocfs2_alloc_context *data_ac) 3450 { 3451 struct ocfs2_dinode *di; 3452 int ret; 3453 3454 struct ocfs2_xattr_info xi = { 3455 .xi_name_index = name_index, 3456 .xi_name = name, 3457 .xi_name_len = strlen(name), 3458 .xi_value = value, 3459 .xi_value_len = value_len, 3460 }; 3461 3462 struct ocfs2_xattr_search xis = { 3463 .not_found = -ENODATA, 3464 }; 3465 3466 struct ocfs2_xattr_search xbs = { 3467 .not_found = -ENODATA, 3468 }; 3469 3470 struct ocfs2_xattr_set_ctxt ctxt = { 3471 .handle = handle, 3472 .meta_ac = meta_ac, 3473 .data_ac = data_ac, 3474 }; 3475 3476 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) 3477 return -EOPNOTSUPP; 3478 3479 /* 3480 * In extreme situation, may need xattr bucket when 3481 * block size is too small. And we have already reserved 3482 * the credits for bucket in mknod. 3483 */ 3484 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) { 3485 xbs.bucket = ocfs2_xattr_bucket_new(inode); 3486 if (!xbs.bucket) { 3487 mlog_errno(-ENOMEM); 3488 return -ENOMEM; 3489 } 3490 } 3491 3492 xis.inode_bh = xbs.inode_bh = di_bh; 3493 di = (struct ocfs2_dinode *)di_bh->b_data; 3494 3495 down_write(&OCFS2_I(inode)->ip_xattr_sem); 3496 3497 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis); 3498 if (ret) 3499 goto cleanup; 3500 if (xis.not_found) { 3501 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs); 3502 if (ret) 3503 goto cleanup; 3504 } 3505 3506 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt); 3507 3508 cleanup: 3509 up_write(&OCFS2_I(inode)->ip_xattr_sem); 3510 brelse(xbs.xattr_bh); 3511 ocfs2_xattr_bucket_free(xbs.bucket); 3512 3513 return ret; 3514 } 3515 3516 /* 3517 * ocfs2_xattr_set() 3518 * 3519 * Set, replace or remove an extended attribute for this inode. 3520 * value is NULL to remove an existing extended attribute, else either 3521 * create or replace an extended attribute. 3522 */ 3523 int ocfs2_xattr_set(struct inode *inode, 3524 int name_index, 3525 const char *name, 3526 const void *value, 3527 size_t value_len, 3528 int flags) 3529 { 3530 struct buffer_head *di_bh = NULL; 3531 struct ocfs2_dinode *di; 3532 int ret, credits, had_lock, ref_meta = 0, ref_credits = 0; 3533 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 3534 struct inode *tl_inode = osb->osb_tl_inode; 3535 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, NULL, }; 3536 struct ocfs2_refcount_tree *ref_tree = NULL; 3537 struct ocfs2_lock_holder oh; 3538 3539 struct ocfs2_xattr_info xi = { 3540 .xi_name_index = name_index, 3541 .xi_name = name, 3542 .xi_name_len = strlen(name), 3543 .xi_value = value, 3544 .xi_value_len = value_len, 3545 }; 3546 3547 struct ocfs2_xattr_search xis = { 3548 .not_found = -ENODATA, 3549 }; 3550 3551 struct ocfs2_xattr_search xbs = { 3552 .not_found = -ENODATA, 3553 }; 3554 3555 if (!ocfs2_supports_xattr(osb)) 3556 return -EOPNOTSUPP; 3557 3558 /* 3559 * Only xbs will be used on indexed trees. xis doesn't need a 3560 * bucket. 3561 */ 3562 xbs.bucket = ocfs2_xattr_bucket_new(inode); 3563 if (!xbs.bucket) { 3564 mlog_errno(-ENOMEM); 3565 return -ENOMEM; 3566 } 3567 3568 had_lock = ocfs2_inode_lock_tracker(inode, &di_bh, 1, &oh); 3569 if (had_lock < 0) { 3570 ret = had_lock; 3571 mlog_errno(ret); 3572 goto cleanup_nolock; 3573 } 3574 xis.inode_bh = xbs.inode_bh = di_bh; 3575 di = (struct ocfs2_dinode *)di_bh->b_data; 3576 3577 down_write(&OCFS2_I(inode)->ip_xattr_sem); 3578 /* 3579 * Scan inode and external block to find the same name 3580 * extended attribute and collect search information. 3581 */ 3582 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis); 3583 if (ret) 3584 goto cleanup; 3585 if (xis.not_found) { 3586 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs); 3587 if (ret) 3588 goto cleanup; 3589 } 3590 3591 if (xis.not_found && xbs.not_found) { 3592 ret = -ENODATA; 3593 if (flags & XATTR_REPLACE) 3594 goto cleanup; 3595 ret = 0; 3596 if (!value) 3597 goto cleanup; 3598 } else { 3599 ret = -EEXIST; 3600 if (flags & XATTR_CREATE) 3601 goto cleanup; 3602 } 3603 3604 /* Check whether the value is refcounted and do some preparation. */ 3605 if (ocfs2_is_refcount_inode(inode) && 3606 (!xis.not_found || !xbs.not_found)) { 3607 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi, 3608 &xis, &xbs, &ref_tree, 3609 &ref_meta, &ref_credits); 3610 if (ret) { 3611 mlog_errno(ret); 3612 goto cleanup; 3613 } 3614 } 3615 3616 inode_lock(tl_inode); 3617 3618 if (ocfs2_truncate_log_needs_flush(osb)) { 3619 ret = __ocfs2_flush_truncate_log(osb); 3620 if (ret < 0) { 3621 inode_unlock(tl_inode); 3622 mlog_errno(ret); 3623 goto cleanup; 3624 } 3625 } 3626 inode_unlock(tl_inode); 3627 3628 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis, 3629 &xbs, &ctxt, ref_meta, &credits); 3630 if (ret) { 3631 mlog_errno(ret); 3632 goto cleanup; 3633 } 3634 3635 /* we need to update inode's ctime field, so add credit for it. */ 3636 credits += OCFS2_INODE_UPDATE_CREDITS; 3637 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits); 3638 if (IS_ERR(ctxt.handle)) { 3639 ret = PTR_ERR(ctxt.handle); 3640 mlog_errno(ret); 3641 goto out_free_ac; 3642 } 3643 3644 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt); 3645 ocfs2_update_inode_fsync_trans(ctxt.handle, inode, 0); 3646 3647 ocfs2_commit_trans(osb, ctxt.handle); 3648 3649 out_free_ac: 3650 if (ctxt.data_ac) 3651 ocfs2_free_alloc_context(ctxt.data_ac); 3652 if (ctxt.meta_ac) 3653 ocfs2_free_alloc_context(ctxt.meta_ac); 3654 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc)) 3655 ocfs2_schedule_truncate_log_flush(osb, 1); 3656 ocfs2_run_deallocs(osb, &ctxt.dealloc); 3657 3658 cleanup: 3659 if (ref_tree) 3660 ocfs2_unlock_refcount_tree(osb, ref_tree, 1); 3661 up_write(&OCFS2_I(inode)->ip_xattr_sem); 3662 if (!value && !ret) { 3663 ret = ocfs2_try_remove_refcount_tree(inode, di_bh); 3664 if (ret) 3665 mlog_errno(ret); 3666 } 3667 ocfs2_inode_unlock_tracker(inode, 1, &oh, had_lock); 3668 cleanup_nolock: 3669 brelse(di_bh); 3670 brelse(xbs.xattr_bh); 3671 ocfs2_xattr_bucket_free(xbs.bucket); 3672 3673 return ret; 3674 } 3675 3676 /* 3677 * Find the xattr extent rec which may contains name_hash. 3678 * e_cpos will be the first name hash of the xattr rec. 3679 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list. 3680 */ 3681 static int ocfs2_xattr_get_rec(struct inode *inode, 3682 u32 name_hash, 3683 u64 *p_blkno, 3684 u32 *e_cpos, 3685 u32 *num_clusters, 3686 struct ocfs2_extent_list *el) 3687 { 3688 int ret = 0, i; 3689 struct buffer_head *eb_bh = NULL; 3690 struct ocfs2_extent_block *eb; 3691 struct ocfs2_extent_rec *rec = NULL; 3692 u64 e_blkno = 0; 3693 3694 if (el->l_tree_depth) { 3695 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash, 3696 &eb_bh); 3697 if (ret) { 3698 mlog_errno(ret); 3699 goto out; 3700 } 3701 3702 eb = (struct ocfs2_extent_block *) eb_bh->b_data; 3703 el = &eb->h_list; 3704 3705 if (el->l_tree_depth) { 3706 ret = ocfs2_error(inode->i_sb, 3707 "Inode %lu has non zero tree depth in xattr tree block %llu\n", 3708 inode->i_ino, 3709 (unsigned long long)eb_bh->b_blocknr); 3710 goto out; 3711 } 3712 } 3713 3714 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) { 3715 rec = &el->l_recs[i]; 3716 3717 if (le32_to_cpu(rec->e_cpos) <= name_hash) { 3718 e_blkno = le64_to_cpu(rec->e_blkno); 3719 break; 3720 } 3721 } 3722 3723 if (!e_blkno) { 3724 ret = ocfs2_error(inode->i_sb, "Inode %lu has bad extent record (%u, %u, 0) in xattr\n", 3725 inode->i_ino, 3726 le32_to_cpu(rec->e_cpos), 3727 ocfs2_rec_clusters(el, rec)); 3728 goto out; 3729 } 3730 3731 *p_blkno = le64_to_cpu(rec->e_blkno); 3732 *num_clusters = le16_to_cpu(rec->e_leaf_clusters); 3733 if (e_cpos) 3734 *e_cpos = le32_to_cpu(rec->e_cpos); 3735 out: 3736 brelse(eb_bh); 3737 return ret; 3738 } 3739 3740 typedef int (xattr_bucket_func)(struct inode *inode, 3741 struct ocfs2_xattr_bucket *bucket, 3742 void *para); 3743 3744 static int ocfs2_find_xe_in_bucket(struct inode *inode, 3745 struct ocfs2_xattr_bucket *bucket, 3746 int name_index, 3747 const char *name, 3748 u32 name_hash, 3749 u16 *xe_index, 3750 int *found) 3751 { 3752 int i, ret = 0, cmp = 1, block_off, new_offset; 3753 struct ocfs2_xattr_header *xh = bucket_xh(bucket); 3754 size_t name_len = strlen(name); 3755 struct ocfs2_xattr_entry *xe = NULL; 3756 char *xe_name; 3757 3758 /* 3759 * We don't use binary search in the bucket because there 3760 * may be multiple entries with the same name hash. 3761 */ 3762 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { 3763 xe = &xh->xh_entries[i]; 3764 3765 if (name_hash > le32_to_cpu(xe->xe_name_hash)) 3766 continue; 3767 else if (name_hash < le32_to_cpu(xe->xe_name_hash)) 3768 break; 3769 3770 cmp = name_index - ocfs2_xattr_get_type(xe); 3771 if (!cmp) 3772 cmp = name_len - xe->xe_name_len; 3773 if (cmp) 3774 continue; 3775 3776 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, 3777 xh, 3778 i, 3779 &block_off, 3780 &new_offset); 3781 if (ret) { 3782 mlog_errno(ret); 3783 break; 3784 } 3785 3786 3787 xe_name = bucket_block(bucket, block_off) + new_offset; 3788 if (!memcmp(name, xe_name, name_len)) { 3789 *xe_index = i; 3790 *found = 1; 3791 ret = 0; 3792 break; 3793 } 3794 } 3795 3796 return ret; 3797 } 3798 3799 /* 3800 * Find the specified xattr entry in a series of buckets. 3801 * This series start from p_blkno and last for num_clusters. 3802 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains 3803 * the num of the valid buckets. 3804 * 3805 * Return the buffer_head this xattr should reside in. And if the xattr's 3806 * hash is in the gap of 2 buckets, return the lower bucket. 3807 */ 3808 static int ocfs2_xattr_bucket_find(struct inode *inode, 3809 int name_index, 3810 const char *name, 3811 u32 name_hash, 3812 u64 p_blkno, 3813 u32 first_hash, 3814 u32 num_clusters, 3815 struct ocfs2_xattr_search *xs) 3816 { 3817 int ret, found = 0; 3818 struct ocfs2_xattr_header *xh = NULL; 3819 struct ocfs2_xattr_entry *xe = NULL; 3820 u16 index = 0; 3821 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); 3822 int low_bucket = 0, bucket, high_bucket; 3823 struct ocfs2_xattr_bucket *search; 3824 u64 blkno, lower_blkno = 0; 3825 3826 search = ocfs2_xattr_bucket_new(inode); 3827 if (!search) { 3828 ret = -ENOMEM; 3829 mlog_errno(ret); 3830 goto out; 3831 } 3832 3833 ret = ocfs2_read_xattr_bucket(search, p_blkno); 3834 if (ret) { 3835 mlog_errno(ret); 3836 goto out; 3837 } 3838 3839 xh = bucket_xh(search); 3840 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1; 3841 while (low_bucket <= high_bucket) { 3842 ocfs2_xattr_bucket_relse(search); 3843 3844 bucket = (low_bucket + high_bucket) / 2; 3845 blkno = p_blkno + bucket * blk_per_bucket; 3846 ret = ocfs2_read_xattr_bucket(search, blkno); 3847 if (ret) { 3848 mlog_errno(ret); 3849 goto out; 3850 } 3851 3852 xh = bucket_xh(search); 3853 xe = &xh->xh_entries[0]; 3854 if (name_hash < le32_to_cpu(xe->xe_name_hash)) { 3855 high_bucket = bucket - 1; 3856 continue; 3857 } 3858 3859 /* 3860 * Check whether the hash of the last entry in our 3861 * bucket is larger than the search one. for an empty 3862 * bucket, the last one is also the first one. 3863 */ 3864 if (xh->xh_count) 3865 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1]; 3866 3867 /* record lower_blkno which may be the insert place. */ 3868 lower_blkno = blkno; 3869 3870 if (name_hash > le32_to_cpu(xe->xe_name_hash)) { 3871 low_bucket = bucket + 1; 3872 continue; 3873 } 3874 3875 /* the searched xattr should reside in this bucket if exists. */ 3876 ret = ocfs2_find_xe_in_bucket(inode, search, 3877 name_index, name, name_hash, 3878 &index, &found); 3879 if (ret) { 3880 mlog_errno(ret); 3881 goto out; 3882 } 3883 break; 3884 } 3885 3886 /* 3887 * Record the bucket we have found. 3888 * When the xattr's hash value is in the gap of 2 buckets, we will 3889 * always set it to the previous bucket. 3890 */ 3891 if (!lower_blkno) 3892 lower_blkno = p_blkno; 3893 3894 /* This should be in cache - we just read it during the search */ 3895 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno); 3896 if (ret) { 3897 mlog_errno(ret); 3898 goto out; 3899 } 3900 3901 xs->header = bucket_xh(xs->bucket); 3902 xs->base = bucket_block(xs->bucket, 0); 3903 xs->end = xs->base + inode->i_sb->s_blocksize; 3904 3905 if (found) { 3906 xs->here = &xs->header->xh_entries[index]; 3907 trace_ocfs2_xattr_bucket_find(OCFS2_I(inode)->ip_blkno, 3908 name, name_index, name_hash, 3909 (unsigned long long)bucket_blkno(xs->bucket), 3910 index); 3911 } else 3912 ret = -ENODATA; 3913 3914 out: 3915 ocfs2_xattr_bucket_free(search); 3916 return ret; 3917 } 3918 3919 static int ocfs2_xattr_index_block_find(struct inode *inode, 3920 struct buffer_head *root_bh, 3921 int name_index, 3922 const char *name, 3923 struct ocfs2_xattr_search *xs) 3924 { 3925 int ret; 3926 struct ocfs2_xattr_block *xb = 3927 (struct ocfs2_xattr_block *)root_bh->b_data; 3928 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root; 3929 struct ocfs2_extent_list *el = &xb_root->xt_list; 3930 u64 p_blkno = 0; 3931 u32 first_hash, num_clusters = 0; 3932 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name)); 3933 3934 if (le16_to_cpu(el->l_next_free_rec) == 0) 3935 return -ENODATA; 3936 3937 trace_ocfs2_xattr_index_block_find(OCFS2_I(inode)->ip_blkno, 3938 name, name_index, name_hash, 3939 (unsigned long long)root_bh->b_blocknr, 3940 -1); 3941 3942 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash, 3943 &num_clusters, el); 3944 if (ret) { 3945 mlog_errno(ret); 3946 goto out; 3947 } 3948 3949 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash); 3950 3951 trace_ocfs2_xattr_index_block_find_rec(OCFS2_I(inode)->ip_blkno, 3952 name, name_index, first_hash, 3953 (unsigned long long)p_blkno, 3954 num_clusters); 3955 3956 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash, 3957 p_blkno, first_hash, num_clusters, xs); 3958 3959 out: 3960 return ret; 3961 } 3962 3963 static int ocfs2_iterate_xattr_buckets(struct inode *inode, 3964 u64 blkno, 3965 u32 clusters, 3966 xattr_bucket_func *func, 3967 void *para) 3968 { 3969 int i, ret = 0; 3970 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)); 3971 u32 num_buckets = clusters * bpc; 3972 struct ocfs2_xattr_bucket *bucket; 3973 3974 bucket = ocfs2_xattr_bucket_new(inode); 3975 if (!bucket) { 3976 mlog_errno(-ENOMEM); 3977 return -ENOMEM; 3978 } 3979 3980 trace_ocfs2_iterate_xattr_buckets( 3981 (unsigned long long)OCFS2_I(inode)->ip_blkno, 3982 (unsigned long long)blkno, clusters); 3983 3984 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) { 3985 ret = ocfs2_read_xattr_bucket(bucket, blkno); 3986 if (ret) { 3987 mlog_errno(ret); 3988 break; 3989 } 3990 3991 /* 3992 * The real bucket num in this series of blocks is stored 3993 * in the 1st bucket. 3994 */ 3995 if (i == 0) 3996 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets); 3997 3998 trace_ocfs2_iterate_xattr_bucket((unsigned long long)blkno, 3999 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash)); 4000 if (func) { 4001 ret = func(inode, bucket, para); 4002 if (ret && ret != -ERANGE) 4003 mlog_errno(ret); 4004 /* Fall through to bucket_relse() */ 4005 } 4006 4007 ocfs2_xattr_bucket_relse(bucket); 4008 if (ret) 4009 break; 4010 } 4011 4012 ocfs2_xattr_bucket_free(bucket); 4013 return ret; 4014 } 4015 4016 struct ocfs2_xattr_tree_list { 4017 char *buffer; 4018 size_t buffer_size; 4019 size_t result; 4020 }; 4021 4022 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb, 4023 struct ocfs2_xattr_header *xh, 4024 int index, 4025 int *block_off, 4026 int *new_offset) 4027 { 4028 u16 name_offset; 4029 4030 if (index < 0 || index >= le16_to_cpu(xh->xh_count)) 4031 return -EINVAL; 4032 4033 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset); 4034 4035 *block_off = name_offset >> sb->s_blocksize_bits; 4036 *new_offset = name_offset % sb->s_blocksize; 4037 4038 return 0; 4039 } 4040 4041 static int ocfs2_list_xattr_bucket(struct inode *inode, 4042 struct ocfs2_xattr_bucket *bucket, 4043 void *para) 4044 { 4045 int ret = 0, type; 4046 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para; 4047 int i, block_off, new_offset; 4048 const char *name; 4049 4050 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) { 4051 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i]; 4052 type = ocfs2_xattr_get_type(entry); 4053 4054 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, 4055 bucket_xh(bucket), 4056 i, 4057 &block_off, 4058 &new_offset); 4059 if (ret) 4060 break; 4061 4062 name = (const char *)bucket_block(bucket, block_off) + 4063 new_offset; 4064 ret = ocfs2_xattr_list_entry(inode->i_sb, 4065 xl->buffer, 4066 xl->buffer_size, 4067 &xl->result, 4068 type, name, 4069 entry->xe_name_len); 4070 if (ret) 4071 break; 4072 } 4073 4074 return ret; 4075 } 4076 4077 static int ocfs2_iterate_xattr_index_block(struct inode *inode, 4078 struct buffer_head *blk_bh, 4079 xattr_tree_rec_func *rec_func, 4080 void *para) 4081 { 4082 struct ocfs2_xattr_block *xb = 4083 (struct ocfs2_xattr_block *)blk_bh->b_data; 4084 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list; 4085 int ret = 0; 4086 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0; 4087 u64 p_blkno = 0; 4088 4089 if (!el->l_next_free_rec || !rec_func) 4090 return 0; 4091 4092 while (name_hash > 0) { 4093 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, 4094 &e_cpos, &num_clusters, el); 4095 if (ret) { 4096 mlog_errno(ret); 4097 break; 4098 } 4099 4100 ret = rec_func(inode, blk_bh, p_blkno, e_cpos, 4101 num_clusters, para); 4102 if (ret) { 4103 if (ret != -ERANGE) 4104 mlog_errno(ret); 4105 break; 4106 } 4107 4108 if (e_cpos == 0) 4109 break; 4110 4111 name_hash = e_cpos - 1; 4112 } 4113 4114 return ret; 4115 4116 } 4117 4118 static int ocfs2_list_xattr_tree_rec(struct inode *inode, 4119 struct buffer_head *root_bh, 4120 u64 blkno, u32 cpos, u32 len, void *para) 4121 { 4122 return ocfs2_iterate_xattr_buckets(inode, blkno, len, 4123 ocfs2_list_xattr_bucket, para); 4124 } 4125 4126 static int ocfs2_xattr_tree_list_index_block(struct inode *inode, 4127 struct buffer_head *blk_bh, 4128 char *buffer, 4129 size_t buffer_size) 4130 { 4131 int ret; 4132 struct ocfs2_xattr_tree_list xl = { 4133 .buffer = buffer, 4134 .buffer_size = buffer_size, 4135 .result = 0, 4136 }; 4137 4138 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh, 4139 ocfs2_list_xattr_tree_rec, &xl); 4140 if (ret) { 4141 mlog_errno(ret); 4142 goto out; 4143 } 4144 4145 ret = xl.result; 4146 out: 4147 return ret; 4148 } 4149 4150 static int cmp_xe(const void *a, const void *b) 4151 { 4152 const struct ocfs2_xattr_entry *l = a, *r = b; 4153 u32 l_hash = le32_to_cpu(l->xe_name_hash); 4154 u32 r_hash = le32_to_cpu(r->xe_name_hash); 4155 4156 if (l_hash > r_hash) 4157 return 1; 4158 if (l_hash < r_hash) 4159 return -1; 4160 return 0; 4161 } 4162 4163 static void swap_xe(void *a, void *b, int size) 4164 { 4165 struct ocfs2_xattr_entry *l = a, *r = b, tmp; 4166 4167 tmp = *l; 4168 memcpy(l, r, sizeof(struct ocfs2_xattr_entry)); 4169 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry)); 4170 } 4171 4172 /* 4173 * When the ocfs2_xattr_block is filled up, new bucket will be created 4174 * and all the xattr entries will be moved to the new bucket. 4175 * The header goes at the start of the bucket, and the names+values are 4176 * filled from the end. This is why *target starts as the last buffer. 4177 * Note: we need to sort the entries since they are not saved in order 4178 * in the ocfs2_xattr_block. 4179 */ 4180 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode, 4181 struct buffer_head *xb_bh, 4182 struct ocfs2_xattr_bucket *bucket) 4183 { 4184 int i, blocksize = inode->i_sb->s_blocksize; 4185 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb); 4186 u16 offset, size, off_change; 4187 struct ocfs2_xattr_entry *xe; 4188 struct ocfs2_xattr_block *xb = 4189 (struct ocfs2_xattr_block *)xb_bh->b_data; 4190 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header; 4191 struct ocfs2_xattr_header *xh = bucket_xh(bucket); 4192 u16 count = le16_to_cpu(xb_xh->xh_count); 4193 char *src = xb_bh->b_data; 4194 char *target = bucket_block(bucket, blks - 1); 4195 4196 trace_ocfs2_cp_xattr_block_to_bucket_begin( 4197 (unsigned long long)xb_bh->b_blocknr, 4198 (unsigned long long)bucket_blkno(bucket)); 4199 4200 for (i = 0; i < blks; i++) 4201 memset(bucket_block(bucket, i), 0, blocksize); 4202 4203 /* 4204 * Since the xe_name_offset is based on ocfs2_xattr_header, 4205 * there is a offset change corresponding to the change of 4206 * ocfs2_xattr_header's position. 4207 */ 4208 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); 4209 xe = &xb_xh->xh_entries[count - 1]; 4210 offset = le16_to_cpu(xe->xe_name_offset) + off_change; 4211 size = blocksize - offset; 4212 4213 /* copy all the names and values. */ 4214 memcpy(target + offset, src + offset, size); 4215 4216 /* Init new header now. */ 4217 xh->xh_count = xb_xh->xh_count; 4218 xh->xh_num_buckets = cpu_to_le16(1); 4219 xh->xh_name_value_len = cpu_to_le16(size); 4220 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size); 4221 4222 /* copy all the entries. */ 4223 target = bucket_block(bucket, 0); 4224 offset = offsetof(struct ocfs2_xattr_header, xh_entries); 4225 size = count * sizeof(struct ocfs2_xattr_entry); 4226 memcpy(target + offset, (char *)xb_xh + offset, size); 4227 4228 /* Change the xe offset for all the xe because of the move. */ 4229 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize + 4230 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); 4231 for (i = 0; i < count; i++) 4232 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change); 4233 4234 trace_ocfs2_cp_xattr_block_to_bucket_end(offset, size, off_change); 4235 4236 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry), 4237 cmp_xe, swap_xe); 4238 } 4239 4240 /* 4241 * After we move xattr from block to index btree, we have to 4242 * update ocfs2_xattr_search to the new xe and base. 4243 * 4244 * When the entry is in xattr block, xattr_bh indicates the storage place. 4245 * While if the entry is in index b-tree, "bucket" indicates the 4246 * real place of the xattr. 4247 */ 4248 static void ocfs2_xattr_update_xattr_search(struct inode *inode, 4249 struct ocfs2_xattr_search *xs, 4250 struct buffer_head *old_bh) 4251 { 4252 char *buf = old_bh->b_data; 4253 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf; 4254 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header; 4255 int i; 4256 4257 xs->header = bucket_xh(xs->bucket); 4258 xs->base = bucket_block(xs->bucket, 0); 4259 xs->end = xs->base + inode->i_sb->s_blocksize; 4260 4261 if (xs->not_found) 4262 return; 4263 4264 i = xs->here - old_xh->xh_entries; 4265 xs->here = &xs->header->xh_entries[i]; 4266 } 4267 4268 static int ocfs2_xattr_create_index_block(struct inode *inode, 4269 struct ocfs2_xattr_search *xs, 4270 struct ocfs2_xattr_set_ctxt *ctxt) 4271 { 4272 int ret; 4273 u32 bit_off, len; 4274 u64 blkno; 4275 handle_t *handle = ctxt->handle; 4276 struct ocfs2_inode_info *oi = OCFS2_I(inode); 4277 struct buffer_head *xb_bh = xs->xattr_bh; 4278 struct ocfs2_xattr_block *xb = 4279 (struct ocfs2_xattr_block *)xb_bh->b_data; 4280 struct ocfs2_xattr_tree_root *xr; 4281 u16 xb_flags = le16_to_cpu(xb->xb_flags); 4282 4283 trace_ocfs2_xattr_create_index_block_begin( 4284 (unsigned long long)xb_bh->b_blocknr); 4285 4286 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED); 4287 BUG_ON(!xs->bucket); 4288 4289 /* 4290 * XXX: 4291 * We can use this lock for now, and maybe move to a dedicated mutex 4292 * if performance becomes a problem later. 4293 */ 4294 down_write(&oi->ip_alloc_sem); 4295 4296 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh, 4297 OCFS2_JOURNAL_ACCESS_WRITE); 4298 if (ret) { 4299 mlog_errno(ret); 4300 goto out; 4301 } 4302 4303 ret = __ocfs2_claim_clusters(handle, ctxt->data_ac, 4304 1, 1, &bit_off, &len); 4305 if (ret) { 4306 mlog_errno(ret); 4307 goto out; 4308 } 4309 4310 /* 4311 * The bucket may spread in many blocks, and 4312 * we will only touch the 1st block and the last block 4313 * in the whole bucket(one for entry and one for data). 4314 */ 4315 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off); 4316 4317 trace_ocfs2_xattr_create_index_block((unsigned long long)blkno); 4318 4319 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno, 1); 4320 if (ret) { 4321 mlog_errno(ret); 4322 goto out; 4323 } 4324 4325 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket, 4326 OCFS2_JOURNAL_ACCESS_CREATE); 4327 if (ret) { 4328 mlog_errno(ret); 4329 goto out; 4330 } 4331 4332 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket); 4333 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket); 4334 4335 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh); 4336 4337 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */ 4338 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize - 4339 offsetof(struct ocfs2_xattr_block, xb_attrs)); 4340 4341 xr = &xb->xb_attrs.xb_root; 4342 xr->xt_clusters = cpu_to_le32(1); 4343 xr->xt_last_eb_blk = 0; 4344 xr->xt_list.l_tree_depth = 0; 4345 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb)); 4346 xr->xt_list.l_next_free_rec = cpu_to_le16(1); 4347 4348 xr->xt_list.l_recs[0].e_cpos = 0; 4349 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno); 4350 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1); 4351 4352 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED); 4353 4354 ocfs2_journal_dirty(handle, xb_bh); 4355 4356 out: 4357 up_write(&oi->ip_alloc_sem); 4358 4359 return ret; 4360 } 4361 4362 static int cmp_xe_offset(const void *a, const void *b) 4363 { 4364 const struct ocfs2_xattr_entry *l = a, *r = b; 4365 u32 l_name_offset = le16_to_cpu(l->xe_name_offset); 4366 u32 r_name_offset = le16_to_cpu(r->xe_name_offset); 4367 4368 if (l_name_offset < r_name_offset) 4369 return 1; 4370 if (l_name_offset > r_name_offset) 4371 return -1; 4372 return 0; 4373 } 4374 4375 /* 4376 * defrag a xattr bucket if we find that the bucket has some 4377 * holes beteen name/value pairs. 4378 * We will move all the name/value pairs to the end of the bucket 4379 * so that we can spare some space for insertion. 4380 */ 4381 static int ocfs2_defrag_xattr_bucket(struct inode *inode, 4382 handle_t *handle, 4383 struct ocfs2_xattr_bucket *bucket) 4384 { 4385 int ret, i; 4386 size_t end, offset, len; 4387 struct ocfs2_xattr_header *xh; 4388 char *entries, *buf, *bucket_buf = NULL; 4389 u64 blkno = bucket_blkno(bucket); 4390 u16 xh_free_start; 4391 size_t blocksize = inode->i_sb->s_blocksize; 4392 struct ocfs2_xattr_entry *xe; 4393 4394 /* 4395 * In order to make the operation more efficient and generic, 4396 * we copy all the blocks into a contiguous memory and do the 4397 * defragment there, so if anything is error, we will not touch 4398 * the real block. 4399 */ 4400 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS); 4401 if (!bucket_buf) { 4402 ret = -EIO; 4403 goto out; 4404 } 4405 4406 buf = bucket_buf; 4407 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize) 4408 memcpy(buf, bucket_block(bucket, i), blocksize); 4409 4410 ret = ocfs2_xattr_bucket_journal_access(handle, bucket, 4411 OCFS2_JOURNAL_ACCESS_WRITE); 4412 if (ret < 0) { 4413 mlog_errno(ret); 4414 goto out; 4415 } 4416 4417 xh = (struct ocfs2_xattr_header *)bucket_buf; 4418 entries = (char *)xh->xh_entries; 4419 xh_free_start = le16_to_cpu(xh->xh_free_start); 4420 4421 trace_ocfs2_defrag_xattr_bucket( 4422 (unsigned long long)blkno, le16_to_cpu(xh->xh_count), 4423 xh_free_start, le16_to_cpu(xh->xh_name_value_len)); 4424 4425 /* 4426 * sort all the entries by their offset. 4427 * the largest will be the first, so that we can 4428 * move them to the end one by one. 4429 */ 4430 sort(entries, le16_to_cpu(xh->xh_count), 4431 sizeof(struct ocfs2_xattr_entry), 4432 cmp_xe_offset, swap_xe); 4433 4434 /* Move all name/values to the end of the bucket. */ 4435 xe = xh->xh_entries; 4436 end = OCFS2_XATTR_BUCKET_SIZE; 4437 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) { 4438 offset = le16_to_cpu(xe->xe_name_offset); 4439 len = namevalue_size_xe(xe); 4440 4441 /* 4442 * We must make sure that the name/value pair 4443 * exist in the same block. So adjust end to 4444 * the previous block end if needed. 4445 */ 4446 if (((end - len) / blocksize != 4447 (end - 1) / blocksize)) 4448 end = end - end % blocksize; 4449 4450 if (end > offset + len) { 4451 memmove(bucket_buf + end - len, 4452 bucket_buf + offset, len); 4453 xe->xe_name_offset = cpu_to_le16(end - len); 4454 } 4455 4456 mlog_bug_on_msg(end < offset + len, "Defrag check failed for " 4457 "bucket %llu\n", (unsigned long long)blkno); 4458 4459 end -= len; 4460 } 4461 4462 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for " 4463 "bucket %llu\n", (unsigned long long)blkno); 4464 4465 if (xh_free_start == end) 4466 goto out; 4467 4468 memset(bucket_buf + xh_free_start, 0, end - xh_free_start); 4469 xh->xh_free_start = cpu_to_le16(end); 4470 4471 /* sort the entries by their name_hash. */ 4472 sort(entries, le16_to_cpu(xh->xh_count), 4473 sizeof(struct ocfs2_xattr_entry), 4474 cmp_xe, swap_xe); 4475 4476 buf = bucket_buf; 4477 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize) 4478 memcpy(bucket_block(bucket, i), buf, blocksize); 4479 ocfs2_xattr_bucket_journal_dirty(handle, bucket); 4480 4481 out: 4482 kfree(bucket_buf); 4483 return ret; 4484 } 4485 4486 /* 4487 * prev_blkno points to the start of an existing extent. new_blkno 4488 * points to a newly allocated extent. Because we know each of our 4489 * clusters contains more than bucket, we can easily split one cluster 4490 * at a bucket boundary. So we take the last cluster of the existing 4491 * extent and split it down the middle. We move the last half of the 4492 * buckets in the last cluster of the existing extent over to the new 4493 * extent. 4494 * 4495 * first_bh is the buffer at prev_blkno so we can update the existing 4496 * extent's bucket count. header_bh is the bucket were we were hoping 4497 * to insert our xattr. If the bucket move places the target in the new 4498 * extent, we'll update first_bh and header_bh after modifying the old 4499 * extent. 4500 * 4501 * first_hash will be set as the 1st xe's name_hash in the new extent. 4502 */ 4503 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode, 4504 handle_t *handle, 4505 struct ocfs2_xattr_bucket *first, 4506 struct ocfs2_xattr_bucket *target, 4507 u64 new_blkno, 4508 u32 num_clusters, 4509 u32 *first_hash) 4510 { 4511 int ret; 4512 struct super_block *sb = inode->i_sb; 4513 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb); 4514 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb)); 4515 int to_move = num_buckets / 2; 4516 u64 src_blkno; 4517 u64 last_cluster_blkno = bucket_blkno(first) + 4518 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1)); 4519 4520 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets); 4521 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize); 4522 4523 trace_ocfs2_mv_xattr_bucket_cross_cluster( 4524 (unsigned long long)last_cluster_blkno, 4525 (unsigned long long)new_blkno); 4526 4527 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first), 4528 last_cluster_blkno, new_blkno, 4529 to_move, first_hash); 4530 if (ret) { 4531 mlog_errno(ret); 4532 goto out; 4533 } 4534 4535 /* This is the first bucket that got moved */ 4536 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket); 4537 4538 /* 4539 * If the target bucket was part of the moved buckets, we need to 4540 * update first and target. 4541 */ 4542 if (bucket_blkno(target) >= src_blkno) { 4543 /* Find the block for the new target bucket */ 4544 src_blkno = new_blkno + 4545 (bucket_blkno(target) - src_blkno); 4546 4547 ocfs2_xattr_bucket_relse(first); 4548 ocfs2_xattr_bucket_relse(target); 4549 4550 /* 4551 * These shouldn't fail - the buffers are in the 4552 * journal from ocfs2_cp_xattr_bucket(). 4553 */ 4554 ret = ocfs2_read_xattr_bucket(first, new_blkno); 4555 if (ret) { 4556 mlog_errno(ret); 4557 goto out; 4558 } 4559 ret = ocfs2_read_xattr_bucket(target, src_blkno); 4560 if (ret) 4561 mlog_errno(ret); 4562 4563 } 4564 4565 out: 4566 return ret; 4567 } 4568 4569 /* 4570 * Find the suitable pos when we divide a bucket into 2. 4571 * We have to make sure the xattrs with the same hash value exist 4572 * in the same bucket. 4573 * 4574 * If this ocfs2_xattr_header covers more than one hash value, find a 4575 * place where the hash value changes. Try to find the most even split. 4576 * The most common case is that all entries have different hash values, 4577 * and the first check we make will find a place to split. 4578 */ 4579 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh) 4580 { 4581 struct ocfs2_xattr_entry *entries = xh->xh_entries; 4582 int count = le16_to_cpu(xh->xh_count); 4583 int delta, middle = count / 2; 4584 4585 /* 4586 * We start at the middle. Each step gets farther away in both 4587 * directions. We therefore hit the change in hash value 4588 * nearest to the middle. Note that this loop does not execute for 4589 * count < 2. 4590 */ 4591 for (delta = 0; delta < middle; delta++) { 4592 /* Let's check delta earlier than middle */ 4593 if (cmp_xe(&entries[middle - delta - 1], 4594 &entries[middle - delta])) 4595 return middle - delta; 4596 4597 /* For even counts, don't walk off the end */ 4598 if ((middle + delta + 1) == count) 4599 continue; 4600 4601 /* Now try delta past middle */ 4602 if (cmp_xe(&entries[middle + delta], 4603 &entries[middle + delta + 1])) 4604 return middle + delta + 1; 4605 } 4606 4607 /* Every entry had the same hash */ 4608 return count; 4609 } 4610 4611 /* 4612 * Move some xattrs in old bucket(blk) to new bucket(new_blk). 4613 * first_hash will record the 1st hash of the new bucket. 4614 * 4615 * Normally half of the xattrs will be moved. But we have to make 4616 * sure that the xattrs with the same hash value are stored in the 4617 * same bucket. If all the xattrs in this bucket have the same hash 4618 * value, the new bucket will be initialized as an empty one and the 4619 * first_hash will be initialized as (hash_value+1). 4620 */ 4621 static int ocfs2_divide_xattr_bucket(struct inode *inode, 4622 handle_t *handle, 4623 u64 blk, 4624 u64 new_blk, 4625 u32 *first_hash, 4626 int new_bucket_head) 4627 { 4628 int ret, i; 4629 int count, start, len, name_value_len = 0, name_offset = 0; 4630 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL; 4631 struct ocfs2_xattr_header *xh; 4632 struct ocfs2_xattr_entry *xe; 4633 int blocksize = inode->i_sb->s_blocksize; 4634 4635 trace_ocfs2_divide_xattr_bucket_begin((unsigned long long)blk, 4636 (unsigned long long)new_blk); 4637 4638 s_bucket = ocfs2_xattr_bucket_new(inode); 4639 t_bucket = ocfs2_xattr_bucket_new(inode); 4640 if (!s_bucket || !t_bucket) { 4641 ret = -ENOMEM; 4642 mlog_errno(ret); 4643 goto out; 4644 } 4645 4646 ret = ocfs2_read_xattr_bucket(s_bucket, blk); 4647 if (ret) { 4648 mlog_errno(ret); 4649 goto out; 4650 } 4651 4652 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket, 4653 OCFS2_JOURNAL_ACCESS_WRITE); 4654 if (ret) { 4655 mlog_errno(ret); 4656 goto out; 4657 } 4658 4659 /* 4660 * Even if !new_bucket_head, we're overwriting t_bucket. Thus, 4661 * there's no need to read it. 4662 */ 4663 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk, new_bucket_head); 4664 if (ret) { 4665 mlog_errno(ret); 4666 goto out; 4667 } 4668 4669 /* 4670 * Hey, if we're overwriting t_bucket, what difference does 4671 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the 4672 * same part of ocfs2_cp_xattr_bucket(). 4673 */ 4674 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket, 4675 new_bucket_head ? 4676 OCFS2_JOURNAL_ACCESS_CREATE : 4677 OCFS2_JOURNAL_ACCESS_WRITE); 4678 if (ret) { 4679 mlog_errno(ret); 4680 goto out; 4681 } 4682 4683 xh = bucket_xh(s_bucket); 4684 count = le16_to_cpu(xh->xh_count); 4685 start = ocfs2_xattr_find_divide_pos(xh); 4686 4687 if (start == count) { 4688 xe = &xh->xh_entries[start-1]; 4689 4690 /* 4691 * initialized a new empty bucket here. 4692 * The hash value is set as one larger than 4693 * that of the last entry in the previous bucket. 4694 */ 4695 for (i = 0; i < t_bucket->bu_blocks; i++) 4696 memset(bucket_block(t_bucket, i), 0, blocksize); 4697 4698 xh = bucket_xh(t_bucket); 4699 xh->xh_free_start = cpu_to_le16(blocksize); 4700 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash; 4701 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1); 4702 4703 goto set_num_buckets; 4704 } 4705 4706 /* copy the whole bucket to the new first. */ 4707 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket); 4708 4709 /* update the new bucket. */ 4710 xh = bucket_xh(t_bucket); 4711 4712 /* 4713 * Calculate the total name/value len and xh_free_start for 4714 * the old bucket first. 4715 */ 4716 name_offset = OCFS2_XATTR_BUCKET_SIZE; 4717 name_value_len = 0; 4718 for (i = 0; i < start; i++) { 4719 xe = &xh->xh_entries[i]; 4720 name_value_len += namevalue_size_xe(xe); 4721 if (le16_to_cpu(xe->xe_name_offset) < name_offset) 4722 name_offset = le16_to_cpu(xe->xe_name_offset); 4723 } 4724 4725 /* 4726 * Now begin the modification to the new bucket. 4727 * 4728 * In the new bucket, We just move the xattr entry to the beginning 4729 * and don't touch the name/value. So there will be some holes in the 4730 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is 4731 * called. 4732 */ 4733 xe = &xh->xh_entries[start]; 4734 len = sizeof(struct ocfs2_xattr_entry) * (count - start); 4735 trace_ocfs2_divide_xattr_bucket_move(len, 4736 (int)((char *)xe - (char *)xh), 4737 (int)((char *)xh->xh_entries - (char *)xh)); 4738 memmove((char *)xh->xh_entries, (char *)xe, len); 4739 xe = &xh->xh_entries[count - start]; 4740 len = sizeof(struct ocfs2_xattr_entry) * start; 4741 memset((char *)xe, 0, len); 4742 4743 le16_add_cpu(&xh->xh_count, -start); 4744 le16_add_cpu(&xh->xh_name_value_len, -name_value_len); 4745 4746 /* Calculate xh_free_start for the new bucket. */ 4747 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE); 4748 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { 4749 xe = &xh->xh_entries[i]; 4750 if (le16_to_cpu(xe->xe_name_offset) < 4751 le16_to_cpu(xh->xh_free_start)) 4752 xh->xh_free_start = xe->xe_name_offset; 4753 } 4754 4755 set_num_buckets: 4756 /* set xh->xh_num_buckets for the new xh. */ 4757 if (new_bucket_head) 4758 xh->xh_num_buckets = cpu_to_le16(1); 4759 else 4760 xh->xh_num_buckets = 0; 4761 4762 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket); 4763 4764 /* store the first_hash of the new bucket. */ 4765 if (first_hash) 4766 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash); 4767 4768 /* 4769 * Now only update the 1st block of the old bucket. If we 4770 * just added a new empty bucket, there is no need to modify 4771 * it. 4772 */ 4773 if (start == count) 4774 goto out; 4775 4776 xh = bucket_xh(s_bucket); 4777 memset(&xh->xh_entries[start], 0, 4778 sizeof(struct ocfs2_xattr_entry) * (count - start)); 4779 xh->xh_count = cpu_to_le16(start); 4780 xh->xh_free_start = cpu_to_le16(name_offset); 4781 xh->xh_name_value_len = cpu_to_le16(name_value_len); 4782 4783 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket); 4784 4785 out: 4786 ocfs2_xattr_bucket_free(s_bucket); 4787 ocfs2_xattr_bucket_free(t_bucket); 4788 4789 return ret; 4790 } 4791 4792 /* 4793 * Copy xattr from one bucket to another bucket. 4794 * 4795 * The caller must make sure that the journal transaction 4796 * has enough space for journaling. 4797 */ 4798 static int ocfs2_cp_xattr_bucket(struct inode *inode, 4799 handle_t *handle, 4800 u64 s_blkno, 4801 u64 t_blkno, 4802 int t_is_new) 4803 { 4804 int ret; 4805 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL; 4806 4807 BUG_ON(s_blkno == t_blkno); 4808 4809 trace_ocfs2_cp_xattr_bucket((unsigned long long)s_blkno, 4810 (unsigned long long)t_blkno, 4811 t_is_new); 4812 4813 s_bucket = ocfs2_xattr_bucket_new(inode); 4814 t_bucket = ocfs2_xattr_bucket_new(inode); 4815 if (!s_bucket || !t_bucket) { 4816 ret = -ENOMEM; 4817 mlog_errno(ret); 4818 goto out; 4819 } 4820 4821 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno); 4822 if (ret) 4823 goto out; 4824 4825 /* 4826 * Even if !t_is_new, we're overwriting t_bucket. Thus, 4827 * there's no need to read it. 4828 */ 4829 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno, t_is_new); 4830 if (ret) 4831 goto out; 4832 4833 /* 4834 * Hey, if we're overwriting t_bucket, what difference does 4835 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new 4836 * cluster to fill, we came here from 4837 * ocfs2_mv_xattr_buckets(), and it is really new - 4838 * ACCESS_CREATE is required. But we also might have moved data 4839 * out of t_bucket before extending back into it. 4840 * ocfs2_add_new_xattr_bucket() can do this - its call to 4841 * ocfs2_add_new_xattr_cluster() may have created a new extent 4842 * and copied out the end of the old extent. Then it re-extends 4843 * the old extent back to create space for new xattrs. That's 4844 * how we get here, and the bucket isn't really new. 4845 */ 4846 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket, 4847 t_is_new ? 4848 OCFS2_JOURNAL_ACCESS_CREATE : 4849 OCFS2_JOURNAL_ACCESS_WRITE); 4850 if (ret) 4851 goto out; 4852 4853 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket); 4854 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket); 4855 4856 out: 4857 ocfs2_xattr_bucket_free(t_bucket); 4858 ocfs2_xattr_bucket_free(s_bucket); 4859 4860 return ret; 4861 } 4862 4863 /* 4864 * src_blk points to the start of an existing extent. last_blk points to 4865 * last cluster in that extent. to_blk points to a newly allocated 4866 * extent. We copy the buckets from the cluster at last_blk to the new 4867 * extent. If start_bucket is non-zero, we skip that many buckets before 4868 * we start copying. The new extent's xh_num_buckets gets set to the 4869 * number of buckets we copied. The old extent's xh_num_buckets shrinks 4870 * by the same amount. 4871 */ 4872 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle, 4873 u64 src_blk, u64 last_blk, u64 to_blk, 4874 unsigned int start_bucket, 4875 u32 *first_hash) 4876 { 4877 int i, ret, credits; 4878 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 4879 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); 4880 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb); 4881 struct ocfs2_xattr_bucket *old_first, *new_first; 4882 4883 trace_ocfs2_mv_xattr_buckets((unsigned long long)last_blk, 4884 (unsigned long long)to_blk); 4885 4886 BUG_ON(start_bucket >= num_buckets); 4887 if (start_bucket) { 4888 num_buckets -= start_bucket; 4889 last_blk += (start_bucket * blks_per_bucket); 4890 } 4891 4892 /* The first bucket of the original extent */ 4893 old_first = ocfs2_xattr_bucket_new(inode); 4894 /* The first bucket of the new extent */ 4895 new_first = ocfs2_xattr_bucket_new(inode); 4896 if (!old_first || !new_first) { 4897 ret = -ENOMEM; 4898 mlog_errno(ret); 4899 goto out; 4900 } 4901 4902 ret = ocfs2_read_xattr_bucket(old_first, src_blk); 4903 if (ret) { 4904 mlog_errno(ret); 4905 goto out; 4906 } 4907 4908 /* 4909 * We need to update the first bucket of the old extent and all 4910 * the buckets going to the new extent. 4911 */ 4912 credits = ((num_buckets + 1) * blks_per_bucket); 4913 ret = ocfs2_extend_trans(handle, credits); 4914 if (ret) { 4915 mlog_errno(ret); 4916 goto out; 4917 } 4918 4919 ret = ocfs2_xattr_bucket_journal_access(handle, old_first, 4920 OCFS2_JOURNAL_ACCESS_WRITE); 4921 if (ret) { 4922 mlog_errno(ret); 4923 goto out; 4924 } 4925 4926 for (i = 0; i < num_buckets; i++) { 4927 ret = ocfs2_cp_xattr_bucket(inode, handle, 4928 last_blk + (i * blks_per_bucket), 4929 to_blk + (i * blks_per_bucket), 4930 1); 4931 if (ret) { 4932 mlog_errno(ret); 4933 goto out; 4934 } 4935 } 4936 4937 /* 4938 * Get the new bucket ready before we dirty anything 4939 * (This actually shouldn't fail, because we already dirtied 4940 * it once in ocfs2_cp_xattr_bucket()). 4941 */ 4942 ret = ocfs2_read_xattr_bucket(new_first, to_blk); 4943 if (ret) { 4944 mlog_errno(ret); 4945 goto out; 4946 } 4947 ret = ocfs2_xattr_bucket_journal_access(handle, new_first, 4948 OCFS2_JOURNAL_ACCESS_WRITE); 4949 if (ret) { 4950 mlog_errno(ret); 4951 goto out; 4952 } 4953 4954 /* Now update the headers */ 4955 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets); 4956 ocfs2_xattr_bucket_journal_dirty(handle, old_first); 4957 4958 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets); 4959 ocfs2_xattr_bucket_journal_dirty(handle, new_first); 4960 4961 if (first_hash) 4962 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash); 4963 4964 out: 4965 ocfs2_xattr_bucket_free(new_first); 4966 ocfs2_xattr_bucket_free(old_first); 4967 return ret; 4968 } 4969 4970 /* 4971 * Move some xattrs in this cluster to the new cluster. 4972 * This function should only be called when bucket size == cluster size. 4973 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead. 4974 */ 4975 static int ocfs2_divide_xattr_cluster(struct inode *inode, 4976 handle_t *handle, 4977 u64 prev_blk, 4978 u64 new_blk, 4979 u32 *first_hash) 4980 { 4981 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); 4982 int ret, credits = 2 * blk_per_bucket; 4983 4984 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize); 4985 4986 ret = ocfs2_extend_trans(handle, credits); 4987 if (ret) { 4988 mlog_errno(ret); 4989 return ret; 4990 } 4991 4992 /* Move half of the xattr in start_blk to the next bucket. */ 4993 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk, 4994 new_blk, first_hash, 1); 4995 } 4996 4997 /* 4998 * Move some xattrs from the old cluster to the new one since they are not 4999 * contiguous in ocfs2 xattr tree. 5000 * 5001 * new_blk starts a new separate cluster, and we will move some xattrs from 5002 * prev_blk to it. v_start will be set as the first name hash value in this 5003 * new cluster so that it can be used as e_cpos during tree insertion and 5004 * don't collide with our original b-tree operations. first_bh and header_bh 5005 * will also be updated since they will be used in ocfs2_extend_xattr_bucket 5006 * to extend the insert bucket. 5007 * 5008 * The problem is how much xattr should we move to the new one and when should 5009 * we update first_bh and header_bh? 5010 * 1. If cluster size > bucket size, that means the previous cluster has more 5011 * than 1 bucket, so just move half nums of bucket into the new cluster and 5012 * update the first_bh and header_bh if the insert bucket has been moved 5013 * to the new cluster. 5014 * 2. If cluster_size == bucket_size: 5015 * a) If the previous extent rec has more than one cluster and the insert 5016 * place isn't in the last cluster, copy the entire last cluster to the 5017 * new one. This time, we don't need to upate the first_bh and header_bh 5018 * since they will not be moved into the new cluster. 5019 * b) Otherwise, move the bottom half of the xattrs in the last cluster into 5020 * the new one. And we set the extend flag to zero if the insert place is 5021 * moved into the new allocated cluster since no extend is needed. 5022 */ 5023 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode, 5024 handle_t *handle, 5025 struct ocfs2_xattr_bucket *first, 5026 struct ocfs2_xattr_bucket *target, 5027 u64 new_blk, 5028 u32 prev_clusters, 5029 u32 *v_start, 5030 int *extend) 5031 { 5032 int ret; 5033 5034 trace_ocfs2_adjust_xattr_cross_cluster( 5035 (unsigned long long)bucket_blkno(first), 5036 (unsigned long long)new_blk, prev_clusters); 5037 5038 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) { 5039 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode, 5040 handle, 5041 first, target, 5042 new_blk, 5043 prev_clusters, 5044 v_start); 5045 if (ret) 5046 mlog_errno(ret); 5047 } else { 5048 /* The start of the last cluster in the first extent */ 5049 u64 last_blk = bucket_blkno(first) + 5050 ((prev_clusters - 1) * 5051 ocfs2_clusters_to_blocks(inode->i_sb, 1)); 5052 5053 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) { 5054 ret = ocfs2_mv_xattr_buckets(inode, handle, 5055 bucket_blkno(first), 5056 last_blk, new_blk, 0, 5057 v_start); 5058 if (ret) 5059 mlog_errno(ret); 5060 } else { 5061 ret = ocfs2_divide_xattr_cluster(inode, handle, 5062 last_blk, new_blk, 5063 v_start); 5064 if (ret) 5065 mlog_errno(ret); 5066 5067 if ((bucket_blkno(target) == last_blk) && extend) 5068 *extend = 0; 5069 } 5070 } 5071 5072 return ret; 5073 } 5074 5075 /* 5076 * Add a new cluster for xattr storage. 5077 * 5078 * If the new cluster is contiguous with the previous one, it will be 5079 * appended to the same extent record, and num_clusters will be updated. 5080 * If not, we will insert a new extent for it and move some xattrs in 5081 * the last cluster into the new allocated one. 5082 * We also need to limit the maximum size of a btree leaf, otherwise we'll 5083 * lose the benefits of hashing because we'll have to search large leaves. 5084 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize, 5085 * if it's bigger). 5086 * 5087 * first_bh is the first block of the previous extent rec and header_bh 5088 * indicates the bucket we will insert the new xattrs. They will be updated 5089 * when the header_bh is moved into the new cluster. 5090 */ 5091 static int ocfs2_add_new_xattr_cluster(struct inode *inode, 5092 struct buffer_head *root_bh, 5093 struct ocfs2_xattr_bucket *first, 5094 struct ocfs2_xattr_bucket *target, 5095 u32 *num_clusters, 5096 u32 prev_cpos, 5097 int *extend, 5098 struct ocfs2_xattr_set_ctxt *ctxt) 5099 { 5100 int ret; 5101 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); 5102 u32 prev_clusters = *num_clusters; 5103 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0; 5104 u64 block; 5105 handle_t *handle = ctxt->handle; 5106 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 5107 struct ocfs2_extent_tree et; 5108 5109 trace_ocfs2_add_new_xattr_cluster_begin( 5110 (unsigned long long)OCFS2_I(inode)->ip_blkno, 5111 (unsigned long long)bucket_blkno(first), 5112 prev_cpos, prev_clusters); 5113 5114 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh); 5115 5116 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh, 5117 OCFS2_JOURNAL_ACCESS_WRITE); 5118 if (ret < 0) { 5119 mlog_errno(ret); 5120 goto leave; 5121 } 5122 5123 ret = __ocfs2_claim_clusters(handle, ctxt->data_ac, 1, 5124 clusters_to_add, &bit_off, &num_bits); 5125 if (ret < 0) { 5126 if (ret != -ENOSPC) 5127 mlog_errno(ret); 5128 goto leave; 5129 } 5130 5131 BUG_ON(num_bits > clusters_to_add); 5132 5133 block = ocfs2_clusters_to_blocks(osb->sb, bit_off); 5134 trace_ocfs2_add_new_xattr_cluster((unsigned long long)block, num_bits); 5135 5136 if (bucket_blkno(first) + (prev_clusters * bpc) == block && 5137 (prev_clusters + num_bits) << osb->s_clustersize_bits <= 5138 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) { 5139 /* 5140 * If this cluster is contiguous with the old one and 5141 * adding this new cluster, we don't surpass the limit of 5142 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be 5143 * initialized and used like other buckets in the previous 5144 * cluster. 5145 * So add it as a contiguous one. The caller will handle 5146 * its init process. 5147 */ 5148 v_start = prev_cpos + prev_clusters; 5149 *num_clusters = prev_clusters + num_bits; 5150 } else { 5151 ret = ocfs2_adjust_xattr_cross_cluster(inode, 5152 handle, 5153 first, 5154 target, 5155 block, 5156 prev_clusters, 5157 &v_start, 5158 extend); 5159 if (ret) { 5160 mlog_errno(ret); 5161 goto leave; 5162 } 5163 } 5164 5165 trace_ocfs2_add_new_xattr_cluster_insert((unsigned long long)block, 5166 v_start, num_bits); 5167 ret = ocfs2_insert_extent(handle, &et, v_start, block, 5168 num_bits, 0, ctxt->meta_ac); 5169 if (ret < 0) { 5170 mlog_errno(ret); 5171 goto leave; 5172 } 5173 5174 ocfs2_journal_dirty(handle, root_bh); 5175 5176 leave: 5177 return ret; 5178 } 5179 5180 /* 5181 * We are given an extent. 'first' is the bucket at the very front of 5182 * the extent. The extent has space for an additional bucket past 5183 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number 5184 * of the target bucket. We wish to shift every bucket past the target 5185 * down one, filling in that additional space. When we get back to the 5186 * target, we split the target between itself and the now-empty bucket 5187 * at target+1 (aka, target_blkno + blks_per_bucket). 5188 */ 5189 static int ocfs2_extend_xattr_bucket(struct inode *inode, 5190 handle_t *handle, 5191 struct ocfs2_xattr_bucket *first, 5192 u64 target_blk, 5193 u32 num_clusters) 5194 { 5195 int ret, credits; 5196 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 5197 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); 5198 u64 end_blk; 5199 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets); 5200 5201 trace_ocfs2_extend_xattr_bucket((unsigned long long)target_blk, 5202 (unsigned long long)bucket_blkno(first), 5203 num_clusters, new_bucket); 5204 5205 /* The extent must have room for an additional bucket */ 5206 BUG_ON(new_bucket >= 5207 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb))); 5208 5209 /* end_blk points to the last existing bucket */ 5210 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket); 5211 5212 /* 5213 * end_blk is the start of the last existing bucket. 5214 * Thus, (end_blk - target_blk) covers the target bucket and 5215 * every bucket after it up to, but not including, the last 5216 * existing bucket. Then we add the last existing bucket, the 5217 * new bucket, and the first bucket (3 * blk_per_bucket). 5218 */ 5219 credits = (end_blk - target_blk) + (3 * blk_per_bucket); 5220 ret = ocfs2_extend_trans(handle, credits); 5221 if (ret) { 5222 mlog_errno(ret); 5223 goto out; 5224 } 5225 5226 ret = ocfs2_xattr_bucket_journal_access(handle, first, 5227 OCFS2_JOURNAL_ACCESS_WRITE); 5228 if (ret) { 5229 mlog_errno(ret); 5230 goto out; 5231 } 5232 5233 while (end_blk != target_blk) { 5234 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk, 5235 end_blk + blk_per_bucket, 0); 5236 if (ret) 5237 goto out; 5238 end_blk -= blk_per_bucket; 5239 } 5240 5241 /* Move half of the xattr in target_blkno to the next bucket. */ 5242 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk, 5243 target_blk + blk_per_bucket, NULL, 0); 5244 5245 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1); 5246 ocfs2_xattr_bucket_journal_dirty(handle, first); 5247 5248 out: 5249 return ret; 5250 } 5251 5252 /* 5253 * Add new xattr bucket in an extent record and adjust the buckets 5254 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the 5255 * bucket we want to insert into. 5256 * 5257 * In the easy case, we will move all the buckets after target down by 5258 * one. Half of target's xattrs will be moved to the next bucket. 5259 * 5260 * If current cluster is full, we'll allocate a new one. This may not 5261 * be contiguous. The underlying calls will make sure that there is 5262 * space for the insert, shifting buckets around if necessary. 5263 * 'target' may be moved by those calls. 5264 */ 5265 static int ocfs2_add_new_xattr_bucket(struct inode *inode, 5266 struct buffer_head *xb_bh, 5267 struct ocfs2_xattr_bucket *target, 5268 struct ocfs2_xattr_set_ctxt *ctxt) 5269 { 5270 struct ocfs2_xattr_block *xb = 5271 (struct ocfs2_xattr_block *)xb_bh->b_data; 5272 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root; 5273 struct ocfs2_extent_list *el = &xb_root->xt_list; 5274 u32 name_hash = 5275 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash); 5276 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 5277 int ret, num_buckets, extend = 1; 5278 u64 p_blkno; 5279 u32 e_cpos, num_clusters; 5280 /* The bucket at the front of the extent */ 5281 struct ocfs2_xattr_bucket *first; 5282 5283 trace_ocfs2_add_new_xattr_bucket( 5284 (unsigned long long)bucket_blkno(target)); 5285 5286 /* The first bucket of the original extent */ 5287 first = ocfs2_xattr_bucket_new(inode); 5288 if (!first) { 5289 ret = -ENOMEM; 5290 mlog_errno(ret); 5291 goto out; 5292 } 5293 5294 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos, 5295 &num_clusters, el); 5296 if (ret) { 5297 mlog_errno(ret); 5298 goto out; 5299 } 5300 5301 ret = ocfs2_read_xattr_bucket(first, p_blkno); 5302 if (ret) { 5303 mlog_errno(ret); 5304 goto out; 5305 } 5306 5307 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters; 5308 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) { 5309 /* 5310 * This can move first+target if the target bucket moves 5311 * to the new extent. 5312 */ 5313 ret = ocfs2_add_new_xattr_cluster(inode, 5314 xb_bh, 5315 first, 5316 target, 5317 &num_clusters, 5318 e_cpos, 5319 &extend, 5320 ctxt); 5321 if (ret) { 5322 mlog_errno(ret); 5323 goto out; 5324 } 5325 } 5326 5327 if (extend) { 5328 ret = ocfs2_extend_xattr_bucket(inode, 5329 ctxt->handle, 5330 first, 5331 bucket_blkno(target), 5332 num_clusters); 5333 if (ret) 5334 mlog_errno(ret); 5335 } 5336 5337 out: 5338 ocfs2_xattr_bucket_free(first); 5339 5340 return ret; 5341 } 5342 5343 /* 5344 * Truncate the specified xe_off entry in xattr bucket. 5345 * bucket is indicated by header_bh and len is the new length. 5346 * Both the ocfs2_xattr_value_root and the entry will be updated here. 5347 * 5348 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed. 5349 */ 5350 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode, 5351 struct ocfs2_xattr_bucket *bucket, 5352 int xe_off, 5353 int len, 5354 struct ocfs2_xattr_set_ctxt *ctxt) 5355 { 5356 int ret, offset; 5357 u64 value_blk; 5358 struct ocfs2_xattr_entry *xe; 5359 struct ocfs2_xattr_header *xh = bucket_xh(bucket); 5360 size_t blocksize = inode->i_sb->s_blocksize; 5361 struct ocfs2_xattr_value_buf vb = { 5362 .vb_access = ocfs2_journal_access, 5363 }; 5364 5365 xe = &xh->xh_entries[xe_off]; 5366 5367 BUG_ON(!xe || ocfs2_xattr_is_local(xe)); 5368 5369 offset = le16_to_cpu(xe->xe_name_offset) + 5370 OCFS2_XATTR_SIZE(xe->xe_name_len); 5371 5372 value_blk = offset / blocksize; 5373 5374 /* We don't allow ocfs2_xattr_value to be stored in different block. */ 5375 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize); 5376 5377 vb.vb_bh = bucket->bu_bhs[value_blk]; 5378 BUG_ON(!vb.vb_bh); 5379 5380 vb.vb_xv = (struct ocfs2_xattr_value_root *) 5381 (vb.vb_bh->b_data + offset % blocksize); 5382 5383 /* 5384 * From here on out we have to dirty the bucket. The generic 5385 * value calls only modify one of the bucket's bhs, but we need 5386 * to send the bucket at once. So if they error, they *could* have 5387 * modified something. We have to assume they did, and dirty 5388 * the whole bucket. This leaves us in a consistent state. 5389 */ 5390 trace_ocfs2_xattr_bucket_value_truncate( 5391 (unsigned long long)bucket_blkno(bucket), xe_off, len); 5392 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt); 5393 if (ret) { 5394 mlog_errno(ret); 5395 goto out; 5396 } 5397 5398 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket, 5399 OCFS2_JOURNAL_ACCESS_WRITE); 5400 if (ret) { 5401 mlog_errno(ret); 5402 goto out; 5403 } 5404 5405 xe->xe_value_size = cpu_to_le64(len); 5406 5407 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket); 5408 5409 out: 5410 return ret; 5411 } 5412 5413 static int ocfs2_rm_xattr_cluster(struct inode *inode, 5414 struct buffer_head *root_bh, 5415 u64 blkno, 5416 u32 cpos, 5417 u32 len, 5418 void *para) 5419 { 5420 int ret; 5421 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 5422 struct inode *tl_inode = osb->osb_tl_inode; 5423 handle_t *handle; 5424 struct ocfs2_xattr_block *xb = 5425 (struct ocfs2_xattr_block *)root_bh->b_data; 5426 struct ocfs2_alloc_context *meta_ac = NULL; 5427 struct ocfs2_cached_dealloc_ctxt dealloc; 5428 struct ocfs2_extent_tree et; 5429 5430 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len, 5431 ocfs2_delete_xattr_in_bucket, para); 5432 if (ret) { 5433 mlog_errno(ret); 5434 return ret; 5435 } 5436 5437 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh); 5438 5439 ocfs2_init_dealloc_ctxt(&dealloc); 5440 5441 trace_ocfs2_rm_xattr_cluster( 5442 (unsigned long long)OCFS2_I(inode)->ip_blkno, 5443 (unsigned long long)blkno, cpos, len); 5444 5445 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno, 5446 len); 5447 5448 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac); 5449 if (ret) { 5450 mlog_errno(ret); 5451 return ret; 5452 } 5453 5454 inode_lock(tl_inode); 5455 5456 if (ocfs2_truncate_log_needs_flush(osb)) { 5457 ret = __ocfs2_flush_truncate_log(osb); 5458 if (ret < 0) { 5459 mlog_errno(ret); 5460 goto out; 5461 } 5462 } 5463 5464 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb)); 5465 if (IS_ERR(handle)) { 5466 ret = -ENOMEM; 5467 mlog_errno(ret); 5468 goto out; 5469 } 5470 5471 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh, 5472 OCFS2_JOURNAL_ACCESS_WRITE); 5473 if (ret) { 5474 mlog_errno(ret); 5475 goto out_commit; 5476 } 5477 5478 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac, 5479 &dealloc); 5480 if (ret) { 5481 mlog_errno(ret); 5482 goto out_commit; 5483 } 5484 5485 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len); 5486 ocfs2_journal_dirty(handle, root_bh); 5487 5488 ret = ocfs2_truncate_log_append(osb, handle, blkno, len); 5489 if (ret) 5490 mlog_errno(ret); 5491 ocfs2_update_inode_fsync_trans(handle, inode, 0); 5492 5493 out_commit: 5494 ocfs2_commit_trans(osb, handle); 5495 out: 5496 ocfs2_schedule_truncate_log_flush(osb, 1); 5497 5498 inode_unlock(tl_inode); 5499 5500 if (meta_ac) 5501 ocfs2_free_alloc_context(meta_ac); 5502 5503 ocfs2_run_deallocs(osb, &dealloc); 5504 5505 return ret; 5506 } 5507 5508 /* 5509 * check whether the xattr bucket is filled up with the same hash value. 5510 * If we want to insert the xattr with the same hash, return -ENOSPC. 5511 * If we want to insert a xattr with different hash value, go ahead 5512 * and ocfs2_divide_xattr_bucket will handle this. 5513 */ 5514 static int ocfs2_check_xattr_bucket_collision(struct inode *inode, 5515 struct ocfs2_xattr_bucket *bucket, 5516 const char *name) 5517 { 5518 struct ocfs2_xattr_header *xh = bucket_xh(bucket); 5519 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name)); 5520 5521 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash)) 5522 return 0; 5523 5524 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash == 5525 xh->xh_entries[0].xe_name_hash) { 5526 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, " 5527 "hash = %u\n", 5528 (unsigned long long)bucket_blkno(bucket), 5529 le32_to_cpu(xh->xh_entries[0].xe_name_hash)); 5530 return -ENOSPC; 5531 } 5532 5533 return 0; 5534 } 5535 5536 /* 5537 * Try to set the entry in the current bucket. If we fail, the caller 5538 * will handle getting us another bucket. 5539 */ 5540 static int ocfs2_xattr_set_entry_bucket(struct inode *inode, 5541 struct ocfs2_xattr_info *xi, 5542 struct ocfs2_xattr_search *xs, 5543 struct ocfs2_xattr_set_ctxt *ctxt) 5544 { 5545 int ret; 5546 struct ocfs2_xa_loc loc; 5547 5548 trace_ocfs2_xattr_set_entry_bucket(xi->xi_name); 5549 5550 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket, 5551 xs->not_found ? NULL : xs->here); 5552 ret = ocfs2_xa_set(&loc, xi, ctxt); 5553 if (!ret) { 5554 xs->here = loc.xl_entry; 5555 goto out; 5556 } 5557 if (ret != -ENOSPC) { 5558 mlog_errno(ret); 5559 goto out; 5560 } 5561 5562 /* Ok, we need space. Let's try defragmenting the bucket. */ 5563 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle, 5564 xs->bucket); 5565 if (ret) { 5566 mlog_errno(ret); 5567 goto out; 5568 } 5569 5570 ret = ocfs2_xa_set(&loc, xi, ctxt); 5571 if (!ret) { 5572 xs->here = loc.xl_entry; 5573 goto out; 5574 } 5575 if (ret != -ENOSPC) 5576 mlog_errno(ret); 5577 5578 5579 out: 5580 return ret; 5581 } 5582 5583 static int ocfs2_xattr_set_entry_index_block(struct inode *inode, 5584 struct ocfs2_xattr_info *xi, 5585 struct ocfs2_xattr_search *xs, 5586 struct ocfs2_xattr_set_ctxt *ctxt) 5587 { 5588 int ret; 5589 5590 trace_ocfs2_xattr_set_entry_index_block(xi->xi_name); 5591 5592 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt); 5593 if (!ret) 5594 goto out; 5595 if (ret != -ENOSPC) { 5596 mlog_errno(ret); 5597 goto out; 5598 } 5599 5600 /* Ack, need more space. Let's try to get another bucket! */ 5601 5602 /* 5603 * We do not allow for overlapping ranges between buckets. And 5604 * the maximum number of collisions we will allow for then is 5605 * one bucket's worth, so check it here whether we need to 5606 * add a new bucket for the insert. 5607 */ 5608 ret = ocfs2_check_xattr_bucket_collision(inode, 5609 xs->bucket, 5610 xi->xi_name); 5611 if (ret) { 5612 mlog_errno(ret); 5613 goto out; 5614 } 5615 5616 ret = ocfs2_add_new_xattr_bucket(inode, 5617 xs->xattr_bh, 5618 xs->bucket, 5619 ctxt); 5620 if (ret) { 5621 mlog_errno(ret); 5622 goto out; 5623 } 5624 5625 /* 5626 * ocfs2_add_new_xattr_bucket() will have updated 5627 * xs->bucket if it moved, but it will not have updated 5628 * any of the other search fields. Thus, we drop it and 5629 * re-search. Everything should be cached, so it'll be 5630 * quick. 5631 */ 5632 ocfs2_xattr_bucket_relse(xs->bucket); 5633 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh, 5634 xi->xi_name_index, 5635 xi->xi_name, xs); 5636 if (ret && ret != -ENODATA) 5637 goto out; 5638 xs->not_found = ret; 5639 5640 /* Ok, we have a new bucket, let's try again */ 5641 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt); 5642 if (ret && (ret != -ENOSPC)) 5643 mlog_errno(ret); 5644 5645 out: 5646 return ret; 5647 } 5648 5649 static int ocfs2_delete_xattr_in_bucket(struct inode *inode, 5650 struct ocfs2_xattr_bucket *bucket, 5651 void *para) 5652 { 5653 int ret = 0, ref_credits; 5654 struct ocfs2_xattr_header *xh = bucket_xh(bucket); 5655 u16 i; 5656 struct ocfs2_xattr_entry *xe; 5657 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 5658 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,}; 5659 int credits = ocfs2_remove_extent_credits(osb->sb) + 5660 ocfs2_blocks_per_xattr_bucket(inode->i_sb); 5661 struct ocfs2_xattr_value_root *xv; 5662 struct ocfs2_rm_xattr_bucket_para *args = 5663 (struct ocfs2_rm_xattr_bucket_para *)para; 5664 5665 ocfs2_init_dealloc_ctxt(&ctxt.dealloc); 5666 5667 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { 5668 xe = &xh->xh_entries[i]; 5669 if (ocfs2_xattr_is_local(xe)) 5670 continue; 5671 5672 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, 5673 i, &xv, NULL); 5674 if (ret) { 5675 mlog_errno(ret); 5676 break; 5677 } 5678 5679 ret = ocfs2_lock_xattr_remove_allocators(inode, xv, 5680 args->ref_ci, 5681 args->ref_root_bh, 5682 &ctxt.meta_ac, 5683 &ref_credits); 5684 5685 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits); 5686 if (IS_ERR(ctxt.handle)) { 5687 ret = PTR_ERR(ctxt.handle); 5688 mlog_errno(ret); 5689 break; 5690 } 5691 5692 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket, 5693 i, 0, &ctxt); 5694 5695 ocfs2_commit_trans(osb, ctxt.handle); 5696 if (ctxt.meta_ac) { 5697 ocfs2_free_alloc_context(ctxt.meta_ac); 5698 ctxt.meta_ac = NULL; 5699 } 5700 if (ret) { 5701 mlog_errno(ret); 5702 break; 5703 } 5704 } 5705 5706 if (ctxt.meta_ac) 5707 ocfs2_free_alloc_context(ctxt.meta_ac); 5708 ocfs2_schedule_truncate_log_flush(osb, 1); 5709 ocfs2_run_deallocs(osb, &ctxt.dealloc); 5710 return ret; 5711 } 5712 5713 /* 5714 * Whenever we modify a xattr value root in the bucket(e.g, CoW 5715 * or change the extent record flag), we need to recalculate 5716 * the metaecc for the whole bucket. So it is done here. 5717 * 5718 * Note: 5719 * We have to give the extra credits for the caller. 5720 */ 5721 static int ocfs2_xattr_bucket_post_refcount(struct inode *inode, 5722 handle_t *handle, 5723 void *para) 5724 { 5725 int ret; 5726 struct ocfs2_xattr_bucket *bucket = 5727 (struct ocfs2_xattr_bucket *)para; 5728 5729 ret = ocfs2_xattr_bucket_journal_access(handle, bucket, 5730 OCFS2_JOURNAL_ACCESS_WRITE); 5731 if (ret) { 5732 mlog_errno(ret); 5733 return ret; 5734 } 5735 5736 ocfs2_xattr_bucket_journal_dirty(handle, bucket); 5737 5738 return 0; 5739 } 5740 5741 /* 5742 * Special action we need if the xattr value is refcounted. 5743 * 5744 * 1. If the xattr is refcounted, lock the tree. 5745 * 2. CoW the xattr if we are setting the new value and the value 5746 * will be stored outside. 5747 * 3. In other case, decrease_refcount will work for us, so just 5748 * lock the refcount tree, calculate the meta and credits is OK. 5749 * 5750 * We have to do CoW before ocfs2_init_xattr_set_ctxt since 5751 * currently CoW is a completed transaction, while this function 5752 * will also lock the allocators and let us deadlock. So we will 5753 * CoW the whole xattr value. 5754 */ 5755 static int ocfs2_prepare_refcount_xattr(struct inode *inode, 5756 struct ocfs2_dinode *di, 5757 struct ocfs2_xattr_info *xi, 5758 struct ocfs2_xattr_search *xis, 5759 struct ocfs2_xattr_search *xbs, 5760 struct ocfs2_refcount_tree **ref_tree, 5761 int *meta_add, 5762 int *credits) 5763 { 5764 int ret = 0; 5765 struct ocfs2_xattr_block *xb; 5766 struct ocfs2_xattr_entry *xe; 5767 char *base; 5768 u32 p_cluster, num_clusters; 5769 unsigned int ext_flags; 5770 int name_offset, name_len; 5771 struct ocfs2_xattr_value_buf vb; 5772 struct ocfs2_xattr_bucket *bucket = NULL; 5773 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 5774 struct ocfs2_post_refcount refcount; 5775 struct ocfs2_post_refcount *p = NULL; 5776 struct buffer_head *ref_root_bh = NULL; 5777 5778 if (!xis->not_found) { 5779 xe = xis->here; 5780 name_offset = le16_to_cpu(xe->xe_name_offset); 5781 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len); 5782 base = xis->base; 5783 vb.vb_bh = xis->inode_bh; 5784 vb.vb_access = ocfs2_journal_access_di; 5785 } else { 5786 int i, block_off = 0; 5787 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data; 5788 xe = xbs->here; 5789 name_offset = le16_to_cpu(xe->xe_name_offset); 5790 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len); 5791 i = xbs->here - xbs->header->xh_entries; 5792 5793 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) { 5794 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, 5795 bucket_xh(xbs->bucket), 5796 i, &block_off, 5797 &name_offset); 5798 if (ret) { 5799 mlog_errno(ret); 5800 goto out; 5801 } 5802 base = bucket_block(xbs->bucket, block_off); 5803 vb.vb_bh = xbs->bucket->bu_bhs[block_off]; 5804 vb.vb_access = ocfs2_journal_access; 5805 5806 if (ocfs2_meta_ecc(osb)) { 5807 /*create parameters for ocfs2_post_refcount. */ 5808 bucket = xbs->bucket; 5809 refcount.credits = bucket->bu_blocks; 5810 refcount.para = bucket; 5811 refcount.func = 5812 ocfs2_xattr_bucket_post_refcount; 5813 p = &refcount; 5814 } 5815 } else { 5816 base = xbs->base; 5817 vb.vb_bh = xbs->xattr_bh; 5818 vb.vb_access = ocfs2_journal_access_xb; 5819 } 5820 } 5821 5822 if (ocfs2_xattr_is_local(xe)) 5823 goto out; 5824 5825 vb.vb_xv = (struct ocfs2_xattr_value_root *) 5826 (base + name_offset + name_len); 5827 5828 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster, 5829 &num_clusters, &vb.vb_xv->xr_list, 5830 &ext_flags); 5831 if (ret) { 5832 mlog_errno(ret); 5833 goto out; 5834 } 5835 5836 /* 5837 * We just need to check the 1st extent record, since we always 5838 * CoW the whole xattr. So there shouldn't be a xattr with 5839 * some REFCOUNT extent recs after the 1st one. 5840 */ 5841 if (!(ext_flags & OCFS2_EXT_REFCOUNTED)) 5842 goto out; 5843 5844 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc), 5845 1, ref_tree, &ref_root_bh); 5846 if (ret) { 5847 mlog_errno(ret); 5848 goto out; 5849 } 5850 5851 /* 5852 * If we are deleting the xattr or the new size will be stored inside, 5853 * cool, leave it there, the xattr truncate process will remove them 5854 * for us(it still needs the refcount tree lock and the meta, credits). 5855 * And the worse case is that every cluster truncate will split the 5856 * refcount tree, and make the original extent become 3. So we will need 5857 * 2 * cluster more extent recs at most. 5858 */ 5859 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) { 5860 5861 ret = ocfs2_refcounted_xattr_delete_need(inode, 5862 &(*ref_tree)->rf_ci, 5863 ref_root_bh, vb.vb_xv, 5864 meta_add, credits); 5865 if (ret) 5866 mlog_errno(ret); 5867 goto out; 5868 } 5869 5870 ret = ocfs2_refcount_cow_xattr(inode, di, &vb, 5871 *ref_tree, ref_root_bh, 0, 5872 le32_to_cpu(vb.vb_xv->xr_clusters), p); 5873 if (ret) 5874 mlog_errno(ret); 5875 5876 out: 5877 brelse(ref_root_bh); 5878 return ret; 5879 } 5880 5881 /* 5882 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root. 5883 * The physical clusters will be added to refcount tree. 5884 */ 5885 static int ocfs2_xattr_value_attach_refcount(struct inode *inode, 5886 struct ocfs2_xattr_value_root *xv, 5887 struct ocfs2_extent_tree *value_et, 5888 struct ocfs2_caching_info *ref_ci, 5889 struct buffer_head *ref_root_bh, 5890 struct ocfs2_cached_dealloc_ctxt *dealloc, 5891 struct ocfs2_post_refcount *refcount) 5892 { 5893 int ret = 0; 5894 u32 clusters = le32_to_cpu(xv->xr_clusters); 5895 u32 cpos, p_cluster, num_clusters; 5896 struct ocfs2_extent_list *el = &xv->xr_list; 5897 unsigned int ext_flags; 5898 5899 cpos = 0; 5900 while (cpos < clusters) { 5901 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster, 5902 &num_clusters, el, &ext_flags); 5903 if (ret) { 5904 mlog_errno(ret); 5905 break; 5906 } 5907 5908 cpos += num_clusters; 5909 if ((ext_flags & OCFS2_EXT_REFCOUNTED)) 5910 continue; 5911 5912 BUG_ON(!p_cluster); 5913 5914 ret = ocfs2_add_refcount_flag(inode, value_et, 5915 ref_ci, ref_root_bh, 5916 cpos - num_clusters, 5917 p_cluster, num_clusters, 5918 dealloc, refcount); 5919 if (ret) { 5920 mlog_errno(ret); 5921 break; 5922 } 5923 } 5924 5925 return ret; 5926 } 5927 5928 /* 5929 * Given a normal ocfs2_xattr_header, refcount all the entries which 5930 * have value stored outside. 5931 * Used for xattrs stored in inode and ocfs2_xattr_block. 5932 */ 5933 static int ocfs2_xattr_attach_refcount_normal(struct inode *inode, 5934 struct ocfs2_xattr_value_buf *vb, 5935 struct ocfs2_xattr_header *header, 5936 struct ocfs2_caching_info *ref_ci, 5937 struct buffer_head *ref_root_bh, 5938 struct ocfs2_cached_dealloc_ctxt *dealloc) 5939 { 5940 5941 struct ocfs2_xattr_entry *xe; 5942 struct ocfs2_xattr_value_root *xv; 5943 struct ocfs2_extent_tree et; 5944 int i, ret = 0; 5945 5946 for (i = 0; i < le16_to_cpu(header->xh_count); i++) { 5947 xe = &header->xh_entries[i]; 5948 5949 if (ocfs2_xattr_is_local(xe)) 5950 continue; 5951 5952 xv = (struct ocfs2_xattr_value_root *)((void *)header + 5953 le16_to_cpu(xe->xe_name_offset) + 5954 OCFS2_XATTR_SIZE(xe->xe_name_len)); 5955 5956 vb->vb_xv = xv; 5957 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb); 5958 5959 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et, 5960 ref_ci, ref_root_bh, 5961 dealloc, NULL); 5962 if (ret) { 5963 mlog_errno(ret); 5964 break; 5965 } 5966 } 5967 5968 return ret; 5969 } 5970 5971 static int ocfs2_xattr_inline_attach_refcount(struct inode *inode, 5972 struct buffer_head *fe_bh, 5973 struct ocfs2_caching_info *ref_ci, 5974 struct buffer_head *ref_root_bh, 5975 struct ocfs2_cached_dealloc_ctxt *dealloc) 5976 { 5977 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data; 5978 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *) 5979 (fe_bh->b_data + inode->i_sb->s_blocksize - 5980 le16_to_cpu(di->i_xattr_inline_size)); 5981 struct ocfs2_xattr_value_buf vb = { 5982 .vb_bh = fe_bh, 5983 .vb_access = ocfs2_journal_access_di, 5984 }; 5985 5986 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header, 5987 ref_ci, ref_root_bh, dealloc); 5988 } 5989 5990 struct ocfs2_xattr_tree_value_refcount_para { 5991 struct ocfs2_caching_info *ref_ci; 5992 struct buffer_head *ref_root_bh; 5993 struct ocfs2_cached_dealloc_ctxt *dealloc; 5994 }; 5995 5996 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb, 5997 struct ocfs2_xattr_bucket *bucket, 5998 int offset, 5999 struct ocfs2_xattr_value_root **xv, 6000 struct buffer_head **bh) 6001 { 6002 int ret, block_off, name_offset; 6003 struct ocfs2_xattr_header *xh = bucket_xh(bucket); 6004 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset]; 6005 void *base; 6006 6007 ret = ocfs2_xattr_bucket_get_name_value(sb, 6008 bucket_xh(bucket), 6009 offset, 6010 &block_off, 6011 &name_offset); 6012 if (ret) { 6013 mlog_errno(ret); 6014 goto out; 6015 } 6016 6017 base = bucket_block(bucket, block_off); 6018 6019 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset + 6020 OCFS2_XATTR_SIZE(xe->xe_name_len)); 6021 6022 if (bh) 6023 *bh = bucket->bu_bhs[block_off]; 6024 out: 6025 return ret; 6026 } 6027 6028 /* 6029 * For a given xattr bucket, refcount all the entries which 6030 * have value stored outside. 6031 */ 6032 static int ocfs2_xattr_bucket_value_refcount(struct inode *inode, 6033 struct ocfs2_xattr_bucket *bucket, 6034 void *para) 6035 { 6036 int i, ret = 0; 6037 struct ocfs2_extent_tree et; 6038 struct ocfs2_xattr_tree_value_refcount_para *ref = 6039 (struct ocfs2_xattr_tree_value_refcount_para *)para; 6040 struct ocfs2_xattr_header *xh = 6041 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data; 6042 struct ocfs2_xattr_entry *xe; 6043 struct ocfs2_xattr_value_buf vb = { 6044 .vb_access = ocfs2_journal_access, 6045 }; 6046 struct ocfs2_post_refcount refcount = { 6047 .credits = bucket->bu_blocks, 6048 .para = bucket, 6049 .func = ocfs2_xattr_bucket_post_refcount, 6050 }; 6051 struct ocfs2_post_refcount *p = NULL; 6052 6053 /* We only need post_refcount if we support metaecc. */ 6054 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb))) 6055 p = &refcount; 6056 6057 trace_ocfs2_xattr_bucket_value_refcount( 6058 (unsigned long long)bucket_blkno(bucket), 6059 le16_to_cpu(xh->xh_count)); 6060 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { 6061 xe = &xh->xh_entries[i]; 6062 6063 if (ocfs2_xattr_is_local(xe)) 6064 continue; 6065 6066 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i, 6067 &vb.vb_xv, &vb.vb_bh); 6068 if (ret) { 6069 mlog_errno(ret); 6070 break; 6071 } 6072 6073 ocfs2_init_xattr_value_extent_tree(&et, 6074 INODE_CACHE(inode), &vb); 6075 6076 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv, 6077 &et, ref->ref_ci, 6078 ref->ref_root_bh, 6079 ref->dealloc, p); 6080 if (ret) { 6081 mlog_errno(ret); 6082 break; 6083 } 6084 } 6085 6086 return ret; 6087 6088 } 6089 6090 static int ocfs2_refcount_xattr_tree_rec(struct inode *inode, 6091 struct buffer_head *root_bh, 6092 u64 blkno, u32 cpos, u32 len, void *para) 6093 { 6094 return ocfs2_iterate_xattr_buckets(inode, blkno, len, 6095 ocfs2_xattr_bucket_value_refcount, 6096 para); 6097 } 6098 6099 static int ocfs2_xattr_block_attach_refcount(struct inode *inode, 6100 struct buffer_head *blk_bh, 6101 struct ocfs2_caching_info *ref_ci, 6102 struct buffer_head *ref_root_bh, 6103 struct ocfs2_cached_dealloc_ctxt *dealloc) 6104 { 6105 int ret = 0; 6106 struct ocfs2_xattr_block *xb = 6107 (struct ocfs2_xattr_block *)blk_bh->b_data; 6108 6109 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) { 6110 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header; 6111 struct ocfs2_xattr_value_buf vb = { 6112 .vb_bh = blk_bh, 6113 .vb_access = ocfs2_journal_access_xb, 6114 }; 6115 6116 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header, 6117 ref_ci, ref_root_bh, 6118 dealloc); 6119 } else { 6120 struct ocfs2_xattr_tree_value_refcount_para para = { 6121 .ref_ci = ref_ci, 6122 .ref_root_bh = ref_root_bh, 6123 .dealloc = dealloc, 6124 }; 6125 6126 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh, 6127 ocfs2_refcount_xattr_tree_rec, 6128 ¶); 6129 } 6130 6131 return ret; 6132 } 6133 6134 int ocfs2_xattr_attach_refcount_tree(struct inode *inode, 6135 struct buffer_head *fe_bh, 6136 struct ocfs2_caching_info *ref_ci, 6137 struct buffer_head *ref_root_bh, 6138 struct ocfs2_cached_dealloc_ctxt *dealloc) 6139 { 6140 int ret = 0; 6141 struct ocfs2_inode_info *oi = OCFS2_I(inode); 6142 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data; 6143 struct buffer_head *blk_bh = NULL; 6144 6145 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) { 6146 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh, 6147 ref_ci, ref_root_bh, 6148 dealloc); 6149 if (ret) { 6150 mlog_errno(ret); 6151 goto out; 6152 } 6153 } 6154 6155 if (!di->i_xattr_loc) 6156 goto out; 6157 6158 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc), 6159 &blk_bh); 6160 if (ret < 0) { 6161 mlog_errno(ret); 6162 goto out; 6163 } 6164 6165 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci, 6166 ref_root_bh, dealloc); 6167 if (ret) 6168 mlog_errno(ret); 6169 6170 brelse(blk_bh); 6171 out: 6172 6173 return ret; 6174 } 6175 6176 typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe); 6177 /* 6178 * Store the information we need in xattr reflink. 6179 * old_bh and new_bh are inode bh for the old and new inode. 6180 */ 6181 struct ocfs2_xattr_reflink { 6182 struct inode *old_inode; 6183 struct inode *new_inode; 6184 struct buffer_head *old_bh; 6185 struct buffer_head *new_bh; 6186 struct ocfs2_caching_info *ref_ci; 6187 struct buffer_head *ref_root_bh; 6188 struct ocfs2_cached_dealloc_ctxt *dealloc; 6189 should_xattr_reflinked *xattr_reflinked; 6190 }; 6191 6192 /* 6193 * Given a xattr header and xe offset, 6194 * return the proper xv and the corresponding bh. 6195 * xattr in inode, block and xattr tree have different implementaions. 6196 */ 6197 typedef int (get_xattr_value_root)(struct super_block *sb, 6198 struct buffer_head *bh, 6199 struct ocfs2_xattr_header *xh, 6200 int offset, 6201 struct ocfs2_xattr_value_root **xv, 6202 struct buffer_head **ret_bh, 6203 void *para); 6204 6205 /* 6206 * Calculate all the xattr value root metadata stored in this xattr header and 6207 * credits we need if we create them from the scratch. 6208 * We use get_xattr_value_root so that all types of xattr container can use it. 6209 */ 6210 static int ocfs2_value_metas_in_xattr_header(struct super_block *sb, 6211 struct buffer_head *bh, 6212 struct ocfs2_xattr_header *xh, 6213 int *metas, int *credits, 6214 int *num_recs, 6215 get_xattr_value_root *func, 6216 void *para) 6217 { 6218 int i, ret = 0; 6219 struct ocfs2_xattr_value_root *xv; 6220 struct ocfs2_xattr_entry *xe; 6221 6222 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { 6223 xe = &xh->xh_entries[i]; 6224 if (ocfs2_xattr_is_local(xe)) 6225 continue; 6226 6227 ret = func(sb, bh, xh, i, &xv, NULL, para); 6228 if (ret) { 6229 mlog_errno(ret); 6230 break; 6231 } 6232 6233 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) * 6234 le16_to_cpu(xv->xr_list.l_next_free_rec); 6235 6236 *credits += ocfs2_calc_extend_credits(sb, 6237 &def_xv.xv.xr_list); 6238 6239 /* 6240 * If the value is a tree with depth > 1, We don't go deep 6241 * to the extent block, so just calculate a maximum record num. 6242 */ 6243 if (!xv->xr_list.l_tree_depth) 6244 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec); 6245 else 6246 *num_recs += ocfs2_clusters_for_bytes(sb, 6247 XATTR_SIZE_MAX); 6248 } 6249 6250 return ret; 6251 } 6252 6253 /* Used by xattr inode and block to return the right xv and buffer_head. */ 6254 static int ocfs2_get_xattr_value_root(struct super_block *sb, 6255 struct buffer_head *bh, 6256 struct ocfs2_xattr_header *xh, 6257 int offset, 6258 struct ocfs2_xattr_value_root **xv, 6259 struct buffer_head **ret_bh, 6260 void *para) 6261 { 6262 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset]; 6263 6264 *xv = (struct ocfs2_xattr_value_root *)((void *)xh + 6265 le16_to_cpu(xe->xe_name_offset) + 6266 OCFS2_XATTR_SIZE(xe->xe_name_len)); 6267 6268 if (ret_bh) 6269 *ret_bh = bh; 6270 6271 return 0; 6272 } 6273 6274 /* 6275 * Lock the meta_ac and caculate how much credits we need for reflink xattrs. 6276 * It is only used for inline xattr and xattr block. 6277 */ 6278 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb, 6279 struct ocfs2_xattr_header *xh, 6280 struct buffer_head *ref_root_bh, 6281 int *credits, 6282 struct ocfs2_alloc_context **meta_ac) 6283 { 6284 int ret, meta_add = 0, num_recs = 0; 6285 struct ocfs2_refcount_block *rb = 6286 (struct ocfs2_refcount_block *)ref_root_bh->b_data; 6287 6288 *credits = 0; 6289 6290 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh, 6291 &meta_add, credits, &num_recs, 6292 ocfs2_get_xattr_value_root, 6293 NULL); 6294 if (ret) { 6295 mlog_errno(ret); 6296 goto out; 6297 } 6298 6299 /* 6300 * We need to add/modify num_recs in refcount tree, so just calculate 6301 * an approximate number we need for refcount tree change. 6302 * Sometimes we need to split the tree, and after split, half recs 6303 * will be moved to the new block, and a new block can only provide 6304 * half number of recs. So we multiple new blocks by 2. 6305 */ 6306 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2; 6307 meta_add += num_recs; 6308 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS; 6309 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL) 6310 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) * 6311 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1; 6312 else 6313 *credits += 1; 6314 6315 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac); 6316 if (ret) 6317 mlog_errno(ret); 6318 6319 out: 6320 return ret; 6321 } 6322 6323 /* 6324 * Given a xattr header, reflink all the xattrs in this container. 6325 * It can be used for inode, block and bucket. 6326 * 6327 * NOTE: 6328 * Before we call this function, the caller has memcpy the xattr in 6329 * old_xh to the new_xh. 6330 * 6331 * If args.xattr_reflinked is set, call it to decide whether the xe should 6332 * be reflinked or not. If not, remove it from the new xattr header. 6333 */ 6334 static int ocfs2_reflink_xattr_header(handle_t *handle, 6335 struct ocfs2_xattr_reflink *args, 6336 struct buffer_head *old_bh, 6337 struct ocfs2_xattr_header *xh, 6338 struct buffer_head *new_bh, 6339 struct ocfs2_xattr_header *new_xh, 6340 struct ocfs2_xattr_value_buf *vb, 6341 struct ocfs2_alloc_context *meta_ac, 6342 get_xattr_value_root *func, 6343 void *para) 6344 { 6345 int ret = 0, i, j; 6346 struct super_block *sb = args->old_inode->i_sb; 6347 struct buffer_head *value_bh; 6348 struct ocfs2_xattr_entry *xe, *last; 6349 struct ocfs2_xattr_value_root *xv, *new_xv; 6350 struct ocfs2_extent_tree data_et; 6351 u32 clusters, cpos, p_cluster, num_clusters; 6352 unsigned int ext_flags = 0; 6353 6354 trace_ocfs2_reflink_xattr_header((unsigned long long)old_bh->b_blocknr, 6355 le16_to_cpu(xh->xh_count)); 6356 6357 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)]; 6358 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) { 6359 xe = &xh->xh_entries[i]; 6360 6361 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) { 6362 xe = &new_xh->xh_entries[j]; 6363 6364 le16_add_cpu(&new_xh->xh_count, -1); 6365 if (new_xh->xh_count) { 6366 memmove(xe, xe + 1, 6367 (void *)last - (void *)xe); 6368 memset(last, 0, 6369 sizeof(struct ocfs2_xattr_entry)); 6370 } 6371 6372 /* 6373 * We don't want j to increase in the next round since 6374 * it is already moved ahead. 6375 */ 6376 j--; 6377 continue; 6378 } 6379 6380 if (ocfs2_xattr_is_local(xe)) 6381 continue; 6382 6383 ret = func(sb, old_bh, xh, i, &xv, NULL, para); 6384 if (ret) { 6385 mlog_errno(ret); 6386 break; 6387 } 6388 6389 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para); 6390 if (ret) { 6391 mlog_errno(ret); 6392 break; 6393 } 6394 6395 /* 6396 * For the xattr which has l_tree_depth = 0, all the extent 6397 * recs have already be copied to the new xh with the 6398 * propriate OCFS2_EXT_REFCOUNTED flag we just need to 6399 * increase the refount count int the refcount tree. 6400 * 6401 * For the xattr which has l_tree_depth > 0, we need 6402 * to initialize it to the empty default value root, 6403 * and then insert the extents one by one. 6404 */ 6405 if (xv->xr_list.l_tree_depth) { 6406 memcpy(new_xv, &def_xv, OCFS2_XATTR_ROOT_SIZE); 6407 vb->vb_xv = new_xv; 6408 vb->vb_bh = value_bh; 6409 ocfs2_init_xattr_value_extent_tree(&data_et, 6410 INODE_CACHE(args->new_inode), vb); 6411 } 6412 6413 clusters = le32_to_cpu(xv->xr_clusters); 6414 cpos = 0; 6415 while (cpos < clusters) { 6416 ret = ocfs2_xattr_get_clusters(args->old_inode, 6417 cpos, 6418 &p_cluster, 6419 &num_clusters, 6420 &xv->xr_list, 6421 &ext_flags); 6422 if (ret) { 6423 mlog_errno(ret); 6424 goto out; 6425 } 6426 6427 BUG_ON(!p_cluster); 6428 6429 if (xv->xr_list.l_tree_depth) { 6430 ret = ocfs2_insert_extent(handle, 6431 &data_et, cpos, 6432 ocfs2_clusters_to_blocks( 6433 args->old_inode->i_sb, 6434 p_cluster), 6435 num_clusters, ext_flags, 6436 meta_ac); 6437 if (ret) { 6438 mlog_errno(ret); 6439 goto out; 6440 } 6441 } 6442 6443 ret = ocfs2_increase_refcount(handle, args->ref_ci, 6444 args->ref_root_bh, 6445 p_cluster, num_clusters, 6446 meta_ac, args->dealloc); 6447 if (ret) { 6448 mlog_errno(ret); 6449 goto out; 6450 } 6451 6452 cpos += num_clusters; 6453 } 6454 } 6455 6456 out: 6457 return ret; 6458 } 6459 6460 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args) 6461 { 6462 int ret = 0, credits = 0; 6463 handle_t *handle; 6464 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb); 6465 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data; 6466 int inline_size = le16_to_cpu(di->i_xattr_inline_size); 6467 int header_off = osb->sb->s_blocksize - inline_size; 6468 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *) 6469 (args->old_bh->b_data + header_off); 6470 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *) 6471 (args->new_bh->b_data + header_off); 6472 struct ocfs2_alloc_context *meta_ac = NULL; 6473 struct ocfs2_inode_info *new_oi; 6474 struct ocfs2_dinode *new_di; 6475 struct ocfs2_xattr_value_buf vb = { 6476 .vb_bh = args->new_bh, 6477 .vb_access = ocfs2_journal_access_di, 6478 }; 6479 6480 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh, 6481 &credits, &meta_ac); 6482 if (ret) { 6483 mlog_errno(ret); 6484 goto out; 6485 } 6486 6487 handle = ocfs2_start_trans(osb, credits); 6488 if (IS_ERR(handle)) { 6489 ret = PTR_ERR(handle); 6490 mlog_errno(ret); 6491 goto out; 6492 } 6493 6494 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode), 6495 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE); 6496 if (ret) { 6497 mlog_errno(ret); 6498 goto out_commit; 6499 } 6500 6501 memcpy(args->new_bh->b_data + header_off, 6502 args->old_bh->b_data + header_off, inline_size); 6503 6504 new_di = (struct ocfs2_dinode *)args->new_bh->b_data; 6505 new_di->i_xattr_inline_size = cpu_to_le16(inline_size); 6506 6507 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh, 6508 args->new_bh, new_xh, &vb, meta_ac, 6509 ocfs2_get_xattr_value_root, NULL); 6510 if (ret) { 6511 mlog_errno(ret); 6512 goto out_commit; 6513 } 6514 6515 new_oi = OCFS2_I(args->new_inode); 6516 /* 6517 * Adjust extent record count to reserve space for extended attribute. 6518 * Inline data count had been adjusted in ocfs2_duplicate_inline_data(). 6519 */ 6520 if (!(new_oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) && 6521 !(ocfs2_inode_is_fast_symlink(args->new_inode))) { 6522 struct ocfs2_extent_list *el = &new_di->id2.i_list; 6523 le16_add_cpu(&el->l_count, -(inline_size / 6524 sizeof(struct ocfs2_extent_rec))); 6525 } 6526 spin_lock(&new_oi->ip_lock); 6527 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL; 6528 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features); 6529 spin_unlock(&new_oi->ip_lock); 6530 6531 ocfs2_journal_dirty(handle, args->new_bh); 6532 6533 out_commit: 6534 ocfs2_commit_trans(osb, handle); 6535 6536 out: 6537 if (meta_ac) 6538 ocfs2_free_alloc_context(meta_ac); 6539 return ret; 6540 } 6541 6542 static int ocfs2_create_empty_xattr_block(struct inode *inode, 6543 struct buffer_head *fe_bh, 6544 struct buffer_head **ret_bh, 6545 int indexed) 6546 { 6547 int ret; 6548 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 6549 struct ocfs2_xattr_set_ctxt ctxt; 6550 6551 memset(&ctxt, 0, sizeof(ctxt)); 6552 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &ctxt.meta_ac); 6553 if (ret < 0) { 6554 mlog_errno(ret); 6555 return ret; 6556 } 6557 6558 ctxt.handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS); 6559 if (IS_ERR(ctxt.handle)) { 6560 ret = PTR_ERR(ctxt.handle); 6561 mlog_errno(ret); 6562 goto out; 6563 } 6564 6565 trace_ocfs2_create_empty_xattr_block( 6566 (unsigned long long)fe_bh->b_blocknr, indexed); 6567 ret = ocfs2_create_xattr_block(inode, fe_bh, &ctxt, indexed, 6568 ret_bh); 6569 if (ret) 6570 mlog_errno(ret); 6571 6572 ocfs2_commit_trans(osb, ctxt.handle); 6573 out: 6574 ocfs2_free_alloc_context(ctxt.meta_ac); 6575 return ret; 6576 } 6577 6578 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args, 6579 struct buffer_head *blk_bh, 6580 struct buffer_head *new_blk_bh) 6581 { 6582 int ret = 0, credits = 0; 6583 handle_t *handle; 6584 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode); 6585 struct ocfs2_dinode *new_di; 6586 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb); 6587 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); 6588 struct ocfs2_xattr_block *xb = 6589 (struct ocfs2_xattr_block *)blk_bh->b_data; 6590 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header; 6591 struct ocfs2_xattr_block *new_xb = 6592 (struct ocfs2_xattr_block *)new_blk_bh->b_data; 6593 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header; 6594 struct ocfs2_alloc_context *meta_ac; 6595 struct ocfs2_xattr_value_buf vb = { 6596 .vb_bh = new_blk_bh, 6597 .vb_access = ocfs2_journal_access_xb, 6598 }; 6599 6600 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh, 6601 &credits, &meta_ac); 6602 if (ret) { 6603 mlog_errno(ret); 6604 return ret; 6605 } 6606 6607 /* One more credits in case we need to add xattr flags in new inode. */ 6608 handle = ocfs2_start_trans(osb, credits + 1); 6609 if (IS_ERR(handle)) { 6610 ret = PTR_ERR(handle); 6611 mlog_errno(ret); 6612 goto out; 6613 } 6614 6615 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) { 6616 ret = ocfs2_journal_access_di(handle, 6617 INODE_CACHE(args->new_inode), 6618 args->new_bh, 6619 OCFS2_JOURNAL_ACCESS_WRITE); 6620 if (ret) { 6621 mlog_errno(ret); 6622 goto out_commit; 6623 } 6624 } 6625 6626 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode), 6627 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE); 6628 if (ret) { 6629 mlog_errno(ret); 6630 goto out_commit; 6631 } 6632 6633 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off, 6634 osb->sb->s_blocksize - header_off); 6635 6636 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh, 6637 new_blk_bh, new_xh, &vb, meta_ac, 6638 ocfs2_get_xattr_value_root, NULL); 6639 if (ret) { 6640 mlog_errno(ret); 6641 goto out_commit; 6642 } 6643 6644 ocfs2_journal_dirty(handle, new_blk_bh); 6645 6646 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) { 6647 new_di = (struct ocfs2_dinode *)args->new_bh->b_data; 6648 spin_lock(&new_oi->ip_lock); 6649 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL; 6650 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features); 6651 spin_unlock(&new_oi->ip_lock); 6652 6653 ocfs2_journal_dirty(handle, args->new_bh); 6654 } 6655 6656 out_commit: 6657 ocfs2_commit_trans(osb, handle); 6658 6659 out: 6660 ocfs2_free_alloc_context(meta_ac); 6661 return ret; 6662 } 6663 6664 struct ocfs2_reflink_xattr_tree_args { 6665 struct ocfs2_xattr_reflink *reflink; 6666 struct buffer_head *old_blk_bh; 6667 struct buffer_head *new_blk_bh; 6668 struct ocfs2_xattr_bucket *old_bucket; 6669 struct ocfs2_xattr_bucket *new_bucket; 6670 }; 6671 6672 /* 6673 * NOTE: 6674 * We have to handle the case that both old bucket and new bucket 6675 * will call this function to get the right ret_bh. 6676 * So The caller must give us the right bh. 6677 */ 6678 static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb, 6679 struct buffer_head *bh, 6680 struct ocfs2_xattr_header *xh, 6681 int offset, 6682 struct ocfs2_xattr_value_root **xv, 6683 struct buffer_head **ret_bh, 6684 void *para) 6685 { 6686 struct ocfs2_reflink_xattr_tree_args *args = 6687 (struct ocfs2_reflink_xattr_tree_args *)para; 6688 struct ocfs2_xattr_bucket *bucket; 6689 6690 if (bh == args->old_bucket->bu_bhs[0]) 6691 bucket = args->old_bucket; 6692 else 6693 bucket = args->new_bucket; 6694 6695 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset, 6696 xv, ret_bh); 6697 } 6698 6699 struct ocfs2_value_tree_metas { 6700 int num_metas; 6701 int credits; 6702 int num_recs; 6703 }; 6704 6705 static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb, 6706 struct buffer_head *bh, 6707 struct ocfs2_xattr_header *xh, 6708 int offset, 6709 struct ocfs2_xattr_value_root **xv, 6710 struct buffer_head **ret_bh, 6711 void *para) 6712 { 6713 struct ocfs2_xattr_bucket *bucket = 6714 (struct ocfs2_xattr_bucket *)para; 6715 6716 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset, 6717 xv, ret_bh); 6718 } 6719 6720 static int ocfs2_calc_value_tree_metas(struct inode *inode, 6721 struct ocfs2_xattr_bucket *bucket, 6722 void *para) 6723 { 6724 struct ocfs2_value_tree_metas *metas = 6725 (struct ocfs2_value_tree_metas *)para; 6726 struct ocfs2_xattr_header *xh = 6727 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data; 6728 6729 /* Add the credits for this bucket first. */ 6730 metas->credits += bucket->bu_blocks; 6731 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0], 6732 xh, &metas->num_metas, 6733 &metas->credits, &metas->num_recs, 6734 ocfs2_value_tree_metas_in_bucket, 6735 bucket); 6736 } 6737 6738 /* 6739 * Given a xattr extent rec starting from blkno and having len clusters, 6740 * iterate all the buckets calculate how much metadata we need for reflinking 6741 * all the ocfs2_xattr_value_root and lock the allocators accordingly. 6742 */ 6743 static int ocfs2_lock_reflink_xattr_rec_allocators( 6744 struct ocfs2_reflink_xattr_tree_args *args, 6745 struct ocfs2_extent_tree *xt_et, 6746 u64 blkno, u32 len, int *credits, 6747 struct ocfs2_alloc_context **meta_ac, 6748 struct ocfs2_alloc_context **data_ac) 6749 { 6750 int ret, num_free_extents; 6751 struct ocfs2_value_tree_metas metas; 6752 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb); 6753 struct ocfs2_refcount_block *rb; 6754 6755 memset(&metas, 0, sizeof(metas)); 6756 6757 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len, 6758 ocfs2_calc_value_tree_metas, &metas); 6759 if (ret) { 6760 mlog_errno(ret); 6761 goto out; 6762 } 6763 6764 *credits = metas.credits; 6765 6766 /* 6767 * Calculate we need for refcount tree change. 6768 * 6769 * We need to add/modify num_recs in refcount tree, so just calculate 6770 * an approximate number we need for refcount tree change. 6771 * Sometimes we need to split the tree, and after split, half recs 6772 * will be moved to the new block, and a new block can only provide 6773 * half number of recs. So we multiple new blocks by 2. 6774 * In the end, we have to add credits for modifying the already 6775 * existed refcount block. 6776 */ 6777 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data; 6778 metas.num_recs = 6779 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) / 6780 ocfs2_refcount_recs_per_rb(osb->sb) * 2; 6781 metas.num_metas += metas.num_recs; 6782 *credits += metas.num_recs + 6783 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS; 6784 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL) 6785 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) * 6786 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1; 6787 else 6788 *credits += 1; 6789 6790 /* count in the xattr tree change. */ 6791 num_free_extents = ocfs2_num_free_extents(xt_et); 6792 if (num_free_extents < 0) { 6793 ret = num_free_extents; 6794 mlog_errno(ret); 6795 goto out; 6796 } 6797 6798 if (num_free_extents < len) 6799 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el); 6800 6801 *credits += ocfs2_calc_extend_credits(osb->sb, 6802 xt_et->et_root_el); 6803 6804 if (metas.num_metas) { 6805 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas, 6806 meta_ac); 6807 if (ret) { 6808 mlog_errno(ret); 6809 goto out; 6810 } 6811 } 6812 6813 if (len) { 6814 ret = ocfs2_reserve_clusters(osb, len, data_ac); 6815 if (ret) 6816 mlog_errno(ret); 6817 } 6818 out: 6819 if (ret) { 6820 if (*meta_ac) { 6821 ocfs2_free_alloc_context(*meta_ac); 6822 *meta_ac = NULL; 6823 } 6824 } 6825 6826 return ret; 6827 } 6828 6829 static int ocfs2_reflink_xattr_bucket(handle_t *handle, 6830 u64 blkno, u64 new_blkno, u32 clusters, 6831 u32 *cpos, int num_buckets, 6832 struct ocfs2_alloc_context *meta_ac, 6833 struct ocfs2_alloc_context *data_ac, 6834 struct ocfs2_reflink_xattr_tree_args *args) 6835 { 6836 int i, j, ret = 0; 6837 struct super_block *sb = args->reflink->old_inode->i_sb; 6838 int bpb = args->old_bucket->bu_blocks; 6839 struct ocfs2_xattr_value_buf vb = { 6840 .vb_access = ocfs2_journal_access, 6841 }; 6842 6843 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) { 6844 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno); 6845 if (ret) { 6846 mlog_errno(ret); 6847 break; 6848 } 6849 6850 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno, 1); 6851 if (ret) { 6852 mlog_errno(ret); 6853 break; 6854 } 6855 6856 ret = ocfs2_xattr_bucket_journal_access(handle, 6857 args->new_bucket, 6858 OCFS2_JOURNAL_ACCESS_CREATE); 6859 if (ret) { 6860 mlog_errno(ret); 6861 break; 6862 } 6863 6864 for (j = 0; j < bpb; j++) 6865 memcpy(bucket_block(args->new_bucket, j), 6866 bucket_block(args->old_bucket, j), 6867 sb->s_blocksize); 6868 6869 /* 6870 * Record the start cpos so that we can use it to initialize 6871 * our xattr tree we also set the xh_num_bucket for the new 6872 * bucket. 6873 */ 6874 if (i == 0) { 6875 *cpos = le32_to_cpu(bucket_xh(args->new_bucket)-> 6876 xh_entries[0].xe_name_hash); 6877 bucket_xh(args->new_bucket)->xh_num_buckets = 6878 cpu_to_le16(num_buckets); 6879 } 6880 6881 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket); 6882 6883 ret = ocfs2_reflink_xattr_header(handle, args->reflink, 6884 args->old_bucket->bu_bhs[0], 6885 bucket_xh(args->old_bucket), 6886 args->new_bucket->bu_bhs[0], 6887 bucket_xh(args->new_bucket), 6888 &vb, meta_ac, 6889 ocfs2_get_reflink_xattr_value_root, 6890 args); 6891 if (ret) { 6892 mlog_errno(ret); 6893 break; 6894 } 6895 6896 /* 6897 * Re-access and dirty the bucket to calculate metaecc. 6898 * Because we may extend the transaction in reflink_xattr_header 6899 * which will let the already accessed block gone. 6900 */ 6901 ret = ocfs2_xattr_bucket_journal_access(handle, 6902 args->new_bucket, 6903 OCFS2_JOURNAL_ACCESS_WRITE); 6904 if (ret) { 6905 mlog_errno(ret); 6906 break; 6907 } 6908 6909 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket); 6910 6911 ocfs2_xattr_bucket_relse(args->old_bucket); 6912 ocfs2_xattr_bucket_relse(args->new_bucket); 6913 } 6914 6915 ocfs2_xattr_bucket_relse(args->old_bucket); 6916 ocfs2_xattr_bucket_relse(args->new_bucket); 6917 return ret; 6918 } 6919 6920 static int ocfs2_reflink_xattr_buckets(handle_t *handle, 6921 struct inode *inode, 6922 struct ocfs2_reflink_xattr_tree_args *args, 6923 struct ocfs2_extent_tree *et, 6924 struct ocfs2_alloc_context *meta_ac, 6925 struct ocfs2_alloc_context *data_ac, 6926 u64 blkno, u32 cpos, u32 len) 6927 { 6928 int ret, first_inserted = 0; 6929 u32 p_cluster, num_clusters, reflink_cpos = 0; 6930 u64 new_blkno; 6931 unsigned int num_buckets, reflink_buckets; 6932 unsigned int bpc = 6933 ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)); 6934 6935 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno); 6936 if (ret) { 6937 mlog_errno(ret); 6938 goto out; 6939 } 6940 num_buckets = le16_to_cpu(bucket_xh(args->old_bucket)->xh_num_buckets); 6941 ocfs2_xattr_bucket_relse(args->old_bucket); 6942 6943 while (len && num_buckets) { 6944 ret = ocfs2_claim_clusters(handle, data_ac, 6945 1, &p_cluster, &num_clusters); 6946 if (ret) { 6947 mlog_errno(ret); 6948 goto out; 6949 } 6950 6951 new_blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster); 6952 reflink_buckets = min(num_buckets, bpc * num_clusters); 6953 6954 ret = ocfs2_reflink_xattr_bucket(handle, blkno, 6955 new_blkno, num_clusters, 6956 &reflink_cpos, reflink_buckets, 6957 meta_ac, data_ac, args); 6958 if (ret) { 6959 mlog_errno(ret); 6960 goto out; 6961 } 6962 6963 /* 6964 * For the 1st allocated cluster, we make it use the same cpos 6965 * so that the xattr tree looks the same as the original one 6966 * in the most case. 6967 */ 6968 if (!first_inserted) { 6969 reflink_cpos = cpos; 6970 first_inserted = 1; 6971 } 6972 ret = ocfs2_insert_extent(handle, et, reflink_cpos, new_blkno, 6973 num_clusters, 0, meta_ac); 6974 if (ret) 6975 mlog_errno(ret); 6976 6977 trace_ocfs2_reflink_xattr_buckets((unsigned long long)new_blkno, 6978 num_clusters, reflink_cpos); 6979 6980 len -= num_clusters; 6981 blkno += ocfs2_clusters_to_blocks(inode->i_sb, num_clusters); 6982 num_buckets -= reflink_buckets; 6983 } 6984 out: 6985 return ret; 6986 } 6987 6988 /* 6989 * Create the same xattr extent record in the new inode's xattr tree. 6990 */ 6991 static int ocfs2_reflink_xattr_rec(struct inode *inode, 6992 struct buffer_head *root_bh, 6993 u64 blkno, 6994 u32 cpos, 6995 u32 len, 6996 void *para) 6997 { 6998 int ret, credits = 0; 6999 handle_t *handle; 7000 struct ocfs2_reflink_xattr_tree_args *args = 7001 (struct ocfs2_reflink_xattr_tree_args *)para; 7002 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 7003 struct ocfs2_alloc_context *meta_ac = NULL; 7004 struct ocfs2_alloc_context *data_ac = NULL; 7005 struct ocfs2_extent_tree et; 7006 7007 trace_ocfs2_reflink_xattr_rec((unsigned long long)blkno, len); 7008 7009 ocfs2_init_xattr_tree_extent_tree(&et, 7010 INODE_CACHE(args->reflink->new_inode), 7011 args->new_blk_bh); 7012 7013 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno, 7014 len, &credits, 7015 &meta_ac, &data_ac); 7016 if (ret) { 7017 mlog_errno(ret); 7018 goto out; 7019 } 7020 7021 handle = ocfs2_start_trans(osb, credits); 7022 if (IS_ERR(handle)) { 7023 ret = PTR_ERR(handle); 7024 mlog_errno(ret); 7025 goto out; 7026 } 7027 7028 ret = ocfs2_reflink_xattr_buckets(handle, inode, args, &et, 7029 meta_ac, data_ac, 7030 blkno, cpos, len); 7031 if (ret) 7032 mlog_errno(ret); 7033 7034 ocfs2_commit_trans(osb, handle); 7035 7036 out: 7037 if (meta_ac) 7038 ocfs2_free_alloc_context(meta_ac); 7039 if (data_ac) 7040 ocfs2_free_alloc_context(data_ac); 7041 return ret; 7042 } 7043 7044 /* 7045 * Create reflinked xattr buckets. 7046 * We will add bucket one by one, and refcount all the xattrs in the bucket 7047 * if they are stored outside. 7048 */ 7049 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args, 7050 struct buffer_head *blk_bh, 7051 struct buffer_head *new_blk_bh) 7052 { 7053 int ret; 7054 struct ocfs2_reflink_xattr_tree_args para; 7055 7056 memset(¶, 0, sizeof(para)); 7057 para.reflink = args; 7058 para.old_blk_bh = blk_bh; 7059 para.new_blk_bh = new_blk_bh; 7060 7061 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode); 7062 if (!para.old_bucket) { 7063 mlog_errno(-ENOMEM); 7064 return -ENOMEM; 7065 } 7066 7067 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode); 7068 if (!para.new_bucket) { 7069 ret = -ENOMEM; 7070 mlog_errno(ret); 7071 goto out; 7072 } 7073 7074 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh, 7075 ocfs2_reflink_xattr_rec, 7076 ¶); 7077 if (ret) 7078 mlog_errno(ret); 7079 7080 out: 7081 ocfs2_xattr_bucket_free(para.old_bucket); 7082 ocfs2_xattr_bucket_free(para.new_bucket); 7083 return ret; 7084 } 7085 7086 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args, 7087 struct buffer_head *blk_bh) 7088 { 7089 int ret, indexed = 0; 7090 struct buffer_head *new_blk_bh = NULL; 7091 struct ocfs2_xattr_block *xb = 7092 (struct ocfs2_xattr_block *)blk_bh->b_data; 7093 7094 7095 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) 7096 indexed = 1; 7097 7098 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh, 7099 &new_blk_bh, indexed); 7100 if (ret) { 7101 mlog_errno(ret); 7102 goto out; 7103 } 7104 7105 if (!indexed) 7106 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh); 7107 else 7108 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh); 7109 if (ret) 7110 mlog_errno(ret); 7111 7112 out: 7113 brelse(new_blk_bh); 7114 return ret; 7115 } 7116 7117 static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe) 7118 { 7119 int type = ocfs2_xattr_get_type(xe); 7120 7121 return type != OCFS2_XATTR_INDEX_SECURITY && 7122 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS && 7123 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT; 7124 } 7125 7126 int ocfs2_reflink_xattrs(struct inode *old_inode, 7127 struct buffer_head *old_bh, 7128 struct inode *new_inode, 7129 struct buffer_head *new_bh, 7130 bool preserve_security) 7131 { 7132 int ret; 7133 struct ocfs2_xattr_reflink args; 7134 struct ocfs2_inode_info *oi = OCFS2_I(old_inode); 7135 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data; 7136 struct buffer_head *blk_bh = NULL; 7137 struct ocfs2_cached_dealloc_ctxt dealloc; 7138 struct ocfs2_refcount_tree *ref_tree; 7139 struct buffer_head *ref_root_bh = NULL; 7140 7141 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb), 7142 le64_to_cpu(di->i_refcount_loc), 7143 1, &ref_tree, &ref_root_bh); 7144 if (ret) { 7145 mlog_errno(ret); 7146 goto out; 7147 } 7148 7149 ocfs2_init_dealloc_ctxt(&dealloc); 7150 7151 args.old_inode = old_inode; 7152 args.new_inode = new_inode; 7153 args.old_bh = old_bh; 7154 args.new_bh = new_bh; 7155 args.ref_ci = &ref_tree->rf_ci; 7156 args.ref_root_bh = ref_root_bh; 7157 args.dealloc = &dealloc; 7158 if (preserve_security) 7159 args.xattr_reflinked = NULL; 7160 else 7161 args.xattr_reflinked = ocfs2_reflink_xattr_no_security; 7162 7163 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) { 7164 ret = ocfs2_reflink_xattr_inline(&args); 7165 if (ret) { 7166 mlog_errno(ret); 7167 goto out_unlock; 7168 } 7169 } 7170 7171 if (!di->i_xattr_loc) 7172 goto out_unlock; 7173 7174 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc), 7175 &blk_bh); 7176 if (ret < 0) { 7177 mlog_errno(ret); 7178 goto out_unlock; 7179 } 7180 7181 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh); 7182 if (ret) 7183 mlog_errno(ret); 7184 7185 brelse(blk_bh); 7186 7187 out_unlock: 7188 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb), 7189 ref_tree, 1); 7190 brelse(ref_root_bh); 7191 7192 if (ocfs2_dealloc_has_cluster(&dealloc)) { 7193 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1); 7194 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc); 7195 } 7196 7197 out: 7198 return ret; 7199 } 7200 7201 /* 7202 * Initialize security and acl for a already created inode. 7203 * Used for reflink a non-preserve-security file. 7204 * 7205 * It uses common api like ocfs2_xattr_set, so the caller 7206 * must not hold any lock expect i_rwsem. 7207 */ 7208 int ocfs2_init_security_and_acl(struct inode *dir, 7209 struct inode *inode, 7210 const struct qstr *qstr) 7211 { 7212 int ret = 0; 7213 struct buffer_head *dir_bh = NULL; 7214 7215 ret = ocfs2_init_security_get(inode, dir, qstr, NULL); 7216 if (ret) { 7217 mlog_errno(ret); 7218 goto leave; 7219 } 7220 7221 ret = ocfs2_inode_lock(dir, &dir_bh, 0); 7222 if (ret) { 7223 mlog_errno(ret); 7224 goto leave; 7225 } 7226 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL); 7227 if (ret) 7228 mlog_errno(ret); 7229 7230 ocfs2_inode_unlock(dir, 0); 7231 brelse(dir_bh); 7232 leave: 7233 return ret; 7234 } 7235 7236 /* 7237 * 'security' attributes support 7238 */ 7239 static int ocfs2_xattr_security_get(const struct xattr_handler *handler, 7240 struct dentry *unused, struct inode *inode, 7241 const char *name, void *buffer, size_t size) 7242 { 7243 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, 7244 name, buffer, size); 7245 } 7246 7247 static int ocfs2_xattr_security_set(const struct xattr_handler *handler, 7248 struct mnt_idmap *idmap, 7249 struct dentry *unused, struct inode *inode, 7250 const char *name, const void *value, 7251 size_t size, int flags) 7252 { 7253 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, 7254 name, value, size, flags); 7255 } 7256 7257 static int ocfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array, 7258 void *fs_info) 7259 { 7260 const struct xattr *xattr; 7261 int err = 0; 7262 7263 for (xattr = xattr_array; xattr->name != NULL; xattr++) { 7264 err = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, 7265 xattr->name, xattr->value, 7266 xattr->value_len, XATTR_CREATE); 7267 if (err) 7268 break; 7269 } 7270 return err; 7271 } 7272 7273 int ocfs2_init_security_get(struct inode *inode, 7274 struct inode *dir, 7275 const struct qstr *qstr, 7276 struct ocfs2_security_xattr_info *si) 7277 { 7278 /* check whether ocfs2 support feature xattr */ 7279 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb))) 7280 return -EOPNOTSUPP; 7281 if (si) 7282 return security_old_inode_init_security(inode, dir, qstr, 7283 &si->name, &si->value, 7284 &si->value_len); 7285 7286 return security_inode_init_security(inode, dir, qstr, 7287 &ocfs2_initxattrs, NULL); 7288 } 7289 7290 int ocfs2_init_security_set(handle_t *handle, 7291 struct inode *inode, 7292 struct buffer_head *di_bh, 7293 struct ocfs2_security_xattr_info *si, 7294 struct ocfs2_alloc_context *xattr_ac, 7295 struct ocfs2_alloc_context *data_ac) 7296 { 7297 return ocfs2_xattr_set_handle(handle, inode, di_bh, 7298 OCFS2_XATTR_INDEX_SECURITY, 7299 si->name, si->value, si->value_len, 0, 7300 xattr_ac, data_ac); 7301 } 7302 7303 const struct xattr_handler ocfs2_xattr_security_handler = { 7304 .prefix = XATTR_SECURITY_PREFIX, 7305 .get = ocfs2_xattr_security_get, 7306 .set = ocfs2_xattr_security_set, 7307 }; 7308 7309 /* 7310 * 'trusted' attributes support 7311 */ 7312 static int ocfs2_xattr_trusted_get(const struct xattr_handler *handler, 7313 struct dentry *unused, struct inode *inode, 7314 const char *name, void *buffer, size_t size) 7315 { 7316 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, 7317 name, buffer, size); 7318 } 7319 7320 static int ocfs2_xattr_trusted_set(const struct xattr_handler *handler, 7321 struct mnt_idmap *idmap, 7322 struct dentry *unused, struct inode *inode, 7323 const char *name, const void *value, 7324 size_t size, int flags) 7325 { 7326 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, 7327 name, value, size, flags); 7328 } 7329 7330 const struct xattr_handler ocfs2_xattr_trusted_handler = { 7331 .prefix = XATTR_TRUSTED_PREFIX, 7332 .get = ocfs2_xattr_trusted_get, 7333 .set = ocfs2_xattr_trusted_set, 7334 }; 7335 7336 /* 7337 * 'user' attributes support 7338 */ 7339 static int ocfs2_xattr_user_get(const struct xattr_handler *handler, 7340 struct dentry *unused, struct inode *inode, 7341 const char *name, void *buffer, size_t size) 7342 { 7343 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 7344 7345 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) 7346 return -EOPNOTSUPP; 7347 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name, 7348 buffer, size); 7349 } 7350 7351 static int ocfs2_xattr_user_set(const struct xattr_handler *handler, 7352 struct mnt_idmap *idmap, 7353 struct dentry *unused, struct inode *inode, 7354 const char *name, const void *value, 7355 size_t size, int flags) 7356 { 7357 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 7358 7359 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) 7360 return -EOPNOTSUPP; 7361 7362 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, 7363 name, value, size, flags); 7364 } 7365 7366 const struct xattr_handler ocfs2_xattr_user_handler = { 7367 .prefix = XATTR_USER_PREFIX, 7368 .get = ocfs2_xattr_user_get, 7369 .set = ocfs2_xattr_user_set, 7370 }; 7371