blob: 06f06f7773d044f01ed58e5eaab893fc1051fc15 [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/**
141 * gfs2_write_super - disk commit all incore transactions
142 * @sb: the filesystem
143 *
144 * This function is called every time sync(2) is called.
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400145 * After this exits, all dirty buffers are synced.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000146 */
147
148static void gfs2_write_super(struct super_block *sb)
149{
Steven Whitehouse5acd3962006-09-04 16:16:45 -0400150 gfs2_log_flush(sb->s_fs_info, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000151}
152
153/**
154 * gfs2_write_super_lockfs - prevent further writes to the filesystem
155 * @sb: the VFS structure for the filesystem
156 *
157 */
158
159static void gfs2_write_super_lockfs(struct super_block *sb)
160{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500161 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000162 int error;
163
David Teiglandb3b94fa2006-01-16 16:50:04 +0000164 for (;;) {
165 error = gfs2_freeze_fs(sdp);
166 if (!error)
167 break;
168
169 switch (error) {
170 case -EBUSY:
171 fs_err(sdp, "waiting for recovery before freeze\n");
172 break;
173
174 default:
175 fs_err(sdp, "error freezing FS: %d\n", error);
176 break;
177 }
178
179 fs_err(sdp, "retrying...\n");
180 msleep(1000);
181 }
182}
183
184/**
185 * gfs2_unlockfs - reallow writes to the filesystem
186 * @sb: the VFS structure for the filesystem
187 *
188 */
189
190static void gfs2_unlockfs(struct super_block *sb)
191{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400192 gfs2_unfreeze_fs(sb->s_fs_info);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000193}
194
195/**
196 * gfs2_statfs - Gather and return stats about the filesystem
197 * @sb: The superblock
198 * @statfsbuf: The buffer
199 *
200 * Returns: 0 on success or error code
201 */
202
Steven Whitehouse0c0834a2006-07-03 11:38:01 -0400203static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000204{
Steven Whitehouse0c0834a2006-07-03 11:38:01 -0400205 struct super_block *sb = dentry->d_inode->i_sb;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500206 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000207 struct gfs2_statfs_change sc;
208 int error;
209
David Teiglandb3b94fa2006-01-16 16:50:04 +0000210 if (gfs2_tune_get(sdp, gt_statfs_slow))
211 error = gfs2_statfs_slow(sdp, &sc);
212 else
213 error = gfs2_statfs_i(sdp, &sc);
214
215 if (error)
216 return error;
217
David Teiglandb3b94fa2006-01-16 16:50:04 +0000218 buf->f_type = GFS2_MAGIC;
219 buf->f_bsize = sdp->sd_sb.sb_bsize;
220 buf->f_blocks = sc.sc_total;
221 buf->f_bfree = sc.sc_free;
222 buf->f_bavail = sc.sc_free;
223 buf->f_files = sc.sc_dinodes + sc.sc_free;
224 buf->f_ffree = sc.sc_free;
225 buf->f_namelen = GFS2_FNAMESIZE;
226
227 return 0;
228}
229
230/**
231 * gfs2_remount_fs - called when the FS is remounted
232 * @sb: the filesystem
233 * @flags: the remount flags
234 * @data: extra data passed in (not used right now)
235 *
236 * Returns: errno
237 */
238
239static int gfs2_remount_fs(struct super_block *sb, int *flags, char *data)
240{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500241 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000242 int error;
243
David Teiglandb3b94fa2006-01-16 16:50:04 +0000244 error = gfs2_mount_args(sdp, data, 1);
245 if (error)
246 return error;
247
248 if (sdp->sd_args.ar_spectator)
249 *flags |= MS_RDONLY;
250 else {
251 if (*flags & MS_RDONLY) {
252 if (!(sb->s_flags & MS_RDONLY))
253 error = gfs2_make_fs_ro(sdp);
254 } else if (!(*flags & MS_RDONLY) &&
255 (sb->s_flags & MS_RDONLY)) {
256 error = gfs2_make_fs_rw(sdp);
257 }
258 }
259
260 if (*flags & (MS_NOATIME | MS_NODIRATIME))
261 set_bit(SDF_NOATIME, &sdp->sd_flags);
262 else
263 clear_bit(SDF_NOATIME, &sdp->sd_flags);
264
265 /* Don't let the VFS update atimes. GFS2 handles this itself. */
266 *flags |= MS_NOATIME | MS_NODIRATIME;
267
268 return error;
269}
270
271/**
272 * gfs2_clear_inode - Deallocate an inode when VFS is done with it
273 * @inode: The VFS inode
274 *
275 */
276
277static void gfs2_clear_inode(struct inode *inode)
278{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400279 /* This tells us its a "real" inode and not one which only
280 * serves to contain an address space (see rgrp.c, meta_io.c)
281 * which therefore doesn't have its own glocks.
282 */
Theodore Ts'obba9dfd2006-09-27 01:50:31 -0700283 if (inode->i_private) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400284 struct gfs2_inode *ip = GFS2_I(inode);
285 gfs2_glock_inode_squish(inode);
286 gfs2_assert(inode->i_sb->s_fs_info, ip->i_gl->gl_state == LM_ST_UNLOCKED);
287 ip->i_gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000288 gfs2_glock_schedule_for_reclaim(ip->i_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400289 gfs2_glock_put(ip->i_gl);
290 ip->i_gl = NULL;
291 if (ip->i_iopen_gh.gh_gl)
292 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000293 }
294}
295
296/**
297 * gfs2_show_options - Show mount options for /proc/mounts
298 * @s: seq_file structure
299 * @mnt: vfsmount
300 *
301 * Returns: 0 on success or error code
302 */
303
304static int gfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
305{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500306 struct gfs2_sbd *sdp = mnt->mnt_sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000307 struct gfs2_args *args = &sdp->sd_args;
308
David Teiglandb3b94fa2006-01-16 16:50:04 +0000309 if (args->ar_lockproto[0])
310 seq_printf(s, ",lockproto=%s", args->ar_lockproto);
311 if (args->ar_locktable[0])
312 seq_printf(s, ",locktable=%s", args->ar_locktable);
313 if (args->ar_hostdata[0])
314 seq_printf(s, ",hostdata=%s", args->ar_hostdata);
315 if (args->ar_spectator)
316 seq_printf(s, ",spectator");
317 if (args->ar_ignore_local_fs)
318 seq_printf(s, ",ignore_local_fs");
319 if (args->ar_localflocks)
320 seq_printf(s, ",localflocks");
321 if (args->ar_localcaching)
322 seq_printf(s, ",localcaching");
323 if (args->ar_debug)
324 seq_printf(s, ",debug");
325 if (args->ar_upgrade)
326 seq_printf(s, ",upgrade");
327 if (args->ar_num_glockd != GFS2_GLOCKD_DEFAULT)
328 seq_printf(s, ",num_glockd=%u", args->ar_num_glockd);
329 if (args->ar_posix_acl)
330 seq_printf(s, ",acl");
331 if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
332 char *state;
333 switch (args->ar_quota) {
334 case GFS2_QUOTA_OFF:
335 state = "off";
336 break;
337 case GFS2_QUOTA_ACCOUNT:
338 state = "account";
339 break;
340 case GFS2_QUOTA_ON:
341 state = "on";
342 break;
343 default:
344 state = "unknown";
345 break;
346 }
347 seq_printf(s, ",quota=%s", state);
348 }
349 if (args->ar_suiddir)
350 seq_printf(s, ",suiddir");
351 if (args->ar_data != GFS2_DATA_DEFAULT) {
352 char *state;
353 switch (args->ar_data) {
354 case GFS2_DATA_WRITEBACK:
355 state = "writeback";
356 break;
357 case GFS2_DATA_ORDERED:
358 state = "ordered";
359 break;
360 default:
361 state = "unknown";
362 break;
363 }
364 seq_printf(s, ",data=%s", state);
365 }
366
367 return 0;
368}
369
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400370/*
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400371 * We have to (at the moment) hold the inodes main lock to cover
372 * the gap between unlocking the shared lock on the iopen lock and
373 * taking the exclusive lock. I'd rather do a shared -> exclusive
374 * conversion on the iopen lock, but we can change that later. This
375 * is safe, just less efficient.
376 */
377static void gfs2_delete_inode(struct inode *inode)
378{
379 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
380 struct gfs2_inode *ip = GFS2_I(inode);
381 struct gfs2_holder gh;
382 int error;
383
Theodore Ts'obba9dfd2006-09-27 01:50:31 -0700384 if (!inode->i_private)
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400385 goto out;
386
387 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &gh);
388 if (unlikely(error)) {
389 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
390 goto out;
391 }
392
393 gfs2_glock_dq(&ip->i_iopen_gh);
394 gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &ip->i_iopen_gh);
395 error = gfs2_glock_nq(&ip->i_iopen_gh);
396 if (error)
397 goto out_uninit;
398
399 if (S_ISDIR(ip->i_di.di_mode) &&
400 (ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
401 error = gfs2_dir_exhash_dealloc(ip);
402 if (error)
403 goto out_unlock;
404 }
405
406 if (ip->i_di.di_eattr) {
407 error = gfs2_ea_dealloc(ip);
408 if (error)
409 goto out_unlock;
410 }
411
412 if (!gfs2_is_stuffed(ip)) {
413 error = gfs2_file_dealloc(ip);
414 if (error)
415 goto out_unlock;
416 }
417
418 error = gfs2_dinode_dealloc(ip);
419
420out_unlock:
421 gfs2_glock_dq(&ip->i_iopen_gh);
422out_uninit:
423 gfs2_holder_uninit(&ip->i_iopen_gh);
424 gfs2_glock_dq_uninit(&gh);
425 if (error)
426 fs_warn(sdp, "gfs2_delete_inode: %d\n", error);
427out:
428 truncate_inode_pages(&inode->i_data, 0);
429 clear_inode(inode);
430}
431
432
433
Steven Whitehouse320dd102006-05-18 16:25:27 -0400434static struct inode *gfs2_alloc_inode(struct super_block *sb)
435{
436 struct gfs2_sbd *sdp = sb->s_fs_info;
437 struct gfs2_inode *ip;
438
439 ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
440 if (ip) {
441 ip->i_flags = 0;
442 ip->i_gl = NULL;
Steven Whitehouse320dd102006-05-18 16:25:27 -0400443 ip->i_greedy = gfs2_tune_get(sdp, gt_greedy_default);
444 ip->i_last_pfault = jiffies;
445 }
446 return &ip->i_inode;
447}
448
449static void gfs2_destroy_inode(struct inode *inode)
450{
451 kmem_cache_free(gfs2_inode_cachep, inode);
452}
453
David Teiglandb3b94fa2006-01-16 16:50:04 +0000454struct super_operations gfs2_super_ops = {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400455 .alloc_inode = gfs2_alloc_inode,
456 .destroy_inode = gfs2_destroy_inode,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000457 .write_inode = gfs2_write_inode,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400458 .delete_inode = gfs2_delete_inode,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000459 .put_super = gfs2_put_super,
460 .write_super = gfs2_write_super,
461 .write_super_lockfs = gfs2_write_super_lockfs,
462 .unlockfs = gfs2_unlockfs,
463 .statfs = gfs2_statfs,
464 .remount_fs = gfs2_remount_fs,
465 .clear_inode = gfs2_clear_inode,
466 .show_options = gfs2_show_options,
467};
468