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>
44 #define MLOG_MASK_PREFIX ML_NAMEI
45 #include <cluster/masklog.h>
53 #include "extent_map.h"
65 #include "buffer_head_io.h"
67 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
70 struct dentry *dentry,
72 struct buffer_head **new_fe_bh,
73 struct buffer_head *parent_fe_bh,
75 struct ocfs2_alloc_context *inode_ac);
77 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
78 struct inode **ret_orphan_dir,
81 struct buffer_head **de_bh);
83 static int ocfs2_orphan_add(struct ocfs2_super *osb,
86 struct ocfs2_dinode *fe,
88 struct buffer_head *de_bh,
89 struct inode *orphan_dir_inode);
91 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
96 /* An orphan dir name is an 8 byte value, printed as a hex string */
97 #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
99 static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
100 struct nameidata *nd)
104 struct inode *inode = NULL;
106 struct ocfs2_inode_info *oi;
108 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
109 dentry->d_name.len, dentry->d_name.name);
111 if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
112 ret = ERR_PTR(-ENAMETOOLONG);
116 mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
117 dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
119 status = ocfs2_inode_lock(dir, NULL, 0);
121 if (status != -ENOENT)
123 ret = ERR_PTR(status);
127 status = ocfs2_lookup_ino_from_name(dir, dentry->d_name.name,
128 dentry->d_name.len, &blkno);
132 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0);
134 ret = ERR_PTR(-EACCES);
139 /* Clear any orphaned state... If we were able to look up the
140 * inode from a directory, it certainly can't be orphaned. We
141 * might have the bad state from a node which intended to
142 * orphan this inode but crashed before it could commit the
144 spin_lock(&oi->ip_lock);
145 oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
146 spin_unlock(&oi->ip_lock);
149 dentry->d_op = &ocfs2_dentry_ops;
150 ret = d_splice_alias(inode, dentry);
154 * If d_splice_alias() finds a DCACHE_DISCONNECTED
155 * dentry, it will d_move() it on top of ourse. The
156 * return value will indicate this however, so in
157 * those cases, we switch them around for the locking
160 * NOTE: This dentry already has ->d_op set from
161 * ocfs2_get_parent() and ocfs2_get_dentry()
166 status = ocfs2_dentry_attach_lock(dentry, inode,
167 OCFS2_I(dir)->ip_blkno);
170 ret = ERR_PTR(status);
176 /* Don't drop the cluster lock until *after* the d_add --
177 * unlink on another node will message us to remove that
178 * dentry under this lock so otherwise we can race this with
179 * the downconvert thread and have a stale dentry. */
180 ocfs2_inode_unlock(dir, 0);
189 static struct inode *ocfs2_get_init_inode(struct inode *dir, int mode)
193 inode = new_inode(dir->i_sb);
195 mlog(ML_ERROR, "new_inode failed!\n");
199 /* populate as many fields early on as possible - many of
200 * these are used by the support functions here and in
206 inode->i_uid = current_fsuid();
207 if (dir->i_mode & S_ISGID) {
208 inode->i_gid = dir->i_gid;
212 inode->i_gid = current_fsgid();
213 inode->i_mode = mode;
217 static int ocfs2_mknod(struct inode *dir,
218 struct dentry *dentry,
223 struct buffer_head *parent_fe_bh = NULL;
224 handle_t *handle = NULL;
225 struct ocfs2_super *osb;
226 struct ocfs2_dinode *dirfe;
227 struct buffer_head *new_fe_bh = NULL;
228 struct buffer_head *de_bh = NULL;
229 struct inode *inode = NULL;
230 struct ocfs2_alloc_context *inode_ac = NULL;
231 struct ocfs2_alloc_context *data_ac = NULL;
232 struct ocfs2_alloc_context *xattr_ac = NULL;
233 int want_clusters = 0;
234 int xattr_credits = 0;
235 struct ocfs2_security_xattr_info si = {
239 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
240 (unsigned long)dev, dentry->d_name.len,
241 dentry->d_name.name);
243 /* get our super block */
244 osb = OCFS2_SB(dir->i_sb);
246 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
248 if (status != -ENOENT)
253 if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
258 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
259 if (!dirfe->i_links_count) {
260 /* can't make a file in a deleted directory. */
265 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
270 /* get a spot inside the dir. */
271 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
273 dentry->d_name.len, &de_bh);
279 /* reserve an inode spot */
280 status = ocfs2_reserve_new_inode(osb, &inode_ac);
282 if (status != -ENOSPC)
287 inode = ocfs2_get_init_inode(dir, mode);
294 /* get security xattr */
295 status = ocfs2_init_security_get(inode, dir, &si);
297 if (status == -EOPNOTSUPP)
305 /* calculate meta data/clusters for setting security xattr */
307 status = ocfs2_calc_security_init(dir, &si, &want_clusters,
308 &xattr_credits, &xattr_ac);
315 /* Reserve a cluster if creating an extent based directory. */
316 if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb))
319 status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
321 if (status != -ENOSPC)
326 handle = ocfs2_start_trans(osb, OCFS2_MKNOD_CREDITS + xattr_credits);
327 if (IS_ERR(handle)) {
328 status = PTR_ERR(handle);
334 /* do the real work now. */
335 status = ocfs2_mknod_locked(osb, dir, inode, dentry, dev,
336 &new_fe_bh, parent_fe_bh, handle,
344 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
351 status = ocfs2_journal_access(handle, dir, parent_fe_bh,
352 OCFS2_JOURNAL_ACCESS_WRITE);
357 le16_add_cpu(&dirfe->i_links_count, 1);
358 status = ocfs2_journal_dirty(handle, parent_fe_bh);
367 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
375 status = ocfs2_add_entry(handle, dentry, inode,
376 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
383 status = ocfs2_dentry_attach_lock(dentry, inode,
384 OCFS2_I(dir)->ip_blkno);
390 insert_inode_hash(inode);
391 dentry->d_op = &ocfs2_dentry_ops;
392 d_instantiate(dentry, inode);
396 ocfs2_commit_trans(osb, handle);
398 ocfs2_inode_unlock(dir, 1);
400 if (status == -ENOSPC)
401 mlog(0, "Disk is full\n");
405 brelse(parent_fe_bh);
409 if ((status < 0) && inode) {
415 ocfs2_free_alloc_context(inode_ac);
418 ocfs2_free_alloc_context(data_ac);
421 ocfs2_free_alloc_context(xattr_ac);
428 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
431 struct dentry *dentry,
433 struct buffer_head **new_fe_bh,
434 struct buffer_head *parent_fe_bh,
436 struct ocfs2_alloc_context *inode_ac)
439 struct ocfs2_dinode *fe = NULL;
440 struct ocfs2_extent_list *fel;
444 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry,
445 inode->i_mode, (unsigned long)dev, dentry->d_name.len,
446 dentry->d_name.name);
450 status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
457 /* populate as many fields early on as possible - many of
458 * these are used by the support functions here and in
460 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
461 OCFS2_I(inode)->ip_blkno = fe_blkno;
462 spin_lock(&osb->osb_lock);
463 inode->i_generation = osb->s_next_generation++;
464 spin_unlock(&osb->osb_lock);
466 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
472 ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
474 status = ocfs2_journal_access(handle, inode, *new_fe_bh,
475 OCFS2_JOURNAL_ACCESS_CREATE);
481 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
482 memset(fe, 0, osb->sb->s_blocksize);
484 fe->i_generation = cpu_to_le32(inode->i_generation);
485 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
486 fe->i_blkno = cpu_to_le64(fe_blkno);
487 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
488 fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
489 fe->i_uid = cpu_to_le32(inode->i_uid);
490 fe->i_gid = cpu_to_le32(inode->i_gid);
491 fe->i_mode = cpu_to_le16(inode->i_mode);
492 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
493 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
494 fe->i_links_count = cpu_to_le16(inode->i_nlink);
496 fe->i_last_eb_blk = 0;
497 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
498 le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
499 fe->i_atime = fe->i_ctime = fe->i_mtime =
500 cpu_to_le64(CURRENT_TIME.tv_sec);
501 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
502 cpu_to_le32(CURRENT_TIME.tv_nsec);
506 * If supported, directories start with inline data.
508 if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
509 u16 feat = le16_to_cpu(fe->i_dyn_features);
511 fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
513 fe->id2.i_data.id_count = cpu_to_le16(ocfs2_max_inline_data(osb->sb));
515 fel = &fe->id2.i_list;
516 fel->l_tree_depth = 0;
517 fel->l_next_free_rec = 0;
518 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
521 status = ocfs2_journal_dirty(handle, *new_fe_bh);
527 if (ocfs2_populate_inode(inode, fe, 1) < 0) {
528 mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
529 "i_blkno=%llu, i_ino=%lu\n",
530 (unsigned long long)(*new_fe_bh)->b_blocknr,
531 (unsigned long long)le64_to_cpu(fe->i_blkno),
536 ocfs2_inode_set_new(osb, inode);
537 if (!ocfs2_mount_local(osb)) {
538 status = ocfs2_create_new_inode_locks(inode);
543 status = 0; /* error in ocfs2_create_new_inode_locks is not
558 static int ocfs2_mkdir(struct inode *dir,
559 struct dentry *dentry,
564 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
565 dentry->d_name.len, dentry->d_name.name);
566 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
572 static int ocfs2_create(struct inode *dir,
573 struct dentry *dentry,
575 struct nameidata *nd)
579 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
580 dentry->d_name.len, dentry->d_name.name);
581 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
587 static int ocfs2_link(struct dentry *old_dentry,
589 struct dentry *dentry)
592 struct inode *inode = old_dentry->d_inode;
594 struct buffer_head *fe_bh = NULL;
595 struct buffer_head *parent_fe_bh = NULL;
596 struct buffer_head *de_bh = NULL;
597 struct ocfs2_dinode *fe = NULL;
598 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
600 mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
601 old_dentry->d_name.len, old_dentry->d_name.name,
602 dentry->d_name.len, dentry->d_name.name);
604 if (S_ISDIR(inode->i_mode))
607 err = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
619 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
624 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
626 dentry->d_name.len, &de_bh);
632 err = ocfs2_inode_lock(inode, &fe_bh, 1);
639 fe = (struct ocfs2_dinode *) fe_bh->b_data;
640 if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
642 goto out_unlock_inode;
645 handle = ocfs2_start_trans(osb, OCFS2_LINK_CREDITS);
646 if (IS_ERR(handle)) {
647 err = PTR_ERR(handle);
650 goto out_unlock_inode;
653 err = ocfs2_journal_access(handle, inode, fe_bh,
654 OCFS2_JOURNAL_ACCESS_WRITE);
661 inode->i_ctime = CURRENT_TIME;
662 fe->i_links_count = cpu_to_le16(inode->i_nlink);
663 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
664 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
666 err = ocfs2_journal_dirty(handle, fe_bh);
668 le16_add_cpu(&fe->i_links_count, -1);
674 err = ocfs2_add_entry(handle, dentry, inode,
675 OCFS2_I(inode)->ip_blkno,
676 parent_fe_bh, de_bh);
678 le16_add_cpu(&fe->i_links_count, -1);
684 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
690 atomic_inc(&inode->i_count);
691 dentry->d_op = &ocfs2_dentry_ops;
692 d_instantiate(dentry, inode);
695 ocfs2_commit_trans(osb, handle);
697 ocfs2_inode_unlock(inode, 1);
700 ocfs2_inode_unlock(dir, 1);
704 brelse(parent_fe_bh);
712 * Takes and drops an exclusive lock on the given dentry. This will
713 * force other nodes to drop it.
715 static int ocfs2_remote_dentry_delete(struct dentry *dentry)
719 ret = ocfs2_dentry_lock(dentry, 1);
723 ocfs2_dentry_unlock(dentry, 1);
728 static inline int inode_is_unlinkable(struct inode *inode)
730 if (S_ISDIR(inode->i_mode)) {
731 if (inode->i_nlink == 2)
736 if (inode->i_nlink == 1)
741 static int ocfs2_unlink(struct inode *dir,
742 struct dentry *dentry)
745 int child_locked = 0;
746 struct inode *inode = dentry->d_inode;
747 struct inode *orphan_dir = NULL;
748 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
750 struct ocfs2_dinode *fe = NULL;
751 struct buffer_head *fe_bh = NULL;
752 struct buffer_head *parent_node_bh = NULL;
753 handle_t *handle = NULL;
754 struct ocfs2_dir_entry *dirent = NULL;
755 struct buffer_head *dirent_bh = NULL;
756 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
757 struct buffer_head *orphan_entry_bh = NULL;
759 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
760 dentry->d_name.len, dentry->d_name.name);
762 BUG_ON(dentry->d_parent->d_inode != dir);
764 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
766 if (inode == osb->root_inode) {
767 mlog(0, "Cannot delete the root directory\n");
771 status = ocfs2_inode_lock(dir, &parent_node_bh, 1);
773 if (status != -ENOENT)
778 status = ocfs2_find_files_on_disk(dentry->d_name.name,
779 dentry->d_name.len, &blkno,
780 dir, &dirent_bh, &dirent);
782 if (status != -ENOENT)
787 if (OCFS2_I(inode)->ip_blkno != blkno) {
790 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
791 (unsigned long long)OCFS2_I(inode)->ip_blkno,
792 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
796 status = ocfs2_inode_lock(inode, &fe_bh, 1);
798 if (status != -ENOENT)
804 if (S_ISDIR(inode->i_mode)) {
805 if (!ocfs2_empty_dir(inode)) {
808 } else if (inode->i_nlink != 2) {
814 status = ocfs2_remote_dentry_delete(dentry);
816 /* This remote delete should succeed under all normal
822 if (inode_is_unlinkable(inode)) {
823 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
832 handle = ocfs2_start_trans(osb, OCFS2_UNLINK_CREDITS);
833 if (IS_ERR(handle)) {
834 status = PTR_ERR(handle);
840 status = ocfs2_journal_access(handle, inode, fe_bh,
841 OCFS2_JOURNAL_ACCESS_WRITE);
847 fe = (struct ocfs2_dinode *) fe_bh->b_data;
849 if (inode_is_unlinkable(inode)) {
850 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
851 orphan_entry_bh, orphan_dir);
858 /* delete the name from the parent dir */
859 status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
865 if (S_ISDIR(inode->i_mode))
868 fe->i_links_count = cpu_to_le16(inode->i_nlink);
870 status = ocfs2_journal_dirty(handle, fe_bh);
876 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
877 if (S_ISDIR(inode->i_mode))
880 status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
883 if (S_ISDIR(inode->i_mode))
889 ocfs2_commit_trans(osb, handle);
892 ocfs2_inode_unlock(inode, 1);
894 ocfs2_inode_unlock(dir, 1);
897 /* This was locked for us in ocfs2_prepare_orphan_dir() */
898 ocfs2_inode_unlock(orphan_dir, 1);
899 mutex_unlock(&orphan_dir->i_mutex);
905 brelse(parent_node_bh);
906 brelse(orphan_entry_bh);
914 * The only place this should be used is rename!
915 * if they have the same id, then the 1st one is the only one locked.
917 static int ocfs2_double_lock(struct ocfs2_super *osb,
918 struct buffer_head **bh1,
919 struct inode *inode1,
920 struct buffer_head **bh2,
921 struct inode *inode2)
924 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
925 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
926 struct buffer_head **tmpbh;
927 struct inode *tmpinode;
929 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
930 (unsigned long long)oi1->ip_blkno,
931 (unsigned long long)oi2->ip_blkno);
938 /* we always want to lock the one with the lower lockid first. */
939 if (oi1->ip_blkno != oi2->ip_blkno) {
940 if (oi1->ip_blkno < oi2->ip_blkno) {
941 /* switch id1 and id2 around */
942 mlog(0, "switching them around...\n");
952 status = ocfs2_inode_lock(inode2, bh2, 1);
954 if (status != -ENOENT)
961 status = ocfs2_inode_lock(inode1, bh1, 1);
964 * An error return must mean that no cluster locks
965 * were held on function exit.
967 if (oi1->ip_blkno != oi2->ip_blkno)
968 ocfs2_inode_unlock(inode2, 1);
970 if (status != -ENOENT)
979 static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
981 ocfs2_inode_unlock(inode1, 1);
983 if (inode1 != inode2)
984 ocfs2_inode_unlock(inode2, 1);
987 static int ocfs2_rename(struct inode *old_dir,
988 struct dentry *old_dentry,
989 struct inode *new_dir,
990 struct dentry *new_dentry)
992 int status = 0, rename_lock = 0, parents_locked = 0;
993 int old_child_locked = 0, new_child_locked = 0;
994 struct inode *old_inode = old_dentry->d_inode;
995 struct inode *new_inode = new_dentry->d_inode;
996 struct inode *orphan_dir = NULL;
997 struct ocfs2_dinode *newfe = NULL;
998 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
999 struct buffer_head *orphan_entry_bh = NULL;
1000 struct buffer_head *newfe_bh = NULL;
1001 struct buffer_head *old_inode_bh = NULL;
1002 struct buffer_head *insert_entry_bh = NULL;
1003 struct ocfs2_super *osb = NULL;
1004 u64 newfe_blkno, old_de_ino;
1005 handle_t *handle = NULL;
1006 struct buffer_head *old_dir_bh = NULL;
1007 struct buffer_head *new_dir_bh = NULL;
1008 struct ocfs2_dir_entry *old_inode_dot_dot_de = NULL, *old_de = NULL,
1010 struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
1011 struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
1012 // this is the 1st dirent bh
1013 nlink_t old_dir_nlink = old_dir->i_nlink;
1014 struct ocfs2_dinode *old_di;
1016 /* At some point it might be nice to break this function up a
1019 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
1020 old_dir, old_dentry, new_dir, new_dentry,
1021 old_dentry->d_name.len, old_dentry->d_name.name,
1022 new_dentry->d_name.len, new_dentry->d_name.name);
1024 osb = OCFS2_SB(old_dir->i_sb);
1027 if (!igrab(new_inode))
1031 /* Assume a directory hierarchy thusly:
1034 * a,b,c, and d are all directories.
1036 * from cwd of 'a' on both nodes:
1040 * And that's why, just like the VFS, we need a file system
1042 if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
1043 status = ocfs2_rename_lock(osb);
1051 /* if old and new are the same, this'll just do one lock. */
1052 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1053 &new_dir_bh, new_dir);
1060 /* make sure both dirs have bhs
1061 * get an extra ref on old_dir_bh if old==new */
1064 new_dir_bh = old_dir_bh;
1067 mlog(ML_ERROR, "no old_dir_bh!\n");
1074 * Aside from allowing a meta data update, the locking here
1075 * also ensures that the downconvert thread on other nodes
1076 * won't have to concurrently downconvert the inode and the
1079 status = ocfs2_inode_lock(old_inode, &old_inode_bh, 1);
1081 if (status != -ENOENT)
1085 old_child_locked = 1;
1087 status = ocfs2_remote_dentry_delete(old_dentry);
1093 if (S_ISDIR(old_inode->i_mode)) {
1094 u64 old_inode_parent;
1096 status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
1097 old_inode, &old_inode_de_bh,
1098 &old_inode_dot_dot_de);
1104 if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
1109 if (!new_inode && new_dir != old_dir &&
1110 new_dir->i_nlink >= OCFS2_LINK_MAX) {
1116 status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
1117 old_dentry->d_name.len,
1125 * Check for inode number is _not_ due to possible IO errors.
1126 * We might rmdir the source, keep it as pwd of some process
1127 * and merrily kill the link to whatever was created under the
1128 * same name. Goodbye sticky bit ;-<
1130 if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
1135 /* check if the target already exists (in which case we need
1137 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1138 new_dentry->d_name.len,
1139 &newfe_blkno, new_dir, &new_de_bh,
1141 /* The only error we allow here is -ENOENT because the new
1142 * file not existing is perfectly valid. */
1143 if ((status < 0) && (status != -ENOENT)) {
1144 /* If we cannot find the file specified we should just */
1145 /* return the error... */
1150 if (!new_de && new_inode) {
1152 * Target was unlinked by another node while we were
1153 * waiting to get to ocfs2_rename(). There isn't
1154 * anything we can do here to help the situation, so
1155 * bubble up the appropriate error.
1161 /* In case we need to overwrite an existing file, we blow it
1164 /* VFS didn't think there existed an inode here, but
1165 * someone else in the cluster must have raced our
1166 * rename to create one. Today we error cleanly, in
1167 * the future we should consider calling iget to build
1168 * a new struct inode for this entry. */
1172 mlog(0, "We found an inode for name %.*s but VFS "
1173 "didn't give us one.\n", new_dentry->d_name.len,
1174 new_dentry->d_name.name);
1178 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1181 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1182 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1183 (unsigned long long)newfe_blkno,
1184 OCFS2_I(new_inode)->ip_flags);
1188 status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
1190 if (status != -ENOENT)
1194 new_child_locked = 1;
1196 status = ocfs2_remote_dentry_delete(new_dentry);
1202 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1204 mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
1205 "newfebh=%p bhblocknr=%llu\n", new_de,
1206 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
1207 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1209 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
1210 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
1220 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1222 status = ocfs2_check_dir_for_entry(new_dir,
1223 new_dentry->d_name.name,
1224 new_dentry->d_name.len);
1228 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1229 new_dentry->d_name.name,
1230 new_dentry->d_name.len,
1238 handle = ocfs2_start_trans(osb, OCFS2_RENAME_CREDITS);
1239 if (IS_ERR(handle)) {
1240 status = PTR_ERR(handle);
1247 if (S_ISDIR(new_inode->i_mode)) {
1248 if (!ocfs2_empty_dir(new_inode) ||
1249 new_inode->i_nlink != 2) {
1250 status = -ENOTEMPTY;
1254 status = ocfs2_journal_access(handle, new_inode, newfe_bh,
1255 OCFS2_JOURNAL_ACCESS_WRITE);
1261 if (S_ISDIR(new_inode->i_mode) ||
1262 (newfe->i_links_count == cpu_to_le16(1))){
1263 status = ocfs2_orphan_add(osb, handle, new_inode,
1265 orphan_entry_bh, orphan_dir);
1272 /* change the dirent to point to the correct inode */
1273 status = ocfs2_update_entry(new_dir, handle, new_de_bh,
1279 new_dir->i_version++;
1281 if (S_ISDIR(new_inode->i_mode))
1282 newfe->i_links_count = 0;
1284 le16_add_cpu(&newfe->i_links_count, -1);
1286 status = ocfs2_journal_dirty(handle, newfe_bh);
1292 /* if the name was not found in new_dir, add it now */
1293 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1294 OCFS2_I(old_inode)->ip_blkno,
1295 new_dir_bh, insert_entry_bh);
1298 old_inode->i_ctime = CURRENT_TIME;
1299 mark_inode_dirty(old_inode);
1301 status = ocfs2_journal_access(handle, old_inode, old_inode_bh,
1302 OCFS2_JOURNAL_ACCESS_WRITE);
1304 old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
1306 old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
1307 old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
1309 status = ocfs2_journal_dirty(handle, old_inode_bh);
1316 * Now that the name has been added to new_dir, remove the old name.
1318 * We don't keep any directory entry context around until now
1319 * because the insert might have changed the type of directory
1320 * we're dealing with.
1322 old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
1323 old_dentry->d_name.len,
1330 status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
1337 new_inode->i_nlink--;
1338 new_inode->i_ctime = CURRENT_TIME;
1340 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1341 if (old_inode_de_bh) {
1342 status = ocfs2_update_entry(old_inode, handle, old_inode_de_bh,
1343 old_inode_dot_dot_de, new_dir);
1346 new_inode->i_nlink--;
1349 mark_inode_dirty(new_dir);
1352 mark_inode_dirty(old_dir);
1353 ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1355 mark_inode_dirty(new_inode);
1356 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1359 if (old_dir != new_dir) {
1360 /* Keep the same times on both directories.*/
1361 new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
1364 * This will also pick up the i_nlink change from the
1367 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
1370 if (old_dir_nlink != old_dir->i_nlink) {
1372 mlog(ML_ERROR, "need to change nlink for old dir "
1373 "%llu from %d to %d but bh is NULL!\n",
1374 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1375 (int)old_dir_nlink, old_dir->i_nlink);
1377 struct ocfs2_dinode *fe;
1378 status = ocfs2_journal_access(handle, old_dir,
1380 OCFS2_JOURNAL_ACCESS_WRITE);
1381 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1382 fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
1383 status = ocfs2_journal_dirty(handle, old_dir_bh);
1387 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
1391 ocfs2_rename_unlock(osb);
1394 ocfs2_commit_trans(osb, handle);
1397 ocfs2_double_unlock(old_dir, new_dir);
1399 if (old_child_locked)
1400 ocfs2_inode_unlock(old_inode, 1);
1402 if (new_child_locked)
1403 ocfs2_inode_unlock(new_inode, 1);
1406 /* This was locked for us in ocfs2_prepare_orphan_dir() */
1407 ocfs2_inode_unlock(orphan_dir, 1);
1408 mutex_unlock(&orphan_dir->i_mutex);
1413 sync_mapping_buffers(old_inode->i_mapping);
1418 brelse(old_inode_bh);
1423 brelse(old_inode_de_bh);
1424 brelse(orphan_entry_bh);
1425 brelse(insert_entry_bh);
1433 * we expect i_size = strlen(symname). Copy symname into the file
1434 * data, including the null terminator.
1436 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1438 struct inode *inode,
1439 const char *symname)
1441 struct buffer_head **bhs = NULL;
1443 struct super_block *sb = osb->sb;
1444 u64 p_blkno, p_blocks;
1445 int virtual, blocks, status, i, bytes_left;
1447 bytes_left = i_size_read(inode) + 1;
1448 /* we can't trust i_blocks because we're actually going to
1449 * write i_size + 1 bytes. */
1450 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1452 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1453 (unsigned long long)inode->i_blocks,
1454 i_size_read(inode), blocks);
1456 /* Sanity check -- make sure we're going to fit. */
1458 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1464 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1471 status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
1478 /* links can never be larger than one cluster so we know this
1479 * is all going to be contiguous, but do a sanity check
1481 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1488 while(bytes_left > 0) {
1489 c = &symname[virtual * sb->s_blocksize];
1491 bhs[virtual] = sb_getblk(sb, p_blkno);
1492 if (!bhs[virtual]) {
1497 ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
1499 status = ocfs2_journal_access(handle, inode, bhs[virtual],
1500 OCFS2_JOURNAL_ACCESS_CREATE);
1506 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1508 memcpy(bhs[virtual]->b_data, c,
1509 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1512 status = ocfs2_journal_dirty(handle, bhs[virtual]);
1520 bytes_left -= sb->s_blocksize;
1527 for(i = 0; i < blocks; i++)
1536 static int ocfs2_symlink(struct inode *dir,
1537 struct dentry *dentry,
1538 const char *symname)
1540 int status, l, credits;
1542 struct ocfs2_super *osb = NULL;
1543 struct inode *inode = NULL;
1544 struct super_block *sb;
1545 struct buffer_head *new_fe_bh = NULL;
1546 struct buffer_head *de_bh = NULL;
1547 struct buffer_head *parent_fe_bh = NULL;
1548 struct ocfs2_dinode *fe = NULL;
1549 struct ocfs2_dinode *dirfe;
1550 handle_t *handle = NULL;
1551 struct ocfs2_alloc_context *inode_ac = NULL;
1552 struct ocfs2_alloc_context *data_ac = NULL;
1553 struct ocfs2_alloc_context *xattr_ac = NULL;
1554 int want_clusters = 0;
1555 int xattr_credits = 0;
1556 struct ocfs2_security_xattr_info si = {
1560 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1561 dentry, symname, dentry->d_name.len, dentry->d_name.name);
1566 l = strlen(symname) + 1;
1568 credits = ocfs2_calc_symlink_credits(sb);
1570 /* lock the parent directory */
1571 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
1573 if (status != -ENOENT)
1578 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1579 if (!dirfe->i_links_count) {
1580 /* can't make a file in a deleted directory. */
1585 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1586 dentry->d_name.len);
1590 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1591 dentry->d_name.name,
1592 dentry->d_name.len, &de_bh);
1598 status = ocfs2_reserve_new_inode(osb, &inode_ac);
1600 if (status != -ENOSPC)
1605 inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
1612 /* get security xattr */
1613 status = ocfs2_init_security_get(inode, dir, &si);
1615 if (status == -EOPNOTSUPP)
1623 /* calculate meta data/clusters for setting security xattr */
1625 status = ocfs2_calc_security_init(dir, &si, &want_clusters,
1626 &xattr_credits, &xattr_ac);
1633 /* don't reserve bitmap space for fast symlinks. */
1634 if (l > ocfs2_fast_symlink_chars(sb))
1637 status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
1639 if (status != -ENOSPC)
1644 handle = ocfs2_start_trans(osb, credits + xattr_credits);
1645 if (IS_ERR(handle)) {
1646 status = PTR_ERR(handle);
1652 status = ocfs2_mknod_locked(osb, dir, inode, dentry,
1653 0, &new_fe_bh, parent_fe_bh, handle,
1660 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1663 if (l > ocfs2_fast_symlink_chars(sb)) {
1666 inode->i_op = &ocfs2_symlink_inode_operations;
1667 status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
1669 handle, data_ac, NULL,
1672 if (status != -ENOSPC && status != -EINTR) {
1674 "Failed to extend file to %llu\n",
1675 (unsigned long long)newsize);
1681 i_size_write(inode, newsize);
1682 inode->i_blocks = ocfs2_inode_sector_count(inode);
1684 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1685 memcpy((char *) fe->id2.i_symlink, symname, l);
1686 i_size_write(inode, newsize);
1687 inode->i_blocks = 0;
1690 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1696 if (!ocfs2_inode_is_fast_symlink(inode)) {
1697 status = ocfs2_create_symlink_data(osb, handle, inode,
1706 status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
1714 status = ocfs2_add_entry(handle, dentry, inode,
1715 le64_to_cpu(fe->i_blkno), parent_fe_bh,
1722 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
1728 insert_inode_hash(inode);
1729 dentry->d_op = &ocfs2_dentry_ops;
1730 d_instantiate(dentry, inode);
1733 ocfs2_commit_trans(osb, handle);
1735 ocfs2_inode_unlock(dir, 1);
1738 brelse(parent_fe_bh);
1743 ocfs2_free_alloc_context(inode_ac);
1745 ocfs2_free_alloc_context(data_ac);
1747 ocfs2_free_alloc_context(xattr_ac);
1748 if ((status < 0) && inode) {
1758 static int ocfs2_blkno_stringify(u64 blkno, char *name)
1760 int status, namelen;
1764 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
1774 if (namelen != OCFS2_ORPHAN_NAMELEN) {
1780 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
1789 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
1790 struct inode **ret_orphan_dir,
1791 struct inode *inode,
1793 struct buffer_head **de_bh)
1795 struct inode *orphan_dir_inode;
1796 struct buffer_head *orphan_dir_bh = NULL;
1799 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1805 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1806 ORPHAN_DIR_SYSTEM_INODE,
1808 if (!orphan_dir_inode) {
1814 mutex_lock(&orphan_dir_inode->i_mutex);
1816 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
1822 status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
1823 orphan_dir_bh, name,
1824 OCFS2_ORPHAN_NAMELEN, de_bh);
1826 ocfs2_inode_unlock(orphan_dir_inode, 1);
1832 *ret_orphan_dir = orphan_dir_inode;
1836 mutex_unlock(&orphan_dir_inode->i_mutex);
1837 iput(orphan_dir_inode);
1840 brelse(orphan_dir_bh);
1846 static int ocfs2_orphan_add(struct ocfs2_super *osb,
1848 struct inode *inode,
1849 struct ocfs2_dinode *fe,
1851 struct buffer_head *de_bh,
1852 struct inode *orphan_dir_inode)
1854 struct buffer_head *orphan_dir_bh = NULL;
1856 struct ocfs2_dinode *orphan_fe;
1858 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
1860 status = ocfs2_read_block(orphan_dir_inode,
1861 OCFS2_I(orphan_dir_inode)->ip_blkno,
1868 status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
1869 OCFS2_JOURNAL_ACCESS_WRITE);
1875 /* we're a cluster, and nlink can change on disk from
1876 * underneath us... */
1877 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
1878 if (S_ISDIR(inode->i_mode))
1879 le16_add_cpu(&orphan_fe->i_links_count, 1);
1880 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
1882 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
1888 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
1889 OCFS2_ORPHAN_NAMELEN, inode,
1890 OCFS2_I(inode)->ip_blkno,
1891 orphan_dir_bh, de_bh);
1897 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
1899 /* Record which orphan dir our inode now resides
1900 * in. delete_inode will use this to determine which orphan
1902 fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
1904 mlog(0, "Inode %llu orphaned in slot %d\n",
1905 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
1908 brelse(orphan_dir_bh);
1914 /* unlike orphan_add, we expect the orphan dir to already be locked here. */
1915 int ocfs2_orphan_del(struct ocfs2_super *osb,
1917 struct inode *orphan_dir_inode,
1918 struct inode *inode,
1919 struct buffer_head *orphan_dir_bh)
1921 char name[OCFS2_ORPHAN_NAMELEN + 1];
1922 struct ocfs2_dinode *orphan_fe;
1924 struct buffer_head *target_de_bh = NULL;
1925 struct ocfs2_dir_entry *target_de = NULL;
1929 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1935 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
1936 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
1937 OCFS2_ORPHAN_NAMELEN);
1939 /* find it's spot in the orphan directory */
1940 target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
1941 orphan_dir_inode, &target_de);
1942 if (!target_de_bh) {
1948 /* remove it from the orphan directory */
1949 status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
1956 status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
1957 OCFS2_JOURNAL_ACCESS_WRITE);
1963 /* do the i_nlink dance! :) */
1964 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
1965 if (S_ISDIR(inode->i_mode))
1966 le16_add_cpu(&orphan_fe->i_links_count, -1);
1967 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
1969 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
1976 brelse(target_de_bh);
1982 const struct inode_operations ocfs2_dir_iops = {
1983 .create = ocfs2_create,
1984 .lookup = ocfs2_lookup,
1986 .unlink = ocfs2_unlink,
1987 .rmdir = ocfs2_unlink,
1988 .symlink = ocfs2_symlink,
1989 .mkdir = ocfs2_mkdir,
1990 .mknod = ocfs2_mknod,
1991 .rename = ocfs2_rename,
1992 .setattr = ocfs2_setattr,
1993 .getattr = ocfs2_getattr,
1994 .permission = ocfs2_permission,
1995 .setxattr = generic_setxattr,
1996 .getxattr = generic_getxattr,
1997 .listxattr = ocfs2_listxattr,
1998 .removexattr = generic_removexattr,