super.c 70.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
 *
 * Trivial changes by Alan Cox to add the LFS fixes
 *
 * Trivial Changes:
 * Rights granted to Hans Reiser to redistribute under other terms providing
 * he accepts all liability including but not limited to patent, fitness
 * for purpose, and direct or indirect claims arising from failure to perform.
 *
 * NO WARRANTY
 */

#include <linux/module.h>
15
#include <linux/slab.h>
L
Linus Torvalds 已提交
16 17
#include <linux/vmalloc.h>
#include <linux/time.h>
18
#include <linux/uaccess.h>
19
#include "reiserfs.h"
20
#include "acl.h"
21
#include "xattr.h"
L
Linus Torvalds 已提交
22 23
#include <linux/init.h>
#include <linux/blkdev.h>
24
#include <linux/backing-dev.h>
L
Linus Torvalds 已提交
25
#include <linux/buffer_head.h>
26
#include <linux/exportfs.h>
27
#include <linux/quotaops.h>
L
Linus Torvalds 已提交
28 29 30
#include <linux/vfs.h>
#include <linux/mount.h>
#include <linux/namei.h>
31
#include <linux/crc32.h>
32
#include <linux/seq_file.h>
L
Linus Torvalds 已提交
33 34 35 36 37 38 39

struct file_system_type reiserfs_fs_type;

static const char reiserfs_3_5_magic_string[] = REISERFS_SUPER_MAGIC_STRING;
static const char reiserfs_3_6_magic_string[] = REISER2FS_SUPER_MAGIC_STRING;
static const char reiserfs_jr_magic_string[] = REISER2FS_JR_SUPER_MAGIC_STRING;

40
int is_reiserfs_3_5(struct reiserfs_super_block *rs)
L
Linus Torvalds 已提交
41
{
42 43
	return !strncmp(rs->s_v1.s_magic, reiserfs_3_5_magic_string,
			strlen(reiserfs_3_5_magic_string));
L
Linus Torvalds 已提交
44 45
}

46
int is_reiserfs_3_6(struct reiserfs_super_block *rs)
L
Linus Torvalds 已提交
47
{
48 49
	return !strncmp(rs->s_v1.s_magic, reiserfs_3_6_magic_string,
			strlen(reiserfs_3_6_magic_string));
L
Linus Torvalds 已提交
50 51
}

52
int is_reiserfs_jr(struct reiserfs_super_block *rs)
L
Linus Torvalds 已提交
53
{
54 55
	return !strncmp(rs->s_v1.s_magic, reiserfs_jr_magic_string,
			strlen(reiserfs_jr_magic_string));
L
Linus Torvalds 已提交
56 57
}

58
static int is_any_reiserfs_magic_string(struct reiserfs_super_block *rs)
L
Linus Torvalds 已提交
59
{
60 61
	return (is_reiserfs_3_5(rs) || is_reiserfs_3_6(rs) ||
		is_reiserfs_jr(rs));
L
Linus Torvalds 已提交
62 63
}

64
static int reiserfs_remount(struct super_block *s, int *flags, char *data);
65
static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf);
L
Linus Torvalds 已提交
66

67
static int reiserfs_sync_fs(struct super_block *s, int wait)
L
Linus Torvalds 已提交
68
{
69 70
	struct reiserfs_transaction_handle th;

71 72 73 74 75
	/*
	 * Writeback quota in non-journalled quota case - journalled quota has
	 * no dirty dquots
	 */
	dquot_writeback_dquots(s, -1);
76 77
	reiserfs_write_lock(s);
	if (!journal_begin(&th, s, 1))
78
		if (!journal_end_sync(&th))
79 80
			reiserfs_flush_old_commits(s);
	reiserfs_write_unlock(s);
81
	return 0;
L
Linus Torvalds 已提交
82 83
}

84
static void flush_old_commits(struct work_struct *work)
L
Linus Torvalds 已提交
85
{
86 87 88 89 90 91
	struct reiserfs_sb_info *sbi;
	struct super_block *s;

	sbi = container_of(work, struct reiserfs_sb_info, old_work.work);
	s = sbi->s_journal->j_work_sb;

92 93 94 95 96 97 98 99 100 101 102 103 104
	/*
	 * We need s_umount for protecting quota writeback. We have to use
	 * trylock as reiserfs_cancel_old_flush() may be waiting for this work
	 * to complete with s_umount held.
	 */
	if (!down_read_trylock(&s->s_umount)) {
		/* Requeue work if we are not cancelling it */
		spin_lock(&sbi->old_work_lock);
		if (sbi->work_queued == 1)
			queue_delayed_work(system_long_wq, &sbi->old_work, HZ);
		spin_unlock(&sbi->old_work_lock);
		return;
	}
105
	spin_lock(&sbi->old_work_lock);
106 107 108
	/* Avoid clobbering the cancel state... */
	if (sbi->work_queued == 1)
		sbi->work_queued = 0;
109 110
	spin_unlock(&sbi->old_work_lock);

111
	reiserfs_sync_fs(s, 1);
112
	up_read(&s->s_umount);
L
Linus Torvalds 已提交
113 114
}

115 116 117 118 119
void reiserfs_schedule_old_flush(struct super_block *s)
{
	struct reiserfs_sb_info *sbi = REISERFS_SB(s);
	unsigned long delay;

120 121 122 123
	/*
	 * Avoid scheduling flush when sb is being shut down. It can race
	 * with journal shutdown and free still queued delayed work.
	 */
124
	if (sb_rdonly(s) || !(s->s_flags & MS_ACTIVE))
125 126 127 128 129 130 131 132 133 134 135
		return;

	spin_lock(&sbi->old_work_lock);
	if (!sbi->work_queued) {
		delay = msecs_to_jiffies(dirty_writeback_interval * 10);
		queue_delayed_work(system_long_wq, &sbi->old_work, delay);
		sbi->work_queued = 1;
	}
	spin_unlock(&sbi->old_work_lock);
}

136
void reiserfs_cancel_old_flush(struct super_block *s)
137 138 139 140
{
	struct reiserfs_sb_info *sbi = REISERFS_SB(s);

	spin_lock(&sbi->old_work_lock);
141 142
	/* Make sure no new flushes will be queued */
	sbi->work_queued = 2;
143
	spin_unlock(&sbi->old_work_lock);
144
	cancel_delayed_work_sync(&REISERFS_SB(s)->old_work);
145 146
}

147
static int reiserfs_freeze(struct super_block *s)
L
Linus Torvalds 已提交
148
{
149
	struct reiserfs_transaction_handle th;
150

151
	reiserfs_cancel_old_flush(s);
152

153
	reiserfs_write_lock(s);
154
	if (!sb_rdonly(s)) {
155 156 157 158 159 160
		int err = journal_begin(&th, s, 1);
		if (err) {
			reiserfs_block_writes(&th);
		} else {
			reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
						     1);
161
			journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
162
			reiserfs_block_writes(&th);
163
			journal_end_sync(&th);
164 165 166
		}
	}
	reiserfs_write_unlock(s);
167
	return 0;
L
Linus Torvalds 已提交
168 169
}

170
static int reiserfs_unfreeze(struct super_block *s)
171
{
172 173
	struct reiserfs_sb_info *sbi = REISERFS_SB(s);

174
	reiserfs_allow_writes(s);
175 176 177 178
	spin_lock(&sbi->old_work_lock);
	/* Allow old_work to run again */
	sbi->work_queued = 0;
	spin_unlock(&sbi->old_work_lock);
179
	return 0;
L
Linus Torvalds 已提交
180 181
}

182
extern const struct in_core_key MAX_IN_CORE_KEY;
L
Linus Torvalds 已提交
183

184 185 186 187 188 189 190 191 192
/*
 * this is used to delete "save link" when there are no items of a
 * file it points to. It can either happen if unlink is completed but
 * "save unlink" removal, or if file has both unlink and truncate
 * pending and as unlink completes first (because key of "save link"
 * protecting unlink is bigger that a key lf "save link" which
 * protects truncate), so there left no items to make truncate
 * completion on
 */
193 194
static int remove_save_link_only(struct super_block *s,
				 struct reiserfs_key *key, int oid_free)
L
Linus Torvalds 已提交
195
{
196 197 198 199 200 201 202 203 204 205 206 207 208
	struct reiserfs_transaction_handle th;
	int err;

	/* we are going to do one balancing */
	err = journal_begin(&th, s, JOURNAL_PER_BALANCE_CNT);
	if (err)
		return err;

	reiserfs_delete_solid_item(&th, NULL, key);
	if (oid_free)
		/* removals are protected by direct items */
		reiserfs_release_objectid(&th, le32_to_cpu(key->k_objectid));

209
	return journal_end(&th);
L
Linus Torvalds 已提交
210
}
211

L
Linus Torvalds 已提交
212 213 214
#ifdef CONFIG_QUOTA
static int reiserfs_quota_on_mount(struct super_block *, int);
#endif
215

216 217 218 219 220 221 222 223 224
/*
 * Look for uncompleted unlinks and truncates and complete them
 *
 * Called with superblock write locked.  If quotas are enabled, we have to
 * release/retake lest we call dquot_quota_on_mount(), proceed to
 * schedule_on_each_cpu() in invalidate_bdev() and deadlock waiting for the per
 * cpu worklets to complete flush_async_commits() that in turn wait for the
 * superblock write lock.
 */
225
static int finish_unfinished(struct super_block *s)
L
Linus Torvalds 已提交
226
{
227 228
	INITIALIZE_PATH(path);
	struct cpu_key max_cpu_key, obj_key;
229
	struct reiserfs_key save_link_key, last_inode_key;
230 231 232 233 234 235 236 237
	int retval = 0;
	struct item_head *ih;
	struct buffer_head *bh;
	int item_pos;
	char *item;
	int done;
	struct inode *inode;
	int truncate;
L
Linus Torvalds 已提交
238
#ifdef CONFIG_QUOTA
239 240
	int i;
	int ms_active_set;
J
Jan Kara 已提交
241
	int quota_enabled[REISERFS_MAXQUOTAS];
L
Linus Torvalds 已提交
242
#endif
243 244 245 246 247 248 249

	/* compose key to look for "save" links */
	max_cpu_key.version = KEY_FORMAT_3_5;
	max_cpu_key.on_disk_key.k_dir_id = ~0U;
	max_cpu_key.on_disk_key.k_objectid = ~0U;
	set_cpu_key_k_offset(&max_cpu_key, ~0U);
	max_cpu_key.key_length = 3;
L
Linus Torvalds 已提交
250

251 252
	memset(&last_inode_key, 0, sizeof(last_inode_key));

L
Linus Torvalds 已提交
253
#ifdef CONFIG_QUOTA
254 255 256 257 258 259 260 261
	/* Needed for iput() to work correctly and not trash data */
	if (s->s_flags & MS_ACTIVE) {
		ms_active_set = 0;
	} else {
		ms_active_set = 1;
		s->s_flags |= MS_ACTIVE;
	}
	/* Turn on quotas so that they are updated correctly */
J
Jan Kara 已提交
262
	for (i = 0; i < REISERFS_MAXQUOTAS; i++) {
263
		quota_enabled[i] = 1;
264
		if (REISERFS_SB(s)->s_qf_names[i]) {
265 266 267 268 269 270
			int ret;

			if (sb_has_quota_active(s, i)) {
				quota_enabled[i] = 0;
				continue;
			}
271
			reiserfs_write_unlock(s);
272
			ret = reiserfs_quota_on_mount(s, i);
273
			reiserfs_write_lock(s);
274
			if (ret < 0)
J
Jeff Mahoney 已提交
275 276 277
				reiserfs_warning(s, "reiserfs-2500",
						 "cannot turn on journaled "
						 "quota: error %d", ret);
278 279
		}
	}
L
Linus Torvalds 已提交
280
#endif
281 282 283 284

	done = 0;
	REISERFS_SB(s)->s_is_unlinked_ok = 1;
	while (!retval) {
285
		int depth;
286 287
		retval = search_item(s, &max_cpu_key, &path);
		if (retval != ITEM_NOT_FOUND) {
J
Jeff Mahoney 已提交
288 289
			reiserfs_error(s, "vs-2140",
				       "search_by_key returned %d", retval);
290 291 292 293 294 295
			break;
		}

		bh = get_last_bh(&path);
		item_pos = get_item_pos(&path);
		if (item_pos != B_NR_ITEMS(bh)) {
J
Jeff Mahoney 已提交
296 297
			reiserfs_warning(s, "vs-2060",
					 "wrong position found");
298 299 300
			break;
		}
		item_pos--;
301
		ih = item_head(bh, item_pos);
302 303 304 305 306 307 308 309 310 311 312 313

		if (le32_to_cpu(ih->ih_key.k_dir_id) != MAX_KEY_OBJECTID)
			/* there are no "save" links anymore */
			break;

		save_link_key = ih->ih_key;
		if (is_indirect_le_ih(ih))
			truncate = 1;
		else
			truncate = 0;

		/* reiserfs_iget needs k_dirid and k_objectid only */
314
		item = ih_item_body(bh, ih);
315 316 317 318 319 320 321 322 323
		obj_key.on_disk_key.k_dir_id = le32_to_cpu(*(__le32 *) item);
		obj_key.on_disk_key.k_objectid =
		    le32_to_cpu(ih->ih_key.k_objectid);
		obj_key.on_disk_key.k_offset = 0;
		obj_key.on_disk_key.k_type = 0;

		pathrelse(&path);

		inode = reiserfs_iget(s, &obj_key);
324
		if (IS_ERR_OR_NULL(inode)) {
325 326 327 328
			/*
			 * the unlink almost completed, it just did not
			 * manage to remove "save" link and release objectid
			 */
J
Jeff Mahoney 已提交
329
			reiserfs_warning(s, "vs-2180", "iget failed for %K",
330 331 332 333 334 335 336
					 &obj_key);
			retval = remove_save_link_only(s, &save_link_key, 1);
			continue;
		}

		if (!truncate && inode->i_nlink) {
			/* file is not unlinked */
J
Jeff Mahoney 已提交
337 338
			reiserfs_warning(s, "vs-2185",
					 "file %K is not unlinked",
339 340 341 342
					 &obj_key);
			retval = remove_save_link_only(s, &save_link_key, 0);
			continue;
		}
343
		depth = reiserfs_write_unlock_nested(inode->i_sb);
344
		dquot_initialize(inode);
345
		reiserfs_write_lock_nested(inode->i_sb, depth);
346 347

		if (truncate && S_ISDIR(inode->i_mode)) {
348 349 350 351 352 353 354
			/*
			 * We got a truncate request for a dir which
			 * is impossible.  The only imaginable way is to
			 * execute unfinished truncate request then boot
			 * into old kernel, remove the file and create dir
			 * with the same key.
			 */
J
Jeff Mahoney 已提交
355 356 357
			reiserfs_warning(s, "green-2101",
					 "impossible truncate on a "
					 "directory %k. Please report",
358 359 360 361 362 363 364 365 366 367
					 INODE_PKEY(inode));
			retval = remove_save_link_only(s, &save_link_key, 0);
			truncate = 0;
			iput(inode);
			continue;
		}

		if (truncate) {
			REISERFS_I(inode)->i_flags |=
			    i_link_saved_truncate_mask;
368 369 370 371
			/*
			 * not completed truncate found. New size was
			 * committed together with "save" link
			 */
372
			reiserfs_info(s, "Truncating %k to %lld ..",
373
				      INODE_PKEY(inode), inode->i_size);
374 375 376 377

			/* don't update modification time */
			reiserfs_truncate_file(inode, 0);

378 379 380 381 382
			retval = remove_save_link(inode, truncate);
		} else {
			REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
			/* not completed unlink (rmdir) found */
			reiserfs_info(s, "Removing %k..", INODE_PKEY(inode));
383 384 385 386 387 388
			if (memcmp(&last_inode_key, INODE_PKEY(inode),
					sizeof(last_inode_key))){
				last_inode_key = *INODE_PKEY(inode);
				/* removal gets completed in iput */
				retval = 0;
			} else {
J
Jeff Mahoney 已提交
389 390 391 392
				reiserfs_warning(s, "super-2189", "Dead loop "
						 "in finish_unfinished "
						 "detected, just remove "
						 "save link\n");
393 394 395
				retval = remove_save_link_only(s,
							&save_link_key, 0);
			}
396 397 398 399 400 401 402 403
		}

		iput(inode);
		printk("done\n");
		done++;
	}
	REISERFS_SB(s)->s_is_unlinked_ok = 0;

L
Linus Torvalds 已提交
404
#ifdef CONFIG_QUOTA
405
	/* Turn quotas off */
406
	reiserfs_write_unlock(s);
J
Jan Kara 已提交
407
	for (i = 0; i < REISERFS_MAXQUOTAS; i++) {
408
		if (sb_dqopt(s)->files[i] && quota_enabled[i])
409
			dquot_quota_off(s, i);
410
	}
411
	reiserfs_write_lock(s);
412 413 414
	if (ms_active_set)
		/* Restore the flag back */
		s->s_flags &= ~MS_ACTIVE;
L
Linus Torvalds 已提交
415
#endif
416 417 418 419 420
	pathrelse(&path);
	if (done)
		reiserfs_info(s, "There were %d uncompleted unlinks/truncates. "
			      "Completed\n", done);
	return retval;
L
Linus Torvalds 已提交
421
}
422

423 424 425 426 427 428
/*
 * to protect file being unlinked from getting lost we "safe" link files
 * being unlinked. This link will be deleted in the same transaction with last
 * item of file. mounting the filesystem we scan all these links and remove
 * files which almost got lost
 */
429 430
void add_save_link(struct reiserfs_transaction_handle *th,
		   struct inode *inode, int truncate)
L
Linus Torvalds 已提交
431
{
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	INITIALIZE_PATH(path);
	int retval;
	struct cpu_key key;
	struct item_head ih;
	__le32 link;

	BUG_ON(!th->t_trans_id);

	/* file can only get one "save link" of each kind */
	RFALSE(truncate &&
	       (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask),
	       "saved link already exists for truncated inode %lx",
	       (long)inode->i_ino);
	RFALSE(!truncate &&
	       (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask),
	       "saved link already exists for unlinked inode %lx",
	       (long)inode->i_ino);

	/* setup key of "save" link */
	key.version = KEY_FORMAT_3_5;
	key.on_disk_key.k_dir_id = MAX_KEY_OBJECTID;
	key.on_disk_key.k_objectid = inode->i_ino;
	if (!truncate) {
		/* unlink, rmdir, rename */
		set_cpu_key_k_offset(&key, 1 + inode->i_sb->s_blocksize);
		set_cpu_key_k_type(&key, TYPE_DIRECT);

		/* item head of "safe" link */
		make_le_item_head(&ih, &key, key.version,
				  1 + inode->i_sb->s_blocksize, TYPE_DIRECT,
				  4 /*length */ , 0xffff /*free space */ );
	} else {
		/* truncate */
		if (S_ISDIR(inode->i_mode))
J
Jeff Mahoney 已提交
466 467 468
			reiserfs_warning(inode->i_sb, "green-2102",
					 "Adding a truncate savelink for "
					 "a directory %k! Please report",
469 470 471 472 473 474 475 476 477 478 479 480 481 482
					 INODE_PKEY(inode));
		set_cpu_key_k_offset(&key, 1);
		set_cpu_key_k_type(&key, TYPE_INDIRECT);

		/* item head of "safe" link */
		make_le_item_head(&ih, &key, key.version, 1, TYPE_INDIRECT,
				  4 /*length */ , 0 /*free space */ );
	}
	key.key_length = 3;

	/* look for its place in the tree */
	retval = search_item(inode->i_sb, &key, &path);
	if (retval != ITEM_NOT_FOUND) {
		if (retval != -ENOSPC)
J
Jeff Mahoney 已提交
483 484 485
			reiserfs_error(inode->i_sb, "vs-2100",
				       "search_by_key (%K) returned %d", &key,
				       retval);
486 487 488
		pathrelse(&path);
		return;
	}
L
Linus Torvalds 已提交
489

490 491 492
	/* body of "save" link */
	link = INODE_PKEY(inode)->k_dir_id;

L
Lucas De Marchi 已提交
493
	/* put "save" link into tree, don't charge quota to anyone */
494 495 496 497
	retval =
	    reiserfs_insert_item(th, &path, &key, &ih, NULL, (char *)&link);
	if (retval) {
		if (retval != -ENOSPC)
J
Jeff Mahoney 已提交
498 499
			reiserfs_error(inode->i_sb, "vs-2120",
				       "insert_item returned %d", retval);
500 501 502 503 504 505 506 507
	} else {
		if (truncate)
			REISERFS_I(inode)->i_flags |=
			    i_link_saved_truncate_mask;
		else
			REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
	}
}
L
Linus Torvalds 已提交
508 509

/* this opens transaction unlike add_save_link */
510
int remove_save_link(struct inode *inode, int truncate)
L
Linus Torvalds 已提交
511
{
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
	struct reiserfs_transaction_handle th;
	struct reiserfs_key key;
	int err;

	/* we are going to do one balancing only */
	err = journal_begin(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
	if (err)
		return err;

	/* setup key of "save" link */
	key.k_dir_id = cpu_to_le32(MAX_KEY_OBJECTID);
	key.k_objectid = INODE_PKEY(inode)->k_objectid;
	if (!truncate) {
		/* unlink, rmdir, rename */
		set_le_key_k_offset(KEY_FORMAT_3_5, &key,
				    1 + inode->i_sb->s_blocksize);
		set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_DIRECT);
	} else {
		/* truncate */
		set_le_key_k_offset(KEY_FORMAT_3_5, &key, 1);
		set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_INDIRECT);
	}
L
Linus Torvalds 已提交
534

535 536 537 538 539 540 541 542 543 544 545 546
	if ((truncate &&
	     (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask)) ||
	    (!truncate &&
	     (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask)))
		/* don't take quota bytes from anywhere */
		reiserfs_delete_solid_item(&th, NULL, &key);
	if (!truncate) {
		reiserfs_release_objectid(&th, inode->i_ino);
		REISERFS_I(inode)->i_flags &= ~i_link_saved_unlink_mask;
	} else
		REISERFS_I(inode)->i_flags &= ~i_link_saved_truncate_mask;

547
	return journal_end(&th);
548
}
L
Linus Torvalds 已提交
549

550
static void reiserfs_kill_sb(struct super_block *s)
L
Linus Torvalds 已提交
551
{
552
	if (REISERFS_SB(s)) {
A
Al Viro 已提交
553
		reiserfs_proc_info_done(s);
554 555 556 557 558 559 560 561 562 563 564 565 566 567
		/*
		 * Force any pending inode evictions to occur now. Any
		 * inodes to be removed that have extended attributes
		 * associated with them need to clean them up before
		 * we can release the extended attribute root dentries.
		 * shrink_dcache_for_umount will BUG if we don't release
		 * those before it's called so ->put_super is too late.
		 */
		shrink_dcache_sb(s);

		dput(REISERFS_SB(s)->xattr_root);
		REISERFS_SB(s)->xattr_root = NULL;
		dput(REISERFS_SB(s)->priv_root);
		REISERFS_SB(s)->priv_root = NULL;
568 569
	}

570 571 572
	kill_block_super(s);
}

573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
#ifdef CONFIG_QUOTA
static int reiserfs_quota_off(struct super_block *sb, int type);

static void reiserfs_quota_off_umount(struct super_block *s)
{
	int type;

	for (type = 0; type < REISERFS_MAXQUOTAS; type++)
		reiserfs_quota_off(s, type);
}
#else
static inline void reiserfs_quota_off_umount(struct super_block *s)
{
}
#endif

589 590 591 592
static void reiserfs_put_super(struct super_block *s)
{
	struct reiserfs_transaction_handle th;
	th.t_trans_id = 0;
593

594
	reiserfs_quota_off_umount(s);
595

F
Frederic Weisbecker 已提交
596
	reiserfs_write_lock(s);
597

598 599 600 601
	/*
	 * change file system state to current state if it was mounted
	 * with read-write permissions
	 */
602
	if (!sb_rdonly(s)) {
603 604 605 606 607
		if (!journal_begin(&th, s, 10)) {
			reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
						     1);
			set_sb_umount_state(SB_DISK_SUPER_BLOCK(s),
					    REISERFS_SB(s)->s_mount_state);
608
			journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
609 610 611
		}
	}

612 613 614
	/*
	 * note, journal_release checks for readonly mount, and can
	 * decide not to do a journal_end
615 616 617
	 */
	journal_release(&th, s);

618
	reiserfs_free_bitmap_cache(s);
619 620 621 622 623 624

	brelse(SB_BUFFER_WITH_SB(s));

	print_statistics(s);

	if (REISERFS_SB(s)->reserved_blocks != 0) {
J
Jeff Mahoney 已提交
625
		reiserfs_warning(s, "green-2005", "reserved blocks left %d",
626 627 628
				 REISERFS_SB(s)->reserved_blocks);
	}

F
Frederic Weisbecker 已提交
629 630
	reiserfs_write_unlock(s);
	mutex_destroy(&REISERFS_SB(s)->lock);
631
	destroy_workqueue(REISERFS_SB(s)->commit_wq);
632 633
	kfree(s->s_fs_info);
	s->s_fs_info = NULL;
L
Linus Torvalds 已提交
634 635
}

636
static struct kmem_cache *reiserfs_inode_cachep;
L
Linus Torvalds 已提交
637 638 639 640

static struct inode *reiserfs_alloc_inode(struct super_block *sb)
{
	struct reiserfs_inode_info *ei;
F
Firo Yang 已提交
641
	ei = kmem_cache_alloc(reiserfs_inode_cachep, GFP_KERNEL);
L
Linus Torvalds 已提交
642 643
	if (!ei)
		return NULL;
A
Al Viro 已提交
644 645
	atomic_set(&ei->openers, 0);
	mutex_init(&ei->tailpack);
646 647 648 649
#ifdef CONFIG_QUOTA
	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
#endif

L
Linus Torvalds 已提交
650 651 652
	return &ei->vfs_inode;
}

N
Nick Piggin 已提交
653
static void reiserfs_i_callback(struct rcu_head *head)
L
Linus Torvalds 已提交
654
{
N
Nick Piggin 已提交
655
	struct inode *inode = container_of(head, struct inode, i_rcu);
L
Linus Torvalds 已提交
656 657 658
	kmem_cache_free(reiserfs_inode_cachep, REISERFS_I(inode));
}

N
Nick Piggin 已提交
659 660 661 662 663
static void reiserfs_destroy_inode(struct inode *inode)
{
	call_rcu(&inode->i_rcu, reiserfs_i_callback);
}

664
static void init_once(void *foo)
L
Linus Torvalds 已提交
665
{
666
	struct reiserfs_inode_info *ei = (struct reiserfs_inode_info *)foo;
L
Linus Torvalds 已提交
667

C
Christoph Lameter 已提交
668 669
	INIT_LIST_HEAD(&ei->i_prealloc_list);
	inode_init_once(&ei->vfs_inode);
L
Linus Torvalds 已提交
670
}
671

672
static int __init init_inodecache(void)
L
Linus Torvalds 已提交
673 674
{
	reiserfs_inode_cachep = kmem_cache_create("reiser_inode_cache",
675 676
						  sizeof(struct
							 reiserfs_inode_info),
677
						  0, (SLAB_RECLAIM_ACCOUNT|
678 679
						      SLAB_MEM_SPREAD|
						      SLAB_ACCOUNT),
680
						  init_once);
L
Linus Torvalds 已提交
681 682 683 684 685 686 687
	if (reiserfs_inode_cachep == NULL)
		return -ENOMEM;
	return 0;
}

static void destroy_inodecache(void)
{
688 689 690 691 692
	/*
	 * Make sure all delayed rcu free inodes are flushed before we
	 * destroy cache.
	 */
	rcu_barrier();
693
	kmem_cache_destroy(reiserfs_inode_cachep);
L
Linus Torvalds 已提交
694 695 696
}

/* we don't mark inodes dirty, we just log them */
697
static void reiserfs_dirty_inode(struct inode *inode, int flags)
698 699 700 701
{
	struct reiserfs_transaction_handle th;

	int err = 0;
702

703
	if (sb_rdonly(inode->i_sb)) {
J
Jeff Mahoney 已提交
704 705
		reiserfs_warning(inode->i_sb, "clm-6006",
				 "writing inode %lu on readonly FS",
706 707 708
				 inode->i_ino);
		return;
	}
709
	reiserfs_write_lock(inode->i_sb);
710

711 712 713
	/*
	 * this is really only used for atime updates, so they don't have
	 * to be included in O_SYNC or fsync
714 715
	 */
	err = journal_begin(&th, inode->i_sb, 1);
716 717 718
	if (err)
		goto out;

719
	reiserfs_update_sd(&th, inode);
720
	journal_end(&th);
721 722

out:
723
	reiserfs_write_unlock(inode->i_sb);
L
Linus Torvalds 已提交
724 725
}

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
static int reiserfs_show_options(struct seq_file *seq, struct dentry *root)
{
	struct super_block *s = root->d_sb;
	struct reiserfs_journal *journal = SB_JOURNAL(s);
	long opts = REISERFS_SB(s)->s_mount_opt;

	if (opts & (1 << REISERFS_LARGETAIL))
		seq_puts(seq, ",tails=on");
	else if (!(opts & (1 << REISERFS_SMALLTAIL)))
		seq_puts(seq, ",notail");
	/* tails=small is default so we don't show it */

	if (!(opts & (1 << REISERFS_BARRIER_FLUSH)))
		seq_puts(seq, ",barrier=none");
	/* barrier=flush is default so we don't show it */

	if (opts & (1 << REISERFS_ERROR_CONTINUE))
		seq_puts(seq, ",errors=continue");
	else if (opts & (1 << REISERFS_ERROR_PANIC))
		seq_puts(seq, ",errors=panic");
	/* errors=ro is default so we don't show it */

	if (opts & (1 << REISERFS_DATA_LOG))
		seq_puts(seq, ",data=journal");
	else if (opts & (1 << REISERFS_DATA_WRITEBACK))
		seq_puts(seq, ",data=writeback");
	/* data=ordered is default so we don't show it */

	if (opts & (1 << REISERFS_ATTRS))
		seq_puts(seq, ",attrs");

	if (opts & (1 << REISERFS_XATTRS_USER))
		seq_puts(seq, ",user_xattr");

	if (opts & (1 << REISERFS_EXPOSE_PRIVROOT))
		seq_puts(seq, ",expose_privroot");

	if (opts & (1 << REISERFS_POSIXACL))
		seq_puts(seq, ",acl");

	if (REISERFS_SB(s)->s_jdev)
767
		seq_show_option(seq, "jdev", REISERFS_SB(s)->s_jdev);
768 769 770 771 772 773

	if (journal->j_max_commit_age != journal->j_default_max_commit_age)
		seq_printf(seq, ",commit=%d", journal->j_max_commit_age);

#ifdef CONFIG_QUOTA
	if (REISERFS_SB(s)->s_qf_names[USRQUOTA])
774 775
		seq_show_option(seq, "usrjquota",
				REISERFS_SB(s)->s_qf_names[USRQUOTA]);
776 777 778
	else if (opts & (1 << REISERFS_USRQUOTA))
		seq_puts(seq, ",usrquota");
	if (REISERFS_SB(s)->s_qf_names[GRPQUOTA])
779 780
		seq_show_option(seq, "grpjquota",
				REISERFS_SB(s)->s_qf_names[GRPQUOTA]);
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
	else if (opts & (1 << REISERFS_GRPQUOTA))
		seq_puts(seq, ",grpquota");
	if (REISERFS_SB(s)->s_jquota_fmt) {
		if (REISERFS_SB(s)->s_jquota_fmt == QFMT_VFS_OLD)
			seq_puts(seq, ",jqfmt=vfsold");
		else if (REISERFS_SB(s)->s_jquota_fmt == QFMT_VFS_V0)
			seq_puts(seq, ",jqfmt=vfsv0");
	}
#endif

	/* Block allocator options */
	if (opts & (1 << REISERFS_NO_BORDER))
		seq_puts(seq, ",block-allocator=noborder");
	if (opts & (1 << REISERFS_NO_UNHASHED_RELOCATION))
		seq_puts(seq, ",block-allocator=no_unhashed_relocation");
	if (opts & (1 << REISERFS_HASHED_RELOCATION))
		seq_puts(seq, ",block-allocator=hashed_relocation");
	if (opts & (1 << REISERFS_TEST4))
		seq_puts(seq, ",block-allocator=test4");
	show_alloc_options(seq, s);
	return 0;
}

L
Linus Torvalds 已提交
804
#ifdef CONFIG_QUOTA
805 806 807 808
static ssize_t reiserfs_quota_write(struct super_block *, int, const char *,
				    size_t, loff_t);
static ssize_t reiserfs_quota_read(struct super_block *, int, char *, size_t,
				   loff_t);
809 810 811 812 813

static struct dquot **reiserfs_get_dquots(struct inode *inode)
{
	return REISERFS_I(inode)->i_dquot;
}
L
Linus Torvalds 已提交
814 815
#endif

816
static const struct super_operations reiserfs_sops = {
817 818 819 820
	.alloc_inode = reiserfs_alloc_inode,
	.destroy_inode = reiserfs_destroy_inode,
	.write_inode = reiserfs_write_inode,
	.dirty_inode = reiserfs_dirty_inode,
A
Al Viro 已提交
821
	.evict_inode = reiserfs_evict_inode,
822 823
	.put_super = reiserfs_put_super,
	.sync_fs = reiserfs_sync_fs,
824 825
	.freeze_fs = reiserfs_freeze,
	.unfreeze_fs = reiserfs_unfreeze,
826 827
	.statfs = reiserfs_statfs,
	.remount_fs = reiserfs_remount,
828
	.show_options = reiserfs_show_options,
L
Linus Torvalds 已提交
829
#ifdef CONFIG_QUOTA
830 831
	.quota_read = reiserfs_quota_read,
	.quota_write = reiserfs_quota_write,
832
	.get_dquots = reiserfs_get_dquots,
L
Linus Torvalds 已提交
833 834 835 836 837 838 839 840 841 842 843
#endif
};

#ifdef CONFIG_QUOTA
#define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")

static int reiserfs_write_dquot(struct dquot *);
static int reiserfs_acquire_dquot(struct dquot *);
static int reiserfs_release_dquot(struct dquot *);
static int reiserfs_mark_dquot_dirty(struct dquot *);
static int reiserfs_write_info(struct super_block *, int);
A
Al Viro 已提交
844
static int reiserfs_quota_on(struct super_block *, int, int, const struct path *);
L
Linus Torvalds 已提交
845

846
static const struct dquot_operations reiserfs_quota_operations = {
847 848 849 850 851
	.write_dquot = reiserfs_write_dquot,
	.acquire_dquot = reiserfs_acquire_dquot,
	.release_dquot = reiserfs_release_dquot,
	.mark_dirty = reiserfs_mark_dquot_dirty,
	.write_info = reiserfs_write_info,
852 853
	.alloc_dquot	= dquot_alloc,
	.destroy_dquot	= dquot_destroy,
854
	.get_next_id	= dquot_get_next_id,
L
Linus Torvalds 已提交
855 856
};

857
static const struct quotactl_ops reiserfs_qctl_operations = {
858
	.quota_on = reiserfs_quota_on,
859
	.quota_off = reiserfs_quota_off,
860
	.quota_sync = dquot_quota_sync,
861
	.get_state = dquot_get_state,
862 863 864
	.set_info = dquot_set_dqinfo,
	.get_dqblk = dquot_get_dqblk,
	.set_dqblk = dquot_set_dqblk,
L
Linus Torvalds 已提交
865 866 867
};
#endif

868
static const struct export_operations reiserfs_export_ops = {
869
	.encode_fh = reiserfs_encode_fh,
C
Christoph Hellwig 已提交
870 871
	.fh_to_dentry = reiserfs_fh_to_dentry,
	.fh_to_parent = reiserfs_fh_to_parent,
872 873
	.get_parent = reiserfs_get_parent,
};
L
Linus Torvalds 已提交
874

875 876 877 878
/*
 * this struct is used in reiserfs_getopt () for containing the value for
 * those mount options that have values rather than being toggles.
 */
L
Linus Torvalds 已提交
879
typedef struct {
880
	char *value;
881 882 883 884 885 886 887 888 889 890 891
	/*
	 * bitmask which is to set on mount_options bitmask
	 * when this value is found, 0 is no bits are to be changed.
	 */
	int setmask;
	/*
	 * bitmask which is to clear on mount_options bitmask
	 * when this value is found, 0 is no bits are to be changed.
	 * This is applied BEFORE setmask
	 */
	int clrmask;
L
Linus Torvalds 已提交
892 893 894 895 896
} arg_desc_t;

/* Set this bit in arg_required to allow empty arguments */
#define REISERFS_OPT_ALLOWEMPTY 31

897 898 899 900
/*
 * this struct is used in reiserfs_getopt() for describing the
 * set of reiserfs mount options
 */
L
Linus Torvalds 已提交
901
typedef struct {
902
	char *option_name;
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921

	/* 0 if argument is not required, not 0 otherwise */
	int arg_required;

	/* list of values accepted by an option */
	const arg_desc_t *values;

	/*
	 * bitmask which is to set on mount_options bitmask
	 * when this value is found, 0 is no bits are to be changed.
	 */
	int setmask;

	/*
	 * bitmask which is to clear on mount_options bitmask
	 * when this value is found, 0 is no bits are to be changed.
	 * This is applied BEFORE setmask
	 */
	int clrmask;
L
Linus Torvalds 已提交
922 923 924 925
} opt_desc_t;

/* possible values for -o data= */
static const arg_desc_t logging_mode[] = {
926 927 928 929 930 931
	{"ordered", 1 << REISERFS_DATA_ORDERED,
	 (1 << REISERFS_DATA_LOG | 1 << REISERFS_DATA_WRITEBACK)},
	{"journal", 1 << REISERFS_DATA_LOG,
	 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_WRITEBACK)},
	{"writeback", 1 << REISERFS_DATA_WRITEBACK,
	 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_LOG)},
932
	{.value = NULL}
L
Linus Torvalds 已提交
933 934 935 936
};

/* possible values for -o barrier= */
static const arg_desc_t barrier_mode[] = {
937 938
	{"none", 1 << REISERFS_BARRIER_NONE, 1 << REISERFS_BARRIER_FLUSH},
	{"flush", 1 << REISERFS_BARRIER_FLUSH, 1 << REISERFS_BARRIER_NONE},
939
	{.value = NULL}
L
Linus Torvalds 已提交
940 941
};

942 943 944 945
/*
 * possible values for "-o block-allocator=" and bits which are to be set in
 * s_mount_opt of reiserfs specific part of in-core super block
 */
L
Linus Torvalds 已提交
946
static const arg_desc_t balloc[] = {
947 948 949 950 951 952 953
	{"noborder", 1 << REISERFS_NO_BORDER, 0},
	{"border", 0, 1 << REISERFS_NO_BORDER},
	{"no_unhashed_relocation", 1 << REISERFS_NO_UNHASHED_RELOCATION, 0},
	{"hashed_relocation", 1 << REISERFS_HASHED_RELOCATION, 0},
	{"test4", 1 << REISERFS_TEST4, 0},
	{"notest4", 0, 1 << REISERFS_TEST4},
	{NULL, 0, 0}
L
Linus Torvalds 已提交
954 955 956
};

static const arg_desc_t tails[] = {
957 958 959 960
	{"on", 1 << REISERFS_LARGETAIL, 1 << REISERFS_SMALLTAIL},
	{"off", 0, (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
	{"small", 1 << REISERFS_SMALLTAIL, 1 << REISERFS_LARGETAIL},
	{NULL, 0, 0}
L
Linus Torvalds 已提交
961 962 963
};

static const arg_desc_t error_actions[] = {
964 965 966 967
	{"panic", 1 << REISERFS_ERROR_PANIC,
	 (1 << REISERFS_ERROR_RO | 1 << REISERFS_ERROR_CONTINUE)},
	{"ro-remount", 1 << REISERFS_ERROR_RO,
	 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_CONTINUE)},
L
Linus Torvalds 已提交
968
#ifdef REISERFS_JOURNAL_ERROR_ALLOWS_NO_LOG
969 970
	{"continue", 1 << REISERFS_ERROR_CONTINUE,
	 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_RO)},
L
Linus Torvalds 已提交
971
#endif
972
	{NULL, 0, 0},
L
Linus Torvalds 已提交
973 974
};

975 976 977 978 979 980 981 982 983
/*
 * proceed only one option from a list *cur - string containing of mount
 * options
 * opts - array of options which are accepted
 * opt_arg - if option is found and requires an argument and if it is specifed
 * in the input - pointer to the argument is stored here
 * bit_flags - if option requires to set a certain bit - it is set here
 * return -1 if unknown option is found, opt->arg_required otherwise
 */
984 985
static int reiserfs_getopt(struct super_block *s, char **cur, opt_desc_t * opts,
			   char **opt_arg, unsigned long *bit_flags)
L
Linus Torvalds 已提交
986
{
987
	char *p;
988 989 990 991 992 993
	/*
	 * foo=bar,
	 * ^   ^  ^
	 * |   |  +-- option_end
	 * |   +-- arg_start
	 * +-- option_start
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
	 */
	const opt_desc_t *opt;
	const arg_desc_t *arg;

	p = *cur;

	/* assume argument cannot contain commas */
	*cur = strchr(p, ',');
	if (*cur) {
		*(*cur) = '\0';
		(*cur)++;
	}

	if (!strncmp(p, "alloc=", 6)) {
1008 1009 1010 1011 1012 1013
		/*
		 * Ugly special case, probably we should redo options
		 * parser so that it can understand several arguments for
		 * some options, also so that it can fill several bitfields
		 * with option values.
		 */
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
		if (reiserfs_parse_alloc_options(s, p + 6)) {
			return -1;
		} else {
			return 0;
		}
	}

	/* for every option in the list */
	for (opt = opts; opt->option_name; opt++) {
		if (!strncmp(p, opt->option_name, strlen(opt->option_name))) {
			if (bit_flags) {
				if (opt->clrmask ==
				    (1 << REISERFS_UNSUPPORTED_OPT))
J
Jeff Mahoney 已提交
1027 1028
					reiserfs_warning(s, "super-6500",
							 "%s not supported.\n",
1029 1030 1031 1032 1033
							 p);
				else
					*bit_flags &= ~opt->clrmask;
				if (opt->setmask ==
				    (1 << REISERFS_UNSUPPORTED_OPT))
J
Jeff Mahoney 已提交
1034 1035
					reiserfs_warning(s, "super-6501",
							 "%s not supported.\n",
1036 1037 1038 1039 1040 1041 1042 1043
							 p);
				else
					*bit_flags |= opt->setmask;
			}
			break;
		}
	}
	if (!opt->option_name) {
J
Jeff Mahoney 已提交
1044 1045
		reiserfs_warning(s, "super-6502",
				 "unknown mount option \"%s\"", p);
1046 1047 1048 1049 1050 1051 1052
		return -1;
	}

	p += strlen(opt->option_name);
	switch (*p) {
	case '=':
		if (!opt->arg_required) {
J
Jeff Mahoney 已提交
1053 1054 1055
			reiserfs_warning(s, "super-6503",
					 "the option \"%s\" does not "
					 "require an argument\n",
1056 1057 1058 1059 1060 1061 1062
					 opt->option_name);
			return -1;
		}
		break;

	case 0:
		if (opt->arg_required) {
J
Jeff Mahoney 已提交
1063 1064 1065
			reiserfs_warning(s, "super-6504",
					 "the option \"%s\" requires an "
					 "argument\n", opt->option_name);
1066 1067 1068 1069
			return -1;
		}
		break;
	default:
J
Jeff Mahoney 已提交
1070 1071
		reiserfs_warning(s, "super-6505",
				 "head of option \"%s\" is only correct\n",
1072 1073 1074 1075
				 opt->option_name);
		return -1;
	}

1076 1077 1078 1079
	/*
	 * move to the argument, or to next option if argument is not
	 * required
	 */
1080 1081 1082 1083 1084 1085
	p++;

	if (opt->arg_required
	    && !(opt->arg_required & (1 << REISERFS_OPT_ALLOWEMPTY))
	    && !strlen(p)) {
		/* this catches "option=," if not allowed */
J
Jeff Mahoney 已提交
1086 1087
		reiserfs_warning(s, "super-6506",
				 "empty argument for \"%s\"\n",
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
				 opt->option_name);
		return -1;
	}

	if (!opt->values) {
		/* *=NULLopt_arg contains pointer to argument */
		*opt_arg = p;
		return opt->arg_required & ~(1 << REISERFS_OPT_ALLOWEMPTY);
	}

	/* values possible for this option are listed in opt->values */
	for (arg = opt->values; arg->value; arg++) {
		if (!strcmp(p, arg->value)) {
			if (bit_flags) {
				*bit_flags &= ~arg->clrmask;
				*bit_flags |= arg->setmask;
			}
			return opt->arg_required;
		}
	}

J
Jeff Mahoney 已提交
1109 1110
	reiserfs_warning(s, "super-6506",
			 "bad value \"%s\" for option \"%s\"\n", p,
1111
			 opt->option_name);
L
Linus Torvalds 已提交
1112 1113 1114 1115
	return -1;
}

/* returns 0 if something is wrong in option string, 1 - otherwise */
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
static int reiserfs_parse_options(struct super_block *s,

				  /* string given via mount's -o */
				  char *options,

				  /*
				   * after the parsing phase, contains the
				   * collection of bitflags defining what
				   * mount options were selected.
				   */
1126
				  unsigned long *mount_options,
1127 1128 1129

				  /* strtol-ed from NNN of resize=NNN */
				  unsigned long *blocks,
1130
				  char **jdev_name,
1131 1132 1133
				  unsigned int *commit_max_age,
				  char **qf_names,
				  unsigned int *qfmt)
L
Linus Torvalds 已提交
1134
{
1135 1136 1137 1138
	int c;
	char *arg = NULL;
	char *pos;
	opt_desc_t opts[] = {
1139 1140 1141 1142
		/*
		 * Compatibility stuff, so that -o notail for old
		 * setups still work
		 */
1143 1144 1145 1146 1147 1148
		{"tails",.arg_required = 't',.values = tails},
		{"notail",.clrmask =
		 (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
		{"conv",.setmask = 1 << REISERFS_CONVERT},
		{"attrs",.setmask = 1 << REISERFS_ATTRS},
		{"noattrs",.clrmask = 1 << REISERFS_ATTRS},
1149
		{"expose_privroot", .setmask = 1 << REISERFS_EXPOSE_PRIVROOT},
L
Linus Torvalds 已提交
1150
#ifdef CONFIG_REISERFS_FS_XATTR
1151 1152
		{"user_xattr",.setmask = 1 << REISERFS_XATTRS_USER},
		{"nouser_xattr",.clrmask = 1 << REISERFS_XATTRS_USER},
L
Linus Torvalds 已提交
1153
#else
1154 1155
		{"user_xattr",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
		{"nouser_xattr",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
L
Linus Torvalds 已提交
1156 1157
#endif
#ifdef CONFIG_REISERFS_FS_POSIX_ACL
1158 1159
		{"acl",.setmask = 1 << REISERFS_POSIXACL},
		{"noacl",.clrmask = 1 << REISERFS_POSIXACL},
L
Linus Torvalds 已提交
1160
#else
1161 1162
		{"acl",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
		{"noacl",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
L
Linus Torvalds 已提交
1163
#endif
1164
		{.option_name = "nolog"},
1165 1166 1167 1168 1169 1170 1171 1172
		{"replayonly",.setmask = 1 << REPLAYONLY},
		{"block-allocator",.arg_required = 'a',.values = balloc},
		{"data",.arg_required = 'd',.values = logging_mode},
		{"barrier",.arg_required = 'b',.values = barrier_mode},
		{"resize",.arg_required = 'r',.values = NULL},
		{"jdev",.arg_required = 'j',.values = NULL},
		{"nolargeio",.arg_required = 'w',.values = NULL},
		{"commit",.arg_required = 'c',.values = NULL},
1173 1174 1175
		{"usrquota",.setmask = 1 << REISERFS_USRQUOTA},
		{"grpquota",.setmask = 1 << REISERFS_GRPQUOTA},
		{"noquota",.clrmask = 1 << REISERFS_USRQUOTA | 1 << REISERFS_GRPQUOTA},
1176 1177 1178 1179 1180 1181
		{"errors",.arg_required = 'e',.values = error_actions},
		{"usrjquota",.arg_required =
		 'u' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
		{"grpjquota",.arg_required =
		 'g' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
		{"jqfmt",.arg_required = 'f',.values = NULL},
1182
		{.option_name = NULL}
1183 1184 1185 1186
	};

	*blocks = 0;
	if (!options || !*options)
1187 1188 1189 1190
		/*
		 * use default configuration: create tails, journaling on, no
		 * conversion to newest format
		 */
1191 1192 1193 1194 1195 1196
		return 1;

	for (pos = options; pos;) {
		c = reiserfs_getopt(s, &pos, opts, &arg, mount_options);
		if (c == -1)
			/* wrong option is given */
1197
			return 0;
L
Linus Torvalds 已提交
1198

1199 1200 1201 1202 1203 1204 1205 1206 1207
		if (c == 'r') {
			char *p;

			p = NULL;
			/* "resize=NNN" or "resize=auto" */

			if (!strcmp(arg, "auto")) {
				/* From JFS code, to auto-get the size. */
				*blocks =
1208
				    i_size_read(s->s_bdev->bd_inode) >> s->
1209 1210 1211 1212 1213
				    s_blocksize_bits;
			} else {
				*blocks = simple_strtoul(arg, &p, 0);
				if (*p != '\0') {
					/* NNN does not look like a number */
J
Jeff Mahoney 已提交
1214 1215 1216
					reiserfs_warning(s, "super-6507",
							 "bad value %s for "
							 "-oresize\n", arg);
1217 1218 1219
					return 0;
				}
			}
L
Linus Torvalds 已提交
1220 1221
		}

1222 1223 1224 1225 1226
		if (c == 'c') {
			char *p = NULL;
			unsigned long val = simple_strtoul(arg, &p, 0);
			/* commit=NNN (time in seconds) */
			if (*p != '\0' || val >= (unsigned int)-1) {
J
Jeff Mahoney 已提交
1227 1228
				reiserfs_warning(s, "super-6508",
						 "bad value %s for -ocommit\n",
1229 1230 1231 1232
						 arg);
				return 0;
			}
			*commit_max_age = (unsigned int)val;
L
Linus Torvalds 已提交
1233 1234
		}

1235
		if (c == 'w') {
J
Jeff Mahoney 已提交
1236 1237
			reiserfs_warning(s, "super-6509", "nolargeio option "
					 "is no longer supported");
1238
			return 0;
L
Linus Torvalds 已提交
1239 1240
		}

1241 1242
		if (c == 'j') {
			if (arg && *arg && jdev_name) {
1243 1244
				/* Hm, already assigned? */
				if (*jdev_name) {
J
Jeff Mahoney 已提交
1245 1246 1247 1248
					reiserfs_warning(s, "super-6510",
							 "journal device was "
							 "already specified to "
							 "be %s", *jdev_name);
1249 1250 1251 1252
					return 0;
				}
				*jdev_name = arg;
			}
L
Linus Torvalds 已提交
1253
		}
1254 1255 1256 1257
#ifdef CONFIG_QUOTA
		if (c == 'u' || c == 'g') {
			int qtype = c == 'u' ? USRQUOTA : GRPQUOTA;

1258
			if (sb_any_quota_loaded(s) &&
1259
			    (!*arg != !REISERFS_SB(s)->s_qf_names[qtype])) {
J
Jeff Mahoney 已提交
1260 1261 1262 1263
				reiserfs_warning(s, "super-6511",
						 "cannot change journaled "
						 "quota options when quota "
						 "turned on.");
1264 1265 1266 1267 1268 1269
				return 0;
			}
			if (*arg) {	/* Some filename specified? */
				if (REISERFS_SB(s)->s_qf_names[qtype]
				    && strcmp(REISERFS_SB(s)->s_qf_names[qtype],
					      arg)) {
J
Jeff Mahoney 已提交
1270 1271 1272
					reiserfs_warning(s, "super-6512",
							 "%s quota file "
							 "already specified.",
1273 1274 1275 1276
							 QTYPE2NAME(qtype));
					return 0;
				}
				if (strchr(arg, '/')) {
J
Jeff Mahoney 已提交
1277 1278 1279
					reiserfs_warning(s, "super-6513",
							 "quotafile must be "
							 "on filesystem root.");
1280 1281
					return 0;
				}
1282
				qf_names[qtype] = kstrdup(arg, GFP_KERNEL);
1283
				if (!qf_names[qtype]) {
J
Jeff Mahoney 已提交
1284 1285 1286 1287
					reiserfs_warning(s, "reiserfs-2502",
							 "not enough memory "
							 "for storing "
							 "quotafile name.");
1288 1289
					return 0;
				}
1290 1291 1292 1293
				if (qtype == USRQUOTA)
					*mount_options |= 1 << REISERFS_USRQUOTA;
				else
					*mount_options |= 1 << REISERFS_GRPQUOTA;
1294
			} else {
1295 1296 1297 1298
				if (qf_names[qtype] !=
				    REISERFS_SB(s)->s_qf_names[qtype])
					kfree(qf_names[qtype]);
				qf_names[qtype] = NULL;
1299 1300 1301 1302
				if (qtype == USRQUOTA)
					*mount_options &= ~(1 << REISERFS_USRQUOTA);
				else
					*mount_options &= ~(1 << REISERFS_GRPQUOTA);
1303
			}
L
Linus Torvalds 已提交
1304
		}
1305 1306
		if (c == 'f') {
			if (!strcmp(arg, "vfsold"))
1307
				*qfmt = QFMT_VFS_OLD;
1308
			else if (!strcmp(arg, "vfsv0"))
1309
				*qfmt = QFMT_VFS_V0;
1310
			else {
J
Jeff Mahoney 已提交
1311 1312 1313
				reiserfs_warning(s, "super-6514",
						 "unknown quota format "
						 "specified.");
1314 1315
				return 0;
			}
1316
			if (sb_any_quota_loaded(s) &&
1317
			    *qfmt != REISERFS_SB(s)->s_jquota_fmt) {
J
Jeff Mahoney 已提交
1318 1319 1320 1321
				reiserfs_warning(s, "super-6515",
						 "cannot change journaled "
						 "quota options when quota "
						 "turned on.");
1322 1323
				return 0;
			}
L
Linus Torvalds 已提交
1324
		}
1325 1326
#else
		if (c == 'u' || c == 'g' || c == 'f') {
J
Jeff Mahoney 已提交
1327 1328
			reiserfs_warning(s, "reiserfs-2503", "journaled "
					 "quota options not supported.");
1329
			return 0;
L
Linus Torvalds 已提交
1330
		}
1331 1332 1333 1334
#endif
	}

#ifdef CONFIG_QUOTA
1335 1336
	if (!REISERFS_SB(s)->s_jquota_fmt && !*qfmt
	    && (qf_names[USRQUOTA] || qf_names[GRPQUOTA])) {
J
Jeff Mahoney 已提交
1337 1338
		reiserfs_warning(s, "super-6515",
				 "journaled quota format not specified.");
L
Linus Torvalds 已提交
1339 1340
		return 0;
	}
1341 1342 1343 1344
	if ((!(*mount_options & (1 << REISERFS_USRQUOTA)) &&
	       sb_has_quota_loaded(s, USRQUOTA)) ||
	    (!(*mount_options & (1 << REISERFS_GRPQUOTA)) &&
	       sb_has_quota_loaded(s, GRPQUOTA))) {
J
Jeff Mahoney 已提交
1345 1346
		reiserfs_warning(s, "super-6516", "quota options must "
				 "be present when quota is turned on.");
1347
		return 0;
L
Linus Torvalds 已提交
1348 1349
	}
#endif
1350

1351
	return 1;
L
Linus Torvalds 已提交
1352 1353
}

1354 1355 1356 1357 1358 1359
static void switch_data_mode(struct super_block *s, unsigned long mode)
{
	REISERFS_SB(s)->s_mount_opt &= ~((1 << REISERFS_DATA_LOG) |
					 (1 << REISERFS_DATA_ORDERED) |
					 (1 << REISERFS_DATA_WRITEBACK));
	REISERFS_SB(s)->s_mount_opt |= (1 << mode);
L
Linus Torvalds 已提交
1360 1361 1362 1363
}

static void handle_data_mode(struct super_block *s, unsigned long mount_options)
{
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
	if (mount_options & (1 << REISERFS_DATA_LOG)) {
		if (!reiserfs_data_log(s)) {
			switch_data_mode(s, REISERFS_DATA_LOG);
			reiserfs_info(s, "switching to journaled data mode\n");
		}
	} else if (mount_options & (1 << REISERFS_DATA_ORDERED)) {
		if (!reiserfs_data_ordered(s)) {
			switch_data_mode(s, REISERFS_DATA_ORDERED);
			reiserfs_info(s, "switching to ordered data mode\n");
		}
	} else if (mount_options & (1 << REISERFS_DATA_WRITEBACK)) {
		if (!reiserfs_data_writeback(s)) {
			switch_data_mode(s, REISERFS_DATA_WRITEBACK);
			reiserfs_info(s, "switching to writeback data mode\n");
		}
	}
L
Linus Torvalds 已提交
1380 1381
}

1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
static void handle_barrier_mode(struct super_block *s, unsigned long bits)
{
	int flush = (1 << REISERFS_BARRIER_FLUSH);
	int none = (1 << REISERFS_BARRIER_NONE);
	int all_barrier = flush | none;

	if (bits & all_barrier) {
		REISERFS_SB(s)->s_mount_opt &= ~all_barrier;
		if (bits & flush) {
			REISERFS_SB(s)->s_mount_opt |= flush;
			printk("reiserfs: enabling write barrier flush mode\n");
		} else if (bits & none) {
			REISERFS_SB(s)->s_mount_opt |= none;
			printk("reiserfs: write barriers turned off\n");
		}
	}
L
Linus Torvalds 已提交
1398 1399
}

1400
static void handle_attrs(struct super_block *s)
L
Linus Torvalds 已提交
1401
{
1402
	struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(s);
L
Linus Torvalds 已提交
1403

1404 1405
	if (reiserfs_attrs(s)) {
		if (old_format_only(s)) {
J
Jeff Mahoney 已提交
1406 1407
			reiserfs_warning(s, "super-6517", "cannot support "
					 "attributes on 3.5.x disk format");
1408
			REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
L
Linus Torvalds 已提交
1409 1410
			return;
		}
1411
		if (!(le32_to_cpu(rs->s_flags) & reiserfs_attrs_cleared)) {
J
Jeff Mahoney 已提交
1412 1413 1414
			reiserfs_warning(s, "super-6518", "cannot support "
					 "attributes until flag is set in "
					 "super-block");
1415
			REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
L
Linus Torvalds 已提交
1416 1417 1418 1419
		}
	}
}

1420 1421 1422 1423 1424 1425
#ifdef CONFIG_QUOTA
static void handle_quota_files(struct super_block *s, char **qf_names,
			       unsigned int *qfmt)
{
	int i;

J
Jan Kara 已提交
1426
	for (i = 0; i < REISERFS_MAXQUOTAS; i++) {
1427 1428 1429 1430
		if (qf_names[i] != REISERFS_SB(s)->s_qf_names[i])
			kfree(REISERFS_SB(s)->s_qf_names[i]);
		REISERFS_SB(s)->s_qf_names[i] = qf_names[i];
	}
1431 1432
	if (*qfmt)
		REISERFS_SB(s)->s_jquota_fmt = *qfmt;
1433 1434 1435
}
#endif

1436
static int reiserfs_remount(struct super_block *s, int *mount_flags, char *arg)
L
Linus Torvalds 已提交
1437
{
1438 1439 1440 1441 1442 1443 1444
	struct reiserfs_super_block *rs;
	struct reiserfs_transaction_handle th;
	unsigned long blocks;
	unsigned long mount_options = REISERFS_SB(s)->s_mount_opt;
	unsigned long safe_mask = 0;
	unsigned int commit_max_age = (unsigned int)-1;
	struct reiserfs_journal *journal = SB_JOURNAL(s);
1445
	char *new_opts;
1446
	int err;
J
Jan Kara 已提交
1447
	char *qf_names[REISERFS_MAXQUOTAS];
1448
	unsigned int qfmt = 0;
L
Linus Torvalds 已提交
1449
#ifdef CONFIG_QUOTA
1450
	int i;
F
Frederic Weisbecker 已提交
1451
#endif
1452

1453 1454 1455 1456
	new_opts = kstrdup(arg, GFP_KERNEL);
	if (arg && !new_opts)
		return -ENOMEM;

1457
	sync_filesystem(s);
F
Frederic Weisbecker 已提交
1458
	reiserfs_write_lock(s);
1459

F
Frederic Weisbecker 已提交
1460
#ifdef CONFIG_QUOTA
1461
	memcpy(qf_names, REISERFS_SB(s)->s_qf_names, sizeof(qf_names));
L
Linus Torvalds 已提交
1462 1463
#endif

1464
	rs = SB_DISK_SUPER_BLOCK(s);
L
Linus Torvalds 已提交
1465

1466
	if (!reiserfs_parse_options
1467 1468
	    (s, arg, &mount_options, &blocks, NULL, &commit_max_age,
	    qf_names, &qfmt)) {
L
Linus Torvalds 已提交
1469
#ifdef CONFIG_QUOTA
J
Jan Kara 已提交
1470
		for (i = 0; i < REISERFS_MAXQUOTAS; i++)
1471 1472
			if (qf_names[i] != REISERFS_SB(s)->s_qf_names[i])
				kfree(qf_names[i]);
L
Linus Torvalds 已提交
1473
#endif
M
Miklos Szeredi 已提交
1474
		err = -EINVAL;
1475
		goto out_err_unlock;
1476
	}
1477 1478 1479
#ifdef CONFIG_QUOTA
	handle_quota_files(s, qf_names, &qfmt);
#endif
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497

	handle_attrs(s);

	/* Add options that are safe here */
	safe_mask |= 1 << REISERFS_SMALLTAIL;
	safe_mask |= 1 << REISERFS_LARGETAIL;
	safe_mask |= 1 << REISERFS_NO_BORDER;
	safe_mask |= 1 << REISERFS_NO_UNHASHED_RELOCATION;
	safe_mask |= 1 << REISERFS_HASHED_RELOCATION;
	safe_mask |= 1 << REISERFS_TEST4;
	safe_mask |= 1 << REISERFS_ATTRS;
	safe_mask |= 1 << REISERFS_XATTRS_USER;
	safe_mask |= 1 << REISERFS_POSIXACL;
	safe_mask |= 1 << REISERFS_BARRIER_FLUSH;
	safe_mask |= 1 << REISERFS_BARRIER_NONE;
	safe_mask |= 1 << REISERFS_ERROR_RO;
	safe_mask |= 1 << REISERFS_ERROR_CONTINUE;
	safe_mask |= 1 << REISERFS_ERROR_PANIC;
1498 1499
	safe_mask |= 1 << REISERFS_USRQUOTA;
	safe_mask |= 1 << REISERFS_GRPQUOTA;
1500

1501 1502 1503 1504
	/*
	 * Update the bitmask, taking care to keep
	 * the bits we're not allowed to change here
	 */
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
	REISERFS_SB(s)->s_mount_opt =
	    (REISERFS_SB(s)->
	     s_mount_opt & ~safe_mask) | (mount_options & safe_mask);

	if (commit_max_age != 0 && commit_max_age != (unsigned int)-1) {
		journal->j_max_commit_age = commit_max_age;
		journal->j_max_trans_age = commit_max_age;
	} else if (commit_max_age == 0) {
		/* 0 means restore defaults. */
		journal->j_max_commit_age = journal->j_default_max_commit_age;
		journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
	}

	if (blocks) {
M
Miklos Szeredi 已提交
1519 1520
		err = reiserfs_resize(s, blocks);
		if (err != 0)
1521
			goto out_err_unlock;
1522 1523 1524
	}

	if (*mount_flags & MS_RDONLY) {
1525
		reiserfs_write_unlock(s);
1526 1527
		reiserfs_xattr_init(s, *mount_flags);
		/* remount read-only */
1528
		if (sb_rdonly(s))
1529
			/* it is read-only already */
1530
			goto out_ok_unlocked;
1531

1532 1533
		err = dquot_suspend(s, -1);
		if (err < 0)
1534 1535
			goto out_err;

1536 1537 1538
		/* try to remount file system with read-only permissions */
		if (sb_umount_state(rs) == REISERFS_VALID_FS
		    || REISERFS_SB(s)->s_mount_state != REISERFS_VALID_FS) {
1539
			goto out_ok_unlocked;
1540 1541
		}

1542 1543
		reiserfs_write_lock(s);

1544 1545
		err = journal_begin(&th, s, 10);
		if (err)
1546
			goto out_err_unlock;
1547 1548 1549 1550

		/* Mounting a rw partition read-only. */
		reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
		set_sb_umount_state(rs, REISERFS_SB(s)->s_mount_state);
1551
		journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
1552 1553
	} else {
		/* remount read-write */
1554
		if (!sb_rdonly(s)) {
1555
			reiserfs_write_unlock(s);
1556
			reiserfs_xattr_init(s, *mount_flags);
1557
			goto out_ok_unlocked;	/* We are read-write already */
1558 1559
		}

M
Miklos Szeredi 已提交
1560 1561
		if (reiserfs_is_journal_aborted(journal)) {
			err = journal->j_errno;
1562
			goto out_err_unlock;
M
Miklos Szeredi 已提交
1563
		}
1564 1565 1566 1567

		handle_data_mode(s, mount_options);
		handle_barrier_mode(s, mount_options);
		REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1568 1569 1570

		/* now it is safe to call journal_begin */
		s->s_flags &= ~MS_RDONLY;
1571 1572
		err = journal_begin(&th, s, 10);
		if (err)
1573
			goto out_err_unlock;
1574 1575 1576 1577 1578 1579

		/* Mount a partition which is read-only, read-write */
		reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
		REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
		s->s_flags &= ~MS_RDONLY;
		set_sb_umount_state(rs, REISERFS_ERROR_FS);
1580 1581
		if (!old_format_only(s))
			set_sb_mnt_count(rs, sb_mnt_count(rs) + 1);
1582
		/* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
1583
		journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
1584 1585 1586 1587
		REISERFS_SB(s)->s_mount_state = REISERFS_VALID_FS;
	}
	/* this will force a full flush of all journal lists */
	SB_JOURNAL(s)->j_must_wait = 1;
1588
	err = journal_end(&th);
1589
	if (err)
1590
		goto out_err_unlock;
1591

1592
	reiserfs_write_unlock(s);
1593
	if (!(*mount_flags & MS_RDONLY)) {
1594
		dquot_resume(s, -1);
1595
		reiserfs_write_lock(s);
1596
		finish_unfinished(s);
1597
		reiserfs_write_unlock(s);
1598 1599 1600
		reiserfs_xattr_init(s, *mount_flags);
	}

1601
out_ok_unlocked:
1602
	return 0;
M
Miklos Szeredi 已提交
1603

1604
out_err_unlock:
1605
	reiserfs_write_unlock(s);
M
Miklos Szeredi 已提交
1606 1607 1608
out_err:
	kfree(new_opts);
	return err;
L
Linus Torvalds 已提交
1609 1610
}

1611
static int read_super_block(struct super_block *s, int offset)
L
Linus Torvalds 已提交
1612
{
1613 1614 1615
	struct buffer_head *bh;
	struct reiserfs_super_block *rs;
	int fs_blocksize;
L
Linus Torvalds 已提交
1616

1617 1618
	bh = sb_bread(s, offset / s->s_blocksize);
	if (!bh) {
J
Jeff Mahoney 已提交
1619
		reiserfs_warning(s, "sh-2006",
1620
				 "bread failed (dev %s, block %lu, size %lu)",
A
Al Viro 已提交
1621
				 s->s_id, offset / s->s_blocksize,
1622 1623 1624
				 s->s_blocksize);
		return 1;
	}
L
Linus Torvalds 已提交
1625

1626 1627 1628 1629 1630
	rs = (struct reiserfs_super_block *)bh->b_data;
	if (!is_any_reiserfs_magic_string(rs)) {
		brelse(bh);
		return 1;
	}
1631 1632 1633
	/*
	 * ok, reiserfs signature (old or new) found in at the given offset
	 */
1634 1635 1636
	fs_blocksize = sb_blocksize(rs);
	brelse(bh);
	sb_set_blocksize(s, fs_blocksize);
L
Linus Torvalds 已提交
1637

1638 1639
	bh = sb_bread(s, offset / s->s_blocksize);
	if (!bh) {
J
Jeff Mahoney 已提交
1640 1641
		reiserfs_warning(s, "sh-2007",
				 "bread failed (dev %s, block %lu, size %lu)",
A
Al Viro 已提交
1642
				 s->s_id, offset / s->s_blocksize,
1643 1644 1645
				 s->s_blocksize);
		return 1;
	}
L
Linus Torvalds 已提交
1646

1647 1648
	rs = (struct reiserfs_super_block *)bh->b_data;
	if (sb_blocksize(rs) != s->s_blocksize) {
J
Jeff Mahoney 已提交
1649
		reiserfs_warning(s, "sh-2011", "can't find a reiserfs "
1650
				 "filesystem on (dev %s, block %llu, size %lu)",
A
Al Viro 已提交
1651
				 s->s_id,
1652 1653 1654 1655 1656
				 (unsigned long long)bh->b_blocknr,
				 s->s_blocksize);
		brelse(bh);
		return 1;
	}
L
Linus Torvalds 已提交
1657

1658 1659
	if (rs->s_v1.s_root_block == cpu_to_le32(-1)) {
		brelse(bh);
J
Jeff Mahoney 已提交
1660 1661 1662 1663
		reiserfs_warning(s, "super-6519", "Unfinished reiserfsck "
				 "--rebuild-tree run detected. Please run\n"
				 "reiserfsck --rebuild-tree and wait for a "
				 "completion. If that fails\n"
1664 1665
				 "get newer reiserfsprogs package");
		return 1;
L
Linus Torvalds 已提交
1666 1667
	}

1668 1669 1670
	SB_BUFFER_WITH_SB(s) = bh;
	SB_DISK_SUPER_BLOCK(s) = rs;

1671 1672 1673 1674
	/*
	 * magic is of non-standard journal filesystem, look at s_version to
	 * find which format is in use
	 */
1675 1676
	if (is_reiserfs_jr(rs)) {
		if (sb_version(rs) == REISERFS_VERSION_2)
1677 1678
			reiserfs_info(s, "found reiserfs format \"3.6\""
				      " with non-standard journal\n");
1679
		else if (sb_version(rs) == REISERFS_VERSION_1)
1680 1681
			reiserfs_info(s, "found reiserfs format \"3.5\""
				      " with non-standard journal\n");
1682
		else {
J
Jeff Mahoney 已提交
1683 1684 1685
			reiserfs_warning(s, "sh-2012", "found unknown "
					 "format \"%u\" of reiserfs with "
					 "non-standard magic", sb_version(rs));
1686 1687 1688
			return 1;
		}
	} else
1689 1690 1691 1692
		/*
		 * s_version of standard format may contain incorrect
		 * information, so we just look at the magic string
		 */
1693 1694 1695 1696 1697 1698
		reiserfs_info(s,
			      "found reiserfs format \"%s\" with standard journal\n",
			      is_reiserfs_3_5(rs) ? "3.5" : "3.6");

	s->s_op = &reiserfs_sops;
	s->s_export_op = &reiserfs_export_ops;
L
Linus Torvalds 已提交
1699
#ifdef CONFIG_QUOTA
1700 1701
	s->s_qcop = &reiserfs_qctl_operations;
	s->dq_op = &reiserfs_quota_operations;
1702
	s->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
L
Linus Torvalds 已提交
1703 1704
#endif

1705 1706 1707
	/*
	 * new format is limited by the 32 bit wide i_blocks field, want to
	 * be one full block below that.
1708 1709 1710
	 */
	s->s_maxbytes = (512LL << 32) - s->s_blocksize;
	return 0;
L
Linus Torvalds 已提交
1711 1712 1713
}

/* after journal replay, reread all bitmap and super blocks */
1714 1715
static int reread_meta_blocks(struct super_block *s)
{
1716
	ll_rw_block(REQ_OP_READ, 0, 1, &SB_BUFFER_WITH_SB(s));
1717 1718
	wait_on_buffer(SB_BUFFER_WITH_SB(s));
	if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
J
Jeff Mahoney 已提交
1719
		reiserfs_warning(s, "reiserfs-2504", "error reading the super");
1720 1721
		return 1;
	}
L
Linus Torvalds 已提交
1722

1723 1724
	return 0;
}
L
Linus Torvalds 已提交
1725

1726
/* hash detection stuff */
L
Linus Torvalds 已提交
1727

1728 1729 1730 1731 1732 1733 1734
/*
 * if root directory is empty - we set default - Yura's - hash and
 * warn about it
 * FIXME: we look for only one name in a directory. If tea and yura
 * both have the same value - we ask user to send report to the
 * mailing list
 */
1735
static __u32 find_hash_out(struct super_block *s)
L
Linus Torvalds 已提交
1736
{
1737 1738 1739 1740 1741
	int retval;
	struct inode *inode;
	struct cpu_key key;
	INITIALIZE_PATH(path);
	struct reiserfs_dir_entry de;
1742
	struct reiserfs_de_head *deh;
1743
	__u32 hash = DEFAULT_HASH;
1744
	__u32 deh_hashval, teahash, r5hash, yurahash;
1745

1746
	inode = d_inode(s->s_root);
1747

1748 1749 1750 1751 1752 1753 1754 1755
	make_cpu_key(&key, inode, ~0, TYPE_DIRENTRY, 3);
	retval = search_by_entry_key(s, &key, &path, &de);
	if (retval == IO_ERROR) {
		pathrelse(&path);
		return UNSET_HASH;
	}
	if (retval == NAME_NOT_FOUND)
		de.de_entry_num--;
1756

1757 1758 1759 1760 1761 1762
	set_de_name_and_namelen(&de);
	deh = de.de_deh + de.de_entry_num;

	if (deh_offset(deh) == DOT_DOT_OFFSET) {
		/* allow override in this case */
		if (reiserfs_rupasov_hash(s))
1763
			hash = YURA_HASH;
1764 1765 1766
		reiserfs_info(s, "FS seems to be empty, autodetect is using the default hash\n");
		goto out;
	}
1767

1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
	deh_hashval = GET_HASH_VALUE(deh_offset(deh));
	r5hash = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen));
	teahash = GET_HASH_VALUE(keyed_hash(de.de_name, de.de_namelen));
	yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen));

	if ((teahash == r5hash && deh_hashval == r5hash) ||
	    (teahash == yurahash && deh_hashval == yurahash) ||
	    (r5hash == yurahash && deh_hashval == yurahash)) {
		reiserfs_warning(s, "reiserfs-2506",
				 "Unable to automatically detect hash "
				 "function. Please mount with -o "
				 "hash={tea,rupasov,r5}");
		hash = UNSET_HASH;
		goto out;
	}

	if (deh_hashval == yurahash)
		hash = YURA_HASH;
	else if (deh_hashval == teahash)
		hash = TEA_HASH;
	else if (deh_hashval == r5hash)
		hash = R5_HASH;
	else {
		reiserfs_warning(s, "reiserfs-2506",
				 "Unrecognised hash function");
		hash = UNSET_HASH;
	}
out:
1796 1797
	pathrelse(&path);
	return hash;
L
Linus Torvalds 已提交
1798 1799
}

1800
/* finds out which hash names are sorted with */
1801
static int what_hash(struct super_block *s)
L
Linus Torvalds 已提交
1802
{
1803 1804 1805 1806
	__u32 code;

	code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));

1807 1808 1809 1810
	/*
	 * reiserfs_hash_detect() == true if any of the hash mount options
	 * were used.  We must check them to make sure the user isn't
	 * using a bad hash value
1811 1812 1813 1814 1815
	 */
	if (code == UNSET_HASH || reiserfs_hash_detect(s))
		code = find_hash_out(s);

	if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
1816 1817 1818
		/*
		 * detection has found the hash, and we must check against the
		 * mount options
1819 1820
		 */
		if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
J
Jeff Mahoney 已提交
1821 1822
			reiserfs_warning(s, "reiserfs-2507",
					 "Error, %s hash detected, "
1823 1824 1825 1826
					 "unable to force rupasov hash",
					 reiserfs_hashname(code));
			code = UNSET_HASH;
		} else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
J
Jeff Mahoney 已提交
1827 1828
			reiserfs_warning(s, "reiserfs-2508",
					 "Error, %s hash detected, "
1829 1830 1831 1832
					 "unable to force tea hash",
					 reiserfs_hashname(code));
			code = UNSET_HASH;
		} else if (reiserfs_r5_hash(s) && code != R5_HASH) {
J
Jeff Mahoney 已提交
1833 1834
			reiserfs_warning(s, "reiserfs-2509",
					 "Error, %s hash detected, "
1835 1836 1837 1838 1839
					 "unable to force r5 hash",
					 reiserfs_hashname(code));
			code = UNSET_HASH;
		}
	} else {
1840 1841 1842 1843
		/*
		 * find_hash_out was not called or
		 * could not determine the hash
		 */
1844 1845 1846 1847 1848 1849 1850 1851 1852
		if (reiserfs_rupasov_hash(s)) {
			code = YURA_HASH;
		} else if (reiserfs_tea_hash(s)) {
			code = TEA_HASH;
		} else if (reiserfs_r5_hash(s)) {
			code = R5_HASH;
		}
	}

1853 1854 1855
	/*
	 * if we are mounted RW, and we have a new valid hash code, update
	 * the super
1856 1857
	 */
	if (code != UNSET_HASH &&
1858
	    !sb_rdonly(s) &&
1859 1860 1861 1862
	    code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
		set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
	}
	return code;
L
Linus Torvalds 已提交
1863 1864
}

1865
/* return pointer to appropriate function */
1866
static hashf_t hash_function(struct super_block *s)
L
Linus Torvalds 已提交
1867
{
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879
	switch (what_hash(s)) {
	case TEA_HASH:
		reiserfs_info(s, "Using tea hash to sort names\n");
		return keyed_hash;
	case YURA_HASH:
		reiserfs_info(s, "Using rupasov hash to sort names\n");
		return yura_hash;
	case R5_HASH:
		reiserfs_info(s, "Using r5 hash to sort names\n");
		return r5_hash;
	}
	return NULL;
L
Linus Torvalds 已提交
1880 1881
}

1882
/* this is used to set up correct value for old partitions */
1883
static int function2code(hashf_t func)
L
Linus Torvalds 已提交
1884
{
1885 1886 1887 1888 1889 1890
	if (func == keyed_hash)
		return TEA_HASH;
	if (func == yura_hash)
		return YURA_HASH;
	if (func == r5_hash)
		return R5_HASH;
L
Linus Torvalds 已提交
1891

1892
	BUG();			/* should never happen */
L
Linus Torvalds 已提交
1893

1894
	return 0;
L
Linus Torvalds 已提交
1895 1896
}

J
Jeff Mahoney 已提交
1897
#define SWARN(silent, s, id, ...)			\
L
Linus Torvalds 已提交
1898
	if (!(silent))				\
J
Jeff Mahoney 已提交
1899
		reiserfs_warning(s, id, __VA_ARGS__)
L
Linus Torvalds 已提交
1900

1901
static int reiserfs_fill_super(struct super_block *s, void *data, int silent)
L
Linus Torvalds 已提交
1902
{
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
	struct inode *root_inode;
	struct reiserfs_transaction_handle th;
	int old_format = 0;
	unsigned long blocks;
	unsigned int commit_max_age = 0;
	int jinit_done = 0;
	struct reiserfs_iget_args args;
	struct reiserfs_super_block *rs;
	char *jdev_name;
	struct reiserfs_sb_info *sbi;
	int errval = -EINVAL;
J
Jan Kara 已提交
1914
	char *qf_names[REISERFS_MAXQUOTAS] = {};
1915
	unsigned int qfmt = 0;
1916

1917
	sbi = kzalloc(sizeof(struct reiserfs_sb_info), GFP_KERNEL);
1918 1919
	if (!sbi)
		return -ENOMEM;
1920 1921
	s->s_fs_info = sbi;
	/* Set default values for options: non-aggressive tails, RO on errors */
1922 1923 1924
	sbi->s_mount_opt |= (1 << REISERFS_SMALLTAIL);
	sbi->s_mount_opt |= (1 << REISERFS_ERROR_RO);
	sbi->s_mount_opt |= (1 << REISERFS_BARRIER_FLUSH);
1925
	/* no preallocation minimum, be smart in reiserfs_file_write instead */
1926
	sbi->s_alloc_options.preallocmin = 0;
1927
	/* Preallocate by 16 blocks (17-1) at once */
1928
	sbi->s_alloc_options.preallocsize = 17;
1929 1930 1931
	/* setup default block allocator options */
	reiserfs_init_alloc_options(s);

1932 1933
	spin_lock_init(&sbi->old_work_lock);
	INIT_DELAYED_WORK(&sbi->old_work, flush_old_commits);
1934 1935
	mutex_init(&sbi->lock);
	sbi->lock_depth = -1;
F
Frederic Weisbecker 已提交
1936

1937 1938 1939 1940 1941 1942 1943 1944
	sbi->commit_wq = alloc_workqueue("reiserfs/%s", WQ_MEM_RECLAIM, 0,
					 s->s_id);
	if (!sbi->commit_wq) {
		SWARN(silent, s, "", "Cannot allocate commit workqueue");
		errval = -ENOMEM;
		goto error_unlocked;
	}

1945 1946
	jdev_name = NULL;
	if (reiserfs_parse_options
1947
	    (s, (char *)data, &sbi->s_mount_opt, &blocks, &jdev_name,
1948
	     &commit_max_age, qf_names, &qfmt) == 0) {
1949
		goto error_unlocked;
1950
	}
1951
	if (jdev_name && jdev_name[0]) {
1952 1953
		sbi->s_jdev = kstrdup(jdev_name, GFP_KERNEL);
		if (!sbi->s_jdev) {
1954 1955 1956 1957 1958
			SWARN(silent, s, "", "Cannot allocate memory for "
				"journal device name");
			goto error;
		}
	}
1959 1960 1961
#ifdef CONFIG_QUOTA
	handle_quota_files(s, qf_names, &qfmt);
#endif
1962 1963

	if (blocks) {
J
Jeff Mahoney 已提交
1964
		SWARN(silent, s, "jmacd-7", "resize option for remount only");
1965
		goto error_unlocked;
1966 1967
	}

1968 1969 1970 1971
	/*
	 * try old format (undistributed bitmap, super block in 8-th 1k
	 * block of a device)
	 */
1972 1973
	if (!read_super_block(s, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
		old_format = 1;
1974 1975 1976 1977 1978

	/*
	 * try new format (64-th 1k block), which can contain reiserfs
	 * super block
	 */
1979
	else if (read_super_block(s, REISERFS_DISK_OFFSET_IN_BYTES)) {
J
Jeff Mahoney 已提交
1980
		SWARN(silent, s, "sh-2021", "can not find reiserfs on %s",
A
Al Viro 已提交
1981
		      s->s_id);
1982
		goto error_unlocked;
1983 1984 1985
	}

	rs = SB_DISK_SUPER_BLOCK(s);
1986 1987 1988 1989 1990
	/*
	 * Let's do basic sanity check to verify that underlying device is not
	 * smaller than the filesystem. If the check fails then abort and
	 * scream, because bad stuff will happen otherwise.
	 */
1991 1992 1993
	if (s->s_bdev && s->s_bdev->bd_inode
	    && i_size_read(s->s_bdev->bd_inode) <
	    sb_block_count(rs) * sb_blocksize(rs)) {
J
Jeff Mahoney 已提交
1994 1995 1996 1997 1998 1999
		SWARN(silent, s, "", "Filesystem cannot be "
		      "mounted because it is bigger than the device");
		SWARN(silent, s, "", "You may need to run fsck "
		      "or increase size of your LVM partition");
		SWARN(silent, s, "", "Or may be you forgot to "
		      "reboot after fdisk when it told you to");
2000
		goto error_unlocked;
2001 2002 2003 2004 2005
	}

	sbi->s_mount_state = SB_REISERFS_STATE(s);
	sbi->s_mount_state = REISERFS_VALID_FS;

2006
	if ((errval = reiserfs_init_bitmap_cache(s))) {
J
Jeff Mahoney 已提交
2007
		SWARN(silent, s, "jmacd-8", "unable to read bitmap");
2008
		goto error_unlocked;
2009
	}
2010

2011
	errval = -EINVAL;
L
Linus Torvalds 已提交
2012
#ifdef CONFIG_REISERFS_CHECK
J
Jeff Mahoney 已提交
2013 2014
	SWARN(silent, s, "", "CONFIG_REISERFS_CHECK is set ON");
	SWARN(silent, s, "", "- it is slow mode for debugging.");
L
Linus Torvalds 已提交
2015 2016
#endif

2017 2018 2019
	/* make data=ordered the default */
	if (!reiserfs_data_log(s) && !reiserfs_data_ordered(s) &&
	    !reiserfs_data_writeback(s)) {
2020
		sbi->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032
	}

	if (reiserfs_data_log(s)) {
		reiserfs_info(s, "using journaled data mode\n");
	} else if (reiserfs_data_ordered(s)) {
		reiserfs_info(s, "using ordered data mode\n");
	} else {
		reiserfs_info(s, "using writeback data mode\n");
	}
	if (reiserfs_barrier_flush(s)) {
		printk("reiserfs: using flush barriers\n");
	}
2033

2034 2035 2036 2037 2038
	if (journal_init(s, jdev_name, old_format, commit_max_age)) {
		SWARN(silent, s, "sh-2022",
		      "unable to initialize journal space");
		goto error_unlocked;
	} else {
2039 2040 2041 2042 2043
		/*
		 * once this is set, journal_release must be called
		 * if we error out of the mount
		 */
		jinit_done = 1;
2044 2045
	}

2046
	if (reread_meta_blocks(s)) {
J
Jeff Mahoney 已提交
2047 2048
		SWARN(silent, s, "jmacd-9",
		      "unable to reread meta blocks after journal init");
2049
		goto error_unlocked;
2050 2051 2052
	}

	if (replay_only(s))
2053
		goto error_unlocked;
2054

2055
	if (bdev_read_only(s->s_bdev) && !sb_rdonly(s)) {
J
Jeff Mahoney 已提交
2056 2057
		SWARN(silent, s, "clm-7000",
		      "Detected readonly device, marking FS readonly");
2058 2059 2060 2061 2062 2063
		s->s_flags |= MS_RDONLY;
	}
	args.objectid = REISERFS_ROOT_OBJECTID;
	args.dirid = REISERFS_ROOT_PARENT_OBJECTID;
	root_inode =
	    iget5_locked(s, REISERFS_ROOT_OBJECTID, reiserfs_find_actor,
2064
			 reiserfs_init_locked_inode, (void *)&args);
2065
	if (!root_inode) {
J
Jeff Mahoney 已提交
2066
		SWARN(silent, s, "jmacd-10", "get root inode failed");
2067
		goto error_unlocked;
2068 2069
	}

2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
	/*
	 * This path assumed to be called with the BKL in the old times.
	 * Now we have inherited the big reiserfs lock from it and many
	 * reiserfs helpers called in the mount path and elsewhere require
	 * this lock to be held even if it's not always necessary. Let's be
	 * conservative and hold it early. The window can be reduced after
	 * careful review of the code.
	 */
	reiserfs_write_lock(s);

2080 2081 2082 2083 2084
	if (root_inode->i_state & I_NEW) {
		reiserfs_read_locked_inode(root_inode, &args);
		unlock_new_inode(root_inode);
	}

2085 2086
	s->s_root = d_make_root(root_inode);
	if (!s->s_root)
2087
		goto error;
2088
	/* define and initialize hash function */
2089 2090 2091 2092 2093 2094 2095 2096 2097
	sbi->s_hash_function = hash_function(s);
	if (sbi->s_hash_function == NULL) {
		dput(s->s_root);
		s->s_root = NULL;
		goto error;
	}

	if (is_reiserfs_3_5(rs)
	    || (is_reiserfs_jr(rs) && SB_VERSION(s) == REISERFS_VERSION_1))
2098
		set_bit(REISERFS_3_5, &sbi->s_properties);
2099
	else if (old_format)
2100
		set_bit(REISERFS_OLD_FORMAT, &sbi->s_properties);
2101
	else
2102
		set_bit(REISERFS_3_6, &sbi->s_properties);
2103

2104
	if (!sb_rdonly(s)) {
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116

		errval = journal_begin(&th, s, 1);
		if (errval) {
			dput(s->s_root);
			s->s_root = NULL;
			goto error;
		}
		reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);

		set_sb_umount_state(rs, REISERFS_ERROR_FS);
		set_sb_fs_state(rs, 0);

2117 2118
		/*
		 * Clear out s_bmap_nr if it would wrap. We can handle this
2119 2120
		 * case, but older revisions can't. This will cause the
		 * file system to fail mount on those older implementations,
2121 2122
		 * avoiding corruption. -jeffm
		 */
2123 2124
		if (bmap_would_wrap(reiserfs_bmap_count(s)) &&
		    sb_bmap_nr(rs) != 0) {
J
Jeff Mahoney 已提交
2125
			reiserfs_warning(s, "super-2030", "This file system "
2126 2127 2128 2129 2130 2131 2132 2133
					"claims to use %u bitmap blocks in "
					"its super block, but requires %u. "
					"Clearing to zero.", sb_bmap_nr(rs),
					reiserfs_bmap_count(s));

			set_sb_bmap_nr(rs, 0);
		}

2134
		if (old_format_only(s)) {
2135 2136 2137 2138
			/*
			 * filesystem of format 3.5 either with standard
			 * or non-standard journal
			 */
2139 2140 2141 2142 2143 2144 2145
			if (convert_reiserfs(s)) {
				/* and -o conv is given */
				if (!silent)
					reiserfs_info(s,
						      "converting 3.5 filesystem to the 3.6 format");

				if (is_reiserfs_3_5(rs))
2146 2147 2148 2149 2150
					/*
					 * put magic string of 3.6 format.
					 * 2.2 will not be able to
					 * mount this filesystem anymore
					 */
2151 2152 2153 2154 2155 2156 2157
					memcpy(rs->s_v1.s_magic,
					       reiserfs_3_6_magic_string,
					       sizeof
					       (reiserfs_3_6_magic_string));

				set_sb_version(rs, REISERFS_VERSION_2);
				reiserfs_convert_objectid_map_v1(s);
2158 2159
				set_bit(REISERFS_3_6, &sbi->s_properties);
				clear_bit(REISERFS_3_5, &sbi->s_properties);
2160 2161 2162
			} else if (!silent) {
				reiserfs_info(s, "using 3.5.x disk format\n");
			}
2163 2164 2165
		} else
			set_sb_mnt_count(rs, sb_mnt_count(rs) + 1);

2166

2167
		journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
2168
		errval = journal_end(&th);
2169 2170 2171 2172 2173 2174
		if (errval) {
			dput(s->s_root);
			s->s_root = NULL;
			goto error;
		}

2175
		reiserfs_write_unlock(s);
2176 2177
		if ((errval = reiserfs_lookup_privroot(s)) ||
		    (errval = reiserfs_xattr_init(s, s->s_flags))) {
2178 2179
			dput(s->s_root);
			s->s_root = NULL;
2180
			goto error_unlocked;
2181
		}
2182
		reiserfs_write_lock(s);
2183

2184 2185 2186
		/*
		 * look for files which were to be removed in previous session
		 */
2187 2188 2189 2190 2191 2192
		finish_unfinished(s);
	} else {
		if (old_format_only(s) && !silent) {
			reiserfs_info(s, "using 3.5.x disk format\n");
		}

2193
		reiserfs_write_unlock(s);
2194 2195
		if ((errval = reiserfs_lookup_privroot(s)) ||
		    (errval = reiserfs_xattr_init(s, s->s_flags))) {
2196 2197
			dput(s->s_root);
			s->s_root = NULL;
2198
			goto error_unlocked;
2199
		}
2200
		reiserfs_write_lock(s);
2201
	}
2202 2203 2204
	/*
	 * mark hash in super block: it could be unset. overwrite should be ok
	 */
2205 2206 2207 2208 2209 2210 2211 2212 2213
	set_sb_hash_function_code(rs, function2code(sbi->s_hash_function));

	handle_attrs(s);

	reiserfs_proc_info_init(s);

	init_waitqueue_head(&(sbi->s_wait));
	spin_lock_init(&sbi->bitmap_lock);

F
Frederic Weisbecker 已提交
2214 2215
	reiserfs_write_unlock(s);

2216 2217
	return (0);

2218
error:
2219 2220 2221 2222 2223 2224
	reiserfs_write_unlock(s);

error_unlocked:
	/* kill the commit thread, free journal ram */
	if (jinit_done) {
		reiserfs_write_lock(s);
2225
		journal_release_error(NULL, s);
2226
		reiserfs_write_unlock(s);
2227
	}
2228

2229 2230 2231
	if (sbi->commit_wq)
		destroy_workqueue(sbi->commit_wq);

2232
	reiserfs_cancel_old_flush(s);
2233

2234
	reiserfs_free_bitmap_cache(s);
2235 2236
	if (SB_BUFFER_WITH_SB(s))
		brelse(SB_BUFFER_WITH_SB(s));
L
Linus Torvalds 已提交
2237
#ifdef CONFIG_QUOTA
2238 2239
	{
		int j;
J
Jan Kara 已提交
2240
		for (j = 0; j < REISERFS_MAXQUOTAS; j++)
2241
			kfree(qf_names[j]);
2242
	}
L
Linus Torvalds 已提交
2243
#endif
2244
	kfree(sbi);
L
Linus Torvalds 已提交
2245

2246 2247
	s->s_fs_info = NULL;
	return errval;
L
Linus Torvalds 已提交
2248 2249
}

2250
static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf)
L
Linus Torvalds 已提交
2251
{
2252
	struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(dentry->d_sb);
2253 2254 2255 2256 2257

	buf->f_namelen = (REISERFS_MAX_NAME(s->s_blocksize));
	buf->f_bfree = sb_free_blocks(rs);
	buf->f_bavail = buf->f_bfree;
	buf->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
2258
	buf->f_bsize = dentry->d_sb->s_blocksize;
2259 2260
	/* changed to accommodate gcc folks. */
	buf->f_type = REISERFS_SUPER_MAGIC;
2261 2262 2263 2264
	buf->f_fsid.val[0] = (u32)crc32_le(0, rs->s_uuid, sizeof(rs->s_uuid)/2);
	buf->f_fsid.val[1] = (u32)crc32_le(0, rs->s_uuid + sizeof(rs->s_uuid)/2,
				sizeof(rs->s_uuid)/2);

2265
	return 0;
L
Linus Torvalds 已提交
2266 2267 2268 2269 2270
}

#ifdef CONFIG_QUOTA
static int reiserfs_write_dquot(struct dquot *dquot)
{
2271 2272
	struct reiserfs_transaction_handle th;
	int ret, err;
2273
	int depth;
2274 2275 2276 2277 2278 2279 2280

	reiserfs_write_lock(dquot->dq_sb);
	ret =
	    journal_begin(&th, dquot->dq_sb,
			  REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
	if (ret)
		goto out;
2281
	depth = reiserfs_write_unlock_nested(dquot->dq_sb);
2282
	ret = dquot_commit(dquot);
2283
	reiserfs_write_lock_nested(dquot->dq_sb, depth);
2284
	err = journal_end(&th);
2285 2286
	if (!ret && err)
		ret = err;
2287
out:
2288 2289
	reiserfs_write_unlock(dquot->dq_sb);
	return ret;
L
Linus Torvalds 已提交
2290 2291 2292 2293
}

static int reiserfs_acquire_dquot(struct dquot *dquot)
{
2294 2295
	struct reiserfs_transaction_handle th;
	int ret, err;
2296
	int depth;
2297 2298 2299 2300 2301 2302 2303

	reiserfs_write_lock(dquot->dq_sb);
	ret =
	    journal_begin(&th, dquot->dq_sb,
			  REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
	if (ret)
		goto out;
2304
	depth = reiserfs_write_unlock_nested(dquot->dq_sb);
2305
	ret = dquot_acquire(dquot);
2306
	reiserfs_write_lock_nested(dquot->dq_sb, depth);
2307
	err = journal_end(&th);
2308 2309
	if (!ret && err)
		ret = err;
2310
out:
2311 2312
	reiserfs_write_unlock(dquot->dq_sb);
	return ret;
L
Linus Torvalds 已提交
2313 2314 2315 2316
}

static int reiserfs_release_dquot(struct dquot *dquot)
{
2317 2318 2319 2320 2321 2322 2323
	struct reiserfs_transaction_handle th;
	int ret, err;

	reiserfs_write_lock(dquot->dq_sb);
	ret =
	    journal_begin(&th, dquot->dq_sb,
			  REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
2324
	reiserfs_write_unlock(dquot->dq_sb);
J
Jan Kara 已提交
2325 2326 2327
	if (ret) {
		/* Release dquot anyway to avoid endless cycle in dqput() */
		dquot_release(dquot);
2328
		goto out;
J
Jan Kara 已提交
2329
	}
2330
	ret = dquot_release(dquot);
2331
	reiserfs_write_lock(dquot->dq_sb);
2332
	err = journal_end(&th);
2333 2334 2335
	if (!ret && err)
		ret = err;
	reiserfs_write_unlock(dquot->dq_sb);
2336
out:
2337
	return ret;
L
Linus Torvalds 已提交
2338 2339 2340 2341
}

static int reiserfs_mark_dquot_dirty(struct dquot *dquot)
{
2342
	/* Are we journaling quotas? */
2343 2344 2345 2346 2347 2348
	if (REISERFS_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
	    REISERFS_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
		dquot_mark_dquot_dirty(dquot);
		return reiserfs_write_dquot(dquot);
	} else
		return dquot_mark_dquot_dirty(dquot);
L
Linus Torvalds 已提交
2349 2350 2351 2352
}

static int reiserfs_write_info(struct super_block *sb, int type)
{
2353 2354
	struct reiserfs_transaction_handle th;
	int ret, err;
2355
	int depth;
2356 2357 2358 2359 2360 2361

	/* Data block + inode block */
	reiserfs_write_lock(sb);
	ret = journal_begin(&th, sb, 2);
	if (ret)
		goto out;
2362
	depth = reiserfs_write_unlock_nested(sb);
2363
	ret = dquot_commit_info(sb, type);
2364
	reiserfs_write_lock_nested(sb, depth);
2365
	err = journal_end(&th);
2366 2367
	if (!ret && err)
		ret = err;
2368
out:
2369 2370
	reiserfs_write_unlock(sb);
	return ret;
L
Linus Torvalds 已提交
2371 2372 2373
}

/*
2374
 * Turn on quotas during mount time - we need to find the quota file and such...
L
Linus Torvalds 已提交
2375 2376 2377
 */
static int reiserfs_quota_on_mount(struct super_block *sb, int type)
{
2378 2379
	return dquot_quota_on_mount(sb, REISERFS_SB(sb)->s_qf_names[type],
					REISERFS_SB(sb)->s_jquota_fmt, type);
L
Linus Torvalds 已提交
2380 2381 2382 2383 2384
}

/*
 * Standard function to be called on quota_on
 */
2385
static int reiserfs_quota_on(struct super_block *sb, int type, int format_id,
A
Al Viro 已提交
2386
			     const struct path *path)
L
Linus Torvalds 已提交
2387
{
2388
	int err;
J
Jan Kara 已提交
2389
	struct inode *inode;
2390
	struct reiserfs_transaction_handle th;
2391
	int opt = type == USRQUOTA ? REISERFS_USRQUOTA : REISERFS_GRPQUOTA;
2392

2393 2394 2395 2396 2397
	reiserfs_write_lock(sb);
	if (!(REISERFS_SB(sb)->s_mount_opt & (1 << opt))) {
		err = -EINVAL;
		goto out;
	}
2398

2399
	/* Quotafile not on the same filesystem? */
2400
	if (path->dentry->d_sb != sb) {
2401 2402
		err = -EXDEV;
		goto out;
2403
	}
2404
	inode = d_inode(path->dentry);
2405 2406 2407 2408
	/*
	 * We must not pack tails for quota files on reiserfs for quota
	 * IO to work
	 */
J
Jan Kara 已提交
2409 2410 2411
	if (!(REISERFS_I(inode)->i_flags & i_nopack_mask)) {
		err = reiserfs_unpack(inode, NULL);
		if (err) {
J
Jeff Mahoney 已提交
2412 2413
			reiserfs_warning(sb, "super-6520",
				"Unpacking tail of quota file failed"
J
Jan Kara 已提交
2414
				" (%d). Cannot turn on quotas.", err);
2415 2416
			err = -EINVAL;
			goto out;
J
Jan Kara 已提交
2417 2418
		}
		mark_inode_dirty(inode);
2419
	}
2420 2421 2422
	/* Journaling quota? */
	if (REISERFS_SB(sb)->s_qf_names[type]) {
		/* Quotafile not of fs root? */
2423
		if (path->dentry->d_parent != sb->s_root)
J
Jeff Mahoney 已提交
2424 2425
			reiserfs_warning(sb, "super-6521",
				 "Quota file not on filesystem root. "
2426
				 "Journalled quota will not work.");
2427 2428 2429 2430 2431 2432 2433 2434 2435 2436
	}

	/*
	 * When we journal data on quota file, we have to flush journal to see
	 * all updates to the file when we bypass pagecache...
	 */
	if (reiserfs_file_data_log(inode)) {
		/* Just start temporary transaction and finish it */
		err = journal_begin(&th, sb, 1);
		if (err)
2437
			goto out;
2438
		err = journal_end_sync(&th);
2439
		if (err)
2440
			goto out;
2441
	}
2442
	reiserfs_write_unlock(sb);
2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453
	err = dquot_quota_on(sb, type, format_id, path);
	if (!err) {
		inode_lock(inode);
		REISERFS_I(inode)->i_attrs |= REISERFS_IMMUTABLE_FL |
					      REISERFS_NOATIME_FL;
		inode_set_flags(inode, S_IMMUTABLE | S_NOATIME,
				S_IMMUTABLE | S_NOATIME);
		inode_unlock(inode);
		mark_inode_dirty(inode);
	}
	return err;
2454
out:
2455
	reiserfs_write_unlock(sb);
2456
	return err;
L
Linus Torvalds 已提交
2457 2458
}

2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483
static int reiserfs_quota_off(struct super_block *sb, int type)
{
	int err;
	struct inode *inode = sb_dqopt(sb)->files[type];

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

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

	inode_lock(inode);
	REISERFS_I(inode)->i_attrs &= ~(REISERFS_IMMUTABLE_FL |
					REISERFS_NOATIME_FL);
	inode_set_flags(inode, 0, S_IMMUTABLE | S_NOATIME);
	inode_unlock(inode);
	mark_inode_dirty(inode);
out_put:
	iput(inode);
	return err;
out:
	return dquot_quota_off(sb, type);
}

2484 2485
/*
 * Read data from quotafile - avoid pagecache and such because we cannot afford
L
Linus Torvalds 已提交
2486
 * acquiring the locks... As quota files are never truncated and quota code
L
Lucas De Marchi 已提交
2487
 * itself serializes the operations (and no one else should touch the files)
2488 2489
 * we don't have to be afraid of races
 */
L
Linus Torvalds 已提交
2490 2491 2492
static ssize_t reiserfs_quota_read(struct super_block *sb, int type, char *data,
				   size_t len, loff_t off)
{
2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
	struct inode *inode = sb_dqopt(sb)->files[type];
	unsigned long blk = off >> sb->s_blocksize_bits;
	int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
	size_t toread;
	struct buffer_head tmp_bh, *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;
2510 2511 2512 2513
		/*
		 * Quota files are without tails so we can safely
		 * use this function
		 */
2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533
		reiserfs_write_lock(sb);
		err = reiserfs_get_block(inode, blk, &tmp_bh, 0);
		reiserfs_write_unlock(sb);
		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;
L
Linus Torvalds 已提交
2534 2535
}

2536 2537 2538 2539
/*
 * Write to quotafile (we know the transaction is already started and has
 * enough credits)
 */
L
Linus Torvalds 已提交
2540 2541 2542
static ssize_t reiserfs_quota_write(struct super_block *sb, int type,
				    const char *data, size_t len, loff_t off)
{
2543 2544 2545 2546 2547 2548 2549
	struct inode *inode = sb_dqopt(sb)->files[type];
	unsigned long blk = off >> sb->s_blocksize_bits;
	int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
	int journal_quota = REISERFS_SB(sb)->s_qf_names[type] != NULL;
	size_t towrite = len;
	struct buffer_head tmp_bh, *bh;

J
Jan Kara 已提交
2550
	if (!current->journal_info) {
2551
		printk(KERN_WARNING "reiserfs: Quota write (off=%llu, len=%llu) cancelled because transaction is not started.\n",
J
Jan Kara 已提交
2552 2553 2554
			(unsigned long long)off, (unsigned long long)len);
		return -EIO;
	}
2555 2556 2557 2558
	while (towrite > 0) {
		tocopy = sb->s_blocksize - offset < towrite ?
		    sb->s_blocksize - offset : towrite;
		tmp_bh.b_state = 0;
2559
		reiserfs_write_lock(sb);
2560
		err = reiserfs_get_block(inode, blk, &tmp_bh, GET_BLOCK_CREATE);
2561
		reiserfs_write_unlock(sb);
2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576
		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);
		unlock_buffer(bh);
2577
		reiserfs_write_lock(sb);
2578
		reiserfs_prepare_for_journal(sb, bh, 1);
2579
		journal_mark_dirty(current->journal_info, bh);
2580 2581
		if (!journal_quota)
			reiserfs_add_ordered_list(inode, bh);
2582
		reiserfs_write_unlock(sb);
2583 2584 2585 2586 2587 2588
		brelse(bh);
		offset = 0;
		towrite -= tocopy;
		data += tocopy;
		blk++;
	}
J
Jan Kara 已提交
2589
out:
2590
	if (len == towrite)
2591 2592 2593
		return err;
	if (inode->i_size < off + len - towrite)
		i_size_write(inode, off + len - towrite);
2594
	inode->i_mtime = inode->i_ctime = current_time(inode);
2595 2596
	mark_inode_dirty(inode);
	return len - towrite;
L
Linus Torvalds 已提交
2597 2598 2599 2600
}

#endif

A
Al Viro 已提交
2601
static struct dentry *get_super_block(struct file_system_type *fs_type,
2602
			   int flags, const char *dev_name,
A
Al Viro 已提交
2603
			   void *data)
L
Linus Torvalds 已提交
2604
{
A
Al Viro 已提交
2605
	return mount_bdev(fs_type, flags, dev_name, data, reiserfs_fill_super);
L
Linus Torvalds 已提交
2606 2607
}

2608
static int __init init_reiserfs_fs(void)
L
Linus Torvalds 已提交
2609 2610 2611
{
	int ret;

2612 2613
	ret = init_inodecache();
	if (ret)
L
Linus Torvalds 已提交
2614 2615
		return ret;

2616
	reiserfs_proc_info_global_init();
L
Linus Torvalds 已提交
2617

2618
	ret = register_filesystem(&reiserfs_fs_type);
2619 2620
	if (ret)
		goto out;
L
Linus Torvalds 已提交
2621

2622 2623
	return 0;
out:
2624 2625
	reiserfs_proc_info_global_done();
	destroy_inodecache();
L
Linus Torvalds 已提交
2626 2627 2628 2629

	return ret;
}

2630
static void __exit exit_reiserfs_fs(void)
L
Linus Torvalds 已提交
2631
{
2632 2633 2634
	reiserfs_proc_info_global_done();
	unregister_filesystem(&reiserfs_fs_type);
	destroy_inodecache();
L
Linus Torvalds 已提交
2635 2636 2637
}

struct file_system_type reiserfs_fs_type = {
2638 2639
	.owner = THIS_MODULE,
	.name = "reiserfs",
A
Al Viro 已提交
2640
	.mount = get_super_block,
2641
	.kill_sb = reiserfs_kill_sb,
2642
	.fs_flags = FS_REQUIRES_DEV,
L
Linus Torvalds 已提交
2643
};
2644
MODULE_ALIAS_FS("reiserfs");
L
Linus Torvalds 已提交
2645

2646 2647 2648
MODULE_DESCRIPTION("ReiserFS journaled filesystem");
MODULE_AUTHOR("Hans Reiser <reiser@namesys.com>");
MODULE_LICENSE("GPL");
L
Linus Torvalds 已提交
2649

2650 2651
module_init(init_reiserfs_fs);
module_exit(exit_reiserfs_fs);