1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Create and rename file, directory, symlinks
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 * Portions of this code from linux/fs/ext3/dir.c
12 * Copyright (C) 1992, 1993, 1994, 1995
13 * Remy Card (card@masi.ibp.fr)
14 * Laboratoire MASI - Institut Blaise pascal
15 * Universite Pierre et Marie Curie (Paris VI)
19 * linux/fs/minix/dir.c
21 * Copyright (C) 1991, 1992 Linux Torvalds
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public
25 * License as published by the Free Software Foundation; either
26 * version 2 of the License, or (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 * General Public License for more details.
33 * You should have received a copy of the GNU General Public
34 * License along with this program; if not, write to the
35 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
36 * Boston, MA 021110-1307, USA.
40 #include <linux/types.h>
41 #include <linux/slab.h>
42 #include <linux/highmem.h>
44 #define MLOG_MASK_PREFIX ML_NAMEI
45 #include <cluster/masklog.h>
53 #include "extent_map.h"
65 #include "buffer_head_io.h"
67 #define NAMEI_RA_CHUNKS 2
68 #define NAMEI_RA_BLOCKS 4
69 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
70 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
72 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
74 const char *name, int namelen,
76 struct ocfs2_dir_entry **res_dir);
78 static int ocfs2_delete_entry(struct ocfs2_journal_handle *handle,
80 struct ocfs2_dir_entry *de_del,
81 struct buffer_head *bh);
83 static int __ocfs2_add_entry(struct ocfs2_journal_handle *handle,
85 const char *name, int namelen,
86 struct inode *inode, u64 blkno,
87 struct buffer_head *parent_fe_bh,
88 struct buffer_head *insert_bh);
90 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
92 struct dentry *dentry, int mode,
94 struct buffer_head **new_fe_bh,
95 struct buffer_head *parent_fe_bh,
96 struct ocfs2_journal_handle *handle,
97 struct inode **ret_inode,
98 struct ocfs2_alloc_context *inode_ac);
100 static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
101 struct ocfs2_journal_handle *handle,
102 struct inode *parent,
104 struct buffer_head *fe_bh,
105 struct ocfs2_alloc_context *data_ac);
107 static int ocfs2_double_lock(struct ocfs2_super *osb,
108 struct ocfs2_journal_handle *handle,
109 struct buffer_head **bh1,
110 struct inode *inode1,
111 struct buffer_head **bh2,
112 struct inode *inode2);
114 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
115 struct ocfs2_journal_handle *handle,
118 struct buffer_head **de_bh);
120 static int ocfs2_orphan_add(struct ocfs2_super *osb,
121 struct ocfs2_journal_handle *handle,
123 struct ocfs2_dinode *fe,
125 struct buffer_head *de_bh);
127 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
128 struct ocfs2_journal_handle *handle,
130 const char *symname);
132 static inline int ocfs2_add_entry(struct ocfs2_journal_handle *handle,
133 struct dentry *dentry,
134 struct inode *inode, u64 blkno,
135 struct buffer_head *parent_fe_bh,
136 struct buffer_head *insert_bh)
138 return __ocfs2_add_entry(handle, dentry->d_parent->d_inode,
139 dentry->d_name.name, dentry->d_name.len,
140 inode, blkno, parent_fe_bh, insert_bh);
143 /* An orphan dir name is an 8 byte value, printed as a hex string */
144 #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
146 static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
147 struct nameidata *nd)
151 struct buffer_head *dirent_bh = NULL;
152 struct inode *inode = NULL;
154 struct ocfs2_dir_entry *dirent;
155 struct ocfs2_inode_info *oi;
157 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
158 dentry->d_name.len, dentry->d_name.name);
160 if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
161 ret = ERR_PTR(-ENAMETOOLONG);
165 mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
166 dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
168 status = ocfs2_meta_lock(dir, NULL, NULL, 0);
170 if (status != -ENOENT)
172 ret = ERR_PTR(status);
176 status = ocfs2_find_files_on_disk(dentry->d_name.name,
177 dentry->d_name.len, &blkno,
178 dir, &dirent_bh, &dirent);
182 inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0);
184 mlog(ML_ERROR, "Unable to create inode %llu\n",
185 (unsigned long long)blkno);
186 ret = ERR_PTR(-EACCES);
191 /* Clear any orphaned state... If we were able to look up the
192 * inode from a directory, it certainly can't be orphaned. We
193 * might have the bad state from a node which intended to
194 * orphan this inode but crashed before it could commit the
196 spin_lock(&oi->ip_lock);
197 oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
198 oi->ip_orphaned_slot = OCFS2_INVALID_SLOT;
199 spin_unlock(&oi->ip_lock);
202 dentry->d_op = &ocfs2_dentry_ops;
203 ret = d_splice_alias(inode, dentry);
207 * If d_splice_alias() finds a DCACHE_DISCONNECTED
208 * dentry, it will d_move() it on top of ourse. The
209 * return value will indicate this however, so in
210 * those cases, we switch them around for the locking
213 * NOTE: This dentry already has ->d_op set from
214 * ocfs2_get_parent() and ocfs2_get_dentry()
219 status = ocfs2_dentry_attach_lock(dentry, inode,
220 OCFS2_I(dir)->ip_blkno);
223 ret = ERR_PTR(status);
229 /* Don't drop the cluster lock until *after* the d_add --
230 * unlink on another node will message us to remove that
231 * dentry under this lock so otherwise we can race this with
232 * the vote thread and have a stale dentry. */
233 ocfs2_meta_unlock(dir, 0);
244 static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
245 struct ocfs2_journal_handle *handle,
246 struct inode *parent,
248 struct buffer_head *fe_bh,
249 struct ocfs2_alloc_context *data_ac)
252 struct buffer_head *new_bh = NULL;
253 struct ocfs2_dir_entry *de = NULL;
257 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
258 data_ac, NULL, &new_bh);
264 ocfs2_set_new_buffer_uptodate(inode, new_bh);
266 status = ocfs2_journal_access(handle, inode, new_bh,
267 OCFS2_JOURNAL_ACCESS_CREATE);
272 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
274 de = (struct ocfs2_dir_entry *) new_bh->b_data;
275 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
278 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
279 strcpy(de->name, ".");
280 ocfs2_set_de_type(de, S_IFDIR);
281 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
282 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
283 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
284 OCFS2_DIR_REC_LEN(1));
286 strcpy(de->name, "..");
287 ocfs2_set_de_type(de, S_IFDIR);
289 status = ocfs2_journal_dirty(handle, new_bh);
295 i_size_write(inode, inode->i_sb->s_blocksize);
297 inode->i_blocks = ocfs2_align_bytes_to_sectors(inode->i_sb->s_blocksize);
298 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
313 static int ocfs2_mknod(struct inode *dir,
314 struct dentry *dentry,
319 struct buffer_head *parent_fe_bh = NULL;
320 struct ocfs2_journal_handle *handle = NULL;
321 struct ocfs2_super *osb;
322 struct ocfs2_dinode *dirfe;
323 struct buffer_head *new_fe_bh = NULL;
324 struct buffer_head *de_bh = NULL;
325 struct inode *inode = NULL;
326 struct ocfs2_alloc_context *inode_ac = NULL;
327 struct ocfs2_alloc_context *data_ac = NULL;
329 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
330 (unsigned long)dev, dentry->d_name.len,
331 dentry->d_name.name);
333 /* get our super block */
334 osb = OCFS2_SB(dir->i_sb);
336 handle = ocfs2_alloc_handle(osb);
337 if (handle == NULL) {
343 status = ocfs2_meta_lock(dir, handle, &parent_fe_bh, 1);
345 if (status != -ENOENT)
350 if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
355 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
356 if (!dirfe->i_links_count) {
357 /* can't make a file in a deleted directory. */
362 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
367 /* get a spot inside the dir. */
368 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
370 dentry->d_name.len, &de_bh);
376 /* reserve an inode spot */
377 status = ocfs2_reserve_new_inode(osb, handle, &inode_ac);
379 if (status != -ENOSPC)
384 /* are we making a directory? If so, reserve a cluster for his
387 status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
389 if (status != -ENOSPC)
395 handle = ocfs2_start_trans(osb, handle, OCFS2_MKNOD_CREDITS);
396 if (IS_ERR(handle)) {
397 status = PTR_ERR(handle);
403 /* do the real work now. */
404 status = ocfs2_mknod_locked(osb, dir, dentry, mode, dev,
405 &new_fe_bh, parent_fe_bh, handle,
413 status = ocfs2_fill_new_dir(osb, handle, dir, inode,
420 status = ocfs2_journal_access(handle, dir, parent_fe_bh,
421 OCFS2_JOURNAL_ACCESS_WRITE);
426 le16_add_cpu(&dirfe->i_links_count, 1);
427 status = ocfs2_journal_dirty(handle, parent_fe_bh);
435 status = ocfs2_add_entry(handle, dentry, inode,
436 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
443 status = ocfs2_dentry_attach_lock(dentry, inode,
444 OCFS2_I(dir)->ip_blkno);
450 insert_inode_hash(inode);
451 dentry->d_op = &ocfs2_dentry_ops;
452 d_instantiate(dentry, inode);
456 ocfs2_commit_trans(handle);
458 if (status == -ENOSPC)
459 mlog(0, "Disk is full\n");
468 brelse(parent_fe_bh);
470 if ((status < 0) && inode)
474 ocfs2_free_alloc_context(inode_ac);
477 ocfs2_free_alloc_context(data_ac);
484 static int ocfs2_mknod_locked(struct ocfs2_super *osb,
486 struct dentry *dentry, int mode,
488 struct buffer_head **new_fe_bh,
489 struct buffer_head *parent_fe_bh,
490 struct ocfs2_journal_handle *handle,
491 struct inode **ret_inode,
492 struct ocfs2_alloc_context *inode_ac)
495 struct ocfs2_dinode *fe = NULL;
496 struct ocfs2_extent_list *fel;
499 struct inode *inode = NULL;
501 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode,
502 (unsigned long)dev, dentry->d_name.len,
503 dentry->d_name.name);
508 status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit,
515 inode = new_inode(dir->i_sb);
517 status = PTR_ERR(inode);
518 mlog(ML_ERROR, "new_inode failed!\n");
522 /* populate as many fields early on as possible - many of
523 * these are used by the support functions here and in
525 inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
526 OCFS2_I(inode)->ip_blkno = fe_blkno;
531 inode->i_mode = mode;
532 spin_lock(&osb->osb_lock);
533 inode->i_generation = osb->s_next_generation++;
534 spin_unlock(&osb->osb_lock);
536 *new_fe_bh = sb_getblk(osb->sb, fe_blkno);
542 ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh);
544 status = ocfs2_journal_access(handle, inode, *new_fe_bh,
545 OCFS2_JOURNAL_ACCESS_CREATE);
551 fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
552 memset(fe, 0, osb->sb->s_blocksize);
554 fe->i_generation = cpu_to_le32(inode->i_generation);
555 fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
556 fe->i_blkno = cpu_to_le64(fe_blkno);
557 fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
558 fe->i_suballoc_slot = cpu_to_le16(osb->slot_num);
559 fe->i_uid = cpu_to_le32(current->fsuid);
560 if (dir->i_mode & S_ISGID) {
561 fe->i_gid = cpu_to_le32(dir->i_gid);
565 fe->i_gid = cpu_to_le32(current->fsgid);
566 fe->i_mode = cpu_to_le16(mode);
567 if (S_ISCHR(mode) || S_ISBLK(mode))
568 fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
570 fe->i_links_count = cpu_to_le16(inode->i_nlink);
572 fe->i_last_eb_blk = 0;
573 strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
574 le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL);
575 fe->i_atime = fe->i_ctime = fe->i_mtime =
576 cpu_to_le64(CURRENT_TIME.tv_sec);
577 fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
578 cpu_to_le32(CURRENT_TIME.tv_nsec);
581 fel = &fe->id2.i_list;
582 fel->l_tree_depth = 0;
583 fel->l_next_free_rec = 0;
584 fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
586 status = ocfs2_journal_dirty(handle, *new_fe_bh);
592 if (ocfs2_populate_inode(inode, fe, 1) < 0) {
593 mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, "
594 "i_blkno=%llu, i_ino=%lu\n",
595 (unsigned long long) (*new_fe_bh)->b_blocknr,
596 (unsigned long long)fe->i_blkno, inode->i_ino);
600 ocfs2_inode_set_new(osb, inode);
601 status = ocfs2_create_new_inode_locks(inode);
605 status = 0; /* error in ocfs2_create_new_inode_locks is not
623 static int ocfs2_mkdir(struct inode *dir,
624 struct dentry *dentry,
629 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
630 dentry->d_name.len, dentry->d_name.name);
631 ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
637 static int ocfs2_create(struct inode *dir,
638 struct dentry *dentry,
640 struct nameidata *nd)
644 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode,
645 dentry->d_name.len, dentry->d_name.name);
646 ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
652 static int ocfs2_link(struct dentry *old_dentry,
654 struct dentry *dentry)
656 struct ocfs2_journal_handle *handle = NULL;
657 struct inode *inode = old_dentry->d_inode;
659 struct buffer_head *fe_bh = NULL;
660 struct buffer_head *parent_fe_bh = NULL;
661 struct buffer_head *de_bh = NULL;
662 struct ocfs2_dinode *fe = NULL;
663 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
665 mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino,
666 old_dentry->d_name.len, old_dentry->d_name.name,
667 dentry->d_name.len, dentry->d_name.name);
669 if (S_ISDIR(inode->i_mode)) {
674 handle = ocfs2_alloc_handle(osb);
675 if (handle == NULL) {
680 err = ocfs2_meta_lock(dir, handle, &parent_fe_bh, 1);
692 err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
697 err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
699 dentry->d_name.len, &de_bh);
705 err = ocfs2_meta_lock(inode, handle, &fe_bh, 1);
712 fe = (struct ocfs2_dinode *) fe_bh->b_data;
713 if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
718 handle = ocfs2_start_trans(osb, handle, OCFS2_LINK_CREDITS);
719 if (IS_ERR(handle)) {
720 err = PTR_ERR(handle);
726 err = ocfs2_journal_access(handle, inode, fe_bh,
727 OCFS2_JOURNAL_ACCESS_WRITE);
734 inode->i_ctime = CURRENT_TIME;
735 fe->i_links_count = cpu_to_le16(inode->i_nlink);
736 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
737 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
739 err = ocfs2_journal_dirty(handle, fe_bh);
741 le16_add_cpu(&fe->i_links_count, -1);
747 err = ocfs2_add_entry(handle, dentry, inode,
748 OCFS2_I(inode)->ip_blkno,
749 parent_fe_bh, de_bh);
751 le16_add_cpu(&fe->i_links_count, -1);
757 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
763 atomic_inc(&inode->i_count);
764 dentry->d_op = &ocfs2_dentry_ops;
765 d_instantiate(dentry, inode);
768 ocfs2_commit_trans(handle);
774 brelse(parent_fe_bh);
782 * Takes and drops an exclusive lock on the given dentry. This will
783 * force other nodes to drop it.
785 static int ocfs2_remote_dentry_delete(struct dentry *dentry)
789 ret = ocfs2_dentry_lock(dentry, 1);
793 ocfs2_dentry_unlock(dentry, 1);
798 static inline int inode_is_unlinkable(struct inode *inode)
800 if (S_ISDIR(inode->i_mode)) {
801 if (inode->i_nlink == 2)
806 if (inode->i_nlink == 1)
811 static int ocfs2_unlink(struct inode *dir,
812 struct dentry *dentry)
815 struct inode *inode = dentry->d_inode;
816 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
818 struct ocfs2_dinode *fe = NULL;
819 struct buffer_head *fe_bh = NULL;
820 struct buffer_head *parent_node_bh = NULL;
821 struct ocfs2_journal_handle *handle = NULL;
822 struct ocfs2_dir_entry *dirent = NULL;
823 struct buffer_head *dirent_bh = NULL;
824 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
825 struct buffer_head *orphan_entry_bh = NULL;
827 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
828 dentry->d_name.len, dentry->d_name.name);
830 BUG_ON(dentry->d_parent->d_inode != dir);
832 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
834 if (inode == osb->root_inode) {
835 mlog(0, "Cannot delete the root directory\n");
840 handle = ocfs2_alloc_handle(osb);
841 if (handle == NULL) {
847 status = ocfs2_meta_lock(dir, handle, &parent_node_bh, 1);
849 if (status != -ENOENT)
854 status = ocfs2_find_files_on_disk(dentry->d_name.name,
855 dentry->d_name.len, &blkno,
856 dir, &dirent_bh, &dirent);
858 if (status != -ENOENT)
863 if (OCFS2_I(inode)->ip_blkno != blkno) {
866 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
867 (unsigned long long)OCFS2_I(inode)->ip_blkno,
868 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
872 status = ocfs2_meta_lock(inode, handle, &fe_bh, 1);
874 if (status != -ENOENT)
879 if (S_ISDIR(inode->i_mode)) {
880 if (!ocfs2_empty_dir(inode)) {
883 } else if (inode->i_nlink != 2) {
889 status = ocfs2_remote_dentry_delete(dentry);
891 /* This vote should succeed under all normal
897 if (inode_is_unlinkable(inode)) {
898 status = ocfs2_prepare_orphan_dir(osb, handle, inode,
907 handle = ocfs2_start_trans(osb, handle, OCFS2_UNLINK_CREDITS);
908 if (IS_ERR(handle)) {
909 status = PTR_ERR(handle);
915 status = ocfs2_journal_access(handle, inode, fe_bh,
916 OCFS2_JOURNAL_ACCESS_WRITE);
922 fe = (struct ocfs2_dinode *) fe_bh->b_data;
924 if (inode_is_unlinkable(inode)) {
925 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
933 /* delete the name from the parent dir */
934 status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
940 if (S_ISDIR(inode->i_mode))
943 fe->i_links_count = cpu_to_le16(inode->i_nlink);
945 status = ocfs2_journal_dirty(handle, fe_bh);
951 if (S_ISDIR(inode->i_mode)) {
953 status = ocfs2_mark_inode_dirty(handle, dir,
963 ocfs2_commit_trans(handle);
972 brelse(parent_node_bh);
975 brelse(orphan_entry_bh);
983 * The only place this should be used is rename!
984 * if they have the same id, then the 1st one is the only one locked.
986 static int ocfs2_double_lock(struct ocfs2_super *osb,
987 struct ocfs2_journal_handle *handle,
988 struct buffer_head **bh1,
989 struct inode *inode1,
990 struct buffer_head **bh2,
991 struct inode *inode2)
994 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
995 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
996 struct buffer_head **tmpbh;
997 struct inode *tmpinode;
999 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
1000 (unsigned long long)oi1->ip_blkno,
1001 (unsigned long long)oi2->ip_blkno);
1010 /* we always want to lock the one with the lower lockid first. */
1011 if (oi1->ip_blkno != oi2->ip_blkno) {
1012 if (oi1->ip_blkno < oi2->ip_blkno) {
1013 /* switch id1 and id2 around */
1014 mlog(0, "switching them around...\n");
1024 status = ocfs2_meta_lock(inode2, handle, bh2, 1);
1026 if (status != -ENOENT)
1032 status = ocfs2_meta_lock(inode1, handle, bh1, 1);
1034 if (status != -ENOENT)
1043 #define PARENT_INO(buffer) \
1044 ((struct ocfs2_dir_entry *) \
1046 le16_to_cpu(((struct ocfs2_dir_entry *)buffer)->rec_len)))->inode
1048 static int ocfs2_rename(struct inode *old_dir,
1049 struct dentry *old_dentry,
1050 struct inode *new_dir,
1051 struct dentry *new_dentry)
1053 int status = 0, rename_lock = 0;
1054 struct inode *old_inode = old_dentry->d_inode;
1055 struct inode *new_inode = new_dentry->d_inode;
1056 struct ocfs2_dinode *newfe = NULL;
1057 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
1058 struct buffer_head *orphan_entry_bh = NULL;
1059 struct buffer_head *newfe_bh = NULL;
1060 struct buffer_head *insert_entry_bh = NULL;
1061 struct ocfs2_super *osb = NULL;
1063 struct ocfs2_journal_handle *handle = NULL;
1064 struct buffer_head *old_dir_bh = NULL;
1065 struct buffer_head *new_dir_bh = NULL;
1066 struct ocfs2_dir_entry *old_de = NULL, *new_de = NULL; // dirent for old_dentry
1068 struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
1069 struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
1070 // this is the 1st dirent bh
1071 nlink_t old_dir_nlink = old_dir->i_nlink, new_dir_nlink = new_dir->i_nlink;
1073 /* At some point it might be nice to break this function up a
1076 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
1077 old_dir, old_dentry, new_dir, new_dentry,
1078 old_dentry->d_name.len, old_dentry->d_name.name,
1079 new_dentry->d_name.len, new_dentry->d_name.name);
1081 osb = OCFS2_SB(old_dir->i_sb);
1084 if (!igrab(new_inode))
1088 if (atomic_read(&old_dentry->d_count) > 2) {
1089 shrink_dcache_parent(old_dentry);
1090 if (atomic_read(&old_dentry->d_count) > 2) {
1096 /* Assume a directory heirarchy thusly:
1099 * a,b,c, and d are all directories.
1101 * from cwd of 'a' on both nodes:
1105 * And that's why, just like the VFS, we need a file system
1107 if (old_dentry != new_dentry) {
1108 status = ocfs2_rename_lock(osb);
1116 handle = ocfs2_alloc_handle(osb);
1117 if (handle == NULL) {
1123 /* if old and new are the same, this'll just do one lock. */
1124 status = ocfs2_double_lock(osb, handle,
1125 &old_dir_bh, old_dir,
1126 &new_dir_bh, new_dir);
1132 /* make sure both dirs have bhs
1133 * get an extra ref on old_dir_bh if old==new */
1136 new_dir_bh = old_dir_bh;
1139 mlog(ML_ERROR, "no old_dir_bh!\n");
1146 * Though we don't require an inode meta data update if
1147 * old_inode is not a directory, we lock anyway here to ensure
1148 * the vote thread on other nodes won't have to concurrently
1149 * downconvert the inode and the dentry locks.
1151 status = ocfs2_meta_lock(old_inode, handle, NULL, 1);
1153 if (status != -ENOENT)
1158 status = ocfs2_remote_dentry_delete(old_dentry);
1164 if (S_ISDIR(old_inode->i_mode)) {
1166 old_inode_de_bh = ocfs2_bread(old_inode, 0, &status, 0);
1167 if (!old_inode_de_bh)
1171 if (le64_to_cpu(PARENT_INO(old_inode_de_bh->b_data)) !=
1172 OCFS2_I(old_dir)->ip_blkno)
1175 if (!new_inode && new_dir!=old_dir &&
1176 new_dir->i_nlink >= OCFS2_LINK_MAX)
1181 old_de_bh = ocfs2_find_entry(old_dentry->d_name.name,
1182 old_dentry->d_name.len,
1188 * Check for inode number is _not_ due to possible IO errors.
1189 * We might rmdir the source, keep it as pwd of some process
1190 * and merrily kill the link to whatever was created under the
1191 * same name. Goodbye sticky bit ;-<
1193 if (le64_to_cpu(old_de->inode) != OCFS2_I(old_inode)->ip_blkno)
1196 /* check if the target already exists (in which case we need
1198 status = ocfs2_find_files_on_disk(new_dentry->d_name.name,
1199 new_dentry->d_name.len,
1200 &newfe_blkno, new_dir, &new_de_bh,
1202 /* The only error we allow here is -ENOENT because the new
1203 * file not existing is perfectly valid. */
1204 if ((status < 0) && (status != -ENOENT)) {
1205 /* If we cannot find the file specified we should just */
1206 /* return the error... */
1211 if (!new_de && new_inode)
1212 mlog(ML_ERROR, "inode %lu does not exist in it's parent "
1213 "directory!", new_inode->i_ino);
1215 /* In case we need to overwrite an existing file, we blow it
1218 /* VFS didn't think there existed an inode here, but
1219 * someone else in the cluster must have raced our
1220 * rename to create one. Today we error cleanly, in
1221 * the future we should consider calling iget to build
1222 * a new struct inode for this entry. */
1226 mlog(0, "We found an inode for name %.*s but VFS "
1227 "didn't give us one.\n", new_dentry->d_name.len,
1228 new_dentry->d_name.name);
1232 if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {
1235 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1236 (unsigned long long)OCFS2_I(new_inode)->ip_blkno,
1237 (unsigned long long)newfe_blkno,
1238 OCFS2_I(new_inode)->ip_flags);
1242 status = ocfs2_meta_lock(new_inode, handle, &newfe_bh, 1);
1244 if (status != -ENOENT)
1249 status = ocfs2_remote_dentry_delete(new_dentry);
1255 newfe = (struct ocfs2_dinode *) newfe_bh->b_data;
1257 mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu "
1258 "newfebh=%p bhblocknr=%llu\n", new_de,
1259 (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ?
1260 (unsigned long long)newfe_bh->b_blocknr : 0ULL);
1262 if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
1263 status = ocfs2_prepare_orphan_dir(osb, handle,
1273 BUG_ON(new_dentry->d_parent->d_inode != new_dir);
1275 status = ocfs2_check_dir_for_entry(new_dir,
1276 new_dentry->d_name.name,
1277 new_dentry->d_name.len);
1281 status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,
1282 new_dentry->d_name.name,
1283 new_dentry->d_name.len,
1291 handle = ocfs2_start_trans(osb, handle, OCFS2_RENAME_CREDITS);
1292 if (IS_ERR(handle)) {
1293 status = PTR_ERR(handle);
1300 if (S_ISDIR(new_inode->i_mode)) {
1301 if (!ocfs2_empty_dir(new_inode) ||
1302 new_inode->i_nlink != 2) {
1303 status = -ENOTEMPTY;
1307 status = ocfs2_journal_access(handle, new_inode, newfe_bh,
1308 OCFS2_JOURNAL_ACCESS_WRITE);
1314 if (S_ISDIR(new_inode->i_mode) ||
1315 (newfe->i_links_count == cpu_to_le16(1))){
1316 status = ocfs2_orphan_add(osb, handle, new_inode,
1325 /* change the dirent to point to the correct inode */
1326 status = ocfs2_journal_access(handle, new_dir, new_de_bh,
1327 OCFS2_JOURNAL_ACCESS_WRITE);
1332 new_de->inode = cpu_to_le64(OCFS2_I(old_inode)->ip_blkno);
1333 new_de->file_type = old_de->file_type;
1334 new_dir->i_version++;
1335 status = ocfs2_journal_dirty(handle, new_de_bh);
1341 if (S_ISDIR(new_inode->i_mode))
1342 newfe->i_links_count = 0;
1344 le16_add_cpu(&newfe->i_links_count, -1);
1346 status = ocfs2_journal_dirty(handle, newfe_bh);
1352 /* if the name was not found in new_dir, add it now */
1353 status = ocfs2_add_entry(handle, new_dentry, old_inode,
1354 OCFS2_I(old_inode)->ip_blkno,
1355 new_dir_bh, insert_entry_bh);
1358 old_inode->i_ctime = CURRENT_TIME;
1359 mark_inode_dirty(old_inode);
1361 /* now that the name has been added to new_dir, remove the old name */
1362 status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);
1369 new_inode->i_nlink--;
1370 new_inode->i_ctime = CURRENT_TIME;
1372 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1373 if (old_inode_de_bh) {
1374 status = ocfs2_journal_access(handle, old_inode,
1376 OCFS2_JOURNAL_ACCESS_WRITE);
1377 PARENT_INO(old_inode_de_bh->b_data) =
1378 cpu_to_le64(OCFS2_I(new_dir)->ip_blkno);
1379 status = ocfs2_journal_dirty(handle, old_inode_de_bh);
1382 new_inode->i_nlink--;
1385 mark_inode_dirty(new_dir);
1388 mark_inode_dirty(old_dir);
1390 mark_inode_dirty(new_inode);
1392 if (old_dir != new_dir)
1393 if (new_dir_nlink != new_dir->i_nlink) {
1395 mlog(ML_ERROR, "need to change nlink for new "
1396 "dir %llu from %d to %d but bh is NULL\n",
1397 (unsigned long long)OCFS2_I(new_dir)->ip_blkno,
1398 (int)new_dir_nlink, new_dir->i_nlink);
1400 struct ocfs2_dinode *fe;
1401 status = ocfs2_journal_access(handle,
1404 OCFS2_JOURNAL_ACCESS_WRITE);
1405 fe = (struct ocfs2_dinode *) new_dir_bh->b_data;
1406 fe->i_links_count = cpu_to_le16(new_dir->i_nlink);
1407 status = ocfs2_journal_dirty(handle, new_dir_bh);
1411 if (old_dir_nlink != old_dir->i_nlink) {
1413 mlog(ML_ERROR, "need to change nlink for old dir "
1414 "%llu from %d to %d but bh is NULL!\n",
1415 (unsigned long long)OCFS2_I(old_dir)->ip_blkno,
1416 (int)old_dir_nlink, old_dir->i_nlink);
1418 struct ocfs2_dinode *fe;
1419 status = ocfs2_journal_access(handle, old_dir,
1421 OCFS2_JOURNAL_ACCESS_WRITE);
1422 fe = (struct ocfs2_dinode *) old_dir_bh->b_data;
1423 fe->i_links_count = cpu_to_le16(old_dir->i_nlink);
1424 status = ocfs2_journal_dirty(handle, old_dir_bh);
1428 ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
1432 ocfs2_rename_unlock(osb);
1435 ocfs2_commit_trans(handle);
1438 sync_mapping_buffers(old_inode->i_mapping);
1452 if (old_inode_de_bh)
1453 brelse(old_inode_de_bh);
1454 if (orphan_entry_bh)
1455 brelse(orphan_entry_bh);
1456 if (insert_entry_bh)
1457 brelse(insert_entry_bh);
1465 * we expect i_size = strlen(symname). Copy symname into the file
1466 * data, including the null terminator.
1468 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1469 struct ocfs2_journal_handle *handle,
1470 struct inode *inode,
1471 const char *symname)
1473 struct buffer_head **bhs = NULL;
1475 struct super_block *sb = osb->sb;
1478 int virtual, blocks, status, i, bytes_left;
1480 bytes_left = i_size_read(inode) + 1;
1481 /* we can't trust i_blocks because we're actually going to
1482 * write i_size + 1 bytes. */
1483 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1485 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1486 (unsigned long long)inode->i_blocks,
1487 i_size_read(inode), blocks);
1489 /* Sanity check -- make sure we're going to fit. */
1491 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1497 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1504 status = ocfs2_extent_map_get_blocks(inode, 0, 1, &p_blkno,
1511 /* links can never be larger than one cluster so we know this
1512 * is all going to be contiguous, but do a sanity check
1514 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1521 while(bytes_left > 0) {
1522 c = &symname[virtual * sb->s_blocksize];
1524 bhs[virtual] = sb_getblk(sb, p_blkno);
1525 if (!bhs[virtual]) {
1530 ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
1532 status = ocfs2_journal_access(handle, inode, bhs[virtual],
1533 OCFS2_JOURNAL_ACCESS_CREATE);
1539 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1541 memcpy(bhs[virtual]->b_data, c,
1542 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1545 status = ocfs2_journal_dirty(handle, bhs[virtual]);
1553 bytes_left -= sb->s_blocksize;
1560 for(i = 0; i < blocks; i++)
1570 static int ocfs2_symlink(struct inode *dir,
1571 struct dentry *dentry,
1572 const char *symname)
1574 int status, l, credits;
1576 struct ocfs2_super *osb = NULL;
1577 struct inode *inode = NULL;
1578 struct super_block *sb;
1579 struct buffer_head *new_fe_bh = NULL;
1580 struct buffer_head *de_bh = NULL;
1581 struct buffer_head *parent_fe_bh = NULL;
1582 struct ocfs2_dinode *fe = NULL;
1583 struct ocfs2_dinode *dirfe;
1584 struct ocfs2_journal_handle *handle = NULL;
1585 struct ocfs2_alloc_context *inode_ac = NULL;
1586 struct ocfs2_alloc_context *data_ac = NULL;
1588 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1589 dentry, symname, dentry->d_name.len, dentry->d_name.name);
1594 l = strlen(symname) + 1;
1596 credits = ocfs2_calc_symlink_credits(sb);
1598 handle = ocfs2_alloc_handle(osb);
1599 if (handle == NULL) {
1605 /* lock the parent directory */
1606 status = ocfs2_meta_lock(dir, handle, &parent_fe_bh, 1);
1608 if (status != -ENOENT)
1613 dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1614 if (!dirfe->i_links_count) {
1615 /* can't make a file in a deleted directory. */
1620 status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
1621 dentry->d_name.len);
1625 status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
1626 dentry->d_name.name,
1627 dentry->d_name.len, &de_bh);
1633 status = ocfs2_reserve_new_inode(osb, handle, &inode_ac);
1635 if (status != -ENOSPC)
1640 /* don't reserve bitmap space for fast symlinks. */
1641 if (l > ocfs2_fast_symlink_chars(sb)) {
1642 status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
1644 if (status != -ENOSPC)
1650 handle = ocfs2_start_trans(osb, handle, credits);
1651 if (IS_ERR(handle)) {
1652 status = PTR_ERR(handle);
1658 status = ocfs2_mknod_locked(osb, dir, dentry,
1659 S_IFLNK | S_IRWXUGO, 0,
1660 &new_fe_bh, parent_fe_bh, handle,
1667 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1670 if (l > ocfs2_fast_symlink_chars(sb)) {
1671 inode->i_op = &ocfs2_symlink_inode_operations;
1672 status = ocfs2_do_extend_allocation(osb, inode, 1, new_fe_bh,
1673 handle, data_ac, NULL,
1676 if (status != -ENOSPC && status != -EINTR) {
1678 "Failed to extend file to %llu\n",
1679 (unsigned long long)newsize);
1685 i_size_write(inode, newsize);
1686 inode->i_blocks = ocfs2_align_bytes_to_sectors(newsize);
1688 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1689 memcpy((char *) fe->id2.i_symlink, symname, l);
1690 i_size_write(inode, newsize);
1691 inode->i_blocks = 0;
1694 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1700 if (!ocfs2_inode_is_fast_symlink(inode)) {
1701 status = ocfs2_create_symlink_data(osb, handle, inode,
1709 status = ocfs2_add_entry(handle, dentry, inode,
1710 le64_to_cpu(fe->i_blkno), parent_fe_bh,
1717 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
1723 insert_inode_hash(inode);
1724 dentry->d_op = &ocfs2_dentry_ops;
1725 d_instantiate(dentry, inode);
1728 ocfs2_commit_trans(handle);
1732 brelse(parent_fe_bh);
1736 ocfs2_free_alloc_context(inode_ac);
1738 ocfs2_free_alloc_context(data_ac);
1739 if ((status < 0) && inode)
1747 int ocfs2_check_dir_entry(struct inode * dir,
1748 struct ocfs2_dir_entry * de,
1749 struct buffer_head * bh,
1750 unsigned long offset)
1752 const char *error_msg = NULL;
1753 const int rlen = le16_to_cpu(de->rec_len);
1755 if (rlen < OCFS2_DIR_REC_LEN(1))
1756 error_msg = "rec_len is smaller than minimal";
1757 else if (rlen % 4 != 0)
1758 error_msg = "rec_len % 4 != 0";
1759 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
1760 error_msg = "rec_len is too small for name_len";
1761 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
1762 error_msg = "directory entry across blocks";
1764 if (error_msg != NULL)
1765 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
1766 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
1767 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
1768 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
1770 return error_msg == NULL ? 1 : 0;
1773 /* we don't always have a dentry for what we want to add, so people
1774 * like orphan dir can call this instead.
1776 * If you pass me insert_bh, I'll skip the search of the other dir
1777 * blocks and put the record in there.
1779 static int __ocfs2_add_entry(struct ocfs2_journal_handle *handle,
1781 const char *name, int namelen,
1782 struct inode *inode, u64 blkno,
1783 struct buffer_head *parent_fe_bh,
1784 struct buffer_head *insert_bh)
1786 unsigned long offset;
1787 unsigned short rec_len;
1788 struct ocfs2_dir_entry *de, *de1;
1789 struct super_block *sb;
1799 rec_len = OCFS2_DIR_REC_LEN(namelen);
1801 de = (struct ocfs2_dir_entry *) insert_bh->b_data;
1803 BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
1804 /* These checks should've already been passed by the
1805 * prepare function, but I guess we can leave them
1807 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
1811 if (ocfs2_match(namelen, name, de)) {
1815 if (((le64_to_cpu(de->inode) == 0) &&
1816 (le16_to_cpu(de->rec_len) >= rec_len)) ||
1817 (le16_to_cpu(de->rec_len) >=
1818 (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
1819 status = ocfs2_journal_access(handle, dir, insert_bh,
1820 OCFS2_JOURNAL_ACCESS_WRITE);
1821 /* By now the buffer is marked for journaling */
1822 offset += le16_to_cpu(de->rec_len);
1823 if (le64_to_cpu(de->inode)) {
1824 de1 = (struct ocfs2_dir_entry *)((char *) de +
1825 OCFS2_DIR_REC_LEN(de->name_len));
1827 cpu_to_le16(le16_to_cpu(de->rec_len) -
1828 OCFS2_DIR_REC_LEN(de->name_len));
1829 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1832 de->file_type = OCFS2_FT_UNKNOWN;
1834 de->inode = cpu_to_le64(blkno);
1835 ocfs2_set_de_type(de, inode->i_mode);
1838 de->name_len = namelen;
1839 memcpy(de->name, name, namelen);
1841 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1843 status = ocfs2_journal_dirty(handle, insert_bh);
1847 offset += le16_to_cpu(de->rec_len);
1848 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
1851 /* when you think about it, the assert above should prevent us
1852 * from ever getting here. */
1862 * ocfs2_delete_entry deletes a directory entry by merging it with the
1865 static int ocfs2_delete_entry(struct ocfs2_journal_handle *handle,
1867 struct ocfs2_dir_entry *de_del,
1868 struct buffer_head *bh)
1870 struct ocfs2_dir_entry *de, *pde;
1871 int i, status = -ENOENT;
1873 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
1877 de = (struct ocfs2_dir_entry *) bh->b_data;
1878 while (i < bh->b_size) {
1879 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
1885 status = ocfs2_journal_access(handle, dir, bh,
1886 OCFS2_JOURNAL_ACCESS_WRITE);
1894 cpu_to_le16(le16_to_cpu(pde->rec_len) +
1895 le16_to_cpu(de->rec_len));
1899 status = ocfs2_journal_dirty(handle, bh);
1902 i += le16_to_cpu(de->rec_len);
1904 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
1912 * Returns 0 if not found, -1 on failure, and 1 on success
1914 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
1916 const char *name, int namelen,
1917 unsigned long offset,
1918 struct ocfs2_dir_entry **res_dir)
1920 struct ocfs2_dir_entry *de;
1921 char *dlimit, *de_buf;
1927 de_buf = bh->b_data;
1928 dlimit = de_buf + dir->i_sb->s_blocksize;
1930 while (de_buf < dlimit) {
1931 /* this code is executed quadratically often */
1932 /* do minimal checking `by hand' */
1934 de = (struct ocfs2_dir_entry *) de_buf;
1936 if (de_buf + namelen <= dlimit &&
1937 ocfs2_match(namelen, name, de)) {
1938 /* found a match - just to be sure, do a full check */
1939 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1948 /* prevent looping on a bad block */
1949 de_len = le16_to_cpu(de->rec_len);
1964 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
1966 struct ocfs2_dir_entry **res_dir)
1968 struct super_block *sb;
1969 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1970 struct buffer_head *bh, *ret = NULL;
1971 unsigned long start, block, b;
1972 int ra_max = 0; /* Number of bh's in the readahead
1974 int ra_ptr = 0; /* Current index into readahead
1977 int nblocks, i, err;
1984 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
1985 start = OCFS2_I(dir)->ip_dir_start_lookup;
1986 if (start >= nblocks)
1993 * We deal with the read-ahead logic here.
1995 if (ra_ptr >= ra_max) {
1996 /* Refill the readahead buffer */
1999 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
2001 * Terminate if we reach the end of the
2002 * directory and must wrap, or if our
2003 * search has finished at this block.
2005 if (b >= nblocks || (num && block == start)) {
2006 bh_use[ra_max] = NULL;
2011 bh = ocfs2_bread(dir, b++, &err, 1);
2012 bh_use[ra_max] = bh;
2015 if ((bh = bh_use[ra_ptr++]) == NULL)
2018 if (!buffer_uptodate(bh)) {
2019 /* read error, skip block & hope for the best */
2020 ocfs2_error(dir->i_sb, "reading directory %llu, "
2022 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2027 i = ocfs2_search_dirblock(bh, dir, name, namelen,
2028 block << sb->s_blocksize_bits,
2031 OCFS2_I(dir)->ip_dir_start_lookup = block;
2033 goto cleanup_and_exit;
2037 goto cleanup_and_exit;
2040 if (++block >= nblocks)
2042 } while (block != start);
2045 * If the directory has grown while we were searching, then
2046 * search the last part of the directory before giving up.
2049 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
2050 if (block < nblocks) {
2056 /* Clean up the read-ahead blocks */
2057 for (; ra_ptr < ra_max; ra_ptr++)
2058 brelse(bh_use[ra_ptr]);
2064 static int ocfs2_blkno_stringify(u64 blkno, char *name)
2066 int status, namelen;
2070 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
2080 if (namelen != OCFS2_ORPHAN_NAMELEN) {
2086 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
2095 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
2096 struct ocfs2_journal_handle *handle,
2097 struct inode *inode,
2099 struct buffer_head **de_bh)
2101 struct inode *orphan_dir_inode = NULL;
2102 struct buffer_head *orphan_dir_bh = NULL;
2105 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2111 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2112 ORPHAN_DIR_SYSTEM_INODE,
2114 if (!orphan_dir_inode) {
2120 ocfs2_handle_add_inode(handle, orphan_dir_inode);
2121 status = ocfs2_meta_lock(orphan_dir_inode, handle, &orphan_dir_bh, 1);
2127 status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
2128 orphan_dir_bh, name,
2129 OCFS2_ORPHAN_NAMELEN, de_bh);
2136 if (orphan_dir_inode)
2137 iput(orphan_dir_inode);
2140 brelse(orphan_dir_bh);
2146 static int ocfs2_orphan_add(struct ocfs2_super *osb,
2147 struct ocfs2_journal_handle *handle,
2148 struct inode *inode,
2149 struct ocfs2_dinode *fe,
2151 struct buffer_head *de_bh)
2153 struct inode *orphan_dir_inode = NULL;
2154 struct buffer_head *orphan_dir_bh = NULL;
2156 struct ocfs2_dinode *orphan_fe;
2158 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
2160 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2161 ORPHAN_DIR_SYSTEM_INODE,
2163 if (!orphan_dir_inode) {
2169 status = ocfs2_read_block(osb,
2170 OCFS2_I(orphan_dir_inode)->ip_blkno,
2171 &orphan_dir_bh, OCFS2_BH_CACHED,
2178 status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
2179 OCFS2_JOURNAL_ACCESS_WRITE);
2185 /* we're a cluster, and nlink can change on disk from
2186 * underneath us... */
2187 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2188 if (S_ISDIR(inode->i_mode))
2189 le16_add_cpu(&orphan_fe->i_links_count, 1);
2190 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2192 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2198 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
2199 OCFS2_ORPHAN_NAMELEN, inode,
2200 OCFS2_I(inode)->ip_blkno,
2201 orphan_dir_bh, de_bh);
2207 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
2209 /* Record which orphan dir our inode now resides
2210 * in. delete_inode will use this to determine which orphan
2212 spin_lock(&OCFS2_I(inode)->ip_lock);
2213 OCFS2_I(inode)->ip_orphaned_slot = osb->slot_num;
2214 spin_unlock(&OCFS2_I(inode)->ip_lock);
2216 mlog(0, "Inode %llu orphaned in slot %d\n",
2217 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
2220 if (orphan_dir_inode)
2221 iput(orphan_dir_inode);
2224 brelse(orphan_dir_bh);
2230 /* unlike orphan_add, we expect the orphan dir to already be locked here. */
2231 int ocfs2_orphan_del(struct ocfs2_super *osb,
2232 struct ocfs2_journal_handle *handle,
2233 struct inode *orphan_dir_inode,
2234 struct inode *inode,
2235 struct buffer_head *orphan_dir_bh)
2237 char name[OCFS2_ORPHAN_NAMELEN + 1];
2238 struct ocfs2_dinode *orphan_fe;
2240 struct buffer_head *target_de_bh = NULL;
2241 struct ocfs2_dir_entry *target_de = NULL;
2245 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2251 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
2252 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
2253 OCFS2_ORPHAN_NAMELEN);
2255 /* find it's spot in the orphan directory */
2256 target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
2257 orphan_dir_inode, &target_de);
2258 if (!target_de_bh) {
2264 /* remove it from the orphan directory */
2265 status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
2272 status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
2273 OCFS2_JOURNAL_ACCESS_WRITE);
2279 /* do the i_nlink dance! :) */
2280 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2281 if (S_ISDIR(inode->i_mode))
2282 le16_add_cpu(&orphan_fe->i_links_count, -1);
2283 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2285 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2293 brelse(target_de_bh);
2299 struct inode_operations ocfs2_dir_iops = {
2300 .create = ocfs2_create,
2301 .lookup = ocfs2_lookup,
2303 .unlink = ocfs2_unlink,
2304 .rmdir = ocfs2_unlink,
2305 .symlink = ocfs2_symlink,
2306 .mkdir = ocfs2_mkdir,
2307 .mknod = ocfs2_mknod,
2308 .rename = ocfs2_rename,
2309 .setattr = ocfs2_setattr,
2310 .getattr = ocfs2_getattr,