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