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