1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Create and rename file, directory, symlinks
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 * Portions of this code from linux/fs/ext3/dir.c
12 * Copyright (C) 1992, 1993, 1994, 1995
13 * Remy Card (card@masi.ibp.fr)
14 * Laboratoire MASI - Institut Blaise pascal
15 * Universite Pierre et Marie Curie (Paris VI)
19 * linux/fs/minix/dir.c
21 * Copyright (C) 1991, 1992 Linux Torvalds
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public
25 * License as published by the Free Software Foundation; either
26 * version 2 of the License, or (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 * General Public License for more details.
33 * You should have received a copy of the GNU General Public
34 * License along with this program; if not, write to the
35 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
36 * Boston, MA 021110-1307, USA.
40 #include <linux/types.h>
41 #include <linux/slab.h>
42 #include <linux/highmem.h>
43 #include <linux/quotaops.h>
45 #include <cluster/masklog.h>
53 #include "extent_map.h"
65 #include "ocfs2_trace.h"
67 #include "buffer_head_io.h"
69 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
73 struct buffer_head **new_fe_bh,
74 struct buffer_head *parent_fe_bh,
76 struct ocfs2_alloc_context *inode_ac);
78 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
79 struct inode **ret_orphan_dir,
82 struct ocfs2_dir_lookup_result *lookup);
84 static int ocfs2_orphan_add(struct ocfs2_super *osb,
87 struct buffer_head *fe_bh,
89 struct ocfs2_dir_lookup_result *lookup,
90 struct inode *orphan_dir_inode);
92 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
97 /* An orphan dir name is an 8 byte value, printed as a hex string */
98 #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
100 static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
105 struct inode *inode = NULL;
107 struct ocfs2_inode_info *oi;
109 trace_ocfs2_lookup(dir, dentry, dentry->d_name.len,
111 (unsigned long long)OCFS2_I(dir)->ip_blkno, 0);
113 if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
114 ret = ERR_PTR(-ENAMETOOLONG);
118 status = ocfs2_inode_lock_nested(dir, NULL, 0, OI_LS_PARENT);
120 if (status != -ENOENT)
122 ret = ERR_PTR(status);
126 status = ocfs2_lookup_ino_from_name(dir, dentry->d_name.name,
127 dentry->d_name.len, &blkno);
131 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0);
133 ret = ERR_PTR(-EACCES);
138 /* Clear any orphaned state... If we were able to look up the
139 * inode from a directory, it certainly can't be orphaned. We
140 * might have the bad state from a node which intended to
141 * orphan this inode but crashed before it could commit the
143 spin_lock(&oi->ip_lock);
144 oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
145 spin_unlock(&oi->ip_lock);
148 ret = d_splice_alias(inode, dentry);
152 * If d_splice_alias() finds a DCACHE_DISCONNECTED
153 * dentry, it will d_move() it on top of ourse. The
154 * return value will indicate this however, so in
155 * those cases, we switch them around for the locking
158 * NOTE: This dentry already has ->d_op set from
159 * ocfs2_get_parent() and ocfs2_get_dentry()
164 status = ocfs2_dentry_attach_lock(dentry, inode,
165 OCFS2_I(dir)->ip_blkno);
168 ret = ERR_PTR(status);
172 ocfs2_dentry_attach_gen(dentry);
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);
183 trace_ocfs2_lookup_ret(ret);
188 static struct inode *ocfs2_get_init_inode(struct inode *dir, umode_t mode)
192 inode = new_inode(dir->i_sb);
194 mlog(ML_ERROR, "new_inode failed!\n");
198 /* populate as many fields early on as possible - many of
199 * these are used by the support functions here and in
203 inode_init_owner(inode, dir, mode);
204 dquot_initialize(inode);
208 static int ocfs2_mknod(struct inode *dir,
209 struct dentry *dentry,
214 struct buffer_head *parent_fe_bh = NULL;
215 handle_t *handle = NULL;
216 struct ocfs2_super *osb;
217 struct ocfs2_dinode *dirfe;
218 struct buffer_head *new_fe_bh = NULL;
219 struct inode *inode = NULL;
220 struct ocfs2_alloc_context *inode_ac = NULL;
221 struct ocfs2_alloc_context *data_ac = NULL;
222 struct ocfs2_alloc_context *meta_ac = NULL;
223 int want_clusters = 0;
225 int xattr_credits = 0;
226 struct ocfs2_security_xattr_info si = {
229 int did_quota_inode = 0;
230 struct ocfs2_dir_lookup_result lookup = { NULL, };
232 int did_block_signals = 0;
233 struct posix_acl *default_acl = NULL, *acl = NULL;
235 trace_ocfs2_mknod(dir, dentry, dentry->d_name.len, dentry->d_name.name,
236 (unsigned long long)OCFS2_I(dir)->ip_blkno,
237 (unsigned long)dev, mode);
239 dquot_initialize(dir);
241 /* get our super block */
242 osb = OCFS2_SB(dir->i_sb);
244 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
246 if (status != -ENOENT)
251 if (S_ISDIR(mode) && (dir->i_nlink >= ocfs2_link_max(osb))) {
256 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
257 if (!ocfs2_read_links_count(dirfe)) {
258 /* can't make a file in a deleted directory. */
263 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
268 /* get a spot inside the dir. */
269 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
271 dentry->d_name.len, &lookup);
277 /* reserve an inode spot */
278 status = ocfs2_reserve_new_inode(osb, &inode_ac);
280 if (status != -ENOSPC)
285 inode = ocfs2_get_init_inode(dir, mode);
292 /* get security xattr */
293 status = ocfs2_init_security_get(inode, dir, &dentry->d_name, &si);
295 if (status == -EOPNOTSUPP)
303 /* calculate meta data/clusters for setting security and acl xattr */
304 status = ocfs2_calc_xattr_init(dir, parent_fe_bh, mode,
306 &xattr_credits, &want_meta);
312 /* Reserve a cluster if creating an extent based directory. */
313 if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) {
316 /* Dir indexing requires extra space as well */
317 if (ocfs2_supports_indexed_dirs(osb))
321 status = ocfs2_reserve_new_metadata_blocks(osb, want_meta, &meta_ac);
323 if (status != -ENOSPC)
328 status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
330 if (status != -ENOSPC)
335 status = posix_acl_create(dir, &mode, &default_acl, &acl);
341 handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb,
344 if (IS_ERR(handle)) {
345 status = PTR_ERR(handle);
351 /* Starting to change things, restart is no longer possible. */
352 ocfs2_block_signals(&oldset);
353 did_block_signals = 1;
355 status = dquot_alloc_inode(inode);
360 /* do the real work now. */
361 status = ocfs2_mknod_locked(osb, dir, inode, dev,
362 &new_fe_bh, parent_fe_bh, handle,
370 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
371 new_fe_bh, data_ac, meta_ac);
377 status = ocfs2_journal_access_di(handle, INODE_CACHE(dir),
379 OCFS2_JOURNAL_ACCESS_WRITE);
384 ocfs2_add_links_count(dirfe, 1);
385 ocfs2_journal_dirty(handle, parent_fe_bh);
390 status = ocfs2_set_acl(handle, inode, new_fe_bh,
391 ACL_TYPE_DEFAULT, default_acl,
394 if (!status && acl) {
395 status = ocfs2_set_acl(handle, inode, new_fe_bh,
396 ACL_TYPE_ACCESS, acl,
406 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
415 * Do this before adding the entry to the directory. We add
416 * also set d_op after success so that ->d_iput() will cleanup
417 * the dentry lock even if ocfs2_add_entry() fails below.
419 status = ocfs2_dentry_attach_lock(dentry, inode,
420 OCFS2_I(dir)->ip_blkno);
426 status = ocfs2_add_entry(handle, dentry, inode,
427 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
434 insert_inode_hash(inode);
435 d_instantiate(dentry, inode);
439 posix_acl_release(default_acl);
441 posix_acl_release(acl);
442 if (status < 0 && did_quota_inode)
443 dquot_free_inode(inode);
445 ocfs2_commit_trans(osb, handle);
447 ocfs2_inode_unlock(dir, 1);
448 if (did_block_signals)
449 ocfs2_unblock_signals(&oldset);
452 brelse(parent_fe_bh);
456 ocfs2_free_dir_lookup_result(&lookup);
459 ocfs2_free_alloc_context(inode_ac);
462 ocfs2_free_alloc_context(data_ac);
465 ocfs2_free_alloc_context(meta_ac);
468 * We should call iput after the i_mutex of the bitmap been
469 * unlocked in ocfs2_free_alloc_context, or the
470 * ocfs2_delete_inode will mutex_lock again.
472 if ((status < 0) && inode) {
473 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR;
484 static int __ocfs2_mknod_locked(struct inode *dir,
487 struct buffer_head **new_fe_bh,
488 struct buffer_head *parent_fe_bh,
490 struct ocfs2_alloc_context *inode_ac,
491 u64 fe_blkno, u64 suballoc_loc, u16 suballoc_bit)
494 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
495 struct ocfs2_dinode *fe = NULL;
496 struct ocfs2_extent_list *fel;
498 struct ocfs2_inode_info *oi = OCFS2_I(inode);
502 /* populate as many fields early on as possible - many of
503 * these are used by the support functions here and in
505 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
506 OCFS2_I(inode)->ip_blkno = fe_blkno;
507 spin_lock(&osb->osb_lock);
508 inode->i_generation = osb->s_next_generation++;
509 spin_unlock(&osb->osb_lock);
511 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
517 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), *new_fe_bh);
519 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
521 OCFS2_JOURNAL_ACCESS_CREATE);
527 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
528 memset(fe, 0, osb->sb->s_blocksize);
530 fe->i_generation = cpu_to_le32(inode->i_generation);
531 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
532 fe->i_blkno = cpu_to_le64(fe_blkno);
533 fe->i_suballoc_loc = cpu_to_le64(suballoc_loc);
534 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
535 fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
536 fe->i_uid = cpu_to_le32(i_uid_read(inode));
537 fe->i_gid = cpu_to_le32(i_gid_read(inode));
538 fe->i_mode = cpu_to_le16(inode->i_mode);
539 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
540 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
542 ocfs2_set_links_count(fe, inode->i_nlink);
544 fe->i_last_eb_blk = 0;
545 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
546 fe->i_flags |= cpu_to_le32(OCFS2_VALID_FL);
547 fe->i_atime = fe->i_ctime = fe->i_mtime =
548 cpu_to_le64(CURRENT_TIME.tv_sec);
549 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
550 cpu_to_le32(CURRENT_TIME.tv_nsec);
554 * If supported, directories start with inline data. If inline
555 * isn't supported, but indexing is, we start them as indexed.
557 feat = le16_to_cpu(fe->i_dyn_features);
558 if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
559 fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
561 fe->id2.i_data.id_count = cpu_to_le16(
562 ocfs2_max_inline_data_with_xattr(osb->sb, fe));
564 fel = &fe->id2.i_list;
565 fel->l_tree_depth = 0;
566 fel->l_next_free_rec = 0;
567 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
570 ocfs2_journal_dirty(handle, *new_fe_bh);
572 ocfs2_populate_inode(inode, fe, 1);
573 ocfs2_ci_set_new(osb, INODE_CACHE(inode));
574 if (!ocfs2_mount_local(osb)) {
575 status = ocfs2_create_new_inode_locks(inode);
580 oi->i_sync_tid = handle->h_transaction->t_tid;
581 oi->i_datasync_tid = handle->h_transaction->t_tid;
596 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
600 struct buffer_head **new_fe_bh,
601 struct buffer_head *parent_fe_bh,
603 struct ocfs2_alloc_context *inode_ac)
606 u64 suballoc_loc, fe_blkno = 0;
611 status = ocfs2_claim_new_inode(handle, dir, parent_fe_bh,
612 inode_ac, &suballoc_loc,
613 &suballoc_bit, &fe_blkno);
619 return __ocfs2_mknod_locked(dir, inode, dev, new_fe_bh,
620 parent_fe_bh, handle, inode_ac,
621 fe_blkno, suballoc_loc, suballoc_bit);
624 static int ocfs2_mkdir(struct inode *dir,
625 struct dentry *dentry,
630 trace_ocfs2_mkdir(dir, dentry, dentry->d_name.len, dentry->d_name.name,
631 OCFS2_I(dir)->ip_blkno, mode);
632 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
639 static int ocfs2_create(struct inode *dir,
640 struct dentry *dentry,
646 trace_ocfs2_create(dir, dentry, dentry->d_name.len, dentry->d_name.name,
647 (unsigned long long)OCFS2_I(dir)->ip_blkno, mode);
648 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
655 static int ocfs2_link(struct dentry *old_dentry,
657 struct dentry *dentry)
660 struct inode *inode = old_dentry->d_inode;
662 struct buffer_head *fe_bh = NULL;
663 struct buffer_head *parent_fe_bh = NULL;
664 struct ocfs2_dinode *fe = NULL;
665 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
666 struct ocfs2_dir_lookup_result lookup = { NULL, };
670 trace_ocfs2_link((unsigned long long)OCFS2_I(inode)->ip_blkno,
671 old_dentry->d_name.len, old_dentry->d_name.name,
672 dentry->d_name.len, dentry->d_name.name);
674 if (S_ISDIR(inode->i_mode))
677 dquot_initialize(dir);
679 err = ocfs2_inode_lock_nested(dir, &parent_fe_bh, 1, OI_LS_PARENT);
691 err = ocfs2_lookup_ino_from_name(dir, old_dentry->d_name.name,
692 old_dentry->d_name.len, &old_de_ino);
699 * Check whether another node removed the source inode while we
702 if (old_de_ino != OCFS2_I(inode)->ip_blkno) {
707 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
712 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
714 dentry->d_name.len, &lookup);
720 err = ocfs2_inode_lock(inode, &fe_bh, 1);
727 fe = (struct ocfs2_dinode *) fe_bh->b_data;
728 if (ocfs2_read_links_count(fe) >= ocfs2_link_max(osb)) {
730 goto out_unlock_inode;
733 handle = ocfs2_start_trans(osb, ocfs2_link_credits(osb->sb));
734 if (IS_ERR(handle)) {
735 err = PTR_ERR(handle);
738 goto out_unlock_inode;
741 /* Starting to change things, restart is no longer possible. */
742 ocfs2_block_signals(&oldset);
744 err = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
745 OCFS2_JOURNAL_ACCESS_WRITE);
752 inode->i_ctime = CURRENT_TIME;
753 ocfs2_set_links_count(fe, inode->i_nlink);
754 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
755 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
756 ocfs2_journal_dirty(handle, fe_bh);
758 err = ocfs2_add_entry(handle, dentry, inode,
759 OCFS2_I(inode)->ip_blkno,
760 parent_fe_bh, &lookup);
762 ocfs2_add_links_count(fe, -1);
768 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
775 d_instantiate(dentry, inode);
778 ocfs2_commit_trans(osb, handle);
779 ocfs2_unblock_signals(&oldset);
781 ocfs2_inode_unlock(inode, 1);
784 ocfs2_inode_unlock(dir, 1);
787 brelse(parent_fe_bh);
789 ocfs2_free_dir_lookup_result(&lookup);
798 * Takes and drops an exclusive lock on the given dentry. This will
799 * force other nodes to drop it.
801 static int ocfs2_remote_dentry_delete(struct dentry *dentry)
805 ret = ocfs2_dentry_lock(dentry, 1);
809 ocfs2_dentry_unlock(dentry, 1);
814 static inline int ocfs2_inode_is_unlinkable(struct inode *inode)
816 if (S_ISDIR(inode->i_mode)) {
817 if (inode->i_nlink == 2)
822 if (inode->i_nlink == 1)
827 static int ocfs2_unlink(struct inode *dir,
828 struct dentry *dentry)
831 int child_locked = 0;
832 bool is_unlinkable = false;
833 struct inode *inode = dentry->d_inode;
834 struct inode *orphan_dir = NULL;
835 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
837 struct ocfs2_dinode *fe = NULL;
838 struct buffer_head *fe_bh = NULL;
839 struct buffer_head *parent_node_bh = NULL;
840 handle_t *handle = NULL;
841 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
842 struct ocfs2_dir_lookup_result lookup = { NULL, };
843 struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
845 trace_ocfs2_unlink(dir, dentry, dentry->d_name.len,
847 (unsigned long long)OCFS2_I(dir)->ip_blkno,
848 (unsigned long long)OCFS2_I(inode)->ip_blkno);
850 dquot_initialize(dir);
852 BUG_ON(dentry->d_parent->d_inode != dir);
854 if (inode == osb->root_inode)
857 status = ocfs2_inode_lock_nested(dir, &parent_node_bh, 1,
860 if (status != -ENOENT)
865 status = ocfs2_find_files_on_disk(dentry->d_name.name,
866 dentry->d_name.len, &blkno, dir,
869 if (status != -ENOENT)
874 if (OCFS2_I(inode)->ip_blkno != blkno) {
877 trace_ocfs2_unlink_noent(
878 (unsigned long long)OCFS2_I(inode)->ip_blkno,
879 (unsigned long long)blkno,
880 OCFS2_I(inode)->ip_flags);
884 status = ocfs2_inode_lock(inode, &fe_bh, 1);
886 if (status != -ENOENT)
892 if (S_ISDIR(inode->i_mode)) {
893 if (inode->i_nlink != 2 || !ocfs2_empty_dir(inode)) {
899 status = ocfs2_remote_dentry_delete(dentry);
901 /* This remote delete should succeed under all normal
907 if (ocfs2_inode_is_unlinkable(inode)) {
908 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
909 OCFS2_I(inode)->ip_blkno,
910 orphan_name, &orphan_insert);
915 is_unlinkable = true;
918 handle = ocfs2_start_trans(osb, ocfs2_unlink_credits(osb->sb));
919 if (IS_ERR(handle)) {
920 status = PTR_ERR(handle);
926 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
927 OCFS2_JOURNAL_ACCESS_WRITE);
933 fe = (struct ocfs2_dinode *) fe_bh->b_data;
935 /* delete the name from the parent dir */
936 status = ocfs2_delete_entry(handle, dir, &lookup);
942 if (S_ISDIR(inode->i_mode))
945 ocfs2_set_links_count(fe, inode->i_nlink);
946 ocfs2_journal_dirty(handle, fe_bh);
948 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
949 if (S_ISDIR(inode->i_mode))
952 status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
955 if (S_ISDIR(inode->i_mode))
961 status = ocfs2_orphan_add(osb, handle, inode, fe_bh,
962 orphan_name, &orphan_insert, orphan_dir);
969 ocfs2_commit_trans(osb, handle);
972 ocfs2_inode_unlock(inode, 1);
974 ocfs2_inode_unlock(dir, 1);
977 /* This was locked for us in ocfs2_prepare_orphan_dir() */
978 ocfs2_inode_unlock(orphan_dir, 1);
979 mutex_unlock(&orphan_dir->i_mutex);
984 brelse(parent_node_bh);
986 ocfs2_free_dir_lookup_result(&orphan_insert);
987 ocfs2_free_dir_lookup_result(&lookup);
989 if (status && (status != -ENOTEMPTY) && (status != -ENOENT))
996 * The only place this should be used is rename!
997 * if they have the same id, then the 1st one is the only one locked.
999 static int ocfs2_double_lock(struct ocfs2_super *osb,
1000 struct buffer_head **bh1,
1001 struct inode *inode1,
1002 struct buffer_head **bh2,
1003 struct inode *inode2)
1006 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
1007 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
1008 struct buffer_head **tmpbh;
1009 struct inode *tmpinode;
1011 trace_ocfs2_double_lock((unsigned long long)oi1->ip_blkno,
1012 (unsigned long long)oi2->ip_blkno);
1019 /* we always want to lock the one with the lower lockid first. */
1020 if (oi1->ip_blkno != oi2->ip_blkno) {
1021 if (oi1->ip_blkno < oi2->ip_blkno) {
1022 /* switch id1 and id2 around */
1032 status = ocfs2_inode_lock_nested(inode2, bh2, 1,
1035 if (status != -ENOENT)
1042 status = ocfs2_inode_lock_nested(inode1, bh1, 1, OI_LS_RENAME2);
1045 * An error return must mean that no cluster locks
1046 * were held on function exit.
1048 if (oi1->ip_blkno != oi2->ip_blkno) {
1049 ocfs2_inode_unlock(inode2, 1);
1054 if (status != -ENOENT)
1058 trace_ocfs2_double_lock_end(
1059 (unsigned long long)OCFS2_I(inode1)->ip_blkno,
1060 (unsigned long long)OCFS2_I(inode2)->ip_blkno);
1068 static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
1070 ocfs2_inode_unlock(inode1, 1);
1072 if (inode1 != inode2)
1073 ocfs2_inode_unlock(inode2, 1);
1076 static int ocfs2_rename(struct inode *old_dir,
1077 struct dentry *old_dentry,
1078 struct inode *new_dir,
1079 struct dentry *new_dentry)
1081 int status = 0, rename_lock = 0, parents_locked = 0, target_exists = 0;
1082 int old_child_locked = 0, new_child_locked = 0, update_dot_dot = 0;
1083 struct inode *old_inode = old_dentry->d_inode;
1084 struct inode *new_inode = new_dentry->d_inode;
1085 struct inode *orphan_dir = NULL;
1086 struct ocfs2_dinode *newfe = NULL;
1087 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
1088 struct buffer_head *newfe_bh = NULL;
1089 struct buffer_head *old_inode_bh = NULL;
1090 struct ocfs2_super *osb = NULL;
1091 u64 newfe_blkno, old_de_ino;
1092 handle_t *handle = NULL;
1093 struct buffer_head *old_dir_bh = NULL;
1094 struct buffer_head *new_dir_bh = NULL;
1095 u32 old_dir_nlink = old_dir->i_nlink;
1096 struct ocfs2_dinode *old_di;
1097 struct ocfs2_dir_lookup_result old_inode_dot_dot_res = { NULL, };
1098 struct ocfs2_dir_lookup_result target_lookup_res = { NULL, };
1099 struct ocfs2_dir_lookup_result old_entry_lookup = { NULL, };
1100 struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
1101 struct ocfs2_dir_lookup_result target_insert = { NULL, };
1103 /* At some point it might be nice to break this function up a
1106 trace_ocfs2_rename(old_dir, old_dentry, new_dir, new_dentry,
1107 old_dentry->d_name.len, old_dentry->d_name.name,
1108 new_dentry->d_name.len, new_dentry->d_name.name);
1110 dquot_initialize(old_dir);
1111 dquot_initialize(new_dir);
1113 osb = OCFS2_SB(old_dir->i_sb);
1116 if (!igrab(new_inode))
1120 /* Assume a directory hierarchy thusly:
1123 * a,b,c, and d are all directories.
1125 * from cwd of 'a' on both nodes:
1129 * And that's why, just like the VFS, we need a file system
1131 if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
1132 status = ocfs2_rename_lock(osb);
1140 /* if old and new are the same, this'll just do one lock. */
1141 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1142 &new_dir_bh, new_dir);
1149 /* make sure both dirs have bhs
1150 * get an extra ref on old_dir_bh if old==new */
1153 new_dir_bh = old_dir_bh;
1156 mlog(ML_ERROR, "no old_dir_bh!\n");
1163 * Aside from allowing a meta data update, the locking here
1164 * also ensures that the downconvert thread on other nodes
1165 * won't have to concurrently downconvert the inode and the
1168 status = ocfs2_inode_lock_nested(old_inode, &old_inode_bh, 1,
1171 if (status != -ENOENT)
1175 old_child_locked = 1;
1177 status = ocfs2_remote_dentry_delete(old_dentry);
1183 if (S_ISDIR(old_inode->i_mode)) {
1184 u64 old_inode_parent;
1187 status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
1189 &old_inode_dot_dot_res);
1195 if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
1200 if (!new_inode && new_dir != old_dir &&
1201 new_dir->i_nlink >= ocfs2_link_max(osb)) {
1207 status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
1208 old_dentry->d_name.len,
1216 * Check for inode number is _not_ due to possible IO errors.
1217 * We might rmdir the source, keep it as pwd of some process
1218 * and merrily kill the link to whatever was created under the
1219 * same name. Goodbye sticky bit ;-<
1221 if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
1226 /* check if the target already exists (in which case we need
1228 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1229 new_dentry->d_name.len,
1230 &newfe_blkno, new_dir,
1231 &target_lookup_res);
1232 /* The only error we allow here is -ENOENT because the new
1233 * file not existing is perfectly valid. */
1234 if ((status < 0) && (status != -ENOENT)) {
1235 /* If we cannot find the file specified we should just */
1236 /* return the error... */
1243 if (!target_exists && new_inode) {
1245 * Target was unlinked by another node while we were
1246 * waiting to get to ocfs2_rename(). There isn't
1247 * anything we can do here to help the situation, so
1248 * bubble up the appropriate error.
1254 /* In case we need to overwrite an existing file, we blow it
1256 if (target_exists) {
1257 /* VFS didn't think there existed an inode here, but
1258 * someone else in the cluster must have raced our
1259 * rename to create one. Today we error cleanly, in
1260 * the future we should consider calling iget to build
1261 * a new struct inode for this entry. */
1265 trace_ocfs2_rename_target_exists(new_dentry->d_name.len,
1266 new_dentry->d_name.name);
1270 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1273 trace_ocfs2_rename_disagree(
1274 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1275 (unsigned long long)newfe_blkno,
1276 OCFS2_I(new_inode)->ip_flags);
1280 status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
1282 if (status != -ENOENT)
1286 new_child_locked = 1;
1288 status = ocfs2_remote_dentry_delete(new_dentry);
1294 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1296 trace_ocfs2_rename_over_existing(
1297 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
1298 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1300 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
1301 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
1302 OCFS2_I(new_inode)->ip_blkno,
1303 orphan_name, &orphan_insert);
1310 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1312 status = ocfs2_check_dir_for_entry(new_dir,
1313 new_dentry->d_name.name,
1314 new_dentry->d_name.len);
1318 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1319 new_dentry->d_name.name,
1320 new_dentry->d_name.len,
1328 handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
1329 if (IS_ERR(handle)) {
1330 status = PTR_ERR(handle);
1336 if (target_exists) {
1337 if (S_ISDIR(new_inode->i_mode)) {
1338 if (new_inode->i_nlink != 2 ||
1339 !ocfs2_empty_dir(new_inode)) {
1340 status = -ENOTEMPTY;
1344 status = ocfs2_journal_access_di(handle, INODE_CACHE(new_inode),
1346 OCFS2_JOURNAL_ACCESS_WRITE);
1352 if (S_ISDIR(new_inode->i_mode) ||
1353 (ocfs2_read_links_count(newfe) == 1)) {
1354 status = ocfs2_orphan_add(osb, handle, new_inode,
1355 newfe_bh, orphan_name,
1356 &orphan_insert, orphan_dir);
1363 /* change the dirent to point to the correct inode */
1364 status = ocfs2_update_entry(new_dir, handle, &target_lookup_res,
1370 new_dir->i_version++;
1372 if (S_ISDIR(new_inode->i_mode))
1373 ocfs2_set_links_count(newfe, 0);
1375 ocfs2_add_links_count(newfe, -1);
1376 ocfs2_journal_dirty(handle, newfe_bh);
1378 /* if the name was not found in new_dir, add it now */
1379 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1380 OCFS2_I(old_inode)->ip_blkno,
1381 new_dir_bh, &target_insert);
1384 old_inode->i_ctime = CURRENT_TIME;
1385 mark_inode_dirty(old_inode);
1387 status = ocfs2_journal_access_di(handle, INODE_CACHE(old_inode),
1389 OCFS2_JOURNAL_ACCESS_WRITE);
1391 old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
1393 old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
1394 old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
1395 ocfs2_journal_dirty(handle, old_inode_bh);
1400 * Now that the name has been added to new_dir, remove the old name.
1402 * We don't keep any directory entry context around until now
1403 * because the insert might have changed the type of directory
1404 * we're dealing with.
1406 status = ocfs2_find_entry(old_dentry->d_name.name,
1407 old_dentry->d_name.len, old_dir,
1412 status = ocfs2_delete_entry(handle, old_dir, &old_entry_lookup);
1419 drop_nlink(new_inode);
1420 new_inode->i_ctime = CURRENT_TIME;
1422 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1424 if (update_dot_dot) {
1425 status = ocfs2_update_entry(old_inode, handle,
1426 &old_inode_dot_dot_res, new_dir);
1427 drop_nlink(old_dir);
1429 drop_nlink(new_inode);
1432 mark_inode_dirty(new_dir);
1435 mark_inode_dirty(old_dir);
1436 ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1438 mark_inode_dirty(new_inode);
1439 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1442 if (old_dir != new_dir) {
1443 /* Keep the same times on both directories.*/
1444 new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
1447 * This will also pick up the i_nlink change from the
1450 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
1453 if (old_dir_nlink != old_dir->i_nlink) {
1455 mlog(ML_ERROR, "need to change nlink for old dir "
1456 "%llu from %d to %d but bh is NULL!\n",
1457 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1458 (int)old_dir_nlink, old_dir->i_nlink);
1460 struct ocfs2_dinode *fe;
1461 status = ocfs2_journal_access_di(handle,
1462 INODE_CACHE(old_dir),
1464 OCFS2_JOURNAL_ACCESS_WRITE);
1465 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1466 ocfs2_set_links_count(fe, old_dir->i_nlink);
1467 ocfs2_journal_dirty(handle, old_dir_bh);
1470 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
1474 ocfs2_rename_unlock(osb);
1477 ocfs2_commit_trans(osb, handle);
1480 ocfs2_double_unlock(old_dir, new_dir);
1482 if (old_child_locked)
1483 ocfs2_inode_unlock(old_inode, 1);
1485 if (new_child_locked)
1486 ocfs2_inode_unlock(new_inode, 1);
1489 /* This was locked for us in ocfs2_prepare_orphan_dir() */
1490 ocfs2_inode_unlock(orphan_dir, 1);
1491 mutex_unlock(&orphan_dir->i_mutex);
1496 sync_mapping_buffers(old_inode->i_mapping);
1501 ocfs2_free_dir_lookup_result(&target_lookup_res);
1502 ocfs2_free_dir_lookup_result(&old_entry_lookup);
1503 ocfs2_free_dir_lookup_result(&old_inode_dot_dot_res);
1504 ocfs2_free_dir_lookup_result(&orphan_insert);
1505 ocfs2_free_dir_lookup_result(&target_insert);
1508 brelse(old_inode_bh);
1519 * we expect i_size = strlen(symname). Copy symname into the file
1520 * data, including the null terminator.
1522 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1524 struct inode *inode,
1525 const char *symname)
1527 struct buffer_head **bhs = NULL;
1529 struct super_block *sb = osb->sb;
1530 u64 p_blkno, p_blocks;
1531 int virtual, blocks, status, i, bytes_left;
1533 bytes_left = i_size_read(inode) + 1;
1534 /* we can't trust i_blocks because we're actually going to
1535 * write i_size + 1 bytes. */
1536 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1538 trace_ocfs2_create_symlink_data((unsigned long long)inode->i_blocks,
1539 i_size_read(inode), blocks);
1541 /* Sanity check -- make sure we're going to fit. */
1543 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1549 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1556 status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
1563 /* links can never be larger than one cluster so we know this
1564 * is all going to be contiguous, but do a sanity check
1566 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1573 while(bytes_left > 0) {
1574 c = &symname[virtual * sb->s_blocksize];
1576 bhs[virtual] = sb_getblk(sb, p_blkno);
1577 if (!bhs[virtual]) {
1582 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode),
1585 status = ocfs2_journal_access(handle, INODE_CACHE(inode),
1587 OCFS2_JOURNAL_ACCESS_CREATE);
1593 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1595 memcpy(bhs[virtual]->b_data, c,
1596 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1599 ocfs2_journal_dirty(handle, bhs[virtual]);
1603 bytes_left -= sb->s_blocksize;
1610 for(i = 0; i < blocks; i++)
1620 static int ocfs2_symlink(struct inode *dir,
1621 struct dentry *dentry,
1622 const char *symname)
1624 int status, l, credits;
1626 struct ocfs2_super *osb = NULL;
1627 struct inode *inode = NULL;
1628 struct super_block *sb;
1629 struct buffer_head *new_fe_bh = NULL;
1630 struct buffer_head *parent_fe_bh = NULL;
1631 struct ocfs2_dinode *fe = NULL;
1632 struct ocfs2_dinode *dirfe;
1633 handle_t *handle = NULL;
1634 struct ocfs2_alloc_context *inode_ac = NULL;
1635 struct ocfs2_alloc_context *data_ac = NULL;
1636 struct ocfs2_alloc_context *xattr_ac = NULL;
1637 int want_clusters = 0;
1638 int xattr_credits = 0;
1639 struct ocfs2_security_xattr_info si = {
1642 int did_quota = 0, did_quota_inode = 0;
1643 struct ocfs2_dir_lookup_result lookup = { NULL, };
1645 int did_block_signals = 0;
1647 trace_ocfs2_symlink_begin(dir, dentry, symname,
1648 dentry->d_name.len, dentry->d_name.name);
1650 dquot_initialize(dir);
1655 l = strlen(symname) + 1;
1657 credits = ocfs2_calc_symlink_credits(sb);
1659 /* lock the parent directory */
1660 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
1662 if (status != -ENOENT)
1667 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1668 if (!ocfs2_read_links_count(dirfe)) {
1669 /* can't make a file in a deleted directory. */
1674 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1675 dentry->d_name.len);
1679 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1680 dentry->d_name.name,
1681 dentry->d_name.len, &lookup);
1687 status = ocfs2_reserve_new_inode(osb, &inode_ac);
1689 if (status != -ENOSPC)
1694 inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
1701 /* get security xattr */
1702 status = ocfs2_init_security_get(inode, dir, &dentry->d_name, &si);
1704 if (status == -EOPNOTSUPP)
1712 /* calculate meta data/clusters for setting security xattr */
1714 status = ocfs2_calc_security_init(dir, &si, &want_clusters,
1715 &xattr_credits, &xattr_ac);
1722 /* don't reserve bitmap space for fast symlinks. */
1723 if (l > ocfs2_fast_symlink_chars(sb))
1726 status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
1728 if (status != -ENOSPC)
1733 handle = ocfs2_start_trans(osb, credits + xattr_credits);
1734 if (IS_ERR(handle)) {
1735 status = PTR_ERR(handle);
1741 /* Starting to change things, restart is no longer possible. */
1742 ocfs2_block_signals(&oldset);
1743 did_block_signals = 1;
1745 status = dquot_alloc_inode(inode);
1748 did_quota_inode = 1;
1750 trace_ocfs2_symlink_create(dir, dentry, dentry->d_name.len,
1751 dentry->d_name.name,
1752 (unsigned long long)OCFS2_I(dir)->ip_blkno,
1755 status = ocfs2_mknod_locked(osb, dir, inode,
1756 0, &new_fe_bh, parent_fe_bh, handle,
1763 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1766 inode->i_op = &ocfs2_symlink_inode_operations;
1767 if (l > ocfs2_fast_symlink_chars(sb)) {
1770 status = dquot_alloc_space_nodirty(inode,
1771 ocfs2_clusters_to_bytes(osb->sb, 1));
1775 inode->i_mapping->a_ops = &ocfs2_aops;
1776 status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
1778 handle, data_ac, NULL,
1781 if (status != -ENOSPC && status != -EINTR) {
1783 "Failed to extend file to %llu\n",
1784 (unsigned long long)newsize);
1790 i_size_write(inode, newsize);
1791 inode->i_blocks = ocfs2_inode_sector_count(inode);
1793 inode->i_mapping->a_ops = &ocfs2_fast_symlink_aops;
1794 memcpy((char *) fe->id2.i_symlink, symname, l);
1795 i_size_write(inode, newsize);
1796 inode->i_blocks = 0;
1799 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1805 if (!ocfs2_inode_is_fast_symlink(inode)) {
1806 status = ocfs2_create_symlink_data(osb, handle, inode,
1815 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
1824 * Do this before adding the entry to the directory. We add
1825 * also set d_op after success so that ->d_iput() will cleanup
1826 * the dentry lock even if ocfs2_add_entry() fails below.
1828 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
1834 status = ocfs2_add_entry(handle, dentry, inode,
1835 le64_to_cpu(fe->i_blkno), parent_fe_bh,
1842 insert_inode_hash(inode);
1843 d_instantiate(dentry, inode);
1845 if (status < 0 && did_quota)
1846 dquot_free_space_nodirty(inode,
1847 ocfs2_clusters_to_bytes(osb->sb, 1));
1848 if (status < 0 && did_quota_inode)
1849 dquot_free_inode(inode);
1851 ocfs2_commit_trans(osb, handle);
1853 ocfs2_inode_unlock(dir, 1);
1854 if (did_block_signals)
1855 ocfs2_unblock_signals(&oldset);
1858 brelse(parent_fe_bh);
1861 ocfs2_free_dir_lookup_result(&lookup);
1863 ocfs2_free_alloc_context(inode_ac);
1865 ocfs2_free_alloc_context(data_ac);
1867 ocfs2_free_alloc_context(xattr_ac);
1868 if ((status < 0) && inode) {
1869 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR;
1880 static int ocfs2_blkno_stringify(u64 blkno, char *name)
1882 int status, namelen;
1884 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
1894 if (namelen != OCFS2_ORPHAN_NAMELEN) {
1900 trace_ocfs2_blkno_stringify(blkno, name, namelen);
1909 static int ocfs2_lookup_lock_orphan_dir(struct ocfs2_super *osb,
1910 struct inode **ret_orphan_dir,
1911 struct buffer_head **ret_orphan_dir_bh)
1913 struct inode *orphan_dir_inode;
1914 struct buffer_head *orphan_dir_bh = NULL;
1917 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1918 ORPHAN_DIR_SYSTEM_INODE,
1920 if (!orphan_dir_inode) {
1926 mutex_lock(&orphan_dir_inode->i_mutex);
1928 ret = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
1930 mutex_unlock(&orphan_dir_inode->i_mutex);
1931 iput(orphan_dir_inode);
1937 *ret_orphan_dir = orphan_dir_inode;
1938 *ret_orphan_dir_bh = orphan_dir_bh;
1943 static int __ocfs2_prepare_orphan_dir(struct inode *orphan_dir_inode,
1944 struct buffer_head *orphan_dir_bh,
1947 struct ocfs2_dir_lookup_result *lookup)
1950 struct ocfs2_super *osb = OCFS2_SB(orphan_dir_inode->i_sb);
1952 ret = ocfs2_blkno_stringify(blkno, name);
1958 ret = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
1959 orphan_dir_bh, name,
1960 OCFS2_ORPHAN_NAMELEN, lookup);
1970 * ocfs2_prepare_orphan_dir() - Prepare an orphan directory for
1971 * insertion of an orphan.
1972 * @osb: ocfs2 file system
1973 * @ret_orphan_dir: Orphan dir inode - returned locked!
1974 * @blkno: Actual block number of the inode to be inserted into orphan dir.
1975 * @lookup: dir lookup result, to be passed back into functions like
1978 * Returns zero on success and the ret_orphan_dir, name and lookup
1979 * fields will be populated.
1981 * Returns non-zero on failure.
1983 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
1984 struct inode **ret_orphan_dir,
1987 struct ocfs2_dir_lookup_result *lookup)
1989 struct inode *orphan_dir_inode = NULL;
1990 struct buffer_head *orphan_dir_bh = NULL;
1993 ret = ocfs2_lookup_lock_orphan_dir(osb, &orphan_dir_inode,
2000 ret = __ocfs2_prepare_orphan_dir(orphan_dir_inode, orphan_dir_bh,
2001 blkno, name, lookup);
2007 *ret_orphan_dir = orphan_dir_inode;
2010 brelse(orphan_dir_bh);
2013 ocfs2_inode_unlock(orphan_dir_inode, 1);
2014 mutex_unlock(&orphan_dir_inode->i_mutex);
2015 iput(orphan_dir_inode);
2023 static int ocfs2_orphan_add(struct ocfs2_super *osb,
2025 struct inode *inode,
2026 struct buffer_head *fe_bh,
2028 struct ocfs2_dir_lookup_result *lookup,
2029 struct inode *orphan_dir_inode)
2031 struct buffer_head *orphan_dir_bh = NULL;
2033 struct ocfs2_dinode *orphan_fe;
2034 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
2036 trace_ocfs2_orphan_add_begin(
2037 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2039 status = ocfs2_read_inode_block(orphan_dir_inode, &orphan_dir_bh);
2045 status = ocfs2_journal_access_di(handle,
2046 INODE_CACHE(orphan_dir_inode),
2048 OCFS2_JOURNAL_ACCESS_WRITE);
2055 * We're going to journal the change of i_flags and i_orphaned_slot.
2056 * It's safe anyway, though some callers may duplicate the journaling.
2057 * Journaling within the func just make the logic look more
2060 status = ocfs2_journal_access_di(handle,
2063 OCFS2_JOURNAL_ACCESS_WRITE);
2069 /* we're a cluster, and nlink can change on disk from
2070 * underneath us... */
2071 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2072 if (S_ISDIR(inode->i_mode))
2073 ocfs2_add_links_count(orphan_fe, 1);
2074 set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
2075 ocfs2_journal_dirty(handle, orphan_dir_bh);
2077 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
2078 OCFS2_ORPHAN_NAMELEN, inode,
2079 OCFS2_I(inode)->ip_blkno,
2080 orphan_dir_bh, lookup);
2086 fe->i_flags |= cpu_to_le32(OCFS2_ORPHANED_FL);
2087 OCFS2_I(inode)->ip_flags &= ~OCFS2_INODE_SKIP_ORPHAN_DIR;
2089 /* Record which orphan dir our inode now resides
2090 * in. delete_inode will use this to determine which orphan
2092 fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
2094 ocfs2_journal_dirty(handle, fe_bh);
2096 trace_ocfs2_orphan_add_end((unsigned long long)OCFS2_I(inode)->ip_blkno,
2101 if (S_ISDIR(inode->i_mode))
2102 ocfs2_add_links_count(orphan_fe, -1);
2103 set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
2107 brelse(orphan_dir_bh);
2112 /* unlike orphan_add, we expect the orphan dir to already be locked here. */
2113 int ocfs2_orphan_del(struct ocfs2_super *osb,
2115 struct inode *orphan_dir_inode,
2116 struct inode *inode,
2117 struct buffer_head *orphan_dir_bh)
2119 char name[OCFS2_ORPHAN_NAMELEN + 1];
2120 struct ocfs2_dinode *orphan_fe;
2122 struct ocfs2_dir_lookup_result lookup = { NULL, };
2124 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2130 trace_ocfs2_orphan_del(
2131 (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
2132 name, OCFS2_ORPHAN_NAMELEN);
2134 /* find it's spot in the orphan directory */
2135 status = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN, orphan_dir_inode,
2142 /* remove it from the orphan directory */
2143 status = ocfs2_delete_entry(handle, orphan_dir_inode, &lookup);
2149 status = ocfs2_journal_access_di(handle,
2150 INODE_CACHE(orphan_dir_inode),
2152 OCFS2_JOURNAL_ACCESS_WRITE);
2158 /* do the i_nlink dance! :) */
2159 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2160 if (S_ISDIR(inode->i_mode))
2161 ocfs2_add_links_count(orphan_fe, -1);
2162 set_nlink(orphan_dir_inode, ocfs2_read_links_count(orphan_fe));
2163 ocfs2_journal_dirty(handle, orphan_dir_bh);
2166 ocfs2_free_dir_lookup_result(&lookup);
2174 * ocfs2_prep_new_orphaned_file() - Prepare the orphan dir to receive a newly
2175 * allocated file. This is different from the typical 'add to orphan dir'
2176 * operation in that the inode does not yet exist. This is a problem because
2177 * the orphan dir stringifies the inode block number to come up with it's
2178 * dirent. Obviously if the inode does not yet exist we have a chicken and egg
2179 * problem. This function works around it by calling deeper into the orphan
2180 * and suballoc code than other callers. Use this only by necessity.
2181 * @dir: The directory which this inode will ultimately wind up under - not the
2183 * @dir_bh: buffer_head the @dir inode block
2184 * @orphan_name: string of length (CFS2_ORPHAN_NAMELEN + 1). Will be filled
2185 * with the string to be used for orphan dirent. Pass back to the orphan dir
2187 * @ret_orphan_dir: orphan dir inode returned to be passed back into orphan
2189 * @ret_di_blkno: block number where the new inode will be allocated.
2190 * @orphan_insert: Dir insert context to be passed back into orphan dir code.
2191 * @ret_inode_ac: Inode alloc context to be passed back to the allocator.
2193 * Returns zero on success and the ret_orphan_dir, name and lookup
2194 * fields will be populated.
2196 * Returns non-zero on failure.
2198 static int ocfs2_prep_new_orphaned_file(struct inode *dir,
2199 struct buffer_head *dir_bh,
2201 struct inode **ret_orphan_dir,
2203 struct ocfs2_dir_lookup_result *orphan_insert,
2204 struct ocfs2_alloc_context **ret_inode_ac)
2208 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2209 struct inode *orphan_dir = NULL;
2210 struct buffer_head *orphan_dir_bh = NULL;
2211 struct ocfs2_alloc_context *inode_ac = NULL;
2213 ret = ocfs2_lookup_lock_orphan_dir(osb, &orphan_dir, &orphan_dir_bh);
2219 /* reserve an inode spot */
2220 ret = ocfs2_reserve_new_inode(osb, &inode_ac);
2227 ret = ocfs2_find_new_inode_loc(dir, dir_bh, inode_ac,
2234 ret = __ocfs2_prepare_orphan_dir(orphan_dir, orphan_dir_bh,
2235 di_blkno, orphan_name, orphan_insert);
2243 *ret_orphan_dir = orphan_dir;
2244 *ret_di_blkno = di_blkno;
2245 *ret_inode_ac = inode_ac;
2247 * orphan_name and orphan_insert are already up to
2248 * date via prepare_orphan_dir
2251 /* Unroll reserve_new_inode* */
2253 ocfs2_free_alloc_context(inode_ac);
2255 /* Unroll orphan dir locking */
2256 mutex_unlock(&orphan_dir->i_mutex);
2257 ocfs2_inode_unlock(orphan_dir, 1);
2261 brelse(orphan_dir_bh);
2266 int ocfs2_create_inode_in_orphan(struct inode *dir,
2268 struct inode **new_inode)
2270 int status, did_quota_inode = 0;
2271 struct inode *inode = NULL;
2272 struct inode *orphan_dir = NULL;
2273 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2274 struct ocfs2_dinode *di = NULL;
2275 handle_t *handle = NULL;
2276 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
2277 struct buffer_head *parent_di_bh = NULL;
2278 struct buffer_head *new_di_bh = NULL;
2279 struct ocfs2_alloc_context *inode_ac = NULL;
2280 struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
2281 u64 uninitialized_var(di_blkno), suballoc_loc;
2284 status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
2286 if (status != -ENOENT)
2291 status = ocfs2_prep_new_orphaned_file(dir, parent_di_bh,
2292 orphan_name, &orphan_dir,
2293 &di_blkno, &orphan_insert, &inode_ac);
2295 if (status != -ENOSPC)
2300 inode = ocfs2_get_init_inode(dir, mode);
2307 handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb, 0, 0));
2308 if (IS_ERR(handle)) {
2309 status = PTR_ERR(handle);
2315 status = dquot_alloc_inode(inode);
2318 did_quota_inode = 1;
2320 status = ocfs2_claim_new_inode_at_loc(handle, dir, inode_ac,
2322 &suballoc_bit, di_blkno);
2329 /* do the real work now. */
2330 status = __ocfs2_mknod_locked(dir, inode,
2331 0, &new_di_bh, parent_di_bh, handle,
2332 inode_ac, di_blkno, suballoc_loc,
2339 di = (struct ocfs2_dinode *)new_di_bh->b_data;
2340 status = ocfs2_orphan_add(osb, handle, inode, new_di_bh, orphan_name,
2341 &orphan_insert, orphan_dir);
2347 /* get open lock so that only nodes can't remove it from orphan dir. */
2348 status = ocfs2_open_lock(inode);
2352 insert_inode_hash(inode);
2354 if (status < 0 && did_quota_inode)
2355 dquot_free_inode(inode);
2357 ocfs2_commit_trans(osb, handle);
2360 /* This was locked for us in ocfs2_prepare_orphan_dir() */
2361 ocfs2_inode_unlock(orphan_dir, 1);
2362 mutex_unlock(&orphan_dir->i_mutex);
2366 if ((status < 0) && inode) {
2372 ocfs2_free_alloc_context(inode_ac);
2379 ocfs2_free_dir_lookup_result(&orphan_insert);
2381 ocfs2_inode_unlock(dir, 1);
2382 brelse(parent_di_bh);
2386 int ocfs2_mv_orphaned_inode_to_new(struct inode *dir,
2387 struct inode *inode,
2388 struct dentry *dentry)
2391 struct buffer_head *parent_di_bh = NULL;
2392 handle_t *handle = NULL;
2393 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2394 struct ocfs2_dinode *dir_di, *di;
2395 struct inode *orphan_dir_inode = NULL;
2396 struct buffer_head *orphan_dir_bh = NULL;
2397 struct buffer_head *di_bh = NULL;
2398 struct ocfs2_dir_lookup_result lookup = { NULL, };
2400 trace_ocfs2_mv_orphaned_inode_to_new(dir, dentry,
2401 dentry->d_name.len, dentry->d_name.name,
2402 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2403 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2405 status = ocfs2_inode_lock(dir, &parent_di_bh, 1);
2407 if (status != -ENOENT)
2412 dir_di = (struct ocfs2_dinode *) parent_di_bh->b_data;
2413 if (!dir_di->i_links_count) {
2414 /* can't make a file in a deleted directory. */
2419 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
2420 dentry->d_name.len);
2424 /* get a spot inside the dir. */
2425 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_di_bh,
2426 dentry->d_name.name,
2427 dentry->d_name.len, &lookup);
2433 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2434 ORPHAN_DIR_SYSTEM_INODE,
2436 if (!orphan_dir_inode) {
2442 mutex_lock(&orphan_dir_inode->i_mutex);
2444 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
2447 mutex_unlock(&orphan_dir_inode->i_mutex);
2448 iput(orphan_dir_inode);
2452 status = ocfs2_read_inode_block(inode, &di_bh);
2458 handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb));
2459 if (IS_ERR(handle)) {
2460 status = PTR_ERR(handle);
2466 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
2467 di_bh, OCFS2_JOURNAL_ACCESS_WRITE);
2473 status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
2480 di = (struct ocfs2_dinode *)di_bh->b_data;
2481 di->i_flags &= ~cpu_to_le32(OCFS2_ORPHANED_FL);
2482 di->i_orphaned_slot = 0;
2483 set_nlink(inode, 1);
2484 ocfs2_set_links_count(di, inode->i_nlink);
2485 ocfs2_journal_dirty(handle, di_bh);
2487 status = ocfs2_add_entry(handle, dentry, inode,
2488 OCFS2_I(inode)->ip_blkno, parent_di_bh,
2495 status = ocfs2_dentry_attach_lock(dentry, inode,
2496 OCFS2_I(dir)->ip_blkno);
2502 d_instantiate(dentry, inode);
2505 ocfs2_commit_trans(osb, handle);
2507 ocfs2_inode_unlock(orphan_dir_inode, 1);
2508 mutex_unlock(&orphan_dir_inode->i_mutex);
2509 iput(orphan_dir_inode);
2512 ocfs2_inode_unlock(dir, 1);
2515 brelse(parent_di_bh);
2516 brelse(orphan_dir_bh);
2518 ocfs2_free_dir_lookup_result(&lookup);
2526 const struct inode_operations ocfs2_dir_iops = {
2527 .create = ocfs2_create,
2528 .lookup = ocfs2_lookup,
2530 .unlink = ocfs2_unlink,
2531 .rmdir = ocfs2_unlink,
2532 .symlink = ocfs2_symlink,
2533 .mkdir = ocfs2_mkdir,
2534 .mknod = ocfs2_mknod,
2535 .rename = ocfs2_rename,
2536 .setattr = ocfs2_setattr,
2537 .getattr = ocfs2_getattr,
2538 .permission = ocfs2_permission,
2539 .setxattr = generic_setxattr,
2540 .getxattr = generic_getxattr,
2541 .listxattr = ocfs2_listxattr,
2542 .removexattr = generic_removexattr,
2543 .fiemap = ocfs2_fiemap,
2544 .get_acl = ocfs2_iop_get_acl,
2545 .set_acl = ocfs2_iop_set_acl,