blob: 820db9333bd5c1afe3608a5eda50a9254b39a3a4 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
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>
David Teiglandb3b94fa2006-01-16 16:50:04 +000015#include <linux/statfs.h>
Steven Whitehoused92a8d42006-02-27 10:57:14 -050016#include <linux/vmalloc.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000017#include <linux/seq_file.h>
18#include <linux/mount.h>
19#include <linux/kthread.h>
20#include <linux/delay.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050021#include <linux/gfs2_ondisk.h>
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040022#include <linux/crc32.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000023
24#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050025#include "lm_interface.h"
26#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000027#include "glock.h"
28#include "inode.h"
29#include "lm.h"
30#include "log.h"
31#include "mount.h"
32#include "ops_super.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000033#include "quota.h"
34#include "recovery.h"
35#include "rgrp.h"
36#include "super.h"
37#include "sys.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050038#include "util.h"
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040039#include "trans.h"
40#include "dir.h"
41#include "eattr.h"
42#include "bmap.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000043
44/**
45 * gfs2_write_inode - Make sure the inode is stable on the disk
46 * @inode: The inode
47 * @sync: synchronous write flag
48 *
49 * Returns: errno
50 */
51
52static int gfs2_write_inode(struct inode *inode, int sync)
53{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040054 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000055
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040056 /* Check this is a "normal" inode */
57 if (inode->u.generic_ip) {
58 if (current->flags & PF_MEMALLOC)
59 return 0;
60 if (sync)
Steven Whitehouse3a8476d2006-06-19 09:10:39 -040061 gfs2_log_flush(GFS2_SB(inode), ip->i_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040062 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000063
64 return 0;
65}
66
67/**
68 * gfs2_put_super - Unmount the filesystem
69 * @sb: The VFS superblock
70 *
71 */
72
73static void gfs2_put_super(struct super_block *sb)
74{
Steven Whitehouse5c676f62006-02-27 17:23:27 -050075 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +000076 int error;
77
78 if (!sdp)
79 return;
80
Abhijith Das86384602006-08-25 11:13:37 -050081 if (!strncmp(sb->s_type->name, "gfs2meta", 8))
Steven Whitehouse5acd3962006-09-04 16:16:45 -040082 return; /* Nothing to do */
Abhijith Das86384602006-08-25 11:13:37 -050083
David Teiglandb3b94fa2006-01-16 16:50:04 +000084 /* Unfreeze the filesystem, if we need to */
85
Steven Whitehousef55ab262006-02-21 12:51:39 +000086 mutex_lock(&sdp->sd_freeze_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +000087 if (sdp->sd_freeze_count)
88 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
Steven Whitehousef55ab262006-02-21 12:51:39 +000089 mutex_unlock(&sdp->sd_freeze_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +000090
David Teiglandb3b94fa2006-01-16 16:50:04 +000091 kthread_stop(sdp->sd_quotad_process);
92 kthread_stop(sdp->sd_logd_process);
93 kthread_stop(sdp->sd_recoverd_process);
94 while (sdp->sd_glockd_num--)
95 kthread_stop(sdp->sd_glockd_process[sdp->sd_glockd_num]);
96 kthread_stop(sdp->sd_scand_process);
97
98 if (!(sb->s_flags & MS_RDONLY)) {
99 error = gfs2_make_fs_ro(sdp);
100 if (error)
101 gfs2_io_error(sdp);
102 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000103 /* At this point, we're through modifying the disk */
104
105 /* Release stuff */
106
Steven Whitehousef42faf42006-01-30 18:34:10 +0000107 iput(sdp->sd_master_dir);
108 iput(sdp->sd_jindex);
109 iput(sdp->sd_inum_inode);
110 iput(sdp->sd_statfs_inode);
111 iput(sdp->sd_rindex);
112 iput(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000113
114 gfs2_glock_put(sdp->sd_rename_gl);
115 gfs2_glock_put(sdp->sd_trans_gl);
116
117 if (!sdp->sd_args.ar_spectator) {
118 gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
119 gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
120 gfs2_glock_dq_uninit(&sdp->sd_ir_gh);
121 gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000122 gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
Steven Whitehousef42faf42006-01-30 18:34:10 +0000123 iput(sdp->sd_ir_inode);
124 iput(sdp->sd_sc_inode);
Steven Whitehousef42faf42006-01-30 18:34:10 +0000125 iput(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000126 }
127
128 gfs2_glock_dq_uninit(&sdp->sd_live_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000129 gfs2_clear_rgrpd(sdp);
130 gfs2_jindex_free(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000131 /* Take apart glock structures and buffer lists */
132 gfs2_gl_hash_clear(sdp, WAIT);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000133 /* Unmount the locking protocol */
134 gfs2_lm_unmount(sdp);
135
136 /* At this point, we're through participating in the lockspace */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000137 gfs2_sys_fs_del(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000138 vfree(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000139}
140
141/**
142 * gfs2_write_super - disk commit all incore transactions
143 * @sb: the filesystem
144 *
145 * This function is called every time sync(2) is called.
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400146 * After this exits, all dirty buffers are synced.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000147 */
148
149static void gfs2_write_super(struct super_block *sb)
150{
Steven Whitehouse5acd3962006-09-04 16:16:45 -0400151 gfs2_log_flush(sb->s_fs_info, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152}
153
154/**
155 * gfs2_write_super_lockfs - prevent further writes to the filesystem
156 * @sb: the VFS structure for the filesystem
157 *
158 */
159
160static void gfs2_write_super_lockfs(struct super_block *sb)
161{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500162 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000163 int error;
164
David Teiglandb3b94fa2006-01-16 16:50:04 +0000165 for (;;) {
166 error = gfs2_freeze_fs(sdp);
167 if (!error)
168 break;
169
170 switch (error) {
171 case -EBUSY:
172 fs_err(sdp, "waiting for recovery before freeze\n");
173 break;
174
175 default:
176 fs_err(sdp, "error freezing FS: %d\n", error);
177 break;
178 }
179
180 fs_err(sdp, "retrying...\n");
181 msleep(1000);
182 }
183}
184
185/**
186 * gfs2_unlockfs - reallow writes to the filesystem
187 * @sb: the VFS structure for the filesystem
188 *
189 */
190
191static void gfs2_unlockfs(struct super_block *sb)
192{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500193 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000194 gfs2_unfreeze_fs(sdp);
195}
196
197/**
198 * gfs2_statfs - Gather and return stats about the filesystem
199 * @sb: The superblock
200 * @statfsbuf: The buffer
201 *
202 * Returns: 0 on success or error code
203 */
204
Steven Whitehouse0c0834a2006-07-03 11:38:01 -0400205static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206{
Steven Whitehouse0c0834a2006-07-03 11:38:01 -0400207 struct super_block *sb = dentry->d_inode->i_sb;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500208 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000209 struct gfs2_statfs_change sc;
210 int error;
211
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212 if (gfs2_tune_get(sdp, gt_statfs_slow))
213 error = gfs2_statfs_slow(sdp, &sc);
214 else
215 error = gfs2_statfs_i(sdp, &sc);
216
217 if (error)
218 return error;
219
David Teiglandb3b94fa2006-01-16 16:50:04 +0000220 buf->f_type = GFS2_MAGIC;
221 buf->f_bsize = sdp->sd_sb.sb_bsize;
222 buf->f_blocks = sc.sc_total;
223 buf->f_bfree = sc.sc_free;
224 buf->f_bavail = sc.sc_free;
225 buf->f_files = sc.sc_dinodes + sc.sc_free;
226 buf->f_ffree = sc.sc_free;
227 buf->f_namelen = GFS2_FNAMESIZE;
228
229 return 0;
230}
231
232/**
233 * gfs2_remount_fs - called when the FS is remounted
234 * @sb: the filesystem
235 * @flags: the remount flags
236 * @data: extra data passed in (not used right now)
237 *
238 * Returns: errno
239 */
240
241static int gfs2_remount_fs(struct super_block *sb, int *flags, char *data)
242{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500243 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000244 int error;
245
David Teiglandb3b94fa2006-01-16 16:50:04 +0000246 error = gfs2_mount_args(sdp, data, 1);
247 if (error)
248 return error;
249
250 if (sdp->sd_args.ar_spectator)
251 *flags |= MS_RDONLY;
252 else {
253 if (*flags & MS_RDONLY) {
254 if (!(sb->s_flags & MS_RDONLY))
255 error = gfs2_make_fs_ro(sdp);
256 } else if (!(*flags & MS_RDONLY) &&
257 (sb->s_flags & MS_RDONLY)) {
258 error = gfs2_make_fs_rw(sdp);
259 }
260 }
261
262 if (*flags & (MS_NOATIME | MS_NODIRATIME))
263 set_bit(SDF_NOATIME, &sdp->sd_flags);
264 else
265 clear_bit(SDF_NOATIME, &sdp->sd_flags);
266
267 /* Don't let the VFS update atimes. GFS2 handles this itself. */
268 *flags |= MS_NOATIME | MS_NODIRATIME;
269
270 return error;
271}
272
273/**
274 * gfs2_clear_inode - Deallocate an inode when VFS is done with it
275 * @inode: The VFS inode
276 *
277 */
278
279static void gfs2_clear_inode(struct inode *inode)
280{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400281 /* This tells us its a "real" inode and not one which only
282 * serves to contain an address space (see rgrp.c, meta_io.c)
283 * which therefore doesn't have its own glocks.
284 */
285 if (inode->u.generic_ip) {
286 struct gfs2_inode *ip = GFS2_I(inode);
287 gfs2_glock_inode_squish(inode);
288 gfs2_assert(inode->i_sb->s_fs_info, ip->i_gl->gl_state == LM_ST_UNLOCKED);
289 ip->i_gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000290 gfs2_glock_schedule_for_reclaim(ip->i_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400291 gfs2_glock_put(ip->i_gl);
292 ip->i_gl = NULL;
293 if (ip->i_iopen_gh.gh_gl)
294 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295 }
296}
297
298/**
299 * gfs2_show_options - Show mount options for /proc/mounts
300 * @s: seq_file structure
301 * @mnt: vfsmount
302 *
303 * Returns: 0 on success or error code
304 */
305
306static int gfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
307{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500308 struct gfs2_sbd *sdp = mnt->mnt_sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000309 struct gfs2_args *args = &sdp->sd_args;
310
David Teiglandb3b94fa2006-01-16 16:50:04 +0000311 if (args->ar_lockproto[0])
312 seq_printf(s, ",lockproto=%s", args->ar_lockproto);
313 if (args->ar_locktable[0])
314 seq_printf(s, ",locktable=%s", args->ar_locktable);
315 if (args->ar_hostdata[0])
316 seq_printf(s, ",hostdata=%s", args->ar_hostdata);
317 if (args->ar_spectator)
318 seq_printf(s, ",spectator");
319 if (args->ar_ignore_local_fs)
320 seq_printf(s, ",ignore_local_fs");
321 if (args->ar_localflocks)
322 seq_printf(s, ",localflocks");
323 if (args->ar_localcaching)
324 seq_printf(s, ",localcaching");
325 if (args->ar_debug)
326 seq_printf(s, ",debug");
327 if (args->ar_upgrade)
328 seq_printf(s, ",upgrade");
329 if (args->ar_num_glockd != GFS2_GLOCKD_DEFAULT)
330 seq_printf(s, ",num_glockd=%u", args->ar_num_glockd);
331 if (args->ar_posix_acl)
332 seq_printf(s, ",acl");
333 if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
334 char *state;
335 switch (args->ar_quota) {
336 case GFS2_QUOTA_OFF:
337 state = "off";
338 break;
339 case GFS2_QUOTA_ACCOUNT:
340 state = "account";
341 break;
342 case GFS2_QUOTA_ON:
343 state = "on";
344 break;
345 default:
346 state = "unknown";
347 break;
348 }
349 seq_printf(s, ",quota=%s", state);
350 }
351 if (args->ar_suiddir)
352 seq_printf(s, ",suiddir");
353 if (args->ar_data != GFS2_DATA_DEFAULT) {
354 char *state;
355 switch (args->ar_data) {
356 case GFS2_DATA_WRITEBACK:
357 state = "writeback";
358 break;
359 case GFS2_DATA_ORDERED:
360 state = "ordered";
361 break;
362 default:
363 state = "unknown";
364 break;
365 }
366 seq_printf(s, ",data=%s", state);
367 }
368
369 return 0;
370}
371
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400372/*
373 * We have to (at the moment) hold the inodes main lock to cover
374 * the gap between unlocking the shared lock on the iopen lock and
375 * taking the exclusive lock. I'd rather do a shared -> exclusive
376 * conversion on the iopen lock, but we can change that later. This
377 * is safe, just less efficient.
378 */
379static void gfs2_delete_inode(struct inode *inode)
380{
381 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
382 struct gfs2_inode *ip = GFS2_I(inode);
383 struct gfs2_holder gh;
384 int error;
385
386 if (!inode->u.generic_ip)
387 goto out;
388
389 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &gh);
390 if (unlikely(error)) {
391 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
392 goto out;
393 }
394
395 gfs2_glock_dq(&ip->i_iopen_gh);
396 gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &ip->i_iopen_gh);
397 error = gfs2_glock_nq(&ip->i_iopen_gh);
398 if (error)
399 goto out_uninit;
400
401 if (S_ISDIR(ip->i_di.di_mode) &&
402 (ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
403 error = gfs2_dir_exhash_dealloc(ip);
404 if (error)
405 goto out_unlock;
406 }
407
408 if (ip->i_di.di_eattr) {
409 error = gfs2_ea_dealloc(ip);
410 if (error)
411 goto out_unlock;
412 }
413
414 if (!gfs2_is_stuffed(ip)) {
415 error = gfs2_file_dealloc(ip);
416 if (error)
417 goto out_unlock;
418 }
419
420 error = gfs2_dinode_dealloc(ip);
421
422out_unlock:
423 gfs2_glock_dq(&ip->i_iopen_gh);
424out_uninit:
425 gfs2_holder_uninit(&ip->i_iopen_gh);
426 gfs2_glock_dq_uninit(&gh);
427 if (error)
428 fs_warn(sdp, "gfs2_delete_inode: %d\n", error);
429out:
430 truncate_inode_pages(&inode->i_data, 0);
431 clear_inode(inode);
432}
433
434
435
Steven Whitehouse320dd102006-05-18 16:25:27 -0400436static struct inode *gfs2_alloc_inode(struct super_block *sb)
437{
438 struct gfs2_sbd *sdp = sb->s_fs_info;
439 struct gfs2_inode *ip;
440
441 ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
442 if (ip) {
443 ip->i_flags = 0;
444 ip->i_gl = NULL;
Steven Whitehouse320dd102006-05-18 16:25:27 -0400445 ip->i_greedy = gfs2_tune_get(sdp, gt_greedy_default);
446 ip->i_last_pfault = jiffies;
447 }
448 return &ip->i_inode;
449}
450
451static void gfs2_destroy_inode(struct inode *inode)
452{
453 kmem_cache_free(gfs2_inode_cachep, inode);
454}
455
David Teiglandb3b94fa2006-01-16 16:50:04 +0000456struct super_operations gfs2_super_ops = {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400457 .alloc_inode = gfs2_alloc_inode,
458 .destroy_inode = gfs2_destroy_inode,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000459 .write_inode = gfs2_write_inode,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400460 .delete_inode = gfs2_delete_inode,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000461 .put_super = gfs2_put_super,
462 .write_super = gfs2_write_super,
463 .write_super_lockfs = gfs2_write_super_lockfs,
464 .unlockfs = gfs2_unlockfs,
465 .statfs = gfs2_statfs,
466 .remount_fs = gfs2_remount_fs,
467 .clear_inode = gfs2_clear_inode,
468 .show_options = gfs2_show_options,
469};
470