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