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(struct ocfs2_journal_handle *handle,
80 struct ocfs2_dir_entry *de_del,
81 struct buffer_head *bh);
83 static int __ocfs2_add_entry(struct ocfs2_journal_handle *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,
96 struct ocfs2_journal_handle *handle,
97 struct inode **ret_inode,
98 struct ocfs2_alloc_context *inode_ac);
100 static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
101 struct ocfs2_journal_handle *handle,
102 struct inode *parent,
104 struct buffer_head *fe_bh,
105 struct ocfs2_alloc_context *data_ac);
107 static int ocfs2_double_lock(struct ocfs2_super *osb,
108 struct ocfs2_journal_handle *handle,
109 struct buffer_head **bh1,
110 struct inode *inode1,
111 struct buffer_head **bh2,
112 struct inode *inode2);
114 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
115 struct inode **ret_orphan_dir,
118 struct buffer_head **de_bh);
120 static int ocfs2_orphan_add(struct ocfs2_super *osb,
121 struct ocfs2_journal_handle *handle,
123 struct ocfs2_dinode *fe,
125 struct buffer_head *de_bh,
126 struct inode *orphan_dir_inode);
128 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
129 struct ocfs2_journal_handle *handle,
131 const char *symname);
133 static inline int ocfs2_add_entry(struct ocfs2_journal_handle *handle,
134 struct dentry *dentry,
135 struct inode *inode, u64 blkno,
136 struct buffer_head *parent_fe_bh,
137 struct buffer_head *insert_bh)
139 return __ocfs2_add_entry(handle, dentry->d_parent->d_inode,
140 dentry->d_name.name, dentry->d_name.len,
141 inode, blkno, parent_fe_bh, insert_bh);
144 /* An orphan dir name is an 8 byte value, printed as a hex string */
145 #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
147 static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
148 struct nameidata *nd)
152 struct buffer_head *dirent_bh = NULL;
153 struct inode *inode = NULL;
155 struct ocfs2_dir_entry *dirent;
156 struct ocfs2_inode_info *oi;
158 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
159 dentry->d_name.len, dentry->d_name.name);
161 if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
162 ret = ERR_PTR(-ENAMETOOLONG);
166 mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
167 dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
169 status = ocfs2_meta_lock(dir, NULL, NULL, 0);
171 if (status != -ENOENT)
173 ret = ERR_PTR(status);
177 status = ocfs2_find_files_on_disk(dentry->d_name.name,
178 dentry->d_name.len, &blkno,
179 dir, &dirent_bh, &dirent);
183 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0);
185 mlog(ML_ERROR, "Unable to create inode %llu\n",
186 (unsigned long long)blkno);
187 ret = ERR_PTR(-EACCES);
192 /* Clear any orphaned state... If we were able to look up the
193 * inode from a directory, it certainly can't be orphaned. We
194 * might have the bad state from a node which intended to
195 * orphan this inode but crashed before it could commit the
197 spin_lock(&oi->ip_lock);
198 oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
199 oi->ip_orphaned_slot = OCFS2_INVALID_SLOT;
200 spin_unlock(&oi->ip_lock);
203 dentry->d_op = &ocfs2_dentry_ops;
204 ret = d_splice_alias(inode, dentry);
208 * If d_splice_alias() finds a DCACHE_DISCONNECTED
209 * dentry, it will d_move() it on top of ourse. The
210 * return value will indicate this however, so in
211 * those cases, we switch them around for the locking
214 * NOTE: This dentry already has ->d_op set from
215 * ocfs2_get_parent() and ocfs2_get_dentry()
220 status = ocfs2_dentry_attach_lock(dentry, inode,
221 OCFS2_I(dir)->ip_blkno);
224 ret = ERR_PTR(status);
230 /* Don't drop the cluster lock until *after* the d_add --
231 * unlink on another node will message us to remove that
232 * dentry under this lock so otherwise we can race this with
233 * the vote thread and have a stale dentry. */
234 ocfs2_meta_unlock(dir, 0);
245 static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
246 struct ocfs2_journal_handle *handle,
247 struct inode *parent,
249 struct buffer_head *fe_bh,
250 struct ocfs2_alloc_context *data_ac)
253 struct buffer_head *new_bh = NULL;
254 struct ocfs2_dir_entry *de = NULL;
258 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
259 data_ac, NULL, &new_bh);
265 ocfs2_set_new_buffer_uptodate(inode, new_bh);
267 status = ocfs2_journal_access(handle, inode, new_bh,
268 OCFS2_JOURNAL_ACCESS_CREATE);
273 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
275 de = (struct ocfs2_dir_entry *) new_bh->b_data;
276 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
279 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
280 strcpy(de->name, ".");
281 ocfs2_set_de_type(de, S_IFDIR);
282 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
283 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
284 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
285 OCFS2_DIR_REC_LEN(1));
287 strcpy(de->name, "..");
288 ocfs2_set_de_type(de, S_IFDIR);
290 status = ocfs2_journal_dirty(handle, new_bh);
296 i_size_write(inode, inode->i_sb->s_blocksize);
298 inode->i_blocks = ocfs2_align_bytes_to_sectors(inode->i_sb->s_blocksize);
299 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
314 static int ocfs2_mknod(struct inode *dir,
315 struct dentry *dentry,
320 struct buffer_head *parent_fe_bh = NULL;
321 struct ocfs2_journal_handle *handle = NULL;
322 struct ocfs2_super *osb;
323 struct ocfs2_dinode *dirfe;
324 struct buffer_head *new_fe_bh = NULL;
325 struct buffer_head *de_bh = NULL;
326 struct inode *inode = NULL;
327 struct ocfs2_alloc_context *inode_ac = NULL;
328 struct ocfs2_alloc_context *data_ac = NULL;
330 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
331 (unsigned long)dev, dentry->d_name.len,
332 dentry->d_name.name);
334 /* get our super block */
335 osb = OCFS2_SB(dir->i_sb);
337 status = ocfs2_meta_lock(dir, NULL, &parent_fe_bh, 1);
339 if (status != -ENOENT)
344 handle = ocfs2_alloc_handle(osb);
345 if (handle == NULL) {
351 if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
356 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
357 if (!dirfe->i_links_count) {
358 /* can't make a file in a deleted directory. */
363 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
368 /* get a spot inside the dir. */
369 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
371 dentry->d_name.len, &de_bh);
377 /* reserve an inode spot */
378 status = ocfs2_reserve_new_inode(osb, handle, &inode_ac);
380 if (status != -ENOSPC)
385 /* are we making a directory? If so, reserve a cluster for his
388 status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
390 if (status != -ENOSPC)
396 handle = ocfs2_start_trans(osb, handle, OCFS2_MKNOD_CREDITS);
397 if (IS_ERR(handle)) {
398 status = PTR_ERR(handle);
404 /* do the real work now. */
405 status = ocfs2_mknod_locked(osb, dir, dentry, mode, dev,
406 &new_fe_bh, parent_fe_bh, handle,
414 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
421 status = ocfs2_journal_access(handle, dir, parent_fe_bh,
422 OCFS2_JOURNAL_ACCESS_WRITE);
427 le16_add_cpu(&dirfe->i_links_count, 1);
428 status = ocfs2_journal_dirty(handle, parent_fe_bh);
436 status = ocfs2_add_entry(handle, dentry, inode,
437 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
444 status = ocfs2_dentry_attach_lock(dentry, inode,
445 OCFS2_I(dir)->ip_blkno);
451 insert_inode_hash(inode);
452 dentry->d_op = &ocfs2_dentry_ops;
453 d_instantiate(dentry, inode);
457 ocfs2_commit_trans(handle);
459 ocfs2_meta_unlock(dir, 1);
461 if (status == -ENOSPC)
462 mlog(0, "Disk is full\n");
471 brelse(parent_fe_bh);
473 if ((status < 0) && inode)
477 ocfs2_free_alloc_context(inode_ac);
480 ocfs2_free_alloc_context(data_ac);
487 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
489 struct dentry *dentry, int mode,
491 struct buffer_head **new_fe_bh,
492 struct buffer_head *parent_fe_bh,
493 struct ocfs2_journal_handle *handle,
494 struct inode **ret_inode,
495 struct ocfs2_alloc_context *inode_ac)
498 struct ocfs2_dinode *fe = NULL;
499 struct ocfs2_extent_list *fel;
502 struct inode *inode = NULL;
504 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
505 (unsigned long)dev, dentry->d_name.len,
506 dentry->d_name.name);
511 status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
518 inode = new_inode(dir->i_sb);
520 status = PTR_ERR(inode);
521 mlog(ML_ERROR, "new_inode failed!\n");
525 /* populate as many fields early on as possible - many of
526 * these are used by the support functions here and in
528 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
529 OCFS2_I(inode)->ip_blkno = fe_blkno;
534 inode->i_mode = mode;
535 spin_lock(&osb->osb_lock);
536 inode->i_generation = osb->s_next_generation++;
537 spin_unlock(&osb->osb_lock);
539 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
545 ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
547 status = ocfs2_journal_access(handle, inode, *new_fe_bh,
548 OCFS2_JOURNAL_ACCESS_CREATE);
554 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
555 memset(fe, 0, osb->sb->s_blocksize);
557 fe->i_generation = cpu_to_le32(inode->i_generation);
558 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
559 fe->i_blkno = cpu_to_le64(fe_blkno);
560 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
561 fe->i_suballoc_slot = cpu_to_le16(osb->slot_num);
562 fe->i_uid = cpu_to_le32(current->fsuid);
563 if (dir->i_mode & S_ISGID) {
564 fe->i_gid = cpu_to_le32(dir->i_gid);
568 fe->i_gid = cpu_to_le32(current->fsgid);
569 fe->i_mode = cpu_to_le16(mode);
570 if (S_ISCHR(mode) || S_ISBLK(mode))
571 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
573 fe->i_links_count = cpu_to_le16(inode->i_nlink);
575 fe->i_last_eb_blk = 0;
576 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
577 le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
578 fe->i_atime = fe->i_ctime = fe->i_mtime =
579 cpu_to_le64(CURRENT_TIME.tv_sec);
580 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
581 cpu_to_le32(CURRENT_TIME.tv_nsec);
584 fel = &fe->id2.i_list;
585 fel->l_tree_depth = 0;
586 fel->l_next_free_rec = 0;
587 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
589 status = ocfs2_journal_dirty(handle, *new_fe_bh);
595 if (ocfs2_populate_inode(inode, fe, 1) < 0) {
596 mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
597 "i_blkno=%llu, i_ino=%lu\n",
598 (unsigned long long) (*new_fe_bh)->b_blocknr,
599 (unsigned long long)fe->i_blkno, inode->i_ino);
603 ocfs2_inode_set_new(osb, inode);
604 status = ocfs2_create_new_inode_locks(inode);
608 status = 0; /* error in ocfs2_create_new_inode_locks is not
626 static int ocfs2_mkdir(struct inode *dir,
627 struct dentry *dentry,
632 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
633 dentry->d_name.len, dentry->d_name.name);
634 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
640 static int ocfs2_create(struct inode *dir,
641 struct dentry *dentry,
643 struct nameidata *nd)
647 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
648 dentry->d_name.len, dentry->d_name.name);
649 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
655 static int ocfs2_link(struct dentry *old_dentry,
657 struct dentry *dentry)
659 struct ocfs2_journal_handle *handle;
660 struct inode *inode = old_dentry->d_inode;
662 struct buffer_head *fe_bh = NULL;
663 struct buffer_head *parent_fe_bh = NULL;
664 struct buffer_head *de_bh = NULL;
665 struct ocfs2_dinode *fe = NULL;
666 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
668 mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
669 old_dentry->d_name.len, old_dentry->d_name.name,
670 dentry->d_name.len, dentry->d_name.name);
672 if (S_ISDIR(inode->i_mode))
675 err = ocfs2_meta_lock(dir, NULL, &parent_fe_bh, 1);
687 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
692 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
694 dentry->d_name.len, &de_bh);
700 err = ocfs2_meta_lock(inode, NULL, &fe_bh, 1);
707 fe = (struct ocfs2_dinode *) fe_bh->b_data;
708 if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
710 goto out_unlock_inode;
713 handle = ocfs2_start_trans(osb, NULL, OCFS2_LINK_CREDITS);
714 if (IS_ERR(handle)) {
715 err = PTR_ERR(handle);
718 goto out_unlock_inode;
721 err = ocfs2_journal_access(handle, inode, fe_bh,
722 OCFS2_JOURNAL_ACCESS_WRITE);
729 inode->i_ctime = CURRENT_TIME;
730 fe->i_links_count = cpu_to_le16(inode->i_nlink);
731 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
732 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
734 err = ocfs2_journal_dirty(handle, fe_bh);
736 le16_add_cpu(&fe->i_links_count, -1);
742 err = ocfs2_add_entry(handle, dentry, inode,
743 OCFS2_I(inode)->ip_blkno,
744 parent_fe_bh, de_bh);
746 le16_add_cpu(&fe->i_links_count, -1);
752 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
758 atomic_inc(&inode->i_count);
759 dentry->d_op = &ocfs2_dentry_ops;
760 d_instantiate(dentry, inode);
763 ocfs2_commit_trans(handle);
765 ocfs2_meta_unlock(inode, 1);
768 ocfs2_meta_unlock(dir, 1);
775 brelse(parent_fe_bh);
783 * Takes and drops an exclusive lock on the given dentry. This will
784 * force other nodes to drop it.
786 static int ocfs2_remote_dentry_delete(struct dentry *dentry)
790 ret = ocfs2_dentry_lock(dentry, 1);
794 ocfs2_dentry_unlock(dentry, 1);
799 static inline int inode_is_unlinkable(struct inode *inode)
801 if (S_ISDIR(inode->i_mode)) {
802 if (inode->i_nlink == 2)
807 if (inode->i_nlink == 1)
812 static int ocfs2_unlink(struct inode *dir,
813 struct dentry *dentry)
816 int child_locked = 0;
817 struct inode *inode = dentry->d_inode;
818 struct inode *orphan_dir = NULL;
819 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
821 struct ocfs2_dinode *fe = NULL;
822 struct buffer_head *fe_bh = NULL;
823 struct buffer_head *parent_node_bh = NULL;
824 struct ocfs2_journal_handle *handle = NULL;
825 struct ocfs2_dir_entry *dirent = NULL;
826 struct buffer_head *dirent_bh = NULL;
827 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
828 struct buffer_head *orphan_entry_bh = NULL;
830 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
831 dentry->d_name.len, dentry->d_name.name);
833 BUG_ON(dentry->d_parent->d_inode != dir);
835 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
837 if (inode == osb->root_inode) {
838 mlog(0, "Cannot delete the root directory\n");
842 status = ocfs2_meta_lock(dir, NULL, &parent_node_bh, 1);
844 if (status != -ENOENT)
849 status = ocfs2_find_files_on_disk(dentry->d_name.name,
850 dentry->d_name.len, &blkno,
851 dir, &dirent_bh, &dirent);
853 if (status != -ENOENT)
858 if (OCFS2_I(inode)->ip_blkno != blkno) {
861 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
862 (unsigned long long)OCFS2_I(inode)->ip_blkno,
863 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
867 status = ocfs2_meta_lock(inode, NULL, &fe_bh, 1);
869 if (status != -ENOENT)
875 if (S_ISDIR(inode->i_mode)) {
876 if (!ocfs2_empty_dir(inode)) {
879 } else if (inode->i_nlink != 2) {
885 status = ocfs2_remote_dentry_delete(dentry);
887 /* This vote should succeed under all normal
893 if (inode_is_unlinkable(inode)) {
894 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
903 handle = ocfs2_start_trans(osb, NULL, OCFS2_UNLINK_CREDITS);
904 if (IS_ERR(handle)) {
905 status = PTR_ERR(handle);
911 status = ocfs2_journal_access(handle, inode, fe_bh,
912 OCFS2_JOURNAL_ACCESS_WRITE);
918 fe = (struct ocfs2_dinode *) fe_bh->b_data;
920 if (inode_is_unlinkable(inode)) {
921 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
922 orphan_entry_bh, orphan_dir);
929 /* delete the name from the parent dir */
930 status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
936 if (S_ISDIR(inode->i_mode))
939 fe->i_links_count = cpu_to_le16(inode->i_nlink);
941 status = ocfs2_journal_dirty(handle, fe_bh);
947 if (S_ISDIR(inode->i_mode)) {
949 status = ocfs2_mark_inode_dirty(handle, dir,
959 ocfs2_commit_trans(handle);
962 ocfs2_meta_unlock(inode, 1);
964 ocfs2_meta_unlock(dir, 1);
967 /* This was locked for us in ocfs2_prepare_orphan_dir() */
968 ocfs2_meta_unlock(orphan_dir, 1);
969 mutex_unlock(&orphan_dir->i_mutex);
980 brelse(parent_node_bh);
983 brelse(orphan_entry_bh);
991 * The only place this should be used is rename!
992 * if they have the same id, then the 1st one is the only one locked.
994 static int ocfs2_double_lock(struct ocfs2_super *osb,
995 struct ocfs2_journal_handle *handle,
996 struct buffer_head **bh1,
997 struct inode *inode1,
998 struct buffer_head **bh2,
999 struct inode *inode2)
1002 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
1003 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
1004 struct buffer_head **tmpbh;
1005 struct inode *tmpinode;
1007 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
1008 (unsigned long long)oi1->ip_blkno,
1009 (unsigned long long)oi2->ip_blkno);
1018 /* we always want to lock the one with the lower lockid first. */
1019 if (oi1->ip_blkno != oi2->ip_blkno) {
1020 if (oi1->ip_blkno < oi2->ip_blkno) {
1021 /* switch id1 and id2 around */
1022 mlog(0, "switching them around...\n");
1032 status = ocfs2_meta_lock(inode2, handle, bh2, 1);
1034 if (status != -ENOENT)
1040 status = ocfs2_meta_lock(inode1, handle, bh1, 1);
1042 if (status != -ENOENT)
1051 #define PARENT_INO(buffer) \
1052 ((struct ocfs2_dir_entry *) \
1054 le16_to_cpu(((struct ocfs2_dir_entry *)buffer)->rec_len)))->inode
1056 static int ocfs2_rename(struct inode *old_dir,
1057 struct dentry *old_dentry,
1058 struct inode *new_dir,
1059 struct dentry *new_dentry)
1061 int status = 0, rename_lock = 0;
1062 struct inode *old_inode = old_dentry->d_inode;
1063 struct inode *new_inode = new_dentry->d_inode;
1064 struct inode *orphan_dir = NULL;
1065 struct ocfs2_dinode *newfe = NULL;
1066 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
1067 struct buffer_head *orphan_entry_bh = NULL;
1068 struct buffer_head *newfe_bh = NULL;
1069 struct buffer_head *insert_entry_bh = NULL;
1070 struct ocfs2_super *osb = NULL;
1072 struct ocfs2_journal_handle *handle = NULL;
1073 struct buffer_head *old_dir_bh = NULL;
1074 struct buffer_head *new_dir_bh = NULL;
1075 struct ocfs2_dir_entry *old_de = NULL, *new_de = NULL; // dirent for old_dentry
1077 struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
1078 struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
1079 // this is the 1st dirent bh
1080 nlink_t old_dir_nlink = old_dir->i_nlink, new_dir_nlink = new_dir->i_nlink;
1082 /* At some point it might be nice to break this function up a
1085 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
1086 old_dir, old_dentry, new_dir, new_dentry,
1087 old_dentry->d_name.len, old_dentry->d_name.name,
1088 new_dentry->d_name.len, new_dentry->d_name.name);
1090 osb = OCFS2_SB(old_dir->i_sb);
1093 if (!igrab(new_inode))
1097 /* Assume a directory heirarchy thusly:
1100 * a,b,c, and d are all directories.
1102 * from cwd of 'a' on both nodes:
1106 * And that's why, just like the VFS, we need a file system
1108 if (old_dentry != new_dentry) {
1109 status = ocfs2_rename_lock(osb);
1117 handle = ocfs2_alloc_handle(osb);
1118 if (handle == NULL) {
1124 /* if old and new are the same, this'll just do one lock. */
1125 status = ocfs2_double_lock(osb, handle,
1126 &old_dir_bh, old_dir,
1127 &new_dir_bh, new_dir);
1133 /* make sure both dirs have bhs
1134 * get an extra ref on old_dir_bh if old==new */
1137 new_dir_bh = old_dir_bh;
1140 mlog(ML_ERROR, "no old_dir_bh!\n");
1147 * Though we don't require an inode meta data update if
1148 * old_inode is not a directory, we lock anyway here to ensure
1149 * the vote thread on other nodes won't have to concurrently
1150 * downconvert the inode and the dentry locks.
1152 status = ocfs2_meta_lock(old_inode, handle, NULL, 1);
1154 if (status != -ENOENT)
1159 status = ocfs2_remote_dentry_delete(old_dentry);
1165 if (S_ISDIR(old_inode->i_mode)) {
1167 old_inode_de_bh = ocfs2_bread(old_inode, 0, &status, 0);
1168 if (!old_inode_de_bh)
1172 if (le64_to_cpu(PARENT_INO(old_inode_de_bh->b_data)) !=
1173 OCFS2_I(old_dir)->ip_blkno)
1176 if (!new_inode && new_dir!=old_dir &&
1177 new_dir->i_nlink >= OCFS2_LINK_MAX)
1182 old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
1183 old_dentry->d_name.len,
1189 * Check for inode number is _not_ due to possible IO errors.
1190 * We might rmdir the source, keep it as pwd of some process
1191 * and merrily kill the link to whatever was created under the
1192 * same name. Goodbye sticky bit ;-<
1194 if (le64_to_cpu(old_de->inode) != OCFS2_I(old_inode)->ip_blkno)
1197 /* check if the target already exists (in which case we need
1199 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1200 new_dentry->d_name.len,
1201 &newfe_blkno, new_dir, &new_de_bh,
1203 /* The only error we allow here is -ENOENT because the new
1204 * file not existing is perfectly valid. */
1205 if ((status < 0) && (status != -ENOENT)) {
1206 /* If we cannot find the file specified we should just */
1207 /* return the error... */
1212 if (!new_de && new_inode)
1213 mlog(ML_ERROR, "inode %lu does not exist in it's parent "
1214 "directory!", new_inode->i_ino);
1216 /* In case we need to overwrite an existing file, we blow it
1219 /* VFS didn't think there existed an inode here, but
1220 * someone else in the cluster must have raced our
1221 * rename to create one. Today we error cleanly, in
1222 * the future we should consider calling iget to build
1223 * a new struct inode for this entry. */
1227 mlog(0, "We found an inode for name %.*s but VFS "
1228 "didn't give us one.\n", new_dentry->d_name.len,
1229 new_dentry->d_name.name);
1233 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1236 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1237 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1238 (unsigned long long)newfe_blkno,
1239 OCFS2_I(new_inode)->ip_flags);
1243 status = ocfs2_meta_lock(new_inode, handle, &newfe_bh, 1);
1245 if (status != -ENOENT)
1250 status = ocfs2_remote_dentry_delete(new_dentry);
1256 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1258 mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
1259 "newfebh=%p bhblocknr=%llu\n", new_de,
1260 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
1261 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1263 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
1264 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
1274 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1276 status = ocfs2_check_dir_for_entry(new_dir,
1277 new_dentry->d_name.name,
1278 new_dentry->d_name.len);
1282 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1283 new_dentry->d_name.name,
1284 new_dentry->d_name.len,
1292 handle = ocfs2_start_trans(osb, handle, OCFS2_RENAME_CREDITS);
1293 if (IS_ERR(handle)) {
1294 status = PTR_ERR(handle);
1301 if (S_ISDIR(new_inode->i_mode)) {
1302 if (!ocfs2_empty_dir(new_inode) ||
1303 new_inode->i_nlink != 2) {
1304 status = -ENOTEMPTY;
1308 status = ocfs2_journal_access(handle, new_inode, newfe_bh,
1309 OCFS2_JOURNAL_ACCESS_WRITE);
1315 if (S_ISDIR(new_inode->i_mode) ||
1316 (newfe->i_links_count == cpu_to_le16(1))){
1317 status = ocfs2_orphan_add(osb, handle, new_inode,
1319 orphan_entry_bh, orphan_dir);
1326 /* change the dirent to point to the correct inode */
1327 status = ocfs2_journal_access(handle, new_dir, new_de_bh,
1328 OCFS2_JOURNAL_ACCESS_WRITE);
1333 new_de->inode = cpu_to_le64(OCFS2_I(old_inode)->ip_blkno);
1334 new_de->file_type = old_de->file_type;
1335 new_dir->i_version++;
1336 status = ocfs2_journal_dirty(handle, new_de_bh);
1342 if (S_ISDIR(new_inode->i_mode))
1343 newfe->i_links_count = 0;
1345 le16_add_cpu(&newfe->i_links_count, -1);
1347 status = ocfs2_journal_dirty(handle, newfe_bh);
1353 /* if the name was not found in new_dir, add it now */
1354 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1355 OCFS2_I(old_inode)->ip_blkno,
1356 new_dir_bh, insert_entry_bh);
1359 old_inode->i_ctime = CURRENT_TIME;
1360 mark_inode_dirty(old_inode);
1362 /* now that the name has been added to new_dir, remove the old name */
1363 status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
1370 new_inode->i_nlink--;
1371 new_inode->i_ctime = CURRENT_TIME;
1373 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1374 if (old_inode_de_bh) {
1375 status = ocfs2_journal_access(handle, old_inode,
1377 OCFS2_JOURNAL_ACCESS_WRITE);
1378 PARENT_INO(old_inode_de_bh->b_data) =
1379 cpu_to_le64(OCFS2_I(new_dir)->ip_blkno);
1380 status = ocfs2_journal_dirty(handle, old_inode_de_bh);
1383 new_inode->i_nlink--;
1386 mark_inode_dirty(new_dir);
1389 mark_inode_dirty(old_dir);
1391 mark_inode_dirty(new_inode);
1393 if (old_dir != new_dir)
1394 if (new_dir_nlink != new_dir->i_nlink) {
1396 mlog(ML_ERROR, "need to change nlink for new "
1397 "dir %llu from %d to %d but bh is NULL\n",
1398 (unsigned long long)OCFS2_I(new_dir)->ip_blkno,
1399 (int)new_dir_nlink, new_dir->i_nlink);
1401 struct ocfs2_dinode *fe;
1402 status = ocfs2_journal_access(handle,
1405 OCFS2_JOURNAL_ACCESS_WRITE);
1406 fe = (struct ocfs2_dinode *) new_dir_bh->b_data;
1407 fe->i_links_count = cpu_to_le16(new_dir->i_nlink);
1408 status = ocfs2_journal_dirty(handle, new_dir_bh);
1412 if (old_dir_nlink != old_dir->i_nlink) {
1414 mlog(ML_ERROR, "need to change nlink for old dir "
1415 "%llu from %d to %d but bh is NULL!\n",
1416 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1417 (int)old_dir_nlink, old_dir->i_nlink);
1419 struct ocfs2_dinode *fe;
1420 status = ocfs2_journal_access(handle, old_dir,
1422 OCFS2_JOURNAL_ACCESS_WRITE);
1423 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1424 fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
1425 status = ocfs2_journal_dirty(handle, old_dir_bh);
1429 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
1433 ocfs2_rename_unlock(osb);
1436 ocfs2_commit_trans(handle);
1439 /* This was locked for us in ocfs2_prepare_orphan_dir() */
1440 ocfs2_meta_unlock(orphan_dir, 1);
1441 mutex_unlock(&orphan_dir->i_mutex);
1446 sync_mapping_buffers(old_inode->i_mapping);
1460 if (old_inode_de_bh)
1461 brelse(old_inode_de_bh);
1462 if (orphan_entry_bh)
1463 brelse(orphan_entry_bh);
1464 if (insert_entry_bh)
1465 brelse(insert_entry_bh);
1473 * we expect i_size = strlen(symname). Copy symname into the file
1474 * data, including the null terminator.
1476 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1477 struct ocfs2_journal_handle *handle,
1478 struct inode *inode,
1479 const char *symname)
1481 struct buffer_head **bhs = NULL;
1483 struct super_block *sb = osb->sb;
1486 int virtual, blocks, status, i, bytes_left;
1488 bytes_left = i_size_read(inode) + 1;
1489 /* we can't trust i_blocks because we're actually going to
1490 * write i_size + 1 bytes. */
1491 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1493 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1494 (unsigned long long)inode->i_blocks,
1495 i_size_read(inode), blocks);
1497 /* Sanity check -- make sure we're going to fit. */
1499 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1505 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1512 status = ocfs2_extent_map_get_blocks(inode, 0, 1, &p_blkno,
1519 /* links can never be larger than one cluster so we know this
1520 * is all going to be contiguous, but do a sanity check
1522 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1529 while(bytes_left > 0) {
1530 c = &symname[virtual * sb->s_blocksize];
1532 bhs[virtual] = sb_getblk(sb, p_blkno);
1533 if (!bhs[virtual]) {
1538 ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
1540 status = ocfs2_journal_access(handle, inode, bhs[virtual],
1541 OCFS2_JOURNAL_ACCESS_CREATE);
1547 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1549 memcpy(bhs[virtual]->b_data, c,
1550 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1553 status = ocfs2_journal_dirty(handle, bhs[virtual]);
1561 bytes_left -= sb->s_blocksize;
1568 for(i = 0; i < blocks; i++)
1578 static int ocfs2_symlink(struct inode *dir,
1579 struct dentry *dentry,
1580 const char *symname)
1582 int status, l, credits;
1584 struct ocfs2_super *osb = NULL;
1585 struct inode *inode = NULL;
1586 struct super_block *sb;
1587 struct buffer_head *new_fe_bh = NULL;
1588 struct buffer_head *de_bh = NULL;
1589 struct buffer_head *parent_fe_bh = NULL;
1590 struct ocfs2_dinode *fe = NULL;
1591 struct ocfs2_dinode *dirfe;
1592 struct ocfs2_journal_handle *handle = NULL;
1593 struct ocfs2_alloc_context *inode_ac = NULL;
1594 struct ocfs2_alloc_context *data_ac = NULL;
1596 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1597 dentry, symname, dentry->d_name.len, dentry->d_name.name);
1602 l = strlen(symname) + 1;
1604 credits = ocfs2_calc_symlink_credits(sb);
1606 handle = ocfs2_alloc_handle(osb);
1607 if (handle == NULL) {
1613 /* lock the parent directory */
1614 status = ocfs2_meta_lock(dir, handle, &parent_fe_bh, 1);
1616 if (status != -ENOENT)
1621 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1622 if (!dirfe->i_links_count) {
1623 /* can't make a file in a deleted directory. */
1628 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1629 dentry->d_name.len);
1633 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1634 dentry->d_name.name,
1635 dentry->d_name.len, &de_bh);
1641 status = ocfs2_reserve_new_inode(osb, handle, &inode_ac);
1643 if (status != -ENOSPC)
1648 /* don't reserve bitmap space for fast symlinks. */
1649 if (l > ocfs2_fast_symlink_chars(sb)) {
1650 status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
1652 if (status != -ENOSPC)
1658 handle = ocfs2_start_trans(osb, handle, credits);
1659 if (IS_ERR(handle)) {
1660 status = PTR_ERR(handle);
1666 status = ocfs2_mknod_locked(osb, dir, dentry,
1667 S_IFLNK | S_IRWXUGO, 0,
1668 &new_fe_bh, parent_fe_bh, handle,
1675 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1678 if (l > ocfs2_fast_symlink_chars(sb)) {
1679 inode->i_op = &ocfs2_symlink_inode_operations;
1680 status = ocfs2_do_extend_allocation(osb, inode, 1, new_fe_bh,
1681 handle, data_ac, NULL,
1684 if (status != -ENOSPC && status != -EINTR) {
1686 "Failed to extend file to %llu\n",
1687 (unsigned long long)newsize);
1693 i_size_write(inode, newsize);
1694 inode->i_blocks = ocfs2_align_bytes_to_sectors(newsize);
1696 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1697 memcpy((char *) fe->id2.i_symlink, symname, l);
1698 i_size_write(inode, newsize);
1699 inode->i_blocks = 0;
1702 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1708 if (!ocfs2_inode_is_fast_symlink(inode)) {
1709 status = ocfs2_create_symlink_data(osb, handle, inode,
1717 status = ocfs2_add_entry(handle, dentry, inode,
1718 le64_to_cpu(fe->i_blkno), parent_fe_bh,
1725 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
1731 insert_inode_hash(inode);
1732 dentry->d_op = &ocfs2_dentry_ops;
1733 d_instantiate(dentry, inode);
1736 ocfs2_commit_trans(handle);
1740 brelse(parent_fe_bh);
1744 ocfs2_free_alloc_context(inode_ac);
1746 ocfs2_free_alloc_context(data_ac);
1747 if ((status < 0) && inode)
1755 int ocfs2_check_dir_entry(struct inode * dir,
1756 struct ocfs2_dir_entry * de,
1757 struct buffer_head * bh,
1758 unsigned long offset)
1760 const char *error_msg = NULL;
1761 const int rlen = le16_to_cpu(de->rec_len);
1763 if (rlen < OCFS2_DIR_REC_LEN(1))
1764 error_msg = "rec_len is smaller than minimal";
1765 else if (rlen % 4 != 0)
1766 error_msg = "rec_len % 4 != 0";
1767 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
1768 error_msg = "rec_len is too small for name_len";
1769 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
1770 error_msg = "directory entry across blocks";
1772 if (error_msg != NULL)
1773 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
1774 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
1775 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
1776 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
1778 return error_msg == NULL ? 1 : 0;
1781 /* we don't always have a dentry for what we want to add, so people
1782 * like orphan dir can call this instead.
1784 * If you pass me insert_bh, I'll skip the search of the other dir
1785 * blocks and put the record in there.
1787 static int __ocfs2_add_entry(struct ocfs2_journal_handle *handle,
1789 const char *name, int namelen,
1790 struct inode *inode, u64 blkno,
1791 struct buffer_head *parent_fe_bh,
1792 struct buffer_head *insert_bh)
1794 unsigned long offset;
1795 unsigned short rec_len;
1796 struct ocfs2_dir_entry *de, *de1;
1797 struct super_block *sb;
1807 rec_len = OCFS2_DIR_REC_LEN(namelen);
1809 de = (struct ocfs2_dir_entry *) insert_bh->b_data;
1811 BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
1812 /* These checks should've already been passed by the
1813 * prepare function, but I guess we can leave them
1815 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
1819 if (ocfs2_match(namelen, name, de)) {
1823 if (((le64_to_cpu(de->inode) == 0) &&
1824 (le16_to_cpu(de->rec_len) >= rec_len)) ||
1825 (le16_to_cpu(de->rec_len) >=
1826 (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
1827 status = ocfs2_journal_access(handle, dir, insert_bh,
1828 OCFS2_JOURNAL_ACCESS_WRITE);
1829 /* By now the buffer is marked for journaling */
1830 offset += le16_to_cpu(de->rec_len);
1831 if (le64_to_cpu(de->inode)) {
1832 de1 = (struct ocfs2_dir_entry *)((char *) de +
1833 OCFS2_DIR_REC_LEN(de->name_len));
1835 cpu_to_le16(le16_to_cpu(de->rec_len) -
1836 OCFS2_DIR_REC_LEN(de->name_len));
1837 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1840 de->file_type = OCFS2_FT_UNKNOWN;
1842 de->inode = cpu_to_le64(blkno);
1843 ocfs2_set_de_type(de, inode->i_mode);
1846 de->name_len = namelen;
1847 memcpy(de->name, name, namelen);
1849 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1851 status = ocfs2_journal_dirty(handle, insert_bh);
1855 offset += le16_to_cpu(de->rec_len);
1856 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
1859 /* when you think about it, the assert above should prevent us
1860 * from ever getting here. */
1870 * ocfs2_delete_entry deletes a directory entry by merging it with the
1873 static int ocfs2_delete_entry(struct ocfs2_journal_handle *handle,
1875 struct ocfs2_dir_entry *de_del,
1876 struct buffer_head *bh)
1878 struct ocfs2_dir_entry *de, *pde;
1879 int i, status = -ENOENT;
1881 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
1885 de = (struct ocfs2_dir_entry *) bh->b_data;
1886 while (i < bh->b_size) {
1887 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
1893 status = ocfs2_journal_access(handle, dir, bh,
1894 OCFS2_JOURNAL_ACCESS_WRITE);
1902 cpu_to_le16(le16_to_cpu(pde->rec_len) +
1903 le16_to_cpu(de->rec_len));
1907 status = ocfs2_journal_dirty(handle, bh);
1910 i += le16_to_cpu(de->rec_len);
1912 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
1920 * Returns 0 if not found, -1 on failure, and 1 on success
1922 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
1924 const char *name, int namelen,
1925 unsigned long offset,
1926 struct ocfs2_dir_entry **res_dir)
1928 struct ocfs2_dir_entry *de;
1929 char *dlimit, *de_buf;
1935 de_buf = bh->b_data;
1936 dlimit = de_buf + dir->i_sb->s_blocksize;
1938 while (de_buf < dlimit) {
1939 /* this code is executed quadratically often */
1940 /* do minimal checking `by hand' */
1942 de = (struct ocfs2_dir_entry *) de_buf;
1944 if (de_buf + namelen <= dlimit &&
1945 ocfs2_match(namelen, name, de)) {
1946 /* found a match - just to be sure, do a full check */
1947 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1956 /* prevent looping on a bad block */
1957 de_len = le16_to_cpu(de->rec_len);
1972 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
1974 struct ocfs2_dir_entry **res_dir)
1976 struct super_block *sb;
1977 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1978 struct buffer_head *bh, *ret = NULL;
1979 unsigned long start, block, b;
1980 int ra_max = 0; /* Number of bh's in the readahead
1982 int ra_ptr = 0; /* Current index into readahead
1985 int nblocks, i, err;
1992 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
1993 start = OCFS2_I(dir)->ip_dir_start_lookup;
1994 if (start >= nblocks)
2001 * We deal with the read-ahead logic here.
2003 if (ra_ptr >= ra_max) {
2004 /* Refill the readahead buffer */
2007 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
2009 * Terminate if we reach the end of the
2010 * directory and must wrap, or if our
2011 * search has finished at this block.
2013 if (b >= nblocks || (num && block == start)) {
2014 bh_use[ra_max] = NULL;
2019 bh = ocfs2_bread(dir, b++, &err, 1);
2020 bh_use[ra_max] = bh;
2023 if ((bh = bh_use[ra_ptr++]) == NULL)
2026 if (!buffer_uptodate(bh)) {
2027 /* read error, skip block & hope for the best */
2028 ocfs2_error(dir->i_sb, "reading directory %llu, "
2030 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2035 i = ocfs2_search_dirblock(bh, dir, name, namelen,
2036 block << sb->s_blocksize_bits,
2039 OCFS2_I(dir)->ip_dir_start_lookup = block;
2041 goto cleanup_and_exit;
2045 goto cleanup_and_exit;
2048 if (++block >= nblocks)
2050 } while (block != start);
2053 * If the directory has grown while we were searching, then
2054 * search the last part of the directory before giving up.
2057 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
2058 if (block < nblocks) {
2064 /* Clean up the read-ahead blocks */
2065 for (; ra_ptr < ra_max; ra_ptr++)
2066 brelse(bh_use[ra_ptr]);
2072 static int ocfs2_blkno_stringify(u64 blkno, char *name)
2074 int status, namelen;
2078 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
2088 if (namelen != OCFS2_ORPHAN_NAMELEN) {
2094 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
2103 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
2104 struct inode **ret_orphan_dir,
2105 struct inode *inode,
2107 struct buffer_head **de_bh)
2109 struct inode *orphan_dir_inode;
2110 struct buffer_head *orphan_dir_bh = NULL;
2113 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2119 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2120 ORPHAN_DIR_SYSTEM_INODE,
2122 if (!orphan_dir_inode) {
2128 mutex_lock(&orphan_dir_inode->i_mutex);
2130 status = ocfs2_meta_lock(orphan_dir_inode, NULL, &orphan_dir_bh, 1);
2136 status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
2137 orphan_dir_bh, name,
2138 OCFS2_ORPHAN_NAMELEN, de_bh);
2140 ocfs2_meta_unlock(orphan_dir_inode, 1);
2146 *ret_orphan_dir = orphan_dir_inode;
2150 mutex_unlock(&orphan_dir_inode->i_mutex);
2151 iput(orphan_dir_inode);
2155 brelse(orphan_dir_bh);
2161 static int ocfs2_orphan_add(struct ocfs2_super *osb,
2162 struct ocfs2_journal_handle *handle,
2163 struct inode *inode,
2164 struct ocfs2_dinode *fe,
2166 struct buffer_head *de_bh,
2167 struct inode *orphan_dir_inode)
2169 struct buffer_head *orphan_dir_bh = NULL;
2171 struct ocfs2_dinode *orphan_fe;
2173 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
2175 status = ocfs2_read_block(osb,
2176 OCFS2_I(orphan_dir_inode)->ip_blkno,
2177 &orphan_dir_bh, OCFS2_BH_CACHED,
2184 status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
2185 OCFS2_JOURNAL_ACCESS_WRITE);
2191 /* we're a cluster, and nlink can change on disk from
2192 * underneath us... */
2193 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2194 if (S_ISDIR(inode->i_mode))
2195 le16_add_cpu(&orphan_fe->i_links_count, 1);
2196 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2198 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2204 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
2205 OCFS2_ORPHAN_NAMELEN, inode,
2206 OCFS2_I(inode)->ip_blkno,
2207 orphan_dir_bh, de_bh);
2213 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
2215 /* Record which orphan dir our inode now resides
2216 * in. delete_inode will use this to determine which orphan
2218 spin_lock(&OCFS2_I(inode)->ip_lock);
2219 OCFS2_I(inode)->ip_orphaned_slot = osb->slot_num;
2220 spin_unlock(&OCFS2_I(inode)->ip_lock);
2222 mlog(0, "Inode %llu orphaned in slot %d\n",
2223 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
2227 brelse(orphan_dir_bh);
2233 /* unlike orphan_add, we expect the orphan dir to already be locked here. */
2234 int ocfs2_orphan_del(struct ocfs2_super *osb,
2235 struct ocfs2_journal_handle *handle,
2236 struct inode *orphan_dir_inode,
2237 struct inode *inode,
2238 struct buffer_head *orphan_dir_bh)
2240 char name[OCFS2_ORPHAN_NAMELEN + 1];
2241 struct ocfs2_dinode *orphan_fe;
2243 struct buffer_head *target_de_bh = NULL;
2244 struct ocfs2_dir_entry *target_de = NULL;
2248 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2254 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
2255 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
2256 OCFS2_ORPHAN_NAMELEN);
2258 /* find it's spot in the orphan directory */
2259 target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
2260 orphan_dir_inode, &target_de);
2261 if (!target_de_bh) {
2267 /* remove it from the orphan directory */
2268 status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
2275 status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
2276 OCFS2_JOURNAL_ACCESS_WRITE);
2282 /* do the i_nlink dance! :) */
2283 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2284 if (S_ISDIR(inode->i_mode))
2285 le16_add_cpu(&orphan_fe->i_links_count, -1);
2286 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2288 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2296 brelse(target_de_bh);
2302 struct inode_operations ocfs2_dir_iops = {
2303 .create = ocfs2_create,
2304 .lookup = ocfs2_lookup,
2306 .unlink = ocfs2_unlink,
2307 .rmdir = ocfs2_unlink,
2308 .symlink = ocfs2_symlink,
2309 .mkdir = ocfs2_mkdir,
2310 .mknod = ocfs2_mknod,
2311 .rename = ocfs2_rename,
2312 .setattr = ocfs2_setattr,
2313 .getattr = ocfs2_getattr,