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