blob: d05f3caec410baa87fa00c00e342c4f5872fefee [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,
121 u32 sectsize);
122static int ocfs2_initialize_super(struct super_block *sb,
123 struct buffer_head *bh,
124 int sector_size);
125static int ocfs2_get_sector(struct super_block *sb,
126 struct buffer_head **bh,
127 int block,
128 int sect_size);
129static void ocfs2_write_super(struct super_block *sb);
130static struct inode *ocfs2_alloc_inode(struct super_block *sb);
131static void ocfs2_destroy_inode(struct inode *inode);
Jan Kara19ece542008-08-21 20:13:17 +0200132static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend);
133static int ocfs2_enable_quotas(struct ocfs2_super *osb);
134static void ocfs2_disable_quotas(struct ocfs2_super *osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800135
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800136static const struct super_operations ocfs2_sops = {
Mark Fashehccd979b2005-12-15 14:31:24 -0800137 .statfs = ocfs2_statfs,
138 .alloc_inode = ocfs2_alloc_inode,
139 .destroy_inode = ocfs2_destroy_inode,
140 .drop_inode = ocfs2_drop_inode,
141 .clear_inode = ocfs2_clear_inode,
142 .delete_inode = ocfs2_delete_inode,
143 .sync_fs = ocfs2_sync_fs,
144 .write_super = ocfs2_write_super,
145 .put_super = ocfs2_put_super,
146 .remount_fs = ocfs2_remount,
Sunil Mushrand5500712007-09-06 13:34:16 -0700147 .show_options = ocfs2_show_options,
Jan Kara9e33d692008-08-25 19:56:50 +0200148 .quota_read = ocfs2_quota_read,
149 .quota_write = ocfs2_quota_write,
Mark Fashehccd979b2005-12-15 14:31:24 -0800150};
151
152enum {
153 Opt_barrier,
154 Opt_err_panic,
155 Opt_err_ro,
156 Opt_intr,
157 Opt_nointr,
158 Opt_hb_none,
159 Opt_hb_local,
160 Opt_data_ordered,
161 Opt_data_writeback,
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800162 Opt_atime_quantum,
Sunil Mushranbaf46612007-06-18 17:00:24 -0700163 Opt_slot,
Mark Fashehd147b3d2007-11-07 14:40:36 -0800164 Opt_commit,
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800165 Opt_localalloc,
Mark Fasheh53fc6222007-12-20 16:49:04 -0800166 Opt_localflocks,
Joel Beckerb61817e2008-02-01 15:08:23 -0800167 Opt_stack,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800168 Opt_user_xattr,
169 Opt_nouser_xattr,
Joel Becker12462f12008-09-03 20:03:40 -0700170 Opt_inode64,
Tiger Yanga68979b2008-11-14 11:17:52 +0800171 Opt_acl,
172 Opt_noacl,
Jan Kara19ece542008-08-21 20:13:17 +0200173 Opt_usrquota,
174 Opt_grpquota,
Mark Fashehccd979b2005-12-15 14:31:24 -0800175 Opt_err,
176};
177
Steven Whitehousea447c092008-10-13 10:46:57 +0100178static const match_table_t tokens = {
Mark Fashehccd979b2005-12-15 14:31:24 -0800179 {Opt_barrier, "barrier=%u"},
180 {Opt_err_panic, "errors=panic"},
181 {Opt_err_ro, "errors=remount-ro"},
182 {Opt_intr, "intr"},
183 {Opt_nointr, "nointr"},
184 {Opt_hb_none, OCFS2_HB_NONE},
185 {Opt_hb_local, OCFS2_HB_LOCAL},
186 {Opt_data_ordered, "data=ordered"},
187 {Opt_data_writeback, "data=writeback"},
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800188 {Opt_atime_quantum, "atime_quantum=%u"},
Sunil Mushranbaf46612007-06-18 17:00:24 -0700189 {Opt_slot, "preferred_slot=%u"},
Mark Fashehd147b3d2007-11-07 14:40:36 -0800190 {Opt_commit, "commit=%u"},
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800191 {Opt_localalloc, "localalloc=%d"},
Mark Fasheh53fc6222007-12-20 16:49:04 -0800192 {Opt_localflocks, "localflocks"},
Joel Beckerb61817e2008-02-01 15:08:23 -0800193 {Opt_stack, "cluster_stack=%s"},
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800194 {Opt_user_xattr, "user_xattr"},
195 {Opt_nouser_xattr, "nouser_xattr"},
Joel Becker12462f12008-09-03 20:03:40 -0700196 {Opt_inode64, "inode64"},
Tiger Yanga68979b2008-11-14 11:17:52 +0800197 {Opt_acl, "acl"},
198 {Opt_noacl, "noacl"},
Jan Kara19ece542008-08-21 20:13:17 +0200199 {Opt_usrquota, "usrquota"},
200 {Opt_grpquota, "grpquota"},
Mark Fashehccd979b2005-12-15 14:31:24 -0800201 {Opt_err, NULL}
202};
203
Sunil Mushran50397502008-12-17 14:17:43 -0800204#ifdef CONFIG_DEBUG_FS
205static int ocfs2_osb_dump(struct ocfs2_super *osb, char *buf, int len)
206{
207 int out = 0;
208 int i;
209 struct ocfs2_cluster_connection *cconn = osb->cconn;
210 struct ocfs2_recovery_map *rm = osb->recovery_map;
Srinivas Eeda15633a22009-06-03 17:02:56 -0700211 struct ocfs2_orphan_scan *os;
Sunil Mushran50397502008-12-17 14:17:43 -0800212
213 out += snprintf(buf + out, len - out,
214 "%10s => Id: %-s Uuid: %-s Gen: 0x%X Label: %-s\n",
215 "Device", osb->dev_str, osb->uuid_str,
216 osb->fs_generation, osb->vol_label);
217
218 out += snprintf(buf + out, len - out,
219 "%10s => State: %d Flags: 0x%lX\n", "Volume",
220 atomic_read(&osb->vol_state), osb->osb_flags);
221
222 out += snprintf(buf + out, len - out,
223 "%10s => Block: %lu Cluster: %d\n", "Sizes",
224 osb->sb->s_blocksize, osb->s_clustersize);
225
226 out += snprintf(buf + out, len - out,
227 "%10s => Compat: 0x%X Incompat: 0x%X "
228 "ROcompat: 0x%X\n",
229 "Features", osb->s_feature_compat,
230 osb->s_feature_incompat, osb->s_feature_ro_compat);
231
232 out += snprintf(buf + out, len - out,
233 "%10s => Opts: 0x%lX AtimeQuanta: %u\n", "Mount",
234 osb->s_mount_opt, osb->s_atime_quantum);
235
236 out += snprintf(buf + out, len - out,
237 "%10s => Stack: %s Name: %*s Version: %d.%d\n",
238 "Cluster",
239 (*osb->osb_cluster_stack == '\0' ?
240 "o2cb" : osb->osb_cluster_stack),
241 cconn->cc_namelen, cconn->cc_name,
242 cconn->cc_version.pv_major, cconn->cc_version.pv_minor);
243
244 spin_lock(&osb->dc_task_lock);
245 out += snprintf(buf + out, len - out,
246 "%10s => Pid: %d Count: %lu WakeSeq: %lu "
247 "WorkSeq: %lu\n", "DownCnvt",
248 task_pid_nr(osb->dc_task), osb->blocked_lock_count,
249 osb->dc_wake_sequence, osb->dc_work_sequence);
250 spin_unlock(&osb->dc_task_lock);
251
252 spin_lock(&osb->osb_lock);
253 out += snprintf(buf + out, len - out, "%10s => Pid: %d Nodes:",
254 "Recovery",
255 (osb->recovery_thread_task ?
256 task_pid_nr(osb->recovery_thread_task) : -1));
257 if (rm->rm_used == 0)
258 out += snprintf(buf + out, len - out, " None\n");
259 else {
260 for (i = 0; i < rm->rm_used; i++)
261 out += snprintf(buf + out, len - out, " %d",
262 rm->rm_entries[i]);
263 out += snprintf(buf + out, len - out, "\n");
264 }
265 spin_unlock(&osb->osb_lock);
266
267 out += snprintf(buf + out, len - out,
268 "%10s => Pid: %d Interval: %lu Needs: %d\n", "Commit",
269 task_pid_nr(osb->commit_task), osb->osb_commit_interval,
270 atomic_read(&osb->needs_checkpoint));
271
272 out += snprintf(buf + out, len - out,
273 "%10s => State: %d NumTxns: %d TxnId: %lu\n",
274 "Journal", osb->journal->j_state,
275 atomic_read(&osb->journal->j_num_trans),
276 osb->journal->j_trans_id);
277
278 out += snprintf(buf + out, len - out,
279 "%10s => GlobalAllocs: %d LocalAllocs: %d "
280 "SubAllocs: %d LAWinMoves: %d SAExtends: %d\n",
281 "Stats",
282 atomic_read(&osb->alloc_stats.bitmap_data),
283 atomic_read(&osb->alloc_stats.local_data),
284 atomic_read(&osb->alloc_stats.bg_allocs),
285 atomic_read(&osb->alloc_stats.moves),
286 atomic_read(&osb->alloc_stats.bg_extends));
287
288 out += snprintf(buf + out, len - out,
289 "%10s => State: %u Descriptor: %llu Size: %u bits "
290 "Default: %u bits\n",
291 "LocalAlloc", osb->local_alloc_state,
292 (unsigned long long)osb->la_last_gd,
293 osb->local_alloc_bits, osb->local_alloc_default_bits);
294
295 spin_lock(&osb->osb_lock);
296 out += snprintf(buf + out, len - out,
297 "%10s => Slot: %d NumStolen: %d\n", "Steal",
298 osb->s_inode_steal_slot,
299 atomic_read(&osb->s_num_inodes_stolen));
300 spin_unlock(&osb->osb_lock);
301
302 out += snprintf(buf + out, len - out, "%10s => %3s %10s\n",
303 "Slots", "Num", "RecoGen");
304
305 for (i = 0; i < osb->max_slots; ++i) {
306 out += snprintf(buf + out, len - out,
307 "%10s %c %3d %10d\n",
308 " ",
309 (i == osb->slot_num ? '*' : ' '),
310 i, osb->slot_recovery_generations[i]);
311 }
312
Srinivas Eeda15633a22009-06-03 17:02:56 -0700313 os = &osb->osb_orphan_scan;
314 out += snprintf(buf + out, len - out, "Orphan Scan=> ");
315 out += snprintf(buf + out, len - out, "Local: %u Global: %u ",
316 os->os_count, os->os_seqno);
317 out += snprintf(buf + out, len - out, " Last Scan: %lu seconds ago\n",
318 (get_seconds() - os->os_scantime.tv_sec));
319
Sunil Mushran50397502008-12-17 14:17:43 -0800320 return out;
321}
322
323static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
324{
325 struct ocfs2_super *osb = inode->i_private;
326 char *buf = NULL;
327
328 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
329 if (!buf)
330 goto bail;
331
332 i_size_write(inode, ocfs2_osb_dump(osb, buf, PAGE_SIZE));
333
334 file->private_data = buf;
335
336 return 0;
337bail:
338 return -ENOMEM;
339}
340
341static int ocfs2_debug_release(struct inode *inode, struct file *file)
342{
343 kfree(file->private_data);
344 return 0;
345}
346
347static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
348 size_t nbytes, loff_t *ppos)
349{
350 return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
351 i_size_read(file->f_mapping->host));
352}
353#else
354static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
355{
356 return 0;
357}
358static int ocfs2_debug_release(struct inode *inode, struct file *file)
359{
360 return 0;
361}
362static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
363 size_t nbytes, loff_t *ppos)
364{
365 return 0;
366}
367#endif /* CONFIG_DEBUG_FS */
368
369static struct file_operations ocfs2_osb_debug_fops = {
370 .open = ocfs2_osb_debug_open,
371 .release = ocfs2_debug_release,
372 .read = ocfs2_debug_read,
373 .llseek = generic_file_llseek,
374};
375
Mark Fashehccd979b2005-12-15 14:31:24 -0800376/*
377 * write_super and sync_fs ripped right out of ext3.
378 */
379static void ocfs2_write_super(struct super_block *sb)
380{
Ingo Molnar7892f2f2006-01-09 15:59:25 -0800381 if (mutex_trylock(&sb->s_lock) != 0)
Mark Fashehccd979b2005-12-15 14:31:24 -0800382 BUG();
383 sb->s_dirt = 0;
384}
385
386static int ocfs2_sync_fs(struct super_block *sb, int wait)
387{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800388 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -0800389 tid_t target;
390 struct ocfs2_super *osb = OCFS2_SB(sb);
391
392 sb->s_dirt = 0;
393
394 if (ocfs2_is_hard_readonly(osb))
395 return -EROFS;
396
397 if (wait) {
398 status = ocfs2_flush_truncate_log(osb);
399 if (status < 0)
400 mlog_errno(status);
401 } else {
402 ocfs2_schedule_truncate_log_flush(osb, 0);
403 }
404
Joel Becker2b4e30f2008-09-03 20:03:41 -0700405 if (jbd2_journal_start_commit(OCFS2_SB(sb)->journal->j_journal,
406 &target)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800407 if (wait)
Joel Becker2b4e30f2008-09-03 20:03:41 -0700408 jbd2_log_wait_commit(OCFS2_SB(sb)->journal->j_journal,
409 target);
Mark Fashehccd979b2005-12-15 14:31:24 -0800410 }
411 return 0;
412}
413
Jan Kara1a224ad2008-08-20 15:43:36 +0200414static int ocfs2_need_system_inode(struct ocfs2_super *osb, int ino)
415{
416 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_USRQUOTA)
417 && (ino == USER_QUOTA_SYSTEM_INODE
418 || ino == LOCAL_USER_QUOTA_SYSTEM_INODE))
419 return 0;
420 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)
421 && (ino == GROUP_QUOTA_SYSTEM_INODE
422 || ino == LOCAL_GROUP_QUOTA_SYSTEM_INODE))
423 return 0;
424 return 1;
425}
426
Mark Fashehccd979b2005-12-15 14:31:24 -0800427static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb)
428{
429 struct inode *new = NULL;
430 int status = 0;
431 int i;
432
433 mlog_entry_void();
434
Jan Kara5fa06132008-01-11 00:11:45 +0100435 new = ocfs2_iget(osb, osb->root_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800436 if (IS_ERR(new)) {
437 status = PTR_ERR(new);
438 mlog_errno(status);
439 goto bail;
440 }
441 osb->root_inode = new;
442
Jan Kara5fa06132008-01-11 00:11:45 +0100443 new = ocfs2_iget(osb, osb->system_dir_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800444 if (IS_ERR(new)) {
445 status = PTR_ERR(new);
446 mlog_errno(status);
447 goto bail;
448 }
449 osb->sys_root_inode = new;
450
451 for (i = OCFS2_FIRST_ONLINE_SYSTEM_INODE;
452 i <= OCFS2_LAST_GLOBAL_SYSTEM_INODE; i++) {
Jan Kara1a224ad2008-08-20 15:43:36 +0200453 if (!ocfs2_need_system_inode(osb, i))
454 continue;
Mark Fashehccd979b2005-12-15 14:31:24 -0800455 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
456 if (!new) {
457 ocfs2_release_system_inodes(osb);
458 status = -EINVAL;
459 mlog_errno(status);
460 /* FIXME: Should ERROR_RO_FS */
461 mlog(ML_ERROR, "Unable to load system inode %d, "
462 "possibly corrupt fs?", i);
463 goto bail;
464 }
465 // the array now has one ref, so drop this one
466 iput(new);
467 }
468
469bail:
470 mlog_exit(status);
471 return status;
472}
473
474static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb)
475{
476 struct inode *new = NULL;
477 int status = 0;
478 int i;
479
480 mlog_entry_void();
481
482 for (i = OCFS2_LAST_GLOBAL_SYSTEM_INODE + 1;
483 i < NUM_SYSTEM_INODES;
484 i++) {
Jan Kara1a224ad2008-08-20 15:43:36 +0200485 if (!ocfs2_need_system_inode(osb, i))
486 continue;
Mark Fashehccd979b2005-12-15 14:31:24 -0800487 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
488 if (!new) {
489 ocfs2_release_system_inodes(osb);
490 status = -EINVAL;
491 mlog(ML_ERROR, "status=%d, sysfile=%d, slot=%d\n",
492 status, i, osb->slot_num);
493 goto bail;
494 }
495 /* the array now has one ref, so drop this one */
496 iput(new);
497 }
498
499bail:
500 mlog_exit(status);
501 return status;
502}
503
Denis Chengbddb8eb32007-09-27 02:10:04 +0800504static void ocfs2_release_system_inodes(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -0800505{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800506 int i;
Mark Fashehccd979b2005-12-15 14:31:24 -0800507 struct inode *inode;
508
509 mlog_entry_void();
510
511 for (i = 0; i < NUM_SYSTEM_INODES; i++) {
512 inode = osb->system_inodes[i];
513 if (inode) {
514 iput(inode);
515 osb->system_inodes[i] = NULL;
516 }
517 }
518
519 inode = osb->sys_root_inode;
520 if (inode) {
521 iput(inode);
522 osb->sys_root_inode = NULL;
523 }
524
525 inode = osb->root_inode;
526 if (inode) {
527 iput(inode);
528 osb->root_inode = NULL;
529 }
530
Denis Chengbddb8eb32007-09-27 02:10:04 +0800531 mlog_exit(0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800532}
533
534/* We're allocating fs objects, use GFP_NOFS */
535static struct inode *ocfs2_alloc_inode(struct super_block *sb)
536{
537 struct ocfs2_inode_info *oi;
538
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800539 oi = kmem_cache_alloc(ocfs2_inode_cachep, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800540 if (!oi)
541 return NULL;
542
Joel Becker2b4e30f2008-09-03 20:03:41 -0700543 jbd2_journal_init_jbd_inode(&oi->ip_jinode, &oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800544 return &oi->vfs_inode;
545}
546
547static void ocfs2_destroy_inode(struct inode *inode)
548{
549 kmem_cache_free(ocfs2_inode_cachep, OCFS2_I(inode));
550}
551
Mark Fasheh5a254032007-07-20 12:56:16 -0700552static unsigned long long ocfs2_max_file_offset(unsigned int bbits,
553 unsigned int cbits)
Mark Fashehccd979b2005-12-15 14:31:24 -0800554{
Mark Fasheh5a254032007-07-20 12:56:16 -0700555 unsigned int bytes = 1 << cbits;
556 unsigned int trim = bytes;
557 unsigned int bitshift = 32;
Mark Fashehccd979b2005-12-15 14:31:24 -0800558
Mark Fasheh5a254032007-07-20 12:56:16 -0700559 /*
560 * i_size and all block offsets in ocfs2 are always 64 bits
561 * wide. i_clusters is 32 bits, in cluster-sized units. So on
562 * 64 bit platforms, cluster size will be the limiting factor.
Mark Fashehccd979b2005-12-15 14:31:24 -0800563 */
564
565#if BITS_PER_LONG == 32
566# if defined(CONFIG_LBD)
Alexey Dobriyan2ecd05a2006-10-11 01:22:05 -0700567 BUILD_BUG_ON(sizeof(sector_t) != 8);
Mark Fasheh5a254032007-07-20 12:56:16 -0700568 /*
569 * We might be limited by page cache size.
570 */
571 if (bytes > PAGE_CACHE_SIZE) {
572 bytes = PAGE_CACHE_SIZE;
573 trim = 1;
574 /*
575 * Shift by 31 here so that we don't get larger than
576 * MAX_LFS_FILESIZE
577 */
578 bitshift = 31;
579 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800580# else
Mark Fasheh5a254032007-07-20 12:56:16 -0700581 /*
582 * We are limited by the size of sector_t. Use block size, as
583 * that's what we expose to the VFS.
584 */
585 bytes = 1 << bbits;
586 trim = 1;
587 bitshift = 31;
Mark Fashehccd979b2005-12-15 14:31:24 -0800588# endif
589#endif
590
Mark Fasheh5a254032007-07-20 12:56:16 -0700591 /*
592 * Trim by a whole cluster when we can actually approach the
593 * on-disk limits. Otherwise we can overflow i_clusters when
594 * an extent start is at the max offset.
595 */
596 return (((unsigned long long)bytes) << bitshift) - trim;
Mark Fashehccd979b2005-12-15 14:31:24 -0800597}
598
599static int ocfs2_remount(struct super_block *sb, int *flags, char *data)
600{
601 int incompat_features;
602 int ret = 0;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800603 struct mount_options parsed_options;
Mark Fashehccd979b2005-12-15 14:31:24 -0800604 struct ocfs2_super *osb = OCFS2_SB(sb);
605
Tiger Yangc0123ad2007-09-08 00:16:10 +0800606 if (!ocfs2_parse_options(sb, data, &parsed_options, 1)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800607 ret = -EINVAL;
608 goto out;
609 }
610
611 if ((osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) !=
Tiger Yangc0123ad2007-09-08 00:16:10 +0800612 (parsed_options.mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800613 ret = -EINVAL;
614 mlog(ML_ERROR, "Cannot change heartbeat mode on remount\n");
615 goto out;
616 }
617
618 if ((osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK) !=
Tiger Yangc0123ad2007-09-08 00:16:10 +0800619 (parsed_options.mount_opt & OCFS2_MOUNT_DATA_WRITEBACK)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800620 ret = -EINVAL;
621 mlog(ML_ERROR, "Cannot change data mode on remount\n");
622 goto out;
623 }
624
Joel Becker12462f12008-09-03 20:03:40 -0700625 /* Probably don't want this on remount; it might
626 * mess with other nodes */
627 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64) &&
628 (parsed_options.mount_opt & OCFS2_MOUNT_INODE64)) {
629 ret = -EINVAL;
630 mlog(ML_ERROR, "Cannot enable inode64 on remount\n");
631 goto out;
632 }
633
Mark Fashehccd979b2005-12-15 14:31:24 -0800634 /* We're going to/from readonly mode. */
635 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
Jan Kara19ece542008-08-21 20:13:17 +0200636 /* Disable quota accounting before remounting RO */
637 if (*flags & MS_RDONLY) {
638 ret = ocfs2_susp_quotas(osb, 0);
639 if (ret < 0)
640 goto out;
641 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800642 /* Lock here so the check of HARD_RO and the potential
643 * setting of SOFT_RO is atomic. */
644 spin_lock(&osb->osb_lock);
645 if (osb->osb_flags & OCFS2_OSB_HARD_RO) {
646 mlog(ML_ERROR, "Remount on readonly device is forbidden.\n");
647 ret = -EROFS;
648 goto unlock_osb;
649 }
650
651 if (*flags & MS_RDONLY) {
652 mlog(0, "Going to ro mode.\n");
653 sb->s_flags |= MS_RDONLY;
654 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
655 } else {
656 mlog(0, "Making ro filesystem writeable.\n");
657
658 if (osb->osb_flags & OCFS2_OSB_ERROR_FS) {
659 mlog(ML_ERROR, "Cannot remount RDWR "
660 "filesystem due to previous errors.\n");
661 ret = -EROFS;
662 goto unlock_osb;
663 }
664 incompat_features = OCFS2_HAS_RO_COMPAT_FEATURE(sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP);
665 if (incompat_features) {
666 mlog(ML_ERROR, "Cannot remount RDWR because "
667 "of unsupported optional features "
668 "(%x).\n", incompat_features);
669 ret = -EINVAL;
670 goto unlock_osb;
671 }
672 sb->s_flags &= ~MS_RDONLY;
673 osb->osb_flags &= ~OCFS2_OSB_SOFT_RO;
674 }
675unlock_osb:
676 spin_unlock(&osb->osb_lock);
Jan Kara19ece542008-08-21 20:13:17 +0200677 /* Enable quota accounting after remounting RW */
678 if (!ret && !(*flags & MS_RDONLY)) {
679 if (sb_any_quota_suspended(sb))
680 ret = ocfs2_susp_quotas(osb, 1);
681 else
682 ret = ocfs2_enable_quotas(osb);
683 if (ret < 0) {
684 /* Return back changes... */
685 spin_lock(&osb->osb_lock);
686 sb->s_flags |= MS_RDONLY;
687 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
688 spin_unlock(&osb->osb_lock);
689 goto out;
690 }
691 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800692 }
693
694 if (!ret) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800695 /* Only save off the new mount options in case of a successful
696 * remount. */
Tiger Yanga68979b2008-11-14 11:17:52 +0800697 if (!(osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR))
698 parsed_options.mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800699 osb->s_mount_opt = parsed_options.mount_opt;
700 osb->s_atime_quantum = parsed_options.atime_quantum;
701 osb->preferred_slot = parsed_options.slot;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800702 if (parsed_options.commit_interval)
703 osb->osb_commit_interval = parsed_options.commit_interval;
Mark Fashehe001e792007-11-07 14:21:45 -0800704
705 if (!ocfs2_is_hard_readonly(osb))
706 ocfs2_set_journal_params(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800707 }
708out:
709 return ret;
710}
711
712static int ocfs2_sb_probe(struct super_block *sb,
713 struct buffer_head **bh,
714 int *sector_size)
715{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800716 int status, tmpstat;
Mark Fashehccd979b2005-12-15 14:31:24 -0800717 struct ocfs1_vol_disk_hdr *hdr;
718 struct ocfs2_dinode *di;
719 int blksize;
720
721 *bh = NULL;
722
723 /* may be > 512 */
724 *sector_size = bdev_hardsect_size(sb->s_bdev);
725 if (*sector_size > OCFS2_MAX_BLOCKSIZE) {
726 mlog(ML_ERROR, "Hardware sector size too large: %d (max=%d)\n",
727 *sector_size, OCFS2_MAX_BLOCKSIZE);
728 status = -EINVAL;
729 goto bail;
730 }
731
732 /* Can this really happen? */
733 if (*sector_size < OCFS2_MIN_BLOCKSIZE)
734 *sector_size = OCFS2_MIN_BLOCKSIZE;
735
736 /* check block zero for old format */
737 status = ocfs2_get_sector(sb, bh, 0, *sector_size);
738 if (status < 0) {
739 mlog_errno(status);
740 goto bail;
741 }
742 hdr = (struct ocfs1_vol_disk_hdr *) (*bh)->b_data;
743 if (hdr->major_version == OCFS1_MAJOR_VERSION) {
744 mlog(ML_ERROR, "incompatible version: %u.%u\n",
745 hdr->major_version, hdr->minor_version);
746 status = -EINVAL;
747 }
748 if (memcmp(hdr->signature, OCFS1_VOLUME_SIGNATURE,
749 strlen(OCFS1_VOLUME_SIGNATURE)) == 0) {
750 mlog(ML_ERROR, "incompatible volume signature: %8s\n",
751 hdr->signature);
752 status = -EINVAL;
753 }
754 brelse(*bh);
755 *bh = NULL;
756 if (status < 0) {
757 mlog(ML_ERROR, "This is an ocfs v1 filesystem which must be "
758 "upgraded before mounting with ocfs v2\n");
759 goto bail;
760 }
761
762 /*
763 * Now check at magic offset for 512, 1024, 2048, 4096
764 * blocksizes. 4096 is the maximum blocksize because it is
765 * the minimum clustersize.
766 */
767 status = -EINVAL;
768 for (blksize = *sector_size;
769 blksize <= OCFS2_MAX_BLOCKSIZE;
770 blksize <<= 1) {
771 tmpstat = ocfs2_get_sector(sb, bh,
772 OCFS2_SUPER_BLOCK_BLKNO,
773 blksize);
774 if (tmpstat < 0) {
775 status = tmpstat;
776 mlog_errno(status);
777 goto bail;
778 }
779 di = (struct ocfs2_dinode *) (*bh)->b_data;
780 status = ocfs2_verify_volume(di, *bh, blksize);
781 if (status >= 0)
782 goto bail;
783 brelse(*bh);
784 *bh = NULL;
785 if (status != -EAGAIN)
786 break;
787 }
788
789bail:
790 return status;
791}
792
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800793static int ocfs2_verify_heartbeat(struct ocfs2_super *osb)
794{
795 if (ocfs2_mount_local(osb)) {
796 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
797 mlog(ML_ERROR, "Cannot heartbeat on a locally "
798 "mounted device.\n");
799 return -EINVAL;
800 }
801 }
802
Joel Beckerb61817e2008-02-01 15:08:23 -0800803 if (ocfs2_userspace_stack(osb)) {
804 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
805 mlog(ML_ERROR, "Userspace stack expected, but "
806 "o2cb heartbeat arguments passed to mount\n");
807 return -EINVAL;
808 }
809 }
810
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800811 if (!(osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
Joel Beckerb61817e2008-02-01 15:08:23 -0800812 if (!ocfs2_mount_local(osb) && !ocfs2_is_hard_readonly(osb) &&
813 !ocfs2_userspace_stack(osb)) {
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800814 mlog(ML_ERROR, "Heartbeat has to be started to mount "
815 "a read-write clustered device.\n");
816 return -EINVAL;
817 }
818 }
819
820 return 0;
821}
822
Joel Beckerb61817e2008-02-01 15:08:23 -0800823/*
824 * If we're using a userspace stack, mount should have passed
825 * a name that matches the disk. If not, mount should not
826 * have passed a stack.
827 */
828static int ocfs2_verify_userspace_stack(struct ocfs2_super *osb,
829 struct mount_options *mopt)
830{
831 if (!ocfs2_userspace_stack(osb) && mopt->cluster_stack[0]) {
832 mlog(ML_ERROR,
833 "cluster stack passed to mount, but this filesystem "
834 "does not support it\n");
835 return -EINVAL;
836 }
837
838 if (ocfs2_userspace_stack(osb) &&
839 strncmp(osb->osb_cluster_stack, mopt->cluster_stack,
840 OCFS2_STACK_LABEL_LEN)) {
841 mlog(ML_ERROR,
842 "cluster stack passed to mount (\"%s\") does not "
843 "match the filesystem (\"%s\")\n",
844 mopt->cluster_stack,
845 osb->osb_cluster_stack);
846 return -EINVAL;
847 }
848
849 return 0;
850}
851
Jan Kara19ece542008-08-21 20:13:17 +0200852static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend)
853{
854 int type;
855 struct super_block *sb = osb->sb;
856 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
857 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
858 int status = 0;
859
860 for (type = 0; type < MAXQUOTAS; type++) {
861 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
862 continue;
863 if (unsuspend)
864 status = vfs_quota_enable(
865 sb_dqopt(sb)->files[type],
866 type, QFMT_OCFS2,
867 DQUOT_SUSPENDED);
868 else
869 status = vfs_quota_disable(sb, type,
870 DQUOT_SUSPENDED);
871 if (status < 0)
872 break;
873 }
874 if (status < 0)
875 mlog(ML_ERROR, "Failed to suspend/unsuspend quotas on "
876 "remount (error = %d).\n", status);
877 return status;
878}
879
880static int ocfs2_enable_quotas(struct ocfs2_super *osb)
881{
882 struct inode *inode[MAXQUOTAS] = { NULL, NULL };
883 struct super_block *sb = osb->sb;
884 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
885 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
886 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
887 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
888 int status;
889 int type;
890
891 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NEGATIVE_USAGE;
892 for (type = 0; type < MAXQUOTAS; type++) {
893 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
894 continue;
895 inode[type] = ocfs2_get_system_file_inode(osb, ino[type],
896 osb->slot_num);
897 if (!inode[type]) {
898 status = -ENOENT;
899 goto out_quota_off;
900 }
901 status = vfs_quota_enable(inode[type], type, QFMT_OCFS2,
902 DQUOT_USAGE_ENABLED);
903 if (status < 0)
904 goto out_quota_off;
905 }
906
907 for (type = 0; type < MAXQUOTAS; type++)
908 iput(inode[type]);
909 return 0;
910out_quota_off:
911 ocfs2_disable_quotas(osb);
912 for (type = 0; type < MAXQUOTAS; type++)
913 iput(inode[type]);
914 mlog_errno(status);
915 return status;
916}
917
918static void ocfs2_disable_quotas(struct ocfs2_super *osb)
919{
920 int type;
921 struct inode *inode;
922 struct super_block *sb = osb->sb;
923
924 /* We mostly ignore errors in this function because there's not much
925 * we can do when we see them */
926 for (type = 0; type < MAXQUOTAS; type++) {
927 if (!sb_has_quota_loaded(sb, type))
928 continue;
929 inode = igrab(sb->s_dquot.files[type]);
930 /* Turn off quotas. This will remove all dquot structures from
931 * memory and so they will be automatically synced to global
932 * quota files */
933 vfs_quota_disable(sb, type, DQUOT_USAGE_ENABLED |
934 DQUOT_LIMITS_ENABLED);
935 if (!inode)
936 continue;
937 iput(inode);
938 }
939}
940
941/* Handle quota on quotactl */
942static int ocfs2_quota_on(struct super_block *sb, int type, int format_id,
943 char *path, int remount)
944{
945 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
946 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
947
948 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
949 return -EINVAL;
950
951 if (remount)
952 return 0; /* Just ignore it has been handled in
953 * ocfs2_remount() */
954 return vfs_quota_enable(sb_dqopt(sb)->files[type], type,
955 format_id, DQUOT_LIMITS_ENABLED);
956}
957
958/* Handle quota off quotactl */
959static int ocfs2_quota_off(struct super_block *sb, int type, int remount)
960{
961 if (remount)
962 return 0; /* Ignore now and handle later in
963 * ocfs2_remount() */
964 return vfs_quota_disable(sb, type, DQUOT_LIMITS_ENABLED);
965}
966
967static struct quotactl_ops ocfs2_quotactl_ops = {
968 .quota_on = ocfs2_quota_on,
969 .quota_off = ocfs2_quota_off,
970 .quota_sync = vfs_quota_sync,
971 .get_info = vfs_get_dqinfo,
972 .set_info = vfs_set_dqinfo,
973 .get_dqblk = vfs_get_dqblk,
974 .set_dqblk = vfs_set_dqblk,
975};
976
Mark Fashehccd979b2005-12-15 14:31:24 -0800977static int ocfs2_fill_super(struct super_block *sb, void *data, int silent)
978{
979 struct dentry *root;
980 int status, sector_size;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800981 struct mount_options parsed_options;
Mark Fashehccd979b2005-12-15 14:31:24 -0800982 struct inode *inode = NULL;
983 struct ocfs2_super *osb = NULL;
984 struct buffer_head *bh = NULL;
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800985 char nodestr[8];
Mark Fashehccd979b2005-12-15 14:31:24 -0800986
987 mlog_entry("%p, %p, %i", sb, data, silent);
988
Tiger Yangc0123ad2007-09-08 00:16:10 +0800989 if (!ocfs2_parse_options(sb, data, &parsed_options, 0)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800990 status = -EINVAL;
991 goto read_super_error;
992 }
993
994 /* probe for superblock */
995 status = ocfs2_sb_probe(sb, &bh, &sector_size);
996 if (status < 0) {
997 mlog(ML_ERROR, "superblock probe failed!\n");
998 goto read_super_error;
999 }
1000
1001 status = ocfs2_initialize_super(sb, bh, sector_size);
1002 osb = OCFS2_SB(sb);
1003 if (status < 0) {
1004 mlog_errno(status);
1005 goto read_super_error;
1006 }
1007 brelse(bh);
1008 bh = NULL;
Tiger Yanga68979b2008-11-14 11:17:52 +08001009
1010 if (!(osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR))
1011 parsed_options.mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
1012
Tiger Yangc0123ad2007-09-08 00:16:10 +08001013 osb->s_mount_opt = parsed_options.mount_opt;
1014 osb->s_atime_quantum = parsed_options.atime_quantum;
1015 osb->preferred_slot = parsed_options.slot;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001016 osb->osb_commit_interval = parsed_options.commit_interval;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001017 osb->local_alloc_default_bits = ocfs2_megabytes_to_clusters(sb, parsed_options.localalloc_opt);
1018 osb->local_alloc_bits = osb->local_alloc_default_bits;
Jan Kara19ece542008-08-21 20:13:17 +02001019 if (osb->s_mount_opt & OCFS2_MOUNT_USRQUOTA &&
1020 !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1021 OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
1022 status = -EINVAL;
1023 mlog(ML_ERROR, "User quotas were requested, but this "
1024 "filesystem does not have the feature enabled.\n");
1025 goto read_super_error;
1026 }
1027 if (osb->s_mount_opt & OCFS2_MOUNT_GRPQUOTA &&
1028 !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1029 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
1030 status = -EINVAL;
1031 mlog(ML_ERROR, "Group quotas were requested, but this "
1032 "filesystem does not have the feature enabled.\n");
1033 goto read_super_error;
1034 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001035
Joel Beckerb61817e2008-02-01 15:08:23 -08001036 status = ocfs2_verify_userspace_stack(osb, &parsed_options);
1037 if (status)
1038 goto read_super_error;
1039
Mark Fashehccd979b2005-12-15 14:31:24 -08001040 sb->s_magic = OCFS2_SUPER_MAGIC;
1041
Tiger Yanga68979b2008-11-14 11:17:52 +08001042 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1043 ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
1044
Mark Fashehccd979b2005-12-15 14:31:24 -08001045 /* Hard readonly mode only if: bdev_read_only, MS_RDONLY,
1046 * heartbeat=none */
1047 if (bdev_read_only(sb->s_bdev)) {
1048 if (!(sb->s_flags & MS_RDONLY)) {
1049 status = -EACCES;
1050 mlog(ML_ERROR, "Readonly device detected but readonly "
1051 "mount was not specified.\n");
1052 goto read_super_error;
1053 }
1054
1055 /* You should not be able to start a local heartbeat
1056 * on a readonly device. */
1057 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
1058 status = -EROFS;
1059 mlog(ML_ERROR, "Local heartbeat specified on readonly "
1060 "device.\n");
1061 goto read_super_error;
1062 }
1063
1064 status = ocfs2_check_journals_nolocks(osb);
1065 if (status < 0) {
1066 if (status == -EROFS)
1067 mlog(ML_ERROR, "Recovery required on readonly "
1068 "file system, but write access is "
1069 "unavailable.\n");
1070 else
1071 mlog_errno(status);
1072 goto read_super_error;
1073 }
1074
1075 ocfs2_set_ro_flag(osb, 1);
1076
1077 printk(KERN_NOTICE "Readonly device detected. No cluster "
1078 "services will be utilized for this mount. Recovery "
1079 "will be skipped.\n");
1080 }
1081
1082 if (!ocfs2_is_hard_readonly(osb)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001083 if (sb->s_flags & MS_RDONLY)
1084 ocfs2_set_ro_flag(osb, 0);
1085 }
1086
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001087 status = ocfs2_verify_heartbeat(osb);
1088 if (status < 0) {
1089 mlog_errno(status);
1090 goto read_super_error;
1091 }
1092
Mark Fashehccd979b2005-12-15 14:31:24 -08001093 osb->osb_debug_root = debugfs_create_dir(osb->uuid_str,
1094 ocfs2_debugfs_root);
1095 if (!osb->osb_debug_root) {
1096 status = -EINVAL;
1097 mlog(ML_ERROR, "Unable to create per-mount debugfs root.\n");
1098 goto read_super_error;
1099 }
1100
Sunil Mushran50397502008-12-17 14:17:43 -08001101 osb->osb_ctxt = debugfs_create_file("fs_state", S_IFREG|S_IRUSR,
1102 osb->osb_debug_root,
1103 osb,
1104 &ocfs2_osb_debug_fops);
1105 if (!osb->osb_ctxt) {
1106 status = -EINVAL;
1107 mlog_errno(status);
1108 goto read_super_error;
1109 }
1110
Mark Fashehccd979b2005-12-15 14:31:24 -08001111 status = ocfs2_mount_volume(sb);
1112 if (osb->root_inode)
1113 inode = igrab(osb->root_inode);
1114
1115 if (status < 0)
1116 goto read_super_error;
1117
1118 if (!inode) {
1119 status = -EIO;
1120 mlog_errno(status);
1121 goto read_super_error;
1122 }
1123
1124 root = d_alloc_root(inode);
1125 if (!root) {
1126 status = -ENOMEM;
1127 mlog_errno(status);
1128 goto read_super_error;
1129 }
1130
1131 sb->s_root = root;
1132
1133 ocfs2_complete_mount_recovery(osb);
1134
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001135 if (ocfs2_mount_local(osb))
1136 snprintf(nodestr, sizeof(nodestr), "local");
1137 else
Joel Becker19fdb622008-01-30 15:38:24 -08001138 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001139
1140 printk(KERN_INFO "ocfs2: Mounting device (%s) on (node %s, slot %d) "
Sunil Mushran781ee3e2006-04-27 16:41:31 -07001141 "with %s data mode.\n",
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001142 osb->dev_str, nodestr, osb->slot_num,
Mark Fashehccd979b2005-12-15 14:31:24 -08001143 osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK ? "writeback" :
1144 "ordered");
1145
1146 atomic_set(&osb->vol_state, VOLUME_MOUNTED);
1147 wake_up(&osb->osb_mount_event);
1148
Jan Kara19ece542008-08-21 20:13:17 +02001149 /* Now we can initialize quotas because we can afford to wait
1150 * for cluster locks recovery now. That also means that truncation
1151 * log recovery can happen but that waits for proper quota setup */
1152 if (!(sb->s_flags & MS_RDONLY)) {
1153 status = ocfs2_enable_quotas(osb);
1154 if (status < 0) {
1155 /* We have to err-out specially here because
1156 * s_root is already set */
1157 mlog_errno(status);
1158 atomic_set(&osb->vol_state, VOLUME_DISABLED);
1159 wake_up(&osb->osb_mount_event);
1160 mlog_exit(status);
1161 return status;
1162 }
1163 }
1164
1165 ocfs2_complete_quota_recovery(osb);
1166
1167 /* Now we wake up again for processes waiting for quotas */
1168 atomic_set(&osb->vol_state, VOLUME_MOUNTED_QUOTAS);
1169 wake_up(&osb->osb_mount_event);
1170
Mark Fashehccd979b2005-12-15 14:31:24 -08001171 mlog_exit(status);
1172 return status;
1173
1174read_super_error:
Mark Fasheha81cb882008-10-07 14:25:16 -07001175 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001176
1177 if (inode)
1178 iput(inode);
1179
1180 if (osb) {
1181 atomic_set(&osb->vol_state, VOLUME_DISABLED);
1182 wake_up(&osb->osb_mount_event);
1183 ocfs2_dismount_volume(sb, 1);
1184 }
1185
1186 mlog_exit(status);
1187 return status;
1188}
1189
David Howells454e2392006-06-23 02:02:57 -07001190static int ocfs2_get_sb(struct file_system_type *fs_type,
1191 int flags,
1192 const char *dev_name,
1193 void *data,
1194 struct vfsmount *mnt)
Mark Fashehccd979b2005-12-15 14:31:24 -08001195{
David Howells454e2392006-06-23 02:02:57 -07001196 return get_sb_bdev(fs_type, flags, dev_name, data, ocfs2_fill_super,
1197 mnt);
Mark Fashehccd979b2005-12-15 14:31:24 -08001198}
1199
1200static struct file_system_type ocfs2_fs_type = {
1201 .owner = THIS_MODULE,
1202 .name = "ocfs2",
1203 .get_sb = ocfs2_get_sb, /* is this called when we mount
1204 * the fs? */
1205 .kill_sb = kill_block_super, /* set to the generic one
1206 * right now, but do we
1207 * need to change that? */
Mark Fasheh1ba9da22006-09-08 14:22:54 -07001208 .fs_flags = FS_REQUIRES_DEV|FS_RENAME_DOES_D_MOVE,
Mark Fashehccd979b2005-12-15 14:31:24 -08001209 .next = NULL
1210};
1211
1212static int ocfs2_parse_options(struct super_block *sb,
1213 char *options,
Tiger Yangc0123ad2007-09-08 00:16:10 +08001214 struct mount_options *mopt,
Mark Fashehccd979b2005-12-15 14:31:24 -08001215 int is_remount)
1216{
1217 int status;
1218 char *p;
1219
1220 mlog_entry("remount: %d, options: \"%s\"\n", is_remount,
1221 options ? options : "(none)");
1222
Mark Fashehd147b3d2007-11-07 14:40:36 -08001223 mopt->commit_interval = 0;
Tiger Yangc0123ad2007-09-08 00:16:10 +08001224 mopt->mount_opt = 0;
1225 mopt->atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
1226 mopt->slot = OCFS2_INVALID_SLOT;
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001227 mopt->localalloc_opt = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE;
Joel Beckerb61817e2008-02-01 15:08:23 -08001228 mopt->cluster_stack[0] = '\0';
Mark Fashehccd979b2005-12-15 14:31:24 -08001229
1230 if (!options) {
1231 status = 1;
1232 goto bail;
1233 }
1234
1235 while ((p = strsep(&options, ",")) != NULL) {
1236 int token, option;
1237 substring_t args[MAX_OPT_ARGS];
1238
1239 if (!*p)
1240 continue;
1241
1242 token = match_token(p, tokens, args);
1243 switch (token) {
1244 case Opt_hb_local:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001245 mopt->mount_opt |= OCFS2_MOUNT_HB_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001246 break;
1247 case Opt_hb_none:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001248 mopt->mount_opt &= ~OCFS2_MOUNT_HB_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001249 break;
1250 case Opt_barrier:
1251 if (match_int(&args[0], &option)) {
1252 status = 0;
1253 goto bail;
1254 }
1255 if (option)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001256 mopt->mount_opt |= OCFS2_MOUNT_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -08001257 else
Tiger Yangc0123ad2007-09-08 00:16:10 +08001258 mopt->mount_opt &= ~OCFS2_MOUNT_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -08001259 break;
1260 case Opt_intr:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001261 mopt->mount_opt &= ~OCFS2_MOUNT_NOINTR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001262 break;
1263 case Opt_nointr:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001264 mopt->mount_opt |= OCFS2_MOUNT_NOINTR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001265 break;
1266 case Opt_err_panic:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001267 mopt->mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001268 break;
1269 case Opt_err_ro:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001270 mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_PANIC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001271 break;
1272 case Opt_data_ordered:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001273 mopt->mount_opt &= ~OCFS2_MOUNT_DATA_WRITEBACK;
Mark Fashehccd979b2005-12-15 14:31:24 -08001274 break;
1275 case Opt_data_writeback:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001276 mopt->mount_opt |= OCFS2_MOUNT_DATA_WRITEBACK;
Mark Fashehccd979b2005-12-15 14:31:24 -08001277 break;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001278 case Opt_user_xattr:
1279 mopt->mount_opt &= ~OCFS2_MOUNT_NOUSERXATTR;
1280 break;
1281 case Opt_nouser_xattr:
1282 mopt->mount_opt |= OCFS2_MOUNT_NOUSERXATTR;
1283 break;
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001284 case Opt_atime_quantum:
1285 if (match_int(&args[0], &option)) {
1286 status = 0;
1287 goto bail;
1288 }
1289 if (option >= 0)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001290 mopt->atime_quantum = option;
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001291 break;
Sunil Mushranbaf46612007-06-18 17:00:24 -07001292 case Opt_slot:
1293 option = 0;
1294 if (match_int(&args[0], &option)) {
1295 status = 0;
1296 goto bail;
1297 }
1298 if (option)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001299 mopt->slot = (s16)option;
Sunil Mushranbaf46612007-06-18 17:00:24 -07001300 break;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001301 case Opt_commit:
1302 option = 0;
1303 if (match_int(&args[0], &option)) {
1304 status = 0;
1305 goto bail;
1306 }
1307 if (option < 0)
1308 return 0;
1309 if (option == 0)
Joel Becker2b4e30f2008-09-03 20:03:41 -07001310 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001311 mopt->commit_interval = HZ * option;
1312 break;
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001313 case Opt_localalloc:
1314 option = 0;
1315 if (match_int(&args[0], &option)) {
1316 status = 0;
1317 goto bail;
1318 }
1319 if (option >= 0 && (option <= ocfs2_local_alloc_size(sb) * 8))
1320 mopt->localalloc_opt = option;
1321 break;
Mark Fasheh53fc6222007-12-20 16:49:04 -08001322 case Opt_localflocks:
1323 /*
1324 * Changing this during remount could race
1325 * flock() requests, or "unbalance" existing
1326 * ones (e.g., a lock is taken in one mode but
1327 * dropped in the other). If users care enough
1328 * to flip locking modes during remount, we
1329 * could add a "local" flag to individual
1330 * flock structures for proper tracking of
1331 * state.
1332 */
1333 if (!is_remount)
1334 mopt->mount_opt |= OCFS2_MOUNT_LOCALFLOCKS;
1335 break;
Joel Beckerb61817e2008-02-01 15:08:23 -08001336 case Opt_stack:
1337 /* Check both that the option we were passed
1338 * is of the right length and that it is a proper
1339 * string of the right length.
1340 */
1341 if (((args[0].to - args[0].from) !=
1342 OCFS2_STACK_LABEL_LEN) ||
1343 (strnlen(args[0].from,
1344 OCFS2_STACK_LABEL_LEN) !=
1345 OCFS2_STACK_LABEL_LEN)) {
1346 mlog(ML_ERROR,
1347 "Invalid cluster_stack option\n");
1348 status = 0;
1349 goto bail;
1350 }
1351 memcpy(mopt->cluster_stack, args[0].from,
1352 OCFS2_STACK_LABEL_LEN);
1353 mopt->cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
1354 break;
Joel Becker12462f12008-09-03 20:03:40 -07001355 case Opt_inode64:
1356 mopt->mount_opt |= OCFS2_MOUNT_INODE64;
1357 break;
Jan Kara19ece542008-08-21 20:13:17 +02001358 case Opt_usrquota:
1359 /* We check only on remount, otherwise features
1360 * aren't yet initialized. */
1361 if (is_remount && !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1362 OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
1363 mlog(ML_ERROR, "User quota requested but "
1364 "filesystem feature is not set\n");
1365 status = 0;
1366 goto bail;
1367 }
1368 mopt->mount_opt |= OCFS2_MOUNT_USRQUOTA;
1369 break;
1370 case Opt_grpquota:
1371 if (is_remount && !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1372 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
1373 mlog(ML_ERROR, "Group quota requested but "
1374 "filesystem feature is not set\n");
1375 status = 0;
1376 goto bail;
1377 }
1378 mopt->mount_opt |= OCFS2_MOUNT_GRPQUOTA;
1379 break;
Tiger Yanga68979b2008-11-14 11:17:52 +08001380#ifdef CONFIG_OCFS2_FS_POSIX_ACL
1381 case Opt_acl:
1382 mopt->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
1383 break;
1384 case Opt_noacl:
1385 mopt->mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
1386 break;
1387#else
1388 case Opt_acl:
1389 case Opt_noacl:
1390 printk(KERN_INFO "ocfs2 (no)acl options not supported\n");
1391 break;
1392#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001393 default:
1394 mlog(ML_ERROR,
1395 "Unrecognized mount option \"%s\" "
1396 "or missing value\n", p);
1397 status = 0;
1398 goto bail;
1399 }
1400 }
1401
1402 status = 1;
1403
1404bail:
1405 mlog_exit(status);
1406 return status;
1407}
1408
Sunil Mushrand5500712007-09-06 13:34:16 -07001409static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
1410{
1411 struct ocfs2_super *osb = OCFS2_SB(mnt->mnt_sb);
1412 unsigned long opts = osb->s_mount_opt;
Mark Fashehebcee4b2008-07-28 14:55:20 -07001413 unsigned int local_alloc_megs;
Sunil Mushrand5500712007-09-06 13:34:16 -07001414
1415 if (opts & OCFS2_MOUNT_HB_LOCAL)
1416 seq_printf(s, ",_netdev,heartbeat=local");
1417 else
1418 seq_printf(s, ",heartbeat=none");
1419
1420 if (opts & OCFS2_MOUNT_NOINTR)
1421 seq_printf(s, ",nointr");
1422
1423 if (opts & OCFS2_MOUNT_DATA_WRITEBACK)
1424 seq_printf(s, ",data=writeback");
1425 else
1426 seq_printf(s, ",data=ordered");
1427
1428 if (opts & OCFS2_MOUNT_BARRIER)
1429 seq_printf(s, ",barrier=1");
1430
1431 if (opts & OCFS2_MOUNT_ERRORS_PANIC)
1432 seq_printf(s, ",errors=panic");
1433 else
1434 seq_printf(s, ",errors=remount-ro");
1435
1436 if (osb->preferred_slot != OCFS2_INVALID_SLOT)
1437 seq_printf(s, ",preferred_slot=%d", osb->preferred_slot);
1438
1439 if (osb->s_atime_quantum != OCFS2_DEFAULT_ATIME_QUANTUM)
1440 seq_printf(s, ",atime_quantum=%u", osb->s_atime_quantum);
1441
Mark Fashehd147b3d2007-11-07 14:40:36 -08001442 if (osb->osb_commit_interval)
1443 seq_printf(s, ",commit=%u",
1444 (unsigned) (osb->osb_commit_interval / HZ));
1445
Mark Fashehebcee4b2008-07-28 14:55:20 -07001446 local_alloc_megs = osb->local_alloc_bits >> (20 - osb->s_clustersize_bits);
1447 if (local_alloc_megs != OCFS2_DEFAULT_LOCAL_ALLOC_SIZE)
1448 seq_printf(s, ",localalloc=%d", local_alloc_megs);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001449
Mark Fasheh53fc6222007-12-20 16:49:04 -08001450 if (opts & OCFS2_MOUNT_LOCALFLOCKS)
1451 seq_printf(s, ",localflocks,");
1452
Joel Beckerb61817e2008-02-01 15:08:23 -08001453 if (osb->osb_cluster_stack[0])
1454 seq_printf(s, ",cluster_stack=%.*s", OCFS2_STACK_LABEL_LEN,
1455 osb->osb_cluster_stack);
Jan Kara19ece542008-08-21 20:13:17 +02001456 if (opts & OCFS2_MOUNT_USRQUOTA)
1457 seq_printf(s, ",usrquota");
1458 if (opts & OCFS2_MOUNT_GRPQUOTA)
1459 seq_printf(s, ",grpquota");
Joel Beckerb61817e2008-02-01 15:08:23 -08001460
Sunil Mushranb0f73cf2008-09-05 11:29:14 -07001461 if (opts & OCFS2_MOUNT_NOUSERXATTR)
1462 seq_printf(s, ",nouser_xattr");
1463 else
1464 seq_printf(s, ",user_xattr");
1465
Joel Becker12462f12008-09-03 20:03:40 -07001466 if (opts & OCFS2_MOUNT_INODE64)
1467 seq_printf(s, ",inode64");
1468
Tiger Yanga68979b2008-11-14 11:17:52 +08001469#ifdef CONFIG_OCFS2_FS_POSIX_ACL
1470 if (opts & OCFS2_MOUNT_POSIX_ACL)
1471 seq_printf(s, ",acl");
1472 else
1473 seq_printf(s, ",noacl");
1474#endif
1475
Sunil Mushrand5500712007-09-06 13:34:16 -07001476 return 0;
1477}
1478
Mark Fashehccd979b2005-12-15 14:31:24 -08001479static int __init ocfs2_init(void)
1480{
1481 int status;
1482
1483 mlog_entry_void();
1484
1485 ocfs2_print_version();
1486
Mark Fashehccd979b2005-12-15 14:31:24 -08001487 status = init_ocfs2_uptodate_cache();
1488 if (status < 0) {
1489 mlog_errno(status);
1490 goto leave;
1491 }
1492
1493 status = ocfs2_initialize_mem_caches();
1494 if (status < 0) {
1495 mlog_errno(status);
1496 goto leave;
1497 }
1498
1499 ocfs2_wq = create_singlethread_workqueue("ocfs2_wq");
1500 if (!ocfs2_wq) {
1501 status = -ENOMEM;
1502 goto leave;
1503 }
1504
Mark Fashehccd979b2005-12-15 14:31:24 -08001505 ocfs2_debugfs_root = debugfs_create_dir("ocfs2", NULL);
1506 if (!ocfs2_debugfs_root) {
1507 status = -EFAULT;
1508 mlog(ML_ERROR, "Unable to create ocfs2 debugfs root.\n");
1509 }
1510
Mark Fasheh171bf932008-10-20 15:36:47 +02001511 status = ocfs2_quota_setup();
1512 if (status)
1513 goto leave;
1514
Joel Becker63e0c482008-01-30 16:58:36 -08001515 ocfs2_set_locking_protocol();
1516
Jan Kara9e33d692008-08-25 19:56:50 +02001517 status = register_quota_format(&ocfs2_quota_format);
Mark Fashehccd979b2005-12-15 14:31:24 -08001518leave:
1519 if (status < 0) {
Mark Fasheh171bf932008-10-20 15:36:47 +02001520 ocfs2_quota_shutdown();
Mark Fashehccd979b2005-12-15 14:31:24 -08001521 ocfs2_free_mem_caches();
1522 exit_ocfs2_uptodate_cache();
Mark Fashehccd979b2005-12-15 14:31:24 -08001523 }
1524
1525 mlog_exit(status);
1526
1527 if (status >= 0) {
1528 return register_filesystem(&ocfs2_fs_type);
1529 } else
1530 return -1;
1531}
1532
1533static void __exit ocfs2_exit(void)
1534{
1535 mlog_entry_void();
1536
Mark Fasheh171bf932008-10-20 15:36:47 +02001537 ocfs2_quota_shutdown();
1538
Mark Fashehccd979b2005-12-15 14:31:24 -08001539 if (ocfs2_wq) {
1540 flush_workqueue(ocfs2_wq);
1541 destroy_workqueue(ocfs2_wq);
1542 }
1543
Jan Kara9e33d692008-08-25 19:56:50 +02001544 unregister_quota_format(&ocfs2_quota_format);
1545
Mark Fashehccd979b2005-12-15 14:31:24 -08001546 debugfs_remove(ocfs2_debugfs_root);
1547
1548 ocfs2_free_mem_caches();
1549
1550 unregister_filesystem(&ocfs2_fs_type);
1551
Mark Fashehccd979b2005-12-15 14:31:24 -08001552 exit_ocfs2_uptodate_cache();
1553
1554 mlog_exit_void();
1555}
1556
1557static void ocfs2_put_super(struct super_block *sb)
1558{
1559 mlog_entry("(0x%p)\n", sb);
1560
1561 ocfs2_sync_blockdev(sb);
1562 ocfs2_dismount_volume(sb, 0);
1563
1564 mlog_exit_void();
1565}
1566
David Howells726c3342006-06-23 02:02:58 -07001567static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
Mark Fashehccd979b2005-12-15 14:31:24 -08001568{
1569 struct ocfs2_super *osb;
1570 u32 numbits, freebits;
1571 int status;
1572 struct ocfs2_dinode *bm_lock;
1573 struct buffer_head *bh = NULL;
1574 struct inode *inode = NULL;
1575
David Howells726c3342006-06-23 02:02:58 -07001576 mlog_entry("(%p, %p)\n", dentry->d_sb, buf);
Mark Fashehccd979b2005-12-15 14:31:24 -08001577
David Howells726c3342006-06-23 02:02:58 -07001578 osb = OCFS2_SB(dentry->d_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001579
1580 inode = ocfs2_get_system_file_inode(osb,
1581 GLOBAL_BITMAP_SYSTEM_INODE,
1582 OCFS2_INVALID_SLOT);
1583 if (!inode) {
1584 mlog(ML_ERROR, "failed to get bitmap inode\n");
1585 status = -EIO;
1586 goto bail;
1587 }
1588
Mark Fashehe63aecb62007-10-18 15:30:42 -07001589 status = ocfs2_inode_lock(inode, &bh, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001590 if (status < 0) {
1591 mlog_errno(status);
1592 goto bail;
1593 }
1594
1595 bm_lock = (struct ocfs2_dinode *) bh->b_data;
1596
1597 numbits = le32_to_cpu(bm_lock->id1.bitmap1.i_total);
1598 freebits = numbits - le32_to_cpu(bm_lock->id1.bitmap1.i_used);
1599
1600 buf->f_type = OCFS2_SUPER_MAGIC;
David Howells726c3342006-06-23 02:02:58 -07001601 buf->f_bsize = dentry->d_sb->s_blocksize;
Mark Fashehccd979b2005-12-15 14:31:24 -08001602 buf->f_namelen = OCFS2_MAX_FILENAME_LEN;
1603 buf->f_blocks = ((sector_t) numbits) *
1604 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1605 buf->f_bfree = ((sector_t) freebits) *
1606 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1607 buf->f_bavail = buf->f_bfree;
1608 buf->f_files = numbits;
1609 buf->f_ffree = freebits;
1610
1611 brelse(bh);
1612
Mark Fashehe63aecb62007-10-18 15:30:42 -07001613 ocfs2_inode_unlock(inode, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001614 status = 0;
1615bail:
1616 if (inode)
1617 iput(inode);
1618
1619 mlog_exit(status);
1620
1621 return status;
1622}
1623
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001624static void ocfs2_inode_init_once(void *data)
Mark Fashehccd979b2005-12-15 14:31:24 -08001625{
1626 struct ocfs2_inode_info *oi = data;
1627
Christoph Lametera35afb82007-05-16 22:10:57 -07001628 oi->ip_flags = 0;
1629 oi->ip_open_count = 0;
1630 spin_lock_init(&oi->ip_lock);
1631 ocfs2_extent_map_init(&oi->vfs_inode);
1632 INIT_LIST_HEAD(&oi->ip_io_markers);
1633 oi->ip_created_trans = 0;
1634 oi->ip_last_trans = 0;
1635 oi->ip_dir_start_lookup = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001636
Christoph Lametera35afb82007-05-16 22:10:57 -07001637 init_rwsem(&oi->ip_alloc_sem);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001638 init_rwsem(&oi->ip_xattr_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -07001639 mutex_init(&oi->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001640
Christoph Lametera35afb82007-05-16 22:10:57 -07001641 oi->ip_blkno = 0ULL;
1642 oi->ip_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001643
Christoph Lametera35afb82007-05-16 22:10:57 -07001644 ocfs2_lock_res_init_once(&oi->ip_rw_lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001645 ocfs2_lock_res_init_once(&oi->ip_inode_lockres);
Christoph Lametera35afb82007-05-16 22:10:57 -07001646 ocfs2_lock_res_init_once(&oi->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001647
Christoph Lametera35afb82007-05-16 22:10:57 -07001648 ocfs2_metadata_cache_init(&oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001649
Christoph Lametera35afb82007-05-16 22:10:57 -07001650 inode_init_once(&oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001651}
1652
1653static int ocfs2_initialize_mem_caches(void)
1654{
1655 ocfs2_inode_cachep = kmem_cache_create("ocfs2_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001656 sizeof(struct ocfs2_inode_info),
1657 0,
1658 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1659 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001660 ocfs2_inode_init_once);
Jan Kara9e33d692008-08-25 19:56:50 +02001661 ocfs2_dquot_cachep = kmem_cache_create("ocfs2_dquot_cache",
1662 sizeof(struct ocfs2_dquot),
1663 0,
1664 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1665 SLAB_MEM_SPREAD),
1666 NULL);
1667 ocfs2_qf_chunk_cachep = kmem_cache_create("ocfs2_qf_chunk_cache",
1668 sizeof(struct ocfs2_quota_chunk),
1669 0,
1670 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
1671 NULL);
1672 if (!ocfs2_inode_cachep || !ocfs2_dquot_cachep ||
1673 !ocfs2_qf_chunk_cachep) {
1674 if (ocfs2_inode_cachep)
1675 kmem_cache_destroy(ocfs2_inode_cachep);
1676 if (ocfs2_dquot_cachep)
1677 kmem_cache_destroy(ocfs2_dquot_cachep);
1678 if (ocfs2_qf_chunk_cachep)
1679 kmem_cache_destroy(ocfs2_qf_chunk_cachep);
Mark Fashehccd979b2005-12-15 14:31:24 -08001680 return -ENOMEM;
Jan Kara9e33d692008-08-25 19:56:50 +02001681 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001682
Mark Fashehccd979b2005-12-15 14:31:24 -08001683 return 0;
1684}
1685
1686static void ocfs2_free_mem_caches(void)
1687{
1688 if (ocfs2_inode_cachep)
1689 kmem_cache_destroy(ocfs2_inode_cachep);
Mark Fashehccd979b2005-12-15 14:31:24 -08001690 ocfs2_inode_cachep = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +02001691
1692 if (ocfs2_dquot_cachep)
1693 kmem_cache_destroy(ocfs2_dquot_cachep);
1694 ocfs2_dquot_cachep = NULL;
1695
1696 if (ocfs2_qf_chunk_cachep)
1697 kmem_cache_destroy(ocfs2_qf_chunk_cachep);
1698 ocfs2_qf_chunk_cachep = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001699}
1700
1701static int ocfs2_get_sector(struct super_block *sb,
1702 struct buffer_head **bh,
1703 int block,
1704 int sect_size)
1705{
1706 if (!sb_set_blocksize(sb, sect_size)) {
1707 mlog(ML_ERROR, "unable to set blocksize\n");
1708 return -EIO;
1709 }
1710
1711 *bh = sb_getblk(sb, block);
1712 if (!*bh) {
1713 mlog_errno(-EIO);
1714 return -EIO;
1715 }
1716 lock_buffer(*bh);
1717 if (!buffer_dirty(*bh))
1718 clear_buffer_uptodate(*bh);
1719 unlock_buffer(*bh);
1720 ll_rw_block(READ, 1, bh);
1721 wait_on_buffer(*bh);
wengang wang28d57d42009-02-13 10:11:47 +08001722 if (!buffer_uptodate(*bh)) {
1723 mlog_errno(-EIO);
1724 brelse(*bh);
1725 *bh = NULL;
1726 return -EIO;
1727 }
1728
Mark Fashehccd979b2005-12-15 14:31:24 -08001729 return 0;
1730}
1731
Mark Fashehccd979b2005-12-15 14:31:24 -08001732static int ocfs2_mount_volume(struct super_block *sb)
1733{
1734 int status = 0;
1735 int unlock_super = 0;
1736 struct ocfs2_super *osb = OCFS2_SB(sb);
1737
1738 mlog_entry_void();
1739
1740 if (ocfs2_is_hard_readonly(osb))
1741 goto leave;
1742
Mark Fashehccd979b2005-12-15 14:31:24 -08001743 status = ocfs2_dlm_init(osb);
1744 if (status < 0) {
1745 mlog_errno(status);
1746 goto leave;
1747 }
1748
Mark Fashehccd979b2005-12-15 14:31:24 -08001749 status = ocfs2_super_lock(osb, 1);
1750 if (status < 0) {
1751 mlog_errno(status);
1752 goto leave;
1753 }
1754 unlock_super = 1;
1755
1756 /* This will load up the node map and add ourselves to it. */
1757 status = ocfs2_find_slot(osb);
1758 if (status < 0) {
1759 mlog_errno(status);
1760 goto leave;
1761 }
1762
Mark Fashehccd979b2005-12-15 14:31:24 -08001763 /* load all node-local system inodes */
1764 status = ocfs2_init_local_system_inodes(osb);
1765 if (status < 0) {
1766 mlog_errno(status);
1767 goto leave;
1768 }
1769
1770 status = ocfs2_check_volume(osb);
1771 if (status < 0) {
1772 mlog_errno(status);
1773 goto leave;
1774 }
1775
1776 status = ocfs2_truncate_log_init(osb);
1777 if (status < 0) {
1778 mlog_errno(status);
1779 goto leave;
1780 }
1781
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001782 if (ocfs2_mount_local(osb))
1783 goto leave;
1784
Mark Fashehccd979b2005-12-15 14:31:24 -08001785leave:
1786 if (unlock_super)
1787 ocfs2_super_unlock(osb, 1);
1788
1789 mlog_exit(status);
1790 return status;
1791}
1792
Mark Fashehccd979b2005-12-15 14:31:24 -08001793static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
1794{
Joel Becker286eaa92008-02-01 15:03:57 -08001795 int tmp, hangup_needed = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001796 struct ocfs2_super *osb = NULL;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001797 char nodestr[8];
Mark Fashehccd979b2005-12-15 14:31:24 -08001798
1799 mlog_entry("(0x%p)\n", sb);
1800
1801 BUG_ON(!sb);
1802 osb = OCFS2_SB(sb);
1803 BUG_ON(!osb);
1804
Sunil Mushran50397502008-12-17 14:17:43 -08001805 debugfs_remove(osb->osb_ctxt);
1806
Jan Kara19ece542008-08-21 20:13:17 +02001807 ocfs2_disable_quotas(osb);
1808
Mark Fashehccd979b2005-12-15 14:31:24 -08001809 ocfs2_shutdown_local_alloc(osb);
1810
1811 ocfs2_truncate_log_shutdown(osb);
1812
Srinivas Eeda83273932009-06-03 17:02:55 -07001813 ocfs2_orphan_scan_stop(osb);
1814
Joel Becker553abd02008-02-01 12:03:57 -08001815 /* This will disable recovery and flush any recovery work. */
1816 ocfs2_recovery_exit(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001817
1818 ocfs2_journal_shutdown(osb);
1819
1820 ocfs2_sync_blockdev(sb);
1821
Joel Becker4670c462008-02-01 14:39:35 -08001822 /* No cluster connection means we've failed during mount, so skip
1823 * all the steps which depended on that to complete. */
1824 if (osb->cconn) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001825 tmp = ocfs2_super_lock(osb, 1);
1826 if (tmp < 0) {
1827 mlog_errno(tmp);
1828 return;
1829 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001830 }
1831
Mark Fasheh19b613d2007-10-04 14:47:09 -07001832 if (osb->slot_num != OCFS2_INVALID_SLOT)
1833 ocfs2_put_slot(osb);
1834
Joel Becker4670c462008-02-01 14:39:35 -08001835 if (osb->cconn)
Mark Fasheh19b613d2007-10-04 14:47:09 -07001836 ocfs2_super_unlock(osb, 1);
1837
Mark Fashehccd979b2005-12-15 14:31:24 -08001838 ocfs2_release_system_inodes(osb);
1839
Joel Becker6953b4c2008-01-29 16:59:56 -08001840 /*
Joel Becker6953b4c2008-01-29 16:59:56 -08001841 * If we're dismounting due to mount error, mount.ocfs2 will clean
1842 * up heartbeat. If we're a local mount, there is no heartbeat.
1843 * If we failed before we got a uuid_str yet, we can't stop
1844 * heartbeat. Otherwise, do it.
1845 */
1846 if (!mnt_err && !ocfs2_mount_local(osb) && osb->uuid_str)
Joel Becker286eaa92008-02-01 15:03:57 -08001847 hangup_needed = 1;
1848
1849 if (osb->cconn)
1850 ocfs2_dlm_shutdown(osb, hangup_needed);
1851
1852 debugfs_remove(osb->osb_debug_root);
1853
1854 if (hangup_needed)
Joel Becker6953b4c2008-01-29 16:59:56 -08001855 ocfs2_cluster_hangup(osb->uuid_str, strlen(osb->uuid_str));
Mark Fashehccd979b2005-12-15 14:31:24 -08001856
1857 atomic_set(&osb->vol_state, VOLUME_DISMOUNTED);
1858
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001859 if (ocfs2_mount_local(osb))
1860 snprintf(nodestr, sizeof(nodestr), "local");
1861 else
Joel Becker19fdb622008-01-30 15:38:24 -08001862 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001863
1864 printk(KERN_INFO "ocfs2: Unmounting device (%s) on (node %s)\n",
1865 osb->dev_str, nodestr);
Mark Fashehccd979b2005-12-15 14:31:24 -08001866
1867 ocfs2_delete_osb(osb);
1868 kfree(osb);
1869 sb->s_dev = 0;
1870 sb->s_fs_info = NULL;
1871}
1872
1873static int ocfs2_setup_osb_uuid(struct ocfs2_super *osb, const unsigned char *uuid,
1874 unsigned uuid_bytes)
1875{
1876 int i, ret;
1877 char *ptr;
1878
1879 BUG_ON(uuid_bytes != OCFS2_VOL_UUID_LEN);
1880
Robert P. J. Daycd861282006-12-13 00:34:52 -08001881 osb->uuid_str = kzalloc(OCFS2_VOL_UUID_LEN * 2 + 1, GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001882 if (osb->uuid_str == NULL)
1883 return -ENOMEM;
1884
Mark Fashehccd979b2005-12-15 14:31:24 -08001885 for (i = 0, ptr = osb->uuid_str; i < OCFS2_VOL_UUID_LEN; i++) {
1886 /* print with null */
1887 ret = snprintf(ptr, 3, "%02X", uuid[i]);
1888 if (ret != 2) /* drop super cleans up */
1889 return -EINVAL;
1890 /* then only advance past the last char */
1891 ptr += 2;
1892 }
1893
1894 return 0;
1895}
1896
1897static int ocfs2_initialize_super(struct super_block *sb,
1898 struct buffer_head *bh,
1899 int sector_size)
1900{
Denis Chengbddb8eb32007-09-27 02:10:04 +08001901 int status;
Mark Fasheh5a254032007-07-20 12:56:16 -07001902 int i, cbits, bbits;
1903 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001904 struct inode *inode = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001905 struct ocfs2_journal *journal;
1906 __le32 uuid_net_key;
1907 struct ocfs2_super *osb;
1908
1909 mlog_entry_void();
1910
Robert P. J. Daycd861282006-12-13 00:34:52 -08001911 osb = kzalloc(sizeof(struct ocfs2_super), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001912 if (!osb) {
1913 status = -ENOMEM;
1914 mlog_errno(status);
1915 goto bail;
1916 }
1917
1918 sb->s_fs_info = osb;
1919 sb->s_op = &ocfs2_sops;
1920 sb->s_export_op = &ocfs2_export_ops;
Jan Kara19ece542008-08-21 20:13:17 +02001921 sb->s_qcop = &ocfs2_quotactl_ops;
1922 sb->dq_op = &ocfs2_quota_operations;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001923 sb->s_xattr = ocfs2_xattr_handlers;
Mark Fashehe0dceaf2007-08-09 16:52:30 -07001924 sb->s_time_gran = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001925 sb->s_flags |= MS_NOATIME;
1926 /* this is needed to support O_LARGEFILE */
Mark Fasheh5a254032007-07-20 12:56:16 -07001927 cbits = le32_to_cpu(di->id2.i_super.s_clustersize_bits);
1928 bbits = le32_to_cpu(di->id2.i_super.s_blocksize_bits);
1929 sb->s_maxbytes = ocfs2_max_file_offset(bbits, cbits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001930
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001931 osb->osb_dx_mask = (1 << (cbits - bbits)) - 1;
1932
1933 for (i = 0; i < 3; i++)
1934 osb->osb_dx_seed[i] = le32_to_cpu(di->id2.i_super.s_dx_seed[i]);
1935 osb->osb_dx_seed[3] = le32_to_cpu(di->id2.i_super.s_uuid_hash);
1936
Mark Fashehccd979b2005-12-15 14:31:24 -08001937 osb->sb = sb;
1938 /* Save off for ocfs2_rw_direct */
1939 osb->s_sectsize_bits = blksize_bits(sector_size);
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01001940 BUG_ON(!osb->s_sectsize_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001941
Mark Fasheh34d024f2007-09-24 15:56:19 -07001942 spin_lock_init(&osb->dc_task_lock);
1943 init_waitqueue_head(&osb->dc_event);
1944 osb->dc_work_sequence = 0;
1945 osb->dc_wake_sequence = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001946 INIT_LIST_HEAD(&osb->blocked_lock_list);
1947 osb->blocked_lock_count = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001948 spin_lock_init(&osb->osb_lock);
Tao Mac8b9cf92009-02-24 17:40:26 -08001949 spin_lock_init(&osb->osb_xattr_lock);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001950 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001951
1952 atomic_set(&osb->alloc_stats.moves, 0);
1953 atomic_set(&osb->alloc_stats.local_data, 0);
1954 atomic_set(&osb->alloc_stats.bitmap_data, 0);
1955 atomic_set(&osb->alloc_stats.bg_allocs, 0);
1956 atomic_set(&osb->alloc_stats.bg_extends, 0);
1957
1958 ocfs2_init_node_maps(osb);
1959
1960 snprintf(osb->dev_str, sizeof(osb->dev_str), "%u,%u",
1961 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1962
Joel Becker553abd02008-02-01 12:03:57 -08001963 status = ocfs2_recovery_init(osb);
1964 if (status) {
1965 mlog(ML_ERROR, "Unable to initialize recovery state\n");
1966 mlog_errno(status);
1967 goto bail;
1968 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001969
Srinivas Eeda83273932009-06-03 17:02:55 -07001970 status = ocfs2_orphan_scan_init(osb);
1971 if (status) {
1972 mlog(ML_ERROR, "Unable to initialize delayed orphan scan\n");
1973 mlog_errno(status);
1974 goto bail;
1975 }
1976
Mark Fashehccd979b2005-12-15 14:31:24 -08001977 init_waitqueue_head(&osb->checkpoint_event);
1978 atomic_set(&osb->needs_checkpoint, 0);
1979
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001980 osb->s_atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
1981
Mark Fashehccd979b2005-12-15 14:31:24 -08001982 osb->slot_num = OCFS2_INVALID_SLOT;
1983
Tiger Yang8154da32008-08-18 17:11:46 +08001984 osb->s_xattr_inline_size = le16_to_cpu(
1985 di->id2.i_super.s_xattr_inline_size);
Tiger Yangfdd77702008-08-18 17:08:55 +08001986
Mark Fashehccd979b2005-12-15 14:31:24 -08001987 osb->local_alloc_state = OCFS2_LA_UNUSED;
1988 osb->local_alloc_bh = NULL;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001989 INIT_DELAYED_WORK(&osb->la_enable_wq, ocfs2_la_enable_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08001990
Mark Fashehccd979b2005-12-15 14:31:24 -08001991 init_waitqueue_head(&osb->osb_mount_event);
1992
1993 osb->vol_label = kmalloc(OCFS2_MAX_VOL_LABEL_LEN, GFP_KERNEL);
1994 if (!osb->vol_label) {
1995 mlog(ML_ERROR, "unable to alloc vol label\n");
1996 status = -ENOMEM;
1997 goto bail;
1998 }
1999
Mark Fashehccd979b2005-12-15 14:31:24 -08002000 osb->max_slots = le16_to_cpu(di->id2.i_super.s_max_slots);
2001 if (osb->max_slots > OCFS2_MAX_SLOTS || osb->max_slots == 0) {
2002 mlog(ML_ERROR, "Invalid number of node slots (%u)\n",
2003 osb->max_slots);
2004 status = -EINVAL;
2005 goto bail;
2006 }
Sunil Mushran781ee3e2006-04-27 16:41:31 -07002007 mlog(0, "max_slots for this device: %u\n", osb->max_slots);
Mark Fashehccd979b2005-12-15 14:31:24 -08002008
Sunil Mushran539d8262008-07-14 17:31:10 -07002009 osb->slot_recovery_generations =
2010 kcalloc(osb->max_slots, sizeof(*osb->slot_recovery_generations),
2011 GFP_KERNEL);
2012 if (!osb->slot_recovery_generations) {
2013 status = -ENOMEM;
2014 mlog_errno(status);
2015 goto bail;
2016 }
2017
Mark Fashehb4df6ed2006-02-22 17:35:08 -08002018 init_waitqueue_head(&osb->osb_wipe_event);
2019 osb->osb_orphan_wipes = kcalloc(osb->max_slots,
2020 sizeof(*osb->osb_orphan_wipes),
2021 GFP_KERNEL);
2022 if (!osb->osb_orphan_wipes) {
2023 status = -ENOMEM;
2024 mlog_errno(status);
2025 goto bail;
2026 }
2027
Mark Fashehccd979b2005-12-15 14:31:24 -08002028 osb->s_feature_compat =
2029 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_compat);
2030 osb->s_feature_ro_compat =
2031 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_ro_compat);
2032 osb->s_feature_incompat =
2033 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_incompat);
2034
2035 if ((i = OCFS2_HAS_INCOMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_INCOMPAT_SUPP))) {
2036 mlog(ML_ERROR, "couldn't mount because of unsupported "
2037 "optional features (%x).\n", i);
2038 status = -EINVAL;
2039 goto bail;
2040 }
2041 if (!(osb->sb->s_flags & MS_RDONLY) &&
2042 (i = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP))) {
2043 mlog(ML_ERROR, "couldn't mount RDWR because of "
2044 "unsupported optional features (%x).\n", i);
2045 status = -EINVAL;
2046 goto bail;
2047 }
2048
Joel Beckerb61817e2008-02-01 15:08:23 -08002049 if (ocfs2_userspace_stack(osb)) {
2050 memcpy(osb->osb_cluster_stack,
2051 OCFS2_RAW_SB(di)->s_cluster_info.ci_stack,
2052 OCFS2_STACK_LABEL_LEN);
2053 osb->osb_cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
2054 if (strlen(osb->osb_cluster_stack) != OCFS2_STACK_LABEL_LEN) {
2055 mlog(ML_ERROR,
2056 "couldn't mount because of an invalid "
2057 "cluster stack label (%s) \n",
2058 osb->osb_cluster_stack);
2059 status = -EINVAL;
2060 goto bail;
2061 }
2062 } else {
2063 /* The empty string is identical with classic tools that
2064 * don't know about s_cluster_info. */
2065 osb->osb_cluster_stack[0] = '\0';
2066 }
2067
Mark Fashehccd979b2005-12-15 14:31:24 -08002068 get_random_bytes(&osb->s_next_generation, sizeof(u32));
2069
2070 /* FIXME
2071 * This should be done in ocfs2_journal_init(), but unknown
2072 * ordering issues will cause the filesystem to crash.
2073 * If anyone wants to figure out what part of the code
2074 * refers to osb->journal before ocfs2_journal_init() is run,
2075 * be my guest.
2076 */
2077 /* initialize our journal structure */
2078
Robert P. J. Daycd861282006-12-13 00:34:52 -08002079 journal = kzalloc(sizeof(struct ocfs2_journal), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08002080 if (!journal) {
2081 mlog(ML_ERROR, "unable to alloc journal\n");
2082 status = -ENOMEM;
2083 goto bail;
2084 }
2085 osb->journal = journal;
2086 journal->j_osb = osb;
2087
2088 atomic_set(&journal->j_num_trans, 0);
2089 init_rwsem(&journal->j_trans_barrier);
2090 init_waitqueue_head(&journal->j_checkpointed);
2091 spin_lock_init(&journal->j_lock);
2092 journal->j_trans_id = (unsigned long) 1;
2093 INIT_LIST_HEAD(&journal->j_la_cleanups);
David Howellsc4028952006-11-22 14:57:56 +00002094 INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
Mark Fashehccd979b2005-12-15 14:31:24 -08002095 journal->j_state = OCFS2_JOURNAL_FREE;
2096
Jan Karaea455f82009-01-12 23:20:31 +01002097 INIT_WORK(&osb->dentry_lock_work, ocfs2_drop_dl_inodes);
2098 osb->dentry_lock_list = NULL;
2099
Mark Fashehccd979b2005-12-15 14:31:24 -08002100 /* get some pseudo constants for clustersize bits */
2101 osb->s_clustersize_bits =
2102 le32_to_cpu(di->id2.i_super.s_clustersize_bits);
2103 osb->s_clustersize = 1 << osb->s_clustersize_bits;
2104 mlog(0, "clusterbits=%d\n", osb->s_clustersize_bits);
2105
2106 if (osb->s_clustersize < OCFS2_MIN_CLUSTERSIZE ||
2107 osb->s_clustersize > OCFS2_MAX_CLUSTERSIZE) {
2108 mlog(ML_ERROR, "Volume has invalid cluster size (%d)\n",
2109 osb->s_clustersize);
2110 status = -EINVAL;
2111 goto bail;
2112 }
2113
2114 if (ocfs2_clusters_to_blocks(osb->sb, le32_to_cpu(di->i_clusters) - 1)
2115 > (u32)~0UL) {
2116 mlog(ML_ERROR, "Volume might try to write to blocks beyond "
2117 "what jbd can address in 32 bits.\n");
2118 status = -EINVAL;
2119 goto bail;
2120 }
2121
2122 if (ocfs2_setup_osb_uuid(osb, di->id2.i_super.s_uuid,
2123 sizeof(di->id2.i_super.s_uuid))) {
2124 mlog(ML_ERROR, "Out of memory trying to setup our uuid.\n");
2125 status = -ENOMEM;
2126 goto bail;
2127 }
2128
Mark Fasheh78427042006-05-04 12:03:26 -07002129 memcpy(&uuid_net_key, di->id2.i_super.s_uuid, sizeof(uuid_net_key));
Mark Fashehccd979b2005-12-15 14:31:24 -08002130
2131 strncpy(osb->vol_label, di->id2.i_super.s_label, 63);
2132 osb->vol_label[63] = '\0';
2133 osb->root_blkno = le64_to_cpu(di->id2.i_super.s_root_blkno);
2134 osb->system_dir_blkno = le64_to_cpu(di->id2.i_super.s_system_dir_blkno);
2135 osb->first_cluster_group_blkno =
2136 le64_to_cpu(di->id2.i_super.s_first_cluster_group);
2137 osb->fs_generation = le32_to_cpu(di->i_fs_generation);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002138 osb->uuid_hash = le32_to_cpu(di->id2.i_super.s_uuid_hash);
Mark Fashehccd979b2005-12-15 14:31:24 -08002139 mlog(0, "vol_label: %s\n", osb->vol_label);
2140 mlog(0, "uuid: %s\n", osb->uuid_str);
Mark Fashehb06970532006-03-03 10:24:33 -08002141 mlog(0, "root_blkno=%llu, system_dir_blkno=%llu\n",
2142 (unsigned long long)osb->root_blkno,
2143 (unsigned long long)osb->system_dir_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002144
2145 osb->osb_dlm_debug = ocfs2_new_dlm_debug();
2146 if (!osb->osb_dlm_debug) {
2147 status = -ENOMEM;
2148 mlog_errno(status);
2149 goto bail;
2150 }
2151
2152 atomic_set(&osb->vol_state, VOLUME_INIT);
2153
2154 /* load root, system_dir, and all global system inodes */
2155 status = ocfs2_init_global_system_inodes(osb);
2156 if (status < 0) {
2157 mlog_errno(status);
2158 goto bail;
2159 }
2160
2161 /*
2162 * global bitmap
2163 */
2164 inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
2165 OCFS2_INVALID_SLOT);
2166 if (!inode) {
2167 status = -EINVAL;
2168 mlog_errno(status);
2169 goto bail;
2170 }
2171
2172 osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08002173 iput(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08002174
Tao Mae9d578a2007-12-18 15:46:10 +08002175 osb->bitmap_cpg = ocfs2_group_bitmap_size(sb) * 8;
Mark Fashehccd979b2005-12-15 14:31:24 -08002176
2177 status = ocfs2_init_slot_info(osb);
2178 if (status < 0) {
2179 mlog_errno(status);
2180 goto bail;
2181 }
2182
Mark Fashehccd979b2005-12-15 14:31:24 -08002183bail:
2184 mlog_exit(status);
2185 return status;
2186}
2187
2188/*
2189 * will return: -EAGAIN if it is ok to keep searching for superblocks
2190 * -EINVAL if there is a bad superblock
2191 * 0 on success
2192 */
2193static int ocfs2_verify_volume(struct ocfs2_dinode *di,
2194 struct buffer_head *bh,
2195 u32 blksz)
2196{
2197 int status = -EAGAIN;
2198
2199 mlog_entry_void();
2200
2201 if (memcmp(di->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
2202 strlen(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) {
Joel Beckerd030cc92008-12-11 15:04:14 -08002203 /* We have to do a raw check of the feature here */
2204 if (le32_to_cpu(di->id2.i_super.s_feature_incompat) &
2205 OCFS2_FEATURE_INCOMPAT_META_ECC) {
2206 status = ocfs2_block_check_validate(bh->b_data,
2207 bh->b_size,
2208 &di->i_check);
2209 if (status)
2210 goto out;
2211 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002212 status = -EINVAL;
2213 if ((1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits)) != blksz) {
2214 mlog(ML_ERROR, "found superblock with incorrect block "
2215 "size: found %u, should be %u\n",
2216 1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits),
2217 blksz);
2218 } else if (le16_to_cpu(di->id2.i_super.s_major_rev_level) !=
2219 OCFS2_MAJOR_REV_LEVEL ||
2220 le16_to_cpu(di->id2.i_super.s_minor_rev_level) !=
2221 OCFS2_MINOR_REV_LEVEL) {
2222 mlog(ML_ERROR, "found superblock with bad version: "
2223 "found %u.%u, should be %u.%u\n",
2224 le16_to_cpu(di->id2.i_super.s_major_rev_level),
2225 le16_to_cpu(di->id2.i_super.s_minor_rev_level),
2226 OCFS2_MAJOR_REV_LEVEL,
2227 OCFS2_MINOR_REV_LEVEL);
2228 } else if (bh->b_blocknr != le64_to_cpu(di->i_blkno)) {
2229 mlog(ML_ERROR, "bad block number on superblock: "
Mark Fashehb06970532006-03-03 10:24:33 -08002230 "found %llu, should be %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07002231 (unsigned long long)le64_to_cpu(di->i_blkno),
Mark Fashehb06970532006-03-03 10:24:33 -08002232 (unsigned long long)bh->b_blocknr);
Mark Fashehccd979b2005-12-15 14:31:24 -08002233 } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
2234 le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
2235 mlog(ML_ERROR, "bad cluster size found: %u\n",
2236 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
2237 } else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
2238 mlog(ML_ERROR, "bad root_blkno: 0\n");
2239 } else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
2240 mlog(ML_ERROR, "bad system_dir_blkno: 0\n");
2241 } else if (le16_to_cpu(di->id2.i_super.s_max_slots) > OCFS2_MAX_SLOTS) {
2242 mlog(ML_ERROR,
2243 "Superblock slots found greater than file system "
2244 "maximum: found %u, max %u\n",
2245 le16_to_cpu(di->id2.i_super.s_max_slots),
2246 OCFS2_MAX_SLOTS);
2247 } else {
2248 /* found it! */
2249 status = 0;
2250 }
2251 }
2252
Joel Beckerd030cc92008-12-11 15:04:14 -08002253out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002254 mlog_exit(status);
2255 return status;
2256}
2257
2258static int ocfs2_check_volume(struct ocfs2_super *osb)
2259{
Denis Chengbddb8eb32007-09-27 02:10:04 +08002260 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08002261 int dirty;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002262 int local;
Mark Fashehccd979b2005-12-15 14:31:24 -08002263 struct ocfs2_dinode *local_alloc = NULL; /* only used if we
2264 * recover
2265 * ourselves. */
2266
2267 mlog_entry_void();
2268
2269 /* Init our journal object. */
2270 status = ocfs2_journal_init(osb->journal, &dirty);
2271 if (status < 0) {
2272 mlog(ML_ERROR, "Could not initialize journal!\n");
2273 goto finally;
2274 }
2275
2276 /* If the journal was unmounted cleanly then we don't want to
2277 * recover anything. Otherwise, journal_load will do that
2278 * dirty work for us :) */
2279 if (!dirty) {
2280 status = ocfs2_journal_wipe(osb->journal, 0);
2281 if (status < 0) {
2282 mlog_errno(status);
2283 goto finally;
2284 }
2285 } else {
2286 mlog(ML_NOTICE, "File system was not unmounted cleanly, "
2287 "recovering volume.\n");
2288 }
2289
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002290 local = ocfs2_mount_local(osb);
2291
Mark Fashehccd979b2005-12-15 14:31:24 -08002292 /* will play back anything left in the journal. */
Sunil Mushran539d8262008-07-14 17:31:10 -07002293 status = ocfs2_journal_load(osb->journal, local, dirty);
Wengang Wang01af4822008-06-10 14:24:48 +08002294 if (status < 0) {
2295 mlog(ML_ERROR, "ocfs2 journal load failed! %d\n", status);
2296 goto finally;
2297 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002298
2299 if (dirty) {
2300 /* recover my local alloc if we didn't unmount cleanly. */
2301 status = ocfs2_begin_local_alloc_recovery(osb,
2302 osb->slot_num,
2303 &local_alloc);
2304 if (status < 0) {
2305 mlog_errno(status);
2306 goto finally;
2307 }
2308 /* we complete the recovery process after we've marked
2309 * ourselves as mounted. */
2310 }
2311
2312 mlog(0, "Journal loaded.\n");
2313
2314 status = ocfs2_load_local_alloc(osb);
2315 if (status < 0) {
2316 mlog_errno(status);
2317 goto finally;
2318 }
2319
2320 if (dirty) {
2321 /* Recovery will be completed after we've mounted the
2322 * rest of the volume. */
2323 osb->dirty = 1;
2324 osb->local_alloc_copy = local_alloc;
2325 local_alloc = NULL;
2326 }
2327
2328 /* go through each journal, trylock it and if you get the
2329 * lock, and it's marked as dirty, set the bit in the recover
2330 * map and launch a recovery thread for it. */
2331 status = ocfs2_mark_dead_nodes(osb);
Srinivas Eeda9140db02009-03-06 14:21:46 -08002332 if (status < 0) {
2333 mlog_errno(status);
2334 goto finally;
2335 }
2336
2337 status = ocfs2_compute_replay_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002338 if (status < 0)
2339 mlog_errno(status);
2340
2341finally:
2342 if (local_alloc)
2343 kfree(local_alloc);
2344
2345 mlog_exit(status);
2346 return status;
2347}
2348
2349/*
2350 * The routine gets called from dismount or close whenever a dismount on
2351 * volume is requested and the osb open count becomes 1.
2352 * It will remove the osb from the global list and also free up all the
2353 * initialized resources and fileobject.
2354 */
2355static void ocfs2_delete_osb(struct ocfs2_super *osb)
2356{
2357 mlog_entry_void();
2358
2359 /* This function assumes that the caller has the main osb resource */
2360
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002361 ocfs2_free_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002362
Mark Fashehb4df6ed2006-02-22 17:35:08 -08002363 kfree(osb->osb_orphan_wipes);
Sunil Mushran539d8262008-07-14 17:31:10 -07002364 kfree(osb->slot_recovery_generations);
Mark Fashehccd979b2005-12-15 14:31:24 -08002365 /* FIXME
2366 * This belongs in journal shutdown, but because we have to
2367 * allocate osb->journal at the start of ocfs2_initalize_osb(),
2368 * we free it here.
2369 */
2370 kfree(osb->journal);
2371 if (osb->local_alloc_copy)
2372 kfree(osb->local_alloc_copy);
2373 kfree(osb->uuid_str);
2374 ocfs2_put_dlm_debug(osb->osb_dlm_debug);
2375 memset(osb, 0, sizeof(struct ocfs2_super));
2376
2377 mlog_exit_void();
2378}
2379
2380/* Put OCFS2 into a readonly state, or (if the user specifies it),
2381 * panic(). We do not support continue-on-error operation. */
2382static void ocfs2_handle_error(struct super_block *sb)
2383{
2384 struct ocfs2_super *osb = OCFS2_SB(sb);
2385
2386 if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_PANIC)
2387 panic("OCFS2: (device %s): panic forced after error\n",
2388 sb->s_id);
2389
2390 ocfs2_set_osb_flag(osb, OCFS2_OSB_ERROR_FS);
2391
2392 if (sb->s_flags & MS_RDONLY &&
2393 (ocfs2_is_soft_readonly(osb) ||
2394 ocfs2_is_hard_readonly(osb)))
2395 return;
2396
2397 printk(KERN_CRIT "File system is now read-only due to the potential "
2398 "of on-disk corruption. Please run fsck.ocfs2 once the file "
2399 "system is unmounted.\n");
2400 sb->s_flags |= MS_RDONLY;
2401 ocfs2_set_ro_flag(osb, 0);
2402}
2403
2404static char error_buf[1024];
2405
2406void __ocfs2_error(struct super_block *sb,
2407 const char *function,
2408 const char *fmt, ...)
2409{
2410 va_list args;
2411
2412 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002413 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Mark Fashehccd979b2005-12-15 14:31:24 -08002414 va_end(args);
2415
2416 /* Not using mlog here because we want to show the actual
2417 * function the error came from. */
2418 printk(KERN_CRIT "OCFS2: ERROR (device %s): %s: %s\n",
2419 sb->s_id, function, error_buf);
2420
2421 ocfs2_handle_error(sb);
2422}
2423
2424/* Handle critical errors. This is intentionally more drastic than
2425 * ocfs2_handle_error, so we only use for things like journal errors,
2426 * etc. */
2427void __ocfs2_abort(struct super_block* sb,
2428 const char *function,
2429 const char *fmt, ...)
2430{
2431 va_list args;
2432
2433 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002434 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Mark Fashehccd979b2005-12-15 14:31:24 -08002435 va_end(args);
2436
2437 printk(KERN_CRIT "OCFS2: abort (device %s): %s: %s\n",
2438 sb->s_id, function, error_buf);
2439
2440 /* We don't have the cluster support yet to go straight to
2441 * hard readonly in here. Until then, we want to keep
2442 * ocfs2_abort() so that we can at least mark critical
2443 * errors.
2444 *
2445 * TODO: This should abort the journal and alert other nodes
2446 * that our slot needs recovery. */
2447
2448 /* Force a panic(). This stinks, but it's better than letting
2449 * things continue without having a proper hard readonly
2450 * here. */
2451 OCFS2_SB(sb)->s_mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
2452 ocfs2_handle_error(sb);
2453}
2454
2455module_init(ocfs2_init);
2456module_exit(ocfs2_exit);