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