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