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