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