xref: /openbmc/linux/fs/ocfs2/inode.c (revision 64f3b2692724fdbe72697b77887685b5bd3f4a3c)
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 #include <cluster/masklog.h>
35ccd979bdSMark Fasheh 
36ccd979bdSMark Fasheh #include "ocfs2.h"
37ccd979bdSMark Fasheh 
38ccd979bdSMark Fasheh #include "alloc.h"
399b7895efSMark Fasheh #include "dir.h"
40d6b32bbbSJoel Becker #include "blockcheck.h"
41ccd979bdSMark Fasheh #include "dlmglue.h"
42ccd979bdSMark Fasheh #include "extent_map.h"
43ccd979bdSMark Fasheh #include "file.h"
44b4df6ed8SMark Fasheh #include "heartbeat.h"
45ccd979bdSMark Fasheh #include "inode.h"
46ccd979bdSMark Fasheh #include "journal.h"
47ccd979bdSMark Fasheh #include "namei.h"
48ccd979bdSMark Fasheh #include "suballoc.h"
49ccd979bdSMark Fasheh #include "super.h"
50ccd979bdSMark Fasheh #include "symlink.h"
51ccd979bdSMark Fasheh #include "sysfile.h"
52ccd979bdSMark Fasheh #include "uptodate.h"
53cf1d6c76STiger Yang #include "xattr.h"
548b2c0dbaSTao Ma #include "refcounttree.h"
55*64f3b269STao Ma #include "ocfs2_trace.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 
134*64f3b269STao Ma 	trace_ocfs2_iget_begin((unsigned long long)blkno, flags,
135*64f3b269STao Ma 			       sysfile_type);
136ccd979bdSMark Fasheh 
137ccd979bdSMark Fasheh 	/* Ok. By now we've either got the offsets passed to us by the
138ccd979bdSMark Fasheh 	 * caller, or we just pulled them off the bh. Lets do some
139ccd979bdSMark Fasheh 	 * sanity checks to make sure they're OK. */
140ccd979bdSMark Fasheh 	if (blkno == 0) {
141ccd979bdSMark Fasheh 		inode = ERR_PTR(-EINVAL);
142ccd979bdSMark Fasheh 		mlog_errno(PTR_ERR(inode));
143ccd979bdSMark Fasheh 		goto bail;
144ccd979bdSMark Fasheh 	}
145ccd979bdSMark Fasheh 
146ccd979bdSMark Fasheh 	args.fi_blkno = blkno;
14724c19ef4SMark Fasheh 	args.fi_flags = flags;
148ccd979bdSMark Fasheh 	args.fi_ino = ino_from_blkno(sb, blkno);
1495fa0613eSJan Kara 	args.fi_sysfile_type = sysfile_type;
150ccd979bdSMark Fasheh 
151ccd979bdSMark Fasheh 	inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor,
152ccd979bdSMark Fasheh 			     ocfs2_init_locked_inode, &args);
153ccd979bdSMark Fasheh 	/* inode was *not* in the inode cache. 2.6.x requires
154ccd979bdSMark Fasheh 	 * us to do our own read_inode call and unlock it
155ccd979bdSMark Fasheh 	 * afterwards. */
156ccd979bdSMark Fasheh 	if (inode == NULL) {
157ccd979bdSMark Fasheh 		inode = ERR_PTR(-ENOMEM);
158ccd979bdSMark Fasheh 		mlog_errno(PTR_ERR(inode));
159ccd979bdSMark Fasheh 		goto bail;
160ccd979bdSMark Fasheh 	}
161*64f3b269STao Ma 	trace_ocfs2_iget5_locked(inode->i_state);
1626218b90eSTao Ma 	if (inode->i_state & I_NEW) {
1636218b90eSTao Ma 		ocfs2_read_locked_inode(inode, &args);
1646218b90eSTao Ma 		unlock_new_inode(inode);
1656218b90eSTao Ma 	}
166ccd979bdSMark Fasheh 	if (is_bad_inode(inode)) {
167ccd979bdSMark Fasheh 		iput(inode);
168ccd979bdSMark Fasheh 		inode = ERR_PTR(-ESTALE);
169ccd979bdSMark Fasheh 		goto bail;
170ccd979bdSMark Fasheh 	}
171ccd979bdSMark Fasheh 
172ccd979bdSMark Fasheh bail:
173ccd979bdSMark Fasheh 	if (!IS_ERR(inode)) {
174*64f3b269STao Ma 		trace_ocfs2_iget_end(inode,
175b0697053SMark Fasheh 			(unsigned long long)OCFS2_I(inode)->ip_blkno);
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 	args = opaque;
196ccd979bdSMark Fasheh 
197ccd979bdSMark Fasheh 	mlog_bug_on_msg(!inode, "No inode in find actor!\n");
198ccd979bdSMark Fasheh 
199*64f3b269STao Ma 	trace_ocfs2_find_actor(inode, inode->i_ino, opaque, args->fi_blkno);
200*64f3b269STao Ma 
201ccd979bdSMark Fasheh 	if (oi->ip_blkno != args->fi_blkno)
202ccd979bdSMark Fasheh 		goto bail;
203ccd979bdSMark Fasheh 
204ccd979bdSMark Fasheh 	ret = 1;
205ccd979bdSMark Fasheh bail:
206ccd979bdSMark Fasheh 	return ret;
207ccd979bdSMark Fasheh }
208ccd979bdSMark Fasheh 
209ccd979bdSMark Fasheh /*
210ccd979bdSMark Fasheh  * initialize the new inode, but don't do anything that would cause
211ccd979bdSMark Fasheh  * us to sleep.
212ccd979bdSMark Fasheh  * return 0 on success, 1 on failure
213ccd979bdSMark Fasheh  */
214ccd979bdSMark Fasheh static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
215ccd979bdSMark Fasheh {
216ccd979bdSMark Fasheh 	struct ocfs2_find_inode_args *args = opaque;
217cb25797dSJan Kara 	static struct lock_class_key ocfs2_quota_ip_alloc_sem_key,
218cb25797dSJan Kara 				     ocfs2_file_ip_alloc_sem_key;
219ccd979bdSMark Fasheh 
220ccd979bdSMark Fasheh 	inode->i_ino = args->fi_ino;
221ccd979bdSMark Fasheh 	OCFS2_I(inode)->ip_blkno = args->fi_blkno;
2225fa0613eSJan Kara 	if (args->fi_sysfile_type != 0)
2235fa0613eSJan Kara 		lockdep_set_class(&inode->i_mutex,
2245fa0613eSJan Kara 			&ocfs2_sysfile_lock_key[args->fi_sysfile_type]);
225cb25797dSJan Kara 	if (args->fi_sysfile_type == USER_QUOTA_SYSTEM_INODE ||
226cb25797dSJan Kara 	    args->fi_sysfile_type == GROUP_QUOTA_SYSTEM_INODE ||
227cb25797dSJan Kara 	    args->fi_sysfile_type == LOCAL_USER_QUOTA_SYSTEM_INODE ||
228cb25797dSJan Kara 	    args->fi_sysfile_type == LOCAL_GROUP_QUOTA_SYSTEM_INODE)
229cb25797dSJan Kara 		lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem,
230cb25797dSJan Kara 				  &ocfs2_quota_ip_alloc_sem_key);
231cb25797dSJan Kara 	else
232cb25797dSJan Kara 		lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem,
233cb25797dSJan Kara 				  &ocfs2_file_ip_alloc_sem_key);
234ccd979bdSMark Fasheh 
235ccd979bdSMark Fasheh 	return 0;
236ccd979bdSMark Fasheh }
237ccd979bdSMark Fasheh 
238b657c95cSJoel Becker void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
239ccd979bdSMark Fasheh 			  int create_ino)
240ccd979bdSMark Fasheh {
241ccd979bdSMark Fasheh 	struct super_block *sb;
242ccd979bdSMark Fasheh 	struct ocfs2_super *osb;
24353da4939SMark Fasheh 	int use_plocks = 1;
244ccd979bdSMark Fasheh 
245ccd979bdSMark Fasheh 	sb = inode->i_sb;
246ccd979bdSMark Fasheh 	osb = OCFS2_SB(sb);
247ccd979bdSMark Fasheh 
24853da4939SMark Fasheh 	if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
24953da4939SMark Fasheh 	    ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks())
25053da4939SMark Fasheh 		use_plocks = 0;
25153da4939SMark Fasheh 
252b657c95cSJoel Becker 	/*
253b657c95cSJoel Becker 	 * These have all been checked by ocfs2_read_inode_block() or set
254b657c95cSJoel Becker 	 * by ocfs2_mknod_locked(), so a failure is a code bug.
255b657c95cSJoel Becker 	 */
256b657c95cSJoel Becker 	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));  /* This means that read_inode
257b657c95cSJoel Becker 						cannot create a superblock
258b657c95cSJoel Becker 						inode today.  change if
259b657c95cSJoel Becker 						that is needed. */
260b657c95cSJoel Becker 	BUG_ON(!(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)));
261b657c95cSJoel Becker 	BUG_ON(le32_to_cpu(fe->i_fs_generation) != osb->fs_generation);
262ccd979bdSMark Fasheh 
263ccd979bdSMark Fasheh 
2648110b073SMark Fasheh 	OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
2658110b073SMark Fasheh 	OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
26615b1e36bSMark Fasheh 	OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
2678110b073SMark Fasheh 
268ccd979bdSMark Fasheh 	inode->i_version = 1;
269ccd979bdSMark Fasheh 	inode->i_generation = le32_to_cpu(fe->i_generation);
270ccd979bdSMark Fasheh 	inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
271ccd979bdSMark Fasheh 	inode->i_mode = le16_to_cpu(fe->i_mode);
272ccd979bdSMark Fasheh 	inode->i_uid = le32_to_cpu(fe->i_uid);
273ccd979bdSMark Fasheh 	inode->i_gid = le32_to_cpu(fe->i_gid);
274ccd979bdSMark Fasheh 
275ccd979bdSMark Fasheh 	/* Fast symlinks will have i_size but no allocated clusters. */
276ccd979bdSMark Fasheh 	if (S_ISLNK(inode->i_mode) && !fe->i_clusters)
277ccd979bdSMark Fasheh 		inode->i_blocks = 0;
278ccd979bdSMark Fasheh 	else
2798110b073SMark Fasheh 		inode->i_blocks = ocfs2_inode_sector_count(inode);
280ccd979bdSMark Fasheh 	inode->i_mapping->a_ops = &ocfs2_aops;
281ccd979bdSMark Fasheh 	inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
282ccd979bdSMark Fasheh 	inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
283ccd979bdSMark Fasheh 	inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
284ccd979bdSMark Fasheh 	inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
285ccd979bdSMark Fasheh 	inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
286ccd979bdSMark Fasheh 	inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
287ccd979bdSMark Fasheh 
288ccd979bdSMark Fasheh 	if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno))
289ccd979bdSMark Fasheh 		mlog(ML_ERROR,
290b0697053SMark Fasheh 		     "ip_blkno %llu != i_blkno %llu!\n",
291b0697053SMark Fasheh 		     (unsigned long long)OCFS2_I(inode)->ip_blkno,
2921ca1a111SMark Fasheh 		     (unsigned long long)le64_to_cpu(fe->i_blkno));
293ccd979bdSMark Fasheh 
294198a1ca3SMark Fasheh 	inode->i_nlink = ocfs2_read_links_count(fe);
295ccd979bdSMark Fasheh 
296*64f3b269STao Ma 	trace_ocfs2_populate_inode(OCFS2_I(inode)->ip_blkno,
297*64f3b269STao Ma 				   le32_to_cpu(fe->i_flags));
298bbbd0eb3SJan Kara 	if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) {
29924c19ef4SMark Fasheh 		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
300bbbd0eb3SJan Kara 		inode->i_flags |= S_NOQUOTA;
301bbbd0eb3SJan Kara 	}
30224c19ef4SMark Fasheh 
303ccd979bdSMark Fasheh 	if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
304ccd979bdSMark Fasheh 		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
305ccd979bdSMark Fasheh 	} else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
306ccd979bdSMark Fasheh 		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
3071a224ad1SJan Kara 	} else if (fe->i_flags & cpu_to_le32(OCFS2_QUOTA_FL)) {
3081a224ad1SJan Kara 		inode->i_flags |= S_NOQUOTA;
309ccd979bdSMark Fasheh 	} else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
310ccd979bdSMark Fasheh 		/* we can't actually hit this as read_inode can't
311ccd979bdSMark Fasheh 		 * handle superblocks today ;-) */
312ccd979bdSMark Fasheh 		BUG();
313ccd979bdSMark Fasheh 	}
314ccd979bdSMark Fasheh 
315ccd979bdSMark Fasheh 	switch (inode->i_mode & S_IFMT) {
316ccd979bdSMark Fasheh 	    case S_IFREG:
31753da4939SMark Fasheh 		    if (use_plocks)
318ccd979bdSMark Fasheh 			    inode->i_fop = &ocfs2_fops;
31953da4939SMark Fasheh 		    else
32053da4939SMark Fasheh 			    inode->i_fop = &ocfs2_fops_no_plocks;
321ccd979bdSMark Fasheh 		    inode->i_op = &ocfs2_file_iops;
322ccd979bdSMark Fasheh 		    i_size_write(inode, le64_to_cpu(fe->i_size));
323ccd979bdSMark Fasheh 		    break;
324ccd979bdSMark Fasheh 	    case S_IFDIR:
325ccd979bdSMark Fasheh 		    inode->i_op = &ocfs2_dir_iops;
32653da4939SMark Fasheh 		    if (use_plocks)
327ccd979bdSMark Fasheh 			    inode->i_fop = &ocfs2_dops;
32853da4939SMark Fasheh 		    else
32953da4939SMark Fasheh 			    inode->i_fop = &ocfs2_dops_no_plocks;
330ccd979bdSMark Fasheh 		    i_size_write(inode, le64_to_cpu(fe->i_size));
3315e98d492SGoldwyn Rodrigues 		    OCFS2_I(inode)->ip_dir_lock_gen = 1;
332ccd979bdSMark Fasheh 		    break;
333ccd979bdSMark Fasheh 	    case S_IFLNK:
334ccd979bdSMark Fasheh 		    if (ocfs2_inode_is_fast_symlink(inode))
335ccd979bdSMark Fasheh 			inode->i_op = &ocfs2_fast_symlink_inode_operations;
336ccd979bdSMark Fasheh 		    else
337ccd979bdSMark Fasheh 			inode->i_op = &ocfs2_symlink_inode_operations;
338ccd979bdSMark Fasheh 		    i_size_write(inode, le64_to_cpu(fe->i_size));
339ccd979bdSMark Fasheh 		    break;
340ccd979bdSMark Fasheh 	    default:
341ccd979bdSMark Fasheh 		    inode->i_op = &ocfs2_special_file_iops;
342ccd979bdSMark Fasheh 		    init_special_inode(inode, inode->i_mode,
343ccd979bdSMark Fasheh 				       inode->i_rdev);
344ccd979bdSMark Fasheh 		    break;
345ccd979bdSMark Fasheh 	}
346ccd979bdSMark Fasheh 
34724c19ef4SMark Fasheh 	if (create_ino) {
34824c19ef4SMark Fasheh 		inode->i_ino = ino_from_blkno(inode->i_sb,
34924c19ef4SMark Fasheh 			       le64_to_cpu(fe->i_blkno));
35024c19ef4SMark Fasheh 
35124c19ef4SMark Fasheh 		/*
35224c19ef4SMark Fasheh 		 * If we ever want to create system files from kernel,
35324c19ef4SMark Fasheh 		 * the generation argument to
35424c19ef4SMark Fasheh 		 * ocfs2_inode_lock_res_init() will have to change.
35524c19ef4SMark Fasheh 		 */
3561ca1a111SMark Fasheh 		BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL);
35724c19ef4SMark Fasheh 
358e63aecb6SMark Fasheh 		ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
35924c19ef4SMark Fasheh 					  OCFS2_LOCK_TYPE_META, 0, inode);
36050008630STiger Yang 
36150008630STiger Yang 		ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
36250008630STiger Yang 					  OCFS2_LOCK_TYPE_OPEN, 0, inode);
36324c19ef4SMark Fasheh 	}
36424c19ef4SMark Fasheh 
36524c19ef4SMark Fasheh 	ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
36624c19ef4SMark Fasheh 				  OCFS2_LOCK_TYPE_RW, inode->i_generation,
36724c19ef4SMark Fasheh 				  inode);
36824c19ef4SMark Fasheh 
369ca4d147eSHerbert Poetzl 	ocfs2_set_inode_flags(inode);
370ca4d147eSHerbert Poetzl 
37113821151STao Ma 	OCFS2_I(inode)->ip_last_used_slot = 0;
37213821151STao Ma 	OCFS2_I(inode)->ip_last_used_group = 0;
373e3b4a97dSMark Fasheh 
374e3b4a97dSMark Fasheh 	if (S_ISDIR(inode->i_mode))
375e3b4a97dSMark Fasheh 		ocfs2_resv_set_type(&OCFS2_I(inode)->ip_la_data_resv,
376e3b4a97dSMark Fasheh 				    OCFS2_RESV_FLAG_DIR);
377ccd979bdSMark Fasheh }
378ccd979bdSMark Fasheh 
379ccd979bdSMark Fasheh static int ocfs2_read_locked_inode(struct inode *inode,
380ccd979bdSMark Fasheh 				   struct ocfs2_find_inode_args *args)
381ccd979bdSMark Fasheh {
382ccd979bdSMark Fasheh 	struct super_block *sb;
383ccd979bdSMark Fasheh 	struct ocfs2_super *osb;
384ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
385ccd979bdSMark Fasheh 	struct buffer_head *bh = NULL;
38624c19ef4SMark Fasheh 	int status, can_lock;
38724c19ef4SMark Fasheh 	u32 generation = 0;
388ccd979bdSMark Fasheh 
389ccd979bdSMark Fasheh 	status = -EINVAL;
390ccd979bdSMark Fasheh 	if (inode == NULL || inode->i_sb == NULL) {
391ccd979bdSMark Fasheh 		mlog(ML_ERROR, "bad inode\n");
39224c19ef4SMark Fasheh 		return status;
393ccd979bdSMark Fasheh 	}
394ccd979bdSMark Fasheh 	sb = inode->i_sb;
395ccd979bdSMark Fasheh 	osb = OCFS2_SB(sb);
396ccd979bdSMark Fasheh 
397ccd979bdSMark Fasheh 	if (!args) {
398ccd979bdSMark Fasheh 		mlog(ML_ERROR, "bad inode args\n");
399ccd979bdSMark Fasheh 		make_bad_inode(inode);
40024c19ef4SMark Fasheh 		return status;
401ccd979bdSMark Fasheh 	}
402ccd979bdSMark Fasheh 
40324c19ef4SMark Fasheh 	/*
40424c19ef4SMark Fasheh 	 * To improve performance of cold-cache inode stats, we take
40524c19ef4SMark Fasheh 	 * the cluster lock here if possible.
40624c19ef4SMark Fasheh 	 *
40724c19ef4SMark Fasheh 	 * Generally, OCFS2 never trusts the contents of an inode
40824c19ef4SMark Fasheh 	 * unless it's holding a cluster lock, so taking it here isn't
40924c19ef4SMark Fasheh 	 * a correctness issue as much as it is a performance
41024c19ef4SMark Fasheh 	 * improvement.
41124c19ef4SMark Fasheh 	 *
41224c19ef4SMark Fasheh 	 * There are three times when taking the lock is not a good idea:
41324c19ef4SMark Fasheh 	 *
41424c19ef4SMark Fasheh 	 * 1) During startup, before we have initialized the DLM.
41524c19ef4SMark Fasheh 	 *
41624c19ef4SMark Fasheh 	 * 2) If we are reading certain system files which never get
41724c19ef4SMark Fasheh 	 *    cluster locks (local alloc, truncate log).
41824c19ef4SMark Fasheh 	 *
41924c19ef4SMark Fasheh 	 * 3) If the process doing the iget() is responsible for
42024c19ef4SMark Fasheh 	 *    orphan dir recovery. We're holding the orphan dir lock and
42124c19ef4SMark Fasheh 	 *    can get into a deadlock with another process on another
42224c19ef4SMark Fasheh 	 *    node in ->delete_inode().
42324c19ef4SMark Fasheh 	 *
42424c19ef4SMark Fasheh 	 * #1 and #2 can be simply solved by never taking the lock
42524c19ef4SMark Fasheh 	 * here for system files (which are the only type we read
42624c19ef4SMark Fasheh 	 * during mount). It's a heavier approach, but our main
427b595076aSUwe Kleine-König 	 * concern is user-accessible files anyway.
42824c19ef4SMark Fasheh 	 *
42924c19ef4SMark Fasheh 	 * #3 works itself out because we'll eventually take the
43024c19ef4SMark Fasheh 	 * cluster lock before trusting anything anyway.
43124c19ef4SMark Fasheh 	 */
43224c19ef4SMark Fasheh 	can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
43350008630STiger Yang 		&& !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY)
434c271c5c2SSunil Mushran 		&& !ocfs2_mount_local(osb);
43524c19ef4SMark Fasheh 
436*64f3b269STao Ma 	trace_ocfs2_read_locked_inode(
437*64f3b269STao Ma 		(unsigned long long)OCFS2_I(inode)->ip_blkno, can_lock);
438*64f3b269STao Ma 
43924c19ef4SMark Fasheh 	/*
44024c19ef4SMark Fasheh 	 * To maintain backwards compatibility with older versions of
44124c19ef4SMark Fasheh 	 * ocfs2-tools, we still store the generation value for system
44224c19ef4SMark Fasheh 	 * files. The only ones that actually matter to userspace are
44324c19ef4SMark Fasheh 	 * the journals, but it's easier and inexpensive to just flag
44424c19ef4SMark Fasheh 	 * all system files similarly.
44524c19ef4SMark Fasheh 	 */
44624c19ef4SMark Fasheh 	if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
44724c19ef4SMark Fasheh 		generation = osb->fs_generation;
44824c19ef4SMark Fasheh 
449e63aecb6SMark Fasheh 	ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
45024c19ef4SMark Fasheh 				  OCFS2_LOCK_TYPE_META,
45124c19ef4SMark Fasheh 				  generation, inode);
45224c19ef4SMark Fasheh 
45350008630STiger Yang 	ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
45450008630STiger Yang 				  OCFS2_LOCK_TYPE_OPEN,
45550008630STiger Yang 				  0, inode);
45650008630STiger Yang 
45724c19ef4SMark Fasheh 	if (can_lock) {
45850008630STiger Yang 		status = ocfs2_open_lock(inode);
45950008630STiger Yang 		if (status) {
46050008630STiger Yang 			make_bad_inode(inode);
46150008630STiger Yang 			mlog_errno(status);
46250008630STiger Yang 			return status;
46350008630STiger Yang 		}
464e63aecb6SMark Fasheh 		status = ocfs2_inode_lock(inode, NULL, 0);
46524c19ef4SMark Fasheh 		if (status) {
46624c19ef4SMark Fasheh 			make_bad_inode(inode);
46724c19ef4SMark Fasheh 			mlog_errno(status);
46824c19ef4SMark Fasheh 			return status;
46924c19ef4SMark Fasheh 		}
47024c19ef4SMark Fasheh 	}
47124c19ef4SMark Fasheh 
47250008630STiger Yang 	if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) {
47350008630STiger Yang 		status = ocfs2_try_open_lock(inode, 0);
47450008630STiger Yang 		if (status) {
47550008630STiger Yang 			make_bad_inode(inode);
47650008630STiger Yang 			return status;
47750008630STiger Yang 		}
47850008630STiger Yang 	}
47950008630STiger Yang 
480b657c95cSJoel Becker 	if (can_lock) {
481b657c95cSJoel Becker 		status = ocfs2_read_inode_block_full(inode, &bh,
482d4a8c93cSJoel Becker 						     OCFS2_BH_IGNORE_CACHE);
483b657c95cSJoel Becker 	} else {
484da1e9098SJoel Becker 		status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh);
485f5ce5a08SSunil Mushran 		/*
486f5ce5a08SSunil Mushran 		 * If buffer is in jbd, then its checksum may not have been
487f5ce5a08SSunil Mushran 		 * computed as yet.
488f5ce5a08SSunil Mushran 		 */
489f5ce5a08SSunil Mushran 		if (!status && !buffer_jbd(bh))
490b657c95cSJoel Becker 			status = ocfs2_validate_inode_block(osb->sb, bh);
491b657c95cSJoel Becker 	}
492ccd979bdSMark Fasheh 	if (status < 0) {
493ccd979bdSMark Fasheh 		mlog_errno(status);
494ccd979bdSMark Fasheh 		goto bail;
495ccd979bdSMark Fasheh 	}
496ccd979bdSMark Fasheh 
49724c19ef4SMark Fasheh 	status = -EINVAL;
498ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) bh->b_data;
499ccd979bdSMark Fasheh 
50024c19ef4SMark Fasheh 	/*
50124c19ef4SMark Fasheh 	 * This is a code bug. Right now the caller needs to
50224c19ef4SMark Fasheh 	 * understand whether it is asking for a system file inode or
50324c19ef4SMark Fasheh 	 * not so the proper lock names can be built.
50424c19ef4SMark Fasheh 	 */
50524c19ef4SMark Fasheh 	mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) !=
50624c19ef4SMark Fasheh 			!!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE),
50724c19ef4SMark Fasheh 			"Inode %llu: system file state is ambigous\n",
50824c19ef4SMark Fasheh 			(unsigned long long)args->fi_blkno);
509ccd979bdSMark Fasheh 
510ccd979bdSMark Fasheh 	if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
511ccd979bdSMark Fasheh 	    S_ISBLK(le16_to_cpu(fe->i_mode)))
512ccd979bdSMark Fasheh 		inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
513ccd979bdSMark Fasheh 
514b657c95cSJoel Becker 	ocfs2_populate_inode(inode, fe, 0);
515ccd979bdSMark Fasheh 
516ccd979bdSMark Fasheh 	BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
517ccd979bdSMark Fasheh 
518ccd979bdSMark Fasheh 	status = 0;
519ccd979bdSMark Fasheh 
520ccd979bdSMark Fasheh bail:
52124c19ef4SMark Fasheh 	if (can_lock)
522e63aecb6SMark Fasheh 		ocfs2_inode_unlock(inode, 0);
52324c19ef4SMark Fasheh 
52424c19ef4SMark Fasheh 	if (status < 0)
52524c19ef4SMark Fasheh 		make_bad_inode(inode);
52624c19ef4SMark Fasheh 
527ccd979bdSMark Fasheh 	if (args && bh)
528ccd979bdSMark Fasheh 		brelse(bh);
529ccd979bdSMark Fasheh 
530ccd979bdSMark Fasheh 	return status;
531ccd979bdSMark Fasheh }
532ccd979bdSMark Fasheh 
533ccd979bdSMark Fasheh void ocfs2_sync_blockdev(struct super_block *sb)
534ccd979bdSMark Fasheh {
535ccd979bdSMark Fasheh 	sync_blockdev(sb->s_bdev);
536ccd979bdSMark Fasheh }
537ccd979bdSMark Fasheh 
538ccd979bdSMark Fasheh static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
539ccd979bdSMark Fasheh 				     struct inode *inode,
540ccd979bdSMark Fasheh 				     struct buffer_head *fe_bh)
541ccd979bdSMark Fasheh {
542ccd979bdSMark Fasheh 	int status = 0;
543ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
54460b11392SMark Fasheh 	handle_t *handle = NULL;
545ccd979bdSMark Fasheh 
546ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) fe_bh->b_data;
547ccd979bdSMark Fasheh 
5481afc32b9SMark Fasheh 	/*
5491afc32b9SMark Fasheh 	 * This check will also skip truncate of inodes with inline
5501afc32b9SMark Fasheh 	 * data and fast symlinks.
5511afc32b9SMark Fasheh 	 */
5523a0782d0SMark Fasheh 	if (fe->i_clusters) {
5532b4e30fbSJoel Becker 		if (ocfs2_should_order_data(inode))
5542b4e30fbSJoel Becker 			ocfs2_begin_ordered_truncate(inode, 0);
5552b4e30fbSJoel Becker 
55660b11392SMark Fasheh 		handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
55760b11392SMark Fasheh 		if (IS_ERR(handle)) {
55860b11392SMark Fasheh 			status = PTR_ERR(handle);
5590350cb07SDan Carpenter 			handle = NULL;
56060b11392SMark Fasheh 			mlog_errno(status);
56160b11392SMark Fasheh 			goto out;
56260b11392SMark Fasheh 		}
56360b11392SMark Fasheh 
5640cf2f763SJoel Becker 		status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
5650cf2f763SJoel Becker 						 fe_bh,
56660b11392SMark Fasheh 						 OCFS2_JOURNAL_ACCESS_WRITE);
56760b11392SMark Fasheh 		if (status < 0) {
56860b11392SMark Fasheh 			mlog_errno(status);
56960b11392SMark Fasheh 			goto out;
57060b11392SMark Fasheh 		}
57160b11392SMark Fasheh 
57260b11392SMark Fasheh 		i_size_write(inode, 0);
57360b11392SMark Fasheh 
57460b11392SMark Fasheh 		status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
57560b11392SMark Fasheh 		if (status < 0) {
57660b11392SMark Fasheh 			mlog_errno(status);
57760b11392SMark Fasheh 			goto out;
57860b11392SMark Fasheh 		}
57960b11392SMark Fasheh 
58060b11392SMark Fasheh 		ocfs2_commit_trans(osb, handle);
58160b11392SMark Fasheh 		handle = NULL;
58260b11392SMark Fasheh 
58378f94673STristan Ye 		status = ocfs2_commit_truncate(osb, inode, fe_bh);
584ccd979bdSMark Fasheh 		if (status < 0) {
585ccd979bdSMark Fasheh 			mlog_errno(status);
5863a0782d0SMark Fasheh 			goto out;
587ccd979bdSMark Fasheh 		}
5883a0782d0SMark Fasheh 	}
589ccd979bdSMark Fasheh 
59060b11392SMark Fasheh out:
59160b11392SMark Fasheh 	if (handle)
59260b11392SMark Fasheh 		ocfs2_commit_trans(osb, handle);
593ccd979bdSMark Fasheh 	return status;
594ccd979bdSMark Fasheh }
595ccd979bdSMark Fasheh 
596ccd979bdSMark Fasheh static int ocfs2_remove_inode(struct inode *inode,
597ccd979bdSMark Fasheh 			      struct buffer_head *di_bh,
598ccd979bdSMark Fasheh 			      struct inode *orphan_dir_inode,
599ccd979bdSMark Fasheh 			      struct buffer_head *orphan_dir_bh)
600ccd979bdSMark Fasheh {
601ccd979bdSMark Fasheh 	int status;
602ccd979bdSMark Fasheh 	struct inode *inode_alloc_inode = NULL;
603ccd979bdSMark Fasheh 	struct buffer_head *inode_alloc_bh = NULL;
6041fabe148SMark Fasheh 	handle_t *handle;
605ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
606ccd979bdSMark Fasheh 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
607ccd979bdSMark Fasheh 
608ccd979bdSMark Fasheh 	inode_alloc_inode =
609ccd979bdSMark Fasheh 		ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
610ccd979bdSMark Fasheh 					    le16_to_cpu(di->i_suballoc_slot));
611ccd979bdSMark Fasheh 	if (!inode_alloc_inode) {
612ccd979bdSMark Fasheh 		status = -EEXIST;
613ccd979bdSMark Fasheh 		mlog_errno(status);
614ccd979bdSMark Fasheh 		goto bail;
615ccd979bdSMark Fasheh 	}
616ccd979bdSMark Fasheh 
6171b1dcc1bSJes Sorensen 	mutex_lock(&inode_alloc_inode->i_mutex);
618e63aecb6SMark Fasheh 	status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1);
619ccd979bdSMark Fasheh 	if (status < 0) {
6201b1dcc1bSJes Sorensen 		mutex_unlock(&inode_alloc_inode->i_mutex);
621ccd979bdSMark Fasheh 
622ccd979bdSMark Fasheh 		mlog_errno(status);
623ccd979bdSMark Fasheh 		goto bail;
624ccd979bdSMark Fasheh 	}
625ccd979bdSMark Fasheh 
626a90714c1SJan Kara 	handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS +
627a90714c1SJan Kara 				   ocfs2_quota_trans_credits(inode->i_sb));
628ccd979bdSMark Fasheh 	if (IS_ERR(handle)) {
629ccd979bdSMark Fasheh 		status = PTR_ERR(handle);
630ccd979bdSMark Fasheh 		mlog_errno(status);
631ccd979bdSMark Fasheh 		goto bail_unlock;
632ccd979bdSMark Fasheh 	}
633ccd979bdSMark Fasheh 
634d4cd1871SLi Dongyang 	if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
635ccd979bdSMark Fasheh 		status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
636ccd979bdSMark Fasheh 					  orphan_dir_bh);
637ccd979bdSMark Fasheh 		if (status < 0) {
638ccd979bdSMark Fasheh 			mlog_errno(status);
639ccd979bdSMark Fasheh 			goto bail_commit;
640ccd979bdSMark Fasheh 		}
641d4cd1871SLi Dongyang 	}
642ccd979bdSMark Fasheh 
643ccd979bdSMark Fasheh 	/* set the inodes dtime */
6440cf2f763SJoel Becker 	status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
645ccd979bdSMark Fasheh 					 OCFS2_JOURNAL_ACCESS_WRITE);
646ccd979bdSMark Fasheh 	if (status < 0) {
647ccd979bdSMark Fasheh 		mlog_errno(status);
648ccd979bdSMark Fasheh 		goto bail_commit;
649ccd979bdSMark Fasheh 	}
650ccd979bdSMark Fasheh 
651ccd979bdSMark Fasheh 	di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec);
6524092d49fSMarcin Slusarz 	di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
653ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, di_bh);
654ccd979bdSMark Fasheh 
6558cb471e8SJoel Becker 	ocfs2_remove_from_cache(INODE_CACHE(inode), di_bh);
65663936ddaSChristoph Hellwig 	dquot_free_inode(inode);
657ccd979bdSMark Fasheh 
658ccd979bdSMark Fasheh 	status = ocfs2_free_dinode(handle, inode_alloc_inode,
659ccd979bdSMark Fasheh 				   inode_alloc_bh, di);
660ccd979bdSMark Fasheh 	if (status < 0)
661ccd979bdSMark Fasheh 		mlog_errno(status);
662ccd979bdSMark Fasheh 
663ccd979bdSMark Fasheh bail_commit:
66402dc1af4SMark Fasheh 	ocfs2_commit_trans(osb, handle);
665ccd979bdSMark Fasheh bail_unlock:
666e63aecb6SMark Fasheh 	ocfs2_inode_unlock(inode_alloc_inode, 1);
6671b1dcc1bSJes Sorensen 	mutex_unlock(&inode_alloc_inode->i_mutex);
668ccd979bdSMark Fasheh 	brelse(inode_alloc_bh);
669ccd979bdSMark Fasheh bail:
670ccd979bdSMark Fasheh 	iput(inode_alloc_inode);
671ccd979bdSMark Fasheh 
672ccd979bdSMark Fasheh 	return status;
673ccd979bdSMark Fasheh }
674ccd979bdSMark Fasheh 
675b4df6ed8SMark Fasheh /*
676b4df6ed8SMark Fasheh  * Serialize with orphan dir recovery. If the process doing
677b4df6ed8SMark Fasheh  * recovery on this orphan dir does an iget() with the dir
678b4df6ed8SMark Fasheh  * i_mutex held, we'll deadlock here. Instead we detect this
679b4df6ed8SMark Fasheh  * and exit early - recovery will wipe this inode for us.
680b4df6ed8SMark Fasheh  */
681b4df6ed8SMark Fasheh static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
682b4df6ed8SMark Fasheh 					     int slot)
683b4df6ed8SMark Fasheh {
684b4df6ed8SMark Fasheh 	int ret = 0;
685b4df6ed8SMark Fasheh 
686b4df6ed8SMark Fasheh 	spin_lock(&osb->osb_lock);
687b4df6ed8SMark Fasheh 	if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) {
688b4df6ed8SMark Fasheh 		ret = -EDEADLK;
689b4df6ed8SMark Fasheh 		goto out;
690b4df6ed8SMark Fasheh 	}
691b4df6ed8SMark Fasheh 	/* This signals to the orphan recovery process that it should
692b4df6ed8SMark Fasheh 	 * wait for us to handle the wipe. */
693b4df6ed8SMark Fasheh 	osb->osb_orphan_wipes[slot]++;
694b4df6ed8SMark Fasheh out:
695b4df6ed8SMark Fasheh 	spin_unlock(&osb->osb_lock);
696*64f3b269STao Ma 	trace_ocfs2_check_orphan_recovery_state(slot, ret);
697b4df6ed8SMark Fasheh 	return ret;
698b4df6ed8SMark Fasheh }
699b4df6ed8SMark Fasheh 
700b4df6ed8SMark Fasheh static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
701b4df6ed8SMark Fasheh 					 int slot)
702b4df6ed8SMark Fasheh {
703b4df6ed8SMark Fasheh 	spin_lock(&osb->osb_lock);
704b4df6ed8SMark Fasheh 	osb->osb_orphan_wipes[slot]--;
705b4df6ed8SMark Fasheh 	spin_unlock(&osb->osb_lock);
706b4df6ed8SMark Fasheh 
707b4df6ed8SMark Fasheh 	wake_up(&osb->osb_wipe_event);
708b4df6ed8SMark Fasheh }
709b4df6ed8SMark Fasheh 
710ccd979bdSMark Fasheh static int ocfs2_wipe_inode(struct inode *inode,
711ccd979bdSMark Fasheh 			    struct buffer_head *di_bh)
712ccd979bdSMark Fasheh {
713d577632eSJoel Becker 	int status, orphaned_slot = -1;
714ccd979bdSMark Fasheh 	struct inode *orphan_dir_inode = NULL;
715ccd979bdSMark Fasheh 	struct buffer_head *orphan_dir_bh = NULL;
716ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
717d4cd1871SLi Dongyang 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
718ccd979bdSMark Fasheh 
719d4cd1871SLi Dongyang 	if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
72050008630STiger Yang 		orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
721b4df6ed8SMark Fasheh 
722b4df6ed8SMark Fasheh 		status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
723b4df6ed8SMark Fasheh 		if (status)
724b4df6ed8SMark Fasheh 			return status;
725b4df6ed8SMark Fasheh 
726ccd979bdSMark Fasheh 		orphan_dir_inode = ocfs2_get_system_file_inode(osb,
727ccd979bdSMark Fasheh 							       ORPHAN_DIR_SYSTEM_INODE,
728ccd979bdSMark Fasheh 							       orphaned_slot);
729ccd979bdSMark Fasheh 		if (!orphan_dir_inode) {
730ccd979bdSMark Fasheh 			status = -EEXIST;
731ccd979bdSMark Fasheh 			mlog_errno(status);
732ccd979bdSMark Fasheh 			goto bail;
733ccd979bdSMark Fasheh 		}
734ccd979bdSMark Fasheh 
735ccd979bdSMark Fasheh 		/* Lock the orphan dir. The lock will be held for the entire
736ccd979bdSMark Fasheh 		 * delete_inode operation. We do this now to avoid races with
737ccd979bdSMark Fasheh 		 * recovery completion on other nodes. */
7381b1dcc1bSJes Sorensen 		mutex_lock(&orphan_dir_inode->i_mutex);
739e63aecb6SMark Fasheh 		status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
740ccd979bdSMark Fasheh 		if (status < 0) {
7411b1dcc1bSJes Sorensen 			mutex_unlock(&orphan_dir_inode->i_mutex);
742ccd979bdSMark Fasheh 
743ccd979bdSMark Fasheh 			mlog_errno(status);
744ccd979bdSMark Fasheh 			goto bail;
745ccd979bdSMark Fasheh 		}
746d4cd1871SLi Dongyang 	}
747ccd979bdSMark Fasheh 
748ccd979bdSMark Fasheh 	/* we do this while holding the orphan dir lock because we
74934d024f8SMark Fasheh 	 * don't want recovery being run from another node to try an
75034d024f8SMark Fasheh 	 * inode delete underneath us -- this will result in two nodes
751ccd979bdSMark Fasheh 	 * truncating the same file! */
752ccd979bdSMark Fasheh 	status = ocfs2_truncate_for_delete(osb, inode, di_bh);
753ccd979bdSMark Fasheh 	if (status < 0) {
754ccd979bdSMark Fasheh 		mlog_errno(status);
755ccd979bdSMark Fasheh 		goto bail_unlock_dir;
756ccd979bdSMark Fasheh 	}
757ccd979bdSMark Fasheh 
7589b7895efSMark Fasheh 	/* Remove any dir index tree */
7599b7895efSMark Fasheh 	if (S_ISDIR(inode->i_mode)) {
7609b7895efSMark Fasheh 		status = ocfs2_dx_dir_truncate(inode, di_bh);
7619b7895efSMark Fasheh 		if (status) {
7629b7895efSMark Fasheh 			mlog_errno(status);
7639b7895efSMark Fasheh 			goto bail_unlock_dir;
7649b7895efSMark Fasheh 		}
7659b7895efSMark Fasheh 	}
7669b7895efSMark Fasheh 
767cf1d6c76STiger Yang 	/*Free extended attribute resources associated with this inode.*/
768cf1d6c76STiger Yang 	status = ocfs2_xattr_remove(inode, di_bh);
769cf1d6c76STiger Yang 	if (status < 0) {
770cf1d6c76STiger Yang 		mlog_errno(status);
771cf1d6c76STiger Yang 		goto bail_unlock_dir;
772cf1d6c76STiger Yang 	}
773cf1d6c76STiger Yang 
7748b2c0dbaSTao Ma 	status = ocfs2_remove_refcount_tree(inode, di_bh);
7758b2c0dbaSTao Ma 	if (status < 0) {
7768b2c0dbaSTao Ma 		mlog_errno(status);
7778b2c0dbaSTao Ma 		goto bail_unlock_dir;
7788b2c0dbaSTao Ma 	}
7798b2c0dbaSTao Ma 
780ccd979bdSMark Fasheh 	status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
781ccd979bdSMark Fasheh 				    orphan_dir_bh);
782ccd979bdSMark Fasheh 	if (status < 0)
783ccd979bdSMark Fasheh 		mlog_errno(status);
784ccd979bdSMark Fasheh 
785ccd979bdSMark Fasheh bail_unlock_dir:
786d4cd1871SLi Dongyang 	if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)
787d4cd1871SLi Dongyang 		return status;
788d4cd1871SLi Dongyang 
789e63aecb6SMark Fasheh 	ocfs2_inode_unlock(orphan_dir_inode, 1);
7901b1dcc1bSJes Sorensen 	mutex_unlock(&orphan_dir_inode->i_mutex);
791ccd979bdSMark Fasheh 	brelse(orphan_dir_bh);
792ccd979bdSMark Fasheh bail:
793ccd979bdSMark Fasheh 	iput(orphan_dir_inode);
794b4df6ed8SMark Fasheh 	ocfs2_signal_wipe_completion(osb, orphaned_slot);
795ccd979bdSMark Fasheh 
796ccd979bdSMark Fasheh 	return status;
797ccd979bdSMark Fasheh }
798ccd979bdSMark Fasheh 
799ccd979bdSMark Fasheh /* There is a series of simple checks that should be done before a
80034d024f8SMark Fasheh  * trylock is even considered. Encapsulate those in this function. */
801ccd979bdSMark Fasheh static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
802ccd979bdSMark Fasheh {
803ccd979bdSMark Fasheh 	int ret = 0;
804ccd979bdSMark Fasheh 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
805ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
806ccd979bdSMark Fasheh 
807*64f3b269STao Ma 	trace_ocfs2_inode_is_valid_to_delete(current, osb->dc_task,
808*64f3b269STao Ma 					     (unsigned long long)oi->ip_blkno,
809*64f3b269STao Ma 					     oi->ip_flags);
810*64f3b269STao Ma 
811ccd979bdSMark Fasheh 	/* We shouldn't be getting here for the root directory
812ccd979bdSMark Fasheh 	 * inode.. */
813ccd979bdSMark Fasheh 	if (inode == osb->root_inode) {
814ccd979bdSMark Fasheh 		mlog(ML_ERROR, "Skipping delete of root inode.\n");
815ccd979bdSMark Fasheh 		goto bail;
816ccd979bdSMark Fasheh 	}
817ccd979bdSMark Fasheh 
81834d024f8SMark Fasheh 	/* If we're coming from downconvert_thread we can't go into our own
819ccd979bdSMark Fasheh 	 * voting [hello, deadlock city!], so unforuntately we just
820ccd979bdSMark Fasheh 	 * have to skip deleting this guy. That's OK though because
821ccd979bdSMark Fasheh 	 * the node who's doing the actual deleting should handle it
822ccd979bdSMark Fasheh 	 * anyway. */
823*64f3b269STao Ma 	if (current == osb->dc_task)
824ccd979bdSMark Fasheh 		goto bail;
825ccd979bdSMark Fasheh 
826ccd979bdSMark Fasheh 	spin_lock(&oi->ip_lock);
827ccd979bdSMark Fasheh 	/* OCFS2 *never* deletes system files. This should technically
828ccd979bdSMark Fasheh 	 * never get here as system file inodes should always have a
829ccd979bdSMark Fasheh 	 * positive link count. */
830ccd979bdSMark Fasheh 	if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
831b0697053SMark Fasheh 		mlog(ML_ERROR, "Skipping delete of system file %llu\n",
832b0697053SMark Fasheh 		     (unsigned long long)oi->ip_blkno);
833ccd979bdSMark Fasheh 		goto bail_unlock;
834ccd979bdSMark Fasheh 	}
835ccd979bdSMark Fasheh 
83634d024f8SMark Fasheh 	/* If we have allowd wipe of this inode for another node, it
83734d024f8SMark Fasheh 	 * will be marked here so we can safely skip it. Recovery will
83834d024f8SMark Fasheh 	 * cleanup any inodes we might inadvertantly skip here. */
839*64f3b269STao Ma 	if (oi->ip_flags & OCFS2_INODE_SKIP_DELETE)
840ccd979bdSMark Fasheh 		goto bail_unlock;
841ccd979bdSMark Fasheh 
842ccd979bdSMark Fasheh 	ret = 1;
843ccd979bdSMark Fasheh bail_unlock:
844ccd979bdSMark Fasheh 	spin_unlock(&oi->ip_lock);
845ccd979bdSMark Fasheh bail:
846ccd979bdSMark Fasheh 	return ret;
847ccd979bdSMark Fasheh }
848ccd979bdSMark Fasheh 
849ccd979bdSMark Fasheh /* Query the cluster to determine whether we should wipe an inode from
850ccd979bdSMark Fasheh  * disk or not.
851ccd979bdSMark Fasheh  *
852ccd979bdSMark Fasheh  * Requires the inode to have the cluster lock. */
853ccd979bdSMark Fasheh static int ocfs2_query_inode_wipe(struct inode *inode,
854ccd979bdSMark Fasheh 				  struct buffer_head *di_bh,
855ccd979bdSMark Fasheh 				  int *wipe)
856ccd979bdSMark Fasheh {
857*64f3b269STao Ma 	int status = 0, reason = 0;
858ccd979bdSMark Fasheh 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
859ccd979bdSMark Fasheh 	struct ocfs2_dinode *di;
860ccd979bdSMark Fasheh 
861ccd979bdSMark Fasheh 	*wipe = 0;
862ccd979bdSMark Fasheh 
863*64f3b269STao Ma 	trace_ocfs2_query_inode_wipe_begin((unsigned long long)oi->ip_blkno,
864*64f3b269STao Ma 					   inode->i_nlink);
865*64f3b269STao Ma 
866ccd979bdSMark Fasheh 	/* While we were waiting for the cluster lock in
867ccd979bdSMark Fasheh 	 * ocfs2_delete_inode, another node might have asked to delete
868ccd979bdSMark Fasheh 	 * the inode. Recheck our flags to catch this. */
869ccd979bdSMark Fasheh 	if (!ocfs2_inode_is_valid_to_delete(inode)) {
870*64f3b269STao Ma 		reason = 1;
871ccd979bdSMark Fasheh 		goto bail;
872ccd979bdSMark Fasheh 	}
873ccd979bdSMark Fasheh 
874ccd979bdSMark Fasheh 	/* Now that we have an up to date inode, we can double check
875ccd979bdSMark Fasheh 	 * the link count. */
876*64f3b269STao Ma 	if (inode->i_nlink)
877ccd979bdSMark Fasheh 		goto bail;
878ccd979bdSMark Fasheh 
879ccd979bdSMark Fasheh 	/* Do some basic inode verification... */
880ccd979bdSMark Fasheh 	di = (struct ocfs2_dinode *) di_bh->b_data;
881d4cd1871SLi Dongyang 	if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL)) &&
882d4cd1871SLi Dongyang 	    !(oi->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
883b54c2ca4STristan Ye 		/*
884b54c2ca4STristan Ye 		 * Inodes in the orphan dir must have ORPHANED_FL.  The only
885b54c2ca4STristan Ye 		 * inodes that come back out of the orphan dir are reflink
886b54c2ca4STristan Ye 		 * targets. A reflink target may be moved out of the orphan
887b54c2ca4STristan Ye 		 * dir between the time we scan the directory and the time we
888b54c2ca4STristan Ye 		 * process it. This would lead to HAS_REFCOUNT_FL being set but
889b54c2ca4STristan Ye 		 * ORPHANED_FL not.
890b54c2ca4STristan Ye 		 */
891b54c2ca4STristan Ye 		if (di->i_dyn_features & cpu_to_le16(OCFS2_HAS_REFCOUNT_FL)) {
892*64f3b269STao Ma 			reason = 2;
893b54c2ca4STristan Ye 			goto bail;
894b54c2ca4STristan Ye 		}
895b54c2ca4STristan Ye 
896ccd979bdSMark Fasheh 		/* for lack of a better error? */
897ccd979bdSMark Fasheh 		status = -EEXIST;
898ccd979bdSMark Fasheh 		mlog(ML_ERROR,
899b0697053SMark Fasheh 		     "Inode %llu (on-disk %llu) not orphaned! "
900ccd979bdSMark Fasheh 		     "Disk flags  0x%x, inode flags 0x%x\n",
901b0697053SMark Fasheh 		     (unsigned long long)oi->ip_blkno,
9021ca1a111SMark Fasheh 		     (unsigned long long)le64_to_cpu(di->i_blkno),
9031ca1a111SMark Fasheh 		     le32_to_cpu(di->i_flags), oi->ip_flags);
904ccd979bdSMark Fasheh 		goto bail;
905ccd979bdSMark Fasheh 	}
906ccd979bdSMark Fasheh 
907ccd979bdSMark Fasheh 	/* has someone already deleted us?! baaad... */
908ccd979bdSMark Fasheh 	if (di->i_dtime) {
909ccd979bdSMark Fasheh 		status = -EEXIST;
910ccd979bdSMark Fasheh 		mlog_errno(status);
911ccd979bdSMark Fasheh 		goto bail;
912ccd979bdSMark Fasheh 	}
913ccd979bdSMark Fasheh 
9146f16bf65SMark Fasheh 	/*
9156f16bf65SMark Fasheh 	 * This is how ocfs2 determines whether an inode is still live
9166f16bf65SMark Fasheh 	 * within the cluster. Every node takes a shared read lock on
9176f16bf65SMark Fasheh 	 * the inode open lock in ocfs2_read_locked_inode(). When we
9186f16bf65SMark Fasheh 	 * get to ->delete_inode(), each node tries to convert it's
9196f16bf65SMark Fasheh 	 * lock to an exclusive. Trylocks are serialized by the inode
9206f16bf65SMark Fasheh 	 * meta data lock. If the upconvert suceeds, we know the inode
9216f16bf65SMark Fasheh 	 * is no longer live and can be deleted.
9226f16bf65SMark Fasheh 	 *
9236f16bf65SMark Fasheh 	 * Though we call this with the meta data lock held, the
9246f16bf65SMark Fasheh 	 * trylock keeps us from ABBA deadlock.
9256f16bf65SMark Fasheh 	 */
9266f16bf65SMark Fasheh 	status = ocfs2_try_open_lock(inode, 1);
92750008630STiger Yang 	if (status == -EAGAIN) {
928ccd979bdSMark Fasheh 		status = 0;
929*64f3b269STao Ma 		reason = 3;
930ccd979bdSMark Fasheh 		goto bail;
931ccd979bdSMark Fasheh 	}
932ccd979bdSMark Fasheh 	if (status < 0) {
933ccd979bdSMark Fasheh 		mlog_errno(status);
934ccd979bdSMark Fasheh 		goto bail;
935ccd979bdSMark Fasheh 	}
936ccd979bdSMark Fasheh 
937ccd979bdSMark Fasheh 	*wipe = 1;
938*64f3b269STao Ma 	trace_ocfs2_query_inode_wipe_succ(le16_to_cpu(di->i_orphaned_slot));
939ccd979bdSMark Fasheh 
940ccd979bdSMark Fasheh bail:
941*64f3b269STao Ma 	trace_ocfs2_query_inode_wipe_end(status, reason);
942ccd979bdSMark Fasheh 	return status;
943ccd979bdSMark Fasheh }
944ccd979bdSMark Fasheh 
945ccd979bdSMark Fasheh /* Support function for ocfs2_delete_inode. Will help us keep the
946ccd979bdSMark Fasheh  * inode data in a consistent state for clear_inode. Always truncates
947ccd979bdSMark Fasheh  * pages, optionally sync's them first. */
948ccd979bdSMark Fasheh static void ocfs2_cleanup_delete_inode(struct inode *inode,
949ccd979bdSMark Fasheh 				       int sync_data)
950ccd979bdSMark Fasheh {
951*64f3b269STao Ma 	trace_ocfs2_cleanup_delete_inode(
952b0697053SMark Fasheh 		(unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
953ccd979bdSMark Fasheh 	if (sync_data)
954ccd979bdSMark Fasheh 		write_inode_now(inode, 1);
955ccd979bdSMark Fasheh 	truncate_inode_pages(&inode->i_data, 0);
956ccd979bdSMark Fasheh }
957ccd979bdSMark Fasheh 
958066d92dcSAl Viro static void ocfs2_delete_inode(struct inode *inode)
959ccd979bdSMark Fasheh {
960ccd979bdSMark Fasheh 	int wipe, status;
961e4b963f1SJoel Becker 	sigset_t oldset;
962ccd979bdSMark Fasheh 	struct buffer_head *di_bh = NULL;
963ccd979bdSMark Fasheh 
964*64f3b269STao Ma 	trace_ocfs2_delete_inode(inode->i_ino,
965*64f3b269STao Ma 				 (unsigned long long)OCFS2_I(inode)->ip_blkno,
966*64f3b269STao Ma 				 is_bad_inode(inode));
967ccd979bdSMark Fasheh 
968a90714c1SJan Kara 	/* When we fail in read_inode() we mark inode as bad. The second test
969a90714c1SJan Kara 	 * catches the case when inode allocation fails before allocating
970a90714c1SJan Kara 	 * a block for inode. */
971*64f3b269STao Ma 	if (is_bad_inode(inode) || !OCFS2_I(inode)->ip_blkno)
972ccd979bdSMark Fasheh 		goto bail;
973ccd979bdSMark Fasheh 
974871a2931SChristoph Hellwig 	dquot_initialize(inode);
975907f4554SChristoph Hellwig 
976ccd979bdSMark Fasheh 	if (!ocfs2_inode_is_valid_to_delete(inode)) {
977ccd979bdSMark Fasheh 		/* It's probably not necessary to truncate_inode_pages
978ccd979bdSMark Fasheh 		 * here but we do it for safety anyway (it will most
979ccd979bdSMark Fasheh 		 * likely be a no-op anyway) */
980ccd979bdSMark Fasheh 		ocfs2_cleanup_delete_inode(inode, 0);
981ccd979bdSMark Fasheh 		goto bail;
982ccd979bdSMark Fasheh 	}
983ccd979bdSMark Fasheh 
984ccd979bdSMark Fasheh 	/* We want to block signals in delete_inode as the lock and
985ccd979bdSMark Fasheh 	 * messaging paths may return us -ERESTARTSYS. Which would
986ccd979bdSMark Fasheh 	 * cause us to exit early, resulting in inodes being orphaned
987ccd979bdSMark Fasheh 	 * forever. */
988e4b963f1SJoel Becker 	ocfs2_block_signals(&oldset);
989ccd979bdSMark Fasheh 
9906ca497a8Swengang wang 	/*
9916ca497a8Swengang wang 	 * Synchronize us against ocfs2_get_dentry. We take this in
9926ca497a8Swengang wang 	 * shared mode so that all nodes can still concurrently
9936ca497a8Swengang wang 	 * process deletes.
9946ca497a8Swengang wang 	 */
9956ca497a8Swengang wang 	status = ocfs2_nfs_sync_lock(OCFS2_SB(inode->i_sb), 0);
9966ca497a8Swengang wang 	if (status < 0) {
9976ca497a8Swengang wang 		mlog(ML_ERROR, "getting nfs sync lock(PR) failed %d\n", status);
9986ca497a8Swengang wang 		ocfs2_cleanup_delete_inode(inode, 0);
9996ca497a8Swengang wang 		goto bail_unblock;
10006ca497a8Swengang wang 	}
1001ccd979bdSMark Fasheh 	/* Lock down the inode. This gives us an up to date view of
1002ccd979bdSMark Fasheh 	 * it's metadata (for verification), and allows us to
100334d024f8SMark Fasheh 	 * serialize delete_inode on multiple nodes.
1004ccd979bdSMark Fasheh 	 *
1005ccd979bdSMark Fasheh 	 * Even though we might be doing a truncate, we don't take the
1006ccd979bdSMark Fasheh 	 * allocation lock here as it won't be needed - nobody will
1007ccd979bdSMark Fasheh 	 * have the file open.
1008ccd979bdSMark Fasheh 	 */
1009e63aecb6SMark Fasheh 	status = ocfs2_inode_lock(inode, &di_bh, 1);
1010ccd979bdSMark Fasheh 	if (status < 0) {
1011ccd979bdSMark Fasheh 		if (status != -ENOENT)
1012ccd979bdSMark Fasheh 			mlog_errno(status);
1013ccd979bdSMark Fasheh 		ocfs2_cleanup_delete_inode(inode, 0);
10146ca497a8Swengang wang 		goto bail_unlock_nfs_sync;
1015ccd979bdSMark Fasheh 	}
1016ccd979bdSMark Fasheh 
1017ccd979bdSMark Fasheh 	/* Query the cluster. This will be the final decision made
1018ccd979bdSMark Fasheh 	 * before we go ahead and wipe the inode. */
1019ccd979bdSMark Fasheh 	status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
1020ccd979bdSMark Fasheh 	if (!wipe || status < 0) {
102134d024f8SMark Fasheh 		/* Error and remote inode busy both mean we won't be
1022ccd979bdSMark Fasheh 		 * removing the inode, so they take almost the same
1023ccd979bdSMark Fasheh 		 * path. */
1024ccd979bdSMark Fasheh 		if (status < 0)
1025ccd979bdSMark Fasheh 			mlog_errno(status);
1026ccd979bdSMark Fasheh 
102734d024f8SMark Fasheh 		/* Someone in the cluster has disallowed a wipe of
102834d024f8SMark Fasheh 		 * this inode, or it was never completely
102934d024f8SMark Fasheh 		 * orphaned. Write out the pages and exit now. */
1030ccd979bdSMark Fasheh 		ocfs2_cleanup_delete_inode(inode, 1);
1031ccd979bdSMark Fasheh 		goto bail_unlock_inode;
1032ccd979bdSMark Fasheh 	}
1033ccd979bdSMark Fasheh 
1034ccd979bdSMark Fasheh 	ocfs2_cleanup_delete_inode(inode, 0);
1035ccd979bdSMark Fasheh 
1036ccd979bdSMark Fasheh 	status = ocfs2_wipe_inode(inode, di_bh);
1037ccd979bdSMark Fasheh 	if (status < 0) {
1038b4df6ed8SMark Fasheh 		if (status != -EDEADLK)
1039ccd979bdSMark Fasheh 			mlog_errno(status);
1040ccd979bdSMark Fasheh 		goto bail_unlock_inode;
1041ccd979bdSMark Fasheh 	}
1042ccd979bdSMark Fasheh 
104324c19ef4SMark Fasheh 	/*
104424c19ef4SMark Fasheh 	 * Mark the inode as successfully deleted.
104524c19ef4SMark Fasheh 	 *
104624c19ef4SMark Fasheh 	 * This is important for ocfs2_clear_inode() as it will check
104724c19ef4SMark Fasheh 	 * this flag and skip any checkpointing work
104824c19ef4SMark Fasheh 	 *
104924c19ef4SMark Fasheh 	 * ocfs2_stuff_meta_lvb() also uses this flag to invalidate
105024c19ef4SMark Fasheh 	 * the LVB for other nodes.
105124c19ef4SMark Fasheh 	 */
1052ccd979bdSMark Fasheh 	OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
1053ccd979bdSMark Fasheh 
1054ccd979bdSMark Fasheh bail_unlock_inode:
1055e63aecb6SMark Fasheh 	ocfs2_inode_unlock(inode, 1);
1056ccd979bdSMark Fasheh 	brelse(di_bh);
10576ca497a8Swengang wang 
10586ca497a8Swengang wang bail_unlock_nfs_sync:
10596ca497a8Swengang wang 	ocfs2_nfs_sync_unlock(OCFS2_SB(inode->i_sb), 0);
10606ca497a8Swengang wang 
1061ccd979bdSMark Fasheh bail_unblock:
1062e4b963f1SJoel Becker 	ocfs2_unblock_signals(&oldset);
1063ccd979bdSMark Fasheh bail:
1064c1e8d35eSTao Ma 	return;
1065ccd979bdSMark Fasheh }
1066ccd979bdSMark Fasheh 
1067066d92dcSAl Viro static void ocfs2_clear_inode(struct inode *inode)
1068ccd979bdSMark Fasheh {
1069ccd979bdSMark Fasheh 	int status;
1070ccd979bdSMark Fasheh 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1071ccd979bdSMark Fasheh 
1072066d92dcSAl Viro 	end_writeback(inode);
1073*64f3b269STao Ma 	trace_ocfs2_clear_inode((unsigned long long)oi->ip_blkno,
1074*64f3b269STao Ma 				inode->i_nlink);
1075ccd979bdSMark Fasheh 
1076ccd979bdSMark Fasheh 	mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
1077ccd979bdSMark Fasheh 			"Inode=%lu\n", inode->i_ino);
1078ccd979bdSMark Fasheh 
10799f754758SChristoph Hellwig 	dquot_drop(inode);
1080257ba15cSChristoph Hellwig 
108134d024f8SMark Fasheh 	/* To preven remote deletes we hold open lock before, now it
108234d024f8SMark Fasheh 	 * is time to unlock PR and EX open locks. */
108350008630STiger Yang 	ocfs2_open_unlock(inode);
108450008630STiger Yang 
1085ccd979bdSMark Fasheh 	/* Do these before all the other work so that we don't bounce
108634d024f8SMark Fasheh 	 * the downconvert thread while waiting to destroy the locks. */
1087ccd979bdSMark Fasheh 	ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres);
1088e63aecb6SMark Fasheh 	ocfs2_mark_lockres_freeing(&oi->ip_inode_lockres);
108950008630STiger Yang 	ocfs2_mark_lockres_freeing(&oi->ip_open_lockres);
1090ccd979bdSMark Fasheh 
10914fe370afSMark Fasheh 	ocfs2_resv_discard(&OCFS2_SB(inode->i_sb)->osb_la_resmap,
10924fe370afSMark Fasheh 			   &oi->ip_la_data_resv);
10934fe370afSMark Fasheh 	ocfs2_resv_init_once(&oi->ip_la_data_resv);
10944fe370afSMark Fasheh 
1095ccd979bdSMark Fasheh 	/* We very well may get a clear_inode before all an inodes
1096ccd979bdSMark Fasheh 	 * metadata has hit disk. Of course, we can't drop any cluster
1097ccd979bdSMark Fasheh 	 * locks until the journal has finished with it. The only
1098ccd979bdSMark Fasheh 	 * exception here are successfully wiped inodes - their
1099ccd979bdSMark Fasheh 	 * metadata can now be considered to be part of the system
1100ccd979bdSMark Fasheh 	 * inodes from which it came. */
1101ccd979bdSMark Fasheh 	if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED))
1102ccd979bdSMark Fasheh 		ocfs2_checkpoint_inode(inode);
1103ccd979bdSMark Fasheh 
1104ccd979bdSMark Fasheh 	mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
1105b0697053SMark Fasheh 			"Clear inode of %llu, inode has io markers\n",
1106b0697053SMark Fasheh 			(unsigned long long)oi->ip_blkno);
1107ccd979bdSMark Fasheh 
110883418978SMark Fasheh 	ocfs2_extent_map_trunc(inode, 0);
110983418978SMark Fasheh 
1110ccd979bdSMark Fasheh 	status = ocfs2_drop_inode_locks(inode);
1111ccd979bdSMark Fasheh 	if (status < 0)
1112ccd979bdSMark Fasheh 		mlog_errno(status);
1113ccd979bdSMark Fasheh 
1114ccd979bdSMark Fasheh 	ocfs2_lock_res_free(&oi->ip_rw_lockres);
1115e63aecb6SMark Fasheh 	ocfs2_lock_res_free(&oi->ip_inode_lockres);
111650008630STiger Yang 	ocfs2_lock_res_free(&oi->ip_open_lockres);
1117ccd979bdSMark Fasheh 
111866fb345dSJoel Becker 	ocfs2_metadata_cache_exit(INODE_CACHE(inode));
1119ccd979bdSMark Fasheh 
11208cb471e8SJoel Becker 	mlog_bug_on_msg(INODE_CACHE(inode)->ci_num_cached,
1121b0697053SMark Fasheh 			"Clear inode of %llu, inode has %u cache items\n",
11228cb471e8SJoel Becker 			(unsigned long long)oi->ip_blkno,
11238cb471e8SJoel Becker 			INODE_CACHE(inode)->ci_num_cached);
1124ccd979bdSMark Fasheh 
11258cb471e8SJoel Becker 	mlog_bug_on_msg(!(INODE_CACHE(inode)->ci_flags & OCFS2_CACHE_FL_INLINE),
1126b0697053SMark Fasheh 			"Clear inode of %llu, inode has a bad flag\n",
1127b0697053SMark Fasheh 			(unsigned long long)oi->ip_blkno);
1128ccd979bdSMark Fasheh 
1129ccd979bdSMark Fasheh 	mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
1130b0697053SMark Fasheh 			"Clear inode of %llu, inode is locked\n",
1131b0697053SMark Fasheh 			(unsigned long long)oi->ip_blkno);
1132ccd979bdSMark Fasheh 
1133251b6eccSMark Fasheh 	mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
1134b0697053SMark Fasheh 			"Clear inode of %llu, io_mutex is locked\n",
1135b0697053SMark Fasheh 			(unsigned long long)oi->ip_blkno);
1136251b6eccSMark Fasheh 	mutex_unlock(&oi->ip_io_mutex);
1137ccd979bdSMark Fasheh 
1138ccd979bdSMark Fasheh 	/*
1139ccd979bdSMark Fasheh 	 * down_trylock() returns 0, down_write_trylock() returns 1
1140ccd979bdSMark Fasheh 	 * kernel 1, world 0
1141ccd979bdSMark Fasheh 	 */
1142ccd979bdSMark Fasheh 	mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
1143b0697053SMark Fasheh 			"Clear inode of %llu, alloc_sem is locked\n",
1144b0697053SMark Fasheh 			(unsigned long long)oi->ip_blkno);
1145ccd979bdSMark Fasheh 	up_write(&oi->ip_alloc_sem);
1146ccd979bdSMark Fasheh 
1147ccd979bdSMark Fasheh 	mlog_bug_on_msg(oi->ip_open_count,
1148b0697053SMark Fasheh 			"Clear inode of %llu has open count %d\n",
1149b0697053SMark Fasheh 			(unsigned long long)oi->ip_blkno, oi->ip_open_count);
1150ccd979bdSMark Fasheh 
1151ccd979bdSMark Fasheh 	/* Clear all other flags. */
115247460d65SJoel Becker 	oi->ip_flags = 0;
1153ccd979bdSMark Fasheh 	oi->ip_dir_start_lookup = 0;
1154ccd979bdSMark Fasheh 	oi->ip_blkno = 0ULL;
1155ae0dff68SSunil Mushran 
1156ae0dff68SSunil Mushran 	/*
1157ae0dff68SSunil Mushran 	 * ip_jinode is used to track txns against this inode. We ensure that
1158ae0dff68SSunil Mushran 	 * the journal is flushed before journal shutdown. Thus it is safe to
1159ae0dff68SSunil Mushran 	 * have inodes get cleaned up after journal shutdown.
1160ae0dff68SSunil Mushran 	 */
11612b4e30fbSJoel Becker 	jbd2_journal_release_jbd_inode(OCFS2_SB(inode->i_sb)->journal->j_journal,
11622b4e30fbSJoel Becker 				       &oi->ip_jinode);
1163ccd979bdSMark Fasheh }
1164ccd979bdSMark Fasheh 
1165066d92dcSAl Viro void ocfs2_evict_inode(struct inode *inode)
1166066d92dcSAl Viro {
1167066d92dcSAl Viro 	if (!inode->i_nlink ||
1168066d92dcSAl Viro 	    (OCFS2_I(inode)->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)) {
1169066d92dcSAl Viro 		ocfs2_delete_inode(inode);
1170066d92dcSAl Viro 	} else {
1171066d92dcSAl Viro 		truncate_inode_pages(&inode->i_data, 0);
1172066d92dcSAl Viro 	}
1173066d92dcSAl Viro 	ocfs2_clear_inode(inode);
1174066d92dcSAl Viro }
1175066d92dcSAl Viro 
1176ccd979bdSMark Fasheh /* Called under inode_lock, with no more references on the
1177ccd979bdSMark Fasheh  * struct inode, so it's safe here to check the flags field
1178ccd979bdSMark Fasheh  * and to manipulate i_nlink without any other locks. */
117945321ac5SAl Viro int ocfs2_drop_inode(struct inode *inode)
1180ccd979bdSMark Fasheh {
1181ccd979bdSMark Fasheh 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
118245321ac5SAl Viro 	int res;
1183ccd979bdSMark Fasheh 
1184*64f3b269STao Ma 	trace_ocfs2_drop_inode((unsigned long long)oi->ip_blkno,
1185*64f3b269STao Ma 				inode->i_nlink, oi->ip_flags);
1186ccd979bdSMark Fasheh 
1187379dfe9dSMark Fasheh 	if (oi->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)
118845321ac5SAl Viro 		res = 1;
1189379dfe9dSMark Fasheh 	else
119045321ac5SAl Viro 		res = generic_drop_inode(inode);
1191ccd979bdSMark Fasheh 
119245321ac5SAl Viro 	return res;
1193ccd979bdSMark Fasheh }
1194ccd979bdSMark Fasheh 
1195ccd979bdSMark Fasheh /*
1196ccd979bdSMark Fasheh  * This is called from our getattr.
1197ccd979bdSMark Fasheh  */
1198ccd979bdSMark Fasheh int ocfs2_inode_revalidate(struct dentry *dentry)
1199ccd979bdSMark Fasheh {
1200ccd979bdSMark Fasheh 	struct inode *inode = dentry->d_inode;
1201ccd979bdSMark Fasheh 	int status = 0;
1202ccd979bdSMark Fasheh 
1203*64f3b269STao Ma 	trace_ocfs2_inode_revalidate(inode,
1204*64f3b269STao Ma 		inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL,
1205*64f3b269STao Ma 		inode ? (unsigned long long)OCFS2_I(inode)->ip_flags : 0);
1206ccd979bdSMark Fasheh 
1207ccd979bdSMark Fasheh 	if (!inode) {
1208ccd979bdSMark Fasheh 		status = -ENOENT;
1209ccd979bdSMark Fasheh 		goto bail;
1210ccd979bdSMark Fasheh 	}
1211ccd979bdSMark Fasheh 
1212ccd979bdSMark Fasheh 	spin_lock(&OCFS2_I(inode)->ip_lock);
1213ccd979bdSMark Fasheh 	if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
1214ccd979bdSMark Fasheh 		spin_unlock(&OCFS2_I(inode)->ip_lock);
1215ccd979bdSMark Fasheh 		status = -ENOENT;
1216ccd979bdSMark Fasheh 		goto bail;
1217ccd979bdSMark Fasheh 	}
1218ccd979bdSMark Fasheh 	spin_unlock(&OCFS2_I(inode)->ip_lock);
1219ccd979bdSMark Fasheh 
1220e63aecb6SMark Fasheh 	/* Let ocfs2_inode_lock do the work of updating our struct
1221ccd979bdSMark Fasheh 	 * inode for us. */
1222e63aecb6SMark Fasheh 	status = ocfs2_inode_lock(inode, NULL, 0);
1223ccd979bdSMark Fasheh 	if (status < 0) {
1224ccd979bdSMark Fasheh 		if (status != -ENOENT)
1225ccd979bdSMark Fasheh 			mlog_errno(status);
1226ccd979bdSMark Fasheh 		goto bail;
1227ccd979bdSMark Fasheh 	}
1228e63aecb6SMark Fasheh 	ocfs2_inode_unlock(inode, 0);
1229ccd979bdSMark Fasheh bail:
1230ccd979bdSMark Fasheh 	return status;
1231ccd979bdSMark Fasheh }
1232ccd979bdSMark Fasheh 
1233ccd979bdSMark Fasheh /*
1234ccd979bdSMark Fasheh  * Updates a disk inode from a
1235ccd979bdSMark Fasheh  * struct inode.
1236ccd979bdSMark Fasheh  * Only takes ip_lock.
1237ccd979bdSMark Fasheh  */
12381fabe148SMark Fasheh int ocfs2_mark_inode_dirty(handle_t *handle,
1239ccd979bdSMark Fasheh 			   struct inode *inode,
1240ccd979bdSMark Fasheh 			   struct buffer_head *bh)
1241ccd979bdSMark Fasheh {
1242ccd979bdSMark Fasheh 	int status;
1243ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
1244ccd979bdSMark Fasheh 
1245*64f3b269STao Ma 	trace_ocfs2_mark_inode_dirty((unsigned long long)OCFS2_I(inode)->ip_blkno);
1246ccd979bdSMark Fasheh 
12470cf2f763SJoel Becker 	status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
1248ccd979bdSMark Fasheh 					 OCFS2_JOURNAL_ACCESS_WRITE);
1249ccd979bdSMark Fasheh 	if (status < 0) {
1250ccd979bdSMark Fasheh 		mlog_errno(status);
1251ccd979bdSMark Fasheh 		goto leave;
1252ccd979bdSMark Fasheh 	}
1253ccd979bdSMark Fasheh 
1254ccd979bdSMark Fasheh 	spin_lock(&OCFS2_I(inode)->ip_lock);
1255ccd979bdSMark Fasheh 	fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
12566e4b0d56SJan Kara 	ocfs2_get_inode_flags(OCFS2_I(inode));
1257ca4d147eSHerbert Poetzl 	fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
125815b1e36bSMark Fasheh 	fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
1259ccd979bdSMark Fasheh 	spin_unlock(&OCFS2_I(inode)->ip_lock);
1260ccd979bdSMark Fasheh 
1261ccd979bdSMark Fasheh 	fe->i_size = cpu_to_le64(i_size_read(inode));
1262198a1ca3SMark Fasheh 	ocfs2_set_links_count(fe, inode->i_nlink);
1263ccd979bdSMark Fasheh 	fe->i_uid = cpu_to_le32(inode->i_uid);
1264ccd979bdSMark Fasheh 	fe->i_gid = cpu_to_le32(inode->i_gid);
1265ccd979bdSMark Fasheh 	fe->i_mode = cpu_to_le16(inode->i_mode);
1266ccd979bdSMark Fasheh 	fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
1267ccd979bdSMark Fasheh 	fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
1268ccd979bdSMark Fasheh 	fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1269ccd979bdSMark Fasheh 	fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1270ccd979bdSMark Fasheh 	fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
1271ccd979bdSMark Fasheh 	fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
1272ccd979bdSMark Fasheh 
1273ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, bh);
1274ccd979bdSMark Fasheh leave:
1275ccd979bdSMark Fasheh 	return status;
1276ccd979bdSMark Fasheh }
1277ccd979bdSMark Fasheh 
1278ccd979bdSMark Fasheh /*
1279ccd979bdSMark Fasheh  *
1280ccd979bdSMark Fasheh  * Updates a struct inode from a disk inode.
1281ccd979bdSMark Fasheh  * does no i/o, only takes ip_lock.
1282ccd979bdSMark Fasheh  */
1283ccd979bdSMark Fasheh void ocfs2_refresh_inode(struct inode *inode,
1284ccd979bdSMark Fasheh 			 struct ocfs2_dinode *fe)
1285ccd979bdSMark Fasheh {
1286ccd979bdSMark Fasheh 	spin_lock(&OCFS2_I(inode)->ip_lock);
1287ccd979bdSMark Fasheh 
1288ccd979bdSMark Fasheh 	OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1289ca4d147eSHerbert Poetzl 	OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
129015b1e36bSMark Fasheh 	OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
1291ca4d147eSHerbert Poetzl 	ocfs2_set_inode_flags(inode);
1292ccd979bdSMark Fasheh 	i_size_write(inode, le64_to_cpu(fe->i_size));
1293198a1ca3SMark Fasheh 	inode->i_nlink = ocfs2_read_links_count(fe);
1294ccd979bdSMark Fasheh 	inode->i_uid = le32_to_cpu(fe->i_uid);
1295ccd979bdSMark Fasheh 	inode->i_gid = le32_to_cpu(fe->i_gid);
1296ccd979bdSMark Fasheh 	inode->i_mode = le16_to_cpu(fe->i_mode);
1297ccd979bdSMark Fasheh 	if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
1298ccd979bdSMark Fasheh 		inode->i_blocks = 0;
1299ccd979bdSMark Fasheh 	else
13008110b073SMark Fasheh 		inode->i_blocks = ocfs2_inode_sector_count(inode);
1301ccd979bdSMark Fasheh 	inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
1302ccd979bdSMark Fasheh 	inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
1303ccd979bdSMark Fasheh 	inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
1304ccd979bdSMark Fasheh 	inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
1305ccd979bdSMark Fasheh 	inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
1306ccd979bdSMark Fasheh 	inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
1307ccd979bdSMark Fasheh 
1308ccd979bdSMark Fasheh 	spin_unlock(&OCFS2_I(inode)->ip_lock);
1309ccd979bdSMark Fasheh }
1310b657c95cSJoel Becker 
1311b657c95cSJoel Becker int ocfs2_validate_inode_block(struct super_block *sb,
1312b657c95cSJoel Becker 			       struct buffer_head *bh)
1313b657c95cSJoel Becker {
1314d6b32bbbSJoel Becker 	int rc;
1315b657c95cSJoel Becker 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1316b657c95cSJoel Becker 
1317*64f3b269STao Ma 	trace_ocfs2_validate_inode_block((unsigned long long)bh->b_blocknr);
1318970e4936SJoel Becker 
1319b657c95cSJoel Becker 	BUG_ON(!buffer_uptodate(bh));
1320b657c95cSJoel Becker 
1321d6b32bbbSJoel Becker 	/*
1322d6b32bbbSJoel Becker 	 * If the ecc fails, we return the error but otherwise
1323d6b32bbbSJoel Becker 	 * leave the filesystem running.  We know any error is
1324d6b32bbbSJoel Becker 	 * local to this block.
1325d6b32bbbSJoel Becker 	 */
1326d6b32bbbSJoel Becker 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check);
132713723d00SJoel Becker 	if (rc) {
132813723d00SJoel Becker 		mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
132913723d00SJoel Becker 		     (unsigned long long)bh->b_blocknr);
1330d6b32bbbSJoel Becker 		goto bail;
133113723d00SJoel Becker 	}
1332d6b32bbbSJoel Becker 
1333d6b32bbbSJoel Becker 	/*
1334d6b32bbbSJoel Becker 	 * Errors after here are fatal.
1335d6b32bbbSJoel Becker 	 */
1336d6b32bbbSJoel Becker 
1337d6b32bbbSJoel Becker 	rc = -EINVAL;
1338d6b32bbbSJoel Becker 
1339b657c95cSJoel Becker 	if (!OCFS2_IS_VALID_DINODE(di)) {
1340b657c95cSJoel Becker 		ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s\n",
1341b657c95cSJoel Becker 			    (unsigned long long)bh->b_blocknr, 7,
1342b657c95cSJoel Becker 			    di->i_signature);
1343b657c95cSJoel Becker 		goto bail;
1344b657c95cSJoel Becker 	}
1345b657c95cSJoel Becker 
1346b657c95cSJoel Becker 	if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
1347b657c95cSJoel Becker 		ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n",
1348b657c95cSJoel Becker 			    (unsigned long long)bh->b_blocknr,
1349b657c95cSJoel Becker 			    (unsigned long long)le64_to_cpu(di->i_blkno));
1350b657c95cSJoel Becker 		goto bail;
1351b657c95cSJoel Becker 	}
1352b657c95cSJoel Becker 
1353b657c95cSJoel Becker 	if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
1354b657c95cSJoel Becker 		ocfs2_error(sb,
1355b657c95cSJoel Becker 			    "Invalid dinode #%llu: OCFS2_VALID_FL not set\n",
1356b657c95cSJoel Becker 			    (unsigned long long)bh->b_blocknr);
1357b657c95cSJoel Becker 		goto bail;
1358b657c95cSJoel Becker 	}
1359b657c95cSJoel Becker 
1360b657c95cSJoel Becker 	if (le32_to_cpu(di->i_fs_generation) !=
1361b657c95cSJoel Becker 	    OCFS2_SB(sb)->fs_generation) {
1362b657c95cSJoel Becker 		ocfs2_error(sb,
1363b657c95cSJoel Becker 			    "Invalid dinode #%llu: fs_generation is %u\n",
1364b657c95cSJoel Becker 			    (unsigned long long)bh->b_blocknr,
1365b657c95cSJoel Becker 			    le32_to_cpu(di->i_fs_generation));
1366b657c95cSJoel Becker 		goto bail;
1367b657c95cSJoel Becker 	}
1368b657c95cSJoel Becker 
1369b657c95cSJoel Becker 	rc = 0;
1370b657c95cSJoel Becker 
1371b657c95cSJoel Becker bail:
1372b657c95cSJoel Becker 	return rc;
1373b657c95cSJoel Becker }
1374b657c95cSJoel Becker 
1375b657c95cSJoel Becker int ocfs2_read_inode_block_full(struct inode *inode, struct buffer_head **bh,
1376b657c95cSJoel Becker 				int flags)
1377b657c95cSJoel Becker {
1378b657c95cSJoel Becker 	int rc;
1379b657c95cSJoel Becker 	struct buffer_head *tmp = *bh;
1380b657c95cSJoel Becker 
13818cb471e8SJoel Becker 	rc = ocfs2_read_blocks(INODE_CACHE(inode), OCFS2_I(inode)->ip_blkno,
13828cb471e8SJoel Becker 			       1, &tmp, flags, ocfs2_validate_inode_block);
1383b657c95cSJoel Becker 
1384b657c95cSJoel Becker 	/* If ocfs2_read_blocks() got us a new bh, pass it up. */
1385970e4936SJoel Becker 	if (!rc && !*bh)
1386b657c95cSJoel Becker 		*bh = tmp;
1387b657c95cSJoel Becker 
1388b657c95cSJoel Becker 	return rc;
1389b657c95cSJoel Becker }
1390b657c95cSJoel Becker 
1391b657c95cSJoel Becker int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh)
1392b657c95cSJoel Becker {
1393b657c95cSJoel Becker 	return ocfs2_read_inode_block_full(inode, bh, 0);
1394b657c95cSJoel Becker }
13956e5a3d75SJoel Becker 
13966e5a3d75SJoel Becker 
13976e5a3d75SJoel Becker static u64 ocfs2_inode_cache_owner(struct ocfs2_caching_info *ci)
13986e5a3d75SJoel Becker {
13996e5a3d75SJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
14006e5a3d75SJoel Becker 
14016e5a3d75SJoel Becker 	return oi->ip_blkno;
14026e5a3d75SJoel Becker }
14036e5a3d75SJoel Becker 
14048cb471e8SJoel Becker static struct super_block *ocfs2_inode_cache_get_super(struct ocfs2_caching_info *ci)
14058cb471e8SJoel Becker {
14068cb471e8SJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
14078cb471e8SJoel Becker 
14088cb471e8SJoel Becker 	return oi->vfs_inode.i_sb;
14098cb471e8SJoel Becker }
14108cb471e8SJoel Becker 
14116e5a3d75SJoel Becker static void ocfs2_inode_cache_lock(struct ocfs2_caching_info *ci)
14126e5a3d75SJoel Becker {
14136e5a3d75SJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
14146e5a3d75SJoel Becker 
14156e5a3d75SJoel Becker 	spin_lock(&oi->ip_lock);
14166e5a3d75SJoel Becker }
14176e5a3d75SJoel Becker 
14186e5a3d75SJoel Becker static void ocfs2_inode_cache_unlock(struct ocfs2_caching_info *ci)
14196e5a3d75SJoel Becker {
14206e5a3d75SJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
14216e5a3d75SJoel Becker 
14226e5a3d75SJoel Becker 	spin_unlock(&oi->ip_lock);
14236e5a3d75SJoel Becker }
14246e5a3d75SJoel Becker 
14256e5a3d75SJoel Becker static void ocfs2_inode_cache_io_lock(struct ocfs2_caching_info *ci)
14266e5a3d75SJoel Becker {
14276e5a3d75SJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
14286e5a3d75SJoel Becker 
14296e5a3d75SJoel Becker 	mutex_lock(&oi->ip_io_mutex);
14306e5a3d75SJoel Becker }
14316e5a3d75SJoel Becker 
14326e5a3d75SJoel Becker static void ocfs2_inode_cache_io_unlock(struct ocfs2_caching_info *ci)
14336e5a3d75SJoel Becker {
14346e5a3d75SJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
14356e5a3d75SJoel Becker 
14366e5a3d75SJoel Becker 	mutex_unlock(&oi->ip_io_mutex);
14376e5a3d75SJoel Becker }
14386e5a3d75SJoel Becker 
14396e5a3d75SJoel Becker const struct ocfs2_caching_operations ocfs2_inode_caching_ops = {
14406e5a3d75SJoel Becker 	.co_owner		= ocfs2_inode_cache_owner,
14418cb471e8SJoel Becker 	.co_get_super		= ocfs2_inode_cache_get_super,
14426e5a3d75SJoel Becker 	.co_cache_lock		= ocfs2_inode_cache_lock,
14436e5a3d75SJoel Becker 	.co_cache_unlock	= ocfs2_inode_cache_unlock,
14446e5a3d75SJoel Becker 	.co_io_lock		= ocfs2_inode_cache_io_lock,
14456e5a3d75SJoel Becker 	.co_io_unlock		= ocfs2_inode_cache_io_unlock,
14466e5a3d75SJoel Becker };
14476e5a3d75SJoel Becker 
1448