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