xref: /openbmc/linux/fs/ocfs2/inode.c (revision 6218b90e7669d83cb8ad744f3400205d260597f2)
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 
134ef6b689bSTao Ma 	mlog(0, "(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 == NULL) {
156ccd979bdSMark Fasheh 		inode = ERR_PTR(-ENOMEM);
157ccd979bdSMark Fasheh 		mlog_errno(PTR_ERR(inode));
158ccd979bdSMark Fasheh 		goto bail;
159ccd979bdSMark Fasheh 	}
160*6218b90eSTao Ma 	if (inode->i_state & I_NEW) {
161*6218b90eSTao Ma 		mlog(0, "Inode was not in inode cache, reading it.\n");
162*6218b90eSTao Ma 		ocfs2_read_locked_inode(inode, &args);
163*6218b90eSTao Ma 		unlock_new_inode(inode);
164*6218b90eSTao Ma 	}
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);
175c1e8d35eSTao Ma 		mlog(0, "inode %p\n", 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 
195ef6b689bSTao Ma 	mlog(0, "(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 	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 
220ef6b689bSTao Ma 	mlog(0, "inode = %p, opaque = %p\n", inode, opaque);
221ccd979bdSMark Fasheh 
222ccd979bdSMark Fasheh 	inode->i_ino = args->fi_ino;
223ccd979bdSMark Fasheh 	OCFS2_I(inode)->ip_blkno = args->fi_blkno;
2245fa0613eSJan Kara 	if (args->fi_sysfile_type != 0)
2255fa0613eSJan Kara 		lockdep_set_class(&inode->i_mutex,
2265fa0613eSJan Kara 			&ocfs2_sysfile_lock_key[args->fi_sysfile_type]);
227cb25797dSJan Kara 	if (args->fi_sysfile_type == USER_QUOTA_SYSTEM_INODE ||
228cb25797dSJan Kara 	    args->fi_sysfile_type == GROUP_QUOTA_SYSTEM_INODE ||
229cb25797dSJan Kara 	    args->fi_sysfile_type == LOCAL_USER_QUOTA_SYSTEM_INODE ||
230cb25797dSJan Kara 	    args->fi_sysfile_type == LOCAL_GROUP_QUOTA_SYSTEM_INODE)
231cb25797dSJan Kara 		lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem,
232cb25797dSJan Kara 				  &ocfs2_quota_ip_alloc_sem_key);
233cb25797dSJan Kara 	else
234cb25797dSJan Kara 		lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem,
235cb25797dSJan Kara 				  &ocfs2_file_ip_alloc_sem_key);
236ccd979bdSMark Fasheh 
237ccd979bdSMark Fasheh 	return 0;
238ccd979bdSMark Fasheh }
239ccd979bdSMark Fasheh 
240b657c95cSJoel Becker void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
241ccd979bdSMark Fasheh 			  int create_ino)
242ccd979bdSMark Fasheh {
243ccd979bdSMark Fasheh 	struct super_block *sb;
244ccd979bdSMark Fasheh 	struct ocfs2_super *osb;
24553da4939SMark Fasheh 	int use_plocks = 1;
246ccd979bdSMark Fasheh 
247ef6b689bSTao Ma 	mlog(0, "(0x%p, size:%llu)\n", inode,
2481ca1a111SMark Fasheh 	     (unsigned long long)le64_to_cpu(fe->i_size));
249ccd979bdSMark Fasheh 
250ccd979bdSMark Fasheh 	sb = inode->i_sb;
251ccd979bdSMark Fasheh 	osb = OCFS2_SB(sb);
252ccd979bdSMark Fasheh 
25353da4939SMark Fasheh 	if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
25453da4939SMark Fasheh 	    ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks())
25553da4939SMark Fasheh 		use_plocks = 0;
25653da4939SMark Fasheh 
257b657c95cSJoel Becker 	/*
258b657c95cSJoel Becker 	 * These have all been checked by ocfs2_read_inode_block() or set
259b657c95cSJoel Becker 	 * by ocfs2_mknod_locked(), so a failure is a code bug.
260b657c95cSJoel Becker 	 */
261b657c95cSJoel Becker 	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));  /* This means that read_inode
262b657c95cSJoel Becker 						cannot create a superblock
263b657c95cSJoel Becker 						inode today.  change if
264b657c95cSJoel Becker 						that is needed. */
265b657c95cSJoel Becker 	BUG_ON(!(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)));
266b657c95cSJoel Becker 	BUG_ON(le32_to_cpu(fe->i_fs_generation) != osb->fs_generation);
267ccd979bdSMark Fasheh 
268ccd979bdSMark Fasheh 
2698110b073SMark Fasheh 	OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
2708110b073SMark Fasheh 	OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
27115b1e36bSMark Fasheh 	OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
2728110b073SMark Fasheh 
273ccd979bdSMark Fasheh 	inode->i_version = 1;
274ccd979bdSMark Fasheh 	inode->i_generation = le32_to_cpu(fe->i_generation);
275ccd979bdSMark Fasheh 	inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
276ccd979bdSMark Fasheh 	inode->i_mode = le16_to_cpu(fe->i_mode);
277ccd979bdSMark Fasheh 	inode->i_uid = le32_to_cpu(fe->i_uid);
278ccd979bdSMark Fasheh 	inode->i_gid = le32_to_cpu(fe->i_gid);
279ccd979bdSMark Fasheh 
280ccd979bdSMark Fasheh 	/* Fast symlinks will have i_size but no allocated clusters. */
281ccd979bdSMark Fasheh 	if (S_ISLNK(inode->i_mode) && !fe->i_clusters)
282ccd979bdSMark Fasheh 		inode->i_blocks = 0;
283ccd979bdSMark Fasheh 	else
2848110b073SMark Fasheh 		inode->i_blocks = ocfs2_inode_sector_count(inode);
285ccd979bdSMark Fasheh 	inode->i_mapping->a_ops = &ocfs2_aops;
286ccd979bdSMark Fasheh 	inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
287ccd979bdSMark Fasheh 	inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
288ccd979bdSMark Fasheh 	inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
289ccd979bdSMark Fasheh 	inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
290ccd979bdSMark Fasheh 	inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
291ccd979bdSMark Fasheh 	inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
292ccd979bdSMark Fasheh 
293ccd979bdSMark Fasheh 	if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno))
294ccd979bdSMark Fasheh 		mlog(ML_ERROR,
295b0697053SMark Fasheh 		     "ip_blkno %llu != i_blkno %llu!\n",
296b0697053SMark Fasheh 		     (unsigned long long)OCFS2_I(inode)->ip_blkno,
2971ca1a111SMark Fasheh 		     (unsigned long long)le64_to_cpu(fe->i_blkno));
298ccd979bdSMark Fasheh 
299198a1ca3SMark Fasheh 	inode->i_nlink = ocfs2_read_links_count(fe);
300ccd979bdSMark Fasheh 
301bbbd0eb3SJan Kara 	if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) {
30224c19ef4SMark Fasheh 		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
303bbbd0eb3SJan Kara 		inode->i_flags |= S_NOQUOTA;
304bbbd0eb3SJan Kara 	}
30524c19ef4SMark Fasheh 
306ccd979bdSMark Fasheh 	if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
307ccd979bdSMark Fasheh 		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
308ccd979bdSMark Fasheh 		mlog(0, "local alloc inode: i_ino=%lu\n", inode->i_ino);
309ccd979bdSMark Fasheh 	} else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
310ccd979bdSMark Fasheh 		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
3111a224ad1SJan Kara 	} else if (fe->i_flags & cpu_to_le32(OCFS2_QUOTA_FL)) {
3121a224ad1SJan Kara 		inode->i_flags |= S_NOQUOTA;
313ccd979bdSMark Fasheh 	} else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
314ccd979bdSMark Fasheh 		mlog(0, "superblock inode: i_ino=%lu\n", inode->i_ino);
315ccd979bdSMark Fasheh 		/* we can't actually hit this as read_inode can't
316ccd979bdSMark Fasheh 		 * handle superblocks today ;-) */
317ccd979bdSMark Fasheh 		BUG();
318ccd979bdSMark Fasheh 	}
319ccd979bdSMark Fasheh 
320ccd979bdSMark Fasheh 	switch (inode->i_mode & S_IFMT) {
321ccd979bdSMark Fasheh 	    case S_IFREG:
32253da4939SMark Fasheh 		    if (use_plocks)
323ccd979bdSMark Fasheh 			    inode->i_fop = &ocfs2_fops;
32453da4939SMark Fasheh 		    else
32553da4939SMark Fasheh 			    inode->i_fop = &ocfs2_fops_no_plocks;
326ccd979bdSMark Fasheh 		    inode->i_op = &ocfs2_file_iops;
327ccd979bdSMark Fasheh 		    i_size_write(inode, le64_to_cpu(fe->i_size));
328ccd979bdSMark Fasheh 		    break;
329ccd979bdSMark Fasheh 	    case S_IFDIR:
330ccd979bdSMark Fasheh 		    inode->i_op = &ocfs2_dir_iops;
33153da4939SMark Fasheh 		    if (use_plocks)
332ccd979bdSMark Fasheh 			    inode->i_fop = &ocfs2_dops;
33353da4939SMark Fasheh 		    else
33453da4939SMark Fasheh 			    inode->i_fop = &ocfs2_dops_no_plocks;
335ccd979bdSMark Fasheh 		    i_size_write(inode, le64_to_cpu(fe->i_size));
3365e98d492SGoldwyn Rodrigues 		    OCFS2_I(inode)->ip_dir_lock_gen = 1;
337ccd979bdSMark Fasheh 		    break;
338ccd979bdSMark Fasheh 	    case S_IFLNK:
339ccd979bdSMark Fasheh 		    if (ocfs2_inode_is_fast_symlink(inode))
340ccd979bdSMark Fasheh 			inode->i_op = &ocfs2_fast_symlink_inode_operations;
341ccd979bdSMark Fasheh 		    else
342ccd979bdSMark Fasheh 			inode->i_op = &ocfs2_symlink_inode_operations;
343ccd979bdSMark Fasheh 		    i_size_write(inode, le64_to_cpu(fe->i_size));
344ccd979bdSMark Fasheh 		    break;
345ccd979bdSMark Fasheh 	    default:
346ccd979bdSMark Fasheh 		    inode->i_op = &ocfs2_special_file_iops;
347ccd979bdSMark Fasheh 		    init_special_inode(inode, inode->i_mode,
348ccd979bdSMark Fasheh 				       inode->i_rdev);
349ccd979bdSMark Fasheh 		    break;
350ccd979bdSMark Fasheh 	}
351ccd979bdSMark Fasheh 
35224c19ef4SMark Fasheh 	if (create_ino) {
35324c19ef4SMark Fasheh 		inode->i_ino = ino_from_blkno(inode->i_sb,
35424c19ef4SMark Fasheh 			       le64_to_cpu(fe->i_blkno));
35524c19ef4SMark Fasheh 
35624c19ef4SMark Fasheh 		/*
35724c19ef4SMark Fasheh 		 * If we ever want to create system files from kernel,
35824c19ef4SMark Fasheh 		 * the generation argument to
35924c19ef4SMark Fasheh 		 * ocfs2_inode_lock_res_init() will have to change.
36024c19ef4SMark Fasheh 		 */
3611ca1a111SMark Fasheh 		BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL);
36224c19ef4SMark Fasheh 
363e63aecb6SMark Fasheh 		ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
36424c19ef4SMark Fasheh 					  OCFS2_LOCK_TYPE_META, 0, inode);
36550008630STiger Yang 
36650008630STiger Yang 		ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
36750008630STiger Yang 					  OCFS2_LOCK_TYPE_OPEN, 0, inode);
36824c19ef4SMark Fasheh 	}
36924c19ef4SMark Fasheh 
37024c19ef4SMark Fasheh 	ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
37124c19ef4SMark Fasheh 				  OCFS2_LOCK_TYPE_RW, inode->i_generation,
37224c19ef4SMark Fasheh 				  inode);
37324c19ef4SMark Fasheh 
374ca4d147eSHerbert Poetzl 	ocfs2_set_inode_flags(inode);
375ca4d147eSHerbert Poetzl 
37613821151STao Ma 	OCFS2_I(inode)->ip_last_used_slot = 0;
37713821151STao Ma 	OCFS2_I(inode)->ip_last_used_group = 0;
378e3b4a97dSMark Fasheh 
379e3b4a97dSMark Fasheh 	if (S_ISDIR(inode->i_mode))
380e3b4a97dSMark Fasheh 		ocfs2_resv_set_type(&OCFS2_I(inode)->ip_la_data_resv,
381e3b4a97dSMark Fasheh 				    OCFS2_RESV_FLAG_DIR);
382ccd979bdSMark Fasheh }
383ccd979bdSMark Fasheh 
384ccd979bdSMark Fasheh static int ocfs2_read_locked_inode(struct inode *inode,
385ccd979bdSMark Fasheh 				   struct ocfs2_find_inode_args *args)
386ccd979bdSMark Fasheh {
387ccd979bdSMark Fasheh 	struct super_block *sb;
388ccd979bdSMark Fasheh 	struct ocfs2_super *osb;
389ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
390ccd979bdSMark Fasheh 	struct buffer_head *bh = NULL;
39124c19ef4SMark Fasheh 	int status, can_lock;
39224c19ef4SMark Fasheh 	u32 generation = 0;
393ccd979bdSMark Fasheh 
394ef6b689bSTao Ma 	mlog(0, "(0x%p, 0x%p)\n", inode, args);
395ccd979bdSMark Fasheh 
396ccd979bdSMark Fasheh 	status = -EINVAL;
397ccd979bdSMark Fasheh 	if (inode == NULL || inode->i_sb == NULL) {
398ccd979bdSMark Fasheh 		mlog(ML_ERROR, "bad inode\n");
39924c19ef4SMark Fasheh 		return status;
400ccd979bdSMark Fasheh 	}
401ccd979bdSMark Fasheh 	sb = inode->i_sb;
402ccd979bdSMark Fasheh 	osb = OCFS2_SB(sb);
403ccd979bdSMark Fasheh 
404ccd979bdSMark Fasheh 	if (!args) {
405ccd979bdSMark Fasheh 		mlog(ML_ERROR, "bad inode args\n");
406ccd979bdSMark Fasheh 		make_bad_inode(inode);
40724c19ef4SMark Fasheh 		return status;
408ccd979bdSMark Fasheh 	}
409ccd979bdSMark Fasheh 
41024c19ef4SMark Fasheh 	/*
41124c19ef4SMark Fasheh 	 * To improve performance of cold-cache inode stats, we take
41224c19ef4SMark Fasheh 	 * the cluster lock here if possible.
41324c19ef4SMark Fasheh 	 *
41424c19ef4SMark Fasheh 	 * Generally, OCFS2 never trusts the contents of an inode
41524c19ef4SMark Fasheh 	 * unless it's holding a cluster lock, so taking it here isn't
41624c19ef4SMark Fasheh 	 * a correctness issue as much as it is a performance
41724c19ef4SMark Fasheh 	 * improvement.
41824c19ef4SMark Fasheh 	 *
41924c19ef4SMark Fasheh 	 * There are three times when taking the lock is not a good idea:
42024c19ef4SMark Fasheh 	 *
42124c19ef4SMark Fasheh 	 * 1) During startup, before we have initialized the DLM.
42224c19ef4SMark Fasheh 	 *
42324c19ef4SMark Fasheh 	 * 2) If we are reading certain system files which never get
42424c19ef4SMark Fasheh 	 *    cluster locks (local alloc, truncate log).
42524c19ef4SMark Fasheh 	 *
42624c19ef4SMark Fasheh 	 * 3) If the process doing the iget() is responsible for
42724c19ef4SMark Fasheh 	 *    orphan dir recovery. We're holding the orphan dir lock and
42824c19ef4SMark Fasheh 	 *    can get into a deadlock with another process on another
42924c19ef4SMark Fasheh 	 *    node in ->delete_inode().
43024c19ef4SMark Fasheh 	 *
43124c19ef4SMark Fasheh 	 * #1 and #2 can be simply solved by never taking the lock
43224c19ef4SMark Fasheh 	 * here for system files (which are the only type we read
43324c19ef4SMark Fasheh 	 * during mount). It's a heavier approach, but our main
434b595076aSUwe Kleine-König 	 * concern is user-accessible files anyway.
43524c19ef4SMark Fasheh 	 *
43624c19ef4SMark Fasheh 	 * #3 works itself out because we'll eventually take the
43724c19ef4SMark Fasheh 	 * cluster lock before trusting anything anyway.
43824c19ef4SMark Fasheh 	 */
43924c19ef4SMark Fasheh 	can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
44050008630STiger Yang 		&& !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY)
441c271c5c2SSunil Mushran 		&& !ocfs2_mount_local(osb);
44224c19ef4SMark Fasheh 
44324c19ef4SMark Fasheh 	/*
44424c19ef4SMark Fasheh 	 * To maintain backwards compatibility with older versions of
44524c19ef4SMark Fasheh 	 * ocfs2-tools, we still store the generation value for system
44624c19ef4SMark Fasheh 	 * files. The only ones that actually matter to userspace are
44724c19ef4SMark Fasheh 	 * the journals, but it's easier and inexpensive to just flag
44824c19ef4SMark Fasheh 	 * all system files similarly.
44924c19ef4SMark Fasheh 	 */
45024c19ef4SMark Fasheh 	if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
45124c19ef4SMark Fasheh 		generation = osb->fs_generation;
45224c19ef4SMark Fasheh 
453e63aecb6SMark Fasheh 	ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
45424c19ef4SMark Fasheh 				  OCFS2_LOCK_TYPE_META,
45524c19ef4SMark Fasheh 				  generation, inode);
45624c19ef4SMark Fasheh 
45750008630STiger Yang 	ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
45850008630STiger Yang 				  OCFS2_LOCK_TYPE_OPEN,
45950008630STiger Yang 				  0, inode);
46050008630STiger Yang 
46124c19ef4SMark Fasheh 	if (can_lock) {
46250008630STiger Yang 		status = ocfs2_open_lock(inode);
46350008630STiger Yang 		if (status) {
46450008630STiger Yang 			make_bad_inode(inode);
46550008630STiger Yang 			mlog_errno(status);
46650008630STiger Yang 			return status;
46750008630STiger Yang 		}
468e63aecb6SMark Fasheh 		status = ocfs2_inode_lock(inode, NULL, 0);
46924c19ef4SMark Fasheh 		if (status) {
47024c19ef4SMark Fasheh 			make_bad_inode(inode);
47124c19ef4SMark Fasheh 			mlog_errno(status);
47224c19ef4SMark Fasheh 			return status;
47324c19ef4SMark Fasheh 		}
47424c19ef4SMark Fasheh 	}
47524c19ef4SMark Fasheh 
47650008630STiger Yang 	if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) {
47750008630STiger Yang 		status = ocfs2_try_open_lock(inode, 0);
47850008630STiger Yang 		if (status) {
47950008630STiger Yang 			make_bad_inode(inode);
48050008630STiger Yang 			return status;
48150008630STiger Yang 		}
48250008630STiger Yang 	}
48350008630STiger Yang 
484b657c95cSJoel Becker 	if (can_lock) {
485b657c95cSJoel Becker 		status = ocfs2_read_inode_block_full(inode, &bh,
486d4a8c93cSJoel Becker 						     OCFS2_BH_IGNORE_CACHE);
487b657c95cSJoel Becker 	} else {
488da1e9098SJoel Becker 		status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh);
489f5ce5a08SSunil Mushran 		/*
490f5ce5a08SSunil Mushran 		 * If buffer is in jbd, then its checksum may not have been
491f5ce5a08SSunil Mushran 		 * computed as yet.
492f5ce5a08SSunil Mushran 		 */
493f5ce5a08SSunil Mushran 		if (!status && !buffer_jbd(bh))
494b657c95cSJoel Becker 			status = ocfs2_validate_inode_block(osb->sb, bh);
495b657c95cSJoel Becker 	}
496ccd979bdSMark Fasheh 	if (status < 0) {
497ccd979bdSMark Fasheh 		mlog_errno(status);
498ccd979bdSMark Fasheh 		goto bail;
499ccd979bdSMark Fasheh 	}
500ccd979bdSMark Fasheh 
50124c19ef4SMark Fasheh 	status = -EINVAL;
502ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) bh->b_data;
503ccd979bdSMark Fasheh 
50424c19ef4SMark Fasheh 	/*
50524c19ef4SMark Fasheh 	 * This is a code bug. Right now the caller needs to
50624c19ef4SMark Fasheh 	 * understand whether it is asking for a system file inode or
50724c19ef4SMark Fasheh 	 * not so the proper lock names can be built.
50824c19ef4SMark Fasheh 	 */
50924c19ef4SMark Fasheh 	mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) !=
51024c19ef4SMark Fasheh 			!!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE),
51124c19ef4SMark Fasheh 			"Inode %llu: system file state is ambigous\n",
51224c19ef4SMark Fasheh 			(unsigned long long)args->fi_blkno);
513ccd979bdSMark Fasheh 
514ccd979bdSMark Fasheh 	if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
515ccd979bdSMark Fasheh 	    S_ISBLK(le16_to_cpu(fe->i_mode)))
516ccd979bdSMark Fasheh 		inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
517ccd979bdSMark Fasheh 
518b657c95cSJoel Becker 	ocfs2_populate_inode(inode, fe, 0);
519ccd979bdSMark Fasheh 
520ccd979bdSMark Fasheh 	BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
521ccd979bdSMark Fasheh 
522ccd979bdSMark Fasheh 	status = 0;
523ccd979bdSMark Fasheh 
524ccd979bdSMark Fasheh bail:
52524c19ef4SMark Fasheh 	if (can_lock)
526e63aecb6SMark Fasheh 		ocfs2_inode_unlock(inode, 0);
52724c19ef4SMark Fasheh 
52824c19ef4SMark Fasheh 	if (status < 0)
52924c19ef4SMark Fasheh 		make_bad_inode(inode);
53024c19ef4SMark Fasheh 
531ccd979bdSMark Fasheh 	if (args && bh)
532ccd979bdSMark Fasheh 		brelse(bh);
533ccd979bdSMark Fasheh 
534ccd979bdSMark Fasheh 	return status;
535ccd979bdSMark Fasheh }
536ccd979bdSMark Fasheh 
537ccd979bdSMark Fasheh void ocfs2_sync_blockdev(struct super_block *sb)
538ccd979bdSMark Fasheh {
539ccd979bdSMark Fasheh 	sync_blockdev(sb->s_bdev);
540ccd979bdSMark Fasheh }
541ccd979bdSMark Fasheh 
542ccd979bdSMark Fasheh static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
543ccd979bdSMark Fasheh 				     struct inode *inode,
544ccd979bdSMark Fasheh 				     struct buffer_head *fe_bh)
545ccd979bdSMark Fasheh {
546ccd979bdSMark Fasheh 	int status = 0;
547ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe;
54860b11392SMark Fasheh 	handle_t *handle = NULL;
549ccd979bdSMark Fasheh 
550ccd979bdSMark Fasheh 	fe = (struct ocfs2_dinode *) fe_bh->b_data;
551ccd979bdSMark Fasheh 
5521afc32b9SMark Fasheh 	/*
5531afc32b9SMark Fasheh 	 * This check will also skip truncate of inodes with inline
5541afc32b9SMark Fasheh 	 * data and fast symlinks.
5551afc32b9SMark Fasheh 	 */
5563a0782d0SMark Fasheh 	if (fe->i_clusters) {
5572b4e30fbSJoel Becker 		if (ocfs2_should_order_data(inode))
5582b4e30fbSJoel Becker 			ocfs2_begin_ordered_truncate(inode, 0);
5592b4e30fbSJoel Becker 
56060b11392SMark Fasheh 		handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
56160b11392SMark Fasheh 		if (IS_ERR(handle)) {
56260b11392SMark Fasheh 			status = PTR_ERR(handle);
5630350cb07SDan Carpenter 			handle = NULL;
56460b11392SMark Fasheh 			mlog_errno(status);
56560b11392SMark Fasheh 			goto out;
56660b11392SMark Fasheh 		}
56760b11392SMark Fasheh 
5680cf2f763SJoel Becker 		status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
5690cf2f763SJoel Becker 						 fe_bh,
57060b11392SMark Fasheh 						 OCFS2_JOURNAL_ACCESS_WRITE);
57160b11392SMark Fasheh 		if (status < 0) {
57260b11392SMark Fasheh 			mlog_errno(status);
57360b11392SMark Fasheh 			goto out;
57460b11392SMark Fasheh 		}
57560b11392SMark Fasheh 
57660b11392SMark Fasheh 		i_size_write(inode, 0);
57760b11392SMark Fasheh 
57860b11392SMark Fasheh 		status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
57960b11392SMark Fasheh 		if (status < 0) {
58060b11392SMark Fasheh 			mlog_errno(status);
58160b11392SMark Fasheh 			goto out;
58260b11392SMark Fasheh 		}
58360b11392SMark Fasheh 
58460b11392SMark Fasheh 		ocfs2_commit_trans(osb, handle);
58560b11392SMark Fasheh 		handle = NULL;
58660b11392SMark Fasheh 
58778f94673STristan Ye 		status = ocfs2_commit_truncate(osb, inode, fe_bh);
588ccd979bdSMark Fasheh 		if (status < 0) {
589ccd979bdSMark Fasheh 			mlog_errno(status);
5903a0782d0SMark Fasheh 			goto out;
591ccd979bdSMark Fasheh 		}
5923a0782d0SMark Fasheh 	}
593ccd979bdSMark Fasheh 
59460b11392SMark Fasheh out:
59560b11392SMark Fasheh 	if (handle)
59660b11392SMark Fasheh 		ocfs2_commit_trans(osb, handle);
597ccd979bdSMark Fasheh 	return status;
598ccd979bdSMark Fasheh }
599ccd979bdSMark Fasheh 
600ccd979bdSMark Fasheh static int ocfs2_remove_inode(struct inode *inode,
601ccd979bdSMark Fasheh 			      struct buffer_head *di_bh,
602ccd979bdSMark Fasheh 			      struct inode *orphan_dir_inode,
603ccd979bdSMark Fasheh 			      struct buffer_head *orphan_dir_bh)
604ccd979bdSMark Fasheh {
605ccd979bdSMark Fasheh 	int status;
606ccd979bdSMark Fasheh 	struct inode *inode_alloc_inode = NULL;
607ccd979bdSMark Fasheh 	struct buffer_head *inode_alloc_bh = NULL;
6081fabe148SMark Fasheh 	handle_t *handle;
609ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
610ccd979bdSMark Fasheh 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
611ccd979bdSMark Fasheh 
612ccd979bdSMark Fasheh 	inode_alloc_inode =
613ccd979bdSMark Fasheh 		ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
614ccd979bdSMark Fasheh 					    le16_to_cpu(di->i_suballoc_slot));
615ccd979bdSMark Fasheh 	if (!inode_alloc_inode) {
616ccd979bdSMark Fasheh 		status = -EEXIST;
617ccd979bdSMark Fasheh 		mlog_errno(status);
618ccd979bdSMark Fasheh 		goto bail;
619ccd979bdSMark Fasheh 	}
620ccd979bdSMark Fasheh 
6211b1dcc1bSJes Sorensen 	mutex_lock(&inode_alloc_inode->i_mutex);
622e63aecb6SMark Fasheh 	status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1);
623ccd979bdSMark Fasheh 	if (status < 0) {
6241b1dcc1bSJes Sorensen 		mutex_unlock(&inode_alloc_inode->i_mutex);
625ccd979bdSMark Fasheh 
626ccd979bdSMark Fasheh 		mlog_errno(status);
627ccd979bdSMark Fasheh 		goto bail;
628ccd979bdSMark Fasheh 	}
629ccd979bdSMark Fasheh 
630a90714c1SJan Kara 	handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS +
631a90714c1SJan Kara 				   ocfs2_quota_trans_credits(inode->i_sb));
632ccd979bdSMark Fasheh 	if (IS_ERR(handle)) {
633ccd979bdSMark Fasheh 		status = PTR_ERR(handle);
634ccd979bdSMark Fasheh 		mlog_errno(status);
635ccd979bdSMark Fasheh 		goto bail_unlock;
636ccd979bdSMark Fasheh 	}
637ccd979bdSMark Fasheh 
638d4cd1871SLi Dongyang 	if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
639ccd979bdSMark Fasheh 		status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
640ccd979bdSMark Fasheh 					  orphan_dir_bh);
641ccd979bdSMark Fasheh 		if (status < 0) {
642ccd979bdSMark Fasheh 			mlog_errno(status);
643ccd979bdSMark Fasheh 			goto bail_commit;
644ccd979bdSMark Fasheh 		}
645d4cd1871SLi Dongyang 	}
646ccd979bdSMark Fasheh 
647ccd979bdSMark Fasheh 	/* set the inodes dtime */
6480cf2f763SJoel Becker 	status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
649ccd979bdSMark Fasheh 					 OCFS2_JOURNAL_ACCESS_WRITE);
650ccd979bdSMark Fasheh 	if (status < 0) {
651ccd979bdSMark Fasheh 		mlog_errno(status);
652ccd979bdSMark Fasheh 		goto bail_commit;
653ccd979bdSMark Fasheh 	}
654ccd979bdSMark Fasheh 
655ccd979bdSMark Fasheh 	di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec);
6564092d49fSMarcin Slusarz 	di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
657ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, di_bh);
658ccd979bdSMark Fasheh 
6598cb471e8SJoel Becker 	ocfs2_remove_from_cache(INODE_CACHE(inode), di_bh);
66063936ddaSChristoph Hellwig 	dquot_free_inode(inode);
661ccd979bdSMark Fasheh 
662ccd979bdSMark Fasheh 	status = ocfs2_free_dinode(handle, inode_alloc_inode,
663ccd979bdSMark Fasheh 				   inode_alloc_bh, di);
664ccd979bdSMark Fasheh 	if (status < 0)
665ccd979bdSMark Fasheh 		mlog_errno(status);
666ccd979bdSMark Fasheh 
667ccd979bdSMark Fasheh bail_commit:
66802dc1af4SMark Fasheh 	ocfs2_commit_trans(osb, handle);
669ccd979bdSMark Fasheh bail_unlock:
670e63aecb6SMark Fasheh 	ocfs2_inode_unlock(inode_alloc_inode, 1);
6711b1dcc1bSJes Sorensen 	mutex_unlock(&inode_alloc_inode->i_mutex);
672ccd979bdSMark Fasheh 	brelse(inode_alloc_bh);
673ccd979bdSMark Fasheh bail:
674ccd979bdSMark Fasheh 	iput(inode_alloc_inode);
675ccd979bdSMark Fasheh 
676ccd979bdSMark Fasheh 	return status;
677ccd979bdSMark Fasheh }
678ccd979bdSMark Fasheh 
679b4df6ed8SMark Fasheh /*
680b4df6ed8SMark Fasheh  * Serialize with orphan dir recovery. If the process doing
681b4df6ed8SMark Fasheh  * recovery on this orphan dir does an iget() with the dir
682b4df6ed8SMark Fasheh  * i_mutex held, we'll deadlock here. Instead we detect this
683b4df6ed8SMark Fasheh  * and exit early - recovery will wipe this inode for us.
684b4df6ed8SMark Fasheh  */
685b4df6ed8SMark Fasheh static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
686b4df6ed8SMark Fasheh 					     int slot)
687b4df6ed8SMark Fasheh {
688b4df6ed8SMark Fasheh 	int ret = 0;
689b4df6ed8SMark Fasheh 
690b4df6ed8SMark Fasheh 	spin_lock(&osb->osb_lock);
691b4df6ed8SMark Fasheh 	if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) {
692b4df6ed8SMark Fasheh 		mlog(0, "Recovery is happening on orphan dir %d, will skip "
693b4df6ed8SMark Fasheh 		     "this inode\n", slot);
694b4df6ed8SMark Fasheh 		ret = -EDEADLK;
695b4df6ed8SMark Fasheh 		goto out;
696b4df6ed8SMark Fasheh 	}
697b4df6ed8SMark Fasheh 	/* This signals to the orphan recovery process that it should
698b4df6ed8SMark Fasheh 	 * wait for us to handle the wipe. */
699b4df6ed8SMark Fasheh 	osb->osb_orphan_wipes[slot]++;
700b4df6ed8SMark Fasheh out:
701b4df6ed8SMark Fasheh 	spin_unlock(&osb->osb_lock);
702b4df6ed8SMark Fasheh 	return ret;
703b4df6ed8SMark Fasheh }
704b4df6ed8SMark Fasheh 
705b4df6ed8SMark Fasheh static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
706b4df6ed8SMark Fasheh 					 int slot)
707b4df6ed8SMark Fasheh {
708b4df6ed8SMark Fasheh 	spin_lock(&osb->osb_lock);
709b4df6ed8SMark Fasheh 	osb->osb_orphan_wipes[slot]--;
710b4df6ed8SMark Fasheh 	spin_unlock(&osb->osb_lock);
711b4df6ed8SMark Fasheh 
712b4df6ed8SMark Fasheh 	wake_up(&osb->osb_wipe_event);
713b4df6ed8SMark Fasheh }
714b4df6ed8SMark Fasheh 
715ccd979bdSMark Fasheh static int ocfs2_wipe_inode(struct inode *inode,
716ccd979bdSMark Fasheh 			    struct buffer_head *di_bh)
717ccd979bdSMark Fasheh {
718d577632eSJoel Becker 	int status, orphaned_slot = -1;
719ccd979bdSMark Fasheh 	struct inode *orphan_dir_inode = NULL;
720ccd979bdSMark Fasheh 	struct buffer_head *orphan_dir_bh = NULL;
721ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
722d4cd1871SLi Dongyang 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
723ccd979bdSMark Fasheh 
724d4cd1871SLi Dongyang 	if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
72550008630STiger Yang 		orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
726b4df6ed8SMark Fasheh 
727b4df6ed8SMark Fasheh 		status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
728b4df6ed8SMark Fasheh 		if (status)
729b4df6ed8SMark Fasheh 			return status;
730b4df6ed8SMark Fasheh 
731ccd979bdSMark Fasheh 		orphan_dir_inode = ocfs2_get_system_file_inode(osb,
732ccd979bdSMark Fasheh 							       ORPHAN_DIR_SYSTEM_INODE,
733ccd979bdSMark Fasheh 							       orphaned_slot);
734ccd979bdSMark Fasheh 		if (!orphan_dir_inode) {
735ccd979bdSMark Fasheh 			status = -EEXIST;
736ccd979bdSMark Fasheh 			mlog_errno(status);
737ccd979bdSMark Fasheh 			goto bail;
738ccd979bdSMark Fasheh 		}
739ccd979bdSMark Fasheh 
740ccd979bdSMark Fasheh 		/* Lock the orphan dir. The lock will be held for the entire
741ccd979bdSMark Fasheh 		 * delete_inode operation. We do this now to avoid races with
742ccd979bdSMark Fasheh 		 * recovery completion on other nodes. */
7431b1dcc1bSJes Sorensen 		mutex_lock(&orphan_dir_inode->i_mutex);
744e63aecb6SMark Fasheh 		status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
745ccd979bdSMark Fasheh 		if (status < 0) {
7461b1dcc1bSJes Sorensen 			mutex_unlock(&orphan_dir_inode->i_mutex);
747ccd979bdSMark Fasheh 
748ccd979bdSMark Fasheh 			mlog_errno(status);
749ccd979bdSMark Fasheh 			goto bail;
750ccd979bdSMark Fasheh 		}
751d4cd1871SLi Dongyang 	}
752ccd979bdSMark Fasheh 
753ccd979bdSMark Fasheh 	/* we do this while holding the orphan dir lock because we
75434d024f8SMark Fasheh 	 * don't want recovery being run from another node to try an
75534d024f8SMark Fasheh 	 * inode delete underneath us -- this will result in two nodes
756ccd979bdSMark Fasheh 	 * truncating the same file! */
757ccd979bdSMark Fasheh 	status = ocfs2_truncate_for_delete(osb, inode, di_bh);
758ccd979bdSMark Fasheh 	if (status < 0) {
759ccd979bdSMark Fasheh 		mlog_errno(status);
760ccd979bdSMark Fasheh 		goto bail_unlock_dir;
761ccd979bdSMark Fasheh 	}
762ccd979bdSMark Fasheh 
7639b7895efSMark Fasheh 	/* Remove any dir index tree */
7649b7895efSMark Fasheh 	if (S_ISDIR(inode->i_mode)) {
7659b7895efSMark Fasheh 		status = ocfs2_dx_dir_truncate(inode, di_bh);
7669b7895efSMark Fasheh 		if (status) {
7679b7895efSMark Fasheh 			mlog_errno(status);
7689b7895efSMark Fasheh 			goto bail_unlock_dir;
7699b7895efSMark Fasheh 		}
7709b7895efSMark Fasheh 	}
7719b7895efSMark Fasheh 
772cf1d6c76STiger Yang 	/*Free extended attribute resources associated with this inode.*/
773cf1d6c76STiger Yang 	status = ocfs2_xattr_remove(inode, di_bh);
774cf1d6c76STiger Yang 	if (status < 0) {
775cf1d6c76STiger Yang 		mlog_errno(status);
776cf1d6c76STiger Yang 		goto bail_unlock_dir;
777cf1d6c76STiger Yang 	}
778cf1d6c76STiger Yang 
7798b2c0dbaSTao Ma 	status = ocfs2_remove_refcount_tree(inode, di_bh);
7808b2c0dbaSTao Ma 	if (status < 0) {
7818b2c0dbaSTao Ma 		mlog_errno(status);
7828b2c0dbaSTao Ma 		goto bail_unlock_dir;
7838b2c0dbaSTao Ma 	}
7848b2c0dbaSTao Ma 
785ccd979bdSMark Fasheh 	status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
786ccd979bdSMark Fasheh 				    orphan_dir_bh);
787ccd979bdSMark Fasheh 	if (status < 0)
788ccd979bdSMark Fasheh 		mlog_errno(status);
789ccd979bdSMark Fasheh 
790ccd979bdSMark Fasheh bail_unlock_dir:
791d4cd1871SLi Dongyang 	if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)
792d4cd1871SLi Dongyang 		return status;
793d4cd1871SLi Dongyang 
794e63aecb6SMark Fasheh 	ocfs2_inode_unlock(orphan_dir_inode, 1);
7951b1dcc1bSJes Sorensen 	mutex_unlock(&orphan_dir_inode->i_mutex);
796ccd979bdSMark Fasheh 	brelse(orphan_dir_bh);
797ccd979bdSMark Fasheh bail:
798ccd979bdSMark Fasheh 	iput(orphan_dir_inode);
799b4df6ed8SMark Fasheh 	ocfs2_signal_wipe_completion(osb, orphaned_slot);
800ccd979bdSMark Fasheh 
801ccd979bdSMark Fasheh 	return status;
802ccd979bdSMark Fasheh }
803ccd979bdSMark Fasheh 
804ccd979bdSMark Fasheh /* There is a series of simple checks that should be done before a
80534d024f8SMark Fasheh  * trylock is even considered. Encapsulate those in this function. */
806ccd979bdSMark Fasheh static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
807ccd979bdSMark Fasheh {
808ccd979bdSMark Fasheh 	int ret = 0;
809ccd979bdSMark Fasheh 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
810ccd979bdSMark Fasheh 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
811ccd979bdSMark Fasheh 
812ccd979bdSMark Fasheh 	/* We shouldn't be getting here for the root directory
813ccd979bdSMark Fasheh 	 * inode.. */
814ccd979bdSMark Fasheh 	if (inode == osb->root_inode) {
815ccd979bdSMark Fasheh 		mlog(ML_ERROR, "Skipping delete of root inode.\n");
816ccd979bdSMark Fasheh 		goto bail;
817ccd979bdSMark Fasheh 	}
818ccd979bdSMark Fasheh 
81934d024f8SMark Fasheh 	/* If we're coming from downconvert_thread we can't go into our own
820ccd979bdSMark Fasheh 	 * voting [hello, deadlock city!], so unforuntately we just
821ccd979bdSMark Fasheh 	 * have to skip deleting this guy. That's OK though because
822ccd979bdSMark Fasheh 	 * the node who's doing the actual deleting should handle it
823ccd979bdSMark Fasheh 	 * anyway. */
82434d024f8SMark Fasheh 	if (current == osb->dc_task) {
825ccd979bdSMark Fasheh 		mlog(0, "Skipping delete of %lu because we're currently "
82634d024f8SMark Fasheh 		     "in downconvert\n", inode->i_ino);
827ccd979bdSMark Fasheh 		goto bail;
828ccd979bdSMark Fasheh 	}
829ccd979bdSMark Fasheh 
830ccd979bdSMark Fasheh 	spin_lock(&oi->ip_lock);
831ccd979bdSMark Fasheh 	/* OCFS2 *never* deletes system files. This should technically
832ccd979bdSMark Fasheh 	 * never get here as system file inodes should always have a
833ccd979bdSMark Fasheh 	 * positive link count. */
834ccd979bdSMark Fasheh 	if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
835b0697053SMark Fasheh 		mlog(ML_ERROR, "Skipping delete of system file %llu\n",
836b0697053SMark Fasheh 		     (unsigned long long)oi->ip_blkno);
837ccd979bdSMark Fasheh 		goto bail_unlock;
838ccd979bdSMark Fasheh 	}
839ccd979bdSMark Fasheh 
84034d024f8SMark Fasheh 	/* If we have allowd wipe of this inode for another node, it
84134d024f8SMark Fasheh 	 * will be marked here so we can safely skip it. Recovery will
84234d024f8SMark Fasheh 	 * cleanup any inodes we might inadvertantly skip here. */
843ccd979bdSMark Fasheh 	if (oi->ip_flags & OCFS2_INODE_SKIP_DELETE) {
844ccd979bdSMark Fasheh 		mlog(0, "Skipping delete of %lu because another node "
845ccd979bdSMark Fasheh 		     "has done this for us.\n", inode->i_ino);
846ccd979bdSMark Fasheh 		goto bail_unlock;
847ccd979bdSMark Fasheh 	}
848ccd979bdSMark Fasheh 
849ccd979bdSMark Fasheh 	ret = 1;
850ccd979bdSMark Fasheh bail_unlock:
851ccd979bdSMark Fasheh 	spin_unlock(&oi->ip_lock);
852ccd979bdSMark Fasheh bail:
853ccd979bdSMark Fasheh 	return ret;
854ccd979bdSMark Fasheh }
855ccd979bdSMark Fasheh 
856ccd979bdSMark Fasheh /* Query the cluster to determine whether we should wipe an inode from
857ccd979bdSMark Fasheh  * disk or not.
858ccd979bdSMark Fasheh  *
859ccd979bdSMark Fasheh  * Requires the inode to have the cluster lock. */
860ccd979bdSMark Fasheh static int ocfs2_query_inode_wipe(struct inode *inode,
861ccd979bdSMark Fasheh 				  struct buffer_head *di_bh,
862ccd979bdSMark Fasheh 				  int *wipe)
863ccd979bdSMark Fasheh {
864ccd979bdSMark Fasheh 	int status = 0;
865ccd979bdSMark Fasheh 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
866ccd979bdSMark Fasheh 	struct ocfs2_dinode *di;
867ccd979bdSMark Fasheh 
868ccd979bdSMark Fasheh 	*wipe = 0;
869ccd979bdSMark Fasheh 
870ccd979bdSMark Fasheh 	/* While we were waiting for the cluster lock in
871ccd979bdSMark Fasheh 	 * ocfs2_delete_inode, another node might have asked to delete
872ccd979bdSMark Fasheh 	 * the inode. Recheck our flags to catch this. */
873ccd979bdSMark Fasheh 	if (!ocfs2_inode_is_valid_to_delete(inode)) {
874b0697053SMark Fasheh 		mlog(0, "Skipping delete of %llu because flags changed\n",
875b0697053SMark Fasheh 		     (unsigned long long)oi->ip_blkno);
876ccd979bdSMark Fasheh 		goto bail;
877ccd979bdSMark Fasheh 	}
878ccd979bdSMark Fasheh 
879ccd979bdSMark Fasheh 	/* Now that we have an up to date inode, we can double check
880ccd979bdSMark Fasheh 	 * the link count. */
881ccd979bdSMark Fasheh 	if (inode->i_nlink) {
882b0697053SMark Fasheh 		mlog(0, "Skipping delete of %llu because nlink = %u\n",
883b0697053SMark Fasheh 		     (unsigned long long)oi->ip_blkno, inode->i_nlink);
884ccd979bdSMark Fasheh 		goto bail;
885ccd979bdSMark Fasheh 	}
886ccd979bdSMark Fasheh 
887ccd979bdSMark Fasheh 	/* Do some basic inode verification... */
888ccd979bdSMark Fasheh 	di = (struct ocfs2_dinode *) di_bh->b_data;
889d4cd1871SLi Dongyang 	if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL)) &&
890d4cd1871SLi Dongyang 	    !(oi->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
891b54c2ca4STristan Ye 		/*
892b54c2ca4STristan Ye 		 * Inodes in the orphan dir must have ORPHANED_FL.  The only
893b54c2ca4STristan Ye 		 * inodes that come back out of the orphan dir are reflink
894b54c2ca4STristan Ye 		 * targets. A reflink target may be moved out of the orphan
895b54c2ca4STristan Ye 		 * dir between the time we scan the directory and the time we
896b54c2ca4STristan Ye 		 * process it. This would lead to HAS_REFCOUNT_FL being set but
897b54c2ca4STristan Ye 		 * ORPHANED_FL not.
898b54c2ca4STristan Ye 		 */
899b54c2ca4STristan Ye 		if (di->i_dyn_features & cpu_to_le16(OCFS2_HAS_REFCOUNT_FL)) {
900b54c2ca4STristan Ye 			mlog(0, "Reflinked inode %llu is no longer orphaned.  "
901b54c2ca4STristan Ye 			     "it shouldn't be deleted\n",
902b54c2ca4STristan Ye 			     (unsigned long long)oi->ip_blkno);
903b54c2ca4STristan Ye 			goto bail;
904b54c2ca4STristan Ye 		}
905b54c2ca4STristan Ye 
906ccd979bdSMark Fasheh 		/* for lack of a better error? */
907ccd979bdSMark Fasheh 		status = -EEXIST;
908ccd979bdSMark Fasheh 		mlog(ML_ERROR,
909b0697053SMark Fasheh 		     "Inode %llu (on-disk %llu) not orphaned! "
910ccd979bdSMark Fasheh 		     "Disk flags  0x%x, inode flags 0x%x\n",
911b0697053SMark Fasheh 		     (unsigned long long)oi->ip_blkno,
9121ca1a111SMark Fasheh 		     (unsigned long long)le64_to_cpu(di->i_blkno),
9131ca1a111SMark Fasheh 		     le32_to_cpu(di->i_flags), oi->ip_flags);
914ccd979bdSMark Fasheh 		goto bail;
915ccd979bdSMark Fasheh 	}
916ccd979bdSMark Fasheh 
917ccd979bdSMark Fasheh 	/* has someone already deleted us?! baaad... */
918ccd979bdSMark Fasheh 	if (di->i_dtime) {
919ccd979bdSMark Fasheh 		status = -EEXIST;
920ccd979bdSMark Fasheh 		mlog_errno(status);
921ccd979bdSMark Fasheh 		goto bail;
922ccd979bdSMark Fasheh 	}
923ccd979bdSMark Fasheh 
9246f16bf65SMark Fasheh 	/*
9256f16bf65SMark Fasheh 	 * This is how ocfs2 determines whether an inode is still live
9266f16bf65SMark Fasheh 	 * within the cluster. Every node takes a shared read lock on
9276f16bf65SMark Fasheh 	 * the inode open lock in ocfs2_read_locked_inode(). When we
9286f16bf65SMark Fasheh 	 * get to ->delete_inode(), each node tries to convert it's
9296f16bf65SMark Fasheh 	 * lock to an exclusive. Trylocks are serialized by the inode
9306f16bf65SMark Fasheh 	 * meta data lock. If the upconvert suceeds, we know the inode
9316f16bf65SMark Fasheh 	 * is no longer live and can be deleted.
9326f16bf65SMark Fasheh 	 *
9336f16bf65SMark Fasheh 	 * Though we call this with the meta data lock held, the
9346f16bf65SMark Fasheh 	 * trylock keeps us from ABBA deadlock.
9356f16bf65SMark Fasheh 	 */
9366f16bf65SMark Fasheh 	status = ocfs2_try_open_lock(inode, 1);
93750008630STiger Yang 	if (status == -EAGAIN) {
938ccd979bdSMark Fasheh 		status = 0;
939b0697053SMark Fasheh 		mlog(0, "Skipping delete of %llu because it is in use on "
940b0697053SMark Fasheh 		     "other nodes\n", (unsigned long long)oi->ip_blkno);
941ccd979bdSMark Fasheh 		goto bail;
942ccd979bdSMark Fasheh 	}
943ccd979bdSMark Fasheh 	if (status < 0) {
944ccd979bdSMark Fasheh 		mlog_errno(status);
945ccd979bdSMark Fasheh 		goto bail;
946ccd979bdSMark Fasheh 	}
947ccd979bdSMark Fasheh 
948ccd979bdSMark Fasheh 	*wipe = 1;
94950008630STiger Yang 	mlog(0, "Inode %llu is ok to wipe from orphan dir %u\n",
95050008630STiger Yang 	     (unsigned long long)oi->ip_blkno,
95150008630STiger Yang 	     le16_to_cpu(di->i_orphaned_slot));
952ccd979bdSMark Fasheh 
953ccd979bdSMark Fasheh bail:
954ccd979bdSMark Fasheh 	return status;
955ccd979bdSMark Fasheh }
956ccd979bdSMark Fasheh 
957ccd979bdSMark Fasheh /* Support function for ocfs2_delete_inode. Will help us keep the
958ccd979bdSMark Fasheh  * inode data in a consistent state for clear_inode. Always truncates
959ccd979bdSMark Fasheh  * pages, optionally sync's them first. */
960ccd979bdSMark Fasheh static void ocfs2_cleanup_delete_inode(struct inode *inode,
961ccd979bdSMark Fasheh 				       int sync_data)
962ccd979bdSMark Fasheh {
963b0697053SMark Fasheh 	mlog(0, "Cleanup inode %llu, sync = %d\n",
964b0697053SMark Fasheh 	     (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
965ccd979bdSMark Fasheh 	if (sync_data)
966ccd979bdSMark Fasheh 		write_inode_now(inode, 1);
967ccd979bdSMark Fasheh 	truncate_inode_pages(&inode->i_data, 0);
968ccd979bdSMark Fasheh }
969ccd979bdSMark Fasheh 
970066d92dcSAl Viro static void ocfs2_delete_inode(struct inode *inode)
971ccd979bdSMark Fasheh {
972ccd979bdSMark Fasheh 	int wipe, status;
973e4b963f1SJoel Becker 	sigset_t oldset;
974ccd979bdSMark Fasheh 	struct buffer_head *di_bh = NULL;
975ccd979bdSMark Fasheh 
976ef6b689bSTao Ma 	mlog(0, "(inode->i_ino = %lu)\n", inode->i_ino);
977ccd979bdSMark Fasheh 
978a90714c1SJan Kara 	/* When we fail in read_inode() we mark inode as bad. The second test
979a90714c1SJan Kara 	 * catches the case when inode allocation fails before allocating
980a90714c1SJan Kara 	 * a block for inode. */
981a90714c1SJan Kara 	if (is_bad_inode(inode) || !OCFS2_I(inode)->ip_blkno) {
982ccd979bdSMark Fasheh 		mlog(0, "Skipping delete of bad inode\n");
983ccd979bdSMark Fasheh 		goto bail;
984ccd979bdSMark Fasheh 	}
985ccd979bdSMark Fasheh 
986871a2931SChristoph Hellwig 	dquot_initialize(inode);
987907f4554SChristoph Hellwig 
988ccd979bdSMark Fasheh 	if (!ocfs2_inode_is_valid_to_delete(inode)) {
989ccd979bdSMark Fasheh 		/* It's probably not necessary to truncate_inode_pages
990ccd979bdSMark Fasheh 		 * here but we do it for safety anyway (it will most
991ccd979bdSMark Fasheh 		 * likely be a no-op anyway) */
992ccd979bdSMark Fasheh 		ocfs2_cleanup_delete_inode(inode, 0);
993ccd979bdSMark Fasheh 		goto bail;
994ccd979bdSMark Fasheh 	}
995ccd979bdSMark Fasheh 
996ccd979bdSMark Fasheh 	/* We want to block signals in delete_inode as the lock and
997ccd979bdSMark Fasheh 	 * messaging paths may return us -ERESTARTSYS. Which would
998ccd979bdSMark Fasheh 	 * cause us to exit early, resulting in inodes being orphaned
999ccd979bdSMark Fasheh 	 * forever. */
1000e4b963f1SJoel Becker 	ocfs2_block_signals(&oldset);
1001ccd979bdSMark Fasheh 
10026ca497a8Swengang wang 	/*
10036ca497a8Swengang wang 	 * Synchronize us against ocfs2_get_dentry. We take this in
10046ca497a8Swengang wang 	 * shared mode so that all nodes can still concurrently
10056ca497a8Swengang wang 	 * process deletes.
10066ca497a8Swengang wang 	 */
10076ca497a8Swengang wang 	status = ocfs2_nfs_sync_lock(OCFS2_SB(inode->i_sb), 0);
10086ca497a8Swengang wang 	if (status < 0) {
10096ca497a8Swengang wang 		mlog(ML_ERROR, "getting nfs sync lock(PR) failed %d\n", status);
10106ca497a8Swengang wang 		ocfs2_cleanup_delete_inode(inode, 0);
10116ca497a8Swengang wang 		goto bail_unblock;
10126ca497a8Swengang wang 	}
1013ccd979bdSMark Fasheh 	/* Lock down the inode. This gives us an up to date view of
1014ccd979bdSMark Fasheh 	 * it's metadata (for verification), and allows us to
101534d024f8SMark Fasheh 	 * serialize delete_inode on multiple nodes.
1016ccd979bdSMark Fasheh 	 *
1017ccd979bdSMark Fasheh 	 * Even though we might be doing a truncate, we don't take the
1018ccd979bdSMark Fasheh 	 * allocation lock here as it won't be needed - nobody will
1019ccd979bdSMark Fasheh 	 * have the file open.
1020ccd979bdSMark Fasheh 	 */
1021e63aecb6SMark Fasheh 	status = ocfs2_inode_lock(inode, &di_bh, 1);
1022ccd979bdSMark Fasheh 	if (status < 0) {
1023ccd979bdSMark Fasheh 		if (status != -ENOENT)
1024ccd979bdSMark Fasheh 			mlog_errno(status);
1025ccd979bdSMark Fasheh 		ocfs2_cleanup_delete_inode(inode, 0);
10266ca497a8Swengang wang 		goto bail_unlock_nfs_sync;
1027ccd979bdSMark Fasheh 	}
1028ccd979bdSMark Fasheh 
1029ccd979bdSMark Fasheh 	/* Query the cluster. This will be the final decision made
1030ccd979bdSMark Fasheh 	 * before we go ahead and wipe the inode. */
1031ccd979bdSMark Fasheh 	status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
1032ccd979bdSMark Fasheh 	if (!wipe || status < 0) {
103334d024f8SMark Fasheh 		/* Error and remote inode busy both mean we won't be
1034ccd979bdSMark Fasheh 		 * removing the inode, so they take almost the same
1035ccd979bdSMark Fasheh 		 * path. */
1036ccd979bdSMark Fasheh 		if (status < 0)
1037ccd979bdSMark Fasheh 			mlog_errno(status);
1038ccd979bdSMark Fasheh 
103934d024f8SMark Fasheh 		/* Someone in the cluster has disallowed a wipe of
104034d024f8SMark Fasheh 		 * this inode, or it was never completely
104134d024f8SMark Fasheh 		 * orphaned. Write out the pages and exit now. */
1042ccd979bdSMark Fasheh 		ocfs2_cleanup_delete_inode(inode, 1);
1043ccd979bdSMark Fasheh 		goto bail_unlock_inode;
1044ccd979bdSMark Fasheh 	}
1045ccd979bdSMark Fasheh 
1046ccd979bdSMark Fasheh 	ocfs2_cleanup_delete_inode(inode, 0);
1047ccd979bdSMark Fasheh 
1048ccd979bdSMark Fasheh 	status = ocfs2_wipe_inode(inode, di_bh);
1049ccd979bdSMark Fasheh 	if (status < 0) {
1050b4df6ed8SMark Fasheh 		if (status != -EDEADLK)
1051ccd979bdSMark Fasheh 			mlog_errno(status);
1052ccd979bdSMark Fasheh 		goto bail_unlock_inode;
1053ccd979bdSMark Fasheh 	}
1054ccd979bdSMark Fasheh 
105524c19ef4SMark Fasheh 	/*
105624c19ef4SMark Fasheh 	 * Mark the inode as successfully deleted.
105724c19ef4SMark Fasheh 	 *
105824c19ef4SMark Fasheh 	 * This is important for ocfs2_clear_inode() as it will check
105924c19ef4SMark Fasheh 	 * this flag and skip any checkpointing work
106024c19ef4SMark Fasheh 	 *
106124c19ef4SMark Fasheh 	 * ocfs2_stuff_meta_lvb() also uses this flag to invalidate
106224c19ef4SMark Fasheh 	 * the LVB for other nodes.
106324c19ef4SMark Fasheh 	 */
1064ccd979bdSMark Fasheh 	OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
1065ccd979bdSMark Fasheh 
1066ccd979bdSMark Fasheh bail_unlock_inode:
1067e63aecb6SMark Fasheh 	ocfs2_inode_unlock(inode, 1);
1068ccd979bdSMark Fasheh 	brelse(di_bh);
10696ca497a8Swengang wang 
10706ca497a8Swengang wang bail_unlock_nfs_sync:
10716ca497a8Swengang wang 	ocfs2_nfs_sync_unlock(OCFS2_SB(inode->i_sb), 0);
10726ca497a8Swengang wang 
1073ccd979bdSMark Fasheh bail_unblock:
1074e4b963f1SJoel Becker 	ocfs2_unblock_signals(&oldset);
1075ccd979bdSMark Fasheh bail:
1076c1e8d35eSTao Ma 	return;
1077ccd979bdSMark Fasheh }
1078ccd979bdSMark Fasheh 
1079066d92dcSAl Viro static void ocfs2_clear_inode(struct inode *inode)
1080ccd979bdSMark Fasheh {
1081ccd979bdSMark Fasheh 	int status;
1082ccd979bdSMark Fasheh 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1083ccd979bdSMark Fasheh 
1084066d92dcSAl Viro 	end_writeback(inode);
1085b0697053SMark Fasheh 	mlog(0, "Clearing inode: %llu, nlink = %u\n",
1086b0697053SMark Fasheh 	     (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_nlink);
1087ccd979bdSMark Fasheh 
1088ccd979bdSMark Fasheh 	mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
1089ccd979bdSMark Fasheh 			"Inode=%lu\n", inode->i_ino);
1090ccd979bdSMark Fasheh 
10919f754758SChristoph Hellwig 	dquot_drop(inode);
1092257ba15cSChristoph Hellwig 
109334d024f8SMark Fasheh 	/* To preven remote deletes we hold open lock before, now it
109434d024f8SMark Fasheh 	 * is time to unlock PR and EX open locks. */
109550008630STiger Yang 	ocfs2_open_unlock(inode);
109650008630STiger Yang 
1097ccd979bdSMark Fasheh 	/* Do these before all the other work so that we don't bounce
109834d024f8SMark Fasheh 	 * the downconvert thread while waiting to destroy the locks. */
1099ccd979bdSMark Fasheh 	ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres);
1100e63aecb6SMark Fasheh 	ocfs2_mark_lockres_freeing(&oi->ip_inode_lockres);
110150008630STiger Yang 	ocfs2_mark_lockres_freeing(&oi->ip_open_lockres);
1102ccd979bdSMark Fasheh 
11034fe370afSMark Fasheh 	ocfs2_resv_discard(&OCFS2_SB(inode->i_sb)->osb_la_resmap,
11044fe370afSMark Fasheh 			   &oi->ip_la_data_resv);
11054fe370afSMark Fasheh 	ocfs2_resv_init_once(&oi->ip_la_data_resv);
11064fe370afSMark Fasheh 
1107ccd979bdSMark Fasheh 	/* We very well may get a clear_inode before all an inodes
1108ccd979bdSMark Fasheh 	 * metadata has hit disk. Of course, we can't drop any cluster
1109ccd979bdSMark Fasheh 	 * locks until the journal has finished with it. The only
1110ccd979bdSMark Fasheh 	 * exception here are successfully wiped inodes - their
1111ccd979bdSMark Fasheh 	 * metadata can now be considered to be part of the system
1112ccd979bdSMark Fasheh 	 * inodes from which it came. */
1113ccd979bdSMark Fasheh 	if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED))
1114ccd979bdSMark Fasheh 		ocfs2_checkpoint_inode(inode);
1115ccd979bdSMark Fasheh 
1116ccd979bdSMark Fasheh 	mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
1117b0697053SMark Fasheh 			"Clear inode of %llu, inode has io markers\n",
1118b0697053SMark Fasheh 			(unsigned long long)oi->ip_blkno);
1119ccd979bdSMark Fasheh 
112083418978SMark Fasheh 	ocfs2_extent_map_trunc(inode, 0);
112183418978SMark Fasheh 
1122ccd979bdSMark Fasheh 	status = ocfs2_drop_inode_locks(inode);
1123ccd979bdSMark Fasheh 	if (status < 0)
1124ccd979bdSMark Fasheh 		mlog_errno(status);
1125ccd979bdSMark Fasheh 
1126ccd979bdSMark Fasheh 	ocfs2_lock_res_free(&oi->ip_rw_lockres);
1127e63aecb6SMark Fasheh 	ocfs2_lock_res_free(&oi->ip_inode_lockres);
112850008630STiger Yang 	ocfs2_lock_res_free(&oi->ip_open_lockres);
1129ccd979bdSMark Fasheh 
113066fb345dSJoel Becker 	ocfs2_metadata_cache_exit(INODE_CACHE(inode));
1131ccd979bdSMark Fasheh 
11328cb471e8SJoel Becker 	mlog_bug_on_msg(INODE_CACHE(inode)->ci_num_cached,
1133b0697053SMark Fasheh 			"Clear inode of %llu, inode has %u cache items\n",
11348cb471e8SJoel Becker 			(unsigned long long)oi->ip_blkno,
11358cb471e8SJoel Becker 			INODE_CACHE(inode)->ci_num_cached);
1136ccd979bdSMark Fasheh 
11378cb471e8SJoel Becker 	mlog_bug_on_msg(!(INODE_CACHE(inode)->ci_flags & OCFS2_CACHE_FL_INLINE),
1138b0697053SMark Fasheh 			"Clear inode of %llu, inode has a bad flag\n",
1139b0697053SMark Fasheh 			(unsigned long long)oi->ip_blkno);
1140ccd979bdSMark Fasheh 
1141ccd979bdSMark Fasheh 	mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
1142b0697053SMark Fasheh 			"Clear inode of %llu, inode is locked\n",
1143b0697053SMark Fasheh 			(unsigned long long)oi->ip_blkno);
1144ccd979bdSMark Fasheh 
1145251b6eccSMark Fasheh 	mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
1146b0697053SMark Fasheh 			"Clear inode of %llu, io_mutex is locked\n",
1147b0697053SMark Fasheh 			(unsigned long long)oi->ip_blkno);
1148251b6eccSMark Fasheh 	mutex_unlock(&oi->ip_io_mutex);
1149ccd979bdSMark Fasheh 
1150ccd979bdSMark Fasheh 	/*
1151ccd979bdSMark Fasheh 	 * down_trylock() returns 0, down_write_trylock() returns 1
1152ccd979bdSMark Fasheh 	 * kernel 1, world 0
1153ccd979bdSMark Fasheh 	 */
1154ccd979bdSMark Fasheh 	mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
1155b0697053SMark Fasheh 			"Clear inode of %llu, alloc_sem is locked\n",
1156b0697053SMark Fasheh 			(unsigned long long)oi->ip_blkno);
1157ccd979bdSMark Fasheh 	up_write(&oi->ip_alloc_sem);
1158ccd979bdSMark Fasheh 
1159ccd979bdSMark Fasheh 	mlog_bug_on_msg(oi->ip_open_count,
1160b0697053SMark Fasheh 			"Clear inode of %llu has open count %d\n",
1161b0697053SMark Fasheh 			(unsigned long long)oi->ip_blkno, oi->ip_open_count);
1162ccd979bdSMark Fasheh 
1163ccd979bdSMark Fasheh 	/* Clear all other flags. */
116447460d65SJoel Becker 	oi->ip_flags = 0;
1165ccd979bdSMark Fasheh 	oi->ip_dir_start_lookup = 0;
1166ccd979bdSMark Fasheh 	oi->ip_blkno = 0ULL;
1167ae0dff68SSunil Mushran 
1168ae0dff68SSunil Mushran 	/*
1169ae0dff68SSunil Mushran 	 * ip_jinode is used to track txns against this inode. We ensure that
1170ae0dff68SSunil Mushran 	 * the journal is flushed before journal shutdown. Thus it is safe to
1171ae0dff68SSunil Mushran 	 * have inodes get cleaned up after journal shutdown.
1172ae0dff68SSunil Mushran 	 */
11732b4e30fbSJoel Becker 	jbd2_journal_release_jbd_inode(OCFS2_SB(inode->i_sb)->journal->j_journal,
11742b4e30fbSJoel Becker 				       &oi->ip_jinode);
1175ccd979bdSMark Fasheh }
1176ccd979bdSMark Fasheh 
1177066d92dcSAl Viro void ocfs2_evict_inode(struct inode *inode)
1178066d92dcSAl Viro {
1179066d92dcSAl Viro 	if (!inode->i_nlink ||
1180066d92dcSAl Viro 	    (OCFS2_I(inode)->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)) {
1181066d92dcSAl Viro 		ocfs2_delete_inode(inode);
1182066d92dcSAl Viro 	} else {
1183066d92dcSAl Viro 		truncate_inode_pages(&inode->i_data, 0);
1184066d92dcSAl Viro 	}
1185066d92dcSAl Viro 	ocfs2_clear_inode(inode);
1186066d92dcSAl Viro }
1187066d92dcSAl Viro 
1188ccd979bdSMark Fasheh /* Called under inode_lock, with no more references on the
1189ccd979bdSMark Fasheh  * struct inode, so it's safe here to check the flags field
1190ccd979bdSMark Fasheh  * and to manipulate i_nlink without any other locks. */
119145321ac5SAl Viro int ocfs2_drop_inode(struct inode *inode)
1192ccd979bdSMark Fasheh {
1193ccd979bdSMark Fasheh 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
119445321ac5SAl Viro 	int res;
1195ccd979bdSMark Fasheh 
1196b0697053SMark Fasheh 	mlog(0, "Drop inode %llu, nlink = %u, ip_flags = 0x%x\n",
1197b0697053SMark Fasheh 	     (unsigned long long)oi->ip_blkno, inode->i_nlink, oi->ip_flags);
1198ccd979bdSMark Fasheh 
1199379dfe9dSMark Fasheh 	if (oi->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)
120045321ac5SAl Viro 		res = 1;
1201379dfe9dSMark Fasheh 	else
120245321ac5SAl Viro 		res = generic_drop_inode(inode);
1203ccd979bdSMark Fasheh 
120445321ac5SAl Viro 	return res;
1205ccd979bdSMark Fasheh }
1206ccd979bdSMark Fasheh 
1207ccd979bdSMark Fasheh /*
1208ccd979bdSMark Fasheh  * This is called from our getattr.
1209ccd979bdSMark Fasheh  */
1210ccd979bdSMark Fasheh int ocfs2_inode_revalidate(struct dentry *dentry)
1211ccd979bdSMark Fasheh {
1212ccd979bdSMark Fasheh 	struct inode *inode = dentry->d_inode;
1213ccd979bdSMark Fasheh 	int status = 0;
1214ccd979bdSMark Fasheh 
1215ef6b689bSTao Ma 	mlog(0, "(inode = 0x%p, ino = %llu)\n", inode,
1216b0697053SMark Fasheh 	     inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL);
1217ccd979bdSMark Fasheh 
1218ccd979bdSMark Fasheh 	if (!inode) {
1219ccd979bdSMark Fasheh 		mlog(0, "eep, no inode!\n");
1220ccd979bdSMark Fasheh 		status = -ENOENT;
1221ccd979bdSMark Fasheh 		goto bail;
1222ccd979bdSMark Fasheh 	}
1223ccd979bdSMark Fasheh 
1224ccd979bdSMark Fasheh 	spin_lock(&OCFS2_I(inode)->ip_lock);
1225ccd979bdSMark Fasheh 	if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
1226ccd979bdSMark Fasheh 		spin_unlock(&OCFS2_I(inode)->ip_lock);
1227ccd979bdSMark Fasheh 		mlog(0, "inode deleted!\n");
1228ccd979bdSMark Fasheh 		status = -ENOENT;
1229ccd979bdSMark Fasheh 		goto bail;
1230ccd979bdSMark Fasheh 	}
1231ccd979bdSMark Fasheh 	spin_unlock(&OCFS2_I(inode)->ip_lock);
1232ccd979bdSMark Fasheh 
1233e63aecb6SMark Fasheh 	/* Let ocfs2_inode_lock do the work of updating our struct
1234ccd979bdSMark Fasheh 	 * inode for us. */
1235e63aecb6SMark Fasheh 	status = ocfs2_inode_lock(inode, NULL, 0);
1236ccd979bdSMark Fasheh 	if (status < 0) {
1237ccd979bdSMark Fasheh 		if (status != -ENOENT)
1238ccd979bdSMark Fasheh 			mlog_errno(status);
1239ccd979bdSMark Fasheh 		goto bail;
1240ccd979bdSMark Fasheh 	}
1241e63aecb6SMark Fasheh 	ocfs2_inode_unlock(inode, 0);
1242ccd979bdSMark Fasheh bail:
1243ccd979bdSMark Fasheh 	return status;
1244ccd979bdSMark Fasheh }
1245ccd979bdSMark Fasheh 
1246ccd979bdSMark Fasheh /*
1247ccd979bdSMark Fasheh  * Updates a disk inode from a
1248ccd979bdSMark Fasheh  * struct inode.
1249ccd979bdSMark Fasheh  * Only takes ip_lock.
1250ccd979bdSMark Fasheh  */
12511fabe148SMark Fasheh int ocfs2_mark_inode_dirty(handle_t *handle,
1252ccd979bdSMark Fasheh 			   struct inode *inode,
1253ccd979bdSMark Fasheh 			   struct buffer_head *bh)
1254ccd979bdSMark Fasheh {
1255ccd979bdSMark Fasheh 	int status;
1256ccd979bdSMark Fasheh 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
1257ccd979bdSMark Fasheh 
1258ef6b689bSTao Ma 	mlog(0, "(inode %llu)\n",
1259b0697053SMark Fasheh 	     (unsigned long long)OCFS2_I(inode)->ip_blkno);
1260ccd979bdSMark Fasheh 
12610cf2f763SJoel Becker 	status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
1262ccd979bdSMark Fasheh 					 OCFS2_JOURNAL_ACCESS_WRITE);
1263ccd979bdSMark Fasheh 	if (status < 0) {
1264ccd979bdSMark Fasheh 		mlog_errno(status);
1265ccd979bdSMark Fasheh 		goto leave;
1266ccd979bdSMark Fasheh 	}
1267ccd979bdSMark Fasheh 
1268ccd979bdSMark Fasheh 	spin_lock(&OCFS2_I(inode)->ip_lock);
1269ccd979bdSMark Fasheh 	fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
12706e4b0d56SJan Kara 	ocfs2_get_inode_flags(OCFS2_I(inode));
1271ca4d147eSHerbert Poetzl 	fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
127215b1e36bSMark Fasheh 	fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
1273ccd979bdSMark Fasheh 	spin_unlock(&OCFS2_I(inode)->ip_lock);
1274ccd979bdSMark Fasheh 
1275ccd979bdSMark Fasheh 	fe->i_size = cpu_to_le64(i_size_read(inode));
1276198a1ca3SMark Fasheh 	ocfs2_set_links_count(fe, inode->i_nlink);
1277ccd979bdSMark Fasheh 	fe->i_uid = cpu_to_le32(inode->i_uid);
1278ccd979bdSMark Fasheh 	fe->i_gid = cpu_to_le32(inode->i_gid);
1279ccd979bdSMark Fasheh 	fe->i_mode = cpu_to_le16(inode->i_mode);
1280ccd979bdSMark Fasheh 	fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
1281ccd979bdSMark Fasheh 	fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
1282ccd979bdSMark Fasheh 	fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1283ccd979bdSMark Fasheh 	fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1284ccd979bdSMark Fasheh 	fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
1285ccd979bdSMark Fasheh 	fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
1286ccd979bdSMark Fasheh 
1287ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, bh);
1288ccd979bdSMark Fasheh leave:
1289ccd979bdSMark Fasheh 	return status;
1290ccd979bdSMark Fasheh }
1291ccd979bdSMark Fasheh 
1292ccd979bdSMark Fasheh /*
1293ccd979bdSMark Fasheh  *
1294ccd979bdSMark Fasheh  * Updates a struct inode from a disk inode.
1295ccd979bdSMark Fasheh  * does no i/o, only takes ip_lock.
1296ccd979bdSMark Fasheh  */
1297ccd979bdSMark Fasheh void ocfs2_refresh_inode(struct inode *inode,
1298ccd979bdSMark Fasheh 			 struct ocfs2_dinode *fe)
1299ccd979bdSMark Fasheh {
1300ccd979bdSMark Fasheh 	spin_lock(&OCFS2_I(inode)->ip_lock);
1301ccd979bdSMark Fasheh 
1302ccd979bdSMark Fasheh 	OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1303ca4d147eSHerbert Poetzl 	OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
130415b1e36bSMark Fasheh 	OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
1305ca4d147eSHerbert Poetzl 	ocfs2_set_inode_flags(inode);
1306ccd979bdSMark Fasheh 	i_size_write(inode, le64_to_cpu(fe->i_size));
1307198a1ca3SMark Fasheh 	inode->i_nlink = ocfs2_read_links_count(fe);
1308ccd979bdSMark Fasheh 	inode->i_uid = le32_to_cpu(fe->i_uid);
1309ccd979bdSMark Fasheh 	inode->i_gid = le32_to_cpu(fe->i_gid);
1310ccd979bdSMark Fasheh 	inode->i_mode = le16_to_cpu(fe->i_mode);
1311ccd979bdSMark Fasheh 	if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
1312ccd979bdSMark Fasheh 		inode->i_blocks = 0;
1313ccd979bdSMark Fasheh 	else
13148110b073SMark Fasheh 		inode->i_blocks = ocfs2_inode_sector_count(inode);
1315ccd979bdSMark Fasheh 	inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
1316ccd979bdSMark Fasheh 	inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
1317ccd979bdSMark Fasheh 	inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
1318ccd979bdSMark Fasheh 	inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
1319ccd979bdSMark Fasheh 	inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
1320ccd979bdSMark Fasheh 	inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
1321ccd979bdSMark Fasheh 
1322ccd979bdSMark Fasheh 	spin_unlock(&OCFS2_I(inode)->ip_lock);
1323ccd979bdSMark Fasheh }
1324b657c95cSJoel Becker 
1325b657c95cSJoel Becker int ocfs2_validate_inode_block(struct super_block *sb,
1326b657c95cSJoel Becker 			       struct buffer_head *bh)
1327b657c95cSJoel Becker {
1328d6b32bbbSJoel Becker 	int rc;
1329b657c95cSJoel Becker 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1330b657c95cSJoel Becker 
1331970e4936SJoel Becker 	mlog(0, "Validating dinode %llu\n",
1332970e4936SJoel Becker 	     (unsigned long long)bh->b_blocknr);
1333970e4936SJoel Becker 
1334b657c95cSJoel Becker 	BUG_ON(!buffer_uptodate(bh));
1335b657c95cSJoel Becker 
1336d6b32bbbSJoel Becker 	/*
1337d6b32bbbSJoel Becker 	 * If the ecc fails, we return the error but otherwise
1338d6b32bbbSJoel Becker 	 * leave the filesystem running.  We know any error is
1339d6b32bbbSJoel Becker 	 * local to this block.
1340d6b32bbbSJoel Becker 	 */
1341d6b32bbbSJoel Becker 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check);
134213723d00SJoel Becker 	if (rc) {
134313723d00SJoel Becker 		mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
134413723d00SJoel Becker 		     (unsigned long long)bh->b_blocknr);
1345d6b32bbbSJoel Becker 		goto bail;
134613723d00SJoel Becker 	}
1347d6b32bbbSJoel Becker 
1348d6b32bbbSJoel Becker 	/*
1349d6b32bbbSJoel Becker 	 * Errors after here are fatal.
1350d6b32bbbSJoel Becker 	 */
1351d6b32bbbSJoel Becker 
1352d6b32bbbSJoel Becker 	rc = -EINVAL;
1353d6b32bbbSJoel Becker 
1354b657c95cSJoel Becker 	if (!OCFS2_IS_VALID_DINODE(di)) {
1355b657c95cSJoel Becker 		ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s\n",
1356b657c95cSJoel Becker 			    (unsigned long long)bh->b_blocknr, 7,
1357b657c95cSJoel Becker 			    di->i_signature);
1358b657c95cSJoel Becker 		goto bail;
1359b657c95cSJoel Becker 	}
1360b657c95cSJoel Becker 
1361b657c95cSJoel Becker 	if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
1362b657c95cSJoel Becker 		ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n",
1363b657c95cSJoel Becker 			    (unsigned long long)bh->b_blocknr,
1364b657c95cSJoel Becker 			    (unsigned long long)le64_to_cpu(di->i_blkno));
1365b657c95cSJoel Becker 		goto bail;
1366b657c95cSJoel Becker 	}
1367b657c95cSJoel Becker 
1368b657c95cSJoel Becker 	if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
1369b657c95cSJoel Becker 		ocfs2_error(sb,
1370b657c95cSJoel Becker 			    "Invalid dinode #%llu: OCFS2_VALID_FL not set\n",
1371b657c95cSJoel Becker 			    (unsigned long long)bh->b_blocknr);
1372b657c95cSJoel Becker 		goto bail;
1373b657c95cSJoel Becker 	}
1374b657c95cSJoel Becker 
1375b657c95cSJoel Becker 	if (le32_to_cpu(di->i_fs_generation) !=
1376b657c95cSJoel Becker 	    OCFS2_SB(sb)->fs_generation) {
1377b657c95cSJoel Becker 		ocfs2_error(sb,
1378b657c95cSJoel Becker 			    "Invalid dinode #%llu: fs_generation is %u\n",
1379b657c95cSJoel Becker 			    (unsigned long long)bh->b_blocknr,
1380b657c95cSJoel Becker 			    le32_to_cpu(di->i_fs_generation));
1381b657c95cSJoel Becker 		goto bail;
1382b657c95cSJoel Becker 	}
1383b657c95cSJoel Becker 
1384b657c95cSJoel Becker 	rc = 0;
1385b657c95cSJoel Becker 
1386b657c95cSJoel Becker bail:
1387b657c95cSJoel Becker 	return rc;
1388b657c95cSJoel Becker }
1389b657c95cSJoel Becker 
1390b657c95cSJoel Becker int ocfs2_read_inode_block_full(struct inode *inode, struct buffer_head **bh,
1391b657c95cSJoel Becker 				int flags)
1392b657c95cSJoel Becker {
1393b657c95cSJoel Becker 	int rc;
1394b657c95cSJoel Becker 	struct buffer_head *tmp = *bh;
1395b657c95cSJoel Becker 
13968cb471e8SJoel Becker 	rc = ocfs2_read_blocks(INODE_CACHE(inode), OCFS2_I(inode)->ip_blkno,
13978cb471e8SJoel Becker 			       1, &tmp, flags, ocfs2_validate_inode_block);
1398b657c95cSJoel Becker 
1399b657c95cSJoel Becker 	/* If ocfs2_read_blocks() got us a new bh, pass it up. */
1400970e4936SJoel Becker 	if (!rc && !*bh)
1401b657c95cSJoel Becker 		*bh = tmp;
1402b657c95cSJoel Becker 
1403b657c95cSJoel Becker 	return rc;
1404b657c95cSJoel Becker }
1405b657c95cSJoel Becker 
1406b657c95cSJoel Becker int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh)
1407b657c95cSJoel Becker {
1408b657c95cSJoel Becker 	return ocfs2_read_inode_block_full(inode, bh, 0);
1409b657c95cSJoel Becker }
14106e5a3d75SJoel Becker 
14116e5a3d75SJoel Becker 
14126e5a3d75SJoel Becker static u64 ocfs2_inode_cache_owner(struct ocfs2_caching_info *ci)
14136e5a3d75SJoel Becker {
14146e5a3d75SJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
14156e5a3d75SJoel Becker 
14166e5a3d75SJoel Becker 	return oi->ip_blkno;
14176e5a3d75SJoel Becker }
14186e5a3d75SJoel Becker 
14198cb471e8SJoel Becker static struct super_block *ocfs2_inode_cache_get_super(struct ocfs2_caching_info *ci)
14208cb471e8SJoel Becker {
14218cb471e8SJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
14228cb471e8SJoel Becker 
14238cb471e8SJoel Becker 	return oi->vfs_inode.i_sb;
14248cb471e8SJoel Becker }
14258cb471e8SJoel Becker 
14266e5a3d75SJoel Becker static void ocfs2_inode_cache_lock(struct ocfs2_caching_info *ci)
14276e5a3d75SJoel Becker {
14286e5a3d75SJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
14296e5a3d75SJoel Becker 
14306e5a3d75SJoel Becker 	spin_lock(&oi->ip_lock);
14316e5a3d75SJoel Becker }
14326e5a3d75SJoel Becker 
14336e5a3d75SJoel Becker static void ocfs2_inode_cache_unlock(struct ocfs2_caching_info *ci)
14346e5a3d75SJoel Becker {
14356e5a3d75SJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
14366e5a3d75SJoel Becker 
14376e5a3d75SJoel Becker 	spin_unlock(&oi->ip_lock);
14386e5a3d75SJoel Becker }
14396e5a3d75SJoel Becker 
14406e5a3d75SJoel Becker static void ocfs2_inode_cache_io_lock(struct ocfs2_caching_info *ci)
14416e5a3d75SJoel Becker {
14426e5a3d75SJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
14436e5a3d75SJoel Becker 
14446e5a3d75SJoel Becker 	mutex_lock(&oi->ip_io_mutex);
14456e5a3d75SJoel Becker }
14466e5a3d75SJoel Becker 
14476e5a3d75SJoel Becker static void ocfs2_inode_cache_io_unlock(struct ocfs2_caching_info *ci)
14486e5a3d75SJoel Becker {
14496e5a3d75SJoel Becker 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
14506e5a3d75SJoel Becker 
14516e5a3d75SJoel Becker 	mutex_unlock(&oi->ip_io_mutex);
14526e5a3d75SJoel Becker }
14536e5a3d75SJoel Becker 
14546e5a3d75SJoel Becker const struct ocfs2_caching_operations ocfs2_inode_caching_ops = {
14556e5a3d75SJoel Becker 	.co_owner		= ocfs2_inode_cache_owner,
14568cb471e8SJoel Becker 	.co_get_super		= ocfs2_inode_cache_get_super,
14576e5a3d75SJoel Becker 	.co_cache_lock		= ocfs2_inode_cache_lock,
14586e5a3d75SJoel Becker 	.co_cache_unlock	= ocfs2_inode_cache_unlock,
14596e5a3d75SJoel Becker 	.co_io_lock		= ocfs2_inode_cache_io_lock,
14606e5a3d75SJoel Becker 	.co_io_unlock		= ocfs2_inode_cache_io_unlock,
14616e5a3d75SJoel Becker };
14626e5a3d75SJoel Becker 
1463