[GFS2] Fix ref count bug that used to bite us on umount
[linux-2.6] / fs / gfs2 / util.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2005 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 v.2.
8  */
9
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/crc32.h>
16 #include <linux/gfs2_ondisk.h>
17 #include <asm/semaphore.h>
18 #include <asm/uaccess.h>
19
20 #include "gfs2.h"
21 #include "lm_interface.h"
22 #include "incore.h"
23 #include "glock.h"
24 #include "lm.h"
25 #include "util.h"
26
27 kmem_cache_t *gfs2_glock_cachep __read_mostly;
28 kmem_cache_t *gfs2_inode_cachep __read_mostly;
29 kmem_cache_t *gfs2_bufdata_cachep __read_mostly;
30
31 void gfs2_assert_i(struct gfs2_sbd *sdp)
32 {
33         printk(KERN_EMERG "GFS2: fsid=%s: fatal assertion failed\n",
34                sdp->sd_fsname);
35 }
36
37 /**
38  * gfs2_assert_withdraw_i - Cause the machine to withdraw if @assertion is false
39  * Returns: -1 if this call withdrew the machine,
40  *          -2 if it was already withdrawn
41  */
42
43 int gfs2_assert_withdraw_i(struct gfs2_sbd *sdp, char *assertion,
44                            const char *function, char *file, unsigned int line)
45 {
46         int me;
47         me = gfs2_lm_withdraw(sdp,
48                 "GFS2: fsid=%s: fatal: assertion \"%s\" failed\n"
49                 "GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
50                 sdp->sd_fsname, assertion,
51                 sdp->sd_fsname, function, file, line);
52         dump_stack();
53         return (me) ? -1 : -2;
54 }
55
56 /**
57  * gfs2_assert_warn_i - Print a message to the console if @assertion is false
58  * Returns: -1 if we printed something
59  *          -2 if we didn't
60  */
61
62 int gfs2_assert_warn_i(struct gfs2_sbd *sdp, char *assertion,
63                        const char *function, char *file, unsigned int line)
64 {
65         if (time_before(jiffies,
66                         sdp->sd_last_warning +
67                         gfs2_tune_get(sdp, gt_complain_secs) * HZ))
68                 return -2;
69
70         printk(KERN_WARNING
71                "GFS2: fsid=%s: warning: assertion \"%s\" failed\n"
72                "GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
73                sdp->sd_fsname, assertion,
74                sdp->sd_fsname, function, file, line);
75
76         if (sdp->sd_args.ar_debug)
77                 BUG();
78         else
79                 dump_stack();
80
81         sdp->sd_last_warning = jiffies;
82
83         return -1;
84 }
85
86 /**
87  * gfs2_consist_i - Flag a filesystem consistency error and withdraw
88  * Returns: -1 if this call withdrew the machine,
89  *          0 if it was already withdrawn
90  */
91
92 int gfs2_consist_i(struct gfs2_sbd *sdp, int cluster_wide, const char *function,
93                    char *file, unsigned int line)
94 {
95         int rv;
96         rv = gfs2_lm_withdraw(sdp,
97                 "GFS2: fsid=%s: fatal: filesystem consistency error\n"
98                 "GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
99                 sdp->sd_fsname,
100                 sdp->sd_fsname, function, file, line);
101         return rv;
102 }
103
104 /**
105  * gfs2_consist_inode_i - Flag an inode consistency error and withdraw
106  * Returns: -1 if this call withdrew the machine,
107  *          0 if it was already withdrawn
108  */
109
110 int gfs2_consist_inode_i(struct gfs2_inode *ip, int cluster_wide,
111                          const char *function, char *file, unsigned int line)
112 {
113         struct gfs2_sbd *sdp = ip->i_sbd;
114         int rv;
115         rv = gfs2_lm_withdraw(sdp,
116                 "GFS2: fsid=%s: fatal: filesystem consistency error\n"
117                 "GFS2: fsid=%s:   inode = %llu %llu\n"
118                 "GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
119                 sdp->sd_fsname,
120                 sdp->sd_fsname, ip->i_num.no_formal_ino, ip->i_num.no_addr,
121                 sdp->sd_fsname, function, file, line);
122         return rv;
123 }
124
125 /**
126  * gfs2_consist_rgrpd_i - Flag a RG consistency error and withdraw
127  * Returns: -1 if this call withdrew the machine,
128  *          0 if it was already withdrawn
129  */
130
131 int gfs2_consist_rgrpd_i(struct gfs2_rgrpd *rgd, int cluster_wide,
132                          const char *function, char *file, unsigned int line)
133 {
134         struct gfs2_sbd *sdp = rgd->rd_sbd;
135         int rv;
136         rv = gfs2_lm_withdraw(sdp,
137                 "GFS2: fsid=%s: fatal: filesystem consistency error\n"
138                 "GFS2: fsid=%s:   RG = %llu\n"
139                 "GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
140                 sdp->sd_fsname,
141                 sdp->sd_fsname, rgd->rd_ri.ri_addr,
142                 sdp->sd_fsname, function, file, line);
143         return rv;
144 }
145
146 /**
147  * gfs2_meta_check_ii - Flag a magic number consistency error and withdraw
148  * Returns: -1 if this call withdrew the machine,
149  *          -2 if it was already withdrawn
150  */
151
152 int gfs2_meta_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh,
153                        const char *type, const char *function, char *file,
154                        unsigned int line)
155 {
156         int me;
157         me = gfs2_lm_withdraw(sdp,
158                 "GFS2: fsid=%s: fatal: invalid metadata block\n"
159                 "GFS2: fsid=%s:   bh = %llu (%s)\n"
160                 "GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
161                 sdp->sd_fsname,
162                 sdp->sd_fsname, (uint64_t)bh->b_blocknr, type,
163                 sdp->sd_fsname, function, file, line);
164         return (me) ? -1 : -2;
165 }
166
167 /**
168  * gfs2_metatype_check_ii - Flag a metadata type consistency error and withdraw
169  * Returns: -1 if this call withdrew the machine,
170  *          -2 if it was already withdrawn
171  */
172
173 int gfs2_metatype_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh,
174                            uint16_t type, uint16_t t, const char *function,
175                            char *file, unsigned int line)
176 {
177         int me;
178         me = gfs2_lm_withdraw(sdp,
179                 "GFS2: fsid=%s: fatal: invalid metadata block\n"
180                 "GFS2: fsid=%s:   bh = %llu (type: exp=%u, found=%u)\n"
181                 "GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
182                 sdp->sd_fsname,
183                 sdp->sd_fsname, (uint64_t)bh->b_blocknr, type, t,
184                 sdp->sd_fsname, function, file, line);
185         return (me) ? -1 : -2;
186 }
187
188 /**
189  * gfs2_io_error_i - Flag an I/O error and withdraw
190  * Returns: -1 if this call withdrew the machine,
191  *          0 if it was already withdrawn
192  */
193
194 int gfs2_io_error_i(struct gfs2_sbd *sdp, const char *function, char *file,
195                     unsigned int line)
196 {
197         int rv;
198         rv = gfs2_lm_withdraw(sdp,
199                 "GFS2: fsid=%s: fatal: I/O error\n"
200                 "GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
201                 sdp->sd_fsname,
202                 sdp->sd_fsname, function, file, line);
203         return rv;
204 }
205
206 /**
207  * gfs2_io_error_bh_i - Flag a buffer I/O error and withdraw
208  * Returns: -1 if this call withdrew the machine,
209  *          0 if it was already withdrawn
210  */
211
212 int gfs2_io_error_bh_i(struct gfs2_sbd *sdp, struct buffer_head *bh,
213                        const char *function, char *file, unsigned int line)
214 {
215         int rv;
216         rv = gfs2_lm_withdraw(sdp,
217                 "GFS2: fsid=%s: fatal: I/O error\n"
218                 "GFS2: fsid=%s:   block = %llu\n"
219                 "GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
220                 sdp->sd_fsname,
221                 sdp->sd_fsname, (uint64_t)bh->b_blocknr,
222                 sdp->sd_fsname, function, file, line);
223         return rv;
224 }
225
226 void gfs2_icbit_munge(struct gfs2_sbd *sdp, unsigned char **bitmap,
227                       unsigned int bit, int new_value)
228 {
229         unsigned int c, o, b = bit;
230         int old_value;
231
232         c = b / (8 * PAGE_SIZE);
233         b %= 8 * PAGE_SIZE;
234         o = b / 8;
235         b %= 8;
236
237         old_value = (bitmap[c][o] & (1 << b));
238         gfs2_assert_withdraw(sdp, !old_value != !new_value);
239
240         if (new_value)
241                 bitmap[c][o] |= 1 << b;
242         else
243                 bitmap[c][o] &= ~(1 << b);
244 }
245