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