1/*
2 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3 * Copyright (C) 2004-2007 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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/bio.h>
13#include <linux/sched.h>
14#include <linux/slab.h>
15#include <linux/spinlock.h>
16#include <linux/completion.h>
17#include <linux/buffer_head.h>
18#include <linux/statfs.h>
19#include <linux/seq_file.h>
20#include <linux/mount.h>
21#include <linux/kthread.h>
22#include <linux/delay.h>
23#include <linux/gfs2_ondisk.h>
24#include <linux/crc32.h>
25#include <linux/time.h>
26#include <linux/wait.h>
27#include <linux/writeback.h>
28#include <linux/backing-dev.h>
29#include <linux/kernel.h>
30
31#include "gfs2.h"
32#include "incore.h"
33#include "bmap.h"
34#include "dir.h"
35#include "glock.h"
36#include "glops.h"
37#include "inode.h"
38#include "log.h"
39#include "meta_io.h"
40#include "quota.h"
41#include "recovery.h"
42#include "rgrp.h"
43#include "super.h"
44#include "trans.h"
45#include "util.h"
46#include "sys.h"
47#include "xattr.h"
48
49#define args_neq(a1, a2, x) ((a1)->ar_##x != (a2)->ar_##x)
50
51enum {
52	Opt_lockproto,
53	Opt_locktable,
54	Opt_hostdata,
55	Opt_spectator,
56	Opt_ignore_local_fs,
57	Opt_localflocks,
58	Opt_localcaching,
59	Opt_debug,
60	Opt_nodebug,
61	Opt_upgrade,
62	Opt_acl,
63	Opt_noacl,
64	Opt_quota_off,
65	Opt_quota_account,
66	Opt_quota_on,
67	Opt_quota,
68	Opt_noquota,
69	Opt_suiddir,
70	Opt_nosuiddir,
71	Opt_data_writeback,
72	Opt_data_ordered,
73	Opt_meta,
74	Opt_discard,
75	Opt_nodiscard,
76	Opt_commit,
77	Opt_err_withdraw,
78	Opt_err_panic,
79	Opt_statfs_quantum,
80	Opt_statfs_percent,
81	Opt_quota_quantum,
82	Opt_barrier,
83	Opt_nobarrier,
84	Opt_rgrplvb,
85	Opt_norgrplvb,
86	Opt_error,
87};
88
89static const match_table_t tokens = {
90	{Opt_lockproto, "lockproto=%s"},
91	{Opt_locktable, "locktable=%s"},
92	{Opt_hostdata, "hostdata=%s"},
93	{Opt_spectator, "spectator"},
94	{Opt_spectator, "norecovery"},
95	{Opt_ignore_local_fs, "ignore_local_fs"},
96	{Opt_localflocks, "localflocks"},
97	{Opt_localcaching, "localcaching"},
98	{Opt_debug, "debug"},
99	{Opt_nodebug, "nodebug"},
100	{Opt_upgrade, "upgrade"},
101	{Opt_acl, "acl"},
102	{Opt_noacl, "noacl"},
103	{Opt_quota_off, "quota=off"},
104	{Opt_quota_account, "quota=account"},
105	{Opt_quota_on, "quota=on"},
106	{Opt_quota, "quota"},
107	{Opt_noquota, "noquota"},
108	{Opt_suiddir, "suiddir"},
109	{Opt_nosuiddir, "nosuiddir"},
110	{Opt_data_writeback, "data=writeback"},
111	{Opt_data_ordered, "data=ordered"},
112	{Opt_meta, "meta"},
113	{Opt_discard, "discard"},
114	{Opt_nodiscard, "nodiscard"},
115	{Opt_commit, "commit=%d"},
116	{Opt_err_withdraw, "errors=withdraw"},
117	{Opt_err_panic, "errors=panic"},
118	{Opt_statfs_quantum, "statfs_quantum=%d"},
119	{Opt_statfs_percent, "statfs_percent=%d"},
120	{Opt_quota_quantum, "quota_quantum=%d"},
121	{Opt_barrier, "barrier"},
122	{Opt_nobarrier, "nobarrier"},
123	{Opt_rgrplvb, "rgrplvb"},
124	{Opt_norgrplvb, "norgrplvb"},
125	{Opt_error, NULL}
126};
127
128/**
129 * gfs2_mount_args - Parse mount options
130 * @args: The structure into which the parsed options will be written
131 * @options: The options to parse
132 *
133 * Return: errno
134 */
135
136int gfs2_mount_args(struct gfs2_args *args, char *options)
137{
138	char *o;
139	int token;
140	substring_t tmp[MAX_OPT_ARGS];
141	int rv;
142
143	/* Split the options into tokens with the "," character and
144	   process them */
145
146	while (1) {
147		o = strsep(&options, ",");
148		if (o == NULL)
149			break;
150		if (*o == '\0')
151			continue;
152
153		token = match_token(o, tokens, tmp);
154		switch (token) {
155		case Opt_lockproto:
156			match_strlcpy(args->ar_lockproto, &tmp[0],
157				      GFS2_LOCKNAME_LEN);
158			break;
159		case Opt_locktable:
160			match_strlcpy(args->ar_locktable, &tmp[0],
161				      GFS2_LOCKNAME_LEN);
162			break;
163		case Opt_hostdata:
164			match_strlcpy(args->ar_hostdata, &tmp[0],
165				      GFS2_LOCKNAME_LEN);
166			break;
167		case Opt_spectator:
168			args->ar_spectator = 1;
169			break;
170		case Opt_ignore_local_fs:
171			/* Retained for backwards compat only */
172			break;
173		case Opt_localflocks:
174			args->ar_localflocks = 1;
175			break;
176		case Opt_localcaching:
177			/* Retained for backwards compat only */
178			break;
179		case Opt_debug:
180			if (args->ar_errors == GFS2_ERRORS_PANIC) {
181				pr_warn("-o debug and -o errors=panic are mutually exclusive\n");
182				return -EINVAL;
183			}
184			args->ar_debug = 1;
185			break;
186		case Opt_nodebug:
187			args->ar_debug = 0;
188			break;
189		case Opt_upgrade:
190			/* Retained for backwards compat only */
191			break;
192		case Opt_acl:
193			args->ar_posix_acl = 1;
194			break;
195		case Opt_noacl:
196			args->ar_posix_acl = 0;
197			break;
198		case Opt_quota_off:
199		case Opt_noquota:
200			args->ar_quota = GFS2_QUOTA_OFF;
201			break;
202		case Opt_quota_account:
203			args->ar_quota = GFS2_QUOTA_ACCOUNT;
204			break;
205		case Opt_quota_on:
206		case Opt_quota:
207			args->ar_quota = GFS2_QUOTA_ON;
208			break;
209		case Opt_suiddir:
210			args->ar_suiddir = 1;
211			break;
212		case Opt_nosuiddir:
213			args->ar_suiddir = 0;
214			break;
215		case Opt_data_writeback:
216			args->ar_data = GFS2_DATA_WRITEBACK;
217			break;
218		case Opt_data_ordered:
219			args->ar_data = GFS2_DATA_ORDERED;
220			break;
221		case Opt_meta:
222			args->ar_meta = 1;
223			break;
224		case Opt_discard:
225			args->ar_discard = 1;
226			break;
227		case Opt_nodiscard:
228			args->ar_discard = 0;
229			break;
230		case Opt_commit:
231			rv = match_int(&tmp[0], &args->ar_commit);
232			if (rv || args->ar_commit <= 0) {
233				pr_warn("commit mount option requires a positive numeric argument\n");
234				return rv ? rv : -EINVAL;
235			}
236			break;
237		case Opt_statfs_quantum:
238			rv = match_int(&tmp[0], &args->ar_statfs_quantum);
239			if (rv || args->ar_statfs_quantum < 0) {
240				pr_warn("statfs_quantum mount option requires a non-negative numeric argument\n");
241				return rv ? rv : -EINVAL;
242			}
243			break;
244		case Opt_quota_quantum:
245			rv = match_int(&tmp[0], &args->ar_quota_quantum);
246			if (rv || args->ar_quota_quantum <= 0) {
247				pr_warn("quota_quantum mount option requires a positive numeric argument\n");
248				return rv ? rv : -EINVAL;
249			}
250			break;
251		case Opt_statfs_percent:
252			rv = match_int(&tmp[0], &args->ar_statfs_percent);
253			if (rv || args->ar_statfs_percent < 0 ||
254			    args->ar_statfs_percent > 100) {
255				pr_warn("statfs_percent mount option requires a numeric argument between 0 and 100\n");
256				return rv ? rv : -EINVAL;
257			}
258			break;
259		case Opt_err_withdraw:
260			args->ar_errors = GFS2_ERRORS_WITHDRAW;
261			break;
262		case Opt_err_panic:
263			if (args->ar_debug) {
264				pr_warn("-o debug and -o errors=panic are mutually exclusive\n");
265				return -EINVAL;
266			}
267			args->ar_errors = GFS2_ERRORS_PANIC;
268			break;
269		case Opt_barrier:
270			args->ar_nobarrier = 0;
271			break;
272		case Opt_nobarrier:
273			args->ar_nobarrier = 1;
274			break;
275		case Opt_rgrplvb:
276			args->ar_rgrplvb = 1;
277			break;
278		case Opt_norgrplvb:
279			args->ar_rgrplvb = 0;
280			break;
281		case Opt_error:
282		default:
283			pr_warn("invalid mount option: %s\n", o);
284			return -EINVAL;
285		}
286	}
287
288	return 0;
289}
290
291/**
292 * gfs2_jindex_free - Clear all the journal index information
293 * @sdp: The GFS2 superblock
294 *
295 */
296
297void gfs2_jindex_free(struct gfs2_sbd *sdp)
298{
299	struct list_head list;
300	struct gfs2_jdesc *jd;
301
302	spin_lock(&sdp->sd_jindex_spin);
303	list_add(&list, &sdp->sd_jindex_list);
304	list_del_init(&sdp->sd_jindex_list);
305	sdp->sd_journals = 0;
306	spin_unlock(&sdp->sd_jindex_spin);
307
308	while (!list_empty(&list)) {
309		jd = list_entry(list.next, struct gfs2_jdesc, jd_list);
310		gfs2_free_journal_extents(jd);
311		list_del(&jd->jd_list);
312		iput(jd->jd_inode);
313		kfree(jd);
314	}
315}
316
317static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
318{
319	struct gfs2_jdesc *jd;
320	int found = 0;
321
322	list_for_each_entry(jd, head, jd_list) {
323		if (jd->jd_jid == jid) {
324			found = 1;
325			break;
326		}
327	}
328
329	if (!found)
330		jd = NULL;
331
332	return jd;
333}
334
335struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
336{
337	struct gfs2_jdesc *jd;
338
339	spin_lock(&sdp->sd_jindex_spin);
340	jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
341	spin_unlock(&sdp->sd_jindex_spin);
342
343	return jd;
344}
345
346int gfs2_jdesc_check(struct gfs2_jdesc *jd)
347{
348	struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
349	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
350	u64 size = i_size_read(jd->jd_inode);
351
352	if (gfs2_check_internal_file_size(jd->jd_inode, 8 << 20, 1 << 30))
353		return -EIO;
354
355	jd->jd_blocks = size >> sdp->sd_sb.sb_bsize_shift;
356
357	if (gfs2_write_alloc_required(ip, 0, size)) {
358		gfs2_consist_inode(ip);
359		return -EIO;
360	}
361
362	return 0;
363}
364
365static int init_threads(struct gfs2_sbd *sdp)
366{
367	struct task_struct *p;
368	int error = 0;
369
370	p = kthread_run(gfs2_logd, sdp, "gfs2_logd");
371	if (IS_ERR(p)) {
372		error = PTR_ERR(p);
373		fs_err(sdp, "can't start logd thread: %d\n", error);
374		return error;
375	}
376	sdp->sd_logd_process = p;
377
378	p = kthread_run(gfs2_quotad, sdp, "gfs2_quotad");
379	if (IS_ERR(p)) {
380		error = PTR_ERR(p);
381		fs_err(sdp, "can't start quotad thread: %d\n", error);
382		goto fail;
383	}
384	sdp->sd_quotad_process = p;
385	return 0;
386
387fail:
388	kthread_stop(sdp->sd_logd_process);
389	return error;
390}
391
392/**
393 * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
394 * @sdp: the filesystem
395 *
396 * Returns: errno
397 */
398
399int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
400{
401	struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
402	struct gfs2_glock *j_gl = ip->i_gl;
403	struct gfs2_holder freeze_gh;
404	struct gfs2_log_header_host head;
405	int error;
406
407	error = init_threads(sdp);
408	if (error)
409		return error;
410
411	error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, 0,
412				   &freeze_gh);
413	if (error)
414		goto fail_threads;
415
416	j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
417
418	error = gfs2_find_jhead(sdp->sd_jdesc, &head);
419	if (error)
420		goto fail;
421
422	if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
423		gfs2_consist(sdp);
424		error = -EIO;
425		goto fail;
426	}
427
428	/*  Initialize some head of the log stuff  */
429	sdp->sd_log_sequence = head.lh_sequence + 1;
430	gfs2_log_pointers_init(sdp, head.lh_blkno);
431
432	error = gfs2_quota_init(sdp);
433	if (error)
434		goto fail;
435
436	set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
437
438	gfs2_glock_dq_uninit(&freeze_gh);
439
440	return 0;
441
442fail:
443	freeze_gh.gh_flags |= GL_NOCACHE;
444	gfs2_glock_dq_uninit(&freeze_gh);
445fail_threads:
446	kthread_stop(sdp->sd_quotad_process);
447	kthread_stop(sdp->sd_logd_process);
448	return error;
449}
450
451void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
452{
453	const struct gfs2_statfs_change *str = buf;
454
455	sc->sc_total = be64_to_cpu(str->sc_total);
456	sc->sc_free = be64_to_cpu(str->sc_free);
457	sc->sc_dinodes = be64_to_cpu(str->sc_dinodes);
458}
459
460static void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
461{
462	struct gfs2_statfs_change *str = buf;
463
464	str->sc_total = cpu_to_be64(sc->sc_total);
465	str->sc_free = cpu_to_be64(sc->sc_free);
466	str->sc_dinodes = cpu_to_be64(sc->sc_dinodes);
467}
468
469int gfs2_statfs_init(struct gfs2_sbd *sdp)
470{
471	struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
472	struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
473	struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
474	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
475	struct buffer_head *m_bh, *l_bh;
476	struct gfs2_holder gh;
477	int error;
478
479	error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
480				   &gh);
481	if (error)
482		return error;
483
484	error = gfs2_meta_inode_buffer(m_ip, &m_bh);
485	if (error)
486		goto out;
487
488	if (sdp->sd_args.ar_spectator) {
489		spin_lock(&sdp->sd_statfs_spin);
490		gfs2_statfs_change_in(m_sc, m_bh->b_data +
491				      sizeof(struct gfs2_dinode));
492		spin_unlock(&sdp->sd_statfs_spin);
493	} else {
494		error = gfs2_meta_inode_buffer(l_ip, &l_bh);
495		if (error)
496			goto out_m_bh;
497
498		spin_lock(&sdp->sd_statfs_spin);
499		gfs2_statfs_change_in(m_sc, m_bh->b_data +
500				      sizeof(struct gfs2_dinode));
501		gfs2_statfs_change_in(l_sc, l_bh->b_data +
502				      sizeof(struct gfs2_dinode));
503		spin_unlock(&sdp->sd_statfs_spin);
504
505		brelse(l_bh);
506	}
507
508out_m_bh:
509	brelse(m_bh);
510out:
511	gfs2_glock_dq_uninit(&gh);
512	return 0;
513}
514
515void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
516			s64 dinodes)
517{
518	struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
519	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
520	struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
521	struct buffer_head *l_bh;
522	s64 x, y;
523	int need_sync = 0;
524	int error;
525
526	error = gfs2_meta_inode_buffer(l_ip, &l_bh);
527	if (error)
528		return;
529
530	gfs2_trans_add_meta(l_ip->i_gl, l_bh);
531
532	spin_lock(&sdp->sd_statfs_spin);
533	l_sc->sc_total += total;
534	l_sc->sc_free += free;
535	l_sc->sc_dinodes += dinodes;
536	gfs2_statfs_change_out(l_sc, l_bh->b_data + sizeof(struct gfs2_dinode));
537	if (sdp->sd_args.ar_statfs_percent) {
538		x = 100 * l_sc->sc_free;
539		y = m_sc->sc_free * sdp->sd_args.ar_statfs_percent;
540		if (x >= y || x <= -y)
541			need_sync = 1;
542	}
543	spin_unlock(&sdp->sd_statfs_spin);
544
545	brelse(l_bh);
546	if (need_sync)
547		gfs2_wake_up_statfs(sdp);
548}
549
550void update_statfs(struct gfs2_sbd *sdp, struct buffer_head *m_bh,
551		   struct buffer_head *l_bh)
552{
553	struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
554	struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
555	struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
556	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
557
558	gfs2_trans_add_meta(l_ip->i_gl, l_bh);
559
560	spin_lock(&sdp->sd_statfs_spin);
561	m_sc->sc_total += l_sc->sc_total;
562	m_sc->sc_free += l_sc->sc_free;
563	m_sc->sc_dinodes += l_sc->sc_dinodes;
564	memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
565	memset(l_bh->b_data + sizeof(struct gfs2_dinode),
566	       0, sizeof(struct gfs2_statfs_change));
567	spin_unlock(&sdp->sd_statfs_spin);
568
569	gfs2_trans_add_meta(m_ip->i_gl, m_bh);
570	gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
571}
572
573int gfs2_statfs_sync(struct super_block *sb, int type)
574{
575	struct gfs2_sbd *sdp = sb->s_fs_info;
576	struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
577	struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
578	struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
579	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
580	struct gfs2_holder gh;
581	struct buffer_head *m_bh, *l_bh;
582	int error;
583
584	sb_start_write(sb);
585	error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
586				   &gh);
587	if (error)
588		goto out;
589
590	error = gfs2_meta_inode_buffer(m_ip, &m_bh);
591	if (error)
592		goto out_unlock;
593
594	spin_lock(&sdp->sd_statfs_spin);
595	gfs2_statfs_change_in(m_sc, m_bh->b_data +
596			      sizeof(struct gfs2_dinode));
597	if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
598		spin_unlock(&sdp->sd_statfs_spin);
599		goto out_bh;
600	}
601	spin_unlock(&sdp->sd_statfs_spin);
602
603	error = gfs2_meta_inode_buffer(l_ip, &l_bh);
604	if (error)
605		goto out_bh;
606
607	error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
608	if (error)
609		goto out_bh2;
610
611	update_statfs(sdp, m_bh, l_bh);
612	sdp->sd_statfs_force_sync = 0;
613
614	gfs2_trans_end(sdp);
615
616out_bh2:
617	brelse(l_bh);
618out_bh:
619	brelse(m_bh);
620out_unlock:
621	gfs2_glock_dq_uninit(&gh);
622out:
623	sb_end_write(sb);
624	return error;
625}
626
627struct lfcc {
628	struct list_head list;
629	struct gfs2_holder gh;
630};
631
632/**
633 * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
634 *                            journals are clean
635 * @sdp: the file system
636 * @state: the state to put the transaction lock into
637 * @t_gh: the hold on the transaction lock
638 *
639 * Returns: errno
640 */
641
642static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp,
643				    struct gfs2_holder *freeze_gh)
644{
645	struct gfs2_inode *ip;
646	struct gfs2_jdesc *jd;
647	struct lfcc *lfcc;
648	LIST_HEAD(list);
649	struct gfs2_log_header_host lh;
650	int error;
651
652	list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
653		lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
654		if (!lfcc) {
655			error = -ENOMEM;
656			goto out;
657		}
658		ip = GFS2_I(jd->jd_inode);
659		error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
660		if (error) {
661			kfree(lfcc);
662			goto out;
663		}
664		list_add(&lfcc->list, &list);
665	}
666
667	error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_EXCLUSIVE,
668				   GL_NOCACHE, freeze_gh);
669
670	list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
671		error = gfs2_jdesc_check(jd);
672		if (error)
673			break;
674		error = gfs2_find_jhead(jd, &lh);
675		if (error)
676			break;
677		if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
678			error = -EBUSY;
679			break;
680		}
681	}
682
683	if (error)
684		gfs2_glock_dq_uninit(freeze_gh);
685
686out:
687	while (!list_empty(&list)) {
688		lfcc = list_entry(list.next, struct lfcc, list);
689		list_del(&lfcc->list);
690		gfs2_glock_dq_uninit(&lfcc->gh);
691		kfree(lfcc);
692	}
693	return error;
694}
695
696void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
697{
698	struct gfs2_dinode *str = buf;
699
700	str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
701	str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
702	str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
703	str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
704	str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
705	str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
706	str->di_uid = cpu_to_be32(i_uid_read(&ip->i_inode));
707	str->di_gid = cpu_to_be32(i_gid_read(&ip->i_inode));
708	str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
709	str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
710	str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
711	str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
712	str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
713	str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
714
715	str->di_goal_meta = cpu_to_be64(ip->i_goal);
716	str->di_goal_data = cpu_to_be64(ip->i_goal);
717	str->di_generation = cpu_to_be64(ip->i_generation);
718
719	str->di_flags = cpu_to_be32(ip->i_diskflags);
720	str->di_height = cpu_to_be16(ip->i_height);
721	str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
722					     !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
723					     GFS2_FORMAT_DE : 0);
724	str->di_depth = cpu_to_be16(ip->i_depth);
725	str->di_entries = cpu_to_be32(ip->i_entries);
726
727	str->di_eattr = cpu_to_be64(ip->i_eattr);
728	str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
729	str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
730	str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
731}
732
733/**
734 * gfs2_write_inode - Make sure the inode is stable on the disk
735 * @inode: The inode
736 * @wbc: The writeback control structure
737 *
738 * Returns: errno
739 */
740
741static int gfs2_write_inode(struct inode *inode, struct writeback_control *wbc)
742{
743	struct gfs2_inode *ip = GFS2_I(inode);
744	struct gfs2_sbd *sdp = GFS2_SB(inode);
745	struct address_space *metamapping = gfs2_glock2aspace(ip->i_gl);
746	struct backing_dev_info *bdi = inode_to_bdi(metamapping->host);
747	int ret = 0;
748
749	if (wbc->sync_mode == WB_SYNC_ALL)
750		gfs2_log_flush(GFS2_SB(inode), ip->i_gl, NORMAL_FLUSH);
751	if (bdi->dirty_exceeded)
752		gfs2_ail1_flush(sdp, wbc);
753	else
754		filemap_fdatawrite(metamapping);
755	if (wbc->sync_mode == WB_SYNC_ALL)
756		ret = filemap_fdatawait(metamapping);
757	if (ret)
758		mark_inode_dirty_sync(inode);
759	return ret;
760}
761
762/**
763 * gfs2_dirty_inode - check for atime updates
764 * @inode: The inode in question
765 * @flags: The type of dirty
766 *
767 * Unfortunately it can be called under any combination of inode
768 * glock and transaction lock, so we have to check carefully.
769 *
770 * At the moment this deals only with atime - it should be possible
771 * to expand that role in future, once a review of the locking has
772 * been carried out.
773 */
774
775static void gfs2_dirty_inode(struct inode *inode, int flags)
776{
777	struct gfs2_inode *ip = GFS2_I(inode);
778	struct gfs2_sbd *sdp = GFS2_SB(inode);
779	struct buffer_head *bh;
780	struct gfs2_holder gh;
781	int need_unlock = 0;
782	int need_endtrans = 0;
783	int ret;
784
785	if (!(flags & (I_DIRTY_DATASYNC|I_DIRTY_SYNC)))
786		return;
787
788	if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
789		ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
790		if (ret) {
791			fs_err(sdp, "dirty_inode: glock %d\n", ret);
792			return;
793		}
794		need_unlock = 1;
795	} else if (WARN_ON_ONCE(ip->i_gl->gl_state != LM_ST_EXCLUSIVE))
796		return;
797
798	if (current->journal_info == NULL) {
799		ret = gfs2_trans_begin(sdp, RES_DINODE, 0);
800		if (ret) {
801			fs_err(sdp, "dirty_inode: gfs2_trans_begin %d\n", ret);
802			goto out;
803		}
804		need_endtrans = 1;
805	}
806
807	ret = gfs2_meta_inode_buffer(ip, &bh);
808	if (ret == 0) {
809		gfs2_trans_add_meta(ip->i_gl, bh);
810		gfs2_dinode_out(ip, bh->b_data);
811		brelse(bh);
812	}
813
814	if (need_endtrans)
815		gfs2_trans_end(sdp);
816out:
817	if (need_unlock)
818		gfs2_glock_dq_uninit(&gh);
819}
820
821/**
822 * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
823 * @sdp: the filesystem
824 *
825 * Returns: errno
826 */
827
828static int gfs2_make_fs_ro(struct gfs2_sbd *sdp)
829{
830	struct gfs2_holder freeze_gh;
831	int error;
832
833	error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, GL_NOCACHE,
834				   &freeze_gh);
835	if (error && !test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
836		return error;
837
838	kthread_stop(sdp->sd_quotad_process);
839	kthread_stop(sdp->sd_logd_process);
840
841	flush_workqueue(gfs2_delete_workqueue);
842	gfs2_quota_sync(sdp->sd_vfs, 0);
843	gfs2_statfs_sync(sdp->sd_vfs, 0);
844
845	down_write(&sdp->sd_log_flush_lock);
846	clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
847	up_write(&sdp->sd_log_flush_lock);
848
849	gfs2_log_flush(sdp, NULL, SHUTDOWN_FLUSH);
850	wait_event(sdp->sd_reserving_log_wait, atomic_read(&sdp->sd_reserving_log) == 0);
851	gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
852
853	if (freeze_gh.gh_gl)
854		gfs2_glock_dq_uninit(&freeze_gh);
855
856	gfs2_quota_cleanup(sdp);
857
858	return error;
859}
860
861/**
862 * gfs2_put_super - Unmount the filesystem
863 * @sb: The VFS superblock
864 *
865 */
866
867static void gfs2_put_super(struct super_block *sb)
868{
869	struct gfs2_sbd *sdp = sb->s_fs_info;
870	int error;
871	struct gfs2_jdesc *jd;
872
873	/* No more recovery requests */
874	set_bit(SDF_NORECOVERY, &sdp->sd_flags);
875	smp_mb();
876
877	/* Wait on outstanding recovery */
878restart:
879	spin_lock(&sdp->sd_jindex_spin);
880	list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
881		if (!test_bit(JDF_RECOVERY, &jd->jd_flags))
882			continue;
883		spin_unlock(&sdp->sd_jindex_spin);
884		wait_on_bit(&jd->jd_flags, JDF_RECOVERY,
885			    TASK_UNINTERRUPTIBLE);
886		goto restart;
887	}
888	spin_unlock(&sdp->sd_jindex_spin);
889
890	if (!(sb->s_flags & MS_RDONLY)) {
891		error = gfs2_make_fs_ro(sdp);
892		if (error)
893			gfs2_io_error(sdp);
894	}
895	/*  At this point, we're through modifying the disk  */
896
897	/*  Release stuff  */
898
899	iput(sdp->sd_jindex);
900	iput(sdp->sd_statfs_inode);
901	iput(sdp->sd_rindex);
902	iput(sdp->sd_quota_inode);
903
904	gfs2_glock_put(sdp->sd_rename_gl);
905	gfs2_glock_put(sdp->sd_freeze_gl);
906
907	if (!sdp->sd_args.ar_spectator) {
908		gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
909		gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
910		gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
911		gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
912		iput(sdp->sd_sc_inode);
913		iput(sdp->sd_qc_inode);
914	}
915
916	gfs2_glock_dq_uninit(&sdp->sd_live_gh);
917	gfs2_clear_rgrpd(sdp);
918	gfs2_jindex_free(sdp);
919	/*  Take apart glock structures and buffer lists  */
920	gfs2_gl_hash_clear(sdp);
921	/*  Unmount the locking protocol  */
922	gfs2_lm_unmount(sdp);
923
924	/*  At this point, we're through participating in the lockspace  */
925	gfs2_sys_fs_del(sdp);
926}
927
928/**
929 * gfs2_sync_fs - sync the filesystem
930 * @sb: the superblock
931 *
932 * Flushes the log to disk.
933 */
934
935static int gfs2_sync_fs(struct super_block *sb, int wait)
936{
937	struct gfs2_sbd *sdp = sb->s_fs_info;
938
939	gfs2_quota_sync(sb, -1);
940	if (wait && sdp)
941		gfs2_log_flush(sdp, NULL, NORMAL_FLUSH);
942	return 0;
943}
944
945void gfs2_freeze_func(struct work_struct *work)
946{
947	int error;
948	struct gfs2_holder freeze_gh;
949	struct gfs2_sbd *sdp = container_of(work, struct gfs2_sbd, sd_freeze_work);
950	struct super_block *sb = sdp->sd_vfs;
951
952	atomic_inc(&sb->s_active);
953	error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, 0,
954				   &freeze_gh);
955	if (error) {
956		printk(KERN_INFO "GFS2: couln't get freeze lock : %d\n", error);
957		gfs2_assert_withdraw(sdp, 0);
958	}
959	else {
960		atomic_set(&sdp->sd_freeze_state, SFS_UNFROZEN);
961		error = thaw_super(sb);
962		if (error) {
963			printk(KERN_INFO "GFS2: couldn't thaw filesystem: %d\n",
964			       error);
965			gfs2_assert_withdraw(sdp, 0);
966		}
967		if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
968			freeze_gh.gh_flags |= GL_NOCACHE;
969		gfs2_glock_dq_uninit(&freeze_gh);
970	}
971	deactivate_super(sb);
972	return;
973}
974
975/**
976 * gfs2_freeze - prevent further writes to the filesystem
977 * @sb: the VFS structure for the filesystem
978 *
979 */
980
981static int gfs2_freeze(struct super_block *sb)
982{
983	struct gfs2_sbd *sdp = sb->s_fs_info;
984	int error = 0;
985
986	mutex_lock(&sdp->sd_freeze_mutex);
987	if (atomic_read(&sdp->sd_freeze_state) != SFS_UNFROZEN)
988		goto out;
989
990	if (test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
991		error = -EINVAL;
992		goto out;
993	}
994
995	for (;;) {
996		error = gfs2_lock_fs_check_clean(sdp, &sdp->sd_freeze_gh);
997		if (!error)
998			break;
999
1000		switch (error) {
1001		case -EBUSY:
1002			fs_err(sdp, "waiting for recovery before freeze\n");
1003			break;
1004
1005		default:
1006			fs_err(sdp, "error freezing FS: %d\n", error);
1007			break;
1008		}
1009
1010		fs_err(sdp, "retrying...\n");
1011		msleep(1000);
1012	}
1013	error = 0;
1014out:
1015	mutex_unlock(&sdp->sd_freeze_mutex);
1016	return error;
1017}
1018
1019/**
1020 * gfs2_unfreeze - reallow writes to the filesystem
1021 * @sb: the VFS structure for the filesystem
1022 *
1023 */
1024
1025static int gfs2_unfreeze(struct super_block *sb)
1026{
1027	struct gfs2_sbd *sdp = sb->s_fs_info;
1028
1029	mutex_lock(&sdp->sd_freeze_mutex);
1030        if (atomic_read(&sdp->sd_freeze_state) != SFS_FROZEN ||
1031	    sdp->sd_freeze_gh.gh_gl == NULL) {
1032		mutex_unlock(&sdp->sd_freeze_mutex);
1033                return 0;
1034	}
1035
1036	gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
1037	mutex_unlock(&sdp->sd_freeze_mutex);
1038	return 0;
1039}
1040
1041/**
1042 * statfs_fill - fill in the sg for a given RG
1043 * @rgd: the RG
1044 * @sc: the sc structure
1045 *
1046 * Returns: 0 on success, -ESTALE if the LVB is invalid
1047 */
1048
1049static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
1050			    struct gfs2_statfs_change_host *sc)
1051{
1052	gfs2_rgrp_verify(rgd);
1053	sc->sc_total += rgd->rd_data;
1054	sc->sc_free += rgd->rd_free;
1055	sc->sc_dinodes += rgd->rd_dinodes;
1056	return 0;
1057}
1058
1059/**
1060 * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
1061 * @sdp: the filesystem
1062 * @sc: the sc info that will be returned
1063 *
1064 * Any error (other than a signal) will cause this routine to fall back
1065 * to the synchronous version.
1066 *
1067 * FIXME: This really shouldn't busy wait like this.
1068 *
1069 * Returns: errno
1070 */
1071
1072static int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
1073{
1074	struct gfs2_rgrpd *rgd_next;
1075	struct gfs2_holder *gha, *gh;
1076	unsigned int slots = 64;
1077	unsigned int x;
1078	int done;
1079	int error = 0, err;
1080
1081	memset(sc, 0, sizeof(struct gfs2_statfs_change_host));
1082	gha = kcalloc(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
1083	if (!gha)
1084		return -ENOMEM;
1085
1086	rgd_next = gfs2_rgrpd_get_first(sdp);
1087
1088	for (;;) {
1089		done = 1;
1090
1091		for (x = 0; x < slots; x++) {
1092			gh = gha + x;
1093
1094			if (gh->gh_gl && gfs2_glock_poll(gh)) {
1095				err = gfs2_glock_wait(gh);
1096				if (err) {
1097					gfs2_holder_uninit(gh);
1098					error = err;
1099				} else {
1100					if (!error)
1101						error = statfs_slow_fill(
1102							gh->gh_gl->gl_object, sc);
1103					gfs2_glock_dq_uninit(gh);
1104				}
1105			}
1106
1107			if (gh->gh_gl)
1108				done = 0;
1109			else if (rgd_next && !error) {
1110				error = gfs2_glock_nq_init(rgd_next->rd_gl,
1111							   LM_ST_SHARED,
1112							   GL_ASYNC,
1113							   gh);
1114				rgd_next = gfs2_rgrpd_get_next(rgd_next);
1115				done = 0;
1116			}
1117
1118			if (signal_pending(current))
1119				error = -ERESTARTSYS;
1120		}
1121
1122		if (done)
1123			break;
1124
1125		yield();
1126	}
1127
1128	kfree(gha);
1129	return error;
1130}
1131
1132/**
1133 * gfs2_statfs_i - Do a statfs
1134 * @sdp: the filesystem
1135 * @sg: the sg structure
1136 *
1137 * Returns: errno
1138 */
1139
1140static int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
1141{
1142	struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
1143	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
1144
1145	spin_lock(&sdp->sd_statfs_spin);
1146
1147	*sc = *m_sc;
1148	sc->sc_total += l_sc->sc_total;
1149	sc->sc_free += l_sc->sc_free;
1150	sc->sc_dinodes += l_sc->sc_dinodes;
1151
1152	spin_unlock(&sdp->sd_statfs_spin);
1153
1154	if (sc->sc_free < 0)
1155		sc->sc_free = 0;
1156	if (sc->sc_free > sc->sc_total)
1157		sc->sc_free = sc->sc_total;
1158	if (sc->sc_dinodes < 0)
1159		sc->sc_dinodes = 0;
1160
1161	return 0;
1162}
1163
1164/**
1165 * gfs2_statfs - Gather and return stats about the filesystem
1166 * @sb: The superblock
1167 * @statfsbuf: The buffer
1168 *
1169 * Returns: 0 on success or error code
1170 */
1171
1172static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
1173{
1174	struct super_block *sb = d_inode(dentry)->i_sb;
1175	struct gfs2_sbd *sdp = sb->s_fs_info;
1176	struct gfs2_statfs_change_host sc;
1177	int error;
1178
1179	error = gfs2_rindex_update(sdp);
1180	if (error)
1181		return error;
1182
1183	if (gfs2_tune_get(sdp, gt_statfs_slow))
1184		error = gfs2_statfs_slow(sdp, &sc);
1185	else
1186		error = gfs2_statfs_i(sdp, &sc);
1187
1188	if (error)
1189		return error;
1190
1191	buf->f_type = GFS2_MAGIC;
1192	buf->f_bsize = sdp->sd_sb.sb_bsize;
1193	buf->f_blocks = sc.sc_total;
1194	buf->f_bfree = sc.sc_free;
1195	buf->f_bavail = sc.sc_free;
1196	buf->f_files = sc.sc_dinodes + sc.sc_free;
1197	buf->f_ffree = sc.sc_free;
1198	buf->f_namelen = GFS2_FNAMESIZE;
1199
1200	return 0;
1201}
1202
1203/**
1204 * gfs2_remount_fs - called when the FS is remounted
1205 * @sb:  the filesystem
1206 * @flags:  the remount flags
1207 * @data:  extra data passed in (not used right now)
1208 *
1209 * Returns: errno
1210 */
1211
1212static int gfs2_remount_fs(struct super_block *sb, int *flags, char *data)
1213{
1214	struct gfs2_sbd *sdp = sb->s_fs_info;
1215	struct gfs2_args args = sdp->sd_args; /* Default to current settings */
1216	struct gfs2_tune *gt = &sdp->sd_tune;
1217	int error;
1218
1219	sync_filesystem(sb);
1220
1221	spin_lock(&gt->gt_spin);
1222	args.ar_commit = gt->gt_logd_secs;
1223	args.ar_quota_quantum = gt->gt_quota_quantum;
1224	if (gt->gt_statfs_slow)
1225		args.ar_statfs_quantum = 0;
1226	else
1227		args.ar_statfs_quantum = gt->gt_statfs_quantum;
1228	spin_unlock(&gt->gt_spin);
1229	error = gfs2_mount_args(&args, data);
1230	if (error)
1231		return error;
1232
1233	/* Not allowed to change locking details */
1234	if (strcmp(args.ar_lockproto, sdp->sd_args.ar_lockproto) ||
1235	    strcmp(args.ar_locktable, sdp->sd_args.ar_locktable) ||
1236	    strcmp(args.ar_hostdata, sdp->sd_args.ar_hostdata))
1237		return -EINVAL;
1238
1239	/* Some flags must not be changed */
1240	if (args_neq(&args, &sdp->sd_args, spectator) ||
1241	    args_neq(&args, &sdp->sd_args, localflocks) ||
1242	    args_neq(&args, &sdp->sd_args, meta))
1243		return -EINVAL;
1244
1245	if (sdp->sd_args.ar_spectator)
1246		*flags |= MS_RDONLY;
1247
1248	if ((sb->s_flags ^ *flags) & MS_RDONLY) {
1249		if (*flags & MS_RDONLY)
1250			error = gfs2_make_fs_ro(sdp);
1251		else
1252			error = gfs2_make_fs_rw(sdp);
1253		if (error)
1254			return error;
1255	}
1256
1257	sdp->sd_args = args;
1258	if (sdp->sd_args.ar_posix_acl)
1259		sb->s_flags |= MS_POSIXACL;
1260	else
1261		sb->s_flags &= ~MS_POSIXACL;
1262	if (sdp->sd_args.ar_nobarrier)
1263		set_bit(SDF_NOBARRIERS, &sdp->sd_flags);
1264	else
1265		clear_bit(SDF_NOBARRIERS, &sdp->sd_flags);
1266	spin_lock(&gt->gt_spin);
1267	gt->gt_logd_secs = args.ar_commit;
1268	gt->gt_quota_quantum = args.ar_quota_quantum;
1269	if (args.ar_statfs_quantum) {
1270		gt->gt_statfs_slow = 0;
1271		gt->gt_statfs_quantum = args.ar_statfs_quantum;
1272	}
1273	else {
1274		gt->gt_statfs_slow = 1;
1275		gt->gt_statfs_quantum = 30;
1276	}
1277	spin_unlock(&gt->gt_spin);
1278
1279	gfs2_online_uevent(sdp);
1280	return 0;
1281}
1282
1283/**
1284 * gfs2_drop_inode - Drop an inode (test for remote unlink)
1285 * @inode: The inode to drop
1286 *
1287 * If we've received a callback on an iopen lock then its because a
1288 * remote node tried to deallocate the inode but failed due to this node
1289 * still having the inode open. Here we mark the link count zero
1290 * since we know that it must have reached zero if the GLF_DEMOTE flag
1291 * is set on the iopen glock. If we didn't do a disk read since the
1292 * remote node removed the final link then we might otherwise miss
1293 * this event. This check ensures that this node will deallocate the
1294 * inode's blocks, or alternatively pass the baton on to another
1295 * node for later deallocation.
1296 */
1297
1298static int gfs2_drop_inode(struct inode *inode)
1299{
1300	struct gfs2_inode *ip = GFS2_I(inode);
1301
1302	if (!test_bit(GIF_FREE_VFS_INODE, &ip->i_flags) && inode->i_nlink) {
1303		struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
1304		if (gl && test_bit(GLF_DEMOTE, &gl->gl_flags))
1305			clear_nlink(inode);
1306	}
1307	return generic_drop_inode(inode);
1308}
1309
1310static int is_ancestor(const struct dentry *d1, const struct dentry *d2)
1311{
1312	do {
1313		if (d1 == d2)
1314			return 1;
1315		d1 = d1->d_parent;
1316	} while (!IS_ROOT(d1));
1317	return 0;
1318}
1319
1320/**
1321 * gfs2_show_options - Show mount options for /proc/mounts
1322 * @s: seq_file structure
1323 * @root: root of this (sub)tree
1324 *
1325 * Returns: 0 on success or error code
1326 */
1327
1328static int gfs2_show_options(struct seq_file *s, struct dentry *root)
1329{
1330	struct gfs2_sbd *sdp = root->d_sb->s_fs_info;
1331	struct gfs2_args *args = &sdp->sd_args;
1332	int val;
1333
1334	if (is_ancestor(root, sdp->sd_master_dir))
1335		seq_puts(s, ",meta");
1336	if (args->ar_lockproto[0])
1337		seq_show_option(s, "lockproto", args->ar_lockproto);
1338	if (args->ar_locktable[0])
1339		seq_show_option(s, "locktable", args->ar_locktable);
1340	if (args->ar_hostdata[0])
1341		seq_show_option(s, "hostdata", args->ar_hostdata);
1342	if (args->ar_spectator)
1343		seq_puts(s, ",spectator");
1344	if (args->ar_localflocks)
1345		seq_puts(s, ",localflocks");
1346	if (args->ar_debug)
1347		seq_puts(s, ",debug");
1348	if (args->ar_posix_acl)
1349		seq_puts(s, ",acl");
1350	if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
1351		char *state;
1352		switch (args->ar_quota) {
1353		case GFS2_QUOTA_OFF:
1354			state = "off";
1355			break;
1356		case GFS2_QUOTA_ACCOUNT:
1357			state = "account";
1358			break;
1359		case GFS2_QUOTA_ON:
1360			state = "on";
1361			break;
1362		default:
1363			state = "unknown";
1364			break;
1365		}
1366		seq_printf(s, ",quota=%s", state);
1367	}
1368	if (args->ar_suiddir)
1369		seq_puts(s, ",suiddir");
1370	if (args->ar_data != GFS2_DATA_DEFAULT) {
1371		char *state;
1372		switch (args->ar_data) {
1373		case GFS2_DATA_WRITEBACK:
1374			state = "writeback";
1375			break;
1376		case GFS2_DATA_ORDERED:
1377			state = "ordered";
1378			break;
1379		default:
1380			state = "unknown";
1381			break;
1382		}
1383		seq_printf(s, ",data=%s", state);
1384	}
1385	if (args->ar_discard)
1386		seq_puts(s, ",discard");
1387	val = sdp->sd_tune.gt_logd_secs;
1388	if (val != 30)
1389		seq_printf(s, ",commit=%d", val);
1390	val = sdp->sd_tune.gt_statfs_quantum;
1391	if (val != 30)
1392		seq_printf(s, ",statfs_quantum=%d", val);
1393	else if (sdp->sd_tune.gt_statfs_slow)
1394		seq_puts(s, ",statfs_quantum=0");
1395	val = sdp->sd_tune.gt_quota_quantum;
1396	if (val != 60)
1397		seq_printf(s, ",quota_quantum=%d", val);
1398	if (args->ar_statfs_percent)
1399		seq_printf(s, ",statfs_percent=%d", args->ar_statfs_percent);
1400	if (args->ar_errors != GFS2_ERRORS_DEFAULT) {
1401		const char *state;
1402
1403		switch (args->ar_errors) {
1404		case GFS2_ERRORS_WITHDRAW:
1405			state = "withdraw";
1406			break;
1407		case GFS2_ERRORS_PANIC:
1408			state = "panic";
1409			break;
1410		default:
1411			state = "unknown";
1412			break;
1413		}
1414		seq_printf(s, ",errors=%s", state);
1415	}
1416	if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
1417		seq_puts(s, ",nobarrier");
1418	if (test_bit(SDF_DEMOTE, &sdp->sd_flags))
1419		seq_puts(s, ",demote_interface_used");
1420	if (args->ar_rgrplvb)
1421		seq_puts(s, ",rgrplvb");
1422	return 0;
1423}
1424
1425static void gfs2_final_release_pages(struct gfs2_inode *ip)
1426{
1427	struct inode *inode = &ip->i_inode;
1428	struct gfs2_glock *gl = ip->i_gl;
1429
1430	truncate_inode_pages(gfs2_glock2aspace(ip->i_gl), 0);
1431	truncate_inode_pages(&inode->i_data, 0);
1432
1433	if (atomic_read(&gl->gl_revokes) == 0) {
1434		clear_bit(GLF_LFLUSH, &gl->gl_flags);
1435		clear_bit(GLF_DIRTY, &gl->gl_flags);
1436	}
1437}
1438
1439static int gfs2_dinode_dealloc(struct gfs2_inode *ip)
1440{
1441	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1442	struct gfs2_rgrpd *rgd;
1443	struct gfs2_holder gh;
1444	int error;
1445
1446	if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
1447		gfs2_consist_inode(ip);
1448		return -EIO;
1449	}
1450
1451	error = gfs2_rindex_update(sdp);
1452	if (error)
1453		return error;
1454
1455	error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
1456	if (error)
1457		return error;
1458
1459	rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
1460	if (!rgd) {
1461		gfs2_consist_inode(ip);
1462		error = -EIO;
1463		goto out_qs;
1464	}
1465
1466	error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
1467	if (error)
1468		goto out_qs;
1469
1470	error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA,
1471				 sdp->sd_jdesc->jd_blocks);
1472	if (error)
1473		goto out_rg_gunlock;
1474
1475	gfs2_free_di(rgd, ip);
1476
1477	gfs2_final_release_pages(ip);
1478
1479	gfs2_trans_end(sdp);
1480
1481out_rg_gunlock:
1482	gfs2_glock_dq_uninit(&gh);
1483out_qs:
1484	gfs2_quota_unhold(ip);
1485	return error;
1486}
1487
1488/**
1489 * gfs2_evict_inode - Remove an inode from cache
1490 * @inode: The inode to evict
1491 *
1492 * There are three cases to consider:
1493 * 1. i_nlink == 0, we are final opener (and must deallocate)
1494 * 2. i_nlink == 0, we are not the final opener (and cannot deallocate)
1495 * 3. i_nlink > 0
1496 *
1497 * If the fs is read only, then we have to treat all cases as per #3
1498 * since we are unable to do any deallocation. The inode will be
1499 * deallocated by the next read/write node to attempt an allocation
1500 * in the same resource group
1501 *
1502 * We have to (at the moment) hold the inodes main lock to cover
1503 * the gap between unlocking the shared lock on the iopen lock and
1504 * taking the exclusive lock. I'd rather do a shared -> exclusive
1505 * conversion on the iopen lock, but we can change that later. This
1506 * is safe, just less efficient.
1507 */
1508
1509static void gfs2_evict_inode(struct inode *inode)
1510{
1511	struct super_block *sb = inode->i_sb;
1512	struct gfs2_sbd *sdp = sb->s_fs_info;
1513	struct gfs2_inode *ip = GFS2_I(inode);
1514	struct gfs2_holder gh;
1515	int error;
1516
1517	if (test_bit(GIF_FREE_VFS_INODE, &ip->i_flags)) {
1518		clear_inode(inode);
1519		return;
1520	}
1521
1522	if (inode->i_nlink || (sb->s_flags & MS_RDONLY))
1523		goto out;
1524
1525	/* Must not read inode block until block type has been verified */
1526	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, &gh);
1527	if (unlikely(error)) {
1528		ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
1529		gfs2_glock_dq_uninit(&ip->i_iopen_gh);
1530		goto out;
1531	}
1532
1533	if (!test_bit(GIF_ALLOC_FAILED, &ip->i_flags)) {
1534		error = gfs2_check_blk_type(sdp, ip->i_no_addr, GFS2_BLKST_UNLINKED);
1535		if (error)
1536			goto out_truncate;
1537	}
1538
1539	if (test_bit(GIF_INVALID, &ip->i_flags)) {
1540		error = gfs2_inode_refresh(ip);
1541		if (error)
1542			goto out_truncate;
1543	}
1544
1545	ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
1546	gfs2_glock_dq_wait(&ip->i_iopen_gh);
1547	gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &ip->i_iopen_gh);
1548	error = gfs2_glock_nq(&ip->i_iopen_gh);
1549	if (error)
1550		goto out_truncate;
1551
1552	/* Case 1 starts here */
1553
1554	if (S_ISDIR(inode->i_mode) &&
1555	    (ip->i_diskflags & GFS2_DIF_EXHASH)) {
1556		error = gfs2_dir_exhash_dealloc(ip);
1557		if (error)
1558			goto out_unlock;
1559	}
1560
1561	if (ip->i_eattr) {
1562		error = gfs2_ea_dealloc(ip);
1563		if (error)
1564			goto out_unlock;
1565	}
1566
1567	if (!gfs2_is_stuffed(ip)) {
1568		error = gfs2_file_dealloc(ip);
1569		if (error)
1570			goto out_unlock;
1571	}
1572
1573	error = gfs2_dinode_dealloc(ip);
1574	goto out_unlock;
1575
1576out_truncate:
1577	gfs2_log_flush(sdp, ip->i_gl, NORMAL_FLUSH);
1578	if (test_bit(GLF_DIRTY, &ip->i_gl->gl_flags)) {
1579		struct address_space *metamapping = gfs2_glock2aspace(ip->i_gl);
1580		filemap_fdatawrite(metamapping);
1581		filemap_fdatawait(metamapping);
1582	}
1583	write_inode_now(inode, 1);
1584	gfs2_ail_flush(ip->i_gl, 0);
1585
1586	/* Case 2 starts here */
1587	error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
1588	if (error)
1589		goto out_unlock;
1590	/* Needs to be done before glock release & also in a transaction */
1591	truncate_inode_pages(&inode->i_data, 0);
1592	gfs2_trans_end(sdp);
1593
1594out_unlock:
1595	/* Error path for case 1 */
1596	if (gfs2_rs_active(ip->i_res))
1597		gfs2_rs_deltree(ip->i_res);
1598
1599	if (test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags)) {
1600		ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
1601		gfs2_glock_dq(&ip->i_iopen_gh);
1602	}
1603	gfs2_holder_uninit(&ip->i_iopen_gh);
1604	gfs2_glock_dq_uninit(&gh);
1605	if (error && error != GLR_TRYFAILED && error != -EROFS)
1606		fs_warn(sdp, "gfs2_evict_inode: %d\n", error);
1607out:
1608	/* Case 3 starts here */
1609	truncate_inode_pages_final(&inode->i_data);
1610	gfs2_rs_delete(ip, NULL);
1611	gfs2_ordered_del_inode(ip);
1612	clear_inode(inode);
1613	gfs2_dir_hash_inval(ip);
1614	ip->i_gl->gl_object = NULL;
1615	flush_delayed_work(&ip->i_gl->gl_work);
1616	gfs2_glock_add_to_lru(ip->i_gl);
1617	gfs2_glock_put(ip->i_gl);
1618	ip->i_gl = NULL;
1619	if (ip->i_iopen_gh.gh_gl) {
1620		ip->i_iopen_gh.gh_gl->gl_object = NULL;
1621		ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
1622		gfs2_glock_dq_uninit(&ip->i_iopen_gh);
1623	}
1624}
1625
1626static struct inode *gfs2_alloc_inode(struct super_block *sb)
1627{
1628	struct gfs2_inode *ip;
1629
1630	ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
1631	if (ip) {
1632		ip->i_flags = 0;
1633		ip->i_gl = NULL;
1634		ip->i_rgd = NULL;
1635		ip->i_res = NULL;
1636	}
1637	return &ip->i_inode;
1638}
1639
1640static void gfs2_i_callback(struct rcu_head *head)
1641{
1642	struct inode *inode = container_of(head, struct inode, i_rcu);
1643	kmem_cache_free(gfs2_inode_cachep, inode);
1644}
1645
1646static void gfs2_destroy_inode(struct inode *inode)
1647{
1648	call_rcu(&inode->i_rcu, gfs2_i_callback);
1649}
1650
1651const struct super_operations gfs2_super_ops = {
1652	.alloc_inode		= gfs2_alloc_inode,
1653	.destroy_inode		= gfs2_destroy_inode,
1654	.write_inode		= gfs2_write_inode,
1655	.dirty_inode		= gfs2_dirty_inode,
1656	.evict_inode		= gfs2_evict_inode,
1657	.put_super		= gfs2_put_super,
1658	.sync_fs		= gfs2_sync_fs,
1659	.freeze_super		= gfs2_freeze,
1660	.thaw_super		= gfs2_unfreeze,
1661	.statfs			= gfs2_statfs,
1662	.remount_fs		= gfs2_remount_fs,
1663	.drop_inode		= gfs2_drop_inode,
1664	.show_options		= gfs2_show_options,
1665};
1666
1667