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