2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
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.
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>
37 * gfs2_jindex_hold - Grab a lock on the jindex
38 * @sdp: The GFS2 superblock
39 * @ji_gh: the holder for the jindex glock
41 * This is very similar to the gfs2_rindex_hold() function, except that
42 * in general we hold the jindex lock for longer periods of time and
43 * we grab it far less frequently (in general) then the rgrp lock.
48 int gfs2_jindex_hold(struct gfs2_sbd
*sdp
, struct gfs2_holder
*ji_gh
)
50 struct gfs2_inode
*dip
= GFS2_I(sdp
->sd_jindex
);
53 struct gfs2_jdesc
*jd
;
58 mutex_lock(&sdp
->sd_jindex_mutex
);
61 error
= gfs2_glock_nq_init(dip
->i_gl
, LM_ST_SHARED
, 0, ji_gh
);
65 name
.len
= sprintf(buf
, "journal%u", sdp
->sd_journals
);
66 name
.hash
= gfs2_disk_hash(name
.name
, name
.len
);
68 error
= gfs2_dir_check(sdp
->sd_jindex
, &name
, NULL
);
69 if (error
== -ENOENT
) {
74 gfs2_glock_dq_uninit(ji_gh
);
80 jd
= kzalloc(sizeof(struct gfs2_jdesc
), GFP_KERNEL
);
84 INIT_LIST_HEAD(&jd
->extent_list
);
85 jd
->jd_inode
= gfs2_lookupi(sdp
->sd_jindex
, &name
, 1);
86 if (!jd
->jd_inode
|| IS_ERR(jd
->jd_inode
)) {
90 error
= PTR_ERR(jd
->jd_inode
);
95 spin_lock(&sdp
->sd_jindex_spin
);
96 jd
->jd_jid
= sdp
->sd_journals
++;
97 list_add_tail(&jd
->jd_list
, &sdp
->sd_jindex_list
);
98 spin_unlock(&sdp
->sd_jindex_spin
);
101 mutex_unlock(&sdp
->sd_jindex_mutex
);
107 * gfs2_jindex_free - Clear all the journal index information
108 * @sdp: The GFS2 superblock
112 void gfs2_jindex_free(struct gfs2_sbd
*sdp
)
114 struct list_head list
, *head
;
115 struct gfs2_jdesc
*jd
;
116 struct gfs2_journal_extent
*jext
;
118 spin_lock(&sdp
->sd_jindex_spin
);
119 list_add(&list
, &sdp
->sd_jindex_list
);
120 list_del_init(&sdp
->sd_jindex_list
);
121 sdp
->sd_journals
= 0;
122 spin_unlock(&sdp
->sd_jindex_spin
);
124 while (!list_empty(&list
)) {
125 jd
= list_entry(list
.next
, struct gfs2_jdesc
, jd_list
);
126 head
= &jd
->extent_list
;
127 while (!list_empty(head
)) {
128 jext
= list_entry(head
->next
,
129 struct gfs2_journal_extent
,
131 list_del(&jext
->extent_list
);
134 list_del(&jd
->jd_list
);
140 static struct gfs2_jdesc
*jdesc_find_i(struct list_head
*head
, unsigned int jid
)
142 struct gfs2_jdesc
*jd
;
145 list_for_each_entry(jd
, head
, jd_list
) {
146 if (jd
->jd_jid
== jid
) {
158 struct gfs2_jdesc
*gfs2_jdesc_find(struct gfs2_sbd
*sdp
, unsigned int jid
)
160 struct gfs2_jdesc
*jd
;
162 spin_lock(&sdp
->sd_jindex_spin
);
163 jd
= jdesc_find_i(&sdp
->sd_jindex_list
, jid
);
164 spin_unlock(&sdp
->sd_jindex_spin
);
169 void gfs2_jdesc_make_dirty(struct gfs2_sbd
*sdp
, unsigned int jid
)
171 struct gfs2_jdesc
*jd
;
173 spin_lock(&sdp
->sd_jindex_spin
);
174 jd
= jdesc_find_i(&sdp
->sd_jindex_list
, jid
);
177 spin_unlock(&sdp
->sd_jindex_spin
);
180 struct gfs2_jdesc
*gfs2_jdesc_find_dirty(struct gfs2_sbd
*sdp
)
182 struct gfs2_jdesc
*jd
;
185 spin_lock(&sdp
->sd_jindex_spin
);
187 list_for_each_entry(jd
, &sdp
->sd_jindex_list
, jd_list
) {
194 spin_unlock(&sdp
->sd_jindex_spin
);
202 int gfs2_jdesc_check(struct gfs2_jdesc
*jd
)
204 struct gfs2_inode
*ip
= GFS2_I(jd
->jd_inode
);
205 struct gfs2_sbd
*sdp
= GFS2_SB(jd
->jd_inode
);
209 if (ip
->i_di
.di_size
< (8 << 20) || ip
->i_di
.di_size
> (1 << 30) ||
210 (ip
->i_di
.di_size
& (sdp
->sd_sb
.sb_bsize
- 1))) {
211 gfs2_consist_inode(ip
);
214 jd
->jd_blocks
= ip
->i_di
.di_size
>> sdp
->sd_sb
.sb_bsize_shift
;
216 error
= gfs2_write_alloc_required(ip
, 0, ip
->i_di
.di_size
, &ar
);
218 gfs2_consist_inode(ip
);
226 * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
227 * @sdp: the filesystem
232 int gfs2_make_fs_rw(struct gfs2_sbd
*sdp
)
234 struct gfs2_inode
*ip
= GFS2_I(sdp
->sd_jdesc
->jd_inode
);
235 struct gfs2_glock
*j_gl
= ip
->i_gl
;
236 struct gfs2_holder t_gh
;
237 struct gfs2_log_header_host head
;
240 error
= gfs2_glock_nq_init(sdp
->sd_trans_gl
, LM_ST_SHARED
, 0, &t_gh
);
244 j_gl
->gl_ops
->go_inval(j_gl
, DIO_METADATA
);
246 error
= gfs2_find_jhead(sdp
->sd_jdesc
, &head
);
250 if (!(head
.lh_flags
& GFS2_LOG_HEAD_UNMOUNT
)) {
256 /* Initialize some head of the log stuff */
257 sdp
->sd_log_sequence
= head
.lh_sequence
+ 1;
258 gfs2_log_pointers_init(sdp
, head
.lh_blkno
);
260 error
= gfs2_quota_init(sdp
);
264 set_bit(SDF_JOURNAL_LIVE
, &sdp
->sd_flags
);
266 gfs2_glock_dq_uninit(&t_gh
);
271 t_gh
.gh_flags
|= GL_NOCACHE
;
272 gfs2_glock_dq_uninit(&t_gh
);
277 static void gfs2_statfs_change_in(struct gfs2_statfs_change_host
*sc
, const void *buf
)
279 const struct gfs2_statfs_change
*str
= buf
;
281 sc
->sc_total
= be64_to_cpu(str
->sc_total
);
282 sc
->sc_free
= be64_to_cpu(str
->sc_free
);
283 sc
->sc_dinodes
= be64_to_cpu(str
->sc_dinodes
);
286 static void gfs2_statfs_change_out(const struct gfs2_statfs_change_host
*sc
, void *buf
)
288 struct gfs2_statfs_change
*str
= buf
;
290 str
->sc_total
= cpu_to_be64(sc
->sc_total
);
291 str
->sc_free
= cpu_to_be64(sc
->sc_free
);
292 str
->sc_dinodes
= cpu_to_be64(sc
->sc_dinodes
);
295 int gfs2_statfs_init(struct gfs2_sbd
*sdp
)
297 struct gfs2_inode
*m_ip
= GFS2_I(sdp
->sd_statfs_inode
);
298 struct gfs2_statfs_change_host
*m_sc
= &sdp
->sd_statfs_master
;
299 struct gfs2_inode
*l_ip
= GFS2_I(sdp
->sd_sc_inode
);
300 struct gfs2_statfs_change_host
*l_sc
= &sdp
->sd_statfs_local
;
301 struct buffer_head
*m_bh
, *l_bh
;
302 struct gfs2_holder gh
;
305 error
= gfs2_glock_nq_init(m_ip
->i_gl
, LM_ST_EXCLUSIVE
, GL_NOCACHE
,
310 error
= gfs2_meta_inode_buffer(m_ip
, &m_bh
);
314 if (sdp
->sd_args
.ar_spectator
) {
315 spin_lock(&sdp
->sd_statfs_spin
);
316 gfs2_statfs_change_in(m_sc
, m_bh
->b_data
+
317 sizeof(struct gfs2_dinode
));
318 spin_unlock(&sdp
->sd_statfs_spin
);
320 error
= gfs2_meta_inode_buffer(l_ip
, &l_bh
);
324 spin_lock(&sdp
->sd_statfs_spin
);
325 gfs2_statfs_change_in(m_sc
, m_bh
->b_data
+
326 sizeof(struct gfs2_dinode
));
327 gfs2_statfs_change_in(l_sc
, l_bh
->b_data
+
328 sizeof(struct gfs2_dinode
));
329 spin_unlock(&sdp
->sd_statfs_spin
);
337 gfs2_glock_dq_uninit(&gh
);
341 void gfs2_statfs_change(struct gfs2_sbd
*sdp
, s64 total
, s64 free
,
344 struct gfs2_inode
*l_ip
= GFS2_I(sdp
->sd_sc_inode
);
345 struct gfs2_statfs_change_host
*l_sc
= &sdp
->sd_statfs_local
;
346 struct buffer_head
*l_bh
;
349 error
= gfs2_meta_inode_buffer(l_ip
, &l_bh
);
353 gfs2_trans_add_bh(l_ip
->i_gl
, l_bh
, 1);
355 spin_lock(&sdp
->sd_statfs_spin
);
356 l_sc
->sc_total
+= total
;
357 l_sc
->sc_free
+= free
;
358 l_sc
->sc_dinodes
+= dinodes
;
359 gfs2_statfs_change_out(l_sc
, l_bh
->b_data
+ sizeof(struct gfs2_dinode
));
360 spin_unlock(&sdp
->sd_statfs_spin
);
365 int gfs2_statfs_sync(struct gfs2_sbd
*sdp
)
367 struct gfs2_inode
*m_ip
= GFS2_I(sdp
->sd_statfs_inode
);
368 struct gfs2_inode
*l_ip
= GFS2_I(sdp
->sd_sc_inode
);
369 struct gfs2_statfs_change_host
*m_sc
= &sdp
->sd_statfs_master
;
370 struct gfs2_statfs_change_host
*l_sc
= &sdp
->sd_statfs_local
;
371 struct gfs2_holder gh
;
372 struct buffer_head
*m_bh
, *l_bh
;
375 error
= gfs2_glock_nq_init(m_ip
->i_gl
, LM_ST_EXCLUSIVE
, GL_NOCACHE
,
380 error
= gfs2_meta_inode_buffer(m_ip
, &m_bh
);
384 spin_lock(&sdp
->sd_statfs_spin
);
385 gfs2_statfs_change_in(m_sc
, m_bh
->b_data
+
386 sizeof(struct gfs2_dinode
));
387 if (!l_sc
->sc_total
&& !l_sc
->sc_free
&& !l_sc
->sc_dinodes
) {
388 spin_unlock(&sdp
->sd_statfs_spin
);
391 spin_unlock(&sdp
->sd_statfs_spin
);
393 error
= gfs2_meta_inode_buffer(l_ip
, &l_bh
);
397 error
= gfs2_trans_begin(sdp
, 2 * RES_DINODE
, 0);
401 gfs2_trans_add_bh(l_ip
->i_gl
, l_bh
, 1);
403 spin_lock(&sdp
->sd_statfs_spin
);
404 m_sc
->sc_total
+= l_sc
->sc_total
;
405 m_sc
->sc_free
+= l_sc
->sc_free
;
406 m_sc
->sc_dinodes
+= l_sc
->sc_dinodes
;
407 memset(l_sc
, 0, sizeof(struct gfs2_statfs_change
));
408 memset(l_bh
->b_data
+ sizeof(struct gfs2_dinode
),
409 0, sizeof(struct gfs2_statfs_change
));
410 spin_unlock(&sdp
->sd_statfs_spin
);
412 gfs2_trans_add_bh(m_ip
->i_gl
, m_bh
, 1);
413 gfs2_statfs_change_out(m_sc
, m_bh
->b_data
+ sizeof(struct gfs2_dinode
));
422 gfs2_glock_dq_uninit(&gh
);
427 * gfs2_statfs_i - Do a statfs
428 * @sdp: the filesystem
429 * @sg: the sg structure
434 int gfs2_statfs_i(struct gfs2_sbd
*sdp
, struct gfs2_statfs_change_host
*sc
)
436 struct gfs2_statfs_change_host
*m_sc
= &sdp
->sd_statfs_master
;
437 struct gfs2_statfs_change_host
*l_sc
= &sdp
->sd_statfs_local
;
439 spin_lock(&sdp
->sd_statfs_spin
);
442 sc
->sc_total
+= l_sc
->sc_total
;
443 sc
->sc_free
+= l_sc
->sc_free
;
444 sc
->sc_dinodes
+= l_sc
->sc_dinodes
;
446 spin_unlock(&sdp
->sd_statfs_spin
);
450 if (sc
->sc_free
> sc
->sc_total
)
451 sc
->sc_free
= sc
->sc_total
;
452 if (sc
->sc_dinodes
< 0)
459 * statfs_fill - fill in the sg for a given RG
461 * @sc: the sc structure
463 * Returns: 0 on success, -ESTALE if the LVB is invalid
466 static int statfs_slow_fill(struct gfs2_rgrpd
*rgd
,
467 struct gfs2_statfs_change_host
*sc
)
469 gfs2_rgrp_verify(rgd
);
470 sc
->sc_total
+= rgd
->rd_data
;
471 sc
->sc_free
+= rgd
->rd_rg
.rg_free
;
472 sc
->sc_dinodes
+= rgd
->rd_rg
.rg_dinodes
;
477 * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
478 * @sdp: the filesystem
479 * @sc: the sc info that will be returned
481 * Any error (other than a signal) will cause this routine to fall back
482 * to the synchronous version.
484 * FIXME: This really shouldn't busy wait like this.
489 int gfs2_statfs_slow(struct gfs2_sbd
*sdp
, struct gfs2_statfs_change_host
*sc
)
491 struct gfs2_holder ri_gh
;
492 struct gfs2_rgrpd
*rgd_next
;
493 struct gfs2_holder
*gha
, *gh
;
494 unsigned int slots
= 64;
499 memset(sc
, 0, sizeof(struct gfs2_statfs_change_host
));
500 gha
= kcalloc(slots
, sizeof(struct gfs2_holder
), GFP_KERNEL
);
504 error
= gfs2_rindex_hold(sdp
, &ri_gh
);
508 rgd_next
= gfs2_rgrpd_get_first(sdp
);
513 for (x
= 0; x
< slots
; x
++) {
516 if (gh
->gh_gl
&& gfs2_glock_poll(gh
)) {
517 err
= gfs2_glock_wait(gh
);
519 gfs2_holder_uninit(gh
);
523 error
= statfs_slow_fill(
524 gh
->gh_gl
->gl_object
, sc
);
525 gfs2_glock_dq_uninit(gh
);
531 else if (rgd_next
&& !error
) {
532 error
= gfs2_glock_nq_init(rgd_next
->rd_gl
,
536 rgd_next
= gfs2_rgrpd_get_next(rgd_next
);
540 if (signal_pending(current
))
541 error
= -ERESTARTSYS
;
550 gfs2_glock_dq_uninit(&ri_gh
);
558 struct list_head list
;
559 struct gfs2_holder gh
;
563 * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
565 * @sdp: the file system
566 * @state: the state to put the transaction lock into
567 * @t_gh: the hold on the transaction lock
572 static int gfs2_lock_fs_check_clean(struct gfs2_sbd
*sdp
,
573 struct gfs2_holder
*t_gh
)
575 struct gfs2_inode
*ip
;
576 struct gfs2_holder ji_gh
;
577 struct gfs2_jdesc
*jd
;
580 struct gfs2_log_header_host lh
;
583 error
= gfs2_jindex_hold(sdp
, &ji_gh
);
587 list_for_each_entry(jd
, &sdp
->sd_jindex_list
, jd_list
) {
588 lfcc
= kmalloc(sizeof(struct lfcc
), GFP_KERNEL
);
593 ip
= GFS2_I(jd
->jd_inode
);
594 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, 0, &lfcc
->gh
);
599 list_add(&lfcc
->list
, &list
);
602 error
= gfs2_glock_nq_init(sdp
->sd_trans_gl
, LM_ST_DEFERRED
,
605 list_for_each_entry(jd
, &sdp
->sd_jindex_list
, jd_list
) {
606 error
= gfs2_jdesc_check(jd
);
609 error
= gfs2_find_jhead(jd
, &lh
);
612 if (!(lh
.lh_flags
& GFS2_LOG_HEAD_UNMOUNT
)) {
619 gfs2_glock_dq_uninit(t_gh
);
622 while (!list_empty(&list
)) {
623 lfcc
= list_entry(list
.next
, struct lfcc
, list
);
624 list_del(&lfcc
->list
);
625 gfs2_glock_dq_uninit(&lfcc
->gh
);
628 gfs2_glock_dq_uninit(&ji_gh
);
633 * gfs2_freeze_fs - freezes the file system
634 * @sdp: the file system
636 * This function flushes data and meta data for all machines by
637 * aquiring the transaction log exclusively. All journals are
638 * ensured to be in a clean state as well.
643 int gfs2_freeze_fs(struct gfs2_sbd
*sdp
)
647 mutex_lock(&sdp
->sd_freeze_lock
);
649 if (!sdp
->sd_freeze_count
++) {
650 error
= gfs2_lock_fs_check_clean(sdp
, &sdp
->sd_freeze_gh
);
652 sdp
->sd_freeze_count
--;
655 mutex_unlock(&sdp
->sd_freeze_lock
);
661 * gfs2_unfreeze_fs - unfreezes the file system
662 * @sdp: the file system
664 * This function allows the file system to proceed by unlocking
665 * the exclusively held transaction lock. Other GFS2 nodes are
666 * now free to acquire the lock shared and go on with their lives.
670 void gfs2_unfreeze_fs(struct gfs2_sbd
*sdp
)
672 mutex_lock(&sdp
->sd_freeze_lock
);
674 if (sdp
->sd_freeze_count
&& !--sdp
->sd_freeze_count
)
675 gfs2_glock_dq_uninit(&sdp
->sd_freeze_gh
);
677 mutex_unlock(&sdp
->sd_freeze_lock
);