blob: 4e6b8e7c423d9552386c3489ff154f412dff1319 [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"
Joel Beckerd030cc92008-12-11 15:04:14 -080055#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080056#include "dlmglue.h"
57#include "export.h"
58#include "extent_map.h"
59#include "heartbeat.h"
60#include "inode.h"
61#include "journal.h"
62#include "localalloc.h"
63#include "namei.h"
64#include "slot_map.h"
65#include "super.h"
66#include "sysfile.h"
67#include "uptodate.h"
68#include "ver.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080069#include "xattr.h"
Jan Kara9e33d692008-08-25 19:56:50 +020070#include "quota.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080071
72#include "buffer_head_io.h"
73
Christoph Lametere18b8902006-12-06 20:33:20 -080074static struct kmem_cache *ocfs2_inode_cachep = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +020075struct kmem_cache *ocfs2_dquot_cachep;
76struct kmem_cache *ocfs2_qf_chunk_cachep;
Mark Fashehccd979b2005-12-15 14:31:24 -080077
Mark Fashehccd979b2005-12-15 14:31:24 -080078/* OCFS2 needs to schedule several differnt types of work which
79 * require cluster locking, disk I/O, recovery waits, etc. Since these
80 * types of work tend to be heavy we avoid using the kernel events
81 * workqueue and schedule on our own. */
82struct workqueue_struct *ocfs2_wq = NULL;
83
84static struct dentry *ocfs2_debugfs_root = NULL;
85
86MODULE_AUTHOR("Oracle");
87MODULE_LICENSE("GPL");
88
Tiger Yangc0123ad2007-09-08 00:16:10 +080089struct mount_options
90{
Mark Fashehd147b3d2007-11-07 14:40:36 -080091 unsigned long commit_interval;
Tiger Yangc0123ad2007-09-08 00:16:10 +080092 unsigned long mount_opt;
93 unsigned int atime_quantum;
94 signed short slot;
Sunil Mushran2fbe8d12007-12-20 14:58:11 -080095 unsigned int localalloc_opt;
Joel Beckerb61817e2008-02-01 15:08:23 -080096 char cluster_stack[OCFS2_STACK_LABEL_LEN + 1];
Tiger Yangc0123ad2007-09-08 00:16:10 +080097};
98
Mark Fashehccd979b2005-12-15 14:31:24 -080099static int ocfs2_parse_options(struct super_block *sb, char *options,
Tiger Yangc0123ad2007-09-08 00:16:10 +0800100 struct mount_options *mopt,
Sunil Mushranbaf46612007-06-18 17:00:24 -0700101 int is_remount);
Sunil Mushrand5500712007-09-06 13:34:16 -0700102static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt);
Mark Fashehccd979b2005-12-15 14:31:24 -0800103static void ocfs2_put_super(struct super_block *sb);
104static int ocfs2_mount_volume(struct super_block *sb);
105static int ocfs2_remount(struct super_block *sb, int *flags, char *data);
106static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err);
107static int ocfs2_initialize_mem_caches(void);
108static void ocfs2_free_mem_caches(void);
109static void ocfs2_delete_osb(struct ocfs2_super *osb);
110
David Howells726c3342006-06-23 02:02:58 -0700111static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf);
Mark Fashehccd979b2005-12-15 14:31:24 -0800112
113static int ocfs2_sync_fs(struct super_block *sb, int wait);
114
115static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb);
116static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb);
Denis Chengbddb8eb32007-09-27 02:10:04 +0800117static void ocfs2_release_system_inodes(struct ocfs2_super *osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800118static int ocfs2_check_volume(struct ocfs2_super *osb);
119static int ocfs2_verify_volume(struct ocfs2_dinode *di,
120 struct buffer_head *bh,
Joel Becker73be1922009-01-06 14:57:08 -0800121 u32 sectsize,
122 struct ocfs2_blockcheck_stats *stats);
Mark Fashehccd979b2005-12-15 14:31:24 -0800123static int ocfs2_initialize_super(struct super_block *sb,
124 struct buffer_head *bh,
Joel Becker73be1922009-01-06 14:57:08 -0800125 int sector_size,
126 struct ocfs2_blockcheck_stats *stats);
Mark Fashehccd979b2005-12-15 14:31:24 -0800127static int ocfs2_get_sector(struct super_block *sb,
128 struct buffer_head **bh,
129 int block,
130 int sect_size);
131static void ocfs2_write_super(struct super_block *sb);
132static struct inode *ocfs2_alloc_inode(struct super_block *sb);
133static void ocfs2_destroy_inode(struct inode *inode);
Jan Kara19ece542008-08-21 20:13:17 +0200134static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend);
135static int ocfs2_enable_quotas(struct ocfs2_super *osb);
136static void ocfs2_disable_quotas(struct ocfs2_super *osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800137
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800138static const struct super_operations ocfs2_sops = {
Mark Fashehccd979b2005-12-15 14:31:24 -0800139 .statfs = ocfs2_statfs,
140 .alloc_inode = ocfs2_alloc_inode,
141 .destroy_inode = ocfs2_destroy_inode,
142 .drop_inode = ocfs2_drop_inode,
143 .clear_inode = ocfs2_clear_inode,
144 .delete_inode = ocfs2_delete_inode,
145 .sync_fs = ocfs2_sync_fs,
146 .write_super = ocfs2_write_super,
147 .put_super = ocfs2_put_super,
148 .remount_fs = ocfs2_remount,
Sunil Mushrand5500712007-09-06 13:34:16 -0700149 .show_options = ocfs2_show_options,
Jan Kara9e33d692008-08-25 19:56:50 +0200150 .quota_read = ocfs2_quota_read,
151 .quota_write = ocfs2_quota_write,
Mark Fashehccd979b2005-12-15 14:31:24 -0800152};
153
154enum {
155 Opt_barrier,
156 Opt_err_panic,
157 Opt_err_ro,
158 Opt_intr,
159 Opt_nointr,
160 Opt_hb_none,
161 Opt_hb_local,
162 Opt_data_ordered,
163 Opt_data_writeback,
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800164 Opt_atime_quantum,
Sunil Mushranbaf46612007-06-18 17:00:24 -0700165 Opt_slot,
Mark Fashehd147b3d2007-11-07 14:40:36 -0800166 Opt_commit,
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800167 Opt_localalloc,
Mark Fasheh53fc6222007-12-20 16:49:04 -0800168 Opt_localflocks,
Joel Beckerb61817e2008-02-01 15:08:23 -0800169 Opt_stack,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800170 Opt_user_xattr,
171 Opt_nouser_xattr,
Joel Becker12462f12008-09-03 20:03:40 -0700172 Opt_inode64,
Tiger Yanga68979b2008-11-14 11:17:52 +0800173 Opt_acl,
174 Opt_noacl,
Jan Kara19ece542008-08-21 20:13:17 +0200175 Opt_usrquota,
176 Opt_grpquota,
Mark Fashehccd979b2005-12-15 14:31:24 -0800177 Opt_err,
178};
179
Steven Whitehousea447c092008-10-13 10:46:57 +0100180static const match_table_t tokens = {
Mark Fashehccd979b2005-12-15 14:31:24 -0800181 {Opt_barrier, "barrier=%u"},
182 {Opt_err_panic, "errors=panic"},
183 {Opt_err_ro, "errors=remount-ro"},
184 {Opt_intr, "intr"},
185 {Opt_nointr, "nointr"},
186 {Opt_hb_none, OCFS2_HB_NONE},
187 {Opt_hb_local, OCFS2_HB_LOCAL},
188 {Opt_data_ordered, "data=ordered"},
189 {Opt_data_writeback, "data=writeback"},
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800190 {Opt_atime_quantum, "atime_quantum=%u"},
Sunil Mushranbaf46612007-06-18 17:00:24 -0700191 {Opt_slot, "preferred_slot=%u"},
Mark Fashehd147b3d2007-11-07 14:40:36 -0800192 {Opt_commit, "commit=%u"},
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800193 {Opt_localalloc, "localalloc=%d"},
Mark Fasheh53fc6222007-12-20 16:49:04 -0800194 {Opt_localflocks, "localflocks"},
Joel Beckerb61817e2008-02-01 15:08:23 -0800195 {Opt_stack, "cluster_stack=%s"},
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800196 {Opt_user_xattr, "user_xattr"},
197 {Opt_nouser_xattr, "nouser_xattr"},
Joel Becker12462f12008-09-03 20:03:40 -0700198 {Opt_inode64, "inode64"},
Tiger Yanga68979b2008-11-14 11:17:52 +0800199 {Opt_acl, "acl"},
200 {Opt_noacl, "noacl"},
Jan Kara19ece542008-08-21 20:13:17 +0200201 {Opt_usrquota, "usrquota"},
202 {Opt_grpquota, "grpquota"},
Mark Fashehccd979b2005-12-15 14:31:24 -0800203 {Opt_err, NULL}
204};
205
Sunil Mushran50397502008-12-17 14:17:43 -0800206#ifdef CONFIG_DEBUG_FS
207static int ocfs2_osb_dump(struct ocfs2_super *osb, char *buf, int len)
208{
209 int out = 0;
210 int i;
211 struct ocfs2_cluster_connection *cconn = osb->cconn;
212 struct ocfs2_recovery_map *rm = osb->recovery_map;
Srinivas Eeda15633a22009-06-03 17:02:56 -0700213 struct ocfs2_orphan_scan *os;
Sunil Mushran50397502008-12-17 14:17:43 -0800214
215 out += snprintf(buf + out, len - out,
216 "%10s => Id: %-s Uuid: %-s Gen: 0x%X Label: %-s\n",
217 "Device", osb->dev_str, osb->uuid_str,
218 osb->fs_generation, osb->vol_label);
219
220 out += snprintf(buf + out, len - out,
221 "%10s => State: %d Flags: 0x%lX\n", "Volume",
222 atomic_read(&osb->vol_state), osb->osb_flags);
223
224 out += snprintf(buf + out, len - out,
225 "%10s => Block: %lu Cluster: %d\n", "Sizes",
226 osb->sb->s_blocksize, osb->s_clustersize);
227
228 out += snprintf(buf + out, len - out,
229 "%10s => Compat: 0x%X Incompat: 0x%X "
230 "ROcompat: 0x%X\n",
231 "Features", osb->s_feature_compat,
232 osb->s_feature_incompat, osb->s_feature_ro_compat);
233
234 out += snprintf(buf + out, len - out,
235 "%10s => Opts: 0x%lX AtimeQuanta: %u\n", "Mount",
236 osb->s_mount_opt, osb->s_atime_quantum);
237
238 out += snprintf(buf + out, len - out,
239 "%10s => Stack: %s Name: %*s Version: %d.%d\n",
240 "Cluster",
241 (*osb->osb_cluster_stack == '\0' ?
242 "o2cb" : osb->osb_cluster_stack),
243 cconn->cc_namelen, cconn->cc_name,
244 cconn->cc_version.pv_major, cconn->cc_version.pv_minor);
245
246 spin_lock(&osb->dc_task_lock);
247 out += snprintf(buf + out, len - out,
248 "%10s => Pid: %d Count: %lu WakeSeq: %lu "
249 "WorkSeq: %lu\n", "DownCnvt",
250 task_pid_nr(osb->dc_task), osb->blocked_lock_count,
251 osb->dc_wake_sequence, osb->dc_work_sequence);
252 spin_unlock(&osb->dc_task_lock);
253
254 spin_lock(&osb->osb_lock);
255 out += snprintf(buf + out, len - out, "%10s => Pid: %d Nodes:",
256 "Recovery",
257 (osb->recovery_thread_task ?
258 task_pid_nr(osb->recovery_thread_task) : -1));
259 if (rm->rm_used == 0)
260 out += snprintf(buf + out, len - out, " None\n");
261 else {
262 for (i = 0; i < rm->rm_used; i++)
263 out += snprintf(buf + out, len - out, " %d",
264 rm->rm_entries[i]);
265 out += snprintf(buf + out, len - out, "\n");
266 }
267 spin_unlock(&osb->osb_lock);
268
269 out += snprintf(buf + out, len - out,
270 "%10s => Pid: %d Interval: %lu Needs: %d\n", "Commit",
271 task_pid_nr(osb->commit_task), osb->osb_commit_interval,
272 atomic_read(&osb->needs_checkpoint));
273
274 out += snprintf(buf + out, len - out,
275 "%10s => State: %d NumTxns: %d TxnId: %lu\n",
276 "Journal", osb->journal->j_state,
277 atomic_read(&osb->journal->j_num_trans),
278 osb->journal->j_trans_id);
279
280 out += snprintf(buf + out, len - out,
281 "%10s => GlobalAllocs: %d LocalAllocs: %d "
282 "SubAllocs: %d LAWinMoves: %d SAExtends: %d\n",
283 "Stats",
284 atomic_read(&osb->alloc_stats.bitmap_data),
285 atomic_read(&osb->alloc_stats.local_data),
286 atomic_read(&osb->alloc_stats.bg_allocs),
287 atomic_read(&osb->alloc_stats.moves),
288 atomic_read(&osb->alloc_stats.bg_extends));
289
290 out += snprintf(buf + out, len - out,
291 "%10s => State: %u Descriptor: %llu Size: %u bits "
292 "Default: %u bits\n",
293 "LocalAlloc", osb->local_alloc_state,
294 (unsigned long long)osb->la_last_gd,
295 osb->local_alloc_bits, osb->local_alloc_default_bits);
296
297 spin_lock(&osb->osb_lock);
298 out += snprintf(buf + out, len - out,
299 "%10s => Slot: %d NumStolen: %d\n", "Steal",
300 osb->s_inode_steal_slot,
301 atomic_read(&osb->s_num_inodes_stolen));
302 spin_unlock(&osb->osb_lock);
303
304 out += snprintf(buf + out, len - out, "%10s => %3s %10s\n",
305 "Slots", "Num", "RecoGen");
306
307 for (i = 0; i < osb->max_slots; ++i) {
308 out += snprintf(buf + out, len - out,
309 "%10s %c %3d %10d\n",
310 " ",
311 (i == osb->slot_num ? '*' : ' '),
312 i, osb->slot_recovery_generations[i]);
313 }
314
Srinivas Eeda15633a22009-06-03 17:02:56 -0700315 os = &osb->osb_orphan_scan;
316 out += snprintf(buf + out, len - out, "Orphan Scan=> ");
317 out += snprintf(buf + out, len - out, "Local: %u Global: %u ",
318 os->os_count, os->os_seqno);
319 out += snprintf(buf + out, len - out, " Last Scan: %lu seconds ago\n",
320 (get_seconds() - os->os_scantime.tv_sec));
321
Sunil Mushran50397502008-12-17 14:17:43 -0800322 return out;
323}
324
325static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
326{
327 struct ocfs2_super *osb = inode->i_private;
328 char *buf = NULL;
329
330 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
331 if (!buf)
332 goto bail;
333
334 i_size_write(inode, ocfs2_osb_dump(osb, buf, PAGE_SIZE));
335
336 file->private_data = buf;
337
338 return 0;
339bail:
340 return -ENOMEM;
341}
342
343static int ocfs2_debug_release(struct inode *inode, struct file *file)
344{
345 kfree(file->private_data);
346 return 0;
347}
348
349static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
350 size_t nbytes, loff_t *ppos)
351{
352 return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
353 i_size_read(file->f_mapping->host));
354}
355#else
356static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
357{
358 return 0;
359}
360static int ocfs2_debug_release(struct inode *inode, struct file *file)
361{
362 return 0;
363}
364static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
365 size_t nbytes, loff_t *ppos)
366{
367 return 0;
368}
369#endif /* CONFIG_DEBUG_FS */
370
371static struct file_operations ocfs2_osb_debug_fops = {
372 .open = ocfs2_osb_debug_open,
373 .release = ocfs2_debug_release,
374 .read = ocfs2_debug_read,
375 .llseek = generic_file_llseek,
376};
377
Mark Fashehccd979b2005-12-15 14:31:24 -0800378/*
379 * write_super and sync_fs ripped right out of ext3.
380 */
381static void ocfs2_write_super(struct super_block *sb)
382{
Ingo Molnar7892f2f42006-01-09 15:59:25 -0800383 if (mutex_trylock(&sb->s_lock) != 0)
Mark Fashehccd979b2005-12-15 14:31:24 -0800384 BUG();
385 sb->s_dirt = 0;
386}
387
388static int ocfs2_sync_fs(struct super_block *sb, int wait)
389{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800390 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -0800391 tid_t target;
392 struct ocfs2_super *osb = OCFS2_SB(sb);
393
394 sb->s_dirt = 0;
395
396 if (ocfs2_is_hard_readonly(osb))
397 return -EROFS;
398
399 if (wait) {
400 status = ocfs2_flush_truncate_log(osb);
401 if (status < 0)
402 mlog_errno(status);
403 } else {
404 ocfs2_schedule_truncate_log_flush(osb, 0);
405 }
406
Joel Becker2b4e30f2008-09-03 20:03:41 -0700407 if (jbd2_journal_start_commit(OCFS2_SB(sb)->journal->j_journal,
408 &target)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800409 if (wait)
Joel Becker2b4e30f2008-09-03 20:03:41 -0700410 jbd2_log_wait_commit(OCFS2_SB(sb)->journal->j_journal,
411 target);
Mark Fashehccd979b2005-12-15 14:31:24 -0800412 }
413 return 0;
414}
415
Jan Kara1a224ad2008-08-20 15:43:36 +0200416static int ocfs2_need_system_inode(struct ocfs2_super *osb, int ino)
417{
418 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_USRQUOTA)
419 && (ino == USER_QUOTA_SYSTEM_INODE
420 || ino == LOCAL_USER_QUOTA_SYSTEM_INODE))
421 return 0;
422 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)
423 && (ino == GROUP_QUOTA_SYSTEM_INODE
424 || ino == LOCAL_GROUP_QUOTA_SYSTEM_INODE))
425 return 0;
426 return 1;
427}
428
Mark Fashehccd979b2005-12-15 14:31:24 -0800429static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb)
430{
431 struct inode *new = NULL;
432 int status = 0;
433 int i;
434
435 mlog_entry_void();
436
Jan Kara5fa06132008-01-11 00:11:45 +0100437 new = ocfs2_iget(osb, osb->root_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800438 if (IS_ERR(new)) {
439 status = PTR_ERR(new);
440 mlog_errno(status);
441 goto bail;
442 }
443 osb->root_inode = new;
444
Jan Kara5fa06132008-01-11 00:11:45 +0100445 new = ocfs2_iget(osb, osb->system_dir_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800446 if (IS_ERR(new)) {
447 status = PTR_ERR(new);
448 mlog_errno(status);
449 goto bail;
450 }
451 osb->sys_root_inode = new;
452
453 for (i = OCFS2_FIRST_ONLINE_SYSTEM_INODE;
454 i <= OCFS2_LAST_GLOBAL_SYSTEM_INODE; i++) {
Jan Kara1a224ad2008-08-20 15:43:36 +0200455 if (!ocfs2_need_system_inode(osb, i))
456 continue;
Mark Fashehccd979b2005-12-15 14:31:24 -0800457 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
458 if (!new) {
459 ocfs2_release_system_inodes(osb);
460 status = -EINVAL;
461 mlog_errno(status);
462 /* FIXME: Should ERROR_RO_FS */
463 mlog(ML_ERROR, "Unable to load system inode %d, "
464 "possibly corrupt fs?", i);
465 goto bail;
466 }
467 // the array now has one ref, so drop this one
468 iput(new);
469 }
470
471bail:
472 mlog_exit(status);
473 return status;
474}
475
476static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb)
477{
478 struct inode *new = NULL;
479 int status = 0;
480 int i;
481
482 mlog_entry_void();
483
484 for (i = OCFS2_LAST_GLOBAL_SYSTEM_INODE + 1;
485 i < NUM_SYSTEM_INODES;
486 i++) {
Jan Kara1a224ad2008-08-20 15:43:36 +0200487 if (!ocfs2_need_system_inode(osb, i))
488 continue;
Mark Fashehccd979b2005-12-15 14:31:24 -0800489 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
490 if (!new) {
491 ocfs2_release_system_inodes(osb);
492 status = -EINVAL;
493 mlog(ML_ERROR, "status=%d, sysfile=%d, slot=%d\n",
494 status, i, osb->slot_num);
495 goto bail;
496 }
497 /* the array now has one ref, so drop this one */
498 iput(new);
499 }
500
501bail:
502 mlog_exit(status);
503 return status;
504}
505
Denis Chengbddb8eb32007-09-27 02:10:04 +0800506static void ocfs2_release_system_inodes(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -0800507{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800508 int i;
Mark Fashehccd979b2005-12-15 14:31:24 -0800509 struct inode *inode;
510
511 mlog_entry_void();
512
513 for (i = 0; i < NUM_SYSTEM_INODES; i++) {
514 inode = osb->system_inodes[i];
515 if (inode) {
516 iput(inode);
517 osb->system_inodes[i] = NULL;
518 }
519 }
520
521 inode = osb->sys_root_inode;
522 if (inode) {
523 iput(inode);
524 osb->sys_root_inode = NULL;
525 }
526
527 inode = osb->root_inode;
528 if (inode) {
529 iput(inode);
530 osb->root_inode = NULL;
531 }
532
Denis Chengbddb8eb32007-09-27 02:10:04 +0800533 mlog_exit(0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800534}
535
536/* We're allocating fs objects, use GFP_NOFS */
537static struct inode *ocfs2_alloc_inode(struct super_block *sb)
538{
539 struct ocfs2_inode_info *oi;
540
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800541 oi = kmem_cache_alloc(ocfs2_inode_cachep, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800542 if (!oi)
543 return NULL;
544
Joel Becker2b4e30f2008-09-03 20:03:41 -0700545 jbd2_journal_init_jbd_inode(&oi->ip_jinode, &oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800546 return &oi->vfs_inode;
547}
548
549static void ocfs2_destroy_inode(struct inode *inode)
550{
551 kmem_cache_free(ocfs2_inode_cachep, OCFS2_I(inode));
552}
553
Mark Fasheh5a254032007-07-20 12:56:16 -0700554static unsigned long long ocfs2_max_file_offset(unsigned int bbits,
555 unsigned int cbits)
Mark Fashehccd979b2005-12-15 14:31:24 -0800556{
Mark Fasheh5a254032007-07-20 12:56:16 -0700557 unsigned int bytes = 1 << cbits;
558 unsigned int trim = bytes;
559 unsigned int bitshift = 32;
Mark Fashehccd979b2005-12-15 14:31:24 -0800560
Mark Fasheh5a254032007-07-20 12:56:16 -0700561 /*
562 * i_size and all block offsets in ocfs2 are always 64 bits
563 * wide. i_clusters is 32 bits, in cluster-sized units. So on
564 * 64 bit platforms, cluster size will be the limiting factor.
Mark Fashehccd979b2005-12-15 14:31:24 -0800565 */
566
567#if BITS_PER_LONG == 32
568# if defined(CONFIG_LBD)
Alexey Dobriyan2ecd05a2006-10-11 01:22:05 -0700569 BUILD_BUG_ON(sizeof(sector_t) != 8);
Mark Fasheh5a254032007-07-20 12:56:16 -0700570 /*
571 * We might be limited by page cache size.
572 */
573 if (bytes > PAGE_CACHE_SIZE) {
574 bytes = PAGE_CACHE_SIZE;
575 trim = 1;
576 /*
577 * Shift by 31 here so that we don't get larger than
578 * MAX_LFS_FILESIZE
579 */
580 bitshift = 31;
581 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800582# else
Mark Fasheh5a254032007-07-20 12:56:16 -0700583 /*
584 * We are limited by the size of sector_t. Use block size, as
585 * that's what we expose to the VFS.
586 */
587 bytes = 1 << bbits;
588 trim = 1;
589 bitshift = 31;
Mark Fashehccd979b2005-12-15 14:31:24 -0800590# endif
591#endif
592
Mark Fasheh5a254032007-07-20 12:56:16 -0700593 /*
594 * Trim by a whole cluster when we can actually approach the
595 * on-disk limits. Otherwise we can overflow i_clusters when
596 * an extent start is at the max offset.
597 */
598 return (((unsigned long long)bytes) << bitshift) - trim;
Mark Fashehccd979b2005-12-15 14:31:24 -0800599}
600
601static int ocfs2_remount(struct super_block *sb, int *flags, char *data)
602{
603 int incompat_features;
604 int ret = 0;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800605 struct mount_options parsed_options;
Mark Fashehccd979b2005-12-15 14:31:24 -0800606 struct ocfs2_super *osb = OCFS2_SB(sb);
607
Tiger Yangc0123ad2007-09-08 00:16:10 +0800608 if (!ocfs2_parse_options(sb, data, &parsed_options, 1)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800609 ret = -EINVAL;
610 goto out;
611 }
612
613 if ((osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) !=
Tiger Yangc0123ad2007-09-08 00:16:10 +0800614 (parsed_options.mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800615 ret = -EINVAL;
616 mlog(ML_ERROR, "Cannot change heartbeat mode on remount\n");
617 goto out;
618 }
619
620 if ((osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK) !=
Tiger Yangc0123ad2007-09-08 00:16:10 +0800621 (parsed_options.mount_opt & OCFS2_MOUNT_DATA_WRITEBACK)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800622 ret = -EINVAL;
623 mlog(ML_ERROR, "Cannot change data mode on remount\n");
624 goto out;
625 }
626
Joel Becker12462f12008-09-03 20:03:40 -0700627 /* Probably don't want this on remount; it might
628 * mess with other nodes */
629 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64) &&
630 (parsed_options.mount_opt & OCFS2_MOUNT_INODE64)) {
631 ret = -EINVAL;
632 mlog(ML_ERROR, "Cannot enable inode64 on remount\n");
633 goto out;
634 }
635
Mark Fashehccd979b2005-12-15 14:31:24 -0800636 /* We're going to/from readonly mode. */
637 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
Jan Kara19ece542008-08-21 20:13:17 +0200638 /* Disable quota accounting before remounting RO */
639 if (*flags & MS_RDONLY) {
640 ret = ocfs2_susp_quotas(osb, 0);
641 if (ret < 0)
642 goto out;
643 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800644 /* Lock here so the check of HARD_RO and the potential
645 * setting of SOFT_RO is atomic. */
646 spin_lock(&osb->osb_lock);
647 if (osb->osb_flags & OCFS2_OSB_HARD_RO) {
648 mlog(ML_ERROR, "Remount on readonly device is forbidden.\n");
649 ret = -EROFS;
650 goto unlock_osb;
651 }
652
653 if (*flags & MS_RDONLY) {
654 mlog(0, "Going to ro mode.\n");
655 sb->s_flags |= MS_RDONLY;
656 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
657 } else {
658 mlog(0, "Making ro filesystem writeable.\n");
659
660 if (osb->osb_flags & OCFS2_OSB_ERROR_FS) {
661 mlog(ML_ERROR, "Cannot remount RDWR "
662 "filesystem due to previous errors.\n");
663 ret = -EROFS;
664 goto unlock_osb;
665 }
666 incompat_features = OCFS2_HAS_RO_COMPAT_FEATURE(sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP);
667 if (incompat_features) {
668 mlog(ML_ERROR, "Cannot remount RDWR because "
669 "of unsupported optional features "
670 "(%x).\n", incompat_features);
671 ret = -EINVAL;
672 goto unlock_osb;
673 }
674 sb->s_flags &= ~MS_RDONLY;
675 osb->osb_flags &= ~OCFS2_OSB_SOFT_RO;
676 }
677unlock_osb:
678 spin_unlock(&osb->osb_lock);
Jan Kara19ece542008-08-21 20:13:17 +0200679 /* Enable quota accounting after remounting RW */
680 if (!ret && !(*flags & MS_RDONLY)) {
681 if (sb_any_quota_suspended(sb))
682 ret = ocfs2_susp_quotas(osb, 1);
683 else
684 ret = ocfs2_enable_quotas(osb);
685 if (ret < 0) {
686 /* Return back changes... */
687 spin_lock(&osb->osb_lock);
688 sb->s_flags |= MS_RDONLY;
689 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
690 spin_unlock(&osb->osb_lock);
691 goto out;
692 }
693 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800694 }
695
696 if (!ret) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800697 /* Only save off the new mount options in case of a successful
698 * remount. */
Tiger Yanga68979b2008-11-14 11:17:52 +0800699 if (!(osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR))
700 parsed_options.mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800701 osb->s_mount_opt = parsed_options.mount_opt;
702 osb->s_atime_quantum = parsed_options.atime_quantum;
703 osb->preferred_slot = parsed_options.slot;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800704 if (parsed_options.commit_interval)
705 osb->osb_commit_interval = parsed_options.commit_interval;
Mark Fashehe001e792007-11-07 14:21:45 -0800706
707 if (!ocfs2_is_hard_readonly(osb))
708 ocfs2_set_journal_params(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800709 }
710out:
711 return ret;
712}
713
714static int ocfs2_sb_probe(struct super_block *sb,
715 struct buffer_head **bh,
Joel Becker73be1922009-01-06 14:57:08 -0800716 int *sector_size,
717 struct ocfs2_blockcheck_stats *stats)
Mark Fashehccd979b2005-12-15 14:31:24 -0800718{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800719 int status, tmpstat;
Mark Fashehccd979b2005-12-15 14:31:24 -0800720 struct ocfs1_vol_disk_hdr *hdr;
721 struct ocfs2_dinode *di;
722 int blksize;
723
724 *bh = NULL;
725
726 /* may be > 512 */
727 *sector_size = bdev_hardsect_size(sb->s_bdev);
728 if (*sector_size > OCFS2_MAX_BLOCKSIZE) {
729 mlog(ML_ERROR, "Hardware sector size too large: %d (max=%d)\n",
730 *sector_size, OCFS2_MAX_BLOCKSIZE);
731 status = -EINVAL;
732 goto bail;
733 }
734
735 /* Can this really happen? */
736 if (*sector_size < OCFS2_MIN_BLOCKSIZE)
737 *sector_size = OCFS2_MIN_BLOCKSIZE;
738
739 /* check block zero for old format */
740 status = ocfs2_get_sector(sb, bh, 0, *sector_size);
741 if (status < 0) {
742 mlog_errno(status);
743 goto bail;
744 }
745 hdr = (struct ocfs1_vol_disk_hdr *) (*bh)->b_data;
746 if (hdr->major_version == OCFS1_MAJOR_VERSION) {
747 mlog(ML_ERROR, "incompatible version: %u.%u\n",
748 hdr->major_version, hdr->minor_version);
749 status = -EINVAL;
750 }
751 if (memcmp(hdr->signature, OCFS1_VOLUME_SIGNATURE,
752 strlen(OCFS1_VOLUME_SIGNATURE)) == 0) {
753 mlog(ML_ERROR, "incompatible volume signature: %8s\n",
754 hdr->signature);
755 status = -EINVAL;
756 }
757 brelse(*bh);
758 *bh = NULL;
759 if (status < 0) {
760 mlog(ML_ERROR, "This is an ocfs v1 filesystem which must be "
761 "upgraded before mounting with ocfs v2\n");
762 goto bail;
763 }
764
765 /*
766 * Now check at magic offset for 512, 1024, 2048, 4096
767 * blocksizes. 4096 is the maximum blocksize because it is
768 * the minimum clustersize.
769 */
770 status = -EINVAL;
771 for (blksize = *sector_size;
772 blksize <= OCFS2_MAX_BLOCKSIZE;
773 blksize <<= 1) {
774 tmpstat = ocfs2_get_sector(sb, bh,
775 OCFS2_SUPER_BLOCK_BLKNO,
776 blksize);
777 if (tmpstat < 0) {
778 status = tmpstat;
779 mlog_errno(status);
780 goto bail;
781 }
782 di = (struct ocfs2_dinode *) (*bh)->b_data;
Joel Becker73be1922009-01-06 14:57:08 -0800783 memset(stats, 0, sizeof(struct ocfs2_blockcheck_stats));
784 status = ocfs2_verify_volume(di, *bh, blksize, stats);
Mark Fashehccd979b2005-12-15 14:31:24 -0800785 if (status >= 0)
786 goto bail;
787 brelse(*bh);
788 *bh = NULL;
789 if (status != -EAGAIN)
790 break;
791 }
792
793bail:
794 return status;
795}
796
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800797static int ocfs2_verify_heartbeat(struct ocfs2_super *osb)
798{
799 if (ocfs2_mount_local(osb)) {
800 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
801 mlog(ML_ERROR, "Cannot heartbeat on a locally "
802 "mounted device.\n");
803 return -EINVAL;
804 }
805 }
806
Joel Beckerb61817e2008-02-01 15:08:23 -0800807 if (ocfs2_userspace_stack(osb)) {
808 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
809 mlog(ML_ERROR, "Userspace stack expected, but "
810 "o2cb heartbeat arguments passed to mount\n");
811 return -EINVAL;
812 }
813 }
814
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800815 if (!(osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
Joel Beckerb61817e2008-02-01 15:08:23 -0800816 if (!ocfs2_mount_local(osb) && !ocfs2_is_hard_readonly(osb) &&
817 !ocfs2_userspace_stack(osb)) {
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800818 mlog(ML_ERROR, "Heartbeat has to be started to mount "
819 "a read-write clustered device.\n");
820 return -EINVAL;
821 }
822 }
823
824 return 0;
825}
826
Joel Beckerb61817e2008-02-01 15:08:23 -0800827/*
828 * If we're using a userspace stack, mount should have passed
829 * a name that matches the disk. If not, mount should not
830 * have passed a stack.
831 */
832static int ocfs2_verify_userspace_stack(struct ocfs2_super *osb,
833 struct mount_options *mopt)
834{
835 if (!ocfs2_userspace_stack(osb) && mopt->cluster_stack[0]) {
836 mlog(ML_ERROR,
837 "cluster stack passed to mount, but this filesystem "
838 "does not support it\n");
839 return -EINVAL;
840 }
841
842 if (ocfs2_userspace_stack(osb) &&
843 strncmp(osb->osb_cluster_stack, mopt->cluster_stack,
844 OCFS2_STACK_LABEL_LEN)) {
845 mlog(ML_ERROR,
846 "cluster stack passed to mount (\"%s\") does not "
847 "match the filesystem (\"%s\")\n",
848 mopt->cluster_stack,
849 osb->osb_cluster_stack);
850 return -EINVAL;
851 }
852
853 return 0;
854}
855
Jan Kara19ece542008-08-21 20:13:17 +0200856static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend)
857{
858 int type;
859 struct super_block *sb = osb->sb;
860 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
861 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
862 int status = 0;
863
864 for (type = 0; type < MAXQUOTAS; type++) {
865 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
866 continue;
867 if (unsuspend)
868 status = vfs_quota_enable(
869 sb_dqopt(sb)->files[type],
870 type, QFMT_OCFS2,
871 DQUOT_SUSPENDED);
872 else
873 status = vfs_quota_disable(sb, type,
874 DQUOT_SUSPENDED);
875 if (status < 0)
876 break;
877 }
878 if (status < 0)
879 mlog(ML_ERROR, "Failed to suspend/unsuspend quotas on "
880 "remount (error = %d).\n", status);
881 return status;
882}
883
884static int ocfs2_enable_quotas(struct ocfs2_super *osb)
885{
886 struct inode *inode[MAXQUOTAS] = { NULL, NULL };
887 struct super_block *sb = osb->sb;
888 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
889 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
890 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
891 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
892 int status;
893 int type;
894
895 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NEGATIVE_USAGE;
896 for (type = 0; type < MAXQUOTAS; type++) {
897 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
898 continue;
899 inode[type] = ocfs2_get_system_file_inode(osb, ino[type],
900 osb->slot_num);
901 if (!inode[type]) {
902 status = -ENOENT;
903 goto out_quota_off;
904 }
905 status = vfs_quota_enable(inode[type], type, QFMT_OCFS2,
906 DQUOT_USAGE_ENABLED);
907 if (status < 0)
908 goto out_quota_off;
909 }
910
911 for (type = 0; type < MAXQUOTAS; type++)
912 iput(inode[type]);
913 return 0;
914out_quota_off:
915 ocfs2_disable_quotas(osb);
916 for (type = 0; type < MAXQUOTAS; type++)
917 iput(inode[type]);
918 mlog_errno(status);
919 return status;
920}
921
922static void ocfs2_disable_quotas(struct ocfs2_super *osb)
923{
924 int type;
925 struct inode *inode;
926 struct super_block *sb = osb->sb;
927
928 /* We mostly ignore errors in this function because there's not much
929 * we can do when we see them */
930 for (type = 0; type < MAXQUOTAS; type++) {
931 if (!sb_has_quota_loaded(sb, type))
932 continue;
933 inode = igrab(sb->s_dquot.files[type]);
934 /* Turn off quotas. This will remove all dquot structures from
935 * memory and so they will be automatically synced to global
936 * quota files */
937 vfs_quota_disable(sb, type, DQUOT_USAGE_ENABLED |
938 DQUOT_LIMITS_ENABLED);
939 if (!inode)
940 continue;
941 iput(inode);
942 }
943}
944
945/* Handle quota on quotactl */
946static int ocfs2_quota_on(struct super_block *sb, int type, int format_id,
947 char *path, int remount)
948{
949 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
950 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
951
952 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
953 return -EINVAL;
954
955 if (remount)
956 return 0; /* Just ignore it has been handled in
957 * ocfs2_remount() */
958 return vfs_quota_enable(sb_dqopt(sb)->files[type], type,
959 format_id, DQUOT_LIMITS_ENABLED);
960}
961
962/* Handle quota off quotactl */
963static int ocfs2_quota_off(struct super_block *sb, int type, int remount)
964{
965 if (remount)
966 return 0; /* Ignore now and handle later in
967 * ocfs2_remount() */
968 return vfs_quota_disable(sb, type, DQUOT_LIMITS_ENABLED);
969}
970
971static struct quotactl_ops ocfs2_quotactl_ops = {
972 .quota_on = ocfs2_quota_on,
973 .quota_off = ocfs2_quota_off,
974 .quota_sync = vfs_quota_sync,
975 .get_info = vfs_get_dqinfo,
976 .set_info = vfs_set_dqinfo,
977 .get_dqblk = vfs_get_dqblk,
978 .set_dqblk = vfs_set_dqblk,
979};
980
Mark Fashehccd979b2005-12-15 14:31:24 -0800981static int ocfs2_fill_super(struct super_block *sb, void *data, int silent)
982{
983 struct dentry *root;
984 int status, sector_size;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800985 struct mount_options parsed_options;
Mark Fashehccd979b2005-12-15 14:31:24 -0800986 struct inode *inode = NULL;
987 struct ocfs2_super *osb = NULL;
988 struct buffer_head *bh = NULL;
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800989 char nodestr[8];
Joel Becker73be1922009-01-06 14:57:08 -0800990 struct ocfs2_blockcheck_stats stats;
Mark Fashehccd979b2005-12-15 14:31:24 -0800991
992 mlog_entry("%p, %p, %i", sb, data, silent);
993
Tiger Yangc0123ad2007-09-08 00:16:10 +0800994 if (!ocfs2_parse_options(sb, data, &parsed_options, 0)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800995 status = -EINVAL;
996 goto read_super_error;
997 }
998
999 /* probe for superblock */
Joel Becker73be1922009-01-06 14:57:08 -08001000 status = ocfs2_sb_probe(sb, &bh, &sector_size, &stats);
Mark Fashehccd979b2005-12-15 14:31:24 -08001001 if (status < 0) {
1002 mlog(ML_ERROR, "superblock probe failed!\n");
1003 goto read_super_error;
1004 }
1005
Joel Becker73be1922009-01-06 14:57:08 -08001006 status = ocfs2_initialize_super(sb, bh, sector_size, &stats);
Mark Fashehccd979b2005-12-15 14:31:24 -08001007 osb = OCFS2_SB(sb);
1008 if (status < 0) {
1009 mlog_errno(status);
1010 goto read_super_error;
1011 }
1012 brelse(bh);
1013 bh = NULL;
Tiger Yanga68979b2008-11-14 11:17:52 +08001014
1015 if (!(osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR))
1016 parsed_options.mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
1017
Tiger Yangc0123ad2007-09-08 00:16:10 +08001018 osb->s_mount_opt = parsed_options.mount_opt;
1019 osb->s_atime_quantum = parsed_options.atime_quantum;
1020 osb->preferred_slot = parsed_options.slot;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001021 osb->osb_commit_interval = parsed_options.commit_interval;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001022 osb->local_alloc_default_bits = ocfs2_megabytes_to_clusters(sb, parsed_options.localalloc_opt);
1023 osb->local_alloc_bits = osb->local_alloc_default_bits;
Jan Kara19ece542008-08-21 20:13:17 +02001024 if (osb->s_mount_opt & OCFS2_MOUNT_USRQUOTA &&
1025 !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1026 OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
1027 status = -EINVAL;
1028 mlog(ML_ERROR, "User quotas were requested, but this "
1029 "filesystem does not have the feature enabled.\n");
1030 goto read_super_error;
1031 }
1032 if (osb->s_mount_opt & OCFS2_MOUNT_GRPQUOTA &&
1033 !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1034 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
1035 status = -EINVAL;
1036 mlog(ML_ERROR, "Group quotas were requested, but this "
1037 "filesystem does not have the feature enabled.\n");
1038 goto read_super_error;
1039 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001040
Joel Beckerb61817e2008-02-01 15:08:23 -08001041 status = ocfs2_verify_userspace_stack(osb, &parsed_options);
1042 if (status)
1043 goto read_super_error;
1044
Mark Fashehccd979b2005-12-15 14:31:24 -08001045 sb->s_magic = OCFS2_SUPER_MAGIC;
1046
Tiger Yanga68979b2008-11-14 11:17:52 +08001047 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1048 ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
1049
Mark Fashehccd979b2005-12-15 14:31:24 -08001050 /* Hard readonly mode only if: bdev_read_only, MS_RDONLY,
1051 * heartbeat=none */
1052 if (bdev_read_only(sb->s_bdev)) {
1053 if (!(sb->s_flags & MS_RDONLY)) {
1054 status = -EACCES;
1055 mlog(ML_ERROR, "Readonly device detected but readonly "
1056 "mount was not specified.\n");
1057 goto read_super_error;
1058 }
1059
1060 /* You should not be able to start a local heartbeat
1061 * on a readonly device. */
1062 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
1063 status = -EROFS;
1064 mlog(ML_ERROR, "Local heartbeat specified on readonly "
1065 "device.\n");
1066 goto read_super_error;
1067 }
1068
1069 status = ocfs2_check_journals_nolocks(osb);
1070 if (status < 0) {
1071 if (status == -EROFS)
1072 mlog(ML_ERROR, "Recovery required on readonly "
1073 "file system, but write access is "
1074 "unavailable.\n");
1075 else
1076 mlog_errno(status);
1077 goto read_super_error;
1078 }
1079
1080 ocfs2_set_ro_flag(osb, 1);
1081
1082 printk(KERN_NOTICE "Readonly device detected. No cluster "
1083 "services will be utilized for this mount. Recovery "
1084 "will be skipped.\n");
1085 }
1086
1087 if (!ocfs2_is_hard_readonly(osb)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001088 if (sb->s_flags & MS_RDONLY)
1089 ocfs2_set_ro_flag(osb, 0);
1090 }
1091
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001092 status = ocfs2_verify_heartbeat(osb);
1093 if (status < 0) {
1094 mlog_errno(status);
1095 goto read_super_error;
1096 }
1097
Mark Fashehccd979b2005-12-15 14:31:24 -08001098 osb->osb_debug_root = debugfs_create_dir(osb->uuid_str,
1099 ocfs2_debugfs_root);
1100 if (!osb->osb_debug_root) {
1101 status = -EINVAL;
1102 mlog(ML_ERROR, "Unable to create per-mount debugfs root.\n");
1103 goto read_super_error;
1104 }
1105
Sunil Mushran50397502008-12-17 14:17:43 -08001106 osb->osb_ctxt = debugfs_create_file("fs_state", S_IFREG|S_IRUSR,
1107 osb->osb_debug_root,
1108 osb,
1109 &ocfs2_osb_debug_fops);
1110 if (!osb->osb_ctxt) {
1111 status = -EINVAL;
1112 mlog_errno(status);
1113 goto read_super_error;
1114 }
1115
Joel Becker73be1922009-01-06 14:57:08 -08001116 if (ocfs2_meta_ecc(osb)) {
1117 status = ocfs2_blockcheck_stats_debugfs_install(
1118 &osb->osb_ecc_stats,
1119 osb->osb_debug_root);
1120 if (status) {
1121 mlog(ML_ERROR,
1122 "Unable to create blockcheck statistics "
1123 "files\n");
1124 goto read_super_error;
1125 }
1126 }
1127
Mark Fashehccd979b2005-12-15 14:31:24 -08001128 status = ocfs2_mount_volume(sb);
1129 if (osb->root_inode)
1130 inode = igrab(osb->root_inode);
1131
1132 if (status < 0)
1133 goto read_super_error;
1134
1135 if (!inode) {
1136 status = -EIO;
1137 mlog_errno(status);
1138 goto read_super_error;
1139 }
1140
1141 root = d_alloc_root(inode);
1142 if (!root) {
1143 status = -ENOMEM;
1144 mlog_errno(status);
1145 goto read_super_error;
1146 }
1147
1148 sb->s_root = root;
1149
1150 ocfs2_complete_mount_recovery(osb);
1151
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001152 if (ocfs2_mount_local(osb))
1153 snprintf(nodestr, sizeof(nodestr), "local");
1154 else
Joel Becker19fdb622008-01-30 15:38:24 -08001155 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001156
1157 printk(KERN_INFO "ocfs2: Mounting device (%s) on (node %s, slot %d) "
Sunil Mushran781ee3e2006-04-27 16:41:31 -07001158 "with %s data mode.\n",
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001159 osb->dev_str, nodestr, osb->slot_num,
Mark Fashehccd979b2005-12-15 14:31:24 -08001160 osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK ? "writeback" :
1161 "ordered");
1162
1163 atomic_set(&osb->vol_state, VOLUME_MOUNTED);
1164 wake_up(&osb->osb_mount_event);
1165
Jan Kara19ece542008-08-21 20:13:17 +02001166 /* Now we can initialize quotas because we can afford to wait
1167 * for cluster locks recovery now. That also means that truncation
1168 * log recovery can happen but that waits for proper quota setup */
1169 if (!(sb->s_flags & MS_RDONLY)) {
1170 status = ocfs2_enable_quotas(osb);
1171 if (status < 0) {
1172 /* We have to err-out specially here because
1173 * s_root is already set */
1174 mlog_errno(status);
1175 atomic_set(&osb->vol_state, VOLUME_DISABLED);
1176 wake_up(&osb->osb_mount_event);
1177 mlog_exit(status);
1178 return status;
1179 }
1180 }
1181
1182 ocfs2_complete_quota_recovery(osb);
1183
1184 /* Now we wake up again for processes waiting for quotas */
1185 atomic_set(&osb->vol_state, VOLUME_MOUNTED_QUOTAS);
1186 wake_up(&osb->osb_mount_event);
1187
Mark Fashehccd979b2005-12-15 14:31:24 -08001188 mlog_exit(status);
1189 return status;
1190
1191read_super_error:
Mark Fasheha81cb882008-10-07 14:25:16 -07001192 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001193
1194 if (inode)
1195 iput(inode);
1196
1197 if (osb) {
1198 atomic_set(&osb->vol_state, VOLUME_DISABLED);
1199 wake_up(&osb->osb_mount_event);
1200 ocfs2_dismount_volume(sb, 1);
1201 }
1202
1203 mlog_exit(status);
1204 return status;
1205}
1206
David Howells454e2392006-06-23 02:02:57 -07001207static int ocfs2_get_sb(struct file_system_type *fs_type,
1208 int flags,
1209 const char *dev_name,
1210 void *data,
1211 struct vfsmount *mnt)
Mark Fashehccd979b2005-12-15 14:31:24 -08001212{
David Howells454e2392006-06-23 02:02:57 -07001213 return get_sb_bdev(fs_type, flags, dev_name, data, ocfs2_fill_super,
1214 mnt);
Mark Fashehccd979b2005-12-15 14:31:24 -08001215}
1216
1217static struct file_system_type ocfs2_fs_type = {
1218 .owner = THIS_MODULE,
1219 .name = "ocfs2",
1220 .get_sb = ocfs2_get_sb, /* is this called when we mount
1221 * the fs? */
1222 .kill_sb = kill_block_super, /* set to the generic one
1223 * right now, but do we
1224 * need to change that? */
Mark Fasheh1ba9da22006-09-08 14:22:54 -07001225 .fs_flags = FS_REQUIRES_DEV|FS_RENAME_DOES_D_MOVE,
Mark Fashehccd979b2005-12-15 14:31:24 -08001226 .next = NULL
1227};
1228
1229static int ocfs2_parse_options(struct super_block *sb,
1230 char *options,
Tiger Yangc0123ad2007-09-08 00:16:10 +08001231 struct mount_options *mopt,
Mark Fashehccd979b2005-12-15 14:31:24 -08001232 int is_remount)
1233{
1234 int status;
1235 char *p;
1236
1237 mlog_entry("remount: %d, options: \"%s\"\n", is_remount,
1238 options ? options : "(none)");
1239
Mark Fashehd147b3d2007-11-07 14:40:36 -08001240 mopt->commit_interval = 0;
Tiger Yangc0123ad2007-09-08 00:16:10 +08001241 mopt->mount_opt = 0;
1242 mopt->atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
1243 mopt->slot = OCFS2_INVALID_SLOT;
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001244 mopt->localalloc_opt = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE;
Joel Beckerb61817e2008-02-01 15:08:23 -08001245 mopt->cluster_stack[0] = '\0';
Mark Fashehccd979b2005-12-15 14:31:24 -08001246
1247 if (!options) {
1248 status = 1;
1249 goto bail;
1250 }
1251
1252 while ((p = strsep(&options, ",")) != NULL) {
1253 int token, option;
1254 substring_t args[MAX_OPT_ARGS];
1255
1256 if (!*p)
1257 continue;
1258
1259 token = match_token(p, tokens, args);
1260 switch (token) {
1261 case Opt_hb_local:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001262 mopt->mount_opt |= OCFS2_MOUNT_HB_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001263 break;
1264 case Opt_hb_none:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001265 mopt->mount_opt &= ~OCFS2_MOUNT_HB_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001266 break;
1267 case Opt_barrier:
1268 if (match_int(&args[0], &option)) {
1269 status = 0;
1270 goto bail;
1271 }
1272 if (option)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001273 mopt->mount_opt |= OCFS2_MOUNT_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -08001274 else
Tiger Yangc0123ad2007-09-08 00:16:10 +08001275 mopt->mount_opt &= ~OCFS2_MOUNT_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -08001276 break;
1277 case Opt_intr:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001278 mopt->mount_opt &= ~OCFS2_MOUNT_NOINTR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001279 break;
1280 case Opt_nointr:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001281 mopt->mount_opt |= OCFS2_MOUNT_NOINTR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001282 break;
1283 case Opt_err_panic:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001284 mopt->mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001285 break;
1286 case Opt_err_ro:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001287 mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_PANIC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001288 break;
1289 case Opt_data_ordered:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001290 mopt->mount_opt &= ~OCFS2_MOUNT_DATA_WRITEBACK;
Mark Fashehccd979b2005-12-15 14:31:24 -08001291 break;
1292 case Opt_data_writeback:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001293 mopt->mount_opt |= OCFS2_MOUNT_DATA_WRITEBACK;
Mark Fashehccd979b2005-12-15 14:31:24 -08001294 break;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001295 case Opt_user_xattr:
1296 mopt->mount_opt &= ~OCFS2_MOUNT_NOUSERXATTR;
1297 break;
1298 case Opt_nouser_xattr:
1299 mopt->mount_opt |= OCFS2_MOUNT_NOUSERXATTR;
1300 break;
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001301 case Opt_atime_quantum:
1302 if (match_int(&args[0], &option)) {
1303 status = 0;
1304 goto bail;
1305 }
1306 if (option >= 0)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001307 mopt->atime_quantum = option;
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001308 break;
Sunil Mushranbaf46612007-06-18 17:00:24 -07001309 case Opt_slot:
1310 option = 0;
1311 if (match_int(&args[0], &option)) {
1312 status = 0;
1313 goto bail;
1314 }
1315 if (option)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001316 mopt->slot = (s16)option;
Sunil Mushranbaf46612007-06-18 17:00:24 -07001317 break;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001318 case Opt_commit:
1319 option = 0;
1320 if (match_int(&args[0], &option)) {
1321 status = 0;
1322 goto bail;
1323 }
1324 if (option < 0)
1325 return 0;
1326 if (option == 0)
Joel Becker2b4e30f2008-09-03 20:03:41 -07001327 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001328 mopt->commit_interval = HZ * option;
1329 break;
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001330 case Opt_localalloc:
1331 option = 0;
1332 if (match_int(&args[0], &option)) {
1333 status = 0;
1334 goto bail;
1335 }
1336 if (option >= 0 && (option <= ocfs2_local_alloc_size(sb) * 8))
1337 mopt->localalloc_opt = option;
1338 break;
Mark Fasheh53fc6222007-12-20 16:49:04 -08001339 case Opt_localflocks:
1340 /*
1341 * Changing this during remount could race
1342 * flock() requests, or "unbalance" existing
1343 * ones (e.g., a lock is taken in one mode but
1344 * dropped in the other). If users care enough
1345 * to flip locking modes during remount, we
1346 * could add a "local" flag to individual
1347 * flock structures for proper tracking of
1348 * state.
1349 */
1350 if (!is_remount)
1351 mopt->mount_opt |= OCFS2_MOUNT_LOCALFLOCKS;
1352 break;
Joel Beckerb61817e2008-02-01 15:08:23 -08001353 case Opt_stack:
1354 /* Check both that the option we were passed
1355 * is of the right length and that it is a proper
1356 * string of the right length.
1357 */
1358 if (((args[0].to - args[0].from) !=
1359 OCFS2_STACK_LABEL_LEN) ||
1360 (strnlen(args[0].from,
1361 OCFS2_STACK_LABEL_LEN) !=
1362 OCFS2_STACK_LABEL_LEN)) {
1363 mlog(ML_ERROR,
1364 "Invalid cluster_stack option\n");
1365 status = 0;
1366 goto bail;
1367 }
1368 memcpy(mopt->cluster_stack, args[0].from,
1369 OCFS2_STACK_LABEL_LEN);
1370 mopt->cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
1371 break;
Joel Becker12462f12008-09-03 20:03:40 -07001372 case Opt_inode64:
1373 mopt->mount_opt |= OCFS2_MOUNT_INODE64;
1374 break;
Jan Kara19ece542008-08-21 20:13:17 +02001375 case Opt_usrquota:
1376 /* We check only on remount, otherwise features
1377 * aren't yet initialized. */
1378 if (is_remount && !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1379 OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
1380 mlog(ML_ERROR, "User quota requested but "
1381 "filesystem feature is not set\n");
1382 status = 0;
1383 goto bail;
1384 }
1385 mopt->mount_opt |= OCFS2_MOUNT_USRQUOTA;
1386 break;
1387 case Opt_grpquota:
1388 if (is_remount && !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1389 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
1390 mlog(ML_ERROR, "Group quota requested but "
1391 "filesystem feature is not set\n");
1392 status = 0;
1393 goto bail;
1394 }
1395 mopt->mount_opt |= OCFS2_MOUNT_GRPQUOTA;
1396 break;
Tiger Yanga68979b2008-11-14 11:17:52 +08001397#ifdef CONFIG_OCFS2_FS_POSIX_ACL
1398 case Opt_acl:
1399 mopt->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
1400 break;
1401 case Opt_noacl:
1402 mopt->mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
1403 break;
1404#else
1405 case Opt_acl:
1406 case Opt_noacl:
1407 printk(KERN_INFO "ocfs2 (no)acl options not supported\n");
1408 break;
1409#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001410 default:
1411 mlog(ML_ERROR,
1412 "Unrecognized mount option \"%s\" "
1413 "or missing value\n", p);
1414 status = 0;
1415 goto bail;
1416 }
1417 }
1418
1419 status = 1;
1420
1421bail:
1422 mlog_exit(status);
1423 return status;
1424}
1425
Sunil Mushrand5500712007-09-06 13:34:16 -07001426static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
1427{
1428 struct ocfs2_super *osb = OCFS2_SB(mnt->mnt_sb);
1429 unsigned long opts = osb->s_mount_opt;
Mark Fashehebcee4b2008-07-28 14:55:20 -07001430 unsigned int local_alloc_megs;
Sunil Mushrand5500712007-09-06 13:34:16 -07001431
1432 if (opts & OCFS2_MOUNT_HB_LOCAL)
1433 seq_printf(s, ",_netdev,heartbeat=local");
1434 else
1435 seq_printf(s, ",heartbeat=none");
1436
1437 if (opts & OCFS2_MOUNT_NOINTR)
1438 seq_printf(s, ",nointr");
1439
1440 if (opts & OCFS2_MOUNT_DATA_WRITEBACK)
1441 seq_printf(s, ",data=writeback");
1442 else
1443 seq_printf(s, ",data=ordered");
1444
1445 if (opts & OCFS2_MOUNT_BARRIER)
1446 seq_printf(s, ",barrier=1");
1447
1448 if (opts & OCFS2_MOUNT_ERRORS_PANIC)
1449 seq_printf(s, ",errors=panic");
1450 else
1451 seq_printf(s, ",errors=remount-ro");
1452
1453 if (osb->preferred_slot != OCFS2_INVALID_SLOT)
1454 seq_printf(s, ",preferred_slot=%d", osb->preferred_slot);
1455
1456 if (osb->s_atime_quantum != OCFS2_DEFAULT_ATIME_QUANTUM)
1457 seq_printf(s, ",atime_quantum=%u", osb->s_atime_quantum);
1458
Mark Fashehd147b3d2007-11-07 14:40:36 -08001459 if (osb->osb_commit_interval)
1460 seq_printf(s, ",commit=%u",
1461 (unsigned) (osb->osb_commit_interval / HZ));
1462
Mark Fashehebcee4b2008-07-28 14:55:20 -07001463 local_alloc_megs = osb->local_alloc_bits >> (20 - osb->s_clustersize_bits);
1464 if (local_alloc_megs != OCFS2_DEFAULT_LOCAL_ALLOC_SIZE)
1465 seq_printf(s, ",localalloc=%d", local_alloc_megs);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001466
Mark Fasheh53fc6222007-12-20 16:49:04 -08001467 if (opts & OCFS2_MOUNT_LOCALFLOCKS)
1468 seq_printf(s, ",localflocks,");
1469
Joel Beckerb61817e2008-02-01 15:08:23 -08001470 if (osb->osb_cluster_stack[0])
1471 seq_printf(s, ",cluster_stack=%.*s", OCFS2_STACK_LABEL_LEN,
1472 osb->osb_cluster_stack);
Jan Kara19ece542008-08-21 20:13:17 +02001473 if (opts & OCFS2_MOUNT_USRQUOTA)
1474 seq_printf(s, ",usrquota");
1475 if (opts & OCFS2_MOUNT_GRPQUOTA)
1476 seq_printf(s, ",grpquota");
Joel Beckerb61817e2008-02-01 15:08:23 -08001477
Sunil Mushranb0f73cf2008-09-05 11:29:14 -07001478 if (opts & OCFS2_MOUNT_NOUSERXATTR)
1479 seq_printf(s, ",nouser_xattr");
1480 else
1481 seq_printf(s, ",user_xattr");
1482
Joel Becker12462f12008-09-03 20:03:40 -07001483 if (opts & OCFS2_MOUNT_INODE64)
1484 seq_printf(s, ",inode64");
1485
Tiger Yanga68979b2008-11-14 11:17:52 +08001486#ifdef CONFIG_OCFS2_FS_POSIX_ACL
1487 if (opts & OCFS2_MOUNT_POSIX_ACL)
1488 seq_printf(s, ",acl");
1489 else
1490 seq_printf(s, ",noacl");
1491#endif
1492
Sunil Mushrand5500712007-09-06 13:34:16 -07001493 return 0;
1494}
1495
Mark Fashehccd979b2005-12-15 14:31:24 -08001496static int __init ocfs2_init(void)
1497{
1498 int status;
1499
1500 mlog_entry_void();
1501
1502 ocfs2_print_version();
1503
Mark Fashehccd979b2005-12-15 14:31:24 -08001504 status = init_ocfs2_uptodate_cache();
1505 if (status < 0) {
1506 mlog_errno(status);
1507 goto leave;
1508 }
1509
1510 status = ocfs2_initialize_mem_caches();
1511 if (status < 0) {
1512 mlog_errno(status);
1513 goto leave;
1514 }
1515
1516 ocfs2_wq = create_singlethread_workqueue("ocfs2_wq");
1517 if (!ocfs2_wq) {
1518 status = -ENOMEM;
1519 goto leave;
1520 }
1521
Mark Fashehccd979b2005-12-15 14:31:24 -08001522 ocfs2_debugfs_root = debugfs_create_dir("ocfs2", NULL);
1523 if (!ocfs2_debugfs_root) {
1524 status = -EFAULT;
1525 mlog(ML_ERROR, "Unable to create ocfs2 debugfs root.\n");
1526 }
1527
Mark Fasheh171bf932008-10-20 15:36:47 +02001528 status = ocfs2_quota_setup();
1529 if (status)
1530 goto leave;
1531
Joel Becker63e0c482008-01-30 16:58:36 -08001532 ocfs2_set_locking_protocol();
1533
Jan Kara9e33d692008-08-25 19:56:50 +02001534 status = register_quota_format(&ocfs2_quota_format);
Mark Fashehccd979b2005-12-15 14:31:24 -08001535leave:
1536 if (status < 0) {
Mark Fasheh171bf932008-10-20 15:36:47 +02001537 ocfs2_quota_shutdown();
Mark Fashehccd979b2005-12-15 14:31:24 -08001538 ocfs2_free_mem_caches();
1539 exit_ocfs2_uptodate_cache();
Mark Fashehccd979b2005-12-15 14:31:24 -08001540 }
1541
1542 mlog_exit(status);
1543
1544 if (status >= 0) {
1545 return register_filesystem(&ocfs2_fs_type);
1546 } else
1547 return -1;
1548}
1549
1550static void __exit ocfs2_exit(void)
1551{
1552 mlog_entry_void();
1553
Mark Fasheh171bf932008-10-20 15:36:47 +02001554 ocfs2_quota_shutdown();
1555
Mark Fashehccd979b2005-12-15 14:31:24 -08001556 if (ocfs2_wq) {
1557 flush_workqueue(ocfs2_wq);
1558 destroy_workqueue(ocfs2_wq);
1559 }
1560
Jan Kara9e33d692008-08-25 19:56:50 +02001561 unregister_quota_format(&ocfs2_quota_format);
1562
Mark Fashehccd979b2005-12-15 14:31:24 -08001563 debugfs_remove(ocfs2_debugfs_root);
1564
1565 ocfs2_free_mem_caches();
1566
1567 unregister_filesystem(&ocfs2_fs_type);
1568
Mark Fashehccd979b2005-12-15 14:31:24 -08001569 exit_ocfs2_uptodate_cache();
1570
1571 mlog_exit_void();
1572}
1573
1574static void ocfs2_put_super(struct super_block *sb)
1575{
1576 mlog_entry("(0x%p)\n", sb);
1577
1578 ocfs2_sync_blockdev(sb);
1579 ocfs2_dismount_volume(sb, 0);
1580
1581 mlog_exit_void();
1582}
1583
David Howells726c3342006-06-23 02:02:58 -07001584static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
Mark Fashehccd979b2005-12-15 14:31:24 -08001585{
1586 struct ocfs2_super *osb;
1587 u32 numbits, freebits;
1588 int status;
1589 struct ocfs2_dinode *bm_lock;
1590 struct buffer_head *bh = NULL;
1591 struct inode *inode = NULL;
1592
David Howells726c3342006-06-23 02:02:58 -07001593 mlog_entry("(%p, %p)\n", dentry->d_sb, buf);
Mark Fashehccd979b2005-12-15 14:31:24 -08001594
David Howells726c3342006-06-23 02:02:58 -07001595 osb = OCFS2_SB(dentry->d_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001596
1597 inode = ocfs2_get_system_file_inode(osb,
1598 GLOBAL_BITMAP_SYSTEM_INODE,
1599 OCFS2_INVALID_SLOT);
1600 if (!inode) {
1601 mlog(ML_ERROR, "failed to get bitmap inode\n");
1602 status = -EIO;
1603 goto bail;
1604 }
1605
Mark Fashehe63aecb62007-10-18 15:30:42 -07001606 status = ocfs2_inode_lock(inode, &bh, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001607 if (status < 0) {
1608 mlog_errno(status);
1609 goto bail;
1610 }
1611
1612 bm_lock = (struct ocfs2_dinode *) bh->b_data;
1613
1614 numbits = le32_to_cpu(bm_lock->id1.bitmap1.i_total);
1615 freebits = numbits - le32_to_cpu(bm_lock->id1.bitmap1.i_used);
1616
1617 buf->f_type = OCFS2_SUPER_MAGIC;
David Howells726c3342006-06-23 02:02:58 -07001618 buf->f_bsize = dentry->d_sb->s_blocksize;
Mark Fashehccd979b2005-12-15 14:31:24 -08001619 buf->f_namelen = OCFS2_MAX_FILENAME_LEN;
1620 buf->f_blocks = ((sector_t) numbits) *
1621 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1622 buf->f_bfree = ((sector_t) freebits) *
1623 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1624 buf->f_bavail = buf->f_bfree;
1625 buf->f_files = numbits;
1626 buf->f_ffree = freebits;
1627
1628 brelse(bh);
1629
Mark Fashehe63aecb62007-10-18 15:30:42 -07001630 ocfs2_inode_unlock(inode, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001631 status = 0;
1632bail:
1633 if (inode)
1634 iput(inode);
1635
1636 mlog_exit(status);
1637
1638 return status;
1639}
1640
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001641static void ocfs2_inode_init_once(void *data)
Mark Fashehccd979b2005-12-15 14:31:24 -08001642{
1643 struct ocfs2_inode_info *oi = data;
1644
Christoph Lametera35afb82007-05-16 22:10:57 -07001645 oi->ip_flags = 0;
1646 oi->ip_open_count = 0;
1647 spin_lock_init(&oi->ip_lock);
1648 ocfs2_extent_map_init(&oi->vfs_inode);
1649 INIT_LIST_HEAD(&oi->ip_io_markers);
1650 oi->ip_created_trans = 0;
1651 oi->ip_last_trans = 0;
1652 oi->ip_dir_start_lookup = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001653
Christoph Lametera35afb82007-05-16 22:10:57 -07001654 init_rwsem(&oi->ip_alloc_sem);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001655 init_rwsem(&oi->ip_xattr_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -07001656 mutex_init(&oi->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001657
Christoph Lametera35afb82007-05-16 22:10:57 -07001658 oi->ip_blkno = 0ULL;
1659 oi->ip_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001660
Christoph Lametera35afb82007-05-16 22:10:57 -07001661 ocfs2_lock_res_init_once(&oi->ip_rw_lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001662 ocfs2_lock_res_init_once(&oi->ip_inode_lockres);
Christoph Lametera35afb82007-05-16 22:10:57 -07001663 ocfs2_lock_res_init_once(&oi->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001664
Christoph Lametera35afb82007-05-16 22:10:57 -07001665 ocfs2_metadata_cache_init(&oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001666
Christoph Lametera35afb82007-05-16 22:10:57 -07001667 inode_init_once(&oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001668}
1669
1670static int ocfs2_initialize_mem_caches(void)
1671{
1672 ocfs2_inode_cachep = kmem_cache_create("ocfs2_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001673 sizeof(struct ocfs2_inode_info),
1674 0,
1675 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1676 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001677 ocfs2_inode_init_once);
Jan Kara9e33d692008-08-25 19:56:50 +02001678 ocfs2_dquot_cachep = kmem_cache_create("ocfs2_dquot_cache",
1679 sizeof(struct ocfs2_dquot),
1680 0,
1681 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1682 SLAB_MEM_SPREAD),
1683 NULL);
1684 ocfs2_qf_chunk_cachep = kmem_cache_create("ocfs2_qf_chunk_cache",
1685 sizeof(struct ocfs2_quota_chunk),
1686 0,
1687 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
1688 NULL);
1689 if (!ocfs2_inode_cachep || !ocfs2_dquot_cachep ||
1690 !ocfs2_qf_chunk_cachep) {
1691 if (ocfs2_inode_cachep)
1692 kmem_cache_destroy(ocfs2_inode_cachep);
1693 if (ocfs2_dquot_cachep)
1694 kmem_cache_destroy(ocfs2_dquot_cachep);
1695 if (ocfs2_qf_chunk_cachep)
1696 kmem_cache_destroy(ocfs2_qf_chunk_cachep);
Mark Fashehccd979b2005-12-15 14:31:24 -08001697 return -ENOMEM;
Jan Kara9e33d692008-08-25 19:56:50 +02001698 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001699
Mark Fashehccd979b2005-12-15 14:31:24 -08001700 return 0;
1701}
1702
1703static void ocfs2_free_mem_caches(void)
1704{
1705 if (ocfs2_inode_cachep)
1706 kmem_cache_destroy(ocfs2_inode_cachep);
Mark Fashehccd979b2005-12-15 14:31:24 -08001707 ocfs2_inode_cachep = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +02001708
1709 if (ocfs2_dquot_cachep)
1710 kmem_cache_destroy(ocfs2_dquot_cachep);
1711 ocfs2_dquot_cachep = NULL;
1712
1713 if (ocfs2_qf_chunk_cachep)
1714 kmem_cache_destroy(ocfs2_qf_chunk_cachep);
1715 ocfs2_qf_chunk_cachep = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001716}
1717
1718static int ocfs2_get_sector(struct super_block *sb,
1719 struct buffer_head **bh,
1720 int block,
1721 int sect_size)
1722{
1723 if (!sb_set_blocksize(sb, sect_size)) {
1724 mlog(ML_ERROR, "unable to set blocksize\n");
1725 return -EIO;
1726 }
1727
1728 *bh = sb_getblk(sb, block);
1729 if (!*bh) {
1730 mlog_errno(-EIO);
1731 return -EIO;
1732 }
1733 lock_buffer(*bh);
1734 if (!buffer_dirty(*bh))
1735 clear_buffer_uptodate(*bh);
1736 unlock_buffer(*bh);
1737 ll_rw_block(READ, 1, bh);
1738 wait_on_buffer(*bh);
wengang wang28d57d42009-02-13 10:11:47 +08001739 if (!buffer_uptodate(*bh)) {
1740 mlog_errno(-EIO);
1741 brelse(*bh);
1742 *bh = NULL;
1743 return -EIO;
1744 }
1745
Mark Fashehccd979b2005-12-15 14:31:24 -08001746 return 0;
1747}
1748
Mark Fashehccd979b2005-12-15 14:31:24 -08001749static int ocfs2_mount_volume(struct super_block *sb)
1750{
1751 int status = 0;
1752 int unlock_super = 0;
1753 struct ocfs2_super *osb = OCFS2_SB(sb);
1754
1755 mlog_entry_void();
1756
1757 if (ocfs2_is_hard_readonly(osb))
1758 goto leave;
1759
Mark Fashehccd979b2005-12-15 14:31:24 -08001760 status = ocfs2_dlm_init(osb);
1761 if (status < 0) {
1762 mlog_errno(status);
1763 goto leave;
1764 }
1765
Mark Fashehccd979b2005-12-15 14:31:24 -08001766 status = ocfs2_super_lock(osb, 1);
1767 if (status < 0) {
1768 mlog_errno(status);
1769 goto leave;
1770 }
1771 unlock_super = 1;
1772
1773 /* This will load up the node map and add ourselves to it. */
1774 status = ocfs2_find_slot(osb);
1775 if (status < 0) {
1776 mlog_errno(status);
1777 goto leave;
1778 }
1779
Mark Fashehccd979b2005-12-15 14:31:24 -08001780 /* load all node-local system inodes */
1781 status = ocfs2_init_local_system_inodes(osb);
1782 if (status < 0) {
1783 mlog_errno(status);
1784 goto leave;
1785 }
1786
1787 status = ocfs2_check_volume(osb);
1788 if (status < 0) {
1789 mlog_errno(status);
1790 goto leave;
1791 }
1792
1793 status = ocfs2_truncate_log_init(osb);
Tao Ma06c59bb2009-05-19 06:47:20 +08001794 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08001795 mlog_errno(status);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001796
Mark Fashehccd979b2005-12-15 14:31:24 -08001797leave:
1798 if (unlock_super)
1799 ocfs2_super_unlock(osb, 1);
1800
1801 mlog_exit(status);
1802 return status;
1803}
1804
Mark Fashehccd979b2005-12-15 14:31:24 -08001805static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
1806{
Joel Becker286eaa92008-02-01 15:03:57 -08001807 int tmp, hangup_needed = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001808 struct ocfs2_super *osb = NULL;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001809 char nodestr[8];
Mark Fashehccd979b2005-12-15 14:31:24 -08001810
1811 mlog_entry("(0x%p)\n", sb);
1812
1813 BUG_ON(!sb);
1814 osb = OCFS2_SB(sb);
1815 BUG_ON(!osb);
1816
Sunil Mushran50397502008-12-17 14:17:43 -08001817 debugfs_remove(osb->osb_ctxt);
1818
Jan Kara19ece542008-08-21 20:13:17 +02001819 ocfs2_disable_quotas(osb);
1820
Mark Fashehccd979b2005-12-15 14:31:24 -08001821 ocfs2_shutdown_local_alloc(osb);
1822
1823 ocfs2_truncate_log_shutdown(osb);
1824
Srinivas Eeda83273932009-06-03 17:02:55 -07001825 ocfs2_orphan_scan_stop(osb);
1826
Joel Becker553abd02008-02-01 12:03:57 -08001827 /* This will disable recovery and flush any recovery work. */
1828 ocfs2_recovery_exit(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001829
1830 ocfs2_journal_shutdown(osb);
1831
1832 ocfs2_sync_blockdev(sb);
1833
Joel Becker4670c462008-02-01 14:39:35 -08001834 /* No cluster connection means we've failed during mount, so skip
1835 * all the steps which depended on that to complete. */
1836 if (osb->cconn) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001837 tmp = ocfs2_super_lock(osb, 1);
1838 if (tmp < 0) {
1839 mlog_errno(tmp);
1840 return;
1841 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001842 }
1843
Mark Fasheh19b613d2007-10-04 14:47:09 -07001844 if (osb->slot_num != OCFS2_INVALID_SLOT)
1845 ocfs2_put_slot(osb);
1846
Joel Becker4670c462008-02-01 14:39:35 -08001847 if (osb->cconn)
Mark Fasheh19b613d2007-10-04 14:47:09 -07001848 ocfs2_super_unlock(osb, 1);
1849
Mark Fashehccd979b2005-12-15 14:31:24 -08001850 ocfs2_release_system_inodes(osb);
1851
Joel Becker6953b4c2008-01-29 16:59:56 -08001852 /*
Joel Becker6953b4c2008-01-29 16:59:56 -08001853 * If we're dismounting due to mount error, mount.ocfs2 will clean
1854 * up heartbeat. If we're a local mount, there is no heartbeat.
1855 * If we failed before we got a uuid_str yet, we can't stop
1856 * heartbeat. Otherwise, do it.
1857 */
1858 if (!mnt_err && !ocfs2_mount_local(osb) && osb->uuid_str)
Joel Becker286eaa92008-02-01 15:03:57 -08001859 hangup_needed = 1;
1860
1861 if (osb->cconn)
1862 ocfs2_dlm_shutdown(osb, hangup_needed);
1863
Joel Becker73be1922009-01-06 14:57:08 -08001864 ocfs2_blockcheck_stats_debugfs_remove(&osb->osb_ecc_stats);
Joel Becker286eaa92008-02-01 15:03:57 -08001865 debugfs_remove(osb->osb_debug_root);
1866
1867 if (hangup_needed)
Joel Becker6953b4c2008-01-29 16:59:56 -08001868 ocfs2_cluster_hangup(osb->uuid_str, strlen(osb->uuid_str));
Mark Fashehccd979b2005-12-15 14:31:24 -08001869
1870 atomic_set(&osb->vol_state, VOLUME_DISMOUNTED);
1871
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001872 if (ocfs2_mount_local(osb))
1873 snprintf(nodestr, sizeof(nodestr), "local");
1874 else
Joel Becker19fdb622008-01-30 15:38:24 -08001875 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001876
1877 printk(KERN_INFO "ocfs2: Unmounting device (%s) on (node %s)\n",
1878 osb->dev_str, nodestr);
Mark Fashehccd979b2005-12-15 14:31:24 -08001879
1880 ocfs2_delete_osb(osb);
1881 kfree(osb);
1882 sb->s_dev = 0;
1883 sb->s_fs_info = NULL;
1884}
1885
1886static int ocfs2_setup_osb_uuid(struct ocfs2_super *osb, const unsigned char *uuid,
1887 unsigned uuid_bytes)
1888{
1889 int i, ret;
1890 char *ptr;
1891
1892 BUG_ON(uuid_bytes != OCFS2_VOL_UUID_LEN);
1893
Robert P. J. Daycd861282006-12-13 00:34:52 -08001894 osb->uuid_str = kzalloc(OCFS2_VOL_UUID_LEN * 2 + 1, GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001895 if (osb->uuid_str == NULL)
1896 return -ENOMEM;
1897
Mark Fashehccd979b2005-12-15 14:31:24 -08001898 for (i = 0, ptr = osb->uuid_str; i < OCFS2_VOL_UUID_LEN; i++) {
1899 /* print with null */
1900 ret = snprintf(ptr, 3, "%02X", uuid[i]);
1901 if (ret != 2) /* drop super cleans up */
1902 return -EINVAL;
1903 /* then only advance past the last char */
1904 ptr += 2;
1905 }
1906
1907 return 0;
1908}
1909
1910static int ocfs2_initialize_super(struct super_block *sb,
1911 struct buffer_head *bh,
Joel Becker73be1922009-01-06 14:57:08 -08001912 int sector_size,
1913 struct ocfs2_blockcheck_stats *stats)
Mark Fashehccd979b2005-12-15 14:31:24 -08001914{
Denis Chengbddb8eb32007-09-27 02:10:04 +08001915 int status;
Mark Fasheh5a254032007-07-20 12:56:16 -07001916 int i, cbits, bbits;
1917 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001918 struct inode *inode = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001919 struct ocfs2_journal *journal;
1920 __le32 uuid_net_key;
1921 struct ocfs2_super *osb;
1922
1923 mlog_entry_void();
1924
Robert P. J. Daycd861282006-12-13 00:34:52 -08001925 osb = kzalloc(sizeof(struct ocfs2_super), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001926 if (!osb) {
1927 status = -ENOMEM;
1928 mlog_errno(status);
1929 goto bail;
1930 }
1931
1932 sb->s_fs_info = osb;
1933 sb->s_op = &ocfs2_sops;
1934 sb->s_export_op = &ocfs2_export_ops;
Jan Kara19ece542008-08-21 20:13:17 +02001935 sb->s_qcop = &ocfs2_quotactl_ops;
1936 sb->dq_op = &ocfs2_quota_operations;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001937 sb->s_xattr = ocfs2_xattr_handlers;
Mark Fashehe0dceaf2007-08-09 16:52:30 -07001938 sb->s_time_gran = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001939 sb->s_flags |= MS_NOATIME;
1940 /* this is needed to support O_LARGEFILE */
Mark Fasheh5a254032007-07-20 12:56:16 -07001941 cbits = le32_to_cpu(di->id2.i_super.s_clustersize_bits);
1942 bbits = le32_to_cpu(di->id2.i_super.s_blocksize_bits);
1943 sb->s_maxbytes = ocfs2_max_file_offset(bbits, cbits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001944
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001945 osb->osb_dx_mask = (1 << (cbits - bbits)) - 1;
1946
1947 for (i = 0; i < 3; i++)
1948 osb->osb_dx_seed[i] = le32_to_cpu(di->id2.i_super.s_dx_seed[i]);
1949 osb->osb_dx_seed[3] = le32_to_cpu(di->id2.i_super.s_uuid_hash);
1950
Mark Fashehccd979b2005-12-15 14:31:24 -08001951 osb->sb = sb;
1952 /* Save off for ocfs2_rw_direct */
1953 osb->s_sectsize_bits = blksize_bits(sector_size);
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01001954 BUG_ON(!osb->s_sectsize_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001955
Mark Fasheh34d024f2007-09-24 15:56:19 -07001956 spin_lock_init(&osb->dc_task_lock);
1957 init_waitqueue_head(&osb->dc_event);
1958 osb->dc_work_sequence = 0;
1959 osb->dc_wake_sequence = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001960 INIT_LIST_HEAD(&osb->blocked_lock_list);
1961 osb->blocked_lock_count = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001962 spin_lock_init(&osb->osb_lock);
Tao Mac8b9cf92009-02-24 17:40:26 -08001963 spin_lock_init(&osb->osb_xattr_lock);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001964 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001965
1966 atomic_set(&osb->alloc_stats.moves, 0);
1967 atomic_set(&osb->alloc_stats.local_data, 0);
1968 atomic_set(&osb->alloc_stats.bitmap_data, 0);
1969 atomic_set(&osb->alloc_stats.bg_allocs, 0);
1970 atomic_set(&osb->alloc_stats.bg_extends, 0);
1971
Joel Becker73be1922009-01-06 14:57:08 -08001972 /* Copy the blockcheck stats from the superblock probe */
1973 osb->osb_ecc_stats = *stats;
1974
Mark Fashehccd979b2005-12-15 14:31:24 -08001975 ocfs2_init_node_maps(osb);
1976
1977 snprintf(osb->dev_str, sizeof(osb->dev_str), "%u,%u",
1978 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1979
Joel Becker553abd02008-02-01 12:03:57 -08001980 status = ocfs2_recovery_init(osb);
1981 if (status) {
1982 mlog(ML_ERROR, "Unable to initialize recovery state\n");
1983 mlog_errno(status);
1984 goto bail;
1985 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001986
Srinivas Eeda83273932009-06-03 17:02:55 -07001987 status = ocfs2_orphan_scan_init(osb);
1988 if (status) {
1989 mlog(ML_ERROR, "Unable to initialize delayed orphan scan\n");
1990 mlog_errno(status);
1991 goto bail;
1992 }
1993
Mark Fashehccd979b2005-12-15 14:31:24 -08001994 init_waitqueue_head(&osb->checkpoint_event);
1995 atomic_set(&osb->needs_checkpoint, 0);
1996
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001997 osb->s_atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
1998
Mark Fashehccd979b2005-12-15 14:31:24 -08001999 osb->slot_num = OCFS2_INVALID_SLOT;
2000
Tiger Yang8154da32008-08-18 17:11:46 +08002001 osb->s_xattr_inline_size = le16_to_cpu(
2002 di->id2.i_super.s_xattr_inline_size);
Tiger Yangfdd77702008-08-18 17:08:55 +08002003
Mark Fashehccd979b2005-12-15 14:31:24 -08002004 osb->local_alloc_state = OCFS2_LA_UNUSED;
2005 osb->local_alloc_bh = NULL;
Mark Fasheh9c7af402008-07-28 18:02:53 -07002006 INIT_DELAYED_WORK(&osb->la_enable_wq, ocfs2_la_enable_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08002007
Mark Fashehccd979b2005-12-15 14:31:24 -08002008 init_waitqueue_head(&osb->osb_mount_event);
2009
2010 osb->vol_label = kmalloc(OCFS2_MAX_VOL_LABEL_LEN, GFP_KERNEL);
2011 if (!osb->vol_label) {
2012 mlog(ML_ERROR, "unable to alloc vol label\n");
2013 status = -ENOMEM;
2014 goto bail;
2015 }
2016
Mark Fashehccd979b2005-12-15 14:31:24 -08002017 osb->max_slots = le16_to_cpu(di->id2.i_super.s_max_slots);
2018 if (osb->max_slots > OCFS2_MAX_SLOTS || osb->max_slots == 0) {
2019 mlog(ML_ERROR, "Invalid number of node slots (%u)\n",
2020 osb->max_slots);
2021 status = -EINVAL;
2022 goto bail;
2023 }
Sunil Mushran781ee3e2006-04-27 16:41:31 -07002024 mlog(0, "max_slots for this device: %u\n", osb->max_slots);
Mark Fashehccd979b2005-12-15 14:31:24 -08002025
Sunil Mushran539d8262008-07-14 17:31:10 -07002026 osb->slot_recovery_generations =
2027 kcalloc(osb->max_slots, sizeof(*osb->slot_recovery_generations),
2028 GFP_KERNEL);
2029 if (!osb->slot_recovery_generations) {
2030 status = -ENOMEM;
2031 mlog_errno(status);
2032 goto bail;
2033 }
2034
Mark Fashehb4df6ed2006-02-22 17:35:08 -08002035 init_waitqueue_head(&osb->osb_wipe_event);
2036 osb->osb_orphan_wipes = kcalloc(osb->max_slots,
2037 sizeof(*osb->osb_orphan_wipes),
2038 GFP_KERNEL);
2039 if (!osb->osb_orphan_wipes) {
2040 status = -ENOMEM;
2041 mlog_errno(status);
2042 goto bail;
2043 }
2044
Mark Fashehccd979b2005-12-15 14:31:24 -08002045 osb->s_feature_compat =
2046 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_compat);
2047 osb->s_feature_ro_compat =
2048 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_ro_compat);
2049 osb->s_feature_incompat =
2050 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_incompat);
2051
2052 if ((i = OCFS2_HAS_INCOMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_INCOMPAT_SUPP))) {
2053 mlog(ML_ERROR, "couldn't mount because of unsupported "
2054 "optional features (%x).\n", i);
2055 status = -EINVAL;
2056 goto bail;
2057 }
2058 if (!(osb->sb->s_flags & MS_RDONLY) &&
2059 (i = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP))) {
2060 mlog(ML_ERROR, "couldn't mount RDWR because of "
2061 "unsupported optional features (%x).\n", i);
2062 status = -EINVAL;
2063 goto bail;
2064 }
2065
Joel Beckerb61817e2008-02-01 15:08:23 -08002066 if (ocfs2_userspace_stack(osb)) {
2067 memcpy(osb->osb_cluster_stack,
2068 OCFS2_RAW_SB(di)->s_cluster_info.ci_stack,
2069 OCFS2_STACK_LABEL_LEN);
2070 osb->osb_cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
2071 if (strlen(osb->osb_cluster_stack) != OCFS2_STACK_LABEL_LEN) {
2072 mlog(ML_ERROR,
2073 "couldn't mount because of an invalid "
2074 "cluster stack label (%s) \n",
2075 osb->osb_cluster_stack);
2076 status = -EINVAL;
2077 goto bail;
2078 }
2079 } else {
2080 /* The empty string is identical with classic tools that
2081 * don't know about s_cluster_info. */
2082 osb->osb_cluster_stack[0] = '\0';
2083 }
2084
Mark Fashehccd979b2005-12-15 14:31:24 -08002085 get_random_bytes(&osb->s_next_generation, sizeof(u32));
2086
2087 /* FIXME
2088 * This should be done in ocfs2_journal_init(), but unknown
2089 * ordering issues will cause the filesystem to crash.
2090 * If anyone wants to figure out what part of the code
2091 * refers to osb->journal before ocfs2_journal_init() is run,
2092 * be my guest.
2093 */
2094 /* initialize our journal structure */
2095
Robert P. J. Daycd861282006-12-13 00:34:52 -08002096 journal = kzalloc(sizeof(struct ocfs2_journal), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08002097 if (!journal) {
2098 mlog(ML_ERROR, "unable to alloc journal\n");
2099 status = -ENOMEM;
2100 goto bail;
2101 }
2102 osb->journal = journal;
2103 journal->j_osb = osb;
2104
2105 atomic_set(&journal->j_num_trans, 0);
2106 init_rwsem(&journal->j_trans_barrier);
2107 init_waitqueue_head(&journal->j_checkpointed);
2108 spin_lock_init(&journal->j_lock);
2109 journal->j_trans_id = (unsigned long) 1;
2110 INIT_LIST_HEAD(&journal->j_la_cleanups);
David Howellsc4028952006-11-22 14:57:56 +00002111 INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
Mark Fashehccd979b2005-12-15 14:31:24 -08002112 journal->j_state = OCFS2_JOURNAL_FREE;
2113
Jan Karaea455f82009-01-12 23:20:31 +01002114 INIT_WORK(&osb->dentry_lock_work, ocfs2_drop_dl_inodes);
2115 osb->dentry_lock_list = NULL;
2116
Mark Fashehccd979b2005-12-15 14:31:24 -08002117 /* get some pseudo constants for clustersize bits */
2118 osb->s_clustersize_bits =
2119 le32_to_cpu(di->id2.i_super.s_clustersize_bits);
2120 osb->s_clustersize = 1 << osb->s_clustersize_bits;
2121 mlog(0, "clusterbits=%d\n", osb->s_clustersize_bits);
2122
2123 if (osb->s_clustersize < OCFS2_MIN_CLUSTERSIZE ||
2124 osb->s_clustersize > OCFS2_MAX_CLUSTERSIZE) {
2125 mlog(ML_ERROR, "Volume has invalid cluster size (%d)\n",
2126 osb->s_clustersize);
2127 status = -EINVAL;
2128 goto bail;
2129 }
2130
2131 if (ocfs2_clusters_to_blocks(osb->sb, le32_to_cpu(di->i_clusters) - 1)
2132 > (u32)~0UL) {
2133 mlog(ML_ERROR, "Volume might try to write to blocks beyond "
2134 "what jbd can address in 32 bits.\n");
2135 status = -EINVAL;
2136 goto bail;
2137 }
2138
2139 if (ocfs2_setup_osb_uuid(osb, di->id2.i_super.s_uuid,
2140 sizeof(di->id2.i_super.s_uuid))) {
2141 mlog(ML_ERROR, "Out of memory trying to setup our uuid.\n");
2142 status = -ENOMEM;
2143 goto bail;
2144 }
2145
Mark Fasheh78427042006-05-04 12:03:26 -07002146 memcpy(&uuid_net_key, di->id2.i_super.s_uuid, sizeof(uuid_net_key));
Mark Fashehccd979b2005-12-15 14:31:24 -08002147
2148 strncpy(osb->vol_label, di->id2.i_super.s_label, 63);
2149 osb->vol_label[63] = '\0';
2150 osb->root_blkno = le64_to_cpu(di->id2.i_super.s_root_blkno);
2151 osb->system_dir_blkno = le64_to_cpu(di->id2.i_super.s_system_dir_blkno);
2152 osb->first_cluster_group_blkno =
2153 le64_to_cpu(di->id2.i_super.s_first_cluster_group);
2154 osb->fs_generation = le32_to_cpu(di->i_fs_generation);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002155 osb->uuid_hash = le32_to_cpu(di->id2.i_super.s_uuid_hash);
Mark Fashehccd979b2005-12-15 14:31:24 -08002156 mlog(0, "vol_label: %s\n", osb->vol_label);
2157 mlog(0, "uuid: %s\n", osb->uuid_str);
Mark Fashehb06970532006-03-03 10:24:33 -08002158 mlog(0, "root_blkno=%llu, system_dir_blkno=%llu\n",
2159 (unsigned long long)osb->root_blkno,
2160 (unsigned long long)osb->system_dir_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002161
2162 osb->osb_dlm_debug = ocfs2_new_dlm_debug();
2163 if (!osb->osb_dlm_debug) {
2164 status = -ENOMEM;
2165 mlog_errno(status);
2166 goto bail;
2167 }
2168
2169 atomic_set(&osb->vol_state, VOLUME_INIT);
2170
2171 /* load root, system_dir, and all global system inodes */
2172 status = ocfs2_init_global_system_inodes(osb);
2173 if (status < 0) {
2174 mlog_errno(status);
2175 goto bail;
2176 }
2177
2178 /*
2179 * global bitmap
2180 */
2181 inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
2182 OCFS2_INVALID_SLOT);
2183 if (!inode) {
2184 status = -EINVAL;
2185 mlog_errno(status);
2186 goto bail;
2187 }
2188
2189 osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08002190 iput(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08002191
Tao Mae9d578a2007-12-18 15:46:10 +08002192 osb->bitmap_cpg = ocfs2_group_bitmap_size(sb) * 8;
Mark Fashehccd979b2005-12-15 14:31:24 -08002193
2194 status = ocfs2_init_slot_info(osb);
2195 if (status < 0) {
2196 mlog_errno(status);
2197 goto bail;
2198 }
2199
Mark Fashehccd979b2005-12-15 14:31:24 -08002200bail:
2201 mlog_exit(status);
2202 return status;
2203}
2204
2205/*
2206 * will return: -EAGAIN if it is ok to keep searching for superblocks
2207 * -EINVAL if there is a bad superblock
2208 * 0 on success
2209 */
2210static int ocfs2_verify_volume(struct ocfs2_dinode *di,
2211 struct buffer_head *bh,
Joel Becker73be1922009-01-06 14:57:08 -08002212 u32 blksz,
2213 struct ocfs2_blockcheck_stats *stats)
Mark Fashehccd979b2005-12-15 14:31:24 -08002214{
2215 int status = -EAGAIN;
2216
2217 mlog_entry_void();
2218
2219 if (memcmp(di->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
2220 strlen(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) {
Joel Beckerd030cc92008-12-11 15:04:14 -08002221 /* We have to do a raw check of the feature here */
2222 if (le32_to_cpu(di->id2.i_super.s_feature_incompat) &
2223 OCFS2_FEATURE_INCOMPAT_META_ECC) {
2224 status = ocfs2_block_check_validate(bh->b_data,
2225 bh->b_size,
Joel Becker73be1922009-01-06 14:57:08 -08002226 &di->i_check,
2227 stats);
Joel Beckerd030cc92008-12-11 15:04:14 -08002228 if (status)
2229 goto out;
2230 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002231 status = -EINVAL;
2232 if ((1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits)) != blksz) {
2233 mlog(ML_ERROR, "found superblock with incorrect block "
2234 "size: found %u, should be %u\n",
2235 1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits),
2236 blksz);
2237 } else if (le16_to_cpu(di->id2.i_super.s_major_rev_level) !=
2238 OCFS2_MAJOR_REV_LEVEL ||
2239 le16_to_cpu(di->id2.i_super.s_minor_rev_level) !=
2240 OCFS2_MINOR_REV_LEVEL) {
2241 mlog(ML_ERROR, "found superblock with bad version: "
2242 "found %u.%u, should be %u.%u\n",
2243 le16_to_cpu(di->id2.i_super.s_major_rev_level),
2244 le16_to_cpu(di->id2.i_super.s_minor_rev_level),
2245 OCFS2_MAJOR_REV_LEVEL,
2246 OCFS2_MINOR_REV_LEVEL);
2247 } else if (bh->b_blocknr != le64_to_cpu(di->i_blkno)) {
2248 mlog(ML_ERROR, "bad block number on superblock: "
Mark Fashehb06970532006-03-03 10:24:33 -08002249 "found %llu, should be %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07002250 (unsigned long long)le64_to_cpu(di->i_blkno),
Mark Fashehb06970532006-03-03 10:24:33 -08002251 (unsigned long long)bh->b_blocknr);
Mark Fashehccd979b2005-12-15 14:31:24 -08002252 } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
2253 le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
2254 mlog(ML_ERROR, "bad cluster size found: %u\n",
2255 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
2256 } else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
2257 mlog(ML_ERROR, "bad root_blkno: 0\n");
2258 } else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
2259 mlog(ML_ERROR, "bad system_dir_blkno: 0\n");
2260 } else if (le16_to_cpu(di->id2.i_super.s_max_slots) > OCFS2_MAX_SLOTS) {
2261 mlog(ML_ERROR,
2262 "Superblock slots found greater than file system "
2263 "maximum: found %u, max %u\n",
2264 le16_to_cpu(di->id2.i_super.s_max_slots),
2265 OCFS2_MAX_SLOTS);
2266 } else {
2267 /* found it! */
2268 status = 0;
2269 }
2270 }
2271
Joel Beckerd030cc92008-12-11 15:04:14 -08002272out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002273 mlog_exit(status);
2274 return status;
2275}
2276
2277static int ocfs2_check_volume(struct ocfs2_super *osb)
2278{
Denis Chengbddb8eb32007-09-27 02:10:04 +08002279 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08002280 int dirty;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002281 int local;
Mark Fashehccd979b2005-12-15 14:31:24 -08002282 struct ocfs2_dinode *local_alloc = NULL; /* only used if we
2283 * recover
2284 * ourselves. */
2285
2286 mlog_entry_void();
2287
2288 /* Init our journal object. */
2289 status = ocfs2_journal_init(osb->journal, &dirty);
2290 if (status < 0) {
2291 mlog(ML_ERROR, "Could not initialize journal!\n");
2292 goto finally;
2293 }
2294
2295 /* If the journal was unmounted cleanly then we don't want to
2296 * recover anything. Otherwise, journal_load will do that
2297 * dirty work for us :) */
2298 if (!dirty) {
2299 status = ocfs2_journal_wipe(osb->journal, 0);
2300 if (status < 0) {
2301 mlog_errno(status);
2302 goto finally;
2303 }
2304 } else {
2305 mlog(ML_NOTICE, "File system was not unmounted cleanly, "
2306 "recovering volume.\n");
2307 }
2308
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002309 local = ocfs2_mount_local(osb);
2310
Mark Fashehccd979b2005-12-15 14:31:24 -08002311 /* will play back anything left in the journal. */
Sunil Mushran539d8262008-07-14 17:31:10 -07002312 status = ocfs2_journal_load(osb->journal, local, dirty);
Wengang Wang01af4822008-06-10 14:24:48 +08002313 if (status < 0) {
2314 mlog(ML_ERROR, "ocfs2 journal load failed! %d\n", status);
2315 goto finally;
2316 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002317
2318 if (dirty) {
2319 /* recover my local alloc if we didn't unmount cleanly. */
2320 status = ocfs2_begin_local_alloc_recovery(osb,
2321 osb->slot_num,
2322 &local_alloc);
2323 if (status < 0) {
2324 mlog_errno(status);
2325 goto finally;
2326 }
2327 /* we complete the recovery process after we've marked
2328 * ourselves as mounted. */
2329 }
2330
2331 mlog(0, "Journal loaded.\n");
2332
2333 status = ocfs2_load_local_alloc(osb);
2334 if (status < 0) {
2335 mlog_errno(status);
2336 goto finally;
2337 }
2338
2339 if (dirty) {
2340 /* Recovery will be completed after we've mounted the
2341 * rest of the volume. */
2342 osb->dirty = 1;
2343 osb->local_alloc_copy = local_alloc;
2344 local_alloc = NULL;
2345 }
2346
2347 /* go through each journal, trylock it and if you get the
2348 * lock, and it's marked as dirty, set the bit in the recover
2349 * map and launch a recovery thread for it. */
2350 status = ocfs2_mark_dead_nodes(osb);
Srinivas Eeda9140db02009-03-06 14:21:46 -08002351 if (status < 0) {
2352 mlog_errno(status);
2353 goto finally;
2354 }
2355
2356 status = ocfs2_compute_replay_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002357 if (status < 0)
2358 mlog_errno(status);
2359
2360finally:
2361 if (local_alloc)
2362 kfree(local_alloc);
2363
2364 mlog_exit(status);
2365 return status;
2366}
2367
2368/*
2369 * The routine gets called from dismount or close whenever a dismount on
2370 * volume is requested and the osb open count becomes 1.
2371 * It will remove the osb from the global list and also free up all the
2372 * initialized resources and fileobject.
2373 */
2374static void ocfs2_delete_osb(struct ocfs2_super *osb)
2375{
2376 mlog_entry_void();
2377
2378 /* This function assumes that the caller has the main osb resource */
2379
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002380 ocfs2_free_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002381
Mark Fashehb4df6ed2006-02-22 17:35:08 -08002382 kfree(osb->osb_orphan_wipes);
Sunil Mushran539d8262008-07-14 17:31:10 -07002383 kfree(osb->slot_recovery_generations);
Mark Fashehccd979b2005-12-15 14:31:24 -08002384 /* FIXME
2385 * This belongs in journal shutdown, but because we have to
2386 * allocate osb->journal at the start of ocfs2_initalize_osb(),
2387 * we free it here.
2388 */
2389 kfree(osb->journal);
2390 if (osb->local_alloc_copy)
2391 kfree(osb->local_alloc_copy);
2392 kfree(osb->uuid_str);
2393 ocfs2_put_dlm_debug(osb->osb_dlm_debug);
2394 memset(osb, 0, sizeof(struct ocfs2_super));
2395
2396 mlog_exit_void();
2397}
2398
2399/* Put OCFS2 into a readonly state, or (if the user specifies it),
2400 * panic(). We do not support continue-on-error operation. */
2401static void ocfs2_handle_error(struct super_block *sb)
2402{
2403 struct ocfs2_super *osb = OCFS2_SB(sb);
2404
2405 if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_PANIC)
2406 panic("OCFS2: (device %s): panic forced after error\n",
2407 sb->s_id);
2408
2409 ocfs2_set_osb_flag(osb, OCFS2_OSB_ERROR_FS);
2410
2411 if (sb->s_flags & MS_RDONLY &&
2412 (ocfs2_is_soft_readonly(osb) ||
2413 ocfs2_is_hard_readonly(osb)))
2414 return;
2415
2416 printk(KERN_CRIT "File system is now read-only due to the potential "
2417 "of on-disk corruption. Please run fsck.ocfs2 once the file "
2418 "system is unmounted.\n");
2419 sb->s_flags |= MS_RDONLY;
2420 ocfs2_set_ro_flag(osb, 0);
2421}
2422
2423static char error_buf[1024];
2424
2425void __ocfs2_error(struct super_block *sb,
2426 const char *function,
2427 const char *fmt, ...)
2428{
2429 va_list args;
2430
2431 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002432 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Mark Fashehccd979b2005-12-15 14:31:24 -08002433 va_end(args);
2434
2435 /* Not using mlog here because we want to show the actual
2436 * function the error came from. */
2437 printk(KERN_CRIT "OCFS2: ERROR (device %s): %s: %s\n",
2438 sb->s_id, function, error_buf);
2439
2440 ocfs2_handle_error(sb);
2441}
2442
2443/* Handle critical errors. This is intentionally more drastic than
2444 * ocfs2_handle_error, so we only use for things like journal errors,
2445 * etc. */
2446void __ocfs2_abort(struct super_block* sb,
2447 const char *function,
2448 const char *fmt, ...)
2449{
2450 va_list args;
2451
2452 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002453 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Mark Fashehccd979b2005-12-15 14:31:24 -08002454 va_end(args);
2455
2456 printk(KERN_CRIT "OCFS2: abort (device %s): %s: %s\n",
2457 sb->s_id, function, error_buf);
2458
2459 /* We don't have the cluster support yet to go straight to
2460 * hard readonly in here. Until then, we want to keep
2461 * ocfs2_abort() so that we can at least mark critical
2462 * errors.
2463 *
2464 * TODO: This should abort the journal and alert other nodes
2465 * that our slot needs recovery. */
2466
2467 /* Force a panic(). This stinks, but it's better than letting
2468 * things continue without having a proper hard readonly
2469 * here. */
2470 OCFS2_SB(sb)->s_mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
2471 ocfs2_handle_error(sb);
2472}
2473
2474module_init(ocfs2_init);
2475module_exit(ocfs2_exit);