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