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