blob: 2eb657c3e7a8f6694c419fea0787dba800d9e781 [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>
Jan Kara19ece542008-08-21 20:13:17 +020044#include <linux/quotaops.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080045
46#define MLOG_MASK_PREFIX ML_SUPER
47#include <cluster/masklog.h>
48
49#include "ocfs2.h"
50
51/* this should be the only file to include a version 1 header */
52#include "ocfs1_fs_compat.h"
53
54#include "alloc.h"
55#include "dlmglue.h"
56#include "export.h"
57#include "extent_map.h"
58#include "heartbeat.h"
59#include "inode.h"
60#include "journal.h"
61#include "localalloc.h"
62#include "namei.h"
63#include "slot_map.h"
64#include "super.h"
65#include "sysfile.h"
66#include "uptodate.h"
67#include "ver.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080068#include "xattr.h"
Jan Kara9e33d692008-08-25 19:56:50 +020069#include "quota.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080070
71#include "buffer_head_io.h"
72
Christoph Lametere18b8902006-12-06 20:33:20 -080073static struct kmem_cache *ocfs2_inode_cachep = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +020074struct kmem_cache *ocfs2_dquot_cachep;
75struct kmem_cache *ocfs2_qf_chunk_cachep;
Mark Fashehccd979b2005-12-15 14:31:24 -080076
Mark Fashehccd979b2005-12-15 14:31:24 -080077/* OCFS2 needs to schedule several differnt types of work which
78 * require cluster locking, disk I/O, recovery waits, etc. Since these
79 * types of work tend to be heavy we avoid using the kernel events
80 * workqueue and schedule on our own. */
81struct workqueue_struct *ocfs2_wq = NULL;
82
83static struct dentry *ocfs2_debugfs_root = NULL;
84
85MODULE_AUTHOR("Oracle");
86MODULE_LICENSE("GPL");
87
Tiger Yangc0123ad2007-09-08 00:16:10 +080088struct mount_options
89{
Mark Fashehd147b3d2007-11-07 14:40:36 -080090 unsigned long commit_interval;
Tiger Yangc0123ad2007-09-08 00:16:10 +080091 unsigned long mount_opt;
92 unsigned int atime_quantum;
93 signed short slot;
Sunil Mushran2fbe8d12007-12-20 14:58:11 -080094 unsigned int localalloc_opt;
Joel Beckerb61817e2008-02-01 15:08:23 -080095 char cluster_stack[OCFS2_STACK_LABEL_LEN + 1];
Tiger Yangc0123ad2007-09-08 00:16:10 +080096};
97
Mark Fashehccd979b2005-12-15 14:31:24 -080098static int ocfs2_parse_options(struct super_block *sb, char *options,
Tiger Yangc0123ad2007-09-08 00:16:10 +080099 struct mount_options *mopt,
Sunil Mushranbaf46612007-06-18 17:00:24 -0700100 int is_remount);
Sunil Mushrand5500712007-09-06 13:34:16 -0700101static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt);
Mark Fashehccd979b2005-12-15 14:31:24 -0800102static void ocfs2_put_super(struct super_block *sb);
103static int ocfs2_mount_volume(struct super_block *sb);
104static int ocfs2_remount(struct super_block *sb, int *flags, char *data);
105static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err);
106static int ocfs2_initialize_mem_caches(void);
107static void ocfs2_free_mem_caches(void);
108static void ocfs2_delete_osb(struct ocfs2_super *osb);
109
David Howells726c3342006-06-23 02:02:58 -0700110static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf);
Mark Fashehccd979b2005-12-15 14:31:24 -0800111
112static int ocfs2_sync_fs(struct super_block *sb, int wait);
113
114static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb);
115static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb);
Denis Chengbddb8eb32007-09-27 02:10:04 +0800116static void ocfs2_release_system_inodes(struct ocfs2_super *osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800117static int ocfs2_check_volume(struct ocfs2_super *osb);
118static int ocfs2_verify_volume(struct ocfs2_dinode *di,
119 struct buffer_head *bh,
120 u32 sectsize);
121static int ocfs2_initialize_super(struct super_block *sb,
122 struct buffer_head *bh,
123 int sector_size);
124static int ocfs2_get_sector(struct super_block *sb,
125 struct buffer_head **bh,
126 int block,
127 int sect_size);
128static void ocfs2_write_super(struct super_block *sb);
129static struct inode *ocfs2_alloc_inode(struct super_block *sb);
130static void ocfs2_destroy_inode(struct inode *inode);
Jan Kara19ece542008-08-21 20:13:17 +0200131static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend);
132static int ocfs2_enable_quotas(struct ocfs2_super *osb);
133static void ocfs2_disable_quotas(struct ocfs2_super *osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800134
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800135static const struct super_operations ocfs2_sops = {
Mark Fashehccd979b2005-12-15 14:31:24 -0800136 .statfs = ocfs2_statfs,
137 .alloc_inode = ocfs2_alloc_inode,
138 .destroy_inode = ocfs2_destroy_inode,
139 .drop_inode = ocfs2_drop_inode,
140 .clear_inode = ocfs2_clear_inode,
141 .delete_inode = ocfs2_delete_inode,
142 .sync_fs = ocfs2_sync_fs,
143 .write_super = ocfs2_write_super,
144 .put_super = ocfs2_put_super,
145 .remount_fs = ocfs2_remount,
Sunil Mushrand5500712007-09-06 13:34:16 -0700146 .show_options = ocfs2_show_options,
Jan Kara9e33d692008-08-25 19:56:50 +0200147 .quota_read = ocfs2_quota_read,
148 .quota_write = ocfs2_quota_write,
Mark Fashehccd979b2005-12-15 14:31:24 -0800149};
150
151enum {
152 Opt_barrier,
153 Opt_err_panic,
154 Opt_err_ro,
155 Opt_intr,
156 Opt_nointr,
157 Opt_hb_none,
158 Opt_hb_local,
159 Opt_data_ordered,
160 Opt_data_writeback,
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800161 Opt_atime_quantum,
Sunil Mushranbaf46612007-06-18 17:00:24 -0700162 Opt_slot,
Mark Fashehd147b3d2007-11-07 14:40:36 -0800163 Opt_commit,
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800164 Opt_localalloc,
Mark Fasheh53fc6222007-12-20 16:49:04 -0800165 Opt_localflocks,
Joel Beckerb61817e2008-02-01 15:08:23 -0800166 Opt_stack,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800167 Opt_user_xattr,
168 Opt_nouser_xattr,
Joel Becker12462f12008-09-03 20:03:40 -0700169 Opt_inode64,
Tiger Yanga68979b2008-11-14 11:17:52 +0800170 Opt_acl,
171 Opt_noacl,
Jan Kara19ece542008-08-21 20:13:17 +0200172 Opt_usrquota,
173 Opt_grpquota,
Mark Fashehccd979b2005-12-15 14:31:24 -0800174 Opt_err,
175};
176
Steven Whitehousea447c092008-10-13 10:46:57 +0100177static const match_table_t tokens = {
Mark Fashehccd979b2005-12-15 14:31:24 -0800178 {Opt_barrier, "barrier=%u"},
179 {Opt_err_panic, "errors=panic"},
180 {Opt_err_ro, "errors=remount-ro"},
181 {Opt_intr, "intr"},
182 {Opt_nointr, "nointr"},
183 {Opt_hb_none, OCFS2_HB_NONE},
184 {Opt_hb_local, OCFS2_HB_LOCAL},
185 {Opt_data_ordered, "data=ordered"},
186 {Opt_data_writeback, "data=writeback"},
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800187 {Opt_atime_quantum, "atime_quantum=%u"},
Sunil Mushranbaf46612007-06-18 17:00:24 -0700188 {Opt_slot, "preferred_slot=%u"},
Mark Fashehd147b3d2007-11-07 14:40:36 -0800189 {Opt_commit, "commit=%u"},
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800190 {Opt_localalloc, "localalloc=%d"},
Mark Fasheh53fc6222007-12-20 16:49:04 -0800191 {Opt_localflocks, "localflocks"},
Joel Beckerb61817e2008-02-01 15:08:23 -0800192 {Opt_stack, "cluster_stack=%s"},
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800193 {Opt_user_xattr, "user_xattr"},
194 {Opt_nouser_xattr, "nouser_xattr"},
Joel Becker12462f12008-09-03 20:03:40 -0700195 {Opt_inode64, "inode64"},
Tiger Yanga68979b2008-11-14 11:17:52 +0800196 {Opt_acl, "acl"},
197 {Opt_noacl, "noacl"},
Jan Kara19ece542008-08-21 20:13:17 +0200198 {Opt_usrquota, "usrquota"},
199 {Opt_grpquota, "grpquota"},
Mark Fashehccd979b2005-12-15 14:31:24 -0800200 {Opt_err, NULL}
201};
202
203/*
204 * write_super and sync_fs ripped right out of ext3.
205 */
206static void ocfs2_write_super(struct super_block *sb)
207{
Ingo Molnar7892f2f42006-01-09 15:59:25 -0800208 if (mutex_trylock(&sb->s_lock) != 0)
Mark Fashehccd979b2005-12-15 14:31:24 -0800209 BUG();
210 sb->s_dirt = 0;
211}
212
213static int ocfs2_sync_fs(struct super_block *sb, int wait)
214{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800215 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -0800216 tid_t target;
217 struct ocfs2_super *osb = OCFS2_SB(sb);
218
219 sb->s_dirt = 0;
220
221 if (ocfs2_is_hard_readonly(osb))
222 return -EROFS;
223
224 if (wait) {
225 status = ocfs2_flush_truncate_log(osb);
226 if (status < 0)
227 mlog_errno(status);
228 } else {
229 ocfs2_schedule_truncate_log_flush(osb, 0);
230 }
231
Joel Becker2b4e30f2008-09-03 20:03:41 -0700232 if (jbd2_journal_start_commit(OCFS2_SB(sb)->journal->j_journal,
233 &target)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800234 if (wait)
Joel Becker2b4e30f2008-09-03 20:03:41 -0700235 jbd2_log_wait_commit(OCFS2_SB(sb)->journal->j_journal,
236 target);
Mark Fashehccd979b2005-12-15 14:31:24 -0800237 }
238 return 0;
239}
240
Jan Kara1a224ad2008-08-20 15:43:36 +0200241static int ocfs2_need_system_inode(struct ocfs2_super *osb, int ino)
242{
243 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_USRQUOTA)
244 && (ino == USER_QUOTA_SYSTEM_INODE
245 || ino == LOCAL_USER_QUOTA_SYSTEM_INODE))
246 return 0;
247 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)
248 && (ino == GROUP_QUOTA_SYSTEM_INODE
249 || ino == LOCAL_GROUP_QUOTA_SYSTEM_INODE))
250 return 0;
251 return 1;
252}
253
Mark Fashehccd979b2005-12-15 14:31:24 -0800254static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb)
255{
256 struct inode *new = NULL;
257 int status = 0;
258 int i;
259
260 mlog_entry_void();
261
Jan Kara5fa06132008-01-11 00:11:45 +0100262 new = ocfs2_iget(osb, osb->root_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800263 if (IS_ERR(new)) {
264 status = PTR_ERR(new);
265 mlog_errno(status);
266 goto bail;
267 }
268 osb->root_inode = new;
269
Jan Kara5fa06132008-01-11 00:11:45 +0100270 new = ocfs2_iget(osb, osb->system_dir_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800271 if (IS_ERR(new)) {
272 status = PTR_ERR(new);
273 mlog_errno(status);
274 goto bail;
275 }
276 osb->sys_root_inode = new;
277
278 for (i = OCFS2_FIRST_ONLINE_SYSTEM_INODE;
279 i <= OCFS2_LAST_GLOBAL_SYSTEM_INODE; i++) {
Jan Kara1a224ad2008-08-20 15:43:36 +0200280 if (!ocfs2_need_system_inode(osb, i))
281 continue;
Mark Fashehccd979b2005-12-15 14:31:24 -0800282 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
283 if (!new) {
284 ocfs2_release_system_inodes(osb);
285 status = -EINVAL;
286 mlog_errno(status);
287 /* FIXME: Should ERROR_RO_FS */
288 mlog(ML_ERROR, "Unable to load system inode %d, "
289 "possibly corrupt fs?", i);
290 goto bail;
291 }
292 // the array now has one ref, so drop this one
293 iput(new);
294 }
295
296bail:
297 mlog_exit(status);
298 return status;
299}
300
301static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb)
302{
303 struct inode *new = NULL;
304 int status = 0;
305 int i;
306
307 mlog_entry_void();
308
309 for (i = OCFS2_LAST_GLOBAL_SYSTEM_INODE + 1;
310 i < NUM_SYSTEM_INODES;
311 i++) {
Jan Kara1a224ad2008-08-20 15:43:36 +0200312 if (!ocfs2_need_system_inode(osb, i))
313 continue;
Mark Fashehccd979b2005-12-15 14:31:24 -0800314 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
315 if (!new) {
316 ocfs2_release_system_inodes(osb);
317 status = -EINVAL;
318 mlog(ML_ERROR, "status=%d, sysfile=%d, slot=%d\n",
319 status, i, osb->slot_num);
320 goto bail;
321 }
322 /* the array now has one ref, so drop this one */
323 iput(new);
324 }
325
326bail:
327 mlog_exit(status);
328 return status;
329}
330
Denis Chengbddb8eb32007-09-27 02:10:04 +0800331static void ocfs2_release_system_inodes(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -0800332{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800333 int i;
Mark Fashehccd979b2005-12-15 14:31:24 -0800334 struct inode *inode;
335
336 mlog_entry_void();
337
338 for (i = 0; i < NUM_SYSTEM_INODES; i++) {
339 inode = osb->system_inodes[i];
340 if (inode) {
341 iput(inode);
342 osb->system_inodes[i] = NULL;
343 }
344 }
345
346 inode = osb->sys_root_inode;
347 if (inode) {
348 iput(inode);
349 osb->sys_root_inode = NULL;
350 }
351
352 inode = osb->root_inode;
353 if (inode) {
354 iput(inode);
355 osb->root_inode = NULL;
356 }
357
Denis Chengbddb8eb32007-09-27 02:10:04 +0800358 mlog_exit(0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800359}
360
361/* We're allocating fs objects, use GFP_NOFS */
362static struct inode *ocfs2_alloc_inode(struct super_block *sb)
363{
364 struct ocfs2_inode_info *oi;
365
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800366 oi = kmem_cache_alloc(ocfs2_inode_cachep, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800367 if (!oi)
368 return NULL;
369
Joel Becker2b4e30f2008-09-03 20:03:41 -0700370 jbd2_journal_init_jbd_inode(&oi->ip_jinode, &oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800371 return &oi->vfs_inode;
372}
373
374static void ocfs2_destroy_inode(struct inode *inode)
375{
376 kmem_cache_free(ocfs2_inode_cachep, OCFS2_I(inode));
377}
378
Mark Fasheh5a254032007-07-20 12:56:16 -0700379static unsigned long long ocfs2_max_file_offset(unsigned int bbits,
380 unsigned int cbits)
Mark Fashehccd979b2005-12-15 14:31:24 -0800381{
Mark Fasheh5a254032007-07-20 12:56:16 -0700382 unsigned int bytes = 1 << cbits;
383 unsigned int trim = bytes;
384 unsigned int bitshift = 32;
Mark Fashehccd979b2005-12-15 14:31:24 -0800385
Mark Fasheh5a254032007-07-20 12:56:16 -0700386 /*
387 * i_size and all block offsets in ocfs2 are always 64 bits
388 * wide. i_clusters is 32 bits, in cluster-sized units. So on
389 * 64 bit platforms, cluster size will be the limiting factor.
Mark Fashehccd979b2005-12-15 14:31:24 -0800390 */
391
392#if BITS_PER_LONG == 32
393# if defined(CONFIG_LBD)
Alexey Dobriyan2ecd05a2006-10-11 01:22:05 -0700394 BUILD_BUG_ON(sizeof(sector_t) != 8);
Mark Fasheh5a254032007-07-20 12:56:16 -0700395 /*
396 * We might be limited by page cache size.
397 */
398 if (bytes > PAGE_CACHE_SIZE) {
399 bytes = PAGE_CACHE_SIZE;
400 trim = 1;
401 /*
402 * Shift by 31 here so that we don't get larger than
403 * MAX_LFS_FILESIZE
404 */
405 bitshift = 31;
406 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800407# else
Mark Fasheh5a254032007-07-20 12:56:16 -0700408 /*
409 * We are limited by the size of sector_t. Use block size, as
410 * that's what we expose to the VFS.
411 */
412 bytes = 1 << bbits;
413 trim = 1;
414 bitshift = 31;
Mark Fashehccd979b2005-12-15 14:31:24 -0800415# endif
416#endif
417
Mark Fasheh5a254032007-07-20 12:56:16 -0700418 /*
419 * Trim by a whole cluster when we can actually approach the
420 * on-disk limits. Otherwise we can overflow i_clusters when
421 * an extent start is at the max offset.
422 */
423 return (((unsigned long long)bytes) << bitshift) - trim;
Mark Fashehccd979b2005-12-15 14:31:24 -0800424}
425
426static int ocfs2_remount(struct super_block *sb, int *flags, char *data)
427{
428 int incompat_features;
429 int ret = 0;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800430 struct mount_options parsed_options;
Mark Fashehccd979b2005-12-15 14:31:24 -0800431 struct ocfs2_super *osb = OCFS2_SB(sb);
432
Tiger Yangc0123ad2007-09-08 00:16:10 +0800433 if (!ocfs2_parse_options(sb, data, &parsed_options, 1)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800434 ret = -EINVAL;
435 goto out;
436 }
437
438 if ((osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) !=
Tiger Yangc0123ad2007-09-08 00:16:10 +0800439 (parsed_options.mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800440 ret = -EINVAL;
441 mlog(ML_ERROR, "Cannot change heartbeat mode on remount\n");
442 goto out;
443 }
444
445 if ((osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK) !=
Tiger Yangc0123ad2007-09-08 00:16:10 +0800446 (parsed_options.mount_opt & OCFS2_MOUNT_DATA_WRITEBACK)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800447 ret = -EINVAL;
448 mlog(ML_ERROR, "Cannot change data mode on remount\n");
449 goto out;
450 }
451
Joel Becker12462f12008-09-03 20:03:40 -0700452 /* Probably don't want this on remount; it might
453 * mess with other nodes */
454 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64) &&
455 (parsed_options.mount_opt & OCFS2_MOUNT_INODE64)) {
456 ret = -EINVAL;
457 mlog(ML_ERROR, "Cannot enable inode64 on remount\n");
458 goto out;
459 }
460
Mark Fashehccd979b2005-12-15 14:31:24 -0800461 /* We're going to/from readonly mode. */
462 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
Jan Kara19ece542008-08-21 20:13:17 +0200463 /* Disable quota accounting before remounting RO */
464 if (*flags & MS_RDONLY) {
465 ret = ocfs2_susp_quotas(osb, 0);
466 if (ret < 0)
467 goto out;
468 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800469 /* Lock here so the check of HARD_RO and the potential
470 * setting of SOFT_RO is atomic. */
471 spin_lock(&osb->osb_lock);
472 if (osb->osb_flags & OCFS2_OSB_HARD_RO) {
473 mlog(ML_ERROR, "Remount on readonly device is forbidden.\n");
474 ret = -EROFS;
475 goto unlock_osb;
476 }
477
478 if (*flags & MS_RDONLY) {
479 mlog(0, "Going to ro mode.\n");
480 sb->s_flags |= MS_RDONLY;
481 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
482 } else {
483 mlog(0, "Making ro filesystem writeable.\n");
484
485 if (osb->osb_flags & OCFS2_OSB_ERROR_FS) {
486 mlog(ML_ERROR, "Cannot remount RDWR "
487 "filesystem due to previous errors.\n");
488 ret = -EROFS;
489 goto unlock_osb;
490 }
491 incompat_features = OCFS2_HAS_RO_COMPAT_FEATURE(sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP);
492 if (incompat_features) {
493 mlog(ML_ERROR, "Cannot remount RDWR because "
494 "of unsupported optional features "
495 "(%x).\n", incompat_features);
496 ret = -EINVAL;
497 goto unlock_osb;
498 }
499 sb->s_flags &= ~MS_RDONLY;
500 osb->osb_flags &= ~OCFS2_OSB_SOFT_RO;
501 }
502unlock_osb:
503 spin_unlock(&osb->osb_lock);
Jan Kara19ece542008-08-21 20:13:17 +0200504 /* Enable quota accounting after remounting RW */
505 if (!ret && !(*flags & MS_RDONLY)) {
506 if (sb_any_quota_suspended(sb))
507 ret = ocfs2_susp_quotas(osb, 1);
508 else
509 ret = ocfs2_enable_quotas(osb);
510 if (ret < 0) {
511 /* Return back changes... */
512 spin_lock(&osb->osb_lock);
513 sb->s_flags |= MS_RDONLY;
514 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
515 spin_unlock(&osb->osb_lock);
516 goto out;
517 }
518 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800519 }
520
521 if (!ret) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800522 /* Only save off the new mount options in case of a successful
523 * remount. */
Tiger Yanga68979b2008-11-14 11:17:52 +0800524 if (!(osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR))
525 parsed_options.mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800526 osb->s_mount_opt = parsed_options.mount_opt;
527 osb->s_atime_quantum = parsed_options.atime_quantum;
528 osb->preferred_slot = parsed_options.slot;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800529 if (parsed_options.commit_interval)
530 osb->osb_commit_interval = parsed_options.commit_interval;
Mark Fashehe001e7962007-11-07 14:21:45 -0800531
532 if (!ocfs2_is_hard_readonly(osb))
533 ocfs2_set_journal_params(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800534 }
535out:
536 return ret;
537}
538
539static int ocfs2_sb_probe(struct super_block *sb,
540 struct buffer_head **bh,
541 int *sector_size)
542{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800543 int status, tmpstat;
Mark Fashehccd979b2005-12-15 14:31:24 -0800544 struct ocfs1_vol_disk_hdr *hdr;
545 struct ocfs2_dinode *di;
546 int blksize;
547
548 *bh = NULL;
549
550 /* may be > 512 */
551 *sector_size = bdev_hardsect_size(sb->s_bdev);
552 if (*sector_size > OCFS2_MAX_BLOCKSIZE) {
553 mlog(ML_ERROR, "Hardware sector size too large: %d (max=%d)\n",
554 *sector_size, OCFS2_MAX_BLOCKSIZE);
555 status = -EINVAL;
556 goto bail;
557 }
558
559 /* Can this really happen? */
560 if (*sector_size < OCFS2_MIN_BLOCKSIZE)
561 *sector_size = OCFS2_MIN_BLOCKSIZE;
562
563 /* check block zero for old format */
564 status = ocfs2_get_sector(sb, bh, 0, *sector_size);
565 if (status < 0) {
566 mlog_errno(status);
567 goto bail;
568 }
569 hdr = (struct ocfs1_vol_disk_hdr *) (*bh)->b_data;
570 if (hdr->major_version == OCFS1_MAJOR_VERSION) {
571 mlog(ML_ERROR, "incompatible version: %u.%u\n",
572 hdr->major_version, hdr->minor_version);
573 status = -EINVAL;
574 }
575 if (memcmp(hdr->signature, OCFS1_VOLUME_SIGNATURE,
576 strlen(OCFS1_VOLUME_SIGNATURE)) == 0) {
577 mlog(ML_ERROR, "incompatible volume signature: %8s\n",
578 hdr->signature);
579 status = -EINVAL;
580 }
581 brelse(*bh);
582 *bh = NULL;
583 if (status < 0) {
584 mlog(ML_ERROR, "This is an ocfs v1 filesystem which must be "
585 "upgraded before mounting with ocfs v2\n");
586 goto bail;
587 }
588
589 /*
590 * Now check at magic offset for 512, 1024, 2048, 4096
591 * blocksizes. 4096 is the maximum blocksize because it is
592 * the minimum clustersize.
593 */
594 status = -EINVAL;
595 for (blksize = *sector_size;
596 blksize <= OCFS2_MAX_BLOCKSIZE;
597 blksize <<= 1) {
598 tmpstat = ocfs2_get_sector(sb, bh,
599 OCFS2_SUPER_BLOCK_BLKNO,
600 blksize);
601 if (tmpstat < 0) {
602 status = tmpstat;
603 mlog_errno(status);
604 goto bail;
605 }
606 di = (struct ocfs2_dinode *) (*bh)->b_data;
607 status = ocfs2_verify_volume(di, *bh, blksize);
608 if (status >= 0)
609 goto bail;
610 brelse(*bh);
611 *bh = NULL;
612 if (status != -EAGAIN)
613 break;
614 }
615
616bail:
617 return status;
618}
619
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800620static int ocfs2_verify_heartbeat(struct ocfs2_super *osb)
621{
622 if (ocfs2_mount_local(osb)) {
623 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
624 mlog(ML_ERROR, "Cannot heartbeat on a locally "
625 "mounted device.\n");
626 return -EINVAL;
627 }
628 }
629
Joel Beckerb61817e2008-02-01 15:08:23 -0800630 if (ocfs2_userspace_stack(osb)) {
631 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
632 mlog(ML_ERROR, "Userspace stack expected, but "
633 "o2cb heartbeat arguments passed to mount\n");
634 return -EINVAL;
635 }
636 }
637
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800638 if (!(osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
Joel Beckerb61817e2008-02-01 15:08:23 -0800639 if (!ocfs2_mount_local(osb) && !ocfs2_is_hard_readonly(osb) &&
640 !ocfs2_userspace_stack(osb)) {
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800641 mlog(ML_ERROR, "Heartbeat has to be started to mount "
642 "a read-write clustered device.\n");
643 return -EINVAL;
644 }
645 }
646
647 return 0;
648}
649
Joel Beckerb61817e2008-02-01 15:08:23 -0800650/*
651 * If we're using a userspace stack, mount should have passed
652 * a name that matches the disk. If not, mount should not
653 * have passed a stack.
654 */
655static int ocfs2_verify_userspace_stack(struct ocfs2_super *osb,
656 struct mount_options *mopt)
657{
658 if (!ocfs2_userspace_stack(osb) && mopt->cluster_stack[0]) {
659 mlog(ML_ERROR,
660 "cluster stack passed to mount, but this filesystem "
661 "does not support it\n");
662 return -EINVAL;
663 }
664
665 if (ocfs2_userspace_stack(osb) &&
666 strncmp(osb->osb_cluster_stack, mopt->cluster_stack,
667 OCFS2_STACK_LABEL_LEN)) {
668 mlog(ML_ERROR,
669 "cluster stack passed to mount (\"%s\") does not "
670 "match the filesystem (\"%s\")\n",
671 mopt->cluster_stack,
672 osb->osb_cluster_stack);
673 return -EINVAL;
674 }
675
676 return 0;
677}
678
Jan Kara19ece542008-08-21 20:13:17 +0200679static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend)
680{
681 int type;
682 struct super_block *sb = osb->sb;
683 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
684 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
685 int status = 0;
686
687 for (type = 0; type < MAXQUOTAS; type++) {
688 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
689 continue;
690 if (unsuspend)
691 status = vfs_quota_enable(
692 sb_dqopt(sb)->files[type],
693 type, QFMT_OCFS2,
694 DQUOT_SUSPENDED);
695 else
696 status = vfs_quota_disable(sb, type,
697 DQUOT_SUSPENDED);
698 if (status < 0)
699 break;
700 }
701 if (status < 0)
702 mlog(ML_ERROR, "Failed to suspend/unsuspend quotas on "
703 "remount (error = %d).\n", status);
704 return status;
705}
706
707static int ocfs2_enable_quotas(struct ocfs2_super *osb)
708{
709 struct inode *inode[MAXQUOTAS] = { NULL, NULL };
710 struct super_block *sb = osb->sb;
711 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
712 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
713 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
714 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
715 int status;
716 int type;
717
718 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NEGATIVE_USAGE;
719 for (type = 0; type < MAXQUOTAS; type++) {
720 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
721 continue;
722 inode[type] = ocfs2_get_system_file_inode(osb, ino[type],
723 osb->slot_num);
724 if (!inode[type]) {
725 status = -ENOENT;
726 goto out_quota_off;
727 }
728 status = vfs_quota_enable(inode[type], type, QFMT_OCFS2,
729 DQUOT_USAGE_ENABLED);
730 if (status < 0)
731 goto out_quota_off;
732 }
733
734 for (type = 0; type < MAXQUOTAS; type++)
735 iput(inode[type]);
736 return 0;
737out_quota_off:
738 ocfs2_disable_quotas(osb);
739 for (type = 0; type < MAXQUOTAS; type++)
740 iput(inode[type]);
741 mlog_errno(status);
742 return status;
743}
744
745static void ocfs2_disable_quotas(struct ocfs2_super *osb)
746{
747 int type;
748 struct inode *inode;
749 struct super_block *sb = osb->sb;
750
751 /* We mostly ignore errors in this function because there's not much
752 * we can do when we see them */
753 for (type = 0; type < MAXQUOTAS; type++) {
754 if (!sb_has_quota_loaded(sb, type))
755 continue;
756 inode = igrab(sb->s_dquot.files[type]);
757 /* Turn off quotas. This will remove all dquot structures from
758 * memory and so they will be automatically synced to global
759 * quota files */
760 vfs_quota_disable(sb, type, DQUOT_USAGE_ENABLED |
761 DQUOT_LIMITS_ENABLED);
762 if (!inode)
763 continue;
764 iput(inode);
765 }
766}
767
768/* Handle quota on quotactl */
769static int ocfs2_quota_on(struct super_block *sb, int type, int format_id,
770 char *path, int remount)
771{
772 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
773 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
774
775 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
776 return -EINVAL;
777
778 if (remount)
779 return 0; /* Just ignore it has been handled in
780 * ocfs2_remount() */
781 return vfs_quota_enable(sb_dqopt(sb)->files[type], type,
782 format_id, DQUOT_LIMITS_ENABLED);
783}
784
785/* Handle quota off quotactl */
786static int ocfs2_quota_off(struct super_block *sb, int type, int remount)
787{
788 if (remount)
789 return 0; /* Ignore now and handle later in
790 * ocfs2_remount() */
791 return vfs_quota_disable(sb, type, DQUOT_LIMITS_ENABLED);
792}
793
794static struct quotactl_ops ocfs2_quotactl_ops = {
795 .quota_on = ocfs2_quota_on,
796 .quota_off = ocfs2_quota_off,
797 .quota_sync = vfs_quota_sync,
798 .get_info = vfs_get_dqinfo,
799 .set_info = vfs_set_dqinfo,
800 .get_dqblk = vfs_get_dqblk,
801 .set_dqblk = vfs_set_dqblk,
802};
803
Mark Fashehccd979b2005-12-15 14:31:24 -0800804static int ocfs2_fill_super(struct super_block *sb, void *data, int silent)
805{
806 struct dentry *root;
807 int status, sector_size;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800808 struct mount_options parsed_options;
Mark Fashehccd979b2005-12-15 14:31:24 -0800809 struct inode *inode = NULL;
810 struct ocfs2_super *osb = NULL;
811 struct buffer_head *bh = NULL;
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800812 char nodestr[8];
Mark Fashehccd979b2005-12-15 14:31:24 -0800813
814 mlog_entry("%p, %p, %i", sb, data, silent);
815
Tiger Yangc0123ad2007-09-08 00:16:10 +0800816 if (!ocfs2_parse_options(sb, data, &parsed_options, 0)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800817 status = -EINVAL;
818 goto read_super_error;
819 }
820
821 /* probe for superblock */
822 status = ocfs2_sb_probe(sb, &bh, &sector_size);
823 if (status < 0) {
824 mlog(ML_ERROR, "superblock probe failed!\n");
825 goto read_super_error;
826 }
827
828 status = ocfs2_initialize_super(sb, bh, sector_size);
829 osb = OCFS2_SB(sb);
830 if (status < 0) {
831 mlog_errno(status);
832 goto read_super_error;
833 }
834 brelse(bh);
835 bh = NULL;
Tiger Yanga68979b2008-11-14 11:17:52 +0800836
837 if (!(osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR))
838 parsed_options.mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
839
Tiger Yangc0123ad2007-09-08 00:16:10 +0800840 osb->s_mount_opt = parsed_options.mount_opt;
841 osb->s_atime_quantum = parsed_options.atime_quantum;
842 osb->preferred_slot = parsed_options.slot;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800843 osb->osb_commit_interval = parsed_options.commit_interval;
Mark Fasheh9c7af402008-07-28 18:02:53 -0700844 osb->local_alloc_default_bits = ocfs2_megabytes_to_clusters(sb, parsed_options.localalloc_opt);
845 osb->local_alloc_bits = osb->local_alloc_default_bits;
Jan Kara19ece542008-08-21 20:13:17 +0200846 if (osb->s_mount_opt & OCFS2_MOUNT_USRQUOTA &&
847 !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
848 OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
849 status = -EINVAL;
850 mlog(ML_ERROR, "User quotas were requested, but this "
851 "filesystem does not have the feature enabled.\n");
852 goto read_super_error;
853 }
854 if (osb->s_mount_opt & OCFS2_MOUNT_GRPQUOTA &&
855 !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
856 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
857 status = -EINVAL;
858 mlog(ML_ERROR, "Group quotas were requested, but this "
859 "filesystem does not have the feature enabled.\n");
860 goto read_super_error;
861 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800862
Joel Beckerb61817e2008-02-01 15:08:23 -0800863 status = ocfs2_verify_userspace_stack(osb, &parsed_options);
864 if (status)
865 goto read_super_error;
866
Mark Fashehccd979b2005-12-15 14:31:24 -0800867 sb->s_magic = OCFS2_SUPER_MAGIC;
868
Tiger Yanga68979b2008-11-14 11:17:52 +0800869 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
870 ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
871
Mark Fashehccd979b2005-12-15 14:31:24 -0800872 /* Hard readonly mode only if: bdev_read_only, MS_RDONLY,
873 * heartbeat=none */
874 if (bdev_read_only(sb->s_bdev)) {
875 if (!(sb->s_flags & MS_RDONLY)) {
876 status = -EACCES;
877 mlog(ML_ERROR, "Readonly device detected but readonly "
878 "mount was not specified.\n");
879 goto read_super_error;
880 }
881
882 /* You should not be able to start a local heartbeat
883 * on a readonly device. */
884 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
885 status = -EROFS;
886 mlog(ML_ERROR, "Local heartbeat specified on readonly "
887 "device.\n");
888 goto read_super_error;
889 }
890
891 status = ocfs2_check_journals_nolocks(osb);
892 if (status < 0) {
893 if (status == -EROFS)
894 mlog(ML_ERROR, "Recovery required on readonly "
895 "file system, but write access is "
896 "unavailable.\n");
897 else
898 mlog_errno(status);
899 goto read_super_error;
900 }
901
902 ocfs2_set_ro_flag(osb, 1);
903
904 printk(KERN_NOTICE "Readonly device detected. No cluster "
905 "services will be utilized for this mount. Recovery "
906 "will be skipped.\n");
907 }
908
909 if (!ocfs2_is_hard_readonly(osb)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800910 if (sb->s_flags & MS_RDONLY)
911 ocfs2_set_ro_flag(osb, 0);
912 }
913
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800914 status = ocfs2_verify_heartbeat(osb);
915 if (status < 0) {
916 mlog_errno(status);
917 goto read_super_error;
918 }
919
Mark Fashehccd979b2005-12-15 14:31:24 -0800920 osb->osb_debug_root = debugfs_create_dir(osb->uuid_str,
921 ocfs2_debugfs_root);
922 if (!osb->osb_debug_root) {
923 status = -EINVAL;
924 mlog(ML_ERROR, "Unable to create per-mount debugfs root.\n");
925 goto read_super_error;
926 }
927
928 status = ocfs2_mount_volume(sb);
929 if (osb->root_inode)
930 inode = igrab(osb->root_inode);
931
932 if (status < 0)
933 goto read_super_error;
934
935 if (!inode) {
936 status = -EIO;
937 mlog_errno(status);
938 goto read_super_error;
939 }
940
941 root = d_alloc_root(inode);
942 if (!root) {
943 status = -ENOMEM;
944 mlog_errno(status);
945 goto read_super_error;
946 }
947
948 sb->s_root = root;
949
950 ocfs2_complete_mount_recovery(osb);
951
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800952 if (ocfs2_mount_local(osb))
953 snprintf(nodestr, sizeof(nodestr), "local");
954 else
Joel Becker19fdb622008-01-30 15:38:24 -0800955 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800956
957 printk(KERN_INFO "ocfs2: Mounting device (%s) on (node %s, slot %d) "
Sunil Mushran781ee3e2006-04-27 16:41:31 -0700958 "with %s data mode.\n",
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800959 osb->dev_str, nodestr, osb->slot_num,
Mark Fashehccd979b2005-12-15 14:31:24 -0800960 osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK ? "writeback" :
961 "ordered");
962
963 atomic_set(&osb->vol_state, VOLUME_MOUNTED);
964 wake_up(&osb->osb_mount_event);
965
Jan Kara19ece542008-08-21 20:13:17 +0200966 /* Now we can initialize quotas because we can afford to wait
967 * for cluster locks recovery now. That also means that truncation
968 * log recovery can happen but that waits for proper quota setup */
969 if (!(sb->s_flags & MS_RDONLY)) {
970 status = ocfs2_enable_quotas(osb);
971 if (status < 0) {
972 /* We have to err-out specially here because
973 * s_root is already set */
974 mlog_errno(status);
975 atomic_set(&osb->vol_state, VOLUME_DISABLED);
976 wake_up(&osb->osb_mount_event);
977 mlog_exit(status);
978 return status;
979 }
980 }
981
982 ocfs2_complete_quota_recovery(osb);
983
984 /* Now we wake up again for processes waiting for quotas */
985 atomic_set(&osb->vol_state, VOLUME_MOUNTED_QUOTAS);
986 wake_up(&osb->osb_mount_event);
987
Mark Fashehccd979b2005-12-15 14:31:24 -0800988 mlog_exit(status);
989 return status;
990
991read_super_error:
Mark Fasheha81cb882008-10-07 14:25:16 -0700992 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800993
994 if (inode)
995 iput(inode);
996
997 if (osb) {
998 atomic_set(&osb->vol_state, VOLUME_DISABLED);
999 wake_up(&osb->osb_mount_event);
1000 ocfs2_dismount_volume(sb, 1);
1001 }
1002
1003 mlog_exit(status);
1004 return status;
1005}
1006
David Howells454e2392006-06-23 02:02:57 -07001007static int ocfs2_get_sb(struct file_system_type *fs_type,
1008 int flags,
1009 const char *dev_name,
1010 void *data,
1011 struct vfsmount *mnt)
Mark Fashehccd979b2005-12-15 14:31:24 -08001012{
David Howells454e2392006-06-23 02:02:57 -07001013 return get_sb_bdev(fs_type, flags, dev_name, data, ocfs2_fill_super,
1014 mnt);
Mark Fashehccd979b2005-12-15 14:31:24 -08001015}
1016
1017static struct file_system_type ocfs2_fs_type = {
1018 .owner = THIS_MODULE,
1019 .name = "ocfs2",
1020 .get_sb = ocfs2_get_sb, /* is this called when we mount
1021 * the fs? */
1022 .kill_sb = kill_block_super, /* set to the generic one
1023 * right now, but do we
1024 * need to change that? */
Mark Fasheh1ba9da22006-09-08 14:22:54 -07001025 .fs_flags = FS_REQUIRES_DEV|FS_RENAME_DOES_D_MOVE,
Mark Fashehccd979b2005-12-15 14:31:24 -08001026 .next = NULL
1027};
1028
1029static int ocfs2_parse_options(struct super_block *sb,
1030 char *options,
Tiger Yangc0123ad2007-09-08 00:16:10 +08001031 struct mount_options *mopt,
Mark Fashehccd979b2005-12-15 14:31:24 -08001032 int is_remount)
1033{
1034 int status;
1035 char *p;
1036
1037 mlog_entry("remount: %d, options: \"%s\"\n", is_remount,
1038 options ? options : "(none)");
1039
Mark Fashehd147b3d2007-11-07 14:40:36 -08001040 mopt->commit_interval = 0;
Tiger Yangc0123ad2007-09-08 00:16:10 +08001041 mopt->mount_opt = 0;
1042 mopt->atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
1043 mopt->slot = OCFS2_INVALID_SLOT;
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001044 mopt->localalloc_opt = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE;
Joel Beckerb61817e2008-02-01 15:08:23 -08001045 mopt->cluster_stack[0] = '\0';
Mark Fashehccd979b2005-12-15 14:31:24 -08001046
1047 if (!options) {
1048 status = 1;
1049 goto bail;
1050 }
1051
1052 while ((p = strsep(&options, ",")) != NULL) {
1053 int token, option;
1054 substring_t args[MAX_OPT_ARGS];
1055
1056 if (!*p)
1057 continue;
1058
1059 token = match_token(p, tokens, args);
1060 switch (token) {
1061 case Opt_hb_local:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001062 mopt->mount_opt |= OCFS2_MOUNT_HB_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001063 break;
1064 case Opt_hb_none:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001065 mopt->mount_opt &= ~OCFS2_MOUNT_HB_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001066 break;
1067 case Opt_barrier:
1068 if (match_int(&args[0], &option)) {
1069 status = 0;
1070 goto bail;
1071 }
1072 if (option)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001073 mopt->mount_opt |= OCFS2_MOUNT_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -08001074 else
Tiger Yangc0123ad2007-09-08 00:16:10 +08001075 mopt->mount_opt &= ~OCFS2_MOUNT_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -08001076 break;
1077 case Opt_intr:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001078 mopt->mount_opt &= ~OCFS2_MOUNT_NOINTR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001079 break;
1080 case Opt_nointr:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001081 mopt->mount_opt |= OCFS2_MOUNT_NOINTR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001082 break;
1083 case Opt_err_panic:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001084 mopt->mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001085 break;
1086 case Opt_err_ro:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001087 mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_PANIC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001088 break;
1089 case Opt_data_ordered:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001090 mopt->mount_opt &= ~OCFS2_MOUNT_DATA_WRITEBACK;
Mark Fashehccd979b2005-12-15 14:31:24 -08001091 break;
1092 case Opt_data_writeback:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001093 mopt->mount_opt |= OCFS2_MOUNT_DATA_WRITEBACK;
Mark Fashehccd979b2005-12-15 14:31:24 -08001094 break;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001095 case Opt_user_xattr:
1096 mopt->mount_opt &= ~OCFS2_MOUNT_NOUSERXATTR;
1097 break;
1098 case Opt_nouser_xattr:
1099 mopt->mount_opt |= OCFS2_MOUNT_NOUSERXATTR;
1100 break;
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001101 case Opt_atime_quantum:
1102 if (match_int(&args[0], &option)) {
1103 status = 0;
1104 goto bail;
1105 }
1106 if (option >= 0)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001107 mopt->atime_quantum = option;
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001108 break;
Sunil Mushranbaf46612007-06-18 17:00:24 -07001109 case Opt_slot:
1110 option = 0;
1111 if (match_int(&args[0], &option)) {
1112 status = 0;
1113 goto bail;
1114 }
1115 if (option)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001116 mopt->slot = (s16)option;
Sunil Mushranbaf46612007-06-18 17:00:24 -07001117 break;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001118 case Opt_commit:
1119 option = 0;
1120 if (match_int(&args[0], &option)) {
1121 status = 0;
1122 goto bail;
1123 }
1124 if (option < 0)
1125 return 0;
1126 if (option == 0)
Joel Becker2b4e30f2008-09-03 20:03:41 -07001127 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001128 mopt->commit_interval = HZ * option;
1129 break;
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001130 case Opt_localalloc:
1131 option = 0;
1132 if (match_int(&args[0], &option)) {
1133 status = 0;
1134 goto bail;
1135 }
1136 if (option >= 0 && (option <= ocfs2_local_alloc_size(sb) * 8))
1137 mopt->localalloc_opt = option;
1138 break;
Mark Fasheh53fc6222007-12-20 16:49:04 -08001139 case Opt_localflocks:
1140 /*
1141 * Changing this during remount could race
1142 * flock() requests, or "unbalance" existing
1143 * ones (e.g., a lock is taken in one mode but
1144 * dropped in the other). If users care enough
1145 * to flip locking modes during remount, we
1146 * could add a "local" flag to individual
1147 * flock structures for proper tracking of
1148 * state.
1149 */
1150 if (!is_remount)
1151 mopt->mount_opt |= OCFS2_MOUNT_LOCALFLOCKS;
1152 break;
Joel Beckerb61817e2008-02-01 15:08:23 -08001153 case Opt_stack:
1154 /* Check both that the option we were passed
1155 * is of the right length and that it is a proper
1156 * string of the right length.
1157 */
1158 if (((args[0].to - args[0].from) !=
1159 OCFS2_STACK_LABEL_LEN) ||
1160 (strnlen(args[0].from,
1161 OCFS2_STACK_LABEL_LEN) !=
1162 OCFS2_STACK_LABEL_LEN)) {
1163 mlog(ML_ERROR,
1164 "Invalid cluster_stack option\n");
1165 status = 0;
1166 goto bail;
1167 }
1168 memcpy(mopt->cluster_stack, args[0].from,
1169 OCFS2_STACK_LABEL_LEN);
1170 mopt->cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
1171 break;
Joel Becker12462f12008-09-03 20:03:40 -07001172 case Opt_inode64:
1173 mopt->mount_opt |= OCFS2_MOUNT_INODE64;
1174 break;
Jan Kara19ece542008-08-21 20:13:17 +02001175 case Opt_usrquota:
1176 /* We check only on remount, otherwise features
1177 * aren't yet initialized. */
1178 if (is_remount && !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1179 OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
1180 mlog(ML_ERROR, "User quota requested but "
1181 "filesystem feature is not set\n");
1182 status = 0;
1183 goto bail;
1184 }
1185 mopt->mount_opt |= OCFS2_MOUNT_USRQUOTA;
1186 break;
1187 case Opt_grpquota:
1188 if (is_remount && !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1189 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
1190 mlog(ML_ERROR, "Group quota requested but "
1191 "filesystem feature is not set\n");
1192 status = 0;
1193 goto bail;
1194 }
1195 mopt->mount_opt |= OCFS2_MOUNT_GRPQUOTA;
1196 break;
Tiger Yanga68979b2008-11-14 11:17:52 +08001197#ifdef CONFIG_OCFS2_FS_POSIX_ACL
1198 case Opt_acl:
1199 mopt->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
1200 break;
1201 case Opt_noacl:
1202 mopt->mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
1203 break;
1204#else
1205 case Opt_acl:
1206 case Opt_noacl:
1207 printk(KERN_INFO "ocfs2 (no)acl options not supported\n");
1208 break;
1209#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001210 default:
1211 mlog(ML_ERROR,
1212 "Unrecognized mount option \"%s\" "
1213 "or missing value\n", p);
1214 status = 0;
1215 goto bail;
1216 }
1217 }
1218
1219 status = 1;
1220
1221bail:
1222 mlog_exit(status);
1223 return status;
1224}
1225
Sunil Mushrand5500712007-09-06 13:34:16 -07001226static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
1227{
1228 struct ocfs2_super *osb = OCFS2_SB(mnt->mnt_sb);
1229 unsigned long opts = osb->s_mount_opt;
Mark Fashehebcee4b2008-07-28 14:55:20 -07001230 unsigned int local_alloc_megs;
Sunil Mushrand5500712007-09-06 13:34:16 -07001231
1232 if (opts & OCFS2_MOUNT_HB_LOCAL)
1233 seq_printf(s, ",_netdev,heartbeat=local");
1234 else
1235 seq_printf(s, ",heartbeat=none");
1236
1237 if (opts & OCFS2_MOUNT_NOINTR)
1238 seq_printf(s, ",nointr");
1239
1240 if (opts & OCFS2_MOUNT_DATA_WRITEBACK)
1241 seq_printf(s, ",data=writeback");
1242 else
1243 seq_printf(s, ",data=ordered");
1244
1245 if (opts & OCFS2_MOUNT_BARRIER)
1246 seq_printf(s, ",barrier=1");
1247
1248 if (opts & OCFS2_MOUNT_ERRORS_PANIC)
1249 seq_printf(s, ",errors=panic");
1250 else
1251 seq_printf(s, ",errors=remount-ro");
1252
1253 if (osb->preferred_slot != OCFS2_INVALID_SLOT)
1254 seq_printf(s, ",preferred_slot=%d", osb->preferred_slot);
1255
1256 if (osb->s_atime_quantum != OCFS2_DEFAULT_ATIME_QUANTUM)
1257 seq_printf(s, ",atime_quantum=%u", osb->s_atime_quantum);
1258
Mark Fashehd147b3d2007-11-07 14:40:36 -08001259 if (osb->osb_commit_interval)
1260 seq_printf(s, ",commit=%u",
1261 (unsigned) (osb->osb_commit_interval / HZ));
1262
Mark Fashehebcee4b2008-07-28 14:55:20 -07001263 local_alloc_megs = osb->local_alloc_bits >> (20 - osb->s_clustersize_bits);
1264 if (local_alloc_megs != OCFS2_DEFAULT_LOCAL_ALLOC_SIZE)
1265 seq_printf(s, ",localalloc=%d", local_alloc_megs);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001266
Mark Fasheh53fc6222007-12-20 16:49:04 -08001267 if (opts & OCFS2_MOUNT_LOCALFLOCKS)
1268 seq_printf(s, ",localflocks,");
1269
Joel Beckerb61817e2008-02-01 15:08:23 -08001270 if (osb->osb_cluster_stack[0])
1271 seq_printf(s, ",cluster_stack=%.*s", OCFS2_STACK_LABEL_LEN,
1272 osb->osb_cluster_stack);
Jan Kara19ece542008-08-21 20:13:17 +02001273 if (opts & OCFS2_MOUNT_USRQUOTA)
1274 seq_printf(s, ",usrquota");
1275 if (opts & OCFS2_MOUNT_GRPQUOTA)
1276 seq_printf(s, ",grpquota");
Joel Beckerb61817e2008-02-01 15:08:23 -08001277
Sunil Mushranb0f73cf2008-09-05 11:29:14 -07001278 if (opts & OCFS2_MOUNT_NOUSERXATTR)
1279 seq_printf(s, ",nouser_xattr");
1280 else
1281 seq_printf(s, ",user_xattr");
1282
Joel Becker12462f12008-09-03 20:03:40 -07001283 if (opts & OCFS2_MOUNT_INODE64)
1284 seq_printf(s, ",inode64");
1285
Tiger Yanga68979b2008-11-14 11:17:52 +08001286#ifdef CONFIG_OCFS2_FS_POSIX_ACL
1287 if (opts & OCFS2_MOUNT_POSIX_ACL)
1288 seq_printf(s, ",acl");
1289 else
1290 seq_printf(s, ",noacl");
1291#endif
1292
Sunil Mushrand5500712007-09-06 13:34:16 -07001293 return 0;
1294}
1295
Mark Fashehccd979b2005-12-15 14:31:24 -08001296static int __init ocfs2_init(void)
1297{
1298 int status;
1299
1300 mlog_entry_void();
1301
1302 ocfs2_print_version();
1303
Mark Fashehccd979b2005-12-15 14:31:24 -08001304 status = init_ocfs2_uptodate_cache();
1305 if (status < 0) {
1306 mlog_errno(status);
1307 goto leave;
1308 }
1309
1310 status = ocfs2_initialize_mem_caches();
1311 if (status < 0) {
1312 mlog_errno(status);
1313 goto leave;
1314 }
1315
1316 ocfs2_wq = create_singlethread_workqueue("ocfs2_wq");
1317 if (!ocfs2_wq) {
1318 status = -ENOMEM;
1319 goto leave;
1320 }
1321
Mark Fashehccd979b2005-12-15 14:31:24 -08001322 ocfs2_debugfs_root = debugfs_create_dir("ocfs2", NULL);
1323 if (!ocfs2_debugfs_root) {
1324 status = -EFAULT;
1325 mlog(ML_ERROR, "Unable to create ocfs2 debugfs root.\n");
1326 }
1327
Mark Fasheh171bf932008-10-20 15:36:47 +02001328 status = ocfs2_quota_setup();
1329 if (status)
1330 goto leave;
1331
Joel Becker63e0c482008-01-30 16:58:36 -08001332 ocfs2_set_locking_protocol();
1333
Jan Kara9e33d692008-08-25 19:56:50 +02001334 status = register_quota_format(&ocfs2_quota_format);
Mark Fashehccd979b2005-12-15 14:31:24 -08001335leave:
1336 if (status < 0) {
Mark Fasheh171bf932008-10-20 15:36:47 +02001337 ocfs2_quota_shutdown();
Mark Fashehccd979b2005-12-15 14:31:24 -08001338 ocfs2_free_mem_caches();
1339 exit_ocfs2_uptodate_cache();
Mark Fashehccd979b2005-12-15 14:31:24 -08001340 }
1341
1342 mlog_exit(status);
1343
1344 if (status >= 0) {
1345 return register_filesystem(&ocfs2_fs_type);
1346 } else
1347 return -1;
1348}
1349
1350static void __exit ocfs2_exit(void)
1351{
1352 mlog_entry_void();
1353
Mark Fasheh171bf932008-10-20 15:36:47 +02001354 ocfs2_quota_shutdown();
1355
Mark Fashehccd979b2005-12-15 14:31:24 -08001356 if (ocfs2_wq) {
1357 flush_workqueue(ocfs2_wq);
1358 destroy_workqueue(ocfs2_wq);
1359 }
1360
Jan Kara9e33d692008-08-25 19:56:50 +02001361 unregister_quota_format(&ocfs2_quota_format);
1362
Mark Fashehccd979b2005-12-15 14:31:24 -08001363 debugfs_remove(ocfs2_debugfs_root);
1364
1365 ocfs2_free_mem_caches();
1366
1367 unregister_filesystem(&ocfs2_fs_type);
1368
Mark Fashehccd979b2005-12-15 14:31:24 -08001369 exit_ocfs2_uptodate_cache();
1370
1371 mlog_exit_void();
1372}
1373
1374static void ocfs2_put_super(struct super_block *sb)
1375{
1376 mlog_entry("(0x%p)\n", sb);
1377
1378 ocfs2_sync_blockdev(sb);
1379 ocfs2_dismount_volume(sb, 0);
1380
1381 mlog_exit_void();
1382}
1383
David Howells726c3342006-06-23 02:02:58 -07001384static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
Mark Fashehccd979b2005-12-15 14:31:24 -08001385{
1386 struct ocfs2_super *osb;
1387 u32 numbits, freebits;
1388 int status;
1389 struct ocfs2_dinode *bm_lock;
1390 struct buffer_head *bh = NULL;
1391 struct inode *inode = NULL;
1392
David Howells726c3342006-06-23 02:02:58 -07001393 mlog_entry("(%p, %p)\n", dentry->d_sb, buf);
Mark Fashehccd979b2005-12-15 14:31:24 -08001394
David Howells726c3342006-06-23 02:02:58 -07001395 osb = OCFS2_SB(dentry->d_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001396
1397 inode = ocfs2_get_system_file_inode(osb,
1398 GLOBAL_BITMAP_SYSTEM_INODE,
1399 OCFS2_INVALID_SLOT);
1400 if (!inode) {
1401 mlog(ML_ERROR, "failed to get bitmap inode\n");
1402 status = -EIO;
1403 goto bail;
1404 }
1405
Mark Fashehe63aecb62007-10-18 15:30:42 -07001406 status = ocfs2_inode_lock(inode, &bh, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001407 if (status < 0) {
1408 mlog_errno(status);
1409 goto bail;
1410 }
1411
1412 bm_lock = (struct ocfs2_dinode *) bh->b_data;
1413
1414 numbits = le32_to_cpu(bm_lock->id1.bitmap1.i_total);
1415 freebits = numbits - le32_to_cpu(bm_lock->id1.bitmap1.i_used);
1416
1417 buf->f_type = OCFS2_SUPER_MAGIC;
David Howells726c3342006-06-23 02:02:58 -07001418 buf->f_bsize = dentry->d_sb->s_blocksize;
Mark Fashehccd979b2005-12-15 14:31:24 -08001419 buf->f_namelen = OCFS2_MAX_FILENAME_LEN;
1420 buf->f_blocks = ((sector_t) numbits) *
1421 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1422 buf->f_bfree = ((sector_t) freebits) *
1423 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1424 buf->f_bavail = buf->f_bfree;
1425 buf->f_files = numbits;
1426 buf->f_ffree = freebits;
1427
1428 brelse(bh);
1429
Mark Fashehe63aecb62007-10-18 15:30:42 -07001430 ocfs2_inode_unlock(inode, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001431 status = 0;
1432bail:
1433 if (inode)
1434 iput(inode);
1435
1436 mlog_exit(status);
1437
1438 return status;
1439}
1440
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001441static void ocfs2_inode_init_once(void *data)
Mark Fashehccd979b2005-12-15 14:31:24 -08001442{
1443 struct ocfs2_inode_info *oi = data;
1444
Christoph Lametera35afb82007-05-16 22:10:57 -07001445 oi->ip_flags = 0;
1446 oi->ip_open_count = 0;
1447 spin_lock_init(&oi->ip_lock);
1448 ocfs2_extent_map_init(&oi->vfs_inode);
1449 INIT_LIST_HEAD(&oi->ip_io_markers);
1450 oi->ip_created_trans = 0;
1451 oi->ip_last_trans = 0;
1452 oi->ip_dir_start_lookup = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001453
Christoph Lametera35afb82007-05-16 22:10:57 -07001454 init_rwsem(&oi->ip_alloc_sem);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001455 init_rwsem(&oi->ip_xattr_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -07001456 mutex_init(&oi->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001457
Christoph Lametera35afb82007-05-16 22:10:57 -07001458 oi->ip_blkno = 0ULL;
1459 oi->ip_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001460
Christoph Lametera35afb82007-05-16 22:10:57 -07001461 ocfs2_lock_res_init_once(&oi->ip_rw_lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001462 ocfs2_lock_res_init_once(&oi->ip_inode_lockres);
Christoph Lametera35afb82007-05-16 22:10:57 -07001463 ocfs2_lock_res_init_once(&oi->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001464
Christoph Lametera35afb82007-05-16 22:10:57 -07001465 ocfs2_metadata_cache_init(&oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001466
Christoph Lametera35afb82007-05-16 22:10:57 -07001467 inode_init_once(&oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001468}
1469
1470static int ocfs2_initialize_mem_caches(void)
1471{
1472 ocfs2_inode_cachep = kmem_cache_create("ocfs2_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001473 sizeof(struct ocfs2_inode_info),
1474 0,
1475 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1476 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001477 ocfs2_inode_init_once);
Jan Kara9e33d692008-08-25 19:56:50 +02001478 ocfs2_dquot_cachep = kmem_cache_create("ocfs2_dquot_cache",
1479 sizeof(struct ocfs2_dquot),
1480 0,
1481 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1482 SLAB_MEM_SPREAD),
1483 NULL);
1484 ocfs2_qf_chunk_cachep = kmem_cache_create("ocfs2_qf_chunk_cache",
1485 sizeof(struct ocfs2_quota_chunk),
1486 0,
1487 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
1488 NULL);
1489 if (!ocfs2_inode_cachep || !ocfs2_dquot_cachep ||
1490 !ocfs2_qf_chunk_cachep) {
1491 if (ocfs2_inode_cachep)
1492 kmem_cache_destroy(ocfs2_inode_cachep);
1493 if (ocfs2_dquot_cachep)
1494 kmem_cache_destroy(ocfs2_dquot_cachep);
1495 if (ocfs2_qf_chunk_cachep)
1496 kmem_cache_destroy(ocfs2_qf_chunk_cachep);
Mark Fashehccd979b2005-12-15 14:31:24 -08001497 return -ENOMEM;
Jan Kara9e33d692008-08-25 19:56:50 +02001498 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001499
Mark Fashehccd979b2005-12-15 14:31:24 -08001500 return 0;
1501}
1502
1503static void ocfs2_free_mem_caches(void)
1504{
1505 if (ocfs2_inode_cachep)
1506 kmem_cache_destroy(ocfs2_inode_cachep);
Mark Fashehccd979b2005-12-15 14:31:24 -08001507 ocfs2_inode_cachep = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +02001508
1509 if (ocfs2_dquot_cachep)
1510 kmem_cache_destroy(ocfs2_dquot_cachep);
1511 ocfs2_dquot_cachep = NULL;
1512
1513 if (ocfs2_qf_chunk_cachep)
1514 kmem_cache_destroy(ocfs2_qf_chunk_cachep);
1515 ocfs2_qf_chunk_cachep = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001516}
1517
1518static int ocfs2_get_sector(struct super_block *sb,
1519 struct buffer_head **bh,
1520 int block,
1521 int sect_size)
1522{
1523 if (!sb_set_blocksize(sb, sect_size)) {
1524 mlog(ML_ERROR, "unable to set blocksize\n");
1525 return -EIO;
1526 }
1527
1528 *bh = sb_getblk(sb, block);
1529 if (!*bh) {
1530 mlog_errno(-EIO);
1531 return -EIO;
1532 }
1533 lock_buffer(*bh);
1534 if (!buffer_dirty(*bh))
1535 clear_buffer_uptodate(*bh);
1536 unlock_buffer(*bh);
1537 ll_rw_block(READ, 1, bh);
1538 wait_on_buffer(*bh);
1539 return 0;
1540}
1541
Mark Fashehccd979b2005-12-15 14:31:24 -08001542static int ocfs2_mount_volume(struct super_block *sb)
1543{
1544 int status = 0;
1545 int unlock_super = 0;
1546 struct ocfs2_super *osb = OCFS2_SB(sb);
1547
1548 mlog_entry_void();
1549
1550 if (ocfs2_is_hard_readonly(osb))
1551 goto leave;
1552
Mark Fashehccd979b2005-12-15 14:31:24 -08001553 status = ocfs2_dlm_init(osb);
1554 if (status < 0) {
1555 mlog_errno(status);
1556 goto leave;
1557 }
1558
Mark Fashehccd979b2005-12-15 14:31:24 -08001559 status = ocfs2_super_lock(osb, 1);
1560 if (status < 0) {
1561 mlog_errno(status);
1562 goto leave;
1563 }
1564 unlock_super = 1;
1565
1566 /* This will load up the node map and add ourselves to it. */
1567 status = ocfs2_find_slot(osb);
1568 if (status < 0) {
1569 mlog_errno(status);
1570 goto leave;
1571 }
1572
Mark Fashehccd979b2005-12-15 14:31:24 -08001573 /* load all node-local system inodes */
1574 status = ocfs2_init_local_system_inodes(osb);
1575 if (status < 0) {
1576 mlog_errno(status);
1577 goto leave;
1578 }
1579
1580 status = ocfs2_check_volume(osb);
1581 if (status < 0) {
1582 mlog_errno(status);
1583 goto leave;
1584 }
1585
1586 status = ocfs2_truncate_log_init(osb);
1587 if (status < 0) {
1588 mlog_errno(status);
1589 goto leave;
1590 }
1591
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001592 if (ocfs2_mount_local(osb))
1593 goto leave;
1594
Mark Fashehccd979b2005-12-15 14:31:24 -08001595leave:
1596 if (unlock_super)
1597 ocfs2_super_unlock(osb, 1);
1598
1599 mlog_exit(status);
1600 return status;
1601}
1602
Mark Fashehccd979b2005-12-15 14:31:24 -08001603static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
1604{
Joel Becker286eaa92008-02-01 15:03:57 -08001605 int tmp, hangup_needed = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001606 struct ocfs2_super *osb = NULL;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001607 char nodestr[8];
Mark Fashehccd979b2005-12-15 14:31:24 -08001608
1609 mlog_entry("(0x%p)\n", sb);
1610
1611 BUG_ON(!sb);
1612 osb = OCFS2_SB(sb);
1613 BUG_ON(!osb);
1614
Jan Kara19ece542008-08-21 20:13:17 +02001615 ocfs2_disable_quotas(osb);
1616
Mark Fashehccd979b2005-12-15 14:31:24 -08001617 ocfs2_shutdown_local_alloc(osb);
1618
1619 ocfs2_truncate_log_shutdown(osb);
1620
Joel Becker553abd02008-02-01 12:03:57 -08001621 /* This will disable recovery and flush any recovery work. */
1622 ocfs2_recovery_exit(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001623
1624 ocfs2_journal_shutdown(osb);
1625
1626 ocfs2_sync_blockdev(sb);
1627
Joel Becker4670c462008-02-01 14:39:35 -08001628 /* No cluster connection means we've failed during mount, so skip
1629 * all the steps which depended on that to complete. */
1630 if (osb->cconn) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001631 tmp = ocfs2_super_lock(osb, 1);
1632 if (tmp < 0) {
1633 mlog_errno(tmp);
1634 return;
1635 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001636 }
1637
Mark Fasheh19b613d2007-10-04 14:47:09 -07001638 if (osb->slot_num != OCFS2_INVALID_SLOT)
1639 ocfs2_put_slot(osb);
1640
Joel Becker4670c462008-02-01 14:39:35 -08001641 if (osb->cconn)
Mark Fasheh19b613d2007-10-04 14:47:09 -07001642 ocfs2_super_unlock(osb, 1);
1643
Mark Fashehccd979b2005-12-15 14:31:24 -08001644 ocfs2_release_system_inodes(osb);
1645
Joel Becker6953b4c2008-01-29 16:59:56 -08001646 /*
Joel Becker6953b4c2008-01-29 16:59:56 -08001647 * If we're dismounting due to mount error, mount.ocfs2 will clean
1648 * up heartbeat. If we're a local mount, there is no heartbeat.
1649 * If we failed before we got a uuid_str yet, we can't stop
1650 * heartbeat. Otherwise, do it.
1651 */
1652 if (!mnt_err && !ocfs2_mount_local(osb) && osb->uuid_str)
Joel Becker286eaa92008-02-01 15:03:57 -08001653 hangup_needed = 1;
1654
1655 if (osb->cconn)
1656 ocfs2_dlm_shutdown(osb, hangup_needed);
1657
1658 debugfs_remove(osb->osb_debug_root);
1659
1660 if (hangup_needed)
Joel Becker6953b4c2008-01-29 16:59:56 -08001661 ocfs2_cluster_hangup(osb->uuid_str, strlen(osb->uuid_str));
Mark Fashehccd979b2005-12-15 14:31:24 -08001662
1663 atomic_set(&osb->vol_state, VOLUME_DISMOUNTED);
1664
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001665 if (ocfs2_mount_local(osb))
1666 snprintf(nodestr, sizeof(nodestr), "local");
1667 else
Joel Becker19fdb622008-01-30 15:38:24 -08001668 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001669
1670 printk(KERN_INFO "ocfs2: Unmounting device (%s) on (node %s)\n",
1671 osb->dev_str, nodestr);
Mark Fashehccd979b2005-12-15 14:31:24 -08001672
1673 ocfs2_delete_osb(osb);
1674 kfree(osb);
1675 sb->s_dev = 0;
1676 sb->s_fs_info = NULL;
1677}
1678
1679static int ocfs2_setup_osb_uuid(struct ocfs2_super *osb, const unsigned char *uuid,
1680 unsigned uuid_bytes)
1681{
1682 int i, ret;
1683 char *ptr;
1684
1685 BUG_ON(uuid_bytes != OCFS2_VOL_UUID_LEN);
1686
Robert P. J. Daycd861282006-12-13 00:34:52 -08001687 osb->uuid_str = kzalloc(OCFS2_VOL_UUID_LEN * 2 + 1, GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001688 if (osb->uuid_str == NULL)
1689 return -ENOMEM;
1690
Mark Fashehccd979b2005-12-15 14:31:24 -08001691 for (i = 0, ptr = osb->uuid_str; i < OCFS2_VOL_UUID_LEN; i++) {
1692 /* print with null */
1693 ret = snprintf(ptr, 3, "%02X", uuid[i]);
1694 if (ret != 2) /* drop super cleans up */
1695 return -EINVAL;
1696 /* then only advance past the last char */
1697 ptr += 2;
1698 }
1699
1700 return 0;
1701}
1702
1703static int ocfs2_initialize_super(struct super_block *sb,
1704 struct buffer_head *bh,
1705 int sector_size)
1706{
Denis Chengbddb8eb32007-09-27 02:10:04 +08001707 int status;
Mark Fasheh5a254032007-07-20 12:56:16 -07001708 int i, cbits, bbits;
1709 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001710 struct inode *inode = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001711 struct ocfs2_journal *journal;
1712 __le32 uuid_net_key;
1713 struct ocfs2_super *osb;
1714
1715 mlog_entry_void();
1716
Robert P. J. Daycd861282006-12-13 00:34:52 -08001717 osb = kzalloc(sizeof(struct ocfs2_super), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001718 if (!osb) {
1719 status = -ENOMEM;
1720 mlog_errno(status);
1721 goto bail;
1722 }
1723
1724 sb->s_fs_info = osb;
1725 sb->s_op = &ocfs2_sops;
1726 sb->s_export_op = &ocfs2_export_ops;
Jan Kara19ece542008-08-21 20:13:17 +02001727 sb->s_qcop = &ocfs2_quotactl_ops;
1728 sb->dq_op = &ocfs2_quota_operations;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001729 sb->s_xattr = ocfs2_xattr_handlers;
Mark Fashehe0dceaf2007-08-09 16:52:30 -07001730 sb->s_time_gran = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001731 sb->s_flags |= MS_NOATIME;
1732 /* this is needed to support O_LARGEFILE */
Mark Fasheh5a254032007-07-20 12:56:16 -07001733 cbits = le32_to_cpu(di->id2.i_super.s_clustersize_bits);
1734 bbits = le32_to_cpu(di->id2.i_super.s_blocksize_bits);
1735 sb->s_maxbytes = ocfs2_max_file_offset(bbits, cbits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001736
1737 osb->sb = sb;
1738 /* Save off for ocfs2_rw_direct */
1739 osb->s_sectsize_bits = blksize_bits(sector_size);
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01001740 BUG_ON(!osb->s_sectsize_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001741
Mark Fasheh34d024f2007-09-24 15:56:19 -07001742 spin_lock_init(&osb->dc_task_lock);
1743 init_waitqueue_head(&osb->dc_event);
1744 osb->dc_work_sequence = 0;
1745 osb->dc_wake_sequence = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001746 INIT_LIST_HEAD(&osb->blocked_lock_list);
1747 osb->blocked_lock_count = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001748 spin_lock_init(&osb->osb_lock);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001749 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001750
1751 atomic_set(&osb->alloc_stats.moves, 0);
1752 atomic_set(&osb->alloc_stats.local_data, 0);
1753 atomic_set(&osb->alloc_stats.bitmap_data, 0);
1754 atomic_set(&osb->alloc_stats.bg_allocs, 0);
1755 atomic_set(&osb->alloc_stats.bg_extends, 0);
1756
1757 ocfs2_init_node_maps(osb);
1758
1759 snprintf(osb->dev_str, sizeof(osb->dev_str), "%u,%u",
1760 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1761
Joel Becker553abd02008-02-01 12:03:57 -08001762 status = ocfs2_recovery_init(osb);
1763 if (status) {
1764 mlog(ML_ERROR, "Unable to initialize recovery state\n");
1765 mlog_errno(status);
1766 goto bail;
1767 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001768
1769 init_waitqueue_head(&osb->checkpoint_event);
1770 atomic_set(&osb->needs_checkpoint, 0);
1771
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001772 osb->s_atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
1773
Mark Fashehccd979b2005-12-15 14:31:24 -08001774 osb->slot_num = OCFS2_INVALID_SLOT;
1775
Tiger Yang8154da32008-08-18 17:11:46 +08001776 osb->s_xattr_inline_size = le16_to_cpu(
1777 di->id2.i_super.s_xattr_inline_size);
Tiger Yangfdd77702008-08-18 17:08:55 +08001778
Mark Fashehccd979b2005-12-15 14:31:24 -08001779 osb->local_alloc_state = OCFS2_LA_UNUSED;
1780 osb->local_alloc_bh = NULL;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001781 INIT_DELAYED_WORK(&osb->la_enable_wq, ocfs2_la_enable_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08001782
Mark Fashehccd979b2005-12-15 14:31:24 -08001783 init_waitqueue_head(&osb->osb_mount_event);
1784
1785 osb->vol_label = kmalloc(OCFS2_MAX_VOL_LABEL_LEN, GFP_KERNEL);
1786 if (!osb->vol_label) {
1787 mlog(ML_ERROR, "unable to alloc vol label\n");
1788 status = -ENOMEM;
1789 goto bail;
1790 }
1791
Mark Fashehccd979b2005-12-15 14:31:24 -08001792 osb->max_slots = le16_to_cpu(di->id2.i_super.s_max_slots);
1793 if (osb->max_slots > OCFS2_MAX_SLOTS || osb->max_slots == 0) {
1794 mlog(ML_ERROR, "Invalid number of node slots (%u)\n",
1795 osb->max_slots);
1796 status = -EINVAL;
1797 goto bail;
1798 }
Sunil Mushran781ee3e2006-04-27 16:41:31 -07001799 mlog(0, "max_slots for this device: %u\n", osb->max_slots);
Mark Fashehccd979b2005-12-15 14:31:24 -08001800
Sunil Mushran539d8262008-07-14 17:31:10 -07001801 osb->slot_recovery_generations =
1802 kcalloc(osb->max_slots, sizeof(*osb->slot_recovery_generations),
1803 GFP_KERNEL);
1804 if (!osb->slot_recovery_generations) {
1805 status = -ENOMEM;
1806 mlog_errno(status);
1807 goto bail;
1808 }
1809
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001810 init_waitqueue_head(&osb->osb_wipe_event);
1811 osb->osb_orphan_wipes = kcalloc(osb->max_slots,
1812 sizeof(*osb->osb_orphan_wipes),
1813 GFP_KERNEL);
1814 if (!osb->osb_orphan_wipes) {
1815 status = -ENOMEM;
1816 mlog_errno(status);
1817 goto bail;
1818 }
1819
Mark Fashehccd979b2005-12-15 14:31:24 -08001820 osb->s_feature_compat =
1821 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_compat);
1822 osb->s_feature_ro_compat =
1823 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_ro_compat);
1824 osb->s_feature_incompat =
1825 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_incompat);
1826
1827 if ((i = OCFS2_HAS_INCOMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_INCOMPAT_SUPP))) {
1828 mlog(ML_ERROR, "couldn't mount because of unsupported "
1829 "optional features (%x).\n", i);
1830 status = -EINVAL;
1831 goto bail;
1832 }
1833 if (!(osb->sb->s_flags & MS_RDONLY) &&
1834 (i = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP))) {
1835 mlog(ML_ERROR, "couldn't mount RDWR because of "
1836 "unsupported optional features (%x).\n", i);
1837 status = -EINVAL;
1838 goto bail;
1839 }
1840
Joel Beckerb61817e2008-02-01 15:08:23 -08001841 if (ocfs2_userspace_stack(osb)) {
1842 memcpy(osb->osb_cluster_stack,
1843 OCFS2_RAW_SB(di)->s_cluster_info.ci_stack,
1844 OCFS2_STACK_LABEL_LEN);
1845 osb->osb_cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
1846 if (strlen(osb->osb_cluster_stack) != OCFS2_STACK_LABEL_LEN) {
1847 mlog(ML_ERROR,
1848 "couldn't mount because of an invalid "
1849 "cluster stack label (%s) \n",
1850 osb->osb_cluster_stack);
1851 status = -EINVAL;
1852 goto bail;
1853 }
1854 } else {
1855 /* The empty string is identical with classic tools that
1856 * don't know about s_cluster_info. */
1857 osb->osb_cluster_stack[0] = '\0';
1858 }
1859
Mark Fashehccd979b2005-12-15 14:31:24 -08001860 get_random_bytes(&osb->s_next_generation, sizeof(u32));
1861
1862 /* FIXME
1863 * This should be done in ocfs2_journal_init(), but unknown
1864 * ordering issues will cause the filesystem to crash.
1865 * If anyone wants to figure out what part of the code
1866 * refers to osb->journal before ocfs2_journal_init() is run,
1867 * be my guest.
1868 */
1869 /* initialize our journal structure */
1870
Robert P. J. Daycd861282006-12-13 00:34:52 -08001871 journal = kzalloc(sizeof(struct ocfs2_journal), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001872 if (!journal) {
1873 mlog(ML_ERROR, "unable to alloc journal\n");
1874 status = -ENOMEM;
1875 goto bail;
1876 }
1877 osb->journal = journal;
1878 journal->j_osb = osb;
1879
1880 atomic_set(&journal->j_num_trans, 0);
1881 init_rwsem(&journal->j_trans_barrier);
1882 init_waitqueue_head(&journal->j_checkpointed);
1883 spin_lock_init(&journal->j_lock);
1884 journal->j_trans_id = (unsigned long) 1;
1885 INIT_LIST_HEAD(&journal->j_la_cleanups);
David Howellsc4028952006-11-22 14:57:56 +00001886 INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
Mark Fashehccd979b2005-12-15 14:31:24 -08001887 journal->j_state = OCFS2_JOURNAL_FREE;
1888
1889 /* get some pseudo constants for clustersize bits */
1890 osb->s_clustersize_bits =
1891 le32_to_cpu(di->id2.i_super.s_clustersize_bits);
1892 osb->s_clustersize = 1 << osb->s_clustersize_bits;
1893 mlog(0, "clusterbits=%d\n", osb->s_clustersize_bits);
1894
1895 if (osb->s_clustersize < OCFS2_MIN_CLUSTERSIZE ||
1896 osb->s_clustersize > OCFS2_MAX_CLUSTERSIZE) {
1897 mlog(ML_ERROR, "Volume has invalid cluster size (%d)\n",
1898 osb->s_clustersize);
1899 status = -EINVAL;
1900 goto bail;
1901 }
1902
1903 if (ocfs2_clusters_to_blocks(osb->sb, le32_to_cpu(di->i_clusters) - 1)
1904 > (u32)~0UL) {
1905 mlog(ML_ERROR, "Volume might try to write to blocks beyond "
1906 "what jbd can address in 32 bits.\n");
1907 status = -EINVAL;
1908 goto bail;
1909 }
1910
1911 if (ocfs2_setup_osb_uuid(osb, di->id2.i_super.s_uuid,
1912 sizeof(di->id2.i_super.s_uuid))) {
1913 mlog(ML_ERROR, "Out of memory trying to setup our uuid.\n");
1914 status = -ENOMEM;
1915 goto bail;
1916 }
1917
Mark Fasheh78427042006-05-04 12:03:26 -07001918 memcpy(&uuid_net_key, di->id2.i_super.s_uuid, sizeof(uuid_net_key));
Mark Fashehccd979b2005-12-15 14:31:24 -08001919
1920 strncpy(osb->vol_label, di->id2.i_super.s_label, 63);
1921 osb->vol_label[63] = '\0';
1922 osb->root_blkno = le64_to_cpu(di->id2.i_super.s_root_blkno);
1923 osb->system_dir_blkno = le64_to_cpu(di->id2.i_super.s_system_dir_blkno);
1924 osb->first_cluster_group_blkno =
1925 le64_to_cpu(di->id2.i_super.s_first_cluster_group);
1926 osb->fs_generation = le32_to_cpu(di->i_fs_generation);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001927 osb->uuid_hash = le32_to_cpu(di->id2.i_super.s_uuid_hash);
Mark Fashehccd979b2005-12-15 14:31:24 -08001928 mlog(0, "vol_label: %s\n", osb->vol_label);
1929 mlog(0, "uuid: %s\n", osb->uuid_str);
Mark Fashehb06970532006-03-03 10:24:33 -08001930 mlog(0, "root_blkno=%llu, system_dir_blkno=%llu\n",
1931 (unsigned long long)osb->root_blkno,
1932 (unsigned long long)osb->system_dir_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001933
1934 osb->osb_dlm_debug = ocfs2_new_dlm_debug();
1935 if (!osb->osb_dlm_debug) {
1936 status = -ENOMEM;
1937 mlog_errno(status);
1938 goto bail;
1939 }
1940
1941 atomic_set(&osb->vol_state, VOLUME_INIT);
1942
1943 /* load root, system_dir, and all global system inodes */
1944 status = ocfs2_init_global_system_inodes(osb);
1945 if (status < 0) {
1946 mlog_errno(status);
1947 goto bail;
1948 }
1949
1950 /*
1951 * global bitmap
1952 */
1953 inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
1954 OCFS2_INVALID_SLOT);
1955 if (!inode) {
1956 status = -EINVAL;
1957 mlog_errno(status);
1958 goto bail;
1959 }
1960
1961 osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001962 iput(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001963
Tao Mae9d578a2007-12-18 15:46:10 +08001964 osb->bitmap_cpg = ocfs2_group_bitmap_size(sb) * 8;
Mark Fashehccd979b2005-12-15 14:31:24 -08001965
1966 status = ocfs2_init_slot_info(osb);
1967 if (status < 0) {
1968 mlog_errno(status);
1969 goto bail;
1970 }
1971
Mark Fashehccd979b2005-12-15 14:31:24 -08001972bail:
1973 mlog_exit(status);
1974 return status;
1975}
1976
1977/*
1978 * will return: -EAGAIN if it is ok to keep searching for superblocks
1979 * -EINVAL if there is a bad superblock
1980 * 0 on success
1981 */
1982static int ocfs2_verify_volume(struct ocfs2_dinode *di,
1983 struct buffer_head *bh,
1984 u32 blksz)
1985{
1986 int status = -EAGAIN;
1987
1988 mlog_entry_void();
1989
1990 if (memcmp(di->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
1991 strlen(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) {
1992 status = -EINVAL;
1993 if ((1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits)) != blksz) {
1994 mlog(ML_ERROR, "found superblock with incorrect block "
1995 "size: found %u, should be %u\n",
1996 1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits),
1997 blksz);
1998 } else if (le16_to_cpu(di->id2.i_super.s_major_rev_level) !=
1999 OCFS2_MAJOR_REV_LEVEL ||
2000 le16_to_cpu(di->id2.i_super.s_minor_rev_level) !=
2001 OCFS2_MINOR_REV_LEVEL) {
2002 mlog(ML_ERROR, "found superblock with bad version: "
2003 "found %u.%u, should be %u.%u\n",
2004 le16_to_cpu(di->id2.i_super.s_major_rev_level),
2005 le16_to_cpu(di->id2.i_super.s_minor_rev_level),
2006 OCFS2_MAJOR_REV_LEVEL,
2007 OCFS2_MINOR_REV_LEVEL);
2008 } else if (bh->b_blocknr != le64_to_cpu(di->i_blkno)) {
2009 mlog(ML_ERROR, "bad block number on superblock: "
Mark Fashehb06970532006-03-03 10:24:33 -08002010 "found %llu, should be %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07002011 (unsigned long long)le64_to_cpu(di->i_blkno),
Mark Fashehb06970532006-03-03 10:24:33 -08002012 (unsigned long long)bh->b_blocknr);
Mark Fashehccd979b2005-12-15 14:31:24 -08002013 } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
2014 le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
2015 mlog(ML_ERROR, "bad cluster size found: %u\n",
2016 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
2017 } else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
2018 mlog(ML_ERROR, "bad root_blkno: 0\n");
2019 } else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
2020 mlog(ML_ERROR, "bad system_dir_blkno: 0\n");
2021 } else if (le16_to_cpu(di->id2.i_super.s_max_slots) > OCFS2_MAX_SLOTS) {
2022 mlog(ML_ERROR,
2023 "Superblock slots found greater than file system "
2024 "maximum: found %u, max %u\n",
2025 le16_to_cpu(di->id2.i_super.s_max_slots),
2026 OCFS2_MAX_SLOTS);
2027 } else {
2028 /* found it! */
2029 status = 0;
2030 }
2031 }
2032
2033 mlog_exit(status);
2034 return status;
2035}
2036
2037static int ocfs2_check_volume(struct ocfs2_super *osb)
2038{
Denis Chengbddb8eb32007-09-27 02:10:04 +08002039 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08002040 int dirty;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002041 int local;
Mark Fashehccd979b2005-12-15 14:31:24 -08002042 struct ocfs2_dinode *local_alloc = NULL; /* only used if we
2043 * recover
2044 * ourselves. */
2045
2046 mlog_entry_void();
2047
2048 /* Init our journal object. */
2049 status = ocfs2_journal_init(osb->journal, &dirty);
2050 if (status < 0) {
2051 mlog(ML_ERROR, "Could not initialize journal!\n");
2052 goto finally;
2053 }
2054
2055 /* If the journal was unmounted cleanly then we don't want to
2056 * recover anything. Otherwise, journal_load will do that
2057 * dirty work for us :) */
2058 if (!dirty) {
2059 status = ocfs2_journal_wipe(osb->journal, 0);
2060 if (status < 0) {
2061 mlog_errno(status);
2062 goto finally;
2063 }
2064 } else {
2065 mlog(ML_NOTICE, "File system was not unmounted cleanly, "
2066 "recovering volume.\n");
2067 }
2068
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002069 local = ocfs2_mount_local(osb);
2070
Mark Fashehccd979b2005-12-15 14:31:24 -08002071 /* will play back anything left in the journal. */
Sunil Mushran539d8262008-07-14 17:31:10 -07002072 status = ocfs2_journal_load(osb->journal, local, dirty);
Wengang Wang01af4822008-06-10 14:24:48 +08002073 if (status < 0) {
2074 mlog(ML_ERROR, "ocfs2 journal load failed! %d\n", status);
2075 goto finally;
2076 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002077
2078 if (dirty) {
2079 /* recover my local alloc if we didn't unmount cleanly. */
2080 status = ocfs2_begin_local_alloc_recovery(osb,
2081 osb->slot_num,
2082 &local_alloc);
2083 if (status < 0) {
2084 mlog_errno(status);
2085 goto finally;
2086 }
2087 /* we complete the recovery process after we've marked
2088 * ourselves as mounted. */
2089 }
2090
2091 mlog(0, "Journal loaded.\n");
2092
2093 status = ocfs2_load_local_alloc(osb);
2094 if (status < 0) {
2095 mlog_errno(status);
2096 goto finally;
2097 }
2098
2099 if (dirty) {
2100 /* Recovery will be completed after we've mounted the
2101 * rest of the volume. */
2102 osb->dirty = 1;
2103 osb->local_alloc_copy = local_alloc;
2104 local_alloc = NULL;
2105 }
2106
2107 /* go through each journal, trylock it and if you get the
2108 * lock, and it's marked as dirty, set the bit in the recover
2109 * map and launch a recovery thread for it. */
2110 status = ocfs2_mark_dead_nodes(osb);
2111 if (status < 0)
2112 mlog_errno(status);
2113
2114finally:
2115 if (local_alloc)
2116 kfree(local_alloc);
2117
2118 mlog_exit(status);
2119 return status;
2120}
2121
2122/*
2123 * The routine gets called from dismount or close whenever a dismount on
2124 * volume is requested and the osb open count becomes 1.
2125 * It will remove the osb from the global list and also free up all the
2126 * initialized resources and fileobject.
2127 */
2128static void ocfs2_delete_osb(struct ocfs2_super *osb)
2129{
2130 mlog_entry_void();
2131
2132 /* This function assumes that the caller has the main osb resource */
2133
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002134 ocfs2_free_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002135
Mark Fashehb4df6ed2006-02-22 17:35:08 -08002136 kfree(osb->osb_orphan_wipes);
Sunil Mushran539d8262008-07-14 17:31:10 -07002137 kfree(osb->slot_recovery_generations);
Mark Fashehccd979b2005-12-15 14:31:24 -08002138 /* FIXME
2139 * This belongs in journal shutdown, but because we have to
2140 * allocate osb->journal at the start of ocfs2_initalize_osb(),
2141 * we free it here.
2142 */
2143 kfree(osb->journal);
2144 if (osb->local_alloc_copy)
2145 kfree(osb->local_alloc_copy);
2146 kfree(osb->uuid_str);
2147 ocfs2_put_dlm_debug(osb->osb_dlm_debug);
2148 memset(osb, 0, sizeof(struct ocfs2_super));
2149
2150 mlog_exit_void();
2151}
2152
2153/* Put OCFS2 into a readonly state, or (if the user specifies it),
2154 * panic(). We do not support continue-on-error operation. */
2155static void ocfs2_handle_error(struct super_block *sb)
2156{
2157 struct ocfs2_super *osb = OCFS2_SB(sb);
2158
2159 if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_PANIC)
2160 panic("OCFS2: (device %s): panic forced after error\n",
2161 sb->s_id);
2162
2163 ocfs2_set_osb_flag(osb, OCFS2_OSB_ERROR_FS);
2164
2165 if (sb->s_flags & MS_RDONLY &&
2166 (ocfs2_is_soft_readonly(osb) ||
2167 ocfs2_is_hard_readonly(osb)))
2168 return;
2169
2170 printk(KERN_CRIT "File system is now read-only due to the potential "
2171 "of on-disk corruption. Please run fsck.ocfs2 once the file "
2172 "system is unmounted.\n");
2173 sb->s_flags |= MS_RDONLY;
2174 ocfs2_set_ro_flag(osb, 0);
2175}
2176
2177static char error_buf[1024];
2178
2179void __ocfs2_error(struct super_block *sb,
2180 const char *function,
2181 const char *fmt, ...)
2182{
2183 va_list args;
2184
2185 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002186 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Mark Fashehccd979b2005-12-15 14:31:24 -08002187 va_end(args);
2188
2189 /* Not using mlog here because we want to show the actual
2190 * function the error came from. */
2191 printk(KERN_CRIT "OCFS2: ERROR (device %s): %s: %s\n",
2192 sb->s_id, function, error_buf);
2193
2194 ocfs2_handle_error(sb);
2195}
2196
2197/* Handle critical errors. This is intentionally more drastic than
2198 * ocfs2_handle_error, so we only use for things like journal errors,
2199 * etc. */
2200void __ocfs2_abort(struct super_block* sb,
2201 const char *function,
2202 const char *fmt, ...)
2203{
2204 va_list args;
2205
2206 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002207 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Mark Fashehccd979b2005-12-15 14:31:24 -08002208 va_end(args);
2209
2210 printk(KERN_CRIT "OCFS2: abort (device %s): %s: %s\n",
2211 sb->s_id, function, error_buf);
2212
2213 /* We don't have the cluster support yet to go straight to
2214 * hard readonly in here. Until then, we want to keep
2215 * ocfs2_abort() so that we can at least mark critical
2216 * errors.
2217 *
2218 * TODO: This should abort the journal and alert other nodes
2219 * that our slot needs recovery. */
2220
2221 /* Force a panic(). This stinks, but it's better than letting
2222 * things continue without having a proper hard readonly
2223 * here. */
2224 OCFS2_SB(sb)->s_mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
2225 ocfs2_handle_error(sb);
2226}
2227
2228module_init(ocfs2_init);
2229module_exit(ocfs2_exit);