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 #define NAMEI_RA_CHUNKS 2
68 #define NAMEI_RA_BLOCKS 4
69 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
70 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
72 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
74 const char *name, int namelen,
76 struct ocfs2_dir_entry **res_dir);
78 static int ocfs2_delete_entry(handle_t *handle,
80 struct ocfs2_dir_entry *de_del,
81 struct buffer_head *bh);
83 static int __ocfs2_add_entry(handle_t *handle,
85 const char *name, int namelen,
86 struct inode *inode, u64 blkno,
87 struct buffer_head *parent_fe_bh,
88 struct buffer_head *insert_bh);
90 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
92 struct dentry *dentry, int mode,
94 struct buffer_head **new_fe_bh,
95 struct buffer_head *parent_fe_bh,
97 struct inode **ret_inode,
98 struct ocfs2_alloc_context *inode_ac);
100 static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
102 struct inode *parent,
104 struct buffer_head *fe_bh,
105 struct ocfs2_alloc_context *data_ac);
107 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
108 struct inode **ret_orphan_dir,
111 struct buffer_head **de_bh);
113 static int ocfs2_orphan_add(struct ocfs2_super *osb,
116 struct ocfs2_dinode *fe,
118 struct buffer_head *de_bh,
119 struct inode *orphan_dir_inode);
121 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
124 const char *symname);
126 static inline int ocfs2_add_entry(handle_t *handle,
127 struct dentry *dentry,
128 struct inode *inode, u64 blkno,
129 struct buffer_head *parent_fe_bh,
130 struct buffer_head *insert_bh)
132 return __ocfs2_add_entry(handle, dentry->d_parent->d_inode,
133 dentry->d_name.name, dentry->d_name.len,
134 inode, blkno, parent_fe_bh, insert_bh);
137 /* An orphan dir name is an 8 byte value, printed as a hex string */
138 #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
140 static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
141 struct nameidata *nd)
145 struct buffer_head *dirent_bh = NULL;
146 struct inode *inode = NULL;
148 struct ocfs2_dir_entry *dirent;
149 struct ocfs2_inode_info *oi;
151 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
152 dentry->d_name.len, dentry->d_name.name);
154 if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
155 ret = ERR_PTR(-ENAMETOOLONG);
159 mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
160 dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
162 status = ocfs2_meta_lock(dir, NULL, 0);
164 if (status != -ENOENT)
166 ret = ERR_PTR(status);
170 status = ocfs2_find_files_on_disk(dentry->d_name.name,
171 dentry->d_name.len, &blkno,
172 dir, &dirent_bh, &dirent);
176 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0);
178 mlog(ML_ERROR, "Unable to create inode %llu\n",
179 (unsigned long long)blkno);
180 ret = ERR_PTR(-EACCES);
185 /* Clear any orphaned state... If we were able to look up the
186 * inode from a directory, it certainly can't be orphaned. We
187 * might have the bad state from a node which intended to
188 * orphan this inode but crashed before it could commit the
190 spin_lock(&oi->ip_lock);
191 oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
192 oi->ip_orphaned_slot = OCFS2_INVALID_SLOT;
193 spin_unlock(&oi->ip_lock);
196 dentry->d_op = &ocfs2_dentry_ops;
197 ret = d_splice_alias(inode, dentry);
201 * If d_splice_alias() finds a DCACHE_DISCONNECTED
202 * dentry, it will d_move() it on top of ourse. The
203 * return value will indicate this however, so in
204 * those cases, we switch them around for the locking
207 * NOTE: This dentry already has ->d_op set from
208 * ocfs2_get_parent() and ocfs2_get_dentry()
213 status = ocfs2_dentry_attach_lock(dentry, inode,
214 OCFS2_I(dir)->ip_blkno);
217 ret = ERR_PTR(status);
223 /* Don't drop the cluster lock until *after* the d_add --
224 * unlink on another node will message us to remove that
225 * dentry under this lock so otherwise we can race this with
226 * the vote thread and have a stale dentry. */
227 ocfs2_meta_unlock(dir, 0);
238 static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
240 struct inode *parent,
242 struct buffer_head *fe_bh,
243 struct ocfs2_alloc_context *data_ac)
246 struct buffer_head *new_bh = NULL;
247 struct ocfs2_dir_entry *de = NULL;
251 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
252 data_ac, NULL, &new_bh);
258 ocfs2_set_new_buffer_uptodate(inode, new_bh);
260 status = ocfs2_journal_access(handle, inode, new_bh,
261 OCFS2_JOURNAL_ACCESS_CREATE);
266 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
268 de = (struct ocfs2_dir_entry *) new_bh->b_data;
269 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
272 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
273 strcpy(de->name, ".");
274 ocfs2_set_de_type(de, S_IFDIR);
275 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
276 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
277 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
278 OCFS2_DIR_REC_LEN(1));
280 strcpy(de->name, "..");
281 ocfs2_set_de_type(de, S_IFDIR);
283 status = ocfs2_journal_dirty(handle, new_bh);
289 i_size_write(inode, inode->i_sb->s_blocksize);
291 inode->i_blocks = ocfs2_align_bytes_to_sectors(inode->i_sb->s_blocksize);
292 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
307 static int ocfs2_mknod(struct inode *dir,
308 struct dentry *dentry,
313 struct buffer_head *parent_fe_bh = NULL;
314 handle_t *handle = NULL;
315 struct ocfs2_super *osb;
316 struct ocfs2_dinode *dirfe;
317 struct buffer_head *new_fe_bh = NULL;
318 struct buffer_head *de_bh = NULL;
319 struct inode *inode = NULL;
320 struct ocfs2_alloc_context *inode_ac = NULL;
321 struct ocfs2_alloc_context *data_ac = NULL;
323 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
324 (unsigned long)dev, dentry->d_name.len,
325 dentry->d_name.name);
327 /* get our super block */
328 osb = OCFS2_SB(dir->i_sb);
330 status = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
332 if (status != -ENOENT)
337 if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
342 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
343 if (!dirfe->i_links_count) {
344 /* can't make a file in a deleted directory. */
349 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
354 /* get a spot inside the dir. */
355 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
357 dentry->d_name.len, &de_bh);
363 /* reserve an inode spot */
364 status = ocfs2_reserve_new_inode(osb, &inode_ac);
366 if (status != -ENOSPC)
371 /* are we making a directory? If so, reserve a cluster for his
374 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
376 if (status != -ENOSPC)
382 handle = ocfs2_start_trans(osb, OCFS2_MKNOD_CREDITS);
383 if (IS_ERR(handle)) {
384 status = PTR_ERR(handle);
390 /* do the real work now. */
391 status = ocfs2_mknod_locked(osb, dir, dentry, mode, dev,
392 &new_fe_bh, parent_fe_bh, handle,
400 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
407 status = ocfs2_journal_access(handle, dir, parent_fe_bh,
408 OCFS2_JOURNAL_ACCESS_WRITE);
413 le16_add_cpu(&dirfe->i_links_count, 1);
414 status = ocfs2_journal_dirty(handle, parent_fe_bh);
422 status = ocfs2_add_entry(handle, dentry, inode,
423 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
430 status = ocfs2_dentry_attach_lock(dentry, inode,
431 OCFS2_I(dir)->ip_blkno);
437 insert_inode_hash(inode);
438 dentry->d_op = &ocfs2_dentry_ops;
439 d_instantiate(dentry, inode);
443 ocfs2_commit_trans(osb, handle);
445 ocfs2_meta_unlock(dir, 1);
447 if (status == -ENOSPC)
448 mlog(0, "Disk is full\n");
457 brelse(parent_fe_bh);
459 if ((status < 0) && inode)
463 ocfs2_free_alloc_context(inode_ac);
466 ocfs2_free_alloc_context(data_ac);
473 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
475 struct dentry *dentry, int mode,
477 struct buffer_head **new_fe_bh,
478 struct buffer_head *parent_fe_bh,
480 struct inode **ret_inode,
481 struct ocfs2_alloc_context *inode_ac)
484 struct ocfs2_dinode *fe = NULL;
485 struct ocfs2_extent_list *fel;
488 struct inode *inode = NULL;
490 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
491 (unsigned long)dev, dentry->d_name.len,
492 dentry->d_name.name);
497 status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
504 inode = new_inode(dir->i_sb);
506 status = PTR_ERR(inode);
507 mlog(ML_ERROR, "new_inode failed!\n");
511 /* populate as many fields early on as possible - many of
512 * these are used by the support functions here and in
514 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
515 OCFS2_I(inode)->ip_blkno = fe_blkno;
520 inode->i_mode = mode;
521 spin_lock(&osb->osb_lock);
522 inode->i_generation = osb->s_next_generation++;
523 spin_unlock(&osb->osb_lock);
525 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
531 ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
533 status = ocfs2_journal_access(handle, inode, *new_fe_bh,
534 OCFS2_JOURNAL_ACCESS_CREATE);
540 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
541 memset(fe, 0, osb->sb->s_blocksize);
543 fe->i_generation = cpu_to_le32(inode->i_generation);
544 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
545 fe->i_blkno = cpu_to_le64(fe_blkno);
546 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
547 fe->i_suballoc_slot = cpu_to_le16(osb->slot_num);
548 fe->i_uid = cpu_to_le32(current->fsuid);
549 if (dir->i_mode & S_ISGID) {
550 fe->i_gid = cpu_to_le32(dir->i_gid);
554 fe->i_gid = cpu_to_le32(current->fsgid);
555 fe->i_mode = cpu_to_le16(mode);
556 if (S_ISCHR(mode) || S_ISBLK(mode))
557 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
559 fe->i_links_count = cpu_to_le16(inode->i_nlink);
561 fe->i_last_eb_blk = 0;
562 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
563 le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
564 fe->i_atime = fe->i_ctime = fe->i_mtime =
565 cpu_to_le64(CURRENT_TIME.tv_sec);
566 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
567 cpu_to_le32(CURRENT_TIME.tv_nsec);
570 fel = &fe->id2.i_list;
571 fel->l_tree_depth = 0;
572 fel->l_next_free_rec = 0;
573 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
575 status = ocfs2_journal_dirty(handle, *new_fe_bh);
581 if (ocfs2_populate_inode(inode, fe, 1) < 0) {
582 mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
583 "i_blkno=%llu, i_ino=%lu\n",
584 (unsigned long long) (*new_fe_bh)->b_blocknr,
585 (unsigned long long)fe->i_blkno, inode->i_ino);
589 ocfs2_inode_set_new(osb, inode);
590 if (!ocfs2_mount_local(osb)) {
591 status = ocfs2_create_new_inode_locks(inode);
596 status = 0; /* error in ocfs2_create_new_inode_locks is not
614 static int ocfs2_mkdir(struct inode *dir,
615 struct dentry *dentry,
620 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
621 dentry->d_name.len, dentry->d_name.name);
622 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
628 static int ocfs2_create(struct inode *dir,
629 struct dentry *dentry,
631 struct nameidata *nd)
635 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
636 dentry->d_name.len, dentry->d_name.name);
637 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
643 static int ocfs2_link(struct dentry *old_dentry,
645 struct dentry *dentry)
648 struct inode *inode = old_dentry->d_inode;
650 struct buffer_head *fe_bh = NULL;
651 struct buffer_head *parent_fe_bh = NULL;
652 struct buffer_head *de_bh = NULL;
653 struct ocfs2_dinode *fe = NULL;
654 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
656 mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
657 old_dentry->d_name.len, old_dentry->d_name.name,
658 dentry->d_name.len, dentry->d_name.name);
660 if (S_ISDIR(inode->i_mode))
663 err = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
675 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
680 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
682 dentry->d_name.len, &de_bh);
688 err = ocfs2_meta_lock(inode, &fe_bh, 1);
695 fe = (struct ocfs2_dinode *) fe_bh->b_data;
696 if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
698 goto out_unlock_inode;
701 handle = ocfs2_start_trans(osb, OCFS2_LINK_CREDITS);
702 if (IS_ERR(handle)) {
703 err = PTR_ERR(handle);
706 goto out_unlock_inode;
709 err = ocfs2_journal_access(handle, inode, fe_bh,
710 OCFS2_JOURNAL_ACCESS_WRITE);
717 inode->i_ctime = CURRENT_TIME;
718 fe->i_links_count = cpu_to_le16(inode->i_nlink);
719 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
720 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
722 err = ocfs2_journal_dirty(handle, fe_bh);
724 le16_add_cpu(&fe->i_links_count, -1);
730 err = ocfs2_add_entry(handle, dentry, inode,
731 OCFS2_I(inode)->ip_blkno,
732 parent_fe_bh, de_bh);
734 le16_add_cpu(&fe->i_links_count, -1);
740 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
746 atomic_inc(&inode->i_count);
747 dentry->d_op = &ocfs2_dentry_ops;
748 d_instantiate(dentry, inode);
751 ocfs2_commit_trans(osb, handle);
753 ocfs2_meta_unlock(inode, 1);
756 ocfs2_meta_unlock(dir, 1);
763 brelse(parent_fe_bh);
771 * Takes and drops an exclusive lock on the given dentry. This will
772 * force other nodes to drop it.
774 static int ocfs2_remote_dentry_delete(struct dentry *dentry)
778 ret = ocfs2_dentry_lock(dentry, 1);
782 ocfs2_dentry_unlock(dentry, 1);
787 static inline int inode_is_unlinkable(struct inode *inode)
789 if (S_ISDIR(inode->i_mode)) {
790 if (inode->i_nlink == 2)
795 if (inode->i_nlink == 1)
800 static int ocfs2_unlink(struct inode *dir,
801 struct dentry *dentry)
804 int child_locked = 0;
805 struct inode *inode = dentry->d_inode;
806 struct inode *orphan_dir = NULL;
807 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
809 struct ocfs2_dinode *fe = NULL;
810 struct buffer_head *fe_bh = NULL;
811 struct buffer_head *parent_node_bh = NULL;
812 handle_t *handle = NULL;
813 struct ocfs2_dir_entry *dirent = NULL;
814 struct buffer_head *dirent_bh = NULL;
815 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
816 struct buffer_head *orphan_entry_bh = NULL;
818 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
819 dentry->d_name.len, dentry->d_name.name);
821 BUG_ON(dentry->d_parent->d_inode != dir);
823 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
825 if (inode == osb->root_inode) {
826 mlog(0, "Cannot delete the root directory\n");
830 status = ocfs2_meta_lock(dir, &parent_node_bh, 1);
832 if (status != -ENOENT)
837 status = ocfs2_find_files_on_disk(dentry->d_name.name,
838 dentry->d_name.len, &blkno,
839 dir, &dirent_bh, &dirent);
841 if (status != -ENOENT)
846 if (OCFS2_I(inode)->ip_blkno != blkno) {
849 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
850 (unsigned long long)OCFS2_I(inode)->ip_blkno,
851 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
855 status = ocfs2_meta_lock(inode, &fe_bh, 1);
857 if (status != -ENOENT)
863 if (S_ISDIR(inode->i_mode)) {
864 if (!ocfs2_empty_dir(inode)) {
867 } else if (inode->i_nlink != 2) {
873 status = ocfs2_remote_dentry_delete(dentry);
875 /* This vote should succeed under all normal
881 if (inode_is_unlinkable(inode)) {
882 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
891 handle = ocfs2_start_trans(osb, OCFS2_UNLINK_CREDITS);
892 if (IS_ERR(handle)) {
893 status = PTR_ERR(handle);
899 status = ocfs2_journal_access(handle, inode, fe_bh,
900 OCFS2_JOURNAL_ACCESS_WRITE);
906 fe = (struct ocfs2_dinode *) fe_bh->b_data;
908 if (inode_is_unlinkable(inode)) {
909 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
910 orphan_entry_bh, orphan_dir);
917 /* delete the name from the parent dir */
918 status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
924 if (S_ISDIR(inode->i_mode))
927 fe->i_links_count = cpu_to_le16(inode->i_nlink);
929 status = ocfs2_journal_dirty(handle, fe_bh);
935 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
936 if (S_ISDIR(inode->i_mode))
939 status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
942 if (S_ISDIR(inode->i_mode))
948 ocfs2_commit_trans(osb, handle);
951 ocfs2_meta_unlock(inode, 1);
953 ocfs2_meta_unlock(dir, 1);
956 /* This was locked for us in ocfs2_prepare_orphan_dir() */
957 ocfs2_meta_unlock(orphan_dir, 1);
958 mutex_unlock(&orphan_dir->i_mutex);
969 brelse(parent_node_bh);
972 brelse(orphan_entry_bh);
980 * The only place this should be used is rename!
981 * if they have the same id, then the 1st one is the only one locked.
983 static int ocfs2_double_lock(struct ocfs2_super *osb,
984 struct buffer_head **bh1,
985 struct inode *inode1,
986 struct buffer_head **bh2,
987 struct inode *inode2)
990 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
991 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
992 struct buffer_head **tmpbh;
993 struct inode *tmpinode;
995 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
996 (unsigned long long)oi1->ip_blkno,
997 (unsigned long long)oi2->ip_blkno);
1004 /* we always want to lock the one with the lower lockid first. */
1005 if (oi1->ip_blkno != oi2->ip_blkno) {
1006 if (oi1->ip_blkno < oi2->ip_blkno) {
1007 /* switch id1 and id2 around */
1008 mlog(0, "switching them around...\n");
1018 status = ocfs2_meta_lock(inode2, bh2, 1);
1020 if (status != -ENOENT)
1027 status = ocfs2_meta_lock(inode1, bh1, 1);
1030 * An error return must mean that no cluster locks
1031 * were held on function exit.
1033 if (oi1->ip_blkno != oi2->ip_blkno)
1034 ocfs2_meta_unlock(inode2, 1);
1036 if (status != -ENOENT)
1045 static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
1047 ocfs2_meta_unlock(inode1, 1);
1049 if (inode1 != inode2)
1050 ocfs2_meta_unlock(inode2, 1);
1053 #define PARENT_INO(buffer) \
1054 ((struct ocfs2_dir_entry *) \
1056 le16_to_cpu(((struct ocfs2_dir_entry *)buffer)->rec_len)))->inode
1058 static int ocfs2_rename(struct inode *old_dir,
1059 struct dentry *old_dentry,
1060 struct inode *new_dir,
1061 struct dentry *new_dentry)
1063 int status = 0, rename_lock = 0, parents_locked = 0;
1064 int old_child_locked = 0, new_child_locked = 0;
1065 struct inode *old_inode = old_dentry->d_inode;
1066 struct inode *new_inode = new_dentry->d_inode;
1067 struct inode *orphan_dir = NULL;
1068 struct ocfs2_dinode *newfe = NULL;
1069 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
1070 struct buffer_head *orphan_entry_bh = NULL;
1071 struct buffer_head *newfe_bh = NULL;
1072 struct buffer_head *old_inode_bh = NULL;
1073 struct buffer_head *insert_entry_bh = NULL;
1074 struct ocfs2_super *osb = NULL;
1076 handle_t *handle = NULL;
1077 struct buffer_head *old_dir_bh = NULL;
1078 struct buffer_head *new_dir_bh = NULL;
1079 struct ocfs2_dir_entry *old_de = NULL, *new_de = NULL; // dirent for old_dentry
1081 struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
1082 struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
1083 // this is the 1st dirent bh
1084 nlink_t old_dir_nlink = old_dir->i_nlink;
1086 /* At some point it might be nice to break this function up a
1089 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
1090 old_dir, old_dentry, new_dir, new_dentry,
1091 old_dentry->d_name.len, old_dentry->d_name.name,
1092 new_dentry->d_name.len, new_dentry->d_name.name);
1094 osb = OCFS2_SB(old_dir->i_sb);
1097 if (!igrab(new_inode))
1101 /* Assume a directory heirarchy thusly:
1104 * a,b,c, and d are all directories.
1106 * from cwd of 'a' on both nodes:
1110 * And that's why, just like the VFS, we need a file system
1112 if (old_dentry != new_dentry) {
1113 status = ocfs2_rename_lock(osb);
1121 /* if old and new are the same, this'll just do one lock. */
1122 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1123 &new_dir_bh, new_dir);
1130 /* make sure both dirs have bhs
1131 * get an extra ref on old_dir_bh if old==new */
1134 new_dir_bh = old_dir_bh;
1137 mlog(ML_ERROR, "no old_dir_bh!\n");
1144 * Aside from allowing a meta data update, the locking here
1145 * also ensures that the vote thread on other nodes won't have
1146 * to concurrently downconvert the inode and the dentry locks.
1148 status = ocfs2_meta_lock(old_inode, &old_inode_bh, 1);
1150 if (status != -ENOENT)
1154 old_child_locked = 1;
1156 status = ocfs2_remote_dentry_delete(old_dentry);
1162 if (S_ISDIR(old_inode->i_mode)) {
1164 old_inode_de_bh = ocfs2_bread(old_inode, 0, &status, 0);
1165 if (!old_inode_de_bh)
1169 if (le64_to_cpu(PARENT_INO(old_inode_de_bh->b_data)) !=
1170 OCFS2_I(old_dir)->ip_blkno)
1173 if (!new_inode && new_dir!=old_dir &&
1174 new_dir->i_nlink >= OCFS2_LINK_MAX)
1179 old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
1180 old_dentry->d_name.len,
1186 * Check for inode number is _not_ due to possible IO errors.
1187 * We might rmdir the source, keep it as pwd of some process
1188 * and merrily kill the link to whatever was created under the
1189 * same name. Goodbye sticky bit ;-<
1191 if (le64_to_cpu(old_de->inode) != OCFS2_I(old_inode)->ip_blkno)
1194 /* check if the target already exists (in which case we need
1196 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1197 new_dentry->d_name.len,
1198 &newfe_blkno, new_dir, &new_de_bh,
1200 /* The only error we allow here is -ENOENT because the new
1201 * file not existing is perfectly valid. */
1202 if ((status < 0) && (status != -ENOENT)) {
1203 /* If we cannot find the file specified we should just */
1204 /* return the error... */
1209 if (!new_de && new_inode)
1210 mlog(ML_ERROR, "inode %lu does not exist in it's parent "
1211 "directory!", new_inode->i_ino);
1213 /* In case we need to overwrite an existing file, we blow it
1216 /* VFS didn't think there existed an inode here, but
1217 * someone else in the cluster must have raced our
1218 * rename to create one. Today we error cleanly, in
1219 * the future we should consider calling iget to build
1220 * a new struct inode for this entry. */
1224 mlog(0, "We found an inode for name %.*s but VFS "
1225 "didn't give us one.\n", new_dentry->d_name.len,
1226 new_dentry->d_name.name);
1230 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1233 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1234 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1235 (unsigned long long)newfe_blkno,
1236 OCFS2_I(new_inode)->ip_flags);
1240 status = ocfs2_meta_lock(new_inode, &newfe_bh, 1);
1242 if (status != -ENOENT)
1246 new_child_locked = 1;
1248 status = ocfs2_remote_dentry_delete(new_dentry);
1254 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1256 mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
1257 "newfebh=%p bhblocknr=%llu\n", new_de,
1258 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
1259 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1261 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
1262 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
1272 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1274 status = ocfs2_check_dir_for_entry(new_dir,
1275 new_dentry->d_name.name,
1276 new_dentry->d_name.len);
1280 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1281 new_dentry->d_name.name,
1282 new_dentry->d_name.len,
1290 handle = ocfs2_start_trans(osb, OCFS2_RENAME_CREDITS);
1291 if (IS_ERR(handle)) {
1292 status = PTR_ERR(handle);
1299 if (S_ISDIR(new_inode->i_mode)) {
1300 if (!ocfs2_empty_dir(new_inode) ||
1301 new_inode->i_nlink != 2) {
1302 status = -ENOTEMPTY;
1306 status = ocfs2_journal_access(handle, new_inode, newfe_bh,
1307 OCFS2_JOURNAL_ACCESS_WRITE);
1313 if (S_ISDIR(new_inode->i_mode) ||
1314 (newfe->i_links_count == cpu_to_le16(1))){
1315 status = ocfs2_orphan_add(osb, handle, new_inode,
1317 orphan_entry_bh, orphan_dir);
1324 /* change the dirent to point to the correct inode */
1325 status = ocfs2_journal_access(handle, new_dir, new_de_bh,
1326 OCFS2_JOURNAL_ACCESS_WRITE);
1331 new_de->inode = cpu_to_le64(OCFS2_I(old_inode)->ip_blkno);
1332 new_de->file_type = old_de->file_type;
1333 new_dir->i_version++;
1334 status = ocfs2_journal_dirty(handle, new_de_bh);
1340 if (S_ISDIR(new_inode->i_mode))
1341 newfe->i_links_count = 0;
1343 le16_add_cpu(&newfe->i_links_count, -1);
1345 status = ocfs2_journal_dirty(handle, newfe_bh);
1351 /* if the name was not found in new_dir, add it now */
1352 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1353 OCFS2_I(old_inode)->ip_blkno,
1354 new_dir_bh, insert_entry_bh);
1357 old_inode->i_ctime = CURRENT_TIME;
1358 mark_inode_dirty(old_inode);
1359 ocfs2_mark_inode_dirty(handle, old_inode, old_inode_bh);
1361 /* now that the name has been added to new_dir, remove the old name */
1362 status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
1369 new_inode->i_nlink--;
1370 new_inode->i_ctime = CURRENT_TIME;
1372 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1373 if (old_inode_de_bh) {
1374 status = ocfs2_journal_access(handle, old_inode,
1376 OCFS2_JOURNAL_ACCESS_WRITE);
1377 PARENT_INO(old_inode_de_bh->b_data) =
1378 cpu_to_le64(OCFS2_I(new_dir)->ip_blkno);
1379 status = ocfs2_journal_dirty(handle, old_inode_de_bh);
1382 new_inode->i_nlink--;
1385 mark_inode_dirty(new_dir);
1388 mark_inode_dirty(old_dir);
1389 ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1391 mark_inode_dirty(new_inode);
1392 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1395 if (old_dir != new_dir) {
1396 /* Keep the same times on both directories.*/
1397 new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
1400 * This will also pick up the i_nlink change from the
1403 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
1406 if (old_dir_nlink != old_dir->i_nlink) {
1408 mlog(ML_ERROR, "need to change nlink for old dir "
1409 "%llu from %d to %d but bh is NULL!\n",
1410 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1411 (int)old_dir_nlink, old_dir->i_nlink);
1413 struct ocfs2_dinode *fe;
1414 status = ocfs2_journal_access(handle, old_dir,
1416 OCFS2_JOURNAL_ACCESS_WRITE);
1417 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1418 fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
1419 status = ocfs2_journal_dirty(handle, old_dir_bh);
1423 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
1427 ocfs2_rename_unlock(osb);
1430 ocfs2_commit_trans(osb, handle);
1433 ocfs2_double_unlock(old_dir, new_dir);
1435 if (old_child_locked)
1436 ocfs2_meta_unlock(old_inode, 1);
1438 if (new_child_locked)
1439 ocfs2_meta_unlock(new_inode, 1);
1442 /* This was locked for us in ocfs2_prepare_orphan_dir() */
1443 ocfs2_meta_unlock(orphan_dir, 1);
1444 mutex_unlock(&orphan_dir->i_mutex);
1449 sync_mapping_buffers(old_inode->i_mapping);
1456 brelse(old_inode_bh);
1465 if (old_inode_de_bh)
1466 brelse(old_inode_de_bh);
1467 if (orphan_entry_bh)
1468 brelse(orphan_entry_bh);
1469 if (insert_entry_bh)
1470 brelse(insert_entry_bh);
1478 * we expect i_size = strlen(symname). Copy symname into the file
1479 * data, including the null terminator.
1481 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1483 struct inode *inode,
1484 const char *symname)
1486 struct buffer_head **bhs = NULL;
1488 struct super_block *sb = osb->sb;
1491 int virtual, blocks, status, i, bytes_left;
1493 bytes_left = i_size_read(inode) + 1;
1494 /* we can't trust i_blocks because we're actually going to
1495 * write i_size + 1 bytes. */
1496 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1498 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1499 (unsigned long long)inode->i_blocks,
1500 i_size_read(inode), blocks);
1502 /* Sanity check -- make sure we're going to fit. */
1504 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1510 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1517 status = ocfs2_extent_map_get_blocks(inode, 0, 1, &p_blkno,
1524 /* links can never be larger than one cluster so we know this
1525 * is all going to be contiguous, but do a sanity check
1527 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1534 while(bytes_left > 0) {
1535 c = &symname[virtual * sb->s_blocksize];
1537 bhs[virtual] = sb_getblk(sb, p_blkno);
1538 if (!bhs[virtual]) {
1543 ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
1545 status = ocfs2_journal_access(handle, inode, bhs[virtual],
1546 OCFS2_JOURNAL_ACCESS_CREATE);
1552 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1554 memcpy(bhs[virtual]->b_data, c,
1555 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1558 status = ocfs2_journal_dirty(handle, bhs[virtual]);
1566 bytes_left -= sb->s_blocksize;
1573 for(i = 0; i < blocks; i++)
1583 static int ocfs2_symlink(struct inode *dir,
1584 struct dentry *dentry,
1585 const char *symname)
1587 int status, l, credits;
1589 struct ocfs2_super *osb = NULL;
1590 struct inode *inode = NULL;
1591 struct super_block *sb;
1592 struct buffer_head *new_fe_bh = NULL;
1593 struct buffer_head *de_bh = NULL;
1594 struct buffer_head *parent_fe_bh = NULL;
1595 struct ocfs2_dinode *fe = NULL;
1596 struct ocfs2_dinode *dirfe;
1597 handle_t *handle = NULL;
1598 struct ocfs2_alloc_context *inode_ac = NULL;
1599 struct ocfs2_alloc_context *data_ac = NULL;
1601 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1602 dentry, symname, dentry->d_name.len, dentry->d_name.name);
1607 l = strlen(symname) + 1;
1609 credits = ocfs2_calc_symlink_credits(sb);
1611 /* lock the parent directory */
1612 status = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
1614 if (status != -ENOENT)
1619 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1620 if (!dirfe->i_links_count) {
1621 /* can't make a file in a deleted directory. */
1626 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1627 dentry->d_name.len);
1631 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1632 dentry->d_name.name,
1633 dentry->d_name.len, &de_bh);
1639 status = ocfs2_reserve_new_inode(osb, &inode_ac);
1641 if (status != -ENOSPC)
1646 /* don't reserve bitmap space for fast symlinks. */
1647 if (l > ocfs2_fast_symlink_chars(sb)) {
1648 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
1650 if (status != -ENOSPC)
1656 handle = ocfs2_start_trans(osb, credits);
1657 if (IS_ERR(handle)) {
1658 status = PTR_ERR(handle);
1664 status = ocfs2_mknod_locked(osb, dir, dentry,
1665 S_IFLNK | S_IRWXUGO, 0,
1666 &new_fe_bh, parent_fe_bh, handle,
1673 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1676 if (l > ocfs2_fast_symlink_chars(sb)) {
1677 inode->i_op = &ocfs2_symlink_inode_operations;
1678 status = ocfs2_do_extend_allocation(osb, inode, 1, new_fe_bh,
1679 handle, data_ac, NULL,
1682 if (status != -ENOSPC && status != -EINTR) {
1684 "Failed to extend file to %llu\n",
1685 (unsigned long long)newsize);
1691 i_size_write(inode, newsize);
1692 inode->i_blocks = ocfs2_align_bytes_to_sectors(newsize);
1694 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1695 memcpy((char *) fe->id2.i_symlink, symname, l);
1696 i_size_write(inode, newsize);
1697 inode->i_blocks = 0;
1700 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1706 if (!ocfs2_inode_is_fast_symlink(inode)) {
1707 status = ocfs2_create_symlink_data(osb, handle, inode,
1715 status = ocfs2_add_entry(handle, dentry, inode,
1716 le64_to_cpu(fe->i_blkno), parent_fe_bh,
1723 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
1729 insert_inode_hash(inode);
1730 dentry->d_op = &ocfs2_dentry_ops;
1731 d_instantiate(dentry, inode);
1734 ocfs2_commit_trans(osb, handle);
1736 ocfs2_meta_unlock(dir, 1);
1741 brelse(parent_fe_bh);
1745 ocfs2_free_alloc_context(inode_ac);
1747 ocfs2_free_alloc_context(data_ac);
1748 if ((status < 0) && inode)
1756 int ocfs2_check_dir_entry(struct inode * dir,
1757 struct ocfs2_dir_entry * de,
1758 struct buffer_head * bh,
1759 unsigned long offset)
1761 const char *error_msg = NULL;
1762 const int rlen = le16_to_cpu(de->rec_len);
1764 if (rlen < OCFS2_DIR_REC_LEN(1))
1765 error_msg = "rec_len is smaller than minimal";
1766 else if (rlen % 4 != 0)
1767 error_msg = "rec_len % 4 != 0";
1768 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
1769 error_msg = "rec_len is too small for name_len";
1770 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
1771 error_msg = "directory entry across blocks";
1773 if (error_msg != NULL)
1774 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
1775 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
1776 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
1777 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
1779 return error_msg == NULL ? 1 : 0;
1782 /* we don't always have a dentry for what we want to add, so people
1783 * like orphan dir can call this instead.
1785 * If you pass me insert_bh, I'll skip the search of the other dir
1786 * blocks and put the record in there.
1788 static int __ocfs2_add_entry(handle_t *handle,
1790 const char *name, int namelen,
1791 struct inode *inode, u64 blkno,
1792 struct buffer_head *parent_fe_bh,
1793 struct buffer_head *insert_bh)
1795 unsigned long offset;
1796 unsigned short rec_len;
1797 struct ocfs2_dir_entry *de, *de1;
1798 struct super_block *sb;
1808 rec_len = OCFS2_DIR_REC_LEN(namelen);
1810 de = (struct ocfs2_dir_entry *) insert_bh->b_data;
1812 BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
1813 /* These checks should've already been passed by the
1814 * prepare function, but I guess we can leave them
1816 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
1820 if (ocfs2_match(namelen, name, de)) {
1824 if (((le64_to_cpu(de->inode) == 0) &&
1825 (le16_to_cpu(de->rec_len) >= rec_len)) ||
1826 (le16_to_cpu(de->rec_len) >=
1827 (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
1828 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1829 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1835 status = ocfs2_journal_access(handle, dir, insert_bh,
1836 OCFS2_JOURNAL_ACCESS_WRITE);
1837 /* By now the buffer is marked for journaling */
1838 offset += le16_to_cpu(de->rec_len);
1839 if (le64_to_cpu(de->inode)) {
1840 de1 = (struct ocfs2_dir_entry *)((char *) de +
1841 OCFS2_DIR_REC_LEN(de->name_len));
1843 cpu_to_le16(le16_to_cpu(de->rec_len) -
1844 OCFS2_DIR_REC_LEN(de->name_len));
1845 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1848 de->file_type = OCFS2_FT_UNKNOWN;
1850 de->inode = cpu_to_le64(blkno);
1851 ocfs2_set_de_type(de, inode->i_mode);
1854 de->name_len = namelen;
1855 memcpy(de->name, name, namelen);
1858 status = ocfs2_journal_dirty(handle, insert_bh);
1862 offset += le16_to_cpu(de->rec_len);
1863 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
1866 /* when you think about it, the assert above should prevent us
1867 * from ever getting here. */
1877 * ocfs2_delete_entry deletes a directory entry by merging it with the
1880 static int ocfs2_delete_entry(handle_t *handle,
1882 struct ocfs2_dir_entry *de_del,
1883 struct buffer_head *bh)
1885 struct ocfs2_dir_entry *de, *pde;
1886 int i, status = -ENOENT;
1888 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
1892 de = (struct ocfs2_dir_entry *) bh->b_data;
1893 while (i < bh->b_size) {
1894 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
1900 status = ocfs2_journal_access(handle, dir, bh,
1901 OCFS2_JOURNAL_ACCESS_WRITE);
1909 cpu_to_le16(le16_to_cpu(pde->rec_len) +
1910 le16_to_cpu(de->rec_len));
1914 status = ocfs2_journal_dirty(handle, bh);
1917 i += le16_to_cpu(de->rec_len);
1919 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
1927 * Returns 0 if not found, -1 on failure, and 1 on success
1929 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
1931 const char *name, int namelen,
1932 unsigned long offset,
1933 struct ocfs2_dir_entry **res_dir)
1935 struct ocfs2_dir_entry *de;
1936 char *dlimit, *de_buf;
1942 de_buf = bh->b_data;
1943 dlimit = de_buf + dir->i_sb->s_blocksize;
1945 while (de_buf < dlimit) {
1946 /* this code is executed quadratically often */
1947 /* do minimal checking `by hand' */
1949 de = (struct ocfs2_dir_entry *) de_buf;
1951 if (de_buf + namelen <= dlimit &&
1952 ocfs2_match(namelen, name, de)) {
1953 /* found a match - just to be sure, do a full check */
1954 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1963 /* prevent looping on a bad block */
1964 de_len = le16_to_cpu(de->rec_len);
1979 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
1981 struct ocfs2_dir_entry **res_dir)
1983 struct super_block *sb;
1984 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1985 struct buffer_head *bh, *ret = NULL;
1986 unsigned long start, block, b;
1987 int ra_max = 0; /* Number of bh's in the readahead
1989 int ra_ptr = 0; /* Current index into readahead
1992 int nblocks, i, err;
1999 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
2000 start = OCFS2_I(dir)->ip_dir_start_lookup;
2001 if (start >= nblocks)
2008 * We deal with the read-ahead logic here.
2010 if (ra_ptr >= ra_max) {
2011 /* Refill the readahead buffer */
2014 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
2016 * Terminate if we reach the end of the
2017 * directory and must wrap, or if our
2018 * search has finished at this block.
2020 if (b >= nblocks || (num && block == start)) {
2021 bh_use[ra_max] = NULL;
2026 bh = ocfs2_bread(dir, b++, &err, 1);
2027 bh_use[ra_max] = bh;
2030 if ((bh = bh_use[ra_ptr++]) == NULL)
2033 if (!buffer_uptodate(bh)) {
2034 /* read error, skip block & hope for the best */
2035 ocfs2_error(dir->i_sb, "reading directory %llu, "
2037 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2042 i = ocfs2_search_dirblock(bh, dir, name, namelen,
2043 block << sb->s_blocksize_bits,
2046 OCFS2_I(dir)->ip_dir_start_lookup = block;
2048 goto cleanup_and_exit;
2052 goto cleanup_and_exit;
2055 if (++block >= nblocks)
2057 } while (block != start);
2060 * If the directory has grown while we were searching, then
2061 * search the last part of the directory before giving up.
2064 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
2065 if (block < nblocks) {
2071 /* Clean up the read-ahead blocks */
2072 for (; ra_ptr < ra_max; ra_ptr++)
2073 brelse(bh_use[ra_ptr]);
2079 static int ocfs2_blkno_stringify(u64 blkno, char *name)
2081 int status, namelen;
2085 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
2095 if (namelen != OCFS2_ORPHAN_NAMELEN) {
2101 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
2110 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
2111 struct inode **ret_orphan_dir,
2112 struct inode *inode,
2114 struct buffer_head **de_bh)
2116 struct inode *orphan_dir_inode;
2117 struct buffer_head *orphan_dir_bh = NULL;
2120 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2126 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2127 ORPHAN_DIR_SYSTEM_INODE,
2129 if (!orphan_dir_inode) {
2135 mutex_lock(&orphan_dir_inode->i_mutex);
2137 status = ocfs2_meta_lock(orphan_dir_inode, &orphan_dir_bh, 1);
2143 status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
2144 orphan_dir_bh, name,
2145 OCFS2_ORPHAN_NAMELEN, de_bh);
2147 ocfs2_meta_unlock(orphan_dir_inode, 1);
2153 *ret_orphan_dir = orphan_dir_inode;
2157 mutex_unlock(&orphan_dir_inode->i_mutex);
2158 iput(orphan_dir_inode);
2162 brelse(orphan_dir_bh);
2168 static int ocfs2_orphan_add(struct ocfs2_super *osb,
2170 struct inode *inode,
2171 struct ocfs2_dinode *fe,
2173 struct buffer_head *de_bh,
2174 struct inode *orphan_dir_inode)
2176 struct buffer_head *orphan_dir_bh = NULL;
2178 struct ocfs2_dinode *orphan_fe;
2180 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
2182 status = ocfs2_read_block(osb,
2183 OCFS2_I(orphan_dir_inode)->ip_blkno,
2184 &orphan_dir_bh, OCFS2_BH_CACHED,
2191 status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
2192 OCFS2_JOURNAL_ACCESS_WRITE);
2198 /* we're a cluster, and nlink can change on disk from
2199 * underneath us... */
2200 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2201 if (S_ISDIR(inode->i_mode))
2202 le16_add_cpu(&orphan_fe->i_links_count, 1);
2203 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2205 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2211 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
2212 OCFS2_ORPHAN_NAMELEN, inode,
2213 OCFS2_I(inode)->ip_blkno,
2214 orphan_dir_bh, de_bh);
2220 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
2222 /* Record which orphan dir our inode now resides
2223 * in. delete_inode will use this to determine which orphan
2225 spin_lock(&OCFS2_I(inode)->ip_lock);
2226 OCFS2_I(inode)->ip_orphaned_slot = osb->slot_num;
2227 spin_unlock(&OCFS2_I(inode)->ip_lock);
2229 mlog(0, "Inode %llu orphaned in slot %d\n",
2230 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
2234 brelse(orphan_dir_bh);
2240 /* unlike orphan_add, we expect the orphan dir to already be locked here. */
2241 int ocfs2_orphan_del(struct ocfs2_super *osb,
2243 struct inode *orphan_dir_inode,
2244 struct inode *inode,
2245 struct buffer_head *orphan_dir_bh)
2247 char name[OCFS2_ORPHAN_NAMELEN + 1];
2248 struct ocfs2_dinode *orphan_fe;
2250 struct buffer_head *target_de_bh = NULL;
2251 struct ocfs2_dir_entry *target_de = NULL;
2255 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2261 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
2262 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
2263 OCFS2_ORPHAN_NAMELEN);
2265 /* find it's spot in the orphan directory */
2266 target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
2267 orphan_dir_inode, &target_de);
2268 if (!target_de_bh) {
2274 /* remove it from the orphan directory */
2275 status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
2282 status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
2283 OCFS2_JOURNAL_ACCESS_WRITE);
2289 /* do the i_nlink dance! :) */
2290 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2291 if (S_ISDIR(inode->i_mode))
2292 le16_add_cpu(&orphan_fe->i_links_count, -1);
2293 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2295 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2303 brelse(target_de_bh);
2309 struct inode_operations ocfs2_dir_iops = {
2310 .create = ocfs2_create,
2311 .lookup = ocfs2_lookup,
2313 .unlink = ocfs2_unlink,
2314 .rmdir = ocfs2_unlink,
2315 .symlink = ocfs2_symlink,
2316 .mkdir = ocfs2_mkdir,
2317 .mknod = ocfs2_mknod,
2318 .rename = ocfs2_rename,
2319 .setattr = ocfs2_setattr,
2320 .getattr = ocfs2_getattr,
2321 .permission = ocfs2_permission,