1/*
2 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
8 */
9
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/spinlock.h>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
15#include <linux/statfs.h>
16#include <linux/seq_file.h>
17#include <linux/mount.h>
18#include <linux/kthread.h>
19#include <linux/delay.h>
20#include <linux/gfs2_ondisk.h>
21#include <linux/crc32.h>
22#include <linux/lm_interface.h>
23
24#include "gfs2.h"
25#include "incore.h"
26#include "glock.h"
27#include "inode.h"
28#include "lm.h"
29#include "log.h"
30#include "mount.h"
31#include "ops_super.h"
32#include "quota.h"
33#include "recovery.h"
34#include "rgrp.h"
35#include "super.h"
36#include "sys.h"
37#include "util.h"
38#include "trans.h"
39#include "dir.h"
40#include "eattr.h"
41#include "bmap.h"
42
43/**
44 * gfs2_write_inode - Make sure the inode is stable on the disk
45 * @inode: The inode
46 * @sync: synchronous write flag
47 *
48 * Returns: errno
49 */
50
51static int gfs2_write_inode(struct inode *inode, int sync)
52{
53	struct gfs2_inode *ip = GFS2_I(inode);
54
55	/* Check this is a "normal" inode */
56	if (inode->i_private) {
57		if (current->flags & PF_MEMALLOC)
58			return 0;
59		if (sync)
60			gfs2_log_flush(GFS2_SB(inode), ip->i_gl);
61	}
62
63	return 0;
64}
65
66/**
67 * gfs2_put_super - Unmount the filesystem
68 * @sb: The VFS superblock
69 *
70 */
71
72static void gfs2_put_super(struct super_block *sb)
73{
74	struct gfs2_sbd *sdp = sb->s_fs_info;
75	int error;
76
77	if (!sdp)
78		return;
79
80	if (!strncmp(sb->s_type->name, "gfs2meta", 8))
81		return; /* Nothing to do */
82
83	/*  Unfreeze the filesystem, if we need to  */
84
85	mutex_lock(&sdp->sd_freeze_lock);
86	if (sdp->sd_freeze_count)
87		gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
88	mutex_unlock(&sdp->sd_freeze_lock);
89
90	kthread_stop(sdp->sd_quotad_process);
91	kthread_stop(sdp->sd_logd_process);
92	kthread_stop(sdp->sd_recoverd_process);
93	while (sdp->sd_glockd_num--)
94		kthread_stop(sdp->sd_glockd_process[sdp->sd_glockd_num]);
95	kthread_stop(sdp->sd_scand_process);
96
97	if (!(sb->s_flags & MS_RDONLY)) {
98		error = gfs2_make_fs_ro(sdp);
99		if (error)
100			gfs2_io_error(sdp);
101	}
102	/*  At this point, we're through modifying the disk  */
103
104	/*  Release stuff  */
105
106	iput(sdp->sd_master_dir);
107	iput(sdp->sd_jindex);
108	iput(sdp->sd_inum_inode);
109	iput(sdp->sd_statfs_inode);
110	iput(sdp->sd_rindex);
111	iput(sdp->sd_quota_inode);
112
113	gfs2_glock_put(sdp->sd_rename_gl);
114	gfs2_glock_put(sdp->sd_trans_gl);
115
116	if (!sdp->sd_args.ar_spectator) {
117		gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
118		gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
119		gfs2_glock_dq_uninit(&sdp->sd_ir_gh);
120		gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
121		gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
122		iput(sdp->sd_ir_inode);
123		iput(sdp->sd_sc_inode);
124		iput(sdp->sd_qc_inode);
125	}
126
127	gfs2_glock_dq_uninit(&sdp->sd_live_gh);
128	gfs2_clear_rgrpd(sdp);
129	gfs2_jindex_free(sdp);
130	/*  Take apart glock structures and buffer lists  */
131	gfs2_gl_hash_clear(sdp, WAIT);
132	/*  Unmount the locking protocol  */
133	gfs2_lm_unmount(sdp);
134
135	/*  At this point, we're through participating in the lockspace  */
136	gfs2_sys_fs_del(sdp);
137	kfree(sdp);
138}
139
140/**
141 * gfs2_write_super
142 * @sb: the superblock
143 *
144 */
145
146static void gfs2_write_super(struct super_block *sb)
147{
148	sb->s_dirt = 0;
149}
150
151/**
152 * gfs2_sync_fs - sync the filesystem
153 * @sb: the superblock
154 *
155 * Flushes the log to disk.
156 */
157static int gfs2_sync_fs(struct super_block *sb, int wait)
158{
159	sb->s_dirt = 0;
160	if (wait)
161		gfs2_log_flush(sb->s_fs_info, NULL);
162	return 0;
163}
164
165/**
166 * gfs2_write_super_lockfs - prevent further writes to the filesystem
167 * @sb: the VFS structure for the filesystem
168 *
169 */
170
171static void gfs2_write_super_lockfs(struct super_block *sb)
172{
173	struct gfs2_sbd *sdp = sb->s_fs_info;
174	int error;
175
176	if (test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
177		return;
178
179	for (;;) {
180		error = gfs2_freeze_fs(sdp);
181		if (!error)
182			break;
183
184		switch (error) {
185		case -EBUSY:
186			fs_err(sdp, "waiting for recovery before freeze\n");
187			break;
188
189		default:
190			fs_err(sdp, "error freezing FS: %d\n", error);
191			break;
192		}
193
194		fs_err(sdp, "retrying...\n");
195		msleep(1000);
196	}
197}
198
199/**
200 * gfs2_unlockfs - reallow writes to the filesystem
201 * @sb: the VFS structure for the filesystem
202 *
203 */
204
205static void gfs2_unlockfs(struct super_block *sb)
206{
207	gfs2_unfreeze_fs(sb->s_fs_info);
208}
209
210/**
211 * gfs2_statfs - Gather and return stats about the filesystem
212 * @sb: The superblock
213 * @statfsbuf: The buffer
214 *
215 * Returns: 0 on success or error code
216 */
217
218static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
219{
220	struct super_block *sb = dentry->d_inode->i_sb;
221	struct gfs2_sbd *sdp = sb->s_fs_info;
222	struct gfs2_statfs_change_host sc;
223	int error;
224
225	if (gfs2_tune_get(sdp, gt_statfs_slow))
226		error = gfs2_statfs_slow(sdp, &sc);
227	else
228		error = gfs2_statfs_i(sdp, &sc);
229
230	if (error)
231		return error;
232
233	buf->f_type = GFS2_MAGIC;
234	buf->f_bsize = sdp->sd_sb.sb_bsize;
235	buf->f_blocks = sc.sc_total;
236	buf->f_bfree = sc.sc_free;
237	buf->f_bavail = sc.sc_free;
238	buf->f_files = sc.sc_dinodes + sc.sc_free;
239	buf->f_ffree = sc.sc_free;
240	buf->f_namelen = GFS2_FNAMESIZE;
241
242	return 0;
243}
244
245/**
246 * gfs2_remount_fs - called when the FS is remounted
247 * @sb:  the filesystem
248 * @flags:  the remount flags
249 * @data:  extra data passed in (not used right now)
250 *
251 * Returns: errno
252 */
253
254static int gfs2_remount_fs(struct super_block *sb, int *flags, char *data)
255{
256	struct gfs2_sbd *sdp = sb->s_fs_info;
257	int error;
258
259	error = gfs2_mount_args(sdp, data, 1);
260	if (error)
261		return error;
262
263	if (sdp->sd_args.ar_spectator)
264		*flags |= MS_RDONLY;
265	else {
266		if (*flags & MS_RDONLY) {
267			if (!(sb->s_flags & MS_RDONLY))
268				error = gfs2_make_fs_ro(sdp);
269		} else if (!(*flags & MS_RDONLY) &&
270			   (sb->s_flags & MS_RDONLY)) {
271			error = gfs2_make_fs_rw(sdp);
272		}
273	}
274
275	if (*flags & (MS_NOATIME | MS_NODIRATIME))
276		set_bit(SDF_NOATIME, &sdp->sd_flags);
277	else
278		clear_bit(SDF_NOATIME, &sdp->sd_flags);
279
280	/* Don't let the VFS update atimes.  GFS2 handles this itself. */
281	*flags |= MS_NOATIME | MS_NODIRATIME;
282
283	return error;
284}
285
286/**
287 * gfs2_drop_inode - Drop an inode (test for remote unlink)
288 * @inode: The inode to drop
289 *
290 * If we've received a callback on an iopen lock then its because a
291 * remote node tried to deallocate the inode but failed due to this node
292 * still having the inode open. Here we mark the link count zero
293 * since we know that it must have reached zero if the GLF_DEMOTE flag
294 * is set on the iopen glock. If we didn't do a disk read since the
295 * remote node removed the final link then we might otherwise miss
296 * this event. This check ensures that this node will deallocate the
297 * inode's blocks, or alternatively pass the baton on to another
298 * node for later deallocation.
299 */
300static void gfs2_drop_inode(struct inode *inode)
301{
302	if (inode->i_private && inode->i_nlink) {
303		struct gfs2_inode *ip = GFS2_I(inode);
304		struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
305		if (gl && test_bit(GLF_DEMOTE, &gl->gl_flags))
306			clear_nlink(inode);
307	}
308	generic_drop_inode(inode);
309}
310
311/**
312 * gfs2_clear_inode - Deallocate an inode when VFS is done with it
313 * @inode: The VFS inode
314 *
315 */
316
317static void gfs2_clear_inode(struct inode *inode)
318{
319	/* This tells us its a "real" inode and not one which only
320	 * serves to contain an address space (see rgrp.c, meta_io.c)
321	 * which therefore doesn't have its own glocks.
322	 */
323	if (inode->i_private) {
324		struct gfs2_inode *ip = GFS2_I(inode);
325		ip->i_gl->gl_object = NULL;
326		gfs2_glock_schedule_for_reclaim(ip->i_gl);
327		gfs2_glock_put(ip->i_gl);
328		ip->i_gl = NULL;
329		if (ip->i_iopen_gh.gh_gl)
330			gfs2_glock_dq_uninit(&ip->i_iopen_gh);
331	}
332}
333
334/**
335 * gfs2_show_options - Show mount options for /proc/mounts
336 * @s: seq_file structure
337 * @mnt: vfsmount
338 *
339 * Returns: 0 on success or error code
340 */
341
342static int gfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
343{
344	struct gfs2_sbd *sdp = mnt->mnt_sb->s_fs_info;
345	struct gfs2_args *args = &sdp->sd_args;
346
347	if (args->ar_lockproto[0])
348		seq_printf(s, ",lockproto=%s", args->ar_lockproto);
349	if (args->ar_locktable[0])
350		seq_printf(s, ",locktable=%s", args->ar_locktable);
351	if (args->ar_hostdata[0])
352		seq_printf(s, ",hostdata=%s", args->ar_hostdata);
353	if (args->ar_spectator)
354		seq_printf(s, ",spectator");
355	if (args->ar_ignore_local_fs)
356		seq_printf(s, ",ignore_local_fs");
357	if (args->ar_localflocks)
358		seq_printf(s, ",localflocks");
359	if (args->ar_localcaching)
360		seq_printf(s, ",localcaching");
361	if (args->ar_debug)
362		seq_printf(s, ",debug");
363	if (args->ar_upgrade)
364		seq_printf(s, ",upgrade");
365	if (args->ar_num_glockd != GFS2_GLOCKD_DEFAULT)
366		seq_printf(s, ",num_glockd=%u", args->ar_num_glockd);
367	if (args->ar_posix_acl)
368		seq_printf(s, ",acl");
369	if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
370		char *state;
371		switch (args->ar_quota) {
372		case GFS2_QUOTA_OFF:
373			state = "off";
374			break;
375		case GFS2_QUOTA_ACCOUNT:
376			state = "account";
377			break;
378		case GFS2_QUOTA_ON:
379			state = "on";
380			break;
381		default:
382			state = "unknown";
383			break;
384		}
385		seq_printf(s, ",quota=%s", state);
386	}
387	if (args->ar_suiddir)
388		seq_printf(s, ",suiddir");
389	if (args->ar_data != GFS2_DATA_DEFAULT) {
390		char *state;
391		switch (args->ar_data) {
392		case GFS2_DATA_WRITEBACK:
393			state = "writeback";
394			break;
395		case GFS2_DATA_ORDERED:
396			state = "ordered";
397			break;
398		default:
399			state = "unknown";
400			break;
401		}
402		seq_printf(s, ",data=%s", state);
403	}
404
405	return 0;
406}
407
408/*
409 * We have to (at the moment) hold the inodes main lock to cover
410 * the gap between unlocking the shared lock on the iopen lock and
411 * taking the exclusive lock. I'd rather do a shared -> exclusive
412 * conversion on the iopen lock, but we can change that later. This
413 * is safe, just less efficient.
414 */
415static void gfs2_delete_inode(struct inode *inode)
416{
417	struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
418	struct gfs2_inode *ip = GFS2_I(inode);
419	struct gfs2_holder gh;
420	int error;
421
422	if (!inode->i_private)
423		goto out;
424
425	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB, &gh);
426	if (unlikely(error)) {
427		gfs2_glock_dq_uninit(&ip->i_iopen_gh);
428		goto out;
429	}
430
431	gfs2_glock_dq(&ip->i_iopen_gh);
432	gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &ip->i_iopen_gh);
433	error = gfs2_glock_nq(&ip->i_iopen_gh);
434	if (error)
435		goto out_uninit;
436
437	if (S_ISDIR(inode->i_mode) &&
438	    (ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
439		error = gfs2_dir_exhash_dealloc(ip);
440		if (error)
441			goto out_unlock;
442	}
443
444	if (ip->i_di.di_eattr) {
445		error = gfs2_ea_dealloc(ip);
446		if (error)
447			goto out_unlock;
448	}
449
450	if (!gfs2_is_stuffed(ip)) {
451		error = gfs2_file_dealloc(ip);
452		if (error)
453			goto out_unlock;
454	}
455
456	error = gfs2_dinode_dealloc(ip);
457	/*
458	 * Must do this before unlock to avoid trying to write back
459	 * potentially dirty data now that inode no longer exists
460	 * on disk.
461	 */
462	truncate_inode_pages(&inode->i_data, 0);
463
464out_unlock:
465	gfs2_glock_dq(&ip->i_iopen_gh);
466out_uninit:
467	gfs2_holder_uninit(&ip->i_iopen_gh);
468	gfs2_glock_dq_uninit(&gh);
469	if (error && error != GLR_TRYFAILED)
470		fs_warn(sdp, "gfs2_delete_inode: %d\n", error);
471out:
472	truncate_inode_pages(&inode->i_data, 0);
473	clear_inode(inode);
474}
475
476
477
478static struct inode *gfs2_alloc_inode(struct super_block *sb)
479{
480	struct gfs2_inode *ip;
481
482	ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
483	if (ip) {
484		ip->i_flags = 0;
485		ip->i_gl = NULL;
486		ip->i_last_pfault = jiffies;
487	}
488	return &ip->i_inode;
489}
490
491static void gfs2_destroy_inode(struct inode *inode)
492{
493	kmem_cache_free(gfs2_inode_cachep, inode);
494}
495
496const struct super_operations gfs2_super_ops = {
497	.alloc_inode		= gfs2_alloc_inode,
498	.destroy_inode		= gfs2_destroy_inode,
499	.write_inode		= gfs2_write_inode,
500	.delete_inode		= gfs2_delete_inode,
501	.put_super		= gfs2_put_super,
502	.write_super		= gfs2_write_super,
503	.sync_fs		= gfs2_sync_fs,
504	.write_super_lockfs 	= gfs2_write_super_lockfs,
505	.unlockfs		= gfs2_unlockfs,
506	.statfs			= gfs2_statfs,
507	.remount_fs		= gfs2_remount_fs,
508	.clear_inode		= gfs2_clear_inode,
509	.drop_inode		= gfs2_drop_inode,
510	.show_options		= gfs2_show_options,
511};
512