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