Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/livep...
[linux-2.6-block.git] / fs / gfs2 / util.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3a8a9a10 3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
b3b94faa
DT
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
e9fc2aa0 7 * of the GNU General Public License version 2.
b3b94faa
DT
8 */
9
d77d1b58
JP
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
b3b94faa
DT
12#include <linux/spinlock.h>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
15#include <linux/crc32.h>
5c676f6d 16#include <linux/gfs2_ondisk.h>
7c0f6ba6 17#include <linux/uaccess.h>
b3b94faa
DT
18
19#include "gfs2.h"
5c676f6d 20#include "incore.h"
b3b94faa 21#include "glock.h"
72244b6b 22#include "rgrp.h"
5c676f6d 23#include "util.h"
b3b94faa 24
e18b890b 25struct kmem_cache *gfs2_glock_cachep __read_mostly;
009d8518 26struct kmem_cache *gfs2_glock_aspace_cachep __read_mostly;
e18b890b
CL
27struct kmem_cache *gfs2_inode_cachep __read_mostly;
28struct kmem_cache *gfs2_bufdata_cachep __read_mostly;
6bdd9be6 29struct kmem_cache *gfs2_rgrpd_cachep __read_mostly;
37b2c837 30struct kmem_cache *gfs2_quotad_cachep __read_mostly;
b54e9a0b 31struct kmem_cache *gfs2_qadata_cachep __read_mostly;
e8c92ed7 32mempool_t *gfs2_page_pool __read_mostly;
b3b94faa 33
b3b94faa
DT
34void gfs2_assert_i(struct gfs2_sbd *sdp)
35{
8382e26b 36 fs_emerg(sdp, "fatal assertion failed\n");
b3b94faa
DT
37}
38
cb94eb06 39int gfs2_lm_withdraw(struct gfs2_sbd *sdp, const char *fmt, ...)
da755fdb 40{
f057f6cd
SW
41 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
42 const struct lm_lockops *lm = ls->ls_ops;
da755fdb 43 va_list args;
cb94eb06 44 struct va_format vaf;
da755fdb 45
d34843d0
BP
46 if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW &&
47 test_and_set_bit(SDF_SHUTDOWN, &sdp->sd_flags))
da755fdb
SW
48 return 0;
49
9e1a9ecd
AG
50 if (fmt) {
51 va_start(args, fmt);
cb94eb06 52
9e1a9ecd
AG
53 vaf.fmt = fmt;
54 vaf.va = &args;
cb94eb06 55
9e1a9ecd 56 fs_err(sdp, "%pV", &vaf);
cb94eb06 57
9e1a9ecd
AG
58 va_end(args);
59 }
da755fdb 60
d34843d0
BP
61 if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW) {
62 fs_err(sdp, "about to withdraw this file system\n");
63 BUG_ON(sdp->sd_args.ar_debug);
da755fdb 64
d34843d0 65 kobject_uevent(&sdp->sd_kobj, KOBJ_OFFLINE);
f057f6cd 66
fd95e81c
SW
67 if (!strcmp(sdp->sd_lockstruct.ls_ops->lm_proto_name, "lock_dlm"))
68 wait_for_completion(&sdp->sd_wdack);
69
d34843d0
BP
70 if (lm->lm_unmount) {
71 fs_err(sdp, "telling LM to unmount\n");
72 lm->lm_unmount(sdp);
73 }
3e11e530 74 set_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags);
d34843d0
BP
75 fs_err(sdp, "withdrawn\n");
76 dump_stack();
f057f6cd 77 }
d34843d0
BP
78
79 if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC)
d77d1b58 80 panic("GFS2: fsid=%s: panic requested\n", sdp->sd_fsname);
da755fdb
SW
81
82 return -1;
83}
84
b3b94faa
DT
85/**
86 * gfs2_assert_withdraw_i - Cause the machine to withdraw if @assertion is false
87 * Returns: -1 if this call withdrew the machine,
88 * -2 if it was already withdrawn
89 */
90
91int gfs2_assert_withdraw_i(struct gfs2_sbd *sdp, char *assertion,
92 const char *function, char *file, unsigned int line)
93{
94 int me;
95 me = gfs2_lm_withdraw(sdp,
cb94eb06
JP
96 "fatal: assertion \"%s\" failed\n"
97 " function = %s, file = %s, line = %u\n",
98 assertion, function, file, line);
18ec7d5c 99 dump_stack();
b3b94faa
DT
100 return (me) ? -1 : -2;
101}
102
103/**
104 * gfs2_assert_warn_i - Print a message to the console if @assertion is false
105 * Returns: -1 if we printed something
106 * -2 if we didn't
107 */
108
109int gfs2_assert_warn_i(struct gfs2_sbd *sdp, char *assertion,
110 const char *function, char *file, unsigned int line)
111{
112 if (time_before(jiffies,
113 sdp->sd_last_warning +
114 gfs2_tune_get(sdp, gt_complain_secs) * HZ))
115 return -2;
116
d34843d0 117 if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW)
8382e26b
JP
118 fs_warn(sdp, "warning: assertion \"%s\" failed at function = %s, file = %s, line = %u\n",
119 assertion, function, file, line);
b3b94faa
DT
120
121 if (sdp->sd_args.ar_debug)
122 BUG();
18ec7d5c
SW
123 else
124 dump_stack();
b3b94faa 125
d34843d0
BP
126 if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC)
127 panic("GFS2: fsid=%s: warning: assertion \"%s\" failed\n"
128 "GFS2: fsid=%s: function = %s, file = %s, line = %u\n",
129 sdp->sd_fsname, assertion,
130 sdp->sd_fsname, function, file, line);
131
b3b94faa
DT
132 sdp->sd_last_warning = jiffies;
133
134 return -1;
135}
136
137/**
138 * gfs2_consist_i - Flag a filesystem consistency error and withdraw
139 * Returns: -1 if this call withdrew the machine,
140 * 0 if it was already withdrawn
141 */
142
143int gfs2_consist_i(struct gfs2_sbd *sdp, int cluster_wide, const char *function,
144 char *file, unsigned int line)
145{
146 int rv;
147 rv = gfs2_lm_withdraw(sdp,
cb94eb06
JP
148 "fatal: filesystem consistency error - function = %s, file = %s, line = %u\n",
149 function, file, line);
b3b94faa
DT
150 return rv;
151}
152
153/**
154 * gfs2_consist_inode_i - Flag an inode consistency error and withdraw
155 * Returns: -1 if this call withdrew the machine,
156 * 0 if it was already withdrawn
157 */
158
159int gfs2_consist_inode_i(struct gfs2_inode *ip, int cluster_wide,
160 const char *function, char *file, unsigned int line)
161{
feaa7bba 162 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
b3b94faa
DT
163 int rv;
164 rv = gfs2_lm_withdraw(sdp,
cb94eb06
JP
165 "fatal: filesystem consistency error\n"
166 " inode = %llu %llu\n"
167 " function = %s, file = %s, line = %u\n",
168 (unsigned long long)ip->i_no_formal_ino,
169 (unsigned long long)ip->i_no_addr,
170 function, file, line);
b3b94faa
DT
171 return rv;
172}
173
174/**
175 * gfs2_consist_rgrpd_i - Flag a RG consistency error and withdraw
176 * Returns: -1 if this call withdrew the machine,
177 * 0 if it was already withdrawn
178 */
179
180int gfs2_consist_rgrpd_i(struct gfs2_rgrpd *rgd, int cluster_wide,
181 const char *function, char *file, unsigned int line)
182{
183 struct gfs2_sbd *sdp = rgd->rd_sbd;
184 int rv;
72244b6b
BP
185
186 gfs2_rgrp_dump(NULL, rgd->rd_gl);
b3b94faa 187 rv = gfs2_lm_withdraw(sdp,
cb94eb06
JP
188 "fatal: filesystem consistency error\n"
189 " RG = %llu\n"
190 " function = %s, file = %s, line = %u\n",
191 (unsigned long long)rgd->rd_addr,
192 function, file, line);
b3b94faa
DT
193 return rv;
194}
195
196/**
197 * gfs2_meta_check_ii - Flag a magic number consistency error and withdraw
198 * Returns: -1 if this call withdrew the machine,
199 * -2 if it was already withdrawn
200 */
201
202int gfs2_meta_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh,
203 const char *type, const char *function, char *file,
204 unsigned int line)
205{
206 int me;
207 me = gfs2_lm_withdraw(sdp,
cb94eb06
JP
208 "fatal: invalid metadata block\n"
209 " bh = %llu (%s)\n"
210 " function = %s, file = %s, line = %u\n",
211 (unsigned long long)bh->b_blocknr, type,
212 function, file, line);
b3b94faa
DT
213 return (me) ? -1 : -2;
214}
215
216/**
217 * gfs2_metatype_check_ii - Flag a metadata type consistency error and withdraw
218 * Returns: -1 if this call withdrew the machine,
219 * -2 if it was already withdrawn
220 */
221
222int gfs2_metatype_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh,
cd915493 223 u16 type, u16 t, const char *function,
b3b94faa
DT
224 char *file, unsigned int line)
225{
226 int me;
227 me = gfs2_lm_withdraw(sdp,
cb94eb06
JP
228 "fatal: invalid metadata block\n"
229 " bh = %llu (type: exp=%u, found=%u)\n"
230 " function = %s, file = %s, line = %u\n",
231 (unsigned long long)bh->b_blocknr, type, t,
232 function, file, line);
b3b94faa
DT
233 return (me) ? -1 : -2;
234}
235
236/**
237 * gfs2_io_error_i - Flag an I/O error and withdraw
238 * Returns: -1 if this call withdrew the machine,
239 * 0 if it was already withdrawn
240 */
241
242int gfs2_io_error_i(struct gfs2_sbd *sdp, const char *function, char *file,
243 unsigned int line)
244{
245 int rv;
246 rv = gfs2_lm_withdraw(sdp,
cb94eb06
JP
247 "fatal: I/O error\n"
248 " function = %s, file = %s, line = %u\n",
249 function, file, line);
b3b94faa
DT
250 return rv;
251}
252
253/**
9e1a9ecd
AG
254 * gfs2_io_error_bh_i - Flag a buffer I/O error
255 * @withdraw: withdraw the filesystem
b3b94faa
DT
256 */
257
9e1a9ecd
AG
258void gfs2_io_error_bh_i(struct gfs2_sbd *sdp, struct buffer_head *bh,
259 const char *function, char *file, unsigned int line,
260 bool withdraw)
b3b94faa 261{
b524abcc
BP
262 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
263 fs_err(sdp,
264 "fatal: I/O error\n"
265 " block = %llu\n"
266 " function = %s, file = %s, line = %u\n",
267 (unsigned long long)bh->b_blocknr,
268 function, file, line);
9e1a9ecd
AG
269 if (withdraw)
270 gfs2_lm_withdraw(sdp, NULL);
b3b94faa
DT
271}
272