1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Create and rename file, directory, symlinks
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 * Portions of this code from linux/fs/ext3/dir.c
12 * Copyright (C) 1992, 1993, 1994, 1995
13 * Remy Card (card@masi.ibp.fr)
14 * Laboratoire MASI - Institut Blaise pascal
15 * Universite Pierre et Marie Curie (Paris VI)
19 * linux/fs/minix/dir.c
21 * Copyright (C) 1991, 1992 Linux Torvalds
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public
25 * License as published by the Free Software Foundation; either
26 * version 2 of the License, or (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 * General Public License for more details.
33 * You should have received a copy of the GNU General Public
34 * License along with this program; if not, write to the
35 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
36 * Boston, MA 021110-1307, USA.
40 #include <linux/types.h>
41 #include <linux/slab.h>
42 #include <linux/highmem.h>
44 #define MLOG_MASK_PREFIX ML_NAMEI
45 #include <cluster/masklog.h>
53 #include "extent_map.h"
65 #include "buffer_head_io.h"
67 #define NAMEI_RA_CHUNKS 2
68 #define NAMEI_RA_BLOCKS 4
69 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
70 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
72 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
74 const char *name, int namelen,
76 struct ocfs2_dir_entry **res_dir);
78 static int ocfs2_delete_entry(handle_t *handle,
80 struct ocfs2_dir_entry *de_del,
81 struct buffer_head *bh);
83 static int __ocfs2_add_entry(handle_t *handle,
85 const char *name, int namelen,
86 struct inode *inode, u64 blkno,
87 struct buffer_head *parent_fe_bh,
88 struct buffer_head *insert_bh);
90 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
92 struct dentry *dentry, int mode,
94 struct buffer_head **new_fe_bh,
95 struct buffer_head *parent_fe_bh,
97 struct inode **ret_inode,
98 struct ocfs2_alloc_context *inode_ac);
100 static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
102 struct inode *parent,
104 struct buffer_head *fe_bh,
105 struct ocfs2_alloc_context *data_ac);
107 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
108 struct inode **ret_orphan_dir,
111 struct buffer_head **de_bh);
113 static int ocfs2_orphan_add(struct ocfs2_super *osb,
116 struct ocfs2_dinode *fe,
118 struct buffer_head *de_bh,
119 struct inode *orphan_dir_inode);
121 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
124 const char *symname);
126 static inline int ocfs2_add_entry(handle_t *handle,
127 struct dentry *dentry,
128 struct inode *inode, u64 blkno,
129 struct buffer_head *parent_fe_bh,
130 struct buffer_head *insert_bh)
132 return __ocfs2_add_entry(handle, dentry->d_parent->d_inode,
133 dentry->d_name.name, dentry->d_name.len,
134 inode, blkno, parent_fe_bh, insert_bh);
137 /* An orphan dir name is an 8 byte value, printed as a hex string */
138 #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
140 static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
141 struct nameidata *nd)
145 struct buffer_head *dirent_bh = NULL;
146 struct inode *inode = NULL;
148 struct ocfs2_dir_entry *dirent;
149 struct ocfs2_inode_info *oi;
151 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
152 dentry->d_name.len, dentry->d_name.name);
154 if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
155 ret = ERR_PTR(-ENAMETOOLONG);
159 mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
160 dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
162 status = ocfs2_meta_lock(dir, NULL, 0);
164 if (status != -ENOENT)
166 ret = ERR_PTR(status);
170 status = ocfs2_find_files_on_disk(dentry->d_name.name,
171 dentry->d_name.len, &blkno,
172 dir, &dirent_bh, &dirent);
176 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0);
178 ret = ERR_PTR(-EACCES);
183 /* Clear any orphaned state... If we were able to look up the
184 * inode from a directory, it certainly can't be orphaned. We
185 * might have the bad state from a node which intended to
186 * orphan this inode but crashed before it could commit the
188 spin_lock(&oi->ip_lock);
189 oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
190 spin_unlock(&oi->ip_lock);
193 dentry->d_op = &ocfs2_dentry_ops;
194 ret = d_splice_alias(inode, dentry);
198 * If d_splice_alias() finds a DCACHE_DISCONNECTED
199 * dentry, it will d_move() it on top of ourse. The
200 * return value will indicate this however, so in
201 * those cases, we switch them around for the locking
204 * NOTE: This dentry already has ->d_op set from
205 * ocfs2_get_parent() and ocfs2_get_dentry()
210 status = ocfs2_dentry_attach_lock(dentry, inode,
211 OCFS2_I(dir)->ip_blkno);
214 ret = ERR_PTR(status);
220 /* Don't drop the cluster lock until *after* the d_add --
221 * unlink on another node will message us to remove that
222 * dentry under this lock so otherwise we can race this with
223 * the vote thread and have a stale dentry. */
224 ocfs2_meta_unlock(dir, 0);
235 static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
237 struct inode *parent,
239 struct buffer_head *fe_bh,
240 struct ocfs2_alloc_context *data_ac)
243 struct buffer_head *new_bh = NULL;
244 struct ocfs2_dir_entry *de = NULL;
248 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
249 data_ac, NULL, &new_bh);
255 ocfs2_set_new_buffer_uptodate(inode, new_bh);
257 status = ocfs2_journal_access(handle, inode, new_bh,
258 OCFS2_JOURNAL_ACCESS_CREATE);
263 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
265 de = (struct ocfs2_dir_entry *) new_bh->b_data;
266 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
269 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
270 strcpy(de->name, ".");
271 ocfs2_set_de_type(de, S_IFDIR);
272 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
273 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
274 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
275 OCFS2_DIR_REC_LEN(1));
277 strcpy(de->name, "..");
278 ocfs2_set_de_type(de, S_IFDIR);
280 status = ocfs2_journal_dirty(handle, new_bh);
286 i_size_write(inode, inode->i_sb->s_blocksize);
288 inode->i_blocks = ocfs2_inode_sector_count(inode);
289 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
304 static int ocfs2_mknod(struct inode *dir,
305 struct dentry *dentry,
310 struct buffer_head *parent_fe_bh = NULL;
311 handle_t *handle = NULL;
312 struct ocfs2_super *osb;
313 struct ocfs2_dinode *dirfe;
314 struct buffer_head *new_fe_bh = NULL;
315 struct buffer_head *de_bh = NULL;
316 struct inode *inode = NULL;
317 struct ocfs2_alloc_context *inode_ac = NULL;
318 struct ocfs2_alloc_context *data_ac = NULL;
320 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
321 (unsigned long)dev, dentry->d_name.len,
322 dentry->d_name.name);
324 /* get our super block */
325 osb = OCFS2_SB(dir->i_sb);
327 status = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
329 if (status != -ENOENT)
334 if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
339 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
340 if (!dirfe->i_links_count) {
341 /* can't make a file in a deleted directory. */
346 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
351 /* get a spot inside the dir. */
352 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
354 dentry->d_name.len, &de_bh);
360 /* reserve an inode spot */
361 status = ocfs2_reserve_new_inode(osb, &inode_ac);
363 if (status != -ENOSPC)
368 /* are we making a directory? If so, reserve a cluster for his
371 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
373 if (status != -ENOSPC)
379 handle = ocfs2_start_trans(osb, OCFS2_MKNOD_CREDITS);
380 if (IS_ERR(handle)) {
381 status = PTR_ERR(handle);
387 /* do the real work now. */
388 status = ocfs2_mknod_locked(osb, dir, dentry, mode, dev,
389 &new_fe_bh, parent_fe_bh, handle,
397 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
404 status = ocfs2_journal_access(handle, dir, parent_fe_bh,
405 OCFS2_JOURNAL_ACCESS_WRITE);
410 le16_add_cpu(&dirfe->i_links_count, 1);
411 status = ocfs2_journal_dirty(handle, parent_fe_bh);
419 status = ocfs2_add_entry(handle, dentry, inode,
420 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
427 status = ocfs2_dentry_attach_lock(dentry, inode,
428 OCFS2_I(dir)->ip_blkno);
434 insert_inode_hash(inode);
435 dentry->d_op = &ocfs2_dentry_ops;
436 d_instantiate(dentry, inode);
440 ocfs2_commit_trans(osb, handle);
442 ocfs2_meta_unlock(dir, 1);
444 if (status == -ENOSPC)
445 mlog(0, "Disk is full\n");
454 brelse(parent_fe_bh);
456 if ((status < 0) && inode)
460 ocfs2_free_alloc_context(inode_ac);
463 ocfs2_free_alloc_context(data_ac);
470 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
472 struct dentry *dentry, int mode,
474 struct buffer_head **new_fe_bh,
475 struct buffer_head *parent_fe_bh,
477 struct inode **ret_inode,
478 struct ocfs2_alloc_context *inode_ac)
481 struct ocfs2_dinode *fe = NULL;
482 struct ocfs2_extent_list *fel;
485 struct inode *inode = NULL;
487 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
488 (unsigned long)dev, dentry->d_name.len,
489 dentry->d_name.name);
494 status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
501 inode = new_inode(dir->i_sb);
503 status = PTR_ERR(inode);
504 mlog(ML_ERROR, "new_inode failed!\n");
508 /* populate as many fields early on as possible - many of
509 * these are used by the support functions here and in
511 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
512 OCFS2_I(inode)->ip_blkno = fe_blkno;
517 inode->i_mode = mode;
518 spin_lock(&osb->osb_lock);
519 inode->i_generation = osb->s_next_generation++;
520 spin_unlock(&osb->osb_lock);
522 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
528 ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
530 status = ocfs2_journal_access(handle, inode, *new_fe_bh,
531 OCFS2_JOURNAL_ACCESS_CREATE);
537 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
538 memset(fe, 0, osb->sb->s_blocksize);
540 fe->i_generation = cpu_to_le32(inode->i_generation);
541 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
542 fe->i_blkno = cpu_to_le64(fe_blkno);
543 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
544 fe->i_suballoc_slot = cpu_to_le16(osb->slot_num);
545 fe->i_uid = cpu_to_le32(current->fsuid);
546 if (dir->i_mode & S_ISGID) {
547 fe->i_gid = cpu_to_le32(dir->i_gid);
551 fe->i_gid = cpu_to_le32(current->fsgid);
552 fe->i_mode = cpu_to_le16(mode);
553 if (S_ISCHR(mode) || S_ISBLK(mode))
554 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
556 fe->i_links_count = cpu_to_le16(inode->i_nlink);
558 fe->i_last_eb_blk = 0;
559 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
560 le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
561 fe->i_atime = fe->i_ctime = fe->i_mtime =
562 cpu_to_le64(CURRENT_TIME.tv_sec);
563 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
564 cpu_to_le32(CURRENT_TIME.tv_nsec);
567 fel = &fe->id2.i_list;
568 fel->l_tree_depth = 0;
569 fel->l_next_free_rec = 0;
570 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
572 status = ocfs2_journal_dirty(handle, *new_fe_bh);
578 if (ocfs2_populate_inode(inode, fe, 1) < 0) {
579 mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
580 "i_blkno=%llu, i_ino=%lu\n",
581 (unsigned long long) (*new_fe_bh)->b_blocknr,
582 (unsigned long long)fe->i_blkno, inode->i_ino);
586 ocfs2_inode_set_new(osb, inode);
587 if (!ocfs2_mount_local(osb)) {
588 status = ocfs2_create_new_inode_locks(inode);
593 status = 0; /* error in ocfs2_create_new_inode_locks is not
611 static int ocfs2_mkdir(struct inode *dir,
612 struct dentry *dentry,
617 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
618 dentry->d_name.len, dentry->d_name.name);
619 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
625 static int ocfs2_create(struct inode *dir,
626 struct dentry *dentry,
628 struct nameidata *nd)
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_IFREG, 0);
640 static int ocfs2_link(struct dentry *old_dentry,
642 struct dentry *dentry)
645 struct inode *inode = old_dentry->d_inode;
647 struct buffer_head *fe_bh = NULL;
648 struct buffer_head *parent_fe_bh = NULL;
649 struct buffer_head *de_bh = NULL;
650 struct ocfs2_dinode *fe = NULL;
651 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
653 mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
654 old_dentry->d_name.len, old_dentry->d_name.name,
655 dentry->d_name.len, dentry->d_name.name);
657 if (S_ISDIR(inode->i_mode))
660 err = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
672 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
677 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
679 dentry->d_name.len, &de_bh);
685 err = ocfs2_meta_lock(inode, &fe_bh, 1);
692 fe = (struct ocfs2_dinode *) fe_bh->b_data;
693 if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
695 goto out_unlock_inode;
698 handle = ocfs2_start_trans(osb, OCFS2_LINK_CREDITS);
699 if (IS_ERR(handle)) {
700 err = PTR_ERR(handle);
703 goto out_unlock_inode;
706 err = ocfs2_journal_access(handle, inode, fe_bh,
707 OCFS2_JOURNAL_ACCESS_WRITE);
714 inode->i_ctime = CURRENT_TIME;
715 fe->i_links_count = cpu_to_le16(inode->i_nlink);
716 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
717 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
719 err = ocfs2_journal_dirty(handle, fe_bh);
721 le16_add_cpu(&fe->i_links_count, -1);
727 err = ocfs2_add_entry(handle, dentry, inode,
728 OCFS2_I(inode)->ip_blkno,
729 parent_fe_bh, de_bh);
731 le16_add_cpu(&fe->i_links_count, -1);
737 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
743 atomic_inc(&inode->i_count);
744 dentry->d_op = &ocfs2_dentry_ops;
745 d_instantiate(dentry, inode);
748 ocfs2_commit_trans(osb, handle);
750 ocfs2_meta_unlock(inode, 1);
753 ocfs2_meta_unlock(dir, 1);
760 brelse(parent_fe_bh);
768 * Takes and drops an exclusive lock on the given dentry. This will
769 * force other nodes to drop it.
771 static int ocfs2_remote_dentry_delete(struct dentry *dentry)
775 ret = ocfs2_dentry_lock(dentry, 1);
779 ocfs2_dentry_unlock(dentry, 1);
784 static inline int inode_is_unlinkable(struct inode *inode)
786 if (S_ISDIR(inode->i_mode)) {
787 if (inode->i_nlink == 2)
792 if (inode->i_nlink == 1)
797 static int ocfs2_unlink(struct inode *dir,
798 struct dentry *dentry)
801 int child_locked = 0;
802 struct inode *inode = dentry->d_inode;
803 struct inode *orphan_dir = NULL;
804 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
806 struct ocfs2_dinode *fe = NULL;
807 struct buffer_head *fe_bh = NULL;
808 struct buffer_head *parent_node_bh = NULL;
809 handle_t *handle = NULL;
810 struct ocfs2_dir_entry *dirent = NULL;
811 struct buffer_head *dirent_bh = NULL;
812 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
813 struct buffer_head *orphan_entry_bh = NULL;
815 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
816 dentry->d_name.len, dentry->d_name.name);
818 BUG_ON(dentry->d_parent->d_inode != dir);
820 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
822 if (inode == osb->root_inode) {
823 mlog(0, "Cannot delete the root directory\n");
827 status = ocfs2_meta_lock(dir, &parent_node_bh, 1);
829 if (status != -ENOENT)
834 status = ocfs2_find_files_on_disk(dentry->d_name.name,
835 dentry->d_name.len, &blkno,
836 dir, &dirent_bh, &dirent);
838 if (status != -ENOENT)
843 if (OCFS2_I(inode)->ip_blkno != blkno) {
846 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
847 (unsigned long long)OCFS2_I(inode)->ip_blkno,
848 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
852 status = ocfs2_meta_lock(inode, &fe_bh, 1);
854 if (status != -ENOENT)
860 if (S_ISDIR(inode->i_mode)) {
861 if (!ocfs2_empty_dir(inode)) {
864 } else if (inode->i_nlink != 2) {
870 status = ocfs2_remote_dentry_delete(dentry);
872 /* This vote should succeed under all normal
878 if (inode_is_unlinkable(inode)) {
879 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
888 handle = ocfs2_start_trans(osb, OCFS2_UNLINK_CREDITS);
889 if (IS_ERR(handle)) {
890 status = PTR_ERR(handle);
896 status = ocfs2_journal_access(handle, inode, fe_bh,
897 OCFS2_JOURNAL_ACCESS_WRITE);
903 fe = (struct ocfs2_dinode *) fe_bh->b_data;
905 if (inode_is_unlinkable(inode)) {
906 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
907 orphan_entry_bh, orphan_dir);
914 /* delete the name from the parent dir */
915 status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
921 if (S_ISDIR(inode->i_mode))
924 fe->i_links_count = cpu_to_le16(inode->i_nlink);
926 status = ocfs2_journal_dirty(handle, fe_bh);
932 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
933 if (S_ISDIR(inode->i_mode))
936 status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh);
939 if (S_ISDIR(inode->i_mode))
945 ocfs2_commit_trans(osb, handle);
948 ocfs2_meta_unlock(inode, 1);
950 ocfs2_meta_unlock(dir, 1);
953 /* This was locked for us in ocfs2_prepare_orphan_dir() */
954 ocfs2_meta_unlock(orphan_dir, 1);
955 mutex_unlock(&orphan_dir->i_mutex);
966 brelse(parent_node_bh);
969 brelse(orphan_entry_bh);
977 * The only place this should be used is rename!
978 * if they have the same id, then the 1st one is the only one locked.
980 static int ocfs2_double_lock(struct ocfs2_super *osb,
981 struct buffer_head **bh1,
982 struct inode *inode1,
983 struct buffer_head **bh2,
984 struct inode *inode2)
987 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
988 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
989 struct buffer_head **tmpbh;
990 struct inode *tmpinode;
992 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
993 (unsigned long long)oi1->ip_blkno,
994 (unsigned long long)oi2->ip_blkno);
1001 /* we always want to lock the one with the lower lockid first. */
1002 if (oi1->ip_blkno != oi2->ip_blkno) {
1003 if (oi1->ip_blkno < oi2->ip_blkno) {
1004 /* switch id1 and id2 around */
1005 mlog(0, "switching them around...\n");
1015 status = ocfs2_meta_lock(inode2, bh2, 1);
1017 if (status != -ENOENT)
1024 status = ocfs2_meta_lock(inode1, bh1, 1);
1027 * An error return must mean that no cluster locks
1028 * were held on function exit.
1030 if (oi1->ip_blkno != oi2->ip_blkno)
1031 ocfs2_meta_unlock(inode2, 1);
1033 if (status != -ENOENT)
1042 static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
1044 ocfs2_meta_unlock(inode1, 1);
1046 if (inode1 != inode2)
1047 ocfs2_meta_unlock(inode2, 1);
1050 #define PARENT_INO(buffer) \
1051 ((struct ocfs2_dir_entry *) \
1053 le16_to_cpu(((struct ocfs2_dir_entry *)buffer)->rec_len)))->inode
1055 static int ocfs2_rename(struct inode *old_dir,
1056 struct dentry *old_dentry,
1057 struct inode *new_dir,
1058 struct dentry *new_dentry)
1060 int status = 0, rename_lock = 0, parents_locked = 0;
1061 int old_child_locked = 0, new_child_locked = 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 *old_inode_bh = NULL;
1070 struct buffer_head *insert_entry_bh = NULL;
1071 struct ocfs2_super *osb = NULL;
1073 handle_t *handle = NULL;
1074 struct buffer_head *old_dir_bh = NULL;
1075 struct buffer_head *new_dir_bh = NULL;
1076 struct ocfs2_dir_entry *old_de = NULL, *new_de = NULL; // dirent for old_dentry
1078 struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
1079 struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
1080 // this is the 1st dirent bh
1081 nlink_t old_dir_nlink = old_dir->i_nlink;
1083 /* At some point it might be nice to break this function up a
1086 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
1087 old_dir, old_dentry, new_dir, new_dentry,
1088 old_dentry->d_name.len, old_dentry->d_name.name,
1089 new_dentry->d_name.len, new_dentry->d_name.name);
1091 osb = OCFS2_SB(old_dir->i_sb);
1094 if (!igrab(new_inode))
1098 /* Assume a directory hierarchy thusly:
1101 * a,b,c, and d are all directories.
1103 * from cwd of 'a' on both nodes:
1107 * And that's why, just like the VFS, we need a file system
1109 if (old_dentry != new_dentry) {
1110 status = ocfs2_rename_lock(osb);
1118 /* if old and new are the same, this'll just do one lock. */
1119 status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
1120 &new_dir_bh, new_dir);
1127 /* make sure both dirs have bhs
1128 * get an extra ref on old_dir_bh if old==new */
1131 new_dir_bh = old_dir_bh;
1134 mlog(ML_ERROR, "no old_dir_bh!\n");
1141 * Aside from allowing a meta data update, the locking here
1142 * also ensures that the vote thread on other nodes won't have
1143 * to concurrently downconvert the inode and the dentry locks.
1145 status = ocfs2_meta_lock(old_inode, &old_inode_bh, 1);
1147 if (status != -ENOENT)
1151 old_child_locked = 1;
1153 status = ocfs2_remote_dentry_delete(old_dentry);
1159 if (S_ISDIR(old_inode->i_mode)) {
1161 old_inode_de_bh = ocfs2_bread(old_inode, 0, &status, 0);
1162 if (!old_inode_de_bh)
1166 if (le64_to_cpu(PARENT_INO(old_inode_de_bh->b_data)) !=
1167 OCFS2_I(old_dir)->ip_blkno)
1170 if (!new_inode && new_dir!=old_dir &&
1171 new_dir->i_nlink >= OCFS2_LINK_MAX)
1176 old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
1177 old_dentry->d_name.len,
1183 * Check for inode number is _not_ due to possible IO errors.
1184 * We might rmdir the source, keep it as pwd of some process
1185 * and merrily kill the link to whatever was created under the
1186 * same name. Goodbye sticky bit ;-<
1188 if (le64_to_cpu(old_de->inode) != OCFS2_I(old_inode)->ip_blkno)
1191 /* check if the target already exists (in which case we need
1193 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1194 new_dentry->d_name.len,
1195 &newfe_blkno, new_dir, &new_de_bh,
1197 /* The only error we allow here is -ENOENT because the new
1198 * file not existing is perfectly valid. */
1199 if ((status < 0) && (status != -ENOENT)) {
1200 /* If we cannot find the file specified we should just */
1201 /* return the error... */
1206 if (!new_de && new_inode)
1207 mlog(ML_ERROR, "inode %lu does not exist in it's parent "
1208 "directory!", new_inode->i_ino);
1210 /* In case we need to overwrite an existing file, we blow it
1213 /* VFS didn't think there existed an inode here, but
1214 * someone else in the cluster must have raced our
1215 * rename to create one. Today we error cleanly, in
1216 * the future we should consider calling iget to build
1217 * a new struct inode for this entry. */
1221 mlog(0, "We found an inode for name %.*s but VFS "
1222 "didn't give us one.\n", new_dentry->d_name.len,
1223 new_dentry->d_name.name);
1227 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1230 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1231 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1232 (unsigned long long)newfe_blkno,
1233 OCFS2_I(new_inode)->ip_flags);
1237 status = ocfs2_meta_lock(new_inode, &newfe_bh, 1);
1239 if (status != -ENOENT)
1243 new_child_locked = 1;
1245 status = ocfs2_remote_dentry_delete(new_dentry);
1251 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1253 mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
1254 "newfebh=%p bhblocknr=%llu\n", new_de,
1255 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
1256 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1258 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
1259 status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
1269 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1271 status = ocfs2_check_dir_for_entry(new_dir,
1272 new_dentry->d_name.name,
1273 new_dentry->d_name.len);
1277 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1278 new_dentry->d_name.name,
1279 new_dentry->d_name.len,
1287 handle = ocfs2_start_trans(osb, OCFS2_RENAME_CREDITS);
1288 if (IS_ERR(handle)) {
1289 status = PTR_ERR(handle);
1296 if (S_ISDIR(new_inode->i_mode)) {
1297 if (!ocfs2_empty_dir(new_inode) ||
1298 new_inode->i_nlink != 2) {
1299 status = -ENOTEMPTY;
1303 status = ocfs2_journal_access(handle, new_inode, newfe_bh,
1304 OCFS2_JOURNAL_ACCESS_WRITE);
1310 if (S_ISDIR(new_inode->i_mode) ||
1311 (newfe->i_links_count == cpu_to_le16(1))){
1312 status = ocfs2_orphan_add(osb, handle, new_inode,
1314 orphan_entry_bh, orphan_dir);
1321 /* change the dirent to point to the correct inode */
1322 status = ocfs2_journal_access(handle, new_dir, new_de_bh,
1323 OCFS2_JOURNAL_ACCESS_WRITE);
1328 new_de->inode = cpu_to_le64(OCFS2_I(old_inode)->ip_blkno);
1329 new_de->file_type = old_de->file_type;
1330 new_dir->i_version++;
1331 status = ocfs2_journal_dirty(handle, new_de_bh);
1337 if (S_ISDIR(new_inode->i_mode))
1338 newfe->i_links_count = 0;
1340 le16_add_cpu(&newfe->i_links_count, -1);
1342 status = ocfs2_journal_dirty(handle, newfe_bh);
1348 /* if the name was not found in new_dir, add it now */
1349 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1350 OCFS2_I(old_inode)->ip_blkno,
1351 new_dir_bh, insert_entry_bh);
1354 old_inode->i_ctime = CURRENT_TIME;
1355 mark_inode_dirty(old_inode);
1356 ocfs2_mark_inode_dirty(handle, old_inode, old_inode_bh);
1358 /* now that the name has been added to new_dir, remove the old name */
1359 status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
1366 new_inode->i_nlink--;
1367 new_inode->i_ctime = CURRENT_TIME;
1369 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1370 if (old_inode_de_bh) {
1371 status = ocfs2_journal_access(handle, old_inode,
1373 OCFS2_JOURNAL_ACCESS_WRITE);
1374 PARENT_INO(old_inode_de_bh->b_data) =
1375 cpu_to_le64(OCFS2_I(new_dir)->ip_blkno);
1376 status = ocfs2_journal_dirty(handle, old_inode_de_bh);
1379 new_inode->i_nlink--;
1382 mark_inode_dirty(new_dir);
1385 mark_inode_dirty(old_dir);
1386 ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);
1388 mark_inode_dirty(new_inode);
1389 ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);
1392 if (old_dir != new_dir) {
1393 /* Keep the same times on both directories.*/
1394 new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;
1397 * This will also pick up the i_nlink change from the
1400 ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);
1403 if (old_dir_nlink != old_dir->i_nlink) {
1405 mlog(ML_ERROR, "need to change nlink for old dir "
1406 "%llu from %d to %d but bh is NULL!\n",
1407 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1408 (int)old_dir_nlink, old_dir->i_nlink);
1410 struct ocfs2_dinode *fe;
1411 status = ocfs2_journal_access(handle, old_dir,
1413 OCFS2_JOURNAL_ACCESS_WRITE);
1414 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1415 fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
1416 status = ocfs2_journal_dirty(handle, old_dir_bh);
1420 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
1424 ocfs2_rename_unlock(osb);
1427 ocfs2_commit_trans(osb, handle);
1430 ocfs2_double_unlock(old_dir, new_dir);
1432 if (old_child_locked)
1433 ocfs2_meta_unlock(old_inode, 1);
1435 if (new_child_locked)
1436 ocfs2_meta_unlock(new_inode, 1);
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);
1453 brelse(old_inode_bh);
1462 if (old_inode_de_bh)
1463 brelse(old_inode_de_bh);
1464 if (orphan_entry_bh)
1465 brelse(orphan_entry_bh);
1466 if (insert_entry_bh)
1467 brelse(insert_entry_bh);
1475 * we expect i_size = strlen(symname). Copy symname into the file
1476 * data, including the null terminator.
1478 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1480 struct inode *inode,
1481 const char *symname)
1483 struct buffer_head **bhs = NULL;
1485 struct super_block *sb = osb->sb;
1486 u64 p_blkno, p_blocks;
1487 int virtual, blocks, status, i, bytes_left;
1489 bytes_left = i_size_read(inode) + 1;
1490 /* we can't trust i_blocks because we're actually going to
1491 * write i_size + 1 bytes. */
1492 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1494 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1495 (unsigned long long)inode->i_blocks,
1496 i_size_read(inode), blocks);
1498 /* Sanity check -- make sure we're going to fit. */
1500 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1506 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1513 status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks,
1520 /* links can never be larger than one cluster so we know this
1521 * is all going to be contiguous, but do a sanity check
1523 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1530 while(bytes_left > 0) {
1531 c = &symname[virtual * sb->s_blocksize];
1533 bhs[virtual] = sb_getblk(sb, p_blkno);
1534 if (!bhs[virtual]) {
1539 ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
1541 status = ocfs2_journal_access(handle, inode, bhs[virtual],
1542 OCFS2_JOURNAL_ACCESS_CREATE);
1548 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1550 memcpy(bhs[virtual]->b_data, c,
1551 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1554 status = ocfs2_journal_dirty(handle, bhs[virtual]);
1562 bytes_left -= sb->s_blocksize;
1569 for(i = 0; i < blocks; i++)
1579 static int ocfs2_symlink(struct inode *dir,
1580 struct dentry *dentry,
1581 const char *symname)
1583 int status, l, credits;
1585 struct ocfs2_super *osb = NULL;
1586 struct inode *inode = NULL;
1587 struct super_block *sb;
1588 struct buffer_head *new_fe_bh = NULL;
1589 struct buffer_head *de_bh = NULL;
1590 struct buffer_head *parent_fe_bh = NULL;
1591 struct ocfs2_dinode *fe = NULL;
1592 struct ocfs2_dinode *dirfe;
1593 handle_t *handle = NULL;
1594 struct ocfs2_alloc_context *inode_ac = NULL;
1595 struct ocfs2_alloc_context *data_ac = NULL;
1597 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1598 dentry, symname, dentry->d_name.len, dentry->d_name.name);
1603 l = strlen(symname) + 1;
1605 credits = ocfs2_calc_symlink_credits(sb);
1607 /* lock the parent directory */
1608 status = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
1610 if (status != -ENOENT)
1615 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1616 if (!dirfe->i_links_count) {
1617 /* can't make a file in a deleted directory. */
1622 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1623 dentry->d_name.len);
1627 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1628 dentry->d_name.name,
1629 dentry->d_name.len, &de_bh);
1635 status = ocfs2_reserve_new_inode(osb, &inode_ac);
1637 if (status != -ENOSPC)
1642 /* don't reserve bitmap space for fast symlinks. */
1643 if (l > ocfs2_fast_symlink_chars(sb)) {
1644 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
1646 if (status != -ENOSPC)
1652 handle = ocfs2_start_trans(osb, credits);
1653 if (IS_ERR(handle)) {
1654 status = PTR_ERR(handle);
1660 status = ocfs2_mknod_locked(osb, dir, dentry,
1661 S_IFLNK | S_IRWXUGO, 0,
1662 &new_fe_bh, parent_fe_bh, handle,
1669 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1672 if (l > ocfs2_fast_symlink_chars(sb)) {
1675 inode->i_op = &ocfs2_symlink_inode_operations;
1676 status = ocfs2_do_extend_allocation(osb, inode, &offset, 1,
1678 handle, data_ac, NULL,
1681 if (status != -ENOSPC && status != -EINTR) {
1683 "Failed to extend file to %llu\n",
1684 (unsigned long long)newsize);
1690 i_size_write(inode, newsize);
1691 inode->i_blocks = ocfs2_inode_sector_count(inode);
1693 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1694 memcpy((char *) fe->id2.i_symlink, symname, l);
1695 i_size_write(inode, newsize);
1696 inode->i_blocks = 0;
1699 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1705 if (!ocfs2_inode_is_fast_symlink(inode)) {
1706 status = ocfs2_create_symlink_data(osb, handle, inode,
1714 status = ocfs2_add_entry(handle, dentry, inode,
1715 le64_to_cpu(fe->i_blkno), parent_fe_bh,
1722 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
1728 insert_inode_hash(inode);
1729 dentry->d_op = &ocfs2_dentry_ops;
1730 d_instantiate(dentry, inode);
1733 ocfs2_commit_trans(osb, handle);
1735 ocfs2_meta_unlock(dir, 1);
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(handle_t *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 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1828 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1834 status = ocfs2_journal_access(handle, dir, insert_bh,
1835 OCFS2_JOURNAL_ACCESS_WRITE);
1836 /* By now the buffer is marked for journaling */
1837 offset += le16_to_cpu(de->rec_len);
1838 if (le64_to_cpu(de->inode)) {
1839 de1 = (struct ocfs2_dir_entry *)((char *) de +
1840 OCFS2_DIR_REC_LEN(de->name_len));
1842 cpu_to_le16(le16_to_cpu(de->rec_len) -
1843 OCFS2_DIR_REC_LEN(de->name_len));
1844 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1847 de->file_type = OCFS2_FT_UNKNOWN;
1849 de->inode = cpu_to_le64(blkno);
1850 ocfs2_set_de_type(de, inode->i_mode);
1853 de->name_len = namelen;
1854 memcpy(de->name, name, namelen);
1857 status = ocfs2_journal_dirty(handle, insert_bh);
1861 offset += le16_to_cpu(de->rec_len);
1862 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
1865 /* when you think about it, the assert above should prevent us
1866 * from ever getting here. */
1876 * ocfs2_delete_entry deletes a directory entry by merging it with the
1879 static int ocfs2_delete_entry(handle_t *handle,
1881 struct ocfs2_dir_entry *de_del,
1882 struct buffer_head *bh)
1884 struct ocfs2_dir_entry *de, *pde;
1885 int i, status = -ENOENT;
1887 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
1891 de = (struct ocfs2_dir_entry *) bh->b_data;
1892 while (i < bh->b_size) {
1893 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
1899 status = ocfs2_journal_access(handle, dir, bh,
1900 OCFS2_JOURNAL_ACCESS_WRITE);
1908 cpu_to_le16(le16_to_cpu(pde->rec_len) +
1909 le16_to_cpu(de->rec_len));
1913 status = ocfs2_journal_dirty(handle, bh);
1916 i += le16_to_cpu(de->rec_len);
1918 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
1926 * Returns 0 if not found, -1 on failure, and 1 on success
1928 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
1930 const char *name, int namelen,
1931 unsigned long offset,
1932 struct ocfs2_dir_entry **res_dir)
1934 struct ocfs2_dir_entry *de;
1935 char *dlimit, *de_buf;
1941 de_buf = bh->b_data;
1942 dlimit = de_buf + dir->i_sb->s_blocksize;
1944 while (de_buf < dlimit) {
1945 /* this code is executed quadratically often */
1946 /* do minimal checking `by hand' */
1948 de = (struct ocfs2_dir_entry *) de_buf;
1950 if (de_buf + namelen <= dlimit &&
1951 ocfs2_match(namelen, name, de)) {
1952 /* found a match - just to be sure, do a full check */
1953 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1962 /* prevent looping on a bad block */
1963 de_len = le16_to_cpu(de->rec_len);
1978 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
1980 struct ocfs2_dir_entry **res_dir)
1982 struct super_block *sb;
1983 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1984 struct buffer_head *bh, *ret = NULL;
1985 unsigned long start, block, b;
1986 int ra_max = 0; /* Number of bh's in the readahead
1988 int ra_ptr = 0; /* Current index into readahead
1991 int nblocks, i, err;
1998 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
1999 start = OCFS2_I(dir)->ip_dir_start_lookup;
2000 if (start >= nblocks)
2007 * We deal with the read-ahead logic here.
2009 if (ra_ptr >= ra_max) {
2010 /* Refill the readahead buffer */
2013 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
2015 * Terminate if we reach the end of the
2016 * directory and must wrap, or if our
2017 * search has finished at this block.
2019 if (b >= nblocks || (num && block == start)) {
2020 bh_use[ra_max] = NULL;
2025 bh = ocfs2_bread(dir, b++, &err, 1);
2026 bh_use[ra_max] = bh;
2029 if ((bh = bh_use[ra_ptr++]) == NULL)
2032 if (!buffer_uptodate(bh)) {
2033 /* read error, skip block & hope for the best */
2034 ocfs2_error(dir->i_sb, "reading directory %llu, "
2036 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2041 i = ocfs2_search_dirblock(bh, dir, name, namelen,
2042 block << sb->s_blocksize_bits,
2045 OCFS2_I(dir)->ip_dir_start_lookup = block;
2047 goto cleanup_and_exit;
2051 goto cleanup_and_exit;
2054 if (++block >= nblocks)
2056 } while (block != start);
2059 * If the directory has grown while we were searching, then
2060 * search the last part of the directory before giving up.
2063 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
2064 if (block < nblocks) {
2070 /* Clean up the read-ahead blocks */
2071 for (; ra_ptr < ra_max; ra_ptr++)
2072 brelse(bh_use[ra_ptr]);
2078 static int ocfs2_blkno_stringify(u64 blkno, char *name)
2080 int status, namelen;
2084 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
2094 if (namelen != OCFS2_ORPHAN_NAMELEN) {
2100 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
2109 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
2110 struct inode **ret_orphan_dir,
2111 struct inode *inode,
2113 struct buffer_head **de_bh)
2115 struct inode *orphan_dir_inode;
2116 struct buffer_head *orphan_dir_bh = NULL;
2119 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2125 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2126 ORPHAN_DIR_SYSTEM_INODE,
2128 if (!orphan_dir_inode) {
2134 mutex_lock(&orphan_dir_inode->i_mutex);
2136 status = ocfs2_meta_lock(orphan_dir_inode, &orphan_dir_bh, 1);
2142 status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
2143 orphan_dir_bh, name,
2144 OCFS2_ORPHAN_NAMELEN, de_bh);
2146 ocfs2_meta_unlock(orphan_dir_inode, 1);
2152 *ret_orphan_dir = orphan_dir_inode;
2156 mutex_unlock(&orphan_dir_inode->i_mutex);
2157 iput(orphan_dir_inode);
2161 brelse(orphan_dir_bh);
2167 static int ocfs2_orphan_add(struct ocfs2_super *osb,
2169 struct inode *inode,
2170 struct ocfs2_dinode *fe,
2172 struct buffer_head *de_bh,
2173 struct inode *orphan_dir_inode)
2175 struct buffer_head *orphan_dir_bh = NULL;
2177 struct ocfs2_dinode *orphan_fe;
2179 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
2181 status = ocfs2_read_block(osb,
2182 OCFS2_I(orphan_dir_inode)->ip_blkno,
2183 &orphan_dir_bh, OCFS2_BH_CACHED,
2190 status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
2191 OCFS2_JOURNAL_ACCESS_WRITE);
2197 /* we're a cluster, and nlink can change on disk from
2198 * underneath us... */
2199 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2200 if (S_ISDIR(inode->i_mode))
2201 le16_add_cpu(&orphan_fe->i_links_count, 1);
2202 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2204 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2210 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
2211 OCFS2_ORPHAN_NAMELEN, inode,
2212 OCFS2_I(inode)->ip_blkno,
2213 orphan_dir_bh, de_bh);
2219 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
2221 /* Record which orphan dir our inode now resides
2222 * in. delete_inode will use this to determine which orphan
2224 fe->i_orphaned_slot = cpu_to_le16(osb->slot_num);
2226 mlog(0, "Inode %llu orphaned in slot %d\n",
2227 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
2231 brelse(orphan_dir_bh);
2237 /* unlike orphan_add, we expect the orphan dir to already be locked here. */
2238 int ocfs2_orphan_del(struct ocfs2_super *osb,
2240 struct inode *orphan_dir_inode,
2241 struct inode *inode,
2242 struct buffer_head *orphan_dir_bh)
2244 char name[OCFS2_ORPHAN_NAMELEN + 1];
2245 struct ocfs2_dinode *orphan_fe;
2247 struct buffer_head *target_de_bh = NULL;
2248 struct ocfs2_dir_entry *target_de = NULL;
2252 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2258 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
2259 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
2260 OCFS2_ORPHAN_NAMELEN);
2262 /* find it's spot in the orphan directory */
2263 target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
2264 orphan_dir_inode, &target_de);
2265 if (!target_de_bh) {
2271 /* remove it from the orphan directory */
2272 status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
2279 status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
2280 OCFS2_JOURNAL_ACCESS_WRITE);
2286 /* do the i_nlink dance! :) */
2287 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2288 if (S_ISDIR(inode->i_mode))
2289 le16_add_cpu(&orphan_fe->i_links_count, -1);
2290 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2292 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2300 brelse(target_de_bh);
2306 const struct inode_operations ocfs2_dir_iops = {
2307 .create = ocfs2_create,
2308 .lookup = ocfs2_lookup,
2310 .unlink = ocfs2_unlink,
2311 .rmdir = ocfs2_unlink,
2312 .symlink = ocfs2_symlink,
2313 .mkdir = ocfs2_mkdir,
2314 .mknod = ocfs2_mknod,
2315 .rename = ocfs2_rename,
2316 .setattr = ocfs2_setattr,
2317 .getattr = ocfs2_getattr,
2318 .permission = ocfs2_permission,