[GFS2] Readpages support
[safe/jmp/linux-2.6] / fs / gfs2 / ops_super.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2005 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 v.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/vmalloc.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 <asm/semaphore.h>
23
24 #include "gfs2.h"
25 #include "lm_interface.h"
26 #include "incore.h"
27 #include "glock.h"
28 #include "inode.h"
29 #include "lm.h"
30 #include "log.h"
31 #include "mount.h"
32 #include "ops_super.h"
33 #include "page.h"
34 #include "quota.h"
35 #include "recovery.h"
36 #include "rgrp.h"
37 #include "super.h"
38 #include "sys.h"
39 #include "util.h"
40
41 /**
42  * gfs2_write_inode - Make sure the inode is stable on the disk
43  * @inode: The inode
44  * @sync: synchronous write flag
45  *
46  * Returns: errno
47  */
48
49 static int gfs2_write_inode(struct inode *inode, int sync)
50 {
51         struct gfs2_inode *ip = inode->u.generic_ip;
52
53         if (current->flags & PF_MEMALLOC)
54                 return 0;
55         if (ip && sync)
56                 gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
57
58         return 0;
59 }
60
61 /**
62  * gfs2_put_super - Unmount the filesystem
63  * @sb: The VFS superblock
64  *
65  */
66
67 static void gfs2_put_super(struct super_block *sb)
68 {
69         struct gfs2_sbd *sdp = sb->s_fs_info;
70         int error;
71
72         if (!sdp)
73                 return;
74
75         /*  Unfreeze the filesystem, if we need to  */
76
77         mutex_lock(&sdp->sd_freeze_lock);
78         if (sdp->sd_freeze_count)
79                 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
80         mutex_unlock(&sdp->sd_freeze_lock);
81
82         kthread_stop(sdp->sd_inoded_process);
83         kthread_stop(sdp->sd_quotad_process);
84         kthread_stop(sdp->sd_logd_process);
85         kthread_stop(sdp->sd_recoverd_process);
86         while (sdp->sd_glockd_num--)
87                 kthread_stop(sdp->sd_glockd_process[sdp->sd_glockd_num]);
88         kthread_stop(sdp->sd_scand_process);
89
90         if (!(sb->s_flags & MS_RDONLY)) {
91                 error = gfs2_make_fs_ro(sdp);
92                 if (error)
93                         gfs2_io_error(sdp);
94         }
95         /*  At this point, we're through modifying the disk  */
96
97         /*  Release stuff  */
98
99         iput(sdp->sd_master_dir);
100         iput(sdp->sd_jindex);
101         iput(sdp->sd_inum_inode);
102         iput(sdp->sd_statfs_inode);
103         iput(sdp->sd_rindex);
104         iput(sdp->sd_quota_inode);
105
106         gfs2_glock_put(sdp->sd_rename_gl);
107         gfs2_glock_put(sdp->sd_trans_gl);
108
109         if (!sdp->sd_args.ar_spectator) {
110                 gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
111                 gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
112                 gfs2_glock_dq_uninit(&sdp->sd_ir_gh);
113                 gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
114                 gfs2_glock_dq_uninit(&sdp->sd_ut_gh);
115                 gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
116                 iput(sdp->sd_ir_inode);
117                 iput(sdp->sd_sc_inode);
118                 iput(sdp->sd_ut_inode);
119                 iput(sdp->sd_qc_inode);
120         }
121
122         gfs2_glock_dq_uninit(&sdp->sd_live_gh);
123         gfs2_clear_rgrpd(sdp);
124         gfs2_jindex_free(sdp);
125         /*  Take apart glock structures and buffer lists  */
126         gfs2_gl_hash_clear(sdp, WAIT);
127         /*  Unmount the locking protocol  */
128         gfs2_lm_unmount(sdp);
129
130         /*  At this point, we're through participating in the lockspace  */
131         gfs2_sys_fs_del(sdp);
132         vfree(sdp);
133         sb->s_fs_info = NULL;
134 }
135
136 /**
137  * gfs2_write_super - disk commit all incore transactions
138  * @sb: the filesystem
139  *
140  * This function is called every time sync(2) is called.
141  * After this exits, all dirty buffers are synced.
142  */
143
144 static void gfs2_write_super(struct super_block *sb)
145 {
146         struct gfs2_sbd *sdp = sb->s_fs_info;
147         gfs2_log_flush(sdp, NULL);
148 }
149
150 /**
151  * gfs2_write_super_lockfs - prevent further writes to the filesystem
152  * @sb: the VFS structure for the filesystem
153  *
154  */
155
156 static void gfs2_write_super_lockfs(struct super_block *sb)
157 {
158         struct gfs2_sbd *sdp = sb->s_fs_info;
159         int error;
160
161         for (;;) {
162                 error = gfs2_freeze_fs(sdp);
163                 if (!error)
164                         break;
165
166                 switch (error) {
167                 case -EBUSY:
168                         fs_err(sdp, "waiting for recovery before freeze\n");
169                         break;
170
171                 default:
172                         fs_err(sdp, "error freezing FS: %d\n", error);
173                         break;
174                 }
175
176                 fs_err(sdp, "retrying...\n");
177                 msleep(1000);
178         }
179 }
180
181 /**
182  * gfs2_unlockfs - reallow writes to the filesystem
183  * @sb: the VFS structure for the filesystem
184  *
185  */
186
187 static void gfs2_unlockfs(struct super_block *sb)
188 {
189         struct gfs2_sbd *sdp = sb->s_fs_info;
190         gfs2_unfreeze_fs(sdp);
191 }
192
193 /**
194  * gfs2_statfs - Gather and return stats about the filesystem
195  * @sb: The superblock
196  * @statfsbuf: The buffer
197  *
198  * Returns: 0 on success or error code
199  */
200
201 static int gfs2_statfs(struct super_block *sb, struct kstatfs *buf)
202 {
203         struct gfs2_sbd *sdp = sb->s_fs_info;
204         struct gfs2_statfs_change sc;
205         int error;
206
207         if (gfs2_tune_get(sdp, gt_statfs_slow))
208                 error = gfs2_statfs_slow(sdp, &sc);
209         else
210                 error = gfs2_statfs_i(sdp, &sc);
211
212         if (error)
213                 return error;
214
215         memset(buf, 0, sizeof(struct kstatfs));
216
217         buf->f_type = GFS2_MAGIC;
218         buf->f_bsize = sdp->sd_sb.sb_bsize;
219         buf->f_blocks = sc.sc_total;
220         buf->f_bfree = sc.sc_free;
221         buf->f_bavail = sc.sc_free;
222         buf->f_files = sc.sc_dinodes + sc.sc_free;
223         buf->f_ffree = sc.sc_free;
224         buf->f_namelen = GFS2_FNAMESIZE;
225
226         return 0;
227 }
228
229 /**
230  * gfs2_remount_fs - called when the FS is remounted
231  * @sb:  the filesystem
232  * @flags:  the remount flags
233  * @data:  extra data passed in (not used right now)
234  *
235  * Returns: errno
236  */
237
238 static int gfs2_remount_fs(struct super_block *sb, int *flags, char *data)
239 {
240         struct gfs2_sbd *sdp = sb->s_fs_info;
241         int error;
242
243         error = gfs2_mount_args(sdp, data, 1);
244         if (error)
245                 return error;
246
247         if (sdp->sd_args.ar_spectator)
248                 *flags |= MS_RDONLY;
249         else {
250                 if (*flags & MS_RDONLY) {
251                         if (!(sb->s_flags & MS_RDONLY))
252                                 error = gfs2_make_fs_ro(sdp);
253                 } else if (!(*flags & MS_RDONLY) &&
254                            (sb->s_flags & MS_RDONLY)) {
255                         error = gfs2_make_fs_rw(sdp);
256                 }
257         }
258
259         if (*flags & (MS_NOATIME | MS_NODIRATIME))
260                 set_bit(SDF_NOATIME, &sdp->sd_flags);
261         else
262                 clear_bit(SDF_NOATIME, &sdp->sd_flags);
263
264         /* Don't let the VFS update atimes.  GFS2 handles this itself. */
265         *flags |= MS_NOATIME | MS_NODIRATIME;
266
267         return error;
268 }
269
270 /**
271  * gfs2_clear_inode - Deallocate an inode when VFS is done with it
272  * @inode: The VFS inode
273  *
274  */
275
276 static void gfs2_clear_inode(struct inode *inode)
277 {
278         struct gfs2_inode *ip = inode->u.generic_ip;
279
280         if (ip) {
281                 spin_lock(&ip->i_spin);
282                 ip->i_vnode = NULL;
283                 inode->u.generic_ip = NULL;
284                 spin_unlock(&ip->i_spin);
285
286                 gfs2_glock_schedule_for_reclaim(ip->i_gl);
287                 gfs2_inode_put(ip);
288         }
289 }
290
291 /**
292  * gfs2_show_options - Show mount options for /proc/mounts
293  * @s: seq_file structure
294  * @mnt: vfsmount
295  *
296  * Returns: 0 on success or error code
297  */
298
299 static int gfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
300 {
301         struct gfs2_sbd *sdp = mnt->mnt_sb->s_fs_info;
302         struct gfs2_args *args = &sdp->sd_args;
303
304         if (args->ar_lockproto[0])
305                 seq_printf(s, ",lockproto=%s", args->ar_lockproto);
306         if (args->ar_locktable[0])
307                 seq_printf(s, ",locktable=%s", args->ar_locktable);
308         if (args->ar_hostdata[0])
309                 seq_printf(s, ",hostdata=%s", args->ar_hostdata);
310         if (args->ar_spectator)
311                 seq_printf(s, ",spectator");
312         if (args->ar_ignore_local_fs)
313                 seq_printf(s, ",ignore_local_fs");
314         if (args->ar_localflocks)
315                 seq_printf(s, ",localflocks");
316         if (args->ar_localcaching)
317                 seq_printf(s, ",localcaching");
318         if (args->ar_debug)
319                 seq_printf(s, ",debug");
320         if (args->ar_upgrade)
321                 seq_printf(s, ",upgrade");
322         if (args->ar_num_glockd != GFS2_GLOCKD_DEFAULT)
323                 seq_printf(s, ",num_glockd=%u", args->ar_num_glockd);
324         if (args->ar_posix_acl)
325                 seq_printf(s, ",acl");
326         if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
327                 char *state;
328                 switch (args->ar_quota) {
329                 case GFS2_QUOTA_OFF:
330                         state = "off";
331                         break;
332                 case GFS2_QUOTA_ACCOUNT:
333                         state = "account";
334                         break;
335                 case GFS2_QUOTA_ON:
336                         state = "on";
337                         break;
338                 default:
339                         state = "unknown";
340                         break;
341                 }
342                 seq_printf(s, ",quota=%s", state);
343         }
344         if (args->ar_suiddir)
345                 seq_printf(s, ",suiddir");
346         if (args->ar_data != GFS2_DATA_DEFAULT) {
347                 char *state;
348                 switch (args->ar_data) {
349                 case GFS2_DATA_WRITEBACK:
350                         state = "writeback";
351                         break;
352                 case GFS2_DATA_ORDERED:
353                         state = "ordered";
354                         break;
355                 default:
356                         state = "unknown";
357                         break;
358                 }
359                 seq_printf(s, ",data=%s", state);
360         }
361
362         return 0;
363 }
364
365 struct super_operations gfs2_super_ops = {
366         .write_inode = gfs2_write_inode,
367         .put_super = gfs2_put_super,
368         .write_super = gfs2_write_super,
369         .write_super_lockfs = gfs2_write_super_lockfs,
370         .unlockfs = gfs2_unlockfs,
371         .statfs = gfs2_statfs,
372         .remount_fs = gfs2_remount_fs,
373         .clear_inode = gfs2_clear_inode,
374         .show_options = gfs2_show_options,
375 };
376