blob: 2e392c994ab1110f69655f324f1012a14592f4fa [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
7 * of the GNU General Public License v.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>
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
David Teiglandb3b94fa2006-01-16 16:50:04 +000081 /* Unfreeze the filesystem, if we need to */
82
Steven Whitehousef55ab262006-02-21 12:51:39 +000083 mutex_lock(&sdp->sd_freeze_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +000084 if (sdp->sd_freeze_count)
85 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
Steven Whitehousef55ab262006-02-21 12:51:39 +000086 mutex_unlock(&sdp->sd_freeze_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +000087
David Teiglandb3b94fa2006-01-16 16:50:04 +000088 kthread_stop(sdp->sd_quotad_process);
89 kthread_stop(sdp->sd_logd_process);
90 kthread_stop(sdp->sd_recoverd_process);
91 while (sdp->sd_glockd_num--)
92 kthread_stop(sdp->sd_glockd_process[sdp->sd_glockd_num]);
93 kthread_stop(sdp->sd_scand_process);
94
95 if (!(sb->s_flags & MS_RDONLY)) {
96 error = gfs2_make_fs_ro(sdp);
97 if (error)
98 gfs2_io_error(sdp);
99 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000100 /* At this point, we're through modifying the disk */
101
102 /* Release stuff */
103
Steven Whitehousef42faf42006-01-30 18:34:10 +0000104 iput(sdp->sd_master_dir);
105 iput(sdp->sd_jindex);
106 iput(sdp->sd_inum_inode);
107 iput(sdp->sd_statfs_inode);
108 iput(sdp->sd_rindex);
109 iput(sdp->sd_quota_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110
111 gfs2_glock_put(sdp->sd_rename_gl);
112 gfs2_glock_put(sdp->sd_trans_gl);
113
114 if (!sdp->sd_args.ar_spectator) {
115 gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
116 gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
117 gfs2_glock_dq_uninit(&sdp->sd_ir_gh);
118 gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000119 gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
Steven Whitehousef42faf42006-01-30 18:34:10 +0000120 iput(sdp->sd_ir_inode);
121 iput(sdp->sd_sc_inode);
Steven Whitehousef42faf42006-01-30 18:34:10 +0000122 iput(sdp->sd_qc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000123 }
124
125 gfs2_glock_dq_uninit(&sdp->sd_live_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000126 gfs2_clear_rgrpd(sdp);
127 gfs2_jindex_free(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000128 /* Take apart glock structures and buffer lists */
129 gfs2_gl_hash_clear(sdp, WAIT);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000130 /* Unmount the locking protocol */
131 gfs2_lm_unmount(sdp);
132
133 /* At this point, we're through participating in the lockspace */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000134 gfs2_sys_fs_del(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000135 vfree(sdp);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500136 sb->s_fs_info = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000137}
138
139/**
140 * gfs2_write_super - disk commit all incore transactions
141 * @sb: the filesystem
142 *
143 * This function is called every time sync(2) is called.
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400144 * After this exits, all dirty buffers are synced.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000145 */
146
147static void gfs2_write_super(struct super_block *sb)
148{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500149 struct gfs2_sbd *sdp = sb->s_fs_info;
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400150 gfs2_log_flush(sdp, 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 Whitehouse5c676f62006-02-27 17:23:27 -0500192 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000193 gfs2_unfreeze_fs(sdp);
194}
195
196/**
197 * gfs2_statfs - Gather and return stats about the filesystem
198 * @sb: The superblock
199 * @statfsbuf: The buffer
200 *
201 * Returns: 0 on success or error code
202 */
203
Steven Whitehouse0c0834a2006-07-03 11:38:01 -0400204static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000205{
Steven Whitehouse0c0834a2006-07-03 11:38:01 -0400206 struct super_block *sb = dentry->d_inode->i_sb;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500207 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000208 struct gfs2_statfs_change sc;
209 int error;
210
David Teiglandb3b94fa2006-01-16 16:50:04 +0000211 if (gfs2_tune_get(sdp, gt_statfs_slow))
212 error = gfs2_statfs_slow(sdp, &sc);
213 else
214 error = gfs2_statfs_i(sdp, &sc);
215
216 if (error)
217 return error;
218
David Teiglandb3b94fa2006-01-16 16:50:04 +0000219 buf->f_type = GFS2_MAGIC;
220 buf->f_bsize = sdp->sd_sb.sb_bsize;
221 buf->f_blocks = sc.sc_total;
222 buf->f_bfree = sc.sc_free;
223 buf->f_bavail = sc.sc_free;
224 buf->f_files = sc.sc_dinodes + sc.sc_free;
225 buf->f_ffree = sc.sc_free;
226 buf->f_namelen = GFS2_FNAMESIZE;
227
228 return 0;
229}
230
231/**
232 * gfs2_remount_fs - called when the FS is remounted
233 * @sb: the filesystem
234 * @flags: the remount flags
235 * @data: extra data passed in (not used right now)
236 *
237 * Returns: errno
238 */
239
240static int gfs2_remount_fs(struct super_block *sb, int *flags, char *data)
241{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500242 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000243 int error;
244
David Teiglandb3b94fa2006-01-16 16:50:04 +0000245 error = gfs2_mount_args(sdp, data, 1);
246 if (error)
247 return error;
248
249 if (sdp->sd_args.ar_spectator)
250 *flags |= MS_RDONLY;
251 else {
252 if (*flags & MS_RDONLY) {
253 if (!(sb->s_flags & MS_RDONLY))
254 error = gfs2_make_fs_ro(sdp);
255 } else if (!(*flags & MS_RDONLY) &&
256 (sb->s_flags & MS_RDONLY)) {
257 error = gfs2_make_fs_rw(sdp);
258 }
259 }
260
261 if (*flags & (MS_NOATIME | MS_NODIRATIME))
262 set_bit(SDF_NOATIME, &sdp->sd_flags);
263 else
264 clear_bit(SDF_NOATIME, &sdp->sd_flags);
265
266 /* Don't let the VFS update atimes. GFS2 handles this itself. */
267 *flags |= MS_NOATIME | MS_NODIRATIME;
268
269 return error;
270}
271
272/**
273 * gfs2_clear_inode - Deallocate an inode when VFS is done with it
274 * @inode: The VFS inode
275 *
276 */
277
278static void gfs2_clear_inode(struct inode *inode)
279{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400280 /* This tells us its a "real" inode and not one which only
281 * serves to contain an address space (see rgrp.c, meta_io.c)
282 * which therefore doesn't have its own glocks.
283 */
284 if (inode->u.generic_ip) {
285 struct gfs2_inode *ip = GFS2_I(inode);
286 gfs2_glock_inode_squish(inode);
287 gfs2_assert(inode->i_sb->s_fs_info, ip->i_gl->gl_state == LM_ST_UNLOCKED);
288 ip->i_gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000289 gfs2_glock_schedule_for_reclaim(ip->i_gl);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400290 gfs2_glock_put(ip->i_gl);
291 ip->i_gl = NULL;
292 if (ip->i_iopen_gh.gh_gl)
293 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000294 }
295}
296
297/**
298 * gfs2_show_options - Show mount options for /proc/mounts
299 * @s: seq_file structure
300 * @mnt: vfsmount
301 *
302 * Returns: 0 on success or error code
303 */
304
305static int gfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
306{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500307 struct gfs2_sbd *sdp = mnt->mnt_sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000308 struct gfs2_args *args = &sdp->sd_args;
309
David Teiglandb3b94fa2006-01-16 16:50:04 +0000310 if (args->ar_lockproto[0])
311 seq_printf(s, ",lockproto=%s", args->ar_lockproto);
312 if (args->ar_locktable[0])
313 seq_printf(s, ",locktable=%s", args->ar_locktable);
314 if (args->ar_hostdata[0])
315 seq_printf(s, ",hostdata=%s", args->ar_hostdata);
316 if (args->ar_spectator)
317 seq_printf(s, ",spectator");
318 if (args->ar_ignore_local_fs)
319 seq_printf(s, ",ignore_local_fs");
320 if (args->ar_localflocks)
321 seq_printf(s, ",localflocks");
322 if (args->ar_localcaching)
323 seq_printf(s, ",localcaching");
324 if (args->ar_debug)
325 seq_printf(s, ",debug");
326 if (args->ar_upgrade)
327 seq_printf(s, ",upgrade");
328 if (args->ar_num_glockd != GFS2_GLOCKD_DEFAULT)
329 seq_printf(s, ",num_glockd=%u", args->ar_num_glockd);
330 if (args->ar_posix_acl)
331 seq_printf(s, ",acl");
332 if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
333 char *state;
334 switch (args->ar_quota) {
335 case GFS2_QUOTA_OFF:
336 state = "off";
337 break;
338 case GFS2_QUOTA_ACCOUNT:
339 state = "account";
340 break;
341 case GFS2_QUOTA_ON:
342 state = "on";
343 break;
344 default:
345 state = "unknown";
346 break;
347 }
348 seq_printf(s, ",quota=%s", state);
349 }
350 if (args->ar_suiddir)
351 seq_printf(s, ",suiddir");
352 if (args->ar_data != GFS2_DATA_DEFAULT) {
353 char *state;
354 switch (args->ar_data) {
355 case GFS2_DATA_WRITEBACK:
356 state = "writeback";
357 break;
358 case GFS2_DATA_ORDERED:
359 state = "ordered";
360 break;
361 default:
362 state = "unknown";
363 break;
364 }
365 seq_printf(s, ",data=%s", state);
366 }
367
368 return 0;
369}
370
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400371/*
372 * We have to (at the moment) hold the inodes main lock to cover
373 * the gap between unlocking the shared lock on the iopen lock and
374 * taking the exclusive lock. I'd rather do a shared -> exclusive
375 * conversion on the iopen lock, but we can change that later. This
376 * is safe, just less efficient.
377 */
378static void gfs2_delete_inode(struct inode *inode)
379{
380 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
381 struct gfs2_inode *ip = GFS2_I(inode);
382 struct gfs2_holder gh;
383 int error;
384
385 if (!inode->u.generic_ip)
386 goto out;
387
388 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &gh);
389 if (unlikely(error)) {
390 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
391 goto out;
392 }
393
394 gfs2_glock_dq(&ip->i_iopen_gh);
395 gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &ip->i_iopen_gh);
396 error = gfs2_glock_nq(&ip->i_iopen_gh);
397 if (error)
398 goto out_uninit;
399
400 if (S_ISDIR(ip->i_di.di_mode) &&
401 (ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
402 error = gfs2_dir_exhash_dealloc(ip);
403 if (error)
404 goto out_unlock;
405 }
406
407 if (ip->i_di.di_eattr) {
408 error = gfs2_ea_dealloc(ip);
409 if (error)
410 goto out_unlock;
411 }
412
413 if (!gfs2_is_stuffed(ip)) {
414 error = gfs2_file_dealloc(ip);
415 if (error)
416 goto out_unlock;
417 }
418
419 error = gfs2_dinode_dealloc(ip);
420
421out_unlock:
422 gfs2_glock_dq(&ip->i_iopen_gh);
423out_uninit:
424 gfs2_holder_uninit(&ip->i_iopen_gh);
425 gfs2_glock_dq_uninit(&gh);
426 if (error)
427 fs_warn(sdp, "gfs2_delete_inode: %d\n", error);
428out:
429 truncate_inode_pages(&inode->i_data, 0);
430 clear_inode(inode);
431}
432
433
434
Steven Whitehouse320dd102006-05-18 16:25:27 -0400435static struct inode *gfs2_alloc_inode(struct super_block *sb)
436{
437 struct gfs2_sbd *sdp = sb->s_fs_info;
438 struct gfs2_inode *ip;
439
440 ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
441 if (ip) {
442 ip->i_flags = 0;
443 ip->i_gl = NULL;
Steven Whitehouse320dd102006-05-18 16:25:27 -0400444 ip->i_greedy = gfs2_tune_get(sdp, gt_greedy_default);
445 ip->i_last_pfault = jiffies;
446 }
447 return &ip->i_inode;
448}
449
450static void gfs2_destroy_inode(struct inode *inode)
451{
452 kmem_cache_free(gfs2_inode_cachep, inode);
453}
454
David Teiglandb3b94fa2006-01-16 16:50:04 +0000455struct super_operations gfs2_super_ops = {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400456 .alloc_inode = gfs2_alloc_inode,
457 .destroy_inode = gfs2_destroy_inode,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000458 .write_inode = gfs2_write_inode,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400459 .delete_inode = gfs2_delete_inode,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000460 .put_super = gfs2_put_super,
461 .write_super = gfs2_write_super,
462 .write_super_lockfs = gfs2_write_super_lockfs,
463 .unlockfs = gfs2_unlockfs,
464 .statfs = gfs2_statfs,
465 .remount_fs = gfs2_remount_fs,
466 .clear_inode = gfs2_clear_inode,
467 .show_options = gfs2_show_options,
468};
469