1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * namei.c 4 * 5 * Create and rename file, directory, symlinks 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 Linux 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/iversion.h> 29 30 #include <cluster/masklog.h> 31 32 #include "ocfs2.h" 33 34 #include "alloc.h" 35 #include "dcache.h" 36 #include "dir.h" 37 #include "dlmglue.h" 38 #include "extent_map.h" 39 #include "file.h" 40 #include "inode.h" 41 #include "journal.h" 42 #include "namei.h" 43 #include "suballoc.h" 44 #include "super.h" 45 #include "symlink.h" 46 #include "sysfile.h" 47 #include "uptodate.h" 48 #include "xattr.h" 49 #include "acl.h" 50 #include "ocfs2_trace.h" 51 #include "ioctl.h" 52 53 #include "buffer_head_io.h" 54 55 static int ocfs2_mknod_locked(struct ocfs2_super *osb, 56 struct inode *dir, 57 struct inode *inode, 58 dev_t dev, 59 struct buffer_head **new_fe_bh, 60 struct buffer_head *parent_fe_bh, 61 handle_t *handle, 62 struct ocfs2_alloc_context *inode_ac); 63 64 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb, 65 struct inode **ret_orphan_dir, 66 u64 blkno, 67 char *name, 68 struct ocfs2_dir_lookup_result *lookup, 69 bool dio); 70 71 static int ocfs2_orphan_add(struct ocfs2_super *osb, 72 handle_t *handle, 73 struct inode *inode, 74 struct buffer_head *fe_bh, 75 char *name, 76 struct ocfs2_dir_lookup_result *lookup, 77 struct inode *orphan_dir_inode, 78 bool dio); 79 80 static int ocfs2_create_symlink_data(struct ocfs2_super *osb, 81 handle_t *handle, 82 struct inode *inode, 83 const char *symname); 84 85 static int ocfs2_double_lock(struct ocfs2_super *osb, 86 struct buffer_head **bh1, 87 struct inode *inode1, 88 struct buffer_head **bh2, 89 struct inode *inode2, 90 int rename); 91 92 static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2); 93 /* An orphan dir name is an 8 byte value, printed as a hex string */ 94 #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64))) 95 96 static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry, 97 unsigned int flags) 98 { 99 int status; 100 u64 blkno; 101 struct inode *inode = NULL; 102 struct dentry *ret; 103 struct ocfs2_inode_info *oi; 104 105 trace_ocfs2_lookup(dir, dentry, dentry->d_name.len, 106 dentry->d_name.name, 107 (unsigned long long)OCFS2_I(dir)->ip_blkno, 0); 108 109 if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) { 110 ret = ERR_PTR(-ENAMETOOLONG); 111 goto bail; 112 } 113 114 status = ocfs2_inode_lock_nested(dir, NULL, 0, OI_LS_PARENT); 115 if (status < 0) { 116 if (status != -ENOENT) 117 mlog_errno(status); 118 ret = ERR_PTR(status); 119 goto bail; 120 } 121 122 status = ocfs2_lookup_ino_from_name(dir, dentry->d_name.name, 123 dentry->d_name.len, &blkno); 124 if (status < 0) 125 goto bail_add; 126 127 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0); 128 if (IS_ERR(inode)) { 129 ret = ERR_PTR(-EACCES); 130 goto bail_unlock; 131 } 132 133 oi = OCFS2_I(inode); 134 /* Clear any orphaned state... If we were able to look up the 135 * inode from a directory, it certainly can't be orphaned. We 136 * might have the bad state from a node which intended to 137 * orphan this inode but crashed before it could commit the 138 * unlink. */ 139 spin_lock(&oi->ip_lock); 140 oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED; 141 spin_unlock(&oi->ip_lock); 142 143 bail_add: 144 ret = d_splice_alias(inode, dentry); 145 if (IS_ERR(ret)) 146 goto bail_unlock; 147 148 if (inode) { 149 /* 150 * If d_splice_alias() finds a DCACHE_DISCONNECTED 151 * dentry, it will d_move() it on top of ourse. The 152 * return value will indicate this however, so in 153 * those cases, we switch them around for the locking 154 * code. 155 * 156 * NOTE: This dentry already has ->d_op set from 157 * ocfs2_get_parent() and ocfs2_get_dentry() 158 */ 159 if (ret) 160 dentry = ret; 161 162 status = ocfs2_dentry_attach_lock(dentry, inode, 163 OCFS2_I(dir)->ip_blkno); 164 if (status) { 165 mlog_errno(status); 166 if (ret) 167 dput(ret); 168 ret = ERR_PTR(status); 169 } 170 } else 171 ocfs2_dentry_attach_gen(dentry); 172 173 bail_unlock: 174 /* Don't drop the cluster lock until *after* the d_add -- 175 * unlink on another node will message us to remove that 176 * dentry under this lock so otherwise we can race this with 177 * the downconvert thread and have a stale dentry. */ 178 ocfs2_inode_unlock(dir, 0); 179 180 bail: 181 182 trace_ocfs2_lookup_ret(ret); 183 184 return ret; 185 } 186 187 static struct inode *ocfs2_get_init_inode(struct inode *dir, umode_t mode) 188 { 189 struct inode *inode; 190 int status; 191 192 inode = new_inode(dir->i_sb); 193 if (!inode) { 194 mlog(ML_ERROR, "new_inode failed!\n"); 195 return ERR_PTR(-ENOMEM); 196 } 197 198 /* populate as many fields early on as possible - many of 199 * these are used by the support functions here and in 200 * callers. */ 201 if (S_ISDIR(mode)) 202 set_nlink(inode, 2); 203 mode = mode_strip_sgid(&nop_mnt_idmap, dir, mode); 204 inode_init_owner(&nop_mnt_idmap, inode, dir, mode); 205 status = dquot_initialize(inode); 206 if (status) { 207 iput(inode); 208 return ERR_PTR(status); 209 } 210 211 return inode; 212 } 213 214 static void ocfs2_cleanup_add_entry_failure(struct ocfs2_super *osb, 215 struct dentry *dentry, struct inode *inode) 216 { 217 struct ocfs2_dentry_lock *dl = dentry->d_fsdata; 218 219 ocfs2_simple_drop_lockres(osb, &dl->dl_lockres); 220 ocfs2_lock_res_free(&dl->dl_lockres); 221 BUG_ON(dl->dl_count != 1); 222 spin_lock(&dentry_attach_lock); 223 dentry->d_fsdata = NULL; 224 spin_unlock(&dentry_attach_lock); 225 kfree(dl); 226 iput(inode); 227 } 228 229 static int ocfs2_mknod(struct mnt_idmap *idmap, 230 struct inode *dir, 231 struct dentry *dentry, 232 umode_t mode, 233 dev_t dev) 234 { 235 int status = 0; 236 struct buffer_head *parent_fe_bh = NULL; 237 handle_t *handle = NULL; 238 struct ocfs2_super *osb; 239 struct ocfs2_dinode *dirfe; 240 struct ocfs2_dinode *fe = NULL; 241 struct buffer_head *new_fe_bh = NULL; 242 struct inode *inode = NULL; 243 struct ocfs2_alloc_context *inode_ac = NULL; 244 struct ocfs2_alloc_context *data_ac = NULL; 245 struct ocfs2_alloc_context *meta_ac = NULL; 246 int want_clusters = 0; 247 int want_meta = 0; 248 int xattr_credits = 0; 249 struct ocfs2_security_xattr_info si = { 250 .name = NULL, 251 .enable = 1, 252 }; 253 int did_quota_inode = 0; 254 struct ocfs2_dir_lookup_result lookup = { NULL, }; 255 sigset_t oldset; 256 int did_block_signals = 0; 257 struct ocfs2_dentry_lock *dl = NULL; 258 259 trace_ocfs2_mknod(dir, dentry, dentry->d_name.len, dentry->d_name.name, 260 (unsigned long long)OCFS2_I(dir)->ip_blkno, 261 (unsigned long)dev, mode); 262 263 status = dquot_initialize(dir); 264 if (status) { 265 mlog_errno(status); 266 return status; 267 } 268 269 /* get our super block */ 270 osb = OCFS2_SB(dir->i_sb); 271 272 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1); 273 if (status < 0) { 274 if (status != -ENOENT) 275 mlog_errno(status); 276 return status; 277 } 278 279 if (S_ISDIR(mode) && (dir->i_nlink >= ocfs2_link_max(osb))) { 280 status = -EMLINK; 281 goto leave; 282 } 283 284 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data; 285 if (!ocfs2_read_links_count(dirfe)) { 286 /* can't make a file in a deleted directory. */ 287 status = -ENOENT; 288 goto leave; 289 } 290 291 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name, 292 dentry->d_name.len); 293 if (status) 294 goto leave; 295 296 /* get a spot inside the dir. */ 297 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh, 298 dentry->d_name.name, 299 dentry->d_name.len, &lookup); 300 if (status < 0) { 301 mlog_errno(status); 302 goto leave; 303 } 304 305 /* reserve an inode spot */ 306 status = ocfs2_reserve_new_inode(osb, &inode_ac); 307 if (status < 0) { 308 if (status != -ENOSPC) 309 mlog_errno(status); 310 goto leave; 311 } 312 313 inode = ocfs2_get_init_inode(dir, mode); 314 if (IS_ERR(inode)) { 315 status = PTR_ERR(inode); 316 inode = NULL; 317 mlog_errno(status); 318 goto leave; 319 } 320 321 /* get security xattr */ 322 status = ocfs2_init_security_get(inode, dir, &dentry->d_name, &si); 323 if (status) { 324 if (status == -EOPNOTSUPP) 325 si.enable = 0; 326 else { 327 mlog_errno(status); 328 goto leave; 329 } 330 } 331 332 /* calculate meta data/clusters for setting security and acl xattr */ 333 status = ocfs2_calc_xattr_init(dir, parent_fe_bh, mode, 334 &si, &want_clusters, 335 &xattr_credits, &want_meta); 336 if (status < 0) { 337 mlog_errno(status); 338 goto leave; 339 } 340 341 /* Reserve a cluster if creating an extent based directory. */ 342 if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) { 343 want_clusters += 1; 344 345 /* Dir indexing requires extra space as well */ 346 if (ocfs2_supports_indexed_dirs(osb)) 347 want_meta++; 348 } 349 350 status = ocfs2_reserve_new_metadata_blocks(osb, want_meta, &meta_ac); 351 if (status < 0) { 352 if (status != -ENOSPC) 353 mlog_errno(status); 354 goto leave; 355 } 356 357 status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac); 358 if (status < 0) { 359 if (status != -ENOSPC) 360 mlog_errno(status); 361 goto leave; 362 } 363 364 handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb, 365 S_ISDIR(mode), 366 xattr_credits)); 367 if (IS_ERR(handle)) { 368 status = PTR_ERR(handle); 369 handle = NULL; 370 mlog_errno(status); 371 goto leave; 372 } 373 374 /* Starting to change things, restart is no longer possible. */ 375 ocfs2_block_signals(&oldset); 376 did_block_signals = 1; 377 378 status = dquot_alloc_inode(inode); 379 if (status) 380 goto leave; 381 did_quota_inode = 1; 382 383 /* do the real work now. */ 384 status = ocfs2_mknod_locked(osb, dir, inode, dev, 385 &new_fe_bh, parent_fe_bh, handle, 386 inode_ac); 387 if (status < 0) { 388 mlog_errno(status); 389 goto leave; 390 } 391 392 fe = (struct ocfs2_dinode *) new_fe_bh->b_data; 393 if (S_ISDIR(mode)) { 394 status = ocfs2_fill_new_dir(osb, handle, dir, inode, 395 new_fe_bh, data_ac, meta_ac); 396 if (status < 0) { 397 mlog_errno(status); 398 goto leave; 399 } 400 401 status = ocfs2_journal_access_di(handle, INODE_CACHE(dir), 402 parent_fe_bh, 403 OCFS2_JOURNAL_ACCESS_WRITE); 404 if (status < 0) { 405 mlog_errno(status); 406 goto leave; 407 } 408 ocfs2_add_links_count(dirfe, 1); 409 ocfs2_journal_dirty(handle, parent_fe_bh); 410 inc_nlink(dir); 411 } 412 413 status = ocfs2_init_acl(handle, inode, dir, new_fe_bh, parent_fe_bh, 414 meta_ac, data_ac); 415 416 if (status < 0) { 417 mlog_errno(status); 418 goto roll_back; 419 } 420 421 if (si.enable) { 422 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si, 423 meta_ac, data_ac); 424 if (status < 0) { 425 mlog_errno(status); 426 goto roll_back; 427 } 428 } 429 430 /* 431 * Do this before adding the entry to the directory. We add 432 * also set d_op after success so that ->d_iput() will cleanup 433 * the dentry lock even if ocfs2_add_entry() fails below. 434 */ 435 status = ocfs2_dentry_attach_lock(dentry, inode, 436 OCFS2_I(dir)->ip_blkno); 437 if (status) { 438 mlog_errno(status); 439 goto roll_back; 440 } 441 442 dl = dentry->d_fsdata; 443 444 status = ocfs2_add_entry(handle, dentry, inode, 445 OCFS2_I(inode)->ip_blkno, parent_fe_bh, 446 &lookup); 447 if (status < 0) { 448 mlog_errno(status); 449 goto roll_back; 450 } 451 452 insert_inode_hash(inode); 453 d_instantiate(dentry, inode); 454 status = 0; 455 456 roll_back: 457 if (status < 0 && S_ISDIR(mode)) { 458 ocfs2_add_links_count(dirfe, -1); 459 drop_nlink(dir); 460 } 461 462 leave: 463 if (status < 0 && did_quota_inode) 464 dquot_free_inode(inode); 465 if (handle) { 466 if (status < 0 && fe) 467 ocfs2_set_links_count(fe, 0); 468 ocfs2_commit_trans(osb, handle); 469 } 470 471 ocfs2_inode_unlock(dir, 1); 472 if (did_block_signals) 473 ocfs2_unblock_signals(&oldset); 474 475 brelse(new_fe_bh); 476 brelse(parent_fe_bh); 477 kfree(si.value); 478 479 ocfs2_free_dir_lookup_result(&lookup); 480 481 if (inode_ac) 482 ocfs2_free_alloc_context(inode_ac); 483 484 if (data_ac) 485 ocfs2_free_alloc_context(data_ac); 486 487 if (meta_ac) 488 ocfs2_free_alloc_context(meta_ac); 489 490 /* 491 * We should call iput after the i_rwsem of the bitmap been 492 * unlocked in ocfs2_free_alloc_context, or the 493 * ocfs2_delete_inode will mutex_lock again. 494 */ 495 if ((status < 0) && inode) { 496 if (dl) 497 ocfs2_cleanup_add_entry_failure(osb, dentry, inode); 498 499 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR; 500 clear_nlink(inode); 501 iput(inode); 502 } 503 504 if (status) 505 mlog_errno(status); 506 507 return status; 508 } 509 510 static int __ocfs2_mknod_locked(struct inode *dir, 511 struct inode *inode, 512 dev_t dev, 513 struct buffer_head **new_fe_bh, 514 handle_t *handle, 515 struct ocfs2_alloc_context *inode_ac, 516 u64 fe_blkno, u64 suballoc_loc, u16 suballoc_bit) 517 { 518 int status = 0; 519 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); 520 struct ocfs2_dinode *fe = NULL; 521 struct ocfs2_extent_list *fel; 522 u16 feat; 523 struct ocfs2_inode_info *oi = OCFS2_I(inode); 524 struct timespec64 ts; 525 526 *new_fe_bh = NULL; 527 528 /* populate as many fields early on as possible - many of 529 * these are used by the support functions here and in 530 * callers. */ 531 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno); 532 oi->ip_blkno = fe_blkno; 533 spin_lock(&osb->osb_lock); 534 inode->i_generation = osb->s_next_generation++; 535 spin_unlock(&osb->osb_lock); 536 537 *new_fe_bh = sb_getblk(osb->sb, fe_blkno); 538 if (!*new_fe_bh) { 539 status = -ENOMEM; 540 mlog_errno(status); 541 goto leave; 542 } 543 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), *new_fe_bh); 544 545 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), 546 *new_fe_bh, 547 OCFS2_JOURNAL_ACCESS_CREATE); 548 if (status < 0) { 549 mlog_errno(status); 550 goto leave; 551 } 552 553 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data; 554 memset(fe, 0, osb->sb->s_blocksize); 555 556 fe->i_generation = cpu_to_le32(inode->i_generation); 557 fe->i_fs_generation = cpu_to_le32(osb->fs_generation); 558 fe->i_blkno = cpu_to_le64(fe_blkno); 559 fe->i_suballoc_loc = cpu_to_le64(suballoc_loc); 560 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit); 561 fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot); 562 fe->i_uid = cpu_to_le32(i_uid_read(inode)); 563 fe->i_gid = cpu_to_le32(i_gid_read(inode)); 564 fe->i_mode = cpu_to_le16(inode->i_mode); 565 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) 566 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev)); 567 568 ocfs2_set_links_count(fe, inode->i_nlink); 569 570 fe->i_last_eb_blk = 0; 571 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE); 572 fe->i_flags |= cpu_to_le32(OCFS2_VALID_FL); 573 ktime_get_coarse_real_ts64(&ts); 574 fe->i_atime = fe->i_ctime = fe->i_mtime = 575 cpu_to_le64(ts.tv_sec); 576 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec = 577 cpu_to_le32(ts.tv_nsec); 578 fe->i_dtime = 0; 579 580 /* 581 * If supported, directories start with inline data. If inline 582 * isn't supported, but indexing is, we start them as indexed. 583 */ 584 feat = le16_to_cpu(fe->i_dyn_features); 585 if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) { 586 fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL); 587 588 fe->id2.i_data.id_count = cpu_to_le16( 589 ocfs2_max_inline_data_with_xattr(osb->sb, fe)); 590 } else { 591 fel = &fe->id2.i_list; 592 fel->l_tree_depth = 0; 593 fel->l_next_free_rec = 0; 594 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb)); 595 } 596 597 ocfs2_journal_dirty(handle, *new_fe_bh); 598 599 ocfs2_populate_inode(inode, fe, 1); 600 ocfs2_ci_set_new(osb, INODE_CACHE(inode)); 601 if (!ocfs2_mount_local(osb)) { 602 status = ocfs2_create_new_inode_locks(inode); 603 if (status < 0) 604 mlog_errno(status); 605 } 606 607 ocfs2_update_inode_fsync_trans(handle, inode, 1); 608 609 leave: 610 if (status < 0) { 611 if (*new_fe_bh) { 612 brelse(*new_fe_bh); 613 *new_fe_bh = NULL; 614 } 615 } 616 617 if (status) 618 mlog_errno(status); 619 return status; 620 } 621 622 static int ocfs2_mknod_locked(struct ocfs2_super *osb, 623 struct inode *dir, 624 struct inode *inode, 625 dev_t dev, 626 struct buffer_head **new_fe_bh, 627 struct buffer_head *parent_fe_bh, 628 handle_t *handle, 629 struct ocfs2_alloc_context *inode_ac) 630 { 631 int status = 0; 632 u64 suballoc_loc, fe_blkno = 0; 633 u16 suballoc_bit; 634 635 *new_fe_bh = NULL; 636 637 status = ocfs2_claim_new_inode(handle, dir, parent_fe_bh, 638 inode_ac, &suballoc_loc, 639 &suballoc_bit, &fe_blkno); 640 if (status < 0) { 641 mlog_errno(status); 642 return status; 643 } 644 645 return __ocfs2_mknod_locked(dir, inode, dev, new_fe_bh, 646 handle, inode_ac, fe_blkno, 647 suballoc_loc, suballoc_bit); 648 } 649 650 static struct dentry *ocfs2_mkdir(struct mnt_idmap *idmap, 651 struct inode *dir, 652 struct dentry *dentry, 653 umode_t mode) 654 { 655 int ret; 656 657 trace_ocfs2_mkdir(dir, dentry, dentry->d_name.len, dentry->d_name.name, 658 OCFS2_I(dir)->ip_blkno, mode); 659 ret = ocfs2_mknod(&nop_mnt_idmap, dir, dentry, mode | S_IFDIR, 0); 660 if (ret) 661 mlog_errno(ret); 662 663 return ERR_PTR(ret); 664 } 665 666 static int ocfs2_create(struct mnt_idmap *idmap, 667 struct inode *dir, 668 struct dentry *dentry, 669 umode_t mode, 670 bool excl) 671 { 672 int ret; 673 674 trace_ocfs2_create(dir, dentry, dentry->d_name.len, dentry->d_name.name, 675 (unsigned long long)OCFS2_I(dir)->ip_blkno, mode); 676 ret = ocfs2_mknod(&nop_mnt_idmap, dir, dentry, mode | S_IFREG, 0); 677 if (ret) 678 mlog_errno(ret); 679 680 return ret; 681 } 682 683 static int ocfs2_link(struct dentry *old_dentry, 684 struct inode *dir, 685 struct dentry *dentry) 686 { 687 handle_t *handle; 688 struct inode *inode = d_inode(old_dentry); 689 struct inode *old_dir = d_inode(old_dentry->d_parent); 690 int err; 691 struct buffer_head *fe_bh = NULL; 692 struct buffer_head *old_dir_bh = NULL; 693 struct buffer_head *parent_fe_bh = NULL; 694 struct ocfs2_dinode *fe = NULL; 695 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); 696 struct ocfs2_dir_lookup_result lookup = { NULL, }; 697 sigset_t oldset; 698 u64 old_de_ino; 699 700 trace_ocfs2_link((unsigned long long)OCFS2_I(inode)->ip_blkno, 701 old_dentry->d_name.len, old_dentry->d_name.name, 702 dentry->d_name.len, dentry->d_name.name); 703 704 if (S_ISDIR(inode->i_mode)) 705 return -EPERM; 706 707 err = dquot_initialize(dir); 708 if (err) { 709 mlog_errno(err); 710 return err; 711 } 712 713 err = ocfs2_double_lock(osb, &old_dir_bh, old_dir, 714 &parent_fe_bh, dir, 0); 715 if (err < 0) { 716 if (err != -ENOENT) 717 mlog_errno(err); 718 return err; 719 } 720 721 /* make sure both dirs have bhs 722 * get an extra ref on old_dir_bh if old==new */ 723 if (!parent_fe_bh) { 724 if (old_dir_bh) { 725 parent_fe_bh = old_dir_bh; 726 get_bh(parent_fe_bh); 727 } else { 728 mlog(ML_ERROR, "%s: no old_dir_bh!\n", osb->uuid_str); 729 err = -EIO; 730 goto out; 731 } 732 } 733 734 if (!dir->i_nlink) { 735 err = -ENOENT; 736 goto out; 737 } 738 739 err = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name, 740 old_dentry->d_name.len, &old_de_ino); 741 if (err) { 742 err = -ENOENT; 743 goto out; 744 } 745 746 /* 747 * Check whether another node removed the source inode while we 748 * were in the vfs. 749 */ 750 if (old_de_ino != OCFS2_I(inode)->ip_blkno) { 751 err = -ENOENT; 752 goto out; 753 } 754 755 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name, 756 dentry->d_name.len); 757 if (err) 758 goto out; 759 760 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh, 761 dentry->d_name.name, 762 dentry->d_name.len, &lookup); 763 if (err < 0) { 764 mlog_errno(err); 765 goto out; 766 } 767 768 err = ocfs2_inode_lock(inode, &fe_bh, 1); 769 if (err < 0) { 770 if (err != -ENOENT) 771 mlog_errno(err); 772 goto out; 773 } 774 775 fe = (struct ocfs2_dinode *) fe_bh->b_data; 776 if (ocfs2_read_links_count(fe) >= ocfs2_link_max(osb)) { 777 err = -EMLINK; 778 goto out_unlock_inode; 779 } 780 781 handle = ocfs2_start_trans(osb, ocfs2_link_credits(osb->sb)); 782 if (IS_ERR(handle)) { 783 err = PTR_ERR(handle); 784 handle = NULL; 785 mlog_errno(err); 786 goto out_unlock_inode; 787 } 788 789 /* Starting to change things, restart is no longer possible. */ 790 ocfs2_block_signals(&oldset); 791 792 err = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh, 793 OCFS2_JOURNAL_ACCESS_WRITE); 794 if (err < 0) { 795 mlog_errno(err); 796 goto out_commit; 797 } 798 799 inc_nlink(inode); 800 inode_set_ctime_current(inode); 801 ocfs2_set_links_count(fe, inode->i_nlink); 802 fe->i_ctime = cpu_to_le64(inode_get_ctime_sec(inode)); 803 fe->i_ctime_nsec = cpu_to_le32(inode_get_ctime_nsec(inode)); 804 ocfs2_update_inode_fsync_trans(handle, inode, 0); 805 ocfs2_journal_dirty(handle, fe_bh); 806 807 err = ocfs2_add_entry(handle, dentry, inode, 808 OCFS2_I(inode)->ip_blkno, 809 parent_fe_bh, &lookup); 810 if (err) { 811 ocfs2_add_links_count(fe, -1); 812 drop_nlink(inode); 813 mlog_errno(err); 814 goto out_commit; 815 } 816 817 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno); 818 if (err) { 819 mlog_errno(err); 820 goto out_commit; 821 } 822 823 ihold(inode); 824 d_instantiate(dentry, inode); 825 826 out_commit: 827 ocfs2_commit_trans(osb, handle); 828 ocfs2_unblock_signals(&oldset); 829 out_unlock_inode: 830 ocfs2_inode_unlock(inode, 1); 831 832 out: 833 ocfs2_double_unlock(old_dir, dir); 834 835 brelse(fe_bh); 836 brelse(parent_fe_bh); 837 brelse(old_dir_bh); 838 839 ocfs2_free_dir_lookup_result(&lookup); 840 841 if (err) 842 mlog_errno(err); 843 844 return err; 845 } 846 847 /* 848 * Takes and drops an exclusive lock on the given dentry. This will 849 * force other nodes to drop it. 850 */ 851 static int ocfs2_remote_dentry_delete(struct dentry *dentry) 852 { 853 int ret; 854 855 ret = ocfs2_dentry_lock(dentry, 1); 856 if (ret) 857 mlog_errno(ret); 858 else 859 ocfs2_dentry_unlock(dentry, 1); 860 861 return ret; 862 } 863 864 static inline int ocfs2_inode_is_unlinkable(struct inode *inode) 865 { 866 if (S_ISDIR(inode->i_mode)) { 867 if (inode->i_nlink == 2) 868 return 1; 869 return 0; 870 } 871 872 if (inode->i_nlink == 1) 873 return 1; 874 return 0; 875 } 876 877 static int ocfs2_unlink(struct inode *dir, 878 struct dentry *dentry) 879 { 880 int status; 881 int child_locked = 0; 882 bool is_unlinkable = false; 883 struct inode *inode = d_inode(dentry); 884 struct inode *orphan_dir = NULL; 885 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); 886 u64 blkno; 887 struct ocfs2_dinode *fe = NULL; 888 struct buffer_head *fe_bh = NULL; 889 struct buffer_head *parent_node_bh = NULL; 890 handle_t *handle = NULL; 891 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1]; 892 struct ocfs2_dir_lookup_result lookup = { NULL, }; 893 struct ocfs2_dir_lookup_result orphan_insert = { NULL, }; 894 895 trace_ocfs2_unlink(dir, dentry, dentry->d_name.len, 896 dentry->d_name.name, 897 (unsigned long long)OCFS2_I(dir)->ip_blkno, 898 (unsigned long long)OCFS2_I(inode)->ip_blkno); 899 900 status = dquot_initialize(dir); 901 if (status) { 902 mlog_errno(status); 903 return status; 904 } 905 906 BUG_ON(d_inode(dentry->d_parent) != dir); 907 908 if (inode == osb->root_inode) 909 return -EPERM; 910 911 status = ocfs2_inode_lock_nested(dir, &parent_node_bh, 1, 912 OI_LS_PARENT); 913 if (status < 0) { 914 if (status != -ENOENT) 915 mlog_errno(status); 916 return status; 917 } 918 919 status = ocfs2_find_files_on_disk(dentry->d_name.name, 920 dentry->d_name.len, &blkno, dir, 921 &lookup); 922 if (status < 0) { 923 if (status != -ENOENT) 924 mlog_errno(status); 925 goto leave; 926 } 927 928 if (OCFS2_I(inode)->ip_blkno != blkno) { 929 status = -ENOENT; 930 931 trace_ocfs2_unlink_noent( 932 (unsigned long long)OCFS2_I(inode)->ip_blkno, 933 (unsigned long long)blkno, 934 OCFS2_I(inode)->ip_flags); 935 goto leave; 936 } 937 938 status = ocfs2_inode_lock(inode, &fe_bh, 1); 939 if (status < 0) { 940 if (status != -ENOENT) 941 mlog_errno(status); 942 goto leave; 943 } 944 child_locked = 1; 945 946 if (S_ISDIR(inode->i_mode)) { 947 if (inode->i_nlink != 2 || !ocfs2_empty_dir(inode)) { 948 status = -ENOTEMPTY; 949 goto leave; 950 } 951 } 952 953 status = ocfs2_remote_dentry_delete(dentry); 954 if (status < 0) { 955 /* This remote delete should succeed under all normal 956 * circumstances. */ 957 mlog_errno(status); 958 goto leave; 959 } 960 961 if (ocfs2_inode_is_unlinkable(inode)) { 962 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, 963 OCFS2_I(inode)->ip_blkno, 964 orphan_name, &orphan_insert, 965 false); 966 if (status < 0) { 967 mlog_errno(status); 968 goto leave; 969 } 970 is_unlinkable = true; 971 } 972 973 handle = ocfs2_start_trans(osb, ocfs2_unlink_credits(osb->sb)); 974 if (IS_ERR(handle)) { 975 status = PTR_ERR(handle); 976 handle = NULL; 977 mlog_errno(status); 978 goto leave; 979 } 980 981 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh, 982 OCFS2_JOURNAL_ACCESS_WRITE); 983 if (status < 0) { 984 mlog_errno(status); 985 goto leave; 986 } 987 988 fe = (struct ocfs2_dinode *) fe_bh->b_data; 989 990 /* delete the name from the parent dir */ 991 status = ocfs2_delete_entry(handle, dir, &lookup); 992 if (status < 0) { 993 mlog_errno(status); 994 goto leave; 995 } 996 997 if (S_ISDIR(inode->i_mode)) 998 drop_nlink(inode); 999 drop_nlink(inode); 1000 ocfs2_set_links_count(fe, inode->i_nlink); 1001 ocfs2_update_inode_fsync_trans(handle, inode, 0); 1002 ocfs2_journal_dirty(handle, fe_bh); 1003 1004 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); 1005 if (S_ISDIR(inode->i_mode)) 1006 drop_nlink(dir); 1007 1008 status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh); 1009 if (status < 0) { 1010 mlog_errno(status); 1011 if (S_ISDIR(inode->i_mode)) 1012 inc_nlink(dir); 1013 goto leave; 1014 } 1015 1016 if (is_unlinkable) { 1017 status = ocfs2_orphan_add(osb, handle, inode, fe_bh, 1018 orphan_name, &orphan_insert, orphan_dir, false); 1019 if (status < 0) 1020 mlog_errno(status); 1021 } 1022 1023 leave: 1024 if (handle) 1025 ocfs2_commit_trans(osb, handle); 1026 1027 if (orphan_dir) { 1028 /* This was locked for us in ocfs2_prepare_orphan_dir() */ 1029 ocfs2_inode_unlock(orphan_dir, 1); 1030 inode_unlock(orphan_dir); 1031 iput(orphan_dir); 1032 } 1033 1034 if (child_locked) 1035 ocfs2_inode_unlock(inode, 1); 1036 1037 ocfs2_inode_unlock(dir, 1); 1038 1039 brelse(fe_bh); 1040 brelse(parent_node_bh); 1041 1042 ocfs2_free_dir_lookup_result(&orphan_insert); 1043 ocfs2_free_dir_lookup_result(&lookup); 1044 1045 if (status && (status != -ENOTEMPTY) && (status != -ENOENT)) 1046 mlog_errno(status); 1047 1048 return status; 1049 } 1050 1051 static int ocfs2_check_if_ancestor(struct ocfs2_super *osb, 1052 u64 src_inode_no, u64 dest_inode_no) 1053 { 1054 int ret = 0, i = 0; 1055 u64 parent_inode_no = 0; 1056 u64 child_inode_no = src_inode_no; 1057 struct inode *child_inode; 1058 1059 #define MAX_LOOKUP_TIMES 32 1060 while (1) { 1061 child_inode = ocfs2_iget(osb, child_inode_no, 0, 0); 1062 if (IS_ERR(child_inode)) { 1063 ret = PTR_ERR(child_inode); 1064 break; 1065 } 1066 1067 ret = ocfs2_inode_lock(child_inode, NULL, 0); 1068 if (ret < 0) { 1069 iput(child_inode); 1070 if (ret != -ENOENT) 1071 mlog_errno(ret); 1072 break; 1073 } 1074 1075 ret = ocfs2_lookup_ino_from_name(child_inode, "..", 2, 1076 &parent_inode_no); 1077 ocfs2_inode_unlock(child_inode, 0); 1078 iput(child_inode); 1079 if (ret < 0) { 1080 ret = -ENOENT; 1081 break; 1082 } 1083 1084 if (parent_inode_no == dest_inode_no) { 1085 ret = 1; 1086 break; 1087 } 1088 1089 if (parent_inode_no == osb->root_inode->i_ino) { 1090 ret = 0; 1091 break; 1092 } 1093 1094 child_inode_no = parent_inode_no; 1095 1096 if (++i >= MAX_LOOKUP_TIMES) { 1097 mlog_ratelimited(ML_NOTICE, "max lookup times reached, " 1098 "filesystem may have nested directories, " 1099 "src inode: %llu, dest inode: %llu.\n", 1100 (unsigned long long)src_inode_no, 1101 (unsigned long long)dest_inode_no); 1102 ret = 0; 1103 break; 1104 } 1105 } 1106 1107 return ret; 1108 } 1109 1110 /* 1111 * The only place this should be used is rename and link! 1112 * if they have the same id, then the 1st one is the only one locked. 1113 */ 1114 static int ocfs2_double_lock(struct ocfs2_super *osb, 1115 struct buffer_head **bh1, 1116 struct inode *inode1, 1117 struct buffer_head **bh2, 1118 struct inode *inode2, 1119 int rename) 1120 { 1121 int status; 1122 int inode1_is_ancestor, inode2_is_ancestor; 1123 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1); 1124 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2); 1125 1126 trace_ocfs2_double_lock((unsigned long long)oi1->ip_blkno, 1127 (unsigned long long)oi2->ip_blkno); 1128 1129 if (*bh1) 1130 *bh1 = NULL; 1131 if (*bh2) 1132 *bh2 = NULL; 1133 1134 /* we always want to lock the one with the lower lockid first. 1135 * and if they are nested, we lock ancestor first */ 1136 if (oi1->ip_blkno != oi2->ip_blkno) { 1137 inode1_is_ancestor = ocfs2_check_if_ancestor(osb, oi2->ip_blkno, 1138 oi1->ip_blkno); 1139 if (inode1_is_ancestor < 0) { 1140 status = inode1_is_ancestor; 1141 goto bail; 1142 } 1143 1144 inode2_is_ancestor = ocfs2_check_if_ancestor(osb, oi1->ip_blkno, 1145 oi2->ip_blkno); 1146 if (inode2_is_ancestor < 0) { 1147 status = inode2_is_ancestor; 1148 goto bail; 1149 } 1150 1151 if ((inode1_is_ancestor == 1) || 1152 (oi1->ip_blkno < oi2->ip_blkno && 1153 inode2_is_ancestor == 0)) { 1154 /* switch id1 and id2 around */ 1155 swap(bh2, bh1); 1156 swap(inode2, inode1); 1157 } 1158 /* lock id2 */ 1159 status = ocfs2_inode_lock_nested(inode2, bh2, 1, 1160 rename == 1 ? OI_LS_RENAME1 : OI_LS_PARENT); 1161 if (status < 0) { 1162 if (status != -ENOENT) 1163 mlog_errno(status); 1164 goto bail; 1165 } 1166 } 1167 1168 /* lock id1 */ 1169 status = ocfs2_inode_lock_nested(inode1, bh1, 1, 1170 rename == 1 ? OI_LS_RENAME2 : OI_LS_PARENT); 1171 if (status < 0) { 1172 /* 1173 * An error return must mean that no cluster locks 1174 * were held on function exit. 1175 */ 1176 if (oi1->ip_blkno != oi2->ip_blkno) { 1177 ocfs2_inode_unlock(inode2, 1); 1178 brelse(*bh2); 1179 *bh2 = NULL; 1180 } 1181 1182 if (status != -ENOENT) 1183 mlog_errno(status); 1184 } 1185 1186 trace_ocfs2_double_lock_end( 1187 (unsigned long long)oi1->ip_blkno, 1188 (unsigned long long)oi2->ip_blkno); 1189 1190 bail: 1191 if (status) 1192 mlog_errno(status); 1193 return status; 1194 } 1195 1196 static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2) 1197 { 1198 ocfs2_inode_unlock(inode1, 1); 1199 1200 if (inode1 != inode2) 1201 ocfs2_inode_unlock(inode2, 1); 1202 } 1203 1204 static int ocfs2_rename(struct mnt_idmap *idmap, 1205 struct inode *old_dir, 1206 struct dentry *old_dentry, 1207 struct inode *new_dir, 1208 struct dentry *new_dentry, 1209 unsigned int flags) 1210 { 1211 int status = 0, rename_lock = 0, parents_locked = 0, target_exists = 0; 1212 int old_child_locked = 0, new_child_locked = 0, update_dot_dot = 0; 1213 struct inode *old_inode = d_inode(old_dentry); 1214 struct inode *new_inode = d_inode(new_dentry); 1215 struct inode *orphan_dir = NULL; 1216 struct ocfs2_dinode *newfe = NULL; 1217 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1]; 1218 struct buffer_head *newfe_bh = NULL; 1219 struct buffer_head *old_inode_bh = NULL; 1220 struct ocfs2_super *osb = NULL; 1221 u64 newfe_blkno, old_de_ino; 1222 handle_t *handle = NULL; 1223 struct buffer_head *old_dir_bh = NULL; 1224 struct buffer_head *new_dir_bh = NULL; 1225 u32 old_dir_nlink = old_dir->i_nlink; 1226 struct ocfs2_dinode *old_di; 1227 struct ocfs2_dir_lookup_result old_inode_dot_dot_res = { NULL, }; 1228 struct ocfs2_dir_lookup_result target_lookup_res = { NULL, }; 1229 struct ocfs2_dir_lookup_result old_entry_lookup = { NULL, }; 1230 struct ocfs2_dir_lookup_result orphan_insert = { NULL, }; 1231 struct ocfs2_dir_lookup_result target_insert = { NULL, }; 1232 bool should_add_orphan = false; 1233 1234 if (flags) 1235 return -EINVAL; 1236 1237 /* At some point it might be nice to break this function up a 1238 * bit. */ 1239 1240 trace_ocfs2_rename(old_dir, old_dentry, new_dir, new_dentry, 1241 old_dentry->d_name.len, old_dentry->d_name.name, 1242 new_dentry->d_name.len, new_dentry->d_name.name); 1243 1244 status = dquot_initialize(old_dir); 1245 if (status) { 1246 mlog_errno(status); 1247 goto bail; 1248 } 1249 status = dquot_initialize(new_dir); 1250 if (status) { 1251 mlog_errno(status); 1252 goto bail; 1253 } 1254 1255 osb = OCFS2_SB(old_dir->i_sb); 1256 1257 if (new_inode) { 1258 if (!igrab(new_inode)) 1259 BUG(); 1260 } 1261 1262 /* Assume a directory hierarchy thusly: 1263 * a/b/c 1264 * a/d 1265 * a,b,c, and d are all directories. 1266 * 1267 * from cwd of 'a' on both nodes: 1268 * node1: mv b/c d 1269 * node2: mv d b/c 1270 * 1271 * And that's why, just like the VFS, we need a file system 1272 * rename lock. */ 1273 if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) { 1274 status = ocfs2_rename_lock(osb); 1275 if (status < 0) { 1276 mlog_errno(status); 1277 goto bail; 1278 } 1279 rename_lock = 1; 1280 1281 /* here we cannot guarantee the inodes haven't just been 1282 * changed, so check if they are nested again */ 1283 status = ocfs2_check_if_ancestor(osb, new_dir->i_ino, 1284 old_inode->i_ino); 1285 if (status < 0) { 1286 mlog_errno(status); 1287 goto bail; 1288 } else if (status == 1) { 1289 status = -EPERM; 1290 trace_ocfs2_rename_not_permitted( 1291 (unsigned long long)old_inode->i_ino, 1292 (unsigned long long)new_dir->i_ino); 1293 goto bail; 1294 } 1295 } 1296 1297 /* if old and new are the same, this'll just do one lock. */ 1298 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir, 1299 &new_dir_bh, new_dir, 1); 1300 if (status < 0) { 1301 mlog_errno(status); 1302 goto bail; 1303 } 1304 parents_locked = 1; 1305 1306 if (!new_dir->i_nlink) { 1307 status = -EACCES; 1308 goto bail; 1309 } 1310 1311 /* make sure both dirs have bhs 1312 * get an extra ref on old_dir_bh if old==new */ 1313 if (!new_dir_bh) { 1314 if (old_dir_bh) { 1315 new_dir_bh = old_dir_bh; 1316 get_bh(new_dir_bh); 1317 } else { 1318 mlog(ML_ERROR, "no old_dir_bh!\n"); 1319 status = -EIO; 1320 goto bail; 1321 } 1322 } 1323 1324 /* 1325 * Aside from allowing a meta data update, the locking here 1326 * also ensures that the downconvert thread on other nodes 1327 * won't have to concurrently downconvert the inode and the 1328 * dentry locks. 1329 */ 1330 status = ocfs2_inode_lock_nested(old_inode, &old_inode_bh, 1, 1331 OI_LS_PARENT); 1332 if (status < 0) { 1333 if (status != -ENOENT) 1334 mlog_errno(status); 1335 goto bail; 1336 } 1337 old_child_locked = 1; 1338 1339 status = ocfs2_remote_dentry_delete(old_dentry); 1340 if (status < 0) { 1341 mlog_errno(status); 1342 goto bail; 1343 } 1344 1345 if (S_ISDIR(old_inode->i_mode) && new_dir != old_dir) { 1346 u64 old_inode_parent; 1347 1348 update_dot_dot = 1; 1349 status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent, 1350 old_inode, 1351 &old_inode_dot_dot_res); 1352 if (status) { 1353 status = -EIO; 1354 goto bail; 1355 } 1356 1357 if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) { 1358 status = -EIO; 1359 goto bail; 1360 } 1361 1362 if (!new_inode && new_dir->i_nlink >= ocfs2_link_max(osb)) { 1363 status = -EMLINK; 1364 goto bail; 1365 } 1366 } 1367 1368 status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name, 1369 old_dentry->d_name.len, 1370 &old_de_ino); 1371 if (status) { 1372 status = -ENOENT; 1373 goto bail; 1374 } 1375 1376 /* 1377 * Check for inode number is _not_ due to possible IO errors. 1378 * We might rmdir the source, keep it as pwd of some process 1379 * and merrily kill the link to whatever was created under the 1380 * same name. Goodbye sticky bit ;-< 1381 */ 1382 if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) { 1383 status = -ENOENT; 1384 goto bail; 1385 } 1386 1387 /* check if the target already exists (in which case we need 1388 * to delete it */ 1389 status = ocfs2_find_files_on_disk(new_dentry->d_name.name, 1390 new_dentry->d_name.len, 1391 &newfe_blkno, new_dir, 1392 &target_lookup_res); 1393 /* The only error we allow here is -ENOENT because the new 1394 * file not existing is perfectly valid. */ 1395 if ((status < 0) && (status != -ENOENT)) { 1396 /* If we cannot find the file specified we should just */ 1397 /* return the error... */ 1398 mlog_errno(status); 1399 goto bail; 1400 } 1401 if (status == 0) 1402 target_exists = 1; 1403 1404 if (!target_exists && new_inode) { 1405 /* 1406 * Target was unlinked by another node while we were 1407 * waiting to get to ocfs2_rename(). There isn't 1408 * anything we can do here to help the situation, so 1409 * bubble up the appropriate error. 1410 */ 1411 status = -ENOENT; 1412 goto bail; 1413 } 1414 1415 /* In case we need to overwrite an existing file, we blow it 1416 * away first */ 1417 if (target_exists) { 1418 /* VFS didn't think there existed an inode here, but 1419 * someone else in the cluster must have raced our 1420 * rename to create one. Today we error cleanly, in 1421 * the future we should consider calling iget to build 1422 * a new struct inode for this entry. */ 1423 if (!new_inode) { 1424 status = -EACCES; 1425 1426 trace_ocfs2_rename_target_exists(new_dentry->d_name.len, 1427 new_dentry->d_name.name); 1428 goto bail; 1429 } 1430 1431 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) { 1432 status = -EACCES; 1433 1434 trace_ocfs2_rename_disagree( 1435 (unsigned long long)OCFS2_I(new_inode)->ip_blkno, 1436 (unsigned long long)newfe_blkno, 1437 OCFS2_I(new_inode)->ip_flags); 1438 goto bail; 1439 } 1440 1441 status = ocfs2_inode_lock(new_inode, &newfe_bh, 1); 1442 if (status < 0) { 1443 if (status != -ENOENT) 1444 mlog_errno(status); 1445 goto bail; 1446 } 1447 new_child_locked = 1; 1448 1449 status = ocfs2_remote_dentry_delete(new_dentry); 1450 if (status < 0) { 1451 mlog_errno(status); 1452 goto bail; 1453 } 1454 1455 newfe = (struct ocfs2_dinode *) newfe_bh->b_data; 1456 1457 trace_ocfs2_rename_over_existing( 1458 (unsigned long long)newfe_blkno, newfe_bh, 1459 (unsigned long long)newfe_bh->b_blocknr); 1460 1461 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) { 1462 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, 1463 OCFS2_I(new_inode)->ip_blkno, 1464 orphan_name, &orphan_insert, 1465 false); 1466 if (status < 0) { 1467 mlog_errno(status); 1468 goto bail; 1469 } 1470 should_add_orphan = true; 1471 } 1472 } else { 1473 BUG_ON(d_inode(new_dentry->d_parent) != new_dir); 1474 1475 status = ocfs2_check_dir_for_entry(new_dir, 1476 new_dentry->d_name.name, 1477 new_dentry->d_name.len); 1478 if (status) 1479 goto bail; 1480 1481 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh, 1482 new_dentry->d_name.name, 1483 new_dentry->d_name.len, 1484 &target_insert); 1485 if (status < 0) { 1486 mlog_errno(status); 1487 goto bail; 1488 } 1489 } 1490 1491 handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb)); 1492 if (IS_ERR(handle)) { 1493 status = PTR_ERR(handle); 1494 handle = NULL; 1495 mlog_errno(status); 1496 goto bail; 1497 } 1498 1499 if (target_exists) { 1500 if (S_ISDIR(new_inode->i_mode)) { 1501 if (new_inode->i_nlink != 2 || 1502 !ocfs2_empty_dir(new_inode)) { 1503 status = -ENOTEMPTY; 1504 goto bail; 1505 } 1506 } 1507 status = ocfs2_journal_access_di(handle, INODE_CACHE(new_inode), 1508 newfe_bh, 1509 OCFS2_JOURNAL_ACCESS_WRITE); 1510 if (status < 0) { 1511 mlog_errno(status); 1512 goto bail; 1513 } 1514 1515 /* change the dirent to point to the correct inode */ 1516 status = ocfs2_update_entry(new_dir, handle, &target_lookup_res, 1517 old_inode); 1518 if (status < 0) { 1519 mlog_errno(status); 1520 goto bail; 1521 } 1522 inode_inc_iversion(new_dir); 1523 1524 if (S_ISDIR(new_inode->i_mode)) 1525 ocfs2_set_links_count(newfe, 0); 1526 else 1527 ocfs2_add_links_count(newfe, -1); 1528 ocfs2_journal_dirty(handle, newfe_bh); 1529 if (should_add_orphan) { 1530 status = ocfs2_orphan_add(osb, handle, new_inode, 1531 newfe_bh, orphan_name, 1532 &orphan_insert, orphan_dir, false); 1533 if (status < 0) { 1534 mlog_errno(status); 1535 goto bail; 1536 } 1537 } 1538 } else { 1539 /* if the name was not found in new_dir, add it now */ 1540 status = ocfs2_add_entry(handle, new_dentry, old_inode, 1541 OCFS2_I(old_inode)->ip_blkno, 1542 new_dir_bh, &target_insert); 1543 if (status < 0) { 1544 mlog_errno(status); 1545 goto bail; 1546 } 1547 } 1548 1549 inode_set_ctime_current(old_inode); 1550 mark_inode_dirty(old_inode); 1551 1552 status = ocfs2_journal_access_di(handle, INODE_CACHE(old_inode), 1553 old_inode_bh, 1554 OCFS2_JOURNAL_ACCESS_WRITE); 1555 if (status >= 0) { 1556 old_di = (struct ocfs2_dinode *) old_inode_bh->b_data; 1557 1558 old_di->i_ctime = cpu_to_le64(inode_get_ctime_sec(old_inode)); 1559 old_di->i_ctime_nsec = cpu_to_le32(inode_get_ctime_nsec(old_inode)); 1560 ocfs2_journal_dirty(handle, old_inode_bh); 1561 } else 1562 mlog_errno(status); 1563 1564 /* 1565 * Now that the name has been added to new_dir, remove the old name. 1566 * 1567 * We don't keep any directory entry context around until now 1568 * because the insert might have changed the type of directory 1569 * we're dealing with. 1570 */ 1571 status = ocfs2_find_entry(old_dentry->d_name.name, 1572 old_dentry->d_name.len, old_dir, 1573 &old_entry_lookup); 1574 if (status) { 1575 if (!is_journal_aborted(osb->journal->j_journal)) { 1576 ocfs2_error(osb->sb, "new entry %.*s is added, but old entry %.*s " 1577 "is not deleted.", 1578 new_dentry->d_name.len, new_dentry->d_name.name, 1579 old_dentry->d_name.len, old_dentry->d_name.name); 1580 } 1581 goto bail; 1582 } 1583 1584 status = ocfs2_delete_entry(handle, old_dir, &old_entry_lookup); 1585 if (status < 0) { 1586 mlog_errno(status); 1587 if (!is_journal_aborted(osb->journal->j_journal)) { 1588 ocfs2_error(osb->sb, "new entry %.*s is added, but old entry %.*s " 1589 "is not deleted.", 1590 new_dentry->d_name.len, new_dentry->d_name.name, 1591 old_dentry->d_name.len, old_dentry->d_name.name); 1592 } 1593 goto bail; 1594 } 1595 1596 if (new_inode) { 1597 drop_nlink(new_inode); 1598 inode_set_ctime_current(new_inode); 1599 } 1600 inode_set_mtime_to_ts(old_dir, inode_set_ctime_current(old_dir)); 1601 1602 if (update_dot_dot) { 1603 status = ocfs2_update_entry(old_inode, handle, 1604 &old_inode_dot_dot_res, new_dir); 1605 if (status < 0) { 1606 mlog_errno(status); 1607 goto bail; 1608 } 1609 } 1610 1611 if (S_ISDIR(old_inode->i_mode)) { 1612 drop_nlink(old_dir); 1613 if (new_inode) { 1614 drop_nlink(new_inode); 1615 } else { 1616 inc_nlink(new_dir); 1617 mark_inode_dirty(new_dir); 1618 } 1619 } 1620 mark_inode_dirty(old_dir); 1621 ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh); 1622 if (new_inode) { 1623 mark_inode_dirty(new_inode); 1624 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh); 1625 } 1626 1627 if (old_dir != new_dir) { 1628 /* Keep the same times on both directories.*/ 1629 inode_set_mtime_to_ts(new_dir, 1630 inode_set_ctime_to_ts(new_dir, inode_get_ctime(old_dir))); 1631 1632 /* 1633 * This will also pick up the i_nlink change from the 1634 * block above. 1635 */ 1636 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh); 1637 } 1638 1639 if (old_dir_nlink != old_dir->i_nlink) { 1640 if (!old_dir_bh) { 1641 mlog(ML_ERROR, "need to change nlink for old dir " 1642 "%llu from %d to %d but bh is NULL!\n", 1643 (unsigned long long)OCFS2_I(old_dir)->ip_blkno, 1644 (int)old_dir_nlink, old_dir->i_nlink); 1645 } else { 1646 struct ocfs2_dinode *fe; 1647 status = ocfs2_journal_access_di(handle, 1648 INODE_CACHE(old_dir), 1649 old_dir_bh, 1650 OCFS2_JOURNAL_ACCESS_WRITE); 1651 if (status < 0) { 1652 mlog_errno(status); 1653 goto bail; 1654 } 1655 fe = (struct ocfs2_dinode *) old_dir_bh->b_data; 1656 ocfs2_set_links_count(fe, old_dir->i_nlink); 1657 ocfs2_journal_dirty(handle, old_dir_bh); 1658 } 1659 } 1660 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir); 1661 status = 0; 1662 bail: 1663 if (handle) 1664 ocfs2_commit_trans(osb, handle); 1665 1666 if (orphan_dir) { 1667 /* This was locked for us in ocfs2_prepare_orphan_dir() */ 1668 ocfs2_inode_unlock(orphan_dir, 1); 1669 inode_unlock(orphan_dir); 1670 iput(orphan_dir); 1671 } 1672 1673 if (new_child_locked) 1674 ocfs2_inode_unlock(new_inode, 1); 1675 1676 if (old_child_locked) 1677 ocfs2_inode_unlock(old_inode, 1); 1678 1679 if (parents_locked) 1680 ocfs2_double_unlock(old_dir, new_dir); 1681 1682 if (rename_lock) 1683 ocfs2_rename_unlock(osb); 1684 1685 if (new_inode) 1686 sync_mapping_buffers(old_inode->i_mapping); 1687 1688 iput(new_inode); 1689 1690 ocfs2_free_dir_lookup_result(&target_lookup_res); 1691 ocfs2_free_dir_lookup_result(&old_entry_lookup); 1692 ocfs2_free_dir_lookup_result(&old_inode_dot_dot_res); 1693 ocfs2_free_dir_lookup_result(&orphan_insert); 1694 ocfs2_free_dir_lookup_result(&target_insert); 1695 1696 brelse(newfe_bh); 1697 brelse(old_inode_bh); 1698 brelse(old_dir_bh); 1699 brelse(new_dir_bh); 1700 1701 if (status) 1702 mlog_errno(status); 1703 1704 return status; 1705 } 1706 1707 /* 1708 * we expect i_size = strlen(symname). Copy symname into the file 1709 * data, including the null terminator. 1710 */ 1711 static int ocfs2_create_symlink_data(struct ocfs2_super *osb, 1712 handle_t *handle, 1713 struct inode *inode, 1714 const char *symname) 1715 { 1716 struct buffer_head **bhs = NULL; 1717 const char *c; 1718 struct super_block *sb = osb->sb; 1719 u64 p_blkno, p_blocks; 1720 int virtual, blocks, status, i, bytes_left; 1721 1722 bytes_left = i_size_read(inode) + 1; 1723 /* we can't trust i_blocks because we're actually going to 1724 * write i_size + 1 bytes. */ 1725 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits; 1726 1727 trace_ocfs2_create_symlink_data((unsigned long long)inode->i_blocks, 1728 i_size_read(inode), blocks); 1729 1730 /* Sanity check -- make sure we're going to fit. */ 1731 if (bytes_left > 1732 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) { 1733 status = -EIO; 1734 mlog_errno(status); 1735 goto bail; 1736 } 1737 1738 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL); 1739 if (!bhs) { 1740 status = -ENOMEM; 1741 mlog_errno(status); 1742 goto bail; 1743 } 1744 1745 status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks, 1746 NULL); 1747 if (status < 0) { 1748 mlog_errno(status); 1749 goto bail; 1750 } 1751 1752 /* links can never be larger than one cluster so we know this 1753 * is all going to be contiguous, but do a sanity check 1754 * anyway. */ 1755 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) { 1756 status = -EIO; 1757 mlog_errno(status); 1758 goto bail; 1759 } 1760 1761 virtual = 0; 1762 while(bytes_left > 0) { 1763 c = &symname[virtual * sb->s_blocksize]; 1764 1765 bhs[virtual] = sb_getblk(sb, p_blkno); 1766 if (!bhs[virtual]) { 1767 status = -ENOMEM; 1768 mlog_errno(status); 1769 goto bail; 1770 } 1771 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), 1772 bhs[virtual]); 1773 1774 status = ocfs2_journal_access(handle, INODE_CACHE(inode), 1775 bhs[virtual], 1776 OCFS2_JOURNAL_ACCESS_CREATE); 1777 if (status < 0) { 1778 mlog_errno(status); 1779 goto bail; 1780 } 1781 1782 memset(bhs[virtual]->b_data, 0, sb->s_blocksize); 1783 1784 memcpy(bhs[virtual]->b_data, c, 1785 (bytes_left > sb->s_blocksize) ? sb->s_blocksize : 1786 bytes_left); 1787 1788 ocfs2_journal_dirty(handle, bhs[virtual]); 1789 1790 virtual++; 1791 p_blkno++; 1792 bytes_left -= sb->s_blocksize; 1793 } 1794 1795 status = 0; 1796 bail: 1797 1798 if (bhs) { 1799 for(i = 0; i < blocks; i++) 1800 brelse(bhs[i]); 1801 kfree(bhs); 1802 } 1803 1804 if (status) 1805 mlog_errno(status); 1806 return status; 1807 } 1808 1809 static int ocfs2_symlink(struct mnt_idmap *idmap, 1810 struct inode *dir, 1811 struct dentry *dentry, 1812 const char *symname) 1813 { 1814 int status, l, credits; 1815 u64 newsize; 1816 struct ocfs2_super *osb = NULL; 1817 struct inode *inode = NULL; 1818 struct super_block *sb; 1819 struct buffer_head *new_fe_bh = NULL; 1820 struct buffer_head *parent_fe_bh = NULL; 1821 struct ocfs2_dinode *fe = NULL; 1822 struct ocfs2_dinode *dirfe; 1823 handle_t *handle = NULL; 1824 struct ocfs2_alloc_context *inode_ac = NULL; 1825 struct ocfs2_alloc_context *data_ac = NULL; 1826 struct ocfs2_alloc_context *xattr_ac = NULL; 1827 int want_clusters = 0; 1828 int xattr_credits = 0; 1829 struct ocfs2_security_xattr_info si = { 1830 .name = NULL, 1831 .enable = 1, 1832 }; 1833 int did_quota = 0, did_quota_inode = 0; 1834 struct ocfs2_dir_lookup_result lookup = { NULL, }; 1835 sigset_t oldset; 1836 int did_block_signals = 0; 1837 struct ocfs2_dentry_lock *dl = NULL; 1838 1839 trace_ocfs2_symlink_begin(dir, dentry, symname, 1840 dentry->d_name.len, dentry->d_name.name); 1841 1842 status = dquot_initialize(dir); 1843 if (status) { 1844 mlog_errno(status); 1845 goto bail; 1846 } 1847 1848 sb = dir->i_sb; 1849 osb = OCFS2_SB(sb); 1850 1851 l = strlen(symname) + 1; 1852 1853 credits = ocfs2_calc_symlink_credits(sb); 1854 1855 /* lock the parent directory */ 1856 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1); 1857 if (status < 0) { 1858 if (status != -ENOENT) 1859 mlog_errno(status); 1860 return status; 1861 } 1862 1863 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data; 1864 if (!ocfs2_read_links_count(dirfe)) { 1865 /* can't make a file in a deleted directory. */ 1866 status = -ENOENT; 1867 goto bail; 1868 } 1869 1870 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name, 1871 dentry->d_name.len); 1872 if (status) 1873 goto bail; 1874 1875 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh, 1876 dentry->d_name.name, 1877 dentry->d_name.len, &lookup); 1878 if (status < 0) { 1879 mlog_errno(status); 1880 goto bail; 1881 } 1882 1883 status = ocfs2_reserve_new_inode(osb, &inode_ac); 1884 if (status < 0) { 1885 if (status != -ENOSPC) 1886 mlog_errno(status); 1887 goto bail; 1888 } 1889 1890 inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO); 1891 if (IS_ERR(inode)) { 1892 status = PTR_ERR(inode); 1893 inode = NULL; 1894 mlog_errno(status); 1895 goto bail; 1896 } 1897 1898 /* get security xattr */ 1899 status = ocfs2_init_security_get(inode, dir, &dentry->d_name, &si); 1900 if (status) { 1901 if (status == -EOPNOTSUPP) 1902 si.enable = 0; 1903 else { 1904 mlog_errno(status); 1905 goto bail; 1906 } 1907 } 1908 1909 /* calculate meta data/clusters for setting security xattr */ 1910 if (si.enable) { 1911 status = ocfs2_calc_security_init(dir, &si, &want_clusters, 1912 &xattr_credits, &xattr_ac); 1913 if (status < 0) { 1914 mlog_errno(status); 1915 goto bail; 1916 } 1917 } 1918 1919 /* don't reserve bitmap space for fast symlinks. */ 1920 if (l > ocfs2_fast_symlink_chars(sb)) 1921 want_clusters += 1; 1922 1923 status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac); 1924 if (status < 0) { 1925 if (status != -ENOSPC) 1926 mlog_errno(status); 1927 goto bail; 1928 } 1929 1930 handle = ocfs2_start_trans(osb, credits + xattr_credits); 1931 if (IS_ERR(handle)) { 1932 status = PTR_ERR(handle); 1933 handle = NULL; 1934 mlog_errno(status); 1935 goto bail; 1936 } 1937 1938 /* Starting to change things, restart is no longer possible. */ 1939 ocfs2_block_signals(&oldset); 1940 did_block_signals = 1; 1941 1942 status = dquot_alloc_inode(inode); 1943 if (status) 1944 goto bail; 1945 did_quota_inode = 1; 1946 1947 trace_ocfs2_symlink_create(dir, dentry, dentry->d_name.len, 1948 dentry->d_name.name, 1949 (unsigned long long)OCFS2_I(dir)->ip_blkno, 1950 inode->i_mode); 1951 1952 status = ocfs2_mknod_locked(osb, dir, inode, 1953 0, &new_fe_bh, parent_fe_bh, handle, 1954 inode_ac); 1955 if (status < 0) { 1956 mlog_errno(status); 1957 goto bail; 1958 } 1959 1960 fe = (struct ocfs2_dinode *) new_fe_bh->b_data; 1961 inode->i_rdev = 0; 1962 newsize = l - 1; 1963 inode->i_op = &ocfs2_symlink_inode_operations; 1964 inode_nohighmem(inode); 1965 if (l > ocfs2_fast_symlink_chars(sb)) { 1966 u32 offset = 0; 1967 1968 status = dquot_alloc_space_nodirty(inode, 1969 ocfs2_clusters_to_bytes(osb->sb, 1)); 1970 if (status) 1971 goto bail; 1972 did_quota = 1; 1973 inode->i_mapping->a_ops = &ocfs2_aops; 1974 status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0, 1975 new_fe_bh, 1976 handle, data_ac, NULL, 1977 NULL); 1978 if (status < 0) { 1979 if (status != -ENOSPC && status != -EINTR) { 1980 mlog(ML_ERROR, 1981 "Failed to extend file to %llu\n", 1982 (unsigned long long)newsize); 1983 mlog_errno(status); 1984 status = -ENOSPC; 1985 } 1986 goto bail; 1987 } 1988 i_size_write(inode, newsize); 1989 inode->i_blocks = ocfs2_inode_sector_count(inode); 1990 } else { 1991 inode->i_mapping->a_ops = &ocfs2_fast_symlink_aops; 1992 memcpy((char *) fe->id2.i_symlink, symname, l); 1993 i_size_write(inode, newsize); 1994 inode->i_blocks = 0; 1995 } 1996 1997 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh); 1998 if (status < 0) { 1999 mlog_errno(status); 2000 goto bail; 2001 } 2002 2003 if (!ocfs2_inode_is_fast_symlink(inode)) { 2004 status = ocfs2_create_symlink_data(osb, handle, inode, 2005 symname); 2006 if (status < 0) { 2007 mlog_errno(status); 2008 goto bail; 2009 } 2010 } 2011 2012 if (si.enable) { 2013 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si, 2014 xattr_ac, data_ac); 2015 if (status < 0) { 2016 mlog_errno(status); 2017 goto bail; 2018 } 2019 } 2020 2021 /* 2022 * Do this before adding the entry to the directory. We add 2023 * also set d_op after success so that ->d_iput() will cleanup 2024 * the dentry lock even if ocfs2_add_entry() fails below. 2025 */ 2026 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno); 2027 if (status) { 2028 mlog_errno(status); 2029 goto bail; 2030 } 2031 2032 dl = dentry->d_fsdata; 2033 2034 status = ocfs2_add_entry(handle, dentry, inode, 2035 le64_to_cpu(fe->i_blkno), parent_fe_bh, 2036 &lookup); 2037 if (status < 0) { 2038 mlog_errno(status); 2039 goto bail; 2040 } 2041 2042 insert_inode_hash(inode); 2043 d_instantiate(dentry, inode); 2044 bail: 2045 if (status < 0 && did_quota) 2046 dquot_free_space_nodirty(inode, 2047 ocfs2_clusters_to_bytes(osb->sb, 1)); 2048 if (status < 0 && did_quota_inode) 2049 dquot_free_inode(inode); 2050 if (handle) { 2051 if (status < 0 && fe) 2052 ocfs2_set_links_count(fe, 0); 2053 ocfs2_commit_trans(osb, handle); 2054 } 2055 2056 ocfs2_inode_unlock(dir, 1); 2057 if (did_block_signals) 2058 ocfs2_unblock_signals(&oldset); 2059 2060 brelse(new_fe_bh); 2061 brelse(parent_fe_bh); 2062 kfree(si.value); 2063 ocfs2_free_dir_lookup_result(&lookup); 2064 if (inode_ac) 2065 ocfs2_free_alloc_context(inode_ac); 2066 if (data_ac) 2067 ocfs2_free_alloc_context(data_ac); 2068 if (xattr_ac) 2069 ocfs2_free_alloc_context(xattr_ac); 2070 if ((status < 0) && inode) { 2071 if (dl) 2072 ocfs2_cleanup_add_entry_failure(osb, dentry, inode); 2073 2074 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR; 2075 clear_nlink(inode); 2076 iput(inode); 2077 } 2078 2079 if (status) 2080 mlog_errno(status); 2081 2082 return status; 2083 } 2084 2085 static int ocfs2_blkno_stringify(u64 blkno, char *name) 2086 { 2087 int status, namelen; 2088 2089 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx", 2090 (long long)blkno); 2091 if (namelen <= 0) { 2092 if (namelen) 2093 status = namelen; 2094 else 2095 status = -EINVAL; 2096 mlog_errno(status); 2097 goto bail; 2098 } 2099 if (namelen != OCFS2_ORPHAN_NAMELEN) { 2100 status = -EINVAL; 2101 mlog_errno(status); 2102 goto bail; 2103 } 2104 2105 trace_ocfs2_blkno_stringify(blkno, name, namelen); 2106 2107 status = 0; 2108 bail: 2109 if (status < 0) 2110 mlog_errno(status); 2111 return status; 2112 } 2113 2114 static int ocfs2_lookup_lock_orphan_dir(struct ocfs2_super *osb, 2115 struct inode **ret_orphan_dir, 2116 struct buffer_head **ret_orphan_dir_bh) 2117 { 2118 struct inode *orphan_dir_inode; 2119 struct buffer_head *orphan_dir_bh = NULL; 2120 int ret = 0; 2121 2122 orphan_dir_inode = ocfs2_get_system_file_inode(osb, 2123 ORPHAN_DIR_SYSTEM_INODE, 2124 osb->slot_num); 2125 if (!orphan_dir_inode) { 2126 ret = -ENOENT; 2127 mlog_errno(ret); 2128 return ret; 2129 } 2130 2131 inode_lock(orphan_dir_inode); 2132 2133 ret = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1); 2134 if (ret < 0) { 2135 inode_unlock(orphan_dir_inode); 2136 iput(orphan_dir_inode); 2137 2138 mlog_errno(ret); 2139 return ret; 2140 } 2141 2142 *ret_orphan_dir = orphan_dir_inode; 2143 *ret_orphan_dir_bh = orphan_dir_bh; 2144 2145 return 0; 2146 } 2147 2148 static int __ocfs2_prepare_orphan_dir(struct inode *orphan_dir_inode, 2149 struct buffer_head *orphan_dir_bh, 2150 u64 blkno, 2151 char *name, 2152 struct ocfs2_dir_lookup_result *lookup, 2153 bool dio) 2154 { 2155 int ret; 2156 struct ocfs2_super *osb = OCFS2_SB(orphan_dir_inode->i_sb); 2157 int namelen = dio ? 2158 (OCFS2_DIO_ORPHAN_PREFIX_LEN + OCFS2_ORPHAN_NAMELEN) : 2159 OCFS2_ORPHAN_NAMELEN; 2160 2161 if (dio) { 2162 ret = snprintf(name, OCFS2_DIO_ORPHAN_PREFIX_LEN + 1, "%s", 2163 OCFS2_DIO_ORPHAN_PREFIX); 2164 if (ret != OCFS2_DIO_ORPHAN_PREFIX_LEN) { 2165 ret = -EINVAL; 2166 mlog_errno(ret); 2167 return ret; 2168 } 2169 2170 ret = ocfs2_blkno_stringify(blkno, 2171 name + OCFS2_DIO_ORPHAN_PREFIX_LEN); 2172 } else 2173 ret = ocfs2_blkno_stringify(blkno, name); 2174 if (ret < 0) { 2175 mlog_errno(ret); 2176 return ret; 2177 } 2178 2179 ret = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode, 2180 orphan_dir_bh, name, 2181 namelen, lookup); 2182 if (ret < 0) { 2183 mlog_errno(ret); 2184 return ret; 2185 } 2186 2187 return 0; 2188 } 2189 2190 /** 2191 * ocfs2_prepare_orphan_dir() - Prepare an orphan directory for 2192 * insertion of an orphan. 2193 * @osb: ocfs2 file system 2194 * @ret_orphan_dir: Orphan dir inode - returned locked! 2195 * @blkno: Actual block number of the inode to be inserted into orphan dir. 2196 * @name: Buffer to store the name of the orphan. 2197 * @lookup: dir lookup result, to be passed back into functions like 2198 * ocfs2_orphan_add 2199 * @dio: Flag indicating if direct IO is being used or not. 2200 * 2201 * Returns zero on success and the ret_orphan_dir, name and lookup 2202 * fields will be populated. 2203 * 2204 * Returns non-zero on failure. 2205 */ 2206 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb, 2207 struct inode **ret_orphan_dir, 2208 u64 blkno, 2209 char *name, 2210 struct ocfs2_dir_lookup_result *lookup, 2211 bool dio) 2212 { 2213 struct inode *orphan_dir_inode = NULL; 2214 struct buffer_head *orphan_dir_bh = NULL; 2215 int ret = 0; 2216 2217 ret = ocfs2_lookup_lock_orphan_dir(osb, &orphan_dir_inode, 2218 &orphan_dir_bh); 2219 if (ret < 0) { 2220 mlog_errno(ret); 2221 return ret; 2222 } 2223 2224 ret = __ocfs2_prepare_orphan_dir(orphan_dir_inode, orphan_dir_bh, 2225 blkno, name, lookup, dio); 2226 if (ret < 0) { 2227 mlog_errno(ret); 2228 goto out; 2229 } 2230 2231 *ret_orphan_dir = orphan_dir_inode; 2232 2233 out: 2234 brelse(orphan_dir_bh); 2235 2236 if (ret) { 2237 ocfs2_inode_unlock(orphan_dir_inode, 1); 2238 inode_unlock(orphan_dir_inode); 2239 iput(orphan_dir_inode); 2240 } 2241 2242 if (ret) 2243 mlog_errno(ret); 2244 return ret; 2245 } 2246 2247 static int ocfs2_orphan_add(struct ocfs2_super *osb, 2248 handle_t *handle, 2249 struct inode *inode, 2250 struct buffer_head *fe_bh, 2251 char *name, 2252 struct ocfs2_dir_lookup_result *lookup, 2253 struct inode *orphan_dir_inode, 2254 bool dio) 2255 { 2256 struct buffer_head *orphan_dir_bh = NULL; 2257 int status = 0; 2258 struct ocfs2_dinode *orphan_fe; 2259 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data; 2260 int namelen = dio ? 2261 (OCFS2_DIO_ORPHAN_PREFIX_LEN + OCFS2_ORPHAN_NAMELEN) : 2262 OCFS2_ORPHAN_NAMELEN; 2263 2264 trace_ocfs2_orphan_add_begin( 2265 (unsigned long long)OCFS2_I(inode)->ip_blkno); 2266 2267 status = ocfs2_read_inode_block(orphan_dir_inode, &orphan_dir_bh); 2268 if (status < 0) { 2269 mlog_errno(status); 2270 goto leave; 2271 } 2272 2273 status = ocfs2_journal_access_di(handle, 2274 INODE_CACHE(orphan_dir_inode), 2275 orphan_dir_bh, 2276 OCFS2_JOURNAL_ACCESS_WRITE); 2277 if (status < 0) { 2278 mlog_errno(status); 2279 goto leave; 2280 } 2281 2282 /* 2283 * We're going to journal the change of i_flags and i_orphaned_slot. 2284 * It's safe anyway, though some callers may duplicate the journaling. 2285 * Journaling within the func just make the logic look more 2286 * straightforward. 2287 */ 2288 status = ocfs2_journal_access_di(handle, 2289 INODE_CACHE(inode), 2290 fe_bh, 2291 OCFS2_JOURNAL_ACCESS_WRITE); 2292 if (status < 0) { 2293 mlog_errno(status); 2294 goto leave; 2295 } 2296 2297 /* we're a cluster, and nlink can change on disk from 2298 * underneath us... */ 2299 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data; 2300 if (S_ISDIR(inode->i_mode)) 2301 ocfs2_add_links_count(orphan_fe, 1); 2302 set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe)); 2303 ocfs2_journal_dirty(handle, orphan_dir_bh); 2304 2305 status = __ocfs2_add_entry(handle, orphan_dir_inode, name, 2306 namelen, inode, 2307 OCFS2_I(inode)->ip_blkno, 2308 orphan_dir_bh, lookup); 2309 if (status < 0) { 2310 mlog_errno(status); 2311 goto rollback; 2312 } 2313 2314 if (dio) { 2315 /* Update flag OCFS2_DIO_ORPHANED_FL and record the orphan 2316 * slot. 2317 */ 2318 fe->i_flags |= cpu_to_le32(OCFS2_DIO_ORPHANED_FL); 2319 fe->i_dio_orphaned_slot = cpu_to_le16(osb->slot_num); 2320 } else { 2321 fe->i_flags |= cpu_to_le32(OCFS2_ORPHANED_FL); 2322 OCFS2_I(inode)->ip_flags &= ~OCFS2_INODE_SKIP_ORPHAN_DIR; 2323 2324 /* Record which orphan dir our inode now resides 2325 * in. delete_inode will use this to determine which orphan 2326 * dir to lock. */ 2327 fe->i_orphaned_slot = cpu_to_le16(osb->slot_num); 2328 } 2329 2330 ocfs2_journal_dirty(handle, fe_bh); 2331 2332 trace_ocfs2_orphan_add_end((unsigned long long)OCFS2_I(inode)->ip_blkno, 2333 osb->slot_num); 2334 2335 rollback: 2336 if (status < 0) { 2337 if (S_ISDIR(inode->i_mode)) 2338 ocfs2_add_links_count(orphan_fe, -1); 2339 set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe)); 2340 } 2341 2342 leave: 2343 brelse(orphan_dir_bh); 2344 2345 return status; 2346 } 2347 2348 /* unlike orphan_add, we expect the orphan dir to already be locked here. */ 2349 int ocfs2_orphan_del(struct ocfs2_super *osb, 2350 handle_t *handle, 2351 struct inode *orphan_dir_inode, 2352 struct inode *inode, 2353 struct buffer_head *orphan_dir_bh, 2354 bool dio) 2355 { 2356 char name[OCFS2_DIO_ORPHAN_PREFIX_LEN + OCFS2_ORPHAN_NAMELEN + 1]; 2357 struct ocfs2_dinode *orphan_fe; 2358 int status = 0; 2359 struct ocfs2_dir_lookup_result lookup = { NULL, }; 2360 2361 if (dio) { 2362 status = snprintf(name, OCFS2_DIO_ORPHAN_PREFIX_LEN + 1, "%s", 2363 OCFS2_DIO_ORPHAN_PREFIX); 2364 if (status != OCFS2_DIO_ORPHAN_PREFIX_LEN) { 2365 status = -EINVAL; 2366 mlog_errno(status); 2367 return status; 2368 } 2369 2370 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, 2371 name + OCFS2_DIO_ORPHAN_PREFIX_LEN); 2372 } else 2373 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name); 2374 if (status < 0) { 2375 mlog_errno(status); 2376 goto leave; 2377 } 2378 2379 trace_ocfs2_orphan_del( 2380 (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno, 2381 name, strlen(name)); 2382 2383 status = ocfs2_journal_access_di(handle, 2384 INODE_CACHE(orphan_dir_inode), 2385 orphan_dir_bh, 2386 OCFS2_JOURNAL_ACCESS_WRITE); 2387 if (status < 0) { 2388 mlog_errno(status); 2389 goto leave; 2390 } 2391 2392 /* find it's spot in the orphan directory */ 2393 status = ocfs2_find_entry(name, strlen(name), orphan_dir_inode, 2394 &lookup); 2395 if (status) { 2396 mlog_errno(status); 2397 goto leave; 2398 } 2399 2400 /* remove it from the orphan directory */ 2401 status = ocfs2_delete_entry(handle, orphan_dir_inode, &lookup); 2402 if (status < 0) { 2403 mlog_errno(status); 2404 goto leave; 2405 } 2406 2407 /* do the i_nlink dance! :) */ 2408 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data; 2409 if (S_ISDIR(inode->i_mode)) 2410 ocfs2_add_links_count(orphan_fe, -1); 2411 set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe)); 2412 ocfs2_journal_dirty(handle, orphan_dir_bh); 2413 2414 leave: 2415 ocfs2_free_dir_lookup_result(&lookup); 2416 2417 if (status) 2418 mlog_errno(status); 2419 return status; 2420 } 2421 2422 /** 2423 * ocfs2_prep_new_orphaned_file() - Prepare the orphan dir to receive a newly 2424 * allocated file. This is different from the typical 'add to orphan dir' 2425 * operation in that the inode does not yet exist. This is a problem because 2426 * the orphan dir stringifies the inode block number to come up with it's 2427 * dirent. Obviously if the inode does not yet exist we have a chicken and egg 2428 * problem. This function works around it by calling deeper into the orphan 2429 * and suballoc code than other callers. Use this only by necessity. 2430 * @dir: The directory which this inode will ultimately wind up under - not the 2431 * orphan dir! 2432 * @dir_bh: buffer_head the @dir inode block 2433 * @orphan_name: string of length (CFS2_ORPHAN_NAMELEN + 1). Will be filled 2434 * with the string to be used for orphan dirent. Pass back to the orphan dir 2435 * code. 2436 * @ret_orphan_dir: orphan dir inode returned to be passed back into orphan 2437 * dir code. 2438 * @ret_di_blkno: block number where the new inode will be allocated. 2439 * @orphan_insert: Dir insert context to be passed back into orphan dir code. 2440 * @ret_inode_ac: Inode alloc context to be passed back to the allocator. 2441 * 2442 * Returns zero on success and the ret_orphan_dir, name and lookup 2443 * fields will be populated. 2444 * 2445 * Returns non-zero on failure. 2446 */ 2447 static int ocfs2_prep_new_orphaned_file(struct inode *dir, 2448 struct buffer_head *dir_bh, 2449 char *orphan_name, 2450 struct inode **ret_orphan_dir, 2451 u64 *ret_di_blkno, 2452 struct ocfs2_dir_lookup_result *orphan_insert, 2453 struct ocfs2_alloc_context **ret_inode_ac) 2454 { 2455 int ret; 2456 u64 di_blkno; 2457 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); 2458 struct inode *orphan_dir = NULL; 2459 struct buffer_head *orphan_dir_bh = NULL; 2460 struct ocfs2_alloc_context *inode_ac = NULL; 2461 2462 ret = ocfs2_lookup_lock_orphan_dir(osb, &orphan_dir, &orphan_dir_bh); 2463 if (ret < 0) { 2464 mlog_errno(ret); 2465 return ret; 2466 } 2467 2468 /* reserve an inode spot */ 2469 ret = ocfs2_reserve_new_inode(osb, &inode_ac); 2470 if (ret < 0) { 2471 if (ret != -ENOSPC) 2472 mlog_errno(ret); 2473 goto out; 2474 } 2475 2476 ret = ocfs2_find_new_inode_loc(dir, dir_bh, inode_ac, 2477 &di_blkno); 2478 if (ret) { 2479 mlog_errno(ret); 2480 goto out; 2481 } 2482 2483 ret = __ocfs2_prepare_orphan_dir(orphan_dir, orphan_dir_bh, 2484 di_blkno, orphan_name, orphan_insert, 2485 false); 2486 if (ret < 0) { 2487 mlog_errno(ret); 2488 goto out; 2489 } 2490 2491 out: 2492 if (ret == 0) { 2493 *ret_orphan_dir = orphan_dir; 2494 *ret_di_blkno = di_blkno; 2495 *ret_inode_ac = inode_ac; 2496 /* 2497 * orphan_name and orphan_insert are already up to 2498 * date via prepare_orphan_dir 2499 */ 2500 } else { 2501 /* Unroll reserve_new_inode* */ 2502 if (inode_ac) 2503 ocfs2_free_alloc_context(inode_ac); 2504 2505 /* Unroll orphan dir locking */ 2506 inode_unlock(orphan_dir); 2507 ocfs2_inode_unlock(orphan_dir, 1); 2508 iput(orphan_dir); 2509 } 2510 2511 brelse(orphan_dir_bh); 2512 2513 return ret; 2514 } 2515 2516 int ocfs2_create_inode_in_orphan(struct inode *dir, 2517 int mode, 2518 struct inode **new_inode) 2519 { 2520 int status, did_quota_inode = 0; 2521 struct inode *inode = NULL; 2522 struct inode *orphan_dir = NULL; 2523 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); 2524 handle_t *handle = NULL; 2525 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1]; 2526 struct buffer_head *parent_di_bh = NULL; 2527 struct buffer_head *new_di_bh = NULL; 2528 struct ocfs2_alloc_context *inode_ac = NULL; 2529 struct ocfs2_dir_lookup_result orphan_insert = { NULL, }; 2530 u64 di_blkno, suballoc_loc; 2531 u16 suballoc_bit; 2532 2533 status = ocfs2_inode_lock(dir, &parent_di_bh, 1); 2534 if (status < 0) { 2535 if (status != -ENOENT) 2536 mlog_errno(status); 2537 return status; 2538 } 2539 2540 status = ocfs2_prep_new_orphaned_file(dir, parent_di_bh, 2541 orphan_name, &orphan_dir, 2542 &di_blkno, &orphan_insert, &inode_ac); 2543 if (status < 0) { 2544 if (status != -ENOSPC) 2545 mlog_errno(status); 2546 goto leave; 2547 } 2548 2549 inode = ocfs2_get_init_inode(dir, mode); 2550 if (IS_ERR(inode)) { 2551 status = PTR_ERR(inode); 2552 inode = NULL; 2553 mlog_errno(status); 2554 goto leave; 2555 } 2556 2557 handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb, 0, 0)); 2558 if (IS_ERR(handle)) { 2559 status = PTR_ERR(handle); 2560 handle = NULL; 2561 mlog_errno(status); 2562 goto leave; 2563 } 2564 2565 status = dquot_alloc_inode(inode); 2566 if (status) 2567 goto leave; 2568 did_quota_inode = 1; 2569 2570 status = ocfs2_claim_new_inode_at_loc(handle, dir, inode_ac, 2571 &suballoc_loc, 2572 &suballoc_bit, di_blkno); 2573 if (status < 0) { 2574 mlog_errno(status); 2575 goto leave; 2576 } 2577 2578 clear_nlink(inode); 2579 /* do the real work now. */ 2580 status = __ocfs2_mknod_locked(dir, inode, 2581 0, &new_di_bh, handle, 2582 inode_ac, di_blkno, suballoc_loc, 2583 suballoc_bit); 2584 if (status < 0) { 2585 mlog_errno(status); 2586 goto leave; 2587 } 2588 2589 status = ocfs2_orphan_add(osb, handle, inode, new_di_bh, orphan_name, 2590 &orphan_insert, orphan_dir, false); 2591 if (status < 0) { 2592 mlog_errno(status); 2593 goto leave; 2594 } 2595 2596 /* get open lock so that only nodes can't remove it from orphan dir. */ 2597 status = ocfs2_open_lock(inode); 2598 if (status < 0) 2599 mlog_errno(status); 2600 2601 insert_inode_hash(inode); 2602 leave: 2603 if (status < 0 && did_quota_inode) 2604 dquot_free_inode(inode); 2605 if (handle) 2606 ocfs2_commit_trans(osb, handle); 2607 2608 if (orphan_dir) { 2609 /* This was locked for us in ocfs2_prepare_orphan_dir() */ 2610 ocfs2_inode_unlock(orphan_dir, 1); 2611 inode_unlock(orphan_dir); 2612 iput(orphan_dir); 2613 } 2614 2615 if ((status < 0) && inode) { 2616 clear_nlink(inode); 2617 iput(inode); 2618 } 2619 2620 if (inode_ac) 2621 ocfs2_free_alloc_context(inode_ac); 2622 2623 brelse(new_di_bh); 2624 2625 if (!status) 2626 *new_inode = inode; 2627 2628 ocfs2_free_dir_lookup_result(&orphan_insert); 2629 2630 ocfs2_inode_unlock(dir, 1); 2631 brelse(parent_di_bh); 2632 return status; 2633 } 2634 2635 int ocfs2_add_inode_to_orphan(struct ocfs2_super *osb, 2636 struct inode *inode) 2637 { 2638 char orphan_name[OCFS2_DIO_ORPHAN_PREFIX_LEN + OCFS2_ORPHAN_NAMELEN + 1]; 2639 struct inode *orphan_dir_inode = NULL; 2640 struct ocfs2_dir_lookup_result orphan_insert = { NULL, }; 2641 struct buffer_head *di_bh = NULL; 2642 int status = 0; 2643 handle_t *handle = NULL; 2644 struct ocfs2_dinode *di = NULL; 2645 2646 status = ocfs2_inode_lock(inode, &di_bh, 1); 2647 if (status < 0) { 2648 mlog_errno(status); 2649 goto bail; 2650 } 2651 2652 di = (struct ocfs2_dinode *) di_bh->b_data; 2653 /* 2654 * Another append dio crashed? 2655 * If so, manually recover it first. 2656 */ 2657 if (unlikely(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))) { 2658 status = ocfs2_truncate_file(inode, di_bh, i_size_read(inode)); 2659 if (status < 0) { 2660 if (status != -ENOSPC) 2661 mlog_errno(status); 2662 goto bail_unlock_inode; 2663 } 2664 2665 status = ocfs2_del_inode_from_orphan(osb, inode, di_bh, 0, 0); 2666 if (status < 0) { 2667 mlog_errno(status); 2668 goto bail_unlock_inode; 2669 } 2670 } 2671 2672 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir_inode, 2673 OCFS2_I(inode)->ip_blkno, 2674 orphan_name, 2675 &orphan_insert, 2676 true); 2677 if (status < 0) { 2678 mlog_errno(status); 2679 goto bail_unlock_inode; 2680 } 2681 2682 handle = ocfs2_start_trans(osb, 2683 OCFS2_INODE_ADD_TO_ORPHAN_CREDITS); 2684 if (IS_ERR(handle)) { 2685 status = PTR_ERR(handle); 2686 goto bail_unlock_orphan; 2687 } 2688 2689 status = ocfs2_orphan_add(osb, handle, inode, di_bh, orphan_name, 2690 &orphan_insert, orphan_dir_inode, true); 2691 if (status) 2692 mlog_errno(status); 2693 2694 ocfs2_commit_trans(osb, handle); 2695 2696 bail_unlock_orphan: 2697 ocfs2_inode_unlock(orphan_dir_inode, 1); 2698 inode_unlock(orphan_dir_inode); 2699 iput(orphan_dir_inode); 2700 2701 ocfs2_free_dir_lookup_result(&orphan_insert); 2702 2703 bail_unlock_inode: 2704 ocfs2_inode_unlock(inode, 1); 2705 brelse(di_bh); 2706 2707 bail: 2708 return status; 2709 } 2710 2711 int ocfs2_del_inode_from_orphan(struct ocfs2_super *osb, 2712 struct inode *inode, struct buffer_head *di_bh, 2713 int update_isize, loff_t end) 2714 { 2715 struct inode *orphan_dir_inode = NULL; 2716 struct buffer_head *orphan_dir_bh = NULL; 2717 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 2718 handle_t *handle = NULL; 2719 int status = 0; 2720 2721 orphan_dir_inode = ocfs2_get_system_file_inode(osb, 2722 ORPHAN_DIR_SYSTEM_INODE, 2723 le16_to_cpu(di->i_dio_orphaned_slot)); 2724 if (!orphan_dir_inode) { 2725 status = -ENOENT; 2726 mlog_errno(status); 2727 goto bail; 2728 } 2729 2730 inode_lock(orphan_dir_inode); 2731 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1); 2732 if (status < 0) { 2733 inode_unlock(orphan_dir_inode); 2734 iput(orphan_dir_inode); 2735 mlog_errno(status); 2736 goto bail; 2737 } 2738 2739 handle = ocfs2_start_trans(osb, 2740 OCFS2_INODE_DEL_FROM_ORPHAN_CREDITS); 2741 if (IS_ERR(handle)) { 2742 status = PTR_ERR(handle); 2743 goto bail_unlock_orphan; 2744 } 2745 2746 BUG_ON(!(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))); 2747 2748 status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, 2749 inode, orphan_dir_bh, true); 2750 if (status < 0) { 2751 mlog_errno(status); 2752 goto bail_commit; 2753 } 2754 2755 status = ocfs2_journal_access_di(handle, 2756 INODE_CACHE(inode), 2757 di_bh, 2758 OCFS2_JOURNAL_ACCESS_WRITE); 2759 if (status < 0) { 2760 mlog_errno(status); 2761 goto bail_commit; 2762 } 2763 2764 di->i_flags &= ~cpu_to_le32(OCFS2_DIO_ORPHANED_FL); 2765 di->i_dio_orphaned_slot = 0; 2766 2767 if (update_isize) { 2768 status = ocfs2_set_inode_size(handle, inode, di_bh, end); 2769 if (status) 2770 mlog_errno(status); 2771 } else 2772 ocfs2_journal_dirty(handle, di_bh); 2773 2774 bail_commit: 2775 ocfs2_commit_trans(osb, handle); 2776 2777 bail_unlock_orphan: 2778 ocfs2_inode_unlock(orphan_dir_inode, 1); 2779 inode_unlock(orphan_dir_inode); 2780 brelse(orphan_dir_bh); 2781 iput(orphan_dir_inode); 2782 2783 bail: 2784 return status; 2785 } 2786 2787 int ocfs2_mv_orphaned_inode_to_new(struct inode *dir, 2788 struct inode *inode, 2789 struct dentry *dentry) 2790 { 2791 int status = 0; 2792 struct buffer_head *parent_di_bh = NULL; 2793 handle_t *handle = NULL; 2794 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); 2795 struct ocfs2_dinode *dir_di, *di; 2796 struct inode *orphan_dir_inode = NULL; 2797 struct buffer_head *orphan_dir_bh = NULL; 2798 struct buffer_head *di_bh = NULL; 2799 struct ocfs2_dir_lookup_result lookup = { NULL, }; 2800 2801 trace_ocfs2_mv_orphaned_inode_to_new(dir, dentry, 2802 dentry->d_name.len, dentry->d_name.name, 2803 (unsigned long long)OCFS2_I(dir)->ip_blkno, 2804 (unsigned long long)OCFS2_I(inode)->ip_blkno); 2805 2806 status = ocfs2_inode_lock(dir, &parent_di_bh, 1); 2807 if (status < 0) { 2808 if (status != -ENOENT) 2809 mlog_errno(status); 2810 return status; 2811 } 2812 2813 dir_di = (struct ocfs2_dinode *) parent_di_bh->b_data; 2814 if (!dir_di->i_links_count) { 2815 /* can't make a file in a deleted directory. */ 2816 status = -ENOENT; 2817 goto leave; 2818 } 2819 2820 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name, 2821 dentry->d_name.len); 2822 if (status) 2823 goto leave; 2824 2825 /* get a spot inside the dir. */ 2826 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_di_bh, 2827 dentry->d_name.name, 2828 dentry->d_name.len, &lookup); 2829 if (status < 0) { 2830 mlog_errno(status); 2831 goto leave; 2832 } 2833 2834 orphan_dir_inode = ocfs2_get_system_file_inode(osb, 2835 ORPHAN_DIR_SYSTEM_INODE, 2836 osb->slot_num); 2837 if (!orphan_dir_inode) { 2838 status = -ENOENT; 2839 mlog_errno(status); 2840 goto leave; 2841 } 2842 2843 inode_lock(orphan_dir_inode); 2844 2845 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1); 2846 if (status < 0) { 2847 mlog_errno(status); 2848 inode_unlock(orphan_dir_inode); 2849 iput(orphan_dir_inode); 2850 goto leave; 2851 } 2852 2853 status = ocfs2_read_inode_block(inode, &di_bh); 2854 if (status < 0) { 2855 mlog_errno(status); 2856 goto orphan_unlock; 2857 } 2858 2859 handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb)); 2860 if (IS_ERR(handle)) { 2861 status = PTR_ERR(handle); 2862 handle = NULL; 2863 mlog_errno(status); 2864 goto orphan_unlock; 2865 } 2866 2867 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), 2868 di_bh, OCFS2_JOURNAL_ACCESS_WRITE); 2869 if (status < 0) { 2870 mlog_errno(status); 2871 goto out_commit; 2872 } 2873 2874 status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode, 2875 orphan_dir_bh, false); 2876 if (status < 0) { 2877 mlog_errno(status); 2878 goto out_commit; 2879 } 2880 2881 di = (struct ocfs2_dinode *)di_bh->b_data; 2882 di->i_flags &= ~cpu_to_le32(OCFS2_ORPHANED_FL); 2883 di->i_orphaned_slot = 0; 2884 set_nlink(inode, 1); 2885 ocfs2_set_links_count(di, inode->i_nlink); 2886 ocfs2_update_inode_fsync_trans(handle, inode, 1); 2887 ocfs2_journal_dirty(handle, di_bh); 2888 2889 status = ocfs2_add_entry(handle, dentry, inode, 2890 OCFS2_I(inode)->ip_blkno, parent_di_bh, 2891 &lookup); 2892 if (status < 0) { 2893 mlog_errno(status); 2894 goto out_commit; 2895 } 2896 2897 status = ocfs2_dentry_attach_lock(dentry, inode, 2898 OCFS2_I(dir)->ip_blkno); 2899 if (status) { 2900 mlog_errno(status); 2901 goto out_commit; 2902 } 2903 2904 d_instantiate(dentry, inode); 2905 status = 0; 2906 out_commit: 2907 ocfs2_commit_trans(osb, handle); 2908 orphan_unlock: 2909 ocfs2_inode_unlock(orphan_dir_inode, 1); 2910 inode_unlock(orphan_dir_inode); 2911 iput(orphan_dir_inode); 2912 leave: 2913 2914 ocfs2_inode_unlock(dir, 1); 2915 2916 brelse(di_bh); 2917 brelse(parent_di_bh); 2918 brelse(orphan_dir_bh); 2919 2920 ocfs2_free_dir_lookup_result(&lookup); 2921 2922 if (status) 2923 mlog_errno(status); 2924 2925 return status; 2926 } 2927 2928 const struct inode_operations ocfs2_dir_iops = { 2929 .create = ocfs2_create, 2930 .lookup = ocfs2_lookup, 2931 .link = ocfs2_link, 2932 .unlink = ocfs2_unlink, 2933 .rmdir = ocfs2_unlink, 2934 .symlink = ocfs2_symlink, 2935 .mkdir = ocfs2_mkdir, 2936 .mknod = ocfs2_mknod, 2937 .rename = ocfs2_rename, 2938 .setattr = ocfs2_setattr, 2939 .getattr = ocfs2_getattr, 2940 .permission = ocfs2_permission, 2941 .listxattr = ocfs2_listxattr, 2942 .fiemap = ocfs2_fiemap, 2943 .get_inode_acl = ocfs2_iop_get_acl, 2944 .set_acl = ocfs2_iop_set_acl, 2945 .fileattr_get = ocfs2_fileattr_get, 2946 .fileattr_set = ocfs2_fileattr_set, 2947 }; 2948