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 /* Reserve a cluster if creating an extent based directory. */
254 if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) {
255 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
257 if (status != -ENOSPC)
263 handle = ocfs2_start_trans(osb, OCFS2_MKNOD_CREDITS);
264 if (IS_ERR(handle)) {
265 status = PTR_ERR(handle);
271 /* do the real work now. */
272 status = ocfs2_mknod_locked(osb, dir, dentry, mode, dev,
273 &new_fe_bh, parent_fe_bh, handle,
281 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
288 status = ocfs2_journal_access(handle, dir, parent_fe_bh,
289 OCFS2_JOURNAL_ACCESS_WRITE);
294 le16_add_cpu(&dirfe->i_links_count, 1);
295 status = ocfs2_journal_dirty(handle, parent_fe_bh);
303 status = ocfs2_add_entry(handle, dentry, inode,
304 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
311 status = ocfs2_dentry_attach_lock(dentry, inode,
312 OCFS2_I(dir)->ip_blkno);
318 insert_inode_hash(inode);
319 dentry->d_op = &ocfs2_dentry_ops;
320 d_instantiate(dentry, inode);
324 ocfs2_commit_trans(osb, handle);
326 ocfs2_meta_unlock(dir, 1);
328 if (status == -ENOSPC)
329 mlog(0, "Disk is full\n");
338 brelse(parent_fe_bh);
340 if ((status < 0) && inode)
344 ocfs2_free_alloc_context(inode_ac);
347 ocfs2_free_alloc_context(data_ac);
354 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
356 struct dentry *dentry, int mode,
358 struct buffer_head **new_fe_bh,
359 struct buffer_head *parent_fe_bh,
361 struct inode **ret_inode,
362 struct ocfs2_alloc_context *inode_ac)
365 struct ocfs2_dinode *fe = NULL;
366 struct ocfs2_extent_list *fel;
369 struct inode *inode = NULL;
371 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
372 (unsigned long)dev, dentry->d_name.len,
373 dentry->d_name.name);
378 status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
385 inode = new_inode(dir->i_sb);
387 status = PTR_ERR(inode);
388 mlog(ML_ERROR, "new_inode failed!\n");
392 /* populate as many fields early on as possible - many of
393 * these are used by the support functions here and in
395 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
396 OCFS2_I(inode)->ip_blkno = fe_blkno;
401 inode->i_mode = mode;
402 spin_lock(&osb->osb_lock);
403 inode->i_generation = osb->s_next_generation++;
404 spin_unlock(&osb->osb_lock);
406 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
412 ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
414 status = ocfs2_journal_access(handle, inode, *new_fe_bh,
415 OCFS2_JOURNAL_ACCESS_CREATE);
421 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
422 memset(fe, 0, osb->sb->s_blocksize);
424 fe->i_generation = cpu_to_le32(inode->i_generation);
425 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
426 fe->i_blkno = cpu_to_le64(fe_blkno);
427 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
428 fe->i_suballoc_slot = cpu_to_le16(osb->slot_num);
429 fe->i_uid = cpu_to_le32(current->fsuid);
430 if (dir->i_mode & S_ISGID) {
431 fe->i_gid = cpu_to_le32(dir->i_gid);
435 fe->i_gid = cpu_to_le32(current->fsgid);
436 fe->i_mode = cpu_to_le16(mode);
437 if (S_ISCHR(mode) || S_ISBLK(mode))
438 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
440 fe->i_links_count = cpu_to_le16(inode->i_nlink);
442 fe->i_last_eb_blk = 0;
443 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
444 le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
445 fe->i_atime = fe->i_ctime = fe->i_mtime =
446 cpu_to_le64(CURRENT_TIME.tv_sec);
447 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
448 cpu_to_le32(CURRENT_TIME.tv_nsec);
452 * If supported, directories start with inline data.
454 if (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) {
455 u16 feat = le16_to_cpu(fe->i_dyn_features);
457 fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
459 fe->id2.i_data.id_count = cpu_to_le16(ocfs2_max_inline_data(osb->sb));
461 fel = &fe->id2.i_list;
462 fel->l_tree_depth = 0;
463 fel->l_next_free_rec = 0;
464 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
467 status = ocfs2_journal_dirty(handle, *new_fe_bh);
473 if (ocfs2_populate_inode(inode, fe, 1) < 0) {
474 mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
475 "i_blkno=%llu, i_ino=%lu\n",
476 (unsigned long long)(*new_fe_bh)->b_blocknr,
477 (unsigned long long)le64_to_cpu(fe->i_blkno),
482 ocfs2_inode_set_new(osb, inode);
483 if (!ocfs2_mount_local(osb)) {
484 status = ocfs2_create_new_inode_locks(inode);
489 status = 0; /* error in ocfs2_create_new_inode_locks is not
507 static int ocfs2_mkdir(struct inode *dir,
508 struct dentry *dentry,
513 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
514 dentry->d_name.len, dentry->d_name.name);
515 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
521 static int ocfs2_create(struct inode *dir,
522 struct dentry *dentry,
524 struct nameidata *nd)
528 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
529 dentry->d_name.len, dentry->d_name.name);
530 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
536 static int ocfs2_link(struct dentry *old_dentry,
538 struct dentry *dentry)
541 struct inode *inode = old_dentry->d_inode;
543 struct buffer_head *fe_bh = NULL;
544 struct buffer_head *parent_fe_bh = NULL;
545 struct buffer_head *de_bh = NULL;
546 struct ocfs2_dinode *fe = NULL;
547 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
549 mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
550 old_dentry->d_name.len, old_dentry->d_name.name,
551 dentry->d_name.len, dentry->d_name.name);
553 if (S_ISDIR(inode->i_mode))
556 err = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
568 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
573 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
575 dentry->d_name.len, &de_bh);
581 err = ocfs2_meta_lock(inode, &fe_bh, 1);
588 fe = (struct ocfs2_dinode *) fe_bh->b_data;
589 if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
591 goto out_unlock_inode;
594 handle = ocfs2_start_trans(osb, OCFS2_LINK_CREDITS);
595 if (IS_ERR(handle)) {
596 err = PTR_ERR(handle);
599 goto out_unlock_inode;
602 err = ocfs2_journal_access(handle, inode, fe_bh,
603 OCFS2_JOURNAL_ACCESS_WRITE);
610 inode->i_ctime = CURRENT_TIME;
611 fe->i_links_count = cpu_to_le16(inode->i_nlink);
612 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
613 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
615 err = ocfs2_journal_dirty(handle, fe_bh);
617 le16_add_cpu(&fe->i_links_count, -1);
623 err = ocfs2_add_entry(handle, dentry, inode,
624 OCFS2_I(inode)->ip_blkno,
625 parent_fe_bh, de_bh);
627 le16_add_cpu(&fe->i_links_count, -1);
633 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
639 atomic_inc(&inode->i_count);
640 dentry->d_op = &ocfs2_dentry_ops;
641 d_instantiate(dentry, inode);
644 ocfs2_commit_trans(osb, handle);
646 ocfs2_meta_unlock(inode, 1);
649 ocfs2_meta_unlock(dir, 1);
656 brelse(parent_fe_bh);
664 * Takes and drops an exclusive lock on the given dentry. This will
665 * force other nodes to drop it.
667 static int ocfs2_remote_dentry_delete(struct dentry *dentry)
671 ret = ocfs2_dentry_lock(dentry, 1);
675 ocfs2_dentry_unlock(dentry, 1);
680 static inline int inode_is_unlinkable(struct inode *inode)
682 if (S_ISDIR(inode->i_mode)) {
683 if (inode->i_nlink == 2)
688 if (inode->i_nlink == 1)
693 static int ocfs2_unlink(struct inode *dir,
694 struct dentry *dentry)
697 int child_locked = 0;
698 struct inode *inode = dentry->d_inode;
699 struct inode *orphan_dir = NULL;
700 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
702 struct ocfs2_dinode *fe = NULL;
703 struct buffer_head *fe_bh = NULL;
704 struct buffer_head *parent_node_bh = NULL;
705 handle_t *handle = NULL;
706 struct ocfs2_dir_entry *dirent = NULL;
707 struct buffer_head *dirent_bh = NULL;
708 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
709 struct buffer_head *orphan_entry_bh = NULL;
711 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
712 dentry->d_name.len, dentry->d_name.name);
714 BUG_ON(dentry->d_parent->d_inode != dir);
716 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
718 if (inode == osb->root_inode) {
719 mlog(0, "Cannot delete the root directory\n");
723 status = ocfs2_meta_lock(dir, &parent_node_bh, 1);
725 if (status != -ENOENT)
730 status = ocfs2_find_files_on_disk(dentry->d_name.name,
731 dentry->d_name.len, &blkno,
732 dir, &dirent_bh, &dirent);
734 if (status != -ENOENT)
739 if (OCFS2_I(inode)->ip_blkno != blkno) {
742 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
743 (unsigned long long)OCFS2_I(inode)->ip_blkno,
744 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
748 status = ocfs2_meta_lock(inode, &fe_bh, 1);
750 if (status != -ENOENT)
756 if (S_ISDIR(inode->i_mode)) {
757 if (!ocfs2_empty_dir(inode)) {
760 } else if (inode->i_nlink != 2) {
766 status = ocfs2_remote_dentry_delete(dentry);
768 /* This vote should succeed under all normal
774 if (inode_is_unlinkable(inode)) {
775 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
784 handle = ocfs2_start_trans(osb, OCFS2_UNLINK_CREDITS);
785 if (IS_ERR(handle)) {
786 status = PTR_ERR(handle);
792 status = ocfs2_journal_access(handle, inode, fe_bh,
793 OCFS2_JOURNAL_ACCESS_WRITE);
799 fe = (struct ocfs2_dinode *) fe_bh->b_data;
801 if (inode_is_unlinkable(inode)) {
802 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
803 orphan_entry_bh, orphan_dir);
810 /* delete the name from the parent dir */
811 status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
817 if (S_ISDIR(inode->i_mode))
820 fe->i_links_count = cpu_to_le16(inode->i_nlink);
822 status = ocfs2_journal_dirty(handle, fe_bh);
828 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
829 if (S_ISDIR(inode->i_mode))
832 status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
835 if (S_ISDIR(inode->i_mode))
841 ocfs2_commit_trans(osb, handle);
844 ocfs2_meta_unlock(inode, 1);
846 ocfs2_meta_unlock(dir, 1);
849 /* This was locked for us in ocfs2_prepare_orphan_dir() */
850 ocfs2_meta_unlock(orphan_dir, 1);
851 mutex_unlock(&orphan_dir->i_mutex);
862 brelse(parent_node_bh);
865 brelse(orphan_entry_bh);
873 * The only place this should be used is rename!
874 * if they have the same id, then the 1st one is the only one locked.
876 static int ocfs2_double_lock(struct ocfs2_super *osb,
877 struct buffer_head **bh1,
878 struct inode *inode1,
879 struct buffer_head **bh2,
880 struct inode *inode2)
883 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
884 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
885 struct buffer_head **tmpbh;
886 struct inode *tmpinode;
888 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
889 (unsigned long long)oi1->ip_blkno,
890 (unsigned long long)oi2->ip_blkno);
897 /* we always want to lock the one with the lower lockid first. */
898 if (oi1->ip_blkno != oi2->ip_blkno) {
899 if (oi1->ip_blkno < oi2->ip_blkno) {
900 /* switch id1 and id2 around */
901 mlog(0, "switching them around...\n");
911 status = ocfs2_meta_lock(inode2, bh2, 1);
913 if (status != -ENOENT)
920 status = ocfs2_meta_lock(inode1, bh1, 1);
923 * An error return must mean that no cluster locks
924 * were held on function exit.
926 if (oi1->ip_blkno != oi2->ip_blkno)
927 ocfs2_meta_unlock(inode2, 1);
929 if (status != -ENOENT)
938 static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
940 ocfs2_meta_unlock(inode1, 1);
942 if (inode1 != inode2)
943 ocfs2_meta_unlock(inode2, 1);
946 static int ocfs2_rename(struct inode *old_dir,
947 struct dentry *old_dentry,
948 struct inode *new_dir,
949 struct dentry *new_dentry)
951 int status = 0, rename_lock = 0, parents_locked = 0;
952 int old_child_locked = 0, new_child_locked = 0;
953 struct inode *old_inode = old_dentry->d_inode;
954 struct inode *new_inode = new_dentry->d_inode;
955 struct inode *orphan_dir = NULL;
956 struct ocfs2_dinode *newfe = NULL;
957 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
958 struct buffer_head *orphan_entry_bh = NULL;
959 struct buffer_head *newfe_bh = NULL;
960 struct buffer_head *old_inode_bh = NULL;
961 struct buffer_head *insert_entry_bh = NULL;
962 struct ocfs2_super *osb = NULL;
963 u64 newfe_blkno, old_de_ino;
964 handle_t *handle = NULL;
965 struct buffer_head *old_dir_bh = NULL;
966 struct buffer_head *new_dir_bh = NULL;
967 struct ocfs2_dir_entry *old_inode_dot_dot_de = NULL, *old_de = NULL,
969 struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
970 struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
971 // this is the 1st dirent bh
972 nlink_t old_dir_nlink = old_dir->i_nlink;
973 struct ocfs2_dinode *old_di;
975 /* At some point it might be nice to break this function up a
978 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
979 old_dir, old_dentry, new_dir, new_dentry,
980 old_dentry->d_name.len, old_dentry->d_name.name,
981 new_dentry->d_name.len, new_dentry->d_name.name);
983 osb = OCFS2_SB(old_dir->i_sb);
986 if (!igrab(new_inode))
990 /* Assume a directory hierarchy thusly:
993 * a,b,c, and d are all directories.
995 * from cwd of 'a' on both nodes:
999 * And that's why, just like the VFS, we need a file system
1001 if (old_dentry != new_dentry) {
1002 status = ocfs2_rename_lock(osb);
1010 /* if old and new are the same, this'll just do one lock. */
1011 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1012 &new_dir_bh, new_dir);
1019 /* make sure both dirs have bhs
1020 * get an extra ref on old_dir_bh if old==new */
1023 new_dir_bh = old_dir_bh;
1026 mlog(ML_ERROR, "no old_dir_bh!\n");
1033 * Aside from allowing a meta data update, the locking here
1034 * also ensures that the vote thread on other nodes won't have
1035 * to concurrently downconvert the inode and the dentry locks.
1037 status = ocfs2_meta_lock(old_inode, &old_inode_bh, 1);
1039 if (status != -ENOENT)
1043 old_child_locked = 1;
1045 status = ocfs2_remote_dentry_delete(old_dentry);
1051 if (S_ISDIR(old_inode->i_mode)) {
1052 u64 old_inode_parent;
1054 status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent,
1055 old_inode, &old_inode_de_bh,
1056 &old_inode_dot_dot_de);
1062 if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) {
1067 if (!new_inode && new_dir != old_dir &&
1068 new_dir->i_nlink >= OCFS2_LINK_MAX) {
1074 status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
1075 old_dentry->d_name.len,
1083 * Check for inode number is _not_ due to possible IO errors.
1084 * We might rmdir the source, keep it as pwd of some process
1085 * and merrily kill the link to whatever was created under the
1086 * same name. Goodbye sticky bit ;-<
1088 if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) {
1093 /* check if the target already exists (in which case we need
1095 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1096 new_dentry->d_name.len,
1097 &newfe_blkno, new_dir, &new_de_bh,
1099 /* The only error we allow here is -ENOENT because the new
1100 * file not existing is perfectly valid. */
1101 if ((status < 0) && (status != -ENOENT)) {
1102 /* If we cannot find the file specified we should just */
1103 /* return the error... */
1108 if (!new_de && new_inode)
1109 mlog(ML_ERROR, "inode %lu does not exist in it's parent "
1110 "directory!", new_inode->i_ino);
1112 /* In case we need to overwrite an existing file, we blow it
1115 /* VFS didn't think there existed an inode here, but
1116 * someone else in the cluster must have raced our
1117 * rename to create one. Today we error cleanly, in
1118 * the future we should consider calling iget to build
1119 * a new struct inode for this entry. */
1123 mlog(0, "We found an inode for name %.*s but VFS "
1124 "didn't give us one.\n", new_dentry->d_name.len,
1125 new_dentry->d_name.name);
1129 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1132 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1133 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1134 (unsigned long long)newfe_blkno,
1135 OCFS2_I(new_inode)->ip_flags);
1139 status = ocfs2_meta_lock(new_inode, &newfe_bh, 1);
1141 if (status != -ENOENT)
1145 new_child_locked = 1;
1147 status = ocfs2_remote_dentry_delete(new_dentry);
1153 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1155 mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
1156 "newfebh=%p bhblocknr=%llu\n", new_de,
1157 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
1158 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1160 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
1161 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
1171 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1173 status = ocfs2_check_dir_for_entry(new_dir,
1174 new_dentry->d_name.name,
1175 new_dentry->d_name.len);
1179 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1180 new_dentry->d_name.name,
1181 new_dentry->d_name.len,
1189 handle = ocfs2_start_trans(osb, OCFS2_RENAME_CREDITS);
1190 if (IS_ERR(handle)) {
1191 status = PTR_ERR(handle);
1198 if (S_ISDIR(new_inode->i_mode)) {
1199 if (!ocfs2_empty_dir(new_inode) ||
1200 new_inode->i_nlink != 2) {
1201 status = -ENOTEMPTY;
1205 status = ocfs2_journal_access(handle, new_inode, newfe_bh,
1206 OCFS2_JOURNAL_ACCESS_WRITE);
1212 if (S_ISDIR(new_inode->i_mode) ||
1213 (newfe->i_links_count == cpu_to_le16(1))){
1214 status = ocfs2_orphan_add(osb, handle, new_inode,
1216 orphan_entry_bh, orphan_dir);
1223 /* change the dirent to point to the correct inode */
1224 status = ocfs2_update_entry(new_dir, handle, new_de_bh,
1230 new_dir->i_version++;
1232 if (S_ISDIR(new_inode->i_mode))
1233 newfe->i_links_count = 0;
1235 le16_add_cpu(&newfe->i_links_count, -1);
1237 status = ocfs2_journal_dirty(handle, newfe_bh);
1243 /* if the name was not found in new_dir, add it now */
1244 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1245 OCFS2_I(old_inode)->ip_blkno,
1246 new_dir_bh, insert_entry_bh);
1249 old_inode->i_ctime = CURRENT_TIME;
1250 mark_inode_dirty(old_inode);
1252 status = ocfs2_journal_access(handle, old_inode, old_inode_bh,
1253 OCFS2_JOURNAL_ACCESS_WRITE);
1255 old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
1257 old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
1258 old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
1260 status = ocfs2_journal_dirty(handle, old_inode_bh);
1267 * Now that the name has been added to new_dir, remove the old name.
1269 * We don't keep any directory entry context around until now
1270 * because the insert might have changed the type of directory
1271 * we're dealing with.
1273 old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
1274 old_dentry->d_name.len,
1281 status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
1288 new_inode->i_nlink--;
1289 new_inode->i_ctime = CURRENT_TIME;
1291 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1292 if (old_inode_de_bh) {
1293 status = ocfs2_update_entry(old_inode, handle, old_inode_de_bh,
1294 old_inode_dot_dot_de, new_dir);
1297 new_inode->i_nlink--;
1300 mark_inode_dirty(new_dir);
1303 mark_inode_dirty(old_dir);
1304 ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1306 mark_inode_dirty(new_inode);
1307 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1310 if (old_dir != new_dir) {
1311 /* Keep the same times on both directories.*/
1312 new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
1315 * This will also pick up the i_nlink change from the
1318 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
1321 if (old_dir_nlink != old_dir->i_nlink) {
1323 mlog(ML_ERROR, "need to change nlink for old dir "
1324 "%llu from %d to %d but bh is NULL!\n",
1325 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1326 (int)old_dir_nlink, old_dir->i_nlink);
1328 struct ocfs2_dinode *fe;
1329 status = ocfs2_journal_access(handle, old_dir,
1331 OCFS2_JOURNAL_ACCESS_WRITE);
1332 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1333 fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
1334 status = ocfs2_journal_dirty(handle, old_dir_bh);
1338 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
1342 ocfs2_rename_unlock(osb);
1345 ocfs2_commit_trans(osb, handle);
1348 ocfs2_double_unlock(old_dir, new_dir);
1350 if (old_child_locked)
1351 ocfs2_meta_unlock(old_inode, 1);
1353 if (new_child_locked)
1354 ocfs2_meta_unlock(new_inode, 1);
1357 /* This was locked for us in ocfs2_prepare_orphan_dir() */
1358 ocfs2_meta_unlock(orphan_dir, 1);
1359 mutex_unlock(&orphan_dir->i_mutex);
1364 sync_mapping_buffers(old_inode->i_mapping);
1371 brelse(old_inode_bh);
1380 if (old_inode_de_bh)
1381 brelse(old_inode_de_bh);
1382 if (orphan_entry_bh)
1383 brelse(orphan_entry_bh);
1384 if (insert_entry_bh)
1385 brelse(insert_entry_bh);
1393 * we expect i_size = strlen(symname). Copy symname into the file
1394 * data, including the null terminator.
1396 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1398 struct inode *inode,
1399 const char *symname)
1401 struct buffer_head **bhs = NULL;
1403 struct super_block *sb = osb->sb;
1404 u64 p_blkno, p_blocks;
1405 int virtual, blocks, status, i, bytes_left;
1407 bytes_left = i_size_read(inode) + 1;
1408 /* we can't trust i_blocks because we're actually going to
1409 * write i_size + 1 bytes. */
1410 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1412 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1413 (unsigned long long)inode->i_blocks,
1414 i_size_read(inode), blocks);
1416 /* Sanity check -- make sure we're going to fit. */
1418 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1424 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1431 status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
1438 /* links can never be larger than one cluster so we know this
1439 * is all going to be contiguous, but do a sanity check
1441 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1448 while(bytes_left > 0) {
1449 c = &symname[virtual * sb->s_blocksize];
1451 bhs[virtual] = sb_getblk(sb, p_blkno);
1452 if (!bhs[virtual]) {
1457 ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
1459 status = ocfs2_journal_access(handle, inode, bhs[virtual],
1460 OCFS2_JOURNAL_ACCESS_CREATE);
1466 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1468 memcpy(bhs[virtual]->b_data, c,
1469 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1472 status = ocfs2_journal_dirty(handle, bhs[virtual]);
1480 bytes_left -= sb->s_blocksize;
1487 for(i = 0; i < blocks; i++)
1497 static int ocfs2_symlink(struct inode *dir,
1498 struct dentry *dentry,
1499 const char *symname)
1501 int status, l, credits;
1503 struct ocfs2_super *osb = NULL;
1504 struct inode *inode = NULL;
1505 struct super_block *sb;
1506 struct buffer_head *new_fe_bh = NULL;
1507 struct buffer_head *de_bh = NULL;
1508 struct buffer_head *parent_fe_bh = NULL;
1509 struct ocfs2_dinode *fe = NULL;
1510 struct ocfs2_dinode *dirfe;
1511 handle_t *handle = NULL;
1512 struct ocfs2_alloc_context *inode_ac = NULL;
1513 struct ocfs2_alloc_context *data_ac = NULL;
1515 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1516 dentry, symname, dentry->d_name.len, dentry->d_name.name);
1521 l = strlen(symname) + 1;
1523 credits = ocfs2_calc_symlink_credits(sb);
1525 /* lock the parent directory */
1526 status = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
1528 if (status != -ENOENT)
1533 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1534 if (!dirfe->i_links_count) {
1535 /* can't make a file in a deleted directory. */
1540 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1541 dentry->d_name.len);
1545 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1546 dentry->d_name.name,
1547 dentry->d_name.len, &de_bh);
1553 status = ocfs2_reserve_new_inode(osb, &inode_ac);
1555 if (status != -ENOSPC)
1560 /* don't reserve bitmap space for fast symlinks. */
1561 if (l > ocfs2_fast_symlink_chars(sb)) {
1562 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
1564 if (status != -ENOSPC)
1570 handle = ocfs2_start_trans(osb, credits);
1571 if (IS_ERR(handle)) {
1572 status = PTR_ERR(handle);
1578 status = ocfs2_mknod_locked(osb, dir, dentry,
1579 S_IFLNK | S_IRWXUGO, 0,
1580 &new_fe_bh, parent_fe_bh, handle,
1587 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1590 if (l > ocfs2_fast_symlink_chars(sb)) {
1593 inode->i_op = &ocfs2_symlink_inode_operations;
1594 status = ocfs2_do_extend_allocation(osb, inode, &offset, 1, 0,
1596 handle, data_ac, NULL,
1599 if (status != -ENOSPC && status != -EINTR) {
1601 "Failed to extend file to %llu\n",
1602 (unsigned long long)newsize);
1608 i_size_write(inode, newsize);
1609 inode->i_blocks = ocfs2_inode_sector_count(inode);
1611 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1612 memcpy((char *) fe->id2.i_symlink, symname, l);
1613 i_size_write(inode, newsize);
1614 inode->i_blocks = 0;
1617 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1623 if (!ocfs2_inode_is_fast_symlink(inode)) {
1624 status = ocfs2_create_symlink_data(osb, handle, inode,
1632 status = ocfs2_add_entry(handle, dentry, inode,
1633 le64_to_cpu(fe->i_blkno), parent_fe_bh,
1640 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
1646 insert_inode_hash(inode);
1647 dentry->d_op = &ocfs2_dentry_ops;
1648 d_instantiate(dentry, inode);
1651 ocfs2_commit_trans(osb, handle);
1653 ocfs2_meta_unlock(dir, 1);
1658 brelse(parent_fe_bh);
1662 ocfs2_free_alloc_context(inode_ac);
1664 ocfs2_free_alloc_context(data_ac);
1665 if ((status < 0) && inode)
1673 static int ocfs2_blkno_stringify(u64 blkno, char *name)
1675 int status, namelen;
1679 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
1689 if (namelen != OCFS2_ORPHAN_NAMELEN) {
1695 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
1704 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
1705 struct inode **ret_orphan_dir,
1706 struct inode *inode,
1708 struct buffer_head **de_bh)
1710 struct inode *orphan_dir_inode;
1711 struct buffer_head *orphan_dir_bh = NULL;
1714 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1720 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1721 ORPHAN_DIR_SYSTEM_INODE,
1723 if (!orphan_dir_inode) {
1729 mutex_lock(&orphan_dir_inode->i_mutex);
1731 status = ocfs2_meta_lock(orphan_dir_inode, &orphan_dir_bh, 1);
1737 status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
1738 orphan_dir_bh, name,
1739 OCFS2_ORPHAN_NAMELEN, de_bh);
1741 ocfs2_meta_unlock(orphan_dir_inode, 1);
1747 *ret_orphan_dir = orphan_dir_inode;
1751 mutex_unlock(&orphan_dir_inode->i_mutex);
1752 iput(orphan_dir_inode);
1756 brelse(orphan_dir_bh);
1762 static int ocfs2_orphan_add(struct ocfs2_super *osb,
1764 struct inode *inode,
1765 struct ocfs2_dinode *fe,
1767 struct buffer_head *de_bh,
1768 struct inode *orphan_dir_inode)
1770 struct buffer_head *orphan_dir_bh = NULL;
1772 struct ocfs2_dinode *orphan_fe;
1774 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
1776 status = ocfs2_read_block(osb,
1777 OCFS2_I(orphan_dir_inode)->ip_blkno,
1778 &orphan_dir_bh, OCFS2_BH_CACHED,
1785 status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
1786 OCFS2_JOURNAL_ACCESS_WRITE);
1792 /* we're a cluster, and nlink can change on disk from
1793 * underneath us... */
1794 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
1795 if (S_ISDIR(inode->i_mode))
1796 le16_add_cpu(&orphan_fe->i_links_count, 1);
1797 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
1799 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
1805 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
1806 OCFS2_ORPHAN_NAMELEN, inode,
1807 OCFS2_I(inode)->ip_blkno,
1808 orphan_dir_bh, de_bh);
1814 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
1816 /* Record which orphan dir our inode now resides
1817 * in. delete_inode will use this to determine which orphan
1819 fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
1821 mlog(0, "Inode %llu orphaned in slot %d\n",
1822 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
1826 brelse(orphan_dir_bh);
1832 /* unlike orphan_add, we expect the orphan dir to already be locked here. */
1833 int ocfs2_orphan_del(struct ocfs2_super *osb,
1835 struct inode *orphan_dir_inode,
1836 struct inode *inode,
1837 struct buffer_head *orphan_dir_bh)
1839 char name[OCFS2_ORPHAN_NAMELEN + 1];
1840 struct ocfs2_dinode *orphan_fe;
1842 struct buffer_head *target_de_bh = NULL;
1843 struct ocfs2_dir_entry *target_de = NULL;
1847 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1853 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
1854 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
1855 OCFS2_ORPHAN_NAMELEN);
1857 /* find it's spot in the orphan directory */
1858 target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
1859 orphan_dir_inode, &target_de);
1860 if (!target_de_bh) {
1866 /* remove it from the orphan directory */
1867 status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
1874 status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
1875 OCFS2_JOURNAL_ACCESS_WRITE);
1881 /* do the i_nlink dance! :) */
1882 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
1883 if (S_ISDIR(inode->i_mode))
1884 le16_add_cpu(&orphan_fe->i_links_count, -1);
1885 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
1887 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
1895 brelse(target_de_bh);
1901 const struct inode_operations ocfs2_dir_iops = {
1902 .create = ocfs2_create,
1903 .lookup = ocfs2_lookup,
1905 .unlink = ocfs2_unlink,
1906 .rmdir = ocfs2_unlink,
1907 .symlink = ocfs2_symlink,
1908 .mkdir = ocfs2_mkdir,
1909 .mknod = ocfs2_mknod,
1910 .rename = ocfs2_rename,
1911 .setattr = ocfs2_setattr,
1912 .getattr = ocfs2_getattr,
1913 .permission = ocfs2_permission,