blob: 201b40a441fe806073c03bbb9fcc046f6423def7 [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>
Alessio Igor Bogani337eb002009-05-12 15:10:54 +020045#include <linux/smp_lock.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080046
47#define MLOG_MASK_PREFIX ML_SUPER
48#include <cluster/masklog.h>
49
50#include "ocfs2.h"
51
52/* this should be the only file to include a version 1 header */
53#include "ocfs1_fs_compat.h"
54
55#include "alloc.h"
Joel Beckerd030cc92008-12-11 15:04:14 -080056#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080057#include "dlmglue.h"
58#include "export.h"
59#include "extent_map.h"
60#include "heartbeat.h"
61#include "inode.h"
62#include "journal.h"
63#include "localalloc.h"
64#include "namei.h"
65#include "slot_map.h"
66#include "super.h"
67#include "sysfile.h"
68#include "uptodate.h"
69#include "ver.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080070#include "xattr.h"
Jan Kara9e33d692008-08-25 19:56:50 +020071#include "quota.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080072
73#include "buffer_head_io.h"
74
Christoph Lametere18b8902006-12-06 20:33:20 -080075static struct kmem_cache *ocfs2_inode_cachep = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +020076struct kmem_cache *ocfs2_dquot_cachep;
77struct kmem_cache *ocfs2_qf_chunk_cachep;
Mark Fashehccd979b2005-12-15 14:31:24 -080078
Mark Fashehccd979b2005-12-15 14:31:24 -080079/* OCFS2 needs to schedule several differnt types of work which
80 * require cluster locking, disk I/O, recovery waits, etc. Since these
81 * types of work tend to be heavy we avoid using the kernel events
82 * workqueue and schedule on our own. */
83struct workqueue_struct *ocfs2_wq = NULL;
84
85static struct dentry *ocfs2_debugfs_root = NULL;
86
87MODULE_AUTHOR("Oracle");
88MODULE_LICENSE("GPL");
89
Tiger Yangc0123ad2007-09-08 00:16:10 +080090struct mount_options
91{
Mark Fashehd147b3d2007-11-07 14:40:36 -080092 unsigned long commit_interval;
Tiger Yangc0123ad2007-09-08 00:16:10 +080093 unsigned long mount_opt;
94 unsigned int atime_quantum;
95 signed short slot;
Sunil Mushran2fbe8d12007-12-20 14:58:11 -080096 unsigned int localalloc_opt;
Joel Beckerb61817e2008-02-01 15:08:23 -080097 char cluster_stack[OCFS2_STACK_LABEL_LEN + 1];
Tiger Yangc0123ad2007-09-08 00:16:10 +080098};
99
Mark Fashehccd979b2005-12-15 14:31:24 -0800100static int ocfs2_parse_options(struct super_block *sb, char *options,
Tiger Yangc0123ad2007-09-08 00:16:10 +0800101 struct mount_options *mopt,
Sunil Mushranbaf46612007-06-18 17:00:24 -0700102 int is_remount);
Sunil Mushrand5500712007-09-06 13:34:16 -0700103static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt);
Mark Fashehccd979b2005-12-15 14:31:24 -0800104static void ocfs2_put_super(struct super_block *sb);
105static int ocfs2_mount_volume(struct super_block *sb);
106static int ocfs2_remount(struct super_block *sb, int *flags, char *data);
107static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err);
108static int ocfs2_initialize_mem_caches(void);
109static void ocfs2_free_mem_caches(void);
110static void ocfs2_delete_osb(struct ocfs2_super *osb);
111
David Howells726c3342006-06-23 02:02:58 -0700112static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf);
Mark Fashehccd979b2005-12-15 14:31:24 -0800113
114static int ocfs2_sync_fs(struct super_block *sb, int wait);
115
116static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb);
117static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb);
Denis Chengbddb8eb32007-09-27 02:10:04 +0800118static void ocfs2_release_system_inodes(struct ocfs2_super *osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800119static int ocfs2_check_volume(struct ocfs2_super *osb);
120static int ocfs2_verify_volume(struct ocfs2_dinode *di,
121 struct buffer_head *bh,
122 u32 sectsize);
123static int ocfs2_initialize_super(struct super_block *sb,
124 struct buffer_head *bh,
125 int sector_size);
126static int ocfs2_get_sector(struct super_block *sb,
127 struct buffer_head **bh,
128 int block,
129 int sect_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800130static struct inode *ocfs2_alloc_inode(struct super_block *sb);
131static void ocfs2_destroy_inode(struct inode *inode);
Jan Kara19ece542008-08-21 20:13:17 +0200132static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend);
133static int ocfs2_enable_quotas(struct ocfs2_super *osb);
134static void ocfs2_disable_quotas(struct ocfs2_super *osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800135
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800136static const struct super_operations ocfs2_sops = {
Mark Fashehccd979b2005-12-15 14:31:24 -0800137 .statfs = ocfs2_statfs,
138 .alloc_inode = ocfs2_alloc_inode,
139 .destroy_inode = ocfs2_destroy_inode,
140 .drop_inode = ocfs2_drop_inode,
141 .clear_inode = ocfs2_clear_inode,
142 .delete_inode = ocfs2_delete_inode,
143 .sync_fs = ocfs2_sync_fs,
Mark Fashehccd979b2005-12-15 14:31:24 -0800144 .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
Sunil Mushran50397502008-12-17 14:17:43 -0800203#ifdef CONFIG_DEBUG_FS
204static int ocfs2_osb_dump(struct ocfs2_super *osb, char *buf, int len)
205{
206 int out = 0;
207 int i;
208 struct ocfs2_cluster_connection *cconn = osb->cconn;
209 struct ocfs2_recovery_map *rm = osb->recovery_map;
210
211 out += snprintf(buf + out, len - out,
212 "%10s => Id: %-s Uuid: %-s Gen: 0x%X Label: %-s\n",
213 "Device", osb->dev_str, osb->uuid_str,
214 osb->fs_generation, osb->vol_label);
215
216 out += snprintf(buf + out, len - out,
217 "%10s => State: %d Flags: 0x%lX\n", "Volume",
218 atomic_read(&osb->vol_state), osb->osb_flags);
219
220 out += snprintf(buf + out, len - out,
221 "%10s => Block: %lu Cluster: %d\n", "Sizes",
222 osb->sb->s_blocksize, osb->s_clustersize);
223
224 out += snprintf(buf + out, len - out,
225 "%10s => Compat: 0x%X Incompat: 0x%X "
226 "ROcompat: 0x%X\n",
227 "Features", osb->s_feature_compat,
228 osb->s_feature_incompat, osb->s_feature_ro_compat);
229
230 out += snprintf(buf + out, len - out,
231 "%10s => Opts: 0x%lX AtimeQuanta: %u\n", "Mount",
232 osb->s_mount_opt, osb->s_atime_quantum);
233
234 out += snprintf(buf + out, len - out,
235 "%10s => Stack: %s Name: %*s Version: %d.%d\n",
236 "Cluster",
237 (*osb->osb_cluster_stack == '\0' ?
238 "o2cb" : osb->osb_cluster_stack),
239 cconn->cc_namelen, cconn->cc_name,
240 cconn->cc_version.pv_major, cconn->cc_version.pv_minor);
241
242 spin_lock(&osb->dc_task_lock);
243 out += snprintf(buf + out, len - out,
244 "%10s => Pid: %d Count: %lu WakeSeq: %lu "
245 "WorkSeq: %lu\n", "DownCnvt",
246 task_pid_nr(osb->dc_task), osb->blocked_lock_count,
247 osb->dc_wake_sequence, osb->dc_work_sequence);
248 spin_unlock(&osb->dc_task_lock);
249
250 spin_lock(&osb->osb_lock);
251 out += snprintf(buf + out, len - out, "%10s => Pid: %d Nodes:",
252 "Recovery",
253 (osb->recovery_thread_task ?
254 task_pid_nr(osb->recovery_thread_task) : -1));
255 if (rm->rm_used == 0)
256 out += snprintf(buf + out, len - out, " None\n");
257 else {
258 for (i = 0; i < rm->rm_used; i++)
259 out += snprintf(buf + out, len - out, " %d",
260 rm->rm_entries[i]);
261 out += snprintf(buf + out, len - out, "\n");
262 }
263 spin_unlock(&osb->osb_lock);
264
265 out += snprintf(buf + out, len - out,
266 "%10s => Pid: %d Interval: %lu Needs: %d\n", "Commit",
267 task_pid_nr(osb->commit_task), osb->osb_commit_interval,
268 atomic_read(&osb->needs_checkpoint));
269
270 out += snprintf(buf + out, len - out,
271 "%10s => State: %d NumTxns: %d TxnId: %lu\n",
272 "Journal", osb->journal->j_state,
273 atomic_read(&osb->journal->j_num_trans),
274 osb->journal->j_trans_id);
275
276 out += snprintf(buf + out, len - out,
277 "%10s => GlobalAllocs: %d LocalAllocs: %d "
278 "SubAllocs: %d LAWinMoves: %d SAExtends: %d\n",
279 "Stats",
280 atomic_read(&osb->alloc_stats.bitmap_data),
281 atomic_read(&osb->alloc_stats.local_data),
282 atomic_read(&osb->alloc_stats.bg_allocs),
283 atomic_read(&osb->alloc_stats.moves),
284 atomic_read(&osb->alloc_stats.bg_extends));
285
286 out += snprintf(buf + out, len - out,
287 "%10s => State: %u Descriptor: %llu Size: %u bits "
288 "Default: %u bits\n",
289 "LocalAlloc", osb->local_alloc_state,
290 (unsigned long long)osb->la_last_gd,
291 osb->local_alloc_bits, osb->local_alloc_default_bits);
292
293 spin_lock(&osb->osb_lock);
294 out += snprintf(buf + out, len - out,
295 "%10s => Slot: %d NumStolen: %d\n", "Steal",
296 osb->s_inode_steal_slot,
297 atomic_read(&osb->s_num_inodes_stolen));
298 spin_unlock(&osb->osb_lock);
299
300 out += snprintf(buf + out, len - out, "%10s => %3s %10s\n",
301 "Slots", "Num", "RecoGen");
302
303 for (i = 0; i < osb->max_slots; ++i) {
304 out += snprintf(buf + out, len - out,
305 "%10s %c %3d %10d\n",
306 " ",
307 (i == osb->slot_num ? '*' : ' '),
308 i, osb->slot_recovery_generations[i]);
309 }
310
311 return out;
312}
313
314static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
315{
316 struct ocfs2_super *osb = inode->i_private;
317 char *buf = NULL;
318
319 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
320 if (!buf)
321 goto bail;
322
323 i_size_write(inode, ocfs2_osb_dump(osb, buf, PAGE_SIZE));
324
325 file->private_data = buf;
326
327 return 0;
328bail:
329 return -ENOMEM;
330}
331
332static int ocfs2_debug_release(struct inode *inode, struct file *file)
333{
334 kfree(file->private_data);
335 return 0;
336}
337
338static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
339 size_t nbytes, loff_t *ppos)
340{
341 return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
342 i_size_read(file->f_mapping->host));
343}
344#else
345static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
346{
347 return 0;
348}
349static int ocfs2_debug_release(struct inode *inode, struct file *file)
350{
351 return 0;
352}
353static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
354 size_t nbytes, loff_t *ppos)
355{
356 return 0;
357}
358#endif /* CONFIG_DEBUG_FS */
359
360static struct file_operations ocfs2_osb_debug_fops = {
361 .open = ocfs2_osb_debug_open,
362 .release = ocfs2_debug_release,
363 .read = ocfs2_debug_read,
364 .llseek = generic_file_llseek,
365};
366
Mark Fashehccd979b2005-12-15 14:31:24 -0800367static int ocfs2_sync_fs(struct super_block *sb, int wait)
368{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800369 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -0800370 tid_t target;
371 struct ocfs2_super *osb = OCFS2_SB(sb);
372
Mark Fashehccd979b2005-12-15 14:31:24 -0800373 if (ocfs2_is_hard_readonly(osb))
374 return -EROFS;
375
376 if (wait) {
377 status = ocfs2_flush_truncate_log(osb);
378 if (status < 0)
379 mlog_errno(status);
380 } else {
381 ocfs2_schedule_truncate_log_flush(osb, 0);
382 }
383
Joel Becker2b4e30f2008-09-03 20:03:41 -0700384 if (jbd2_journal_start_commit(OCFS2_SB(sb)->journal->j_journal,
385 &target)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800386 if (wait)
Joel Becker2b4e30f2008-09-03 20:03:41 -0700387 jbd2_log_wait_commit(OCFS2_SB(sb)->journal->j_journal,
388 target);
Mark Fashehccd979b2005-12-15 14:31:24 -0800389 }
390 return 0;
391}
392
Jan Kara1a224ad2008-08-20 15:43:36 +0200393static int ocfs2_need_system_inode(struct ocfs2_super *osb, int ino)
394{
395 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_USRQUOTA)
396 && (ino == USER_QUOTA_SYSTEM_INODE
397 || ino == LOCAL_USER_QUOTA_SYSTEM_INODE))
398 return 0;
399 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)
400 && (ino == GROUP_QUOTA_SYSTEM_INODE
401 || ino == LOCAL_GROUP_QUOTA_SYSTEM_INODE))
402 return 0;
403 return 1;
404}
405
Mark Fashehccd979b2005-12-15 14:31:24 -0800406static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb)
407{
408 struct inode *new = NULL;
409 int status = 0;
410 int i;
411
412 mlog_entry_void();
413
Jan Kara5fa06132008-01-11 00:11:45 +0100414 new = ocfs2_iget(osb, osb->root_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800415 if (IS_ERR(new)) {
416 status = PTR_ERR(new);
417 mlog_errno(status);
418 goto bail;
419 }
420 osb->root_inode = new;
421
Jan Kara5fa06132008-01-11 00:11:45 +0100422 new = ocfs2_iget(osb, osb->system_dir_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800423 if (IS_ERR(new)) {
424 status = PTR_ERR(new);
425 mlog_errno(status);
426 goto bail;
427 }
428 osb->sys_root_inode = new;
429
430 for (i = OCFS2_FIRST_ONLINE_SYSTEM_INODE;
431 i <= OCFS2_LAST_GLOBAL_SYSTEM_INODE; i++) {
Jan Kara1a224ad2008-08-20 15:43:36 +0200432 if (!ocfs2_need_system_inode(osb, i))
433 continue;
Mark Fashehccd979b2005-12-15 14:31:24 -0800434 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
435 if (!new) {
436 ocfs2_release_system_inodes(osb);
437 status = -EINVAL;
438 mlog_errno(status);
439 /* FIXME: Should ERROR_RO_FS */
440 mlog(ML_ERROR, "Unable to load system inode %d, "
441 "possibly corrupt fs?", i);
442 goto bail;
443 }
444 // the array now has one ref, so drop this one
445 iput(new);
446 }
447
448bail:
449 mlog_exit(status);
450 return status;
451}
452
453static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb)
454{
455 struct inode *new = NULL;
456 int status = 0;
457 int i;
458
459 mlog_entry_void();
460
461 for (i = OCFS2_LAST_GLOBAL_SYSTEM_INODE + 1;
462 i < NUM_SYSTEM_INODES;
463 i++) {
Jan Kara1a224ad2008-08-20 15:43:36 +0200464 if (!ocfs2_need_system_inode(osb, i))
465 continue;
Mark Fashehccd979b2005-12-15 14:31:24 -0800466 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
467 if (!new) {
468 ocfs2_release_system_inodes(osb);
469 status = -EINVAL;
470 mlog(ML_ERROR, "status=%d, sysfile=%d, slot=%d\n",
471 status, i, osb->slot_num);
472 goto bail;
473 }
474 /* the array now has one ref, so drop this one */
475 iput(new);
476 }
477
478bail:
479 mlog_exit(status);
480 return status;
481}
482
Denis Chengbddb8eb32007-09-27 02:10:04 +0800483static void ocfs2_release_system_inodes(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -0800484{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800485 int i;
Mark Fashehccd979b2005-12-15 14:31:24 -0800486 struct inode *inode;
487
488 mlog_entry_void();
489
490 for (i = 0; i < NUM_SYSTEM_INODES; i++) {
491 inode = osb->system_inodes[i];
492 if (inode) {
493 iput(inode);
494 osb->system_inodes[i] = NULL;
495 }
496 }
497
498 inode = osb->sys_root_inode;
499 if (inode) {
500 iput(inode);
501 osb->sys_root_inode = NULL;
502 }
503
504 inode = osb->root_inode;
505 if (inode) {
506 iput(inode);
507 osb->root_inode = NULL;
508 }
509
Denis Chengbddb8eb32007-09-27 02:10:04 +0800510 mlog_exit(0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800511}
512
513/* We're allocating fs objects, use GFP_NOFS */
514static struct inode *ocfs2_alloc_inode(struct super_block *sb)
515{
516 struct ocfs2_inode_info *oi;
517
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800518 oi = kmem_cache_alloc(ocfs2_inode_cachep, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800519 if (!oi)
520 return NULL;
521
Joel Becker2b4e30f2008-09-03 20:03:41 -0700522 jbd2_journal_init_jbd_inode(&oi->ip_jinode, &oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800523 return &oi->vfs_inode;
524}
525
526static void ocfs2_destroy_inode(struct inode *inode)
527{
528 kmem_cache_free(ocfs2_inode_cachep, OCFS2_I(inode));
529}
530
Mark Fasheh5a254032007-07-20 12:56:16 -0700531static unsigned long long ocfs2_max_file_offset(unsigned int bbits,
532 unsigned int cbits)
Mark Fashehccd979b2005-12-15 14:31:24 -0800533{
Mark Fasheh5a254032007-07-20 12:56:16 -0700534 unsigned int bytes = 1 << cbits;
535 unsigned int trim = bytes;
536 unsigned int bitshift = 32;
Mark Fashehccd979b2005-12-15 14:31:24 -0800537
Mark Fasheh5a254032007-07-20 12:56:16 -0700538 /*
539 * i_size and all block offsets in ocfs2 are always 64 bits
540 * wide. i_clusters is 32 bits, in cluster-sized units. So on
541 * 64 bit platforms, cluster size will be the limiting factor.
Mark Fashehccd979b2005-12-15 14:31:24 -0800542 */
543
544#if BITS_PER_LONG == 32
545# if defined(CONFIG_LBD)
Alexey Dobriyan2ecd05a2006-10-11 01:22:05 -0700546 BUILD_BUG_ON(sizeof(sector_t) != 8);
Mark Fasheh5a254032007-07-20 12:56:16 -0700547 /*
548 * We might be limited by page cache size.
549 */
550 if (bytes > PAGE_CACHE_SIZE) {
551 bytes = PAGE_CACHE_SIZE;
552 trim = 1;
553 /*
554 * Shift by 31 here so that we don't get larger than
555 * MAX_LFS_FILESIZE
556 */
557 bitshift = 31;
558 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800559# else
Mark Fasheh5a254032007-07-20 12:56:16 -0700560 /*
561 * We are limited by the size of sector_t. Use block size, as
562 * that's what we expose to the VFS.
563 */
564 bytes = 1 << bbits;
565 trim = 1;
566 bitshift = 31;
Mark Fashehccd979b2005-12-15 14:31:24 -0800567# endif
568#endif
569
Mark Fasheh5a254032007-07-20 12:56:16 -0700570 /*
571 * Trim by a whole cluster when we can actually approach the
572 * on-disk limits. Otherwise we can overflow i_clusters when
573 * an extent start is at the max offset.
574 */
575 return (((unsigned long long)bytes) << bitshift) - trim;
Mark Fashehccd979b2005-12-15 14:31:24 -0800576}
577
578static int ocfs2_remount(struct super_block *sb, int *flags, char *data)
579{
580 int incompat_features;
581 int ret = 0;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800582 struct mount_options parsed_options;
Mark Fashehccd979b2005-12-15 14:31:24 -0800583 struct ocfs2_super *osb = OCFS2_SB(sb);
584
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200585 lock_kernel();
586
Tiger Yangc0123ad2007-09-08 00:16:10 +0800587 if (!ocfs2_parse_options(sb, data, &parsed_options, 1)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800588 ret = -EINVAL;
589 goto out;
590 }
591
592 if ((osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) !=
Tiger Yangc0123ad2007-09-08 00:16:10 +0800593 (parsed_options.mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800594 ret = -EINVAL;
595 mlog(ML_ERROR, "Cannot change heartbeat mode on remount\n");
596 goto out;
597 }
598
599 if ((osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK) !=
Tiger Yangc0123ad2007-09-08 00:16:10 +0800600 (parsed_options.mount_opt & OCFS2_MOUNT_DATA_WRITEBACK)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800601 ret = -EINVAL;
602 mlog(ML_ERROR, "Cannot change data mode on remount\n");
603 goto out;
604 }
605
Joel Becker12462f12008-09-03 20:03:40 -0700606 /* Probably don't want this on remount; it might
607 * mess with other nodes */
608 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64) &&
609 (parsed_options.mount_opt & OCFS2_MOUNT_INODE64)) {
610 ret = -EINVAL;
611 mlog(ML_ERROR, "Cannot enable inode64 on remount\n");
612 goto out;
613 }
614
Mark Fashehccd979b2005-12-15 14:31:24 -0800615 /* We're going to/from readonly mode. */
616 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
Jan Kara19ece542008-08-21 20:13:17 +0200617 /* Disable quota accounting before remounting RO */
618 if (*flags & MS_RDONLY) {
619 ret = ocfs2_susp_quotas(osb, 0);
620 if (ret < 0)
621 goto out;
622 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800623 /* Lock here so the check of HARD_RO and the potential
624 * setting of SOFT_RO is atomic. */
625 spin_lock(&osb->osb_lock);
626 if (osb->osb_flags & OCFS2_OSB_HARD_RO) {
627 mlog(ML_ERROR, "Remount on readonly device is forbidden.\n");
628 ret = -EROFS;
629 goto unlock_osb;
630 }
631
632 if (*flags & MS_RDONLY) {
633 mlog(0, "Going to ro mode.\n");
634 sb->s_flags |= MS_RDONLY;
635 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
636 } else {
637 mlog(0, "Making ro filesystem writeable.\n");
638
639 if (osb->osb_flags & OCFS2_OSB_ERROR_FS) {
640 mlog(ML_ERROR, "Cannot remount RDWR "
641 "filesystem due to previous errors.\n");
642 ret = -EROFS;
643 goto unlock_osb;
644 }
645 incompat_features = OCFS2_HAS_RO_COMPAT_FEATURE(sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP);
646 if (incompat_features) {
647 mlog(ML_ERROR, "Cannot remount RDWR because "
648 "of unsupported optional features "
649 "(%x).\n", incompat_features);
650 ret = -EINVAL;
651 goto unlock_osb;
652 }
653 sb->s_flags &= ~MS_RDONLY;
654 osb->osb_flags &= ~OCFS2_OSB_SOFT_RO;
655 }
656unlock_osb:
657 spin_unlock(&osb->osb_lock);
Jan Kara19ece542008-08-21 20:13:17 +0200658 /* Enable quota accounting after remounting RW */
659 if (!ret && !(*flags & MS_RDONLY)) {
660 if (sb_any_quota_suspended(sb))
661 ret = ocfs2_susp_quotas(osb, 1);
662 else
663 ret = ocfs2_enable_quotas(osb);
664 if (ret < 0) {
665 /* Return back changes... */
666 spin_lock(&osb->osb_lock);
667 sb->s_flags |= MS_RDONLY;
668 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
669 spin_unlock(&osb->osb_lock);
670 goto out;
671 }
672 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800673 }
674
675 if (!ret) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800676 /* Only save off the new mount options in case of a successful
677 * remount. */
Tiger Yanga68979b2008-11-14 11:17:52 +0800678 if (!(osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR))
679 parsed_options.mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800680 osb->s_mount_opt = parsed_options.mount_opt;
681 osb->s_atime_quantum = parsed_options.atime_quantum;
682 osb->preferred_slot = parsed_options.slot;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800683 if (parsed_options.commit_interval)
684 osb->osb_commit_interval = parsed_options.commit_interval;
Mark Fashehe001e792007-11-07 14:21:45 -0800685
686 if (!ocfs2_is_hard_readonly(osb))
687 ocfs2_set_journal_params(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800688 }
689out:
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200690 unlock_kernel();
Mark Fashehccd979b2005-12-15 14:31:24 -0800691 return ret;
692}
693
694static int ocfs2_sb_probe(struct super_block *sb,
695 struct buffer_head **bh,
696 int *sector_size)
697{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800698 int status, tmpstat;
Mark Fashehccd979b2005-12-15 14:31:24 -0800699 struct ocfs1_vol_disk_hdr *hdr;
700 struct ocfs2_dinode *di;
701 int blksize;
702
703 *bh = NULL;
704
705 /* may be > 512 */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400706 *sector_size = bdev_logical_block_size(sb->s_bdev);
Mark Fashehccd979b2005-12-15 14:31:24 -0800707 if (*sector_size > OCFS2_MAX_BLOCKSIZE) {
708 mlog(ML_ERROR, "Hardware sector size too large: %d (max=%d)\n",
709 *sector_size, OCFS2_MAX_BLOCKSIZE);
710 status = -EINVAL;
711 goto bail;
712 }
713
714 /* Can this really happen? */
715 if (*sector_size < OCFS2_MIN_BLOCKSIZE)
716 *sector_size = OCFS2_MIN_BLOCKSIZE;
717
718 /* check block zero for old format */
719 status = ocfs2_get_sector(sb, bh, 0, *sector_size);
720 if (status < 0) {
721 mlog_errno(status);
722 goto bail;
723 }
724 hdr = (struct ocfs1_vol_disk_hdr *) (*bh)->b_data;
725 if (hdr->major_version == OCFS1_MAJOR_VERSION) {
726 mlog(ML_ERROR, "incompatible version: %u.%u\n",
727 hdr->major_version, hdr->minor_version);
728 status = -EINVAL;
729 }
730 if (memcmp(hdr->signature, OCFS1_VOLUME_SIGNATURE,
731 strlen(OCFS1_VOLUME_SIGNATURE)) == 0) {
732 mlog(ML_ERROR, "incompatible volume signature: %8s\n",
733 hdr->signature);
734 status = -EINVAL;
735 }
736 brelse(*bh);
737 *bh = NULL;
738 if (status < 0) {
739 mlog(ML_ERROR, "This is an ocfs v1 filesystem which must be "
740 "upgraded before mounting with ocfs v2\n");
741 goto bail;
742 }
743
744 /*
745 * Now check at magic offset for 512, 1024, 2048, 4096
746 * blocksizes. 4096 is the maximum blocksize because it is
747 * the minimum clustersize.
748 */
749 status = -EINVAL;
750 for (blksize = *sector_size;
751 blksize <= OCFS2_MAX_BLOCKSIZE;
752 blksize <<= 1) {
753 tmpstat = ocfs2_get_sector(sb, bh,
754 OCFS2_SUPER_BLOCK_BLKNO,
755 blksize);
756 if (tmpstat < 0) {
757 status = tmpstat;
758 mlog_errno(status);
759 goto bail;
760 }
761 di = (struct ocfs2_dinode *) (*bh)->b_data;
762 status = ocfs2_verify_volume(di, *bh, blksize);
763 if (status >= 0)
764 goto bail;
765 brelse(*bh);
766 *bh = NULL;
767 if (status != -EAGAIN)
768 break;
769 }
770
771bail:
772 return status;
773}
774
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800775static int ocfs2_verify_heartbeat(struct ocfs2_super *osb)
776{
777 if (ocfs2_mount_local(osb)) {
778 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
779 mlog(ML_ERROR, "Cannot heartbeat on a locally "
780 "mounted device.\n");
781 return -EINVAL;
782 }
783 }
784
Joel Beckerb61817e2008-02-01 15:08:23 -0800785 if (ocfs2_userspace_stack(osb)) {
786 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
787 mlog(ML_ERROR, "Userspace stack expected, but "
788 "o2cb heartbeat arguments passed to mount\n");
789 return -EINVAL;
790 }
791 }
792
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800793 if (!(osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
Joel Beckerb61817e2008-02-01 15:08:23 -0800794 if (!ocfs2_mount_local(osb) && !ocfs2_is_hard_readonly(osb) &&
795 !ocfs2_userspace_stack(osb)) {
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800796 mlog(ML_ERROR, "Heartbeat has to be started to mount "
797 "a read-write clustered device.\n");
798 return -EINVAL;
799 }
800 }
801
802 return 0;
803}
804
Joel Beckerb61817e2008-02-01 15:08:23 -0800805/*
806 * If we're using a userspace stack, mount should have passed
807 * a name that matches the disk. If not, mount should not
808 * have passed a stack.
809 */
810static int ocfs2_verify_userspace_stack(struct ocfs2_super *osb,
811 struct mount_options *mopt)
812{
813 if (!ocfs2_userspace_stack(osb) && mopt->cluster_stack[0]) {
814 mlog(ML_ERROR,
815 "cluster stack passed to mount, but this filesystem "
816 "does not support it\n");
817 return -EINVAL;
818 }
819
820 if (ocfs2_userspace_stack(osb) &&
821 strncmp(osb->osb_cluster_stack, mopt->cluster_stack,
822 OCFS2_STACK_LABEL_LEN)) {
823 mlog(ML_ERROR,
824 "cluster stack passed to mount (\"%s\") does not "
825 "match the filesystem (\"%s\")\n",
826 mopt->cluster_stack,
827 osb->osb_cluster_stack);
828 return -EINVAL;
829 }
830
831 return 0;
832}
833
Jan Kara19ece542008-08-21 20:13:17 +0200834static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend)
835{
836 int type;
837 struct super_block *sb = osb->sb;
838 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
839 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
840 int status = 0;
841
842 for (type = 0; type < MAXQUOTAS; type++) {
843 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
844 continue;
845 if (unsuspend)
846 status = vfs_quota_enable(
847 sb_dqopt(sb)->files[type],
848 type, QFMT_OCFS2,
849 DQUOT_SUSPENDED);
850 else
851 status = vfs_quota_disable(sb, type,
852 DQUOT_SUSPENDED);
853 if (status < 0)
854 break;
855 }
856 if (status < 0)
857 mlog(ML_ERROR, "Failed to suspend/unsuspend quotas on "
858 "remount (error = %d).\n", status);
859 return status;
860}
861
862static int ocfs2_enable_quotas(struct ocfs2_super *osb)
863{
864 struct inode *inode[MAXQUOTAS] = { NULL, NULL };
865 struct super_block *sb = osb->sb;
866 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
867 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
868 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
869 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
870 int status;
871 int type;
872
873 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NEGATIVE_USAGE;
874 for (type = 0; type < MAXQUOTAS; type++) {
875 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
876 continue;
877 inode[type] = ocfs2_get_system_file_inode(osb, ino[type],
878 osb->slot_num);
879 if (!inode[type]) {
880 status = -ENOENT;
881 goto out_quota_off;
882 }
883 status = vfs_quota_enable(inode[type], type, QFMT_OCFS2,
884 DQUOT_USAGE_ENABLED);
885 if (status < 0)
886 goto out_quota_off;
887 }
888
889 for (type = 0; type < MAXQUOTAS; type++)
890 iput(inode[type]);
891 return 0;
892out_quota_off:
893 ocfs2_disable_quotas(osb);
894 for (type = 0; type < MAXQUOTAS; type++)
895 iput(inode[type]);
896 mlog_errno(status);
897 return status;
898}
899
900static void ocfs2_disable_quotas(struct ocfs2_super *osb)
901{
902 int type;
903 struct inode *inode;
904 struct super_block *sb = osb->sb;
905
906 /* We mostly ignore errors in this function because there's not much
907 * we can do when we see them */
908 for (type = 0; type < MAXQUOTAS; type++) {
909 if (!sb_has_quota_loaded(sb, type))
910 continue;
911 inode = igrab(sb->s_dquot.files[type]);
912 /* Turn off quotas. This will remove all dquot structures from
913 * memory and so they will be automatically synced to global
914 * quota files */
915 vfs_quota_disable(sb, type, DQUOT_USAGE_ENABLED |
916 DQUOT_LIMITS_ENABLED);
917 if (!inode)
918 continue;
919 iput(inode);
920 }
921}
922
923/* Handle quota on quotactl */
924static int ocfs2_quota_on(struct super_block *sb, int type, int format_id,
925 char *path, int remount)
926{
927 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
928 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
929
930 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
931 return -EINVAL;
932
933 if (remount)
934 return 0; /* Just ignore it has been handled in
935 * ocfs2_remount() */
936 return vfs_quota_enable(sb_dqopt(sb)->files[type], type,
937 format_id, DQUOT_LIMITS_ENABLED);
938}
939
940/* Handle quota off quotactl */
941static int ocfs2_quota_off(struct super_block *sb, int type, int remount)
942{
943 if (remount)
944 return 0; /* Ignore now and handle later in
945 * ocfs2_remount() */
946 return vfs_quota_disable(sb, type, DQUOT_LIMITS_ENABLED);
947}
948
949static struct quotactl_ops ocfs2_quotactl_ops = {
950 .quota_on = ocfs2_quota_on,
951 .quota_off = ocfs2_quota_off,
952 .quota_sync = vfs_quota_sync,
953 .get_info = vfs_get_dqinfo,
954 .set_info = vfs_set_dqinfo,
955 .get_dqblk = vfs_get_dqblk,
956 .set_dqblk = vfs_set_dqblk,
957};
958
Mark Fashehccd979b2005-12-15 14:31:24 -0800959static int ocfs2_fill_super(struct super_block *sb, void *data, int silent)
960{
961 struct dentry *root;
962 int status, sector_size;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800963 struct mount_options parsed_options;
Mark Fashehccd979b2005-12-15 14:31:24 -0800964 struct inode *inode = NULL;
965 struct ocfs2_super *osb = NULL;
966 struct buffer_head *bh = NULL;
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800967 char nodestr[8];
Mark Fashehccd979b2005-12-15 14:31:24 -0800968
969 mlog_entry("%p, %p, %i", sb, data, silent);
970
Tiger Yangc0123ad2007-09-08 00:16:10 +0800971 if (!ocfs2_parse_options(sb, data, &parsed_options, 0)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800972 status = -EINVAL;
973 goto read_super_error;
974 }
975
976 /* probe for superblock */
977 status = ocfs2_sb_probe(sb, &bh, &sector_size);
978 if (status < 0) {
979 mlog(ML_ERROR, "superblock probe failed!\n");
980 goto read_super_error;
981 }
982
983 status = ocfs2_initialize_super(sb, bh, sector_size);
984 osb = OCFS2_SB(sb);
985 if (status < 0) {
986 mlog_errno(status);
987 goto read_super_error;
988 }
989 brelse(bh);
990 bh = NULL;
Tiger Yanga68979b2008-11-14 11:17:52 +0800991
992 if (!(osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR))
993 parsed_options.mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
994
Tiger Yangc0123ad2007-09-08 00:16:10 +0800995 osb->s_mount_opt = parsed_options.mount_opt;
996 osb->s_atime_quantum = parsed_options.atime_quantum;
997 osb->preferred_slot = parsed_options.slot;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800998 osb->osb_commit_interval = parsed_options.commit_interval;
Mark Fasheh9c7af402008-07-28 18:02:53 -0700999 osb->local_alloc_default_bits = ocfs2_megabytes_to_clusters(sb, parsed_options.localalloc_opt);
1000 osb->local_alloc_bits = osb->local_alloc_default_bits;
Jan Kara19ece542008-08-21 20:13:17 +02001001 if (osb->s_mount_opt & OCFS2_MOUNT_USRQUOTA &&
1002 !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1003 OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
1004 status = -EINVAL;
1005 mlog(ML_ERROR, "User quotas were requested, but this "
1006 "filesystem does not have the feature enabled.\n");
1007 goto read_super_error;
1008 }
1009 if (osb->s_mount_opt & OCFS2_MOUNT_GRPQUOTA &&
1010 !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1011 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
1012 status = -EINVAL;
1013 mlog(ML_ERROR, "Group quotas were requested, but this "
1014 "filesystem does not have the feature enabled.\n");
1015 goto read_super_error;
1016 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001017
Joel Beckerb61817e2008-02-01 15:08:23 -08001018 status = ocfs2_verify_userspace_stack(osb, &parsed_options);
1019 if (status)
1020 goto read_super_error;
1021
Mark Fashehccd979b2005-12-15 14:31:24 -08001022 sb->s_magic = OCFS2_SUPER_MAGIC;
1023
Tiger Yanga68979b2008-11-14 11:17:52 +08001024 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1025 ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
1026
Mark Fashehccd979b2005-12-15 14:31:24 -08001027 /* Hard readonly mode only if: bdev_read_only, MS_RDONLY,
1028 * heartbeat=none */
1029 if (bdev_read_only(sb->s_bdev)) {
1030 if (!(sb->s_flags & MS_RDONLY)) {
1031 status = -EACCES;
1032 mlog(ML_ERROR, "Readonly device detected but readonly "
1033 "mount was not specified.\n");
1034 goto read_super_error;
1035 }
1036
1037 /* You should not be able to start a local heartbeat
1038 * on a readonly device. */
1039 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
1040 status = -EROFS;
1041 mlog(ML_ERROR, "Local heartbeat specified on readonly "
1042 "device.\n");
1043 goto read_super_error;
1044 }
1045
1046 status = ocfs2_check_journals_nolocks(osb);
1047 if (status < 0) {
1048 if (status == -EROFS)
1049 mlog(ML_ERROR, "Recovery required on readonly "
1050 "file system, but write access is "
1051 "unavailable.\n");
1052 else
1053 mlog_errno(status);
1054 goto read_super_error;
1055 }
1056
1057 ocfs2_set_ro_flag(osb, 1);
1058
1059 printk(KERN_NOTICE "Readonly device detected. No cluster "
1060 "services will be utilized for this mount. Recovery "
1061 "will be skipped.\n");
1062 }
1063
1064 if (!ocfs2_is_hard_readonly(osb)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001065 if (sb->s_flags & MS_RDONLY)
1066 ocfs2_set_ro_flag(osb, 0);
1067 }
1068
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001069 status = ocfs2_verify_heartbeat(osb);
1070 if (status < 0) {
1071 mlog_errno(status);
1072 goto read_super_error;
1073 }
1074
Mark Fashehccd979b2005-12-15 14:31:24 -08001075 osb->osb_debug_root = debugfs_create_dir(osb->uuid_str,
1076 ocfs2_debugfs_root);
1077 if (!osb->osb_debug_root) {
1078 status = -EINVAL;
1079 mlog(ML_ERROR, "Unable to create per-mount debugfs root.\n");
1080 goto read_super_error;
1081 }
1082
Sunil Mushran50397502008-12-17 14:17:43 -08001083 osb->osb_ctxt = debugfs_create_file("fs_state", S_IFREG|S_IRUSR,
1084 osb->osb_debug_root,
1085 osb,
1086 &ocfs2_osb_debug_fops);
1087 if (!osb->osb_ctxt) {
1088 status = -EINVAL;
1089 mlog_errno(status);
1090 goto read_super_error;
1091 }
1092
Mark Fashehccd979b2005-12-15 14:31:24 -08001093 status = ocfs2_mount_volume(sb);
1094 if (osb->root_inode)
1095 inode = igrab(osb->root_inode);
1096
1097 if (status < 0)
1098 goto read_super_error;
1099
1100 if (!inode) {
1101 status = -EIO;
1102 mlog_errno(status);
1103 goto read_super_error;
1104 }
1105
1106 root = d_alloc_root(inode);
1107 if (!root) {
1108 status = -ENOMEM;
1109 mlog_errno(status);
1110 goto read_super_error;
1111 }
1112
1113 sb->s_root = root;
1114
1115 ocfs2_complete_mount_recovery(osb);
1116
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001117 if (ocfs2_mount_local(osb))
1118 snprintf(nodestr, sizeof(nodestr), "local");
1119 else
Joel Becker19fdb622008-01-30 15:38:24 -08001120 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001121
1122 printk(KERN_INFO "ocfs2: Mounting device (%s) on (node %s, slot %d) "
Sunil Mushran781ee3e2006-04-27 16:41:31 -07001123 "with %s data mode.\n",
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001124 osb->dev_str, nodestr, osb->slot_num,
Mark Fashehccd979b2005-12-15 14:31:24 -08001125 osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK ? "writeback" :
1126 "ordered");
1127
1128 atomic_set(&osb->vol_state, VOLUME_MOUNTED);
1129 wake_up(&osb->osb_mount_event);
1130
Jan Kara19ece542008-08-21 20:13:17 +02001131 /* Now we can initialize quotas because we can afford to wait
1132 * for cluster locks recovery now. That also means that truncation
1133 * log recovery can happen but that waits for proper quota setup */
1134 if (!(sb->s_flags & MS_RDONLY)) {
1135 status = ocfs2_enable_quotas(osb);
1136 if (status < 0) {
1137 /* We have to err-out specially here because
1138 * s_root is already set */
1139 mlog_errno(status);
1140 atomic_set(&osb->vol_state, VOLUME_DISABLED);
1141 wake_up(&osb->osb_mount_event);
1142 mlog_exit(status);
1143 return status;
1144 }
1145 }
1146
1147 ocfs2_complete_quota_recovery(osb);
1148
1149 /* Now we wake up again for processes waiting for quotas */
1150 atomic_set(&osb->vol_state, VOLUME_MOUNTED_QUOTAS);
1151 wake_up(&osb->osb_mount_event);
1152
Mark Fashehccd979b2005-12-15 14:31:24 -08001153 mlog_exit(status);
1154 return status;
1155
1156read_super_error:
Mark Fasheha81cb882008-10-07 14:25:16 -07001157 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001158
1159 if (inode)
1160 iput(inode);
1161
1162 if (osb) {
1163 atomic_set(&osb->vol_state, VOLUME_DISABLED);
1164 wake_up(&osb->osb_mount_event);
1165 ocfs2_dismount_volume(sb, 1);
1166 }
1167
1168 mlog_exit(status);
1169 return status;
1170}
1171
David Howells454e2392006-06-23 02:02:57 -07001172static int ocfs2_get_sb(struct file_system_type *fs_type,
1173 int flags,
1174 const char *dev_name,
1175 void *data,
1176 struct vfsmount *mnt)
Mark Fashehccd979b2005-12-15 14:31:24 -08001177{
David Howells454e2392006-06-23 02:02:57 -07001178 return get_sb_bdev(fs_type, flags, dev_name, data, ocfs2_fill_super,
1179 mnt);
Mark Fashehccd979b2005-12-15 14:31:24 -08001180}
1181
1182static struct file_system_type ocfs2_fs_type = {
1183 .owner = THIS_MODULE,
1184 .name = "ocfs2",
1185 .get_sb = ocfs2_get_sb, /* is this called when we mount
1186 * the fs? */
1187 .kill_sb = kill_block_super, /* set to the generic one
1188 * right now, but do we
1189 * need to change that? */
Mark Fasheh1ba9da22006-09-08 14:22:54 -07001190 .fs_flags = FS_REQUIRES_DEV|FS_RENAME_DOES_D_MOVE,
Mark Fashehccd979b2005-12-15 14:31:24 -08001191 .next = NULL
1192};
1193
1194static int ocfs2_parse_options(struct super_block *sb,
1195 char *options,
Tiger Yangc0123ad2007-09-08 00:16:10 +08001196 struct mount_options *mopt,
Mark Fashehccd979b2005-12-15 14:31:24 -08001197 int is_remount)
1198{
1199 int status;
1200 char *p;
1201
1202 mlog_entry("remount: %d, options: \"%s\"\n", is_remount,
1203 options ? options : "(none)");
1204
Mark Fashehd147b3d2007-11-07 14:40:36 -08001205 mopt->commit_interval = 0;
Tiger Yangc0123ad2007-09-08 00:16:10 +08001206 mopt->mount_opt = 0;
1207 mopt->atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
1208 mopt->slot = OCFS2_INVALID_SLOT;
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001209 mopt->localalloc_opt = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE;
Joel Beckerb61817e2008-02-01 15:08:23 -08001210 mopt->cluster_stack[0] = '\0';
Mark Fashehccd979b2005-12-15 14:31:24 -08001211
1212 if (!options) {
1213 status = 1;
1214 goto bail;
1215 }
1216
1217 while ((p = strsep(&options, ",")) != NULL) {
1218 int token, option;
1219 substring_t args[MAX_OPT_ARGS];
1220
1221 if (!*p)
1222 continue;
1223
1224 token = match_token(p, tokens, args);
1225 switch (token) {
1226 case Opt_hb_local:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001227 mopt->mount_opt |= OCFS2_MOUNT_HB_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001228 break;
1229 case Opt_hb_none:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001230 mopt->mount_opt &= ~OCFS2_MOUNT_HB_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001231 break;
1232 case Opt_barrier:
1233 if (match_int(&args[0], &option)) {
1234 status = 0;
1235 goto bail;
1236 }
1237 if (option)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001238 mopt->mount_opt |= OCFS2_MOUNT_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -08001239 else
Tiger Yangc0123ad2007-09-08 00:16:10 +08001240 mopt->mount_opt &= ~OCFS2_MOUNT_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -08001241 break;
1242 case Opt_intr:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001243 mopt->mount_opt &= ~OCFS2_MOUNT_NOINTR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001244 break;
1245 case Opt_nointr:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001246 mopt->mount_opt |= OCFS2_MOUNT_NOINTR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001247 break;
1248 case Opt_err_panic:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001249 mopt->mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001250 break;
1251 case Opt_err_ro:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001252 mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_PANIC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001253 break;
1254 case Opt_data_ordered:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001255 mopt->mount_opt &= ~OCFS2_MOUNT_DATA_WRITEBACK;
Mark Fashehccd979b2005-12-15 14:31:24 -08001256 break;
1257 case Opt_data_writeback:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001258 mopt->mount_opt |= OCFS2_MOUNT_DATA_WRITEBACK;
Mark Fashehccd979b2005-12-15 14:31:24 -08001259 break;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001260 case Opt_user_xattr:
1261 mopt->mount_opt &= ~OCFS2_MOUNT_NOUSERXATTR;
1262 break;
1263 case Opt_nouser_xattr:
1264 mopt->mount_opt |= OCFS2_MOUNT_NOUSERXATTR;
1265 break;
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001266 case Opt_atime_quantum:
1267 if (match_int(&args[0], &option)) {
1268 status = 0;
1269 goto bail;
1270 }
1271 if (option >= 0)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001272 mopt->atime_quantum = option;
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001273 break;
Sunil Mushranbaf46612007-06-18 17:00:24 -07001274 case Opt_slot:
1275 option = 0;
1276 if (match_int(&args[0], &option)) {
1277 status = 0;
1278 goto bail;
1279 }
1280 if (option)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001281 mopt->slot = (s16)option;
Sunil Mushranbaf46612007-06-18 17:00:24 -07001282 break;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001283 case Opt_commit:
1284 option = 0;
1285 if (match_int(&args[0], &option)) {
1286 status = 0;
1287 goto bail;
1288 }
1289 if (option < 0)
1290 return 0;
1291 if (option == 0)
Joel Becker2b4e30f2008-09-03 20:03:41 -07001292 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001293 mopt->commit_interval = HZ * option;
1294 break;
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001295 case Opt_localalloc:
1296 option = 0;
1297 if (match_int(&args[0], &option)) {
1298 status = 0;
1299 goto bail;
1300 }
1301 if (option >= 0 && (option <= ocfs2_local_alloc_size(sb) * 8))
1302 mopt->localalloc_opt = option;
1303 break;
Mark Fasheh53fc6222007-12-20 16:49:04 -08001304 case Opt_localflocks:
1305 /*
1306 * Changing this during remount could race
1307 * flock() requests, or "unbalance" existing
1308 * ones (e.g., a lock is taken in one mode but
1309 * dropped in the other). If users care enough
1310 * to flip locking modes during remount, we
1311 * could add a "local" flag to individual
1312 * flock structures for proper tracking of
1313 * state.
1314 */
1315 if (!is_remount)
1316 mopt->mount_opt |= OCFS2_MOUNT_LOCALFLOCKS;
1317 break;
Joel Beckerb61817e2008-02-01 15:08:23 -08001318 case Opt_stack:
1319 /* Check both that the option we were passed
1320 * is of the right length and that it is a proper
1321 * string of the right length.
1322 */
1323 if (((args[0].to - args[0].from) !=
1324 OCFS2_STACK_LABEL_LEN) ||
1325 (strnlen(args[0].from,
1326 OCFS2_STACK_LABEL_LEN) !=
1327 OCFS2_STACK_LABEL_LEN)) {
1328 mlog(ML_ERROR,
1329 "Invalid cluster_stack option\n");
1330 status = 0;
1331 goto bail;
1332 }
1333 memcpy(mopt->cluster_stack, args[0].from,
1334 OCFS2_STACK_LABEL_LEN);
1335 mopt->cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
1336 break;
Joel Becker12462f12008-09-03 20:03:40 -07001337 case Opt_inode64:
1338 mopt->mount_opt |= OCFS2_MOUNT_INODE64;
1339 break;
Jan Kara19ece542008-08-21 20:13:17 +02001340 case Opt_usrquota:
1341 /* We check only on remount, otherwise features
1342 * aren't yet initialized. */
1343 if (is_remount && !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1344 OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
1345 mlog(ML_ERROR, "User quota requested but "
1346 "filesystem feature is not set\n");
1347 status = 0;
1348 goto bail;
1349 }
1350 mopt->mount_opt |= OCFS2_MOUNT_USRQUOTA;
1351 break;
1352 case Opt_grpquota:
1353 if (is_remount && !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1354 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
1355 mlog(ML_ERROR, "Group quota requested but "
1356 "filesystem feature is not set\n");
1357 status = 0;
1358 goto bail;
1359 }
1360 mopt->mount_opt |= OCFS2_MOUNT_GRPQUOTA;
1361 break;
Tiger Yanga68979b2008-11-14 11:17:52 +08001362#ifdef CONFIG_OCFS2_FS_POSIX_ACL
1363 case Opt_acl:
1364 mopt->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
1365 break;
1366 case Opt_noacl:
1367 mopt->mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
1368 break;
1369#else
1370 case Opt_acl:
1371 case Opt_noacl:
1372 printk(KERN_INFO "ocfs2 (no)acl options not supported\n");
1373 break;
1374#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001375 default:
1376 mlog(ML_ERROR,
1377 "Unrecognized mount option \"%s\" "
1378 "or missing value\n", p);
1379 status = 0;
1380 goto bail;
1381 }
1382 }
1383
1384 status = 1;
1385
1386bail:
1387 mlog_exit(status);
1388 return status;
1389}
1390
Sunil Mushrand5500712007-09-06 13:34:16 -07001391static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
1392{
1393 struct ocfs2_super *osb = OCFS2_SB(mnt->mnt_sb);
1394 unsigned long opts = osb->s_mount_opt;
Mark Fashehebcee4b2008-07-28 14:55:20 -07001395 unsigned int local_alloc_megs;
Sunil Mushrand5500712007-09-06 13:34:16 -07001396
1397 if (opts & OCFS2_MOUNT_HB_LOCAL)
1398 seq_printf(s, ",_netdev,heartbeat=local");
1399 else
1400 seq_printf(s, ",heartbeat=none");
1401
1402 if (opts & OCFS2_MOUNT_NOINTR)
1403 seq_printf(s, ",nointr");
1404
1405 if (opts & OCFS2_MOUNT_DATA_WRITEBACK)
1406 seq_printf(s, ",data=writeback");
1407 else
1408 seq_printf(s, ",data=ordered");
1409
1410 if (opts & OCFS2_MOUNT_BARRIER)
1411 seq_printf(s, ",barrier=1");
1412
1413 if (opts & OCFS2_MOUNT_ERRORS_PANIC)
1414 seq_printf(s, ",errors=panic");
1415 else
1416 seq_printf(s, ",errors=remount-ro");
1417
1418 if (osb->preferred_slot != OCFS2_INVALID_SLOT)
1419 seq_printf(s, ",preferred_slot=%d", osb->preferred_slot);
1420
1421 if (osb->s_atime_quantum != OCFS2_DEFAULT_ATIME_QUANTUM)
1422 seq_printf(s, ",atime_quantum=%u", osb->s_atime_quantum);
1423
Mark Fashehd147b3d2007-11-07 14:40:36 -08001424 if (osb->osb_commit_interval)
1425 seq_printf(s, ",commit=%u",
1426 (unsigned) (osb->osb_commit_interval / HZ));
1427
Mark Fashehebcee4b2008-07-28 14:55:20 -07001428 local_alloc_megs = osb->local_alloc_bits >> (20 - osb->s_clustersize_bits);
1429 if (local_alloc_megs != OCFS2_DEFAULT_LOCAL_ALLOC_SIZE)
1430 seq_printf(s, ",localalloc=%d", local_alloc_megs);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001431
Mark Fasheh53fc6222007-12-20 16:49:04 -08001432 if (opts & OCFS2_MOUNT_LOCALFLOCKS)
1433 seq_printf(s, ",localflocks,");
1434
Joel Beckerb61817e2008-02-01 15:08:23 -08001435 if (osb->osb_cluster_stack[0])
1436 seq_printf(s, ",cluster_stack=%.*s", OCFS2_STACK_LABEL_LEN,
1437 osb->osb_cluster_stack);
Jan Kara19ece542008-08-21 20:13:17 +02001438 if (opts & OCFS2_MOUNT_USRQUOTA)
1439 seq_printf(s, ",usrquota");
1440 if (opts & OCFS2_MOUNT_GRPQUOTA)
1441 seq_printf(s, ",grpquota");
Joel Beckerb61817e2008-02-01 15:08:23 -08001442
Sunil Mushranb0f73cf2008-09-05 11:29:14 -07001443 if (opts & OCFS2_MOUNT_NOUSERXATTR)
1444 seq_printf(s, ",nouser_xattr");
1445 else
1446 seq_printf(s, ",user_xattr");
1447
Joel Becker12462f12008-09-03 20:03:40 -07001448 if (opts & OCFS2_MOUNT_INODE64)
1449 seq_printf(s, ",inode64");
1450
Tiger Yanga68979b2008-11-14 11:17:52 +08001451#ifdef CONFIG_OCFS2_FS_POSIX_ACL
1452 if (opts & OCFS2_MOUNT_POSIX_ACL)
1453 seq_printf(s, ",acl");
1454 else
1455 seq_printf(s, ",noacl");
1456#endif
1457
Sunil Mushrand5500712007-09-06 13:34:16 -07001458 return 0;
1459}
1460
Mark Fashehccd979b2005-12-15 14:31:24 -08001461static int __init ocfs2_init(void)
1462{
1463 int status;
1464
1465 mlog_entry_void();
1466
1467 ocfs2_print_version();
1468
Mark Fashehccd979b2005-12-15 14:31:24 -08001469 status = init_ocfs2_uptodate_cache();
1470 if (status < 0) {
1471 mlog_errno(status);
1472 goto leave;
1473 }
1474
1475 status = ocfs2_initialize_mem_caches();
1476 if (status < 0) {
1477 mlog_errno(status);
1478 goto leave;
1479 }
1480
1481 ocfs2_wq = create_singlethread_workqueue("ocfs2_wq");
1482 if (!ocfs2_wq) {
1483 status = -ENOMEM;
1484 goto leave;
1485 }
1486
Mark Fashehccd979b2005-12-15 14:31:24 -08001487 ocfs2_debugfs_root = debugfs_create_dir("ocfs2", NULL);
1488 if (!ocfs2_debugfs_root) {
1489 status = -EFAULT;
1490 mlog(ML_ERROR, "Unable to create ocfs2 debugfs root.\n");
1491 }
1492
Mark Fasheh171bf932008-10-20 15:36:47 +02001493 status = ocfs2_quota_setup();
1494 if (status)
1495 goto leave;
1496
Joel Becker63e0c482008-01-30 16:58:36 -08001497 ocfs2_set_locking_protocol();
1498
Jan Kara9e33d692008-08-25 19:56:50 +02001499 status = register_quota_format(&ocfs2_quota_format);
Mark Fashehccd979b2005-12-15 14:31:24 -08001500leave:
1501 if (status < 0) {
Mark Fasheh171bf932008-10-20 15:36:47 +02001502 ocfs2_quota_shutdown();
Mark Fashehccd979b2005-12-15 14:31:24 -08001503 ocfs2_free_mem_caches();
1504 exit_ocfs2_uptodate_cache();
Mark Fashehccd979b2005-12-15 14:31:24 -08001505 }
1506
1507 mlog_exit(status);
1508
1509 if (status >= 0) {
1510 return register_filesystem(&ocfs2_fs_type);
1511 } else
1512 return -1;
1513}
1514
1515static void __exit ocfs2_exit(void)
1516{
1517 mlog_entry_void();
1518
Mark Fasheh171bf932008-10-20 15:36:47 +02001519 ocfs2_quota_shutdown();
1520
Mark Fashehccd979b2005-12-15 14:31:24 -08001521 if (ocfs2_wq) {
1522 flush_workqueue(ocfs2_wq);
1523 destroy_workqueue(ocfs2_wq);
1524 }
1525
Jan Kara9e33d692008-08-25 19:56:50 +02001526 unregister_quota_format(&ocfs2_quota_format);
1527
Mark Fashehccd979b2005-12-15 14:31:24 -08001528 debugfs_remove(ocfs2_debugfs_root);
1529
1530 ocfs2_free_mem_caches();
1531
1532 unregister_filesystem(&ocfs2_fs_type);
1533
Mark Fashehccd979b2005-12-15 14:31:24 -08001534 exit_ocfs2_uptodate_cache();
1535
1536 mlog_exit_void();
1537}
1538
1539static void ocfs2_put_super(struct super_block *sb)
1540{
1541 mlog_entry("(0x%p)\n", sb);
1542
Christoph Hellwig6cfd0142009-05-05 15:40:36 +02001543 lock_kernel();
1544
Mark Fashehccd979b2005-12-15 14:31:24 -08001545 ocfs2_sync_blockdev(sb);
1546 ocfs2_dismount_volume(sb, 0);
1547
Christoph Hellwig6cfd0142009-05-05 15:40:36 +02001548 unlock_kernel();
1549
Mark Fashehccd979b2005-12-15 14:31:24 -08001550 mlog_exit_void();
1551}
1552
David Howells726c3342006-06-23 02:02:58 -07001553static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
Mark Fashehccd979b2005-12-15 14:31:24 -08001554{
1555 struct ocfs2_super *osb;
1556 u32 numbits, freebits;
1557 int status;
1558 struct ocfs2_dinode *bm_lock;
1559 struct buffer_head *bh = NULL;
1560 struct inode *inode = NULL;
1561
David Howells726c3342006-06-23 02:02:58 -07001562 mlog_entry("(%p, %p)\n", dentry->d_sb, buf);
Mark Fashehccd979b2005-12-15 14:31:24 -08001563
David Howells726c3342006-06-23 02:02:58 -07001564 osb = OCFS2_SB(dentry->d_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001565
1566 inode = ocfs2_get_system_file_inode(osb,
1567 GLOBAL_BITMAP_SYSTEM_INODE,
1568 OCFS2_INVALID_SLOT);
1569 if (!inode) {
1570 mlog(ML_ERROR, "failed to get bitmap inode\n");
1571 status = -EIO;
1572 goto bail;
1573 }
1574
Mark Fashehe63aecb62007-10-18 15:30:42 -07001575 status = ocfs2_inode_lock(inode, &bh, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001576 if (status < 0) {
1577 mlog_errno(status);
1578 goto bail;
1579 }
1580
1581 bm_lock = (struct ocfs2_dinode *) bh->b_data;
1582
1583 numbits = le32_to_cpu(bm_lock->id1.bitmap1.i_total);
1584 freebits = numbits - le32_to_cpu(bm_lock->id1.bitmap1.i_used);
1585
1586 buf->f_type = OCFS2_SUPER_MAGIC;
David Howells726c3342006-06-23 02:02:58 -07001587 buf->f_bsize = dentry->d_sb->s_blocksize;
Mark Fashehccd979b2005-12-15 14:31:24 -08001588 buf->f_namelen = OCFS2_MAX_FILENAME_LEN;
1589 buf->f_blocks = ((sector_t) numbits) *
1590 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1591 buf->f_bfree = ((sector_t) freebits) *
1592 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1593 buf->f_bavail = buf->f_bfree;
1594 buf->f_files = numbits;
1595 buf->f_ffree = freebits;
1596
1597 brelse(bh);
1598
Mark Fashehe63aecb62007-10-18 15:30:42 -07001599 ocfs2_inode_unlock(inode, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001600 status = 0;
1601bail:
1602 if (inode)
1603 iput(inode);
1604
1605 mlog_exit(status);
1606
1607 return status;
1608}
1609
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001610static void ocfs2_inode_init_once(void *data)
Mark Fashehccd979b2005-12-15 14:31:24 -08001611{
1612 struct ocfs2_inode_info *oi = data;
1613
Christoph Lametera35afb82007-05-16 22:10:57 -07001614 oi->ip_flags = 0;
1615 oi->ip_open_count = 0;
1616 spin_lock_init(&oi->ip_lock);
1617 ocfs2_extent_map_init(&oi->vfs_inode);
1618 INIT_LIST_HEAD(&oi->ip_io_markers);
1619 oi->ip_created_trans = 0;
1620 oi->ip_last_trans = 0;
1621 oi->ip_dir_start_lookup = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001622
Christoph Lametera35afb82007-05-16 22:10:57 -07001623 init_rwsem(&oi->ip_alloc_sem);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001624 init_rwsem(&oi->ip_xattr_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -07001625 mutex_init(&oi->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001626
Christoph Lametera35afb82007-05-16 22:10:57 -07001627 oi->ip_blkno = 0ULL;
1628 oi->ip_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001629
Christoph Lametera35afb82007-05-16 22:10:57 -07001630 ocfs2_lock_res_init_once(&oi->ip_rw_lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001631 ocfs2_lock_res_init_once(&oi->ip_inode_lockres);
Christoph Lametera35afb82007-05-16 22:10:57 -07001632 ocfs2_lock_res_init_once(&oi->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001633
Christoph Lametera35afb82007-05-16 22:10:57 -07001634 ocfs2_metadata_cache_init(&oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001635
Christoph Lametera35afb82007-05-16 22:10:57 -07001636 inode_init_once(&oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001637}
1638
1639static int ocfs2_initialize_mem_caches(void)
1640{
1641 ocfs2_inode_cachep = kmem_cache_create("ocfs2_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001642 sizeof(struct ocfs2_inode_info),
1643 0,
1644 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1645 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001646 ocfs2_inode_init_once);
Jan Kara9e33d692008-08-25 19:56:50 +02001647 ocfs2_dquot_cachep = kmem_cache_create("ocfs2_dquot_cache",
1648 sizeof(struct ocfs2_dquot),
1649 0,
1650 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1651 SLAB_MEM_SPREAD),
1652 NULL);
1653 ocfs2_qf_chunk_cachep = kmem_cache_create("ocfs2_qf_chunk_cache",
1654 sizeof(struct ocfs2_quota_chunk),
1655 0,
1656 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
1657 NULL);
1658 if (!ocfs2_inode_cachep || !ocfs2_dquot_cachep ||
1659 !ocfs2_qf_chunk_cachep) {
1660 if (ocfs2_inode_cachep)
1661 kmem_cache_destroy(ocfs2_inode_cachep);
1662 if (ocfs2_dquot_cachep)
1663 kmem_cache_destroy(ocfs2_dquot_cachep);
1664 if (ocfs2_qf_chunk_cachep)
1665 kmem_cache_destroy(ocfs2_qf_chunk_cachep);
Mark Fashehccd979b2005-12-15 14:31:24 -08001666 return -ENOMEM;
Jan Kara9e33d692008-08-25 19:56:50 +02001667 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001668
Mark Fashehccd979b2005-12-15 14:31:24 -08001669 return 0;
1670}
1671
1672static void ocfs2_free_mem_caches(void)
1673{
1674 if (ocfs2_inode_cachep)
1675 kmem_cache_destroy(ocfs2_inode_cachep);
Mark Fashehccd979b2005-12-15 14:31:24 -08001676 ocfs2_inode_cachep = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +02001677
1678 if (ocfs2_dquot_cachep)
1679 kmem_cache_destroy(ocfs2_dquot_cachep);
1680 ocfs2_dquot_cachep = NULL;
1681
1682 if (ocfs2_qf_chunk_cachep)
1683 kmem_cache_destroy(ocfs2_qf_chunk_cachep);
1684 ocfs2_qf_chunk_cachep = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001685}
1686
1687static int ocfs2_get_sector(struct super_block *sb,
1688 struct buffer_head **bh,
1689 int block,
1690 int sect_size)
1691{
1692 if (!sb_set_blocksize(sb, sect_size)) {
1693 mlog(ML_ERROR, "unable to set blocksize\n");
1694 return -EIO;
1695 }
1696
1697 *bh = sb_getblk(sb, block);
1698 if (!*bh) {
1699 mlog_errno(-EIO);
1700 return -EIO;
1701 }
1702 lock_buffer(*bh);
1703 if (!buffer_dirty(*bh))
1704 clear_buffer_uptodate(*bh);
1705 unlock_buffer(*bh);
1706 ll_rw_block(READ, 1, bh);
1707 wait_on_buffer(*bh);
wengang wang28d57d42009-02-13 10:11:47 +08001708 if (!buffer_uptodate(*bh)) {
1709 mlog_errno(-EIO);
1710 brelse(*bh);
1711 *bh = NULL;
1712 return -EIO;
1713 }
1714
Mark Fashehccd979b2005-12-15 14:31:24 -08001715 return 0;
1716}
1717
Mark Fashehccd979b2005-12-15 14:31:24 -08001718static int ocfs2_mount_volume(struct super_block *sb)
1719{
1720 int status = 0;
1721 int unlock_super = 0;
1722 struct ocfs2_super *osb = OCFS2_SB(sb);
1723
1724 mlog_entry_void();
1725
1726 if (ocfs2_is_hard_readonly(osb))
1727 goto leave;
1728
Mark Fashehccd979b2005-12-15 14:31:24 -08001729 status = ocfs2_dlm_init(osb);
1730 if (status < 0) {
1731 mlog_errno(status);
1732 goto leave;
1733 }
1734
Mark Fashehccd979b2005-12-15 14:31:24 -08001735 status = ocfs2_super_lock(osb, 1);
1736 if (status < 0) {
1737 mlog_errno(status);
1738 goto leave;
1739 }
1740 unlock_super = 1;
1741
1742 /* This will load up the node map and add ourselves to it. */
1743 status = ocfs2_find_slot(osb);
1744 if (status < 0) {
1745 mlog_errno(status);
1746 goto leave;
1747 }
1748
Mark Fashehccd979b2005-12-15 14:31:24 -08001749 /* load all node-local system inodes */
1750 status = ocfs2_init_local_system_inodes(osb);
1751 if (status < 0) {
1752 mlog_errno(status);
1753 goto leave;
1754 }
1755
1756 status = ocfs2_check_volume(osb);
1757 if (status < 0) {
1758 mlog_errno(status);
1759 goto leave;
1760 }
1761
1762 status = ocfs2_truncate_log_init(osb);
1763 if (status < 0) {
1764 mlog_errno(status);
1765 goto leave;
1766 }
1767
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001768 if (ocfs2_mount_local(osb))
1769 goto leave;
1770
Mark Fashehccd979b2005-12-15 14:31:24 -08001771leave:
1772 if (unlock_super)
1773 ocfs2_super_unlock(osb, 1);
1774
1775 mlog_exit(status);
1776 return status;
1777}
1778
Mark Fashehccd979b2005-12-15 14:31:24 -08001779static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
1780{
Joel Becker286eaa92008-02-01 15:03:57 -08001781 int tmp, hangup_needed = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001782 struct ocfs2_super *osb = NULL;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001783 char nodestr[8];
Mark Fashehccd979b2005-12-15 14:31:24 -08001784
1785 mlog_entry("(0x%p)\n", sb);
1786
1787 BUG_ON(!sb);
1788 osb = OCFS2_SB(sb);
1789 BUG_ON(!osb);
1790
Sunil Mushran50397502008-12-17 14:17:43 -08001791 debugfs_remove(osb->osb_ctxt);
1792
Jan Kara19ece542008-08-21 20:13:17 +02001793 ocfs2_disable_quotas(osb);
1794
Mark Fashehccd979b2005-12-15 14:31:24 -08001795 ocfs2_shutdown_local_alloc(osb);
1796
1797 ocfs2_truncate_log_shutdown(osb);
1798
Joel Becker553abd02008-02-01 12:03:57 -08001799 /* This will disable recovery and flush any recovery work. */
1800 ocfs2_recovery_exit(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001801
1802 ocfs2_journal_shutdown(osb);
1803
1804 ocfs2_sync_blockdev(sb);
1805
Joel Becker4670c462008-02-01 14:39:35 -08001806 /* No cluster connection means we've failed during mount, so skip
1807 * all the steps which depended on that to complete. */
1808 if (osb->cconn) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001809 tmp = ocfs2_super_lock(osb, 1);
1810 if (tmp < 0) {
1811 mlog_errno(tmp);
1812 return;
1813 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001814 }
1815
Mark Fasheh19b613d2007-10-04 14:47:09 -07001816 if (osb->slot_num != OCFS2_INVALID_SLOT)
1817 ocfs2_put_slot(osb);
1818
Joel Becker4670c462008-02-01 14:39:35 -08001819 if (osb->cconn)
Mark Fasheh19b613d2007-10-04 14:47:09 -07001820 ocfs2_super_unlock(osb, 1);
1821
Mark Fashehccd979b2005-12-15 14:31:24 -08001822 ocfs2_release_system_inodes(osb);
1823
Joel Becker6953b4c2008-01-29 16:59:56 -08001824 /*
Joel Becker6953b4c2008-01-29 16:59:56 -08001825 * If we're dismounting due to mount error, mount.ocfs2 will clean
1826 * up heartbeat. If we're a local mount, there is no heartbeat.
1827 * If we failed before we got a uuid_str yet, we can't stop
1828 * heartbeat. Otherwise, do it.
1829 */
1830 if (!mnt_err && !ocfs2_mount_local(osb) && osb->uuid_str)
Joel Becker286eaa92008-02-01 15:03:57 -08001831 hangup_needed = 1;
1832
1833 if (osb->cconn)
1834 ocfs2_dlm_shutdown(osb, hangup_needed);
1835
1836 debugfs_remove(osb->osb_debug_root);
1837
1838 if (hangup_needed)
Joel Becker6953b4c2008-01-29 16:59:56 -08001839 ocfs2_cluster_hangup(osb->uuid_str, strlen(osb->uuid_str));
Mark Fashehccd979b2005-12-15 14:31:24 -08001840
1841 atomic_set(&osb->vol_state, VOLUME_DISMOUNTED);
1842
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001843 if (ocfs2_mount_local(osb))
1844 snprintf(nodestr, sizeof(nodestr), "local");
1845 else
Joel Becker19fdb622008-01-30 15:38:24 -08001846 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001847
1848 printk(KERN_INFO "ocfs2: Unmounting device (%s) on (node %s)\n",
1849 osb->dev_str, nodestr);
Mark Fashehccd979b2005-12-15 14:31:24 -08001850
1851 ocfs2_delete_osb(osb);
1852 kfree(osb);
1853 sb->s_dev = 0;
1854 sb->s_fs_info = NULL;
1855}
1856
1857static int ocfs2_setup_osb_uuid(struct ocfs2_super *osb, const unsigned char *uuid,
1858 unsigned uuid_bytes)
1859{
1860 int i, ret;
1861 char *ptr;
1862
1863 BUG_ON(uuid_bytes != OCFS2_VOL_UUID_LEN);
1864
Robert P. J. Daycd861282006-12-13 00:34:52 -08001865 osb->uuid_str = kzalloc(OCFS2_VOL_UUID_LEN * 2 + 1, GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001866 if (osb->uuid_str == NULL)
1867 return -ENOMEM;
1868
Mark Fashehccd979b2005-12-15 14:31:24 -08001869 for (i = 0, ptr = osb->uuid_str; i < OCFS2_VOL_UUID_LEN; i++) {
1870 /* print with null */
1871 ret = snprintf(ptr, 3, "%02X", uuid[i]);
1872 if (ret != 2) /* drop super cleans up */
1873 return -EINVAL;
1874 /* then only advance past the last char */
1875 ptr += 2;
1876 }
1877
1878 return 0;
1879}
1880
1881static int ocfs2_initialize_super(struct super_block *sb,
1882 struct buffer_head *bh,
1883 int sector_size)
1884{
Denis Chengbddb8eb32007-09-27 02:10:04 +08001885 int status;
Mark Fasheh5a254032007-07-20 12:56:16 -07001886 int i, cbits, bbits;
1887 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001888 struct inode *inode = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001889 struct ocfs2_journal *journal;
1890 __le32 uuid_net_key;
1891 struct ocfs2_super *osb;
1892
1893 mlog_entry_void();
1894
Robert P. J. Daycd861282006-12-13 00:34:52 -08001895 osb = kzalloc(sizeof(struct ocfs2_super), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001896 if (!osb) {
1897 status = -ENOMEM;
1898 mlog_errno(status);
1899 goto bail;
1900 }
1901
1902 sb->s_fs_info = osb;
1903 sb->s_op = &ocfs2_sops;
1904 sb->s_export_op = &ocfs2_export_ops;
Jan Kara19ece542008-08-21 20:13:17 +02001905 sb->s_qcop = &ocfs2_quotactl_ops;
1906 sb->dq_op = &ocfs2_quota_operations;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001907 sb->s_xattr = ocfs2_xattr_handlers;
Mark Fashehe0dceaf2007-08-09 16:52:30 -07001908 sb->s_time_gran = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001909 sb->s_flags |= MS_NOATIME;
1910 /* this is needed to support O_LARGEFILE */
Mark Fasheh5a254032007-07-20 12:56:16 -07001911 cbits = le32_to_cpu(di->id2.i_super.s_clustersize_bits);
1912 bbits = le32_to_cpu(di->id2.i_super.s_blocksize_bits);
1913 sb->s_maxbytes = ocfs2_max_file_offset(bbits, cbits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001914
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001915 osb->osb_dx_mask = (1 << (cbits - bbits)) - 1;
1916
1917 for (i = 0; i < 3; i++)
1918 osb->osb_dx_seed[i] = le32_to_cpu(di->id2.i_super.s_dx_seed[i]);
1919 osb->osb_dx_seed[3] = le32_to_cpu(di->id2.i_super.s_uuid_hash);
1920
Mark Fashehccd979b2005-12-15 14:31:24 -08001921 osb->sb = sb;
1922 /* Save off for ocfs2_rw_direct */
1923 osb->s_sectsize_bits = blksize_bits(sector_size);
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01001924 BUG_ON(!osb->s_sectsize_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001925
Mark Fasheh34d024f2007-09-24 15:56:19 -07001926 spin_lock_init(&osb->dc_task_lock);
1927 init_waitqueue_head(&osb->dc_event);
1928 osb->dc_work_sequence = 0;
1929 osb->dc_wake_sequence = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001930 INIT_LIST_HEAD(&osb->blocked_lock_list);
1931 osb->blocked_lock_count = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001932 spin_lock_init(&osb->osb_lock);
Tao Mac8b9cf92009-02-24 17:40:26 -08001933 spin_lock_init(&osb->osb_xattr_lock);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001934 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001935
1936 atomic_set(&osb->alloc_stats.moves, 0);
1937 atomic_set(&osb->alloc_stats.local_data, 0);
1938 atomic_set(&osb->alloc_stats.bitmap_data, 0);
1939 atomic_set(&osb->alloc_stats.bg_allocs, 0);
1940 atomic_set(&osb->alloc_stats.bg_extends, 0);
1941
1942 ocfs2_init_node_maps(osb);
1943
1944 snprintf(osb->dev_str, sizeof(osb->dev_str), "%u,%u",
1945 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1946
Joel Becker553abd02008-02-01 12:03:57 -08001947 status = ocfs2_recovery_init(osb);
1948 if (status) {
1949 mlog(ML_ERROR, "Unable to initialize recovery state\n");
1950 mlog_errno(status);
1951 goto bail;
1952 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001953
1954 init_waitqueue_head(&osb->checkpoint_event);
1955 atomic_set(&osb->needs_checkpoint, 0);
1956
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001957 osb->s_atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
1958
Mark Fashehccd979b2005-12-15 14:31:24 -08001959 osb->slot_num = OCFS2_INVALID_SLOT;
1960
Tiger Yang8154da32008-08-18 17:11:46 +08001961 osb->s_xattr_inline_size = le16_to_cpu(
1962 di->id2.i_super.s_xattr_inline_size);
Tiger Yangfdd77702008-08-18 17:08:55 +08001963
Mark Fashehccd979b2005-12-15 14:31:24 -08001964 osb->local_alloc_state = OCFS2_LA_UNUSED;
1965 osb->local_alloc_bh = NULL;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001966 INIT_DELAYED_WORK(&osb->la_enable_wq, ocfs2_la_enable_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08001967
Mark Fashehccd979b2005-12-15 14:31:24 -08001968 init_waitqueue_head(&osb->osb_mount_event);
1969
1970 osb->vol_label = kmalloc(OCFS2_MAX_VOL_LABEL_LEN, GFP_KERNEL);
1971 if (!osb->vol_label) {
1972 mlog(ML_ERROR, "unable to alloc vol label\n");
1973 status = -ENOMEM;
1974 goto bail;
1975 }
1976
Mark Fashehccd979b2005-12-15 14:31:24 -08001977 osb->max_slots = le16_to_cpu(di->id2.i_super.s_max_slots);
1978 if (osb->max_slots > OCFS2_MAX_SLOTS || osb->max_slots == 0) {
1979 mlog(ML_ERROR, "Invalid number of node slots (%u)\n",
1980 osb->max_slots);
1981 status = -EINVAL;
1982 goto bail;
1983 }
Sunil Mushran781ee3e2006-04-27 16:41:31 -07001984 mlog(0, "max_slots for this device: %u\n", osb->max_slots);
Mark Fashehccd979b2005-12-15 14:31:24 -08001985
Sunil Mushran539d8262008-07-14 17:31:10 -07001986 osb->slot_recovery_generations =
1987 kcalloc(osb->max_slots, sizeof(*osb->slot_recovery_generations),
1988 GFP_KERNEL);
1989 if (!osb->slot_recovery_generations) {
1990 status = -ENOMEM;
1991 mlog_errno(status);
1992 goto bail;
1993 }
1994
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001995 init_waitqueue_head(&osb->osb_wipe_event);
1996 osb->osb_orphan_wipes = kcalloc(osb->max_slots,
1997 sizeof(*osb->osb_orphan_wipes),
1998 GFP_KERNEL);
1999 if (!osb->osb_orphan_wipes) {
2000 status = -ENOMEM;
2001 mlog_errno(status);
2002 goto bail;
2003 }
2004
Mark Fashehccd979b2005-12-15 14:31:24 -08002005 osb->s_feature_compat =
2006 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_compat);
2007 osb->s_feature_ro_compat =
2008 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_ro_compat);
2009 osb->s_feature_incompat =
2010 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_incompat);
2011
2012 if ((i = OCFS2_HAS_INCOMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_INCOMPAT_SUPP))) {
2013 mlog(ML_ERROR, "couldn't mount because of unsupported "
2014 "optional features (%x).\n", i);
2015 status = -EINVAL;
2016 goto bail;
2017 }
2018 if (!(osb->sb->s_flags & MS_RDONLY) &&
2019 (i = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP))) {
2020 mlog(ML_ERROR, "couldn't mount RDWR because of "
2021 "unsupported optional features (%x).\n", i);
2022 status = -EINVAL;
2023 goto bail;
2024 }
2025
Joel Beckerb61817e2008-02-01 15:08:23 -08002026 if (ocfs2_userspace_stack(osb)) {
2027 memcpy(osb->osb_cluster_stack,
2028 OCFS2_RAW_SB(di)->s_cluster_info.ci_stack,
2029 OCFS2_STACK_LABEL_LEN);
2030 osb->osb_cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
2031 if (strlen(osb->osb_cluster_stack) != OCFS2_STACK_LABEL_LEN) {
2032 mlog(ML_ERROR,
2033 "couldn't mount because of an invalid "
2034 "cluster stack label (%s) \n",
2035 osb->osb_cluster_stack);
2036 status = -EINVAL;
2037 goto bail;
2038 }
2039 } else {
2040 /* The empty string is identical with classic tools that
2041 * don't know about s_cluster_info. */
2042 osb->osb_cluster_stack[0] = '\0';
2043 }
2044
Mark Fashehccd979b2005-12-15 14:31:24 -08002045 get_random_bytes(&osb->s_next_generation, sizeof(u32));
2046
2047 /* FIXME
2048 * This should be done in ocfs2_journal_init(), but unknown
2049 * ordering issues will cause the filesystem to crash.
2050 * If anyone wants to figure out what part of the code
2051 * refers to osb->journal before ocfs2_journal_init() is run,
2052 * be my guest.
2053 */
2054 /* initialize our journal structure */
2055
Robert P. J. Daycd861282006-12-13 00:34:52 -08002056 journal = kzalloc(sizeof(struct ocfs2_journal), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08002057 if (!journal) {
2058 mlog(ML_ERROR, "unable to alloc journal\n");
2059 status = -ENOMEM;
2060 goto bail;
2061 }
2062 osb->journal = journal;
2063 journal->j_osb = osb;
2064
2065 atomic_set(&journal->j_num_trans, 0);
2066 init_rwsem(&journal->j_trans_barrier);
2067 init_waitqueue_head(&journal->j_checkpointed);
2068 spin_lock_init(&journal->j_lock);
2069 journal->j_trans_id = (unsigned long) 1;
2070 INIT_LIST_HEAD(&journal->j_la_cleanups);
David Howellsc4028952006-11-22 14:57:56 +00002071 INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
Mark Fashehccd979b2005-12-15 14:31:24 -08002072 journal->j_state = OCFS2_JOURNAL_FREE;
2073
Jan Karaea455f82009-01-12 23:20:31 +01002074 INIT_WORK(&osb->dentry_lock_work, ocfs2_drop_dl_inodes);
2075 osb->dentry_lock_list = NULL;
2076
Mark Fashehccd979b2005-12-15 14:31:24 -08002077 /* get some pseudo constants for clustersize bits */
2078 osb->s_clustersize_bits =
2079 le32_to_cpu(di->id2.i_super.s_clustersize_bits);
2080 osb->s_clustersize = 1 << osb->s_clustersize_bits;
2081 mlog(0, "clusterbits=%d\n", osb->s_clustersize_bits);
2082
2083 if (osb->s_clustersize < OCFS2_MIN_CLUSTERSIZE ||
2084 osb->s_clustersize > OCFS2_MAX_CLUSTERSIZE) {
2085 mlog(ML_ERROR, "Volume has invalid cluster size (%d)\n",
2086 osb->s_clustersize);
2087 status = -EINVAL;
2088 goto bail;
2089 }
2090
2091 if (ocfs2_clusters_to_blocks(osb->sb, le32_to_cpu(di->i_clusters) - 1)
2092 > (u32)~0UL) {
2093 mlog(ML_ERROR, "Volume might try to write to blocks beyond "
2094 "what jbd can address in 32 bits.\n");
2095 status = -EINVAL;
2096 goto bail;
2097 }
2098
2099 if (ocfs2_setup_osb_uuid(osb, di->id2.i_super.s_uuid,
2100 sizeof(di->id2.i_super.s_uuid))) {
2101 mlog(ML_ERROR, "Out of memory trying to setup our uuid.\n");
2102 status = -ENOMEM;
2103 goto bail;
2104 }
2105
Mark Fasheh78427042006-05-04 12:03:26 -07002106 memcpy(&uuid_net_key, di->id2.i_super.s_uuid, sizeof(uuid_net_key));
Mark Fashehccd979b2005-12-15 14:31:24 -08002107
2108 strncpy(osb->vol_label, di->id2.i_super.s_label, 63);
2109 osb->vol_label[63] = '\0';
2110 osb->root_blkno = le64_to_cpu(di->id2.i_super.s_root_blkno);
2111 osb->system_dir_blkno = le64_to_cpu(di->id2.i_super.s_system_dir_blkno);
2112 osb->first_cluster_group_blkno =
2113 le64_to_cpu(di->id2.i_super.s_first_cluster_group);
2114 osb->fs_generation = le32_to_cpu(di->i_fs_generation);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002115 osb->uuid_hash = le32_to_cpu(di->id2.i_super.s_uuid_hash);
Mark Fashehccd979b2005-12-15 14:31:24 -08002116 mlog(0, "vol_label: %s\n", osb->vol_label);
2117 mlog(0, "uuid: %s\n", osb->uuid_str);
Mark Fashehb0697052006-03-03 10:24:33 -08002118 mlog(0, "root_blkno=%llu, system_dir_blkno=%llu\n",
2119 (unsigned long long)osb->root_blkno,
2120 (unsigned long long)osb->system_dir_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002121
2122 osb->osb_dlm_debug = ocfs2_new_dlm_debug();
2123 if (!osb->osb_dlm_debug) {
2124 status = -ENOMEM;
2125 mlog_errno(status);
2126 goto bail;
2127 }
2128
2129 atomic_set(&osb->vol_state, VOLUME_INIT);
2130
2131 /* load root, system_dir, and all global system inodes */
2132 status = ocfs2_init_global_system_inodes(osb);
2133 if (status < 0) {
2134 mlog_errno(status);
2135 goto bail;
2136 }
2137
2138 /*
2139 * global bitmap
2140 */
2141 inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
2142 OCFS2_INVALID_SLOT);
2143 if (!inode) {
2144 status = -EINVAL;
2145 mlog_errno(status);
2146 goto bail;
2147 }
2148
2149 osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08002150 iput(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08002151
Tao Mae9d578a2007-12-18 15:46:10 +08002152 osb->bitmap_cpg = ocfs2_group_bitmap_size(sb) * 8;
Mark Fashehccd979b2005-12-15 14:31:24 -08002153
2154 status = ocfs2_init_slot_info(osb);
2155 if (status < 0) {
2156 mlog_errno(status);
2157 goto bail;
2158 }
2159
Mark Fashehccd979b2005-12-15 14:31:24 -08002160bail:
2161 mlog_exit(status);
2162 return status;
2163}
2164
2165/*
2166 * will return: -EAGAIN if it is ok to keep searching for superblocks
2167 * -EINVAL if there is a bad superblock
2168 * 0 on success
2169 */
2170static int ocfs2_verify_volume(struct ocfs2_dinode *di,
2171 struct buffer_head *bh,
2172 u32 blksz)
2173{
2174 int status = -EAGAIN;
2175
2176 mlog_entry_void();
2177
2178 if (memcmp(di->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
2179 strlen(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) {
Joel Beckerd030cc92008-12-11 15:04:14 -08002180 /* We have to do a raw check of the feature here */
2181 if (le32_to_cpu(di->id2.i_super.s_feature_incompat) &
2182 OCFS2_FEATURE_INCOMPAT_META_ECC) {
2183 status = ocfs2_block_check_validate(bh->b_data,
2184 bh->b_size,
2185 &di->i_check);
2186 if (status)
2187 goto out;
2188 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002189 status = -EINVAL;
2190 if ((1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits)) != blksz) {
2191 mlog(ML_ERROR, "found superblock with incorrect block "
2192 "size: found %u, should be %u\n",
2193 1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits),
2194 blksz);
2195 } else if (le16_to_cpu(di->id2.i_super.s_major_rev_level) !=
2196 OCFS2_MAJOR_REV_LEVEL ||
2197 le16_to_cpu(di->id2.i_super.s_minor_rev_level) !=
2198 OCFS2_MINOR_REV_LEVEL) {
2199 mlog(ML_ERROR, "found superblock with bad version: "
2200 "found %u.%u, should be %u.%u\n",
2201 le16_to_cpu(di->id2.i_super.s_major_rev_level),
2202 le16_to_cpu(di->id2.i_super.s_minor_rev_level),
2203 OCFS2_MAJOR_REV_LEVEL,
2204 OCFS2_MINOR_REV_LEVEL);
2205 } else if (bh->b_blocknr != le64_to_cpu(di->i_blkno)) {
2206 mlog(ML_ERROR, "bad block number on superblock: "
Mark Fashehb0697052006-03-03 10:24:33 -08002207 "found %llu, should be %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07002208 (unsigned long long)le64_to_cpu(di->i_blkno),
Mark Fashehb0697052006-03-03 10:24:33 -08002209 (unsigned long long)bh->b_blocknr);
Mark Fashehccd979b2005-12-15 14:31:24 -08002210 } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
2211 le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
2212 mlog(ML_ERROR, "bad cluster size found: %u\n",
2213 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
2214 } else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
2215 mlog(ML_ERROR, "bad root_blkno: 0\n");
2216 } else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
2217 mlog(ML_ERROR, "bad system_dir_blkno: 0\n");
2218 } else if (le16_to_cpu(di->id2.i_super.s_max_slots) > OCFS2_MAX_SLOTS) {
2219 mlog(ML_ERROR,
2220 "Superblock slots found greater than file system "
2221 "maximum: found %u, max %u\n",
2222 le16_to_cpu(di->id2.i_super.s_max_slots),
2223 OCFS2_MAX_SLOTS);
2224 } else {
2225 /* found it! */
2226 status = 0;
2227 }
2228 }
2229
Joel Beckerd030cc92008-12-11 15:04:14 -08002230out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002231 mlog_exit(status);
2232 return status;
2233}
2234
2235static int ocfs2_check_volume(struct ocfs2_super *osb)
2236{
Denis Chengbddb8eb32007-09-27 02:10:04 +08002237 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08002238 int dirty;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002239 int local;
Mark Fashehccd979b2005-12-15 14:31:24 -08002240 struct ocfs2_dinode *local_alloc = NULL; /* only used if we
2241 * recover
2242 * ourselves. */
2243
2244 mlog_entry_void();
2245
2246 /* Init our journal object. */
2247 status = ocfs2_journal_init(osb->journal, &dirty);
2248 if (status < 0) {
2249 mlog(ML_ERROR, "Could not initialize journal!\n");
2250 goto finally;
2251 }
2252
2253 /* If the journal was unmounted cleanly then we don't want to
2254 * recover anything. Otherwise, journal_load will do that
2255 * dirty work for us :) */
2256 if (!dirty) {
2257 status = ocfs2_journal_wipe(osb->journal, 0);
2258 if (status < 0) {
2259 mlog_errno(status);
2260 goto finally;
2261 }
2262 } else {
2263 mlog(ML_NOTICE, "File system was not unmounted cleanly, "
2264 "recovering volume.\n");
2265 }
2266
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002267 local = ocfs2_mount_local(osb);
2268
Mark Fashehccd979b2005-12-15 14:31:24 -08002269 /* will play back anything left in the journal. */
Sunil Mushran539d8262008-07-14 17:31:10 -07002270 status = ocfs2_journal_load(osb->journal, local, dirty);
Wengang Wang01af4822008-06-10 14:24:48 +08002271 if (status < 0) {
2272 mlog(ML_ERROR, "ocfs2 journal load failed! %d\n", status);
2273 goto finally;
2274 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002275
2276 if (dirty) {
2277 /* recover my local alloc if we didn't unmount cleanly. */
2278 status = ocfs2_begin_local_alloc_recovery(osb,
2279 osb->slot_num,
2280 &local_alloc);
2281 if (status < 0) {
2282 mlog_errno(status);
2283 goto finally;
2284 }
2285 /* we complete the recovery process after we've marked
2286 * ourselves as mounted. */
2287 }
2288
2289 mlog(0, "Journal loaded.\n");
2290
2291 status = ocfs2_load_local_alloc(osb);
2292 if (status < 0) {
2293 mlog_errno(status);
2294 goto finally;
2295 }
2296
2297 if (dirty) {
2298 /* Recovery will be completed after we've mounted the
2299 * rest of the volume. */
2300 osb->dirty = 1;
2301 osb->local_alloc_copy = local_alloc;
2302 local_alloc = NULL;
2303 }
2304
2305 /* go through each journal, trylock it and if you get the
2306 * lock, and it's marked as dirty, set the bit in the recover
2307 * map and launch a recovery thread for it. */
2308 status = ocfs2_mark_dead_nodes(osb);
Srinivas Eeda9140db02009-03-06 14:21:46 -08002309 if (status < 0) {
2310 mlog_errno(status);
2311 goto finally;
2312 }
2313
2314 status = ocfs2_compute_replay_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002315 if (status < 0)
2316 mlog_errno(status);
2317
2318finally:
2319 if (local_alloc)
2320 kfree(local_alloc);
2321
2322 mlog_exit(status);
2323 return status;
2324}
2325
2326/*
2327 * The routine gets called from dismount or close whenever a dismount on
2328 * volume is requested and the osb open count becomes 1.
2329 * It will remove the osb from the global list and also free up all the
2330 * initialized resources and fileobject.
2331 */
2332static void ocfs2_delete_osb(struct ocfs2_super *osb)
2333{
2334 mlog_entry_void();
2335
2336 /* This function assumes that the caller has the main osb resource */
2337
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002338 ocfs2_free_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002339
Mark Fashehb4df6ed2006-02-22 17:35:08 -08002340 kfree(osb->osb_orphan_wipes);
Sunil Mushran539d8262008-07-14 17:31:10 -07002341 kfree(osb->slot_recovery_generations);
Mark Fashehccd979b2005-12-15 14:31:24 -08002342 /* FIXME
2343 * This belongs in journal shutdown, but because we have to
2344 * allocate osb->journal at the start of ocfs2_initalize_osb(),
2345 * we free it here.
2346 */
2347 kfree(osb->journal);
2348 if (osb->local_alloc_copy)
2349 kfree(osb->local_alloc_copy);
2350 kfree(osb->uuid_str);
2351 ocfs2_put_dlm_debug(osb->osb_dlm_debug);
2352 memset(osb, 0, sizeof(struct ocfs2_super));
2353
2354 mlog_exit_void();
2355}
2356
2357/* Put OCFS2 into a readonly state, or (if the user specifies it),
2358 * panic(). We do not support continue-on-error operation. */
2359static void ocfs2_handle_error(struct super_block *sb)
2360{
2361 struct ocfs2_super *osb = OCFS2_SB(sb);
2362
2363 if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_PANIC)
2364 panic("OCFS2: (device %s): panic forced after error\n",
2365 sb->s_id);
2366
2367 ocfs2_set_osb_flag(osb, OCFS2_OSB_ERROR_FS);
2368
2369 if (sb->s_flags & MS_RDONLY &&
2370 (ocfs2_is_soft_readonly(osb) ||
2371 ocfs2_is_hard_readonly(osb)))
2372 return;
2373
2374 printk(KERN_CRIT "File system is now read-only due to the potential "
2375 "of on-disk corruption. Please run fsck.ocfs2 once the file "
2376 "system is unmounted.\n");
2377 sb->s_flags |= MS_RDONLY;
2378 ocfs2_set_ro_flag(osb, 0);
2379}
2380
2381static char error_buf[1024];
2382
2383void __ocfs2_error(struct super_block *sb,
2384 const char *function,
2385 const char *fmt, ...)
2386{
2387 va_list args;
2388
2389 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002390 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Mark Fashehccd979b2005-12-15 14:31:24 -08002391 va_end(args);
2392
2393 /* Not using mlog here because we want to show the actual
2394 * function the error came from. */
2395 printk(KERN_CRIT "OCFS2: ERROR (device %s): %s: %s\n",
2396 sb->s_id, function, error_buf);
2397
2398 ocfs2_handle_error(sb);
2399}
2400
2401/* Handle critical errors. This is intentionally more drastic than
2402 * ocfs2_handle_error, so we only use for things like journal errors,
2403 * etc. */
2404void __ocfs2_abort(struct super_block* sb,
2405 const char *function,
2406 const char *fmt, ...)
2407{
2408 va_list args;
2409
2410 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002411 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Mark Fashehccd979b2005-12-15 14:31:24 -08002412 va_end(args);
2413
2414 printk(KERN_CRIT "OCFS2: abort (device %s): %s: %s\n",
2415 sb->s_id, function, error_buf);
2416
2417 /* We don't have the cluster support yet to go straight to
2418 * hard readonly in here. Until then, we want to keep
2419 * ocfs2_abort() so that we can at least mark critical
2420 * errors.
2421 *
2422 * TODO: This should abort the journal and alert other nodes
2423 * that our slot needs recovery. */
2424
2425 /* Force a panic(). This stinks, but it's better than letting
2426 * things continue without having a proper hard readonly
2427 * here. */
2428 OCFS2_SB(sb)->s_mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
2429 ocfs2_handle_error(sb);
2430}
2431
2432module_init(ocfs2_init);
2433module_exit(ocfs2_exit);