[GFS2] Fix bug in super block reading code
[linux-2.6] / fs / gfs2 / super.c
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 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 <linux/bio.h>
18
19 #include "gfs2.h"
20 #include "lm_interface.h"
21 #include "incore.h"
22 #include "bmap.h"
23 #include "dir.h"
24 #include "format.h"
25 #include "glock.h"
26 #include "glops.h"
27 #include "inode.h"
28 #include "log.h"
29 #include "meta_io.h"
30 #include "quota.h"
31 #include "recovery.h"
32 #include "rgrp.h"
33 #include "super.h"
34 #include "trans.h"
35 #include "util.h"
36
37 /**
38  * gfs2_tune_init - Fill a gfs2_tune structure with default values
39  * @gt: tune
40  *
41  */
42
43 void gfs2_tune_init(struct gfs2_tune *gt)
44 {
45         spin_lock_init(&gt->gt_spin);
46
47         gt->gt_ilimit = 100;
48         gt->gt_ilimit_tries = 3;
49         gt->gt_ilimit_min = 1;
50         gt->gt_demote_secs = 300;
51         gt->gt_incore_log_blocks = 1024;
52         gt->gt_log_flush_secs = 60;
53         gt->gt_jindex_refresh_secs = 60;
54         gt->gt_scand_secs = 15;
55         gt->gt_recoverd_secs = 60;
56         gt->gt_logd_secs = 1;
57         gt->gt_quotad_secs = 5;
58         gt->gt_quota_simul_sync = 64;
59         gt->gt_quota_warn_period = 10;
60         gt->gt_quota_scale_num = 1;
61         gt->gt_quota_scale_den = 1;
62         gt->gt_quota_cache_secs = 300;
63         gt->gt_quota_quantum = 60;
64         gt->gt_atime_quantum = 3600;
65         gt->gt_new_files_jdata = 0;
66         gt->gt_new_files_directio = 0;
67         gt->gt_max_atomic_write = 4 << 20;
68         gt->gt_max_readahead = 1 << 18;
69         gt->gt_lockdump_size = 131072;
70         gt->gt_stall_secs = 600;
71         gt->gt_complain_secs = 10;
72         gt->gt_reclaim_limit = 5000;
73         gt->gt_entries_per_readdir = 32;
74         gt->gt_prefetch_secs = 10;
75         gt->gt_greedy_default = HZ / 10;
76         gt->gt_greedy_quantum = HZ / 40;
77         gt->gt_greedy_max = HZ / 4;
78         gt->gt_statfs_quantum = 30;
79         gt->gt_statfs_slow = 0;
80 }
81
82 /**
83  * gfs2_check_sb - Check superblock
84  * @sdp: the filesystem
85  * @sb: The superblock
86  * @silent: Don't print a message if the check fails
87  *
88  * Checks the version code of the FS is one that we understand how to
89  * read and that the sizes of the various on-disk structures have not
90  * changed.
91  */
92
93 int gfs2_check_sb(struct gfs2_sbd *sdp, struct gfs2_sb *sb, int silent)
94 {
95         unsigned int x;
96
97         if (sb->sb_header.mh_magic != GFS2_MAGIC ||
98             sb->sb_header.mh_type != GFS2_METATYPE_SB) {
99                 if (!silent)
100                         printk(KERN_WARNING "GFS2: not a GFS2 filesystem\n");
101                 return -EINVAL;
102         }
103
104         /*  If format numbers match exactly, we're done.  */
105
106         if (sb->sb_fs_format == GFS2_FORMAT_FS &&
107             sb->sb_multihost_format == GFS2_FORMAT_MULTI)
108                 return 0;
109
110         if (sb->sb_fs_format != GFS2_FORMAT_FS) {
111                 for (x = 0; gfs2_old_fs_formats[x]; x++)
112                         if (gfs2_old_fs_formats[x] == sb->sb_fs_format)
113                                 break;
114
115                 if (!gfs2_old_fs_formats[x]) {
116                         printk(KERN_WARNING
117                                "GFS2: code version (%u, %u) is incompatible "
118                                "with ondisk format (%u, %u)\n",
119                                GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
120                                sb->sb_fs_format, sb->sb_multihost_format);
121                         printk(KERN_WARNING
122                                "GFS2: I don't know how to upgrade this FS\n");
123                         return -EINVAL;
124                 }
125         }
126
127         if (sb->sb_multihost_format != GFS2_FORMAT_MULTI) {
128                 for (x = 0; gfs2_old_multihost_formats[x]; x++)
129                         if (gfs2_old_multihost_formats[x] ==
130                             sb->sb_multihost_format)
131                                 break;
132
133                 if (!gfs2_old_multihost_formats[x]) {
134                         printk(KERN_WARNING
135                                "GFS2: code version (%u, %u) is incompatible "
136                                "with ondisk format (%u, %u)\n",
137                                GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
138                                sb->sb_fs_format, sb->sb_multihost_format);
139                         printk(KERN_WARNING
140                                "GFS2: I don't know how to upgrade this FS\n");
141                         return -EINVAL;
142                 }
143         }
144
145         if (!sdp->sd_args.ar_upgrade) {
146                 printk(KERN_WARNING
147                        "GFS2: code version (%u, %u) is incompatible "
148                        "with ondisk format (%u, %u)\n",
149                        GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
150                        sb->sb_fs_format, sb->sb_multihost_format);
151                 printk(KERN_INFO
152                        "GFS2: Use the \"upgrade\" mount option to upgrade "
153                        "the FS\n");
154                 printk(KERN_INFO "GFS2: See the manual for more details\n");
155                 return -EINVAL;
156         }
157
158         return 0;
159 }
160
161
162 static int end_bio_io_page(struct bio *bio, unsigned int bytes_done, int error)
163 {
164         struct page *page = bio->bi_private;
165         if (bio->bi_size)
166                 return 1;
167
168         if (!error)
169                 SetPageUptodate(page);
170         else
171                 printk(KERN_WARNING "gfs2: error %d reading superblock\n", error);
172         unlock_page(page);
173         return 0;
174 }
175
176 static struct page *gfs2_read_super(struct super_block *sb, sector_t sector)
177 {
178         struct page *page;
179         struct bio *bio;
180
181         page = alloc_page(GFP_KERNEL);
182         if (unlikely(!page))
183                 return NULL;
184
185         ClearPageUptodate(page);
186         ClearPageDirty(page);
187         lock_page(page);
188
189         bio = bio_alloc(GFP_KERNEL, 1);
190         if (unlikely(!bio)) {
191                 __free_page(page);
192                 return NULL;
193         }
194
195         bio->bi_sector = sector;
196         bio->bi_bdev = sb->s_bdev;
197         bio_add_page(bio, page, PAGE_SIZE, 0);
198
199         bio->bi_end_io = end_bio_io_page;
200         bio->bi_private = page;
201         submit_bio(READ_SYNC, bio);
202         wait_on_page_locked(page);
203         bio_put(bio);
204         if (!PageUptodate(page)) {
205                 __free_page(page);
206                 return NULL;
207         }
208         return page;
209 }
210
211 /**
212  * gfs2_read_sb - Read super block
213  * @sdp: The GFS2 superblock
214  * @gl: the glock for the superblock (assumed to be held)
215  * @silent: Don't print message if mount fails
216  *
217  */
218
219 int gfs2_read_sb(struct gfs2_sbd *sdp, struct gfs2_glock *gl, int silent)
220 {
221         uint32_t hash_blocks, ind_blocks, leaf_blocks;
222         uint32_t tmp_blocks;
223         unsigned int x;
224         int error;
225         struct page *page;
226         char *sb;
227
228         page = gfs2_read_super(sdp->sd_vfs, GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift);
229         if (!page) {
230                 if (!silent)
231                         fs_err(sdp, "can't read superblock\n");
232                 return -EIO;
233         }
234         sb = kmap(page);
235         gfs2_sb_in(&sdp->sd_sb, sb);
236         kunmap(page);
237         __free_page(page);
238
239         error = gfs2_check_sb(sdp, &sdp->sd_sb, silent);
240         if (error)
241                 return error;
242
243         sdp->sd_fsb2bb_shift = sdp->sd_sb.sb_bsize_shift -
244                                GFS2_BASIC_BLOCK_SHIFT;
245         sdp->sd_fsb2bb = 1 << sdp->sd_fsb2bb_shift;
246         sdp->sd_diptrs = (sdp->sd_sb.sb_bsize -
247                           sizeof(struct gfs2_dinode)) / sizeof(uint64_t);
248         sdp->sd_inptrs = (sdp->sd_sb.sb_bsize -
249                           sizeof(struct gfs2_meta_header)) / sizeof(uint64_t);
250         sdp->sd_jbsize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header);
251         sdp->sd_hash_bsize = sdp->sd_sb.sb_bsize / 2;
252         sdp->sd_hash_bsize_shift = sdp->sd_sb.sb_bsize_shift - 1;
253         sdp->sd_hash_ptrs = sdp->sd_hash_bsize / sizeof(uint64_t);
254         sdp->sd_qc_per_block = (sdp->sd_sb.sb_bsize -
255                                 sizeof(struct gfs2_meta_header)) /
256                                 sizeof(struct gfs2_quota_change);
257
258         /* Compute maximum reservation required to add a entry to a directory */
259
260         hash_blocks = DIV_ROUND_UP(sizeof(uint64_t) * (1 << GFS2_DIR_MAX_DEPTH),
261                              sdp->sd_jbsize);
262
263         ind_blocks = 0;
264         for (tmp_blocks = hash_blocks; tmp_blocks > sdp->sd_diptrs;) {
265                 tmp_blocks = DIV_ROUND_UP(tmp_blocks, sdp->sd_inptrs);
266                 ind_blocks += tmp_blocks;
267         }
268
269         leaf_blocks = 2 + GFS2_DIR_MAX_DEPTH;
270
271         sdp->sd_max_dirres = hash_blocks + ind_blocks + leaf_blocks;
272
273         sdp->sd_heightsize[0] = sdp->sd_sb.sb_bsize -
274                                 sizeof(struct gfs2_dinode);
275         sdp->sd_heightsize[1] = sdp->sd_sb.sb_bsize * sdp->sd_diptrs;
276         for (x = 2;; x++) {
277                 uint64_t space, d;
278                 uint32_t m;
279
280                 space = sdp->sd_heightsize[x - 1] * sdp->sd_inptrs;
281                 d = space;
282                 m = do_div(d, sdp->sd_inptrs);
283
284                 if (d != sdp->sd_heightsize[x - 1] || m)
285                         break;
286                 sdp->sd_heightsize[x] = space;
287         }
288         sdp->sd_max_height = x;
289         gfs2_assert(sdp, sdp->sd_max_height <= GFS2_MAX_META_HEIGHT);
290
291         sdp->sd_jheightsize[0] = sdp->sd_sb.sb_bsize -
292                                  sizeof(struct gfs2_dinode);
293         sdp->sd_jheightsize[1] = sdp->sd_jbsize * sdp->sd_diptrs;
294         for (x = 2;; x++) {
295                 uint64_t space, d;
296                 uint32_t m;
297
298                 space = sdp->sd_jheightsize[x - 1] * sdp->sd_inptrs;
299                 d = space;
300                 m = do_div(d, sdp->sd_inptrs);
301
302                 if (d != sdp->sd_jheightsize[x - 1] || m)
303                         break;
304                 sdp->sd_jheightsize[x] = space;
305         }
306         sdp->sd_max_jheight = x;
307         gfs2_assert(sdp, sdp->sd_max_jheight <= GFS2_MAX_META_HEIGHT);
308
309         return 0;
310 }
311
312 /**
313  * gfs2_jindex_hold - Grab a lock on the jindex
314  * @sdp: The GFS2 superblock
315  * @ji_gh: the holder for the jindex glock
316  *
317  * This is very similar to the gfs2_rindex_hold() function, except that
318  * in general we hold the jindex lock for longer periods of time and
319  * we grab it far less frequently (in general) then the rgrp lock.
320  *
321  * Returns: errno
322  */
323
324 int gfs2_jindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ji_gh)
325 {
326         struct gfs2_inode *dip = GFS2_I(sdp->sd_jindex);
327         struct qstr name;
328         char buf[20];
329         struct gfs2_jdesc *jd;
330         int error;
331
332         name.name = buf;
333
334         mutex_lock(&sdp->sd_jindex_mutex);
335
336         for (;;) {
337                 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED,
338                                            GL_LOCAL_EXCL, ji_gh);
339                 if (error)
340                         break;
341
342                 name.len = sprintf(buf, "journal%u", sdp->sd_journals);
343                 name.hash = gfs2_disk_hash(name.name, name.len);
344
345                 error = gfs2_dir_search(sdp->sd_jindex, &name, NULL, NULL);
346                 if (error == -ENOENT) {
347                         error = 0;
348                         break;
349                 }
350
351                 gfs2_glock_dq_uninit(ji_gh);
352
353                 if (error)
354                         break;
355
356                 error = -ENOMEM;
357                 jd = kzalloc(sizeof(struct gfs2_jdesc), GFP_KERNEL);
358                 if (!jd)
359                         break;
360
361                 jd->jd_inode = gfs2_lookupi(sdp->sd_jindex, &name, 1, NULL);
362                 if (!jd->jd_inode || IS_ERR(jd->jd_inode)) {
363                         if (!jd->jd_inode)
364                                 error = -ENOENT;
365                         else
366                                 error = PTR_ERR(jd->jd_inode);
367                         kfree(jd);
368                         break;
369                 }
370
371                 spin_lock(&sdp->sd_jindex_spin);
372                 jd->jd_jid = sdp->sd_journals++;
373                 list_add_tail(&jd->jd_list, &sdp->sd_jindex_list);
374                 spin_unlock(&sdp->sd_jindex_spin);
375         }
376
377         mutex_unlock(&sdp->sd_jindex_mutex);
378
379         return error;
380 }
381
382 /**
383  * gfs2_jindex_free - Clear all the journal index information
384  * @sdp: The GFS2 superblock
385  *
386  */
387
388 void gfs2_jindex_free(struct gfs2_sbd *sdp)
389 {
390         struct list_head list;
391         struct gfs2_jdesc *jd;
392
393         spin_lock(&sdp->sd_jindex_spin);
394         list_add(&list, &sdp->sd_jindex_list);
395         list_del_init(&sdp->sd_jindex_list);
396         sdp->sd_journals = 0;
397         spin_unlock(&sdp->sd_jindex_spin);
398
399         while (!list_empty(&list)) {
400                 jd = list_entry(list.next, struct gfs2_jdesc, jd_list);
401                 list_del(&jd->jd_list);
402                 iput(jd->jd_inode);
403                 kfree(jd);
404         }
405 }
406
407 static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
408 {
409         struct gfs2_jdesc *jd;
410         int found = 0;
411
412         list_for_each_entry(jd, head, jd_list) {
413                 if (jd->jd_jid == jid) {
414                         found = 1;
415                         break;
416                 }
417         }
418
419         if (!found)
420                 jd = NULL;
421
422         return jd;
423 }
424
425 struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
426 {
427         struct gfs2_jdesc *jd;
428
429         spin_lock(&sdp->sd_jindex_spin);
430         jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
431         spin_unlock(&sdp->sd_jindex_spin);
432
433         return jd;
434 }
435
436 void gfs2_jdesc_make_dirty(struct gfs2_sbd *sdp, unsigned int jid)
437 {
438         struct gfs2_jdesc *jd;
439
440         spin_lock(&sdp->sd_jindex_spin);
441         jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
442         if (jd)
443                 jd->jd_dirty = 1;
444         spin_unlock(&sdp->sd_jindex_spin);
445 }
446
447 struct gfs2_jdesc *gfs2_jdesc_find_dirty(struct gfs2_sbd *sdp)
448 {
449         struct gfs2_jdesc *jd;
450         int found = 0;
451
452         spin_lock(&sdp->sd_jindex_spin);
453
454         list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
455                 if (jd->jd_dirty) {
456                         jd->jd_dirty = 0;
457                         found = 1;
458                         break;
459                 }
460         }
461         spin_unlock(&sdp->sd_jindex_spin);
462
463         if (!found)
464                 jd = NULL;
465
466         return jd;
467 }
468
469 int gfs2_jdesc_check(struct gfs2_jdesc *jd)
470 {
471         struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
472         struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
473         int ar;
474         int error;
475
476         if (ip->i_di.di_size < (8 << 20) || ip->i_di.di_size > (1 << 30) ||
477             (ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1))) {
478                 gfs2_consist_inode(ip);
479                 return -EIO;
480         }
481         jd->jd_blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
482
483         error = gfs2_write_alloc_required(ip, 0, ip->i_di.di_size, &ar);
484         if (!error && ar) {
485                 gfs2_consist_inode(ip);
486                 error = -EIO;
487         }
488
489         return error;
490 }
491
492 /**
493  * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
494  * @sdp: the filesystem
495  *
496  * Returns: errno
497  */
498
499 int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
500 {
501         struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
502         struct gfs2_glock *j_gl = ip->i_gl;
503         struct gfs2_holder t_gh;
504         struct gfs2_log_header head;
505         int error;
506
507         error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED,
508                                    GL_LOCAL_EXCL, &t_gh);
509         if (error)
510                 return error;
511
512         gfs2_meta_cache_flush(ip);
513         j_gl->gl_ops->go_inval(j_gl, DIO_METADATA | DIO_DATA);
514
515         error = gfs2_find_jhead(sdp->sd_jdesc, &head);
516         if (error)
517                 goto fail;
518
519         if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
520                 gfs2_consist(sdp);
521                 error = -EIO;
522                 goto fail;
523         }
524
525         /*  Initialize some head of the log stuff  */
526         sdp->sd_log_sequence = head.lh_sequence + 1;
527         gfs2_log_pointers_init(sdp, head.lh_blkno);
528
529         error = gfs2_quota_init(sdp);
530         if (error)
531                 goto fail_unlinked;
532
533         set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
534
535         gfs2_glock_dq_uninit(&t_gh);
536
537         return 0;
538
539  fail_unlinked:
540
541  fail:
542         t_gh.gh_flags |= GL_NOCACHE;
543         gfs2_glock_dq_uninit(&t_gh);
544
545         return error;
546 }
547
548 /**
549  * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
550  * @sdp: the filesystem
551  *
552  * Returns: errno
553  */
554
555 int gfs2_make_fs_ro(struct gfs2_sbd *sdp)
556 {
557         struct gfs2_holder t_gh;
558         int error;
559
560         gfs2_quota_sync(sdp);
561         gfs2_statfs_sync(sdp);
562
563         error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED,
564                                 GL_LOCAL_EXCL | GL_NOCACHE,
565                                 &t_gh);
566         if (error && !test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
567                 return error;
568
569         gfs2_meta_syncfs(sdp);
570         gfs2_log_shutdown(sdp);
571
572         clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
573
574         if (t_gh.gh_gl)
575                 gfs2_glock_dq_uninit(&t_gh);
576
577         gfs2_quota_cleanup(sdp);
578
579         return error;
580 }
581
582 int gfs2_statfs_init(struct gfs2_sbd *sdp)
583 {
584         struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
585         struct gfs2_statfs_change *m_sc = &sdp->sd_statfs_master;
586         struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
587         struct gfs2_statfs_change *l_sc = &sdp->sd_statfs_local;
588         struct buffer_head *m_bh, *l_bh;
589         struct gfs2_holder gh;
590         int error;
591
592         error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
593                                    &gh);
594         if (error)
595                 return error;
596
597         error = gfs2_meta_inode_buffer(m_ip, &m_bh);
598         if (error)
599                 goto out;
600
601         if (sdp->sd_args.ar_spectator) {
602                 spin_lock(&sdp->sd_statfs_spin);
603                 gfs2_statfs_change_in(m_sc, m_bh->b_data +
604                                       sizeof(struct gfs2_dinode));
605                 spin_unlock(&sdp->sd_statfs_spin);
606         } else {
607                 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
608                 if (error)
609                         goto out_m_bh;
610
611                 spin_lock(&sdp->sd_statfs_spin);
612                 gfs2_statfs_change_in(m_sc, m_bh->b_data +
613                                       sizeof(struct gfs2_dinode));
614                 gfs2_statfs_change_in(l_sc, l_bh->b_data +
615                                       sizeof(struct gfs2_dinode));
616                 spin_unlock(&sdp->sd_statfs_spin);
617
618                 brelse(l_bh);
619         }
620
621  out_m_bh:
622         brelse(m_bh);
623
624  out:
625         gfs2_glock_dq_uninit(&gh);
626
627         return 0;
628 }
629
630 void gfs2_statfs_change(struct gfs2_sbd *sdp, int64_t total, int64_t free,
631                         int64_t dinodes)
632 {
633         struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
634         struct gfs2_statfs_change *l_sc = &sdp->sd_statfs_local;
635         struct buffer_head *l_bh;
636         int error;
637
638         error = gfs2_meta_inode_buffer(l_ip, &l_bh);
639         if (error)
640                 return;
641
642         mutex_lock(&sdp->sd_statfs_mutex);
643         gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
644         mutex_unlock(&sdp->sd_statfs_mutex);
645
646         spin_lock(&sdp->sd_statfs_spin);
647         l_sc->sc_total += total;
648         l_sc->sc_free += free;
649         l_sc->sc_dinodes += dinodes;
650         gfs2_statfs_change_out(l_sc, l_bh->b_data +
651                                sizeof(struct gfs2_dinode));     
652         spin_unlock(&sdp->sd_statfs_spin);
653
654         brelse(l_bh);
655 }
656
657 int gfs2_statfs_sync(struct gfs2_sbd *sdp)
658 {
659         struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
660         struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
661         struct gfs2_statfs_change *m_sc = &sdp->sd_statfs_master;
662         struct gfs2_statfs_change *l_sc = &sdp->sd_statfs_local;
663         struct gfs2_holder gh;
664         struct buffer_head *m_bh, *l_bh;
665         int error;
666
667         error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
668                                    &gh);
669         if (error)
670                 return error;
671
672         error = gfs2_meta_inode_buffer(m_ip, &m_bh);
673         if (error)
674                 goto out;
675
676         spin_lock(&sdp->sd_statfs_spin);
677         gfs2_statfs_change_in(m_sc, m_bh->b_data +
678                               sizeof(struct gfs2_dinode));      
679         if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
680                 spin_unlock(&sdp->sd_statfs_spin);
681                 goto out_bh;
682         }
683         spin_unlock(&sdp->sd_statfs_spin);
684
685         error = gfs2_meta_inode_buffer(l_ip, &l_bh);
686         if (error)
687                 goto out_bh;
688
689         error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
690         if (error)
691                 goto out_bh2;
692
693         mutex_lock(&sdp->sd_statfs_mutex);
694         gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
695         mutex_unlock(&sdp->sd_statfs_mutex);
696
697         spin_lock(&sdp->sd_statfs_spin);
698         m_sc->sc_total += l_sc->sc_total;
699         m_sc->sc_free += l_sc->sc_free;
700         m_sc->sc_dinodes += l_sc->sc_dinodes;
701         memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
702         memset(l_bh->b_data + sizeof(struct gfs2_dinode),
703                0, sizeof(struct gfs2_statfs_change));
704         spin_unlock(&sdp->sd_statfs_spin);
705
706         gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
707         gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
708
709         gfs2_trans_end(sdp);
710
711  out_bh2:
712         brelse(l_bh);
713
714  out_bh:
715         brelse(m_bh);
716
717  out:
718         gfs2_glock_dq_uninit(&gh);
719
720         return error;
721 }
722
723 /**
724  * gfs2_statfs_i - Do a statfs
725  * @sdp: the filesystem
726  * @sg: the sg structure
727  *
728  * Returns: errno
729  */
730
731 int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change *sc)
732 {
733         struct gfs2_statfs_change *m_sc = &sdp->sd_statfs_master;
734         struct gfs2_statfs_change *l_sc = &sdp->sd_statfs_local;
735
736         spin_lock(&sdp->sd_statfs_spin);
737
738         *sc = *m_sc;
739         sc->sc_total += l_sc->sc_total;
740         sc->sc_free += l_sc->sc_free;
741         sc->sc_dinodes += l_sc->sc_dinodes;
742
743         spin_unlock(&sdp->sd_statfs_spin);
744
745         if (sc->sc_free < 0)
746                 sc->sc_free = 0;
747         if (sc->sc_free > sc->sc_total)
748                 sc->sc_free = sc->sc_total;
749         if (sc->sc_dinodes < 0)
750                 sc->sc_dinodes = 0;
751
752         return 0;
753 }
754
755 /**
756  * statfs_fill - fill in the sg for a given RG
757  * @rgd: the RG
758  * @sc: the sc structure
759  *
760  * Returns: 0 on success, -ESTALE if the LVB is invalid
761  */
762
763 static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
764                             struct gfs2_statfs_change *sc)
765 {
766         gfs2_rgrp_verify(rgd);
767         sc->sc_total += rgd->rd_ri.ri_data;
768         sc->sc_free += rgd->rd_rg.rg_free;
769         sc->sc_dinodes += rgd->rd_rg.rg_dinodes;
770         return 0;
771 }
772
773 /**
774  * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
775  * @sdp: the filesystem
776  * @sc: the sc info that will be returned
777  *
778  * Any error (other than a signal) will cause this routine to fall back
779  * to the synchronous version.
780  *
781  * FIXME: This really shouldn't busy wait like this.
782  *
783  * Returns: errno
784  */
785
786 int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change *sc)
787 {
788         struct gfs2_holder ri_gh;
789         struct gfs2_rgrpd *rgd_next;
790         struct gfs2_holder *gha, *gh;
791         unsigned int slots = 64;
792         unsigned int x;
793         int done;
794         int error = 0, err;
795
796         memset(sc, 0, sizeof(struct gfs2_statfs_change));
797         gha = kcalloc(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
798         if (!gha)
799                 return -ENOMEM;
800
801         error = gfs2_rindex_hold(sdp, &ri_gh);
802         if (error)
803                 goto out;
804
805         rgd_next = gfs2_rgrpd_get_first(sdp);
806
807         for (;;) {
808                 done = 1;
809
810                 for (x = 0; x < slots; x++) {
811                         gh = gha + x;
812
813                         if (gh->gh_gl && gfs2_glock_poll(gh)) {
814                                 err = gfs2_glock_wait(gh);
815                                 if (err) {
816                                         gfs2_holder_uninit(gh);
817                                         error = err;
818                                 } else {
819                                         if (!error)
820                                                 error = statfs_slow_fill(
821                                                         gh->gh_gl->gl_object, sc);
822                                         gfs2_glock_dq_uninit(gh);
823                                 }
824                         }
825
826                         if (gh->gh_gl)
827                                 done = 0;
828                         else if (rgd_next && !error) {
829                                 error = gfs2_glock_nq_init(rgd_next->rd_gl,
830                                                            LM_ST_SHARED,
831                                                            GL_ASYNC,
832                                                            gh);
833                                 rgd_next = gfs2_rgrpd_get_next(rgd_next);
834                                 done = 0;
835                         }
836
837                         if (signal_pending(current))
838                                 error = -ERESTARTSYS;
839                 }
840
841                 if (done)
842                         break;
843
844                 yield();
845         }
846
847         gfs2_glock_dq_uninit(&ri_gh);
848
849  out:
850         kfree(gha);
851
852         return error;
853 }
854
855 struct lfcc {
856         struct list_head list;
857         struct gfs2_holder gh;
858 };
859
860 /**
861  * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
862  *                            journals are clean
863  * @sdp: the file system
864  * @state: the state to put the transaction lock into
865  * @t_gh: the hold on the transaction lock
866  *
867  * Returns: errno
868  */
869
870 static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp,
871                                     struct gfs2_holder *t_gh)
872 {
873         struct gfs2_inode *ip;
874         struct gfs2_holder ji_gh;
875         struct gfs2_jdesc *jd;
876         struct lfcc *lfcc;
877         LIST_HEAD(list);
878         struct gfs2_log_header lh;
879         int error;
880
881         error = gfs2_jindex_hold(sdp, &ji_gh);
882         if (error)
883                 return error;
884
885         list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
886                 lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
887                 if (!lfcc) {
888                         error = -ENOMEM;
889                         goto out;
890                 }
891                 ip = GFS2_I(jd->jd_inode);
892                 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
893                 if (error) {
894                         kfree(lfcc);
895                         goto out;
896                 }
897                 list_add(&lfcc->list, &list);
898         }
899
900         error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_DEFERRED,
901                                LM_FLAG_PRIORITY | GL_NOCACHE,
902                                t_gh);
903
904         list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
905                 error = gfs2_jdesc_check(jd);
906                 if (error)
907                         break;
908                 error = gfs2_find_jhead(jd, &lh);
909                 if (error)
910                         break;
911                 if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
912                         error = -EBUSY;
913                         break;
914                 }
915         }
916
917         if (error)
918                 gfs2_glock_dq_uninit(t_gh);
919
920  out:
921         while (!list_empty(&list)) {
922                 lfcc = list_entry(list.next, struct lfcc, list);
923                 list_del(&lfcc->list);
924                 gfs2_glock_dq_uninit(&lfcc->gh);
925                 kfree(lfcc);
926         }
927         gfs2_glock_dq_uninit(&ji_gh);
928
929         return error;
930 }
931
932 /**
933  * gfs2_freeze_fs - freezes the file system
934  * @sdp: the file system
935  *
936  * This function flushes data and meta data for all machines by
937  * aquiring the transaction log exclusively.  All journals are
938  * ensured to be in a clean state as well.
939  *
940  * Returns: errno
941  */
942
943 int gfs2_freeze_fs(struct gfs2_sbd *sdp)
944 {
945         int error = 0;
946
947         mutex_lock(&sdp->sd_freeze_lock);
948
949         if (!sdp->sd_freeze_count++) {
950                 error = gfs2_lock_fs_check_clean(sdp, &sdp->sd_freeze_gh);
951                 if (error)
952                         sdp->sd_freeze_count--;
953         }
954
955         mutex_unlock(&sdp->sd_freeze_lock);
956
957         return error;
958 }
959
960 /**
961  * gfs2_unfreeze_fs - unfreezes the file system
962  * @sdp: the file system
963  *
964  * This function allows the file system to proceed by unlocking
965  * the exclusively held transaction lock.  Other GFS2 nodes are
966  * now free to acquire the lock shared and go on with their lives.
967  *
968  */
969
970 void gfs2_unfreeze_fs(struct gfs2_sbd *sdp)
971 {
972         mutex_lock(&sdp->sd_freeze_lock);
973
974         if (sdp->sd_freeze_count && !--sdp->sd_freeze_count)
975                 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
976
977         mutex_unlock(&sdp->sd_freeze_lock);
978 }
979