1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * vfs' aops, fops, dops and iops
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <linux/highmem.h>
30 #include <linux/pagemap.h>
31 #include <linux/smp_lock.h>
33 #include <asm/byteorder.h>
35 #define MLOG_MASK_PREFIX ML_INODE
36 #include <cluster/masklog.h>
42 #include "extent_map.h"
44 #include "heartbeat.h"
55 #include "buffer_head_io.h"
57 #define OCFS2_FI_FLAG_NOWAIT 0x1
58 #define OCFS2_FI_FLAG_DELETE 0x2
59 struct ocfs2_find_inode_args
63 unsigned int fi_flags;
66 static int ocfs2_read_locked_inode(struct inode *inode,
67 struct ocfs2_find_inode_args *args);
68 static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
69 static int ocfs2_find_actor(struct inode *inode, void *opaque);
70 static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
72 struct buffer_head *fe_bh);
74 void ocfs2_set_inode_flags(struct inode *inode)
76 unsigned int flags = OCFS2_I(inode)->ip_attr;
78 inode->i_flags &= ~(S_IMMUTABLE |
79 S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC);
81 if (flags & OCFS2_IMMUTABLE_FL)
82 inode->i_flags |= S_IMMUTABLE;
84 if (flags & OCFS2_SYNC_FL)
85 inode->i_flags |= S_SYNC;
86 if (flags & OCFS2_APPEND_FL)
87 inode->i_flags |= S_APPEND;
88 if (flags & OCFS2_NOATIME_FL)
89 inode->i_flags |= S_NOATIME;
90 if (flags & OCFS2_DIRSYNC_FL)
91 inode->i_flags |= S_DIRSYNC;
94 struct inode *ocfs2_ilookup_for_vote(struct ocfs2_super *osb,
98 struct ocfs2_find_inode_args args;
100 /* ocfs2_ilookup_for_vote should *only* be called from the
102 BUG_ON(current != osb->vote_task);
104 args.fi_blkno = blkno;
105 args.fi_flags = OCFS2_FI_FLAG_NOWAIT;
107 args.fi_flags |= OCFS2_FI_FLAG_DELETE;
108 args.fi_ino = ino_from_blkno(osb->sb, blkno);
109 return ilookup5(osb->sb, args.fi_ino, ocfs2_find_actor, &args);
112 struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno)
114 struct inode *inode = NULL;
115 struct super_block *sb = osb->sb;
116 struct ocfs2_find_inode_args args;
118 mlog_entry("(blkno = %llu)\n", (unsigned long long)blkno);
120 /* Ok. By now we've either got the offsets passed to us by the
121 * caller, or we just pulled them off the bh. Lets do some
122 * sanity checks to make sure they're OK. */
124 inode = ERR_PTR(-EINVAL);
125 mlog_errno(PTR_ERR(inode));
129 args.fi_blkno = blkno;
131 args.fi_ino = ino_from_blkno(sb, blkno);
133 inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor,
134 ocfs2_init_locked_inode, &args);
135 /* inode was *not* in the inode cache. 2.6.x requires
136 * us to do our own read_inode call and unlock it
138 if (inode && inode->i_state & I_NEW) {
139 mlog(0, "Inode was not in inode cache, reading it.\n");
140 ocfs2_read_locked_inode(inode, &args);
141 unlock_new_inode(inode);
144 inode = ERR_PTR(-ENOMEM);
145 mlog_errno(PTR_ERR(inode));
148 if (is_bad_inode(inode)) {
150 inode = ERR_PTR(-ESTALE);
151 mlog_errno(PTR_ERR(inode));
156 if (!IS_ERR(inode)) {
157 mlog(0, "returning inode with number %llu\n",
158 (unsigned long long)OCFS2_I(inode)->ip_blkno);
159 mlog_exit_ptr(inode);
161 mlog_errno(PTR_ERR(inode));
168 * here's how inodes get read from disk:
169 * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR
170 * found? : return the in-memory inode
171 * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE
174 static int ocfs2_find_actor(struct inode *inode, void *opaque)
176 struct ocfs2_find_inode_args *args = NULL;
177 struct ocfs2_inode_info *oi = OCFS2_I(inode);
180 mlog_entry("(0x%p, %lu, 0x%p)\n", inode, inode->i_ino, opaque);
184 mlog_bug_on_msg(!inode, "No inode in find actor!\n");
186 if (oi->ip_blkno != args->fi_blkno)
189 /* OCFS2_FI_FLAG_NOWAIT is *only* set from
190 * ocfs2_ilookup_for_vote which won't create an inode for one
191 * that isn't found. The vote thread which doesn't want to get
192 * an inode which is in the process of going away - otherwise
193 * the call to __wait_on_freeing_inode in find_inode_fast will
194 * cause it to deadlock on an inode which may be waiting on a
195 * vote (or lock release) in delete_inode */
196 if ((args->fi_flags & OCFS2_FI_FLAG_NOWAIT) &&
197 (inode->i_state & (I_FREEING|I_CLEAR))) {
198 /* As stated above, we're not going to return an
199 * inode. In the case of a delete vote, the voting
200 * code is going to signal the other node to go
201 * ahead. Mark that state here, so this freeing inode
202 * has the state when it gets to delete_inode. */
203 if (args->fi_flags & OCFS2_FI_FLAG_DELETE) {
204 spin_lock(&oi->ip_lock);
205 ocfs2_mark_inode_remotely_deleted(inode);
206 spin_unlock(&oi->ip_lock);
218 * initialize the new inode, but don't do anything that would cause
220 * return 0 on success, 1 on failure
222 static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
224 struct ocfs2_find_inode_args *args = opaque;
226 mlog_entry("inode = %p, opaque = %p\n", inode, opaque);
228 inode->i_ino = args->fi_ino;
229 OCFS2_I(inode)->ip_blkno = args->fi_blkno;
235 int ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
238 struct super_block *sb;
239 struct ocfs2_super *osb;
240 int status = -EINVAL;
242 mlog_entry("(0x%p, size:%llu)\n", inode,
243 (unsigned long long)fe->i_size);
248 /* this means that read_inode cannot create a superblock inode
249 * today. change if needed. */
250 if (!OCFS2_IS_VALID_DINODE(fe) ||
251 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
252 mlog(ML_ERROR, "Invalid dinode: i_ino=%lu, i_blkno=%llu, "
253 "signature = %.*s, flags = 0x%x\n",
255 (unsigned long long)le64_to_cpu(fe->i_blkno), 7,
256 fe->i_signature, le32_to_cpu(fe->i_flags));
260 if (le32_to_cpu(fe->i_fs_generation) != osb->fs_generation) {
261 mlog(ML_ERROR, "file entry generation does not match "
262 "superblock! osb->fs_generation=%x, "
263 "fe->i_fs_generation=%x\n",
264 osb->fs_generation, le32_to_cpu(fe->i_fs_generation));
268 inode->i_version = 1;
269 inode->i_generation = le32_to_cpu(fe->i_generation);
270 inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
271 inode->i_mode = le16_to_cpu(fe->i_mode);
272 inode->i_uid = le32_to_cpu(fe->i_uid);
273 inode->i_gid = le32_to_cpu(fe->i_gid);
274 inode->i_blksize = (u32)osb->s_clustersize;
276 /* Fast symlinks will have i_size but no allocated clusters. */
277 if (S_ISLNK(inode->i_mode) && !fe->i_clusters)
281 ocfs2_align_bytes_to_sectors(le64_to_cpu(fe->i_size));
282 inode->i_mapping->a_ops = &ocfs2_aops;
283 inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
284 inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
285 inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
286 inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
287 inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
288 inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
290 if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno))
292 "ip_blkno %llu != i_blkno %llu!\n",
293 (unsigned long long)OCFS2_I(inode)->ip_blkno,
294 (unsigned long long)fe->i_blkno);
296 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
297 OCFS2_I(inode)->ip_orphaned_slot = OCFS2_INVALID_SLOT;
298 OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
301 inode->i_ino = ino_from_blkno(inode->i_sb,
302 le64_to_cpu(fe->i_blkno));
304 mlog(0, "blkno = %llu, ino = %lu, create_ino = %s\n",
305 (unsigned long long)fe->i_blkno, inode->i_ino, create_ino ? "true" : "false");
307 inode->i_nlink = le16_to_cpu(fe->i_links_count);
309 if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
310 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
311 mlog(0, "local alloc inode: i_ino=%lu\n", inode->i_ino);
312 } else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
313 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
314 } else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
315 mlog(0, "superblock inode: i_ino=%lu\n", inode->i_ino);
316 /* we can't actually hit this as read_inode can't
317 * handle superblocks today ;-) */
321 switch (inode->i_mode & S_IFMT) {
323 inode->i_fop = &ocfs2_fops;
324 inode->i_op = &ocfs2_file_iops;
325 i_size_write(inode, le64_to_cpu(fe->i_size));
328 inode->i_op = &ocfs2_dir_iops;
329 inode->i_fop = &ocfs2_dops;
330 i_size_write(inode, le64_to_cpu(fe->i_size));
333 if (ocfs2_inode_is_fast_symlink(inode))
334 inode->i_op = &ocfs2_fast_symlink_inode_operations;
336 inode->i_op = &ocfs2_symlink_inode_operations;
337 i_size_write(inode, le64_to_cpu(fe->i_size));
340 inode->i_op = &ocfs2_special_file_iops;
341 init_special_inode(inode, inode->i_mode,
346 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
347 OCFS2_LOCK_TYPE_RW, inode);
348 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_meta_lockres,
349 OCFS2_LOCK_TYPE_META, inode);
350 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_data_lockres,
351 OCFS2_LOCK_TYPE_DATA, inode);
353 ocfs2_set_inode_flags(inode);
354 inode->i_flags |= S_NOATIME;
362 static int ocfs2_read_locked_inode(struct inode *inode,
363 struct ocfs2_find_inode_args *args)
365 struct super_block *sb;
366 struct ocfs2_super *osb;
367 struct ocfs2_dinode *fe;
368 struct buffer_head *bh = NULL;
372 mlog_entry("(0x%p, 0x%p)\n", inode, args);
375 if (inode == NULL || inode->i_sb == NULL) {
376 mlog(ML_ERROR, "bad inode\n");
383 mlog(ML_ERROR, "bad inode args\n");
384 make_bad_inode(inode);
388 /* Read the FE off disk. This is safe because the kernel only
389 * does one read_inode2 for a new inode, and if it doesn't
390 * exist yet then nobody can be working on it! */
391 status = ocfs2_read_block(osb, args->fi_blkno, &bh, 0, NULL);
394 make_bad_inode(inode);
398 fe = (struct ocfs2_dinode *) bh->b_data;
399 if (!OCFS2_IS_VALID_DINODE(fe)) {
400 mlog(ML_ERROR, "Invalid dinode #%llu: signature = %.*s\n",
401 (unsigned long long)fe->i_blkno, 7, fe->i_signature);
402 make_bad_inode(inode);
406 if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL))
409 if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
410 S_ISBLK(le16_to_cpu(fe->i_mode)))
411 inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
414 if (ocfs2_populate_inode(inode, fe, 0) < 0) {
415 mlog(ML_ERROR, "populate failed! i_blkno=%llu, i_ino=%lu\n",
416 (unsigned long long)fe->i_blkno, inode->i_ino);
417 make_bad_inode(inode);
421 BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
424 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
436 void ocfs2_sync_blockdev(struct super_block *sb)
438 sync_blockdev(sb->s_bdev);
441 static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
443 struct buffer_head *fe_bh)
446 struct ocfs2_journal_handle *handle = NULL;
447 struct ocfs2_truncate_context *tc = NULL;
448 struct ocfs2_dinode *fe;
452 fe = (struct ocfs2_dinode *) fe_bh->b_data;
454 /* zero allocation, zero truncate :) */
458 handle = ocfs2_start_trans(osb, handle, OCFS2_INODE_UPDATE_CREDITS);
459 if (IS_ERR(handle)) {
460 status = PTR_ERR(handle);
466 status = ocfs2_set_inode_size(handle, inode, fe_bh, 0ULL);
472 ocfs2_commit_trans(handle);
475 status = ocfs2_prepare_truncate(osb, inode, fe_bh, &tc);
481 status = ocfs2_commit_truncate(osb, inode, fe_bh, tc);
488 ocfs2_commit_trans(handle);
494 static int ocfs2_remove_inode(struct inode *inode,
495 struct buffer_head *di_bh,
496 struct inode *orphan_dir_inode,
497 struct buffer_head *orphan_dir_bh)
500 struct inode *inode_alloc_inode = NULL;
501 struct buffer_head *inode_alloc_bh = NULL;
502 struct ocfs2_journal_handle *handle;
503 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
504 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
507 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
508 le16_to_cpu(di->i_suballoc_slot));
509 if (!inode_alloc_inode) {
515 mutex_lock(&inode_alloc_inode->i_mutex);
516 status = ocfs2_meta_lock(inode_alloc_inode, NULL, &inode_alloc_bh, 1);
518 mutex_unlock(&inode_alloc_inode->i_mutex);
524 handle = ocfs2_start_trans(osb, NULL, OCFS2_DELETE_INODE_CREDITS);
525 if (IS_ERR(handle)) {
526 status = PTR_ERR(handle);
531 status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
538 /* set the inodes dtime */
539 status = ocfs2_journal_access(handle, inode, di_bh,
540 OCFS2_JOURNAL_ACCESS_WRITE);
546 di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec);
547 le32_and_cpu(&di->i_flags, ~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
549 status = ocfs2_journal_dirty(handle, di_bh);
555 ocfs2_remove_from_cache(inode, di_bh);
557 status = ocfs2_free_dinode(handle, inode_alloc_inode,
563 ocfs2_commit_trans(handle);
565 ocfs2_meta_unlock(inode_alloc_inode, 1);
566 mutex_unlock(&inode_alloc_inode->i_mutex);
567 brelse(inode_alloc_bh);
569 iput(inode_alloc_inode);
575 * Serialize with orphan dir recovery. If the process doing
576 * recovery on this orphan dir does an iget() with the dir
577 * i_mutex held, we'll deadlock here. Instead we detect this
578 * and exit early - recovery will wipe this inode for us.
580 static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
585 spin_lock(&osb->osb_lock);
586 if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) {
587 mlog(0, "Recovery is happening on orphan dir %d, will skip "
588 "this inode\n", slot);
592 /* This signals to the orphan recovery process that it should
593 * wait for us to handle the wipe. */
594 osb->osb_orphan_wipes[slot]++;
596 spin_unlock(&osb->osb_lock);
600 static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
603 spin_lock(&osb->osb_lock);
604 osb->osb_orphan_wipes[slot]--;
605 spin_unlock(&osb->osb_lock);
607 wake_up(&osb->osb_wipe_event);
610 static int ocfs2_wipe_inode(struct inode *inode,
611 struct buffer_head *di_bh)
613 int status, orphaned_slot;
614 struct inode *orphan_dir_inode = NULL;
615 struct buffer_head *orphan_dir_bh = NULL;
616 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
618 /* We've already voted on this so it should be readonly - no
619 * spinlock needed. */
620 orphaned_slot = OCFS2_I(inode)->ip_orphaned_slot;
622 status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
626 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
627 ORPHAN_DIR_SYSTEM_INODE,
629 if (!orphan_dir_inode) {
635 /* Lock the orphan dir. The lock will be held for the entire
636 * delete_inode operation. We do this now to avoid races with
637 * recovery completion on other nodes. */
638 mutex_lock(&orphan_dir_inode->i_mutex);
639 status = ocfs2_meta_lock(orphan_dir_inode, NULL, &orphan_dir_bh, 1);
641 mutex_unlock(&orphan_dir_inode->i_mutex);
647 /* we do this while holding the orphan dir lock because we
648 * don't want recovery being run from another node to vote for
649 * an inode delete on us -- this will result in two nodes
650 * truncating the same file! */
651 status = ocfs2_truncate_for_delete(osb, inode, di_bh);
654 goto bail_unlock_dir;
657 status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
663 ocfs2_meta_unlock(orphan_dir_inode, 1);
664 mutex_unlock(&orphan_dir_inode->i_mutex);
665 brelse(orphan_dir_bh);
667 iput(orphan_dir_inode);
668 ocfs2_signal_wipe_completion(osb, orphaned_slot);
673 /* There is a series of simple checks that should be done before a
674 * vote is even considered. Encapsulate those in this function. */
675 static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
678 struct ocfs2_inode_info *oi = OCFS2_I(inode);
679 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
681 /* We shouldn't be getting here for the root directory
683 if (inode == osb->root_inode) {
684 mlog(ML_ERROR, "Skipping delete of root inode.\n");
688 /* If we're coming from process_vote we can't go into our own
689 * voting [hello, deadlock city!], so unforuntately we just
690 * have to skip deleting this guy. That's OK though because
691 * the node who's doing the actual deleting should handle it
693 if (current == osb->vote_task) {
694 mlog(0, "Skipping delete of %lu because we're currently "
695 "in process_vote\n", inode->i_ino);
699 spin_lock(&oi->ip_lock);
700 /* OCFS2 *never* deletes system files. This should technically
701 * never get here as system file inodes should always have a
702 * positive link count. */
703 if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
704 mlog(ML_ERROR, "Skipping delete of system file %llu\n",
705 (unsigned long long)oi->ip_blkno);
709 /* If we have voted "yes" on the wipe of this inode for
710 * another node, it will be marked here so we can safely skip
711 * it. Recovery will cleanup any inodes we might inadvertantly
713 if (oi->ip_flags & OCFS2_INODE_SKIP_DELETE) {
714 mlog(0, "Skipping delete of %lu because another node "
715 "has done this for us.\n", inode->i_ino);
721 spin_unlock(&oi->ip_lock);
726 /* Query the cluster to determine whether we should wipe an inode from
729 * Requires the inode to have the cluster lock. */
730 static int ocfs2_query_inode_wipe(struct inode *inode,
731 struct buffer_head *di_bh,
735 struct ocfs2_inode_info *oi = OCFS2_I(inode);
736 struct ocfs2_dinode *di;
740 /* While we were waiting for the cluster lock in
741 * ocfs2_delete_inode, another node might have asked to delete
742 * the inode. Recheck our flags to catch this. */
743 if (!ocfs2_inode_is_valid_to_delete(inode)) {
744 mlog(0, "Skipping delete of %llu because flags changed\n",
745 (unsigned long long)oi->ip_blkno);
749 /* Now that we have an up to date inode, we can double check
751 if (inode->i_nlink) {
752 mlog(0, "Skipping delete of %llu because nlink = %u\n",
753 (unsigned long long)oi->ip_blkno, inode->i_nlink);
757 /* Do some basic inode verification... */
758 di = (struct ocfs2_dinode *) di_bh->b_data;
759 if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL))) {
760 /* for lack of a better error? */
763 "Inode %llu (on-disk %llu) not orphaned! "
764 "Disk flags 0x%x, inode flags 0x%x\n",
765 (unsigned long long)oi->ip_blkno,
766 (unsigned long long)di->i_blkno, di->i_flags,
771 /* has someone already deleted us?! baaad... */
778 status = ocfs2_request_delete_vote(inode);
779 /* -EBUSY means that other nodes are still using the
780 * inode. We're done here though, so avoid doing anything on
781 * disk and let them worry about deleting it. */
782 if (status == -EBUSY) {
784 mlog(0, "Skipping delete of %llu because it is in use on"
785 "other nodes\n", (unsigned long long)oi->ip_blkno);
793 spin_lock(&oi->ip_lock);
794 if (oi->ip_orphaned_slot == OCFS2_INVALID_SLOT) {
795 /* Nobody knew which slot this inode was orphaned
796 * into. This may happen during node death and
797 * recovery knows how to clean it up so we can safely
798 * ignore this inode for now on. */
799 mlog(0, "Nobody knew where inode %llu was orphaned!\n",
800 (unsigned long long)oi->ip_blkno);
804 mlog(0, "Inode %llu is ok to wipe from orphan dir %d\n",
805 (unsigned long long)oi->ip_blkno, oi->ip_orphaned_slot);
807 spin_unlock(&oi->ip_lock);
813 /* Support function for ocfs2_delete_inode. Will help us keep the
814 * inode data in a consistent state for clear_inode. Always truncates
815 * pages, optionally sync's them first. */
816 static void ocfs2_cleanup_delete_inode(struct inode *inode,
819 mlog(0, "Cleanup inode %llu, sync = %d\n",
820 (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
822 write_inode_now(inode, 1);
823 truncate_inode_pages(&inode->i_data, 0);
826 void ocfs2_delete_inode(struct inode *inode)
829 sigset_t blocked, oldset;
830 struct buffer_head *di_bh = NULL;
832 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
834 if (is_bad_inode(inode)) {
835 mlog(0, "Skipping delete of bad inode\n");
839 if (!ocfs2_inode_is_valid_to_delete(inode)) {
840 /* It's probably not necessary to truncate_inode_pages
841 * here but we do it for safety anyway (it will most
842 * likely be a no-op anyway) */
843 ocfs2_cleanup_delete_inode(inode, 0);
847 /* We want to block signals in delete_inode as the lock and
848 * messaging paths may return us -ERESTARTSYS. Which would
849 * cause us to exit early, resulting in inodes being orphaned
851 sigfillset(&blocked);
852 status = sigprocmask(SIG_BLOCK, &blocked, &oldset);
855 ocfs2_cleanup_delete_inode(inode, 1);
859 /* Lock down the inode. This gives us an up to date view of
860 * it's metadata (for verification), and allows us to
861 * serialize delete_inode votes.
863 * Even though we might be doing a truncate, we don't take the
864 * allocation lock here as it won't be needed - nobody will
865 * have the file open.
867 status = ocfs2_meta_lock(inode, NULL, &di_bh, 1);
869 if (status != -ENOENT)
871 ocfs2_cleanup_delete_inode(inode, 0);
875 /* Query the cluster. This will be the final decision made
876 * before we go ahead and wipe the inode. */
877 status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
878 if (!wipe || status < 0) {
879 /* Error and inode busy vote both mean we won't be
880 * removing the inode, so they take almost the same
885 /* Someone in the cluster has voted to not wipe this
886 * inode, or it was never completely orphaned. Write
887 * out the pages and exit now. */
888 ocfs2_cleanup_delete_inode(inode, 1);
889 goto bail_unlock_inode;
892 ocfs2_cleanup_delete_inode(inode, 0);
894 status = ocfs2_wipe_inode(inode, di_bh);
896 if (status != -EDEADLK)
898 goto bail_unlock_inode;
901 /* Mark the inode as successfully deleted. This is important
902 * for ocfs2_clear_inode as it will check this flag and skip
903 * any checkpointing work */
904 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
907 ocfs2_meta_unlock(inode, 1);
910 status = sigprocmask(SIG_SETMASK, &oldset, NULL);
918 void ocfs2_clear_inode(struct inode *inode)
921 struct ocfs2_inode_info *oi = OCFS2_I(inode);
928 mlog(0, "Clearing inode: %llu, nlink = %u\n",
929 (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_nlink);
931 mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
932 "Inode=%lu\n", inode->i_ino);
934 /* Do these before all the other work so that we don't bounce
935 * the vote thread while waiting to destroy the locks. */
936 ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres);
937 ocfs2_mark_lockres_freeing(&oi->ip_meta_lockres);
938 ocfs2_mark_lockres_freeing(&oi->ip_data_lockres);
940 /* We very well may get a clear_inode before all an inodes
941 * metadata has hit disk. Of course, we can't drop any cluster
942 * locks until the journal has finished with it. The only
943 * exception here are successfully wiped inodes - their
944 * metadata can now be considered to be part of the system
945 * inodes from which it came. */
946 if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED))
947 ocfs2_checkpoint_inode(inode);
949 mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
950 "Clear inode of %llu, inode has io markers\n",
951 (unsigned long long)oi->ip_blkno);
953 ocfs2_extent_map_drop(inode, 0);
954 ocfs2_extent_map_init(inode);
956 status = ocfs2_drop_inode_locks(inode);
960 ocfs2_lock_res_free(&oi->ip_rw_lockres);
961 ocfs2_lock_res_free(&oi->ip_meta_lockres);
962 ocfs2_lock_res_free(&oi->ip_data_lockres);
964 ocfs2_metadata_cache_purge(inode);
966 mlog_bug_on_msg(oi->ip_metadata_cache.ci_num_cached,
967 "Clear inode of %llu, inode has %u cache items\n",
968 (unsigned long long)oi->ip_blkno, oi->ip_metadata_cache.ci_num_cached);
970 mlog_bug_on_msg(!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE),
971 "Clear inode of %llu, inode has a bad flag\n",
972 (unsigned long long)oi->ip_blkno);
974 mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
975 "Clear inode of %llu, inode is locked\n",
976 (unsigned long long)oi->ip_blkno);
978 mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
979 "Clear inode of %llu, io_mutex is locked\n",
980 (unsigned long long)oi->ip_blkno);
981 mutex_unlock(&oi->ip_io_mutex);
984 * down_trylock() returns 0, down_write_trylock() returns 1
987 mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
988 "Clear inode of %llu, alloc_sem is locked\n",
989 (unsigned long long)oi->ip_blkno);
990 up_write(&oi->ip_alloc_sem);
992 mlog_bug_on_msg(oi->ip_open_count,
993 "Clear inode of %llu has open count %d\n",
994 (unsigned long long)oi->ip_blkno, oi->ip_open_count);
995 mlog_bug_on_msg(!list_empty(&oi->ip_handle_list),
996 "Clear inode of %llu has non empty handle list\n",
997 (unsigned long long)oi->ip_blkno);
998 mlog_bug_on_msg(oi->ip_handle,
999 "Clear inode of %llu has non empty handle pointer\n",
1000 (unsigned long long)oi->ip_blkno);
1002 /* Clear all other flags. */
1003 oi->ip_flags = OCFS2_INODE_CACHE_INLINE;
1004 oi->ip_created_trans = 0;
1005 oi->ip_last_trans = 0;
1006 oi->ip_dir_start_lookup = 0;
1007 oi->ip_blkno = 0ULL;
1013 /* Called under inode_lock, with no more references on the
1014 * struct inode, so it's safe here to check the flags field
1015 * and to manipulate i_nlink without any other locks. */
1016 void ocfs2_drop_inode(struct inode *inode)
1018 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1022 mlog(0, "Drop inode %llu, nlink = %u, ip_flags = 0x%x\n",
1023 (unsigned long long)oi->ip_blkno, inode->i_nlink, oi->ip_flags);
1025 /* Testing ip_orphaned_slot here wouldn't work because we may
1026 * not have gotten a delete_inode vote from any other nodes
1028 if (oi->ip_flags & OCFS2_INODE_MAYBE_ORPHANED) {
1029 mlog(0, "Inode was orphaned on another node, clearing nlink.\n");
1033 generic_drop_inode(inode);
1039 * TODO: this should probably be merged into ocfs2_get_block
1041 * However, you now need to pay attention to the cont_prepare_write()
1042 * stuff in ocfs2_get_block (that is, ocfs2_get_block pretty much
1043 * expects never to extend).
1045 struct buffer_head *ocfs2_bread(struct inode *inode,
1046 int block, int *err, int reada)
1048 struct buffer_head *bh = NULL;
1051 int readflags = OCFS2_BH_CACHED;
1054 readflags |= OCFS2_BH_READAHEAD;
1056 if (((u64)block << inode->i_sb->s_blocksize_bits) >=
1057 i_size_read(inode)) {
1062 tmperr = ocfs2_extent_map_get_blocks(inode, block, 1,
1069 tmperr = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno, &bh,
1089 * This is called from our getattr.
1091 int ocfs2_inode_revalidate(struct dentry *dentry)
1093 struct inode *inode = dentry->d_inode;
1096 mlog_entry("(inode = 0x%p, ino = %llu)\n", inode,
1097 inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL);
1100 mlog(0, "eep, no inode!\n");
1105 spin_lock(&OCFS2_I(inode)->ip_lock);
1106 if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
1107 spin_unlock(&OCFS2_I(inode)->ip_lock);
1108 mlog(0, "inode deleted!\n");
1112 spin_unlock(&OCFS2_I(inode)->ip_lock);
1114 /* Let ocfs2_meta_lock do the work of updating our struct
1116 status = ocfs2_meta_lock(inode, NULL, NULL, 0);
1118 if (status != -ENOENT)
1122 ocfs2_meta_unlock(inode, 0);
1130 * Updates a disk inode from a
1132 * Only takes ip_lock.
1134 int ocfs2_mark_inode_dirty(struct ocfs2_journal_handle *handle,
1135 struct inode *inode,
1136 struct buffer_head *bh)
1139 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
1141 mlog_entry("(inode %llu)\n",
1142 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1144 status = ocfs2_journal_access(handle, inode, bh,
1145 OCFS2_JOURNAL_ACCESS_WRITE);
1151 spin_lock(&OCFS2_I(inode)->ip_lock);
1152 fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
1153 fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
1154 spin_unlock(&OCFS2_I(inode)->ip_lock);
1156 fe->i_size = cpu_to_le64(i_size_read(inode));
1157 fe->i_links_count = cpu_to_le16(inode->i_nlink);
1158 fe->i_uid = cpu_to_le32(inode->i_uid);
1159 fe->i_gid = cpu_to_le32(inode->i_gid);
1160 fe->i_mode = cpu_to_le16(inode->i_mode);
1161 fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
1162 fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
1163 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1164 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1165 fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
1166 fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
1168 status = ocfs2_journal_dirty(handle, bh);
1181 * Updates a struct inode from a disk inode.
1182 * does no i/o, only takes ip_lock.
1184 void ocfs2_refresh_inode(struct inode *inode,
1185 struct ocfs2_dinode *fe)
1187 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1189 spin_lock(&OCFS2_I(inode)->ip_lock);
1191 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1192 OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
1193 ocfs2_set_inode_flags(inode);
1194 i_size_write(inode, le64_to_cpu(fe->i_size));
1195 inode->i_nlink = le16_to_cpu(fe->i_links_count);
1196 inode->i_uid = le32_to_cpu(fe->i_uid);
1197 inode->i_gid = le32_to_cpu(fe->i_gid);
1198 inode->i_mode = le16_to_cpu(fe->i_mode);
1199 inode->i_blksize = (u32) osb->s_clustersize;
1200 if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
1201 inode->i_blocks = 0;
1203 inode->i_blocks = ocfs2_align_bytes_to_sectors(i_size_read(inode));
1204 inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
1205 inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
1206 inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
1207 inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
1208 inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
1209 inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
1211 spin_unlock(&OCFS2_I(inode)->ip_lock);