intel-iommu: Introduce domain_sg_mapping() to speed up intel_map_sg()
[linux/fpc-iii.git] / fs / gfs2 / super.c
blob0a68013364708dbcafe39398952defaa9f560450
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/bio.h>
11 #include <linux/sched.h>
12 #include <linux/slab.h>
13 #include <linux/spinlock.h>
14 #include <linux/completion.h>
15 #include <linux/buffer_head.h>
16 #include <linux/statfs.h>
17 #include <linux/seq_file.h>
18 #include <linux/mount.h>
19 #include <linux/kthread.h>
20 #include <linux/delay.h>
21 #include <linux/gfs2_ondisk.h>
22 #include <linux/crc32.h>
23 #include <linux/time.h>
25 #include "gfs2.h"
26 #include "incore.h"
27 #include "bmap.h"
28 #include "dir.h"
29 #include "glock.h"
30 #include "glops.h"
31 #include "inode.h"
32 #include "log.h"
33 #include "meta_io.h"
34 #include "quota.h"
35 #include "recovery.h"
36 #include "rgrp.h"
37 #include "super.h"
38 #include "trans.h"
39 #include "util.h"
40 #include "sys.h"
41 #include "eattr.h"
43 #define args_neq(a1, a2, x) ((a1)->ar_##x != (a2)->ar_##x)
45 enum {
46 Opt_lockproto,
47 Opt_locktable,
48 Opt_hostdata,
49 Opt_spectator,
50 Opt_ignore_local_fs,
51 Opt_localflocks,
52 Opt_localcaching,
53 Opt_debug,
54 Opt_nodebug,
55 Opt_upgrade,
56 Opt_acl,
57 Opt_noacl,
58 Opt_quota_off,
59 Opt_quota_account,
60 Opt_quota_on,
61 Opt_quota,
62 Opt_noquota,
63 Opt_suiddir,
64 Opt_nosuiddir,
65 Opt_data_writeback,
66 Opt_data_ordered,
67 Opt_meta,
68 Opt_discard,
69 Opt_nodiscard,
70 Opt_commit,
71 Opt_error,
74 static const match_table_t tokens = {
75 {Opt_lockproto, "lockproto=%s"},
76 {Opt_locktable, "locktable=%s"},
77 {Opt_hostdata, "hostdata=%s"},
78 {Opt_spectator, "spectator"},
79 {Opt_ignore_local_fs, "ignore_local_fs"},
80 {Opt_localflocks, "localflocks"},
81 {Opt_localcaching, "localcaching"},
82 {Opt_debug, "debug"},
83 {Opt_nodebug, "nodebug"},
84 {Opt_upgrade, "upgrade"},
85 {Opt_acl, "acl"},
86 {Opt_noacl, "noacl"},
87 {Opt_quota_off, "quota=off"},
88 {Opt_quota_account, "quota=account"},
89 {Opt_quota_on, "quota=on"},
90 {Opt_quota, "quota"},
91 {Opt_noquota, "noquota"},
92 {Opt_suiddir, "suiddir"},
93 {Opt_nosuiddir, "nosuiddir"},
94 {Opt_data_writeback, "data=writeback"},
95 {Opt_data_ordered, "data=ordered"},
96 {Opt_meta, "meta"},
97 {Opt_discard, "discard"},
98 {Opt_nodiscard, "nodiscard"},
99 {Opt_commit, "commit=%d"},
100 {Opt_error, NULL}
104 * gfs2_mount_args - Parse mount options
105 * @sdp:
106 * @data:
108 * Return: errno
111 int gfs2_mount_args(struct gfs2_sbd *sdp, struct gfs2_args *args, char *options)
113 char *o;
114 int token;
115 substring_t tmp[MAX_OPT_ARGS];
116 int rv;
118 /* Split the options into tokens with the "," character and
119 process them */
121 while (1) {
122 o = strsep(&options, ",");
123 if (o == NULL)
124 break;
125 if (*o == '\0')
126 continue;
128 token = match_token(o, tokens, tmp);
129 switch (token) {
130 case Opt_lockproto:
131 match_strlcpy(args->ar_lockproto, &tmp[0],
132 GFS2_LOCKNAME_LEN);
133 break;
134 case Opt_locktable:
135 match_strlcpy(args->ar_locktable, &tmp[0],
136 GFS2_LOCKNAME_LEN);
137 break;
138 case Opt_hostdata:
139 match_strlcpy(args->ar_hostdata, &tmp[0],
140 GFS2_LOCKNAME_LEN);
141 break;
142 case Opt_spectator:
143 args->ar_spectator = 1;
144 break;
145 case Opt_ignore_local_fs:
146 args->ar_ignore_local_fs = 1;
147 break;
148 case Opt_localflocks:
149 args->ar_localflocks = 1;
150 break;
151 case Opt_localcaching:
152 args->ar_localcaching = 1;
153 break;
154 case Opt_debug:
155 args->ar_debug = 1;
156 break;
157 case Opt_nodebug:
158 args->ar_debug = 0;
159 break;
160 case Opt_upgrade:
161 args->ar_upgrade = 1;
162 break;
163 case Opt_acl:
164 args->ar_posix_acl = 1;
165 break;
166 case Opt_noacl:
167 args->ar_posix_acl = 0;
168 break;
169 case Opt_quota_off:
170 case Opt_noquota:
171 args->ar_quota = GFS2_QUOTA_OFF;
172 break;
173 case Opt_quota_account:
174 args->ar_quota = GFS2_QUOTA_ACCOUNT;
175 break;
176 case Opt_quota_on:
177 case Opt_quota:
178 args->ar_quota = GFS2_QUOTA_ON;
179 break;
180 case Opt_suiddir:
181 args->ar_suiddir = 1;
182 break;
183 case Opt_nosuiddir:
184 args->ar_suiddir = 0;
185 break;
186 case Opt_data_writeback:
187 args->ar_data = GFS2_DATA_WRITEBACK;
188 break;
189 case Opt_data_ordered:
190 args->ar_data = GFS2_DATA_ORDERED;
191 break;
192 case Opt_meta:
193 args->ar_meta = 1;
194 break;
195 case Opt_discard:
196 args->ar_discard = 1;
197 break;
198 case Opt_nodiscard:
199 args->ar_discard = 0;
200 break;
201 case Opt_commit:
202 rv = match_int(&tmp[0], &args->ar_commit);
203 if (rv || args->ar_commit <= 0) {
204 fs_info(sdp, "commit mount option requires a positive numeric argument\n");
205 return rv ? rv : -EINVAL;
207 break;
208 case Opt_error:
209 default:
210 fs_info(sdp, "invalid mount option: %s\n", o);
211 return -EINVAL;
215 return 0;
219 * gfs2_jindex_free - Clear all the journal index information
220 * @sdp: The GFS2 superblock
224 void gfs2_jindex_free(struct gfs2_sbd *sdp)
226 struct list_head list, *head;
227 struct gfs2_jdesc *jd;
228 struct gfs2_journal_extent *jext;
230 spin_lock(&sdp->sd_jindex_spin);
231 list_add(&list, &sdp->sd_jindex_list);
232 list_del_init(&sdp->sd_jindex_list);
233 sdp->sd_journals = 0;
234 spin_unlock(&sdp->sd_jindex_spin);
236 while (!list_empty(&list)) {
237 jd = list_entry(list.next, struct gfs2_jdesc, jd_list);
238 head = &jd->extent_list;
239 while (!list_empty(head)) {
240 jext = list_entry(head->next,
241 struct gfs2_journal_extent,
242 extent_list);
243 list_del(&jext->extent_list);
244 kfree(jext);
246 list_del(&jd->jd_list);
247 iput(jd->jd_inode);
248 kfree(jd);
252 static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
254 struct gfs2_jdesc *jd;
255 int found = 0;
257 list_for_each_entry(jd, head, jd_list) {
258 if (jd->jd_jid == jid) {
259 found = 1;
260 break;
264 if (!found)
265 jd = NULL;
267 return jd;
270 struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
272 struct gfs2_jdesc *jd;
274 spin_lock(&sdp->sd_jindex_spin);
275 jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
276 spin_unlock(&sdp->sd_jindex_spin);
278 return jd;
281 int gfs2_jdesc_check(struct gfs2_jdesc *jd)
283 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
284 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
285 int ar;
286 int error;
288 if (ip->i_disksize < (8 << 20) || ip->i_disksize > (1 << 30) ||
289 (ip->i_disksize & (sdp->sd_sb.sb_bsize - 1))) {
290 gfs2_consist_inode(ip);
291 return -EIO;
293 jd->jd_blocks = ip->i_disksize >> sdp->sd_sb.sb_bsize_shift;
295 error = gfs2_write_alloc_required(ip, 0, ip->i_disksize, &ar);
296 if (!error && ar) {
297 gfs2_consist_inode(ip);
298 error = -EIO;
301 return error;
305 * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
306 * @sdp: the filesystem
308 * Returns: errno
311 int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
313 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
314 struct gfs2_glock *j_gl = ip->i_gl;
315 struct gfs2_holder t_gh;
316 struct gfs2_log_header_host head;
317 int error;
319 error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, 0, &t_gh);
320 if (error)
321 return error;
323 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
325 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
326 if (error)
327 goto fail;
329 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
330 gfs2_consist(sdp);
331 error = -EIO;
332 goto fail;
335 /* Initialize some head of the log stuff */
336 sdp->sd_log_sequence = head.lh_sequence + 1;
337 gfs2_log_pointers_init(sdp, head.lh_blkno);
339 error = gfs2_quota_init(sdp);
340 if (error)
341 goto fail;
343 set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
345 gfs2_glock_dq_uninit(&t_gh);
347 return 0;
349 fail:
350 t_gh.gh_flags |= GL_NOCACHE;
351 gfs2_glock_dq_uninit(&t_gh);
353 return error;
356 static void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
358 const struct gfs2_statfs_change *str = buf;
360 sc->sc_total = be64_to_cpu(str->sc_total);
361 sc->sc_free = be64_to_cpu(str->sc_free);
362 sc->sc_dinodes = be64_to_cpu(str->sc_dinodes);
365 static void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
367 struct gfs2_statfs_change *str = buf;
369 str->sc_total = cpu_to_be64(sc->sc_total);
370 str->sc_free = cpu_to_be64(sc->sc_free);
371 str->sc_dinodes = cpu_to_be64(sc->sc_dinodes);
374 int gfs2_statfs_init(struct gfs2_sbd *sdp)
376 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
377 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
378 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
379 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
380 struct buffer_head *m_bh, *l_bh;
381 struct gfs2_holder gh;
382 int error;
384 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
385 &gh);
386 if (error)
387 return error;
389 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
390 if (error)
391 goto out;
393 if (sdp->sd_args.ar_spectator) {
394 spin_lock(&sdp->sd_statfs_spin);
395 gfs2_statfs_change_in(m_sc, m_bh->b_data +
396 sizeof(struct gfs2_dinode));
397 spin_unlock(&sdp->sd_statfs_spin);
398 } else {
399 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
400 if (error)
401 goto out_m_bh;
403 spin_lock(&sdp->sd_statfs_spin);
404 gfs2_statfs_change_in(m_sc, m_bh->b_data +
405 sizeof(struct gfs2_dinode));
406 gfs2_statfs_change_in(l_sc, l_bh->b_data +
407 sizeof(struct gfs2_dinode));
408 spin_unlock(&sdp->sd_statfs_spin);
410 brelse(l_bh);
413 out_m_bh:
414 brelse(m_bh);
415 out:
416 gfs2_glock_dq_uninit(&gh);
417 return 0;
420 void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
421 s64 dinodes)
423 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
424 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
425 struct buffer_head *l_bh;
426 int error;
428 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
429 if (error)
430 return;
432 gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
434 spin_lock(&sdp->sd_statfs_spin);
435 l_sc->sc_total += total;
436 l_sc->sc_free += free;
437 l_sc->sc_dinodes += dinodes;
438 gfs2_statfs_change_out(l_sc, l_bh->b_data + sizeof(struct gfs2_dinode));
439 spin_unlock(&sdp->sd_statfs_spin);
441 brelse(l_bh);
444 int gfs2_statfs_sync(struct gfs2_sbd *sdp)
446 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
447 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
448 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
449 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
450 struct gfs2_holder gh;
451 struct buffer_head *m_bh, *l_bh;
452 int error;
454 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
455 &gh);
456 if (error)
457 return error;
459 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
460 if (error)
461 goto out;
463 spin_lock(&sdp->sd_statfs_spin);
464 gfs2_statfs_change_in(m_sc, m_bh->b_data +
465 sizeof(struct gfs2_dinode));
466 if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
467 spin_unlock(&sdp->sd_statfs_spin);
468 goto out_bh;
470 spin_unlock(&sdp->sd_statfs_spin);
472 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
473 if (error)
474 goto out_bh;
476 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
477 if (error)
478 goto out_bh2;
480 gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
482 spin_lock(&sdp->sd_statfs_spin);
483 m_sc->sc_total += l_sc->sc_total;
484 m_sc->sc_free += l_sc->sc_free;
485 m_sc->sc_dinodes += l_sc->sc_dinodes;
486 memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
487 memset(l_bh->b_data + sizeof(struct gfs2_dinode),
488 0, sizeof(struct gfs2_statfs_change));
489 spin_unlock(&sdp->sd_statfs_spin);
491 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
492 gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
494 gfs2_trans_end(sdp);
496 out_bh2:
497 brelse(l_bh);
498 out_bh:
499 brelse(m_bh);
500 out:
501 gfs2_glock_dq_uninit(&gh);
502 return error;
505 struct lfcc {
506 struct list_head list;
507 struct gfs2_holder gh;
511 * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
512 * journals are clean
513 * @sdp: the file system
514 * @state: the state to put the transaction lock into
515 * @t_gh: the hold on the transaction lock
517 * Returns: errno
520 static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp,
521 struct gfs2_holder *t_gh)
523 struct gfs2_inode *ip;
524 struct gfs2_jdesc *jd;
525 struct lfcc *lfcc;
526 LIST_HEAD(list);
527 struct gfs2_log_header_host lh;
528 int error;
530 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
531 lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
532 if (!lfcc) {
533 error = -ENOMEM;
534 goto out;
536 ip = GFS2_I(jd->jd_inode);
537 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
538 if (error) {
539 kfree(lfcc);
540 goto out;
542 list_add(&lfcc->list, &list);
545 error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_DEFERRED,
546 GL_NOCACHE, t_gh);
548 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
549 error = gfs2_jdesc_check(jd);
550 if (error)
551 break;
552 error = gfs2_find_jhead(jd, &lh);
553 if (error)
554 break;
555 if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
556 error = -EBUSY;
557 break;
561 if (error)
562 gfs2_glock_dq_uninit(t_gh);
564 out:
565 while (!list_empty(&list)) {
566 lfcc = list_entry(list.next, struct lfcc, list);
567 list_del(&lfcc->list);
568 gfs2_glock_dq_uninit(&lfcc->gh);
569 kfree(lfcc);
571 return error;
575 * gfs2_freeze_fs - freezes the file system
576 * @sdp: the file system
578 * This function flushes data and meta data for all machines by
579 * aquiring the transaction log exclusively. All journals are
580 * ensured to be in a clean state as well.
582 * Returns: errno
585 int gfs2_freeze_fs(struct gfs2_sbd *sdp)
587 int error = 0;
589 mutex_lock(&sdp->sd_freeze_lock);
591 if (!sdp->sd_freeze_count++) {
592 error = gfs2_lock_fs_check_clean(sdp, &sdp->sd_freeze_gh);
593 if (error)
594 sdp->sd_freeze_count--;
597 mutex_unlock(&sdp->sd_freeze_lock);
599 return error;
603 * gfs2_unfreeze_fs - unfreezes the file system
604 * @sdp: the file system
606 * This function allows the file system to proceed by unlocking
607 * the exclusively held transaction lock. Other GFS2 nodes are
608 * now free to acquire the lock shared and go on with their lives.
612 void gfs2_unfreeze_fs(struct gfs2_sbd *sdp)
614 mutex_lock(&sdp->sd_freeze_lock);
616 if (sdp->sd_freeze_count && !--sdp->sd_freeze_count)
617 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
619 mutex_unlock(&sdp->sd_freeze_lock);
624 * gfs2_write_inode - Make sure the inode is stable on the disk
625 * @inode: The inode
626 * @sync: synchronous write flag
628 * Returns: errno
631 static int gfs2_write_inode(struct inode *inode, int sync)
633 struct gfs2_inode *ip = GFS2_I(inode);
634 struct gfs2_sbd *sdp = GFS2_SB(inode);
635 struct gfs2_holder gh;
636 struct buffer_head *bh;
637 struct timespec atime;
638 struct gfs2_dinode *di;
639 int ret = 0;
641 /* Check this is a "normal" inode, etc */
642 if (!test_bit(GIF_USER, &ip->i_flags) ||
643 (current->flags & PF_MEMALLOC))
644 return 0;
645 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
646 if (ret)
647 goto do_flush;
648 ret = gfs2_trans_begin(sdp, RES_DINODE, 0);
649 if (ret)
650 goto do_unlock;
651 ret = gfs2_meta_inode_buffer(ip, &bh);
652 if (ret == 0) {
653 di = (struct gfs2_dinode *)bh->b_data;
654 atime.tv_sec = be64_to_cpu(di->di_atime);
655 atime.tv_nsec = be32_to_cpu(di->di_atime_nsec);
656 if (timespec_compare(&inode->i_atime, &atime) > 0) {
657 gfs2_trans_add_bh(ip->i_gl, bh, 1);
658 gfs2_dinode_out(ip, bh->b_data);
660 brelse(bh);
662 gfs2_trans_end(sdp);
663 do_unlock:
664 gfs2_glock_dq_uninit(&gh);
665 do_flush:
666 if (sync != 0)
667 gfs2_log_flush(GFS2_SB(inode), ip->i_gl);
668 return ret;
672 * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
673 * @sdp: the filesystem
675 * Returns: errno
678 static int gfs2_make_fs_ro(struct gfs2_sbd *sdp)
680 struct gfs2_holder t_gh;
681 int error;
683 gfs2_quota_sync(sdp);
684 gfs2_statfs_sync(sdp);
686 error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, GL_NOCACHE,
687 &t_gh);
688 if (error && !test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
689 return error;
691 gfs2_meta_syncfs(sdp);
692 gfs2_log_shutdown(sdp);
694 clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
696 if (t_gh.gh_gl)
697 gfs2_glock_dq_uninit(&t_gh);
699 gfs2_quota_cleanup(sdp);
701 return error;
704 static int gfs2_umount_recovery_wait(void *word)
706 schedule();
707 return 0;
711 * gfs2_put_super - Unmount the filesystem
712 * @sb: The VFS superblock
716 static void gfs2_put_super(struct super_block *sb)
718 struct gfs2_sbd *sdp = sb->s_fs_info;
719 int error;
720 struct gfs2_jdesc *jd;
722 /* Unfreeze the filesystem, if we need to */
724 mutex_lock(&sdp->sd_freeze_lock);
725 if (sdp->sd_freeze_count)
726 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
727 mutex_unlock(&sdp->sd_freeze_lock);
729 /* No more recovery requests */
730 set_bit(SDF_NORECOVERY, &sdp->sd_flags);
731 smp_mb();
733 /* Wait on outstanding recovery */
734 restart:
735 spin_lock(&sdp->sd_jindex_spin);
736 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
737 if (!test_bit(JDF_RECOVERY, &jd->jd_flags))
738 continue;
739 spin_unlock(&sdp->sd_jindex_spin);
740 wait_on_bit(&jd->jd_flags, JDF_RECOVERY,
741 gfs2_umount_recovery_wait, TASK_UNINTERRUPTIBLE);
742 goto restart;
744 spin_unlock(&sdp->sd_jindex_spin);
746 kthread_stop(sdp->sd_quotad_process);
747 kthread_stop(sdp->sd_logd_process);
749 if (!(sb->s_flags & MS_RDONLY)) {
750 error = gfs2_make_fs_ro(sdp);
751 if (error)
752 gfs2_io_error(sdp);
754 /* At this point, we're through modifying the disk */
756 /* Release stuff */
758 iput(sdp->sd_jindex);
759 iput(sdp->sd_inum_inode);
760 iput(sdp->sd_statfs_inode);
761 iput(sdp->sd_rindex);
762 iput(sdp->sd_quota_inode);
764 gfs2_glock_put(sdp->sd_rename_gl);
765 gfs2_glock_put(sdp->sd_trans_gl);
767 if (!sdp->sd_args.ar_spectator) {
768 gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
769 gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
770 gfs2_glock_dq_uninit(&sdp->sd_ir_gh);
771 gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
772 gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
773 iput(sdp->sd_ir_inode);
774 iput(sdp->sd_sc_inode);
775 iput(sdp->sd_qc_inode);
778 gfs2_glock_dq_uninit(&sdp->sd_live_gh);
779 gfs2_clear_rgrpd(sdp);
780 gfs2_jindex_free(sdp);
781 /* Take apart glock structures and buffer lists */
782 gfs2_gl_hash_clear(sdp);
783 /* Unmount the locking protocol */
784 gfs2_lm_unmount(sdp);
786 /* At this point, we're through participating in the lockspace */
787 gfs2_sys_fs_del(sdp);
791 * gfs2_sync_fs - sync the filesystem
792 * @sb: the superblock
794 * Flushes the log to disk.
797 static int gfs2_sync_fs(struct super_block *sb, int wait)
799 if (wait && sb->s_fs_info)
800 gfs2_log_flush(sb->s_fs_info, NULL);
801 return 0;
805 * gfs2_freeze - prevent further writes to the filesystem
806 * @sb: the VFS structure for the filesystem
810 static int gfs2_freeze(struct super_block *sb)
812 struct gfs2_sbd *sdp = sb->s_fs_info;
813 int error;
815 if (test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
816 return -EINVAL;
818 for (;;) {
819 error = gfs2_freeze_fs(sdp);
820 if (!error)
821 break;
823 switch (error) {
824 case -EBUSY:
825 fs_err(sdp, "waiting for recovery before freeze\n");
826 break;
828 default:
829 fs_err(sdp, "error freezing FS: %d\n", error);
830 break;
833 fs_err(sdp, "retrying...\n");
834 msleep(1000);
836 return 0;
840 * gfs2_unfreeze - reallow writes to the filesystem
841 * @sb: the VFS structure for the filesystem
845 static int gfs2_unfreeze(struct super_block *sb)
847 gfs2_unfreeze_fs(sb->s_fs_info);
848 return 0;
852 * statfs_fill - fill in the sg for a given RG
853 * @rgd: the RG
854 * @sc: the sc structure
856 * Returns: 0 on success, -ESTALE if the LVB is invalid
859 static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
860 struct gfs2_statfs_change_host *sc)
862 gfs2_rgrp_verify(rgd);
863 sc->sc_total += rgd->rd_data;
864 sc->sc_free += rgd->rd_free;
865 sc->sc_dinodes += rgd->rd_dinodes;
866 return 0;
870 * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
871 * @sdp: the filesystem
872 * @sc: the sc info that will be returned
874 * Any error (other than a signal) will cause this routine to fall back
875 * to the synchronous version.
877 * FIXME: This really shouldn't busy wait like this.
879 * Returns: errno
882 static int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
884 struct gfs2_holder ri_gh;
885 struct gfs2_rgrpd *rgd_next;
886 struct gfs2_holder *gha, *gh;
887 unsigned int slots = 64;
888 unsigned int x;
889 int done;
890 int error = 0, err;
892 memset(sc, 0, sizeof(struct gfs2_statfs_change_host));
893 gha = kcalloc(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
894 if (!gha)
895 return -ENOMEM;
897 error = gfs2_rindex_hold(sdp, &ri_gh);
898 if (error)
899 goto out;
901 rgd_next = gfs2_rgrpd_get_first(sdp);
903 for (;;) {
904 done = 1;
906 for (x = 0; x < slots; x++) {
907 gh = gha + x;
909 if (gh->gh_gl && gfs2_glock_poll(gh)) {
910 err = gfs2_glock_wait(gh);
911 if (err) {
912 gfs2_holder_uninit(gh);
913 error = err;
914 } else {
915 if (!error)
916 error = statfs_slow_fill(
917 gh->gh_gl->gl_object, sc);
918 gfs2_glock_dq_uninit(gh);
922 if (gh->gh_gl)
923 done = 0;
924 else if (rgd_next && !error) {
925 error = gfs2_glock_nq_init(rgd_next->rd_gl,
926 LM_ST_SHARED,
927 GL_ASYNC,
928 gh);
929 rgd_next = gfs2_rgrpd_get_next(rgd_next);
930 done = 0;
933 if (signal_pending(current))
934 error = -ERESTARTSYS;
937 if (done)
938 break;
940 yield();
943 gfs2_glock_dq_uninit(&ri_gh);
945 out:
946 kfree(gha);
947 return error;
951 * gfs2_statfs_i - Do a statfs
952 * @sdp: the filesystem
953 * @sg: the sg structure
955 * Returns: errno
958 static int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
960 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
961 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
963 spin_lock(&sdp->sd_statfs_spin);
965 *sc = *m_sc;
966 sc->sc_total += l_sc->sc_total;
967 sc->sc_free += l_sc->sc_free;
968 sc->sc_dinodes += l_sc->sc_dinodes;
970 spin_unlock(&sdp->sd_statfs_spin);
972 if (sc->sc_free < 0)
973 sc->sc_free = 0;
974 if (sc->sc_free > sc->sc_total)
975 sc->sc_free = sc->sc_total;
976 if (sc->sc_dinodes < 0)
977 sc->sc_dinodes = 0;
979 return 0;
983 * gfs2_statfs - Gather and return stats about the filesystem
984 * @sb: The superblock
985 * @statfsbuf: The buffer
987 * Returns: 0 on success or error code
990 static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
992 struct super_block *sb = dentry->d_inode->i_sb;
993 struct gfs2_sbd *sdp = sb->s_fs_info;
994 struct gfs2_statfs_change_host sc;
995 int error;
997 if (gfs2_tune_get(sdp, gt_statfs_slow))
998 error = gfs2_statfs_slow(sdp, &sc);
999 else
1000 error = gfs2_statfs_i(sdp, &sc);
1002 if (error)
1003 return error;
1005 buf->f_type = GFS2_MAGIC;
1006 buf->f_bsize = sdp->sd_sb.sb_bsize;
1007 buf->f_blocks = sc.sc_total;
1008 buf->f_bfree = sc.sc_free;
1009 buf->f_bavail = sc.sc_free;
1010 buf->f_files = sc.sc_dinodes + sc.sc_free;
1011 buf->f_ffree = sc.sc_free;
1012 buf->f_namelen = GFS2_FNAMESIZE;
1014 return 0;
1018 * gfs2_remount_fs - called when the FS is remounted
1019 * @sb: the filesystem
1020 * @flags: the remount flags
1021 * @data: extra data passed in (not used right now)
1023 * Returns: errno
1026 static int gfs2_remount_fs(struct super_block *sb, int *flags, char *data)
1028 struct gfs2_sbd *sdp = sb->s_fs_info;
1029 struct gfs2_args args = sdp->sd_args; /* Default to current settings */
1030 struct gfs2_tune *gt = &sdp->sd_tune;
1031 int error;
1033 spin_lock(&gt->gt_spin);
1034 args.ar_commit = gt->gt_log_flush_secs;
1035 spin_unlock(&gt->gt_spin);
1036 error = gfs2_mount_args(sdp, &args, data);
1037 if (error)
1038 return error;
1040 /* Not allowed to change locking details */
1041 if (strcmp(args.ar_lockproto, sdp->sd_args.ar_lockproto) ||
1042 strcmp(args.ar_locktable, sdp->sd_args.ar_locktable) ||
1043 strcmp(args.ar_hostdata, sdp->sd_args.ar_hostdata))
1044 return -EINVAL;
1046 /* Some flags must not be changed */
1047 if (args_neq(&args, &sdp->sd_args, spectator) ||
1048 args_neq(&args, &sdp->sd_args, ignore_local_fs) ||
1049 args_neq(&args, &sdp->sd_args, localflocks) ||
1050 args_neq(&args, &sdp->sd_args, localcaching) ||
1051 args_neq(&args, &sdp->sd_args, meta))
1052 return -EINVAL;
1054 if (sdp->sd_args.ar_spectator)
1055 *flags |= MS_RDONLY;
1057 if ((sb->s_flags ^ *flags) & MS_RDONLY) {
1058 if (*flags & MS_RDONLY)
1059 error = gfs2_make_fs_ro(sdp);
1060 else
1061 error = gfs2_make_fs_rw(sdp);
1062 if (error)
1063 return error;
1066 sdp->sd_args = args;
1067 if (sdp->sd_args.ar_posix_acl)
1068 sb->s_flags |= MS_POSIXACL;
1069 else
1070 sb->s_flags &= ~MS_POSIXACL;
1071 spin_lock(&gt->gt_spin);
1072 gt->gt_log_flush_secs = args.ar_commit;
1073 spin_unlock(&gt->gt_spin);
1075 return 0;
1079 * gfs2_drop_inode - Drop an inode (test for remote unlink)
1080 * @inode: The inode to drop
1082 * If we've received a callback on an iopen lock then its because a
1083 * remote node tried to deallocate the inode but failed due to this node
1084 * still having the inode open. Here we mark the link count zero
1085 * since we know that it must have reached zero if the GLF_DEMOTE flag
1086 * is set on the iopen glock. If we didn't do a disk read since the
1087 * remote node removed the final link then we might otherwise miss
1088 * this event. This check ensures that this node will deallocate the
1089 * inode's blocks, or alternatively pass the baton on to another
1090 * node for later deallocation.
1093 static void gfs2_drop_inode(struct inode *inode)
1095 struct gfs2_inode *ip = GFS2_I(inode);
1097 if (test_bit(GIF_USER, &ip->i_flags) && inode->i_nlink) {
1098 struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
1099 if (gl && test_bit(GLF_DEMOTE, &gl->gl_flags))
1100 clear_nlink(inode);
1102 generic_drop_inode(inode);
1106 * gfs2_clear_inode - Deallocate an inode when VFS is done with it
1107 * @inode: The VFS inode
1111 static void gfs2_clear_inode(struct inode *inode)
1113 struct gfs2_inode *ip = GFS2_I(inode);
1115 /* This tells us its a "real" inode and not one which only
1116 * serves to contain an address space (see rgrp.c, meta_io.c)
1117 * which therefore doesn't have its own glocks.
1119 if (test_bit(GIF_USER, &ip->i_flags)) {
1120 ip->i_gl->gl_object = NULL;
1121 gfs2_glock_put(ip->i_gl);
1122 ip->i_gl = NULL;
1123 if (ip->i_iopen_gh.gh_gl) {
1124 ip->i_iopen_gh.gh_gl->gl_object = NULL;
1125 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
1130 static int is_ancestor(const struct dentry *d1, const struct dentry *d2)
1132 do {
1133 if (d1 == d2)
1134 return 1;
1135 d1 = d1->d_parent;
1136 } while (!IS_ROOT(d1));
1137 return 0;
1141 * gfs2_show_options - Show mount options for /proc/mounts
1142 * @s: seq_file structure
1143 * @mnt: vfsmount
1145 * Returns: 0 on success or error code
1148 static int gfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
1150 struct gfs2_sbd *sdp = mnt->mnt_sb->s_fs_info;
1151 struct gfs2_args *args = &sdp->sd_args;
1152 int lfsecs;
1154 if (is_ancestor(mnt->mnt_root, sdp->sd_master_dir))
1155 seq_printf(s, ",meta");
1156 if (args->ar_lockproto[0])
1157 seq_printf(s, ",lockproto=%s", args->ar_lockproto);
1158 if (args->ar_locktable[0])
1159 seq_printf(s, ",locktable=%s", args->ar_locktable);
1160 if (args->ar_hostdata[0])
1161 seq_printf(s, ",hostdata=%s", args->ar_hostdata);
1162 if (args->ar_spectator)
1163 seq_printf(s, ",spectator");
1164 if (args->ar_ignore_local_fs)
1165 seq_printf(s, ",ignore_local_fs");
1166 if (args->ar_localflocks)
1167 seq_printf(s, ",localflocks");
1168 if (args->ar_localcaching)
1169 seq_printf(s, ",localcaching");
1170 if (args->ar_debug)
1171 seq_printf(s, ",debug");
1172 if (args->ar_upgrade)
1173 seq_printf(s, ",upgrade");
1174 if (args->ar_posix_acl)
1175 seq_printf(s, ",acl");
1176 if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
1177 char *state;
1178 switch (args->ar_quota) {
1179 case GFS2_QUOTA_OFF:
1180 state = "off";
1181 break;
1182 case GFS2_QUOTA_ACCOUNT:
1183 state = "account";
1184 break;
1185 case GFS2_QUOTA_ON:
1186 state = "on";
1187 break;
1188 default:
1189 state = "unknown";
1190 break;
1192 seq_printf(s, ",quota=%s", state);
1194 if (args->ar_suiddir)
1195 seq_printf(s, ",suiddir");
1196 if (args->ar_data != GFS2_DATA_DEFAULT) {
1197 char *state;
1198 switch (args->ar_data) {
1199 case GFS2_DATA_WRITEBACK:
1200 state = "writeback";
1201 break;
1202 case GFS2_DATA_ORDERED:
1203 state = "ordered";
1204 break;
1205 default:
1206 state = "unknown";
1207 break;
1209 seq_printf(s, ",data=%s", state);
1211 if (args->ar_discard)
1212 seq_printf(s, ",discard");
1213 lfsecs = sdp->sd_tune.gt_log_flush_secs;
1214 if (lfsecs != 60)
1215 seq_printf(s, ",commit=%d", lfsecs);
1216 return 0;
1220 * We have to (at the moment) hold the inodes main lock to cover
1221 * the gap between unlocking the shared lock on the iopen lock and
1222 * taking the exclusive lock. I'd rather do a shared -> exclusive
1223 * conversion on the iopen lock, but we can change that later. This
1224 * is safe, just less efficient.
1227 static void gfs2_delete_inode(struct inode *inode)
1229 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
1230 struct gfs2_inode *ip = GFS2_I(inode);
1231 struct gfs2_holder gh;
1232 int error;
1234 if (!test_bit(GIF_USER, &ip->i_flags))
1235 goto out;
1237 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1238 if (unlikely(error)) {
1239 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
1240 goto out;
1243 gfs2_glock_dq_wait(&ip->i_iopen_gh);
1244 gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &ip->i_iopen_gh);
1245 error = gfs2_glock_nq(&ip->i_iopen_gh);
1246 if (error)
1247 goto out_truncate;
1249 if (S_ISDIR(inode->i_mode) &&
1250 (ip->i_diskflags & GFS2_DIF_EXHASH)) {
1251 error = gfs2_dir_exhash_dealloc(ip);
1252 if (error)
1253 goto out_unlock;
1256 if (ip->i_eattr) {
1257 error = gfs2_ea_dealloc(ip);
1258 if (error)
1259 goto out_unlock;
1262 if (!gfs2_is_stuffed(ip)) {
1263 error = gfs2_file_dealloc(ip);
1264 if (error)
1265 goto out_unlock;
1268 error = gfs2_dinode_dealloc(ip);
1269 if (error)
1270 goto out_unlock;
1272 out_truncate:
1273 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
1274 if (error)
1275 goto out_unlock;
1276 /* Needs to be done before glock release & also in a transaction */
1277 truncate_inode_pages(&inode->i_data, 0);
1278 gfs2_trans_end(sdp);
1280 out_unlock:
1281 if (test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags))
1282 gfs2_glock_dq(&ip->i_iopen_gh);
1283 gfs2_holder_uninit(&ip->i_iopen_gh);
1284 gfs2_glock_dq_uninit(&gh);
1285 if (error && error != GLR_TRYFAILED && error != -EROFS)
1286 fs_warn(sdp, "gfs2_delete_inode: %d\n", error);
1287 out:
1288 truncate_inode_pages(&inode->i_data, 0);
1289 clear_inode(inode);
1292 static struct inode *gfs2_alloc_inode(struct super_block *sb)
1294 struct gfs2_inode *ip;
1296 ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
1297 if (ip) {
1298 ip->i_flags = 0;
1299 ip->i_gl = NULL;
1301 return &ip->i_inode;
1304 static void gfs2_destroy_inode(struct inode *inode)
1306 kmem_cache_free(gfs2_inode_cachep, inode);
1309 const struct super_operations gfs2_super_ops = {
1310 .alloc_inode = gfs2_alloc_inode,
1311 .destroy_inode = gfs2_destroy_inode,
1312 .write_inode = gfs2_write_inode,
1313 .delete_inode = gfs2_delete_inode,
1314 .put_super = gfs2_put_super,
1315 .sync_fs = gfs2_sync_fs,
1316 .freeze_fs = gfs2_freeze,
1317 .unfreeze_fs = gfs2_unfreeze,
1318 .statfs = gfs2_statfs,
1319 .remount_fs = gfs2_remount_fs,
1320 .clear_inode = gfs2_clear_inode,
1321 .drop_inode = gfs2_drop_inode,
1322 .show_options = gfs2_show_options,