]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/gfs2/super.c
Merge branch 'master'
[linux-2.6-omap-h63xx.git] / 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 int gfs2_do_upgrade(struct gfs2_sbd *sdp, struct gfs2_glock *sb_gl)
268 {
269         return 0;
270 }
271
272 /**
273  * gfs2_jindex_hold - Grab a lock on the jindex
274  * @sdp: The GFS2 superblock
275  * @ji_gh: the holder for the jindex glock
276  *
277  * This is very similar to the gfs2_rindex_hold() function, except that
278  * in general we hold the jindex lock for longer periods of time and
279  * we grab it far less frequently (in general) then the rgrp lock.
280  *
281  * Returns: errno
282  */
283
284 int gfs2_jindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ji_gh)
285 {
286         struct gfs2_inode *dip = sdp->sd_jindex->u.generic_ip;
287         struct qstr name;
288         char buf[20];
289         struct gfs2_jdesc *jd;
290         int error;
291
292         name.name = buf;
293
294         mutex_lock(&sdp->sd_jindex_mutex);
295
296         for (;;) {
297                 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED,
298                                            GL_LOCAL_EXCL, ji_gh);
299                 if (error)
300                         break;
301
302                 name.len = sprintf(buf, "journal%u", sdp->sd_journals);
303                 name.hash = gfs2_disk_hash(name.name, name.len);
304
305                 error = gfs2_dir_search(sdp->sd_jindex,
306                                         &name, NULL, NULL);
307                 if (error == -ENOENT) {
308                         error = 0;
309                         break;
310                 }
311
312                 gfs2_glock_dq_uninit(ji_gh);
313
314                 if (error)
315                         break;
316
317                 error = -ENOMEM;
318                 jd = kzalloc(sizeof(struct gfs2_jdesc), GFP_KERNEL);
319                 if (!jd)
320                         break;
321
322                 jd->jd_inode = gfs2_lookupi(sdp->sd_jindex, &name, 1, NULL);
323                 if (!jd->jd_inode || IS_ERR(jd->jd_inode)) {
324                         if (!jd->jd_inode)
325                                 error = -ENOENT;
326                         else
327                                 error = PTR_ERR(jd->jd_inode);
328                         kfree(jd);
329                         break;
330                 }
331
332                 spin_lock(&sdp->sd_jindex_spin);
333                 jd->jd_jid = sdp->sd_journals++;
334                 list_add_tail(&jd->jd_list, &sdp->sd_jindex_list);
335                 spin_unlock(&sdp->sd_jindex_spin);
336         }
337
338         mutex_unlock(&sdp->sd_jindex_mutex);
339
340         return error;
341 }
342
343 /**
344  * gfs2_jindex_free - Clear all the journal index information
345  * @sdp: The GFS2 superblock
346  *
347  */
348
349 void gfs2_jindex_free(struct gfs2_sbd *sdp)
350 {
351         struct list_head list;
352         struct gfs2_jdesc *jd;
353
354         spin_lock(&sdp->sd_jindex_spin);
355         list_add(&list, &sdp->sd_jindex_list);
356         list_del_init(&sdp->sd_jindex_list);
357         sdp->sd_journals = 0;
358         spin_unlock(&sdp->sd_jindex_spin);
359
360         while (!list_empty(&list)) {
361                 jd = list_entry(list.next, struct gfs2_jdesc, jd_list);
362                 list_del(&jd->jd_list);
363                 iput(jd->jd_inode);
364                 kfree(jd);
365         }
366 }
367
368 static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
369 {
370         struct gfs2_jdesc *jd;
371         int found = 0;
372
373         list_for_each_entry(jd, head, jd_list) {
374                 if (jd->jd_jid == jid) {
375                         found = 1;
376                         break;
377                 }
378         }
379
380         if (!found)
381                 jd = NULL;
382
383         return jd;
384 }
385
386 struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
387 {
388         struct gfs2_jdesc *jd;
389
390         spin_lock(&sdp->sd_jindex_spin);
391         jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
392         spin_unlock(&sdp->sd_jindex_spin);
393
394         return jd;
395 }
396
397 void gfs2_jdesc_make_dirty(struct gfs2_sbd *sdp, unsigned int jid)
398 {
399         struct gfs2_jdesc *jd;
400
401         spin_lock(&sdp->sd_jindex_spin);
402         jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
403         if (jd)
404                 jd->jd_dirty = 1;
405         spin_unlock(&sdp->sd_jindex_spin);
406 }
407
408 struct gfs2_jdesc *gfs2_jdesc_find_dirty(struct gfs2_sbd *sdp)
409 {
410         struct gfs2_jdesc *jd;
411         int found = 0;
412
413         spin_lock(&sdp->sd_jindex_spin);
414
415         list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
416                 if (jd->jd_dirty) {
417                         jd->jd_dirty = 0;
418                         found = 1;
419                         break;
420                 }
421         }
422         spin_unlock(&sdp->sd_jindex_spin);
423
424         if (!found)
425                 jd = NULL;
426
427         return jd;
428 }
429
430 int gfs2_jdesc_check(struct gfs2_jdesc *jd)
431 {
432         struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
433         struct gfs2_sbd *sdp = ip->i_sbd;
434         int ar;
435         int error;
436
437         if (ip->i_di.di_size < (8 << 20) ||
438             ip->i_di.di_size > (1 << 30) ||
439             (ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1))) {
440                 gfs2_consist_inode(ip);
441                 return -EIO;
442         }
443         jd->jd_blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
444
445         error = gfs2_write_alloc_required(ip,
446                                           0, ip->i_di.di_size,
447                                           &ar);
448         if (!error && ar) {
449                 gfs2_consist_inode(ip);
450                 error = -EIO;
451         }
452
453         return error;
454 }
455
456 /**
457  * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
458  * @sdp: the filesystem
459  *
460  * Returns: errno
461  */
462
463 int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
464 {
465         struct gfs2_inode *ip = sdp->sd_jdesc->jd_inode->u.generic_ip;
466         struct gfs2_glock *j_gl = ip->i_gl;
467         struct gfs2_holder t_gh;
468         struct gfs2_log_header head;
469         int error;
470
471         error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED,
472                                    GL_LOCAL_EXCL, &t_gh);
473         if (error)
474                 return error;
475
476         gfs2_meta_cache_flush(ip);
477         j_gl->gl_ops->go_inval(j_gl, DIO_METADATA | DIO_DATA);
478
479         error = gfs2_find_jhead(sdp->sd_jdesc, &head);
480         if (error)
481                 goto fail;
482
483         if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
484                 gfs2_consist(sdp);
485                 error = -EIO;
486                 goto fail;
487         }
488
489         /*  Initialize some head of the log stuff  */
490         sdp->sd_log_sequence = head.lh_sequence + 1;
491         gfs2_log_pointers_init(sdp, head.lh_blkno);
492
493         error = gfs2_unlinked_init(sdp);
494         if (error)
495                 goto fail;
496         error = gfs2_quota_init(sdp);
497         if (error)
498                 goto fail_unlinked;
499
500         set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
501
502         gfs2_glock_dq_uninit(&t_gh);
503
504         return 0;
505
506  fail_unlinked:
507         gfs2_unlinked_cleanup(sdp);
508
509  fail:
510         t_gh.gh_flags |= GL_NOCACHE;
511         gfs2_glock_dq_uninit(&t_gh);
512
513         return error;
514 }
515
516 /**
517  * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
518  * @sdp: the filesystem
519  *
520  * Returns: errno
521  */
522
523 int gfs2_make_fs_ro(struct gfs2_sbd *sdp)
524 {
525         struct gfs2_holder t_gh;
526         int error;
527
528         gfs2_unlinked_dealloc(sdp);
529         gfs2_quota_sync(sdp);
530         gfs2_statfs_sync(sdp);
531
532         error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED,
533                                 GL_LOCAL_EXCL | GL_NOCACHE,
534                                 &t_gh);
535         if (error && !test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
536                 return error;
537
538         gfs2_meta_syncfs(sdp);
539         gfs2_log_shutdown(sdp);
540
541         clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
542
543         if (t_gh.gh_gl)
544                 gfs2_glock_dq_uninit(&t_gh);
545
546         gfs2_unlinked_cleanup(sdp);
547         gfs2_quota_cleanup(sdp);
548
549         return error;
550 }
551
552 int gfs2_statfs_init(struct gfs2_sbd *sdp)
553 {
554         struct gfs2_inode *m_ip = sdp->sd_statfs_inode->u.generic_ip;
555         struct gfs2_statfs_change *m_sc = &sdp->sd_statfs_master;
556         struct gfs2_inode *l_ip = sdp->sd_sc_inode->u.generic_ip;
557         struct gfs2_statfs_change *l_sc = &sdp->sd_statfs_local;
558         struct buffer_head *m_bh, *l_bh;
559         struct gfs2_holder gh;
560         int error;
561
562         error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
563                                    &gh);
564         if (error)
565                 return error;
566
567         error = gfs2_meta_inode_buffer(m_ip, &m_bh);
568         if (error)
569                 goto out;
570
571         if (sdp->sd_args.ar_spectator) {
572                 spin_lock(&sdp->sd_statfs_spin);
573                 gfs2_statfs_change_in(m_sc, m_bh->b_data +
574                                       sizeof(struct gfs2_dinode));
575                 spin_unlock(&sdp->sd_statfs_spin);
576         } else {
577                 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
578                 if (error)
579                         goto out_m_bh;
580
581                 spin_lock(&sdp->sd_statfs_spin);
582                 gfs2_statfs_change_in(m_sc, m_bh->b_data +
583                                       sizeof(struct gfs2_dinode));
584                 gfs2_statfs_change_in(l_sc, l_bh->b_data +
585                                       sizeof(struct gfs2_dinode));
586                 spin_unlock(&sdp->sd_statfs_spin);
587
588                 brelse(l_bh);
589         }
590
591  out_m_bh:
592         brelse(m_bh);
593
594  out:
595         gfs2_glock_dq_uninit(&gh);
596
597         return 0;
598 }
599
600 void gfs2_statfs_change(struct gfs2_sbd *sdp, int64_t total, int64_t free,
601                         int64_t dinodes)
602 {
603         struct gfs2_inode *l_ip = sdp->sd_sc_inode->u.generic_ip;
604         struct gfs2_statfs_change *l_sc = &sdp->sd_statfs_local;
605         struct buffer_head *l_bh;
606         int error;
607
608         error = gfs2_meta_inode_buffer(l_ip, &l_bh);
609         if (error)
610                 return;
611
612         mutex_lock(&sdp->sd_statfs_mutex);
613         gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
614         mutex_unlock(&sdp->sd_statfs_mutex);
615
616         spin_lock(&sdp->sd_statfs_spin);
617         l_sc->sc_total += total;
618         l_sc->sc_free += free;
619         l_sc->sc_dinodes += dinodes;
620         gfs2_statfs_change_out(l_sc, l_bh->b_data +
621                                sizeof(struct gfs2_dinode));     
622         spin_unlock(&sdp->sd_statfs_spin);
623
624         brelse(l_bh);
625 }
626
627 int gfs2_statfs_sync(struct gfs2_sbd *sdp)
628 {
629         struct gfs2_inode *m_ip = sdp->sd_statfs_inode->u.generic_ip;
630         struct gfs2_inode *l_ip = sdp->sd_sc_inode->u.generic_ip;
631         struct gfs2_statfs_change *m_sc = &sdp->sd_statfs_master;
632         struct gfs2_statfs_change *l_sc = &sdp->sd_statfs_local;
633         struct gfs2_holder gh;
634         struct buffer_head *m_bh, *l_bh;
635         int error;
636
637         error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
638                                    &gh);
639         if (error)
640                 return error;
641
642         error = gfs2_meta_inode_buffer(m_ip, &m_bh);
643         if (error)
644                 goto out;
645
646         spin_lock(&sdp->sd_statfs_spin);
647         gfs2_statfs_change_in(m_sc, m_bh->b_data +
648                               sizeof(struct gfs2_dinode));      
649         if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
650                 spin_unlock(&sdp->sd_statfs_spin);
651                 goto out_bh;
652         }
653         spin_unlock(&sdp->sd_statfs_spin);
654
655         error = gfs2_meta_inode_buffer(l_ip, &l_bh);
656         if (error)
657                 goto out_bh;
658
659         error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
660         if (error)
661                 goto out_bh2;
662
663         mutex_lock(&sdp->sd_statfs_mutex);
664         gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
665         mutex_unlock(&sdp->sd_statfs_mutex);
666
667         spin_lock(&sdp->sd_statfs_spin);
668         m_sc->sc_total += l_sc->sc_total;
669         m_sc->sc_free += l_sc->sc_free;
670         m_sc->sc_dinodes += l_sc->sc_dinodes;
671         memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
672         memset(l_bh->b_data + sizeof(struct gfs2_dinode),
673                0, sizeof(struct gfs2_statfs_change));
674         spin_unlock(&sdp->sd_statfs_spin);
675
676         gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
677         gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
678
679         gfs2_trans_end(sdp);
680
681  out_bh2:
682         brelse(l_bh);
683
684  out_bh:
685         brelse(m_bh);
686
687  out:
688         gfs2_glock_dq_uninit(&gh);
689
690         return error;
691 }
692
693 /**
694  * gfs2_statfs_i - Do a statfs
695  * @sdp: the filesystem
696  * @sg: the sg structure
697  *
698  * Returns: errno
699  */
700
701 int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change *sc)
702 {
703         struct gfs2_statfs_change *m_sc = &sdp->sd_statfs_master;
704         struct gfs2_statfs_change *l_sc = &sdp->sd_statfs_local;
705
706         spin_lock(&sdp->sd_statfs_spin);
707
708         *sc = *m_sc;
709         sc->sc_total += l_sc->sc_total;
710         sc->sc_free += l_sc->sc_free;
711         sc->sc_dinodes += l_sc->sc_dinodes;
712
713         spin_unlock(&sdp->sd_statfs_spin);
714
715         if (sc->sc_free < 0)
716                 sc->sc_free = 0;
717         if (sc->sc_free > sc->sc_total)
718                 sc->sc_free = sc->sc_total;
719         if (sc->sc_dinodes < 0)
720                 sc->sc_dinodes = 0;
721
722         return 0;
723 }
724
725 /**
726  * statfs_fill - fill in the sg for a given RG
727  * @rgd: the RG
728  * @sc: the sc structure
729  *
730  * Returns: 0 on success, -ESTALE if the LVB is invalid
731  */
732
733 static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
734                             struct gfs2_statfs_change *sc)
735 {
736         gfs2_rgrp_verify(rgd);
737         sc->sc_total += rgd->rd_ri.ri_data;
738         sc->sc_free += rgd->rd_rg.rg_free;
739         sc->sc_dinodes += rgd->rd_rg.rg_dinodes;
740         return 0;
741 }
742
743 /**
744  * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
745  * @sdp: the filesystem
746  * @sc: the sc info that will be returned
747  *
748  * Any error (other than a signal) will cause this routine to fall back
749  * to the synchronous version.
750  *
751  * FIXME: This really shouldn't busy wait like this.
752  *
753  * Returns: errno
754  */
755
756 int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change *sc)
757 {
758         struct gfs2_holder ri_gh;
759         struct gfs2_rgrpd *rgd_next;
760         struct gfs2_holder *gha, *gh;
761         unsigned int slots = 64;
762         unsigned int x;
763         int done;
764         int error = 0, err;
765
766         memset(sc, 0, sizeof(struct gfs2_statfs_change));
767         gha = kcalloc(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
768         if (!gha)
769                 return -ENOMEM;
770
771         error = gfs2_rindex_hold(sdp, &ri_gh);
772         if (error)
773                 goto out;
774
775         rgd_next = gfs2_rgrpd_get_first(sdp);
776
777         for (;;) {
778                 done = 1;
779
780                 for (x = 0; x < slots; x++) {
781                         gh = gha + x;
782
783                         if (gh->gh_gl && gfs2_glock_poll(gh)) {
784                                 err = gfs2_glock_wait(gh);
785                                 if (err) {
786                                         gfs2_holder_uninit(gh);
787                                         error = err;
788                                 } else {
789                                         if (!error)
790                                                 error = statfs_slow_fill(
791                                                         gh->gh_gl->gl_object, sc);
792                                         gfs2_glock_dq_uninit(gh);
793                                 }
794                         }
795
796                         if (gh->gh_gl)
797                                 done = 0;
798                         else if (rgd_next && !error) {
799                                 error = gfs2_glock_nq_init(rgd_next->rd_gl,
800                                                            LM_ST_SHARED,
801                                                            GL_ASYNC,
802                                                            gh);
803                                 rgd_next = gfs2_rgrpd_get_next(rgd_next);
804                                 done = 0;
805                         }
806
807                         if (signal_pending(current))
808                                 error = -ERESTARTSYS;
809                 }
810
811                 if (done)
812                         break;
813
814                 yield();
815         }
816
817         gfs2_glock_dq_uninit(&ri_gh);
818
819  out:
820         kfree(gha);
821
822         return error;
823 }
824
825 struct lfcc {
826         struct list_head list;
827         struct gfs2_holder gh;
828 };
829
830 /**
831  * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
832  *                            journals are clean
833  * @sdp: the file system
834  * @state: the state to put the transaction lock into
835  * @t_gh: the hold on the transaction lock
836  *
837  * Returns: errno
838  */
839
840 int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp, struct gfs2_holder *t_gh)
841 {
842         struct gfs2_inode *ip;
843         struct gfs2_holder ji_gh;
844         struct gfs2_jdesc *jd;
845         struct lfcc *lfcc;
846         LIST_HEAD(list);
847         struct gfs2_log_header lh;
848         int error;
849
850         error = gfs2_jindex_hold(sdp, &ji_gh);
851         if (error)
852                 return error;
853
854         list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
855                 lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
856                 if (!lfcc) {
857                         error = -ENOMEM;
858                         goto out;
859                 }
860                 ip = jd->jd_inode->u.generic_ip;
861                 error = gfs2_glock_nq_init(ip->i_gl,
862                                            LM_ST_SHARED, 0,
863                                            &lfcc->gh);
864                 if (error) {
865                         kfree(lfcc);
866                         goto out;
867                 }
868                 list_add(&lfcc->list, &list);
869         }
870
871         error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_DEFERRED,
872                                LM_FLAG_PRIORITY | GL_NOCACHE,
873                                t_gh);
874
875         list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
876                 error = gfs2_jdesc_check(jd);
877                 if (error)
878                         break;
879                 error = gfs2_find_jhead(jd, &lh);
880                 if (error)
881                         break;
882                 if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
883                         error = -EBUSY;
884                         break;
885                 }
886         }
887
888         if (error)
889                 gfs2_glock_dq_uninit(t_gh);
890
891  out:
892         while (!list_empty(&list)) {
893                 lfcc = list_entry(list.next, struct lfcc, list);
894                 list_del(&lfcc->list);
895                 gfs2_glock_dq_uninit(&lfcc->gh);
896                 kfree(lfcc);
897         }
898         gfs2_glock_dq_uninit(&ji_gh);
899
900         return error;
901 }
902
903 /**
904  * gfs2_freeze_fs - freezes the file system
905  * @sdp: the file system
906  *
907  * This function flushes data and meta data for all machines by
908  * aquiring the transaction log exclusively.  All journals are
909  * ensured to be in a clean state as well.
910  *
911  * Returns: errno
912  */
913
914 int gfs2_freeze_fs(struct gfs2_sbd *sdp)
915 {
916         int error = 0;
917
918         mutex_lock(&sdp->sd_freeze_lock);
919
920         if (!sdp->sd_freeze_count++) {
921                 error = gfs2_lock_fs_check_clean(sdp, &sdp->sd_freeze_gh);
922                 if (error)
923                         sdp->sd_freeze_count--;
924         }
925
926         mutex_unlock(&sdp->sd_freeze_lock);
927
928         return error;
929 }
930
931 /**
932  * gfs2_unfreeze_fs - unfreezes the file system
933  * @sdp: the file system
934  *
935  * This function allows the file system to proceed by unlocking
936  * the exclusively held transaction lock.  Other GFS2 nodes are
937  * now free to acquire the lock shared and go on with their lives.
938  *
939  */
940
941 void gfs2_unfreeze_fs(struct gfs2_sbd *sdp)
942 {
943         mutex_lock(&sdp->sd_freeze_lock);
944
945         if (sdp->sd_freeze_count && !--sdp->sd_freeze_count)
946                 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
947
948         mutex_unlock(&sdp->sd_freeze_lock);
949 }
950