xref: /openbmc/linux/fs/ocfs2/inode.c (revision 066d92dcbfa5842d98f6c4c671220cef50a9720f)
1ccd979bdSMark Fasheh /* -*- mode: c; c-basic-offset: 8; -*-
2ccd979bdSMark Fasheh  * vim: noexpandtab sw=8 ts=8 sts=0:
3ccd979bdSMark Fasheh  *
4ccd979bdSMark Fasheh  * inode.c
5ccd979bdSMark Fasheh  *
6ccd979bdSMark Fasheh  * vfs' aops, fops, dops and iops
7ccd979bdSMark Fasheh  *
8ccd979bdSMark Fasheh  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
9ccd979bdSMark Fasheh  *
10ccd979bdSMark Fasheh  * This program is free software; you can redistribute it and/or
11ccd979bdSMark Fasheh  * modify it under the terms of the GNU General Public
12ccd979bdSMark Fasheh  * License as published by the Free Software Foundation; either
13ccd979bdSMark Fasheh  * version 2 of the License, or (at your option) any later version.
14ccd979bdSMark Fasheh  *
15ccd979bdSMark Fasheh  * This program is distributed in the hope that it will be useful,
16ccd979bdSMark Fasheh  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17ccd979bdSMark Fasheh  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18ccd979bdSMark Fasheh  * General Public License for more details.
19ccd979bdSMark Fasheh  *
20ccd979bdSMark Fasheh  * You should have received a copy of the GNU General Public
21ccd979bdSMark Fasheh  * License along with this program; if not, write to the
22ccd979bdSMark Fasheh  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23ccd979bdSMark Fasheh  * Boston, MA 021110-1307, USA.
24ccd979bdSMark Fasheh  */
25ccd979bdSMark Fasheh 
26ccd979bdSMark Fasheh #include <linux/fs.h>
27ccd979bdSMark Fasheh #include <linux/types.h>
28ccd979bdSMark Fasheh #include <linux/highmem.h>
29ccd979bdSMark Fasheh #include <linux/pagemap.h>
30a90714c1SJan Kara #include <linux/quotaops.h>
31ccd979bdSMark Fasheh 
32ccd979bdSMark Fasheh #include <asm/byteorder.h>
33ccd979bdSMark Fasheh 
34ccd979bdSMark Fasheh #define MLOG_MASK_PREFIX ML_INODE
35ccd979bdSMark Fasheh #include <cluster/masklog.h>
36ccd979bdSMark Fasheh 
37ccd979bdSMark Fasheh #include "ocfs2.h"
38ccd979bdSMark Fasheh 
39ccd979bdSMark Fasheh #include "alloc.h"
409b7895efSMark Fasheh #include "dir.h"
41d6b32bbbSJoel Becker #include "blockcheck.h"
42ccd979bdSMark Fasheh #include "dlmglue.h"
43ccd979bdSMark Fasheh #include "extent_map.h"
44ccd979bdSMark Fasheh #include "file.h"
45b4df6ed8SMark Fasheh #include "heartbeat.h"
46ccd979bdSMark Fasheh #include "inode.h"
47ccd979bdSMark Fasheh #include "journal.h"
48ccd979bdSMark Fasheh #include "namei.h"
49ccd979bdSMark Fasheh #include "suballoc.h"
50ccd979bdSMark Fasheh #include "super.h"
51ccd979bdSMark Fasheh #include "symlink.h"
52ccd979bdSMark Fasheh #include "sysfile.h"
53ccd979bdSMark Fasheh #include "uptodate.h"
54cf1d6c76STiger Yang #include "xattr.h"
558b2c0dbaSTao Ma #include "refcounttree.h"
56ccd979bdSMark Fasheh 
57ccd979bdSMark Fasheh #include "buffer_head_io.h"
58ccd979bdSMark Fasheh 
59ccd979bdSMark Fasheh struct ocfs2_find_inode_args
60ccd979bdSMark Fasheh {
61ccd979bdSMark Fasheh 	u64		fi_blkno;
62ccd979bdSMark Fasheh 	unsigned long	fi_ino;
63ccd979bdSMark Fasheh 	unsigned int	fi_flags;
645fa0613eSJan Kara 	unsigned int	fi_sysfile_type;
65ccd979bdSMark Fasheh };
66ccd979bdSMark Fasheh 
675fa0613eSJan Kara static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES];
685fa0613eSJan Kara 
69ccd979bdSMark Fasheh static int ocfs2_read_locked_inode(struct inode *inode,
70ccd979bdSMark Fasheh 				   struct ocfs2_find_inode_args *args);
71ccd979bdSMark Fasheh static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
72ccd979bdSMark Fasheh static int ocfs2_find_actor(struct inode *inode, void *opaque);
73ccd979bdSMark Fasheh static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
74ccd979bdSMark Fasheh 				    struct inode *inode,
75ccd979bdSMark Fasheh 				    struct buffer_head *fe_bh);
76ccd979bdSMark Fasheh 
77ca4d147eSHerbert Poetzl void ocfs2_set_inode_flags(struct inode *inode)
78ca4d147eSHerbert Poetzl {
79ca4d147eSHerbert Poetzl 	unsigned int flags = OCFS2_I(inode)->ip_attr;
80ca4d147eSHerbert Poetzl 
81ca4d147eSHerbert Poetzl 	inode->i_flags &= ~(S_IMMUTABLE |
82ca4d147eSHerbert Poetzl 		S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC);
83ca4d147eSHerbert Poetzl 
84ca4d147eSHerbert Poetzl 	if (flags & OCFS2_IMMUTABLE_FL)
85ca4d147eSHerbert Poetzl 		inode->i_flags |= S_IMMUTABLE;
86ca4d147eSHerbert Poetzl 
87ca4d147eSHerbert Poetzl 	if (flags & OCFS2_SYNC_FL)
88ca4d147eSHerbert Poetzl 		inode->i_flags |= S_SYNC;
89ca4d147eSHerbert Poetzl 	if (flags & OCFS2_APPEND_FL)
90ca4d147eSHerbert Poetzl 		inode->i_flags |= S_APPEND;
91ca4d147eSHerbert Poetzl 	if (flags & OCFS2_NOATIME_FL)
92ca4d147eSHerbert Poetzl 		inode->i_flags |= S_NOATIME;
93ca4d147eSHerbert Poetzl 	if (flags & OCFS2_DIRSYNC_FL)
94ca4d147eSHerbert Poetzl 		inode->i_flags |= S_DIRSYNC;
95ca4d147eSHerbert Poetzl }
96ca4d147eSHerbert Poetzl 
976e4b0d56SJan Kara /* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */
986e4b0d56SJan Kara void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi)
996e4b0d56SJan Kara {
1006e4b0d56SJan Kara 	unsigned int flags = oi->vfs_inode.i_flags;
1016e4b0d56SJan Kara 
1026e4b0d56SJan Kara 	oi->ip_attr &= ~(OCFS2_SYNC_FL|OCFS2_APPEND_FL|
1036e4b0d56SJan Kara 			OCFS2_IMMUTABLE_FL|OCFS2_NOATIME_FL|OCFS2_DIRSYNC_FL);
1046e4b0d56SJan Kara 	if (flags & S_SYNC)
1056e4b0d56SJan Kara 		oi->ip_attr |= OCFS2_SYNC_FL;
1066e4b0d56SJan Kara 	if (flags & S_APPEND)
1076e4b0d56SJan Kara 		oi->ip_attr |= OCFS2_APPEND_FL;
1086e4b0d56SJan Kara 	if (flags & S_IMMUTABLE)
1096e4b0d56SJan Kara 		oi->ip_attr |= OCFS2_IMMUTABLE_FL;
1106e4b0d56SJan Kara 	if (flags & S_NOATIME)
1116e4b0d56SJan Kara 		oi->ip_attr |= OCFS2_NOATIME_FL;
1126e4b0d56SJan Kara 	if (flags & S_DIRSYNC)
1136e4b0d56SJan Kara 		oi->ip_attr |= OCFS2_DIRSYNC_FL;
1146e4b0d56SJan Kara }
1156e4b0d56SJan Kara 
1166ca497a8Swengang wang struct inode *ocfs2_ilookup(struct super_block *sb, u64 blkno)
1176ca497a8Swengang wang {
1186ca497a8Swengang wang 	struct ocfs2_find_inode_args args;
1196ca497a8Swengang wang 
1206ca497a8Swengang wang 	args.fi_blkno = blkno;
1216ca497a8Swengang wang 	args.fi_flags = 0;
1226ca497a8Swengang wang 	args.fi_ino = ino_from_blkno(sb, blkno);
1236ca497a8Swengang wang 	args.fi_sysfile_type = 0;
1246ca497a8Swengang wang 
1256ca497a8Swengang wang 	return ilookup5(sb, blkno, ocfs2_find_actor, &args);
1266ca497a8Swengang wang }
1275fa0613eSJan Kara struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,
1285fa0613eSJan Kara 			 int sysfile_type)
129ccd979bdSMark Fasheh {
130ccd979bdSMark Fasheh 	struct inode *inode = NULL;
131ccd979bdSMark Fasheh 	struct super_block *sb = osb->sb;
132ccd979bdSMark Fasheh 	struct ocfs2_find_inode_args args;
133ccd979bdSMark Fasheh 
134b0697053SMark Fasheh 	mlog_entry("(blkno = %llu)\n", (unsigned long long)blkno);
135ccd979bdSMark Fasheh 
136ccd979bdSMark Fasheh 	/* Ok. By now we've either got the offsets passed to us by the
137ccd979bdSMark Fasheh 	 * caller, or we just pulled them off the bh. Lets do some
138ccd979bdSMark Fasheh 	 * sanity checks to make sure they're OK. */
139ccd979bdSMark Fasheh 	if (blkno == 0) {
140ccd979bdSMark Fasheh 		inode = ERR_PTR(-EINVAL);
141ccd979bdSMark Fasheh 		mlog_errno(PTR_ERR(inode));
142ccd979bdSMark Fasheh 		goto bail;
143ccd979bdSMark Fasheh 	}
144ccd979bdSMark Fasheh 
145ccd979bdSMark Fasheh 	args.fi_blkno = blkno;
14624c19ef4SMark Fasheh 	args.fi_flags = flags;
147ccd979bdSMark Fasheh 	args.fi_ino = ino_from_blkno(sb, blkno);
1485fa0613eSJan Kara 	args.fi_sysfile_type = sysfile_type;
149ccd979bdSMark Fasheh 
150ccd979bdSMark Fasheh 	inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor,
151ccd979bdSMark Fasheh 			     ocfs2_init_locked_inode, &args);
152ccd979bdSMark Fasheh 	/* inode was *not* in the inode cache. 2.6.x requires
153ccd979bdSMark Fasheh 	 * us to do our own read_inode call and unlock it
154ccd979bdSMark Fasheh 	 * afterwards. */
155ccd979bdSMark Fasheh 	if (inode && inode->i_state & I_NEW) {
156ccd979bdSMark Fasheh 		mlog(0, "Inode was not in inode cache, reading it.\n");
157ccd979bdSMark Fasheh 		ocfs2_read_locked_inode(inode, &args);
158ccd979bdSMark Fasheh 		unlock_new_inode(inode);
159ccd979bdSMark Fasheh 	}
160ccd979bdSMark Fasheh 	if (inode == NULL) {
161ccd979bdSMark Fasheh 		inode = ERR_PTR(-ENOMEM);
162ccd979bdSMark Fasheh 		mlog_errno(PTR_ERR(inode));
163ccd979bdSMark Fasheh 		goto bail;
164ccd979bdSMark Fasheh 	}
165ccd979bdSMark Fasheh 	if (is_bad_inode(inode)) {
166ccd979bdSMark Fasheh 		iput(inode);
167ccd979bdSMark Fasheh 		inode = ERR_PTR(-ESTALE);
168ccd979bdSMark Fasheh 		goto bail;
169ccd979bdSMark Fasheh 	}
170ccd979bdSMark Fasheh 
171ccd979bdSMark Fasheh bail:
172ccd979bdSMark Fasheh 	if (!IS_ERR(inode)) {
173b0697053SMark Fasheh 		mlog(0, "returning inode with number %llu\n",
174b0697053SMark Fasheh 		     (unsigned long long)OCFS2_I(inode)->ip_blkno);
175ccd979bdSMark Fasheh 		mlog_exit_ptr(inode);
1766a1bd4a5SMark Fasheh 	}
177ccd979bdSMark Fasheh 
178ccd979bdSMark Fasheh 	return inode;
179ccd979bdSMark Fasheh }
180ccd979bdSMark Fasheh 
181ccd979bdSMark Fasheh 
182ccd979bdSMark Fasheh /*
183ccd979bdSMark Fasheh  * here's how inodes get read from disk:
184ccd979bdSMark Fasheh  * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR
185ccd979bdSMark Fasheh  * found? : return the in-memory inode
186ccd979bdSMark Fasheh  * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE
187ccd979bdSMark Fasheh  */
188ccd979bdSMark Fasheh 
189ccd979bdSMark Fasheh static int ocfs2_find_actor(struct inode *inode, void *opaque)
190ccd979bdSMark Fasheh {
191ccd979bdSMark Fasheh 	struct ocfs2_find_inode_args *args = NULL;
192ccd979bdSMark Fasheh 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
193ccd979bdSMark Fasheh 	int ret = 0;
194ccd979bdSMark Fasheh 
195ccd979bdSMark Fasheh 	mlog_entry("(0x%p, %lu, 0x%p)\n", inode, inode->i_ino, opaque);
196ccd979bdSMark Fasheh 
197ccd979bdSMark Fasheh 	args = opaque;
198ccd979bdSMark Fasheh 
199ccd979bdSMark Fasheh 	mlog_bug_on_msg(!inode, "No inode in find actor!\n");
200ccd979bdSMark Fasheh 
201ccd979bdSMark Fasheh 	if (oi->ip_blkno != args->fi_blkno)
202ccd979bdSMark Fasheh 		goto bail;
203ccd979bdSMark Fasheh 
204ccd979bdSMark Fasheh 	ret = 1;
205ccd979bdSMark Fasheh bail:
206ccd979bdSMark Fasheh 	mlog_exit(ret);
207ccd979bdSMark Fasheh 	return ret;
208ccd979bdSMark Fasheh }
209ccd979bdSMark Fasheh 
210ccd979bdSMark Fasheh /*
211ccd979bdSMark Fasheh  * initialize the new inode, but don't do anything that would cause
212ccd979bdSMark Fasheh  * us to sleep.
213ccd979bdSMark Fasheh  * return 0 on success, 1 on failure
214ccd979bdSMark Fasheh  */
215ccd979bdSMark Fasheh static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
216ccd979bdSMark Fasheh {
217ccd979bdSMark Fasheh 	struct ocfs2_find_inode_args *args = opaque;
218cb25797dSJan Kara 	static struct lock_class_key ocfs2_quota_ip_alloc_sem_key,
219cb25797dSJan Kara 				     ocfs2_file_ip_alloc_sem_key;
220ccd979bdSMark Fasheh 
221ccd979bdSMark Fasheh 	mlog_entry("inode = %p, opaque = %p\n", inode, opaque);
222ccd979bdSMark Fasheh 
223ccd979bdSMark Fasheh 	inode->i_ino = args->fi_ino;
224ccd979bdSMark Fasheh 	OCFS2_I(inode)->ip_blkno = args->fi_blkno;
2255fa0613eSJan Kara 	if (args->fi_sysfile_type != 0)
2265fa0613eSJan Kara 		lockdep_set_class(&inode->i_mutex,
2275fa0613eSJan Kara 			&ocfs2_sysfile_lock_key[args->fi_sysfile_type]);
228cb25797dSJan Kara 	if (args->fi_sysfile_type == USER_QUOTA_SYSTEM_INODE ||
229cb25797dSJan Kara 	    args->fi_sysfile_type == GROUP_QUOTA_SYSTEM_INODE ||
230cb25797dSJan Kara 	    args->fi_sysfile_type == LOCAL_USER_QUOTA_SYSTEM_INODE ||
231cb25797dSJan Kara 	    args->fi_sysfile_type == LOCAL_GROUP_QUOTA_SYSTEM_INODE)
232cb25797dSJan Kara 		lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem,
233cb25797dSJan Kara 				  &ocfs2_quota_ip_alloc_sem_key);
234cb25797dSJan Kara 	else
235cb25797dSJan Kara 		lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem,
236cb25797dSJan Kara 				  &ocfs2_file_ip_alloc_sem_key);
237ccd979bdSMark Fasheh 
238ccd979bdSMark Fasheh 	mlog_exit(0);
239ccd979bdSMark Fasheh 	return 0;
240ccd979bdSMark Fasheh }
241ccd979bdSMark Fasheh 
242b657c95cSJoel Becker void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
243ccd979bdSMark Fasheh 			  int create_ino)
244ccd979bdSMark Fasheh {
245ccd979bdSMark Fasheh 	struct super_block *sb;
246ccd979bdSMark Fasheh 	struct ocfs2_super *osb;
24753da4939SMark Fasheh 	int use_plocks = 1;
248ccd979bdSMark Fasheh 
249b0697053SMark Fasheh 	mlog_entry("(0x%p, size:%llu)\n", inode,
2501ca1a111SMark Fasheh 		   (unsigned long long)le64_to_cpu(fe->i_size));
251ccd979bdSMark Fasheh 
252ccd979bdSMark Fasheh 	sb = inode->i_sb;
253ccd979bdSMark Fasheh 	osb = OCFS2_SB(sb);
254ccd979bdSMark Fasheh 
25553da4939SMark Fasheh 	if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
25653da4939SMark Fasheh 	    ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks())
25753da4939SMark Fasheh 		use_plocks = 0;
25853da4939SMark Fasheh 
259b657c95cSJoel Becker 	/*
260b657c95cSJoel Becker 	 * These have all been checked by ocfs2_read_inode_block() or set
261b657c95cSJoel Becker 	 * by ocfs2_mknod_locked(), so a failure is a code bug.
262b657c95cSJoel Becker 	 */
263b657c95cSJoel Becker 	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));  /* This means that read_inode
264b657c95cSJoel Becker 						cannot create a superblock
265b657c95cSJoel Becker 						inode today.  change if
266b657c95cSJoel Becker 						that is needed. */
267b657c95cSJoel Becker 	BUG_ON(!(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)));
268b657c95cSJoel Becker 	BUG_ON(le32_to_cpu(fe->i_fs_generation) != osb->fs_generation);
269ccd979bdSMark Fasheh 
270ccd979bdSMark Fasheh 
2718110b073SMark Fasheh 	OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
2728110b073SMark Fasheh 	OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
27315b1e36bSMark Fasheh 	OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
2748110b073SMark Fasheh 
275ccd979bdSMark Fasheh 	inode->i_version = 1;
276ccd979bdSMark Fasheh 	inode->i_generation = le32_to_cpu(fe->i_generation);
277ccd979bdSMark Fasheh 	inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
278ccd979bdSMark Fasheh 	inode->i_mode = le16_to_cpu(fe->i_mode);
279ccd979bdSMark Fasheh 	inode->i_uid = le32_to_cpu(fe->i_uid);
280ccd979bdSMark Fasheh 	inode->i_gid = le32_to_cpu(fe->i_gid);
281ccd979bdSMark Fasheh 
282ccd979bdSMark Fasheh 	/* Fast symlinks will have i_size but no allocated clusters. */
283ccd979bdSMark Fasheh 	if (S_ISLNK(inode->i_mode) && !fe->i_clusters)
284ccd979bdSMark Fasheh 		inode->i_blocks = 0;
285ccd979bdSMark Fasheh 	else
2868110b073SMark Fasheh 		inode->i_blocks = ocfs2_inode_sector_count(inode);
287ccd979bdSMark Fasheh 	inode->i_mapping->a_ops = &ocfs2_aops;
288ccd979bdSMark Fasheh 	inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
289ccd979bdSMark Fasheh 	inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
290ccd979bdSMark Fasheh 	inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
291ccd979bdSMark Fasheh 	inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
292ccd979bdSMark Fasheh 	inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
293ccd979bdSMark Fasheh 	inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
294ccd979bdSMark Fasheh 
295ccd979bdSMark Fasheh 	if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno))
296ccd979bdSMark Fasheh 		mlog(ML_ERROR,
297b0697053SMark Fasheh 		     "ip_blkno %llu != i_blkno %llu!\n",
298b0697053SMark Fasheh 		     (unsigned long long)OCFS2_I(inode)->ip_blkno,
2991ca1a111SMark Fasheh 		     (unsigned long long)le64_to_cpu(fe->i_blkno));
300ccd979bdSMark Fasheh 
301198a1ca3SMark Fasheh 	inode->i_nlink = ocfs2_read_links_count(fe);
302ccd979bdSMark Fasheh 
303bbbd0eb3SJan Kara 	if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) {
30424c19ef4SMark Fasheh 		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
305bbbd0eb3SJan Kara 		inode->i_flags |= S_NOQUOTA;
306bbbd0eb3SJan Kara 	}
30724c19ef4SMark Fasheh 
308ccd979bdSMark Fasheh 	if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
309ccd979bdSMark Fasheh 		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
310ccd979bdSMark Fasheh 		mlog(0, "local alloc inode: i_ino=%lu\n", inode->i_ino);
311ccd979bdSMark Fasheh 	} else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
312ccd979bdSMark Fasheh 		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
3131a224ad1SJan Kara 	} else if (fe->i_flags & cpu_to_le32(OCFS2_QUOTA_FL)) {
3141a224ad1SJan Kara 		inode->i_flags |= S_NOQUOTA;
315ccd979bdSMark Fasheh 	} else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
316ccd979bdSMark Fasheh 		mlog(0, "superblock inode: i_ino=%lu\n", inode->i_ino);
317ccd979bdSMark Fasheh 		/* we can't actually hit this as read_inode can't
318ccd979bdSMark Fasheh 		 * handle superblocks today ;-) */
319ccd979bdSMark Fasheh 		BUG();
320ccd979bdSMark Fasheh 	}
321ccd979bdSMark Fasheh 
322ccd979bdSMark Fasheh 	switch (inode->i_mode & S_IFMT) {
323ccd979bdSMark Fasheh 	    case S_IFREG:
32453da4939SMark Fasheh 		    if (use_plocks)
325ccd979bdSMark Fasheh 			    inode->i_fop = &ocfs2_fops;
32653da4939SMark Fasheh 		    else
32753da4939SMark Fasheh 			    inode->i_fop = &ocfs2_fops_no_plocks;
328ccd979bdSMark Fasheh 		    inode->i_op = &ocfs2_file_iops;
329ccd979bdSMark Fasheh 		    i_size_write(inode, le64_to_cpu(fe->i_size));
330ccd979bdSMark Fasheh 		    break;
331ccd979bdSMark Fasheh 	    case S_IFDIR:
332ccd979bdSMark Fasheh 		    inode->i_op = &ocfs2_dir_iops;
33353da4939SMark Fasheh 		    if (use_plocks)
334ccd979bdSMark Fasheh 			    inode->i_fop = &ocfs2_dops;
33553da4939SMark Fasheh 		    else
33653da4939SMark Fasheh 			    inode->i_fop = &ocfs2_dops_no_plocks;
337ccd979bdSMark Fasheh 		    i_size_write(inode, le64_to_cpu(fe->i_size));
338ccd979bdSMark Fasheh 		    break;
339ccd979bdSMark Fasheh 	    case S_IFLNK:
340ccd979bdSMark Fasheh 		    if (ocfs2_inode_is_fast_symlink(inode))
341ccd979bdSMark Fasheh 			inode->i_op = &ocfs2_fast_symlink_inode_operations;
342ccd979bdSMark Fasheh 		    else
343ccd979bdSMark Fasheh 			inode->i_op = &ocfs2_symlink_inode_operations;
344ccd979bdSMark Fasheh 		    i_size_write(inode, le64_to_cpu(fe->i_size));
345ccd979bdSMark Fasheh 		    break;
346ccd979bdSMark Fasheh 	    default:
347ccd979bdSMark Fasheh 		    inode->i_op = &ocfs2_special_file_iops;
348ccd979bdSMark Fasheh 		    init_special_inode(inode, inode->i_mode,
349ccd979bdSMark Fasheh 				       inode->i_rdev);
350ccd979bdSMark Fasheh 		    break;
351ccd979bdSMark Fasheh 	}
352ccd979bdSMark Fasheh 
35324c19ef4SMark Fasheh 	if (create_ino) {
35424c19ef4SMark Fasheh 		inode->i_ino = ino_from_blkno(inode->i_sb,
35524c19ef4SMark Fasheh 			       le64_to_cpu(fe->i_blkno));
35624c19ef4SMark Fasheh 
35724c19ef4SMark Fasheh 		/*
35824c19ef4SMark Fasheh 		 * If we ever want to create system files from kernel,
35924c19ef4SMark Fasheh 		 * the generation argument to
36024c19ef4SMark Fasheh 		 * ocfs2_inode_lock_res_init() will have to change.
36124c19ef4SMark Fasheh 		 */
3621ca1a111SMark Fasheh 		BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL);
36324c19ef4SMark Fasheh 
364e63aecb6SMark Fasheh 		ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
36524c19ef4SMark Fasheh 					  OCFS2_LOCK_TYPE_META, 0, inode);
36650008630STiger Yang 
36750008630STiger Yang 		ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
36850008630STiger Yang 					  OCFS2_LOCK_TYPE_OPEN, 0, inode);
36924c19ef4SMark Fasheh 	}
37024c19ef4SMark Fasheh 
37124c19ef4SMark Fasheh 	ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
37224c19ef4SMark Fasheh 				  OCFS2_LOCK_TYPE_RW, inode->i_generation,
37324c19ef4SMark Fasheh 				  inode);
37424c19ef4SMark Fasheh 
375ca4d147eSHerbert Poetzl 	ocfs2_set_inode_flags(inode);
376ca4d147eSHerbert Poetzl 
37713821151STao Ma 	OCFS2_I(inode)->ip_last_used_slot = 0;
37813821151STao Ma 	OCFS2_I(inode)->ip_last_used_group = 0;
379e3b4a97dSMark Fasheh 
380e3b4a97dSMark Fasheh 	if (S_ISDIR(inode->i_mode))
381e3b4a97dSMark Fasheh 		ocfs2_resv_set_type(&OCFS2_I(inode)->ip_la_data_resv,
382e3b4a97dSMark Fasheh 				    OCFS2_RESV_FLAG_DIR);
383b657c95cSJoel Becker 	mlog_exit_void();
384ccd979bdSMark Fasheh }
385ccd979bdSMark Fasheh 
386ccd979bdSMark Fasheh static int ocfs2_read_locked_inode(struct inode *inode,
387ccd979bdSMark Fasheh 				   struct ocfs2_find_inode_args *args)
388ccd979bdSMark Fasheh {
389ccd979bdSMark Fasheh 	struct super_block *sb;
390ccd979bdSMark Fasheh 	struct ocfs2_super *osb;
391ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
392ccd979bdSMark Fasheh 	struct buffer_head *bh = NULL;
39324c19ef4SMark Fasheh 	int status, can_lock;
39424c19ef4SMark Fasheh 	u32 generation = 0;
395ccd979bdSMark Fasheh 
396ccd979bdSMark Fasheh 	mlog_entry("(0x%p, 0x%p)\n", inode, args);
397ccd979bdSMark Fasheh 
398ccd979bdSMark Fasheh 	status = -EINVAL;
399ccd979bdSMark Fasheh 	if (inode == NULL || inode->i_sb == NULL) {
400ccd979bdSMark Fasheh 		mlog(ML_ERROR, "bad inode\n");
40124c19ef4SMark Fasheh 		return status;
402ccd979bdSMark Fasheh 	}
403ccd979bdSMark Fasheh 	sb = inode->i_sb;
404ccd979bdSMark Fasheh 	osb = OCFS2_SB(sb);
405ccd979bdSMark Fasheh 
406ccd979bdSMark Fasheh 	if (!args) {
407ccd979bdSMark Fasheh 		mlog(ML_ERROR, "bad inode args\n");
408ccd979bdSMark Fasheh 		make_bad_inode(inode);
40924c19ef4SMark Fasheh 		return status;
410ccd979bdSMark Fasheh 	}
411ccd979bdSMark Fasheh 
41224c19ef4SMark Fasheh 	/*
41324c19ef4SMark Fasheh 	 * To improve performance of cold-cache inode stats, we take
41424c19ef4SMark Fasheh 	 * the cluster lock here if possible.
41524c19ef4SMark Fasheh 	 *
41624c19ef4SMark Fasheh 	 * Generally, OCFS2 never trusts the contents of an inode
41724c19ef4SMark Fasheh 	 * unless it's holding a cluster lock, so taking it here isn't
41824c19ef4SMark Fasheh 	 * a correctness issue as much as it is a performance
41924c19ef4SMark Fasheh 	 * improvement.
42024c19ef4SMark Fasheh 	 *
42124c19ef4SMark Fasheh 	 * There are three times when taking the lock is not a good idea:
42224c19ef4SMark Fasheh 	 *
42324c19ef4SMark Fasheh 	 * 1) During startup, before we have initialized the DLM.
42424c19ef4SMark Fasheh 	 *
42524c19ef4SMark Fasheh 	 * 2) If we are reading certain system files which never get
42624c19ef4SMark Fasheh 	 *    cluster locks (local alloc, truncate log).
42724c19ef4SMark Fasheh 	 *
42824c19ef4SMark Fasheh 	 * 3) If the process doing the iget() is responsible for
42924c19ef4SMark Fasheh 	 *    orphan dir recovery. We're holding the orphan dir lock and
43024c19ef4SMark Fasheh 	 *    can get into a deadlock with another process on another
43124c19ef4SMark Fasheh 	 *    node in ->delete_inode().
43224c19ef4SMark Fasheh 	 *
43324c19ef4SMark Fasheh 	 * #1 and #2 can be simply solved by never taking the lock
43424c19ef4SMark Fasheh 	 * here for system files (which are the only type we read
43524c19ef4SMark Fasheh 	 * during mount). It's a heavier approach, but our main
43624c19ef4SMark Fasheh 	 * concern is user-accesible files anyway.
43724c19ef4SMark Fasheh 	 *
43824c19ef4SMark Fasheh 	 * #3 works itself out because we'll eventually take the
43924c19ef4SMark Fasheh 	 * cluster lock before trusting anything anyway.
44024c19ef4SMark Fasheh 	 */
44124c19ef4SMark Fasheh 	can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
44250008630STiger Yang 		&& !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY)
443c271c5c2SSunil Mushran 		&& !ocfs2_mount_local(osb);
44424c19ef4SMark Fasheh 
44524c19ef4SMark Fasheh 	/*
44624c19ef4SMark Fasheh 	 * To maintain backwards compatibility with older versions of
44724c19ef4SMark Fasheh 	 * ocfs2-tools, we still store the generation value for system
44824c19ef4SMark Fasheh 	 * files. The only ones that actually matter to userspace are
44924c19ef4SMark Fasheh 	 * the journals, but it's easier and inexpensive to just flag
45024c19ef4SMark Fasheh 	 * all system files similarly.
45124c19ef4SMark Fasheh 	 */
45224c19ef4SMark Fasheh 	if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
45324c19ef4SMark Fasheh 		generation = osb->fs_generation;
45424c19ef4SMark Fasheh 
455e63aecb6SMark Fasheh 	ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
45624c19ef4SMark Fasheh 				  OCFS2_LOCK_TYPE_META,
45724c19ef4SMark Fasheh 				  generation, inode);
45824c19ef4SMark Fasheh 
45950008630STiger Yang 	ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
46050008630STiger Yang 				  OCFS2_LOCK_TYPE_OPEN,
46150008630STiger Yang 				  0, inode);
46250008630STiger Yang 
46324c19ef4SMark Fasheh 	if (can_lock) {
46450008630STiger Yang 		status = ocfs2_open_lock(inode);
46550008630STiger Yang 		if (status) {
46650008630STiger Yang 			make_bad_inode(inode);
46750008630STiger Yang 			mlog_errno(status);
46850008630STiger Yang 			return status;
46950008630STiger Yang 		}
470e63aecb6SMark Fasheh 		status = ocfs2_inode_lock(inode, NULL, 0);
47124c19ef4SMark Fasheh 		if (status) {
47224c19ef4SMark Fasheh 			make_bad_inode(inode);
47324c19ef4SMark Fasheh 			mlog_errno(status);
47424c19ef4SMark Fasheh 			return status;
47524c19ef4SMark Fasheh 		}
47624c19ef4SMark Fasheh 	}
47724c19ef4SMark Fasheh 
47850008630STiger Yang 	if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) {
47950008630STiger Yang 		status = ocfs2_try_open_lock(inode, 0);
48050008630STiger Yang 		if (status) {
48150008630STiger Yang 			make_bad_inode(inode);
48250008630STiger Yang 			return status;
48350008630STiger Yang 		}
48450008630STiger Yang 	}
48550008630STiger Yang 
486b657c95cSJoel Becker 	if (can_lock) {
487b657c95cSJoel Becker 		status = ocfs2_read_inode_block_full(inode, &bh,
488d4a8c93cSJoel Becker 						     OCFS2_BH_IGNORE_CACHE);
489b657c95cSJoel Becker 	} else {
490da1e9098SJoel Becker 		status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh);
491b657c95cSJoel Becker 		if (!status)
492b657c95cSJoel Becker 			status = ocfs2_validate_inode_block(osb->sb, bh);
493b657c95cSJoel Becker 	}
494ccd979bdSMark Fasheh 	if (status < 0) {
495ccd979bdSMark Fasheh 		mlog_errno(status);
496ccd979bdSMark Fasheh 		goto bail;
497ccd979bdSMark Fasheh 	}
498ccd979bdSMark Fasheh 
49924c19ef4SMark Fasheh 	status = -EINVAL;
500ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) bh->b_data;
501ccd979bdSMark Fasheh 
50224c19ef4SMark Fasheh 	/*
50324c19ef4SMark Fasheh 	 * This is a code bug. Right now the caller needs to
50424c19ef4SMark Fasheh 	 * understand whether it is asking for a system file inode or
50524c19ef4SMark Fasheh 	 * not so the proper lock names can be built.
50624c19ef4SMark Fasheh 	 */
50724c19ef4SMark Fasheh 	mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) !=
50824c19ef4SMark Fasheh 			!!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE),
50924c19ef4SMark Fasheh 			"Inode %llu: system file state is ambigous\n",
51024c19ef4SMark Fasheh 			(unsigned long long)args->fi_blkno);
511ccd979bdSMark Fasheh 
512ccd979bdSMark Fasheh 	if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
513ccd979bdSMark Fasheh 	    S_ISBLK(le16_to_cpu(fe->i_mode)))
514ccd979bdSMark Fasheh 		inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
515ccd979bdSMark Fasheh 
516b657c95cSJoel Becker 	ocfs2_populate_inode(inode, fe, 0);
517ccd979bdSMark Fasheh 
518ccd979bdSMark Fasheh 	BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
519ccd979bdSMark Fasheh 
520ccd979bdSMark Fasheh 	status = 0;
521ccd979bdSMark Fasheh 
522ccd979bdSMark Fasheh bail:
52324c19ef4SMark Fasheh 	if (can_lock)
524e63aecb6SMark Fasheh 		ocfs2_inode_unlock(inode, 0);
52524c19ef4SMark Fasheh 
52624c19ef4SMark Fasheh 	if (status < 0)
52724c19ef4SMark Fasheh 		make_bad_inode(inode);
52824c19ef4SMark Fasheh 
529ccd979bdSMark Fasheh 	if (args && bh)
530ccd979bdSMark Fasheh 		brelse(bh);
531ccd979bdSMark Fasheh 
532ccd979bdSMark Fasheh 	mlog_exit(status);
533ccd979bdSMark Fasheh 	return status;
534ccd979bdSMark Fasheh }
535ccd979bdSMark Fasheh 
536ccd979bdSMark Fasheh void ocfs2_sync_blockdev(struct super_block *sb)
537ccd979bdSMark Fasheh {
538ccd979bdSMark Fasheh 	sync_blockdev(sb->s_bdev);
539ccd979bdSMark Fasheh }
540ccd979bdSMark Fasheh 
541ccd979bdSMark Fasheh static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
542ccd979bdSMark Fasheh 				     struct inode *inode,
543ccd979bdSMark Fasheh 				     struct buffer_head *fe_bh)
544ccd979bdSMark Fasheh {
545ccd979bdSMark Fasheh 	int status = 0;
546ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
54760b11392SMark Fasheh 	handle_t *handle = NULL;
548ccd979bdSMark Fasheh 
549ccd979bdSMark Fasheh 	mlog_entry_void();
550ccd979bdSMark Fasheh 
551ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) fe_bh->b_data;
552ccd979bdSMark Fasheh 
5531afc32b9SMark Fasheh 	/*
5541afc32b9SMark Fasheh 	 * This check will also skip truncate of inodes with inline
5551afc32b9SMark Fasheh 	 * data and fast symlinks.
5561afc32b9SMark Fasheh 	 */
5573a0782d0SMark Fasheh 	if (fe->i_clusters) {
5582b4e30fbSJoel Becker 		if (ocfs2_should_order_data(inode))
5592b4e30fbSJoel Becker 			ocfs2_begin_ordered_truncate(inode, 0);
5602b4e30fbSJoel Becker 
56160b11392SMark Fasheh 		handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
56260b11392SMark Fasheh 		if (IS_ERR(handle)) {
56360b11392SMark Fasheh 			status = PTR_ERR(handle);
5640350cb07SDan Carpenter 			handle = NULL;
56560b11392SMark Fasheh 			mlog_errno(status);
56660b11392SMark Fasheh 			goto out;
56760b11392SMark Fasheh 		}
56860b11392SMark Fasheh 
5690cf2f763SJoel Becker 		status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
5700cf2f763SJoel Becker 						 fe_bh,
57160b11392SMark Fasheh 						 OCFS2_JOURNAL_ACCESS_WRITE);
57260b11392SMark Fasheh 		if (status < 0) {
57360b11392SMark Fasheh 			mlog_errno(status);
57460b11392SMark Fasheh 			goto out;
57560b11392SMark Fasheh 		}
57660b11392SMark Fasheh 
57760b11392SMark Fasheh 		i_size_write(inode, 0);
57860b11392SMark Fasheh 
57960b11392SMark Fasheh 		status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
58060b11392SMark Fasheh 		if (status < 0) {
58160b11392SMark Fasheh 			mlog_errno(status);
58260b11392SMark Fasheh 			goto out;
58360b11392SMark Fasheh 		}
58460b11392SMark Fasheh 
58560b11392SMark Fasheh 		ocfs2_commit_trans(osb, handle);
58660b11392SMark Fasheh 		handle = NULL;
58760b11392SMark Fasheh 
58878f94673STristan Ye 		status = ocfs2_commit_truncate(osb, inode, fe_bh);
589ccd979bdSMark Fasheh 		if (status < 0) {
590ccd979bdSMark Fasheh 			mlog_errno(status);
5913a0782d0SMark Fasheh 			goto out;
592ccd979bdSMark Fasheh 		}
5933a0782d0SMark Fasheh 	}
594ccd979bdSMark Fasheh 
59560b11392SMark Fasheh out:
59660b11392SMark Fasheh 	if (handle)
59760b11392SMark Fasheh 		ocfs2_commit_trans(osb, handle);
598ccd979bdSMark Fasheh 	mlog_exit(status);
599ccd979bdSMark Fasheh 	return status;
600ccd979bdSMark Fasheh }
601ccd979bdSMark Fasheh 
602ccd979bdSMark Fasheh static int ocfs2_remove_inode(struct inode *inode,
603ccd979bdSMark Fasheh 			      struct buffer_head *di_bh,
604ccd979bdSMark Fasheh 			      struct inode *orphan_dir_inode,
605ccd979bdSMark Fasheh 			      struct buffer_head *orphan_dir_bh)
606ccd979bdSMark Fasheh {
607ccd979bdSMark Fasheh 	int status;
608ccd979bdSMark Fasheh 	struct inode *inode_alloc_inode = NULL;
609ccd979bdSMark Fasheh 	struct buffer_head *inode_alloc_bh = NULL;
6101fabe148SMark Fasheh 	handle_t *handle;
611ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
612ccd979bdSMark Fasheh 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
613ccd979bdSMark Fasheh 
614ccd979bdSMark Fasheh 	inode_alloc_inode =
615ccd979bdSMark Fasheh 		ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
616ccd979bdSMark Fasheh 					    le16_to_cpu(di->i_suballoc_slot));
617ccd979bdSMark Fasheh 	if (!inode_alloc_inode) {
618ccd979bdSMark Fasheh 		status = -EEXIST;
619ccd979bdSMark Fasheh 		mlog_errno(status);
620ccd979bdSMark Fasheh 		goto bail;
621ccd979bdSMark Fasheh 	}
622ccd979bdSMark Fasheh 
6231b1dcc1bSJes Sorensen 	mutex_lock(&inode_alloc_inode->i_mutex);
624e63aecb6SMark Fasheh 	status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1);
625ccd979bdSMark Fasheh 	if (status < 0) {
6261b1dcc1bSJes Sorensen 		mutex_unlock(&inode_alloc_inode->i_mutex);
627ccd979bdSMark Fasheh 
628ccd979bdSMark Fasheh 		mlog_errno(status);
629ccd979bdSMark Fasheh 		goto bail;
630ccd979bdSMark Fasheh 	}
631ccd979bdSMark Fasheh 
632a90714c1SJan Kara 	handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS +
633a90714c1SJan Kara 				   ocfs2_quota_trans_credits(inode->i_sb));
634ccd979bdSMark Fasheh 	if (IS_ERR(handle)) {
635ccd979bdSMark Fasheh 		status = PTR_ERR(handle);
636ccd979bdSMark Fasheh 		mlog_errno(status);
637ccd979bdSMark Fasheh 		goto bail_unlock;
638ccd979bdSMark Fasheh 	}
639ccd979bdSMark Fasheh 
640d4cd1871SLi Dongyang 	if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
641ccd979bdSMark Fasheh 		status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
642ccd979bdSMark Fasheh 					  orphan_dir_bh);
643ccd979bdSMark Fasheh 		if (status < 0) {
644ccd979bdSMark Fasheh 			mlog_errno(status);
645ccd979bdSMark Fasheh 			goto bail_commit;
646ccd979bdSMark Fasheh 		}
647d4cd1871SLi Dongyang 	}
648ccd979bdSMark Fasheh 
649ccd979bdSMark Fasheh 	/* set the inodes dtime */
6500cf2f763SJoel Becker 	status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
651ccd979bdSMark Fasheh 					 OCFS2_JOURNAL_ACCESS_WRITE);
652ccd979bdSMark Fasheh 	if (status < 0) {
653ccd979bdSMark Fasheh 		mlog_errno(status);
654ccd979bdSMark Fasheh 		goto bail_commit;
655ccd979bdSMark Fasheh 	}
656ccd979bdSMark Fasheh 
657ccd979bdSMark Fasheh 	di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec);
6584092d49fSMarcin Slusarz 	di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
659ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, di_bh);
660ccd979bdSMark Fasheh 
6618cb471e8SJoel Becker 	ocfs2_remove_from_cache(INODE_CACHE(inode), di_bh);
66263936ddaSChristoph Hellwig 	dquot_free_inode(inode);
663ccd979bdSMark Fasheh 
664ccd979bdSMark Fasheh 	status = ocfs2_free_dinode(handle, inode_alloc_inode,
665ccd979bdSMark Fasheh 				   inode_alloc_bh, di);
666ccd979bdSMark Fasheh 	if (status < 0)
667ccd979bdSMark Fasheh 		mlog_errno(status);
668ccd979bdSMark Fasheh 
669ccd979bdSMark Fasheh bail_commit:
67002dc1af4SMark Fasheh 	ocfs2_commit_trans(osb, handle);
671ccd979bdSMark Fasheh bail_unlock:
672e63aecb6SMark Fasheh 	ocfs2_inode_unlock(inode_alloc_inode, 1);
6731b1dcc1bSJes Sorensen 	mutex_unlock(&inode_alloc_inode->i_mutex);
674ccd979bdSMark Fasheh 	brelse(inode_alloc_bh);
675ccd979bdSMark Fasheh bail:
676ccd979bdSMark Fasheh 	iput(inode_alloc_inode);
677ccd979bdSMark Fasheh 
678ccd979bdSMark Fasheh 	return status;
679ccd979bdSMark Fasheh }
680ccd979bdSMark Fasheh 
681b4df6ed8SMark Fasheh /*
682b4df6ed8SMark Fasheh  * Serialize with orphan dir recovery. If the process doing
683b4df6ed8SMark Fasheh  * recovery on this orphan dir does an iget() with the dir
684b4df6ed8SMark Fasheh  * i_mutex held, we'll deadlock here. Instead we detect this
685b4df6ed8SMark Fasheh  * and exit early - recovery will wipe this inode for us.
686b4df6ed8SMark Fasheh  */
687b4df6ed8SMark Fasheh static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
688b4df6ed8SMark Fasheh 					     int slot)
689b4df6ed8SMark Fasheh {
690b4df6ed8SMark Fasheh 	int ret = 0;
691b4df6ed8SMark Fasheh 
692b4df6ed8SMark Fasheh 	spin_lock(&osb->osb_lock);
693b4df6ed8SMark Fasheh 	if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) {
694b4df6ed8SMark Fasheh 		mlog(0, "Recovery is happening on orphan dir %d, will skip "
695b4df6ed8SMark Fasheh 		     "this inode\n", slot);
696b4df6ed8SMark Fasheh 		ret = -EDEADLK;
697b4df6ed8SMark Fasheh 		goto out;
698b4df6ed8SMark Fasheh 	}
699b4df6ed8SMark Fasheh 	/* This signals to the orphan recovery process that it should
700b4df6ed8SMark Fasheh 	 * wait for us to handle the wipe. */
701b4df6ed8SMark Fasheh 	osb->osb_orphan_wipes[slot]++;
702b4df6ed8SMark Fasheh out:
703b4df6ed8SMark Fasheh 	spin_unlock(&osb->osb_lock);
704b4df6ed8SMark Fasheh 	return ret;
705b4df6ed8SMark Fasheh }
706b4df6ed8SMark Fasheh 
707b4df6ed8SMark Fasheh static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
708b4df6ed8SMark Fasheh 					 int slot)
709b4df6ed8SMark Fasheh {
710b4df6ed8SMark Fasheh 	spin_lock(&osb->osb_lock);
711b4df6ed8SMark Fasheh 	osb->osb_orphan_wipes[slot]--;
712b4df6ed8SMark Fasheh 	spin_unlock(&osb->osb_lock);
713b4df6ed8SMark Fasheh 
714b4df6ed8SMark Fasheh 	wake_up(&osb->osb_wipe_event);
715b4df6ed8SMark Fasheh }
716b4df6ed8SMark Fasheh 
717ccd979bdSMark Fasheh static int ocfs2_wipe_inode(struct inode *inode,
718ccd979bdSMark Fasheh 			    struct buffer_head *di_bh)
719ccd979bdSMark Fasheh {
720d577632eSJoel Becker 	int status, orphaned_slot = -1;
721ccd979bdSMark Fasheh 	struct inode *orphan_dir_inode = NULL;
722ccd979bdSMark Fasheh 	struct buffer_head *orphan_dir_bh = NULL;
723ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
724d4cd1871SLi Dongyang 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
725ccd979bdSMark Fasheh 
726d4cd1871SLi Dongyang 	if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
72750008630STiger Yang 		orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
728b4df6ed8SMark Fasheh 
729b4df6ed8SMark Fasheh 		status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
730b4df6ed8SMark Fasheh 		if (status)
731b4df6ed8SMark Fasheh 			return status;
732b4df6ed8SMark Fasheh 
733ccd979bdSMark Fasheh 		orphan_dir_inode = ocfs2_get_system_file_inode(osb,
734ccd979bdSMark Fasheh 							       ORPHAN_DIR_SYSTEM_INODE,
735ccd979bdSMark Fasheh 							       orphaned_slot);
736ccd979bdSMark Fasheh 		if (!orphan_dir_inode) {
737ccd979bdSMark Fasheh 			status = -EEXIST;
738ccd979bdSMark Fasheh 			mlog_errno(status);
739ccd979bdSMark Fasheh 			goto bail;
740ccd979bdSMark Fasheh 		}
741ccd979bdSMark Fasheh 
742ccd979bdSMark Fasheh 		/* Lock the orphan dir. The lock will be held for the entire
743ccd979bdSMark Fasheh 		 * delete_inode operation. We do this now to avoid races with
744ccd979bdSMark Fasheh 		 * recovery completion on other nodes. */
7451b1dcc1bSJes Sorensen 		mutex_lock(&orphan_dir_inode->i_mutex);
746e63aecb6SMark Fasheh 		status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
747ccd979bdSMark Fasheh 		if (status < 0) {
7481b1dcc1bSJes Sorensen 			mutex_unlock(&orphan_dir_inode->i_mutex);
749ccd979bdSMark Fasheh 
750ccd979bdSMark Fasheh 			mlog_errno(status);
751ccd979bdSMark Fasheh 			goto bail;
752ccd979bdSMark Fasheh 		}
753d4cd1871SLi Dongyang 	}
754ccd979bdSMark Fasheh 
755ccd979bdSMark Fasheh 	/* we do this while holding the orphan dir lock because we
75634d024f8SMark Fasheh 	 * don't want recovery being run from another node to try an
75734d024f8SMark Fasheh 	 * inode delete underneath us -- this will result in two nodes
758ccd979bdSMark Fasheh 	 * truncating the same file! */
759ccd979bdSMark Fasheh 	status = ocfs2_truncate_for_delete(osb, inode, di_bh);
760ccd979bdSMark Fasheh 	if (status < 0) {
761ccd979bdSMark Fasheh 		mlog_errno(status);
762ccd979bdSMark Fasheh 		goto bail_unlock_dir;
763ccd979bdSMark Fasheh 	}
764ccd979bdSMark Fasheh 
7659b7895efSMark Fasheh 	/* Remove any dir index tree */
7669b7895efSMark Fasheh 	if (S_ISDIR(inode->i_mode)) {
7679b7895efSMark Fasheh 		status = ocfs2_dx_dir_truncate(inode, di_bh);
7689b7895efSMark Fasheh 		if (status) {
7699b7895efSMark Fasheh 			mlog_errno(status);
7709b7895efSMark Fasheh 			goto bail_unlock_dir;
7719b7895efSMark Fasheh 		}
7729b7895efSMark Fasheh 	}
7739b7895efSMark Fasheh 
774cf1d6c76STiger Yang 	/*Free extended attribute resources associated with this inode.*/
775cf1d6c76STiger Yang 	status = ocfs2_xattr_remove(inode, di_bh);
776cf1d6c76STiger Yang 	if (status < 0) {
777cf1d6c76STiger Yang 		mlog_errno(status);
778cf1d6c76STiger Yang 		goto bail_unlock_dir;
779cf1d6c76STiger Yang 	}
780cf1d6c76STiger Yang 
7818b2c0dbaSTao Ma 	status = ocfs2_remove_refcount_tree(inode, di_bh);
7828b2c0dbaSTao Ma 	if (status < 0) {
7838b2c0dbaSTao Ma 		mlog_errno(status);
7848b2c0dbaSTao Ma 		goto bail_unlock_dir;
7858b2c0dbaSTao Ma 	}
7868b2c0dbaSTao Ma 
787ccd979bdSMark Fasheh 	status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
788ccd979bdSMark Fasheh 				    orphan_dir_bh);
789ccd979bdSMark Fasheh 	if (status < 0)
790ccd979bdSMark Fasheh 		mlog_errno(status);
791ccd979bdSMark Fasheh 
792ccd979bdSMark Fasheh bail_unlock_dir:
793d4cd1871SLi Dongyang 	if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)
794d4cd1871SLi Dongyang 		return status;
795d4cd1871SLi Dongyang 
796e63aecb6SMark Fasheh 	ocfs2_inode_unlock(orphan_dir_inode, 1);
7971b1dcc1bSJes Sorensen 	mutex_unlock(&orphan_dir_inode->i_mutex);
798ccd979bdSMark Fasheh 	brelse(orphan_dir_bh);
799ccd979bdSMark Fasheh bail:
800ccd979bdSMark Fasheh 	iput(orphan_dir_inode);
801b4df6ed8SMark Fasheh 	ocfs2_signal_wipe_completion(osb, orphaned_slot);
802ccd979bdSMark Fasheh 
803ccd979bdSMark Fasheh 	return status;
804ccd979bdSMark Fasheh }
805ccd979bdSMark Fasheh 
806ccd979bdSMark Fasheh /* There is a series of simple checks that should be done before a
80734d024f8SMark Fasheh  * trylock is even considered. Encapsulate those in this function. */
808ccd979bdSMark Fasheh static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
809ccd979bdSMark Fasheh {
810ccd979bdSMark Fasheh 	int ret = 0;
811ccd979bdSMark Fasheh 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
812ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
813ccd979bdSMark Fasheh 
814ccd979bdSMark Fasheh 	/* We shouldn't be getting here for the root directory
815ccd979bdSMark Fasheh 	 * inode.. */
816ccd979bdSMark Fasheh 	if (inode == osb->root_inode) {
817ccd979bdSMark Fasheh 		mlog(ML_ERROR, "Skipping delete of root inode.\n");
818ccd979bdSMark Fasheh 		goto bail;
819ccd979bdSMark Fasheh 	}
820ccd979bdSMark Fasheh 
82134d024f8SMark Fasheh 	/* If we're coming from downconvert_thread we can't go into our own
822ccd979bdSMark Fasheh 	 * voting [hello, deadlock city!], so unforuntately we just
823ccd979bdSMark Fasheh 	 * have to skip deleting this guy. That's OK though because
824ccd979bdSMark Fasheh 	 * the node who's doing the actual deleting should handle it
825ccd979bdSMark Fasheh 	 * anyway. */
82634d024f8SMark Fasheh 	if (current == osb->dc_task) {
827ccd979bdSMark Fasheh 		mlog(0, "Skipping delete of %lu because we're currently "
82834d024f8SMark Fasheh 		     "in downconvert\n", inode->i_ino);
829ccd979bdSMark Fasheh 		goto bail;
830ccd979bdSMark Fasheh 	}
831ccd979bdSMark Fasheh 
832ccd979bdSMark Fasheh 	spin_lock(&oi->ip_lock);
833ccd979bdSMark Fasheh 	/* OCFS2 *never* deletes system files. This should technically
834ccd979bdSMark Fasheh 	 * never get here as system file inodes should always have a
835ccd979bdSMark Fasheh 	 * positive link count. */
836ccd979bdSMark Fasheh 	if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
837b0697053SMark Fasheh 		mlog(ML_ERROR, "Skipping delete of system file %llu\n",
838b0697053SMark Fasheh 		     (unsigned long long)oi->ip_blkno);
839ccd979bdSMark Fasheh 		goto bail_unlock;
840ccd979bdSMark Fasheh 	}
841ccd979bdSMark Fasheh 
84234d024f8SMark Fasheh 	/* If we have allowd wipe of this inode for another node, it
84334d024f8SMark Fasheh 	 * will be marked here so we can safely skip it. Recovery will
84434d024f8SMark Fasheh 	 * cleanup any inodes we might inadvertantly skip here. */
845ccd979bdSMark Fasheh 	if (oi->ip_flags & OCFS2_INODE_SKIP_DELETE) {
846ccd979bdSMark Fasheh 		mlog(0, "Skipping delete of %lu because another node "
847ccd979bdSMark Fasheh 		     "has done this for us.\n", inode->i_ino);
848ccd979bdSMark Fasheh 		goto bail_unlock;
849ccd979bdSMark Fasheh 	}
850ccd979bdSMark Fasheh 
851ccd979bdSMark Fasheh 	ret = 1;
852ccd979bdSMark Fasheh bail_unlock:
853ccd979bdSMark Fasheh 	spin_unlock(&oi->ip_lock);
854ccd979bdSMark Fasheh bail:
855ccd979bdSMark Fasheh 	return ret;
856ccd979bdSMark Fasheh }
857ccd979bdSMark Fasheh 
858ccd979bdSMark Fasheh /* Query the cluster to determine whether we should wipe an inode from
859ccd979bdSMark Fasheh  * disk or not.
860ccd979bdSMark Fasheh  *
861ccd979bdSMark Fasheh  * Requires the inode to have the cluster lock. */
862ccd979bdSMark Fasheh static int ocfs2_query_inode_wipe(struct inode *inode,
863ccd979bdSMark Fasheh 				  struct buffer_head *di_bh,
864ccd979bdSMark Fasheh 				  int *wipe)
865ccd979bdSMark Fasheh {
866ccd979bdSMark Fasheh 	int status = 0;
867ccd979bdSMark Fasheh 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
868ccd979bdSMark Fasheh 	struct ocfs2_dinode *di;
869ccd979bdSMark Fasheh 
870ccd979bdSMark Fasheh 	*wipe = 0;
871ccd979bdSMark Fasheh 
872ccd979bdSMark Fasheh 	/* While we were waiting for the cluster lock in
873ccd979bdSMark Fasheh 	 * ocfs2_delete_inode, another node might have asked to delete
874ccd979bdSMark Fasheh 	 * the inode. Recheck our flags to catch this. */
875ccd979bdSMark Fasheh 	if (!ocfs2_inode_is_valid_to_delete(inode)) {
876b0697053SMark Fasheh 		mlog(0, "Skipping delete of %llu because flags changed\n",
877b0697053SMark Fasheh 		     (unsigned long long)oi->ip_blkno);
878ccd979bdSMark Fasheh 		goto bail;
879ccd979bdSMark Fasheh 	}
880ccd979bdSMark Fasheh 
881ccd979bdSMark Fasheh 	/* Now that we have an up to date inode, we can double check
882ccd979bdSMark Fasheh 	 * the link count. */
883ccd979bdSMark Fasheh 	if (inode->i_nlink) {
884b0697053SMark Fasheh 		mlog(0, "Skipping delete of %llu because nlink = %u\n",
885b0697053SMark Fasheh 		     (unsigned long long)oi->ip_blkno, inode->i_nlink);
886ccd979bdSMark Fasheh 		goto bail;
887ccd979bdSMark Fasheh 	}
888ccd979bdSMark Fasheh 
889ccd979bdSMark Fasheh 	/* Do some basic inode verification... */
890ccd979bdSMark Fasheh 	di = (struct ocfs2_dinode *) di_bh->b_data;
891d4cd1871SLi Dongyang 	if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL)) &&
892d4cd1871SLi Dongyang 	    !(oi->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
893b54c2ca4STristan Ye 		/*
894b54c2ca4STristan Ye 		 * Inodes in the orphan dir must have ORPHANED_FL.  The only
895b54c2ca4STristan Ye 		 * inodes that come back out of the orphan dir are reflink
896b54c2ca4STristan Ye 		 * targets. A reflink target may be moved out of the orphan
897b54c2ca4STristan Ye 		 * dir between the time we scan the directory and the time we
898b54c2ca4STristan Ye 		 * process it. This would lead to HAS_REFCOUNT_FL being set but
899b54c2ca4STristan Ye 		 * ORPHANED_FL not.
900b54c2ca4STristan Ye 		 */
901b54c2ca4STristan Ye 		if (di->i_dyn_features & cpu_to_le16(OCFS2_HAS_REFCOUNT_FL)) {
902b54c2ca4STristan Ye 			mlog(0, "Reflinked inode %llu is no longer orphaned.  "
903b54c2ca4STristan Ye 			     "it shouldn't be deleted\n",
904b54c2ca4STristan Ye 			     (unsigned long long)oi->ip_blkno);
905b54c2ca4STristan Ye 			goto bail;
906b54c2ca4STristan Ye 		}
907b54c2ca4STristan Ye 
908ccd979bdSMark Fasheh 		/* for lack of a better error? */
909ccd979bdSMark Fasheh 		status = -EEXIST;
910ccd979bdSMark Fasheh 		mlog(ML_ERROR,
911b0697053SMark Fasheh 		     "Inode %llu (on-disk %llu) not orphaned! "
912ccd979bdSMark Fasheh 		     "Disk flags  0x%x, inode flags 0x%x\n",
913b0697053SMark Fasheh 		     (unsigned long long)oi->ip_blkno,
9141ca1a111SMark Fasheh 		     (unsigned long long)le64_to_cpu(di->i_blkno),
9151ca1a111SMark Fasheh 		     le32_to_cpu(di->i_flags), oi->ip_flags);
916ccd979bdSMark Fasheh 		goto bail;
917ccd979bdSMark Fasheh 	}
918ccd979bdSMark Fasheh 
919ccd979bdSMark Fasheh 	/* has someone already deleted us?! baaad... */
920ccd979bdSMark Fasheh 	if (di->i_dtime) {
921ccd979bdSMark Fasheh 		status = -EEXIST;
922ccd979bdSMark Fasheh 		mlog_errno(status);
923ccd979bdSMark Fasheh 		goto bail;
924ccd979bdSMark Fasheh 	}
925ccd979bdSMark Fasheh 
9266f16bf65SMark Fasheh 	/*
9276f16bf65SMark Fasheh 	 * This is how ocfs2 determines whether an inode is still live
9286f16bf65SMark Fasheh 	 * within the cluster. Every node takes a shared read lock on
9296f16bf65SMark Fasheh 	 * the inode open lock in ocfs2_read_locked_inode(). When we
9306f16bf65SMark Fasheh 	 * get to ->delete_inode(), each node tries to convert it's
9316f16bf65SMark Fasheh 	 * lock to an exclusive. Trylocks are serialized by the inode
9326f16bf65SMark Fasheh 	 * meta data lock. If the upconvert suceeds, we know the inode
9336f16bf65SMark Fasheh 	 * is no longer live and can be deleted.
9346f16bf65SMark Fasheh 	 *
9356f16bf65SMark Fasheh 	 * Though we call this with the meta data lock held, the
9366f16bf65SMark Fasheh 	 * trylock keeps us from ABBA deadlock.
9376f16bf65SMark Fasheh 	 */
9386f16bf65SMark Fasheh 	status = ocfs2_try_open_lock(inode, 1);
93950008630STiger Yang 	if (status == -EAGAIN) {
940ccd979bdSMark Fasheh 		status = 0;
941b0697053SMark Fasheh 		mlog(0, "Skipping delete of %llu because it is in use on "
942b0697053SMark Fasheh 		     "other nodes\n", (unsigned long long)oi->ip_blkno);
943ccd979bdSMark Fasheh 		goto bail;
944ccd979bdSMark Fasheh 	}
945ccd979bdSMark Fasheh 	if (status < 0) {
946ccd979bdSMark Fasheh 		mlog_errno(status);
947ccd979bdSMark Fasheh 		goto bail;
948ccd979bdSMark Fasheh 	}
949ccd979bdSMark Fasheh 
950ccd979bdSMark Fasheh 	*wipe = 1;
95150008630STiger Yang 	mlog(0, "Inode %llu is ok to wipe from orphan dir %u\n",
95250008630STiger Yang 	     (unsigned long long)oi->ip_blkno,
95350008630STiger Yang 	     le16_to_cpu(di->i_orphaned_slot));
954ccd979bdSMark Fasheh 
955ccd979bdSMark Fasheh bail:
956ccd979bdSMark Fasheh 	return status;
957ccd979bdSMark Fasheh }
958ccd979bdSMark Fasheh 
959ccd979bdSMark Fasheh /* Support function for ocfs2_delete_inode. Will help us keep the
960ccd979bdSMark Fasheh  * inode data in a consistent state for clear_inode. Always truncates
961ccd979bdSMark Fasheh  * pages, optionally sync's them first. */
962ccd979bdSMark Fasheh static void ocfs2_cleanup_delete_inode(struct inode *inode,
963ccd979bdSMark Fasheh 				       int sync_data)
964ccd979bdSMark Fasheh {
965b0697053SMark Fasheh 	mlog(0, "Cleanup inode %llu, sync = %d\n",
966b0697053SMark Fasheh 	     (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
967ccd979bdSMark Fasheh 	if (sync_data)
968ccd979bdSMark Fasheh 		write_inode_now(inode, 1);
969ccd979bdSMark Fasheh 	truncate_inode_pages(&inode->i_data, 0);
970ccd979bdSMark Fasheh }
971ccd979bdSMark Fasheh 
972*066d92dcSAl Viro static void ocfs2_delete_inode(struct inode *inode)
973ccd979bdSMark Fasheh {
974ccd979bdSMark Fasheh 	int wipe, status;
975e4b963f1SJoel Becker 	sigset_t oldset;
976ccd979bdSMark Fasheh 	struct buffer_head *di_bh = NULL;
977ccd979bdSMark Fasheh 
978ccd979bdSMark Fasheh 	mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
979ccd979bdSMark Fasheh 
980a90714c1SJan Kara 	/* When we fail in read_inode() we mark inode as bad. The second test
981a90714c1SJan Kara 	 * catches the case when inode allocation fails before allocating
982a90714c1SJan Kara 	 * a block for inode. */
983a90714c1SJan Kara 	if (is_bad_inode(inode) || !OCFS2_I(inode)->ip_blkno) {
984ccd979bdSMark Fasheh 		mlog(0, "Skipping delete of bad inode\n");
985ccd979bdSMark Fasheh 		goto bail;
986ccd979bdSMark Fasheh 	}
987ccd979bdSMark Fasheh 
988871a2931SChristoph Hellwig 	dquot_initialize(inode);
989907f4554SChristoph Hellwig 
990ccd979bdSMark Fasheh 	if (!ocfs2_inode_is_valid_to_delete(inode)) {
991ccd979bdSMark Fasheh 		/* It's probably not necessary to truncate_inode_pages
992ccd979bdSMark Fasheh 		 * here but we do it for safety anyway (it will most
993ccd979bdSMark Fasheh 		 * likely be a no-op anyway) */
994ccd979bdSMark Fasheh 		ocfs2_cleanup_delete_inode(inode, 0);
995ccd979bdSMark Fasheh 		goto bail;
996ccd979bdSMark Fasheh 	}
997ccd979bdSMark Fasheh 
998ccd979bdSMark Fasheh 	/* We want to block signals in delete_inode as the lock and
999ccd979bdSMark Fasheh 	 * messaging paths may return us -ERESTARTSYS. Which would
1000ccd979bdSMark Fasheh 	 * cause us to exit early, resulting in inodes being orphaned
1001ccd979bdSMark Fasheh 	 * forever. */
1002e4b963f1SJoel Becker 	ocfs2_block_signals(&oldset);
1003ccd979bdSMark Fasheh 
10046ca497a8Swengang wang 	/*
10056ca497a8Swengang wang 	 * Synchronize us against ocfs2_get_dentry. We take this in
10066ca497a8Swengang wang 	 * shared mode so that all nodes can still concurrently
10076ca497a8Swengang wang 	 * process deletes.
10086ca497a8Swengang wang 	 */
10096ca497a8Swengang wang 	status = ocfs2_nfs_sync_lock(OCFS2_SB(inode->i_sb), 0);
10106ca497a8Swengang wang 	if (status < 0) {
10116ca497a8Swengang wang 		mlog(ML_ERROR, "getting nfs sync lock(PR) failed %d\n", status);
10126ca497a8Swengang wang 		ocfs2_cleanup_delete_inode(inode, 0);
10136ca497a8Swengang wang 		goto bail_unblock;
10146ca497a8Swengang wang 	}
1015ccd979bdSMark Fasheh 	/* Lock down the inode. This gives us an up to date view of
1016ccd979bdSMark Fasheh 	 * it's metadata (for verification), and allows us to
101734d024f8SMark Fasheh 	 * serialize delete_inode on multiple nodes.
1018ccd979bdSMark Fasheh 	 *
1019ccd979bdSMark Fasheh 	 * Even though we might be doing a truncate, we don't take the
1020ccd979bdSMark Fasheh 	 * allocation lock here as it won't be needed - nobody will
1021ccd979bdSMark Fasheh 	 * have the file open.
1022ccd979bdSMark Fasheh 	 */
1023e63aecb6SMark Fasheh 	status = ocfs2_inode_lock(inode, &di_bh, 1);
1024ccd979bdSMark Fasheh 	if (status < 0) {
1025ccd979bdSMark Fasheh 		if (status != -ENOENT)
1026ccd979bdSMark Fasheh 			mlog_errno(status);
1027ccd979bdSMark Fasheh 		ocfs2_cleanup_delete_inode(inode, 0);
10286ca497a8Swengang wang 		goto bail_unlock_nfs_sync;
1029ccd979bdSMark Fasheh 	}
1030ccd979bdSMark Fasheh 
1031ccd979bdSMark Fasheh 	/* Query the cluster. This will be the final decision made
1032ccd979bdSMark Fasheh 	 * before we go ahead and wipe the inode. */
1033ccd979bdSMark Fasheh 	status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
1034ccd979bdSMark Fasheh 	if (!wipe || status < 0) {
103534d024f8SMark Fasheh 		/* Error and remote inode busy both mean we won't be
1036ccd979bdSMark Fasheh 		 * removing the inode, so they take almost the same
1037ccd979bdSMark Fasheh 		 * path. */
1038ccd979bdSMark Fasheh 		if (status < 0)
1039ccd979bdSMark Fasheh 			mlog_errno(status);
1040ccd979bdSMark Fasheh 
104134d024f8SMark Fasheh 		/* Someone in the cluster has disallowed a wipe of
104234d024f8SMark Fasheh 		 * this inode, or it was never completely
104334d024f8SMark Fasheh 		 * orphaned. Write out the pages and exit now. */
1044ccd979bdSMark Fasheh 		ocfs2_cleanup_delete_inode(inode, 1);
1045ccd979bdSMark Fasheh 		goto bail_unlock_inode;
1046ccd979bdSMark Fasheh 	}
1047ccd979bdSMark Fasheh 
1048ccd979bdSMark Fasheh 	ocfs2_cleanup_delete_inode(inode, 0);
1049ccd979bdSMark Fasheh 
1050ccd979bdSMark Fasheh 	status = ocfs2_wipe_inode(inode, di_bh);
1051ccd979bdSMark Fasheh 	if (status < 0) {
1052b4df6ed8SMark Fasheh 		if (status != -EDEADLK)
1053ccd979bdSMark Fasheh 			mlog_errno(status);
1054ccd979bdSMark Fasheh 		goto bail_unlock_inode;
1055ccd979bdSMark Fasheh 	}
1056ccd979bdSMark Fasheh 
105724c19ef4SMark Fasheh 	/*
105824c19ef4SMark Fasheh 	 * Mark the inode as successfully deleted.
105924c19ef4SMark Fasheh 	 *
106024c19ef4SMark Fasheh 	 * This is important for ocfs2_clear_inode() as it will check
106124c19ef4SMark Fasheh 	 * this flag and skip any checkpointing work
106224c19ef4SMark Fasheh 	 *
106324c19ef4SMark Fasheh 	 * ocfs2_stuff_meta_lvb() also uses this flag to invalidate
106424c19ef4SMark Fasheh 	 * the LVB for other nodes.
106524c19ef4SMark Fasheh 	 */
1066ccd979bdSMark Fasheh 	OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
1067ccd979bdSMark Fasheh 
1068ccd979bdSMark Fasheh bail_unlock_inode:
1069e63aecb6SMark Fasheh 	ocfs2_inode_unlock(inode, 1);
1070ccd979bdSMark Fasheh 	brelse(di_bh);
10716ca497a8Swengang wang 
10726ca497a8Swengang wang bail_unlock_nfs_sync:
10736ca497a8Swengang wang 	ocfs2_nfs_sync_unlock(OCFS2_SB(inode->i_sb), 0);
10746ca497a8Swengang wang 
1075ccd979bdSMark Fasheh bail_unblock:
1076e4b963f1SJoel Becker 	ocfs2_unblock_signals(&oldset);
1077ccd979bdSMark Fasheh bail:
1078ccd979bdSMark Fasheh 	mlog_exit_void();
1079ccd979bdSMark Fasheh }
1080ccd979bdSMark Fasheh 
1081*066d92dcSAl Viro static void ocfs2_clear_inode(struct inode *inode)
1082ccd979bdSMark Fasheh {
1083ccd979bdSMark Fasheh 	int status;
1084ccd979bdSMark Fasheh 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1085ccd979bdSMark Fasheh 
1086ccd979bdSMark Fasheh 	mlog_entry_void();
1087ccd979bdSMark Fasheh 
1088*066d92dcSAl Viro 	end_writeback(inode);
1089b0697053SMark Fasheh 	mlog(0, "Clearing inode: %llu, nlink = %u\n",
1090b0697053SMark Fasheh 	     (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_nlink);
1091ccd979bdSMark Fasheh 
1092ccd979bdSMark Fasheh 	mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
1093ccd979bdSMark Fasheh 			"Inode=%lu\n", inode->i_ino);
1094ccd979bdSMark Fasheh 
10959f754758SChristoph Hellwig 	dquot_drop(inode);
1096257ba15cSChristoph Hellwig 
109734d024f8SMark Fasheh 	/* To preven remote deletes we hold open lock before, now it
109834d024f8SMark Fasheh 	 * is time to unlock PR and EX open locks. */
109950008630STiger Yang 	ocfs2_open_unlock(inode);
110050008630STiger Yang 
1101ccd979bdSMark Fasheh 	/* Do these before all the other work so that we don't bounce
110234d024f8SMark Fasheh 	 * the downconvert thread while waiting to destroy the locks. */
1103ccd979bdSMark Fasheh 	ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres);
1104e63aecb6SMark Fasheh 	ocfs2_mark_lockres_freeing(&oi->ip_inode_lockres);
110550008630STiger Yang 	ocfs2_mark_lockres_freeing(&oi->ip_open_lockres);
1106ccd979bdSMark Fasheh 
11074fe370afSMark Fasheh 	ocfs2_resv_discard(&OCFS2_SB(inode->i_sb)->osb_la_resmap,
11084fe370afSMark Fasheh 			   &oi->ip_la_data_resv);
11094fe370afSMark Fasheh 	ocfs2_resv_init_once(&oi->ip_la_data_resv);
11104fe370afSMark Fasheh 
1111ccd979bdSMark Fasheh 	/* We very well may get a clear_inode before all an inodes
1112ccd979bdSMark Fasheh 	 * metadata has hit disk. Of course, we can't drop any cluster
1113ccd979bdSMark Fasheh 	 * locks until the journal has finished with it. The only
1114ccd979bdSMark Fasheh 	 * exception here are successfully wiped inodes - their
1115ccd979bdSMark Fasheh 	 * metadata can now be considered to be part of the system
1116ccd979bdSMark Fasheh 	 * inodes from which it came. */
1117ccd979bdSMark Fasheh 	if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED))
1118ccd979bdSMark Fasheh 		ocfs2_checkpoint_inode(inode);
1119ccd979bdSMark Fasheh 
1120ccd979bdSMark Fasheh 	mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
1121b0697053SMark Fasheh 			"Clear inode of %llu, inode has io markers\n",
1122b0697053SMark Fasheh 			(unsigned long long)oi->ip_blkno);
1123ccd979bdSMark Fasheh 
112483418978SMark Fasheh 	ocfs2_extent_map_trunc(inode, 0);
112583418978SMark Fasheh 
1126ccd979bdSMark Fasheh 	status = ocfs2_drop_inode_locks(inode);
1127ccd979bdSMark Fasheh 	if (status < 0)
1128ccd979bdSMark Fasheh 		mlog_errno(status);
1129ccd979bdSMark Fasheh 
1130ccd979bdSMark Fasheh 	ocfs2_lock_res_free(&oi->ip_rw_lockres);
1131e63aecb6SMark Fasheh 	ocfs2_lock_res_free(&oi->ip_inode_lockres);
113250008630STiger Yang 	ocfs2_lock_res_free(&oi->ip_open_lockres);
1133ccd979bdSMark Fasheh 
113466fb345dSJoel Becker 	ocfs2_metadata_cache_exit(INODE_CACHE(inode));
1135ccd979bdSMark Fasheh 
11368cb471e8SJoel Becker 	mlog_bug_on_msg(INODE_CACHE(inode)->ci_num_cached,
1137b0697053SMark Fasheh 			"Clear inode of %llu, inode has %u cache items\n",
11388cb471e8SJoel Becker 			(unsigned long long)oi->ip_blkno,
11398cb471e8SJoel Becker 			INODE_CACHE(inode)->ci_num_cached);
1140ccd979bdSMark Fasheh 
11418cb471e8SJoel Becker 	mlog_bug_on_msg(!(INODE_CACHE(inode)->ci_flags & OCFS2_CACHE_FL_INLINE),
1142b0697053SMark Fasheh 			"Clear inode of %llu, inode has a bad flag\n",
1143b0697053SMark Fasheh 			(unsigned long long)oi->ip_blkno);
1144ccd979bdSMark Fasheh 
1145ccd979bdSMark Fasheh 	mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
1146b0697053SMark Fasheh 			"Clear inode of %llu, inode is locked\n",
1147b0697053SMark Fasheh 			(unsigned long long)oi->ip_blkno);
1148ccd979bdSMark Fasheh 
1149251b6eccSMark Fasheh 	mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
1150b0697053SMark Fasheh 			"Clear inode of %llu, io_mutex is locked\n",
1151b0697053SMark Fasheh 			(unsigned long long)oi->ip_blkno);
1152251b6eccSMark Fasheh 	mutex_unlock(&oi->ip_io_mutex);
1153ccd979bdSMark Fasheh 
1154ccd979bdSMark Fasheh 	/*
1155ccd979bdSMark Fasheh 	 * down_trylock() returns 0, down_write_trylock() returns 1
1156ccd979bdSMark Fasheh 	 * kernel 1, world 0
1157ccd979bdSMark Fasheh 	 */
1158ccd979bdSMark Fasheh 	mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
1159b0697053SMark Fasheh 			"Clear inode of %llu, alloc_sem is locked\n",
1160b0697053SMark Fasheh 			(unsigned long long)oi->ip_blkno);
1161ccd979bdSMark Fasheh 	up_write(&oi->ip_alloc_sem);
1162ccd979bdSMark Fasheh 
1163ccd979bdSMark Fasheh 	mlog_bug_on_msg(oi->ip_open_count,
1164b0697053SMark Fasheh 			"Clear inode of %llu has open count %d\n",
1165b0697053SMark Fasheh 			(unsigned long long)oi->ip_blkno, oi->ip_open_count);
1166ccd979bdSMark Fasheh 
1167ccd979bdSMark Fasheh 	/* Clear all other flags. */
116847460d65SJoel Becker 	oi->ip_flags = 0;
1169ccd979bdSMark Fasheh 	oi->ip_dir_start_lookup = 0;
1170ccd979bdSMark Fasheh 	oi->ip_blkno = 0ULL;
1171ae0dff68SSunil Mushran 
1172ae0dff68SSunil Mushran 	/*
1173ae0dff68SSunil Mushran 	 * ip_jinode is used to track txns against this inode. We ensure that
1174ae0dff68SSunil Mushran 	 * the journal is flushed before journal shutdown. Thus it is safe to
1175ae0dff68SSunil Mushran 	 * have inodes get cleaned up after journal shutdown.
1176ae0dff68SSunil Mushran 	 */
11772b4e30fbSJoel Becker 	jbd2_journal_release_jbd_inode(OCFS2_SB(inode->i_sb)->journal->j_journal,
11782b4e30fbSJoel Becker 				       &oi->ip_jinode);
1179ccd979bdSMark Fasheh 
1180ccd979bdSMark Fasheh 	mlog_exit_void();
1181ccd979bdSMark Fasheh }
1182ccd979bdSMark Fasheh 
1183*066d92dcSAl Viro void ocfs2_evict_inode(struct inode *inode)
1184*066d92dcSAl Viro {
1185*066d92dcSAl Viro 	if (!inode->i_nlink ||
1186*066d92dcSAl Viro 	    (OCFS2_I(inode)->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)) {
1187*066d92dcSAl Viro 		ocfs2_delete_inode(inode);
1188*066d92dcSAl Viro 	} else {
1189*066d92dcSAl Viro 		truncate_inode_pages(&inode->i_data, 0);
1190*066d92dcSAl Viro 	}
1191*066d92dcSAl Viro 	ocfs2_clear_inode(inode);
1192*066d92dcSAl Viro }
1193*066d92dcSAl Viro 
1194ccd979bdSMark Fasheh /* Called under inode_lock, with no more references on the
1195ccd979bdSMark Fasheh  * struct inode, so it's safe here to check the flags field
1196ccd979bdSMark Fasheh  * and to manipulate i_nlink without any other locks. */
1197ccd979bdSMark Fasheh void ocfs2_drop_inode(struct inode *inode)
1198ccd979bdSMark Fasheh {
1199ccd979bdSMark Fasheh 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1200ccd979bdSMark Fasheh 
1201ccd979bdSMark Fasheh 	mlog_entry_void();
1202ccd979bdSMark Fasheh 
1203b0697053SMark Fasheh 	mlog(0, "Drop inode %llu, nlink = %u, ip_flags = 0x%x\n",
1204b0697053SMark Fasheh 	     (unsigned long long)oi->ip_blkno, inode->i_nlink, oi->ip_flags);
1205ccd979bdSMark Fasheh 
1206379dfe9dSMark Fasheh 	if (oi->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)
1207379dfe9dSMark Fasheh 		generic_delete_inode(inode);
1208379dfe9dSMark Fasheh 	else
1209ccd979bdSMark Fasheh 		generic_drop_inode(inode);
1210ccd979bdSMark Fasheh 
1211ccd979bdSMark Fasheh 	mlog_exit_void();
1212ccd979bdSMark Fasheh }
1213ccd979bdSMark Fasheh 
1214ccd979bdSMark Fasheh /*
1215ccd979bdSMark Fasheh  * This is called from our getattr.
1216ccd979bdSMark Fasheh  */
1217ccd979bdSMark Fasheh int ocfs2_inode_revalidate(struct dentry *dentry)
1218ccd979bdSMark Fasheh {
1219ccd979bdSMark Fasheh 	struct inode *inode = dentry->d_inode;
1220ccd979bdSMark Fasheh 	int status = 0;
1221ccd979bdSMark Fasheh 
1222b0697053SMark Fasheh 	mlog_entry("(inode = 0x%p, ino = %llu)\n", inode,
1223b0697053SMark Fasheh 		   inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL);
1224ccd979bdSMark Fasheh 
1225ccd979bdSMark Fasheh 	if (!inode) {
1226ccd979bdSMark Fasheh 		mlog(0, "eep, no inode!\n");
1227ccd979bdSMark Fasheh 		status = -ENOENT;
1228ccd979bdSMark Fasheh 		goto bail;
1229ccd979bdSMark Fasheh 	}
1230ccd979bdSMark Fasheh 
1231ccd979bdSMark Fasheh 	spin_lock(&OCFS2_I(inode)->ip_lock);
1232ccd979bdSMark Fasheh 	if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
1233ccd979bdSMark Fasheh 		spin_unlock(&OCFS2_I(inode)->ip_lock);
1234ccd979bdSMark Fasheh 		mlog(0, "inode deleted!\n");
1235ccd979bdSMark Fasheh 		status = -ENOENT;
1236ccd979bdSMark Fasheh 		goto bail;
1237ccd979bdSMark Fasheh 	}
1238ccd979bdSMark Fasheh 	spin_unlock(&OCFS2_I(inode)->ip_lock);
1239ccd979bdSMark Fasheh 
1240e63aecb6SMark Fasheh 	/* Let ocfs2_inode_lock do the work of updating our struct
1241ccd979bdSMark Fasheh 	 * inode for us. */
1242e63aecb6SMark Fasheh 	status = ocfs2_inode_lock(inode, NULL, 0);
1243ccd979bdSMark Fasheh 	if (status < 0) {
1244ccd979bdSMark Fasheh 		if (status != -ENOENT)
1245ccd979bdSMark Fasheh 			mlog_errno(status);
1246ccd979bdSMark Fasheh 		goto bail;
1247ccd979bdSMark Fasheh 	}
1248e63aecb6SMark Fasheh 	ocfs2_inode_unlock(inode, 0);
1249ccd979bdSMark Fasheh bail:
1250ccd979bdSMark Fasheh 	mlog_exit(status);
1251ccd979bdSMark Fasheh 
1252ccd979bdSMark Fasheh 	return status;
1253ccd979bdSMark Fasheh }
1254ccd979bdSMark Fasheh 
1255ccd979bdSMark Fasheh /*
1256ccd979bdSMark Fasheh  * Updates a disk inode from a
1257ccd979bdSMark Fasheh  * struct inode.
1258ccd979bdSMark Fasheh  * Only takes ip_lock.
1259ccd979bdSMark Fasheh  */
12601fabe148SMark Fasheh int ocfs2_mark_inode_dirty(handle_t *handle,
1261ccd979bdSMark Fasheh 			   struct inode *inode,
1262ccd979bdSMark Fasheh 			   struct buffer_head *bh)
1263ccd979bdSMark Fasheh {
1264ccd979bdSMark Fasheh 	int status;
1265ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
1266ccd979bdSMark Fasheh 
1267b0697053SMark Fasheh 	mlog_entry("(inode %llu)\n",
1268b0697053SMark Fasheh 		   (unsigned long long)OCFS2_I(inode)->ip_blkno);
1269ccd979bdSMark Fasheh 
12700cf2f763SJoel Becker 	status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
1271ccd979bdSMark Fasheh 					 OCFS2_JOURNAL_ACCESS_WRITE);
1272ccd979bdSMark Fasheh 	if (status < 0) {
1273ccd979bdSMark Fasheh 		mlog_errno(status);
1274ccd979bdSMark Fasheh 		goto leave;
1275ccd979bdSMark Fasheh 	}
1276ccd979bdSMark Fasheh 
1277ccd979bdSMark Fasheh 	spin_lock(&OCFS2_I(inode)->ip_lock);
1278ccd979bdSMark Fasheh 	fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
12796e4b0d56SJan Kara 	ocfs2_get_inode_flags(OCFS2_I(inode));
1280ca4d147eSHerbert Poetzl 	fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
128115b1e36bSMark Fasheh 	fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
1282ccd979bdSMark Fasheh 	spin_unlock(&OCFS2_I(inode)->ip_lock);
1283ccd979bdSMark Fasheh 
1284ccd979bdSMark Fasheh 	fe->i_size = cpu_to_le64(i_size_read(inode));
1285198a1ca3SMark Fasheh 	ocfs2_set_links_count(fe, inode->i_nlink);
1286ccd979bdSMark Fasheh 	fe->i_uid = cpu_to_le32(inode->i_uid);
1287ccd979bdSMark Fasheh 	fe->i_gid = cpu_to_le32(inode->i_gid);
1288ccd979bdSMark Fasheh 	fe->i_mode = cpu_to_le16(inode->i_mode);
1289ccd979bdSMark Fasheh 	fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
1290ccd979bdSMark Fasheh 	fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
1291ccd979bdSMark Fasheh 	fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1292ccd979bdSMark Fasheh 	fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1293ccd979bdSMark Fasheh 	fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
1294ccd979bdSMark Fasheh 	fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
1295ccd979bdSMark Fasheh 
1296ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, bh);
1297ccd979bdSMark Fasheh leave:
1298ccd979bdSMark Fasheh 	mlog_exit(status);
1299ccd979bdSMark Fasheh 	return status;
1300ccd979bdSMark Fasheh }
1301ccd979bdSMark Fasheh 
1302ccd979bdSMark Fasheh /*
1303ccd979bdSMark Fasheh  *
1304ccd979bdSMark Fasheh  * Updates a struct inode from a disk inode.
1305ccd979bdSMark Fasheh  * does no i/o, only takes ip_lock.
1306ccd979bdSMark Fasheh  */
1307ccd979bdSMark Fasheh void ocfs2_refresh_inode(struct inode *inode,
1308ccd979bdSMark Fasheh 			 struct ocfs2_dinode *fe)
1309ccd979bdSMark Fasheh {
1310ccd979bdSMark Fasheh 	spin_lock(&OCFS2_I(inode)->ip_lock);
1311ccd979bdSMark Fasheh 
1312ccd979bdSMark Fasheh 	OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1313ca4d147eSHerbert Poetzl 	OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
131415b1e36bSMark Fasheh 	OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
1315ca4d147eSHerbert Poetzl 	ocfs2_set_inode_flags(inode);
1316ccd979bdSMark Fasheh 	i_size_write(inode, le64_to_cpu(fe->i_size));
1317198a1ca3SMark Fasheh 	inode->i_nlink = ocfs2_read_links_count(fe);
1318ccd979bdSMark Fasheh 	inode->i_uid = le32_to_cpu(fe->i_uid);
1319ccd979bdSMark Fasheh 	inode->i_gid = le32_to_cpu(fe->i_gid);
1320ccd979bdSMark Fasheh 	inode->i_mode = le16_to_cpu(fe->i_mode);
1321ccd979bdSMark Fasheh 	if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
1322ccd979bdSMark Fasheh 		inode->i_blocks = 0;
1323ccd979bdSMark Fasheh 	else
13248110b073SMark Fasheh 		inode->i_blocks = ocfs2_inode_sector_count(inode);
1325ccd979bdSMark Fasheh 	inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
1326ccd979bdSMark Fasheh 	inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
1327ccd979bdSMark Fasheh 	inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
1328ccd979bdSMark Fasheh 	inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
1329ccd979bdSMark Fasheh 	inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
1330ccd979bdSMark Fasheh 	inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
1331ccd979bdSMark Fasheh 
1332ccd979bdSMark Fasheh 	spin_unlock(&OCFS2_I(inode)->ip_lock);
1333ccd979bdSMark Fasheh }
1334b657c95cSJoel Becker 
1335b657c95cSJoel Becker int ocfs2_validate_inode_block(struct super_block *sb,
1336b657c95cSJoel Becker 			       struct buffer_head *bh)
1337b657c95cSJoel Becker {
1338d6b32bbbSJoel Becker 	int rc;
1339b657c95cSJoel Becker 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1340b657c95cSJoel Becker 
1341970e4936SJoel Becker 	mlog(0, "Validating dinode %llu\n",
1342970e4936SJoel Becker 	     (unsigned long long)bh->b_blocknr);
1343970e4936SJoel Becker 
1344b657c95cSJoel Becker 	BUG_ON(!buffer_uptodate(bh));
1345b657c95cSJoel Becker 
1346d6b32bbbSJoel Becker 	/*
1347d6b32bbbSJoel Becker 	 * If the ecc fails, we return the error but otherwise
1348d6b32bbbSJoel Becker 	 * leave the filesystem running.  We know any error is
1349d6b32bbbSJoel Becker 	 * local to this block.
1350d6b32bbbSJoel Becker 	 */
1351d6b32bbbSJoel Becker 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check);
135213723d00SJoel Becker 	if (rc) {
135313723d00SJoel Becker 		mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
135413723d00SJoel Becker 		     (unsigned long long)bh->b_blocknr);
1355d6b32bbbSJoel Becker 		goto bail;
135613723d00SJoel Becker 	}
1357d6b32bbbSJoel Becker 
1358d6b32bbbSJoel Becker 	/*
1359d6b32bbbSJoel Becker 	 * Errors after here are fatal.
1360d6b32bbbSJoel Becker 	 */
1361d6b32bbbSJoel Becker 
1362d6b32bbbSJoel Becker 	rc = -EINVAL;
1363d6b32bbbSJoel Becker 
1364b657c95cSJoel Becker 	if (!OCFS2_IS_VALID_DINODE(di)) {
1365b657c95cSJoel Becker 		ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s\n",
1366b657c95cSJoel Becker 			    (unsigned long long)bh->b_blocknr, 7,
1367b657c95cSJoel Becker 			    di->i_signature);
1368b657c95cSJoel Becker 		goto bail;
1369b657c95cSJoel Becker 	}
1370b657c95cSJoel Becker 
1371b657c95cSJoel Becker 	if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
1372b657c95cSJoel Becker 		ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n",
1373b657c95cSJoel Becker 			    (unsigned long long)bh->b_blocknr,
1374b657c95cSJoel Becker 			    (unsigned long long)le64_to_cpu(di->i_blkno));
1375b657c95cSJoel Becker 		goto bail;
1376b657c95cSJoel Becker 	}
1377b657c95cSJoel Becker 
1378b657c95cSJoel Becker 	if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
1379b657c95cSJoel Becker 		ocfs2_error(sb,
1380b657c95cSJoel Becker 			    "Invalid dinode #%llu: OCFS2_VALID_FL not set\n",
1381b657c95cSJoel Becker 			    (unsigned long long)bh->b_blocknr);
1382b657c95cSJoel Becker 		goto bail;
1383b657c95cSJoel Becker 	}
1384b657c95cSJoel Becker 
1385b657c95cSJoel Becker 	if (le32_to_cpu(di->i_fs_generation) !=
1386b657c95cSJoel Becker 	    OCFS2_SB(sb)->fs_generation) {
1387b657c95cSJoel Becker 		ocfs2_error(sb,
1388b657c95cSJoel Becker 			    "Invalid dinode #%llu: fs_generation is %u\n",
1389b657c95cSJoel Becker 			    (unsigned long long)bh->b_blocknr,
1390b657c95cSJoel Becker 			    le32_to_cpu(di->i_fs_generation));
1391b657c95cSJoel Becker 		goto bail;
1392b657c95cSJoel Becker 	}
1393b657c95cSJoel Becker 
1394b657c95cSJoel Becker 	rc = 0;
1395b657c95cSJoel Becker 
1396b657c95cSJoel Becker bail:
1397b657c95cSJoel Becker 	return rc;
1398b657c95cSJoel Becker }
1399b657c95cSJoel Becker 
1400b657c95cSJoel Becker int ocfs2_read_inode_block_full(struct inode *inode, struct buffer_head **bh,
1401b657c95cSJoel Becker 				int flags)
1402b657c95cSJoel Becker {
1403b657c95cSJoel Becker 	int rc;
1404b657c95cSJoel Becker 	struct buffer_head *tmp = *bh;
1405b657c95cSJoel Becker 
14068cb471e8SJoel Becker 	rc = ocfs2_read_blocks(INODE_CACHE(inode), OCFS2_I(inode)->ip_blkno,
14078cb471e8SJoel Becker 			       1, &tmp, flags, ocfs2_validate_inode_block);
1408b657c95cSJoel Becker 
1409b657c95cSJoel Becker 	/* If ocfs2_read_blocks() got us a new bh, pass it up. */
1410970e4936SJoel Becker 	if (!rc && !*bh)
1411b657c95cSJoel Becker 		*bh = tmp;
1412b657c95cSJoel Becker 
1413b657c95cSJoel Becker 	return rc;
1414b657c95cSJoel Becker }
1415b657c95cSJoel Becker 
1416b657c95cSJoel Becker int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh)
1417b657c95cSJoel Becker {
1418b657c95cSJoel Becker 	return ocfs2_read_inode_block_full(inode, bh, 0);
1419b657c95cSJoel Becker }
14206e5a3d75SJoel Becker 
14216e5a3d75SJoel Becker 
14226e5a3d75SJoel Becker static u64 ocfs2_inode_cache_owner(struct ocfs2_caching_info *ci)
14236e5a3d75SJoel Becker {
14246e5a3d75SJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
14256e5a3d75SJoel Becker 
14266e5a3d75SJoel Becker 	return oi->ip_blkno;
14276e5a3d75SJoel Becker }
14286e5a3d75SJoel Becker 
14298cb471e8SJoel Becker static struct super_block *ocfs2_inode_cache_get_super(struct ocfs2_caching_info *ci)
14308cb471e8SJoel Becker {
14318cb471e8SJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
14328cb471e8SJoel Becker 
14338cb471e8SJoel Becker 	return oi->vfs_inode.i_sb;
14348cb471e8SJoel Becker }
14358cb471e8SJoel Becker 
14366e5a3d75SJoel Becker static void ocfs2_inode_cache_lock(struct ocfs2_caching_info *ci)
14376e5a3d75SJoel Becker {
14386e5a3d75SJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
14396e5a3d75SJoel Becker 
14406e5a3d75SJoel Becker 	spin_lock(&oi->ip_lock);
14416e5a3d75SJoel Becker }
14426e5a3d75SJoel Becker 
14436e5a3d75SJoel Becker static void ocfs2_inode_cache_unlock(struct ocfs2_caching_info *ci)
14446e5a3d75SJoel Becker {
14456e5a3d75SJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
14466e5a3d75SJoel Becker 
14476e5a3d75SJoel Becker 	spin_unlock(&oi->ip_lock);
14486e5a3d75SJoel Becker }
14496e5a3d75SJoel Becker 
14506e5a3d75SJoel Becker static void ocfs2_inode_cache_io_lock(struct ocfs2_caching_info *ci)
14516e5a3d75SJoel Becker {
14526e5a3d75SJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
14536e5a3d75SJoel Becker 
14546e5a3d75SJoel Becker 	mutex_lock(&oi->ip_io_mutex);
14556e5a3d75SJoel Becker }
14566e5a3d75SJoel Becker 
14576e5a3d75SJoel Becker static void ocfs2_inode_cache_io_unlock(struct ocfs2_caching_info *ci)
14586e5a3d75SJoel Becker {
14596e5a3d75SJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
14606e5a3d75SJoel Becker 
14616e5a3d75SJoel Becker 	mutex_unlock(&oi->ip_io_mutex);
14626e5a3d75SJoel Becker }
14636e5a3d75SJoel Becker 
14646e5a3d75SJoel Becker const struct ocfs2_caching_operations ocfs2_inode_caching_ops = {
14656e5a3d75SJoel Becker 	.co_owner		= ocfs2_inode_cache_owner,
14668cb471e8SJoel Becker 	.co_get_super		= ocfs2_inode_cache_get_super,
14676e5a3d75SJoel Becker 	.co_cache_lock		= ocfs2_inode_cache_lock,
14686e5a3d75SJoel Becker 	.co_cache_unlock	= ocfs2_inode_cache_unlock,
14696e5a3d75SJoel Becker 	.co_io_lock		= ocfs2_inode_cache_io_lock,
14706e5a3d75SJoel Becker 	.co_io_unlock		= ocfs2_inode_cache_io_unlock,
14716e5a3d75SJoel Becker };
14726e5a3d75SJoel Becker 
1473