Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[sfrench/cifs-2.6.git] / fs / gfs2 / ops_super.c
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
51 static 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
72 static 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
146 static 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  */
157 static int gfs2_sync_fs(struct super_block *sb, int wait)
158 {
159         sb->s_dirt = 0;
160         gfs2_log_flush(sb->s_fs_info, NULL);
161         return 0;
162 }
163
164 /**
165  * gfs2_write_super_lockfs - prevent further writes to the filesystem
166  * @sb: the VFS structure for the filesystem
167  *
168  */
169
170 static void gfs2_write_super_lockfs(struct super_block *sb)
171 {
172         struct gfs2_sbd *sdp = sb->s_fs_info;
173         int error;
174
175         for (;;) {
176                 error = gfs2_freeze_fs(sdp);
177                 if (!error)
178                         break;
179
180                 switch (error) {
181                 case -EBUSY:
182                         fs_err(sdp, "waiting for recovery before freeze\n");
183                         break;
184
185                 default:
186                         fs_err(sdp, "error freezing FS: %d\n", error);
187                         break;
188                 }
189
190                 fs_err(sdp, "retrying...\n");
191                 msleep(1000);
192         }
193 }
194
195 /**
196  * gfs2_unlockfs - reallow writes to the filesystem
197  * @sb: the VFS structure for the filesystem
198  *
199  */
200
201 static void gfs2_unlockfs(struct super_block *sb)
202 {
203         gfs2_unfreeze_fs(sb->s_fs_info);
204 }
205
206 /**
207  * gfs2_statfs - Gather and return stats about the filesystem
208  * @sb: The superblock
209  * @statfsbuf: The buffer
210  *
211  * Returns: 0 on success or error code
212  */
213
214 static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
215 {
216         struct super_block *sb = dentry->d_inode->i_sb;
217         struct gfs2_sbd *sdp = sb->s_fs_info;
218         struct gfs2_statfs_change sc;
219         int error;
220
221         if (gfs2_tune_get(sdp, gt_statfs_slow))
222                 error = gfs2_statfs_slow(sdp, &sc);
223         else
224                 error = gfs2_statfs_i(sdp, &sc);
225
226         if (error)
227                 return error;
228
229         buf->f_type = GFS2_MAGIC;
230         buf->f_bsize = sdp->sd_sb.sb_bsize;
231         buf->f_blocks = sc.sc_total;
232         buf->f_bfree = sc.sc_free;
233         buf->f_bavail = sc.sc_free;
234         buf->f_files = sc.sc_dinodes + sc.sc_free;
235         buf->f_ffree = sc.sc_free;
236         buf->f_namelen = GFS2_FNAMESIZE;
237
238         return 0;
239 }
240
241 /**
242  * gfs2_remount_fs - called when the FS is remounted
243  * @sb:  the filesystem
244  * @flags:  the remount flags
245  * @data:  extra data passed in (not used right now)
246  *
247  * Returns: errno
248  */
249
250 static int gfs2_remount_fs(struct super_block *sb, int *flags, char *data)
251 {
252         struct gfs2_sbd *sdp = sb->s_fs_info;
253         int error;
254
255         error = gfs2_mount_args(sdp, data, 1);
256         if (error)
257                 return error;
258
259         if (sdp->sd_args.ar_spectator)
260                 *flags |= MS_RDONLY;
261         else {
262                 if (*flags & MS_RDONLY) {
263                         if (!(sb->s_flags & MS_RDONLY))
264                                 error = gfs2_make_fs_ro(sdp);
265                 } else if (!(*flags & MS_RDONLY) &&
266                            (sb->s_flags & MS_RDONLY)) {
267                         error = gfs2_make_fs_rw(sdp);
268                 }
269         }
270
271         if (*flags & (MS_NOATIME | MS_NODIRATIME))
272                 set_bit(SDF_NOATIME, &sdp->sd_flags);
273         else
274                 clear_bit(SDF_NOATIME, &sdp->sd_flags);
275
276         /* Don't let the VFS update atimes.  GFS2 handles this itself. */
277         *flags |= MS_NOATIME | MS_NODIRATIME;
278
279         return error;
280 }
281
282 /**
283  * gfs2_clear_inode - Deallocate an inode when VFS is done with it
284  * @inode: The VFS inode
285  *
286  */
287
288 static void gfs2_clear_inode(struct inode *inode)
289 {
290         /* This tells us its a "real" inode and not one which only
291          * serves to contain an address space (see rgrp.c, meta_io.c)
292          * which therefore doesn't have its own glocks.
293          */
294         if (inode->i_private) {
295                 struct gfs2_inode *ip = GFS2_I(inode);
296                 gfs2_glock_inode_squish(inode);
297                 gfs2_assert(inode->i_sb->s_fs_info, ip->i_gl->gl_state == LM_ST_UNLOCKED);
298                 ip->i_gl->gl_object = NULL;
299                 gfs2_glock_schedule_for_reclaim(ip->i_gl);
300                 gfs2_glock_put(ip->i_gl);
301                 ip->i_gl = NULL;
302                 if (ip->i_iopen_gh.gh_gl)
303                         gfs2_glock_dq_uninit(&ip->i_iopen_gh);
304         }
305 }
306
307 /**
308  * gfs2_show_options - Show mount options for /proc/mounts
309  * @s: seq_file structure
310  * @mnt: vfsmount
311  *
312  * Returns: 0 on success or error code
313  */
314
315 static int gfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
316 {
317         struct gfs2_sbd *sdp = mnt->mnt_sb->s_fs_info;
318         struct gfs2_args *args = &sdp->sd_args;
319
320         if (args->ar_lockproto[0])
321                 seq_printf(s, ",lockproto=%s", args->ar_lockproto);
322         if (args->ar_locktable[0])
323                 seq_printf(s, ",locktable=%s", args->ar_locktable);
324         if (args->ar_hostdata[0])
325                 seq_printf(s, ",hostdata=%s", args->ar_hostdata);
326         if (args->ar_spectator)
327                 seq_printf(s, ",spectator");
328         if (args->ar_ignore_local_fs)
329                 seq_printf(s, ",ignore_local_fs");
330         if (args->ar_localflocks)
331                 seq_printf(s, ",localflocks");
332         if (args->ar_localcaching)
333                 seq_printf(s, ",localcaching");
334         if (args->ar_debug)
335                 seq_printf(s, ",debug");
336         if (args->ar_upgrade)
337                 seq_printf(s, ",upgrade");
338         if (args->ar_num_glockd != GFS2_GLOCKD_DEFAULT)
339                 seq_printf(s, ",num_glockd=%u", args->ar_num_glockd);
340         if (args->ar_posix_acl)
341                 seq_printf(s, ",acl");
342         if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
343                 char *state;
344                 switch (args->ar_quota) {
345                 case GFS2_QUOTA_OFF:
346                         state = "off";
347                         break;
348                 case GFS2_QUOTA_ACCOUNT:
349                         state = "account";
350                         break;
351                 case GFS2_QUOTA_ON:
352                         state = "on";
353                         break;
354                 default:
355                         state = "unknown";
356                         break;
357                 }
358                 seq_printf(s, ",quota=%s", state);
359         }
360         if (args->ar_suiddir)
361                 seq_printf(s, ",suiddir");
362         if (args->ar_data != GFS2_DATA_DEFAULT) {
363                 char *state;
364                 switch (args->ar_data) {
365                 case GFS2_DATA_WRITEBACK:
366                         state = "writeback";
367                         break;
368                 case GFS2_DATA_ORDERED:
369                         state = "ordered";
370                         break;
371                 default:
372                         state = "unknown";
373                         break;
374                 }
375                 seq_printf(s, ",data=%s", state);
376         }
377
378         return 0;
379 }
380
381 /*
382  * We have to (at the moment) hold the inodes main lock to cover
383  * the gap between unlocking the shared lock on the iopen lock and
384  * taking the exclusive lock. I'd rather do a shared -> exclusive
385  * conversion on the iopen lock, but we can change that later. This
386  * is safe, just less efficient.
387  */
388 static void gfs2_delete_inode(struct inode *inode)
389 {
390         struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
391         struct gfs2_inode *ip = GFS2_I(inode);
392         struct gfs2_holder gh;
393         int error;
394
395         if (!inode->i_private)
396                 goto out;
397
398         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &gh);
399         if (unlikely(error)) {
400                 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
401                 goto out;
402         }
403
404         gfs2_glock_dq(&ip->i_iopen_gh);
405         gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &ip->i_iopen_gh);
406         error = gfs2_glock_nq(&ip->i_iopen_gh);
407         if (error)
408                 goto out_uninit;
409
410         if (S_ISDIR(ip->i_di.di_mode) &&
411             (ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
412                 error = gfs2_dir_exhash_dealloc(ip);
413                 if (error)
414                         goto out_unlock;
415         }
416
417         if (ip->i_di.di_eattr) {
418                 error = gfs2_ea_dealloc(ip);
419                 if (error)
420                         goto out_unlock;
421         }
422
423         if (!gfs2_is_stuffed(ip)) {
424                 error = gfs2_file_dealloc(ip);
425                 if (error)
426                         goto out_unlock;
427         }
428
429         error = gfs2_dinode_dealloc(ip);
430
431 out_unlock:
432         gfs2_glock_dq(&ip->i_iopen_gh);
433 out_uninit:
434         gfs2_holder_uninit(&ip->i_iopen_gh);
435         gfs2_glock_dq_uninit(&gh);
436         if (error)
437                 fs_warn(sdp, "gfs2_delete_inode: %d\n", error);
438 out:
439         truncate_inode_pages(&inode->i_data, 0);
440         clear_inode(inode);
441 }
442
443
444
445 static struct inode *gfs2_alloc_inode(struct super_block *sb)
446 {
447         struct gfs2_sbd *sdp = sb->s_fs_info;
448         struct gfs2_inode *ip;
449
450         ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
451         if (ip) {
452                 ip->i_flags = 0;
453                 ip->i_gl = NULL;
454                 ip->i_greedy = gfs2_tune_get(sdp, gt_greedy_default);
455                 ip->i_last_pfault = jiffies;
456         }
457         return &ip->i_inode;
458 }
459
460 static void gfs2_destroy_inode(struct inode *inode)
461 {
462         kmem_cache_free(gfs2_inode_cachep, inode);
463 }
464
465 struct super_operations gfs2_super_ops = {
466         .alloc_inode            = gfs2_alloc_inode,
467         .destroy_inode          = gfs2_destroy_inode,
468         .write_inode            = gfs2_write_inode,
469         .delete_inode           = gfs2_delete_inode,
470         .put_super              = gfs2_put_super,
471         .write_super            = gfs2_write_super,
472         .sync_fs                = gfs2_sync_fs,
473         .write_super_lockfs     = gfs2_write_super_lockfs,
474         .unlockfs               = gfs2_unlockfs,
475         .statfs                 = gfs2_statfs,
476         .remount_fs             = gfs2_remount_fs,
477         .clear_inode            = gfs2_clear_inode,
478         .show_options           = gfs2_show_options,
479 };
480