blob: 1d91dd1e8711c1c26a6295bb4aa07d2e2318d21f [file] [log] [blame]
Thomas Gleixner328970d2019-05-24 12:04:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mark Fashehccd979b2005-12-15 14:31:24 -08002/* -*- mode: c; c-basic-offset: 8; -*-
3 * vim: noexpandtab sw=8 ts=8 sts=0:
4 *
5 * super.c
6 *
7 * load/unload driver, mount/dismount volumes
8 *
9 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
Mark Fashehccd979b2005-12-15 14:31:24 -080010 */
11
12#include <linux/module.h>
13#include <linux/fs.h>
14#include <linux/types.h>
15#include <linux/slab.h>
16#include <linux/highmem.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080017#include <linux/init.h>
18#include <linux/random.h>
19#include <linux/statfs.h>
20#include <linux/moduleparam.h>
21#include <linux/blkdev.h>
22#include <linux/socket.h>
23#include <linux/inet.h>
24#include <linux/parser.h>
25#include <linux/crc32.h>
26#include <linux/debugfs.h>
Sunil Mushrand5500712007-09-06 13:34:16 -070027#include <linux/mount.h>
Joel Becker6953b4c2008-01-29 16:59:56 -080028#include <linux/seq_file.h>
Jan Kara19ece542008-08-21 20:13:17 +020029#include <linux/quotaops.h>
Dan Magenheimer1cfd8bd2011-05-26 10:02:08 -060030#include <linux/cleancache.h>
Ingo Molnarf361bf42017-02-03 23:47:37 +010031#include <linux/signal.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080032
Wengang Wang80a9a842011-02-21 11:13:14 +080033#define CREATE_TRACE_POINTS
34#include "ocfs2_trace.h"
35
Mark Fashehccd979b2005-12-15 14:31:24 -080036#include <cluster/masklog.h>
37
38#include "ocfs2.h"
39
40/* this should be the only file to include a version 1 header */
41#include "ocfs1_fs_compat.h"
42
43#include "alloc.h"
Mark Fasheha11f7e62011-06-22 14:23:38 -070044#include "aops.h"
Joel Beckerd030cc92008-12-11 15:04:14 -080045#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080046#include "dlmglue.h"
47#include "export.h"
48#include "extent_map.h"
49#include "heartbeat.h"
50#include "inode.h"
51#include "journal.h"
52#include "localalloc.h"
53#include "namei.h"
54#include "slot_map.h"
55#include "super.h"
56#include "sysfile.h"
57#include "uptodate.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080058#include "xattr.h"
Jan Kara9e33d692008-08-25 19:56:50 +020059#include "quota.h"
Tao Ma374a2632009-08-24 11:13:37 +080060#include "refcounttree.h"
Tiger Yangb89c5422010-01-25 14:11:06 +080061#include "suballoc.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080062
63#include "buffer_head_io.h"
Gang Hea849d462016-03-22 14:24:27 -070064#include "filecheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080065
Fabian Frederick1a5c4e22014-06-04 16:06:06 -070066static struct kmem_cache *ocfs2_inode_cachep;
Jan Kara9e33d692008-08-25 19:56:50 +020067struct kmem_cache *ocfs2_dquot_cachep;
68struct kmem_cache *ocfs2_qf_chunk_cachep;
Mark Fashehccd979b2005-12-15 14:31:24 -080069
Fabian Frederick1a5c4e22014-06-04 16:06:06 -070070static struct dentry *ocfs2_debugfs_root;
Mark Fashehccd979b2005-12-15 14:31:24 -080071
72MODULE_AUTHOR("Oracle");
73MODULE_LICENSE("GPL");
Goldwyn Rodriguesff8fb332014-01-21 15:48:20 -080074MODULE_DESCRIPTION("OCFS2 cluster file system");
Mark Fashehccd979b2005-12-15 14:31:24 -080075
Tiger Yangc0123ad2007-09-08 00:16:10 +080076struct mount_options
77{
Mark Fashehd147b3d2007-11-07 14:40:36 -080078 unsigned long commit_interval;
Tiger Yangc0123ad2007-09-08 00:16:10 +080079 unsigned long mount_opt;
80 unsigned int atime_quantum;
Junxiao Bi38d51b22020-08-06 23:18:02 -070081 unsigned short slot;
Mark Fasheh73c8a802010-04-05 18:17:13 -070082 int localalloc_opt;
Mark Fashehd02f00c2009-12-07 13:10:48 -080083 unsigned int resv_level;
Mark Fasheh83f92312010-04-05 18:17:16 -070084 int dir_resv_level;
Joel Beckerb61817e2008-02-01 15:08:23 -080085 char cluster_stack[OCFS2_STACK_LABEL_LEN + 1];
Tiger Yangc0123ad2007-09-08 00:16:10 +080086};
87
Mark Fashehccd979b2005-12-15 14:31:24 -080088static int ocfs2_parse_options(struct super_block *sb, char *options,
Tiger Yangc0123ad2007-09-08 00:16:10 +080089 struct mount_options *mopt,
Sunil Mushranbaf46612007-06-18 17:00:24 -070090 int is_remount);
Jan Kara5297aad2009-10-15 14:54:04 +020091static int ocfs2_check_set_options(struct super_block *sb,
92 struct mount_options *options);
Al Viro34c80b12011-12-08 21:32:45 -050093static int ocfs2_show_options(struct seq_file *s, struct dentry *root);
Mark Fashehccd979b2005-12-15 14:31:24 -080094static void ocfs2_put_super(struct super_block *sb);
95static int ocfs2_mount_volume(struct super_block *sb);
96static int ocfs2_remount(struct super_block *sb, int *flags, char *data);
97static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err);
98static int ocfs2_initialize_mem_caches(void);
99static void ocfs2_free_mem_caches(void);
100static void ocfs2_delete_osb(struct ocfs2_super *osb);
101
David Howells726c3342006-06-23 02:02:58 -0700102static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf);
Mark Fashehccd979b2005-12-15 14:31:24 -0800103
104static int ocfs2_sync_fs(struct super_block *sb, int wait);
105
106static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb);
107static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb);
Denis Chengbddb8eb32007-09-27 02:10:04 +0800108static void ocfs2_release_system_inodes(struct ocfs2_super *osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800109static int ocfs2_check_volume(struct ocfs2_super *osb);
110static int ocfs2_verify_volume(struct ocfs2_dinode *di,
111 struct buffer_head *bh,
Joel Becker73be1922009-01-06 14:57:08 -0800112 u32 sectsize,
113 struct ocfs2_blockcheck_stats *stats);
Mark Fashehccd979b2005-12-15 14:31:24 -0800114static int ocfs2_initialize_super(struct super_block *sb,
115 struct buffer_head *bh,
Joel Becker73be1922009-01-06 14:57:08 -0800116 int sector_size,
117 struct ocfs2_blockcheck_stats *stats);
Mark Fashehccd979b2005-12-15 14:31:24 -0800118static int ocfs2_get_sector(struct super_block *sb,
119 struct buffer_head **bh,
120 int block,
121 int sect_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800122static struct inode *ocfs2_alloc_inode(struct super_block *sb);
Al Viroe91b9192019-04-15 20:26:42 -0400123static void ocfs2_free_inode(struct inode *inode);
Jan Kara19ece542008-08-21 20:13:17 +0200124static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend);
125static int ocfs2_enable_quotas(struct ocfs2_super *osb);
126static void ocfs2_disable_quotas(struct ocfs2_super *osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800127
Jan Kara1c92ec62014-09-29 15:02:51 +0200128static struct dquot **ocfs2_get_dquots(struct inode *inode)
129{
130 return OCFS2_I(inode)->i_dquot;
131}
132
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800133static const struct super_operations ocfs2_sops = {
Mark Fashehccd979b2005-12-15 14:31:24 -0800134 .statfs = ocfs2_statfs,
135 .alloc_inode = ocfs2_alloc_inode,
Al Viroe91b9192019-04-15 20:26:42 -0400136 .free_inode = ocfs2_free_inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800137 .drop_inode = ocfs2_drop_inode,
Al Viro066d92d2010-06-08 21:28:10 -0400138 .evict_inode = ocfs2_evict_inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800139 .sync_fs = ocfs2_sync_fs,
Mark Fashehccd979b2005-12-15 14:31:24 -0800140 .put_super = ocfs2_put_super,
141 .remount_fs = ocfs2_remount,
Sunil Mushrand5500712007-09-06 13:34:16 -0700142 .show_options = ocfs2_show_options,
Jan Kara9e33d692008-08-25 19:56:50 +0200143 .quota_read = ocfs2_quota_read,
144 .quota_write = ocfs2_quota_write,
Jan Kara1c92ec62014-09-29 15:02:51 +0200145 .get_dquots = ocfs2_get_dquots,
Mark Fashehccd979b2005-12-15 14:31:24 -0800146};
147
148enum {
149 Opt_barrier,
150 Opt_err_panic,
151 Opt_err_ro,
152 Opt_intr,
153 Opt_nointr,
154 Opt_hb_none,
155 Opt_hb_local,
Sunil Mushran2c442712010-10-07 15:23:50 -0700156 Opt_hb_global,
Mark Fashehccd979b2005-12-15 14:31:24 -0800157 Opt_data_ordered,
158 Opt_data_writeback,
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800159 Opt_atime_quantum,
Sunil Mushranbaf46612007-06-18 17:00:24 -0700160 Opt_slot,
Mark Fashehd147b3d2007-11-07 14:40:36 -0800161 Opt_commit,
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800162 Opt_localalloc,
Mark Fasheh53fc6222007-12-20 16:49:04 -0800163 Opt_localflocks,
Joel Beckerb61817e2008-02-01 15:08:23 -0800164 Opt_stack,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800165 Opt_user_xattr,
166 Opt_nouser_xattr,
Joel Becker12462f12008-09-03 20:03:40 -0700167 Opt_inode64,
Tiger Yanga68979b2008-11-14 11:17:52 +0800168 Opt_acl,
169 Opt_noacl,
Jan Kara19ece542008-08-21 20:13:17 +0200170 Opt_usrquota,
171 Opt_grpquota,
Tristan Ye7bdb0d12010-10-11 16:46:39 +0800172 Opt_coherency_buffered,
173 Opt_coherency_full,
Mark Fashehd02f00c2009-12-07 13:10:48 -0800174 Opt_resv_level,
Mark Fasheh83f92312010-04-05 18:17:16 -0700175 Opt_dir_resv_level,
alex chen1dfeb762015-02-10 14:09:04 -0800176 Opt_journal_async_commit,
Goldwyn Rodrigues7d0fb912015-09-04 15:44:11 -0700177 Opt_err_cont,
Gang He912f6552020-06-01 21:45:29 -0700178 Opt_nocluster,
Mark Fashehccd979b2005-12-15 14:31:24 -0800179 Opt_err,
180};
181
Steven Whitehousea447c092008-10-13 10:46:57 +0100182static const match_table_t tokens = {
Mark Fashehccd979b2005-12-15 14:31:24 -0800183 {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},
Sunil Mushran2c442712010-10-07 15:23:50 -0700190 {Opt_hb_global, OCFS2_HB_GLOBAL},
Mark Fashehccd979b2005-12-15 14:31:24 -0800191 {Opt_data_ordered, "data=ordered"},
192 {Opt_data_writeback, "data=writeback"},
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800193 {Opt_atime_quantum, "atime_quantum=%u"},
Sunil Mushranbaf46612007-06-18 17:00:24 -0700194 {Opt_slot, "preferred_slot=%u"},
Mark Fashehd147b3d2007-11-07 14:40:36 -0800195 {Opt_commit, "commit=%u"},
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800196 {Opt_localalloc, "localalloc=%d"},
Mark Fasheh53fc6222007-12-20 16:49:04 -0800197 {Opt_localflocks, "localflocks"},
Joel Beckerb61817e2008-02-01 15:08:23 -0800198 {Opt_stack, "cluster_stack=%s"},
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800199 {Opt_user_xattr, "user_xattr"},
200 {Opt_nouser_xattr, "nouser_xattr"},
Joel Becker12462f12008-09-03 20:03:40 -0700201 {Opt_inode64, "inode64"},
Tiger Yanga68979b2008-11-14 11:17:52 +0800202 {Opt_acl, "acl"},
203 {Opt_noacl, "noacl"},
Jan Kara19ece542008-08-21 20:13:17 +0200204 {Opt_usrquota, "usrquota"},
205 {Opt_grpquota, "grpquota"},
Tristan Ye7bdb0d12010-10-11 16:46:39 +0800206 {Opt_coherency_buffered, "coherency=buffered"},
207 {Opt_coherency_full, "coherency=full"},
Mark Fashehd02f00c2009-12-07 13:10:48 -0800208 {Opt_resv_level, "resv_level=%u"},
Mark Fasheh83f92312010-04-05 18:17:16 -0700209 {Opt_dir_resv_level, "dir_resv_level=%u"},
alex chen1dfeb762015-02-10 14:09:04 -0800210 {Opt_journal_async_commit, "journal_async_commit"},
Goldwyn Rodrigues7d0fb912015-09-04 15:44:11 -0700211 {Opt_err_cont, "errors=continue"},
Gang He912f6552020-06-01 21:45:29 -0700212 {Opt_nocluster, "nocluster"},
Mark Fashehccd979b2005-12-15 14:31:24 -0800213 {Opt_err, NULL}
214};
215
Sunil Mushran50397502008-12-17 14:17:43 -0800216#ifdef CONFIG_DEBUG_FS
217static int ocfs2_osb_dump(struct ocfs2_super *osb, char *buf, int len)
218{
Sunil Mushran50397502008-12-17 14:17:43 -0800219 struct ocfs2_cluster_connection *cconn = osb->cconn;
220 struct ocfs2_recovery_map *rm = osb->recovery_map;
Sunil Mushrandf152c22009-06-22 11:40:07 -0700221 struct ocfs2_orphan_scan *os = &osb->osb_orphan_scan;
222 int i, out = 0;
jiangyiwenbfd97a02016-03-15 14:53:01 -0700223 unsigned long flags;
Sunil Mushran50397502008-12-17 14:17:43 -0800224
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700225 out += scnprintf(buf + out, len - out,
Sunil Mushran50397502008-12-17 14:17:43 -0800226 "%10s => Id: %-s Uuid: %-s Gen: 0x%X Label: %-s\n",
227 "Device", osb->dev_str, osb->uuid_str,
228 osb->fs_generation, osb->vol_label);
229
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700230 out += scnprintf(buf + out, len - out,
Sunil Mushran50397502008-12-17 14:17:43 -0800231 "%10s => State: %d Flags: 0x%lX\n", "Volume",
232 atomic_read(&osb->vol_state), osb->osb_flags);
233
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700234 out += scnprintf(buf + out, len - out,
Sunil Mushran50397502008-12-17 14:17:43 -0800235 "%10s => Block: %lu Cluster: %d\n", "Sizes",
236 osb->sb->s_blocksize, osb->s_clustersize);
237
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700238 out += scnprintf(buf + out, len - out,
Sunil Mushran50397502008-12-17 14:17:43 -0800239 "%10s => Compat: 0x%X Incompat: 0x%X "
240 "ROcompat: 0x%X\n",
241 "Features", osb->s_feature_compat,
242 osb->s_feature_incompat, osb->s_feature_ro_compat);
243
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700244 out += scnprintf(buf + out, len - out,
Sunil Mushran50397502008-12-17 14:17:43 -0800245 "%10s => Opts: 0x%lX AtimeQuanta: %u\n", "Mount",
246 osb->s_mount_opt, osb->s_atime_quantum);
247
Sunil Mushranc3d38842009-06-19 14:45:55 -0700248 if (cconn) {
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700249 out += scnprintf(buf + out, len - out,
Sunil Mushranc3d38842009-06-19 14:45:55 -0700250 "%10s => Stack: %s Name: %*s "
251 "Version: %d.%d\n", "Cluster",
252 (*osb->osb_cluster_stack == '\0' ?
253 "o2cb" : osb->osb_cluster_stack),
254 cconn->cc_namelen, cconn->cc_name,
255 cconn->cc_version.pv_major,
256 cconn->cc_version.pv_minor);
257 }
Sunil Mushran50397502008-12-17 14:17:43 -0800258
jiangyiwenbfd97a02016-03-15 14:53:01 -0700259 spin_lock_irqsave(&osb->dc_task_lock, flags);
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700260 out += scnprintf(buf + out, len - out,
Sunil Mushran50397502008-12-17 14:17:43 -0800261 "%10s => Pid: %d Count: %lu WakeSeq: %lu "
262 "WorkSeq: %lu\n", "DownCnvt",
Sunil Mushranc3d38842009-06-19 14:45:55 -0700263 (osb->dc_task ? task_pid_nr(osb->dc_task) : -1),
264 osb->blocked_lock_count, osb->dc_wake_sequence,
265 osb->dc_work_sequence);
jiangyiwenbfd97a02016-03-15 14:53:01 -0700266 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
Sunil Mushran50397502008-12-17 14:17:43 -0800267
268 spin_lock(&osb->osb_lock);
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700269 out += scnprintf(buf + out, len - out, "%10s => Pid: %d Nodes:",
Sunil Mushran50397502008-12-17 14:17:43 -0800270 "Recovery",
271 (osb->recovery_thread_task ?
272 task_pid_nr(osb->recovery_thread_task) : -1));
273 if (rm->rm_used == 0)
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700274 out += scnprintf(buf + out, len - out, " None\n");
Sunil Mushran50397502008-12-17 14:17:43 -0800275 else {
276 for (i = 0; i < rm->rm_used; i++)
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700277 out += scnprintf(buf + out, len - out, " %d",
Sunil Mushran50397502008-12-17 14:17:43 -0800278 rm->rm_entries[i]);
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700279 out += scnprintf(buf + out, len - out, "\n");
Sunil Mushran50397502008-12-17 14:17:43 -0800280 }
281 spin_unlock(&osb->osb_lock);
282
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700283 out += scnprintf(buf + out, len - out,
Goldwyn Rodrigues8fa9d172013-07-03 15:00:51 -0700284 "%10s => Pid: %d Interval: %lu\n", "Commit",
Sunil Mushranc3d38842009-06-19 14:45:55 -0700285 (osb->commit_task ? task_pid_nr(osb->commit_task) : -1),
Goldwyn Rodrigues8fa9d172013-07-03 15:00:51 -0700286 osb->osb_commit_interval);
Sunil Mushran50397502008-12-17 14:17:43 -0800287
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700288 out += scnprintf(buf + out, len - out,
Sunil Mushranc3d38842009-06-19 14:45:55 -0700289 "%10s => State: %d TxnId: %lu NumTxns: %d\n",
Sunil Mushran50397502008-12-17 14:17:43 -0800290 "Journal", osb->journal->j_state,
Sunil Mushranc3d38842009-06-19 14:45:55 -0700291 osb->journal->j_trans_id,
292 atomic_read(&osb->journal->j_num_trans));
Sunil Mushran50397502008-12-17 14:17:43 -0800293
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700294 out += scnprintf(buf + out, len - out,
Sunil Mushran50397502008-12-17 14:17:43 -0800295 "%10s => GlobalAllocs: %d LocalAllocs: %d "
296 "SubAllocs: %d LAWinMoves: %d SAExtends: %d\n",
297 "Stats",
298 atomic_read(&osb->alloc_stats.bitmap_data),
299 atomic_read(&osb->alloc_stats.local_data),
300 atomic_read(&osb->alloc_stats.bg_allocs),
301 atomic_read(&osb->alloc_stats.moves),
302 atomic_read(&osb->alloc_stats.bg_extends));
303
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700304 out += scnprintf(buf + out, len - out,
Sunil Mushran50397502008-12-17 14:17:43 -0800305 "%10s => State: %u Descriptor: %llu Size: %u bits "
306 "Default: %u bits\n",
307 "LocalAlloc", osb->local_alloc_state,
308 (unsigned long long)osb->la_last_gd,
309 osb->local_alloc_bits, osb->local_alloc_default_bits);
310
311 spin_lock(&osb->osb_lock);
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700312 out += scnprintf(buf + out, len - out,
Tiger Yangb89c5422010-01-25 14:11:06 +0800313 "%10s => InodeSlot: %d StolenInodes: %d, "
314 "MetaSlot: %d StolenMeta: %d\n", "Steal",
Sunil Mushran50397502008-12-17 14:17:43 -0800315 osb->s_inode_steal_slot,
Tiger Yangb89c5422010-01-25 14:11:06 +0800316 atomic_read(&osb->s_num_inodes_stolen),
317 osb->s_meta_steal_slot,
318 atomic_read(&osb->s_num_meta_stolen));
Sunil Mushran50397502008-12-17 14:17:43 -0800319 spin_unlock(&osb->osb_lock);
320
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700321 out += scnprintf(buf + out, len - out, "OrphanScan => ");
322 out += scnprintf(buf + out, len - out, "Local: %u Global: %u ",
Sunil Mushrandf152c22009-06-22 11:40:07 -0700323 os->os_count, os->os_seqno);
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700324 out += scnprintf(buf + out, len - out, " Last Scan: ");
Sunil Mushrandf152c22009-06-22 11:40:07 -0700325 if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE)
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700326 out += scnprintf(buf + out, len - out, "Disabled\n");
Sunil Mushrandf152c22009-06-22 11:40:07 -0700327 else
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700328 out += scnprintf(buf + out, len - out, "%lu seconds ago\n",
Deepa Dinamani395627b2016-12-12 16:41:23 -0800329 (unsigned long)(ktime_get_seconds() - os->os_scantime));
Sunil Mushrandf152c22009-06-22 11:40:07 -0700330
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700331 out += scnprintf(buf + out, len - out, "%10s => %3s %10s\n",
Sunil Mushran50397502008-12-17 14:17:43 -0800332 "Slots", "Num", "RecoGen");
Sunil Mushran50397502008-12-17 14:17:43 -0800333 for (i = 0; i < osb->max_slots; ++i) {
Takashi Iwaid293d3a2020-04-01 21:04:05 -0700334 out += scnprintf(buf + out, len - out,
Sunil Mushran50397502008-12-17 14:17:43 -0800335 "%10s %c %3d %10d\n",
336 " ",
337 (i == osb->slot_num ? '*' : ' '),
338 i, osb->slot_recovery_generations[i]);
339 }
340
341 return out;
342}
343
344static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
345{
346 struct ocfs2_super *osb = inode->i_private;
347 char *buf = NULL;
348
349 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
350 if (!buf)
351 goto bail;
352
353 i_size_write(inode, ocfs2_osb_dump(osb, buf, PAGE_SIZE));
354
355 file->private_data = buf;
356
357 return 0;
358bail:
359 return -ENOMEM;
360}
361
362static int ocfs2_debug_release(struct inode *inode, struct file *file)
363{
364 kfree(file->private_data);
365 return 0;
366}
367
368static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
369 size_t nbytes, loff_t *ppos)
370{
371 return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
372 i_size_read(file->f_mapping->host));
373}
374#else
375static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
376{
377 return 0;
378}
379static int ocfs2_debug_release(struct inode *inode, struct file *file)
380{
381 return 0;
382}
383static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
384 size_t nbytes, loff_t *ppos)
385{
386 return 0;
387}
388#endif /* CONFIG_DEBUG_FS */
389
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700390static const struct file_operations ocfs2_osb_debug_fops = {
Sunil Mushran50397502008-12-17 14:17:43 -0800391 .open = ocfs2_osb_debug_open,
392 .release = ocfs2_debug_release,
393 .read = ocfs2_debug_read,
394 .llseek = generic_file_llseek,
395};
396
Mark Fashehccd979b2005-12-15 14:31:24 -0800397static int ocfs2_sync_fs(struct super_block *sb, int wait)
398{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800399 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -0800400 tid_t target;
401 struct ocfs2_super *osb = OCFS2_SB(sb);
402
Mark Fashehccd979b2005-12-15 14:31:24 -0800403 if (ocfs2_is_hard_readonly(osb))
404 return -EROFS;
405
406 if (wait) {
407 status = ocfs2_flush_truncate_log(osb);
408 if (status < 0)
409 mlog_errno(status);
410 } else {
411 ocfs2_schedule_truncate_log_flush(osb, 0);
412 }
413
piaojun1119d3c2018-04-05 16:18:33 -0700414 if (jbd2_journal_start_commit(osb->journal->j_journal,
Joel Becker2b4e30f2008-09-03 20:03:41 -0700415 &target)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800416 if (wait)
piaojun1119d3c2018-04-05 16:18:33 -0700417 jbd2_log_wait_commit(osb->journal->j_journal,
Joel Becker2b4e30f2008-09-03 20:03:41 -0700418 target);
Mark Fashehccd979b2005-12-15 14:31:24 -0800419 }
420 return 0;
421}
422
Jan Kara1a224ad2008-08-20 15:43:36 +0200423static int ocfs2_need_system_inode(struct ocfs2_super *osb, int ino)
424{
425 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_USRQUOTA)
426 && (ino == USER_QUOTA_SYSTEM_INODE
427 || ino == LOCAL_USER_QUOTA_SYSTEM_INODE))
428 return 0;
429 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)
430 && (ino == GROUP_QUOTA_SYSTEM_INODE
431 || ino == LOCAL_GROUP_QUOTA_SYSTEM_INODE))
432 return 0;
433 return 1;
434}
435
Mark Fashehccd979b2005-12-15 14:31:24 -0800436static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb)
437{
438 struct inode *new = NULL;
439 int status = 0;
440 int i;
441
Jan Kara5fa06132008-01-11 00:11:45 +0100442 new = ocfs2_iget(osb, osb->root_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800443 if (IS_ERR(new)) {
444 status = PTR_ERR(new);
445 mlog_errno(status);
446 goto bail;
447 }
448 osb->root_inode = new;
449
Jan Kara5fa06132008-01-11 00:11:45 +0100450 new = ocfs2_iget(osb, osb->system_dir_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800451 if (IS_ERR(new)) {
452 status = PTR_ERR(new);
453 mlog_errno(status);
454 goto bail;
455 }
456 osb->sys_root_inode = new;
457
458 for (i = OCFS2_FIRST_ONLINE_SYSTEM_INODE;
459 i <= OCFS2_LAST_GLOBAL_SYSTEM_INODE; i++) {
Jan Kara1a224ad2008-08-20 15:43:36 +0200460 if (!ocfs2_need_system_inode(osb, i))
461 continue;
Mark Fashehccd979b2005-12-15 14:31:24 -0800462 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
463 if (!new) {
464 ocfs2_release_system_inodes(osb);
piaojun025bcbd2018-01-31 16:14:44 -0800465 status = ocfs2_is_soft_readonly(osb) ? -EROFS : -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800466 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800467 mlog(ML_ERROR, "Unable to load system inode %d, "
468 "possibly corrupt fs?", i);
469 goto bail;
470 }
471 // the array now has one ref, so drop this one
472 iput(new);
473 }
474
475bail:
Tao Mac1e8d352011-03-07 16:43:21 +0800476 if (status)
477 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800478 return status;
479}
480
481static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb)
482{
483 struct inode *new = NULL;
484 int status = 0;
485 int i;
486
Mark Fashehccd979b2005-12-15 14:31:24 -0800487 for (i = OCFS2_LAST_GLOBAL_SYSTEM_INODE + 1;
488 i < NUM_SYSTEM_INODES;
489 i++) {
Jan Kara1a224ad2008-08-20 15:43:36 +0200490 if (!ocfs2_need_system_inode(osb, i))
491 continue;
Mark Fashehccd979b2005-12-15 14:31:24 -0800492 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
493 if (!new) {
494 ocfs2_release_system_inodes(osb);
piaojun025bcbd2018-01-31 16:14:44 -0800495 status = ocfs2_is_soft_readonly(osb) ? -EROFS : -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800496 mlog(ML_ERROR, "status=%d, sysfile=%d, slot=%d\n",
497 status, i, osb->slot_num);
498 goto bail;
499 }
500 /* the array now has one ref, so drop this one */
501 iput(new);
502 }
503
504bail:
Tao Mac1e8d352011-03-07 16:43:21 +0800505 if (status)
506 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800507 return status;
508}
509
Denis Chengbddb8eb32007-09-27 02:10:04 +0800510static void ocfs2_release_system_inodes(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -0800511{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800512 int i;
Mark Fashehccd979b2005-12-15 14:31:24 -0800513 struct inode *inode;
514
Tao Mab4d693f2010-08-16 16:58:21 +0800515 for (i = 0; i < NUM_GLOBAL_SYSTEM_INODES; i++) {
516 inode = osb->global_system_inodes[i];
Mark Fashehccd979b2005-12-15 14:31:24 -0800517 if (inode) {
518 iput(inode);
Tao Mab4d693f2010-08-16 16:58:21 +0800519 osb->global_system_inodes[i] = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800520 }
521 }
522
523 inode = osb->sys_root_inode;
524 if (inode) {
525 iput(inode);
526 osb->sys_root_inode = NULL;
527 }
528
529 inode = osb->root_inode;
530 if (inode) {
531 iput(inode);
532 osb->root_inode = NULL;
533 }
534
Tao Mab4d693f2010-08-16 16:58:21 +0800535 if (!osb->local_system_inodes)
Tao Mac1e8d352011-03-07 16:43:21 +0800536 return;
Tao Mab4d693f2010-08-16 16:58:21 +0800537
538 for (i = 0; i < NUM_LOCAL_SYSTEM_INODES * osb->max_slots; i++) {
539 if (osb->local_system_inodes[i]) {
540 iput(osb->local_system_inodes[i]);
541 osb->local_system_inodes[i] = NULL;
542 }
543 }
544
545 kfree(osb->local_system_inodes);
546 osb->local_system_inodes = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800547}
548
549/* We're allocating fs objects, use GFP_NOFS */
550static struct inode *ocfs2_alloc_inode(struct super_block *sb)
551{
552 struct ocfs2_inode_info *oi;
553
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800554 oi = kmem_cache_alloc(ocfs2_inode_cachep, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800555 if (!oi)
556 return NULL;
557
Darrick J. Wong2931cdc2014-04-03 14:46:48 -0700558 oi->i_sync_tid = 0;
559 oi->i_datasync_tid = 0;
Jan Kara1c92ec62014-09-29 15:02:51 +0200560 memset(&oi->i_dquot, 0, sizeof(oi->i_dquot));
Darrick J. Wong2931cdc2014-04-03 14:46:48 -0700561
Joel Becker2b4e30f2008-09-03 20:03:41 -0700562 jbd2_journal_init_jbd_inode(&oi->ip_jinode, &oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800563 return &oi->vfs_inode;
564}
565
Al Viroe91b9192019-04-15 20:26:42 -0400566static void ocfs2_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100567{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100568 kmem_cache_free(ocfs2_inode_cachep, OCFS2_I(inode));
569}
570
Mark Fasheh5a254032007-07-20 12:56:16 -0700571static unsigned long long ocfs2_max_file_offset(unsigned int bbits,
572 unsigned int cbits)
Mark Fashehccd979b2005-12-15 14:31:24 -0800573{
Mark Fasheh5a254032007-07-20 12:56:16 -0700574 unsigned int bytes = 1 << cbits;
575 unsigned int trim = bytes;
576 unsigned int bitshift = 32;
Mark Fashehccd979b2005-12-15 14:31:24 -0800577
Mark Fasheh5a254032007-07-20 12:56:16 -0700578 /*
579 * i_size and all block offsets in ocfs2 are always 64 bits
580 * wide. i_clusters is 32 bits, in cluster-sized units. So on
581 * 64 bit platforms, cluster size will be the limiting factor.
Mark Fashehccd979b2005-12-15 14:31:24 -0800582 */
583
584#if BITS_PER_LONG == 32
Alexey Dobriyan2ecd05a2006-10-11 01:22:05 -0700585 BUILD_BUG_ON(sizeof(sector_t) != 8);
Mark Fasheh5a254032007-07-20 12:56:16 -0700586 /*
587 * We might be limited by page cache size.
588 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300589 if (bytes > PAGE_SIZE) {
590 bytes = PAGE_SIZE;
Mark Fasheh5a254032007-07-20 12:56:16 -0700591 trim = 1;
592 /*
593 * Shift by 31 here so that we don't get larger than
594 * MAX_LFS_FILESIZE
595 */
596 bitshift = 31;
597 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800598#endif
599
Mark Fasheh5a254032007-07-20 12:56:16 -0700600 /*
601 * Trim by a whole cluster when we can actually approach the
602 * on-disk limits. Otherwise we can overflow i_clusters when
603 * an extent start is at the max offset.
604 */
605 return (((unsigned long long)bytes) << bitshift) - trim;
Mark Fashehccd979b2005-12-15 14:31:24 -0800606}
607
608static int ocfs2_remount(struct super_block *sb, int *flags, char *data)
609{
610 int incompat_features;
611 int ret = 0;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800612 struct mount_options parsed_options;
Mark Fashehccd979b2005-12-15 14:31:24 -0800613 struct ocfs2_super *osb = OCFS2_SB(sb);
Sunil Mushran2c442712010-10-07 15:23:50 -0700614 u32 tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -0800615
Theodore Ts'o02b99842014-03-13 10:14:33 -0400616 sync_filesystem(sb);
617
Jan Kara5297aad2009-10-15 14:54:04 +0200618 if (!ocfs2_parse_options(sb, data, &parsed_options, 1) ||
619 !ocfs2_check_set_options(sb, &parsed_options)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800620 ret = -EINVAL;
621 goto out;
622 }
623
Gang He912f6552020-06-01 21:45:29 -0700624 tmp = OCFS2_MOUNT_NOCLUSTER;
625 if ((osb->s_mount_opt & tmp) != (parsed_options.mount_opt & tmp)) {
626 ret = -EINVAL;
627 mlog(ML_ERROR, "Cannot change nocluster option on remount\n");
628 goto out;
629 }
630
Sunil Mushran2c442712010-10-07 15:23:50 -0700631 tmp = OCFS2_MOUNT_HB_LOCAL | OCFS2_MOUNT_HB_GLOBAL |
632 OCFS2_MOUNT_HB_NONE;
633 if ((osb->s_mount_opt & tmp) != (parsed_options.mount_opt & tmp)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800634 ret = -EINVAL;
635 mlog(ML_ERROR, "Cannot change heartbeat mode on remount\n");
636 goto out;
637 }
638
639 if ((osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK) !=
Tiger Yangc0123ad2007-09-08 00:16:10 +0800640 (parsed_options.mount_opt & OCFS2_MOUNT_DATA_WRITEBACK)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800641 ret = -EINVAL;
642 mlog(ML_ERROR, "Cannot change data mode on remount\n");
643 goto out;
644 }
645
Joel Becker12462f12008-09-03 20:03:40 -0700646 /* Probably don't want this on remount; it might
647 * mess with other nodes */
648 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64) &&
649 (parsed_options.mount_opt & OCFS2_MOUNT_INODE64)) {
650 ret = -EINVAL;
651 mlog(ML_ERROR, "Cannot enable inode64 on remount\n");
652 goto out;
653 }
654
Mark Fashehccd979b2005-12-15 14:31:24 -0800655 /* We're going to/from readonly mode. */
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800656 if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
Jan Kara19ece542008-08-21 20:13:17 +0200657 /* Disable quota accounting before remounting RO */
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800658 if (*flags & SB_RDONLY) {
Jan Kara19ece542008-08-21 20:13:17 +0200659 ret = ocfs2_susp_quotas(osb, 0);
660 if (ret < 0)
661 goto out;
662 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800663 /* Lock here so the check of HARD_RO and the potential
664 * setting of SOFT_RO is atomic. */
665 spin_lock(&osb->osb_lock);
666 if (osb->osb_flags & OCFS2_OSB_HARD_RO) {
667 mlog(ML_ERROR, "Remount on readonly device is forbidden.\n");
668 ret = -EROFS;
669 goto unlock_osb;
670 }
671
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800672 if (*flags & SB_RDONLY) {
673 sb->s_flags |= SB_RDONLY;
Mark Fashehccd979b2005-12-15 14:31:24 -0800674 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
675 } else {
Mark Fashehccd979b2005-12-15 14:31:24 -0800676 if (osb->osb_flags & OCFS2_OSB_ERROR_FS) {
677 mlog(ML_ERROR, "Cannot remount RDWR "
678 "filesystem due to previous errors.\n");
679 ret = -EROFS;
680 goto unlock_osb;
681 }
682 incompat_features = OCFS2_HAS_RO_COMPAT_FEATURE(sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP);
683 if (incompat_features) {
684 mlog(ML_ERROR, "Cannot remount RDWR because "
685 "of unsupported optional features "
686 "(%x).\n", incompat_features);
687 ret = -EINVAL;
688 goto unlock_osb;
689 }
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800690 sb->s_flags &= ~SB_RDONLY;
Mark Fashehccd979b2005-12-15 14:31:24 -0800691 osb->osb_flags &= ~OCFS2_OSB_SOFT_RO;
692 }
Tao Ma32a42d32011-02-23 21:29:08 +0800693 trace_ocfs2_remount(sb->s_flags, osb->osb_flags, *flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800694unlock_osb:
695 spin_unlock(&osb->osb_lock);
Jan Kara19ece542008-08-21 20:13:17 +0200696 /* Enable quota accounting after remounting RW */
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800697 if (!ret && !(*flags & SB_RDONLY)) {
Jan Kara19ece542008-08-21 20:13:17 +0200698 if (sb_any_quota_suspended(sb))
699 ret = ocfs2_susp_quotas(osb, 1);
700 else
701 ret = ocfs2_enable_quotas(osb);
702 if (ret < 0) {
703 /* Return back changes... */
704 spin_lock(&osb->osb_lock);
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800705 sb->s_flags |= SB_RDONLY;
Jan Kara19ece542008-08-21 20:13:17 +0200706 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
707 spin_unlock(&osb->osb_lock);
708 goto out;
709 }
710 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800711 }
712
713 if (!ret) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800714 /* Only save off the new mount options in case of a successful
715 * remount. */
Tiger Yangc0123ad2007-09-08 00:16:10 +0800716 osb->s_mount_opt = parsed_options.mount_opt;
717 osb->s_atime_quantum = parsed_options.atime_quantum;
718 osb->preferred_slot = parsed_options.slot;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800719 if (parsed_options.commit_interval)
720 osb->osb_commit_interval = parsed_options.commit_interval;
Mark Fashehe001e792007-11-07 14:21:45 -0800721
722 if (!ocfs2_is_hard_readonly(osb))
723 ocfs2_set_journal_params(osb);
Jan Kara57b09bb2009-10-15 14:54:05 +0200724
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800725 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
Jan Kara57b09bb2009-10-15 14:54:05 +0200726 ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ?
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800727 SB_POSIXACL : 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800728 }
729out:
730 return ret;
731}
732
733static int ocfs2_sb_probe(struct super_block *sb,
734 struct buffer_head **bh,
Joel Becker73be1922009-01-06 14:57:08 -0800735 int *sector_size,
736 struct ocfs2_blockcheck_stats *stats)
Mark Fashehccd979b2005-12-15 14:31:24 -0800737{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800738 int status, tmpstat;
Mark Fashehccd979b2005-12-15 14:31:24 -0800739 struct ocfs1_vol_disk_hdr *hdr;
740 struct ocfs2_dinode *di;
741 int blksize;
742
743 *bh = NULL;
744
745 /* may be > 512 */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400746 *sector_size = bdev_logical_block_size(sb->s_bdev);
Mark Fashehccd979b2005-12-15 14:31:24 -0800747 if (*sector_size > OCFS2_MAX_BLOCKSIZE) {
748 mlog(ML_ERROR, "Hardware sector size too large: %d (max=%d)\n",
749 *sector_size, OCFS2_MAX_BLOCKSIZE);
750 status = -EINVAL;
751 goto bail;
752 }
753
754 /* Can this really happen? */
755 if (*sector_size < OCFS2_MIN_BLOCKSIZE)
756 *sector_size = OCFS2_MIN_BLOCKSIZE;
757
758 /* check block zero for old format */
759 status = ocfs2_get_sector(sb, bh, 0, *sector_size);
760 if (status < 0) {
761 mlog_errno(status);
762 goto bail;
763 }
764 hdr = (struct ocfs1_vol_disk_hdr *) (*bh)->b_data;
765 if (hdr->major_version == OCFS1_MAJOR_VERSION) {
766 mlog(ML_ERROR, "incompatible version: %u.%u\n",
767 hdr->major_version, hdr->minor_version);
768 status = -EINVAL;
769 }
770 if (memcmp(hdr->signature, OCFS1_VOLUME_SIGNATURE,
771 strlen(OCFS1_VOLUME_SIGNATURE)) == 0) {
772 mlog(ML_ERROR, "incompatible volume signature: %8s\n",
773 hdr->signature);
774 status = -EINVAL;
775 }
776 brelse(*bh);
777 *bh = NULL;
778 if (status < 0) {
779 mlog(ML_ERROR, "This is an ocfs v1 filesystem which must be "
780 "upgraded before mounting with ocfs v2\n");
781 goto bail;
782 }
783
784 /*
785 * Now check at magic offset for 512, 1024, 2048, 4096
786 * blocksizes. 4096 is the maximum blocksize because it is
787 * the minimum clustersize.
788 */
789 status = -EINVAL;
790 for (blksize = *sector_size;
791 blksize <= OCFS2_MAX_BLOCKSIZE;
792 blksize <<= 1) {
793 tmpstat = ocfs2_get_sector(sb, bh,
794 OCFS2_SUPER_BLOCK_BLKNO,
795 blksize);
796 if (tmpstat < 0) {
797 status = tmpstat;
798 mlog_errno(status);
Joel Beckerfb5cbe92009-10-28 22:28:24 -0700799 break;
Mark Fashehccd979b2005-12-15 14:31:24 -0800800 }
801 di = (struct ocfs2_dinode *) (*bh)->b_data;
Joel Becker73be1922009-01-06 14:57:08 -0800802 memset(stats, 0, sizeof(struct ocfs2_blockcheck_stats));
Jan Kara1c1d9792009-07-22 13:17:19 +0200803 spin_lock_init(&stats->b_lock);
Joel Beckerfb5cbe92009-10-28 22:28:24 -0700804 tmpstat = ocfs2_verify_volume(di, *bh, blksize, stats);
805 if (tmpstat < 0) {
806 brelse(*bh);
807 *bh = NULL;
808 }
809 if (tmpstat != -EAGAIN) {
810 status = tmpstat;
Mark Fashehccd979b2005-12-15 14:31:24 -0800811 break;
Joel Beckerfb5cbe92009-10-28 22:28:24 -0700812 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800813 }
814
815bail:
816 return status;
817}
818
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800819static int ocfs2_verify_heartbeat(struct ocfs2_super *osb)
820{
Sunil Mushran2c442712010-10-07 15:23:50 -0700821 u32 hb_enabled = OCFS2_MOUNT_HB_LOCAL | OCFS2_MOUNT_HB_GLOBAL;
822
823 if (osb->s_mount_opt & hb_enabled) {
824 if (ocfs2_mount_local(osb)) {
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800825 mlog(ML_ERROR, "Cannot heartbeat on a locally "
826 "mounted device.\n");
827 return -EINVAL;
828 }
Sunil Mushran2c442712010-10-07 15:23:50 -0700829 if (ocfs2_userspace_stack(osb)) {
Joel Beckerb61817e2008-02-01 15:08:23 -0800830 mlog(ML_ERROR, "Userspace stack expected, but "
831 "o2cb heartbeat arguments passed to mount\n");
832 return -EINVAL;
833 }
Sunil Mushran2c442712010-10-07 15:23:50 -0700834 if (((osb->s_mount_opt & OCFS2_MOUNT_HB_GLOBAL) &&
835 !ocfs2_cluster_o2cb_global_heartbeat(osb)) ||
836 ((osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) &&
837 ocfs2_cluster_o2cb_global_heartbeat(osb))) {
838 mlog(ML_ERROR, "Mismatching o2cb heartbeat modes\n");
839 return -EINVAL;
840 }
Joel Beckerb61817e2008-02-01 15:08:23 -0800841 }
842
Sunil Mushran2c442712010-10-07 15:23:50 -0700843 if (!(osb->s_mount_opt & hb_enabled)) {
Joel Beckerb61817e2008-02-01 15:08:23 -0800844 if (!ocfs2_mount_local(osb) && !ocfs2_is_hard_readonly(osb) &&
845 !ocfs2_userspace_stack(osb)) {
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800846 mlog(ML_ERROR, "Heartbeat has to be started to mount "
847 "a read-write clustered device.\n");
848 return -EINVAL;
849 }
850 }
851
852 return 0;
853}
854
Joel Beckerb61817e2008-02-01 15:08:23 -0800855/*
856 * If we're using a userspace stack, mount should have passed
857 * a name that matches the disk. If not, mount should not
858 * have passed a stack.
859 */
860static int ocfs2_verify_userspace_stack(struct ocfs2_super *osb,
861 struct mount_options *mopt)
862{
863 if (!ocfs2_userspace_stack(osb) && mopt->cluster_stack[0]) {
864 mlog(ML_ERROR,
865 "cluster stack passed to mount, but this filesystem "
866 "does not support it\n");
867 return -EINVAL;
868 }
869
870 if (ocfs2_userspace_stack(osb) &&
Gang He912f6552020-06-01 21:45:29 -0700871 !(osb->s_mount_opt & OCFS2_MOUNT_NOCLUSTER) &&
Joel Beckerb61817e2008-02-01 15:08:23 -0800872 strncmp(osb->osb_cluster_stack, mopt->cluster_stack,
873 OCFS2_STACK_LABEL_LEN)) {
874 mlog(ML_ERROR,
875 "cluster stack passed to mount (\"%s\") does not "
876 "match the filesystem (\"%s\")\n",
877 mopt->cluster_stack,
878 osb->osb_cluster_stack);
879 return -EINVAL;
880 }
881
882 return 0;
883}
884
Jan Kara19ece542008-08-21 20:13:17 +0200885static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend)
886{
887 int type;
888 struct super_block *sb = osb->sb;
Jan Kara52362812014-09-10 21:06:39 +0200889 unsigned int feature[OCFS2_MAXQUOTAS] = {
890 OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
891 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
Jan Kara19ece542008-08-21 20:13:17 +0200892 int status = 0;
893
Jan Kara52362812014-09-10 21:06:39 +0200894 for (type = 0; type < OCFS2_MAXQUOTAS; type++) {
Jan Kara19ece542008-08-21 20:13:17 +0200895 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
896 continue;
897 if (unsuspend)
Christoph Hellwig0f0dd622010-05-19 07:16:41 -0400898 status = dquot_resume(sb, type);
Jan Karaeea7feb2010-05-13 22:14:53 +0200899 else {
900 struct ocfs2_mem_dqinfo *oinfo;
901
902 /* Cancel periodic syncing before suspending */
903 oinfo = sb_dqinfo(sb, type)->dqi_priv;
904 cancel_delayed_work_sync(&oinfo->dqi_sync_work);
Christoph Hellwig0f0dd622010-05-19 07:16:41 -0400905 status = dquot_suspend(sb, type);
Jan Karaeea7feb2010-05-13 22:14:53 +0200906 }
Jan Kara19ece542008-08-21 20:13:17 +0200907 if (status < 0)
908 break;
909 }
910 if (status < 0)
911 mlog(ML_ERROR, "Failed to suspend/unsuspend quotas on "
912 "remount (error = %d).\n", status);
913 return status;
914}
915
916static int ocfs2_enable_quotas(struct ocfs2_super *osb)
917{
Jan Kara52362812014-09-10 21:06:39 +0200918 struct inode *inode[OCFS2_MAXQUOTAS] = { NULL, NULL };
Jan Kara19ece542008-08-21 20:13:17 +0200919 struct super_block *sb = osb->sb;
Jan Kara52362812014-09-10 21:06:39 +0200920 unsigned int feature[OCFS2_MAXQUOTAS] = {
921 OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
922 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
923 unsigned int ino[OCFS2_MAXQUOTAS] = {
924 LOCAL_USER_QUOTA_SYSTEM_INODE,
Jan Kara19ece542008-08-21 20:13:17 +0200925 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
926 int status;
927 int type;
928
929 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NEGATIVE_USAGE;
Jan Kara52362812014-09-10 21:06:39 +0200930 for (type = 0; type < OCFS2_MAXQUOTAS; type++) {
Jan Kara19ece542008-08-21 20:13:17 +0200931 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
932 continue;
933 inode[type] = ocfs2_get_system_file_inode(osb, ino[type],
934 osb->slot_num);
935 if (!inode[type]) {
936 status = -ENOENT;
937 goto out_quota_off;
938 }
Jan Kara7212b952019-11-01 18:55:38 +0100939 status = dquot_load_quota_inode(inode[type], type, QFMT_OCFS2,
940 DQUOT_USAGE_ENABLED);
Jan Kara19ece542008-08-21 20:13:17 +0200941 if (status < 0)
942 goto out_quota_off;
943 }
944
Jan Kara52362812014-09-10 21:06:39 +0200945 for (type = 0; type < OCFS2_MAXQUOTAS; type++)
Jan Kara19ece542008-08-21 20:13:17 +0200946 iput(inode[type]);
947 return 0;
948out_quota_off:
949 ocfs2_disable_quotas(osb);
Jan Kara52362812014-09-10 21:06:39 +0200950 for (type = 0; type < OCFS2_MAXQUOTAS; type++)
Jan Kara19ece542008-08-21 20:13:17 +0200951 iput(inode[type]);
952 mlog_errno(status);
953 return status;
954}
955
956static void ocfs2_disable_quotas(struct ocfs2_super *osb)
957{
958 int type;
959 struct inode *inode;
960 struct super_block *sb = osb->sb;
Jan Karac06bcbf2010-05-13 22:14:53 +0200961 struct ocfs2_mem_dqinfo *oinfo;
Jan Kara19ece542008-08-21 20:13:17 +0200962
963 /* We mostly ignore errors in this function because there's not much
964 * we can do when we see them */
Jan Kara52362812014-09-10 21:06:39 +0200965 for (type = 0; type < OCFS2_MAXQUOTAS; type++) {
Jan Kara19ece542008-08-21 20:13:17 +0200966 if (!sb_has_quota_loaded(sb, type))
967 continue;
Jan Karac06bcbf2010-05-13 22:14:53 +0200968 oinfo = sb_dqinfo(sb, type)->dqi_priv;
969 cancel_delayed_work_sync(&oinfo->dqi_sync_work);
Jan Kara19ece542008-08-21 20:13:17 +0200970 inode = igrab(sb->s_dquot.files[type]);
971 /* Turn off quotas. This will remove all dquot structures from
972 * memory and so they will be automatically synced to global
973 * quota files */
Christoph Hellwig0f0dd622010-05-19 07:16:41 -0400974 dquot_disable(sb, type, DQUOT_USAGE_ENABLED |
975 DQUOT_LIMITS_ENABLED);
Jan Kara19ece542008-08-21 20:13:17 +0200976 if (!inode)
977 continue;
978 iput(inode);
979 }
980}
981
Mark Fashehccd979b2005-12-15 14:31:24 -0800982static int ocfs2_fill_super(struct super_block *sb, void *data, int silent)
983{
984 struct dentry *root;
985 int status, sector_size;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800986 struct mount_options parsed_options;
Mark Fashehccd979b2005-12-15 14:31:24 -0800987 struct inode *inode = NULL;
988 struct ocfs2_super *osb = NULL;
989 struct buffer_head *bh = NULL;
Goldwyn Rodrigues49fa8140e2013-08-28 16:35:21 -0700990 char nodestr[12];
Joel Becker73be1922009-01-06 14:57:08 -0800991 struct ocfs2_blockcheck_stats stats;
Mark Fashehccd979b2005-12-15 14:31:24 -0800992
Tao Ma32a42d32011-02-23 21:29:08 +0800993 trace_ocfs2_fill_super(sb, data, silent);
Mark Fashehccd979b2005-12-15 14:31:24 -0800994
Tiger Yangc0123ad2007-09-08 00:16:10 +0800995 if (!ocfs2_parse_options(sb, data, &parsed_options, 0)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800996 status = -EINVAL;
997 goto read_super_error;
998 }
999
1000 /* probe for superblock */
Joel Becker73be1922009-01-06 14:57:08 -08001001 status = ocfs2_sb_probe(sb, &bh, &sector_size, &stats);
Mark Fashehccd979b2005-12-15 14:31:24 -08001002 if (status < 0) {
1003 mlog(ML_ERROR, "superblock probe failed!\n");
1004 goto read_super_error;
1005 }
1006
Joel Becker73be1922009-01-06 14:57:08 -08001007 status = ocfs2_initialize_super(sb, bh, sector_size, &stats);
Mark Fashehccd979b2005-12-15 14:31:24 -08001008 osb = OCFS2_SB(sb);
1009 if (status < 0) {
1010 mlog_errno(status);
1011 goto read_super_error;
1012 }
1013 brelse(bh);
1014 bh = NULL;
Tiger Yanga68979b2008-11-14 11:17:52 +08001015
Jan Kara5297aad2009-10-15 14:54:04 +02001016 if (!ocfs2_check_set_options(sb, &parsed_options)) {
1017 status = -EINVAL;
1018 goto read_super_error;
1019 }
Tiger Yangc0123ad2007-09-08 00:16:10 +08001020 osb->s_mount_opt = parsed_options.mount_opt;
1021 osb->s_atime_quantum = parsed_options.atime_quantum;
1022 osb->preferred_slot = parsed_options.slot;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001023 osb->osb_commit_interval = parsed_options.commit_interval;
Mark Fasheh73c8a802010-04-05 18:17:13 -07001024
1025 ocfs2_la_set_sizes(osb, parsed_options.localalloc_opt);
Mark Fashehd02f00c2009-12-07 13:10:48 -08001026 osb->osb_resv_level = parsed_options.resv_level;
Mark Fasheh83f92312010-04-05 18:17:16 -07001027 osb->osb_dir_resv_level = parsed_options.resv_level;
1028 if (parsed_options.dir_resv_level == -1)
1029 osb->osb_dir_resv_level = parsed_options.resv_level;
1030 else
1031 osb->osb_dir_resv_level = parsed_options.dir_resv_level;
Mark Fashehccd979b2005-12-15 14:31:24 -08001032
Joel Beckerb61817e2008-02-01 15:08:23 -08001033 status = ocfs2_verify_userspace_stack(osb, &parsed_options);
1034 if (status)
1035 goto read_super_error;
1036
Mark Fashehccd979b2005-12-15 14:31:24 -08001037 sb->s_magic = OCFS2_SUPER_MAGIC;
1038
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001039 sb->s_flags = (sb->s_flags & ~(SB_POSIXACL | SB_NOSEC)) |
1040 ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ? SB_POSIXACL : 0);
Tiger Yanga68979b2008-11-14 11:17:52 +08001041
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001042 /* Hard readonly mode only if: bdev_read_only, SB_RDONLY,
Mark Fashehccd979b2005-12-15 14:31:24 -08001043 * heartbeat=none */
1044 if (bdev_read_only(sb->s_bdev)) {
David Howellsbc98a422017-07-17 08:45:34 +01001045 if (!sb_rdonly(sb)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001046 status = -EACCES;
1047 mlog(ML_ERROR, "Readonly device detected but readonly "
1048 "mount was not specified.\n");
1049 goto read_super_error;
1050 }
1051
1052 /* You should not be able to start a local heartbeat
1053 * on a readonly device. */
1054 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
1055 status = -EROFS;
1056 mlog(ML_ERROR, "Local heartbeat specified on readonly "
1057 "device.\n");
1058 goto read_super_error;
1059 }
1060
1061 status = ocfs2_check_journals_nolocks(osb);
1062 if (status < 0) {
1063 if (status == -EROFS)
1064 mlog(ML_ERROR, "Recovery required on readonly "
1065 "file system, but write access is "
1066 "unavailable.\n");
1067 else
Sunil Mushran2bd63212010-01-25 16:57:38 -08001068 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001069 goto read_super_error;
1070 }
1071
1072 ocfs2_set_ro_flag(osb, 1);
1073
Sunil Mushran619c2002011-07-24 10:34:54 -07001074 printk(KERN_NOTICE "ocfs2: Readonly device (%s) detected. "
1075 "Cluster services will not be used for this mount. "
1076 "Recovery will be skipped.\n", osb->dev_str);
Mark Fashehccd979b2005-12-15 14:31:24 -08001077 }
1078
1079 if (!ocfs2_is_hard_readonly(osb)) {
David Howellsbc98a422017-07-17 08:45:34 +01001080 if (sb_rdonly(sb))
Mark Fashehccd979b2005-12-15 14:31:24 -08001081 ocfs2_set_ro_flag(osb, 0);
1082 }
1083
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001084 status = ocfs2_verify_heartbeat(osb);
1085 if (status < 0) {
1086 mlog_errno(status);
1087 goto read_super_error;
1088 }
1089
Mark Fashehccd979b2005-12-15 14:31:24 -08001090 osb->osb_debug_root = debugfs_create_dir(osb->uuid_str,
1091 ocfs2_debugfs_root);
Mark Fashehccd979b2005-12-15 14:31:24 -08001092
Greg Kroah-Hartman5e7a3ed2019-09-23 15:33:15 -07001093 debugfs_create_file("fs_state", S_IFREG|S_IRUSR, osb->osb_debug_root,
1094 osb, &ocfs2_osb_debug_fops);
Sunil Mushran50397502008-12-17 14:17:43 -08001095
Greg Kroah-Hartmane5815952019-07-11 20:53:12 -07001096 if (ocfs2_meta_ecc(osb))
1097 ocfs2_blockcheck_stats_debugfs_install( &osb->osb_ecc_stats,
1098 osb->osb_debug_root);
Joel Becker73be1922009-01-06 14:57:08 -08001099
Mark Fashehccd979b2005-12-15 14:31:24 -08001100 status = ocfs2_mount_volume(sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001101 if (status < 0)
1102 goto read_super_error;
1103
Al Virobe0d93f2012-02-12 21:46:49 -05001104 if (osb->root_inode)
1105 inode = igrab(osb->root_inode);
1106
Mark Fashehccd979b2005-12-15 14:31:24 -08001107 if (!inode) {
1108 status = -EIO;
1109 mlog_errno(status);
1110 goto read_super_error;
1111 }
1112
Al Viro48fde702012-01-08 22:15:13 -05001113 root = d_make_root(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001114 if (!root) {
1115 status = -ENOMEM;
1116 mlog_errno(status);
1117 goto read_super_error;
1118 }
1119
1120 sb->s_root = root;
1121
1122 ocfs2_complete_mount_recovery(osb);
1123
Gang He5f483c42018-04-05 16:19:29 -07001124 osb->osb_dev_kset = kset_create_and_add(sb->s_id, NULL,
1125 &ocfs2_kset->kobj);
1126 if (!osb->osb_dev_kset) {
1127 status = -ENOMEM;
1128 mlog(ML_ERROR, "Unable to create device kset %s.\n", sb->s_id);
1129 goto read_super_error;
1130 }
1131
1132 /* Create filecheck sysfs related directories/files at
1133 * /sys/fs/ocfs2/<devname>/filecheck */
1134 if (ocfs2_filecheck_create_sysfs(osb)) {
1135 status = -ENOMEM;
1136 mlog(ML_ERROR, "Unable to create filecheck sysfs directory at "
1137 "/sys/fs/ocfs2/%s/filecheck.\n", sb->s_id);
1138 goto read_super_error;
1139 }
1140
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001141 if (ocfs2_mount_local(osb))
1142 snprintf(nodestr, sizeof(nodestr), "local");
1143 else
Joel Becker19fdb622008-01-30 15:38:24 -08001144 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001145
1146 printk(KERN_INFO "ocfs2: Mounting device (%s) on (node %s, slot %d) "
Sunil Mushran781ee3e2006-04-27 16:41:31 -07001147 "with %s data mode.\n",
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001148 osb->dev_str, nodestr, osb->slot_num,
Mark Fashehccd979b2005-12-15 14:31:24 -08001149 osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK ? "writeback" :
1150 "ordered");
1151
Gang He912f6552020-06-01 21:45:29 -07001152 if ((osb->s_mount_opt & OCFS2_MOUNT_NOCLUSTER) &&
1153 !(osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_LOCAL_MOUNT))
1154 printk(KERN_NOTICE "ocfs2: The shared device (%s) is mounted "
1155 "without cluster aware mode.\n", osb->dev_str);
1156
Mark Fashehccd979b2005-12-15 14:31:24 -08001157 atomic_set(&osb->vol_state, VOLUME_MOUNTED);
1158 wake_up(&osb->osb_mount_event);
1159
Jan Kara19ece542008-08-21 20:13:17 +02001160 /* Now we can initialize quotas because we can afford to wait
1161 * for cluster locks recovery now. That also means that truncation
1162 * log recovery can happen but that waits for proper quota setup */
David Howellsbc98a422017-07-17 08:45:34 +01001163 if (!sb_rdonly(sb)) {
Jan Kara19ece542008-08-21 20:13:17 +02001164 status = ocfs2_enable_quotas(osb);
1165 if (status < 0) {
1166 /* We have to err-out specially here because
1167 * s_root is already set */
1168 mlog_errno(status);
1169 atomic_set(&osb->vol_state, VOLUME_DISABLED);
1170 wake_up(&osb->osb_mount_event);
Jan Kara19ece542008-08-21 20:13:17 +02001171 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
Sunil Mushrandf152c22009-06-22 11:40:07 -07001181 /* Start this when the mount is almost sure of being successful */
Jeff Mahoney8b712cd2009-07-07 17:22:12 -04001182 ocfs2_orphan_scan_start(osb);
Sunil Mushrandf152c22009-06-22 11:40:07 -07001183
Mark Fashehccd979b2005-12-15 14:31:24 -08001184 return status;
1185
1186read_super_error:
Mark Fasheha81cb882008-10-07 14:25:16 -07001187 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001188
Gang Hea52370b2018-01-31 16:14:29 -08001189 if (status)
1190 mlog_errno(status);
1191
Mark Fashehccd979b2005-12-15 14:31:24 -08001192 if (osb) {
1193 atomic_set(&osb->vol_state, VOLUME_DISABLED);
1194 wake_up(&osb->osb_mount_event);
1195 ocfs2_dismount_volume(sb, 1);
1196 }
1197
Mark Fashehccd979b2005-12-15 14:31:24 -08001198 return status;
1199}
1200
Al Viro152a0832010-07-25 00:46:55 +04001201static struct dentry *ocfs2_mount(struct file_system_type *fs_type,
David Howells454e2392006-06-23 02:02:57 -07001202 int flags,
1203 const char *dev_name,
Al Viro152a0832010-07-25 00:46:55 +04001204 void *data)
Mark Fashehccd979b2005-12-15 14:31:24 -08001205{
Al Viro152a0832010-07-25 00:46:55 +04001206 return mount_bdev(fs_type, flags, dev_name, data, ocfs2_fill_super);
Mark Fashehccd979b2005-12-15 14:31:24 -08001207}
1208
1209static struct file_system_type ocfs2_fs_type = {
1210 .owner = THIS_MODULE,
1211 .name = "ocfs2",
Al Viro152a0832010-07-25 00:46:55 +04001212 .mount = ocfs2_mount,
Goldwyn Rodrigues8ed6b232014-04-03 14:46:59 -07001213 .kill_sb = kill_block_super,
Mark Fasheh1ba9da22006-09-08 14:22:54 -07001214 .fs_flags = FS_REQUIRES_DEV|FS_RENAME_DOES_D_MOVE,
Mark Fashehccd979b2005-12-15 14:31:24 -08001215 .next = NULL
1216};
Eric W. Biederman91417702013-03-07 01:08:55 -08001217MODULE_ALIAS_FS("ocfs2");
Mark Fashehccd979b2005-12-15 14:31:24 -08001218
Jan Kara5297aad2009-10-15 14:54:04 +02001219static int ocfs2_check_set_options(struct super_block *sb,
1220 struct mount_options *options)
1221{
1222 if (options->mount_opt & OCFS2_MOUNT_USRQUOTA &&
1223 !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1224 OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
1225 mlog(ML_ERROR, "User quotas were requested, but this "
1226 "filesystem does not have the feature enabled.\n");
1227 return 0;
1228 }
1229 if (options->mount_opt & OCFS2_MOUNT_GRPQUOTA &&
1230 !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1231 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
1232 mlog(ML_ERROR, "Group quotas were requested, but this "
1233 "filesystem does not have the feature enabled.\n");
1234 return 0;
1235 }
1236 if (options->mount_opt & OCFS2_MOUNT_POSIX_ACL &&
1237 !OCFS2_HAS_INCOMPAT_FEATURE(sb, OCFS2_FEATURE_INCOMPAT_XATTR)) {
1238 mlog(ML_ERROR, "ACL support requested but extended attributes "
1239 "feature is not enabled\n");
1240 return 0;
1241 }
1242 /* No ACL setting specified? Use XATTR feature... */
1243 if (!(options->mount_opt & (OCFS2_MOUNT_POSIX_ACL |
1244 OCFS2_MOUNT_NO_POSIX_ACL))) {
1245 if (OCFS2_HAS_INCOMPAT_FEATURE(sb, OCFS2_FEATURE_INCOMPAT_XATTR))
1246 options->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
1247 else
1248 options->mount_opt |= OCFS2_MOUNT_NO_POSIX_ACL;
1249 }
1250 return 1;
1251}
1252
Mark Fashehccd979b2005-12-15 14:31:24 -08001253static int ocfs2_parse_options(struct super_block *sb,
1254 char *options,
Tiger Yangc0123ad2007-09-08 00:16:10 +08001255 struct mount_options *mopt,
Mark Fashehccd979b2005-12-15 14:31:24 -08001256 int is_remount)
1257{
Mark Fasheh52c303c2011-01-31 11:31:04 -08001258 int status, user_stack = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001259 char *p;
Sunil Mushran2c442712010-10-07 15:23:50 -07001260 u32 tmp;
Norton.Zhu3eb5bdf2016-01-14 15:17:09 -08001261 int token, option;
1262 substring_t args[MAX_OPT_ARGS];
Mark Fashehccd979b2005-12-15 14:31:24 -08001263
Tao Ma32a42d32011-02-23 21:29:08 +08001264 trace_ocfs2_parse_options(is_remount, options ? options : "(none)");
Mark Fashehccd979b2005-12-15 14:31:24 -08001265
Mark Fashehd147b3d2007-11-07 14:40:36 -08001266 mopt->commit_interval = 0;
Sunil Mushran4b37fcb2010-04-13 18:00:31 -07001267 mopt->mount_opt = OCFS2_MOUNT_NOINTR;
Tiger Yangc0123ad2007-09-08 00:16:10 +08001268 mopt->atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
1269 mopt->slot = OCFS2_INVALID_SLOT;
Mark Fasheh73c8a802010-04-05 18:17:13 -07001270 mopt->localalloc_opt = -1;
Joel Beckerb61817e2008-02-01 15:08:23 -08001271 mopt->cluster_stack[0] = '\0';
Mark Fashehd02f00c2009-12-07 13:10:48 -08001272 mopt->resv_level = OCFS2_DEFAULT_RESV_LEVEL;
Mark Fasheh83f92312010-04-05 18:17:16 -07001273 mopt->dir_resv_level = -1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001274
1275 if (!options) {
1276 status = 1;
1277 goto bail;
1278 }
1279
1280 while ((p = strsep(&options, ",")) != NULL) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001281 if (!*p)
1282 continue;
1283
1284 token = match_token(p, tokens, args);
1285 switch (token) {
1286 case Opt_hb_local:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001287 mopt->mount_opt |= OCFS2_MOUNT_HB_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001288 break;
1289 case Opt_hb_none:
Sunil Mushran2c442712010-10-07 15:23:50 -07001290 mopt->mount_opt |= OCFS2_MOUNT_HB_NONE;
1291 break;
1292 case Opt_hb_global:
1293 mopt->mount_opt |= OCFS2_MOUNT_HB_GLOBAL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001294 break;
1295 case Opt_barrier:
1296 if (match_int(&args[0], &option)) {
1297 status = 0;
1298 goto bail;
1299 }
1300 if (option)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001301 mopt->mount_opt |= OCFS2_MOUNT_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -08001302 else
Tiger Yangc0123ad2007-09-08 00:16:10 +08001303 mopt->mount_opt &= ~OCFS2_MOUNT_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -08001304 break;
1305 case Opt_intr:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001306 mopt->mount_opt &= ~OCFS2_MOUNT_NOINTR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001307 break;
1308 case Opt_nointr:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001309 mopt->mount_opt |= OCFS2_MOUNT_NOINTR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001310 break;
1311 case Opt_err_panic:
Goldwyn Rodrigues7d0fb912015-09-04 15:44:11 -07001312 mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_CONT;
1313 mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_ROFS;
Tiger Yangc0123ad2007-09-08 00:16:10 +08001314 mopt->mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001315 break;
1316 case Opt_err_ro:
Goldwyn Rodrigues7d0fb912015-09-04 15:44:11 -07001317 mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_CONT;
Tiger Yangc0123ad2007-09-08 00:16:10 +08001318 mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_PANIC;
Goldwyn Rodrigues7d0fb912015-09-04 15:44:11 -07001319 mopt->mount_opt |= OCFS2_MOUNT_ERRORS_ROFS;
1320 break;
1321 case Opt_err_cont:
1322 mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_ROFS;
1323 mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_PANIC;
1324 mopt->mount_opt |= OCFS2_MOUNT_ERRORS_CONT;
Mark Fashehccd979b2005-12-15 14:31:24 -08001325 break;
1326 case Opt_data_ordered:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001327 mopt->mount_opt &= ~OCFS2_MOUNT_DATA_WRITEBACK;
Mark Fashehccd979b2005-12-15 14:31:24 -08001328 break;
1329 case Opt_data_writeback:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001330 mopt->mount_opt |= OCFS2_MOUNT_DATA_WRITEBACK;
Mark Fashehccd979b2005-12-15 14:31:24 -08001331 break;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001332 case Opt_user_xattr:
1333 mopt->mount_opt &= ~OCFS2_MOUNT_NOUSERXATTR;
1334 break;
1335 case Opt_nouser_xattr:
1336 mopt->mount_opt |= OCFS2_MOUNT_NOUSERXATTR;
1337 break;
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001338 case Opt_atime_quantum:
1339 if (match_int(&args[0], &option)) {
1340 status = 0;
1341 goto bail;
1342 }
1343 if (option >= 0)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001344 mopt->atime_quantum = option;
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001345 break;
Sunil Mushranbaf46612007-06-18 17:00:24 -07001346 case Opt_slot:
Sunil Mushranbaf46612007-06-18 17:00:24 -07001347 if (match_int(&args[0], &option)) {
1348 status = 0;
1349 goto bail;
1350 }
1351 if (option)
Junxiao Bi38d51b22020-08-06 23:18:02 -07001352 mopt->slot = (u16)option;
Sunil Mushranbaf46612007-06-18 17:00:24 -07001353 break;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001354 case Opt_commit:
Mark Fashehd147b3d2007-11-07 14:40:36 -08001355 if (match_int(&args[0], &option)) {
1356 status = 0;
1357 goto bail;
1358 }
1359 if (option < 0)
1360 return 0;
1361 if (option == 0)
Joel Becker2b4e30f2008-09-03 20:03:41 -07001362 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001363 mopt->commit_interval = HZ * option;
1364 break;
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001365 case Opt_localalloc:
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001366 if (match_int(&args[0], &option)) {
1367 status = 0;
1368 goto bail;
1369 }
Mark Fasheh73c8a802010-04-05 18:17:13 -07001370 if (option >= 0)
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001371 mopt->localalloc_opt = option;
1372 break;
Mark Fasheh53fc6222007-12-20 16:49:04 -08001373 case Opt_localflocks:
1374 /*
1375 * Changing this during remount could race
1376 * flock() requests, or "unbalance" existing
1377 * ones (e.g., a lock is taken in one mode but
1378 * dropped in the other). If users care enough
1379 * to flip locking modes during remount, we
1380 * could add a "local" flag to individual
1381 * flock structures for proper tracking of
1382 * state.
1383 */
1384 if (!is_remount)
1385 mopt->mount_opt |= OCFS2_MOUNT_LOCALFLOCKS;
1386 break;
Joel Beckerb61817e2008-02-01 15:08:23 -08001387 case Opt_stack:
1388 /* Check both that the option we were passed
1389 * is of the right length and that it is a proper
1390 * string of the right length.
1391 */
1392 if (((args[0].to - args[0].from) !=
1393 OCFS2_STACK_LABEL_LEN) ||
1394 (strnlen(args[0].from,
1395 OCFS2_STACK_LABEL_LEN) !=
1396 OCFS2_STACK_LABEL_LEN)) {
1397 mlog(ML_ERROR,
1398 "Invalid cluster_stack option\n");
1399 status = 0;
1400 goto bail;
1401 }
1402 memcpy(mopt->cluster_stack, args[0].from,
1403 OCFS2_STACK_LABEL_LEN);
1404 mopt->cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
Mark Fasheh52c303c2011-01-31 11:31:04 -08001405 /*
1406 * Open code the memcmp here as we don't have
1407 * an osb to pass to
1408 * ocfs2_userspace_stack().
1409 */
1410 if (memcmp(mopt->cluster_stack,
1411 OCFS2_CLASSIC_CLUSTER_STACK,
1412 OCFS2_STACK_LABEL_LEN))
1413 user_stack = 1;
Joel Beckerb61817e2008-02-01 15:08:23 -08001414 break;
Joel Becker12462f12008-09-03 20:03:40 -07001415 case Opt_inode64:
1416 mopt->mount_opt |= OCFS2_MOUNT_INODE64;
1417 break;
Jan Kara19ece542008-08-21 20:13:17 +02001418 case Opt_usrquota:
Jan Kara19ece542008-08-21 20:13:17 +02001419 mopt->mount_opt |= OCFS2_MOUNT_USRQUOTA;
1420 break;
1421 case Opt_grpquota:
Jan Kara19ece542008-08-21 20:13:17 +02001422 mopt->mount_opt |= OCFS2_MOUNT_GRPQUOTA;
1423 break;
Tristan Ye7bdb0d12010-10-11 16:46:39 +08001424 case Opt_coherency_buffered:
1425 mopt->mount_opt |= OCFS2_MOUNT_COHERENCY_BUFFERED;
1426 break;
1427 case Opt_coherency_full:
1428 mopt->mount_opt &= ~OCFS2_MOUNT_COHERENCY_BUFFERED;
1429 break;
Tiger Yanga68979b2008-11-14 11:17:52 +08001430 case Opt_acl:
1431 mopt->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
Jan Kara5297aad2009-10-15 14:54:04 +02001432 mopt->mount_opt &= ~OCFS2_MOUNT_NO_POSIX_ACL;
Tiger Yanga68979b2008-11-14 11:17:52 +08001433 break;
1434 case Opt_noacl:
Jan Kara5297aad2009-10-15 14:54:04 +02001435 mopt->mount_opt |= OCFS2_MOUNT_NO_POSIX_ACL;
Tiger Yanga68979b2008-11-14 11:17:52 +08001436 mopt->mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
1437 break;
Mark Fashehd02f00c2009-12-07 13:10:48 -08001438 case Opt_resv_level:
1439 if (is_remount)
1440 break;
1441 if (match_int(&args[0], &option)) {
1442 status = 0;
1443 goto bail;
1444 }
1445 if (option >= OCFS2_MIN_RESV_LEVEL &&
1446 option < OCFS2_MAX_RESV_LEVEL)
1447 mopt->resv_level = option;
1448 break;
Mark Fasheh83f92312010-04-05 18:17:16 -07001449 case Opt_dir_resv_level:
1450 if (is_remount)
1451 break;
1452 if (match_int(&args[0], &option)) {
1453 status = 0;
1454 goto bail;
1455 }
1456 if (option >= OCFS2_MIN_RESV_LEVEL &&
1457 option < OCFS2_MAX_RESV_LEVEL)
1458 mopt->dir_resv_level = option;
1459 break;
alex chen1dfeb762015-02-10 14:09:04 -08001460 case Opt_journal_async_commit:
1461 mopt->mount_opt |= OCFS2_MOUNT_JOURNAL_ASYNC_COMMIT;
1462 break;
Gang He912f6552020-06-01 21:45:29 -07001463 case Opt_nocluster:
1464 mopt->mount_opt |= OCFS2_MOUNT_NOCLUSTER;
1465 break;
Mark Fashehccd979b2005-12-15 14:31:24 -08001466 default:
1467 mlog(ML_ERROR,
1468 "Unrecognized mount option \"%s\" "
1469 "or missing value\n", p);
1470 status = 0;
1471 goto bail;
1472 }
1473 }
1474
Mark Fasheh52c303c2011-01-31 11:31:04 -08001475 if (user_stack == 0) {
1476 /* Ensure only one heartbeat mode */
1477 tmp = mopt->mount_opt & (OCFS2_MOUNT_HB_LOCAL |
1478 OCFS2_MOUNT_HB_GLOBAL |
1479 OCFS2_MOUNT_HB_NONE);
1480 if (hweight32(tmp) != 1) {
1481 mlog(ML_ERROR, "Invalid heartbeat mount options\n");
1482 status = 0;
1483 goto bail;
1484 }
Sunil Mushran2c442712010-10-07 15:23:50 -07001485 }
1486
Mark Fashehccd979b2005-12-15 14:31:24 -08001487 status = 1;
1488
1489bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001490 return status;
1491}
1492
Al Viro34c80b12011-12-08 21:32:45 -05001493static int ocfs2_show_options(struct seq_file *s, struct dentry *root)
Sunil Mushrand5500712007-09-06 13:34:16 -07001494{
Al Viro34c80b12011-12-08 21:32:45 -05001495 struct ocfs2_super *osb = OCFS2_SB(root->d_sb);
Sunil Mushrand5500712007-09-06 13:34:16 -07001496 unsigned long opts = osb->s_mount_opt;
Mark Fashehebcee4b2008-07-28 14:55:20 -07001497 unsigned int local_alloc_megs;
Sunil Mushrand5500712007-09-06 13:34:16 -07001498
Sunil Mushran2c442712010-10-07 15:23:50 -07001499 if (opts & (OCFS2_MOUNT_HB_LOCAL | OCFS2_MOUNT_HB_GLOBAL)) {
1500 seq_printf(s, ",_netdev");
1501 if (opts & OCFS2_MOUNT_HB_LOCAL)
1502 seq_printf(s, ",%s", OCFS2_HB_LOCAL);
1503 else
1504 seq_printf(s, ",%s", OCFS2_HB_GLOBAL);
1505 } else
1506 seq_printf(s, ",%s", OCFS2_HB_NONE);
Sunil Mushrand5500712007-09-06 13:34:16 -07001507
1508 if (opts & OCFS2_MOUNT_NOINTR)
1509 seq_printf(s, ",nointr");
1510
1511 if (opts & OCFS2_MOUNT_DATA_WRITEBACK)
1512 seq_printf(s, ",data=writeback");
1513 else
1514 seq_printf(s, ",data=ordered");
1515
1516 if (opts & OCFS2_MOUNT_BARRIER)
1517 seq_printf(s, ",barrier=1");
1518
1519 if (opts & OCFS2_MOUNT_ERRORS_PANIC)
1520 seq_printf(s, ",errors=panic");
Goldwyn Rodrigues7d0fb912015-09-04 15:44:11 -07001521 else if (opts & OCFS2_MOUNT_ERRORS_CONT)
1522 seq_printf(s, ",errors=continue");
Sunil Mushrand5500712007-09-06 13:34:16 -07001523 else
1524 seq_printf(s, ",errors=remount-ro");
1525
1526 if (osb->preferred_slot != OCFS2_INVALID_SLOT)
1527 seq_printf(s, ",preferred_slot=%d", osb->preferred_slot);
1528
Al Viro34c80b12011-12-08 21:32:45 -05001529 seq_printf(s, ",atime_quantum=%u", osb->s_atime_quantum);
Sunil Mushrand5500712007-09-06 13:34:16 -07001530
Mark Fashehd147b3d2007-11-07 14:40:36 -08001531 if (osb->osb_commit_interval)
1532 seq_printf(s, ",commit=%u",
1533 (unsigned) (osb->osb_commit_interval / HZ));
1534
Mark Fashehebcee4b2008-07-28 14:55:20 -07001535 local_alloc_megs = osb->local_alloc_bits >> (20 - osb->s_clustersize_bits);
Mark Fasheh6b820212010-04-05 18:17:14 -07001536 if (local_alloc_megs != ocfs2_la_default_mb(osb))
Mark Fashehebcee4b2008-07-28 14:55:20 -07001537 seq_printf(s, ",localalloc=%d", local_alloc_megs);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001538
Mark Fasheh53fc6222007-12-20 16:49:04 -08001539 if (opts & OCFS2_MOUNT_LOCALFLOCKS)
1540 seq_printf(s, ",localflocks,");
1541
Joel Beckerb61817e2008-02-01 15:08:23 -08001542 if (osb->osb_cluster_stack[0])
Kees Cooka068acf2015-09-04 15:44:57 -07001543 seq_show_option_n(s, "cluster_stack", osb->osb_cluster_stack,
1544 OCFS2_STACK_LABEL_LEN);
Jan Kara19ece542008-08-21 20:13:17 +02001545 if (opts & OCFS2_MOUNT_USRQUOTA)
1546 seq_printf(s, ",usrquota");
1547 if (opts & OCFS2_MOUNT_GRPQUOTA)
1548 seq_printf(s, ",grpquota");
Joel Beckerb61817e2008-02-01 15:08:23 -08001549
Tristan Ye7bdb0d12010-10-11 16:46:39 +08001550 if (opts & OCFS2_MOUNT_COHERENCY_BUFFERED)
1551 seq_printf(s, ",coherency=buffered");
1552 else
1553 seq_printf(s, ",coherency=full");
1554
Sunil Mushranb0f73cf2008-09-05 11:29:14 -07001555 if (opts & OCFS2_MOUNT_NOUSERXATTR)
1556 seq_printf(s, ",nouser_xattr");
1557 else
1558 seq_printf(s, ",user_xattr");
1559
Joel Becker12462f12008-09-03 20:03:40 -07001560 if (opts & OCFS2_MOUNT_INODE64)
1561 seq_printf(s, ",inode64");
1562
Tiger Yanga68979b2008-11-14 11:17:52 +08001563 if (opts & OCFS2_MOUNT_POSIX_ACL)
1564 seq_printf(s, ",acl");
1565 else
1566 seq_printf(s, ",noacl");
Tiger Yanga68979b2008-11-14 11:17:52 +08001567
Mark Fashehd02f00c2009-12-07 13:10:48 -08001568 if (osb->osb_resv_level != OCFS2_DEFAULT_RESV_LEVEL)
1569 seq_printf(s, ",resv_level=%d", osb->osb_resv_level);
1570
Mark Fasheh83f92312010-04-05 18:17:16 -07001571 if (osb->osb_dir_resv_level != osb->osb_resv_level)
1572 seq_printf(s, ",dir_resv_level=%d", osb->osb_resv_level);
1573
alex chen1dfeb762015-02-10 14:09:04 -08001574 if (opts & OCFS2_MOUNT_JOURNAL_ASYNC_COMMIT)
1575 seq_printf(s, ",journal_async_commit");
1576
Gang He912f6552020-06-01 21:45:29 -07001577 if (opts & OCFS2_MOUNT_NOCLUSTER)
1578 seq_printf(s, ",nocluster");
1579
Sunil Mushrand5500712007-09-06 13:34:16 -07001580 return 0;
1581}
1582
Mark Fashehccd979b2005-12-15 14:31:24 -08001583static int __init ocfs2_init(void)
1584{
Wengang Wangc18ceab2014-04-03 14:46:46 -07001585 int status;
Mark Fasheha11f7e62011-06-22 14:23:38 -07001586
Mark Fashehccd979b2005-12-15 14:31:24 -08001587 status = init_ocfs2_uptodate_cache();
Al Viro342827d2012-03-17 22:03:58 -04001588 if (status < 0)
1589 goto out1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001590
1591 status = ocfs2_initialize_mem_caches();
Al Viro342827d2012-03-17 22:03:58 -04001592 if (status < 0)
1593 goto out2;
Mark Fashehccd979b2005-12-15 14:31:24 -08001594
Mark Fashehccd979b2005-12-15 14:31:24 -08001595 ocfs2_debugfs_root = debugfs_create_dir("ocfs2", NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001596
Joel Becker63e0c482008-01-30 16:58:36 -08001597 ocfs2_set_locking_protocol();
1598
Jan Kara9e33d692008-08-25 19:56:50 +02001599 status = register_quota_format(&ocfs2_quota_format);
Al Viro342827d2012-03-17 22:03:58 -04001600 if (status < 0)
jiangyiwen35ddf782016-03-25 14:21:32 -07001601 goto out3;
Al Viro342827d2012-03-17 22:03:58 -04001602 status = register_filesystem(&ocfs2_fs_type);
1603 if (!status)
1604 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001605
Al Viro342827d2012-03-17 22:03:58 -04001606 unregister_quota_format(&ocfs2_quota_format);
Al Viro342827d2012-03-17 22:03:58 -04001607out3:
jiangyiwen35ddf782016-03-25 14:21:32 -07001608 debugfs_remove(ocfs2_debugfs_root);
Al Viro342827d2012-03-17 22:03:58 -04001609 ocfs2_free_mem_caches();
1610out2:
1611 exit_ocfs2_uptodate_cache();
1612out1:
1613 mlog_errno(status);
1614 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001615}
1616
1617static void __exit ocfs2_exit(void)
1618{
Jan Kara9e33d692008-08-25 19:56:50 +02001619 unregister_quota_format(&ocfs2_quota_format);
1620
Mark Fashehccd979b2005-12-15 14:31:24 -08001621 debugfs_remove(ocfs2_debugfs_root);
1622
1623 ocfs2_free_mem_caches();
1624
1625 unregister_filesystem(&ocfs2_fs_type);
1626
Mark Fashehccd979b2005-12-15 14:31:24 -08001627 exit_ocfs2_uptodate_cache();
Mark Fashehccd979b2005-12-15 14:31:24 -08001628}
1629
1630static void ocfs2_put_super(struct super_block *sb)
1631{
Tao Ma32a42d32011-02-23 21:29:08 +08001632 trace_ocfs2_put_super(sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001633
1634 ocfs2_sync_blockdev(sb);
1635 ocfs2_dismount_volume(sb, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001636}
1637
David Howells726c3342006-06-23 02:02:58 -07001638static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
Mark Fashehccd979b2005-12-15 14:31:24 -08001639{
1640 struct ocfs2_super *osb;
1641 u32 numbits, freebits;
1642 int status;
1643 struct ocfs2_dinode *bm_lock;
1644 struct buffer_head *bh = NULL;
1645 struct inode *inode = NULL;
1646
Tao Ma32a42d32011-02-23 21:29:08 +08001647 trace_ocfs2_statfs(dentry->d_sb, buf);
Mark Fashehccd979b2005-12-15 14:31:24 -08001648
David Howells726c3342006-06-23 02:02:58 -07001649 osb = OCFS2_SB(dentry->d_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001650
1651 inode = ocfs2_get_system_file_inode(osb,
1652 GLOBAL_BITMAP_SYSTEM_INODE,
1653 OCFS2_INVALID_SLOT);
1654 if (!inode) {
1655 mlog(ML_ERROR, "failed to get bitmap inode\n");
1656 status = -EIO;
1657 goto bail;
1658 }
1659
Mark Fashehe63aecb62007-10-18 15:30:42 -07001660 status = ocfs2_inode_lock(inode, &bh, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001661 if (status < 0) {
1662 mlog_errno(status);
1663 goto bail;
1664 }
1665
1666 bm_lock = (struct ocfs2_dinode *) bh->b_data;
1667
1668 numbits = le32_to_cpu(bm_lock->id1.bitmap1.i_total);
1669 freebits = numbits - le32_to_cpu(bm_lock->id1.bitmap1.i_used);
1670
1671 buf->f_type = OCFS2_SUPER_MAGIC;
David Howells726c3342006-06-23 02:02:58 -07001672 buf->f_bsize = dentry->d_sb->s_blocksize;
Mark Fashehccd979b2005-12-15 14:31:24 -08001673 buf->f_namelen = OCFS2_MAX_FILENAME_LEN;
1674 buf->f_blocks = ((sector_t) numbits) *
1675 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1676 buf->f_bfree = ((sector_t) freebits) *
1677 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1678 buf->f_bavail = buf->f_bfree;
1679 buf->f_files = numbits;
1680 buf->f_ffree = freebits;
Coly Li837711f2009-01-16 16:33:05 +08001681 buf->f_fsid.val[0] = crc32_le(0, osb->uuid_str, OCFS2_VOL_UUID_LEN)
1682 & 0xFFFFFFFFUL;
1683 buf->f_fsid.val[1] = crc32_le(0, osb->uuid_str + OCFS2_VOL_UUID_LEN,
1684 OCFS2_VOL_UUID_LEN) & 0xFFFFFFFFUL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001685
1686 brelse(bh);
1687
Mark Fashehe63aecb62007-10-18 15:30:42 -07001688 ocfs2_inode_unlock(inode, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001689 status = 0;
1690bail:
Joseph Qi72865d92016-01-14 15:17:27 -08001691 iput(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001692
Tao Mac1e8d352011-03-07 16:43:21 +08001693 if (status)
1694 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001695
1696 return status;
1697}
1698
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001699static void ocfs2_inode_init_once(void *data)
Mark Fashehccd979b2005-12-15 14:31:24 -08001700{
1701 struct ocfs2_inode_info *oi = data;
1702
Christoph Lametera35afb82007-05-16 22:10:57 -07001703 oi->ip_flags = 0;
1704 oi->ip_open_count = 0;
1705 spin_lock_init(&oi->ip_lock);
1706 ocfs2_extent_map_init(&oi->vfs_inode);
1707 INIT_LIST_HEAD(&oi->ip_io_markers);
Ryan Ding4506cfb2016-03-25 14:21:06 -07001708 INIT_LIST_HEAD(&oi->ip_unwritten_list);
Christoph Lametera35afb82007-05-16 22:10:57 -07001709 oi->ip_dir_start_lookup = 0;
Christoph Lametera35afb82007-05-16 22:10:57 -07001710 init_rwsem(&oi->ip_alloc_sem);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001711 init_rwsem(&oi->ip_xattr_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -07001712 mutex_init(&oi->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001713
Christoph Lametera35afb82007-05-16 22:10:57 -07001714 oi->ip_blkno = 0ULL;
1715 oi->ip_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001716
Mark Fasheh4fe370a2009-12-07 13:15:40 -08001717 ocfs2_resv_init_once(&oi->ip_la_data_resv);
1718
Christoph Lametera35afb82007-05-16 22:10:57 -07001719 ocfs2_lock_res_init_once(&oi->ip_rw_lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001720 ocfs2_lock_res_init_once(&oi->ip_inode_lockres);
Christoph Lametera35afb82007-05-16 22:10:57 -07001721 ocfs2_lock_res_init_once(&oi->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001722
Joel Becker8cb471e2009-02-10 20:00:41 -08001723 ocfs2_metadata_cache_init(INODE_CACHE(&oi->vfs_inode),
Joel Becker6e5a3d72009-02-10 19:00:37 -08001724 &ocfs2_inode_caching_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08001725
Christoph Lametera35afb82007-05-16 22:10:57 -07001726 inode_init_once(&oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001727}
1728
1729static int ocfs2_initialize_mem_caches(void)
1730{
1731 ocfs2_inode_cachep = kmem_cache_create("ocfs2_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001732 sizeof(struct ocfs2_inode_info),
1733 0,
1734 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -08001735 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
Paul Mundt20c2df82007-07-20 10:11:58 +09001736 ocfs2_inode_init_once);
Jan Kara9e33d692008-08-25 19:56:50 +02001737 ocfs2_dquot_cachep = kmem_cache_create("ocfs2_dquot_cache",
1738 sizeof(struct ocfs2_dquot),
1739 0,
1740 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1741 SLAB_MEM_SPREAD),
1742 NULL);
1743 ocfs2_qf_chunk_cachep = kmem_cache_create("ocfs2_qf_chunk_cache",
1744 sizeof(struct ocfs2_quota_chunk),
1745 0,
1746 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
1747 NULL);
1748 if (!ocfs2_inode_cachep || !ocfs2_dquot_cachep ||
1749 !ocfs2_qf_chunk_cachep) {
piaojuna17b4852018-04-05 16:18:52 -07001750 kmem_cache_destroy(ocfs2_inode_cachep);
1751 kmem_cache_destroy(ocfs2_dquot_cachep);
1752 kmem_cache_destroy(ocfs2_qf_chunk_cachep);
Mark Fashehccd979b2005-12-15 14:31:24 -08001753 return -ENOMEM;
Jan Kara9e33d692008-08-25 19:56:50 +02001754 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001755
Mark Fashehccd979b2005-12-15 14:31:24 -08001756 return 0;
1757}
1758
1759static void ocfs2_free_mem_caches(void)
1760{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10001761 /*
1762 * Make sure all delayed rcu free inodes are flushed before we
1763 * destroy cache.
1764 */
1765 rcu_barrier();
piaojuna17b4852018-04-05 16:18:52 -07001766 kmem_cache_destroy(ocfs2_inode_cachep);
Mark Fashehccd979b2005-12-15 14:31:24 -08001767 ocfs2_inode_cachep = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +02001768
piaojuna17b4852018-04-05 16:18:52 -07001769 kmem_cache_destroy(ocfs2_dquot_cachep);
Jan Kara9e33d692008-08-25 19:56:50 +02001770 ocfs2_dquot_cachep = NULL;
1771
piaojuna17b4852018-04-05 16:18:52 -07001772 kmem_cache_destroy(ocfs2_qf_chunk_cachep);
Jan Kara9e33d692008-08-25 19:56:50 +02001773 ocfs2_qf_chunk_cachep = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001774}
1775
1776static int ocfs2_get_sector(struct super_block *sb,
1777 struct buffer_head **bh,
1778 int block,
1779 int sect_size)
1780{
1781 if (!sb_set_blocksize(sb, sect_size)) {
1782 mlog(ML_ERROR, "unable to set blocksize\n");
1783 return -EIO;
1784 }
1785
1786 *bh = sb_getblk(sb, block);
1787 if (!*bh) {
Rui Xiang7391a292013-11-12 15:06:54 -08001788 mlog_errno(-ENOMEM);
1789 return -ENOMEM;
Mark Fashehccd979b2005-12-15 14:31:24 -08001790 }
1791 lock_buffer(*bh);
1792 if (!buffer_dirty(*bh))
1793 clear_buffer_uptodate(*bh);
1794 unlock_buffer(*bh);
Mike Christiedfec8a12016-06-05 14:31:44 -05001795 ll_rw_block(REQ_OP_READ, 0, 1, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001796 wait_on_buffer(*bh);
wengang wang28d57d42009-02-13 10:11:47 +08001797 if (!buffer_uptodate(*bh)) {
1798 mlog_errno(-EIO);
1799 brelse(*bh);
1800 *bh = NULL;
1801 return -EIO;
1802 }
1803
Mark Fashehccd979b2005-12-15 14:31:24 -08001804 return 0;
1805}
1806
Mark Fashehccd979b2005-12-15 14:31:24 -08001807static int ocfs2_mount_volume(struct super_block *sb)
1808{
1809 int status = 0;
1810 int unlock_super = 0;
1811 struct ocfs2_super *osb = OCFS2_SB(sb);
1812
Mark Fashehccd979b2005-12-15 14:31:24 -08001813 if (ocfs2_is_hard_readonly(osb))
1814 goto leave;
1815
Gang He5500ab42019-03-05 15:41:45 -08001816 mutex_init(&osb->obs_trim_fs_mutex);
1817
Mark Fashehccd979b2005-12-15 14:31:24 -08001818 status = ocfs2_dlm_init(osb);
1819 if (status < 0) {
1820 mlog_errno(status);
Gang Hea52370b2018-01-31 16:14:29 -08001821 if (status == -EBADR && ocfs2_userspace_stack(osb))
1822 mlog(ML_ERROR, "couldn't mount because cluster name on"
1823 " disk does not match the running cluster name.\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08001824 goto leave;
1825 }
1826
Mark Fashehccd979b2005-12-15 14:31:24 -08001827 status = ocfs2_super_lock(osb, 1);
1828 if (status < 0) {
1829 mlog_errno(status);
1830 goto leave;
1831 }
1832 unlock_super = 1;
1833
1834 /* This will load up the node map and add ourselves to it. */
1835 status = ocfs2_find_slot(osb);
1836 if (status < 0) {
1837 mlog_errno(status);
1838 goto leave;
1839 }
1840
Mark Fashehccd979b2005-12-15 14:31:24 -08001841 /* load all node-local system inodes */
1842 status = ocfs2_init_local_system_inodes(osb);
1843 if (status < 0) {
1844 mlog_errno(status);
1845 goto leave;
1846 }
1847
1848 status = ocfs2_check_volume(osb);
1849 if (status < 0) {
1850 mlog_errno(status);
1851 goto leave;
1852 }
1853
1854 status = ocfs2_truncate_log_init(osb);
Tao Ma06c59bb2009-05-19 06:47:20 +08001855 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08001856 mlog_errno(status);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001857
Mark Fashehccd979b2005-12-15 14:31:24 -08001858leave:
1859 if (unlock_super)
1860 ocfs2_super_unlock(osb, 1);
1861
Mark Fashehccd979b2005-12-15 14:31:24 -08001862 return status;
1863}
1864
Mark Fashehccd979b2005-12-15 14:31:24 -08001865static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
1866{
Joel Becker286eaa92008-02-01 15:03:57 -08001867 int tmp, hangup_needed = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001868 struct ocfs2_super *osb = NULL;
Goldwyn Rodrigues99d7a882013-09-24 15:27:32 -07001869 char nodestr[12];
Mark Fashehccd979b2005-12-15 14:31:24 -08001870
Tao Ma32a42d32011-02-23 21:29:08 +08001871 trace_ocfs2_dismount_volume(sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001872
1873 BUG_ON(!sb);
1874 osb = OCFS2_SB(sb);
1875 BUG_ON(!osb);
1876
Gang He5f483c42018-04-05 16:19:29 -07001877 /* Remove file check sysfs related directores/files,
1878 * and wait for the pending file check operations */
1879 ocfs2_filecheck_remove_sysfs(osb);
1880
1881 kset_unregister(osb->osb_dev_kset);
1882
Sunil Mushran692684e2009-06-19 16:53:17 -07001883 /* Orphan scan should be stopped as early as possible */
1884 ocfs2_orphan_scan_stop(osb);
1885
Jan Kara19ece542008-08-21 20:13:17 +02001886 ocfs2_disable_quotas(osb);
1887
Jan Karae3a767b2014-04-03 14:46:56 -07001888 /* All dquots should be freed by now */
1889 WARN_ON(!llist_empty(&osb->dquot_drop_list));
1890 /* Wait for worker to be done with the work structure in osb */
1891 cancel_work_sync(&osb->dquot_drop_work);
1892
Mark Fashehccd979b2005-12-15 14:31:24 -08001893 ocfs2_shutdown_local_alloc(osb);
1894
Xue jiufeib253bfd2014-06-23 13:22:08 -07001895 ocfs2_truncate_log_shutdown(osb);
1896
Joel Becker553abd02008-02-01 12:03:57 -08001897 /* This will disable recovery and flush any recovery work. */
1898 ocfs2_recovery_exit(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001899
1900 ocfs2_journal_shutdown(osb);
1901
1902 ocfs2_sync_blockdev(sb);
1903
Tao Ma374a2632009-08-24 11:13:37 +08001904 ocfs2_purge_refcount_trees(osb);
1905
Joel Becker4670c462008-02-01 14:39:35 -08001906 /* No cluster connection means we've failed during mount, so skip
1907 * all the steps which depended on that to complete. */
1908 if (osb->cconn) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001909 tmp = ocfs2_super_lock(osb, 1);
1910 if (tmp < 0) {
1911 mlog_errno(tmp);
1912 return;
1913 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001914 }
1915
Mark Fasheh19b613d2007-10-04 14:47:09 -07001916 if (osb->slot_num != OCFS2_INVALID_SLOT)
1917 ocfs2_put_slot(osb);
1918
Joel Becker4670c462008-02-01 14:39:35 -08001919 if (osb->cconn)
Mark Fasheh19b613d2007-10-04 14:47:09 -07001920 ocfs2_super_unlock(osb, 1);
1921
Mark Fashehccd979b2005-12-15 14:31:24 -08001922 ocfs2_release_system_inodes(osb);
1923
Joel Becker6953b4c2008-01-29 16:59:56 -08001924 /*
Joel Becker6953b4c2008-01-29 16:59:56 -08001925 * If we're dismounting due to mount error, mount.ocfs2 will clean
1926 * up heartbeat. If we're a local mount, there is no heartbeat.
1927 * If we failed before we got a uuid_str yet, we can't stop
1928 * heartbeat. Otherwise, do it.
1929 */
Tiger Yang03efed82011-05-28 00:34:19 +08001930 if (!mnt_err && !ocfs2_mount_local(osb) && osb->uuid_str &&
1931 !ocfs2_is_hard_readonly(osb))
Joel Becker286eaa92008-02-01 15:03:57 -08001932 hangup_needed = 1;
1933
1934 if (osb->cconn)
1935 ocfs2_dlm_shutdown(osb, hangup_needed);
1936
Joel Becker73be1922009-01-06 14:57:08 -08001937 ocfs2_blockcheck_stats_debugfs_remove(&osb->osb_ecc_stats);
Greg Kroah-Hartman5e7a3ed2019-09-23 15:33:15 -07001938 debugfs_remove_recursive(osb->osb_debug_root);
Joel Becker286eaa92008-02-01 15:03:57 -08001939
1940 if (hangup_needed)
Joel Becker6953b4c2008-01-29 16:59:56 -08001941 ocfs2_cluster_hangup(osb->uuid_str, strlen(osb->uuid_str));
Mark Fashehccd979b2005-12-15 14:31:24 -08001942
1943 atomic_set(&osb->vol_state, VOLUME_DISMOUNTED);
1944
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001945 if (ocfs2_mount_local(osb))
1946 snprintf(nodestr, sizeof(nodestr), "local");
1947 else
Joel Becker19fdb622008-01-30 15:38:24 -08001948 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001949
1950 printk(KERN_INFO "ocfs2: Unmounting device (%s) on (node %s)\n",
1951 osb->dev_str, nodestr);
Mark Fashehccd979b2005-12-15 14:31:24 -08001952
1953 ocfs2_delete_osb(osb);
1954 kfree(osb);
1955 sb->s_dev = 0;
1956 sb->s_fs_info = NULL;
1957}
1958
1959static int ocfs2_setup_osb_uuid(struct ocfs2_super *osb, const unsigned char *uuid,
1960 unsigned uuid_bytes)
1961{
1962 int i, ret;
1963 char *ptr;
1964
1965 BUG_ON(uuid_bytes != OCFS2_VOL_UUID_LEN);
1966
Robert P. J. Daycd861282006-12-13 00:34:52 -08001967 osb->uuid_str = kzalloc(OCFS2_VOL_UUID_LEN * 2 + 1, GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001968 if (osb->uuid_str == NULL)
1969 return -ENOMEM;
1970
Mark Fashehccd979b2005-12-15 14:31:24 -08001971 for (i = 0, ptr = osb->uuid_str; i < OCFS2_VOL_UUID_LEN; i++) {
1972 /* print with null */
1973 ret = snprintf(ptr, 3, "%02X", uuid[i]);
1974 if (ret != 2) /* drop super cleans up */
1975 return -EINVAL;
1976 /* then only advance past the last char */
1977 ptr += 2;
1978 }
1979
1980 return 0;
1981}
1982
Patrick J. LoPresti3bdb8ef2010-07-22 15:05:57 -07001983/* Make sure entire volume is addressable by our journal. Requires
1984 osb_clusters_at_boot to be valid and for the journal to have been
1985 initialized by ocfs2_journal_init(). */
1986static int ocfs2_journal_addressable(struct ocfs2_super *osb)
1987{
1988 int status = 0;
1989 u64 max_block =
1990 ocfs2_clusters_to_blocks(osb->sb,
1991 osb->osb_clusters_at_boot) - 1;
1992
1993 /* 32-bit block number is always OK. */
1994 if (max_block <= (u32)~0ULL)
1995 goto out;
1996
1997 /* Volume is "huge", so see if our journal is new enough to
1998 support it. */
1999 if (!(OCFS2_HAS_COMPAT_FEATURE(osb->sb,
2000 OCFS2_FEATURE_COMPAT_JBD2_SB) &&
2001 jbd2_journal_check_used_features(osb->journal->j_journal, 0, 0,
2002 JBD2_FEATURE_INCOMPAT_64BIT))) {
2003 mlog(ML_ERROR, "The journal cannot address the entire volume. "
2004 "Enable the 'block64' journal option with tunefs.ocfs2");
2005 status = -EFBIG;
2006 goto out;
2007 }
2008
2009 out:
2010 return status;
2011}
2012
Mark Fashehccd979b2005-12-15 14:31:24 -08002013static int ocfs2_initialize_super(struct super_block *sb,
2014 struct buffer_head *bh,
Joel Becker73be1922009-01-06 14:57:08 -08002015 int sector_size,
2016 struct ocfs2_blockcheck_stats *stats)
Mark Fashehccd979b2005-12-15 14:31:24 -08002017{
Denis Chengbddb8eb32007-09-27 02:10:04 +08002018 int status;
Mark Fasheh5a254032007-07-20 12:56:16 -07002019 int i, cbits, bbits;
2020 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08002021 struct inode *inode = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002022 struct ocfs2_journal *journal;
Mark Fashehccd979b2005-12-15 14:31:24 -08002023 struct ocfs2_super *osb;
Patrick J. LoPresti3bdb8ef2010-07-22 15:05:57 -07002024 u64 total_blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -08002025
Robert P. J. Daycd861282006-12-13 00:34:52 -08002026 osb = kzalloc(sizeof(struct ocfs2_super), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08002027 if (!osb) {
2028 status = -ENOMEM;
2029 mlog_errno(status);
2030 goto bail;
2031 }
2032
2033 sb->s_fs_info = osb;
2034 sb->s_op = &ocfs2_sops;
Al Viroba871672010-12-18 12:10:00 -05002035 sb->s_d_op = &ocfs2_dentry_ops;
Mark Fashehccd979b2005-12-15 14:31:24 -08002036 sb->s_export_op = &ocfs2_export_ops;
Jan Kara664dbd52014-10-08 18:30:19 +02002037 sb->s_qcop = &dquot_quotactl_sysfile_ops;
Jan Kara19ece542008-08-21 20:13:17 +02002038 sb->dq_op = &ocfs2_quota_operations;
Jan Kara1c92ec62014-09-29 15:02:51 +02002039 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002040 sb->s_xattr = ocfs2_xattr_handlers;
Mark Fashehe0dceaf2007-08-09 16:52:30 -07002041 sb->s_time_gran = 1;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002042 sb->s_flags |= SB_NOATIME;
Mark Fashehccd979b2005-12-15 14:31:24 -08002043 /* this is needed to support O_LARGEFILE */
Mark Fasheh5a254032007-07-20 12:56:16 -07002044 cbits = le32_to_cpu(di->id2.i_super.s_clustersize_bits);
2045 bbits = le32_to_cpu(di->id2.i_super.s_blocksize_bits);
2046 sb->s_maxbytes = ocfs2_max_file_offset(bbits, cbits);
Christoph Hellwig85787092017-05-10 15:06:33 +02002047 memcpy(&sb->s_uuid, di->id2.i_super.s_uuid,
Vladimir Davydov58be19d2015-04-14 15:46:39 -07002048 sizeof(di->id2.i_super.s_uuid));
Mark Fashehccd979b2005-12-15 14:31:24 -08002049
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002050 osb->osb_dx_mask = (1 << (cbits - bbits)) - 1;
2051
2052 for (i = 0; i < 3; i++)
2053 osb->osb_dx_seed[i] = le32_to_cpu(di->id2.i_super.s_dx_seed[i]);
2054 osb->osb_dx_seed[3] = le32_to_cpu(di->id2.i_super.s_uuid_hash);
2055
Mark Fashehccd979b2005-12-15 14:31:24 -08002056 osb->sb = sb;
Mark Fashehccd979b2005-12-15 14:31:24 -08002057 osb->s_sectsize_bits = blksize_bits(sector_size);
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01002058 BUG_ON(!osb->s_sectsize_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08002059
Mark Fasheh34d024f2007-09-24 15:56:19 -07002060 spin_lock_init(&osb->dc_task_lock);
2061 init_waitqueue_head(&osb->dc_event);
2062 osb->dc_work_sequence = 0;
2063 osb->dc_wake_sequence = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002064 INIT_LIST_HEAD(&osb->blocked_lock_list);
2065 osb->blocked_lock_count = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002066 spin_lock_init(&osb->osb_lock);
Tao Mac8b9cf92009-02-24 17:40:26 -08002067 spin_lock_init(&osb->osb_xattr_lock);
Tiger Yangb89c5422010-01-25 14:11:06 +08002068 ocfs2_init_steal_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002069
jiangyiwen43b10a22014-04-03 14:47:13 -07002070 mutex_init(&osb->system_file_mutex);
2071
Mark Fashehccd979b2005-12-15 14:31:24 -08002072 atomic_set(&osb->alloc_stats.moves, 0);
2073 atomic_set(&osb->alloc_stats.local_data, 0);
2074 atomic_set(&osb->alloc_stats.bitmap_data, 0);
2075 atomic_set(&osb->alloc_stats.bg_allocs, 0);
2076 atomic_set(&osb->alloc_stats.bg_extends, 0);
2077
Joel Becker73be1922009-01-06 14:57:08 -08002078 /* Copy the blockcheck stats from the superblock probe */
2079 osb->osb_ecc_stats = *stats;
2080
Mark Fashehccd979b2005-12-15 14:31:24 -08002081 ocfs2_init_node_maps(osb);
2082
2083 snprintf(osb->dev_str, sizeof(osb->dev_str), "%u,%u",
2084 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
2085
Goldwyn Rodrigues75d9bbc2010-10-11 12:57:09 -05002086 osb->max_slots = le16_to_cpu(di->id2.i_super.s_max_slots);
2087 if (osb->max_slots > OCFS2_MAX_SLOTS || osb->max_slots == 0) {
2088 mlog(ML_ERROR, "Invalid number of node slots (%u)\n",
2089 osb->max_slots);
2090 status = -EINVAL;
2091 goto bail;
2092 }
Goldwyn Rodrigues75d9bbc2010-10-11 12:57:09 -05002093
Jeff Mahoney8b712cd2009-07-07 17:22:12 -04002094 ocfs2_orphan_scan_init(osb);
2095
Joel Becker553abd02008-02-01 12:03:57 -08002096 status = ocfs2_recovery_init(osb);
2097 if (status) {
2098 mlog(ML_ERROR, "Unable to initialize recovery state\n");
2099 mlog_errno(status);
2100 goto bail;
2101 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002102
2103 init_waitqueue_head(&osb->checkpoint_event);
Mark Fashehccd979b2005-12-15 14:31:24 -08002104
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002105 osb->s_atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
2106
Mark Fashehccd979b2005-12-15 14:31:24 -08002107 osb->slot_num = OCFS2_INVALID_SLOT;
2108
Tiger Yang8154da32008-08-18 17:11:46 +08002109 osb->s_xattr_inline_size = le16_to_cpu(
2110 di->id2.i_super.s_xattr_inline_size);
Tiger Yangfdd77702008-08-18 17:08:55 +08002111
Mark Fashehccd979b2005-12-15 14:31:24 -08002112 osb->local_alloc_state = OCFS2_LA_UNUSED;
2113 osb->local_alloc_bh = NULL;
Mark Fasheh9c7af402008-07-28 18:02:53 -07002114 INIT_DELAYED_WORK(&osb->la_enable_wq, ocfs2_la_enable_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08002115
Mark Fashehccd979b2005-12-15 14:31:24 -08002116 init_waitqueue_head(&osb->osb_mount_event);
2117
Mark Fashehd02f00c2009-12-07 13:10:48 -08002118 status = ocfs2_resmap_init(osb, &osb->osb_la_resmap);
2119 if (status) {
2120 mlog_errno(status);
2121 goto bail;
2122 }
2123
Mark Fashehccd979b2005-12-15 14:31:24 -08002124 osb->vol_label = kmalloc(OCFS2_MAX_VOL_LABEL_LEN, GFP_KERNEL);
2125 if (!osb->vol_label) {
2126 mlog(ML_ERROR, "unable to alloc vol label\n");
2127 status = -ENOMEM;
2128 goto bail;
2129 }
2130
Sunil Mushran539d8262008-07-14 17:31:10 -07002131 osb->slot_recovery_generations =
2132 kcalloc(osb->max_slots, sizeof(*osb->slot_recovery_generations),
2133 GFP_KERNEL);
2134 if (!osb->slot_recovery_generations) {
2135 status = -ENOMEM;
2136 mlog_errno(status);
2137 goto bail;
2138 }
2139
Mark Fashehb4df6ed2006-02-22 17:35:08 -08002140 init_waitqueue_head(&osb->osb_wipe_event);
2141 osb->osb_orphan_wipes = kcalloc(osb->max_slots,
2142 sizeof(*osb->osb_orphan_wipes),
2143 GFP_KERNEL);
2144 if (!osb->osb_orphan_wipes) {
2145 status = -ENOMEM;
2146 mlog_errno(status);
2147 goto bail;
2148 }
2149
Tao Ma374a2632009-08-24 11:13:37 +08002150 osb->osb_rf_lock_tree = RB_ROOT;
2151
Mark Fashehccd979b2005-12-15 14:31:24 -08002152 osb->s_feature_compat =
2153 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_compat);
2154 osb->s_feature_ro_compat =
2155 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_ro_compat);
2156 osb->s_feature_incompat =
2157 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_incompat);
2158
2159 if ((i = OCFS2_HAS_INCOMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_INCOMPAT_SUPP))) {
2160 mlog(ML_ERROR, "couldn't mount because of unsupported "
2161 "optional features (%x).\n", i);
2162 status = -EINVAL;
2163 goto bail;
2164 }
David Howellsbc98a422017-07-17 08:45:34 +01002165 if (!sb_rdonly(osb->sb) && (i = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP))) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002166 mlog(ML_ERROR, "couldn't mount RDWR because of "
2167 "unsupported optional features (%x).\n", i);
2168 status = -EINVAL;
2169 goto bail;
2170 }
2171
Sunil Mushran98f486f22010-10-09 10:24:46 -07002172 if (ocfs2_clusterinfo_valid(osb)) {
2173 osb->osb_stackflags =
2174 OCFS2_RAW_SB(di)->s_cluster_info.ci_stackflags;
Goldwyn Rodriguesc74a3bd2014-01-21 15:48:21 -08002175 strlcpy(osb->osb_cluster_stack,
Joel Beckerb61817e2008-02-01 15:08:23 -08002176 OCFS2_RAW_SB(di)->s_cluster_info.ci_stack,
Goldwyn Rodriguesc74a3bd2014-01-21 15:48:21 -08002177 OCFS2_STACK_LABEL_LEN + 1);
Joel Beckerb61817e2008-02-01 15:08:23 -08002178 if (strlen(osb->osb_cluster_stack) != OCFS2_STACK_LABEL_LEN) {
2179 mlog(ML_ERROR,
2180 "couldn't mount because of an invalid "
2181 "cluster stack label (%s) \n",
2182 osb->osb_cluster_stack);
2183 status = -EINVAL;
2184 goto bail;
2185 }
Goldwyn Rodriguesc74a3bd2014-01-21 15:48:21 -08002186 strlcpy(osb->osb_cluster_name,
2187 OCFS2_RAW_SB(di)->s_cluster_info.ci_cluster,
2188 OCFS2_CLUSTER_NAME_LEN + 1);
Joel Beckerb61817e2008-02-01 15:08:23 -08002189 } else {
2190 /* The empty string is identical with classic tools that
2191 * don't know about s_cluster_info. */
2192 osb->osb_cluster_stack[0] = '\0';
2193 }
2194
Mark Fashehccd979b2005-12-15 14:31:24 -08002195 get_random_bytes(&osb->s_next_generation, sizeof(u32));
2196
2197 /* FIXME
2198 * This should be done in ocfs2_journal_init(), but unknown
2199 * ordering issues will cause the filesystem to crash.
2200 * If anyone wants to figure out what part of the code
2201 * refers to osb->journal before ocfs2_journal_init() is run,
2202 * be my guest.
2203 */
2204 /* initialize our journal structure */
2205
Robert P. J. Daycd861282006-12-13 00:34:52 -08002206 journal = kzalloc(sizeof(struct ocfs2_journal), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08002207 if (!journal) {
2208 mlog(ML_ERROR, "unable to alloc journal\n");
2209 status = -ENOMEM;
2210 goto bail;
2211 }
2212 osb->journal = journal;
2213 journal->j_osb = osb;
2214
2215 atomic_set(&journal->j_num_trans, 0);
2216 init_rwsem(&journal->j_trans_barrier);
2217 init_waitqueue_head(&journal->j_checkpointed);
2218 spin_lock_init(&journal->j_lock);
2219 journal->j_trans_id = (unsigned long) 1;
2220 INIT_LIST_HEAD(&journal->j_la_cleanups);
David Howellsc4028952006-11-22 14:57:56 +00002221 INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
Mark Fashehccd979b2005-12-15 14:31:24 -08002222 journal->j_state = OCFS2_JOURNAL_FREE;
2223
Jan Karae3a767b2014-04-03 14:46:56 -07002224 INIT_WORK(&osb->dquot_drop_work, ocfs2_drop_dquot_refs);
2225 init_llist_head(&osb->dquot_drop_list);
2226
Mark Fashehccd979b2005-12-15 14:31:24 -08002227 /* get some pseudo constants for clustersize bits */
2228 osb->s_clustersize_bits =
2229 le32_to_cpu(di->id2.i_super.s_clustersize_bits);
2230 osb->s_clustersize = 1 << osb->s_clustersize_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08002231
2232 if (osb->s_clustersize < OCFS2_MIN_CLUSTERSIZE ||
2233 osb->s_clustersize > OCFS2_MAX_CLUSTERSIZE) {
2234 mlog(ML_ERROR, "Volume has invalid cluster size (%d)\n",
2235 osb->s_clustersize);
2236 status = -EINVAL;
2237 goto bail;
2238 }
2239
Patrick J. LoPresti3bdb8ef2010-07-22 15:05:57 -07002240 total_blocks = ocfs2_clusters_to_blocks(osb->sb,
2241 le32_to_cpu(di->i_clusters));
2242
2243 status = generic_check_addressable(osb->sb->s_blocksize_bits,
2244 total_blocks);
2245 if (status) {
2246 mlog(ML_ERROR, "Volume too large "
2247 "to mount safely on this system");
2248 status = -EFBIG;
Mark Fashehccd979b2005-12-15 14:31:24 -08002249 goto bail;
2250 }
2251
2252 if (ocfs2_setup_osb_uuid(osb, di->id2.i_super.s_uuid,
2253 sizeof(di->id2.i_super.s_uuid))) {
2254 mlog(ML_ERROR, "Out of memory trying to setup our uuid.\n");
2255 status = -ENOMEM;
2256 goto bail;
2257 }
2258
Fabian Frederick69201bb2014-06-04 16:06:07 -07002259 strlcpy(osb->vol_label, di->id2.i_super.s_label,
2260 OCFS2_MAX_VOL_LABEL_LEN);
Mark Fashehccd979b2005-12-15 14:31:24 -08002261 osb->root_blkno = le64_to_cpu(di->id2.i_super.s_root_blkno);
2262 osb->system_dir_blkno = le64_to_cpu(di->id2.i_super.s_system_dir_blkno);
2263 osb->first_cluster_group_blkno =
2264 le64_to_cpu(di->id2.i_super.s_first_cluster_group);
2265 osb->fs_generation = le32_to_cpu(di->i_fs_generation);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002266 osb->uuid_hash = le32_to_cpu(di->id2.i_super.s_uuid_hash);
Tao Ma32a42d32011-02-23 21:29:08 +08002267 trace_ocfs2_initialize_super(osb->vol_label, osb->uuid_str,
2268 (unsigned long long)osb->root_blkno,
2269 (unsigned long long)osb->system_dir_blkno,
2270 osb->s_clustersize_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08002271
2272 osb->osb_dlm_debug = ocfs2_new_dlm_debug();
2273 if (!osb->osb_dlm_debug) {
2274 status = -ENOMEM;
2275 mlog_errno(status);
2276 goto bail;
2277 }
2278
2279 atomic_set(&osb->vol_state, VOLUME_INIT);
2280
2281 /* load root, system_dir, and all global system inodes */
2282 status = ocfs2_init_global_system_inodes(osb);
2283 if (status < 0) {
2284 mlog_errno(status);
2285 goto bail;
2286 }
2287
2288 /*
2289 * global bitmap
2290 */
2291 inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
2292 OCFS2_INVALID_SLOT);
2293 if (!inode) {
2294 status = -EINVAL;
2295 mlog_errno(status);
2296 goto bail;
2297 }
2298
2299 osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
Mark Fasheh6b820212010-04-05 18:17:14 -07002300 osb->osb_clusters_at_boot = OCFS2_I(inode)->ip_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08002301 iput(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08002302
Tao Ma85718822010-04-13 14:38:06 +08002303 osb->bitmap_cpg = ocfs2_group_bitmap_size(sb, 0,
2304 osb->s_feature_incompat) * 8;
Mark Fashehccd979b2005-12-15 14:31:24 -08002305
2306 status = ocfs2_init_slot_info(osb);
2307 if (status < 0) {
2308 mlog_errno(status);
2309 goto bail;
2310 }
Vladimir Davydov9de16262015-04-14 15:46:42 -07002311 cleancache_init_shared_fs(sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002312
Bhaktipriya Shridhar44be9752016-10-07 16:57:13 -07002313 osb->ocfs2_wq = alloc_ordered_workqueue("ocfs2_wq", WQ_MEM_RECLAIM);
jiangyiwen35ddf782016-03-25 14:21:32 -07002314 if (!osb->ocfs2_wq) {
2315 status = -ENOMEM;
2316 mlog_errno(status);
2317 }
2318
Mark Fashehccd979b2005-12-15 14:31:24 -08002319bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08002320 return status;
2321}
2322
2323/*
2324 * will return: -EAGAIN if it is ok to keep searching for superblocks
2325 * -EINVAL if there is a bad superblock
2326 * 0 on success
2327 */
2328static int ocfs2_verify_volume(struct ocfs2_dinode *di,
2329 struct buffer_head *bh,
Joel Becker73be1922009-01-06 14:57:08 -08002330 u32 blksz,
2331 struct ocfs2_blockcheck_stats *stats)
Mark Fashehccd979b2005-12-15 14:31:24 -08002332{
2333 int status = -EAGAIN;
2334
Mark Fashehccd979b2005-12-15 14:31:24 -08002335 if (memcmp(di->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
2336 strlen(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) {
Joel Beckerd030cc92008-12-11 15:04:14 -08002337 /* We have to do a raw check of the feature here */
2338 if (le32_to_cpu(di->id2.i_super.s_feature_incompat) &
2339 OCFS2_FEATURE_INCOMPAT_META_ECC) {
2340 status = ocfs2_block_check_validate(bh->b_data,
2341 bh->b_size,
Joel Becker73be1922009-01-06 14:57:08 -08002342 &di->i_check,
2343 stats);
Joel Beckerd030cc92008-12-11 15:04:14 -08002344 if (status)
2345 goto out;
2346 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002347 status = -EINVAL;
2348 if ((1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits)) != blksz) {
2349 mlog(ML_ERROR, "found superblock with incorrect block "
2350 "size: found %u, should be %u\n",
2351 1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits),
2352 blksz);
2353 } else if (le16_to_cpu(di->id2.i_super.s_major_rev_level) !=
2354 OCFS2_MAJOR_REV_LEVEL ||
2355 le16_to_cpu(di->id2.i_super.s_minor_rev_level) !=
2356 OCFS2_MINOR_REV_LEVEL) {
2357 mlog(ML_ERROR, "found superblock with bad version: "
2358 "found %u.%u, should be %u.%u\n",
2359 le16_to_cpu(di->id2.i_super.s_major_rev_level),
2360 le16_to_cpu(di->id2.i_super.s_minor_rev_level),
2361 OCFS2_MAJOR_REV_LEVEL,
2362 OCFS2_MINOR_REV_LEVEL);
2363 } else if (bh->b_blocknr != le64_to_cpu(di->i_blkno)) {
2364 mlog(ML_ERROR, "bad block number on superblock: "
Mark Fashehb06970532006-03-03 10:24:33 -08002365 "found %llu, should be %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07002366 (unsigned long long)le64_to_cpu(di->i_blkno),
Mark Fashehb06970532006-03-03 10:24:33 -08002367 (unsigned long long)bh->b_blocknr);
Mark Fashehccd979b2005-12-15 14:31:24 -08002368 } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
2369 le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
2370 mlog(ML_ERROR, "bad cluster size found: %u\n",
2371 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
2372 } else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
2373 mlog(ML_ERROR, "bad root_blkno: 0\n");
2374 } else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
2375 mlog(ML_ERROR, "bad system_dir_blkno: 0\n");
2376 } else if (le16_to_cpu(di->id2.i_super.s_max_slots) > OCFS2_MAX_SLOTS) {
2377 mlog(ML_ERROR,
2378 "Superblock slots found greater than file system "
2379 "maximum: found %u, max %u\n",
2380 le16_to_cpu(di->id2.i_super.s_max_slots),
2381 OCFS2_MAX_SLOTS);
2382 } else {
2383 /* found it! */
2384 status = 0;
2385 }
2386 }
2387
Joel Beckerd030cc92008-12-11 15:04:14 -08002388out:
Tao Mac1e8d352011-03-07 16:43:21 +08002389 if (status && status != -EAGAIN)
2390 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002391 return status;
2392}
2393
2394static int ocfs2_check_volume(struct ocfs2_super *osb)
2395{
Denis Chengbddb8eb32007-09-27 02:10:04 +08002396 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08002397 int dirty;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002398 int local;
Mark Fashehccd979b2005-12-15 14:31:24 -08002399 struct ocfs2_dinode *local_alloc = NULL; /* only used if we
2400 * recover
2401 * ourselves. */
2402
Mark Fashehccd979b2005-12-15 14:31:24 -08002403 /* Init our journal object. */
2404 status = ocfs2_journal_init(osb->journal, &dirty);
2405 if (status < 0) {
2406 mlog(ML_ERROR, "Could not initialize journal!\n");
2407 goto finally;
2408 }
2409
Patrick J. LoPresti3bdb8ef2010-07-22 15:05:57 -07002410 /* Now that journal has been initialized, check to make sure
2411 entire volume is addressable. */
2412 status = ocfs2_journal_addressable(osb);
2413 if (status)
2414 goto finally;
2415
Mark Fashehccd979b2005-12-15 14:31:24 -08002416 /* If the journal was unmounted cleanly then we don't want to
2417 * recover anything. Otherwise, journal_load will do that
2418 * dirty work for us :) */
2419 if (!dirty) {
2420 status = ocfs2_journal_wipe(osb->journal, 0);
2421 if (status < 0) {
2422 mlog_errno(status);
2423 goto finally;
2424 }
2425 } else {
Sunil Mushran619c2002011-07-24 10:34:54 -07002426 printk(KERN_NOTICE "ocfs2: File system on device (%s) was not "
2427 "unmounted cleanly, recovering it.\n", osb->dev_str);
Mark Fashehccd979b2005-12-15 14:31:24 -08002428 }
2429
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002430 local = ocfs2_mount_local(osb);
2431
Mark Fashehccd979b2005-12-15 14:31:24 -08002432 /* will play back anything left in the journal. */
Sunil Mushran539d8262008-07-14 17:31:10 -07002433 status = ocfs2_journal_load(osb->journal, local, dirty);
Wengang Wang01af4822008-06-10 14:24:48 +08002434 if (status < 0) {
2435 mlog(ML_ERROR, "ocfs2 journal load failed! %d\n", status);
2436 goto finally;
2437 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002438
alex chen1dfeb762015-02-10 14:09:04 -08002439 if (osb->s_mount_opt & OCFS2_MOUNT_JOURNAL_ASYNC_COMMIT)
2440 jbd2_journal_set_features(osb->journal->j_journal,
2441 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2442 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2443 else
2444 jbd2_journal_clear_features(osb->journal->j_journal,
2445 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2446 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2447
Mark Fashehccd979b2005-12-15 14:31:24 -08002448 if (dirty) {
2449 /* recover my local alloc if we didn't unmount cleanly. */
2450 status = ocfs2_begin_local_alloc_recovery(osb,
2451 osb->slot_num,
2452 &local_alloc);
2453 if (status < 0) {
2454 mlog_errno(status);
2455 goto finally;
2456 }
2457 /* we complete the recovery process after we've marked
2458 * ourselves as mounted. */
2459 }
2460
Mark Fashehccd979b2005-12-15 14:31:24 -08002461 status = ocfs2_load_local_alloc(osb);
2462 if (status < 0) {
2463 mlog_errno(status);
2464 goto finally;
2465 }
2466
2467 if (dirty) {
2468 /* Recovery will be completed after we've mounted the
2469 * rest of the volume. */
Mark Fashehccd979b2005-12-15 14:31:24 -08002470 osb->local_alloc_copy = local_alloc;
2471 local_alloc = NULL;
2472 }
2473
2474 /* go through each journal, trylock it and if you get the
2475 * lock, and it's marked as dirty, set the bit in the recover
2476 * map and launch a recovery thread for it. */
2477 status = ocfs2_mark_dead_nodes(osb);
Srinivas Eeda9140db02009-03-06 14:21:46 -08002478 if (status < 0) {
2479 mlog_errno(status);
2480 goto finally;
2481 }
2482
2483 status = ocfs2_compute_replay_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002484 if (status < 0)
2485 mlog_errno(status);
2486
2487finally:
Tim Gardnerd787ab02013-02-21 16:42:44 -08002488 kfree(local_alloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08002489
Tao Mac1e8d352011-03-07 16:43:21 +08002490 if (status)
2491 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002492 return status;
2493}
2494
2495/*
2496 * The routine gets called from dismount or close whenever a dismount on
2497 * volume is requested and the osb open count becomes 1.
2498 * It will remove the osb from the global list and also free up all the
2499 * initialized resources and fileobject.
2500 */
2501static void ocfs2_delete_osb(struct ocfs2_super *osb)
2502{
Mark Fashehccd979b2005-12-15 14:31:24 -08002503 /* This function assumes that the caller has the main osb resource */
2504
jiangyiwen35ddf782016-03-25 14:21:32 -07002505 /* ocfs2_initializer_super have already created this workqueue */
piaojun23e08132017-11-15 17:31:25 -08002506 if (osb->ocfs2_wq)
jiangyiwen35ddf782016-03-25 14:21:32 -07002507 destroy_workqueue(osb->ocfs2_wq);
jiangyiwen35ddf782016-03-25 14:21:32 -07002508
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002509 ocfs2_free_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002510
Mark Fashehb4df6ed2006-02-22 17:35:08 -08002511 kfree(osb->osb_orphan_wipes);
Sunil Mushran539d8262008-07-14 17:31:10 -07002512 kfree(osb->slot_recovery_generations);
Mark Fashehccd979b2005-12-15 14:31:24 -08002513 /* FIXME
2514 * This belongs in journal shutdown, but because we have to
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02002515 * allocate osb->journal at the start of ocfs2_initialize_osb(),
Mark Fashehccd979b2005-12-15 14:31:24 -08002516 * we free it here.
2517 */
2518 kfree(osb->journal);
Tim Gardnerd787ab02013-02-21 16:42:44 -08002519 kfree(osb->local_alloc_copy);
Mark Fashehccd979b2005-12-15 14:31:24 -08002520 kfree(osb->uuid_str);
Joseph Qif13a5682014-09-25 16:05:11 -07002521 kfree(osb->vol_label);
Mark Fashehccd979b2005-12-15 14:31:24 -08002522 ocfs2_put_dlm_debug(osb->osb_dlm_debug);
2523 memset(osb, 0, sizeof(struct ocfs2_super));
Mark Fashehccd979b2005-12-15 14:31:24 -08002524}
2525
Goldwyn Rodrigues7d0fb912015-09-04 15:44:11 -07002526/* Depending on the mount option passed, perform one of the following:
2527 * Put OCFS2 into a readonly state (default)
2528 * Return EIO so that only the process errs
2529 * Fix the error as if fsck.ocfs2 -y
2530 * panic
2531 */
2532static int ocfs2_handle_error(struct super_block *sb)
Mark Fashehccd979b2005-12-15 14:31:24 -08002533{
2534 struct ocfs2_super *osb = OCFS2_SB(sb);
Goldwyn Rodrigues7d0fb912015-09-04 15:44:11 -07002535 int rv = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002536
2537 ocfs2_set_osb_flag(osb, OCFS2_OSB_ERROR_FS);
Goldwyn Rodrigues7d0fb912015-09-04 15:44:11 -07002538 pr_crit("On-disk corruption discovered. "
2539 "Please run fsck.ocfs2 once the filesystem is unmounted.\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08002540
Goldwyn Rodrigues7d0fb912015-09-04 15:44:11 -07002541 if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_PANIC) {
2542 panic("OCFS2: (device %s): panic forced after error\n",
2543 sb->s_id);
2544 } else if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_CONT) {
2545 pr_crit("OCFS2: Returning error to the calling process.\n");
2546 rv = -EIO;
2547 } else { /* default option */
2548 rv = -EROFS;
David Howellsbc98a422017-07-17 08:45:34 +01002549 if (sb_rdonly(sb) && (ocfs2_is_soft_readonly(osb) || ocfs2_is_hard_readonly(osb)))
Goldwyn Rodrigues7d0fb912015-09-04 15:44:11 -07002550 return rv;
Mark Fashehccd979b2005-12-15 14:31:24 -08002551
Goldwyn Rodrigues7d0fb912015-09-04 15:44:11 -07002552 pr_crit("OCFS2: File system is now read-only.\n");
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002553 sb->s_flags |= SB_RDONLY;
Goldwyn Rodrigues7d0fb912015-09-04 15:44:11 -07002554 ocfs2_set_ro_flag(osb, 0);
2555 }
2556
2557 return rv;
Mark Fashehccd979b2005-12-15 14:31:24 -08002558}
2559
Goldwyn Rodrigues7d0fb912015-09-04 15:44:11 -07002560int __ocfs2_error(struct super_block *sb, const char *function,
Joe Perches1543306e2015-04-14 15:43:46 -07002561 const char *fmt, ...)
Mark Fashehccd979b2005-12-15 14:31:24 -08002562{
Joe Perches1543306e2015-04-14 15:43:46 -07002563 struct va_format vaf;
Mark Fashehccd979b2005-12-15 14:31:24 -08002564 va_list args;
2565
2566 va_start(args, fmt);
Joe Perches1543306e2015-04-14 15:43:46 -07002567 vaf.fmt = fmt;
2568 vaf.va = &args;
Mark Fashehccd979b2005-12-15 14:31:24 -08002569
2570 /* Not using mlog here because we want to show the actual
2571 * function the error came from. */
Joe Perches7ecef142015-09-04 15:44:51 -07002572 printk(KERN_CRIT "OCFS2: ERROR (device %s): %s: %pV",
Joe Perches1543306e2015-04-14 15:43:46 -07002573 sb->s_id, function, &vaf);
2574
2575 va_end(args);
Mark Fashehccd979b2005-12-15 14:31:24 -08002576
Goldwyn Rodrigues7d0fb912015-09-04 15:44:11 -07002577 return ocfs2_handle_error(sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002578}
2579
2580/* Handle critical errors. This is intentionally more drastic than
2581 * ocfs2_handle_error, so we only use for things like journal errors,
2582 * etc. */
Joe Perches1543306e2015-04-14 15:43:46 -07002583void __ocfs2_abort(struct super_block *sb, const char *function,
Mark Fashehccd979b2005-12-15 14:31:24 -08002584 const char *fmt, ...)
2585{
Joe Perches1543306e2015-04-14 15:43:46 -07002586 struct va_format vaf;
Mark Fashehccd979b2005-12-15 14:31:24 -08002587 va_list args;
2588
2589 va_start(args, fmt);
Mark Fashehccd979b2005-12-15 14:31:24 -08002590
Joe Perches1543306e2015-04-14 15:43:46 -07002591 vaf.fmt = fmt;
2592 vaf.va = &args;
2593
Joe Perches7ecef142015-09-04 15:44:51 -07002594 printk(KERN_CRIT "OCFS2: abort (device %s): %s: %pV",
Joe Perches1543306e2015-04-14 15:43:46 -07002595 sb->s_id, function, &vaf);
2596
2597 va_end(args);
Mark Fashehccd979b2005-12-15 14:31:24 -08002598
2599 /* We don't have the cluster support yet to go straight to
2600 * hard readonly in here. Until then, we want to keep
2601 * ocfs2_abort() so that we can at least mark critical
2602 * errors.
2603 *
2604 * TODO: This should abort the journal and alert other nodes
2605 * that our slot needs recovery. */
2606
2607 /* Force a panic(). This stinks, but it's better than letting
2608 * things continue without having a proper hard readonly
2609 * here. */
Sunil Mushrana2f2ddb2009-08-19 15:16:01 -07002610 if (!ocfs2_mount_local(OCFS2_SB(sb)))
2611 OCFS2_SB(sb)->s_mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
Mark Fashehccd979b2005-12-15 14:31:24 -08002612 ocfs2_handle_error(sb);
2613}
2614
Joel Beckere4b963f2009-09-02 17:17:36 -07002615/*
2616 * Void signal blockers, because in-kernel sigprocmask() only fails
2617 * when SIG_* is wrong.
2618 */
2619void ocfs2_block_signals(sigset_t *oldset)
2620{
2621 int rc;
2622 sigset_t blocked;
2623
2624 sigfillset(&blocked);
2625 rc = sigprocmask(SIG_BLOCK, &blocked, oldset);
2626 BUG_ON(rc);
2627}
2628
2629void ocfs2_unblock_signals(sigset_t *oldset)
2630{
2631 int rc = sigprocmask(SIG_SETMASK, oldset, NULL);
2632 BUG_ON(rc);
2633}
2634
Mark Fashehccd979b2005-12-15 14:31:24 -08002635module_init(ocfs2_init);
2636module_exit(ocfs2_exit);