Merge branch 'for-2.6.34' of git://linux-nfs.org/~bfields/linux
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ocfs2 / super.c
blobdee03197a4940b62b985f3e68d74c358aa214379
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * super.c
6 * load/unload driver, mount/dismount volumes
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
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.
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.
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.
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>
46 #define MLOG_MASK_PREFIX ML_SUPER
47 #include <cluster/masklog.h>
49 #include "ocfs2.h"
51 /* this should be the only file to include a version 1 header */
52 #include "ocfs1_fs_compat.h"
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"
74 #include "buffer_head_io.h"
76 static struct kmem_cache *ocfs2_inode_cachep = NULL;
77 struct kmem_cache *ocfs2_dquot_cachep;
78 struct kmem_cache *ocfs2_qf_chunk_cachep;
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;
86 static struct dentry *ocfs2_debugfs_root = NULL;
88 MODULE_AUTHOR("Oracle");
89 MODULE_LICENSE("GPL");
91 struct mount_options
93 unsigned long commit_interval;
94 unsigned long mount_opt;
95 unsigned int atime_quantum;
96 signed short slot;
97 unsigned int localalloc_opt;
98 char cluster_stack[OCFS2_STACK_LABEL_LEN + 1];
101 static int ocfs2_parse_options(struct super_block *sb, char *options,
102 struct mount_options *mopt,
103 int is_remount);
104 static int ocfs2_check_set_options(struct super_block *sb,
105 struct mount_options *options);
106 static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt);
107 static void ocfs2_put_super(struct super_block *sb);
108 static int ocfs2_mount_volume(struct super_block *sb);
109 static int ocfs2_remount(struct super_block *sb, int *flags, char *data);
110 static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err);
111 static int ocfs2_initialize_mem_caches(void);
112 static void ocfs2_free_mem_caches(void);
113 static void ocfs2_delete_osb(struct ocfs2_super *osb);
115 static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf);
117 static int ocfs2_sync_fs(struct super_block *sb, int wait);
119 static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb);
120 static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb);
121 static void ocfs2_release_system_inodes(struct ocfs2_super *osb);
122 static int ocfs2_check_volume(struct ocfs2_super *osb);
123 static int ocfs2_verify_volume(struct ocfs2_dinode *di,
124 struct buffer_head *bh,
125 u32 sectsize,
126 struct ocfs2_blockcheck_stats *stats);
127 static int ocfs2_initialize_super(struct super_block *sb,
128 struct buffer_head *bh,
129 int sector_size,
130 struct ocfs2_blockcheck_stats *stats);
131 static int ocfs2_get_sector(struct super_block *sb,
132 struct buffer_head **bh,
133 int block,
134 int sect_size);
135 static struct inode *ocfs2_alloc_inode(struct super_block *sb);
136 static void ocfs2_destroy_inode(struct inode *inode);
137 static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend);
138 static int ocfs2_enable_quotas(struct ocfs2_super *osb);
139 static void ocfs2_disable_quotas(struct ocfs2_super *osb);
141 static const struct super_operations ocfs2_sops = {
142 .statfs = ocfs2_statfs,
143 .alloc_inode = ocfs2_alloc_inode,
144 .destroy_inode = ocfs2_destroy_inode,
145 .drop_inode = ocfs2_drop_inode,
146 .clear_inode = ocfs2_clear_inode,
147 .delete_inode = ocfs2_delete_inode,
148 .sync_fs = ocfs2_sync_fs,
149 .put_super = ocfs2_put_super,
150 .remount_fs = ocfs2_remount,
151 .show_options = ocfs2_show_options,
152 .quota_read = ocfs2_quota_read,
153 .quota_write = ocfs2_quota_write,
156 enum {
157 Opt_barrier,
158 Opt_err_panic,
159 Opt_err_ro,
160 Opt_intr,
161 Opt_nointr,
162 Opt_hb_none,
163 Opt_hb_local,
164 Opt_data_ordered,
165 Opt_data_writeback,
166 Opt_atime_quantum,
167 Opt_slot,
168 Opt_commit,
169 Opt_localalloc,
170 Opt_localflocks,
171 Opt_stack,
172 Opt_user_xattr,
173 Opt_nouser_xattr,
174 Opt_inode64,
175 Opt_acl,
176 Opt_noacl,
177 Opt_usrquota,
178 Opt_grpquota,
179 Opt_err,
182 static const match_table_t tokens = {
183 {Opt_barrier, "barrier=%u"},
184 {Opt_err_panic, "errors=panic"},
185 {Opt_err_ro, "errors=remount-ro"},
186 {Opt_intr, "intr"},
187 {Opt_nointr, "nointr"},
188 {Opt_hb_none, OCFS2_HB_NONE},
189 {Opt_hb_local, OCFS2_HB_LOCAL},
190 {Opt_data_ordered, "data=ordered"},
191 {Opt_data_writeback, "data=writeback"},
192 {Opt_atime_quantum, "atime_quantum=%u"},
193 {Opt_slot, "preferred_slot=%u"},
194 {Opt_commit, "commit=%u"},
195 {Opt_localalloc, "localalloc=%d"},
196 {Opt_localflocks, "localflocks"},
197 {Opt_stack, "cluster_stack=%s"},
198 {Opt_user_xattr, "user_xattr"},
199 {Opt_nouser_xattr, "nouser_xattr"},
200 {Opt_inode64, "inode64"},
201 {Opt_acl, "acl"},
202 {Opt_noacl, "noacl"},
203 {Opt_usrquota, "usrquota"},
204 {Opt_grpquota, "grpquota"},
205 {Opt_err, NULL}
208 #ifdef CONFIG_DEBUG_FS
209 static int ocfs2_osb_dump(struct ocfs2_super *osb, char *buf, int len)
211 struct ocfs2_cluster_connection *cconn = osb->cconn;
212 struct ocfs2_recovery_map *rm = osb->recovery_map;
213 struct ocfs2_orphan_scan *os = &osb->osb_orphan_scan;
214 int i, out = 0;
216 out += snprintf(buf + out, len - out,
217 "%10s => Id: %-s Uuid: %-s Gen: 0x%X Label: %-s\n",
218 "Device", osb->dev_str, osb->uuid_str,
219 osb->fs_generation, osb->vol_label);
221 out += snprintf(buf + out, len - out,
222 "%10s => State: %d Flags: 0x%lX\n", "Volume",
223 atomic_read(&osb->vol_state), osb->osb_flags);
225 out += snprintf(buf + out, len - out,
226 "%10s => Block: %lu Cluster: %d\n", "Sizes",
227 osb->sb->s_blocksize, osb->s_clustersize);
229 out += snprintf(buf + out, len - out,
230 "%10s => Compat: 0x%X Incompat: 0x%X "
231 "ROcompat: 0x%X\n",
232 "Features", osb->s_feature_compat,
233 osb->s_feature_incompat, osb->s_feature_ro_compat);
235 out += snprintf(buf + out, len - out,
236 "%10s => Opts: 0x%lX AtimeQuanta: %u\n", "Mount",
237 osb->s_mount_opt, osb->s_atime_quantum);
239 if (cconn) {
240 out += snprintf(buf + out, len - out,
241 "%10s => Stack: %s Name: %*s "
242 "Version: %d.%d\n", "Cluster",
243 (*osb->osb_cluster_stack == '\0' ?
244 "o2cb" : osb->osb_cluster_stack),
245 cconn->cc_namelen, cconn->cc_name,
246 cconn->cc_version.pv_major,
247 cconn->cc_version.pv_minor);
250 spin_lock(&osb->dc_task_lock);
251 out += snprintf(buf + out, len - out,
252 "%10s => Pid: %d Count: %lu WakeSeq: %lu "
253 "WorkSeq: %lu\n", "DownCnvt",
254 (osb->dc_task ? task_pid_nr(osb->dc_task) : -1),
255 osb->blocked_lock_count, osb->dc_wake_sequence,
256 osb->dc_work_sequence);
257 spin_unlock(&osb->dc_task_lock);
259 spin_lock(&osb->osb_lock);
260 out += snprintf(buf + out, len - out, "%10s => Pid: %d Nodes:",
261 "Recovery",
262 (osb->recovery_thread_task ?
263 task_pid_nr(osb->recovery_thread_task) : -1));
264 if (rm->rm_used == 0)
265 out += snprintf(buf + out, len - out, " None\n");
266 else {
267 for (i = 0; i < rm->rm_used; i++)
268 out += snprintf(buf + out, len - out, " %d",
269 rm->rm_entries[i]);
270 out += snprintf(buf + out, len - out, "\n");
272 spin_unlock(&osb->osb_lock);
274 out += snprintf(buf + out, len - out,
275 "%10s => Pid: %d Interval: %lu Needs: %d\n", "Commit",
276 (osb->commit_task ? task_pid_nr(osb->commit_task) : -1),
277 osb->osb_commit_interval,
278 atomic_read(&osb->needs_checkpoint));
280 out += snprintf(buf + out, len - out,
281 "%10s => State: %d TxnId: %lu NumTxns: %d\n",
282 "Journal", osb->journal->j_state,
283 osb->journal->j_trans_id,
284 atomic_read(&osb->journal->j_num_trans));
286 out += snprintf(buf + out, len - out,
287 "%10s => GlobalAllocs: %d LocalAllocs: %d "
288 "SubAllocs: %d LAWinMoves: %d SAExtends: %d\n",
289 "Stats",
290 atomic_read(&osb->alloc_stats.bitmap_data),
291 atomic_read(&osb->alloc_stats.local_data),
292 atomic_read(&osb->alloc_stats.bg_allocs),
293 atomic_read(&osb->alloc_stats.moves),
294 atomic_read(&osb->alloc_stats.bg_extends));
296 out += snprintf(buf + out, len - out,
297 "%10s => State: %u Descriptor: %llu Size: %u bits "
298 "Default: %u bits\n",
299 "LocalAlloc", osb->local_alloc_state,
300 (unsigned long long)osb->la_last_gd,
301 osb->local_alloc_bits, osb->local_alloc_default_bits);
303 spin_lock(&osb->osb_lock);
304 out += snprintf(buf + out, len - out,
305 "%10s => InodeSlot: %d StolenInodes: %d, "
306 "MetaSlot: %d StolenMeta: %d\n", "Steal",
307 osb->s_inode_steal_slot,
308 atomic_read(&osb->s_num_inodes_stolen),
309 osb->s_meta_steal_slot,
310 atomic_read(&osb->s_num_meta_stolen));
311 spin_unlock(&osb->osb_lock);
313 out += snprintf(buf + out, len - out, "OrphanScan => ");
314 out += snprintf(buf + out, len - out, "Local: %u Global: %u ",
315 os->os_count, os->os_seqno);
316 out += snprintf(buf + out, len - out, " Last Scan: ");
317 if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE)
318 out += snprintf(buf + out, len - out, "Disabled\n");
319 else
320 out += snprintf(buf + out, len - out, "%lu seconds ago\n",
321 (get_seconds() - os->os_scantime.tv_sec));
323 out += snprintf(buf + out, len - out, "%10s => %3s %10s\n",
324 "Slots", "Num", "RecoGen");
325 for (i = 0; i < osb->max_slots; ++i) {
326 out += snprintf(buf + out, len - out,
327 "%10s %c %3d %10d\n",
328 " ",
329 (i == osb->slot_num ? '*' : ' '),
330 i, osb->slot_recovery_generations[i]);
333 return out;
336 static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
338 struct ocfs2_super *osb = inode->i_private;
339 char *buf = NULL;
341 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
342 if (!buf)
343 goto bail;
345 i_size_write(inode, ocfs2_osb_dump(osb, buf, PAGE_SIZE));
347 file->private_data = buf;
349 return 0;
350 bail:
351 return -ENOMEM;
354 static int ocfs2_debug_release(struct inode *inode, struct file *file)
356 kfree(file->private_data);
357 return 0;
360 static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
361 size_t nbytes, loff_t *ppos)
363 return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
364 i_size_read(file->f_mapping->host));
366 #else
367 static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
369 return 0;
371 static int ocfs2_debug_release(struct inode *inode, struct file *file)
373 return 0;
375 static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
376 size_t nbytes, loff_t *ppos)
378 return 0;
380 #endif /* CONFIG_DEBUG_FS */
382 static const struct file_operations ocfs2_osb_debug_fops = {
383 .open = ocfs2_osb_debug_open,
384 .release = ocfs2_debug_release,
385 .read = ocfs2_debug_read,
386 .llseek = generic_file_llseek,
389 static int ocfs2_sync_fs(struct super_block *sb, int wait)
391 int status;
392 tid_t target;
393 struct ocfs2_super *osb = OCFS2_SB(sb);
395 if (ocfs2_is_hard_readonly(osb))
396 return -EROFS;
398 if (wait) {
399 status = ocfs2_flush_truncate_log(osb);
400 if (status < 0)
401 mlog_errno(status);
402 } else {
403 ocfs2_schedule_truncate_log_flush(osb, 0);
406 if (jbd2_journal_start_commit(OCFS2_SB(sb)->journal->j_journal,
407 &target)) {
408 if (wait)
409 jbd2_log_wait_commit(OCFS2_SB(sb)->journal->j_journal,
410 target);
412 return 0;
415 static int ocfs2_need_system_inode(struct ocfs2_super *osb, int ino)
417 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_USRQUOTA)
418 && (ino == USER_QUOTA_SYSTEM_INODE
419 || ino == LOCAL_USER_QUOTA_SYSTEM_INODE))
420 return 0;
421 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)
422 && (ino == GROUP_QUOTA_SYSTEM_INODE
423 || ino == LOCAL_GROUP_QUOTA_SYSTEM_INODE))
424 return 0;
425 return 1;
428 static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb)
430 struct inode *new = NULL;
431 int status = 0;
432 int i;
434 mlog_entry_void();
436 new = ocfs2_iget(osb, osb->root_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
437 if (IS_ERR(new)) {
438 status = PTR_ERR(new);
439 mlog_errno(status);
440 goto bail;
442 osb->root_inode = new;
444 new = ocfs2_iget(osb, osb->system_dir_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
445 if (IS_ERR(new)) {
446 status = PTR_ERR(new);
447 mlog_errno(status);
448 goto bail;
450 osb->sys_root_inode = new;
452 for (i = OCFS2_FIRST_ONLINE_SYSTEM_INODE;
453 i <= OCFS2_LAST_GLOBAL_SYSTEM_INODE; i++) {
454 if (!ocfs2_need_system_inode(osb, i))
455 continue;
456 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
457 if (!new) {
458 ocfs2_release_system_inodes(osb);
459 status = -EINVAL;
460 mlog_errno(status);
461 /* FIXME: Should ERROR_RO_FS */
462 mlog(ML_ERROR, "Unable to load system inode %d, "
463 "possibly corrupt fs?", i);
464 goto bail;
466 // the array now has one ref, so drop this one
467 iput(new);
470 bail:
471 mlog_exit(status);
472 return status;
475 static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb)
477 struct inode *new = NULL;
478 int status = 0;
479 int i;
481 mlog_entry_void();
483 for (i = OCFS2_LAST_GLOBAL_SYSTEM_INODE + 1;
484 i < NUM_SYSTEM_INODES;
485 i++) {
486 if (!ocfs2_need_system_inode(osb, i))
487 continue;
488 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
489 if (!new) {
490 ocfs2_release_system_inodes(osb);
491 status = -EINVAL;
492 mlog(ML_ERROR, "status=%d, sysfile=%d, slot=%d\n",
493 status, i, osb->slot_num);
494 goto bail;
496 /* the array now has one ref, so drop this one */
497 iput(new);
500 bail:
501 mlog_exit(status);
502 return status;
505 static void ocfs2_release_system_inodes(struct ocfs2_super *osb)
507 int i;
508 struct inode *inode;
510 mlog_entry_void();
512 for (i = 0; i < NUM_SYSTEM_INODES; i++) {
513 inode = osb->system_inodes[i];
514 if (inode) {
515 iput(inode);
516 osb->system_inodes[i] = NULL;
520 inode = osb->sys_root_inode;
521 if (inode) {
522 iput(inode);
523 osb->sys_root_inode = NULL;
526 inode = osb->root_inode;
527 if (inode) {
528 iput(inode);
529 osb->root_inode = NULL;
532 mlog_exit(0);
535 /* We're allocating fs objects, use GFP_NOFS */
536 static struct inode *ocfs2_alloc_inode(struct super_block *sb)
538 struct ocfs2_inode_info *oi;
540 oi = kmem_cache_alloc(ocfs2_inode_cachep, GFP_NOFS);
541 if (!oi)
542 return NULL;
544 jbd2_journal_init_jbd_inode(&oi->ip_jinode, &oi->vfs_inode);
545 return &oi->vfs_inode;
548 static void ocfs2_destroy_inode(struct inode *inode)
550 kmem_cache_free(ocfs2_inode_cachep, OCFS2_I(inode));
553 static unsigned long long ocfs2_max_file_offset(unsigned int bbits,
554 unsigned int cbits)
556 unsigned int bytes = 1 << cbits;
557 unsigned int trim = bytes;
558 unsigned int bitshift = 32;
561 * i_size and all block offsets in ocfs2 are always 64 bits
562 * wide. i_clusters is 32 bits, in cluster-sized units. So on
563 * 64 bit platforms, cluster size will be the limiting factor.
566 #if BITS_PER_LONG == 32
567 # if defined(CONFIG_LBDAF)
568 BUILD_BUG_ON(sizeof(sector_t) != 8);
570 * We might be limited by page cache size.
572 if (bytes > PAGE_CACHE_SIZE) {
573 bytes = PAGE_CACHE_SIZE;
574 trim = 1;
576 * Shift by 31 here so that we don't get larger than
577 * MAX_LFS_FILESIZE
579 bitshift = 31;
581 # else
583 * We are limited by the size of sector_t. Use block size, as
584 * that's what we expose to the VFS.
586 bytes = 1 << bbits;
587 trim = 1;
588 bitshift = 31;
589 # endif
590 #endif
593 * Trim by a whole cluster when we can actually approach the
594 * on-disk limits. Otherwise we can overflow i_clusters when
595 * an extent start is at the max offset.
597 return (((unsigned long long)bytes) << bitshift) - trim;
600 static int ocfs2_remount(struct super_block *sb, int *flags, char *data)
602 int incompat_features;
603 int ret = 0;
604 struct mount_options parsed_options;
605 struct ocfs2_super *osb = OCFS2_SB(sb);
607 lock_kernel();
609 if (!ocfs2_parse_options(sb, data, &parsed_options, 1) ||
610 !ocfs2_check_set_options(sb, &parsed_options)) {
611 ret = -EINVAL;
612 goto out;
615 if ((osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) !=
616 (parsed_options.mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
617 ret = -EINVAL;
618 mlog(ML_ERROR, "Cannot change heartbeat mode on remount\n");
619 goto out;
622 if ((osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK) !=
623 (parsed_options.mount_opt & OCFS2_MOUNT_DATA_WRITEBACK)) {
624 ret = -EINVAL;
625 mlog(ML_ERROR, "Cannot change data mode on remount\n");
626 goto out;
629 /* Probably don't want this on remount; it might
630 * mess with other nodes */
631 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64) &&
632 (parsed_options.mount_opt & OCFS2_MOUNT_INODE64)) {
633 ret = -EINVAL;
634 mlog(ML_ERROR, "Cannot enable inode64 on remount\n");
635 goto out;
638 /* We're going to/from readonly mode. */
639 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
640 /* Disable quota accounting before remounting RO */
641 if (*flags & MS_RDONLY) {
642 ret = ocfs2_susp_quotas(osb, 0);
643 if (ret < 0)
644 goto out;
646 /* Lock here so the check of HARD_RO and the potential
647 * setting of SOFT_RO is atomic. */
648 spin_lock(&osb->osb_lock);
649 if (osb->osb_flags & OCFS2_OSB_HARD_RO) {
650 mlog(ML_ERROR, "Remount on readonly device is forbidden.\n");
651 ret = -EROFS;
652 goto unlock_osb;
655 if (*flags & MS_RDONLY) {
656 mlog(0, "Going to ro mode.\n");
657 sb->s_flags |= MS_RDONLY;
658 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
659 } else {
660 mlog(0, "Making ro filesystem writeable.\n");
662 if (osb->osb_flags & OCFS2_OSB_ERROR_FS) {
663 mlog(ML_ERROR, "Cannot remount RDWR "
664 "filesystem due to previous errors.\n");
665 ret = -EROFS;
666 goto unlock_osb;
668 incompat_features = OCFS2_HAS_RO_COMPAT_FEATURE(sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP);
669 if (incompat_features) {
670 mlog(ML_ERROR, "Cannot remount RDWR because "
671 "of unsupported optional features "
672 "(%x).\n", incompat_features);
673 ret = -EINVAL;
674 goto unlock_osb;
676 sb->s_flags &= ~MS_RDONLY;
677 osb->osb_flags &= ~OCFS2_OSB_SOFT_RO;
679 unlock_osb:
680 spin_unlock(&osb->osb_lock);
681 /* Enable quota accounting after remounting RW */
682 if (!ret && !(*flags & MS_RDONLY)) {
683 if (sb_any_quota_suspended(sb))
684 ret = ocfs2_susp_quotas(osb, 1);
685 else
686 ret = ocfs2_enable_quotas(osb);
687 if (ret < 0) {
688 /* Return back changes... */
689 spin_lock(&osb->osb_lock);
690 sb->s_flags |= MS_RDONLY;
691 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
692 spin_unlock(&osb->osb_lock);
693 goto out;
698 if (!ret) {
699 /* Only save off the new mount options in case of a successful
700 * remount. */
701 osb->s_mount_opt = parsed_options.mount_opt;
702 osb->s_atime_quantum = parsed_options.atime_quantum;
703 osb->preferred_slot = parsed_options.slot;
704 if (parsed_options.commit_interval)
705 osb->osb_commit_interval = parsed_options.commit_interval;
707 if (!ocfs2_is_hard_readonly(osb))
708 ocfs2_set_journal_params(osb);
710 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
711 ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ?
712 MS_POSIXACL : 0);
714 out:
715 unlock_kernel();
716 return ret;
719 static int ocfs2_sb_probe(struct super_block *sb,
720 struct buffer_head **bh,
721 int *sector_size,
722 struct ocfs2_blockcheck_stats *stats)
724 int status, tmpstat;
725 struct ocfs1_vol_disk_hdr *hdr;
726 struct ocfs2_dinode *di;
727 int blksize;
729 *bh = NULL;
731 /* may be > 512 */
732 *sector_size = bdev_logical_block_size(sb->s_bdev);
733 if (*sector_size > OCFS2_MAX_BLOCKSIZE) {
734 mlog(ML_ERROR, "Hardware sector size too large: %d (max=%d)\n",
735 *sector_size, OCFS2_MAX_BLOCKSIZE);
736 status = -EINVAL;
737 goto bail;
740 /* Can this really happen? */
741 if (*sector_size < OCFS2_MIN_BLOCKSIZE)
742 *sector_size = OCFS2_MIN_BLOCKSIZE;
744 /* check block zero for old format */
745 status = ocfs2_get_sector(sb, bh, 0, *sector_size);
746 if (status < 0) {
747 mlog_errno(status);
748 goto bail;
750 hdr = (struct ocfs1_vol_disk_hdr *) (*bh)->b_data;
751 if (hdr->major_version == OCFS1_MAJOR_VERSION) {
752 mlog(ML_ERROR, "incompatible version: %u.%u\n",
753 hdr->major_version, hdr->minor_version);
754 status = -EINVAL;
756 if (memcmp(hdr->signature, OCFS1_VOLUME_SIGNATURE,
757 strlen(OCFS1_VOLUME_SIGNATURE)) == 0) {
758 mlog(ML_ERROR, "incompatible volume signature: %8s\n",
759 hdr->signature);
760 status = -EINVAL;
762 brelse(*bh);
763 *bh = NULL;
764 if (status < 0) {
765 mlog(ML_ERROR, "This is an ocfs v1 filesystem which must be "
766 "upgraded before mounting with ocfs v2\n");
767 goto bail;
771 * Now check at magic offset for 512, 1024, 2048, 4096
772 * blocksizes. 4096 is the maximum blocksize because it is
773 * the minimum clustersize.
775 status = -EINVAL;
776 for (blksize = *sector_size;
777 blksize <= OCFS2_MAX_BLOCKSIZE;
778 blksize <<= 1) {
779 tmpstat = ocfs2_get_sector(sb, bh,
780 OCFS2_SUPER_BLOCK_BLKNO,
781 blksize);
782 if (tmpstat < 0) {
783 status = tmpstat;
784 mlog_errno(status);
785 break;
787 di = (struct ocfs2_dinode *) (*bh)->b_data;
788 memset(stats, 0, sizeof(struct ocfs2_blockcheck_stats));
789 spin_lock_init(&stats->b_lock);
790 tmpstat = ocfs2_verify_volume(di, *bh, blksize, stats);
791 if (tmpstat < 0) {
792 brelse(*bh);
793 *bh = NULL;
795 if (tmpstat != -EAGAIN) {
796 status = tmpstat;
797 break;
801 bail:
802 return status;
805 static int ocfs2_verify_heartbeat(struct ocfs2_super *osb)
807 if (ocfs2_mount_local(osb)) {
808 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
809 mlog(ML_ERROR, "Cannot heartbeat on a locally "
810 "mounted device.\n");
811 return -EINVAL;
815 if (ocfs2_userspace_stack(osb)) {
816 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
817 mlog(ML_ERROR, "Userspace stack expected, but "
818 "o2cb heartbeat arguments passed to mount\n");
819 return -EINVAL;
823 if (!(osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
824 if (!ocfs2_mount_local(osb) && !ocfs2_is_hard_readonly(osb) &&
825 !ocfs2_userspace_stack(osb)) {
826 mlog(ML_ERROR, "Heartbeat has to be started to mount "
827 "a read-write clustered device.\n");
828 return -EINVAL;
832 return 0;
836 * If we're using a userspace stack, mount should have passed
837 * a name that matches the disk. If not, mount should not
838 * have passed a stack.
840 static int ocfs2_verify_userspace_stack(struct ocfs2_super *osb,
841 struct mount_options *mopt)
843 if (!ocfs2_userspace_stack(osb) && mopt->cluster_stack[0]) {
844 mlog(ML_ERROR,
845 "cluster stack passed to mount, but this filesystem "
846 "does not support it\n");
847 return -EINVAL;
850 if (ocfs2_userspace_stack(osb) &&
851 strncmp(osb->osb_cluster_stack, mopt->cluster_stack,
852 OCFS2_STACK_LABEL_LEN)) {
853 mlog(ML_ERROR,
854 "cluster stack passed to mount (\"%s\") does not "
855 "match the filesystem (\"%s\")\n",
856 mopt->cluster_stack,
857 osb->osb_cluster_stack);
858 return -EINVAL;
861 return 0;
864 static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend)
866 int type;
867 struct super_block *sb = osb->sb;
868 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
869 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
870 int status = 0;
872 for (type = 0; type < MAXQUOTAS; type++) {
873 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
874 continue;
875 if (unsuspend)
876 status = vfs_quota_enable(
877 sb_dqopt(sb)->files[type],
878 type, QFMT_OCFS2,
879 DQUOT_SUSPENDED);
880 else
881 status = vfs_quota_disable(sb, type,
882 DQUOT_SUSPENDED);
883 if (status < 0)
884 break;
886 if (status < 0)
887 mlog(ML_ERROR, "Failed to suspend/unsuspend quotas on "
888 "remount (error = %d).\n", status);
889 return status;
892 static int ocfs2_enable_quotas(struct ocfs2_super *osb)
894 struct inode *inode[MAXQUOTAS] = { NULL, NULL };
895 struct super_block *sb = osb->sb;
896 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
897 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
898 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
899 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
900 int status;
901 int type;
903 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NEGATIVE_USAGE;
904 for (type = 0; type < MAXQUOTAS; type++) {
905 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
906 continue;
907 inode[type] = ocfs2_get_system_file_inode(osb, ino[type],
908 osb->slot_num);
909 if (!inode[type]) {
910 status = -ENOENT;
911 goto out_quota_off;
913 status = vfs_quota_enable(inode[type], type, QFMT_OCFS2,
914 DQUOT_USAGE_ENABLED);
915 if (status < 0)
916 goto out_quota_off;
919 for (type = 0; type < MAXQUOTAS; type++)
920 iput(inode[type]);
921 return 0;
922 out_quota_off:
923 ocfs2_disable_quotas(osb);
924 for (type = 0; type < MAXQUOTAS; type++)
925 iput(inode[type]);
926 mlog_errno(status);
927 return status;
930 static void ocfs2_disable_quotas(struct ocfs2_super *osb)
932 int type;
933 struct inode *inode;
934 struct super_block *sb = osb->sb;
936 /* We mostly ignore errors in this function because there's not much
937 * we can do when we see them */
938 for (type = 0; type < MAXQUOTAS; type++) {
939 if (!sb_has_quota_loaded(sb, type))
940 continue;
941 inode = igrab(sb->s_dquot.files[type]);
942 /* Turn off quotas. This will remove all dquot structures from
943 * memory and so they will be automatically synced to global
944 * quota files */
945 vfs_quota_disable(sb, type, DQUOT_USAGE_ENABLED |
946 DQUOT_LIMITS_ENABLED);
947 if (!inode)
948 continue;
949 iput(inode);
953 /* Handle quota on quotactl */
954 static int ocfs2_quota_on(struct super_block *sb, int type, int format_id,
955 char *path, int remount)
957 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
958 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
960 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
961 return -EINVAL;
963 if (remount)
964 return 0; /* Just ignore it has been handled in
965 * ocfs2_remount() */
966 return vfs_quota_enable(sb_dqopt(sb)->files[type], type,
967 format_id, DQUOT_LIMITS_ENABLED);
970 /* Handle quota off quotactl */
971 static int ocfs2_quota_off(struct super_block *sb, int type, int remount)
973 if (remount)
974 return 0; /* Ignore now and handle later in
975 * ocfs2_remount() */
976 return vfs_quota_disable(sb, type, DQUOT_LIMITS_ENABLED);
979 static const struct quotactl_ops ocfs2_quotactl_ops = {
980 .quota_on = ocfs2_quota_on,
981 .quota_off = ocfs2_quota_off,
982 .quota_sync = vfs_quota_sync,
983 .get_info = vfs_get_dqinfo,
984 .set_info = vfs_set_dqinfo,
985 .get_dqblk = vfs_get_dqblk,
986 .set_dqblk = vfs_set_dqblk,
989 static int ocfs2_fill_super(struct super_block *sb, void *data, int silent)
991 struct dentry *root;
992 int status, sector_size;
993 struct mount_options parsed_options;
994 struct inode *inode = NULL;
995 struct ocfs2_super *osb = NULL;
996 struct buffer_head *bh = NULL;
997 char nodestr[8];
998 struct ocfs2_blockcheck_stats stats;
1000 mlog_entry("%p, %p, %i", sb, data, silent);
1002 if (!ocfs2_parse_options(sb, data, &parsed_options, 0)) {
1003 status = -EINVAL;
1004 goto read_super_error;
1007 /* probe for superblock */
1008 status = ocfs2_sb_probe(sb, &bh, &sector_size, &stats);
1009 if (status < 0) {
1010 mlog(ML_ERROR, "superblock probe failed!\n");
1011 goto read_super_error;
1014 status = ocfs2_initialize_super(sb, bh, sector_size, &stats);
1015 osb = OCFS2_SB(sb);
1016 if (status < 0) {
1017 mlog_errno(status);
1018 goto read_super_error;
1020 brelse(bh);
1021 bh = NULL;
1023 if (!ocfs2_check_set_options(sb, &parsed_options)) {
1024 status = -EINVAL;
1025 goto read_super_error;
1027 osb->s_mount_opt = parsed_options.mount_opt;
1028 osb->s_atime_quantum = parsed_options.atime_quantum;
1029 osb->preferred_slot = parsed_options.slot;
1030 osb->osb_commit_interval = parsed_options.commit_interval;
1031 osb->local_alloc_default_bits = ocfs2_megabytes_to_clusters(sb, parsed_options.localalloc_opt);
1032 osb->local_alloc_bits = osb->local_alloc_default_bits;
1034 status = ocfs2_verify_userspace_stack(osb, &parsed_options);
1035 if (status)
1036 goto read_super_error;
1038 sb->s_magic = OCFS2_SUPER_MAGIC;
1040 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1041 ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
1043 /* Hard readonly mode only if: bdev_read_only, MS_RDONLY,
1044 * heartbeat=none */
1045 if (bdev_read_only(sb->s_bdev)) {
1046 if (!(sb->s_flags & MS_RDONLY)) {
1047 status = -EACCES;
1048 mlog(ML_ERROR, "Readonly device detected but readonly "
1049 "mount was not specified.\n");
1050 goto read_super_error;
1053 /* You should not be able to start a local heartbeat
1054 * on a readonly device. */
1055 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
1056 status = -EROFS;
1057 mlog(ML_ERROR, "Local heartbeat specified on readonly "
1058 "device.\n");
1059 goto read_super_error;
1062 status = ocfs2_check_journals_nolocks(osb);
1063 if (status < 0) {
1064 if (status == -EROFS)
1065 mlog(ML_ERROR, "Recovery required on readonly "
1066 "file system, but write access is "
1067 "unavailable.\n");
1068 else
1069 mlog_errno(status);
1070 goto read_super_error;
1073 ocfs2_set_ro_flag(osb, 1);
1075 printk(KERN_NOTICE "Readonly device detected. No cluster "
1076 "services will be utilized for this mount. Recovery "
1077 "will be skipped.\n");
1080 if (!ocfs2_is_hard_readonly(osb)) {
1081 if (sb->s_flags & MS_RDONLY)
1082 ocfs2_set_ro_flag(osb, 0);
1085 status = ocfs2_verify_heartbeat(osb);
1086 if (status < 0) {
1087 mlog_errno(status);
1088 goto read_super_error;
1091 osb->osb_debug_root = debugfs_create_dir(osb->uuid_str,
1092 ocfs2_debugfs_root);
1093 if (!osb->osb_debug_root) {
1094 status = -EINVAL;
1095 mlog(ML_ERROR, "Unable to create per-mount debugfs root.\n");
1096 goto read_super_error;
1099 osb->osb_ctxt = debugfs_create_file("fs_state", S_IFREG|S_IRUSR,
1100 osb->osb_debug_root,
1101 osb,
1102 &ocfs2_osb_debug_fops);
1103 if (!osb->osb_ctxt) {
1104 status = -EINVAL;
1105 mlog_errno(status);
1106 goto read_super_error;
1109 if (ocfs2_meta_ecc(osb)) {
1110 status = ocfs2_blockcheck_stats_debugfs_install(
1111 &osb->osb_ecc_stats,
1112 osb->osb_debug_root);
1113 if (status) {
1114 mlog(ML_ERROR,
1115 "Unable to create blockcheck statistics "
1116 "files\n");
1117 goto read_super_error;
1121 status = ocfs2_mount_volume(sb);
1122 if (osb->root_inode)
1123 inode = igrab(osb->root_inode);
1125 if (status < 0)
1126 goto read_super_error;
1128 if (!inode) {
1129 status = -EIO;
1130 mlog_errno(status);
1131 goto read_super_error;
1134 root = d_alloc_root(inode);
1135 if (!root) {
1136 status = -ENOMEM;
1137 mlog_errno(status);
1138 goto read_super_error;
1141 sb->s_root = root;
1143 ocfs2_complete_mount_recovery(osb);
1145 if (ocfs2_mount_local(osb))
1146 snprintf(nodestr, sizeof(nodestr), "local");
1147 else
1148 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
1150 printk(KERN_INFO "ocfs2: Mounting device (%s) on (node %s, slot %d) "
1151 "with %s data mode.\n",
1152 osb->dev_str, nodestr, osb->slot_num,
1153 osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK ? "writeback" :
1154 "ordered");
1156 atomic_set(&osb->vol_state, VOLUME_MOUNTED);
1157 wake_up(&osb->osb_mount_event);
1159 /* Now we can initialize quotas because we can afford to wait
1160 * for cluster locks recovery now. That also means that truncation
1161 * log recovery can happen but that waits for proper quota setup */
1162 if (!(sb->s_flags & MS_RDONLY)) {
1163 status = ocfs2_enable_quotas(osb);
1164 if (status < 0) {
1165 /* We have to err-out specially here because
1166 * s_root is already set */
1167 mlog_errno(status);
1168 atomic_set(&osb->vol_state, VOLUME_DISABLED);
1169 wake_up(&osb->osb_mount_event);
1170 mlog_exit(status);
1171 return status;
1175 ocfs2_complete_quota_recovery(osb);
1177 /* Now we wake up again for processes waiting for quotas */
1178 atomic_set(&osb->vol_state, VOLUME_MOUNTED_QUOTAS);
1179 wake_up(&osb->osb_mount_event);
1181 /* Start this when the mount is almost sure of being successful */
1182 ocfs2_orphan_scan_start(osb);
1184 mlog_exit(status);
1185 return status;
1187 read_super_error:
1188 brelse(bh);
1190 if (inode)
1191 iput(inode);
1193 if (osb) {
1194 atomic_set(&osb->vol_state, VOLUME_DISABLED);
1195 wake_up(&osb->osb_mount_event);
1196 ocfs2_dismount_volume(sb, 1);
1199 mlog_exit(status);
1200 return status;
1203 static int ocfs2_get_sb(struct file_system_type *fs_type,
1204 int flags,
1205 const char *dev_name,
1206 void *data,
1207 struct vfsmount *mnt)
1209 return get_sb_bdev(fs_type, flags, dev_name, data, ocfs2_fill_super,
1210 mnt);
1213 static void ocfs2_kill_sb(struct super_block *sb)
1215 struct ocfs2_super *osb = OCFS2_SB(sb);
1217 /* Failed mount? */
1218 if (!osb || atomic_read(&osb->vol_state) == VOLUME_DISABLED)
1219 goto out;
1221 /* Prevent further queueing of inode drop events */
1222 spin_lock(&dentry_list_lock);
1223 ocfs2_set_osb_flag(osb, OCFS2_OSB_DROP_DENTRY_LOCK_IMMED);
1224 spin_unlock(&dentry_list_lock);
1225 /* Wait for work to finish and/or remove it */
1226 cancel_work_sync(&osb->dentry_lock_work);
1227 out:
1228 kill_block_super(sb);
1231 static struct file_system_type ocfs2_fs_type = {
1232 .owner = THIS_MODULE,
1233 .name = "ocfs2",
1234 .get_sb = ocfs2_get_sb, /* is this called when we mount
1235 * the fs? */
1236 .kill_sb = ocfs2_kill_sb,
1238 .fs_flags = FS_REQUIRES_DEV|FS_RENAME_DOES_D_MOVE,
1239 .next = NULL
1242 static int ocfs2_check_set_options(struct super_block *sb,
1243 struct mount_options *options)
1245 if (options->mount_opt & OCFS2_MOUNT_USRQUOTA &&
1246 !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1247 OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
1248 mlog(ML_ERROR, "User quotas were requested, but this "
1249 "filesystem does not have the feature enabled.\n");
1250 return 0;
1252 if (options->mount_opt & OCFS2_MOUNT_GRPQUOTA &&
1253 !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1254 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
1255 mlog(ML_ERROR, "Group quotas were requested, but this "
1256 "filesystem does not have the feature enabled.\n");
1257 return 0;
1259 if (options->mount_opt & OCFS2_MOUNT_POSIX_ACL &&
1260 !OCFS2_HAS_INCOMPAT_FEATURE(sb, OCFS2_FEATURE_INCOMPAT_XATTR)) {
1261 mlog(ML_ERROR, "ACL support requested but extended attributes "
1262 "feature is not enabled\n");
1263 return 0;
1265 /* No ACL setting specified? Use XATTR feature... */
1266 if (!(options->mount_opt & (OCFS2_MOUNT_POSIX_ACL |
1267 OCFS2_MOUNT_NO_POSIX_ACL))) {
1268 if (OCFS2_HAS_INCOMPAT_FEATURE(sb, OCFS2_FEATURE_INCOMPAT_XATTR))
1269 options->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
1270 else
1271 options->mount_opt |= OCFS2_MOUNT_NO_POSIX_ACL;
1273 return 1;
1276 static int ocfs2_parse_options(struct super_block *sb,
1277 char *options,
1278 struct mount_options *mopt,
1279 int is_remount)
1281 int status;
1282 char *p;
1284 mlog_entry("remount: %d, options: \"%s\"\n", is_remount,
1285 options ? options : "(none)");
1287 mopt->commit_interval = 0;
1288 mopt->mount_opt = 0;
1289 mopt->atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
1290 mopt->slot = OCFS2_INVALID_SLOT;
1291 mopt->localalloc_opt = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE;
1292 mopt->cluster_stack[0] = '\0';
1294 if (!options) {
1295 status = 1;
1296 goto bail;
1299 while ((p = strsep(&options, ",")) != NULL) {
1300 int token, option;
1301 substring_t args[MAX_OPT_ARGS];
1303 if (!*p)
1304 continue;
1306 token = match_token(p, tokens, args);
1307 switch (token) {
1308 case Opt_hb_local:
1309 mopt->mount_opt |= OCFS2_MOUNT_HB_LOCAL;
1310 break;
1311 case Opt_hb_none:
1312 mopt->mount_opt &= ~OCFS2_MOUNT_HB_LOCAL;
1313 break;
1314 case Opt_barrier:
1315 if (match_int(&args[0], &option)) {
1316 status = 0;
1317 goto bail;
1319 if (option)
1320 mopt->mount_opt |= OCFS2_MOUNT_BARRIER;
1321 else
1322 mopt->mount_opt &= ~OCFS2_MOUNT_BARRIER;
1323 break;
1324 case Opt_intr:
1325 mopt->mount_opt &= ~OCFS2_MOUNT_NOINTR;
1326 break;
1327 case Opt_nointr:
1328 mopt->mount_opt |= OCFS2_MOUNT_NOINTR;
1329 break;
1330 case Opt_err_panic:
1331 mopt->mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
1332 break;
1333 case Opt_err_ro:
1334 mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_PANIC;
1335 break;
1336 case Opt_data_ordered:
1337 mopt->mount_opt &= ~OCFS2_MOUNT_DATA_WRITEBACK;
1338 break;
1339 case Opt_data_writeback:
1340 mopt->mount_opt |= OCFS2_MOUNT_DATA_WRITEBACK;
1341 break;
1342 case Opt_user_xattr:
1343 mopt->mount_opt &= ~OCFS2_MOUNT_NOUSERXATTR;
1344 break;
1345 case Opt_nouser_xattr:
1346 mopt->mount_opt |= OCFS2_MOUNT_NOUSERXATTR;
1347 break;
1348 case Opt_atime_quantum:
1349 if (match_int(&args[0], &option)) {
1350 status = 0;
1351 goto bail;
1353 if (option >= 0)
1354 mopt->atime_quantum = option;
1355 break;
1356 case Opt_slot:
1357 option = 0;
1358 if (match_int(&args[0], &option)) {
1359 status = 0;
1360 goto bail;
1362 if (option)
1363 mopt->slot = (s16)option;
1364 break;
1365 case Opt_commit:
1366 option = 0;
1367 if (match_int(&args[0], &option)) {
1368 status = 0;
1369 goto bail;
1371 if (option < 0)
1372 return 0;
1373 if (option == 0)
1374 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1375 mopt->commit_interval = HZ * option;
1376 break;
1377 case Opt_localalloc:
1378 option = 0;
1379 if (match_int(&args[0], &option)) {
1380 status = 0;
1381 goto bail;
1383 if (option >= 0 && (option <= ocfs2_local_alloc_size(sb) * 8))
1384 mopt->localalloc_opt = option;
1385 break;
1386 case Opt_localflocks:
1388 * Changing this during remount could race
1389 * flock() requests, or "unbalance" existing
1390 * ones (e.g., a lock is taken in one mode but
1391 * dropped in the other). If users care enough
1392 * to flip locking modes during remount, we
1393 * could add a "local" flag to individual
1394 * flock structures for proper tracking of
1395 * state.
1397 if (!is_remount)
1398 mopt->mount_opt |= OCFS2_MOUNT_LOCALFLOCKS;
1399 break;
1400 case Opt_stack:
1401 /* Check both that the option we were passed
1402 * is of the right length and that it is a proper
1403 * string of the right length.
1405 if (((args[0].to - args[0].from) !=
1406 OCFS2_STACK_LABEL_LEN) ||
1407 (strnlen(args[0].from,
1408 OCFS2_STACK_LABEL_LEN) !=
1409 OCFS2_STACK_LABEL_LEN)) {
1410 mlog(ML_ERROR,
1411 "Invalid cluster_stack option\n");
1412 status = 0;
1413 goto bail;
1415 memcpy(mopt->cluster_stack, args[0].from,
1416 OCFS2_STACK_LABEL_LEN);
1417 mopt->cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
1418 break;
1419 case Opt_inode64:
1420 mopt->mount_opt |= OCFS2_MOUNT_INODE64;
1421 break;
1422 case Opt_usrquota:
1423 mopt->mount_opt |= OCFS2_MOUNT_USRQUOTA;
1424 break;
1425 case Opt_grpquota:
1426 mopt->mount_opt |= OCFS2_MOUNT_GRPQUOTA;
1427 break;
1428 case Opt_acl:
1429 mopt->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
1430 mopt->mount_opt &= ~OCFS2_MOUNT_NO_POSIX_ACL;
1431 break;
1432 case Opt_noacl:
1433 mopt->mount_opt |= OCFS2_MOUNT_NO_POSIX_ACL;
1434 mopt->mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
1435 break;
1436 default:
1437 mlog(ML_ERROR,
1438 "Unrecognized mount option \"%s\" "
1439 "or missing value\n", p);
1440 status = 0;
1441 goto bail;
1445 status = 1;
1447 bail:
1448 mlog_exit(status);
1449 return status;
1452 static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
1454 struct ocfs2_super *osb = OCFS2_SB(mnt->mnt_sb);
1455 unsigned long opts = osb->s_mount_opt;
1456 unsigned int local_alloc_megs;
1458 if (opts & OCFS2_MOUNT_HB_LOCAL)
1459 seq_printf(s, ",_netdev,heartbeat=local");
1460 else
1461 seq_printf(s, ",heartbeat=none");
1463 if (opts & OCFS2_MOUNT_NOINTR)
1464 seq_printf(s, ",nointr");
1466 if (opts & OCFS2_MOUNT_DATA_WRITEBACK)
1467 seq_printf(s, ",data=writeback");
1468 else
1469 seq_printf(s, ",data=ordered");
1471 if (opts & OCFS2_MOUNT_BARRIER)
1472 seq_printf(s, ",barrier=1");
1474 if (opts & OCFS2_MOUNT_ERRORS_PANIC)
1475 seq_printf(s, ",errors=panic");
1476 else
1477 seq_printf(s, ",errors=remount-ro");
1479 if (osb->preferred_slot != OCFS2_INVALID_SLOT)
1480 seq_printf(s, ",preferred_slot=%d", osb->preferred_slot);
1482 if (osb->s_atime_quantum != OCFS2_DEFAULT_ATIME_QUANTUM)
1483 seq_printf(s, ",atime_quantum=%u", osb->s_atime_quantum);
1485 if (osb->osb_commit_interval)
1486 seq_printf(s, ",commit=%u",
1487 (unsigned) (osb->osb_commit_interval / HZ));
1489 local_alloc_megs = osb->local_alloc_bits >> (20 - osb->s_clustersize_bits);
1490 if (local_alloc_megs != OCFS2_DEFAULT_LOCAL_ALLOC_SIZE)
1491 seq_printf(s, ",localalloc=%d", local_alloc_megs);
1493 if (opts & OCFS2_MOUNT_LOCALFLOCKS)
1494 seq_printf(s, ",localflocks,");
1496 if (osb->osb_cluster_stack[0])
1497 seq_printf(s, ",cluster_stack=%.*s", OCFS2_STACK_LABEL_LEN,
1498 osb->osb_cluster_stack);
1499 if (opts & OCFS2_MOUNT_USRQUOTA)
1500 seq_printf(s, ",usrquota");
1501 if (opts & OCFS2_MOUNT_GRPQUOTA)
1502 seq_printf(s, ",grpquota");
1504 if (opts & OCFS2_MOUNT_NOUSERXATTR)
1505 seq_printf(s, ",nouser_xattr");
1506 else
1507 seq_printf(s, ",user_xattr");
1509 if (opts & OCFS2_MOUNT_INODE64)
1510 seq_printf(s, ",inode64");
1512 if (opts & OCFS2_MOUNT_POSIX_ACL)
1513 seq_printf(s, ",acl");
1514 else
1515 seq_printf(s, ",noacl");
1517 return 0;
1520 static int __init ocfs2_init(void)
1522 int status;
1524 mlog_entry_void();
1526 ocfs2_print_version();
1528 status = init_ocfs2_uptodate_cache();
1529 if (status < 0) {
1530 mlog_errno(status);
1531 goto leave;
1534 status = ocfs2_initialize_mem_caches();
1535 if (status < 0) {
1536 mlog_errno(status);
1537 goto leave;
1540 ocfs2_wq = create_singlethread_workqueue("ocfs2_wq");
1541 if (!ocfs2_wq) {
1542 status = -ENOMEM;
1543 goto leave;
1546 ocfs2_debugfs_root = debugfs_create_dir("ocfs2", NULL);
1547 if (!ocfs2_debugfs_root) {
1548 status = -EFAULT;
1549 mlog(ML_ERROR, "Unable to create ocfs2 debugfs root.\n");
1552 status = ocfs2_quota_setup();
1553 if (status)
1554 goto leave;
1556 ocfs2_set_locking_protocol();
1558 status = register_quota_format(&ocfs2_quota_format);
1559 leave:
1560 if (status < 0) {
1561 ocfs2_quota_shutdown();
1562 ocfs2_free_mem_caches();
1563 exit_ocfs2_uptodate_cache();
1566 mlog_exit(status);
1568 if (status >= 0) {
1569 return register_filesystem(&ocfs2_fs_type);
1570 } else
1571 return -1;
1574 static void __exit ocfs2_exit(void)
1576 mlog_entry_void();
1578 ocfs2_quota_shutdown();
1580 if (ocfs2_wq) {
1581 flush_workqueue(ocfs2_wq);
1582 destroy_workqueue(ocfs2_wq);
1585 unregister_quota_format(&ocfs2_quota_format);
1587 debugfs_remove(ocfs2_debugfs_root);
1589 ocfs2_free_mem_caches();
1591 unregister_filesystem(&ocfs2_fs_type);
1593 exit_ocfs2_uptodate_cache();
1595 mlog_exit_void();
1598 static void ocfs2_put_super(struct super_block *sb)
1600 mlog_entry("(0x%p)\n", sb);
1602 lock_kernel();
1604 ocfs2_sync_blockdev(sb);
1605 ocfs2_dismount_volume(sb, 0);
1607 unlock_kernel();
1609 mlog_exit_void();
1612 static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
1614 struct ocfs2_super *osb;
1615 u32 numbits, freebits;
1616 int status;
1617 struct ocfs2_dinode *bm_lock;
1618 struct buffer_head *bh = NULL;
1619 struct inode *inode = NULL;
1621 mlog_entry("(%p, %p)\n", dentry->d_sb, buf);
1623 osb = OCFS2_SB(dentry->d_sb);
1625 inode = ocfs2_get_system_file_inode(osb,
1626 GLOBAL_BITMAP_SYSTEM_INODE,
1627 OCFS2_INVALID_SLOT);
1628 if (!inode) {
1629 mlog(ML_ERROR, "failed to get bitmap inode\n");
1630 status = -EIO;
1631 goto bail;
1634 status = ocfs2_inode_lock(inode, &bh, 0);
1635 if (status < 0) {
1636 mlog_errno(status);
1637 goto bail;
1640 bm_lock = (struct ocfs2_dinode *) bh->b_data;
1642 numbits = le32_to_cpu(bm_lock->id1.bitmap1.i_total);
1643 freebits = numbits - le32_to_cpu(bm_lock->id1.bitmap1.i_used);
1645 buf->f_type = OCFS2_SUPER_MAGIC;
1646 buf->f_bsize = dentry->d_sb->s_blocksize;
1647 buf->f_namelen = OCFS2_MAX_FILENAME_LEN;
1648 buf->f_blocks = ((sector_t) numbits) *
1649 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1650 buf->f_bfree = ((sector_t) freebits) *
1651 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1652 buf->f_bavail = buf->f_bfree;
1653 buf->f_files = numbits;
1654 buf->f_ffree = freebits;
1655 buf->f_fsid.val[0] = crc32_le(0, osb->uuid_str, OCFS2_VOL_UUID_LEN)
1656 & 0xFFFFFFFFUL;
1657 buf->f_fsid.val[1] = crc32_le(0, osb->uuid_str + OCFS2_VOL_UUID_LEN,
1658 OCFS2_VOL_UUID_LEN) & 0xFFFFFFFFUL;
1660 brelse(bh);
1662 ocfs2_inode_unlock(inode, 0);
1663 status = 0;
1664 bail:
1665 if (inode)
1666 iput(inode);
1668 mlog_exit(status);
1670 return status;
1673 static void ocfs2_inode_init_once(void *data)
1675 struct ocfs2_inode_info *oi = data;
1677 oi->ip_flags = 0;
1678 oi->ip_open_count = 0;
1679 spin_lock_init(&oi->ip_lock);
1680 ocfs2_extent_map_init(&oi->vfs_inode);
1681 INIT_LIST_HEAD(&oi->ip_io_markers);
1682 oi->ip_dir_start_lookup = 0;
1684 init_rwsem(&oi->ip_alloc_sem);
1685 init_rwsem(&oi->ip_xattr_sem);
1686 mutex_init(&oi->ip_io_mutex);
1688 oi->ip_blkno = 0ULL;
1689 oi->ip_clusters = 0;
1691 ocfs2_lock_res_init_once(&oi->ip_rw_lockres);
1692 ocfs2_lock_res_init_once(&oi->ip_inode_lockres);
1693 ocfs2_lock_res_init_once(&oi->ip_open_lockres);
1695 ocfs2_metadata_cache_init(INODE_CACHE(&oi->vfs_inode),
1696 &ocfs2_inode_caching_ops);
1698 inode_init_once(&oi->vfs_inode);
1701 static int ocfs2_initialize_mem_caches(void)
1703 ocfs2_inode_cachep = kmem_cache_create("ocfs2_inode_cache",
1704 sizeof(struct ocfs2_inode_info),
1706 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1707 SLAB_MEM_SPREAD),
1708 ocfs2_inode_init_once);
1709 ocfs2_dquot_cachep = kmem_cache_create("ocfs2_dquot_cache",
1710 sizeof(struct ocfs2_dquot),
1712 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1713 SLAB_MEM_SPREAD),
1714 NULL);
1715 ocfs2_qf_chunk_cachep = kmem_cache_create("ocfs2_qf_chunk_cache",
1716 sizeof(struct ocfs2_quota_chunk),
1718 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
1719 NULL);
1720 if (!ocfs2_inode_cachep || !ocfs2_dquot_cachep ||
1721 !ocfs2_qf_chunk_cachep) {
1722 if (ocfs2_inode_cachep)
1723 kmem_cache_destroy(ocfs2_inode_cachep);
1724 if (ocfs2_dquot_cachep)
1725 kmem_cache_destroy(ocfs2_dquot_cachep);
1726 if (ocfs2_qf_chunk_cachep)
1727 kmem_cache_destroy(ocfs2_qf_chunk_cachep);
1728 return -ENOMEM;
1731 return 0;
1734 static void ocfs2_free_mem_caches(void)
1736 if (ocfs2_inode_cachep)
1737 kmem_cache_destroy(ocfs2_inode_cachep);
1738 ocfs2_inode_cachep = NULL;
1740 if (ocfs2_dquot_cachep)
1741 kmem_cache_destroy(ocfs2_dquot_cachep);
1742 ocfs2_dquot_cachep = NULL;
1744 if (ocfs2_qf_chunk_cachep)
1745 kmem_cache_destroy(ocfs2_qf_chunk_cachep);
1746 ocfs2_qf_chunk_cachep = NULL;
1749 static int ocfs2_get_sector(struct super_block *sb,
1750 struct buffer_head **bh,
1751 int block,
1752 int sect_size)
1754 if (!sb_set_blocksize(sb, sect_size)) {
1755 mlog(ML_ERROR, "unable to set blocksize\n");
1756 return -EIO;
1759 *bh = sb_getblk(sb, block);
1760 if (!*bh) {
1761 mlog_errno(-EIO);
1762 return -EIO;
1764 lock_buffer(*bh);
1765 if (!buffer_dirty(*bh))
1766 clear_buffer_uptodate(*bh);
1767 unlock_buffer(*bh);
1768 ll_rw_block(READ, 1, bh);
1769 wait_on_buffer(*bh);
1770 if (!buffer_uptodate(*bh)) {
1771 mlog_errno(-EIO);
1772 brelse(*bh);
1773 *bh = NULL;
1774 return -EIO;
1777 return 0;
1780 static int ocfs2_mount_volume(struct super_block *sb)
1782 int status = 0;
1783 int unlock_super = 0;
1784 struct ocfs2_super *osb = OCFS2_SB(sb);
1786 mlog_entry_void();
1788 if (ocfs2_is_hard_readonly(osb))
1789 goto leave;
1791 status = ocfs2_dlm_init(osb);
1792 if (status < 0) {
1793 mlog_errno(status);
1794 goto leave;
1797 status = ocfs2_super_lock(osb, 1);
1798 if (status < 0) {
1799 mlog_errno(status);
1800 goto leave;
1802 unlock_super = 1;
1804 /* This will load up the node map and add ourselves to it. */
1805 status = ocfs2_find_slot(osb);
1806 if (status < 0) {
1807 mlog_errno(status);
1808 goto leave;
1811 /* load all node-local system inodes */
1812 status = ocfs2_init_local_system_inodes(osb);
1813 if (status < 0) {
1814 mlog_errno(status);
1815 goto leave;
1818 status = ocfs2_check_volume(osb);
1819 if (status < 0) {
1820 mlog_errno(status);
1821 goto leave;
1824 status = ocfs2_truncate_log_init(osb);
1825 if (status < 0)
1826 mlog_errno(status);
1828 leave:
1829 if (unlock_super)
1830 ocfs2_super_unlock(osb, 1);
1832 mlog_exit(status);
1833 return status;
1836 static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
1838 int tmp, hangup_needed = 0;
1839 struct ocfs2_super *osb = NULL;
1840 char nodestr[8];
1842 mlog_entry("(0x%p)\n", sb);
1844 BUG_ON(!sb);
1845 osb = OCFS2_SB(sb);
1846 BUG_ON(!osb);
1848 debugfs_remove(osb->osb_ctxt);
1851 * Flush inode dropping work queue so that deletes are
1852 * performed while the filesystem is still working
1854 ocfs2_drop_all_dl_inodes(osb);
1856 /* Orphan scan should be stopped as early as possible */
1857 ocfs2_orphan_scan_stop(osb);
1859 ocfs2_disable_quotas(osb);
1861 ocfs2_shutdown_local_alloc(osb);
1863 ocfs2_truncate_log_shutdown(osb);
1865 /* This will disable recovery and flush any recovery work. */
1866 ocfs2_recovery_exit(osb);
1868 ocfs2_journal_shutdown(osb);
1870 ocfs2_sync_blockdev(sb);
1872 ocfs2_purge_refcount_trees(osb);
1874 /* No cluster connection means we've failed during mount, so skip
1875 * all the steps which depended on that to complete. */
1876 if (osb->cconn) {
1877 tmp = ocfs2_super_lock(osb, 1);
1878 if (tmp < 0) {
1879 mlog_errno(tmp);
1880 return;
1884 if (osb->slot_num != OCFS2_INVALID_SLOT)
1885 ocfs2_put_slot(osb);
1887 if (osb->cconn)
1888 ocfs2_super_unlock(osb, 1);
1890 ocfs2_release_system_inodes(osb);
1893 * If we're dismounting due to mount error, mount.ocfs2 will clean
1894 * up heartbeat. If we're a local mount, there is no heartbeat.
1895 * If we failed before we got a uuid_str yet, we can't stop
1896 * heartbeat. Otherwise, do it.
1898 if (!mnt_err && !ocfs2_mount_local(osb) && osb->uuid_str)
1899 hangup_needed = 1;
1901 if (osb->cconn)
1902 ocfs2_dlm_shutdown(osb, hangup_needed);
1904 ocfs2_blockcheck_stats_debugfs_remove(&osb->osb_ecc_stats);
1905 debugfs_remove(osb->osb_debug_root);
1907 if (hangup_needed)
1908 ocfs2_cluster_hangup(osb->uuid_str, strlen(osb->uuid_str));
1910 atomic_set(&osb->vol_state, VOLUME_DISMOUNTED);
1912 if (ocfs2_mount_local(osb))
1913 snprintf(nodestr, sizeof(nodestr), "local");
1914 else
1915 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
1917 printk(KERN_INFO "ocfs2: Unmounting device (%s) on (node %s)\n",
1918 osb->dev_str, nodestr);
1920 ocfs2_delete_osb(osb);
1921 kfree(osb);
1922 sb->s_dev = 0;
1923 sb->s_fs_info = NULL;
1926 static int ocfs2_setup_osb_uuid(struct ocfs2_super *osb, const unsigned char *uuid,
1927 unsigned uuid_bytes)
1929 int i, ret;
1930 char *ptr;
1932 BUG_ON(uuid_bytes != OCFS2_VOL_UUID_LEN);
1934 osb->uuid_str = kzalloc(OCFS2_VOL_UUID_LEN * 2 + 1, GFP_KERNEL);
1935 if (osb->uuid_str == NULL)
1936 return -ENOMEM;
1938 for (i = 0, ptr = osb->uuid_str; i < OCFS2_VOL_UUID_LEN; i++) {
1939 /* print with null */
1940 ret = snprintf(ptr, 3, "%02X", uuid[i]);
1941 if (ret != 2) /* drop super cleans up */
1942 return -EINVAL;
1943 /* then only advance past the last char */
1944 ptr += 2;
1947 return 0;
1950 static int ocfs2_initialize_super(struct super_block *sb,
1951 struct buffer_head *bh,
1952 int sector_size,
1953 struct ocfs2_blockcheck_stats *stats)
1955 int status;
1956 int i, cbits, bbits;
1957 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1958 struct inode *inode = NULL;
1959 struct ocfs2_journal *journal;
1960 __le32 uuid_net_key;
1961 struct ocfs2_super *osb;
1963 mlog_entry_void();
1965 osb = kzalloc(sizeof(struct ocfs2_super), GFP_KERNEL);
1966 if (!osb) {
1967 status = -ENOMEM;
1968 mlog_errno(status);
1969 goto bail;
1972 sb->s_fs_info = osb;
1973 sb->s_op = &ocfs2_sops;
1974 sb->s_export_op = &ocfs2_export_ops;
1975 sb->s_qcop = &ocfs2_quotactl_ops;
1976 sb->dq_op = &ocfs2_quota_operations;
1977 sb->s_xattr = ocfs2_xattr_handlers;
1978 sb->s_time_gran = 1;
1979 sb->s_flags |= MS_NOATIME;
1980 /* this is needed to support O_LARGEFILE */
1981 cbits = le32_to_cpu(di->id2.i_super.s_clustersize_bits);
1982 bbits = le32_to_cpu(di->id2.i_super.s_blocksize_bits);
1983 sb->s_maxbytes = ocfs2_max_file_offset(bbits, cbits);
1985 osb->osb_dx_mask = (1 << (cbits - bbits)) - 1;
1987 for (i = 0; i < 3; i++)
1988 osb->osb_dx_seed[i] = le32_to_cpu(di->id2.i_super.s_dx_seed[i]);
1989 osb->osb_dx_seed[3] = le32_to_cpu(di->id2.i_super.s_uuid_hash);
1991 osb->sb = sb;
1992 /* Save off for ocfs2_rw_direct */
1993 osb->s_sectsize_bits = blksize_bits(sector_size);
1994 BUG_ON(!osb->s_sectsize_bits);
1996 spin_lock_init(&osb->dc_task_lock);
1997 init_waitqueue_head(&osb->dc_event);
1998 osb->dc_work_sequence = 0;
1999 osb->dc_wake_sequence = 0;
2000 INIT_LIST_HEAD(&osb->blocked_lock_list);
2001 osb->blocked_lock_count = 0;
2002 spin_lock_init(&osb->osb_lock);
2003 spin_lock_init(&osb->osb_xattr_lock);
2004 ocfs2_init_steal_slots(osb);
2006 atomic_set(&osb->alloc_stats.moves, 0);
2007 atomic_set(&osb->alloc_stats.local_data, 0);
2008 atomic_set(&osb->alloc_stats.bitmap_data, 0);
2009 atomic_set(&osb->alloc_stats.bg_allocs, 0);
2010 atomic_set(&osb->alloc_stats.bg_extends, 0);
2012 /* Copy the blockcheck stats from the superblock probe */
2013 osb->osb_ecc_stats = *stats;
2015 ocfs2_init_node_maps(osb);
2017 snprintf(osb->dev_str, sizeof(osb->dev_str), "%u,%u",
2018 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
2020 ocfs2_orphan_scan_init(osb);
2022 status = ocfs2_recovery_init(osb);
2023 if (status) {
2024 mlog(ML_ERROR, "Unable to initialize recovery state\n");
2025 mlog_errno(status);
2026 goto bail;
2029 init_waitqueue_head(&osb->checkpoint_event);
2030 atomic_set(&osb->needs_checkpoint, 0);
2032 osb->s_atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
2034 osb->slot_num = OCFS2_INVALID_SLOT;
2036 osb->s_xattr_inline_size = le16_to_cpu(
2037 di->id2.i_super.s_xattr_inline_size);
2039 osb->local_alloc_state = OCFS2_LA_UNUSED;
2040 osb->local_alloc_bh = NULL;
2041 INIT_DELAYED_WORK(&osb->la_enable_wq, ocfs2_la_enable_worker);
2043 init_waitqueue_head(&osb->osb_mount_event);
2045 osb->vol_label = kmalloc(OCFS2_MAX_VOL_LABEL_LEN, GFP_KERNEL);
2046 if (!osb->vol_label) {
2047 mlog(ML_ERROR, "unable to alloc vol label\n");
2048 status = -ENOMEM;
2049 goto bail;
2052 osb->max_slots = le16_to_cpu(di->id2.i_super.s_max_slots);
2053 if (osb->max_slots > OCFS2_MAX_SLOTS || osb->max_slots == 0) {
2054 mlog(ML_ERROR, "Invalid number of node slots (%u)\n",
2055 osb->max_slots);
2056 status = -EINVAL;
2057 goto bail;
2059 mlog(0, "max_slots for this device: %u\n", osb->max_slots);
2061 osb->slot_recovery_generations =
2062 kcalloc(osb->max_slots, sizeof(*osb->slot_recovery_generations),
2063 GFP_KERNEL);
2064 if (!osb->slot_recovery_generations) {
2065 status = -ENOMEM;
2066 mlog_errno(status);
2067 goto bail;
2070 init_waitqueue_head(&osb->osb_wipe_event);
2071 osb->osb_orphan_wipes = kcalloc(osb->max_slots,
2072 sizeof(*osb->osb_orphan_wipes),
2073 GFP_KERNEL);
2074 if (!osb->osb_orphan_wipes) {
2075 status = -ENOMEM;
2076 mlog_errno(status);
2077 goto bail;
2080 osb->osb_rf_lock_tree = RB_ROOT;
2082 osb->s_feature_compat =
2083 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_compat);
2084 osb->s_feature_ro_compat =
2085 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_ro_compat);
2086 osb->s_feature_incompat =
2087 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_incompat);
2089 if ((i = OCFS2_HAS_INCOMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_INCOMPAT_SUPP))) {
2090 mlog(ML_ERROR, "couldn't mount because of unsupported "
2091 "optional features (%x).\n", i);
2092 status = -EINVAL;
2093 goto bail;
2095 if (!(osb->sb->s_flags & MS_RDONLY) &&
2096 (i = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP))) {
2097 mlog(ML_ERROR, "couldn't mount RDWR because of "
2098 "unsupported optional features (%x).\n", i);
2099 status = -EINVAL;
2100 goto bail;
2103 if (ocfs2_userspace_stack(osb)) {
2104 memcpy(osb->osb_cluster_stack,
2105 OCFS2_RAW_SB(di)->s_cluster_info.ci_stack,
2106 OCFS2_STACK_LABEL_LEN);
2107 osb->osb_cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
2108 if (strlen(osb->osb_cluster_stack) != OCFS2_STACK_LABEL_LEN) {
2109 mlog(ML_ERROR,
2110 "couldn't mount because of an invalid "
2111 "cluster stack label (%s) \n",
2112 osb->osb_cluster_stack);
2113 status = -EINVAL;
2114 goto bail;
2116 } else {
2117 /* The empty string is identical with classic tools that
2118 * don't know about s_cluster_info. */
2119 osb->osb_cluster_stack[0] = '\0';
2122 get_random_bytes(&osb->s_next_generation, sizeof(u32));
2124 /* FIXME
2125 * This should be done in ocfs2_journal_init(), but unknown
2126 * ordering issues will cause the filesystem to crash.
2127 * If anyone wants to figure out what part of the code
2128 * refers to osb->journal before ocfs2_journal_init() is run,
2129 * be my guest.
2131 /* initialize our journal structure */
2133 journal = kzalloc(sizeof(struct ocfs2_journal), GFP_KERNEL);
2134 if (!journal) {
2135 mlog(ML_ERROR, "unable to alloc journal\n");
2136 status = -ENOMEM;
2137 goto bail;
2139 osb->journal = journal;
2140 journal->j_osb = osb;
2142 atomic_set(&journal->j_num_trans, 0);
2143 init_rwsem(&journal->j_trans_barrier);
2144 init_waitqueue_head(&journal->j_checkpointed);
2145 spin_lock_init(&journal->j_lock);
2146 journal->j_trans_id = (unsigned long) 1;
2147 INIT_LIST_HEAD(&journal->j_la_cleanups);
2148 INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
2149 journal->j_state = OCFS2_JOURNAL_FREE;
2151 INIT_WORK(&osb->dentry_lock_work, ocfs2_drop_dl_inodes);
2152 osb->dentry_lock_list = NULL;
2154 /* get some pseudo constants for clustersize bits */
2155 osb->s_clustersize_bits =
2156 le32_to_cpu(di->id2.i_super.s_clustersize_bits);
2157 osb->s_clustersize = 1 << osb->s_clustersize_bits;
2158 mlog(0, "clusterbits=%d\n", osb->s_clustersize_bits);
2160 if (osb->s_clustersize < OCFS2_MIN_CLUSTERSIZE ||
2161 osb->s_clustersize > OCFS2_MAX_CLUSTERSIZE) {
2162 mlog(ML_ERROR, "Volume has invalid cluster size (%d)\n",
2163 osb->s_clustersize);
2164 status = -EINVAL;
2165 goto bail;
2168 if (ocfs2_clusters_to_blocks(osb->sb, le32_to_cpu(di->i_clusters) - 1)
2169 > (u32)~0UL) {
2170 mlog(ML_ERROR, "Volume might try to write to blocks beyond "
2171 "what jbd can address in 32 bits.\n");
2172 status = -EINVAL;
2173 goto bail;
2176 if (ocfs2_setup_osb_uuid(osb, di->id2.i_super.s_uuid,
2177 sizeof(di->id2.i_super.s_uuid))) {
2178 mlog(ML_ERROR, "Out of memory trying to setup our uuid.\n");
2179 status = -ENOMEM;
2180 goto bail;
2183 memcpy(&uuid_net_key, di->id2.i_super.s_uuid, sizeof(uuid_net_key));
2185 strncpy(osb->vol_label, di->id2.i_super.s_label, 63);
2186 osb->vol_label[63] = '\0';
2187 osb->root_blkno = le64_to_cpu(di->id2.i_super.s_root_blkno);
2188 osb->system_dir_blkno = le64_to_cpu(di->id2.i_super.s_system_dir_blkno);
2189 osb->first_cluster_group_blkno =
2190 le64_to_cpu(di->id2.i_super.s_first_cluster_group);
2191 osb->fs_generation = le32_to_cpu(di->i_fs_generation);
2192 osb->uuid_hash = le32_to_cpu(di->id2.i_super.s_uuid_hash);
2193 mlog(0, "vol_label: %s\n", osb->vol_label);
2194 mlog(0, "uuid: %s\n", osb->uuid_str);
2195 mlog(0, "root_blkno=%llu, system_dir_blkno=%llu\n",
2196 (unsigned long long)osb->root_blkno,
2197 (unsigned long long)osb->system_dir_blkno);
2199 osb->osb_dlm_debug = ocfs2_new_dlm_debug();
2200 if (!osb->osb_dlm_debug) {
2201 status = -ENOMEM;
2202 mlog_errno(status);
2203 goto bail;
2206 atomic_set(&osb->vol_state, VOLUME_INIT);
2208 /* load root, system_dir, and all global system inodes */
2209 status = ocfs2_init_global_system_inodes(osb);
2210 if (status < 0) {
2211 mlog_errno(status);
2212 goto bail;
2216 * global bitmap
2218 inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
2219 OCFS2_INVALID_SLOT);
2220 if (!inode) {
2221 status = -EINVAL;
2222 mlog_errno(status);
2223 goto bail;
2226 osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
2227 iput(inode);
2229 osb->bitmap_cpg = ocfs2_group_bitmap_size(sb) * 8;
2231 status = ocfs2_init_slot_info(osb);
2232 if (status < 0) {
2233 mlog_errno(status);
2234 goto bail;
2237 bail:
2238 mlog_exit(status);
2239 return status;
2243 * will return: -EAGAIN if it is ok to keep searching for superblocks
2244 * -EINVAL if there is a bad superblock
2245 * 0 on success
2247 static int ocfs2_verify_volume(struct ocfs2_dinode *di,
2248 struct buffer_head *bh,
2249 u32 blksz,
2250 struct ocfs2_blockcheck_stats *stats)
2252 int status = -EAGAIN;
2254 mlog_entry_void();
2256 if (memcmp(di->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
2257 strlen(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) {
2258 /* We have to do a raw check of the feature here */
2259 if (le32_to_cpu(di->id2.i_super.s_feature_incompat) &
2260 OCFS2_FEATURE_INCOMPAT_META_ECC) {
2261 status = ocfs2_block_check_validate(bh->b_data,
2262 bh->b_size,
2263 &di->i_check,
2264 stats);
2265 if (status)
2266 goto out;
2268 status = -EINVAL;
2269 if ((1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits)) != blksz) {
2270 mlog(ML_ERROR, "found superblock with incorrect block "
2271 "size: found %u, should be %u\n",
2272 1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits),
2273 blksz);
2274 } else if (le16_to_cpu(di->id2.i_super.s_major_rev_level) !=
2275 OCFS2_MAJOR_REV_LEVEL ||
2276 le16_to_cpu(di->id2.i_super.s_minor_rev_level) !=
2277 OCFS2_MINOR_REV_LEVEL) {
2278 mlog(ML_ERROR, "found superblock with bad version: "
2279 "found %u.%u, should be %u.%u\n",
2280 le16_to_cpu(di->id2.i_super.s_major_rev_level),
2281 le16_to_cpu(di->id2.i_super.s_minor_rev_level),
2282 OCFS2_MAJOR_REV_LEVEL,
2283 OCFS2_MINOR_REV_LEVEL);
2284 } else if (bh->b_blocknr != le64_to_cpu(di->i_blkno)) {
2285 mlog(ML_ERROR, "bad block number on superblock: "
2286 "found %llu, should be %llu\n",
2287 (unsigned long long)le64_to_cpu(di->i_blkno),
2288 (unsigned long long)bh->b_blocknr);
2289 } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
2290 le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
2291 mlog(ML_ERROR, "bad cluster size found: %u\n",
2292 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
2293 } else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
2294 mlog(ML_ERROR, "bad root_blkno: 0\n");
2295 } else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
2296 mlog(ML_ERROR, "bad system_dir_blkno: 0\n");
2297 } else if (le16_to_cpu(di->id2.i_super.s_max_slots) > OCFS2_MAX_SLOTS) {
2298 mlog(ML_ERROR,
2299 "Superblock slots found greater than file system "
2300 "maximum: found %u, max %u\n",
2301 le16_to_cpu(di->id2.i_super.s_max_slots),
2302 OCFS2_MAX_SLOTS);
2303 } else {
2304 /* found it! */
2305 status = 0;
2309 out:
2310 mlog_exit(status);
2311 return status;
2314 static int ocfs2_check_volume(struct ocfs2_super *osb)
2316 int status;
2317 int dirty;
2318 int local;
2319 struct ocfs2_dinode *local_alloc = NULL; /* only used if we
2320 * recover
2321 * ourselves. */
2323 mlog_entry_void();
2325 /* Init our journal object. */
2326 status = ocfs2_journal_init(osb->journal, &dirty);
2327 if (status < 0) {
2328 mlog(ML_ERROR, "Could not initialize journal!\n");
2329 goto finally;
2332 /* If the journal was unmounted cleanly then we don't want to
2333 * recover anything. Otherwise, journal_load will do that
2334 * dirty work for us :) */
2335 if (!dirty) {
2336 status = ocfs2_journal_wipe(osb->journal, 0);
2337 if (status < 0) {
2338 mlog_errno(status);
2339 goto finally;
2341 } else {
2342 mlog(ML_NOTICE, "File system was not unmounted cleanly, "
2343 "recovering volume.\n");
2346 local = ocfs2_mount_local(osb);
2348 /* will play back anything left in the journal. */
2349 status = ocfs2_journal_load(osb->journal, local, dirty);
2350 if (status < 0) {
2351 mlog(ML_ERROR, "ocfs2 journal load failed! %d\n", status);
2352 goto finally;
2355 if (dirty) {
2356 /* recover my local alloc if we didn't unmount cleanly. */
2357 status = ocfs2_begin_local_alloc_recovery(osb,
2358 osb->slot_num,
2359 &local_alloc);
2360 if (status < 0) {
2361 mlog_errno(status);
2362 goto finally;
2364 /* we complete the recovery process after we've marked
2365 * ourselves as mounted. */
2368 mlog(0, "Journal loaded.\n");
2370 status = ocfs2_load_local_alloc(osb);
2371 if (status < 0) {
2372 mlog_errno(status);
2373 goto finally;
2376 if (dirty) {
2377 /* Recovery will be completed after we've mounted the
2378 * rest of the volume. */
2379 osb->dirty = 1;
2380 osb->local_alloc_copy = local_alloc;
2381 local_alloc = NULL;
2384 /* go through each journal, trylock it and if you get the
2385 * lock, and it's marked as dirty, set the bit in the recover
2386 * map and launch a recovery thread for it. */
2387 status = ocfs2_mark_dead_nodes(osb);
2388 if (status < 0) {
2389 mlog_errno(status);
2390 goto finally;
2393 status = ocfs2_compute_replay_slots(osb);
2394 if (status < 0)
2395 mlog_errno(status);
2397 finally:
2398 if (local_alloc)
2399 kfree(local_alloc);
2401 mlog_exit(status);
2402 return status;
2406 * The routine gets called from dismount or close whenever a dismount on
2407 * volume is requested and the osb open count becomes 1.
2408 * It will remove the osb from the global list and also free up all the
2409 * initialized resources and fileobject.
2411 static void ocfs2_delete_osb(struct ocfs2_super *osb)
2413 mlog_entry_void();
2415 /* This function assumes that the caller has the main osb resource */
2417 ocfs2_free_slot_info(osb);
2419 kfree(osb->osb_orphan_wipes);
2420 kfree(osb->slot_recovery_generations);
2421 /* FIXME
2422 * This belongs in journal shutdown, but because we have to
2423 * allocate osb->journal at the start of ocfs2_initalize_osb(),
2424 * we free it here.
2426 kfree(osb->journal);
2427 if (osb->local_alloc_copy)
2428 kfree(osb->local_alloc_copy);
2429 kfree(osb->uuid_str);
2430 ocfs2_put_dlm_debug(osb->osb_dlm_debug);
2431 memset(osb, 0, sizeof(struct ocfs2_super));
2433 mlog_exit_void();
2436 /* Put OCFS2 into a readonly state, or (if the user specifies it),
2437 * panic(). We do not support continue-on-error operation. */
2438 static void ocfs2_handle_error(struct super_block *sb)
2440 struct ocfs2_super *osb = OCFS2_SB(sb);
2442 if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_PANIC)
2443 panic("OCFS2: (device %s): panic forced after error\n",
2444 sb->s_id);
2446 ocfs2_set_osb_flag(osb, OCFS2_OSB_ERROR_FS);
2448 if (sb->s_flags & MS_RDONLY &&
2449 (ocfs2_is_soft_readonly(osb) ||
2450 ocfs2_is_hard_readonly(osb)))
2451 return;
2453 printk(KERN_CRIT "File system is now read-only due to the potential "
2454 "of on-disk corruption. Please run fsck.ocfs2 once the file "
2455 "system is unmounted.\n");
2456 sb->s_flags |= MS_RDONLY;
2457 ocfs2_set_ro_flag(osb, 0);
2460 static char error_buf[1024];
2462 void __ocfs2_error(struct super_block *sb,
2463 const char *function,
2464 const char *fmt, ...)
2466 va_list args;
2468 va_start(args, fmt);
2469 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
2470 va_end(args);
2472 /* Not using mlog here because we want to show the actual
2473 * function the error came from. */
2474 printk(KERN_CRIT "OCFS2: ERROR (device %s): %s: %s\n",
2475 sb->s_id, function, error_buf);
2477 ocfs2_handle_error(sb);
2480 /* Handle critical errors. This is intentionally more drastic than
2481 * ocfs2_handle_error, so we only use for things like journal errors,
2482 * etc. */
2483 void __ocfs2_abort(struct super_block* sb,
2484 const char *function,
2485 const char *fmt, ...)
2487 va_list args;
2489 va_start(args, fmt);
2490 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
2491 va_end(args);
2493 printk(KERN_CRIT "OCFS2: abort (device %s): %s: %s\n",
2494 sb->s_id, function, error_buf);
2496 /* We don't have the cluster support yet to go straight to
2497 * hard readonly in here. Until then, we want to keep
2498 * ocfs2_abort() so that we can at least mark critical
2499 * errors.
2501 * TODO: This should abort the journal and alert other nodes
2502 * that our slot needs recovery. */
2504 /* Force a panic(). This stinks, but it's better than letting
2505 * things continue without having a proper hard readonly
2506 * here. */
2507 if (!ocfs2_mount_local(OCFS2_SB(sb)))
2508 OCFS2_SB(sb)->s_mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
2509 ocfs2_handle_error(sb);
2512 module_init(ocfs2_init);
2513 module_exit(ocfs2_exit);