vserver 1.9.3
[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_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 struct super_operations reiserfs_sops = 
496 {
497   .alloc_inode = reiserfs_alloc_inode,
498   .destroy_inode = reiserfs_destroy_inode,
499   .write_inode = reiserfs_write_inode,
500   .dirty_inode = reiserfs_dirty_inode,
501   .delete_inode = reiserfs_delete_inode,
502   .clear_inode  = reiserfs_clear_inode,
503   .put_super = reiserfs_put_super,
504   .write_super = reiserfs_write_super,
505   .write_super_lockfs = reiserfs_write_super_lockfs,
506   .unlockfs = reiserfs_unlockfs,
507   .statfs = reiserfs_statfs,
508   .remount_fs = reiserfs_remount,
509
510 };
511
512 static struct export_operations reiserfs_export_ops = {
513   .encode_fh = reiserfs_encode_fh,
514   .decode_fh = reiserfs_decode_fh,
515   .get_parent = reiserfs_get_parent,
516   .get_dentry = reiserfs_get_dentry,
517 } ;
518
519 /* this struct is used in reiserfs_getopt () for containing the value for those
520    mount options that have values rather than being toggles. */
521 typedef struct {
522     char * value;
523     int setmask; /* bitmask which is to set on mount_options bitmask when this
524                     value is found, 0 is no bits are to be changed. */
525     int clrmask; /* bitmask which is to clear on mount_options bitmask when  this
526                     value is found, 0 is no bits are to be changed. This is
527                     applied BEFORE setmask */
528 } arg_desc_t;
529
530
531 /* this struct is used in reiserfs_getopt() for describing the set of reiserfs
532    mount options */
533 typedef struct {
534     char * option_name;
535     int arg_required; /* 0 if argument is not required, not 0 otherwise */
536     const arg_desc_t * values; /* list of values accepted by an option */
537     int setmask; /* bitmask which is to set on mount_options bitmask when this
538                     value is found, 0 is no bits are to be changed. */
539     int clrmask; /* bitmask which is to clear on mount_options bitmask when  this
540                     value is found, 0 is no bits are to be changed. This is
541                     applied BEFORE setmask */
542 } opt_desc_t;
543
544 /* possible values for -o data= */
545 static const arg_desc_t logging_mode[] = {
546     {"ordered", 1<<REISERFS_DATA_ORDERED, (1<<REISERFS_DATA_LOG|1<<REISERFS_DATA_WRITEBACK)},
547     {"journal", 1<<REISERFS_DATA_LOG, (1<<REISERFS_DATA_ORDERED|1<<REISERFS_DATA_WRITEBACK)},
548     {"writeback", 1<<REISERFS_DATA_WRITEBACK, (1<<REISERFS_DATA_ORDERED|1<<REISERFS_DATA_LOG)},
549     {NULL, 0}
550 };
551
552 /* possible values for -o barrier= */
553 static const arg_desc_t barrier_mode[] = {
554     {"none", 1<<REISERFS_BARRIER_NONE, 1<<REISERFS_BARRIER_FLUSH},
555     {"flush", 1<<REISERFS_BARRIER_FLUSH, 1<<REISERFS_BARRIER_NONE},
556     {NULL, 0}
557 };
558
559 /* possible values for "-o block-allocator=" and bits which are to be set in
560    s_mount_opt of reiserfs specific part of in-core super block */
561 static const arg_desc_t balloc[] = {
562     {"noborder", 1<<REISERFS_NO_BORDER, 0},
563     {"border", 0, 1<<REISERFS_NO_BORDER},
564     {"no_unhashed_relocation", 1<<REISERFS_NO_UNHASHED_RELOCATION, 0},
565     {"hashed_relocation", 1<<REISERFS_HASHED_RELOCATION, 0},
566     {"test4", 1<<REISERFS_TEST4, 0},
567     {"notest4", 0, 1<<REISERFS_TEST4},
568     {NULL, 0, 0}
569 };
570
571 static const arg_desc_t tails[] = {
572     {"on", 1<<REISERFS_LARGETAIL, 1<<REISERFS_SMALLTAIL},
573     {"off", 0, (1<<REISERFS_LARGETAIL)|(1<<REISERFS_SMALLTAIL)},
574     {"small", 1<<REISERFS_SMALLTAIL, 1<<REISERFS_LARGETAIL},
575     {NULL, 0, 0}
576 };
577
578 int reiserfs_default_io_size = 128 * 1024; /* Default recommended I/O size is 128k.
579                                               There might be broken applications that are
580                                               confused by this. Use nolargeio mount option
581                                               to get usual i/o size = PAGE_SIZE.
582                                             */
583
584 /* proceed only one option from a list *cur - string containing of mount options
585    opts - array of options which are accepted
586    opt_arg - if option is found and requires an argument and if it is specifed
587    in the input - pointer to the argument is stored here
588    bit_flags - if option requires to set a certain bit - it is set here
589    return -1 if unknown option is found, opt->arg_required otherwise */
590 static int reiserfs_getopt ( struct super_block * s, char ** cur, opt_desc_t * opts, char ** opt_arg,
591                             unsigned long * bit_flags)
592 {
593     char * p;
594     /* foo=bar, 
595        ^   ^  ^
596        |   |  +-- option_end
597        |   +-- arg_start
598        +-- option_start
599     */
600     const opt_desc_t * opt;
601     const arg_desc_t * arg;
602     
603     
604     p = *cur;
605     
606     /* assume argument cannot contain commas */
607     *cur = strchr (p, ',');
608     if (*cur) {
609         *(*cur) = '\0';
610         (*cur) ++;
611     }
612
613     if ( !strncmp (p, "alloc=", 6) ) {
614         /* Ugly special case, probably we should redo options parser so that
615            it can understand several arguments for some options, also so that
616            it can fill several bitfields with option values. */
617         if ( reiserfs_parse_alloc_options( s, p + 6) ) {
618             return -1;
619         } else {
620             return 0;
621         }
622     }
623
624  
625     /* for every option in the list */
626     for (opt = opts; opt->option_name; opt ++) {
627         if (!strncmp (p, opt->option_name, strlen (opt->option_name))) {
628             if (bit_flags) {
629                 *bit_flags &= ~opt->clrmask;
630                 *bit_flags |= opt->setmask;
631             }
632             break;
633         }
634     }
635     if (!opt->option_name) {
636         reiserfs_warning (s, "unknown mount option \"%s\"", p);
637         return -1;
638     }
639     
640     p += strlen (opt->option_name);
641     switch (*p) {
642     case '=':
643         if (!opt->arg_required) {
644             reiserfs_warning (s, "the option \"%s\" does not require an argument",
645                     opt->option_name);
646             return -1;
647         }
648         break;
649         
650     case 0:
651         if (opt->arg_required) {
652             reiserfs_warning (s, "the option \"%s\" requires an argument", opt->option_name);
653             return -1;
654         }
655         break;
656     default:
657         reiserfs_warning (s, "head of option \"%s\" is only correct", opt->option_name);
658         return -1;
659     }
660
661     /* move to the argument, or to next option if argument is not required */
662     p ++;
663     
664     if ( opt->arg_required && !strlen (p) ) {
665         /* this catches "option=," */
666         reiserfs_warning (s, "empty argument for \"%s\"", opt->option_name);
667         return -1;
668     }
669     
670     if (!opt->values) {
671         /* *=NULLopt_arg contains pointer to argument */
672         *opt_arg = p;
673         return opt->arg_required;
674     }
675     
676     /* values possible for this option are listed in opt->values */
677     for (arg = opt->values; arg->value; arg ++) {
678         if (!strcmp (p, arg->value)) {
679             if (bit_flags) {
680                 *bit_flags &= ~arg->clrmask;
681                 *bit_flags |= arg->setmask;
682             }
683             return opt->arg_required;
684         }
685     }
686     
687     reiserfs_warning (s, "bad value \"%s\" for option \"%s\"", p, opt->option_name);
688     return -1;
689 }
690
691 /* returns 0 if something is wrong in option string, 1 - otherwise */
692 static int reiserfs_parse_options (struct super_block * s, char * options, /* string given via mount's -o */
693                                    unsigned long * mount_options,
694                                    /* after the parsing phase, contains the
695                                       collection of bitflags defining what
696                                       mount options were selected. */
697                                    unsigned long * blocks, /* strtol-ed from NNN of resize=NNN */
698                                    char ** jdev_name,
699                                    unsigned int * commit_max_age)
700 {
701     int c;
702     char * arg = NULL;
703     char * pos;
704     opt_desc_t opts[] = {
705         /* Compatibility stuff, so that -o notail for old setups still work */
706         {"tails",       .arg_required = 't', .values = tails},
707         {"notail",      .clrmask = (1<<REISERFS_LARGETAIL)|(1<<REISERFS_SMALLTAIL)},
708         {"conv",        .setmask = 1<<REISERFS_CONVERT},
709         {"attrs",       .setmask = 1<<REISERFS_ATTRS},
710         {"noattrs",     .clrmask = 1<<REISERFS_ATTRS},
711         {"user_xattr",  .setmask = 1<<REISERFS_XATTRS_USER},
712         {"nouser_xattr",.clrmask = 1<<REISERFS_XATTRS_USER},
713         {"tagxid",      .setmask = 1<<REISERFS_TAGXID},
714 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
715         {"acl",         .setmask = 1<<REISERFS_POSIXACL},
716         {"noacl",       .clrmask = 1<<REISERFS_POSIXACL},
717 #endif
718         {"nolog",},      /* This is unsupported */
719         {"replayonly",  .setmask = 1<<REPLAYONLY},
720         {"block-allocator", .arg_required = 'a', .values = balloc},
721         {"data",        .arg_required = 'd', .values = logging_mode},
722         {"barrier",     .arg_required = 'b', .values = barrier_mode},
723         {"resize",      .arg_required = 'r', .values = NULL},
724         {"jdev",        .arg_required = 'j', .values = NULL},
725         {"nolargeio",   .arg_required = 'w', .values = NULL},
726         {"commit",      .arg_required = 'c', .values = NULL},
727         {"usrquota",},
728         {"grpquota",},
729         {NULL,}
730     };
731         
732     *blocks = 0;
733     if (!options || !*options)
734         /* use default configuration: create tails, journaling on, no
735            conversion to newest format */
736         return 1;
737     
738     for (pos = options; pos; ) {
739         c = reiserfs_getopt (s, &pos, opts, &arg, mount_options);
740         if (c == -1)
741             /* wrong option is given */
742             return 0;
743         
744         if (c == 'r') {
745             char * p;
746             
747             p = NULL;
748             /* "resize=NNN" */
749             *blocks = simple_strtoul (arg, &p, 0);
750             if (*p != '\0') {
751                 /* NNN does not look like a number */
752                 reiserfs_warning (s, "reiserfs_parse_options: bad value %s", arg);
753                 return 0;
754             }
755         }
756
757         if ( c == 'c' ) {
758                 char *p = NULL;
759                 unsigned long val = simple_strtoul (arg, &p, 0);
760                 /* commit=NNN (time in seconds) */
761                 if ( *p != '\0' || val >= (unsigned int)-1) {
762                         reiserfs_warning (s, "reiserfs_parse_options: bad value %s", arg);                      return 0;
763                 }
764                 *commit_max_age = (unsigned int)val;
765         }
766
767         if ( c == 'w' ) {
768                 char *p=NULL;
769                 int val = simple_strtoul (arg, &p, 0);
770
771                 if ( *p != '\0') {
772                     reiserfs_warning (s, "reiserfs_parse_options: non-numeric value %s for nolargeio option", arg);
773                     return 0;
774                 }
775                 if ( val ) 
776                     reiserfs_default_io_size = PAGE_SIZE;
777                 else
778                     reiserfs_default_io_size = 128 * 1024;
779         }
780
781         if (c == 'j') {
782             if (arg && *arg && jdev_name) {
783                 if ( *jdev_name ) { //Hm, already assigned?
784                     reiserfs_warning (s, "reiserfs_parse_options: journal device was already  specified to be %s", *jdev_name);
785                     return 0;
786                 }
787                 *jdev_name = arg;
788             }
789         }
790     }
791     
792     return 1;
793 }
794
795 static void switch_data_mode(struct super_block *s, unsigned long mode) {
796     REISERFS_SB(s)->s_mount_opt &= ~((1 << REISERFS_DATA_LOG) |
797                                        (1 << REISERFS_DATA_ORDERED) |
798                                        (1 << REISERFS_DATA_WRITEBACK));
799     REISERFS_SB(s)->s_mount_opt |= (1 << mode);
800 }
801
802 static void handle_data_mode(struct super_block *s, unsigned long mount_options)
803 {
804     if (mount_options & (1 << REISERFS_DATA_LOG)) {
805         if (!reiserfs_data_log(s)) {
806             switch_data_mode(s, REISERFS_DATA_LOG);
807             reiserfs_info (s, "switching to journaled data mode\n");
808         }
809     } else if (mount_options & (1 << REISERFS_DATA_ORDERED)) {
810         if (!reiserfs_data_ordered(s)) {
811             switch_data_mode(s, REISERFS_DATA_ORDERED);
812             reiserfs_info (s, "switching to ordered data mode\n");
813         }
814     } else if (mount_options & (1 << REISERFS_DATA_WRITEBACK)) {
815         if (!reiserfs_data_writeback(s)) {
816             switch_data_mode(s, REISERFS_DATA_WRITEBACK);
817             reiserfs_info (s, "switching to writeback data mode\n");
818         }
819     }
820 }
821
822 static void handle_barrier_mode(struct super_block *s, unsigned long bits) {
823     int flush = (1 << REISERFS_BARRIER_FLUSH);
824     int none = (1 << REISERFS_BARRIER_NONE);
825     int all_barrier = flush | none;
826
827     if (bits & all_barrier) {
828         REISERFS_SB(s)->s_mount_opt &= ~all_barrier;
829         if (bits & flush) {
830             REISERFS_SB(s)->s_mount_opt |= flush;
831             printk("reiserfs: enabling write barrier flush mode\n");
832         } else if (bits & none) {
833             REISERFS_SB(s)->s_mount_opt |= none;
834             printk("reiserfs: write barriers turned off\n");
835         }
836    }
837 }
838
839 static void handle_attrs( struct super_block *s )
840 {
841         struct reiserfs_super_block * rs;
842
843         if( reiserfs_attrs( s ) ) {
844                 rs = SB_DISK_SUPER_BLOCK (s);
845                 if( old_format_only(s) ) {
846                         reiserfs_warning(s, "reiserfs: cannot support attributes on 3.5.x disk format" );
847                         REISERFS_SB(s) -> s_mount_opt &= ~ ( 1 << REISERFS_ATTRS );
848                         return;
849                 }
850                 if( !( le32_to_cpu( rs -> s_flags ) & reiserfs_attrs_cleared ) ) {
851                                 reiserfs_warning(s, "reiserfs: cannot support attributes until flag is set in super-block" );
852                                 REISERFS_SB(s) -> s_mount_opt &= ~ ( 1 << REISERFS_ATTRS );
853                 }
854         }
855 }
856
857 static int reiserfs_remount (struct super_block * s, int * mount_flags, char * arg)
858 {
859   struct reiserfs_super_block * rs;
860   struct reiserfs_transaction_handle th ;
861   unsigned long blocks;
862   unsigned long mount_options = REISERFS_SB(s)->s_mount_opt;
863   unsigned long safe_mask = 0;
864   unsigned int commit_max_age = (unsigned int)-1;
865
866   rs = SB_DISK_SUPER_BLOCK (s);
867
868   if (!reiserfs_parse_options(s, arg, &mount_options, &blocks, NULL, &commit_max_age))
869     return -EINVAL;
870   
871   handle_attrs(s);
872
873   /* Add options that are safe here */
874   safe_mask |= 1 << REISERFS_SMALLTAIL;
875   safe_mask |= 1 << REISERFS_LARGETAIL;
876   safe_mask |= 1 << REISERFS_NO_BORDER;
877   safe_mask |= 1 << REISERFS_NO_UNHASHED_RELOCATION;
878   safe_mask |= 1 << REISERFS_HASHED_RELOCATION;
879   safe_mask |= 1 << REISERFS_TEST4;
880   safe_mask |= 1 << REISERFS_ATTRS;
881   safe_mask |= 1 << REISERFS_XATTRS_USER;
882   safe_mask |= 1 << REISERFS_POSIXACL;
883   safe_mask |= 1 << REISERFS_BARRIER_FLUSH;
884   safe_mask |= 1 << REISERFS_BARRIER_NONE;
885
886   /* Update the bitmask, taking care to keep
887    * the bits we're not allowed to change here */
888   REISERFS_SB(s)->s_mount_opt = (REISERFS_SB(s)->s_mount_opt & ~safe_mask) |  (mount_options & safe_mask);
889
890   if(commit_max_age != 0 && commit_max_age != (unsigned int)-1) {
891     SB_JOURNAL_MAX_COMMIT_AGE(s) = commit_max_age;
892     SB_JOURNAL_MAX_TRANS_AGE(s) = commit_max_age;
893   }
894   else if(commit_max_age == 0)
895   {
896     /* 0 means restore defaults. */
897     SB_JOURNAL_MAX_COMMIT_AGE(s) = SB_JOURNAL_DEFAULT_MAX_COMMIT_AGE(s);
898     SB_JOURNAL_MAX_TRANS_AGE(s) = JOURNAL_MAX_TRANS_AGE;
899   }
900
901   if(blocks) {
902     int rc = reiserfs_resize(s, blocks);
903     if (rc != 0)
904       return rc;
905   }
906
907   if (*mount_flags & MS_RDONLY) {
908     reiserfs_xattr_init (s, *mount_flags);
909     /* remount read-only */
910     if (s->s_flags & MS_RDONLY)
911       /* it is read-only already */
912       return 0;
913     /* try to remount file system with read-only permissions */
914     if (sb_umount_state(rs) == REISERFS_VALID_FS || REISERFS_SB(s)->s_mount_state != REISERFS_VALID_FS) {
915       return 0;
916     }
917
918     journal_begin(&th, s, 10) ;
919     /* Mounting a rw partition read-only. */
920     reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
921     set_sb_umount_state( rs, REISERFS_SB(s)->s_mount_state );
922     journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
923   } else {
924     /* remount read-write */
925     if (!(s->s_flags & MS_RDONLY)) {
926         reiserfs_xattr_init (s, *mount_flags);
927         return 0; /* We are read-write already */
928     }
929
930     handle_data_mode(s, mount_options);
931     handle_barrier_mode(s, mount_options);
932     REISERFS_SB(s)->s_mount_state = sb_umount_state(rs) ;
933     s->s_flags &= ~MS_RDONLY ; /* now it is safe to call journal_begin */
934     journal_begin(&th, s, 10) ;
935     
936     /* Mount a partition which is read-only, read-write */
937     reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
938     REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
939     s->s_flags &= ~MS_RDONLY;
940     set_sb_umount_state( rs, REISERFS_ERROR_FS );
941     /* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
942     journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
943     REISERFS_SB(s)->s_mount_state = REISERFS_VALID_FS ;
944   }
945   /* this will force a full flush of all journal lists */
946   SB_JOURNAL(s)->j_must_wait = 1 ;
947   journal_end(&th, s, 10) ;
948   s->s_dirt = 0;
949
950   if (!( *mount_flags & MS_RDONLY ) ) {
951     finish_unfinished( s );
952     reiserfs_xattr_init (s, *mount_flags);
953   }
954
955   return 0;
956 }
957
958 /* load_bitmap_info_data - Sets up the reiserfs_bitmap_info structure from disk.
959  * @sb - superblock for this filesystem
960  * @bi - the bitmap info to be loaded. Requires that bi->bh is valid.
961  *
962  * This routine counts how many free bits there are, finding the first zero
963  * as a side effect. Could also be implemented as a loop of test_bit() calls, or
964  * a loop of find_first_zero_bit() calls. This implementation is similar to
965  * find_first_zero_bit(), but doesn't return after it finds the first bit.
966  * Should only be called on fs mount, but should be fairly efficient anyways.
967  *
968  * bi->first_zero_hint is considered unset if it == 0, since the bitmap itself
969  * will * invariably occupt block 0 represented in the bitmap. The only
970  * exception to this is when free_count also == 0, since there will be no
971  * free blocks at all.
972  */
973
974 static void load_bitmap_info_data (struct super_block *sb,
975                                    struct reiserfs_bitmap_info *bi)
976 {
977     unsigned long *cur = (unsigned long *)bi->bh->b_data;
978
979     while ((char *)cur < (bi->bh->b_data + sb->s_blocksize)) {
980
981         /* No need to scan if all 0's or all 1's.
982          * Since we're only counting 0's, we can simply ignore all 1's */
983         if (*cur == 0) {
984             if (bi->first_zero_hint == 0) {
985                 bi->first_zero_hint = ((char *)cur - bi->bh->b_data) << 3;
986             }
987             bi->free_count += sizeof(unsigned long)*8;
988         } else if (*cur != ~0L) {
989             int b;
990             for (b = 0; b < sizeof(unsigned long)*8; b++) {
991                 if (!reiserfs_test_le_bit (b, cur)) {
992                     bi->free_count ++;
993                     if (bi->first_zero_hint == 0)
994                         bi->first_zero_hint =
995                                         (((char *)cur - bi->bh->b_data) << 3) + b;
996                     }
997                 }
998             }
999         cur ++;
1000     }
1001
1002 #ifdef CONFIG_REISERFS_CHECK
1003 // This outputs a lot of unneded info on big FSes
1004 //    reiserfs_warning ("bitmap loaded from block %d: %d free blocks",
1005 //                    bi->bh->b_blocknr, bi->free_count);
1006 #endif
1007 }
1008   
1009 static int read_bitmaps (struct super_block * s)
1010 {
1011     int i, bmap_nr;
1012
1013     SB_AP_BITMAP (s) = vmalloc (sizeof (struct reiserfs_bitmap_info) * SB_BMAP_NR(s));
1014     if (SB_AP_BITMAP (s) == 0)
1015         return 1;
1016     memset (SB_AP_BITMAP (s), 0, sizeof (struct reiserfs_bitmap_info) * SB_BMAP_NR(s));
1017     for (i = 0, bmap_nr = REISERFS_DISK_OFFSET_IN_BYTES / s->s_blocksize + 1;
1018          i < SB_BMAP_NR(s); i++, bmap_nr = s->s_blocksize * 8 * i) {
1019         SB_AP_BITMAP (s)[i].bh = sb_getblk(s, bmap_nr);
1020         if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh))
1021             ll_rw_block(READ, 1, &SB_AP_BITMAP(s)[i].bh);
1022     }
1023     for (i = 0; i < SB_BMAP_NR(s); i++) {
1024         wait_on_buffer(SB_AP_BITMAP (s)[i].bh);
1025         if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) {
1026             reiserfs_warning(s,"sh-2029: reiserfs read_bitmaps: "
1027                          "bitmap block (#%lu) reading failed",
1028                          SB_AP_BITMAP(s)[i].bh->b_blocknr);
1029             for (i = 0; i < SB_BMAP_NR(s); i++)
1030                 brelse(SB_AP_BITMAP(s)[i].bh);
1031             vfree(SB_AP_BITMAP(s));
1032             SB_AP_BITMAP(s) = NULL;
1033             return 1;
1034         }
1035         load_bitmap_info_data (s, SB_AP_BITMAP (s) + i);
1036     }
1037     return 0;
1038 }
1039
1040 static int read_old_bitmaps (struct super_block * s)
1041 {
1042   int i ;
1043   struct reiserfs_super_block * rs = SB_DISK_SUPER_BLOCK(s);
1044   int bmp1 = (REISERFS_OLD_DISK_OFFSET_IN_BYTES / s->s_blocksize) + 1;  /* first of bitmap blocks */
1045
1046   /* read true bitmap */
1047   SB_AP_BITMAP (s) = vmalloc (sizeof (struct reiserfs_buffer_info *) * sb_bmap_nr(rs));
1048   if (SB_AP_BITMAP (s) == 0)
1049     return 1;
1050
1051   memset (SB_AP_BITMAP (s), 0, sizeof (struct reiserfs_buffer_info *) * sb_bmap_nr(rs));
1052
1053   for (i = 0; i < sb_bmap_nr(rs); i ++) {
1054     SB_AP_BITMAP (s)[i].bh = sb_bread (s, bmp1 + i);
1055     if (!SB_AP_BITMAP (s)[i].bh)
1056       return 1;
1057     load_bitmap_info_data (s, SB_AP_BITMAP (s) + i);
1058   }
1059
1060   return 0;
1061 }
1062
1063 void check_bitmap (struct super_block * s)
1064 {
1065   int i = 0;
1066   int free = 0;
1067   char * buf;
1068
1069   while (i < SB_BLOCK_COUNT (s)) {
1070     buf = SB_AP_BITMAP (s)[i / (s->s_blocksize * 8)].bh->b_data;
1071     if (!reiserfs_test_le_bit (i % (s->s_blocksize * 8), buf))
1072       free ++;
1073     i ++;
1074   }
1075
1076   if (free != SB_FREE_BLOCKS (s))
1077     reiserfs_warning (s,"vs-4000: check_bitmap: %d free blocks, must be %d",
1078                       free, SB_FREE_BLOCKS (s));
1079 }
1080
1081 static int read_super_block (struct super_block * s, int offset)
1082 {
1083     struct buffer_head * bh;
1084     struct reiserfs_super_block * rs;
1085     int fs_blocksize;
1086  
1087
1088     bh = sb_bread (s, offset / s->s_blocksize);
1089     if (!bh) {
1090       reiserfs_warning (s, "sh-2006: read_super_block: "
1091               "bread failed (dev %s, block %lu, size %lu)",
1092               reiserfs_bdevname (s), offset / s->s_blocksize, s->s_blocksize);
1093       return 1;
1094     }
1095  
1096     rs = (struct reiserfs_super_block *)bh->b_data;
1097     if (!is_any_reiserfs_magic_string (rs)) {
1098       brelse (bh);
1099       return 1;
1100     }
1101  
1102     //
1103     // ok, reiserfs signature (old or new) found in at the given offset
1104     //    
1105     fs_blocksize = sb_blocksize(rs);
1106     brelse (bh);
1107     sb_set_blocksize (s, fs_blocksize);
1108     
1109     bh = sb_bread (s, offset / s->s_blocksize);
1110     if (!bh) {
1111         reiserfs_warning (s, "sh-2007: read_super_block: "
1112                 "bread failed (dev %s, block %lu, size %lu)\n",
1113                 reiserfs_bdevname (s), offset / s->s_blocksize, s->s_blocksize);
1114         return 1;
1115     }
1116     
1117     rs = (struct reiserfs_super_block *)bh->b_data;
1118     if (sb_blocksize(rs) != s->s_blocksize) {
1119         reiserfs_warning (s, "sh-2011: read_super_block: "
1120                 "can't find a reiserfs filesystem on (dev %s, block %Lu, size %lu)\n",
1121                 reiserfs_bdevname (s), (unsigned long long)bh->b_blocknr, s->s_blocksize);
1122         brelse (bh);
1123         return 1;
1124     }
1125
1126     if ( rs->s_v1.s_root_block == -1 ) {
1127        brelse(bh) ;
1128        reiserfs_warning (s, "Unfinished reiserfsck --rebuild-tree run detected. Please run\n"
1129               "reiserfsck --rebuild-tree and wait for a completion. If that fails\n"
1130               "get newer reiserfsprogs package");
1131        return 1;
1132     }
1133
1134     SB_BUFFER_WITH_SB (s) = bh;
1135     SB_DISK_SUPER_BLOCK (s) = rs;
1136
1137     if (is_reiserfs_jr (rs)) {
1138         /* magic is of non-standard journal filesystem, look at s_version to
1139            find which format is in use */
1140         if (sb_version(rs) == REISERFS_VERSION_2)
1141           reiserfs_warning (s, "read_super_block: found reiserfs format \"3.6\""
1142                   " with non-standard journal");
1143         else if (sb_version(rs) == REISERFS_VERSION_1)
1144           reiserfs_warning (s, "read_super_block: found reiserfs format \"3.5\""
1145                   " with non-standard journal");
1146         else {
1147           reiserfs_warning (s, "sh-2012: read_super_block: found unknown "
1148                             "format \"%u\" of reiserfs with non-standard magic",
1149                             sb_version(rs));
1150         return 1;
1151         }
1152     }
1153     else
1154       /* s_version of standard format may contain incorrect information,
1155          so we just look at the magic string */
1156       reiserfs_info (s, "found reiserfs format \"%s\" with standard journal\n",
1157               is_reiserfs_3_5 (rs) ? "3.5" : "3.6");
1158
1159     s->s_op = &reiserfs_sops;
1160     s->s_export_op = &reiserfs_export_ops;
1161
1162     /* new format is limited by the 32 bit wide i_blocks field, want to
1163     ** be one full block below that.
1164     */
1165     s->s_maxbytes = (512LL << 32) - s->s_blocksize ;
1166     return 0;
1167 }
1168
1169
1170
1171 /* after journal replay, reread all bitmap and super blocks */
1172 static int reread_meta_blocks(struct super_block *s) {
1173   int i ;
1174   ll_rw_block(READ, 1, &(SB_BUFFER_WITH_SB(s))) ;
1175   wait_on_buffer(SB_BUFFER_WITH_SB(s)) ;
1176   if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
1177     reiserfs_warning (s, "reread_meta_blocks, error reading the super") ;
1178     return 1 ;
1179   }
1180
1181   for (i = 0; i < SB_BMAP_NR(s) ; i++) {
1182     ll_rw_block(READ, 1, &(SB_AP_BITMAP(s)[i].bh)) ;
1183     wait_on_buffer(SB_AP_BITMAP(s)[i].bh) ;
1184     if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) {
1185       reiserfs_warning (s, "reread_meta_blocks, error reading bitmap block number %d at %llu",
1186         i, (unsigned long long)SB_AP_BITMAP(s)[i].bh->b_blocknr) ;
1187       return 1 ;
1188     }
1189   }
1190   return 0 ;
1191
1192 }
1193
1194
1195 /////////////////////////////////////////////////////
1196 // hash detection stuff
1197
1198
1199 // if root directory is empty - we set default - Yura's - hash and
1200 // warn about it
1201 // FIXME: we look for only one name in a directory. If tea and yura
1202 // bith have the same value - we ask user to send report to the
1203 // mailing list
1204 __u32 find_hash_out (struct super_block * s)
1205 {
1206     int retval;
1207     struct inode * inode;
1208     struct cpu_key key;
1209     INITIALIZE_PATH (path);
1210     struct reiserfs_dir_entry de;
1211     __u32 hash = DEFAULT_HASH;
1212
1213     inode = s->s_root->d_inode;
1214
1215     do { // Some serious "goto"-hater was there ;)
1216         u32 teahash, r5hash, yurahash;
1217
1218         make_cpu_key (&key, inode, ~0, TYPE_DIRENTRY, 3);
1219         retval = search_by_entry_key (s, &key, &path, &de);
1220         if (retval == IO_ERROR) {
1221             pathrelse (&path);
1222             return UNSET_HASH ;
1223         }
1224         if (retval == NAME_NOT_FOUND)
1225             de.de_entry_num --;
1226         set_de_name_and_namelen (&de);
1227         if (deh_offset( &(de.de_deh[de.de_entry_num]) ) == DOT_DOT_OFFSET) {
1228             /* allow override in this case */
1229             if (reiserfs_rupasov_hash(s)) {
1230                 hash = YURA_HASH ;
1231             }
1232             reiserfs_warning(s,"FS seems to be empty, autodetect "
1233                              "is using the default hash");
1234             break;
1235         }
1236         r5hash=GET_HASH_VALUE (r5_hash (de.de_name, de.de_namelen));
1237         teahash=GET_HASH_VALUE (keyed_hash (de.de_name, de.de_namelen));
1238         yurahash=GET_HASH_VALUE (yura_hash (de.de_name, de.de_namelen));
1239         if ( ( (teahash == r5hash) && (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash) ) ||
1240              ( (teahash == yurahash) && (yurahash == GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])))) ) ||
1241              ( (r5hash == yurahash) && (yurahash == GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])))) ) ) {
1242             reiserfs_warning(s,"Unable to automatically detect hash function. "
1243                              "Please mount with -o hash={tea,rupasov,r5}",
1244                              reiserfs_bdevname (s));
1245             hash = UNSET_HASH;
1246             break;
1247         }
1248         if (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])) ) == yurahash)
1249             hash = YURA_HASH;
1250         else if (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])) ) == teahash)
1251             hash = TEA_HASH;
1252         else if (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])) ) == r5hash)
1253             hash = R5_HASH;
1254         else {
1255             reiserfs_warning (s,"Unrecognised hash function");
1256             hash = UNSET_HASH;
1257         }
1258     } while (0);
1259
1260     pathrelse (&path);
1261     return hash;
1262 }
1263
1264 // finds out which hash names are sorted with
1265 static int what_hash (struct super_block * s)
1266 {
1267     __u32 code;
1268
1269     code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));
1270
1271     /* reiserfs_hash_detect() == true if any of the hash mount options
1272     ** were used.  We must check them to make sure the user isn't
1273     ** using a bad hash value
1274     */
1275     if (code == UNSET_HASH || reiserfs_hash_detect(s))
1276         code = find_hash_out (s);
1277
1278     if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
1279         /* detection has found the hash, and we must check against the 
1280         ** mount options 
1281         */
1282         if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
1283             reiserfs_warning (s, "Error, %s hash detected, "
1284                    "unable to force rupasov hash", reiserfs_hashname(code)) ;
1285             code = UNSET_HASH ;
1286         } else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
1287             reiserfs_warning (s, "Error, %s hash detected, "
1288                    "unable to force tea hash", reiserfs_hashname(code)) ;
1289             code = UNSET_HASH ;
1290         } else if (reiserfs_r5_hash(s) && code != R5_HASH) {
1291             reiserfs_warning (s, "Error, %s hash detected, "
1292                    "unable to force r5 hash", reiserfs_hashname(code)) ;
1293             code = UNSET_HASH ;
1294         } 
1295     } else { 
1296         /* find_hash_out was not called or could not determine the hash */
1297         if (reiserfs_rupasov_hash(s)) {
1298             code = YURA_HASH ;
1299         } else if (reiserfs_tea_hash(s)) {
1300             code = TEA_HASH ;
1301         } else if (reiserfs_r5_hash(s)) {
1302             code = R5_HASH ;
1303         } 
1304     }
1305
1306     /* if we are mounted RW, and we have a new valid hash code, update 
1307     ** the super
1308     */
1309     if (code != UNSET_HASH && 
1310         !(s->s_flags & MS_RDONLY) && 
1311         code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1312         set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1313     }
1314     return code;
1315 }
1316
1317 // return pointer to appropriate function
1318 static hashf_t hash_function (struct super_block * s)
1319 {
1320     switch (what_hash (s)) {
1321     case TEA_HASH:
1322         reiserfs_info (s, "Using tea hash to sort names\n");
1323         return keyed_hash;
1324     case YURA_HASH:
1325         reiserfs_info (s, "Using rupasov hash to sort names\n");
1326         return yura_hash;
1327     case R5_HASH:
1328         reiserfs_info (s, "Using r5 hash to sort names\n");
1329         return r5_hash;
1330     }
1331     return NULL;
1332 }
1333
1334 // this is used to set up correct value for old partitions
1335 int function2code (hashf_t func)
1336 {
1337     if (func == keyed_hash)
1338         return TEA_HASH;
1339     if (func == yura_hash)
1340         return YURA_HASH;
1341     if (func == r5_hash)
1342         return R5_HASH;
1343
1344     BUG() ; // should never happen
1345
1346     return 0;
1347 }
1348
1349 #define SWARN(silent, s, ...)                   \
1350         if (!(silent))                          \
1351                 reiserfs_warning (s, __VA_ARGS__)
1352
1353 static int reiserfs_fill_super (struct super_block * s, void * data, int silent)
1354 {
1355     struct inode *root_inode;
1356     int j;
1357     struct reiserfs_transaction_handle th ;
1358     int old_format = 0;
1359     unsigned long blocks;
1360     unsigned int commit_max_age = 0;
1361     int jinit_done = 0 ;
1362     struct reiserfs_iget_args args ;
1363     struct reiserfs_super_block * rs;
1364     char *jdev_name;
1365     struct reiserfs_sb_info *sbi;
1366     int errval = -EINVAL;
1367
1368     sbi = kmalloc(sizeof(struct reiserfs_sb_info), GFP_KERNEL);
1369     if (!sbi) {
1370         errval = -ENOMEM;
1371         goto error;
1372     }
1373     s->s_fs_info = sbi;
1374     memset (sbi, 0, sizeof (struct reiserfs_sb_info));
1375     /* Set default values for options: non-aggressive tails */
1376     REISERFS_SB(s)->s_mount_opt = ( 1 << REISERFS_SMALLTAIL );
1377     /* no preallocation minimum, be smart in
1378        reiserfs_file_write instead */
1379     REISERFS_SB(s)->s_alloc_options.preallocmin = 0;
1380     /* Preallocate by 16 blocks (17-1) at once */
1381     REISERFS_SB(s)->s_alloc_options.preallocsize = 17;
1382     /* Initialize the rwsem for xattr dir */
1383     init_rwsem(&REISERFS_SB(s)->xattr_dir_sem);
1384
1385     /* setup default block allocator options */
1386     reiserfs_init_alloc_options(s);
1387
1388     jdev_name = NULL;
1389     if (reiserfs_parse_options (s, (char *) data, &(sbi->s_mount_opt), &blocks, &jdev_name, &commit_max_age) == 0) {
1390         goto error;
1391     }
1392
1393     if (blocks) {
1394         SWARN (silent, s, "jmacd-7: reiserfs_fill_super: resize option "
1395                "for remount only");
1396         goto error;
1397     }   
1398
1399     /* try old format (undistributed bitmap, super block in 8-th 1k block of a device) */
1400     if (!read_super_block (s, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
1401       old_format = 1;
1402     /* try new format (64-th 1k block), which can contain reiserfs super block */
1403     else if (read_super_block (s, REISERFS_DISK_OFFSET_IN_BYTES)) {
1404       SWARN(silent, s, "sh-2021: reiserfs_fill_super: can not find reiserfs on %s", reiserfs_bdevname (s));
1405       goto error;
1406     }
1407
1408     rs = SB_DISK_SUPER_BLOCK (s);
1409     /* Let's do basic sanity check to verify that underlying device is not
1410        smaller than the filesystem. If the check fails then abort and scream,
1411        because bad stuff will happen otherwise. */
1412     if ( s->s_bdev && s->s_bdev->bd_inode && i_size_read(s->s_bdev->bd_inode) < sb_block_count(rs)*sb_blocksize(rs)) {
1413         SWARN (silent, s, "Filesystem on %s cannot be mounted because it is bigger than the device", reiserfs_bdevname(s));
1414         SWARN(silent, s, "You may need to run fsck or increase size of your LVM partition");
1415         SWARN(silent, s, "Or may be you forgot to reboot after fdisk when it told you to");
1416         goto error;
1417     }
1418
1419     sbi->s_mount_state = SB_REISERFS_STATE(s);
1420     sbi->s_mount_state = REISERFS_VALID_FS ;
1421
1422     if (old_format ? read_old_bitmaps(s) : read_bitmaps(s)) {
1423         SWARN(silent, s, "jmacd-8: reiserfs_fill_super: unable to read bitmap");
1424         goto error;
1425     }
1426 #ifdef CONFIG_REISERFS_CHECK
1427     SWARN (silent, s, "CONFIG_REISERFS_CHECK is set ON");
1428     SWARN (silent, s, "- it is slow mode for debugging.");
1429 #endif
1430
1431     /* make data=ordered the default */
1432     if (!reiserfs_data_log(s) && !reiserfs_data_ordered(s) &&
1433         !reiserfs_data_writeback(s))
1434     {
1435          REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
1436     }
1437
1438     if (reiserfs_data_log(s)) {
1439         reiserfs_info (s, "using journaled data mode\n");
1440     } else if (reiserfs_data_ordered(s)) {
1441         reiserfs_info (s, "using ordered data mode\n");
1442     } else {
1443         reiserfs_info (s, "using writeback data mode\n");
1444     }
1445     if (reiserfs_barrier_flush(s)) {
1446         printk("reiserfs: using flush barriers\n");
1447     }
1448
1449     // set_device_ro(s->s_dev, 1) ;
1450     if( journal_init(s, jdev_name, old_format, commit_max_age) ) {
1451         SWARN(silent, s, "sh-2022: reiserfs_fill_super: unable to initialize journal space") ;
1452         goto error ;
1453     } else {
1454         jinit_done = 1 ; /* once this is set, journal_release must be called
1455                          ** if we error out of the mount
1456                          */
1457     }
1458     if (reread_meta_blocks(s)) {
1459         SWARN(silent, s, "jmacd-9: reiserfs_fill_super: unable to reread meta blocks after journal init") ;
1460         goto error ;
1461     }
1462
1463     if (replay_only (s))
1464         goto error;
1465
1466     if (bdev_read_only(s->s_bdev) && !(s->s_flags & MS_RDONLY)) {
1467         SWARN(silent, s, "clm-7000: Detected readonly device, marking FS readonly") ;
1468         s->s_flags |= MS_RDONLY ;
1469     }
1470     args.objectid = REISERFS_ROOT_OBJECTID ;
1471     args.dirid = REISERFS_ROOT_PARENT_OBJECTID ;
1472     root_inode = iget5_locked (s, REISERFS_ROOT_OBJECTID, reiserfs_find_actor, reiserfs_init_locked_inode, (void *)(&args));
1473     if (!root_inode) {
1474         SWARN(silent, s, "jmacd-10: reiserfs_fill_super: get root inode failed");
1475         goto error;
1476     }
1477
1478     if (root_inode->i_state & I_NEW) {
1479         reiserfs_read_locked_inode(root_inode, &args);
1480         unlock_new_inode(root_inode);
1481     }
1482
1483     s->s_root = d_alloc_root(root_inode);  
1484     if (!s->s_root) {
1485         iput(root_inode);
1486         goto error;
1487     }
1488
1489     // define and initialize hash function
1490     sbi->s_hash_function = hash_function (s);
1491     if (sbi->s_hash_function == NULL) {
1492       dput(s->s_root) ;
1493       s->s_root = NULL ;
1494       goto error ;
1495     }
1496
1497     if (is_reiserfs_3_5 (rs) || (is_reiserfs_jr (rs) && SB_VERSION (s) == REISERFS_VERSION_1))
1498         set_bit(REISERFS_3_5, &(sbi->s_properties));
1499     else
1500         set_bit(REISERFS_3_6, &(sbi->s_properties));
1501     
1502     if (!(s->s_flags & MS_RDONLY)) {
1503
1504         journal_begin(&th, s, 1) ;
1505         reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
1506
1507         set_sb_umount_state( rs, REISERFS_ERROR_FS );
1508         set_sb_fs_state (rs, 0);
1509         
1510         if (old_format_only(s)) {
1511           /* filesystem of format 3.5 either with standard or non-standard
1512              journal */
1513           if (convert_reiserfs (s)) {
1514             /* and -o conv is given */
1515             if(!silent)
1516               reiserfs_info (s,"converting 3.5 filesystem to the 3.6 format") ;
1517
1518             if (is_reiserfs_3_5 (rs))
1519               /* put magic string of 3.6 format. 2.2 will not be able to
1520                  mount this filesystem anymore */
1521               memcpy (rs->s_v1.s_magic, reiserfs_3_6_magic_string,
1522                       sizeof (reiserfs_3_6_magic_string));
1523
1524             set_sb_version(rs,REISERFS_VERSION_2);
1525             reiserfs_convert_objectid_map_v1(s) ;
1526             set_bit(REISERFS_3_6, &(sbi->s_properties));
1527             clear_bit(REISERFS_3_5, &(sbi->s_properties));
1528           } else if (!silent){
1529             reiserfs_info (s, "using 3.5.x disk format\n") ;
1530           }
1531         }
1532
1533         journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
1534         journal_end(&th, s, 1) ;
1535
1536         if (reiserfs_xattr_init (s, s->s_flags)) {
1537             dput (s->s_root);
1538             s->s_root = NULL;
1539             goto error;
1540         }
1541
1542         /* look for files which were to be removed in previous session */
1543         finish_unfinished (s);
1544     } else {
1545         if ( old_format_only(s) && !silent) {
1546             reiserfs_info (s, "using 3.5.x disk format\n") ;
1547         }
1548
1549         if (reiserfs_xattr_init (s, s->s_flags)) {
1550             dput (s->s_root);
1551             s->s_root = NULL;
1552             goto error;
1553         }
1554     }
1555     // mark hash in super block: it could be unset. overwrite should be ok
1556     set_sb_hash_function_code( rs, function2code(sbi->s_hash_function ) );
1557
1558     handle_attrs( s );
1559
1560     reiserfs_proc_info_init( s );
1561
1562     init_waitqueue_head (&(sbi->s_wait));
1563     sbi->bitmap_lock = SPIN_LOCK_UNLOCKED;
1564
1565     return (0);
1566
1567  error:
1568     if (jinit_done) { /* kill the commit thread, free journal ram */
1569         journal_release_error(NULL, s) ;
1570     }
1571     if (SB_DISK_SUPER_BLOCK (s)) {
1572         for (j = 0; j < SB_BMAP_NR (s); j ++) {
1573             if (SB_AP_BITMAP (s))
1574                 brelse (SB_AP_BITMAP (s)[j].bh);
1575         }
1576         if (SB_AP_BITMAP (s))
1577             vfree (SB_AP_BITMAP (s));
1578     }
1579     if (SB_BUFFER_WITH_SB (s))
1580         brelse(SB_BUFFER_WITH_SB (s));
1581
1582     if (sbi != NULL) {
1583         kfree(sbi);
1584     }
1585
1586     s->s_fs_info = NULL;
1587     return errval;
1588 }
1589
1590
1591 static int reiserfs_statfs (struct super_block * s, struct kstatfs * buf)
1592 {
1593   struct reiserfs_super_block * rs = SB_DISK_SUPER_BLOCK (s);
1594   
1595   buf->f_namelen = (REISERFS_MAX_NAME (s->s_blocksize));
1596   buf->f_bfree   = sb_free_blocks(rs);
1597   buf->f_bavail  = buf->f_bfree;
1598   buf->f_blocks  = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
1599   buf->f_bsize   = s->s_blocksize;
1600   /* changed to accommodate gcc folks.*/
1601   buf->f_type    =  REISERFS_SUPER_MAGIC;
1602   return 0;
1603 }
1604
1605 static struct super_block*
1606 get_super_block (struct file_system_type *fs_type, int flags,
1607                  const char *dev_name, void *data)
1608 {
1609         return get_sb_bdev(fs_type, flags, dev_name, data, reiserfs_fill_super);
1610 }
1611
1612 static int __init
1613 init_reiserfs_fs ( void )
1614 {
1615         int ret;
1616
1617         if ((ret = init_inodecache ())) {
1618                 return ret;
1619         }
1620
1621         if ((ret = reiserfs_xattr_register_handlers ()))
1622             goto failed_reiserfs_xattr_register_handlers;
1623
1624         reiserfs_proc_info_global_init ();
1625         reiserfs_proc_register_global ("version", reiserfs_global_version_in_proc);
1626
1627         ret = register_filesystem (& reiserfs_fs_type);
1628
1629         if (ret == 0) {
1630                 return 0;
1631         }
1632
1633         reiserfs_xattr_unregister_handlers ();
1634
1635 failed_reiserfs_xattr_register_handlers:
1636         reiserfs_proc_unregister_global ("version");
1637         reiserfs_proc_info_global_done ();
1638         destroy_inodecache ();
1639
1640         return ret;
1641 }
1642
1643 static void __exit
1644 exit_reiserfs_fs ( void )
1645 {
1646         reiserfs_xattr_unregister_handlers ();
1647         reiserfs_proc_unregister_global ("version");
1648         reiserfs_proc_info_global_done ();
1649         unregister_filesystem (& reiserfs_fs_type);
1650         destroy_inodecache ();
1651 }
1652
1653 struct file_system_type reiserfs_fs_type = {
1654         .owner          = THIS_MODULE,
1655         .name           = "reiserfs",
1656         .get_sb         = get_super_block,
1657         .kill_sb        = kill_block_super,
1658         .fs_flags       = FS_REQUIRES_DEV,
1659 };
1660
1661 MODULE_DESCRIPTION ("ReiserFS journaled filesystem");
1662 MODULE_AUTHOR      ("Hans Reiser <reiser@namesys.com>");
1663 MODULE_LICENSE     ("GPL");
1664
1665 module_init (init_reiserfs_fs);
1666 module_exit (exit_reiserfs_fs);