blob: 9122d59f8127c6768d14ffe7c1ab48fa2a9e056a [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * super.c
5 *
6 * load/unload driver, mount/dismount volumes
7 *
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
26#include <linux/module.h>
27#include <linux/fs.h>
28#include <linux/types.h>
29#include <linux/slab.h>
30#include <linux/highmem.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080031#include <linux/init.h>
32#include <linux/random.h>
33#include <linux/statfs.h>
34#include <linux/moduleparam.h>
35#include <linux/blkdev.h>
36#include <linux/socket.h>
37#include <linux/inet.h>
38#include <linux/parser.h>
39#include <linux/crc32.h>
40#include <linux/debugfs.h>
Sunil Mushrand5500712007-09-06 13:34:16 -070041#include <linux/mount.h>
Joel Becker6953b4c2008-01-29 16:59:56 -080042#include <linux/seq_file.h>
Jan Kara19ece542008-08-21 20:13:17 +020043#include <linux/quotaops.h>
Alessio Igor Bogani337eb002009-05-12 15:10:54 +020044#include <linux/smp_lock.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080045
46#define MLOG_MASK_PREFIX ML_SUPER
47#include <cluster/masklog.h>
48
49#include "ocfs2.h"
50
51/* this should be the only file to include a version 1 header */
52#include "ocfs1_fs_compat.h"
53
54#include "alloc.h"
Joel Beckerd030cc92008-12-11 15:04:14 -080055#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080056#include "dlmglue.h"
57#include "export.h"
58#include "extent_map.h"
59#include "heartbeat.h"
60#include "inode.h"
61#include "journal.h"
62#include "localalloc.h"
63#include "namei.h"
64#include "slot_map.h"
65#include "super.h"
66#include "sysfile.h"
67#include "uptodate.h"
68#include "ver.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080069#include "xattr.h"
Jan Kara9e33d692008-08-25 19:56:50 +020070#include "quota.h"
Tao Ma374a2632009-08-24 11:13:37 +080071#include "refcounttree.h"
Tiger Yangb89c5422010-01-25 14:11:06 +080072#include "suballoc.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080073
74#include "buffer_head_io.h"
75
Christoph Lametere18b8902006-12-06 20:33:20 -080076static struct kmem_cache *ocfs2_inode_cachep = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +020077struct kmem_cache *ocfs2_dquot_cachep;
78struct kmem_cache *ocfs2_qf_chunk_cachep;
Mark Fashehccd979b2005-12-15 14:31:24 -080079
Mark Fashehccd979b2005-12-15 14:31:24 -080080/* OCFS2 needs to schedule several differnt types of work which
81 * require cluster locking, disk I/O, recovery waits, etc. Since these
82 * types of work tend to be heavy we avoid using the kernel events
83 * workqueue and schedule on our own. */
84struct workqueue_struct *ocfs2_wq = NULL;
85
86static struct dentry *ocfs2_debugfs_root = NULL;
87
88MODULE_AUTHOR("Oracle");
89MODULE_LICENSE("GPL");
90
Tiger Yangc0123ad2007-09-08 00:16:10 +080091struct mount_options
92{
Mark Fashehd147b3d2007-11-07 14:40:36 -080093 unsigned long commit_interval;
Tiger Yangc0123ad2007-09-08 00:16:10 +080094 unsigned long mount_opt;
95 unsigned int atime_quantum;
96 signed short slot;
Mark Fasheh73c8a802010-04-05 18:17:13 -070097 int localalloc_opt;
Mark Fashehd02f00c2009-12-07 13:10:48 -080098 unsigned int resv_level;
Mark Fasheh83f92312010-04-05 18:17:16 -070099 int dir_resv_level;
Joel Beckerb61817e2008-02-01 15:08:23 -0800100 char cluster_stack[OCFS2_STACK_LABEL_LEN + 1];
Tiger Yangc0123ad2007-09-08 00:16:10 +0800101};
102
Mark Fashehccd979b2005-12-15 14:31:24 -0800103static int ocfs2_parse_options(struct super_block *sb, char *options,
Tiger Yangc0123ad2007-09-08 00:16:10 +0800104 struct mount_options *mopt,
Sunil Mushranbaf46612007-06-18 17:00:24 -0700105 int is_remount);
Jan Kara5297aad2009-10-15 14:54:04 +0200106static int ocfs2_check_set_options(struct super_block *sb,
107 struct mount_options *options);
Sunil Mushrand5500712007-09-06 13:34:16 -0700108static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt);
Mark Fashehccd979b2005-12-15 14:31:24 -0800109static void ocfs2_put_super(struct super_block *sb);
110static int ocfs2_mount_volume(struct super_block *sb);
111static int ocfs2_remount(struct super_block *sb, int *flags, char *data);
112static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err);
113static int ocfs2_initialize_mem_caches(void);
114static void ocfs2_free_mem_caches(void);
115static void ocfs2_delete_osb(struct ocfs2_super *osb);
116
David Howells726c3342006-06-23 02:02:58 -0700117static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf);
Mark Fashehccd979b2005-12-15 14:31:24 -0800118
119static int ocfs2_sync_fs(struct super_block *sb, int wait);
120
121static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb);
122static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb);
Denis Chengbddb8eb32007-09-27 02:10:04 +0800123static void ocfs2_release_system_inodes(struct ocfs2_super *osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800124static int ocfs2_check_volume(struct ocfs2_super *osb);
125static int ocfs2_verify_volume(struct ocfs2_dinode *di,
126 struct buffer_head *bh,
Joel Becker73be1922009-01-06 14:57:08 -0800127 u32 sectsize,
128 struct ocfs2_blockcheck_stats *stats);
Mark Fashehccd979b2005-12-15 14:31:24 -0800129static int ocfs2_initialize_super(struct super_block *sb,
130 struct buffer_head *bh,
Joel Becker73be1922009-01-06 14:57:08 -0800131 int sector_size,
132 struct ocfs2_blockcheck_stats *stats);
Mark Fashehccd979b2005-12-15 14:31:24 -0800133static int ocfs2_get_sector(struct super_block *sb,
134 struct buffer_head **bh,
135 int block,
136 int sect_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800137static struct inode *ocfs2_alloc_inode(struct super_block *sb);
138static void ocfs2_destroy_inode(struct inode *inode);
Jan Kara19ece542008-08-21 20:13:17 +0200139static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend);
140static int ocfs2_enable_quotas(struct ocfs2_super *osb);
141static void ocfs2_disable_quotas(struct ocfs2_super *osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800142
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800143static const struct super_operations ocfs2_sops = {
Mark Fashehccd979b2005-12-15 14:31:24 -0800144 .statfs = ocfs2_statfs,
145 .alloc_inode = ocfs2_alloc_inode,
146 .destroy_inode = ocfs2_destroy_inode,
147 .drop_inode = ocfs2_drop_inode,
Al Viro066d92d2010-06-08 21:28:10 -0400148 .evict_inode = ocfs2_evict_inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800149 .sync_fs = ocfs2_sync_fs,
Mark Fashehccd979b2005-12-15 14:31:24 -0800150 .put_super = ocfs2_put_super,
151 .remount_fs = ocfs2_remount,
Sunil Mushrand5500712007-09-06 13:34:16 -0700152 .show_options = ocfs2_show_options,
Jan Kara9e33d692008-08-25 19:56:50 +0200153 .quota_read = ocfs2_quota_read,
154 .quota_write = ocfs2_quota_write,
Mark Fashehccd979b2005-12-15 14:31:24 -0800155};
156
157enum {
158 Opt_barrier,
159 Opt_err_panic,
160 Opt_err_ro,
161 Opt_intr,
162 Opt_nointr,
163 Opt_hb_none,
164 Opt_hb_local,
165 Opt_data_ordered,
166 Opt_data_writeback,
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800167 Opt_atime_quantum,
Sunil Mushranbaf46612007-06-18 17:00:24 -0700168 Opt_slot,
Mark Fashehd147b3d2007-11-07 14:40:36 -0800169 Opt_commit,
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800170 Opt_localalloc,
Mark Fasheh53fc6222007-12-20 16:49:04 -0800171 Opt_localflocks,
Joel Beckerb61817e2008-02-01 15:08:23 -0800172 Opt_stack,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800173 Opt_user_xattr,
174 Opt_nouser_xattr,
Joel Becker12462f12008-09-03 20:03:40 -0700175 Opt_inode64,
Tiger Yanga68979b2008-11-14 11:17:52 +0800176 Opt_acl,
177 Opt_noacl,
Jan Kara19ece542008-08-21 20:13:17 +0200178 Opt_usrquota,
179 Opt_grpquota,
Tristan Ye7bdb0d12010-10-11 16:46:39 +0800180 Opt_coherency_buffered,
181 Opt_coherency_full,
Mark Fashehd02f00c2009-12-07 13:10:48 -0800182 Opt_resv_level,
Mark Fasheh83f92312010-04-05 18:17:16 -0700183 Opt_dir_resv_level,
Mark Fashehccd979b2005-12-15 14:31:24 -0800184 Opt_err,
185};
186
Steven Whitehousea447c092008-10-13 10:46:57 +0100187static const match_table_t tokens = {
Mark Fashehccd979b2005-12-15 14:31:24 -0800188 {Opt_barrier, "barrier=%u"},
189 {Opt_err_panic, "errors=panic"},
190 {Opt_err_ro, "errors=remount-ro"},
191 {Opt_intr, "intr"},
192 {Opt_nointr, "nointr"},
193 {Opt_hb_none, OCFS2_HB_NONE},
194 {Opt_hb_local, OCFS2_HB_LOCAL},
195 {Opt_data_ordered, "data=ordered"},
196 {Opt_data_writeback, "data=writeback"},
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800197 {Opt_atime_quantum, "atime_quantum=%u"},
Sunil Mushranbaf46612007-06-18 17:00:24 -0700198 {Opt_slot, "preferred_slot=%u"},
Mark Fashehd147b3d2007-11-07 14:40:36 -0800199 {Opt_commit, "commit=%u"},
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800200 {Opt_localalloc, "localalloc=%d"},
Mark Fasheh53fc6222007-12-20 16:49:04 -0800201 {Opt_localflocks, "localflocks"},
Joel Beckerb61817e2008-02-01 15:08:23 -0800202 {Opt_stack, "cluster_stack=%s"},
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800203 {Opt_user_xattr, "user_xattr"},
204 {Opt_nouser_xattr, "nouser_xattr"},
Joel Becker12462f12008-09-03 20:03:40 -0700205 {Opt_inode64, "inode64"},
Tiger Yanga68979b2008-11-14 11:17:52 +0800206 {Opt_acl, "acl"},
207 {Opt_noacl, "noacl"},
Jan Kara19ece542008-08-21 20:13:17 +0200208 {Opt_usrquota, "usrquota"},
209 {Opt_grpquota, "grpquota"},
Tristan Ye7bdb0d12010-10-11 16:46:39 +0800210 {Opt_coherency_buffered, "coherency=buffered"},
211 {Opt_coherency_full, "coherency=full"},
Mark Fashehd02f00c2009-12-07 13:10:48 -0800212 {Opt_resv_level, "resv_level=%u"},
Mark Fasheh83f92312010-04-05 18:17:16 -0700213 {Opt_dir_resv_level, "dir_resv_level=%u"},
Mark Fashehccd979b2005-12-15 14:31:24 -0800214 {Opt_err, NULL}
215};
216
Sunil Mushran50397502008-12-17 14:17:43 -0800217#ifdef CONFIG_DEBUG_FS
218static int ocfs2_osb_dump(struct ocfs2_super *osb, char *buf, int len)
219{
Sunil Mushran50397502008-12-17 14:17:43 -0800220 struct ocfs2_cluster_connection *cconn = osb->cconn;
221 struct ocfs2_recovery_map *rm = osb->recovery_map;
Sunil Mushrandf152c22009-06-22 11:40:07 -0700222 struct ocfs2_orphan_scan *os = &osb->osb_orphan_scan;
223 int i, out = 0;
Sunil Mushran50397502008-12-17 14:17:43 -0800224
225 out += snprintf(buf + out, len - out,
226 "%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
230 out += snprintf(buf + out, len - out,
231 "%10s => State: %d Flags: 0x%lX\n", "Volume",
232 atomic_read(&osb->vol_state), osb->osb_flags);
233
234 out += snprintf(buf + out, len - out,
235 "%10s => Block: %lu Cluster: %d\n", "Sizes",
236 osb->sb->s_blocksize, osb->s_clustersize);
237
238 out += snprintf(buf + out, len - out,
239 "%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
244 out += snprintf(buf + out, len - out,
245 "%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) {
249 out += snprintf(buf + out, len - out,
250 "%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
259 spin_lock(&osb->dc_task_lock);
260 out += snprintf(buf + out, len - out,
261 "%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);
Sunil Mushran50397502008-12-17 14:17:43 -0800266 spin_unlock(&osb->dc_task_lock);
267
268 spin_lock(&osb->osb_lock);
269 out += snprintf(buf + out, len - out, "%10s => Pid: %d Nodes:",
270 "Recovery",
271 (osb->recovery_thread_task ?
272 task_pid_nr(osb->recovery_thread_task) : -1));
273 if (rm->rm_used == 0)
274 out += snprintf(buf + out, len - out, " None\n");
275 else {
276 for (i = 0; i < rm->rm_used; i++)
277 out += snprintf(buf + out, len - out, " %d",
278 rm->rm_entries[i]);
279 out += snprintf(buf + out, len - out, "\n");
280 }
281 spin_unlock(&osb->osb_lock);
282
283 out += snprintf(buf + out, len - out,
284 "%10s => Pid: %d Interval: %lu Needs: %d\n", "Commit",
Sunil Mushranc3d38842009-06-19 14:45:55 -0700285 (osb->commit_task ? task_pid_nr(osb->commit_task) : -1),
286 osb->osb_commit_interval,
Sunil Mushran50397502008-12-17 14:17:43 -0800287 atomic_read(&osb->needs_checkpoint));
288
289 out += snprintf(buf + out, len - out,
Sunil Mushranc3d38842009-06-19 14:45:55 -0700290 "%10s => State: %d TxnId: %lu NumTxns: %d\n",
Sunil Mushran50397502008-12-17 14:17:43 -0800291 "Journal", osb->journal->j_state,
Sunil Mushranc3d38842009-06-19 14:45:55 -0700292 osb->journal->j_trans_id,
293 atomic_read(&osb->journal->j_num_trans));
Sunil Mushran50397502008-12-17 14:17:43 -0800294
295 out += snprintf(buf + out, len - out,
296 "%10s => GlobalAllocs: %d LocalAllocs: %d "
297 "SubAllocs: %d LAWinMoves: %d SAExtends: %d\n",
298 "Stats",
299 atomic_read(&osb->alloc_stats.bitmap_data),
300 atomic_read(&osb->alloc_stats.local_data),
301 atomic_read(&osb->alloc_stats.bg_allocs),
302 atomic_read(&osb->alloc_stats.moves),
303 atomic_read(&osb->alloc_stats.bg_extends));
304
305 out += snprintf(buf + out, len - out,
306 "%10s => State: %u Descriptor: %llu Size: %u bits "
307 "Default: %u bits\n",
308 "LocalAlloc", osb->local_alloc_state,
309 (unsigned long long)osb->la_last_gd,
310 osb->local_alloc_bits, osb->local_alloc_default_bits);
311
312 spin_lock(&osb->osb_lock);
313 out += snprintf(buf + out, len - out,
Tiger Yangb89c5422010-01-25 14:11:06 +0800314 "%10s => InodeSlot: %d StolenInodes: %d, "
315 "MetaSlot: %d StolenMeta: %d\n", "Steal",
Sunil Mushran50397502008-12-17 14:17:43 -0800316 osb->s_inode_steal_slot,
Tiger Yangb89c5422010-01-25 14:11:06 +0800317 atomic_read(&osb->s_num_inodes_stolen),
318 osb->s_meta_steal_slot,
319 atomic_read(&osb->s_num_meta_stolen));
Sunil Mushran50397502008-12-17 14:17:43 -0800320 spin_unlock(&osb->osb_lock);
321
Sunil Mushrandf152c22009-06-22 11:40:07 -0700322 out += snprintf(buf + out, len - out, "OrphanScan => ");
323 out += snprintf(buf + out, len - out, "Local: %u Global: %u ",
324 os->os_count, os->os_seqno);
325 out += snprintf(buf + out, len - out, " Last Scan: ");
326 if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE)
327 out += snprintf(buf + out, len - out, "Disabled\n");
328 else
329 out += snprintf(buf + out, len - out, "%lu seconds ago\n",
330 (get_seconds() - os->os_scantime.tv_sec));
331
Sunil Mushran50397502008-12-17 14:17:43 -0800332 out += snprintf(buf + out, len - out, "%10s => %3s %10s\n",
333 "Slots", "Num", "RecoGen");
Sunil Mushran50397502008-12-17 14:17:43 -0800334 for (i = 0; i < osb->max_slots; ++i) {
335 out += snprintf(buf + out, len - out,
336 "%10s %c %3d %10d\n",
337 " ",
338 (i == osb->slot_num ? '*' : ' '),
339 i, osb->slot_recovery_generations[i]);
340 }
341
342 return out;
343}
344
345static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
346{
347 struct ocfs2_super *osb = inode->i_private;
348 char *buf = NULL;
349
350 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
351 if (!buf)
352 goto bail;
353
354 i_size_write(inode, ocfs2_osb_dump(osb, buf, PAGE_SIZE));
355
356 file->private_data = buf;
357
358 return 0;
359bail:
360 return -ENOMEM;
361}
362
363static int ocfs2_debug_release(struct inode *inode, struct file *file)
364{
365 kfree(file->private_data);
366 return 0;
367}
368
369static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
370 size_t nbytes, loff_t *ppos)
371{
372 return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
373 i_size_read(file->f_mapping->host));
374}
375#else
376static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
377{
378 return 0;
379}
380static int ocfs2_debug_release(struct inode *inode, struct file *file)
381{
382 return 0;
383}
384static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
385 size_t nbytes, loff_t *ppos)
386{
387 return 0;
388}
389#endif /* CONFIG_DEBUG_FS */
390
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700391static const struct file_operations ocfs2_osb_debug_fops = {
Sunil Mushran50397502008-12-17 14:17:43 -0800392 .open = ocfs2_osb_debug_open,
393 .release = ocfs2_debug_release,
394 .read = ocfs2_debug_read,
395 .llseek = generic_file_llseek,
396};
397
Mark Fashehccd979b2005-12-15 14:31:24 -0800398static int ocfs2_sync_fs(struct super_block *sb, int wait)
399{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800400 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -0800401 tid_t target;
402 struct ocfs2_super *osb = OCFS2_SB(sb);
403
Mark Fashehccd979b2005-12-15 14:31:24 -0800404 if (ocfs2_is_hard_readonly(osb))
405 return -EROFS;
406
407 if (wait) {
408 status = ocfs2_flush_truncate_log(osb);
409 if (status < 0)
410 mlog_errno(status);
411 } else {
412 ocfs2_schedule_truncate_log_flush(osb, 0);
413 }
414
Joel Becker2b4e30f2008-09-03 20:03:41 -0700415 if (jbd2_journal_start_commit(OCFS2_SB(sb)->journal->j_journal,
416 &target)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800417 if (wait)
Joel Becker2b4e30f2008-09-03 20:03:41 -0700418 jbd2_log_wait_commit(OCFS2_SB(sb)->journal->j_journal,
419 target);
Mark Fashehccd979b2005-12-15 14:31:24 -0800420 }
421 return 0;
422}
423
Jan Kara1a224ad2008-08-20 15:43:36 +0200424static int ocfs2_need_system_inode(struct ocfs2_super *osb, int ino)
425{
426 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_USRQUOTA)
427 && (ino == USER_QUOTA_SYSTEM_INODE
428 || ino == LOCAL_USER_QUOTA_SYSTEM_INODE))
429 return 0;
430 if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)
431 && (ino == GROUP_QUOTA_SYSTEM_INODE
432 || ino == LOCAL_GROUP_QUOTA_SYSTEM_INODE))
433 return 0;
434 return 1;
435}
436
Mark Fashehccd979b2005-12-15 14:31:24 -0800437static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb)
438{
439 struct inode *new = NULL;
440 int status = 0;
441 int i;
442
443 mlog_entry_void();
444
Jan Kara5fa06132008-01-11 00:11:45 +0100445 new = ocfs2_iget(osb, osb->root_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800446 if (IS_ERR(new)) {
447 status = PTR_ERR(new);
448 mlog_errno(status);
449 goto bail;
450 }
451 osb->root_inode = new;
452
Jan Kara5fa06132008-01-11 00:11:45 +0100453 new = ocfs2_iget(osb, osb->system_dir_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800454 if (IS_ERR(new)) {
455 status = PTR_ERR(new);
456 mlog_errno(status);
457 goto bail;
458 }
459 osb->sys_root_inode = new;
460
461 for (i = OCFS2_FIRST_ONLINE_SYSTEM_INODE;
462 i <= OCFS2_LAST_GLOBAL_SYSTEM_INODE; i++) {
Jan Kara1a224ad2008-08-20 15:43:36 +0200463 if (!ocfs2_need_system_inode(osb, i))
464 continue;
Mark Fashehccd979b2005-12-15 14:31:24 -0800465 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
466 if (!new) {
467 ocfs2_release_system_inodes(osb);
468 status = -EINVAL;
469 mlog_errno(status);
470 /* FIXME: Should ERROR_RO_FS */
471 mlog(ML_ERROR, "Unable to load system inode %d, "
472 "possibly corrupt fs?", i);
473 goto bail;
474 }
475 // the array now has one ref, so drop this one
476 iput(new);
477 }
478
479bail:
480 mlog_exit(status);
481 return status;
482}
483
484static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb)
485{
486 struct inode *new = NULL;
487 int status = 0;
488 int i;
489
490 mlog_entry_void();
491
492 for (i = OCFS2_LAST_GLOBAL_SYSTEM_INODE + 1;
493 i < NUM_SYSTEM_INODES;
494 i++) {
Jan Kara1a224ad2008-08-20 15:43:36 +0200495 if (!ocfs2_need_system_inode(osb, i))
496 continue;
Mark Fashehccd979b2005-12-15 14:31:24 -0800497 new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
498 if (!new) {
499 ocfs2_release_system_inodes(osb);
500 status = -EINVAL;
501 mlog(ML_ERROR, "status=%d, sysfile=%d, slot=%d\n",
502 status, i, osb->slot_num);
503 goto bail;
504 }
505 /* the array now has one ref, so drop this one */
506 iput(new);
507 }
508
509bail:
510 mlog_exit(status);
511 return status;
512}
513
Denis Chengbddb8eb32007-09-27 02:10:04 +0800514static void ocfs2_release_system_inodes(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -0800515{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800516 int i;
Mark Fashehccd979b2005-12-15 14:31:24 -0800517 struct inode *inode;
518
519 mlog_entry_void();
520
Tao Mab4d693f2010-08-16 16:58:21 +0800521 for (i = 0; i < NUM_GLOBAL_SYSTEM_INODES; i++) {
522 inode = osb->global_system_inodes[i];
Mark Fashehccd979b2005-12-15 14:31:24 -0800523 if (inode) {
524 iput(inode);
Tao Mab4d693f2010-08-16 16:58:21 +0800525 osb->global_system_inodes[i] = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800526 }
527 }
528
529 inode = osb->sys_root_inode;
530 if (inode) {
531 iput(inode);
532 osb->sys_root_inode = NULL;
533 }
534
535 inode = osb->root_inode;
536 if (inode) {
537 iput(inode);
538 osb->root_inode = NULL;
539 }
540
Tao Mab4d693f2010-08-16 16:58:21 +0800541 if (!osb->local_system_inodes)
542 goto out;
543
544 for (i = 0; i < NUM_LOCAL_SYSTEM_INODES * osb->max_slots; i++) {
545 if (osb->local_system_inodes[i]) {
546 iput(osb->local_system_inodes[i]);
547 osb->local_system_inodes[i] = NULL;
548 }
549 }
550
551 kfree(osb->local_system_inodes);
552 osb->local_system_inodes = NULL;
553
554out:
Denis Chengbddb8eb32007-09-27 02:10:04 +0800555 mlog_exit(0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800556}
557
558/* We're allocating fs objects, use GFP_NOFS */
559static struct inode *ocfs2_alloc_inode(struct super_block *sb)
560{
561 struct ocfs2_inode_info *oi;
562
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800563 oi = kmem_cache_alloc(ocfs2_inode_cachep, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800564 if (!oi)
565 return NULL;
566
Joel Becker2b4e30f2008-09-03 20:03:41 -0700567 jbd2_journal_init_jbd_inode(&oi->ip_jinode, &oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800568 return &oi->vfs_inode;
569}
570
571static void ocfs2_destroy_inode(struct inode *inode)
572{
573 kmem_cache_free(ocfs2_inode_cachep, OCFS2_I(inode));
574}
575
Mark Fasheh5a254032007-07-20 12:56:16 -0700576static unsigned long long ocfs2_max_file_offset(unsigned int bbits,
577 unsigned int cbits)
Mark Fashehccd979b2005-12-15 14:31:24 -0800578{
Mark Fasheh5a254032007-07-20 12:56:16 -0700579 unsigned int bytes = 1 << cbits;
580 unsigned int trim = bytes;
581 unsigned int bitshift = 32;
Mark Fashehccd979b2005-12-15 14:31:24 -0800582
Mark Fasheh5a254032007-07-20 12:56:16 -0700583 /*
584 * i_size and all block offsets in ocfs2 are always 64 bits
585 * wide. i_clusters is 32 bits, in cluster-sized units. So on
586 * 64 bit platforms, cluster size will be the limiting factor.
Mark Fashehccd979b2005-12-15 14:31:24 -0800587 */
588
589#if BITS_PER_LONG == 32
Bartlomiej Zolnierkiewicz90c699a2009-06-19 08:08:50 +0200590# if defined(CONFIG_LBDAF)
Alexey Dobriyan2ecd05a2006-10-11 01:22:05 -0700591 BUILD_BUG_ON(sizeof(sector_t) != 8);
Mark Fasheh5a254032007-07-20 12:56:16 -0700592 /*
593 * We might be limited by page cache size.
594 */
595 if (bytes > PAGE_CACHE_SIZE) {
596 bytes = PAGE_CACHE_SIZE;
597 trim = 1;
598 /*
599 * Shift by 31 here so that we don't get larger than
600 * MAX_LFS_FILESIZE
601 */
602 bitshift = 31;
603 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800604# else
Mark Fasheh5a254032007-07-20 12:56:16 -0700605 /*
606 * We are limited by the size of sector_t. Use block size, as
607 * that's what we expose to the VFS.
608 */
609 bytes = 1 << bbits;
610 trim = 1;
611 bitshift = 31;
Mark Fashehccd979b2005-12-15 14:31:24 -0800612# endif
613#endif
614
Mark Fasheh5a254032007-07-20 12:56:16 -0700615 /*
616 * Trim by a whole cluster when we can actually approach the
617 * on-disk limits. Otherwise we can overflow i_clusters when
618 * an extent start is at the max offset.
619 */
620 return (((unsigned long long)bytes) << bitshift) - trim;
Mark Fashehccd979b2005-12-15 14:31:24 -0800621}
622
623static int ocfs2_remount(struct super_block *sb, int *flags, char *data)
624{
625 int incompat_features;
626 int ret = 0;
Tiger Yangc0123ad2007-09-08 00:16:10 +0800627 struct mount_options parsed_options;
Mark Fashehccd979b2005-12-15 14:31:24 -0800628 struct ocfs2_super *osb = OCFS2_SB(sb);
629
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200630 lock_kernel();
631
Jan Kara5297aad2009-10-15 14:54:04 +0200632 if (!ocfs2_parse_options(sb, data, &parsed_options, 1) ||
633 !ocfs2_check_set_options(sb, &parsed_options)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800634 ret = -EINVAL;
635 goto out;
636 }
637
638 if ((osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) !=
Tiger Yangc0123ad2007-09-08 00:16:10 +0800639 (parsed_options.mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800640 ret = -EINVAL;
641 mlog(ML_ERROR, "Cannot change heartbeat mode on remount\n");
642 goto out;
643 }
644
645 if ((osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK) !=
Tiger Yangc0123ad2007-09-08 00:16:10 +0800646 (parsed_options.mount_opt & OCFS2_MOUNT_DATA_WRITEBACK)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800647 ret = -EINVAL;
648 mlog(ML_ERROR, "Cannot change data mode on remount\n");
649 goto out;
650 }
651
Joel Becker12462f12008-09-03 20:03:40 -0700652 /* Probably don't want this on remount; it might
653 * mess with other nodes */
654 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64) &&
655 (parsed_options.mount_opt & OCFS2_MOUNT_INODE64)) {
656 ret = -EINVAL;
657 mlog(ML_ERROR, "Cannot enable inode64 on remount\n");
658 goto out;
659 }
660
Mark Fashehccd979b2005-12-15 14:31:24 -0800661 /* We're going to/from readonly mode. */
662 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
Jan Kara19ece542008-08-21 20:13:17 +0200663 /* Disable quota accounting before remounting RO */
664 if (*flags & MS_RDONLY) {
665 ret = ocfs2_susp_quotas(osb, 0);
666 if (ret < 0)
667 goto out;
668 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800669 /* Lock here so the check of HARD_RO and the potential
670 * setting of SOFT_RO is atomic. */
671 spin_lock(&osb->osb_lock);
672 if (osb->osb_flags & OCFS2_OSB_HARD_RO) {
673 mlog(ML_ERROR, "Remount on readonly device is forbidden.\n");
674 ret = -EROFS;
675 goto unlock_osb;
676 }
677
678 if (*flags & MS_RDONLY) {
679 mlog(0, "Going to ro mode.\n");
680 sb->s_flags |= MS_RDONLY;
681 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
682 } else {
683 mlog(0, "Making ro filesystem writeable.\n");
684
685 if (osb->osb_flags & OCFS2_OSB_ERROR_FS) {
686 mlog(ML_ERROR, "Cannot remount RDWR "
687 "filesystem due to previous errors.\n");
688 ret = -EROFS;
689 goto unlock_osb;
690 }
691 incompat_features = OCFS2_HAS_RO_COMPAT_FEATURE(sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP);
692 if (incompat_features) {
693 mlog(ML_ERROR, "Cannot remount RDWR because "
694 "of unsupported optional features "
695 "(%x).\n", incompat_features);
696 ret = -EINVAL;
697 goto unlock_osb;
698 }
699 sb->s_flags &= ~MS_RDONLY;
700 osb->osb_flags &= ~OCFS2_OSB_SOFT_RO;
701 }
702unlock_osb:
703 spin_unlock(&osb->osb_lock);
Jan Kara19ece542008-08-21 20:13:17 +0200704 /* Enable quota accounting after remounting RW */
705 if (!ret && !(*flags & MS_RDONLY)) {
706 if (sb_any_quota_suspended(sb))
707 ret = ocfs2_susp_quotas(osb, 1);
708 else
709 ret = ocfs2_enable_quotas(osb);
710 if (ret < 0) {
711 /* Return back changes... */
712 spin_lock(&osb->osb_lock);
713 sb->s_flags |= MS_RDONLY;
714 osb->osb_flags |= OCFS2_OSB_SOFT_RO;
715 spin_unlock(&osb->osb_lock);
716 goto out;
717 }
718 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800719 }
720
721 if (!ret) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800722 /* Only save off the new mount options in case of a successful
723 * remount. */
Tiger Yangc0123ad2007-09-08 00:16:10 +0800724 osb->s_mount_opt = parsed_options.mount_opt;
725 osb->s_atime_quantum = parsed_options.atime_quantum;
726 osb->preferred_slot = parsed_options.slot;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800727 if (parsed_options.commit_interval)
728 osb->osb_commit_interval = parsed_options.commit_interval;
Mark Fashehe001e792007-11-07 14:21:45 -0800729
730 if (!ocfs2_is_hard_readonly(osb))
731 ocfs2_set_journal_params(osb);
Jan Kara57b09bb2009-10-15 14:54:05 +0200732
733 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
734 ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ?
735 MS_POSIXACL : 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800736 }
737out:
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200738 unlock_kernel();
Mark Fashehccd979b2005-12-15 14:31:24 -0800739 return ret;
740}
741
742static int ocfs2_sb_probe(struct super_block *sb,
743 struct buffer_head **bh,
Joel Becker73be1922009-01-06 14:57:08 -0800744 int *sector_size,
745 struct ocfs2_blockcheck_stats *stats)
Mark Fashehccd979b2005-12-15 14:31:24 -0800746{
Denis Chengbddb8eb32007-09-27 02:10:04 +0800747 int status, tmpstat;
Mark Fashehccd979b2005-12-15 14:31:24 -0800748 struct ocfs1_vol_disk_hdr *hdr;
749 struct ocfs2_dinode *di;
750 int blksize;
751
752 *bh = NULL;
753
754 /* may be > 512 */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400755 *sector_size = bdev_logical_block_size(sb->s_bdev);
Mark Fashehccd979b2005-12-15 14:31:24 -0800756 if (*sector_size > OCFS2_MAX_BLOCKSIZE) {
757 mlog(ML_ERROR, "Hardware sector size too large: %d (max=%d)\n",
758 *sector_size, OCFS2_MAX_BLOCKSIZE);
759 status = -EINVAL;
760 goto bail;
761 }
762
763 /* Can this really happen? */
764 if (*sector_size < OCFS2_MIN_BLOCKSIZE)
765 *sector_size = OCFS2_MIN_BLOCKSIZE;
766
767 /* check block zero for old format */
768 status = ocfs2_get_sector(sb, bh, 0, *sector_size);
769 if (status < 0) {
770 mlog_errno(status);
771 goto bail;
772 }
773 hdr = (struct ocfs1_vol_disk_hdr *) (*bh)->b_data;
774 if (hdr->major_version == OCFS1_MAJOR_VERSION) {
775 mlog(ML_ERROR, "incompatible version: %u.%u\n",
776 hdr->major_version, hdr->minor_version);
777 status = -EINVAL;
778 }
779 if (memcmp(hdr->signature, OCFS1_VOLUME_SIGNATURE,
780 strlen(OCFS1_VOLUME_SIGNATURE)) == 0) {
781 mlog(ML_ERROR, "incompatible volume signature: %8s\n",
782 hdr->signature);
783 status = -EINVAL;
784 }
785 brelse(*bh);
786 *bh = NULL;
787 if (status < 0) {
788 mlog(ML_ERROR, "This is an ocfs v1 filesystem which must be "
789 "upgraded before mounting with ocfs v2\n");
790 goto bail;
791 }
792
793 /*
794 * Now check at magic offset for 512, 1024, 2048, 4096
795 * blocksizes. 4096 is the maximum blocksize because it is
796 * the minimum clustersize.
797 */
798 status = -EINVAL;
799 for (blksize = *sector_size;
800 blksize <= OCFS2_MAX_BLOCKSIZE;
801 blksize <<= 1) {
802 tmpstat = ocfs2_get_sector(sb, bh,
803 OCFS2_SUPER_BLOCK_BLKNO,
804 blksize);
805 if (tmpstat < 0) {
806 status = tmpstat;
807 mlog_errno(status);
Joel Beckerfb5cbe92009-10-28 22:28:24 -0700808 break;
Mark Fashehccd979b2005-12-15 14:31:24 -0800809 }
810 di = (struct ocfs2_dinode *) (*bh)->b_data;
Joel Becker73be1922009-01-06 14:57:08 -0800811 memset(stats, 0, sizeof(struct ocfs2_blockcheck_stats));
Jan Kara1c1d9792009-07-22 13:17:19 +0200812 spin_lock_init(&stats->b_lock);
Joel Beckerfb5cbe92009-10-28 22:28:24 -0700813 tmpstat = ocfs2_verify_volume(di, *bh, blksize, stats);
814 if (tmpstat < 0) {
815 brelse(*bh);
816 *bh = NULL;
817 }
818 if (tmpstat != -EAGAIN) {
819 status = tmpstat;
Mark Fashehccd979b2005-12-15 14:31:24 -0800820 break;
Joel Beckerfb5cbe92009-10-28 22:28:24 -0700821 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800822 }
823
824bail:
825 return status;
826}
827
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800828static int ocfs2_verify_heartbeat(struct ocfs2_super *osb)
829{
830 if (ocfs2_mount_local(osb)) {
831 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
832 mlog(ML_ERROR, "Cannot heartbeat on a locally "
833 "mounted device.\n");
834 return -EINVAL;
835 }
836 }
837
Joel Beckerb61817e2008-02-01 15:08:23 -0800838 if (ocfs2_userspace_stack(osb)) {
839 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
840 mlog(ML_ERROR, "Userspace stack expected, but "
841 "o2cb heartbeat arguments passed to mount\n");
842 return -EINVAL;
843 }
844 }
845
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800846 if (!(osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
Joel Beckerb61817e2008-02-01 15:08:23 -0800847 if (!ocfs2_mount_local(osb) && !ocfs2_is_hard_readonly(osb) &&
848 !ocfs2_userspace_stack(osb)) {
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800849 mlog(ML_ERROR, "Heartbeat has to be started to mount "
850 "a read-write clustered device.\n");
851 return -EINVAL;
852 }
853 }
854
855 return 0;
856}
857
Joel Beckerb61817e2008-02-01 15:08:23 -0800858/*
859 * If we're using a userspace stack, mount should have passed
860 * a name that matches the disk. If not, mount should not
861 * have passed a stack.
862 */
863static int ocfs2_verify_userspace_stack(struct ocfs2_super *osb,
864 struct mount_options *mopt)
865{
866 if (!ocfs2_userspace_stack(osb) && mopt->cluster_stack[0]) {
867 mlog(ML_ERROR,
868 "cluster stack passed to mount, but this filesystem "
869 "does not support it\n");
870 return -EINVAL;
871 }
872
873 if (ocfs2_userspace_stack(osb) &&
874 strncmp(osb->osb_cluster_stack, mopt->cluster_stack,
875 OCFS2_STACK_LABEL_LEN)) {
876 mlog(ML_ERROR,
877 "cluster stack passed to mount (\"%s\") does not "
878 "match the filesystem (\"%s\")\n",
879 mopt->cluster_stack,
880 osb->osb_cluster_stack);
881 return -EINVAL;
882 }
883
884 return 0;
885}
886
Jan Kara19ece542008-08-21 20:13:17 +0200887static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend)
888{
889 int type;
890 struct super_block *sb = osb->sb;
891 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
892 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
893 int status = 0;
894
895 for (type = 0; type < MAXQUOTAS; type++) {
896 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
897 continue;
898 if (unsuspend)
Christoph Hellwig0f0dd622010-05-19 07:16:41 -0400899 status = dquot_resume(sb, type);
Jan Karaeea7feb2010-05-13 22:14:53 +0200900 else {
901 struct ocfs2_mem_dqinfo *oinfo;
902
903 /* Cancel periodic syncing before suspending */
904 oinfo = sb_dqinfo(sb, type)->dqi_priv;
905 cancel_delayed_work_sync(&oinfo->dqi_sync_work);
Christoph Hellwig0f0dd622010-05-19 07:16:41 -0400906 status = dquot_suspend(sb, type);
Jan Karaeea7feb2010-05-13 22:14:53 +0200907 }
Jan Kara19ece542008-08-21 20:13:17 +0200908 if (status < 0)
909 break;
910 }
911 if (status < 0)
912 mlog(ML_ERROR, "Failed to suspend/unsuspend quotas on "
913 "remount (error = %d).\n", status);
914 return status;
915}
916
917static int ocfs2_enable_quotas(struct ocfs2_super *osb)
918{
919 struct inode *inode[MAXQUOTAS] = { NULL, NULL };
920 struct super_block *sb = osb->sb;
921 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
922 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
923 unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
924 LOCAL_GROUP_QUOTA_SYSTEM_INODE };
925 int status;
926 int type;
927
928 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NEGATIVE_USAGE;
929 for (type = 0; type < MAXQUOTAS; type++) {
930 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
931 continue;
932 inode[type] = ocfs2_get_system_file_inode(osb, ino[type],
933 osb->slot_num);
934 if (!inode[type]) {
935 status = -ENOENT;
936 goto out_quota_off;
937 }
Christoph Hellwig287a8092010-05-19 07:16:45 -0400938 status = dquot_enable(inode[type], type, QFMT_OCFS2,
939 DQUOT_USAGE_ENABLED);
Jan Kara19ece542008-08-21 20:13:17 +0200940 if (status < 0)
941 goto out_quota_off;
942 }
943
944 for (type = 0; type < MAXQUOTAS; type++)
945 iput(inode[type]);
946 return 0;
947out_quota_off:
948 ocfs2_disable_quotas(osb);
949 for (type = 0; type < MAXQUOTAS; type++)
950 iput(inode[type]);
951 mlog_errno(status);
952 return status;
953}
954
955static void ocfs2_disable_quotas(struct ocfs2_super *osb)
956{
957 int type;
958 struct inode *inode;
959 struct super_block *sb = osb->sb;
Jan Karac06bcbf2010-05-13 22:14:53 +0200960 struct ocfs2_mem_dqinfo *oinfo;
Jan Kara19ece542008-08-21 20:13:17 +0200961
962 /* We mostly ignore errors in this function because there's not much
963 * we can do when we see them */
964 for (type = 0; type < MAXQUOTAS; type++) {
965 if (!sb_has_quota_loaded(sb, type))
966 continue;
Jan Karac06bcbf2010-05-13 22:14:53 +0200967 /* Cancel periodic syncing before we grab dqonoff_mutex */
968 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
982/* Handle quota on quotactl */
983static int ocfs2_quota_on(struct super_block *sb, int type, int format_id,
Christoph Hellwig307ae182010-05-19 07:16:43 -0400984 char *path)
Jan Kara19ece542008-08-21 20:13:17 +0200985{
986 unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
987 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
988
989 if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
990 return -EINVAL;
991
Christoph Hellwig287a8092010-05-19 07:16:45 -0400992 return dquot_enable(sb_dqopt(sb)->files[type], type,
993 format_id, DQUOT_LIMITS_ENABLED);
Jan Kara19ece542008-08-21 20:13:17 +0200994}
995
996/* Handle quota off quotactl */
Christoph Hellwig307ae182010-05-19 07:16:43 -0400997static int ocfs2_quota_off(struct super_block *sb, int type)
Jan Kara19ece542008-08-21 20:13:17 +0200998{
Christoph Hellwig0f0dd622010-05-19 07:16:41 -0400999 return dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
Jan Kara19ece542008-08-21 20:13:17 +02001000}
1001
Alexey Dobriyan0d54b212009-09-21 17:01:09 -07001002static const struct quotactl_ops ocfs2_quotactl_ops = {
Jan Kara19ece542008-08-21 20:13:17 +02001003 .quota_on = ocfs2_quota_on,
1004 .quota_off = ocfs2_quota_off,
Christoph Hellwig287a8092010-05-19 07:16:45 -04001005 .quota_sync = dquot_quota_sync,
1006 .get_info = dquot_get_dqinfo,
1007 .set_info = dquot_set_dqinfo,
1008 .get_dqblk = dquot_get_dqblk,
1009 .set_dqblk = dquot_set_dqblk,
Jan Kara19ece542008-08-21 20:13:17 +02001010};
1011
Mark Fashehccd979b2005-12-15 14:31:24 -08001012static int ocfs2_fill_super(struct super_block *sb, void *data, int silent)
1013{
1014 struct dentry *root;
1015 int status, sector_size;
Tiger Yangc0123ad2007-09-08 00:16:10 +08001016 struct mount_options parsed_options;
Mark Fashehccd979b2005-12-15 14:31:24 -08001017 struct inode *inode = NULL;
1018 struct ocfs2_super *osb = NULL;
1019 struct buffer_head *bh = NULL;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001020 char nodestr[8];
Joel Becker73be1922009-01-06 14:57:08 -08001021 struct ocfs2_blockcheck_stats stats;
Mark Fashehccd979b2005-12-15 14:31:24 -08001022
1023 mlog_entry("%p, %p, %i", sb, data, silent);
1024
Tiger Yangc0123ad2007-09-08 00:16:10 +08001025 if (!ocfs2_parse_options(sb, data, &parsed_options, 0)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001026 status = -EINVAL;
1027 goto read_super_error;
1028 }
1029
1030 /* probe for superblock */
Joel Becker73be1922009-01-06 14:57:08 -08001031 status = ocfs2_sb_probe(sb, &bh, &sector_size, &stats);
Mark Fashehccd979b2005-12-15 14:31:24 -08001032 if (status < 0) {
1033 mlog(ML_ERROR, "superblock probe failed!\n");
1034 goto read_super_error;
1035 }
1036
Joel Becker73be1922009-01-06 14:57:08 -08001037 status = ocfs2_initialize_super(sb, bh, sector_size, &stats);
Mark Fashehccd979b2005-12-15 14:31:24 -08001038 osb = OCFS2_SB(sb);
1039 if (status < 0) {
1040 mlog_errno(status);
1041 goto read_super_error;
1042 }
1043 brelse(bh);
1044 bh = NULL;
Tiger Yanga68979b2008-11-14 11:17:52 +08001045
Jan Kara5297aad2009-10-15 14:54:04 +02001046 if (!ocfs2_check_set_options(sb, &parsed_options)) {
1047 status = -EINVAL;
1048 goto read_super_error;
1049 }
Tiger Yangc0123ad2007-09-08 00:16:10 +08001050 osb->s_mount_opt = parsed_options.mount_opt;
1051 osb->s_atime_quantum = parsed_options.atime_quantum;
1052 osb->preferred_slot = parsed_options.slot;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001053 osb->osb_commit_interval = parsed_options.commit_interval;
Mark Fasheh73c8a802010-04-05 18:17:13 -07001054
1055 ocfs2_la_set_sizes(osb, parsed_options.localalloc_opt);
Mark Fashehd02f00c2009-12-07 13:10:48 -08001056 osb->osb_resv_level = parsed_options.resv_level;
Mark Fasheh83f92312010-04-05 18:17:16 -07001057 osb->osb_dir_resv_level = parsed_options.resv_level;
1058 if (parsed_options.dir_resv_level == -1)
1059 osb->osb_dir_resv_level = parsed_options.resv_level;
1060 else
1061 osb->osb_dir_resv_level = parsed_options.dir_resv_level;
Mark Fashehccd979b2005-12-15 14:31:24 -08001062
Joel Beckerb61817e2008-02-01 15:08:23 -08001063 status = ocfs2_verify_userspace_stack(osb, &parsed_options);
1064 if (status)
1065 goto read_super_error;
1066
Mark Fashehccd979b2005-12-15 14:31:24 -08001067 sb->s_magic = OCFS2_SUPER_MAGIC;
1068
Tiger Yanga68979b2008-11-14 11:17:52 +08001069 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1070 ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
1071
Mark Fashehccd979b2005-12-15 14:31:24 -08001072 /* Hard readonly mode only if: bdev_read_only, MS_RDONLY,
1073 * heartbeat=none */
1074 if (bdev_read_only(sb->s_bdev)) {
1075 if (!(sb->s_flags & MS_RDONLY)) {
1076 status = -EACCES;
1077 mlog(ML_ERROR, "Readonly device detected but readonly "
1078 "mount was not specified.\n");
1079 goto read_super_error;
1080 }
1081
1082 /* You should not be able to start a local heartbeat
1083 * on a readonly device. */
1084 if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
1085 status = -EROFS;
1086 mlog(ML_ERROR, "Local heartbeat specified on readonly "
1087 "device.\n");
1088 goto read_super_error;
1089 }
1090
1091 status = ocfs2_check_journals_nolocks(osb);
1092 if (status < 0) {
1093 if (status == -EROFS)
1094 mlog(ML_ERROR, "Recovery required on readonly "
1095 "file system, but write access is "
1096 "unavailable.\n");
1097 else
Sunil Mushran2bd63212010-01-25 16:57:38 -08001098 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001099 goto read_super_error;
1100 }
1101
1102 ocfs2_set_ro_flag(osb, 1);
1103
1104 printk(KERN_NOTICE "Readonly device detected. No cluster "
1105 "services will be utilized for this mount. Recovery "
1106 "will be skipped.\n");
1107 }
1108
1109 if (!ocfs2_is_hard_readonly(osb)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001110 if (sb->s_flags & MS_RDONLY)
1111 ocfs2_set_ro_flag(osb, 0);
1112 }
1113
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001114 status = ocfs2_verify_heartbeat(osb);
1115 if (status < 0) {
1116 mlog_errno(status);
1117 goto read_super_error;
1118 }
1119
Mark Fashehccd979b2005-12-15 14:31:24 -08001120 osb->osb_debug_root = debugfs_create_dir(osb->uuid_str,
1121 ocfs2_debugfs_root);
1122 if (!osb->osb_debug_root) {
1123 status = -EINVAL;
1124 mlog(ML_ERROR, "Unable to create per-mount debugfs root.\n");
1125 goto read_super_error;
1126 }
1127
Sunil Mushran50397502008-12-17 14:17:43 -08001128 osb->osb_ctxt = debugfs_create_file("fs_state", S_IFREG|S_IRUSR,
1129 osb->osb_debug_root,
1130 osb,
1131 &ocfs2_osb_debug_fops);
1132 if (!osb->osb_ctxt) {
1133 status = -EINVAL;
1134 mlog_errno(status);
1135 goto read_super_error;
1136 }
1137
Joel Becker73be1922009-01-06 14:57:08 -08001138 if (ocfs2_meta_ecc(osb)) {
1139 status = ocfs2_blockcheck_stats_debugfs_install(
1140 &osb->osb_ecc_stats,
1141 osb->osb_debug_root);
1142 if (status) {
1143 mlog(ML_ERROR,
1144 "Unable to create blockcheck statistics "
1145 "files\n");
1146 goto read_super_error;
1147 }
1148 }
1149
Mark Fashehccd979b2005-12-15 14:31:24 -08001150 status = ocfs2_mount_volume(sb);
1151 if (osb->root_inode)
1152 inode = igrab(osb->root_inode);
1153
1154 if (status < 0)
1155 goto read_super_error;
1156
1157 if (!inode) {
1158 status = -EIO;
1159 mlog_errno(status);
1160 goto read_super_error;
1161 }
1162
1163 root = d_alloc_root(inode);
1164 if (!root) {
1165 status = -ENOMEM;
1166 mlog_errno(status);
1167 goto read_super_error;
1168 }
1169
1170 sb->s_root = root;
1171
1172 ocfs2_complete_mount_recovery(osb);
1173
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001174 if (ocfs2_mount_local(osb))
1175 snprintf(nodestr, sizeof(nodestr), "local");
1176 else
Joel Becker19fdb622008-01-30 15:38:24 -08001177 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001178
1179 printk(KERN_INFO "ocfs2: Mounting device (%s) on (node %s, slot %d) "
Sunil Mushran781ee3e2006-04-27 16:41:31 -07001180 "with %s data mode.\n",
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001181 osb->dev_str, nodestr, osb->slot_num,
Mark Fashehccd979b2005-12-15 14:31:24 -08001182 osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK ? "writeback" :
1183 "ordered");
1184
1185 atomic_set(&osb->vol_state, VOLUME_MOUNTED);
1186 wake_up(&osb->osb_mount_event);
1187
Jan Kara19ece542008-08-21 20:13:17 +02001188 /* Now we can initialize quotas because we can afford to wait
1189 * for cluster locks recovery now. That also means that truncation
1190 * log recovery can happen but that waits for proper quota setup */
1191 if (!(sb->s_flags & MS_RDONLY)) {
1192 status = ocfs2_enable_quotas(osb);
1193 if (status < 0) {
1194 /* We have to err-out specially here because
1195 * s_root is already set */
1196 mlog_errno(status);
1197 atomic_set(&osb->vol_state, VOLUME_DISABLED);
1198 wake_up(&osb->osb_mount_event);
1199 mlog_exit(status);
1200 return status;
1201 }
1202 }
1203
1204 ocfs2_complete_quota_recovery(osb);
1205
1206 /* Now we wake up again for processes waiting for quotas */
1207 atomic_set(&osb->vol_state, VOLUME_MOUNTED_QUOTAS);
1208 wake_up(&osb->osb_mount_event);
1209
Sunil Mushrandf152c22009-06-22 11:40:07 -07001210 /* Start this when the mount is almost sure of being successful */
Jeff Mahoney8b712cd2009-07-07 17:22:12 -04001211 ocfs2_orphan_scan_start(osb);
Sunil Mushrandf152c22009-06-22 11:40:07 -07001212
Mark Fashehccd979b2005-12-15 14:31:24 -08001213 mlog_exit(status);
1214 return status;
1215
1216read_super_error:
Mark Fasheha81cb882008-10-07 14:25:16 -07001217 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001218
1219 if (inode)
1220 iput(inode);
1221
1222 if (osb) {
1223 atomic_set(&osb->vol_state, VOLUME_DISABLED);
1224 wake_up(&osb->osb_mount_event);
1225 ocfs2_dismount_volume(sb, 1);
1226 }
1227
1228 mlog_exit(status);
1229 return status;
1230}
1231
David Howells454e2392006-06-23 02:02:57 -07001232static int ocfs2_get_sb(struct file_system_type *fs_type,
1233 int flags,
1234 const char *dev_name,
1235 void *data,
1236 struct vfsmount *mnt)
Mark Fashehccd979b2005-12-15 14:31:24 -08001237{
David Howells454e2392006-06-23 02:02:57 -07001238 return get_sb_bdev(fs_type, flags, dev_name, data, ocfs2_fill_super,
1239 mnt);
Mark Fashehccd979b2005-12-15 14:31:24 -08001240}
1241
Jan Karaf7b1aa62009-07-20 12:12:36 +02001242static void ocfs2_kill_sb(struct super_block *sb)
1243{
1244 struct ocfs2_super *osb = OCFS2_SB(sb);
1245
Jan Kara5fd13182009-07-30 17:01:53 +02001246 /* Failed mount? */
1247 if (!osb || atomic_read(&osb->vol_state) == VOLUME_DISABLED)
1248 goto out;
1249
Jan Karaf7b1aa62009-07-20 12:12:36 +02001250 /* Prevent further queueing of inode drop events */
1251 spin_lock(&dentry_list_lock);
1252 ocfs2_set_osb_flag(osb, OCFS2_OSB_DROP_DENTRY_LOCK_IMMED);
1253 spin_unlock(&dentry_list_lock);
1254 /* Wait for work to finish and/or remove it */
1255 cancel_work_sync(&osb->dentry_lock_work);
Jan Kara5fd13182009-07-30 17:01:53 +02001256out:
Jan Karaf7b1aa62009-07-20 12:12:36 +02001257 kill_block_super(sb);
1258}
1259
Mark Fashehccd979b2005-12-15 14:31:24 -08001260static struct file_system_type ocfs2_fs_type = {
1261 .owner = THIS_MODULE,
1262 .name = "ocfs2",
1263 .get_sb = ocfs2_get_sb, /* is this called when we mount
1264 * the fs? */
Jan Karaf7b1aa62009-07-20 12:12:36 +02001265 .kill_sb = ocfs2_kill_sb,
1266
Mark Fasheh1ba9da22006-09-08 14:22:54 -07001267 .fs_flags = FS_REQUIRES_DEV|FS_RENAME_DOES_D_MOVE,
Mark Fashehccd979b2005-12-15 14:31:24 -08001268 .next = NULL
1269};
1270
Jan Kara5297aad2009-10-15 14:54:04 +02001271static int ocfs2_check_set_options(struct super_block *sb,
1272 struct mount_options *options)
1273{
1274 if (options->mount_opt & OCFS2_MOUNT_USRQUOTA &&
1275 !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1276 OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
1277 mlog(ML_ERROR, "User quotas were requested, but this "
1278 "filesystem does not have the feature enabled.\n");
1279 return 0;
1280 }
1281 if (options->mount_opt & OCFS2_MOUNT_GRPQUOTA &&
1282 !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1283 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
1284 mlog(ML_ERROR, "Group quotas were requested, but this "
1285 "filesystem does not have the feature enabled.\n");
1286 return 0;
1287 }
1288 if (options->mount_opt & OCFS2_MOUNT_POSIX_ACL &&
1289 !OCFS2_HAS_INCOMPAT_FEATURE(sb, OCFS2_FEATURE_INCOMPAT_XATTR)) {
1290 mlog(ML_ERROR, "ACL support requested but extended attributes "
1291 "feature is not enabled\n");
1292 return 0;
1293 }
1294 /* No ACL setting specified? Use XATTR feature... */
1295 if (!(options->mount_opt & (OCFS2_MOUNT_POSIX_ACL |
1296 OCFS2_MOUNT_NO_POSIX_ACL))) {
1297 if (OCFS2_HAS_INCOMPAT_FEATURE(sb, OCFS2_FEATURE_INCOMPAT_XATTR))
1298 options->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
1299 else
1300 options->mount_opt |= OCFS2_MOUNT_NO_POSIX_ACL;
1301 }
1302 return 1;
1303}
1304
Mark Fashehccd979b2005-12-15 14:31:24 -08001305static int ocfs2_parse_options(struct super_block *sb,
1306 char *options,
Tiger Yangc0123ad2007-09-08 00:16:10 +08001307 struct mount_options *mopt,
Mark Fashehccd979b2005-12-15 14:31:24 -08001308 int is_remount)
1309{
1310 int status;
1311 char *p;
1312
1313 mlog_entry("remount: %d, options: \"%s\"\n", is_remount,
1314 options ? options : "(none)");
1315
Mark Fashehd147b3d2007-11-07 14:40:36 -08001316 mopt->commit_interval = 0;
Sunil Mushran4b37fcb2010-04-13 18:00:31 -07001317 mopt->mount_opt = OCFS2_MOUNT_NOINTR;
Tiger Yangc0123ad2007-09-08 00:16:10 +08001318 mopt->atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
1319 mopt->slot = OCFS2_INVALID_SLOT;
Mark Fasheh73c8a802010-04-05 18:17:13 -07001320 mopt->localalloc_opt = -1;
Joel Beckerb61817e2008-02-01 15:08:23 -08001321 mopt->cluster_stack[0] = '\0';
Mark Fashehd02f00c2009-12-07 13:10:48 -08001322 mopt->resv_level = OCFS2_DEFAULT_RESV_LEVEL;
Mark Fasheh83f92312010-04-05 18:17:16 -07001323 mopt->dir_resv_level = -1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001324
1325 if (!options) {
1326 status = 1;
1327 goto bail;
1328 }
1329
1330 while ((p = strsep(&options, ",")) != NULL) {
1331 int token, option;
1332 substring_t args[MAX_OPT_ARGS];
1333
1334 if (!*p)
1335 continue;
1336
1337 token = match_token(p, tokens, args);
1338 switch (token) {
1339 case Opt_hb_local:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001340 mopt->mount_opt |= OCFS2_MOUNT_HB_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001341 break;
1342 case Opt_hb_none:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001343 mopt->mount_opt &= ~OCFS2_MOUNT_HB_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001344 break;
1345 case Opt_barrier:
1346 if (match_int(&args[0], &option)) {
1347 status = 0;
1348 goto bail;
1349 }
1350 if (option)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001351 mopt->mount_opt |= OCFS2_MOUNT_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -08001352 else
Tiger Yangc0123ad2007-09-08 00:16:10 +08001353 mopt->mount_opt &= ~OCFS2_MOUNT_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -08001354 break;
1355 case Opt_intr:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001356 mopt->mount_opt &= ~OCFS2_MOUNT_NOINTR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001357 break;
1358 case Opt_nointr:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001359 mopt->mount_opt |= OCFS2_MOUNT_NOINTR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001360 break;
1361 case Opt_err_panic:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001362 mopt->mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001363 break;
1364 case Opt_err_ro:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001365 mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_PANIC;
Mark Fashehccd979b2005-12-15 14:31:24 -08001366 break;
1367 case Opt_data_ordered:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001368 mopt->mount_opt &= ~OCFS2_MOUNT_DATA_WRITEBACK;
Mark Fashehccd979b2005-12-15 14:31:24 -08001369 break;
1370 case Opt_data_writeback:
Tiger Yangc0123ad2007-09-08 00:16:10 +08001371 mopt->mount_opt |= OCFS2_MOUNT_DATA_WRITEBACK;
Mark Fashehccd979b2005-12-15 14:31:24 -08001372 break;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001373 case Opt_user_xattr:
1374 mopt->mount_opt &= ~OCFS2_MOUNT_NOUSERXATTR;
1375 break;
1376 case Opt_nouser_xattr:
1377 mopt->mount_opt |= OCFS2_MOUNT_NOUSERXATTR;
1378 break;
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001379 case Opt_atime_quantum:
1380 if (match_int(&args[0], &option)) {
1381 status = 0;
1382 goto bail;
1383 }
1384 if (option >= 0)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001385 mopt->atime_quantum = option;
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001386 break;
Sunil Mushranbaf46612007-06-18 17:00:24 -07001387 case Opt_slot:
1388 option = 0;
1389 if (match_int(&args[0], &option)) {
1390 status = 0;
1391 goto bail;
1392 }
1393 if (option)
Tiger Yangc0123ad2007-09-08 00:16:10 +08001394 mopt->slot = (s16)option;
Sunil Mushranbaf46612007-06-18 17:00:24 -07001395 break;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001396 case Opt_commit:
1397 option = 0;
1398 if (match_int(&args[0], &option)) {
1399 status = 0;
1400 goto bail;
1401 }
1402 if (option < 0)
1403 return 0;
1404 if (option == 0)
Joel Becker2b4e30f2008-09-03 20:03:41 -07001405 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Mark Fashehd147b3d2007-11-07 14:40:36 -08001406 mopt->commit_interval = HZ * option;
1407 break;
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001408 case Opt_localalloc:
1409 option = 0;
1410 if (match_int(&args[0], &option)) {
1411 status = 0;
1412 goto bail;
1413 }
Mark Fasheh73c8a802010-04-05 18:17:13 -07001414 if (option >= 0)
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001415 mopt->localalloc_opt = option;
1416 break;
Mark Fasheh53fc6222007-12-20 16:49:04 -08001417 case Opt_localflocks:
1418 /*
1419 * Changing this during remount could race
1420 * flock() requests, or "unbalance" existing
1421 * ones (e.g., a lock is taken in one mode but
1422 * dropped in the other). If users care enough
1423 * to flip locking modes during remount, we
1424 * could add a "local" flag to individual
1425 * flock structures for proper tracking of
1426 * state.
1427 */
1428 if (!is_remount)
1429 mopt->mount_opt |= OCFS2_MOUNT_LOCALFLOCKS;
1430 break;
Joel Beckerb61817e2008-02-01 15:08:23 -08001431 case Opt_stack:
1432 /* Check both that the option we were passed
1433 * is of the right length and that it is a proper
1434 * string of the right length.
1435 */
1436 if (((args[0].to - args[0].from) !=
1437 OCFS2_STACK_LABEL_LEN) ||
1438 (strnlen(args[0].from,
1439 OCFS2_STACK_LABEL_LEN) !=
1440 OCFS2_STACK_LABEL_LEN)) {
1441 mlog(ML_ERROR,
1442 "Invalid cluster_stack option\n");
1443 status = 0;
1444 goto bail;
1445 }
1446 memcpy(mopt->cluster_stack, args[0].from,
1447 OCFS2_STACK_LABEL_LEN);
1448 mopt->cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
1449 break;
Joel Becker12462f12008-09-03 20:03:40 -07001450 case Opt_inode64:
1451 mopt->mount_opt |= OCFS2_MOUNT_INODE64;
1452 break;
Jan Kara19ece542008-08-21 20:13:17 +02001453 case Opt_usrquota:
Jan Kara19ece542008-08-21 20:13:17 +02001454 mopt->mount_opt |= OCFS2_MOUNT_USRQUOTA;
1455 break;
1456 case Opt_grpquota:
Jan Kara19ece542008-08-21 20:13:17 +02001457 mopt->mount_opt |= OCFS2_MOUNT_GRPQUOTA;
1458 break;
Tristan Ye7bdb0d12010-10-11 16:46:39 +08001459 case Opt_coherency_buffered:
1460 mopt->mount_opt |= OCFS2_MOUNT_COHERENCY_BUFFERED;
1461 break;
1462 case Opt_coherency_full:
1463 mopt->mount_opt &= ~OCFS2_MOUNT_COHERENCY_BUFFERED;
1464 break;
Tiger Yanga68979b2008-11-14 11:17:52 +08001465 case Opt_acl:
1466 mopt->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
Jan Kara5297aad2009-10-15 14:54:04 +02001467 mopt->mount_opt &= ~OCFS2_MOUNT_NO_POSIX_ACL;
Tiger Yanga68979b2008-11-14 11:17:52 +08001468 break;
1469 case Opt_noacl:
Jan Kara5297aad2009-10-15 14:54:04 +02001470 mopt->mount_opt |= OCFS2_MOUNT_NO_POSIX_ACL;
Tiger Yanga68979b2008-11-14 11:17:52 +08001471 mopt->mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
1472 break;
Mark Fashehd02f00c2009-12-07 13:10:48 -08001473 case Opt_resv_level:
1474 if (is_remount)
1475 break;
1476 if (match_int(&args[0], &option)) {
1477 status = 0;
1478 goto bail;
1479 }
1480 if (option >= OCFS2_MIN_RESV_LEVEL &&
1481 option < OCFS2_MAX_RESV_LEVEL)
1482 mopt->resv_level = option;
1483 break;
Mark Fasheh83f92312010-04-05 18:17:16 -07001484 case Opt_dir_resv_level:
1485 if (is_remount)
1486 break;
1487 if (match_int(&args[0], &option)) {
1488 status = 0;
1489 goto bail;
1490 }
1491 if (option >= OCFS2_MIN_RESV_LEVEL &&
1492 option < OCFS2_MAX_RESV_LEVEL)
1493 mopt->dir_resv_level = option;
1494 break;
Mark Fashehccd979b2005-12-15 14:31:24 -08001495 default:
1496 mlog(ML_ERROR,
1497 "Unrecognized mount option \"%s\" "
1498 "or missing value\n", p);
1499 status = 0;
1500 goto bail;
1501 }
1502 }
1503
1504 status = 1;
1505
1506bail:
1507 mlog_exit(status);
1508 return status;
1509}
1510
Sunil Mushrand5500712007-09-06 13:34:16 -07001511static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
1512{
1513 struct ocfs2_super *osb = OCFS2_SB(mnt->mnt_sb);
1514 unsigned long opts = osb->s_mount_opt;
Mark Fashehebcee4b2008-07-28 14:55:20 -07001515 unsigned int local_alloc_megs;
Sunil Mushrand5500712007-09-06 13:34:16 -07001516
1517 if (opts & OCFS2_MOUNT_HB_LOCAL)
1518 seq_printf(s, ",_netdev,heartbeat=local");
1519 else
1520 seq_printf(s, ",heartbeat=none");
1521
1522 if (opts & OCFS2_MOUNT_NOINTR)
1523 seq_printf(s, ",nointr");
1524
1525 if (opts & OCFS2_MOUNT_DATA_WRITEBACK)
1526 seq_printf(s, ",data=writeback");
1527 else
1528 seq_printf(s, ",data=ordered");
1529
1530 if (opts & OCFS2_MOUNT_BARRIER)
1531 seq_printf(s, ",barrier=1");
1532
1533 if (opts & OCFS2_MOUNT_ERRORS_PANIC)
1534 seq_printf(s, ",errors=panic");
1535 else
1536 seq_printf(s, ",errors=remount-ro");
1537
1538 if (osb->preferred_slot != OCFS2_INVALID_SLOT)
1539 seq_printf(s, ",preferred_slot=%d", osb->preferred_slot);
1540
1541 if (osb->s_atime_quantum != OCFS2_DEFAULT_ATIME_QUANTUM)
1542 seq_printf(s, ",atime_quantum=%u", osb->s_atime_quantum);
1543
Mark Fashehd147b3d2007-11-07 14:40:36 -08001544 if (osb->osb_commit_interval)
1545 seq_printf(s, ",commit=%u",
1546 (unsigned) (osb->osb_commit_interval / HZ));
1547
Mark Fashehebcee4b2008-07-28 14:55:20 -07001548 local_alloc_megs = osb->local_alloc_bits >> (20 - osb->s_clustersize_bits);
Mark Fasheh6b820212010-04-05 18:17:14 -07001549 if (local_alloc_megs != ocfs2_la_default_mb(osb))
Mark Fashehebcee4b2008-07-28 14:55:20 -07001550 seq_printf(s, ",localalloc=%d", local_alloc_megs);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001551
Mark Fasheh53fc6222007-12-20 16:49:04 -08001552 if (opts & OCFS2_MOUNT_LOCALFLOCKS)
1553 seq_printf(s, ",localflocks,");
1554
Joel Beckerb61817e2008-02-01 15:08:23 -08001555 if (osb->osb_cluster_stack[0])
1556 seq_printf(s, ",cluster_stack=%.*s", OCFS2_STACK_LABEL_LEN,
1557 osb->osb_cluster_stack);
Jan Kara19ece542008-08-21 20:13:17 +02001558 if (opts & OCFS2_MOUNT_USRQUOTA)
1559 seq_printf(s, ",usrquota");
1560 if (opts & OCFS2_MOUNT_GRPQUOTA)
1561 seq_printf(s, ",grpquota");
Joel Beckerb61817e2008-02-01 15:08:23 -08001562
Tristan Ye7bdb0d12010-10-11 16:46:39 +08001563 if (opts & OCFS2_MOUNT_COHERENCY_BUFFERED)
1564 seq_printf(s, ",coherency=buffered");
1565 else
1566 seq_printf(s, ",coherency=full");
1567
Sunil Mushranb0f73cf2008-09-05 11:29:14 -07001568 if (opts & OCFS2_MOUNT_NOUSERXATTR)
1569 seq_printf(s, ",nouser_xattr");
1570 else
1571 seq_printf(s, ",user_xattr");
1572
Joel Becker12462f12008-09-03 20:03:40 -07001573 if (opts & OCFS2_MOUNT_INODE64)
1574 seq_printf(s, ",inode64");
1575
Tiger Yanga68979b2008-11-14 11:17:52 +08001576 if (opts & OCFS2_MOUNT_POSIX_ACL)
1577 seq_printf(s, ",acl");
1578 else
1579 seq_printf(s, ",noacl");
Tiger Yanga68979b2008-11-14 11:17:52 +08001580
Mark Fashehd02f00c2009-12-07 13:10:48 -08001581 if (osb->osb_resv_level != OCFS2_DEFAULT_RESV_LEVEL)
1582 seq_printf(s, ",resv_level=%d", osb->osb_resv_level);
1583
Mark Fasheh83f92312010-04-05 18:17:16 -07001584 if (osb->osb_dir_resv_level != osb->osb_resv_level)
1585 seq_printf(s, ",dir_resv_level=%d", osb->osb_resv_level);
1586
Sunil Mushrand5500712007-09-06 13:34:16 -07001587 return 0;
1588}
1589
Mark Fashehccd979b2005-12-15 14:31:24 -08001590static int __init ocfs2_init(void)
1591{
1592 int status;
1593
1594 mlog_entry_void();
1595
1596 ocfs2_print_version();
1597
Mark Fashehccd979b2005-12-15 14:31:24 -08001598 status = init_ocfs2_uptodate_cache();
1599 if (status < 0) {
1600 mlog_errno(status);
1601 goto leave;
1602 }
1603
1604 status = ocfs2_initialize_mem_caches();
1605 if (status < 0) {
1606 mlog_errno(status);
1607 goto leave;
1608 }
1609
1610 ocfs2_wq = create_singlethread_workqueue("ocfs2_wq");
1611 if (!ocfs2_wq) {
1612 status = -ENOMEM;
1613 goto leave;
1614 }
1615
Mark Fashehccd979b2005-12-15 14:31:24 -08001616 ocfs2_debugfs_root = debugfs_create_dir("ocfs2", NULL);
1617 if (!ocfs2_debugfs_root) {
1618 status = -EFAULT;
1619 mlog(ML_ERROR, "Unable to create ocfs2 debugfs root.\n");
1620 }
1621
Mark Fasheh171bf932008-10-20 15:36:47 +02001622 status = ocfs2_quota_setup();
1623 if (status)
1624 goto leave;
1625
Joel Becker63e0c482008-01-30 16:58:36 -08001626 ocfs2_set_locking_protocol();
1627
Jan Kara9e33d692008-08-25 19:56:50 +02001628 status = register_quota_format(&ocfs2_quota_format);
Mark Fashehccd979b2005-12-15 14:31:24 -08001629leave:
1630 if (status < 0) {
Mark Fasheh171bf932008-10-20 15:36:47 +02001631 ocfs2_quota_shutdown();
Mark Fashehccd979b2005-12-15 14:31:24 -08001632 ocfs2_free_mem_caches();
1633 exit_ocfs2_uptodate_cache();
Mark Fashehccd979b2005-12-15 14:31:24 -08001634 }
1635
1636 mlog_exit(status);
1637
1638 if (status >= 0) {
1639 return register_filesystem(&ocfs2_fs_type);
1640 } else
1641 return -1;
1642}
1643
1644static void __exit ocfs2_exit(void)
1645{
1646 mlog_entry_void();
1647
Mark Fasheh171bf932008-10-20 15:36:47 +02001648 ocfs2_quota_shutdown();
1649
Mark Fashehccd979b2005-12-15 14:31:24 -08001650 if (ocfs2_wq) {
1651 flush_workqueue(ocfs2_wq);
1652 destroy_workqueue(ocfs2_wq);
1653 }
1654
Jan Kara9e33d692008-08-25 19:56:50 +02001655 unregister_quota_format(&ocfs2_quota_format);
1656
Mark Fashehccd979b2005-12-15 14:31:24 -08001657 debugfs_remove(ocfs2_debugfs_root);
1658
1659 ocfs2_free_mem_caches();
1660
1661 unregister_filesystem(&ocfs2_fs_type);
1662
Mark Fashehccd979b2005-12-15 14:31:24 -08001663 exit_ocfs2_uptodate_cache();
1664
1665 mlog_exit_void();
1666}
1667
1668static void ocfs2_put_super(struct super_block *sb)
1669{
1670 mlog_entry("(0x%p)\n", sb);
1671
Christoph Hellwig6cfd0142009-05-05 15:40:36 +02001672 lock_kernel();
1673
Mark Fashehccd979b2005-12-15 14:31:24 -08001674 ocfs2_sync_blockdev(sb);
1675 ocfs2_dismount_volume(sb, 0);
1676
Christoph Hellwig6cfd0142009-05-05 15:40:36 +02001677 unlock_kernel();
1678
Mark Fashehccd979b2005-12-15 14:31:24 -08001679 mlog_exit_void();
1680}
1681
David Howells726c3342006-06-23 02:02:58 -07001682static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
Mark Fashehccd979b2005-12-15 14:31:24 -08001683{
1684 struct ocfs2_super *osb;
1685 u32 numbits, freebits;
1686 int status;
1687 struct ocfs2_dinode *bm_lock;
1688 struct buffer_head *bh = NULL;
1689 struct inode *inode = NULL;
1690
David Howells726c3342006-06-23 02:02:58 -07001691 mlog_entry("(%p, %p)\n", dentry->d_sb, buf);
Mark Fashehccd979b2005-12-15 14:31:24 -08001692
David Howells726c3342006-06-23 02:02:58 -07001693 osb = OCFS2_SB(dentry->d_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001694
1695 inode = ocfs2_get_system_file_inode(osb,
1696 GLOBAL_BITMAP_SYSTEM_INODE,
1697 OCFS2_INVALID_SLOT);
1698 if (!inode) {
1699 mlog(ML_ERROR, "failed to get bitmap inode\n");
1700 status = -EIO;
1701 goto bail;
1702 }
1703
Mark Fashehe63aecb62007-10-18 15:30:42 -07001704 status = ocfs2_inode_lock(inode, &bh, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001705 if (status < 0) {
1706 mlog_errno(status);
1707 goto bail;
1708 }
1709
1710 bm_lock = (struct ocfs2_dinode *) bh->b_data;
1711
1712 numbits = le32_to_cpu(bm_lock->id1.bitmap1.i_total);
1713 freebits = numbits - le32_to_cpu(bm_lock->id1.bitmap1.i_used);
1714
1715 buf->f_type = OCFS2_SUPER_MAGIC;
David Howells726c3342006-06-23 02:02:58 -07001716 buf->f_bsize = dentry->d_sb->s_blocksize;
Mark Fashehccd979b2005-12-15 14:31:24 -08001717 buf->f_namelen = OCFS2_MAX_FILENAME_LEN;
1718 buf->f_blocks = ((sector_t) numbits) *
1719 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1720 buf->f_bfree = ((sector_t) freebits) *
1721 (osb->s_clustersize >> osb->sb->s_blocksize_bits);
1722 buf->f_bavail = buf->f_bfree;
1723 buf->f_files = numbits;
1724 buf->f_ffree = freebits;
Coly Li837711f2009-01-16 16:33:05 +08001725 buf->f_fsid.val[0] = crc32_le(0, osb->uuid_str, OCFS2_VOL_UUID_LEN)
1726 & 0xFFFFFFFFUL;
1727 buf->f_fsid.val[1] = crc32_le(0, osb->uuid_str + OCFS2_VOL_UUID_LEN,
1728 OCFS2_VOL_UUID_LEN) & 0xFFFFFFFFUL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001729
1730 brelse(bh);
1731
Mark Fashehe63aecb62007-10-18 15:30:42 -07001732 ocfs2_inode_unlock(inode, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001733 status = 0;
1734bail:
1735 if (inode)
1736 iput(inode);
1737
1738 mlog_exit(status);
1739
1740 return status;
1741}
1742
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001743static void ocfs2_inode_init_once(void *data)
Mark Fashehccd979b2005-12-15 14:31:24 -08001744{
1745 struct ocfs2_inode_info *oi = data;
1746
Christoph Lametera35afb82007-05-16 22:10:57 -07001747 oi->ip_flags = 0;
1748 oi->ip_open_count = 0;
1749 spin_lock_init(&oi->ip_lock);
1750 ocfs2_extent_map_init(&oi->vfs_inode);
1751 INIT_LIST_HEAD(&oi->ip_io_markers);
Christoph Lametera35afb82007-05-16 22:10:57 -07001752 oi->ip_dir_start_lookup = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001753
Christoph Lametera35afb82007-05-16 22:10:57 -07001754 init_rwsem(&oi->ip_alloc_sem);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001755 init_rwsem(&oi->ip_xattr_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -07001756 mutex_init(&oi->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001757
Christoph Lametera35afb82007-05-16 22:10:57 -07001758 oi->ip_blkno = 0ULL;
1759 oi->ip_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001760
Mark Fasheh4fe370a2009-12-07 13:15:40 -08001761 ocfs2_resv_init_once(&oi->ip_la_data_resv);
1762
Christoph Lametera35afb82007-05-16 22:10:57 -07001763 ocfs2_lock_res_init_once(&oi->ip_rw_lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001764 ocfs2_lock_res_init_once(&oi->ip_inode_lockres);
Christoph Lametera35afb82007-05-16 22:10:57 -07001765 ocfs2_lock_res_init_once(&oi->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001766
Joel Becker8cb471e2009-02-10 20:00:41 -08001767 ocfs2_metadata_cache_init(INODE_CACHE(&oi->vfs_inode),
Joel Becker6e5a3d72009-02-10 19:00:37 -08001768 &ocfs2_inode_caching_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08001769
Christoph Lametera35afb82007-05-16 22:10:57 -07001770 inode_init_once(&oi->vfs_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001771}
1772
1773static int ocfs2_initialize_mem_caches(void)
1774{
1775 ocfs2_inode_cachep = kmem_cache_create("ocfs2_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001776 sizeof(struct ocfs2_inode_info),
1777 0,
1778 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1779 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001780 ocfs2_inode_init_once);
Jan Kara9e33d692008-08-25 19:56:50 +02001781 ocfs2_dquot_cachep = kmem_cache_create("ocfs2_dquot_cache",
1782 sizeof(struct ocfs2_dquot),
1783 0,
1784 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1785 SLAB_MEM_SPREAD),
1786 NULL);
1787 ocfs2_qf_chunk_cachep = kmem_cache_create("ocfs2_qf_chunk_cache",
1788 sizeof(struct ocfs2_quota_chunk),
1789 0,
1790 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
1791 NULL);
1792 if (!ocfs2_inode_cachep || !ocfs2_dquot_cachep ||
1793 !ocfs2_qf_chunk_cachep) {
1794 if (ocfs2_inode_cachep)
1795 kmem_cache_destroy(ocfs2_inode_cachep);
1796 if (ocfs2_dquot_cachep)
1797 kmem_cache_destroy(ocfs2_dquot_cachep);
1798 if (ocfs2_qf_chunk_cachep)
1799 kmem_cache_destroy(ocfs2_qf_chunk_cachep);
Mark Fashehccd979b2005-12-15 14:31:24 -08001800 return -ENOMEM;
Jan Kara9e33d692008-08-25 19:56:50 +02001801 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001802
Mark Fashehccd979b2005-12-15 14:31:24 -08001803 return 0;
1804}
1805
1806static void ocfs2_free_mem_caches(void)
1807{
1808 if (ocfs2_inode_cachep)
1809 kmem_cache_destroy(ocfs2_inode_cachep);
Mark Fashehccd979b2005-12-15 14:31:24 -08001810 ocfs2_inode_cachep = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +02001811
1812 if (ocfs2_dquot_cachep)
1813 kmem_cache_destroy(ocfs2_dquot_cachep);
1814 ocfs2_dquot_cachep = NULL;
1815
1816 if (ocfs2_qf_chunk_cachep)
1817 kmem_cache_destroy(ocfs2_qf_chunk_cachep);
1818 ocfs2_qf_chunk_cachep = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001819}
1820
1821static int ocfs2_get_sector(struct super_block *sb,
1822 struct buffer_head **bh,
1823 int block,
1824 int sect_size)
1825{
1826 if (!sb_set_blocksize(sb, sect_size)) {
1827 mlog(ML_ERROR, "unable to set blocksize\n");
1828 return -EIO;
1829 }
1830
1831 *bh = sb_getblk(sb, block);
1832 if (!*bh) {
1833 mlog_errno(-EIO);
1834 return -EIO;
1835 }
1836 lock_buffer(*bh);
1837 if (!buffer_dirty(*bh))
1838 clear_buffer_uptodate(*bh);
1839 unlock_buffer(*bh);
1840 ll_rw_block(READ, 1, bh);
1841 wait_on_buffer(*bh);
wengang wang28d57d42009-02-13 10:11:47 +08001842 if (!buffer_uptodate(*bh)) {
1843 mlog_errno(-EIO);
1844 brelse(*bh);
1845 *bh = NULL;
1846 return -EIO;
1847 }
1848
Mark Fashehccd979b2005-12-15 14:31:24 -08001849 return 0;
1850}
1851
Mark Fashehccd979b2005-12-15 14:31:24 -08001852static int ocfs2_mount_volume(struct super_block *sb)
1853{
1854 int status = 0;
1855 int unlock_super = 0;
1856 struct ocfs2_super *osb = OCFS2_SB(sb);
1857
1858 mlog_entry_void();
1859
1860 if (ocfs2_is_hard_readonly(osb))
1861 goto leave;
1862
Mark Fashehccd979b2005-12-15 14:31:24 -08001863 status = ocfs2_dlm_init(osb);
1864 if (status < 0) {
1865 mlog_errno(status);
1866 goto leave;
1867 }
1868
Mark Fashehccd979b2005-12-15 14:31:24 -08001869 status = ocfs2_super_lock(osb, 1);
1870 if (status < 0) {
1871 mlog_errno(status);
1872 goto leave;
1873 }
1874 unlock_super = 1;
1875
1876 /* This will load up the node map and add ourselves to it. */
1877 status = ocfs2_find_slot(osb);
1878 if (status < 0) {
1879 mlog_errno(status);
1880 goto leave;
1881 }
1882
Mark Fashehccd979b2005-12-15 14:31:24 -08001883 /* load all node-local system inodes */
1884 status = ocfs2_init_local_system_inodes(osb);
1885 if (status < 0) {
1886 mlog_errno(status);
1887 goto leave;
1888 }
1889
1890 status = ocfs2_check_volume(osb);
1891 if (status < 0) {
1892 mlog_errno(status);
1893 goto leave;
1894 }
1895
1896 status = ocfs2_truncate_log_init(osb);
Tao Ma06c59bb2009-05-19 06:47:20 +08001897 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08001898 mlog_errno(status);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001899
Mark Fashehccd979b2005-12-15 14:31:24 -08001900leave:
1901 if (unlock_super)
1902 ocfs2_super_unlock(osb, 1);
1903
1904 mlog_exit(status);
1905 return status;
1906}
1907
Mark Fashehccd979b2005-12-15 14:31:24 -08001908static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
1909{
Joel Becker286eaa92008-02-01 15:03:57 -08001910 int tmp, hangup_needed = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001911 struct ocfs2_super *osb = NULL;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001912 char nodestr[8];
Mark Fashehccd979b2005-12-15 14:31:24 -08001913
1914 mlog_entry("(0x%p)\n", sb);
1915
1916 BUG_ON(!sb);
1917 osb = OCFS2_SB(sb);
1918 BUG_ON(!osb);
1919
Sunil Mushran50397502008-12-17 14:17:43 -08001920 debugfs_remove(osb->osb_ctxt);
1921
Jan Karaf7b1aa62009-07-20 12:12:36 +02001922 /*
1923 * Flush inode dropping work queue so that deletes are
1924 * performed while the filesystem is still working
1925 */
1926 ocfs2_drop_all_dl_inodes(osb);
1927
Sunil Mushran692684e2009-06-19 16:53:17 -07001928 /* Orphan scan should be stopped as early as possible */
1929 ocfs2_orphan_scan_stop(osb);
1930
Jan Kara19ece542008-08-21 20:13:17 +02001931 ocfs2_disable_quotas(osb);
1932
Mark Fashehccd979b2005-12-15 14:31:24 -08001933 ocfs2_shutdown_local_alloc(osb);
1934
1935 ocfs2_truncate_log_shutdown(osb);
1936
Joel Becker553abd02008-02-01 12:03:57 -08001937 /* This will disable recovery and flush any recovery work. */
1938 ocfs2_recovery_exit(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001939
1940 ocfs2_journal_shutdown(osb);
1941
1942 ocfs2_sync_blockdev(sb);
1943
Tao Ma374a2632009-08-24 11:13:37 +08001944 ocfs2_purge_refcount_trees(osb);
1945
Joel Becker4670c462008-02-01 14:39:35 -08001946 /* No cluster connection means we've failed during mount, so skip
1947 * all the steps which depended on that to complete. */
1948 if (osb->cconn) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001949 tmp = ocfs2_super_lock(osb, 1);
1950 if (tmp < 0) {
1951 mlog_errno(tmp);
1952 return;
1953 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001954 }
1955
Mark Fasheh19b613d2007-10-04 14:47:09 -07001956 if (osb->slot_num != OCFS2_INVALID_SLOT)
1957 ocfs2_put_slot(osb);
1958
Joel Becker4670c462008-02-01 14:39:35 -08001959 if (osb->cconn)
Mark Fasheh19b613d2007-10-04 14:47:09 -07001960 ocfs2_super_unlock(osb, 1);
1961
Mark Fashehccd979b2005-12-15 14:31:24 -08001962 ocfs2_release_system_inodes(osb);
1963
Joel Becker6953b4c2008-01-29 16:59:56 -08001964 /*
Joel Becker6953b4c2008-01-29 16:59:56 -08001965 * If we're dismounting due to mount error, mount.ocfs2 will clean
1966 * up heartbeat. If we're a local mount, there is no heartbeat.
1967 * If we failed before we got a uuid_str yet, we can't stop
1968 * heartbeat. Otherwise, do it.
1969 */
1970 if (!mnt_err && !ocfs2_mount_local(osb) && osb->uuid_str)
Joel Becker286eaa92008-02-01 15:03:57 -08001971 hangup_needed = 1;
1972
1973 if (osb->cconn)
1974 ocfs2_dlm_shutdown(osb, hangup_needed);
1975
Joel Becker73be1922009-01-06 14:57:08 -08001976 ocfs2_blockcheck_stats_debugfs_remove(&osb->osb_ecc_stats);
Joel Becker286eaa92008-02-01 15:03:57 -08001977 debugfs_remove(osb->osb_debug_root);
1978
1979 if (hangup_needed)
Joel Becker6953b4c2008-01-29 16:59:56 -08001980 ocfs2_cluster_hangup(osb->uuid_str, strlen(osb->uuid_str));
Mark Fashehccd979b2005-12-15 14:31:24 -08001981
1982 atomic_set(&osb->vol_state, VOLUME_DISMOUNTED);
1983
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001984 if (ocfs2_mount_local(osb))
1985 snprintf(nodestr, sizeof(nodestr), "local");
1986 else
Joel Becker19fdb622008-01-30 15:38:24 -08001987 snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001988
1989 printk(KERN_INFO "ocfs2: Unmounting device (%s) on (node %s)\n",
1990 osb->dev_str, nodestr);
Mark Fashehccd979b2005-12-15 14:31:24 -08001991
1992 ocfs2_delete_osb(osb);
1993 kfree(osb);
1994 sb->s_dev = 0;
1995 sb->s_fs_info = NULL;
1996}
1997
1998static int ocfs2_setup_osb_uuid(struct ocfs2_super *osb, const unsigned char *uuid,
1999 unsigned uuid_bytes)
2000{
2001 int i, ret;
2002 char *ptr;
2003
2004 BUG_ON(uuid_bytes != OCFS2_VOL_UUID_LEN);
2005
Robert P. J. Daycd861282006-12-13 00:34:52 -08002006 osb->uuid_str = kzalloc(OCFS2_VOL_UUID_LEN * 2 + 1, GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08002007 if (osb->uuid_str == NULL)
2008 return -ENOMEM;
2009
Mark Fashehccd979b2005-12-15 14:31:24 -08002010 for (i = 0, ptr = osb->uuid_str; i < OCFS2_VOL_UUID_LEN; i++) {
2011 /* print with null */
2012 ret = snprintf(ptr, 3, "%02X", uuid[i]);
2013 if (ret != 2) /* drop super cleans up */
2014 return -EINVAL;
2015 /* then only advance past the last char */
2016 ptr += 2;
2017 }
2018
2019 return 0;
2020}
2021
Patrick J. LoPresti3bdb8ef2010-07-22 15:05:57 -07002022/* Make sure entire volume is addressable by our journal. Requires
2023 osb_clusters_at_boot to be valid and for the journal to have been
2024 initialized by ocfs2_journal_init(). */
2025static int ocfs2_journal_addressable(struct ocfs2_super *osb)
2026{
2027 int status = 0;
2028 u64 max_block =
2029 ocfs2_clusters_to_blocks(osb->sb,
2030 osb->osb_clusters_at_boot) - 1;
2031
2032 /* 32-bit block number is always OK. */
2033 if (max_block <= (u32)~0ULL)
2034 goto out;
2035
2036 /* Volume is "huge", so see if our journal is new enough to
2037 support it. */
2038 if (!(OCFS2_HAS_COMPAT_FEATURE(osb->sb,
2039 OCFS2_FEATURE_COMPAT_JBD2_SB) &&
2040 jbd2_journal_check_used_features(osb->journal->j_journal, 0, 0,
2041 JBD2_FEATURE_INCOMPAT_64BIT))) {
2042 mlog(ML_ERROR, "The journal cannot address the entire volume. "
2043 "Enable the 'block64' journal option with tunefs.ocfs2");
2044 status = -EFBIG;
2045 goto out;
2046 }
2047
2048 out:
2049 return status;
2050}
2051
Mark Fashehccd979b2005-12-15 14:31:24 -08002052static int ocfs2_initialize_super(struct super_block *sb,
2053 struct buffer_head *bh,
Joel Becker73be1922009-01-06 14:57:08 -08002054 int sector_size,
2055 struct ocfs2_blockcheck_stats *stats)
Mark Fashehccd979b2005-12-15 14:31:24 -08002056{
Denis Chengbddb8eb32007-09-27 02:10:04 +08002057 int status;
Mark Fasheh5a254032007-07-20 12:56:16 -07002058 int i, cbits, bbits;
2059 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08002060 struct inode *inode = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002061 struct ocfs2_journal *journal;
2062 __le32 uuid_net_key;
2063 struct ocfs2_super *osb;
Patrick J. LoPresti3bdb8ef2010-07-22 15:05:57 -07002064 u64 total_blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -08002065
2066 mlog_entry_void();
2067
Robert P. J. Daycd861282006-12-13 00:34:52 -08002068 osb = kzalloc(sizeof(struct ocfs2_super), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08002069 if (!osb) {
2070 status = -ENOMEM;
2071 mlog_errno(status);
2072 goto bail;
2073 }
2074
2075 sb->s_fs_info = osb;
2076 sb->s_op = &ocfs2_sops;
2077 sb->s_export_op = &ocfs2_export_ops;
Jan Kara19ece542008-08-21 20:13:17 +02002078 sb->s_qcop = &ocfs2_quotactl_ops;
2079 sb->dq_op = &ocfs2_quota_operations;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002080 sb->s_xattr = ocfs2_xattr_handlers;
Mark Fashehe0dceaf2007-08-09 16:52:30 -07002081 sb->s_time_gran = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08002082 sb->s_flags |= MS_NOATIME;
2083 /* this is needed to support O_LARGEFILE */
Mark Fasheh5a254032007-07-20 12:56:16 -07002084 cbits = le32_to_cpu(di->id2.i_super.s_clustersize_bits);
2085 bbits = le32_to_cpu(di->id2.i_super.s_blocksize_bits);
2086 sb->s_maxbytes = ocfs2_max_file_offset(bbits, cbits);
Mark Fashehccd979b2005-12-15 14:31:24 -08002087
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002088 osb->osb_dx_mask = (1 << (cbits - bbits)) - 1;
2089
2090 for (i = 0; i < 3; i++)
2091 osb->osb_dx_seed[i] = le32_to_cpu(di->id2.i_super.s_dx_seed[i]);
2092 osb->osb_dx_seed[3] = le32_to_cpu(di->id2.i_super.s_uuid_hash);
2093
Mark Fashehccd979b2005-12-15 14:31:24 -08002094 osb->sb = sb;
2095 /* Save off for ocfs2_rw_direct */
2096 osb->s_sectsize_bits = blksize_bits(sector_size);
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01002097 BUG_ON(!osb->s_sectsize_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08002098
Mark Fasheh34d024f2007-09-24 15:56:19 -07002099 spin_lock_init(&osb->dc_task_lock);
2100 init_waitqueue_head(&osb->dc_event);
2101 osb->dc_work_sequence = 0;
2102 osb->dc_wake_sequence = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002103 INIT_LIST_HEAD(&osb->blocked_lock_list);
2104 osb->blocked_lock_count = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002105 spin_lock_init(&osb->osb_lock);
Tao Mac8b9cf92009-02-24 17:40:26 -08002106 spin_lock_init(&osb->osb_xattr_lock);
Tiger Yangb89c5422010-01-25 14:11:06 +08002107 ocfs2_init_steal_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002108
2109 atomic_set(&osb->alloc_stats.moves, 0);
2110 atomic_set(&osb->alloc_stats.local_data, 0);
2111 atomic_set(&osb->alloc_stats.bitmap_data, 0);
2112 atomic_set(&osb->alloc_stats.bg_allocs, 0);
2113 atomic_set(&osb->alloc_stats.bg_extends, 0);
2114
Joel Becker73be1922009-01-06 14:57:08 -08002115 /* Copy the blockcheck stats from the superblock probe */
2116 osb->osb_ecc_stats = *stats;
2117
Mark Fashehccd979b2005-12-15 14:31:24 -08002118 ocfs2_init_node_maps(osb);
2119
2120 snprintf(osb->dev_str, sizeof(osb->dev_str), "%u,%u",
2121 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
2122
Goldwyn Rodrigues75d9bbc2010-10-11 12:57:09 -05002123 osb->max_slots = le16_to_cpu(di->id2.i_super.s_max_slots);
2124 if (osb->max_slots > OCFS2_MAX_SLOTS || osb->max_slots == 0) {
2125 mlog(ML_ERROR, "Invalid number of node slots (%u)\n",
2126 osb->max_slots);
2127 status = -EINVAL;
2128 goto bail;
2129 }
2130 mlog(0, "max_slots for this device: %u\n", osb->max_slots);
2131
Jeff Mahoney8b712cd2009-07-07 17:22:12 -04002132 ocfs2_orphan_scan_init(osb);
2133
Joel Becker553abd02008-02-01 12:03:57 -08002134 status = ocfs2_recovery_init(osb);
2135 if (status) {
2136 mlog(ML_ERROR, "Unable to initialize recovery state\n");
2137 mlog_errno(status);
2138 goto bail;
2139 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002140
2141 init_waitqueue_head(&osb->checkpoint_event);
2142 atomic_set(&osb->needs_checkpoint, 0);
2143
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002144 osb->s_atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
2145
Mark Fashehccd979b2005-12-15 14:31:24 -08002146 osb->slot_num = OCFS2_INVALID_SLOT;
2147
Tiger Yang8154da32008-08-18 17:11:46 +08002148 osb->s_xattr_inline_size = le16_to_cpu(
2149 di->id2.i_super.s_xattr_inline_size);
Tiger Yangfdd77702008-08-18 17:08:55 +08002150
Mark Fashehccd979b2005-12-15 14:31:24 -08002151 osb->local_alloc_state = OCFS2_LA_UNUSED;
2152 osb->local_alloc_bh = NULL;
Mark Fasheh9c7af402008-07-28 18:02:53 -07002153 INIT_DELAYED_WORK(&osb->la_enable_wq, ocfs2_la_enable_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08002154
Mark Fashehccd979b2005-12-15 14:31:24 -08002155 init_waitqueue_head(&osb->osb_mount_event);
2156
Mark Fashehd02f00c2009-12-07 13:10:48 -08002157 status = ocfs2_resmap_init(osb, &osb->osb_la_resmap);
2158 if (status) {
2159 mlog_errno(status);
2160 goto bail;
2161 }
2162
Mark Fashehccd979b2005-12-15 14:31:24 -08002163 osb->vol_label = kmalloc(OCFS2_MAX_VOL_LABEL_LEN, GFP_KERNEL);
2164 if (!osb->vol_label) {
2165 mlog(ML_ERROR, "unable to alloc vol label\n");
2166 status = -ENOMEM;
2167 goto bail;
2168 }
2169
Sunil Mushran539d8262008-07-14 17:31:10 -07002170 osb->slot_recovery_generations =
2171 kcalloc(osb->max_slots, sizeof(*osb->slot_recovery_generations),
2172 GFP_KERNEL);
2173 if (!osb->slot_recovery_generations) {
2174 status = -ENOMEM;
2175 mlog_errno(status);
2176 goto bail;
2177 }
2178
Mark Fashehb4df6ed2006-02-22 17:35:08 -08002179 init_waitqueue_head(&osb->osb_wipe_event);
2180 osb->osb_orphan_wipes = kcalloc(osb->max_slots,
2181 sizeof(*osb->osb_orphan_wipes),
2182 GFP_KERNEL);
2183 if (!osb->osb_orphan_wipes) {
2184 status = -ENOMEM;
2185 mlog_errno(status);
2186 goto bail;
2187 }
2188
Tao Ma374a2632009-08-24 11:13:37 +08002189 osb->osb_rf_lock_tree = RB_ROOT;
2190
Mark Fashehccd979b2005-12-15 14:31:24 -08002191 osb->s_feature_compat =
2192 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_compat);
2193 osb->s_feature_ro_compat =
2194 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_ro_compat);
2195 osb->s_feature_incompat =
2196 le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_incompat);
2197
2198 if ((i = OCFS2_HAS_INCOMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_INCOMPAT_SUPP))) {
2199 mlog(ML_ERROR, "couldn't mount because of unsupported "
2200 "optional features (%x).\n", i);
2201 status = -EINVAL;
2202 goto bail;
2203 }
2204 if (!(osb->sb->s_flags & MS_RDONLY) &&
2205 (i = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP))) {
2206 mlog(ML_ERROR, "couldn't mount RDWR because of "
2207 "unsupported optional features (%x).\n", i);
2208 status = -EINVAL;
2209 goto bail;
2210 }
2211
Joel Beckerb61817e2008-02-01 15:08:23 -08002212 if (ocfs2_userspace_stack(osb)) {
2213 memcpy(osb->osb_cluster_stack,
2214 OCFS2_RAW_SB(di)->s_cluster_info.ci_stack,
2215 OCFS2_STACK_LABEL_LEN);
2216 osb->osb_cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
2217 if (strlen(osb->osb_cluster_stack) != OCFS2_STACK_LABEL_LEN) {
2218 mlog(ML_ERROR,
2219 "couldn't mount because of an invalid "
2220 "cluster stack label (%s) \n",
2221 osb->osb_cluster_stack);
2222 status = -EINVAL;
2223 goto bail;
2224 }
2225 } else {
2226 /* The empty string is identical with classic tools that
2227 * don't know about s_cluster_info. */
2228 osb->osb_cluster_stack[0] = '\0';
2229 }
2230
Mark Fashehccd979b2005-12-15 14:31:24 -08002231 get_random_bytes(&osb->s_next_generation, sizeof(u32));
2232
2233 /* FIXME
2234 * This should be done in ocfs2_journal_init(), but unknown
2235 * ordering issues will cause the filesystem to crash.
2236 * If anyone wants to figure out what part of the code
2237 * refers to osb->journal before ocfs2_journal_init() is run,
2238 * be my guest.
2239 */
2240 /* initialize our journal structure */
2241
Robert P. J. Daycd861282006-12-13 00:34:52 -08002242 journal = kzalloc(sizeof(struct ocfs2_journal), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08002243 if (!journal) {
2244 mlog(ML_ERROR, "unable to alloc journal\n");
2245 status = -ENOMEM;
2246 goto bail;
2247 }
2248 osb->journal = journal;
2249 journal->j_osb = osb;
2250
2251 atomic_set(&journal->j_num_trans, 0);
2252 init_rwsem(&journal->j_trans_barrier);
2253 init_waitqueue_head(&journal->j_checkpointed);
2254 spin_lock_init(&journal->j_lock);
2255 journal->j_trans_id = (unsigned long) 1;
2256 INIT_LIST_HEAD(&journal->j_la_cleanups);
David Howellsc4028952006-11-22 14:57:56 +00002257 INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
Mark Fashehccd979b2005-12-15 14:31:24 -08002258 journal->j_state = OCFS2_JOURNAL_FREE;
2259
Jan Karaea455f82009-01-12 23:20:31 +01002260 INIT_WORK(&osb->dentry_lock_work, ocfs2_drop_dl_inodes);
2261 osb->dentry_lock_list = NULL;
2262
Mark Fashehccd979b2005-12-15 14:31:24 -08002263 /* get some pseudo constants for clustersize bits */
2264 osb->s_clustersize_bits =
2265 le32_to_cpu(di->id2.i_super.s_clustersize_bits);
2266 osb->s_clustersize = 1 << osb->s_clustersize_bits;
2267 mlog(0, "clusterbits=%d\n", osb->s_clustersize_bits);
2268
2269 if (osb->s_clustersize < OCFS2_MIN_CLUSTERSIZE ||
2270 osb->s_clustersize > OCFS2_MAX_CLUSTERSIZE) {
2271 mlog(ML_ERROR, "Volume has invalid cluster size (%d)\n",
2272 osb->s_clustersize);
2273 status = -EINVAL;
2274 goto bail;
2275 }
2276
Patrick J. LoPresti3bdb8ef2010-07-22 15:05:57 -07002277 total_blocks = ocfs2_clusters_to_blocks(osb->sb,
2278 le32_to_cpu(di->i_clusters));
2279
2280 status = generic_check_addressable(osb->sb->s_blocksize_bits,
2281 total_blocks);
2282 if (status) {
2283 mlog(ML_ERROR, "Volume too large "
2284 "to mount safely on this system");
2285 status = -EFBIG;
Mark Fashehccd979b2005-12-15 14:31:24 -08002286 goto bail;
2287 }
2288
2289 if (ocfs2_setup_osb_uuid(osb, di->id2.i_super.s_uuid,
2290 sizeof(di->id2.i_super.s_uuid))) {
2291 mlog(ML_ERROR, "Out of memory trying to setup our uuid.\n");
2292 status = -ENOMEM;
2293 goto bail;
2294 }
2295
Mark Fasheh78427042006-05-04 12:03:26 -07002296 memcpy(&uuid_net_key, di->id2.i_super.s_uuid, sizeof(uuid_net_key));
Mark Fashehccd979b2005-12-15 14:31:24 -08002297
2298 strncpy(osb->vol_label, di->id2.i_super.s_label, 63);
2299 osb->vol_label[63] = '\0';
2300 osb->root_blkno = le64_to_cpu(di->id2.i_super.s_root_blkno);
2301 osb->system_dir_blkno = le64_to_cpu(di->id2.i_super.s_system_dir_blkno);
2302 osb->first_cluster_group_blkno =
2303 le64_to_cpu(di->id2.i_super.s_first_cluster_group);
2304 osb->fs_generation = le32_to_cpu(di->i_fs_generation);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002305 osb->uuid_hash = le32_to_cpu(di->id2.i_super.s_uuid_hash);
Mark Fashehccd979b2005-12-15 14:31:24 -08002306 mlog(0, "vol_label: %s\n", osb->vol_label);
2307 mlog(0, "uuid: %s\n", osb->uuid_str);
Mark Fashehb0697052006-03-03 10:24:33 -08002308 mlog(0, "root_blkno=%llu, system_dir_blkno=%llu\n",
2309 (unsigned long long)osb->root_blkno,
2310 (unsigned long long)osb->system_dir_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002311
2312 osb->osb_dlm_debug = ocfs2_new_dlm_debug();
2313 if (!osb->osb_dlm_debug) {
2314 status = -ENOMEM;
2315 mlog_errno(status);
2316 goto bail;
2317 }
2318
2319 atomic_set(&osb->vol_state, VOLUME_INIT);
2320
2321 /* load root, system_dir, and all global system inodes */
2322 status = ocfs2_init_global_system_inodes(osb);
2323 if (status < 0) {
2324 mlog_errno(status);
2325 goto bail;
2326 }
2327
2328 /*
2329 * global bitmap
2330 */
2331 inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
2332 OCFS2_INVALID_SLOT);
2333 if (!inode) {
2334 status = -EINVAL;
2335 mlog_errno(status);
2336 goto bail;
2337 }
2338
2339 osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
Mark Fasheh6b820212010-04-05 18:17:14 -07002340 osb->osb_clusters_at_boot = OCFS2_I(inode)->ip_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08002341 iput(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08002342
Tao Ma85718822010-04-13 14:38:06 +08002343 osb->bitmap_cpg = ocfs2_group_bitmap_size(sb, 0,
2344 osb->s_feature_incompat) * 8;
Mark Fashehccd979b2005-12-15 14:31:24 -08002345
2346 status = ocfs2_init_slot_info(osb);
2347 if (status < 0) {
2348 mlog_errno(status);
2349 goto bail;
2350 }
2351
Mark Fashehccd979b2005-12-15 14:31:24 -08002352bail:
2353 mlog_exit(status);
2354 return status;
2355}
2356
2357/*
2358 * will return: -EAGAIN if it is ok to keep searching for superblocks
2359 * -EINVAL if there is a bad superblock
2360 * 0 on success
2361 */
2362static int ocfs2_verify_volume(struct ocfs2_dinode *di,
2363 struct buffer_head *bh,
Joel Becker73be1922009-01-06 14:57:08 -08002364 u32 blksz,
2365 struct ocfs2_blockcheck_stats *stats)
Mark Fashehccd979b2005-12-15 14:31:24 -08002366{
2367 int status = -EAGAIN;
2368
2369 mlog_entry_void();
2370
2371 if (memcmp(di->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
2372 strlen(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) {
Joel Beckerd030cc92008-12-11 15:04:14 -08002373 /* We have to do a raw check of the feature here */
2374 if (le32_to_cpu(di->id2.i_super.s_feature_incompat) &
2375 OCFS2_FEATURE_INCOMPAT_META_ECC) {
2376 status = ocfs2_block_check_validate(bh->b_data,
2377 bh->b_size,
Joel Becker73be1922009-01-06 14:57:08 -08002378 &di->i_check,
2379 stats);
Joel Beckerd030cc92008-12-11 15:04:14 -08002380 if (status)
2381 goto out;
2382 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002383 status = -EINVAL;
2384 if ((1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits)) != blksz) {
2385 mlog(ML_ERROR, "found superblock with incorrect block "
2386 "size: found %u, should be %u\n",
2387 1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits),
2388 blksz);
2389 } else if (le16_to_cpu(di->id2.i_super.s_major_rev_level) !=
2390 OCFS2_MAJOR_REV_LEVEL ||
2391 le16_to_cpu(di->id2.i_super.s_minor_rev_level) !=
2392 OCFS2_MINOR_REV_LEVEL) {
2393 mlog(ML_ERROR, "found superblock with bad version: "
2394 "found %u.%u, should be %u.%u\n",
2395 le16_to_cpu(di->id2.i_super.s_major_rev_level),
2396 le16_to_cpu(di->id2.i_super.s_minor_rev_level),
2397 OCFS2_MAJOR_REV_LEVEL,
2398 OCFS2_MINOR_REV_LEVEL);
2399 } else if (bh->b_blocknr != le64_to_cpu(di->i_blkno)) {
2400 mlog(ML_ERROR, "bad block number on superblock: "
Mark Fashehb0697052006-03-03 10:24:33 -08002401 "found %llu, should be %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07002402 (unsigned long long)le64_to_cpu(di->i_blkno),
Mark Fashehb0697052006-03-03 10:24:33 -08002403 (unsigned long long)bh->b_blocknr);
Mark Fashehccd979b2005-12-15 14:31:24 -08002404 } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
2405 le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
2406 mlog(ML_ERROR, "bad cluster size found: %u\n",
2407 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
2408 } else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
2409 mlog(ML_ERROR, "bad root_blkno: 0\n");
2410 } else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
2411 mlog(ML_ERROR, "bad system_dir_blkno: 0\n");
2412 } else if (le16_to_cpu(di->id2.i_super.s_max_slots) > OCFS2_MAX_SLOTS) {
2413 mlog(ML_ERROR,
2414 "Superblock slots found greater than file system "
2415 "maximum: found %u, max %u\n",
2416 le16_to_cpu(di->id2.i_super.s_max_slots),
2417 OCFS2_MAX_SLOTS);
2418 } else {
2419 /* found it! */
2420 status = 0;
2421 }
2422 }
2423
Joel Beckerd030cc92008-12-11 15:04:14 -08002424out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002425 mlog_exit(status);
2426 return status;
2427}
2428
2429static int ocfs2_check_volume(struct ocfs2_super *osb)
2430{
Denis Chengbddb8eb32007-09-27 02:10:04 +08002431 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08002432 int dirty;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002433 int local;
Mark Fashehccd979b2005-12-15 14:31:24 -08002434 struct ocfs2_dinode *local_alloc = NULL; /* only used if we
2435 * recover
2436 * ourselves. */
2437
2438 mlog_entry_void();
2439
2440 /* Init our journal object. */
2441 status = ocfs2_journal_init(osb->journal, &dirty);
2442 if (status < 0) {
2443 mlog(ML_ERROR, "Could not initialize journal!\n");
2444 goto finally;
2445 }
2446
Patrick J. LoPresti3bdb8ef2010-07-22 15:05:57 -07002447 /* Now that journal has been initialized, check to make sure
2448 entire volume is addressable. */
2449 status = ocfs2_journal_addressable(osb);
2450 if (status)
2451 goto finally;
2452
Mark Fashehccd979b2005-12-15 14:31:24 -08002453 /* If the journal was unmounted cleanly then we don't want to
2454 * recover anything. Otherwise, journal_load will do that
2455 * dirty work for us :) */
2456 if (!dirty) {
2457 status = ocfs2_journal_wipe(osb->journal, 0);
2458 if (status < 0) {
2459 mlog_errno(status);
2460 goto finally;
2461 }
2462 } else {
2463 mlog(ML_NOTICE, "File system was not unmounted cleanly, "
2464 "recovering volume.\n");
2465 }
2466
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002467 local = ocfs2_mount_local(osb);
2468
Mark Fashehccd979b2005-12-15 14:31:24 -08002469 /* will play back anything left in the journal. */
Sunil Mushran539d8262008-07-14 17:31:10 -07002470 status = ocfs2_journal_load(osb->journal, local, dirty);
Wengang Wang01af4822008-06-10 14:24:48 +08002471 if (status < 0) {
2472 mlog(ML_ERROR, "ocfs2 journal load failed! %d\n", status);
2473 goto finally;
2474 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002475
2476 if (dirty) {
2477 /* recover my local alloc if we didn't unmount cleanly. */
2478 status = ocfs2_begin_local_alloc_recovery(osb,
2479 osb->slot_num,
2480 &local_alloc);
2481 if (status < 0) {
2482 mlog_errno(status);
2483 goto finally;
2484 }
2485 /* we complete the recovery process after we've marked
2486 * ourselves as mounted. */
2487 }
2488
2489 mlog(0, "Journal loaded.\n");
2490
2491 status = ocfs2_load_local_alloc(osb);
2492 if (status < 0) {
2493 mlog_errno(status);
2494 goto finally;
2495 }
2496
2497 if (dirty) {
2498 /* Recovery will be completed after we've mounted the
2499 * rest of the volume. */
2500 osb->dirty = 1;
2501 osb->local_alloc_copy = local_alloc;
2502 local_alloc = NULL;
2503 }
2504
2505 /* go through each journal, trylock it and if you get the
2506 * lock, and it's marked as dirty, set the bit in the recover
2507 * map and launch a recovery thread for it. */
2508 status = ocfs2_mark_dead_nodes(osb);
Srinivas Eeda9140db02009-03-06 14:21:46 -08002509 if (status < 0) {
2510 mlog_errno(status);
2511 goto finally;
2512 }
2513
2514 status = ocfs2_compute_replay_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002515 if (status < 0)
2516 mlog_errno(status);
2517
2518finally:
2519 if (local_alloc)
2520 kfree(local_alloc);
2521
2522 mlog_exit(status);
2523 return status;
2524}
2525
2526/*
2527 * The routine gets called from dismount or close whenever a dismount on
2528 * volume is requested and the osb open count becomes 1.
2529 * It will remove the osb from the global list and also free up all the
2530 * initialized resources and fileobject.
2531 */
2532static void ocfs2_delete_osb(struct ocfs2_super *osb)
2533{
2534 mlog_entry_void();
2535
2536 /* This function assumes that the caller has the main osb resource */
2537
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002538 ocfs2_free_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002539
Mark Fashehb4df6ed2006-02-22 17:35:08 -08002540 kfree(osb->osb_orphan_wipes);
Sunil Mushran539d8262008-07-14 17:31:10 -07002541 kfree(osb->slot_recovery_generations);
Mark Fashehccd979b2005-12-15 14:31:24 -08002542 /* FIXME
2543 * This belongs in journal shutdown, but because we have to
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02002544 * allocate osb->journal at the start of ocfs2_initialize_osb(),
Mark Fashehccd979b2005-12-15 14:31:24 -08002545 * we free it here.
2546 */
2547 kfree(osb->journal);
2548 if (osb->local_alloc_copy)
2549 kfree(osb->local_alloc_copy);
2550 kfree(osb->uuid_str);
2551 ocfs2_put_dlm_debug(osb->osb_dlm_debug);
2552 memset(osb, 0, sizeof(struct ocfs2_super));
2553
2554 mlog_exit_void();
2555}
2556
2557/* Put OCFS2 into a readonly state, or (if the user specifies it),
2558 * panic(). We do not support continue-on-error operation. */
2559static void ocfs2_handle_error(struct super_block *sb)
2560{
2561 struct ocfs2_super *osb = OCFS2_SB(sb);
2562
2563 if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_PANIC)
2564 panic("OCFS2: (device %s): panic forced after error\n",
2565 sb->s_id);
2566
2567 ocfs2_set_osb_flag(osb, OCFS2_OSB_ERROR_FS);
2568
2569 if (sb->s_flags & MS_RDONLY &&
2570 (ocfs2_is_soft_readonly(osb) ||
2571 ocfs2_is_hard_readonly(osb)))
2572 return;
2573
2574 printk(KERN_CRIT "File system is now read-only due to the potential "
2575 "of on-disk corruption. Please run fsck.ocfs2 once the file "
2576 "system is unmounted.\n");
2577 sb->s_flags |= MS_RDONLY;
2578 ocfs2_set_ro_flag(osb, 0);
2579}
2580
2581static char error_buf[1024];
2582
2583void __ocfs2_error(struct super_block *sb,
2584 const char *function,
2585 const char *fmt, ...)
2586{
2587 va_list args;
2588
2589 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002590 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Mark Fashehccd979b2005-12-15 14:31:24 -08002591 va_end(args);
2592
2593 /* Not using mlog here because we want to show the actual
2594 * function the error came from. */
2595 printk(KERN_CRIT "OCFS2: ERROR (device %s): %s: %s\n",
2596 sb->s_id, function, error_buf);
2597
2598 ocfs2_handle_error(sb);
2599}
2600
2601/* Handle critical errors. This is intentionally more drastic than
2602 * ocfs2_handle_error, so we only use for things like journal errors,
2603 * etc. */
2604void __ocfs2_abort(struct super_block* sb,
2605 const char *function,
2606 const char *fmt, ...)
2607{
2608 va_list args;
2609
2610 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002611 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Mark Fashehccd979b2005-12-15 14:31:24 -08002612 va_end(args);
2613
2614 printk(KERN_CRIT "OCFS2: abort (device %s): %s: %s\n",
2615 sb->s_id, function, error_buf);
2616
2617 /* We don't have the cluster support yet to go straight to
2618 * hard readonly in here. Until then, we want to keep
2619 * ocfs2_abort() so that we can at least mark critical
2620 * errors.
2621 *
2622 * TODO: This should abort the journal and alert other nodes
2623 * that our slot needs recovery. */
2624
2625 /* Force a panic(). This stinks, but it's better than letting
2626 * things continue without having a proper hard readonly
2627 * here. */
Sunil Mushrana2f2ddb2009-08-19 15:16:01 -07002628 if (!ocfs2_mount_local(OCFS2_SB(sb)))
2629 OCFS2_SB(sb)->s_mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
Mark Fashehccd979b2005-12-15 14:31:24 -08002630 ocfs2_handle_error(sb);
2631}
2632
Joel Beckere4b963f2009-09-02 17:17:36 -07002633/*
2634 * Void signal blockers, because in-kernel sigprocmask() only fails
2635 * when SIG_* is wrong.
2636 */
2637void ocfs2_block_signals(sigset_t *oldset)
2638{
2639 int rc;
2640 sigset_t blocked;
2641
2642 sigfillset(&blocked);
2643 rc = sigprocmask(SIG_BLOCK, &blocked, oldset);
2644 BUG_ON(rc);
2645}
2646
2647void ocfs2_unblock_signals(sigset_t *oldset)
2648{
2649 int rc = sigprocmask(SIG_SETMASK, oldset, NULL);
2650 BUG_ON(rc);
2651}
2652
Mark Fashehccd979b2005-12-15 14:31:24 -08002653module_init(ocfs2_init);
2654module_exit(ocfs2_exit);