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