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