]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/ocfs2/super.c
ocfs2: Mark system files as not subject to quota accounting
[mirror_ubuntu-artful-kernel.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>
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>
d550071c 42#include <linux/mount.h>
6953b4c0 43#include <linux/seq_file.h>
ccd979bd
MF
44
45#define MLOG_MASK_PREFIX ML_SUPER
46#include <cluster/masklog.h>
47
48#include "ocfs2.h"
49
50/* this should be the only file to include a version 1 header */
51#include "ocfs1_fs_compat.h"
52
53#include "alloc.h"
54#include "dlmglue.h"
55#include "export.h"
56#include "extent_map.h"
57#include "heartbeat.h"
58#include "inode.h"
59#include "journal.h"
60#include "localalloc.h"
61#include "namei.h"
62#include "slot_map.h"
63#include "super.h"
64#include "sysfile.h"
65#include "uptodate.h"
66#include "ver.h"
cf1d6c76 67#include "xattr.h"
ccd979bd
MF
68
69#include "buffer_head_io.h"
70
e18b890b 71static struct kmem_cache *ocfs2_inode_cachep = NULL;
ccd979bd 72
ccd979bd
MF
73/* OCFS2 needs to schedule several differnt types of work which
74 * require cluster locking, disk I/O, recovery waits, etc. Since these
75 * types of work tend to be heavy we avoid using the kernel events
76 * workqueue and schedule on our own. */
77struct workqueue_struct *ocfs2_wq = NULL;
78
79static struct dentry *ocfs2_debugfs_root = NULL;
80
81MODULE_AUTHOR("Oracle");
82MODULE_LICENSE("GPL");
83
c0123ade
TY
84struct mount_options
85{
d147b3d6 86 unsigned long commit_interval;
c0123ade
TY
87 unsigned long mount_opt;
88 unsigned int atime_quantum;
89 signed short slot;
2fbe8d1e 90 unsigned int localalloc_opt;
b61817e1 91 char cluster_stack[OCFS2_STACK_LABEL_LEN + 1];
c0123ade
TY
92};
93
ccd979bd 94static int ocfs2_parse_options(struct super_block *sb, char *options,
c0123ade 95 struct mount_options *mopt,
baf4661a 96 int is_remount);
d550071c 97static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt);
ccd979bd
MF
98static void ocfs2_put_super(struct super_block *sb);
99static int ocfs2_mount_volume(struct super_block *sb);
100static int ocfs2_remount(struct super_block *sb, int *flags, char *data);
101static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err);
102static int ocfs2_initialize_mem_caches(void);
103static void ocfs2_free_mem_caches(void);
104static void ocfs2_delete_osb(struct ocfs2_super *osb);
105
726c3342 106static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf);
ccd979bd
MF
107
108static int ocfs2_sync_fs(struct super_block *sb, int wait);
109
110static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb);
111static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb);
bddb8eb3 112static void ocfs2_release_system_inodes(struct ocfs2_super *osb);
ccd979bd
MF
113static int ocfs2_check_volume(struct ocfs2_super *osb);
114static int ocfs2_verify_volume(struct ocfs2_dinode *di,
115 struct buffer_head *bh,
116 u32 sectsize);
117static int ocfs2_initialize_super(struct super_block *sb,
118 struct buffer_head *bh,
119 int sector_size);
120static int ocfs2_get_sector(struct super_block *sb,
121 struct buffer_head **bh,
122 int block,
123 int sect_size);
124static void ocfs2_write_super(struct super_block *sb);
125static struct inode *ocfs2_alloc_inode(struct super_block *sb);
126static void ocfs2_destroy_inode(struct inode *inode);
127
ee9b6d61 128static const struct super_operations ocfs2_sops = {
ccd979bd
MF
129 .statfs = ocfs2_statfs,
130 .alloc_inode = ocfs2_alloc_inode,
131 .destroy_inode = ocfs2_destroy_inode,
132 .drop_inode = ocfs2_drop_inode,
133 .clear_inode = ocfs2_clear_inode,
134 .delete_inode = ocfs2_delete_inode,
135 .sync_fs = ocfs2_sync_fs,
136 .write_super = ocfs2_write_super,
137 .put_super = ocfs2_put_super,
138 .remount_fs = ocfs2_remount,
d550071c 139 .show_options = ocfs2_show_options,
ccd979bd
MF
140};
141
142enum {
143 Opt_barrier,
144 Opt_err_panic,
145 Opt_err_ro,
146 Opt_intr,
147 Opt_nointr,
148 Opt_hb_none,
149 Opt_hb_local,
150 Opt_data_ordered,
151 Opt_data_writeback,
7f1a37e3 152 Opt_atime_quantum,
baf4661a 153 Opt_slot,
d147b3d6 154 Opt_commit,
2fbe8d1e 155 Opt_localalloc,
53fc622b 156 Opt_localflocks,
b61817e1 157 Opt_stack,
cf1d6c76
TY
158 Opt_user_xattr,
159 Opt_nouser_xattr,
12462f1d 160 Opt_inode64,
a68979b8
TY
161 Opt_acl,
162 Opt_noacl,
ccd979bd
MF
163 Opt_err,
164};
165
a447c093 166static const match_table_t tokens = {
ccd979bd
MF
167 {Opt_barrier, "barrier=%u"},
168 {Opt_err_panic, "errors=panic"},
169 {Opt_err_ro, "errors=remount-ro"},
170 {Opt_intr, "intr"},
171 {Opt_nointr, "nointr"},
172 {Opt_hb_none, OCFS2_HB_NONE},
173 {Opt_hb_local, OCFS2_HB_LOCAL},
174 {Opt_data_ordered, "data=ordered"},
175 {Opt_data_writeback, "data=writeback"},
7f1a37e3 176 {Opt_atime_quantum, "atime_quantum=%u"},
baf4661a 177 {Opt_slot, "preferred_slot=%u"},
d147b3d6 178 {Opt_commit, "commit=%u"},
2fbe8d1e 179 {Opt_localalloc, "localalloc=%d"},
53fc622b 180 {Opt_localflocks, "localflocks"},
b61817e1 181 {Opt_stack, "cluster_stack=%s"},
cf1d6c76
TY
182 {Opt_user_xattr, "user_xattr"},
183 {Opt_nouser_xattr, "nouser_xattr"},
12462f1d 184 {Opt_inode64, "inode64"},
a68979b8
TY
185 {Opt_acl, "acl"},
186 {Opt_noacl, "noacl"},
ccd979bd
MF
187 {Opt_err, NULL}
188};
189
190/*
191 * write_super and sync_fs ripped right out of ext3.
192 */
193static void ocfs2_write_super(struct super_block *sb)
194{
7892f2f4 195 if (mutex_trylock(&sb->s_lock) != 0)
ccd979bd
MF
196 BUG();
197 sb->s_dirt = 0;
198}
199
200static int ocfs2_sync_fs(struct super_block *sb, int wait)
201{
bddb8eb3 202 int status;
ccd979bd
MF
203 tid_t target;
204 struct ocfs2_super *osb = OCFS2_SB(sb);
205
206 sb->s_dirt = 0;
207
208 if (ocfs2_is_hard_readonly(osb))
209 return -EROFS;
210
211 if (wait) {
212 status = ocfs2_flush_truncate_log(osb);
213 if (status < 0)
214 mlog_errno(status);
215 } else {
216 ocfs2_schedule_truncate_log_flush(osb, 0);
217 }
218
2b4e30fb
JB
219 if (jbd2_journal_start_commit(OCFS2_SB(sb)->journal->j_journal,
220 &target)) {
ccd979bd 221 if (wait)
2b4e30fb
JB
222 jbd2_log_wait_commit(OCFS2_SB(sb)->journal->j_journal,
223 target);
ccd979bd
MF
224 }
225 return 0;
226}
227
1a224ad1
JK
228static int ocfs2_need_system_inode(struct ocfs2_super *osb, int ino)
229{
230 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_USRQUOTA)
231 && (ino == USER_QUOTA_SYSTEM_INODE
232 || ino == LOCAL_USER_QUOTA_SYSTEM_INODE))
233 return 0;
234 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)
235 && (ino == GROUP_QUOTA_SYSTEM_INODE
236 || ino == LOCAL_GROUP_QUOTA_SYSTEM_INODE))
237 return 0;
238 return 1;
239}
240
ccd979bd
MF
241static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb)
242{
243 struct inode *new = NULL;
244 int status = 0;
245 int i;
246
247 mlog_entry_void();
248
5fa0613e 249 new = ocfs2_iget(osb, osb->root_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
ccd979bd
MF
250 if (IS_ERR(new)) {
251 status = PTR_ERR(new);
252 mlog_errno(status);
253 goto bail;
254 }
255 osb->root_inode = new;
256
5fa0613e 257 new = ocfs2_iget(osb, osb->system_dir_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
ccd979bd
MF
258 if (IS_ERR(new)) {
259 status = PTR_ERR(new);
260 mlog_errno(status);
261 goto bail;
262 }
263 osb->sys_root_inode = new;
264
265 for (i = OCFS2_FIRST_ONLINE_SYSTEM_INODE;
266 i <= OCFS2_LAST_GLOBAL_SYSTEM_INODE; i++) {
1a224ad1
JK
267 if (!ocfs2_need_system_inode(osb, i))
268 continue;
ccd979bd
MF
269 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
270 if (!new) {
271 ocfs2_release_system_inodes(osb);
272 status = -EINVAL;
273 mlog_errno(status);
274 /* FIXME: Should ERROR_RO_FS */
275 mlog(ML_ERROR, "Unable to load system inode %d, "
276 "possibly corrupt fs?", i);
277 goto bail;
278 }
279 // the array now has one ref, so drop this one
280 iput(new);
281 }
282
283bail:
284 mlog_exit(status);
285 return status;
286}
287
288static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb)
289{
290 struct inode *new = NULL;
291 int status = 0;
292 int i;
293
294 mlog_entry_void();
295
296 for (i = OCFS2_LAST_GLOBAL_SYSTEM_INODE + 1;
297 i < NUM_SYSTEM_INODES;
298 i++) {
1a224ad1
JK
299 if (!ocfs2_need_system_inode(osb, i))
300 continue;
ccd979bd
MF
301 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
302 if (!new) {
303 ocfs2_release_system_inodes(osb);
304 status = -EINVAL;
305 mlog(ML_ERROR, "status=%d, sysfile=%d, slot=%d\n",
306 status, i, osb->slot_num);
307 goto bail;
308 }
309 /* the array now has one ref, so drop this one */
310 iput(new);
311 }
312
313bail:
314 mlog_exit(status);
315 return status;
316}
317
bddb8eb3 318static void ocfs2_release_system_inodes(struct ocfs2_super *osb)
ccd979bd 319{
bddb8eb3 320 int i;
ccd979bd
MF
321 struct inode *inode;
322
323 mlog_entry_void();
324
325 for (i = 0; i < NUM_SYSTEM_INODES; i++) {
326 inode = osb->system_inodes[i];
327 if (inode) {
328 iput(inode);
329 osb->system_inodes[i] = NULL;
330 }
331 }
332
333 inode = osb->sys_root_inode;
334 if (inode) {
335 iput(inode);
336 osb->sys_root_inode = NULL;
337 }
338
339 inode = osb->root_inode;
340 if (inode) {
341 iput(inode);
342 osb->root_inode = NULL;
343 }
344
bddb8eb3 345 mlog_exit(0);
ccd979bd
MF
346}
347
348/* We're allocating fs objects, use GFP_NOFS */
349static struct inode *ocfs2_alloc_inode(struct super_block *sb)
350{
351 struct ocfs2_inode_info *oi;
352
e6b4f8da 353 oi = kmem_cache_alloc(ocfs2_inode_cachep, GFP_NOFS);
ccd979bd
MF
354 if (!oi)
355 return NULL;
356
2b4e30fb 357 jbd2_journal_init_jbd_inode(&oi->ip_jinode, &oi->vfs_inode);
ccd979bd
MF
358 return &oi->vfs_inode;
359}
360
361static void ocfs2_destroy_inode(struct inode *inode)
362{
363 kmem_cache_free(ocfs2_inode_cachep, OCFS2_I(inode));
364}
365
5a254031
MF
366static unsigned long long ocfs2_max_file_offset(unsigned int bbits,
367 unsigned int cbits)
ccd979bd 368{
5a254031
MF
369 unsigned int bytes = 1 << cbits;
370 unsigned int trim = bytes;
371 unsigned int bitshift = 32;
372
373 /*
374 * i_size and all block offsets in ocfs2 are always 64 bits
375 * wide. i_clusters is 32 bits, in cluster-sized units. So on
376 * 64 bit platforms, cluster size will be the limiting factor.
ccd979bd
MF
377 */
378
379#if BITS_PER_LONG == 32
380# if defined(CONFIG_LBD)
2ecd05ae 381 BUILD_BUG_ON(sizeof(sector_t) != 8);
5a254031
MF
382 /*
383 * We might be limited by page cache size.
384 */
385 if (bytes > PAGE_CACHE_SIZE) {
386 bytes = PAGE_CACHE_SIZE;
387 trim = 1;
388 /*
389 * Shift by 31 here so that we don't get larger than
390 * MAX_LFS_FILESIZE
391 */
392 bitshift = 31;
393 }
ccd979bd 394# else
5a254031
MF
395 /*
396 * We are limited by the size of sector_t. Use block size, as
397 * that's what we expose to the VFS.
398 */
399 bytes = 1 << bbits;
400 trim = 1;
401 bitshift = 31;
ccd979bd
MF
402# endif
403#endif
404
5a254031
MF
405 /*
406 * Trim by a whole cluster when we can actually approach the
407 * on-disk limits. Otherwise we can overflow i_clusters when
408 * an extent start is at the max offset.
409 */
410 return (((unsigned long long)bytes) << bitshift) - trim;
ccd979bd
MF
411}
412
413static int ocfs2_remount(struct super_block *sb, int *flags, char *data)
414{
415 int incompat_features;
416 int ret = 0;
c0123ade 417 struct mount_options parsed_options;
ccd979bd
MF
418 struct ocfs2_super *osb = OCFS2_SB(sb);
419
c0123ade 420 if (!ocfs2_parse_options(sb, data, &parsed_options, 1)) {
ccd979bd
MF
421 ret = -EINVAL;
422 goto out;
423 }
424
425 if ((osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) !=
c0123ade 426 (parsed_options.mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
ccd979bd
MF
427 ret = -EINVAL;
428 mlog(ML_ERROR, "Cannot change heartbeat mode on remount\n");
429 goto out;
430 }
431
432 if ((osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK) !=
c0123ade 433 (parsed_options.mount_opt & OCFS2_MOUNT_DATA_WRITEBACK)) {
ccd979bd
MF
434 ret = -EINVAL;
435 mlog(ML_ERROR, "Cannot change data mode on remount\n");
436 goto out;
437 }
438
12462f1d
JB
439 /* Probably don't want this on remount; it might
440 * mess with other nodes */
441 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64) &&
442 (parsed_options.mount_opt & OCFS2_MOUNT_INODE64)) {
443 ret = -EINVAL;
444 mlog(ML_ERROR, "Cannot enable inode64 on remount\n");
445 goto out;
446 }
447
ccd979bd
MF
448 /* We're going to/from readonly mode. */
449 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
450 /* Lock here so the check of HARD_RO and the potential
451 * setting of SOFT_RO is atomic. */
452 spin_lock(&osb->osb_lock);
453 if (osb->osb_flags & OCFS2_OSB_HARD_RO) {
454 mlog(ML_ERROR, "Remount on readonly device is forbidden.\n");
455 ret = -EROFS;
456 goto unlock_osb;
457 }
458
459 if (*flags & MS_RDONLY) {
460 mlog(0, "Going to ro mode.\n");
461 sb->s_flags |= MS_RDONLY;
462 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
463 } else {
464 mlog(0, "Making ro filesystem writeable.\n");
465
466 if (osb->osb_flags & OCFS2_OSB_ERROR_FS) {
467 mlog(ML_ERROR, "Cannot remount RDWR "
468 "filesystem due to previous errors.\n");
469 ret = -EROFS;
470 goto unlock_osb;
471 }
472 incompat_features = OCFS2_HAS_RO_COMPAT_FEATURE(sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP);
473 if (incompat_features) {
474 mlog(ML_ERROR, "Cannot remount RDWR because "
475 "of unsupported optional features "
476 "(%x).\n", incompat_features);
477 ret = -EINVAL;
478 goto unlock_osb;
479 }
480 sb->s_flags &= ~MS_RDONLY;
481 osb->osb_flags &= ~OCFS2_OSB_SOFT_RO;
482 }
483unlock_osb:
484 spin_unlock(&osb->osb_lock);
485 }
486
487 if (!ret) {
ccd979bd
MF
488 /* Only save off the new mount options in case of a successful
489 * remount. */
a68979b8
TY
490 if (!(osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR))
491 parsed_options.mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
c0123ade
TY
492 osb->s_mount_opt = parsed_options.mount_opt;
493 osb->s_atime_quantum = parsed_options.atime_quantum;
494 osb->preferred_slot = parsed_options.slot;
d147b3d6
MF
495 if (parsed_options.commit_interval)
496 osb->osb_commit_interval = parsed_options.commit_interval;
e001e796
MF
497
498 if (!ocfs2_is_hard_readonly(osb))
499 ocfs2_set_journal_params(osb);
ccd979bd
MF
500 }
501out:
502 return ret;
503}
504
505static int ocfs2_sb_probe(struct super_block *sb,
506 struct buffer_head **bh,
507 int *sector_size)
508{
bddb8eb3 509 int status, tmpstat;
ccd979bd
MF
510 struct ocfs1_vol_disk_hdr *hdr;
511 struct ocfs2_dinode *di;
512 int blksize;
513
514 *bh = NULL;
515
516 /* may be > 512 */
517 *sector_size = bdev_hardsect_size(sb->s_bdev);
518 if (*sector_size > OCFS2_MAX_BLOCKSIZE) {
519 mlog(ML_ERROR, "Hardware sector size too large: %d (max=%d)\n",
520 *sector_size, OCFS2_MAX_BLOCKSIZE);
521 status = -EINVAL;
522 goto bail;
523 }
524
525 /* Can this really happen? */
526 if (*sector_size < OCFS2_MIN_BLOCKSIZE)
527 *sector_size = OCFS2_MIN_BLOCKSIZE;
528
529 /* check block zero for old format */
530 status = ocfs2_get_sector(sb, bh, 0, *sector_size);
531 if (status < 0) {
532 mlog_errno(status);
533 goto bail;
534 }
535 hdr = (struct ocfs1_vol_disk_hdr *) (*bh)->b_data;
536 if (hdr->major_version == OCFS1_MAJOR_VERSION) {
537 mlog(ML_ERROR, "incompatible version: %u.%u\n",
538 hdr->major_version, hdr->minor_version);
539 status = -EINVAL;
540 }
541 if (memcmp(hdr->signature, OCFS1_VOLUME_SIGNATURE,
542 strlen(OCFS1_VOLUME_SIGNATURE)) == 0) {
543 mlog(ML_ERROR, "incompatible volume signature: %8s\n",
544 hdr->signature);
545 status = -EINVAL;
546 }
547 brelse(*bh);
548 *bh = NULL;
549 if (status < 0) {
550 mlog(ML_ERROR, "This is an ocfs v1 filesystem which must be "
551 "upgraded before mounting with ocfs v2\n");
552 goto bail;
553 }
554
555 /*
556 * Now check at magic offset for 512, 1024, 2048, 4096
557 * blocksizes. 4096 is the maximum blocksize because it is
558 * the minimum clustersize.
559 */
560 status = -EINVAL;
561 for (blksize = *sector_size;
562 blksize <= OCFS2_MAX_BLOCKSIZE;
563 blksize <<= 1) {
564 tmpstat = ocfs2_get_sector(sb, bh,
565 OCFS2_SUPER_BLOCK_BLKNO,
566 blksize);
567 if (tmpstat < 0) {
568 status = tmpstat;
569 mlog_errno(status);
570 goto bail;
571 }
572 di = (struct ocfs2_dinode *) (*bh)->b_data;
573 status = ocfs2_verify_volume(di, *bh, blksize);
574 if (status >= 0)
575 goto bail;
576 brelse(*bh);
577 *bh = NULL;
578 if (status != -EAGAIN)
579 break;
580 }
581
582bail:
583 return status;
584}
585
c271c5c2
SM
586static int ocfs2_verify_heartbeat(struct ocfs2_super *osb)
587{
588 if (ocfs2_mount_local(osb)) {
589 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
590 mlog(ML_ERROR, "Cannot heartbeat on a locally "
591 "mounted device.\n");
592 return -EINVAL;
593 }
594 }
595
b61817e1
JB
596 if (ocfs2_userspace_stack(osb)) {
597 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
598 mlog(ML_ERROR, "Userspace stack expected, but "
599 "o2cb heartbeat arguments passed to mount\n");
600 return -EINVAL;
601 }
602 }
603
c271c5c2 604 if (!(osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
b61817e1
JB
605 if (!ocfs2_mount_local(osb) && !ocfs2_is_hard_readonly(osb) &&
606 !ocfs2_userspace_stack(osb)) {
c271c5c2
SM
607 mlog(ML_ERROR, "Heartbeat has to be started to mount "
608 "a read-write clustered device.\n");
609 return -EINVAL;
610 }
611 }
612
613 return 0;
614}
615
b61817e1
JB
616/*
617 * If we're using a userspace stack, mount should have passed
618 * a name that matches the disk. If not, mount should not
619 * have passed a stack.
620 */
621static int ocfs2_verify_userspace_stack(struct ocfs2_super *osb,
622 struct mount_options *mopt)
623{
624 if (!ocfs2_userspace_stack(osb) && mopt->cluster_stack[0]) {
625 mlog(ML_ERROR,
626 "cluster stack passed to mount, but this filesystem "
627 "does not support it\n");
628 return -EINVAL;
629 }
630
631 if (ocfs2_userspace_stack(osb) &&
632 strncmp(osb->osb_cluster_stack, mopt->cluster_stack,
633 OCFS2_STACK_LABEL_LEN)) {
634 mlog(ML_ERROR,
635 "cluster stack passed to mount (\"%s\") does not "
636 "match the filesystem (\"%s\")\n",
637 mopt->cluster_stack,
638 osb->osb_cluster_stack);
639 return -EINVAL;
640 }
641
642 return 0;
643}
644
ccd979bd
MF
645static int ocfs2_fill_super(struct super_block *sb, void *data, int silent)
646{
647 struct dentry *root;
648 int status, sector_size;
c0123ade 649 struct mount_options parsed_options;
ccd979bd
MF
650 struct inode *inode = NULL;
651 struct ocfs2_super *osb = NULL;
652 struct buffer_head *bh = NULL;
c271c5c2 653 char nodestr[8];
ccd979bd
MF
654
655 mlog_entry("%p, %p, %i", sb, data, silent);
656
c0123ade 657 if (!ocfs2_parse_options(sb, data, &parsed_options, 0)) {
ccd979bd
MF
658 status = -EINVAL;
659 goto read_super_error;
660 }
661
662 /* probe for superblock */
663 status = ocfs2_sb_probe(sb, &bh, &sector_size);
664 if (status < 0) {
665 mlog(ML_ERROR, "superblock probe failed!\n");
666 goto read_super_error;
667 }
668
669 status = ocfs2_initialize_super(sb, bh, sector_size);
670 osb = OCFS2_SB(sb);
671 if (status < 0) {
672 mlog_errno(status);
673 goto read_super_error;
674 }
675 brelse(bh);
676 bh = NULL;
a68979b8
TY
677
678 if (!(osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR))
679 parsed_options.mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
680
c0123ade
TY
681 osb->s_mount_opt = parsed_options.mount_opt;
682 osb->s_atime_quantum = parsed_options.atime_quantum;
683 osb->preferred_slot = parsed_options.slot;
d147b3d6 684 osb->osb_commit_interval = parsed_options.commit_interval;
9c7af40b
MF
685 osb->local_alloc_default_bits = ocfs2_megabytes_to_clusters(sb, parsed_options.localalloc_opt);
686 osb->local_alloc_bits = osb->local_alloc_default_bits;
ccd979bd 687
b61817e1
JB
688 status = ocfs2_verify_userspace_stack(osb, &parsed_options);
689 if (status)
690 goto read_super_error;
691
ccd979bd
MF
692 sb->s_magic = OCFS2_SUPER_MAGIC;
693
a68979b8
TY
694 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
695 ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
696
ccd979bd
MF
697 /* Hard readonly mode only if: bdev_read_only, MS_RDONLY,
698 * heartbeat=none */
699 if (bdev_read_only(sb->s_bdev)) {
700 if (!(sb->s_flags & MS_RDONLY)) {
701 status = -EACCES;
702 mlog(ML_ERROR, "Readonly device detected but readonly "
703 "mount was not specified.\n");
704 goto read_super_error;
705 }
706
707 /* You should not be able to start a local heartbeat
708 * on a readonly device. */
709 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
710 status = -EROFS;
711 mlog(ML_ERROR, "Local heartbeat specified on readonly "
712 "device.\n");
713 goto read_super_error;
714 }
715
716 status = ocfs2_check_journals_nolocks(osb);
717 if (status < 0) {
718 if (status == -EROFS)
719 mlog(ML_ERROR, "Recovery required on readonly "
720 "file system, but write access is "
721 "unavailable.\n");
722 else
723 mlog_errno(status);
724 goto read_super_error;
725 }
726
727 ocfs2_set_ro_flag(osb, 1);
728
729 printk(KERN_NOTICE "Readonly device detected. No cluster "
730 "services will be utilized for this mount. Recovery "
731 "will be skipped.\n");
732 }
733
734 if (!ocfs2_is_hard_readonly(osb)) {
ccd979bd
MF
735 if (sb->s_flags & MS_RDONLY)
736 ocfs2_set_ro_flag(osb, 0);
737 }
738
c271c5c2
SM
739 status = ocfs2_verify_heartbeat(osb);
740 if (status < 0) {
741 mlog_errno(status);
742 goto read_super_error;
743 }
744
ccd979bd
MF
745 osb->osb_debug_root = debugfs_create_dir(osb->uuid_str,
746 ocfs2_debugfs_root);
747 if (!osb->osb_debug_root) {
748 status = -EINVAL;
749 mlog(ML_ERROR, "Unable to create per-mount debugfs root.\n");
750 goto read_super_error;
751 }
752
753 status = ocfs2_mount_volume(sb);
754 if (osb->root_inode)
755 inode = igrab(osb->root_inode);
756
757 if (status < 0)
758 goto read_super_error;
759
760 if (!inode) {
761 status = -EIO;
762 mlog_errno(status);
763 goto read_super_error;
764 }
765
766 root = d_alloc_root(inode);
767 if (!root) {
768 status = -ENOMEM;
769 mlog_errno(status);
770 goto read_super_error;
771 }
772
773 sb->s_root = root;
774
775 ocfs2_complete_mount_recovery(osb);
776
c271c5c2
SM
777 if (ocfs2_mount_local(osb))
778 snprintf(nodestr, sizeof(nodestr), "local");
779 else
19fdb624 780 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
c271c5c2
SM
781
782 printk(KERN_INFO "ocfs2: Mounting device (%s) on (node %s, slot %d) "
781ee3e2 783 "with %s data mode.\n",
c271c5c2 784 osb->dev_str, nodestr, osb->slot_num,
ccd979bd
MF
785 osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK ? "writeback" :
786 "ordered");
787
788 atomic_set(&osb->vol_state, VOLUME_MOUNTED);
789 wake_up(&osb->osb_mount_event);
790
791 mlog_exit(status);
792 return status;
793
794read_super_error:
a81cb88b 795 brelse(bh);
ccd979bd
MF
796
797 if (inode)
798 iput(inode);
799
800 if (osb) {
801 atomic_set(&osb->vol_state, VOLUME_DISABLED);
802 wake_up(&osb->osb_mount_event);
803 ocfs2_dismount_volume(sb, 1);
804 }
805
806 mlog_exit(status);
807 return status;
808}
809
454e2398
DH
810static int ocfs2_get_sb(struct file_system_type *fs_type,
811 int flags,
812 const char *dev_name,
813 void *data,
814 struct vfsmount *mnt)
ccd979bd 815{
454e2398
DH
816 return get_sb_bdev(fs_type, flags, dev_name, data, ocfs2_fill_super,
817 mnt);
ccd979bd
MF
818}
819
820static struct file_system_type ocfs2_fs_type = {
821 .owner = THIS_MODULE,
822 .name = "ocfs2",
823 .get_sb = ocfs2_get_sb, /* is this called when we mount
824 * the fs? */
825 .kill_sb = kill_block_super, /* set to the generic one
826 * right now, but do we
827 * need to change that? */
1ba9da2f 828 .fs_flags = FS_REQUIRES_DEV|FS_RENAME_DOES_D_MOVE,
ccd979bd
MF
829 .next = NULL
830};
831
832static int ocfs2_parse_options(struct super_block *sb,
833 char *options,
c0123ade 834 struct mount_options *mopt,
ccd979bd
MF
835 int is_remount)
836{
837 int status;
838 char *p;
839
840 mlog_entry("remount: %d, options: \"%s\"\n", is_remount,
841 options ? options : "(none)");
842
d147b3d6 843 mopt->commit_interval = 0;
c0123ade
TY
844 mopt->mount_opt = 0;
845 mopt->atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
846 mopt->slot = OCFS2_INVALID_SLOT;
2fbe8d1e 847 mopt->localalloc_opt = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE;
b61817e1 848 mopt->cluster_stack[0] = '\0';
ccd979bd
MF
849
850 if (!options) {
851 status = 1;
852 goto bail;
853 }
854
855 while ((p = strsep(&options, ",")) != NULL) {
856 int token, option;
857 substring_t args[MAX_OPT_ARGS];
858
859 if (!*p)
860 continue;
861
862 token = match_token(p, tokens, args);
863 switch (token) {
864 case Opt_hb_local:
c0123ade 865 mopt->mount_opt |= OCFS2_MOUNT_HB_LOCAL;
ccd979bd
MF
866 break;
867 case Opt_hb_none:
c0123ade 868 mopt->mount_opt &= ~OCFS2_MOUNT_HB_LOCAL;
ccd979bd
MF
869 break;
870 case Opt_barrier:
871 if (match_int(&args[0], &option)) {
872 status = 0;
873 goto bail;
874 }
875 if (option)
c0123ade 876 mopt->mount_opt |= OCFS2_MOUNT_BARRIER;
ccd979bd 877 else
c0123ade 878 mopt->mount_opt &= ~OCFS2_MOUNT_BARRIER;
ccd979bd
MF
879 break;
880 case Opt_intr:
c0123ade 881 mopt->mount_opt &= ~OCFS2_MOUNT_NOINTR;
ccd979bd
MF
882 break;
883 case Opt_nointr:
c0123ade 884 mopt->mount_opt |= OCFS2_MOUNT_NOINTR;
ccd979bd
MF
885 break;
886 case Opt_err_panic:
c0123ade 887 mopt->mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
ccd979bd
MF
888 break;
889 case Opt_err_ro:
c0123ade 890 mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_PANIC;
ccd979bd
MF
891 break;
892 case Opt_data_ordered:
c0123ade 893 mopt->mount_opt &= ~OCFS2_MOUNT_DATA_WRITEBACK;
ccd979bd
MF
894 break;
895 case Opt_data_writeback:
c0123ade 896 mopt->mount_opt |= OCFS2_MOUNT_DATA_WRITEBACK;
ccd979bd 897 break;
cf1d6c76
TY
898 case Opt_user_xattr:
899 mopt->mount_opt &= ~OCFS2_MOUNT_NOUSERXATTR;
900 break;
901 case Opt_nouser_xattr:
902 mopt->mount_opt |= OCFS2_MOUNT_NOUSERXATTR;
903 break;
7f1a37e3
TY
904 case Opt_atime_quantum:
905 if (match_int(&args[0], &option)) {
906 status = 0;
907 goto bail;
908 }
909 if (option >= 0)
c0123ade 910 mopt->atime_quantum = option;
7f1a37e3 911 break;
baf4661a
SM
912 case Opt_slot:
913 option = 0;
914 if (match_int(&args[0], &option)) {
915 status = 0;
916 goto bail;
917 }
918 if (option)
c0123ade 919 mopt->slot = (s16)option;
baf4661a 920 break;
d147b3d6
MF
921 case Opt_commit:
922 option = 0;
923 if (match_int(&args[0], &option)) {
924 status = 0;
925 goto bail;
926 }
927 if (option < 0)
928 return 0;
929 if (option == 0)
2b4e30fb 930 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
d147b3d6
MF
931 mopt->commit_interval = HZ * option;
932 break;
2fbe8d1e
SM
933 case Opt_localalloc:
934 option = 0;
935 if (match_int(&args[0], &option)) {
936 status = 0;
937 goto bail;
938 }
939 if (option >= 0 && (option <= ocfs2_local_alloc_size(sb) * 8))
940 mopt->localalloc_opt = option;
941 break;
53fc622b
MF
942 case Opt_localflocks:
943 /*
944 * Changing this during remount could race
945 * flock() requests, or "unbalance" existing
946 * ones (e.g., a lock is taken in one mode but
947 * dropped in the other). If users care enough
948 * to flip locking modes during remount, we
949 * could add a "local" flag to individual
950 * flock structures for proper tracking of
951 * state.
952 */
953 if (!is_remount)
954 mopt->mount_opt |= OCFS2_MOUNT_LOCALFLOCKS;
955 break;
b61817e1
JB
956 case Opt_stack:
957 /* Check both that the option we were passed
958 * is of the right length and that it is a proper
959 * string of the right length.
960 */
961 if (((args[0].to - args[0].from) !=
962 OCFS2_STACK_LABEL_LEN) ||
963 (strnlen(args[0].from,
964 OCFS2_STACK_LABEL_LEN) !=
965 OCFS2_STACK_LABEL_LEN)) {
966 mlog(ML_ERROR,
967 "Invalid cluster_stack option\n");
968 status = 0;
969 goto bail;
970 }
971 memcpy(mopt->cluster_stack, args[0].from,
972 OCFS2_STACK_LABEL_LEN);
973 mopt->cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
974 break;
12462f1d
JB
975 case Opt_inode64:
976 mopt->mount_opt |= OCFS2_MOUNT_INODE64;
977 break;
a68979b8
TY
978#ifdef CONFIG_OCFS2_FS_POSIX_ACL
979 case Opt_acl:
980 mopt->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
981 break;
982 case Opt_noacl:
983 mopt->mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
984 break;
985#else
986 case Opt_acl:
987 case Opt_noacl:
988 printk(KERN_INFO "ocfs2 (no)acl options not supported\n");
989 break;
990#endif
ccd979bd
MF
991 default:
992 mlog(ML_ERROR,
993 "Unrecognized mount option \"%s\" "
994 "or missing value\n", p);
995 status = 0;
996 goto bail;
997 }
998 }
999
1000 status = 1;
1001
1002bail:
1003 mlog_exit(status);
1004 return status;
1005}
1006
d550071c
SM
1007static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
1008{
1009 struct ocfs2_super *osb = OCFS2_SB(mnt->mnt_sb);
1010 unsigned long opts = osb->s_mount_opt;
ebcee4b5 1011 unsigned int local_alloc_megs;
d550071c
SM
1012
1013 if (opts & OCFS2_MOUNT_HB_LOCAL)
1014 seq_printf(s, ",_netdev,heartbeat=local");
1015 else
1016 seq_printf(s, ",heartbeat=none");
1017
1018 if (opts & OCFS2_MOUNT_NOINTR)
1019 seq_printf(s, ",nointr");
1020
1021 if (opts & OCFS2_MOUNT_DATA_WRITEBACK)
1022 seq_printf(s, ",data=writeback");
1023 else
1024 seq_printf(s, ",data=ordered");
1025
1026 if (opts & OCFS2_MOUNT_BARRIER)
1027 seq_printf(s, ",barrier=1");
1028
1029 if (opts & OCFS2_MOUNT_ERRORS_PANIC)
1030 seq_printf(s, ",errors=panic");
1031 else
1032 seq_printf(s, ",errors=remount-ro");
1033
1034 if (osb->preferred_slot != OCFS2_INVALID_SLOT)
1035 seq_printf(s, ",preferred_slot=%d", osb->preferred_slot);
1036
1037 if (osb->s_atime_quantum != OCFS2_DEFAULT_ATIME_QUANTUM)
1038 seq_printf(s, ",atime_quantum=%u", osb->s_atime_quantum);
1039
d147b3d6
MF
1040 if (osb->osb_commit_interval)
1041 seq_printf(s, ",commit=%u",
1042 (unsigned) (osb->osb_commit_interval / HZ));
1043
ebcee4b5
MF
1044 local_alloc_megs = osb->local_alloc_bits >> (20 - osb->s_clustersize_bits);
1045 if (local_alloc_megs != OCFS2_DEFAULT_LOCAL_ALLOC_SIZE)
1046 seq_printf(s, ",localalloc=%d", local_alloc_megs);
2fbe8d1e 1047
53fc622b
MF
1048 if (opts & OCFS2_MOUNT_LOCALFLOCKS)
1049 seq_printf(s, ",localflocks,");
1050
b61817e1
JB
1051 if (osb->osb_cluster_stack[0])
1052 seq_printf(s, ",cluster_stack=%.*s", OCFS2_STACK_LABEL_LEN,
1053 osb->osb_cluster_stack);
1054
b0f73cfc
SM
1055 if (opts & OCFS2_MOUNT_NOUSERXATTR)
1056 seq_printf(s, ",nouser_xattr");
1057 else
1058 seq_printf(s, ",user_xattr");
1059
12462f1d
JB
1060 if (opts & OCFS2_MOUNT_INODE64)
1061 seq_printf(s, ",inode64");
1062
a68979b8
TY
1063#ifdef CONFIG_OCFS2_FS_POSIX_ACL
1064 if (opts & OCFS2_MOUNT_POSIX_ACL)
1065 seq_printf(s, ",acl");
1066 else
1067 seq_printf(s, ",noacl");
1068#endif
1069
d550071c
SM
1070 return 0;
1071}
1072
ccd979bd
MF
1073static int __init ocfs2_init(void)
1074{
1075 int status;
1076
1077 mlog_entry_void();
1078
1079 ocfs2_print_version();
1080
ccd979bd
MF
1081 status = init_ocfs2_uptodate_cache();
1082 if (status < 0) {
1083 mlog_errno(status);
1084 goto leave;
1085 }
1086
1087 status = ocfs2_initialize_mem_caches();
1088 if (status < 0) {
1089 mlog_errno(status);
1090 goto leave;
1091 }
1092
1093 ocfs2_wq = create_singlethread_workqueue("ocfs2_wq");
1094 if (!ocfs2_wq) {
1095 status = -ENOMEM;
1096 goto leave;
1097 }
1098
ccd979bd
MF
1099 ocfs2_debugfs_root = debugfs_create_dir("ocfs2", NULL);
1100 if (!ocfs2_debugfs_root) {
1101 status = -EFAULT;
1102 mlog(ML_ERROR, "Unable to create ocfs2 debugfs root.\n");
1103 }
1104
63e0c48a
JB
1105 ocfs2_set_locking_protocol();
1106
ccd979bd
MF
1107leave:
1108 if (status < 0) {
1109 ocfs2_free_mem_caches();
1110 exit_ocfs2_uptodate_cache();
ccd979bd
MF
1111 }
1112
1113 mlog_exit(status);
1114
1115 if (status >= 0) {
1116 return register_filesystem(&ocfs2_fs_type);
1117 } else
1118 return -1;
1119}
1120
1121static void __exit ocfs2_exit(void)
1122{
1123 mlog_entry_void();
1124
1125 if (ocfs2_wq) {
1126 flush_workqueue(ocfs2_wq);
1127 destroy_workqueue(ocfs2_wq);
1128 }
1129
1130 debugfs_remove(ocfs2_debugfs_root);
1131
1132 ocfs2_free_mem_caches();
1133
1134 unregister_filesystem(&ocfs2_fs_type);
1135
ccd979bd
MF
1136 exit_ocfs2_uptodate_cache();
1137
1138 mlog_exit_void();
1139}
1140
1141static void ocfs2_put_super(struct super_block *sb)
1142{
1143 mlog_entry("(0x%p)\n", sb);
1144
1145 ocfs2_sync_blockdev(sb);
1146 ocfs2_dismount_volume(sb, 0);
1147
1148 mlog_exit_void();
1149}
1150
726c3342 1151static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
ccd979bd
MF
1152{
1153 struct ocfs2_super *osb;
1154 u32 numbits, freebits;
1155 int status;
1156 struct ocfs2_dinode *bm_lock;
1157 struct buffer_head *bh = NULL;
1158 struct inode *inode = NULL;
1159
726c3342 1160 mlog_entry("(%p, %p)\n", dentry->d_sb, buf);
ccd979bd 1161
726c3342 1162 osb = OCFS2_SB(dentry->d_sb);
ccd979bd
MF
1163
1164 inode = ocfs2_get_system_file_inode(osb,
1165 GLOBAL_BITMAP_SYSTEM_INODE,
1166 OCFS2_INVALID_SLOT);
1167 if (!inode) {
1168 mlog(ML_ERROR, "failed to get bitmap inode\n");
1169 status = -EIO;
1170 goto bail;
1171 }
1172
e63aecb6 1173 status = ocfs2_inode_lock(inode, &bh, 0);
ccd979bd
MF
1174 if (status < 0) {
1175 mlog_errno(status);
1176 goto bail;
1177 }
1178
1179 bm_lock = (struct ocfs2_dinode *) bh->b_data;
1180
1181 numbits = le32_to_cpu(bm_lock->id1.bitmap1.i_total);
1182 freebits = numbits - le32_to_cpu(bm_lock->id1.bitmap1.i_used);
1183
1184 buf->f_type = OCFS2_SUPER_MAGIC;
726c3342 1185 buf->f_bsize = dentry->d_sb->s_blocksize;
ccd979bd
MF
1186 buf->f_namelen = OCFS2_MAX_FILENAME_LEN;
1187 buf->f_blocks = ((sector_t) numbits) *
1188 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1189 buf->f_bfree = ((sector_t) freebits) *
1190 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1191 buf->f_bavail = buf->f_bfree;
1192 buf->f_files = numbits;
1193 buf->f_ffree = freebits;
1194
1195 brelse(bh);
1196
e63aecb6 1197 ocfs2_inode_unlock(inode, 0);
ccd979bd
MF
1198 status = 0;
1199bail:
1200 if (inode)
1201 iput(inode);
1202
1203 mlog_exit(status);
1204
1205 return status;
1206}
1207
51cc5068 1208static void ocfs2_inode_init_once(void *data)
ccd979bd
MF
1209{
1210 struct ocfs2_inode_info *oi = data;
1211
a35afb83
CL
1212 oi->ip_flags = 0;
1213 oi->ip_open_count = 0;
1214 spin_lock_init(&oi->ip_lock);
1215 ocfs2_extent_map_init(&oi->vfs_inode);
1216 INIT_LIST_HEAD(&oi->ip_io_markers);
1217 oi->ip_created_trans = 0;
1218 oi->ip_last_trans = 0;
1219 oi->ip_dir_start_lookup = 0;
ccd979bd 1220
a35afb83 1221 init_rwsem(&oi->ip_alloc_sem);
cf1d6c76 1222 init_rwsem(&oi->ip_xattr_sem);
a35afb83 1223 mutex_init(&oi->ip_io_mutex);
ccd979bd 1224
a35afb83
CL
1225 oi->ip_blkno = 0ULL;
1226 oi->ip_clusters = 0;
ccd979bd 1227
a35afb83 1228 ocfs2_lock_res_init_once(&oi->ip_rw_lockres);
e63aecb6 1229 ocfs2_lock_res_init_once(&oi->ip_inode_lockres);
a35afb83 1230 ocfs2_lock_res_init_once(&oi->ip_open_lockres);
ccd979bd 1231
a35afb83 1232 ocfs2_metadata_cache_init(&oi->vfs_inode);
ccd979bd 1233
a35afb83 1234 inode_init_once(&oi->vfs_inode);
ccd979bd
MF
1235}
1236
1237static int ocfs2_initialize_mem_caches(void)
1238{
1239 ocfs2_inode_cachep = kmem_cache_create("ocfs2_inode_cache",
fffb60f9
PJ
1240 sizeof(struct ocfs2_inode_info),
1241 0,
1242 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1243 SLAB_MEM_SPREAD),
20c2df83 1244 ocfs2_inode_init_once);
ccd979bd
MF
1245 if (!ocfs2_inode_cachep)
1246 return -ENOMEM;
1247
ccd979bd
MF
1248 return 0;
1249}
1250
1251static void ocfs2_free_mem_caches(void)
1252{
1253 if (ocfs2_inode_cachep)
1254 kmem_cache_destroy(ocfs2_inode_cachep);
ccd979bd
MF
1255
1256 ocfs2_inode_cachep = NULL;
ccd979bd
MF
1257}
1258
1259static int ocfs2_get_sector(struct super_block *sb,
1260 struct buffer_head **bh,
1261 int block,
1262 int sect_size)
1263{
1264 if (!sb_set_blocksize(sb, sect_size)) {
1265 mlog(ML_ERROR, "unable to set blocksize\n");
1266 return -EIO;
1267 }
1268
1269 *bh = sb_getblk(sb, block);
1270 if (!*bh) {
1271 mlog_errno(-EIO);
1272 return -EIO;
1273 }
1274 lock_buffer(*bh);
1275 if (!buffer_dirty(*bh))
1276 clear_buffer_uptodate(*bh);
1277 unlock_buffer(*bh);
1278 ll_rw_block(READ, 1, bh);
1279 wait_on_buffer(*bh);
1280 return 0;
1281}
1282
ccd979bd
MF
1283static int ocfs2_mount_volume(struct super_block *sb)
1284{
1285 int status = 0;
1286 int unlock_super = 0;
1287 struct ocfs2_super *osb = OCFS2_SB(sb);
1288
1289 mlog_entry_void();
1290
1291 if (ocfs2_is_hard_readonly(osb))
1292 goto leave;
1293
ccd979bd
MF
1294 status = ocfs2_dlm_init(osb);
1295 if (status < 0) {
1296 mlog_errno(status);
1297 goto leave;
1298 }
1299
ccd979bd
MF
1300 status = ocfs2_super_lock(osb, 1);
1301 if (status < 0) {
1302 mlog_errno(status);
1303 goto leave;
1304 }
1305 unlock_super = 1;
1306
1307 /* This will load up the node map and add ourselves to it. */
1308 status = ocfs2_find_slot(osb);
1309 if (status < 0) {
1310 mlog_errno(status);
1311 goto leave;
1312 }
1313
ccd979bd
MF
1314 /* load all node-local system inodes */
1315 status = ocfs2_init_local_system_inodes(osb);
1316 if (status < 0) {
1317 mlog_errno(status);
1318 goto leave;
1319 }
1320
1321 status = ocfs2_check_volume(osb);
1322 if (status < 0) {
1323 mlog_errno(status);
1324 goto leave;
1325 }
1326
1327 status = ocfs2_truncate_log_init(osb);
1328 if (status < 0) {
1329 mlog_errno(status);
1330 goto leave;
1331 }
1332
c271c5c2
SM
1333 if (ocfs2_mount_local(osb))
1334 goto leave;
1335
ccd979bd
MF
1336leave:
1337 if (unlock_super)
1338 ocfs2_super_unlock(osb, 1);
1339
1340 mlog_exit(status);
1341 return status;
1342}
1343
ccd979bd
MF
1344static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
1345{
286eaa95 1346 int tmp, hangup_needed = 0;
ccd979bd 1347 struct ocfs2_super *osb = NULL;
c271c5c2 1348 char nodestr[8];
ccd979bd
MF
1349
1350 mlog_entry("(0x%p)\n", sb);
1351
1352 BUG_ON(!sb);
1353 osb = OCFS2_SB(sb);
1354 BUG_ON(!osb);
1355
1356 ocfs2_shutdown_local_alloc(osb);
1357
1358 ocfs2_truncate_log_shutdown(osb);
1359
553abd04
JB
1360 /* This will disable recovery and flush any recovery work. */
1361 ocfs2_recovery_exit(osb);
ccd979bd
MF
1362
1363 ocfs2_journal_shutdown(osb);
1364
1365 ocfs2_sync_blockdev(sb);
1366
4670c46d
JB
1367 /* No cluster connection means we've failed during mount, so skip
1368 * all the steps which depended on that to complete. */
1369 if (osb->cconn) {
ccd979bd
MF
1370 tmp = ocfs2_super_lock(osb, 1);
1371 if (tmp < 0) {
1372 mlog_errno(tmp);
1373 return;
1374 }
19b613d4 1375 }
ccd979bd 1376
19b613d4
MF
1377 if (osb->slot_num != OCFS2_INVALID_SLOT)
1378 ocfs2_put_slot(osb);
ccd979bd 1379
4670c46d 1380 if (osb->cconn)
ccd979bd 1381 ocfs2_super_unlock(osb, 1);
ccd979bd
MF
1382
1383 ocfs2_release_system_inodes(osb);
1384
6953b4c0 1385 /*
6953b4c0
JB
1386 * If we're dismounting due to mount error, mount.ocfs2 will clean
1387 * up heartbeat. If we're a local mount, there is no heartbeat.
1388 * If we failed before we got a uuid_str yet, we can't stop
1389 * heartbeat. Otherwise, do it.
1390 */
1391 if (!mnt_err && !ocfs2_mount_local(osb) && osb->uuid_str)
286eaa95
JB
1392 hangup_needed = 1;
1393
1394 if (osb->cconn)
1395 ocfs2_dlm_shutdown(osb, hangup_needed);
1396
1397 debugfs_remove(osb->osb_debug_root);
1398
1399 if (hangup_needed)
6953b4c0 1400 ocfs2_cluster_hangup(osb->uuid_str, strlen(osb->uuid_str));
ccd979bd
MF
1401
1402 atomic_set(&osb->vol_state, VOLUME_DISMOUNTED);
1403
c271c5c2
SM
1404 if (ocfs2_mount_local(osb))
1405 snprintf(nodestr, sizeof(nodestr), "local");
1406 else
19fdb624 1407 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
c271c5c2
SM
1408
1409 printk(KERN_INFO "ocfs2: Unmounting device (%s) on (node %s)\n",
1410 osb->dev_str, nodestr);
ccd979bd
MF
1411
1412 ocfs2_delete_osb(osb);
1413 kfree(osb);
1414 sb->s_dev = 0;
1415 sb->s_fs_info = NULL;
1416}
1417
1418static int ocfs2_setup_osb_uuid(struct ocfs2_super *osb, const unsigned char *uuid,
1419 unsigned uuid_bytes)
1420{
1421 int i, ret;
1422 char *ptr;
1423
1424 BUG_ON(uuid_bytes != OCFS2_VOL_UUID_LEN);
1425
cd861280 1426 osb->uuid_str = kzalloc(OCFS2_VOL_UUID_LEN * 2 + 1, GFP_KERNEL);
ccd979bd
MF
1427 if (osb->uuid_str == NULL)
1428 return -ENOMEM;
1429
ccd979bd
MF
1430 for (i = 0, ptr = osb->uuid_str; i < OCFS2_VOL_UUID_LEN; i++) {
1431 /* print with null */
1432 ret = snprintf(ptr, 3, "%02X", uuid[i]);
1433 if (ret != 2) /* drop super cleans up */
1434 return -EINVAL;
1435 /* then only advance past the last char */
1436 ptr += 2;
1437 }
1438
1439 return 0;
1440}
1441
1442static int ocfs2_initialize_super(struct super_block *sb,
1443 struct buffer_head *bh,
1444 int sector_size)
1445{
bddb8eb3 1446 int status;
5a254031
MF
1447 int i, cbits, bbits;
1448 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
ccd979bd 1449 struct inode *inode = NULL;
ccd979bd
MF
1450 struct ocfs2_journal *journal;
1451 __le32 uuid_net_key;
1452 struct ocfs2_super *osb;
1453
1454 mlog_entry_void();
1455
cd861280 1456 osb = kzalloc(sizeof(struct ocfs2_super), GFP_KERNEL);
ccd979bd
MF
1457 if (!osb) {
1458 status = -ENOMEM;
1459 mlog_errno(status);
1460 goto bail;
1461 }
1462
1463 sb->s_fs_info = osb;
1464 sb->s_op = &ocfs2_sops;
1465 sb->s_export_op = &ocfs2_export_ops;
cf1d6c76 1466 sb->s_xattr = ocfs2_xattr_handlers;
e0dceaf0 1467 sb->s_time_gran = 1;
ccd979bd
MF
1468 sb->s_flags |= MS_NOATIME;
1469 /* this is needed to support O_LARGEFILE */
5a254031
MF
1470 cbits = le32_to_cpu(di->id2.i_super.s_clustersize_bits);
1471 bbits = le32_to_cpu(di->id2.i_super.s_blocksize_bits);
1472 sb->s_maxbytes = ocfs2_max_file_offset(bbits, cbits);
ccd979bd
MF
1473
1474 osb->sb = sb;
1475 /* Save off for ocfs2_rw_direct */
1476 osb->s_sectsize_bits = blksize_bits(sector_size);
ebdec83b 1477 BUG_ON(!osb->s_sectsize_bits);
ccd979bd 1478
34d024f8
MF
1479 spin_lock_init(&osb->dc_task_lock);
1480 init_waitqueue_head(&osb->dc_event);
1481 osb->dc_work_sequence = 0;
1482 osb->dc_wake_sequence = 0;
ccd979bd
MF
1483 INIT_LIST_HEAD(&osb->blocked_lock_list);
1484 osb->blocked_lock_count = 0;
ccd979bd 1485 spin_lock_init(&osb->osb_lock);
4d0ddb2c 1486 ocfs2_init_inode_steal_slot(osb);
ccd979bd
MF
1487
1488 atomic_set(&osb->alloc_stats.moves, 0);
1489 atomic_set(&osb->alloc_stats.local_data, 0);
1490 atomic_set(&osb->alloc_stats.bitmap_data, 0);
1491 atomic_set(&osb->alloc_stats.bg_allocs, 0);
1492 atomic_set(&osb->alloc_stats.bg_extends, 0);
1493
1494 ocfs2_init_node_maps(osb);
1495
1496 snprintf(osb->dev_str, sizeof(osb->dev_str), "%u,%u",
1497 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1498
553abd04
JB
1499 status = ocfs2_recovery_init(osb);
1500 if (status) {
1501 mlog(ML_ERROR, "Unable to initialize recovery state\n");
1502 mlog_errno(status);
1503 goto bail;
1504 }
ccd979bd
MF
1505
1506 init_waitqueue_head(&osb->checkpoint_event);
1507 atomic_set(&osb->needs_checkpoint, 0);
1508
7f1a37e3
TY
1509 osb->s_atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
1510
ccd979bd
MF
1511 osb->slot_num = OCFS2_INVALID_SLOT;
1512
8154da3d
TY
1513 osb->s_xattr_inline_size = le16_to_cpu(
1514 di->id2.i_super.s_xattr_inline_size);
fdd77704 1515
ccd979bd
MF
1516 osb->local_alloc_state = OCFS2_LA_UNUSED;
1517 osb->local_alloc_bh = NULL;
9c7af40b 1518 INIT_DELAYED_WORK(&osb->la_enable_wq, ocfs2_la_enable_worker);
ccd979bd 1519
ccd979bd
MF
1520 init_waitqueue_head(&osb->osb_mount_event);
1521
1522 osb->vol_label = kmalloc(OCFS2_MAX_VOL_LABEL_LEN, GFP_KERNEL);
1523 if (!osb->vol_label) {
1524 mlog(ML_ERROR, "unable to alloc vol label\n");
1525 status = -ENOMEM;
1526 goto bail;
1527 }
1528
ccd979bd
MF
1529 osb->max_slots = le16_to_cpu(di->id2.i_super.s_max_slots);
1530 if (osb->max_slots > OCFS2_MAX_SLOTS || osb->max_slots == 0) {
1531 mlog(ML_ERROR, "Invalid number of node slots (%u)\n",
1532 osb->max_slots);
1533 status = -EINVAL;
1534 goto bail;
1535 }
781ee3e2 1536 mlog(0, "max_slots for this device: %u\n", osb->max_slots);
ccd979bd 1537
539d8264
SM
1538 osb->slot_recovery_generations =
1539 kcalloc(osb->max_slots, sizeof(*osb->slot_recovery_generations),
1540 GFP_KERNEL);
1541 if (!osb->slot_recovery_generations) {
1542 status = -ENOMEM;
1543 mlog_errno(status);
1544 goto bail;
1545 }
1546
b4df6ed8
MF
1547 init_waitqueue_head(&osb->osb_wipe_event);
1548 osb->osb_orphan_wipes = kcalloc(osb->max_slots,
1549 sizeof(*osb->osb_orphan_wipes),
1550 GFP_KERNEL);
1551 if (!osb->osb_orphan_wipes) {
1552 status = -ENOMEM;
1553 mlog_errno(status);
1554 goto bail;
1555 }
1556
ccd979bd
MF
1557 osb->s_feature_compat =
1558 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_compat);
1559 osb->s_feature_ro_compat =
1560 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_ro_compat);
1561 osb->s_feature_incompat =
1562 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_incompat);
1563
1564 if ((i = OCFS2_HAS_INCOMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_INCOMPAT_SUPP))) {
1565 mlog(ML_ERROR, "couldn't mount because of unsupported "
1566 "optional features (%x).\n", i);
1567 status = -EINVAL;
1568 goto bail;
1569 }
1570 if (!(osb->sb->s_flags & MS_RDONLY) &&
1571 (i = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP))) {
1572 mlog(ML_ERROR, "couldn't mount RDWR because of "
1573 "unsupported optional features (%x).\n", i);
1574 status = -EINVAL;
1575 goto bail;
1576 }
1577
b61817e1
JB
1578 if (ocfs2_userspace_stack(osb)) {
1579 memcpy(osb->osb_cluster_stack,
1580 OCFS2_RAW_SB(di)->s_cluster_info.ci_stack,
1581 OCFS2_STACK_LABEL_LEN);
1582 osb->osb_cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
1583 if (strlen(osb->osb_cluster_stack) != OCFS2_STACK_LABEL_LEN) {
1584 mlog(ML_ERROR,
1585 "couldn't mount because of an invalid "
1586 "cluster stack label (%s) \n",
1587 osb->osb_cluster_stack);
1588 status = -EINVAL;
1589 goto bail;
1590 }
1591 } else {
1592 /* The empty string is identical with classic tools that
1593 * don't know about s_cluster_info. */
1594 osb->osb_cluster_stack[0] = '\0';
1595 }
1596
ccd979bd
MF
1597 get_random_bytes(&osb->s_next_generation, sizeof(u32));
1598
1599 /* FIXME
1600 * This should be done in ocfs2_journal_init(), but unknown
1601 * ordering issues will cause the filesystem to crash.
1602 * If anyone wants to figure out what part of the code
1603 * refers to osb->journal before ocfs2_journal_init() is run,
1604 * be my guest.
1605 */
1606 /* initialize our journal structure */
1607
cd861280 1608 journal = kzalloc(sizeof(struct ocfs2_journal), GFP_KERNEL);
ccd979bd
MF
1609 if (!journal) {
1610 mlog(ML_ERROR, "unable to alloc journal\n");
1611 status = -ENOMEM;
1612 goto bail;
1613 }
1614 osb->journal = journal;
1615 journal->j_osb = osb;
1616
1617 atomic_set(&journal->j_num_trans, 0);
1618 init_rwsem(&journal->j_trans_barrier);
1619 init_waitqueue_head(&journal->j_checkpointed);
1620 spin_lock_init(&journal->j_lock);
1621 journal->j_trans_id = (unsigned long) 1;
1622 INIT_LIST_HEAD(&journal->j_la_cleanups);
c4028958 1623 INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
ccd979bd
MF
1624 journal->j_state = OCFS2_JOURNAL_FREE;
1625
1626 /* get some pseudo constants for clustersize bits */
1627 osb->s_clustersize_bits =
1628 le32_to_cpu(di->id2.i_super.s_clustersize_bits);
1629 osb->s_clustersize = 1 << osb->s_clustersize_bits;
1630 mlog(0, "clusterbits=%d\n", osb->s_clustersize_bits);
1631
1632 if (osb->s_clustersize < OCFS2_MIN_CLUSTERSIZE ||
1633 osb->s_clustersize > OCFS2_MAX_CLUSTERSIZE) {
1634 mlog(ML_ERROR, "Volume has invalid cluster size (%d)\n",
1635 osb->s_clustersize);
1636 status = -EINVAL;
1637 goto bail;
1638 }
1639
1640 if (ocfs2_clusters_to_blocks(osb->sb, le32_to_cpu(di->i_clusters) - 1)
1641 > (u32)~0UL) {
1642 mlog(ML_ERROR, "Volume might try to write to blocks beyond "
1643 "what jbd can address in 32 bits.\n");
1644 status = -EINVAL;
1645 goto bail;
1646 }
1647
1648 if (ocfs2_setup_osb_uuid(osb, di->id2.i_super.s_uuid,
1649 sizeof(di->id2.i_super.s_uuid))) {
1650 mlog(ML_ERROR, "Out of memory trying to setup our uuid.\n");
1651 status = -ENOMEM;
1652 goto bail;
1653 }
1654
78427043 1655 memcpy(&uuid_net_key, di->id2.i_super.s_uuid, sizeof(uuid_net_key));
ccd979bd
MF
1656
1657 strncpy(osb->vol_label, di->id2.i_super.s_label, 63);
1658 osb->vol_label[63] = '\0';
1659 osb->root_blkno = le64_to_cpu(di->id2.i_super.s_root_blkno);
1660 osb->system_dir_blkno = le64_to_cpu(di->id2.i_super.s_system_dir_blkno);
1661 osb->first_cluster_group_blkno =
1662 le64_to_cpu(di->id2.i_super.s_first_cluster_group);
1663 osb->fs_generation = le32_to_cpu(di->i_fs_generation);
cf1d6c76 1664 osb->uuid_hash = le32_to_cpu(di->id2.i_super.s_uuid_hash);
ccd979bd
MF
1665 mlog(0, "vol_label: %s\n", osb->vol_label);
1666 mlog(0, "uuid: %s\n", osb->uuid_str);
b0697053
MF
1667 mlog(0, "root_blkno=%llu, system_dir_blkno=%llu\n",
1668 (unsigned long long)osb->root_blkno,
1669 (unsigned long long)osb->system_dir_blkno);
ccd979bd
MF
1670
1671 osb->osb_dlm_debug = ocfs2_new_dlm_debug();
1672 if (!osb->osb_dlm_debug) {
1673 status = -ENOMEM;
1674 mlog_errno(status);
1675 goto bail;
1676 }
1677
1678 atomic_set(&osb->vol_state, VOLUME_INIT);
1679
1680 /* load root, system_dir, and all global system inodes */
1681 status = ocfs2_init_global_system_inodes(osb);
1682 if (status < 0) {
1683 mlog_errno(status);
1684 goto bail;
1685 }
1686
1687 /*
1688 * global bitmap
1689 */
1690 inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
1691 OCFS2_INVALID_SLOT);
1692 if (!inode) {
1693 status = -EINVAL;
1694 mlog_errno(status);
1695 goto bail;
1696 }
1697
1698 osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
ccd979bd 1699 iput(inode);
ccd979bd 1700
e9d578a8 1701 osb->bitmap_cpg = ocfs2_group_bitmap_size(sb) * 8;
ccd979bd
MF
1702
1703 status = ocfs2_init_slot_info(osb);
1704 if (status < 0) {
1705 mlog_errno(status);
1706 goto bail;
1707 }
1708
ccd979bd
MF
1709bail:
1710 mlog_exit(status);
1711 return status;
1712}
1713
1714/*
1715 * will return: -EAGAIN if it is ok to keep searching for superblocks
1716 * -EINVAL if there is a bad superblock
1717 * 0 on success
1718 */
1719static int ocfs2_verify_volume(struct ocfs2_dinode *di,
1720 struct buffer_head *bh,
1721 u32 blksz)
1722{
1723 int status = -EAGAIN;
1724
1725 mlog_entry_void();
1726
1727 if (memcmp(di->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
1728 strlen(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) {
1729 status = -EINVAL;
1730 if ((1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits)) != blksz) {
1731 mlog(ML_ERROR, "found superblock with incorrect block "
1732 "size: found %u, should be %u\n",
1733 1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits),
1734 blksz);
1735 } else if (le16_to_cpu(di->id2.i_super.s_major_rev_level) !=
1736 OCFS2_MAJOR_REV_LEVEL ||
1737 le16_to_cpu(di->id2.i_super.s_minor_rev_level) !=
1738 OCFS2_MINOR_REV_LEVEL) {
1739 mlog(ML_ERROR, "found superblock with bad version: "
1740 "found %u.%u, should be %u.%u\n",
1741 le16_to_cpu(di->id2.i_super.s_major_rev_level),
1742 le16_to_cpu(di->id2.i_super.s_minor_rev_level),
1743 OCFS2_MAJOR_REV_LEVEL,
1744 OCFS2_MINOR_REV_LEVEL);
1745 } else if (bh->b_blocknr != le64_to_cpu(di->i_blkno)) {
1746 mlog(ML_ERROR, "bad block number on superblock: "
b0697053 1747 "found %llu, should be %llu\n",
1ca1a111 1748 (unsigned long long)le64_to_cpu(di->i_blkno),
b0697053 1749 (unsigned long long)bh->b_blocknr);
ccd979bd
MF
1750 } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
1751 le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
1752 mlog(ML_ERROR, "bad cluster size found: %u\n",
1753 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
1754 } else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
1755 mlog(ML_ERROR, "bad root_blkno: 0\n");
1756 } else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
1757 mlog(ML_ERROR, "bad system_dir_blkno: 0\n");
1758 } else if (le16_to_cpu(di->id2.i_super.s_max_slots) > OCFS2_MAX_SLOTS) {
1759 mlog(ML_ERROR,
1760 "Superblock slots found greater than file system "
1761 "maximum: found %u, max %u\n",
1762 le16_to_cpu(di->id2.i_super.s_max_slots),
1763 OCFS2_MAX_SLOTS);
1764 } else {
1765 /* found it! */
1766 status = 0;
1767 }
1768 }
1769
1770 mlog_exit(status);
1771 return status;
1772}
1773
1774static int ocfs2_check_volume(struct ocfs2_super *osb)
1775{
bddb8eb3 1776 int status;
ccd979bd 1777 int dirty;
c271c5c2 1778 int local;
ccd979bd
MF
1779 struct ocfs2_dinode *local_alloc = NULL; /* only used if we
1780 * recover
1781 * ourselves. */
1782
1783 mlog_entry_void();
1784
1785 /* Init our journal object. */
1786 status = ocfs2_journal_init(osb->journal, &dirty);
1787 if (status < 0) {
1788 mlog(ML_ERROR, "Could not initialize journal!\n");
1789 goto finally;
1790 }
1791
1792 /* If the journal was unmounted cleanly then we don't want to
1793 * recover anything. Otherwise, journal_load will do that
1794 * dirty work for us :) */
1795 if (!dirty) {
1796 status = ocfs2_journal_wipe(osb->journal, 0);
1797 if (status < 0) {
1798 mlog_errno(status);
1799 goto finally;
1800 }
1801 } else {
1802 mlog(ML_NOTICE, "File system was not unmounted cleanly, "
1803 "recovering volume.\n");
1804 }
1805
c271c5c2
SM
1806 local = ocfs2_mount_local(osb);
1807
ccd979bd 1808 /* will play back anything left in the journal. */
539d8264 1809 status = ocfs2_journal_load(osb->journal, local, dirty);
01af4820
WW
1810 if (status < 0) {
1811 mlog(ML_ERROR, "ocfs2 journal load failed! %d\n", status);
1812 goto finally;
1813 }
ccd979bd
MF
1814
1815 if (dirty) {
1816 /* recover my local alloc if we didn't unmount cleanly. */
1817 status = ocfs2_begin_local_alloc_recovery(osb,
1818 osb->slot_num,
1819 &local_alloc);
1820 if (status < 0) {
1821 mlog_errno(status);
1822 goto finally;
1823 }
1824 /* we complete the recovery process after we've marked
1825 * ourselves as mounted. */
1826 }
1827
1828 mlog(0, "Journal loaded.\n");
1829
1830 status = ocfs2_load_local_alloc(osb);
1831 if (status < 0) {
1832 mlog_errno(status);
1833 goto finally;
1834 }
1835
1836 if (dirty) {
1837 /* Recovery will be completed after we've mounted the
1838 * rest of the volume. */
1839 osb->dirty = 1;
1840 osb->local_alloc_copy = local_alloc;
1841 local_alloc = NULL;
1842 }
1843
1844 /* go through each journal, trylock it and if you get the
1845 * lock, and it's marked as dirty, set the bit in the recover
1846 * map and launch a recovery thread for it. */
1847 status = ocfs2_mark_dead_nodes(osb);
1848 if (status < 0)
1849 mlog_errno(status);
1850
1851finally:
1852 if (local_alloc)
1853 kfree(local_alloc);
1854
1855 mlog_exit(status);
1856 return status;
1857}
1858
1859/*
1860 * The routine gets called from dismount or close whenever a dismount on
1861 * volume is requested and the osb open count becomes 1.
1862 * It will remove the osb from the global list and also free up all the
1863 * initialized resources and fileobject.
1864 */
1865static void ocfs2_delete_osb(struct ocfs2_super *osb)
1866{
1867 mlog_entry_void();
1868
1869 /* This function assumes that the caller has the main osb resource */
1870
8e8a4603 1871 ocfs2_free_slot_info(osb);
ccd979bd 1872
b4df6ed8 1873 kfree(osb->osb_orphan_wipes);
539d8264 1874 kfree(osb->slot_recovery_generations);
ccd979bd
MF
1875 /* FIXME
1876 * This belongs in journal shutdown, but because we have to
1877 * allocate osb->journal at the start of ocfs2_initalize_osb(),
1878 * we free it here.
1879 */
1880 kfree(osb->journal);
1881 if (osb->local_alloc_copy)
1882 kfree(osb->local_alloc_copy);
1883 kfree(osb->uuid_str);
1884 ocfs2_put_dlm_debug(osb->osb_dlm_debug);
1885 memset(osb, 0, sizeof(struct ocfs2_super));
1886
1887 mlog_exit_void();
1888}
1889
1890/* Put OCFS2 into a readonly state, or (if the user specifies it),
1891 * panic(). We do not support continue-on-error operation. */
1892static void ocfs2_handle_error(struct super_block *sb)
1893{
1894 struct ocfs2_super *osb = OCFS2_SB(sb);
1895
1896 if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_PANIC)
1897 panic("OCFS2: (device %s): panic forced after error\n",
1898 sb->s_id);
1899
1900 ocfs2_set_osb_flag(osb, OCFS2_OSB_ERROR_FS);
1901
1902 if (sb->s_flags & MS_RDONLY &&
1903 (ocfs2_is_soft_readonly(osb) ||
1904 ocfs2_is_hard_readonly(osb)))
1905 return;
1906
1907 printk(KERN_CRIT "File system is now read-only due to the potential "
1908 "of on-disk corruption. Please run fsck.ocfs2 once the file "
1909 "system is unmounted.\n");
1910 sb->s_flags |= MS_RDONLY;
1911 ocfs2_set_ro_flag(osb, 0);
1912}
1913
1914static char error_buf[1024];
1915
1916void __ocfs2_error(struct super_block *sb,
1917 const char *function,
1918 const char *fmt, ...)
1919{
1920 va_list args;
1921
1922 va_start(args, fmt);
4a6e617a 1923 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
ccd979bd
MF
1924 va_end(args);
1925
1926 /* Not using mlog here because we want to show the actual
1927 * function the error came from. */
1928 printk(KERN_CRIT "OCFS2: ERROR (device %s): %s: %s\n",
1929 sb->s_id, function, error_buf);
1930
1931 ocfs2_handle_error(sb);
1932}
1933
1934/* Handle critical errors. This is intentionally more drastic than
1935 * ocfs2_handle_error, so we only use for things like journal errors,
1936 * etc. */
1937void __ocfs2_abort(struct super_block* sb,
1938 const char *function,
1939 const char *fmt, ...)
1940{
1941 va_list args;
1942
1943 va_start(args, fmt);
4a6e617a 1944 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
ccd979bd
MF
1945 va_end(args);
1946
1947 printk(KERN_CRIT "OCFS2: abort (device %s): %s: %s\n",
1948 sb->s_id, function, error_buf);
1949
1950 /* We don't have the cluster support yet to go straight to
1951 * hard readonly in here. Until then, we want to keep
1952 * ocfs2_abort() so that we can at least mark critical
1953 * errors.
1954 *
1955 * TODO: This should abort the journal and alert other nodes
1956 * that our slot needs recovery. */
1957
1958 /* Force a panic(). This stinks, but it's better than letting
1959 * things continue without having a proper hard readonly
1960 * here. */
1961 OCFS2_SB(sb)->s_mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
1962 ocfs2_handle_error(sb);
1963}
1964
1965module_init(ocfs2_init);
1966module_exit(ocfs2_exit);