blob: 86f500c5d63d4e176f5f02b77999ea300cfb2593 [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);
1794 if (status < 0) {
1795 mlog_errno(status);
1796 goto leave;
1797 }
1798
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001799 if (ocfs2_mount_local(osb))
1800 goto leave;
1801
Mark Fashehccd979b2005-12-15 14:31:24 -08001802leave:
1803 if (unlock_super)
1804 ocfs2_super_unlock(osb, 1);
1805
1806 mlog_exit(status);
1807 return status;
1808}
1809
Mark Fashehccd979b2005-12-15 14:31:24 -08001810static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
1811{
Joel Becker286eaa92008-02-01 15:03:57 -08001812 int tmp, hangup_needed = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001813 struct ocfs2_super *osb = NULL;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001814 char nodestr[8];
Mark Fashehccd979b2005-12-15 14:31:24 -08001815
1816 mlog_entry("(0x%p)\n", sb);
1817
1818 BUG_ON(!sb);
1819 osb = OCFS2_SB(sb);
1820 BUG_ON(!osb);
1821
Sunil Mushran50397502008-12-17 14:17:43 -08001822 debugfs_remove(osb->osb_ctxt);
1823
Jan Kara19ece542008-08-21 20:13:17 +02001824 ocfs2_disable_quotas(osb);
1825
Mark Fashehccd979b2005-12-15 14:31:24 -08001826 ocfs2_shutdown_local_alloc(osb);
1827
1828 ocfs2_truncate_log_shutdown(osb);
1829
Srinivas Eeda83273932009-06-03 17:02:55 -07001830 ocfs2_orphan_scan_stop(osb);
1831
Joel Becker553abd02008-02-01 12:03:57 -08001832 /* This will disable recovery and flush any recovery work. */
1833 ocfs2_recovery_exit(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001834
1835 ocfs2_journal_shutdown(osb);
1836
1837 ocfs2_sync_blockdev(sb);
1838
Joel Becker4670c462008-02-01 14:39:35 -08001839 /* No cluster connection means we've failed during mount, so skip
1840 * all the steps which depended on that to complete. */
1841 if (osb->cconn) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001842 tmp = ocfs2_super_lock(osb, 1);
1843 if (tmp < 0) {
1844 mlog_errno(tmp);
1845 return;
1846 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001847 }
1848
Mark Fasheh19b613d2007-10-04 14:47:09 -07001849 if (osb->slot_num != OCFS2_INVALID_SLOT)
1850 ocfs2_put_slot(osb);
1851
Joel Becker4670c462008-02-01 14:39:35 -08001852 if (osb->cconn)
Mark Fasheh19b613d2007-10-04 14:47:09 -07001853 ocfs2_super_unlock(osb, 1);
1854
Mark Fashehccd979b2005-12-15 14:31:24 -08001855 ocfs2_release_system_inodes(osb);
1856
Joel Becker6953b4c2008-01-29 16:59:56 -08001857 /*
Joel Becker6953b4c2008-01-29 16:59:56 -08001858 * If we're dismounting due to mount error, mount.ocfs2 will clean
1859 * up heartbeat. If we're a local mount, there is no heartbeat.
1860 * If we failed before we got a uuid_str yet, we can't stop
1861 * heartbeat. Otherwise, do it.
1862 */
1863 if (!mnt_err && !ocfs2_mount_local(osb) && osb->uuid_str)
Joel Becker286eaa92008-02-01 15:03:57 -08001864 hangup_needed = 1;
1865
1866 if (osb->cconn)
1867 ocfs2_dlm_shutdown(osb, hangup_needed);
1868
Joel Becker73be1922009-01-06 14:57:08 -08001869 ocfs2_blockcheck_stats_debugfs_remove(&osb->osb_ecc_stats);
Joel Becker286eaa92008-02-01 15:03:57 -08001870 debugfs_remove(osb->osb_debug_root);
1871
1872 if (hangup_needed)
Joel Becker6953b4c2008-01-29 16:59:56 -08001873 ocfs2_cluster_hangup(osb->uuid_str, strlen(osb->uuid_str));
Mark Fashehccd979b2005-12-15 14:31:24 -08001874
1875 atomic_set(&osb->vol_state, VOLUME_DISMOUNTED);
1876
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001877 if (ocfs2_mount_local(osb))
1878 snprintf(nodestr, sizeof(nodestr), "local");
1879 else
Joel Becker19fdb622008-01-30 15:38:24 -08001880 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001881
1882 printk(KERN_INFO "ocfs2: Unmounting device (%s) on (node %s)\n",
1883 osb->dev_str, nodestr);
Mark Fashehccd979b2005-12-15 14:31:24 -08001884
1885 ocfs2_delete_osb(osb);
1886 kfree(osb);
1887 sb->s_dev = 0;
1888 sb->s_fs_info = NULL;
1889}
1890
1891static int ocfs2_setup_osb_uuid(struct ocfs2_super *osb, const unsigned char *uuid,
1892 unsigned uuid_bytes)
1893{
1894 int i, ret;
1895 char *ptr;
1896
1897 BUG_ON(uuid_bytes != OCFS2_VOL_UUID_LEN);
1898
Robert P. J. Daycd861282006-12-13 00:34:52 -08001899 osb->uuid_str = kzalloc(OCFS2_VOL_UUID_LEN * 2 + 1, GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001900 if (osb->uuid_str == NULL)
1901 return -ENOMEM;
1902
Mark Fashehccd979b2005-12-15 14:31:24 -08001903 for (i = 0, ptr = osb->uuid_str; i < OCFS2_VOL_UUID_LEN; i++) {
1904 /* print with null */
1905 ret = snprintf(ptr, 3, "%02X", uuid[i]);
1906 if (ret != 2) /* drop super cleans up */
1907 return -EINVAL;
1908 /* then only advance past the last char */
1909 ptr += 2;
1910 }
1911
1912 return 0;
1913}
1914
1915static int ocfs2_initialize_super(struct super_block *sb,
1916 struct buffer_head *bh,
Joel Becker73be1922009-01-06 14:57:08 -08001917 int sector_size,
1918 struct ocfs2_blockcheck_stats *stats)
Mark Fashehccd979b2005-12-15 14:31:24 -08001919{
Denis Chengbddb8eb32007-09-27 02:10:04 +08001920 int status;
Mark Fasheh5a254032007-07-20 12:56:16 -07001921 int i, cbits, bbits;
1922 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001923 struct inode *inode = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001924 struct ocfs2_journal *journal;
1925 __le32 uuid_net_key;
1926 struct ocfs2_super *osb;
1927
1928 mlog_entry_void();
1929
Robert P. J. Daycd861282006-12-13 00:34:52 -08001930 osb = kzalloc(sizeof(struct ocfs2_super), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001931 if (!osb) {
1932 status = -ENOMEM;
1933 mlog_errno(status);
1934 goto bail;
1935 }
1936
1937 sb->s_fs_info = osb;
1938 sb->s_op = &ocfs2_sops;
1939 sb->s_export_op = &ocfs2_export_ops;
Jan Kara19ece542008-08-21 20:13:17 +02001940 sb->s_qcop = &ocfs2_quotactl_ops;
1941 sb->dq_op = &ocfs2_quota_operations;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001942 sb->s_xattr = ocfs2_xattr_handlers;
Mark Fashehe0dceaf2007-08-09 16:52:30 -07001943 sb->s_time_gran = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001944 sb->s_flags |= MS_NOATIME;
1945 /* this is needed to support O_LARGEFILE */
Mark Fasheh5a254032007-07-20 12:56:16 -07001946 cbits = le32_to_cpu(di->id2.i_super.s_clustersize_bits);
1947 bbits = le32_to_cpu(di->id2.i_super.s_blocksize_bits);
1948 sb->s_maxbytes = ocfs2_max_file_offset(bbits, cbits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001949
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001950 osb->osb_dx_mask = (1 << (cbits - bbits)) - 1;
1951
1952 for (i = 0; i < 3; i++)
1953 osb->osb_dx_seed[i] = le32_to_cpu(di->id2.i_super.s_dx_seed[i]);
1954 osb->osb_dx_seed[3] = le32_to_cpu(di->id2.i_super.s_uuid_hash);
1955
Mark Fashehccd979b2005-12-15 14:31:24 -08001956 osb->sb = sb;
1957 /* Save off for ocfs2_rw_direct */
1958 osb->s_sectsize_bits = blksize_bits(sector_size);
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01001959 BUG_ON(!osb->s_sectsize_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001960
Mark Fasheh34d024f2007-09-24 15:56:19 -07001961 spin_lock_init(&osb->dc_task_lock);
1962 init_waitqueue_head(&osb->dc_event);
1963 osb->dc_work_sequence = 0;
1964 osb->dc_wake_sequence = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001965 INIT_LIST_HEAD(&osb->blocked_lock_list);
1966 osb->blocked_lock_count = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001967 spin_lock_init(&osb->osb_lock);
Tao Mac8b9cf92009-02-24 17:40:26 -08001968 spin_lock_init(&osb->osb_xattr_lock);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001969 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001970
1971 atomic_set(&osb->alloc_stats.moves, 0);
1972 atomic_set(&osb->alloc_stats.local_data, 0);
1973 atomic_set(&osb->alloc_stats.bitmap_data, 0);
1974 atomic_set(&osb->alloc_stats.bg_allocs, 0);
1975 atomic_set(&osb->alloc_stats.bg_extends, 0);
1976
Joel Becker73be1922009-01-06 14:57:08 -08001977 /* Copy the blockcheck stats from the superblock probe */
1978 osb->osb_ecc_stats = *stats;
1979
Mark Fashehccd979b2005-12-15 14:31:24 -08001980 ocfs2_init_node_maps(osb);
1981
1982 snprintf(osb->dev_str, sizeof(osb->dev_str), "%u,%u",
1983 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1984
Joel Becker553abd02008-02-01 12:03:57 -08001985 status = ocfs2_recovery_init(osb);
1986 if (status) {
1987 mlog(ML_ERROR, "Unable to initialize recovery state\n");
1988 mlog_errno(status);
1989 goto bail;
1990 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001991
Srinivas Eeda83273932009-06-03 17:02:55 -07001992 status = ocfs2_orphan_scan_init(osb);
1993 if (status) {
1994 mlog(ML_ERROR, "Unable to initialize delayed orphan scan\n");
1995 mlog_errno(status);
1996 goto bail;
1997 }
1998
Mark Fashehccd979b2005-12-15 14:31:24 -08001999 init_waitqueue_head(&osb->checkpoint_event);
2000 atomic_set(&osb->needs_checkpoint, 0);
2001
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002002 osb->s_atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
2003
Mark Fashehccd979b2005-12-15 14:31:24 -08002004 osb->slot_num = OCFS2_INVALID_SLOT;
2005
Tiger Yang8154da32008-08-18 17:11:46 +08002006 osb->s_xattr_inline_size = le16_to_cpu(
2007 di->id2.i_super.s_xattr_inline_size);
Tiger Yangfdd77702008-08-18 17:08:55 +08002008
Mark Fashehccd979b2005-12-15 14:31:24 -08002009 osb->local_alloc_state = OCFS2_LA_UNUSED;
2010 osb->local_alloc_bh = NULL;
Mark Fasheh9c7af402008-07-28 18:02:53 -07002011 INIT_DELAYED_WORK(&osb->la_enable_wq, ocfs2_la_enable_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08002012
Mark Fashehccd979b2005-12-15 14:31:24 -08002013 init_waitqueue_head(&osb->osb_mount_event);
2014
2015 osb->vol_label = kmalloc(OCFS2_MAX_VOL_LABEL_LEN, GFP_KERNEL);
2016 if (!osb->vol_label) {
2017 mlog(ML_ERROR, "unable to alloc vol label\n");
2018 status = -ENOMEM;
2019 goto bail;
2020 }
2021
Mark Fashehccd979b2005-12-15 14:31:24 -08002022 osb->max_slots = le16_to_cpu(di->id2.i_super.s_max_slots);
2023 if (osb->max_slots > OCFS2_MAX_SLOTS || osb->max_slots == 0) {
2024 mlog(ML_ERROR, "Invalid number of node slots (%u)\n",
2025 osb->max_slots);
2026 status = -EINVAL;
2027 goto bail;
2028 }
Sunil Mushran781ee3e2006-04-27 16:41:31 -07002029 mlog(0, "max_slots for this device: %u\n", osb->max_slots);
Mark Fashehccd979b2005-12-15 14:31:24 -08002030
Sunil Mushran539d8262008-07-14 17:31:10 -07002031 osb->slot_recovery_generations =
2032 kcalloc(osb->max_slots, sizeof(*osb->slot_recovery_generations),
2033 GFP_KERNEL);
2034 if (!osb->slot_recovery_generations) {
2035 status = -ENOMEM;
2036 mlog_errno(status);
2037 goto bail;
2038 }
2039
Mark Fashehb4df6ed2006-02-22 17:35:08 -08002040 init_waitqueue_head(&osb->osb_wipe_event);
2041 osb->osb_orphan_wipes = kcalloc(osb->max_slots,
2042 sizeof(*osb->osb_orphan_wipes),
2043 GFP_KERNEL);
2044 if (!osb->osb_orphan_wipes) {
2045 status = -ENOMEM;
2046 mlog_errno(status);
2047 goto bail;
2048 }
2049
Mark Fashehccd979b2005-12-15 14:31:24 -08002050 osb->s_feature_compat =
2051 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_compat);
2052 osb->s_feature_ro_compat =
2053 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_ro_compat);
2054 osb->s_feature_incompat =
2055 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_incompat);
2056
2057 if ((i = OCFS2_HAS_INCOMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_INCOMPAT_SUPP))) {
2058 mlog(ML_ERROR, "couldn't mount because of unsupported "
2059 "optional features (%x).\n", i);
2060 status = -EINVAL;
2061 goto bail;
2062 }
2063 if (!(osb->sb->s_flags & MS_RDONLY) &&
2064 (i = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP))) {
2065 mlog(ML_ERROR, "couldn't mount RDWR because of "
2066 "unsupported optional features (%x).\n", i);
2067 status = -EINVAL;
2068 goto bail;
2069 }
2070
Joel Beckerb61817e2008-02-01 15:08:23 -08002071 if (ocfs2_userspace_stack(osb)) {
2072 memcpy(osb->osb_cluster_stack,
2073 OCFS2_RAW_SB(di)->s_cluster_info.ci_stack,
2074 OCFS2_STACK_LABEL_LEN);
2075 osb->osb_cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
2076 if (strlen(osb->osb_cluster_stack) != OCFS2_STACK_LABEL_LEN) {
2077 mlog(ML_ERROR,
2078 "couldn't mount because of an invalid "
2079 "cluster stack label (%s) \n",
2080 osb->osb_cluster_stack);
2081 status = -EINVAL;
2082 goto bail;
2083 }
2084 } else {
2085 /* The empty string is identical with classic tools that
2086 * don't know about s_cluster_info. */
2087 osb->osb_cluster_stack[0] = '\0';
2088 }
2089
Mark Fashehccd979b2005-12-15 14:31:24 -08002090 get_random_bytes(&osb->s_next_generation, sizeof(u32));
2091
2092 /* FIXME
2093 * This should be done in ocfs2_journal_init(), but unknown
2094 * ordering issues will cause the filesystem to crash.
2095 * If anyone wants to figure out what part of the code
2096 * refers to osb->journal before ocfs2_journal_init() is run,
2097 * be my guest.
2098 */
2099 /* initialize our journal structure */
2100
Robert P. J. Daycd861282006-12-13 00:34:52 -08002101 journal = kzalloc(sizeof(struct ocfs2_journal), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08002102 if (!journal) {
2103 mlog(ML_ERROR, "unable to alloc journal\n");
2104 status = -ENOMEM;
2105 goto bail;
2106 }
2107 osb->journal = journal;
2108 journal->j_osb = osb;
2109
2110 atomic_set(&journal->j_num_trans, 0);
2111 init_rwsem(&journal->j_trans_barrier);
2112 init_waitqueue_head(&journal->j_checkpointed);
2113 spin_lock_init(&journal->j_lock);
2114 journal->j_trans_id = (unsigned long) 1;
2115 INIT_LIST_HEAD(&journal->j_la_cleanups);
David Howellsc4028952006-11-22 14:57:56 +00002116 INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
Mark Fashehccd979b2005-12-15 14:31:24 -08002117 journal->j_state = OCFS2_JOURNAL_FREE;
2118
Jan Karaea455f82009-01-12 23:20:31 +01002119 INIT_WORK(&osb->dentry_lock_work, ocfs2_drop_dl_inodes);
2120 osb->dentry_lock_list = NULL;
2121
Mark Fashehccd979b2005-12-15 14:31:24 -08002122 /* get some pseudo constants for clustersize bits */
2123 osb->s_clustersize_bits =
2124 le32_to_cpu(di->id2.i_super.s_clustersize_bits);
2125 osb->s_clustersize = 1 << osb->s_clustersize_bits;
2126 mlog(0, "clusterbits=%d\n", osb->s_clustersize_bits);
2127
2128 if (osb->s_clustersize < OCFS2_MIN_CLUSTERSIZE ||
2129 osb->s_clustersize > OCFS2_MAX_CLUSTERSIZE) {
2130 mlog(ML_ERROR, "Volume has invalid cluster size (%d)\n",
2131 osb->s_clustersize);
2132 status = -EINVAL;
2133 goto bail;
2134 }
2135
2136 if (ocfs2_clusters_to_blocks(osb->sb, le32_to_cpu(di->i_clusters) - 1)
2137 > (u32)~0UL) {
2138 mlog(ML_ERROR, "Volume might try to write to blocks beyond "
2139 "what jbd can address in 32 bits.\n");
2140 status = -EINVAL;
2141 goto bail;
2142 }
2143
2144 if (ocfs2_setup_osb_uuid(osb, di->id2.i_super.s_uuid,
2145 sizeof(di->id2.i_super.s_uuid))) {
2146 mlog(ML_ERROR, "Out of memory trying to setup our uuid.\n");
2147 status = -ENOMEM;
2148 goto bail;
2149 }
2150
Mark Fasheh78427042006-05-04 12:03:26 -07002151 memcpy(&uuid_net_key, di->id2.i_super.s_uuid, sizeof(uuid_net_key));
Mark Fashehccd979b2005-12-15 14:31:24 -08002152
2153 strncpy(osb->vol_label, di->id2.i_super.s_label, 63);
2154 osb->vol_label[63] = '\0';
2155 osb->root_blkno = le64_to_cpu(di->id2.i_super.s_root_blkno);
2156 osb->system_dir_blkno = le64_to_cpu(di->id2.i_super.s_system_dir_blkno);
2157 osb->first_cluster_group_blkno =
2158 le64_to_cpu(di->id2.i_super.s_first_cluster_group);
2159 osb->fs_generation = le32_to_cpu(di->i_fs_generation);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002160 osb->uuid_hash = le32_to_cpu(di->id2.i_super.s_uuid_hash);
Mark Fashehccd979b2005-12-15 14:31:24 -08002161 mlog(0, "vol_label: %s\n", osb->vol_label);
2162 mlog(0, "uuid: %s\n", osb->uuid_str);
Mark Fashehb06970532006-03-03 10:24:33 -08002163 mlog(0, "root_blkno=%llu, system_dir_blkno=%llu\n",
2164 (unsigned long long)osb->root_blkno,
2165 (unsigned long long)osb->system_dir_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002166
2167 osb->osb_dlm_debug = ocfs2_new_dlm_debug();
2168 if (!osb->osb_dlm_debug) {
2169 status = -ENOMEM;
2170 mlog_errno(status);
2171 goto bail;
2172 }
2173
2174 atomic_set(&osb->vol_state, VOLUME_INIT);
2175
2176 /* load root, system_dir, and all global system inodes */
2177 status = ocfs2_init_global_system_inodes(osb);
2178 if (status < 0) {
2179 mlog_errno(status);
2180 goto bail;
2181 }
2182
2183 /*
2184 * global bitmap
2185 */
2186 inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
2187 OCFS2_INVALID_SLOT);
2188 if (!inode) {
2189 status = -EINVAL;
2190 mlog_errno(status);
2191 goto bail;
2192 }
2193
2194 osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08002195 iput(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08002196
Tao Mae9d578a2007-12-18 15:46:10 +08002197 osb->bitmap_cpg = ocfs2_group_bitmap_size(sb) * 8;
Mark Fashehccd979b2005-12-15 14:31:24 -08002198
2199 status = ocfs2_init_slot_info(osb);
2200 if (status < 0) {
2201 mlog_errno(status);
2202 goto bail;
2203 }
2204
Mark Fashehccd979b2005-12-15 14:31:24 -08002205bail:
2206 mlog_exit(status);
2207 return status;
2208}
2209
2210/*
2211 * will return: -EAGAIN if it is ok to keep searching for superblocks
2212 * -EINVAL if there is a bad superblock
2213 * 0 on success
2214 */
2215static int ocfs2_verify_volume(struct ocfs2_dinode *di,
2216 struct buffer_head *bh,
Joel Becker73be1922009-01-06 14:57:08 -08002217 u32 blksz,
2218 struct ocfs2_blockcheck_stats *stats)
Mark Fashehccd979b2005-12-15 14:31:24 -08002219{
2220 int status = -EAGAIN;
2221
2222 mlog_entry_void();
2223
2224 if (memcmp(di->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
2225 strlen(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) {
Joel Beckerd030cc92008-12-11 15:04:14 -08002226 /* We have to do a raw check of the feature here */
2227 if (le32_to_cpu(di->id2.i_super.s_feature_incompat) &
2228 OCFS2_FEATURE_INCOMPAT_META_ECC) {
2229 status = ocfs2_block_check_validate(bh->b_data,
2230 bh->b_size,
Joel Becker73be1922009-01-06 14:57:08 -08002231 &di->i_check,
2232 stats);
Joel Beckerd030cc92008-12-11 15:04:14 -08002233 if (status)
2234 goto out;
2235 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002236 status = -EINVAL;
2237 if ((1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits)) != blksz) {
2238 mlog(ML_ERROR, "found superblock with incorrect block "
2239 "size: found %u, should be %u\n",
2240 1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits),
2241 blksz);
2242 } else if (le16_to_cpu(di->id2.i_super.s_major_rev_level) !=
2243 OCFS2_MAJOR_REV_LEVEL ||
2244 le16_to_cpu(di->id2.i_super.s_minor_rev_level) !=
2245 OCFS2_MINOR_REV_LEVEL) {
2246 mlog(ML_ERROR, "found superblock with bad version: "
2247 "found %u.%u, should be %u.%u\n",
2248 le16_to_cpu(di->id2.i_super.s_major_rev_level),
2249 le16_to_cpu(di->id2.i_super.s_minor_rev_level),
2250 OCFS2_MAJOR_REV_LEVEL,
2251 OCFS2_MINOR_REV_LEVEL);
2252 } else if (bh->b_blocknr != le64_to_cpu(di->i_blkno)) {
2253 mlog(ML_ERROR, "bad block number on superblock: "
Mark Fashehb06970532006-03-03 10:24:33 -08002254 "found %llu, should be %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07002255 (unsigned long long)le64_to_cpu(di->i_blkno),
Mark Fashehb06970532006-03-03 10:24:33 -08002256 (unsigned long long)bh->b_blocknr);
Mark Fashehccd979b2005-12-15 14:31:24 -08002257 } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
2258 le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
2259 mlog(ML_ERROR, "bad cluster size found: %u\n",
2260 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
2261 } else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
2262 mlog(ML_ERROR, "bad root_blkno: 0\n");
2263 } else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
2264 mlog(ML_ERROR, "bad system_dir_blkno: 0\n");
2265 } else if (le16_to_cpu(di->id2.i_super.s_max_slots) > OCFS2_MAX_SLOTS) {
2266 mlog(ML_ERROR,
2267 "Superblock slots found greater than file system "
2268 "maximum: found %u, max %u\n",
2269 le16_to_cpu(di->id2.i_super.s_max_slots),
2270 OCFS2_MAX_SLOTS);
2271 } else {
2272 /* found it! */
2273 status = 0;
2274 }
2275 }
2276
Joel Beckerd030cc92008-12-11 15:04:14 -08002277out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002278 mlog_exit(status);
2279 return status;
2280}
2281
2282static int ocfs2_check_volume(struct ocfs2_super *osb)
2283{
Denis Chengbddb8eb32007-09-27 02:10:04 +08002284 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08002285 int dirty;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002286 int local;
Mark Fashehccd979b2005-12-15 14:31:24 -08002287 struct ocfs2_dinode *local_alloc = NULL; /* only used if we
2288 * recover
2289 * ourselves. */
2290
2291 mlog_entry_void();
2292
2293 /* Init our journal object. */
2294 status = ocfs2_journal_init(osb->journal, &dirty);
2295 if (status < 0) {
2296 mlog(ML_ERROR, "Could not initialize journal!\n");
2297 goto finally;
2298 }
2299
2300 /* If the journal was unmounted cleanly then we don't want to
2301 * recover anything. Otherwise, journal_load will do that
2302 * dirty work for us :) */
2303 if (!dirty) {
2304 status = ocfs2_journal_wipe(osb->journal, 0);
2305 if (status < 0) {
2306 mlog_errno(status);
2307 goto finally;
2308 }
2309 } else {
2310 mlog(ML_NOTICE, "File system was not unmounted cleanly, "
2311 "recovering volume.\n");
2312 }
2313
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002314 local = ocfs2_mount_local(osb);
2315
Mark Fashehccd979b2005-12-15 14:31:24 -08002316 /* will play back anything left in the journal. */
Sunil Mushran539d8262008-07-14 17:31:10 -07002317 status = ocfs2_journal_load(osb->journal, local, dirty);
Wengang Wang01af4822008-06-10 14:24:48 +08002318 if (status < 0) {
2319 mlog(ML_ERROR, "ocfs2 journal load failed! %d\n", status);
2320 goto finally;
2321 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002322
2323 if (dirty) {
2324 /* recover my local alloc if we didn't unmount cleanly. */
2325 status = ocfs2_begin_local_alloc_recovery(osb,
2326 osb->slot_num,
2327 &local_alloc);
2328 if (status < 0) {
2329 mlog_errno(status);
2330 goto finally;
2331 }
2332 /* we complete the recovery process after we've marked
2333 * ourselves as mounted. */
2334 }
2335
2336 mlog(0, "Journal loaded.\n");
2337
2338 status = ocfs2_load_local_alloc(osb);
2339 if (status < 0) {
2340 mlog_errno(status);
2341 goto finally;
2342 }
2343
2344 if (dirty) {
2345 /* Recovery will be completed after we've mounted the
2346 * rest of the volume. */
2347 osb->dirty = 1;
2348 osb->local_alloc_copy = local_alloc;
2349 local_alloc = NULL;
2350 }
2351
2352 /* go through each journal, trylock it and if you get the
2353 * lock, and it's marked as dirty, set the bit in the recover
2354 * map and launch a recovery thread for it. */
2355 status = ocfs2_mark_dead_nodes(osb);
Srinivas Eeda9140db02009-03-06 14:21:46 -08002356 if (status < 0) {
2357 mlog_errno(status);
2358 goto finally;
2359 }
2360
2361 status = ocfs2_compute_replay_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002362 if (status < 0)
2363 mlog_errno(status);
2364
2365finally:
2366 if (local_alloc)
2367 kfree(local_alloc);
2368
2369 mlog_exit(status);
2370 return status;
2371}
2372
2373/*
2374 * The routine gets called from dismount or close whenever a dismount on
2375 * volume is requested and the osb open count becomes 1.
2376 * It will remove the osb from the global list and also free up all the
2377 * initialized resources and fileobject.
2378 */
2379static void ocfs2_delete_osb(struct ocfs2_super *osb)
2380{
2381 mlog_entry_void();
2382
2383 /* This function assumes that the caller has the main osb resource */
2384
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002385 ocfs2_free_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002386
Mark Fashehb4df6ed2006-02-22 17:35:08 -08002387 kfree(osb->osb_orphan_wipes);
Sunil Mushran539d8262008-07-14 17:31:10 -07002388 kfree(osb->slot_recovery_generations);
Mark Fashehccd979b2005-12-15 14:31:24 -08002389 /* FIXME
2390 * This belongs in journal shutdown, but because we have to
2391 * allocate osb->journal at the start of ocfs2_initalize_osb(),
2392 * we free it here.
2393 */
2394 kfree(osb->journal);
2395 if (osb->local_alloc_copy)
2396 kfree(osb->local_alloc_copy);
2397 kfree(osb->uuid_str);
2398 ocfs2_put_dlm_debug(osb->osb_dlm_debug);
2399 memset(osb, 0, sizeof(struct ocfs2_super));
2400
2401 mlog_exit_void();
2402}
2403
2404/* Put OCFS2 into a readonly state, or (if the user specifies it),
2405 * panic(). We do not support continue-on-error operation. */
2406static void ocfs2_handle_error(struct super_block *sb)
2407{
2408 struct ocfs2_super *osb = OCFS2_SB(sb);
2409
2410 if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_PANIC)
2411 panic("OCFS2: (device %s): panic forced after error\n",
2412 sb->s_id);
2413
2414 ocfs2_set_osb_flag(osb, OCFS2_OSB_ERROR_FS);
2415
2416 if (sb->s_flags & MS_RDONLY &&
2417 (ocfs2_is_soft_readonly(osb) ||
2418 ocfs2_is_hard_readonly(osb)))
2419 return;
2420
2421 printk(KERN_CRIT "File system is now read-only due to the potential "
2422 "of on-disk corruption. Please run fsck.ocfs2 once the file "
2423 "system is unmounted.\n");
2424 sb->s_flags |= MS_RDONLY;
2425 ocfs2_set_ro_flag(osb, 0);
2426}
2427
2428static char error_buf[1024];
2429
2430void __ocfs2_error(struct super_block *sb,
2431 const char *function,
2432 const char *fmt, ...)
2433{
2434 va_list args;
2435
2436 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002437 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Mark Fashehccd979b2005-12-15 14:31:24 -08002438 va_end(args);
2439
2440 /* Not using mlog here because we want to show the actual
2441 * function the error came from. */
2442 printk(KERN_CRIT "OCFS2: ERROR (device %s): %s: %s\n",
2443 sb->s_id, function, error_buf);
2444
2445 ocfs2_handle_error(sb);
2446}
2447
2448/* Handle critical errors. This is intentionally more drastic than
2449 * ocfs2_handle_error, so we only use for things like journal errors,
2450 * etc. */
2451void __ocfs2_abort(struct super_block* sb,
2452 const char *function,
2453 const char *fmt, ...)
2454{
2455 va_list args;
2456
2457 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002458 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Mark Fashehccd979b2005-12-15 14:31:24 -08002459 va_end(args);
2460
2461 printk(KERN_CRIT "OCFS2: abort (device %s): %s: %s\n",
2462 sb->s_id, function, error_buf);
2463
2464 /* We don't have the cluster support yet to go straight to
2465 * hard readonly in here. Until then, we want to keep
2466 * ocfs2_abort() so that we can at least mark critical
2467 * errors.
2468 *
2469 * TODO: This should abort the journal and alert other nodes
2470 * that our slot needs recovery. */
2471
2472 /* Force a panic(). This stinks, but it's better than letting
2473 * things continue without having a proper hard readonly
2474 * here. */
2475 OCFS2_SB(sb)->s_mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
2476 ocfs2_handle_error(sb);
2477}
2478
2479module_init(ocfs2_init);
2480module_exit(ocfs2_exit);