1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Create and rename file, directory, symlinks
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 * Portions of this code from linux/fs/ext3/dir.c
12 * Copyright (C) 1992, 1993, 1994, 1995
13 * Remy Card (card@masi.ibp.fr)
14 * Laboratoire MASI - Institut Blaise pascal
15 * Universite Pierre et Marie Curie (Paris VI)
19 * linux/fs/minix/dir.c
21 * Copyright (C) 1991, 1992 Linux Torvalds
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public
25 * License as published by the Free Software Foundation; either
26 * version 2 of the License, or (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 * General Public License for more details.
33 * You should have received a copy of the GNU General Public
34 * License along with this program; if not, write to the
35 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
36 * Boston, MA 021110-1307, USA.
40 #include <linux/types.h>
41 #include <linux/slab.h>
42 #include <linux/highmem.h>
43 #include <linux/quotaops.h>
45 #define MLOG_MASK_PREFIX ML_NAMEI
46 #include <cluster/masklog.h>
54 #include "extent_map.h"
67 #include "buffer_head_io.h"
69 static int ocfs2_mknod_locked(struct ocfs2_super
*osb
,
72 struct dentry
*dentry
,
74 struct buffer_head
**new_fe_bh
,
75 struct buffer_head
*parent_fe_bh
,
77 struct ocfs2_alloc_context
*inode_ac
);
79 static int ocfs2_prepare_orphan_dir(struct ocfs2_super
*osb
,
80 struct inode
**ret_orphan_dir
,
83 struct ocfs2_dir_lookup_result
*lookup
);
85 static int ocfs2_orphan_add(struct ocfs2_super
*osb
,
88 struct ocfs2_dinode
*fe
,
90 struct ocfs2_dir_lookup_result
*lookup
,
91 struct inode
*orphan_dir_inode
);
93 static int ocfs2_create_symlink_data(struct ocfs2_super
*osb
,
98 /* An orphan dir name is an 8 byte value, printed as a hex string */
99 #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
101 static struct dentry
*ocfs2_lookup(struct inode
*dir
, struct dentry
*dentry
,
102 struct nameidata
*nd
)
106 struct inode
*inode
= NULL
;
108 struct ocfs2_inode_info
*oi
;
110 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir
, dentry
,
111 dentry
->d_name
.len
, dentry
->d_name
.name
);
113 if (dentry
->d_name
.len
> OCFS2_MAX_FILENAME_LEN
) {
114 ret
= ERR_PTR(-ENAMETOOLONG
);
118 mlog(0, "find name %.*s in directory %llu\n", dentry
->d_name
.len
,
119 dentry
->d_name
.name
, (unsigned long long)OCFS2_I(dir
)->ip_blkno
);
121 status
= ocfs2_inode_lock(dir
, NULL
, 0);
123 if (status
!= -ENOENT
)
125 ret
= ERR_PTR(status
);
129 status
= ocfs2_lookup_ino_from_name(dir
, dentry
->d_name
.name
,
130 dentry
->d_name
.len
, &blkno
);
134 inode
= ocfs2_iget(OCFS2_SB(dir
->i_sb
), blkno
, 0, 0);
136 ret
= ERR_PTR(-EACCES
);
141 /* Clear any orphaned state... If we were able to look up the
142 * inode from a directory, it certainly can't be orphaned. We
143 * might have the bad state from a node which intended to
144 * orphan this inode but crashed before it could commit the
146 spin_lock(&oi
->ip_lock
);
147 oi
->ip_flags
&= ~OCFS2_INODE_MAYBE_ORPHANED
;
148 spin_unlock(&oi
->ip_lock
);
151 dentry
->d_op
= &ocfs2_dentry_ops
;
152 ret
= d_splice_alias(inode
, dentry
);
156 * If d_splice_alias() finds a DCACHE_DISCONNECTED
157 * dentry, it will d_move() it on top of ourse. The
158 * return value will indicate this however, so in
159 * those cases, we switch them around for the locking
162 * NOTE: This dentry already has ->d_op set from
163 * ocfs2_get_parent() and ocfs2_get_dentry()
168 status
= ocfs2_dentry_attach_lock(dentry
, inode
,
169 OCFS2_I(dir
)->ip_blkno
);
172 ret
= ERR_PTR(status
);
178 /* Don't drop the cluster lock until *after* the d_add --
179 * unlink on another node will message us to remove that
180 * dentry under this lock so otherwise we can race this with
181 * the downconvert thread and have a stale dentry. */
182 ocfs2_inode_unlock(dir
, 0);
191 static struct inode
*ocfs2_get_init_inode(struct inode
*dir
, int mode
)
195 inode
= new_inode(dir
->i_sb
);
197 mlog(ML_ERROR
, "new_inode failed!\n");
201 /* populate as many fields early on as possible - many of
202 * these are used by the support functions here and in
208 inode
->i_uid
= current_fsuid();
209 if (dir
->i_mode
& S_ISGID
) {
210 inode
->i_gid
= dir
->i_gid
;
214 inode
->i_gid
= current_fsgid();
215 inode
->i_mode
= mode
;
220 static int ocfs2_mknod(struct inode
*dir
,
221 struct dentry
*dentry
,
226 struct buffer_head
*parent_fe_bh
= NULL
;
227 handle_t
*handle
= NULL
;
228 struct ocfs2_super
*osb
;
229 struct ocfs2_dinode
*dirfe
;
230 struct buffer_head
*new_fe_bh
= NULL
;
231 struct inode
*inode
= NULL
;
232 struct ocfs2_alloc_context
*inode_ac
= NULL
;
233 struct ocfs2_alloc_context
*data_ac
= NULL
;
234 struct ocfs2_alloc_context
*meta_ac
= NULL
;
235 int want_clusters
= 0;
237 int xattr_credits
= 0;
238 struct ocfs2_security_xattr_info si
= {
241 int did_quota_inode
= 0;
242 struct ocfs2_dir_lookup_result lookup
= { NULL
, };
244 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir
, dentry
, mode
,
245 (unsigned long)dev
, dentry
->d_name
.len
,
246 dentry
->d_name
.name
);
248 /* get our super block */
249 osb
= OCFS2_SB(dir
->i_sb
);
251 status
= ocfs2_inode_lock(dir
, &parent_fe_bh
, 1);
253 if (status
!= -ENOENT
)
258 if (S_ISDIR(mode
) && (dir
->i_nlink
>= OCFS2_LINK_MAX
)) {
263 dirfe
= (struct ocfs2_dinode
*) parent_fe_bh
->b_data
;
264 if (!dirfe
->i_links_count
) {
265 /* can't make a file in a deleted directory. */
270 status
= ocfs2_check_dir_for_entry(dir
, dentry
->d_name
.name
,
275 /* get a spot inside the dir. */
276 status
= ocfs2_prepare_dir_for_insert(osb
, dir
, parent_fe_bh
,
278 dentry
->d_name
.len
, &lookup
);
284 /* reserve an inode spot */
285 status
= ocfs2_reserve_new_inode(osb
, &inode_ac
);
287 if (status
!= -ENOSPC
)
292 inode
= ocfs2_get_init_inode(dir
, mode
);
299 /* get security xattr */
300 status
= ocfs2_init_security_get(inode
, dir
, &si
);
302 if (status
== -EOPNOTSUPP
)
310 /* calculate meta data/clusters for setting security and acl xattr */
311 status
= ocfs2_calc_xattr_init(dir
, parent_fe_bh
, mode
,
313 &xattr_credits
, &want_meta
);
319 /* Reserve a cluster if creating an extent based directory. */
320 if (S_ISDIR(mode
) && !ocfs2_supports_inline_data(osb
)) {
323 /* Dir indexing requires extra space as well */
324 if (ocfs2_supports_indexed_dirs(osb
)) {
330 status
= ocfs2_reserve_new_metadata_blocks(osb
, want_meta
, &meta_ac
);
332 if (status
!= -ENOSPC
)
337 status
= ocfs2_reserve_clusters(osb
, want_clusters
, &data_ac
);
339 if (status
!= -ENOSPC
)
344 handle
= ocfs2_start_trans(osb
, ocfs2_mknod_credits(osb
->sb
,
347 if (IS_ERR(handle
)) {
348 status
= PTR_ERR(handle
);
354 /* We don't use standard VFS wrapper because we don't want vfs_dq_init
356 if (sb_any_quota_active(osb
->sb
) &&
357 osb
->sb
->dq_op
->alloc_inode(inode
, 1) == NO_QUOTA
) {
363 /* do the real work now. */
364 status
= ocfs2_mknod_locked(osb
, dir
, inode
, dentry
, dev
,
365 &new_fe_bh
, parent_fe_bh
, handle
,
373 status
= ocfs2_fill_new_dir(osb
, handle
, dir
, inode
,
374 new_fe_bh
, data_ac
, meta_ac
);
380 status
= ocfs2_journal_access_di(handle
, dir
, parent_fe_bh
,
381 OCFS2_JOURNAL_ACCESS_WRITE
);
386 le16_add_cpu(&dirfe
->i_links_count
, 1);
387 status
= ocfs2_journal_dirty(handle
, parent_fe_bh
);
395 status
= ocfs2_init_acl(handle
, inode
, dir
, new_fe_bh
, parent_fe_bh
,
403 status
= ocfs2_init_security_set(handle
, inode
, new_fe_bh
, &si
,
411 status
= ocfs2_add_entry(handle
, dentry
, inode
,
412 OCFS2_I(inode
)->ip_blkno
, parent_fe_bh
,
419 status
= ocfs2_dentry_attach_lock(dentry
, inode
,
420 OCFS2_I(dir
)->ip_blkno
);
426 insert_inode_hash(inode
);
427 dentry
->d_op
= &ocfs2_dentry_ops
;
428 d_instantiate(dentry
, inode
);
431 if (status
< 0 && did_quota_inode
)
432 vfs_dq_free_inode(inode
);
434 ocfs2_commit_trans(osb
, handle
);
436 ocfs2_inode_unlock(dir
, 1);
438 if (status
== -ENOSPC
)
439 mlog(0, "Disk is full\n");
442 brelse(parent_fe_bh
);
446 ocfs2_free_dir_lookup_result(&lookup
);
448 if ((status
< 0) && inode
) {
454 ocfs2_free_alloc_context(inode_ac
);
457 ocfs2_free_alloc_context(data_ac
);
460 ocfs2_free_alloc_context(meta_ac
);
467 static int ocfs2_mknod_locked(struct ocfs2_super
*osb
,
470 struct dentry
*dentry
,
472 struct buffer_head
**new_fe_bh
,
473 struct buffer_head
*parent_fe_bh
,
475 struct ocfs2_alloc_context
*inode_ac
)
478 struct ocfs2_dinode
*fe
= NULL
;
479 struct ocfs2_extent_list
*fel
;
484 mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir
, dentry
,
485 inode
->i_mode
, (unsigned long)dev
, dentry
->d_name
.len
,
486 dentry
->d_name
.name
);
490 status
= ocfs2_claim_new_inode(osb
, handle
, inode_ac
, &suballoc_bit
,
497 /* populate as many fields early on as possible - many of
498 * these are used by the support functions here and in
500 inode
->i_ino
= ino_from_blkno(osb
->sb
, fe_blkno
);
501 OCFS2_I(inode
)->ip_blkno
= fe_blkno
;
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_di(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(inode_ac
->ac_alloc_slot
);
529 fe
->i_uid
= cpu_to_le32(inode
->i_uid
);
530 fe
->i_gid
= cpu_to_le32(inode
->i_gid
);
531 fe
->i_mode
= cpu_to_le16(inode
->i_mode
);
532 if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
))
533 fe
->id1
.dev1
.i_rdev
= cpu_to_le64(huge_encode_dev(dev
));
534 fe
->i_links_count
= cpu_to_le16(inode
->i_nlink
);
536 fe
->i_last_eb_blk
= 0;
537 strcpy(fe
->i_signature
, OCFS2_INODE_SIGNATURE
);
538 le32_add_cpu(&fe
->i_flags
, OCFS2_VALID_FL
);
539 fe
->i_atime
= fe
->i_ctime
= fe
->i_mtime
=
540 cpu_to_le64(CURRENT_TIME
.tv_sec
);
541 fe
->i_mtime_nsec
= fe
->i_ctime_nsec
= fe
->i_atime_nsec
=
542 cpu_to_le32(CURRENT_TIME
.tv_nsec
);
546 * If supported, directories start with inline data. If inline
547 * isn't supported, but indexing is, we start them as indexed.
549 feat
= le16_to_cpu(fe
->i_dyn_features
);
550 if (S_ISDIR(inode
->i_mode
) && ocfs2_supports_inline_data(osb
)) {
551 fe
->i_dyn_features
= cpu_to_le16(feat
| OCFS2_INLINE_DATA_FL
);
553 fe
->id2
.i_data
.id_count
= cpu_to_le16(
554 ocfs2_max_inline_data_with_xattr(osb
->sb
, fe
));
556 fel
= &fe
->id2
.i_list
;
557 fel
->l_tree_depth
= 0;
558 fel
->l_next_free_rec
= 0;
559 fel
->l_count
= cpu_to_le16(ocfs2_extent_recs_per_inode(osb
->sb
));
562 status
= ocfs2_journal_dirty(handle
, *new_fe_bh
);
568 ocfs2_populate_inode(inode
, fe
, 1);
569 ocfs2_inode_set_new(osb
, inode
);
570 if (!ocfs2_mount_local(osb
)) {
571 status
= ocfs2_create_new_inode_locks(inode
);
576 status
= 0; /* error in ocfs2_create_new_inode_locks is not
591 static int ocfs2_mkdir(struct inode
*dir
,
592 struct dentry
*dentry
,
597 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir
, dentry
, mode
,
598 dentry
->d_name
.len
, dentry
->d_name
.name
);
599 ret
= ocfs2_mknod(dir
, dentry
, mode
| S_IFDIR
, 0);
605 static int ocfs2_create(struct inode
*dir
,
606 struct dentry
*dentry
,
608 struct nameidata
*nd
)
612 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir
, dentry
, mode
,
613 dentry
->d_name
.len
, dentry
->d_name
.name
);
614 ret
= ocfs2_mknod(dir
, dentry
, mode
| S_IFREG
, 0);
620 static int ocfs2_link(struct dentry
*old_dentry
,
622 struct dentry
*dentry
)
625 struct inode
*inode
= old_dentry
->d_inode
;
627 struct buffer_head
*fe_bh
= NULL
;
628 struct buffer_head
*parent_fe_bh
= NULL
;
629 struct ocfs2_dinode
*fe
= NULL
;
630 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
631 struct ocfs2_dir_lookup_result lookup
= { NULL
, };
633 mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode
->i_ino
,
634 old_dentry
->d_name
.len
, old_dentry
->d_name
.name
,
635 dentry
->d_name
.len
, dentry
->d_name
.name
);
637 if (S_ISDIR(inode
->i_mode
))
640 err
= ocfs2_inode_lock(dir
, &parent_fe_bh
, 1);
652 err
= ocfs2_check_dir_for_entry(dir
, dentry
->d_name
.name
,
657 err
= ocfs2_prepare_dir_for_insert(osb
, dir
, parent_fe_bh
,
659 dentry
->d_name
.len
, &lookup
);
665 err
= ocfs2_inode_lock(inode
, &fe_bh
, 1);
672 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
673 if (le16_to_cpu(fe
->i_links_count
) >= OCFS2_LINK_MAX
) {
675 goto out_unlock_inode
;
678 handle
= ocfs2_start_trans(osb
, ocfs2_link_credits(osb
->sb
));
679 if (IS_ERR(handle
)) {
680 err
= PTR_ERR(handle
);
683 goto out_unlock_inode
;
686 err
= ocfs2_journal_access_di(handle
, inode
, fe_bh
,
687 OCFS2_JOURNAL_ACCESS_WRITE
);
694 inode
->i_ctime
= CURRENT_TIME
;
695 fe
->i_links_count
= cpu_to_le16(inode
->i_nlink
);
696 fe
->i_ctime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
697 fe
->i_ctime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
699 err
= ocfs2_journal_dirty(handle
, fe_bh
);
701 le16_add_cpu(&fe
->i_links_count
, -1);
707 err
= ocfs2_add_entry(handle
, dentry
, inode
,
708 OCFS2_I(inode
)->ip_blkno
,
709 parent_fe_bh
, &lookup
);
711 le16_add_cpu(&fe
->i_links_count
, -1);
717 err
= ocfs2_dentry_attach_lock(dentry
, inode
, OCFS2_I(dir
)->ip_blkno
);
723 atomic_inc(&inode
->i_count
);
724 dentry
->d_op
= &ocfs2_dentry_ops
;
725 d_instantiate(dentry
, inode
);
728 ocfs2_commit_trans(osb
, handle
);
730 ocfs2_inode_unlock(inode
, 1);
733 ocfs2_inode_unlock(dir
, 1);
736 brelse(parent_fe_bh
);
738 ocfs2_free_dir_lookup_result(&lookup
);
746 * Takes and drops an exclusive lock on the given dentry. This will
747 * force other nodes to drop it.
749 static int ocfs2_remote_dentry_delete(struct dentry
*dentry
)
753 ret
= ocfs2_dentry_lock(dentry
, 1);
757 ocfs2_dentry_unlock(dentry
, 1);
762 static inline int inode_is_unlinkable(struct inode
*inode
)
764 if (S_ISDIR(inode
->i_mode
)) {
765 if (inode
->i_nlink
== 2)
770 if (inode
->i_nlink
== 1)
775 static int ocfs2_unlink(struct inode
*dir
,
776 struct dentry
*dentry
)
779 int child_locked
= 0;
780 struct inode
*inode
= dentry
->d_inode
;
781 struct inode
*orphan_dir
= NULL
;
782 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
784 struct ocfs2_dinode
*fe
= NULL
;
785 struct buffer_head
*fe_bh
= NULL
;
786 struct buffer_head
*parent_node_bh
= NULL
;
787 handle_t
*handle
= NULL
;
788 char orphan_name
[OCFS2_ORPHAN_NAMELEN
+ 1];
789 struct ocfs2_dir_lookup_result lookup
= { NULL
, };
790 struct ocfs2_dir_lookup_result orphan_insert
= { NULL
, };
792 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir
, dentry
,
793 dentry
->d_name
.len
, dentry
->d_name
.name
);
795 BUG_ON(dentry
->d_parent
->d_inode
!= dir
);
797 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
799 if (inode
== osb
->root_inode
) {
800 mlog(0, "Cannot delete the root directory\n");
804 status
= ocfs2_inode_lock(dir
, &parent_node_bh
, 1);
806 if (status
!= -ENOENT
)
811 status
= ocfs2_find_files_on_disk(dentry
->d_name
.name
,
812 dentry
->d_name
.len
, &blkno
, dir
,
815 if (status
!= -ENOENT
)
820 if (OCFS2_I(inode
)->ip_blkno
!= blkno
) {
823 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
824 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
825 (unsigned long long)blkno
, OCFS2_I(inode
)->ip_flags
);
829 status
= ocfs2_inode_lock(inode
, &fe_bh
, 1);
831 if (status
!= -ENOENT
)
837 if (S_ISDIR(inode
->i_mode
)) {
838 if (!ocfs2_empty_dir(inode
)) {
841 } else if (inode
->i_nlink
!= 2) {
847 status
= ocfs2_remote_dentry_delete(dentry
);
849 /* This remote delete should succeed under all normal
855 if (inode_is_unlinkable(inode
)) {
856 status
= ocfs2_prepare_orphan_dir(osb
, &orphan_dir
, inode
,
857 orphan_name
, &orphan_insert
);
864 handle
= ocfs2_start_trans(osb
, ocfs2_unlink_credits(osb
->sb
));
865 if (IS_ERR(handle
)) {
866 status
= PTR_ERR(handle
);
872 status
= ocfs2_journal_access_di(handle
, inode
, fe_bh
,
873 OCFS2_JOURNAL_ACCESS_WRITE
);
879 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
881 if (inode_is_unlinkable(inode
)) {
882 status
= ocfs2_orphan_add(osb
, handle
, inode
, fe
, orphan_name
,
883 &orphan_insert
, orphan_dir
);
890 /* delete the name from the parent dir */
891 status
= ocfs2_delete_entry(handle
, dir
, &lookup
);
897 if (S_ISDIR(inode
->i_mode
))
900 fe
->i_links_count
= cpu_to_le16(inode
->i_nlink
);
902 status
= ocfs2_journal_dirty(handle
, fe_bh
);
908 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
909 if (S_ISDIR(inode
->i_mode
))
912 status
= ocfs2_mark_inode_dirty(handle
, dir
, parent_node_bh
);
915 if (S_ISDIR(inode
->i_mode
))
921 ocfs2_commit_trans(osb
, handle
);
924 ocfs2_inode_unlock(inode
, 1);
926 ocfs2_inode_unlock(dir
, 1);
929 /* This was locked for us in ocfs2_prepare_orphan_dir() */
930 ocfs2_inode_unlock(orphan_dir
, 1);
931 mutex_unlock(&orphan_dir
->i_mutex
);
936 brelse(parent_node_bh
);
938 ocfs2_free_dir_lookup_result(&orphan_insert
);
939 ocfs2_free_dir_lookup_result(&lookup
);
947 * The only place this should be used is rename!
948 * if they have the same id, then the 1st one is the only one locked.
950 static int ocfs2_double_lock(struct ocfs2_super
*osb
,
951 struct buffer_head
**bh1
,
952 struct inode
*inode1
,
953 struct buffer_head
**bh2
,
954 struct inode
*inode2
)
957 struct ocfs2_inode_info
*oi1
= OCFS2_I(inode1
);
958 struct ocfs2_inode_info
*oi2
= OCFS2_I(inode2
);
959 struct buffer_head
**tmpbh
;
960 struct inode
*tmpinode
;
962 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
963 (unsigned long long)oi1
->ip_blkno
,
964 (unsigned long long)oi2
->ip_blkno
);
971 /* we always want to lock the one with the lower lockid first. */
972 if (oi1
->ip_blkno
!= oi2
->ip_blkno
) {
973 if (oi1
->ip_blkno
< oi2
->ip_blkno
) {
974 /* switch id1 and id2 around */
975 mlog(0, "switching them around...\n");
985 status
= ocfs2_inode_lock(inode2
, bh2
, 1);
987 if (status
!= -ENOENT
)
994 status
= ocfs2_inode_lock(inode1
, bh1
, 1);
997 * An error return must mean that no cluster locks
998 * were held on function exit.
1000 if (oi1
->ip_blkno
!= oi2
->ip_blkno
)
1001 ocfs2_inode_unlock(inode2
, 1);
1003 if (status
!= -ENOENT
)
1012 static void ocfs2_double_unlock(struct inode
*inode1
, struct inode
*inode2
)
1014 ocfs2_inode_unlock(inode1
, 1);
1016 if (inode1
!= inode2
)
1017 ocfs2_inode_unlock(inode2
, 1);
1020 static int ocfs2_rename(struct inode
*old_dir
,
1021 struct dentry
*old_dentry
,
1022 struct inode
*new_dir
,
1023 struct dentry
*new_dentry
)
1025 int status
= 0, rename_lock
= 0, parents_locked
= 0, target_exists
= 0;
1026 int old_child_locked
= 0, new_child_locked
= 0, update_dot_dot
= 0;
1027 struct inode
*old_inode
= old_dentry
->d_inode
;
1028 struct inode
*new_inode
= new_dentry
->d_inode
;
1029 struct inode
*orphan_dir
= NULL
;
1030 struct ocfs2_dinode
*newfe
= NULL
;
1031 char orphan_name
[OCFS2_ORPHAN_NAMELEN
+ 1];
1032 struct buffer_head
*orphan_entry_bh
= NULL
;
1033 struct buffer_head
*newfe_bh
= NULL
;
1034 struct buffer_head
*old_inode_bh
= NULL
;
1035 struct buffer_head
*insert_entry_bh
= NULL
;
1036 struct ocfs2_super
*osb
= NULL
;
1037 u64 newfe_blkno
, old_de_ino
;
1038 handle_t
*handle
= NULL
;
1039 struct buffer_head
*old_dir_bh
= NULL
;
1040 struct buffer_head
*new_dir_bh
= NULL
;
1041 nlink_t old_dir_nlink
= old_dir
->i_nlink
;
1042 struct ocfs2_dinode
*old_di
;
1043 struct ocfs2_dir_lookup_result old_inode_dot_dot_res
= { NULL
, };
1044 struct ocfs2_dir_lookup_result target_lookup_res
= { NULL
, };
1045 struct ocfs2_dir_lookup_result old_entry_lookup
= { NULL
, };
1046 struct ocfs2_dir_lookup_result orphan_insert
= { NULL
, };
1047 struct ocfs2_dir_lookup_result target_insert
= { NULL
, };
1049 /* At some point it might be nice to break this function up a
1052 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
1053 old_dir
, old_dentry
, new_dir
, new_dentry
,
1054 old_dentry
->d_name
.len
, old_dentry
->d_name
.name
,
1055 new_dentry
->d_name
.len
, new_dentry
->d_name
.name
);
1057 osb
= OCFS2_SB(old_dir
->i_sb
);
1060 if (!igrab(new_inode
))
1064 /* Assume a directory hierarchy thusly:
1067 * a,b,c, and d are all directories.
1069 * from cwd of 'a' on both nodes:
1073 * And that's why, just like the VFS, we need a file system
1075 if (old_dir
!= new_dir
&& S_ISDIR(old_inode
->i_mode
)) {
1076 status
= ocfs2_rename_lock(osb
);
1084 /* if old and new are the same, this'll just do one lock. */
1085 status
= ocfs2_double_lock(osb
, &old_dir_bh
, old_dir
,
1086 &new_dir_bh
, new_dir
);
1093 /* make sure both dirs have bhs
1094 * get an extra ref on old_dir_bh if old==new */
1097 new_dir_bh
= old_dir_bh
;
1100 mlog(ML_ERROR
, "no old_dir_bh!\n");
1107 * Aside from allowing a meta data update, the locking here
1108 * also ensures that the downconvert thread on other nodes
1109 * won't have to concurrently downconvert the inode and the
1112 status
= ocfs2_inode_lock(old_inode
, &old_inode_bh
, 1);
1114 if (status
!= -ENOENT
)
1118 old_child_locked
= 1;
1120 status
= ocfs2_remote_dentry_delete(old_dentry
);
1126 if (S_ISDIR(old_inode
->i_mode
)) {
1127 u64 old_inode_parent
;
1130 status
= ocfs2_find_files_on_disk("..", 2, &old_inode_parent
,
1132 &old_inode_dot_dot_res
);
1138 if (old_inode_parent
!= OCFS2_I(old_dir
)->ip_blkno
) {
1143 if (!new_inode
&& new_dir
!= old_dir
&&
1144 new_dir
->i_nlink
>= OCFS2_LINK_MAX
) {
1150 status
= ocfs2_lookup_ino_from_name(old_dir
, old_dentry
->d_name
.name
,
1151 old_dentry
->d_name
.len
,
1159 * Check for inode number is _not_ due to possible IO errors.
1160 * We might rmdir the source, keep it as pwd of some process
1161 * and merrily kill the link to whatever was created under the
1162 * same name. Goodbye sticky bit ;-<
1164 if (old_de_ino
!= OCFS2_I(old_inode
)->ip_blkno
) {
1169 /* check if the target already exists (in which case we need
1171 status
= ocfs2_find_files_on_disk(new_dentry
->d_name
.name
,
1172 new_dentry
->d_name
.len
,
1173 &newfe_blkno
, new_dir
,
1174 &target_lookup_res
);
1175 /* The only error we allow here is -ENOENT because the new
1176 * file not existing is perfectly valid. */
1177 if ((status
< 0) && (status
!= -ENOENT
)) {
1178 /* If we cannot find the file specified we should just */
1179 /* return the error... */
1186 if (!target_exists
&& new_inode
) {
1188 * Target was unlinked by another node while we were
1189 * waiting to get to ocfs2_rename(). There isn't
1190 * anything we can do here to help the situation, so
1191 * bubble up the appropriate error.
1197 /* In case we need to overwrite an existing file, we blow it
1199 if (target_exists
) {
1200 /* VFS didn't think there existed an inode here, but
1201 * someone else in the cluster must have raced our
1202 * rename to create one. Today we error cleanly, in
1203 * the future we should consider calling iget to build
1204 * a new struct inode for this entry. */
1208 mlog(0, "We found an inode for name %.*s but VFS "
1209 "didn't give us one.\n", new_dentry
->d_name
.len
,
1210 new_dentry
->d_name
.name
);
1214 if (OCFS2_I(new_inode
)->ip_blkno
!= newfe_blkno
) {
1217 mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n",
1218 (unsigned long long)OCFS2_I(new_inode
)->ip_blkno
,
1219 (unsigned long long)newfe_blkno
,
1220 OCFS2_I(new_inode
)->ip_flags
);
1224 status
= ocfs2_inode_lock(new_inode
, &newfe_bh
, 1);
1226 if (status
!= -ENOENT
)
1230 new_child_locked
= 1;
1232 status
= ocfs2_remote_dentry_delete(new_dentry
);
1238 newfe
= (struct ocfs2_dinode
*) newfe_bh
->b_data
;
1240 mlog(0, "aha rename over existing... new_blkno=%llu "
1241 "newfebh=%p bhblocknr=%llu\n",
1242 (unsigned long long)newfe_blkno
, newfe_bh
, newfe_bh
?
1243 (unsigned long long)newfe_bh
->b_blocknr
: 0ULL);
1245 if (S_ISDIR(new_inode
->i_mode
) || (new_inode
->i_nlink
== 1)) {
1246 status
= ocfs2_prepare_orphan_dir(osb
, &orphan_dir
,
1256 BUG_ON(new_dentry
->d_parent
->d_inode
!= new_dir
);
1258 status
= ocfs2_check_dir_for_entry(new_dir
,
1259 new_dentry
->d_name
.name
,
1260 new_dentry
->d_name
.len
);
1264 status
= ocfs2_prepare_dir_for_insert(osb
, new_dir
, new_dir_bh
,
1265 new_dentry
->d_name
.name
,
1266 new_dentry
->d_name
.len
,
1274 handle
= ocfs2_start_trans(osb
, ocfs2_rename_credits(osb
->sb
));
1275 if (IS_ERR(handle
)) {
1276 status
= PTR_ERR(handle
);
1282 if (target_exists
) {
1283 if (S_ISDIR(new_inode
->i_mode
)) {
1284 if (!ocfs2_empty_dir(new_inode
) ||
1285 new_inode
->i_nlink
!= 2) {
1286 status
= -ENOTEMPTY
;
1290 status
= ocfs2_journal_access_di(handle
, new_inode
, newfe_bh
,
1291 OCFS2_JOURNAL_ACCESS_WRITE
);
1297 if (S_ISDIR(new_inode
->i_mode
) ||
1298 (newfe
->i_links_count
== cpu_to_le16(1))){
1299 status
= ocfs2_orphan_add(osb
, handle
, new_inode
,
1301 &orphan_insert
, orphan_dir
);
1308 /* change the dirent to point to the correct inode */
1309 status
= ocfs2_update_entry(new_dir
, handle
, &target_lookup_res
,
1315 new_dir
->i_version
++;
1317 if (S_ISDIR(new_inode
->i_mode
))
1318 newfe
->i_links_count
= 0;
1320 le16_add_cpu(&newfe
->i_links_count
, -1);
1322 status
= ocfs2_journal_dirty(handle
, newfe_bh
);
1328 /* if the name was not found in new_dir, add it now */
1329 status
= ocfs2_add_entry(handle
, new_dentry
, old_inode
,
1330 OCFS2_I(old_inode
)->ip_blkno
,
1331 new_dir_bh
, &target_insert
);
1334 old_inode
->i_ctime
= CURRENT_TIME
;
1335 mark_inode_dirty(old_inode
);
1337 status
= ocfs2_journal_access_di(handle
, old_inode
, old_inode_bh
,
1338 OCFS2_JOURNAL_ACCESS_WRITE
);
1340 old_di
= (struct ocfs2_dinode
*) old_inode_bh
->b_data
;
1342 old_di
->i_ctime
= cpu_to_le64(old_inode
->i_ctime
.tv_sec
);
1343 old_di
->i_ctime_nsec
= cpu_to_le32(old_inode
->i_ctime
.tv_nsec
);
1345 status
= ocfs2_journal_dirty(handle
, old_inode_bh
);
1352 * Now that the name has been added to new_dir, remove the old name.
1354 * We don't keep any directory entry context around until now
1355 * because the insert might have changed the type of directory
1356 * we're dealing with.
1358 status
= ocfs2_find_entry(old_dentry
->d_name
.name
,
1359 old_dentry
->d_name
.len
, old_dir
,
1364 status
= ocfs2_delete_entry(handle
, old_dir
, &old_entry_lookup
);
1371 new_inode
->i_nlink
--;
1372 new_inode
->i_ctime
= CURRENT_TIME
;
1374 old_dir
->i_ctime
= old_dir
->i_mtime
= CURRENT_TIME
;
1376 if (update_dot_dot
) {
1377 status
= ocfs2_update_entry(old_inode
, handle
,
1378 &old_inode_dot_dot_res
, new_dir
);
1381 new_inode
->i_nlink
--;
1384 mark_inode_dirty(new_dir
);
1387 mark_inode_dirty(old_dir
);
1388 ocfs2_mark_inode_dirty(handle
, old_dir
, old_dir_bh
);
1390 mark_inode_dirty(new_inode
);
1391 ocfs2_mark_inode_dirty(handle
, new_inode
, newfe_bh
);
1394 if (old_dir
!= new_dir
) {
1395 /* Keep the same times on both directories.*/
1396 new_dir
->i_ctime
= new_dir
->i_mtime
= old_dir
->i_ctime
;
1399 * This will also pick up the i_nlink change from the
1402 ocfs2_mark_inode_dirty(handle
, new_dir
, new_dir_bh
);
1405 if (old_dir_nlink
!= old_dir
->i_nlink
) {
1407 mlog(ML_ERROR
, "need to change nlink for old dir "
1408 "%llu from %d to %d but bh is NULL!\n",
1409 (unsigned long long)OCFS2_I(old_dir
)->ip_blkno
,
1410 (int)old_dir_nlink
, old_dir
->i_nlink
);
1412 struct ocfs2_dinode
*fe
;
1413 status
= ocfs2_journal_access_di(handle
, old_dir
,
1415 OCFS2_JOURNAL_ACCESS_WRITE
);
1416 fe
= (struct ocfs2_dinode
*) old_dir_bh
->b_data
;
1417 fe
->i_links_count
= cpu_to_le16(old_dir
->i_nlink
);
1418 status
= ocfs2_journal_dirty(handle
, old_dir_bh
);
1422 ocfs2_dentry_move(old_dentry
, new_dentry
, old_dir
, new_dir
);
1426 ocfs2_rename_unlock(osb
);
1429 ocfs2_commit_trans(osb
, handle
);
1432 ocfs2_double_unlock(old_dir
, new_dir
);
1434 if (old_child_locked
)
1435 ocfs2_inode_unlock(old_inode
, 1);
1437 if (new_child_locked
)
1438 ocfs2_inode_unlock(new_inode
, 1);
1441 /* This was locked for us in ocfs2_prepare_orphan_dir() */
1442 ocfs2_inode_unlock(orphan_dir
, 1);
1443 mutex_unlock(&orphan_dir
->i_mutex
);
1448 sync_mapping_buffers(old_inode
->i_mapping
);
1453 ocfs2_free_dir_lookup_result(&target_lookup_res
);
1454 ocfs2_free_dir_lookup_result(&old_entry_lookup
);
1455 ocfs2_free_dir_lookup_result(&old_inode_dot_dot_res
);
1456 ocfs2_free_dir_lookup_result(&orphan_insert
);
1457 ocfs2_free_dir_lookup_result(&target_insert
);
1460 brelse(old_inode_bh
);
1463 brelse(orphan_entry_bh
);
1464 brelse(insert_entry_bh
);
1472 * we expect i_size = strlen(symname). Copy symname into the file
1473 * data, including the null terminator.
1475 static int ocfs2_create_symlink_data(struct ocfs2_super
*osb
,
1477 struct inode
*inode
,
1478 const char *symname
)
1480 struct buffer_head
**bhs
= NULL
;
1482 struct super_block
*sb
= osb
->sb
;
1483 u64 p_blkno
, p_blocks
;
1484 int virtual, blocks
, status
, i
, bytes_left
;
1486 bytes_left
= i_size_read(inode
) + 1;
1487 /* we can't trust i_blocks because we're actually going to
1488 * write i_size + 1 bytes. */
1489 blocks
= (bytes_left
+ sb
->s_blocksize
- 1) >> sb
->s_blocksize_bits
;
1491 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1492 (unsigned long long)inode
->i_blocks
,
1493 i_size_read(inode
), blocks
);
1495 /* Sanity check -- make sure we're going to fit. */
1497 ocfs2_clusters_to_bytes(sb
, OCFS2_I(inode
)->ip_clusters
)) {
1503 bhs
= kcalloc(blocks
, sizeof(struct buffer_head
*), GFP_KERNEL
);
1510 status
= ocfs2_extent_map_get_blocks(inode
, 0, &p_blkno
, &p_blocks
,
1517 /* links can never be larger than one cluster so we know this
1518 * is all going to be contiguous, but do a sanity check
1520 if ((p_blocks
<< sb
->s_blocksize_bits
) < bytes_left
) {
1527 while(bytes_left
> 0) {
1528 c
= &symname
[virtual * sb
->s_blocksize
];
1530 bhs
[virtual] = sb_getblk(sb
, p_blkno
);
1531 if (!bhs
[virtual]) {
1536 ocfs2_set_new_buffer_uptodate(inode
, bhs
[virtual]);
1538 status
= ocfs2_journal_access(handle
, inode
, bhs
[virtual],
1539 OCFS2_JOURNAL_ACCESS_CREATE
);
1545 memset(bhs
[virtual]->b_data
, 0, sb
->s_blocksize
);
1547 memcpy(bhs
[virtual]->b_data
, c
,
1548 (bytes_left
> sb
->s_blocksize
) ? sb
->s_blocksize
:
1551 status
= ocfs2_journal_dirty(handle
, bhs
[virtual]);
1559 bytes_left
-= sb
->s_blocksize
;
1566 for(i
= 0; i
< blocks
; i
++)
1575 static int ocfs2_symlink(struct inode
*dir
,
1576 struct dentry
*dentry
,
1577 const char *symname
)
1579 int status
, l
, credits
;
1581 struct ocfs2_super
*osb
= NULL
;
1582 struct inode
*inode
= NULL
;
1583 struct super_block
*sb
;
1584 struct buffer_head
*new_fe_bh
= NULL
;
1585 struct buffer_head
*parent_fe_bh
= NULL
;
1586 struct ocfs2_dinode
*fe
= NULL
;
1587 struct ocfs2_dinode
*dirfe
;
1588 handle_t
*handle
= NULL
;
1589 struct ocfs2_alloc_context
*inode_ac
= NULL
;
1590 struct ocfs2_alloc_context
*data_ac
= NULL
;
1591 struct ocfs2_alloc_context
*xattr_ac
= NULL
;
1592 int want_clusters
= 0;
1593 int xattr_credits
= 0;
1594 struct ocfs2_security_xattr_info si
= {
1597 int did_quota
= 0, did_quota_inode
= 0;
1598 struct ocfs2_dir_lookup_result lookup
= { NULL
, };
1600 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir
,
1601 dentry
, symname
, dentry
->d_name
.len
, dentry
->d_name
.name
);
1606 l
= strlen(symname
) + 1;
1608 credits
= ocfs2_calc_symlink_credits(sb
);
1610 /* lock the parent directory */
1611 status
= ocfs2_inode_lock(dir
, &parent_fe_bh
, 1);
1613 if (status
!= -ENOENT
)
1618 dirfe
= (struct ocfs2_dinode
*) parent_fe_bh
->b_data
;
1619 if (!dirfe
->i_links_count
) {
1620 /* can't make a file in a deleted directory. */
1625 status
= ocfs2_check_dir_for_entry(dir
, dentry
->d_name
.name
,
1626 dentry
->d_name
.len
);
1630 status
= ocfs2_prepare_dir_for_insert(osb
, dir
, parent_fe_bh
,
1631 dentry
->d_name
.name
,
1632 dentry
->d_name
.len
, &lookup
);
1638 status
= ocfs2_reserve_new_inode(osb
, &inode_ac
);
1640 if (status
!= -ENOSPC
)
1645 inode
= ocfs2_get_init_inode(dir
, S_IFLNK
| S_IRWXUGO
);
1652 /* get security xattr */
1653 status
= ocfs2_init_security_get(inode
, dir
, &si
);
1655 if (status
== -EOPNOTSUPP
)
1663 /* calculate meta data/clusters for setting security xattr */
1665 status
= ocfs2_calc_security_init(dir
, &si
, &want_clusters
,
1666 &xattr_credits
, &xattr_ac
);
1673 /* don't reserve bitmap space for fast symlinks. */
1674 if (l
> ocfs2_fast_symlink_chars(sb
))
1677 status
= ocfs2_reserve_clusters(osb
, want_clusters
, &data_ac
);
1679 if (status
!= -ENOSPC
)
1684 handle
= ocfs2_start_trans(osb
, credits
+ xattr_credits
);
1685 if (IS_ERR(handle
)) {
1686 status
= PTR_ERR(handle
);
1692 /* We don't use standard VFS wrapper because we don't want vfs_dq_init
1694 if (sb_any_quota_active(osb
->sb
) &&
1695 osb
->sb
->dq_op
->alloc_inode(inode
, 1) == NO_QUOTA
) {
1699 did_quota_inode
= 1;
1701 status
= ocfs2_mknod_locked(osb
, dir
, inode
, dentry
,
1702 0, &new_fe_bh
, parent_fe_bh
, handle
,
1709 fe
= (struct ocfs2_dinode
*) new_fe_bh
->b_data
;
1712 if (l
> ocfs2_fast_symlink_chars(sb
)) {
1715 inode
->i_op
= &ocfs2_symlink_inode_operations
;
1716 if (vfs_dq_alloc_space_nodirty(inode
,
1717 ocfs2_clusters_to_bytes(osb
->sb
, 1))) {
1722 status
= ocfs2_add_inode_data(osb
, inode
, &offset
, 1, 0,
1724 handle
, data_ac
, NULL
,
1727 if (status
!= -ENOSPC
&& status
!= -EINTR
) {
1729 "Failed to extend file to %llu\n",
1730 (unsigned long long)newsize
);
1736 i_size_write(inode
, newsize
);
1737 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
1739 inode
->i_op
= &ocfs2_fast_symlink_inode_operations
;
1740 memcpy((char *) fe
->id2
.i_symlink
, symname
, l
);
1741 i_size_write(inode
, newsize
);
1742 inode
->i_blocks
= 0;
1745 status
= ocfs2_mark_inode_dirty(handle
, inode
, new_fe_bh
);
1751 if (!ocfs2_inode_is_fast_symlink(inode
)) {
1752 status
= ocfs2_create_symlink_data(osb
, handle
, inode
,
1761 status
= ocfs2_init_security_set(handle
, inode
, new_fe_bh
, &si
,
1769 status
= ocfs2_add_entry(handle
, dentry
, inode
,
1770 le64_to_cpu(fe
->i_blkno
), parent_fe_bh
,
1777 status
= ocfs2_dentry_attach_lock(dentry
, inode
, OCFS2_I(dir
)->ip_blkno
);
1783 insert_inode_hash(inode
);
1784 dentry
->d_op
= &ocfs2_dentry_ops
;
1785 d_instantiate(dentry
, inode
);
1787 if (status
< 0 && did_quota
)
1788 vfs_dq_free_space_nodirty(inode
,
1789 ocfs2_clusters_to_bytes(osb
->sb
, 1));
1790 if (status
< 0 && did_quota_inode
)
1791 vfs_dq_free_inode(inode
);
1793 ocfs2_commit_trans(osb
, handle
);
1795 ocfs2_inode_unlock(dir
, 1);
1798 brelse(parent_fe_bh
);
1801 ocfs2_free_dir_lookup_result(&lookup
);
1803 ocfs2_free_alloc_context(inode_ac
);
1805 ocfs2_free_alloc_context(data_ac
);
1807 ocfs2_free_alloc_context(xattr_ac
);
1808 if ((status
< 0) && inode
) {
1818 static int ocfs2_blkno_stringify(u64 blkno
, char *name
)
1820 int status
, namelen
;
1824 namelen
= snprintf(name
, OCFS2_ORPHAN_NAMELEN
+ 1, "%016llx",
1834 if (namelen
!= OCFS2_ORPHAN_NAMELEN
) {
1840 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name
,
1849 static int ocfs2_prepare_orphan_dir(struct ocfs2_super
*osb
,
1850 struct inode
**ret_orphan_dir
,
1851 struct inode
*inode
,
1853 struct ocfs2_dir_lookup_result
*lookup
)
1855 struct inode
*orphan_dir_inode
;
1856 struct buffer_head
*orphan_dir_bh
= NULL
;
1859 status
= ocfs2_blkno_stringify(OCFS2_I(inode
)->ip_blkno
, name
);
1865 orphan_dir_inode
= ocfs2_get_system_file_inode(osb
,
1866 ORPHAN_DIR_SYSTEM_INODE
,
1868 if (!orphan_dir_inode
) {
1874 mutex_lock(&orphan_dir_inode
->i_mutex
);
1876 status
= ocfs2_inode_lock(orphan_dir_inode
, &orphan_dir_bh
, 1);
1882 status
= ocfs2_prepare_dir_for_insert(osb
, orphan_dir_inode
,
1883 orphan_dir_bh
, name
,
1884 OCFS2_ORPHAN_NAMELEN
, lookup
);
1886 ocfs2_inode_unlock(orphan_dir_inode
, 1);
1892 *ret_orphan_dir
= orphan_dir_inode
;
1896 mutex_unlock(&orphan_dir_inode
->i_mutex
);
1897 iput(orphan_dir_inode
);
1900 brelse(orphan_dir_bh
);
1906 static int ocfs2_orphan_add(struct ocfs2_super
*osb
,
1908 struct inode
*inode
,
1909 struct ocfs2_dinode
*fe
,
1911 struct ocfs2_dir_lookup_result
*lookup
,
1912 struct inode
*orphan_dir_inode
)
1914 struct buffer_head
*orphan_dir_bh
= NULL
;
1916 struct ocfs2_dinode
*orphan_fe
;
1918 mlog_entry("(inode->i_ino = %lu)\n", inode
->i_ino
);
1920 status
= ocfs2_read_inode_block(orphan_dir_inode
, &orphan_dir_bh
);
1926 status
= ocfs2_journal_access_di(handle
, orphan_dir_inode
, orphan_dir_bh
,
1927 OCFS2_JOURNAL_ACCESS_WRITE
);
1933 /* we're a cluster, and nlink can change on disk from
1934 * underneath us... */
1935 orphan_fe
= (struct ocfs2_dinode
*) orphan_dir_bh
->b_data
;
1936 if (S_ISDIR(inode
->i_mode
))
1937 le16_add_cpu(&orphan_fe
->i_links_count
, 1);
1938 orphan_dir_inode
->i_nlink
= le16_to_cpu(orphan_fe
->i_links_count
);
1940 status
= ocfs2_journal_dirty(handle
, orphan_dir_bh
);
1946 status
= __ocfs2_add_entry(handle
, orphan_dir_inode
, name
,
1947 OCFS2_ORPHAN_NAMELEN
, inode
,
1948 OCFS2_I(inode
)->ip_blkno
,
1949 orphan_dir_bh
, lookup
);
1955 le32_add_cpu(&fe
->i_flags
, OCFS2_ORPHANED_FL
);
1957 /* Record which orphan dir our inode now resides
1958 * in. delete_inode will use this to determine which orphan
1960 fe
->i_orphaned_slot
= cpu_to_le16(osb
->slot_num
);
1962 mlog(0, "Inode %llu orphaned in slot %d\n",
1963 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, osb
->slot_num
);
1966 brelse(orphan_dir_bh
);
1972 /* unlike orphan_add, we expect the orphan dir to already be locked here. */
1973 int ocfs2_orphan_del(struct ocfs2_super
*osb
,
1975 struct inode
*orphan_dir_inode
,
1976 struct inode
*inode
,
1977 struct buffer_head
*orphan_dir_bh
)
1979 char name
[OCFS2_ORPHAN_NAMELEN
+ 1];
1980 struct ocfs2_dinode
*orphan_fe
;
1982 struct ocfs2_dir_lookup_result lookup
= { NULL
, };
1986 status
= ocfs2_blkno_stringify(OCFS2_I(inode
)->ip_blkno
, name
);
1992 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
1993 name
, (unsigned long long)OCFS2_I(orphan_dir_inode
)->ip_blkno
,
1994 OCFS2_ORPHAN_NAMELEN
);
1996 /* find it's spot in the orphan directory */
1997 status
= ocfs2_find_entry(name
, OCFS2_ORPHAN_NAMELEN
, orphan_dir_inode
,
2004 /* remove it from the orphan directory */
2005 status
= ocfs2_delete_entry(handle
, orphan_dir_inode
, &lookup
);
2011 status
= ocfs2_journal_access_di(handle
,orphan_dir_inode
, orphan_dir_bh
,
2012 OCFS2_JOURNAL_ACCESS_WRITE
);
2018 /* do the i_nlink dance! :) */
2019 orphan_fe
= (struct ocfs2_dinode
*) orphan_dir_bh
->b_data
;
2020 if (S_ISDIR(inode
->i_mode
))
2021 le16_add_cpu(&orphan_fe
->i_links_count
, -1);
2022 orphan_dir_inode
->i_nlink
= le16_to_cpu(orphan_fe
->i_links_count
);
2024 status
= ocfs2_journal_dirty(handle
, orphan_dir_bh
);
2031 ocfs2_free_dir_lookup_result(&lookup
);
2037 const struct inode_operations ocfs2_dir_iops
= {
2038 .create
= ocfs2_create
,
2039 .lookup
= ocfs2_lookup
,
2041 .unlink
= ocfs2_unlink
,
2042 .rmdir
= ocfs2_unlink
,
2043 .symlink
= ocfs2_symlink
,
2044 .mkdir
= ocfs2_mkdir
,
2045 .mknod
= ocfs2_mknod
,
2046 .rename
= ocfs2_rename
,
2047 .setattr
= ocfs2_setattr
,
2048 .getattr
= ocfs2_getattr
,
2049 .permission
= ocfs2_permission
,
2050 .setxattr
= generic_setxattr
,
2051 .getxattr
= generic_getxattr
,
2052 .listxattr
= ocfs2_listxattr
,
2053 .removexattr
= generic_removexattr
,