super.c 24.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
/*
 *   Copyright (C) International Business Machines Corp., 2000-2004
 *   Portions Copyright (C) Christoph Hellwig, 2001-2002
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
D
Dave Kleikamp 已提交
7
 *   the Free Software Foundation; either version 2 of the License, or
L
Linus Torvalds 已提交
8
 *   (at your option) any later version.
D
Dave Kleikamp 已提交
9
 *
L
Linus Torvalds 已提交
10 11 12 13 14 15
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
D
Dave Kleikamp 已提交
16
 *   along with this program;  if not, write to the Free Software
L
Linus Torvalds 已提交
17 18 19 20 21 22 23 24
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/fs.h>
#include <linux/module.h>
#include <linux/parser.h>
#include <linux/completion.h>
#include <linux/vfs.h>
25
#include <linux/quotaops.h>
26
#include <linux/mount.h>
L
Linus Torvalds 已提交
27
#include <linux/moduleparam.h>
C
Christoph Hellwig 已提交
28
#include <linux/kthread.h>
29
#include <linux/posix_acl.h>
30
#include <linux/buffer_head.h>
31
#include <linux/exportfs.h>
C
Coly Li 已提交
32
#include <linux/crc32.h>
33
#include <linux/slab.h>
34
#include <linux/uaccess.h>
35
#include <linux/seq_file.h>
36
#include <linux/blkdev.h>
L
Linus Torvalds 已提交
37 38 39

#include "jfs_incore.h"
#include "jfs_filsys.h"
40
#include "jfs_inode.h"
L
Linus Torvalds 已提交
41 42 43 44 45 46
#include "jfs_metapage.h"
#include "jfs_superblock.h"
#include "jfs_dmap.h"
#include "jfs_imap.h"
#include "jfs_acl.h"
#include "jfs_debug.h"
47
#include "jfs_xattr.h"
48
#include "jfs_dinode.h"
L
Linus Torvalds 已提交
49 50 51 52 53

MODULE_DESCRIPTION("The Journaled Filesystem (JFS)");
MODULE_AUTHOR("Steve Best/Dave Kleikamp/Barry Arndt, IBM");
MODULE_LICENSE("GPL");

54
static struct kmem_cache *jfs_inode_cachep;
L
Linus Torvalds 已提交
55

56
static const struct super_operations jfs_super_operations;
57
static const struct export_operations jfs_export_operations;
L
Linus Torvalds 已提交
58 59 60
static struct file_system_type jfs_fs_type;

#define MAX_COMMIT_THREADS 64
61
static int commit_threads;
L
Linus Torvalds 已提交
62 63 64
module_param(commit_threads, int, 0);
MODULE_PARM_DESC(commit_threads, "Number of commit threads");

C
Christoph Hellwig 已提交
65 66 67
static struct task_struct *jfsCommitThread[MAX_COMMIT_THREADS];
struct task_struct *jfsIOthread;
struct task_struct *jfsSyncThread;
L
Linus Torvalds 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

#ifdef CONFIG_JFS_DEBUG
int jfsloglevel = JFS_LOGLEVEL_WARN;
module_param(jfsloglevel, int, 0644);
MODULE_PARM_DESC(jfsloglevel, "Specify JFS loglevel (0, 1 or 2)");
#endif

static void jfs_handle_error(struct super_block *sb)
{
	struct jfs_sb_info *sbi = JFS_SBI(sb);

	if (sb->s_flags & MS_RDONLY)
		return;

	updateSuper(sb, FM_DIRTY);

	if (sbi->flag & JFS_ERR_PANIC)
		panic("JFS (device %s): panic forced after error\n",
			sb->s_id);
	else if (sbi->flag & JFS_ERR_REMOUNT_RO) {
88
		jfs_err("ERROR: (device %s): remounting filesystem as read-only",
L
Linus Torvalds 已提交
89 90
			sb->s_id);
		sb->s_flags |= MS_RDONLY;
D
Dave Kleikamp 已提交
91
	}
L
Linus Torvalds 已提交
92 93 94 95

	/* nothing is done for continue beyond marking the superblock dirty */
}

J
Joe Perches 已提交
96
void jfs_error(struct super_block *sb, const char *fmt, ...)
L
Linus Torvalds 已提交
97
{
J
Joe Perches 已提交
98
	struct va_format vaf;
L
Linus Torvalds 已提交
99 100
	va_list args;

J
Joe Perches 已提交
101 102 103 104
	va_start(args, fmt);

	vaf.fmt = fmt;
	vaf.va = &args;
L
Linus Torvalds 已提交
105

106
	pr_err("ERROR: (device %s): %ps: %pV\n",
J
Joe Perches 已提交
107 108 109
	       sb->s_id, __builtin_return_address(0), &vaf);

	va_end(args);
L
Linus Torvalds 已提交
110 111 112 113 114 115 116 117 118 119 120

	jfs_handle_error(sb);
}

static struct inode *jfs_alloc_inode(struct super_block *sb)
{
	struct jfs_inode_info *jfs_inode;

	jfs_inode = kmem_cache_alloc(jfs_inode_cachep, GFP_NOFS);
	if (!jfs_inode)
		return NULL;
J
Jan Kara 已提交
121 122 123
#ifdef CONFIG_QUOTA
	memset(&jfs_inode->i_dquot, 0, sizeof(jfs_inode->i_dquot));
#endif
L
Linus Torvalds 已提交
124 125 126
	return &jfs_inode->vfs_inode;
}

N
Nick Piggin 已提交
127 128 129 130 131 132 133
static void jfs_i_callback(struct rcu_head *head)
{
	struct inode *inode = container_of(head, struct inode, i_rcu);
	struct jfs_inode_info *ji = JFS_IP(inode);
	kmem_cache_free(jfs_inode_cachep, ji);
}

L
Linus Torvalds 已提交
134 135 136 137
static void jfs_destroy_inode(struct inode *inode)
{
	struct jfs_inode_info *ji = JFS_IP(inode);

D
Dave Kleikamp 已提交
138 139
	BUG_ON(!list_empty(&ji->anon_inode_list));

L
Linus Torvalds 已提交
140 141 142 143 144 145 146
	spin_lock_irq(&ji->ag_lock);
	if (ji->active_ag != -1) {
		struct bmap *bmap = JFS_SBI(inode->i_sb)->bmap;
		atomic_dec(&bmap->db_active[ji->active_ag]);
		ji->active_ag = -1;
	}
	spin_unlock_irq(&ji->ag_lock);
N
Nick Piggin 已提交
147
	call_rcu(&inode->i_rcu, jfs_i_callback);
L
Linus Torvalds 已提交
148 149
}

150
static int jfs_statfs(struct dentry *dentry, struct kstatfs *buf)
L
Linus Torvalds 已提交
151
{
152
	struct jfs_sb_info *sbi = JFS_SBI(dentry->d_sb);
L
Linus Torvalds 已提交
153 154 155 156 157 158 159 160 161 162 163 164
	s64 maxinodes;
	struct inomap *imap = JFS_IP(sbi->ipimap)->i_imap;

	jfs_info("In jfs_statfs");
	buf->f_type = JFS_SUPER_MAGIC;
	buf->f_bsize = sbi->bsize;
	buf->f_blocks = sbi->bmap->db_mapsize;
	buf->f_bfree = sbi->bmap->db_nfree;
	buf->f_bavail = sbi->bmap->db_nfree;
	/*
	 * If we really return the number of allocated & free inodes, some
	 * applications will fail because they won't see enough free inodes.
165
	 * We'll try to calculate some guess as to how many inodes we can
L
Linus Torvalds 已提交
166 167 168 169 170 171 172 173 174 175 176
	 * really allocate
	 *
	 * buf->f_files = atomic_read(&imap->im_numinos);
	 * buf->f_ffree = atomic_read(&imap->im_numfree);
	 */
	maxinodes = min((s64) atomic_read(&imap->im_numinos) +
			((sbi->bmap->db_nfree >> imap->im_l2nbperiext)
			 << L2INOSPEREXT), (s64) 0xffffffffLL);
	buf->f_files = maxinodes;
	buf->f_ffree = maxinodes - (atomic_read(&imap->im_numinos) -
				    atomic_read(&imap->im_numfree));
C
Coly Li 已提交
177 178 179
	buf->f_fsid.val[0] = (u32)crc32_le(0, sbi->uuid, sizeof(sbi->uuid)/2);
	buf->f_fsid.val[1] = (u32)crc32_le(0, sbi->uuid + sizeof(sbi->uuid)/2,
					sizeof(sbi->uuid)/2);
L
Linus Torvalds 已提交
180 181 182 183 184

	buf->f_namelen = JFS_NAME_MAX;
	return 0;
}

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
#ifdef CONFIG_QUOTA
static int jfs_quota_off(struct super_block *sb, int type);
static int jfs_quota_on(struct super_block *sb, int type, int format_id,
			const struct path *path);

static void jfs_quota_off_umount(struct super_block *sb)
{
	int type;

	for (type = 0; type < MAXQUOTAS; type++)
		jfs_quota_off(sb, type);
}

static const struct quotactl_ops jfs_quotactl_ops = {
	.quota_on	= jfs_quota_on,
	.quota_off	= jfs_quota_off,
	.quota_sync	= dquot_quota_sync,
	.get_state	= dquot_get_state,
	.set_info	= dquot_set_dqinfo,
	.get_dqblk	= dquot_get_dqblk,
	.set_dqblk	= dquot_set_dqblk,
	.get_nextdqblk	= dquot_get_next_dqblk,
};
#else
static inline void jfs_quota_off_umount(struct super_block *sb)
{
}
#endif

L
Linus Torvalds 已提交
214 215 216 217 218 219
static void jfs_put_super(struct super_block *sb)
{
	struct jfs_sb_info *sbi = JFS_SBI(sb);
	int rc;

	jfs_info("In jfs_put_super");
220

221
	jfs_quota_off_umount(sb);
222

L
Linus Torvalds 已提交
223 224 225
	rc = jfs_umount(sb);
	if (rc)
		jfs_err("jfs_umount failed with return code %d", rc);
226 227

	unload_nls(sbi->nls_tab);
L
Linus Torvalds 已提交
228

229 230 231
	truncate_inode_pages(sbi->direct_inode->i_mapping, 0);
	iput(sbi->direct_inode);

L
Linus Torvalds 已提交
232 233 234 235 236
	kfree(sbi);
}

enum {
	Opt_integrity, Opt_nointegrity, Opt_iocharset, Opt_resize,
237
	Opt_resize_nosize, Opt_errors, Opt_ignore, Opt_err, Opt_quota,
238 239
	Opt_usrquota, Opt_grpquota, Opt_uid, Opt_gid, Opt_umask,
	Opt_discard, Opt_nodiscard, Opt_discard_minblk
L
Linus Torvalds 已提交
240 241
};

242
static const match_table_t tokens = {
L
Linus Torvalds 已提交
243 244 245 246 247 248 249 250
	{Opt_integrity, "integrity"},
	{Opt_nointegrity, "nointegrity"},
	{Opt_iocharset, "iocharset=%s"},
	{Opt_resize, "resize=%u"},
	{Opt_resize_nosize, "resize"},
	{Opt_errors, "errors=%s"},
	{Opt_ignore, "noquota"},
	{Opt_ignore, "quota"},
251 252
	{Opt_usrquota, "usrquota"},
	{Opt_grpquota, "grpquota"},
253 254 255
	{Opt_uid, "uid=%u"},
	{Opt_gid, "gid=%u"},
	{Opt_umask, "umask=%u"},
256 257 258
	{Opt_discard, "discard"},
	{Opt_nodiscard, "nodiscard"},
	{Opt_discard_minblk, "discard=%u"},
L
Linus Torvalds 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
	{Opt_err, NULL}
};

static int parse_options(char *options, struct super_block *sb, s64 *newLVSize,
			 int *flag)
{
	void *nls_map = (void *)-1;	/* -1: no change;  NULL: none */
	char *p;
	struct jfs_sb_info *sbi = JFS_SBI(sb);

	*newLVSize = 0;

	if (!options)
		return 1;

	while ((p = strsep(&options, ",")) != NULL) {
		substring_t args[MAX_OPT_ARGS];
		int token;
		if (!*p)
			continue;

		token = match_token(p, tokens, args);
		switch (token) {
		case Opt_integrity:
			*flag &= ~JFS_NOINTEGRITY;
			break;
		case Opt_nointegrity:
			*flag |= JFS_NOINTEGRITY;
			break;
		case Opt_ignore:
			/* Silently ignore the quota options */
			/* Don't do anything ;-) */
			break;
		case Opt_iocharset:
			if (nls_map && nls_map != (void *) -1)
				unload_nls(nls_map);
			if (!strcmp(args[0].from, "none"))
				nls_map = NULL;
			else {
				nls_map = load_nls(args[0].from);
				if (!nls_map) {
300
					pr_err("JFS: charset not found\n");
L
Linus Torvalds 已提交
301 302 303 304 305 306 307
					goto cleanup;
				}
			}
			break;
		case Opt_resize:
		{
			char *resize = args[0].from;
308 309 310 311
			int rc = kstrtoll(resize, 0, newLVSize);

			if (rc)
				goto cleanup;
L
Linus Torvalds 已提交
312 313 314 315 316 317 318
			break;
		}
		case Opt_resize_nosize:
		{
			*newLVSize = sb->s_bdev->bd_inode->i_size >>
				sb->s_blocksize_bits;
			if (*newLVSize == 0)
319
				pr_err("JFS: Cannot determine volume size\n");
L
Linus Torvalds 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
			break;
		}
		case Opt_errors:
		{
			char *errors = args[0].from;
			if (!errors || !*errors)
				goto cleanup;
			if (!strcmp(errors, "continue")) {
				*flag &= ~JFS_ERR_REMOUNT_RO;
				*flag &= ~JFS_ERR_PANIC;
				*flag |= JFS_ERR_CONTINUE;
			} else if (!strcmp(errors, "remount-ro")) {
				*flag &= ~JFS_ERR_CONTINUE;
				*flag &= ~JFS_ERR_PANIC;
				*flag |= JFS_ERR_REMOUNT_RO;
			} else if (!strcmp(errors, "panic")) {
				*flag &= ~JFS_ERR_CONTINUE;
				*flag &= ~JFS_ERR_REMOUNT_RO;
				*flag |= JFS_ERR_PANIC;
			} else {
340
				pr_err("JFS: %s is an invalid error handler\n",
L
Linus Torvalds 已提交
341 342 343 344 345
				       errors);
				goto cleanup;
			}
			break;
		}
346

347
#ifdef CONFIG_QUOTA
348 349 350 351 352 353 354 355 356 357 358
		case Opt_quota:
		case Opt_usrquota:
			*flag |= JFS_USRQUOTA;
			break;
		case Opt_grpquota:
			*flag |= JFS_GRPQUOTA;
			break;
#else
		case Opt_usrquota:
		case Opt_grpquota:
		case Opt_quota:
359
			pr_err("JFS: quota operations not supported\n");
360 361
			break;
#endif
362 363 364
		case Opt_uid:
		{
			char *uid = args[0].from;
365 366 367 368 369
			uid_t val;
			int rc = kstrtouint(uid, 0, &val);

			if (rc)
				goto cleanup;
370 371 372
			sbi->uid = make_kuid(current_user_ns(), val);
			if (!uid_valid(sbi->uid))
				goto cleanup;
373 374
			break;
		}
375

376 377 378
		case Opt_gid:
		{
			char *gid = args[0].from;
379 380 381 382 383
			gid_t val;
			int rc = kstrtouint(gid, 0, &val);

			if (rc)
				goto cleanup;
384 385 386
			sbi->gid = make_kgid(current_user_ns(), val);
			if (!gid_valid(sbi->gid))
				goto cleanup;
387 388
			break;
		}
389

390 391 392
		case Opt_umask:
		{
			char *umask = args[0].from;
393 394 395 396
			int rc = kstrtouint(umask, 8, &sbi->umask);

			if (rc)
				goto cleanup;
397
			if (sbi->umask & ~0777) {
398
				pr_err("JFS: Invalid value of umask\n");
399 400 401 402
				goto cleanup;
			}
			break;
		}
403 404 405 406 407 408 409 410 411

		case Opt_discard:
		{
			struct request_queue *q = bdev_get_queue(sb->s_bdev);
			/* if set to 1, even copying files will cause
			 * trimming :O
			 * -> user has more control over the online trimming
			 */
			sbi->minblks_trim = 64;
412
			if (blk_queue_discard(q))
413
				*flag |= JFS_DISCARD;
414 415
			else
				pr_err("JFS: discard option not supported on device\n");
416 417 418 419 420 421 422 423 424 425 426
			break;
		}

		case Opt_nodiscard:
			*flag &= ~JFS_DISCARD;
			break;

		case Opt_discard_minblk:
		{
			struct request_queue *q = bdev_get_queue(sb->s_bdev);
			char *minblks_trim = args[0].from;
427
			int rc;
428 429
			if (blk_queue_discard(q)) {
				*flag |= JFS_DISCARD;
430 431 432 433 434
				rc = kstrtouint(minblks_trim, 0,
						&sbi->minblks_trim);
				if (rc)
					goto cleanup;
			} else
435
				pr_err("JFS: discard option not supported on device\n");
436 437 438
			break;
		}

L
Linus Torvalds 已提交
439
		default:
440 441
			printk("jfs: Unrecognized mount option \"%s\" or missing value\n",
			       p);
L
Linus Torvalds 已提交
442 443 444 445 446 447
			goto cleanup;
		}
	}

	if (nls_map != (void *) -1) {
		/* Discard old (if remount) */
448
		unload_nls(sbi->nls_tab);
L
Linus Torvalds 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
		sbi->nls_tab = nls_map;
	}
	return 1;

cleanup:
	if (nls_map && nls_map != (void *) -1)
		unload_nls(nls_map);
	return 0;
}

static int jfs_remount(struct super_block *sb, int *flags, char *data)
{
	s64 newLVSize = 0;
	int rc = 0;
	int flag = JFS_SBI(sb)->flag;
464
	int ret;
L
Linus Torvalds 已提交
465

466
	sync_filesystem(sb);
467
	if (!parse_options(data, sb, &newLVSize, &flag))
L
Linus Torvalds 已提交
468
		return -EINVAL;
J
Jan Blunck 已提交
469

L
Linus Torvalds 已提交
470 471
	if (newLVSize) {
		if (sb->s_flags & MS_RDONLY) {
472
			pr_err("JFS: resize requires volume to be mounted read-write\n");
L
Linus Torvalds 已提交
473 474 475
			return -EROFS;
		}
		rc = jfs_extendfs(sb, newLVSize, 0);
J
Jan Blunck 已提交
476
		if (rc)
L
Linus Torvalds 已提交
477 478 479 480
			return rc;
	}

	if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
481 482 483 484 485 486
		/*
		 * Invalidate any previously read metadata.  fsck may have
		 * changed the on-disk data since we mounted r/o
		 */
		truncate_inode_pages(JFS_SBI(sb)->direct_inode->i_mapping, 0);

L
Linus Torvalds 已提交
487
		JFS_SBI(sb)->flag = flag;
488
		ret = jfs_mount_rw(sb, 1);
489 490 491 492

		/* mark the fs r/w for quota activity */
		sb->s_flags &= ~MS_RDONLY;

493
		dquot_resume(sb, -1);
494
		return ret;
L
Linus Torvalds 已提交
495 496
	}
	if ((!(sb->s_flags & MS_RDONLY)) && (*flags & MS_RDONLY)) {
497
		rc = dquot_suspend(sb, -1);
498
		if (rc < 0)
499
			return rc;
L
Linus Torvalds 已提交
500 501 502 503 504 505 506
		rc = jfs_umount_rw(sb);
		JFS_SBI(sb)->flag = flag;
		return rc;
	}
	if ((JFS_SBI(sb)->flag & JFS_NOINTEGRITY) != (flag & JFS_NOINTEGRITY))
		if (!(sb->s_flags & MS_RDONLY)) {
			rc = jfs_umount_rw(sb);
J
Jan Blunck 已提交
507
			if (rc)
L
Linus Torvalds 已提交
508
				return rc;
J
Jan Blunck 已提交
509

L
Linus Torvalds 已提交
510
			JFS_SBI(sb)->flag = flag;
511 512
			ret = jfs_mount_rw(sb, 1);
			return ret;
L
Linus Torvalds 已提交
513 514 515 516 517 518 519 520 521 522 523 524
		}
	JFS_SBI(sb)->flag = flag;

	return 0;
}

static int jfs_fill_super(struct super_block *sb, void *data, int silent)
{
	struct jfs_sb_info *sbi;
	struct inode *inode;
	int rc;
	s64 newLVSize = 0;
525
	int flag, ret = -EINVAL;
L
Linus Torvalds 已提交
526 527 528

	jfs_info("In jfs_read_super: s_flags=0x%lx", sb->s_flags);

529
	sbi = kzalloc(sizeof(struct jfs_sb_info), GFP_KERNEL);
J
Jan Blunck 已提交
530
	if (!sbi)
531
		return -ENOMEM;
J
Jan Blunck 已提交
532

L
Linus Torvalds 已提交
533
	sb->s_fs_info = sbi;
534
	sb->s_max_links = JFS_LINK_MAX;
L
Linus Torvalds 已提交
535
	sbi->sb = sb;
536 537 538
	sbi->uid = INVALID_UID;
	sbi->gid = INVALID_GID;
	sbi->umask = -1;
L
Linus Torvalds 已提交
539 540 541 542

	/* initialize the mount flag and determine the default error handler */
	flag = JFS_ERR_REMOUNT_RO;

J
Jan Blunck 已提交
543 544
	if (!parse_options((char *) data, sb, &newLVSize, &flag))
		goto out_kfree;
L
Linus Torvalds 已提交
545 546 547 548 549 550 551
	sbi->flag = flag;

#ifdef CONFIG_JFS_POSIX_ACL
	sb->s_flags |= MS_POSIXACL;
#endif

	if (newLVSize) {
552
		pr_err("resize option for remount only\n");
J
Jan Blunck 已提交
553
		goto out_kfree;
L
Linus Torvalds 已提交
554 555 556 557 558 559 560 561 562 563 564 565
	}

	/*
	 * Initialize blocksize to 4K.
	 */
	sb_set_blocksize(sb, PSIZE);

	/*
	 * Set method vectors.
	 */
	sb->s_op = &jfs_super_operations;
	sb->s_export_op = &jfs_export_operations;
566
	sb->s_xattr = jfs_xattr_handlers;
567 568
#ifdef CONFIG_QUOTA
	sb->dq_op = &dquot_operations;
569
	sb->s_qcop = &jfs_quotactl_ops;
J
Jan Kara 已提交
570
	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
571
#endif
L
Linus Torvalds 已提交
572

573 574 575 576
	/*
	 * Initialize direct-mapping inode/address-space
	 */
	inode = new_inode(sb);
577 578
	if (inode == NULL) {
		ret = -ENOMEM;
J
Jan Blunck 已提交
579
		goto out_unload;
580
	}
581 582 583
	inode->i_ino = 0;
	inode->i_size = sb->s_bdev->bd_inode->i_size;
	inode->i_mapping->a_ops = &jfs_metapage_aops;
A
Al Viro 已提交
584
	hlist_add_fake(&inode->i_hash);
585 586 587 588
	mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);

	sbi->direct_inode = inode;

L
Linus Torvalds 已提交
589 590
	rc = jfs_mount(sb);
	if (rc) {
591
		if (!silent)
L
Linus Torvalds 已提交
592
			jfs_err("jfs_mount failed w/return code = %d", rc);
593
		goto out_mount_failed;
L
Linus Torvalds 已提交
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
	}
	if (sb->s_flags & MS_RDONLY)
		sbi->log = NULL;
	else {
		rc = jfs_mount_rw(sb, 0);
		if (rc) {
			if (!silent) {
				jfs_err("jfs_mount_rw failed, return code = %d",
					rc);
			}
			goto out_no_rw;
		}
	}

	sb->s_magic = JFS_SUPER_MAGIC;

610 611 612
	if (sbi->mntflag & JFS_OS2)
		sb->s_d_op = &jfs_ci_dentry_operations;

613 614 615
	inode = jfs_iget(sb, ROOT_I);
	if (IS_ERR(inode)) {
		ret = PTR_ERR(inode);
616
		goto out_no_rw;
617
	}
618
	sb->s_root = d_make_root(inode);
L
Linus Torvalds 已提交
619 620 621 622 623 624 625 626 627 628
	if (!sb->s_root)
		goto out_no_root;

	/* logical blocks are represented by 40 bits in pxd_t, etc. */
	sb->s_maxbytes = ((u64) sb->s_blocksize) << 40;
#if BITS_PER_LONG == 32
	/*
	 * Page cache is indexed by long.
	 * I would use MAX_LFS_FILESIZE, but it's only half as big
	 */
629
	sb->s_maxbytes = min(((u64) PAGE_SIZE << 32) - 1,
630
			     (u64)sb->s_maxbytes);
L
Linus Torvalds 已提交
631 632 633 634 635
#endif
	sb->s_time_gran = 1;
	return 0;

out_no_root:
636
	jfs_err("jfs_read_super: get root dentry failed");
L
Linus Torvalds 已提交
637 638 639

out_no_rw:
	rc = jfs_umount(sb);
640
	if (rc)
L
Linus Torvalds 已提交
641
		jfs_err("jfs_umount failed with return code %d", rc);
642
out_mount_failed:
643
	filemap_write_and_wait(sbi->direct_inode->i_mapping);
644 645 646 647
	truncate_inode_pages(sbi->direct_inode->i_mapping, 0);
	make_bad_inode(sbi->direct_inode);
	iput(sbi->direct_inode);
	sbi->direct_inode = NULL;
J
Jan Blunck 已提交
648
out_unload:
649
	unload_nls(sbi->nls_tab);
J
Jan Blunck 已提交
650
out_kfree:
L
Linus Torvalds 已提交
651
	kfree(sbi);
652
	return ret;
L
Linus Torvalds 已提交
653 654
}

655
static int jfs_freeze(struct super_block *sb)
L
Linus Torvalds 已提交
656 657 658
{
	struct jfs_sb_info *sbi = JFS_SBI(sb);
	struct jfs_log *log = sbi->log;
659
	int rc = 0;
L
Linus Torvalds 已提交
660 661 662

	if (!(sb->s_flags & MS_RDONLY)) {
		txQuiesce(sb);
663 664
		rc = lmLogShutdown(log);
		if (rc) {
J
Joe Perches 已提交
665
			jfs_error(sb, "lmLogShutdown failed\n");
666 667 668 669 670 671 672 673

			/* let operations fail rather than hang */
			txResume(sb);

			return rc;
		}
		rc = updateSuper(sb, FM_CLEAN);
		if (rc) {
674
			jfs_err("jfs_freeze: updateSuper failed");
675 676 677 678 679 680
			/*
			 * Don't fail here. Everything succeeded except
			 * marking the superblock clean, so there's really
			 * no harm in leaving it frozen for now.
			 */
		}
L
Linus Torvalds 已提交
681
	}
682
	return 0;
L
Linus Torvalds 已提交
683 684
}

685
static int jfs_unfreeze(struct super_block *sb)
L
Linus Torvalds 已提交
686 687 688 689 690 691
{
	struct jfs_sb_info *sbi = JFS_SBI(sb);
	struct jfs_log *log = sbi->log;
	int rc = 0;

	if (!(sb->s_flags & MS_RDONLY)) {
692 693
		rc = updateSuper(sb, FM_MOUNT);
		if (rc) {
J
Joe Perches 已提交
694
			jfs_error(sb, "updateSuper failed\n");
695 696 697 698
			goto out;
		}
		rc = lmLogInit(log);
		if (rc)
J
Joe Perches 已提交
699
			jfs_error(sb, "lmLogInit failed\n");
700 701
out:
		txResume(sb);
L
Linus Torvalds 已提交
702
	}
703
	return rc;
L
Linus Torvalds 已提交
704 705
}

A
Al Viro 已提交
706 707
static struct dentry *jfs_do_mount(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *data)
L
Linus Torvalds 已提交
708
{
A
Al Viro 已提交
709
	return mount_bdev(fs_type, flags, dev_name, data, jfs_fill_super);
L
Linus Torvalds 已提交
710 711 712 713 714 715 716
}

static int jfs_sync_fs(struct super_block *sb, int wait)
{
	struct jfs_log *log = JFS_SBI(sb)->log;

	/* log == NULL indicates read-only mount */
717
	if (log) {
718 719 720 721 722
		/*
		 * Write quota structures to quota file, sync_blockdev() will
		 * write them to disk later
		 */
		dquot_writeback_dquots(sb, -1);
L
Linus Torvalds 已提交
723
		jfs_flush_journal(log, wait);
724
		jfs_syncpt(log, 0);
725
	}
L
Linus Torvalds 已提交
726 727 728 729

	return 0;
}

730
static int jfs_show_options(struct seq_file *seq, struct dentry *root)
731
{
732
	struct jfs_sb_info *sbi = JFS_SBI(root->d_sb);
733

734 735 736 737
	if (uid_valid(sbi->uid))
		seq_printf(seq, ",uid=%d", from_kuid(&init_user_ns, sbi->uid));
	if (gid_valid(sbi->gid))
		seq_printf(seq, ",gid=%d", from_kgid(&init_user_ns, sbi->gid));
738 739
	if (sbi->umask != -1)
		seq_printf(seq, ",umask=%03o", sbi->umask);
740 741
	if (sbi->flag & JFS_NOINTEGRITY)
		seq_puts(seq, ",nointegrity");
742 743
	if (sbi->flag & JFS_DISCARD)
		seq_printf(seq, ",discard=%u", sbi->minblks_trim);
M
Miklos Szeredi 已提交
744 745 746 747 748 749
	if (sbi->nls_tab)
		seq_printf(seq, ",iocharset=%s", sbi->nls_tab->charset);
	if (sbi->flag & JFS_ERR_CONTINUE)
		seq_printf(seq, ",errors=continue");
	if (sbi->flag & JFS_ERR_PANIC)
		seq_printf(seq, ",errors=panic");
750

751
#ifdef CONFIG_QUOTA
752 753 754 755 756 757 758 759 760 761
	if (sbi->flag & JFS_USRQUOTA)
		seq_puts(seq, ",usrquota");

	if (sbi->flag & JFS_GRPQUOTA)
		seq_puts(seq, ",grpquota");
#endif

	return 0;
}

762 763 764 765
#ifdef CONFIG_QUOTA

/* Read data from quotafile - avoid pagecache and such because we cannot afford
 * acquiring the locks... As quota files are never truncated and quota code
L
Lucas De Marchi 已提交
766
 * itself serializes the operations (and no one else should touch the files)
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
 * we don't have to be afraid of races */
static ssize_t jfs_quota_read(struct super_block *sb, int type, char *data,
			      size_t len, loff_t off)
{
	struct inode *inode = sb_dqopt(sb)->files[type];
	sector_t blk = off >> sb->s_blocksize_bits;
	int err = 0;
	int offset = off & (sb->s_blocksize - 1);
	int tocopy;
	size_t toread;
	struct buffer_head tmp_bh;
	struct buffer_head *bh;
	loff_t i_size = i_size_read(inode);

	if (off > i_size)
		return 0;
	if (off+len > i_size)
		len = i_size-off;
	toread = len;
	while (toread > 0) {
		tocopy = sb->s_blocksize - offset < toread ?
				sb->s_blocksize - offset : toread;

		tmp_bh.b_state = 0;
F
Fabian Frederick 已提交
791
		tmp_bh.b_size = i_blocksize(inode);
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
		err = jfs_get_block(inode, blk, &tmp_bh, 0);
		if (err)
			return err;
		if (!buffer_mapped(&tmp_bh))	/* A hole? */
			memset(data, 0, tocopy);
		else {
			bh = sb_bread(sb, tmp_bh.b_blocknr);
			if (!bh)
				return -EIO;
			memcpy(data, bh->b_data+offset, tocopy);
			brelse(bh);
		}
		offset = 0;
		toread -= tocopy;
		data += tocopy;
		blk++;
	}
	return len;
}

/* Write to quotafile */
static ssize_t jfs_quota_write(struct super_block *sb, int type,
			       const char *data, size_t len, loff_t off)
{
	struct inode *inode = sb_dqopt(sb)->files[type];
	sector_t blk = off >> sb->s_blocksize_bits;
	int err = 0;
	int offset = off & (sb->s_blocksize - 1);
	int tocopy;
	size_t towrite = len;
	struct buffer_head tmp_bh;
	struct buffer_head *bh;

A
Al Viro 已提交
825
	inode_lock(inode);
826 827 828 829 830
	while (towrite > 0) {
		tocopy = sb->s_blocksize - offset < towrite ?
				sb->s_blocksize - offset : towrite;

		tmp_bh.b_state = 0;
F
Fabian Frederick 已提交
831
		tmp_bh.b_size = i_blocksize(inode);
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
		err = jfs_get_block(inode, blk, &tmp_bh, 1);
		if (err)
			goto out;
		if (offset || tocopy != sb->s_blocksize)
			bh = sb_bread(sb, tmp_bh.b_blocknr);
		else
			bh = sb_getblk(sb, tmp_bh.b_blocknr);
		if (!bh) {
			err = -EIO;
			goto out;
		}
		lock_buffer(bh);
		memcpy(bh->b_data+offset, data, tocopy);
		flush_dcache_page(bh->b_page);
		set_buffer_uptodate(bh);
		mark_buffer_dirty(bh);
		unlock_buffer(bh);
		brelse(bh);
		offset = 0;
		towrite -= tocopy;
		data += tocopy;
		blk++;
	}
out:
856
	if (len == towrite) {
A
Al Viro 已提交
857
		inode_unlock(inode);
858
		return err;
859
	}
860 861 862
	if (inode->i_size < off+len-towrite)
		i_size_write(inode, off+len-towrite);
	inode->i_version++;
863
	inode->i_mtime = inode->i_ctime = current_time(inode);
864
	mark_inode_dirty(inode);
A
Al Viro 已提交
865
	inode_unlock(inode);
866 867 868
	return len - towrite;
}

J
Jan Kara 已提交
869 870 871 872
static struct dquot **jfs_get_dquots(struct inode *inode)
{
	return JFS_IP(inode)->i_dquot;
}
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917

static int jfs_quota_on(struct super_block *sb, int type, int format_id,
			const struct path *path)
{
	int err;
	struct inode *inode;

	err = dquot_quota_on(sb, type, format_id, path);
	if (err)
		return err;

	inode = d_inode(path->dentry);
	inode_lock(inode);
	JFS_IP(inode)->mode2 |= JFS_NOATIME_FL | JFS_IMMUTABLE_FL;
	inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
			S_NOATIME | S_IMMUTABLE);
	inode_unlock(inode);
	mark_inode_dirty(inode);

	return 0;
}

static int jfs_quota_off(struct super_block *sb, int type)
{
	struct inode *inode = sb_dqopt(sb)->files[type];
	int err;

	if (!inode || !igrab(inode))
		goto out;

	err = dquot_quota_off(sb, type);
	if (err)
		goto out_put;

	inode_lock(inode);
	JFS_IP(inode)->mode2 &= ~(JFS_NOATIME_FL | JFS_IMMUTABLE_FL);
	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
	inode_unlock(inode);
	mark_inode_dirty(inode);
out_put:
	iput(inode);
	return err;
out:
	return dquot_quota_off(sb, type);
}
918 919
#endif

920
static const struct super_operations jfs_super_operations = {
L
Linus Torvalds 已提交
921 922 923 924
	.alloc_inode	= jfs_alloc_inode,
	.destroy_inode	= jfs_destroy_inode,
	.dirty_inode	= jfs_dirty_inode,
	.write_inode	= jfs_write_inode,
A
Al Viro 已提交
925
	.evict_inode	= jfs_evict_inode,
L
Linus Torvalds 已提交
926 927
	.put_super	= jfs_put_super,
	.sync_fs	= jfs_sync_fs,
928 929
	.freeze_fs	= jfs_freeze,
	.unfreeze_fs	= jfs_unfreeze,
L
Linus Torvalds 已提交
930 931
	.statfs		= jfs_statfs,
	.remount_fs	= jfs_remount,
932 933 934 935
	.show_options	= jfs_show_options,
#ifdef CONFIG_QUOTA
	.quota_read	= jfs_quota_read,
	.quota_write	= jfs_quota_write,
J
Jan Kara 已提交
936
	.get_dquots	= jfs_get_dquots,
937
#endif
L
Linus Torvalds 已提交
938 939
};

940
static const struct export_operations jfs_export_operations = {
C
Christoph Hellwig 已提交
941 942
	.fh_to_dentry	= jfs_fh_to_dentry,
	.fh_to_parent	= jfs_fh_to_parent,
L
Linus Torvalds 已提交
943 944 945 946 947 948
	.get_parent	= jfs_get_parent,
};

static struct file_system_type jfs_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "jfs",
A
Al Viro 已提交
949
	.mount		= jfs_do_mount,
L
Linus Torvalds 已提交
950 951 952
	.kill_sb	= kill_block_super,
	.fs_flags	= FS_REQUIRES_DEV,
};
953
MODULE_ALIAS_FS("jfs");
L
Linus Torvalds 已提交
954

955
static void init_once(void *foo)
L
Linus Torvalds 已提交
956 957 958
{
	struct jfs_inode_info *jfs_ip = (struct jfs_inode_info *) foo;

C
Christoph Lameter 已提交
959 960 961 962 963 964 965 966
	memset(jfs_ip, 0, sizeof(struct jfs_inode_info));
	INIT_LIST_HEAD(&jfs_ip->anon_inode_list);
	init_rwsem(&jfs_ip->rdwrlock);
	mutex_init(&jfs_ip->commit_mutex);
	init_rwsem(&jfs_ip->xattr_sem);
	spin_lock_init(&jfs_ip->ag_lock);
	jfs_ip->active_ag = -1;
	inode_init_once(&jfs_ip->vfs_inode);
L
Linus Torvalds 已提交
967 968 969 970 971 972 973 974
}

static int __init init_jfs_fs(void)
{
	int i;
	int rc;

	jfs_inode_cachep =
D
Dave Kleikamp 已提交
975
	    kmem_cache_create("jfs_ip", sizeof(struct jfs_inode_info), 0,
976
			    SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|SLAB_ACCOUNT,
977
			    init_once);
L
Linus Torvalds 已提交
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
	if (jfs_inode_cachep == NULL)
		return -ENOMEM;

	/*
	 * Metapage initialization
	 */
	rc = metapage_init();
	if (rc) {
		jfs_err("metapage_init failed w/rc = %d", rc);
		goto free_slab;
	}

	/*
	 * Transaction Manager initialization
	 */
	rc = txInit();
	if (rc) {
		jfs_err("txInit failed w/rc = %d", rc);
		goto free_metapage;
	}

	/*
	 * I/O completion thread (endio)
	 */
C
Christoph Hellwig 已提交
1002 1003 1004 1005
	jfsIOthread = kthread_run(jfsIOWait, NULL, "jfsIO");
	if (IS_ERR(jfsIOthread)) {
		rc = PTR_ERR(jfsIOthread);
		jfs_err("init_jfs_fs: fork failed w/rc = %d", rc);
L
Linus Torvalds 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014
		goto end_txmngr;
	}

	if (commit_threads < 1)
		commit_threads = num_online_cpus();
	if (commit_threads > MAX_COMMIT_THREADS)
		commit_threads = MAX_COMMIT_THREADS;

	for (i = 0; i < commit_threads; i++) {
1015 1016
		jfsCommitThread[i] = kthread_run(jfs_lazycommit, NULL,
						 "jfsCommit");
C
Christoph Hellwig 已提交
1017 1018 1019
		if (IS_ERR(jfsCommitThread[i])) {
			rc = PTR_ERR(jfsCommitThread[i]);
			jfs_err("init_jfs_fs: fork failed w/rc = %d", rc);
L
Linus Torvalds 已提交
1020 1021 1022 1023 1024
			commit_threads = i;
			goto kill_committask;
		}
	}

C
Christoph Hellwig 已提交
1025 1026 1027 1028
	jfsSyncThread = kthread_run(jfs_sync, NULL, "jfsSync");
	if (IS_ERR(jfsSyncThread)) {
		rc = PTR_ERR(jfsSyncThread);
		jfs_err("init_jfs_fs: fork failed w/rc = %d", rc);
L
Linus Torvalds 已提交
1029 1030 1031 1032 1033 1034 1035
		goto kill_committask;
	}

#ifdef PROC_FS_JFS
	jfs_proc_init();
#endif

1036 1037 1038
	rc = register_filesystem(&jfs_fs_type);
	if (!rc)
		return 0;
L
Linus Torvalds 已提交
1039

1040 1041 1042 1043
#ifdef PROC_FS_JFS
	jfs_proc_clean();
#endif
	kthread_stop(jfsSyncThread);
L
Linus Torvalds 已提交
1044 1045
kill_committask:
	for (i = 0; i < commit_threads; i++)
C
Christoph Hellwig 已提交
1046 1047
		kthread_stop(jfsCommitThread[i]);
	kthread_stop(jfsIOthread);
L
Linus Torvalds 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
end_txmngr:
	txExit();
free_metapage:
	metapage_exit();
free_slab:
	kmem_cache_destroy(jfs_inode_cachep);
	return rc;
}

static void __exit exit_jfs_fs(void)
{
	int i;

	jfs_info("exit_jfs_fs called");

	txExit();
	metapage_exit();
C
Christoph Hellwig 已提交
1065 1066

	kthread_stop(jfsIOthread);
L
Linus Torvalds 已提交
1067
	for (i = 0; i < commit_threads; i++)
C
Christoph Hellwig 已提交
1068 1069
		kthread_stop(jfsCommitThread[i]);
	kthread_stop(jfsSyncThread);
L
Linus Torvalds 已提交
1070 1071 1072 1073
#ifdef PROC_FS_JFS
	jfs_proc_clean();
#endif
	unregister_filesystem(&jfs_fs_type);
1074 1075 1076 1077 1078 1079

	/*
	 * Make sure all delayed rcu free inodes are flushed before we
	 * destroy cache.
	 */
	rcu_barrier();
L
Linus Torvalds 已提交
1080 1081 1082 1083 1084
	kmem_cache_destroy(jfs_inode_cachep);
}

module_init(init_jfs_fs)
module_exit(exit_jfs_fs)