blob: f5cef2ad7ae1a36d3f2953ef423b8bbc8adfcead [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Bob Petersonda6dd402007-12-11 18:49:21 -06003 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
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>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050015#include <linux/crc32.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050016#include <linux/gfs2_ondisk.h>
Steven Whitehousef45b7dd2006-07-27 13:53:53 -040017#include <linux/bio.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020018#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000019
20#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050021#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include "bmap.h"
23#include "dir.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000024#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"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050034#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000035
David Teiglandb3b94fa2006-01-16 16:50:04 +000036/**
37 * gfs2_jindex_hold - Grab a lock on the jindex
38 * @sdp: The GFS2 superblock
39 * @ji_gh: the holder for the jindex glock
40 *
41 * This is very similar to the gfs2_rindex_hold() function, except that
42 * in general we hold the jindex lock for longer periods of time and
43 * we grab it far less frequently (in general) then the rgrp lock.
44 *
45 * Returns: errno
46 */
47
48int gfs2_jindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ji_gh)
49{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040050 struct gfs2_inode *dip = GFS2_I(sdp->sd_jindex);
David Teiglandb3b94fa2006-01-16 16:50:04 +000051 struct qstr name;
52 char buf[20];
53 struct gfs2_jdesc *jd;
54 int error;
55
56 name.name = buf;
57
Steven Whitehousef55ab262006-02-21 12:51:39 +000058 mutex_lock(&sdp->sd_jindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +000059
60 for (;;) {
Steven Whitehouse1c0f4872007-01-22 12:10:39 -050061 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, ji_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +000062 if (error)
63 break;
64
65 name.len = sprintf(buf, "journal%u", sdp->sd_journals);
Steven Whitehousec7526662006-03-20 12:30:04 -050066 name.hash = gfs2_disk_hash(name.name, name.len);
David Teiglandb3b94fa2006-01-16 16:50:04 +000067
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010068 error = gfs2_dir_check(sdp->sd_jindex, &name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +000069 if (error == -ENOENT) {
70 error = 0;
71 break;
72 }
73
74 gfs2_glock_dq_uninit(ji_gh);
75
76 if (error)
77 break;
78
79 error = -ENOMEM;
80 jd = kzalloc(sizeof(struct gfs2_jdesc), GFP_KERNEL);
81 if (!jd)
82 break;
83
Bob Peterson0811a122008-01-03 09:24:53 -060084 INIT_LIST_HEAD(&jd->extent_list);
Al Viroa569c712008-07-23 14:42:05 -040085 jd->jd_inode = gfs2_lookupi(sdp->sd_jindex, &name, 1);
Steven Whitehousec7526662006-03-20 12:30:04 -050086 if (!jd->jd_inode || IS_ERR(jd->jd_inode)) {
87 if (!jd->jd_inode)
88 error = -ENOENT;
89 else
90 error = PTR_ERR(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000091 kfree(jd);
92 break;
93 }
94
95 spin_lock(&sdp->sd_jindex_spin);
96 jd->jd_jid = sdp->sd_journals++;
97 list_add_tail(&jd->jd_list, &sdp->sd_jindex_list);
98 spin_unlock(&sdp->sd_jindex_spin);
99 }
100
Steven Whitehousef55ab262006-02-21 12:51:39 +0000101 mutex_unlock(&sdp->sd_jindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000102
103 return error;
104}
105
106/**
107 * gfs2_jindex_free - Clear all the journal index information
108 * @sdp: The GFS2 superblock
109 *
110 */
111
112void gfs2_jindex_free(struct gfs2_sbd *sdp)
113{
Bob Petersonda6dd402007-12-11 18:49:21 -0600114 struct list_head list, *head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000115 struct gfs2_jdesc *jd;
Bob Petersonda6dd402007-12-11 18:49:21 -0600116 struct gfs2_journal_extent *jext;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000117
118 spin_lock(&sdp->sd_jindex_spin);
119 list_add(&list, &sdp->sd_jindex_list);
120 list_del_init(&sdp->sd_jindex_list);
121 sdp->sd_journals = 0;
122 spin_unlock(&sdp->sd_jindex_spin);
123
124 while (!list_empty(&list)) {
125 jd = list_entry(list.next, struct gfs2_jdesc, jd_list);
Bob Petersonda6dd402007-12-11 18:49:21 -0600126 head = &jd->extent_list;
127 while (!list_empty(head)) {
128 jext = list_entry(head->next,
129 struct gfs2_journal_extent,
130 extent_list);
131 list_del(&jext->extent_list);
132 kfree(jext);
133 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000134 list_del(&jd->jd_list);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000135 iput(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000136 kfree(jd);
137 }
138}
139
140static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
141{
142 struct gfs2_jdesc *jd;
143 int found = 0;
144
145 list_for_each_entry(jd, head, jd_list) {
146 if (jd->jd_jid == jid) {
147 found = 1;
148 break;
149 }
150 }
151
152 if (!found)
153 jd = NULL;
154
155 return jd;
156}
157
158struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
159{
160 struct gfs2_jdesc *jd;
161
162 spin_lock(&sdp->sd_jindex_spin);
163 jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
164 spin_unlock(&sdp->sd_jindex_spin);
165
166 return jd;
167}
168
169void gfs2_jdesc_make_dirty(struct gfs2_sbd *sdp, unsigned int jid)
170{
171 struct gfs2_jdesc *jd;
172
173 spin_lock(&sdp->sd_jindex_spin);
174 jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
175 if (jd)
176 jd->jd_dirty = 1;
177 spin_unlock(&sdp->sd_jindex_spin);
178}
179
180struct gfs2_jdesc *gfs2_jdesc_find_dirty(struct gfs2_sbd *sdp)
181{
182 struct gfs2_jdesc *jd;
183 int found = 0;
184
185 spin_lock(&sdp->sd_jindex_spin);
186
187 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
188 if (jd->jd_dirty) {
189 jd->jd_dirty = 0;
190 found = 1;
191 break;
192 }
193 }
194 spin_unlock(&sdp->sd_jindex_spin);
195
196 if (!found)
197 jd = NULL;
198
199 return jd;
200}
201
202int gfs2_jdesc_check(struct gfs2_jdesc *jd)
203{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400204 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
205 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206 int ar;
207 int error;
208
Steven Whitehousec9e98882008-11-04 09:47:33 +0000209 if (ip->i_disksize < (8 << 20) || ip->i_disksize > (1 << 30) ||
210 (ip->i_disksize & (sdp->sd_sb.sb_bsize - 1))) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000211 gfs2_consist_inode(ip);
212 return -EIO;
213 }
Steven Whitehousec9e98882008-11-04 09:47:33 +0000214 jd->jd_blocks = ip->i_disksize >> sdp->sd_sb.sb_bsize_shift;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000215
Steven Whitehousec9e98882008-11-04 09:47:33 +0000216 error = gfs2_write_alloc_required(ip, 0, ip->i_disksize, &ar);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000217 if (!error && ar) {
218 gfs2_consist_inode(ip);
219 error = -EIO;
220 }
221
222 return error;
223}
224
David Teiglandb3b94fa2006-01-16 16:50:04 +0000225/**
226 * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
227 * @sdp: the filesystem
228 *
229 * Returns: errno
230 */
231
232int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
233{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400234 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500235 struct gfs2_glock *j_gl = ip->i_gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000236 struct gfs2_holder t_gh;
Al Viro55167622006-10-13 21:47:13 -0400237 struct gfs2_log_header_host head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000238 int error;
239
Steven Whitehouse1c0f4872007-01-22 12:10:39 -0500240 error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, 0, &t_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000241 if (error)
242 return error;
243
Steven Whitehouse1a14d3a2006-11-20 10:37:45 -0500244 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000245
246 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
247 if (error)
248 goto fail;
249
250 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
251 gfs2_consist(sdp);
252 error = -EIO;
253 goto fail;
254 }
255
256 /* Initialize some head of the log stuff */
257 sdp->sd_log_sequence = head.lh_sequence + 1;
258 gfs2_log_pointers_init(sdp, head.lh_blkno);
259
David Teiglandb3b94fa2006-01-16 16:50:04 +0000260 error = gfs2_quota_init(sdp);
261 if (error)
Steven Whitehousea91ea692006-09-04 12:04:26 -0400262 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000263
264 set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
265
266 gfs2_glock_dq_uninit(&t_gh);
267
268 return 0;
269
Steven Whitehousea91ea692006-09-04 12:04:26 -0400270fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000271 t_gh.gh_flags |= GL_NOCACHE;
272 gfs2_glock_dq_uninit(&t_gh);
273
274 return error;
275}
276
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100277static void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
278{
279 const struct gfs2_statfs_change *str = buf;
280
281 sc->sc_total = be64_to_cpu(str->sc_total);
282 sc->sc_free = be64_to_cpu(str->sc_free);
283 sc->sc_dinodes = be64_to_cpu(str->sc_dinodes);
284}
285
286static void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
287{
288 struct gfs2_statfs_change *str = buf;
289
290 str->sc_total = cpu_to_be64(sc->sc_total);
291 str->sc_free = cpu_to_be64(sc->sc_free);
292 str->sc_dinodes = cpu_to_be64(sc->sc_dinodes);
293}
294
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295int gfs2_statfs_init(struct gfs2_sbd *sdp)
296{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400297 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
Al Virobd209cc2006-10-13 23:43:19 -0400298 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400299 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
Al Virobd209cc2006-10-13 23:43:19 -0400300 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000301 struct buffer_head *m_bh, *l_bh;
302 struct gfs2_holder gh;
303 int error;
304
305 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
306 &gh);
307 if (error)
308 return error;
309
310 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
311 if (error)
312 goto out;
313
314 if (sdp->sd_args.ar_spectator) {
315 spin_lock(&sdp->sd_statfs_spin);
316 gfs2_statfs_change_in(m_sc, m_bh->b_data +
317 sizeof(struct gfs2_dinode));
318 spin_unlock(&sdp->sd_statfs_spin);
319 } else {
320 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
321 if (error)
322 goto out_m_bh;
323
324 spin_lock(&sdp->sd_statfs_spin);
325 gfs2_statfs_change_in(m_sc, m_bh->b_data +
326 sizeof(struct gfs2_dinode));
327 gfs2_statfs_change_in(l_sc, l_bh->b_data +
328 sizeof(struct gfs2_dinode));
329 spin_unlock(&sdp->sd_statfs_spin);
330
331 brelse(l_bh);
332 }
333
Steven Whitehousea91ea692006-09-04 12:04:26 -0400334out_m_bh:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000335 brelse(m_bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400336out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000337 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000338 return 0;
339}
340
Steven Whitehousecd915492006-09-04 12:49:07 -0400341void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
342 s64 dinodes)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000343{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400344 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
Al Virobd209cc2006-10-13 23:43:19 -0400345 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000346 struct buffer_head *l_bh;
347 int error;
348
349 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
350 if (error)
351 return;
352
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000353 gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354
355 spin_lock(&sdp->sd_statfs_spin);
356 l_sc->sc_total += total;
357 l_sc->sc_free += free;
358 l_sc->sc_dinodes += dinodes;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400359 gfs2_statfs_change_out(l_sc, l_bh->b_data + sizeof(struct gfs2_dinode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000360 spin_unlock(&sdp->sd_statfs_spin);
361
362 brelse(l_bh);
363}
364
365int gfs2_statfs_sync(struct gfs2_sbd *sdp)
366{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400367 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
368 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
Al Virobd209cc2006-10-13 23:43:19 -0400369 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
370 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000371 struct gfs2_holder gh;
372 struct buffer_head *m_bh, *l_bh;
373 int error;
374
375 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
376 &gh);
377 if (error)
378 return error;
379
380 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
381 if (error)
382 goto out;
383
384 spin_lock(&sdp->sd_statfs_spin);
385 gfs2_statfs_change_in(m_sc, m_bh->b_data +
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400386 sizeof(struct gfs2_dinode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000387 if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
388 spin_unlock(&sdp->sd_statfs_spin);
389 goto out_bh;
390 }
391 spin_unlock(&sdp->sd_statfs_spin);
392
393 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
394 if (error)
395 goto out_bh;
396
397 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
398 if (error)
399 goto out_bh2;
400
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000401 gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000402
403 spin_lock(&sdp->sd_statfs_spin);
404 m_sc->sc_total += l_sc->sc_total;
405 m_sc->sc_free += l_sc->sc_free;
406 m_sc->sc_dinodes += l_sc->sc_dinodes;
407 memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
408 memset(l_bh->b_data + sizeof(struct gfs2_dinode),
409 0, sizeof(struct gfs2_statfs_change));
410 spin_unlock(&sdp->sd_statfs_spin);
411
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000412 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000413 gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
414
415 gfs2_trans_end(sdp);
416
Steven Whitehousea91ea692006-09-04 12:04:26 -0400417out_bh2:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000418 brelse(l_bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400419out_bh:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000420 brelse(m_bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400421out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000422 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423 return error;
424}
425
426/**
427 * gfs2_statfs_i - Do a statfs
428 * @sdp: the filesystem
429 * @sg: the sg structure
430 *
431 * Returns: errno
432 */
433
Al Virobd209cc2006-10-13 23:43:19 -0400434int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000435{
Al Virobd209cc2006-10-13 23:43:19 -0400436 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
437 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000438
439 spin_lock(&sdp->sd_statfs_spin);
440
441 *sc = *m_sc;
442 sc->sc_total += l_sc->sc_total;
443 sc->sc_free += l_sc->sc_free;
444 sc->sc_dinodes += l_sc->sc_dinodes;
445
446 spin_unlock(&sdp->sd_statfs_spin);
447
448 if (sc->sc_free < 0)
449 sc->sc_free = 0;
450 if (sc->sc_free > sc->sc_total)
451 sc->sc_free = sc->sc_total;
452 if (sc->sc_dinodes < 0)
453 sc->sc_dinodes = 0;
454
455 return 0;
456}
457
458/**
459 * statfs_fill - fill in the sg for a given RG
460 * @rgd: the RG
461 * @sc: the sc structure
462 *
463 * Returns: 0 on success, -ESTALE if the LVB is invalid
464 */
465
466static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
Al Virobd209cc2006-10-13 23:43:19 -0400467 struct gfs2_statfs_change_host *sc)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000468{
469 gfs2_rgrp_verify(rgd);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100470 sc->sc_total += rgd->rd_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000471 sc->sc_free += rgd->rd_rg.rg_free;
472 sc->sc_dinodes += rgd->rd_rg.rg_dinodes;
473 return 0;
474}
475
476/**
477 * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
478 * @sdp: the filesystem
479 * @sc: the sc info that will be returned
480 *
481 * Any error (other than a signal) will cause this routine to fall back
482 * to the synchronous version.
483 *
484 * FIXME: This really shouldn't busy wait like this.
485 *
486 * Returns: errno
487 */
488
Al Virobd209cc2006-10-13 23:43:19 -0400489int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000490{
491 struct gfs2_holder ri_gh;
492 struct gfs2_rgrpd *rgd_next;
493 struct gfs2_holder *gha, *gh;
494 unsigned int slots = 64;
495 unsigned int x;
496 int done;
497 int error = 0, err;
498
Al Virobd209cc2006-10-13 23:43:19 -0400499 memset(sc, 0, sizeof(struct gfs2_statfs_change_host));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000500 gha = kcalloc(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
501 if (!gha)
502 return -ENOMEM;
503
504 error = gfs2_rindex_hold(sdp, &ri_gh);
505 if (error)
506 goto out;
507
508 rgd_next = gfs2_rgrpd_get_first(sdp);
509
510 for (;;) {
511 done = 1;
512
513 for (x = 0; x < slots; x++) {
514 gh = gha + x;
515
516 if (gh->gh_gl && gfs2_glock_poll(gh)) {
517 err = gfs2_glock_wait(gh);
518 if (err) {
519 gfs2_holder_uninit(gh);
520 error = err;
521 } else {
522 if (!error)
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500523 error = statfs_slow_fill(
524 gh->gh_gl->gl_object, sc);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000525 gfs2_glock_dq_uninit(gh);
526 }
527 }
528
529 if (gh->gh_gl)
530 done = 0;
531 else if (rgd_next && !error) {
532 error = gfs2_glock_nq_init(rgd_next->rd_gl,
533 LM_ST_SHARED,
534 GL_ASYNC,
535 gh);
536 rgd_next = gfs2_rgrpd_get_next(rgd_next);
537 done = 0;
538 }
539
540 if (signal_pending(current))
541 error = -ERESTARTSYS;
542 }
543
544 if (done)
545 break;
546
547 yield();
548 }
549
550 gfs2_glock_dq_uninit(&ri_gh);
551
Steven Whitehousea91ea692006-09-04 12:04:26 -0400552out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000553 kfree(gha);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000554 return error;
555}
556
557struct lfcc {
558 struct list_head list;
559 struct gfs2_holder gh;
560};
561
562/**
563 * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
564 * journals are clean
565 * @sdp: the file system
566 * @state: the state to put the transaction lock into
567 * @t_gh: the hold on the transaction lock
568 *
569 * Returns: errno
570 */
571
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400572static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp,
573 struct gfs2_holder *t_gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000574{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500575 struct gfs2_inode *ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000576 struct gfs2_holder ji_gh;
577 struct gfs2_jdesc *jd;
578 struct lfcc *lfcc;
579 LIST_HEAD(list);
Al Viro55167622006-10-13 21:47:13 -0400580 struct gfs2_log_header_host lh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000581 int error;
582
583 error = gfs2_jindex_hold(sdp, &ji_gh);
584 if (error)
585 return error;
586
587 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
588 lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
589 if (!lfcc) {
590 error = -ENOMEM;
591 goto out;
592 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400593 ip = GFS2_I(jd->jd_inode);
594 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000595 if (error) {
596 kfree(lfcc);
597 goto out;
598 }
599 list_add(&lfcc->list, &list);
600 }
601
602 error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_DEFERRED,
Steven Whitehouse6802e342008-05-21 17:03:22 +0100603 GL_NOCACHE, t_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000604
605 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
606 error = gfs2_jdesc_check(jd);
607 if (error)
608 break;
609 error = gfs2_find_jhead(jd, &lh);
610 if (error)
611 break;
612 if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
613 error = -EBUSY;
614 break;
615 }
616 }
617
618 if (error)
619 gfs2_glock_dq_uninit(t_gh);
620
Steven Whitehousea91ea692006-09-04 12:04:26 -0400621out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000622 while (!list_empty(&list)) {
623 lfcc = list_entry(list.next, struct lfcc, list);
624 list_del(&lfcc->list);
625 gfs2_glock_dq_uninit(&lfcc->gh);
626 kfree(lfcc);
627 }
628 gfs2_glock_dq_uninit(&ji_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000629 return error;
630}
631
632/**
633 * gfs2_freeze_fs - freezes the file system
634 * @sdp: the file system
635 *
636 * This function flushes data and meta data for all machines by
637 * aquiring the transaction log exclusively. All journals are
638 * ensured to be in a clean state as well.
639 *
640 * Returns: errno
641 */
642
643int gfs2_freeze_fs(struct gfs2_sbd *sdp)
644{
645 int error = 0;
646
Steven Whitehousef55ab262006-02-21 12:51:39 +0000647 mutex_lock(&sdp->sd_freeze_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000648
649 if (!sdp->sd_freeze_count++) {
650 error = gfs2_lock_fs_check_clean(sdp, &sdp->sd_freeze_gh);
651 if (error)
652 sdp->sd_freeze_count--;
653 }
654
Steven Whitehousef55ab262006-02-21 12:51:39 +0000655 mutex_unlock(&sdp->sd_freeze_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000656
657 return error;
658}
659
660/**
661 * gfs2_unfreeze_fs - unfreezes the file system
662 * @sdp: the file system
663 *
664 * This function allows the file system to proceed by unlocking
665 * the exclusively held transaction lock. Other GFS2 nodes are
666 * now free to acquire the lock shared and go on with their lives.
667 *
668 */
669
670void gfs2_unfreeze_fs(struct gfs2_sbd *sdp)
671{
Steven Whitehousef55ab262006-02-21 12:51:39 +0000672 mutex_lock(&sdp->sd_freeze_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000673
674 if (sdp->sd_freeze_count && !--sdp->sd_freeze_count)
675 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
676
Steven Whitehousef55ab262006-02-21 12:51:39 +0000677 mutex_unlock(&sdp->sd_freeze_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678}
679