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