1/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 *
4 * Trivial changes by Alan Cox to add the LFS fixes
5 *
6 * Trivial Changes:
7 * Rights granted to Hans Reiser to redistribute under other terms providing
8 * he accepts all liability including but not limited to patent, fitness
9 * for purpose, and direct or indirect claims arising from failure to perform.
10 *
11 * NO WARRANTY
12 */
13
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/vmalloc.h>
17#include <linux/time.h>
18#include <linux/uaccess.h>
19#include "reiserfs.h"
20#include "acl.h"
21#include "xattr.h"
22#include <linux/init.h>
23#include <linux/blkdev.h>
24#include <linux/buffer_head.h>
25#include <linux/exportfs.h>
26#include <linux/quotaops.h>
27#include <linux/vfs.h>
28#include <linux/mount.h>
29#include <linux/namei.h>
30#include <linux/crc32.h>
31#include <linux/seq_file.h>
32
33struct file_system_type reiserfs_fs_type;
34
35static const char reiserfs_3_5_magic_string[] = REISERFS_SUPER_MAGIC_STRING;
36static const char reiserfs_3_6_magic_string[] = REISER2FS_SUPER_MAGIC_STRING;
37static const char reiserfs_jr_magic_string[] = REISER2FS_JR_SUPER_MAGIC_STRING;
38
39int is_reiserfs_3_5(struct reiserfs_super_block *rs)
40{
41	return !strncmp(rs->s_v1.s_magic, reiserfs_3_5_magic_string,
42			strlen(reiserfs_3_5_magic_string));
43}
44
45int is_reiserfs_3_6(struct reiserfs_super_block *rs)
46{
47	return !strncmp(rs->s_v1.s_magic, reiserfs_3_6_magic_string,
48			strlen(reiserfs_3_6_magic_string));
49}
50
51int is_reiserfs_jr(struct reiserfs_super_block *rs)
52{
53	return !strncmp(rs->s_v1.s_magic, reiserfs_jr_magic_string,
54			strlen(reiserfs_jr_magic_string));
55}
56
57static int is_any_reiserfs_magic_string(struct reiserfs_super_block *rs)
58{
59	return (is_reiserfs_3_5(rs) || is_reiserfs_3_6(rs) ||
60		is_reiserfs_jr(rs));
61}
62
63static int reiserfs_remount(struct super_block *s, int *flags, char *data);
64static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf);
65
66static int reiserfs_sync_fs(struct super_block *s, int wait)
67{
68	struct reiserfs_transaction_handle th;
69
70	/*
71	 * Writeback quota in non-journalled quota case - journalled quota has
72	 * no dirty dquots
73	 */
74	dquot_writeback_dquots(s, -1);
75	reiserfs_write_lock(s);
76	if (!journal_begin(&th, s, 1))
77		if (!journal_end_sync(&th))
78			reiserfs_flush_old_commits(s);
79	reiserfs_write_unlock(s);
80	return 0;
81}
82
83static void flush_old_commits(struct work_struct *work)
84{
85	struct reiserfs_sb_info *sbi;
86	struct super_block *s;
87
88	sbi = container_of(work, struct reiserfs_sb_info, old_work.work);
89	s = sbi->s_journal->j_work_sb;
90
91	spin_lock(&sbi->old_work_lock);
92	sbi->work_queued = 0;
93	spin_unlock(&sbi->old_work_lock);
94
95	reiserfs_sync_fs(s, 1);
96}
97
98void reiserfs_schedule_old_flush(struct super_block *s)
99{
100	struct reiserfs_sb_info *sbi = REISERFS_SB(s);
101	unsigned long delay;
102
103	/*
104	 * Avoid scheduling flush when sb is being shut down. It can race
105	 * with journal shutdown and free still queued delayed work.
106	 */
107	if (s->s_flags & MS_RDONLY || !(s->s_flags & MS_ACTIVE))
108		return;
109
110	spin_lock(&sbi->old_work_lock);
111	if (!sbi->work_queued) {
112		delay = msecs_to_jiffies(dirty_writeback_interval * 10);
113		queue_delayed_work(system_long_wq, &sbi->old_work, delay);
114		sbi->work_queued = 1;
115	}
116	spin_unlock(&sbi->old_work_lock);
117}
118
119static void cancel_old_flush(struct super_block *s)
120{
121	struct reiserfs_sb_info *sbi = REISERFS_SB(s);
122
123	cancel_delayed_work_sync(&REISERFS_SB(s)->old_work);
124	spin_lock(&sbi->old_work_lock);
125	sbi->work_queued = 0;
126	spin_unlock(&sbi->old_work_lock);
127}
128
129static int reiserfs_freeze(struct super_block *s)
130{
131	struct reiserfs_transaction_handle th;
132
133	cancel_old_flush(s);
134
135	reiserfs_write_lock(s);
136	if (!(s->s_flags & MS_RDONLY)) {
137		int err = journal_begin(&th, s, 1);
138		if (err) {
139			reiserfs_block_writes(&th);
140		} else {
141			reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
142						     1);
143			journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
144			reiserfs_block_writes(&th);
145			journal_end_sync(&th);
146		}
147	}
148	reiserfs_write_unlock(s);
149	return 0;
150}
151
152static int reiserfs_unfreeze(struct super_block *s)
153{
154	reiserfs_allow_writes(s);
155	return 0;
156}
157
158extern const struct in_core_key MAX_IN_CORE_KEY;
159
160/*
161 * this is used to delete "save link" when there are no items of a
162 * file it points to. It can either happen if unlink is completed but
163 * "save unlink" removal, or if file has both unlink and truncate
164 * pending and as unlink completes first (because key of "save link"
165 * protecting unlink is bigger that a key lf "save link" which
166 * protects truncate), so there left no items to make truncate
167 * completion on
168 */
169static int remove_save_link_only(struct super_block *s,
170				 struct reiserfs_key *key, int oid_free)
171{
172	struct reiserfs_transaction_handle th;
173	int err;
174
175	/* we are going to do one balancing */
176	err = journal_begin(&th, s, JOURNAL_PER_BALANCE_CNT);
177	if (err)
178		return err;
179
180	reiserfs_delete_solid_item(&th, NULL, key);
181	if (oid_free)
182		/* removals are protected by direct items */
183		reiserfs_release_objectid(&th, le32_to_cpu(key->k_objectid));
184
185	return journal_end(&th);
186}
187
188#ifdef CONFIG_QUOTA
189static int reiserfs_quota_on_mount(struct super_block *, int);
190#endif
191
192/* look for uncompleted unlinks and truncates and complete them */
193static int finish_unfinished(struct super_block *s)
194{
195	INITIALIZE_PATH(path);
196	struct cpu_key max_cpu_key, obj_key;
197	struct reiserfs_key save_link_key, last_inode_key;
198	int retval = 0;
199	struct item_head *ih;
200	struct buffer_head *bh;
201	int item_pos;
202	char *item;
203	int done;
204	struct inode *inode;
205	int truncate;
206#ifdef CONFIG_QUOTA
207	int i;
208	int ms_active_set;
209	int quota_enabled[REISERFS_MAXQUOTAS];
210#endif
211
212	/* compose key to look for "save" links */
213	max_cpu_key.version = KEY_FORMAT_3_5;
214	max_cpu_key.on_disk_key.k_dir_id = ~0U;
215	max_cpu_key.on_disk_key.k_objectid = ~0U;
216	set_cpu_key_k_offset(&max_cpu_key, ~0U);
217	max_cpu_key.key_length = 3;
218
219	memset(&last_inode_key, 0, sizeof(last_inode_key));
220
221#ifdef CONFIG_QUOTA
222	/* Needed for iput() to work correctly and not trash data */
223	if (s->s_flags & MS_ACTIVE) {
224		ms_active_set = 0;
225	} else {
226		ms_active_set = 1;
227		s->s_flags |= MS_ACTIVE;
228	}
229	/* Turn on quotas so that they are updated correctly */
230	for (i = 0; i < REISERFS_MAXQUOTAS; i++) {
231		quota_enabled[i] = 1;
232		if (REISERFS_SB(s)->s_qf_names[i]) {
233			int ret;
234
235			if (sb_has_quota_active(s, i)) {
236				quota_enabled[i] = 0;
237				continue;
238			}
239			ret = reiserfs_quota_on_mount(s, i);
240			if (ret < 0)
241				reiserfs_warning(s, "reiserfs-2500",
242						 "cannot turn on journaled "
243						 "quota: error %d", ret);
244		}
245	}
246#endif
247
248	done = 0;
249	REISERFS_SB(s)->s_is_unlinked_ok = 1;
250	while (!retval) {
251		int depth;
252		retval = search_item(s, &max_cpu_key, &path);
253		if (retval != ITEM_NOT_FOUND) {
254			reiserfs_error(s, "vs-2140",
255				       "search_by_key returned %d", retval);
256			break;
257		}
258
259		bh = get_last_bh(&path);
260		item_pos = get_item_pos(&path);
261		if (item_pos != B_NR_ITEMS(bh)) {
262			reiserfs_warning(s, "vs-2060",
263					 "wrong position found");
264			break;
265		}
266		item_pos--;
267		ih = item_head(bh, item_pos);
268
269		if (le32_to_cpu(ih->ih_key.k_dir_id) != MAX_KEY_OBJECTID)
270			/* there are no "save" links anymore */
271			break;
272
273		save_link_key = ih->ih_key;
274		if (is_indirect_le_ih(ih))
275			truncate = 1;
276		else
277			truncate = 0;
278
279		/* reiserfs_iget needs k_dirid and k_objectid only */
280		item = ih_item_body(bh, ih);
281		obj_key.on_disk_key.k_dir_id = le32_to_cpu(*(__le32 *) item);
282		obj_key.on_disk_key.k_objectid =
283		    le32_to_cpu(ih->ih_key.k_objectid);
284		obj_key.on_disk_key.k_offset = 0;
285		obj_key.on_disk_key.k_type = 0;
286
287		pathrelse(&path);
288
289		inode = reiserfs_iget(s, &obj_key);
290		if (!inode) {
291			/*
292			 * the unlink almost completed, it just did not
293			 * manage to remove "save" link and release objectid
294			 */
295			reiserfs_warning(s, "vs-2180", "iget failed for %K",
296					 &obj_key);
297			retval = remove_save_link_only(s, &save_link_key, 1);
298			continue;
299		}
300
301		if (!truncate && inode->i_nlink) {
302			/* file is not unlinked */
303			reiserfs_warning(s, "vs-2185",
304					 "file %K is not unlinked",
305					 &obj_key);
306			retval = remove_save_link_only(s, &save_link_key, 0);
307			continue;
308		}
309		depth = reiserfs_write_unlock_nested(inode->i_sb);
310		dquot_initialize(inode);
311		reiserfs_write_lock_nested(inode->i_sb, depth);
312
313		if (truncate && S_ISDIR(inode->i_mode)) {
314			/*
315			 * We got a truncate request for a dir which
316			 * is impossible.  The only imaginable way is to
317			 * execute unfinished truncate request then boot
318			 * into old kernel, remove the file and create dir
319			 * with the same key.
320			 */
321			reiserfs_warning(s, "green-2101",
322					 "impossible truncate on a "
323					 "directory %k. Please report",
324					 INODE_PKEY(inode));
325			retval = remove_save_link_only(s, &save_link_key, 0);
326			truncate = 0;
327			iput(inode);
328			continue;
329		}
330
331		if (truncate) {
332			REISERFS_I(inode)->i_flags |=
333			    i_link_saved_truncate_mask;
334			/*
335			 * not completed truncate found. New size was
336			 * committed together with "save" link
337			 */
338			reiserfs_info(s, "Truncating %k to %lld ..",
339				      INODE_PKEY(inode), inode->i_size);
340
341			/* don't update modification time */
342			reiserfs_truncate_file(inode, 0);
343
344			retval = remove_save_link(inode, truncate);
345		} else {
346			REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
347			/* not completed unlink (rmdir) found */
348			reiserfs_info(s, "Removing %k..", INODE_PKEY(inode));
349			if (memcmp(&last_inode_key, INODE_PKEY(inode),
350					sizeof(last_inode_key))){
351				last_inode_key = *INODE_PKEY(inode);
352				/* removal gets completed in iput */
353				retval = 0;
354			} else {
355				reiserfs_warning(s, "super-2189", "Dead loop "
356						 "in finish_unfinished "
357						 "detected, just remove "
358						 "save link\n");
359				retval = remove_save_link_only(s,
360							&save_link_key, 0);
361			}
362		}
363
364		iput(inode);
365		printk("done\n");
366		done++;
367	}
368	REISERFS_SB(s)->s_is_unlinked_ok = 0;
369
370#ifdef CONFIG_QUOTA
371	/* Turn quotas off */
372	reiserfs_write_unlock(s);
373	for (i = 0; i < REISERFS_MAXQUOTAS; i++) {
374		if (sb_dqopt(s)->files[i] && quota_enabled[i])
375			dquot_quota_off(s, i);
376	}
377	reiserfs_write_lock(s);
378	if (ms_active_set)
379		/* Restore the flag back */
380		s->s_flags &= ~MS_ACTIVE;
381#endif
382	pathrelse(&path);
383	if (done)
384		reiserfs_info(s, "There were %d uncompleted unlinks/truncates. "
385			      "Completed\n", done);
386	return retval;
387}
388
389/*
390 * to protect file being unlinked from getting lost we "safe" link files
391 * being unlinked. This link will be deleted in the same transaction with last
392 * item of file. mounting the filesystem we scan all these links and remove
393 * files which almost got lost
394 */
395void add_save_link(struct reiserfs_transaction_handle *th,
396		   struct inode *inode, int truncate)
397{
398	INITIALIZE_PATH(path);
399	int retval;
400	struct cpu_key key;
401	struct item_head ih;
402	__le32 link;
403
404	BUG_ON(!th->t_trans_id);
405
406	/* file can only get one "save link" of each kind */
407	RFALSE(truncate &&
408	       (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask),
409	       "saved link already exists for truncated inode %lx",
410	       (long)inode->i_ino);
411	RFALSE(!truncate &&
412	       (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask),
413	       "saved link already exists for unlinked inode %lx",
414	       (long)inode->i_ino);
415
416	/* setup key of "save" link */
417	key.version = KEY_FORMAT_3_5;
418	key.on_disk_key.k_dir_id = MAX_KEY_OBJECTID;
419	key.on_disk_key.k_objectid = inode->i_ino;
420	if (!truncate) {
421		/* unlink, rmdir, rename */
422		set_cpu_key_k_offset(&key, 1 + inode->i_sb->s_blocksize);
423		set_cpu_key_k_type(&key, TYPE_DIRECT);
424
425		/* item head of "safe" link */
426		make_le_item_head(&ih, &key, key.version,
427				  1 + inode->i_sb->s_blocksize, TYPE_DIRECT,
428				  4 /*length */ , 0xffff /*free space */ );
429	} else {
430		/* truncate */
431		if (S_ISDIR(inode->i_mode))
432			reiserfs_warning(inode->i_sb, "green-2102",
433					 "Adding a truncate savelink for "
434					 "a directory %k! Please report",
435					 INODE_PKEY(inode));
436		set_cpu_key_k_offset(&key, 1);
437		set_cpu_key_k_type(&key, TYPE_INDIRECT);
438
439		/* item head of "safe" link */
440		make_le_item_head(&ih, &key, key.version, 1, TYPE_INDIRECT,
441				  4 /*length */ , 0 /*free space */ );
442	}
443	key.key_length = 3;
444
445	/* look for its place in the tree */
446	retval = search_item(inode->i_sb, &key, &path);
447	if (retval != ITEM_NOT_FOUND) {
448		if (retval != -ENOSPC)
449			reiserfs_error(inode->i_sb, "vs-2100",
450				       "search_by_key (%K) returned %d", &key,
451				       retval);
452		pathrelse(&path);
453		return;
454	}
455
456	/* body of "save" link */
457	link = INODE_PKEY(inode)->k_dir_id;
458
459	/* put "save" link into tree, don't charge quota to anyone */
460	retval =
461	    reiserfs_insert_item(th, &path, &key, &ih, NULL, (char *)&link);
462	if (retval) {
463		if (retval != -ENOSPC)
464			reiserfs_error(inode->i_sb, "vs-2120",
465				       "insert_item returned %d", retval);
466	} else {
467		if (truncate)
468			REISERFS_I(inode)->i_flags |=
469			    i_link_saved_truncate_mask;
470		else
471			REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
472	}
473}
474
475/* this opens transaction unlike add_save_link */
476int remove_save_link(struct inode *inode, int truncate)
477{
478	struct reiserfs_transaction_handle th;
479	struct reiserfs_key key;
480	int err;
481
482	/* we are going to do one balancing only */
483	err = journal_begin(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
484	if (err)
485		return err;
486
487	/* setup key of "save" link */
488	key.k_dir_id = cpu_to_le32(MAX_KEY_OBJECTID);
489	key.k_objectid = INODE_PKEY(inode)->k_objectid;
490	if (!truncate) {
491		/* unlink, rmdir, rename */
492		set_le_key_k_offset(KEY_FORMAT_3_5, &key,
493				    1 + inode->i_sb->s_blocksize);
494		set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_DIRECT);
495	} else {
496		/* truncate */
497		set_le_key_k_offset(KEY_FORMAT_3_5, &key, 1);
498		set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_INDIRECT);
499	}
500
501	if ((truncate &&
502	     (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask)) ||
503	    (!truncate &&
504	     (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask)))
505		/* don't take quota bytes from anywhere */
506		reiserfs_delete_solid_item(&th, NULL, &key);
507	if (!truncate) {
508		reiserfs_release_objectid(&th, inode->i_ino);
509		REISERFS_I(inode)->i_flags &= ~i_link_saved_unlink_mask;
510	} else
511		REISERFS_I(inode)->i_flags &= ~i_link_saved_truncate_mask;
512
513	return journal_end(&th);
514}
515
516static void reiserfs_kill_sb(struct super_block *s)
517{
518	if (REISERFS_SB(s)) {
519		reiserfs_proc_info_done(s);
520		/*
521		 * Force any pending inode evictions to occur now. Any
522		 * inodes to be removed that have extended attributes
523		 * associated with them need to clean them up before
524		 * we can release the extended attribute root dentries.
525		 * shrink_dcache_for_umount will BUG if we don't release
526		 * those before it's called so ->put_super is too late.
527		 */
528		shrink_dcache_sb(s);
529
530		dput(REISERFS_SB(s)->xattr_root);
531		REISERFS_SB(s)->xattr_root = NULL;
532		dput(REISERFS_SB(s)->priv_root);
533		REISERFS_SB(s)->priv_root = NULL;
534	}
535
536	kill_block_super(s);
537}
538
539static void reiserfs_put_super(struct super_block *s)
540{
541	struct reiserfs_transaction_handle th;
542	th.t_trans_id = 0;
543
544	dquot_disable(s, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
545
546	reiserfs_write_lock(s);
547
548	/*
549	 * change file system state to current state if it was mounted
550	 * with read-write permissions
551	 */
552	if (!(s->s_flags & MS_RDONLY)) {
553		if (!journal_begin(&th, s, 10)) {
554			reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
555						     1);
556			set_sb_umount_state(SB_DISK_SUPER_BLOCK(s),
557					    REISERFS_SB(s)->s_mount_state);
558			journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
559		}
560	}
561
562	/*
563	 * note, journal_release checks for readonly mount, and can
564	 * decide not to do a journal_end
565	 */
566	journal_release(&th, s);
567
568	reiserfs_free_bitmap_cache(s);
569
570	brelse(SB_BUFFER_WITH_SB(s));
571
572	print_statistics(s);
573
574	if (REISERFS_SB(s)->reserved_blocks != 0) {
575		reiserfs_warning(s, "green-2005", "reserved blocks left %d",
576				 REISERFS_SB(s)->reserved_blocks);
577	}
578
579	reiserfs_write_unlock(s);
580	mutex_destroy(&REISERFS_SB(s)->lock);
581	destroy_workqueue(REISERFS_SB(s)->commit_wq);
582	kfree(s->s_fs_info);
583	s->s_fs_info = NULL;
584}
585
586static struct kmem_cache *reiserfs_inode_cachep;
587
588static struct inode *reiserfs_alloc_inode(struct super_block *sb)
589{
590	struct reiserfs_inode_info *ei;
591	ei = (struct reiserfs_inode_info *)
592	    kmem_cache_alloc(reiserfs_inode_cachep, GFP_KERNEL);
593	if (!ei)
594		return NULL;
595	atomic_set(&ei->openers, 0);
596	mutex_init(&ei->tailpack);
597#ifdef CONFIG_QUOTA
598	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
599#endif
600
601	return &ei->vfs_inode;
602}
603
604static void reiserfs_i_callback(struct rcu_head *head)
605{
606	struct inode *inode = container_of(head, struct inode, i_rcu);
607	kmem_cache_free(reiserfs_inode_cachep, REISERFS_I(inode));
608}
609
610static void reiserfs_destroy_inode(struct inode *inode)
611{
612	call_rcu(&inode->i_rcu, reiserfs_i_callback);
613}
614
615static void init_once(void *foo)
616{
617	struct reiserfs_inode_info *ei = (struct reiserfs_inode_info *)foo;
618
619	INIT_LIST_HEAD(&ei->i_prealloc_list);
620	inode_init_once(&ei->vfs_inode);
621}
622
623static int __init init_inodecache(void)
624{
625	reiserfs_inode_cachep = kmem_cache_create("reiser_inode_cache",
626						  sizeof(struct
627							 reiserfs_inode_info),
628						  0, (SLAB_RECLAIM_ACCOUNT|
629							SLAB_MEM_SPREAD),
630						  init_once);
631	if (reiserfs_inode_cachep == NULL)
632		return -ENOMEM;
633	return 0;
634}
635
636static void destroy_inodecache(void)
637{
638	/*
639	 * Make sure all delayed rcu free inodes are flushed before we
640	 * destroy cache.
641	 */
642	rcu_barrier();
643	kmem_cache_destroy(reiserfs_inode_cachep);
644}
645
646/* we don't mark inodes dirty, we just log them */
647static void reiserfs_dirty_inode(struct inode *inode, int flags)
648{
649	struct reiserfs_transaction_handle th;
650
651	int err = 0;
652
653	if (inode->i_sb->s_flags & MS_RDONLY) {
654		reiserfs_warning(inode->i_sb, "clm-6006",
655				 "writing inode %lu on readonly FS",
656				 inode->i_ino);
657		return;
658	}
659	reiserfs_write_lock(inode->i_sb);
660
661	/*
662	 * this is really only used for atime updates, so they don't have
663	 * to be included in O_SYNC or fsync
664	 */
665	err = journal_begin(&th, inode->i_sb, 1);
666	if (err)
667		goto out;
668
669	reiserfs_update_sd(&th, inode);
670	journal_end(&th);
671
672out:
673	reiserfs_write_unlock(inode->i_sb);
674}
675
676static int reiserfs_show_options(struct seq_file *seq, struct dentry *root)
677{
678	struct super_block *s = root->d_sb;
679	struct reiserfs_journal *journal = SB_JOURNAL(s);
680	long opts = REISERFS_SB(s)->s_mount_opt;
681
682	if (opts & (1 << REISERFS_LARGETAIL))
683		seq_puts(seq, ",tails=on");
684	else if (!(opts & (1 << REISERFS_SMALLTAIL)))
685		seq_puts(seq, ",notail");
686	/* tails=small is default so we don't show it */
687
688	if (!(opts & (1 << REISERFS_BARRIER_FLUSH)))
689		seq_puts(seq, ",barrier=none");
690	/* barrier=flush is default so we don't show it */
691
692	if (opts & (1 << REISERFS_ERROR_CONTINUE))
693		seq_puts(seq, ",errors=continue");
694	else if (opts & (1 << REISERFS_ERROR_PANIC))
695		seq_puts(seq, ",errors=panic");
696	/* errors=ro is default so we don't show it */
697
698	if (opts & (1 << REISERFS_DATA_LOG))
699		seq_puts(seq, ",data=journal");
700	else if (opts & (1 << REISERFS_DATA_WRITEBACK))
701		seq_puts(seq, ",data=writeback");
702	/* data=ordered is default so we don't show it */
703
704	if (opts & (1 << REISERFS_ATTRS))
705		seq_puts(seq, ",attrs");
706
707	if (opts & (1 << REISERFS_XATTRS_USER))
708		seq_puts(seq, ",user_xattr");
709
710	if (opts & (1 << REISERFS_EXPOSE_PRIVROOT))
711		seq_puts(seq, ",expose_privroot");
712
713	if (opts & (1 << REISERFS_POSIXACL))
714		seq_puts(seq, ",acl");
715
716	if (REISERFS_SB(s)->s_jdev)
717		seq_show_option(seq, "jdev", REISERFS_SB(s)->s_jdev);
718
719	if (journal->j_max_commit_age != journal->j_default_max_commit_age)
720		seq_printf(seq, ",commit=%d", journal->j_max_commit_age);
721
722#ifdef CONFIG_QUOTA
723	if (REISERFS_SB(s)->s_qf_names[USRQUOTA])
724		seq_show_option(seq, "usrjquota",
725				REISERFS_SB(s)->s_qf_names[USRQUOTA]);
726	else if (opts & (1 << REISERFS_USRQUOTA))
727		seq_puts(seq, ",usrquota");
728	if (REISERFS_SB(s)->s_qf_names[GRPQUOTA])
729		seq_show_option(seq, "grpjquota",
730				REISERFS_SB(s)->s_qf_names[GRPQUOTA]);
731	else if (opts & (1 << REISERFS_GRPQUOTA))
732		seq_puts(seq, ",grpquota");
733	if (REISERFS_SB(s)->s_jquota_fmt) {
734		if (REISERFS_SB(s)->s_jquota_fmt == QFMT_VFS_OLD)
735			seq_puts(seq, ",jqfmt=vfsold");
736		else if (REISERFS_SB(s)->s_jquota_fmt == QFMT_VFS_V0)
737			seq_puts(seq, ",jqfmt=vfsv0");
738	}
739#endif
740
741	/* Block allocator options */
742	if (opts & (1 << REISERFS_NO_BORDER))
743		seq_puts(seq, ",block-allocator=noborder");
744	if (opts & (1 << REISERFS_NO_UNHASHED_RELOCATION))
745		seq_puts(seq, ",block-allocator=no_unhashed_relocation");
746	if (opts & (1 << REISERFS_HASHED_RELOCATION))
747		seq_puts(seq, ",block-allocator=hashed_relocation");
748	if (opts & (1 << REISERFS_TEST4))
749		seq_puts(seq, ",block-allocator=test4");
750	show_alloc_options(seq, s);
751	return 0;
752}
753
754#ifdef CONFIG_QUOTA
755static ssize_t reiserfs_quota_write(struct super_block *, int, const char *,
756				    size_t, loff_t);
757static ssize_t reiserfs_quota_read(struct super_block *, int, char *, size_t,
758				   loff_t);
759
760static struct dquot **reiserfs_get_dquots(struct inode *inode)
761{
762	return REISERFS_I(inode)->i_dquot;
763}
764#endif
765
766static const struct super_operations reiserfs_sops = {
767	.alloc_inode = reiserfs_alloc_inode,
768	.destroy_inode = reiserfs_destroy_inode,
769	.write_inode = reiserfs_write_inode,
770	.dirty_inode = reiserfs_dirty_inode,
771	.evict_inode = reiserfs_evict_inode,
772	.put_super = reiserfs_put_super,
773	.sync_fs = reiserfs_sync_fs,
774	.freeze_fs = reiserfs_freeze,
775	.unfreeze_fs = reiserfs_unfreeze,
776	.statfs = reiserfs_statfs,
777	.remount_fs = reiserfs_remount,
778	.show_options = reiserfs_show_options,
779#ifdef CONFIG_QUOTA
780	.quota_read = reiserfs_quota_read,
781	.quota_write = reiserfs_quota_write,
782	.get_dquots = reiserfs_get_dquots,
783#endif
784};
785
786#ifdef CONFIG_QUOTA
787#define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
788
789static int reiserfs_write_dquot(struct dquot *);
790static int reiserfs_acquire_dquot(struct dquot *);
791static int reiserfs_release_dquot(struct dquot *);
792static int reiserfs_mark_dquot_dirty(struct dquot *);
793static int reiserfs_write_info(struct super_block *, int);
794static int reiserfs_quota_on(struct super_block *, int, int, struct path *);
795
796static const struct dquot_operations reiserfs_quota_operations = {
797	.write_dquot = reiserfs_write_dquot,
798	.acquire_dquot = reiserfs_acquire_dquot,
799	.release_dquot = reiserfs_release_dquot,
800	.mark_dirty = reiserfs_mark_dquot_dirty,
801	.write_info = reiserfs_write_info,
802	.alloc_dquot	= dquot_alloc,
803	.destroy_dquot	= dquot_destroy,
804};
805
806static const struct quotactl_ops reiserfs_qctl_operations = {
807	.quota_on = reiserfs_quota_on,
808	.quota_off = dquot_quota_off,
809	.quota_sync = dquot_quota_sync,
810	.get_state = dquot_get_state,
811	.set_info = dquot_set_dqinfo,
812	.get_dqblk = dquot_get_dqblk,
813	.set_dqblk = dquot_set_dqblk,
814};
815#endif
816
817static const struct export_operations reiserfs_export_ops = {
818	.encode_fh = reiserfs_encode_fh,
819	.fh_to_dentry = reiserfs_fh_to_dentry,
820	.fh_to_parent = reiserfs_fh_to_parent,
821	.get_parent = reiserfs_get_parent,
822};
823
824/*
825 * this struct is used in reiserfs_getopt () for containing the value for
826 * those mount options that have values rather than being toggles.
827 */
828typedef struct {
829	char *value;
830	/*
831	 * bitmask which is to set on mount_options bitmask
832	 * when this value is found, 0 is no bits are to be changed.
833	 */
834	int setmask;
835	/*
836	 * bitmask which is to clear on mount_options bitmask
837	 * when this value is found, 0 is no bits are to be changed.
838	 * This is applied BEFORE setmask
839	 */
840	int clrmask;
841} arg_desc_t;
842
843/* Set this bit in arg_required to allow empty arguments */
844#define REISERFS_OPT_ALLOWEMPTY 31
845
846/*
847 * this struct is used in reiserfs_getopt() for describing the
848 * set of reiserfs mount options
849 */
850typedef struct {
851	char *option_name;
852
853	/* 0 if argument is not required, not 0 otherwise */
854	int arg_required;
855
856	/* list of values accepted by an option */
857	const arg_desc_t *values;
858
859	/*
860	 * bitmask which is to set on mount_options bitmask
861	 * when this value is found, 0 is no bits are to be changed.
862	 */
863	int setmask;
864
865	/*
866	 * bitmask which is to clear on mount_options bitmask
867	 * when this value is found, 0 is no bits are to be changed.
868	 * This is applied BEFORE setmask
869	 */
870	int clrmask;
871} opt_desc_t;
872
873/* possible values for -o data= */
874static const arg_desc_t logging_mode[] = {
875	{"ordered", 1 << REISERFS_DATA_ORDERED,
876	 (1 << REISERFS_DATA_LOG | 1 << REISERFS_DATA_WRITEBACK)},
877	{"journal", 1 << REISERFS_DATA_LOG,
878	 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_WRITEBACK)},
879	{"writeback", 1 << REISERFS_DATA_WRITEBACK,
880	 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_LOG)},
881	{.value = NULL}
882};
883
884/* possible values for -o barrier= */
885static const arg_desc_t barrier_mode[] = {
886	{"none", 1 << REISERFS_BARRIER_NONE, 1 << REISERFS_BARRIER_FLUSH},
887	{"flush", 1 << REISERFS_BARRIER_FLUSH, 1 << REISERFS_BARRIER_NONE},
888	{.value = NULL}
889};
890
891/*
892 * possible values for "-o block-allocator=" and bits which are to be set in
893 * s_mount_opt of reiserfs specific part of in-core super block
894 */
895static const arg_desc_t balloc[] = {
896	{"noborder", 1 << REISERFS_NO_BORDER, 0},
897	{"border", 0, 1 << REISERFS_NO_BORDER},
898	{"no_unhashed_relocation", 1 << REISERFS_NO_UNHASHED_RELOCATION, 0},
899	{"hashed_relocation", 1 << REISERFS_HASHED_RELOCATION, 0},
900	{"test4", 1 << REISERFS_TEST4, 0},
901	{"notest4", 0, 1 << REISERFS_TEST4},
902	{NULL, 0, 0}
903};
904
905static const arg_desc_t tails[] = {
906	{"on", 1 << REISERFS_LARGETAIL, 1 << REISERFS_SMALLTAIL},
907	{"off", 0, (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
908	{"small", 1 << REISERFS_SMALLTAIL, 1 << REISERFS_LARGETAIL},
909	{NULL, 0, 0}
910};
911
912static const arg_desc_t error_actions[] = {
913	{"panic", 1 << REISERFS_ERROR_PANIC,
914	 (1 << REISERFS_ERROR_RO | 1 << REISERFS_ERROR_CONTINUE)},
915	{"ro-remount", 1 << REISERFS_ERROR_RO,
916	 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_CONTINUE)},
917#ifdef REISERFS_JOURNAL_ERROR_ALLOWS_NO_LOG
918	{"continue", 1 << REISERFS_ERROR_CONTINUE,
919	 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_RO)},
920#endif
921	{NULL, 0, 0},
922};
923
924/*
925 * proceed only one option from a list *cur - string containing of mount
926 * options
927 * opts - array of options which are accepted
928 * opt_arg - if option is found and requires an argument and if it is specifed
929 * in the input - pointer to the argument is stored here
930 * bit_flags - if option requires to set a certain bit - it is set here
931 * return -1 if unknown option is found, opt->arg_required otherwise
932 */
933static int reiserfs_getopt(struct super_block *s, char **cur, opt_desc_t * opts,
934			   char **opt_arg, unsigned long *bit_flags)
935{
936	char *p;
937	/*
938	 * foo=bar,
939	 * ^   ^  ^
940	 * |   |  +-- option_end
941	 * |   +-- arg_start
942	 * +-- option_start
943	 */
944	const opt_desc_t *opt;
945	const arg_desc_t *arg;
946
947	p = *cur;
948
949	/* assume argument cannot contain commas */
950	*cur = strchr(p, ',');
951	if (*cur) {
952		*(*cur) = '\0';
953		(*cur)++;
954	}
955
956	if (!strncmp(p, "alloc=", 6)) {
957		/*
958		 * Ugly special case, probably we should redo options
959		 * parser so that it can understand several arguments for
960		 * some options, also so that it can fill several bitfields
961		 * with option values.
962		 */
963		if (reiserfs_parse_alloc_options(s, p + 6)) {
964			return -1;
965		} else {
966			return 0;
967		}
968	}
969
970	/* for every option in the list */
971	for (opt = opts; opt->option_name; opt++) {
972		if (!strncmp(p, opt->option_name, strlen(opt->option_name))) {
973			if (bit_flags) {
974				if (opt->clrmask ==
975				    (1 << REISERFS_UNSUPPORTED_OPT))
976					reiserfs_warning(s, "super-6500",
977							 "%s not supported.\n",
978							 p);
979				else
980					*bit_flags &= ~opt->clrmask;
981				if (opt->setmask ==
982				    (1 << REISERFS_UNSUPPORTED_OPT))
983					reiserfs_warning(s, "super-6501",
984							 "%s not supported.\n",
985							 p);
986				else
987					*bit_flags |= opt->setmask;
988			}
989			break;
990		}
991	}
992	if (!opt->option_name) {
993		reiserfs_warning(s, "super-6502",
994				 "unknown mount option \"%s\"", p);
995		return -1;
996	}
997
998	p += strlen(opt->option_name);
999	switch (*p) {
1000	case '=':
1001		if (!opt->arg_required) {
1002			reiserfs_warning(s, "super-6503",
1003					 "the option \"%s\" does not "
1004					 "require an argument\n",
1005					 opt->option_name);
1006			return -1;
1007		}
1008		break;
1009
1010	case 0:
1011		if (opt->arg_required) {
1012			reiserfs_warning(s, "super-6504",
1013					 "the option \"%s\" requires an "
1014					 "argument\n", opt->option_name);
1015			return -1;
1016		}
1017		break;
1018	default:
1019		reiserfs_warning(s, "super-6505",
1020				 "head of option \"%s\" is only correct\n",
1021				 opt->option_name);
1022		return -1;
1023	}
1024
1025	/*
1026	 * move to the argument, or to next option if argument is not
1027	 * required
1028	 */
1029	p++;
1030
1031	if (opt->arg_required
1032	    && !(opt->arg_required & (1 << REISERFS_OPT_ALLOWEMPTY))
1033	    && !strlen(p)) {
1034		/* this catches "option=," if not allowed */
1035		reiserfs_warning(s, "super-6506",
1036				 "empty argument for \"%s\"\n",
1037				 opt->option_name);
1038		return -1;
1039	}
1040
1041	if (!opt->values) {
1042		/* *=NULLopt_arg contains pointer to argument */
1043		*opt_arg = p;
1044		return opt->arg_required & ~(1 << REISERFS_OPT_ALLOWEMPTY);
1045	}
1046
1047	/* values possible for this option are listed in opt->values */
1048	for (arg = opt->values; arg->value; arg++) {
1049		if (!strcmp(p, arg->value)) {
1050			if (bit_flags) {
1051				*bit_flags &= ~arg->clrmask;
1052				*bit_flags |= arg->setmask;
1053			}
1054			return opt->arg_required;
1055		}
1056	}
1057
1058	reiserfs_warning(s, "super-6506",
1059			 "bad value \"%s\" for option \"%s\"\n", p,
1060			 opt->option_name);
1061	return -1;
1062}
1063
1064/* returns 0 if something is wrong in option string, 1 - otherwise */
1065static int reiserfs_parse_options(struct super_block *s,
1066
1067				  /* string given via mount's -o */
1068				  char *options,
1069
1070				  /*
1071				   * after the parsing phase, contains the
1072				   * collection of bitflags defining what
1073				   * mount options were selected.
1074				   */
1075				  unsigned long *mount_options,
1076
1077				  /* strtol-ed from NNN of resize=NNN */
1078				  unsigned long *blocks,
1079				  char **jdev_name,
1080				  unsigned int *commit_max_age,
1081				  char **qf_names,
1082				  unsigned int *qfmt)
1083{
1084	int c;
1085	char *arg = NULL;
1086	char *pos;
1087	opt_desc_t opts[] = {
1088		/*
1089		 * Compatibility stuff, so that -o notail for old
1090		 * setups still work
1091		 */
1092		{"tails",.arg_required = 't',.values = tails},
1093		{"notail",.clrmask =
1094		 (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
1095		{"conv",.setmask = 1 << REISERFS_CONVERT},
1096		{"attrs",.setmask = 1 << REISERFS_ATTRS},
1097		{"noattrs",.clrmask = 1 << REISERFS_ATTRS},
1098		{"expose_privroot", .setmask = 1 << REISERFS_EXPOSE_PRIVROOT},
1099#ifdef CONFIG_REISERFS_FS_XATTR
1100		{"user_xattr",.setmask = 1 << REISERFS_XATTRS_USER},
1101		{"nouser_xattr",.clrmask = 1 << REISERFS_XATTRS_USER},
1102#else
1103		{"user_xattr",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
1104		{"nouser_xattr",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
1105#endif
1106#ifdef CONFIG_REISERFS_FS_POSIX_ACL
1107		{"acl",.setmask = 1 << REISERFS_POSIXACL},
1108		{"noacl",.clrmask = 1 << REISERFS_POSIXACL},
1109#else
1110		{"acl",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
1111		{"noacl",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
1112#endif
1113		{.option_name = "nolog"},
1114		{"replayonly",.setmask = 1 << REPLAYONLY},
1115		{"block-allocator",.arg_required = 'a',.values = balloc},
1116		{"data",.arg_required = 'd',.values = logging_mode},
1117		{"barrier",.arg_required = 'b',.values = barrier_mode},
1118		{"resize",.arg_required = 'r',.values = NULL},
1119		{"jdev",.arg_required = 'j',.values = NULL},
1120		{"nolargeio",.arg_required = 'w',.values = NULL},
1121		{"commit",.arg_required = 'c',.values = NULL},
1122		{"usrquota",.setmask = 1 << REISERFS_USRQUOTA},
1123		{"grpquota",.setmask = 1 << REISERFS_GRPQUOTA},
1124		{"noquota",.clrmask = 1 << REISERFS_USRQUOTA | 1 << REISERFS_GRPQUOTA},
1125		{"errors",.arg_required = 'e',.values = error_actions},
1126		{"usrjquota",.arg_required =
1127		 'u' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
1128		{"grpjquota",.arg_required =
1129		 'g' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
1130		{"jqfmt",.arg_required = 'f',.values = NULL},
1131		{.option_name = NULL}
1132	};
1133
1134	*blocks = 0;
1135	if (!options || !*options)
1136		/*
1137		 * use default configuration: create tails, journaling on, no
1138		 * conversion to newest format
1139		 */
1140		return 1;
1141
1142	for (pos = options; pos;) {
1143		c = reiserfs_getopt(s, &pos, opts, &arg, mount_options);
1144		if (c == -1)
1145			/* wrong option is given */
1146			return 0;
1147
1148		if (c == 'r') {
1149			char *p;
1150
1151			p = NULL;
1152			/* "resize=NNN" or "resize=auto" */
1153
1154			if (!strcmp(arg, "auto")) {
1155				/* From JFS code, to auto-get the size. */
1156				*blocks =
1157				    s->s_bdev->bd_inode->i_size >> s->
1158				    s_blocksize_bits;
1159			} else {
1160				*blocks = simple_strtoul(arg, &p, 0);
1161				if (*p != '\0') {
1162					/* NNN does not look like a number */
1163					reiserfs_warning(s, "super-6507",
1164							 "bad value %s for "
1165							 "-oresize\n", arg);
1166					return 0;
1167				}
1168			}
1169		}
1170
1171		if (c == 'c') {
1172			char *p = NULL;
1173			unsigned long val = simple_strtoul(arg, &p, 0);
1174			/* commit=NNN (time in seconds) */
1175			if (*p != '\0' || val >= (unsigned int)-1) {
1176				reiserfs_warning(s, "super-6508",
1177						 "bad value %s for -ocommit\n",
1178						 arg);
1179				return 0;
1180			}
1181			*commit_max_age = (unsigned int)val;
1182		}
1183
1184		if (c == 'w') {
1185			reiserfs_warning(s, "super-6509", "nolargeio option "
1186					 "is no longer supported");
1187			return 0;
1188		}
1189
1190		if (c == 'j') {
1191			if (arg && *arg && jdev_name) {
1192				/* Hm, already assigned? */
1193				if (*jdev_name) {
1194					reiserfs_warning(s, "super-6510",
1195							 "journal device was "
1196							 "already specified to "
1197							 "be %s", *jdev_name);
1198					return 0;
1199				}
1200				*jdev_name = arg;
1201			}
1202		}
1203#ifdef CONFIG_QUOTA
1204		if (c == 'u' || c == 'g') {
1205			int qtype = c == 'u' ? USRQUOTA : GRPQUOTA;
1206
1207			if (sb_any_quota_loaded(s) &&
1208			    (!*arg != !REISERFS_SB(s)->s_qf_names[qtype])) {
1209				reiserfs_warning(s, "super-6511",
1210						 "cannot change journaled "
1211						 "quota options when quota "
1212						 "turned on.");
1213				return 0;
1214			}
1215			if (*arg) {	/* Some filename specified? */
1216				if (REISERFS_SB(s)->s_qf_names[qtype]
1217				    && strcmp(REISERFS_SB(s)->s_qf_names[qtype],
1218					      arg)) {
1219					reiserfs_warning(s, "super-6512",
1220							 "%s quota file "
1221							 "already specified.",
1222							 QTYPE2NAME(qtype));
1223					return 0;
1224				}
1225				if (strchr(arg, '/')) {
1226					reiserfs_warning(s, "super-6513",
1227							 "quotafile must be "
1228							 "on filesystem root.");
1229					return 0;
1230				}
1231				qf_names[qtype] = kstrdup(arg, GFP_KERNEL);
1232				if (!qf_names[qtype]) {
1233					reiserfs_warning(s, "reiserfs-2502",
1234							 "not enough memory "
1235							 "for storing "
1236							 "quotafile name.");
1237					return 0;
1238				}
1239				if (qtype == USRQUOTA)
1240					*mount_options |= 1 << REISERFS_USRQUOTA;
1241				else
1242					*mount_options |= 1 << REISERFS_GRPQUOTA;
1243			} else {
1244				if (qf_names[qtype] !=
1245				    REISERFS_SB(s)->s_qf_names[qtype])
1246					kfree(qf_names[qtype]);
1247				qf_names[qtype] = NULL;
1248				if (qtype == USRQUOTA)
1249					*mount_options &= ~(1 << REISERFS_USRQUOTA);
1250				else
1251					*mount_options &= ~(1 << REISERFS_GRPQUOTA);
1252			}
1253		}
1254		if (c == 'f') {
1255			if (!strcmp(arg, "vfsold"))
1256				*qfmt = QFMT_VFS_OLD;
1257			else if (!strcmp(arg, "vfsv0"))
1258				*qfmt = QFMT_VFS_V0;
1259			else {
1260				reiserfs_warning(s, "super-6514",
1261						 "unknown quota format "
1262						 "specified.");
1263				return 0;
1264			}
1265			if (sb_any_quota_loaded(s) &&
1266			    *qfmt != REISERFS_SB(s)->s_jquota_fmt) {
1267				reiserfs_warning(s, "super-6515",
1268						 "cannot change journaled "
1269						 "quota options when quota "
1270						 "turned on.");
1271				return 0;
1272			}
1273		}
1274#else
1275		if (c == 'u' || c == 'g' || c == 'f') {
1276			reiserfs_warning(s, "reiserfs-2503", "journaled "
1277					 "quota options not supported.");
1278			return 0;
1279		}
1280#endif
1281	}
1282
1283#ifdef CONFIG_QUOTA
1284	if (!REISERFS_SB(s)->s_jquota_fmt && !*qfmt
1285	    && (qf_names[USRQUOTA] || qf_names[GRPQUOTA])) {
1286		reiserfs_warning(s, "super-6515",
1287				 "journaled quota format not specified.");
1288		return 0;
1289	}
1290	if ((!(*mount_options & (1 << REISERFS_USRQUOTA)) &&
1291	       sb_has_quota_loaded(s, USRQUOTA)) ||
1292	    (!(*mount_options & (1 << REISERFS_GRPQUOTA)) &&
1293	       sb_has_quota_loaded(s, GRPQUOTA))) {
1294		reiserfs_warning(s, "super-6516", "quota options must "
1295				 "be present when quota is turned on.");
1296		return 0;
1297	}
1298#endif
1299
1300	return 1;
1301}
1302
1303static void switch_data_mode(struct super_block *s, unsigned long mode)
1304{
1305	REISERFS_SB(s)->s_mount_opt &= ~((1 << REISERFS_DATA_LOG) |
1306					 (1 << REISERFS_DATA_ORDERED) |
1307					 (1 << REISERFS_DATA_WRITEBACK));
1308	REISERFS_SB(s)->s_mount_opt |= (1 << mode);
1309}
1310
1311static void handle_data_mode(struct super_block *s, unsigned long mount_options)
1312{
1313	if (mount_options & (1 << REISERFS_DATA_LOG)) {
1314		if (!reiserfs_data_log(s)) {
1315			switch_data_mode(s, REISERFS_DATA_LOG);
1316			reiserfs_info(s, "switching to journaled data mode\n");
1317		}
1318	} else if (mount_options & (1 << REISERFS_DATA_ORDERED)) {
1319		if (!reiserfs_data_ordered(s)) {
1320			switch_data_mode(s, REISERFS_DATA_ORDERED);
1321			reiserfs_info(s, "switching to ordered data mode\n");
1322		}
1323	} else if (mount_options & (1 << REISERFS_DATA_WRITEBACK)) {
1324		if (!reiserfs_data_writeback(s)) {
1325			switch_data_mode(s, REISERFS_DATA_WRITEBACK);
1326			reiserfs_info(s, "switching to writeback data mode\n");
1327		}
1328	}
1329}
1330
1331static void handle_barrier_mode(struct super_block *s, unsigned long bits)
1332{
1333	int flush = (1 << REISERFS_BARRIER_FLUSH);
1334	int none = (1 << REISERFS_BARRIER_NONE);
1335	int all_barrier = flush | none;
1336
1337	if (bits & all_barrier) {
1338		REISERFS_SB(s)->s_mount_opt &= ~all_barrier;
1339		if (bits & flush) {
1340			REISERFS_SB(s)->s_mount_opt |= flush;
1341			printk("reiserfs: enabling write barrier flush mode\n");
1342		} else if (bits & none) {
1343			REISERFS_SB(s)->s_mount_opt |= none;
1344			printk("reiserfs: write barriers turned off\n");
1345		}
1346	}
1347}
1348
1349static void handle_attrs(struct super_block *s)
1350{
1351	struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(s);
1352
1353	if (reiserfs_attrs(s)) {
1354		if (old_format_only(s)) {
1355			reiserfs_warning(s, "super-6517", "cannot support "
1356					 "attributes on 3.5.x disk format");
1357			REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
1358			return;
1359		}
1360		if (!(le32_to_cpu(rs->s_flags) & reiserfs_attrs_cleared)) {
1361			reiserfs_warning(s, "super-6518", "cannot support "
1362					 "attributes until flag is set in "
1363					 "super-block");
1364			REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
1365		}
1366	}
1367}
1368
1369#ifdef CONFIG_QUOTA
1370static void handle_quota_files(struct super_block *s, char **qf_names,
1371			       unsigned int *qfmt)
1372{
1373	int i;
1374
1375	for (i = 0; i < REISERFS_MAXQUOTAS; i++) {
1376		if (qf_names[i] != REISERFS_SB(s)->s_qf_names[i])
1377			kfree(REISERFS_SB(s)->s_qf_names[i]);
1378		REISERFS_SB(s)->s_qf_names[i] = qf_names[i];
1379	}
1380	if (*qfmt)
1381		REISERFS_SB(s)->s_jquota_fmt = *qfmt;
1382}
1383#endif
1384
1385static int reiserfs_remount(struct super_block *s, int *mount_flags, char *arg)
1386{
1387	struct reiserfs_super_block *rs;
1388	struct reiserfs_transaction_handle th;
1389	unsigned long blocks;
1390	unsigned long mount_options = REISERFS_SB(s)->s_mount_opt;
1391	unsigned long safe_mask = 0;
1392	unsigned int commit_max_age = (unsigned int)-1;
1393	struct reiserfs_journal *journal = SB_JOURNAL(s);
1394	char *new_opts = kstrdup(arg, GFP_KERNEL);
1395	int err;
1396	char *qf_names[REISERFS_MAXQUOTAS];
1397	unsigned int qfmt = 0;
1398#ifdef CONFIG_QUOTA
1399	int i;
1400#endif
1401
1402	sync_filesystem(s);
1403	reiserfs_write_lock(s);
1404
1405#ifdef CONFIG_QUOTA
1406	memcpy(qf_names, REISERFS_SB(s)->s_qf_names, sizeof(qf_names));
1407#endif
1408
1409	rs = SB_DISK_SUPER_BLOCK(s);
1410
1411	if (!reiserfs_parse_options
1412	    (s, arg, &mount_options, &blocks, NULL, &commit_max_age,
1413	    qf_names, &qfmt)) {
1414#ifdef CONFIG_QUOTA
1415		for (i = 0; i < REISERFS_MAXQUOTAS; i++)
1416			if (qf_names[i] != REISERFS_SB(s)->s_qf_names[i])
1417				kfree(qf_names[i]);
1418#endif
1419		err = -EINVAL;
1420		goto out_err_unlock;
1421	}
1422#ifdef CONFIG_QUOTA
1423	handle_quota_files(s, qf_names, &qfmt);
1424#endif
1425
1426	handle_attrs(s);
1427
1428	/* Add options that are safe here */
1429	safe_mask |= 1 << REISERFS_SMALLTAIL;
1430	safe_mask |= 1 << REISERFS_LARGETAIL;
1431	safe_mask |= 1 << REISERFS_NO_BORDER;
1432	safe_mask |= 1 << REISERFS_NO_UNHASHED_RELOCATION;
1433	safe_mask |= 1 << REISERFS_HASHED_RELOCATION;
1434	safe_mask |= 1 << REISERFS_TEST4;
1435	safe_mask |= 1 << REISERFS_ATTRS;
1436	safe_mask |= 1 << REISERFS_XATTRS_USER;
1437	safe_mask |= 1 << REISERFS_POSIXACL;
1438	safe_mask |= 1 << REISERFS_BARRIER_FLUSH;
1439	safe_mask |= 1 << REISERFS_BARRIER_NONE;
1440	safe_mask |= 1 << REISERFS_ERROR_RO;
1441	safe_mask |= 1 << REISERFS_ERROR_CONTINUE;
1442	safe_mask |= 1 << REISERFS_ERROR_PANIC;
1443	safe_mask |= 1 << REISERFS_USRQUOTA;
1444	safe_mask |= 1 << REISERFS_GRPQUOTA;
1445
1446	/*
1447	 * Update the bitmask, taking care to keep
1448	 * the bits we're not allowed to change here
1449	 */
1450	REISERFS_SB(s)->s_mount_opt =
1451	    (REISERFS_SB(s)->
1452	     s_mount_opt & ~safe_mask) | (mount_options & safe_mask);
1453
1454	if (commit_max_age != 0 && commit_max_age != (unsigned int)-1) {
1455		journal->j_max_commit_age = commit_max_age;
1456		journal->j_max_trans_age = commit_max_age;
1457	} else if (commit_max_age == 0) {
1458		/* 0 means restore defaults. */
1459		journal->j_max_commit_age = journal->j_default_max_commit_age;
1460		journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
1461	}
1462
1463	if (blocks) {
1464		err = reiserfs_resize(s, blocks);
1465		if (err != 0)
1466			goto out_err_unlock;
1467	}
1468
1469	if (*mount_flags & MS_RDONLY) {
1470		reiserfs_write_unlock(s);
1471		reiserfs_xattr_init(s, *mount_flags);
1472		/* remount read-only */
1473		if (s->s_flags & MS_RDONLY)
1474			/* it is read-only already */
1475			goto out_ok_unlocked;
1476
1477		err = dquot_suspend(s, -1);
1478		if (err < 0)
1479			goto out_err;
1480
1481		/* try to remount file system with read-only permissions */
1482		if (sb_umount_state(rs) == REISERFS_VALID_FS
1483		    || REISERFS_SB(s)->s_mount_state != REISERFS_VALID_FS) {
1484			goto out_ok_unlocked;
1485		}
1486
1487		reiserfs_write_lock(s);
1488
1489		err = journal_begin(&th, s, 10);
1490		if (err)
1491			goto out_err_unlock;
1492
1493		/* Mounting a rw partition read-only. */
1494		reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1495		set_sb_umount_state(rs, REISERFS_SB(s)->s_mount_state);
1496		journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
1497	} else {
1498		/* remount read-write */
1499		if (!(s->s_flags & MS_RDONLY)) {
1500			reiserfs_write_unlock(s);
1501			reiserfs_xattr_init(s, *mount_flags);
1502			goto out_ok_unlocked;	/* We are read-write already */
1503		}
1504
1505		if (reiserfs_is_journal_aborted(journal)) {
1506			err = journal->j_errno;
1507			goto out_err_unlock;
1508		}
1509
1510		handle_data_mode(s, mount_options);
1511		handle_barrier_mode(s, mount_options);
1512		REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1513
1514		/* now it is safe to call journal_begin */
1515		s->s_flags &= ~MS_RDONLY;
1516		err = journal_begin(&th, s, 10);
1517		if (err)
1518			goto out_err_unlock;
1519
1520		/* Mount a partition which is read-only, read-write */
1521		reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1522		REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1523		s->s_flags &= ~MS_RDONLY;
1524		set_sb_umount_state(rs, REISERFS_ERROR_FS);
1525		if (!old_format_only(s))
1526			set_sb_mnt_count(rs, sb_mnt_count(rs) + 1);
1527		/* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
1528		journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
1529		REISERFS_SB(s)->s_mount_state = REISERFS_VALID_FS;
1530	}
1531	/* this will force a full flush of all journal lists */
1532	SB_JOURNAL(s)->j_must_wait = 1;
1533	err = journal_end(&th);
1534	if (err)
1535		goto out_err_unlock;
1536
1537	reiserfs_write_unlock(s);
1538	if (!(*mount_flags & MS_RDONLY)) {
1539		dquot_resume(s, -1);
1540		reiserfs_write_lock(s);
1541		finish_unfinished(s);
1542		reiserfs_write_unlock(s);
1543		reiserfs_xattr_init(s, *mount_flags);
1544	}
1545
1546out_ok_unlocked:
1547	replace_mount_options(s, new_opts);
1548	return 0;
1549
1550out_err_unlock:
1551	reiserfs_write_unlock(s);
1552out_err:
1553	kfree(new_opts);
1554	return err;
1555}
1556
1557static int read_super_block(struct super_block *s, int offset)
1558{
1559	struct buffer_head *bh;
1560	struct reiserfs_super_block *rs;
1561	int fs_blocksize;
1562
1563	bh = sb_bread(s, offset / s->s_blocksize);
1564	if (!bh) {
1565		reiserfs_warning(s, "sh-2006",
1566				 "bread failed (dev %s, block %lu, size %lu)",
1567				 s->s_id, offset / s->s_blocksize,
1568				 s->s_blocksize);
1569		return 1;
1570	}
1571
1572	rs = (struct reiserfs_super_block *)bh->b_data;
1573	if (!is_any_reiserfs_magic_string(rs)) {
1574		brelse(bh);
1575		return 1;
1576	}
1577	/*
1578	 * ok, reiserfs signature (old or new) found in at the given offset
1579	 */
1580	fs_blocksize = sb_blocksize(rs);
1581	brelse(bh);
1582	sb_set_blocksize(s, fs_blocksize);
1583
1584	bh = sb_bread(s, offset / s->s_blocksize);
1585	if (!bh) {
1586		reiserfs_warning(s, "sh-2007",
1587				 "bread failed (dev %s, block %lu, size %lu)",
1588				 s->s_id, offset / s->s_blocksize,
1589				 s->s_blocksize);
1590		return 1;
1591	}
1592
1593	rs = (struct reiserfs_super_block *)bh->b_data;
1594	if (sb_blocksize(rs) != s->s_blocksize) {
1595		reiserfs_warning(s, "sh-2011", "can't find a reiserfs "
1596				 "filesystem on (dev %s, block %llu, size %lu)",
1597				 s->s_id,
1598				 (unsigned long long)bh->b_blocknr,
1599				 s->s_blocksize);
1600		brelse(bh);
1601		return 1;
1602	}
1603
1604	if (rs->s_v1.s_root_block == cpu_to_le32(-1)) {
1605		brelse(bh);
1606		reiserfs_warning(s, "super-6519", "Unfinished reiserfsck "
1607				 "--rebuild-tree run detected. Please run\n"
1608				 "reiserfsck --rebuild-tree and wait for a "
1609				 "completion. If that fails\n"
1610				 "get newer reiserfsprogs package");
1611		return 1;
1612	}
1613
1614	SB_BUFFER_WITH_SB(s) = bh;
1615	SB_DISK_SUPER_BLOCK(s) = rs;
1616
1617	/*
1618	 * magic is of non-standard journal filesystem, look at s_version to
1619	 * find which format is in use
1620	 */
1621	if (is_reiserfs_jr(rs)) {
1622		if (sb_version(rs) == REISERFS_VERSION_2)
1623			reiserfs_info(s, "found reiserfs format \"3.6\""
1624				      " with non-standard journal\n");
1625		else if (sb_version(rs) == REISERFS_VERSION_1)
1626			reiserfs_info(s, "found reiserfs format \"3.5\""
1627				      " with non-standard journal\n");
1628		else {
1629			reiserfs_warning(s, "sh-2012", "found unknown "
1630					 "format \"%u\" of reiserfs with "
1631					 "non-standard magic", sb_version(rs));
1632			return 1;
1633		}
1634	} else
1635		/*
1636		 * s_version of standard format may contain incorrect
1637		 * information, so we just look at the magic string
1638		 */
1639		reiserfs_info(s,
1640			      "found reiserfs format \"%s\" with standard journal\n",
1641			      is_reiserfs_3_5(rs) ? "3.5" : "3.6");
1642
1643	s->s_op = &reiserfs_sops;
1644	s->s_export_op = &reiserfs_export_ops;
1645#ifdef CONFIG_QUOTA
1646	s->s_qcop = &reiserfs_qctl_operations;
1647	s->dq_op = &reiserfs_quota_operations;
1648	s->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
1649#endif
1650
1651	/*
1652	 * new format is limited by the 32 bit wide i_blocks field, want to
1653	 * be one full block below that.
1654	 */
1655	s->s_maxbytes = (512LL << 32) - s->s_blocksize;
1656	return 0;
1657}
1658
1659/* after journal replay, reread all bitmap and super blocks */
1660static int reread_meta_blocks(struct super_block *s)
1661{
1662	ll_rw_block(READ, 1, &SB_BUFFER_WITH_SB(s));
1663	wait_on_buffer(SB_BUFFER_WITH_SB(s));
1664	if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
1665		reiserfs_warning(s, "reiserfs-2504", "error reading the super");
1666		return 1;
1667	}
1668
1669	return 0;
1670}
1671
1672/* hash detection stuff */
1673
1674/*
1675 * if root directory is empty - we set default - Yura's - hash and
1676 * warn about it
1677 * FIXME: we look for only one name in a directory. If tea and yura
1678 * both have the same value - we ask user to send report to the
1679 * mailing list
1680 */
1681static __u32 find_hash_out(struct super_block *s)
1682{
1683	int retval;
1684	struct inode *inode;
1685	struct cpu_key key;
1686	INITIALIZE_PATH(path);
1687	struct reiserfs_dir_entry de;
1688	struct reiserfs_de_head *deh;
1689	__u32 hash = DEFAULT_HASH;
1690	__u32 deh_hashval, teahash, r5hash, yurahash;
1691
1692	inode = d_inode(s->s_root);
1693
1694	make_cpu_key(&key, inode, ~0, TYPE_DIRENTRY, 3);
1695	retval = search_by_entry_key(s, &key, &path, &de);
1696	if (retval == IO_ERROR) {
1697		pathrelse(&path);
1698		return UNSET_HASH;
1699	}
1700	if (retval == NAME_NOT_FOUND)
1701		de.de_entry_num--;
1702
1703	set_de_name_and_namelen(&de);
1704	deh = de.de_deh + de.de_entry_num;
1705
1706	if (deh_offset(deh) == DOT_DOT_OFFSET) {
1707		/* allow override in this case */
1708		if (reiserfs_rupasov_hash(s))
1709			hash = YURA_HASH;
1710		reiserfs_info(s, "FS seems to be empty, autodetect is using the default hash\n");
1711		goto out;
1712	}
1713
1714	deh_hashval = GET_HASH_VALUE(deh_offset(deh));
1715	r5hash = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen));
1716	teahash = GET_HASH_VALUE(keyed_hash(de.de_name, de.de_namelen));
1717	yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen));
1718
1719	if ((teahash == r5hash && deh_hashval == r5hash) ||
1720	    (teahash == yurahash && deh_hashval == yurahash) ||
1721	    (r5hash == yurahash && deh_hashval == yurahash)) {
1722		reiserfs_warning(s, "reiserfs-2506",
1723				 "Unable to automatically detect hash "
1724				 "function. Please mount with -o "
1725				 "hash={tea,rupasov,r5}");
1726		hash = UNSET_HASH;
1727		goto out;
1728	}
1729
1730	if (deh_hashval == yurahash)
1731		hash = YURA_HASH;
1732	else if (deh_hashval == teahash)
1733		hash = TEA_HASH;
1734	else if (deh_hashval == r5hash)
1735		hash = R5_HASH;
1736	else {
1737		reiserfs_warning(s, "reiserfs-2506",
1738				 "Unrecognised hash function");
1739		hash = UNSET_HASH;
1740	}
1741out:
1742	pathrelse(&path);
1743	return hash;
1744}
1745
1746/* finds out which hash names are sorted with */
1747static int what_hash(struct super_block *s)
1748{
1749	__u32 code;
1750
1751	code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));
1752
1753	/*
1754	 * reiserfs_hash_detect() == true if any of the hash mount options
1755	 * were used.  We must check them to make sure the user isn't
1756	 * using a bad hash value
1757	 */
1758	if (code == UNSET_HASH || reiserfs_hash_detect(s))
1759		code = find_hash_out(s);
1760
1761	if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
1762		/*
1763		 * detection has found the hash, and we must check against the
1764		 * mount options
1765		 */
1766		if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
1767			reiserfs_warning(s, "reiserfs-2507",
1768					 "Error, %s hash detected, "
1769					 "unable to force rupasov hash",
1770					 reiserfs_hashname(code));
1771			code = UNSET_HASH;
1772		} else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
1773			reiserfs_warning(s, "reiserfs-2508",
1774					 "Error, %s hash detected, "
1775					 "unable to force tea hash",
1776					 reiserfs_hashname(code));
1777			code = UNSET_HASH;
1778		} else if (reiserfs_r5_hash(s) && code != R5_HASH) {
1779			reiserfs_warning(s, "reiserfs-2509",
1780					 "Error, %s hash detected, "
1781					 "unable to force r5 hash",
1782					 reiserfs_hashname(code));
1783			code = UNSET_HASH;
1784		}
1785	} else {
1786		/*
1787		 * find_hash_out was not called or
1788		 * could not determine the hash
1789		 */
1790		if (reiserfs_rupasov_hash(s)) {
1791			code = YURA_HASH;
1792		} else if (reiserfs_tea_hash(s)) {
1793			code = TEA_HASH;
1794		} else if (reiserfs_r5_hash(s)) {
1795			code = R5_HASH;
1796		}
1797	}
1798
1799	/*
1800	 * if we are mounted RW, and we have a new valid hash code, update
1801	 * the super
1802	 */
1803	if (code != UNSET_HASH &&
1804	    !(s->s_flags & MS_RDONLY) &&
1805	    code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1806		set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1807	}
1808	return code;
1809}
1810
1811/* return pointer to appropriate function */
1812static hashf_t hash_function(struct super_block *s)
1813{
1814	switch (what_hash(s)) {
1815	case TEA_HASH:
1816		reiserfs_info(s, "Using tea hash to sort names\n");
1817		return keyed_hash;
1818	case YURA_HASH:
1819		reiserfs_info(s, "Using rupasov hash to sort names\n");
1820		return yura_hash;
1821	case R5_HASH:
1822		reiserfs_info(s, "Using r5 hash to sort names\n");
1823		return r5_hash;
1824	}
1825	return NULL;
1826}
1827
1828/* this is used to set up correct value for old partitions */
1829static int function2code(hashf_t func)
1830{
1831	if (func == keyed_hash)
1832		return TEA_HASH;
1833	if (func == yura_hash)
1834		return YURA_HASH;
1835	if (func == r5_hash)
1836		return R5_HASH;
1837
1838	BUG();			/* should never happen */
1839
1840	return 0;
1841}
1842
1843#define SWARN(silent, s, id, ...)			\
1844	if (!(silent))				\
1845		reiserfs_warning(s, id, __VA_ARGS__)
1846
1847static int reiserfs_fill_super(struct super_block *s, void *data, int silent)
1848{
1849	struct inode *root_inode;
1850	struct reiserfs_transaction_handle th;
1851	int old_format = 0;
1852	unsigned long blocks;
1853	unsigned int commit_max_age = 0;
1854	int jinit_done = 0;
1855	struct reiserfs_iget_args args;
1856	struct reiserfs_super_block *rs;
1857	char *jdev_name;
1858	struct reiserfs_sb_info *sbi;
1859	int errval = -EINVAL;
1860	char *qf_names[REISERFS_MAXQUOTAS] = {};
1861	unsigned int qfmt = 0;
1862
1863	save_mount_options(s, data);
1864
1865	sbi = kzalloc(sizeof(struct reiserfs_sb_info), GFP_KERNEL);
1866	if (!sbi)
1867		return -ENOMEM;
1868	s->s_fs_info = sbi;
1869	/* Set default values for options: non-aggressive tails, RO on errors */
1870	sbi->s_mount_opt |= (1 << REISERFS_SMALLTAIL);
1871	sbi->s_mount_opt |= (1 << REISERFS_ERROR_RO);
1872	sbi->s_mount_opt |= (1 << REISERFS_BARRIER_FLUSH);
1873	/* no preallocation minimum, be smart in reiserfs_file_write instead */
1874	sbi->s_alloc_options.preallocmin = 0;
1875	/* Preallocate by 16 blocks (17-1) at once */
1876	sbi->s_alloc_options.preallocsize = 17;
1877	/* setup default block allocator options */
1878	reiserfs_init_alloc_options(s);
1879
1880	spin_lock_init(&sbi->old_work_lock);
1881	INIT_DELAYED_WORK(&sbi->old_work, flush_old_commits);
1882	mutex_init(&sbi->lock);
1883	sbi->lock_depth = -1;
1884
1885	sbi->commit_wq = alloc_workqueue("reiserfs/%s", WQ_MEM_RECLAIM, 0,
1886					 s->s_id);
1887	if (!sbi->commit_wq) {
1888		SWARN(silent, s, "", "Cannot allocate commit workqueue");
1889		errval = -ENOMEM;
1890		goto error_unlocked;
1891	}
1892
1893	jdev_name = NULL;
1894	if (reiserfs_parse_options
1895	    (s, (char *)data, &sbi->s_mount_opt, &blocks, &jdev_name,
1896	     &commit_max_age, qf_names, &qfmt) == 0) {
1897		goto error_unlocked;
1898	}
1899	if (jdev_name && jdev_name[0]) {
1900		sbi->s_jdev = kstrdup(jdev_name, GFP_KERNEL);
1901		if (!sbi->s_jdev) {
1902			SWARN(silent, s, "", "Cannot allocate memory for "
1903				"journal device name");
1904			goto error;
1905		}
1906	}
1907#ifdef CONFIG_QUOTA
1908	handle_quota_files(s, qf_names, &qfmt);
1909#endif
1910
1911	if (blocks) {
1912		SWARN(silent, s, "jmacd-7", "resize option for remount only");
1913		goto error_unlocked;
1914	}
1915
1916	/*
1917	 * try old format (undistributed bitmap, super block in 8-th 1k
1918	 * block of a device)
1919	 */
1920	if (!read_super_block(s, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
1921		old_format = 1;
1922
1923	/*
1924	 * try new format (64-th 1k block), which can contain reiserfs
1925	 * super block
1926	 */
1927	else if (read_super_block(s, REISERFS_DISK_OFFSET_IN_BYTES)) {
1928		SWARN(silent, s, "sh-2021", "can not find reiserfs on %s",
1929		      s->s_id);
1930		goto error_unlocked;
1931	}
1932
1933	rs = SB_DISK_SUPER_BLOCK(s);
1934	/*
1935	 * Let's do basic sanity check to verify that underlying device is not
1936	 * smaller than the filesystem. If the check fails then abort and
1937	 * scream, because bad stuff will happen otherwise.
1938	 */
1939	if (s->s_bdev && s->s_bdev->bd_inode
1940	    && i_size_read(s->s_bdev->bd_inode) <
1941	    sb_block_count(rs) * sb_blocksize(rs)) {
1942		SWARN(silent, s, "", "Filesystem cannot be "
1943		      "mounted because it is bigger than the device");
1944		SWARN(silent, s, "", "You may need to run fsck "
1945		      "or increase size of your LVM partition");
1946		SWARN(silent, s, "", "Or may be you forgot to "
1947		      "reboot after fdisk when it told you to");
1948		goto error_unlocked;
1949	}
1950
1951	sbi->s_mount_state = SB_REISERFS_STATE(s);
1952	sbi->s_mount_state = REISERFS_VALID_FS;
1953
1954	if ((errval = reiserfs_init_bitmap_cache(s))) {
1955		SWARN(silent, s, "jmacd-8", "unable to read bitmap");
1956		goto error_unlocked;
1957	}
1958
1959	errval = -EINVAL;
1960#ifdef CONFIG_REISERFS_CHECK
1961	SWARN(silent, s, "", "CONFIG_REISERFS_CHECK is set ON");
1962	SWARN(silent, s, "", "- it is slow mode for debugging.");
1963#endif
1964
1965	/* make data=ordered the default */
1966	if (!reiserfs_data_log(s) && !reiserfs_data_ordered(s) &&
1967	    !reiserfs_data_writeback(s)) {
1968		sbi->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
1969	}
1970
1971	if (reiserfs_data_log(s)) {
1972		reiserfs_info(s, "using journaled data mode\n");
1973	} else if (reiserfs_data_ordered(s)) {
1974		reiserfs_info(s, "using ordered data mode\n");
1975	} else {
1976		reiserfs_info(s, "using writeback data mode\n");
1977	}
1978	if (reiserfs_barrier_flush(s)) {
1979		printk("reiserfs: using flush barriers\n");
1980	}
1981
1982	if (journal_init(s, jdev_name, old_format, commit_max_age)) {
1983		SWARN(silent, s, "sh-2022",
1984		      "unable to initialize journal space");
1985		goto error_unlocked;
1986	} else {
1987		/*
1988		 * once this is set, journal_release must be called
1989		 * if we error out of the mount
1990		 */
1991		jinit_done = 1;
1992	}
1993
1994	if (reread_meta_blocks(s)) {
1995		SWARN(silent, s, "jmacd-9",
1996		      "unable to reread meta blocks after journal init");
1997		goto error_unlocked;
1998	}
1999
2000	if (replay_only(s))
2001		goto error_unlocked;
2002
2003	if (bdev_read_only(s->s_bdev) && !(s->s_flags & MS_RDONLY)) {
2004		SWARN(silent, s, "clm-7000",
2005		      "Detected readonly device, marking FS readonly");
2006		s->s_flags |= MS_RDONLY;
2007	}
2008	args.objectid = REISERFS_ROOT_OBJECTID;
2009	args.dirid = REISERFS_ROOT_PARENT_OBJECTID;
2010	root_inode =
2011	    iget5_locked(s, REISERFS_ROOT_OBJECTID, reiserfs_find_actor,
2012			 reiserfs_init_locked_inode, (void *)&args);
2013	if (!root_inode) {
2014		SWARN(silent, s, "jmacd-10", "get root inode failed");
2015		goto error_unlocked;
2016	}
2017
2018	/*
2019	 * This path assumed to be called with the BKL in the old times.
2020	 * Now we have inherited the big reiserfs lock from it and many
2021	 * reiserfs helpers called in the mount path and elsewhere require
2022	 * this lock to be held even if it's not always necessary. Let's be
2023	 * conservative and hold it early. The window can be reduced after
2024	 * careful review of the code.
2025	 */
2026	reiserfs_write_lock(s);
2027
2028	if (root_inode->i_state & I_NEW) {
2029		reiserfs_read_locked_inode(root_inode, &args);
2030		unlock_new_inode(root_inode);
2031	}
2032
2033	s->s_root = d_make_root(root_inode);
2034	if (!s->s_root)
2035		goto error;
2036	/* define and initialize hash function */
2037	sbi->s_hash_function = hash_function(s);
2038	if (sbi->s_hash_function == NULL) {
2039		dput(s->s_root);
2040		s->s_root = NULL;
2041		goto error;
2042	}
2043
2044	if (is_reiserfs_3_5(rs)
2045	    || (is_reiserfs_jr(rs) && SB_VERSION(s) == REISERFS_VERSION_1))
2046		set_bit(REISERFS_3_5, &sbi->s_properties);
2047	else if (old_format)
2048		set_bit(REISERFS_OLD_FORMAT, &sbi->s_properties);
2049	else
2050		set_bit(REISERFS_3_6, &sbi->s_properties);
2051
2052	if (!(s->s_flags & MS_RDONLY)) {
2053
2054		errval = journal_begin(&th, s, 1);
2055		if (errval) {
2056			dput(s->s_root);
2057			s->s_root = NULL;
2058			goto error;
2059		}
2060		reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
2061
2062		set_sb_umount_state(rs, REISERFS_ERROR_FS);
2063		set_sb_fs_state(rs, 0);
2064
2065		/*
2066		 * Clear out s_bmap_nr if it would wrap. We can handle this
2067		 * case, but older revisions can't. This will cause the
2068		 * file system to fail mount on those older implementations,
2069		 * avoiding corruption. -jeffm
2070		 */
2071		if (bmap_would_wrap(reiserfs_bmap_count(s)) &&
2072		    sb_bmap_nr(rs) != 0) {
2073			reiserfs_warning(s, "super-2030", "This file system "
2074					"claims to use %u bitmap blocks in "
2075					"its super block, but requires %u. "
2076					"Clearing to zero.", sb_bmap_nr(rs),
2077					reiserfs_bmap_count(s));
2078
2079			set_sb_bmap_nr(rs, 0);
2080		}
2081
2082		if (old_format_only(s)) {
2083			/*
2084			 * filesystem of format 3.5 either with standard
2085			 * or non-standard journal
2086			 */
2087			if (convert_reiserfs(s)) {
2088				/* and -o conv is given */
2089				if (!silent)
2090					reiserfs_info(s,
2091						      "converting 3.5 filesystem to the 3.6 format");
2092
2093				if (is_reiserfs_3_5(rs))
2094					/*
2095					 * put magic string of 3.6 format.
2096					 * 2.2 will not be able to
2097					 * mount this filesystem anymore
2098					 */
2099					memcpy(rs->s_v1.s_magic,
2100					       reiserfs_3_6_magic_string,
2101					       sizeof
2102					       (reiserfs_3_6_magic_string));
2103
2104				set_sb_version(rs, REISERFS_VERSION_2);
2105				reiserfs_convert_objectid_map_v1(s);
2106				set_bit(REISERFS_3_6, &sbi->s_properties);
2107				clear_bit(REISERFS_3_5, &sbi->s_properties);
2108			} else if (!silent) {
2109				reiserfs_info(s, "using 3.5.x disk format\n");
2110			}
2111		} else
2112			set_sb_mnt_count(rs, sb_mnt_count(rs) + 1);
2113
2114
2115		journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
2116		errval = journal_end(&th);
2117		if (errval) {
2118			dput(s->s_root);
2119			s->s_root = NULL;
2120			goto error;
2121		}
2122
2123		reiserfs_write_unlock(s);
2124		if ((errval = reiserfs_lookup_privroot(s)) ||
2125		    (errval = reiserfs_xattr_init(s, s->s_flags))) {
2126			dput(s->s_root);
2127			s->s_root = NULL;
2128			goto error_unlocked;
2129		}
2130		reiserfs_write_lock(s);
2131
2132		/*
2133		 * look for files which were to be removed in previous session
2134		 */
2135		finish_unfinished(s);
2136	} else {
2137		if (old_format_only(s) && !silent) {
2138			reiserfs_info(s, "using 3.5.x disk format\n");
2139		}
2140
2141		reiserfs_write_unlock(s);
2142		if ((errval = reiserfs_lookup_privroot(s)) ||
2143		    (errval = reiserfs_xattr_init(s, s->s_flags))) {
2144			dput(s->s_root);
2145			s->s_root = NULL;
2146			goto error_unlocked;
2147		}
2148		reiserfs_write_lock(s);
2149	}
2150	/*
2151	 * mark hash in super block: it could be unset. overwrite should be ok
2152	 */
2153	set_sb_hash_function_code(rs, function2code(sbi->s_hash_function));
2154
2155	handle_attrs(s);
2156
2157	reiserfs_proc_info_init(s);
2158
2159	init_waitqueue_head(&(sbi->s_wait));
2160	spin_lock_init(&sbi->bitmap_lock);
2161
2162	reiserfs_write_unlock(s);
2163
2164	return (0);
2165
2166error:
2167	reiserfs_write_unlock(s);
2168
2169error_unlocked:
2170	/* kill the commit thread, free journal ram */
2171	if (jinit_done) {
2172		reiserfs_write_lock(s);
2173		journal_release_error(NULL, s);
2174		reiserfs_write_unlock(s);
2175	}
2176
2177	if (sbi->commit_wq)
2178		destroy_workqueue(sbi->commit_wq);
2179
2180	cancel_delayed_work_sync(&REISERFS_SB(s)->old_work);
2181
2182	reiserfs_free_bitmap_cache(s);
2183	if (SB_BUFFER_WITH_SB(s))
2184		brelse(SB_BUFFER_WITH_SB(s));
2185#ifdef CONFIG_QUOTA
2186	{
2187		int j;
2188		for (j = 0; j < REISERFS_MAXQUOTAS; j++)
2189			kfree(qf_names[j]);
2190	}
2191#endif
2192	kfree(sbi);
2193
2194	s->s_fs_info = NULL;
2195	return errval;
2196}
2197
2198static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf)
2199{
2200	struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(dentry->d_sb);
2201
2202	buf->f_namelen = (REISERFS_MAX_NAME(s->s_blocksize));
2203	buf->f_bfree = sb_free_blocks(rs);
2204	buf->f_bavail = buf->f_bfree;
2205	buf->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
2206	buf->f_bsize = dentry->d_sb->s_blocksize;
2207	/* changed to accommodate gcc folks. */
2208	buf->f_type = REISERFS_SUPER_MAGIC;
2209	buf->f_fsid.val[0] = (u32)crc32_le(0, rs->s_uuid, sizeof(rs->s_uuid)/2);
2210	buf->f_fsid.val[1] = (u32)crc32_le(0, rs->s_uuid + sizeof(rs->s_uuid)/2,
2211				sizeof(rs->s_uuid)/2);
2212
2213	return 0;
2214}
2215
2216#ifdef CONFIG_QUOTA
2217static int reiserfs_write_dquot(struct dquot *dquot)
2218{
2219	struct reiserfs_transaction_handle th;
2220	int ret, err;
2221	int depth;
2222
2223	reiserfs_write_lock(dquot->dq_sb);
2224	ret =
2225	    journal_begin(&th, dquot->dq_sb,
2226			  REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
2227	if (ret)
2228		goto out;
2229	depth = reiserfs_write_unlock_nested(dquot->dq_sb);
2230	ret = dquot_commit(dquot);
2231	reiserfs_write_lock_nested(dquot->dq_sb, depth);
2232	err = journal_end(&th);
2233	if (!ret && err)
2234		ret = err;
2235out:
2236	reiserfs_write_unlock(dquot->dq_sb);
2237	return ret;
2238}
2239
2240static int reiserfs_acquire_dquot(struct dquot *dquot)
2241{
2242	struct reiserfs_transaction_handle th;
2243	int ret, err;
2244	int depth;
2245
2246	reiserfs_write_lock(dquot->dq_sb);
2247	ret =
2248	    journal_begin(&th, dquot->dq_sb,
2249			  REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
2250	if (ret)
2251		goto out;
2252	depth = reiserfs_write_unlock_nested(dquot->dq_sb);
2253	ret = dquot_acquire(dquot);
2254	reiserfs_write_lock_nested(dquot->dq_sb, depth);
2255	err = journal_end(&th);
2256	if (!ret && err)
2257		ret = err;
2258out:
2259	reiserfs_write_unlock(dquot->dq_sb);
2260	return ret;
2261}
2262
2263static int reiserfs_release_dquot(struct dquot *dquot)
2264{
2265	struct reiserfs_transaction_handle th;
2266	int ret, err;
2267
2268	reiserfs_write_lock(dquot->dq_sb);
2269	ret =
2270	    journal_begin(&th, dquot->dq_sb,
2271			  REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
2272	reiserfs_write_unlock(dquot->dq_sb);
2273	if (ret) {
2274		/* Release dquot anyway to avoid endless cycle in dqput() */
2275		dquot_release(dquot);
2276		goto out;
2277	}
2278	ret = dquot_release(dquot);
2279	reiserfs_write_lock(dquot->dq_sb);
2280	err = journal_end(&th);
2281	if (!ret && err)
2282		ret = err;
2283	reiserfs_write_unlock(dquot->dq_sb);
2284out:
2285	return ret;
2286}
2287
2288static int reiserfs_mark_dquot_dirty(struct dquot *dquot)
2289{
2290	/* Are we journaling quotas? */
2291	if (REISERFS_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2292	    REISERFS_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
2293		dquot_mark_dquot_dirty(dquot);
2294		return reiserfs_write_dquot(dquot);
2295	} else
2296		return dquot_mark_dquot_dirty(dquot);
2297}
2298
2299static int reiserfs_write_info(struct super_block *sb, int type)
2300{
2301	struct reiserfs_transaction_handle th;
2302	int ret, err;
2303	int depth;
2304
2305	/* Data block + inode block */
2306	reiserfs_write_lock(sb);
2307	ret = journal_begin(&th, sb, 2);
2308	if (ret)
2309		goto out;
2310	depth = reiserfs_write_unlock_nested(sb);
2311	ret = dquot_commit_info(sb, type);
2312	reiserfs_write_lock_nested(sb, depth);
2313	err = journal_end(&th);
2314	if (!ret && err)
2315		ret = err;
2316out:
2317	reiserfs_write_unlock(sb);
2318	return ret;
2319}
2320
2321/*
2322 * Turn on quotas during mount time - we need to find the quota file and such...
2323 */
2324static int reiserfs_quota_on_mount(struct super_block *sb, int type)
2325{
2326	return dquot_quota_on_mount(sb, REISERFS_SB(sb)->s_qf_names[type],
2327					REISERFS_SB(sb)->s_jquota_fmt, type);
2328}
2329
2330/*
2331 * Standard function to be called on quota_on
2332 */
2333static int reiserfs_quota_on(struct super_block *sb, int type, int format_id,
2334			     struct path *path)
2335{
2336	int err;
2337	struct inode *inode;
2338	struct reiserfs_transaction_handle th;
2339	int opt = type == USRQUOTA ? REISERFS_USRQUOTA : REISERFS_GRPQUOTA;
2340
2341	reiserfs_write_lock(sb);
2342	if (!(REISERFS_SB(sb)->s_mount_opt & (1 << opt))) {
2343		err = -EINVAL;
2344		goto out;
2345	}
2346
2347	/* Quotafile not on the same filesystem? */
2348	if (path->dentry->d_sb != sb) {
2349		err = -EXDEV;
2350		goto out;
2351	}
2352	inode = d_inode(path->dentry);
2353	/*
2354	 * We must not pack tails for quota files on reiserfs for quota
2355	 * IO to work
2356	 */
2357	if (!(REISERFS_I(inode)->i_flags & i_nopack_mask)) {
2358		err = reiserfs_unpack(inode, NULL);
2359		if (err) {
2360			reiserfs_warning(sb, "super-6520",
2361				"Unpacking tail of quota file failed"
2362				" (%d). Cannot turn on quotas.", err);
2363			err = -EINVAL;
2364			goto out;
2365		}
2366		mark_inode_dirty(inode);
2367	}
2368	/* Journaling quota? */
2369	if (REISERFS_SB(sb)->s_qf_names[type]) {
2370		/* Quotafile not of fs root? */
2371		if (path->dentry->d_parent != sb->s_root)
2372			reiserfs_warning(sb, "super-6521",
2373				 "Quota file not on filesystem root. "
2374				 "Journalled quota will not work.");
2375	}
2376
2377	/*
2378	 * When we journal data on quota file, we have to flush journal to see
2379	 * all updates to the file when we bypass pagecache...
2380	 */
2381	if (reiserfs_file_data_log(inode)) {
2382		/* Just start temporary transaction and finish it */
2383		err = journal_begin(&th, sb, 1);
2384		if (err)
2385			goto out;
2386		err = journal_end_sync(&th);
2387		if (err)
2388			goto out;
2389	}
2390	reiserfs_write_unlock(sb);
2391	return dquot_quota_on(sb, type, format_id, path);
2392out:
2393	reiserfs_write_unlock(sb);
2394	return err;
2395}
2396
2397/*
2398 * Read data from quotafile - avoid pagecache and such because we cannot afford
2399 * acquiring the locks... As quota files are never truncated and quota code
2400 * itself serializes the operations (and no one else should touch the files)
2401 * we don't have to be afraid of races
2402 */
2403static ssize_t reiserfs_quota_read(struct super_block *sb, int type, char *data,
2404				   size_t len, loff_t off)
2405{
2406	struct inode *inode = sb_dqopt(sb)->files[type];
2407	unsigned long blk = off >> sb->s_blocksize_bits;
2408	int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2409	size_t toread;
2410	struct buffer_head tmp_bh, *bh;
2411	loff_t i_size = i_size_read(inode);
2412
2413	if (off > i_size)
2414		return 0;
2415	if (off + len > i_size)
2416		len = i_size - off;
2417	toread = len;
2418	while (toread > 0) {
2419		tocopy =
2420		    sb->s_blocksize - offset <
2421		    toread ? sb->s_blocksize - offset : toread;
2422		tmp_bh.b_state = 0;
2423		/*
2424		 * Quota files are without tails so we can safely
2425		 * use this function
2426		 */
2427		reiserfs_write_lock(sb);
2428		err = reiserfs_get_block(inode, blk, &tmp_bh, 0);
2429		reiserfs_write_unlock(sb);
2430		if (err)
2431			return err;
2432		if (!buffer_mapped(&tmp_bh))	/* A hole? */
2433			memset(data, 0, tocopy);
2434		else {
2435			bh = sb_bread(sb, tmp_bh.b_blocknr);
2436			if (!bh)
2437				return -EIO;
2438			memcpy(data, bh->b_data + offset, tocopy);
2439			brelse(bh);
2440		}
2441		offset = 0;
2442		toread -= tocopy;
2443		data += tocopy;
2444		blk++;
2445	}
2446	return len;
2447}
2448
2449/*
2450 * Write to quotafile (we know the transaction is already started and has
2451 * enough credits)
2452 */
2453static ssize_t reiserfs_quota_write(struct super_block *sb, int type,
2454				    const char *data, size_t len, loff_t off)
2455{
2456	struct inode *inode = sb_dqopt(sb)->files[type];
2457	unsigned long blk = off >> sb->s_blocksize_bits;
2458	int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2459	int journal_quota = REISERFS_SB(sb)->s_qf_names[type] != NULL;
2460	size_t towrite = len;
2461	struct buffer_head tmp_bh, *bh;
2462
2463	if (!current->journal_info) {
2464		printk(KERN_WARNING "reiserfs: Quota write (off=%llu, len=%llu) cancelled because transaction is not started.\n",
2465			(unsigned long long)off, (unsigned long long)len);
2466		return -EIO;
2467	}
2468	while (towrite > 0) {
2469		tocopy = sb->s_blocksize - offset < towrite ?
2470		    sb->s_blocksize - offset : towrite;
2471		tmp_bh.b_state = 0;
2472		reiserfs_write_lock(sb);
2473		err = reiserfs_get_block(inode, blk, &tmp_bh, GET_BLOCK_CREATE);
2474		reiserfs_write_unlock(sb);
2475		if (err)
2476			goto out;
2477		if (offset || tocopy != sb->s_blocksize)
2478			bh = sb_bread(sb, tmp_bh.b_blocknr);
2479		else
2480			bh = sb_getblk(sb, tmp_bh.b_blocknr);
2481		if (!bh) {
2482			err = -EIO;
2483			goto out;
2484		}
2485		lock_buffer(bh);
2486		memcpy(bh->b_data + offset, data, tocopy);
2487		flush_dcache_page(bh->b_page);
2488		set_buffer_uptodate(bh);
2489		unlock_buffer(bh);
2490		reiserfs_write_lock(sb);
2491		reiserfs_prepare_for_journal(sb, bh, 1);
2492		journal_mark_dirty(current->journal_info, bh);
2493		if (!journal_quota)
2494			reiserfs_add_ordered_list(inode, bh);
2495		reiserfs_write_unlock(sb);
2496		brelse(bh);
2497		offset = 0;
2498		towrite -= tocopy;
2499		data += tocopy;
2500		blk++;
2501	}
2502out:
2503	if (len == towrite)
2504		return err;
2505	if (inode->i_size < off + len - towrite)
2506		i_size_write(inode, off + len - towrite);
2507	inode->i_version++;
2508	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2509	mark_inode_dirty(inode);
2510	return len - towrite;
2511}
2512
2513#endif
2514
2515static struct dentry *get_super_block(struct file_system_type *fs_type,
2516			   int flags, const char *dev_name,
2517			   void *data)
2518{
2519	return mount_bdev(fs_type, flags, dev_name, data, reiserfs_fill_super);
2520}
2521
2522static int __init init_reiserfs_fs(void)
2523{
2524	int ret;
2525
2526	ret = init_inodecache();
2527	if (ret)
2528		return ret;
2529
2530	reiserfs_proc_info_global_init();
2531
2532	ret = register_filesystem(&reiserfs_fs_type);
2533	if (ret)
2534		goto out;
2535
2536	return 0;
2537out:
2538	reiserfs_proc_info_global_done();
2539	destroy_inodecache();
2540
2541	return ret;
2542}
2543
2544static void __exit exit_reiserfs_fs(void)
2545{
2546	reiserfs_proc_info_global_done();
2547	unregister_filesystem(&reiserfs_fs_type);
2548	destroy_inodecache();
2549}
2550
2551struct file_system_type reiserfs_fs_type = {
2552	.owner = THIS_MODULE,
2553	.name = "reiserfs",
2554	.mount = get_super_block,
2555	.kill_sb = reiserfs_kill_sb,
2556	.fs_flags = FS_REQUIRES_DEV,
2557};
2558MODULE_ALIAS_FS("reiserfs");
2559
2560MODULE_DESCRIPTION("ReiserFS journaled filesystem");
2561MODULE_AUTHOR("Hans Reiser <reiser@namesys.com>");
2562MODULE_LICENSE("GPL");
2563
2564module_init(init_reiserfs_fs);
2565module_exit(exit_reiserfs_fs);
2566