1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * dir.c 4 * 5 * Creates, reads, walks and deletes directory-nodes 6 * 7 * Copyright (C) 2002, 2004 Oracle. All rights reserved. 8 * 9 * Portions of this code from linux/fs/ext3/dir.c 10 * 11 * Copyright (C) 1992, 1993, 1994, 1995 12 * Remy Card (card@masi.ibp.fr) 13 * Laboratoire MASI - Institut Blaise pascal 14 * Universite Pierre et Marie Curie (Paris VI) 15 * 16 * from 17 * 18 * linux/fs/minix/dir.c 19 * 20 * Copyright (C) 1991, 1992 Linus Torvalds 21 */ 22 23 #include <linux/fs.h> 24 #include <linux/types.h> 25 #include <linux/slab.h> 26 #include <linux/highmem.h> 27 #include <linux/quotaops.h> 28 #include <linux/sort.h> 29 #include <linux/iversion.h> 30 31 #include <cluster/masklog.h> 32 33 #include "ocfs2.h" 34 35 #include "alloc.h" 36 #include "blockcheck.h" 37 #include "dir.h" 38 #include "dlmglue.h" 39 #include "extent_map.h" 40 #include "file.h" 41 #include "inode.h" 42 #include "journal.h" 43 #include "namei.h" 44 #include "suballoc.h" 45 #include "super.h" 46 #include "sysfile.h" 47 #include "uptodate.h" 48 #include "ocfs2_trace.h" 49 50 #include "buffer_head_io.h" 51 52 #define NAMEI_RA_CHUNKS 2 53 #define NAMEI_RA_BLOCKS 4 54 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS) 55 56 static int ocfs2_do_extend_dir(struct super_block *sb, 57 handle_t *handle, 58 struct inode *dir, 59 struct buffer_head *parent_fe_bh, 60 struct ocfs2_alloc_context *data_ac, 61 struct ocfs2_alloc_context *meta_ac, 62 struct buffer_head **new_bh); 63 static int ocfs2_dir_indexed(struct inode *inode); 64 65 /* 66 * These are distinct checks because future versions of the file system will 67 * want to have a trailing dirent structure independent of indexing. 68 */ 69 static int ocfs2_supports_dir_trailer(struct inode *dir) 70 { 71 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); 72 73 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) 74 return 0; 75 76 return ocfs2_meta_ecc(osb) || ocfs2_dir_indexed(dir); 77 } 78 79 /* 80 * "new' here refers to the point at which we're creating a new 81 * directory via "mkdir()", but also when we're expanding an inline 82 * directory. In either case, we don't yet have the indexing bit set 83 * on the directory, so the standard checks will fail in when metaecc 84 * is turned off. Only directory-initialization type functions should 85 * use this then. Everything else wants ocfs2_supports_dir_trailer() 86 */ 87 static int ocfs2_new_dir_wants_trailer(struct inode *dir) 88 { 89 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); 90 91 return ocfs2_meta_ecc(osb) || 92 ocfs2_supports_indexed_dirs(osb); 93 } 94 95 static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb) 96 { 97 return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer); 98 } 99 100 #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb)))) 101 102 /* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make 103 * them more consistent? */ 104 struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize, 105 void *data) 106 { 107 char *p = data; 108 109 p += blocksize - sizeof(struct ocfs2_dir_block_trailer); 110 return (struct ocfs2_dir_block_trailer *)p; 111 } 112 113 /* 114 * XXX: This is executed once on every dirent. We should consider optimizing 115 * it. 116 */ 117 static int ocfs2_skip_dir_trailer(struct inode *dir, 118 struct ocfs2_dir_entry *de, 119 unsigned long offset, 120 unsigned long blklen) 121 { 122 unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer); 123 124 if (!ocfs2_supports_dir_trailer(dir)) 125 return 0; 126 127 if (offset != toff) 128 return 0; 129 130 return 1; 131 } 132 133 static void ocfs2_init_dir_trailer(struct inode *inode, 134 struct buffer_head *bh, u16 rec_len) 135 { 136 struct ocfs2_dir_block_trailer *trailer; 137 138 trailer = ocfs2_trailer_from_bh(bh, inode->i_sb); 139 strscpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE); 140 trailer->db_compat_rec_len = 141 cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer)); 142 trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno); 143 trailer->db_blkno = cpu_to_le64(bh->b_blocknr); 144 trailer->db_free_rec_len = cpu_to_le16(rec_len); 145 } 146 /* 147 * Link an unindexed block with a dir trailer structure into the index free 148 * list. This function will modify dirdata_bh, but assumes you've already 149 * passed it to the journal. 150 */ 151 static int ocfs2_dx_dir_link_trailer(struct inode *dir, handle_t *handle, 152 struct buffer_head *dx_root_bh, 153 struct buffer_head *dirdata_bh) 154 { 155 int ret; 156 struct ocfs2_dx_root_block *dx_root; 157 struct ocfs2_dir_block_trailer *trailer; 158 159 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh, 160 OCFS2_JOURNAL_ACCESS_WRITE); 161 if (ret) { 162 mlog_errno(ret); 163 goto out; 164 } 165 trailer = ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb); 166 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; 167 168 trailer->db_free_next = dx_root->dr_free_blk; 169 dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr); 170 171 ocfs2_journal_dirty(handle, dx_root_bh); 172 173 out: 174 return ret; 175 } 176 177 static int ocfs2_free_list_at_root(struct ocfs2_dir_lookup_result *res) 178 { 179 return res->dl_prev_leaf_bh == NULL; 180 } 181 182 void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res) 183 { 184 brelse(res->dl_dx_root_bh); 185 brelse(res->dl_leaf_bh); 186 brelse(res->dl_dx_leaf_bh); 187 brelse(res->dl_prev_leaf_bh); 188 } 189 190 static int ocfs2_dir_indexed(struct inode *inode) 191 { 192 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INDEXED_DIR_FL) 193 return 1; 194 return 0; 195 } 196 197 static inline int ocfs2_dx_root_inline(struct ocfs2_dx_root_block *dx_root) 198 { 199 return dx_root->dr_flags & OCFS2_DX_FLAG_INLINE; 200 } 201 202 /* 203 * Hashing code adapted from ext3 204 */ 205 #define DELTA 0x9E3779B9 206 207 static void TEA_transform(__u32 buf[4], __u32 const in[]) 208 { 209 __u32 sum = 0; 210 __u32 b0 = buf[0], b1 = buf[1]; 211 __u32 a = in[0], b = in[1], c = in[2], d = in[3]; 212 int n = 16; 213 214 do { 215 sum += DELTA; 216 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b); 217 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d); 218 } while (--n); 219 220 buf[0] += b0; 221 buf[1] += b1; 222 } 223 224 static void str2hashbuf(const char *msg, int len, __u32 *buf, int num) 225 { 226 __u32 pad, val; 227 int i; 228 229 pad = (__u32)len | ((__u32)len << 8); 230 pad |= pad << 16; 231 232 val = pad; 233 if (len > num*4) 234 len = num * 4; 235 for (i = 0; i < len; i++) { 236 if ((i % 4) == 0) 237 val = pad; 238 val = msg[i] + (val << 8); 239 if ((i % 4) == 3) { 240 *buf++ = val; 241 val = pad; 242 num--; 243 } 244 } 245 if (--num >= 0) 246 *buf++ = val; 247 while (--num >= 0) 248 *buf++ = pad; 249 } 250 251 static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len, 252 struct ocfs2_dx_hinfo *hinfo) 253 { 254 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); 255 const char *p; 256 __u32 in[8], buf[4]; 257 258 /* 259 * XXX: Is this really necessary, if the index is never looked 260 * at by readdir? Is a hash value of '0' a bad idea? 261 */ 262 if ((len == 1 && !strncmp(".", name, 1)) || 263 (len == 2 && !strncmp("..", name, 2))) { 264 buf[0] = buf[1] = 0; 265 goto out; 266 } 267 268 #ifdef OCFS2_DEBUG_DX_DIRS 269 /* 270 * This makes it very easy to debug indexing problems. We 271 * should never allow this to be selected without hand editing 272 * this file though. 273 */ 274 buf[0] = buf[1] = len; 275 goto out; 276 #endif 277 278 memcpy(buf, osb->osb_dx_seed, sizeof(buf)); 279 280 p = name; 281 while (len > 0) { 282 str2hashbuf(p, len, in, 4); 283 TEA_transform(buf, in); 284 len -= 16; 285 p += 16; 286 } 287 288 out: 289 hinfo->major_hash = buf[0]; 290 hinfo->minor_hash = buf[1]; 291 } 292 293 /* 294 * bh passed here can be an inode block or a dir data block, depending 295 * on the inode inline data flag. 296 */ 297 static int ocfs2_check_dir_entry(struct inode *dir, 298 struct ocfs2_dir_entry *de, 299 struct buffer_head *bh, 300 char *buf, 301 unsigned int size, 302 unsigned long offset) 303 { 304 const char *error_msg = NULL; 305 unsigned long next_offset; 306 int rlen; 307 308 if (offset > size - OCFS2_DIR_REC_LEN(1)) { 309 /* Dirent is (maybe partially) beyond the buffer 310 * boundaries so touching 'de' members is unsafe. 311 */ 312 mlog(ML_ERROR, "directory entry (#%llu: offset=%lu) " 313 "too close to end or out-of-bounds", 314 (unsigned long long)OCFS2_I(dir)->ip_blkno, offset); 315 return 0; 316 } 317 318 rlen = le16_to_cpu(de->rec_len); 319 next_offset = ((char *) de - buf) + rlen; 320 321 if (unlikely(rlen < OCFS2_DIR_REC_LEN(1))) 322 error_msg = "rec_len is smaller than minimal"; 323 else if (unlikely(rlen % 4 != 0)) 324 error_msg = "rec_len % 4 != 0"; 325 else if (unlikely(rlen < OCFS2_DIR_REC_LEN(de->name_len))) 326 error_msg = "rec_len is too small for name_len"; 327 else if (unlikely(next_offset > size)) 328 error_msg = "directory entry overrun"; 329 else if (unlikely(next_offset > size - OCFS2_DIR_REC_LEN(1)) && 330 next_offset != size) 331 error_msg = "directory entry too close to end"; 332 333 if (unlikely(error_msg != NULL)) 334 mlog(ML_ERROR, "bad entry in directory #%llu: %s - " 335 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n", 336 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg, 337 offset, (unsigned long long)le64_to_cpu(de->inode), rlen, 338 de->name_len); 339 340 return error_msg == NULL ? 1 : 0; 341 } 342 343 static inline int ocfs2_match(int len, 344 const char * const name, 345 struct ocfs2_dir_entry *de) 346 { 347 if (len != de->name_len) 348 return 0; 349 if (!de->inode) 350 return 0; 351 return !memcmp(name, de->name, len); 352 } 353 354 /* 355 * Returns 0 if not found, -1 on failure, and 1 on success 356 */ 357 static inline int ocfs2_search_dirblock(struct buffer_head *bh, 358 struct inode *dir, 359 const char *name, int namelen, 360 unsigned long offset, 361 char *first_de, 362 unsigned int bytes, 363 struct ocfs2_dir_entry **res_dir) 364 { 365 struct ocfs2_dir_entry *de; 366 char *dlimit, *de_buf; 367 int de_len; 368 int ret = 0; 369 370 de_buf = first_de; 371 dlimit = de_buf + bytes; 372 373 while (de_buf < dlimit - OCFS2_DIR_MEMBER_LEN) { 374 /* this code is executed quadratically often */ 375 /* do minimal checking `by hand' */ 376 377 de = (struct ocfs2_dir_entry *) de_buf; 378 379 if (de->name + namelen <= dlimit && 380 ocfs2_match(namelen, name, de)) { 381 /* found a match - just to be sure, do a full check */ 382 if (!ocfs2_check_dir_entry(dir, de, bh, first_de, 383 bytes, offset)) { 384 ret = -1; 385 goto bail; 386 } 387 *res_dir = de; 388 ret = 1; 389 goto bail; 390 } 391 392 /* prevent looping on a bad block */ 393 de_len = le16_to_cpu(de->rec_len); 394 if (de_len <= 0) { 395 ret = -1; 396 goto bail; 397 } 398 399 de_buf += de_len; 400 offset += de_len; 401 } 402 403 bail: 404 trace_ocfs2_search_dirblock(ret); 405 return ret; 406 } 407 408 static struct buffer_head *ocfs2_find_entry_id(const char *name, 409 int namelen, 410 struct inode *dir, 411 struct ocfs2_dir_entry **res_dir) 412 { 413 int ret, found; 414 struct buffer_head *di_bh = NULL; 415 struct ocfs2_dinode *di; 416 struct ocfs2_inline_data *data; 417 418 ret = ocfs2_read_inode_block(dir, &di_bh); 419 if (ret) { 420 mlog_errno(ret); 421 goto out; 422 } 423 424 di = (struct ocfs2_dinode *)di_bh->b_data; 425 data = &di->id2.i_data; 426 427 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0, 428 data->id_data, i_size_read(dir), res_dir); 429 if (found == 1) 430 return di_bh; 431 432 brelse(di_bh); 433 out: 434 return NULL; 435 } 436 437 static int ocfs2_validate_dir_block(struct super_block *sb, 438 struct buffer_head *bh) 439 { 440 int rc; 441 struct ocfs2_dir_block_trailer *trailer = 442 ocfs2_trailer_from_bh(bh, sb); 443 444 445 /* 446 * We don't validate dirents here, that's handled 447 * in-place when the code walks them. 448 */ 449 trace_ocfs2_validate_dir_block((unsigned long long)bh->b_blocknr); 450 451 BUG_ON(!buffer_uptodate(bh)); 452 453 /* 454 * If the ecc fails, we return the error but otherwise 455 * leave the filesystem running. We know any error is 456 * local to this block. 457 * 458 * Note that we are safe to call this even if the directory 459 * doesn't have a trailer. Filesystems without metaecc will do 460 * nothing, and filesystems with it will have one. 461 */ 462 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check); 463 if (rc) 464 mlog(ML_ERROR, "Checksum failed for dinode %llu\n", 465 (unsigned long long)bh->b_blocknr); 466 467 return rc; 468 } 469 470 /* 471 * Validate a directory trailer. 472 * 473 * We check the trailer here rather than in ocfs2_validate_dir_block() 474 * because that function doesn't have the inode to test. 475 */ 476 static int ocfs2_check_dir_trailer(struct inode *dir, struct buffer_head *bh) 477 { 478 int rc = 0; 479 struct ocfs2_dir_block_trailer *trailer; 480 481 trailer = ocfs2_trailer_from_bh(bh, dir->i_sb); 482 if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) { 483 rc = ocfs2_error(dir->i_sb, 484 "Invalid dirblock #%llu: signature = %.*s\n", 485 (unsigned long long)bh->b_blocknr, 7, 486 trailer->db_signature); 487 goto out; 488 } 489 if (le64_to_cpu(trailer->db_blkno) != bh->b_blocknr) { 490 rc = ocfs2_error(dir->i_sb, 491 "Directory block #%llu has an invalid db_blkno of %llu\n", 492 (unsigned long long)bh->b_blocknr, 493 (unsigned long long)le64_to_cpu(trailer->db_blkno)); 494 goto out; 495 } 496 if (le64_to_cpu(trailer->db_parent_dinode) != 497 OCFS2_I(dir)->ip_blkno) { 498 rc = ocfs2_error(dir->i_sb, 499 "Directory block #%llu on dinode #%llu has an invalid parent_dinode of %llu\n", 500 (unsigned long long)bh->b_blocknr, 501 (unsigned long long)OCFS2_I(dir)->ip_blkno, 502 (unsigned long long)le64_to_cpu(trailer->db_blkno)); 503 goto out; 504 } 505 out: 506 return rc; 507 } 508 509 /* 510 * This function forces all errors to -EIO for consistency with its 511 * predecessor, ocfs2_bread(). We haven't audited what returning the 512 * real error codes would do to callers. We log the real codes with 513 * mlog_errno() before we squash them. 514 */ 515 static int ocfs2_read_dir_block(struct inode *inode, u64 v_block, 516 struct buffer_head **bh, int flags) 517 { 518 int rc = 0; 519 struct buffer_head *tmp = *bh; 520 521 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags, 522 ocfs2_validate_dir_block); 523 if (rc) { 524 mlog_errno(rc); 525 goto out; 526 } 527 528 if (!(flags & OCFS2_BH_READAHEAD) && 529 ocfs2_supports_dir_trailer(inode)) { 530 rc = ocfs2_check_dir_trailer(inode, tmp); 531 if (rc) { 532 if (!*bh) 533 brelse(tmp); 534 mlog_errno(rc); 535 goto out; 536 } 537 } 538 539 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */ 540 if (!*bh) 541 *bh = tmp; 542 543 out: 544 return rc ? -EIO : 0; 545 } 546 547 /* 548 * Read the block at 'phys' which belongs to this directory 549 * inode. This function does no virtual->physical block translation - 550 * what's passed in is assumed to be a valid directory block. 551 */ 552 static int ocfs2_read_dir_block_direct(struct inode *dir, u64 phys, 553 struct buffer_head **bh) 554 { 555 int ret; 556 struct buffer_head *tmp = *bh; 557 558 ret = ocfs2_read_block(INODE_CACHE(dir), phys, &tmp, 559 ocfs2_validate_dir_block); 560 if (ret) { 561 mlog_errno(ret); 562 goto out; 563 } 564 565 if (ocfs2_supports_dir_trailer(dir)) { 566 ret = ocfs2_check_dir_trailer(dir, tmp); 567 if (ret) { 568 if (!*bh) 569 brelse(tmp); 570 mlog_errno(ret); 571 goto out; 572 } 573 } 574 575 if (!ret && !*bh) 576 *bh = tmp; 577 out: 578 return ret; 579 } 580 581 static int ocfs2_validate_dx_root(struct super_block *sb, 582 struct buffer_head *bh) 583 { 584 int ret; 585 struct ocfs2_dx_root_block *dx_root; 586 587 BUG_ON(!buffer_uptodate(bh)); 588 589 dx_root = (struct ocfs2_dx_root_block *) bh->b_data; 590 591 ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_root->dr_check); 592 if (ret) { 593 mlog(ML_ERROR, 594 "Checksum failed for dir index root block %llu\n", 595 (unsigned long long)bh->b_blocknr); 596 goto bail; 597 } 598 599 if (!OCFS2_IS_VALID_DX_ROOT(dx_root)) { 600 ret = ocfs2_error(sb, 601 "Dir Index Root # %llu has bad signature %.*s\n", 602 (unsigned long long)le64_to_cpu(dx_root->dr_blkno), 603 7, dx_root->dr_signature); 604 goto bail; 605 } 606 607 if (!(dx_root->dr_flags & OCFS2_DX_FLAG_INLINE)) { 608 struct ocfs2_extent_list *el = &dx_root->dr_list; 609 610 if (le16_to_cpu(el->l_count) != ocfs2_extent_recs_per_dx_root(sb)) { 611 ret = ocfs2_error(sb, 612 "Dir Index Root # %llu has invalid l_count %u (expected %u)\n", 613 (unsigned long long)le64_to_cpu(dx_root->dr_blkno), 614 le16_to_cpu(el->l_count), 615 ocfs2_extent_recs_per_dx_root(sb)); 616 goto bail; 617 } 618 619 if (le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count)) { 620 ret = ocfs2_error(sb, 621 "Dir Index Root # %llu has invalid l_next_free_rec %u (l_count %u)\n", 622 (unsigned long long)le64_to_cpu(dx_root->dr_blkno), 623 le16_to_cpu(el->l_next_free_rec), 624 le16_to_cpu(el->l_count)); 625 goto bail; 626 } 627 } 628 629 bail: 630 return ret; 631 } 632 633 static int ocfs2_read_dx_root(struct inode *dir, struct ocfs2_dinode *di, 634 struct buffer_head **dx_root_bh) 635 { 636 int ret; 637 u64 blkno = le64_to_cpu(di->i_dx_root); 638 struct buffer_head *tmp = *dx_root_bh; 639 640 ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp, 641 ocfs2_validate_dx_root); 642 643 /* If ocfs2_read_block() got us a new bh, pass it up. */ 644 if (!ret && !*dx_root_bh) 645 *dx_root_bh = tmp; 646 647 return ret; 648 } 649 650 static int ocfs2_validate_dx_leaf(struct super_block *sb, 651 struct buffer_head *bh) 652 { 653 int ret; 654 struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)bh->b_data; 655 656 BUG_ON(!buffer_uptodate(bh)); 657 658 ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_leaf->dl_check); 659 if (ret) { 660 mlog(ML_ERROR, 661 "Checksum failed for dir index leaf block %llu\n", 662 (unsigned long long)bh->b_blocknr); 663 return ret; 664 } 665 666 if (!OCFS2_IS_VALID_DX_LEAF(dx_leaf)) { 667 ret = ocfs2_error(sb, "Dir Index Leaf has bad signature %.*s\n", 668 7, dx_leaf->dl_signature); 669 } 670 671 return ret; 672 } 673 674 static int ocfs2_read_dx_leaf(struct inode *dir, u64 blkno, 675 struct buffer_head **dx_leaf_bh) 676 { 677 int ret; 678 struct buffer_head *tmp = *dx_leaf_bh; 679 680 ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp, 681 ocfs2_validate_dx_leaf); 682 683 /* If ocfs2_read_block() got us a new bh, pass it up. */ 684 if (!ret && !*dx_leaf_bh) 685 *dx_leaf_bh = tmp; 686 687 return ret; 688 } 689 690 /* 691 * Read a series of dx_leaf blocks. This expects all buffer_head 692 * pointers to be NULL on function entry. 693 */ 694 static int ocfs2_read_dx_leaves(struct inode *dir, u64 start, int num, 695 struct buffer_head **dx_leaf_bhs) 696 { 697 int ret; 698 699 ret = ocfs2_read_blocks(INODE_CACHE(dir), start, num, dx_leaf_bhs, 0, 700 ocfs2_validate_dx_leaf); 701 if (ret) 702 mlog_errno(ret); 703 704 return ret; 705 } 706 707 static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen, 708 struct inode *dir, 709 struct ocfs2_dir_entry **res_dir) 710 { 711 struct super_block *sb; 712 struct buffer_head *bh_use[NAMEI_RA_SIZE]; 713 struct buffer_head *bh, *ret = NULL; 714 unsigned long start, block, b; 715 int ra_max = 0; /* Number of bh's in the readahead 716 buffer, bh_use[] */ 717 int ra_ptr = 0; /* Current index into readahead 718 buffer */ 719 int num = 0; 720 int nblocks, i; 721 722 sb = dir->i_sb; 723 724 nblocks = i_size_read(dir) >> sb->s_blocksize_bits; 725 start = OCFS2_I(dir)->ip_dir_start_lookup; 726 if (start >= nblocks) 727 start = 0; 728 block = start; 729 730 restart: 731 do { 732 /* 733 * We deal with the read-ahead logic here. 734 */ 735 if (ra_ptr >= ra_max) { 736 /* Refill the readahead buffer */ 737 ra_ptr = 0; 738 b = block; 739 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) { 740 /* 741 * Terminate if we reach the end of the 742 * directory and must wrap, or if our 743 * search has finished at this block. 744 */ 745 if (b >= nblocks || (num && block == start)) { 746 bh_use[ra_max] = NULL; 747 break; 748 } 749 num++; 750 751 bh = NULL; 752 ocfs2_read_dir_block(dir, b++, &bh, 753 OCFS2_BH_READAHEAD); 754 bh_use[ra_max] = bh; 755 } 756 } 757 if ((bh = bh_use[ra_ptr++]) == NULL) 758 goto next; 759 if (ocfs2_read_dir_block(dir, block, &bh, 0)) { 760 /* read error, skip block & hope for the best. 761 * ocfs2_read_dir_block() has released the bh. */ 762 mlog(ML_ERROR, "reading directory %llu, " 763 "offset %lu\n", 764 (unsigned long long)OCFS2_I(dir)->ip_blkno, 765 block); 766 goto next; 767 } 768 i = ocfs2_search_dirblock(bh, dir, name, namelen, 769 block << sb->s_blocksize_bits, 770 bh->b_data, sb->s_blocksize, 771 res_dir); 772 if (i == 1) { 773 OCFS2_I(dir)->ip_dir_start_lookup = block; 774 ret = bh; 775 goto cleanup_and_exit; 776 } else { 777 brelse(bh); 778 if (i < 0) 779 goto cleanup_and_exit; 780 } 781 next: 782 if (++block >= nblocks) 783 block = 0; 784 } while (block != start); 785 786 /* 787 * If the directory has grown while we were searching, then 788 * search the last part of the directory before giving up. 789 */ 790 block = nblocks; 791 nblocks = i_size_read(dir) >> sb->s_blocksize_bits; 792 if (block < nblocks) { 793 start = 0; 794 goto restart; 795 } 796 797 cleanup_and_exit: 798 /* Clean up the read-ahead blocks */ 799 for (; ra_ptr < ra_max; ra_ptr++) 800 brelse(bh_use[ra_ptr]); 801 802 trace_ocfs2_find_entry_el(ret); 803 return ret; 804 } 805 806 static int ocfs2_dx_dir_lookup_rec(struct inode *inode, 807 struct ocfs2_extent_list *el, 808 u32 major_hash, 809 u32 *ret_cpos, 810 u64 *ret_phys_blkno, 811 unsigned int *ret_clen) 812 { 813 int ret = 0, i, found; 814 struct buffer_head *eb_bh = NULL; 815 struct ocfs2_extent_block *eb; 816 struct ocfs2_extent_rec *rec = NULL; 817 818 if (el->l_tree_depth) { 819 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, major_hash, 820 &eb_bh); 821 if (ret) { 822 mlog_errno(ret); 823 goto out; 824 } 825 826 eb = (struct ocfs2_extent_block *) eb_bh->b_data; 827 el = &eb->h_list; 828 829 if (el->l_tree_depth) { 830 ret = ocfs2_error(inode->i_sb, 831 "Inode %llu has non zero tree depth in btree tree block %llu\n", 832 inode->i_ino, 833 (unsigned long long)eb_bh->b_blocknr); 834 goto out; 835 } 836 } 837 838 found = 0; 839 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) { 840 rec = &el->l_recs[i]; 841 842 if (le32_to_cpu(rec->e_cpos) <= major_hash) { 843 found = 1; 844 break; 845 } 846 } 847 848 if (!found) { 849 ret = ocfs2_error(inode->i_sb, 850 "Inode %llu has no extent record for hash %u in btree (next_free_rec %u)\n", 851 inode->i_ino, major_hash, 852 le16_to_cpu(el->l_next_free_rec)); 853 goto out; 854 } 855 856 if (ret_phys_blkno) 857 *ret_phys_blkno = le64_to_cpu(rec->e_blkno); 858 if (ret_cpos) 859 *ret_cpos = le32_to_cpu(rec->e_cpos); 860 if (ret_clen) 861 *ret_clen = le16_to_cpu(rec->e_leaf_clusters); 862 863 out: 864 brelse(eb_bh); 865 return ret; 866 } 867 868 /* 869 * Returns the block index, from the start of the cluster which this 870 * hash belongs too. 871 */ 872 static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb, 873 u32 minor_hash) 874 { 875 return minor_hash & osb->osb_dx_mask; 876 } 877 878 static inline unsigned int ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb, 879 struct ocfs2_dx_hinfo *hinfo) 880 { 881 return __ocfs2_dx_dir_hash_idx(osb, hinfo->minor_hash); 882 } 883 884 static int ocfs2_dx_dir_lookup(struct inode *inode, 885 struct ocfs2_extent_list *el, 886 struct ocfs2_dx_hinfo *hinfo, 887 u32 *ret_cpos, 888 u64 *ret_phys_blkno) 889 { 890 int ret = 0; 891 unsigned int cend, clen; 892 u32 cpos; 893 u64 blkno; 894 u32 name_hash = hinfo->major_hash; 895 896 ret = ocfs2_dx_dir_lookup_rec(inode, el, name_hash, &cpos, &blkno, 897 &clen); 898 if (ret) { 899 mlog_errno(ret); 900 goto out; 901 } 902 903 cend = cpos + clen; 904 if (name_hash >= cend) { 905 /* We want the last cluster */ 906 blkno += ocfs2_clusters_to_blocks(inode->i_sb, clen - 1); 907 cpos += clen - 1; 908 } else { 909 blkno += ocfs2_clusters_to_blocks(inode->i_sb, 910 name_hash - cpos); 911 cpos = name_hash; 912 } 913 914 /* 915 * We now have the cluster which should hold our entry. To 916 * find the exact block from the start of the cluster to 917 * search, we take the lower bits of the hash. 918 */ 919 blkno += ocfs2_dx_dir_hash_idx(OCFS2_SB(inode->i_sb), hinfo); 920 921 if (ret_phys_blkno) 922 *ret_phys_blkno = blkno; 923 if (ret_cpos) 924 *ret_cpos = cpos; 925 926 out: 927 928 return ret; 929 } 930 931 static int ocfs2_dx_dir_search(const char *name, int namelen, 932 struct inode *dir, 933 struct ocfs2_dx_root_block *dx_root, 934 struct ocfs2_dir_lookup_result *res) 935 { 936 int ret, i, found; 937 u64 phys; 938 struct buffer_head *dx_leaf_bh = NULL; 939 struct ocfs2_dx_leaf *dx_leaf; 940 struct ocfs2_dx_entry *dx_entry = NULL; 941 struct buffer_head *dir_ent_bh = NULL; 942 struct ocfs2_dir_entry *dir_ent = NULL; 943 struct ocfs2_dx_hinfo *hinfo = &res->dl_hinfo; 944 struct ocfs2_extent_list *dr_el; 945 struct ocfs2_dx_entry_list *entry_list; 946 947 ocfs2_dx_dir_name_hash(dir, name, namelen, &res->dl_hinfo); 948 949 if (ocfs2_dx_root_inline(dx_root)) { 950 entry_list = &dx_root->dr_entries; 951 goto search; 952 } 953 954 dr_el = &dx_root->dr_list; 955 956 ret = ocfs2_dx_dir_lookup(dir, dr_el, hinfo, NULL, &phys); 957 if (ret) { 958 mlog_errno(ret); 959 goto out; 960 } 961 962 trace_ocfs2_dx_dir_search((unsigned long long)OCFS2_I(dir)->ip_blkno, 963 namelen, name, hinfo->major_hash, 964 hinfo->minor_hash, (unsigned long long)phys); 965 966 ret = ocfs2_read_dx_leaf(dir, phys, &dx_leaf_bh); 967 if (ret) { 968 mlog_errno(ret); 969 goto out; 970 } 971 972 dx_leaf = (struct ocfs2_dx_leaf *) dx_leaf_bh->b_data; 973 974 trace_ocfs2_dx_dir_search_leaf_info( 975 le16_to_cpu(dx_leaf->dl_list.de_num_used), 976 le16_to_cpu(dx_leaf->dl_list.de_count)); 977 978 entry_list = &dx_leaf->dl_list; 979 980 search: 981 /* 982 * Empty leaf is legal, so no need to check for that. 983 */ 984 found = 0; 985 for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) { 986 dx_entry = &entry_list->de_entries[i]; 987 988 if (hinfo->major_hash != le32_to_cpu(dx_entry->dx_major_hash) 989 || hinfo->minor_hash != le32_to_cpu(dx_entry->dx_minor_hash)) 990 continue; 991 992 /* 993 * Search unindexed leaf block now. We're not 994 * guaranteed to find anything. 995 */ 996 ret = ocfs2_read_dir_block_direct(dir, 997 le64_to_cpu(dx_entry->dx_dirent_blk), 998 &dir_ent_bh); 999 if (ret) { 1000 mlog_errno(ret); 1001 goto out; 1002 } 1003 1004 /* 1005 * XXX: We should check the unindexed block here, 1006 * before using it. 1007 */ 1008 1009 found = ocfs2_search_dirblock(dir_ent_bh, dir, name, namelen, 1010 0, dir_ent_bh->b_data, 1011 dir->i_sb->s_blocksize, &dir_ent); 1012 if (found == 1) 1013 break; 1014 1015 if (found == -1) { 1016 /* This means we found a bad directory entry. */ 1017 ret = -EIO; 1018 mlog_errno(ret); 1019 goto out; 1020 } 1021 1022 brelse(dir_ent_bh); 1023 dir_ent_bh = NULL; 1024 } 1025 1026 if (found <= 0) { 1027 ret = -ENOENT; 1028 goto out; 1029 } 1030 1031 res->dl_leaf_bh = dir_ent_bh; 1032 res->dl_entry = dir_ent; 1033 res->dl_dx_leaf_bh = dx_leaf_bh; 1034 res->dl_dx_entry = dx_entry; 1035 1036 ret = 0; 1037 out: 1038 if (ret) { 1039 brelse(dx_leaf_bh); 1040 brelse(dir_ent_bh); 1041 } 1042 return ret; 1043 } 1044 1045 static int ocfs2_find_entry_dx(const char *name, int namelen, 1046 struct inode *dir, 1047 struct ocfs2_dir_lookup_result *lookup) 1048 { 1049 int ret; 1050 struct buffer_head *di_bh = NULL; 1051 struct ocfs2_dinode *di; 1052 struct buffer_head *dx_root_bh = NULL; 1053 struct ocfs2_dx_root_block *dx_root; 1054 1055 ret = ocfs2_read_inode_block(dir, &di_bh); 1056 if (ret) { 1057 mlog_errno(ret); 1058 goto out; 1059 } 1060 1061 di = (struct ocfs2_dinode *)di_bh->b_data; 1062 1063 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh); 1064 if (ret) { 1065 mlog_errno(ret); 1066 goto out; 1067 } 1068 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data; 1069 1070 ret = ocfs2_dx_dir_search(name, namelen, dir, dx_root, lookup); 1071 if (ret) { 1072 if (ret != -ENOENT) 1073 mlog_errno(ret); 1074 goto out; 1075 } 1076 1077 lookup->dl_dx_root_bh = dx_root_bh; 1078 dx_root_bh = NULL; 1079 out: 1080 brelse(di_bh); 1081 brelse(dx_root_bh); 1082 return ret; 1083 } 1084 1085 /* 1086 * Try to find an entry of the provided name within 'dir'. 1087 * 1088 * If nothing was found, -ENOENT is returned. Otherwise, zero is 1089 * returned and the struct 'res' will contain information useful to 1090 * other directory manipulation functions. 1091 * 1092 * Caller can NOT assume anything about the contents of the 1093 * buffer_heads - they are passed back only so that it can be passed 1094 * into any one of the manipulation functions (add entry, delete 1095 * entry, etc). As an example, bh in the extent directory case is a 1096 * data block, in the inline-data case it actually points to an inode, 1097 * in the indexed directory case, multiple buffers are involved. 1098 */ 1099 int ocfs2_find_entry(const char *name, int namelen, 1100 struct inode *dir, struct ocfs2_dir_lookup_result *lookup) 1101 { 1102 struct buffer_head *bh; 1103 struct ocfs2_dir_entry *res_dir = NULL; 1104 int ret = 0; 1105 1106 if (ocfs2_dir_indexed(dir)) 1107 return ocfs2_find_entry_dx(name, namelen, dir, lookup); 1108 1109 if (unlikely(i_size_read(dir) <= 0)) { 1110 ret = -EFSCORRUPTED; 1111 mlog_errno(ret); 1112 goto out; 1113 } 1114 /* 1115 * The unindexed dir code only uses part of the lookup 1116 * structure, so there's no reason to push it down further 1117 * than this. 1118 */ 1119 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { 1120 if (unlikely(i_size_read(dir) > dir->i_sb->s_blocksize)) { 1121 ret = -EFSCORRUPTED; 1122 mlog_errno(ret); 1123 goto out; 1124 } 1125 bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir); 1126 } else { 1127 bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir); 1128 } 1129 1130 if (bh == NULL) 1131 return -ENOENT; 1132 1133 lookup->dl_leaf_bh = bh; 1134 lookup->dl_entry = res_dir; 1135 out: 1136 return ret; 1137 } 1138 1139 /* 1140 * Update inode number and type of a previously found directory entry. 1141 */ 1142 int ocfs2_update_entry(struct inode *dir, handle_t *handle, 1143 struct ocfs2_dir_lookup_result *res, 1144 struct inode *new_entry_inode) 1145 { 1146 int ret; 1147 ocfs2_journal_access_func access = ocfs2_journal_access_db; 1148 struct ocfs2_dir_entry *de = res->dl_entry; 1149 struct buffer_head *de_bh = res->dl_leaf_bh; 1150 1151 /* 1152 * The same code works fine for both inline-data and extent 1153 * based directories, so no need to split this up. The only 1154 * difference is the journal_access function. 1155 */ 1156 1157 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) 1158 access = ocfs2_journal_access_di; 1159 1160 ret = access(handle, INODE_CACHE(dir), de_bh, 1161 OCFS2_JOURNAL_ACCESS_WRITE); 1162 if (ret) { 1163 mlog_errno(ret); 1164 goto out; 1165 } 1166 1167 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno); 1168 ocfs2_set_de_type(de, new_entry_inode->i_mode); 1169 1170 ocfs2_journal_dirty(handle, de_bh); 1171 1172 out: 1173 return ret; 1174 } 1175 1176 /* 1177 * __ocfs2_delete_entry deletes a directory entry by merging it with the 1178 * previous entry 1179 */ 1180 static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir, 1181 struct ocfs2_dir_entry *de_del, 1182 struct buffer_head *bh, char *first_de, 1183 unsigned int bytes) 1184 { 1185 struct ocfs2_dir_entry *de, *pde; 1186 int i, status = -ENOENT; 1187 ocfs2_journal_access_func access = ocfs2_journal_access_db; 1188 1189 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) 1190 access = ocfs2_journal_access_di; 1191 1192 i = 0; 1193 pde = NULL; 1194 de = (struct ocfs2_dir_entry *) first_de; 1195 while (i < bytes) { 1196 if (!ocfs2_check_dir_entry(dir, de, bh, first_de, bytes, i)) { 1197 status = -EIO; 1198 mlog_errno(status); 1199 goto bail; 1200 } 1201 if (de == de_del) { 1202 status = access(handle, INODE_CACHE(dir), bh, 1203 OCFS2_JOURNAL_ACCESS_WRITE); 1204 if (status < 0) { 1205 status = -EIO; 1206 mlog_errno(status); 1207 goto bail; 1208 } 1209 if (pde) 1210 le16_add_cpu(&pde->rec_len, 1211 le16_to_cpu(de->rec_len)); 1212 de->inode = 0; 1213 inode_inc_iversion(dir); 1214 ocfs2_journal_dirty(handle, bh); 1215 goto bail; 1216 } 1217 i += le16_to_cpu(de->rec_len); 1218 pde = de; 1219 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len)); 1220 } 1221 bail: 1222 return status; 1223 } 1224 1225 static unsigned int ocfs2_figure_dirent_hole(struct ocfs2_dir_entry *de) 1226 { 1227 unsigned int hole; 1228 1229 if (le64_to_cpu(de->inode) == 0) 1230 hole = le16_to_cpu(de->rec_len); 1231 else 1232 hole = le16_to_cpu(de->rec_len) - 1233 OCFS2_DIR_REC_LEN(de->name_len); 1234 1235 return hole; 1236 } 1237 1238 static int ocfs2_find_max_rec_len(struct super_block *sb, 1239 struct buffer_head *dirblock_bh) 1240 { 1241 int size, this_hole, largest_hole = 0; 1242 char *trailer, *de_buf, *limit, *start = dirblock_bh->b_data; 1243 struct ocfs2_dir_entry *de; 1244 1245 trailer = (char *)ocfs2_trailer_from_bh(dirblock_bh, sb); 1246 size = ocfs2_dir_trailer_blk_off(sb); 1247 limit = start + size; 1248 de_buf = start; 1249 de = (struct ocfs2_dir_entry *)de_buf; 1250 do { 1251 if (de_buf != trailer) { 1252 this_hole = ocfs2_figure_dirent_hole(de); 1253 if (this_hole > largest_hole) 1254 largest_hole = this_hole; 1255 } 1256 1257 de_buf += le16_to_cpu(de->rec_len); 1258 de = (struct ocfs2_dir_entry *)de_buf; 1259 } while (de_buf < limit); 1260 1261 if (largest_hole >= OCFS2_DIR_MIN_REC_LEN) 1262 return largest_hole; 1263 return 0; 1264 } 1265 1266 static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list, 1267 int index) 1268 { 1269 int num_used = le16_to_cpu(entry_list->de_num_used); 1270 1271 if (num_used == 1 || index == (num_used - 1)) 1272 goto clear; 1273 1274 memmove(&entry_list->de_entries[index], 1275 &entry_list->de_entries[index + 1], 1276 (num_used - index - 1)*sizeof(struct ocfs2_dx_entry)); 1277 clear: 1278 num_used--; 1279 memset(&entry_list->de_entries[num_used], 0, 1280 sizeof(struct ocfs2_dx_entry)); 1281 entry_list->de_num_used = cpu_to_le16(num_used); 1282 } 1283 1284 static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir, 1285 struct ocfs2_dir_lookup_result *lookup) 1286 { 1287 int ret, index, max_rec_len, add_to_free_list = 0; 1288 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh; 1289 struct buffer_head *leaf_bh = lookup->dl_leaf_bh; 1290 struct ocfs2_dx_leaf *dx_leaf; 1291 struct ocfs2_dx_entry *dx_entry = lookup->dl_dx_entry; 1292 struct ocfs2_dir_block_trailer *trailer; 1293 struct ocfs2_dx_root_block *dx_root; 1294 struct ocfs2_dx_entry_list *entry_list; 1295 1296 /* 1297 * This function gets a bit messy because we might have to 1298 * modify the root block, regardless of whether the indexed 1299 * entries are stored inline. 1300 */ 1301 1302 /* 1303 * *Only* set 'entry_list' here, based on where we're looking 1304 * for the indexed entries. Later, we might still want to 1305 * journal both blocks, based on free list state. 1306 */ 1307 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; 1308 if (ocfs2_dx_root_inline(dx_root)) { 1309 entry_list = &dx_root->dr_entries; 1310 } else { 1311 dx_leaf = (struct ocfs2_dx_leaf *) lookup->dl_dx_leaf_bh->b_data; 1312 entry_list = &dx_leaf->dl_list; 1313 } 1314 1315 /* Neither of these are a disk corruption - that should have 1316 * been caught by lookup, before we got here. */ 1317 BUG_ON(le16_to_cpu(entry_list->de_count) <= 0); 1318 BUG_ON(le16_to_cpu(entry_list->de_num_used) <= 0); 1319 1320 index = (char *)dx_entry - (char *)entry_list->de_entries; 1321 index /= sizeof(*dx_entry); 1322 1323 if (index >= le16_to_cpu(entry_list->de_num_used)) { 1324 mlog(ML_ERROR, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)\n", 1325 (unsigned long long)OCFS2_I(dir)->ip_blkno, index, 1326 entry_list, dx_entry); 1327 return -EIO; 1328 } 1329 1330 /* 1331 * We know that removal of this dirent will leave enough room 1332 * for a new one, so add this block to the free list if it 1333 * isn't already there. 1334 */ 1335 trailer = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb); 1336 if (trailer->db_free_rec_len == 0) 1337 add_to_free_list = 1; 1338 1339 /* 1340 * Add the block holding our index into the journal before 1341 * removing the unindexed entry. If we get an error return 1342 * from __ocfs2_delete_entry(), then it hasn't removed the 1343 * entry yet. Likewise, successful return means we *must* 1344 * remove the indexed entry. 1345 * 1346 * We're also careful to journal the root tree block here as 1347 * the entry count needs to be updated. Also, we might be 1348 * adding to the start of the free list. 1349 */ 1350 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh, 1351 OCFS2_JOURNAL_ACCESS_WRITE); 1352 if (ret) { 1353 mlog_errno(ret); 1354 goto out; 1355 } 1356 1357 if (!ocfs2_dx_root_inline(dx_root)) { 1358 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), 1359 lookup->dl_dx_leaf_bh, 1360 OCFS2_JOURNAL_ACCESS_WRITE); 1361 if (ret) { 1362 mlog_errno(ret); 1363 goto out; 1364 } 1365 } 1366 1367 trace_ocfs2_delete_entry_dx((unsigned long long)OCFS2_I(dir)->ip_blkno, 1368 index); 1369 1370 ret = __ocfs2_delete_entry(handle, dir, lookup->dl_entry, 1371 leaf_bh, leaf_bh->b_data, leaf_bh->b_size); 1372 if (ret) { 1373 mlog_errno(ret); 1374 goto out; 1375 } 1376 1377 max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, leaf_bh); 1378 trailer->db_free_rec_len = cpu_to_le16(max_rec_len); 1379 if (add_to_free_list) { 1380 trailer->db_free_next = dx_root->dr_free_blk; 1381 dx_root->dr_free_blk = cpu_to_le64(leaf_bh->b_blocknr); 1382 ocfs2_journal_dirty(handle, dx_root_bh); 1383 } 1384 1385 /* leaf_bh was journal_accessed for us in __ocfs2_delete_entry */ 1386 ocfs2_journal_dirty(handle, leaf_bh); 1387 1388 le32_add_cpu(&dx_root->dr_num_entries, -1); 1389 ocfs2_journal_dirty(handle, dx_root_bh); 1390 1391 ocfs2_dx_list_remove_entry(entry_list, index); 1392 1393 if (!ocfs2_dx_root_inline(dx_root)) 1394 ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh); 1395 1396 out: 1397 return ret; 1398 } 1399 1400 static inline int ocfs2_delete_entry_id(handle_t *handle, 1401 struct inode *dir, 1402 struct ocfs2_dir_entry *de_del, 1403 struct buffer_head *bh) 1404 { 1405 int ret; 1406 struct buffer_head *di_bh = NULL; 1407 struct ocfs2_dinode *di; 1408 struct ocfs2_inline_data *data; 1409 1410 ret = ocfs2_read_inode_block(dir, &di_bh); 1411 if (ret) { 1412 mlog_errno(ret); 1413 goto out; 1414 } 1415 1416 di = (struct ocfs2_dinode *)di_bh->b_data; 1417 data = &di->id2.i_data; 1418 1419 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data, 1420 i_size_read(dir)); 1421 1422 brelse(di_bh); 1423 out: 1424 return ret; 1425 } 1426 1427 static inline int ocfs2_delete_entry_el(handle_t *handle, 1428 struct inode *dir, 1429 struct ocfs2_dir_entry *de_del, 1430 struct buffer_head *bh) 1431 { 1432 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data, 1433 bh->b_size); 1434 } 1435 1436 /* 1437 * Delete a directory entry. Hide the details of directory 1438 * implementation from the caller. 1439 */ 1440 int ocfs2_delete_entry(handle_t *handle, 1441 struct inode *dir, 1442 struct ocfs2_dir_lookup_result *res) 1443 { 1444 if (ocfs2_dir_indexed(dir)) 1445 return ocfs2_delete_entry_dx(handle, dir, res); 1446 1447 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) 1448 return ocfs2_delete_entry_id(handle, dir, res->dl_entry, 1449 res->dl_leaf_bh); 1450 1451 return ocfs2_delete_entry_el(handle, dir, res->dl_entry, 1452 res->dl_leaf_bh); 1453 } 1454 1455 /* 1456 * Check whether 'de' has enough room to hold an entry of 1457 * 'new_rec_len' bytes. 1458 */ 1459 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de, 1460 unsigned int new_rec_len) 1461 { 1462 unsigned int de_really_used; 1463 1464 /* Check whether this is an empty record with enough space */ 1465 if (le64_to_cpu(de->inode) == 0 && 1466 le16_to_cpu(de->rec_len) >= new_rec_len) 1467 return 1; 1468 1469 /* 1470 * Record might have free space at the end which we can 1471 * use. 1472 */ 1473 de_really_used = OCFS2_DIR_REC_LEN(de->name_len); 1474 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len)) 1475 return 1; 1476 1477 return 0; 1478 } 1479 1480 static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf *dx_leaf, 1481 struct ocfs2_dx_entry *dx_new_entry) 1482 { 1483 int i; 1484 1485 i = le16_to_cpu(dx_leaf->dl_list.de_num_used); 1486 dx_leaf->dl_list.de_entries[i] = *dx_new_entry; 1487 1488 le16_add_cpu(&dx_leaf->dl_list.de_num_used, 1); 1489 } 1490 1491 static void ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list *entry_list, 1492 struct ocfs2_dx_hinfo *hinfo, 1493 u64 dirent_blk) 1494 { 1495 int i; 1496 struct ocfs2_dx_entry *dx_entry; 1497 1498 i = le16_to_cpu(entry_list->de_num_used); 1499 dx_entry = &entry_list->de_entries[i]; 1500 1501 memset(dx_entry, 0, sizeof(*dx_entry)); 1502 dx_entry->dx_major_hash = cpu_to_le32(hinfo->major_hash); 1503 dx_entry->dx_minor_hash = cpu_to_le32(hinfo->minor_hash); 1504 dx_entry->dx_dirent_blk = cpu_to_le64(dirent_blk); 1505 1506 le16_add_cpu(&entry_list->de_num_used, 1); 1507 } 1508 1509 static int __ocfs2_dx_dir_leaf_insert(struct inode *dir, handle_t *handle, 1510 struct ocfs2_dx_hinfo *hinfo, 1511 u64 dirent_blk, 1512 struct buffer_head *dx_leaf_bh) 1513 { 1514 int ret; 1515 struct ocfs2_dx_leaf *dx_leaf; 1516 1517 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh, 1518 OCFS2_JOURNAL_ACCESS_WRITE); 1519 if (ret) { 1520 mlog_errno(ret); 1521 goto out; 1522 } 1523 1524 dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data; 1525 ocfs2_dx_entry_list_insert(&dx_leaf->dl_list, hinfo, dirent_blk); 1526 ocfs2_journal_dirty(handle, dx_leaf_bh); 1527 1528 out: 1529 return ret; 1530 } 1531 1532 static void ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle, 1533 struct ocfs2_dx_hinfo *hinfo, 1534 u64 dirent_blk, 1535 struct ocfs2_dx_root_block *dx_root) 1536 { 1537 ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk); 1538 } 1539 1540 static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle, 1541 struct ocfs2_dir_lookup_result *lookup) 1542 { 1543 int ret = 0; 1544 struct ocfs2_dx_root_block *dx_root; 1545 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh; 1546 1547 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh, 1548 OCFS2_JOURNAL_ACCESS_WRITE); 1549 if (ret) { 1550 mlog_errno(ret); 1551 goto out; 1552 } 1553 1554 dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data; 1555 if (ocfs2_dx_root_inline(dx_root)) { 1556 ocfs2_dx_inline_root_insert(dir, handle, 1557 &lookup->dl_hinfo, 1558 lookup->dl_leaf_bh->b_blocknr, 1559 dx_root); 1560 } else { 1561 ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo, 1562 lookup->dl_leaf_bh->b_blocknr, 1563 lookup->dl_dx_leaf_bh); 1564 if (ret) 1565 goto out; 1566 } 1567 1568 le32_add_cpu(&dx_root->dr_num_entries, 1); 1569 ocfs2_journal_dirty(handle, dx_root_bh); 1570 1571 out: 1572 return ret; 1573 } 1574 1575 static void ocfs2_remove_block_from_free_list(struct inode *dir, 1576 handle_t *handle, 1577 struct ocfs2_dir_lookup_result *lookup) 1578 { 1579 struct ocfs2_dir_block_trailer *trailer, *prev; 1580 struct ocfs2_dx_root_block *dx_root; 1581 struct buffer_head *bh; 1582 1583 trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb); 1584 1585 if (ocfs2_free_list_at_root(lookup)) { 1586 bh = lookup->dl_dx_root_bh; 1587 dx_root = (struct ocfs2_dx_root_block *)bh->b_data; 1588 dx_root->dr_free_blk = trailer->db_free_next; 1589 } else { 1590 bh = lookup->dl_prev_leaf_bh; 1591 prev = ocfs2_trailer_from_bh(bh, dir->i_sb); 1592 prev->db_free_next = trailer->db_free_next; 1593 } 1594 1595 trailer->db_free_rec_len = cpu_to_le16(0); 1596 trailer->db_free_next = cpu_to_le64(0); 1597 1598 ocfs2_journal_dirty(handle, bh); 1599 ocfs2_journal_dirty(handle, lookup->dl_leaf_bh); 1600 } 1601 1602 /* 1603 * This expects that a journal write has been reserved on 1604 * lookup->dl_prev_leaf_bh or lookup->dl_dx_root_bh 1605 */ 1606 static void ocfs2_recalc_free_list(struct inode *dir, handle_t *handle, 1607 struct ocfs2_dir_lookup_result *lookup) 1608 { 1609 int max_rec_len; 1610 struct ocfs2_dir_block_trailer *trailer; 1611 1612 /* Walk dl_leaf_bh to figure out what the new free rec_len is. */ 1613 max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, lookup->dl_leaf_bh); 1614 if (max_rec_len) { 1615 /* 1616 * There's still room in this block, so no need to remove it 1617 * from the free list. In this case, we just want to update 1618 * the rec len accounting. 1619 */ 1620 trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb); 1621 trailer->db_free_rec_len = cpu_to_le16(max_rec_len); 1622 ocfs2_journal_dirty(handle, lookup->dl_leaf_bh); 1623 } else { 1624 ocfs2_remove_block_from_free_list(dir, handle, lookup); 1625 } 1626 } 1627 1628 /* we don't always have a dentry for what we want to add, so people 1629 * like orphan dir can call this instead. 1630 * 1631 * The lookup context must have been filled from 1632 * ocfs2_prepare_dir_for_insert. 1633 */ 1634 int __ocfs2_add_entry(handle_t *handle, 1635 struct inode *dir, 1636 const char *name, int namelen, 1637 struct inode *inode, u64 blkno, 1638 struct buffer_head *parent_fe_bh, 1639 struct ocfs2_dir_lookup_result *lookup) 1640 { 1641 unsigned long offset; 1642 unsigned short rec_len; 1643 struct ocfs2_dir_entry *de, *de1; 1644 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data; 1645 struct super_block *sb = dir->i_sb; 1646 int retval; 1647 unsigned int size = sb->s_blocksize; 1648 struct buffer_head *insert_bh = lookup->dl_leaf_bh; 1649 char *data_start = insert_bh->b_data; 1650 1651 if (ocfs2_dir_indexed(dir)) { 1652 struct buffer_head *bh; 1653 1654 /* 1655 * An indexed dir may require that we update the free space 1656 * list. Reserve a write to the previous node in the list so 1657 * that we don't fail later. 1658 * 1659 * XXX: This can be either a dx_root_block, or an unindexed 1660 * directory tree leaf block. 1661 */ 1662 if (ocfs2_free_list_at_root(lookup)) { 1663 bh = lookup->dl_dx_root_bh; 1664 retval = ocfs2_journal_access_dr(handle, 1665 INODE_CACHE(dir), bh, 1666 OCFS2_JOURNAL_ACCESS_WRITE); 1667 } else { 1668 bh = lookup->dl_prev_leaf_bh; 1669 retval = ocfs2_journal_access_db(handle, 1670 INODE_CACHE(dir), bh, 1671 OCFS2_JOURNAL_ACCESS_WRITE); 1672 } 1673 if (retval) { 1674 mlog_errno(retval); 1675 return retval; 1676 } 1677 } else if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { 1678 data_start = di->id2.i_data.id_data; 1679 size = i_size_read(dir); 1680 1681 BUG_ON(insert_bh != parent_fe_bh); 1682 } 1683 1684 rec_len = OCFS2_DIR_REC_LEN(namelen); 1685 offset = 0; 1686 de = (struct ocfs2_dir_entry *) data_start; 1687 while (1) { 1688 BUG_ON((char *)de >= (size + data_start)); 1689 1690 /* These checks should've already been passed by the 1691 * prepare function, but I guess we can leave them 1692 * here anyway. */ 1693 if (!ocfs2_check_dir_entry(dir, de, insert_bh, data_start, 1694 size, offset)) { 1695 retval = -ENOENT; 1696 goto bail; 1697 } 1698 if (ocfs2_match(namelen, name, de)) { 1699 retval = -EEXIST; 1700 goto bail; 1701 } 1702 1703 /* We're guaranteed that we should have space, so we 1704 * can't possibly have hit the trailer...right? */ 1705 mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size), 1706 "Hit dir trailer trying to insert %.*s " 1707 "(namelen %d) into directory %llu. " 1708 "offset is %lu, trailer offset is %d\n", 1709 namelen, name, namelen, 1710 (unsigned long long)parent_fe_bh->b_blocknr, 1711 offset, ocfs2_dir_trailer_blk_off(dir->i_sb)); 1712 1713 if (ocfs2_dirent_would_fit(de, rec_len)) { 1714 inode_set_mtime_to_ts(dir, 1715 inode_set_ctime_current(dir)); 1716 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh); 1717 if (retval < 0) { 1718 mlog_errno(retval); 1719 goto bail; 1720 } 1721 1722 if (insert_bh == parent_fe_bh) 1723 retval = ocfs2_journal_access_di(handle, 1724 INODE_CACHE(dir), 1725 insert_bh, 1726 OCFS2_JOURNAL_ACCESS_WRITE); 1727 else { 1728 retval = ocfs2_journal_access_db(handle, 1729 INODE_CACHE(dir), 1730 insert_bh, 1731 OCFS2_JOURNAL_ACCESS_WRITE); 1732 1733 if (!retval && ocfs2_dir_indexed(dir)) 1734 retval = ocfs2_dx_dir_insert(dir, 1735 handle, 1736 lookup); 1737 } 1738 1739 if (retval) { 1740 mlog_errno(retval); 1741 goto bail; 1742 } 1743 1744 /* By now the buffer is marked for journaling */ 1745 offset += le16_to_cpu(de->rec_len); 1746 if (le64_to_cpu(de->inode)) { 1747 de1 = (struct ocfs2_dir_entry *)((char *) de + 1748 OCFS2_DIR_REC_LEN(de->name_len)); 1749 de1->rec_len = 1750 cpu_to_le16(le16_to_cpu(de->rec_len) - 1751 OCFS2_DIR_REC_LEN(de->name_len)); 1752 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len)); 1753 de = de1; 1754 } 1755 de->file_type = FT_UNKNOWN; 1756 if (blkno) { 1757 de->inode = cpu_to_le64(blkno); 1758 ocfs2_set_de_type(de, inode->i_mode); 1759 } else 1760 de->inode = 0; 1761 de->name_len = namelen; 1762 memcpy(de->name, name, namelen); 1763 1764 if (ocfs2_dir_indexed(dir)) 1765 ocfs2_recalc_free_list(dir, handle, lookup); 1766 1767 inode_inc_iversion(dir); 1768 ocfs2_journal_dirty(handle, insert_bh); 1769 retval = 0; 1770 goto bail; 1771 } 1772 1773 offset += le16_to_cpu(de->rec_len); 1774 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len)); 1775 } 1776 1777 /* when you think about it, the assert above should prevent us 1778 * from ever getting here. */ 1779 retval = -ENOSPC; 1780 bail: 1781 if (retval) 1782 mlog_errno(retval); 1783 1784 return retval; 1785 } 1786 1787 static int ocfs2_dir_foreach_blk_id(struct inode *inode, 1788 u64 *f_version, 1789 struct dir_context *ctx) 1790 { 1791 int ret, i; 1792 unsigned long offset = ctx->pos; 1793 struct buffer_head *di_bh = NULL; 1794 struct ocfs2_dinode *di; 1795 struct ocfs2_inline_data *data; 1796 struct ocfs2_dir_entry *de; 1797 1798 ret = ocfs2_read_inode_block(inode, &di_bh); 1799 if (ret) { 1800 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n", 1801 (unsigned long long)OCFS2_I(inode)->ip_blkno); 1802 goto out; 1803 } 1804 1805 di = (struct ocfs2_dinode *)di_bh->b_data; 1806 data = &di->id2.i_data; 1807 1808 while (ctx->pos < i_size_read(inode)) { 1809 /* If the dir block has changed since the last call to 1810 * readdir(2), then we might be pointing to an invalid 1811 * dirent right now. Scan from the start of the block 1812 * to make sure. */ 1813 if (!inode_eq_iversion(inode, *f_version)) { 1814 for (i = 0; i < i_size_read(inode) && i < offset; ) { 1815 de = (struct ocfs2_dir_entry *) 1816 (data->id_data + i); 1817 /* It's too expensive to do a full 1818 * dirent test each time round this 1819 * loop, but we do have to test at 1820 * least that it is non-zero. A 1821 * failure will be detected in the 1822 * dirent test below. */ 1823 if (le16_to_cpu(de->rec_len) < 1824 OCFS2_DIR_REC_LEN(1)) 1825 break; 1826 i += le16_to_cpu(de->rec_len); 1827 } 1828 ctx->pos = offset = i; 1829 *f_version = inode_query_iversion(inode); 1830 } 1831 1832 de = (struct ocfs2_dir_entry *) (data->id_data + ctx->pos); 1833 if (!ocfs2_check_dir_entry(inode, de, di_bh, (char *)data->id_data, 1834 i_size_read(inode), ctx->pos)) { 1835 /* On error, skip the f_pos to the end. */ 1836 ctx->pos = i_size_read(inode); 1837 break; 1838 } 1839 offset += le16_to_cpu(de->rec_len); 1840 if (le64_to_cpu(de->inode)) { 1841 if (!dir_emit(ctx, de->name, de->name_len, 1842 le64_to_cpu(de->inode), 1843 fs_ftype_to_dtype(de->file_type))) 1844 goto out; 1845 } 1846 ctx->pos += le16_to_cpu(de->rec_len); 1847 } 1848 out: 1849 brelse(di_bh); 1850 return 0; 1851 } 1852 1853 /* 1854 * NOTE: This function can be called against unindexed directories, 1855 * and indexed ones. 1856 */ 1857 static int ocfs2_dir_foreach_blk_el(struct inode *inode, 1858 u64 *f_version, 1859 struct dir_context *ctx, 1860 bool persist) 1861 { 1862 unsigned long offset, blk, last_ra_blk = 0; 1863 int i; 1864 struct buffer_head * bh, * tmp; 1865 struct ocfs2_dir_entry * de; 1866 struct super_block * sb = inode->i_sb; 1867 unsigned int ra_sectors = 16; 1868 int stored = 0; 1869 1870 bh = NULL; 1871 1872 offset = ctx->pos & (sb->s_blocksize - 1); 1873 1874 while (ctx->pos < i_size_read(inode)) { 1875 blk = ctx->pos >> sb->s_blocksize_bits; 1876 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) { 1877 /* Skip the corrupt dirblock and keep trying */ 1878 ctx->pos += sb->s_blocksize - offset; 1879 continue; 1880 } 1881 1882 /* The idea here is to begin with 8k read-ahead and to stay 1883 * 4k ahead of our current position. 1884 * 1885 * TODO: Use the pagecache for this. We just need to 1886 * make sure it's cluster-safe... */ 1887 if (!last_ra_blk 1888 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) { 1889 for (i = ra_sectors >> (sb->s_blocksize_bits - 9); 1890 i > 0; i--) { 1891 tmp = NULL; 1892 if (!ocfs2_read_dir_block(inode, ++blk, &tmp, 1893 OCFS2_BH_READAHEAD)) 1894 brelse(tmp); 1895 } 1896 last_ra_blk = blk; 1897 ra_sectors = 8; 1898 } 1899 1900 /* If the dir block has changed since the last call to 1901 * readdir(2), then we might be pointing to an invalid 1902 * dirent right now. Scan from the start of the block 1903 * to make sure. */ 1904 if (!inode_eq_iversion(inode, *f_version)) { 1905 for (i = 0; i < sb->s_blocksize && i < offset; ) { 1906 de = (struct ocfs2_dir_entry *) (bh->b_data + i); 1907 /* It's too expensive to do a full 1908 * dirent test each time round this 1909 * loop, but we do have to test at 1910 * least that it is non-zero. A 1911 * failure will be detected in the 1912 * dirent test below. */ 1913 if (le16_to_cpu(de->rec_len) < 1914 OCFS2_DIR_REC_LEN(1)) 1915 break; 1916 i += le16_to_cpu(de->rec_len); 1917 } 1918 offset = i; 1919 ctx->pos = (ctx->pos & ~(sb->s_blocksize - 1)) 1920 | offset; 1921 *f_version = inode_query_iversion(inode); 1922 } 1923 1924 while (ctx->pos < i_size_read(inode) 1925 && offset < sb->s_blocksize) { 1926 de = (struct ocfs2_dir_entry *) (bh->b_data + offset); 1927 if (!ocfs2_check_dir_entry(inode, de, bh, bh->b_data, 1928 sb->s_blocksize, offset)) { 1929 /* On error, skip the f_pos to the 1930 next block. */ 1931 ctx->pos = (ctx->pos | (sb->s_blocksize - 1)) + 1; 1932 break; 1933 } 1934 if (le64_to_cpu(de->inode)) { 1935 if (!dir_emit(ctx, de->name, 1936 de->name_len, 1937 le64_to_cpu(de->inode), 1938 fs_ftype_to_dtype(de->file_type))) { 1939 brelse(bh); 1940 return 0; 1941 } 1942 stored++; 1943 } 1944 offset += le16_to_cpu(de->rec_len); 1945 ctx->pos += le16_to_cpu(de->rec_len); 1946 } 1947 offset = 0; 1948 brelse(bh); 1949 bh = NULL; 1950 if (!persist && stored) 1951 break; 1952 } 1953 return 0; 1954 } 1955 1956 static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version, 1957 struct dir_context *ctx, 1958 bool persist) 1959 { 1960 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) 1961 return ocfs2_dir_foreach_blk_id(inode, f_version, ctx); 1962 return ocfs2_dir_foreach_blk_el(inode, f_version, ctx, persist); 1963 } 1964 1965 /* 1966 * This is intended to be called from inside other kernel functions, 1967 * so we fake some arguments. 1968 */ 1969 int ocfs2_dir_foreach(struct inode *inode, struct dir_context *ctx) 1970 { 1971 u64 version = inode_query_iversion(inode); 1972 ocfs2_dir_foreach_blk(inode, &version, ctx, true); 1973 return 0; 1974 } 1975 1976 /* 1977 * ocfs2_readdir() 1978 * 1979 */ 1980 int ocfs2_readdir(struct file *file, struct dir_context *ctx) 1981 { 1982 int error = 0; 1983 struct inode *inode = file_inode(file); 1984 struct ocfs2_file_private *fp = file->private_data; 1985 int lock_level = 0; 1986 1987 trace_ocfs2_readdir((unsigned long long)OCFS2_I(inode)->ip_blkno); 1988 1989 error = ocfs2_inode_lock_atime(inode, file->f_path.mnt, &lock_level, 1); 1990 if (lock_level && error >= 0) { 1991 /* We release EX lock which used to update atime 1992 * and get PR lock again to reduce contention 1993 * on commonly accessed directories. */ 1994 ocfs2_inode_unlock(inode, 1); 1995 lock_level = 0; 1996 error = ocfs2_inode_lock(inode, NULL, 0); 1997 } 1998 if (error < 0) { 1999 if (error != -ENOENT) 2000 mlog_errno(error); 2001 /* we haven't got any yet, so propagate the error. */ 2002 goto bail_nolock; 2003 } 2004 2005 error = ocfs2_dir_foreach_blk(inode, &fp->cookie, ctx, false); 2006 2007 ocfs2_inode_unlock(inode, lock_level); 2008 if (error) 2009 mlog_errno(error); 2010 2011 bail_nolock: 2012 2013 return error; 2014 } 2015 2016 /* 2017 * NOTE: this should always be called with parent dir i_rwsem taken. 2018 */ 2019 int ocfs2_find_files_on_disk(const char *name, 2020 int namelen, 2021 u64 *blkno, 2022 struct inode *inode, 2023 struct ocfs2_dir_lookup_result *lookup) 2024 { 2025 int status = -ENOENT; 2026 2027 trace_ocfs2_find_files_on_disk(namelen, name, blkno, 2028 (unsigned long long)OCFS2_I(inode)->ip_blkno); 2029 2030 status = ocfs2_find_entry(name, namelen, inode, lookup); 2031 if (status) 2032 goto leave; 2033 2034 *blkno = le64_to_cpu(lookup->dl_entry->inode); 2035 2036 status = 0; 2037 leave: 2038 2039 return status; 2040 } 2041 2042 /* 2043 * Convenience function for callers which just want the block number 2044 * mapped to a name and don't require the full dirent info, etc. 2045 */ 2046 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name, 2047 int namelen, u64 *blkno) 2048 { 2049 int ret; 2050 struct ocfs2_dir_lookup_result lookup = { NULL, }; 2051 2052 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup); 2053 ocfs2_free_dir_lookup_result(&lookup); 2054 2055 return ret; 2056 } 2057 2058 /* Check for a name within a directory. 2059 * 2060 * Return 0 if the name does not exist 2061 * Return -EEXIST if the directory contains the name 2062 * Return -EFSCORRUPTED if found corruption 2063 * 2064 * Callers should have i_rwsem + a cluster lock on dir 2065 */ 2066 int ocfs2_check_dir_for_entry(struct inode *dir, 2067 const char *name, 2068 int namelen) 2069 { 2070 int ret = 0; 2071 struct ocfs2_dir_lookup_result lookup = { NULL, }; 2072 2073 trace_ocfs2_check_dir_for_entry( 2074 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name); 2075 2076 ret = ocfs2_find_entry(name, namelen, dir, &lookup); 2077 if (ret == 0) { 2078 ret = -EEXIST; 2079 mlog_errno(ret); 2080 } else if (ret == -ENOENT) { 2081 ret = 0; 2082 } 2083 2084 ocfs2_free_dir_lookup_result(&lookup); 2085 2086 return ret; 2087 } 2088 2089 struct ocfs2_empty_dir_priv { 2090 struct dir_context ctx; 2091 unsigned seen_dot; 2092 unsigned seen_dot_dot; 2093 unsigned seen_other; 2094 unsigned dx_dir; 2095 }; 2096 static bool ocfs2_empty_dir_filldir(struct dir_context *ctx, const char *name, 2097 int name_len, loff_t pos, u64 ino, 2098 unsigned type) 2099 { 2100 struct ocfs2_empty_dir_priv *p = 2101 container_of(ctx, struct ocfs2_empty_dir_priv, ctx); 2102 2103 /* 2104 * Check the positions of "." and ".." records to be sure 2105 * they're in the correct place. 2106 * 2107 * Indexed directories don't need to proceed past the first 2108 * two entries, so we end the scan after seeing '..'. Despite 2109 * that, we allow the scan to proceed In the event that we 2110 * have a corrupted indexed directory (no dot or dot dot 2111 * entries). This allows us to double check for existing 2112 * entries which might not have been found in the index. 2113 */ 2114 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) { 2115 p->seen_dot = 1; 2116 return true; 2117 } 2118 2119 if (name_len == 2 && !strncmp("..", name, 2) && 2120 pos == OCFS2_DIR_REC_LEN(1)) { 2121 p->seen_dot_dot = 1; 2122 2123 if (p->dx_dir && p->seen_dot) 2124 return false; 2125 2126 return true; 2127 } 2128 2129 p->seen_other = 1; 2130 return false; 2131 } 2132 2133 static int ocfs2_empty_dir_dx(struct inode *inode, 2134 struct ocfs2_empty_dir_priv *priv) 2135 { 2136 int ret; 2137 struct buffer_head *di_bh = NULL; 2138 struct buffer_head *dx_root_bh = NULL; 2139 struct ocfs2_dinode *di; 2140 struct ocfs2_dx_root_block *dx_root; 2141 2142 priv->dx_dir = 1; 2143 2144 ret = ocfs2_read_inode_block(inode, &di_bh); 2145 if (ret) { 2146 mlog_errno(ret); 2147 goto out; 2148 } 2149 di = (struct ocfs2_dinode *)di_bh->b_data; 2150 2151 ret = ocfs2_read_dx_root(inode, di, &dx_root_bh); 2152 if (ret) { 2153 mlog_errno(ret); 2154 goto out; 2155 } 2156 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; 2157 2158 if (le32_to_cpu(dx_root->dr_num_entries) != 2) 2159 priv->seen_other = 1; 2160 2161 out: 2162 brelse(di_bh); 2163 brelse(dx_root_bh); 2164 return ret; 2165 } 2166 2167 /* 2168 * routine to check that the specified directory is empty (for rmdir) 2169 * 2170 * Returns 1 if dir is empty, zero otherwise. 2171 * 2172 * XXX: This is a performance problem for unindexed directories. 2173 */ 2174 int ocfs2_empty_dir(struct inode *inode) 2175 { 2176 int ret; 2177 struct ocfs2_empty_dir_priv priv = { 2178 .ctx.actor = ocfs2_empty_dir_filldir, 2179 }; 2180 2181 if (ocfs2_dir_indexed(inode)) { 2182 ret = ocfs2_empty_dir_dx(inode, &priv); 2183 if (ret) 2184 mlog_errno(ret); 2185 /* 2186 * We still run ocfs2_dir_foreach to get the checks 2187 * for "." and "..". 2188 */ 2189 } 2190 2191 ret = ocfs2_dir_foreach(inode, &priv.ctx); 2192 if (ret) 2193 mlog_errno(ret); 2194 2195 if (!priv.seen_dot || !priv.seen_dot_dot) { 2196 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n", 2197 (unsigned long long)OCFS2_I(inode)->ip_blkno); 2198 /* 2199 * XXX: Is it really safe to allow an unlink to continue? 2200 */ 2201 return 1; 2202 } 2203 2204 return !priv.seen_other; 2205 } 2206 2207 /* 2208 * Fills "." and ".." dirents in a new directory block. Returns dirent for 2209 * "..", which might be used during creation of a directory with a trailing 2210 * header. It is otherwise safe to ignore the return code. 2211 */ 2212 static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode, 2213 struct inode *parent, 2214 char *start, 2215 unsigned int size) 2216 { 2217 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start; 2218 2219 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno); 2220 de->name_len = 1; 2221 de->rec_len = 2222 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len)); 2223 strscpy(de->name, "."); 2224 ocfs2_set_de_type(de, S_IFDIR); 2225 2226 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len)); 2227 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno); 2228 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1)); 2229 de->name_len = 2; 2230 strscpy(de->name, ".."); 2231 ocfs2_set_de_type(de, S_IFDIR); 2232 2233 return de; 2234 } 2235 2236 /* 2237 * This works together with code in ocfs2_mknod_locked() which sets 2238 * the inline-data flag and initializes the inline-data section. 2239 */ 2240 static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb, 2241 handle_t *handle, 2242 struct inode *parent, 2243 struct inode *inode, 2244 struct buffer_head *di_bh) 2245 { 2246 int ret; 2247 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 2248 struct ocfs2_inline_data *data = &di->id2.i_data; 2249 unsigned int size = le16_to_cpu(data->id_count); 2250 2251 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, 2252 OCFS2_JOURNAL_ACCESS_WRITE); 2253 if (ret) { 2254 mlog_errno(ret); 2255 goto out; 2256 } 2257 2258 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size); 2259 ocfs2_journal_dirty(handle, di_bh); 2260 2261 i_size_write(inode, size); 2262 set_nlink(inode, 2); 2263 inode->i_blocks = ocfs2_inode_sector_count(inode); 2264 2265 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh); 2266 if (ret < 0) 2267 mlog_errno(ret); 2268 2269 out: 2270 return ret; 2271 } 2272 2273 static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb, 2274 handle_t *handle, 2275 struct inode *parent, 2276 struct inode *inode, 2277 struct buffer_head *fe_bh, 2278 struct ocfs2_alloc_context *data_ac, 2279 struct buffer_head **ret_new_bh) 2280 { 2281 int status; 2282 unsigned int size = osb->sb->s_blocksize; 2283 struct buffer_head *new_bh = NULL; 2284 struct ocfs2_dir_entry *de; 2285 2286 if (ocfs2_new_dir_wants_trailer(inode)) 2287 size = ocfs2_dir_trailer_blk_off(parent->i_sb); 2288 2289 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh, 2290 data_ac, NULL, &new_bh); 2291 if (status < 0) { 2292 mlog_errno(status); 2293 goto bail; 2294 } 2295 2296 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh); 2297 2298 status = ocfs2_journal_access_db(handle, INODE_CACHE(inode), new_bh, 2299 OCFS2_JOURNAL_ACCESS_CREATE); 2300 if (status < 0) { 2301 mlog_errno(status); 2302 goto bail; 2303 } 2304 memset(new_bh->b_data, 0, osb->sb->s_blocksize); 2305 2306 de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size); 2307 if (ocfs2_new_dir_wants_trailer(inode)) { 2308 int size = le16_to_cpu(de->rec_len); 2309 2310 /* 2311 * Figure out the size of the hole left over after 2312 * insertion of '.' and '..'. The trailer wants this 2313 * information. 2314 */ 2315 size -= OCFS2_DIR_REC_LEN(2); 2316 size -= sizeof(struct ocfs2_dir_block_trailer); 2317 2318 ocfs2_init_dir_trailer(inode, new_bh, size); 2319 } 2320 2321 ocfs2_journal_dirty(handle, new_bh); 2322 2323 i_size_write(inode, inode->i_sb->s_blocksize); 2324 set_nlink(inode, 2); 2325 inode->i_blocks = ocfs2_inode_sector_count(inode); 2326 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); 2327 if (status < 0) { 2328 mlog_errno(status); 2329 goto bail; 2330 } 2331 2332 status = 0; 2333 if (ret_new_bh) { 2334 *ret_new_bh = new_bh; 2335 new_bh = NULL; 2336 } 2337 bail: 2338 brelse(new_bh); 2339 2340 return status; 2341 } 2342 2343 static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb, 2344 handle_t *handle, struct inode *dir, 2345 struct buffer_head *di_bh, 2346 struct buffer_head *dirdata_bh, 2347 struct ocfs2_alloc_context *meta_ac, 2348 int dx_inline, u32 num_entries, 2349 struct buffer_head **ret_dx_root_bh) 2350 { 2351 int ret; 2352 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; 2353 u16 dr_suballoc_bit; 2354 u64 suballoc_loc, dr_blkno; 2355 unsigned int num_bits; 2356 struct buffer_head *dx_root_bh = NULL; 2357 struct ocfs2_dx_root_block *dx_root; 2358 struct ocfs2_dir_block_trailer *trailer = 2359 ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb); 2360 2361 ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc, 2362 &dr_suballoc_bit, &num_bits, &dr_blkno); 2363 if (ret) { 2364 mlog_errno(ret); 2365 goto out; 2366 } 2367 2368 trace_ocfs2_dx_dir_attach_index( 2369 (unsigned long long)OCFS2_I(dir)->ip_blkno, 2370 (unsigned long long)dr_blkno); 2371 2372 dx_root_bh = sb_getblk(osb->sb, dr_blkno); 2373 if (dx_root_bh == NULL) { 2374 ret = -ENOMEM; 2375 goto out; 2376 } 2377 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dx_root_bh); 2378 2379 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh, 2380 OCFS2_JOURNAL_ACCESS_CREATE); 2381 if (ret < 0) { 2382 mlog_errno(ret); 2383 goto out; 2384 } 2385 2386 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; 2387 memset(dx_root, 0, osb->sb->s_blocksize); 2388 strscpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE); 2389 dx_root->dr_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot); 2390 dx_root->dr_suballoc_loc = cpu_to_le64(suballoc_loc); 2391 dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit); 2392 dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation); 2393 dx_root->dr_blkno = cpu_to_le64(dr_blkno); 2394 dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno); 2395 dx_root->dr_num_entries = cpu_to_le32(num_entries); 2396 if (le16_to_cpu(trailer->db_free_rec_len)) 2397 dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr); 2398 else 2399 dx_root->dr_free_blk = cpu_to_le64(0); 2400 2401 if (dx_inline) { 2402 dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE; 2403 dx_root->dr_entries.de_count = 2404 cpu_to_le16(ocfs2_dx_entries_per_root(osb->sb)); 2405 } else { 2406 dx_root->dr_list.l_count = 2407 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb)); 2408 } 2409 ocfs2_journal_dirty(handle, dx_root_bh); 2410 2411 ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh, 2412 OCFS2_JOURNAL_ACCESS_CREATE); 2413 if (ret) { 2414 mlog_errno(ret); 2415 goto out; 2416 } 2417 2418 di->i_dx_root = cpu_to_le64(dr_blkno); 2419 2420 spin_lock(&OCFS2_I(dir)->ip_lock); 2421 OCFS2_I(dir)->ip_dyn_features |= OCFS2_INDEXED_DIR_FL; 2422 di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features); 2423 spin_unlock(&OCFS2_I(dir)->ip_lock); 2424 2425 ocfs2_journal_dirty(handle, di_bh); 2426 2427 *ret_dx_root_bh = dx_root_bh; 2428 dx_root_bh = NULL; 2429 2430 out: 2431 brelse(dx_root_bh); 2432 return ret; 2433 } 2434 2435 static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb, 2436 handle_t *handle, struct inode *dir, 2437 struct buffer_head **dx_leaves, 2438 int num_dx_leaves, u64 start_blk) 2439 { 2440 int ret, i; 2441 struct ocfs2_dx_leaf *dx_leaf; 2442 struct buffer_head *bh; 2443 2444 for (i = 0; i < num_dx_leaves; i++) { 2445 bh = sb_getblk(osb->sb, start_blk + i); 2446 if (bh == NULL) { 2447 ret = -ENOMEM; 2448 goto out; 2449 } 2450 dx_leaves[i] = bh; 2451 2452 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), bh); 2453 2454 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), bh, 2455 OCFS2_JOURNAL_ACCESS_CREATE); 2456 if (ret < 0) { 2457 mlog_errno(ret); 2458 goto out; 2459 } 2460 2461 dx_leaf = (struct ocfs2_dx_leaf *) bh->b_data; 2462 2463 memset(dx_leaf, 0, osb->sb->s_blocksize); 2464 strscpy(dx_leaf->dl_signature, OCFS2_DX_LEAF_SIGNATURE); 2465 dx_leaf->dl_fs_generation = cpu_to_le32(osb->fs_generation); 2466 dx_leaf->dl_blkno = cpu_to_le64(bh->b_blocknr); 2467 dx_leaf->dl_list.de_count = 2468 cpu_to_le16(ocfs2_dx_entries_per_leaf(osb->sb)); 2469 2470 trace_ocfs2_dx_dir_format_cluster( 2471 (unsigned long long)OCFS2_I(dir)->ip_blkno, 2472 (unsigned long long)bh->b_blocknr, 2473 le16_to_cpu(dx_leaf->dl_list.de_count)); 2474 2475 ocfs2_journal_dirty(handle, bh); 2476 } 2477 2478 ret = 0; 2479 out: 2480 return ret; 2481 } 2482 2483 /* 2484 * Allocates and formats a new cluster for use in an indexed dir 2485 * leaf. This version will not do the extent insert, so that it can be 2486 * used by operations which need careful ordering. 2487 */ 2488 static int __ocfs2_dx_dir_new_cluster(struct inode *dir, 2489 u32 cpos, handle_t *handle, 2490 struct ocfs2_alloc_context *data_ac, 2491 struct buffer_head **dx_leaves, 2492 int num_dx_leaves, u64 *ret_phys_blkno) 2493 { 2494 int ret; 2495 u32 phys, num; 2496 u64 phys_blkno; 2497 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); 2498 2499 /* 2500 * XXX: For create, this should claim cluster for the index 2501 * *before* the unindexed insert so that we have a better 2502 * chance of contiguousness as the directory grows in number 2503 * of entries. 2504 */ 2505 ret = __ocfs2_claim_clusters(handle, data_ac, 1, 1, &phys, &num); 2506 if (ret) { 2507 mlog_errno(ret); 2508 goto out; 2509 } 2510 2511 /* 2512 * Format the new cluster first. That way, we're inserting 2513 * valid data. 2514 */ 2515 phys_blkno = ocfs2_clusters_to_blocks(osb->sb, phys); 2516 ret = ocfs2_dx_dir_format_cluster(osb, handle, dir, dx_leaves, 2517 num_dx_leaves, phys_blkno); 2518 if (ret) { 2519 mlog_errno(ret); 2520 goto out; 2521 } 2522 2523 *ret_phys_blkno = phys_blkno; 2524 out: 2525 return ret; 2526 } 2527 2528 static int ocfs2_dx_dir_new_cluster(struct inode *dir, 2529 struct ocfs2_extent_tree *et, 2530 u32 cpos, handle_t *handle, 2531 struct ocfs2_alloc_context *data_ac, 2532 struct ocfs2_alloc_context *meta_ac, 2533 struct buffer_head **dx_leaves, 2534 int num_dx_leaves) 2535 { 2536 int ret; 2537 u64 phys_blkno; 2538 2539 ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves, 2540 num_dx_leaves, &phys_blkno); 2541 if (ret) { 2542 mlog_errno(ret); 2543 goto out; 2544 } 2545 2546 ret = ocfs2_insert_extent(handle, et, cpos, phys_blkno, 1, 0, 2547 meta_ac); 2548 if (ret) 2549 mlog_errno(ret); 2550 out: 2551 return ret; 2552 } 2553 2554 static struct buffer_head **ocfs2_dx_dir_kmalloc_leaves(struct super_block *sb, 2555 int *ret_num_leaves) 2556 { 2557 int num_dx_leaves = ocfs2_clusters_to_blocks(sb, 1); 2558 struct buffer_head **dx_leaves; 2559 2560 dx_leaves = kzalloc_objs(struct buffer_head *, num_dx_leaves, GFP_NOFS); 2561 if (dx_leaves && ret_num_leaves) 2562 *ret_num_leaves = num_dx_leaves; 2563 2564 return dx_leaves; 2565 } 2566 2567 static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb, 2568 handle_t *handle, 2569 struct inode *parent, 2570 struct inode *inode, 2571 struct buffer_head *di_bh, 2572 struct ocfs2_alloc_context *data_ac, 2573 struct ocfs2_alloc_context *meta_ac) 2574 { 2575 int ret; 2576 struct buffer_head *leaf_bh = NULL; 2577 struct buffer_head *dx_root_bh = NULL; 2578 struct ocfs2_dx_hinfo hinfo; 2579 struct ocfs2_dx_root_block *dx_root; 2580 struct ocfs2_dx_entry_list *entry_list; 2581 2582 /* 2583 * Our strategy is to create the directory as though it were 2584 * unindexed, then add the index block. This works with very 2585 * little complication since the state of a new directory is a 2586 * very well known quantity. 2587 * 2588 * Essentially, we have two dirents ("." and ".."), in the 1st 2589 * block which need indexing. These are easily inserted into 2590 * the index block. 2591 */ 2592 2593 ret = ocfs2_fill_new_dir_el(osb, handle, parent, inode, di_bh, 2594 data_ac, &leaf_bh); 2595 if (ret) { 2596 mlog_errno(ret); 2597 goto out; 2598 } 2599 2600 ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh, leaf_bh, 2601 meta_ac, 1, 2, &dx_root_bh); 2602 if (ret) { 2603 mlog_errno(ret); 2604 goto out; 2605 } 2606 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; 2607 entry_list = &dx_root->dr_entries; 2608 2609 /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */ 2610 ocfs2_dx_dir_name_hash(inode, ".", 1, &hinfo); 2611 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr); 2612 2613 ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo); 2614 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr); 2615 2616 out: 2617 brelse(dx_root_bh); 2618 brelse(leaf_bh); 2619 return ret; 2620 } 2621 2622 int ocfs2_fill_new_dir(struct ocfs2_super *osb, 2623 handle_t *handle, 2624 struct inode *parent, 2625 struct inode *inode, 2626 struct buffer_head *fe_bh, 2627 struct ocfs2_alloc_context *data_ac, 2628 struct ocfs2_alloc_context *meta_ac) 2629 2630 { 2631 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL); 2632 2633 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) 2634 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh); 2635 2636 if (ocfs2_supports_indexed_dirs(osb)) 2637 return ocfs2_fill_new_dir_dx(osb, handle, parent, inode, fe_bh, 2638 data_ac, meta_ac); 2639 2640 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh, 2641 data_ac, NULL); 2642 } 2643 2644 static int ocfs2_dx_dir_index_block(struct inode *dir, 2645 handle_t *handle, 2646 struct buffer_head **dx_leaves, 2647 int num_dx_leaves, 2648 u32 *num_dx_entries, 2649 struct buffer_head *dirent_bh) 2650 { 2651 int ret = 0, namelen, i; 2652 char *de_buf, *limit; 2653 struct ocfs2_dir_entry *de; 2654 struct buffer_head *dx_leaf_bh; 2655 struct ocfs2_dx_hinfo hinfo; 2656 u64 dirent_blk = dirent_bh->b_blocknr; 2657 2658 de_buf = dirent_bh->b_data; 2659 limit = de_buf + dir->i_sb->s_blocksize; 2660 2661 while (de_buf < limit) { 2662 de = (struct ocfs2_dir_entry *)de_buf; 2663 2664 namelen = de->name_len; 2665 if (!namelen || !de->inode) 2666 goto inc; 2667 2668 ocfs2_dx_dir_name_hash(dir, de->name, namelen, &hinfo); 2669 2670 i = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), &hinfo); 2671 dx_leaf_bh = dx_leaves[i]; 2672 2673 ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &hinfo, 2674 dirent_blk, dx_leaf_bh); 2675 if (ret) { 2676 mlog_errno(ret); 2677 goto out; 2678 } 2679 2680 *num_dx_entries = *num_dx_entries + 1; 2681 2682 inc: 2683 de_buf += le16_to_cpu(de->rec_len); 2684 } 2685 2686 out: 2687 return ret; 2688 } 2689 2690 /* 2691 * XXX: This expects dx_root_bh to already be part of the transaction. 2692 */ 2693 static void ocfs2_dx_dir_index_root_block(struct inode *dir, 2694 struct buffer_head *dx_root_bh, 2695 struct buffer_head *dirent_bh) 2696 { 2697 char *de_buf, *limit; 2698 struct ocfs2_dx_root_block *dx_root; 2699 struct ocfs2_dir_entry *de; 2700 struct ocfs2_dx_hinfo hinfo; 2701 u64 dirent_blk = dirent_bh->b_blocknr; 2702 2703 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; 2704 2705 de_buf = dirent_bh->b_data; 2706 limit = de_buf + dir->i_sb->s_blocksize; 2707 2708 while (de_buf < limit) { 2709 de = (struct ocfs2_dir_entry *)de_buf; 2710 2711 if (!de->name_len || !de->inode) 2712 goto inc; 2713 2714 ocfs2_dx_dir_name_hash(dir, de->name, de->name_len, &hinfo); 2715 2716 trace_ocfs2_dx_dir_index_root_block( 2717 (unsigned long long)dir->i_ino, 2718 hinfo.major_hash, hinfo.minor_hash, 2719 de->name_len, de->name, 2720 le16_to_cpu(dx_root->dr_entries.de_num_used)); 2721 2722 ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo, 2723 dirent_blk); 2724 2725 le32_add_cpu(&dx_root->dr_num_entries, 1); 2726 inc: 2727 de_buf += le16_to_cpu(de->rec_len); 2728 } 2729 } 2730 2731 /* 2732 * Count the number of inline directory entries in di_bh and compare 2733 * them against the number of entries we can hold in an inline dx root 2734 * block. 2735 */ 2736 static int ocfs2_new_dx_should_be_inline(struct inode *dir, 2737 struct buffer_head *di_bh) 2738 { 2739 int dirent_count = 0; 2740 char *de_buf, *limit; 2741 struct ocfs2_dir_entry *de; 2742 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 2743 2744 de_buf = di->id2.i_data.id_data; 2745 limit = de_buf + i_size_read(dir); 2746 2747 while (de_buf < limit) { 2748 de = (struct ocfs2_dir_entry *)de_buf; 2749 2750 if (de->name_len && de->inode) 2751 dirent_count++; 2752 2753 de_buf += le16_to_cpu(de->rec_len); 2754 } 2755 2756 /* We are careful to leave room for one extra record. */ 2757 return dirent_count < ocfs2_dx_entries_per_root(dir->i_sb); 2758 } 2759 2760 /* 2761 * Expand rec_len of the rightmost dirent in a directory block so that it 2762 * contains the end of our valid space for dirents. We do this during 2763 * expansion from an inline directory to one with extents. The first dir block 2764 * in that case is taken from the inline data portion of the inode block. 2765 * 2766 * This will also return the largest amount of contiguous space for a dirent 2767 * in the block. That value is *not* necessarily the last dirent, even after 2768 * expansion. The directory indexing code wants this value for free space 2769 * accounting. We do this here since we're already walking the entire dir 2770 * block. 2771 * 2772 * We add the dir trailer if this filesystem wants it. 2773 */ 2774 static unsigned int ocfs2_expand_last_dirent(char *start, unsigned int old_size, 2775 struct inode *dir) 2776 { 2777 struct super_block *sb = dir->i_sb; 2778 struct ocfs2_dir_entry *de; 2779 struct ocfs2_dir_entry *prev_de; 2780 char *de_buf, *limit; 2781 unsigned int new_size = sb->s_blocksize; 2782 unsigned int bytes, this_hole; 2783 unsigned int largest_hole = 0; 2784 2785 if (ocfs2_new_dir_wants_trailer(dir)) 2786 new_size = ocfs2_dir_trailer_blk_off(sb); 2787 2788 bytes = new_size - old_size; 2789 2790 limit = start + old_size; 2791 de_buf = start; 2792 de = (struct ocfs2_dir_entry *)de_buf; 2793 do { 2794 this_hole = ocfs2_figure_dirent_hole(de); 2795 if (this_hole > largest_hole) 2796 largest_hole = this_hole; 2797 2798 prev_de = de; 2799 de_buf += le16_to_cpu(de->rec_len); 2800 de = (struct ocfs2_dir_entry *)de_buf; 2801 } while (de_buf < limit); 2802 2803 le16_add_cpu(&prev_de->rec_len, bytes); 2804 2805 /* We need to double check this after modification of the final 2806 * dirent. */ 2807 this_hole = ocfs2_figure_dirent_hole(prev_de); 2808 if (this_hole > largest_hole) 2809 largest_hole = this_hole; 2810 2811 if (largest_hole >= OCFS2_DIR_MIN_REC_LEN) 2812 return largest_hole; 2813 return 0; 2814 } 2815 2816 /* 2817 * We allocate enough clusters to fulfill "blocks_wanted", but set 2818 * i_size to exactly one block. Ocfs2_extend_dir() will handle the 2819 * rest automatically for us. 2820 * 2821 * *first_block_bh is a pointer to the 1st data block allocated to the 2822 * directory. 2823 */ 2824 static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh, 2825 unsigned int blocks_wanted, 2826 struct ocfs2_dir_lookup_result *lookup, 2827 struct buffer_head **first_block_bh) 2828 { 2829 u32 alloc, dx_alloc, bit_off, len, num_dx_entries = 0; 2830 struct super_block *sb = dir->i_sb; 2831 int ret, i, num_dx_leaves = 0, dx_inline = 0, 2832 credits = ocfs2_inline_to_extents_credits(sb); 2833 u64 dx_insert_blkno, blkno, 2834 bytes = blocks_wanted << sb->s_blocksize_bits; 2835 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); 2836 struct ocfs2_inode_info *oi = OCFS2_I(dir); 2837 struct ocfs2_alloc_context *data_ac = NULL; 2838 struct ocfs2_alloc_context *meta_ac = NULL; 2839 struct buffer_head *dirdata_bh = NULL; 2840 struct buffer_head *dx_root_bh = NULL; 2841 struct buffer_head **dx_leaves = NULL; 2842 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 2843 handle_t *handle; 2844 struct ocfs2_extent_tree et; 2845 struct ocfs2_extent_tree dx_et; 2846 int did_quota = 0, bytes_allocated = 0; 2847 2848 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir), di_bh); 2849 2850 alloc = ocfs2_clusters_for_bytes(sb, bytes); 2851 dx_alloc = 0; 2852 2853 down_write(&oi->ip_alloc_sem); 2854 2855 if (ocfs2_supports_indexed_dirs(osb)) { 2856 credits += ocfs2_add_dir_index_credits(sb); 2857 2858 dx_inline = ocfs2_new_dx_should_be_inline(dir, di_bh); 2859 if (!dx_inline) { 2860 /* Add one more cluster for an index leaf */ 2861 dx_alloc++; 2862 dx_leaves = ocfs2_dx_dir_kmalloc_leaves(sb, 2863 &num_dx_leaves); 2864 if (!dx_leaves) { 2865 ret = -ENOMEM; 2866 mlog_errno(ret); 2867 goto out; 2868 } 2869 } 2870 2871 /* This gets us the dx_root */ 2872 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac); 2873 if (ret) { 2874 mlog_errno(ret); 2875 goto out; 2876 } 2877 } 2878 2879 /* 2880 * We should never need more than 2 clusters for the unindexed 2881 * tree - maximum dirent size is far less than one block. In 2882 * fact, the only time we'd need more than one cluster is if 2883 * blocksize == clustersize and the dirent won't fit in the 2884 * extra space that the expansion to a single block gives. As 2885 * of today, that only happens on 4k/4k file systems. 2886 */ 2887 BUG_ON(alloc > 2); 2888 2889 ret = ocfs2_reserve_clusters(osb, alloc + dx_alloc, &data_ac); 2890 if (ret) { 2891 mlog_errno(ret); 2892 goto out; 2893 } 2894 2895 /* 2896 * Prepare for worst case allocation scenario of two separate 2897 * extents in the unindexed tree. 2898 */ 2899 if (alloc == 2) 2900 credits += OCFS2_SUBALLOC_ALLOC; 2901 2902 handle = ocfs2_start_trans(osb, credits); 2903 if (IS_ERR(handle)) { 2904 ret = PTR_ERR(handle); 2905 mlog_errno(ret); 2906 goto out; 2907 } 2908 2909 ret = dquot_alloc_space_nodirty(dir, 2910 ocfs2_clusters_to_bytes(osb->sb, alloc + dx_alloc)); 2911 if (ret) 2912 goto out_commit; 2913 did_quota = 1; 2914 2915 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) { 2916 /* 2917 * Allocate our index cluster first, to maximize the 2918 * possibility that unindexed leaves grow 2919 * contiguously. 2920 */ 2921 ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, 2922 dx_leaves, num_dx_leaves, 2923 &dx_insert_blkno); 2924 if (ret) { 2925 mlog_errno(ret); 2926 goto out_commit; 2927 } 2928 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1); 2929 } 2930 2931 /* 2932 * Try to claim as many clusters as the bitmap can give though 2933 * if we only get one now, that's enough to continue. The rest 2934 * will be claimed after the conversion to extents. 2935 */ 2936 if (ocfs2_dir_resv_allowed(osb)) 2937 data_ac->ac_resv = &oi->ip_la_data_resv; 2938 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, &len); 2939 if (ret) { 2940 mlog_errno(ret); 2941 goto out_commit; 2942 } 2943 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1); 2944 2945 /* 2946 * Operations are carefully ordered so that we set up the new 2947 * data block first. The conversion from inline data to 2948 * extents follows. 2949 */ 2950 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off); 2951 dirdata_bh = sb_getblk(sb, blkno); 2952 if (!dirdata_bh) { 2953 ret = -ENOMEM; 2954 mlog_errno(ret); 2955 goto out_commit; 2956 } 2957 2958 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dirdata_bh); 2959 2960 ret = ocfs2_journal_access_db(handle, INODE_CACHE(dir), dirdata_bh, 2961 OCFS2_JOURNAL_ACCESS_CREATE); 2962 if (ret) { 2963 mlog_errno(ret); 2964 goto out_commit; 2965 } 2966 2967 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir)); 2968 memset(dirdata_bh->b_data + i_size_read(dir), 0, 2969 sb->s_blocksize - i_size_read(dir)); 2970 i = ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), dir); 2971 if (ocfs2_new_dir_wants_trailer(dir)) { 2972 /* 2973 * Prepare the dir trailer up front. It will otherwise look 2974 * like a valid dirent. Even if inserting the index fails 2975 * (unlikely), then all we'll have done is given first dir 2976 * block a small amount of fragmentation. 2977 */ 2978 ocfs2_init_dir_trailer(dir, dirdata_bh, i); 2979 } 2980 2981 ocfs2_update_inode_fsync_trans(handle, dir, 1); 2982 ocfs2_journal_dirty(handle, dirdata_bh); 2983 2984 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) { 2985 /* 2986 * Dx dirs with an external cluster need to do this up 2987 * front. Inline dx root's get handled later, after 2988 * we've allocated our root block. We get passed back 2989 * a total number of items so that dr_num_entries can 2990 * be correctly set once the dx_root has been 2991 * allocated. 2992 */ 2993 ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves, 2994 num_dx_leaves, &num_dx_entries, 2995 dirdata_bh); 2996 if (ret) { 2997 mlog_errno(ret); 2998 goto out_commit; 2999 } 3000 } 3001 3002 /* 3003 * Set extent, i_size, etc on the directory. After this, the 3004 * inode should contain the same exact dirents as before and 3005 * be fully accessible from system calls. 3006 * 3007 * We let the later dirent insert modify c/mtime - to the user 3008 * the data hasn't changed. 3009 */ 3010 ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh, 3011 OCFS2_JOURNAL_ACCESS_CREATE); 3012 if (ret) { 3013 mlog_errno(ret); 3014 goto out_commit; 3015 } 3016 3017 spin_lock(&oi->ip_lock); 3018 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL; 3019 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); 3020 spin_unlock(&oi->ip_lock); 3021 3022 ocfs2_dinode_new_extent_list(dir, di); 3023 3024 i_size_write(dir, sb->s_blocksize); 3025 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); 3026 3027 di->i_size = cpu_to_le64(sb->s_blocksize); 3028 di->i_ctime = di->i_mtime = cpu_to_le64(inode_get_ctime_sec(dir)); 3029 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode_get_ctime_nsec(dir)); 3030 ocfs2_update_inode_fsync_trans(handle, dir, 1); 3031 3032 /* 3033 * This should never fail as our extent list is empty and all 3034 * related blocks have been journaled already. 3035 */ 3036 ret = ocfs2_insert_extent(handle, &et, 0, blkno, len, 3037 0, NULL); 3038 if (ret) { 3039 mlog_errno(ret); 3040 goto out_commit; 3041 } 3042 3043 /* 3044 * Set i_blocks after the extent insert for the most up to 3045 * date ip_clusters value. 3046 */ 3047 dir->i_blocks = ocfs2_inode_sector_count(dir); 3048 3049 ocfs2_journal_dirty(handle, di_bh); 3050 3051 if (ocfs2_supports_indexed_dirs(osb)) { 3052 ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh, 3053 dirdata_bh, meta_ac, dx_inline, 3054 num_dx_entries, &dx_root_bh); 3055 if (ret) { 3056 mlog_errno(ret); 3057 goto out_commit; 3058 } 3059 3060 if (dx_inline) { 3061 ocfs2_dx_dir_index_root_block(dir, dx_root_bh, 3062 dirdata_bh); 3063 } else { 3064 ocfs2_init_dx_root_extent_tree(&dx_et, 3065 INODE_CACHE(dir), 3066 dx_root_bh); 3067 ret = ocfs2_insert_extent(handle, &dx_et, 0, 3068 dx_insert_blkno, 1, 0, NULL); 3069 if (ret) 3070 mlog_errno(ret); 3071 } 3072 } 3073 3074 /* 3075 * We asked for two clusters, but only got one in the 1st 3076 * pass. Claim the 2nd cluster as a separate extent. 3077 */ 3078 if (alloc > len) { 3079 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, 3080 &len); 3081 if (ret) { 3082 mlog_errno(ret); 3083 goto out_commit; 3084 } 3085 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off); 3086 3087 ret = ocfs2_insert_extent(handle, &et, 1, 3088 blkno, len, 0, NULL); 3089 if (ret) { 3090 mlog_errno(ret); 3091 goto out_commit; 3092 } 3093 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1); 3094 } 3095 3096 *first_block_bh = dirdata_bh; 3097 dirdata_bh = NULL; 3098 if (ocfs2_supports_indexed_dirs(osb)) { 3099 unsigned int off; 3100 3101 if (!dx_inline) { 3102 /* 3103 * We need to return the correct block within the 3104 * cluster which should hold our entry. 3105 */ 3106 off = ocfs2_dx_dir_hash_idx(osb, 3107 &lookup->dl_hinfo); 3108 get_bh(dx_leaves[off]); 3109 lookup->dl_dx_leaf_bh = dx_leaves[off]; 3110 } 3111 lookup->dl_dx_root_bh = dx_root_bh; 3112 dx_root_bh = NULL; 3113 } 3114 3115 out_commit: 3116 if (ret < 0 && did_quota) 3117 dquot_free_space_nodirty(dir, bytes_allocated); 3118 3119 ocfs2_commit_trans(osb, handle); 3120 3121 out: 3122 up_write(&oi->ip_alloc_sem); 3123 if (data_ac) 3124 ocfs2_free_alloc_context(data_ac); 3125 if (meta_ac) 3126 ocfs2_free_alloc_context(meta_ac); 3127 3128 if (dx_leaves) { 3129 for (i = 0; i < num_dx_leaves; i++) 3130 brelse(dx_leaves[i]); 3131 kfree(dx_leaves); 3132 } 3133 3134 brelse(dirdata_bh); 3135 brelse(dx_root_bh); 3136 3137 return ret; 3138 } 3139 3140 /* returns a bh of the 1st new block in the allocation. */ 3141 static int ocfs2_do_extend_dir(struct super_block *sb, 3142 handle_t *handle, 3143 struct inode *dir, 3144 struct buffer_head *parent_fe_bh, 3145 struct ocfs2_alloc_context *data_ac, 3146 struct ocfs2_alloc_context *meta_ac, 3147 struct buffer_head **new_bh) 3148 { 3149 int status; 3150 int extend, did_quota = 0; 3151 u64 p_blkno, v_blkno; 3152 3153 spin_lock(&OCFS2_I(dir)->ip_lock); 3154 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)); 3155 spin_unlock(&OCFS2_I(dir)->ip_lock); 3156 3157 if (extend) { 3158 u32 offset = OCFS2_I(dir)->ip_clusters; 3159 3160 status = dquot_alloc_space_nodirty(dir, 3161 ocfs2_clusters_to_bytes(sb, 1)); 3162 if (status) 3163 goto bail; 3164 did_quota = 1; 3165 3166 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset, 3167 1, 0, parent_fe_bh, handle, 3168 data_ac, meta_ac, NULL); 3169 BUG_ON(status == -EAGAIN); 3170 if (status < 0) { 3171 mlog_errno(status); 3172 goto bail; 3173 } 3174 } 3175 3176 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir)); 3177 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL); 3178 if (status < 0) { 3179 mlog_errno(status); 3180 goto bail; 3181 } 3182 3183 *new_bh = sb_getblk(sb, p_blkno); 3184 if (!*new_bh) { 3185 status = -ENOMEM; 3186 mlog_errno(status); 3187 goto bail; 3188 } 3189 status = 0; 3190 bail: 3191 if (did_quota && status < 0) 3192 dquot_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1)); 3193 return status; 3194 } 3195 3196 /* 3197 * Assumes you already have a cluster lock on the directory. 3198 * 3199 * 'blocks_wanted' is only used if we have an inline directory which 3200 * is to be turned into an extent based one. The size of the dirent to 3201 * insert might be larger than the space gained by growing to just one 3202 * block, so we may have to grow the inode by two blocks in that case. 3203 * 3204 * If the directory is already indexed, dx_root_bh must be provided. 3205 */ 3206 static int ocfs2_extend_dir(struct ocfs2_super *osb, 3207 struct inode *dir, 3208 struct buffer_head *parent_fe_bh, 3209 unsigned int blocks_wanted, 3210 struct ocfs2_dir_lookup_result *lookup, 3211 struct buffer_head **new_de_bh) 3212 { 3213 int status = 0; 3214 int credits, num_free_extents, drop_alloc_sem = 0; 3215 loff_t dir_i_size; 3216 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data; 3217 struct ocfs2_extent_list *el = &fe->id2.i_list; 3218 struct ocfs2_alloc_context *data_ac = NULL; 3219 struct ocfs2_alloc_context *meta_ac = NULL; 3220 handle_t *handle = NULL; 3221 struct buffer_head *new_bh = NULL; 3222 struct ocfs2_dir_entry * de; 3223 struct super_block *sb = osb->sb; 3224 struct ocfs2_extent_tree et; 3225 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh; 3226 3227 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { 3228 /* 3229 * This would be a code error as an inline directory should 3230 * never have an index root. 3231 */ 3232 BUG_ON(dx_root_bh); 3233 3234 status = ocfs2_expand_inline_dir(dir, parent_fe_bh, 3235 blocks_wanted, lookup, 3236 &new_bh); 3237 if (status) { 3238 mlog_errno(status); 3239 goto bail; 3240 } 3241 3242 /* Expansion from inline to an indexed directory will 3243 * have given us this. */ 3244 dx_root_bh = lookup->dl_dx_root_bh; 3245 3246 if (blocks_wanted == 1) { 3247 /* 3248 * If the new dirent will fit inside the space 3249 * created by pushing out to one block, then 3250 * we can complete the operation 3251 * here. Otherwise we have to expand i_size 3252 * and format the 2nd block below. 3253 */ 3254 BUG_ON(new_bh == NULL); 3255 goto bail_bh; 3256 } 3257 3258 /* 3259 * Get rid of 'new_bh' - we want to format the 2nd 3260 * data block and return that instead. 3261 */ 3262 brelse(new_bh); 3263 new_bh = NULL; 3264 3265 down_write(&OCFS2_I(dir)->ip_alloc_sem); 3266 drop_alloc_sem = 1; 3267 dir_i_size = i_size_read(dir); 3268 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS; 3269 goto do_extend; 3270 } 3271 3272 down_write(&OCFS2_I(dir)->ip_alloc_sem); 3273 drop_alloc_sem = 1; 3274 dir_i_size = i_size_read(dir); 3275 trace_ocfs2_extend_dir((unsigned long long)OCFS2_I(dir)->ip_blkno, 3276 dir_i_size); 3277 3278 /* dir->i_size is always block aligned. */ 3279 spin_lock(&OCFS2_I(dir)->ip_lock); 3280 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) { 3281 spin_unlock(&OCFS2_I(dir)->ip_lock); 3282 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir), 3283 parent_fe_bh); 3284 num_free_extents = ocfs2_num_free_extents(&et); 3285 if (num_free_extents < 0) { 3286 status = num_free_extents; 3287 mlog_errno(status); 3288 goto bail; 3289 } 3290 3291 if (!num_free_extents) { 3292 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac); 3293 if (status < 0) { 3294 if (status != -ENOSPC) 3295 mlog_errno(status); 3296 goto bail; 3297 } 3298 } 3299 3300 status = ocfs2_reserve_clusters(osb, 1, &data_ac); 3301 if (status < 0) { 3302 if (status != -ENOSPC) 3303 mlog_errno(status); 3304 goto bail; 3305 } 3306 3307 if (ocfs2_dir_resv_allowed(osb)) 3308 data_ac->ac_resv = &OCFS2_I(dir)->ip_la_data_resv; 3309 3310 credits = ocfs2_calc_extend_credits(sb, el); 3311 } else { 3312 spin_unlock(&OCFS2_I(dir)->ip_lock); 3313 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS; 3314 } 3315 3316 do_extend: 3317 if (ocfs2_dir_indexed(dir)) 3318 credits++; /* For attaching the new dirent block to the 3319 * dx_root */ 3320 3321 handle = ocfs2_start_trans(osb, credits); 3322 if (IS_ERR(handle)) { 3323 status = PTR_ERR(handle); 3324 handle = NULL; 3325 mlog_errno(status); 3326 goto bail; 3327 } 3328 3329 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh, 3330 data_ac, meta_ac, &new_bh); 3331 if (status < 0) { 3332 mlog_errno(status); 3333 goto bail; 3334 } 3335 3336 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), new_bh); 3337 3338 status = ocfs2_journal_access_db(handle, INODE_CACHE(dir), new_bh, 3339 OCFS2_JOURNAL_ACCESS_CREATE); 3340 if (status < 0) { 3341 mlog_errno(status); 3342 goto bail; 3343 } 3344 memset(new_bh->b_data, 0, sb->s_blocksize); 3345 3346 de = (struct ocfs2_dir_entry *) new_bh->b_data; 3347 de->inode = 0; 3348 if (ocfs2_supports_dir_trailer(dir)) { 3349 de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb)); 3350 3351 ocfs2_init_dir_trailer(dir, new_bh, le16_to_cpu(de->rec_len)); 3352 3353 if (ocfs2_dir_indexed(dir)) { 3354 status = ocfs2_dx_dir_link_trailer(dir, handle, 3355 dx_root_bh, new_bh); 3356 if (status) { 3357 mlog_errno(status); 3358 goto bail; 3359 } 3360 } 3361 } else { 3362 de->rec_len = cpu_to_le16(sb->s_blocksize); 3363 } 3364 ocfs2_update_inode_fsync_trans(handle, dir, 1); 3365 ocfs2_journal_dirty(handle, new_bh); 3366 3367 dir_i_size += dir->i_sb->s_blocksize; 3368 i_size_write(dir, dir_i_size); 3369 dir->i_blocks = ocfs2_inode_sector_count(dir); 3370 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh); 3371 if (status < 0) { 3372 mlog_errno(status); 3373 goto bail; 3374 } 3375 3376 bail_bh: 3377 *new_de_bh = new_bh; 3378 get_bh(*new_de_bh); 3379 bail: 3380 if (handle) 3381 ocfs2_commit_trans(osb, handle); 3382 if (drop_alloc_sem) 3383 up_write(&OCFS2_I(dir)->ip_alloc_sem); 3384 3385 if (data_ac) 3386 ocfs2_free_alloc_context(data_ac); 3387 if (meta_ac) 3388 ocfs2_free_alloc_context(meta_ac); 3389 3390 brelse(new_bh); 3391 3392 return status; 3393 } 3394 3395 static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh, 3396 const char *name, int namelen, 3397 struct buffer_head **ret_de_bh, 3398 unsigned int *blocks_wanted) 3399 { 3400 int ret; 3401 struct super_block *sb = dir->i_sb; 3402 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 3403 struct ocfs2_dir_entry *de, *last_de = NULL; 3404 char *first_de, *de_buf, *limit; 3405 unsigned long offset = 0; 3406 unsigned int rec_len, new_rec_len, free_space; 3407 3408 /* 3409 * This calculates how many free bytes we'd have in block zero, should 3410 * this function force expansion to an extent tree. 3411 */ 3412 if (ocfs2_new_dir_wants_trailer(dir)) 3413 free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir); 3414 else 3415 free_space = dir->i_sb->s_blocksize - i_size_read(dir); 3416 3417 first_de = di->id2.i_data.id_data; 3418 de_buf = first_de; 3419 limit = de_buf + i_size_read(dir); 3420 rec_len = OCFS2_DIR_REC_LEN(namelen); 3421 3422 while (de_buf < limit) { 3423 de = (struct ocfs2_dir_entry *)de_buf; 3424 3425 if (!ocfs2_check_dir_entry(dir, de, di_bh, first_de, 3426 i_size_read(dir), offset)) { 3427 ret = -ENOENT; 3428 goto out; 3429 } 3430 if (ocfs2_match(namelen, name, de)) { 3431 ret = -EEXIST; 3432 goto out; 3433 } 3434 /* 3435 * No need to check for a trailing dirent record here as 3436 * they're not used for inline dirs. 3437 */ 3438 3439 if (ocfs2_dirent_would_fit(de, rec_len)) { 3440 /* Ok, we found a spot. Return this bh and let 3441 * the caller actually fill it in. */ 3442 *ret_de_bh = di_bh; 3443 get_bh(*ret_de_bh); 3444 ret = 0; 3445 goto out; 3446 } 3447 3448 last_de = de; 3449 de_buf += le16_to_cpu(de->rec_len); 3450 offset += le16_to_cpu(de->rec_len); 3451 } 3452 3453 if (!last_de) { 3454 ret = ocfs2_error(sb, "Directory entry (#%llu: size=%lld) " 3455 "is unexpectedly short", 3456 (unsigned long long)OCFS2_I(dir)->ip_blkno, 3457 i_size_read(dir)); 3458 goto out; 3459 } 3460 3461 /* 3462 * We're going to require expansion of the directory - figure 3463 * out how many blocks we'll need so that a place for the 3464 * dirent can be found. 3465 */ 3466 *blocks_wanted = 1; 3467 new_rec_len = le16_to_cpu(last_de->rec_len) + free_space; 3468 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len))) 3469 *blocks_wanted = 2; 3470 3471 ret = -ENOSPC; 3472 out: 3473 return ret; 3474 } 3475 3476 static int ocfs2_find_dir_space_el(struct inode *dir, const char *name, 3477 int namelen, struct buffer_head **ret_de_bh) 3478 { 3479 unsigned long offset; 3480 struct buffer_head *bh = NULL; 3481 unsigned short rec_len; 3482 struct ocfs2_dir_entry *de; 3483 struct super_block *sb = dir->i_sb; 3484 int status; 3485 int blocksize = dir->i_sb->s_blocksize; 3486 3487 status = ocfs2_read_dir_block(dir, 0, &bh, 0); 3488 if (status) 3489 goto bail; 3490 3491 rec_len = OCFS2_DIR_REC_LEN(namelen); 3492 offset = 0; 3493 de = (struct ocfs2_dir_entry *) bh->b_data; 3494 while (1) { 3495 if ((char *)de >= sb->s_blocksize + bh->b_data) { 3496 brelse(bh); 3497 bh = NULL; 3498 3499 if (i_size_read(dir) <= offset) { 3500 /* 3501 * Caller will have to expand this 3502 * directory. 3503 */ 3504 status = -ENOSPC; 3505 goto bail; 3506 } 3507 status = ocfs2_read_dir_block(dir, 3508 offset >> sb->s_blocksize_bits, 3509 &bh, 0); 3510 if (status) 3511 goto bail; 3512 3513 /* move to next block */ 3514 de = (struct ocfs2_dir_entry *) bh->b_data; 3515 } 3516 if (!ocfs2_check_dir_entry(dir, de, bh, bh->b_data, blocksize, 3517 offset)) { 3518 status = -ENOENT; 3519 goto bail; 3520 } 3521 if (ocfs2_match(namelen, name, de)) { 3522 status = -EEXIST; 3523 goto bail; 3524 } 3525 3526 if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize, 3527 blocksize)) 3528 goto next; 3529 3530 if (ocfs2_dirent_would_fit(de, rec_len)) { 3531 /* Ok, we found a spot. Return this bh and let 3532 * the caller actually fill it in. */ 3533 *ret_de_bh = bh; 3534 get_bh(*ret_de_bh); 3535 status = 0; 3536 goto bail; 3537 } 3538 next: 3539 offset += le16_to_cpu(de->rec_len); 3540 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len)); 3541 } 3542 3543 bail: 3544 brelse(bh); 3545 if (status) 3546 mlog_errno(status); 3547 3548 return status; 3549 } 3550 3551 static int dx_leaf_sort_cmp(const void *a, const void *b) 3552 { 3553 const struct ocfs2_dx_entry *entry1 = a; 3554 const struct ocfs2_dx_entry *entry2 = b; 3555 u32 major_hash1 = le32_to_cpu(entry1->dx_major_hash); 3556 u32 major_hash2 = le32_to_cpu(entry2->dx_major_hash); 3557 u32 minor_hash1 = le32_to_cpu(entry1->dx_minor_hash); 3558 u32 minor_hash2 = le32_to_cpu(entry2->dx_minor_hash); 3559 3560 if (major_hash1 > major_hash2) 3561 return 1; 3562 if (major_hash1 < major_hash2) 3563 return -1; 3564 3565 /* 3566 * It is not strictly necessary to sort by minor 3567 */ 3568 if (minor_hash1 > minor_hash2) 3569 return 1; 3570 if (minor_hash1 < minor_hash2) 3571 return -1; 3572 return 0; 3573 } 3574 3575 static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf) 3576 { 3577 struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list; 3578 int i, num = le16_to_cpu(dl_list->de_num_used); 3579 3580 for (i = 0; i < (num - 1); i++) { 3581 if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) != 3582 le32_to_cpu(dl_list->de_entries[i + 1].dx_major_hash)) 3583 return 0; 3584 } 3585 3586 return 1; 3587 } 3588 3589 /* 3590 * Find the optimal value to split this leaf on. This expects the leaf 3591 * entries to be in sorted order. 3592 * 3593 * leaf_cpos is the cpos of the leaf we're splitting. insert_hash is 3594 * the hash we want to insert. 3595 * 3596 * This function is only concerned with the major hash - that which 3597 * determines which cluster an item belongs to. 3598 */ 3599 static int ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf *dx_leaf, 3600 u32 leaf_cpos, u32 insert_hash, 3601 u32 *split_hash) 3602 { 3603 struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list; 3604 int i, num_used = le16_to_cpu(dl_list->de_num_used); 3605 int allsame; 3606 3607 /* 3608 * There's a couple rare, but nasty corner cases we have to 3609 * check for here. All of them involve a leaf where all value 3610 * have the same hash, which is what we look for first. 3611 * 3612 * Most of the time, all of the above is false, and we simply 3613 * pick the median value for a split. 3614 */ 3615 allsame = ocfs2_dx_leaf_same_major(dx_leaf); 3616 if (allsame) { 3617 u32 val = le32_to_cpu(dl_list->de_entries[0].dx_major_hash); 3618 3619 if (val == insert_hash) { 3620 /* 3621 * No matter where we would choose to split, 3622 * the new entry would want to occupy the same 3623 * block as these. Since there's no space left 3624 * in their existing block, we know there 3625 * won't be space after the split. 3626 */ 3627 return -ENOSPC; 3628 } 3629 3630 if (val == leaf_cpos) { 3631 /* 3632 * Because val is the same as leaf_cpos (which 3633 * is the smallest value this leaf can have), 3634 * yet is not equal to insert_hash, then we 3635 * know that insert_hash *must* be larger than 3636 * val (and leaf_cpos). At least cpos+1 in value. 3637 * 3638 * We also know then, that there cannot be an 3639 * adjacent extent (otherwise we'd be looking 3640 * at it). Choosing this value gives us a 3641 * chance to get some contiguousness. 3642 */ 3643 *split_hash = leaf_cpos + 1; 3644 return 0; 3645 } 3646 3647 if (val > insert_hash) { 3648 /* 3649 * val can not be the same as insert hash, and 3650 * also must be larger than leaf_cpos. Also, 3651 * we know that there can't be a leaf between 3652 * cpos and val, otherwise the entries with 3653 * hash 'val' would be there. 3654 */ 3655 *split_hash = val; 3656 return 0; 3657 } 3658 3659 *split_hash = insert_hash; 3660 return 0; 3661 } 3662 3663 /* 3664 * Since the records are sorted and the checks above 3665 * guaranteed that not all records in this block are the same, 3666 * we simple travel forward, from the median, and pick the 1st 3667 * record whose value is larger than leaf_cpos. 3668 */ 3669 for (i = (num_used / 2); i < num_used; i++) 3670 if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) > 3671 leaf_cpos) 3672 break; 3673 3674 BUG_ON(i == num_used); /* Should be impossible */ 3675 *split_hash = le32_to_cpu(dl_list->de_entries[i].dx_major_hash); 3676 return 0; 3677 } 3678 3679 /* 3680 * Transfer all entries in orig_dx_leaves whose major hash is equal to or 3681 * larger than split_hash into new_dx_leaves. We use a temporary 3682 * buffer (tmp_dx_leaf) to make the changes to the original leaf blocks. 3683 * 3684 * Since the block offset inside a leaf (cluster) is a constant mask 3685 * of minor_hash, we can optimize - an item at block offset X within 3686 * the original cluster, will be at offset X within the new cluster. 3687 */ 3688 static void ocfs2_dx_dir_transfer_leaf(struct inode *dir, u32 split_hash, 3689 handle_t *handle, 3690 struct ocfs2_dx_leaf *tmp_dx_leaf, 3691 struct buffer_head **orig_dx_leaves, 3692 struct buffer_head **new_dx_leaves, 3693 int num_dx_leaves) 3694 { 3695 int i, j, num_used; 3696 u32 major_hash; 3697 struct ocfs2_dx_leaf *orig_dx_leaf, *new_dx_leaf; 3698 struct ocfs2_dx_entry_list *orig_list, *tmp_list; 3699 struct ocfs2_dx_entry *dx_entry; 3700 3701 tmp_list = &tmp_dx_leaf->dl_list; 3702 3703 for (i = 0; i < num_dx_leaves; i++) { 3704 orig_dx_leaf = (struct ocfs2_dx_leaf *) orig_dx_leaves[i]->b_data; 3705 orig_list = &orig_dx_leaf->dl_list; 3706 new_dx_leaf = (struct ocfs2_dx_leaf *) new_dx_leaves[i]->b_data; 3707 3708 num_used = le16_to_cpu(orig_list->de_num_used); 3709 3710 memcpy(tmp_dx_leaf, orig_dx_leaf, dir->i_sb->s_blocksize); 3711 tmp_list->de_num_used = cpu_to_le16(0); 3712 memset(&tmp_list->de_entries, 0, sizeof(*dx_entry)*num_used); 3713 3714 for (j = 0; j < num_used; j++) { 3715 dx_entry = &orig_list->de_entries[j]; 3716 major_hash = le32_to_cpu(dx_entry->dx_major_hash); 3717 if (major_hash >= split_hash) 3718 ocfs2_dx_dir_leaf_insert_tail(new_dx_leaf, 3719 dx_entry); 3720 else 3721 ocfs2_dx_dir_leaf_insert_tail(tmp_dx_leaf, 3722 dx_entry); 3723 } 3724 memcpy(orig_dx_leaf, tmp_dx_leaf, dir->i_sb->s_blocksize); 3725 3726 ocfs2_journal_dirty(handle, orig_dx_leaves[i]); 3727 ocfs2_journal_dirty(handle, new_dx_leaves[i]); 3728 } 3729 } 3730 3731 static int ocfs2_dx_dir_rebalance_credits(struct ocfs2_super *osb, 3732 struct ocfs2_dx_root_block *dx_root) 3733 { 3734 int credits = ocfs2_clusters_to_blocks(osb->sb, 3); 3735 3736 credits += ocfs2_calc_extend_credits(osb->sb, &dx_root->dr_list); 3737 credits += ocfs2_quota_trans_credits(osb->sb); 3738 return credits; 3739 } 3740 3741 /* 3742 * Find the median value in dx_leaf_bh and allocate a new leaf to move 3743 * half our entries into. 3744 */ 3745 static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir, 3746 struct buffer_head *dx_root_bh, 3747 struct buffer_head *dx_leaf_bh, 3748 struct ocfs2_dx_hinfo *hinfo, u32 leaf_cpos, 3749 u64 leaf_blkno) 3750 { 3751 struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data; 3752 int credits, ret, i, num_used, did_quota = 0; 3753 u32 cpos, split_hash, insert_hash = hinfo->major_hash; 3754 u64 orig_leaves_start; 3755 int num_dx_leaves; 3756 struct buffer_head **orig_dx_leaves = NULL; 3757 struct buffer_head **new_dx_leaves = NULL; 3758 struct ocfs2_alloc_context *data_ac = NULL, *meta_ac = NULL; 3759 struct ocfs2_extent_tree et; 3760 handle_t *handle = NULL; 3761 struct ocfs2_dx_root_block *dx_root; 3762 struct ocfs2_dx_leaf *tmp_dx_leaf = NULL; 3763 3764 trace_ocfs2_dx_dir_rebalance((unsigned long long)OCFS2_I(dir)->ip_blkno, 3765 (unsigned long long)leaf_blkno, 3766 insert_hash); 3767 3768 ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh); 3769 3770 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; 3771 /* 3772 * XXX: This is a rather large limit. We should use a more 3773 * realistic value. 3774 */ 3775 if (le32_to_cpu(dx_root->dr_clusters) == UINT_MAX) 3776 return -ENOSPC; 3777 3778 num_used = le16_to_cpu(dx_leaf->dl_list.de_num_used); 3779 if (num_used < le16_to_cpu(dx_leaf->dl_list.de_count)) { 3780 mlog(ML_ERROR, "DX Dir: %llu, Asked to rebalance empty leaf: " 3781 "%llu, %d\n", (unsigned long long)OCFS2_I(dir)->ip_blkno, 3782 (unsigned long long)leaf_blkno, num_used); 3783 ret = -EIO; 3784 goto out; 3785 } 3786 3787 orig_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves); 3788 if (!orig_dx_leaves) { 3789 ret = -ENOMEM; 3790 mlog_errno(ret); 3791 goto out; 3792 } 3793 3794 new_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, NULL); 3795 if (!new_dx_leaves) { 3796 ret = -ENOMEM; 3797 mlog_errno(ret); 3798 goto out; 3799 } 3800 3801 ret = ocfs2_lock_allocators(dir, &et, 1, 0, &data_ac, &meta_ac); 3802 if (ret) { 3803 if (ret != -ENOSPC) 3804 mlog_errno(ret); 3805 goto out; 3806 } 3807 3808 credits = ocfs2_dx_dir_rebalance_credits(osb, dx_root); 3809 handle = ocfs2_start_trans(osb, credits); 3810 if (IS_ERR(handle)) { 3811 ret = PTR_ERR(handle); 3812 handle = NULL; 3813 mlog_errno(ret); 3814 goto out; 3815 } 3816 3817 ret = dquot_alloc_space_nodirty(dir, 3818 ocfs2_clusters_to_bytes(dir->i_sb, 1)); 3819 if (ret) 3820 goto out_commit; 3821 did_quota = 1; 3822 3823 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh, 3824 OCFS2_JOURNAL_ACCESS_WRITE); 3825 if (ret) { 3826 mlog_errno(ret); 3827 goto out_commit; 3828 } 3829 3830 /* 3831 * This block is changing anyway, so we can sort it in place. 3832 */ 3833 sort(dx_leaf->dl_list.de_entries, num_used, 3834 sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp, 3835 NULL); 3836 3837 ocfs2_journal_dirty(handle, dx_leaf_bh); 3838 3839 ret = ocfs2_dx_dir_find_leaf_split(dx_leaf, leaf_cpos, insert_hash, 3840 &split_hash); 3841 if (ret) { 3842 mlog_errno(ret); 3843 goto out_commit; 3844 } 3845 3846 trace_ocfs2_dx_dir_rebalance_split(leaf_cpos, split_hash, insert_hash); 3847 3848 /* 3849 * We have to carefully order operations here. There are items 3850 * which want to be in the new cluster before insert, but in 3851 * order to put those items in the new cluster, we alter the 3852 * old cluster. A failure to insert gets nasty. 3853 * 3854 * So, start by reserving writes to the old 3855 * cluster. ocfs2_dx_dir_new_cluster will reserve writes on 3856 * the new cluster for us, before inserting it. The insert 3857 * won't happen if there's an error before that. Once the 3858 * insert is done then, we can transfer from one leaf into the 3859 * other without fear of hitting any error. 3860 */ 3861 3862 /* 3863 * The leaf transfer wants some scratch space so that we don't 3864 * wind up doing a bunch of expensive memmove(). 3865 */ 3866 tmp_dx_leaf = kmalloc(osb->sb->s_blocksize, GFP_NOFS); 3867 if (!tmp_dx_leaf) { 3868 ret = -ENOMEM; 3869 mlog_errno(ret); 3870 goto out_commit; 3871 } 3872 3873 orig_leaves_start = ocfs2_block_to_cluster_start(dir->i_sb, leaf_blkno); 3874 ret = ocfs2_read_dx_leaves(dir, orig_leaves_start, num_dx_leaves, 3875 orig_dx_leaves); 3876 if (ret) { 3877 mlog_errno(ret); 3878 goto out_commit; 3879 } 3880 3881 cpos = split_hash; 3882 ret = ocfs2_dx_dir_new_cluster(dir, &et, cpos, handle, 3883 data_ac, meta_ac, new_dx_leaves, 3884 num_dx_leaves); 3885 if (ret) { 3886 mlog_errno(ret); 3887 goto out_commit; 3888 } 3889 3890 for (i = 0; i < num_dx_leaves; i++) { 3891 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), 3892 orig_dx_leaves[i], 3893 OCFS2_JOURNAL_ACCESS_WRITE); 3894 if (ret) { 3895 mlog_errno(ret); 3896 goto out_commit; 3897 } 3898 3899 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), 3900 new_dx_leaves[i], 3901 OCFS2_JOURNAL_ACCESS_WRITE); 3902 if (ret) { 3903 mlog_errno(ret); 3904 goto out_commit; 3905 } 3906 } 3907 3908 ocfs2_dx_dir_transfer_leaf(dir, split_hash, handle, tmp_dx_leaf, 3909 orig_dx_leaves, new_dx_leaves, num_dx_leaves); 3910 3911 out_commit: 3912 if (ret < 0 && did_quota) 3913 dquot_free_space_nodirty(dir, 3914 ocfs2_clusters_to_bytes(dir->i_sb, 1)); 3915 3916 ocfs2_update_inode_fsync_trans(handle, dir, 1); 3917 ocfs2_commit_trans(osb, handle); 3918 3919 out: 3920 if (orig_dx_leaves || new_dx_leaves) { 3921 for (i = 0; i < num_dx_leaves; i++) { 3922 if (orig_dx_leaves) 3923 brelse(orig_dx_leaves[i]); 3924 if (new_dx_leaves) 3925 brelse(new_dx_leaves[i]); 3926 } 3927 kfree(orig_dx_leaves); 3928 kfree(new_dx_leaves); 3929 } 3930 3931 if (meta_ac) 3932 ocfs2_free_alloc_context(meta_ac); 3933 if (data_ac) 3934 ocfs2_free_alloc_context(data_ac); 3935 3936 kfree(tmp_dx_leaf); 3937 return ret; 3938 } 3939 3940 static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir, 3941 struct buffer_head *di_bh, 3942 struct buffer_head *dx_root_bh, 3943 const char *name, int namelen, 3944 struct ocfs2_dir_lookup_result *lookup) 3945 { 3946 int ret, rebalanced = 0; 3947 struct ocfs2_dx_root_block *dx_root; 3948 struct buffer_head *dx_leaf_bh = NULL; 3949 struct ocfs2_dx_leaf *dx_leaf; 3950 u64 blkno; 3951 u32 leaf_cpos; 3952 3953 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; 3954 3955 restart_search: 3956 ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo, 3957 &leaf_cpos, &blkno); 3958 if (ret) { 3959 mlog_errno(ret); 3960 goto out; 3961 } 3962 3963 ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh); 3964 if (ret) { 3965 mlog_errno(ret); 3966 goto out; 3967 } 3968 3969 dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data; 3970 3971 if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >= 3972 le16_to_cpu(dx_leaf->dl_list.de_count)) { 3973 if (rebalanced) { 3974 /* 3975 * Rebalancing should have provided us with 3976 * space in an appropriate leaf. 3977 * 3978 * XXX: Is this an abnormal condition then? 3979 * Should we print a message here? 3980 */ 3981 ret = -ENOSPC; 3982 goto out; 3983 } 3984 3985 ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh, 3986 &lookup->dl_hinfo, leaf_cpos, 3987 blkno); 3988 if (ret) { 3989 if (ret != -ENOSPC) 3990 mlog_errno(ret); 3991 goto out; 3992 } 3993 3994 /* 3995 * Restart the lookup. The rebalance might have 3996 * changed which block our item fits into. Mark our 3997 * progress, so we only execute this once. 3998 */ 3999 brelse(dx_leaf_bh); 4000 dx_leaf_bh = NULL; 4001 rebalanced = 1; 4002 goto restart_search; 4003 } 4004 4005 lookup->dl_dx_leaf_bh = dx_leaf_bh; 4006 dx_leaf_bh = NULL; 4007 4008 out: 4009 brelse(dx_leaf_bh); 4010 return ret; 4011 } 4012 4013 static int ocfs2_search_dx_free_list(struct inode *dir, 4014 struct buffer_head *dx_root_bh, 4015 int namelen, 4016 struct ocfs2_dir_lookup_result *lookup) 4017 { 4018 int ret = -ENOSPC; 4019 struct buffer_head *leaf_bh = NULL, *prev_leaf_bh = NULL; 4020 struct ocfs2_dir_block_trailer *db; 4021 u64 next_block; 4022 int rec_len = OCFS2_DIR_REC_LEN(namelen); 4023 struct ocfs2_dx_root_block *dx_root; 4024 4025 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; 4026 next_block = le64_to_cpu(dx_root->dr_free_blk); 4027 4028 while (next_block) { 4029 brelse(prev_leaf_bh); 4030 prev_leaf_bh = leaf_bh; 4031 leaf_bh = NULL; 4032 4033 ret = ocfs2_read_dir_block_direct(dir, next_block, &leaf_bh); 4034 if (ret) { 4035 mlog_errno(ret); 4036 goto out; 4037 } 4038 4039 db = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb); 4040 if (rec_len <= le16_to_cpu(db->db_free_rec_len)) { 4041 lookup->dl_leaf_bh = leaf_bh; 4042 lookup->dl_prev_leaf_bh = prev_leaf_bh; 4043 leaf_bh = NULL; 4044 prev_leaf_bh = NULL; 4045 break; 4046 } 4047 4048 next_block = le64_to_cpu(db->db_free_next); 4049 } 4050 4051 if (!next_block) 4052 ret = -ENOSPC; 4053 4054 out: 4055 4056 brelse(leaf_bh); 4057 brelse(prev_leaf_bh); 4058 return ret; 4059 } 4060 4061 static int ocfs2_expand_inline_dx_root(struct inode *dir, 4062 struct buffer_head *dx_root_bh) 4063 { 4064 int ret, num_dx_leaves, i, j, did_quota = 0; 4065 struct buffer_head **dx_leaves = NULL; 4066 struct ocfs2_extent_tree et; 4067 u64 insert_blkno; 4068 struct ocfs2_alloc_context *data_ac = NULL; 4069 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); 4070 handle_t *handle = NULL; 4071 struct ocfs2_dx_root_block *dx_root; 4072 struct ocfs2_dx_entry_list *entry_list; 4073 struct ocfs2_dx_entry *dx_entry; 4074 struct ocfs2_dx_leaf *target_leaf; 4075 4076 ret = ocfs2_reserve_clusters(osb, 1, &data_ac); 4077 if (ret) { 4078 mlog_errno(ret); 4079 goto out; 4080 } 4081 4082 dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves); 4083 if (!dx_leaves) { 4084 ret = -ENOMEM; 4085 mlog_errno(ret); 4086 goto out; 4087 } 4088 4089 handle = ocfs2_start_trans(osb, ocfs2_calc_dxi_expand_credits(osb->sb)); 4090 if (IS_ERR(handle)) { 4091 ret = PTR_ERR(handle); 4092 mlog_errno(ret); 4093 goto out; 4094 } 4095 4096 ret = dquot_alloc_space_nodirty(dir, 4097 ocfs2_clusters_to_bytes(osb->sb, 1)); 4098 if (ret) 4099 goto out_commit; 4100 did_quota = 1; 4101 4102 /* 4103 * We do this up front, before the allocation, so that a 4104 * failure to add the dx_root_bh to the journal won't result 4105 * us losing clusters. 4106 */ 4107 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh, 4108 OCFS2_JOURNAL_ACCESS_WRITE); 4109 if (ret) { 4110 mlog_errno(ret); 4111 goto out_commit; 4112 } 4113 4114 ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, dx_leaves, 4115 num_dx_leaves, &insert_blkno); 4116 if (ret) { 4117 mlog_errno(ret); 4118 goto out_commit; 4119 } 4120 4121 /* 4122 * Transfer the entries from our dx_root into the appropriate 4123 * block 4124 */ 4125 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data; 4126 entry_list = &dx_root->dr_entries; 4127 4128 for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) { 4129 dx_entry = &entry_list->de_entries[i]; 4130 4131 j = __ocfs2_dx_dir_hash_idx(osb, 4132 le32_to_cpu(dx_entry->dx_minor_hash)); 4133 target_leaf = (struct ocfs2_dx_leaf *)dx_leaves[j]->b_data; 4134 4135 ocfs2_dx_dir_leaf_insert_tail(target_leaf, dx_entry); 4136 4137 /* Each leaf has been passed to the journal already 4138 * via __ocfs2_dx_dir_new_cluster() */ 4139 } 4140 4141 dx_root->dr_flags &= ~OCFS2_DX_FLAG_INLINE; 4142 4143 dx_root->dr_list.l_tree_depth = 0; 4144 dx_root->dr_list.l_count = 4145 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb)); 4146 dx_root->dr_list.l_next_free_rec = 0; 4147 memset(&dx_root->dr_list.l_recs, 0, 4148 osb->sb->s_blocksize - 4149 (offsetof(struct ocfs2_dx_root_block, dr_list) + 4150 offsetof(struct ocfs2_extent_list, l_recs))); 4151 4152 /* This should never fail considering we start with an empty 4153 * dx_root. */ 4154 ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh); 4155 ret = ocfs2_insert_extent(handle, &et, 0, insert_blkno, 1, 0, NULL); 4156 if (ret) 4157 mlog_errno(ret); 4158 did_quota = 0; 4159 4160 ocfs2_update_inode_fsync_trans(handle, dir, 1); 4161 ocfs2_journal_dirty(handle, dx_root_bh); 4162 4163 out_commit: 4164 if (ret < 0 && did_quota) 4165 dquot_free_space_nodirty(dir, 4166 ocfs2_clusters_to_bytes(dir->i_sb, 1)); 4167 4168 ocfs2_commit_trans(osb, handle); 4169 4170 out: 4171 if (data_ac) 4172 ocfs2_free_alloc_context(data_ac); 4173 4174 if (dx_leaves) { 4175 for (i = 0; i < num_dx_leaves; i++) 4176 brelse(dx_leaves[i]); 4177 kfree(dx_leaves); 4178 } 4179 return ret; 4180 } 4181 4182 static int ocfs2_inline_dx_has_space(struct buffer_head *dx_root_bh) 4183 { 4184 struct ocfs2_dx_root_block *dx_root; 4185 struct ocfs2_dx_entry_list *entry_list; 4186 4187 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data; 4188 entry_list = &dx_root->dr_entries; 4189 4190 if (le16_to_cpu(entry_list->de_num_used) >= 4191 le16_to_cpu(entry_list->de_count)) 4192 return -ENOSPC; 4193 4194 return 0; 4195 } 4196 4197 static int ocfs2_prepare_dx_dir_for_insert(struct inode *dir, 4198 struct buffer_head *di_bh, 4199 const char *name, 4200 int namelen, 4201 struct ocfs2_dir_lookup_result *lookup) 4202 { 4203 int ret, free_dx_root = 1; 4204 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); 4205 struct buffer_head *dx_root_bh = NULL; 4206 struct buffer_head *leaf_bh = NULL; 4207 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 4208 struct ocfs2_dx_root_block *dx_root; 4209 4210 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh); 4211 if (ret) { 4212 mlog_errno(ret); 4213 goto out; 4214 } 4215 4216 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; 4217 if (le32_to_cpu(dx_root->dr_num_entries) == OCFS2_DX_ENTRIES_MAX) { 4218 ret = -ENOSPC; 4219 mlog_errno(ret); 4220 goto out; 4221 } 4222 4223 if (ocfs2_dx_root_inline(dx_root)) { 4224 ret = ocfs2_inline_dx_has_space(dx_root_bh); 4225 4226 if (ret == 0) 4227 goto search_el; 4228 4229 /* 4230 * We ran out of room in the root block. Expand it to 4231 * an extent, then allow ocfs2_find_dir_space_dx to do 4232 * the rest. 4233 */ 4234 ret = ocfs2_expand_inline_dx_root(dir, dx_root_bh); 4235 if (ret) { 4236 mlog_errno(ret); 4237 goto out; 4238 } 4239 } 4240 4241 /* 4242 * Insert preparation for an indexed directory is split into two 4243 * steps. The call to find_dir_space_dx reserves room in the index for 4244 * an additional item. If we run out of space there, it's a real error 4245 * we can't continue on. 4246 */ 4247 ret = ocfs2_find_dir_space_dx(osb, dir, di_bh, dx_root_bh, name, 4248 namelen, lookup); 4249 if (ret) { 4250 mlog_errno(ret); 4251 goto out; 4252 } 4253 4254 search_el: 4255 /* 4256 * Next, we need to find space in the unindexed tree. This call 4257 * searches using the free space linked list. If the unindexed tree 4258 * lacks sufficient space, we'll expand it below. The expansion code 4259 * is smart enough to add any new blocks to the free space list. 4260 */ 4261 ret = ocfs2_search_dx_free_list(dir, dx_root_bh, namelen, lookup); 4262 if (ret && ret != -ENOSPC) { 4263 mlog_errno(ret); 4264 goto out; 4265 } 4266 4267 /* Do this up here - ocfs2_extend_dir might need the dx_root */ 4268 lookup->dl_dx_root_bh = dx_root_bh; 4269 free_dx_root = 0; 4270 4271 if (ret == -ENOSPC) { 4272 ret = ocfs2_extend_dir(osb, dir, di_bh, 1, lookup, &leaf_bh); 4273 4274 if (ret) { 4275 mlog_errno(ret); 4276 goto out; 4277 } 4278 4279 /* 4280 * We make the assumption here that new leaf blocks are added 4281 * to the front of our free list. 4282 */ 4283 lookup->dl_prev_leaf_bh = NULL; 4284 lookup->dl_leaf_bh = leaf_bh; 4285 } 4286 4287 out: 4288 if (free_dx_root) 4289 brelse(dx_root_bh); 4290 return ret; 4291 } 4292 4293 /* 4294 * Get a directory ready for insert. Any directory allocation required 4295 * happens here. Success returns zero, and enough context in the dir 4296 * lookup result that ocfs2_add_entry() will be able complete the task 4297 * with minimal performance impact. 4298 */ 4299 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb, 4300 struct inode *dir, 4301 struct buffer_head *parent_fe_bh, 4302 const char *name, 4303 int namelen, 4304 struct ocfs2_dir_lookup_result *lookup) 4305 { 4306 int ret; 4307 unsigned int blocks_wanted = 1; 4308 struct buffer_head *bh = NULL; 4309 4310 trace_ocfs2_prepare_dir_for_insert( 4311 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen); 4312 4313 /* 4314 * Do this up front to reduce confusion. 4315 * 4316 * The directory might start inline, then be turned into an 4317 * indexed one, in which case we'd need to hash deep inside 4318 * ocfs2_find_dir_space_id(). Since 4319 * ocfs2_prepare_dx_dir_for_insert() also needs this hash 4320 * done, there seems no point in spreading out the calls. We 4321 * can optimize away the case where the file system doesn't 4322 * support indexing. 4323 */ 4324 if (ocfs2_supports_indexed_dirs(osb)) 4325 ocfs2_dx_dir_name_hash(dir, name, namelen, &lookup->dl_hinfo); 4326 4327 if (ocfs2_dir_indexed(dir)) { 4328 ret = ocfs2_prepare_dx_dir_for_insert(dir, parent_fe_bh, 4329 name, namelen, lookup); 4330 if (ret) 4331 mlog_errno(ret); 4332 goto out; 4333 } 4334 4335 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { 4336 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name, 4337 namelen, &bh, &blocks_wanted); 4338 } else 4339 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh); 4340 4341 if (ret && ret != -ENOSPC) { 4342 mlog_errno(ret); 4343 goto out; 4344 } 4345 4346 if (ret == -ENOSPC) { 4347 /* 4348 * We have to expand the directory to add this name. 4349 */ 4350 BUG_ON(bh); 4351 4352 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted, 4353 lookup, &bh); 4354 if (ret) { 4355 if (ret != -ENOSPC) 4356 mlog_errno(ret); 4357 goto out; 4358 } 4359 4360 BUG_ON(!bh); 4361 } 4362 4363 lookup->dl_leaf_bh = bh; 4364 bh = NULL; 4365 out: 4366 brelse(bh); 4367 return ret; 4368 } 4369 4370 static int ocfs2_dx_dir_remove_index(struct inode *dir, 4371 struct buffer_head *di_bh, 4372 struct buffer_head *dx_root_bh) 4373 { 4374 int ret; 4375 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); 4376 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 4377 struct ocfs2_dx_root_block *dx_root; 4378 struct inode *dx_alloc_inode = NULL; 4379 struct buffer_head *dx_alloc_bh = NULL; 4380 handle_t *handle; 4381 u64 blk; 4382 u16 bit; 4383 u64 bg_blkno; 4384 4385 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data; 4386 4387 dx_alloc_inode = ocfs2_get_system_file_inode(osb, 4388 EXTENT_ALLOC_SYSTEM_INODE, 4389 le16_to_cpu(dx_root->dr_suballoc_slot)); 4390 if (!dx_alloc_inode) { 4391 ret = -ENOMEM; 4392 mlog_errno(ret); 4393 goto out; 4394 } 4395 inode_lock(dx_alloc_inode); 4396 4397 ret = ocfs2_inode_lock(dx_alloc_inode, &dx_alloc_bh, 1); 4398 if (ret) { 4399 mlog_errno(ret); 4400 goto out_mutex; 4401 } 4402 4403 handle = ocfs2_start_trans(osb, OCFS2_DX_ROOT_REMOVE_CREDITS); 4404 if (IS_ERR(handle)) { 4405 ret = PTR_ERR(handle); 4406 mlog_errno(ret); 4407 goto out_unlock; 4408 } 4409 4410 ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh, 4411 OCFS2_JOURNAL_ACCESS_WRITE); 4412 if (ret) { 4413 mlog_errno(ret); 4414 goto out_commit; 4415 } 4416 4417 spin_lock(&OCFS2_I(dir)->ip_lock); 4418 OCFS2_I(dir)->ip_dyn_features &= ~OCFS2_INDEXED_DIR_FL; 4419 di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features); 4420 spin_unlock(&OCFS2_I(dir)->ip_lock); 4421 di->i_dx_root = cpu_to_le64(0ULL); 4422 ocfs2_update_inode_fsync_trans(handle, dir, 1); 4423 4424 ocfs2_journal_dirty(handle, di_bh); 4425 4426 blk = le64_to_cpu(dx_root->dr_blkno); 4427 bit = le16_to_cpu(dx_root->dr_suballoc_bit); 4428 if (dx_root->dr_suballoc_loc) 4429 bg_blkno = le64_to_cpu(dx_root->dr_suballoc_loc); 4430 else 4431 bg_blkno = ocfs2_which_suballoc_group(blk, bit); 4432 ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh, 4433 bit, bg_blkno, 1); 4434 if (ret) 4435 mlog_errno(ret); 4436 4437 out_commit: 4438 ocfs2_commit_trans(osb, handle); 4439 4440 out_unlock: 4441 ocfs2_inode_unlock(dx_alloc_inode, 1); 4442 4443 out_mutex: 4444 inode_unlock(dx_alloc_inode); 4445 brelse(dx_alloc_bh); 4446 out: 4447 iput(dx_alloc_inode); 4448 return ret; 4449 } 4450 4451 int ocfs2_dx_dir_truncate(struct inode *dir, struct buffer_head *di_bh) 4452 { 4453 int ret; 4454 unsigned int clen; 4455 u32 major_hash = UINT_MAX, p_cpos, cpos; 4456 u64 blkno; 4457 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); 4458 struct buffer_head *dx_root_bh = NULL; 4459 struct ocfs2_dx_root_block *dx_root; 4460 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 4461 struct ocfs2_cached_dealloc_ctxt dealloc; 4462 struct ocfs2_extent_tree et; 4463 4464 ocfs2_init_dealloc_ctxt(&dealloc); 4465 4466 if (!ocfs2_dir_indexed(dir)) 4467 return 0; 4468 4469 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh); 4470 if (ret) { 4471 mlog_errno(ret); 4472 goto out; 4473 } 4474 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; 4475 4476 if (ocfs2_dx_root_inline(dx_root)) 4477 goto remove_index; 4478 4479 ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh); 4480 4481 /* XXX: What if dr_clusters is too large? */ 4482 while (le32_to_cpu(dx_root->dr_clusters)) { 4483 ret = ocfs2_dx_dir_lookup_rec(dir, &dx_root->dr_list, 4484 major_hash, &cpos, &blkno, &clen); 4485 if (ret) { 4486 mlog_errno(ret); 4487 goto out; 4488 } 4489 4490 p_cpos = ocfs2_blocks_to_clusters(dir->i_sb, blkno); 4491 4492 ret = ocfs2_remove_btree_range(dir, &et, cpos, p_cpos, clen, 0, 4493 &dealloc, 0, false); 4494 if (ret) { 4495 mlog_errno(ret); 4496 goto out; 4497 } 4498 4499 if (cpos == 0) 4500 break; 4501 4502 major_hash = cpos - 1; 4503 } 4504 4505 remove_index: 4506 ret = ocfs2_dx_dir_remove_index(dir, di_bh, dx_root_bh); 4507 if (ret) { 4508 mlog_errno(ret); 4509 goto out; 4510 } 4511 4512 ocfs2_remove_from_cache(INODE_CACHE(dir), dx_root_bh); 4513 out: 4514 ocfs2_schedule_truncate_log_flush(osb, 1); 4515 ocfs2_run_deallocs(osb, &dealloc); 4516 4517 brelse(dx_root_bh); 4518 return ret; 4519 } 4520