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