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/blkdev.h>
9 #include <linux/vmalloc.h>
10 #include <asm/uaccess.h>
11 #include <linux/reiserfs_fs.h>
12 #include <linux/smp_lock.h>
13 #include <linux/locks.h>
14 #include <linux/init.h>
16 #define REISERFS_OLD_BLOCKSIZE 4096
17 #define REISERFS_SUPER_MAGIC_STRING_OFFSET_NJ 20
19 const char reiserfs_3_5_magic_string[] = REISERFS_SUPER_MAGIC_STRING;
20 const char reiserfs_3_6_magic_string[] = REISER2FS_SUPER_MAGIC_STRING;
21 const char reiserfs_jr_magic_string[] = REISER2FS_JR_SUPER_MAGIC_STRING;
23 int is_reiserfs_3_5 (struct reiserfs_super_block * rs)
25 return !strncmp (rs->s_v1.s_magic, reiserfs_3_5_magic_string,
26 strlen (reiserfs_3_5_magic_string));
30 int is_reiserfs_3_6 (struct reiserfs_super_block * rs)
32 return !strncmp (rs->s_v1.s_magic, reiserfs_3_6_magic_string,
33 strlen (reiserfs_3_6_magic_string));
37 int is_reiserfs_jr (struct reiserfs_super_block * rs)
39 return !strncmp (rs->s_v1.s_magic, reiserfs_jr_magic_string,
40 strlen (reiserfs_jr_magic_string));
44 static int is_any_reiserfs_magic_string (struct reiserfs_super_block * rs)
46 return (is_reiserfs_3_5 (rs) || is_reiserfs_3_6 (rs) ||
50 static int reiserfs_remount (struct super_block * s, int * flags, char * data);
51 static int reiserfs_statfs (struct super_block * s, struct statfs * buf);
53 static void reiserfs_write_super (struct super_block * s)
58 if (!(s->s_flags & MS_RDONLY)) {
59 dirty = flush_old_commits(s, 1) ;
65 static void reiserfs_write_super_lockfs (struct super_block * s)
69 struct reiserfs_transaction_handle th ;
71 if (!(s->s_flags & MS_RDONLY)) {
72 journal_begin(&th, s, 1) ;
73 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
74 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
75 reiserfs_block_writes(&th) ;
76 journal_end(&th, s, 1) ;
82 void reiserfs_unlockfs(struct super_block *s) {
83 reiserfs_allow_writes(s) ;
86 extern const struct key MAX_KEY;
89 /* this is used to delete "save link" when there are no items of a
90 file it points to. It can either happen if unlink is completed but
91 "save unlink" removal, or if file has both unlink and truncate
92 pending and as unlink completes first (because key of "save link"
93 protecting unlink is bigger that a key lf "save link" which
94 protects truncate), so there left no items to make truncate
96 static void remove_save_link_only (struct super_block * s, struct key * key, int oid_free)
98 struct reiserfs_transaction_handle th;
100 /* we are going to do one balancing */
101 journal_begin (&th, s, JOURNAL_PER_BALANCE_CNT);
103 reiserfs_delete_solid_item (&th, key);
105 /* removals are protected by direct items */
106 reiserfs_release_objectid (&th, le32_to_cpu (key->k_objectid));
108 journal_end (&th, s, JOURNAL_PER_BALANCE_CNT);
112 /* look for uncompleted unlinks and truncates and complete them */
113 static void finish_unfinished (struct super_block * s)
115 INITIALIZE_PATH (path);
116 struct cpu_key max_cpu_key, obj_key;
117 struct key save_link_key;
119 struct item_head * ih;
120 struct buffer_head * bh;
124 struct inode * inode;
128 /* compose key to look for "save" links */
129 max_cpu_key.version = KEY_FORMAT_3_5;
130 max_cpu_key.on_disk_key = MAX_KEY;
131 max_cpu_key.key_length = 3;
134 s -> u.reiserfs_sb.s_is_unlinked_ok = 1;
136 retval = search_item (s, &max_cpu_key, &path);
137 if (retval != ITEM_NOT_FOUND) {
138 reiserfs_warning (s, "vs-2140: finish_unfinished: search_by_key returned %d\n",
143 bh = get_last_bh (&path);
144 item_pos = get_item_pos (&path);
145 if (item_pos != B_NR_ITEMS (bh)) {
146 reiserfs_warning (s, "vs-2060: finish_unfinished: wrong position found\n");
150 ih = B_N_PITEM_HEAD (bh, item_pos);
152 if (le32_to_cpu (ih->ih_key.k_dir_id) != MAX_KEY_OBJECTID)
153 /* there are no "save" links anymore */
156 save_link_key = ih->ih_key;
157 if (is_indirect_le_ih (ih))
162 /* reiserfs_iget needs k_dirid and k_objectid only */
163 item = B_I_PITEM (bh, ih);
164 obj_key.on_disk_key.k_dir_id = le32_to_cpu (*(__u32 *)item);
165 obj_key.on_disk_key.k_objectid = le32_to_cpu (ih->ih_key.k_objectid);
166 obj_key.on_disk_key.u.k_offset_v1.k_offset = 0;
167 obj_key.on_disk_key.u.k_offset_v1.k_uniqueness = 0;
171 inode = reiserfs_iget (s, &obj_key);
173 /* the unlink almost completed, it just did not manage to remove
174 "save" link and release objectid */
175 reiserfs_warning (s, "vs-2180: finish_unfinished: iget failed for %K\n",
177 remove_save_link_only (s, &save_link_key, 1);
181 if (!truncate && inode->i_nlink) {
182 /* file is not unlinked */
183 reiserfs_warning (s, "vs-2185: finish_unfinished: file %K is not unlinked\n",
185 remove_save_link_only (s, &save_link_key, 0);
189 if (truncate && S_ISDIR (inode->i_mode) ) {
190 /* We got a truncate request for a dir which is impossible.
191 The only imaginable way is to execute unfinished truncate request
192 then boot into old kernel, remove the file and create dir with
194 reiserfs_warning(s, "green-2101: impossible truncate on a directory %k. Please report\n", INODE_PKEY (inode));
195 remove_save_link_only (s, &save_link_key, 0);
202 inode -> u.reiserfs_i.i_flags |= i_link_saved_truncate_mask;
203 /* not completed truncate found. New size was committed together
205 reiserfs_warning (s, "Truncating %k to %Ld ..",
206 INODE_PKEY (inode), inode->i_size);
207 reiserfs_truncate_file (inode, 0/*don't update modification time*/);
208 remove_save_link (inode, truncate);
210 inode -> u.reiserfs_i.i_flags |= i_link_saved_unlink_mask;
211 /* not completed unlink (rmdir) found */
212 reiserfs_warning (s, "Removing %k..", INODE_PKEY (inode));
213 /* removal gets completed in iput */
220 s -> u.reiserfs_sb.s_is_unlinked_ok = 0;
224 reiserfs_warning (s, "There were %d uncompleted unlinks/truncates. "
225 "Completed\n", done);
228 /* to protect file being unlinked from getting lost we "safe" link files
229 being unlinked. This link will be deleted in the same transaction with last
230 item of file. mounting the filesytem we scan all these links and remove
231 files which almost got lost */
232 void add_save_link (struct reiserfs_transaction_handle * th,
233 struct inode * inode, int truncate)
235 INITIALIZE_PATH (path);
241 /* file can only get one "save link" of each kind */
243 ( inode -> u.reiserfs_i.i_flags & i_link_saved_truncate_mask ),
244 "saved link already exists for truncated inode %lx",
245 ( long ) inode -> i_ino );
247 ( inode -> u.reiserfs_i.i_flags & i_link_saved_unlink_mask ),
248 "saved link already exists for unlinked inode %lx",
249 ( long ) inode -> i_ino );
251 /* setup key of "save" link */
252 key.version = KEY_FORMAT_3_5;
253 key.on_disk_key.k_dir_id = MAX_KEY_OBJECTID;
254 key.on_disk_key.k_objectid = inode->i_ino;
256 /* unlink, rmdir, rename */
257 set_cpu_key_k_offset (&key, 1 + inode->i_sb->s_blocksize);
258 set_cpu_key_k_type (&key, TYPE_DIRECT);
260 /* item head of "safe" link */
261 make_le_item_head (&ih, &key, key.version, 1 + inode->i_sb->s_blocksize, TYPE_DIRECT,
262 4/*length*/, 0xffff/*free space*/);
265 if (S_ISDIR (inode->i_mode))
266 reiserfs_warning(inode->i_sb, "green-2102: Adding a truncate savelink for a directory %k! Please report\n", INODE_PKEY(inode));
267 set_cpu_key_k_offset (&key, 1);
268 set_cpu_key_k_type (&key, TYPE_INDIRECT);
270 /* item head of "safe" link */
271 make_le_item_head (&ih, &key, key.version, 1, TYPE_INDIRECT,
272 4/*length*/, 0/*free space*/);
276 /* look for its place in the tree */
277 retval = search_item (inode->i_sb, &key, &path);
278 if (retval != ITEM_NOT_FOUND) {
279 if ( retval != -ENOSPC )
280 reiserfs_warning (inode->i_sb, "vs-2100: add_save_link:"
281 "search_by_key (%K) returned %d\n", &key, retval);
286 /* body of "save" link */
287 link = INODE_PKEY (inode)->k_dir_id;
289 /* put "save" link inot tree */
290 retval = reiserfs_insert_item (th, &path, &key, &ih, (char *)&link);
292 if (retval != -ENOSPC)
293 reiserfs_warning (inode->i_sb, "vs-2120: add_save_link: insert_item returned %d\n",
297 inode -> u.reiserfs_i.i_flags |= i_link_saved_truncate_mask;
299 inode -> u.reiserfs_i.i_flags |= i_link_saved_unlink_mask;
304 /* this opens transaction unlike add_save_link */
305 void remove_save_link (struct inode * inode, int truncate)
307 struct reiserfs_transaction_handle th;
311 /* we are going to do one balancing only */
312 journal_begin (&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
314 /* setup key of "save" link */
315 key.k_dir_id = cpu_to_le32 (MAX_KEY_OBJECTID);
316 key.k_objectid = INODE_PKEY (inode)->k_objectid;
318 /* unlink, rmdir, rename */
319 set_le_key_k_offset (KEY_FORMAT_3_5, &key,
320 1 + inode->i_sb->s_blocksize);
321 set_le_key_k_type (KEY_FORMAT_3_5, &key, TYPE_DIRECT);
324 set_le_key_k_offset (KEY_FORMAT_3_5, &key, 1);
325 set_le_key_k_type (KEY_FORMAT_3_5, &key, TYPE_INDIRECT);
329 ( inode -> u.reiserfs_i.i_flags & i_link_saved_truncate_mask ) ) ||
331 ( inode -> u.reiserfs_i.i_flags & i_link_saved_unlink_mask ) ) )
332 reiserfs_delete_solid_item (&th, &key);
334 reiserfs_release_objectid (&th, inode->i_ino);
335 inode -> u.reiserfs_i.i_flags &= ~i_link_saved_unlink_mask;
337 inode -> u.reiserfs_i.i_flags &= ~i_link_saved_truncate_mask;
339 journal_end (&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
343 static void reiserfs_put_super (struct super_block * s)
346 struct reiserfs_transaction_handle th ;
348 /* change file system state to current state if it was mounted with read-write permissions */
349 if (!(s->s_flags & MS_RDONLY)) {
350 journal_begin(&th, s, 10) ;
351 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
352 set_sb_umount_state( SB_DISK_SUPER_BLOCK(s), s->u.reiserfs_sb.s_mount_state );
353 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
356 /* note, journal_release checks for readonly mount, and can decide not
357 ** to do a journal_end
359 journal_release(&th, s) ;
361 for (i = 0; i < SB_BMAP_NR (s); i ++)
362 brelse (SB_AP_BITMAP (s)[i].bh);
364 vfree (SB_AP_BITMAP (s));
366 brelse (SB_BUFFER_WITH_SB (s));
368 print_statistics (s);
370 if (s->u.reiserfs_sb.s_kmallocs != 0) {
371 reiserfs_warning (s, "vs-2004: reiserfs_put_super: allocated memory left %d\n",
372 s->u.reiserfs_sb.s_kmallocs);
375 if (s->u.reiserfs_sb.reserved_blocks != 0) {
376 reiserfs_warning (s, "green-2005: reiserfs_put_super: reserved blocks left %d\n",
377 s->u.reiserfs_sb.reserved_blocks);
380 reiserfs_proc_unregister( s, "journal" );
381 reiserfs_proc_unregister( s, "oidmap" );
382 reiserfs_proc_unregister( s, "on-disk-super" );
383 reiserfs_proc_unregister( s, "bitmap" );
384 reiserfs_proc_unregister( s, "per-level" );
385 reiserfs_proc_unregister( s, "super" );
386 reiserfs_proc_unregister( s, "version" );
387 reiserfs_proc_info_done( s );
391 /* we don't mark inodes dirty, we just log them */
392 static void reiserfs_dirty_inode (struct inode * inode) {
393 struct reiserfs_transaction_handle th ;
395 if (inode->i_sb->s_flags & MS_RDONLY) {
396 reiserfs_warning(inode->i_sb, "clm-6006: writing inode %lu on readonly FS\n",
402 /* this is really only used for atime updates, so they don't have
403 ** to be included in O_SYNC or fsync
405 journal_begin(&th, inode->i_sb, 1) ;
406 reiserfs_update_sd (&th, inode);
407 journal_end(&th, inode->i_sb, 1) ;
411 struct super_operations reiserfs_sops =
413 read_inode: reiserfs_read_inode,
414 read_inode2: reiserfs_read_inode2,
415 write_inode: reiserfs_write_inode,
416 dirty_inode: reiserfs_dirty_inode,
417 delete_inode: reiserfs_delete_inode,
418 put_super: reiserfs_put_super,
419 write_super: reiserfs_write_super,
420 write_super_lockfs: reiserfs_write_super_lockfs,
421 unlockfs: reiserfs_unlockfs,
422 statfs: reiserfs_statfs,
423 remount_fs: reiserfs_remount,
425 fh_to_dentry: reiserfs_fh_to_dentry,
426 dentry_to_fh: reiserfs_dentry_to_fh,
430 /* this struct is used in reiserfs_getopt () for containing the value for those
431 mount options that have values rather than being toggles. */
434 int setmask; /* bitmask which is to set on mount_options bitmask when this
435 value is found, 0 is no bits are to be changed. */
436 int clrmask; /* bitmask which is to clear on mount_options bitmask when this
437 value is found, 0 is no bits are to be changed. This is
438 applied BEFORE setmask */
442 /* this struct is used in reiserfs_getopt() for describing the set of reiserfs
446 int arg_required; /* 0 is argument is not required, not 0 otherwise */
447 const arg_desc_t * values; /* list of values accepted by an option */
448 int setmask; /* bitmask which is to set on mount_options bitmask when this
449 value is found, 0 is no bits are to be changed. */
450 int clrmask; /* bitmask which is to clear on mount_options bitmask when this
451 value is found, 0 is no bits are to be changed. This is
452 applied BEFORE setmask */
456 /* possible values for "-o hash=" and bits which are to be set in s_mount_opt
457 of reiserfs specific part of in-core super block */
458 static const arg_desc_t hash[] = {
459 {"rupasov", 1<<FORCE_RUPASOV_HASH,(1<<FORCE_TEA_HASH)|(1<<FORCE_R5_HASH)},
460 {"tea", 1<<FORCE_TEA_HASH,(1<<FORCE_RUPASOV_HASH)|(1<<FORCE_R5_HASH)},
461 {"r5", 1<<FORCE_R5_HASH,(1<<FORCE_RUPASOV_HASH)|(1<<FORCE_TEA_HASH)},
462 {"detect", 1<<FORCE_HASH_DETECT, (1<<FORCE_RUPASOV_HASH)|(1<<FORCE_TEA_HASH)|(1<<FORCE_R5_HASH)},
467 /* possible values for "-o block-allocator=" and bits which are to be set in
468 s_mount_opt of reiserfs specific part of in-core super block */
469 static const arg_desc_t balloc[] = {
470 {"noborder", 1<<REISERFS_NO_BORDER, 0},
471 {"border", 0, 1<<REISERFS_NO_BORDER},
472 {"no_unhashed_relocation", 1<<REISERFS_NO_UNHASHED_RELOCATION, 0},
473 {"hashed_relocation", 1<<REISERFS_HASHED_RELOCATION, 0},
474 {"test4", 1<<REISERFS_TEST4, 0},
475 {"notest4", 0, 1<<REISERFS_TEST4},
479 static const arg_desc_t tails[] = {
480 {"on", 1<<REISERFS_LARGETAIL, 1<<REISERFS_SMALLTAIL},
481 {"off", 0, (1<<REISERFS_LARGETAIL)|(1<<REISERFS_SMALLTAIL)},
482 {"small", 1<<REISERFS_SMALLTAIL, 1<<REISERFS_LARGETAIL},
487 /* proceed only one option from a list *cur - string containing of mount options
488 opts - array of options which are accepted
489 opt_arg - if option is found and requires an argument and if it is specifed
490 in the input - pointer to the argument is stored here
491 bit_flags - if option requires to set a certain bit - it is set here
492 return -1 if unknown option is found, opt->arg_required otherwise */
493 static int reiserfs_getopt ( struct super_block * s, char ** cur, opt_desc_t * opts, char ** opt_arg,
494 unsigned long * bit_flags)
503 const opt_desc_t * opt;
504 const arg_desc_t * arg;
509 /* assume argument cannot contain commas */
510 *cur = strchr (p, ',');
516 if ( !strncmp (p, "alloc=", 6) ) {
517 /* Ugly special case, probably we should redo options parser so that
518 it can understand several arguments for some options, also so that
519 it can fill several bitfields with option values. */
520 if ( reiserfs_parse_alloc_options( s, p + 6) ) {
527 /* for every option in the list */
528 for (opt = opts; opt->option_name; opt ++) {
529 if (!strncmp (p, opt->option_name, strlen (opt->option_name))) {
531 *bit_flags &= ~opt->clrmask;
532 *bit_flags |= opt->setmask;
537 if (!opt->option_name) {
538 printk ("reiserfs_getopt: unknown option \"%s\"\n", p);
542 p += strlen (opt->option_name);
545 if (!opt->arg_required) {
546 printk ("reiserfs_getopt: the option \"%s\" does not require an argument\n",
553 if (opt->arg_required) {
554 printk ("reiserfs_getopt: the option \"%s\" requires an argument\n", opt->option_name);
559 printk ("reiserfs_getopt: head of option \"%s\" is only correct\n", opt->option_name);
563 /* move to the argument, or to next option if argument is not required */
566 if ( opt->arg_required && !strlen (p) ) {
567 /* this catches "option=," */
568 printk ("reiserfs_getopt: empty argument for \"%s\"\n", opt->option_name);
573 /* *=NULLopt_arg contains pointer to argument */
575 return opt->arg_required;
578 /* values possible for this option are listed in opt->values */
579 for (arg = opt->values; arg->value; arg ++) {
580 if (!strcmp (p, arg->value)) {
582 *bit_flags &= ~arg->clrmask;
583 *bit_flags |= arg->setmask;
585 return opt->arg_required;
589 printk ("reiserfs_getopt: bad value \"%s\" for option \"%s\"\n", p, opt->option_name);
593 /* returns 0 if something is wrong in option string, 1 - otherwise */
594 static int reiserfs_parse_options (struct super_block * s, char * options, /* string given via mount's -o */
595 unsigned long * mount_options,
596 /* after the parsing phase, contains the
597 collection of bitflags defining what
598 mount options were selected. */
599 unsigned long * blocks) /* strtol-ed from NNN of resize=NNN */
604 opt_desc_t opts[] = {
605 {"tails", 't', tails, 0, 0},
606 /* Compatibility stuff, so that -o notail
607 for old setups still work */
608 {"notail", 0, 0, 0, (1<<REISERFS_LARGETAIL)|(1<<REISERFS_SMALLTAIL)},
609 {"conv", 0, 0, 1<<REISERFS_CONVERT, 0},
610 {"nolog", 0, 0, 0, 0}, /* This is unsupported */
611 {"replayonly", 0, 0, 1<<REPLAYONLY, 0},
613 {"block-allocator", 'a', balloc, 0, 0},
614 {"hash", 'h', hash, 1<<FORCE_HASH_DETECT, 0},
616 {"resize", 'r', 0, 0, 0},
617 {"attrs", 0, 0, 1<<REISERFS_ATTRS, 0},
618 {"noattrs", 0, 0, 0, 1<<REISERFS_ATTRS},
623 if (!options || !*options)
624 /* use default configuration: create tails, journaling on, no
625 conversion to newest format */
628 for (pos = options; pos; ) {
629 c = reiserfs_getopt (s, &pos, opts, &arg, mount_options);
631 /* wrong option is given */
639 *blocks = simple_strtoul (arg, &p, 0);
641 /* NNN does not look like a number */
642 printk ("reiserfs_parse_options: bad value %s\n", arg);
652 int reiserfs_is_super(struct super_block *s) {
653 return (s->s_dev != 0 && s->s_op == &reiserfs_sops) ;
657 static void handle_attrs( struct super_block *s )
659 struct reiserfs_super_block * rs;
661 if( reiserfs_attrs( s ) ) {
662 rs = SB_DISK_SUPER_BLOCK (s);
663 if( old_format_only(s) ) {
664 reiserfs_warning(s, "reiserfs: cannot support attributes on 3.5.x disk format\n" );
665 s -> u.reiserfs_sb.s_mount_opt &= ~ ( 1 << REISERFS_ATTRS );
668 if( !( le32_to_cpu( rs -> s_flags ) & reiserfs_attrs_cleared ) ) {
669 reiserfs_warning(s, "reiserfs: cannot support attributes until flag is set in super-block\n" );
670 s -> u.reiserfs_sb.s_mount_opt &= ~ ( 1 << REISERFS_ATTRS );
675 static int reiserfs_remount (struct super_block * s, int * mount_flags, char * data)
677 struct reiserfs_super_block * rs;
678 struct reiserfs_transaction_handle th ;
679 unsigned long blocks;
680 unsigned long mount_options = s->u.reiserfs_sb.s_mount_opt;
681 unsigned long safe_mask = 0;
683 rs = SB_DISK_SUPER_BLOCK (s);
684 if (!reiserfs_parse_options(s, data, &mount_options, &blocks))
687 /* Add options that are safe here */
688 safe_mask |= 1 << REISERFS_SMALLTAIL;
689 safe_mask |= 1 << REISERFS_LARGETAIL;
690 safe_mask |= 1 << REISERFS_NO_BORDER;
691 safe_mask |= 1 << REISERFS_NO_UNHASHED_RELOCATION;
692 safe_mask |= 1 << REISERFS_HASHED_RELOCATION;
693 safe_mask |= 1 << REISERFS_TEST4;
694 safe_mask |= 1 << REISERFS_ATTRS;
696 /* Update the bitmask, taking care to keep
697 * the bits we're not allowed to change here */
698 s->u.reiserfs_sb.s_mount_opt = (s->u.reiserfs_sb.s_mount_opt & ~safe_mask) | (mount_options & safe_mask);
703 int rc = reiserfs_resize(s, blocks);
708 if (*mount_flags & MS_RDONLY) {
709 /* remount read-only */
710 if (s->s_flags & MS_RDONLY)
711 /* it is read-only already */
713 /* try to remount file system with read-only permissions */
714 if (sb_umount_state(rs) == REISERFS_VALID_FS || s->u.reiserfs_sb.s_mount_state != REISERFS_VALID_FS) {
718 journal_begin(&th, s, 10) ;
719 /* Mounting a rw partition read-only. */
720 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
721 set_sb_umount_state( rs, s->u.reiserfs_sb.s_mount_state );
722 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
725 /* remount read-write */
726 if (!(s->s_flags & MS_RDONLY))
727 return 0; /* We are read-write already */
729 s->s_flags &= ~MS_RDONLY ; /* now it is safe to call journal_begin */
730 journal_begin(&th, s, 10) ;
732 /* Mount a partition which is read-only, read-write */
733 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
734 s->u.reiserfs_sb.s_mount_state = sb_umount_state(rs);
735 s->s_flags &= ~MS_RDONLY;
736 set_sb_umount_state( rs, REISERFS_ERROR_FS );
737 /* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
738 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
740 s->u.reiserfs_sb.s_mount_state = REISERFS_VALID_FS ;
742 /* this will force a full flush of all journal lists */
743 SB_JOURNAL(s)->j_must_wait = 1 ;
744 journal_end(&th, s, 10) ;
746 if (!( *mount_flags & MS_RDONLY ) )
747 finish_unfinished( s );
752 /* load_bitmap_info_data - Sets up the reiserfs_bitmap_info structure from disk.
753 * @sb - superblock for this filesystem
754 * @bi - the bitmap info to be loaded. Requires that bi->bh is valid.
756 * This routine counts how many free bits there are, finding the first zero
757 * as a side effect. Could also be implemented as a loop of test_bit() calls, or
758 * a loop of find_first_zero_bit() calls. This implementation is similar to
759 * find_first_zero_bit(), but doesn't return after it finds the first bit.
760 * Should only be called on fs mount, but should be fairly efficient anyways.
762 * bi->first_zero_hint is considered unset if it == 0, since the bitmap itself
763 * will * invariably occupt block 0 represented in the bitmap. The only
764 * exception to this is when free_count also == 0, since there will be no
765 * free blocks at all.
767 static void load_bitmap_info_data (struct super_block *sb,
768 struct reiserfs_bitmap_info *bi)
770 unsigned long *cur = (unsigned long *)bi->bh->b_data;
772 while ((char *)cur < (bi->bh->b_data + sb->s_blocksize)) {
774 /* No need to scan if all 0's or all 1's.
775 * Since we're only counting 0's, we can simply ignore all 1's */
777 if (bi->first_zero_hint == 0) {
778 bi->first_zero_hint = ((char *)cur - bi->bh->b_data) << 3;
780 bi->free_count += sizeof ( unsigned long ) * 8;
781 } else if (*cur != ~0L) {
783 for (b = 0; b < sizeof ( unsigned long ) * 8; b++) {
784 if (!reiserfs_test_le_bit (b, cur)) {
786 if (bi->first_zero_hint == 0)
787 bi->first_zero_hint =
788 (((char *)cur - bi->bh->b_data) << 3) + b;
795 #ifdef CONFIG_REISERFS_CHECK
796 // This outputs a lot of unneded info on big FSes
797 // reiserfs_warning ("bitmap loaded from block %d: %d free blocks\n",
798 // bi->bh->b_blocknr, bi->free_count);
802 static int read_bitmaps (struct super_block * s)
806 SB_AP_BITMAP (s) = vmalloc (sizeof (struct reiserfs_bitmap_info) * SB_BMAP_NR(s));
807 if (SB_AP_BITMAP (s) == 0)
809 memset (SB_AP_BITMAP (s), 0, sizeof (struct reiserfs_bitmap_info) * SB_BMAP_NR(s));
811 for (i = 0, bmp = REISERFS_DISK_OFFSET_IN_BYTES / s->s_blocksize + 1;
812 i < SB_BMAP_NR(s); i++, bmp = s->s_blocksize * 8 * i) {
813 SB_AP_BITMAP (s)[i].bh = sb_getblk (s, bmp);
814 if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh))
815 ll_rw_block(READ, 1, &SB_AP_BITMAP(s)[i].bh);
817 for (i = 0; i < SB_BMAP_NR(s); i++) {
818 wait_on_buffer(SB_AP_BITMAP (s)[i].bh);
819 if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) {
820 reiserfs_warning(s, "sh-2029: reiserfs read_bitmaps: "
821 "bitmap block (#%lu) reading failed\n",
822 SB_AP_BITMAP(s)[i].bh->b_blocknr);
823 for (i = 0; i < SB_BMAP_NR(s); i++)
824 brelse(SB_AP_BITMAP(s)[i].bh);
825 vfree(SB_AP_BITMAP(s));
826 SB_AP_BITMAP(s) = NULL;
829 load_bitmap_info_data (s, SB_AP_BITMAP (s) + i);
834 static int read_old_bitmaps (struct super_block * s)
837 struct reiserfs_super_block * rs = SB_DISK_SUPER_BLOCK(s);
838 int bmp1 = (REISERFS_OLD_DISK_OFFSET_IN_BYTES / s->s_blocksize) + 1; /* first of bitmap blocks */
840 /* read true bitmap */
841 SB_AP_BITMAP (s) = vmalloc (sizeof (struct reiserfs_buffer_info *) * sb_bmap_nr(rs));
842 if (SB_AP_BITMAP (s) == 0)
845 memset (SB_AP_BITMAP (s), 0, sizeof (struct reiserfs_buffer_info *) * sb_bmap_nr(rs));
847 for (i = 0; i < sb_bmap_nr(rs); i ++) {
848 SB_AP_BITMAP (s)[i].bh = reiserfs_bread (s, bmp1 + i, s->s_blocksize);
849 if (!SB_AP_BITMAP (s)[i].bh)
851 load_bitmap_info_data (s, SB_AP_BITMAP (s) + i);
857 void check_bitmap (struct super_block * s)
863 while (i < SB_BLOCK_COUNT (s)) {
864 buf = SB_AP_BITMAP (s)[i / (s->s_blocksize * 8)].bh->b_data;
865 if (!reiserfs_test_le_bit (i % (s->s_blocksize * 8), buf))
870 if (free != SB_FREE_BLOCKS (s))
871 reiserfs_warning (s, "vs-4000: check_bitmap: %d free blocks, must be %d\n",
872 free, SB_FREE_BLOCKS (s));
875 static int read_super_block (struct super_block * s, int size, int offset)
877 struct buffer_head * bh;
878 struct reiserfs_super_block * rs;
881 bh = bread (s->s_dev, offset / size, size);
883 printk ("sh-2006: reiserfs read_super_block: "
884 "bread failed (dev %s, block %u, size %u)\n",
885 kdevname (s->s_dev), offset / size, size);
889 rs = (struct reiserfs_super_block *)bh->b_data;
890 if (!is_any_reiserfs_magic_string (rs)) {
896 // ok, reiserfs signature (old or new) found in at the given offset
898 s->s_blocksize = sb_blocksize(rs);
899 s->s_blocksize_bits = 0;
900 while ((1 << s->s_blocksize_bits) != s->s_blocksize)
901 s->s_blocksize_bits ++;
905 if (s->s_blocksize != 4096) {
906 printk("Unsupported reiserfs blocksize: %ld on %s, only 4096 bytes "
907 "blocksize is supported.\n", s->s_blocksize, kdevname (s->s_dev));
911 if (s->s_blocksize != size)
912 set_blocksize (s->s_dev, s->s_blocksize);
914 bh = reiserfs_bread (s, offset / s->s_blocksize, s->s_blocksize);
916 printk("sh-2007: reiserfs read_super_block: "
917 "bread failed (dev %s, block %u, size %u)\n",
918 kdevname (s->s_dev), offset / size, size);
922 rs = (struct reiserfs_super_block *)bh->b_data;
923 if (!is_any_reiserfs_magic_string (rs) || sb_blocksize(rs) !=
925 printk ("sh-2011: read_super_block: "
926 "can't find a reiserfs filesystem on (dev %s, block %lu, size %lu)\n",
927 kdevname(s->s_dev), bh->b_blocknr, s->s_blocksize);
932 if (sb_root_block(rs) == -1) {
934 printk("dev %s: Unfinished reiserfsck --rebuild-tree run detected. Please run\n"
935 "reiserfsck --rebuild-tree and wait for a completion. If that fails\n"
936 "get newer reiserfsprogs package\n", kdevname (s->s_dev));
940 SB_BUFFER_WITH_SB (s) = bh;
941 SB_DISK_SUPER_BLOCK (s) = rs;
942 if (is_reiserfs_jr (rs)) {
943 /* magic is of non-standard journal filesystem, look at s_version to
944 find which format is in use */
945 if (sb_version(rs) == REISERFS_VERSION_2)
946 printk ("reiserfs: found format \"3.6\" with non-standard journal\n");
947 else if (sb_version(rs) == REISERFS_VERSION_1)
948 printk ("reiserfs: found format \"3.5\" with non-standard journal\n");
950 printk ("sh-2012: read_super_block: found unknown format \"%u\" "
951 "of reiserfs with non-standard magic\n", sb_version(rs));
956 /* s_version may contain incorrect information. Look at the magic
958 printk ("reiserfs: found format \"%s\" with standard journal\n",
959 is_reiserfs_3_5 (rs) ? "3.5" : "3.6");
960 s->s_op = &reiserfs_sops;
962 /* new format is limited by the 32 bit wide i_blocks field, want to
963 ** be one full block below that.
965 s->s_maxbytes = (512LL << 32) - s->s_blocksize ;
971 /* after journal replay, reread all bitmap and super blocks */
972 static int reread_meta_blocks(struct super_block *s) {
974 ll_rw_block(READ, 1, &(SB_BUFFER_WITH_SB(s))) ;
975 wait_on_buffer(SB_BUFFER_WITH_SB(s)) ;
976 if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
977 reiserfs_warning(s, "sh-2016: reiserfs reread_meta_blocks, "
978 "error reading the super\n") ;
982 for (i = 0; i < SB_BMAP_NR(s) ; i++) {
983 ll_rw_block(READ, 1, &(SB_AP_BITMAP(s)[i].bh)) ;
984 wait_on_buffer(SB_AP_BITMAP(s)[i].bh) ;
985 if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) {
986 reiserfs_warning(s, "reread_meta_blocks, error reading bitmap block number %d at %ld\n", i, SB_AP_BITMAP(s)[i].bh->b_blocknr) ;
995 /////////////////////////////////////////////////////
996 // hash detection stuff
999 // if root directory is empty - we set default - Yura's - hash and
1001 // FIXME: we look for only one name in a directory. If tea and yura
1002 // bith have the same value - we ask user to send report to the
1004 __u32 find_hash_out (struct super_block * s)
1007 struct inode * inode;
1009 INITIALIZE_PATH (path);
1010 struct reiserfs_dir_entry de;
1011 __u32 hash = DEFAULT_HASH;
1013 inode = s->s_root->d_inode;
1015 do { // Some serious "goto"-hater was there ;)
1016 u32 teahash, r5hash, yurahash;
1018 make_cpu_key (&key, inode, ~0, TYPE_DIRENTRY, 3);
1019 retval = search_by_entry_key (s, &key, &path, &de);
1020 if (retval == IO_ERROR) {
1024 if (retval == NAME_NOT_FOUND)
1026 set_de_name_and_namelen (&de);
1027 if (deh_offset( &(de.de_deh[de.de_entry_num]) ) == DOT_DOT_OFFSET) {
1028 /* allow override in this case */
1029 if (reiserfs_rupasov_hash(s)) {
1032 reiserfs_warning(s, "reiserfs: FS seems to be empty, autodetect "
1033 "is using the default hash\n");
1036 r5hash=GET_HASH_VALUE (r5_hash (de.de_name, de.de_namelen));
1037 teahash=GET_HASH_VALUE (keyed_hash (de.de_name, de.de_namelen));
1038 yurahash=GET_HASH_VALUE (yura_hash (de.de_name, de.de_namelen));
1039 if ( ( (teahash == r5hash) && (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash) ) ||
1040 ( (teahash == yurahash) && (yurahash == GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])))) ) ||
1041 ( (r5hash == yurahash) && (yurahash == GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])))) ) ) {
1042 reiserfs_warning(s, "reiserfs: Unable to automatically detect hash"
1043 "function please mount with -o hash={tea,rupasov,r5}\n");
1047 if (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])) ) == yurahash)
1049 else if (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])) ) == teahash)
1051 else if (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])) ) == r5hash)
1054 reiserfs_warning(s, "reiserfs: Unrecognised hash function\n");
1063 // finds out which hash names are sorted with
1064 static int what_hash (struct super_block * s)
1068 code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));
1070 /* reiserfs_hash_detect() == true if any of the hash mount options
1071 ** were used. We must check them to make sure the user isn't
1072 ** using a bad hash value
1074 if (code == UNSET_HASH || reiserfs_hash_detect(s))
1075 code = find_hash_out (s);
1077 if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
1078 /* detection has found the hash, and we must check against the
1081 if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
1082 printk("REISERFS: Error, %s hash detected, "
1083 "unable to force rupasov hash\n", reiserfs_hashname(code)) ;
1085 } else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
1086 printk("REISERFS: Error, %s hash detected, "
1087 "unable to force tea hash\n", reiserfs_hashname(code)) ;
1089 } else if (reiserfs_r5_hash(s) && code != R5_HASH) {
1090 printk("REISERFS: Error, %s hash detected, "
1091 "unable to force r5 hash\n", reiserfs_hashname(code)) ;
1095 /* find_hash_out was not called or could not determine the hash */
1096 if (reiserfs_rupasov_hash(s)) {
1098 } else if (reiserfs_tea_hash(s)) {
1100 } else if (reiserfs_r5_hash(s)) {
1105 /* if we are mounted RW, and we have a new valid hash code, update
1108 if (code != UNSET_HASH &&
1109 !(s->s_flags & MS_RDONLY) &&
1110 code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1111 set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1116 // return pointer to appropriate function
1117 static hashf_t hash_function (struct super_block * s)
1119 switch (what_hash (s)) {
1121 reiserfs_warning (s, "Using tea hash to sort names\n");
1124 reiserfs_warning (s, "Using rupasov hash to sort names\n");
1127 reiserfs_warning (s, "Using r5 hash to sort names\n");
1133 // this is used to set up correct value for old partitions
1134 int function2code (hashf_t func)
1136 if (func == keyed_hash)
1138 if (func == yura_hash)
1140 if (func == r5_hash)
1143 BUG() ; // should never happen
1148 static struct super_block * reiserfs_read_super (struct super_block * s, void * data, int silent)
1151 struct inode *root_inode;
1152 kdev_t dev = s->s_dev;
1154 struct reiserfs_transaction_handle th ;
1156 unsigned long blocks;
1157 int jinit_done = 0 ;
1158 struct reiserfs_iget4_args args ;
1160 struct reiserfs_super_block * rs;
1163 memset (&s->u.reiserfs_sb, 0, sizeof (struct reiserfs_sb_info));
1164 /* Set default values for options: non-aggressive tails */
1165 s->u.reiserfs_sb.s_mount_opt = ( 1 << REISERFS_SMALLTAIL );
1166 /* default block allocator option: skip_busy */
1167 s->u.reiserfs_sb.s_alloc_options.bits = ( 1 << 5);
1168 /* If file grew past 4 blocks, start preallocation blocks for it. */
1169 s->u.reiserfs_sb.s_alloc_options.preallocmin = 4;
1170 /* Preallocate by 8 blocks (9-1) at once */
1171 s->u.reiserfs_sb.s_alloc_options.preallocsize = 9;
1173 if (reiserfs_parse_options (s, (char *) data, &(s->u.reiserfs_sb.s_mount_opt), &blocks) == 0) {
1181 reiserfs_warning(s,"zam-2013: reserfs resize option for remount only\n");
1185 if (blksize_size[MAJOR(dev)] && blksize_size[MAJOR(dev)][MINOR(dev)] != 0) {
1186 /* as blocksize is set for partition we use it */
1187 size = blksize_size[MAJOR(dev)][MINOR(dev)];
1190 set_blocksize (s->s_dev, BLOCK_SIZE);
1192 /* try old format (undistributed bitmap, super block in 8-th 1k block of a device) */
1193 if (!read_super_block (s, size, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
1195 /* try new format (64-th 1k block), which can contain reiserfs super block */
1196 else if (read_super_block (s, size, REISERFS_DISK_OFFSET_IN_BYTES)) {
1197 printk("sh-2021: reiserfs_read_super: can not find reiserfs on %s\n", bdevname(s->s_dev));
1201 rs = SB_DISK_SUPER_BLOCK (s);
1203 /* Let's do basic sanity check to verify that underlying device is not
1204 smaller than the filesystem. If the check fails then abort and scream,
1205 because bad stuff will happen otherwise. */
1206 if ( blk_size[MAJOR(dev)][MINOR(dev)] < sb_block_count(rs)*(sb_blocksize(rs)>>10) ) {
1207 printk("Filesystem on %s cannot be mounted because it is bigger than the device\n", kdevname(dev));
1208 printk("You may need to run fsck or increase size of your LVM partition\n");
1209 printk("Or may be you forgot to reboot after fdisk when it told you to\n");
1213 s->u.reiserfs_sb.s_mount_state = SB_REISERFS_STATE(s);
1214 s->u.reiserfs_sb.s_mount_state = REISERFS_VALID_FS ;
1216 if (old_format ? read_old_bitmaps(s) : read_bitmaps(s)) {
1217 reiserfs_warning (s, "sh-2014: reiserfs_read_super: unable to read bitmap\n");
1220 #ifdef CONFIG_REISERFS_CHECK
1221 printk("reiserfs:warning: CONFIG_REISERFS_CHECK is set ON\n");
1222 printk("reiserfs:warning: - it is slow mode for debugging.\n");
1228 if( journal_init(s, jdev_name, old_format) ) {
1229 reiserfs_warning(s, "sh-2022: reiserfs_read_super: unable to initialize journal space\n") ;
1232 jinit_done = 1 ; /* once this is set, journal_release must be called
1233 ** if we error out of the mount
1236 if (reread_meta_blocks(s)) {
1237 reiserfs_warning(s, "sh-2015: reiserfs_read_super: unable to reread meta blocks after journal init\n") ;
1241 if (replay_only (s))
1244 if (is_read_only(s->s_dev) && !(s->s_flags & MS_RDONLY)) {
1245 reiserfs_warning(s, "clm-7000: Detected readonly device, marking FS readonly\n") ;
1246 s->s_flags |= MS_RDONLY ;
1248 args.objectid = REISERFS_ROOT_PARENT_OBJECTID ;
1249 root_inode = iget4 (s, REISERFS_ROOT_OBJECTID, 0, (void *)(&args));
1251 reiserfs_warning (s, "reiserfs_read_super: get root inode failed\n");
1255 s->s_root = d_alloc_root(root_inode);
1261 // define and initialize hash function
1262 s->u.reiserfs_sb.s_hash_function = hash_function (s);
1263 if (s->u.reiserfs_sb.s_hash_function == NULL) {
1269 if (is_reiserfs_3_5 (rs) || (is_reiserfs_jr (rs) && SB_VERSION (s) == REISERFS_VERSION_1))
1270 set_bit(REISERFS_3_5, &(s->u.reiserfs_sb.s_properties));
1272 set_bit(REISERFS_3_6, &(s->u.reiserfs_sb.s_properties));
1274 if (!(s->s_flags & MS_RDONLY)) {
1276 journal_begin(&th, s, 1) ;
1277 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
1279 set_sb_umount_state( rs, REISERFS_ERROR_FS );
1280 set_sb_fs_state (rs, 0);
1282 if (old_format_only(s)) {
1283 /* filesystem of format 3.5 either with standard or non-standard
1285 if (convert_reiserfs (s)) {
1286 /* and -o conv is given */
1287 reiserfs_warning (s, "reiserfs: converting 3.5 filesystem to the 3.6 format\n") ;
1289 if (is_reiserfs_3_5 (rs))
1290 /* put magic string of 3.6 format. 2.2 will not be able to
1291 mount this filesystem anymore */
1292 memcpy (rs->s_v1.s_magic, reiserfs_3_6_magic_string,
1293 sizeof (reiserfs_3_6_magic_string));
1295 set_sb_version(rs,REISERFS_VERSION_2);
1296 reiserfs_convert_objectid_map_v1(s) ;
1297 set_bit(REISERFS_3_6, &(s->u.reiserfs_sb.s_properties));
1298 clear_bit(REISERFS_3_5, &(s->u.reiserfs_sb.s_properties));
1302 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
1303 journal_end(&th, s, 1) ;
1305 /* look for files which were to be removed in previous session */
1306 finish_unfinished (s);
1311 // mark hash in super block: it could be unset. overwrite should be ok
1312 set_sb_hash_function_code( rs, function2code(s->u.reiserfs_sb.s_hash_function ) );
1316 reiserfs_proc_info_init( s );
1317 reiserfs_proc_register( s, "version", reiserfs_version_in_proc );
1318 reiserfs_proc_register( s, "super", reiserfs_super_in_proc );
1319 reiserfs_proc_register( s, "per-level", reiserfs_per_level_in_proc );
1320 reiserfs_proc_register( s, "bitmap", reiserfs_bitmap_in_proc );
1321 reiserfs_proc_register( s, "on-disk-super", reiserfs_on_disk_super_in_proc );
1322 reiserfs_proc_register( s, "oidmap", reiserfs_oidmap_in_proc );
1323 reiserfs_proc_register( s, "journal", reiserfs_journal_in_proc );
1324 init_waitqueue_head (&(s->u.reiserfs_sb.s_wait));
1329 if (jinit_done) { /* kill the commit thread, free journal ram */
1330 journal_release_error(NULL, s) ;
1332 if (SB_DISK_SUPER_BLOCK (s)) {
1333 for (j = 0; j < SB_BMAP_NR (s); j ++) {
1334 if (SB_AP_BITMAP (s))
1335 brelse (SB_AP_BITMAP (s)[j].bh);
1337 if (SB_AP_BITMAP (s))
1338 vfree (SB_AP_BITMAP (s));
1340 if (SB_BUFFER_WITH_SB (s))
1341 brelse(SB_BUFFER_WITH_SB (s));
1347 static int reiserfs_statfs (struct super_block * s, struct statfs * buf)
1349 struct reiserfs_super_block * rs = SB_DISK_SUPER_BLOCK (s);
1351 buf->f_namelen = (REISERFS_MAX_NAME (s->s_blocksize));
1354 buf->f_bfree = sb_free_blocks(rs);
1355 buf->f_bavail = buf->f_bfree;
1356 buf->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
1357 buf->f_bsize = s->s_blocksize;
1358 /* changed to accomodate gcc folks.*/
1359 buf->f_type = REISERFS_SUPER_MAGIC;
1363 static DECLARE_FSTYPE_DEV(reiserfs_fs_type,"reiserfs",reiserfs_read_super);
1365 static int __init init_reiserfs_fs (void)
1367 reiserfs_proc_info_global_init();
1368 reiserfs_proc_register_global( "version",
1369 reiserfs_global_version_in_proc );
1370 return register_filesystem(&reiserfs_fs_type);
1373 MODULE_DESCRIPTION("ReiserFS journaled filesystem");
1374 MODULE_AUTHOR("Hans Reiser <reiser@namesys.com>");
1375 MODULE_LICENSE("GPL");
1378 static void __exit exit_reiserfs_fs(void)
1380 reiserfs_proc_unregister_global( "version" );
1381 reiserfs_proc_info_global_done();
1382 unregister_filesystem(&reiserfs_fs_type);
1386 module_init(init_reiserfs_fs) ;
1387 module_exit(exit_reiserfs_fs) ;