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