Root/fs/ocfs2/super.c

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

Archive Download this file



interactive