0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 #include <linux/fs.h>
0024 #include <linux/types.h>
0025 #include <linux/slab.h>
0026 #include <linux/highmem.h>
0027 #include <linux/quotaops.h>
0028 #include <linux/iversion.h>
0029
0030 #include <cluster/masklog.h>
0031
0032 #include "ocfs2.h"
0033
0034 #include "alloc.h"
0035 #include "dcache.h"
0036 #include "dir.h"
0037 #include "dlmglue.h"
0038 #include "extent_map.h"
0039 #include "file.h"
0040 #include "inode.h"
0041 #include "journal.h"
0042 #include "namei.h"
0043 #include "suballoc.h"
0044 #include "super.h"
0045 #include "symlink.h"
0046 #include "sysfile.h"
0047 #include "uptodate.h"
0048 #include "xattr.h"
0049 #include "acl.h"
0050 #include "ocfs2_trace.h"
0051 #include "ioctl.h"
0052
0053 #include "buffer_head_io.h"
0054
0055 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
0056 struct inode *dir,
0057 struct inode *inode,
0058 dev_t dev,
0059 struct buffer_head **new_fe_bh,
0060 struct buffer_head *parent_fe_bh,
0061 handle_t *handle,
0062 struct ocfs2_alloc_context *inode_ac);
0063
0064 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
0065 struct inode **ret_orphan_dir,
0066 u64 blkno,
0067 char *name,
0068 struct ocfs2_dir_lookup_result *lookup,
0069 bool dio);
0070
0071 static int ocfs2_orphan_add(struct ocfs2_super *osb,
0072 handle_t *handle,
0073 struct inode *inode,
0074 struct buffer_head *fe_bh,
0075 char *name,
0076 struct ocfs2_dir_lookup_result *lookup,
0077 struct inode *orphan_dir_inode,
0078 bool dio);
0079
0080 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
0081 handle_t *handle,
0082 struct inode *inode,
0083 const char *symname);
0084
0085 static int ocfs2_double_lock(struct ocfs2_super *osb,
0086 struct buffer_head **bh1,
0087 struct inode *inode1,
0088 struct buffer_head **bh2,
0089 struct inode *inode2,
0090 int rename);
0091
0092 static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2);
0093
0094 #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
0095
0096 static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
0097 unsigned int flags)
0098 {
0099 int status;
0100 u64 blkno;
0101 struct inode *inode = NULL;
0102 struct dentry *ret;
0103 struct ocfs2_inode_info *oi;
0104
0105 trace_ocfs2_lookup(dir, dentry, dentry->d_name.len,
0106 dentry->d_name.name,
0107 (unsigned long long)OCFS2_I(dir)->ip_blkno, 0);
0108
0109 if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
0110 ret = ERR_PTR(-ENAMETOOLONG);
0111 goto bail;
0112 }
0113
0114 status = ocfs2_inode_lock_nested(dir, NULL, 0, OI_LS_PARENT);
0115 if (status < 0) {
0116 if (status != -ENOENT)
0117 mlog_errno(status);
0118 ret = ERR_PTR(status);
0119 goto bail;
0120 }
0121
0122 status = ocfs2_lookup_ino_from_name(dir, dentry->d_name.name,
0123 dentry->d_name.len, &blkno);
0124 if (status < 0)
0125 goto bail_add;
0126
0127 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0);
0128 if (IS_ERR(inode)) {
0129 ret = ERR_PTR(-EACCES);
0130 goto bail_unlock;
0131 }
0132
0133 oi = OCFS2_I(inode);
0134
0135
0136
0137
0138
0139 spin_lock(&oi->ip_lock);
0140 oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
0141 spin_unlock(&oi->ip_lock);
0142
0143 bail_add:
0144 ret = d_splice_alias(inode, dentry);
0145
0146 if (inode) {
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157 if (!IS_ERR_OR_NULL(ret))
0158 dentry = ret;
0159
0160 status = ocfs2_dentry_attach_lock(dentry, inode,
0161 OCFS2_I(dir)->ip_blkno);
0162 if (status) {
0163 mlog_errno(status);
0164 ret = ERR_PTR(status);
0165 goto bail_unlock;
0166 }
0167 } else
0168 ocfs2_dentry_attach_gen(dentry);
0169
0170 bail_unlock:
0171
0172
0173
0174
0175 ocfs2_inode_unlock(dir, 0);
0176
0177 bail:
0178
0179 trace_ocfs2_lookup_ret(ret);
0180
0181 return ret;
0182 }
0183
0184 static struct inode *ocfs2_get_init_inode(struct inode *dir, umode_t mode)
0185 {
0186 struct inode *inode;
0187 int status;
0188
0189 inode = new_inode(dir->i_sb);
0190 if (!inode) {
0191 mlog(ML_ERROR, "new_inode failed!\n");
0192 return ERR_PTR(-ENOMEM);
0193 }
0194
0195
0196
0197
0198 if (S_ISDIR(mode))
0199 set_nlink(inode, 2);
0200 mode = mode_strip_sgid(&init_user_ns, dir, mode);
0201 inode_init_owner(&init_user_ns, inode, dir, mode);
0202 status = dquot_initialize(inode);
0203 if (status)
0204 return ERR_PTR(status);
0205
0206 return inode;
0207 }
0208
0209 static void ocfs2_cleanup_add_entry_failure(struct ocfs2_super *osb,
0210 struct dentry *dentry, struct inode *inode)
0211 {
0212 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
0213
0214 ocfs2_simple_drop_lockres(osb, &dl->dl_lockres);
0215 ocfs2_lock_res_free(&dl->dl_lockres);
0216 BUG_ON(dl->dl_count != 1);
0217 spin_lock(&dentry_attach_lock);
0218 dentry->d_fsdata = NULL;
0219 spin_unlock(&dentry_attach_lock);
0220 kfree(dl);
0221 iput(inode);
0222 }
0223
0224 static int ocfs2_mknod(struct user_namespace *mnt_userns,
0225 struct inode *dir,
0226 struct dentry *dentry,
0227 umode_t mode,
0228 dev_t dev)
0229 {
0230 int status = 0;
0231 struct buffer_head *parent_fe_bh = NULL;
0232 handle_t *handle = NULL;
0233 struct ocfs2_super *osb;
0234 struct ocfs2_dinode *dirfe;
0235 struct buffer_head *new_fe_bh = NULL;
0236 struct inode *inode = NULL;
0237 struct ocfs2_alloc_context *inode_ac = NULL;
0238 struct ocfs2_alloc_context *data_ac = NULL;
0239 struct ocfs2_alloc_context *meta_ac = NULL;
0240 int want_clusters = 0;
0241 int want_meta = 0;
0242 int xattr_credits = 0;
0243 struct ocfs2_security_xattr_info si = {
0244 .enable = 1,
0245 };
0246 int did_quota_inode = 0;
0247 struct ocfs2_dir_lookup_result lookup = { NULL, };
0248 sigset_t oldset;
0249 int did_block_signals = 0;
0250 struct ocfs2_dentry_lock *dl = NULL;
0251
0252 trace_ocfs2_mknod(dir, dentry, dentry->d_name.len, dentry->d_name.name,
0253 (unsigned long long)OCFS2_I(dir)->ip_blkno,
0254 (unsigned long)dev, mode);
0255
0256 status = dquot_initialize(dir);
0257 if (status) {
0258 mlog_errno(status);
0259 return status;
0260 }
0261
0262
0263 osb = OCFS2_SB(dir->i_sb);
0264
0265 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
0266 if (status < 0) {
0267 if (status != -ENOENT)
0268 mlog_errno(status);
0269 return status;
0270 }
0271
0272 if (S_ISDIR(mode) && (dir->i_nlink >= ocfs2_link_max(osb))) {
0273 status = -EMLINK;
0274 goto leave;
0275 }
0276
0277 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
0278 if (!ocfs2_read_links_count(dirfe)) {
0279
0280 status = -ENOENT;
0281 goto leave;
0282 }
0283
0284 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
0285 dentry->d_name.len);
0286 if (status)
0287 goto leave;
0288
0289
0290 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
0291 dentry->d_name.name,
0292 dentry->d_name.len, &lookup);
0293 if (status < 0) {
0294 mlog_errno(status);
0295 goto leave;
0296 }
0297
0298
0299 status = ocfs2_reserve_new_inode(osb, &inode_ac);
0300 if (status < 0) {
0301 if (status != -ENOSPC)
0302 mlog_errno(status);
0303 goto leave;
0304 }
0305
0306 inode = ocfs2_get_init_inode(dir, mode);
0307 if (IS_ERR(inode)) {
0308 status = PTR_ERR(inode);
0309 inode = NULL;
0310 mlog_errno(status);
0311 goto leave;
0312 }
0313
0314
0315 status = ocfs2_init_security_get(inode, dir, &dentry->d_name, &si);
0316 if (status) {
0317 if (status == -EOPNOTSUPP)
0318 si.enable = 0;
0319 else {
0320 mlog_errno(status);
0321 goto leave;
0322 }
0323 }
0324
0325
0326 status = ocfs2_calc_xattr_init(dir, parent_fe_bh, mode,
0327 &si, &want_clusters,
0328 &xattr_credits, &want_meta);
0329 if (status < 0) {
0330 mlog_errno(status);
0331 goto leave;
0332 }
0333
0334
0335 if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) {
0336 want_clusters += 1;
0337
0338
0339 if (ocfs2_supports_indexed_dirs(osb))
0340 want_meta++;
0341 }
0342
0343 status = ocfs2_reserve_new_metadata_blocks(osb, want_meta, &meta_ac);
0344 if (status < 0) {
0345 if (status != -ENOSPC)
0346 mlog_errno(status);
0347 goto leave;
0348 }
0349
0350 status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
0351 if (status < 0) {
0352 if (status != -ENOSPC)
0353 mlog_errno(status);
0354 goto leave;
0355 }
0356
0357 handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb,
0358 S_ISDIR(mode),
0359 xattr_credits));
0360 if (IS_ERR(handle)) {
0361 status = PTR_ERR(handle);
0362 handle = NULL;
0363 mlog_errno(status);
0364 goto leave;
0365 }
0366
0367
0368 ocfs2_block_signals(&oldset);
0369 did_block_signals = 1;
0370
0371 status = dquot_alloc_inode(inode);
0372 if (status)
0373 goto leave;
0374 did_quota_inode = 1;
0375
0376
0377 status = ocfs2_mknod_locked(osb, dir, inode, dev,
0378 &new_fe_bh, parent_fe_bh, handle,
0379 inode_ac);
0380 if (status < 0) {
0381 mlog_errno(status);
0382 goto leave;
0383 }
0384
0385 if (S_ISDIR(mode)) {
0386 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
0387 new_fe_bh, data_ac, meta_ac);
0388 if (status < 0) {
0389 mlog_errno(status);
0390 goto leave;
0391 }
0392
0393 status = ocfs2_journal_access_di(handle, INODE_CACHE(dir),
0394 parent_fe_bh,
0395 OCFS2_JOURNAL_ACCESS_WRITE);
0396 if (status < 0) {
0397 mlog_errno(status);
0398 goto leave;
0399 }
0400 ocfs2_add_links_count(dirfe, 1);
0401 ocfs2_journal_dirty(handle, parent_fe_bh);
0402 inc_nlink(dir);
0403 }
0404
0405 status = ocfs2_init_acl(handle, inode, dir, new_fe_bh, parent_fe_bh,
0406 meta_ac, data_ac);
0407
0408 if (status < 0) {
0409 mlog_errno(status);
0410 goto roll_back;
0411 }
0412
0413 if (si.enable) {
0414 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
0415 meta_ac, data_ac);
0416 if (status < 0) {
0417 mlog_errno(status);
0418 goto roll_back;
0419 }
0420 }
0421
0422
0423
0424
0425
0426
0427 status = ocfs2_dentry_attach_lock(dentry, inode,
0428 OCFS2_I(dir)->ip_blkno);
0429 if (status) {
0430 mlog_errno(status);
0431 goto roll_back;
0432 }
0433
0434 dl = dentry->d_fsdata;
0435
0436 status = ocfs2_add_entry(handle, dentry, inode,
0437 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
0438 &lookup);
0439 if (status < 0) {
0440 mlog_errno(status);
0441 goto roll_back;
0442 }
0443
0444 insert_inode_hash(inode);
0445 d_instantiate(dentry, inode);
0446 status = 0;
0447
0448 roll_back:
0449 if (status < 0 && S_ISDIR(mode)) {
0450 ocfs2_add_links_count(dirfe, -1);
0451 drop_nlink(dir);
0452 }
0453
0454 leave:
0455 if (status < 0 && did_quota_inode)
0456 dquot_free_inode(inode);
0457 if (handle)
0458 ocfs2_commit_trans(osb, handle);
0459
0460 ocfs2_inode_unlock(dir, 1);
0461 if (did_block_signals)
0462 ocfs2_unblock_signals(&oldset);
0463
0464 brelse(new_fe_bh);
0465 brelse(parent_fe_bh);
0466 kfree(si.value);
0467
0468 ocfs2_free_dir_lookup_result(&lookup);
0469
0470 if (inode_ac)
0471 ocfs2_free_alloc_context(inode_ac);
0472
0473 if (data_ac)
0474 ocfs2_free_alloc_context(data_ac);
0475
0476 if (meta_ac)
0477 ocfs2_free_alloc_context(meta_ac);
0478
0479
0480
0481
0482
0483
0484 if ((status < 0) && inode) {
0485 if (dl)
0486 ocfs2_cleanup_add_entry_failure(osb, dentry, inode);
0487
0488 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR;
0489 clear_nlink(inode);
0490 iput(inode);
0491 }
0492
0493 if (status)
0494 mlog_errno(status);
0495
0496 return status;
0497 }
0498
0499 static int __ocfs2_mknod_locked(struct inode *dir,
0500 struct inode *inode,
0501 dev_t dev,
0502 struct buffer_head **new_fe_bh,
0503 struct buffer_head *parent_fe_bh,
0504 handle_t *handle,
0505 struct ocfs2_alloc_context *inode_ac,
0506 u64 fe_blkno, u64 suballoc_loc, u16 suballoc_bit)
0507 {
0508 int status = 0;
0509 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
0510 struct ocfs2_dinode *fe = NULL;
0511 struct ocfs2_extent_list *fel;
0512 u16 feat;
0513 struct ocfs2_inode_info *oi = OCFS2_I(inode);
0514 struct timespec64 ts;
0515
0516 *new_fe_bh = NULL;
0517
0518
0519
0520
0521 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
0522 oi->ip_blkno = fe_blkno;
0523 spin_lock(&osb->osb_lock);
0524 inode->i_generation = osb->s_next_generation++;
0525 spin_unlock(&osb->osb_lock);
0526
0527 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
0528 if (!*new_fe_bh) {
0529 status = -ENOMEM;
0530 mlog_errno(status);
0531 goto leave;
0532 }
0533 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), *new_fe_bh);
0534
0535 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
0536 *new_fe_bh,
0537 OCFS2_JOURNAL_ACCESS_CREATE);
0538 if (status < 0) {
0539 mlog_errno(status);
0540 goto leave;
0541 }
0542
0543 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
0544 memset(fe, 0, osb->sb->s_blocksize);
0545
0546 fe->i_generation = cpu_to_le32(inode->i_generation);
0547 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
0548 fe->i_blkno = cpu_to_le64(fe_blkno);
0549 fe->i_suballoc_loc = cpu_to_le64(suballoc_loc);
0550 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
0551 fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
0552 fe->i_uid = cpu_to_le32(i_uid_read(inode));
0553 fe->i_gid = cpu_to_le32(i_gid_read(inode));
0554 fe->i_mode = cpu_to_le16(inode->i_mode);
0555 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
0556 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
0557
0558 ocfs2_set_links_count(fe, inode->i_nlink);
0559
0560 fe->i_last_eb_blk = 0;
0561 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
0562 fe->i_flags |= cpu_to_le32(OCFS2_VALID_FL);
0563 ktime_get_real_ts64(&ts);
0564 fe->i_atime = fe->i_ctime = fe->i_mtime =
0565 cpu_to_le64(ts.tv_sec);
0566 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
0567 cpu_to_le32(ts.tv_nsec);
0568 fe->i_dtime = 0;
0569
0570
0571
0572
0573
0574 feat = le16_to_cpu(fe->i_dyn_features);
0575 if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
0576 fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
0577
0578 fe->id2.i_data.id_count = cpu_to_le16(
0579 ocfs2_max_inline_data_with_xattr(osb->sb, fe));
0580 } else {
0581 fel = &fe->id2.i_list;
0582 fel->l_tree_depth = 0;
0583 fel->l_next_free_rec = 0;
0584 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
0585 }
0586
0587 ocfs2_journal_dirty(handle, *new_fe_bh);
0588
0589 ocfs2_populate_inode(inode, fe, 1);
0590 ocfs2_ci_set_new(osb, INODE_CACHE(inode));
0591 if (!ocfs2_mount_local(osb)) {
0592 status = ocfs2_create_new_inode_locks(inode);
0593 if (status < 0)
0594 mlog_errno(status);
0595 }
0596
0597 ocfs2_update_inode_fsync_trans(handle, inode, 1);
0598
0599 leave:
0600 if (status < 0) {
0601 if (*new_fe_bh) {
0602 brelse(*new_fe_bh);
0603 *new_fe_bh = NULL;
0604 }
0605 }
0606
0607 if (status)
0608 mlog_errno(status);
0609 return status;
0610 }
0611
0612 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
0613 struct inode *dir,
0614 struct inode *inode,
0615 dev_t dev,
0616 struct buffer_head **new_fe_bh,
0617 struct buffer_head *parent_fe_bh,
0618 handle_t *handle,
0619 struct ocfs2_alloc_context *inode_ac)
0620 {
0621 int status = 0;
0622 u64 suballoc_loc, fe_blkno = 0;
0623 u16 suballoc_bit;
0624
0625 *new_fe_bh = NULL;
0626
0627 status = ocfs2_claim_new_inode(handle, dir, parent_fe_bh,
0628 inode_ac, &suballoc_loc,
0629 &suballoc_bit, &fe_blkno);
0630 if (status < 0) {
0631 mlog_errno(status);
0632 return status;
0633 }
0634
0635 status = __ocfs2_mknod_locked(dir, inode, dev, new_fe_bh,
0636 parent_fe_bh, handle, inode_ac,
0637 fe_blkno, suballoc_loc, suballoc_bit);
0638 if (status < 0) {
0639 u64 bg_blkno = ocfs2_which_suballoc_group(fe_blkno, suballoc_bit);
0640 int tmp = ocfs2_free_suballoc_bits(handle, inode_ac->ac_inode,
0641 inode_ac->ac_bh, suballoc_bit, bg_blkno, 1);
0642 if (tmp)
0643 mlog_errno(tmp);
0644 }
0645
0646 return status;
0647 }
0648
0649 static int ocfs2_mkdir(struct user_namespace *mnt_userns,
0650 struct inode *dir,
0651 struct dentry *dentry,
0652 umode_t mode)
0653 {
0654 int ret;
0655
0656 trace_ocfs2_mkdir(dir, dentry, dentry->d_name.len, dentry->d_name.name,
0657 OCFS2_I(dir)->ip_blkno, mode);
0658 ret = ocfs2_mknod(&init_user_ns, dir, dentry, mode | S_IFDIR, 0);
0659 if (ret)
0660 mlog_errno(ret);
0661
0662 return ret;
0663 }
0664
0665 static int ocfs2_create(struct user_namespace *mnt_userns,
0666 struct inode *dir,
0667 struct dentry *dentry,
0668 umode_t mode,
0669 bool excl)
0670 {
0671 int ret;
0672
0673 trace_ocfs2_create(dir, dentry, dentry->d_name.len, dentry->d_name.name,
0674 (unsigned long long)OCFS2_I(dir)->ip_blkno, mode);
0675 ret = ocfs2_mknod(&init_user_ns, dir, dentry, mode | S_IFREG, 0);
0676 if (ret)
0677 mlog_errno(ret);
0678
0679 return ret;
0680 }
0681
0682 static int ocfs2_link(struct dentry *old_dentry,
0683 struct inode *dir,
0684 struct dentry *dentry)
0685 {
0686 handle_t *handle;
0687 struct inode *inode = d_inode(old_dentry);
0688 struct inode *old_dir = d_inode(old_dentry->d_parent);
0689 int err;
0690 struct buffer_head *fe_bh = NULL;
0691 struct buffer_head *old_dir_bh = NULL;
0692 struct buffer_head *parent_fe_bh = NULL;
0693 struct ocfs2_dinode *fe = NULL;
0694 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
0695 struct ocfs2_dir_lookup_result lookup = { NULL, };
0696 sigset_t oldset;
0697 u64 old_de_ino;
0698
0699 trace_ocfs2_link((unsigned long long)OCFS2_I(inode)->ip_blkno,
0700 old_dentry->d_name.len, old_dentry->d_name.name,
0701 dentry->d_name.len, dentry->d_name.name);
0702
0703 if (S_ISDIR(inode->i_mode))
0704 return -EPERM;
0705
0706 err = dquot_initialize(dir);
0707 if (err) {
0708 mlog_errno(err);
0709 return err;
0710 }
0711
0712 err = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
0713 &parent_fe_bh, dir, 0);
0714 if (err < 0) {
0715 if (err != -ENOENT)
0716 mlog_errno(err);
0717 return err;
0718 }
0719
0720
0721
0722 if (!parent_fe_bh) {
0723 if (old_dir_bh) {
0724 parent_fe_bh = old_dir_bh;
0725 get_bh(parent_fe_bh);
0726 } else {
0727 mlog(ML_ERROR, "%s: no old_dir_bh!\n", osb->uuid_str);
0728 err = -EIO;
0729 goto out;
0730 }
0731 }
0732
0733 if (!dir->i_nlink) {
0734 err = -ENOENT;
0735 goto out;
0736 }
0737
0738 err = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
0739 old_dentry->d_name.len, &old_de_ino);
0740 if (err) {
0741 err = -ENOENT;
0742 goto out;
0743 }
0744
0745
0746
0747
0748
0749 if (old_de_ino != OCFS2_I(inode)->ip_blkno) {
0750 err = -ENOENT;
0751 goto out;
0752 }
0753
0754 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
0755 dentry->d_name.len);
0756 if (err)
0757 goto out;
0758
0759 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
0760 dentry->d_name.name,
0761 dentry->d_name.len, &lookup);
0762 if (err < 0) {
0763 mlog_errno(err);
0764 goto out;
0765 }
0766
0767 err = ocfs2_inode_lock(inode, &fe_bh, 1);
0768 if (err < 0) {
0769 if (err != -ENOENT)
0770 mlog_errno(err);
0771 goto out;
0772 }
0773
0774 fe = (struct ocfs2_dinode *) fe_bh->b_data;
0775 if (ocfs2_read_links_count(fe) >= ocfs2_link_max(osb)) {
0776 err = -EMLINK;
0777 goto out_unlock_inode;
0778 }
0779
0780 handle = ocfs2_start_trans(osb, ocfs2_link_credits(osb->sb));
0781 if (IS_ERR(handle)) {
0782 err = PTR_ERR(handle);
0783 handle = NULL;
0784 mlog_errno(err);
0785 goto out_unlock_inode;
0786 }
0787
0788
0789 ocfs2_block_signals(&oldset);
0790
0791 err = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
0792 OCFS2_JOURNAL_ACCESS_WRITE);
0793 if (err < 0) {
0794 mlog_errno(err);
0795 goto out_commit;
0796 }
0797
0798 inc_nlink(inode);
0799 inode->i_ctime = current_time(inode);
0800 ocfs2_set_links_count(fe, inode->i_nlink);
0801 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
0802 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
0803 ocfs2_journal_dirty(handle, fe_bh);
0804
0805 err = ocfs2_add_entry(handle, dentry, inode,
0806 OCFS2_I(inode)->ip_blkno,
0807 parent_fe_bh, &lookup);
0808 if (err) {
0809 ocfs2_add_links_count(fe, -1);
0810 drop_nlink(inode);
0811 mlog_errno(err);
0812 goto out_commit;
0813 }
0814
0815 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
0816 if (err) {
0817 mlog_errno(err);
0818 goto out_commit;
0819 }
0820
0821 ihold(inode);
0822 d_instantiate(dentry, inode);
0823
0824 out_commit:
0825 ocfs2_commit_trans(osb, handle);
0826 ocfs2_unblock_signals(&oldset);
0827 out_unlock_inode:
0828 ocfs2_inode_unlock(inode, 1);
0829
0830 out:
0831 ocfs2_double_unlock(old_dir, dir);
0832
0833 brelse(fe_bh);
0834 brelse(parent_fe_bh);
0835 brelse(old_dir_bh);
0836
0837 ocfs2_free_dir_lookup_result(&lookup);
0838
0839 if (err)
0840 mlog_errno(err);
0841
0842 return err;
0843 }
0844
0845
0846
0847
0848
0849 static int ocfs2_remote_dentry_delete(struct dentry *dentry)
0850 {
0851 int ret;
0852
0853 ret = ocfs2_dentry_lock(dentry, 1);
0854 if (ret)
0855 mlog_errno(ret);
0856 else
0857 ocfs2_dentry_unlock(dentry, 1);
0858
0859 return ret;
0860 }
0861
0862 static inline int ocfs2_inode_is_unlinkable(struct inode *inode)
0863 {
0864 if (S_ISDIR(inode->i_mode)) {
0865 if (inode->i_nlink == 2)
0866 return 1;
0867 return 0;
0868 }
0869
0870 if (inode->i_nlink == 1)
0871 return 1;
0872 return 0;
0873 }
0874
0875 static int ocfs2_unlink(struct inode *dir,
0876 struct dentry *dentry)
0877 {
0878 int status;
0879 int child_locked = 0;
0880 bool is_unlinkable = false;
0881 struct inode *inode = d_inode(dentry);
0882 struct inode *orphan_dir = NULL;
0883 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
0884 u64 blkno;
0885 struct ocfs2_dinode *fe = NULL;
0886 struct buffer_head *fe_bh = NULL;
0887 struct buffer_head *parent_node_bh = NULL;
0888 handle_t *handle = NULL;
0889 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
0890 struct ocfs2_dir_lookup_result lookup = { NULL, };
0891 struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
0892
0893 trace_ocfs2_unlink(dir, dentry, dentry->d_name.len,
0894 dentry->d_name.name,
0895 (unsigned long long)OCFS2_I(dir)->ip_blkno,
0896 (unsigned long long)OCFS2_I(inode)->ip_blkno);
0897
0898 status = dquot_initialize(dir);
0899 if (status) {
0900 mlog_errno(status);
0901 return status;
0902 }
0903
0904 BUG_ON(d_inode(dentry->d_parent) != dir);
0905
0906 if (inode == osb->root_inode)
0907 return -EPERM;
0908
0909 status = ocfs2_inode_lock_nested(dir, &parent_node_bh, 1,
0910 OI_LS_PARENT);
0911 if (status < 0) {
0912 if (status != -ENOENT)
0913 mlog_errno(status);
0914 return status;
0915 }
0916
0917 status = ocfs2_find_files_on_disk(dentry->d_name.name,
0918 dentry->d_name.len, &blkno, dir,
0919 &lookup);
0920 if (status < 0) {
0921 if (status != -ENOENT)
0922 mlog_errno(status);
0923 goto leave;
0924 }
0925
0926 if (OCFS2_I(inode)->ip_blkno != blkno) {
0927 status = -ENOENT;
0928
0929 trace_ocfs2_unlink_noent(
0930 (unsigned long long)OCFS2_I(inode)->ip_blkno,
0931 (unsigned long long)blkno,
0932 OCFS2_I(inode)->ip_flags);
0933 goto leave;
0934 }
0935
0936 status = ocfs2_inode_lock(inode, &fe_bh, 1);
0937 if (status < 0) {
0938 if (status != -ENOENT)
0939 mlog_errno(status);
0940 goto leave;
0941 }
0942 child_locked = 1;
0943
0944 if (S_ISDIR(inode->i_mode)) {
0945 if (inode->i_nlink != 2 || !ocfs2_empty_dir(inode)) {
0946 status = -ENOTEMPTY;
0947 goto leave;
0948 }
0949 }
0950
0951 status = ocfs2_remote_dentry_delete(dentry);
0952 if (status < 0) {
0953
0954
0955 mlog_errno(status);
0956 goto leave;
0957 }
0958
0959 if (ocfs2_inode_is_unlinkable(inode)) {
0960 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
0961 OCFS2_I(inode)->ip_blkno,
0962 orphan_name, &orphan_insert,
0963 false);
0964 if (status < 0) {
0965 mlog_errno(status);
0966 goto leave;
0967 }
0968 is_unlinkable = true;
0969 }
0970
0971 handle = ocfs2_start_trans(osb, ocfs2_unlink_credits(osb->sb));
0972 if (IS_ERR(handle)) {
0973 status = PTR_ERR(handle);
0974 handle = NULL;
0975 mlog_errno(status);
0976 goto leave;
0977 }
0978
0979 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
0980 OCFS2_JOURNAL_ACCESS_WRITE);
0981 if (status < 0) {
0982 mlog_errno(status);
0983 goto leave;
0984 }
0985
0986 fe = (struct ocfs2_dinode *) fe_bh->b_data;
0987
0988
0989 status = ocfs2_delete_entry(handle, dir, &lookup);
0990 if (status < 0) {
0991 mlog_errno(status);
0992 goto leave;
0993 }
0994
0995 if (S_ISDIR(inode->i_mode))
0996 drop_nlink(inode);
0997 drop_nlink(inode);
0998 ocfs2_set_links_count(fe, inode->i_nlink);
0999 ocfs2_journal_dirty(handle, fe_bh);
1000
1001 dir->i_ctime = dir->i_mtime = current_time(dir);
1002 if (S_ISDIR(inode->i_mode))
1003 drop_nlink(dir);
1004
1005 status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
1006 if (status < 0) {
1007 mlog_errno(status);
1008 if (S_ISDIR(inode->i_mode))
1009 inc_nlink(dir);
1010 goto leave;
1011 }
1012
1013 if (is_unlinkable) {
1014 status = ocfs2_orphan_add(osb, handle, inode, fe_bh,
1015 orphan_name, &orphan_insert, orphan_dir, false);
1016 if (status < 0)
1017 mlog_errno(status);
1018 }
1019
1020 leave:
1021 if (handle)
1022 ocfs2_commit_trans(osb, handle);
1023
1024 if (orphan_dir) {
1025
1026 ocfs2_inode_unlock(orphan_dir, 1);
1027 inode_unlock(orphan_dir);
1028 iput(orphan_dir);
1029 }
1030
1031 if (child_locked)
1032 ocfs2_inode_unlock(inode, 1);
1033
1034 ocfs2_inode_unlock(dir, 1);
1035
1036 brelse(fe_bh);
1037 brelse(parent_node_bh);
1038
1039 ocfs2_free_dir_lookup_result(&orphan_insert);
1040 ocfs2_free_dir_lookup_result(&lookup);
1041
1042 if (status && (status != -ENOTEMPTY) && (status != -ENOENT))
1043 mlog_errno(status);
1044
1045 return status;
1046 }
1047
1048 static int ocfs2_check_if_ancestor(struct ocfs2_super *osb,
1049 u64 src_inode_no, u64 dest_inode_no)
1050 {
1051 int ret = 0, i = 0;
1052 u64 parent_inode_no = 0;
1053 u64 child_inode_no = src_inode_no;
1054 struct inode *child_inode;
1055
1056 #define MAX_LOOKUP_TIMES 32
1057 while (1) {
1058 child_inode = ocfs2_iget(osb, child_inode_no, 0, 0);
1059 if (IS_ERR(child_inode)) {
1060 ret = PTR_ERR(child_inode);
1061 break;
1062 }
1063
1064 ret = ocfs2_inode_lock(child_inode, NULL, 0);
1065 if (ret < 0) {
1066 iput(child_inode);
1067 if (ret != -ENOENT)
1068 mlog_errno(ret);
1069 break;
1070 }
1071
1072 ret = ocfs2_lookup_ino_from_name(child_inode, "..", 2,
1073 &parent_inode_no);
1074 ocfs2_inode_unlock(child_inode, 0);
1075 iput(child_inode);
1076 if (ret < 0) {
1077 ret = -ENOENT;
1078 break;
1079 }
1080
1081 if (parent_inode_no == dest_inode_no) {
1082 ret = 1;
1083 break;
1084 }
1085
1086 if (parent_inode_no == osb->root_inode->i_ino) {
1087 ret = 0;
1088 break;
1089 }
1090
1091 child_inode_no = parent_inode_no;
1092
1093 if (++i >= MAX_LOOKUP_TIMES) {
1094 mlog_ratelimited(ML_NOTICE, "max lookup times reached, "
1095 "filesystem may have nested directories, "
1096 "src inode: %llu, dest inode: %llu.\n",
1097 (unsigned long long)src_inode_no,
1098 (unsigned long long)dest_inode_no);
1099 ret = 0;
1100 break;
1101 }
1102 }
1103
1104 return ret;
1105 }
1106
1107
1108
1109
1110
1111 static int ocfs2_double_lock(struct ocfs2_super *osb,
1112 struct buffer_head **bh1,
1113 struct inode *inode1,
1114 struct buffer_head **bh2,
1115 struct inode *inode2,
1116 int rename)
1117 {
1118 int status;
1119 int inode1_is_ancestor, inode2_is_ancestor;
1120 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
1121 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
1122
1123 trace_ocfs2_double_lock((unsigned long long)oi1->ip_blkno,
1124 (unsigned long long)oi2->ip_blkno);
1125
1126 if (*bh1)
1127 *bh1 = NULL;
1128 if (*bh2)
1129 *bh2 = NULL;
1130
1131
1132
1133 if (oi1->ip_blkno != oi2->ip_blkno) {
1134 inode1_is_ancestor = ocfs2_check_if_ancestor(osb, oi2->ip_blkno,
1135 oi1->ip_blkno);
1136 if (inode1_is_ancestor < 0) {
1137 status = inode1_is_ancestor;
1138 goto bail;
1139 }
1140
1141 inode2_is_ancestor = ocfs2_check_if_ancestor(osb, oi1->ip_blkno,
1142 oi2->ip_blkno);
1143 if (inode2_is_ancestor < 0) {
1144 status = inode2_is_ancestor;
1145 goto bail;
1146 }
1147
1148 if ((inode1_is_ancestor == 1) ||
1149 (oi1->ip_blkno < oi2->ip_blkno &&
1150 inode2_is_ancestor == 0)) {
1151
1152 swap(bh2, bh1);
1153 swap(inode2, inode1);
1154 }
1155
1156 status = ocfs2_inode_lock_nested(inode2, bh2, 1,
1157 rename == 1 ? OI_LS_RENAME1 : OI_LS_PARENT);
1158 if (status < 0) {
1159 if (status != -ENOENT)
1160 mlog_errno(status);
1161 goto bail;
1162 }
1163 }
1164
1165
1166 status = ocfs2_inode_lock_nested(inode1, bh1, 1,
1167 rename == 1 ? OI_LS_RENAME2 : OI_LS_PARENT);
1168 if (status < 0) {
1169
1170
1171
1172
1173 if (oi1->ip_blkno != oi2->ip_blkno) {
1174 ocfs2_inode_unlock(inode2, 1);
1175 brelse(*bh2);
1176 *bh2 = NULL;
1177 }
1178
1179 if (status != -ENOENT)
1180 mlog_errno(status);
1181 }
1182
1183 trace_ocfs2_double_lock_end(
1184 (unsigned long long)oi1->ip_blkno,
1185 (unsigned long long)oi2->ip_blkno);
1186
1187 bail:
1188 if (status)
1189 mlog_errno(status);
1190 return status;
1191 }
1192
1193 static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
1194 {
1195 ocfs2_inode_unlock(inode1, 1);
1196
1197 if (inode1 != inode2)
1198 ocfs2_inode_unlock(inode2, 1);
1199 }
1200
1201 static int ocfs2_rename(struct user_namespace *mnt_userns,
1202 struct inode *old_dir,
1203 struct dentry *old_dentry,
1204 struct inode *new_dir,
1205 struct dentry *new_dentry,
1206 unsigned int flags)
1207 {
1208 int status = 0, rename_lock = 0, parents_locked = 0, target_exists = 0;
1209 int old_child_locked = 0, new_child_locked = 0, update_dot_dot = 0;
1210 struct inode *old_inode = d_inode(old_dentry);
1211 struct inode *new_inode = d_inode(new_dentry);
1212 struct inode *orphan_dir = NULL;
1213 struct ocfs2_dinode *newfe = NULL;
1214 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
1215 struct buffer_head *newfe_bh = NULL;
1216 struct buffer_head *old_inode_bh = NULL;
1217 struct ocfs2_super *osb = NULL;
1218 u64 newfe_blkno, old_de_ino;
1219 handle_t *handle = NULL;
1220 struct buffer_head *old_dir_bh = NULL;
1221 struct buffer_head *new_dir_bh = NULL;
1222 u32 old_dir_nlink = old_dir->i_nlink;
1223 struct ocfs2_dinode *old_di;
1224 struct ocfs2_dir_lookup_result old_inode_dot_dot_res = { NULL, };
1225 struct ocfs2_dir_lookup_result target_lookup_res = { NULL, };
1226 struct ocfs2_dir_lookup_result old_entry_lookup = { NULL, };
1227 struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
1228 struct ocfs2_dir_lookup_result target_insert = { NULL, };
1229 bool should_add_orphan = false;
1230
1231 if (flags)
1232 return -EINVAL;
1233
1234
1235
1236
1237 trace_ocfs2_rename(old_dir, old_dentry, new_dir, new_dentry,
1238 old_dentry->d_name.len, old_dentry->d_name.name,
1239 new_dentry->d_name.len, new_dentry->d_name.name);
1240
1241 status = dquot_initialize(old_dir);
1242 if (status) {
1243 mlog_errno(status);
1244 goto bail;
1245 }
1246 status = dquot_initialize(new_dir);
1247 if (status) {
1248 mlog_errno(status);
1249 goto bail;
1250 }
1251
1252 osb = OCFS2_SB(old_dir->i_sb);
1253
1254 if (new_inode) {
1255 if (!igrab(new_inode))
1256 BUG();
1257 }
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270 if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
1271 status = ocfs2_rename_lock(osb);
1272 if (status < 0) {
1273 mlog_errno(status);
1274 goto bail;
1275 }
1276 rename_lock = 1;
1277
1278
1279
1280 status = ocfs2_check_if_ancestor(osb, new_dir->i_ino,
1281 old_inode->i_ino);
1282 if (status < 0) {
1283 mlog_errno(status);
1284 goto bail;
1285 } else if (status == 1) {
1286 status = -EPERM;
1287 trace_ocfs2_rename_not_permitted(
1288 (unsigned long long)old_inode->i_ino,
1289 (unsigned long long)new_dir->i_ino);
1290 goto bail;
1291 }
1292 }
1293
1294
1295 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1296 &new_dir_bh, new_dir, 1);
1297 if (status < 0) {
1298 mlog_errno(status);
1299 goto bail;
1300 }
1301 parents_locked = 1;
1302
1303 if (!new_dir->i_nlink) {
1304 status = -EACCES;
1305 goto bail;
1306 }
1307
1308
1309
1310 if (!new_dir_bh) {
1311 if (old_dir_bh) {
1312 new_dir_bh = old_dir_bh;
1313 get_bh(new_dir_bh);
1314 } else {
1315 mlog(ML_ERROR, "no old_dir_bh!\n");
1316 status = -EIO;
1317 goto bail;
1318 }
1319 }
1320
1321
1322
1323
1324
1325
1326
1327 status = ocfs2_inode_lock_nested(old_inode, &old_inode_bh, 1,
1328 OI_LS_PARENT);
1329 if (status < 0) {
1330 if (status != -ENOENT)
1331 mlog_errno(status);
1332 goto bail;
1333 }
1334 old_child_locked = 1;
1335
1336 status = ocfs2_remote_dentry_delete(old_dentry);
1337 if (status < 0) {
1338 mlog_errno(status);
1339 goto bail;
1340 }
1341
1342 if (S_ISDIR(old_inode->i_mode)) {
1343 u64 old_inode_parent;
1344
1345 update_dot_dot = 1;
1346 status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
1347 old_inode,
1348 &old_inode_dot_dot_res);
1349 if (status) {
1350 status = -EIO;
1351 goto bail;
1352 }
1353
1354 if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
1355 status = -EIO;
1356 goto bail;
1357 }
1358
1359 if (!new_inode && new_dir != old_dir &&
1360 new_dir->i_nlink >= ocfs2_link_max(osb)) {
1361 status = -EMLINK;
1362 goto bail;
1363 }
1364 }
1365
1366 status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
1367 old_dentry->d_name.len,
1368 &old_de_ino);
1369 if (status) {
1370 status = -ENOENT;
1371 goto bail;
1372 }
1373
1374
1375
1376
1377
1378
1379
1380 if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
1381 status = -ENOENT;
1382 goto bail;
1383 }
1384
1385
1386
1387 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1388 new_dentry->d_name.len,
1389 &newfe_blkno, new_dir,
1390 &target_lookup_res);
1391
1392
1393 if ((status < 0) && (status != -ENOENT)) {
1394
1395
1396 mlog_errno(status);
1397 goto bail;
1398 }
1399 if (status == 0)
1400 target_exists = 1;
1401
1402 if (!target_exists && new_inode) {
1403
1404
1405
1406
1407
1408
1409 status = -ENOENT;
1410 goto bail;
1411 }
1412
1413
1414
1415 if (target_exists) {
1416
1417
1418
1419
1420
1421 if (!new_inode) {
1422 status = -EACCES;
1423
1424 trace_ocfs2_rename_target_exists(new_dentry->d_name.len,
1425 new_dentry->d_name.name);
1426 goto bail;
1427 }
1428
1429 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1430 status = -EACCES;
1431
1432 trace_ocfs2_rename_disagree(
1433 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1434 (unsigned long long)newfe_blkno,
1435 OCFS2_I(new_inode)->ip_flags);
1436 goto bail;
1437 }
1438
1439 status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
1440 if (status < 0) {
1441 if (status != -ENOENT)
1442 mlog_errno(status);
1443 goto bail;
1444 }
1445 new_child_locked = 1;
1446
1447 status = ocfs2_remote_dentry_delete(new_dentry);
1448 if (status < 0) {
1449 mlog_errno(status);
1450 goto bail;
1451 }
1452
1453 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1454
1455 trace_ocfs2_rename_over_existing(
1456 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
1457 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1458
1459 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
1460 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
1461 OCFS2_I(new_inode)->ip_blkno,
1462 orphan_name, &orphan_insert,
1463 false);
1464 if (status < 0) {
1465 mlog_errno(status);
1466 goto bail;
1467 }
1468 should_add_orphan = true;
1469 }
1470 } else {
1471 BUG_ON(d_inode(new_dentry->d_parent) != new_dir);
1472
1473 status = ocfs2_check_dir_for_entry(new_dir,
1474 new_dentry->d_name.name,
1475 new_dentry->d_name.len);
1476 if (status)
1477 goto bail;
1478
1479 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1480 new_dentry->d_name.name,
1481 new_dentry->d_name.len,
1482 &target_insert);
1483 if (status < 0) {
1484 mlog_errno(status);
1485 goto bail;
1486 }
1487 }
1488
1489 handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
1490 if (IS_ERR(handle)) {
1491 status = PTR_ERR(handle);
1492 handle = NULL;
1493 mlog_errno(status);
1494 goto bail;
1495 }
1496
1497 if (target_exists) {
1498 if (S_ISDIR(new_inode->i_mode)) {
1499 if (new_inode->i_nlink != 2 ||
1500 !ocfs2_empty_dir(new_inode)) {
1501 status = -ENOTEMPTY;
1502 goto bail;
1503 }
1504 }
1505 status = ocfs2_journal_access_di(handle, INODE_CACHE(new_inode),
1506 newfe_bh,
1507 OCFS2_JOURNAL_ACCESS_WRITE);
1508 if (status < 0) {
1509 mlog_errno(status);
1510 goto bail;
1511 }
1512
1513
1514 status = ocfs2_update_entry(new_dir, handle, &target_lookup_res,
1515 old_inode);
1516 if (status < 0) {
1517 mlog_errno(status);
1518 goto bail;
1519 }
1520 inode_inc_iversion(new_dir);
1521
1522 if (S_ISDIR(new_inode->i_mode))
1523 ocfs2_set_links_count(newfe, 0);
1524 else
1525 ocfs2_add_links_count(newfe, -1);
1526 ocfs2_journal_dirty(handle, newfe_bh);
1527 if (should_add_orphan) {
1528 status = ocfs2_orphan_add(osb, handle, new_inode,
1529 newfe_bh, orphan_name,
1530 &orphan_insert, orphan_dir, false);
1531 if (status < 0) {
1532 mlog_errno(status);
1533 goto bail;
1534 }
1535 }
1536 } else {
1537
1538 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1539 OCFS2_I(old_inode)->ip_blkno,
1540 new_dir_bh, &target_insert);
1541 }
1542
1543 old_inode->i_ctime = current_time(old_inode);
1544 mark_inode_dirty(old_inode);
1545
1546 status = ocfs2_journal_access_di(handle, INODE_CACHE(old_inode),
1547 old_inode_bh,
1548 OCFS2_JOURNAL_ACCESS_WRITE);
1549 if (status >= 0) {
1550 old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
1551
1552 old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
1553 old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
1554 ocfs2_journal_dirty(handle, old_inode_bh);
1555 } else
1556 mlog_errno(status);
1557
1558
1559
1560
1561
1562
1563
1564
1565 status = ocfs2_find_entry(old_dentry->d_name.name,
1566 old_dentry->d_name.len, old_dir,
1567 &old_entry_lookup);
1568 if (status) {
1569 if (!is_journal_aborted(osb->journal->j_journal)) {
1570 ocfs2_error(osb->sb, "new entry %.*s is added, but old entry %.*s "
1571 "is not deleted.",
1572 new_dentry->d_name.len, new_dentry->d_name.name,
1573 old_dentry->d_name.len, old_dentry->d_name.name);
1574 }
1575 goto bail;
1576 }
1577
1578 status = ocfs2_delete_entry(handle, old_dir, &old_entry_lookup);
1579 if (status < 0) {
1580 mlog_errno(status);
1581 if (!is_journal_aborted(osb->journal->j_journal)) {
1582 ocfs2_error(osb->sb, "new entry %.*s is added, but old entry %.*s "
1583 "is not deleted.",
1584 new_dentry->d_name.len, new_dentry->d_name.name,
1585 old_dentry->d_name.len, old_dentry->d_name.name);
1586 }
1587 goto bail;
1588 }
1589
1590 if (new_inode) {
1591 drop_nlink(new_inode);
1592 new_inode->i_ctime = current_time(new_inode);
1593 }
1594 old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
1595
1596 if (update_dot_dot) {
1597 status = ocfs2_update_entry(old_inode, handle,
1598 &old_inode_dot_dot_res, new_dir);
1599 drop_nlink(old_dir);
1600 if (new_inode) {
1601 drop_nlink(new_inode);
1602 } else {
1603 inc_nlink(new_dir);
1604 mark_inode_dirty(new_dir);
1605 }
1606 }
1607 mark_inode_dirty(old_dir);
1608 ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1609 if (new_inode) {
1610 mark_inode_dirty(new_inode);
1611 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1612 }
1613
1614 if (old_dir != new_dir) {
1615
1616 new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
1617
1618
1619
1620
1621
1622 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
1623 }
1624
1625 if (old_dir_nlink != old_dir->i_nlink) {
1626 if (!old_dir_bh) {
1627 mlog(ML_ERROR, "need to change nlink for old dir "
1628 "%llu from %d to %d but bh is NULL!\n",
1629 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1630 (int)old_dir_nlink, old_dir->i_nlink);
1631 } else {
1632 struct ocfs2_dinode *fe;
1633 status = ocfs2_journal_access_di(handle,
1634 INODE_CACHE(old_dir),
1635 old_dir_bh,
1636 OCFS2_JOURNAL_ACCESS_WRITE);
1637 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1638 ocfs2_set_links_count(fe, old_dir->i_nlink);
1639 ocfs2_journal_dirty(handle, old_dir_bh);
1640 }
1641 }
1642 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
1643 status = 0;
1644 bail:
1645 if (handle)
1646 ocfs2_commit_trans(osb, handle);
1647
1648 if (orphan_dir) {
1649
1650 ocfs2_inode_unlock(orphan_dir, 1);
1651 inode_unlock(orphan_dir);
1652 iput(orphan_dir);
1653 }
1654
1655 if (new_child_locked)
1656 ocfs2_inode_unlock(new_inode, 1);
1657
1658 if (old_child_locked)
1659 ocfs2_inode_unlock(old_inode, 1);
1660
1661 if (parents_locked)
1662 ocfs2_double_unlock(old_dir, new_dir);
1663
1664 if (rename_lock)
1665 ocfs2_rename_unlock(osb);
1666
1667 if (new_inode)
1668 sync_mapping_buffers(old_inode->i_mapping);
1669
1670 iput(new_inode);
1671
1672 ocfs2_free_dir_lookup_result(&target_lookup_res);
1673 ocfs2_free_dir_lookup_result(&old_entry_lookup);
1674 ocfs2_free_dir_lookup_result(&old_inode_dot_dot_res);
1675 ocfs2_free_dir_lookup_result(&orphan_insert);
1676 ocfs2_free_dir_lookup_result(&target_insert);
1677
1678 brelse(newfe_bh);
1679 brelse(old_inode_bh);
1680 brelse(old_dir_bh);
1681 brelse(new_dir_bh);
1682
1683 if (status)
1684 mlog_errno(status);
1685
1686 return status;
1687 }
1688
1689
1690
1691
1692
1693 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1694 handle_t *handle,
1695 struct inode *inode,
1696 const char *symname)
1697 {
1698 struct buffer_head **bhs = NULL;
1699 const char *c;
1700 struct super_block *sb = osb->sb;
1701 u64 p_blkno, p_blocks;
1702 int virtual, blocks, status, i, bytes_left;
1703
1704 bytes_left = i_size_read(inode) + 1;
1705
1706
1707 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1708
1709 trace_ocfs2_create_symlink_data((unsigned long long)inode->i_blocks,
1710 i_size_read(inode), blocks);
1711
1712
1713 if (bytes_left >
1714 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1715 status = -EIO;
1716 mlog_errno(status);
1717 goto bail;
1718 }
1719
1720 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1721 if (!bhs) {
1722 status = -ENOMEM;
1723 mlog_errno(status);
1724 goto bail;
1725 }
1726
1727 status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
1728 NULL);
1729 if (status < 0) {
1730 mlog_errno(status);
1731 goto bail;
1732 }
1733
1734
1735
1736
1737 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1738 status = -EIO;
1739 mlog_errno(status);
1740 goto bail;
1741 }
1742
1743 virtual = 0;
1744 while(bytes_left > 0) {
1745 c = &symname[virtual * sb->s_blocksize];
1746
1747 bhs[virtual] = sb_getblk(sb, p_blkno);
1748 if (!bhs[virtual]) {
1749 status = -ENOMEM;
1750 mlog_errno(status);
1751 goto bail;
1752 }
1753 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode),
1754 bhs[virtual]);
1755
1756 status = ocfs2_journal_access(handle, INODE_CACHE(inode),
1757 bhs[virtual],
1758 OCFS2_JOURNAL_ACCESS_CREATE);
1759 if (status < 0) {
1760 mlog_errno(status);
1761 goto bail;
1762 }
1763
1764 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1765
1766 memcpy(bhs[virtual]->b_data, c,
1767 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1768 bytes_left);
1769
1770 ocfs2_journal_dirty(handle, bhs[virtual]);
1771
1772 virtual++;
1773 p_blkno++;
1774 bytes_left -= sb->s_blocksize;
1775 }
1776
1777 status = 0;
1778 bail:
1779
1780 if (bhs) {
1781 for(i = 0; i < blocks; i++)
1782 brelse(bhs[i]);
1783 kfree(bhs);
1784 }
1785
1786 if (status)
1787 mlog_errno(status);
1788 return status;
1789 }
1790
1791 static int ocfs2_symlink(struct user_namespace *mnt_userns,
1792 struct inode *dir,
1793 struct dentry *dentry,
1794 const char *symname)
1795 {
1796 int status, l, credits;
1797 u64 newsize;
1798 struct ocfs2_super *osb = NULL;
1799 struct inode *inode = NULL;
1800 struct super_block *sb;
1801 struct buffer_head *new_fe_bh = NULL;
1802 struct buffer_head *parent_fe_bh = NULL;
1803 struct ocfs2_dinode *fe = NULL;
1804 struct ocfs2_dinode *dirfe;
1805 handle_t *handle = NULL;
1806 struct ocfs2_alloc_context *inode_ac = NULL;
1807 struct ocfs2_alloc_context *data_ac = NULL;
1808 struct ocfs2_alloc_context *xattr_ac = NULL;
1809 int want_clusters = 0;
1810 int xattr_credits = 0;
1811 struct ocfs2_security_xattr_info si = {
1812 .enable = 1,
1813 };
1814 int did_quota = 0, did_quota_inode = 0;
1815 struct ocfs2_dir_lookup_result lookup = { NULL, };
1816 sigset_t oldset;
1817 int did_block_signals = 0;
1818 struct ocfs2_dentry_lock *dl = NULL;
1819
1820 trace_ocfs2_symlink_begin(dir, dentry, symname,
1821 dentry->d_name.len, dentry->d_name.name);
1822
1823 status = dquot_initialize(dir);
1824 if (status) {
1825 mlog_errno(status);
1826 goto bail;
1827 }
1828
1829 sb = dir->i_sb;
1830 osb = OCFS2_SB(sb);
1831
1832 l = strlen(symname) + 1;
1833
1834 credits = ocfs2_calc_symlink_credits(sb);
1835
1836
1837 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
1838 if (status < 0) {
1839 if (status != -ENOENT)
1840 mlog_errno(status);
1841 return status;
1842 }
1843
1844 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1845 if (!ocfs2_read_links_count(dirfe)) {
1846
1847 status = -ENOENT;
1848 goto bail;
1849 }
1850
1851 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1852 dentry->d_name.len);
1853 if (status)
1854 goto bail;
1855
1856 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1857 dentry->d_name.name,
1858 dentry->d_name.len, &lookup);
1859 if (status < 0) {
1860 mlog_errno(status);
1861 goto bail;
1862 }
1863
1864 status = ocfs2_reserve_new_inode(osb, &inode_ac);
1865 if (status < 0) {
1866 if (status != -ENOSPC)
1867 mlog_errno(status);
1868 goto bail;
1869 }
1870
1871 inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
1872 if (IS_ERR(inode)) {
1873 status = PTR_ERR(inode);
1874 inode = NULL;
1875 mlog_errno(status);
1876 goto bail;
1877 }
1878
1879
1880 status = ocfs2_init_security_get(inode, dir, &dentry->d_name, &si);
1881 if (status) {
1882 if (status == -EOPNOTSUPP)
1883 si.enable = 0;
1884 else {
1885 mlog_errno(status);
1886 goto bail;
1887 }
1888 }
1889
1890
1891 if (si.enable) {
1892 status = ocfs2_calc_security_init(dir, &si, &want_clusters,
1893 &xattr_credits, &xattr_ac);
1894 if (status < 0) {
1895 mlog_errno(status);
1896 goto bail;
1897 }
1898 }
1899
1900
1901 if (l > ocfs2_fast_symlink_chars(sb))
1902 want_clusters += 1;
1903
1904 status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
1905 if (status < 0) {
1906 if (status != -ENOSPC)
1907 mlog_errno(status);
1908 goto bail;
1909 }
1910
1911 handle = ocfs2_start_trans(osb, credits + xattr_credits);
1912 if (IS_ERR(handle)) {
1913 status = PTR_ERR(handle);
1914 handle = NULL;
1915 mlog_errno(status);
1916 goto bail;
1917 }
1918
1919
1920 ocfs2_block_signals(&oldset);
1921 did_block_signals = 1;
1922
1923 status = dquot_alloc_inode(inode);
1924 if (status)
1925 goto bail;
1926 did_quota_inode = 1;
1927
1928 trace_ocfs2_symlink_create(dir, dentry, dentry->d_name.len,
1929 dentry->d_name.name,
1930 (unsigned long long)OCFS2_I(dir)->ip_blkno,
1931 inode->i_mode);
1932
1933 status = ocfs2_mknod_locked(osb, dir, inode,
1934 0, &new_fe_bh, parent_fe_bh, handle,
1935 inode_ac);
1936 if (status < 0) {
1937 mlog_errno(status);
1938 goto bail;
1939 }
1940
1941 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1942 inode->i_rdev = 0;
1943 newsize = l - 1;
1944 inode->i_op = &ocfs2_symlink_inode_operations;
1945 inode_nohighmem(inode);
1946 if (l > ocfs2_fast_symlink_chars(sb)) {
1947 u32 offset = 0;
1948
1949 status = dquot_alloc_space_nodirty(inode,
1950 ocfs2_clusters_to_bytes(osb->sb, 1));
1951 if (status)
1952 goto bail;
1953 did_quota = 1;
1954 inode->i_mapping->a_ops = &ocfs2_aops;
1955 status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
1956 new_fe_bh,
1957 handle, data_ac, NULL,
1958 NULL);
1959 if (status < 0) {
1960 if (status != -ENOSPC && status != -EINTR) {
1961 mlog(ML_ERROR,
1962 "Failed to extend file to %llu\n",
1963 (unsigned long long)newsize);
1964 mlog_errno(status);
1965 status = -ENOSPC;
1966 }
1967 goto bail;
1968 }
1969 i_size_write(inode, newsize);
1970 inode->i_blocks = ocfs2_inode_sector_count(inode);
1971 } else {
1972 inode->i_mapping->a_ops = &ocfs2_fast_symlink_aops;
1973 memcpy((char *) fe->id2.i_symlink, symname, l);
1974 i_size_write(inode, newsize);
1975 inode->i_blocks = 0;
1976 }
1977
1978 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1979 if (status < 0) {
1980 mlog_errno(status);
1981 goto bail;
1982 }
1983
1984 if (!ocfs2_inode_is_fast_symlink(inode)) {
1985 status = ocfs2_create_symlink_data(osb, handle, inode,
1986 symname);
1987 if (status < 0) {
1988 mlog_errno(status);
1989 goto bail;
1990 }
1991 }
1992
1993 if (si.enable) {
1994 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
1995 xattr_ac, data_ac);
1996 if (status < 0) {
1997 mlog_errno(status);
1998 goto bail;
1999 }
2000 }
2001
2002
2003
2004
2005
2006
2007 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
2008 if (status) {
2009 mlog_errno(status);
2010 goto bail;
2011 }
2012
2013 dl = dentry->d_fsdata;
2014
2015 status = ocfs2_add_entry(handle, dentry, inode,
2016 le64_to_cpu(fe->i_blkno), parent_fe_bh,
2017 &lookup);
2018 if (status < 0) {
2019 mlog_errno(status);
2020 goto bail;
2021 }
2022
2023 insert_inode_hash(inode);
2024 d_instantiate(dentry, inode);
2025 bail:
2026 if (status < 0 && did_quota)
2027 dquot_free_space_nodirty(inode,
2028 ocfs2_clusters_to_bytes(osb->sb, 1));
2029 if (status < 0 && did_quota_inode)
2030 dquot_free_inode(inode);
2031 if (handle)
2032 ocfs2_commit_trans(osb, handle);
2033
2034 ocfs2_inode_unlock(dir, 1);
2035 if (did_block_signals)
2036 ocfs2_unblock_signals(&oldset);
2037
2038 brelse(new_fe_bh);
2039 brelse(parent_fe_bh);
2040 kfree(si.value);
2041 ocfs2_free_dir_lookup_result(&lookup);
2042 if (inode_ac)
2043 ocfs2_free_alloc_context(inode_ac);
2044 if (data_ac)
2045 ocfs2_free_alloc_context(data_ac);
2046 if (xattr_ac)
2047 ocfs2_free_alloc_context(xattr_ac);
2048 if ((status < 0) && inode) {
2049 if (dl)
2050 ocfs2_cleanup_add_entry_failure(osb, dentry, inode);
2051
2052 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR;
2053 clear_nlink(inode);
2054 iput(inode);
2055 }
2056
2057 if (status)
2058 mlog_errno(status);
2059
2060 return status;
2061 }
2062
2063 static int ocfs2_blkno_stringify(u64 blkno, char *name)
2064 {
2065 int status, namelen;
2066
2067 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
2068 (long long)blkno);
2069 if (namelen <= 0) {
2070 if (namelen)
2071 status = namelen;
2072 else
2073 status = -EINVAL;
2074 mlog_errno(status);
2075 goto bail;
2076 }
2077 if (namelen != OCFS2_ORPHAN_NAMELEN) {
2078 status = -EINVAL;
2079 mlog_errno(status);
2080 goto bail;
2081 }
2082
2083 trace_ocfs2_blkno_stringify(blkno, name, namelen);
2084
2085 status = 0;
2086 bail:
2087 if (status < 0)
2088 mlog_errno(status);
2089 return status;
2090 }
2091
2092 static int ocfs2_lookup_lock_orphan_dir(struct ocfs2_super *osb,
2093 struct inode **ret_orphan_dir,
2094 struct buffer_head **ret_orphan_dir_bh)
2095 {
2096 struct inode *orphan_dir_inode;
2097 struct buffer_head *orphan_dir_bh = NULL;
2098 int ret = 0;
2099
2100 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2101 ORPHAN_DIR_SYSTEM_INODE,
2102 osb->slot_num);
2103 if (!orphan_dir_inode) {
2104 ret = -ENOENT;
2105 mlog_errno(ret);
2106 return ret;
2107 }
2108
2109 inode_lock(orphan_dir_inode);
2110
2111 ret = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
2112 if (ret < 0) {
2113 inode_unlock(orphan_dir_inode);
2114 iput(orphan_dir_inode);
2115
2116 mlog_errno(ret);
2117 return ret;
2118 }
2119
2120 *ret_orphan_dir = orphan_dir_inode;
2121 *ret_orphan_dir_bh = orphan_dir_bh;
2122
2123 return 0;
2124 }
2125
2126 static int __ocfs2_prepare_orphan_dir(struct inode *orphan_dir_inode,
2127 struct buffer_head *orphan_dir_bh,
2128 u64 blkno,
2129 char *name,
2130 struct ocfs2_dir_lookup_result *lookup,
2131 bool dio)
2132 {
2133 int ret;
2134 struct ocfs2_super *osb = OCFS2_SB(orphan_dir_inode->i_sb);
2135 int namelen = dio ?
2136 (OCFS2_DIO_ORPHAN_PREFIX_LEN + OCFS2_ORPHAN_NAMELEN) :
2137 OCFS2_ORPHAN_NAMELEN;
2138
2139 if (dio) {
2140 ret = snprintf(name, OCFS2_DIO_ORPHAN_PREFIX_LEN + 1, "%s",
2141 OCFS2_DIO_ORPHAN_PREFIX);
2142 if (ret != OCFS2_DIO_ORPHAN_PREFIX_LEN) {
2143 ret = -EINVAL;
2144 mlog_errno(ret);
2145 return ret;
2146 }
2147
2148 ret = ocfs2_blkno_stringify(blkno,
2149 name + OCFS2_DIO_ORPHAN_PREFIX_LEN);
2150 } else
2151 ret = ocfs2_blkno_stringify(blkno, name);
2152 if (ret < 0) {
2153 mlog_errno(ret);
2154 return ret;
2155 }
2156
2157 ret = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
2158 orphan_dir_bh, name,
2159 namelen, lookup);
2160 if (ret < 0) {
2161 mlog_errno(ret);
2162 return ret;
2163 }
2164
2165 return 0;
2166 }
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
2183 struct inode **ret_orphan_dir,
2184 u64 blkno,
2185 char *name,
2186 struct ocfs2_dir_lookup_result *lookup,
2187 bool dio)
2188 {
2189 struct inode *orphan_dir_inode = NULL;
2190 struct buffer_head *orphan_dir_bh = NULL;
2191 int ret = 0;
2192
2193 ret = ocfs2_lookup_lock_orphan_dir(osb, &orphan_dir_inode,
2194 &orphan_dir_bh);
2195 if (ret < 0) {
2196 mlog_errno(ret);
2197 return ret;
2198 }
2199
2200 ret = __ocfs2_prepare_orphan_dir(orphan_dir_inode, orphan_dir_bh,
2201 blkno, name, lookup, dio);
2202 if (ret < 0) {
2203 mlog_errno(ret);
2204 goto out;
2205 }
2206
2207 *ret_orphan_dir = orphan_dir_inode;
2208
2209 out:
2210 brelse(orphan_dir_bh);
2211
2212 if (ret) {
2213 ocfs2_inode_unlock(orphan_dir_inode, 1);
2214 inode_unlock(orphan_dir_inode);
2215 iput(orphan_dir_inode);
2216 }
2217
2218 if (ret)
2219 mlog_errno(ret);
2220 return ret;
2221 }
2222
2223 static int ocfs2_orphan_add(struct ocfs2_super *osb,
2224 handle_t *handle,
2225 struct inode *inode,
2226 struct buffer_head *fe_bh,
2227 char *name,
2228 struct ocfs2_dir_lookup_result *lookup,
2229 struct inode *orphan_dir_inode,
2230 bool dio)
2231 {
2232 struct buffer_head *orphan_dir_bh = NULL;
2233 int status = 0;
2234 struct ocfs2_dinode *orphan_fe;
2235 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
2236 int namelen = dio ?
2237 (OCFS2_DIO_ORPHAN_PREFIX_LEN + OCFS2_ORPHAN_NAMELEN) :
2238 OCFS2_ORPHAN_NAMELEN;
2239
2240 trace_ocfs2_orphan_add_begin(
2241 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2242
2243 status = ocfs2_read_inode_block(orphan_dir_inode, &orphan_dir_bh);
2244 if (status < 0) {
2245 mlog_errno(status);
2246 goto leave;
2247 }
2248
2249 status = ocfs2_journal_access_di(handle,
2250 INODE_CACHE(orphan_dir_inode),
2251 orphan_dir_bh,
2252 OCFS2_JOURNAL_ACCESS_WRITE);
2253 if (status < 0) {
2254 mlog_errno(status);
2255 goto leave;
2256 }
2257
2258
2259
2260
2261
2262
2263
2264 status = ocfs2_journal_access_di(handle,
2265 INODE_CACHE(inode),
2266 fe_bh,
2267 OCFS2_JOURNAL_ACCESS_WRITE);
2268 if (status < 0) {
2269 mlog_errno(status);
2270 goto leave;
2271 }
2272
2273
2274
2275 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2276 if (S_ISDIR(inode->i_mode))
2277 ocfs2_add_links_count(orphan_fe, 1);
2278 set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
2279 ocfs2_journal_dirty(handle, orphan_dir_bh);
2280
2281 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
2282 namelen, inode,
2283 OCFS2_I(inode)->ip_blkno,
2284 orphan_dir_bh, lookup);
2285 if (status < 0) {
2286 mlog_errno(status);
2287 goto rollback;
2288 }
2289
2290 if (dio) {
2291
2292
2293
2294 fe->i_flags |= cpu_to_le32(OCFS2_DIO_ORPHANED_FL);
2295 fe->i_dio_orphaned_slot = cpu_to_le16(osb->slot_num);
2296 } else {
2297 fe->i_flags |= cpu_to_le32(OCFS2_ORPHANED_FL);
2298 OCFS2_I(inode)->ip_flags &= ~OCFS2_INODE_SKIP_ORPHAN_DIR;
2299
2300
2301
2302
2303 fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
2304 }
2305
2306 ocfs2_journal_dirty(handle, fe_bh);
2307
2308 trace_ocfs2_orphan_add_end((unsigned long long)OCFS2_I(inode)->ip_blkno,
2309 osb->slot_num);
2310
2311 rollback:
2312 if (status < 0) {
2313 if (S_ISDIR(inode->i_mode))
2314 ocfs2_add_links_count(orphan_fe, -1);
2315 set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
2316 }
2317
2318 leave:
2319 brelse(orphan_dir_bh);
2320
2321 return status;
2322 }
2323
2324
2325 int ocfs2_orphan_del(struct ocfs2_super *osb,
2326 handle_t *handle,
2327 struct inode *orphan_dir_inode,
2328 struct inode *inode,
2329 struct buffer_head *orphan_dir_bh,
2330 bool dio)
2331 {
2332 char name[OCFS2_DIO_ORPHAN_PREFIX_LEN + OCFS2_ORPHAN_NAMELEN + 1];
2333 struct ocfs2_dinode *orphan_fe;
2334 int status = 0;
2335 struct ocfs2_dir_lookup_result lookup = { NULL, };
2336
2337 if (dio) {
2338 status = snprintf(name, OCFS2_DIO_ORPHAN_PREFIX_LEN + 1, "%s",
2339 OCFS2_DIO_ORPHAN_PREFIX);
2340 if (status != OCFS2_DIO_ORPHAN_PREFIX_LEN) {
2341 status = -EINVAL;
2342 mlog_errno(status);
2343 return status;
2344 }
2345
2346 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno,
2347 name + OCFS2_DIO_ORPHAN_PREFIX_LEN);
2348 } else
2349 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2350 if (status < 0) {
2351 mlog_errno(status);
2352 goto leave;
2353 }
2354
2355 trace_ocfs2_orphan_del(
2356 (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
2357 name, strlen(name));
2358
2359 status = ocfs2_journal_access_di(handle,
2360 INODE_CACHE(orphan_dir_inode),
2361 orphan_dir_bh,
2362 OCFS2_JOURNAL_ACCESS_WRITE);
2363 if (status < 0) {
2364 mlog_errno(status);
2365 goto leave;
2366 }
2367
2368
2369 status = ocfs2_find_entry(name, strlen(name), orphan_dir_inode,
2370 &lookup);
2371 if (status) {
2372 mlog_errno(status);
2373 goto leave;
2374 }
2375
2376
2377 status = ocfs2_delete_entry(handle, orphan_dir_inode, &lookup);
2378 if (status < 0) {
2379 mlog_errno(status);
2380 goto leave;
2381 }
2382
2383
2384 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2385 if (S_ISDIR(inode->i_mode))
2386 ocfs2_add_links_count(orphan_fe, -1);
2387 set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
2388 ocfs2_journal_dirty(handle, orphan_dir_bh);
2389
2390 leave:
2391 ocfs2_free_dir_lookup_result(&lookup);
2392
2393 if (status)
2394 mlog_errno(status);
2395 return status;
2396 }
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423 static int ocfs2_prep_new_orphaned_file(struct inode *dir,
2424 struct buffer_head *dir_bh,
2425 char *orphan_name,
2426 struct inode **ret_orphan_dir,
2427 u64 *ret_di_blkno,
2428 struct ocfs2_dir_lookup_result *orphan_insert,
2429 struct ocfs2_alloc_context **ret_inode_ac)
2430 {
2431 int ret;
2432 u64 di_blkno;
2433 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2434 struct inode *orphan_dir = NULL;
2435 struct buffer_head *orphan_dir_bh = NULL;
2436 struct ocfs2_alloc_context *inode_ac = NULL;
2437
2438 ret = ocfs2_lookup_lock_orphan_dir(osb, &orphan_dir, &orphan_dir_bh);
2439 if (ret < 0) {
2440 mlog_errno(ret);
2441 return ret;
2442 }
2443
2444
2445 ret = ocfs2_reserve_new_inode(osb, &inode_ac);
2446 if (ret < 0) {
2447 if (ret != -ENOSPC)
2448 mlog_errno(ret);
2449 goto out;
2450 }
2451
2452 ret = ocfs2_find_new_inode_loc(dir, dir_bh, inode_ac,
2453 &di_blkno);
2454 if (ret) {
2455 mlog_errno(ret);
2456 goto out;
2457 }
2458
2459 ret = __ocfs2_prepare_orphan_dir(orphan_dir, orphan_dir_bh,
2460 di_blkno, orphan_name, orphan_insert,
2461 false);
2462 if (ret < 0) {
2463 mlog_errno(ret);
2464 goto out;
2465 }
2466
2467 out:
2468 if (ret == 0) {
2469 *ret_orphan_dir = orphan_dir;
2470 *ret_di_blkno = di_blkno;
2471 *ret_inode_ac = inode_ac;
2472
2473
2474
2475
2476 } else {
2477
2478 if (inode_ac)
2479 ocfs2_free_alloc_context(inode_ac);
2480
2481
2482 inode_unlock(orphan_dir);
2483 ocfs2_inode_unlock(orphan_dir, 1);
2484 iput(orphan_dir);
2485 }
2486
2487 brelse(orphan_dir_bh);
2488
2489 return ret;
2490 }
2491
2492 int ocfs2_create_inode_in_orphan(struct inode *dir,
2493 int mode,
2494 struct inode **new_inode)
2495 {
2496 int status, did_quota_inode = 0;
2497 struct inode *inode = NULL;
2498 struct inode *orphan_dir = NULL;
2499 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2500 handle_t *handle = NULL;
2501 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
2502 struct buffer_head *parent_di_bh = NULL;
2503 struct buffer_head *new_di_bh = NULL;
2504 struct ocfs2_alloc_context *inode_ac = NULL;
2505 struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
2506 u64 di_blkno, suballoc_loc;
2507 u16 suballoc_bit;
2508
2509 status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
2510 if (status < 0) {
2511 if (status != -ENOENT)
2512 mlog_errno(status);
2513 return status;
2514 }
2515
2516 status = ocfs2_prep_new_orphaned_file(dir, parent_di_bh,
2517 orphan_name, &orphan_dir,
2518 &di_blkno, &orphan_insert, &inode_ac);
2519 if (status < 0) {
2520 if (status != -ENOSPC)
2521 mlog_errno(status);
2522 goto leave;
2523 }
2524
2525 inode = ocfs2_get_init_inode(dir, mode);
2526 if (IS_ERR(inode)) {
2527 status = PTR_ERR(inode);
2528 inode = NULL;
2529 mlog_errno(status);
2530 goto leave;
2531 }
2532
2533 handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb, 0, 0));
2534 if (IS_ERR(handle)) {
2535 status = PTR_ERR(handle);
2536 handle = NULL;
2537 mlog_errno(status);
2538 goto leave;
2539 }
2540
2541 status = dquot_alloc_inode(inode);
2542 if (status)
2543 goto leave;
2544 did_quota_inode = 1;
2545
2546 status = ocfs2_claim_new_inode_at_loc(handle, dir, inode_ac,
2547 &suballoc_loc,
2548 &suballoc_bit, di_blkno);
2549 if (status < 0) {
2550 mlog_errno(status);
2551 goto leave;
2552 }
2553
2554 clear_nlink(inode);
2555
2556 status = __ocfs2_mknod_locked(dir, inode,
2557 0, &new_di_bh, parent_di_bh, handle,
2558 inode_ac, di_blkno, suballoc_loc,
2559 suballoc_bit);
2560 if (status < 0) {
2561 mlog_errno(status);
2562 goto leave;
2563 }
2564
2565 status = ocfs2_orphan_add(osb, handle, inode, new_di_bh, orphan_name,
2566 &orphan_insert, orphan_dir, false);
2567 if (status < 0) {
2568 mlog_errno(status);
2569 goto leave;
2570 }
2571
2572
2573 status = ocfs2_open_lock(inode);
2574 if (status < 0)
2575 mlog_errno(status);
2576
2577 insert_inode_hash(inode);
2578 leave:
2579 if (status < 0 && did_quota_inode)
2580 dquot_free_inode(inode);
2581 if (handle)
2582 ocfs2_commit_trans(osb, handle);
2583
2584 if (orphan_dir) {
2585
2586 ocfs2_inode_unlock(orphan_dir, 1);
2587 inode_unlock(orphan_dir);
2588 iput(orphan_dir);
2589 }
2590
2591 if ((status < 0) && inode) {
2592 clear_nlink(inode);
2593 iput(inode);
2594 }
2595
2596 if (inode_ac)
2597 ocfs2_free_alloc_context(inode_ac);
2598
2599 brelse(new_di_bh);
2600
2601 if (!status)
2602 *new_inode = inode;
2603
2604 ocfs2_free_dir_lookup_result(&orphan_insert);
2605
2606 ocfs2_inode_unlock(dir, 1);
2607 brelse(parent_di_bh);
2608 return status;
2609 }
2610
2611 int ocfs2_add_inode_to_orphan(struct ocfs2_super *osb,
2612 struct inode *inode)
2613 {
2614 char orphan_name[OCFS2_DIO_ORPHAN_PREFIX_LEN + OCFS2_ORPHAN_NAMELEN + 1];
2615 struct inode *orphan_dir_inode = NULL;
2616 struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
2617 struct buffer_head *di_bh = NULL;
2618 int status = 0;
2619 handle_t *handle = NULL;
2620 struct ocfs2_dinode *di = NULL;
2621
2622 status = ocfs2_inode_lock(inode, &di_bh, 1);
2623 if (status < 0) {
2624 mlog_errno(status);
2625 goto bail;
2626 }
2627
2628 di = (struct ocfs2_dinode *) di_bh->b_data;
2629
2630
2631
2632
2633 if (unlikely(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))) {
2634 status = ocfs2_truncate_file(inode, di_bh, i_size_read(inode));
2635 if (status < 0) {
2636 if (status != -ENOSPC)
2637 mlog_errno(status);
2638 goto bail_unlock_inode;
2639 }
2640
2641 status = ocfs2_del_inode_from_orphan(osb, inode, di_bh, 0, 0);
2642 if (status < 0) {
2643 mlog_errno(status);
2644 goto bail_unlock_inode;
2645 }
2646 }
2647
2648 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir_inode,
2649 OCFS2_I(inode)->ip_blkno,
2650 orphan_name,
2651 &orphan_insert,
2652 true);
2653 if (status < 0) {
2654 mlog_errno(status);
2655 goto bail_unlock_inode;
2656 }
2657
2658 handle = ocfs2_start_trans(osb,
2659 OCFS2_INODE_ADD_TO_ORPHAN_CREDITS);
2660 if (IS_ERR(handle)) {
2661 status = PTR_ERR(handle);
2662 goto bail_unlock_orphan;
2663 }
2664
2665 status = ocfs2_orphan_add(osb, handle, inode, di_bh, orphan_name,
2666 &orphan_insert, orphan_dir_inode, true);
2667 if (status)
2668 mlog_errno(status);
2669
2670 ocfs2_commit_trans(osb, handle);
2671
2672 bail_unlock_orphan:
2673 ocfs2_inode_unlock(orphan_dir_inode, 1);
2674 inode_unlock(orphan_dir_inode);
2675 iput(orphan_dir_inode);
2676
2677 ocfs2_free_dir_lookup_result(&orphan_insert);
2678
2679 bail_unlock_inode:
2680 ocfs2_inode_unlock(inode, 1);
2681 brelse(di_bh);
2682
2683 bail:
2684 return status;
2685 }
2686
2687 int ocfs2_del_inode_from_orphan(struct ocfs2_super *osb,
2688 struct inode *inode, struct buffer_head *di_bh,
2689 int update_isize, loff_t end)
2690 {
2691 struct inode *orphan_dir_inode = NULL;
2692 struct buffer_head *orphan_dir_bh = NULL;
2693 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2694 handle_t *handle = NULL;
2695 int status = 0;
2696
2697 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2698 ORPHAN_DIR_SYSTEM_INODE,
2699 le16_to_cpu(di->i_dio_orphaned_slot));
2700 if (!orphan_dir_inode) {
2701 status = -ENOENT;
2702 mlog_errno(status);
2703 goto bail;
2704 }
2705
2706 inode_lock(orphan_dir_inode);
2707 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
2708 if (status < 0) {
2709 inode_unlock(orphan_dir_inode);
2710 iput(orphan_dir_inode);
2711 mlog_errno(status);
2712 goto bail;
2713 }
2714
2715 handle = ocfs2_start_trans(osb,
2716 OCFS2_INODE_DEL_FROM_ORPHAN_CREDITS);
2717 if (IS_ERR(handle)) {
2718 status = PTR_ERR(handle);
2719 goto bail_unlock_orphan;
2720 }
2721
2722 BUG_ON(!(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL)));
2723
2724 status = ocfs2_orphan_del(osb, handle, orphan_dir_inode,
2725 inode, orphan_dir_bh, true);
2726 if (status < 0) {
2727 mlog_errno(status);
2728 goto bail_commit;
2729 }
2730
2731 status = ocfs2_journal_access_di(handle,
2732 INODE_CACHE(inode),
2733 di_bh,
2734 OCFS2_JOURNAL_ACCESS_WRITE);
2735 if (status < 0) {
2736 mlog_errno(status);
2737 goto bail_commit;
2738 }
2739
2740 di->i_flags &= ~cpu_to_le32(OCFS2_DIO_ORPHANED_FL);
2741 di->i_dio_orphaned_slot = 0;
2742
2743 if (update_isize) {
2744 status = ocfs2_set_inode_size(handle, inode, di_bh, end);
2745 if (status)
2746 mlog_errno(status);
2747 } else
2748 ocfs2_journal_dirty(handle, di_bh);
2749
2750 bail_commit:
2751 ocfs2_commit_trans(osb, handle);
2752
2753 bail_unlock_orphan:
2754 ocfs2_inode_unlock(orphan_dir_inode, 1);
2755 inode_unlock(orphan_dir_inode);
2756 brelse(orphan_dir_bh);
2757 iput(orphan_dir_inode);
2758
2759 bail:
2760 return status;
2761 }
2762
2763 int ocfs2_mv_orphaned_inode_to_new(struct inode *dir,
2764 struct inode *inode,
2765 struct dentry *dentry)
2766 {
2767 int status = 0;
2768 struct buffer_head *parent_di_bh = NULL;
2769 handle_t *handle = NULL;
2770 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2771 struct ocfs2_dinode *dir_di, *di;
2772 struct inode *orphan_dir_inode = NULL;
2773 struct buffer_head *orphan_dir_bh = NULL;
2774 struct buffer_head *di_bh = NULL;
2775 struct ocfs2_dir_lookup_result lookup = { NULL, };
2776
2777 trace_ocfs2_mv_orphaned_inode_to_new(dir, dentry,
2778 dentry->d_name.len, dentry->d_name.name,
2779 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2780 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2781
2782 status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
2783 if (status < 0) {
2784 if (status != -ENOENT)
2785 mlog_errno(status);
2786 return status;
2787 }
2788
2789 dir_di = (struct ocfs2_dinode *) parent_di_bh->b_data;
2790 if (!dir_di->i_links_count) {
2791
2792 status = -ENOENT;
2793 goto leave;
2794 }
2795
2796 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
2797 dentry->d_name.len);
2798 if (status)
2799 goto leave;
2800
2801
2802 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_di_bh,
2803 dentry->d_name.name,
2804 dentry->d_name.len, &lookup);
2805 if (status < 0) {
2806 mlog_errno(status);
2807 goto leave;
2808 }
2809
2810 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2811 ORPHAN_DIR_SYSTEM_INODE,
2812 osb->slot_num);
2813 if (!orphan_dir_inode) {
2814 status = -ENOENT;
2815 mlog_errno(status);
2816 goto leave;
2817 }
2818
2819 inode_lock(orphan_dir_inode);
2820
2821 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
2822 if (status < 0) {
2823 mlog_errno(status);
2824 inode_unlock(orphan_dir_inode);
2825 iput(orphan_dir_inode);
2826 goto leave;
2827 }
2828
2829 status = ocfs2_read_inode_block(inode, &di_bh);
2830 if (status < 0) {
2831 mlog_errno(status);
2832 goto orphan_unlock;
2833 }
2834
2835 handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
2836 if (IS_ERR(handle)) {
2837 status = PTR_ERR(handle);
2838 handle = NULL;
2839 mlog_errno(status);
2840 goto orphan_unlock;
2841 }
2842
2843 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
2844 di_bh, OCFS2_JOURNAL_ACCESS_WRITE);
2845 if (status < 0) {
2846 mlog_errno(status);
2847 goto out_commit;
2848 }
2849
2850 status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
2851 orphan_dir_bh, false);
2852 if (status < 0) {
2853 mlog_errno(status);
2854 goto out_commit;
2855 }
2856
2857 di = (struct ocfs2_dinode *)di_bh->b_data;
2858 di->i_flags &= ~cpu_to_le32(OCFS2_ORPHANED_FL);
2859 di->i_orphaned_slot = 0;
2860 set_nlink(inode, 1);
2861 ocfs2_set_links_count(di, inode->i_nlink);
2862 ocfs2_update_inode_fsync_trans(handle, inode, 1);
2863 ocfs2_journal_dirty(handle, di_bh);
2864
2865 status = ocfs2_add_entry(handle, dentry, inode,
2866 OCFS2_I(inode)->ip_blkno, parent_di_bh,
2867 &lookup);
2868 if (status < 0) {
2869 mlog_errno(status);
2870 goto out_commit;
2871 }
2872
2873 status = ocfs2_dentry_attach_lock(dentry, inode,
2874 OCFS2_I(dir)->ip_blkno);
2875 if (status) {
2876 mlog_errno(status);
2877 goto out_commit;
2878 }
2879
2880 d_instantiate(dentry, inode);
2881 status = 0;
2882 out_commit:
2883 ocfs2_commit_trans(osb, handle);
2884 orphan_unlock:
2885 ocfs2_inode_unlock(orphan_dir_inode, 1);
2886 inode_unlock(orphan_dir_inode);
2887 iput(orphan_dir_inode);
2888 leave:
2889
2890 ocfs2_inode_unlock(dir, 1);
2891
2892 brelse(di_bh);
2893 brelse(parent_di_bh);
2894 brelse(orphan_dir_bh);
2895
2896 ocfs2_free_dir_lookup_result(&lookup);
2897
2898 if (status)
2899 mlog_errno(status);
2900
2901 return status;
2902 }
2903
2904 const struct inode_operations ocfs2_dir_iops = {
2905 .create = ocfs2_create,
2906 .lookup = ocfs2_lookup,
2907 .link = ocfs2_link,
2908 .unlink = ocfs2_unlink,
2909 .rmdir = ocfs2_unlink,
2910 .symlink = ocfs2_symlink,
2911 .mkdir = ocfs2_mkdir,
2912 .mknod = ocfs2_mknod,
2913 .rename = ocfs2_rename,
2914 .setattr = ocfs2_setattr,
2915 .getattr = ocfs2_getattr,
2916 .permission = ocfs2_permission,
2917 .listxattr = ocfs2_listxattr,
2918 .fiemap = ocfs2_fiemap,
2919 .get_acl = ocfs2_iop_get_acl,
2920 .set_acl = ocfs2_iop_set_acl,
2921 .fileattr_get = ocfs2_fileattr_get,
2922 .fileattr_set = ocfs2_fileattr_set,
2923 };