blob: 7685b46f934b4ad07c419a4b11b4eef60aa0c167 [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>
16#include <linux/seq_file.h>
17#include <linux/mount.h>
18#include <linux/kthread.h>
19#include <linux/delay.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050020#include <linux/gfs2_ondisk.h>
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040021#include <linux/crc32.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020022#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000023
24#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050025#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000026#include "glock.h"
27#include "inode.h"
28#include "lm.h"
29#include "log.h"
30#include "mount.h"
31#include "ops_super.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000032#include "quota.h"
33#include "recovery.h"
34#include "rgrp.h"
35#include "super.h"
36#include "sys.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050037#include "util.h"
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040038#include "trans.h"
39#include "dir.h"
40#include "eattr.h"
41#include "bmap.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000042
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
51static int gfs2_write_inode(struct inode *inode, int sync)
52{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040053 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000054
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040055 /* Check this is a "normal" inode */
Theodore Ts'obba9dfd2006-09-27 01:50:31 -070056 if (inode->i_private) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040057 if (current->flags & PF_MEMALLOC)
58 return 0;
59 if (sync)
Steven Whitehouse3a8476d2006-06-19 09:10:39 -040060 gfs2_log_flush(GFS2_SB(inode), ip->i_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040061 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000062
63 return 0;
64}
65
66/**
67 * gfs2_put_super - Unmount the filesystem
68 * @sb: The VFS superblock
69 *
70 */
71
72static void gfs2_put_super(struct super_block *sb)
73{
Steven Whitehouse5c676f62006-02-27 17:23:27 -050074 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +000075 int error;
76
77 if (!sdp)
78 return;
79
Abhijith Das86384602006-08-25 11:13:37 -050080 if (!strncmp(sb->s_type->name, "gfs2meta", 8))
Steven Whitehouse5acd3962006-09-04 16:16:45 -040081 return; /* Nothing to do */
Abhijith Das86384602006-08-25 11:13:37 -050082
David Teiglandb3b94fa2006-01-16 16:50:04 +000083 /* Unfreeze the filesystem, if we need to */
84
Steven Whitehousef55ab262006-02-21 12:51:39 +000085 mutex_lock(&sdp->sd_freeze_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +000086 if (sdp->sd_freeze_count)
87 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
Steven Whitehousef55ab262006-02-21 12:51:39 +000088 mutex_unlock(&sdp->sd_freeze_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +000089
David Teiglandb3b94fa2006-01-16 16:50:04 +000090 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 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000102 /* At this point, we're through modifying the disk */
103
104 /* Release stuff */
105
Steven Whitehousef42faf42006-01-30 18:34:10 +0000106 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);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000112
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);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000121 gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
Steven Whitehousef42faf42006-01-30 18:34:10 +0000122 iput(sdp->sd_ir_inode);
123 iput(sdp->sd_sc_inode);
Steven Whitehousef42faf42006-01-30 18:34:10 +0000124 iput(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000125 }
126
127 gfs2_glock_dq_uninit(&sdp->sd_live_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000128 gfs2_clear_rgrpd(sdp);
129 gfs2_jindex_free(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000130 /* Take apart glock structures and buffer lists */
131 gfs2_gl_hash_clear(sdp, WAIT);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000132 /* Unmount the locking protocol */
133 gfs2_lm_unmount(sdp);
134
135 /* At this point, we're through participating in the lockspace */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000136 gfs2_sys_fs_del(sdp);
Steven Whitehouseff6af412006-09-09 16:56:34 -0400137 kfree(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000138}
139
140/**
Steven Whitehouse4a221952006-11-01 09:57:57 -0500141 * gfs2_write_super
142 * @sb: the superblock
David Teiglandb3b94fa2006-01-16 16:50:04 +0000143 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000144 */
145
146static void gfs2_write_super(struct super_block *sb)
147{
Steven Whitehouse4a221952006-11-01 09:57:57 -0500148 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 */
157static int gfs2_sync_fs(struct super_block *sb, int wait)
158{
159 sb->s_dirt = 0;
Steven Whitehouseb0041572006-11-23 10:51:34 -0500160 if (wait)
161 gfs2_log_flush(sb->s_fs_info, NULL);
Steven Whitehouse4a221952006-11-01 09:57:57 -0500162 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000163}
164
165/**
166 * gfs2_write_super_lockfs - prevent further writes to the filesystem
167 * @sb: the VFS structure for the filesystem
168 *
169 */
170
171static void gfs2_write_super_lockfs(struct super_block *sb)
172{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500173 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000174 int error;
175
David Teiglandb3b94fa2006-01-16 16:50:04 +0000176 for (;;) {
177 error = gfs2_freeze_fs(sdp);
178 if (!error)
179 break;
180
181 switch (error) {
182 case -EBUSY:
183 fs_err(sdp, "waiting for recovery before freeze\n");
184 break;
185
186 default:
187 fs_err(sdp, "error freezing FS: %d\n", error);
188 break;
189 }
190
191 fs_err(sdp, "retrying...\n");
192 msleep(1000);
193 }
194}
195
196/**
197 * gfs2_unlockfs - reallow writes to the filesystem
198 * @sb: the VFS structure for the filesystem
199 *
200 */
201
202static void gfs2_unlockfs(struct super_block *sb)
203{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400204 gfs2_unfreeze_fs(sb->s_fs_info);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000205}
206
207/**
208 * gfs2_statfs - Gather and return stats about the filesystem
209 * @sb: The superblock
210 * @statfsbuf: The buffer
211 *
212 * Returns: 0 on success or error code
213 */
214
Steven Whitehouse0c0834a2006-07-03 11:38:01 -0400215static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000216{
Steven Whitehouse0c0834a2006-07-03 11:38:01 -0400217 struct super_block *sb = dentry->d_inode->i_sb;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500218 struct gfs2_sbd *sdp = sb->s_fs_info;
Al Virobd209cc2006-10-13 23:43:19 -0400219 struct gfs2_statfs_change_host sc;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000220 int error;
221
David Teiglandb3b94fa2006-01-16 16:50:04 +0000222 if (gfs2_tune_get(sdp, gt_statfs_slow))
223 error = gfs2_statfs_slow(sdp, &sc);
224 else
225 error = gfs2_statfs_i(sdp, &sc);
226
227 if (error)
228 return error;
229
David Teiglandb3b94fa2006-01-16 16:50:04 +0000230 buf->f_type = GFS2_MAGIC;
231 buf->f_bsize = sdp->sd_sb.sb_bsize;
232 buf->f_blocks = sc.sc_total;
233 buf->f_bfree = sc.sc_free;
234 buf->f_bavail = sc.sc_free;
235 buf->f_files = sc.sc_dinodes + sc.sc_free;
236 buf->f_ffree = sc.sc_free;
237 buf->f_namelen = GFS2_FNAMESIZE;
238
239 return 0;
240}
241
242/**
243 * gfs2_remount_fs - called when the FS is remounted
244 * @sb: the filesystem
245 * @flags: the remount flags
246 * @data: extra data passed in (not used right now)
247 *
248 * Returns: errno
249 */
250
251static int gfs2_remount_fs(struct super_block *sb, int *flags, char *data)
252{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500253 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000254 int error;
255
David Teiglandb3b94fa2006-01-16 16:50:04 +0000256 error = gfs2_mount_args(sdp, data, 1);
257 if (error)
258 return error;
259
260 if (sdp->sd_args.ar_spectator)
261 *flags |= MS_RDONLY;
262 else {
263 if (*flags & MS_RDONLY) {
264 if (!(sb->s_flags & MS_RDONLY))
265 error = gfs2_make_fs_ro(sdp);
266 } else if (!(*flags & MS_RDONLY) &&
267 (sb->s_flags & MS_RDONLY)) {
268 error = gfs2_make_fs_rw(sdp);
269 }
270 }
271
272 if (*flags & (MS_NOATIME | MS_NODIRATIME))
273 set_bit(SDF_NOATIME, &sdp->sd_flags);
274 else
275 clear_bit(SDF_NOATIME, &sdp->sd_flags);
276
277 /* Don't let the VFS update atimes. GFS2 handles this itself. */
278 *flags |= MS_NOATIME | MS_NODIRATIME;
279
280 return error;
281}
282
283/**
284 * gfs2_clear_inode - Deallocate an inode when VFS is done with it
285 * @inode: The VFS inode
286 *
287 */
288
289static void gfs2_clear_inode(struct inode *inode)
290{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400291 /* This tells us its a "real" inode and not one which only
292 * serves to contain an address space (see rgrp.c, meta_io.c)
293 * which therefore doesn't have its own glocks.
294 */
Theodore Ts'obba9dfd2006-09-27 01:50:31 -0700295 if (inode->i_private) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400296 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400297 ip->i_gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000298 gfs2_glock_schedule_for_reclaim(ip->i_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400299 gfs2_glock_put(ip->i_gl);
300 ip->i_gl = NULL;
301 if (ip->i_iopen_gh.gh_gl)
302 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000303 }
304}
305
306/**
307 * gfs2_show_options - Show mount options for /proc/mounts
308 * @s: seq_file structure
309 * @mnt: vfsmount
310 *
311 * Returns: 0 on success or error code
312 */
313
314static int gfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
315{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500316 struct gfs2_sbd *sdp = mnt->mnt_sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000317 struct gfs2_args *args = &sdp->sd_args;
318
David Teiglandb3b94fa2006-01-16 16:50:04 +0000319 if (args->ar_lockproto[0])
320 seq_printf(s, ",lockproto=%s", args->ar_lockproto);
321 if (args->ar_locktable[0])
322 seq_printf(s, ",locktable=%s", args->ar_locktable);
323 if (args->ar_hostdata[0])
324 seq_printf(s, ",hostdata=%s", args->ar_hostdata);
325 if (args->ar_spectator)
326 seq_printf(s, ",spectator");
327 if (args->ar_ignore_local_fs)
328 seq_printf(s, ",ignore_local_fs");
329 if (args->ar_localflocks)
330 seq_printf(s, ",localflocks");
331 if (args->ar_localcaching)
332 seq_printf(s, ",localcaching");
333 if (args->ar_debug)
334 seq_printf(s, ",debug");
335 if (args->ar_upgrade)
336 seq_printf(s, ",upgrade");
337 if (args->ar_num_glockd != GFS2_GLOCKD_DEFAULT)
338 seq_printf(s, ",num_glockd=%u", args->ar_num_glockd);
339 if (args->ar_posix_acl)
340 seq_printf(s, ",acl");
341 if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
342 char *state;
343 switch (args->ar_quota) {
344 case GFS2_QUOTA_OFF:
345 state = "off";
346 break;
347 case GFS2_QUOTA_ACCOUNT:
348 state = "account";
349 break;
350 case GFS2_QUOTA_ON:
351 state = "on";
352 break;
353 default:
354 state = "unknown";
355 break;
356 }
357 seq_printf(s, ",quota=%s", state);
358 }
359 if (args->ar_suiddir)
360 seq_printf(s, ",suiddir");
361 if (args->ar_data != GFS2_DATA_DEFAULT) {
362 char *state;
363 switch (args->ar_data) {
364 case GFS2_DATA_WRITEBACK:
365 state = "writeback";
366 break;
367 case GFS2_DATA_ORDERED:
368 state = "ordered";
369 break;
370 default:
371 state = "unknown";
372 break;
373 }
374 seq_printf(s, ",data=%s", state);
375 }
376
377 return 0;
378}
379
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400380/*
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400381 * We have to (at the moment) hold the inodes main lock to cover
382 * the gap between unlocking the shared lock on the iopen lock and
383 * taking the exclusive lock. I'd rather do a shared -> exclusive
384 * conversion on the iopen lock, but we can change that later. This
385 * is safe, just less efficient.
386 */
387static void gfs2_delete_inode(struct inode *inode)
388{
389 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
390 struct gfs2_inode *ip = GFS2_I(inode);
391 struct gfs2_holder gh;
392 int error;
393
Theodore Ts'obba9dfd2006-09-27 01:50:31 -0700394 if (!inode->i_private)
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400395 goto out;
396
Steven Whitehouseb0041572006-11-23 10:51:34 -0500397 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB, &gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400398 if (unlikely(error)) {
399 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
400 goto out;
401 }
402
403 gfs2_glock_dq(&ip->i_iopen_gh);
404 gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &ip->i_iopen_gh);
405 error = gfs2_glock_nq(&ip->i_iopen_gh);
406 if (error)
407 goto out_uninit;
408
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500409 if (S_ISDIR(inode->i_mode) &&
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400410 (ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
411 error = gfs2_dir_exhash_dealloc(ip);
412 if (error)
413 goto out_unlock;
414 }
415
416 if (ip->i_di.di_eattr) {
417 error = gfs2_ea_dealloc(ip);
418 if (error)
419 goto out_unlock;
420 }
421
422 if (!gfs2_is_stuffed(ip)) {
423 error = gfs2_file_dealloc(ip);
424 if (error)
425 goto out_unlock;
426 }
427
428 error = gfs2_dinode_dealloc(ip);
429
430out_unlock:
431 gfs2_glock_dq(&ip->i_iopen_gh);
432out_uninit:
433 gfs2_holder_uninit(&ip->i_iopen_gh);
434 gfs2_glock_dq_uninit(&gh);
435 if (error)
436 fs_warn(sdp, "gfs2_delete_inode: %d\n", error);
437out:
438 truncate_inode_pages(&inode->i_data, 0);
439 clear_inode(inode);
440}
441
442
443
Steven Whitehouse320dd102006-05-18 16:25:27 -0400444static struct inode *gfs2_alloc_inode(struct super_block *sb)
445{
446 struct gfs2_sbd *sdp = sb->s_fs_info;
447 struct gfs2_inode *ip;
448
449 ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
450 if (ip) {
451 ip->i_flags = 0;
452 ip->i_gl = NULL;
Steven Whitehouse320dd102006-05-18 16:25:27 -0400453 ip->i_greedy = gfs2_tune_get(sdp, gt_greedy_default);
454 ip->i_last_pfault = jiffies;
455 }
456 return &ip->i_inode;
457}
458
459static void gfs2_destroy_inode(struct inode *inode)
460{
461 kmem_cache_free(gfs2_inode_cachep, inode);
462}
463
David Teiglandb3b94fa2006-01-16 16:50:04 +0000464struct super_operations gfs2_super_ops = {
Steven Whitehouse4a221952006-11-01 09:57:57 -0500465 .alloc_inode = gfs2_alloc_inode,
466 .destroy_inode = gfs2_destroy_inode,
467 .write_inode = gfs2_write_inode,
468 .delete_inode = gfs2_delete_inode,
469 .put_super = gfs2_put_super,
470 .write_super = gfs2_write_super,
471 .sync_fs = gfs2_sync_fs,
472 .write_super_lockfs = gfs2_write_super_lockfs,
473 .unlockfs = gfs2_unlockfs,
474 .statfs = gfs2_statfs,
475 .remount_fs = gfs2_remount_fs,
476 .clear_inode = gfs2_clear_inode,
477 .show_options = gfs2_show_options,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000478};
479