~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/reiserfs/super.c

Version: ~ [ linux-5.1-rc1 ] ~ [ linux-5.0.3 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.30 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.107 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.164 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.176 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.136 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.63 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp