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