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>
43 #include <linux/vs_tag.h>
45 #define MLOG_MASK_PREFIX ML_NAMEI
46 #include <cluster/masklog.h>
54 #include "extent_map.h"
66 #include "buffer_head_io.h"
68 #define NAMEI_RA_CHUNKS 2
69 #define NAMEI_RA_BLOCKS 4
70 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
71 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
73 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
75 const char *name, int namelen,
77 struct ocfs2_dir_entry **res_dir);
79 static int ocfs2_delete_entry(handle_t *handle,
81 struct ocfs2_dir_entry *de_del,
82 struct buffer_head *bh);
84 static int __ocfs2_add_entry(handle_t *handle,
86 const char *name, int namelen,
87 struct inode *inode, u64 blkno,
88 struct buffer_head *parent_fe_bh,
89 struct buffer_head *insert_bh);
91 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
93 struct dentry *dentry, int mode,
95 struct buffer_head **new_fe_bh,
96 struct buffer_head *parent_fe_bh,
98 struct inode **ret_inode,
99 struct ocfs2_alloc_context *inode_ac);
101 static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
103 struct inode *parent,
105 struct buffer_head *fe_bh,
106 struct ocfs2_alloc_context *data_ac);
108 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
109 struct inode **ret_orphan_dir,
112 struct buffer_head **de_bh);
114 static int ocfs2_orphan_add(struct ocfs2_super *osb,
117 struct ocfs2_dinode *fe,
119 struct buffer_head *de_bh,
120 struct inode *orphan_dir_inode);
122 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
125 const char *symname);
127 static inline int ocfs2_add_entry(handle_t *handle,
128 struct dentry *dentry,
129 struct inode *inode, u64 blkno,
130 struct buffer_head *parent_fe_bh,
131 struct buffer_head *insert_bh)
133 return __ocfs2_add_entry(handle, dentry->d_parent->d_inode,
134 dentry->d_name.name, dentry->d_name.len,
135 inode, blkno, parent_fe_bh, insert_bh);
138 /* An orphan dir name is an 8 byte value, printed as a hex string */
139 #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
141 static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
142 struct nameidata *nd)
146 struct buffer_head *dirent_bh = NULL;
147 struct inode *inode = NULL;
149 struct ocfs2_dir_entry *dirent;
150 struct ocfs2_inode_info *oi;
152 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
153 dentry->d_name.len, dentry->d_name.name);
155 if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
156 ret = ERR_PTR(-ENAMETOOLONG);
160 mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
161 dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
163 status = ocfs2_meta_lock(dir, NULL, 0);
165 if (status != -ENOENT)
167 ret = ERR_PTR(status);
171 status = ocfs2_find_files_on_disk(dentry->d_name.name,
172 dentry->d_name.len, &blkno,
173 dir, &dirent_bh, &dirent);
177 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0);
179 mlog(ML_ERROR, "Unable to create inode %llu\n",
180 (unsigned long long)blkno);
181 ret = ERR_PTR(-EACCES);
186 /* Clear any orphaned state... If we were able to look up the
187 * inode from a directory, it certainly can't be orphaned. We
188 * might have the bad state from a node which intended to
189 * orphan this inode but crashed before it could commit the
191 spin_lock(&oi->ip_lock);
192 oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
193 oi->ip_orphaned_slot = OCFS2_INVALID_SLOT;
194 spin_unlock(&oi->ip_lock);
197 dentry->d_op = &ocfs2_dentry_ops;
198 ret = d_splice_alias(inode, dentry);
202 * If d_splice_alias() finds a DCACHE_DISCONNECTED
203 * dentry, it will d_move() it on top of ourse. The
204 * return value will indicate this however, so in
205 * those cases, we switch them around for the locking
208 * NOTE: This dentry already has ->d_op set from
209 * ocfs2_get_parent() and ocfs2_get_dentry()
214 status = ocfs2_dentry_attach_lock(dentry, inode,
215 OCFS2_I(dir)->ip_blkno);
218 ret = ERR_PTR(status);
224 /* Don't drop the cluster lock until *after* the d_add --
225 * unlink on another node will message us to remove that
226 * dentry under this lock so otherwise we can race this with
227 * the vote thread and have a stale dentry. */
228 ocfs2_meta_unlock(dir, 0);
239 static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
241 struct inode *parent,
243 struct buffer_head *fe_bh,
244 struct ocfs2_alloc_context *data_ac)
247 struct buffer_head *new_bh = NULL;
248 struct ocfs2_dir_entry *de = NULL;
252 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
253 data_ac, NULL, &new_bh);
259 ocfs2_set_new_buffer_uptodate(inode, new_bh);
261 status = ocfs2_journal_access(handle, inode, new_bh,
262 OCFS2_JOURNAL_ACCESS_CREATE);
267 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
269 de = (struct ocfs2_dir_entry *) new_bh->b_data;
270 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
273 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
274 strcpy(de->name, ".");
275 ocfs2_set_de_type(de, S_IFDIR);
276 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
277 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
278 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
279 OCFS2_DIR_REC_LEN(1));
281 strcpy(de->name, "..");
282 ocfs2_set_de_type(de, S_IFDIR);
284 status = ocfs2_journal_dirty(handle, new_bh);
290 i_size_write(inode, inode->i_sb->s_blocksize);
292 inode->i_blocks = ocfs2_align_bytes_to_sectors(inode->i_sb->s_blocksize);
293 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
308 static int ocfs2_mknod(struct inode *dir,
309 struct dentry *dentry,
314 struct buffer_head *parent_fe_bh = NULL;
315 handle_t *handle = NULL;
316 struct ocfs2_super *osb;
317 struct ocfs2_dinode *dirfe;
318 struct buffer_head *new_fe_bh = NULL;
319 struct buffer_head *de_bh = NULL;
320 struct inode *inode = NULL;
321 struct ocfs2_alloc_context *inode_ac = NULL;
322 struct ocfs2_alloc_context *data_ac = NULL;
324 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
325 (unsigned long)dev, dentry->d_name.len,
326 dentry->d_name.name);
328 /* get our super block */
329 osb = OCFS2_SB(dir->i_sb);
331 status = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
333 if (status != -ENOENT)
338 if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
343 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
344 if (!dirfe->i_links_count) {
345 /* can't make a file in a deleted directory. */
350 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
355 /* get a spot inside the dir. */
356 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
358 dentry->d_name.len, &de_bh);
364 /* reserve an inode spot */
365 status = ocfs2_reserve_new_inode(osb, &inode_ac);
367 if (status != -ENOSPC)
372 /* are we making a directory? If so, reserve a cluster for his
375 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
377 if (status != -ENOSPC)
383 handle = ocfs2_start_trans(osb, OCFS2_MKNOD_CREDITS);
384 if (IS_ERR(handle)) {
385 status = PTR_ERR(handle);
391 /* do the real work now. */
392 status = ocfs2_mknod_locked(osb, dir, dentry, mode, dev,
393 &new_fe_bh, parent_fe_bh, handle,
401 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
408 status = ocfs2_journal_access(handle, dir, parent_fe_bh,
409 OCFS2_JOURNAL_ACCESS_WRITE);
414 le16_add_cpu(&dirfe->i_links_count, 1);
415 status = ocfs2_journal_dirty(handle, parent_fe_bh);
423 status = ocfs2_add_entry(handle, dentry, inode,
424 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
431 status = ocfs2_dentry_attach_lock(dentry, inode,
432 OCFS2_I(dir)->ip_blkno);
438 insert_inode_hash(inode);
439 dentry->d_op = &ocfs2_dentry_ops;
440 d_instantiate(dentry, inode);
444 ocfs2_commit_trans(osb, handle);
446 ocfs2_meta_unlock(dir, 1);
448 if (status == -ENOSPC)
449 mlog(0, "Disk is full\n");
458 brelse(parent_fe_bh);
460 if ((status < 0) && inode)
464 ocfs2_free_alloc_context(inode_ac);
467 ocfs2_free_alloc_context(data_ac);
474 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
476 struct dentry *dentry, int mode,
478 struct buffer_head **new_fe_bh,
479 struct buffer_head *parent_fe_bh,
481 struct inode **ret_inode,
482 struct ocfs2_alloc_context *inode_ac)
485 struct ocfs2_dinode *fe = NULL;
486 struct ocfs2_extent_list *fel;
489 struct inode *inode = NULL;
494 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
495 (unsigned long)dev, dentry->d_name.len,
496 dentry->d_name.name);
501 status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
508 inode = new_inode(dir->i_sb);
510 status = PTR_ERR(inode);
511 mlog(ML_ERROR, "new_inode failed!\n");
515 /* populate as many fields early on as possible - many of
516 * these are used by the support functions here and in
518 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
519 OCFS2_I(inode)->ip_blkno = fe_blkno;
524 inode->i_mode = mode;
525 spin_lock(&osb->osb_lock);
526 inode->i_generation = osb->s_next_generation++;
527 spin_unlock(&osb->osb_lock);
529 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
535 ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
537 status = ocfs2_journal_access(handle, inode, *new_fe_bh,
538 OCFS2_JOURNAL_ACCESS_CREATE);
544 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
545 memset(fe, 0, osb->sb->s_blocksize);
547 fe->i_generation = cpu_to_le32(inode->i_generation);
548 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
549 fe->i_blkno = cpu_to_le64(fe_blkno);
550 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
551 fe->i_suballoc_slot = cpu_to_le16(osb->slot_num);
553 tag = dx_current_fstag(osb->sb);
554 uid = current->fsuid;
555 if (dir->i_mode & S_ISGID) {
560 gid = current->fsgid;
562 fe->i_uid = cpu_to_le32(TAGINO_UID(DX_TAG(inode), uid, tag));
563 fe->i_gid = cpu_to_le32(TAGINO_GID(DX_TAG(inode), gid, tag));
565 fe->i_mode = cpu_to_le16(mode);
566 if (S_ISCHR(mode) || S_ISBLK(mode))
567 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
569 fe->i_links_count = cpu_to_le16(inode->i_nlink);
571 fe->i_last_eb_blk = 0;
572 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
573 le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
574 fe->i_atime = fe->i_ctime = fe->i_mtime =
575 cpu_to_le64(CURRENT_TIME.tv_sec);
576 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
577 cpu_to_le32(CURRENT_TIME.tv_nsec);
580 fel = &fe->id2.i_list;
581 fel->l_tree_depth = 0;
582 fel->l_next_free_rec = 0;
583 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
585 status = ocfs2_journal_dirty(handle, *new_fe_bh);
591 if (ocfs2_populate_inode(inode, fe, 1) < 0) {
592 mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
593 "i_blkno=%llu, i_ino=%lu\n",
594 (unsigned long long) (*new_fe_bh)->b_blocknr,
595 (unsigned long long)fe->i_blkno, inode->i_ino);
599 ocfs2_inode_set_new(osb, inode);
600 if (!ocfs2_mount_local(osb)) {
601 status = ocfs2_create_new_inode_locks(inode);
606 status = 0; /* error in ocfs2_create_new_inode_locks is not
624 static int ocfs2_mkdir(struct inode *dir,
625 struct dentry *dentry,
630 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
631 dentry->d_name.len, dentry->d_name.name);
632 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
638 static int ocfs2_create(struct inode *dir,
639 struct dentry *dentry,
641 struct nameidata *nd)
645 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
646 dentry->d_name.len, dentry->d_name.name);
647 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
653 static int ocfs2_link(struct dentry *old_dentry,
655 struct dentry *dentry)
658 struct inode *inode = old_dentry->d_inode;
660 struct buffer_head *fe_bh = NULL;
661 struct buffer_head *parent_fe_bh = NULL;
662 struct buffer_head *de_bh = NULL;
663 struct ocfs2_dinode *fe = NULL;
664 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
666 mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
667 old_dentry->d_name.len, old_dentry->d_name.name,
668 dentry->d_name.len, dentry->d_name.name);
670 if (S_ISDIR(inode->i_mode))
673 err = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
685 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
690 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
692 dentry->d_name.len, &de_bh);
698 err = ocfs2_meta_lock(inode, &fe_bh, 1);
705 fe = (struct ocfs2_dinode *) fe_bh->b_data;
706 if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
708 goto out_unlock_inode;
711 handle = ocfs2_start_trans(osb, OCFS2_LINK_CREDITS);
712 if (IS_ERR(handle)) {
713 err = PTR_ERR(handle);
716 goto out_unlock_inode;
719 err = ocfs2_journal_access(handle, inode, fe_bh,
720 OCFS2_JOURNAL_ACCESS_WRITE);
727 inode->i_ctime = CURRENT_TIME;
728 fe->i_links_count = cpu_to_le16(inode->i_nlink);
729 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
730 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
732 err = ocfs2_journal_dirty(handle, fe_bh);
734 le16_add_cpu(&fe->i_links_count, -1);
740 err = ocfs2_add_entry(handle, dentry, inode,
741 OCFS2_I(inode)->ip_blkno,
742 parent_fe_bh, de_bh);
744 le16_add_cpu(&fe->i_links_count, -1);
750 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
756 atomic_inc(&inode->i_count);
757 dentry->d_op = &ocfs2_dentry_ops;
758 d_instantiate(dentry, inode);
761 ocfs2_commit_trans(osb, handle);
763 ocfs2_meta_unlock(inode, 1);
766 ocfs2_meta_unlock(dir, 1);
773 brelse(parent_fe_bh);
781 * Takes and drops an exclusive lock on the given dentry. This will
782 * force other nodes to drop it.
784 static int ocfs2_remote_dentry_delete(struct dentry *dentry)
788 ret = ocfs2_dentry_lock(dentry, 1);
792 ocfs2_dentry_unlock(dentry, 1);
797 static inline int inode_is_unlinkable(struct inode *inode)
799 if (S_ISDIR(inode->i_mode)) {
800 if (inode->i_nlink == 2)
805 if (inode->i_nlink == 1)
810 static int ocfs2_unlink(struct inode *dir,
811 struct dentry *dentry)
814 int child_locked = 0;
815 struct inode *inode = dentry->d_inode;
816 struct inode *orphan_dir = NULL;
817 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
819 struct ocfs2_dinode *fe = NULL;
820 struct buffer_head *fe_bh = NULL;
821 struct buffer_head *parent_node_bh = NULL;
822 handle_t *handle = NULL;
823 struct ocfs2_dir_entry *dirent = NULL;
824 struct buffer_head *dirent_bh = NULL;
825 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
826 struct buffer_head *orphan_entry_bh = NULL;
828 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
829 dentry->d_name.len, dentry->d_name.name);
831 BUG_ON(dentry->d_parent->d_inode != dir);
833 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
835 if (inode == osb->root_inode) {
836 mlog(0, "Cannot delete the root directory\n");
840 status = ocfs2_meta_lock(dir, &parent_node_bh, 1);
842 if (status != -ENOENT)
847 status = ocfs2_find_files_on_disk(dentry->d_name.name,
848 dentry->d_name.len, &blkno,
849 dir, &dirent_bh, &dirent);
851 if (status != -ENOENT)
856 if (OCFS2_I(inode)->ip_blkno != blkno) {
859 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
860 (unsigned long long)OCFS2_I(inode)->ip_blkno,
861 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
865 status = ocfs2_meta_lock(inode, &fe_bh, 1);
867 if (status != -ENOENT)
873 if (S_ISDIR(inode->i_mode)) {
874 if (!ocfs2_empty_dir(inode)) {
877 } else if (inode->i_nlink != 2) {
883 status = ocfs2_remote_dentry_delete(dentry);
885 /* This vote should succeed under all normal
891 if (inode_is_unlinkable(inode)) {
892 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
901 handle = ocfs2_start_trans(osb, OCFS2_UNLINK_CREDITS);
902 if (IS_ERR(handle)) {
903 status = PTR_ERR(handle);
909 status = ocfs2_journal_access(handle, inode, fe_bh,
910 OCFS2_JOURNAL_ACCESS_WRITE);
916 fe = (struct ocfs2_dinode *) fe_bh->b_data;
918 if (inode_is_unlinkable(inode)) {
919 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
920 orphan_entry_bh, orphan_dir);
927 /* delete the name from the parent dir */
928 status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
934 if (S_ISDIR(inode->i_mode))
937 fe->i_links_count = cpu_to_le16(inode->i_nlink);
939 status = ocfs2_journal_dirty(handle, fe_bh);
945 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
946 if (S_ISDIR(inode->i_mode))
949 status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
952 if (S_ISDIR(inode->i_mode))
958 ocfs2_commit_trans(osb, handle);
961 ocfs2_meta_unlock(inode, 1);
963 ocfs2_meta_unlock(dir, 1);
966 /* This was locked for us in ocfs2_prepare_orphan_dir() */
967 ocfs2_meta_unlock(orphan_dir, 1);
968 mutex_unlock(&orphan_dir->i_mutex);
979 brelse(parent_node_bh);
982 brelse(orphan_entry_bh);
990 * The only place this should be used is rename!
991 * if they have the same id, then the 1st one is the only one locked.
993 static int ocfs2_double_lock(struct ocfs2_super *osb,
994 struct buffer_head **bh1,
995 struct inode *inode1,
996 struct buffer_head **bh2,
997 struct inode *inode2)
1000 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
1001 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
1002 struct buffer_head **tmpbh;
1003 struct inode *tmpinode;
1005 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
1006 (unsigned long long)oi1->ip_blkno,
1007 (unsigned long long)oi2->ip_blkno);
1014 /* we always want to lock the one with the lower lockid first. */
1015 if (oi1->ip_blkno != oi2->ip_blkno) {
1016 if (oi1->ip_blkno < oi2->ip_blkno) {
1017 /* switch id1 and id2 around */
1018 mlog(0, "switching them around...\n");
1028 status = ocfs2_meta_lock(inode2, bh2, 1);
1030 if (status != -ENOENT)
1037 status = ocfs2_meta_lock(inode1, bh1, 1);
1040 * An error return must mean that no cluster locks
1041 * were held on function exit.
1043 if (oi1->ip_blkno != oi2->ip_blkno)
1044 ocfs2_meta_unlock(inode2, 1);
1046 if (status != -ENOENT)
1055 static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
1057 ocfs2_meta_unlock(inode1, 1);
1059 if (inode1 != inode2)
1060 ocfs2_meta_unlock(inode2, 1);
1063 #define PARENT_INO(buffer) \
1064 ((struct ocfs2_dir_entry *) \
1066 le16_to_cpu(((struct ocfs2_dir_entry *)buffer)->rec_len)))->inode
1068 static int ocfs2_rename(struct inode *old_dir,
1069 struct dentry *old_dentry,
1070 struct inode *new_dir,
1071 struct dentry *new_dentry)
1073 int status = 0, rename_lock = 0, parents_locked = 0;
1074 int old_child_locked = 0, new_child_locked = 0;
1075 struct inode *old_inode = old_dentry->d_inode;
1076 struct inode *new_inode = new_dentry->d_inode;
1077 struct inode *orphan_dir = NULL;
1078 struct ocfs2_dinode *newfe = NULL;
1079 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
1080 struct buffer_head *orphan_entry_bh = NULL;
1081 struct buffer_head *newfe_bh = NULL;
1082 struct buffer_head *old_inode_bh = NULL;
1083 struct buffer_head *insert_entry_bh = NULL;
1084 struct ocfs2_super *osb = NULL;
1086 handle_t *handle = NULL;
1087 struct buffer_head *old_dir_bh = NULL;
1088 struct buffer_head *new_dir_bh = NULL;
1089 struct ocfs2_dir_entry *old_de = NULL, *new_de = NULL; // dirent for old_dentry
1091 struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
1092 struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
1093 // this is the 1st dirent bh
1094 nlink_t old_dir_nlink = old_dir->i_nlink;
1096 /* At some point it might be nice to break this function up a
1099 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
1100 old_dir, old_dentry, new_dir, new_dentry,
1101 old_dentry->d_name.len, old_dentry->d_name.name,
1102 new_dentry->d_name.len, new_dentry->d_name.name);
1104 osb = OCFS2_SB(old_dir->i_sb);
1107 if (!igrab(new_inode))
1111 /* Assume a directory heirarchy thusly:
1114 * a,b,c, and d are all directories.
1116 * from cwd of 'a' on both nodes:
1120 * And that's why, just like the VFS, we need a file system
1122 if (old_dentry != new_dentry) {
1123 status = ocfs2_rename_lock(osb);
1131 /* if old and new are the same, this'll just do one lock. */
1132 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1133 &new_dir_bh, new_dir);
1140 /* make sure both dirs have bhs
1141 * get an extra ref on old_dir_bh if old==new */
1144 new_dir_bh = old_dir_bh;
1147 mlog(ML_ERROR, "no old_dir_bh!\n");
1154 * Aside from allowing a meta data update, the locking here
1155 * also ensures that the vote thread on other nodes won't have
1156 * to concurrently downconvert the inode and the dentry locks.
1158 status = ocfs2_meta_lock(old_inode, &old_inode_bh, 1);
1160 if (status != -ENOENT)
1164 old_child_locked = 1;
1166 status = ocfs2_remote_dentry_delete(old_dentry);
1172 if (S_ISDIR(old_inode->i_mode)) {
1174 old_inode_de_bh = ocfs2_bread(old_inode, 0, &status, 0);
1175 if (!old_inode_de_bh)
1179 if (le64_to_cpu(PARENT_INO(old_inode_de_bh->b_data)) !=
1180 OCFS2_I(old_dir)->ip_blkno)
1183 if (!new_inode && new_dir!=old_dir &&
1184 new_dir->i_nlink >= OCFS2_LINK_MAX)
1189 old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
1190 old_dentry->d_name.len,
1196 * Check for inode number is _not_ due to possible IO errors.
1197 * We might rmdir the source, keep it as pwd of some process
1198 * and merrily kill the link to whatever was created under the
1199 * same name. Goodbye sticky bit ;-<
1201 if (le64_to_cpu(old_de->inode) != OCFS2_I(old_inode)->ip_blkno)
1204 /* check if the target already exists (in which case we need
1206 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1207 new_dentry->d_name.len,
1208 &newfe_blkno, new_dir, &new_de_bh,
1210 /* The only error we allow here is -ENOENT because the new
1211 * file not existing is perfectly valid. */
1212 if ((status < 0) && (status != -ENOENT)) {
1213 /* If we cannot find the file specified we should just */
1214 /* return the error... */
1219 if (!new_de && new_inode)
1220 mlog(ML_ERROR, "inode %lu does not exist in it's parent "
1221 "directory!", new_inode->i_ino);
1223 /* In case we need to overwrite an existing file, we blow it
1226 /* VFS didn't think there existed an inode here, but
1227 * someone else in the cluster must have raced our
1228 * rename to create one. Today we error cleanly, in
1229 * the future we should consider calling iget to build
1230 * a new struct inode for this entry. */
1234 mlog(0, "We found an inode for name %.*s but VFS "
1235 "didn't give us one.\n", new_dentry->d_name.len,
1236 new_dentry->d_name.name);
1240 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1243 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1244 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1245 (unsigned long long)newfe_blkno,
1246 OCFS2_I(new_inode)->ip_flags);
1250 status = ocfs2_meta_lock(new_inode, &newfe_bh, 1);
1252 if (status != -ENOENT)
1256 new_child_locked = 1;
1258 status = ocfs2_remote_dentry_delete(new_dentry);
1264 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1266 mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
1267 "newfebh=%p bhblocknr=%llu\n", new_de,
1268 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
1269 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1271 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
1272 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
1282 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1284 status = ocfs2_check_dir_for_entry(new_dir,
1285 new_dentry->d_name.name,
1286 new_dentry->d_name.len);
1290 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1291 new_dentry->d_name.name,
1292 new_dentry->d_name.len,
1300 handle = ocfs2_start_trans(osb, OCFS2_RENAME_CREDITS);
1301 if (IS_ERR(handle)) {
1302 status = PTR_ERR(handle);
1309 if (S_ISDIR(new_inode->i_mode)) {
1310 if (!ocfs2_empty_dir(new_inode) ||
1311 new_inode->i_nlink != 2) {
1312 status = -ENOTEMPTY;
1316 status = ocfs2_journal_access(handle, new_inode, newfe_bh,
1317 OCFS2_JOURNAL_ACCESS_WRITE);
1323 if (S_ISDIR(new_inode->i_mode) ||
1324 (newfe->i_links_count == cpu_to_le16(1))){
1325 status = ocfs2_orphan_add(osb, handle, new_inode,
1327 orphan_entry_bh, orphan_dir);
1334 /* change the dirent to point to the correct inode */
1335 status = ocfs2_journal_access(handle, new_dir, new_de_bh,
1336 OCFS2_JOURNAL_ACCESS_WRITE);
1341 new_de->inode = cpu_to_le64(OCFS2_I(old_inode)->ip_blkno);
1342 new_de->file_type = old_de->file_type;
1343 new_dir->i_version++;
1344 status = ocfs2_journal_dirty(handle, new_de_bh);
1350 if (S_ISDIR(new_inode->i_mode))
1351 newfe->i_links_count = 0;
1353 le16_add_cpu(&newfe->i_links_count, -1);
1355 status = ocfs2_journal_dirty(handle, newfe_bh);
1361 /* if the name was not found in new_dir, add it now */
1362 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1363 OCFS2_I(old_inode)->ip_blkno,
1364 new_dir_bh, insert_entry_bh);
1367 old_inode->i_ctime = CURRENT_TIME;
1368 mark_inode_dirty(old_inode);
1369 ocfs2_mark_inode_dirty(handle, old_inode, old_inode_bh);
1371 /* now that the name has been added to new_dir, remove the old name */
1372 status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
1379 new_inode->i_nlink--;
1380 new_inode->i_ctime = CURRENT_TIME;
1382 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1383 if (old_inode_de_bh) {
1384 status = ocfs2_journal_access(handle, old_inode,
1386 OCFS2_JOURNAL_ACCESS_WRITE);
1387 PARENT_INO(old_inode_de_bh->b_data) =
1388 cpu_to_le64(OCFS2_I(new_dir)->ip_blkno);
1389 status = ocfs2_journal_dirty(handle, old_inode_de_bh);
1392 new_inode->i_nlink--;
1395 mark_inode_dirty(new_dir);
1398 mark_inode_dirty(old_dir);
1399 ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1401 mark_inode_dirty(new_inode);
1402 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1405 if (old_dir != new_dir) {
1406 /* Keep the same times on both directories.*/
1407 new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
1410 * This will also pick up the i_nlink change from the
1413 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
1416 if (old_dir_nlink != old_dir->i_nlink) {
1418 mlog(ML_ERROR, "need to change nlink for old dir "
1419 "%llu from %d to %d but bh is NULL!\n",
1420 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1421 (int)old_dir_nlink, old_dir->i_nlink);
1423 struct ocfs2_dinode *fe;
1424 status = ocfs2_journal_access(handle, old_dir,
1426 OCFS2_JOURNAL_ACCESS_WRITE);
1427 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1428 fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
1429 status = ocfs2_journal_dirty(handle, old_dir_bh);
1433 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
1437 ocfs2_rename_unlock(osb);
1440 ocfs2_commit_trans(osb, handle);
1443 ocfs2_double_unlock(old_dir, new_dir);
1445 if (old_child_locked)
1446 ocfs2_meta_unlock(old_inode, 1);
1448 if (new_child_locked)
1449 ocfs2_meta_unlock(new_inode, 1);
1452 /* This was locked for us in ocfs2_prepare_orphan_dir() */
1453 ocfs2_meta_unlock(orphan_dir, 1);
1454 mutex_unlock(&orphan_dir->i_mutex);
1459 sync_mapping_buffers(old_inode->i_mapping);
1466 brelse(old_inode_bh);
1475 if (old_inode_de_bh)
1476 brelse(old_inode_de_bh);
1477 if (orphan_entry_bh)
1478 brelse(orphan_entry_bh);
1479 if (insert_entry_bh)
1480 brelse(insert_entry_bh);
1488 * we expect i_size = strlen(symname). Copy symname into the file
1489 * data, including the null terminator.
1491 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1493 struct inode *inode,
1494 const char *symname)
1496 struct buffer_head **bhs = NULL;
1498 struct super_block *sb = osb->sb;
1501 int virtual, blocks, status, i, bytes_left;
1503 bytes_left = i_size_read(inode) + 1;
1504 /* we can't trust i_blocks because we're actually going to
1505 * write i_size + 1 bytes. */
1506 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1508 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1509 (unsigned long long)inode->i_blocks,
1510 i_size_read(inode), blocks);
1512 /* Sanity check -- make sure we're going to fit. */
1514 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1520 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1527 status = ocfs2_extent_map_get_blocks(inode, 0, 1, &p_blkno,
1534 /* links can never be larger than one cluster so we know this
1535 * is all going to be contiguous, but do a sanity check
1537 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1544 while(bytes_left > 0) {
1545 c = &symname[virtual * sb->s_blocksize];
1547 bhs[virtual] = sb_getblk(sb, p_blkno);
1548 if (!bhs[virtual]) {
1553 ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
1555 status = ocfs2_journal_access(handle, inode, bhs[virtual],
1556 OCFS2_JOURNAL_ACCESS_CREATE);
1562 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1564 memcpy(bhs[virtual]->b_data, c,
1565 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1568 status = ocfs2_journal_dirty(handle, bhs[virtual]);
1576 bytes_left -= sb->s_blocksize;
1583 for(i = 0; i < blocks; i++)
1593 static int ocfs2_symlink(struct inode *dir,
1594 struct dentry *dentry,
1595 const char *symname)
1597 int status, l, credits;
1599 struct ocfs2_super *osb = NULL;
1600 struct inode *inode = NULL;
1601 struct super_block *sb;
1602 struct buffer_head *new_fe_bh = NULL;
1603 struct buffer_head *de_bh = NULL;
1604 struct buffer_head *parent_fe_bh = NULL;
1605 struct ocfs2_dinode *fe = NULL;
1606 struct ocfs2_dinode *dirfe;
1607 handle_t *handle = NULL;
1608 struct ocfs2_alloc_context *inode_ac = NULL;
1609 struct ocfs2_alloc_context *data_ac = NULL;
1611 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1612 dentry, symname, dentry->d_name.len, dentry->d_name.name);
1617 l = strlen(symname) + 1;
1619 credits = ocfs2_calc_symlink_credits(sb);
1621 /* lock the parent directory */
1622 status = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
1624 if (status != -ENOENT)
1629 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1630 if (!dirfe->i_links_count) {
1631 /* can't make a file in a deleted directory. */
1636 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1637 dentry->d_name.len);
1641 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1642 dentry->d_name.name,
1643 dentry->d_name.len, &de_bh);
1649 status = ocfs2_reserve_new_inode(osb, &inode_ac);
1651 if (status != -ENOSPC)
1656 /* don't reserve bitmap space for fast symlinks. */
1657 if (l > ocfs2_fast_symlink_chars(sb)) {
1658 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
1660 if (status != -ENOSPC)
1666 handle = ocfs2_start_trans(osb, credits);
1667 if (IS_ERR(handle)) {
1668 status = PTR_ERR(handle);
1674 status = ocfs2_mknod_locked(osb, dir, dentry,
1675 S_IFLNK | S_IRWXUGO, 0,
1676 &new_fe_bh, parent_fe_bh, handle,
1683 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1686 if (l > ocfs2_fast_symlink_chars(sb)) {
1687 inode->i_op = &ocfs2_symlink_inode_operations;
1688 status = ocfs2_do_extend_allocation(osb, inode, 1, new_fe_bh,
1689 handle, data_ac, NULL,
1692 if (status != -ENOSPC && status != -EINTR) {
1694 "Failed to extend file to %llu\n",
1695 (unsigned long long)newsize);
1701 i_size_write(inode, newsize);
1702 inode->i_blocks = ocfs2_align_bytes_to_sectors(newsize);
1704 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1705 memcpy((char *) fe->id2.i_symlink, symname, l);
1706 i_size_write(inode, newsize);
1707 inode->i_blocks = 0;
1710 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1716 if (!ocfs2_inode_is_fast_symlink(inode)) {
1717 status = ocfs2_create_symlink_data(osb, handle, inode,
1725 status = ocfs2_add_entry(handle, dentry, inode,
1726 le64_to_cpu(fe->i_blkno), parent_fe_bh,
1733 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
1739 insert_inode_hash(inode);
1740 dentry->d_op = &ocfs2_dentry_ops;
1741 d_instantiate(dentry, inode);
1744 ocfs2_commit_trans(osb, handle);
1746 ocfs2_meta_unlock(dir, 1);
1751 brelse(parent_fe_bh);
1755 ocfs2_free_alloc_context(inode_ac);
1757 ocfs2_free_alloc_context(data_ac);
1758 if ((status < 0) && inode)
1766 int ocfs2_check_dir_entry(struct inode * dir,
1767 struct ocfs2_dir_entry * de,
1768 struct buffer_head * bh,
1769 unsigned long offset)
1771 const char *error_msg = NULL;
1772 const int rlen = le16_to_cpu(de->rec_len);
1774 if (rlen < OCFS2_DIR_REC_LEN(1))
1775 error_msg = "rec_len is smaller than minimal";
1776 else if (rlen % 4 != 0)
1777 error_msg = "rec_len % 4 != 0";
1778 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
1779 error_msg = "rec_len is too small for name_len";
1780 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
1781 error_msg = "directory entry across blocks";
1783 if (error_msg != NULL)
1784 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
1785 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
1786 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
1787 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
1789 return error_msg == NULL ? 1 : 0;
1792 /* we don't always have a dentry for what we want to add, so people
1793 * like orphan dir can call this instead.
1795 * If you pass me insert_bh, I'll skip the search of the other dir
1796 * blocks and put the record in there.
1798 static int __ocfs2_add_entry(handle_t *handle,
1800 const char *name, int namelen,
1801 struct inode *inode, u64 blkno,
1802 struct buffer_head *parent_fe_bh,
1803 struct buffer_head *insert_bh)
1805 unsigned long offset;
1806 unsigned short rec_len;
1807 struct ocfs2_dir_entry *de, *de1;
1808 struct super_block *sb;
1818 rec_len = OCFS2_DIR_REC_LEN(namelen);
1820 de = (struct ocfs2_dir_entry *) insert_bh->b_data;
1822 BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
1823 /* These checks should've already been passed by the
1824 * prepare function, but I guess we can leave them
1826 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
1830 if (ocfs2_match(namelen, name, de)) {
1834 if (((le64_to_cpu(de->inode) == 0) &&
1835 (le16_to_cpu(de->rec_len) >= rec_len)) ||
1836 (le16_to_cpu(de->rec_len) >=
1837 (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
1838 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1839 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1845 status = ocfs2_journal_access(handle, dir, insert_bh,
1846 OCFS2_JOURNAL_ACCESS_WRITE);
1847 /* By now the buffer is marked for journaling */
1848 offset += le16_to_cpu(de->rec_len);
1849 if (le64_to_cpu(de->inode)) {
1850 de1 = (struct ocfs2_dir_entry *)((char *) de +
1851 OCFS2_DIR_REC_LEN(de->name_len));
1853 cpu_to_le16(le16_to_cpu(de->rec_len) -
1854 OCFS2_DIR_REC_LEN(de->name_len));
1855 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1858 de->file_type = OCFS2_FT_UNKNOWN;
1860 de->inode = cpu_to_le64(blkno);
1861 ocfs2_set_de_type(de, inode->i_mode);
1864 de->name_len = namelen;
1865 memcpy(de->name, name, namelen);
1868 status = ocfs2_journal_dirty(handle, insert_bh);
1872 offset += le16_to_cpu(de->rec_len);
1873 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
1876 /* when you think about it, the assert above should prevent us
1877 * from ever getting here. */
1887 * ocfs2_delete_entry deletes a directory entry by merging it with the
1890 static int ocfs2_delete_entry(handle_t *handle,
1892 struct ocfs2_dir_entry *de_del,
1893 struct buffer_head *bh)
1895 struct ocfs2_dir_entry *de, *pde;
1896 int i, status = -ENOENT;
1898 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
1902 de = (struct ocfs2_dir_entry *) bh->b_data;
1903 while (i < bh->b_size) {
1904 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
1910 status = ocfs2_journal_access(handle, dir, bh,
1911 OCFS2_JOURNAL_ACCESS_WRITE);
1919 cpu_to_le16(le16_to_cpu(pde->rec_len) +
1920 le16_to_cpu(de->rec_len));
1924 status = ocfs2_journal_dirty(handle, bh);
1927 i += le16_to_cpu(de->rec_len);
1929 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
1937 * Returns 0 if not found, -1 on failure, and 1 on success
1939 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
1941 const char *name, int namelen,
1942 unsigned long offset,
1943 struct ocfs2_dir_entry **res_dir)
1945 struct ocfs2_dir_entry *de;
1946 char *dlimit, *de_buf;
1952 de_buf = bh->b_data;
1953 dlimit = de_buf + dir->i_sb->s_blocksize;
1955 while (de_buf < dlimit) {
1956 /* this code is executed quadratically often */
1957 /* do minimal checking `by hand' */
1959 de = (struct ocfs2_dir_entry *) de_buf;
1961 if (de_buf + namelen <= dlimit &&
1962 ocfs2_match(namelen, name, de)) {
1963 /* found a match - just to be sure, do a full check */
1964 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1973 /* prevent looping on a bad block */
1974 de_len = le16_to_cpu(de->rec_len);
1989 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
1991 struct ocfs2_dir_entry **res_dir)
1993 struct super_block *sb;
1994 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1995 struct buffer_head *bh, *ret = NULL;
1996 unsigned long start, block, b;
1997 int ra_max = 0; /* Number of bh's in the readahead
1999 int ra_ptr = 0; /* Current index into readahead
2002 int nblocks, i, err;
2009 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
2010 start = OCFS2_I(dir)->ip_dir_start_lookup;
2011 if (start >= nblocks)
2018 * We deal with the read-ahead logic here.
2020 if (ra_ptr >= ra_max) {
2021 /* Refill the readahead buffer */
2024 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
2026 * Terminate if we reach the end of the
2027 * directory and must wrap, or if our
2028 * search has finished at this block.
2030 if (b >= nblocks || (num && block == start)) {
2031 bh_use[ra_max] = NULL;
2036 bh = ocfs2_bread(dir, b++, &err, 1);
2037 bh_use[ra_max] = bh;
2040 if ((bh = bh_use[ra_ptr++]) == NULL)
2043 if (!buffer_uptodate(bh)) {
2044 /* read error, skip block & hope for the best */
2045 ocfs2_error(dir->i_sb, "reading directory %llu, "
2047 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2052 i = ocfs2_search_dirblock(bh, dir, name, namelen,
2053 block << sb->s_blocksize_bits,
2056 OCFS2_I(dir)->ip_dir_start_lookup = block;
2058 goto cleanup_and_exit;
2062 goto cleanup_and_exit;
2065 if (++block >= nblocks)
2067 } while (block != start);
2070 * If the directory has grown while we were searching, then
2071 * search the last part of the directory before giving up.
2074 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
2075 if (block < nblocks) {
2081 /* Clean up the read-ahead blocks */
2082 for (; ra_ptr < ra_max; ra_ptr++)
2083 brelse(bh_use[ra_ptr]);
2089 static int ocfs2_blkno_stringify(u64 blkno, char *name)
2091 int status, namelen;
2095 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
2105 if (namelen != OCFS2_ORPHAN_NAMELEN) {
2111 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
2120 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
2121 struct inode **ret_orphan_dir,
2122 struct inode *inode,
2124 struct buffer_head **de_bh)
2126 struct inode *orphan_dir_inode;
2127 struct buffer_head *orphan_dir_bh = NULL;
2130 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2136 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2137 ORPHAN_DIR_SYSTEM_INODE,
2139 if (!orphan_dir_inode) {
2145 mutex_lock(&orphan_dir_inode->i_mutex);
2147 status = ocfs2_meta_lock(orphan_dir_inode, &orphan_dir_bh, 1);
2153 status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
2154 orphan_dir_bh, name,
2155 OCFS2_ORPHAN_NAMELEN, de_bh);
2157 ocfs2_meta_unlock(orphan_dir_inode, 1);
2163 *ret_orphan_dir = orphan_dir_inode;
2167 mutex_unlock(&orphan_dir_inode->i_mutex);
2168 iput(orphan_dir_inode);
2172 brelse(orphan_dir_bh);
2178 static int ocfs2_orphan_add(struct ocfs2_super *osb,
2180 struct inode *inode,
2181 struct ocfs2_dinode *fe,
2183 struct buffer_head *de_bh,
2184 struct inode *orphan_dir_inode)
2186 struct buffer_head *orphan_dir_bh = NULL;
2188 struct ocfs2_dinode *orphan_fe;
2190 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
2192 status = ocfs2_read_block(osb,
2193 OCFS2_I(orphan_dir_inode)->ip_blkno,
2194 &orphan_dir_bh, OCFS2_BH_CACHED,
2201 status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
2202 OCFS2_JOURNAL_ACCESS_WRITE);
2208 /* we're a cluster, and nlink can change on disk from
2209 * underneath us... */
2210 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2211 if (S_ISDIR(inode->i_mode))
2212 le16_add_cpu(&orphan_fe->i_links_count, 1);
2213 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2215 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2221 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
2222 OCFS2_ORPHAN_NAMELEN, inode,
2223 OCFS2_I(inode)->ip_blkno,
2224 orphan_dir_bh, de_bh);
2230 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
2232 /* Record which orphan dir our inode now resides
2233 * in. delete_inode will use this to determine which orphan
2235 spin_lock(&OCFS2_I(inode)->ip_lock);
2236 OCFS2_I(inode)->ip_orphaned_slot = osb->slot_num;
2237 spin_unlock(&OCFS2_I(inode)->ip_lock);
2239 mlog(0, "Inode %llu orphaned in slot %d\n",
2240 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
2244 brelse(orphan_dir_bh);
2250 /* unlike orphan_add, we expect the orphan dir to already be locked here. */
2251 int ocfs2_orphan_del(struct ocfs2_super *osb,
2253 struct inode *orphan_dir_inode,
2254 struct inode *inode,
2255 struct buffer_head *orphan_dir_bh)
2257 char name[OCFS2_ORPHAN_NAMELEN + 1];
2258 struct ocfs2_dinode *orphan_fe;
2260 struct buffer_head *target_de_bh = NULL;
2261 struct ocfs2_dir_entry *target_de = NULL;
2265 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2271 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
2272 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
2273 OCFS2_ORPHAN_NAMELEN);
2275 /* find it's spot in the orphan directory */
2276 target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
2277 orphan_dir_inode, &target_de);
2278 if (!target_de_bh) {
2284 /* remove it from the orphan directory */
2285 status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
2292 status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
2293 OCFS2_JOURNAL_ACCESS_WRITE);
2299 /* do the i_nlink dance! :) */
2300 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2301 if (S_ISDIR(inode->i_mode))
2302 le16_add_cpu(&orphan_fe->i_links_count, -1);
2303 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2305 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2313 brelse(target_de_bh);
2319 struct inode_operations ocfs2_dir_iops = {
2320 .create = ocfs2_create,
2321 .lookup = ocfs2_lookup,
2323 .unlink = ocfs2_unlink,
2324 .rmdir = ocfs2_unlink,
2325 .symlink = ocfs2_symlink,
2326 .mkdir = ocfs2_mkdir,
2327 .mknod = ocfs2_mknod,
2328 .rename = ocfs2_rename,
2329 .setattr = ocfs2_setattr,
2330 .getattr = ocfs2_getattr,
2331 .sync_flags = ocfs2_sync_flags,
2332 .permission = ocfs2_permission,