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