2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/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>
30 #include "ops_super.h"
43 * gfs2_write_inode - Make sure the inode is stable on the disk
45 * @sync: synchronous write flag
50 static int gfs2_write_inode(struct inode *inode, int sync)
52 struct gfs2_inode *ip = GFS2_I(inode);
54 /* Check this is a "normal" inode */
55 if (test_bit(GIF_USER, &ip->i_flags)) {
56 if (current->flags & PF_MEMALLOC)
59 gfs2_log_flush(GFS2_SB(inode), ip->i_gl);
66 * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
67 * @sdp: the filesystem
72 static int gfs2_make_fs_ro(struct gfs2_sbd *sdp)
74 struct gfs2_holder t_gh;
78 gfs2_statfs_sync(sdp);
80 error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, GL_NOCACHE,
82 if (error && !test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
85 gfs2_meta_syncfs(sdp);
86 gfs2_log_shutdown(sdp);
88 clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
91 gfs2_glock_dq_uninit(&t_gh);
93 gfs2_quota_cleanup(sdp);
99 * gfs2_put_super - Unmount the filesystem
100 * @sb: The VFS superblock
104 static void gfs2_put_super(struct super_block *sb)
106 struct gfs2_sbd *sdp = sb->s_fs_info;
109 /* Unfreeze the filesystem, if we need to */
111 mutex_lock(&sdp->sd_freeze_lock);
112 if (sdp->sd_freeze_count)
113 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
114 mutex_unlock(&sdp->sd_freeze_lock);
116 kthread_stop(sdp->sd_quotad_process);
117 kthread_stop(sdp->sd_logd_process);
118 kthread_stop(sdp->sd_recoverd_process);
119 while (sdp->sd_glockd_num--)
120 kthread_stop(sdp->sd_glockd_process[sdp->sd_glockd_num]);
122 if (!(sb->s_flags & MS_RDONLY)) {
123 error = gfs2_make_fs_ro(sdp);
127 /* At this point, we're through modifying the disk */
131 iput(sdp->sd_jindex);
132 iput(sdp->sd_inum_inode);
133 iput(sdp->sd_statfs_inode);
134 iput(sdp->sd_rindex);
135 iput(sdp->sd_quota_inode);
137 gfs2_glock_put(sdp->sd_rename_gl);
138 gfs2_glock_put(sdp->sd_trans_gl);
140 if (!sdp->sd_args.ar_spectator) {
141 gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
142 gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
143 gfs2_glock_dq_uninit(&sdp->sd_ir_gh);
144 gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
145 gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
146 iput(sdp->sd_ir_inode);
147 iput(sdp->sd_sc_inode);
148 iput(sdp->sd_qc_inode);
151 gfs2_glock_dq_uninit(&sdp->sd_live_gh);
152 gfs2_clear_rgrpd(sdp);
153 gfs2_jindex_free(sdp);
154 /* Take apart glock structures and buffer lists */
155 gfs2_gl_hash_clear(sdp);
156 /* Unmount the locking protocol */
157 gfs2_lm_unmount(sdp);
159 /* At this point, we're through participating in the lockspace */
160 gfs2_sys_fs_del(sdp);
166 * @sb: the superblock
170 static void gfs2_write_super(struct super_block *sb)
176 * gfs2_sync_fs - sync the filesystem
177 * @sb: the superblock
179 * Flushes the log to disk.
182 static int gfs2_sync_fs(struct super_block *sb, int wait)
185 if (wait && sb->s_fs_info)
186 gfs2_log_flush(sb->s_fs_info, NULL);
191 * gfs2_write_super_lockfs - prevent further writes to the filesystem
192 * @sb: the VFS structure for the filesystem
196 static void gfs2_write_super_lockfs(struct super_block *sb)
198 struct gfs2_sbd *sdp = sb->s_fs_info;
201 if (test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
205 error = gfs2_freeze_fs(sdp);
211 fs_err(sdp, "waiting for recovery before freeze\n");
215 fs_err(sdp, "error freezing FS: %d\n", error);
219 fs_err(sdp, "retrying...\n");
225 * gfs2_unlockfs - reallow writes to the filesystem
226 * @sb: the VFS structure for the filesystem
230 static void gfs2_unlockfs(struct super_block *sb)
232 gfs2_unfreeze_fs(sb->s_fs_info);
236 * gfs2_statfs - Gather and return stats about the filesystem
237 * @sb: The superblock
238 * @statfsbuf: The buffer
240 * Returns: 0 on success or error code
243 static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
245 struct super_block *sb = dentry->d_inode->i_sb;
246 struct gfs2_sbd *sdp = sb->s_fs_info;
247 struct gfs2_statfs_change_host sc;
250 if (gfs2_tune_get(sdp, gt_statfs_slow))
251 error = gfs2_statfs_slow(sdp, &sc);
253 error = gfs2_statfs_i(sdp, &sc);
258 buf->f_type = GFS2_MAGIC;
259 buf->f_bsize = sdp->sd_sb.sb_bsize;
260 buf->f_blocks = sc.sc_total;
261 buf->f_bfree = sc.sc_free;
262 buf->f_bavail = sc.sc_free;
263 buf->f_files = sc.sc_dinodes + sc.sc_free;
264 buf->f_ffree = sc.sc_free;
265 buf->f_namelen = GFS2_FNAMESIZE;
271 * gfs2_remount_fs - called when the FS is remounted
272 * @sb: the filesystem
273 * @flags: the remount flags
274 * @data: extra data passed in (not used right now)
279 static int gfs2_remount_fs(struct super_block *sb, int *flags, char *data)
281 struct gfs2_sbd *sdp = sb->s_fs_info;
284 error = gfs2_mount_args(sdp, data, 1);
288 if (sdp->sd_args.ar_spectator)
291 if (*flags & MS_RDONLY) {
292 if (!(sb->s_flags & MS_RDONLY))
293 error = gfs2_make_fs_ro(sdp);
294 } else if (!(*flags & MS_RDONLY) &&
295 (sb->s_flags & MS_RDONLY)) {
296 error = gfs2_make_fs_rw(sdp);
300 if (*flags & (MS_NOATIME | MS_NODIRATIME))
301 set_bit(SDF_NOATIME, &sdp->sd_flags);
303 clear_bit(SDF_NOATIME, &sdp->sd_flags);
305 /* Don't let the VFS update atimes. GFS2 handles this itself. */
306 *flags |= MS_NOATIME | MS_NODIRATIME;
312 * gfs2_drop_inode - Drop an inode (test for remote unlink)
313 * @inode: The inode to drop
315 * If we've received a callback on an iopen lock then its because a
316 * remote node tried to deallocate the inode but failed due to this node
317 * still having the inode open. Here we mark the link count zero
318 * since we know that it must have reached zero if the GLF_DEMOTE flag
319 * is set on the iopen glock. If we didn't do a disk read since the
320 * remote node removed the final link then we might otherwise miss
321 * this event. This check ensures that this node will deallocate the
322 * inode's blocks, or alternatively pass the baton on to another
323 * node for later deallocation.
326 static void gfs2_drop_inode(struct inode *inode)
328 struct gfs2_inode *ip = GFS2_I(inode);
330 if (test_bit(GIF_USER, &ip->i_flags) && inode->i_nlink) {
331 struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
332 if (gl && test_bit(GLF_DEMOTE, &gl->gl_flags))
335 generic_drop_inode(inode);
339 * gfs2_clear_inode - Deallocate an inode when VFS is done with it
340 * @inode: The VFS inode
344 static void gfs2_clear_inode(struct inode *inode)
346 struct gfs2_inode *ip = GFS2_I(inode);
348 /* This tells us its a "real" inode and not one which only
349 * serves to contain an address space (see rgrp.c, meta_io.c)
350 * which therefore doesn't have its own glocks.
352 if (test_bit(GIF_USER, &ip->i_flags)) {
353 ip->i_gl->gl_object = NULL;
354 gfs2_glock_schedule_for_reclaim(ip->i_gl);
355 gfs2_glock_put(ip->i_gl);
357 if (ip->i_iopen_gh.gh_gl) {
358 ip->i_iopen_gh.gh_gl->gl_object = NULL;
359 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
364 static int is_ancestor(const struct dentry *d1, const struct dentry *d2)
370 } while (!IS_ROOT(d1));
375 * gfs2_show_options - Show mount options for /proc/mounts
376 * @s: seq_file structure
379 * Returns: 0 on success or error code
382 static int gfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
384 struct gfs2_sbd *sdp = mnt->mnt_sb->s_fs_info;
385 struct gfs2_args *args = &sdp->sd_args;
387 if (is_ancestor(mnt->mnt_root, sdp->sd_master_dir))
388 seq_printf(s, ",meta");
389 if (args->ar_lockproto[0])
390 seq_printf(s, ",lockproto=%s", args->ar_lockproto);
391 if (args->ar_locktable[0])
392 seq_printf(s, ",locktable=%s", args->ar_locktable);
393 if (args->ar_hostdata[0])
394 seq_printf(s, ",hostdata=%s", args->ar_hostdata);
395 if (args->ar_spectator)
396 seq_printf(s, ",spectator");
397 if (args->ar_ignore_local_fs)
398 seq_printf(s, ",ignore_local_fs");
399 if (args->ar_localflocks)
400 seq_printf(s, ",localflocks");
401 if (args->ar_localcaching)
402 seq_printf(s, ",localcaching");
404 seq_printf(s, ",debug");
405 if (args->ar_upgrade)
406 seq_printf(s, ",upgrade");
407 if (args->ar_num_glockd != GFS2_GLOCKD_DEFAULT)
408 seq_printf(s, ",num_glockd=%u", args->ar_num_glockd);
409 if (args->ar_posix_acl)
410 seq_printf(s, ",acl");
411 if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
413 switch (args->ar_quota) {
417 case GFS2_QUOTA_ACCOUNT:
427 seq_printf(s, ",quota=%s", state);
429 if (args->ar_suiddir)
430 seq_printf(s, ",suiddir");
431 if (args->ar_data != GFS2_DATA_DEFAULT) {
433 switch (args->ar_data) {
434 case GFS2_DATA_WRITEBACK:
437 case GFS2_DATA_ORDERED:
444 seq_printf(s, ",data=%s", state);
451 * We have to (at the moment) hold the inodes main lock to cover
452 * the gap between unlocking the shared lock on the iopen lock and
453 * taking the exclusive lock. I'd rather do a shared -> exclusive
454 * conversion on the iopen lock, but we can change that later. This
455 * is safe, just less efficient.
458 static void gfs2_delete_inode(struct inode *inode)
460 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
461 struct gfs2_inode *ip = GFS2_I(inode);
462 struct gfs2_holder gh;
465 if (!test_bit(GIF_USER, &ip->i_flags))
468 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
469 if (unlikely(error)) {
470 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
474 gfs2_glock_dq_wait(&ip->i_iopen_gh);
475 gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &ip->i_iopen_gh);
476 error = gfs2_glock_nq(&ip->i_iopen_gh);
480 if (S_ISDIR(inode->i_mode) &&
481 (ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
482 error = gfs2_dir_exhash_dealloc(ip);
487 if (ip->i_di.di_eattr) {
488 error = gfs2_ea_dealloc(ip);
493 if (!gfs2_is_stuffed(ip)) {
494 error = gfs2_file_dealloc(ip);
499 error = gfs2_dinode_dealloc(ip);
503 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
506 /* Needs to be done before glock release & also in a transaction */
507 truncate_inode_pages(&inode->i_data, 0);
511 gfs2_glock_dq(&ip->i_iopen_gh);
513 gfs2_holder_uninit(&ip->i_iopen_gh);
514 gfs2_glock_dq_uninit(&gh);
515 if (error && error != GLR_TRYFAILED)
516 fs_warn(sdp, "gfs2_delete_inode: %d\n", error);
518 truncate_inode_pages(&inode->i_data, 0);
522 static struct inode *gfs2_alloc_inode(struct super_block *sb)
524 struct gfs2_inode *ip;
526 ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
534 static void gfs2_destroy_inode(struct inode *inode)
536 kmem_cache_free(gfs2_inode_cachep, inode);
539 const struct super_operations gfs2_super_ops = {
540 .alloc_inode = gfs2_alloc_inode,
541 .destroy_inode = gfs2_destroy_inode,
542 .write_inode = gfs2_write_inode,
543 .delete_inode = gfs2_delete_inode,
544 .put_super = gfs2_put_super,
545 .write_super = gfs2_write_super,
546 .sync_fs = gfs2_sync_fs,
547 .write_super_lockfs = gfs2_write_super_lockfs,
548 .unlockfs = gfs2_unlockfs,
549 .statfs = gfs2_statfs,
550 .remount_fs = gfs2_remount_fs,
551 .clear_inode = gfs2_clear_inode,
552 .drop_inode = gfs2_drop_inode,
553 .show_options = gfs2_show_options,