HID: fix tty<->hid deadlock
[linux-2.6/mini2440.git] / fs / gfs2 / super.c
blobc3ba3d9d0aac3ca4fa94f906691247e4816c6fbc
1 /*
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.
8 */
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>
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"
36 /**
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.
45 * Returns: errno
48 int gfs2_jindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ji_gh)
50 struct gfs2_inode *dip = GFS2_I(sdp->sd_jindex);
51 struct qstr name;
52 char buf[20];
53 struct gfs2_jdesc *jd;
54 int error;
56 name.name = buf;
58 mutex_lock(&sdp->sd_jindex_mutex);
60 for (;;) {
61 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, ji_gh);
62 if (error)
63 break;
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) {
70 error = 0;
71 break;
74 gfs2_glock_dq_uninit(ji_gh);
76 if (error)
77 break;
79 error = -ENOMEM;
80 jd = kzalloc(sizeof(struct gfs2_jdesc), GFP_KERNEL);
81 if (!jd)
82 break;
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)) {
87 if (!jd->jd_inode)
88 error = -ENOENT;
89 else
90 error = PTR_ERR(jd->jd_inode);
91 kfree(jd);
92 break;
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);
103 return error;
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,
130 extent_list);
131 list_del(&jext->extent_list);
132 kfree(jext);
134 list_del(&jd->jd_list);
135 iput(jd->jd_inode);
136 kfree(jd);
140 static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
142 struct gfs2_jdesc *jd;
143 int found = 0;
145 list_for_each_entry(jd, head, jd_list) {
146 if (jd->jd_jid == jid) {
147 found = 1;
148 break;
152 if (!found)
153 jd = NULL;
155 return jd;
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);
166 return jd;
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);
175 if (jd)
176 jd->jd_dirty = 1;
177 spin_unlock(&sdp->sd_jindex_spin);
180 struct gfs2_jdesc *gfs2_jdesc_find_dirty(struct gfs2_sbd *sdp)
182 struct gfs2_jdesc *jd;
183 int found = 0;
185 spin_lock(&sdp->sd_jindex_spin);
187 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
188 if (jd->jd_dirty) {
189 jd->jd_dirty = 0;
190 found = 1;
191 break;
194 spin_unlock(&sdp->sd_jindex_spin);
196 if (!found)
197 jd = NULL;
199 return jd;
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);
206 int ar;
207 int error;
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);
212 return -EIO;
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);
217 if (!error && ar) {
218 gfs2_consist_inode(ip);
219 error = -EIO;
222 return error;
226 * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
227 * @sdp: the filesystem
229 * Returns: errno
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;
238 int error;
240 error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, 0, &t_gh);
241 if (error)
242 return error;
244 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
246 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
247 if (error)
248 goto fail;
250 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
251 gfs2_consist(sdp);
252 error = -EIO;
253 goto fail;
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);
261 if (error)
262 goto fail;
264 set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
266 gfs2_glock_dq_uninit(&t_gh);
268 return 0;
270 fail:
271 t_gh.gh_flags |= GL_NOCACHE;
272 gfs2_glock_dq_uninit(&t_gh);
274 return error;
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;
303 int error;
305 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
306 &gh);
307 if (error)
308 return error;
310 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
311 if (error)
312 goto out;
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);
319 } else {
320 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
321 if (error)
322 goto out_m_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);
331 brelse(l_bh);
334 out_m_bh:
335 brelse(m_bh);
336 out:
337 gfs2_glock_dq_uninit(&gh);
338 return 0;
341 void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
342 s64 dinodes)
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;
347 int error;
349 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
350 if (error)
351 return;
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);
362 brelse(l_bh);
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;
373 int error;
375 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
376 &gh);
377 if (error)
378 return error;
380 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
381 if (error)
382 goto out;
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);
389 goto out_bh;
391 spin_unlock(&sdp->sd_statfs_spin);
393 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
394 if (error)
395 goto out_bh;
397 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
398 if (error)
399 goto out_bh2;
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));
415 gfs2_trans_end(sdp);
417 out_bh2:
418 brelse(l_bh);
419 out_bh:
420 brelse(m_bh);
421 out:
422 gfs2_glock_dq_uninit(&gh);
423 return error;
427 * gfs2_statfs_i - Do a statfs
428 * @sdp: the filesystem
429 * @sg: the sg structure
431 * Returns: errno
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);
441 *sc = *m_sc;
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);
448 if (sc->sc_free < 0)
449 sc->sc_free = 0;
450 if (sc->sc_free > sc->sc_total)
451 sc->sc_free = sc->sc_total;
452 if (sc->sc_dinodes < 0)
453 sc->sc_dinodes = 0;
455 return 0;
459 * statfs_fill - fill in the sg for a given RG
460 * @rgd: the 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;
473 return 0;
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.
486 * Returns: errno
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;
495 unsigned int x;
496 int done;
497 int error = 0, err;
499 memset(sc, 0, sizeof(struct gfs2_statfs_change_host));
500 gha = kcalloc(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
501 if (!gha)
502 return -ENOMEM;
504 error = gfs2_rindex_hold(sdp, &ri_gh);
505 if (error)
506 goto out;
508 rgd_next = gfs2_rgrpd_get_first(sdp);
510 for (;;) {
511 done = 1;
513 for (x = 0; x < slots; x++) {
514 gh = gha + x;
516 if (gh->gh_gl && gfs2_glock_poll(gh)) {
517 err = gfs2_glock_wait(gh);
518 if (err) {
519 gfs2_holder_uninit(gh);
520 error = err;
521 } else {
522 if (!error)
523 error = statfs_slow_fill(
524 gh->gh_gl->gl_object, sc);
525 gfs2_glock_dq_uninit(gh);
529 if (gh->gh_gl)
530 done = 0;
531 else if (rgd_next && !error) {
532 error = gfs2_glock_nq_init(rgd_next->rd_gl,
533 LM_ST_SHARED,
534 GL_ASYNC,
535 gh);
536 rgd_next = gfs2_rgrpd_get_next(rgd_next);
537 done = 0;
540 if (signal_pending(current))
541 error = -ERESTARTSYS;
544 if (done)
545 break;
547 yield();
550 gfs2_glock_dq_uninit(&ri_gh);
552 out:
553 kfree(gha);
554 return error;
557 struct lfcc {
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
564 * journals are clean
565 * @sdp: the file system
566 * @state: the state to put the transaction lock into
567 * @t_gh: the hold on the transaction lock
569 * Returns: errno
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;
578 struct lfcc *lfcc;
579 LIST_HEAD(list);
580 struct gfs2_log_header_host lh;
581 int error;
583 error = gfs2_jindex_hold(sdp, &ji_gh);
584 if (error)
585 return error;
587 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
588 lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
589 if (!lfcc) {
590 error = -ENOMEM;
591 goto out;
593 ip = GFS2_I(jd->jd_inode);
594 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
595 if (error) {
596 kfree(lfcc);
597 goto out;
599 list_add(&lfcc->list, &list);
602 error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_DEFERRED,
603 GL_NOCACHE, t_gh);
605 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
606 error = gfs2_jdesc_check(jd);
607 if (error)
608 break;
609 error = gfs2_find_jhead(jd, &lh);
610 if (error)
611 break;
612 if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
613 error = -EBUSY;
614 break;
618 if (error)
619 gfs2_glock_dq_uninit(t_gh);
621 out:
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);
626 kfree(lfcc);
628 gfs2_glock_dq_uninit(&ji_gh);
629 return error;
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.
640 * Returns: errno
643 int gfs2_freeze_fs(struct gfs2_sbd *sdp)
645 int error = 0;
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);
651 if (error)
652 sdp->sd_freeze_count--;
655 mutex_unlock(&sdp->sd_freeze_lock);
657 return error;
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);