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