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;
233 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
234 (unsigned long)dev, dentry->d_name.len,
235 dentry->d_name.name);
237 /* get our super block */
238 osb = OCFS2_SB(dir->i_sb);
240 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
242 if (status != -ENOENT)
247 if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
252 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
253 if (!dirfe->i_links_count) {
254 /* can't make a file in a deleted directory. */
259 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
264 /* get a spot inside the dir. */
265 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
267 dentry->d_name.len, &de_bh);
273 /* reserve an inode spot */
274 status = ocfs2_reserve_new_inode(osb, &inode_ac);
276 if (status != -ENOSPC)
281 inode = ocfs2_get_init_inode(dir, mode);
288 /* Reserve a cluster if creating an extent based directory. */
289 if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) {
290 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
292 if (status != -ENOSPC)
298 handle = ocfs2_start_trans(osb, OCFS2_MKNOD_CREDITS);
299 if (IS_ERR(handle)) {
300 status = PTR_ERR(handle);
306 /* do the real work now. */
307 status = ocfs2_mknod_locked(osb, dir, inode, dentry, dev,
308 &new_fe_bh, parent_fe_bh, handle,
316 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
323 status = ocfs2_journal_access(handle, dir, parent_fe_bh,
324 OCFS2_JOURNAL_ACCESS_WRITE);
329 le16_add_cpu(&dirfe->i_links_count, 1);
330 status = ocfs2_journal_dirty(handle, parent_fe_bh);
338 status = ocfs2_add_entry(handle, dentry, inode,
339 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
346 status = ocfs2_dentry_attach_lock(dentry, inode,
347 OCFS2_I(dir)->ip_blkno);
353 insert_inode_hash(inode);
354 dentry->d_op = &ocfs2_dentry_ops;
355 d_instantiate(dentry, inode);
359 ocfs2_commit_trans(osb, handle);
361 ocfs2_inode_unlock(dir, 1);
363 if (status == -ENOSPC)
364 mlog(0, "Disk is full\n");
368 brelse(parent_fe_bh);
370 if ((status < 0) && inode) {
376 ocfs2_free_alloc_context(inode_ac);
379 ocfs2_free_alloc_context(data_ac);
386 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
389 struct dentry *dentry,
391 struct buffer_head **new_fe_bh,
392 struct buffer_head *parent_fe_bh,
394 struct ocfs2_alloc_context *inode_ac)
397 struct ocfs2_dinode *fe = NULL;
398 struct ocfs2_extent_list *fel;
402 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry,
403 inode->i_mode, (unsigned long)dev, dentry->d_name.len,
404 dentry->d_name.name);
408 status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
415 /* populate as many fields early on as possible - many of
416 * these are used by the support functions here and in
418 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
419 OCFS2_I(inode)->ip_blkno = fe_blkno;
420 spin_lock(&osb->osb_lock);
421 inode->i_generation = osb->s_next_generation++;
422 spin_unlock(&osb->osb_lock);
424 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
430 ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
432 status = ocfs2_journal_access(handle, inode, *new_fe_bh,
433 OCFS2_JOURNAL_ACCESS_CREATE);
439 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
440 memset(fe, 0, osb->sb->s_blocksize);
442 fe->i_generation = cpu_to_le32(inode->i_generation);
443 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
444 fe->i_blkno = cpu_to_le64(fe_blkno);
445 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
446 fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
447 fe->i_uid = cpu_to_le32(inode->i_uid);
448 fe->i_gid = cpu_to_le32(inode->i_gid);
449 fe->i_mode = cpu_to_le16(inode->i_mode);
450 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
451 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
452 fe->i_links_count = cpu_to_le16(inode->i_nlink);
454 fe->i_last_eb_blk = 0;
455 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
456 le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
457 fe->i_atime = fe->i_ctime = fe->i_mtime =
458 cpu_to_le64(CURRENT_TIME.tv_sec);
459 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
460 cpu_to_le32(CURRENT_TIME.tv_nsec);
464 * If supported, directories start with inline data.
466 if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
467 u16 feat = le16_to_cpu(fe->i_dyn_features);
469 fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);
471 fe->id2.i_data.id_count = cpu_to_le16(ocfs2_max_inline_data(osb->sb));
473 fel = &fe->id2.i_list;
474 fel->l_tree_depth = 0;
475 fel->l_next_free_rec = 0;
476 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
479 status = ocfs2_journal_dirty(handle, *new_fe_bh);
485 if (ocfs2_populate_inode(inode, fe, 1) < 0) {
486 mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
487 "i_blkno=%llu, i_ino=%lu\n",
488 (unsigned long long)(*new_fe_bh)->b_blocknr,
489 (unsigned long long)le64_to_cpu(fe->i_blkno),
494 ocfs2_inode_set_new(osb, inode);
495 if (!ocfs2_mount_local(osb)) {
496 status = ocfs2_create_new_inode_locks(inode);
501 status = 0; /* error in ocfs2_create_new_inode_locks is not
516 static int ocfs2_mkdir(struct inode *dir,
517 struct dentry *dentry,
522 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
523 dentry->d_name.len, dentry->d_name.name);
524 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
530 static int ocfs2_create(struct inode *dir,
531 struct dentry *dentry,
533 struct nameidata *nd)
537 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
538 dentry->d_name.len, dentry->d_name.name);
539 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
545 static int ocfs2_link(struct dentry *old_dentry,
547 struct dentry *dentry)
550 struct inode *inode = old_dentry->d_inode;
552 struct buffer_head *fe_bh = NULL;
553 struct buffer_head *parent_fe_bh = NULL;
554 struct buffer_head *de_bh = NULL;
555 struct ocfs2_dinode *fe = NULL;
556 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
558 mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
559 old_dentry->d_name.len, old_dentry->d_name.name,
560 dentry->d_name.len, dentry->d_name.name);
562 if (S_ISDIR(inode->i_mode))
565 err = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
577 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
582 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
584 dentry->d_name.len, &de_bh);
590 err = ocfs2_inode_lock(inode, &fe_bh, 1);
597 fe = (struct ocfs2_dinode *) fe_bh->b_data;
598 if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
600 goto out_unlock_inode;
603 handle = ocfs2_start_trans(osb, OCFS2_LINK_CREDITS);
604 if (IS_ERR(handle)) {
605 err = PTR_ERR(handle);
608 goto out_unlock_inode;
611 err = ocfs2_journal_access(handle, inode, fe_bh,
612 OCFS2_JOURNAL_ACCESS_WRITE);
619 inode->i_ctime = CURRENT_TIME;
620 fe->i_links_count = cpu_to_le16(inode->i_nlink);
621 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
622 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
624 err = ocfs2_journal_dirty(handle, fe_bh);
626 le16_add_cpu(&fe->i_links_count, -1);
632 err = ocfs2_add_entry(handle, dentry, inode,
633 OCFS2_I(inode)->ip_blkno,
634 parent_fe_bh, de_bh);
636 le16_add_cpu(&fe->i_links_count, -1);
642 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
648 atomic_inc(&inode->i_count);
649 dentry->d_op = &ocfs2_dentry_ops;
650 d_instantiate(dentry, inode);
653 ocfs2_commit_trans(osb, handle);
655 ocfs2_inode_unlock(inode, 1);
658 ocfs2_inode_unlock(dir, 1);
662 brelse(parent_fe_bh);
670 * Takes and drops an exclusive lock on the given dentry. This will
671 * force other nodes to drop it.
673 static int ocfs2_remote_dentry_delete(struct dentry *dentry)
677 ret = ocfs2_dentry_lock(dentry, 1);
681 ocfs2_dentry_unlock(dentry, 1);
686 static inline int inode_is_unlinkable(struct inode *inode)
688 if (S_ISDIR(inode->i_mode)) {
689 if (inode->i_nlink == 2)
694 if (inode->i_nlink == 1)
699 static int ocfs2_unlink(struct inode *dir,
700 struct dentry *dentry)
703 int child_locked = 0;
704 struct inode *inode = dentry->d_inode;
705 struct inode *orphan_dir = NULL;
706 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
708 struct ocfs2_dinode *fe = NULL;
709 struct buffer_head *fe_bh = NULL;
710 struct buffer_head *parent_node_bh = NULL;
711 handle_t *handle = NULL;
712 struct ocfs2_dir_entry *dirent = NULL;
713 struct buffer_head *dirent_bh = NULL;
714 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
715 struct buffer_head *orphan_entry_bh = NULL;
717 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
718 dentry->d_name.len, dentry->d_name.name);
720 BUG_ON(dentry->d_parent->d_inode != dir);
722 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
724 if (inode == osb->root_inode) {
725 mlog(0, "Cannot delete the root directory\n");
729 status = ocfs2_inode_lock(dir, &parent_node_bh, 1);
731 if (status != -ENOENT)
736 status = ocfs2_find_files_on_disk(dentry->d_name.name,
737 dentry->d_name.len, &blkno,
738 dir, &dirent_bh, &dirent);
740 if (status != -ENOENT)
745 if (OCFS2_I(inode)->ip_blkno != blkno) {
748 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
749 (unsigned long long)OCFS2_I(inode)->ip_blkno,
750 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
754 status = ocfs2_inode_lock(inode, &fe_bh, 1);
756 if (status != -ENOENT)
762 if (S_ISDIR(inode->i_mode)) {
763 if (!ocfs2_empty_dir(inode)) {
766 } else if (inode->i_nlink != 2) {
772 status = ocfs2_remote_dentry_delete(dentry);
774 /* This remote delete should succeed under all normal
780 if (inode_is_unlinkable(inode)) {
781 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
790 handle = ocfs2_start_trans(osb, OCFS2_UNLINK_CREDITS);
791 if (IS_ERR(handle)) {
792 status = PTR_ERR(handle);
798 status = ocfs2_journal_access(handle, inode, fe_bh,
799 OCFS2_JOURNAL_ACCESS_WRITE);
805 fe = (struct ocfs2_dinode *) fe_bh->b_data;
807 if (inode_is_unlinkable(inode)) {
808 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
809 orphan_entry_bh, orphan_dir);
816 /* delete the name from the parent dir */
817 status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
823 if (S_ISDIR(inode->i_mode))
826 fe->i_links_count = cpu_to_le16(inode->i_nlink);
828 status = ocfs2_journal_dirty(handle, fe_bh);
834 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
835 if (S_ISDIR(inode->i_mode))
838 status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
841 if (S_ISDIR(inode->i_mode))
847 ocfs2_commit_trans(osb, handle);
850 ocfs2_inode_unlock(inode, 1);
852 ocfs2_inode_unlock(dir, 1);
855 /* This was locked for us in ocfs2_prepare_orphan_dir() */
856 ocfs2_inode_unlock(orphan_dir, 1);
857 mutex_unlock(&orphan_dir->i_mutex);
863 brelse(parent_node_bh);
864 brelse(orphan_entry_bh);
872 * The only place this should be used is rename!
873 * if they have the same id, then the 1st one is the only one locked.
875 static int ocfs2_double_lock(struct ocfs2_super *osb,
876 struct buffer_head **bh1,
877 struct inode *inode1,
878 struct buffer_head **bh2,
879 struct inode *inode2)
882 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
883 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
884 struct buffer_head **tmpbh;
885 struct inode *tmpinode;
887 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
888 (unsigned long long)oi1->ip_blkno,
889 (unsigned long long)oi2->ip_blkno);
896 /* we always want to lock the one with the lower lockid first. */
897 if (oi1->ip_blkno != oi2->ip_blkno) {
898 if (oi1->ip_blkno < oi2->ip_blkno) {
899 /* switch id1 and id2 around */
900 mlog(0, "switching them around...\n");
910 status = ocfs2_inode_lock(inode2, bh2, 1);
912 if (status != -ENOENT)
919 status = ocfs2_inode_lock(inode1, bh1, 1);
922 * An error return must mean that no cluster locks
923 * were held on function exit.
925 if (oi1->ip_blkno != oi2->ip_blkno)
926 ocfs2_inode_unlock(inode2, 1);
928 if (status != -ENOENT)
937 static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
939 ocfs2_inode_unlock(inode1, 1);
941 if (inode1 != inode2)
942 ocfs2_inode_unlock(inode2, 1);
945 static int ocfs2_rename(struct inode *old_dir,
946 struct dentry *old_dentry,
947 struct inode *new_dir,
948 struct dentry *new_dentry)
950 int status = 0, rename_lock = 0, parents_locked = 0;
951 int old_child_locked = 0, new_child_locked = 0;
952 struct inode *old_inode = old_dentry->d_inode;
953 struct inode *new_inode = new_dentry->d_inode;
954 struct inode *orphan_dir = NULL;
955 struct ocfs2_dinode *newfe = NULL;
956 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
957 struct buffer_head *orphan_entry_bh = NULL;
958 struct buffer_head *newfe_bh = NULL;
959 struct buffer_head *old_inode_bh = NULL;
960 struct buffer_head *insert_entry_bh = NULL;
961 struct ocfs2_super *osb = NULL;
962 u64 newfe_blkno, old_de_ino;
963 handle_t *handle = NULL;
964 struct buffer_head *old_dir_bh = NULL;
965 struct buffer_head *new_dir_bh = NULL;
966 struct ocfs2_dir_entry *old_inode_dot_dot_de = NULL, *old_de = NULL,
968 struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
969 struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
970 // this is the 1st dirent bh
971 nlink_t old_dir_nlink = old_dir->i_nlink;
972 struct ocfs2_dinode *old_di;
974 /* At some point it might be nice to break this function up a
977 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
978 old_dir, old_dentry, new_dir, new_dentry,
979 old_dentry->d_name.len, old_dentry->d_name.name,
980 new_dentry->d_name.len, new_dentry->d_name.name);
982 osb = OCFS2_SB(old_dir->i_sb);
985 if (!igrab(new_inode))
989 /* Assume a directory hierarchy thusly:
992 * a,b,c, and d are all directories.
994 * from cwd of 'a' on both nodes:
998 * And that's why, just like the VFS, we need a file system
1000 if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) {
1001 status = ocfs2_rename_lock(osb);
1009 /* if old and new are the same, this'll just do one lock. */
1010 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1011 &new_dir_bh, new_dir);
1018 /* make sure both dirs have bhs
1019 * get an extra ref on old_dir_bh if old==new */
1022 new_dir_bh = old_dir_bh;
1025 mlog(ML_ERROR, "no old_dir_bh!\n");
1032 * Aside from allowing a meta data update, the locking here
1033 * also ensures that the downconvert thread on other nodes
1034 * won't have to concurrently downconvert the inode and the
1037 status = ocfs2_inode_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) {
1110 * Target was unlinked by another node while we were
1111 * waiting to get to ocfs2_rename(). There isn't
1112 * anything we can do here to help the situation, so
1113 * bubble up the appropriate error.
1119 /* In case we need to overwrite an existing file, we blow it
1122 /* VFS didn't think there existed an inode here, but
1123 * someone else in the cluster must have raced our
1124 * rename to create one. Today we error cleanly, in
1125 * the future we should consider calling iget to build
1126 * a new struct inode for this entry. */
1130 mlog(0, "We found an inode for name %.*s but VFS "
1131 "didn't give us one.\n", new_dentry->d_name.len,
1132 new_dentry->d_name.name);
1136 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1139 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1140 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1141 (unsigned long long)newfe_blkno,
1142 OCFS2_I(new_inode)->ip_flags);
1146 status = ocfs2_inode_lock(new_inode, &newfe_bh, 1);
1148 if (status != -ENOENT)
1152 new_child_locked = 1;
1154 status = ocfs2_remote_dentry_delete(new_dentry);
1160 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1162 mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
1163 "newfebh=%p bhblocknr=%llu\n", new_de,
1164 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
1165 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1167 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
1168 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
1178 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1180 status = ocfs2_check_dir_for_entry(new_dir,
1181 new_dentry->d_name.name,
1182 new_dentry->d_name.len);
1186 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1187 new_dentry->d_name.name,
1188 new_dentry->d_name.len,
1196 handle = ocfs2_start_trans(osb, OCFS2_RENAME_CREDITS);
1197 if (IS_ERR(handle)) {
1198 status = PTR_ERR(handle);
1205 if (S_ISDIR(new_inode->i_mode)) {
1206 if (!ocfs2_empty_dir(new_inode) ||
1207 new_inode->i_nlink != 2) {
1208 status = -ENOTEMPTY;
1212 status = ocfs2_journal_access(handle, new_inode, newfe_bh,
1213 OCFS2_JOURNAL_ACCESS_WRITE);
1219 if (S_ISDIR(new_inode->i_mode) ||
1220 (newfe->i_links_count == cpu_to_le16(1))){
1221 status = ocfs2_orphan_add(osb, handle, new_inode,
1223 orphan_entry_bh, orphan_dir);
1230 /* change the dirent to point to the correct inode */
1231 status = ocfs2_update_entry(new_dir, handle, new_de_bh,
1237 new_dir->i_version++;
1239 if (S_ISDIR(new_inode->i_mode))
1240 newfe->i_links_count = 0;
1242 le16_add_cpu(&newfe->i_links_count, -1);
1244 status = ocfs2_journal_dirty(handle, newfe_bh);
1250 /* if the name was not found in new_dir, add it now */
1251 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1252 OCFS2_I(old_inode)->ip_blkno,
1253 new_dir_bh, insert_entry_bh);
1256 old_inode->i_ctime = CURRENT_TIME;
1257 mark_inode_dirty(old_inode);
1259 status = ocfs2_journal_access(handle, old_inode, old_inode_bh,
1260 OCFS2_JOURNAL_ACCESS_WRITE);
1262 old_di = (struct ocfs2_dinode *) old_inode_bh->b_data;
1264 old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);
1265 old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);
1267 status = ocfs2_journal_dirty(handle, old_inode_bh);
1274 * Now that the name has been added to new_dir, remove the old name.
1276 * We don't keep any directory entry context around until now
1277 * because the insert might have changed the type of directory
1278 * we're dealing with.
1280 old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
1281 old_dentry->d_name.len,
1288 status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
1295 new_inode->i_nlink--;
1296 new_inode->i_ctime = CURRENT_TIME;
1298 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1299 if (old_inode_de_bh) {
1300 status = ocfs2_update_entry(old_inode, handle, old_inode_de_bh,
1301 old_inode_dot_dot_de, new_dir);
1304 new_inode->i_nlink--;
1307 mark_inode_dirty(new_dir);
1310 mark_inode_dirty(old_dir);
1311 ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1313 mark_inode_dirty(new_inode);
1314 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1317 if (old_dir != new_dir) {
1318 /* Keep the same times on both directories.*/
1319 new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
1322 * This will also pick up the i_nlink change from the
1325 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
1328 if (old_dir_nlink != old_dir->i_nlink) {
1330 mlog(ML_ERROR, "need to change nlink for old dir "
1331 "%llu from %d to %d but bh is NULL!\n",
1332 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1333 (int)old_dir_nlink, old_dir->i_nlink);
1335 struct ocfs2_dinode *fe;
1336 status = ocfs2_journal_access(handle, old_dir,
1338 OCFS2_JOURNAL_ACCESS_WRITE);
1339 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1340 fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
1341 status = ocfs2_journal_dirty(handle, old_dir_bh);
1345 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
1349 ocfs2_rename_unlock(osb);
1352 ocfs2_commit_trans(osb, handle);
1355 ocfs2_double_unlock(old_dir, new_dir);
1357 if (old_child_locked)
1358 ocfs2_inode_unlock(old_inode, 1);
1360 if (new_child_locked)
1361 ocfs2_inode_unlock(new_inode, 1);
1364 /* This was locked for us in ocfs2_prepare_orphan_dir() */
1365 ocfs2_inode_unlock(orphan_dir, 1);
1366 mutex_unlock(&orphan_dir->i_mutex);
1371 sync_mapping_buffers(old_inode->i_mapping);
1376 brelse(old_inode_bh);
1381 brelse(old_inode_de_bh);
1382 brelse(orphan_entry_bh);
1383 brelse(insert_entry_bh);
1391 * we expect i_size = strlen(symname). Copy symname into the file
1392 * data, including the null terminator.
1394 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1396 struct inode *inode,
1397 const char *symname)
1399 struct buffer_head **bhs = NULL;
1401 struct super_block *sb = osb->sb;
1402 u64 p_blkno, p_blocks;
1403 int virtual, blocks, status, i, bytes_left;
1405 bytes_left = i_size_read(inode) + 1;
1406 /* we can't trust i_blocks because we're actually going to
1407 * write i_size + 1 bytes. */
1408 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1410 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1411 (unsigned long long)inode->i_blocks,
1412 i_size_read(inode), blocks);
1414 /* Sanity check -- make sure we're going to fit. */
1416 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1422 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1429 status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
1436 /* links can never be larger than one cluster so we know this
1437 * is all going to be contiguous, but do a sanity check
1439 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1446 while(bytes_left > 0) {
1447 c = &symname[virtual * sb->s_blocksize];
1449 bhs[virtual] = sb_getblk(sb, p_blkno);
1450 if (!bhs[virtual]) {
1455 ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
1457 status = ocfs2_journal_access(handle, inode, bhs[virtual],
1458 OCFS2_JOURNAL_ACCESS_CREATE);
1464 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1466 memcpy(bhs[virtual]->b_data, c,
1467 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1470 status = ocfs2_journal_dirty(handle, bhs[virtual]);
1478 bytes_left -= sb->s_blocksize;
1485 for(i = 0; i < blocks; i++)
1494 static int ocfs2_symlink(struct inode *dir,
1495 struct dentry *dentry,
1496 const char *symname)
1498 int status, l, credits;
1500 struct ocfs2_super *osb = NULL;
1501 struct inode *inode = NULL;
1502 struct super_block *sb;
1503 struct buffer_head *new_fe_bh = NULL;
1504 struct buffer_head *de_bh = NULL;
1505 struct buffer_head *parent_fe_bh = NULL;
1506 struct ocfs2_dinode *fe = NULL;
1507 struct ocfs2_dinode *dirfe;
1508 handle_t *handle = NULL;
1509 struct ocfs2_alloc_context *inode_ac = NULL;
1510 struct ocfs2_alloc_context *data_ac = NULL;
1512 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1513 dentry, symname, dentry->d_name.len, dentry->d_name.name);
1518 l = strlen(symname) + 1;
1520 credits = ocfs2_calc_symlink_credits(sb);
1522 /* lock the parent directory */
1523 status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
1525 if (status != -ENOENT)
1530 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1531 if (!dirfe->i_links_count) {
1532 /* can't make a file in a deleted directory. */
1537 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1538 dentry->d_name.len);
1542 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1543 dentry->d_name.name,
1544 dentry->d_name.len, &de_bh);
1550 status = ocfs2_reserve_new_inode(osb, &inode_ac);
1552 if (status != -ENOSPC)
1557 inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO);
1564 /* don't reserve bitmap space for fast symlinks. */
1565 if (l > ocfs2_fast_symlink_chars(sb)) {
1566 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
1568 if (status != -ENOSPC)
1574 handle = ocfs2_start_trans(osb, credits);
1575 if (IS_ERR(handle)) {
1576 status = PTR_ERR(handle);
1582 status = ocfs2_mknod_locked(osb, dir, inode, dentry,
1583 0, &new_fe_bh, parent_fe_bh, handle,
1590 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1593 if (l > ocfs2_fast_symlink_chars(sb)) {
1596 inode->i_op = &ocfs2_symlink_inode_operations;
1597 status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0,
1599 handle, data_ac, NULL,
1602 if (status != -ENOSPC && status != -EINTR) {
1604 "Failed to extend file to %llu\n",
1605 (unsigned long long)newsize);
1611 i_size_write(inode, newsize);
1612 inode->i_blocks = ocfs2_inode_sector_count(inode);
1614 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1615 memcpy((char *) fe->id2.i_symlink, symname, l);
1616 i_size_write(inode, newsize);
1617 inode->i_blocks = 0;
1620 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1626 if (!ocfs2_inode_is_fast_symlink(inode)) {
1627 status = ocfs2_create_symlink_data(osb, handle, inode,
1635 status = ocfs2_add_entry(handle, dentry, inode,
1636 le64_to_cpu(fe->i_blkno), parent_fe_bh,
1643 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
1649 insert_inode_hash(inode);
1650 dentry->d_op = &ocfs2_dentry_ops;
1651 d_instantiate(dentry, inode);
1654 ocfs2_commit_trans(osb, handle);
1656 ocfs2_inode_unlock(dir, 1);
1659 brelse(parent_fe_bh);
1662 ocfs2_free_alloc_context(inode_ac);
1664 ocfs2_free_alloc_context(data_ac);
1665 if ((status < 0) && inode) {
1675 static int ocfs2_blkno_stringify(u64 blkno, char *name)
1677 int status, namelen;
1681 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
1691 if (namelen != OCFS2_ORPHAN_NAMELEN) {
1697 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
1706 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
1707 struct inode **ret_orphan_dir,
1708 struct inode *inode,
1710 struct buffer_head **de_bh)
1712 struct inode *orphan_dir_inode;
1713 struct buffer_head *orphan_dir_bh = NULL;
1716 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1722 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1723 ORPHAN_DIR_SYSTEM_INODE,
1725 if (!orphan_dir_inode) {
1731 mutex_lock(&orphan_dir_inode->i_mutex);
1733 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
1739 status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
1740 orphan_dir_bh, name,
1741 OCFS2_ORPHAN_NAMELEN, de_bh);
1743 ocfs2_inode_unlock(orphan_dir_inode, 1);
1749 *ret_orphan_dir = orphan_dir_inode;
1753 mutex_unlock(&orphan_dir_inode->i_mutex);
1754 iput(orphan_dir_inode);
1757 brelse(orphan_dir_bh);
1763 static int ocfs2_orphan_add(struct ocfs2_super *osb,
1765 struct inode *inode,
1766 struct ocfs2_dinode *fe,
1768 struct buffer_head *de_bh,
1769 struct inode *orphan_dir_inode)
1771 struct buffer_head *orphan_dir_bh = NULL;
1773 struct ocfs2_dinode *orphan_fe;
1775 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
1777 status = ocfs2_read_block(orphan_dir_inode,
1778 OCFS2_I(orphan_dir_inode)->ip_blkno,
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);
1825 brelse(orphan_dir_bh);
1831 /* unlike orphan_add, we expect the orphan dir to already be locked here. */
1832 int ocfs2_orphan_del(struct ocfs2_super *osb,
1834 struct inode *orphan_dir_inode,
1835 struct inode *inode,
1836 struct buffer_head *orphan_dir_bh)
1838 char name[OCFS2_ORPHAN_NAMELEN + 1];
1839 struct ocfs2_dinode *orphan_fe;
1841 struct buffer_head *target_de_bh = NULL;
1842 struct ocfs2_dir_entry *target_de = NULL;
1846 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
1852 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
1853 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
1854 OCFS2_ORPHAN_NAMELEN);
1856 /* find it's spot in the orphan directory */
1857 target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
1858 orphan_dir_inode, &target_de);
1859 if (!target_de_bh) {
1865 /* remove it from the orphan directory */
1866 status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
1873 status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
1874 OCFS2_JOURNAL_ACCESS_WRITE);
1880 /* do the i_nlink dance! :) */
1881 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
1882 if (S_ISDIR(inode->i_mode))
1883 le16_add_cpu(&orphan_fe->i_links_count, -1);
1884 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
1886 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
1893 brelse(target_de_bh);
1899 const struct inode_operations ocfs2_dir_iops = {
1900 .create = ocfs2_create,
1901 .lookup = ocfs2_lookup,
1903 .unlink = ocfs2_unlink,
1904 .rmdir = ocfs2_unlink,
1905 .symlink = ocfs2_symlink,
1906 .mkdir = ocfs2_mkdir,
1907 .mknod = ocfs2_mknod,
1908 .rename = ocfs2_rename,
1909 .setattr = ocfs2_setattr,
1910 .getattr = ocfs2_getattr,
1911 .permission = ocfs2_permission,
1912 .setxattr = generic_setxattr,
1913 .getxattr = generic_getxattr,
1914 .listxattr = ocfs2_listxattr,
1915 .removexattr = generic_removexattr,