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