1/*
2 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
8 */
9
10#include <linux/spinlock.h>
11#include <linux/completion.h>
12#include <linux/buffer_head.h>
13#include <linux/crc32.h>
14#include <linux/gfs2_ondisk.h>
15#include <asm/uaccess.h>
16
17#include "gfs2.h"
18#include "incore.h"
19#include "glock.h"
20#include "util.h"
21
22struct kmem_cache *gfs2_glock_cachep __read_mostly;
23struct kmem_cache *gfs2_glock_aspace_cachep __read_mostly;
24struct kmem_cache *gfs2_inode_cachep __read_mostly;
25struct kmem_cache *gfs2_bufdata_cachep __read_mostly;
26struct kmem_cache *gfs2_rgrpd_cachep __read_mostly;
27struct kmem_cache *gfs2_quotad_cachep __read_mostly;
28
29void gfs2_assert_i(struct gfs2_sbd *sdp)
30{
31	printk(KERN_EMERG "GFS2: fsid=%s: fatal assertion failed\n",
32	       sdp->sd_fsname);
33}
34
35int gfs2_lm_withdraw(struct gfs2_sbd *sdp, char *fmt, ...)
36{
37	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
38	const struct lm_lockops *lm = ls->ls_ops;
39	va_list args;
40
41	if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW &&
42	    test_and_set_bit(SDF_SHUTDOWN, &sdp->sd_flags))
43		return 0;
44
45	va_start(args, fmt);
46	vprintk(fmt, args);
47	va_end(args);
48
49	if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW) {
50		fs_err(sdp, "about to withdraw this file system\n");
51		BUG_ON(sdp->sd_args.ar_debug);
52
53		kobject_uevent(&sdp->sd_kobj, KOBJ_OFFLINE);
54
55		if (lm->lm_unmount) {
56			fs_err(sdp, "telling LM to unmount\n");
57			lm->lm_unmount(sdp);
58		}
59		fs_err(sdp, "withdrawn\n");
60		dump_stack();
61	}
62
63	if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC)
64		panic("GFS2: fsid=%s: panic requested.\n", sdp->sd_fsname);
65
66	return -1;
67}
68
69/**
70 * gfs2_assert_withdraw_i - Cause the machine to withdraw if @assertion is false
71 * Returns: -1 if this call withdrew the machine,
72 *          -2 if it was already withdrawn
73 */
74
75int gfs2_assert_withdraw_i(struct gfs2_sbd *sdp, char *assertion,
76			   const char *function, char *file, unsigned int line)
77{
78	int me;
79	me = gfs2_lm_withdraw(sdp,
80		"GFS2: fsid=%s: fatal: assertion \"%s\" failed\n"
81		"GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
82		sdp->sd_fsname, assertion,
83		sdp->sd_fsname, function, file, line);
84	dump_stack();
85	return (me) ? -1 : -2;
86}
87
88/**
89 * gfs2_assert_warn_i - Print a message to the console if @assertion is false
90 * Returns: -1 if we printed something
91 *          -2 if we didn't
92 */
93
94int gfs2_assert_warn_i(struct gfs2_sbd *sdp, char *assertion,
95		       const char *function, char *file, unsigned int line)
96{
97	if (time_before(jiffies,
98			sdp->sd_last_warning +
99			gfs2_tune_get(sdp, gt_complain_secs) * HZ))
100		return -2;
101
102	if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW)
103		printk(KERN_WARNING
104		       "GFS2: fsid=%s: warning: assertion \"%s\" failed\n"
105		       "GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
106		       sdp->sd_fsname, assertion,
107		       sdp->sd_fsname, function, file, line);
108
109	if (sdp->sd_args.ar_debug)
110		BUG();
111	else
112		dump_stack();
113
114	if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC)
115		panic("GFS2: fsid=%s: warning: assertion \"%s\" failed\n"
116		      "GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
117		      sdp->sd_fsname, assertion,
118		      sdp->sd_fsname, function, file, line);
119
120	sdp->sd_last_warning = jiffies;
121
122	return -1;
123}
124
125/**
126 * gfs2_consist_i - Flag a filesystem consistency error and withdraw
127 * Returns: -1 if this call withdrew the machine,
128 *          0 if it was already withdrawn
129 */
130
131int gfs2_consist_i(struct gfs2_sbd *sdp, int cluster_wide, const char *function,
132		   char *file, unsigned int line)
133{
134	int rv;
135	rv = gfs2_lm_withdraw(sdp,
136		"GFS2: fsid=%s: fatal: filesystem consistency error\n"
137		"GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
138		sdp->sd_fsname,
139		sdp->sd_fsname, function, file, line);
140	return rv;
141}
142
143/**
144 * gfs2_consist_inode_i - Flag an inode consistency error and withdraw
145 * Returns: -1 if this call withdrew the machine,
146 *          0 if it was already withdrawn
147 */
148
149int gfs2_consist_inode_i(struct gfs2_inode *ip, int cluster_wide,
150			 const char *function, char *file, unsigned int line)
151{
152	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
153	int rv;
154	rv = gfs2_lm_withdraw(sdp,
155		"GFS2: fsid=%s: fatal: filesystem consistency error\n"
156		"GFS2: fsid=%s:   inode = %llu %llu\n"
157		"GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
158		sdp->sd_fsname,
159		sdp->sd_fsname, (unsigned long long)ip->i_no_formal_ino,
160		(unsigned long long)ip->i_no_addr,
161		sdp->sd_fsname, function, file, line);
162	return rv;
163}
164
165/**
166 * gfs2_consist_rgrpd_i - Flag a RG consistency error and withdraw
167 * Returns: -1 if this call withdrew the machine,
168 *          0 if it was already withdrawn
169 */
170
171int gfs2_consist_rgrpd_i(struct gfs2_rgrpd *rgd, int cluster_wide,
172			 const char *function, char *file, unsigned int line)
173{
174	struct gfs2_sbd *sdp = rgd->rd_sbd;
175	int rv;
176	rv = gfs2_lm_withdraw(sdp,
177		"GFS2: fsid=%s: fatal: filesystem consistency error\n"
178		"GFS2: fsid=%s:   RG = %llu\n"
179		"GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
180		sdp->sd_fsname,
181		sdp->sd_fsname, (unsigned long long)rgd->rd_addr,
182		sdp->sd_fsname, function, file, line);
183	return rv;
184}
185
186/**
187 * gfs2_meta_check_ii - Flag a magic number consistency error and withdraw
188 * Returns: -1 if this call withdrew the machine,
189 *          -2 if it was already withdrawn
190 */
191
192int gfs2_meta_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh,
193		       const char *type, const char *function, char *file,
194		       unsigned int line)
195{
196	int me;
197	me = gfs2_lm_withdraw(sdp,
198		"GFS2: fsid=%s: fatal: invalid metadata block\n"
199		"GFS2: fsid=%s:   bh = %llu (%s)\n"
200		"GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
201		sdp->sd_fsname,
202		sdp->sd_fsname, (unsigned long long)bh->b_blocknr, type,
203		sdp->sd_fsname, function, file, line);
204	return (me) ? -1 : -2;
205}
206
207/**
208 * gfs2_metatype_check_ii - Flag a metadata type consistency error and withdraw
209 * Returns: -1 if this call withdrew the machine,
210 *          -2 if it was already withdrawn
211 */
212
213int gfs2_metatype_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh,
214			   u16 type, u16 t, const char *function,
215			   char *file, unsigned int line)
216{
217	int me;
218	me = gfs2_lm_withdraw(sdp,
219		"GFS2: fsid=%s: fatal: invalid metadata block\n"
220		"GFS2: fsid=%s:   bh = %llu (type: exp=%u, found=%u)\n"
221		"GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
222		sdp->sd_fsname,
223		sdp->sd_fsname, (unsigned long long)bh->b_blocknr, type, t,
224		sdp->sd_fsname, function, file, line);
225	return (me) ? -1 : -2;
226}
227
228/**
229 * gfs2_io_error_i - Flag an I/O error and withdraw
230 * Returns: -1 if this call withdrew the machine,
231 *          0 if it was already withdrawn
232 */
233
234int gfs2_io_error_i(struct gfs2_sbd *sdp, const char *function, char *file,
235		    unsigned int line)
236{
237	int rv;
238	rv = gfs2_lm_withdraw(sdp,
239		"GFS2: fsid=%s: fatal: I/O error\n"
240		"GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
241		sdp->sd_fsname,
242		sdp->sd_fsname, function, file, line);
243	return rv;
244}
245
246/**
247 * gfs2_io_error_bh_i - Flag a buffer I/O error and withdraw
248 * Returns: -1 if this call withdrew the machine,
249 *          0 if it was already withdrawn
250 */
251
252int gfs2_io_error_bh_i(struct gfs2_sbd *sdp, struct buffer_head *bh,
253		       const char *function, char *file, unsigned int line)
254{
255	int rv;
256	rv = gfs2_lm_withdraw(sdp,
257		"GFS2: fsid=%s: fatal: I/O error\n"
258		"GFS2: fsid=%s:   block = %llu\n"
259		"GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
260		sdp->sd_fsname,
261		sdp->sd_fsname, (unsigned long long)bh->b_blocknr,
262		sdp->sd_fsname, function, file, line);
263	return rv;
264}
265
266void gfs2_icbit_munge(struct gfs2_sbd *sdp, unsigned char **bitmap,
267		      unsigned int bit, int new_value)
268{
269	unsigned int c, o, b = bit;
270	int old_value;
271
272	c = b / (8 * PAGE_SIZE);
273	b %= 8 * PAGE_SIZE;
274	o = b / 8;
275	b %= 8;
276
277	old_value = (bitmap[c][o] & (1 << b));
278	gfs2_assert_withdraw(sdp, !old_value != !new_value);
279
280	if (new_value)
281		bitmap[c][o] |= 1 << b;
282	else
283		bitmap[c][o] &= ~(1 << b);
284}
285