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