blob: 79ff8d9d37e0844252130272309cd5853b04445e [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;
211
212 out += snprintf(buf + out, len - out,
213 "%10s => Id: %-s Uuid: %-s Gen: 0x%X Label: %-s\n",
214 "Device", osb->dev_str, osb->uuid_str,
215 osb->fs_generation, osb->vol_label);
216
217 out += snprintf(buf + out, len - out,
218 "%10s => State: %d Flags: 0x%lX\n", "Volume",
219 atomic_read(&osb->vol_state), osb->osb_flags);
220
221 out += snprintf(buf + out, len - out,
222 "%10s => Block: %lu Cluster: %d\n", "Sizes",
223 osb->sb->s_blocksize, osb->s_clustersize);
224
225 out += snprintf(buf + out, len - out,
226 "%10s => Compat: 0x%X Incompat: 0x%X "
227 "ROcompat: 0x%X\n",
228 "Features", osb->s_feature_compat,
229 osb->s_feature_incompat, osb->s_feature_ro_compat);
230
231 out += snprintf(buf + out, len - out,
232 "%10s => Opts: 0x%lX AtimeQuanta: %u\n", "Mount",
233 osb->s_mount_opt, osb->s_atime_quantum);
234
235 out += snprintf(buf + out, len - out,
236 "%10s => Stack: %s Name: %*s Version: %d.%d\n",
237 "Cluster",
238 (*osb->osb_cluster_stack == '\0' ?
239 "o2cb" : osb->osb_cluster_stack),
240 cconn->cc_namelen, cconn->cc_name,
241 cconn->cc_version.pv_major, cconn->cc_version.pv_minor);
242
243 spin_lock(&osb->dc_task_lock);
244 out += snprintf(buf + out, len - out,
245 "%10s => Pid: %d Count: %lu WakeSeq: %lu "
246 "WorkSeq: %lu\n", "DownCnvt",
247 task_pid_nr(osb->dc_task), osb->blocked_lock_count,
248 osb->dc_wake_sequence, osb->dc_work_sequence);
249 spin_unlock(&osb->dc_task_lock);
250
251 spin_lock(&osb->osb_lock);
252 out += snprintf(buf + out, len - out, "%10s => Pid: %d Nodes:",
253 "Recovery",
254 (osb->recovery_thread_task ?
255 task_pid_nr(osb->recovery_thread_task) : -1));
256 if (rm->rm_used == 0)
257 out += snprintf(buf + out, len - out, " None\n");
258 else {
259 for (i = 0; i < rm->rm_used; i++)
260 out += snprintf(buf + out, len - out, " %d",
261 rm->rm_entries[i]);
262 out += snprintf(buf + out, len - out, "\n");
263 }
264 spin_unlock(&osb->osb_lock);
265
266 out += snprintf(buf + out, len - out,
267 "%10s => Pid: %d Interval: %lu Needs: %d\n", "Commit",
268 task_pid_nr(osb->commit_task), osb->osb_commit_interval,
269 atomic_read(&osb->needs_checkpoint));
270
271 out += snprintf(buf + out, len - out,
272 "%10s => State: %d NumTxns: %d TxnId: %lu\n",
273 "Journal", osb->journal->j_state,
274 atomic_read(&osb->journal->j_num_trans),
275 osb->journal->j_trans_id);
276
277 out += snprintf(buf + out, len - out,
278 "%10s => GlobalAllocs: %d LocalAllocs: %d "
279 "SubAllocs: %d LAWinMoves: %d SAExtends: %d\n",
280 "Stats",
281 atomic_read(&osb->alloc_stats.bitmap_data),
282 atomic_read(&osb->alloc_stats.local_data),
283 atomic_read(&osb->alloc_stats.bg_allocs),
284 atomic_read(&osb->alloc_stats.moves),
285 atomic_read(&osb->alloc_stats.bg_extends));
286
287 out += snprintf(buf + out, len - out,
288 "%10s => State: %u Descriptor: %llu Size: %u bits "
289 "Default: %u bits\n",
290 "LocalAlloc", osb->local_alloc_state,
291 (unsigned long long)osb->la_last_gd,
292 osb->local_alloc_bits, osb->local_alloc_default_bits);
293
294 spin_lock(&osb->osb_lock);
295 out += snprintf(buf + out, len - out,
296 "%10s => Slot: %d NumStolen: %d\n", "Steal",
297 osb->s_inode_steal_slot,
298 atomic_read(&osb->s_num_inodes_stolen));
299 spin_unlock(&osb->osb_lock);
300
301 out += snprintf(buf + out, len - out, "%10s => %3s %10s\n",
302 "Slots", "Num", "RecoGen");
303
304 for (i = 0; i < osb->max_slots; ++i) {
305 out += snprintf(buf + out, len - out,
306 "%10s %c %3d %10d\n",
307 " ",
308 (i == osb->slot_num ? '*' : ' '),
309 i, osb->slot_recovery_generations[i]);
310 }
311
312 return out;
313}
314
315static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
316{
317 struct ocfs2_super *osb = inode->i_private;
318 char *buf = NULL;
319
320 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
321 if (!buf)
322 goto bail;
323
324 i_size_write(inode, ocfs2_osb_dump(osb, buf, PAGE_SIZE));
325
326 file->private_data = buf;
327
328 return 0;
329bail:
330 return -ENOMEM;
331}
332
333static int ocfs2_debug_release(struct inode *inode, struct file *file)
334{
335 kfree(file->private_data);
336 return 0;
337}
338
339static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
340 size_t nbytes, loff_t *ppos)
341{
342 return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
343 i_size_read(file->f_mapping->host));
344}
345#else
346static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
347{
348 return 0;
349}
350static int ocfs2_debug_release(struct inode *inode, struct file *file)
351{
352 return 0;
353}
354static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
355 size_t nbytes, loff_t *ppos)
356{
357 return 0;
358}
359#endif /* CONFIG_DEBUG_FS */
360
361static struct file_operations ocfs2_osb_debug_fops = {
362 .open = ocfs2_osb_debug_open,
363 .release = ocfs2_debug_release,
364 .read = ocfs2_debug_read,
365 .llseek = generic_file_llseek,
366};
367
Mark Fashehccd979b2005-12-15 14:31:24 -0800368/*
369 * write_super and sync_fs ripped right out of ext3.
370 */
371static void ocfs2_write_super(struct super_block *sb)
372{
Ingo Molnar7892f2f2006-01-09 15:59:25 -0800373 if (mutex_trylock(&sb->s_lock) != 0)
Mark Fashehccd979b2005-12-15 14:31:24 -0800374 BUG();
375 sb->s_dirt = 0;
376}
377
378static int ocfs2_sync_fs(struct super_block *sb, int wait)
379{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800380 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -0800381 tid_t target;
382 struct ocfs2_super *osb = OCFS2_SB(sb);
383
384 sb->s_dirt = 0;
385
386 if (ocfs2_is_hard_readonly(osb))
387 return -EROFS;
388
389 if (wait) {
390 status = ocfs2_flush_truncate_log(osb);
391 if (status < 0)
392 mlog_errno(status);
393 } else {
394 ocfs2_schedule_truncate_log_flush(osb, 0);
395 }
396
Joel Becker2b4e30f2008-09-03 20:03:41 -0700397 if (jbd2_journal_start_commit(OCFS2_SB(sb)->journal->j_journal,
398 &target)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800399 if (wait)
Joel Becker2b4e30f2008-09-03 20:03:41 -0700400 jbd2_log_wait_commit(OCFS2_SB(sb)->journal->j_journal,
401 target);
Mark Fashehccd979b2005-12-15 14:31:24 -0800402 }
403 return 0;
404}
405
Jan Kara1a224ad2008-08-20 15:43:36 +0200406static int ocfs2_need_system_inode(struct ocfs2_super *osb, int ino)
407{
408 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_USRQUOTA)
409 && (ino == USER_QUOTA_SYSTEM_INODE
410 || ino == LOCAL_USER_QUOTA_SYSTEM_INODE))
411 return 0;
412 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)
413 && (ino == GROUP_QUOTA_SYSTEM_INODE
414 || ino == LOCAL_GROUP_QUOTA_SYSTEM_INODE))
415 return 0;
416 return 1;
417}
418
Mark Fashehccd979b2005-12-15 14:31:24 -0800419static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb)
420{
421 struct inode *new = NULL;
422 int status = 0;
423 int i;
424
425 mlog_entry_void();
426
Jan Kara5fa06132008-01-11 00:11:45 +0100427 new = ocfs2_iget(osb, osb->root_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800428 if (IS_ERR(new)) {
429 status = PTR_ERR(new);
430 mlog_errno(status);
431 goto bail;
432 }
433 osb->root_inode = new;
434
Jan Kara5fa06132008-01-11 00:11:45 +0100435 new = ocfs2_iget(osb, osb->system_dir_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->sys_root_inode = new;
442
443 for (i = OCFS2_FIRST_ONLINE_SYSTEM_INODE;
444 i <= OCFS2_LAST_GLOBAL_SYSTEM_INODE; i++) {
Jan Kara1a224ad2008-08-20 15:43:36 +0200445 if (!ocfs2_need_system_inode(osb, i))
446 continue;
Mark Fashehccd979b2005-12-15 14:31:24 -0800447 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
448 if (!new) {
449 ocfs2_release_system_inodes(osb);
450 status = -EINVAL;
451 mlog_errno(status);
452 /* FIXME: Should ERROR_RO_FS */
453 mlog(ML_ERROR, "Unable to load system inode %d, "
454 "possibly corrupt fs?", i);
455 goto bail;
456 }
457 // the array now has one ref, so drop this one
458 iput(new);
459 }
460
461bail:
462 mlog_exit(status);
463 return status;
464}
465
466static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb)
467{
468 struct inode *new = NULL;
469 int status = 0;
470 int i;
471
472 mlog_entry_void();
473
474 for (i = OCFS2_LAST_GLOBAL_SYSTEM_INODE + 1;
475 i < NUM_SYSTEM_INODES;
476 i++) {
Jan Kara1a224ad2008-08-20 15:43:36 +0200477 if (!ocfs2_need_system_inode(osb, i))
478 continue;
Mark Fashehccd979b2005-12-15 14:31:24 -0800479 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
480 if (!new) {
481 ocfs2_release_system_inodes(osb);
482 status = -EINVAL;
483 mlog(ML_ERROR, "status=%d, sysfile=%d, slot=%d\n",
484 status, i, osb->slot_num);
485 goto bail;
486 }
487 /* the array now has one ref, so drop this one */
488 iput(new);
489 }
490
491bail:
492 mlog_exit(status);
493 return status;
494}
495
Denis Chengbddb8eb32007-09-27 02:10:04 +0800496static void ocfs2_release_system_inodes(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -0800497{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800498 int i;
Mark Fashehccd979b2005-12-15 14:31:24 -0800499 struct inode *inode;
500
501 mlog_entry_void();
502
503 for (i = 0; i < NUM_SYSTEM_INODES; i++) {
504 inode = osb->system_inodes[i];
505 if (inode) {
506 iput(inode);
507 osb->system_inodes[i] = NULL;
508 }
509 }
510
511 inode = osb->sys_root_inode;
512 if (inode) {
513 iput(inode);
514 osb->sys_root_inode = NULL;
515 }
516
517 inode = osb->root_inode;
518 if (inode) {
519 iput(inode);
520 osb->root_inode = NULL;
521 }
522
Denis Chengbddb8eb32007-09-27 02:10:04 +0800523 mlog_exit(0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800524}
525
526/* We're allocating fs objects, use GFP_NOFS */
527static struct inode *ocfs2_alloc_inode(struct super_block *sb)
528{
529 struct ocfs2_inode_info *oi;
530
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800531 oi = kmem_cache_alloc(ocfs2_inode_cachep, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800532 if (!oi)
533 return NULL;
534
Joel Becker2b4e30f2008-09-03 20:03:41 -0700535 jbd2_journal_init_jbd_inode(&oi->ip_jinode, &oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800536 return &oi->vfs_inode;
537}
538
539static void ocfs2_destroy_inode(struct inode *inode)
540{
541 kmem_cache_free(ocfs2_inode_cachep, OCFS2_I(inode));
542}
543
Mark Fasheh5a254032007-07-20 12:56:16 -0700544static unsigned long long ocfs2_max_file_offset(unsigned int bbits,
545 unsigned int cbits)
Mark Fashehccd979b2005-12-15 14:31:24 -0800546{
Mark Fasheh5a254032007-07-20 12:56:16 -0700547 unsigned int bytes = 1 << cbits;
548 unsigned int trim = bytes;
549 unsigned int bitshift = 32;
Mark Fashehccd979b2005-12-15 14:31:24 -0800550
Mark Fasheh5a254032007-07-20 12:56:16 -0700551 /*
552 * i_size and all block offsets in ocfs2 are always 64 bits
553 * wide. i_clusters is 32 bits, in cluster-sized units. So on
554 * 64 bit platforms, cluster size will be the limiting factor.
Mark Fashehccd979b2005-12-15 14:31:24 -0800555 */
556
557#if BITS_PER_LONG == 32
558# if defined(CONFIG_LBD)
Alexey Dobriyan2ecd05a2006-10-11 01:22:05 -0700559 BUILD_BUG_ON(sizeof(sector_t) != 8);
Mark Fasheh5a254032007-07-20 12:56:16 -0700560 /*
561 * We might be limited by page cache size.
562 */
563 if (bytes > PAGE_CACHE_SIZE) {
564 bytes = PAGE_CACHE_SIZE;
565 trim = 1;
566 /*
567 * Shift by 31 here so that we don't get larger than
568 * MAX_LFS_FILESIZE
569 */
570 bitshift = 31;
571 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800572# else
Mark Fasheh5a254032007-07-20 12:56:16 -0700573 /*
574 * We are limited by the size of sector_t. Use block size, as
575 * that's what we expose to the VFS.
576 */
577 bytes = 1 << bbits;
578 trim = 1;
579 bitshift = 31;
Mark Fashehccd979b2005-12-15 14:31:24 -0800580# endif
581#endif
582
Mark Fasheh5a254032007-07-20 12:56:16 -0700583 /*
584 * Trim by a whole cluster when we can actually approach the
585 * on-disk limits. Otherwise we can overflow i_clusters when
586 * an extent start is at the max offset.
587 */
588 return (((unsigned long long)bytes) << bitshift) - trim;
Mark Fashehccd979b2005-12-15 14:31:24 -0800589}
590
591static int ocfs2_remount(struct super_block *sb, int *flags, char *data)
592{
593 int incompat_features;
594 int ret = 0;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800595 struct mount_options parsed_options;
Mark Fashehccd979b2005-12-15 14:31:24 -0800596 struct ocfs2_super *osb = OCFS2_SB(sb);
597
Tiger Yangc0123ad2007-09-08 00:16:10 +0800598 if (!ocfs2_parse_options(sb, data, &parsed_options, 1)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800599 ret = -EINVAL;
600 goto out;
601 }
602
603 if ((osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) !=
Tiger Yangc0123ad2007-09-08 00:16:10 +0800604 (parsed_options.mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800605 ret = -EINVAL;
606 mlog(ML_ERROR, "Cannot change heartbeat mode on remount\n");
607 goto out;
608 }
609
610 if ((osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK) !=
Tiger Yangc0123ad2007-09-08 00:16:10 +0800611 (parsed_options.mount_opt & OCFS2_MOUNT_DATA_WRITEBACK)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800612 ret = -EINVAL;
613 mlog(ML_ERROR, "Cannot change data mode on remount\n");
614 goto out;
615 }
616
Joel Becker12462f12008-09-03 20:03:40 -0700617 /* Probably don't want this on remount; it might
618 * mess with other nodes */
619 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64) &&
620 (parsed_options.mount_opt & OCFS2_MOUNT_INODE64)) {
621 ret = -EINVAL;
622 mlog(ML_ERROR, "Cannot enable inode64 on remount\n");
623 goto out;
624 }
625
Mark Fashehccd979b2005-12-15 14:31:24 -0800626 /* We're going to/from readonly mode. */
627 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
Jan Kara19ece542008-08-21 20:13:17 +0200628 /* Disable quota accounting before remounting RO */
629 if (*flags & MS_RDONLY) {
630 ret = ocfs2_susp_quotas(osb, 0);
631 if (ret < 0)
632 goto out;
633 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800634 /* Lock here so the check of HARD_RO and the potential
635 * setting of SOFT_RO is atomic. */
636 spin_lock(&osb->osb_lock);
637 if (osb->osb_flags & OCFS2_OSB_HARD_RO) {
638 mlog(ML_ERROR, "Remount on readonly device is forbidden.\n");
639 ret = -EROFS;
640 goto unlock_osb;
641 }
642
643 if (*flags & MS_RDONLY) {
644 mlog(0, "Going to ro mode.\n");
645 sb->s_flags |= MS_RDONLY;
646 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
647 } else {
648 mlog(0, "Making ro filesystem writeable.\n");
649
650 if (osb->osb_flags & OCFS2_OSB_ERROR_FS) {
651 mlog(ML_ERROR, "Cannot remount RDWR "
652 "filesystem due to previous errors.\n");
653 ret = -EROFS;
654 goto unlock_osb;
655 }
656 incompat_features = OCFS2_HAS_RO_COMPAT_FEATURE(sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP);
657 if (incompat_features) {
658 mlog(ML_ERROR, "Cannot remount RDWR because "
659 "of unsupported optional features "
660 "(%x).\n", incompat_features);
661 ret = -EINVAL;
662 goto unlock_osb;
663 }
664 sb->s_flags &= ~MS_RDONLY;
665 osb->osb_flags &= ~OCFS2_OSB_SOFT_RO;
666 }
667unlock_osb:
668 spin_unlock(&osb->osb_lock);
Jan Kara19ece542008-08-21 20:13:17 +0200669 /* Enable quota accounting after remounting RW */
670 if (!ret && !(*flags & MS_RDONLY)) {
671 if (sb_any_quota_suspended(sb))
672 ret = ocfs2_susp_quotas(osb, 1);
673 else
674 ret = ocfs2_enable_quotas(osb);
675 if (ret < 0) {
676 /* Return back changes... */
677 spin_lock(&osb->osb_lock);
678 sb->s_flags |= MS_RDONLY;
679 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
680 spin_unlock(&osb->osb_lock);
681 goto out;
682 }
683 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800684 }
685
686 if (!ret) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800687 /* Only save off the new mount options in case of a successful
688 * remount. */
Tiger Yanga68979b2008-11-14 11:17:52 +0800689 if (!(osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR))
690 parsed_options.mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800691 osb->s_mount_opt = parsed_options.mount_opt;
692 osb->s_atime_quantum = parsed_options.atime_quantum;
693 osb->preferred_slot = parsed_options.slot;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800694 if (parsed_options.commit_interval)
695 osb->osb_commit_interval = parsed_options.commit_interval;
Mark Fashehe001e792007-11-07 14:21:45 -0800696
697 if (!ocfs2_is_hard_readonly(osb))
698 ocfs2_set_journal_params(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800699 }
700out:
701 return ret;
702}
703
704static int ocfs2_sb_probe(struct super_block *sb,
705 struct buffer_head **bh,
706 int *sector_size)
707{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800708 int status, tmpstat;
Mark Fashehccd979b2005-12-15 14:31:24 -0800709 struct ocfs1_vol_disk_hdr *hdr;
710 struct ocfs2_dinode *di;
711 int blksize;
712
713 *bh = NULL;
714
715 /* may be > 512 */
716 *sector_size = bdev_hardsect_size(sb->s_bdev);
717 if (*sector_size > OCFS2_MAX_BLOCKSIZE) {
718 mlog(ML_ERROR, "Hardware sector size too large: %d (max=%d)\n",
719 *sector_size, OCFS2_MAX_BLOCKSIZE);
720 status = -EINVAL;
721 goto bail;
722 }
723
724 /* Can this really happen? */
725 if (*sector_size < OCFS2_MIN_BLOCKSIZE)
726 *sector_size = OCFS2_MIN_BLOCKSIZE;
727
728 /* check block zero for old format */
729 status = ocfs2_get_sector(sb, bh, 0, *sector_size);
730 if (status < 0) {
731 mlog_errno(status);
732 goto bail;
733 }
734 hdr = (struct ocfs1_vol_disk_hdr *) (*bh)->b_data;
735 if (hdr->major_version == OCFS1_MAJOR_VERSION) {
736 mlog(ML_ERROR, "incompatible version: %u.%u\n",
737 hdr->major_version, hdr->minor_version);
738 status = -EINVAL;
739 }
740 if (memcmp(hdr->signature, OCFS1_VOLUME_SIGNATURE,
741 strlen(OCFS1_VOLUME_SIGNATURE)) == 0) {
742 mlog(ML_ERROR, "incompatible volume signature: %8s\n",
743 hdr->signature);
744 status = -EINVAL;
745 }
746 brelse(*bh);
747 *bh = NULL;
748 if (status < 0) {
749 mlog(ML_ERROR, "This is an ocfs v1 filesystem which must be "
750 "upgraded before mounting with ocfs v2\n");
751 goto bail;
752 }
753
754 /*
755 * Now check at magic offset for 512, 1024, 2048, 4096
756 * blocksizes. 4096 is the maximum blocksize because it is
757 * the minimum clustersize.
758 */
759 status = -EINVAL;
760 for (blksize = *sector_size;
761 blksize <= OCFS2_MAX_BLOCKSIZE;
762 blksize <<= 1) {
763 tmpstat = ocfs2_get_sector(sb, bh,
764 OCFS2_SUPER_BLOCK_BLKNO,
765 blksize);
766 if (tmpstat < 0) {
767 status = tmpstat;
768 mlog_errno(status);
769 goto bail;
770 }
771 di = (struct ocfs2_dinode *) (*bh)->b_data;
772 status = ocfs2_verify_volume(di, *bh, blksize);
773 if (status >= 0)
774 goto bail;
775 brelse(*bh);
776 *bh = NULL;
777 if (status != -EAGAIN)
778 break;
779 }
780
781bail:
782 return status;
783}
784
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800785static int ocfs2_verify_heartbeat(struct ocfs2_super *osb)
786{
787 if (ocfs2_mount_local(osb)) {
788 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
789 mlog(ML_ERROR, "Cannot heartbeat on a locally "
790 "mounted device.\n");
791 return -EINVAL;
792 }
793 }
794
Joel Beckerb61817e2008-02-01 15:08:23 -0800795 if (ocfs2_userspace_stack(osb)) {
796 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
797 mlog(ML_ERROR, "Userspace stack expected, but "
798 "o2cb heartbeat arguments passed to mount\n");
799 return -EINVAL;
800 }
801 }
802
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800803 if (!(osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
Joel Beckerb61817e2008-02-01 15:08:23 -0800804 if (!ocfs2_mount_local(osb) && !ocfs2_is_hard_readonly(osb) &&
805 !ocfs2_userspace_stack(osb)) {
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800806 mlog(ML_ERROR, "Heartbeat has to be started to mount "
807 "a read-write clustered device.\n");
808 return -EINVAL;
809 }
810 }
811
812 return 0;
813}
814
Joel Beckerb61817e2008-02-01 15:08:23 -0800815/*
816 * If we're using a userspace stack, mount should have passed
817 * a name that matches the disk. If not, mount should not
818 * have passed a stack.
819 */
820static int ocfs2_verify_userspace_stack(struct ocfs2_super *osb,
821 struct mount_options *mopt)
822{
823 if (!ocfs2_userspace_stack(osb) && mopt->cluster_stack[0]) {
824 mlog(ML_ERROR,
825 "cluster stack passed to mount, but this filesystem "
826 "does not support it\n");
827 return -EINVAL;
828 }
829
830 if (ocfs2_userspace_stack(osb) &&
831 strncmp(osb->osb_cluster_stack, mopt->cluster_stack,
832 OCFS2_STACK_LABEL_LEN)) {
833 mlog(ML_ERROR,
834 "cluster stack passed to mount (\"%s\") does not "
835 "match the filesystem (\"%s\")\n",
836 mopt->cluster_stack,
837 osb->osb_cluster_stack);
838 return -EINVAL;
839 }
840
841 return 0;
842}
843
Jan Kara19ece542008-08-21 20:13:17 +0200844static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend)
845{
846 int type;
847 struct super_block *sb = osb->sb;
848 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
849 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
850 int status = 0;
851
852 for (type = 0; type < MAXQUOTAS; type++) {
853 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
854 continue;
855 if (unsuspend)
856 status = vfs_quota_enable(
857 sb_dqopt(sb)->files[type],
858 type, QFMT_OCFS2,
859 DQUOT_SUSPENDED);
860 else
861 status = vfs_quota_disable(sb, type,
862 DQUOT_SUSPENDED);
863 if (status < 0)
864 break;
865 }
866 if (status < 0)
867 mlog(ML_ERROR, "Failed to suspend/unsuspend quotas on "
868 "remount (error = %d).\n", status);
869 return status;
870}
871
872static int ocfs2_enable_quotas(struct ocfs2_super *osb)
873{
874 struct inode *inode[MAXQUOTAS] = { NULL, NULL };
875 struct super_block *sb = osb->sb;
876 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
877 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
878 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
879 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
880 int status;
881 int type;
882
883 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NEGATIVE_USAGE;
884 for (type = 0; type < MAXQUOTAS; type++) {
885 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
886 continue;
887 inode[type] = ocfs2_get_system_file_inode(osb, ino[type],
888 osb->slot_num);
889 if (!inode[type]) {
890 status = -ENOENT;
891 goto out_quota_off;
892 }
893 status = vfs_quota_enable(inode[type], type, QFMT_OCFS2,
894 DQUOT_USAGE_ENABLED);
895 if (status < 0)
896 goto out_quota_off;
897 }
898
899 for (type = 0; type < MAXQUOTAS; type++)
900 iput(inode[type]);
901 return 0;
902out_quota_off:
903 ocfs2_disable_quotas(osb);
904 for (type = 0; type < MAXQUOTAS; type++)
905 iput(inode[type]);
906 mlog_errno(status);
907 return status;
908}
909
910static void ocfs2_disable_quotas(struct ocfs2_super *osb)
911{
912 int type;
913 struct inode *inode;
914 struct super_block *sb = osb->sb;
915
916 /* We mostly ignore errors in this function because there's not much
917 * we can do when we see them */
918 for (type = 0; type < MAXQUOTAS; type++) {
919 if (!sb_has_quota_loaded(sb, type))
920 continue;
921 inode = igrab(sb->s_dquot.files[type]);
922 /* Turn off quotas. This will remove all dquot structures from
923 * memory and so they will be automatically synced to global
924 * quota files */
925 vfs_quota_disable(sb, type, DQUOT_USAGE_ENABLED |
926 DQUOT_LIMITS_ENABLED);
927 if (!inode)
928 continue;
929 iput(inode);
930 }
931}
932
933/* Handle quota on quotactl */
934static int ocfs2_quota_on(struct super_block *sb, int type, int format_id,
935 char *path, int remount)
936{
937 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
938 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
939
940 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
941 return -EINVAL;
942
943 if (remount)
944 return 0; /* Just ignore it has been handled in
945 * ocfs2_remount() */
946 return vfs_quota_enable(sb_dqopt(sb)->files[type], type,
947 format_id, DQUOT_LIMITS_ENABLED);
948}
949
950/* Handle quota off quotactl */
951static int ocfs2_quota_off(struct super_block *sb, int type, int remount)
952{
953 if (remount)
954 return 0; /* Ignore now and handle later in
955 * ocfs2_remount() */
956 return vfs_quota_disable(sb, type, DQUOT_LIMITS_ENABLED);
957}
958
959static struct quotactl_ops ocfs2_quotactl_ops = {
960 .quota_on = ocfs2_quota_on,
961 .quota_off = ocfs2_quota_off,
962 .quota_sync = vfs_quota_sync,
963 .get_info = vfs_get_dqinfo,
964 .set_info = vfs_set_dqinfo,
965 .get_dqblk = vfs_get_dqblk,
966 .set_dqblk = vfs_set_dqblk,
967};
968
Mark Fashehccd979b2005-12-15 14:31:24 -0800969static int ocfs2_fill_super(struct super_block *sb, void *data, int silent)
970{
971 struct dentry *root;
972 int status, sector_size;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800973 struct mount_options parsed_options;
Mark Fashehccd979b2005-12-15 14:31:24 -0800974 struct inode *inode = NULL;
975 struct ocfs2_super *osb = NULL;
976 struct buffer_head *bh = NULL;
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800977 char nodestr[8];
Mark Fashehccd979b2005-12-15 14:31:24 -0800978
979 mlog_entry("%p, %p, %i", sb, data, silent);
980
Tiger Yangc0123ad2007-09-08 00:16:10 +0800981 if (!ocfs2_parse_options(sb, data, &parsed_options, 0)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800982 status = -EINVAL;
983 goto read_super_error;
984 }
985
986 /* probe for superblock */
987 status = ocfs2_sb_probe(sb, &bh, &sector_size);
988 if (status < 0) {
989 mlog(ML_ERROR, "superblock probe failed!\n");
990 goto read_super_error;
991 }
992
993 status = ocfs2_initialize_super(sb, bh, sector_size);
994 osb = OCFS2_SB(sb);
995 if (status < 0) {
996 mlog_errno(status);
997 goto read_super_error;
998 }
999 brelse(bh);
1000 bh = NULL;
Tiger Yanga68979b2008-11-14 11:17:52 +08001001
1002 if (!(osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR))
1003 parsed_options.mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
1004
Tiger Yangc0123ad2007-09-08 00:16:10 +08001005 osb->s_mount_opt = parsed_options.mount_opt;
1006 osb->s_atime_quantum = parsed_options.atime_quantum;
1007 osb->preferred_slot = parsed_options.slot;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001008 osb->osb_commit_interval = parsed_options.commit_interval;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001009 osb->local_alloc_default_bits = ocfs2_megabytes_to_clusters(sb, parsed_options.localalloc_opt);
1010 osb->local_alloc_bits = osb->local_alloc_default_bits;
Jan Kara19ece542008-08-21 20:13:17 +02001011 if (osb->s_mount_opt & OCFS2_MOUNT_USRQUOTA &&
1012 !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1013 OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
1014 status = -EINVAL;
1015 mlog(ML_ERROR, "User quotas were requested, but this "
1016 "filesystem does not have the feature enabled.\n");
1017 goto read_super_error;
1018 }
1019 if (osb->s_mount_opt & OCFS2_MOUNT_GRPQUOTA &&
1020 !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1021 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
1022 status = -EINVAL;
1023 mlog(ML_ERROR, "Group quotas were requested, but this "
1024 "filesystem does not have the feature enabled.\n");
1025 goto read_super_error;
1026 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001027
Joel Beckerb61817e2008-02-01 15:08:23 -08001028 status = ocfs2_verify_userspace_stack(osb, &parsed_options);
1029 if (status)
1030 goto read_super_error;
1031
Mark Fashehccd979b2005-12-15 14:31:24 -08001032 sb->s_magic = OCFS2_SUPER_MAGIC;
1033
Tiger Yanga68979b2008-11-14 11:17:52 +08001034 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1035 ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
1036
Mark Fashehccd979b2005-12-15 14:31:24 -08001037 /* Hard readonly mode only if: bdev_read_only, MS_RDONLY,
1038 * heartbeat=none */
1039 if (bdev_read_only(sb->s_bdev)) {
1040 if (!(sb->s_flags & MS_RDONLY)) {
1041 status = -EACCES;
1042 mlog(ML_ERROR, "Readonly device detected but readonly "
1043 "mount was not specified.\n");
1044 goto read_super_error;
1045 }
1046
1047 /* You should not be able to start a local heartbeat
1048 * on a readonly device. */
1049 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
1050 status = -EROFS;
1051 mlog(ML_ERROR, "Local heartbeat specified on readonly "
1052 "device.\n");
1053 goto read_super_error;
1054 }
1055
1056 status = ocfs2_check_journals_nolocks(osb);
1057 if (status < 0) {
1058 if (status == -EROFS)
1059 mlog(ML_ERROR, "Recovery required on readonly "
1060 "file system, but write access is "
1061 "unavailable.\n");
1062 else
1063 mlog_errno(status);
1064 goto read_super_error;
1065 }
1066
1067 ocfs2_set_ro_flag(osb, 1);
1068
1069 printk(KERN_NOTICE "Readonly device detected. No cluster "
1070 "services will be utilized for this mount. Recovery "
1071 "will be skipped.\n");
1072 }
1073
1074 if (!ocfs2_is_hard_readonly(osb)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001075 if (sb->s_flags & MS_RDONLY)
1076 ocfs2_set_ro_flag(osb, 0);
1077 }
1078
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001079 status = ocfs2_verify_heartbeat(osb);
1080 if (status < 0) {
1081 mlog_errno(status);
1082 goto read_super_error;
1083 }
1084
Mark Fashehccd979b2005-12-15 14:31:24 -08001085 osb->osb_debug_root = debugfs_create_dir(osb->uuid_str,
1086 ocfs2_debugfs_root);
1087 if (!osb->osb_debug_root) {
1088 status = -EINVAL;
1089 mlog(ML_ERROR, "Unable to create per-mount debugfs root.\n");
1090 goto read_super_error;
1091 }
1092
Sunil Mushran50397502008-12-17 14:17:43 -08001093 osb->osb_ctxt = debugfs_create_file("fs_state", S_IFREG|S_IRUSR,
1094 osb->osb_debug_root,
1095 osb,
1096 &ocfs2_osb_debug_fops);
1097 if (!osb->osb_ctxt) {
1098 status = -EINVAL;
1099 mlog_errno(status);
1100 goto read_super_error;
1101 }
1102
Mark Fashehccd979b2005-12-15 14:31:24 -08001103 status = ocfs2_mount_volume(sb);
1104 if (osb->root_inode)
1105 inode = igrab(osb->root_inode);
1106
1107 if (status < 0)
1108 goto read_super_error;
1109
1110 if (!inode) {
1111 status = -EIO;
1112 mlog_errno(status);
1113 goto read_super_error;
1114 }
1115
1116 root = d_alloc_root(inode);
1117 if (!root) {
1118 status = -ENOMEM;
1119 mlog_errno(status);
1120 goto read_super_error;
1121 }
1122
1123 sb->s_root = root;
1124
1125 ocfs2_complete_mount_recovery(osb);
1126
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001127 if (ocfs2_mount_local(osb))
1128 snprintf(nodestr, sizeof(nodestr), "local");
1129 else
Joel Becker19fdb622008-01-30 15:38:24 -08001130 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001131
1132 printk(KERN_INFO "ocfs2: Mounting device (%s) on (node %s, slot %d) "
Sunil Mushran781ee3e2006-04-27 16:41:31 -07001133 "with %s data mode.\n",
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001134 osb->dev_str, nodestr, osb->slot_num,
Mark Fashehccd979b2005-12-15 14:31:24 -08001135 osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK ? "writeback" :
1136 "ordered");
1137
1138 atomic_set(&osb->vol_state, VOLUME_MOUNTED);
1139 wake_up(&osb->osb_mount_event);
1140
Jan Kara19ece542008-08-21 20:13:17 +02001141 /* Now we can initialize quotas because we can afford to wait
1142 * for cluster locks recovery now. That also means that truncation
1143 * log recovery can happen but that waits for proper quota setup */
1144 if (!(sb->s_flags & MS_RDONLY)) {
1145 status = ocfs2_enable_quotas(osb);
1146 if (status < 0) {
1147 /* We have to err-out specially here because
1148 * s_root is already set */
1149 mlog_errno(status);
1150 atomic_set(&osb->vol_state, VOLUME_DISABLED);
1151 wake_up(&osb->osb_mount_event);
1152 mlog_exit(status);
1153 return status;
1154 }
1155 }
1156
1157 ocfs2_complete_quota_recovery(osb);
1158
1159 /* Now we wake up again for processes waiting for quotas */
1160 atomic_set(&osb->vol_state, VOLUME_MOUNTED_QUOTAS);
1161 wake_up(&osb->osb_mount_event);
1162
Mark Fashehccd979b2005-12-15 14:31:24 -08001163 mlog_exit(status);
1164 return status;
1165
1166read_super_error:
Mark Fasheha81cb882008-10-07 14:25:16 -07001167 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001168
1169 if (inode)
1170 iput(inode);
1171
1172 if (osb) {
1173 atomic_set(&osb->vol_state, VOLUME_DISABLED);
1174 wake_up(&osb->osb_mount_event);
1175 ocfs2_dismount_volume(sb, 1);
1176 }
1177
1178 mlog_exit(status);
1179 return status;
1180}
1181
David Howells454e2392006-06-23 02:02:57 -07001182static int ocfs2_get_sb(struct file_system_type *fs_type,
1183 int flags,
1184 const char *dev_name,
1185 void *data,
1186 struct vfsmount *mnt)
Mark Fashehccd979b2005-12-15 14:31:24 -08001187{
David Howells454e2392006-06-23 02:02:57 -07001188 return get_sb_bdev(fs_type, flags, dev_name, data, ocfs2_fill_super,
1189 mnt);
Mark Fashehccd979b2005-12-15 14:31:24 -08001190}
1191
1192static struct file_system_type ocfs2_fs_type = {
1193 .owner = THIS_MODULE,
1194 .name = "ocfs2",
1195 .get_sb = ocfs2_get_sb, /* is this called when we mount
1196 * the fs? */
1197 .kill_sb = kill_block_super, /* set to the generic one
1198 * right now, but do we
1199 * need to change that? */
Mark Fasheh1ba9da22006-09-08 14:22:54 -07001200 .fs_flags = FS_REQUIRES_DEV|FS_RENAME_DOES_D_MOVE,
Mark Fashehccd979b2005-12-15 14:31:24 -08001201 .next = NULL
1202};
1203
1204static int ocfs2_parse_options(struct super_block *sb,
1205 char *options,
Tiger Yangc0123ad2007-09-08 00:16:10 +08001206 struct mount_options *mopt,
Mark Fashehccd979b2005-12-15 14:31:24 -08001207 int is_remount)
1208{
1209 int status;
1210 char *p;
1211
1212 mlog_entry("remount: %d, options: \"%s\"\n", is_remount,
1213 options ? options : "(none)");
1214
Mark Fashehd147b3d2007-11-07 14:40:36 -08001215 mopt->commit_interval = 0;
Tiger Yangc0123ad2007-09-08 00:16:10 +08001216 mopt->mount_opt = 0;
1217 mopt->atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
1218 mopt->slot = OCFS2_INVALID_SLOT;
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001219 mopt->localalloc_opt = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE;
Joel Beckerb61817e2008-02-01 15:08:23 -08001220 mopt->cluster_stack[0] = '\0';
Mark Fashehccd979b2005-12-15 14:31:24 -08001221
1222 if (!options) {
1223 status = 1;
1224 goto bail;
1225 }
1226
1227 while ((p = strsep(&options, ",")) != NULL) {
1228 int token, option;
1229 substring_t args[MAX_OPT_ARGS];
1230
1231 if (!*p)
1232 continue;
1233
1234 token = match_token(p, tokens, args);
1235 switch (token) {
1236 case Opt_hb_local:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001237 mopt->mount_opt |= OCFS2_MOUNT_HB_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001238 break;
1239 case Opt_hb_none:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001240 mopt->mount_opt &= ~OCFS2_MOUNT_HB_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001241 break;
1242 case Opt_barrier:
1243 if (match_int(&args[0], &option)) {
1244 status = 0;
1245 goto bail;
1246 }
1247 if (option)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001248 mopt->mount_opt |= OCFS2_MOUNT_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -08001249 else
Tiger Yangc0123ad2007-09-08 00:16:10 +08001250 mopt->mount_opt &= ~OCFS2_MOUNT_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -08001251 break;
1252 case Opt_intr:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001253 mopt->mount_opt &= ~OCFS2_MOUNT_NOINTR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001254 break;
1255 case Opt_nointr:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001256 mopt->mount_opt |= OCFS2_MOUNT_NOINTR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001257 break;
1258 case Opt_err_panic:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001259 mopt->mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001260 break;
1261 case Opt_err_ro:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001262 mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_PANIC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001263 break;
1264 case Opt_data_ordered:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001265 mopt->mount_opt &= ~OCFS2_MOUNT_DATA_WRITEBACK;
Mark Fashehccd979b2005-12-15 14:31:24 -08001266 break;
1267 case Opt_data_writeback:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001268 mopt->mount_opt |= OCFS2_MOUNT_DATA_WRITEBACK;
Mark Fashehccd979b2005-12-15 14:31:24 -08001269 break;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001270 case Opt_user_xattr:
1271 mopt->mount_opt &= ~OCFS2_MOUNT_NOUSERXATTR;
1272 break;
1273 case Opt_nouser_xattr:
1274 mopt->mount_opt |= OCFS2_MOUNT_NOUSERXATTR;
1275 break;
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001276 case Opt_atime_quantum:
1277 if (match_int(&args[0], &option)) {
1278 status = 0;
1279 goto bail;
1280 }
1281 if (option >= 0)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001282 mopt->atime_quantum = option;
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001283 break;
Sunil Mushranbaf46612007-06-18 17:00:24 -07001284 case Opt_slot:
1285 option = 0;
1286 if (match_int(&args[0], &option)) {
1287 status = 0;
1288 goto bail;
1289 }
1290 if (option)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001291 mopt->slot = (s16)option;
Sunil Mushranbaf46612007-06-18 17:00:24 -07001292 break;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001293 case Opt_commit:
1294 option = 0;
1295 if (match_int(&args[0], &option)) {
1296 status = 0;
1297 goto bail;
1298 }
1299 if (option < 0)
1300 return 0;
1301 if (option == 0)
Joel Becker2b4e30f2008-09-03 20:03:41 -07001302 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001303 mopt->commit_interval = HZ * option;
1304 break;
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001305 case Opt_localalloc:
1306 option = 0;
1307 if (match_int(&args[0], &option)) {
1308 status = 0;
1309 goto bail;
1310 }
1311 if (option >= 0 && (option <= ocfs2_local_alloc_size(sb) * 8))
1312 mopt->localalloc_opt = option;
1313 break;
Mark Fasheh53fc6222007-12-20 16:49:04 -08001314 case Opt_localflocks:
1315 /*
1316 * Changing this during remount could race
1317 * flock() requests, or "unbalance" existing
1318 * ones (e.g., a lock is taken in one mode but
1319 * dropped in the other). If users care enough
1320 * to flip locking modes during remount, we
1321 * could add a "local" flag to individual
1322 * flock structures for proper tracking of
1323 * state.
1324 */
1325 if (!is_remount)
1326 mopt->mount_opt |= OCFS2_MOUNT_LOCALFLOCKS;
1327 break;
Joel Beckerb61817e2008-02-01 15:08:23 -08001328 case Opt_stack:
1329 /* Check both that the option we were passed
1330 * is of the right length and that it is a proper
1331 * string of the right length.
1332 */
1333 if (((args[0].to - args[0].from) !=
1334 OCFS2_STACK_LABEL_LEN) ||
1335 (strnlen(args[0].from,
1336 OCFS2_STACK_LABEL_LEN) !=
1337 OCFS2_STACK_LABEL_LEN)) {
1338 mlog(ML_ERROR,
1339 "Invalid cluster_stack option\n");
1340 status = 0;
1341 goto bail;
1342 }
1343 memcpy(mopt->cluster_stack, args[0].from,
1344 OCFS2_STACK_LABEL_LEN);
1345 mopt->cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
1346 break;
Joel Becker12462f12008-09-03 20:03:40 -07001347 case Opt_inode64:
1348 mopt->mount_opt |= OCFS2_MOUNT_INODE64;
1349 break;
Jan Kara19ece542008-08-21 20:13:17 +02001350 case Opt_usrquota:
1351 /* We check only on remount, otherwise features
1352 * aren't yet initialized. */
1353 if (is_remount && !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1354 OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
1355 mlog(ML_ERROR, "User quota requested but "
1356 "filesystem feature is not set\n");
1357 status = 0;
1358 goto bail;
1359 }
1360 mopt->mount_opt |= OCFS2_MOUNT_USRQUOTA;
1361 break;
1362 case Opt_grpquota:
1363 if (is_remount && !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1364 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
1365 mlog(ML_ERROR, "Group quota requested but "
1366 "filesystem feature is not set\n");
1367 status = 0;
1368 goto bail;
1369 }
1370 mopt->mount_opt |= OCFS2_MOUNT_GRPQUOTA;
1371 break;
Tiger Yanga68979b2008-11-14 11:17:52 +08001372#ifdef CONFIG_OCFS2_FS_POSIX_ACL
1373 case Opt_acl:
1374 mopt->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
1375 break;
1376 case Opt_noacl:
1377 mopt->mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
1378 break;
1379#else
1380 case Opt_acl:
1381 case Opt_noacl:
1382 printk(KERN_INFO "ocfs2 (no)acl options not supported\n");
1383 break;
1384#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001385 default:
1386 mlog(ML_ERROR,
1387 "Unrecognized mount option \"%s\" "
1388 "or missing value\n", p);
1389 status = 0;
1390 goto bail;
1391 }
1392 }
1393
1394 status = 1;
1395
1396bail:
1397 mlog_exit(status);
1398 return status;
1399}
1400
Sunil Mushrand5500712007-09-06 13:34:16 -07001401static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
1402{
1403 struct ocfs2_super *osb = OCFS2_SB(mnt->mnt_sb);
1404 unsigned long opts = osb->s_mount_opt;
Mark Fashehebcee4b2008-07-28 14:55:20 -07001405 unsigned int local_alloc_megs;
Sunil Mushrand5500712007-09-06 13:34:16 -07001406
1407 if (opts & OCFS2_MOUNT_HB_LOCAL)
1408 seq_printf(s, ",_netdev,heartbeat=local");
1409 else
1410 seq_printf(s, ",heartbeat=none");
1411
1412 if (opts & OCFS2_MOUNT_NOINTR)
1413 seq_printf(s, ",nointr");
1414
1415 if (opts & OCFS2_MOUNT_DATA_WRITEBACK)
1416 seq_printf(s, ",data=writeback");
1417 else
1418 seq_printf(s, ",data=ordered");
1419
1420 if (opts & OCFS2_MOUNT_BARRIER)
1421 seq_printf(s, ",barrier=1");
1422
1423 if (opts & OCFS2_MOUNT_ERRORS_PANIC)
1424 seq_printf(s, ",errors=panic");
1425 else
1426 seq_printf(s, ",errors=remount-ro");
1427
1428 if (osb->preferred_slot != OCFS2_INVALID_SLOT)
1429 seq_printf(s, ",preferred_slot=%d", osb->preferred_slot);
1430
1431 if (osb->s_atime_quantum != OCFS2_DEFAULT_ATIME_QUANTUM)
1432 seq_printf(s, ",atime_quantum=%u", osb->s_atime_quantum);
1433
Mark Fashehd147b3d2007-11-07 14:40:36 -08001434 if (osb->osb_commit_interval)
1435 seq_printf(s, ",commit=%u",
1436 (unsigned) (osb->osb_commit_interval / HZ));
1437
Mark Fashehebcee4b2008-07-28 14:55:20 -07001438 local_alloc_megs = osb->local_alloc_bits >> (20 - osb->s_clustersize_bits);
1439 if (local_alloc_megs != OCFS2_DEFAULT_LOCAL_ALLOC_SIZE)
1440 seq_printf(s, ",localalloc=%d", local_alloc_megs);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001441
Mark Fasheh53fc6222007-12-20 16:49:04 -08001442 if (opts & OCFS2_MOUNT_LOCALFLOCKS)
1443 seq_printf(s, ",localflocks,");
1444
Joel Beckerb61817e2008-02-01 15:08:23 -08001445 if (osb->osb_cluster_stack[0])
1446 seq_printf(s, ",cluster_stack=%.*s", OCFS2_STACK_LABEL_LEN,
1447 osb->osb_cluster_stack);
Jan Kara19ece542008-08-21 20:13:17 +02001448 if (opts & OCFS2_MOUNT_USRQUOTA)
1449 seq_printf(s, ",usrquota");
1450 if (opts & OCFS2_MOUNT_GRPQUOTA)
1451 seq_printf(s, ",grpquota");
Joel Beckerb61817e2008-02-01 15:08:23 -08001452
Sunil Mushranb0f73cf2008-09-05 11:29:14 -07001453 if (opts & OCFS2_MOUNT_NOUSERXATTR)
1454 seq_printf(s, ",nouser_xattr");
1455 else
1456 seq_printf(s, ",user_xattr");
1457
Joel Becker12462f12008-09-03 20:03:40 -07001458 if (opts & OCFS2_MOUNT_INODE64)
1459 seq_printf(s, ",inode64");
1460
Tiger Yanga68979b2008-11-14 11:17:52 +08001461#ifdef CONFIG_OCFS2_FS_POSIX_ACL
1462 if (opts & OCFS2_MOUNT_POSIX_ACL)
1463 seq_printf(s, ",acl");
1464 else
1465 seq_printf(s, ",noacl");
1466#endif
1467
Sunil Mushrand5500712007-09-06 13:34:16 -07001468 return 0;
1469}
1470
Mark Fashehccd979b2005-12-15 14:31:24 -08001471static int __init ocfs2_init(void)
1472{
1473 int status;
1474
1475 mlog_entry_void();
1476
1477 ocfs2_print_version();
1478
Mark Fashehccd979b2005-12-15 14:31:24 -08001479 status = init_ocfs2_uptodate_cache();
1480 if (status < 0) {
1481 mlog_errno(status);
1482 goto leave;
1483 }
1484
1485 status = ocfs2_initialize_mem_caches();
1486 if (status < 0) {
1487 mlog_errno(status);
1488 goto leave;
1489 }
1490
1491 ocfs2_wq = create_singlethread_workqueue("ocfs2_wq");
1492 if (!ocfs2_wq) {
1493 status = -ENOMEM;
1494 goto leave;
1495 }
1496
Mark Fashehccd979b2005-12-15 14:31:24 -08001497 ocfs2_debugfs_root = debugfs_create_dir("ocfs2", NULL);
1498 if (!ocfs2_debugfs_root) {
1499 status = -EFAULT;
1500 mlog(ML_ERROR, "Unable to create ocfs2 debugfs root.\n");
1501 }
1502
Mark Fasheh171bf932008-10-20 15:36:47 +02001503 status = ocfs2_quota_setup();
1504 if (status)
1505 goto leave;
1506
Joel Becker63e0c482008-01-30 16:58:36 -08001507 ocfs2_set_locking_protocol();
1508
Jan Kara9e33d692008-08-25 19:56:50 +02001509 status = register_quota_format(&ocfs2_quota_format);
Mark Fashehccd979b2005-12-15 14:31:24 -08001510leave:
1511 if (status < 0) {
Mark Fasheh171bf932008-10-20 15:36:47 +02001512 ocfs2_quota_shutdown();
Mark Fashehccd979b2005-12-15 14:31:24 -08001513 ocfs2_free_mem_caches();
1514 exit_ocfs2_uptodate_cache();
Mark Fashehccd979b2005-12-15 14:31:24 -08001515 }
1516
1517 mlog_exit(status);
1518
1519 if (status >= 0) {
1520 return register_filesystem(&ocfs2_fs_type);
1521 } else
1522 return -1;
1523}
1524
1525static void __exit ocfs2_exit(void)
1526{
1527 mlog_entry_void();
1528
Mark Fasheh171bf932008-10-20 15:36:47 +02001529 ocfs2_quota_shutdown();
1530
Mark Fashehccd979b2005-12-15 14:31:24 -08001531 if (ocfs2_wq) {
1532 flush_workqueue(ocfs2_wq);
1533 destroy_workqueue(ocfs2_wq);
1534 }
1535
Jan Kara9e33d692008-08-25 19:56:50 +02001536 unregister_quota_format(&ocfs2_quota_format);
1537
Mark Fashehccd979b2005-12-15 14:31:24 -08001538 debugfs_remove(ocfs2_debugfs_root);
1539
1540 ocfs2_free_mem_caches();
1541
1542 unregister_filesystem(&ocfs2_fs_type);
1543
Mark Fashehccd979b2005-12-15 14:31:24 -08001544 exit_ocfs2_uptodate_cache();
1545
1546 mlog_exit_void();
1547}
1548
1549static void ocfs2_put_super(struct super_block *sb)
1550{
1551 mlog_entry("(0x%p)\n", sb);
1552
1553 ocfs2_sync_blockdev(sb);
1554 ocfs2_dismount_volume(sb, 0);
1555
1556 mlog_exit_void();
1557}
1558
David Howells726c3342006-06-23 02:02:58 -07001559static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
Mark Fashehccd979b2005-12-15 14:31:24 -08001560{
1561 struct ocfs2_super *osb;
1562 u32 numbits, freebits;
1563 int status;
1564 struct ocfs2_dinode *bm_lock;
1565 struct buffer_head *bh = NULL;
1566 struct inode *inode = NULL;
1567
David Howells726c3342006-06-23 02:02:58 -07001568 mlog_entry("(%p, %p)\n", dentry->d_sb, buf);
Mark Fashehccd979b2005-12-15 14:31:24 -08001569
David Howells726c3342006-06-23 02:02:58 -07001570 osb = OCFS2_SB(dentry->d_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001571
1572 inode = ocfs2_get_system_file_inode(osb,
1573 GLOBAL_BITMAP_SYSTEM_INODE,
1574 OCFS2_INVALID_SLOT);
1575 if (!inode) {
1576 mlog(ML_ERROR, "failed to get bitmap inode\n");
1577 status = -EIO;
1578 goto bail;
1579 }
1580
Mark Fashehe63aecb62007-10-18 15:30:42 -07001581 status = ocfs2_inode_lock(inode, &bh, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001582 if (status < 0) {
1583 mlog_errno(status);
1584 goto bail;
1585 }
1586
1587 bm_lock = (struct ocfs2_dinode *) bh->b_data;
1588
1589 numbits = le32_to_cpu(bm_lock->id1.bitmap1.i_total);
1590 freebits = numbits - le32_to_cpu(bm_lock->id1.bitmap1.i_used);
1591
1592 buf->f_type = OCFS2_SUPER_MAGIC;
David Howells726c3342006-06-23 02:02:58 -07001593 buf->f_bsize = dentry->d_sb->s_blocksize;
Mark Fashehccd979b2005-12-15 14:31:24 -08001594 buf->f_namelen = OCFS2_MAX_FILENAME_LEN;
1595 buf->f_blocks = ((sector_t) numbits) *
1596 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1597 buf->f_bfree = ((sector_t) freebits) *
1598 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1599 buf->f_bavail = buf->f_bfree;
1600 buf->f_files = numbits;
1601 buf->f_ffree = freebits;
1602
1603 brelse(bh);
1604
Mark Fashehe63aecb62007-10-18 15:30:42 -07001605 ocfs2_inode_unlock(inode, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001606 status = 0;
1607bail:
1608 if (inode)
1609 iput(inode);
1610
1611 mlog_exit(status);
1612
1613 return status;
1614}
1615
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001616static void ocfs2_inode_init_once(void *data)
Mark Fashehccd979b2005-12-15 14:31:24 -08001617{
1618 struct ocfs2_inode_info *oi = data;
1619
Christoph Lametera35afb82007-05-16 22:10:57 -07001620 oi->ip_flags = 0;
1621 oi->ip_open_count = 0;
1622 spin_lock_init(&oi->ip_lock);
1623 ocfs2_extent_map_init(&oi->vfs_inode);
1624 INIT_LIST_HEAD(&oi->ip_io_markers);
1625 oi->ip_created_trans = 0;
1626 oi->ip_last_trans = 0;
1627 oi->ip_dir_start_lookup = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001628
Christoph Lametera35afb82007-05-16 22:10:57 -07001629 init_rwsem(&oi->ip_alloc_sem);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001630 init_rwsem(&oi->ip_xattr_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -07001631 mutex_init(&oi->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001632
Christoph Lametera35afb82007-05-16 22:10:57 -07001633 oi->ip_blkno = 0ULL;
1634 oi->ip_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001635
Christoph Lametera35afb82007-05-16 22:10:57 -07001636 ocfs2_lock_res_init_once(&oi->ip_rw_lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001637 ocfs2_lock_res_init_once(&oi->ip_inode_lockres);
Christoph Lametera35afb82007-05-16 22:10:57 -07001638 ocfs2_lock_res_init_once(&oi->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001639
Christoph Lametera35afb82007-05-16 22:10:57 -07001640 ocfs2_metadata_cache_init(&oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001641
Christoph Lametera35afb82007-05-16 22:10:57 -07001642 inode_init_once(&oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001643}
1644
1645static int ocfs2_initialize_mem_caches(void)
1646{
1647 ocfs2_inode_cachep = kmem_cache_create("ocfs2_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001648 sizeof(struct ocfs2_inode_info),
1649 0,
1650 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1651 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001652 ocfs2_inode_init_once);
Jan Kara9e33d692008-08-25 19:56:50 +02001653 ocfs2_dquot_cachep = kmem_cache_create("ocfs2_dquot_cache",
1654 sizeof(struct ocfs2_dquot),
1655 0,
1656 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1657 SLAB_MEM_SPREAD),
1658 NULL);
1659 ocfs2_qf_chunk_cachep = kmem_cache_create("ocfs2_qf_chunk_cache",
1660 sizeof(struct ocfs2_quota_chunk),
1661 0,
1662 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
1663 NULL);
1664 if (!ocfs2_inode_cachep || !ocfs2_dquot_cachep ||
1665 !ocfs2_qf_chunk_cachep) {
1666 if (ocfs2_inode_cachep)
1667 kmem_cache_destroy(ocfs2_inode_cachep);
1668 if (ocfs2_dquot_cachep)
1669 kmem_cache_destroy(ocfs2_dquot_cachep);
1670 if (ocfs2_qf_chunk_cachep)
1671 kmem_cache_destroy(ocfs2_qf_chunk_cachep);
Mark Fashehccd979b2005-12-15 14:31:24 -08001672 return -ENOMEM;
Jan Kara9e33d692008-08-25 19:56:50 +02001673 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001674
Mark Fashehccd979b2005-12-15 14:31:24 -08001675 return 0;
1676}
1677
1678static void ocfs2_free_mem_caches(void)
1679{
1680 if (ocfs2_inode_cachep)
1681 kmem_cache_destroy(ocfs2_inode_cachep);
Mark Fashehccd979b2005-12-15 14:31:24 -08001682 ocfs2_inode_cachep = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +02001683
1684 if (ocfs2_dquot_cachep)
1685 kmem_cache_destroy(ocfs2_dquot_cachep);
1686 ocfs2_dquot_cachep = NULL;
1687
1688 if (ocfs2_qf_chunk_cachep)
1689 kmem_cache_destroy(ocfs2_qf_chunk_cachep);
1690 ocfs2_qf_chunk_cachep = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001691}
1692
1693static int ocfs2_get_sector(struct super_block *sb,
1694 struct buffer_head **bh,
1695 int block,
1696 int sect_size)
1697{
1698 if (!sb_set_blocksize(sb, sect_size)) {
1699 mlog(ML_ERROR, "unable to set blocksize\n");
1700 return -EIO;
1701 }
1702
1703 *bh = sb_getblk(sb, block);
1704 if (!*bh) {
1705 mlog_errno(-EIO);
1706 return -EIO;
1707 }
1708 lock_buffer(*bh);
1709 if (!buffer_dirty(*bh))
1710 clear_buffer_uptodate(*bh);
1711 unlock_buffer(*bh);
1712 ll_rw_block(READ, 1, bh);
1713 wait_on_buffer(*bh);
wengang wang28d57d42009-02-13 10:11:47 +08001714 if (!buffer_uptodate(*bh)) {
1715 mlog_errno(-EIO);
1716 brelse(*bh);
1717 *bh = NULL;
1718 return -EIO;
1719 }
1720
Mark Fashehccd979b2005-12-15 14:31:24 -08001721 return 0;
1722}
1723
Mark Fashehccd979b2005-12-15 14:31:24 -08001724static int ocfs2_mount_volume(struct super_block *sb)
1725{
1726 int status = 0;
1727 int unlock_super = 0;
1728 struct ocfs2_super *osb = OCFS2_SB(sb);
1729
1730 mlog_entry_void();
1731
1732 if (ocfs2_is_hard_readonly(osb))
1733 goto leave;
1734
Mark Fashehccd979b2005-12-15 14:31:24 -08001735 status = ocfs2_dlm_init(osb);
1736 if (status < 0) {
1737 mlog_errno(status);
1738 goto leave;
1739 }
1740
Mark Fashehccd979b2005-12-15 14:31:24 -08001741 status = ocfs2_super_lock(osb, 1);
1742 if (status < 0) {
1743 mlog_errno(status);
1744 goto leave;
1745 }
1746 unlock_super = 1;
1747
1748 /* This will load up the node map and add ourselves to it. */
1749 status = ocfs2_find_slot(osb);
1750 if (status < 0) {
1751 mlog_errno(status);
1752 goto leave;
1753 }
1754
Mark Fashehccd979b2005-12-15 14:31:24 -08001755 /* load all node-local system inodes */
1756 status = ocfs2_init_local_system_inodes(osb);
1757 if (status < 0) {
1758 mlog_errno(status);
1759 goto leave;
1760 }
1761
1762 status = ocfs2_check_volume(osb);
1763 if (status < 0) {
1764 mlog_errno(status);
1765 goto leave;
1766 }
1767
1768 status = ocfs2_truncate_log_init(osb);
1769 if (status < 0) {
1770 mlog_errno(status);
1771 goto leave;
1772 }
1773
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001774 if (ocfs2_mount_local(osb))
1775 goto leave;
1776
Mark Fashehccd979b2005-12-15 14:31:24 -08001777leave:
1778 if (unlock_super)
1779 ocfs2_super_unlock(osb, 1);
1780
1781 mlog_exit(status);
1782 return status;
1783}
1784
Mark Fashehccd979b2005-12-15 14:31:24 -08001785static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
1786{
Joel Becker286eaa92008-02-01 15:03:57 -08001787 int tmp, hangup_needed = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001788 struct ocfs2_super *osb = NULL;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001789 char nodestr[8];
Mark Fashehccd979b2005-12-15 14:31:24 -08001790
1791 mlog_entry("(0x%p)\n", sb);
1792
1793 BUG_ON(!sb);
1794 osb = OCFS2_SB(sb);
1795 BUG_ON(!osb);
1796
Sunil Mushran50397502008-12-17 14:17:43 -08001797 debugfs_remove(osb->osb_ctxt);
1798
Jan Kara19ece542008-08-21 20:13:17 +02001799 ocfs2_disable_quotas(osb);
1800
Mark Fashehccd979b2005-12-15 14:31:24 -08001801 ocfs2_shutdown_local_alloc(osb);
1802
1803 ocfs2_truncate_log_shutdown(osb);
1804
Joel Becker553abd02008-02-01 12:03:57 -08001805 /* This will disable recovery and flush any recovery work. */
1806 ocfs2_recovery_exit(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001807
1808 ocfs2_journal_shutdown(osb);
1809
1810 ocfs2_sync_blockdev(sb);
1811
Joel Becker4670c462008-02-01 14:39:35 -08001812 /* No cluster connection means we've failed during mount, so skip
1813 * all the steps which depended on that to complete. */
1814 if (osb->cconn) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001815 tmp = ocfs2_super_lock(osb, 1);
1816 if (tmp < 0) {
1817 mlog_errno(tmp);
1818 return;
1819 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001820 }
1821
Mark Fasheh19b613d2007-10-04 14:47:09 -07001822 if (osb->slot_num != OCFS2_INVALID_SLOT)
1823 ocfs2_put_slot(osb);
1824
Joel Becker4670c462008-02-01 14:39:35 -08001825 if (osb->cconn)
Mark Fasheh19b613d2007-10-04 14:47:09 -07001826 ocfs2_super_unlock(osb, 1);
1827
Mark Fashehccd979b2005-12-15 14:31:24 -08001828 ocfs2_release_system_inodes(osb);
1829
Joel Becker6953b4c2008-01-29 16:59:56 -08001830 /*
Joel Becker6953b4c2008-01-29 16:59:56 -08001831 * If we're dismounting due to mount error, mount.ocfs2 will clean
1832 * up heartbeat. If we're a local mount, there is no heartbeat.
1833 * If we failed before we got a uuid_str yet, we can't stop
1834 * heartbeat. Otherwise, do it.
1835 */
1836 if (!mnt_err && !ocfs2_mount_local(osb) && osb->uuid_str)
Joel Becker286eaa92008-02-01 15:03:57 -08001837 hangup_needed = 1;
1838
1839 if (osb->cconn)
1840 ocfs2_dlm_shutdown(osb, hangup_needed);
1841
1842 debugfs_remove(osb->osb_debug_root);
1843
1844 if (hangup_needed)
Joel Becker6953b4c2008-01-29 16:59:56 -08001845 ocfs2_cluster_hangup(osb->uuid_str, strlen(osb->uuid_str));
Mark Fashehccd979b2005-12-15 14:31:24 -08001846
1847 atomic_set(&osb->vol_state, VOLUME_DISMOUNTED);
1848
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001849 if (ocfs2_mount_local(osb))
1850 snprintf(nodestr, sizeof(nodestr), "local");
1851 else
Joel Becker19fdb622008-01-30 15:38:24 -08001852 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001853
1854 printk(KERN_INFO "ocfs2: Unmounting device (%s) on (node %s)\n",
1855 osb->dev_str, nodestr);
Mark Fashehccd979b2005-12-15 14:31:24 -08001856
1857 ocfs2_delete_osb(osb);
1858 kfree(osb);
1859 sb->s_dev = 0;
1860 sb->s_fs_info = NULL;
1861}
1862
1863static int ocfs2_setup_osb_uuid(struct ocfs2_super *osb, const unsigned char *uuid,
1864 unsigned uuid_bytes)
1865{
1866 int i, ret;
1867 char *ptr;
1868
1869 BUG_ON(uuid_bytes != OCFS2_VOL_UUID_LEN);
1870
Robert P. J. Daycd861282006-12-13 00:34:52 -08001871 osb->uuid_str = kzalloc(OCFS2_VOL_UUID_LEN * 2 + 1, GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001872 if (osb->uuid_str == NULL)
1873 return -ENOMEM;
1874
Mark Fashehccd979b2005-12-15 14:31:24 -08001875 for (i = 0, ptr = osb->uuid_str; i < OCFS2_VOL_UUID_LEN; i++) {
1876 /* print with null */
1877 ret = snprintf(ptr, 3, "%02X", uuid[i]);
1878 if (ret != 2) /* drop super cleans up */
1879 return -EINVAL;
1880 /* then only advance past the last char */
1881 ptr += 2;
1882 }
1883
1884 return 0;
1885}
1886
1887static int ocfs2_initialize_super(struct super_block *sb,
1888 struct buffer_head *bh,
1889 int sector_size)
1890{
Denis Chengbddb8eb32007-09-27 02:10:04 +08001891 int status;
Mark Fasheh5a254032007-07-20 12:56:16 -07001892 int i, cbits, bbits;
1893 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001894 struct inode *inode = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001895 struct ocfs2_journal *journal;
1896 __le32 uuid_net_key;
1897 struct ocfs2_super *osb;
1898
1899 mlog_entry_void();
1900
Robert P. J. Daycd861282006-12-13 00:34:52 -08001901 osb = kzalloc(sizeof(struct ocfs2_super), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001902 if (!osb) {
1903 status = -ENOMEM;
1904 mlog_errno(status);
1905 goto bail;
1906 }
1907
1908 sb->s_fs_info = osb;
1909 sb->s_op = &ocfs2_sops;
1910 sb->s_export_op = &ocfs2_export_ops;
Jan Kara19ece542008-08-21 20:13:17 +02001911 sb->s_qcop = &ocfs2_quotactl_ops;
1912 sb->dq_op = &ocfs2_quota_operations;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001913 sb->s_xattr = ocfs2_xattr_handlers;
Mark Fashehe0dceaf2007-08-09 16:52:30 -07001914 sb->s_time_gran = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001915 sb->s_flags |= MS_NOATIME;
1916 /* this is needed to support O_LARGEFILE */
Mark Fasheh5a254032007-07-20 12:56:16 -07001917 cbits = le32_to_cpu(di->id2.i_super.s_clustersize_bits);
1918 bbits = le32_to_cpu(di->id2.i_super.s_blocksize_bits);
1919 sb->s_maxbytes = ocfs2_max_file_offset(bbits, cbits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001920
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001921 osb->osb_dx_mask = (1 << (cbits - bbits)) - 1;
1922
1923 for (i = 0; i < 3; i++)
1924 osb->osb_dx_seed[i] = le32_to_cpu(di->id2.i_super.s_dx_seed[i]);
1925 osb->osb_dx_seed[3] = le32_to_cpu(di->id2.i_super.s_uuid_hash);
1926
Mark Fashehccd979b2005-12-15 14:31:24 -08001927 osb->sb = sb;
1928 /* Save off for ocfs2_rw_direct */
1929 osb->s_sectsize_bits = blksize_bits(sector_size);
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01001930 BUG_ON(!osb->s_sectsize_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001931
Mark Fasheh34d024f2007-09-24 15:56:19 -07001932 spin_lock_init(&osb->dc_task_lock);
1933 init_waitqueue_head(&osb->dc_event);
1934 osb->dc_work_sequence = 0;
1935 osb->dc_wake_sequence = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001936 INIT_LIST_HEAD(&osb->blocked_lock_list);
1937 osb->blocked_lock_count = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001938 spin_lock_init(&osb->osb_lock);
Tao Mac8b9cf92009-02-24 17:40:26 -08001939 spin_lock_init(&osb->osb_xattr_lock);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001940 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001941
1942 atomic_set(&osb->alloc_stats.moves, 0);
1943 atomic_set(&osb->alloc_stats.local_data, 0);
1944 atomic_set(&osb->alloc_stats.bitmap_data, 0);
1945 atomic_set(&osb->alloc_stats.bg_allocs, 0);
1946 atomic_set(&osb->alloc_stats.bg_extends, 0);
1947
1948 ocfs2_init_node_maps(osb);
1949
1950 snprintf(osb->dev_str, sizeof(osb->dev_str), "%u,%u",
1951 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1952
Joel Becker553abd02008-02-01 12:03:57 -08001953 status = ocfs2_recovery_init(osb);
1954 if (status) {
1955 mlog(ML_ERROR, "Unable to initialize recovery state\n");
1956 mlog_errno(status);
1957 goto bail;
1958 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001959
1960 init_waitqueue_head(&osb->checkpoint_event);
1961 atomic_set(&osb->needs_checkpoint, 0);
1962
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001963 osb->s_atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
1964
Mark Fashehccd979b2005-12-15 14:31:24 -08001965 osb->slot_num = OCFS2_INVALID_SLOT;
1966
Tiger Yang8154da32008-08-18 17:11:46 +08001967 osb->s_xattr_inline_size = le16_to_cpu(
1968 di->id2.i_super.s_xattr_inline_size);
Tiger Yangfdd77702008-08-18 17:08:55 +08001969
Mark Fashehccd979b2005-12-15 14:31:24 -08001970 osb->local_alloc_state = OCFS2_LA_UNUSED;
1971 osb->local_alloc_bh = NULL;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001972 INIT_DELAYED_WORK(&osb->la_enable_wq, ocfs2_la_enable_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08001973
Mark Fashehccd979b2005-12-15 14:31:24 -08001974 init_waitqueue_head(&osb->osb_mount_event);
1975
1976 osb->vol_label = kmalloc(OCFS2_MAX_VOL_LABEL_LEN, GFP_KERNEL);
1977 if (!osb->vol_label) {
1978 mlog(ML_ERROR, "unable to alloc vol label\n");
1979 status = -ENOMEM;
1980 goto bail;
1981 }
1982
Mark Fashehccd979b2005-12-15 14:31:24 -08001983 osb->max_slots = le16_to_cpu(di->id2.i_super.s_max_slots);
1984 if (osb->max_slots > OCFS2_MAX_SLOTS || osb->max_slots == 0) {
1985 mlog(ML_ERROR, "Invalid number of node slots (%u)\n",
1986 osb->max_slots);
1987 status = -EINVAL;
1988 goto bail;
1989 }
Sunil Mushran781ee3e2006-04-27 16:41:31 -07001990 mlog(0, "max_slots for this device: %u\n", osb->max_slots);
Mark Fashehccd979b2005-12-15 14:31:24 -08001991
Sunil Mushran539d8262008-07-14 17:31:10 -07001992 osb->slot_recovery_generations =
1993 kcalloc(osb->max_slots, sizeof(*osb->slot_recovery_generations),
1994 GFP_KERNEL);
1995 if (!osb->slot_recovery_generations) {
1996 status = -ENOMEM;
1997 mlog_errno(status);
1998 goto bail;
1999 }
2000
Mark Fashehb4df6ed2006-02-22 17:35:08 -08002001 init_waitqueue_head(&osb->osb_wipe_event);
2002 osb->osb_orphan_wipes = kcalloc(osb->max_slots,
2003 sizeof(*osb->osb_orphan_wipes),
2004 GFP_KERNEL);
2005 if (!osb->osb_orphan_wipes) {
2006 status = -ENOMEM;
2007 mlog_errno(status);
2008 goto bail;
2009 }
2010
Mark Fashehccd979b2005-12-15 14:31:24 -08002011 osb->s_feature_compat =
2012 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_compat);
2013 osb->s_feature_ro_compat =
2014 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_ro_compat);
2015 osb->s_feature_incompat =
2016 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_incompat);
2017
2018 if ((i = OCFS2_HAS_INCOMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_INCOMPAT_SUPP))) {
2019 mlog(ML_ERROR, "couldn't mount because of unsupported "
2020 "optional features (%x).\n", i);
2021 status = -EINVAL;
2022 goto bail;
2023 }
2024 if (!(osb->sb->s_flags & MS_RDONLY) &&
2025 (i = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP))) {
2026 mlog(ML_ERROR, "couldn't mount RDWR because of "
2027 "unsupported optional features (%x).\n", i);
2028 status = -EINVAL;
2029 goto bail;
2030 }
2031
Joel Beckerb61817e2008-02-01 15:08:23 -08002032 if (ocfs2_userspace_stack(osb)) {
2033 memcpy(osb->osb_cluster_stack,
2034 OCFS2_RAW_SB(di)->s_cluster_info.ci_stack,
2035 OCFS2_STACK_LABEL_LEN);
2036 osb->osb_cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
2037 if (strlen(osb->osb_cluster_stack) != OCFS2_STACK_LABEL_LEN) {
2038 mlog(ML_ERROR,
2039 "couldn't mount because of an invalid "
2040 "cluster stack label (%s) \n",
2041 osb->osb_cluster_stack);
2042 status = -EINVAL;
2043 goto bail;
2044 }
2045 } else {
2046 /* The empty string is identical with classic tools that
2047 * don't know about s_cluster_info. */
2048 osb->osb_cluster_stack[0] = '\0';
2049 }
2050
Mark Fashehccd979b2005-12-15 14:31:24 -08002051 get_random_bytes(&osb->s_next_generation, sizeof(u32));
2052
2053 /* FIXME
2054 * This should be done in ocfs2_journal_init(), but unknown
2055 * ordering issues will cause the filesystem to crash.
2056 * If anyone wants to figure out what part of the code
2057 * refers to osb->journal before ocfs2_journal_init() is run,
2058 * be my guest.
2059 */
2060 /* initialize our journal structure */
2061
Robert P. J. Daycd861282006-12-13 00:34:52 -08002062 journal = kzalloc(sizeof(struct ocfs2_journal), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08002063 if (!journal) {
2064 mlog(ML_ERROR, "unable to alloc journal\n");
2065 status = -ENOMEM;
2066 goto bail;
2067 }
2068 osb->journal = journal;
2069 journal->j_osb = osb;
2070
2071 atomic_set(&journal->j_num_trans, 0);
2072 init_rwsem(&journal->j_trans_barrier);
2073 init_waitqueue_head(&journal->j_checkpointed);
2074 spin_lock_init(&journal->j_lock);
2075 journal->j_trans_id = (unsigned long) 1;
2076 INIT_LIST_HEAD(&journal->j_la_cleanups);
David Howellsc4028952006-11-22 14:57:56 +00002077 INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
Mark Fashehccd979b2005-12-15 14:31:24 -08002078 journal->j_state = OCFS2_JOURNAL_FREE;
2079
Jan Karaea455f82009-01-12 23:20:31 +01002080 INIT_WORK(&osb->dentry_lock_work, ocfs2_drop_dl_inodes);
2081 osb->dentry_lock_list = NULL;
2082
Mark Fashehccd979b2005-12-15 14:31:24 -08002083 /* get some pseudo constants for clustersize bits */
2084 osb->s_clustersize_bits =
2085 le32_to_cpu(di->id2.i_super.s_clustersize_bits);
2086 osb->s_clustersize = 1 << osb->s_clustersize_bits;
2087 mlog(0, "clusterbits=%d\n", osb->s_clustersize_bits);
2088
2089 if (osb->s_clustersize < OCFS2_MIN_CLUSTERSIZE ||
2090 osb->s_clustersize > OCFS2_MAX_CLUSTERSIZE) {
2091 mlog(ML_ERROR, "Volume has invalid cluster size (%d)\n",
2092 osb->s_clustersize);
2093 status = -EINVAL;
2094 goto bail;
2095 }
2096
2097 if (ocfs2_clusters_to_blocks(osb->sb, le32_to_cpu(di->i_clusters) - 1)
2098 > (u32)~0UL) {
2099 mlog(ML_ERROR, "Volume might try to write to blocks beyond "
2100 "what jbd can address in 32 bits.\n");
2101 status = -EINVAL;
2102 goto bail;
2103 }
2104
2105 if (ocfs2_setup_osb_uuid(osb, di->id2.i_super.s_uuid,
2106 sizeof(di->id2.i_super.s_uuid))) {
2107 mlog(ML_ERROR, "Out of memory trying to setup our uuid.\n");
2108 status = -ENOMEM;
2109 goto bail;
2110 }
2111
Mark Fasheh78427042006-05-04 12:03:26 -07002112 memcpy(&uuid_net_key, di->id2.i_super.s_uuid, sizeof(uuid_net_key));
Mark Fashehccd979b2005-12-15 14:31:24 -08002113
2114 strncpy(osb->vol_label, di->id2.i_super.s_label, 63);
2115 osb->vol_label[63] = '\0';
2116 osb->root_blkno = le64_to_cpu(di->id2.i_super.s_root_blkno);
2117 osb->system_dir_blkno = le64_to_cpu(di->id2.i_super.s_system_dir_blkno);
2118 osb->first_cluster_group_blkno =
2119 le64_to_cpu(di->id2.i_super.s_first_cluster_group);
2120 osb->fs_generation = le32_to_cpu(di->i_fs_generation);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002121 osb->uuid_hash = le32_to_cpu(di->id2.i_super.s_uuid_hash);
Mark Fashehccd979b2005-12-15 14:31:24 -08002122 mlog(0, "vol_label: %s\n", osb->vol_label);
2123 mlog(0, "uuid: %s\n", osb->uuid_str);
Mark Fashehb0697052006-03-03 10:24:33 -08002124 mlog(0, "root_blkno=%llu, system_dir_blkno=%llu\n",
2125 (unsigned long long)osb->root_blkno,
2126 (unsigned long long)osb->system_dir_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002127
2128 osb->osb_dlm_debug = ocfs2_new_dlm_debug();
2129 if (!osb->osb_dlm_debug) {
2130 status = -ENOMEM;
2131 mlog_errno(status);
2132 goto bail;
2133 }
2134
2135 atomic_set(&osb->vol_state, VOLUME_INIT);
2136
2137 /* load root, system_dir, and all global system inodes */
2138 status = ocfs2_init_global_system_inodes(osb);
2139 if (status < 0) {
2140 mlog_errno(status);
2141 goto bail;
2142 }
2143
2144 /*
2145 * global bitmap
2146 */
2147 inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
2148 OCFS2_INVALID_SLOT);
2149 if (!inode) {
2150 status = -EINVAL;
2151 mlog_errno(status);
2152 goto bail;
2153 }
2154
2155 osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08002156 iput(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08002157
Tao Mae9d578a2007-12-18 15:46:10 +08002158 osb->bitmap_cpg = ocfs2_group_bitmap_size(sb) * 8;
Mark Fashehccd979b2005-12-15 14:31:24 -08002159
2160 status = ocfs2_init_slot_info(osb);
2161 if (status < 0) {
2162 mlog_errno(status);
2163 goto bail;
2164 }
2165
Mark Fashehccd979b2005-12-15 14:31:24 -08002166bail:
2167 mlog_exit(status);
2168 return status;
2169}
2170
2171/*
2172 * will return: -EAGAIN if it is ok to keep searching for superblocks
2173 * -EINVAL if there is a bad superblock
2174 * 0 on success
2175 */
2176static int ocfs2_verify_volume(struct ocfs2_dinode *di,
2177 struct buffer_head *bh,
2178 u32 blksz)
2179{
2180 int status = -EAGAIN;
2181
2182 mlog_entry_void();
2183
2184 if (memcmp(di->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
2185 strlen(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) {
Joel Beckerd030cc92008-12-11 15:04:14 -08002186 /* We have to do a raw check of the feature here */
2187 if (le32_to_cpu(di->id2.i_super.s_feature_incompat) &
2188 OCFS2_FEATURE_INCOMPAT_META_ECC) {
2189 status = ocfs2_block_check_validate(bh->b_data,
2190 bh->b_size,
2191 &di->i_check);
2192 if (status)
2193 goto out;
2194 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002195 status = -EINVAL;
2196 if ((1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits)) != blksz) {
2197 mlog(ML_ERROR, "found superblock with incorrect block "
2198 "size: found %u, should be %u\n",
2199 1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits),
2200 blksz);
2201 } else if (le16_to_cpu(di->id2.i_super.s_major_rev_level) !=
2202 OCFS2_MAJOR_REV_LEVEL ||
2203 le16_to_cpu(di->id2.i_super.s_minor_rev_level) !=
2204 OCFS2_MINOR_REV_LEVEL) {
2205 mlog(ML_ERROR, "found superblock with bad version: "
2206 "found %u.%u, should be %u.%u\n",
2207 le16_to_cpu(di->id2.i_super.s_major_rev_level),
2208 le16_to_cpu(di->id2.i_super.s_minor_rev_level),
2209 OCFS2_MAJOR_REV_LEVEL,
2210 OCFS2_MINOR_REV_LEVEL);
2211 } else if (bh->b_blocknr != le64_to_cpu(di->i_blkno)) {
2212 mlog(ML_ERROR, "bad block number on superblock: "
Mark Fashehb0697052006-03-03 10:24:33 -08002213 "found %llu, should be %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07002214 (unsigned long long)le64_to_cpu(di->i_blkno),
Mark Fashehb0697052006-03-03 10:24:33 -08002215 (unsigned long long)bh->b_blocknr);
Mark Fashehccd979b2005-12-15 14:31:24 -08002216 } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
2217 le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
2218 mlog(ML_ERROR, "bad cluster size found: %u\n",
2219 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
2220 } else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
2221 mlog(ML_ERROR, "bad root_blkno: 0\n");
2222 } else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
2223 mlog(ML_ERROR, "bad system_dir_blkno: 0\n");
2224 } else if (le16_to_cpu(di->id2.i_super.s_max_slots) > OCFS2_MAX_SLOTS) {
2225 mlog(ML_ERROR,
2226 "Superblock slots found greater than file system "
2227 "maximum: found %u, max %u\n",
2228 le16_to_cpu(di->id2.i_super.s_max_slots),
2229 OCFS2_MAX_SLOTS);
2230 } else {
2231 /* found it! */
2232 status = 0;
2233 }
2234 }
2235
Joel Beckerd030cc92008-12-11 15:04:14 -08002236out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002237 mlog_exit(status);
2238 return status;
2239}
2240
2241static int ocfs2_check_volume(struct ocfs2_super *osb)
2242{
Denis Chengbddb8eb32007-09-27 02:10:04 +08002243 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08002244 int dirty;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002245 int local;
Mark Fashehccd979b2005-12-15 14:31:24 -08002246 struct ocfs2_dinode *local_alloc = NULL; /* only used if we
2247 * recover
2248 * ourselves. */
2249
2250 mlog_entry_void();
2251
2252 /* Init our journal object. */
2253 status = ocfs2_journal_init(osb->journal, &dirty);
2254 if (status < 0) {
2255 mlog(ML_ERROR, "Could not initialize journal!\n");
2256 goto finally;
2257 }
2258
2259 /* If the journal was unmounted cleanly then we don't want to
2260 * recover anything. Otherwise, journal_load will do that
2261 * dirty work for us :) */
2262 if (!dirty) {
2263 status = ocfs2_journal_wipe(osb->journal, 0);
2264 if (status < 0) {
2265 mlog_errno(status);
2266 goto finally;
2267 }
2268 } else {
2269 mlog(ML_NOTICE, "File system was not unmounted cleanly, "
2270 "recovering volume.\n");
2271 }
2272
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002273 local = ocfs2_mount_local(osb);
2274
Mark Fashehccd979b2005-12-15 14:31:24 -08002275 /* will play back anything left in the journal. */
Sunil Mushran539d8262008-07-14 17:31:10 -07002276 status = ocfs2_journal_load(osb->journal, local, dirty);
Wengang Wang01af4822008-06-10 14:24:48 +08002277 if (status < 0) {
2278 mlog(ML_ERROR, "ocfs2 journal load failed! %d\n", status);
2279 goto finally;
2280 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002281
2282 if (dirty) {
2283 /* recover my local alloc if we didn't unmount cleanly. */
2284 status = ocfs2_begin_local_alloc_recovery(osb,
2285 osb->slot_num,
2286 &local_alloc);
2287 if (status < 0) {
2288 mlog_errno(status);
2289 goto finally;
2290 }
2291 /* we complete the recovery process after we've marked
2292 * ourselves as mounted. */
2293 }
2294
2295 mlog(0, "Journal loaded.\n");
2296
2297 status = ocfs2_load_local_alloc(osb);
2298 if (status < 0) {
2299 mlog_errno(status);
2300 goto finally;
2301 }
2302
2303 if (dirty) {
2304 /* Recovery will be completed after we've mounted the
2305 * rest of the volume. */
2306 osb->dirty = 1;
2307 osb->local_alloc_copy = local_alloc;
2308 local_alloc = NULL;
2309 }
2310
2311 /* go through each journal, trylock it and if you get the
2312 * lock, and it's marked as dirty, set the bit in the recover
2313 * map and launch a recovery thread for it. */
2314 status = ocfs2_mark_dead_nodes(osb);
Srinivas Eeda9140db02009-03-06 14:21:46 -08002315 if (status < 0) {
2316 mlog_errno(status);
2317 goto finally;
2318 }
2319
2320 status = ocfs2_compute_replay_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002321 if (status < 0)
2322 mlog_errno(status);
2323
2324finally:
2325 if (local_alloc)
2326 kfree(local_alloc);
2327
2328 mlog_exit(status);
2329 return status;
2330}
2331
2332/*
2333 * The routine gets called from dismount or close whenever a dismount on
2334 * volume is requested and the osb open count becomes 1.
2335 * It will remove the osb from the global list and also free up all the
2336 * initialized resources and fileobject.
2337 */
2338static void ocfs2_delete_osb(struct ocfs2_super *osb)
2339{
2340 mlog_entry_void();
2341
2342 /* This function assumes that the caller has the main osb resource */
2343
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002344 ocfs2_free_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002345
Mark Fashehb4df6ed2006-02-22 17:35:08 -08002346 kfree(osb->osb_orphan_wipes);
Sunil Mushran539d8262008-07-14 17:31:10 -07002347 kfree(osb->slot_recovery_generations);
Mark Fashehccd979b2005-12-15 14:31:24 -08002348 /* FIXME
2349 * This belongs in journal shutdown, but because we have to
2350 * allocate osb->journal at the start of ocfs2_initalize_osb(),
2351 * we free it here.
2352 */
2353 kfree(osb->journal);
2354 if (osb->local_alloc_copy)
2355 kfree(osb->local_alloc_copy);
2356 kfree(osb->uuid_str);
2357 ocfs2_put_dlm_debug(osb->osb_dlm_debug);
2358 memset(osb, 0, sizeof(struct ocfs2_super));
2359
2360 mlog_exit_void();
2361}
2362
2363/* Put OCFS2 into a readonly state, or (if the user specifies it),
2364 * panic(). We do not support continue-on-error operation. */
2365static void ocfs2_handle_error(struct super_block *sb)
2366{
2367 struct ocfs2_super *osb = OCFS2_SB(sb);
2368
2369 if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_PANIC)
2370 panic("OCFS2: (device %s): panic forced after error\n",
2371 sb->s_id);
2372
2373 ocfs2_set_osb_flag(osb, OCFS2_OSB_ERROR_FS);
2374
2375 if (sb->s_flags & MS_RDONLY &&
2376 (ocfs2_is_soft_readonly(osb) ||
2377 ocfs2_is_hard_readonly(osb)))
2378 return;
2379
2380 printk(KERN_CRIT "File system is now read-only due to the potential "
2381 "of on-disk corruption. Please run fsck.ocfs2 once the file "
2382 "system is unmounted.\n");
2383 sb->s_flags |= MS_RDONLY;
2384 ocfs2_set_ro_flag(osb, 0);
2385}
2386
2387static char error_buf[1024];
2388
2389void __ocfs2_error(struct super_block *sb,
2390 const char *function,
2391 const char *fmt, ...)
2392{
2393 va_list args;
2394
2395 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002396 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Mark Fashehccd979b2005-12-15 14:31:24 -08002397 va_end(args);
2398
2399 /* Not using mlog here because we want to show the actual
2400 * function the error came from. */
2401 printk(KERN_CRIT "OCFS2: ERROR (device %s): %s: %s\n",
2402 sb->s_id, function, error_buf);
2403
2404 ocfs2_handle_error(sb);
2405}
2406
2407/* Handle critical errors. This is intentionally more drastic than
2408 * ocfs2_handle_error, so we only use for things like journal errors,
2409 * etc. */
2410void __ocfs2_abort(struct super_block* sb,
2411 const char *function,
2412 const char *fmt, ...)
2413{
2414 va_list args;
2415
2416 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002417 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Mark Fashehccd979b2005-12-15 14:31:24 -08002418 va_end(args);
2419
2420 printk(KERN_CRIT "OCFS2: abort (device %s): %s: %s\n",
2421 sb->s_id, function, error_buf);
2422
2423 /* We don't have the cluster support yet to go straight to
2424 * hard readonly in here. Until then, we want to keep
2425 * ocfs2_abort() so that we can at least mark critical
2426 * errors.
2427 *
2428 * TODO: This should abort the journal and alert other nodes
2429 * that our slot needs recovery. */
2430
2431 /* Force a panic(). This stinks, but it's better than letting
2432 * things continue without having a proper hard readonly
2433 * here. */
2434 OCFS2_SB(sb)->s_mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
2435 ocfs2_handle_error(sb);
2436}
2437
2438module_init(ocfs2_init);
2439module_exit(ocfs2_exit);