]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/gfs2/super.c
GFS2: Streamline alloc calculations for writes
[net-next-2.6.git] / fs / gfs2 / super.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
da6dd40d 3 * Copyright (C) 2004-2007 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
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>
71b86f56 15#include <linux/crc32.h>
5c676f6d 16#include <linux/gfs2_ondisk.h>
f45b7ddd 17#include <linux/bio.h>
7d308590 18#include <linux/lm_interface.h>
b3b94faa
DT
19
20#include "gfs2.h"
5c676f6d 21#include "incore.h"
b3b94faa
DT
22#include "bmap.h"
23#include "dir.h"
b3b94faa
DT
24#include "glock.h"
25#include "glops.h"
26#include "inode.h"
27#include "log.h"
28#include "meta_io.h"
29#include "quota.h"
30#include "recovery.h"
31#include "rgrp.h"
32#include "super.h"
33#include "trans.h"
5c676f6d 34#include "util.h"
b3b94faa 35
b3b94faa
DT
36static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
37{
38 struct gfs2_jdesc *jd;
39 int found = 0;
40
41 list_for_each_entry(jd, head, jd_list) {
42 if (jd->jd_jid == jid) {
43 found = 1;
44 break;
45 }
46 }
47
48 if (!found)
49 jd = NULL;
50
51 return jd;
52}
53
54struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
55{
56 struct gfs2_jdesc *jd;
57
58 spin_lock(&sdp->sd_jindex_spin);
59 jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
60 spin_unlock(&sdp->sd_jindex_spin);
61
62 return jd;
63}
64
b3b94faa
DT
65int gfs2_jdesc_check(struct gfs2_jdesc *jd)
66{
feaa7bba
SW
67 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
68 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
b3b94faa
DT
69 int ar;
70 int error;
71
c9e98886
SW
72 if (ip->i_disksize < (8 << 20) || ip->i_disksize > (1 << 30) ||
73 (ip->i_disksize & (sdp->sd_sb.sb_bsize - 1))) {
b3b94faa
DT
74 gfs2_consist_inode(ip);
75 return -EIO;
76 }
c9e98886 77 jd->jd_blocks = ip->i_disksize >> sdp->sd_sb.sb_bsize_shift;
b3b94faa 78
c9e98886 79 error = gfs2_write_alloc_required(ip, 0, ip->i_disksize, &ar);
b3b94faa
DT
80 if (!error && ar) {
81 gfs2_consist_inode(ip);
82 error = -EIO;
83 }
84
85 return error;
86}
87
b3b94faa
DT
88/**
89 * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
90 * @sdp: the filesystem
91 *
92 * Returns: errno
93 */
94
95int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
96{
feaa7bba 97 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
5c676f6d 98 struct gfs2_glock *j_gl = ip->i_gl;
b3b94faa 99 struct gfs2_holder t_gh;
55167622 100 struct gfs2_log_header_host head;
b3b94faa
DT
101 int error;
102
1c0f4872 103 error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, 0, &t_gh);
b3b94faa
DT
104 if (error)
105 return error;
106
1a14d3a6 107 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
b3b94faa
DT
108
109 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
110 if (error)
111 goto fail;
112
113 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
114 gfs2_consist(sdp);
115 error = -EIO;
116 goto fail;
117 }
118
119 /* Initialize some head of the log stuff */
120 sdp->sd_log_sequence = head.lh_sequence + 1;
121 gfs2_log_pointers_init(sdp, head.lh_blkno);
122
b3b94faa
DT
123 error = gfs2_quota_init(sdp);
124 if (error)
a91ea69f 125 goto fail;
b3b94faa
DT
126
127 set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
128
129 gfs2_glock_dq_uninit(&t_gh);
130
131 return 0;
132
a91ea69f 133fail:
b3b94faa
DT
134 t_gh.gh_flags |= GL_NOCACHE;
135 gfs2_glock_dq_uninit(&t_gh);
136
137 return error;
138}
139
bb8d8a6f
SW
140static void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
141{
142 const struct gfs2_statfs_change *str = buf;
143
144 sc->sc_total = be64_to_cpu(str->sc_total);
145 sc->sc_free = be64_to_cpu(str->sc_free);
146 sc->sc_dinodes = be64_to_cpu(str->sc_dinodes);
147}
148
149static void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
150{
151 struct gfs2_statfs_change *str = buf;
152
153 str->sc_total = cpu_to_be64(sc->sc_total);
154 str->sc_free = cpu_to_be64(sc->sc_free);
155 str->sc_dinodes = cpu_to_be64(sc->sc_dinodes);
156}
157
b3b94faa
DT
158int gfs2_statfs_init(struct gfs2_sbd *sdp)
159{
feaa7bba 160 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
bd209cc0 161 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
feaa7bba 162 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
bd209cc0 163 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
b3b94faa
DT
164 struct buffer_head *m_bh, *l_bh;
165 struct gfs2_holder gh;
166 int error;
167
168 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
169 &gh);
170 if (error)
171 return error;
172
173 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
174 if (error)
175 goto out;
176
177 if (sdp->sd_args.ar_spectator) {
178 spin_lock(&sdp->sd_statfs_spin);
179 gfs2_statfs_change_in(m_sc, m_bh->b_data +
180 sizeof(struct gfs2_dinode));
181 spin_unlock(&sdp->sd_statfs_spin);
182 } else {
183 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
184 if (error)
185 goto out_m_bh;
186
187 spin_lock(&sdp->sd_statfs_spin);
188 gfs2_statfs_change_in(m_sc, m_bh->b_data +
189 sizeof(struct gfs2_dinode));
190 gfs2_statfs_change_in(l_sc, l_bh->b_data +
191 sizeof(struct gfs2_dinode));
192 spin_unlock(&sdp->sd_statfs_spin);
193
194 brelse(l_bh);
195 }
196
a91ea69f 197out_m_bh:
b3b94faa 198 brelse(m_bh);
a91ea69f 199out:
b3b94faa 200 gfs2_glock_dq_uninit(&gh);
b3b94faa
DT
201 return 0;
202}
203
cd915493
SW
204void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
205 s64 dinodes)
b3b94faa 206{
feaa7bba 207 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
bd209cc0 208 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
b3b94faa
DT
209 struct buffer_head *l_bh;
210 int error;
211
212 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
213 if (error)
214 return;
215
d4e9c4c3 216 gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
b3b94faa
DT
217
218 spin_lock(&sdp->sd_statfs_spin);
219 l_sc->sc_total += total;
220 l_sc->sc_free += free;
221 l_sc->sc_dinodes += dinodes;
907b9bce 222 gfs2_statfs_change_out(l_sc, l_bh->b_data + sizeof(struct gfs2_dinode));
b3b94faa
DT
223 spin_unlock(&sdp->sd_statfs_spin);
224
225 brelse(l_bh);
226}
227
228int gfs2_statfs_sync(struct gfs2_sbd *sdp)
229{
feaa7bba
SW
230 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
231 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
bd209cc0
AV
232 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
233 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
b3b94faa
DT
234 struct gfs2_holder gh;
235 struct buffer_head *m_bh, *l_bh;
236 int error;
237
238 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
239 &gh);
240 if (error)
241 return error;
242
243 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
244 if (error)
245 goto out;
246
247 spin_lock(&sdp->sd_statfs_spin);
248 gfs2_statfs_change_in(m_sc, m_bh->b_data +
907b9bce 249 sizeof(struct gfs2_dinode));
b3b94faa
DT
250 if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
251 spin_unlock(&sdp->sd_statfs_spin);
252 goto out_bh;
253 }
254 spin_unlock(&sdp->sd_statfs_spin);
255
256 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
257 if (error)
258 goto out_bh;
259
260 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
261 if (error)
262 goto out_bh2;
263
d4e9c4c3 264 gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
b3b94faa
DT
265
266 spin_lock(&sdp->sd_statfs_spin);
267 m_sc->sc_total += l_sc->sc_total;
268 m_sc->sc_free += l_sc->sc_free;
269 m_sc->sc_dinodes += l_sc->sc_dinodes;
270 memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
271 memset(l_bh->b_data + sizeof(struct gfs2_dinode),
272 0, sizeof(struct gfs2_statfs_change));
273 spin_unlock(&sdp->sd_statfs_spin);
274
d4e9c4c3 275 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
b3b94faa
DT
276 gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
277
278 gfs2_trans_end(sdp);
279
a91ea69f 280out_bh2:
b3b94faa 281 brelse(l_bh);
a91ea69f 282out_bh:
b3b94faa 283 brelse(m_bh);
a91ea69f 284out:
b3b94faa 285 gfs2_glock_dq_uninit(&gh);
b3b94faa
DT
286 return error;
287}
288
b3b94faa
DT
289struct lfcc {
290 struct list_head list;
291 struct gfs2_holder gh;
292};
293
294/**
295 * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
296 * journals are clean
297 * @sdp: the file system
298 * @state: the state to put the transaction lock into
299 * @t_gh: the hold on the transaction lock
300 *
301 * Returns: errno
302 */
303
08bc2dbc
AB
304static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp,
305 struct gfs2_holder *t_gh)
b3b94faa 306{
5c676f6d 307 struct gfs2_inode *ip;
b3b94faa
DT
308 struct gfs2_holder ji_gh;
309 struct gfs2_jdesc *jd;
310 struct lfcc *lfcc;
311 LIST_HEAD(list);
55167622 312 struct gfs2_log_header_host lh;
b3b94faa
DT
313 int error;
314
b3b94faa
DT
315 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
316 lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
317 if (!lfcc) {
318 error = -ENOMEM;
319 goto out;
320 }
feaa7bba
SW
321 ip = GFS2_I(jd->jd_inode);
322 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
b3b94faa
DT
323 if (error) {
324 kfree(lfcc);
325 goto out;
326 }
327 list_add(&lfcc->list, &list);
328 }
329
330 error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_DEFERRED,
6802e340 331 GL_NOCACHE, t_gh);
b3b94faa
DT
332
333 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
334 error = gfs2_jdesc_check(jd);
335 if (error)
336 break;
337 error = gfs2_find_jhead(jd, &lh);
338 if (error)
339 break;
340 if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
341 error = -EBUSY;
342 break;
343 }
344 }
345
346 if (error)
347 gfs2_glock_dq_uninit(t_gh);
348
a91ea69f 349out:
b3b94faa
DT
350 while (!list_empty(&list)) {
351 lfcc = list_entry(list.next, struct lfcc, list);
352 list_del(&lfcc->list);
353 gfs2_glock_dq_uninit(&lfcc->gh);
354 kfree(lfcc);
355 }
356 gfs2_glock_dq_uninit(&ji_gh);
b3b94faa
DT
357 return error;
358}
359
360/**
361 * gfs2_freeze_fs - freezes the file system
362 * @sdp: the file system
363 *
364 * This function flushes data and meta data for all machines by
365 * aquiring the transaction log exclusively. All journals are
366 * ensured to be in a clean state as well.
367 *
368 * Returns: errno
369 */
370
371int gfs2_freeze_fs(struct gfs2_sbd *sdp)
372{
373 int error = 0;
374
f55ab26a 375 mutex_lock(&sdp->sd_freeze_lock);
b3b94faa
DT
376
377 if (!sdp->sd_freeze_count++) {
378 error = gfs2_lock_fs_check_clean(sdp, &sdp->sd_freeze_gh);
379 if (error)
380 sdp->sd_freeze_count--;
381 }
382
f55ab26a 383 mutex_unlock(&sdp->sd_freeze_lock);
b3b94faa
DT
384
385 return error;
386}
387
388/**
389 * gfs2_unfreeze_fs - unfreezes the file system
390 * @sdp: the file system
391 *
392 * This function allows the file system to proceed by unlocking
393 * the exclusively held transaction lock. Other GFS2 nodes are
394 * now free to acquire the lock shared and go on with their lives.
395 *
396 */
397
398void gfs2_unfreeze_fs(struct gfs2_sbd *sdp)
399{
f55ab26a 400 mutex_lock(&sdp->sd_freeze_lock);
b3b94faa
DT
401
402 if (sdp->sd_freeze_count && !--sdp->sd_freeze_count)
403 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
404
f55ab26a 405 mutex_unlock(&sdp->sd_freeze_lock);
b3b94faa
DT
406}
407