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,
69 struct dentry *dentry, int mode,
71 struct buffer_head **new_fe_bh,
72 struct buffer_head *parent_fe_bh,
74 struct inode **ret_inode,
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_meta_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);
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 vote thread and have a stale dentry. */
180 ocfs2_meta_unlock(dir, 0);
189 static int ocfs2_mknod(struct inode *dir,
190 struct dentry *dentry,
195 struct buffer_head *parent_fe_bh = NULL;
196 handle_t *handle = NULL;
197 struct ocfs2_super *osb;
198 struct ocfs2_dinode *dirfe;
199 struct buffer_head *new_fe_bh = NULL;
200 struct buffer_head *de_bh = NULL;
201 struct inode *inode = NULL;
202 struct ocfs2_alloc_context *inode_ac = NULL;
203 struct ocfs2_alloc_context *data_ac = NULL;
205 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
206 (unsigned long)dev, dentry->d_name.len,
207 dentry->d_name.name);
209 /* get our super block */
210 osb = OCFS2_SB(dir->i_sb);
212 status = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
214 if (status != -ENOENT)
219 if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
224 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
225 if (!dirfe->i_links_count) {
226 /* can't make a file in a deleted directory. */
231 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
236 /* get a spot inside the dir. */
237 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
239 dentry->d_name.len, &de_bh);
245 /* reserve an inode spot */
246 status = ocfs2_reserve_new_inode(osb, &inode_ac);
248 if (status != -ENOSPC)
253 /* are we making a directory? If so, reserve a cluster for his
256 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
258 if (status != -ENOSPC)
264 handle = ocfs2_start_trans(osb, OCFS2_MKNOD_CREDITS);
265 if (IS_ERR(handle)) {
266 status = PTR_ERR(handle);
272 /* do the real work now. */
273 status = ocfs2_mknod_locked(osb, dir, dentry, mode, dev,
274 &new_fe_bh, parent_fe_bh, handle,
282 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
289 status = ocfs2_journal_access(handle, dir, parent_fe_bh,
290 OCFS2_JOURNAL_ACCESS_WRITE);
295 le16_add_cpu(&dirfe->i_links_count, 1);
296 status = ocfs2_journal_dirty(handle, parent_fe_bh);
304 status = ocfs2_add_entry(handle, dentry, inode,
305 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
312 status = ocfs2_dentry_attach_lock(dentry, inode,
313 OCFS2_I(dir)->ip_blkno);
319 insert_inode_hash(inode);
320 dentry->d_op = &ocfs2_dentry_ops;
321 d_instantiate(dentry, inode);
325 ocfs2_commit_trans(osb, handle);
327 ocfs2_meta_unlock(dir, 1);
329 if (status == -ENOSPC)
330 mlog(0, "Disk is full\n");
339 brelse(parent_fe_bh);
341 if ((status < 0) && inode)
345 ocfs2_free_alloc_context(inode_ac);
348 ocfs2_free_alloc_context(data_ac);
355 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
357 struct dentry *dentry, int mode,
359 struct buffer_head **new_fe_bh,
360 struct buffer_head *parent_fe_bh,
362 struct inode **ret_inode,
363 struct ocfs2_alloc_context *inode_ac)
366 struct ocfs2_dinode *fe = NULL;
367 struct ocfs2_extent_list *fel;
370 struct inode *inode = NULL;
372 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
373 (unsigned long)dev, dentry->d_name.len,
374 dentry->d_name.name);
379 status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
386 inode = new_inode(dir->i_sb);
388 status = PTR_ERR(inode);
389 mlog(ML_ERROR, "new_inode failed!\n");
393 /* populate as many fields early on as possible - many of
394 * these are used by the support functions here and in
396 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
397 OCFS2_I(inode)->ip_blkno = fe_blkno;
402 inode->i_mode = mode;
403 spin_lock(&osb->osb_lock);
404 inode->i_generation = osb->s_next_generation++;
405 spin_unlock(&osb->osb_lock);
407 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
413 ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
415 status = ocfs2_journal_access(handle, inode, *new_fe_bh,
416 OCFS2_JOURNAL_ACCESS_CREATE);
422 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
423 memset(fe, 0, osb->sb->s_blocksize);
425 fe->i_generation = cpu_to_le32(inode->i_generation);
426 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
427 fe->i_blkno = cpu_to_le64(fe_blkno);
428 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
429 fe->i_suballoc_slot = cpu_to_le16(osb->slot_num);
430 fe->i_uid = cpu_to_le32(current->fsuid);
431 if (dir->i_mode & S_ISGID) {
432 fe->i_gid = cpu_to_le32(dir->i_gid);
436 fe->i_gid = cpu_to_le32(current->fsgid);
437 fe->i_mode = cpu_to_le16(mode);
438 if (S_ISCHR(mode) || S_ISBLK(mode))
439 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
441 fe->i_links_count = cpu_to_le16(inode->i_nlink);
443 fe->i_last_eb_blk = 0;
444 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
445 le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
446 fe->i_atime = fe->i_ctime = fe->i_mtime =
447 cpu_to_le64(CURRENT_TIME.tv_sec);
448 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
449 cpu_to_le32(CURRENT_TIME.tv_nsec);
452 fel = &fe->id2.i_list;
453 fel->l_tree_depth = 0;
454 fel->l_next_free_rec = 0;
455 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
457 status = ocfs2_journal_dirty(handle, *new_fe_bh);
463 if (ocfs2_populate_inode(inode, fe, 1) < 0) {
464 mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
465 "i_blkno=%llu, i_ino=%lu\n",
466 (unsigned long long)(*new_fe_bh)->b_blocknr,
467 (unsigned long long)le64_to_cpu(fe->i_blkno),
472 ocfs2_inode_set_new(osb, inode);
473 if (!ocfs2_mount_local(osb)) {
474 status = ocfs2_create_new_inode_locks(inode);
479 status = 0; /* error in ocfs2_create_new_inode_locks is not
497 static int ocfs2_mkdir(struct inode *dir,
498 struct dentry *dentry,
503 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
504 dentry->d_name.len, dentry->d_name.name);
505 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
511 static int ocfs2_create(struct inode *dir,
512 struct dentry *dentry,
514 struct nameidata *nd)
518 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
519 dentry->d_name.len, dentry->d_name.name);
520 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
526 static int ocfs2_link(struct dentry *old_dentry,
528 struct dentry *dentry)
531 struct inode *inode = old_dentry->d_inode;
533 struct buffer_head *fe_bh = NULL;
534 struct buffer_head *parent_fe_bh = NULL;
535 struct buffer_head *de_bh = NULL;
536 struct ocfs2_dinode *fe = NULL;
537 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
539 mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
540 old_dentry->d_name.len, old_dentry->d_name.name,
541 dentry->d_name.len, dentry->d_name.name);
543 if (S_ISDIR(inode->i_mode))
546 err = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
558 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
563 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
565 dentry->d_name.len, &de_bh);
571 err = ocfs2_meta_lock(inode, &fe_bh, 1);
578 fe = (struct ocfs2_dinode *) fe_bh->b_data;
579 if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
581 goto out_unlock_inode;
584 handle = ocfs2_start_trans(osb, OCFS2_LINK_CREDITS);
585 if (IS_ERR(handle)) {
586 err = PTR_ERR(handle);
589 goto out_unlock_inode;
592 err = ocfs2_journal_access(handle, inode, fe_bh,
593 OCFS2_JOURNAL_ACCESS_WRITE);
600 inode->i_ctime = CURRENT_TIME;
601 fe->i_links_count = cpu_to_le16(inode->i_nlink);
602 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
603 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
605 err = ocfs2_journal_dirty(handle, fe_bh);
607 le16_add_cpu(&fe->i_links_count, -1);
613 err = ocfs2_add_entry(handle, dentry, inode,
614 OCFS2_I(inode)->ip_blkno,
615 parent_fe_bh, de_bh);
617 le16_add_cpu(&fe->i_links_count, -1);
623 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
629 atomic_inc(&inode->i_count);
630 dentry->d_op = &ocfs2_dentry_ops;
631 d_instantiate(dentry, inode);
634 ocfs2_commit_trans(osb, handle);
636 ocfs2_meta_unlock(inode, 1);
639 ocfs2_meta_unlock(dir, 1);
646 brelse(parent_fe_bh);
654 * Takes and drops an exclusive lock on the given dentry. This will
655 * force other nodes to drop it.
657 static int ocfs2_remote_dentry_delete(struct dentry *dentry)
661 ret = ocfs2_dentry_lock(dentry, 1);
665 ocfs2_dentry_unlock(dentry, 1);
670 static inline int inode_is_unlinkable(struct inode *inode)
672 if (S_ISDIR(inode->i_mode)) {
673 if (inode->i_nlink == 2)
678 if (inode->i_nlink == 1)
683 static int ocfs2_unlink(struct inode *dir,
684 struct dentry *dentry)
687 int child_locked = 0;
688 struct inode *inode = dentry->d_inode;
689 struct inode *orphan_dir = NULL;
690 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
692 struct ocfs2_dinode *fe = NULL;
693 struct buffer_head *fe_bh = NULL;
694 struct buffer_head *parent_node_bh = NULL;
695 handle_t *handle = NULL;
696 struct ocfs2_dir_entry *dirent = NULL;
697 struct buffer_head *dirent_bh = NULL;
698 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
699 struct buffer_head *orphan_entry_bh = NULL;
701 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
702 dentry->d_name.len, dentry->d_name.name);
704 BUG_ON(dentry->d_parent->d_inode != dir);
706 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
708 if (inode == osb->root_inode) {
709 mlog(0, "Cannot delete the root directory\n");
713 status = ocfs2_meta_lock(dir, &parent_node_bh, 1);
715 if (status != -ENOENT)
720 status = ocfs2_find_files_on_disk(dentry->d_name.name,
721 dentry->d_name.len, &blkno,
722 dir, &dirent_bh, &dirent);
724 if (status != -ENOENT)
729 if (OCFS2_I(inode)->ip_blkno != blkno) {
732 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
733 (unsigned long long)OCFS2_I(inode)->ip_blkno,
734 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
738 status = ocfs2_meta_lock(inode, &fe_bh, 1);
740 if (status != -ENOENT)
746 if (S_ISDIR(inode->i_mode)) {
747 if (!ocfs2_empty_dir(inode)) {
750 } else if (inode->i_nlink != 2) {
756 status = ocfs2_remote_dentry_delete(dentry);
758 /* This vote should succeed under all normal
764 if (inode_is_unlinkable(inode)) {
765 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
774 handle = ocfs2_start_trans(osb, OCFS2_UNLINK_CREDITS);
775 if (IS_ERR(handle)) {
776 status = PTR_ERR(handle);
782 status = ocfs2_journal_access(handle, inode, fe_bh,
783 OCFS2_JOURNAL_ACCESS_WRITE);
789 fe = (struct ocfs2_dinode *) fe_bh->b_data;
791 if (inode_is_unlinkable(inode)) {
792 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
793 orphan_entry_bh, orphan_dir);
800 /* delete the name from the parent dir */
801 status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
807 if (S_ISDIR(inode->i_mode))
810 fe->i_links_count = cpu_to_le16(inode->i_nlink);
812 status = ocfs2_journal_dirty(handle, fe_bh);
818 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
819 if (S_ISDIR(inode->i_mode))
822 status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
825 if (S_ISDIR(inode->i_mode))
831 ocfs2_commit_trans(osb, handle);
834 ocfs2_meta_unlock(inode, 1);
836 ocfs2_meta_unlock(dir, 1);
839 /* This was locked for us in ocfs2_prepare_orphan_dir() */
840 ocfs2_meta_unlock(orphan_dir, 1);
841 mutex_unlock(&orphan_dir->i_mutex);
852 brelse(parent_node_bh);
855 brelse(orphan_entry_bh);
863 * The only place this should be used is rename!
864 * if they have the same id, then the 1st one is the only one locked.
866 static int ocfs2_double_lock(struct ocfs2_super *osb,
867 struct buffer_head **bh1,
868 struct inode *inode1,
869 struct buffer_head **bh2,
870 struct inode *inode2)
873 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
874 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
875 struct buffer_head **tmpbh;
876 struct inode *tmpinode;
878 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
879 (unsigned long long)oi1->ip_blkno,
880 (unsigned long long)oi2->ip_blkno);
887 /* we always want to lock the one with the lower lockid first. */
888 if (oi1->ip_blkno != oi2->ip_blkno) {
889 if (oi1->ip_blkno < oi2->ip_blkno) {
890 /* switch id1 and id2 around */
891 mlog(0, "switching them around...\n");
901 status = ocfs2_meta_lock(inode2, bh2, 1);
903 if (status != -ENOENT)
910 status = ocfs2_meta_lock(inode1, bh1, 1);
913 * An error return must mean that no cluster locks
914 * were held on function exit.
916 if (oi1->ip_blkno != oi2->ip_blkno)
917 ocfs2_meta_unlock(inode2, 1);
919 if (status != -ENOENT)
928 static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
930 ocfs2_meta_unlock(inode1, 1);
932 if (inode1 != inode2)
933 ocfs2_meta_unlock(inode2, 1);
936 static int ocfs2_rename(struct inode *old_dir,
937 struct dentry *old_dentry,
938 struct inode *new_dir,
939 struct dentry *new_dentry)
941 int status = 0, rename_lock = 0, parents_locked = 0;
942 int old_child_locked = 0, new_child_locked = 0;
943 struct inode *old_inode = old_dentry->d_inode;
944 struct inode *new_inode = new_dentry->d_inode;
945 struct inode *orphan_dir = NULL;
946 struct ocfs2_dinode *newfe = NULL;
947 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
948 struct buffer_head *orphan_entry_bh = NULL;
949 struct buffer_head *newfe_bh = NULL;
950 struct buffer_head *old_inode_bh = NULL;
951 struct buffer_head *insert_entry_bh = NULL;
952 struct ocfs2_super *osb = NULL;
954 handle_t *handle = NULL;
955 struct buffer_head *old_dir_bh = NULL;
956 struct buffer_head *new_dir_bh = NULL;
957 struct ocfs2_dir_entry *old_inode_dot_dot_de = NULL, *old_de = NULL,
959 struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
960 struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
961 // this is the 1st dirent bh
962 nlink_t old_dir_nlink = old_dir->i_nlink;
963 struct ocfs2_dinode *old_di;
965 /* At some point it might be nice to break this function up a
968 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
969 old_dir, old_dentry, new_dir, new_dentry,
970 old_dentry->d_name.len, old_dentry->d_name.name,
971 new_dentry->d_name.len, new_dentry->d_name.name);
973 osb = OCFS2_SB(old_dir->i_sb);
976 if (!igrab(new_inode))
980 /* Assume a directory hierarchy thusly:
983 * a,b,c, and d are all directories.
985 * from cwd of 'a' on both nodes:
989 * And that's why, just like the VFS, we need a file system
991 if (old_dentry != new_dentry) {
992 status = ocfs2_rename_lock(osb);
1000 /* if old and new are the same, this'll just do one lock. */
1001 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1002 &new_dir_bh, new_dir);
1009 /* make sure both dirs have bhs
1010 * get an extra ref on old_dir_bh if old==new */
1013 new_dir_bh = old_dir_bh;
1016 mlog(ML_ERROR, "no old_dir_bh!\n");
1023 * Aside from allowing a meta data update, the locking here
1024 * also ensures that the vote thread on other nodes won't have
1025 * to concurrently downconvert the inode and the dentry locks.
1027 status = ocfs2_meta_lock(old_inode, &old_inode_bh, 1);
1029 if (status != -ENOENT)
1033 old_child_locked = 1;
1035 status = ocfs2_remote_dentry_delete(old_dentry);
1041 if (S_ISDIR(old_inode->i_mode)) {
1042 u64 old_inode_parent;
1044 status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
1045 old_inode, &old_inode_de_bh,
1046 &old_inode_dot_dot_de);
1052 if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
1057 if (!new_inode && new_dir != old_dir &&
1058 new_dir->i_nlink >= OCFS2_LINK_MAX) {
1065 old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
1066 old_dentry->d_name.len,
1072 * Check for inode number is _not_ due to possible IO errors.
1073 * We might rmdir the source, keep it as pwd of some process
1074 * and merrily kill the link to whatever was created under the
1075 * same name. Goodbye sticky bit ;-<
1077 if (le64_to_cpu(old_de->inode) != OCFS2_I(old_inode)->ip_blkno)
1080 /* check if the target already exists (in which case we need
1082 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1083 new_dentry->d_name.len,
1084 &newfe_blkno, new_dir, &new_de_bh,
1086 /* The only error we allow here is -ENOENT because the new
1087 * file not existing is perfectly valid. */
1088 if ((status < 0) && (status != -ENOENT)) {
1089 /* If we cannot find the file specified we should just */
1090 /* return the error... */
1095 if (!new_de && new_inode)
1096 mlog(ML_ERROR, "inode %lu does not exist in it's parent "
1097 "directory!", new_inode->i_ino);
1099 /* In case we need to overwrite an existing file, we blow it
1102 /* VFS didn't think there existed an inode here, but
1103 * someone else in the cluster must have raced our
1104 * rename to create one. Today we error cleanly, in
1105 * the future we should consider calling iget to build
1106 * a new struct inode for this entry. */
1110 mlog(0, "We found an inode for name %.*s but VFS "
1111 "didn't give us one.\n", new_dentry->d_name.len,
1112 new_dentry->d_name.name);
1116 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1119 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1120 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1121 (unsigned long long)newfe_blkno,
1122 OCFS2_I(new_inode)->ip_flags);
1126 status = ocfs2_meta_lock(new_inode, &newfe_bh, 1);
1128 if (status != -ENOENT)
1132 new_child_locked = 1;
1134 status = ocfs2_remote_dentry_delete(new_dentry);
1140 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1142 mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
1143 "newfebh=%p bhblocknr=%llu\n", new_de,
1144 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
1145 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1147 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
1148 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
1158 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1160 status = ocfs2_check_dir_for_entry(new_dir,
1161 new_dentry->d_name.name,
1162 new_dentry->d_name.len);
1166 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1167 new_dentry->d_name.name,
1168 new_dentry->d_name.len,
1176 handle = ocfs2_start_trans(osb, OCFS2_RENAME_CREDITS);
1177 if (IS_ERR(handle)) {
1178 status = PTR_ERR(handle);
1185 if (S_ISDIR(new_inode->i_mode)) {
1186 if (!ocfs2_empty_dir(new_inode) ||
1187 new_inode->i_nlink != 2) {
1188 status = -ENOTEMPTY;
1192 status = ocfs2_journal_access(handle, new_inode, newfe_bh,
1193 OCFS2_JOURNAL_ACCESS_WRITE);
1199 if (S_ISDIR(new_inode->i_mode) ||
1200 (newfe->i_links_count == cpu_to_le16(1))){
1201 status = ocfs2_orphan_add(osb, handle, new_inode,
1203 orphan_entry_bh, orphan_dir);
1210 /* change the dirent to point to the correct inode */
1211 status = ocfs2_update_entry(new_dir, handle, new_de_bh,
1217 new_dir->i_version++;
1219 if (S_ISDIR(new_inode->i_mode))
1220 newfe->i_links_count = 0;
1222 le16_add_cpu(&newfe->i_links_count, -1);
1224 status = ocfs2_journal_dirty(handle, newfe_bh);
1230 /* if the name was not found in new_dir, add it now */
1231 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1232 OCFS2_I(old_inode)->ip_blkno,
1233 new_dir_bh, insert_entry_bh);
1236 old_inode->i_ctime = CURRENT_TIME;
1237 mark_inode_dirty(old_inode);
1239 status = ocfs2_journal_access(handle, old_inode, old_inode_bh,
1240 OCFS2_JOURNAL_ACCESS_WRITE);
1242 old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
1244 old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
1245 old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
1247 status = ocfs2_journal_dirty(handle, old_inode_bh);
1253 /* now that the name has been added to new_dir, remove the old name */
1254 status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
1261 new_inode->i_nlink--;
1262 new_inode->i_ctime = CURRENT_TIME;
1264 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1265 if (old_inode_de_bh) {
1266 status = ocfs2_update_entry(old_inode, handle, old_inode_de_bh,
1267 old_inode_dot_dot_de, new_dir);
1270 new_inode->i_nlink--;
1273 mark_inode_dirty(new_dir);
1276 mark_inode_dirty(old_dir);
1277 ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1279 mark_inode_dirty(new_inode);
1280 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1283 if (old_dir != new_dir) {
1284 /* Keep the same times on both directories.*/
1285 new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
1288 * This will also pick up the i_nlink change from the
1291 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
1294 if (old_dir_nlink != old_dir->i_nlink) {
1296 mlog(ML_ERROR, "need to change nlink for old dir "
1297 "%llu from %d to %d but bh is NULL!\n",
1298 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1299 (int)old_dir_nlink, old_dir->i_nlink);
1301 struct ocfs2_dinode *fe;
1302 status = ocfs2_journal_access(handle, old_dir,
1304 OCFS2_JOURNAL_ACCESS_WRITE);
1305 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1306 fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
1307 status = ocfs2_journal_dirty(handle, old_dir_bh);
1311 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
1315 ocfs2_rename_unlock(osb);
1318 ocfs2_commit_trans(osb, handle);
1321 ocfs2_double_unlock(old_dir, new_dir);
1323 if (old_child_locked)
1324 ocfs2_meta_unlock(old_inode, 1);
1326 if (new_child_locked)
1327 ocfs2_meta_unlock(new_inode, 1);
1330 /* This was locked for us in ocfs2_prepare_orphan_dir() */
1331 ocfs2_meta_unlock(orphan_dir, 1);
1332 mutex_unlock(&orphan_dir->i_mutex);
1337 sync_mapping_buffers(old_inode->i_mapping);
1344 brelse(old_inode_bh);
1353 if (old_inode_de_bh)
1354 brelse(old_inode_de_bh);
1355 if (orphan_entry_bh)
1356 brelse(orphan_entry_bh);
1357 if (insert_entry_bh)
1358 brelse(insert_entry_bh);
1366 * we expect i_size = strlen(symname). Copy symname into the file
1367 * data, including the null terminator.
1369 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1371 struct inode *inode,
1372 const char *symname)
1374 struct buffer_head **bhs = NULL;
1376 struct super_block *sb = osb->sb;
1377 u64 p_blkno, p_blocks;
1378 int virtual, blocks, status, i, bytes_left;
1380 bytes_left = i_size_read(inode) + 1;
1381 /* we can't trust i_blocks because we're actually going to
1382 * write i_size + 1 bytes. */
1383 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1385 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1386 (unsigned long long)inode->i_blocks,
1387 i_size_read(inode), blocks);
1389 /* Sanity check -- make sure we're going to fit. */
1391 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1397 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1404 status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
1411 /* links can never be larger than one cluster so we know this
1412 * is all going to be contiguous, but do a sanity check
1414 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1421 while(bytes_left > 0) {
1422 c = &symname[virtual * sb->s_blocksize];
1424 bhs[virtual] = sb_getblk(sb, p_blkno);
1425 if (!bhs[virtual]) {
1430 ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
1432 status = ocfs2_journal_access(handle, inode, bhs[virtual],
1433 OCFS2_JOURNAL_ACCESS_CREATE);
1439 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1441 memcpy(bhs[virtual]->b_data, c,
1442 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1445 status = ocfs2_journal_dirty(handle, bhs[virtual]);
1453 bytes_left -= sb->s_blocksize;
1460 for(i = 0; i < blocks; i++)
1470 static int ocfs2_symlink(struct inode *dir,
1471 struct dentry *dentry,
1472 const char *symname)
1474 int status, l, credits;
1476 struct ocfs2_super *osb = NULL;
1477 struct inode *inode = NULL;
1478 struct super_block *sb;
1479 struct buffer_head *new_fe_bh = NULL;
1480 struct buffer_head *de_bh = NULL;
1481 struct buffer_head *parent_fe_bh = NULL;
1482 struct ocfs2_dinode *fe = NULL;
1483 struct ocfs2_dinode *dirfe;
1484 handle_t *handle = NULL;
1485 struct ocfs2_alloc_context *inode_ac = NULL;
1486 struct ocfs2_alloc_context *data_ac = NULL;
1488 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1489 dentry, symname, dentry->d_name.len, dentry->d_name.name);
1494 l = strlen(symname) + 1;
1496 credits = ocfs2_calc_symlink_credits(sb);
1498 /* lock the parent directory */
1499 status = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
1501 if (status != -ENOENT)
1506 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1507 if (!dirfe->i_links_count) {
1508 /* can't make a file in a deleted directory. */
1513 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1514 dentry->d_name.len);
1518 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1519 dentry->d_name.name,
1520 dentry->d_name.len, &de_bh);
1526 status = ocfs2_reserve_new_inode(osb, &inode_ac);
1528 if (status != -ENOSPC)
1533 /* don't reserve bitmap space for fast symlinks. */
1534 if (l > ocfs2_fast_symlink_chars(sb)) {
1535 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
1537 if (status != -ENOSPC)
1543 handle = ocfs2_start_trans(osb, credits);
1544 if (IS_ERR(handle)) {
1545 status = PTR_ERR(handle);
1551 status = ocfs2_mknod_locked(osb, dir, dentry,
1552 S_IFLNK | S_IRWXUGO, 0,
1553 &new_fe_bh, parent_fe_bh, handle,
1560 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1563 if (l > ocfs2_fast_symlink_chars(sb)) {
1566 inode->i_op = &ocfs2_symlink_inode_operations;
1567 status = ocfs2_do_extend_allocation(osb, inode, &offset, 1, 0,
1569 handle, data_ac, NULL,
1572 if (status != -ENOSPC && status != -EINTR) {
1574 "Failed to extend file to %llu\n",
1575 (unsigned long long)newsize);
1581 i_size_write(inode, newsize);
1582 inode->i_blocks = ocfs2_inode_sector_count(inode);
1584 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1585 memcpy((char *) fe->id2.i_symlink, symname, l);
1586 i_size_write(inode, newsize);
1587 inode->i_blocks = 0;
1590 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1596 if (!ocfs2_inode_is_fast_symlink(inode)) {
1597 status = ocfs2_create_symlink_data(osb, handle, inode,
1605 status = ocfs2_add_entry(handle, dentry, inode,
1606 le64_to_cpu(fe->i_blkno), parent_fe_bh,
1613 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
1619 insert_inode_hash(inode);
1620 dentry->d_op = &ocfs2_dentry_ops;
1621 d_instantiate(dentry, inode);
1624 ocfs2_commit_trans(osb, handle);
1626 ocfs2_meta_unlock(dir, 1);
1631 brelse(parent_fe_bh);
1635 ocfs2_free_alloc_context(inode_ac);
1637 ocfs2_free_alloc_context(data_ac);
1638 if ((status < 0) && inode)
1646 static int ocfs2_blkno_stringify(u64 blkno, char *name)
1648 int status, namelen;
1652 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
1662 if (namelen != OCFS2_ORPHAN_NAMELEN) {
1668 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
1677 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
1678 struct inode **ret_orphan_dir,
1679 struct inode *inode,
1681 struct buffer_head **de_bh)
1683 struct inode *orphan_dir_inode;
1684 struct buffer_head *orphan_dir_bh = NULL;
1687 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1693 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1694 ORPHAN_DIR_SYSTEM_INODE,
1696 if (!orphan_dir_inode) {
1702 mutex_lock(&orphan_dir_inode->i_mutex);
1704 status = ocfs2_meta_lock(orphan_dir_inode, &orphan_dir_bh, 1);
1710 status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
1711 orphan_dir_bh, name,
1712 OCFS2_ORPHAN_NAMELEN, de_bh);
1714 ocfs2_meta_unlock(orphan_dir_inode, 1);
1720 *ret_orphan_dir = orphan_dir_inode;
1724 mutex_unlock(&orphan_dir_inode->i_mutex);
1725 iput(orphan_dir_inode);
1729 brelse(orphan_dir_bh);
1735 static int ocfs2_orphan_add(struct ocfs2_super *osb,
1737 struct inode *inode,
1738 struct ocfs2_dinode *fe,
1740 struct buffer_head *de_bh,
1741 struct inode *orphan_dir_inode)
1743 struct buffer_head *orphan_dir_bh = NULL;
1745 struct ocfs2_dinode *orphan_fe;
1747 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
1749 status = ocfs2_read_block(osb,
1750 OCFS2_I(orphan_dir_inode)->ip_blkno,
1751 &orphan_dir_bh, OCFS2_BH_CACHED,
1758 status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
1759 OCFS2_JOURNAL_ACCESS_WRITE);
1765 /* we're a cluster, and nlink can change on disk from
1766 * underneath us... */
1767 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
1768 if (S_ISDIR(inode->i_mode))
1769 le16_add_cpu(&orphan_fe->i_links_count, 1);
1770 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
1772 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
1778 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
1779 OCFS2_ORPHAN_NAMELEN, inode,
1780 OCFS2_I(inode)->ip_blkno,
1781 orphan_dir_bh, de_bh);
1787 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
1789 /* Record which orphan dir our inode now resides
1790 * in. delete_inode will use this to determine which orphan
1792 fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
1794 mlog(0, "Inode %llu orphaned in slot %d\n",
1795 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
1799 brelse(orphan_dir_bh);
1805 /* unlike orphan_add, we expect the orphan dir to already be locked here. */
1806 int ocfs2_orphan_del(struct ocfs2_super *osb,
1808 struct inode *orphan_dir_inode,
1809 struct inode *inode,
1810 struct buffer_head *orphan_dir_bh)
1812 char name[OCFS2_ORPHAN_NAMELEN + 1];
1813 struct ocfs2_dinode *orphan_fe;
1815 struct buffer_head *target_de_bh = NULL;
1816 struct ocfs2_dir_entry *target_de = NULL;
1820 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1826 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
1827 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
1828 OCFS2_ORPHAN_NAMELEN);
1830 /* find it's spot in the orphan directory */
1831 target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
1832 orphan_dir_inode, &target_de);
1833 if (!target_de_bh) {
1839 /* remove it from the orphan directory */
1840 status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
1847 status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
1848 OCFS2_JOURNAL_ACCESS_WRITE);
1854 /* do the i_nlink dance! :) */
1855 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
1856 if (S_ISDIR(inode->i_mode))
1857 le16_add_cpu(&orphan_fe->i_links_count, -1);
1858 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
1860 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
1868 brelse(target_de_bh);
1874 const struct inode_operations ocfs2_dir_iops = {
1875 .create = ocfs2_create,
1876 .lookup = ocfs2_lookup,
1878 .unlink = ocfs2_unlink,
1879 .rmdir = ocfs2_unlink,
1880 .symlink = ocfs2_symlink,
1881 .mkdir = ocfs2_mkdir,
1882 .mknod = ocfs2_mknod,
1883 .rename = ocfs2_rename,
1884 .setattr = ocfs2_setattr,
1885 .getattr = ocfs2_getattr,
1886 .permission = ocfs2_permission,