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