make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / fs / reiserfs / super.c
1 /*
2  * Copyright 2000-2002 by Hans Reiser, licensing governed by reiserfs/README
3  */
4
5 #include <linux/config.h>
6 #include <linux/module.h>
7 #include <linux/sched.h>
8 #include <linux/vmalloc.h>
9 #include <asm/uaccess.h>
10 #include <linux/reiserfs_fs.h>
11 #include <linux/smp_lock.h>
12 #include <linux/locks.h>
13 #include <linux/init.h>
14
15 #define REISERFS_OLD_BLOCKSIZE 4096
16 #define REISERFS_SUPER_MAGIC_STRING_OFFSET_NJ 20
17
18 char reiserfs_super_magic_string[] = REISERFS_SUPER_MAGIC_STRING;
19 char reiser2fs_super_magic_string[] = REISER2FS_SUPER_MAGIC_STRING;
20
21 static int reiserfs_remount (struct super_block * s, int * flags, char * data);
22 static int reiserfs_statfs (struct super_block * s, struct statfs * buf);
23
24 static void reiserfs_write_super (struct super_block * s)
25 {
26
27   int dirty = 0 ;
28   lock_kernel() ;
29   if (!(s->s_flags & MS_RDONLY)) {
30     dirty = flush_old_commits(s, 1) ;
31   }
32   s->s_dirt = dirty;
33   unlock_kernel() ;
34 }
35
36 static void reiserfs_write_super_lockfs (struct super_block * s)
37 {
38
39   int dirty = 0 ;
40   struct reiserfs_transaction_handle th ;
41   lock_kernel() ;
42   if (!(s->s_flags & MS_RDONLY)) {
43     journal_begin(&th, s, 1) ;
44     reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
45     journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
46     reiserfs_block_writes(&th) ;
47     journal_end(&th, s, 1) ;
48   }
49   s->s_dirt = dirty;
50   unlock_kernel() ;
51 }
52
53 void reiserfs_unlockfs(struct super_block *s) {
54   reiserfs_allow_writes(s) ;
55 }
56
57 extern const struct key  MAX_KEY;
58
59
60 /* this is used to delete "save link" when there are no items of a
61    file it points to. It can either happen if unlink is completed but
62    "save unlink" removal, or if file has both unlink and truncate
63    pending and as unlink completes first (because key of "save link"
64    protecting unlink is bigger that a key lf "save link" which
65    protects truncate), so there left no items to make truncate
66    completion on */
67 static void remove_save_link_only (struct super_block * s, struct key * key, int oid_free)
68 {
69     struct reiserfs_transaction_handle th;
70
71      /* we are going to do one balancing */
72      journal_begin (&th, s, JOURNAL_PER_BALANCE_CNT);
73  
74      reiserfs_delete_solid_item (&th, key);
75      if (oid_free)
76         /* removals are protected by direct items */
77         reiserfs_release_objectid (&th, le32_to_cpu (key->k_objectid));
78
79      journal_end (&th, s, JOURNAL_PER_BALANCE_CNT);
80 }
81  
82  
83 /* look for uncompleted unlinks and truncates and complete them */
84 static void finish_unfinished (struct super_block * s)
85 {
86     INITIALIZE_PATH (path);
87     struct cpu_key max_cpu_key, obj_key;
88     struct key save_link_key;
89     int retval;
90     struct item_head * ih;
91     struct buffer_head * bh;
92     int item_pos;
93     char * item;
94     int done;
95     struct inode * inode;
96     int truncate;
97  
98  
99     /* compose key to look for "save" links */
100     max_cpu_key.version = KEY_FORMAT_3_5;
101     max_cpu_key.on_disk_key = MAX_KEY;
102     max_cpu_key.key_length = 3;
103  
104     done = 0;
105     s -> u.reiserfs_sb.s_is_unlinked_ok = 1;
106     while (1) {
107         retval = search_item (s, &max_cpu_key, &path);
108         if (retval != ITEM_NOT_FOUND) {
109             reiserfs_warning ("vs-2140: finish_unfinished: search_by_key returned %d\n",
110                               retval);
111             break;
112         }
113         
114         bh = get_last_bh (&path);
115         item_pos = get_item_pos (&path);
116         if (item_pos != B_NR_ITEMS (bh)) {
117             reiserfs_warning ("vs-2060: finish_unfinished: wrong position found\n");
118             break;
119         }
120         item_pos --;
121         ih = B_N_PITEM_HEAD (bh, item_pos);
122  
123         if (le32_to_cpu (ih->ih_key.k_dir_id) != MAX_KEY_OBJECTID)
124             /* there are no "save" links anymore */
125             break;
126  
127         save_link_key = ih->ih_key;
128         if (is_indirect_le_ih (ih))
129             truncate = 1;
130         else
131             truncate = 0;
132  
133         /* reiserfs_iget needs k_dirid and k_objectid only */
134         item = B_I_PITEM (bh, ih);
135         obj_key.on_disk_key.k_dir_id = le32_to_cpu (*(__u32 *)item);
136         obj_key.on_disk_key.k_objectid = le32_to_cpu (ih->ih_key.k_objectid);
137         obj_key.on_disk_key.u.k_offset_v1.k_offset = 0;
138         obj_key.on_disk_key.u.k_offset_v1.k_uniqueness = 0;
139         
140         pathrelse (&path);
141  
142         inode = reiserfs_iget (s, &obj_key);
143         if (!inode) {
144             /* the unlink almost completed, it just did not manage to remove
145                "save" link and release objectid */
146             reiserfs_warning ("vs-2180: finish_unfinished: iget failed for %K\n",
147                               &obj_key);
148             remove_save_link_only (s, &save_link_key, 1);
149             continue;
150         }
151
152         if (!truncate && inode->i_nlink) {
153             /* file is not unlinked */
154             reiserfs_warning ("vs-2185: finish_unfinished: file %K is not unlinked\n",
155                               &obj_key);
156             remove_save_link_only (s, &save_link_key, 0);
157             continue;
158         }
159
160         if (truncate && S_ISDIR (inode->i_mode) ) {
161             /* We got a truncate request for a dir which is impossible.
162                The only imaginable way is to execute unfinished truncate request
163                then boot into old kernel, remove the file and create dir with
164                the same key. */
165             reiserfs_warning("green-2101: impossible truncate on a directory %k. Please report\n", INODE_PKEY (inode));
166             remove_save_link_only (s, &save_link_key, 0);
167             truncate = 0;
168             iput (inode); 
169             continue;
170         }
171  
172         if (truncate) {
173             inode -> u.reiserfs_i.i_flags |= i_link_saved_truncate_mask;
174             /* not completed truncate found. New size was committed together
175                with "save" link */
176             reiserfs_warning ("Truncating %k to %Ld ..",
177                               INODE_PKEY (inode), inode->i_size);
178             reiserfs_truncate_file (inode, 0/*don't update modification time*/);
179             remove_save_link (inode, truncate);
180         } else {
181             inode -> u.reiserfs_i.i_flags |= i_link_saved_unlink_mask;
182             /* not completed unlink (rmdir) found */
183             reiserfs_warning ("Removing %k..", INODE_PKEY (inode));
184             /* removal gets completed in iput */
185         }
186  
187         iput (inode);
188         printk ("done\n");
189         done ++;
190     }
191     s -> u.reiserfs_sb.s_is_unlinked_ok = 0;
192      
193     pathrelse (&path);
194     if (done)
195         reiserfs_warning ("There were %d uncompleted unlinks/truncates. "
196                           "Completed\n", done);
197 }
198  
199 /* to protect file being unlinked from getting lost we "safe" link files
200    being unlinked. This link will be deleted in the same transaction with last
201    item of file. mounting the filesytem we scan all these links and remove
202    files which almost got lost */
203 void add_save_link (struct reiserfs_transaction_handle * th,
204                     struct inode * inode, int truncate)
205 {
206     INITIALIZE_PATH (path);
207     int retval;
208     struct cpu_key key;
209     struct item_head ih;
210     __u32 link;
211
212     /* file can only get one "save link" of each kind */
213     RFALSE( truncate && 
214             ( inode -> u.reiserfs_i.i_flags & i_link_saved_truncate_mask ),
215             "saved link already exists for truncated inode %lx",
216             ( long ) inode -> i_ino );
217     RFALSE( !truncate && 
218             ( inode -> u.reiserfs_i.i_flags & i_link_saved_unlink_mask ),
219             "saved link already exists for unlinked inode %lx",
220             ( long ) inode -> i_ino );
221
222     /* setup key of "save" link */
223     key.version = KEY_FORMAT_3_5;
224     key.on_disk_key.k_dir_id = MAX_KEY_OBJECTID;
225     key.on_disk_key.k_objectid = inode->i_ino;
226     if (!truncate) {
227         /* unlink, rmdir, rename */
228         set_cpu_key_k_offset (&key, 1 + inode->i_sb->s_blocksize);
229         set_cpu_key_k_type (&key, TYPE_DIRECT);
230
231         /* item head of "safe" link */
232         make_le_item_head (&ih, &key, key.version, 1 + inode->i_sb->s_blocksize, TYPE_DIRECT,
233                            4/*length*/, 0xffff/*free space*/);
234     } else {
235         /* truncate */
236         if (S_ISDIR (inode->i_mode))
237             reiserfs_warning("green-2102: Adding a truncate savelink for a directory %k! Please report\n", INODE_PKEY(inode));
238         set_cpu_key_k_offset (&key, 1);
239         set_cpu_key_k_type (&key, TYPE_INDIRECT);
240
241         /* item head of "safe" link */
242         make_le_item_head (&ih, &key, key.version, 1, TYPE_INDIRECT,
243                            4/*length*/, 0/*free space*/);
244     }
245     key.key_length = 3;
246
247     /* look for its place in the tree */
248     retval = search_item (inode->i_sb, &key, &path);
249     if (retval != ITEM_NOT_FOUND) {
250         if ( retval != -ENOSPC )
251             reiserfs_warning ("vs-2100: add_save_link:"
252                           "search_by_key (%K) returned %d\n", &key, retval);
253         pathrelse (&path);
254         return;
255     }
256
257     /* body of "save" link */
258     link = cpu_to_le32 (INODE_PKEY (inode)->k_dir_id);
259
260     /* put "save" link inot tree */
261     retval = reiserfs_insert_item (th, &path, &key, &ih, (char *)&link);
262     if (retval) {
263         if (retval != -ENOSPC)
264             reiserfs_warning ("vs-2120: add_save_link: insert_item returned %d\n",
265                           retval);
266     } else {
267         if( truncate )
268             inode -> u.reiserfs_i.i_flags |= i_link_saved_truncate_mask;
269         else
270             inode -> u.reiserfs_i.i_flags |= i_link_saved_unlink_mask;
271     }
272 }
273
274
275 /* this opens transaction unlike add_save_link */
276 void remove_save_link (struct inode * inode, int truncate)
277 {
278     struct reiserfs_transaction_handle th;
279     struct key key;
280  
281  
282     /* we are going to do one balancing only */
283     journal_begin (&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
284  
285     /* setup key of "save" link */
286     key.k_dir_id = cpu_to_le32 (MAX_KEY_OBJECTID);
287     key.k_objectid = INODE_PKEY (inode)->k_objectid;
288     if (!truncate) {
289         /* unlink, rmdir, rename */
290         set_le_key_k_offset (KEY_FORMAT_3_5, &key,
291                              1 + inode->i_sb->s_blocksize);
292         set_le_key_k_type (KEY_FORMAT_3_5, &key, TYPE_DIRECT);
293     } else {
294         /* truncate */
295         set_le_key_k_offset (KEY_FORMAT_3_5, &key, 1);
296         set_le_key_k_type (KEY_FORMAT_3_5, &key, TYPE_INDIRECT);
297     }
298  
299     if( ( truncate && 
300           ( inode -> u.reiserfs_i.i_flags & i_link_saved_truncate_mask ) ) ||
301         ( !truncate && 
302           ( inode -> u.reiserfs_i.i_flags & i_link_saved_unlink_mask ) ) )
303         reiserfs_delete_solid_item (&th, &key);
304     if (!truncate) {
305         reiserfs_release_objectid (&th, inode->i_ino);
306         inode -> u.reiserfs_i.i_flags &= ~i_link_saved_unlink_mask;
307     } else
308         inode -> u.reiserfs_i.i_flags &= ~i_link_saved_truncate_mask;
309  
310     journal_end (&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
311 }
312
313
314 static void reiserfs_put_super (struct super_block * s)
315 {
316   int i;
317   struct reiserfs_transaction_handle th ;
318   
319   /* change file system state to current state if it was mounted with read-write permissions */
320   if (!(s->s_flags & MS_RDONLY)) {
321     journal_begin(&th, s, 10) ;
322     reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
323     set_sb_state( SB_DISK_SUPER_BLOCK(s), s->u.reiserfs_sb.s_mount_state );
324     journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
325   }
326
327   /* note, journal_release checks for readonly mount, and can decide not
328   ** to do a journal_end
329   */
330   journal_release(&th, s) ;
331
332   for (i = 0; i < SB_BMAP_NR (s); i ++)
333     brelse (SB_AP_BITMAP (s)[i].bh);
334
335   vfree (SB_AP_BITMAP (s));
336
337   brelse (SB_BUFFER_WITH_SB (s));
338
339   print_statistics (s);
340
341   if (s->u.reiserfs_sb.s_kmallocs != 0) {
342     reiserfs_warning ("vs-2004: reiserfs_put_super: allocated memory left %d\n",
343                       s->u.reiserfs_sb.s_kmallocs);
344   }
345
346   if (s->u.reiserfs_sb.reserved_blocks != 0) {
347     reiserfs_warning ("green-2005: reiserfs_put_super: reserved blocks left %d\n",
348                       s->u.reiserfs_sb.reserved_blocks);
349   }
350
351   reiserfs_proc_unregister( s, "journal" );
352   reiserfs_proc_unregister( s, "oidmap" );
353   reiserfs_proc_unregister( s, "on-disk-super" );
354   reiserfs_proc_unregister( s, "bitmap" );
355   reiserfs_proc_unregister( s, "per-level" );
356   reiserfs_proc_unregister( s, "super" );
357   reiserfs_proc_unregister( s, "version" );
358   reiserfs_proc_info_done( s );
359   return;
360 }
361
362 /* we don't mark inodes dirty, we just log them */
363 static void reiserfs_dirty_inode (struct inode * inode) {
364     struct reiserfs_transaction_handle th ;
365
366     if (inode->i_sb->s_flags & MS_RDONLY) {
367         reiserfs_warning("clm-6006: writing inode %lu on readonly FS\n", 
368                           inode->i_ino) ;
369         return ;
370     }
371     lock_kernel() ;
372
373     /* this is really only used for atime updates, so they don't have
374     ** to be included in O_SYNC or fsync
375     */
376     journal_begin(&th, inode->i_sb, 1) ;
377     reiserfs_update_sd (&th, inode);
378     journal_end(&th, inode->i_sb, 1) ;
379     unlock_kernel() ;
380 }
381
382 struct super_operations reiserfs_sops = 
383 {
384   read_inode: reiserfs_read_inode,
385   read_inode2: reiserfs_read_inode2,
386   write_inode: reiserfs_write_inode,
387   dirty_inode: reiserfs_dirty_inode,
388   delete_inode: reiserfs_delete_inode,
389   put_super: reiserfs_put_super,
390   write_super: reiserfs_write_super,
391   write_super_lockfs: reiserfs_write_super_lockfs,
392   unlockfs: reiserfs_unlockfs,
393   statfs: reiserfs_statfs,
394   remount_fs: reiserfs_remount,
395
396   fh_to_dentry: reiserfs_fh_to_dentry,
397   dentry_to_fh: reiserfs_dentry_to_fh,
398
399 };
400
401 /* this struct is used in reiserfs_getopt () for containing the value for those
402    mount options that have values rather than being toggles. */
403 typedef struct {
404     char * value;
405     int bitmask; /* bit which is to be set in mount_options bitmask when this
406                     value is found, 0 is no bits are to be set */
407 } arg_desc_t;
408
409
410 /* this struct is used in reiserfs_getopt() for describing the set of reiserfs
411    mount options */
412 typedef struct {
413     char * option_name;
414     int arg_required; /* 0 is argument is not required, not 0 otherwise */
415     const arg_desc_t * values; /* list of values accepted by an option */
416     int bitmask;  /* bit which is to be set in mount_options bitmask when this
417                      option is selected, 0 is not bits are to be set */
418 } opt_desc_t;
419
420
421 /* possible values for "-o hash=" and bits which are to be set in s_mount_opt
422    of reiserfs specific part of in-core super block */
423 const arg_desc_t hash[] = {
424     {"rupasov", FORCE_RUPASOV_HASH},
425     {"tea", FORCE_TEA_HASH},
426     {"r5", FORCE_R5_HASH},
427     {"detect", FORCE_HASH_DETECT},
428     {NULL, 0}
429 };
430
431
432 /* possible values for "-o block-allocator=" and bits which are to be set in
433    s_mount_opt of reiserfs specific part of in-core super block */
434 const arg_desc_t balloc[] = {
435     {"noborder", REISERFS_NO_BORDER},
436     {"no_unhashed_relocation", REISERFS_NO_UNHASHED_RELOCATION},
437     {"hashed_relocation", REISERFS_HASHED_RELOCATION},
438     {"test4", REISERFS_TEST4},
439     {NULL, 0}
440 };
441
442 const arg_desc_t tails[] = {
443     {"on", REISERFS_LARGETAIL},
444     {"off", -1},
445     {"small", REISERFS_SMALLTAIL},
446     {NULL, 0}
447 };
448
449
450 /* proceed only one option from a list *cur - string containing of mount options
451    opts - array of options which are accepted
452    opt_arg - if option is found and requires an argument and if it is specifed
453    in the input - pointer to the argument is stored here
454    bit_flags - if option requires to set a certain bit - it is set here
455    return -1 if unknown option is found, opt->arg_required otherwise */
456 static int reiserfs_getopt ( struct super_block * s, char ** cur, opt_desc_t * opts, char ** opt_arg,
457                             unsigned long * bit_flags)
458 {
459     char * p;
460     /* foo=bar, 
461        ^   ^  ^
462        |   |  +-- option_end
463        |   +-- arg_start
464        +-- option_start
465     */
466     const opt_desc_t * opt;
467     const arg_desc_t * arg;
468     
469     
470     p = *cur;
471     
472     /* assume argument cannot contain commas */
473     *cur = strchr (p, ',');
474     if (*cur) {
475         *(*cur) = '\0';
476         (*cur) ++;
477     }
478
479     if ( !strncmp (p, "alloc=", 6) ) {
480         /* Ugly special case, probably we should redo options parser so that
481            it can understand several arguments for some options, also so that
482            it can fill several bitfields with option values. */
483         reiserfs_parse_alloc_options( s, p + 6);
484         return 0;
485     }
486         
487     /* for every option in the list */
488     for (opt = opts; opt->option_name; opt ++) {
489         if (!strncmp (p, opt->option_name, strlen (opt->option_name))) {
490             if (bit_flags && opt->bitmask != -1 )
491                 set_bit (opt->bitmask, bit_flags);
492             break;
493         }
494     }
495     if (!opt->option_name) {
496         printk ("reiserfs_getopt: unknown option \"%s\"\n", p);
497         return -1;
498     }
499     
500     p += strlen (opt->option_name);
501     switch (*p) {
502     case '=':
503         if (!opt->arg_required) {
504             printk ("reiserfs_getopt: the option \"%s\" does not require an argument\n",
505                     opt->option_name);
506             return -1;
507         }
508         break;
509         
510     case 0:
511         if (opt->arg_required) {
512             printk ("reiserfs_getopt: the option \"%s\" requires an argument\n", opt->option_name);
513             return -1;
514         }
515         break;
516     default:
517         printk ("reiserfs_getopt: head of option \"%s\" is only correct\n", opt->option_name);
518         return -1;
519     }
520         
521     /* move to the argument, or to next option if argument is not required */
522     p ++;
523     
524     if ( opt->arg_required && !strlen (p) ) {
525         /* this catches "option=," */
526         printk ("reiserfs_getopt: empty argument for \"%s\"\n", opt->option_name);
527         return -1;
528     }
529     
530     if (!opt->values) {
531         /* *opt_arg contains pointer to argument */
532         *opt_arg = p;
533         return opt->arg_required;
534     }
535     
536     /* values possible for this option are listed in opt->values */
537     for (arg = opt->values; arg->value; arg ++) {
538         if (!strcmp (p, arg->value)) {
539             if (bit_flags && arg->bitmask != -1 )
540                 set_bit (arg->bitmask, bit_flags);
541             return opt->arg_required;
542         }
543     }
544     
545     printk ("reiserfs_getopt: bad value \"%s\" for option \"%s\"\n", p, opt->option_name);
546     return -1;
547 }
548
549
550 /* returns 0 if something is wrong in option string, 1 - otherwise */
551 static int reiserfs_parse_options (struct super_block * s, char * options, /* string given via mount's -o */
552                                    unsigned long * mount_options,
553                                    /* after the parsing phase, contains the
554                                       collection of bitflags defining what
555                                       mount options were selected. */
556                                    unsigned long * blocks) /* strtol-ed from NNN of resize=NNN */
557 {
558     int c;
559     char * arg = NULL;
560     char * pos;
561     opt_desc_t opts[] = {
562                 {"tails", 't', tails, -1},
563                 {"notail", 0, 0, -1}, /* Compatibility stuff, so that -o notail for old setups still work */
564                 {"conv", 0, 0, REISERFS_CONVERT}, 
565                 {"nolog", 0, 0, -1},
566                 {"replayonly", 0, 0, REPLAYONLY},
567                 
568                 {"block-allocator", 'a', balloc, -1}, 
569                 {"hash", 'h', hash, FORCE_HASH_DETECT},
570                 
571                 {"resize", 'r', 0, -1},
572                 {"attrs", 0, 0, REISERFS_ATTRS},
573                 {NULL, 0, 0, 0}
574     };
575         
576     *blocks = 0;
577     if (!options || !*options)
578         /* use default configuration: create tails, journaling on, no
579            conversion to newest format */
580         return 1;
581     else
582         /* Drop defaults to zeroes */
583         *mount_options = 0;
584     
585     for (pos = options; pos; ) {
586         c = reiserfs_getopt (s, &pos, opts, &arg, mount_options);
587         if (c == -1)
588             /* wrong option is given */
589             return 0;
590         
591         if (c == 'r') {
592             char * p;
593             
594             p = 0;
595             /* "resize=NNN" */
596             *blocks = simple_strtoul (arg, &p, 0);
597             if (*p != '\0') {
598                 /* NNN does not look like a number */
599                 printk ("reiserfs_parse_options: bad value %s\n", arg);
600                 return 0;
601             }
602         }
603     }
604     
605     return 1;
606 }
607
608
609 int reiserfs_is_super(struct super_block *s) {
610    return (s->s_dev != 0 && s->s_op == &reiserfs_sops) ;
611 }
612
613
614 static void handle_attrs( struct super_block *s )
615 {
616         struct reiserfs_super_block * rs;
617
618         if( reiserfs_attrs( s ) ) {
619                 rs = SB_DISK_SUPER_BLOCK (s);
620                 if( old_format_only(s) ) {
621                         reiserfs_warning( "reiserfs: cannot support attributes on 3.5.x disk format\n" );
622                         s -> u.reiserfs_sb.s_mount_opt &= ~ ( 1 << REISERFS_ATTRS );
623                         return;
624                 }
625                 if( !( le32_to_cpu( rs -> s_flags ) & reiserfs_attrs_cleared ) ) {
626                                 reiserfs_warning( "reiserfs: cannot support attributes until flag is set in super-block\n" );
627                                 s -> u.reiserfs_sb.s_mount_opt &= ~ ( 1 << REISERFS_ATTRS );
628                 }
629         }
630 }
631
632 static int reiserfs_remount (struct super_block * s, int * mount_flags, char * data)
633 {
634   struct reiserfs_super_block * rs;
635   struct reiserfs_transaction_handle th ;
636   unsigned long blocks;
637   unsigned long mount_options = 0;
638
639   rs = SB_DISK_SUPER_BLOCK (s);
640
641   if (!reiserfs_parse_options(s, data, &mount_options, &blocks))
642         return 0;
643
644 #define SET_OPT( opt, bits, super )                                     \
645     if( ( bits ) & ( 1 << ( opt ) ) )                                   \
646             ( super ) -> u.reiserfs_sb.s_mount_opt |= ( 1 << ( opt ) )
647
648   /* set options in the super-block bitmask */
649   SET_OPT( REISERFS_SMALLTAIL, mount_options, s );
650   SET_OPT( REISERFS_LARGETAIL, mount_options, s );
651   SET_OPT( REISERFS_NO_BORDER, mount_options, s );
652   SET_OPT( REISERFS_NO_UNHASHED_RELOCATION, mount_options, s );
653   SET_OPT( REISERFS_HASHED_RELOCATION, mount_options, s );
654   SET_OPT( REISERFS_TEST4, mount_options, s );
655   SET_OPT( REISERFS_ATTRS, mount_options, s );
656 #undef SET_OPT
657
658   handle_attrs( s );
659
660   if(blocks) {
661       int rc = reiserfs_resize(s, blocks);
662       if (rc != 0)
663           return rc;
664   }
665
666   if (*mount_flags & MS_RDONLY) {
667     /* remount read-only */
668     if (s->s_flags & MS_RDONLY)
669       /* it is read-only already */
670       return 0;
671     /* try to remount file system with read-only permissions */
672     if (sb_state(rs) == REISERFS_VALID_FS || s->u.reiserfs_sb.s_mount_state != REISERFS_VALID_FS) {
673       return 0;
674     }
675
676     journal_begin(&th, s, 10) ;
677     /* Mounting a rw partition read-only. */
678     reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
679     set_sb_state( rs, s->u.reiserfs_sb.s_mount_state );
680     journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
681     s->s_dirt = 0;
682   } else {
683     /* remount read-write */
684     if (!(s->s_flags & MS_RDONLY))
685         return 0; /* We are read-write already */
686
687     s->u.reiserfs_sb.s_mount_state = sb_state(rs) ;
688     s->s_flags &= ~MS_RDONLY ; /* now it is safe to call journal_begin */
689     journal_begin(&th, s, 10) ;
690     
691     /* Mount a partition which is read-only, read-write */
692     reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
693     s->u.reiserfs_sb.s_mount_state = sb_state(rs);
694     s->s_flags &= ~MS_RDONLY;
695     set_sb_state( rs, REISERFS_ERROR_FS );
696     /* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
697     journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
698     s->s_dirt = 0;
699     s->u.reiserfs_sb.s_mount_state = REISERFS_VALID_FS ;
700   }
701   /* this will force a full flush of all journal lists */
702   SB_JOURNAL(s)->j_must_wait = 1 ;
703   journal_end(&th, s, 10) ;
704
705   if (!( *mount_flags & MS_RDONLY ) )
706     finish_unfinished( s );
707
708   return 0;
709 }
710
711 /* load_bitmap_info_data - Sets up the reiserfs_bitmap_info structure from disk.
712  * @sb - superblock for this filesystem
713  * @bi - the bitmap info to be loaded. Requires that bi->bh is valid.
714  *
715  * This routine counts how many free bits there are, finding the first zero
716  * as a side effect. Could also be implemented as a loop of test_bit() calls, or
717  * a loop of find_first_zero_bit() calls. This implementation is similar to
718  * find_first_zero_bit(), but doesn't return after it finds the first bit.
719  * Should only be called on fs mount, but should be fairly efficient anyways.
720  *
721  * bi->first_zero_hint is considered unset if it == 0, since the bitmap itself
722  * will * invariably occupt block 0 represented in the bitmap. The only
723  * exception to this is when free_count also == 0, since there will be no
724  * free blocks at all.
725  */
726 static void load_bitmap_info_data (struct super_block *sb,
727                                    struct reiserfs_bitmap_info *bi)
728 {
729     unsigned long *cur = (unsigned long *)bi->bh->b_data;
730
731     while ((char *)cur < (bi->bh->b_data + sb->s_blocksize)) {
732
733         /* No need to scan if all 0's or all 1's.
734          * Since we're only counting 0's, we can simply ignore all 1's */
735         if (*cur == 0) {
736             if (bi->first_zero_hint == 0) {
737                 bi->first_zero_hint = ((char *)cur - bi->bh->b_data) << 3;
738             }
739             bi->free_count += sizeof ( unsigned long ) * 8;
740         } else if (*cur != ~0L) {
741             int b;
742             for (b = 0; b < sizeof ( unsigned long ) * 8; b++) {
743                 if (!reiserfs_test_le_bit (b, cur)) {
744                     bi->free_count ++;
745                     if (bi->first_zero_hint == 0)
746                         bi->first_zero_hint =
747                                         (((char *)cur - bi->bh->b_data) << 3) + b;
748                     }
749                 }
750             }
751         cur ++;
752     }
753
754 #ifdef CONFIG_REISERFS_CHECK
755 // This outputs a lot of unneded info on big FSes
756 //    reiserfs_warning ("bitmap loaded from block %d: %d free blocks\n",
757 //                    bi->bh->b_blocknr, bi->free_count);
758 #endif
759 }
760
761 static int read_bitmaps (struct super_block * s)
762 {
763     int i, bmp;
764
765     SB_AP_BITMAP (s) = vmalloc (sizeof (struct reiserfs_bitmap_info) * SB_BMAP_NR(s));
766     if (SB_AP_BITMAP (s) == 0)
767       return 1;
768     memset (SB_AP_BITMAP (s), 0, sizeof (struct reiserfs_bitmap_info) * SB_BMAP_NR(s));
769
770     for (i = 0, bmp = REISERFS_DISK_OFFSET_IN_BYTES / s->s_blocksize + 1;
771          i < SB_BMAP_NR(s); i++, bmp = s->s_blocksize * 8 * i) {
772       SB_AP_BITMAP (s)[i].bh = sb_getblk (s, bmp);
773       if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh))
774         ll_rw_block(READ, 1, &SB_AP_BITMAP(s)[i].bh); 
775     }
776     for (i = 0; i < SB_BMAP_NR(s); i++) {
777       wait_on_buffer(SB_AP_BITMAP (s)[i].bh);
778       if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) {
779         reiserfs_warning("sh-2029: reiserfs read_bitmaps: "
780                          "bitmap block (#%lu) reading failed\n",
781                          SB_AP_BITMAP(s)[i].bh->b_blocknr);
782         for (i = 0; i < SB_BMAP_NR(s); i++)
783           brelse(SB_AP_BITMAP(s)[i].bh);
784         vfree(SB_AP_BITMAP(s));
785         SB_AP_BITMAP(s) = NULL;
786         return 1;
787       }
788       load_bitmap_info_data (s, SB_AP_BITMAP (s) + i);
789     }   
790     return 0;
791 }
792
793 static int read_old_bitmaps (struct super_block * s)
794 {
795   int i ;
796   struct reiserfs_super_block * rs = SB_DISK_SUPER_BLOCK(s);
797   int bmp1 = (REISERFS_OLD_DISK_OFFSET_IN_BYTES / s->s_blocksize) + 1;  /* first of bitmap blocks */
798
799   /* read true bitmap */
800   SB_AP_BITMAP (s) = vmalloc (sizeof (struct reiserfs_buffer_info *) * sb_bmap_nr(rs));
801   if (SB_AP_BITMAP (s) == 0)
802     return 1;
803
804   memset (SB_AP_BITMAP (s), 0, sizeof (struct reiserfs_buffer_info *) * sb_bmap_nr(rs));
805
806   for (i = 0; i < sb_bmap_nr(rs); i ++) {
807     SB_AP_BITMAP (s)[i].bh = reiserfs_bread (s, bmp1 + i, s->s_blocksize);
808     if (!SB_AP_BITMAP (s)[i].bh)
809       return 1;
810     load_bitmap_info_data (s, SB_AP_BITMAP (s) + i);
811   }
812
813   return 0;
814 }
815
816 void check_bitmap (struct super_block * s)
817 {
818   int i = 0;
819   int free = 0;
820   char * buf;
821
822   while (i < SB_BLOCK_COUNT (s)) {
823     buf = SB_AP_BITMAP (s)[i / (s->s_blocksize * 8)].bh->b_data;
824     if (!reiserfs_test_le_bit (i % (s->s_blocksize * 8), buf))
825       free ++;
826     i ++;
827   }
828
829   if (free != SB_FREE_BLOCKS (s))
830     reiserfs_warning ("vs-4000: check_bitmap: %d free blocks, must be %d\n",
831                       free, SB_FREE_BLOCKS (s));
832 }
833
834 static int read_super_block (struct super_block * s, int size, int offset)
835 {
836     struct buffer_head * bh;
837     struct reiserfs_super_block * rs;
838  
839
840     bh = bread (s->s_dev, offset / size, size);
841     if (!bh) {
842       printk ("read_super_block: "
843               "bread failed (dev %s, block %d, size %d)\n",
844               kdevname (s->s_dev), offset / size, size);
845       return 1;
846     }
847  
848     rs = (struct reiserfs_super_block *)bh->b_data;
849     if (!is_reiserfs_magic_string (rs)) {
850       printk ("read_super_block: "
851               "can't find a reiserfs filesystem on (dev %s, block %lu, size %d)\n",
852               kdevname(s->s_dev), bh->b_blocknr, size);
853       brelse (bh);
854       return 1;
855     }
856  
857     //
858     // ok, reiserfs signature (old or new) found in at the given offset
859     //    
860     s->s_blocksize = sb_blocksize(rs);
861     s->s_blocksize_bits = 0;
862     while ((1 << s->s_blocksize_bits) != s->s_blocksize)
863         s->s_blocksize_bits ++;
864
865     brelse (bh);
866
867     if (s->s_blocksize != 4096) {
868         printk("Unsupported reiserfs blocksize: %d on %s, only 4096 bytes "
869                "blocksize is supported.\n", s->s_blocksize, kdevname (s->s_dev));
870         return 1;
871     }
872     
873     if (s->s_blocksize != size)
874         set_blocksize (s->s_dev, s->s_blocksize);
875
876     bh = reiserfs_bread (s, offset / s->s_blocksize, s->s_blocksize);
877     if (!bh) {
878         printk("read_super_block: "
879                 "bread failed (dev %s, block %d, size %d)\n",
880                 kdevname (s->s_dev), offset / size, size);
881         return 1;
882     }
883     
884     rs = (struct reiserfs_super_block *)bh->b_data;
885     if (!is_reiserfs_magic_string (rs) ||
886         sb_blocksize(rs) != s->s_blocksize) {
887         printk ("read_super_block: "
888                 "can't find a reiserfs filesystem on (dev %s, block %lu, size %d)\n",
889                 kdevname(s->s_dev), bh->b_blocknr, size);
890         brelse (bh);
891         printk ("read_super_block: can't find a reiserfs filesystem on dev %s.\n", kdevname(s->s_dev));
892         return 1;
893     }
894     /* must check to be sure we haven't pulled an old format super out
895     ** of the old format's log.  This is a kludge of a check, but it
896     ** will work.  If block we've just read in is inside the
897     ** journal for that super, it can't be valid.  
898     */
899     if (bh->b_blocknr >= sb_journal_block(rs) && 
900         bh->b_blocknr < (sb_journal_block(rs) + JOURNAL_BLOCK_COUNT)) {
901         brelse(bh) ;
902         printk("super-459: read_super_block: "
903                "super found at block %lu is within its own log. "
904                "It must not be of this format type.\n", bh->b_blocknr) ;
905         return 1 ;
906     }
907
908     if ( rs->s_root_block == -1 ) {
909         brelse(bh) ;
910         printk("dev %s: Unfinished reiserfsck --rebuild-tree run detected. Please run\n"
911                "reiserfsck --rebuild-tree and wait for a completion. If that fails\n"
912                "get newer reiserfsprogs package\n", kdevname (s->s_dev));
913         return 1;
914     }
915
916     SB_BUFFER_WITH_SB (s) = bh;
917     SB_DISK_SUPER_BLOCK (s) = rs;
918
919     s->s_op = &reiserfs_sops;
920
921     /* new format is limited by the 32 bit wide i_blocks field, want to
922     ** be one full block below that.
923     */
924     s->s_maxbytes = (512LL << 32) - s->s_blocksize ;
925     return 0;
926 }
927
928
929
930 /* after journal replay, reread all bitmap and super blocks */
931 static int reread_meta_blocks(struct super_block *s) {
932   int i ;
933   ll_rw_block(READ, 1, &(SB_BUFFER_WITH_SB(s))) ;
934   wait_on_buffer(SB_BUFFER_WITH_SB(s)) ;
935   if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
936     printk("reread_meta_blocks, error reading the super\n") ;
937     return 1 ;
938   }
939
940   for (i = 0; i < SB_BMAP_NR(s) ; i++) {
941     ll_rw_block(READ, 1, &(SB_AP_BITMAP(s)[i].bh)) ;
942     wait_on_buffer(SB_AP_BITMAP(s)[i].bh) ;
943     if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) {
944       printk("reread_meta_blocks, error reading bitmap block number %d at
945       %ld\n", i, SB_AP_BITMAP(s)[i].bh->b_blocknr) ;
946       return 1 ;
947     }
948   }
949   return 0 ;
950
951 }
952
953
954 /////////////////////////////////////////////////////
955 // hash detection stuff
956
957
958 // if root directory is empty - we set default - Yura's - hash and
959 // warn about it
960 // FIXME: we look for only one name in a directory. If tea and yura
961 // bith have the same value - we ask user to send report to the
962 // mailing list
963 __u32 find_hash_out (struct super_block * s)
964 {
965     int retval;
966     struct inode * inode;
967     struct cpu_key key;
968     INITIALIZE_PATH (path);
969     struct reiserfs_dir_entry de;
970     __u32 hash = DEFAULT_HASH;
971
972     inode = s->s_root->d_inode;
973
974     do { // Some serious "goto"-hater was there ;)
975         u32 teahash, r5hash, yurahash;
976
977         make_cpu_key (&key, inode, ~0, TYPE_DIRENTRY, 3);
978         retval = search_by_entry_key (s, &key, &path, &de);
979         if (retval == IO_ERROR) {
980             pathrelse (&path);
981             return UNSET_HASH ;
982         }
983         if (retval == NAME_NOT_FOUND)
984             de.de_entry_num --;
985         set_de_name_and_namelen (&de);
986         if (deh_offset( &(de.de_deh[de.de_entry_num]) ) == DOT_DOT_OFFSET) {
987             /* allow override in this case */
988             if (reiserfs_rupasov_hash(s)) {
989                 hash = YURA_HASH ;
990             }
991             reiserfs_warning("reiserfs: FS seems to be empty, autodetect "
992                              "is using the default hash\n");
993             break;
994         }
995         r5hash=GET_HASH_VALUE (r5_hash (de.de_name, de.de_namelen));
996         teahash=GET_HASH_VALUE (keyed_hash (de.de_name, de.de_namelen));
997         yurahash=GET_HASH_VALUE (yura_hash (de.de_name, de.de_namelen));
998         if ( ( (teahash == r5hash) && (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash) ) ||
999              ( (teahash == yurahash) && (yurahash == GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])))) ) ||
1000              ( (r5hash == yurahash) && (yurahash == GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])))) ) ) {
1001             reiserfs_warning("reiserfs: Unable to automatically detect hash"
1002                 "function for device %s\n"
1003                 "please mount with -o hash={tea,rupasov,r5}\n", kdevname (s->s_dev));
1004             hash = UNSET_HASH;
1005             break;
1006         }
1007         if (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])) ) == yurahash)
1008             hash = YURA_HASH;
1009         else if (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])) ) == teahash)
1010             hash = TEA_HASH;
1011         else if (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])) ) == r5hash)
1012             hash = R5_HASH;
1013         else {
1014             reiserfs_warning("reiserfs: Unrecognised hash function for "
1015                              "device %s\n", kdevname (s->s_dev));
1016             hash = UNSET_HASH;
1017         }
1018     } while (0);
1019
1020     pathrelse (&path);
1021     return hash;
1022 }
1023
1024 // finds out which hash names are sorted with
1025 static int what_hash (struct super_block * s)
1026 {
1027     __u32 code;
1028
1029     code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));
1030
1031     /* reiserfs_hash_detect() == true if any of the hash mount options
1032     ** were used.  We must check them to make sure the user isn't
1033     ** using a bad hash value
1034     */
1035     if (code == UNSET_HASH || reiserfs_hash_detect(s))
1036         code = find_hash_out (s);
1037
1038     if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
1039         /* detection has found the hash, and we must check against the 
1040         ** mount options 
1041         */
1042         if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
1043             printk("REISERFS: Error, %s hash detected, "
1044                    "unable to force rupasov hash\n", reiserfs_hashname(code)) ;
1045             code = UNSET_HASH ;
1046         } else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
1047             printk("REISERFS: Error, %s hash detected, "
1048                    "unable to force tea hash\n", reiserfs_hashname(code)) ;
1049             code = UNSET_HASH ;
1050         } else if (reiserfs_r5_hash(s) && code != R5_HASH) {
1051             printk("REISERFS: Error, %s hash detected, "
1052                    "unable to force r5 hash\n", reiserfs_hashname(code)) ;
1053             code = UNSET_HASH ;
1054         } 
1055     } else { 
1056         /* find_hash_out was not called or could not determine the hash */
1057         if (reiserfs_rupasov_hash(s)) {
1058             code = YURA_HASH ;
1059         } else if (reiserfs_tea_hash(s)) {
1060             code = TEA_HASH ;
1061         } else if (reiserfs_r5_hash(s)) {
1062             code = R5_HASH ;
1063         } 
1064     }
1065
1066     /* if we are mounted RW, and we have a new valid hash code, update 
1067     ** the super
1068     */
1069     if (code != UNSET_HASH && 
1070         !(s->s_flags & MS_RDONLY) && 
1071         code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1072         set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1073     }
1074     return code;
1075 }
1076
1077 // return pointer to appropriate function
1078 static hashf_t hash_function (struct super_block * s)
1079 {
1080     switch (what_hash (s)) {
1081     case TEA_HASH:
1082         reiserfs_warning ("Using tea hash to sort names\n");
1083         return keyed_hash;
1084     case YURA_HASH:
1085         reiserfs_warning ("Using rupasov hash to sort names\n");
1086         return yura_hash;
1087     case R5_HASH:
1088         reiserfs_warning ("Using r5 hash to sort names\n");
1089         return r5_hash;
1090     }
1091     return NULL;
1092 }
1093
1094 // this is used to set up correct value for old partitions
1095 int function2code (hashf_t func)
1096 {
1097     if (func == keyed_hash)
1098         return TEA_HASH;
1099     if (func == yura_hash)
1100         return YURA_HASH;
1101     if (func == r5_hash)
1102         return R5_HASH;
1103
1104     BUG() ; // should never happen 
1105
1106     return 0;
1107 }
1108
1109 static struct super_block * reiserfs_read_super (struct super_block * s, void * data, int silent)
1110 {
1111     int size;
1112     struct inode *root_inode;
1113     kdev_t dev = s->s_dev;
1114     int j;
1115     extern int *blksize_size[];
1116     struct reiserfs_transaction_handle th ;
1117     int old_format = 0;
1118     unsigned long blocks;
1119     int jinit_done = 0 ;
1120     struct reiserfs_iget4_args args ;
1121     int old_magic;
1122     struct reiserfs_super_block * rs;
1123
1124
1125     memset (&s->u.reiserfs_sb, 0, sizeof (struct reiserfs_sb_info));
1126
1127     /* Set default values for options: non-aggressive tails */
1128     s->u.reiserfs_sb.s_mount_opt = ( 1 << REISERFS_SMALLTAIL );
1129     /* default block allocator option: skip_busy */
1130     s->u.reiserfs_sb.s_alloc_options.bits = ( 1 << 5);
1131     /* If file grew past 4 blocks, start preallocation blocks for it. */
1132     s->u.reiserfs_sb.s_alloc_options.preallocmin = 4;
1133     /* Preallocate by 8 blocks (9-1) at once */
1134     s->u.reiserfs_sb.s_alloc_options.preallocsize = 9;
1135
1136     if (reiserfs_parse_options (s, (char *) data, &(s->u.reiserfs_sb.s_mount_opt), &blocks) == 0) {
1137         return NULL;
1138     }
1139
1140     if (blocks) {
1141         printk("reserfs: resize option for remount only\n");
1142         return NULL;
1143     }   
1144
1145     if (blksize_size[MAJOR(dev)] && blksize_size[MAJOR(dev)][MINOR(dev)] != 0) {
1146         /* as blocksize is set for partition we use it */
1147         size = blksize_size[MAJOR(dev)][MINOR(dev)];
1148     } else {
1149         size = BLOCK_SIZE;
1150         set_blocksize (s->s_dev, BLOCK_SIZE);
1151     }
1152
1153     /* read block (64-th 1k block), which can contain reiserfs super block */
1154     if (read_super_block (s, size, REISERFS_DISK_OFFSET_IN_BYTES)) {
1155         // try old format (undistributed bitmap, super block in 8-th 1k block of a device)
1156         if (read_super_block (s, size, REISERFS_OLD_DISK_OFFSET_IN_BYTES)) 
1157             goto error;
1158         else
1159             old_format = 1;
1160     }
1161
1162     rs = SB_DISK_SUPER_BLOCK (s);
1163
1164     s->u.reiserfs_sb.s_mount_state = SB_REISERFS_STATE(s);
1165     s->u.reiserfs_sb.s_mount_state = REISERFS_VALID_FS ;
1166
1167     if (old_format ? read_old_bitmaps(s) : read_bitmaps(s)) { 
1168         printk ("reiserfs_read_super: unable to read bitmap\n");
1169         goto error;
1170     }
1171 #ifdef CONFIG_REISERFS_CHECK
1172     printk("reiserfs:warning: CONFIG_REISERFS_CHECK is set ON\n");
1173     printk("reiserfs:warning: - it is slow mode for debugging.\n");
1174 #endif
1175
1176     // set_device_ro(s->s_dev, 1) ;
1177     if (journal_init(s)) {
1178         printk("reiserfs_read_super: unable to initialize journal space\n") ;
1179         goto error ;
1180     } else {
1181         jinit_done = 1 ; /* once this is set, journal_release must be called
1182                          ** if we error out of the mount 
1183                          */
1184     }
1185     if (reread_meta_blocks(s)) {
1186         printk("reiserfs_read_super: unable to reread meta blocks after journal init\n") ;
1187         goto error ;
1188     }
1189
1190     if (replay_only (s))
1191         goto error;
1192
1193     if (is_read_only(s->s_dev) && !(s->s_flags & MS_RDONLY)) {
1194         printk("clm-7000: Detected readonly device, marking FS readonly\n") ;
1195         s->s_flags |= MS_RDONLY ;
1196     }
1197     args.objectid = REISERFS_ROOT_PARENT_OBJECTID ;
1198     root_inode = iget4 (s, REISERFS_ROOT_OBJECTID, 0, (void *)(&args));
1199     if (!root_inode) {
1200         printk ("reiserfs_read_super: get root inode failed\n");
1201         goto error;
1202     }
1203
1204     s->s_root = d_alloc_root(root_inode);  
1205     if (!s->s_root) {
1206         iput(root_inode);
1207         goto error;
1208     }
1209
1210     // define and initialize hash function
1211     s->u.reiserfs_sb.s_hash_function = hash_function (s);
1212     if (s->u.reiserfs_sb.s_hash_function == NULL) {
1213       dput(s->s_root) ;
1214       s->s_root = NULL ;
1215       goto error ;
1216     }
1217
1218     rs = SB_DISK_SUPER_BLOCK (s);
1219     old_magic = strncmp (rs->s_magic,  REISER2FS_SUPER_MAGIC_STRING,
1220                            strlen ( REISER2FS_SUPER_MAGIC_STRING));
1221     if (!old_magic)
1222         set_bit(REISERFS_3_6, &(s->u.reiserfs_sb.s_properties));
1223     else
1224         set_bit(REISERFS_3_5, &(s->u.reiserfs_sb.s_properties));
1225
1226     if (!(s->s_flags & MS_RDONLY)) {
1227
1228         journal_begin(&th, s, 1) ;
1229         reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
1230
1231         set_sb_state( rs, REISERFS_ERROR_FS );
1232
1233         if ( old_magic ) {
1234             // filesystem created under 3.5.x found
1235             if (convert_reiserfs (s)) {
1236                 reiserfs_warning("reiserfs: converting 3.5.x filesystem to the new format\n") ;
1237                 // after this 3.5.x will not be able to mount this partition
1238                 memcpy (rs->s_magic, REISER2FS_SUPER_MAGIC_STRING, 
1239                         sizeof (REISER2FS_SUPER_MAGIC_STRING));
1240
1241                 reiserfs_convert_objectid_map_v1(s) ;
1242                 set_bit(REISERFS_3_6, &(s->u.reiserfs_sb.s_properties));
1243                 clear_bit(REISERFS_3_5, &(s->u.reiserfs_sb.s_properties));
1244             } else {
1245                 reiserfs_warning("reiserfs: using 3.5.x disk format\n") ;
1246             }
1247         }
1248
1249         journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
1250         journal_end(&th, s, 1) ;
1251         
1252         /* look for files which were to be removed in previous session */
1253         finish_unfinished (s);
1254
1255         s->s_dirt = 0;
1256     } else {
1257         if ( old_magic ) {
1258             reiserfs_warning("reiserfs: using 3.5.x disk format\n") ;
1259         }
1260     }
1261     // mark hash in super block: it could be unset. overwrite should be ok
1262     set_sb_hash_function_code( rs, function2code(s->u.reiserfs_sb.s_hash_function ) );
1263
1264     handle_attrs( s );
1265
1266     reiserfs_proc_info_init( s );
1267     reiserfs_proc_register( s, "version", reiserfs_version_in_proc );
1268     reiserfs_proc_register( s, "super", reiserfs_super_in_proc );
1269     reiserfs_proc_register( s, "per-level", reiserfs_per_level_in_proc );
1270     reiserfs_proc_register( s, "bitmap", reiserfs_bitmap_in_proc );
1271     reiserfs_proc_register( s, "on-disk-super", reiserfs_on_disk_super_in_proc );
1272     reiserfs_proc_register( s, "oidmap", reiserfs_oidmap_in_proc );
1273     reiserfs_proc_register( s, "journal", reiserfs_journal_in_proc );
1274     init_waitqueue_head (&(s->u.reiserfs_sb.s_wait));
1275
1276     printk("%s\n", reiserfs_get_version_string()) ;
1277     return s;
1278
1279  error:
1280     if (jinit_done) { /* kill the commit thread, free journal ram */
1281         journal_release_error(NULL, s) ;
1282     }
1283     if (SB_DISK_SUPER_BLOCK (s)) {
1284         for (j = 0; j < SB_BMAP_NR (s); j ++) {
1285             if (SB_AP_BITMAP (s))
1286                 brelse (SB_AP_BITMAP (s)[j].bh);
1287         }
1288         if (SB_AP_BITMAP (s))
1289             vfree (SB_AP_BITMAP (s));
1290     }
1291     if (SB_BUFFER_WITH_SB (s))
1292         brelse(SB_BUFFER_WITH_SB (s));
1293
1294     return NULL;
1295 }
1296
1297
1298 static int reiserfs_statfs (struct super_block * s, struct statfs * buf)
1299 {
1300   struct reiserfs_super_block * rs = SB_DISK_SUPER_BLOCK (s);
1301   
1302   buf->f_namelen = (REISERFS_MAX_NAME (s->s_blocksize));
1303   buf->f_ffree   = -1;
1304   buf->f_files   = -1;
1305   buf->f_bfree   = sb_free_blocks(rs);
1306   buf->f_bavail  = buf->f_bfree;
1307   buf->f_blocks  = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
1308   buf->f_bsize   = s->s_blocksize;
1309   /* changed to accomodate gcc folks.*/
1310   buf->f_type    =  REISERFS_SUPER_MAGIC;
1311   return 0;
1312 }
1313
1314 static DECLARE_FSTYPE_DEV(reiserfs_fs_type,"reiserfs",reiserfs_read_super);
1315
1316 static int __init init_reiserfs_fs (void)
1317 {
1318         reiserfs_proc_info_global_init();
1319         reiserfs_proc_register_global( "version", 
1320                                        reiserfs_global_version_in_proc );
1321         return register_filesystem(&reiserfs_fs_type);
1322 }
1323
1324 MODULE_DESCRIPTION("ReiserFS journaled filesystem");
1325 MODULE_AUTHOR("Hans Reiser <reiser@namesys.com>");
1326 MODULE_LICENSE("GPL");
1327 EXPORT_NO_SYMBOLS;
1328
1329 static void __exit exit_reiserfs_fs(void)
1330 {
1331         reiserfs_proc_unregister_global( "version" );
1332         reiserfs_proc_info_global_done();
1333         unregister_filesystem(&reiserfs_fs_type);
1334 }
1335
1336
1337 module_init(init_reiserfs_fs) ;
1338 module_exit(exit_reiserfs_fs) ;
1339
1340
1341