super.c 62.2 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 18 19 20 21 22 23 24
#include <linux/vmalloc.h>
#include <linux/time.h>
#include <asm/uaccess.h>
#include <linux/reiserfs_fs.h>
#include <linux/reiserfs_acl.h>
#include <linux/reiserfs_xattr.h>
#include <linux/init.h>
#include <linux/blkdev.h>
#include <linux/buffer_head.h>
25
#include <linux/exportfs.h>
26
#include <linux/quotaops.h>
L
Linus Torvalds 已提交
27 28 29
#include <linux/vfs.h>
#include <linux/mount.h>
#include <linux/namei.h>
30
#include <linux/crc32.h>
31
#include <linux/smp_lock.h>
L
Linus Torvalds 已提交
32 33 34 35 36 37 38

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;

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

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

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

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

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

66
static int reiserfs_sync_fs(struct super_block *s, int wait)
L
Linus Torvalds 已提交
67
{
68 69 70 71 72 73 74 75 76
	struct reiserfs_transaction_handle th;

	reiserfs_write_lock(s);
	if (!journal_begin(&th, s, 1))
		if (!journal_end_sync(&th, s, 1))
			reiserfs_flush_old_commits(s);
	s->s_dirt = 0;	/* Even if it's not true.
			 * We'll loop forever in sync_supers otherwise */
	reiserfs_write_unlock(s);
77
	return 0;
L
Linus Torvalds 已提交
78 79 80 81
}

static void reiserfs_write_super(struct super_block *s)
{
82
	reiserfs_sync_fs(s, 1);
L
Linus Torvalds 已提交
83 84
}

85
static int reiserfs_freeze(struct super_block *s)
L
Linus Torvalds 已提交
86
{
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	struct reiserfs_transaction_handle th;
	reiserfs_write_lock(s);
	if (!(s->s_flags & MS_RDONLY)) {
		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);
			journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
			reiserfs_block_writes(&th);
			journal_end_sync(&th, s, 1);
		}
	}
	s->s_dirt = 0;
	reiserfs_write_unlock(s);
103
	return 0;
L
Linus Torvalds 已提交
104 105
}

106
static int reiserfs_unfreeze(struct super_block *s)
107 108
{
	reiserfs_allow_writes(s);
109
	return 0;
L
Linus Torvalds 已提交
110 111
}

112
extern const struct in_core_key MAX_IN_CORE_KEY;
L
Linus Torvalds 已提交
113 114 115 116 117 118 119 120

/* 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 */
121 122
static int remove_save_link_only(struct super_block *s,
				 struct reiserfs_key *key, int oid_free)
L
Linus Torvalds 已提交
123
{
124 125 126 127 128 129 130 131 132 133 134 135 136 137
	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));

	return journal_end(&th, s, JOURNAL_PER_BALANCE_CNT);
L
Linus Torvalds 已提交
138
}
139

L
Linus Torvalds 已提交
140 141 142
#ifdef CONFIG_QUOTA
static int reiserfs_quota_on_mount(struct super_block *, int);
#endif
143

L
Linus Torvalds 已提交
144
/* look for uncompleted unlinks and truncates and complete them */
145
static int finish_unfinished(struct super_block *s)
L
Linus Torvalds 已提交
146
{
147 148
	INITIALIZE_PATH(path);
	struct cpu_key max_cpu_key, obj_key;
149
	struct reiserfs_key save_link_key, last_inode_key;
150 151 152 153 154 155 156 157
	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 已提交
158
#ifdef CONFIG_QUOTA
159 160
	int i;
	int ms_active_set;
161
	int quota_enabled[MAXQUOTAS];
L
Linus Torvalds 已提交
162
#endif
163 164 165 166 167 168 169

	/* 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 已提交
170

171 172
	memset(&last_inode_key, 0, sizeof(last_inode_key));

L
Linus Torvalds 已提交
173
#ifdef CONFIG_QUOTA
174 175 176 177 178 179 180 181 182
	/* 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 */
	for (i = 0; i < MAXQUOTAS; i++) {
183
		quota_enabled[i] = 1;
184
		if (REISERFS_SB(s)->s_qf_names[i]) {
185 186 187 188 189 190 191
			int ret;

			if (sb_has_quota_active(s, i)) {
				quota_enabled[i] = 0;
				continue;
			}
			ret = reiserfs_quota_on_mount(s, i);
192
			if (ret < 0)
J
Jeff Mahoney 已提交
193 194 195
				reiserfs_warning(s, "reiserfs-2500",
						 "cannot turn on journaled "
						 "quota: error %d", ret);
196 197
		}
	}
L
Linus Torvalds 已提交
198
#endif
199 200 201 202 203 204

	done = 0;
	REISERFS_SB(s)->s_is_unlinked_ok = 1;
	while (!retval) {
		retval = search_item(s, &max_cpu_key, &path);
		if (retval != ITEM_NOT_FOUND) {
J
Jeff Mahoney 已提交
205 206
			reiserfs_error(s, "vs-2140",
				       "search_by_key returned %d", retval);
207 208 209 210 211 212
			break;
		}

		bh = get_last_bh(&path);
		item_pos = get_item_pos(&path);
		if (item_pos != B_NR_ITEMS(bh)) {
J
Jeff Mahoney 已提交
213 214
			reiserfs_warning(s, "vs-2060",
					 "wrong position found");
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
			break;
		}
		item_pos--;
		ih = B_N_PITEM_HEAD(bh, item_pos);

		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 */
		item = B_I_PITEM(bh, ih);
		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);
		if (!inode) {
			/* the unlink almost completed, it just did not manage to remove
			   "save" link and release objectid */
J
Jeff Mahoney 已提交
244
			reiserfs_warning(s, "vs-2180", "iget failed for %K",
245 246 247 248 249 250 251
					 &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 已提交
252 253
			reiserfs_warning(s, "vs-2185",
					 "file %K is not unlinked",
254 255 256 257
					 &obj_key);
			retval = remove_save_link_only(s, &save_link_key, 0);
			continue;
		}
258
		dquot_initialize(inode);
259 260 261 262 263 264

		if (truncate && S_ISDIR(inode->i_mode)) {
			/* 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 已提交
265 266 267
			reiserfs_warning(s, "green-2101",
					 "impossible truncate on a "
					 "directory %k. Please report",
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
					 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;
			/* not completed truncate found. New size was committed together
			   with "save" link */
			reiserfs_info(s, "Truncating %k to %Ld ..",
				      INODE_PKEY(inode), inode->i_size);
			reiserfs_truncate_file(inode,
					       0
					       /*don't update modification time */
					       );
			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));
291 292 293 294 295 296
			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 已提交
297 298 299 300
				reiserfs_warning(s, "super-2189", "Dead loop "
						 "in finish_unfinished "
						 "detected, just remove "
						 "save link\n");
301 302 303
				retval = remove_save_link_only(s,
							&save_link_key, 0);
			}
304 305 306 307 308 309 310 311
		}

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

L
Linus Torvalds 已提交
312
#ifdef CONFIG_QUOTA
313 314
	/* Turn quotas off */
	for (i = 0; i < MAXQUOTAS; i++) {
315
		if (sb_dqopt(s)->files[i] && quota_enabled[i])
316
			dquot_quota_off(s, i);
317 318 319 320
	}
	if (ms_active_set)
		/* Restore the flag back */
		s->s_flags &= ~MS_ACTIVE;
L
Linus Torvalds 已提交
321
#endif
322 323 324 325 326
	pathrelse(&path);
	if (done)
		reiserfs_info(s, "There were %d uncompleted unlinks/truncates. "
			      "Completed\n", done);
	return retval;
L
Linus Torvalds 已提交
327
}
328

L
Linus Torvalds 已提交
329 330
/* 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
331
   item of file. mounting the filesystem we scan all these links and remove
L
Linus Torvalds 已提交
332
   files which almost got lost */
333 334
void add_save_link(struct reiserfs_transaction_handle *th,
		   struct inode *inode, int truncate)
L
Linus Torvalds 已提交
335
{
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
	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 已提交
370 371 372
			reiserfs_warning(inode->i_sb, "green-2102",
					 "Adding a truncate savelink for "
					 "a directory %k! Please report",
373 374 375 376 377 378 379 380 381 382 383 384 385 386
					 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 已提交
387 388 389
			reiserfs_error(inode->i_sb, "vs-2100",
				       "search_by_key (%K) returned %d", &key,
				       retval);
390 391 392
		pathrelse(&path);
		return;
	}
L
Linus Torvalds 已提交
393

394 395 396 397 398 399 400 401
	/* body of "save" link */
	link = INODE_PKEY(inode)->k_dir_id;

	/* put "save" link inot tree, don't charge quota to anyone */
	retval =
	    reiserfs_insert_item(th, &path, &key, &ih, NULL, (char *)&link);
	if (retval) {
		if (retval != -ENOSPC)
J
Jeff Mahoney 已提交
402 403
			reiserfs_error(inode->i_sb, "vs-2120",
				       "insert_item returned %d", retval);
404 405 406 407 408 409 410 411
	} 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 已提交
412 413

/* this opens transaction unlike add_save_link */
414
int remove_save_link(struct inode *inode, int truncate)
L
Linus Torvalds 已提交
415
{
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
	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 已提交
438

439 440 441 442 443 444 445 446 447 448 449 450 451 452
	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;

	return journal_end(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
}
L
Linus Torvalds 已提交
453

454
static void reiserfs_kill_sb(struct super_block *s)
L
Linus Torvalds 已提交
455
{
456 457 458 459 460 461 462 463 464 465 466
	if (REISERFS_SB(s)) {
		if (REISERFS_SB(s)->xattr_root) {
			d_invalidate(REISERFS_SB(s)->xattr_root);
			dput(REISERFS_SB(s)->xattr_root);
			REISERFS_SB(s)->xattr_root = NULL;
		}
		if (REISERFS_SB(s)->priv_root) {
			d_invalidate(REISERFS_SB(s)->priv_root);
			dput(REISERFS_SB(s)->priv_root);
			REISERFS_SB(s)->priv_root = NULL;
		}
467 468
	}

469 470 471 472 473 474 475
	kill_block_super(s);
}

static void reiserfs_put_super(struct super_block *s)
{
	struct reiserfs_transaction_handle th;
	th.t_trans_id = 0;
476

477 478
	dquot_disable(s, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);

F
Frederic Weisbecker 已提交
479
	reiserfs_write_lock(s);
480

481 482 483
	if (s->s_dirt)
		reiserfs_write_super(s);

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
	/* change file system state to current state if it was mounted with read-write permissions */
	if (!(s->s_flags & MS_RDONLY)) {
		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);
			journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
		}
	}

	/* note, journal_release checks for readonly mount, and can decide not
	 ** to do a journal_end
	 */
	journal_release(&th, s);

500
	reiserfs_free_bitmap_cache(s);
501 502 503 504 505 506

	brelse(SB_BUFFER_WITH_SB(s));

	print_statistics(s);

	if (REISERFS_SB(s)->reserved_blocks != 0) {
J
Jeff Mahoney 已提交
507
		reiserfs_warning(s, "green-2005", "reserved blocks left %d",
508 509 510 511 512
				 REISERFS_SB(s)->reserved_blocks);
	}

	reiserfs_proc_info_done(s);

F
Frederic Weisbecker 已提交
513 514
	reiserfs_write_unlock(s);
	mutex_destroy(&REISERFS_SB(s)->lock);
515 516
	kfree(s->s_fs_info);
	s->s_fs_info = NULL;
L
Linus Torvalds 已提交
517 518
}

519
static struct kmem_cache *reiserfs_inode_cachep;
L
Linus Torvalds 已提交
520 521 522 523

static struct inode *reiserfs_alloc_inode(struct super_block *sb)
{
	struct reiserfs_inode_info *ei;
524
	ei = (struct reiserfs_inode_info *)
525
	    kmem_cache_alloc(reiserfs_inode_cachep, GFP_KERNEL);
L
Linus Torvalds 已提交
526 527
	if (!ei)
		return NULL;
A
Al Viro 已提交
528 529
	atomic_set(&ei->openers, 0);
	mutex_init(&ei->tailpack);
L
Linus Torvalds 已提交
530 531 532 533 534 535 536 537
	return &ei->vfs_inode;
}

static void reiserfs_destroy_inode(struct inode *inode)
{
	kmem_cache_free(reiserfs_inode_cachep, REISERFS_I(inode));
}

538
static void init_once(void *foo)
L
Linus Torvalds 已提交
539
{
540
	struct reiserfs_inode_info *ei = (struct reiserfs_inode_info *)foo;
L
Linus Torvalds 已提交
541

C
Christoph Lameter 已提交
542 543
	INIT_LIST_HEAD(&ei->i_prealloc_list);
	inode_init_once(&ei->vfs_inode);
L
Linus Torvalds 已提交
544
}
545

L
Linus Torvalds 已提交
546 547 548
static int init_inodecache(void)
{
	reiserfs_inode_cachep = kmem_cache_create("reiser_inode_cache",
549 550
						  sizeof(struct
							 reiserfs_inode_info),
551 552
						  0, (SLAB_RECLAIM_ACCOUNT|
							SLAB_MEM_SPREAD),
553
						  init_once);
L
Linus Torvalds 已提交
554 555 556 557 558 559 560
	if (reiserfs_inode_cachep == NULL)
		return -ENOMEM;
	return 0;
}

static void destroy_inodecache(void)
{
561
	kmem_cache_destroy(reiserfs_inode_cachep);
L
Linus Torvalds 已提交
562 563 564
}

/* we don't mark inodes dirty, we just log them */
565 566 567 568 569
static void reiserfs_dirty_inode(struct inode *inode)
{
	struct reiserfs_transaction_handle th;

	int err = 0;
570 571
	int lock_depth;

572
	if (inode->i_sb->s_flags & MS_RDONLY) {
J
Jeff Mahoney 已提交
573 574
		reiserfs_warning(inode->i_sb, "clm-6006",
				 "writing inode %lu on readonly FS",
575 576 577
				 inode->i_ino);
		return;
	}
578
	lock_depth = reiserfs_write_lock_once(inode->i_sb);
579 580 581 582 583

	/* this is really only used for atime updates, so they don't have
	 ** to be included in O_SYNC or fsync
	 */
	err = journal_begin(&th, inode->i_sb, 1);
584 585 586
	if (err)
		goto out;

587 588
	reiserfs_update_sd(&th, inode);
	journal_end(&th, inode->i_sb, 1);
589 590 591

out:
	reiserfs_write_unlock_once(inode->i_sb, lock_depth);
L
Linus Torvalds 已提交
592 593 594
}

#ifdef CONFIG_QUOTA
595 596 597 598
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);
L
Linus Torvalds 已提交
599 600
#endif

601
static const struct super_operations reiserfs_sops = {
602 603 604 605
	.alloc_inode = reiserfs_alloc_inode,
	.destroy_inode = reiserfs_destroy_inode,
	.write_inode = reiserfs_write_inode,
	.dirty_inode = reiserfs_dirty_inode,
A
Al Viro 已提交
606
	.evict_inode = reiserfs_evict_inode,
607 608 609
	.put_super = reiserfs_put_super,
	.write_super = reiserfs_write_super,
	.sync_fs = reiserfs_sync_fs,
610 611
	.freeze_fs = reiserfs_freeze,
	.unfreeze_fs = reiserfs_unfreeze,
612 613
	.statfs = reiserfs_statfs,
	.remount_fs = reiserfs_remount,
M
Miklos Szeredi 已提交
614
	.show_options = generic_show_options,
L
Linus Torvalds 已提交
615
#ifdef CONFIG_QUOTA
616 617
	.quota_read = reiserfs_quota_read,
	.quota_write = reiserfs_quota_write,
L
Linus Torvalds 已提交
618 619 620 621 622 623 624 625 626 627 628
#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);
629
static int reiserfs_quota_on(struct super_block *, int, int, char *);
L
Linus Torvalds 已提交
630

631
static const struct dquot_operations reiserfs_quota_operations = {
632 633 634 635 636
	.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,
637 638
	.alloc_dquot	= dquot_alloc,
	.destroy_dquot	= dquot_destroy,
L
Linus Torvalds 已提交
639 640
};

641
static const struct quotactl_ops reiserfs_qctl_operations = {
642
	.quota_on = reiserfs_quota_on,
643 644 645 646 647 648
	.quota_off = dquot_quota_off,
	.quota_sync = dquot_quota_sync,
	.get_info = dquot_get_dqinfo,
	.set_info = dquot_set_dqinfo,
	.get_dqblk = dquot_get_dqblk,
	.set_dqblk = dquot_set_dqblk,
L
Linus Torvalds 已提交
649 650 651
};
#endif

652
static const struct export_operations reiserfs_export_ops = {
653
	.encode_fh = reiserfs_encode_fh,
C
Christoph Hellwig 已提交
654 655
	.fh_to_dentry = reiserfs_fh_to_dentry,
	.fh_to_parent = reiserfs_fh_to_parent,
656 657
	.get_parent = reiserfs_get_parent,
};
L
Linus Torvalds 已提交
658 659 660 661

/* this struct is used in reiserfs_getopt () for containing the value for those
   mount options that have values rather than being toggles. */
typedef struct {
662 663 664 665 666 667
	char *value;
	int setmask;		/* bitmask which is to set on mount_options bitmask when this
				   value is found, 0 is no bits are to be changed. */
	int clrmask;		/* 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 */
L
Linus Torvalds 已提交
668 669 670 671 672 673 674 675
} arg_desc_t;

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

/* this struct is used in reiserfs_getopt() for describing the set of reiserfs
   mount options */
typedef struct {
676 677 678 679 680 681 682 683
	char *option_name;
	int arg_required;	/* 0 if argument is not required, not 0 otherwise */
	const arg_desc_t *values;	/* list of values accepted by an option */
	int setmask;		/* bitmask which is to set on mount_options bitmask when this
				   value is found, 0 is no bits are to be changed. */
	int clrmask;		/* 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 */
L
Linus Torvalds 已提交
684 685 686 687
} opt_desc_t;

/* possible values for -o data= */
static const arg_desc_t logging_mode[] = {
688 689 690 691 692 693
	{"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)},
694
	{.value = NULL}
L
Linus Torvalds 已提交
695 696 697 698
};

/* possible values for -o barrier= */
static const arg_desc_t barrier_mode[] = {
699 700
	{"none", 1 << REISERFS_BARRIER_NONE, 1 << REISERFS_BARRIER_FLUSH},
	{"flush", 1 << REISERFS_BARRIER_FLUSH, 1 << REISERFS_BARRIER_NONE},
701
	{.value = NULL}
L
Linus Torvalds 已提交
702 703 704 705 706
};

/* 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 */
static const arg_desc_t balloc[] = {
707 708 709 710 711 712 713
	{"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 已提交
714 715 716
};

static const arg_desc_t tails[] = {
717 718 719 720
	{"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 已提交
721 722 723
};

static const arg_desc_t error_actions[] = {
724 725 726 727
	{"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 已提交
728
#ifdef REISERFS_JOURNAL_ERROR_ALLOWS_NO_LOG
729 730
	{"continue", 1 << REISERFS_ERROR_CONTINUE,
	 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_RO)},
L
Linus Torvalds 已提交
731
#endif
732
	{NULL, 0, 0},
L
Linus Torvalds 已提交
733 734 735 736 737 738 739 740
};

/* 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 */
741 742
static int reiserfs_getopt(struct super_block *s, char **cur, opt_desc_t * opts,
			   char **opt_arg, unsigned long *bit_flags)
L
Linus Torvalds 已提交
743
{
744
	char *p;
745
	/* foo=bar,
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
	   ^   ^  ^
	   |   |  +-- option_end
	   |   +-- arg_start
	   +-- option_start
	 */
	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)) {
		/* 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. */
		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 已提交
780 781
					reiserfs_warning(s, "super-6500",
							 "%s not supported.\n",
782 783 784 785 786
							 p);
				else
					*bit_flags &= ~opt->clrmask;
				if (opt->setmask ==
				    (1 << REISERFS_UNSUPPORTED_OPT))
J
Jeff Mahoney 已提交
787 788
					reiserfs_warning(s, "super-6501",
							 "%s not supported.\n",
789 790 791 792 793 794 795 796
							 p);
				else
					*bit_flags |= opt->setmask;
			}
			break;
		}
	}
	if (!opt->option_name) {
J
Jeff Mahoney 已提交
797 798
		reiserfs_warning(s, "super-6502",
				 "unknown mount option \"%s\"", p);
799 800 801 802 803 804 805
		return -1;
	}

	p += strlen(opt->option_name);
	switch (*p) {
	case '=':
		if (!opt->arg_required) {
J
Jeff Mahoney 已提交
806 807 808
			reiserfs_warning(s, "super-6503",
					 "the option \"%s\" does not "
					 "require an argument\n",
809 810 811 812 813 814 815
					 opt->option_name);
			return -1;
		}
		break;

	case 0:
		if (opt->arg_required) {
J
Jeff Mahoney 已提交
816 817 818
			reiserfs_warning(s, "super-6504",
					 "the option \"%s\" requires an "
					 "argument\n", opt->option_name);
819 820 821 822
			return -1;
		}
		break;
	default:
J
Jeff Mahoney 已提交
823 824
		reiserfs_warning(s, "super-6505",
				 "head of option \"%s\" is only correct\n",
825 826 827 828 829 830 831 832 833 834 835
				 opt->option_name);
		return -1;
	}

	/* move to the argument, or to next option if argument is not required */
	p++;

	if (opt->arg_required
	    && !(opt->arg_required & (1 << REISERFS_OPT_ALLOWEMPTY))
	    && !strlen(p)) {
		/* this catches "option=," if not allowed */
J
Jeff Mahoney 已提交
836 837
		reiserfs_warning(s, "super-6506",
				 "empty argument for \"%s\"\n",
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
				 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 已提交
859 860
	reiserfs_warning(s, "super-6506",
			 "bad value \"%s\" for option \"%s\"\n", p,
861
			 opt->option_name);
L
Linus Torvalds 已提交
862 863 864 865
	return -1;
}

/* returns 0 if something is wrong in option string, 1 - otherwise */
866 867 868 869 870 871 872
static int reiserfs_parse_options(struct super_block *s, char *options,	/* string given via mount's -o */
				  unsigned long *mount_options,
				  /* after the parsing phase, contains the
				     collection of bitflags defining what
				     mount options were selected. */
				  unsigned long *blocks,	/* strtol-ed from NNN of resize=NNN */
				  char **jdev_name,
873 874 875
				  unsigned int *commit_max_age,
				  char **qf_names,
				  unsigned int *qfmt)
L
Linus Torvalds 已提交
876
{
877 878 879 880 881 882 883 884 885 886 887
	int c;
	char *arg = NULL;
	char *pos;
	opt_desc_t opts[] = {
		/* Compatibility stuff, so that -o notail for old setups still work */
		{"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},
888
		{"expose_privroot", .setmask = 1 << REISERFS_EXPOSE_PRIVROOT},
L
Linus Torvalds 已提交
889
#ifdef CONFIG_REISERFS_FS_XATTR
890 891
		{"user_xattr",.setmask = 1 << REISERFS_XATTRS_USER},
		{"nouser_xattr",.clrmask = 1 << REISERFS_XATTRS_USER},
L
Linus Torvalds 已提交
892
#else
893 894
		{"user_xattr",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
		{"nouser_xattr",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
L
Linus Torvalds 已提交
895 896
#endif
#ifdef CONFIG_REISERFS_FS_POSIX_ACL
897 898
		{"acl",.setmask = 1 << REISERFS_POSIXACL},
		{"noacl",.clrmask = 1 << REISERFS_POSIXACL},
L
Linus Torvalds 已提交
899
#else
900 901
		{"acl",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
		{"noacl",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
L
Linus Torvalds 已提交
902
#endif
903
		{.option_name = "nolog"},
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
		{"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},
		{"usrquota",.setmask = 1 << REISERFS_QUOTA},
		{"grpquota",.setmask = 1 << REISERFS_QUOTA},
		{"noquota",.clrmask = 1 << REISERFS_QUOTA},
		{"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},
921
		{.option_name = NULL}
922 923 924 925 926 927 928 929 930 931 932 933
	};

	*blocks = 0;
	if (!options || !*options)
		/* use default configuration: create tails, journaling on, no
		   conversion to newest format */
		return 1;

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

936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
		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 =
				    s->s_bdev->bd_inode->i_size >> s->
				    s_blocksize_bits;
			} else {
				*blocks = simple_strtoul(arg, &p, 0);
				if (*p != '\0') {
					/* NNN does not look like a number */
J
Jeff Mahoney 已提交
951 952 953
					reiserfs_warning(s, "super-6507",
							 "bad value %s for "
							 "-oresize\n", arg);
954 955 956
					return 0;
				}
			}
L
Linus Torvalds 已提交
957 958
		}

959 960 961 962 963
		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 已提交
964 965
				reiserfs_warning(s, "super-6508",
						 "bad value %s for -ocommit\n",
966 967 968 969
						 arg);
				return 0;
			}
			*commit_max_age = (unsigned int)val;
L
Linus Torvalds 已提交
970 971
		}

972
		if (c == 'w') {
J
Jeff Mahoney 已提交
973 974
			reiserfs_warning(s, "super-6509", "nolargeio option "
					 "is no longer supported");
975
			return 0;
L
Linus Torvalds 已提交
976 977
		}

978 979 980
		if (c == 'j') {
			if (arg && *arg && jdev_name) {
				if (*jdev_name) {	//Hm, already assigned?
J
Jeff Mahoney 已提交
981 982 983 984
					reiserfs_warning(s, "super-6510",
							 "journal device was "
							 "already specified to "
							 "be %s", *jdev_name);
985 986 987 988
					return 0;
				}
				*jdev_name = arg;
			}
L
Linus Torvalds 已提交
989
		}
990 991 992 993
#ifdef CONFIG_QUOTA
		if (c == 'u' || c == 'g') {
			int qtype = c == 'u' ? USRQUOTA : GRPQUOTA;

994
			if (sb_any_quota_loaded(s) &&
995
			    (!*arg != !REISERFS_SB(s)->s_qf_names[qtype])) {
J
Jeff Mahoney 已提交
996 997 998 999
				reiserfs_warning(s, "super-6511",
						 "cannot change journaled "
						 "quota options when quota "
						 "turned on.");
1000 1001 1002 1003 1004 1005
				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 已提交
1006 1007 1008
					reiserfs_warning(s, "super-6512",
							 "%s quota file "
							 "already specified.",
1009 1010 1011 1012
							 QTYPE2NAME(qtype));
					return 0;
				}
				if (strchr(arg, '/')) {
J
Jeff Mahoney 已提交
1013 1014 1015
					reiserfs_warning(s, "super-6513",
							 "quotafile must be "
							 "on filesystem root.");
1016 1017
					return 0;
				}
1018
				qf_names[qtype] =
1019
				    kmalloc(strlen(arg) + 1, GFP_KERNEL);
1020
				if (!qf_names[qtype]) {
J
Jeff Mahoney 已提交
1021 1022 1023 1024
					reiserfs_warning(s, "reiserfs-2502",
							 "not enough memory "
							 "for storing "
							 "quotafile name.");
1025 1026
					return 0;
				}
1027
				strcpy(qf_names[qtype], arg);
1028 1029
				*mount_options |= 1 << REISERFS_QUOTA;
			} else {
1030 1031 1032 1033
				if (qf_names[qtype] !=
				    REISERFS_SB(s)->s_qf_names[qtype])
					kfree(qf_names[qtype]);
				qf_names[qtype] = NULL;
1034
			}
L
Linus Torvalds 已提交
1035
		}
1036 1037
		if (c == 'f') {
			if (!strcmp(arg, "vfsold"))
1038
				*qfmt = QFMT_VFS_OLD;
1039
			else if (!strcmp(arg, "vfsv0"))
1040
				*qfmt = QFMT_VFS_V0;
1041
			else {
J
Jeff Mahoney 已提交
1042 1043 1044
				reiserfs_warning(s, "super-6514",
						 "unknown quota format "
						 "specified.");
1045 1046
				return 0;
			}
1047
			if (sb_any_quota_loaded(s) &&
1048
			    *qfmt != REISERFS_SB(s)->s_jquota_fmt) {
J
Jeff Mahoney 已提交
1049 1050 1051 1052
				reiserfs_warning(s, "super-6515",
						 "cannot change journaled "
						 "quota options when quota "
						 "turned on.");
1053 1054
				return 0;
			}
L
Linus Torvalds 已提交
1055
		}
1056 1057
#else
		if (c == 'u' || c == 'g' || c == 'f') {
J
Jeff Mahoney 已提交
1058 1059
			reiserfs_warning(s, "reiserfs-2503", "journaled "
					 "quota options not supported.");
1060
			return 0;
L
Linus Torvalds 已提交
1061
		}
1062 1063 1064 1065
#endif
	}

#ifdef CONFIG_QUOTA
1066 1067
	if (!REISERFS_SB(s)->s_jquota_fmt && !*qfmt
	    && (qf_names[USRQUOTA] || qf_names[GRPQUOTA])) {
J
Jeff Mahoney 已提交
1068 1069
		reiserfs_warning(s, "super-6515",
				 "journaled quota format not specified.");
L
Linus Torvalds 已提交
1070 1071
		return 0;
	}
1072 1073
	/* This checking is not precise wrt the quota type but for our purposes it is sufficient */
	if (!(*mount_options & (1 << REISERFS_QUOTA))
1074
	    && sb_any_quota_loaded(s)) {
J
Jeff Mahoney 已提交
1075 1076
		reiserfs_warning(s, "super-6516", "quota options must "
				 "be present when quota is turned on.");
1077
		return 0;
L
Linus Torvalds 已提交
1078 1079
	}
#endif
1080

1081
	return 1;
L
Linus Torvalds 已提交
1082 1083
}

1084 1085 1086 1087 1088 1089
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 已提交
1090 1091 1092 1093
}

static void handle_data_mode(struct super_block *s, unsigned long mount_options)
{
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
	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 已提交
1110 1111
}

1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
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 已提交
1128 1129
}

1130
static void handle_attrs(struct super_block *s)
L
Linus Torvalds 已提交
1131
{
1132
	struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(s);
L
Linus Torvalds 已提交
1133

1134 1135
	if (reiserfs_attrs(s)) {
		if (old_format_only(s)) {
J
Jeff Mahoney 已提交
1136 1137
			reiserfs_warning(s, "super-6517", "cannot support "
					 "attributes on 3.5.x disk format");
1138
			REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
L
Linus Torvalds 已提交
1139 1140
			return;
		}
1141
		if (!(le32_to_cpu(rs->s_flags) & reiserfs_attrs_cleared)) {
J
Jeff Mahoney 已提交
1142 1143 1144
			reiserfs_warning(s, "super-6518", "cannot support "
					 "attributes until flag is set in "
					 "super-block");
1145
			REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
L
Linus Torvalds 已提交
1146 1147 1148 1149
		}
	}
}

1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
#ifdef CONFIG_QUOTA
static void handle_quota_files(struct super_block *s, char **qf_names,
			       unsigned int *qfmt)
{
	int i;

	for (i = 0; i < MAXQUOTAS; i++) {
		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];
	}
	REISERFS_SB(s)->s_jquota_fmt = *qfmt;
}
#endif

1165
static int reiserfs_remount(struct super_block *s, int *mount_flags, char *arg)
L
Linus Torvalds 已提交
1166
{
1167 1168 1169 1170 1171 1172 1173
	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);
M
Miklos Szeredi 已提交
1174
	char *new_opts = kstrdup(arg, GFP_KERNEL);
1175
	int err;
1176 1177
	char *qf_names[MAXQUOTAS];
	unsigned int qfmt = 0;
L
Linus Torvalds 已提交
1178
#ifdef CONFIG_QUOTA
1179
	int i;
F
Frederic Weisbecker 已提交
1180
#endif
1181

F
Frederic Weisbecker 已提交
1182
	reiserfs_write_lock(s);
1183

F
Frederic Weisbecker 已提交
1184
#ifdef CONFIG_QUOTA
1185
	memcpy(qf_names, REISERFS_SB(s)->s_qf_names, sizeof(qf_names));
L
Linus Torvalds 已提交
1186 1187
#endif

1188
	rs = SB_DISK_SUPER_BLOCK(s);
L
Linus Torvalds 已提交
1189

1190
	if (!reiserfs_parse_options
1191 1192
	    (s, arg, &mount_options, &blocks, NULL, &commit_max_age,
	    qf_names, &qfmt)) {
L
Linus Torvalds 已提交
1193
#ifdef CONFIG_QUOTA
1194 1195 1196
		for (i = 0; i < MAXQUOTAS; i++)
			if (qf_names[i] != REISERFS_SB(s)->s_qf_names[i])
				kfree(qf_names[i]);
L
Linus Torvalds 已提交
1197
#endif
M
Miklos Szeredi 已提交
1198 1199
		err = -EINVAL;
		goto out_err;
1200
	}
1201 1202 1203
#ifdef CONFIG_QUOTA
	handle_quota_files(s, qf_names, &qfmt);
#endif
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239

	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;
	safe_mask |= 1 << REISERFS_QUOTA;

	/* Update the bitmask, taking care to keep
	 * the bits we're not allowed to change here */
	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 已提交
1240 1241 1242
		err = reiserfs_resize(s, blocks);
		if (err != 0)
			goto out_err;
1243 1244 1245 1246 1247 1248 1249
	}

	if (*mount_flags & MS_RDONLY) {
		reiserfs_xattr_init(s, *mount_flags);
		/* remount read-only */
		if (s->s_flags & MS_RDONLY)
			/* it is read-only already */
M
Miklos Szeredi 已提交
1250
			goto out_ok;
1251

1252 1253
		err = dquot_suspend(s, -1);
		if (err < 0)
1254 1255
			goto out_err;

1256 1257 1258
		/* 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) {
M
Miklos Szeredi 已提交
1259
			goto out_ok;
1260 1261 1262 1263
		}

		err = journal_begin(&th, s, 10);
		if (err)
M
Miklos Szeredi 已提交
1264
			goto out_err;
1265 1266 1267 1268 1269 1270 1271 1272 1273

		/* 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);
		journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
	} else {
		/* remount read-write */
		if (!(s->s_flags & MS_RDONLY)) {
			reiserfs_xattr_init(s, *mount_flags);
M
Miklos Szeredi 已提交
1274
			goto out_ok;	/* We are read-write already */
1275 1276
		}

M
Miklos Szeredi 已提交
1277 1278 1279 1280
		if (reiserfs_is_journal_aborted(journal)) {
			err = journal->j_errno;
			goto out_err;
		}
1281 1282 1283 1284 1285 1286 1287

		handle_data_mode(s, mount_options);
		handle_barrier_mode(s, mount_options);
		REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
		s->s_flags &= ~MS_RDONLY;	/* now it is safe to call journal_begin */
		err = journal_begin(&th, s, 10);
		if (err)
M
Miklos Szeredi 已提交
1288
			goto out_err;
1289 1290 1291 1292 1293 1294

		/* 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);
1295 1296
		if (!old_format_only(s))
			set_sb_mnt_count(rs, sb_mnt_count(rs) + 1);
1297 1298 1299 1300 1301 1302 1303 1304
		/* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
		journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
		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;
	err = journal_end(&th, s, 10);
	if (err)
M
Miklos Szeredi 已提交
1305
		goto out_err;
1306 1307 1308
	s->s_dirt = 0;

	if (!(*mount_flags & MS_RDONLY)) {
1309
		dquot_resume(s, -1);
1310 1311 1312 1313
		finish_unfinished(s);
		reiserfs_xattr_init(s, *mount_flags);
	}

M
Miklos Szeredi 已提交
1314
out_ok:
1315
	replace_mount_options(s, new_opts);
F
Frederic Weisbecker 已提交
1316
	reiserfs_write_unlock(s);
1317
	return 0;
M
Miklos Szeredi 已提交
1318 1319 1320

out_err:
	kfree(new_opts);
F
Frederic Weisbecker 已提交
1321
	reiserfs_write_unlock(s);
M
Miklos Szeredi 已提交
1322
	return err;
L
Linus Torvalds 已提交
1323 1324
}

1325
static int read_super_block(struct super_block *s, int offset)
L
Linus Torvalds 已提交
1326
{
1327 1328 1329
	struct buffer_head *bh;
	struct reiserfs_super_block *rs;
	int fs_blocksize;
L
Linus Torvalds 已提交
1330

1331 1332
	bh = sb_bread(s, offset / s->s_blocksize);
	if (!bh) {
J
Jeff Mahoney 已提交
1333
		reiserfs_warning(s, "sh-2006",
1334 1335 1336 1337 1338
				 "bread failed (dev %s, block %lu, size %lu)",
				 reiserfs_bdevname(s), offset / s->s_blocksize,
				 s->s_blocksize);
		return 1;
	}
L
Linus Torvalds 已提交
1339

1340 1341 1342 1343 1344 1345 1346
	rs = (struct reiserfs_super_block *)bh->b_data;
	if (!is_any_reiserfs_magic_string(rs)) {
		brelse(bh);
		return 1;
	}
	//
	// ok, reiserfs signature (old or new) found in at the given offset
1347
	//
1348 1349 1350
	fs_blocksize = sb_blocksize(rs);
	brelse(bh);
	sb_set_blocksize(s, fs_blocksize);
L
Linus Torvalds 已提交
1351

1352 1353
	bh = sb_bread(s, offset / s->s_blocksize);
	if (!bh) {
J
Jeff Mahoney 已提交
1354 1355
		reiserfs_warning(s, "sh-2007",
				 "bread failed (dev %s, block %lu, size %lu)",
1356 1357 1358 1359
				 reiserfs_bdevname(s), offset / s->s_blocksize,
				 s->s_blocksize);
		return 1;
	}
L
Linus Torvalds 已提交
1360

1361 1362
	rs = (struct reiserfs_super_block *)bh->b_data;
	if (sb_blocksize(rs) != s->s_blocksize) {
J
Jeff Mahoney 已提交
1363 1364
		reiserfs_warning(s, "sh-2011", "can't find a reiserfs "
				 "filesystem on (dev %s, block %Lu, size %lu)",
1365 1366 1367 1368 1369 1370
				 reiserfs_bdevname(s),
				 (unsigned long long)bh->b_blocknr,
				 s->s_blocksize);
		brelse(bh);
		return 1;
	}
L
Linus Torvalds 已提交
1371

1372 1373
	if (rs->s_v1.s_root_block == cpu_to_le32(-1)) {
		brelse(bh);
J
Jeff Mahoney 已提交
1374 1375 1376 1377
		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"
1378 1379
				 "get newer reiserfsprogs package");
		return 1;
L
Linus Torvalds 已提交
1380 1381
	}

1382 1383 1384 1385 1386 1387 1388
	SB_BUFFER_WITH_SB(s) = bh;
	SB_DISK_SUPER_BLOCK(s) = rs;

	if (is_reiserfs_jr(rs)) {
		/* magic is of non-standard journal filesystem, look at s_version to
		   find which format is in use */
		if (sb_version(rs) == REISERFS_VERSION_2)
1389 1390
			reiserfs_info(s, "found reiserfs format \"3.6\""
				      " with non-standard journal\n");
1391
		else if (sb_version(rs) == REISERFS_VERSION_1)
1392 1393
			reiserfs_info(s, "found reiserfs format \"3.5\""
				      " with non-standard journal\n");
1394
		else {
J
Jeff Mahoney 已提交
1395 1396 1397
			reiserfs_warning(s, "sh-2012", "found unknown "
					 "format \"%u\" of reiserfs with "
					 "non-standard magic", sb_version(rs));
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
			return 1;
		}
	} else
		/* s_version of standard format may contain incorrect information,
		   so we just look at the magic string */
		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 已提交
1409
#ifdef CONFIG_QUOTA
1410 1411
	s->s_qcop = &reiserfs_qctl_operations;
	s->dq_op = &reiserfs_quota_operations;
L
Linus Torvalds 已提交
1412 1413
#endif

1414 1415 1416 1417 1418
	/* new format is limited by the 32 bit wide i_blocks field, want to
	 ** be one full block below that.
	 */
	s->s_maxbytes = (512LL << 32) - s->s_blocksize;
	return 0;
L
Linus Torvalds 已提交
1419 1420 1421
}

/* after journal replay, reread all bitmap and super blocks */
1422 1423 1424
static int reread_meta_blocks(struct super_block *s)
{
	ll_rw_block(READ, 1, &(SB_BUFFER_WITH_SB(s)));
F
Frederic Weisbecker 已提交
1425
	reiserfs_write_unlock(s);
1426
	wait_on_buffer(SB_BUFFER_WITH_SB(s));
F
Frederic Weisbecker 已提交
1427
	reiserfs_write_lock(s);
1428
	if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
J
Jeff Mahoney 已提交
1429
		reiserfs_warning(s, "reiserfs-2504", "error reading the super");
1430 1431
		return 1;
	}
L
Linus Torvalds 已提交
1432

1433 1434
	return 0;
}
L
Linus Torvalds 已提交
1435 1436 1437 1438 1439 1440 1441 1442 1443

/////////////////////////////////////////////////////
// hash detection stuff

// 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
// bith have the same value - we ask user to send report to the
// mailing list
1444
static __u32 find_hash_out(struct super_block *s)
L
Linus Torvalds 已提交
1445
{
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
	int retval;
	struct inode *inode;
	struct cpu_key key;
	INITIALIZE_PATH(path);
	struct reiserfs_dir_entry de;
	__u32 hash = DEFAULT_HASH;

	inode = s->s_root->d_inode;

	do {			// Some serious "goto"-hater was there ;)
		u32 teahash, r5hash, yurahash;

		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--;
		set_de_name_and_namelen(&de);
		if (deh_offset(&(de.de_deh[de.de_entry_num])) == DOT_DOT_OFFSET) {
			/* allow override in this case */
			if (reiserfs_rupasov_hash(s)) {
				hash = YURA_HASH;
			}
1472 1473
			reiserfs_info(s, "FS seems to be empty, autodetect "
					 "is using the default hash\n");
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
			break;
		}
		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)
		     &&
		     (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num])))
		      == r5hash)) || ((teahash == yurahash)
				      && (yurahash ==
					  GET_HASH_VALUE(deh_offset
							 (&
							  (de.
							   de_deh[de.
								  de_entry_num])))))
		    || ((r5hash == yurahash)
			&& (yurahash ==
			    GET_HASH_VALUE(deh_offset
					   (&(de.de_deh[de.de_entry_num])))))) {
J
Jeff Mahoney 已提交
1493 1494 1495 1496
			reiserfs_warning(s, "reiserfs-2506", "Unable to "
					 "automatically detect hash function. "
					 "Please mount with -o "
					 "hash={tea,rupasov,r5}");
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
			hash = UNSET_HASH;
			break;
		}
		if (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num]))) ==
		    yurahash)
			hash = YURA_HASH;
		else if (GET_HASH_VALUE
			 (deh_offset(&(de.de_deh[de.de_entry_num]))) == teahash)
			hash = TEA_HASH;
		else if (GET_HASH_VALUE
			 (deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash)
			hash = R5_HASH;
		else {
J
Jeff Mahoney 已提交
1510 1511
			reiserfs_warning(s, "reiserfs-2506",
					 "Unrecognised hash function");
1512 1513 1514 1515 1516 1517
			hash = UNSET_HASH;
		}
	} while (0);

	pathrelse(&path);
	return hash;
L
Linus Torvalds 已提交
1518 1519 1520
}

// finds out which hash names are sorted with
1521
static int what_hash(struct super_block *s)
L
Linus Torvalds 已提交
1522
{
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
	__u32 code;

	code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));

	/* 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
	 */
	if (code == UNSET_HASH || reiserfs_hash_detect(s))
		code = find_hash_out(s);

	if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
1535 1536
		/* detection has found the hash, and we must check against the
		 ** mount options
1537 1538
		 */
		if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
J
Jeff Mahoney 已提交
1539 1540
			reiserfs_warning(s, "reiserfs-2507",
					 "Error, %s hash detected, "
1541 1542 1543 1544
					 "unable to force rupasov hash",
					 reiserfs_hashname(code));
			code = UNSET_HASH;
		} else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
J
Jeff Mahoney 已提交
1545 1546
			reiserfs_warning(s, "reiserfs-2508",
					 "Error, %s hash detected, "
1547 1548 1549 1550
					 "unable to force tea hash",
					 reiserfs_hashname(code));
			code = UNSET_HASH;
		} else if (reiserfs_r5_hash(s) && code != R5_HASH) {
J
Jeff Mahoney 已提交
1551 1552
			reiserfs_warning(s, "reiserfs-2509",
					 "Error, %s hash detected, "
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
					 "unable to force r5 hash",
					 reiserfs_hashname(code));
			code = UNSET_HASH;
		}
	} else {
		/* find_hash_out was not called or could not determine the hash */
		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;
		}
	}

1568
	/* if we are mounted RW, and we have a new valid hash code, update
1569 1570 1571 1572 1573 1574 1575 1576
	 ** the super
	 */
	if (code != UNSET_HASH &&
	    !(s->s_flags & MS_RDONLY) &&
	    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 已提交
1577 1578 1579
}

// return pointer to appropriate function
1580
static hashf_t hash_function(struct super_block *s)
L
Linus Torvalds 已提交
1581
{
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
	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 已提交
1594 1595 1596
}

// this is used to set up correct value for old partitions
1597
static int function2code(hashf_t func)
L
Linus Torvalds 已提交
1598
{
1599 1600 1601 1602 1603 1604
	if (func == keyed_hash)
		return TEA_HASH;
	if (func == yura_hash)
		return YURA_HASH;
	if (func == r5_hash)
		return R5_HASH;
L
Linus Torvalds 已提交
1605

1606
	BUG();			// should never happen
L
Linus Torvalds 已提交
1607

1608
	return 0;
L
Linus Torvalds 已提交
1609 1610
}

J
Jeff Mahoney 已提交
1611
#define SWARN(silent, s, id, ...)			\
L
Linus Torvalds 已提交
1612
	if (!(silent))				\
J
Jeff Mahoney 已提交
1613
		reiserfs_warning(s, id, __VA_ARGS__)
L
Linus Torvalds 已提交
1614

1615
static int reiserfs_fill_super(struct super_block *s, void *data, int silent)
L
Linus Torvalds 已提交
1616
{
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
	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;
1628 1629
	char *qf_names[MAXQUOTAS] = {};
	unsigned int qfmt = 0;
1630

M
Miklos Szeredi 已提交
1631 1632
	save_mount_options(s, data);

1633
	sbi = kzalloc(sizeof(struct reiserfs_sb_info), GFP_KERNEL);
1634 1635
	if (!sbi)
		return -ENOMEM;
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
	s->s_fs_info = sbi;
	/* Set default values for options: non-aggressive tails, RO on errors */
	REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_SMALLTAIL);
	REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_ERROR_RO);
	/* no preallocation minimum, be smart in
	   reiserfs_file_write instead */
	REISERFS_SB(s)->s_alloc_options.preallocmin = 0;
	/* Preallocate by 16 blocks (17-1) at once */
	REISERFS_SB(s)->s_alloc_options.preallocsize = 17;
	/* setup default block allocator options */
	reiserfs_init_alloc_options(s);

F
Frederic Weisbecker 已提交
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
	mutex_init(&REISERFS_SB(s)->lock);
	REISERFS_SB(s)->lock_depth = -1;

	/*
	 * This function is called with the bkl, which also was the old
	 * locking used here.
	 * do_journal_begin() will soon check if we hold the lock (ie: was the
	 * bkl). This is likely because do_journal_begin() has several another
	 * callers because at this time, it doesn't seem to be necessary to
	 * protect against anything.
	 * Anyway, let's be conservative and lock for now.
	 */
	reiserfs_write_lock(s);

1662 1663 1664
	jdev_name = NULL;
	if (reiserfs_parse_options
	    (s, (char *)data, &(sbi->s_mount_opt), &blocks, &jdev_name,
1665
	     &commit_max_age, qf_names, &qfmt) == 0) {
1666 1667
		goto error;
	}
1668 1669 1670
#ifdef CONFIG_QUOTA
	handle_quota_files(s, qf_names, &qfmt);
#endif
1671 1672

	if (blocks) {
J
Jeff Mahoney 已提交
1673
		SWARN(silent, s, "jmacd-7", "resize option for remount only");
1674 1675 1676 1677 1678 1679 1680 1681
		goto error;
	}

	/* try old format (undistributed bitmap, super block in 8-th 1k block of a device) */
	if (!read_super_block(s, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
		old_format = 1;
	/* try new format (64-th 1k block), which can contain reiserfs super block */
	else if (read_super_block(s, REISERFS_DISK_OFFSET_IN_BYTES)) {
J
Jeff Mahoney 已提交
1682
		SWARN(silent, s, "sh-2021", "can not find reiserfs on %s",
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
		      reiserfs_bdevname(s));
		goto error;
	}

	rs = SB_DISK_SUPER_BLOCK(s);
	/* 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. */
	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 已提交
1694 1695 1696 1697 1698 1699
		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");
1700 1701 1702 1703 1704 1705
		goto error;
	}

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

1706
	if ((errval = reiserfs_init_bitmap_cache(s))) {
J
Jeff Mahoney 已提交
1707
		SWARN(silent, s, "jmacd-8", "unable to read bitmap");
1708 1709
		goto error;
	}
1710
	errval = -EINVAL;
L
Linus Torvalds 已提交
1711
#ifdef CONFIG_REISERFS_CHECK
J
Jeff Mahoney 已提交
1712 1713
	SWARN(silent, s, "", "CONFIG_REISERFS_CHECK is set ON");
	SWARN(silent, s, "", "- it is slow mode for debugging.");
L
Linus Torvalds 已提交
1714 1715
#endif

1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
	/* make data=ordered the default */
	if (!reiserfs_data_log(s) && !reiserfs_data_ordered(s) &&
	    !reiserfs_data_writeback(s)) {
		REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
	}

	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");
	}
	// set_device_ro(s->s_dev, 1) ;
	if (journal_init(s, jdev_name, old_format, commit_max_age)) {
J
Jeff Mahoney 已提交
1734 1735
		SWARN(silent, s, "sh-2022",
		      "unable to initialize journal space");
1736 1737 1738 1739 1740 1741 1742
		goto error;
	} else {
		jinit_done = 1;	/* once this is set, journal_release must be called
				 ** if we error out of the mount
				 */
	}
	if (reread_meta_blocks(s)) {
J
Jeff Mahoney 已提交
1743 1744
		SWARN(silent, s, "jmacd-9",
		      "unable to reread meta blocks after journal init");
1745 1746 1747 1748 1749 1750 1751
		goto error;
	}

	if (replay_only(s))
		goto error;

	if (bdev_read_only(s->s_bdev) && !(s->s_flags & MS_RDONLY)) {
J
Jeff Mahoney 已提交
1752 1753
		SWARN(silent, s, "clm-7000",
		      "Detected readonly device, marking FS readonly");
1754 1755 1756 1757 1758 1759 1760 1761
		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,
			 reiserfs_init_locked_inode, (void *)(&args));
	if (!root_inode) {
J
Jeff Mahoney 已提交
1762
		SWARN(silent, s, "jmacd-10", "get root inode failed");
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
		goto error;
	}

	if (root_inode->i_state & I_NEW) {
		reiserfs_read_locked_inode(root_inode, &args);
		unlock_new_inode(root_inode);
	}

	s->s_root = d_alloc_root(root_inode);
	if (!s->s_root) {
		iput(root_inode);
		goto error;
	}
	// define and initialize hash function
	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))
		set_bit(REISERFS_3_5, &(sbi->s_properties));
1787 1788
	else if (old_format)
		set_bit(REISERFS_OLD_FORMAT, &(sbi->s_properties));
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
	else
		set_bit(REISERFS_3_6, &(sbi->s_properties));

	if (!(s->s_flags & MS_RDONLY)) {

		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);

1805 1806 1807 1808 1809 1810
		/* Clear out s_bmap_nr if it would wrap. We can handle this
		 * case, but older revisions can't. This will cause the
		 * file system to fail mount on those older implementations,
		 * avoiding corruption. -jeffm */
		if (bmap_would_wrap(reiserfs_bmap_count(s)) &&
		    sb_bmap_nr(rs) != 0) {
J
Jeff Mahoney 已提交
1811
			reiserfs_warning(s, "super-2030", "This file system "
1812 1813 1814 1815 1816 1817 1818 1819
					"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);
		}

1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
		if (old_format_only(s)) {
			/* filesystem of format 3.5 either with standard or non-standard
			   journal */
			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))
					/* put magic string of 3.6 format. 2.2 will not be able to
					   mount this filesystem anymore */
					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);
				set_bit(REISERFS_3_6, &(sbi->s_properties));
				clear_bit(REISERFS_3_5, &(sbi->s_properties));
			} else if (!silent) {
				reiserfs_info(s, "using 3.5.x disk format\n");
			}
1844 1845 1846
		} else
			set_sb_mnt_count(rs, sb_mnt_count(rs) + 1);

1847 1848 1849 1850 1851 1852 1853 1854 1855

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

1856 1857
		if ((errval = reiserfs_lookup_privroot(s)) ||
		    (errval = reiserfs_xattr_init(s, s->s_flags))) {
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
			dput(s->s_root);
			s->s_root = NULL;
			goto error;
		}

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

1870 1871
		if ((errval = reiserfs_lookup_privroot(s)) ||
		    (errval = reiserfs_xattr_init(s, s->s_flags))) {
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
			dput(s->s_root);
			s->s_root = NULL;
			goto error;
		}
	}
	// mark hash in super block: it could be unset. overwrite should be ok
	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 已提交
1887 1888
	reiserfs_write_unlock(s);

1889 1890
	return (0);

1891
error:
1892 1893 1894
	if (jinit_done) {	/* kill the commit thread, free journal ram */
		journal_release_error(NULL, s);
	}
1895

1896 1897
	reiserfs_write_unlock(s);

1898
	reiserfs_free_bitmap_cache(s);
1899 1900
	if (SB_BUFFER_WITH_SB(s))
		brelse(SB_BUFFER_WITH_SB(s));
L
Linus Torvalds 已提交
1901
#ifdef CONFIG_QUOTA
1902 1903
	{
		int j;
1904 1905
		for (j = 0; j < MAXQUOTAS; j++)
			kfree(qf_names[j]);
1906
	}
L
Linus Torvalds 已提交
1907
#endif
1908
	kfree(sbi);
L
Linus Torvalds 已提交
1909

1910 1911
	s->s_fs_info = NULL;
	return errval;
L
Linus Torvalds 已提交
1912 1913
}

1914
static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf)
L
Linus Torvalds 已提交
1915
{
1916
	struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(dentry->d_sb);
1917 1918 1919 1920 1921

	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;
1922
	buf->f_bsize = dentry->d_sb->s_blocksize;
1923 1924
	/* changed to accommodate gcc folks. */
	buf->f_type = REISERFS_SUPER_MAGIC;
1925 1926 1927 1928
	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);

1929
	return 0;
L
Linus Torvalds 已提交
1930 1931 1932 1933 1934
}

#ifdef CONFIG_QUOTA
static int reiserfs_write_dquot(struct dquot *dquot)
{
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
	struct reiserfs_transaction_handle th;
	int ret, err;

	reiserfs_write_lock(dquot->dq_sb);
	ret =
	    journal_begin(&th, dquot->dq_sb,
			  REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
	if (ret)
		goto out;
	ret = dquot_commit(dquot);
	err =
	    journal_end(&th, dquot->dq_sb,
			REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
	if (!ret && err)
		ret = err;
      out:
	reiserfs_write_unlock(dquot->dq_sb);
	return ret;
L
Linus Torvalds 已提交
1953 1954 1955 1956
}

static int reiserfs_acquire_dquot(struct dquot *dquot)
{
1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974
	struct reiserfs_transaction_handle th;
	int ret, err;

	reiserfs_write_lock(dquot->dq_sb);
	ret =
	    journal_begin(&th, dquot->dq_sb,
			  REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
	if (ret)
		goto out;
	ret = dquot_acquire(dquot);
	err =
	    journal_end(&th, dquot->dq_sb,
			REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
	if (!ret && err)
		ret = err;
      out:
	reiserfs_write_unlock(dquot->dq_sb);
	return ret;
L
Linus Torvalds 已提交
1975 1976 1977 1978
}

static int reiserfs_release_dquot(struct dquot *dquot)
{
1979 1980 1981 1982 1983 1984 1985
	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));
J
Jan Kara 已提交
1986 1987 1988
	if (ret) {
		/* Release dquot anyway to avoid endless cycle in dqput() */
		dquot_release(dquot);
1989
		goto out;
J
Jan Kara 已提交
1990
	}
1991 1992 1993 1994 1995 1996 1997 1998 1999
	ret = dquot_release(dquot);
	err =
	    journal_end(&th, dquot->dq_sb,
			REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
	if (!ret && err)
		ret = err;
      out:
	reiserfs_write_unlock(dquot->dq_sb);
	return ret;
L
Linus Torvalds 已提交
2000 2001 2002 2003
}

static int reiserfs_mark_dquot_dirty(struct dquot *dquot)
{
2004
	/* Are we journaling quotas? */
2005 2006 2007 2008 2009 2010
	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 已提交
2011 2012 2013 2014
}

static int reiserfs_write_info(struct super_block *sb, int type)
{
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
	struct reiserfs_transaction_handle th;
	int ret, err;

	/* Data block + inode block */
	reiserfs_write_lock(sb);
	ret = journal_begin(&th, sb, 2);
	if (ret)
		goto out;
	ret = dquot_commit_info(sb, type);
	err = journal_end(&th, sb, 2);
	if (!ret && err)
		ret = err;
      out:
	reiserfs_write_unlock(sb);
	return ret;
L
Linus Torvalds 已提交
2030 2031 2032
}

/*
2033
 * Turn on quotas during mount time - we need to find the quota file and such...
L
Linus Torvalds 已提交
2034 2035 2036
 */
static int reiserfs_quota_on_mount(struct super_block *sb, int type)
{
2037 2038
	return dquot_quota_on_mount(sb, REISERFS_SB(sb)->s_qf_names[type],
					REISERFS_SB(sb)->s_jquota_fmt, type);
L
Linus Torvalds 已提交
2039 2040 2041 2042 2043
}

/*
 * Standard function to be called on quota_on
 */
2044
static int reiserfs_quota_on(struct super_block *sb, int type, int format_id,
2045
			     char *name)
L
Linus Torvalds 已提交
2046
{
2047
	int err;
2048
	struct path path;
J
Jan Kara 已提交
2049
	struct inode *inode;
2050
	struct reiserfs_transaction_handle th;
2051 2052 2053

	if (!(REISERFS_SB(sb)->s_mount_opt & (1 << REISERFS_QUOTA)))
		return -EINVAL;
2054

2055
	err = kern_path(name, LOOKUP_FOLLOW, &path);
2056 2057 2058
	if (err)
		return err;
	/* Quotafile not on the same filesystem? */
2059
	if (path.mnt->mnt_sb != sb) {
2060 2061
		err = -EXDEV;
		goto out;
2062
	}
2063
	inode = path.dentry->d_inode;
2064
	/* We must not pack tails for quota files on reiserfs for quota IO to work */
J
Jan Kara 已提交
2065 2066 2067
	if (!(REISERFS_I(inode)->i_flags & i_nopack_mask)) {
		err = reiserfs_unpack(inode, NULL);
		if (err) {
J
Jeff Mahoney 已提交
2068 2069
			reiserfs_warning(sb, "super-6520",
				"Unpacking tail of quota file failed"
J
Jan Kara 已提交
2070
				" (%d). Cannot turn on quotas.", err);
2071 2072
			err = -EINVAL;
			goto out;
J
Jan Kara 已提交
2073 2074
		}
		mark_inode_dirty(inode);
2075
	}
2076 2077 2078
	/* Journaling quota? */
	if (REISERFS_SB(sb)->s_qf_names[type]) {
		/* Quotafile not of fs root? */
2079
		if (path.dentry->d_parent != sb->s_root)
J
Jeff Mahoney 已提交
2080 2081
			reiserfs_warning(sb, "super-6521",
				 "Quota file not on filesystem root. "
2082
				 "Journalled quota will not work.");
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
	}

	/*
	 * 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)
2093
			goto out;
2094 2095
		err = journal_end_sync(&th, sb, 1);
		if (err)
2096
			goto out;
2097
	}
2098
	err = dquot_quota_on_path(sb, type, format_id, &path);
2099
out:
2100
	path_put(&path);
2101
	return err;
L
Linus Torvalds 已提交
2102 2103 2104 2105 2106 2107 2108 2109 2110
}

/* Read data from quotafile - avoid pagecache and such because we cannot afford
 * acquiring the locks... As quota files are never truncated and quota code
 * itself serializes the operations (and noone else should touch the files)
 * we don't have to be afraid of races */
static ssize_t reiserfs_quota_read(struct super_block *sb, int type, char *data,
				   size_t len, loff_t off)
{
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148
	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;
		/* Quota files are without tails so we can safely use this function */
		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 已提交
2149 2150 2151 2152 2153 2154 2155
}

/* Write to quotafile (we know the transaction is already started and has
 * enough credits) */
static ssize_t reiserfs_quota_write(struct super_block *sb, int type,
				    const char *data, size_t len, loff_t off)
{
2156 2157 2158 2159 2160 2161 2162
	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 已提交
2163 2164 2165 2166 2167 2168
	if (!current->journal_info) {
		printk(KERN_WARNING "reiserfs: Quota write (off=%Lu, len=%Lu)"
			" cancelled because transaction is not started.\n",
			(unsigned long long)off, (unsigned long long)len);
		return -EIO;
	}
2169
	mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
	while (towrite > 0) {
		tocopy = sb->s_blocksize - offset < towrite ?
		    sb->s_blocksize - offset : towrite;
		tmp_bh.b_state = 0;
		err = reiserfs_get_block(inode, blk, &tmp_bh, GET_BLOCK_CREATE);
		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);
		reiserfs_prepare_for_journal(sb, bh, 1);
		journal_mark_dirty(current->journal_info, sb, bh);
		if (!journal_quota)
			reiserfs_add_ordered_list(inode, bh);
		brelse(bh);
		offset = 0;
		towrite -= tocopy;
		data += tocopy;
		blk++;
	}
J
Jan Kara 已提交
2200
out:
2201 2202
	if (len == towrite) {
		mutex_unlock(&inode->i_mutex);
2203
		return err;
2204
	}
2205 2206 2207 2208 2209
	if (inode->i_size < off + len - towrite)
		i_size_write(inode, off + len - towrite);
	inode->i_version++;
	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
	mark_inode_dirty(inode);
2210
	mutex_unlock(&inode->i_mutex);
2211
	return len - towrite;
L
Linus Torvalds 已提交
2212 2213 2214 2215
}

#endif

A
Al Viro 已提交
2216
static struct dentry *get_super_block(struct file_system_type *fs_type,
2217
			   int flags, const char *dev_name,
A
Al Viro 已提交
2218
			   void *data)
L
Linus Torvalds 已提交
2219
{
A
Al Viro 已提交
2220
	return mount_bdev(fs_type, flags, dev_name, data, reiserfs_fill_super);
L
Linus Torvalds 已提交
2221 2222
}

2223
static int __init init_reiserfs_fs(void)
L
Linus Torvalds 已提交
2224 2225 2226
{
	int ret;

2227
	if ((ret = init_inodecache())) {
L
Linus Torvalds 已提交
2228 2229 2230
		return ret;
	}

2231
	reiserfs_proc_info_global_init();
L
Linus Torvalds 已提交
2232

2233
	ret = register_filesystem(&reiserfs_fs_type);
L
Linus Torvalds 已提交
2234 2235 2236 2237 2238

	if (ret == 0) {
		return 0;
	}

2239 2240
	reiserfs_proc_info_global_done();
	destroy_inodecache();
L
Linus Torvalds 已提交
2241 2242 2243 2244

	return ret;
}

2245
static void __exit exit_reiserfs_fs(void)
L
Linus Torvalds 已提交
2246
{
2247 2248 2249
	reiserfs_proc_info_global_done();
	unregister_filesystem(&reiserfs_fs_type);
	destroy_inodecache();
L
Linus Torvalds 已提交
2250 2251 2252
}

struct file_system_type reiserfs_fs_type = {
2253 2254
	.owner = THIS_MODULE,
	.name = "reiserfs",
A
Al Viro 已提交
2255
	.mount = get_super_block,
2256
	.kill_sb = reiserfs_kill_sb,
2257
	.fs_flags = FS_REQUIRES_DEV,
L
Linus Torvalds 已提交
2258 2259
};

2260 2261 2262
MODULE_DESCRIPTION("ReiserFS journaled filesystem");
MODULE_AUTHOR("Hans Reiser <reiser@namesys.com>");
MODULE_LICENSE("GPL");
L
Linus Torvalds 已提交
2263

2264 2265
module_init(init_reiserfs_fs);
module_exit(exit_reiserfs_fs);