blob: 41bb0197cf4c0427115bbdf6e833c4861a1764a0 [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- 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>
Sunil Mushrand5500712007-09-06 13:34:16 -070042#include <linux/mount.h>
Joel Becker6953b4c2008-01-29 16:59:56 -080043#include <linux/seq_file.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080044
45#define MLOG_MASK_PREFIX ML_SUPER
46#include <cluster/masklog.h>
47
48#include "ocfs2.h"
49
50/* this should be the only file to include a version 1 header */
51#include "ocfs1_fs_compat.h"
52
53#include "alloc.h"
54#include "dlmglue.h"
55#include "export.h"
56#include "extent_map.h"
57#include "heartbeat.h"
58#include "inode.h"
59#include "journal.h"
60#include "localalloc.h"
61#include "namei.h"
62#include "slot_map.h"
63#include "super.h"
64#include "sysfile.h"
65#include "uptodate.h"
66#include "ver.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080067#include "xattr.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080068
69#include "buffer_head_io.h"
70
Christoph Lametere18b8902006-12-06 20:33:20 -080071static struct kmem_cache *ocfs2_inode_cachep = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -080072
Mark Fashehccd979b2005-12-15 14:31:24 -080073/* OCFS2 needs to schedule several differnt types of work which
74 * require cluster locking, disk I/O, recovery waits, etc. Since these
75 * types of work tend to be heavy we avoid using the kernel events
76 * workqueue and schedule on our own. */
77struct workqueue_struct *ocfs2_wq = NULL;
78
79static struct dentry *ocfs2_debugfs_root = NULL;
80
81MODULE_AUTHOR("Oracle");
82MODULE_LICENSE("GPL");
83
Tiger Yangc0123ad2007-09-08 00:16:10 +080084struct mount_options
85{
Mark Fashehd147b3d2007-11-07 14:40:36 -080086 unsigned long commit_interval;
Tiger Yangc0123ad2007-09-08 00:16:10 +080087 unsigned long mount_opt;
88 unsigned int atime_quantum;
89 signed short slot;
Sunil Mushran2fbe8d12007-12-20 14:58:11 -080090 unsigned int localalloc_opt;
Joel Beckerb61817e2008-02-01 15:08:23 -080091 char cluster_stack[OCFS2_STACK_LABEL_LEN + 1];
Tiger Yangc0123ad2007-09-08 00:16:10 +080092};
93
Mark Fashehccd979b2005-12-15 14:31:24 -080094static int ocfs2_parse_options(struct super_block *sb, char *options,
Tiger Yangc0123ad2007-09-08 00:16:10 +080095 struct mount_options *mopt,
Sunil Mushranbaf46612007-06-18 17:00:24 -070096 int is_remount);
Sunil Mushrand5500712007-09-06 13:34:16 -070097static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt);
Mark Fashehccd979b2005-12-15 14:31:24 -080098static void ocfs2_put_super(struct super_block *sb);
99static int ocfs2_mount_volume(struct super_block *sb);
100static int ocfs2_remount(struct super_block *sb, int *flags, char *data);
101static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err);
102static int ocfs2_initialize_mem_caches(void);
103static void ocfs2_free_mem_caches(void);
104static void ocfs2_delete_osb(struct ocfs2_super *osb);
105
David Howells726c3342006-06-23 02:02:58 -0700106static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf);
Mark Fashehccd979b2005-12-15 14:31:24 -0800107
108static int ocfs2_sync_fs(struct super_block *sb, int wait);
109
110static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb);
111static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb);
Denis Chengbddb8eb32007-09-27 02:10:04 +0800112static void ocfs2_release_system_inodes(struct ocfs2_super *osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800113static int ocfs2_check_volume(struct ocfs2_super *osb);
114static int ocfs2_verify_volume(struct ocfs2_dinode *di,
115 struct buffer_head *bh,
116 u32 sectsize);
117static int ocfs2_initialize_super(struct super_block *sb,
118 struct buffer_head *bh,
119 int sector_size);
120static int ocfs2_get_sector(struct super_block *sb,
121 struct buffer_head **bh,
122 int block,
123 int sect_size);
124static void ocfs2_write_super(struct super_block *sb);
125static struct inode *ocfs2_alloc_inode(struct super_block *sb);
126static void ocfs2_destroy_inode(struct inode *inode);
127
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800128static const struct super_operations ocfs2_sops = {
Mark Fashehccd979b2005-12-15 14:31:24 -0800129 .statfs = ocfs2_statfs,
130 .alloc_inode = ocfs2_alloc_inode,
131 .destroy_inode = ocfs2_destroy_inode,
132 .drop_inode = ocfs2_drop_inode,
133 .clear_inode = ocfs2_clear_inode,
134 .delete_inode = ocfs2_delete_inode,
135 .sync_fs = ocfs2_sync_fs,
136 .write_super = ocfs2_write_super,
137 .put_super = ocfs2_put_super,
138 .remount_fs = ocfs2_remount,
Sunil Mushrand5500712007-09-06 13:34:16 -0700139 .show_options = ocfs2_show_options,
Mark Fashehccd979b2005-12-15 14:31:24 -0800140};
141
142enum {
143 Opt_barrier,
144 Opt_err_panic,
145 Opt_err_ro,
146 Opt_intr,
147 Opt_nointr,
148 Opt_hb_none,
149 Opt_hb_local,
150 Opt_data_ordered,
151 Opt_data_writeback,
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800152 Opt_atime_quantum,
Sunil Mushranbaf46612007-06-18 17:00:24 -0700153 Opt_slot,
Mark Fashehd147b3d2007-11-07 14:40:36 -0800154 Opt_commit,
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800155 Opt_localalloc,
Mark Fasheh53fc6222007-12-20 16:49:04 -0800156 Opt_localflocks,
Joel Beckerb61817e2008-02-01 15:08:23 -0800157 Opt_stack,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800158 Opt_user_xattr,
159 Opt_nouser_xattr,
Joel Becker12462f12008-09-03 20:03:40 -0700160 Opt_inode64,
Tiger Yanga68979b2008-11-14 11:17:52 +0800161 Opt_acl,
162 Opt_noacl,
Mark Fashehccd979b2005-12-15 14:31:24 -0800163 Opt_err,
164};
165
Steven Whitehousea447c092008-10-13 10:46:57 +0100166static const match_table_t tokens = {
Mark Fashehccd979b2005-12-15 14:31:24 -0800167 {Opt_barrier, "barrier=%u"},
168 {Opt_err_panic, "errors=panic"},
169 {Opt_err_ro, "errors=remount-ro"},
170 {Opt_intr, "intr"},
171 {Opt_nointr, "nointr"},
172 {Opt_hb_none, OCFS2_HB_NONE},
173 {Opt_hb_local, OCFS2_HB_LOCAL},
174 {Opt_data_ordered, "data=ordered"},
175 {Opt_data_writeback, "data=writeback"},
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800176 {Opt_atime_quantum, "atime_quantum=%u"},
Sunil Mushranbaf46612007-06-18 17:00:24 -0700177 {Opt_slot, "preferred_slot=%u"},
Mark Fashehd147b3d2007-11-07 14:40:36 -0800178 {Opt_commit, "commit=%u"},
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800179 {Opt_localalloc, "localalloc=%d"},
Mark Fasheh53fc6222007-12-20 16:49:04 -0800180 {Opt_localflocks, "localflocks"},
Joel Beckerb61817e2008-02-01 15:08:23 -0800181 {Opt_stack, "cluster_stack=%s"},
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800182 {Opt_user_xattr, "user_xattr"},
183 {Opt_nouser_xattr, "nouser_xattr"},
Joel Becker12462f12008-09-03 20:03:40 -0700184 {Opt_inode64, "inode64"},
Tiger Yanga68979b2008-11-14 11:17:52 +0800185 {Opt_acl, "acl"},
186 {Opt_noacl, "noacl"},
Mark Fashehccd979b2005-12-15 14:31:24 -0800187 {Opt_err, NULL}
188};
189
190/*
191 * write_super and sync_fs ripped right out of ext3.
192 */
193static void ocfs2_write_super(struct super_block *sb)
194{
Ingo Molnar7892f2f2006-01-09 15:59:25 -0800195 if (mutex_trylock(&sb->s_lock) != 0)
Mark Fashehccd979b2005-12-15 14:31:24 -0800196 BUG();
197 sb->s_dirt = 0;
198}
199
200static int ocfs2_sync_fs(struct super_block *sb, int wait)
201{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800202 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -0800203 tid_t target;
204 struct ocfs2_super *osb = OCFS2_SB(sb);
205
206 sb->s_dirt = 0;
207
208 if (ocfs2_is_hard_readonly(osb))
209 return -EROFS;
210
211 if (wait) {
212 status = ocfs2_flush_truncate_log(osb);
213 if (status < 0)
214 mlog_errno(status);
215 } else {
216 ocfs2_schedule_truncate_log_flush(osb, 0);
217 }
218
Joel Becker2b4e30f2008-09-03 20:03:41 -0700219 if (jbd2_journal_start_commit(OCFS2_SB(sb)->journal->j_journal,
220 &target)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800221 if (wait)
Joel Becker2b4e30f2008-09-03 20:03:41 -0700222 jbd2_log_wait_commit(OCFS2_SB(sb)->journal->j_journal,
223 target);
Mark Fashehccd979b2005-12-15 14:31:24 -0800224 }
225 return 0;
226}
227
Jan Kara1a224ad2008-08-20 15:43:36 +0200228static int ocfs2_need_system_inode(struct ocfs2_super *osb, int ino)
229{
230 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_USRQUOTA)
231 && (ino == USER_QUOTA_SYSTEM_INODE
232 || ino == LOCAL_USER_QUOTA_SYSTEM_INODE))
233 return 0;
234 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)
235 && (ino == GROUP_QUOTA_SYSTEM_INODE
236 || ino == LOCAL_GROUP_QUOTA_SYSTEM_INODE))
237 return 0;
238 return 1;
239}
240
Mark Fashehccd979b2005-12-15 14:31:24 -0800241static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb)
242{
243 struct inode *new = NULL;
244 int status = 0;
245 int i;
246
247 mlog_entry_void();
248
Jan Kara5fa06132008-01-11 00:11:45 +0100249 new = ocfs2_iget(osb, osb->root_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800250 if (IS_ERR(new)) {
251 status = PTR_ERR(new);
252 mlog_errno(status);
253 goto bail;
254 }
255 osb->root_inode = new;
256
Jan Kara5fa06132008-01-11 00:11:45 +0100257 new = ocfs2_iget(osb, osb->system_dir_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800258 if (IS_ERR(new)) {
259 status = PTR_ERR(new);
260 mlog_errno(status);
261 goto bail;
262 }
263 osb->sys_root_inode = new;
264
265 for (i = OCFS2_FIRST_ONLINE_SYSTEM_INODE;
266 i <= OCFS2_LAST_GLOBAL_SYSTEM_INODE; i++) {
Jan Kara1a224ad2008-08-20 15:43:36 +0200267 if (!ocfs2_need_system_inode(osb, i))
268 continue;
Mark Fashehccd979b2005-12-15 14:31:24 -0800269 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
270 if (!new) {
271 ocfs2_release_system_inodes(osb);
272 status = -EINVAL;
273 mlog_errno(status);
274 /* FIXME: Should ERROR_RO_FS */
275 mlog(ML_ERROR, "Unable to load system inode %d, "
276 "possibly corrupt fs?", i);
277 goto bail;
278 }
279 // the array now has one ref, so drop this one
280 iput(new);
281 }
282
283bail:
284 mlog_exit(status);
285 return status;
286}
287
288static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb)
289{
290 struct inode *new = NULL;
291 int status = 0;
292 int i;
293
294 mlog_entry_void();
295
296 for (i = OCFS2_LAST_GLOBAL_SYSTEM_INODE + 1;
297 i < NUM_SYSTEM_INODES;
298 i++) {
Jan Kara1a224ad2008-08-20 15:43:36 +0200299 if (!ocfs2_need_system_inode(osb, i))
300 continue;
Mark Fashehccd979b2005-12-15 14:31:24 -0800301 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
302 if (!new) {
303 ocfs2_release_system_inodes(osb);
304 status = -EINVAL;
305 mlog(ML_ERROR, "status=%d, sysfile=%d, slot=%d\n",
306 status, i, osb->slot_num);
307 goto bail;
308 }
309 /* the array now has one ref, so drop this one */
310 iput(new);
311 }
312
313bail:
314 mlog_exit(status);
315 return status;
316}
317
Denis Chengbddb8eb32007-09-27 02:10:04 +0800318static void ocfs2_release_system_inodes(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -0800319{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800320 int i;
Mark Fashehccd979b2005-12-15 14:31:24 -0800321 struct inode *inode;
322
323 mlog_entry_void();
324
325 for (i = 0; i < NUM_SYSTEM_INODES; i++) {
326 inode = osb->system_inodes[i];
327 if (inode) {
328 iput(inode);
329 osb->system_inodes[i] = NULL;
330 }
331 }
332
333 inode = osb->sys_root_inode;
334 if (inode) {
335 iput(inode);
336 osb->sys_root_inode = NULL;
337 }
338
339 inode = osb->root_inode;
340 if (inode) {
341 iput(inode);
342 osb->root_inode = NULL;
343 }
344
Denis Chengbddb8eb32007-09-27 02:10:04 +0800345 mlog_exit(0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800346}
347
348/* We're allocating fs objects, use GFP_NOFS */
349static struct inode *ocfs2_alloc_inode(struct super_block *sb)
350{
351 struct ocfs2_inode_info *oi;
352
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800353 oi = kmem_cache_alloc(ocfs2_inode_cachep, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800354 if (!oi)
355 return NULL;
356
Joel Becker2b4e30f2008-09-03 20:03:41 -0700357 jbd2_journal_init_jbd_inode(&oi->ip_jinode, &oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800358 return &oi->vfs_inode;
359}
360
361static void ocfs2_destroy_inode(struct inode *inode)
362{
363 kmem_cache_free(ocfs2_inode_cachep, OCFS2_I(inode));
364}
365
Mark Fasheh5a254032007-07-20 12:56:16 -0700366static unsigned long long ocfs2_max_file_offset(unsigned int bbits,
367 unsigned int cbits)
Mark Fashehccd979b2005-12-15 14:31:24 -0800368{
Mark Fasheh5a254032007-07-20 12:56:16 -0700369 unsigned int bytes = 1 << cbits;
370 unsigned int trim = bytes;
371 unsigned int bitshift = 32;
Mark Fashehccd979b2005-12-15 14:31:24 -0800372
Mark Fasheh5a254032007-07-20 12:56:16 -0700373 /*
374 * i_size and all block offsets in ocfs2 are always 64 bits
375 * wide. i_clusters is 32 bits, in cluster-sized units. So on
376 * 64 bit platforms, cluster size will be the limiting factor.
Mark Fashehccd979b2005-12-15 14:31:24 -0800377 */
378
379#if BITS_PER_LONG == 32
380# if defined(CONFIG_LBD)
Alexey Dobriyan2ecd05a2006-10-11 01:22:05 -0700381 BUILD_BUG_ON(sizeof(sector_t) != 8);
Mark Fasheh5a254032007-07-20 12:56:16 -0700382 /*
383 * We might be limited by page cache size.
384 */
385 if (bytes > PAGE_CACHE_SIZE) {
386 bytes = PAGE_CACHE_SIZE;
387 trim = 1;
388 /*
389 * Shift by 31 here so that we don't get larger than
390 * MAX_LFS_FILESIZE
391 */
392 bitshift = 31;
393 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800394# else
Mark Fasheh5a254032007-07-20 12:56:16 -0700395 /*
396 * We are limited by the size of sector_t. Use block size, as
397 * that's what we expose to the VFS.
398 */
399 bytes = 1 << bbits;
400 trim = 1;
401 bitshift = 31;
Mark Fashehccd979b2005-12-15 14:31:24 -0800402# endif
403#endif
404
Mark Fasheh5a254032007-07-20 12:56:16 -0700405 /*
406 * Trim by a whole cluster when we can actually approach the
407 * on-disk limits. Otherwise we can overflow i_clusters when
408 * an extent start is at the max offset.
409 */
410 return (((unsigned long long)bytes) << bitshift) - trim;
Mark Fashehccd979b2005-12-15 14:31:24 -0800411}
412
413static int ocfs2_remount(struct super_block *sb, int *flags, char *data)
414{
415 int incompat_features;
416 int ret = 0;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800417 struct mount_options parsed_options;
Mark Fashehccd979b2005-12-15 14:31:24 -0800418 struct ocfs2_super *osb = OCFS2_SB(sb);
419
Tiger Yangc0123ad2007-09-08 00:16:10 +0800420 if (!ocfs2_parse_options(sb, data, &parsed_options, 1)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800421 ret = -EINVAL;
422 goto out;
423 }
424
425 if ((osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) !=
Tiger Yangc0123ad2007-09-08 00:16:10 +0800426 (parsed_options.mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800427 ret = -EINVAL;
428 mlog(ML_ERROR, "Cannot change heartbeat mode on remount\n");
429 goto out;
430 }
431
432 if ((osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK) !=
Tiger Yangc0123ad2007-09-08 00:16:10 +0800433 (parsed_options.mount_opt & OCFS2_MOUNT_DATA_WRITEBACK)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800434 ret = -EINVAL;
435 mlog(ML_ERROR, "Cannot change data mode on remount\n");
436 goto out;
437 }
438
Joel Becker12462f12008-09-03 20:03:40 -0700439 /* Probably don't want this on remount; it might
440 * mess with other nodes */
441 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64) &&
442 (parsed_options.mount_opt & OCFS2_MOUNT_INODE64)) {
443 ret = -EINVAL;
444 mlog(ML_ERROR, "Cannot enable inode64 on remount\n");
445 goto out;
446 }
447
Mark Fashehccd979b2005-12-15 14:31:24 -0800448 /* We're going to/from readonly mode. */
449 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
450 /* Lock here so the check of HARD_RO and the potential
451 * setting of SOFT_RO is atomic. */
452 spin_lock(&osb->osb_lock);
453 if (osb->osb_flags & OCFS2_OSB_HARD_RO) {
454 mlog(ML_ERROR, "Remount on readonly device is forbidden.\n");
455 ret = -EROFS;
456 goto unlock_osb;
457 }
458
459 if (*flags & MS_RDONLY) {
460 mlog(0, "Going to ro mode.\n");
461 sb->s_flags |= MS_RDONLY;
462 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
463 } else {
464 mlog(0, "Making ro filesystem writeable.\n");
465
466 if (osb->osb_flags & OCFS2_OSB_ERROR_FS) {
467 mlog(ML_ERROR, "Cannot remount RDWR "
468 "filesystem due to previous errors.\n");
469 ret = -EROFS;
470 goto unlock_osb;
471 }
472 incompat_features = OCFS2_HAS_RO_COMPAT_FEATURE(sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP);
473 if (incompat_features) {
474 mlog(ML_ERROR, "Cannot remount RDWR because "
475 "of unsupported optional features "
476 "(%x).\n", incompat_features);
477 ret = -EINVAL;
478 goto unlock_osb;
479 }
480 sb->s_flags &= ~MS_RDONLY;
481 osb->osb_flags &= ~OCFS2_OSB_SOFT_RO;
482 }
483unlock_osb:
484 spin_unlock(&osb->osb_lock);
485 }
486
487 if (!ret) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800488 /* Only save off the new mount options in case of a successful
489 * remount. */
Tiger Yanga68979b2008-11-14 11:17:52 +0800490 if (!(osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR))
491 parsed_options.mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800492 osb->s_mount_opt = parsed_options.mount_opt;
493 osb->s_atime_quantum = parsed_options.atime_quantum;
494 osb->preferred_slot = parsed_options.slot;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800495 if (parsed_options.commit_interval)
496 osb->osb_commit_interval = parsed_options.commit_interval;
Mark Fashehe001e792007-11-07 14:21:45 -0800497
498 if (!ocfs2_is_hard_readonly(osb))
499 ocfs2_set_journal_params(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800500 }
501out:
502 return ret;
503}
504
505static int ocfs2_sb_probe(struct super_block *sb,
506 struct buffer_head **bh,
507 int *sector_size)
508{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800509 int status, tmpstat;
Mark Fashehccd979b2005-12-15 14:31:24 -0800510 struct ocfs1_vol_disk_hdr *hdr;
511 struct ocfs2_dinode *di;
512 int blksize;
513
514 *bh = NULL;
515
516 /* may be > 512 */
517 *sector_size = bdev_hardsect_size(sb->s_bdev);
518 if (*sector_size > OCFS2_MAX_BLOCKSIZE) {
519 mlog(ML_ERROR, "Hardware sector size too large: %d (max=%d)\n",
520 *sector_size, OCFS2_MAX_BLOCKSIZE);
521 status = -EINVAL;
522 goto bail;
523 }
524
525 /* Can this really happen? */
526 if (*sector_size < OCFS2_MIN_BLOCKSIZE)
527 *sector_size = OCFS2_MIN_BLOCKSIZE;
528
529 /* check block zero for old format */
530 status = ocfs2_get_sector(sb, bh, 0, *sector_size);
531 if (status < 0) {
532 mlog_errno(status);
533 goto bail;
534 }
535 hdr = (struct ocfs1_vol_disk_hdr *) (*bh)->b_data;
536 if (hdr->major_version == OCFS1_MAJOR_VERSION) {
537 mlog(ML_ERROR, "incompatible version: %u.%u\n",
538 hdr->major_version, hdr->minor_version);
539 status = -EINVAL;
540 }
541 if (memcmp(hdr->signature, OCFS1_VOLUME_SIGNATURE,
542 strlen(OCFS1_VOLUME_SIGNATURE)) == 0) {
543 mlog(ML_ERROR, "incompatible volume signature: %8s\n",
544 hdr->signature);
545 status = -EINVAL;
546 }
547 brelse(*bh);
548 *bh = NULL;
549 if (status < 0) {
550 mlog(ML_ERROR, "This is an ocfs v1 filesystem which must be "
551 "upgraded before mounting with ocfs v2\n");
552 goto bail;
553 }
554
555 /*
556 * Now check at magic offset for 512, 1024, 2048, 4096
557 * blocksizes. 4096 is the maximum blocksize because it is
558 * the minimum clustersize.
559 */
560 status = -EINVAL;
561 for (blksize = *sector_size;
562 blksize <= OCFS2_MAX_BLOCKSIZE;
563 blksize <<= 1) {
564 tmpstat = ocfs2_get_sector(sb, bh,
565 OCFS2_SUPER_BLOCK_BLKNO,
566 blksize);
567 if (tmpstat < 0) {
568 status = tmpstat;
569 mlog_errno(status);
570 goto bail;
571 }
572 di = (struct ocfs2_dinode *) (*bh)->b_data;
573 status = ocfs2_verify_volume(di, *bh, blksize);
574 if (status >= 0)
575 goto bail;
576 brelse(*bh);
577 *bh = NULL;
578 if (status != -EAGAIN)
579 break;
580 }
581
582bail:
583 return status;
584}
585
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800586static int ocfs2_verify_heartbeat(struct ocfs2_super *osb)
587{
588 if (ocfs2_mount_local(osb)) {
589 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
590 mlog(ML_ERROR, "Cannot heartbeat on a locally "
591 "mounted device.\n");
592 return -EINVAL;
593 }
594 }
595
Joel Beckerb61817e2008-02-01 15:08:23 -0800596 if (ocfs2_userspace_stack(osb)) {
597 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
598 mlog(ML_ERROR, "Userspace stack expected, but "
599 "o2cb heartbeat arguments passed to mount\n");
600 return -EINVAL;
601 }
602 }
603
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800604 if (!(osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
Joel Beckerb61817e2008-02-01 15:08:23 -0800605 if (!ocfs2_mount_local(osb) && !ocfs2_is_hard_readonly(osb) &&
606 !ocfs2_userspace_stack(osb)) {
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800607 mlog(ML_ERROR, "Heartbeat has to be started to mount "
608 "a read-write clustered device.\n");
609 return -EINVAL;
610 }
611 }
612
613 return 0;
614}
615
Joel Beckerb61817e2008-02-01 15:08:23 -0800616/*
617 * If we're using a userspace stack, mount should have passed
618 * a name that matches the disk. If not, mount should not
619 * have passed a stack.
620 */
621static int ocfs2_verify_userspace_stack(struct ocfs2_super *osb,
622 struct mount_options *mopt)
623{
624 if (!ocfs2_userspace_stack(osb) && mopt->cluster_stack[0]) {
625 mlog(ML_ERROR,
626 "cluster stack passed to mount, but this filesystem "
627 "does not support it\n");
628 return -EINVAL;
629 }
630
631 if (ocfs2_userspace_stack(osb) &&
632 strncmp(osb->osb_cluster_stack, mopt->cluster_stack,
633 OCFS2_STACK_LABEL_LEN)) {
634 mlog(ML_ERROR,
635 "cluster stack passed to mount (\"%s\") does not "
636 "match the filesystem (\"%s\")\n",
637 mopt->cluster_stack,
638 osb->osb_cluster_stack);
639 return -EINVAL;
640 }
641
642 return 0;
643}
644
Mark Fashehccd979b2005-12-15 14:31:24 -0800645static int ocfs2_fill_super(struct super_block *sb, void *data, int silent)
646{
647 struct dentry *root;
648 int status, sector_size;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800649 struct mount_options parsed_options;
Mark Fashehccd979b2005-12-15 14:31:24 -0800650 struct inode *inode = NULL;
651 struct ocfs2_super *osb = NULL;
652 struct buffer_head *bh = NULL;
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800653 char nodestr[8];
Mark Fashehccd979b2005-12-15 14:31:24 -0800654
655 mlog_entry("%p, %p, %i", sb, data, silent);
656
Tiger Yangc0123ad2007-09-08 00:16:10 +0800657 if (!ocfs2_parse_options(sb, data, &parsed_options, 0)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800658 status = -EINVAL;
659 goto read_super_error;
660 }
661
662 /* probe for superblock */
663 status = ocfs2_sb_probe(sb, &bh, &sector_size);
664 if (status < 0) {
665 mlog(ML_ERROR, "superblock probe failed!\n");
666 goto read_super_error;
667 }
668
669 status = ocfs2_initialize_super(sb, bh, sector_size);
670 osb = OCFS2_SB(sb);
671 if (status < 0) {
672 mlog_errno(status);
673 goto read_super_error;
674 }
675 brelse(bh);
676 bh = NULL;
Tiger Yanga68979b2008-11-14 11:17:52 +0800677
678 if (!(osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR))
679 parsed_options.mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
680
Tiger Yangc0123ad2007-09-08 00:16:10 +0800681 osb->s_mount_opt = parsed_options.mount_opt;
682 osb->s_atime_quantum = parsed_options.atime_quantum;
683 osb->preferred_slot = parsed_options.slot;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800684 osb->osb_commit_interval = parsed_options.commit_interval;
Mark Fasheh9c7af402008-07-28 18:02:53 -0700685 osb->local_alloc_default_bits = ocfs2_megabytes_to_clusters(sb, parsed_options.localalloc_opt);
686 osb->local_alloc_bits = osb->local_alloc_default_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800687
Joel Beckerb61817e2008-02-01 15:08:23 -0800688 status = ocfs2_verify_userspace_stack(osb, &parsed_options);
689 if (status)
690 goto read_super_error;
691
Mark Fashehccd979b2005-12-15 14:31:24 -0800692 sb->s_magic = OCFS2_SUPER_MAGIC;
693
Tiger Yanga68979b2008-11-14 11:17:52 +0800694 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
695 ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
696
Mark Fashehccd979b2005-12-15 14:31:24 -0800697 /* Hard readonly mode only if: bdev_read_only, MS_RDONLY,
698 * heartbeat=none */
699 if (bdev_read_only(sb->s_bdev)) {
700 if (!(sb->s_flags & MS_RDONLY)) {
701 status = -EACCES;
702 mlog(ML_ERROR, "Readonly device detected but readonly "
703 "mount was not specified.\n");
704 goto read_super_error;
705 }
706
707 /* You should not be able to start a local heartbeat
708 * on a readonly device. */
709 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
710 status = -EROFS;
711 mlog(ML_ERROR, "Local heartbeat specified on readonly "
712 "device.\n");
713 goto read_super_error;
714 }
715
716 status = ocfs2_check_journals_nolocks(osb);
717 if (status < 0) {
718 if (status == -EROFS)
719 mlog(ML_ERROR, "Recovery required on readonly "
720 "file system, but write access is "
721 "unavailable.\n");
722 else
723 mlog_errno(status);
724 goto read_super_error;
725 }
726
727 ocfs2_set_ro_flag(osb, 1);
728
729 printk(KERN_NOTICE "Readonly device detected. No cluster "
730 "services will be utilized for this mount. Recovery "
731 "will be skipped.\n");
732 }
733
734 if (!ocfs2_is_hard_readonly(osb)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800735 if (sb->s_flags & MS_RDONLY)
736 ocfs2_set_ro_flag(osb, 0);
737 }
738
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800739 status = ocfs2_verify_heartbeat(osb);
740 if (status < 0) {
741 mlog_errno(status);
742 goto read_super_error;
743 }
744
Mark Fashehccd979b2005-12-15 14:31:24 -0800745 osb->osb_debug_root = debugfs_create_dir(osb->uuid_str,
746 ocfs2_debugfs_root);
747 if (!osb->osb_debug_root) {
748 status = -EINVAL;
749 mlog(ML_ERROR, "Unable to create per-mount debugfs root.\n");
750 goto read_super_error;
751 }
752
753 status = ocfs2_mount_volume(sb);
754 if (osb->root_inode)
755 inode = igrab(osb->root_inode);
756
757 if (status < 0)
758 goto read_super_error;
759
760 if (!inode) {
761 status = -EIO;
762 mlog_errno(status);
763 goto read_super_error;
764 }
765
766 root = d_alloc_root(inode);
767 if (!root) {
768 status = -ENOMEM;
769 mlog_errno(status);
770 goto read_super_error;
771 }
772
773 sb->s_root = root;
774
775 ocfs2_complete_mount_recovery(osb);
776
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800777 if (ocfs2_mount_local(osb))
778 snprintf(nodestr, sizeof(nodestr), "local");
779 else
Joel Becker19fdb622008-01-30 15:38:24 -0800780 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800781
782 printk(KERN_INFO "ocfs2: Mounting device (%s) on (node %s, slot %d) "
Sunil Mushran781ee3e2006-04-27 16:41:31 -0700783 "with %s data mode.\n",
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800784 osb->dev_str, nodestr, osb->slot_num,
Mark Fashehccd979b2005-12-15 14:31:24 -0800785 osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK ? "writeback" :
786 "ordered");
787
788 atomic_set(&osb->vol_state, VOLUME_MOUNTED);
789 wake_up(&osb->osb_mount_event);
790
791 mlog_exit(status);
792 return status;
793
794read_super_error:
Mark Fasheha81cb882008-10-07 14:25:16 -0700795 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800796
797 if (inode)
798 iput(inode);
799
800 if (osb) {
801 atomic_set(&osb->vol_state, VOLUME_DISABLED);
802 wake_up(&osb->osb_mount_event);
803 ocfs2_dismount_volume(sb, 1);
804 }
805
806 mlog_exit(status);
807 return status;
808}
809
David Howells454e2392006-06-23 02:02:57 -0700810static int ocfs2_get_sb(struct file_system_type *fs_type,
811 int flags,
812 const char *dev_name,
813 void *data,
814 struct vfsmount *mnt)
Mark Fashehccd979b2005-12-15 14:31:24 -0800815{
David Howells454e2392006-06-23 02:02:57 -0700816 return get_sb_bdev(fs_type, flags, dev_name, data, ocfs2_fill_super,
817 mnt);
Mark Fashehccd979b2005-12-15 14:31:24 -0800818}
819
820static struct file_system_type ocfs2_fs_type = {
821 .owner = THIS_MODULE,
822 .name = "ocfs2",
823 .get_sb = ocfs2_get_sb, /* is this called when we mount
824 * the fs? */
825 .kill_sb = kill_block_super, /* set to the generic one
826 * right now, but do we
827 * need to change that? */
Mark Fasheh1ba9da22006-09-08 14:22:54 -0700828 .fs_flags = FS_REQUIRES_DEV|FS_RENAME_DOES_D_MOVE,
Mark Fashehccd979b2005-12-15 14:31:24 -0800829 .next = NULL
830};
831
832static int ocfs2_parse_options(struct super_block *sb,
833 char *options,
Tiger Yangc0123ad2007-09-08 00:16:10 +0800834 struct mount_options *mopt,
Mark Fashehccd979b2005-12-15 14:31:24 -0800835 int is_remount)
836{
837 int status;
838 char *p;
839
840 mlog_entry("remount: %d, options: \"%s\"\n", is_remount,
841 options ? options : "(none)");
842
Mark Fashehd147b3d2007-11-07 14:40:36 -0800843 mopt->commit_interval = 0;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800844 mopt->mount_opt = 0;
845 mopt->atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
846 mopt->slot = OCFS2_INVALID_SLOT;
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800847 mopt->localalloc_opt = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE;
Joel Beckerb61817e2008-02-01 15:08:23 -0800848 mopt->cluster_stack[0] = '\0';
Mark Fashehccd979b2005-12-15 14:31:24 -0800849
850 if (!options) {
851 status = 1;
852 goto bail;
853 }
854
855 while ((p = strsep(&options, ",")) != NULL) {
856 int token, option;
857 substring_t args[MAX_OPT_ARGS];
858
859 if (!*p)
860 continue;
861
862 token = match_token(p, tokens, args);
863 switch (token) {
864 case Opt_hb_local:
Tiger Yangc0123ad2007-09-08 00:16:10 +0800865 mopt->mount_opt |= OCFS2_MOUNT_HB_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800866 break;
867 case Opt_hb_none:
Tiger Yangc0123ad2007-09-08 00:16:10 +0800868 mopt->mount_opt &= ~OCFS2_MOUNT_HB_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800869 break;
870 case Opt_barrier:
871 if (match_int(&args[0], &option)) {
872 status = 0;
873 goto bail;
874 }
875 if (option)
Tiger Yangc0123ad2007-09-08 00:16:10 +0800876 mopt->mount_opt |= OCFS2_MOUNT_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -0800877 else
Tiger Yangc0123ad2007-09-08 00:16:10 +0800878 mopt->mount_opt &= ~OCFS2_MOUNT_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -0800879 break;
880 case Opt_intr:
Tiger Yangc0123ad2007-09-08 00:16:10 +0800881 mopt->mount_opt &= ~OCFS2_MOUNT_NOINTR;
Mark Fashehccd979b2005-12-15 14:31:24 -0800882 break;
883 case Opt_nointr:
Tiger Yangc0123ad2007-09-08 00:16:10 +0800884 mopt->mount_opt |= OCFS2_MOUNT_NOINTR;
Mark Fashehccd979b2005-12-15 14:31:24 -0800885 break;
886 case Opt_err_panic:
Tiger Yangc0123ad2007-09-08 00:16:10 +0800887 mopt->mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
Mark Fashehccd979b2005-12-15 14:31:24 -0800888 break;
889 case Opt_err_ro:
Tiger Yangc0123ad2007-09-08 00:16:10 +0800890 mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_PANIC;
Mark Fashehccd979b2005-12-15 14:31:24 -0800891 break;
892 case Opt_data_ordered:
Tiger Yangc0123ad2007-09-08 00:16:10 +0800893 mopt->mount_opt &= ~OCFS2_MOUNT_DATA_WRITEBACK;
Mark Fashehccd979b2005-12-15 14:31:24 -0800894 break;
895 case Opt_data_writeback:
Tiger Yangc0123ad2007-09-08 00:16:10 +0800896 mopt->mount_opt |= OCFS2_MOUNT_DATA_WRITEBACK;
Mark Fashehccd979b2005-12-15 14:31:24 -0800897 break;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800898 case Opt_user_xattr:
899 mopt->mount_opt &= ~OCFS2_MOUNT_NOUSERXATTR;
900 break;
901 case Opt_nouser_xattr:
902 mopt->mount_opt |= OCFS2_MOUNT_NOUSERXATTR;
903 break;
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800904 case Opt_atime_quantum:
905 if (match_int(&args[0], &option)) {
906 status = 0;
907 goto bail;
908 }
909 if (option >= 0)
Tiger Yangc0123ad2007-09-08 00:16:10 +0800910 mopt->atime_quantum = option;
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800911 break;
Sunil Mushranbaf46612007-06-18 17:00:24 -0700912 case Opt_slot:
913 option = 0;
914 if (match_int(&args[0], &option)) {
915 status = 0;
916 goto bail;
917 }
918 if (option)
Tiger Yangc0123ad2007-09-08 00:16:10 +0800919 mopt->slot = (s16)option;
Sunil Mushranbaf46612007-06-18 17:00:24 -0700920 break;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800921 case Opt_commit:
922 option = 0;
923 if (match_int(&args[0], &option)) {
924 status = 0;
925 goto bail;
926 }
927 if (option < 0)
928 return 0;
929 if (option == 0)
Joel Becker2b4e30f2008-09-03 20:03:41 -0700930 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800931 mopt->commit_interval = HZ * option;
932 break;
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800933 case Opt_localalloc:
934 option = 0;
935 if (match_int(&args[0], &option)) {
936 status = 0;
937 goto bail;
938 }
939 if (option >= 0 && (option <= ocfs2_local_alloc_size(sb) * 8))
940 mopt->localalloc_opt = option;
941 break;
Mark Fasheh53fc6222007-12-20 16:49:04 -0800942 case Opt_localflocks:
943 /*
944 * Changing this during remount could race
945 * flock() requests, or "unbalance" existing
946 * ones (e.g., a lock is taken in one mode but
947 * dropped in the other). If users care enough
948 * to flip locking modes during remount, we
949 * could add a "local" flag to individual
950 * flock structures for proper tracking of
951 * state.
952 */
953 if (!is_remount)
954 mopt->mount_opt |= OCFS2_MOUNT_LOCALFLOCKS;
955 break;
Joel Beckerb61817e2008-02-01 15:08:23 -0800956 case Opt_stack:
957 /* Check both that the option we were passed
958 * is of the right length and that it is a proper
959 * string of the right length.
960 */
961 if (((args[0].to - args[0].from) !=
962 OCFS2_STACK_LABEL_LEN) ||
963 (strnlen(args[0].from,
964 OCFS2_STACK_LABEL_LEN) !=
965 OCFS2_STACK_LABEL_LEN)) {
966 mlog(ML_ERROR,
967 "Invalid cluster_stack option\n");
968 status = 0;
969 goto bail;
970 }
971 memcpy(mopt->cluster_stack, args[0].from,
972 OCFS2_STACK_LABEL_LEN);
973 mopt->cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
974 break;
Joel Becker12462f12008-09-03 20:03:40 -0700975 case Opt_inode64:
976 mopt->mount_opt |= OCFS2_MOUNT_INODE64;
977 break;
Tiger Yanga68979b2008-11-14 11:17:52 +0800978#ifdef CONFIG_OCFS2_FS_POSIX_ACL
979 case Opt_acl:
980 mopt->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
981 break;
982 case Opt_noacl:
983 mopt->mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
984 break;
985#else
986 case Opt_acl:
987 case Opt_noacl:
988 printk(KERN_INFO "ocfs2 (no)acl options not supported\n");
989 break;
990#endif
Mark Fashehccd979b2005-12-15 14:31:24 -0800991 default:
992 mlog(ML_ERROR,
993 "Unrecognized mount option \"%s\" "
994 "or missing value\n", p);
995 status = 0;
996 goto bail;
997 }
998 }
999
1000 status = 1;
1001
1002bail:
1003 mlog_exit(status);
1004 return status;
1005}
1006
Sunil Mushrand5500712007-09-06 13:34:16 -07001007static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
1008{
1009 struct ocfs2_super *osb = OCFS2_SB(mnt->mnt_sb);
1010 unsigned long opts = osb->s_mount_opt;
Mark Fashehebcee4b2008-07-28 14:55:20 -07001011 unsigned int local_alloc_megs;
Sunil Mushrand5500712007-09-06 13:34:16 -07001012
1013 if (opts & OCFS2_MOUNT_HB_LOCAL)
1014 seq_printf(s, ",_netdev,heartbeat=local");
1015 else
1016 seq_printf(s, ",heartbeat=none");
1017
1018 if (opts & OCFS2_MOUNT_NOINTR)
1019 seq_printf(s, ",nointr");
1020
1021 if (opts & OCFS2_MOUNT_DATA_WRITEBACK)
1022 seq_printf(s, ",data=writeback");
1023 else
1024 seq_printf(s, ",data=ordered");
1025
1026 if (opts & OCFS2_MOUNT_BARRIER)
1027 seq_printf(s, ",barrier=1");
1028
1029 if (opts & OCFS2_MOUNT_ERRORS_PANIC)
1030 seq_printf(s, ",errors=panic");
1031 else
1032 seq_printf(s, ",errors=remount-ro");
1033
1034 if (osb->preferred_slot != OCFS2_INVALID_SLOT)
1035 seq_printf(s, ",preferred_slot=%d", osb->preferred_slot);
1036
1037 if (osb->s_atime_quantum != OCFS2_DEFAULT_ATIME_QUANTUM)
1038 seq_printf(s, ",atime_quantum=%u", osb->s_atime_quantum);
1039
Mark Fashehd147b3d2007-11-07 14:40:36 -08001040 if (osb->osb_commit_interval)
1041 seq_printf(s, ",commit=%u",
1042 (unsigned) (osb->osb_commit_interval / HZ));
1043
Mark Fashehebcee4b2008-07-28 14:55:20 -07001044 local_alloc_megs = osb->local_alloc_bits >> (20 - osb->s_clustersize_bits);
1045 if (local_alloc_megs != OCFS2_DEFAULT_LOCAL_ALLOC_SIZE)
1046 seq_printf(s, ",localalloc=%d", local_alloc_megs);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001047
Mark Fasheh53fc6222007-12-20 16:49:04 -08001048 if (opts & OCFS2_MOUNT_LOCALFLOCKS)
1049 seq_printf(s, ",localflocks,");
1050
Joel Beckerb61817e2008-02-01 15:08:23 -08001051 if (osb->osb_cluster_stack[0])
1052 seq_printf(s, ",cluster_stack=%.*s", OCFS2_STACK_LABEL_LEN,
1053 osb->osb_cluster_stack);
1054
Sunil Mushranb0f73cf2008-09-05 11:29:14 -07001055 if (opts & OCFS2_MOUNT_NOUSERXATTR)
1056 seq_printf(s, ",nouser_xattr");
1057 else
1058 seq_printf(s, ",user_xattr");
1059
Joel Becker12462f12008-09-03 20:03:40 -07001060 if (opts & OCFS2_MOUNT_INODE64)
1061 seq_printf(s, ",inode64");
1062
Tiger Yanga68979b2008-11-14 11:17:52 +08001063#ifdef CONFIG_OCFS2_FS_POSIX_ACL
1064 if (opts & OCFS2_MOUNT_POSIX_ACL)
1065 seq_printf(s, ",acl");
1066 else
1067 seq_printf(s, ",noacl");
1068#endif
1069
Sunil Mushrand5500712007-09-06 13:34:16 -07001070 return 0;
1071}
1072
Mark Fashehccd979b2005-12-15 14:31:24 -08001073static int __init ocfs2_init(void)
1074{
1075 int status;
1076
1077 mlog_entry_void();
1078
1079 ocfs2_print_version();
1080
Mark Fashehccd979b2005-12-15 14:31:24 -08001081 status = init_ocfs2_uptodate_cache();
1082 if (status < 0) {
1083 mlog_errno(status);
1084 goto leave;
1085 }
1086
1087 status = ocfs2_initialize_mem_caches();
1088 if (status < 0) {
1089 mlog_errno(status);
1090 goto leave;
1091 }
1092
1093 ocfs2_wq = create_singlethread_workqueue("ocfs2_wq");
1094 if (!ocfs2_wq) {
1095 status = -ENOMEM;
1096 goto leave;
1097 }
1098
Mark Fashehccd979b2005-12-15 14:31:24 -08001099 ocfs2_debugfs_root = debugfs_create_dir("ocfs2", NULL);
1100 if (!ocfs2_debugfs_root) {
1101 status = -EFAULT;
1102 mlog(ML_ERROR, "Unable to create ocfs2 debugfs root.\n");
1103 }
1104
Joel Becker63e0c482008-01-30 16:58:36 -08001105 ocfs2_set_locking_protocol();
1106
Mark Fashehccd979b2005-12-15 14:31:24 -08001107leave:
1108 if (status < 0) {
1109 ocfs2_free_mem_caches();
1110 exit_ocfs2_uptodate_cache();
Mark Fashehccd979b2005-12-15 14:31:24 -08001111 }
1112
1113 mlog_exit(status);
1114
1115 if (status >= 0) {
1116 return register_filesystem(&ocfs2_fs_type);
1117 } else
1118 return -1;
1119}
1120
1121static void __exit ocfs2_exit(void)
1122{
1123 mlog_entry_void();
1124
1125 if (ocfs2_wq) {
1126 flush_workqueue(ocfs2_wq);
1127 destroy_workqueue(ocfs2_wq);
1128 }
1129
1130 debugfs_remove(ocfs2_debugfs_root);
1131
1132 ocfs2_free_mem_caches();
1133
1134 unregister_filesystem(&ocfs2_fs_type);
1135
Mark Fashehccd979b2005-12-15 14:31:24 -08001136 exit_ocfs2_uptodate_cache();
1137
1138 mlog_exit_void();
1139}
1140
1141static void ocfs2_put_super(struct super_block *sb)
1142{
1143 mlog_entry("(0x%p)\n", sb);
1144
1145 ocfs2_sync_blockdev(sb);
1146 ocfs2_dismount_volume(sb, 0);
1147
1148 mlog_exit_void();
1149}
1150
David Howells726c3342006-06-23 02:02:58 -07001151static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
Mark Fashehccd979b2005-12-15 14:31:24 -08001152{
1153 struct ocfs2_super *osb;
1154 u32 numbits, freebits;
1155 int status;
1156 struct ocfs2_dinode *bm_lock;
1157 struct buffer_head *bh = NULL;
1158 struct inode *inode = NULL;
1159
David Howells726c3342006-06-23 02:02:58 -07001160 mlog_entry("(%p, %p)\n", dentry->d_sb, buf);
Mark Fashehccd979b2005-12-15 14:31:24 -08001161
David Howells726c3342006-06-23 02:02:58 -07001162 osb = OCFS2_SB(dentry->d_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001163
1164 inode = ocfs2_get_system_file_inode(osb,
1165 GLOBAL_BITMAP_SYSTEM_INODE,
1166 OCFS2_INVALID_SLOT);
1167 if (!inode) {
1168 mlog(ML_ERROR, "failed to get bitmap inode\n");
1169 status = -EIO;
1170 goto bail;
1171 }
1172
Mark Fashehe63aecb62007-10-18 15:30:42 -07001173 status = ocfs2_inode_lock(inode, &bh, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001174 if (status < 0) {
1175 mlog_errno(status);
1176 goto bail;
1177 }
1178
1179 bm_lock = (struct ocfs2_dinode *) bh->b_data;
1180
1181 numbits = le32_to_cpu(bm_lock->id1.bitmap1.i_total);
1182 freebits = numbits - le32_to_cpu(bm_lock->id1.bitmap1.i_used);
1183
1184 buf->f_type = OCFS2_SUPER_MAGIC;
David Howells726c3342006-06-23 02:02:58 -07001185 buf->f_bsize = dentry->d_sb->s_blocksize;
Mark Fashehccd979b2005-12-15 14:31:24 -08001186 buf->f_namelen = OCFS2_MAX_FILENAME_LEN;
1187 buf->f_blocks = ((sector_t) numbits) *
1188 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1189 buf->f_bfree = ((sector_t) freebits) *
1190 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1191 buf->f_bavail = buf->f_bfree;
1192 buf->f_files = numbits;
1193 buf->f_ffree = freebits;
1194
1195 brelse(bh);
1196
Mark Fashehe63aecb62007-10-18 15:30:42 -07001197 ocfs2_inode_unlock(inode, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001198 status = 0;
1199bail:
1200 if (inode)
1201 iput(inode);
1202
1203 mlog_exit(status);
1204
1205 return status;
1206}
1207
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001208static void ocfs2_inode_init_once(void *data)
Mark Fashehccd979b2005-12-15 14:31:24 -08001209{
1210 struct ocfs2_inode_info *oi = data;
1211
Christoph Lametera35afb82007-05-16 22:10:57 -07001212 oi->ip_flags = 0;
1213 oi->ip_open_count = 0;
1214 spin_lock_init(&oi->ip_lock);
1215 ocfs2_extent_map_init(&oi->vfs_inode);
1216 INIT_LIST_HEAD(&oi->ip_io_markers);
1217 oi->ip_created_trans = 0;
1218 oi->ip_last_trans = 0;
1219 oi->ip_dir_start_lookup = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001220
Christoph Lametera35afb82007-05-16 22:10:57 -07001221 init_rwsem(&oi->ip_alloc_sem);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001222 init_rwsem(&oi->ip_xattr_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -07001223 mutex_init(&oi->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001224
Christoph Lametera35afb82007-05-16 22:10:57 -07001225 oi->ip_blkno = 0ULL;
1226 oi->ip_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001227
Christoph Lametera35afb82007-05-16 22:10:57 -07001228 ocfs2_lock_res_init_once(&oi->ip_rw_lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001229 ocfs2_lock_res_init_once(&oi->ip_inode_lockres);
Christoph Lametera35afb82007-05-16 22:10:57 -07001230 ocfs2_lock_res_init_once(&oi->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001231
Christoph Lametera35afb82007-05-16 22:10:57 -07001232 ocfs2_metadata_cache_init(&oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001233
Christoph Lametera35afb82007-05-16 22:10:57 -07001234 inode_init_once(&oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001235}
1236
1237static int ocfs2_initialize_mem_caches(void)
1238{
1239 ocfs2_inode_cachep = kmem_cache_create("ocfs2_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001240 sizeof(struct ocfs2_inode_info),
1241 0,
1242 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1243 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001244 ocfs2_inode_init_once);
Mark Fashehccd979b2005-12-15 14:31:24 -08001245 if (!ocfs2_inode_cachep)
1246 return -ENOMEM;
1247
Mark Fashehccd979b2005-12-15 14:31:24 -08001248 return 0;
1249}
1250
1251static void ocfs2_free_mem_caches(void)
1252{
1253 if (ocfs2_inode_cachep)
1254 kmem_cache_destroy(ocfs2_inode_cachep);
Mark Fashehccd979b2005-12-15 14:31:24 -08001255
1256 ocfs2_inode_cachep = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001257}
1258
1259static int ocfs2_get_sector(struct super_block *sb,
1260 struct buffer_head **bh,
1261 int block,
1262 int sect_size)
1263{
1264 if (!sb_set_blocksize(sb, sect_size)) {
1265 mlog(ML_ERROR, "unable to set blocksize\n");
1266 return -EIO;
1267 }
1268
1269 *bh = sb_getblk(sb, block);
1270 if (!*bh) {
1271 mlog_errno(-EIO);
1272 return -EIO;
1273 }
1274 lock_buffer(*bh);
1275 if (!buffer_dirty(*bh))
1276 clear_buffer_uptodate(*bh);
1277 unlock_buffer(*bh);
1278 ll_rw_block(READ, 1, bh);
1279 wait_on_buffer(*bh);
1280 return 0;
1281}
1282
Mark Fashehccd979b2005-12-15 14:31:24 -08001283static int ocfs2_mount_volume(struct super_block *sb)
1284{
1285 int status = 0;
1286 int unlock_super = 0;
1287 struct ocfs2_super *osb = OCFS2_SB(sb);
1288
1289 mlog_entry_void();
1290
1291 if (ocfs2_is_hard_readonly(osb))
1292 goto leave;
1293
Mark Fashehccd979b2005-12-15 14:31:24 -08001294 status = ocfs2_dlm_init(osb);
1295 if (status < 0) {
1296 mlog_errno(status);
1297 goto leave;
1298 }
1299
Mark Fashehccd979b2005-12-15 14:31:24 -08001300 status = ocfs2_super_lock(osb, 1);
1301 if (status < 0) {
1302 mlog_errno(status);
1303 goto leave;
1304 }
1305 unlock_super = 1;
1306
1307 /* This will load up the node map and add ourselves to it. */
1308 status = ocfs2_find_slot(osb);
1309 if (status < 0) {
1310 mlog_errno(status);
1311 goto leave;
1312 }
1313
Mark Fashehccd979b2005-12-15 14:31:24 -08001314 /* load all node-local system inodes */
1315 status = ocfs2_init_local_system_inodes(osb);
1316 if (status < 0) {
1317 mlog_errno(status);
1318 goto leave;
1319 }
1320
1321 status = ocfs2_check_volume(osb);
1322 if (status < 0) {
1323 mlog_errno(status);
1324 goto leave;
1325 }
1326
1327 status = ocfs2_truncate_log_init(osb);
1328 if (status < 0) {
1329 mlog_errno(status);
1330 goto leave;
1331 }
1332
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001333 if (ocfs2_mount_local(osb))
1334 goto leave;
1335
Mark Fashehccd979b2005-12-15 14:31:24 -08001336leave:
1337 if (unlock_super)
1338 ocfs2_super_unlock(osb, 1);
1339
1340 mlog_exit(status);
1341 return status;
1342}
1343
Mark Fashehccd979b2005-12-15 14:31:24 -08001344static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
1345{
Joel Becker286eaa92008-02-01 15:03:57 -08001346 int tmp, hangup_needed = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001347 struct ocfs2_super *osb = NULL;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001348 char nodestr[8];
Mark Fashehccd979b2005-12-15 14:31:24 -08001349
1350 mlog_entry("(0x%p)\n", sb);
1351
1352 BUG_ON(!sb);
1353 osb = OCFS2_SB(sb);
1354 BUG_ON(!osb);
1355
1356 ocfs2_shutdown_local_alloc(osb);
1357
1358 ocfs2_truncate_log_shutdown(osb);
1359
Joel Becker553abd02008-02-01 12:03:57 -08001360 /* This will disable recovery and flush any recovery work. */
1361 ocfs2_recovery_exit(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001362
1363 ocfs2_journal_shutdown(osb);
1364
1365 ocfs2_sync_blockdev(sb);
1366
Joel Becker4670c462008-02-01 14:39:35 -08001367 /* No cluster connection means we've failed during mount, so skip
1368 * all the steps which depended on that to complete. */
1369 if (osb->cconn) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001370 tmp = ocfs2_super_lock(osb, 1);
1371 if (tmp < 0) {
1372 mlog_errno(tmp);
1373 return;
1374 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001375 }
1376
Mark Fasheh19b613d2007-10-04 14:47:09 -07001377 if (osb->slot_num != OCFS2_INVALID_SLOT)
1378 ocfs2_put_slot(osb);
1379
Joel Becker4670c462008-02-01 14:39:35 -08001380 if (osb->cconn)
Mark Fasheh19b613d2007-10-04 14:47:09 -07001381 ocfs2_super_unlock(osb, 1);
1382
Mark Fashehccd979b2005-12-15 14:31:24 -08001383 ocfs2_release_system_inodes(osb);
1384
Joel Becker6953b4c2008-01-29 16:59:56 -08001385 /*
Joel Becker6953b4c2008-01-29 16:59:56 -08001386 * If we're dismounting due to mount error, mount.ocfs2 will clean
1387 * up heartbeat. If we're a local mount, there is no heartbeat.
1388 * If we failed before we got a uuid_str yet, we can't stop
1389 * heartbeat. Otherwise, do it.
1390 */
1391 if (!mnt_err && !ocfs2_mount_local(osb) && osb->uuid_str)
Joel Becker286eaa92008-02-01 15:03:57 -08001392 hangup_needed = 1;
1393
1394 if (osb->cconn)
1395 ocfs2_dlm_shutdown(osb, hangup_needed);
1396
1397 debugfs_remove(osb->osb_debug_root);
1398
1399 if (hangup_needed)
Joel Becker6953b4c2008-01-29 16:59:56 -08001400 ocfs2_cluster_hangup(osb->uuid_str, strlen(osb->uuid_str));
Mark Fashehccd979b2005-12-15 14:31:24 -08001401
1402 atomic_set(&osb->vol_state, VOLUME_DISMOUNTED);
1403
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001404 if (ocfs2_mount_local(osb))
1405 snprintf(nodestr, sizeof(nodestr), "local");
1406 else
Joel Becker19fdb622008-01-30 15:38:24 -08001407 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001408
1409 printk(KERN_INFO "ocfs2: Unmounting device (%s) on (node %s)\n",
1410 osb->dev_str, nodestr);
Mark Fashehccd979b2005-12-15 14:31:24 -08001411
1412 ocfs2_delete_osb(osb);
1413 kfree(osb);
1414 sb->s_dev = 0;
1415 sb->s_fs_info = NULL;
1416}
1417
1418static int ocfs2_setup_osb_uuid(struct ocfs2_super *osb, const unsigned char *uuid,
1419 unsigned uuid_bytes)
1420{
1421 int i, ret;
1422 char *ptr;
1423
1424 BUG_ON(uuid_bytes != OCFS2_VOL_UUID_LEN);
1425
Robert P. J. Daycd861282006-12-13 00:34:52 -08001426 osb->uuid_str = kzalloc(OCFS2_VOL_UUID_LEN * 2 + 1, GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001427 if (osb->uuid_str == NULL)
1428 return -ENOMEM;
1429
Mark Fashehccd979b2005-12-15 14:31:24 -08001430 for (i = 0, ptr = osb->uuid_str; i < OCFS2_VOL_UUID_LEN; i++) {
1431 /* print with null */
1432 ret = snprintf(ptr, 3, "%02X", uuid[i]);
1433 if (ret != 2) /* drop super cleans up */
1434 return -EINVAL;
1435 /* then only advance past the last char */
1436 ptr += 2;
1437 }
1438
1439 return 0;
1440}
1441
1442static int ocfs2_initialize_super(struct super_block *sb,
1443 struct buffer_head *bh,
1444 int sector_size)
1445{
Denis Chengbddb8eb32007-09-27 02:10:04 +08001446 int status;
Mark Fasheh5a254032007-07-20 12:56:16 -07001447 int i, cbits, bbits;
1448 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001449 struct inode *inode = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001450 struct ocfs2_journal *journal;
1451 __le32 uuid_net_key;
1452 struct ocfs2_super *osb;
1453
1454 mlog_entry_void();
1455
Robert P. J. Daycd861282006-12-13 00:34:52 -08001456 osb = kzalloc(sizeof(struct ocfs2_super), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001457 if (!osb) {
1458 status = -ENOMEM;
1459 mlog_errno(status);
1460 goto bail;
1461 }
1462
1463 sb->s_fs_info = osb;
1464 sb->s_op = &ocfs2_sops;
1465 sb->s_export_op = &ocfs2_export_ops;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001466 sb->s_xattr = ocfs2_xattr_handlers;
Mark Fashehe0dceaf2007-08-09 16:52:30 -07001467 sb->s_time_gran = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001468 sb->s_flags |= MS_NOATIME;
1469 /* this is needed to support O_LARGEFILE */
Mark Fasheh5a254032007-07-20 12:56:16 -07001470 cbits = le32_to_cpu(di->id2.i_super.s_clustersize_bits);
1471 bbits = le32_to_cpu(di->id2.i_super.s_blocksize_bits);
1472 sb->s_maxbytes = ocfs2_max_file_offset(bbits, cbits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001473
1474 osb->sb = sb;
1475 /* Save off for ocfs2_rw_direct */
1476 osb->s_sectsize_bits = blksize_bits(sector_size);
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01001477 BUG_ON(!osb->s_sectsize_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001478
Mark Fasheh34d024f2007-09-24 15:56:19 -07001479 spin_lock_init(&osb->dc_task_lock);
1480 init_waitqueue_head(&osb->dc_event);
1481 osb->dc_work_sequence = 0;
1482 osb->dc_wake_sequence = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001483 INIT_LIST_HEAD(&osb->blocked_lock_list);
1484 osb->blocked_lock_count = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001485 spin_lock_init(&osb->osb_lock);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001486 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001487
1488 atomic_set(&osb->alloc_stats.moves, 0);
1489 atomic_set(&osb->alloc_stats.local_data, 0);
1490 atomic_set(&osb->alloc_stats.bitmap_data, 0);
1491 atomic_set(&osb->alloc_stats.bg_allocs, 0);
1492 atomic_set(&osb->alloc_stats.bg_extends, 0);
1493
1494 ocfs2_init_node_maps(osb);
1495
1496 snprintf(osb->dev_str, sizeof(osb->dev_str), "%u,%u",
1497 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1498
Joel Becker553abd02008-02-01 12:03:57 -08001499 status = ocfs2_recovery_init(osb);
1500 if (status) {
1501 mlog(ML_ERROR, "Unable to initialize recovery state\n");
1502 mlog_errno(status);
1503 goto bail;
1504 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001505
1506 init_waitqueue_head(&osb->checkpoint_event);
1507 atomic_set(&osb->needs_checkpoint, 0);
1508
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001509 osb->s_atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
1510
Mark Fashehccd979b2005-12-15 14:31:24 -08001511 osb->slot_num = OCFS2_INVALID_SLOT;
1512
Tiger Yang8154da32008-08-18 17:11:46 +08001513 osb->s_xattr_inline_size = le16_to_cpu(
1514 di->id2.i_super.s_xattr_inline_size);
Tiger Yangfdd77702008-08-18 17:08:55 +08001515
Mark Fashehccd979b2005-12-15 14:31:24 -08001516 osb->local_alloc_state = OCFS2_LA_UNUSED;
1517 osb->local_alloc_bh = NULL;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001518 INIT_DELAYED_WORK(&osb->la_enable_wq, ocfs2_la_enable_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08001519
Mark Fashehccd979b2005-12-15 14:31:24 -08001520 init_waitqueue_head(&osb->osb_mount_event);
1521
1522 osb->vol_label = kmalloc(OCFS2_MAX_VOL_LABEL_LEN, GFP_KERNEL);
1523 if (!osb->vol_label) {
1524 mlog(ML_ERROR, "unable to alloc vol label\n");
1525 status = -ENOMEM;
1526 goto bail;
1527 }
1528
Mark Fashehccd979b2005-12-15 14:31:24 -08001529 osb->max_slots = le16_to_cpu(di->id2.i_super.s_max_slots);
1530 if (osb->max_slots > OCFS2_MAX_SLOTS || osb->max_slots == 0) {
1531 mlog(ML_ERROR, "Invalid number of node slots (%u)\n",
1532 osb->max_slots);
1533 status = -EINVAL;
1534 goto bail;
1535 }
Sunil Mushran781ee3e2006-04-27 16:41:31 -07001536 mlog(0, "max_slots for this device: %u\n", osb->max_slots);
Mark Fashehccd979b2005-12-15 14:31:24 -08001537
Sunil Mushran539d8262008-07-14 17:31:10 -07001538 osb->slot_recovery_generations =
1539 kcalloc(osb->max_slots, sizeof(*osb->slot_recovery_generations),
1540 GFP_KERNEL);
1541 if (!osb->slot_recovery_generations) {
1542 status = -ENOMEM;
1543 mlog_errno(status);
1544 goto bail;
1545 }
1546
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001547 init_waitqueue_head(&osb->osb_wipe_event);
1548 osb->osb_orphan_wipes = kcalloc(osb->max_slots,
1549 sizeof(*osb->osb_orphan_wipes),
1550 GFP_KERNEL);
1551 if (!osb->osb_orphan_wipes) {
1552 status = -ENOMEM;
1553 mlog_errno(status);
1554 goto bail;
1555 }
1556
Mark Fashehccd979b2005-12-15 14:31:24 -08001557 osb->s_feature_compat =
1558 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_compat);
1559 osb->s_feature_ro_compat =
1560 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_ro_compat);
1561 osb->s_feature_incompat =
1562 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_incompat);
1563
1564 if ((i = OCFS2_HAS_INCOMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_INCOMPAT_SUPP))) {
1565 mlog(ML_ERROR, "couldn't mount because of unsupported "
1566 "optional features (%x).\n", i);
1567 status = -EINVAL;
1568 goto bail;
1569 }
1570 if (!(osb->sb->s_flags & MS_RDONLY) &&
1571 (i = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP))) {
1572 mlog(ML_ERROR, "couldn't mount RDWR because of "
1573 "unsupported optional features (%x).\n", i);
1574 status = -EINVAL;
1575 goto bail;
1576 }
1577
Joel Beckerb61817e2008-02-01 15:08:23 -08001578 if (ocfs2_userspace_stack(osb)) {
1579 memcpy(osb->osb_cluster_stack,
1580 OCFS2_RAW_SB(di)->s_cluster_info.ci_stack,
1581 OCFS2_STACK_LABEL_LEN);
1582 osb->osb_cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
1583 if (strlen(osb->osb_cluster_stack) != OCFS2_STACK_LABEL_LEN) {
1584 mlog(ML_ERROR,
1585 "couldn't mount because of an invalid "
1586 "cluster stack label (%s) \n",
1587 osb->osb_cluster_stack);
1588 status = -EINVAL;
1589 goto bail;
1590 }
1591 } else {
1592 /* The empty string is identical with classic tools that
1593 * don't know about s_cluster_info. */
1594 osb->osb_cluster_stack[0] = '\0';
1595 }
1596
Mark Fashehccd979b2005-12-15 14:31:24 -08001597 get_random_bytes(&osb->s_next_generation, sizeof(u32));
1598
1599 /* FIXME
1600 * This should be done in ocfs2_journal_init(), but unknown
1601 * ordering issues will cause the filesystem to crash.
1602 * If anyone wants to figure out what part of the code
1603 * refers to osb->journal before ocfs2_journal_init() is run,
1604 * be my guest.
1605 */
1606 /* initialize our journal structure */
1607
Robert P. J. Daycd861282006-12-13 00:34:52 -08001608 journal = kzalloc(sizeof(struct ocfs2_journal), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001609 if (!journal) {
1610 mlog(ML_ERROR, "unable to alloc journal\n");
1611 status = -ENOMEM;
1612 goto bail;
1613 }
1614 osb->journal = journal;
1615 journal->j_osb = osb;
1616
1617 atomic_set(&journal->j_num_trans, 0);
1618 init_rwsem(&journal->j_trans_barrier);
1619 init_waitqueue_head(&journal->j_checkpointed);
1620 spin_lock_init(&journal->j_lock);
1621 journal->j_trans_id = (unsigned long) 1;
1622 INIT_LIST_HEAD(&journal->j_la_cleanups);
David Howellsc4028952006-11-22 14:57:56 +00001623 INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
Mark Fashehccd979b2005-12-15 14:31:24 -08001624 journal->j_state = OCFS2_JOURNAL_FREE;
1625
1626 /* get some pseudo constants for clustersize bits */
1627 osb->s_clustersize_bits =
1628 le32_to_cpu(di->id2.i_super.s_clustersize_bits);
1629 osb->s_clustersize = 1 << osb->s_clustersize_bits;
1630 mlog(0, "clusterbits=%d\n", osb->s_clustersize_bits);
1631
1632 if (osb->s_clustersize < OCFS2_MIN_CLUSTERSIZE ||
1633 osb->s_clustersize > OCFS2_MAX_CLUSTERSIZE) {
1634 mlog(ML_ERROR, "Volume has invalid cluster size (%d)\n",
1635 osb->s_clustersize);
1636 status = -EINVAL;
1637 goto bail;
1638 }
1639
1640 if (ocfs2_clusters_to_blocks(osb->sb, le32_to_cpu(di->i_clusters) - 1)
1641 > (u32)~0UL) {
1642 mlog(ML_ERROR, "Volume might try to write to blocks beyond "
1643 "what jbd can address in 32 bits.\n");
1644 status = -EINVAL;
1645 goto bail;
1646 }
1647
1648 if (ocfs2_setup_osb_uuid(osb, di->id2.i_super.s_uuid,
1649 sizeof(di->id2.i_super.s_uuid))) {
1650 mlog(ML_ERROR, "Out of memory trying to setup our uuid.\n");
1651 status = -ENOMEM;
1652 goto bail;
1653 }
1654
Mark Fasheh78427042006-05-04 12:03:26 -07001655 memcpy(&uuid_net_key, di->id2.i_super.s_uuid, sizeof(uuid_net_key));
Mark Fashehccd979b2005-12-15 14:31:24 -08001656
1657 strncpy(osb->vol_label, di->id2.i_super.s_label, 63);
1658 osb->vol_label[63] = '\0';
1659 osb->root_blkno = le64_to_cpu(di->id2.i_super.s_root_blkno);
1660 osb->system_dir_blkno = le64_to_cpu(di->id2.i_super.s_system_dir_blkno);
1661 osb->first_cluster_group_blkno =
1662 le64_to_cpu(di->id2.i_super.s_first_cluster_group);
1663 osb->fs_generation = le32_to_cpu(di->i_fs_generation);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001664 osb->uuid_hash = le32_to_cpu(di->id2.i_super.s_uuid_hash);
Mark Fashehccd979b2005-12-15 14:31:24 -08001665 mlog(0, "vol_label: %s\n", osb->vol_label);
1666 mlog(0, "uuid: %s\n", osb->uuid_str);
Mark Fashehb06970532006-03-03 10:24:33 -08001667 mlog(0, "root_blkno=%llu, system_dir_blkno=%llu\n",
1668 (unsigned long long)osb->root_blkno,
1669 (unsigned long long)osb->system_dir_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001670
1671 osb->osb_dlm_debug = ocfs2_new_dlm_debug();
1672 if (!osb->osb_dlm_debug) {
1673 status = -ENOMEM;
1674 mlog_errno(status);
1675 goto bail;
1676 }
1677
1678 atomic_set(&osb->vol_state, VOLUME_INIT);
1679
1680 /* load root, system_dir, and all global system inodes */
1681 status = ocfs2_init_global_system_inodes(osb);
1682 if (status < 0) {
1683 mlog_errno(status);
1684 goto bail;
1685 }
1686
1687 /*
1688 * global bitmap
1689 */
1690 inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
1691 OCFS2_INVALID_SLOT);
1692 if (!inode) {
1693 status = -EINVAL;
1694 mlog_errno(status);
1695 goto bail;
1696 }
1697
1698 osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001699 iput(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001700
Tao Mae9d578a2007-12-18 15:46:10 +08001701 osb->bitmap_cpg = ocfs2_group_bitmap_size(sb) * 8;
Mark Fashehccd979b2005-12-15 14:31:24 -08001702
1703 status = ocfs2_init_slot_info(osb);
1704 if (status < 0) {
1705 mlog_errno(status);
1706 goto bail;
1707 }
1708
Mark Fashehccd979b2005-12-15 14:31:24 -08001709bail:
1710 mlog_exit(status);
1711 return status;
1712}
1713
1714/*
1715 * will return: -EAGAIN if it is ok to keep searching for superblocks
1716 * -EINVAL if there is a bad superblock
1717 * 0 on success
1718 */
1719static int ocfs2_verify_volume(struct ocfs2_dinode *di,
1720 struct buffer_head *bh,
1721 u32 blksz)
1722{
1723 int status = -EAGAIN;
1724
1725 mlog_entry_void();
1726
1727 if (memcmp(di->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
1728 strlen(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) {
1729 status = -EINVAL;
1730 if ((1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits)) != blksz) {
1731 mlog(ML_ERROR, "found superblock with incorrect block "
1732 "size: found %u, should be %u\n",
1733 1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits),
1734 blksz);
1735 } else if (le16_to_cpu(di->id2.i_super.s_major_rev_level) !=
1736 OCFS2_MAJOR_REV_LEVEL ||
1737 le16_to_cpu(di->id2.i_super.s_minor_rev_level) !=
1738 OCFS2_MINOR_REV_LEVEL) {
1739 mlog(ML_ERROR, "found superblock with bad version: "
1740 "found %u.%u, should be %u.%u\n",
1741 le16_to_cpu(di->id2.i_super.s_major_rev_level),
1742 le16_to_cpu(di->id2.i_super.s_minor_rev_level),
1743 OCFS2_MAJOR_REV_LEVEL,
1744 OCFS2_MINOR_REV_LEVEL);
1745 } else if (bh->b_blocknr != le64_to_cpu(di->i_blkno)) {
1746 mlog(ML_ERROR, "bad block number on superblock: "
Mark Fashehb06970532006-03-03 10:24:33 -08001747 "found %llu, should be %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001748 (unsigned long long)le64_to_cpu(di->i_blkno),
Mark Fashehb06970532006-03-03 10:24:33 -08001749 (unsigned long long)bh->b_blocknr);
Mark Fashehccd979b2005-12-15 14:31:24 -08001750 } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
1751 le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
1752 mlog(ML_ERROR, "bad cluster size found: %u\n",
1753 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
1754 } else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
1755 mlog(ML_ERROR, "bad root_blkno: 0\n");
1756 } else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
1757 mlog(ML_ERROR, "bad system_dir_blkno: 0\n");
1758 } else if (le16_to_cpu(di->id2.i_super.s_max_slots) > OCFS2_MAX_SLOTS) {
1759 mlog(ML_ERROR,
1760 "Superblock slots found greater than file system "
1761 "maximum: found %u, max %u\n",
1762 le16_to_cpu(di->id2.i_super.s_max_slots),
1763 OCFS2_MAX_SLOTS);
1764 } else {
1765 /* found it! */
1766 status = 0;
1767 }
1768 }
1769
1770 mlog_exit(status);
1771 return status;
1772}
1773
1774static int ocfs2_check_volume(struct ocfs2_super *osb)
1775{
Denis Chengbddb8eb32007-09-27 02:10:04 +08001776 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001777 int dirty;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001778 int local;
Mark Fashehccd979b2005-12-15 14:31:24 -08001779 struct ocfs2_dinode *local_alloc = NULL; /* only used if we
1780 * recover
1781 * ourselves. */
1782
1783 mlog_entry_void();
1784
1785 /* Init our journal object. */
1786 status = ocfs2_journal_init(osb->journal, &dirty);
1787 if (status < 0) {
1788 mlog(ML_ERROR, "Could not initialize journal!\n");
1789 goto finally;
1790 }
1791
1792 /* If the journal was unmounted cleanly then we don't want to
1793 * recover anything. Otherwise, journal_load will do that
1794 * dirty work for us :) */
1795 if (!dirty) {
1796 status = ocfs2_journal_wipe(osb->journal, 0);
1797 if (status < 0) {
1798 mlog_errno(status);
1799 goto finally;
1800 }
1801 } else {
1802 mlog(ML_NOTICE, "File system was not unmounted cleanly, "
1803 "recovering volume.\n");
1804 }
1805
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001806 local = ocfs2_mount_local(osb);
1807
Mark Fashehccd979b2005-12-15 14:31:24 -08001808 /* will play back anything left in the journal. */
Sunil Mushran539d8262008-07-14 17:31:10 -07001809 status = ocfs2_journal_load(osb->journal, local, dirty);
Wengang Wang01af4822008-06-10 14:24:48 +08001810 if (status < 0) {
1811 mlog(ML_ERROR, "ocfs2 journal load failed! %d\n", status);
1812 goto finally;
1813 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001814
1815 if (dirty) {
1816 /* recover my local alloc if we didn't unmount cleanly. */
1817 status = ocfs2_begin_local_alloc_recovery(osb,
1818 osb->slot_num,
1819 &local_alloc);
1820 if (status < 0) {
1821 mlog_errno(status);
1822 goto finally;
1823 }
1824 /* we complete the recovery process after we've marked
1825 * ourselves as mounted. */
1826 }
1827
1828 mlog(0, "Journal loaded.\n");
1829
1830 status = ocfs2_load_local_alloc(osb);
1831 if (status < 0) {
1832 mlog_errno(status);
1833 goto finally;
1834 }
1835
1836 if (dirty) {
1837 /* Recovery will be completed after we've mounted the
1838 * rest of the volume. */
1839 osb->dirty = 1;
1840 osb->local_alloc_copy = local_alloc;
1841 local_alloc = NULL;
1842 }
1843
1844 /* go through each journal, trylock it and if you get the
1845 * lock, and it's marked as dirty, set the bit in the recover
1846 * map and launch a recovery thread for it. */
1847 status = ocfs2_mark_dead_nodes(osb);
1848 if (status < 0)
1849 mlog_errno(status);
1850
1851finally:
1852 if (local_alloc)
1853 kfree(local_alloc);
1854
1855 mlog_exit(status);
1856 return status;
1857}
1858
1859/*
1860 * The routine gets called from dismount or close whenever a dismount on
1861 * volume is requested and the osb open count becomes 1.
1862 * It will remove the osb from the global list and also free up all the
1863 * initialized resources and fileobject.
1864 */
1865static void ocfs2_delete_osb(struct ocfs2_super *osb)
1866{
1867 mlog_entry_void();
1868
1869 /* This function assumes that the caller has the main osb resource */
1870
Mark Fasheh8e8a4602008-02-01 11:59:09 -08001871 ocfs2_free_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001872
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001873 kfree(osb->osb_orphan_wipes);
Sunil Mushran539d8262008-07-14 17:31:10 -07001874 kfree(osb->slot_recovery_generations);
Mark Fashehccd979b2005-12-15 14:31:24 -08001875 /* FIXME
1876 * This belongs in journal shutdown, but because we have to
1877 * allocate osb->journal at the start of ocfs2_initalize_osb(),
1878 * we free it here.
1879 */
1880 kfree(osb->journal);
1881 if (osb->local_alloc_copy)
1882 kfree(osb->local_alloc_copy);
1883 kfree(osb->uuid_str);
1884 ocfs2_put_dlm_debug(osb->osb_dlm_debug);
1885 memset(osb, 0, sizeof(struct ocfs2_super));
1886
1887 mlog_exit_void();
1888}
1889
1890/* Put OCFS2 into a readonly state, or (if the user specifies it),
1891 * panic(). We do not support continue-on-error operation. */
1892static void ocfs2_handle_error(struct super_block *sb)
1893{
1894 struct ocfs2_super *osb = OCFS2_SB(sb);
1895
1896 if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_PANIC)
1897 panic("OCFS2: (device %s): panic forced after error\n",
1898 sb->s_id);
1899
1900 ocfs2_set_osb_flag(osb, OCFS2_OSB_ERROR_FS);
1901
1902 if (sb->s_flags & MS_RDONLY &&
1903 (ocfs2_is_soft_readonly(osb) ||
1904 ocfs2_is_hard_readonly(osb)))
1905 return;
1906
1907 printk(KERN_CRIT "File system is now read-only due to the potential "
1908 "of on-disk corruption. Please run fsck.ocfs2 once the file "
1909 "system is unmounted.\n");
1910 sb->s_flags |= MS_RDONLY;
1911 ocfs2_set_ro_flag(osb, 0);
1912}
1913
1914static char error_buf[1024];
1915
1916void __ocfs2_error(struct super_block *sb,
1917 const char *function,
1918 const char *fmt, ...)
1919{
1920 va_list args;
1921
1922 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001923 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Mark Fashehccd979b2005-12-15 14:31:24 -08001924 va_end(args);
1925
1926 /* Not using mlog here because we want to show the actual
1927 * function the error came from. */
1928 printk(KERN_CRIT "OCFS2: ERROR (device %s): %s: %s\n",
1929 sb->s_id, function, error_buf);
1930
1931 ocfs2_handle_error(sb);
1932}
1933
1934/* Handle critical errors. This is intentionally more drastic than
1935 * ocfs2_handle_error, so we only use for things like journal errors,
1936 * etc. */
1937void __ocfs2_abort(struct super_block* sb,
1938 const char *function,
1939 const char *fmt, ...)
1940{
1941 va_list args;
1942
1943 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001944 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Mark Fashehccd979b2005-12-15 14:31:24 -08001945 va_end(args);
1946
1947 printk(KERN_CRIT "OCFS2: abort (device %s): %s: %s\n",
1948 sb->s_id, function, error_buf);
1949
1950 /* We don't have the cluster support yet to go straight to
1951 * hard readonly in here. Until then, we want to keep
1952 * ocfs2_abort() so that we can at least mark critical
1953 * errors.
1954 *
1955 * TODO: This should abort the journal and alert other nodes
1956 * that our slot needs recovery. */
1957
1958 /* Force a panic(). This stinks, but it's better than letting
1959 * things continue without having a proper hard readonly
1960 * here. */
1961 OCFS2_SB(sb)->s_mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
1962 ocfs2_handle_error(sb);
1963}
1964
1965module_init(ocfs2_init);
1966module_exit(ocfs2_exit);