]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/ocfs2/super.c
fix typos concerning "initiali[zs]e"
[net-next-2.6.git] / fs / ocfs2 / super.c
CommitLineData
ccd979bd
MF
1/* -*- 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>
ccd979bd
MF
31#include <linux/init.h>
32#include <linux/random.h>
33#include <linux/statfs.h>
34#include <linux/moduleparam.h>
35#include <linux/blkdev.h>
36#include <linux/socket.h>
37#include <linux/inet.h>
38#include <linux/parser.h>
39#include <linux/crc32.h>
40#include <linux/debugfs.h>
d550071c 41#include <linux/mount.h>
6953b4c0 42#include <linux/seq_file.h>
19ece546 43#include <linux/quotaops.h>
337eb00a 44#include <linux/smp_lock.h>
ccd979bd
MF
45
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"
d030cc97 55#include "blockcheck.h"
ccd979bd
MF
56#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"
cf1d6c76 69#include "xattr.h"
9e33d69f 70#include "quota.h"
374a263e 71#include "refcounttree.h"
b89c5428 72#include "suballoc.h"
ccd979bd
MF
73
74#include "buffer_head_io.h"
75
e18b890b 76static struct kmem_cache *ocfs2_inode_cachep = NULL;
9e33d69f
JK
77struct kmem_cache *ocfs2_dquot_cachep;
78struct kmem_cache *ocfs2_qf_chunk_cachep;
ccd979bd 79
ccd979bd
MF
80/* OCFS2 needs to schedule several differnt types of work which
81 * require cluster locking, disk I/O, recovery waits, etc. Since these
82 * types of work tend to be heavy we avoid using the kernel events
83 * workqueue and schedule on our own. */
84struct workqueue_struct *ocfs2_wq = NULL;
85
86static struct dentry *ocfs2_debugfs_root = NULL;
87
88MODULE_AUTHOR("Oracle");
89MODULE_LICENSE("GPL");
90
c0123ade
TY
91struct mount_options
92{
d147b3d6 93 unsigned long commit_interval;
c0123ade
TY
94 unsigned long mount_opt;
95 unsigned int atime_quantum;
96 signed short slot;
73c8a800 97 int localalloc_opt;
d02f00cc 98 unsigned int resv_level;
83f92318 99 int dir_resv_level;
b61817e1 100 char cluster_stack[OCFS2_STACK_LABEL_LEN + 1];
c0123ade
TY
101};
102
ccd979bd 103static int ocfs2_parse_options(struct super_block *sb, char *options,
c0123ade 104 struct mount_options *mopt,
baf4661a 105 int is_remount);
5297aad8
JK
106static int ocfs2_check_set_options(struct super_block *sb,
107 struct mount_options *options);
d550071c 108static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt);
ccd979bd
MF
109static void ocfs2_put_super(struct super_block *sb);
110static int ocfs2_mount_volume(struct super_block *sb);
111static int ocfs2_remount(struct super_block *sb, int *flags, char *data);
112static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err);
113static int ocfs2_initialize_mem_caches(void);
114static void ocfs2_free_mem_caches(void);
115static void ocfs2_delete_osb(struct ocfs2_super *osb);
116
726c3342 117static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf);
ccd979bd
MF
118
119static int ocfs2_sync_fs(struct super_block *sb, int wait);
120
121static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb);
122static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb);
bddb8eb3 123static void ocfs2_release_system_inodes(struct ocfs2_super *osb);
ccd979bd
MF
124static int ocfs2_check_volume(struct ocfs2_super *osb);
125static int ocfs2_verify_volume(struct ocfs2_dinode *di,
126 struct buffer_head *bh,
73be192b
JB
127 u32 sectsize,
128 struct ocfs2_blockcheck_stats *stats);
ccd979bd
MF
129static int ocfs2_initialize_super(struct super_block *sb,
130 struct buffer_head *bh,
73be192b
JB
131 int sector_size,
132 struct ocfs2_blockcheck_stats *stats);
ccd979bd
MF
133static int ocfs2_get_sector(struct super_block *sb,
134 struct buffer_head **bh,
135 int block,
136 int sect_size);
ccd979bd
MF
137static struct inode *ocfs2_alloc_inode(struct super_block *sb);
138static void ocfs2_destroy_inode(struct inode *inode);
19ece546
JK
139static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend);
140static int ocfs2_enable_quotas(struct ocfs2_super *osb);
141static void ocfs2_disable_quotas(struct ocfs2_super *osb);
ccd979bd 142
ee9b6d61 143static const struct super_operations ocfs2_sops = {
ccd979bd
MF
144 .statfs = ocfs2_statfs,
145 .alloc_inode = ocfs2_alloc_inode,
146 .destroy_inode = ocfs2_destroy_inode,
147 .drop_inode = ocfs2_drop_inode,
148 .clear_inode = ocfs2_clear_inode,
149 .delete_inode = ocfs2_delete_inode,
150 .sync_fs = ocfs2_sync_fs,
ccd979bd
MF
151 .put_super = ocfs2_put_super,
152 .remount_fs = ocfs2_remount,
d550071c 153 .show_options = ocfs2_show_options,
9e33d69f
JK
154 .quota_read = ocfs2_quota_read,
155 .quota_write = ocfs2_quota_write,
ccd979bd
MF
156};
157
158enum {
159 Opt_barrier,
160 Opt_err_panic,
161 Opt_err_ro,
162 Opt_intr,
163 Opt_nointr,
164 Opt_hb_none,
165 Opt_hb_local,
166 Opt_data_ordered,
167 Opt_data_writeback,
7f1a37e3 168 Opt_atime_quantum,
baf4661a 169 Opt_slot,
d147b3d6 170 Opt_commit,
2fbe8d1e 171 Opt_localalloc,
53fc622b 172 Opt_localflocks,
b61817e1 173 Opt_stack,
cf1d6c76
TY
174 Opt_user_xattr,
175 Opt_nouser_xattr,
12462f1d 176 Opt_inode64,
a68979b8
TY
177 Opt_acl,
178 Opt_noacl,
19ece546
JK
179 Opt_usrquota,
180 Opt_grpquota,
d02f00cc 181 Opt_resv_level,
83f92318 182 Opt_dir_resv_level,
ccd979bd
MF
183 Opt_err,
184};
185
a447c093 186static const match_table_t tokens = {
ccd979bd
MF
187 {Opt_barrier, "barrier=%u"},
188 {Opt_err_panic, "errors=panic"},
189 {Opt_err_ro, "errors=remount-ro"},
190 {Opt_intr, "intr"},
191 {Opt_nointr, "nointr"},
192 {Opt_hb_none, OCFS2_HB_NONE},
193 {Opt_hb_local, OCFS2_HB_LOCAL},
194 {Opt_data_ordered, "data=ordered"},
195 {Opt_data_writeback, "data=writeback"},
7f1a37e3 196 {Opt_atime_quantum, "atime_quantum=%u"},
baf4661a 197 {Opt_slot, "preferred_slot=%u"},
d147b3d6 198 {Opt_commit, "commit=%u"},
2fbe8d1e 199 {Opt_localalloc, "localalloc=%d"},
53fc622b 200 {Opt_localflocks, "localflocks"},
b61817e1 201 {Opt_stack, "cluster_stack=%s"},
cf1d6c76
TY
202 {Opt_user_xattr, "user_xattr"},
203 {Opt_nouser_xattr, "nouser_xattr"},
12462f1d 204 {Opt_inode64, "inode64"},
a68979b8
TY
205 {Opt_acl, "acl"},
206 {Opt_noacl, "noacl"},
19ece546
JK
207 {Opt_usrquota, "usrquota"},
208 {Opt_grpquota, "grpquota"},
d02f00cc 209 {Opt_resv_level, "resv_level=%u"},
83f92318 210 {Opt_dir_resv_level, "dir_resv_level=%u"},
ccd979bd
MF
211 {Opt_err, NULL}
212};
213
50397507
SM
214#ifdef CONFIG_DEBUG_FS
215static int ocfs2_osb_dump(struct ocfs2_super *osb, char *buf, int len)
216{
50397507
SM
217 struct ocfs2_cluster_connection *cconn = osb->cconn;
218 struct ocfs2_recovery_map *rm = osb->recovery_map;
df152c24
SM
219 struct ocfs2_orphan_scan *os = &osb->osb_orphan_scan;
220 int i, out = 0;
50397507
SM
221
222 out += snprintf(buf + out, len - out,
223 "%10s => Id: %-s Uuid: %-s Gen: 0x%X Label: %-s\n",
224 "Device", osb->dev_str, osb->uuid_str,
225 osb->fs_generation, osb->vol_label);
226
227 out += snprintf(buf + out, len - out,
228 "%10s => State: %d Flags: 0x%lX\n", "Volume",
229 atomic_read(&osb->vol_state), osb->osb_flags);
230
231 out += snprintf(buf + out, len - out,
232 "%10s => Block: %lu Cluster: %d\n", "Sizes",
233 osb->sb->s_blocksize, osb->s_clustersize);
234
235 out += snprintf(buf + out, len - out,
236 "%10s => Compat: 0x%X Incompat: 0x%X "
237 "ROcompat: 0x%X\n",
238 "Features", osb->s_feature_compat,
239 osb->s_feature_incompat, osb->s_feature_ro_compat);
240
241 out += snprintf(buf + out, len - out,
242 "%10s => Opts: 0x%lX AtimeQuanta: %u\n", "Mount",
243 osb->s_mount_opt, osb->s_atime_quantum);
244
c3d38840
SM
245 if (cconn) {
246 out += snprintf(buf + out, len - out,
247 "%10s => Stack: %s Name: %*s "
248 "Version: %d.%d\n", "Cluster",
249 (*osb->osb_cluster_stack == '\0' ?
250 "o2cb" : osb->osb_cluster_stack),
251 cconn->cc_namelen, cconn->cc_name,
252 cconn->cc_version.pv_major,
253 cconn->cc_version.pv_minor);
254 }
50397507
SM
255
256 spin_lock(&osb->dc_task_lock);
257 out += snprintf(buf + out, len - out,
258 "%10s => Pid: %d Count: %lu WakeSeq: %lu "
259 "WorkSeq: %lu\n", "DownCnvt",
c3d38840
SM
260 (osb->dc_task ? task_pid_nr(osb->dc_task) : -1),
261 osb->blocked_lock_count, osb->dc_wake_sequence,
262 osb->dc_work_sequence);
50397507
SM
263 spin_unlock(&osb->dc_task_lock);
264
265 spin_lock(&osb->osb_lock);
266 out += snprintf(buf + out, len - out, "%10s => Pid: %d Nodes:",
267 "Recovery",
268 (osb->recovery_thread_task ?
269 task_pid_nr(osb->recovery_thread_task) : -1));
270 if (rm->rm_used == 0)
271 out += snprintf(buf + out, len - out, " None\n");
272 else {
273 for (i = 0; i < rm->rm_used; i++)
274 out += snprintf(buf + out, len - out, " %d",
275 rm->rm_entries[i]);
276 out += snprintf(buf + out, len - out, "\n");
277 }
278 spin_unlock(&osb->osb_lock);
279
280 out += snprintf(buf + out, len - out,
281 "%10s => Pid: %d Interval: %lu Needs: %d\n", "Commit",
c3d38840
SM
282 (osb->commit_task ? task_pid_nr(osb->commit_task) : -1),
283 osb->osb_commit_interval,
50397507
SM
284 atomic_read(&osb->needs_checkpoint));
285
286 out += snprintf(buf + out, len - out,
c3d38840 287 "%10s => State: %d TxnId: %lu NumTxns: %d\n",
50397507 288 "Journal", osb->journal->j_state,
c3d38840
SM
289 osb->journal->j_trans_id,
290 atomic_read(&osb->journal->j_num_trans));
50397507
SM
291
292 out += snprintf(buf + out, len - out,
293 "%10s => GlobalAllocs: %d LocalAllocs: %d "
294 "SubAllocs: %d LAWinMoves: %d SAExtends: %d\n",
295 "Stats",
296 atomic_read(&osb->alloc_stats.bitmap_data),
297 atomic_read(&osb->alloc_stats.local_data),
298 atomic_read(&osb->alloc_stats.bg_allocs),
299 atomic_read(&osb->alloc_stats.moves),
300 atomic_read(&osb->alloc_stats.bg_extends));
301
302 out += snprintf(buf + out, len - out,
303 "%10s => State: %u Descriptor: %llu Size: %u bits "
304 "Default: %u bits\n",
305 "LocalAlloc", osb->local_alloc_state,
306 (unsigned long long)osb->la_last_gd,
307 osb->local_alloc_bits, osb->local_alloc_default_bits);
308
309 spin_lock(&osb->osb_lock);
310 out += snprintf(buf + out, len - out,
b89c5428
TY
311 "%10s => InodeSlot: %d StolenInodes: %d, "
312 "MetaSlot: %d StolenMeta: %d\n", "Steal",
50397507 313 osb->s_inode_steal_slot,
b89c5428
TY
314 atomic_read(&osb->s_num_inodes_stolen),
315 osb->s_meta_steal_slot,
316 atomic_read(&osb->s_num_meta_stolen));
50397507
SM
317 spin_unlock(&osb->osb_lock);
318
df152c24
SM
319 out += snprintf(buf + out, len - out, "OrphanScan => ");
320 out += snprintf(buf + out, len - out, "Local: %u Global: %u ",
321 os->os_count, os->os_seqno);
322 out += snprintf(buf + out, len - out, " Last Scan: ");
323 if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE)
324 out += snprintf(buf + out, len - out, "Disabled\n");
325 else
326 out += snprintf(buf + out, len - out, "%lu seconds ago\n",
327 (get_seconds() - os->os_scantime.tv_sec));
328
50397507
SM
329 out += snprintf(buf + out, len - out, "%10s => %3s %10s\n",
330 "Slots", "Num", "RecoGen");
50397507
SM
331 for (i = 0; i < osb->max_slots; ++i) {
332 out += snprintf(buf + out, len - out,
333 "%10s %c %3d %10d\n",
334 " ",
335 (i == osb->slot_num ? '*' : ' '),
336 i, osb->slot_recovery_generations[i]);
337 }
338
339 return out;
340}
341
342static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
343{
344 struct ocfs2_super *osb = inode->i_private;
345 char *buf = NULL;
346
347 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
348 if (!buf)
349 goto bail;
350
351 i_size_write(inode, ocfs2_osb_dump(osb, buf, PAGE_SIZE));
352
353 file->private_data = buf;
354
355 return 0;
356bail:
357 return -ENOMEM;
358}
359
360static int ocfs2_debug_release(struct inode *inode, struct file *file)
361{
362 kfree(file->private_data);
363 return 0;
364}
365
366static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
367 size_t nbytes, loff_t *ppos)
368{
369 return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
370 i_size_read(file->f_mapping->host));
371}
372#else
373static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
374{
375 return 0;
376}
377static int ocfs2_debug_release(struct inode *inode, struct file *file)
378{
379 return 0;
380}
381static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
382 size_t nbytes, loff_t *ppos)
383{
384 return 0;
385}
386#endif /* CONFIG_DEBUG_FS */
387
828c0950 388static const struct file_operations ocfs2_osb_debug_fops = {
50397507
SM
389 .open = ocfs2_osb_debug_open,
390 .release = ocfs2_debug_release,
391 .read = ocfs2_debug_read,
392 .llseek = generic_file_llseek,
393};
394
ccd979bd
MF
395static int ocfs2_sync_fs(struct super_block *sb, int wait)
396{
bddb8eb3 397 int status;
ccd979bd
MF
398 tid_t target;
399 struct ocfs2_super *osb = OCFS2_SB(sb);
400
ccd979bd
MF
401 if (ocfs2_is_hard_readonly(osb))
402 return -EROFS;
403
404 if (wait) {
405 status = ocfs2_flush_truncate_log(osb);
406 if (status < 0)
407 mlog_errno(status);
408 } else {
409 ocfs2_schedule_truncate_log_flush(osb, 0);
410 }
411
2b4e30fb
JB
412 if (jbd2_journal_start_commit(OCFS2_SB(sb)->journal->j_journal,
413 &target)) {
ccd979bd 414 if (wait)
2b4e30fb
JB
415 jbd2_log_wait_commit(OCFS2_SB(sb)->journal->j_journal,
416 target);
ccd979bd
MF
417 }
418 return 0;
419}
420
1a224ad1
JK
421static int ocfs2_need_system_inode(struct ocfs2_super *osb, int ino)
422{
423 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_USRQUOTA)
424 && (ino == USER_QUOTA_SYSTEM_INODE
425 || ino == LOCAL_USER_QUOTA_SYSTEM_INODE))
426 return 0;
427 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)
428 && (ino == GROUP_QUOTA_SYSTEM_INODE
429 || ino == LOCAL_GROUP_QUOTA_SYSTEM_INODE))
430 return 0;
431 return 1;
432}
433
ccd979bd
MF
434static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb)
435{
436 struct inode *new = NULL;
437 int status = 0;
438 int i;
439
440 mlog_entry_void();
441
5fa0613e 442 new = ocfs2_iget(osb, osb->root_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
ccd979bd
MF
443 if (IS_ERR(new)) {
444 status = PTR_ERR(new);
445 mlog_errno(status);
446 goto bail;
447 }
448 osb->root_inode = new;
449
5fa0613e 450 new = ocfs2_iget(osb, osb->system_dir_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
ccd979bd
MF
451 if (IS_ERR(new)) {
452 status = PTR_ERR(new);
453 mlog_errno(status);
454 goto bail;
455 }
456 osb->sys_root_inode = new;
457
458 for (i = OCFS2_FIRST_ONLINE_SYSTEM_INODE;
459 i <= OCFS2_LAST_GLOBAL_SYSTEM_INODE; i++) {
1a224ad1
JK
460 if (!ocfs2_need_system_inode(osb, i))
461 continue;
ccd979bd
MF
462 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
463 if (!new) {
464 ocfs2_release_system_inodes(osb);
465 status = -EINVAL;
466 mlog_errno(status);
467 /* FIXME: Should ERROR_RO_FS */
468 mlog(ML_ERROR, "Unable to load system inode %d, "
469 "possibly corrupt fs?", i);
470 goto bail;
471 }
472 // the array now has one ref, so drop this one
473 iput(new);
474 }
475
476bail:
477 mlog_exit(status);
478 return status;
479}
480
481static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb)
482{
483 struct inode *new = NULL;
484 int status = 0;
485 int i;
486
487 mlog_entry_void();
488
489 for (i = OCFS2_LAST_GLOBAL_SYSTEM_INODE + 1;
490 i < NUM_SYSTEM_INODES;
491 i++) {
1a224ad1
JK
492 if (!ocfs2_need_system_inode(osb, i))
493 continue;
ccd979bd
MF
494 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
495 if (!new) {
496 ocfs2_release_system_inodes(osb);
497 status = -EINVAL;
498 mlog(ML_ERROR, "status=%d, sysfile=%d, slot=%d\n",
499 status, i, osb->slot_num);
500 goto bail;
501 }
502 /* the array now has one ref, so drop this one */
503 iput(new);
504 }
505
506bail:
507 mlog_exit(status);
508 return status;
509}
510
bddb8eb3 511static void ocfs2_release_system_inodes(struct ocfs2_super *osb)
ccd979bd 512{
bddb8eb3 513 int i;
ccd979bd
MF
514 struct inode *inode;
515
516 mlog_entry_void();
517
518 for (i = 0; i < NUM_SYSTEM_INODES; i++) {
519 inode = osb->system_inodes[i];
520 if (inode) {
521 iput(inode);
522 osb->system_inodes[i] = NULL;
523 }
524 }
525
526 inode = osb->sys_root_inode;
527 if (inode) {
528 iput(inode);
529 osb->sys_root_inode = NULL;
530 }
531
532 inode = osb->root_inode;
533 if (inode) {
534 iput(inode);
535 osb->root_inode = NULL;
536 }
537
bddb8eb3 538 mlog_exit(0);
ccd979bd
MF
539}
540
541/* We're allocating fs objects, use GFP_NOFS */
542static struct inode *ocfs2_alloc_inode(struct super_block *sb)
543{
544 struct ocfs2_inode_info *oi;
545
e6b4f8da 546 oi = kmem_cache_alloc(ocfs2_inode_cachep, GFP_NOFS);
ccd979bd
MF
547 if (!oi)
548 return NULL;
549
2b4e30fb 550 jbd2_journal_init_jbd_inode(&oi->ip_jinode, &oi->vfs_inode);
ccd979bd
MF
551 return &oi->vfs_inode;
552}
553
554static void ocfs2_destroy_inode(struct inode *inode)
555{
556 kmem_cache_free(ocfs2_inode_cachep, OCFS2_I(inode));
557}
558
5a254031
MF
559static unsigned long long ocfs2_max_file_offset(unsigned int bbits,
560 unsigned int cbits)
ccd979bd 561{
5a254031
MF
562 unsigned int bytes = 1 << cbits;
563 unsigned int trim = bytes;
564 unsigned int bitshift = 32;
565
566 /*
567 * i_size and all block offsets in ocfs2 are always 64 bits
568 * wide. i_clusters is 32 bits, in cluster-sized units. So on
569 * 64 bit platforms, cluster size will be the limiting factor.
ccd979bd
MF
570 */
571
572#if BITS_PER_LONG == 32
90c699a9 573# if defined(CONFIG_LBDAF)
2ecd05ae 574 BUILD_BUG_ON(sizeof(sector_t) != 8);
5a254031
MF
575 /*
576 * We might be limited by page cache size.
577 */
578 if (bytes > PAGE_CACHE_SIZE) {
579 bytes = PAGE_CACHE_SIZE;
580 trim = 1;
581 /*
582 * Shift by 31 here so that we don't get larger than
583 * MAX_LFS_FILESIZE
584 */
585 bitshift = 31;
586 }
ccd979bd 587# else
5a254031
MF
588 /*
589 * We are limited by the size of sector_t. Use block size, as
590 * that's what we expose to the VFS.
591 */
592 bytes = 1 << bbits;
593 trim = 1;
594 bitshift = 31;
ccd979bd
MF
595# endif
596#endif
597
5a254031
MF
598 /*
599 * Trim by a whole cluster when we can actually approach the
600 * on-disk limits. Otherwise we can overflow i_clusters when
601 * an extent start is at the max offset.
602 */
603 return (((unsigned long long)bytes) << bitshift) - trim;
ccd979bd
MF
604}
605
606static int ocfs2_remount(struct super_block *sb, int *flags, char *data)
607{
608 int incompat_features;
609 int ret = 0;
c0123ade 610 struct mount_options parsed_options;
ccd979bd
MF
611 struct ocfs2_super *osb = OCFS2_SB(sb);
612
337eb00a
AIB
613 lock_kernel();
614
5297aad8
JK
615 if (!ocfs2_parse_options(sb, data, &parsed_options, 1) ||
616 !ocfs2_check_set_options(sb, &parsed_options)) {
ccd979bd
MF
617 ret = -EINVAL;
618 goto out;
619 }
620
621 if ((osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) !=
c0123ade 622 (parsed_options.mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
ccd979bd
MF
623 ret = -EINVAL;
624 mlog(ML_ERROR, "Cannot change heartbeat mode on remount\n");
625 goto out;
626 }
627
628 if ((osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK) !=
c0123ade 629 (parsed_options.mount_opt & OCFS2_MOUNT_DATA_WRITEBACK)) {
ccd979bd
MF
630 ret = -EINVAL;
631 mlog(ML_ERROR, "Cannot change data mode on remount\n");
632 goto out;
633 }
634
12462f1d
JB
635 /* Probably don't want this on remount; it might
636 * mess with other nodes */
637 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64) &&
638 (parsed_options.mount_opt & OCFS2_MOUNT_INODE64)) {
639 ret = -EINVAL;
640 mlog(ML_ERROR, "Cannot enable inode64 on remount\n");
641 goto out;
642 }
643
ccd979bd
MF
644 /* We're going to/from readonly mode. */
645 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
19ece546
JK
646 /* Disable quota accounting before remounting RO */
647 if (*flags & MS_RDONLY) {
648 ret = ocfs2_susp_quotas(osb, 0);
649 if (ret < 0)
650 goto out;
651 }
ccd979bd
MF
652 /* Lock here so the check of HARD_RO and the potential
653 * setting of SOFT_RO is atomic. */
654 spin_lock(&osb->osb_lock);
655 if (osb->osb_flags & OCFS2_OSB_HARD_RO) {
656 mlog(ML_ERROR, "Remount on readonly device is forbidden.\n");
657 ret = -EROFS;
658 goto unlock_osb;
659 }
660
661 if (*flags & MS_RDONLY) {
662 mlog(0, "Going to ro mode.\n");
663 sb->s_flags |= MS_RDONLY;
664 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
665 } else {
666 mlog(0, "Making ro filesystem writeable.\n");
667
668 if (osb->osb_flags & OCFS2_OSB_ERROR_FS) {
669 mlog(ML_ERROR, "Cannot remount RDWR "
670 "filesystem due to previous errors.\n");
671 ret = -EROFS;
672 goto unlock_osb;
673 }
674 incompat_features = OCFS2_HAS_RO_COMPAT_FEATURE(sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP);
675 if (incompat_features) {
676 mlog(ML_ERROR, "Cannot remount RDWR because "
677 "of unsupported optional features "
678 "(%x).\n", incompat_features);
679 ret = -EINVAL;
680 goto unlock_osb;
681 }
682 sb->s_flags &= ~MS_RDONLY;
683 osb->osb_flags &= ~OCFS2_OSB_SOFT_RO;
684 }
685unlock_osb:
686 spin_unlock(&osb->osb_lock);
19ece546
JK
687 /* Enable quota accounting after remounting RW */
688 if (!ret && !(*flags & MS_RDONLY)) {
689 if (sb_any_quota_suspended(sb))
690 ret = ocfs2_susp_quotas(osb, 1);
691 else
692 ret = ocfs2_enable_quotas(osb);
693 if (ret < 0) {
694 /* Return back changes... */
695 spin_lock(&osb->osb_lock);
696 sb->s_flags |= MS_RDONLY;
697 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
698 spin_unlock(&osb->osb_lock);
699 goto out;
700 }
701 }
ccd979bd
MF
702 }
703
704 if (!ret) {
ccd979bd
MF
705 /* Only save off the new mount options in case of a successful
706 * remount. */
c0123ade
TY
707 osb->s_mount_opt = parsed_options.mount_opt;
708 osb->s_atime_quantum = parsed_options.atime_quantum;
709 osb->preferred_slot = parsed_options.slot;
d147b3d6
MF
710 if (parsed_options.commit_interval)
711 osb->osb_commit_interval = parsed_options.commit_interval;
e001e796
MF
712
713 if (!ocfs2_is_hard_readonly(osb))
714 ocfs2_set_journal_params(osb);
57b09bb5
JK
715
716 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
717 ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ?
718 MS_POSIXACL : 0);
ccd979bd
MF
719 }
720out:
337eb00a 721 unlock_kernel();
ccd979bd
MF
722 return ret;
723}
724
725static int ocfs2_sb_probe(struct super_block *sb,
726 struct buffer_head **bh,
73be192b
JB
727 int *sector_size,
728 struct ocfs2_blockcheck_stats *stats)
ccd979bd 729{
bddb8eb3 730 int status, tmpstat;
ccd979bd
MF
731 struct ocfs1_vol_disk_hdr *hdr;
732 struct ocfs2_dinode *di;
733 int blksize;
734
735 *bh = NULL;
736
737 /* may be > 512 */
e1defc4f 738 *sector_size = bdev_logical_block_size(sb->s_bdev);
ccd979bd
MF
739 if (*sector_size > OCFS2_MAX_BLOCKSIZE) {
740 mlog(ML_ERROR, "Hardware sector size too large: %d (max=%d)\n",
741 *sector_size, OCFS2_MAX_BLOCKSIZE);
742 status = -EINVAL;
743 goto bail;
744 }
745
746 /* Can this really happen? */
747 if (*sector_size < OCFS2_MIN_BLOCKSIZE)
748 *sector_size = OCFS2_MIN_BLOCKSIZE;
749
750 /* check block zero for old format */
751 status = ocfs2_get_sector(sb, bh, 0, *sector_size);
752 if (status < 0) {
753 mlog_errno(status);
754 goto bail;
755 }
756 hdr = (struct ocfs1_vol_disk_hdr *) (*bh)->b_data;
757 if (hdr->major_version == OCFS1_MAJOR_VERSION) {
758 mlog(ML_ERROR, "incompatible version: %u.%u\n",
759 hdr->major_version, hdr->minor_version);
760 status = -EINVAL;
761 }
762 if (memcmp(hdr->signature, OCFS1_VOLUME_SIGNATURE,
763 strlen(OCFS1_VOLUME_SIGNATURE)) == 0) {
764 mlog(ML_ERROR, "incompatible volume signature: %8s\n",
765 hdr->signature);
766 status = -EINVAL;
767 }
768 brelse(*bh);
769 *bh = NULL;
770 if (status < 0) {
771 mlog(ML_ERROR, "This is an ocfs v1 filesystem which must be "
772 "upgraded before mounting with ocfs v2\n");
773 goto bail;
774 }
775
776 /*
777 * Now check at magic offset for 512, 1024, 2048, 4096
778 * blocksizes. 4096 is the maximum blocksize because it is
779 * the minimum clustersize.
780 */
781 status = -EINVAL;
782 for (blksize = *sector_size;
783 blksize <= OCFS2_MAX_BLOCKSIZE;
784 blksize <<= 1) {
785 tmpstat = ocfs2_get_sector(sb, bh,
786 OCFS2_SUPER_BLOCK_BLKNO,
787 blksize);
788 if (tmpstat < 0) {
789 status = tmpstat;
790 mlog_errno(status);
fb5cbe9e 791 break;
ccd979bd
MF
792 }
793 di = (struct ocfs2_dinode *) (*bh)->b_data;
73be192b 794 memset(stats, 0, sizeof(struct ocfs2_blockcheck_stats));
1c1d9793 795 spin_lock_init(&stats->b_lock);
fb5cbe9e
JB
796 tmpstat = ocfs2_verify_volume(di, *bh, blksize, stats);
797 if (tmpstat < 0) {
798 brelse(*bh);
799 *bh = NULL;
800 }
801 if (tmpstat != -EAGAIN) {
802 status = tmpstat;
ccd979bd 803 break;
fb5cbe9e 804 }
ccd979bd
MF
805 }
806
807bail:
808 return status;
809}
810
c271c5c2
SM
811static int ocfs2_verify_heartbeat(struct ocfs2_super *osb)
812{
813 if (ocfs2_mount_local(osb)) {
814 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
815 mlog(ML_ERROR, "Cannot heartbeat on a locally "
816 "mounted device.\n");
817 return -EINVAL;
818 }
819 }
820
b61817e1
JB
821 if (ocfs2_userspace_stack(osb)) {
822 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
823 mlog(ML_ERROR, "Userspace stack expected, but "
824 "o2cb heartbeat arguments passed to mount\n");
825 return -EINVAL;
826 }
827 }
828
c271c5c2 829 if (!(osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
b61817e1
JB
830 if (!ocfs2_mount_local(osb) && !ocfs2_is_hard_readonly(osb) &&
831 !ocfs2_userspace_stack(osb)) {
c271c5c2
SM
832 mlog(ML_ERROR, "Heartbeat has to be started to mount "
833 "a read-write clustered device.\n");
834 return -EINVAL;
835 }
836 }
837
838 return 0;
839}
840
b61817e1
JB
841/*
842 * If we're using a userspace stack, mount should have passed
843 * a name that matches the disk. If not, mount should not
844 * have passed a stack.
845 */
846static int ocfs2_verify_userspace_stack(struct ocfs2_super *osb,
847 struct mount_options *mopt)
848{
849 if (!ocfs2_userspace_stack(osb) && mopt->cluster_stack[0]) {
850 mlog(ML_ERROR,
851 "cluster stack passed to mount, but this filesystem "
852 "does not support it\n");
853 return -EINVAL;
854 }
855
856 if (ocfs2_userspace_stack(osb) &&
857 strncmp(osb->osb_cluster_stack, mopt->cluster_stack,
858 OCFS2_STACK_LABEL_LEN)) {
859 mlog(ML_ERROR,
860 "cluster stack passed to mount (\"%s\") does not "
861 "match the filesystem (\"%s\")\n",
862 mopt->cluster_stack,
863 osb->osb_cluster_stack);
864 return -EINVAL;
865 }
866
867 return 0;
868}
869
19ece546
JK
870static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend)
871{
872 int type;
873 struct super_block *sb = osb->sb;
874 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
875 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
876 int status = 0;
877
878 for (type = 0; type < MAXQUOTAS; type++) {
879 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
880 continue;
881 if (unsuspend)
882 status = vfs_quota_enable(
883 sb_dqopt(sb)->files[type],
884 type, QFMT_OCFS2,
885 DQUOT_SUSPENDED);
886 else
887 status = vfs_quota_disable(sb, type,
888 DQUOT_SUSPENDED);
889 if (status < 0)
890 break;
891 }
892 if (status < 0)
893 mlog(ML_ERROR, "Failed to suspend/unsuspend quotas on "
894 "remount (error = %d).\n", status);
895 return status;
896}
897
898static int ocfs2_enable_quotas(struct ocfs2_super *osb)
899{
900 struct inode *inode[MAXQUOTAS] = { NULL, NULL };
901 struct super_block *sb = osb->sb;
902 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
903 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
904 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
905 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
906 int status;
907 int type;
908
909 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NEGATIVE_USAGE;
910 for (type = 0; type < MAXQUOTAS; type++) {
911 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
912 continue;
913 inode[type] = ocfs2_get_system_file_inode(osb, ino[type],
914 osb->slot_num);
915 if (!inode[type]) {
916 status = -ENOENT;
917 goto out_quota_off;
918 }
919 status = vfs_quota_enable(inode[type], type, QFMT_OCFS2,
920 DQUOT_USAGE_ENABLED);
921 if (status < 0)
922 goto out_quota_off;
923 }
924
925 for (type = 0; type < MAXQUOTAS; type++)
926 iput(inode[type]);
927 return 0;
928out_quota_off:
929 ocfs2_disable_quotas(osb);
930 for (type = 0; type < MAXQUOTAS; type++)
931 iput(inode[type]);
932 mlog_errno(status);
933 return status;
934}
935
936static void ocfs2_disable_quotas(struct ocfs2_super *osb)
937{
938 int type;
939 struct inode *inode;
940 struct super_block *sb = osb->sb;
c06bcbfa 941 struct ocfs2_mem_dqinfo *oinfo;
19ece546
JK
942
943 /* We mostly ignore errors in this function because there's not much
944 * we can do when we see them */
945 for (type = 0; type < MAXQUOTAS; type++) {
946 if (!sb_has_quota_loaded(sb, type))
947 continue;
c06bcbfa
JK
948 /* Cancel periodic syncing before we grab dqonoff_mutex */
949 oinfo = sb_dqinfo(sb, type)->dqi_priv;
950 cancel_delayed_work_sync(&oinfo->dqi_sync_work);
19ece546
JK
951 inode = igrab(sb->s_dquot.files[type]);
952 /* Turn off quotas. This will remove all dquot structures from
953 * memory and so they will be automatically synced to global
954 * quota files */
955 vfs_quota_disable(sb, type, DQUOT_USAGE_ENABLED |
956 DQUOT_LIMITS_ENABLED);
957 if (!inode)
958 continue;
959 iput(inode);
960 }
961}
962
963/* Handle quota on quotactl */
964static int ocfs2_quota_on(struct super_block *sb, int type, int format_id,
965 char *path, int remount)
966{
967 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
968 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
969
970 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
971 return -EINVAL;
972
973 if (remount)
974 return 0; /* Just ignore it has been handled in
975 * ocfs2_remount() */
976 return vfs_quota_enable(sb_dqopt(sb)->files[type], type,
977 format_id, DQUOT_LIMITS_ENABLED);
978}
979
980/* Handle quota off quotactl */
981static int ocfs2_quota_off(struct super_block *sb, int type, int remount)
982{
983 if (remount)
984 return 0; /* Ignore now and handle later in
985 * ocfs2_remount() */
986 return vfs_quota_disable(sb, type, DQUOT_LIMITS_ENABLED);
987}
988
0d54b217 989static const struct quotactl_ops ocfs2_quotactl_ops = {
19ece546
JK
990 .quota_on = ocfs2_quota_on,
991 .quota_off = ocfs2_quota_off,
992 .quota_sync = vfs_quota_sync,
993 .get_info = vfs_get_dqinfo,
994 .set_info = vfs_set_dqinfo,
995 .get_dqblk = vfs_get_dqblk,
996 .set_dqblk = vfs_set_dqblk,
997};
998
ccd979bd
MF
999static int ocfs2_fill_super(struct super_block *sb, void *data, int silent)
1000{
1001 struct dentry *root;
1002 int status, sector_size;
c0123ade 1003 struct mount_options parsed_options;
ccd979bd
MF
1004 struct inode *inode = NULL;
1005 struct ocfs2_super *osb = NULL;
1006 struct buffer_head *bh = NULL;
c271c5c2 1007 char nodestr[8];
73be192b 1008 struct ocfs2_blockcheck_stats stats;
ccd979bd
MF
1009
1010 mlog_entry("%p, %p, %i", sb, data, silent);
1011
c0123ade 1012 if (!ocfs2_parse_options(sb, data, &parsed_options, 0)) {
ccd979bd
MF
1013 status = -EINVAL;
1014 goto read_super_error;
1015 }
1016
1017 /* probe for superblock */
73be192b 1018 status = ocfs2_sb_probe(sb, &bh, &sector_size, &stats);
ccd979bd
MF
1019 if (status < 0) {
1020 mlog(ML_ERROR, "superblock probe failed!\n");
1021 goto read_super_error;
1022 }
1023
73be192b 1024 status = ocfs2_initialize_super(sb, bh, sector_size, &stats);
ccd979bd
MF
1025 osb = OCFS2_SB(sb);
1026 if (status < 0) {
1027 mlog_errno(status);
1028 goto read_super_error;
1029 }
1030 brelse(bh);
1031 bh = NULL;
a68979b8 1032
5297aad8
JK
1033 if (!ocfs2_check_set_options(sb, &parsed_options)) {
1034 status = -EINVAL;
1035 goto read_super_error;
1036 }
c0123ade
TY
1037 osb->s_mount_opt = parsed_options.mount_opt;
1038 osb->s_atime_quantum = parsed_options.atime_quantum;
1039 osb->preferred_slot = parsed_options.slot;
d147b3d6 1040 osb->osb_commit_interval = parsed_options.commit_interval;
73c8a800
MF
1041
1042 ocfs2_la_set_sizes(osb, parsed_options.localalloc_opt);
d02f00cc 1043 osb->osb_resv_level = parsed_options.resv_level;
83f92318
MF
1044 osb->osb_dir_resv_level = parsed_options.resv_level;
1045 if (parsed_options.dir_resv_level == -1)
1046 osb->osb_dir_resv_level = parsed_options.resv_level;
1047 else
1048 osb->osb_dir_resv_level = parsed_options.dir_resv_level;
ccd979bd 1049
b61817e1
JB
1050 status = ocfs2_verify_userspace_stack(osb, &parsed_options);
1051 if (status)
1052 goto read_super_error;
1053
ccd979bd
MF
1054 sb->s_magic = OCFS2_SUPER_MAGIC;
1055
a68979b8
TY
1056 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1057 ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
1058
ccd979bd
MF
1059 /* Hard readonly mode only if: bdev_read_only, MS_RDONLY,
1060 * heartbeat=none */
1061 if (bdev_read_only(sb->s_bdev)) {
1062 if (!(sb->s_flags & MS_RDONLY)) {
1063 status = -EACCES;
1064 mlog(ML_ERROR, "Readonly device detected but readonly "
1065 "mount was not specified.\n");
1066 goto read_super_error;
1067 }
1068
1069 /* You should not be able to start a local heartbeat
1070 * on a readonly device. */
1071 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
1072 status = -EROFS;
1073 mlog(ML_ERROR, "Local heartbeat specified on readonly "
1074 "device.\n");
1075 goto read_super_error;
1076 }
1077
1078 status = ocfs2_check_journals_nolocks(osb);
1079 if (status < 0) {
1080 if (status == -EROFS)
1081 mlog(ML_ERROR, "Recovery required on readonly "
1082 "file system, but write access is "
1083 "unavailable.\n");
1084 else
2bd63216 1085 mlog_errno(status);
ccd979bd
MF
1086 goto read_super_error;
1087 }
1088
1089 ocfs2_set_ro_flag(osb, 1);
1090
1091 printk(KERN_NOTICE "Readonly device detected. No cluster "
1092 "services will be utilized for this mount. Recovery "
1093 "will be skipped.\n");
1094 }
1095
1096 if (!ocfs2_is_hard_readonly(osb)) {
ccd979bd
MF
1097 if (sb->s_flags & MS_RDONLY)
1098 ocfs2_set_ro_flag(osb, 0);
1099 }
1100
c271c5c2
SM
1101 status = ocfs2_verify_heartbeat(osb);
1102 if (status < 0) {
1103 mlog_errno(status);
1104 goto read_super_error;
1105 }
1106
ccd979bd
MF
1107 osb->osb_debug_root = debugfs_create_dir(osb->uuid_str,
1108 ocfs2_debugfs_root);
1109 if (!osb->osb_debug_root) {
1110 status = -EINVAL;
1111 mlog(ML_ERROR, "Unable to create per-mount debugfs root.\n");
1112 goto read_super_error;
1113 }
1114
50397507
SM
1115 osb->osb_ctxt = debugfs_create_file("fs_state", S_IFREG|S_IRUSR,
1116 osb->osb_debug_root,
1117 osb,
1118 &ocfs2_osb_debug_fops);
1119 if (!osb->osb_ctxt) {
1120 status = -EINVAL;
1121 mlog_errno(status);
1122 goto read_super_error;
1123 }
1124
73be192b
JB
1125 if (ocfs2_meta_ecc(osb)) {
1126 status = ocfs2_blockcheck_stats_debugfs_install(
1127 &osb->osb_ecc_stats,
1128 osb->osb_debug_root);
1129 if (status) {
1130 mlog(ML_ERROR,
1131 "Unable to create blockcheck statistics "
1132 "files\n");
1133 goto read_super_error;
1134 }
1135 }
1136
ccd979bd
MF
1137 status = ocfs2_mount_volume(sb);
1138 if (osb->root_inode)
1139 inode = igrab(osb->root_inode);
1140
1141 if (status < 0)
1142 goto read_super_error;
1143
1144 if (!inode) {
1145 status = -EIO;
1146 mlog_errno(status);
1147 goto read_super_error;
1148 }
1149
1150 root = d_alloc_root(inode);
1151 if (!root) {
1152 status = -ENOMEM;
1153 mlog_errno(status);
1154 goto read_super_error;
1155 }
1156
1157 sb->s_root = root;
1158
1159 ocfs2_complete_mount_recovery(osb);
1160
c271c5c2
SM
1161 if (ocfs2_mount_local(osb))
1162 snprintf(nodestr, sizeof(nodestr), "local");
1163 else
19fdb624 1164 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
c271c5c2
SM
1165
1166 printk(KERN_INFO "ocfs2: Mounting device (%s) on (node %s, slot %d) "
781ee3e2 1167 "with %s data mode.\n",
c271c5c2 1168 osb->dev_str, nodestr, osb->slot_num,
ccd979bd
MF
1169 osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK ? "writeback" :
1170 "ordered");
1171
1172 atomic_set(&osb->vol_state, VOLUME_MOUNTED);
1173 wake_up(&osb->osb_mount_event);
1174
19ece546
JK
1175 /* Now we can initialize quotas because we can afford to wait
1176 * for cluster locks recovery now. That also means that truncation
1177 * log recovery can happen but that waits for proper quota setup */
1178 if (!(sb->s_flags & MS_RDONLY)) {
1179 status = ocfs2_enable_quotas(osb);
1180 if (status < 0) {
1181 /* We have to err-out specially here because
1182 * s_root is already set */
1183 mlog_errno(status);
1184 atomic_set(&osb->vol_state, VOLUME_DISABLED);
1185 wake_up(&osb->osb_mount_event);
1186 mlog_exit(status);
1187 return status;
1188 }
1189 }
1190
1191 ocfs2_complete_quota_recovery(osb);
1192
1193 /* Now we wake up again for processes waiting for quotas */
1194 atomic_set(&osb->vol_state, VOLUME_MOUNTED_QUOTAS);
1195 wake_up(&osb->osb_mount_event);
1196
df152c24 1197 /* Start this when the mount is almost sure of being successful */
8b712cd5 1198 ocfs2_orphan_scan_start(osb);
df152c24 1199
ccd979bd
MF
1200 mlog_exit(status);
1201 return status;
1202
1203read_super_error:
a81cb88b 1204 brelse(bh);
ccd979bd
MF
1205
1206 if (inode)
1207 iput(inode);
1208
1209 if (osb) {
1210 atomic_set(&osb->vol_state, VOLUME_DISABLED);
1211 wake_up(&osb->osb_mount_event);
1212 ocfs2_dismount_volume(sb, 1);
1213 }
1214
1215 mlog_exit(status);
1216 return status;
1217}
1218
454e2398
DH
1219static int ocfs2_get_sb(struct file_system_type *fs_type,
1220 int flags,
1221 const char *dev_name,
1222 void *data,
1223 struct vfsmount *mnt)
ccd979bd 1224{
454e2398
DH
1225 return get_sb_bdev(fs_type, flags, dev_name, data, ocfs2_fill_super,
1226 mnt);
ccd979bd
MF
1227}
1228
f7b1aa69
JK
1229static void ocfs2_kill_sb(struct super_block *sb)
1230{
1231 struct ocfs2_super *osb = OCFS2_SB(sb);
1232
5fd13189
JK
1233 /* Failed mount? */
1234 if (!osb || atomic_read(&osb->vol_state) == VOLUME_DISABLED)
1235 goto out;
1236
f7b1aa69
JK
1237 /* Prevent further queueing of inode drop events */
1238 spin_lock(&dentry_list_lock);
1239 ocfs2_set_osb_flag(osb, OCFS2_OSB_DROP_DENTRY_LOCK_IMMED);
1240 spin_unlock(&dentry_list_lock);
1241 /* Wait for work to finish and/or remove it */
1242 cancel_work_sync(&osb->dentry_lock_work);
5fd13189 1243out:
f7b1aa69
JK
1244 kill_block_super(sb);
1245}
1246
ccd979bd
MF
1247static struct file_system_type ocfs2_fs_type = {
1248 .owner = THIS_MODULE,
1249 .name = "ocfs2",
1250 .get_sb = ocfs2_get_sb, /* is this called when we mount
1251 * the fs? */
f7b1aa69
JK
1252 .kill_sb = ocfs2_kill_sb,
1253
1ba9da2f 1254 .fs_flags = FS_REQUIRES_DEV|FS_RENAME_DOES_D_MOVE,
ccd979bd
MF
1255 .next = NULL
1256};
1257
5297aad8
JK
1258static int ocfs2_check_set_options(struct super_block *sb,
1259 struct mount_options *options)
1260{
1261 if (options->mount_opt & OCFS2_MOUNT_USRQUOTA &&
1262 !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1263 OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
1264 mlog(ML_ERROR, "User quotas were requested, but this "
1265 "filesystem does not have the feature enabled.\n");
1266 return 0;
1267 }
1268 if (options->mount_opt & OCFS2_MOUNT_GRPQUOTA &&
1269 !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1270 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
1271 mlog(ML_ERROR, "Group quotas were requested, but this "
1272 "filesystem does not have the feature enabled.\n");
1273 return 0;
1274 }
1275 if (options->mount_opt & OCFS2_MOUNT_POSIX_ACL &&
1276 !OCFS2_HAS_INCOMPAT_FEATURE(sb, OCFS2_FEATURE_INCOMPAT_XATTR)) {
1277 mlog(ML_ERROR, "ACL support requested but extended attributes "
1278 "feature is not enabled\n");
1279 return 0;
1280 }
1281 /* No ACL setting specified? Use XATTR feature... */
1282 if (!(options->mount_opt & (OCFS2_MOUNT_POSIX_ACL |
1283 OCFS2_MOUNT_NO_POSIX_ACL))) {
1284 if (OCFS2_HAS_INCOMPAT_FEATURE(sb, OCFS2_FEATURE_INCOMPAT_XATTR))
1285 options->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
1286 else
1287 options->mount_opt |= OCFS2_MOUNT_NO_POSIX_ACL;
1288 }
1289 return 1;
1290}
1291
ccd979bd
MF
1292static int ocfs2_parse_options(struct super_block *sb,
1293 char *options,
c0123ade 1294 struct mount_options *mopt,
ccd979bd
MF
1295 int is_remount)
1296{
1297 int status;
1298 char *p;
1299
1300 mlog_entry("remount: %d, options: \"%s\"\n", is_remount,
1301 options ? options : "(none)");
1302
d147b3d6 1303 mopt->commit_interval = 0;
4b37fcb7 1304 mopt->mount_opt = OCFS2_MOUNT_NOINTR;
c0123ade
TY
1305 mopt->atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
1306 mopt->slot = OCFS2_INVALID_SLOT;
73c8a800 1307 mopt->localalloc_opt = -1;
b61817e1 1308 mopt->cluster_stack[0] = '\0';
d02f00cc 1309 mopt->resv_level = OCFS2_DEFAULT_RESV_LEVEL;
83f92318 1310 mopt->dir_resv_level = -1;
ccd979bd
MF
1311
1312 if (!options) {
1313 status = 1;
1314 goto bail;
1315 }
1316
1317 while ((p = strsep(&options, ",")) != NULL) {
1318 int token, option;
1319 substring_t args[MAX_OPT_ARGS];
1320
1321 if (!*p)
1322 continue;
1323
1324 token = match_token(p, tokens, args);
1325 switch (token) {
1326 case Opt_hb_local:
c0123ade 1327 mopt->mount_opt |= OCFS2_MOUNT_HB_LOCAL;
ccd979bd
MF
1328 break;
1329 case Opt_hb_none:
c0123ade 1330 mopt->mount_opt &= ~OCFS2_MOUNT_HB_LOCAL;
ccd979bd
MF
1331 break;
1332 case Opt_barrier:
1333 if (match_int(&args[0], &option)) {
1334 status = 0;
1335 goto bail;
1336 }
1337 if (option)
c0123ade 1338 mopt->mount_opt |= OCFS2_MOUNT_BARRIER;
ccd979bd 1339 else
c0123ade 1340 mopt->mount_opt &= ~OCFS2_MOUNT_BARRIER;
ccd979bd
MF
1341 break;
1342 case Opt_intr:
c0123ade 1343 mopt->mount_opt &= ~OCFS2_MOUNT_NOINTR;
ccd979bd
MF
1344 break;
1345 case Opt_nointr:
c0123ade 1346 mopt->mount_opt |= OCFS2_MOUNT_NOINTR;
ccd979bd
MF
1347 break;
1348 case Opt_err_panic:
c0123ade 1349 mopt->mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
ccd979bd
MF
1350 break;
1351 case Opt_err_ro:
c0123ade 1352 mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_PANIC;
ccd979bd
MF
1353 break;
1354 case Opt_data_ordered:
c0123ade 1355 mopt->mount_opt &= ~OCFS2_MOUNT_DATA_WRITEBACK;
ccd979bd
MF
1356 break;
1357 case Opt_data_writeback:
c0123ade 1358 mopt->mount_opt |= OCFS2_MOUNT_DATA_WRITEBACK;
ccd979bd 1359 break;
cf1d6c76
TY
1360 case Opt_user_xattr:
1361 mopt->mount_opt &= ~OCFS2_MOUNT_NOUSERXATTR;
1362 break;
1363 case Opt_nouser_xattr:
1364 mopt->mount_opt |= OCFS2_MOUNT_NOUSERXATTR;
1365 break;
7f1a37e3
TY
1366 case Opt_atime_quantum:
1367 if (match_int(&args[0], &option)) {
1368 status = 0;
1369 goto bail;
1370 }
1371 if (option >= 0)
c0123ade 1372 mopt->atime_quantum = option;
7f1a37e3 1373 break;
baf4661a
SM
1374 case Opt_slot:
1375 option = 0;
1376 if (match_int(&args[0], &option)) {
1377 status = 0;
1378 goto bail;
1379 }
1380 if (option)
c0123ade 1381 mopt->slot = (s16)option;
baf4661a 1382 break;
d147b3d6
MF
1383 case Opt_commit:
1384 option = 0;
1385 if (match_int(&args[0], &option)) {
1386 status = 0;
1387 goto bail;
1388 }
1389 if (option < 0)
1390 return 0;
1391 if (option == 0)
2b4e30fb 1392 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
d147b3d6
MF
1393 mopt->commit_interval = HZ * option;
1394 break;
2fbe8d1e
SM
1395 case Opt_localalloc:
1396 option = 0;
1397 if (match_int(&args[0], &option)) {
1398 status = 0;
1399 goto bail;
1400 }
73c8a800 1401 if (option >= 0)
2fbe8d1e
SM
1402 mopt->localalloc_opt = option;
1403 break;
53fc622b
MF
1404 case Opt_localflocks:
1405 /*
1406 * Changing this during remount could race
1407 * flock() requests, or "unbalance" existing
1408 * ones (e.g., a lock is taken in one mode but
1409 * dropped in the other). If users care enough
1410 * to flip locking modes during remount, we
1411 * could add a "local" flag to individual
1412 * flock structures for proper tracking of
1413 * state.
1414 */
1415 if (!is_remount)
1416 mopt->mount_opt |= OCFS2_MOUNT_LOCALFLOCKS;
1417 break;
b61817e1
JB
1418 case Opt_stack:
1419 /* Check both that the option we were passed
1420 * is of the right length and that it is a proper
1421 * string of the right length.
1422 */
1423 if (((args[0].to - args[0].from) !=
1424 OCFS2_STACK_LABEL_LEN) ||
1425 (strnlen(args[0].from,
1426 OCFS2_STACK_LABEL_LEN) !=
1427 OCFS2_STACK_LABEL_LEN)) {
1428 mlog(ML_ERROR,
1429 "Invalid cluster_stack option\n");
1430 status = 0;
1431 goto bail;
1432 }
1433 memcpy(mopt->cluster_stack, args[0].from,
1434 OCFS2_STACK_LABEL_LEN);
1435 mopt->cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
1436 break;
12462f1d
JB
1437 case Opt_inode64:
1438 mopt->mount_opt |= OCFS2_MOUNT_INODE64;
1439 break;
19ece546 1440 case Opt_usrquota:
19ece546
JK
1441 mopt->mount_opt |= OCFS2_MOUNT_USRQUOTA;
1442 break;
1443 case Opt_grpquota:
19ece546
JK
1444 mopt->mount_opt |= OCFS2_MOUNT_GRPQUOTA;
1445 break;
a68979b8
TY
1446 case Opt_acl:
1447 mopt->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
5297aad8 1448 mopt->mount_opt &= ~OCFS2_MOUNT_NO_POSIX_ACL;
a68979b8
TY
1449 break;
1450 case Opt_noacl:
5297aad8 1451 mopt->mount_opt |= OCFS2_MOUNT_NO_POSIX_ACL;
a68979b8
TY
1452 mopt->mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
1453 break;
d02f00cc
MF
1454 case Opt_resv_level:
1455 if (is_remount)
1456 break;
1457 if (match_int(&args[0], &option)) {
1458 status = 0;
1459 goto bail;
1460 }
1461 if (option >= OCFS2_MIN_RESV_LEVEL &&
1462 option < OCFS2_MAX_RESV_LEVEL)
1463 mopt->resv_level = option;
1464 break;
83f92318
MF
1465 case Opt_dir_resv_level:
1466 if (is_remount)
1467 break;
1468 if (match_int(&args[0], &option)) {
1469 status = 0;
1470 goto bail;
1471 }
1472 if (option >= OCFS2_MIN_RESV_LEVEL &&
1473 option < OCFS2_MAX_RESV_LEVEL)
1474 mopt->dir_resv_level = option;
1475 break;
ccd979bd
MF
1476 default:
1477 mlog(ML_ERROR,
1478 "Unrecognized mount option \"%s\" "
1479 "or missing value\n", p);
1480 status = 0;
1481 goto bail;
1482 }
1483 }
1484
1485 status = 1;
1486
1487bail:
1488 mlog_exit(status);
1489 return status;
1490}
1491
d550071c
SM
1492static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
1493{
1494 struct ocfs2_super *osb = OCFS2_SB(mnt->mnt_sb);
1495 unsigned long opts = osb->s_mount_opt;
ebcee4b5 1496 unsigned int local_alloc_megs;
d550071c
SM
1497
1498 if (opts & OCFS2_MOUNT_HB_LOCAL)
1499 seq_printf(s, ",_netdev,heartbeat=local");
1500 else
1501 seq_printf(s, ",heartbeat=none");
1502
1503 if (opts & OCFS2_MOUNT_NOINTR)
1504 seq_printf(s, ",nointr");
1505
1506 if (opts & OCFS2_MOUNT_DATA_WRITEBACK)
1507 seq_printf(s, ",data=writeback");
1508 else
1509 seq_printf(s, ",data=ordered");
1510
1511 if (opts & OCFS2_MOUNT_BARRIER)
1512 seq_printf(s, ",barrier=1");
1513
1514 if (opts & OCFS2_MOUNT_ERRORS_PANIC)
1515 seq_printf(s, ",errors=panic");
1516 else
1517 seq_printf(s, ",errors=remount-ro");
1518
1519 if (osb->preferred_slot != OCFS2_INVALID_SLOT)
1520 seq_printf(s, ",preferred_slot=%d", osb->preferred_slot);
1521
1522 if (osb->s_atime_quantum != OCFS2_DEFAULT_ATIME_QUANTUM)
1523 seq_printf(s, ",atime_quantum=%u", osb->s_atime_quantum);
1524
d147b3d6
MF
1525 if (osb->osb_commit_interval)
1526 seq_printf(s, ",commit=%u",
1527 (unsigned) (osb->osb_commit_interval / HZ));
1528
ebcee4b5 1529 local_alloc_megs = osb->local_alloc_bits >> (20 - osb->s_clustersize_bits);
6b82021b 1530 if (local_alloc_megs != ocfs2_la_default_mb(osb))
ebcee4b5 1531 seq_printf(s, ",localalloc=%d", local_alloc_megs);
2fbe8d1e 1532
53fc622b
MF
1533 if (opts & OCFS2_MOUNT_LOCALFLOCKS)
1534 seq_printf(s, ",localflocks,");
1535
b61817e1
JB
1536 if (osb->osb_cluster_stack[0])
1537 seq_printf(s, ",cluster_stack=%.*s", OCFS2_STACK_LABEL_LEN,
1538 osb->osb_cluster_stack);
19ece546
JK
1539 if (opts & OCFS2_MOUNT_USRQUOTA)
1540 seq_printf(s, ",usrquota");
1541 if (opts & OCFS2_MOUNT_GRPQUOTA)
1542 seq_printf(s, ",grpquota");
b61817e1 1543
b0f73cfc
SM
1544 if (opts & OCFS2_MOUNT_NOUSERXATTR)
1545 seq_printf(s, ",nouser_xattr");
1546 else
1547 seq_printf(s, ",user_xattr");
1548
12462f1d
JB
1549 if (opts & OCFS2_MOUNT_INODE64)
1550 seq_printf(s, ",inode64");
1551
a68979b8
TY
1552 if (opts & OCFS2_MOUNT_POSIX_ACL)
1553 seq_printf(s, ",acl");
1554 else
1555 seq_printf(s, ",noacl");
a68979b8 1556
d02f00cc
MF
1557 if (osb->osb_resv_level != OCFS2_DEFAULT_RESV_LEVEL)
1558 seq_printf(s, ",resv_level=%d", osb->osb_resv_level);
1559
83f92318
MF
1560 if (osb->osb_dir_resv_level != osb->osb_resv_level)
1561 seq_printf(s, ",dir_resv_level=%d", osb->osb_resv_level);
1562
d550071c
SM
1563 return 0;
1564}
1565
ccd979bd
MF
1566static int __init ocfs2_init(void)
1567{
1568 int status;
1569
1570 mlog_entry_void();
1571
1572 ocfs2_print_version();
1573
ccd979bd
MF
1574 status = init_ocfs2_uptodate_cache();
1575 if (status < 0) {
1576 mlog_errno(status);
1577 goto leave;
1578 }
1579
1580 status = ocfs2_initialize_mem_caches();
1581 if (status < 0) {
1582 mlog_errno(status);
1583 goto leave;
1584 }
1585
1586 ocfs2_wq = create_singlethread_workqueue("ocfs2_wq");
1587 if (!ocfs2_wq) {
1588 status = -ENOMEM;
1589 goto leave;
1590 }
1591
ccd979bd
MF
1592 ocfs2_debugfs_root = debugfs_create_dir("ocfs2", NULL);
1593 if (!ocfs2_debugfs_root) {
1594 status = -EFAULT;
1595 mlog(ML_ERROR, "Unable to create ocfs2 debugfs root.\n");
1596 }
1597
171bf93c
MF
1598 status = ocfs2_quota_setup();
1599 if (status)
1600 goto leave;
1601
63e0c48a
JB
1602 ocfs2_set_locking_protocol();
1603
9e33d69f 1604 status = register_quota_format(&ocfs2_quota_format);
ccd979bd
MF
1605leave:
1606 if (status < 0) {
171bf93c 1607 ocfs2_quota_shutdown();
ccd979bd
MF
1608 ocfs2_free_mem_caches();
1609 exit_ocfs2_uptodate_cache();
ccd979bd
MF
1610 }
1611
1612 mlog_exit(status);
1613
1614 if (status >= 0) {
1615 return register_filesystem(&ocfs2_fs_type);
1616 } else
1617 return -1;
1618}
1619
1620static void __exit ocfs2_exit(void)
1621{
1622 mlog_entry_void();
1623
171bf93c
MF
1624 ocfs2_quota_shutdown();
1625
ccd979bd
MF
1626 if (ocfs2_wq) {
1627 flush_workqueue(ocfs2_wq);
1628 destroy_workqueue(ocfs2_wq);
1629 }
1630
9e33d69f
JK
1631 unregister_quota_format(&ocfs2_quota_format);
1632
ccd979bd
MF
1633 debugfs_remove(ocfs2_debugfs_root);
1634
1635 ocfs2_free_mem_caches();
1636
1637 unregister_filesystem(&ocfs2_fs_type);
1638
ccd979bd
MF
1639 exit_ocfs2_uptodate_cache();
1640
1641 mlog_exit_void();
1642}
1643
1644static void ocfs2_put_super(struct super_block *sb)
1645{
1646 mlog_entry("(0x%p)\n", sb);
1647
6cfd0148
CH
1648 lock_kernel();
1649
ccd979bd
MF
1650 ocfs2_sync_blockdev(sb);
1651 ocfs2_dismount_volume(sb, 0);
1652
6cfd0148
CH
1653 unlock_kernel();
1654
ccd979bd
MF
1655 mlog_exit_void();
1656}
1657
726c3342 1658static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
ccd979bd
MF
1659{
1660 struct ocfs2_super *osb;
1661 u32 numbits, freebits;
1662 int status;
1663 struct ocfs2_dinode *bm_lock;
1664 struct buffer_head *bh = NULL;
1665 struct inode *inode = NULL;
1666
726c3342 1667 mlog_entry("(%p, %p)\n", dentry->d_sb, buf);
ccd979bd 1668
726c3342 1669 osb = OCFS2_SB(dentry->d_sb);
ccd979bd
MF
1670
1671 inode = ocfs2_get_system_file_inode(osb,
1672 GLOBAL_BITMAP_SYSTEM_INODE,
1673 OCFS2_INVALID_SLOT);
1674 if (!inode) {
1675 mlog(ML_ERROR, "failed to get bitmap inode\n");
1676 status = -EIO;
1677 goto bail;
1678 }
1679
e63aecb6 1680 status = ocfs2_inode_lock(inode, &bh, 0);
ccd979bd
MF
1681 if (status < 0) {
1682 mlog_errno(status);
1683 goto bail;
1684 }
1685
1686 bm_lock = (struct ocfs2_dinode *) bh->b_data;
1687
1688 numbits = le32_to_cpu(bm_lock->id1.bitmap1.i_total);
1689 freebits = numbits - le32_to_cpu(bm_lock->id1.bitmap1.i_used);
1690
1691 buf->f_type = OCFS2_SUPER_MAGIC;
726c3342 1692 buf->f_bsize = dentry->d_sb->s_blocksize;
ccd979bd
MF
1693 buf->f_namelen = OCFS2_MAX_FILENAME_LEN;
1694 buf->f_blocks = ((sector_t) numbits) *
1695 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1696 buf->f_bfree = ((sector_t) freebits) *
1697 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1698 buf->f_bavail = buf->f_bfree;
1699 buf->f_files = numbits;
1700 buf->f_ffree = freebits;
837711f8
CL
1701 buf->f_fsid.val[0] = crc32_le(0, osb->uuid_str, OCFS2_VOL_UUID_LEN)
1702 & 0xFFFFFFFFUL;
1703 buf->f_fsid.val[1] = crc32_le(0, osb->uuid_str + OCFS2_VOL_UUID_LEN,
1704 OCFS2_VOL_UUID_LEN) & 0xFFFFFFFFUL;
ccd979bd
MF
1705
1706 brelse(bh);
1707
e63aecb6 1708 ocfs2_inode_unlock(inode, 0);
ccd979bd
MF
1709 status = 0;
1710bail:
1711 if (inode)
1712 iput(inode);
1713
1714 mlog_exit(status);
1715
1716 return status;
1717}
1718
51cc5068 1719static void ocfs2_inode_init_once(void *data)
ccd979bd
MF
1720{
1721 struct ocfs2_inode_info *oi = data;
1722
a35afb83
CL
1723 oi->ip_flags = 0;
1724 oi->ip_open_count = 0;
1725 spin_lock_init(&oi->ip_lock);
1726 ocfs2_extent_map_init(&oi->vfs_inode);
1727 INIT_LIST_HEAD(&oi->ip_io_markers);
a35afb83 1728 oi->ip_dir_start_lookup = 0;
ccd979bd 1729
a35afb83 1730 init_rwsem(&oi->ip_alloc_sem);
cf1d6c76 1731 init_rwsem(&oi->ip_xattr_sem);
a35afb83 1732 mutex_init(&oi->ip_io_mutex);
ccd979bd 1733
a35afb83
CL
1734 oi->ip_blkno = 0ULL;
1735 oi->ip_clusters = 0;
ccd979bd 1736
4fe370af
MF
1737 ocfs2_resv_init_once(&oi->ip_la_data_resv);
1738
a35afb83 1739 ocfs2_lock_res_init_once(&oi->ip_rw_lockres);
e63aecb6 1740 ocfs2_lock_res_init_once(&oi->ip_inode_lockres);
a35afb83 1741 ocfs2_lock_res_init_once(&oi->ip_open_lockres);
ccd979bd 1742
8cb471e8 1743 ocfs2_metadata_cache_init(INODE_CACHE(&oi->vfs_inode),
6e5a3d75 1744 &ocfs2_inode_caching_ops);
ccd979bd 1745
a35afb83 1746 inode_init_once(&oi->vfs_inode);
ccd979bd
MF
1747}
1748
1749static int ocfs2_initialize_mem_caches(void)
1750{
1751 ocfs2_inode_cachep = kmem_cache_create("ocfs2_inode_cache",
fffb60f9
PJ
1752 sizeof(struct ocfs2_inode_info),
1753 0,
1754 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1755 SLAB_MEM_SPREAD),
20c2df83 1756 ocfs2_inode_init_once);
9e33d69f
JK
1757 ocfs2_dquot_cachep = kmem_cache_create("ocfs2_dquot_cache",
1758 sizeof(struct ocfs2_dquot),
1759 0,
1760 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1761 SLAB_MEM_SPREAD),
1762 NULL);
1763 ocfs2_qf_chunk_cachep = kmem_cache_create("ocfs2_qf_chunk_cache",
1764 sizeof(struct ocfs2_quota_chunk),
1765 0,
1766 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
1767 NULL);
1768 if (!ocfs2_inode_cachep || !ocfs2_dquot_cachep ||
1769 !ocfs2_qf_chunk_cachep) {
1770 if (ocfs2_inode_cachep)
1771 kmem_cache_destroy(ocfs2_inode_cachep);
1772 if (ocfs2_dquot_cachep)
1773 kmem_cache_destroy(ocfs2_dquot_cachep);
1774 if (ocfs2_qf_chunk_cachep)
1775 kmem_cache_destroy(ocfs2_qf_chunk_cachep);
ccd979bd 1776 return -ENOMEM;
9e33d69f 1777 }
ccd979bd 1778
ccd979bd
MF
1779 return 0;
1780}
1781
1782static void ocfs2_free_mem_caches(void)
1783{
1784 if (ocfs2_inode_cachep)
1785 kmem_cache_destroy(ocfs2_inode_cachep);
ccd979bd 1786 ocfs2_inode_cachep = NULL;
9e33d69f
JK
1787
1788 if (ocfs2_dquot_cachep)
1789 kmem_cache_destroy(ocfs2_dquot_cachep);
1790 ocfs2_dquot_cachep = NULL;
1791
1792 if (ocfs2_qf_chunk_cachep)
1793 kmem_cache_destroy(ocfs2_qf_chunk_cachep);
1794 ocfs2_qf_chunk_cachep = NULL;
ccd979bd
MF
1795}
1796
1797static int ocfs2_get_sector(struct super_block *sb,
1798 struct buffer_head **bh,
1799 int block,
1800 int sect_size)
1801{
1802 if (!sb_set_blocksize(sb, sect_size)) {
1803 mlog(ML_ERROR, "unable to set blocksize\n");
1804 return -EIO;
1805 }
1806
1807 *bh = sb_getblk(sb, block);
1808 if (!*bh) {
1809 mlog_errno(-EIO);
1810 return -EIO;
1811 }
1812 lock_buffer(*bh);
1813 if (!buffer_dirty(*bh))
1814 clear_buffer_uptodate(*bh);
1815 unlock_buffer(*bh);
1816 ll_rw_block(READ, 1, bh);
1817 wait_on_buffer(*bh);
28d57d43 1818 if (!buffer_uptodate(*bh)) {
1819 mlog_errno(-EIO);
1820 brelse(*bh);
1821 *bh = NULL;
1822 return -EIO;
1823 }
1824
ccd979bd
MF
1825 return 0;
1826}
1827
ccd979bd
MF
1828static int ocfs2_mount_volume(struct super_block *sb)
1829{
1830 int status = 0;
1831 int unlock_super = 0;
1832 struct ocfs2_super *osb = OCFS2_SB(sb);
1833
1834 mlog_entry_void();
1835
1836 if (ocfs2_is_hard_readonly(osb))
1837 goto leave;
1838
ccd979bd
MF
1839 status = ocfs2_dlm_init(osb);
1840 if (status < 0) {
1841 mlog_errno(status);
1842 goto leave;
1843 }
1844
ccd979bd
MF
1845 status = ocfs2_super_lock(osb, 1);
1846 if (status < 0) {
1847 mlog_errno(status);
1848 goto leave;
1849 }
1850 unlock_super = 1;
1851
1852 /* This will load up the node map and add ourselves to it. */
1853 status = ocfs2_find_slot(osb);
1854 if (status < 0) {
1855 mlog_errno(status);
1856 goto leave;
1857 }
1858
ccd979bd
MF
1859 /* load all node-local system inodes */
1860 status = ocfs2_init_local_system_inodes(osb);
1861 if (status < 0) {
1862 mlog_errno(status);
1863 goto leave;
1864 }
1865
1866 status = ocfs2_check_volume(osb);
1867 if (status < 0) {
1868 mlog_errno(status);
1869 goto leave;
1870 }
1871
1872 status = ocfs2_truncate_log_init(osb);
06c59bb8 1873 if (status < 0)
ccd979bd 1874 mlog_errno(status);
c271c5c2 1875
ccd979bd
MF
1876leave:
1877 if (unlock_super)
1878 ocfs2_super_unlock(osb, 1);
1879
1880 mlog_exit(status);
1881 return status;
1882}
1883
ccd979bd
MF
1884static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
1885{
286eaa95 1886 int tmp, hangup_needed = 0;
ccd979bd 1887 struct ocfs2_super *osb = NULL;
c271c5c2 1888 char nodestr[8];
ccd979bd
MF
1889
1890 mlog_entry("(0x%p)\n", sb);
1891
1892 BUG_ON(!sb);
1893 osb = OCFS2_SB(sb);
1894 BUG_ON(!osb);
1895
50397507
SM
1896 debugfs_remove(osb->osb_ctxt);
1897
f7b1aa69
JK
1898 /*
1899 * Flush inode dropping work queue so that deletes are
1900 * performed while the filesystem is still working
1901 */
1902 ocfs2_drop_all_dl_inodes(osb);
1903
692684e1
SM
1904 /* Orphan scan should be stopped as early as possible */
1905 ocfs2_orphan_scan_stop(osb);
1906
19ece546
JK
1907 ocfs2_disable_quotas(osb);
1908
ccd979bd
MF
1909 ocfs2_shutdown_local_alloc(osb);
1910
1911 ocfs2_truncate_log_shutdown(osb);
1912
553abd04
JB
1913 /* This will disable recovery and flush any recovery work. */
1914 ocfs2_recovery_exit(osb);
ccd979bd
MF
1915
1916 ocfs2_journal_shutdown(osb);
1917
1918 ocfs2_sync_blockdev(sb);
1919
374a263e
TM
1920 ocfs2_purge_refcount_trees(osb);
1921
4670c46d
JB
1922 /* No cluster connection means we've failed during mount, so skip
1923 * all the steps which depended on that to complete. */
1924 if (osb->cconn) {
ccd979bd
MF
1925 tmp = ocfs2_super_lock(osb, 1);
1926 if (tmp < 0) {
1927 mlog_errno(tmp);
1928 return;
1929 }
19b613d4 1930 }
ccd979bd 1931
19b613d4
MF
1932 if (osb->slot_num != OCFS2_INVALID_SLOT)
1933 ocfs2_put_slot(osb);
ccd979bd 1934
4670c46d 1935 if (osb->cconn)
ccd979bd 1936 ocfs2_super_unlock(osb, 1);
ccd979bd
MF
1937
1938 ocfs2_release_system_inodes(osb);
1939
6953b4c0 1940 /*
6953b4c0
JB
1941 * If we're dismounting due to mount error, mount.ocfs2 will clean
1942 * up heartbeat. If we're a local mount, there is no heartbeat.
1943 * If we failed before we got a uuid_str yet, we can't stop
1944 * heartbeat. Otherwise, do it.
1945 */
1946 if (!mnt_err && !ocfs2_mount_local(osb) && osb->uuid_str)
286eaa95
JB
1947 hangup_needed = 1;
1948
1949 if (osb->cconn)
1950 ocfs2_dlm_shutdown(osb, hangup_needed);
1951
73be192b 1952 ocfs2_blockcheck_stats_debugfs_remove(&osb->osb_ecc_stats);
286eaa95
JB
1953 debugfs_remove(osb->osb_debug_root);
1954
1955 if (hangup_needed)
6953b4c0 1956 ocfs2_cluster_hangup(osb->uuid_str, strlen(osb->uuid_str));
ccd979bd
MF
1957
1958 atomic_set(&osb->vol_state, VOLUME_DISMOUNTED);
1959
c271c5c2
SM
1960 if (ocfs2_mount_local(osb))
1961 snprintf(nodestr, sizeof(nodestr), "local");
1962 else
19fdb624 1963 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
c271c5c2
SM
1964
1965 printk(KERN_INFO "ocfs2: Unmounting device (%s) on (node %s)\n",
1966 osb->dev_str, nodestr);
ccd979bd
MF
1967
1968 ocfs2_delete_osb(osb);
1969 kfree(osb);
1970 sb->s_dev = 0;
1971 sb->s_fs_info = NULL;
1972}
1973
1974static int ocfs2_setup_osb_uuid(struct ocfs2_super *osb, const unsigned char *uuid,
1975 unsigned uuid_bytes)
1976{
1977 int i, ret;
1978 char *ptr;
1979
1980 BUG_ON(uuid_bytes != OCFS2_VOL_UUID_LEN);
1981
cd861280 1982 osb->uuid_str = kzalloc(OCFS2_VOL_UUID_LEN * 2 + 1, GFP_KERNEL);
ccd979bd
MF
1983 if (osb->uuid_str == NULL)
1984 return -ENOMEM;
1985
ccd979bd
MF
1986 for (i = 0, ptr = osb->uuid_str; i < OCFS2_VOL_UUID_LEN; i++) {
1987 /* print with null */
1988 ret = snprintf(ptr, 3, "%02X", uuid[i]);
1989 if (ret != 2) /* drop super cleans up */
1990 return -EINVAL;
1991 /* then only advance past the last char */
1992 ptr += 2;
1993 }
1994
1995 return 0;
1996}
1997
1998static int ocfs2_initialize_super(struct super_block *sb,
1999 struct buffer_head *bh,
73be192b
JB
2000 int sector_size,
2001 struct ocfs2_blockcheck_stats *stats)
ccd979bd 2002{
bddb8eb3 2003 int status;
5a254031
MF
2004 int i, cbits, bbits;
2005 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
ccd979bd 2006 struct inode *inode = NULL;
ccd979bd
MF
2007 struct ocfs2_journal *journal;
2008 __le32 uuid_net_key;
2009 struct ocfs2_super *osb;
2010
2011 mlog_entry_void();
2012
cd861280 2013 osb = kzalloc(sizeof(struct ocfs2_super), GFP_KERNEL);
ccd979bd
MF
2014 if (!osb) {
2015 status = -ENOMEM;
2016 mlog_errno(status);
2017 goto bail;
2018 }
2019
2020 sb->s_fs_info = osb;
2021 sb->s_op = &ocfs2_sops;
2022 sb->s_export_op = &ocfs2_export_ops;
19ece546
JK
2023 sb->s_qcop = &ocfs2_quotactl_ops;
2024 sb->dq_op = &ocfs2_quota_operations;
cf1d6c76 2025 sb->s_xattr = ocfs2_xattr_handlers;
e0dceaf0 2026 sb->s_time_gran = 1;
ccd979bd
MF
2027 sb->s_flags |= MS_NOATIME;
2028 /* this is needed to support O_LARGEFILE */
5a254031
MF
2029 cbits = le32_to_cpu(di->id2.i_super.s_clustersize_bits);
2030 bbits = le32_to_cpu(di->id2.i_super.s_blocksize_bits);
2031 sb->s_maxbytes = ocfs2_max_file_offset(bbits, cbits);
ccd979bd 2032
9b7895ef
MF
2033 osb->osb_dx_mask = (1 << (cbits - bbits)) - 1;
2034
2035 for (i = 0; i < 3; i++)
2036 osb->osb_dx_seed[i] = le32_to_cpu(di->id2.i_super.s_dx_seed[i]);
2037 osb->osb_dx_seed[3] = le32_to_cpu(di->id2.i_super.s_uuid_hash);
2038
ccd979bd
MF
2039 osb->sb = sb;
2040 /* Save off for ocfs2_rw_direct */
2041 osb->s_sectsize_bits = blksize_bits(sector_size);
ebdec83b 2042 BUG_ON(!osb->s_sectsize_bits);
ccd979bd 2043
34d024f8
MF
2044 spin_lock_init(&osb->dc_task_lock);
2045 init_waitqueue_head(&osb->dc_event);
2046 osb->dc_work_sequence = 0;
2047 osb->dc_wake_sequence = 0;
ccd979bd
MF
2048 INIT_LIST_HEAD(&osb->blocked_lock_list);
2049 osb->blocked_lock_count = 0;
ccd979bd 2050 spin_lock_init(&osb->osb_lock);
c8b9cf9a 2051 spin_lock_init(&osb->osb_xattr_lock);
b89c5428 2052 ocfs2_init_steal_slots(osb);
ccd979bd
MF
2053
2054 atomic_set(&osb->alloc_stats.moves, 0);
2055 atomic_set(&osb->alloc_stats.local_data, 0);
2056 atomic_set(&osb->alloc_stats.bitmap_data, 0);
2057 atomic_set(&osb->alloc_stats.bg_allocs, 0);
2058 atomic_set(&osb->alloc_stats.bg_extends, 0);
2059
73be192b
JB
2060 /* Copy the blockcheck stats from the superblock probe */
2061 osb->osb_ecc_stats = *stats;
2062
ccd979bd
MF
2063 ocfs2_init_node_maps(osb);
2064
2065 snprintf(osb->dev_str, sizeof(osb->dev_str), "%u,%u",
2066 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
2067
8b712cd5
JM
2068 ocfs2_orphan_scan_init(osb);
2069
553abd04
JB
2070 status = ocfs2_recovery_init(osb);
2071 if (status) {
2072 mlog(ML_ERROR, "Unable to initialize recovery state\n");
2073 mlog_errno(status);
2074 goto bail;
2075 }
ccd979bd
MF
2076
2077 init_waitqueue_head(&osb->checkpoint_event);
2078 atomic_set(&osb->needs_checkpoint, 0);
2079
7f1a37e3
TY
2080 osb->s_atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
2081
ccd979bd
MF
2082 osb->slot_num = OCFS2_INVALID_SLOT;
2083
8154da3d
TY
2084 osb->s_xattr_inline_size = le16_to_cpu(
2085 di->id2.i_super.s_xattr_inline_size);
fdd77704 2086
ccd979bd
MF
2087 osb->local_alloc_state = OCFS2_LA_UNUSED;
2088 osb->local_alloc_bh = NULL;
9c7af40b 2089 INIT_DELAYED_WORK(&osb->la_enable_wq, ocfs2_la_enable_worker);
ccd979bd 2090
ccd979bd
MF
2091 init_waitqueue_head(&osb->osb_mount_event);
2092
d02f00cc
MF
2093 status = ocfs2_resmap_init(osb, &osb->osb_la_resmap);
2094 if (status) {
2095 mlog_errno(status);
2096 goto bail;
2097 }
2098
ccd979bd
MF
2099 osb->vol_label = kmalloc(OCFS2_MAX_VOL_LABEL_LEN, GFP_KERNEL);
2100 if (!osb->vol_label) {
2101 mlog(ML_ERROR, "unable to alloc vol label\n");
2102 status = -ENOMEM;
2103 goto bail;
2104 }
2105
ccd979bd
MF
2106 osb->max_slots = le16_to_cpu(di->id2.i_super.s_max_slots);
2107 if (osb->max_slots > OCFS2_MAX_SLOTS || osb->max_slots == 0) {
2108 mlog(ML_ERROR, "Invalid number of node slots (%u)\n",
2109 osb->max_slots);
2110 status = -EINVAL;
2111 goto bail;
2112 }
781ee3e2 2113 mlog(0, "max_slots for this device: %u\n", osb->max_slots);
ccd979bd 2114
539d8264
SM
2115 osb->slot_recovery_generations =
2116 kcalloc(osb->max_slots, sizeof(*osb->slot_recovery_generations),
2117 GFP_KERNEL);
2118 if (!osb->slot_recovery_generations) {
2119 status = -ENOMEM;
2120 mlog_errno(status);
2121 goto bail;
2122 }
2123
b4df6ed8
MF
2124 init_waitqueue_head(&osb->osb_wipe_event);
2125 osb->osb_orphan_wipes = kcalloc(osb->max_slots,
2126 sizeof(*osb->osb_orphan_wipes),
2127 GFP_KERNEL);
2128 if (!osb->osb_orphan_wipes) {
2129 status = -ENOMEM;
2130 mlog_errno(status);
2131 goto bail;
2132 }
2133
374a263e
TM
2134 osb->osb_rf_lock_tree = RB_ROOT;
2135
ccd979bd
MF
2136 osb->s_feature_compat =
2137 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_compat);
2138 osb->s_feature_ro_compat =
2139 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_ro_compat);
2140 osb->s_feature_incompat =
2141 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_incompat);
2142
2143 if ((i = OCFS2_HAS_INCOMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_INCOMPAT_SUPP))) {
2144 mlog(ML_ERROR, "couldn't mount because of unsupported "
2145 "optional features (%x).\n", i);
2146 status = -EINVAL;
2147 goto bail;
2148 }
2149 if (!(osb->sb->s_flags & MS_RDONLY) &&
2150 (i = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP))) {
2151 mlog(ML_ERROR, "couldn't mount RDWR because of "
2152 "unsupported optional features (%x).\n", i);
2153 status = -EINVAL;
2154 goto bail;
2155 }
2156
b61817e1
JB
2157 if (ocfs2_userspace_stack(osb)) {
2158 memcpy(osb->osb_cluster_stack,
2159 OCFS2_RAW_SB(di)->s_cluster_info.ci_stack,
2160 OCFS2_STACK_LABEL_LEN);
2161 osb->osb_cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
2162 if (strlen(osb->osb_cluster_stack) != OCFS2_STACK_LABEL_LEN) {
2163 mlog(ML_ERROR,
2164 "couldn't mount because of an invalid "
2165 "cluster stack label (%s) \n",
2166 osb->osb_cluster_stack);
2167 status = -EINVAL;
2168 goto bail;
2169 }
2170 } else {
2171 /* The empty string is identical with classic tools that
2172 * don't know about s_cluster_info. */
2173 osb->osb_cluster_stack[0] = '\0';
2174 }
2175
ccd979bd
MF
2176 get_random_bytes(&osb->s_next_generation, sizeof(u32));
2177
2178 /* FIXME
2179 * This should be done in ocfs2_journal_init(), but unknown
2180 * ordering issues will cause the filesystem to crash.
2181 * If anyone wants to figure out what part of the code
2182 * refers to osb->journal before ocfs2_journal_init() is run,
2183 * be my guest.
2184 */
2185 /* initialize our journal structure */
2186
cd861280 2187 journal = kzalloc(sizeof(struct ocfs2_journal), GFP_KERNEL);
ccd979bd
MF
2188 if (!journal) {
2189 mlog(ML_ERROR, "unable to alloc journal\n");
2190 status = -ENOMEM;
2191 goto bail;
2192 }
2193 osb->journal = journal;
2194 journal->j_osb = osb;
2195
2196 atomic_set(&journal->j_num_trans, 0);
2197 init_rwsem(&journal->j_trans_barrier);
2198 init_waitqueue_head(&journal->j_checkpointed);
2199 spin_lock_init(&journal->j_lock);
2200 journal->j_trans_id = (unsigned long) 1;
2201 INIT_LIST_HEAD(&journal->j_la_cleanups);
c4028958 2202 INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
ccd979bd
MF
2203 journal->j_state = OCFS2_JOURNAL_FREE;
2204
ea455f8a
JK
2205 INIT_WORK(&osb->dentry_lock_work, ocfs2_drop_dl_inodes);
2206 osb->dentry_lock_list = NULL;
2207
ccd979bd
MF
2208 /* get some pseudo constants for clustersize bits */
2209 osb->s_clustersize_bits =
2210 le32_to_cpu(di->id2.i_super.s_clustersize_bits);
2211 osb->s_clustersize = 1 << osb->s_clustersize_bits;
2212 mlog(0, "clusterbits=%d\n", osb->s_clustersize_bits);
2213
2214 if (osb->s_clustersize < OCFS2_MIN_CLUSTERSIZE ||
2215 osb->s_clustersize > OCFS2_MAX_CLUSTERSIZE) {
2216 mlog(ML_ERROR, "Volume has invalid cluster size (%d)\n",
2217 osb->s_clustersize);
2218 status = -EINVAL;
2219 goto bail;
2220 }
2221
2222 if (ocfs2_clusters_to_blocks(osb->sb, le32_to_cpu(di->i_clusters) - 1)
2223 > (u32)~0UL) {
2224 mlog(ML_ERROR, "Volume might try to write to blocks beyond "
2225 "what jbd can address in 32 bits.\n");
2226 status = -EINVAL;
2227 goto bail;
2228 }
2229
2230 if (ocfs2_setup_osb_uuid(osb, di->id2.i_super.s_uuid,
2231 sizeof(di->id2.i_super.s_uuid))) {
2232 mlog(ML_ERROR, "Out of memory trying to setup our uuid.\n");
2233 status = -ENOMEM;
2234 goto bail;
2235 }
2236
78427043 2237 memcpy(&uuid_net_key, di->id2.i_super.s_uuid, sizeof(uuid_net_key));
ccd979bd
MF
2238
2239 strncpy(osb->vol_label, di->id2.i_super.s_label, 63);
2240 osb->vol_label[63] = '\0';
2241 osb->root_blkno = le64_to_cpu(di->id2.i_super.s_root_blkno);
2242 osb->system_dir_blkno = le64_to_cpu(di->id2.i_super.s_system_dir_blkno);
2243 osb->first_cluster_group_blkno =
2244 le64_to_cpu(di->id2.i_super.s_first_cluster_group);
2245 osb->fs_generation = le32_to_cpu(di->i_fs_generation);
cf1d6c76 2246 osb->uuid_hash = le32_to_cpu(di->id2.i_super.s_uuid_hash);
ccd979bd
MF
2247 mlog(0, "vol_label: %s\n", osb->vol_label);
2248 mlog(0, "uuid: %s\n", osb->uuid_str);
b0697053
MF
2249 mlog(0, "root_blkno=%llu, system_dir_blkno=%llu\n",
2250 (unsigned long long)osb->root_blkno,
2251 (unsigned long long)osb->system_dir_blkno);
ccd979bd
MF
2252
2253 osb->osb_dlm_debug = ocfs2_new_dlm_debug();
2254 if (!osb->osb_dlm_debug) {
2255 status = -ENOMEM;
2256 mlog_errno(status);
2257 goto bail;
2258 }
2259
2260 atomic_set(&osb->vol_state, VOLUME_INIT);
2261
2262 /* load root, system_dir, and all global system inodes */
2263 status = ocfs2_init_global_system_inodes(osb);
2264 if (status < 0) {
2265 mlog_errno(status);
2266 goto bail;
2267 }
2268
2269 /*
2270 * global bitmap
2271 */
2272 inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
2273 OCFS2_INVALID_SLOT);
2274 if (!inode) {
2275 status = -EINVAL;
2276 mlog_errno(status);
2277 goto bail;
2278 }
2279
2280 osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
6b82021b 2281 osb->osb_clusters_at_boot = OCFS2_I(inode)->ip_clusters;
ccd979bd 2282 iput(inode);
ccd979bd 2283
8571882c
TM
2284 osb->bitmap_cpg = ocfs2_group_bitmap_size(sb, 0,
2285 osb->s_feature_incompat) * 8;
ccd979bd
MF
2286
2287 status = ocfs2_init_slot_info(osb);
2288 if (status < 0) {
2289 mlog_errno(status);
2290 goto bail;
2291 }
2292
ccd979bd
MF
2293bail:
2294 mlog_exit(status);
2295 return status;
2296}
2297
2298/*
2299 * will return: -EAGAIN if it is ok to keep searching for superblocks
2300 * -EINVAL if there is a bad superblock
2301 * 0 on success
2302 */
2303static int ocfs2_verify_volume(struct ocfs2_dinode *di,
2304 struct buffer_head *bh,
73be192b
JB
2305 u32 blksz,
2306 struct ocfs2_blockcheck_stats *stats)
ccd979bd
MF
2307{
2308 int status = -EAGAIN;
2309
2310 mlog_entry_void();
2311
2312 if (memcmp(di->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
2313 strlen(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) {
d030cc97
JB
2314 /* We have to do a raw check of the feature here */
2315 if (le32_to_cpu(di->id2.i_super.s_feature_incompat) &
2316 OCFS2_FEATURE_INCOMPAT_META_ECC) {
2317 status = ocfs2_block_check_validate(bh->b_data,
2318 bh->b_size,
73be192b
JB
2319 &di->i_check,
2320 stats);
d030cc97
JB
2321 if (status)
2322 goto out;
2323 }
ccd979bd
MF
2324 status = -EINVAL;
2325 if ((1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits)) != blksz) {
2326 mlog(ML_ERROR, "found superblock with incorrect block "
2327 "size: found %u, should be %u\n",
2328 1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits),
2329 blksz);
2330 } else if (le16_to_cpu(di->id2.i_super.s_major_rev_level) !=
2331 OCFS2_MAJOR_REV_LEVEL ||
2332 le16_to_cpu(di->id2.i_super.s_minor_rev_level) !=
2333 OCFS2_MINOR_REV_LEVEL) {
2334 mlog(ML_ERROR, "found superblock with bad version: "
2335 "found %u.%u, should be %u.%u\n",
2336 le16_to_cpu(di->id2.i_super.s_major_rev_level),
2337 le16_to_cpu(di->id2.i_super.s_minor_rev_level),
2338 OCFS2_MAJOR_REV_LEVEL,
2339 OCFS2_MINOR_REV_LEVEL);
2340 } else if (bh->b_blocknr != le64_to_cpu(di->i_blkno)) {
2341 mlog(ML_ERROR, "bad block number on superblock: "
b0697053 2342 "found %llu, should be %llu\n",
1ca1a111 2343 (unsigned long long)le64_to_cpu(di->i_blkno),
b0697053 2344 (unsigned long long)bh->b_blocknr);
ccd979bd
MF
2345 } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
2346 le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
2347 mlog(ML_ERROR, "bad cluster size found: %u\n",
2348 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
2349 } else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
2350 mlog(ML_ERROR, "bad root_blkno: 0\n");
2351 } else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
2352 mlog(ML_ERROR, "bad system_dir_blkno: 0\n");
2353 } else if (le16_to_cpu(di->id2.i_super.s_max_slots) > OCFS2_MAX_SLOTS) {
2354 mlog(ML_ERROR,
2355 "Superblock slots found greater than file system "
2356 "maximum: found %u, max %u\n",
2357 le16_to_cpu(di->id2.i_super.s_max_slots),
2358 OCFS2_MAX_SLOTS);
2359 } else {
2360 /* found it! */
2361 status = 0;
2362 }
2363 }
2364
d030cc97 2365out:
ccd979bd
MF
2366 mlog_exit(status);
2367 return status;
2368}
2369
2370static int ocfs2_check_volume(struct ocfs2_super *osb)
2371{
bddb8eb3 2372 int status;
ccd979bd 2373 int dirty;
c271c5c2 2374 int local;
ccd979bd
MF
2375 struct ocfs2_dinode *local_alloc = NULL; /* only used if we
2376 * recover
2377 * ourselves. */
2378
2379 mlog_entry_void();
2380
2381 /* Init our journal object. */
2382 status = ocfs2_journal_init(osb->journal, &dirty);
2383 if (status < 0) {
2384 mlog(ML_ERROR, "Could not initialize journal!\n");
2385 goto finally;
2386 }
2387
2388 /* If the journal was unmounted cleanly then we don't want to
2389 * recover anything. Otherwise, journal_load will do that
2390 * dirty work for us :) */
2391 if (!dirty) {
2392 status = ocfs2_journal_wipe(osb->journal, 0);
2393 if (status < 0) {
2394 mlog_errno(status);
2395 goto finally;
2396 }
2397 } else {
2398 mlog(ML_NOTICE, "File system was not unmounted cleanly, "
2399 "recovering volume.\n");
2400 }
2401
c271c5c2
SM
2402 local = ocfs2_mount_local(osb);
2403
ccd979bd 2404 /* will play back anything left in the journal. */
539d8264 2405 status = ocfs2_journal_load(osb->journal, local, dirty);
01af4820
WW
2406 if (status < 0) {
2407 mlog(ML_ERROR, "ocfs2 journal load failed! %d\n", status);
2408 goto finally;
2409 }
ccd979bd
MF
2410
2411 if (dirty) {
2412 /* recover my local alloc if we didn't unmount cleanly. */
2413 status = ocfs2_begin_local_alloc_recovery(osb,
2414 osb->slot_num,
2415 &local_alloc);
2416 if (status < 0) {
2417 mlog_errno(status);
2418 goto finally;
2419 }
2420 /* we complete the recovery process after we've marked
2421 * ourselves as mounted. */
2422 }
2423
2424 mlog(0, "Journal loaded.\n");
2425
2426 status = ocfs2_load_local_alloc(osb);
2427 if (status < 0) {
2428 mlog_errno(status);
2429 goto finally;
2430 }
2431
2432 if (dirty) {
2433 /* Recovery will be completed after we've mounted the
2434 * rest of the volume. */
2435 osb->dirty = 1;
2436 osb->local_alloc_copy = local_alloc;
2437 local_alloc = NULL;
2438 }
2439
2440 /* go through each journal, trylock it and if you get the
2441 * lock, and it's marked as dirty, set the bit in the recover
2442 * map and launch a recovery thread for it. */
2443 status = ocfs2_mark_dead_nodes(osb);
9140db04
SE
2444 if (status < 0) {
2445 mlog_errno(status);
2446 goto finally;
2447 }
2448
2449 status = ocfs2_compute_replay_slots(osb);
ccd979bd
MF
2450 if (status < 0)
2451 mlog_errno(status);
2452
2453finally:
2454 if (local_alloc)
2455 kfree(local_alloc);
2456
2457 mlog_exit(status);
2458 return status;
2459}
2460
2461/*
2462 * The routine gets called from dismount or close whenever a dismount on
2463 * volume is requested and the osb open count becomes 1.
2464 * It will remove the osb from the global list and also free up all the
2465 * initialized resources and fileobject.
2466 */
2467static void ocfs2_delete_osb(struct ocfs2_super *osb)
2468{
2469 mlog_entry_void();
2470
2471 /* This function assumes that the caller has the main osb resource */
2472
8e8a4603 2473 ocfs2_free_slot_info(osb);
ccd979bd 2474
b4df6ed8 2475 kfree(osb->osb_orphan_wipes);
539d8264 2476 kfree(osb->slot_recovery_generations);
ccd979bd
MF
2477 /* FIXME
2478 * This belongs in journal shutdown, but because we have to
421f91d2 2479 * allocate osb->journal at the start of ocfs2_initialize_osb(),
ccd979bd
MF
2480 * we free it here.
2481 */
2482 kfree(osb->journal);
2483 if (osb->local_alloc_copy)
2484 kfree(osb->local_alloc_copy);
2485 kfree(osb->uuid_str);
2486 ocfs2_put_dlm_debug(osb->osb_dlm_debug);
2487 memset(osb, 0, sizeof(struct ocfs2_super));
2488
2489 mlog_exit_void();
2490}
2491
2492/* Put OCFS2 into a readonly state, or (if the user specifies it),
2493 * panic(). We do not support continue-on-error operation. */
2494static void ocfs2_handle_error(struct super_block *sb)
2495{
2496 struct ocfs2_super *osb = OCFS2_SB(sb);
2497
2498 if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_PANIC)
2499 panic("OCFS2: (device %s): panic forced after error\n",
2500 sb->s_id);
2501
2502 ocfs2_set_osb_flag(osb, OCFS2_OSB_ERROR_FS);
2503
2504 if (sb->s_flags & MS_RDONLY &&
2505 (ocfs2_is_soft_readonly(osb) ||
2506 ocfs2_is_hard_readonly(osb)))
2507 return;
2508
2509 printk(KERN_CRIT "File system is now read-only due to the potential "
2510 "of on-disk corruption. Please run fsck.ocfs2 once the file "
2511 "system is unmounted.\n");
2512 sb->s_flags |= MS_RDONLY;
2513 ocfs2_set_ro_flag(osb, 0);
2514}
2515
2516static char error_buf[1024];
2517
2518void __ocfs2_error(struct super_block *sb,
2519 const char *function,
2520 const char *fmt, ...)
2521{
2522 va_list args;
2523
2524 va_start(args, fmt);
4a6e617a 2525 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
ccd979bd
MF
2526 va_end(args);
2527
2528 /* Not using mlog here because we want to show the actual
2529 * function the error came from. */
2530 printk(KERN_CRIT "OCFS2: ERROR (device %s): %s: %s\n",
2531 sb->s_id, function, error_buf);
2532
2533 ocfs2_handle_error(sb);
2534}
2535
2536/* Handle critical errors. This is intentionally more drastic than
2537 * ocfs2_handle_error, so we only use for things like journal errors,
2538 * etc. */
2539void __ocfs2_abort(struct super_block* sb,
2540 const char *function,
2541 const char *fmt, ...)
2542{
2543 va_list args;
2544
2545 va_start(args, fmt);
4a6e617a 2546 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
ccd979bd
MF
2547 va_end(args);
2548
2549 printk(KERN_CRIT "OCFS2: abort (device %s): %s: %s\n",
2550 sb->s_id, function, error_buf);
2551
2552 /* We don't have the cluster support yet to go straight to
2553 * hard readonly in here. Until then, we want to keep
2554 * ocfs2_abort() so that we can at least mark critical
2555 * errors.
2556 *
2557 * TODO: This should abort the journal and alert other nodes
2558 * that our slot needs recovery. */
2559
2560 /* Force a panic(). This stinks, but it's better than letting
2561 * things continue without having a proper hard readonly
2562 * here. */
a2f2ddbf
SM
2563 if (!ocfs2_mount_local(OCFS2_SB(sb)))
2564 OCFS2_SB(sb)->s_mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
ccd979bd
MF
2565 ocfs2_handle_error(sb);
2566}
2567
e4b963f1
JB
2568/*
2569 * Void signal blockers, because in-kernel sigprocmask() only fails
2570 * when SIG_* is wrong.
2571 */
2572void ocfs2_block_signals(sigset_t *oldset)
2573{
2574 int rc;
2575 sigset_t blocked;
2576
2577 sigfillset(&blocked);
2578 rc = sigprocmask(SIG_BLOCK, &blocked, oldset);
2579 BUG_ON(rc);
2580}
2581
2582void ocfs2_unblock_signals(sigset_t *oldset)
2583{
2584 int rc = sigprocmask(SIG_SETMASK, oldset, NULL);
2585 BUG_ON(rc);
2586}
2587
ccd979bd
MF
2588module_init(ocfs2_init);
2589module_exit(ocfs2_exit);