xref: /openbmc/linux/fs/ocfs2/inode.c (revision 8730046c)
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * inode.c
5  *
6  * vfs' aops, fops, dops and iops
7  *
8  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this program; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 021110-1307, USA.
24  */
25 
26 #include <linux/fs.h>
27 #include <linux/types.h>
28 #include <linux/highmem.h>
29 #include <linux/pagemap.h>
30 #include <linux/quotaops.h>
31 
32 #include <asm/byteorder.h>
33 
34 #include <cluster/masklog.h>
35 
36 #include "ocfs2.h"
37 
38 #include "alloc.h"
39 #include "dir.h"
40 #include "blockcheck.h"
41 #include "dlmglue.h"
42 #include "extent_map.h"
43 #include "file.h"
44 #include "heartbeat.h"
45 #include "inode.h"
46 #include "journal.h"
47 #include "namei.h"
48 #include "suballoc.h"
49 #include "super.h"
50 #include "symlink.h"
51 #include "sysfile.h"
52 #include "uptodate.h"
53 #include "xattr.h"
54 #include "refcounttree.h"
55 #include "ocfs2_trace.h"
56 #include "filecheck.h"
57 
58 #include "buffer_head_io.h"
59 
60 struct ocfs2_find_inode_args
61 {
62 	u64		fi_blkno;
63 	unsigned long	fi_ino;
64 	unsigned int	fi_flags;
65 	unsigned int	fi_sysfile_type;
66 };
67 
68 static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES];
69 
70 static int ocfs2_read_locked_inode(struct inode *inode,
71 				   struct ocfs2_find_inode_args *args);
72 static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
73 static int ocfs2_find_actor(struct inode *inode, void *opaque);
74 static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
75 				    struct inode *inode,
76 				    struct buffer_head *fe_bh);
77 
78 static int ocfs2_filecheck_read_inode_block_full(struct inode *inode,
79 						 struct buffer_head **bh,
80 						 int flags, int type);
81 static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
82 						struct buffer_head *bh);
83 static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
84 					      struct buffer_head *bh);
85 
86 void ocfs2_set_inode_flags(struct inode *inode)
87 {
88 	unsigned int flags = OCFS2_I(inode)->ip_attr;
89 
90 	inode->i_flags &= ~(S_IMMUTABLE |
91 		S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC);
92 
93 	if (flags & OCFS2_IMMUTABLE_FL)
94 		inode->i_flags |= S_IMMUTABLE;
95 
96 	if (flags & OCFS2_SYNC_FL)
97 		inode->i_flags |= S_SYNC;
98 	if (flags & OCFS2_APPEND_FL)
99 		inode->i_flags |= S_APPEND;
100 	if (flags & OCFS2_NOATIME_FL)
101 		inode->i_flags |= S_NOATIME;
102 	if (flags & OCFS2_DIRSYNC_FL)
103 		inode->i_flags |= S_DIRSYNC;
104 }
105 
106 /* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */
107 void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi)
108 {
109 	unsigned int flags = oi->vfs_inode.i_flags;
110 
111 	oi->ip_attr &= ~(OCFS2_SYNC_FL|OCFS2_APPEND_FL|
112 			OCFS2_IMMUTABLE_FL|OCFS2_NOATIME_FL|OCFS2_DIRSYNC_FL);
113 	if (flags & S_SYNC)
114 		oi->ip_attr |= OCFS2_SYNC_FL;
115 	if (flags & S_APPEND)
116 		oi->ip_attr |= OCFS2_APPEND_FL;
117 	if (flags & S_IMMUTABLE)
118 		oi->ip_attr |= OCFS2_IMMUTABLE_FL;
119 	if (flags & S_NOATIME)
120 		oi->ip_attr |= OCFS2_NOATIME_FL;
121 	if (flags & S_DIRSYNC)
122 		oi->ip_attr |= OCFS2_DIRSYNC_FL;
123 }
124 
125 struct inode *ocfs2_ilookup(struct super_block *sb, u64 blkno)
126 {
127 	struct ocfs2_find_inode_args args;
128 
129 	args.fi_blkno = blkno;
130 	args.fi_flags = 0;
131 	args.fi_ino = ino_from_blkno(sb, blkno);
132 	args.fi_sysfile_type = 0;
133 
134 	return ilookup5(sb, blkno, ocfs2_find_actor, &args);
135 }
136 struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,
137 			 int sysfile_type)
138 {
139 	int rc = 0;
140 	struct inode *inode = NULL;
141 	struct super_block *sb = osb->sb;
142 	struct ocfs2_find_inode_args args;
143 	journal_t *journal = OCFS2_SB(sb)->journal->j_journal;
144 
145 	trace_ocfs2_iget_begin((unsigned long long)blkno, flags,
146 			       sysfile_type);
147 
148 	/* Ok. By now we've either got the offsets passed to us by the
149 	 * caller, or we just pulled them off the bh. Lets do some
150 	 * sanity checks to make sure they're OK. */
151 	if (blkno == 0) {
152 		inode = ERR_PTR(-EINVAL);
153 		mlog_errno(PTR_ERR(inode));
154 		goto bail;
155 	}
156 
157 	args.fi_blkno = blkno;
158 	args.fi_flags = flags;
159 	args.fi_ino = ino_from_blkno(sb, blkno);
160 	args.fi_sysfile_type = sysfile_type;
161 
162 	inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor,
163 			     ocfs2_init_locked_inode, &args);
164 	/* inode was *not* in the inode cache. 2.6.x requires
165 	 * us to do our own read_inode call and unlock it
166 	 * afterwards. */
167 	if (inode == NULL) {
168 		inode = ERR_PTR(-ENOMEM);
169 		mlog_errno(PTR_ERR(inode));
170 		goto bail;
171 	}
172 	trace_ocfs2_iget5_locked(inode->i_state);
173 	if (inode->i_state & I_NEW) {
174 		rc = ocfs2_read_locked_inode(inode, &args);
175 		unlock_new_inode(inode);
176 	}
177 	if (is_bad_inode(inode)) {
178 		iput(inode);
179 		inode = ERR_PTR(rc);
180 		goto bail;
181 	}
182 
183 	/*
184 	 * Set transaction id's of transactions that have to be committed
185 	 * to finish f[data]sync. We set them to currently running transaction
186 	 * as we cannot be sure that the inode or some of its metadata isn't
187 	 * part of the transaction - the inode could have been reclaimed and
188 	 * now it is reread from disk.
189 	 */
190 	if (journal) {
191 		transaction_t *transaction;
192 		tid_t tid;
193 		struct ocfs2_inode_info *oi = OCFS2_I(inode);
194 
195 		read_lock(&journal->j_state_lock);
196 		if (journal->j_running_transaction)
197 			transaction = journal->j_running_transaction;
198 		else
199 			transaction = journal->j_committing_transaction;
200 		if (transaction)
201 			tid = transaction->t_tid;
202 		else
203 			tid = journal->j_commit_sequence;
204 		read_unlock(&journal->j_state_lock);
205 		oi->i_sync_tid = tid;
206 		oi->i_datasync_tid = tid;
207 	}
208 
209 bail:
210 	if (!IS_ERR(inode)) {
211 		trace_ocfs2_iget_end(inode,
212 			(unsigned long long)OCFS2_I(inode)->ip_blkno);
213 	}
214 
215 	return inode;
216 }
217 
218 
219 /*
220  * here's how inodes get read from disk:
221  * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR
222  * found? : return the in-memory inode
223  * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE
224  */
225 
226 static int ocfs2_find_actor(struct inode *inode, void *opaque)
227 {
228 	struct ocfs2_find_inode_args *args = NULL;
229 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
230 	int ret = 0;
231 
232 	args = opaque;
233 
234 	mlog_bug_on_msg(!inode, "No inode in find actor!\n");
235 
236 	trace_ocfs2_find_actor(inode, inode->i_ino, opaque, args->fi_blkno);
237 
238 	if (oi->ip_blkno != args->fi_blkno)
239 		goto bail;
240 
241 	ret = 1;
242 bail:
243 	return ret;
244 }
245 
246 /*
247  * initialize the new inode, but don't do anything that would cause
248  * us to sleep.
249  * return 0 on success, 1 on failure
250  */
251 static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
252 {
253 	struct ocfs2_find_inode_args *args = opaque;
254 	static struct lock_class_key ocfs2_quota_ip_alloc_sem_key,
255 				     ocfs2_file_ip_alloc_sem_key;
256 
257 	inode->i_ino = args->fi_ino;
258 	OCFS2_I(inode)->ip_blkno = args->fi_blkno;
259 	if (args->fi_sysfile_type != 0)
260 		lockdep_set_class(&inode->i_rwsem,
261 			&ocfs2_sysfile_lock_key[args->fi_sysfile_type]);
262 	if (args->fi_sysfile_type == USER_QUOTA_SYSTEM_INODE ||
263 	    args->fi_sysfile_type == GROUP_QUOTA_SYSTEM_INODE ||
264 	    args->fi_sysfile_type == LOCAL_USER_QUOTA_SYSTEM_INODE ||
265 	    args->fi_sysfile_type == LOCAL_GROUP_QUOTA_SYSTEM_INODE)
266 		lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem,
267 				  &ocfs2_quota_ip_alloc_sem_key);
268 	else
269 		lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem,
270 				  &ocfs2_file_ip_alloc_sem_key);
271 
272 	return 0;
273 }
274 
275 void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
276 			  int create_ino)
277 {
278 	struct super_block *sb;
279 	struct ocfs2_super *osb;
280 	int use_plocks = 1;
281 
282 	sb = inode->i_sb;
283 	osb = OCFS2_SB(sb);
284 
285 	if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
286 	    ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks())
287 		use_plocks = 0;
288 
289 	/*
290 	 * These have all been checked by ocfs2_read_inode_block() or set
291 	 * by ocfs2_mknod_locked(), so a failure is a code bug.
292 	 */
293 	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));  /* This means that read_inode
294 						cannot create a superblock
295 						inode today.  change if
296 						that is needed. */
297 	BUG_ON(!(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)));
298 	BUG_ON(le32_to_cpu(fe->i_fs_generation) != osb->fs_generation);
299 
300 
301 	OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
302 	OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
303 	OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
304 
305 	inode->i_version = 1;
306 	inode->i_generation = le32_to_cpu(fe->i_generation);
307 	inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
308 	inode->i_mode = le16_to_cpu(fe->i_mode);
309 	i_uid_write(inode, le32_to_cpu(fe->i_uid));
310 	i_gid_write(inode, le32_to_cpu(fe->i_gid));
311 
312 	/* Fast symlinks will have i_size but no allocated clusters. */
313 	if (S_ISLNK(inode->i_mode) && !fe->i_clusters) {
314 		inode->i_blocks = 0;
315 		inode->i_mapping->a_ops = &ocfs2_fast_symlink_aops;
316 	} else {
317 		inode->i_blocks = ocfs2_inode_sector_count(inode);
318 		inode->i_mapping->a_ops = &ocfs2_aops;
319 	}
320 	inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
321 	inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
322 	inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
323 	inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
324 	inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
325 	inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
326 
327 	if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno))
328 		mlog(ML_ERROR,
329 		     "ip_blkno %llu != i_blkno %llu!\n",
330 		     (unsigned long long)OCFS2_I(inode)->ip_blkno,
331 		     (unsigned long long)le64_to_cpu(fe->i_blkno));
332 
333 	set_nlink(inode, ocfs2_read_links_count(fe));
334 
335 	trace_ocfs2_populate_inode(OCFS2_I(inode)->ip_blkno,
336 				   le32_to_cpu(fe->i_flags));
337 	if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) {
338 		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
339 		inode->i_flags |= S_NOQUOTA;
340 	}
341 
342 	if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
343 		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
344 	} else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
345 		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
346 	} else if (fe->i_flags & cpu_to_le32(OCFS2_QUOTA_FL)) {
347 		inode->i_flags |= S_NOQUOTA;
348 	} else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
349 		/* we can't actually hit this as read_inode can't
350 		 * handle superblocks today ;-) */
351 		BUG();
352 	}
353 
354 	switch (inode->i_mode & S_IFMT) {
355 	    case S_IFREG:
356 		    if (use_plocks)
357 			    inode->i_fop = &ocfs2_fops;
358 		    else
359 			    inode->i_fop = &ocfs2_fops_no_plocks;
360 		    inode->i_op = &ocfs2_file_iops;
361 		    i_size_write(inode, le64_to_cpu(fe->i_size));
362 		    break;
363 	    case S_IFDIR:
364 		    inode->i_op = &ocfs2_dir_iops;
365 		    if (use_plocks)
366 			    inode->i_fop = &ocfs2_dops;
367 		    else
368 			    inode->i_fop = &ocfs2_dops_no_plocks;
369 		    i_size_write(inode, le64_to_cpu(fe->i_size));
370 		    OCFS2_I(inode)->ip_dir_lock_gen = 1;
371 		    break;
372 	    case S_IFLNK:
373 		    inode->i_op = &ocfs2_symlink_inode_operations;
374 		    inode_nohighmem(inode);
375 		    i_size_write(inode, le64_to_cpu(fe->i_size));
376 		    break;
377 	    default:
378 		    inode->i_op = &ocfs2_special_file_iops;
379 		    init_special_inode(inode, inode->i_mode,
380 				       inode->i_rdev);
381 		    break;
382 	}
383 
384 	if (create_ino) {
385 		inode->i_ino = ino_from_blkno(inode->i_sb,
386 			       le64_to_cpu(fe->i_blkno));
387 
388 		/*
389 		 * If we ever want to create system files from kernel,
390 		 * the generation argument to
391 		 * ocfs2_inode_lock_res_init() will have to change.
392 		 */
393 		BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL);
394 
395 		ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
396 					  OCFS2_LOCK_TYPE_META, 0, inode);
397 
398 		ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
399 					  OCFS2_LOCK_TYPE_OPEN, 0, inode);
400 	}
401 
402 	ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
403 				  OCFS2_LOCK_TYPE_RW, inode->i_generation,
404 				  inode);
405 
406 	ocfs2_set_inode_flags(inode);
407 
408 	OCFS2_I(inode)->ip_last_used_slot = 0;
409 	OCFS2_I(inode)->ip_last_used_group = 0;
410 
411 	if (S_ISDIR(inode->i_mode))
412 		ocfs2_resv_set_type(&OCFS2_I(inode)->ip_la_data_resv,
413 				    OCFS2_RESV_FLAG_DIR);
414 }
415 
416 static int ocfs2_read_locked_inode(struct inode *inode,
417 				   struct ocfs2_find_inode_args *args)
418 {
419 	struct super_block *sb;
420 	struct ocfs2_super *osb;
421 	struct ocfs2_dinode *fe;
422 	struct buffer_head *bh = NULL;
423 	int status, can_lock, lock_level = 0;
424 	u32 generation = 0;
425 
426 	status = -EINVAL;
427 	sb = inode->i_sb;
428 	osb = OCFS2_SB(sb);
429 
430 	/*
431 	 * To improve performance of cold-cache inode stats, we take
432 	 * the cluster lock here if possible.
433 	 *
434 	 * Generally, OCFS2 never trusts the contents of an inode
435 	 * unless it's holding a cluster lock, so taking it here isn't
436 	 * a correctness issue as much as it is a performance
437 	 * improvement.
438 	 *
439 	 * There are three times when taking the lock is not a good idea:
440 	 *
441 	 * 1) During startup, before we have initialized the DLM.
442 	 *
443 	 * 2) If we are reading certain system files which never get
444 	 *    cluster locks (local alloc, truncate log).
445 	 *
446 	 * 3) If the process doing the iget() is responsible for
447 	 *    orphan dir recovery. We're holding the orphan dir lock and
448 	 *    can get into a deadlock with another process on another
449 	 *    node in ->delete_inode().
450 	 *
451 	 * #1 and #2 can be simply solved by never taking the lock
452 	 * here for system files (which are the only type we read
453 	 * during mount). It's a heavier approach, but our main
454 	 * concern is user-accessible files anyway.
455 	 *
456 	 * #3 works itself out because we'll eventually take the
457 	 * cluster lock before trusting anything anyway.
458 	 */
459 	can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
460 		&& !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY)
461 		&& !ocfs2_mount_local(osb);
462 
463 	trace_ocfs2_read_locked_inode(
464 		(unsigned long long)OCFS2_I(inode)->ip_blkno, can_lock);
465 
466 	/*
467 	 * To maintain backwards compatibility with older versions of
468 	 * ocfs2-tools, we still store the generation value for system
469 	 * files. The only ones that actually matter to userspace are
470 	 * the journals, but it's easier and inexpensive to just flag
471 	 * all system files similarly.
472 	 */
473 	if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
474 		generation = osb->fs_generation;
475 
476 	ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
477 				  OCFS2_LOCK_TYPE_META,
478 				  generation, inode);
479 
480 	ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
481 				  OCFS2_LOCK_TYPE_OPEN,
482 				  0, inode);
483 
484 	if (can_lock) {
485 		status = ocfs2_open_lock(inode);
486 		if (status) {
487 			make_bad_inode(inode);
488 			mlog_errno(status);
489 			return status;
490 		}
491 		status = ocfs2_inode_lock(inode, NULL, lock_level);
492 		if (status) {
493 			make_bad_inode(inode);
494 			mlog_errno(status);
495 			return status;
496 		}
497 	}
498 
499 	if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) {
500 		status = ocfs2_try_open_lock(inode, 0);
501 		if (status) {
502 			make_bad_inode(inode);
503 			return status;
504 		}
505 	}
506 
507 	if (can_lock) {
508 		if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_CHK)
509 			status = ocfs2_filecheck_read_inode_block_full(inode,
510 						&bh, OCFS2_BH_IGNORE_CACHE, 0);
511 		else if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_FIX)
512 			status = ocfs2_filecheck_read_inode_block_full(inode,
513 						&bh, OCFS2_BH_IGNORE_CACHE, 1);
514 		else
515 			status = ocfs2_read_inode_block_full(inode,
516 						&bh, OCFS2_BH_IGNORE_CACHE);
517 	} else {
518 		status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh);
519 		/*
520 		 * If buffer is in jbd, then its checksum may not have been
521 		 * computed as yet.
522 		 */
523 		if (!status && !buffer_jbd(bh)) {
524 			if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_CHK)
525 				status = ocfs2_filecheck_validate_inode_block(
526 								osb->sb, bh);
527 			else if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_FIX)
528 				status = ocfs2_filecheck_repair_inode_block(
529 								osb->sb, bh);
530 			else
531 				status = ocfs2_validate_inode_block(
532 								osb->sb, bh);
533 		}
534 	}
535 	if (status < 0) {
536 		mlog_errno(status);
537 		goto bail;
538 	}
539 
540 	status = -EINVAL;
541 	fe = (struct ocfs2_dinode *) bh->b_data;
542 
543 	/*
544 	 * This is a code bug. Right now the caller needs to
545 	 * understand whether it is asking for a system file inode or
546 	 * not so the proper lock names can be built.
547 	 */
548 	mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) !=
549 			!!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE),
550 			"Inode %llu: system file state is ambigous\n",
551 			(unsigned long long)args->fi_blkno);
552 
553 	if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
554 	    S_ISBLK(le16_to_cpu(fe->i_mode)))
555 		inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
556 
557 	ocfs2_populate_inode(inode, fe, 0);
558 
559 	BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
560 
561 	if (buffer_dirty(bh) && !buffer_jbd(bh)) {
562 		if (can_lock) {
563 			ocfs2_inode_unlock(inode, lock_level);
564 			lock_level = 1;
565 			ocfs2_inode_lock(inode, NULL, lock_level);
566 		}
567 		status = ocfs2_write_block(osb, bh, INODE_CACHE(inode));
568 		if (status < 0) {
569 			mlog_errno(status);
570 			goto bail;
571 		}
572 	}
573 
574 	status = 0;
575 
576 bail:
577 	if (can_lock)
578 		ocfs2_inode_unlock(inode, lock_level);
579 
580 	if (status < 0)
581 		make_bad_inode(inode);
582 
583 	brelse(bh);
584 
585 	return status;
586 }
587 
588 void ocfs2_sync_blockdev(struct super_block *sb)
589 {
590 	sync_blockdev(sb->s_bdev);
591 }
592 
593 static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
594 				     struct inode *inode,
595 				     struct buffer_head *fe_bh)
596 {
597 	int status = 0;
598 	struct ocfs2_dinode *fe;
599 	handle_t *handle = NULL;
600 
601 	fe = (struct ocfs2_dinode *) fe_bh->b_data;
602 
603 	/*
604 	 * This check will also skip truncate of inodes with inline
605 	 * data and fast symlinks.
606 	 */
607 	if (fe->i_clusters) {
608 		if (ocfs2_should_order_data(inode))
609 			ocfs2_begin_ordered_truncate(inode, 0);
610 
611 		handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
612 		if (IS_ERR(handle)) {
613 			status = PTR_ERR(handle);
614 			handle = NULL;
615 			mlog_errno(status);
616 			goto out;
617 		}
618 
619 		status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
620 						 fe_bh,
621 						 OCFS2_JOURNAL_ACCESS_WRITE);
622 		if (status < 0) {
623 			mlog_errno(status);
624 			goto out;
625 		}
626 
627 		i_size_write(inode, 0);
628 
629 		status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
630 		if (status < 0) {
631 			mlog_errno(status);
632 			goto out;
633 		}
634 
635 		ocfs2_commit_trans(osb, handle);
636 		handle = NULL;
637 
638 		status = ocfs2_commit_truncate(osb, inode, fe_bh);
639 		if (status < 0) {
640 			mlog_errno(status);
641 			goto out;
642 		}
643 	}
644 
645 out:
646 	if (handle)
647 		ocfs2_commit_trans(osb, handle);
648 	return status;
649 }
650 
651 static int ocfs2_remove_inode(struct inode *inode,
652 			      struct buffer_head *di_bh,
653 			      struct inode *orphan_dir_inode,
654 			      struct buffer_head *orphan_dir_bh)
655 {
656 	int status;
657 	struct inode *inode_alloc_inode = NULL;
658 	struct buffer_head *inode_alloc_bh = NULL;
659 	handle_t *handle;
660 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
661 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
662 
663 	inode_alloc_inode =
664 		ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
665 					    le16_to_cpu(di->i_suballoc_slot));
666 	if (!inode_alloc_inode) {
667 		status = -ENOENT;
668 		mlog_errno(status);
669 		goto bail;
670 	}
671 
672 	inode_lock(inode_alloc_inode);
673 	status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1);
674 	if (status < 0) {
675 		inode_unlock(inode_alloc_inode);
676 
677 		mlog_errno(status);
678 		goto bail;
679 	}
680 
681 	handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS +
682 				   ocfs2_quota_trans_credits(inode->i_sb));
683 	if (IS_ERR(handle)) {
684 		status = PTR_ERR(handle);
685 		mlog_errno(status);
686 		goto bail_unlock;
687 	}
688 
689 	if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
690 		status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
691 					  orphan_dir_bh, false);
692 		if (status < 0) {
693 			mlog_errno(status);
694 			goto bail_commit;
695 		}
696 	}
697 
698 	/* set the inodes dtime */
699 	status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
700 					 OCFS2_JOURNAL_ACCESS_WRITE);
701 	if (status < 0) {
702 		mlog_errno(status);
703 		goto bail_commit;
704 	}
705 
706 	di->i_dtime = cpu_to_le64(ktime_get_real_seconds());
707 	di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
708 	ocfs2_journal_dirty(handle, di_bh);
709 
710 	ocfs2_remove_from_cache(INODE_CACHE(inode), di_bh);
711 	dquot_free_inode(inode);
712 
713 	status = ocfs2_free_dinode(handle, inode_alloc_inode,
714 				   inode_alloc_bh, di);
715 	if (status < 0)
716 		mlog_errno(status);
717 
718 bail_commit:
719 	ocfs2_commit_trans(osb, handle);
720 bail_unlock:
721 	ocfs2_inode_unlock(inode_alloc_inode, 1);
722 	inode_unlock(inode_alloc_inode);
723 	brelse(inode_alloc_bh);
724 bail:
725 	iput(inode_alloc_inode);
726 
727 	return status;
728 }
729 
730 /*
731  * Serialize with orphan dir recovery. If the process doing
732  * recovery on this orphan dir does an iget() with the dir
733  * i_mutex held, we'll deadlock here. Instead we detect this
734  * and exit early - recovery will wipe this inode for us.
735  */
736 static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
737 					     int slot)
738 {
739 	int ret = 0;
740 
741 	spin_lock(&osb->osb_lock);
742 	if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) {
743 		ret = -EDEADLK;
744 		goto out;
745 	}
746 	/* This signals to the orphan recovery process that it should
747 	 * wait for us to handle the wipe. */
748 	osb->osb_orphan_wipes[slot]++;
749 out:
750 	spin_unlock(&osb->osb_lock);
751 	trace_ocfs2_check_orphan_recovery_state(slot, ret);
752 	return ret;
753 }
754 
755 static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
756 					 int slot)
757 {
758 	spin_lock(&osb->osb_lock);
759 	osb->osb_orphan_wipes[slot]--;
760 	spin_unlock(&osb->osb_lock);
761 
762 	wake_up(&osb->osb_wipe_event);
763 }
764 
765 static int ocfs2_wipe_inode(struct inode *inode,
766 			    struct buffer_head *di_bh)
767 {
768 	int status, orphaned_slot = -1;
769 	struct inode *orphan_dir_inode = NULL;
770 	struct buffer_head *orphan_dir_bh = NULL;
771 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
772 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
773 
774 	if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
775 		orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
776 
777 		status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
778 		if (status)
779 			return status;
780 
781 		orphan_dir_inode = ocfs2_get_system_file_inode(osb,
782 							       ORPHAN_DIR_SYSTEM_INODE,
783 							       orphaned_slot);
784 		if (!orphan_dir_inode) {
785 			status = -ENOENT;
786 			mlog_errno(status);
787 			goto bail;
788 		}
789 
790 		/* Lock the orphan dir. The lock will be held for the entire
791 		 * delete_inode operation. We do this now to avoid races with
792 		 * recovery completion on other nodes. */
793 		inode_lock(orphan_dir_inode);
794 		status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
795 		if (status < 0) {
796 			inode_unlock(orphan_dir_inode);
797 
798 			mlog_errno(status);
799 			goto bail;
800 		}
801 	}
802 
803 	/* we do this while holding the orphan dir lock because we
804 	 * don't want recovery being run from another node to try an
805 	 * inode delete underneath us -- this will result in two nodes
806 	 * truncating the same file! */
807 	status = ocfs2_truncate_for_delete(osb, inode, di_bh);
808 	if (status < 0) {
809 		mlog_errno(status);
810 		goto bail_unlock_dir;
811 	}
812 
813 	/* Remove any dir index tree */
814 	if (S_ISDIR(inode->i_mode)) {
815 		status = ocfs2_dx_dir_truncate(inode, di_bh);
816 		if (status) {
817 			mlog_errno(status);
818 			goto bail_unlock_dir;
819 		}
820 	}
821 
822 	/*Free extended attribute resources associated with this inode.*/
823 	status = ocfs2_xattr_remove(inode, di_bh);
824 	if (status < 0) {
825 		mlog_errno(status);
826 		goto bail_unlock_dir;
827 	}
828 
829 	status = ocfs2_remove_refcount_tree(inode, di_bh);
830 	if (status < 0) {
831 		mlog_errno(status);
832 		goto bail_unlock_dir;
833 	}
834 
835 	status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
836 				    orphan_dir_bh);
837 	if (status < 0)
838 		mlog_errno(status);
839 
840 bail_unlock_dir:
841 	if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)
842 		return status;
843 
844 	ocfs2_inode_unlock(orphan_dir_inode, 1);
845 	inode_unlock(orphan_dir_inode);
846 	brelse(orphan_dir_bh);
847 bail:
848 	iput(orphan_dir_inode);
849 	ocfs2_signal_wipe_completion(osb, orphaned_slot);
850 
851 	return status;
852 }
853 
854 /* There is a series of simple checks that should be done before a
855  * trylock is even considered. Encapsulate those in this function. */
856 static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
857 {
858 	int ret = 0;
859 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
860 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
861 
862 	trace_ocfs2_inode_is_valid_to_delete(current, osb->dc_task,
863 					     (unsigned long long)oi->ip_blkno,
864 					     oi->ip_flags);
865 
866 	/* We shouldn't be getting here for the root directory
867 	 * inode.. */
868 	if (inode == osb->root_inode) {
869 		mlog(ML_ERROR, "Skipping delete of root inode.\n");
870 		goto bail;
871 	}
872 
873 	/*
874 	 * If we're coming from downconvert_thread we can't go into our own
875 	 * voting [hello, deadlock city!] so we cannot delete the inode. But
876 	 * since we dropped last inode ref when downconverting dentry lock,
877 	 * we cannot have the file open and thus the node doing unlink will
878 	 * take care of deleting the inode.
879 	 */
880 	if (current == osb->dc_task)
881 		goto bail;
882 
883 	spin_lock(&oi->ip_lock);
884 	/* OCFS2 *never* deletes system files. This should technically
885 	 * never get here as system file inodes should always have a
886 	 * positive link count. */
887 	if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
888 		mlog(ML_ERROR, "Skipping delete of system file %llu\n",
889 		     (unsigned long long)oi->ip_blkno);
890 		goto bail_unlock;
891 	}
892 
893 	ret = 1;
894 bail_unlock:
895 	spin_unlock(&oi->ip_lock);
896 bail:
897 	return ret;
898 }
899 
900 /* Query the cluster to determine whether we should wipe an inode from
901  * disk or not.
902  *
903  * Requires the inode to have the cluster lock. */
904 static int ocfs2_query_inode_wipe(struct inode *inode,
905 				  struct buffer_head *di_bh,
906 				  int *wipe)
907 {
908 	int status = 0, reason = 0;
909 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
910 	struct ocfs2_dinode *di;
911 
912 	*wipe = 0;
913 
914 	trace_ocfs2_query_inode_wipe_begin((unsigned long long)oi->ip_blkno,
915 					   inode->i_nlink);
916 
917 	/* While we were waiting for the cluster lock in
918 	 * ocfs2_delete_inode, another node might have asked to delete
919 	 * the inode. Recheck our flags to catch this. */
920 	if (!ocfs2_inode_is_valid_to_delete(inode)) {
921 		reason = 1;
922 		goto bail;
923 	}
924 
925 	/* Now that we have an up to date inode, we can double check
926 	 * the link count. */
927 	if (inode->i_nlink)
928 		goto bail;
929 
930 	/* Do some basic inode verification... */
931 	di = (struct ocfs2_dinode *) di_bh->b_data;
932 	if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL)) &&
933 	    !(oi->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
934 		/*
935 		 * Inodes in the orphan dir must have ORPHANED_FL.  The only
936 		 * inodes that come back out of the orphan dir are reflink
937 		 * targets. A reflink target may be moved out of the orphan
938 		 * dir between the time we scan the directory and the time we
939 		 * process it. This would lead to HAS_REFCOUNT_FL being set but
940 		 * ORPHANED_FL not.
941 		 */
942 		if (di->i_dyn_features & cpu_to_le16(OCFS2_HAS_REFCOUNT_FL)) {
943 			reason = 2;
944 			goto bail;
945 		}
946 
947 		/* for lack of a better error? */
948 		status = -EEXIST;
949 		mlog(ML_ERROR,
950 		     "Inode %llu (on-disk %llu) not orphaned! "
951 		     "Disk flags  0x%x, inode flags 0x%x\n",
952 		     (unsigned long long)oi->ip_blkno,
953 		     (unsigned long long)le64_to_cpu(di->i_blkno),
954 		     le32_to_cpu(di->i_flags), oi->ip_flags);
955 		goto bail;
956 	}
957 
958 	/* has someone already deleted us?! baaad... */
959 	if (di->i_dtime) {
960 		status = -EEXIST;
961 		mlog_errno(status);
962 		goto bail;
963 	}
964 
965 	/*
966 	 * This is how ocfs2 determines whether an inode is still live
967 	 * within the cluster. Every node takes a shared read lock on
968 	 * the inode open lock in ocfs2_read_locked_inode(). When we
969 	 * get to ->delete_inode(), each node tries to convert it's
970 	 * lock to an exclusive. Trylocks are serialized by the inode
971 	 * meta data lock. If the upconvert succeeds, we know the inode
972 	 * is no longer live and can be deleted.
973 	 *
974 	 * Though we call this with the meta data lock held, the
975 	 * trylock keeps us from ABBA deadlock.
976 	 */
977 	status = ocfs2_try_open_lock(inode, 1);
978 	if (status == -EAGAIN) {
979 		status = 0;
980 		reason = 3;
981 		goto bail;
982 	}
983 	if (status < 0) {
984 		mlog_errno(status);
985 		goto bail;
986 	}
987 
988 	*wipe = 1;
989 	trace_ocfs2_query_inode_wipe_succ(le16_to_cpu(di->i_orphaned_slot));
990 
991 bail:
992 	trace_ocfs2_query_inode_wipe_end(status, reason);
993 	return status;
994 }
995 
996 /* Support function for ocfs2_delete_inode. Will help us keep the
997  * inode data in a consistent state for clear_inode. Always truncates
998  * pages, optionally sync's them first. */
999 static void ocfs2_cleanup_delete_inode(struct inode *inode,
1000 				       int sync_data)
1001 {
1002 	trace_ocfs2_cleanup_delete_inode(
1003 		(unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
1004 	if (sync_data)
1005 		filemap_write_and_wait(inode->i_mapping);
1006 	truncate_inode_pages_final(&inode->i_data);
1007 }
1008 
1009 static void ocfs2_delete_inode(struct inode *inode)
1010 {
1011 	int wipe, status;
1012 	sigset_t oldset;
1013 	struct buffer_head *di_bh = NULL;
1014 	struct ocfs2_dinode *di = NULL;
1015 
1016 	trace_ocfs2_delete_inode(inode->i_ino,
1017 				 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1018 				 is_bad_inode(inode));
1019 
1020 	/* When we fail in read_inode() we mark inode as bad. The second test
1021 	 * catches the case when inode allocation fails before allocating
1022 	 * a block for inode. */
1023 	if (is_bad_inode(inode) || !OCFS2_I(inode)->ip_blkno)
1024 		goto bail;
1025 
1026 	if (!ocfs2_inode_is_valid_to_delete(inode)) {
1027 		/* It's probably not necessary to truncate_inode_pages
1028 		 * here but we do it for safety anyway (it will most
1029 		 * likely be a no-op anyway) */
1030 		ocfs2_cleanup_delete_inode(inode, 0);
1031 		goto bail;
1032 	}
1033 
1034 	dquot_initialize(inode);
1035 
1036 	/* We want to block signals in delete_inode as the lock and
1037 	 * messaging paths may return us -ERESTARTSYS. Which would
1038 	 * cause us to exit early, resulting in inodes being orphaned
1039 	 * forever. */
1040 	ocfs2_block_signals(&oldset);
1041 
1042 	/*
1043 	 * Synchronize us against ocfs2_get_dentry. We take this in
1044 	 * shared mode so that all nodes can still concurrently
1045 	 * process deletes.
1046 	 */
1047 	status = ocfs2_nfs_sync_lock(OCFS2_SB(inode->i_sb), 0);
1048 	if (status < 0) {
1049 		mlog(ML_ERROR, "getting nfs sync lock(PR) failed %d\n", status);
1050 		ocfs2_cleanup_delete_inode(inode, 0);
1051 		goto bail_unblock;
1052 	}
1053 	/* Lock down the inode. This gives us an up to date view of
1054 	 * it's metadata (for verification), and allows us to
1055 	 * serialize delete_inode on multiple nodes.
1056 	 *
1057 	 * Even though we might be doing a truncate, we don't take the
1058 	 * allocation lock here as it won't be needed - nobody will
1059 	 * have the file open.
1060 	 */
1061 	status = ocfs2_inode_lock(inode, &di_bh, 1);
1062 	if (status < 0) {
1063 		if (status != -ENOENT)
1064 			mlog_errno(status);
1065 		ocfs2_cleanup_delete_inode(inode, 0);
1066 		goto bail_unlock_nfs_sync;
1067 	}
1068 
1069 	di = (struct ocfs2_dinode *)di_bh->b_data;
1070 	/* Skip inode deletion and wait for dio orphan entry recovered
1071 	 * first */
1072 	if (unlikely(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))) {
1073 		ocfs2_cleanup_delete_inode(inode, 0);
1074 		goto bail_unlock_inode;
1075 	}
1076 
1077 	/* Query the cluster. This will be the final decision made
1078 	 * before we go ahead and wipe the inode. */
1079 	status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
1080 	if (!wipe || status < 0) {
1081 		/* Error and remote inode busy both mean we won't be
1082 		 * removing the inode, so they take almost the same
1083 		 * path. */
1084 		if (status < 0)
1085 			mlog_errno(status);
1086 
1087 		/* Someone in the cluster has disallowed a wipe of
1088 		 * this inode, or it was never completely
1089 		 * orphaned. Write out the pages and exit now. */
1090 		ocfs2_cleanup_delete_inode(inode, 1);
1091 		goto bail_unlock_inode;
1092 	}
1093 
1094 	ocfs2_cleanup_delete_inode(inode, 0);
1095 
1096 	status = ocfs2_wipe_inode(inode, di_bh);
1097 	if (status < 0) {
1098 		if (status != -EDEADLK)
1099 			mlog_errno(status);
1100 		goto bail_unlock_inode;
1101 	}
1102 
1103 	/*
1104 	 * Mark the inode as successfully deleted.
1105 	 *
1106 	 * This is important for ocfs2_clear_inode() as it will check
1107 	 * this flag and skip any checkpointing work
1108 	 *
1109 	 * ocfs2_stuff_meta_lvb() also uses this flag to invalidate
1110 	 * the LVB for other nodes.
1111 	 */
1112 	OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
1113 
1114 bail_unlock_inode:
1115 	ocfs2_inode_unlock(inode, 1);
1116 	brelse(di_bh);
1117 
1118 bail_unlock_nfs_sync:
1119 	ocfs2_nfs_sync_unlock(OCFS2_SB(inode->i_sb), 0);
1120 
1121 bail_unblock:
1122 	ocfs2_unblock_signals(&oldset);
1123 bail:
1124 	return;
1125 }
1126 
1127 static void ocfs2_clear_inode(struct inode *inode)
1128 {
1129 	int status;
1130 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1131 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1132 
1133 	clear_inode(inode);
1134 	trace_ocfs2_clear_inode((unsigned long long)oi->ip_blkno,
1135 				inode->i_nlink);
1136 
1137 	mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
1138 			"Inode=%lu\n", inode->i_ino);
1139 
1140 	dquot_drop(inode);
1141 
1142 	/* To preven remote deletes we hold open lock before, now it
1143 	 * is time to unlock PR and EX open locks. */
1144 	ocfs2_open_unlock(inode);
1145 
1146 	/* Do these before all the other work so that we don't bounce
1147 	 * the downconvert thread while waiting to destroy the locks. */
1148 	ocfs2_mark_lockres_freeing(osb, &oi->ip_rw_lockres);
1149 	ocfs2_mark_lockres_freeing(osb, &oi->ip_inode_lockres);
1150 	ocfs2_mark_lockres_freeing(osb, &oi->ip_open_lockres);
1151 
1152 	ocfs2_resv_discard(&OCFS2_SB(inode->i_sb)->osb_la_resmap,
1153 			   &oi->ip_la_data_resv);
1154 	ocfs2_resv_init_once(&oi->ip_la_data_resv);
1155 
1156 	/* We very well may get a clear_inode before all an inodes
1157 	 * metadata has hit disk. Of course, we can't drop any cluster
1158 	 * locks until the journal has finished with it. The only
1159 	 * exception here are successfully wiped inodes - their
1160 	 * metadata can now be considered to be part of the system
1161 	 * inodes from which it came. */
1162 	if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED))
1163 		ocfs2_checkpoint_inode(inode);
1164 
1165 	mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
1166 			"Clear inode of %llu, inode has io markers\n",
1167 			(unsigned long long)oi->ip_blkno);
1168 	mlog_bug_on_msg(!list_empty(&oi->ip_unwritten_list),
1169 			"Clear inode of %llu, inode has unwritten extents\n",
1170 			(unsigned long long)oi->ip_blkno);
1171 
1172 	ocfs2_extent_map_trunc(inode, 0);
1173 
1174 	status = ocfs2_drop_inode_locks(inode);
1175 	if (status < 0)
1176 		mlog_errno(status);
1177 
1178 	ocfs2_lock_res_free(&oi->ip_rw_lockres);
1179 	ocfs2_lock_res_free(&oi->ip_inode_lockres);
1180 	ocfs2_lock_res_free(&oi->ip_open_lockres);
1181 
1182 	ocfs2_metadata_cache_exit(INODE_CACHE(inode));
1183 
1184 	mlog_bug_on_msg(INODE_CACHE(inode)->ci_num_cached,
1185 			"Clear inode of %llu, inode has %u cache items\n",
1186 			(unsigned long long)oi->ip_blkno,
1187 			INODE_CACHE(inode)->ci_num_cached);
1188 
1189 	mlog_bug_on_msg(!(INODE_CACHE(inode)->ci_flags & OCFS2_CACHE_FL_INLINE),
1190 			"Clear inode of %llu, inode has a bad flag\n",
1191 			(unsigned long long)oi->ip_blkno);
1192 
1193 	mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
1194 			"Clear inode of %llu, inode is locked\n",
1195 			(unsigned long long)oi->ip_blkno);
1196 
1197 	mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
1198 			"Clear inode of %llu, io_mutex is locked\n",
1199 			(unsigned long long)oi->ip_blkno);
1200 	mutex_unlock(&oi->ip_io_mutex);
1201 
1202 	/*
1203 	 * down_trylock() returns 0, down_write_trylock() returns 1
1204 	 * kernel 1, world 0
1205 	 */
1206 	mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
1207 			"Clear inode of %llu, alloc_sem is locked\n",
1208 			(unsigned long long)oi->ip_blkno);
1209 	up_write(&oi->ip_alloc_sem);
1210 
1211 	mlog_bug_on_msg(oi->ip_open_count,
1212 			"Clear inode of %llu has open count %d\n",
1213 			(unsigned long long)oi->ip_blkno, oi->ip_open_count);
1214 
1215 	/* Clear all other flags. */
1216 	oi->ip_flags = 0;
1217 	oi->ip_dir_start_lookup = 0;
1218 	oi->ip_blkno = 0ULL;
1219 
1220 	/*
1221 	 * ip_jinode is used to track txns against this inode. We ensure that
1222 	 * the journal is flushed before journal shutdown. Thus it is safe to
1223 	 * have inodes get cleaned up after journal shutdown.
1224 	 */
1225 	jbd2_journal_release_jbd_inode(OCFS2_SB(inode->i_sb)->journal->j_journal,
1226 				       &oi->ip_jinode);
1227 }
1228 
1229 void ocfs2_evict_inode(struct inode *inode)
1230 {
1231 	if (!inode->i_nlink ||
1232 	    (OCFS2_I(inode)->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)) {
1233 		ocfs2_delete_inode(inode);
1234 	} else {
1235 		truncate_inode_pages_final(&inode->i_data);
1236 	}
1237 	ocfs2_clear_inode(inode);
1238 }
1239 
1240 /* Called under inode_lock, with no more references on the
1241  * struct inode, so it's safe here to check the flags field
1242  * and to manipulate i_nlink without any other locks. */
1243 int ocfs2_drop_inode(struct inode *inode)
1244 {
1245 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1246 
1247 	trace_ocfs2_drop_inode((unsigned long long)oi->ip_blkno,
1248 				inode->i_nlink, oi->ip_flags);
1249 
1250 	assert_spin_locked(&inode->i_lock);
1251 	inode->i_state |= I_WILL_FREE;
1252 	spin_unlock(&inode->i_lock);
1253 	write_inode_now(inode, 1);
1254 	spin_lock(&inode->i_lock);
1255 	WARN_ON(inode->i_state & I_NEW);
1256 	inode->i_state &= ~I_WILL_FREE;
1257 
1258 	return 1;
1259 }
1260 
1261 /*
1262  * This is called from our getattr.
1263  */
1264 int ocfs2_inode_revalidate(struct dentry *dentry)
1265 {
1266 	struct inode *inode = d_inode(dentry);
1267 	int status = 0;
1268 
1269 	trace_ocfs2_inode_revalidate(inode,
1270 		inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL,
1271 		inode ? (unsigned long long)OCFS2_I(inode)->ip_flags : 0);
1272 
1273 	if (!inode) {
1274 		status = -ENOENT;
1275 		goto bail;
1276 	}
1277 
1278 	spin_lock(&OCFS2_I(inode)->ip_lock);
1279 	if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
1280 		spin_unlock(&OCFS2_I(inode)->ip_lock);
1281 		status = -ENOENT;
1282 		goto bail;
1283 	}
1284 	spin_unlock(&OCFS2_I(inode)->ip_lock);
1285 
1286 	/* Let ocfs2_inode_lock do the work of updating our struct
1287 	 * inode for us. */
1288 	status = ocfs2_inode_lock(inode, NULL, 0);
1289 	if (status < 0) {
1290 		if (status != -ENOENT)
1291 			mlog_errno(status);
1292 		goto bail;
1293 	}
1294 	ocfs2_inode_unlock(inode, 0);
1295 bail:
1296 	return status;
1297 }
1298 
1299 /*
1300  * Updates a disk inode from a
1301  * struct inode.
1302  * Only takes ip_lock.
1303  */
1304 int ocfs2_mark_inode_dirty(handle_t *handle,
1305 			   struct inode *inode,
1306 			   struct buffer_head *bh)
1307 {
1308 	int status;
1309 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
1310 
1311 	trace_ocfs2_mark_inode_dirty((unsigned long long)OCFS2_I(inode)->ip_blkno);
1312 
1313 	status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
1314 					 OCFS2_JOURNAL_ACCESS_WRITE);
1315 	if (status < 0) {
1316 		mlog_errno(status);
1317 		goto leave;
1318 	}
1319 
1320 	spin_lock(&OCFS2_I(inode)->ip_lock);
1321 	fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
1322 	ocfs2_get_inode_flags(OCFS2_I(inode));
1323 	fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
1324 	fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
1325 	spin_unlock(&OCFS2_I(inode)->ip_lock);
1326 
1327 	fe->i_size = cpu_to_le64(i_size_read(inode));
1328 	ocfs2_set_links_count(fe, inode->i_nlink);
1329 	fe->i_uid = cpu_to_le32(i_uid_read(inode));
1330 	fe->i_gid = cpu_to_le32(i_gid_read(inode));
1331 	fe->i_mode = cpu_to_le16(inode->i_mode);
1332 	fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
1333 	fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
1334 	fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1335 	fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1336 	fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
1337 	fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
1338 
1339 	ocfs2_journal_dirty(handle, bh);
1340 	ocfs2_update_inode_fsync_trans(handle, inode, 1);
1341 leave:
1342 	return status;
1343 }
1344 
1345 /*
1346  *
1347  * Updates a struct inode from a disk inode.
1348  * does no i/o, only takes ip_lock.
1349  */
1350 void ocfs2_refresh_inode(struct inode *inode,
1351 			 struct ocfs2_dinode *fe)
1352 {
1353 	spin_lock(&OCFS2_I(inode)->ip_lock);
1354 
1355 	OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1356 	OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
1357 	OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
1358 	ocfs2_set_inode_flags(inode);
1359 	i_size_write(inode, le64_to_cpu(fe->i_size));
1360 	set_nlink(inode, ocfs2_read_links_count(fe));
1361 	i_uid_write(inode, le32_to_cpu(fe->i_uid));
1362 	i_gid_write(inode, le32_to_cpu(fe->i_gid));
1363 	inode->i_mode = le16_to_cpu(fe->i_mode);
1364 	if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
1365 		inode->i_blocks = 0;
1366 	else
1367 		inode->i_blocks = ocfs2_inode_sector_count(inode);
1368 	inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
1369 	inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
1370 	inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
1371 	inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
1372 	inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
1373 	inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
1374 
1375 	spin_unlock(&OCFS2_I(inode)->ip_lock);
1376 }
1377 
1378 int ocfs2_validate_inode_block(struct super_block *sb,
1379 			       struct buffer_head *bh)
1380 {
1381 	int rc;
1382 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1383 
1384 	trace_ocfs2_validate_inode_block((unsigned long long)bh->b_blocknr);
1385 
1386 	BUG_ON(!buffer_uptodate(bh));
1387 
1388 	/*
1389 	 * If the ecc fails, we return the error but otherwise
1390 	 * leave the filesystem running.  We know any error is
1391 	 * local to this block.
1392 	 */
1393 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check);
1394 	if (rc) {
1395 		mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
1396 		     (unsigned long long)bh->b_blocknr);
1397 		goto bail;
1398 	}
1399 
1400 	/*
1401 	 * Errors after here are fatal.
1402 	 */
1403 
1404 	rc = -EINVAL;
1405 
1406 	if (!OCFS2_IS_VALID_DINODE(di)) {
1407 		rc = ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s\n",
1408 				 (unsigned long long)bh->b_blocknr, 7,
1409 				 di->i_signature);
1410 		goto bail;
1411 	}
1412 
1413 	if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
1414 		rc = ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n",
1415 				 (unsigned long long)bh->b_blocknr,
1416 				 (unsigned long long)le64_to_cpu(di->i_blkno));
1417 		goto bail;
1418 	}
1419 
1420 	if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
1421 		rc = ocfs2_error(sb,
1422 				 "Invalid dinode #%llu: OCFS2_VALID_FL not set\n",
1423 				 (unsigned long long)bh->b_blocknr);
1424 		goto bail;
1425 	}
1426 
1427 	if (le32_to_cpu(di->i_fs_generation) !=
1428 	    OCFS2_SB(sb)->fs_generation) {
1429 		rc = ocfs2_error(sb,
1430 				 "Invalid dinode #%llu: fs_generation is %u\n",
1431 				 (unsigned long long)bh->b_blocknr,
1432 				 le32_to_cpu(di->i_fs_generation));
1433 		goto bail;
1434 	}
1435 
1436 	rc = 0;
1437 
1438 bail:
1439 	return rc;
1440 }
1441 
1442 static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
1443 						struct buffer_head *bh)
1444 {
1445 	int rc = 0;
1446 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1447 
1448 	trace_ocfs2_filecheck_validate_inode_block(
1449 		(unsigned long long)bh->b_blocknr);
1450 
1451 	BUG_ON(!buffer_uptodate(bh));
1452 
1453 	/*
1454 	 * Call ocfs2_validate_meta_ecc() first since it has ecc repair
1455 	 * function, but we should not return error immediately when ecc
1456 	 * validation fails, because the reason is quite likely the invalid
1457 	 * inode number inputed.
1458 	 */
1459 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check);
1460 	if (rc) {
1461 		mlog(ML_ERROR,
1462 		     "Filecheck: checksum failed for dinode %llu\n",
1463 		     (unsigned long long)bh->b_blocknr);
1464 		rc = -OCFS2_FILECHECK_ERR_BLOCKECC;
1465 	}
1466 
1467 	if (!OCFS2_IS_VALID_DINODE(di)) {
1468 		mlog(ML_ERROR,
1469 		     "Filecheck: invalid dinode #%llu: signature = %.*s\n",
1470 		     (unsigned long long)bh->b_blocknr, 7, di->i_signature);
1471 		rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
1472 		goto bail;
1473 	} else if (rc)
1474 		goto bail;
1475 
1476 	if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
1477 		mlog(ML_ERROR,
1478 		     "Filecheck: invalid dinode #%llu: i_blkno is %llu\n",
1479 		     (unsigned long long)bh->b_blocknr,
1480 		     (unsigned long long)le64_to_cpu(di->i_blkno));
1481 		rc = -OCFS2_FILECHECK_ERR_BLOCKNO;
1482 		goto bail;
1483 	}
1484 
1485 	if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
1486 		mlog(ML_ERROR,
1487 		     "Filecheck: invalid dinode #%llu: OCFS2_VALID_FL "
1488 		     "not set\n",
1489 		     (unsigned long long)bh->b_blocknr);
1490 		rc = -OCFS2_FILECHECK_ERR_VALIDFLAG;
1491 		goto bail;
1492 	}
1493 
1494 	if (le32_to_cpu(di->i_fs_generation) !=
1495 	    OCFS2_SB(sb)->fs_generation) {
1496 		mlog(ML_ERROR,
1497 		     "Filecheck: invalid dinode #%llu: fs_generation is %u\n",
1498 		     (unsigned long long)bh->b_blocknr,
1499 		     le32_to_cpu(di->i_fs_generation));
1500 		rc = -OCFS2_FILECHECK_ERR_GENERATION;
1501 		goto bail;
1502 	}
1503 
1504 bail:
1505 	return rc;
1506 }
1507 
1508 static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
1509 					      struct buffer_head *bh)
1510 {
1511 	int changed = 0;
1512 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1513 
1514 	if (!ocfs2_filecheck_validate_inode_block(sb, bh))
1515 		return 0;
1516 
1517 	trace_ocfs2_filecheck_repair_inode_block(
1518 		(unsigned long long)bh->b_blocknr);
1519 
1520 	if (ocfs2_is_hard_readonly(OCFS2_SB(sb)) ||
1521 	    ocfs2_is_soft_readonly(OCFS2_SB(sb))) {
1522 		mlog(ML_ERROR,
1523 		     "Filecheck: cannot repair dinode #%llu "
1524 		     "on readonly filesystem\n",
1525 		     (unsigned long long)bh->b_blocknr);
1526 		return -OCFS2_FILECHECK_ERR_READONLY;
1527 	}
1528 
1529 	if (buffer_jbd(bh)) {
1530 		mlog(ML_ERROR,
1531 		     "Filecheck: cannot repair dinode #%llu, "
1532 		     "its buffer is in jbd\n",
1533 		     (unsigned long long)bh->b_blocknr);
1534 		return -OCFS2_FILECHECK_ERR_INJBD;
1535 	}
1536 
1537 	if (!OCFS2_IS_VALID_DINODE(di)) {
1538 		/* Cannot fix invalid inode block */
1539 		return -OCFS2_FILECHECK_ERR_INVALIDINO;
1540 	}
1541 
1542 	if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
1543 		/* Cannot just add VALID_FL flag back as a fix,
1544 		 * need more things to check here.
1545 		 */
1546 		return -OCFS2_FILECHECK_ERR_VALIDFLAG;
1547 	}
1548 
1549 	if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
1550 		di->i_blkno = cpu_to_le64(bh->b_blocknr);
1551 		changed = 1;
1552 		mlog(ML_ERROR,
1553 		     "Filecheck: reset dinode #%llu: i_blkno to %llu\n",
1554 		     (unsigned long long)bh->b_blocknr,
1555 		     (unsigned long long)le64_to_cpu(di->i_blkno));
1556 	}
1557 
1558 	if (le32_to_cpu(di->i_fs_generation) !=
1559 	    OCFS2_SB(sb)->fs_generation) {
1560 		di->i_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
1561 		changed = 1;
1562 		mlog(ML_ERROR,
1563 		     "Filecheck: reset dinode #%llu: fs_generation to %u\n",
1564 		     (unsigned long long)bh->b_blocknr,
1565 		     le32_to_cpu(di->i_fs_generation));
1566 	}
1567 
1568 	if (changed || ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check)) {
1569 		ocfs2_compute_meta_ecc(sb, bh->b_data, &di->i_check);
1570 		mark_buffer_dirty(bh);
1571 		mlog(ML_ERROR,
1572 		     "Filecheck: reset dinode #%llu: compute meta ecc\n",
1573 		     (unsigned long long)bh->b_blocknr);
1574 	}
1575 
1576 	return 0;
1577 }
1578 
1579 static int
1580 ocfs2_filecheck_read_inode_block_full(struct inode *inode,
1581 				      struct buffer_head **bh,
1582 				      int flags, int type)
1583 {
1584 	int rc;
1585 	struct buffer_head *tmp = *bh;
1586 
1587 	if (!type) /* Check inode block */
1588 		rc = ocfs2_read_blocks(INODE_CACHE(inode),
1589 				OCFS2_I(inode)->ip_blkno,
1590 				1, &tmp, flags,
1591 				ocfs2_filecheck_validate_inode_block);
1592 	else /* Repair inode block */
1593 		rc = ocfs2_read_blocks(INODE_CACHE(inode),
1594 				OCFS2_I(inode)->ip_blkno,
1595 				1, &tmp, flags,
1596 				ocfs2_filecheck_repair_inode_block);
1597 
1598 	/* If ocfs2_read_blocks() got us a new bh, pass it up. */
1599 	if (!rc && !*bh)
1600 		*bh = tmp;
1601 
1602 	return rc;
1603 }
1604 
1605 int ocfs2_read_inode_block_full(struct inode *inode, struct buffer_head **bh,
1606 				int flags)
1607 {
1608 	int rc;
1609 	struct buffer_head *tmp = *bh;
1610 
1611 	rc = ocfs2_read_blocks(INODE_CACHE(inode), OCFS2_I(inode)->ip_blkno,
1612 			       1, &tmp, flags, ocfs2_validate_inode_block);
1613 
1614 	/* If ocfs2_read_blocks() got us a new bh, pass it up. */
1615 	if (!rc && !*bh)
1616 		*bh = tmp;
1617 
1618 	return rc;
1619 }
1620 
1621 int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh)
1622 {
1623 	return ocfs2_read_inode_block_full(inode, bh, 0);
1624 }
1625 
1626 
1627 static u64 ocfs2_inode_cache_owner(struct ocfs2_caching_info *ci)
1628 {
1629 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
1630 
1631 	return oi->ip_blkno;
1632 }
1633 
1634 static struct super_block *ocfs2_inode_cache_get_super(struct ocfs2_caching_info *ci)
1635 {
1636 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
1637 
1638 	return oi->vfs_inode.i_sb;
1639 }
1640 
1641 static void ocfs2_inode_cache_lock(struct ocfs2_caching_info *ci)
1642 {
1643 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
1644 
1645 	spin_lock(&oi->ip_lock);
1646 }
1647 
1648 static void ocfs2_inode_cache_unlock(struct ocfs2_caching_info *ci)
1649 {
1650 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
1651 
1652 	spin_unlock(&oi->ip_lock);
1653 }
1654 
1655 static void ocfs2_inode_cache_io_lock(struct ocfs2_caching_info *ci)
1656 {
1657 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
1658 
1659 	mutex_lock(&oi->ip_io_mutex);
1660 }
1661 
1662 static void ocfs2_inode_cache_io_unlock(struct ocfs2_caching_info *ci)
1663 {
1664 	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
1665 
1666 	mutex_unlock(&oi->ip_io_mutex);
1667 }
1668 
1669 const struct ocfs2_caching_operations ocfs2_inode_caching_ops = {
1670 	.co_owner		= ocfs2_inode_cache_owner,
1671 	.co_get_super		= ocfs2_inode_cache_get_super,
1672 	.co_cache_lock		= ocfs2_inode_cache_lock,
1673 	.co_cache_unlock	= ocfs2_inode_cache_unlock,
1674 	.co_io_lock		= ocfs2_inode_cache_io_lock,
1675 	.co_io_unlock		= ocfs2_inode_cache_io_unlock,
1676 };
1677 
1678