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"
64 #include "buffer_head_io.h"
66 #define NAMEI_RA_CHUNKS 2
67 #define NAMEI_RA_BLOCKS 4
68 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
69 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
71 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
73 const char *name, int namelen,
75 struct ocfs2_dir_entry **res_dir);
77 static int ocfs2_delete_entry(struct ocfs2_journal_handle *handle,
79 struct ocfs2_dir_entry *de_del,
80 struct buffer_head *bh);
82 static int __ocfs2_add_entry(struct ocfs2_journal_handle *handle,
84 const char *name, int namelen,
85 struct inode *inode, u64 blkno,
86 struct buffer_head *parent_fe_bh,
87 struct buffer_head *insert_bh);
89 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
91 struct dentry *dentry, int mode,
93 struct buffer_head **new_fe_bh,
94 struct buffer_head *parent_fe_bh,
95 struct ocfs2_journal_handle *handle,
96 struct inode **ret_inode,
97 struct ocfs2_alloc_context *inode_ac);
99 static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
100 struct ocfs2_journal_handle *handle,
101 struct inode *parent,
103 struct buffer_head *fe_bh,
104 struct ocfs2_alloc_context *data_ac);
106 static int ocfs2_double_lock(struct ocfs2_super *osb,
107 struct ocfs2_journal_handle *handle,
108 struct buffer_head **bh1,
109 struct inode *inode1,
110 struct buffer_head **bh2,
111 struct inode *inode2);
113 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
114 struct ocfs2_journal_handle *handle,
117 struct buffer_head **de_bh);
119 static int ocfs2_orphan_add(struct ocfs2_super *osb,
120 struct ocfs2_journal_handle *handle,
122 struct ocfs2_dinode *fe,
124 struct buffer_head *de_bh);
126 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
127 struct ocfs2_journal_handle *handle,
129 const char *symname);
131 static inline int ocfs2_add_entry(struct ocfs2_journal_handle *handle,
132 struct dentry *dentry,
133 struct inode *inode, u64 blkno,
134 struct buffer_head *parent_fe_bh,
135 struct buffer_head *insert_bh)
137 return __ocfs2_add_entry(handle, dentry->d_parent->d_inode,
138 dentry->d_name.name, dentry->d_name.len,
139 inode, blkno, parent_fe_bh, insert_bh);
142 /* An orphan dir name is an 8 byte value, printed as a hex string */
143 #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
145 static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
146 struct nameidata *nd)
150 struct buffer_head *dirent_bh = NULL;
151 struct inode *inode = NULL;
153 struct ocfs2_dir_entry *dirent;
154 struct ocfs2_inode_info *oi;
156 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
157 dentry->d_name.len, dentry->d_name.name);
159 if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
160 ret = ERR_PTR(-ENAMETOOLONG);
164 mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
165 dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
167 status = ocfs2_meta_lock(dir, NULL, NULL, 0);
169 if (status != -ENOENT)
171 ret = ERR_PTR(status);
175 status = ocfs2_find_files_on_disk(dentry->d_name.name,
176 dentry->d_name.len, &blkno,
177 dir, &dirent_bh, &dirent);
181 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno);
183 mlog(ML_ERROR, "Unable to create inode %llu\n",
184 (unsigned long long)blkno);
185 ret = ERR_PTR(-EACCES);
190 /* Clear any orphaned state... If we were able to look up the
191 * inode from a directory, it certainly can't be orphaned. We
192 * might have the bad state from a node which intended to
193 * orphan this inode but crashed before it could commit the
195 spin_lock(&oi->ip_lock);
196 oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
197 oi->ip_orphaned_slot = OCFS2_INVALID_SLOT;
198 spin_unlock(&oi->ip_lock);
202 dentry->d_op = &ocfs2_dentry_ops;
203 ret = d_splice_alias(inode, dentry);
206 /* Don't drop the cluster lock until *after* the d_add --
207 * unlink on another node will message us to remove that
208 * dentry under this lock so otherwise we can race this with
209 * the vote thread and have a stale dentry. */
210 ocfs2_meta_unlock(dir, 0);
221 static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
222 struct ocfs2_journal_handle *handle,
223 struct inode *parent,
225 struct buffer_head *fe_bh,
226 struct ocfs2_alloc_context *data_ac)
229 struct buffer_head *new_bh = NULL;
230 struct ocfs2_dir_entry *de = NULL;
234 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
235 data_ac, NULL, &new_bh);
241 ocfs2_set_new_buffer_uptodate(inode, new_bh);
243 status = ocfs2_journal_access(handle, inode, new_bh,
244 OCFS2_JOURNAL_ACCESS_CREATE);
249 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
251 de = (struct ocfs2_dir_entry *) new_bh->b_data;
252 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
255 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
256 strcpy(de->name, ".");
257 ocfs2_set_de_type(de, S_IFDIR);
258 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
259 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
260 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
261 OCFS2_DIR_REC_LEN(1));
263 strcpy(de->name, "..");
264 ocfs2_set_de_type(de, S_IFDIR);
266 status = ocfs2_journal_dirty(handle, new_bh);
272 i_size_write(inode, inode->i_sb->s_blocksize);
274 inode->i_blocks = ocfs2_align_bytes_to_sectors(inode->i_sb->s_blocksize);
275 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
290 static int ocfs2_mknod(struct inode *dir,
291 struct dentry *dentry,
296 struct buffer_head *parent_fe_bh = NULL;
297 struct ocfs2_journal_handle *handle = NULL;
298 struct ocfs2_super *osb;
299 struct ocfs2_dinode *dirfe;
300 struct buffer_head *new_fe_bh = NULL;
301 struct buffer_head *de_bh = NULL;
302 struct inode *inode = NULL;
303 struct ocfs2_alloc_context *inode_ac = NULL;
304 struct ocfs2_alloc_context *data_ac = NULL;
306 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
307 (unsigned long)dev, dentry->d_name.len,
308 dentry->d_name.name);
310 /* get our super block */
311 osb = OCFS2_SB(dir->i_sb);
313 handle = ocfs2_alloc_handle(osb);
314 if (handle == NULL) {
320 status = ocfs2_meta_lock(dir, handle, &parent_fe_bh, 1);
322 if (status != -ENOENT)
327 if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
332 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
333 if (!dirfe->i_links_count) {
334 /* can't make a file in a deleted directory. */
339 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
344 /* get a spot inside the dir. */
345 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
347 dentry->d_name.len, &de_bh);
353 /* reserve an inode spot */
354 status = ocfs2_reserve_new_inode(osb, handle, &inode_ac);
356 if (status != -ENOSPC)
361 /* are we making a directory? If so, reserve a cluster for his
364 status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
366 if (status != -ENOSPC)
372 handle = ocfs2_start_trans(osb, handle, OCFS2_MKNOD_CREDITS);
373 if (IS_ERR(handle)) {
374 status = PTR_ERR(handle);
380 /* do the real work now. */
381 status = ocfs2_mknod_locked(osb, dir, dentry, mode, dev,
382 &new_fe_bh, parent_fe_bh, handle,
390 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
397 status = ocfs2_journal_access(handle, dir, parent_fe_bh,
398 OCFS2_JOURNAL_ACCESS_WRITE);
403 le16_add_cpu(&dirfe->i_links_count, 1);
404 status = ocfs2_journal_dirty(handle, parent_fe_bh);
412 status = ocfs2_add_entry(handle, dentry, inode,
413 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
420 insert_inode_hash(inode);
421 dentry->d_op = &ocfs2_dentry_ops;
422 d_instantiate(dentry, inode);
426 ocfs2_commit_trans(handle);
428 if (status == -ENOSPC)
429 mlog(0, "Disk is full\n");
438 brelse(parent_fe_bh);
440 if ((status < 0) && inode)
444 ocfs2_free_alloc_context(inode_ac);
447 ocfs2_free_alloc_context(data_ac);
454 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
456 struct dentry *dentry, int mode,
458 struct buffer_head **new_fe_bh,
459 struct buffer_head *parent_fe_bh,
460 struct ocfs2_journal_handle *handle,
461 struct inode **ret_inode,
462 struct ocfs2_alloc_context *inode_ac)
465 struct ocfs2_dinode *fe = NULL;
466 struct ocfs2_extent_list *fel;
469 struct inode *inode = NULL;
471 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
472 (unsigned long)dev, dentry->d_name.len,
473 dentry->d_name.name);
478 status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
485 inode = new_inode(dir->i_sb);
487 status = PTR_ERR(inode);
488 mlog(ML_ERROR, "new_inode failed!\n");
492 /* populate as many fields early on as possible - many of
493 * these are used by the support functions here and in
495 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
496 OCFS2_I(inode)->ip_blkno = fe_blkno;
501 inode->i_mode = mode;
502 spin_lock(&osb->osb_lock);
503 inode->i_generation = osb->s_next_generation++;
504 spin_unlock(&osb->osb_lock);
506 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
512 ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
514 status = ocfs2_journal_access(handle, inode, *new_fe_bh,
515 OCFS2_JOURNAL_ACCESS_CREATE);
521 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
522 memset(fe, 0, osb->sb->s_blocksize);
524 fe->i_generation = cpu_to_le32(inode->i_generation);
525 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
526 fe->i_blkno = cpu_to_le64(fe_blkno);
527 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
528 fe->i_suballoc_slot = cpu_to_le16(osb->slot_num);
529 fe->i_uid = cpu_to_le32(current->fsuid);
530 if (dir->i_mode & S_ISGID) {
531 fe->i_gid = cpu_to_le32(dir->i_gid);
535 fe->i_gid = cpu_to_le32(current->fsgid);
536 fe->i_mode = cpu_to_le16(mode);
537 if (S_ISCHR(mode) || S_ISBLK(mode))
538 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
540 fe->i_links_count = cpu_to_le16(inode->i_nlink);
542 fe->i_last_eb_blk = 0;
543 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
544 le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
545 fe->i_atime = fe->i_ctime = fe->i_mtime =
546 cpu_to_le64(CURRENT_TIME.tv_sec);
547 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
548 cpu_to_le32(CURRENT_TIME.tv_nsec);
551 fel = &fe->id2.i_list;
552 fel->l_tree_depth = 0;
553 fel->l_next_free_rec = 0;
554 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
556 status = ocfs2_journal_dirty(handle, *new_fe_bh);
562 if (ocfs2_populate_inode(inode, fe, 1) < 0) {
563 mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
564 "i_blkno=%llu, i_ino=%lu\n",
565 (unsigned long long) (*new_fe_bh)->b_blocknr,
566 (unsigned long long)fe->i_blkno, inode->i_ino);
570 ocfs2_inode_set_new(osb, inode);
571 status = ocfs2_create_new_inode_locks(inode);
575 status = 0; /* error in ocfs2_create_new_inode_locks is not
593 static int ocfs2_mkdir(struct inode *dir,
594 struct dentry *dentry,
599 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
600 dentry->d_name.len, dentry->d_name.name);
601 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
607 static int ocfs2_create(struct inode *dir,
608 struct dentry *dentry,
610 struct nameidata *nd)
614 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
615 dentry->d_name.len, dentry->d_name.name);
616 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
622 static int ocfs2_link(struct dentry *old_dentry,
624 struct dentry *dentry)
626 struct ocfs2_journal_handle *handle = NULL;
627 struct inode *inode = old_dentry->d_inode;
629 struct buffer_head *fe_bh = NULL;
630 struct buffer_head *parent_fe_bh = NULL;
631 struct buffer_head *de_bh = NULL;
632 struct ocfs2_dinode *fe = NULL;
633 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
635 mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
636 old_dentry->d_name.len, old_dentry->d_name.name,
637 dentry->d_name.len, dentry->d_name.name);
639 if (S_ISDIR(inode->i_mode)) {
644 if (inode->i_nlink >= OCFS2_LINK_MAX) {
649 handle = ocfs2_alloc_handle(osb);
650 if (handle == NULL) {
655 err = ocfs2_meta_lock(dir, handle, &parent_fe_bh, 1);
662 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
667 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
669 dentry->d_name.len, &de_bh);
675 err = ocfs2_meta_lock(inode, handle, &fe_bh, 1);
682 fe = (struct ocfs2_dinode *) fe_bh->b_data;
683 if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
688 handle = ocfs2_start_trans(osb, handle, OCFS2_LINK_CREDITS);
689 if (IS_ERR(handle)) {
690 err = PTR_ERR(handle);
696 err = ocfs2_journal_access(handle, inode, fe_bh,
697 OCFS2_JOURNAL_ACCESS_WRITE);
704 inode->i_ctime = CURRENT_TIME;
705 fe->i_links_count = cpu_to_le16(inode->i_nlink);
706 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
707 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
709 err = ocfs2_journal_dirty(handle, fe_bh);
711 le16_add_cpu(&fe->i_links_count, -1);
717 err = ocfs2_add_entry(handle, dentry, inode,
718 OCFS2_I(inode)->ip_blkno,
719 parent_fe_bh, de_bh);
721 le16_add_cpu(&fe->i_links_count, -1);
727 atomic_inc(&inode->i_count);
728 dentry->d_op = &ocfs2_dentry_ops;
729 d_instantiate(dentry, inode);
732 ocfs2_commit_trans(handle);
738 brelse(parent_fe_bh);
745 static int ocfs2_unlink(struct inode *dir,
746 struct dentry *dentry)
749 unsigned int saved_nlink = 0;
750 struct inode *inode = dentry->d_inode;
751 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
753 struct ocfs2_dinode *fe = NULL;
754 struct buffer_head *fe_bh = NULL;
755 struct buffer_head *parent_node_bh = NULL;
756 struct ocfs2_journal_handle *handle = NULL;
757 struct ocfs2_dir_entry *dirent = NULL;
758 struct buffer_head *dirent_bh = NULL;
759 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
760 struct buffer_head *orphan_entry_bh = NULL;
762 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
763 dentry->d_name.len, dentry->d_name.name);
765 BUG_ON(dentry->d_parent->d_inode != dir);
767 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
769 if (inode == osb->root_inode) {
770 mlog(0, "Cannot delete the root directory\n");
775 handle = ocfs2_alloc_handle(osb);
776 if (handle == NULL) {
782 status = ocfs2_meta_lock(dir, handle, &parent_node_bh, 1);
784 if (status != -ENOENT)
789 status = ocfs2_find_files_on_disk(dentry->d_name.name,
790 dentry->d_name.len, &blkno,
791 dir, &dirent_bh, &dirent);
793 if (status != -ENOENT)
798 if (OCFS2_I(inode)->ip_blkno != blkno) {
801 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
802 (unsigned long long)OCFS2_I(inode)->ip_blkno,
803 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
807 status = ocfs2_meta_lock(inode, handle, &fe_bh, 1);
809 if (status != -ENOENT)
814 if (S_ISDIR(inode->i_mode)) {
815 if (!ocfs2_empty_dir(inode)) {
818 } else if (inode->i_nlink != 2) {
824 /* There are still a few steps left until we can consider the
825 * unlink to have succeeded. Save off nlink here before
826 * modification so we can set it back in case we hit an issue
828 saved_nlink = inode->i_nlink;
829 if (S_ISDIR(inode->i_mode))
834 status = ocfs2_request_unlink_vote(inode, dentry,
835 (unsigned int) inode->i_nlink);
837 /* This vote should succeed under all normal
843 if (!inode->i_nlink) {
844 status = ocfs2_prepare_orphan_dir(osb, handle, inode,
853 handle = ocfs2_start_trans(osb, handle, OCFS2_UNLINK_CREDITS);
854 if (IS_ERR(handle)) {
855 status = PTR_ERR(handle);
861 status = ocfs2_journal_access(handle, inode, fe_bh,
862 OCFS2_JOURNAL_ACCESS_WRITE);
868 fe = (struct ocfs2_dinode *) fe_bh->b_data;
870 if (!inode->i_nlink) {
871 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
879 /* delete the name from the parent dir */
880 status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
886 /* We can set nlink on the dinode now. clear the saved version
887 * so that it doesn't get set later. */
888 fe->i_links_count = cpu_to_le16(inode->i_nlink);
891 status = ocfs2_journal_dirty(handle, fe_bh);
897 if (S_ISDIR(inode->i_mode)) {
899 status = ocfs2_mark_inode_dirty(handle, dir,
908 if (status < 0 && saved_nlink)
909 inode->i_nlink = saved_nlink;
912 ocfs2_commit_trans(handle);
921 brelse(parent_node_bh);
924 brelse(orphan_entry_bh);
932 * The only place this should be used is rename!
933 * if they have the same id, then the 1st one is the only one locked.
935 static int ocfs2_double_lock(struct ocfs2_super *osb,
936 struct ocfs2_journal_handle *handle,
937 struct buffer_head **bh1,
938 struct inode *inode1,
939 struct buffer_head **bh2,
940 struct inode *inode2)
943 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
944 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
945 struct buffer_head **tmpbh;
946 struct inode *tmpinode;
948 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
949 (unsigned long long)oi1->ip_blkno,
950 (unsigned long long)oi2->ip_blkno);
959 /* we always want to lock the one with the lower lockid first. */
960 if (oi1->ip_blkno != oi2->ip_blkno) {
961 if (oi1->ip_blkno < oi2->ip_blkno) {
962 /* switch id1 and id2 around */
963 mlog(0, "switching them around...\n");
973 status = ocfs2_meta_lock(inode2, handle, bh2, 1);
975 if (status != -ENOENT)
981 status = ocfs2_meta_lock(inode1, handle, bh1, 1);
983 if (status != -ENOENT)
992 #define PARENT_INO(buffer) \
993 ((struct ocfs2_dir_entry *) \
995 le16_to_cpu(((struct ocfs2_dir_entry *)buffer)->rec_len)))->inode
997 static int ocfs2_rename(struct inode *old_dir,
998 struct dentry *old_dentry,
999 struct inode *new_dir,
1000 struct dentry *new_dentry)
1002 int status = 0, rename_lock = 0;
1003 struct inode *old_inode = old_dentry->d_inode;
1004 struct inode *new_inode = new_dentry->d_inode;
1005 struct ocfs2_dinode *newfe = NULL;
1006 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
1007 struct buffer_head *orphan_entry_bh = NULL;
1008 struct buffer_head *newfe_bh = NULL;
1009 struct buffer_head *insert_entry_bh = NULL;
1010 struct ocfs2_super *osb = NULL;
1012 struct ocfs2_journal_handle *handle = NULL;
1013 struct buffer_head *old_dir_bh = NULL;
1014 struct buffer_head *new_dir_bh = NULL;
1015 struct ocfs2_dir_entry *old_de = NULL, *new_de = NULL; // dirent for old_dentry
1017 struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
1018 struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
1019 // this is the 1st dirent bh
1020 nlink_t old_dir_nlink = old_dir->i_nlink, new_dir_nlink = new_dir->i_nlink;
1021 unsigned int links_count;
1023 /* At some point it might be nice to break this function up a
1026 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
1027 old_dir, old_dentry, new_dir, new_dentry,
1028 old_dentry->d_name.len, old_dentry->d_name.name,
1029 new_dentry->d_name.len, new_dentry->d_name.name);
1031 osb = OCFS2_SB(old_dir->i_sb);
1034 if (!igrab(new_inode))
1038 if (atomic_read(&old_dentry->d_count) > 2) {
1039 shrink_dcache_parent(old_dentry);
1040 if (atomic_read(&old_dentry->d_count) > 2) {
1046 /* Assume a directory heirarchy thusly:
1049 * a,b,c, and d are all directories.
1051 * from cwd of 'a' on both nodes:
1055 * And that's why, just like the VFS, we need a file system
1057 if (old_dentry != new_dentry) {
1058 status = ocfs2_rename_lock(osb);
1066 handle = ocfs2_alloc_handle(osb);
1067 if (handle == NULL) {
1073 /* if old and new are the same, this'll just do one lock. */
1074 status = ocfs2_double_lock(osb, handle,
1075 &old_dir_bh, old_dir,
1076 &new_dir_bh, new_dir);
1082 /* make sure both dirs have bhs
1083 * get an extra ref on old_dir_bh if old==new */
1086 new_dir_bh = old_dir_bh;
1089 mlog(ML_ERROR, "no old_dir_bh!\n");
1095 if (S_ISDIR(old_inode->i_mode)) {
1096 /* Directories actually require metadata updates to
1097 * the directory info so we can't get away with not
1098 * doing node locking on it. */
1099 status = ocfs2_meta_lock(old_inode, handle, NULL, 1);
1101 if (status != -ENOENT)
1106 status = ocfs2_request_rename_vote(old_inode, old_dentry);
1113 old_inode_de_bh = ocfs2_bread(old_inode, 0, &status, 0);
1114 if (!old_inode_de_bh)
1118 if (le64_to_cpu(PARENT_INO(old_inode_de_bh->b_data)) !=
1119 OCFS2_I(old_dir)->ip_blkno)
1122 if (!new_inode && new_dir!=old_dir &&
1123 new_dir->i_nlink >= OCFS2_LINK_MAX)
1126 /* Ah, the simple case - we're a file so just send a
1128 status = ocfs2_request_rename_vote(old_inode, old_dentry);
1136 old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
1137 old_dentry->d_name.len,
1143 * Check for inode number is _not_ due to possible IO errors.
1144 * We might rmdir the source, keep it as pwd of some process
1145 * and merrily kill the link to whatever was created under the
1146 * same name. Goodbye sticky bit ;-<
1148 if (le64_to_cpu(old_de->inode) != OCFS2_I(old_inode)->ip_blkno)
1151 /* check if the target already exists (in which case we need
1153 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1154 new_dentry->d_name.len,
1155 &newfe_blkno, new_dir, &new_de_bh,
1157 /* The only error we allow here is -ENOENT because the new
1158 * file not existing is perfectly valid. */
1159 if ((status < 0) && (status != -ENOENT)) {
1160 /* If we cannot find the file specified we should just */
1161 /* return the error... */
1166 if (!new_de && new_inode)
1167 mlog(ML_ERROR, "inode %lu does not exist in it's parent "
1168 "directory!", new_inode->i_ino);
1170 /* In case we need to overwrite an existing file, we blow it
1173 /* VFS didn't think there existed an inode here, but
1174 * someone else in the cluster must have raced our
1175 * rename to create one. Today we error cleanly, in
1176 * the future we should consider calling iget to build
1177 * a new struct inode for this entry. */
1181 mlog(0, "We found an inode for name %.*s but VFS "
1182 "didn't give us one.\n", new_dentry->d_name.len,
1183 new_dentry->d_name.name);
1187 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1190 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1191 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1192 (unsigned long long)newfe_blkno,
1193 OCFS2_I(new_inode)->ip_flags);
1197 status = ocfs2_meta_lock(new_inode, handle, &newfe_bh, 1);
1199 if (status != -ENOENT)
1204 if (S_ISDIR(new_inode->i_mode))
1207 links_count = (unsigned int) (new_inode->i_nlink - 1);
1209 status = ocfs2_request_unlink_vote(new_inode, new_dentry,
1216 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1218 mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
1219 "newfebh=%p bhblocknr=%llu\n", new_de,
1220 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
1221 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1223 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
1224 status = ocfs2_prepare_orphan_dir(osb, handle,
1234 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1236 status = ocfs2_check_dir_for_entry(new_dir,
1237 new_dentry->d_name.name,
1238 new_dentry->d_name.len);
1242 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1243 new_dentry->d_name.name,
1244 new_dentry->d_name.len,
1252 handle = ocfs2_start_trans(osb, handle, OCFS2_RENAME_CREDITS);
1253 if (IS_ERR(handle)) {
1254 status = PTR_ERR(handle);
1261 if (S_ISDIR(new_inode->i_mode)) {
1262 if (!ocfs2_empty_dir(new_inode) ||
1263 new_inode->i_nlink != 2) {
1264 status = -ENOTEMPTY;
1268 status = ocfs2_journal_access(handle, new_inode, newfe_bh,
1269 OCFS2_JOURNAL_ACCESS_WRITE);
1275 if (S_ISDIR(new_inode->i_mode) ||
1276 (newfe->i_links_count == cpu_to_le16(1))){
1277 status = ocfs2_orphan_add(osb, handle, new_inode,
1286 /* change the dirent to point to the correct inode */
1287 status = ocfs2_journal_access(handle, new_dir, new_de_bh,
1288 OCFS2_JOURNAL_ACCESS_WRITE);
1293 new_de->inode = cpu_to_le64(OCFS2_I(old_inode)->ip_blkno);
1294 new_de->file_type = old_de->file_type;
1295 new_dir->i_version++;
1296 status = ocfs2_journal_dirty(handle, new_de_bh);
1302 if (S_ISDIR(new_inode->i_mode))
1303 newfe->i_links_count = 0;
1305 le16_add_cpu(&newfe->i_links_count, -1);
1307 status = ocfs2_journal_dirty(handle, newfe_bh);
1313 /* if the name was not found in new_dir, add it now */
1314 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1315 OCFS2_I(old_inode)->ip_blkno,
1316 new_dir_bh, insert_entry_bh);
1319 old_inode->i_ctime = CURRENT_TIME;
1320 mark_inode_dirty(old_inode);
1322 /* now that the name has been added to new_dir, remove the old name */
1323 status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
1330 new_inode->i_nlink--;
1331 new_inode->i_ctime = CURRENT_TIME;
1333 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1334 if (old_inode_de_bh) {
1335 status = ocfs2_journal_access(handle, old_inode,
1337 OCFS2_JOURNAL_ACCESS_WRITE);
1338 PARENT_INO(old_inode_de_bh->b_data) =
1339 cpu_to_le64(OCFS2_I(new_dir)->ip_blkno);
1340 status = ocfs2_journal_dirty(handle, old_inode_de_bh);
1343 new_inode->i_nlink--;
1346 mark_inode_dirty(new_dir);
1349 mark_inode_dirty(old_dir);
1351 mark_inode_dirty(new_inode);
1353 if (old_dir != new_dir)
1354 if (new_dir_nlink != new_dir->i_nlink) {
1356 mlog(ML_ERROR, "need to change nlink for new "
1357 "dir %llu from %d to %d but bh is NULL\n",
1358 (unsigned long long)OCFS2_I(new_dir)->ip_blkno,
1359 (int)new_dir_nlink, new_dir->i_nlink);
1361 struct ocfs2_dinode *fe;
1362 status = ocfs2_journal_access(handle,
1365 OCFS2_JOURNAL_ACCESS_WRITE);
1366 fe = (struct ocfs2_dinode *) new_dir_bh->b_data;
1367 fe->i_links_count = cpu_to_le16(new_dir->i_nlink);
1368 status = ocfs2_journal_dirty(handle, new_dir_bh);
1372 if (old_dir_nlink != old_dir->i_nlink) {
1374 mlog(ML_ERROR, "need to change nlink for old dir "
1375 "%llu from %d to %d but bh is NULL!\n",
1376 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1377 (int)old_dir_nlink, old_dir->i_nlink);
1379 struct ocfs2_dinode *fe;
1380 status = ocfs2_journal_access(handle, old_dir,
1382 OCFS2_JOURNAL_ACCESS_WRITE);
1383 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1384 fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
1385 status = ocfs2_journal_dirty(handle, old_dir_bh);
1392 ocfs2_rename_unlock(osb);
1395 ocfs2_commit_trans(handle);
1398 sync_mapping_buffers(old_inode->i_mapping);
1412 if (old_inode_de_bh)
1413 brelse(old_inode_de_bh);
1414 if (orphan_entry_bh)
1415 brelse(orphan_entry_bh);
1416 if (insert_entry_bh)
1417 brelse(insert_entry_bh);
1425 * we expect i_size = strlen(symname). Copy symname into the file
1426 * data, including the null terminator.
1428 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1429 struct ocfs2_journal_handle *handle,
1430 struct inode *inode,
1431 const char *symname)
1433 struct buffer_head **bhs = NULL;
1435 struct super_block *sb = osb->sb;
1438 int virtual, blocks, status, i, bytes_left;
1440 bytes_left = i_size_read(inode) + 1;
1441 /* we can't trust i_blocks because we're actually going to
1442 * write i_size + 1 bytes. */
1443 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1445 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1446 (unsigned long long)inode->i_blocks,
1447 i_size_read(inode), blocks);
1449 /* Sanity check -- make sure we're going to fit. */
1451 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1457 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1464 status = ocfs2_extent_map_get_blocks(inode, 0, 1, &p_blkno,
1471 /* links can never be larger than one cluster so we know this
1472 * is all going to be contiguous, but do a sanity check
1474 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1481 while(bytes_left > 0) {
1482 c = &symname[virtual * sb->s_blocksize];
1484 bhs[virtual] = sb_getblk(sb, p_blkno);
1485 if (!bhs[virtual]) {
1490 ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
1492 status = ocfs2_journal_access(handle, inode, bhs[virtual],
1493 OCFS2_JOURNAL_ACCESS_CREATE);
1499 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1501 memcpy(bhs[virtual]->b_data, c,
1502 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1505 status = ocfs2_journal_dirty(handle, bhs[virtual]);
1513 bytes_left -= sb->s_blocksize;
1520 for(i = 0; i < blocks; i++)
1530 static int ocfs2_symlink(struct inode *dir,
1531 struct dentry *dentry,
1532 const char *symname)
1534 int status, l, credits;
1536 struct ocfs2_super *osb = NULL;
1537 struct inode *inode = NULL;
1538 struct super_block *sb;
1539 struct buffer_head *new_fe_bh = NULL;
1540 struct buffer_head *de_bh = NULL;
1541 struct buffer_head *parent_fe_bh = NULL;
1542 struct ocfs2_dinode *fe = NULL;
1543 struct ocfs2_dinode *dirfe;
1544 struct ocfs2_journal_handle *handle = NULL;
1545 struct ocfs2_alloc_context *inode_ac = NULL;
1546 struct ocfs2_alloc_context *data_ac = NULL;
1548 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1549 dentry, symname, dentry->d_name.len, dentry->d_name.name);
1554 l = strlen(symname) + 1;
1556 credits = ocfs2_calc_symlink_credits(sb);
1558 handle = ocfs2_alloc_handle(osb);
1559 if (handle == NULL) {
1565 /* lock the parent directory */
1566 status = ocfs2_meta_lock(dir, handle, &parent_fe_bh, 1);
1568 if (status != -ENOENT)
1573 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1574 if (!dirfe->i_links_count) {
1575 /* can't make a file in a deleted directory. */
1580 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1581 dentry->d_name.len);
1585 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1586 dentry->d_name.name,
1587 dentry->d_name.len, &de_bh);
1593 status = ocfs2_reserve_new_inode(osb, handle, &inode_ac);
1595 if (status != -ENOSPC)
1600 /* don't reserve bitmap space for fast symlinks. */
1601 if (l > ocfs2_fast_symlink_chars(sb)) {
1602 status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
1604 if (status != -ENOSPC)
1610 handle = ocfs2_start_trans(osb, handle, credits);
1611 if (IS_ERR(handle)) {
1612 status = PTR_ERR(handle);
1618 status = ocfs2_mknod_locked(osb, dir, dentry,
1619 S_IFLNK | S_IRWXUGO, 0,
1620 &new_fe_bh, parent_fe_bh, handle,
1627 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1630 if (l > ocfs2_fast_symlink_chars(sb)) {
1631 inode->i_op = &ocfs2_symlink_inode_operations;
1632 status = ocfs2_do_extend_allocation(osb, inode, 1, new_fe_bh,
1633 handle, data_ac, NULL,
1636 if (status != -ENOSPC && status != -EINTR) {
1638 "Failed to extend file to %llu\n",
1639 (unsigned long long)newsize);
1645 i_size_write(inode, newsize);
1646 inode->i_blocks = ocfs2_align_bytes_to_sectors(newsize);
1648 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1649 memcpy((char *) fe->id2.i_symlink, symname, l);
1650 i_size_write(inode, newsize);
1651 inode->i_blocks = 0;
1654 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1660 if (!ocfs2_inode_is_fast_symlink(inode)) {
1661 status = ocfs2_create_symlink_data(osb, handle, inode,
1669 status = ocfs2_add_entry(handle, dentry, inode,
1670 le64_to_cpu(fe->i_blkno), parent_fe_bh,
1677 insert_inode_hash(inode);
1678 dentry->d_op = &ocfs2_dentry_ops;
1679 d_instantiate(dentry, inode);
1682 ocfs2_commit_trans(handle);
1686 brelse(parent_fe_bh);
1690 ocfs2_free_alloc_context(inode_ac);
1692 ocfs2_free_alloc_context(data_ac);
1693 if ((status < 0) && inode)
1701 int ocfs2_check_dir_entry(struct inode * dir,
1702 struct ocfs2_dir_entry * de,
1703 struct buffer_head * bh,
1704 unsigned long offset)
1706 const char *error_msg = NULL;
1707 const int rlen = le16_to_cpu(de->rec_len);
1709 if (rlen < OCFS2_DIR_REC_LEN(1))
1710 error_msg = "rec_len is smaller than minimal";
1711 else if (rlen % 4 != 0)
1712 error_msg = "rec_len % 4 != 0";
1713 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
1714 error_msg = "rec_len is too small for name_len";
1715 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
1716 error_msg = "directory entry across blocks";
1718 if (error_msg != NULL)
1719 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
1720 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
1721 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
1722 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
1724 return error_msg == NULL ? 1 : 0;
1727 /* we don't always have a dentry for what we want to add, so people
1728 * like orphan dir can call this instead.
1730 * If you pass me insert_bh, I'll skip the search of the other dir
1731 * blocks and put the record in there.
1733 static int __ocfs2_add_entry(struct ocfs2_journal_handle *handle,
1735 const char *name, int namelen,
1736 struct inode *inode, u64 blkno,
1737 struct buffer_head *parent_fe_bh,
1738 struct buffer_head *insert_bh)
1740 unsigned long offset;
1741 unsigned short rec_len;
1742 struct ocfs2_dir_entry *de, *de1;
1743 struct super_block *sb;
1753 rec_len = OCFS2_DIR_REC_LEN(namelen);
1755 de = (struct ocfs2_dir_entry *) insert_bh->b_data;
1757 BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
1758 /* These checks should've already been passed by the
1759 * prepare function, but I guess we can leave them
1761 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
1765 if (ocfs2_match(namelen, name, de)) {
1769 if (((le64_to_cpu(de->inode) == 0) &&
1770 (le16_to_cpu(de->rec_len) >= rec_len)) ||
1771 (le16_to_cpu(de->rec_len) >=
1772 (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
1773 status = ocfs2_journal_access(handle, dir, insert_bh,
1774 OCFS2_JOURNAL_ACCESS_WRITE);
1775 /* By now the buffer is marked for journaling */
1776 offset += le16_to_cpu(de->rec_len);
1777 if (le64_to_cpu(de->inode)) {
1778 de1 = (struct ocfs2_dir_entry *)((char *) de +
1779 OCFS2_DIR_REC_LEN(de->name_len));
1781 cpu_to_le16(le16_to_cpu(de->rec_len) -
1782 OCFS2_DIR_REC_LEN(de->name_len));
1783 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1786 de->file_type = OCFS2_FT_UNKNOWN;
1788 de->inode = cpu_to_le64(blkno);
1789 ocfs2_set_de_type(de, inode->i_mode);
1792 de->name_len = namelen;
1793 memcpy(de->name, name, namelen);
1795 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1797 status = ocfs2_journal_dirty(handle, insert_bh);
1801 offset += le16_to_cpu(de->rec_len);
1802 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
1805 /* when you think about it, the assert above should prevent us
1806 * from ever getting here. */
1816 * ocfs2_delete_entry deletes a directory entry by merging it with the
1819 static int ocfs2_delete_entry(struct ocfs2_journal_handle *handle,
1821 struct ocfs2_dir_entry *de_del,
1822 struct buffer_head *bh)
1824 struct ocfs2_dir_entry *de, *pde;
1825 int i, status = -ENOENT;
1827 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
1831 de = (struct ocfs2_dir_entry *) bh->b_data;
1832 while (i < bh->b_size) {
1833 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
1839 status = ocfs2_journal_access(handle, dir, bh,
1840 OCFS2_JOURNAL_ACCESS_WRITE);
1848 cpu_to_le16(le16_to_cpu(pde->rec_len) +
1849 le16_to_cpu(de->rec_len));
1853 status = ocfs2_journal_dirty(handle, bh);
1856 i += le16_to_cpu(de->rec_len);
1858 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
1866 * Returns 0 if not found, -1 on failure, and 1 on success
1868 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
1870 const char *name, int namelen,
1871 unsigned long offset,
1872 struct ocfs2_dir_entry **res_dir)
1874 struct ocfs2_dir_entry *de;
1875 char *dlimit, *de_buf;
1881 de_buf = bh->b_data;
1882 dlimit = de_buf + dir->i_sb->s_blocksize;
1884 while (de_buf < dlimit) {
1885 /* this code is executed quadratically often */
1886 /* do minimal checking `by hand' */
1888 de = (struct ocfs2_dir_entry *) de_buf;
1890 if (de_buf + namelen <= dlimit &&
1891 ocfs2_match(namelen, name, de)) {
1892 /* found a match - just to be sure, do a full check */
1893 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1902 /* prevent looping on a bad block */
1903 de_len = le16_to_cpu(de->rec_len);
1918 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
1920 struct ocfs2_dir_entry **res_dir)
1922 struct super_block *sb;
1923 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1924 struct buffer_head *bh, *ret = NULL;
1925 unsigned long start, block, b;
1926 int ra_max = 0; /* Number of bh's in the readahead
1928 int ra_ptr = 0; /* Current index into readahead
1931 int nblocks, i, err;
1938 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
1939 start = OCFS2_I(dir)->ip_dir_start_lookup;
1940 if (start >= nblocks)
1947 * We deal with the read-ahead logic here.
1949 if (ra_ptr >= ra_max) {
1950 /* Refill the readahead buffer */
1953 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
1955 * Terminate if we reach the end of the
1956 * directory and must wrap, or if our
1957 * search has finished at this block.
1959 if (b >= nblocks || (num && block == start)) {
1960 bh_use[ra_max] = NULL;
1965 /* XXX: questionable readahead stuff here */
1966 bh = ocfs2_bread(dir, b++, &err, 1);
1967 bh_use[ra_max] = bh;
1970 ll_rw_block(READ, 1, &bh);
1974 if ((bh = bh_use[ra_ptr++]) == NULL)
1977 if (!buffer_uptodate(bh)) {
1978 /* read error, skip block & hope for the best */
1982 i = ocfs2_search_dirblock(bh, dir, name, namelen,
1983 block << sb->s_blocksize_bits,
1986 OCFS2_I(dir)->ip_dir_start_lookup = block;
1988 goto cleanup_and_exit;
1992 goto cleanup_and_exit;
1995 if (++block >= nblocks)
1997 } while (block != start);
2000 * If the directory has grown while we were searching, then
2001 * search the last part of the directory before giving up.
2004 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
2005 if (block < nblocks) {
2011 /* Clean up the read-ahead blocks */
2012 for (; ra_ptr < ra_max; ra_ptr++)
2013 brelse(bh_use[ra_ptr]);
2019 static int ocfs2_blkno_stringify(u64 blkno, char *name)
2021 int status, namelen;
2025 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
2035 if (namelen != OCFS2_ORPHAN_NAMELEN) {
2041 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
2050 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
2051 struct ocfs2_journal_handle *handle,
2052 struct inode *inode,
2054 struct buffer_head **de_bh)
2056 struct inode *orphan_dir_inode = NULL;
2057 struct buffer_head *orphan_dir_bh = NULL;
2060 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2066 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2067 ORPHAN_DIR_SYSTEM_INODE,
2069 if (!orphan_dir_inode) {
2075 ocfs2_handle_add_inode(handle, orphan_dir_inode);
2076 status = ocfs2_meta_lock(orphan_dir_inode, handle, &orphan_dir_bh, 1);
2082 status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
2083 orphan_dir_bh, name,
2084 OCFS2_ORPHAN_NAMELEN, de_bh);
2091 if (orphan_dir_inode)
2092 iput(orphan_dir_inode);
2095 brelse(orphan_dir_bh);
2101 static int ocfs2_orphan_add(struct ocfs2_super *osb,
2102 struct ocfs2_journal_handle *handle,
2103 struct inode *inode,
2104 struct ocfs2_dinode *fe,
2106 struct buffer_head *de_bh)
2108 struct inode *orphan_dir_inode = NULL;
2109 struct buffer_head *orphan_dir_bh = NULL;
2111 struct ocfs2_dinode *orphan_fe;
2113 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
2115 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2116 ORPHAN_DIR_SYSTEM_INODE,
2118 if (!orphan_dir_inode) {
2124 status = ocfs2_read_block(osb,
2125 OCFS2_I(orphan_dir_inode)->ip_blkno,
2126 &orphan_dir_bh, OCFS2_BH_CACHED,
2133 status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
2134 OCFS2_JOURNAL_ACCESS_WRITE);
2140 /* we're a cluster, and nlink can change on disk from
2141 * underneath us... */
2142 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2143 if (S_ISDIR(inode->i_mode))
2144 le16_add_cpu(&orphan_fe->i_links_count, 1);
2145 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2147 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2153 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
2154 OCFS2_ORPHAN_NAMELEN, inode,
2155 OCFS2_I(inode)->ip_blkno,
2156 orphan_dir_bh, de_bh);
2162 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
2164 /* Record which orphan dir our inode now resides
2165 * in. delete_inode will use this to determine which orphan
2167 spin_lock(&OCFS2_I(inode)->ip_lock);
2168 OCFS2_I(inode)->ip_orphaned_slot = osb->slot_num;
2169 spin_unlock(&OCFS2_I(inode)->ip_lock);
2171 mlog(0, "Inode %llu orphaned in slot %d\n",
2172 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
2175 if (orphan_dir_inode)
2176 iput(orphan_dir_inode);
2179 brelse(orphan_dir_bh);
2185 /* unlike orphan_add, we expect the orphan dir to already be locked here. */
2186 int ocfs2_orphan_del(struct ocfs2_super *osb,
2187 struct ocfs2_journal_handle *handle,
2188 struct inode *orphan_dir_inode,
2189 struct inode *inode,
2190 struct buffer_head *orphan_dir_bh)
2192 char name[OCFS2_ORPHAN_NAMELEN + 1];
2193 struct ocfs2_dinode *orphan_fe;
2195 struct buffer_head *target_de_bh = NULL;
2196 struct ocfs2_dir_entry *target_de = NULL;
2200 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2206 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
2207 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
2208 OCFS2_ORPHAN_NAMELEN);
2210 /* find it's spot in the orphan directory */
2211 target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
2212 orphan_dir_inode, &target_de);
2213 if (!target_de_bh) {
2219 /* remove it from the orphan directory */
2220 status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
2227 status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
2228 OCFS2_JOURNAL_ACCESS_WRITE);
2234 /* do the i_nlink dance! :) */
2235 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2236 if (S_ISDIR(inode->i_mode))
2237 le16_add_cpu(&orphan_fe->i_links_count, -1);
2238 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2240 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2248 brelse(target_de_bh);
2254 struct inode_operations ocfs2_dir_iops = {
2255 .create = ocfs2_create,
2256 .lookup = ocfs2_lookup,
2258 .unlink = ocfs2_unlink,
2259 .rmdir = ocfs2_unlink,
2260 .symlink = ocfs2_symlink,
2261 .mkdir = ocfs2_mkdir,
2262 .mknod = ocfs2_mknod,
2263 .rename = ocfs2_rename,
2264 .setattr = ocfs2_setattr,
2265 .getattr = ocfs2_getattr,