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