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