GFS2: The war on bloat
[pandora-kernel.git] / fs / gfs2 / ops_super.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2008 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 "log.h"
29 #include "mount.h"
30 #include "ops_super.h"
31 #include "quota.h"
32 #include "recovery.h"
33 #include "rgrp.h"
34 #include "super.h"
35 #include "sys.h"
36 #include "util.h"
37 #include "trans.h"
38 #include "dir.h"
39 #include "eattr.h"
40 #include "bmap.h"
41
42 /**
43  * gfs2_write_inode - Make sure the inode is stable on the disk
44  * @inode: The inode
45  * @sync: synchronous write flag
46  *
47  * Returns: errno
48  */
49
50 static int gfs2_write_inode(struct inode *inode, int sync)
51 {
52         struct gfs2_inode *ip = GFS2_I(inode);
53
54         /* Check this is a "normal" inode */
55         if (test_bit(GIF_USER, &ip->i_flags)) {
56                 if (current->flags & PF_MEMALLOC)
57                         return 0;
58                 if (sync)
59                         gfs2_log_flush(GFS2_SB(inode), ip->i_gl);
60         }
61
62         return 0;
63 }
64
65 /**
66  * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
67  * @sdp: the filesystem
68  *
69  * Returns: errno
70  */
71
72 static int gfs2_make_fs_ro(struct gfs2_sbd *sdp)
73 {
74         struct gfs2_holder t_gh;
75         int error;
76
77         gfs2_quota_sync(sdp);
78         gfs2_statfs_sync(sdp);
79
80         error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, GL_NOCACHE,
81                                    &t_gh);
82         if (error && !test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
83                 return error;
84
85         gfs2_meta_syncfs(sdp);
86         gfs2_log_shutdown(sdp);
87
88         clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
89
90         if (t_gh.gh_gl)
91                 gfs2_glock_dq_uninit(&t_gh);
92
93         gfs2_quota_cleanup(sdp);
94
95         return error;
96 }
97
98 /**
99  * gfs2_put_super - Unmount the filesystem
100  * @sb: The VFS superblock
101  *
102  */
103
104 static void gfs2_put_super(struct super_block *sb)
105 {
106         struct gfs2_sbd *sdp = sb->s_fs_info;
107         int error;
108
109         /*  Unfreeze the filesystem, if we need to  */
110
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);
115
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]);
121
122         if (!(sb->s_flags & MS_RDONLY)) {
123                 error = gfs2_make_fs_ro(sdp);
124                 if (error)
125                         gfs2_io_error(sdp);
126         }
127         /*  At this point, we're through modifying the disk  */
128
129         /*  Release stuff  */
130
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);
136
137         gfs2_glock_put(sdp->sd_rename_gl);
138         gfs2_glock_put(sdp->sd_trans_gl);
139
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);
149         }
150
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);
158
159         /*  At this point, we're through participating in the lockspace  */
160         gfs2_sys_fs_del(sdp);
161         kfree(sdp);
162 }
163
164 /**
165  * gfs2_write_super
166  * @sb: the superblock
167  *
168  */
169
170 static void gfs2_write_super(struct super_block *sb)
171 {
172         sb->s_dirt = 0;
173 }
174
175 /**
176  * gfs2_sync_fs - sync the filesystem
177  * @sb: the superblock
178  *
179  * Flushes the log to disk.
180  */
181
182 static int gfs2_sync_fs(struct super_block *sb, int wait)
183 {
184         sb->s_dirt = 0;
185         if (wait && sb->s_fs_info)
186                 gfs2_log_flush(sb->s_fs_info, NULL);
187         return 0;
188 }
189
190 /**
191  * gfs2_write_super_lockfs - prevent further writes to the filesystem
192  * @sb: the VFS structure for the filesystem
193  *
194  */
195
196 static void gfs2_write_super_lockfs(struct super_block *sb)
197 {
198         struct gfs2_sbd *sdp = sb->s_fs_info;
199         int error;
200
201         if (test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
202                 return;
203
204         for (;;) {
205                 error = gfs2_freeze_fs(sdp);
206                 if (!error)
207                         break;
208
209                 switch (error) {
210                 case -EBUSY:
211                         fs_err(sdp, "waiting for recovery before freeze\n");
212                         break;
213
214                 default:
215                         fs_err(sdp, "error freezing FS: %d\n", error);
216                         break;
217                 }
218
219                 fs_err(sdp, "retrying...\n");
220                 msleep(1000);
221         }
222 }
223
224 /**
225  * gfs2_unlockfs - reallow writes to the filesystem
226  * @sb: the VFS structure for the filesystem
227  *
228  */
229
230 static void gfs2_unlockfs(struct super_block *sb)
231 {
232         gfs2_unfreeze_fs(sb->s_fs_info);
233 }
234
235 /**
236  * gfs2_statfs - Gather and return stats about the filesystem
237  * @sb: The superblock
238  * @statfsbuf: The buffer
239  *
240  * Returns: 0 on success or error code
241  */
242
243 static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
244 {
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;
248         int error;
249
250         if (gfs2_tune_get(sdp, gt_statfs_slow))
251                 error = gfs2_statfs_slow(sdp, &sc);
252         else
253                 error = gfs2_statfs_i(sdp, &sc);
254
255         if (error)
256                 return error;
257
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;
266
267         return 0;
268 }
269
270 /**
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)
275  *
276  * Returns: errno
277  */
278
279 static int gfs2_remount_fs(struct super_block *sb, int *flags, char *data)
280 {
281         struct gfs2_sbd *sdp = sb->s_fs_info;
282         int error;
283
284         error = gfs2_mount_args(sdp, data, 1);
285         if (error)
286                 return error;
287
288         if (sdp->sd_args.ar_spectator)
289                 *flags |= MS_RDONLY;
290         else {
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);
297                 }
298         }
299
300         if (*flags & (MS_NOATIME | MS_NODIRATIME))
301                 set_bit(SDF_NOATIME, &sdp->sd_flags);
302         else
303                 clear_bit(SDF_NOATIME, &sdp->sd_flags);
304
305         /* Don't let the VFS update atimes.  GFS2 handles this itself. */
306         *flags |= MS_NOATIME | MS_NODIRATIME;
307
308         return error;
309 }
310
311 /**
312  * gfs2_drop_inode - Drop an inode (test for remote unlink)
313  * @inode: The inode to drop
314  *
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.
324  */
325
326 static void gfs2_drop_inode(struct inode *inode)
327 {
328         struct gfs2_inode *ip = GFS2_I(inode);
329
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))
333                         clear_nlink(inode);
334         }
335         generic_drop_inode(inode);
336 }
337
338 /**
339  * gfs2_clear_inode - Deallocate an inode when VFS is done with it
340  * @inode: The VFS inode
341  *
342  */
343
344 static void gfs2_clear_inode(struct inode *inode)
345 {
346         struct gfs2_inode *ip = GFS2_I(inode);
347
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.
351          */
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);
356                 ip->i_gl = NULL;
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);
360                 }
361         }
362 }
363
364 static int is_ancestor(const struct dentry *d1, const struct dentry *d2)
365 {
366         do {
367                 if (d1 == d2)
368                         return 1;
369                 d1 = d1->d_parent;
370         } while (!IS_ROOT(d1));
371         return 0;
372 }
373
374 /**
375  * gfs2_show_options - Show mount options for /proc/mounts
376  * @s: seq_file structure
377  * @mnt: vfsmount
378  *
379  * Returns: 0 on success or error code
380  */
381
382 static int gfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
383 {
384         struct gfs2_sbd *sdp = mnt->mnt_sb->s_fs_info;
385         struct gfs2_args *args = &sdp->sd_args;
386
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");
403         if (args->ar_debug)
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) {
412                 char *state;
413                 switch (args->ar_quota) {
414                 case GFS2_QUOTA_OFF:
415                         state = "off";
416                         break;
417                 case GFS2_QUOTA_ACCOUNT:
418                         state = "account";
419                         break;
420                 case GFS2_QUOTA_ON:
421                         state = "on";
422                         break;
423                 default:
424                         state = "unknown";
425                         break;
426                 }
427                 seq_printf(s, ",quota=%s", state);
428         }
429         if (args->ar_suiddir)
430                 seq_printf(s, ",suiddir");
431         if (args->ar_data != GFS2_DATA_DEFAULT) {
432                 char *state;
433                 switch (args->ar_data) {
434                 case GFS2_DATA_WRITEBACK:
435                         state = "writeback";
436                         break;
437                 case GFS2_DATA_ORDERED:
438                         state = "ordered";
439                         break;
440                 default:
441                         state = "unknown";
442                         break;
443                 }
444                 seq_printf(s, ",data=%s", state);
445         }
446
447         return 0;
448 }
449
450 /*
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.
456  */
457
458 static void gfs2_delete_inode(struct inode *inode)
459 {
460         struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
461         struct gfs2_inode *ip = GFS2_I(inode);
462         struct gfs2_holder gh;
463         int error;
464
465         if (!test_bit(GIF_USER, &ip->i_flags))
466                 goto out;
467
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);
471                 goto out;
472         }
473
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);
477         if (error)
478                 goto out_uninit;
479
480         if (S_ISDIR(inode->i_mode) &&
481             (ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
482                 error = gfs2_dir_exhash_dealloc(ip);
483                 if (error)
484                         goto out_unlock;
485         }
486
487         if (ip->i_di.di_eattr) {
488                 error = gfs2_ea_dealloc(ip);
489                 if (error)
490                         goto out_unlock;
491         }
492
493         if (!gfs2_is_stuffed(ip)) {
494                 error = gfs2_file_dealloc(ip);
495                 if (error)
496                         goto out_unlock;
497         }
498
499         error = gfs2_dinode_dealloc(ip);
500         if (error)
501                 goto out_unlock;
502
503         error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
504         if (error)
505                 goto out_unlock;
506         /* Needs to be done before glock release & also in a transaction */
507         truncate_inode_pages(&inode->i_data, 0);
508         gfs2_trans_end(sdp);
509
510 out_unlock:
511         gfs2_glock_dq(&ip->i_iopen_gh);
512 out_uninit:
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);
517 out:
518         truncate_inode_pages(&inode->i_data, 0);
519         clear_inode(inode);
520 }
521
522 static struct inode *gfs2_alloc_inode(struct super_block *sb)
523 {
524         struct gfs2_inode *ip;
525
526         ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
527         if (ip) {
528                 ip->i_flags = 0;
529                 ip->i_gl = NULL;
530         }
531         return &ip->i_inode;
532 }
533
534 static void gfs2_destroy_inode(struct inode *inode)
535 {
536         kmem_cache_free(gfs2_inode_cachep, inode);
537 }
538
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,
554 };
555