xref: /openbmc/linux/fs/ocfs2/super.c (revision c10d12e3)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * super.c
4  *
5  * load/unload driver, mount/dismount volumes
6  *
7  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
8  */
9 
10 #include <linux/module.h>
11 #include <linux/fs.h>
12 #include <linux/types.h>
13 #include <linux/slab.h>
14 #include <linux/highmem.h>
15 #include <linux/init.h>
16 #include <linux/random.h>
17 #include <linux/statfs.h>
18 #include <linux/moduleparam.h>
19 #include <linux/blkdev.h>
20 #include <linux/socket.h>
21 #include <linux/inet.h>
22 #include <linux/parser.h>
23 #include <linux/crc32.h>
24 #include <linux/debugfs.h>
25 #include <linux/mount.h>
26 #include <linux/seq_file.h>
27 #include <linux/quotaops.h>
28 #include <linux/signal.h>
29 
30 #define CREATE_TRACE_POINTS
31 #include "ocfs2_trace.h"
32 
33 #include <cluster/masklog.h>
34 
35 #include "ocfs2.h"
36 
37 /* this should be the only file to include a version 1 header */
38 #include "ocfs1_fs_compat.h"
39 
40 #include "alloc.h"
41 #include "aops.h"
42 #include "blockcheck.h"
43 #include "dlmglue.h"
44 #include "export.h"
45 #include "extent_map.h"
46 #include "heartbeat.h"
47 #include "inode.h"
48 #include "journal.h"
49 #include "localalloc.h"
50 #include "namei.h"
51 #include "slot_map.h"
52 #include "super.h"
53 #include "sysfile.h"
54 #include "uptodate.h"
55 #include "xattr.h"
56 #include "quota.h"
57 #include "refcounttree.h"
58 #include "suballoc.h"
59 
60 #include "buffer_head_io.h"
61 #include "filecheck.h"
62 
63 static struct kmem_cache *ocfs2_inode_cachep;
64 struct kmem_cache *ocfs2_dquot_cachep;
65 struct kmem_cache *ocfs2_qf_chunk_cachep;
66 
67 static struct dentry *ocfs2_debugfs_root;
68 
69 MODULE_AUTHOR("Oracle");
70 MODULE_LICENSE("GPL");
71 MODULE_DESCRIPTION("OCFS2 cluster file system");
72 
73 struct mount_options
74 {
75 	unsigned long	commit_interval;
76 	unsigned long	mount_opt;
77 	unsigned int	atime_quantum;
78 	unsigned short	slot;
79 	int		localalloc_opt;
80 	unsigned int	resv_level;
81 	int		dir_resv_level;
82 	char		cluster_stack[OCFS2_STACK_LABEL_LEN + 1];
83 };
84 
85 static int ocfs2_parse_options(struct super_block *sb, char *options,
86 			       struct mount_options *mopt,
87 			       int is_remount);
88 static int ocfs2_check_set_options(struct super_block *sb,
89 				   struct mount_options *options);
90 static int ocfs2_show_options(struct seq_file *s, struct dentry *root);
91 static void ocfs2_put_super(struct super_block *sb);
92 static int ocfs2_mount_volume(struct super_block *sb);
93 static int ocfs2_remount(struct super_block *sb, int *flags, char *data);
94 static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err);
95 static int ocfs2_initialize_mem_caches(void);
96 static void ocfs2_free_mem_caches(void);
97 static void ocfs2_delete_osb(struct ocfs2_super *osb);
98 
99 static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf);
100 
101 static int ocfs2_sync_fs(struct super_block *sb, int wait);
102 
103 static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb);
104 static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb);
105 static void ocfs2_release_system_inodes(struct ocfs2_super *osb);
106 static int ocfs2_check_volume(struct ocfs2_super *osb);
107 static int ocfs2_verify_volume(struct ocfs2_dinode *di,
108 			       struct buffer_head *bh,
109 			       u32 sectsize,
110 			       struct ocfs2_blockcheck_stats *stats);
111 static int ocfs2_initialize_super(struct super_block *sb,
112 				  struct buffer_head *bh,
113 				  int sector_size,
114 				  struct ocfs2_blockcheck_stats *stats);
115 static int ocfs2_get_sector(struct super_block *sb,
116 			    struct buffer_head **bh,
117 			    int block,
118 			    int sect_size);
119 static struct inode *ocfs2_alloc_inode(struct super_block *sb);
120 static void ocfs2_free_inode(struct inode *inode);
121 static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend);
122 static int ocfs2_enable_quotas(struct ocfs2_super *osb);
123 static void ocfs2_disable_quotas(struct ocfs2_super *osb);
124 
125 static struct dquot **ocfs2_get_dquots(struct inode *inode)
126 {
127 	return OCFS2_I(inode)->i_dquot;
128 }
129 
130 static const struct super_operations ocfs2_sops = {
131 	.statfs		= ocfs2_statfs,
132 	.alloc_inode	= ocfs2_alloc_inode,
133 	.free_inode	= ocfs2_free_inode,
134 	.drop_inode	= ocfs2_drop_inode,
135 	.evict_inode	= ocfs2_evict_inode,
136 	.sync_fs	= ocfs2_sync_fs,
137 	.put_super	= ocfs2_put_super,
138 	.remount_fs	= ocfs2_remount,
139 	.show_options   = ocfs2_show_options,
140 	.quota_read	= ocfs2_quota_read,
141 	.quota_write	= ocfs2_quota_write,
142 	.get_dquots	= ocfs2_get_dquots,
143 };
144 
145 enum {
146 	Opt_barrier,
147 	Opt_err_panic,
148 	Opt_err_ro,
149 	Opt_intr,
150 	Opt_nointr,
151 	Opt_hb_none,
152 	Opt_hb_local,
153 	Opt_hb_global,
154 	Opt_data_ordered,
155 	Opt_data_writeback,
156 	Opt_atime_quantum,
157 	Opt_slot,
158 	Opt_commit,
159 	Opt_localalloc,
160 	Opt_localflocks,
161 	Opt_stack,
162 	Opt_user_xattr,
163 	Opt_nouser_xattr,
164 	Opt_inode64,
165 	Opt_acl,
166 	Opt_noacl,
167 	Opt_usrquota,
168 	Opt_grpquota,
169 	Opt_coherency_buffered,
170 	Opt_coherency_full,
171 	Opt_resv_level,
172 	Opt_dir_resv_level,
173 	Opt_journal_async_commit,
174 	Opt_err_cont,
175 	Opt_nocluster,
176 	Opt_err,
177 };
178 
179 static const match_table_t tokens = {
180 	{Opt_barrier, "barrier=%u"},
181 	{Opt_err_panic, "errors=panic"},
182 	{Opt_err_ro, "errors=remount-ro"},
183 	{Opt_intr, "intr"},
184 	{Opt_nointr, "nointr"},
185 	{Opt_hb_none, OCFS2_HB_NONE},
186 	{Opt_hb_local, OCFS2_HB_LOCAL},
187 	{Opt_hb_global, OCFS2_HB_GLOBAL},
188 	{Opt_data_ordered, "data=ordered"},
189 	{Opt_data_writeback, "data=writeback"},
190 	{Opt_atime_quantum, "atime_quantum=%u"},
191 	{Opt_slot, "preferred_slot=%u"},
192 	{Opt_commit, "commit=%u"},
193 	{Opt_localalloc, "localalloc=%d"},
194 	{Opt_localflocks, "localflocks"},
195 	{Opt_stack, "cluster_stack=%s"},
196 	{Opt_user_xattr, "user_xattr"},
197 	{Opt_nouser_xattr, "nouser_xattr"},
198 	{Opt_inode64, "inode64"},
199 	{Opt_acl, "acl"},
200 	{Opt_noacl, "noacl"},
201 	{Opt_usrquota, "usrquota"},
202 	{Opt_grpquota, "grpquota"},
203 	{Opt_coherency_buffered, "coherency=buffered"},
204 	{Opt_coherency_full, "coherency=full"},
205 	{Opt_resv_level, "resv_level=%u"},
206 	{Opt_dir_resv_level, "dir_resv_level=%u"},
207 	{Opt_journal_async_commit, "journal_async_commit"},
208 	{Opt_err_cont, "errors=continue"},
209 	{Opt_nocluster, "nocluster"},
210 	{Opt_err, NULL}
211 };
212 
213 #ifdef CONFIG_DEBUG_FS
214 static int ocfs2_osb_dump(struct ocfs2_super *osb, char *buf, int len)
215 {
216 	struct ocfs2_cluster_connection *cconn = osb->cconn;
217 	struct ocfs2_recovery_map *rm = osb->recovery_map;
218 	struct ocfs2_orphan_scan *os = &osb->osb_orphan_scan;
219 	int i, out = 0;
220 	unsigned long flags;
221 
222 	out += scnprintf(buf + out, len - out,
223 			"%10s => Id: %-s  Uuid: %-s  Gen: 0x%X  Label: %-s\n",
224 			"Device", osb->dev_str, osb->uuid_str,
225 			osb->fs_generation, osb->vol_label);
226 
227 	out += scnprintf(buf + out, len - out,
228 			"%10s => State: %d  Flags: 0x%lX\n", "Volume",
229 			atomic_read(&osb->vol_state), osb->osb_flags);
230 
231 	out += scnprintf(buf + out, len - out,
232 			"%10s => Block: %lu  Cluster: %d\n", "Sizes",
233 			osb->sb->s_blocksize, osb->s_clustersize);
234 
235 	out += scnprintf(buf + out, len - out,
236 			"%10s => Compat: 0x%X  Incompat: 0x%X  "
237 			"ROcompat: 0x%X\n",
238 			"Features", osb->s_feature_compat,
239 			osb->s_feature_incompat, osb->s_feature_ro_compat);
240 
241 	out += scnprintf(buf + out, len - out,
242 			"%10s => Opts: 0x%lX  AtimeQuanta: %u\n", "Mount",
243 			osb->s_mount_opt, osb->s_atime_quantum);
244 
245 	if (cconn) {
246 		out += scnprintf(buf + out, len - out,
247 				"%10s => Stack: %s  Name: %*s  "
248 				"Version: %d.%d\n", "Cluster",
249 				(*osb->osb_cluster_stack == '\0' ?
250 				 "o2cb" : osb->osb_cluster_stack),
251 				cconn->cc_namelen, cconn->cc_name,
252 				cconn->cc_version.pv_major,
253 				cconn->cc_version.pv_minor);
254 	}
255 
256 	spin_lock_irqsave(&osb->dc_task_lock, flags);
257 	out += scnprintf(buf + out, len - out,
258 			"%10s => Pid: %d  Count: %lu  WakeSeq: %lu  "
259 			"WorkSeq: %lu\n", "DownCnvt",
260 			(osb->dc_task ?  task_pid_nr(osb->dc_task) : -1),
261 			osb->blocked_lock_count, osb->dc_wake_sequence,
262 			osb->dc_work_sequence);
263 	spin_unlock_irqrestore(&osb->dc_task_lock, flags);
264 
265 	spin_lock(&osb->osb_lock);
266 	out += scnprintf(buf + out, len - out, "%10s => Pid: %d  Nodes:",
267 			"Recovery",
268 			(osb->recovery_thread_task ?
269 			 task_pid_nr(osb->recovery_thread_task) : -1));
270 	if (rm->rm_used == 0)
271 		out += scnprintf(buf + out, len - out, " None\n");
272 	else {
273 		for (i = 0; i < rm->rm_used; i++)
274 			out += scnprintf(buf + out, len - out, " %d",
275 					rm->rm_entries[i]);
276 		out += scnprintf(buf + out, len - out, "\n");
277 	}
278 	spin_unlock(&osb->osb_lock);
279 
280 	out += scnprintf(buf + out, len - out,
281 			"%10s => Pid: %d  Interval: %lu\n", "Commit",
282 			(osb->commit_task ? task_pid_nr(osb->commit_task) : -1),
283 			osb->osb_commit_interval);
284 
285 	out += scnprintf(buf + out, len - out,
286 			"%10s => State: %d  TxnId: %lu  NumTxns: %d\n",
287 			"Journal", osb->journal->j_state,
288 			osb->journal->j_trans_id,
289 			atomic_read(&osb->journal->j_num_trans));
290 
291 	out += scnprintf(buf + out, len - out,
292 			"%10s => GlobalAllocs: %d  LocalAllocs: %d  "
293 			"SubAllocs: %d  LAWinMoves: %d  SAExtends: %d\n",
294 			"Stats",
295 			atomic_read(&osb->alloc_stats.bitmap_data),
296 			atomic_read(&osb->alloc_stats.local_data),
297 			atomic_read(&osb->alloc_stats.bg_allocs),
298 			atomic_read(&osb->alloc_stats.moves),
299 			atomic_read(&osb->alloc_stats.bg_extends));
300 
301 	out += scnprintf(buf + out, len - out,
302 			"%10s => State: %u  Descriptor: %llu  Size: %u bits  "
303 			"Default: %u bits\n",
304 			"LocalAlloc", osb->local_alloc_state,
305 			(unsigned long long)osb->la_last_gd,
306 			osb->local_alloc_bits, osb->local_alloc_default_bits);
307 
308 	spin_lock(&osb->osb_lock);
309 	out += scnprintf(buf + out, len - out,
310 			"%10s => InodeSlot: %d  StolenInodes: %d, "
311 			"MetaSlot: %d  StolenMeta: %d\n", "Steal",
312 			osb->s_inode_steal_slot,
313 			atomic_read(&osb->s_num_inodes_stolen),
314 			osb->s_meta_steal_slot,
315 			atomic_read(&osb->s_num_meta_stolen));
316 	spin_unlock(&osb->osb_lock);
317 
318 	out += scnprintf(buf + out, len - out, "OrphanScan => ");
319 	out += scnprintf(buf + out, len - out, "Local: %u  Global: %u ",
320 			os->os_count, os->os_seqno);
321 	out += scnprintf(buf + out, len - out, " Last Scan: ");
322 	if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE)
323 		out += scnprintf(buf + out, len - out, "Disabled\n");
324 	else
325 		out += scnprintf(buf + out, len - out, "%lu seconds ago\n",
326 				(unsigned long)(ktime_get_seconds() - os->os_scantime));
327 
328 	out += scnprintf(buf + out, len - out, "%10s => %3s  %10s\n",
329 			"Slots", "Num", "RecoGen");
330 	for (i = 0; i < osb->max_slots; ++i) {
331 		out += scnprintf(buf + out, len - out,
332 				"%10s  %c %3d  %10d\n",
333 				" ",
334 				(i == osb->slot_num ? '*' : ' '),
335 				i, osb->slot_recovery_generations[i]);
336 	}
337 
338 	return out;
339 }
340 
341 static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
342 {
343 	struct ocfs2_super *osb = inode->i_private;
344 	char *buf = NULL;
345 
346 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
347 	if (!buf)
348 		goto bail;
349 
350 	i_size_write(inode, ocfs2_osb_dump(osb, buf, PAGE_SIZE));
351 
352 	file->private_data = buf;
353 
354 	return 0;
355 bail:
356 	return -ENOMEM;
357 }
358 
359 static int ocfs2_debug_release(struct inode *inode, struct file *file)
360 {
361 	kfree(file->private_data);
362 	return 0;
363 }
364 
365 static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
366 				size_t nbytes, loff_t *ppos)
367 {
368 	return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
369 				       i_size_read(file->f_mapping->host));
370 }
371 #else
372 static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
373 {
374 	return 0;
375 }
376 static int ocfs2_debug_release(struct inode *inode, struct file *file)
377 {
378 	return 0;
379 }
380 static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
381 				size_t nbytes, loff_t *ppos)
382 {
383 	return 0;
384 }
385 #endif	/* CONFIG_DEBUG_FS */
386 
387 static const struct file_operations ocfs2_osb_debug_fops = {
388 	.open =		ocfs2_osb_debug_open,
389 	.release =	ocfs2_debug_release,
390 	.read =		ocfs2_debug_read,
391 	.llseek =	generic_file_llseek,
392 };
393 
394 static int ocfs2_sync_fs(struct super_block *sb, int wait)
395 {
396 	int status;
397 	tid_t target;
398 	struct ocfs2_super *osb = OCFS2_SB(sb);
399 
400 	if (ocfs2_is_hard_readonly(osb))
401 		return -EROFS;
402 
403 	if (wait) {
404 		status = ocfs2_flush_truncate_log(osb);
405 		if (status < 0)
406 			mlog_errno(status);
407 	} else {
408 		ocfs2_schedule_truncate_log_flush(osb, 0);
409 	}
410 
411 	if (jbd2_journal_start_commit(osb->journal->j_journal,
412 				      &target)) {
413 		if (wait)
414 			jbd2_log_wait_commit(osb->journal->j_journal,
415 					     target);
416 	}
417 	return 0;
418 }
419 
420 static int ocfs2_need_system_inode(struct ocfs2_super *osb, int ino)
421 {
422 	if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_USRQUOTA)
423 	    && (ino == USER_QUOTA_SYSTEM_INODE
424 		|| ino == LOCAL_USER_QUOTA_SYSTEM_INODE))
425 		return 0;
426 	if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)
427 	    && (ino == GROUP_QUOTA_SYSTEM_INODE
428 		|| ino == LOCAL_GROUP_QUOTA_SYSTEM_INODE))
429 		return 0;
430 	return 1;
431 }
432 
433 static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb)
434 {
435 	struct inode *new = NULL;
436 	int status = 0;
437 	int i;
438 
439 	new = ocfs2_iget(osb, osb->root_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
440 	if (IS_ERR(new)) {
441 		status = PTR_ERR(new);
442 		mlog_errno(status);
443 		goto bail;
444 	}
445 	osb->root_inode = new;
446 
447 	new = ocfs2_iget(osb, osb->system_dir_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
448 	if (IS_ERR(new)) {
449 		status = PTR_ERR(new);
450 		mlog_errno(status);
451 		goto bail;
452 	}
453 	osb->sys_root_inode = new;
454 
455 	for (i = OCFS2_FIRST_ONLINE_SYSTEM_INODE;
456 	     i <= OCFS2_LAST_GLOBAL_SYSTEM_INODE; i++) {
457 		if (!ocfs2_need_system_inode(osb, i))
458 			continue;
459 		new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
460 		if (!new) {
461 			ocfs2_release_system_inodes(osb);
462 			status = ocfs2_is_soft_readonly(osb) ? -EROFS : -EINVAL;
463 			mlog_errno(status);
464 			mlog(ML_ERROR, "Unable to load system inode %d, "
465 			     "possibly corrupt fs?", i);
466 			goto bail;
467 		}
468 		// the array now has one ref, so drop this one
469 		iput(new);
470 	}
471 
472 bail:
473 	if (status)
474 		mlog_errno(status);
475 	return status;
476 }
477 
478 static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb)
479 {
480 	struct inode *new = NULL;
481 	int status = 0;
482 	int i;
483 
484 	for (i = OCFS2_LAST_GLOBAL_SYSTEM_INODE + 1;
485 	     i < NUM_SYSTEM_INODES;
486 	     i++) {
487 		if (!ocfs2_need_system_inode(osb, i))
488 			continue;
489 		new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
490 		if (!new) {
491 			ocfs2_release_system_inodes(osb);
492 			status = ocfs2_is_soft_readonly(osb) ? -EROFS : -EINVAL;
493 			mlog(ML_ERROR, "status=%d, sysfile=%d, slot=%d\n",
494 			     status, i, osb->slot_num);
495 			goto bail;
496 		}
497 		/* the array now has one ref, so drop this one */
498 		iput(new);
499 	}
500 
501 bail:
502 	if (status)
503 		mlog_errno(status);
504 	return status;
505 }
506 
507 static void ocfs2_release_system_inodes(struct ocfs2_super *osb)
508 {
509 	int i;
510 	struct inode *inode;
511 
512 	for (i = 0; i < NUM_GLOBAL_SYSTEM_INODES; i++) {
513 		inode = osb->global_system_inodes[i];
514 		if (inode) {
515 			iput(inode);
516 			osb->global_system_inodes[i] = NULL;
517 		}
518 	}
519 
520 	inode = osb->sys_root_inode;
521 	if (inode) {
522 		iput(inode);
523 		osb->sys_root_inode = NULL;
524 	}
525 
526 	inode = osb->root_inode;
527 	if (inode) {
528 		iput(inode);
529 		osb->root_inode = NULL;
530 	}
531 
532 	if (!osb->local_system_inodes)
533 		return;
534 
535 	for (i = 0; i < NUM_LOCAL_SYSTEM_INODES * osb->max_slots; i++) {
536 		if (osb->local_system_inodes[i]) {
537 			iput(osb->local_system_inodes[i]);
538 			osb->local_system_inodes[i] = NULL;
539 		}
540 	}
541 
542 	kfree(osb->local_system_inodes);
543 	osb->local_system_inodes = NULL;
544 }
545 
546 /* We're allocating fs objects, use GFP_NOFS */
547 static struct inode *ocfs2_alloc_inode(struct super_block *sb)
548 {
549 	struct ocfs2_inode_info *oi;
550 
551 	oi = kmem_cache_alloc(ocfs2_inode_cachep, GFP_NOFS);
552 	if (!oi)
553 		return NULL;
554 
555 	oi->i_sync_tid = 0;
556 	oi->i_datasync_tid = 0;
557 	memset(&oi->i_dquot, 0, sizeof(oi->i_dquot));
558 
559 	jbd2_journal_init_jbd_inode(&oi->ip_jinode, &oi->vfs_inode);
560 	return &oi->vfs_inode;
561 }
562 
563 static void ocfs2_free_inode(struct inode *inode)
564 {
565 	kmem_cache_free(ocfs2_inode_cachep, OCFS2_I(inode));
566 }
567 
568 static unsigned long long ocfs2_max_file_offset(unsigned int bbits,
569 						unsigned int cbits)
570 {
571 	unsigned int bytes = 1 << cbits;
572 	unsigned int trim = bytes;
573 	unsigned int bitshift = 32;
574 
575 	/*
576 	 * i_size and all block offsets in ocfs2 are always 64 bits
577 	 * wide. i_clusters is 32 bits, in cluster-sized units. So on
578 	 * 64 bit platforms, cluster size will be the limiting factor.
579 	 */
580 
581 #if BITS_PER_LONG == 32
582 	BUILD_BUG_ON(sizeof(sector_t) != 8);
583 	/*
584 	 * We might be limited by page cache size.
585 	 */
586 	if (bytes > PAGE_SIZE) {
587 		bytes = PAGE_SIZE;
588 		trim = 1;
589 		/*
590 		 * Shift by 31 here so that we don't get larger than
591 		 * MAX_LFS_FILESIZE
592 		 */
593 		bitshift = 31;
594 	}
595 #endif
596 
597 	/*
598 	 * Trim by a whole cluster when we can actually approach the
599 	 * on-disk limits. Otherwise we can overflow i_clusters when
600 	 * an extent start is at the max offset.
601 	 */
602 	return (((unsigned long long)bytes) << bitshift) - trim;
603 }
604 
605 static int ocfs2_remount(struct super_block *sb, int *flags, char *data)
606 {
607 	int incompat_features;
608 	int ret = 0;
609 	struct mount_options parsed_options;
610 	struct ocfs2_super *osb = OCFS2_SB(sb);
611 	u32 tmp;
612 
613 	sync_filesystem(sb);
614 
615 	if (!ocfs2_parse_options(sb, data, &parsed_options, 1) ||
616 	    !ocfs2_check_set_options(sb, &parsed_options)) {
617 		ret = -EINVAL;
618 		goto out;
619 	}
620 
621 	tmp = OCFS2_MOUNT_NOCLUSTER;
622 	if ((osb->s_mount_opt & tmp) != (parsed_options.mount_opt & tmp)) {
623 		ret = -EINVAL;
624 		mlog(ML_ERROR, "Cannot change nocluster option on remount\n");
625 		goto out;
626 	}
627 
628 	tmp = OCFS2_MOUNT_HB_LOCAL | OCFS2_MOUNT_HB_GLOBAL |
629 		OCFS2_MOUNT_HB_NONE;
630 	if ((osb->s_mount_opt & tmp) != (parsed_options.mount_opt & tmp)) {
631 		ret = -EINVAL;
632 		mlog(ML_ERROR, "Cannot change heartbeat mode on remount\n");
633 		goto out;
634 	}
635 
636 	if ((osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK) !=
637 	    (parsed_options.mount_opt & OCFS2_MOUNT_DATA_WRITEBACK)) {
638 		ret = -EINVAL;
639 		mlog(ML_ERROR, "Cannot change data mode on remount\n");
640 		goto out;
641 	}
642 
643 	/* Probably don't want this on remount; it might
644 	 * mess with other nodes */
645 	if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64) &&
646 	    (parsed_options.mount_opt & OCFS2_MOUNT_INODE64)) {
647 		ret = -EINVAL;
648 		mlog(ML_ERROR, "Cannot enable inode64 on remount\n");
649 		goto out;
650 	}
651 
652 	/* We're going to/from readonly mode. */
653 	if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
654 		/* Disable quota accounting before remounting RO */
655 		if (*flags & SB_RDONLY) {
656 			ret = ocfs2_susp_quotas(osb, 0);
657 			if (ret < 0)
658 				goto out;
659 		}
660 		/* Lock here so the check of HARD_RO and the potential
661 		 * setting of SOFT_RO is atomic. */
662 		spin_lock(&osb->osb_lock);
663 		if (osb->osb_flags & OCFS2_OSB_HARD_RO) {
664 			mlog(ML_ERROR, "Remount on readonly device is forbidden.\n");
665 			ret = -EROFS;
666 			goto unlock_osb;
667 		}
668 
669 		if (*flags & SB_RDONLY) {
670 			sb->s_flags |= SB_RDONLY;
671 			osb->osb_flags |= OCFS2_OSB_SOFT_RO;
672 		} else {
673 			if (osb->osb_flags & OCFS2_OSB_ERROR_FS) {
674 				mlog(ML_ERROR, "Cannot remount RDWR "
675 				     "filesystem due to previous errors.\n");
676 				ret = -EROFS;
677 				goto unlock_osb;
678 			}
679 			incompat_features = OCFS2_HAS_RO_COMPAT_FEATURE(sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP);
680 			if (incompat_features) {
681 				mlog(ML_ERROR, "Cannot remount RDWR because "
682 				     "of unsupported optional features "
683 				     "(%x).\n", incompat_features);
684 				ret = -EINVAL;
685 				goto unlock_osb;
686 			}
687 			sb->s_flags &= ~SB_RDONLY;
688 			osb->osb_flags &= ~OCFS2_OSB_SOFT_RO;
689 		}
690 		trace_ocfs2_remount(sb->s_flags, osb->osb_flags, *flags);
691 unlock_osb:
692 		spin_unlock(&osb->osb_lock);
693 		/* Enable quota accounting after remounting RW */
694 		if (!ret && !(*flags & SB_RDONLY)) {
695 			if (sb_any_quota_suspended(sb))
696 				ret = ocfs2_susp_quotas(osb, 1);
697 			else
698 				ret = ocfs2_enable_quotas(osb);
699 			if (ret < 0) {
700 				/* Return back changes... */
701 				spin_lock(&osb->osb_lock);
702 				sb->s_flags |= SB_RDONLY;
703 				osb->osb_flags |= OCFS2_OSB_SOFT_RO;
704 				spin_unlock(&osb->osb_lock);
705 				goto out;
706 			}
707 		}
708 	}
709 
710 	if (!ret) {
711 		/* Only save off the new mount options in case of a successful
712 		 * remount. */
713 		osb->s_mount_opt = parsed_options.mount_opt;
714 		osb->s_atime_quantum = parsed_options.atime_quantum;
715 		osb->preferred_slot = parsed_options.slot;
716 		if (parsed_options.commit_interval)
717 			osb->osb_commit_interval = parsed_options.commit_interval;
718 
719 		if (!ocfs2_is_hard_readonly(osb))
720 			ocfs2_set_journal_params(osb);
721 
722 		sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
723 			((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ?
724 							SB_POSIXACL : 0);
725 	}
726 out:
727 	return ret;
728 }
729 
730 static int ocfs2_sb_probe(struct super_block *sb,
731 			  struct buffer_head **bh,
732 			  int *sector_size,
733 			  struct ocfs2_blockcheck_stats *stats)
734 {
735 	int status, tmpstat;
736 	struct ocfs1_vol_disk_hdr *hdr;
737 	struct ocfs2_dinode *di;
738 	int blksize;
739 
740 	*bh = NULL;
741 
742 	/* may be > 512 */
743 	*sector_size = bdev_logical_block_size(sb->s_bdev);
744 	if (*sector_size > OCFS2_MAX_BLOCKSIZE) {
745 		mlog(ML_ERROR, "Hardware sector size too large: %d (max=%d)\n",
746 		     *sector_size, OCFS2_MAX_BLOCKSIZE);
747 		status = -EINVAL;
748 		goto bail;
749 	}
750 
751 	/* Can this really happen? */
752 	if (*sector_size < OCFS2_MIN_BLOCKSIZE)
753 		*sector_size = OCFS2_MIN_BLOCKSIZE;
754 
755 	/* check block zero for old format */
756 	status = ocfs2_get_sector(sb, bh, 0, *sector_size);
757 	if (status < 0) {
758 		mlog_errno(status);
759 		goto bail;
760 	}
761 	hdr = (struct ocfs1_vol_disk_hdr *) (*bh)->b_data;
762 	if (hdr->major_version == OCFS1_MAJOR_VERSION) {
763 		mlog(ML_ERROR, "incompatible version: %u.%u\n",
764 		     hdr->major_version, hdr->minor_version);
765 		status = -EINVAL;
766 	}
767 	if (memcmp(hdr->signature, OCFS1_VOLUME_SIGNATURE,
768 		   strlen(OCFS1_VOLUME_SIGNATURE)) == 0) {
769 		mlog(ML_ERROR, "incompatible volume signature: %8s\n",
770 		     hdr->signature);
771 		status = -EINVAL;
772 	}
773 	brelse(*bh);
774 	*bh = NULL;
775 	if (status < 0) {
776 		mlog(ML_ERROR, "This is an ocfs v1 filesystem which must be "
777 		     "upgraded before mounting with ocfs v2\n");
778 		goto bail;
779 	}
780 
781 	/*
782 	 * Now check at magic offset for 512, 1024, 2048, 4096
783 	 * blocksizes.  4096 is the maximum blocksize because it is
784 	 * the minimum clustersize.
785 	 */
786 	status = -EINVAL;
787 	for (blksize = *sector_size;
788 	     blksize <= OCFS2_MAX_BLOCKSIZE;
789 	     blksize <<= 1) {
790 		tmpstat = ocfs2_get_sector(sb, bh,
791 					   OCFS2_SUPER_BLOCK_BLKNO,
792 					   blksize);
793 		if (tmpstat < 0) {
794 			status = tmpstat;
795 			mlog_errno(status);
796 			break;
797 		}
798 		di = (struct ocfs2_dinode *) (*bh)->b_data;
799 		memset(stats, 0, sizeof(struct ocfs2_blockcheck_stats));
800 		spin_lock_init(&stats->b_lock);
801 		tmpstat = ocfs2_verify_volume(di, *bh, blksize, stats);
802 		if (tmpstat < 0) {
803 			brelse(*bh);
804 			*bh = NULL;
805 		}
806 		if (tmpstat != -EAGAIN) {
807 			status = tmpstat;
808 			break;
809 		}
810 	}
811 
812 bail:
813 	return status;
814 }
815 
816 static int ocfs2_verify_heartbeat(struct ocfs2_super *osb)
817 {
818 	u32 hb_enabled = OCFS2_MOUNT_HB_LOCAL | OCFS2_MOUNT_HB_GLOBAL;
819 
820 	if (osb->s_mount_opt & hb_enabled) {
821 		if (ocfs2_mount_local(osb)) {
822 			mlog(ML_ERROR, "Cannot heartbeat on a locally "
823 			     "mounted device.\n");
824 			return -EINVAL;
825 		}
826 		if (ocfs2_userspace_stack(osb)) {
827 			mlog(ML_ERROR, "Userspace stack expected, but "
828 			     "o2cb heartbeat arguments passed to mount\n");
829 			return -EINVAL;
830 		}
831 		if (((osb->s_mount_opt & OCFS2_MOUNT_HB_GLOBAL) &&
832 		     !ocfs2_cluster_o2cb_global_heartbeat(osb)) ||
833 		    ((osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) &&
834 		     ocfs2_cluster_o2cb_global_heartbeat(osb))) {
835 			mlog(ML_ERROR, "Mismatching o2cb heartbeat modes\n");
836 			return -EINVAL;
837 		}
838 	}
839 
840 	if (!(osb->s_mount_opt & hb_enabled)) {
841 		if (!ocfs2_mount_local(osb) && !ocfs2_is_hard_readonly(osb) &&
842 		    !ocfs2_userspace_stack(osb)) {
843 			mlog(ML_ERROR, "Heartbeat has to be started to mount "
844 			     "a read-write clustered device.\n");
845 			return -EINVAL;
846 		}
847 	}
848 
849 	return 0;
850 }
851 
852 /*
853  * If we're using a userspace stack, mount should have passed
854  * a name that matches the disk.  If not, mount should not
855  * have passed a stack.
856  */
857 static int ocfs2_verify_userspace_stack(struct ocfs2_super *osb,
858 					struct mount_options *mopt)
859 {
860 	if (!ocfs2_userspace_stack(osb) && mopt->cluster_stack[0]) {
861 		mlog(ML_ERROR,
862 		     "cluster stack passed to mount, but this filesystem "
863 		     "does not support it\n");
864 		return -EINVAL;
865 	}
866 
867 	if (ocfs2_userspace_stack(osb) &&
868 	    !(osb->s_mount_opt & OCFS2_MOUNT_NOCLUSTER) &&
869 	    strncmp(osb->osb_cluster_stack, mopt->cluster_stack,
870 		    OCFS2_STACK_LABEL_LEN)) {
871 		mlog(ML_ERROR,
872 		     "cluster stack passed to mount (\"%s\") does not "
873 		     "match the filesystem (\"%s\")\n",
874 		     mopt->cluster_stack,
875 		     osb->osb_cluster_stack);
876 		return -EINVAL;
877 	}
878 
879 	return 0;
880 }
881 
882 static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend)
883 {
884 	int type;
885 	struct super_block *sb = osb->sb;
886 	unsigned int feature[OCFS2_MAXQUOTAS] = {
887 					OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
888 					OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
889 	int status = 0;
890 
891 	for (type = 0; type < OCFS2_MAXQUOTAS; type++) {
892 		if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
893 			continue;
894 		if (unsuspend)
895 			status = dquot_resume(sb, type);
896 		else {
897 			struct ocfs2_mem_dqinfo *oinfo;
898 
899 			/* Cancel periodic syncing before suspending */
900 			oinfo = sb_dqinfo(sb, type)->dqi_priv;
901 			cancel_delayed_work_sync(&oinfo->dqi_sync_work);
902 			status = dquot_suspend(sb, type);
903 		}
904 		if (status < 0)
905 			break;
906 	}
907 	if (status < 0)
908 		mlog(ML_ERROR, "Failed to suspend/unsuspend quotas on "
909 		     "remount (error = %d).\n", status);
910 	return status;
911 }
912 
913 static int ocfs2_enable_quotas(struct ocfs2_super *osb)
914 {
915 	struct inode *inode[OCFS2_MAXQUOTAS] = { NULL, NULL };
916 	struct super_block *sb = osb->sb;
917 	unsigned int feature[OCFS2_MAXQUOTAS] = {
918 					OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
919 					OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
920 	unsigned int ino[OCFS2_MAXQUOTAS] = {
921 					LOCAL_USER_QUOTA_SYSTEM_INODE,
922 					LOCAL_GROUP_QUOTA_SYSTEM_INODE };
923 	int status;
924 	int type;
925 
926 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NEGATIVE_USAGE;
927 	for (type = 0; type < OCFS2_MAXQUOTAS; type++) {
928 		if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
929 			continue;
930 		inode[type] = ocfs2_get_system_file_inode(osb, ino[type],
931 							osb->slot_num);
932 		if (!inode[type]) {
933 			status = -ENOENT;
934 			goto out_quota_off;
935 		}
936 		status = dquot_load_quota_inode(inode[type], type, QFMT_OCFS2,
937 						DQUOT_USAGE_ENABLED);
938 		if (status < 0)
939 			goto out_quota_off;
940 	}
941 
942 	for (type = 0; type < OCFS2_MAXQUOTAS; type++)
943 		iput(inode[type]);
944 	return 0;
945 out_quota_off:
946 	ocfs2_disable_quotas(osb);
947 	for (type = 0; type < OCFS2_MAXQUOTAS; type++)
948 		iput(inode[type]);
949 	mlog_errno(status);
950 	return status;
951 }
952 
953 static void ocfs2_disable_quotas(struct ocfs2_super *osb)
954 {
955 	int type;
956 	struct inode *inode;
957 	struct super_block *sb = osb->sb;
958 	struct ocfs2_mem_dqinfo *oinfo;
959 
960 	/* We mostly ignore errors in this function because there's not much
961 	 * we can do when we see them */
962 	for (type = 0; type < OCFS2_MAXQUOTAS; type++) {
963 		if (!sb_has_quota_loaded(sb, type))
964 			continue;
965 		oinfo = sb_dqinfo(sb, type)->dqi_priv;
966 		cancel_delayed_work_sync(&oinfo->dqi_sync_work);
967 		inode = igrab(sb->s_dquot.files[type]);
968 		/* Turn off quotas. This will remove all dquot structures from
969 		 * memory and so they will be automatically synced to global
970 		 * quota files */
971 		dquot_disable(sb, type, DQUOT_USAGE_ENABLED |
972 					DQUOT_LIMITS_ENABLED);
973 		iput(inode);
974 	}
975 }
976 
977 static int ocfs2_fill_super(struct super_block *sb, void *data, int silent)
978 {
979 	struct dentry *root;
980 	int status, sector_size;
981 	struct mount_options parsed_options;
982 	struct inode *inode = NULL;
983 	struct ocfs2_super *osb = NULL;
984 	struct buffer_head *bh = NULL;
985 	char nodestr[12];
986 	struct ocfs2_blockcheck_stats stats;
987 
988 	trace_ocfs2_fill_super(sb, data, silent);
989 
990 	if (!ocfs2_parse_options(sb, data, &parsed_options, 0)) {
991 		status = -EINVAL;
992 		goto read_super_error;
993 	}
994 
995 	/* probe for superblock */
996 	status = ocfs2_sb_probe(sb, &bh, &sector_size, &stats);
997 	if (status < 0) {
998 		mlog(ML_ERROR, "superblock probe failed!\n");
999 		goto read_super_error;
1000 	}
1001 
1002 	status = ocfs2_initialize_super(sb, bh, sector_size, &stats);
1003 	osb = OCFS2_SB(sb);
1004 	if (status < 0) {
1005 		mlog_errno(status);
1006 		goto read_super_error;
1007 	}
1008 	brelse(bh);
1009 	bh = NULL;
1010 
1011 	if (!ocfs2_check_set_options(sb, &parsed_options)) {
1012 		status = -EINVAL;
1013 		goto read_super_error;
1014 	}
1015 	osb->s_mount_opt = parsed_options.mount_opt;
1016 	osb->s_atime_quantum = parsed_options.atime_quantum;
1017 	osb->preferred_slot = parsed_options.slot;
1018 	osb->osb_commit_interval = parsed_options.commit_interval;
1019 
1020 	ocfs2_la_set_sizes(osb, parsed_options.localalloc_opt);
1021 	osb->osb_resv_level = parsed_options.resv_level;
1022 	osb->osb_dir_resv_level = parsed_options.resv_level;
1023 	if (parsed_options.dir_resv_level == -1)
1024 		osb->osb_dir_resv_level = parsed_options.resv_level;
1025 	else
1026 		osb->osb_dir_resv_level = parsed_options.dir_resv_level;
1027 
1028 	status = ocfs2_verify_userspace_stack(osb, &parsed_options);
1029 	if (status)
1030 		goto read_super_error;
1031 
1032 	sb->s_magic = OCFS2_SUPER_MAGIC;
1033 
1034 	sb->s_flags = (sb->s_flags & ~(SB_POSIXACL | SB_NOSEC)) |
1035 		((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ? SB_POSIXACL : 0);
1036 
1037 	/* Hard readonly mode only if: bdev_read_only, SB_RDONLY,
1038 	 * heartbeat=none */
1039 	if (bdev_read_only(sb->s_bdev)) {
1040 		if (!sb_rdonly(sb)) {
1041 			status = -EACCES;
1042 			mlog(ML_ERROR, "Readonly device detected but readonly "
1043 			     "mount was not specified.\n");
1044 			goto read_super_error;
1045 		}
1046 
1047 		/* You should not be able to start a local heartbeat
1048 		 * on a readonly device. */
1049 		if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
1050 			status = -EROFS;
1051 			mlog(ML_ERROR, "Local heartbeat specified on readonly "
1052 			     "device.\n");
1053 			goto read_super_error;
1054 		}
1055 
1056 		status = ocfs2_check_journals_nolocks(osb);
1057 		if (status < 0) {
1058 			if (status == -EROFS)
1059 				mlog(ML_ERROR, "Recovery required on readonly "
1060 				     "file system, but write access is "
1061 				     "unavailable.\n");
1062 			else
1063 				mlog_errno(status);
1064 			goto read_super_error;
1065 		}
1066 
1067 		ocfs2_set_ro_flag(osb, 1);
1068 
1069 		printk(KERN_NOTICE "ocfs2: Readonly device (%s) detected. "
1070 		       "Cluster services will not be used for this mount. "
1071 		       "Recovery will be skipped.\n", osb->dev_str);
1072 	}
1073 
1074 	if (!ocfs2_is_hard_readonly(osb)) {
1075 		if (sb_rdonly(sb))
1076 			ocfs2_set_ro_flag(osb, 0);
1077 	}
1078 
1079 	status = ocfs2_verify_heartbeat(osb);
1080 	if (status < 0) {
1081 		mlog_errno(status);
1082 		goto read_super_error;
1083 	}
1084 
1085 	osb->osb_debug_root = debugfs_create_dir(osb->uuid_str,
1086 						 ocfs2_debugfs_root);
1087 
1088 	debugfs_create_file("fs_state", S_IFREG|S_IRUSR, osb->osb_debug_root,
1089 			    osb, &ocfs2_osb_debug_fops);
1090 
1091 	if (ocfs2_meta_ecc(osb))
1092 		ocfs2_blockcheck_stats_debugfs_install( &osb->osb_ecc_stats,
1093 							osb->osb_debug_root);
1094 
1095 	status = ocfs2_mount_volume(sb);
1096 	if (status < 0)
1097 		goto read_super_error;
1098 
1099 	if (osb->root_inode)
1100 		inode = igrab(osb->root_inode);
1101 
1102 	if (!inode) {
1103 		status = -EIO;
1104 		mlog_errno(status);
1105 		goto read_super_error;
1106 	}
1107 
1108 	osb->osb_dev_kset = kset_create_and_add(sb->s_id, NULL,
1109 						&ocfs2_kset->kobj);
1110 	if (!osb->osb_dev_kset) {
1111 		status = -ENOMEM;
1112 		mlog(ML_ERROR, "Unable to create device kset %s.\n", sb->s_id);
1113 		goto read_super_error;
1114 	}
1115 
1116 	/* Create filecheck sysfs related directories/files at
1117 	 * /sys/fs/ocfs2/<devname>/filecheck */
1118 	if (ocfs2_filecheck_create_sysfs(osb)) {
1119 		status = -ENOMEM;
1120 		mlog(ML_ERROR, "Unable to create filecheck sysfs directory at "
1121 			"/sys/fs/ocfs2/%s/filecheck.\n", sb->s_id);
1122 		goto read_super_error;
1123 	}
1124 
1125 	root = d_make_root(inode);
1126 	if (!root) {
1127 		status = -ENOMEM;
1128 		mlog_errno(status);
1129 		goto read_super_error;
1130 	}
1131 
1132 	sb->s_root = root;
1133 
1134 	ocfs2_complete_mount_recovery(osb);
1135 
1136 	if (ocfs2_mount_local(osb))
1137 		snprintf(nodestr, sizeof(nodestr), "local");
1138 	else
1139 		snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
1140 
1141 	printk(KERN_INFO "ocfs2: Mounting device (%s) on (node %s, slot %d) "
1142 	       "with %s data mode.\n",
1143 	       osb->dev_str, nodestr, osb->slot_num,
1144 	       osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK ? "writeback" :
1145 	       "ordered");
1146 
1147 	if ((osb->s_mount_opt & OCFS2_MOUNT_NOCLUSTER) &&
1148 	   !(osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_LOCAL_MOUNT))
1149 		printk(KERN_NOTICE "ocfs2: The shared device (%s) is mounted "
1150 		       "without cluster aware mode.\n", osb->dev_str);
1151 
1152 	atomic_set(&osb->vol_state, VOLUME_MOUNTED);
1153 	wake_up(&osb->osb_mount_event);
1154 
1155 	/* Now we can initialize quotas because we can afford to wait
1156 	 * for cluster locks recovery now. That also means that truncation
1157 	 * log recovery can happen but that waits for proper quota setup */
1158 	if (!sb_rdonly(sb)) {
1159 		status = ocfs2_enable_quotas(osb);
1160 		if (status < 0) {
1161 			/* We have to err-out specially here because
1162 			 * s_root is already set */
1163 			mlog_errno(status);
1164 			atomic_set(&osb->vol_state, VOLUME_DISABLED);
1165 			wake_up(&osb->osb_mount_event);
1166 			return status;
1167 		}
1168 	}
1169 
1170 	ocfs2_complete_quota_recovery(osb);
1171 
1172 	/* Now we wake up again for processes waiting for quotas */
1173 	atomic_set(&osb->vol_state, VOLUME_MOUNTED_QUOTAS);
1174 	wake_up(&osb->osb_mount_event);
1175 
1176 	/* Start this when the mount is almost sure of being successful */
1177 	ocfs2_orphan_scan_start(osb);
1178 
1179 	return status;
1180 
1181 read_super_error:
1182 	brelse(bh);
1183 
1184 	if (status)
1185 		mlog_errno(status);
1186 
1187 	if (osb) {
1188 		atomic_set(&osb->vol_state, VOLUME_DISABLED);
1189 		wake_up(&osb->osb_mount_event);
1190 		ocfs2_dismount_volume(sb, 1);
1191 	}
1192 
1193 	return status;
1194 }
1195 
1196 static struct dentry *ocfs2_mount(struct file_system_type *fs_type,
1197 			int flags,
1198 			const char *dev_name,
1199 			void *data)
1200 {
1201 	return mount_bdev(fs_type, flags, dev_name, data, ocfs2_fill_super);
1202 }
1203 
1204 static struct file_system_type ocfs2_fs_type = {
1205 	.owner          = THIS_MODULE,
1206 	.name           = "ocfs2",
1207 	.mount          = ocfs2_mount,
1208 	.kill_sb        = kill_block_super,
1209 	.fs_flags       = FS_REQUIRES_DEV|FS_RENAME_DOES_D_MOVE,
1210 	.next           = NULL
1211 };
1212 MODULE_ALIAS_FS("ocfs2");
1213 
1214 static int ocfs2_check_set_options(struct super_block *sb,
1215 				   struct mount_options *options)
1216 {
1217 	if (options->mount_opt & OCFS2_MOUNT_USRQUOTA &&
1218 	    !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1219 					 OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
1220 		mlog(ML_ERROR, "User quotas were requested, but this "
1221 		     "filesystem does not have the feature enabled.\n");
1222 		return 0;
1223 	}
1224 	if (options->mount_opt & OCFS2_MOUNT_GRPQUOTA &&
1225 	    !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1226 					 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
1227 		mlog(ML_ERROR, "Group quotas were requested, but this "
1228 		     "filesystem does not have the feature enabled.\n");
1229 		return 0;
1230 	}
1231 	if (options->mount_opt & OCFS2_MOUNT_POSIX_ACL &&
1232 	    !OCFS2_HAS_INCOMPAT_FEATURE(sb, OCFS2_FEATURE_INCOMPAT_XATTR)) {
1233 		mlog(ML_ERROR, "ACL support requested but extended attributes "
1234 		     "feature is not enabled\n");
1235 		return 0;
1236 	}
1237 	/* No ACL setting specified? Use XATTR feature... */
1238 	if (!(options->mount_opt & (OCFS2_MOUNT_POSIX_ACL |
1239 				    OCFS2_MOUNT_NO_POSIX_ACL))) {
1240 		if (OCFS2_HAS_INCOMPAT_FEATURE(sb, OCFS2_FEATURE_INCOMPAT_XATTR))
1241 			options->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
1242 		else
1243 			options->mount_opt |= OCFS2_MOUNT_NO_POSIX_ACL;
1244 	}
1245 	return 1;
1246 }
1247 
1248 static int ocfs2_parse_options(struct super_block *sb,
1249 			       char *options,
1250 			       struct mount_options *mopt,
1251 			       int is_remount)
1252 {
1253 	int status, user_stack = 0;
1254 	char *p;
1255 	u32 tmp;
1256 	int token, option;
1257 	substring_t args[MAX_OPT_ARGS];
1258 
1259 	trace_ocfs2_parse_options(is_remount, options ? options : "(none)");
1260 
1261 	mopt->commit_interval = 0;
1262 	mopt->mount_opt = OCFS2_MOUNT_NOINTR;
1263 	mopt->atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
1264 	mopt->slot = OCFS2_INVALID_SLOT;
1265 	mopt->localalloc_opt = -1;
1266 	mopt->cluster_stack[0] = '\0';
1267 	mopt->resv_level = OCFS2_DEFAULT_RESV_LEVEL;
1268 	mopt->dir_resv_level = -1;
1269 
1270 	if (!options) {
1271 		status = 1;
1272 		goto bail;
1273 	}
1274 
1275 	while ((p = strsep(&options, ",")) != NULL) {
1276 		if (!*p)
1277 			continue;
1278 
1279 		token = match_token(p, tokens, args);
1280 		switch (token) {
1281 		case Opt_hb_local:
1282 			mopt->mount_opt |= OCFS2_MOUNT_HB_LOCAL;
1283 			break;
1284 		case Opt_hb_none:
1285 			mopt->mount_opt |= OCFS2_MOUNT_HB_NONE;
1286 			break;
1287 		case Opt_hb_global:
1288 			mopt->mount_opt |= OCFS2_MOUNT_HB_GLOBAL;
1289 			break;
1290 		case Opt_barrier:
1291 			if (match_int(&args[0], &option)) {
1292 				status = 0;
1293 				goto bail;
1294 			}
1295 			if (option)
1296 				mopt->mount_opt |= OCFS2_MOUNT_BARRIER;
1297 			else
1298 				mopt->mount_opt &= ~OCFS2_MOUNT_BARRIER;
1299 			break;
1300 		case Opt_intr:
1301 			mopt->mount_opt &= ~OCFS2_MOUNT_NOINTR;
1302 			break;
1303 		case Opt_nointr:
1304 			mopt->mount_opt |= OCFS2_MOUNT_NOINTR;
1305 			break;
1306 		case Opt_err_panic:
1307 			mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_CONT;
1308 			mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_ROFS;
1309 			mopt->mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
1310 			break;
1311 		case Opt_err_ro:
1312 			mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_CONT;
1313 			mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_PANIC;
1314 			mopt->mount_opt |= OCFS2_MOUNT_ERRORS_ROFS;
1315 			break;
1316 		case Opt_err_cont:
1317 			mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_ROFS;
1318 			mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_PANIC;
1319 			mopt->mount_opt |= OCFS2_MOUNT_ERRORS_CONT;
1320 			break;
1321 		case Opt_data_ordered:
1322 			mopt->mount_opt &= ~OCFS2_MOUNT_DATA_WRITEBACK;
1323 			break;
1324 		case Opt_data_writeback:
1325 			mopt->mount_opt |= OCFS2_MOUNT_DATA_WRITEBACK;
1326 			break;
1327 		case Opt_user_xattr:
1328 			mopt->mount_opt &= ~OCFS2_MOUNT_NOUSERXATTR;
1329 			break;
1330 		case Opt_nouser_xattr:
1331 			mopt->mount_opt |= OCFS2_MOUNT_NOUSERXATTR;
1332 			break;
1333 		case Opt_atime_quantum:
1334 			if (match_int(&args[0], &option)) {
1335 				status = 0;
1336 				goto bail;
1337 			}
1338 			if (option >= 0)
1339 				mopt->atime_quantum = option;
1340 			break;
1341 		case Opt_slot:
1342 			if (match_int(&args[0], &option)) {
1343 				status = 0;
1344 				goto bail;
1345 			}
1346 			if (option)
1347 				mopt->slot = (u16)option;
1348 			break;
1349 		case Opt_commit:
1350 			if (match_int(&args[0], &option)) {
1351 				status = 0;
1352 				goto bail;
1353 			}
1354 			if (option < 0)
1355 				return 0;
1356 			if (option == 0)
1357 				option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1358 			mopt->commit_interval = HZ * option;
1359 			break;
1360 		case Opt_localalloc:
1361 			if (match_int(&args[0], &option)) {
1362 				status = 0;
1363 				goto bail;
1364 			}
1365 			if (option >= 0)
1366 				mopt->localalloc_opt = option;
1367 			break;
1368 		case Opt_localflocks:
1369 			/*
1370 			 * Changing this during remount could race
1371 			 * flock() requests, or "unbalance" existing
1372 			 * ones (e.g., a lock is taken in one mode but
1373 			 * dropped in the other). If users care enough
1374 			 * to flip locking modes during remount, we
1375 			 * could add a "local" flag to individual
1376 			 * flock structures for proper tracking of
1377 			 * state.
1378 			 */
1379 			if (!is_remount)
1380 				mopt->mount_opt |= OCFS2_MOUNT_LOCALFLOCKS;
1381 			break;
1382 		case Opt_stack:
1383 			/* Check both that the option we were passed
1384 			 * is of the right length and that it is a proper
1385 			 * string of the right length.
1386 			 */
1387 			if (((args[0].to - args[0].from) !=
1388 			     OCFS2_STACK_LABEL_LEN) ||
1389 			    (strnlen(args[0].from,
1390 				     OCFS2_STACK_LABEL_LEN) !=
1391 			     OCFS2_STACK_LABEL_LEN)) {
1392 				mlog(ML_ERROR,
1393 				     "Invalid cluster_stack option\n");
1394 				status = 0;
1395 				goto bail;
1396 			}
1397 			memcpy(mopt->cluster_stack, args[0].from,
1398 			       OCFS2_STACK_LABEL_LEN);
1399 			mopt->cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
1400 			/*
1401 			 * Open code the memcmp here as we don't have
1402 			 * an osb to pass to
1403 			 * ocfs2_userspace_stack().
1404 			 */
1405 			if (memcmp(mopt->cluster_stack,
1406 				   OCFS2_CLASSIC_CLUSTER_STACK,
1407 				   OCFS2_STACK_LABEL_LEN))
1408 				user_stack = 1;
1409 			break;
1410 		case Opt_inode64:
1411 			mopt->mount_opt |= OCFS2_MOUNT_INODE64;
1412 			break;
1413 		case Opt_usrquota:
1414 			mopt->mount_opt |= OCFS2_MOUNT_USRQUOTA;
1415 			break;
1416 		case Opt_grpquota:
1417 			mopt->mount_opt |= OCFS2_MOUNT_GRPQUOTA;
1418 			break;
1419 		case Opt_coherency_buffered:
1420 			mopt->mount_opt |= OCFS2_MOUNT_COHERENCY_BUFFERED;
1421 			break;
1422 		case Opt_coherency_full:
1423 			mopt->mount_opt &= ~OCFS2_MOUNT_COHERENCY_BUFFERED;
1424 			break;
1425 		case Opt_acl:
1426 			mopt->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
1427 			mopt->mount_opt &= ~OCFS2_MOUNT_NO_POSIX_ACL;
1428 			break;
1429 		case Opt_noacl:
1430 			mopt->mount_opt |= OCFS2_MOUNT_NO_POSIX_ACL;
1431 			mopt->mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
1432 			break;
1433 		case Opt_resv_level:
1434 			if (is_remount)
1435 				break;
1436 			if (match_int(&args[0], &option)) {
1437 				status = 0;
1438 				goto bail;
1439 			}
1440 			if (option >= OCFS2_MIN_RESV_LEVEL &&
1441 			    option < OCFS2_MAX_RESV_LEVEL)
1442 				mopt->resv_level = option;
1443 			break;
1444 		case Opt_dir_resv_level:
1445 			if (is_remount)
1446 				break;
1447 			if (match_int(&args[0], &option)) {
1448 				status = 0;
1449 				goto bail;
1450 			}
1451 			if (option >= OCFS2_MIN_RESV_LEVEL &&
1452 			    option < OCFS2_MAX_RESV_LEVEL)
1453 				mopt->dir_resv_level = option;
1454 			break;
1455 		case Opt_journal_async_commit:
1456 			mopt->mount_opt |= OCFS2_MOUNT_JOURNAL_ASYNC_COMMIT;
1457 			break;
1458 		case Opt_nocluster:
1459 			mopt->mount_opt |= OCFS2_MOUNT_NOCLUSTER;
1460 			break;
1461 		default:
1462 			mlog(ML_ERROR,
1463 			     "Unrecognized mount option \"%s\" "
1464 			     "or missing value\n", p);
1465 			status = 0;
1466 			goto bail;
1467 		}
1468 	}
1469 
1470 	if (user_stack == 0) {
1471 		/* Ensure only one heartbeat mode */
1472 		tmp = mopt->mount_opt & (OCFS2_MOUNT_HB_LOCAL |
1473 					 OCFS2_MOUNT_HB_GLOBAL |
1474 					 OCFS2_MOUNT_HB_NONE);
1475 		if (hweight32(tmp) != 1) {
1476 			mlog(ML_ERROR, "Invalid heartbeat mount options\n");
1477 			status = 0;
1478 			goto bail;
1479 		}
1480 	}
1481 
1482 	status = 1;
1483 
1484 bail:
1485 	return status;
1486 }
1487 
1488 static int ocfs2_show_options(struct seq_file *s, struct dentry *root)
1489 {
1490 	struct ocfs2_super *osb = OCFS2_SB(root->d_sb);
1491 	unsigned long opts = osb->s_mount_opt;
1492 	unsigned int local_alloc_megs;
1493 
1494 	if (opts & (OCFS2_MOUNT_HB_LOCAL | OCFS2_MOUNT_HB_GLOBAL)) {
1495 		seq_printf(s, ",_netdev");
1496 		if (opts & OCFS2_MOUNT_HB_LOCAL)
1497 			seq_printf(s, ",%s", OCFS2_HB_LOCAL);
1498 		else
1499 			seq_printf(s, ",%s", OCFS2_HB_GLOBAL);
1500 	} else
1501 		seq_printf(s, ",%s", OCFS2_HB_NONE);
1502 
1503 	if (opts & OCFS2_MOUNT_NOINTR)
1504 		seq_printf(s, ",nointr");
1505 
1506 	if (opts & OCFS2_MOUNT_DATA_WRITEBACK)
1507 		seq_printf(s, ",data=writeback");
1508 	else
1509 		seq_printf(s, ",data=ordered");
1510 
1511 	if (opts & OCFS2_MOUNT_BARRIER)
1512 		seq_printf(s, ",barrier=1");
1513 
1514 	if (opts & OCFS2_MOUNT_ERRORS_PANIC)
1515 		seq_printf(s, ",errors=panic");
1516 	else if (opts & OCFS2_MOUNT_ERRORS_CONT)
1517 		seq_printf(s, ",errors=continue");
1518 	else
1519 		seq_printf(s, ",errors=remount-ro");
1520 
1521 	if (osb->preferred_slot != OCFS2_INVALID_SLOT)
1522 		seq_printf(s, ",preferred_slot=%d", osb->preferred_slot);
1523 
1524 	seq_printf(s, ",atime_quantum=%u", osb->s_atime_quantum);
1525 
1526 	if (osb->osb_commit_interval)
1527 		seq_printf(s, ",commit=%u",
1528 			   (unsigned) (osb->osb_commit_interval / HZ));
1529 
1530 	local_alloc_megs = osb->local_alloc_bits >> (20 - osb->s_clustersize_bits);
1531 	if (local_alloc_megs != ocfs2_la_default_mb(osb))
1532 		seq_printf(s, ",localalloc=%d", local_alloc_megs);
1533 
1534 	if (opts & OCFS2_MOUNT_LOCALFLOCKS)
1535 		seq_printf(s, ",localflocks,");
1536 
1537 	if (osb->osb_cluster_stack[0])
1538 		seq_show_option_n(s, "cluster_stack", osb->osb_cluster_stack,
1539 				  OCFS2_STACK_LABEL_LEN);
1540 	if (opts & OCFS2_MOUNT_USRQUOTA)
1541 		seq_printf(s, ",usrquota");
1542 	if (opts & OCFS2_MOUNT_GRPQUOTA)
1543 		seq_printf(s, ",grpquota");
1544 
1545 	if (opts & OCFS2_MOUNT_COHERENCY_BUFFERED)
1546 		seq_printf(s, ",coherency=buffered");
1547 	else
1548 		seq_printf(s, ",coherency=full");
1549 
1550 	if (opts & OCFS2_MOUNT_NOUSERXATTR)
1551 		seq_printf(s, ",nouser_xattr");
1552 	else
1553 		seq_printf(s, ",user_xattr");
1554 
1555 	if (opts & OCFS2_MOUNT_INODE64)
1556 		seq_printf(s, ",inode64");
1557 
1558 	if (opts & OCFS2_MOUNT_POSIX_ACL)
1559 		seq_printf(s, ",acl");
1560 	else
1561 		seq_printf(s, ",noacl");
1562 
1563 	if (osb->osb_resv_level != OCFS2_DEFAULT_RESV_LEVEL)
1564 		seq_printf(s, ",resv_level=%d", osb->osb_resv_level);
1565 
1566 	if (osb->osb_dir_resv_level != osb->osb_resv_level)
1567 		seq_printf(s, ",dir_resv_level=%d", osb->osb_resv_level);
1568 
1569 	if (opts & OCFS2_MOUNT_JOURNAL_ASYNC_COMMIT)
1570 		seq_printf(s, ",journal_async_commit");
1571 
1572 	if (opts & OCFS2_MOUNT_NOCLUSTER)
1573 		seq_printf(s, ",nocluster");
1574 
1575 	return 0;
1576 }
1577 
1578 static int __init ocfs2_init(void)
1579 {
1580 	int status;
1581 
1582 	status = init_ocfs2_uptodate_cache();
1583 	if (status < 0)
1584 		goto out1;
1585 
1586 	status = ocfs2_initialize_mem_caches();
1587 	if (status < 0)
1588 		goto out2;
1589 
1590 	ocfs2_debugfs_root = debugfs_create_dir("ocfs2", NULL);
1591 
1592 	ocfs2_set_locking_protocol();
1593 
1594 	status = register_quota_format(&ocfs2_quota_format);
1595 	if (status < 0)
1596 		goto out3;
1597 	status = register_filesystem(&ocfs2_fs_type);
1598 	if (!status)
1599 		return 0;
1600 
1601 	unregister_quota_format(&ocfs2_quota_format);
1602 out3:
1603 	debugfs_remove(ocfs2_debugfs_root);
1604 	ocfs2_free_mem_caches();
1605 out2:
1606 	exit_ocfs2_uptodate_cache();
1607 out1:
1608 	mlog_errno(status);
1609 	return status;
1610 }
1611 
1612 static void __exit ocfs2_exit(void)
1613 {
1614 	unregister_quota_format(&ocfs2_quota_format);
1615 
1616 	debugfs_remove(ocfs2_debugfs_root);
1617 
1618 	ocfs2_free_mem_caches();
1619 
1620 	unregister_filesystem(&ocfs2_fs_type);
1621 
1622 	exit_ocfs2_uptodate_cache();
1623 }
1624 
1625 static void ocfs2_put_super(struct super_block *sb)
1626 {
1627 	trace_ocfs2_put_super(sb);
1628 
1629 	ocfs2_sync_blockdev(sb);
1630 	ocfs2_dismount_volume(sb, 0);
1631 }
1632 
1633 static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
1634 {
1635 	struct ocfs2_super *osb;
1636 	u32 numbits, freebits;
1637 	int status;
1638 	struct ocfs2_dinode *bm_lock;
1639 	struct buffer_head *bh = NULL;
1640 	struct inode *inode = NULL;
1641 
1642 	trace_ocfs2_statfs(dentry->d_sb, buf);
1643 
1644 	osb = OCFS2_SB(dentry->d_sb);
1645 
1646 	inode = ocfs2_get_system_file_inode(osb,
1647 					    GLOBAL_BITMAP_SYSTEM_INODE,
1648 					    OCFS2_INVALID_SLOT);
1649 	if (!inode) {
1650 		mlog(ML_ERROR, "failed to get bitmap inode\n");
1651 		status = -EIO;
1652 		goto bail;
1653 	}
1654 
1655 	status = ocfs2_inode_lock(inode, &bh, 0);
1656 	if (status < 0) {
1657 		mlog_errno(status);
1658 		goto bail;
1659 	}
1660 
1661 	bm_lock = (struct ocfs2_dinode *) bh->b_data;
1662 
1663 	numbits = le32_to_cpu(bm_lock->id1.bitmap1.i_total);
1664 	freebits = numbits - le32_to_cpu(bm_lock->id1.bitmap1.i_used);
1665 
1666 	buf->f_type = OCFS2_SUPER_MAGIC;
1667 	buf->f_bsize = dentry->d_sb->s_blocksize;
1668 	buf->f_namelen = OCFS2_MAX_FILENAME_LEN;
1669 	buf->f_blocks = ((sector_t) numbits) *
1670 			(osb->s_clustersize >> osb->sb->s_blocksize_bits);
1671 	buf->f_bfree = ((sector_t) freebits) *
1672 		       (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1673 	buf->f_bavail = buf->f_bfree;
1674 	buf->f_files = numbits;
1675 	buf->f_ffree = freebits;
1676 	buf->f_fsid.val[0] = crc32_le(0, osb->uuid_str, OCFS2_VOL_UUID_LEN)
1677 				& 0xFFFFFFFFUL;
1678 	buf->f_fsid.val[1] = crc32_le(0, osb->uuid_str + OCFS2_VOL_UUID_LEN,
1679 				OCFS2_VOL_UUID_LEN) & 0xFFFFFFFFUL;
1680 
1681 	brelse(bh);
1682 
1683 	ocfs2_inode_unlock(inode, 0);
1684 	status = 0;
1685 bail:
1686 	iput(inode);
1687 
1688 	if (status)
1689 		mlog_errno(status);
1690 
1691 	return status;
1692 }
1693 
1694 static void ocfs2_inode_init_once(void *data)
1695 {
1696 	struct ocfs2_inode_info *oi = data;
1697 
1698 	oi->ip_flags = 0;
1699 	oi->ip_open_count = 0;
1700 	spin_lock_init(&oi->ip_lock);
1701 	ocfs2_extent_map_init(&oi->vfs_inode);
1702 	INIT_LIST_HEAD(&oi->ip_io_markers);
1703 	INIT_LIST_HEAD(&oi->ip_unwritten_list);
1704 	oi->ip_dir_start_lookup = 0;
1705 	init_rwsem(&oi->ip_alloc_sem);
1706 	init_rwsem(&oi->ip_xattr_sem);
1707 	mutex_init(&oi->ip_io_mutex);
1708 
1709 	oi->ip_blkno = 0ULL;
1710 	oi->ip_clusters = 0;
1711 	oi->ip_next_orphan = NULL;
1712 
1713 	ocfs2_resv_init_once(&oi->ip_la_data_resv);
1714 
1715 	ocfs2_lock_res_init_once(&oi->ip_rw_lockres);
1716 	ocfs2_lock_res_init_once(&oi->ip_inode_lockres);
1717 	ocfs2_lock_res_init_once(&oi->ip_open_lockres);
1718 
1719 	ocfs2_metadata_cache_init(INODE_CACHE(&oi->vfs_inode),
1720 				  &ocfs2_inode_caching_ops);
1721 
1722 	inode_init_once(&oi->vfs_inode);
1723 }
1724 
1725 static int ocfs2_initialize_mem_caches(void)
1726 {
1727 	ocfs2_inode_cachep = kmem_cache_create("ocfs2_inode_cache",
1728 				       sizeof(struct ocfs2_inode_info),
1729 				       0,
1730 				       (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1731 						SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1732 				       ocfs2_inode_init_once);
1733 	ocfs2_dquot_cachep = kmem_cache_create("ocfs2_dquot_cache",
1734 					sizeof(struct ocfs2_dquot),
1735 					0,
1736 					(SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1737 						SLAB_MEM_SPREAD),
1738 					NULL);
1739 	ocfs2_qf_chunk_cachep = kmem_cache_create("ocfs2_qf_chunk_cache",
1740 					sizeof(struct ocfs2_quota_chunk),
1741 					0,
1742 					(SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
1743 					NULL);
1744 	if (!ocfs2_inode_cachep || !ocfs2_dquot_cachep ||
1745 	    !ocfs2_qf_chunk_cachep) {
1746 		kmem_cache_destroy(ocfs2_inode_cachep);
1747 		kmem_cache_destroy(ocfs2_dquot_cachep);
1748 		kmem_cache_destroy(ocfs2_qf_chunk_cachep);
1749 		return -ENOMEM;
1750 	}
1751 
1752 	return 0;
1753 }
1754 
1755 static void ocfs2_free_mem_caches(void)
1756 {
1757 	/*
1758 	 * Make sure all delayed rcu free inodes are flushed before we
1759 	 * destroy cache.
1760 	 */
1761 	rcu_barrier();
1762 	kmem_cache_destroy(ocfs2_inode_cachep);
1763 	ocfs2_inode_cachep = NULL;
1764 
1765 	kmem_cache_destroy(ocfs2_dquot_cachep);
1766 	ocfs2_dquot_cachep = NULL;
1767 
1768 	kmem_cache_destroy(ocfs2_qf_chunk_cachep);
1769 	ocfs2_qf_chunk_cachep = NULL;
1770 }
1771 
1772 static int ocfs2_get_sector(struct super_block *sb,
1773 			    struct buffer_head **bh,
1774 			    int block,
1775 			    int sect_size)
1776 {
1777 	if (!sb_set_blocksize(sb, sect_size)) {
1778 		mlog(ML_ERROR, "unable to set blocksize\n");
1779 		return -EIO;
1780 	}
1781 
1782 	*bh = sb_getblk(sb, block);
1783 	if (!*bh) {
1784 		mlog_errno(-ENOMEM);
1785 		return -ENOMEM;
1786 	}
1787 	lock_buffer(*bh);
1788 	if (!buffer_dirty(*bh))
1789 		clear_buffer_uptodate(*bh);
1790 	unlock_buffer(*bh);
1791 	ll_rw_block(REQ_OP_READ, 0, 1, bh);
1792 	wait_on_buffer(*bh);
1793 	if (!buffer_uptodate(*bh)) {
1794 		mlog_errno(-EIO);
1795 		brelse(*bh);
1796 		*bh = NULL;
1797 		return -EIO;
1798 	}
1799 
1800 	return 0;
1801 }
1802 
1803 static int ocfs2_mount_volume(struct super_block *sb)
1804 {
1805 	int status = 0;
1806 	int unlock_super = 0;
1807 	struct ocfs2_super *osb = OCFS2_SB(sb);
1808 
1809 	if (ocfs2_is_hard_readonly(osb))
1810 		goto leave;
1811 
1812 	mutex_init(&osb->obs_trim_fs_mutex);
1813 
1814 	status = ocfs2_dlm_init(osb);
1815 	if (status < 0) {
1816 		mlog_errno(status);
1817 		if (status == -EBADR && ocfs2_userspace_stack(osb))
1818 			mlog(ML_ERROR, "couldn't mount because cluster name on"
1819 			" disk does not match the running cluster name.\n");
1820 		goto leave;
1821 	}
1822 
1823 	status = ocfs2_super_lock(osb, 1);
1824 	if (status < 0) {
1825 		mlog_errno(status);
1826 		goto leave;
1827 	}
1828 	unlock_super = 1;
1829 
1830 	/* This will load up the node map and add ourselves to it. */
1831 	status = ocfs2_find_slot(osb);
1832 	if (status < 0) {
1833 		mlog_errno(status);
1834 		goto leave;
1835 	}
1836 
1837 	/* load all node-local system inodes */
1838 	status = ocfs2_init_local_system_inodes(osb);
1839 	if (status < 0) {
1840 		mlog_errno(status);
1841 		goto leave;
1842 	}
1843 
1844 	status = ocfs2_check_volume(osb);
1845 	if (status < 0) {
1846 		mlog_errno(status);
1847 		goto leave;
1848 	}
1849 
1850 	status = ocfs2_truncate_log_init(osb);
1851 	if (status < 0)
1852 		mlog_errno(status);
1853 
1854 leave:
1855 	if (unlock_super)
1856 		ocfs2_super_unlock(osb, 1);
1857 
1858 	return status;
1859 }
1860 
1861 static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
1862 {
1863 	int tmp, hangup_needed = 0;
1864 	struct ocfs2_super *osb = NULL;
1865 	char nodestr[12];
1866 
1867 	trace_ocfs2_dismount_volume(sb);
1868 
1869 	BUG_ON(!sb);
1870 	osb = OCFS2_SB(sb);
1871 	BUG_ON(!osb);
1872 
1873 	/* Remove file check sysfs related directores/files,
1874 	 * and wait for the pending file check operations */
1875 	ocfs2_filecheck_remove_sysfs(osb);
1876 
1877 	kset_unregister(osb->osb_dev_kset);
1878 
1879 	/* Orphan scan should be stopped as early as possible */
1880 	ocfs2_orphan_scan_stop(osb);
1881 
1882 	ocfs2_disable_quotas(osb);
1883 
1884 	/* All dquots should be freed by now */
1885 	WARN_ON(!llist_empty(&osb->dquot_drop_list));
1886 	/* Wait for worker to be done with the work structure in osb */
1887 	cancel_work_sync(&osb->dquot_drop_work);
1888 
1889 	ocfs2_shutdown_local_alloc(osb);
1890 
1891 	ocfs2_truncate_log_shutdown(osb);
1892 
1893 	/* This will disable recovery and flush any recovery work. */
1894 	ocfs2_recovery_exit(osb);
1895 
1896 	ocfs2_sync_blockdev(sb);
1897 
1898 	ocfs2_purge_refcount_trees(osb);
1899 
1900 	/* No cluster connection means we've failed during mount, so skip
1901 	 * all the steps which depended on that to complete. */
1902 	if (osb->cconn) {
1903 		tmp = ocfs2_super_lock(osb, 1);
1904 		if (tmp < 0) {
1905 			mlog_errno(tmp);
1906 			return;
1907 		}
1908 	}
1909 
1910 	if (osb->slot_num != OCFS2_INVALID_SLOT)
1911 		ocfs2_put_slot(osb);
1912 
1913 	if (osb->cconn)
1914 		ocfs2_super_unlock(osb, 1);
1915 
1916 	ocfs2_release_system_inodes(osb);
1917 
1918 	ocfs2_journal_shutdown(osb);
1919 
1920 	/*
1921 	 * If we're dismounting due to mount error, mount.ocfs2 will clean
1922 	 * up heartbeat.  If we're a local mount, there is no heartbeat.
1923 	 * If we failed before we got a uuid_str yet, we can't stop
1924 	 * heartbeat.  Otherwise, do it.
1925 	 */
1926 	if (!mnt_err && !ocfs2_mount_local(osb) && osb->uuid_str &&
1927 	    !ocfs2_is_hard_readonly(osb))
1928 		hangup_needed = 1;
1929 
1930 	if (osb->cconn)
1931 		ocfs2_dlm_shutdown(osb, hangup_needed);
1932 
1933 	ocfs2_blockcheck_stats_debugfs_remove(&osb->osb_ecc_stats);
1934 	debugfs_remove_recursive(osb->osb_debug_root);
1935 
1936 	if (hangup_needed)
1937 		ocfs2_cluster_hangup(osb->uuid_str, strlen(osb->uuid_str));
1938 
1939 	atomic_set(&osb->vol_state, VOLUME_DISMOUNTED);
1940 
1941 	if (ocfs2_mount_local(osb))
1942 		snprintf(nodestr, sizeof(nodestr), "local");
1943 	else
1944 		snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
1945 
1946 	printk(KERN_INFO "ocfs2: Unmounting device (%s) on (node %s)\n",
1947 	       osb->dev_str, nodestr);
1948 
1949 	ocfs2_delete_osb(osb);
1950 	kfree(osb);
1951 	sb->s_dev = 0;
1952 	sb->s_fs_info = NULL;
1953 }
1954 
1955 static int ocfs2_setup_osb_uuid(struct ocfs2_super *osb, const unsigned char *uuid,
1956 				unsigned uuid_bytes)
1957 {
1958 	int i, ret;
1959 	char *ptr;
1960 
1961 	BUG_ON(uuid_bytes != OCFS2_VOL_UUID_LEN);
1962 
1963 	osb->uuid_str = kzalloc(OCFS2_VOL_UUID_LEN * 2 + 1, GFP_KERNEL);
1964 	if (osb->uuid_str == NULL)
1965 		return -ENOMEM;
1966 
1967 	for (i = 0, ptr = osb->uuid_str; i < OCFS2_VOL_UUID_LEN; i++) {
1968 		/* print with null */
1969 		ret = snprintf(ptr, 3, "%02X", uuid[i]);
1970 		if (ret != 2) /* drop super cleans up */
1971 			return -EINVAL;
1972 		/* then only advance past the last char */
1973 		ptr += 2;
1974 	}
1975 
1976 	return 0;
1977 }
1978 
1979 /* Make sure entire volume is addressable by our journal.  Requires
1980    osb_clusters_at_boot to be valid and for the journal to have been
1981    initialized by ocfs2_journal_init(). */
1982 static int ocfs2_journal_addressable(struct ocfs2_super *osb)
1983 {
1984 	int status = 0;
1985 	u64 max_block =
1986 		ocfs2_clusters_to_blocks(osb->sb,
1987 					 osb->osb_clusters_at_boot) - 1;
1988 
1989 	/* 32-bit block number is always OK. */
1990 	if (max_block <= (u32)~0ULL)
1991 		goto out;
1992 
1993 	/* Volume is "huge", so see if our journal is new enough to
1994 	   support it. */
1995 	if (!(OCFS2_HAS_COMPAT_FEATURE(osb->sb,
1996 				       OCFS2_FEATURE_COMPAT_JBD2_SB) &&
1997 	      jbd2_journal_check_used_features(osb->journal->j_journal, 0, 0,
1998 					       JBD2_FEATURE_INCOMPAT_64BIT))) {
1999 		mlog(ML_ERROR, "The journal cannot address the entire volume. "
2000 		     "Enable the 'block64' journal option with tunefs.ocfs2");
2001 		status = -EFBIG;
2002 		goto out;
2003 	}
2004 
2005  out:
2006 	return status;
2007 }
2008 
2009 static int ocfs2_initialize_super(struct super_block *sb,
2010 				  struct buffer_head *bh,
2011 				  int sector_size,
2012 				  struct ocfs2_blockcheck_stats *stats)
2013 {
2014 	int status;
2015 	int i, cbits, bbits;
2016 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2017 	struct inode *inode = NULL;
2018 	struct ocfs2_super *osb;
2019 	u64 total_blocks;
2020 
2021 	osb = kzalloc(sizeof(struct ocfs2_super), GFP_KERNEL);
2022 	if (!osb) {
2023 		status = -ENOMEM;
2024 		mlog_errno(status);
2025 		goto bail;
2026 	}
2027 
2028 	sb->s_fs_info = osb;
2029 	sb->s_op = &ocfs2_sops;
2030 	sb->s_d_op = &ocfs2_dentry_ops;
2031 	sb->s_export_op = &ocfs2_export_ops;
2032 	sb->s_qcop = &dquot_quotactl_sysfile_ops;
2033 	sb->dq_op = &ocfs2_quota_operations;
2034 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
2035 	sb->s_xattr = ocfs2_xattr_handlers;
2036 	sb->s_time_gran = 1;
2037 	sb->s_flags |= SB_NOATIME;
2038 	/* this is needed to support O_LARGEFILE */
2039 	cbits = le32_to_cpu(di->id2.i_super.s_clustersize_bits);
2040 	bbits = le32_to_cpu(di->id2.i_super.s_blocksize_bits);
2041 	sb->s_maxbytes = ocfs2_max_file_offset(bbits, cbits);
2042 	memcpy(&sb->s_uuid, di->id2.i_super.s_uuid,
2043 	       sizeof(di->id2.i_super.s_uuid));
2044 
2045 	osb->osb_dx_mask = (1 << (cbits - bbits)) - 1;
2046 
2047 	for (i = 0; i < 3; i++)
2048 		osb->osb_dx_seed[i] = le32_to_cpu(di->id2.i_super.s_dx_seed[i]);
2049 	osb->osb_dx_seed[3] = le32_to_cpu(di->id2.i_super.s_uuid_hash);
2050 
2051 	osb->sb = sb;
2052 	osb->s_sectsize_bits = blksize_bits(sector_size);
2053 	BUG_ON(!osb->s_sectsize_bits);
2054 
2055 	spin_lock_init(&osb->dc_task_lock);
2056 	init_waitqueue_head(&osb->dc_event);
2057 	osb->dc_work_sequence = 0;
2058 	osb->dc_wake_sequence = 0;
2059 	INIT_LIST_HEAD(&osb->blocked_lock_list);
2060 	osb->blocked_lock_count = 0;
2061 	spin_lock_init(&osb->osb_lock);
2062 	spin_lock_init(&osb->osb_xattr_lock);
2063 	ocfs2_init_steal_slots(osb);
2064 
2065 	mutex_init(&osb->system_file_mutex);
2066 
2067 	atomic_set(&osb->alloc_stats.moves, 0);
2068 	atomic_set(&osb->alloc_stats.local_data, 0);
2069 	atomic_set(&osb->alloc_stats.bitmap_data, 0);
2070 	atomic_set(&osb->alloc_stats.bg_allocs, 0);
2071 	atomic_set(&osb->alloc_stats.bg_extends, 0);
2072 
2073 	/* Copy the blockcheck stats from the superblock probe */
2074 	osb->osb_ecc_stats = *stats;
2075 
2076 	ocfs2_init_node_maps(osb);
2077 
2078 	snprintf(osb->dev_str, sizeof(osb->dev_str), "%u,%u",
2079 		 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
2080 
2081 	osb->max_slots = le16_to_cpu(di->id2.i_super.s_max_slots);
2082 	if (osb->max_slots > OCFS2_MAX_SLOTS || osb->max_slots == 0) {
2083 		mlog(ML_ERROR, "Invalid number of node slots (%u)\n",
2084 		     osb->max_slots);
2085 		status = -EINVAL;
2086 		goto bail;
2087 	}
2088 
2089 	ocfs2_orphan_scan_init(osb);
2090 
2091 	status = ocfs2_recovery_init(osb);
2092 	if (status) {
2093 		mlog(ML_ERROR, "Unable to initialize recovery state\n");
2094 		mlog_errno(status);
2095 		goto bail;
2096 	}
2097 
2098 	init_waitqueue_head(&osb->checkpoint_event);
2099 
2100 	osb->s_atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
2101 
2102 	osb->slot_num = OCFS2_INVALID_SLOT;
2103 
2104 	osb->s_xattr_inline_size = le16_to_cpu(
2105 					di->id2.i_super.s_xattr_inline_size);
2106 
2107 	osb->local_alloc_state = OCFS2_LA_UNUSED;
2108 	osb->local_alloc_bh = NULL;
2109 	INIT_DELAYED_WORK(&osb->la_enable_wq, ocfs2_la_enable_worker);
2110 
2111 	init_waitqueue_head(&osb->osb_mount_event);
2112 
2113 	status = ocfs2_resmap_init(osb, &osb->osb_la_resmap);
2114 	if (status) {
2115 		mlog_errno(status);
2116 		goto bail;
2117 	}
2118 
2119 	osb->vol_label = kmalloc(OCFS2_MAX_VOL_LABEL_LEN, GFP_KERNEL);
2120 	if (!osb->vol_label) {
2121 		mlog(ML_ERROR, "unable to alloc vol label\n");
2122 		status = -ENOMEM;
2123 		goto bail;
2124 	}
2125 
2126 	osb->slot_recovery_generations =
2127 		kcalloc(osb->max_slots, sizeof(*osb->slot_recovery_generations),
2128 			GFP_KERNEL);
2129 	if (!osb->slot_recovery_generations) {
2130 		status = -ENOMEM;
2131 		mlog_errno(status);
2132 		goto bail;
2133 	}
2134 
2135 	init_waitqueue_head(&osb->osb_wipe_event);
2136 	osb->osb_orphan_wipes = kcalloc(osb->max_slots,
2137 					sizeof(*osb->osb_orphan_wipes),
2138 					GFP_KERNEL);
2139 	if (!osb->osb_orphan_wipes) {
2140 		status = -ENOMEM;
2141 		mlog_errno(status);
2142 		goto bail;
2143 	}
2144 
2145 	osb->osb_rf_lock_tree = RB_ROOT;
2146 
2147 	osb->s_feature_compat =
2148 		le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_compat);
2149 	osb->s_feature_ro_compat =
2150 		le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_ro_compat);
2151 	osb->s_feature_incompat =
2152 		le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_incompat);
2153 
2154 	if ((i = OCFS2_HAS_INCOMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_INCOMPAT_SUPP))) {
2155 		mlog(ML_ERROR, "couldn't mount because of unsupported "
2156 		     "optional features (%x).\n", i);
2157 		status = -EINVAL;
2158 		goto bail;
2159 	}
2160 	if (!sb_rdonly(osb->sb) && (i = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP))) {
2161 		mlog(ML_ERROR, "couldn't mount RDWR because of "
2162 		     "unsupported optional features (%x).\n", i);
2163 		status = -EINVAL;
2164 		goto bail;
2165 	}
2166 
2167 	if (ocfs2_clusterinfo_valid(osb)) {
2168 		/*
2169 		 * ci_stack and ci_cluster in ocfs2_cluster_info may not be null
2170 		 * terminated, so make sure no overflow happens here by using
2171 		 * memcpy. Destination strings will always be null terminated
2172 		 * because osb is allocated using kzalloc.
2173 		 */
2174 		osb->osb_stackflags =
2175 			OCFS2_RAW_SB(di)->s_cluster_info.ci_stackflags;
2176 		memcpy(osb->osb_cluster_stack,
2177 		       OCFS2_RAW_SB(di)->s_cluster_info.ci_stack,
2178 		       OCFS2_STACK_LABEL_LEN);
2179 		if (strlen(osb->osb_cluster_stack) != OCFS2_STACK_LABEL_LEN) {
2180 			mlog(ML_ERROR,
2181 			     "couldn't mount because of an invalid "
2182 			     "cluster stack label (%s) \n",
2183 			     osb->osb_cluster_stack);
2184 			status = -EINVAL;
2185 			goto bail;
2186 		}
2187 		memcpy(osb->osb_cluster_name,
2188 			OCFS2_RAW_SB(di)->s_cluster_info.ci_cluster,
2189 			OCFS2_CLUSTER_NAME_LEN);
2190 	} else {
2191 		/* The empty string is identical with classic tools that
2192 		 * don't know about s_cluster_info. */
2193 		osb->osb_cluster_stack[0] = '\0';
2194 	}
2195 
2196 	get_random_bytes(&osb->s_next_generation, sizeof(u32));
2197 
2198 	INIT_WORK(&osb->dquot_drop_work, ocfs2_drop_dquot_refs);
2199 	init_llist_head(&osb->dquot_drop_list);
2200 
2201 	/* get some pseudo constants for clustersize bits */
2202 	osb->s_clustersize_bits =
2203 		le32_to_cpu(di->id2.i_super.s_clustersize_bits);
2204 	osb->s_clustersize = 1 << osb->s_clustersize_bits;
2205 
2206 	if (osb->s_clustersize < OCFS2_MIN_CLUSTERSIZE ||
2207 	    osb->s_clustersize > OCFS2_MAX_CLUSTERSIZE) {
2208 		mlog(ML_ERROR, "Volume has invalid cluster size (%d)\n",
2209 		     osb->s_clustersize);
2210 		status = -EINVAL;
2211 		goto bail;
2212 	}
2213 
2214 	total_blocks = ocfs2_clusters_to_blocks(osb->sb,
2215 						le32_to_cpu(di->i_clusters));
2216 
2217 	status = generic_check_addressable(osb->sb->s_blocksize_bits,
2218 					   total_blocks);
2219 	if (status) {
2220 		mlog(ML_ERROR, "Volume too large "
2221 		     "to mount safely on this system");
2222 		status = -EFBIG;
2223 		goto bail;
2224 	}
2225 
2226 	if (ocfs2_setup_osb_uuid(osb, di->id2.i_super.s_uuid,
2227 				 sizeof(di->id2.i_super.s_uuid))) {
2228 		mlog(ML_ERROR, "Out of memory trying to setup our uuid.\n");
2229 		status = -ENOMEM;
2230 		goto bail;
2231 	}
2232 
2233 	strlcpy(osb->vol_label, di->id2.i_super.s_label,
2234 		OCFS2_MAX_VOL_LABEL_LEN);
2235 	osb->root_blkno = le64_to_cpu(di->id2.i_super.s_root_blkno);
2236 	osb->system_dir_blkno = le64_to_cpu(di->id2.i_super.s_system_dir_blkno);
2237 	osb->first_cluster_group_blkno =
2238 		le64_to_cpu(di->id2.i_super.s_first_cluster_group);
2239 	osb->fs_generation = le32_to_cpu(di->i_fs_generation);
2240 	osb->uuid_hash = le32_to_cpu(di->id2.i_super.s_uuid_hash);
2241 	trace_ocfs2_initialize_super(osb->vol_label, osb->uuid_str,
2242 				     (unsigned long long)osb->root_blkno,
2243 				     (unsigned long long)osb->system_dir_blkno,
2244 				     osb->s_clustersize_bits);
2245 
2246 	osb->osb_dlm_debug = ocfs2_new_dlm_debug();
2247 	if (!osb->osb_dlm_debug) {
2248 		status = -ENOMEM;
2249 		mlog_errno(status);
2250 		goto bail;
2251 	}
2252 
2253 	atomic_set(&osb->vol_state, VOLUME_INIT);
2254 
2255 	/* load root, system_dir, and all global system inodes */
2256 	status = ocfs2_init_global_system_inodes(osb);
2257 	if (status < 0) {
2258 		mlog_errno(status);
2259 		goto bail;
2260 	}
2261 
2262 	/*
2263 	 * global bitmap
2264 	 */
2265 	inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
2266 					    OCFS2_INVALID_SLOT);
2267 	if (!inode) {
2268 		status = -EINVAL;
2269 		mlog_errno(status);
2270 		goto bail;
2271 	}
2272 
2273 	osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
2274 	osb->osb_clusters_at_boot = OCFS2_I(inode)->ip_clusters;
2275 	iput(inode);
2276 
2277 	osb->bitmap_cpg = ocfs2_group_bitmap_size(sb, 0,
2278 				 osb->s_feature_incompat) * 8;
2279 
2280 	status = ocfs2_init_slot_info(osb);
2281 	if (status < 0) {
2282 		mlog_errno(status);
2283 		goto bail;
2284 	}
2285 
2286 	osb->ocfs2_wq = alloc_ordered_workqueue("ocfs2_wq", WQ_MEM_RECLAIM);
2287 	if (!osb->ocfs2_wq) {
2288 		status = -ENOMEM;
2289 		mlog_errno(status);
2290 	}
2291 
2292 bail:
2293 	return status;
2294 }
2295 
2296 /*
2297  * will return: -EAGAIN if it is ok to keep searching for superblocks
2298  *              -EINVAL if there is a bad superblock
2299  *              0 on success
2300  */
2301 static int ocfs2_verify_volume(struct ocfs2_dinode *di,
2302 			       struct buffer_head *bh,
2303 			       u32 blksz,
2304 			       struct ocfs2_blockcheck_stats *stats)
2305 {
2306 	int status = -EAGAIN;
2307 
2308 	if (memcmp(di->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
2309 		   strlen(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) {
2310 		/* We have to do a raw check of the feature here */
2311 		if (le32_to_cpu(di->id2.i_super.s_feature_incompat) &
2312 		    OCFS2_FEATURE_INCOMPAT_META_ECC) {
2313 			status = ocfs2_block_check_validate(bh->b_data,
2314 							    bh->b_size,
2315 							    &di->i_check,
2316 							    stats);
2317 			if (status)
2318 				goto out;
2319 		}
2320 		status = -EINVAL;
2321 		if ((1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits)) != blksz) {
2322 			mlog(ML_ERROR, "found superblock with incorrect block "
2323 			     "size: found %u, should be %u\n",
2324 			     1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits),
2325 			       blksz);
2326 		} else if (le16_to_cpu(di->id2.i_super.s_major_rev_level) !=
2327 			   OCFS2_MAJOR_REV_LEVEL ||
2328 			   le16_to_cpu(di->id2.i_super.s_minor_rev_level) !=
2329 			   OCFS2_MINOR_REV_LEVEL) {
2330 			mlog(ML_ERROR, "found superblock with bad version: "
2331 			     "found %u.%u, should be %u.%u\n",
2332 			     le16_to_cpu(di->id2.i_super.s_major_rev_level),
2333 			     le16_to_cpu(di->id2.i_super.s_minor_rev_level),
2334 			     OCFS2_MAJOR_REV_LEVEL,
2335 			     OCFS2_MINOR_REV_LEVEL);
2336 		} else if (bh->b_blocknr != le64_to_cpu(di->i_blkno)) {
2337 			mlog(ML_ERROR, "bad block number on superblock: "
2338 			     "found %llu, should be %llu\n",
2339 			     (unsigned long long)le64_to_cpu(di->i_blkno),
2340 			     (unsigned long long)bh->b_blocknr);
2341 		} else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
2342 			    le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
2343 			mlog(ML_ERROR, "bad cluster size found: %u\n",
2344 			     1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
2345 		} else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
2346 			mlog(ML_ERROR, "bad root_blkno: 0\n");
2347 		} else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
2348 			mlog(ML_ERROR, "bad system_dir_blkno: 0\n");
2349 		} else if (le16_to_cpu(di->id2.i_super.s_max_slots) > OCFS2_MAX_SLOTS) {
2350 			mlog(ML_ERROR,
2351 			     "Superblock slots found greater than file system "
2352 			     "maximum: found %u, max %u\n",
2353 			     le16_to_cpu(di->id2.i_super.s_max_slots),
2354 			     OCFS2_MAX_SLOTS);
2355 		} else {
2356 			/* found it! */
2357 			status = 0;
2358 		}
2359 	}
2360 
2361 out:
2362 	if (status && status != -EAGAIN)
2363 		mlog_errno(status);
2364 	return status;
2365 }
2366 
2367 static int ocfs2_check_volume(struct ocfs2_super *osb)
2368 {
2369 	int status;
2370 	int dirty;
2371 	int local;
2372 	struct ocfs2_dinode *local_alloc = NULL; /* only used if we
2373 						  * recover
2374 						  * ourselves. */
2375 
2376 	/* Init our journal object. */
2377 	status = ocfs2_journal_init(osb, &dirty);
2378 	if (status < 0) {
2379 		mlog(ML_ERROR, "Could not initialize journal!\n");
2380 		goto finally;
2381 	}
2382 
2383 	/* Now that journal has been initialized, check to make sure
2384 	   entire volume is addressable. */
2385 	status = ocfs2_journal_addressable(osb);
2386 	if (status)
2387 		goto finally;
2388 
2389 	/* If the journal was unmounted cleanly then we don't want to
2390 	 * recover anything. Otherwise, journal_load will do that
2391 	 * dirty work for us :) */
2392 	if (!dirty) {
2393 		status = ocfs2_journal_wipe(osb->journal, 0);
2394 		if (status < 0) {
2395 			mlog_errno(status);
2396 			goto finally;
2397 		}
2398 	} else {
2399 		printk(KERN_NOTICE "ocfs2: File system on device (%s) was not "
2400 		       "unmounted cleanly, recovering it.\n", osb->dev_str);
2401 	}
2402 
2403 	local = ocfs2_mount_local(osb);
2404 
2405 	/* will play back anything left in the journal. */
2406 	status = ocfs2_journal_load(osb->journal, local, dirty);
2407 	if (status < 0) {
2408 		mlog(ML_ERROR, "ocfs2 journal load failed! %d\n", status);
2409 		goto finally;
2410 	}
2411 
2412 	if (osb->s_mount_opt & OCFS2_MOUNT_JOURNAL_ASYNC_COMMIT)
2413 		jbd2_journal_set_features(osb->journal->j_journal,
2414 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2415 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2416 	else
2417 		jbd2_journal_clear_features(osb->journal->j_journal,
2418 				JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2419 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2420 
2421 	if (dirty) {
2422 		/* recover my local alloc if we didn't unmount cleanly. */
2423 		status = ocfs2_begin_local_alloc_recovery(osb,
2424 							  osb->slot_num,
2425 							  &local_alloc);
2426 		if (status < 0) {
2427 			mlog_errno(status);
2428 			goto finally;
2429 		}
2430 		/* we complete the recovery process after we've marked
2431 		 * ourselves as mounted. */
2432 	}
2433 
2434 	status = ocfs2_load_local_alloc(osb);
2435 	if (status < 0) {
2436 		mlog_errno(status);
2437 		goto finally;
2438 	}
2439 
2440 	if (dirty) {
2441 		/* Recovery will be completed after we've mounted the
2442 		 * rest of the volume. */
2443 		osb->local_alloc_copy = local_alloc;
2444 		local_alloc = NULL;
2445 	}
2446 
2447 	/* go through each journal, trylock it and if you get the
2448 	 * lock, and it's marked as dirty, set the bit in the recover
2449 	 * map and launch a recovery thread for it. */
2450 	status = ocfs2_mark_dead_nodes(osb);
2451 	if (status < 0) {
2452 		mlog_errno(status);
2453 		goto finally;
2454 	}
2455 
2456 	status = ocfs2_compute_replay_slots(osb);
2457 	if (status < 0)
2458 		mlog_errno(status);
2459 
2460 finally:
2461 	kfree(local_alloc);
2462 
2463 	if (status)
2464 		mlog_errno(status);
2465 	return status;
2466 }
2467 
2468 /*
2469  * The routine gets called from dismount or close whenever a dismount on
2470  * volume is requested and the osb open count becomes 1.
2471  * It will remove the osb from the global list and also free up all the
2472  * initialized resources and fileobject.
2473  */
2474 static void ocfs2_delete_osb(struct ocfs2_super *osb)
2475 {
2476 	/* This function assumes that the caller has the main osb resource */
2477 
2478 	/* ocfs2_initializer_super have already created this workqueue */
2479 	if (osb->ocfs2_wq)
2480 		destroy_workqueue(osb->ocfs2_wq);
2481 
2482 	ocfs2_free_slot_info(osb);
2483 
2484 	kfree(osb->osb_orphan_wipes);
2485 	kfree(osb->slot_recovery_generations);
2486 	kfree(osb->local_alloc_copy);
2487 	kfree(osb->uuid_str);
2488 	kfree(osb->vol_label);
2489 	ocfs2_put_dlm_debug(osb->osb_dlm_debug);
2490 	memset(osb, 0, sizeof(struct ocfs2_super));
2491 }
2492 
2493 /* Depending on the mount option passed, perform one of the following:
2494  * Put OCFS2 into a readonly state (default)
2495  * Return EIO so that only the process errs
2496  * Fix the error as if fsck.ocfs2 -y
2497  * panic
2498  */
2499 static int ocfs2_handle_error(struct super_block *sb)
2500 {
2501 	struct ocfs2_super *osb = OCFS2_SB(sb);
2502 	int rv = 0;
2503 
2504 	ocfs2_set_osb_flag(osb, OCFS2_OSB_ERROR_FS);
2505 	pr_crit("On-disk corruption discovered. "
2506 		"Please run fsck.ocfs2 once the filesystem is unmounted.\n");
2507 
2508 	if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_PANIC) {
2509 		panic("OCFS2: (device %s): panic forced after error\n",
2510 		      sb->s_id);
2511 	} else if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_CONT) {
2512 		pr_crit("OCFS2: Returning error to the calling process.\n");
2513 		rv = -EIO;
2514 	} else { /* default option */
2515 		rv = -EROFS;
2516 		if (sb_rdonly(sb) && (ocfs2_is_soft_readonly(osb) || ocfs2_is_hard_readonly(osb)))
2517 			return rv;
2518 
2519 		pr_crit("OCFS2: File system is now read-only.\n");
2520 		sb->s_flags |= SB_RDONLY;
2521 		ocfs2_set_ro_flag(osb, 0);
2522 	}
2523 
2524 	return rv;
2525 }
2526 
2527 int __ocfs2_error(struct super_block *sb, const char *function,
2528 		  const char *fmt, ...)
2529 {
2530 	struct va_format vaf;
2531 	va_list args;
2532 
2533 	va_start(args, fmt);
2534 	vaf.fmt = fmt;
2535 	vaf.va = &args;
2536 
2537 	/* Not using mlog here because we want to show the actual
2538 	 * function the error came from. */
2539 	printk(KERN_CRIT "OCFS2: ERROR (device %s): %s: %pV",
2540 	       sb->s_id, function, &vaf);
2541 
2542 	va_end(args);
2543 
2544 	return ocfs2_handle_error(sb);
2545 }
2546 
2547 /* Handle critical errors. This is intentionally more drastic than
2548  * ocfs2_handle_error, so we only use for things like journal errors,
2549  * etc. */
2550 void __ocfs2_abort(struct super_block *sb, const char *function,
2551 		   const char *fmt, ...)
2552 {
2553 	struct va_format vaf;
2554 	va_list args;
2555 
2556 	va_start(args, fmt);
2557 
2558 	vaf.fmt = fmt;
2559 	vaf.va = &args;
2560 
2561 	printk(KERN_CRIT "OCFS2: abort (device %s): %s: %pV",
2562 	       sb->s_id, function, &vaf);
2563 
2564 	va_end(args);
2565 
2566 	/* We don't have the cluster support yet to go straight to
2567 	 * hard readonly in here. Until then, we want to keep
2568 	 * ocfs2_abort() so that we can at least mark critical
2569 	 * errors.
2570 	 *
2571 	 * TODO: This should abort the journal and alert other nodes
2572 	 * that our slot needs recovery. */
2573 
2574 	/* Force a panic(). This stinks, but it's better than letting
2575 	 * things continue without having a proper hard readonly
2576 	 * here. */
2577 	if (!ocfs2_mount_local(OCFS2_SB(sb)))
2578 		OCFS2_SB(sb)->s_mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
2579 	ocfs2_handle_error(sb);
2580 }
2581 
2582 /*
2583  * Void signal blockers, because in-kernel sigprocmask() only fails
2584  * when SIG_* is wrong.
2585  */
2586 void ocfs2_block_signals(sigset_t *oldset)
2587 {
2588 	int rc;
2589 	sigset_t blocked;
2590 
2591 	sigfillset(&blocked);
2592 	rc = sigprocmask(SIG_BLOCK, &blocked, oldset);
2593 	BUG_ON(rc);
2594 }
2595 
2596 void ocfs2_unblock_signals(sigset_t *oldset)
2597 {
2598 	int rc = sigprocmask(SIG_SETMASK, oldset, NULL);
2599 	BUG_ON(rc);
2600 }
2601 
2602 module_init(ocfs2_init);
2603 module_exit(ocfs2_exit);
2604