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