2 * Copyright 2000-2002 by Hans Reiser, licensing governed by reiserfs/README
5 #include <linux/config.h>
6 #include <linux/module.h>
7 #include <linux/sched.h>
8 #include <linux/vmalloc.h>
9 #include <asm/uaccess.h>
10 #include <linux/reiserfs_fs.h>
11 #include <linux/smp_lock.h>
12 #include <linux/locks.h>
13 #include <linux/init.h>
15 #define REISERFS_OLD_BLOCKSIZE 4096
16 #define REISERFS_SUPER_MAGIC_STRING_OFFSET_NJ 20
18 char reiserfs_super_magic_string[] = REISERFS_SUPER_MAGIC_STRING;
19 char reiser2fs_super_magic_string[] = REISER2FS_SUPER_MAGIC_STRING;
21 static int reiserfs_remount (struct super_block * s, int * flags, char * data);
22 static int reiserfs_statfs (struct super_block * s, struct statfs * buf);
24 static void reiserfs_write_super (struct super_block * s)
29 if (!(s->s_flags & MS_RDONLY)) {
30 dirty = flush_old_commits(s, 1) ;
36 static void reiserfs_write_super_lockfs (struct super_block * s)
40 struct reiserfs_transaction_handle th ;
42 if (!(s->s_flags & MS_RDONLY)) {
43 journal_begin(&th, s, 1) ;
44 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
45 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
46 reiserfs_block_writes(&th) ;
47 journal_end(&th, s, 1) ;
53 void reiserfs_unlockfs(struct super_block *s) {
54 reiserfs_allow_writes(s) ;
57 extern const struct key MAX_KEY;
60 /* this is used to delete "save link" when there are no items of a
61 file it points to. It can either happen if unlink is completed but
62 "save unlink" removal, or if file has both unlink and truncate
63 pending and as unlink completes first (because key of "save link"
64 protecting unlink is bigger that a key lf "save link" which
65 protects truncate), so there left no items to make truncate
67 static void remove_save_link_only (struct super_block * s, struct key * key, int oid_free)
69 struct reiserfs_transaction_handle th;
71 /* we are going to do one balancing */
72 journal_begin (&th, s, JOURNAL_PER_BALANCE_CNT);
74 reiserfs_delete_solid_item (&th, key);
76 /* removals are protected by direct items */
77 reiserfs_release_objectid (&th, le32_to_cpu (key->k_objectid));
79 journal_end (&th, s, JOURNAL_PER_BALANCE_CNT);
83 /* look for uncompleted unlinks and truncates and complete them */
84 static void finish_unfinished (struct super_block * s)
86 INITIALIZE_PATH (path);
87 struct cpu_key max_cpu_key, obj_key;
88 struct key save_link_key;
90 struct item_head * ih;
91 struct buffer_head * bh;
99 /* compose key to look for "save" links */
100 max_cpu_key.version = KEY_FORMAT_3_5;
101 max_cpu_key.on_disk_key = MAX_KEY;
102 max_cpu_key.key_length = 3;
105 s -> u.reiserfs_sb.s_is_unlinked_ok = 1;
107 retval = search_item (s, &max_cpu_key, &path);
108 if (retval != ITEM_NOT_FOUND) {
109 reiserfs_warning ("vs-2140: finish_unfinished: search_by_key returned %d\n",
114 bh = get_last_bh (&path);
115 item_pos = get_item_pos (&path);
116 if (item_pos != B_NR_ITEMS (bh)) {
117 reiserfs_warning ("vs-2060: finish_unfinished: wrong position found\n");
121 ih = B_N_PITEM_HEAD (bh, item_pos);
123 if (le32_to_cpu (ih->ih_key.k_dir_id) != MAX_KEY_OBJECTID)
124 /* there are no "save" links anymore */
127 save_link_key = ih->ih_key;
128 if (is_indirect_le_ih (ih))
133 /* reiserfs_iget needs k_dirid and k_objectid only */
134 item = B_I_PITEM (bh, ih);
135 obj_key.on_disk_key.k_dir_id = le32_to_cpu (*(__u32 *)item);
136 obj_key.on_disk_key.k_objectid = le32_to_cpu (ih->ih_key.k_objectid);
137 obj_key.on_disk_key.u.k_offset_v1.k_offset = 0;
138 obj_key.on_disk_key.u.k_offset_v1.k_uniqueness = 0;
142 inode = reiserfs_iget (s, &obj_key);
144 /* the unlink almost completed, it just did not manage to remove
145 "save" link and release objectid */
146 reiserfs_warning ("vs-2180: finish_unfinished: iget failed for %K\n",
148 remove_save_link_only (s, &save_link_key, 1);
152 if (!truncate && inode->i_nlink) {
153 /* file is not unlinked */
154 reiserfs_warning ("vs-2185: finish_unfinished: file %K is not unlinked\n",
156 remove_save_link_only (s, &save_link_key, 0);
160 if (truncate && S_ISDIR (inode->i_mode) ) {
161 /* We got a truncate request for a dir which is impossible.
162 The only imaginable way is to execute unfinished truncate request
163 then boot into old kernel, remove the file and create dir with
165 reiserfs_warning("green-2101: impossible truncate on a directory %k. Please report\n", INODE_PKEY (inode));
166 remove_save_link_only (s, &save_link_key, 0);
173 inode -> u.reiserfs_i.i_flags |= i_link_saved_truncate_mask;
174 /* not completed truncate found. New size was committed together
176 reiserfs_warning ("Truncating %k to %Ld ..",
177 INODE_PKEY (inode), inode->i_size);
178 reiserfs_truncate_file (inode, 0/*don't update modification time*/);
179 remove_save_link (inode, truncate);
181 inode -> u.reiserfs_i.i_flags |= i_link_saved_unlink_mask;
182 /* not completed unlink (rmdir) found */
183 reiserfs_warning ("Removing %k..", INODE_PKEY (inode));
184 /* removal gets completed in iput */
191 s -> u.reiserfs_sb.s_is_unlinked_ok = 0;
195 reiserfs_warning ("There were %d uncompleted unlinks/truncates. "
196 "Completed\n", done);
199 /* to protect file being unlinked from getting lost we "safe" link files
200 being unlinked. This link will be deleted in the same transaction with last
201 item of file. mounting the filesytem we scan all these links and remove
202 files which almost got lost */
203 void add_save_link (struct reiserfs_transaction_handle * th,
204 struct inode * inode, int truncate)
206 INITIALIZE_PATH (path);
212 /* file can only get one "save link" of each kind */
214 ( inode -> u.reiserfs_i.i_flags & i_link_saved_truncate_mask ),
215 "saved link already exists for truncated inode %lx",
216 ( long ) inode -> i_ino );
218 ( inode -> u.reiserfs_i.i_flags & i_link_saved_unlink_mask ),
219 "saved link already exists for unlinked inode %lx",
220 ( long ) inode -> i_ino );
222 /* setup key of "save" link */
223 key.version = KEY_FORMAT_3_5;
224 key.on_disk_key.k_dir_id = MAX_KEY_OBJECTID;
225 key.on_disk_key.k_objectid = inode->i_ino;
227 /* unlink, rmdir, rename */
228 set_cpu_key_k_offset (&key, 1 + inode->i_sb->s_blocksize);
229 set_cpu_key_k_type (&key, TYPE_DIRECT);
231 /* item head of "safe" link */
232 make_le_item_head (&ih, &key, key.version, 1 + inode->i_sb->s_blocksize, TYPE_DIRECT,
233 4/*length*/, 0xffff/*free space*/);
236 if (S_ISDIR (inode->i_mode))
237 reiserfs_warning("green-2102: Adding a truncate savelink for a directory %k! Please report\n", INODE_PKEY(inode));
238 set_cpu_key_k_offset (&key, 1);
239 set_cpu_key_k_type (&key, TYPE_INDIRECT);
241 /* item head of "safe" link */
242 make_le_item_head (&ih, &key, key.version, 1, TYPE_INDIRECT,
243 4/*length*/, 0/*free space*/);
247 /* look for its place in the tree */
248 retval = search_item (inode->i_sb, &key, &path);
249 if (retval != ITEM_NOT_FOUND) {
250 if ( retval != -ENOSPC )
251 reiserfs_warning ("vs-2100: add_save_link:"
252 "search_by_key (%K) returned %d\n", &key, retval);
257 /* body of "save" link */
258 link = cpu_to_le32 (INODE_PKEY (inode)->k_dir_id);
260 /* put "save" link inot tree */
261 retval = reiserfs_insert_item (th, &path, &key, &ih, (char *)&link);
263 if (retval != -ENOSPC)
264 reiserfs_warning ("vs-2120: add_save_link: insert_item returned %d\n",
268 inode -> u.reiserfs_i.i_flags |= i_link_saved_truncate_mask;
270 inode -> u.reiserfs_i.i_flags |= i_link_saved_unlink_mask;
275 /* this opens transaction unlike add_save_link */
276 void remove_save_link (struct inode * inode, int truncate)
278 struct reiserfs_transaction_handle th;
282 /* we are going to do one balancing only */
283 journal_begin (&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
285 /* setup key of "save" link */
286 key.k_dir_id = cpu_to_le32 (MAX_KEY_OBJECTID);
287 key.k_objectid = INODE_PKEY (inode)->k_objectid;
289 /* unlink, rmdir, rename */
290 set_le_key_k_offset (KEY_FORMAT_3_5, &key,
291 1 + inode->i_sb->s_blocksize);
292 set_le_key_k_type (KEY_FORMAT_3_5, &key, TYPE_DIRECT);
295 set_le_key_k_offset (KEY_FORMAT_3_5, &key, 1);
296 set_le_key_k_type (KEY_FORMAT_3_5, &key, TYPE_INDIRECT);
300 ( inode -> u.reiserfs_i.i_flags & i_link_saved_truncate_mask ) ) ||
302 ( inode -> u.reiserfs_i.i_flags & i_link_saved_unlink_mask ) ) )
303 reiserfs_delete_solid_item (&th, &key);
305 reiserfs_release_objectid (&th, inode->i_ino);
306 inode -> u.reiserfs_i.i_flags &= ~i_link_saved_unlink_mask;
308 inode -> u.reiserfs_i.i_flags &= ~i_link_saved_truncate_mask;
310 journal_end (&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
314 static void reiserfs_put_super (struct super_block * s)
317 struct reiserfs_transaction_handle th ;
319 /* change file system state to current state if it was mounted with read-write permissions */
320 if (!(s->s_flags & MS_RDONLY)) {
321 journal_begin(&th, s, 10) ;
322 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
323 set_sb_state( SB_DISK_SUPER_BLOCK(s), s->u.reiserfs_sb.s_mount_state );
324 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
327 /* note, journal_release checks for readonly mount, and can decide not
328 ** to do a journal_end
330 journal_release(&th, s) ;
332 for (i = 0; i < SB_BMAP_NR (s); i ++)
333 brelse (SB_AP_BITMAP (s)[i].bh);
335 vfree (SB_AP_BITMAP (s));
337 brelse (SB_BUFFER_WITH_SB (s));
339 print_statistics (s);
341 if (s->u.reiserfs_sb.s_kmallocs != 0) {
342 reiserfs_warning ("vs-2004: reiserfs_put_super: allocated memory left %d\n",
343 s->u.reiserfs_sb.s_kmallocs);
346 if (s->u.reiserfs_sb.reserved_blocks != 0) {
347 reiserfs_warning ("green-2005: reiserfs_put_super: reserved blocks left %d\n",
348 s->u.reiserfs_sb.reserved_blocks);
351 reiserfs_proc_unregister( s, "journal" );
352 reiserfs_proc_unregister( s, "oidmap" );
353 reiserfs_proc_unregister( s, "on-disk-super" );
354 reiserfs_proc_unregister( s, "bitmap" );
355 reiserfs_proc_unregister( s, "per-level" );
356 reiserfs_proc_unregister( s, "super" );
357 reiserfs_proc_unregister( s, "version" );
358 reiserfs_proc_info_done( s );
362 /* we don't mark inodes dirty, we just log them */
363 static void reiserfs_dirty_inode (struct inode * inode) {
364 struct reiserfs_transaction_handle th ;
366 if (inode->i_sb->s_flags & MS_RDONLY) {
367 reiserfs_warning("clm-6006: writing inode %lu on readonly FS\n",
373 /* this is really only used for atime updates, so they don't have
374 ** to be included in O_SYNC or fsync
376 journal_begin(&th, inode->i_sb, 1) ;
377 reiserfs_update_sd (&th, inode);
378 journal_end(&th, inode->i_sb, 1) ;
382 struct super_operations reiserfs_sops =
384 read_inode: reiserfs_read_inode,
385 read_inode2: reiserfs_read_inode2,
386 write_inode: reiserfs_write_inode,
387 dirty_inode: reiserfs_dirty_inode,
388 delete_inode: reiserfs_delete_inode,
389 put_super: reiserfs_put_super,
390 write_super: reiserfs_write_super,
391 write_super_lockfs: reiserfs_write_super_lockfs,
392 unlockfs: reiserfs_unlockfs,
393 statfs: reiserfs_statfs,
394 remount_fs: reiserfs_remount,
396 fh_to_dentry: reiserfs_fh_to_dentry,
397 dentry_to_fh: reiserfs_dentry_to_fh,
401 /* this struct is used in reiserfs_getopt () for containing the value for those
402 mount options that have values rather than being toggles. */
405 int bitmask; /* bit which is to be set in mount_options bitmask when this
406 value is found, 0 is no bits are to be set */
410 /* this struct is used in reiserfs_getopt() for describing the set of reiserfs
414 int arg_required; /* 0 is argument is not required, not 0 otherwise */
415 const arg_desc_t * values; /* list of values accepted by an option */
416 int bitmask; /* bit which is to be set in mount_options bitmask when this
417 option is selected, 0 is not bits are to be set */
421 /* possible values for "-o hash=" and bits which are to be set in s_mount_opt
422 of reiserfs specific part of in-core super block */
423 const arg_desc_t hash[] = {
424 {"rupasov", FORCE_RUPASOV_HASH},
425 {"tea", FORCE_TEA_HASH},
426 {"r5", FORCE_R5_HASH},
427 {"detect", FORCE_HASH_DETECT},
432 /* possible values for "-o block-allocator=" and bits which are to be set in
433 s_mount_opt of reiserfs specific part of in-core super block */
434 const arg_desc_t balloc[] = {
435 {"noborder", REISERFS_NO_BORDER},
436 {"no_unhashed_relocation", REISERFS_NO_UNHASHED_RELOCATION},
437 {"hashed_relocation", REISERFS_HASHED_RELOCATION},
438 {"test4", REISERFS_TEST4},
442 const arg_desc_t tails[] = {
443 {"on", REISERFS_LARGETAIL},
445 {"small", REISERFS_SMALLTAIL},
450 /* proceed only one option from a list *cur - string containing of mount options
451 opts - array of options which are accepted
452 opt_arg - if option is found and requires an argument and if it is specifed
453 in the input - pointer to the argument is stored here
454 bit_flags - if option requires to set a certain bit - it is set here
455 return -1 if unknown option is found, opt->arg_required otherwise */
456 static int reiserfs_getopt ( struct super_block * s, char ** cur, opt_desc_t * opts, char ** opt_arg,
457 unsigned long * bit_flags)
466 const opt_desc_t * opt;
467 const arg_desc_t * arg;
472 /* assume argument cannot contain commas */
473 *cur = strchr (p, ',');
479 if ( !strncmp (p, "alloc=", 6) ) {
480 /* Ugly special case, probably we should redo options parser so that
481 it can understand several arguments for some options, also so that
482 it can fill several bitfields with option values. */
483 reiserfs_parse_alloc_options( s, p + 6);
487 /* for every option in the list */
488 for (opt = opts; opt->option_name; opt ++) {
489 if (!strncmp (p, opt->option_name, strlen (opt->option_name))) {
490 if (bit_flags && opt->bitmask != -1 )
491 set_bit (opt->bitmask, bit_flags);
495 if (!opt->option_name) {
496 printk ("reiserfs_getopt: unknown option \"%s\"\n", p);
500 p += strlen (opt->option_name);
503 if (!opt->arg_required) {
504 printk ("reiserfs_getopt: the option \"%s\" does not require an argument\n",
511 if (opt->arg_required) {
512 printk ("reiserfs_getopt: the option \"%s\" requires an argument\n", opt->option_name);
517 printk ("reiserfs_getopt: head of option \"%s\" is only correct\n", opt->option_name);
521 /* move to the argument, or to next option if argument is not required */
524 if ( opt->arg_required && !strlen (p) ) {
525 /* this catches "option=," */
526 printk ("reiserfs_getopt: empty argument for \"%s\"\n", opt->option_name);
531 /* *opt_arg contains pointer to argument */
533 return opt->arg_required;
536 /* values possible for this option are listed in opt->values */
537 for (arg = opt->values; arg->value; arg ++) {
538 if (!strcmp (p, arg->value)) {
539 if (bit_flags && arg->bitmask != -1 )
540 set_bit (arg->bitmask, bit_flags);
541 return opt->arg_required;
545 printk ("reiserfs_getopt: bad value \"%s\" for option \"%s\"\n", p, opt->option_name);
550 /* returns 0 if something is wrong in option string, 1 - otherwise */
551 static int reiserfs_parse_options (struct super_block * s, char * options, /* string given via mount's -o */
552 unsigned long * mount_options,
553 /* after the parsing phase, contains the
554 collection of bitflags defining what
555 mount options were selected. */
556 unsigned long * blocks) /* strtol-ed from NNN of resize=NNN */
561 opt_desc_t opts[] = {
562 {"tails", 't', tails, -1},
563 {"notail", 0, 0, -1}, /* Compatibility stuff, so that -o notail for old setups still work */
564 {"conv", 0, 0, REISERFS_CONVERT},
566 {"replayonly", 0, 0, REPLAYONLY},
568 {"block-allocator", 'a', balloc, -1},
569 {"hash", 'h', hash, FORCE_HASH_DETECT},
571 {"resize", 'r', 0, -1},
572 {"attrs", 0, 0, REISERFS_ATTRS},
577 if (!options || !*options)
578 /* use default configuration: create tails, journaling on, no
579 conversion to newest format */
582 /* Drop defaults to zeroes */
585 for (pos = options; pos; ) {
586 c = reiserfs_getopt (s, &pos, opts, &arg, mount_options);
588 /* wrong option is given */
596 *blocks = simple_strtoul (arg, &p, 0);
598 /* NNN does not look like a number */
599 printk ("reiserfs_parse_options: bad value %s\n", arg);
609 int reiserfs_is_super(struct super_block *s) {
610 return (s->s_dev != 0 && s->s_op == &reiserfs_sops) ;
614 static void handle_attrs( struct super_block *s )
616 struct reiserfs_super_block * rs;
618 if( reiserfs_attrs( s ) ) {
619 rs = SB_DISK_SUPER_BLOCK (s);
620 if( old_format_only(s) ) {
621 reiserfs_warning( "reiserfs: cannot support attributes on 3.5.x disk format\n" );
622 s -> u.reiserfs_sb.s_mount_opt &= ~ ( 1 << REISERFS_ATTRS );
625 if( !( le32_to_cpu( rs -> s_flags ) & reiserfs_attrs_cleared ) ) {
626 reiserfs_warning( "reiserfs: cannot support attributes until flag is set in super-block\n" );
627 s -> u.reiserfs_sb.s_mount_opt &= ~ ( 1 << REISERFS_ATTRS );
632 static int reiserfs_remount (struct super_block * s, int * mount_flags, char * data)
634 struct reiserfs_super_block * rs;
635 struct reiserfs_transaction_handle th ;
636 unsigned long blocks;
637 unsigned long mount_options = 0;
639 rs = SB_DISK_SUPER_BLOCK (s);
641 if (!reiserfs_parse_options(s, data, &mount_options, &blocks))
644 #define SET_OPT( opt, bits, super ) \
645 if( ( bits ) & ( 1 << ( opt ) ) ) \
646 ( super ) -> u.reiserfs_sb.s_mount_opt |= ( 1 << ( opt ) )
648 /* set options in the super-block bitmask */
649 SET_OPT( REISERFS_SMALLTAIL, mount_options, s );
650 SET_OPT( REISERFS_LARGETAIL, mount_options, s );
651 SET_OPT( REISERFS_NO_BORDER, mount_options, s );
652 SET_OPT( REISERFS_NO_UNHASHED_RELOCATION, mount_options, s );
653 SET_OPT( REISERFS_HASHED_RELOCATION, mount_options, s );
654 SET_OPT( REISERFS_TEST4, mount_options, s );
655 SET_OPT( REISERFS_ATTRS, mount_options, s );
661 int rc = reiserfs_resize(s, blocks);
666 if (*mount_flags & MS_RDONLY) {
667 /* remount read-only */
668 if (s->s_flags & MS_RDONLY)
669 /* it is read-only already */
671 /* try to remount file system with read-only permissions */
672 if (sb_state(rs) == REISERFS_VALID_FS || s->u.reiserfs_sb.s_mount_state != REISERFS_VALID_FS) {
676 journal_begin(&th, s, 10) ;
677 /* Mounting a rw partition read-only. */
678 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
679 set_sb_state( rs, s->u.reiserfs_sb.s_mount_state );
680 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
683 /* remount read-write */
684 if (!(s->s_flags & MS_RDONLY))
685 return 0; /* We are read-write already */
687 s->u.reiserfs_sb.s_mount_state = sb_state(rs) ;
688 s->s_flags &= ~MS_RDONLY ; /* now it is safe to call journal_begin */
689 journal_begin(&th, s, 10) ;
691 /* Mount a partition which is read-only, read-write */
692 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
693 s->u.reiserfs_sb.s_mount_state = sb_state(rs);
694 s->s_flags &= ~MS_RDONLY;
695 set_sb_state( rs, REISERFS_ERROR_FS );
696 /* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
697 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
699 s->u.reiserfs_sb.s_mount_state = REISERFS_VALID_FS ;
701 /* this will force a full flush of all journal lists */
702 SB_JOURNAL(s)->j_must_wait = 1 ;
703 journal_end(&th, s, 10) ;
705 if (!( *mount_flags & MS_RDONLY ) )
706 finish_unfinished( s );
711 /* load_bitmap_info_data - Sets up the reiserfs_bitmap_info structure from disk.
712 * @sb - superblock for this filesystem
713 * @bi - the bitmap info to be loaded. Requires that bi->bh is valid.
715 * This routine counts how many free bits there are, finding the first zero
716 * as a side effect. Could also be implemented as a loop of test_bit() calls, or
717 * a loop of find_first_zero_bit() calls. This implementation is similar to
718 * find_first_zero_bit(), but doesn't return after it finds the first bit.
719 * Should only be called on fs mount, but should be fairly efficient anyways.
721 * bi->first_zero_hint is considered unset if it == 0, since the bitmap itself
722 * will * invariably occupt block 0 represented in the bitmap. The only
723 * exception to this is when free_count also == 0, since there will be no
724 * free blocks at all.
726 static void load_bitmap_info_data (struct super_block *sb,
727 struct reiserfs_bitmap_info *bi)
729 unsigned long *cur = (unsigned long *)bi->bh->b_data;
731 while ((char *)cur < (bi->bh->b_data + sb->s_blocksize)) {
733 /* No need to scan if all 0's or all 1's.
734 * Since we're only counting 0's, we can simply ignore all 1's */
736 if (bi->first_zero_hint == 0) {
737 bi->first_zero_hint = ((char *)cur - bi->bh->b_data) << 3;
739 bi->free_count += sizeof ( unsigned long ) * 8;
740 } else if (*cur != ~0L) {
742 for (b = 0; b < sizeof ( unsigned long ) * 8; b++) {
743 if (!reiserfs_test_le_bit (b, cur)) {
745 if (bi->first_zero_hint == 0)
746 bi->first_zero_hint =
747 (((char *)cur - bi->bh->b_data) << 3) + b;
754 #ifdef CONFIG_REISERFS_CHECK
755 // This outputs a lot of unneded info on big FSes
756 // reiserfs_warning ("bitmap loaded from block %d: %d free blocks\n",
757 // bi->bh->b_blocknr, bi->free_count);
761 static int read_bitmaps (struct super_block * s)
765 SB_AP_BITMAP (s) = vmalloc (sizeof (struct reiserfs_bitmap_info) * SB_BMAP_NR(s));
766 if (SB_AP_BITMAP (s) == 0)
768 memset (SB_AP_BITMAP (s), 0, sizeof (struct reiserfs_bitmap_info) * SB_BMAP_NR(s));
770 for (i = 0, bmp = REISERFS_DISK_OFFSET_IN_BYTES / s->s_blocksize + 1;
771 i < SB_BMAP_NR(s); i++, bmp = s->s_blocksize * 8 * i) {
772 SB_AP_BITMAP (s)[i].bh = sb_getblk (s, bmp);
773 if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh))
774 ll_rw_block(READ, 1, &SB_AP_BITMAP(s)[i].bh);
776 for (i = 0; i < SB_BMAP_NR(s); i++) {
777 wait_on_buffer(SB_AP_BITMAP (s)[i].bh);
778 if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) {
779 reiserfs_warning("sh-2029: reiserfs read_bitmaps: "
780 "bitmap block (#%lu) reading failed\n",
781 SB_AP_BITMAP(s)[i].bh->b_blocknr);
782 for (i = 0; i < SB_BMAP_NR(s); i++)
783 brelse(SB_AP_BITMAP(s)[i].bh);
784 vfree(SB_AP_BITMAP(s));
785 SB_AP_BITMAP(s) = NULL;
788 load_bitmap_info_data (s, SB_AP_BITMAP (s) + i);
793 static int read_old_bitmaps (struct super_block * s)
796 struct reiserfs_super_block * rs = SB_DISK_SUPER_BLOCK(s);
797 int bmp1 = (REISERFS_OLD_DISK_OFFSET_IN_BYTES / s->s_blocksize) + 1; /* first of bitmap blocks */
799 /* read true bitmap */
800 SB_AP_BITMAP (s) = vmalloc (sizeof (struct reiserfs_buffer_info *) * sb_bmap_nr(rs));
801 if (SB_AP_BITMAP (s) == 0)
804 memset (SB_AP_BITMAP (s), 0, sizeof (struct reiserfs_buffer_info *) * sb_bmap_nr(rs));
806 for (i = 0; i < sb_bmap_nr(rs); i ++) {
807 SB_AP_BITMAP (s)[i].bh = reiserfs_bread (s, bmp1 + i, s->s_blocksize);
808 if (!SB_AP_BITMAP (s)[i].bh)
810 load_bitmap_info_data (s, SB_AP_BITMAP (s) + i);
816 void check_bitmap (struct super_block * s)
822 while (i < SB_BLOCK_COUNT (s)) {
823 buf = SB_AP_BITMAP (s)[i / (s->s_blocksize * 8)].bh->b_data;
824 if (!reiserfs_test_le_bit (i % (s->s_blocksize * 8), buf))
829 if (free != SB_FREE_BLOCKS (s))
830 reiserfs_warning ("vs-4000: check_bitmap: %d free blocks, must be %d\n",
831 free, SB_FREE_BLOCKS (s));
834 static int read_super_block (struct super_block * s, int size, int offset)
836 struct buffer_head * bh;
837 struct reiserfs_super_block * rs;
840 bh = bread (s->s_dev, offset / size, size);
842 printk ("read_super_block: "
843 "bread failed (dev %s, block %d, size %d)\n",
844 kdevname (s->s_dev), offset / size, size);
848 rs = (struct reiserfs_super_block *)bh->b_data;
849 if (!is_reiserfs_magic_string (rs)) {
850 printk ("read_super_block: "
851 "can't find a reiserfs filesystem on (dev %s, block %lu, size %d)\n",
852 kdevname(s->s_dev), bh->b_blocknr, size);
858 // ok, reiserfs signature (old or new) found in at the given offset
860 s->s_blocksize = sb_blocksize(rs);
861 s->s_blocksize_bits = 0;
862 while ((1 << s->s_blocksize_bits) != s->s_blocksize)
863 s->s_blocksize_bits ++;
867 if (s->s_blocksize != 4096) {
868 printk("Unsupported reiserfs blocksize: %d on %s, only 4096 bytes "
869 "blocksize is supported.\n", s->s_blocksize, kdevname (s->s_dev));
873 if (s->s_blocksize != size)
874 set_blocksize (s->s_dev, s->s_blocksize);
876 bh = reiserfs_bread (s, offset / s->s_blocksize, s->s_blocksize);
878 printk("read_super_block: "
879 "bread failed (dev %s, block %d, size %d)\n",
880 kdevname (s->s_dev), offset / size, size);
884 rs = (struct reiserfs_super_block *)bh->b_data;
885 if (!is_reiserfs_magic_string (rs) ||
886 sb_blocksize(rs) != s->s_blocksize) {
887 printk ("read_super_block: "
888 "can't find a reiserfs filesystem on (dev %s, block %lu, size %d)\n",
889 kdevname(s->s_dev), bh->b_blocknr, size);
891 printk ("read_super_block: can't find a reiserfs filesystem on dev %s.\n", kdevname(s->s_dev));
894 /* must check to be sure we haven't pulled an old format super out
895 ** of the old format's log. This is a kludge of a check, but it
896 ** will work. If block we've just read in is inside the
897 ** journal for that super, it can't be valid.
899 if (bh->b_blocknr >= sb_journal_block(rs) &&
900 bh->b_blocknr < (sb_journal_block(rs) + JOURNAL_BLOCK_COUNT)) {
902 printk("super-459: read_super_block: "
903 "super found at block %lu is within its own log. "
904 "It must not be of this format type.\n", bh->b_blocknr) ;
908 if ( rs->s_root_block == -1 ) {
910 printk("dev %s: Unfinished reiserfsck --rebuild-tree run detected. Please run\n"
911 "reiserfsck --rebuild-tree and wait for a completion. If that fails\n"
912 "get newer reiserfsprogs package\n", kdevname (s->s_dev));
916 SB_BUFFER_WITH_SB (s) = bh;
917 SB_DISK_SUPER_BLOCK (s) = rs;
919 s->s_op = &reiserfs_sops;
921 /* new format is limited by the 32 bit wide i_blocks field, want to
922 ** be one full block below that.
924 s->s_maxbytes = (512LL << 32) - s->s_blocksize ;
930 /* after journal replay, reread all bitmap and super blocks */
931 static int reread_meta_blocks(struct super_block *s) {
933 ll_rw_block(READ, 1, &(SB_BUFFER_WITH_SB(s))) ;
934 wait_on_buffer(SB_BUFFER_WITH_SB(s)) ;
935 if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
936 printk("reread_meta_blocks, error reading the super\n") ;
940 for (i = 0; i < SB_BMAP_NR(s) ; i++) {
941 ll_rw_block(READ, 1, &(SB_AP_BITMAP(s)[i].bh)) ;
942 wait_on_buffer(SB_AP_BITMAP(s)[i].bh) ;
943 if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) {
944 printk("reread_meta_blocks, error reading bitmap block number %d at
945 %ld\n", i, SB_AP_BITMAP(s)[i].bh->b_blocknr) ;
954 /////////////////////////////////////////////////////
955 // hash detection stuff
958 // if root directory is empty - we set default - Yura's - hash and
960 // FIXME: we look for only one name in a directory. If tea and yura
961 // bith have the same value - we ask user to send report to the
963 __u32 find_hash_out (struct super_block * s)
966 struct inode * inode;
968 INITIALIZE_PATH (path);
969 struct reiserfs_dir_entry de;
970 __u32 hash = DEFAULT_HASH;
972 inode = s->s_root->d_inode;
974 do { // Some serious "goto"-hater was there ;)
975 u32 teahash, r5hash, yurahash;
977 make_cpu_key (&key, inode, ~0, TYPE_DIRENTRY, 3);
978 retval = search_by_entry_key (s, &key, &path, &de);
979 if (retval == IO_ERROR) {
983 if (retval == NAME_NOT_FOUND)
985 set_de_name_and_namelen (&de);
986 if (deh_offset( &(de.de_deh[de.de_entry_num]) ) == DOT_DOT_OFFSET) {
987 /* allow override in this case */
988 if (reiserfs_rupasov_hash(s)) {
991 reiserfs_warning("reiserfs: FS seems to be empty, autodetect "
992 "is using the default hash\n");
995 r5hash=GET_HASH_VALUE (r5_hash (de.de_name, de.de_namelen));
996 teahash=GET_HASH_VALUE (keyed_hash (de.de_name, de.de_namelen));
997 yurahash=GET_HASH_VALUE (yura_hash (de.de_name, de.de_namelen));
998 if ( ( (teahash == r5hash) && (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash) ) ||
999 ( (teahash == yurahash) && (yurahash == GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])))) ) ||
1000 ( (r5hash == yurahash) && (yurahash == GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])))) ) ) {
1001 reiserfs_warning("reiserfs: Unable to automatically detect hash"
1002 "function for device %s\n"
1003 "please mount with -o hash={tea,rupasov,r5}\n", kdevname (s->s_dev));
1007 if (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])) ) == yurahash)
1009 else if (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])) ) == teahash)
1011 else if (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])) ) == r5hash)
1014 reiserfs_warning("reiserfs: Unrecognised hash function for "
1015 "device %s\n", kdevname (s->s_dev));
1024 // finds out which hash names are sorted with
1025 static int what_hash (struct super_block * s)
1029 code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));
1031 /* reiserfs_hash_detect() == true if any of the hash mount options
1032 ** were used. We must check them to make sure the user isn't
1033 ** using a bad hash value
1035 if (code == UNSET_HASH || reiserfs_hash_detect(s))
1036 code = find_hash_out (s);
1038 if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
1039 /* detection has found the hash, and we must check against the
1042 if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
1043 printk("REISERFS: Error, %s hash detected, "
1044 "unable to force rupasov hash\n", reiserfs_hashname(code)) ;
1046 } else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
1047 printk("REISERFS: Error, %s hash detected, "
1048 "unable to force tea hash\n", reiserfs_hashname(code)) ;
1050 } else if (reiserfs_r5_hash(s) && code != R5_HASH) {
1051 printk("REISERFS: Error, %s hash detected, "
1052 "unable to force r5 hash\n", reiserfs_hashname(code)) ;
1056 /* find_hash_out was not called or could not determine the hash */
1057 if (reiserfs_rupasov_hash(s)) {
1059 } else if (reiserfs_tea_hash(s)) {
1061 } else if (reiserfs_r5_hash(s)) {
1066 /* if we are mounted RW, and we have a new valid hash code, update
1069 if (code != UNSET_HASH &&
1070 !(s->s_flags & MS_RDONLY) &&
1071 code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1072 set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1077 // return pointer to appropriate function
1078 static hashf_t hash_function (struct super_block * s)
1080 switch (what_hash (s)) {
1082 reiserfs_warning ("Using tea hash to sort names\n");
1085 reiserfs_warning ("Using rupasov hash to sort names\n");
1088 reiserfs_warning ("Using r5 hash to sort names\n");
1094 // this is used to set up correct value for old partitions
1095 int function2code (hashf_t func)
1097 if (func == keyed_hash)
1099 if (func == yura_hash)
1101 if (func == r5_hash)
1104 BUG() ; // should never happen
1109 static struct super_block * reiserfs_read_super (struct super_block * s, void * data, int silent)
1112 struct inode *root_inode;
1113 kdev_t dev = s->s_dev;
1115 extern int *blksize_size[];
1116 struct reiserfs_transaction_handle th ;
1118 unsigned long blocks;
1119 int jinit_done = 0 ;
1120 struct reiserfs_iget4_args args ;
1122 struct reiserfs_super_block * rs;
1125 memset (&s->u.reiserfs_sb, 0, sizeof (struct reiserfs_sb_info));
1127 /* Set default values for options: non-aggressive tails */
1128 s->u.reiserfs_sb.s_mount_opt = ( 1 << REISERFS_SMALLTAIL );
1129 /* default block allocator option: skip_busy */
1130 s->u.reiserfs_sb.s_alloc_options.bits = ( 1 << 5);
1131 /* If file grew past 4 blocks, start preallocation blocks for it. */
1132 s->u.reiserfs_sb.s_alloc_options.preallocmin = 4;
1133 /* Preallocate by 8 blocks (9-1) at once */
1134 s->u.reiserfs_sb.s_alloc_options.preallocsize = 9;
1136 if (reiserfs_parse_options (s, (char *) data, &(s->u.reiserfs_sb.s_mount_opt), &blocks) == 0) {
1141 printk("reserfs: resize option for remount only\n");
1145 if (blksize_size[MAJOR(dev)] && blksize_size[MAJOR(dev)][MINOR(dev)] != 0) {
1146 /* as blocksize is set for partition we use it */
1147 size = blksize_size[MAJOR(dev)][MINOR(dev)];
1150 set_blocksize (s->s_dev, BLOCK_SIZE);
1153 /* read block (64-th 1k block), which can contain reiserfs super block */
1154 if (read_super_block (s, size, REISERFS_DISK_OFFSET_IN_BYTES)) {
1155 // try old format (undistributed bitmap, super block in 8-th 1k block of a device)
1156 if (read_super_block (s, size, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
1162 rs = SB_DISK_SUPER_BLOCK (s);
1164 s->u.reiserfs_sb.s_mount_state = SB_REISERFS_STATE(s);
1165 s->u.reiserfs_sb.s_mount_state = REISERFS_VALID_FS ;
1167 if (old_format ? read_old_bitmaps(s) : read_bitmaps(s)) {
1168 printk ("reiserfs_read_super: unable to read bitmap\n");
1171 #ifdef CONFIG_REISERFS_CHECK
1172 printk("reiserfs:warning: CONFIG_REISERFS_CHECK is set ON\n");
1173 printk("reiserfs:warning: - it is slow mode for debugging.\n");
1176 // set_device_ro(s->s_dev, 1) ;
1177 if (journal_init(s)) {
1178 printk("reiserfs_read_super: unable to initialize journal space\n") ;
1181 jinit_done = 1 ; /* once this is set, journal_release must be called
1182 ** if we error out of the mount
1185 if (reread_meta_blocks(s)) {
1186 printk("reiserfs_read_super: unable to reread meta blocks after journal init\n") ;
1190 if (replay_only (s))
1193 if (is_read_only(s->s_dev) && !(s->s_flags & MS_RDONLY)) {
1194 printk("clm-7000: Detected readonly device, marking FS readonly\n") ;
1195 s->s_flags |= MS_RDONLY ;
1197 args.objectid = REISERFS_ROOT_PARENT_OBJECTID ;
1198 root_inode = iget4 (s, REISERFS_ROOT_OBJECTID, 0, (void *)(&args));
1200 printk ("reiserfs_read_super: get root inode failed\n");
1204 s->s_root = d_alloc_root(root_inode);
1210 // define and initialize hash function
1211 s->u.reiserfs_sb.s_hash_function = hash_function (s);
1212 if (s->u.reiserfs_sb.s_hash_function == NULL) {
1218 rs = SB_DISK_SUPER_BLOCK (s);
1219 old_magic = strncmp (rs->s_magic, REISER2FS_SUPER_MAGIC_STRING,
1220 strlen ( REISER2FS_SUPER_MAGIC_STRING));
1222 set_bit(REISERFS_3_6, &(s->u.reiserfs_sb.s_properties));
1224 set_bit(REISERFS_3_5, &(s->u.reiserfs_sb.s_properties));
1226 if (!(s->s_flags & MS_RDONLY)) {
1228 journal_begin(&th, s, 1) ;
1229 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
1231 set_sb_state( rs, REISERFS_ERROR_FS );
1234 // filesystem created under 3.5.x found
1235 if (convert_reiserfs (s)) {
1236 reiserfs_warning("reiserfs: converting 3.5.x filesystem to the new format\n") ;
1237 // after this 3.5.x will not be able to mount this partition
1238 memcpy (rs->s_magic, REISER2FS_SUPER_MAGIC_STRING,
1239 sizeof (REISER2FS_SUPER_MAGIC_STRING));
1241 reiserfs_convert_objectid_map_v1(s) ;
1242 set_bit(REISERFS_3_6, &(s->u.reiserfs_sb.s_properties));
1243 clear_bit(REISERFS_3_5, &(s->u.reiserfs_sb.s_properties));
1245 reiserfs_warning("reiserfs: using 3.5.x disk format\n") ;
1249 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
1250 journal_end(&th, s, 1) ;
1252 /* look for files which were to be removed in previous session */
1253 finish_unfinished (s);
1258 reiserfs_warning("reiserfs: using 3.5.x disk format\n") ;
1261 // mark hash in super block: it could be unset. overwrite should be ok
1262 set_sb_hash_function_code( rs, function2code(s->u.reiserfs_sb.s_hash_function ) );
1266 reiserfs_proc_info_init( s );
1267 reiserfs_proc_register( s, "version", reiserfs_version_in_proc );
1268 reiserfs_proc_register( s, "super", reiserfs_super_in_proc );
1269 reiserfs_proc_register( s, "per-level", reiserfs_per_level_in_proc );
1270 reiserfs_proc_register( s, "bitmap", reiserfs_bitmap_in_proc );
1271 reiserfs_proc_register( s, "on-disk-super", reiserfs_on_disk_super_in_proc );
1272 reiserfs_proc_register( s, "oidmap", reiserfs_oidmap_in_proc );
1273 reiserfs_proc_register( s, "journal", reiserfs_journal_in_proc );
1274 init_waitqueue_head (&(s->u.reiserfs_sb.s_wait));
1276 printk("%s\n", reiserfs_get_version_string()) ;
1280 if (jinit_done) { /* kill the commit thread, free journal ram */
1281 journal_release_error(NULL, s) ;
1283 if (SB_DISK_SUPER_BLOCK (s)) {
1284 for (j = 0; j < SB_BMAP_NR (s); j ++) {
1285 if (SB_AP_BITMAP (s))
1286 brelse (SB_AP_BITMAP (s)[j].bh);
1288 if (SB_AP_BITMAP (s))
1289 vfree (SB_AP_BITMAP (s));
1291 if (SB_BUFFER_WITH_SB (s))
1292 brelse(SB_BUFFER_WITH_SB (s));
1298 static int reiserfs_statfs (struct super_block * s, struct statfs * buf)
1300 struct reiserfs_super_block * rs = SB_DISK_SUPER_BLOCK (s);
1302 buf->f_namelen = (REISERFS_MAX_NAME (s->s_blocksize));
1305 buf->f_bfree = sb_free_blocks(rs);
1306 buf->f_bavail = buf->f_bfree;
1307 buf->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
1308 buf->f_bsize = s->s_blocksize;
1309 /* changed to accomodate gcc folks.*/
1310 buf->f_type = REISERFS_SUPER_MAGIC;
1314 static DECLARE_FSTYPE_DEV(reiserfs_fs_type,"reiserfs",reiserfs_read_super);
1316 static int __init init_reiserfs_fs (void)
1318 reiserfs_proc_info_global_init();
1319 reiserfs_proc_register_global( "version",
1320 reiserfs_global_version_in_proc );
1321 return register_filesystem(&reiserfs_fs_type);
1324 MODULE_DESCRIPTION("ReiserFS journaled filesystem");
1325 MODULE_AUTHOR("Hans Reiser <reiser@namesys.com>");
1326 MODULE_LICENSE("GPL");
1329 static void __exit exit_reiserfs_fs(void)
1331 reiserfs_proc_unregister_global( "version" );
1332 reiserfs_proc_info_global_done();
1333 unregister_filesystem(&reiserfs_fs_type);
1337 module_init(init_reiserfs_fs) ;
1338 module_exit(exit_reiserfs_fs) ;