2 * Copyright 2000 Hans Reiser
3 * See README for licensing and copyright details
5 * Ported to FreeBSD by Jean-Sébastien Pédron <jspedron@club-internet.fr>
10 #include <gnu/fs/reiserfs/reiserfs_fs.h>
12 const char reiserfs_3_5_magic_string[] = REISERFS_SUPER_MAGIC_STRING;
13 const char reiserfs_3_6_magic_string[] = REISER2FS_SUPER_MAGIC_STRING;
14 const char reiserfs_jr_magic_string[] = REISER2FS_JR_SUPER_MAGIC_STRING;
17 * Default recommended I/O size is 128k. There might be broken
18 * applications that are confused by this. Use nolargeio mount option to
19 * get usual i/o size = PAGE_SIZE.
21 int reiserfs_default_io_size = 128 * 1024;
23 static vfs_cmount_t reiserfs_cmount;
24 static vfs_fhtovp_t reiserfs_fhtovp;
25 static vfs_mount_t reiserfs_mount;
26 static vfs_root_t reiserfs_root;
27 static vfs_statfs_t reiserfs_statfs;
28 static vfs_unmount_t reiserfs_unmount;
30 static int reiserfs_mountfs(struct vnode *devvp, struct mount *mp,
32 static void load_bitmap_info_data(struct reiserfs_sb_info *sbi,
33 struct reiserfs_bitmap_info *bi);
34 static int read_bitmaps(struct reiserfs_mount *rmp);
35 static int read_old_bitmaps(struct reiserfs_mount *rmp);
36 static int read_super_block(struct reiserfs_mount *rmp, int offset);
37 static hashf_t hash_function(struct reiserfs_mount *rmp);
39 static int get_root_node(struct reiserfs_mount *rmp,
40 struct reiserfs_node **root);
41 uint32_t find_hash_out(struct reiserfs_mount *rmp);
43 MALLOC_DEFINE(M_REISERFSMNT, "reiserfs_mount", "ReiserFS mount structure");
44 MALLOC_DEFINE(M_REISERFSPATH, "reiserfs_path", "ReiserFS path structure");
45 MALLOC_DEFINE(M_REISERFSNODE, "reiserfs_node", "ReiserFS vnode private part");
47 /* -------------------------------------------------------------------
49 * -------------------------------------------------------------------*/
52 reiserfs_cmount(struct mntarg *ma, void *data, int flags)
54 struct reiserfs_args args;
57 error = copyin(data, &args, sizeof(args));
61 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
62 ma = mount_arg(ma, "export", &args.export, sizeof args.export);
64 error = kernel_mount(ma, flags);
73 reiserfs_mount(struct mount *mp)
80 struct vfsoptlist *opts;
81 struct reiserfs_mount *rmp;
82 struct reiserfs_sb_info *sbi;
83 struct nameidata nd, *ndp = &nd;
87 if (!(mp->mnt_flag & MNT_RDONLY))
90 /* Get the new options passed to mount */
91 opts = mp->mnt_optnew;
93 /* `fspath' contains the mount point (eg. /mnt/linux); REQUIRED */
94 vfs_getopt(opts, "fspath", (void **)&path, NULL);
95 reiserfs_log(LOG_INFO, "mount point is `%s'\n", path);
97 /* `from' contains the device name (eg. /dev/ad0s1); REQUIRED */
99 error = vfs_getopt(opts, "from", (void **)&fspec, &len);
100 if (!error && fspec[len - 1] != '\0')
102 reiserfs_log(LOG_INFO, "device is `%s'\n", fspec);
104 /* Handle MNT_UPDATE (mp->mnt_flag) */
105 if (mp->mnt_flag & MNT_UPDATE) {
106 /* For now, only NFS export is supported. */
107 if (vfs_flagopt(opts, "export", NULL, 0))
111 /* Not an update, or updating the name: look up the name
112 * and verify that it refers to a sensible disk device. */
116 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
117 if ((error = namei(ndp)) != 0)
119 NDFREE(ndp, NDF_ONLY_PNBUF);
122 if (!vn_isdisk(devvp, &error)) {
127 /* If mount by non-root, then verify that user has necessary
128 * permissions on the device. */
130 if ((mp->mnt_flag & MNT_RDONLY) == 0)
132 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
134 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
140 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
141 error = reiserfs_mountfs(devvp, mp, td);
143 /* TODO Handle MNT_UPDATE */
153 rmp = VFSTOREISERFS(mp);
154 sbi = rmp->rm_reiserfs;
157 * Note that this strncpy() is ok because of a check at the start
158 * of reiserfs_mount().
160 reiserfs_log(LOG_DEBUG, "prepare statfs data\n");
161 (void)copystr(fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
162 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
163 (void)reiserfs_statfs(mp, &mp->mnt_stat);
165 reiserfs_log(LOG_DEBUG, "done\n");
170 * Unmount system call
173 reiserfs_unmount(struct mount *mp, int mntflags)
175 int error, flags = 0;
176 struct reiserfs_mount *rmp;
177 struct reiserfs_sb_info *sbi;
179 reiserfs_log(LOG_DEBUG, "get private data\n");
180 rmp = VFSTOREISERFS(mp);
181 sbi = rmp->rm_reiserfs;
183 /* Flangs handling */
184 reiserfs_log(LOG_DEBUG, "handle mntflags\n");
185 if (mntflags & MNT_FORCE)
188 /* Flush files -> vflush */
189 reiserfs_log(LOG_DEBUG, "flush vnodes\n");
190 if ((error = vflush(mp, 0, flags, curthread)))
193 /* XXX Super block update */
196 if (SB_AP_BITMAP(sbi)) {
198 reiserfs_log(LOG_DEBUG,
199 "release bitmap buffers (total: %d)\n",
201 for (i = 0; i < SB_BMAP_NR(sbi); i++) {
202 if (SB_AP_BITMAP(sbi)[i].bp_data) {
203 free(SB_AP_BITMAP(sbi)[i].bp_data,
205 SB_AP_BITMAP(sbi)[i].bp_data = NULL;
209 reiserfs_log(LOG_DEBUG, "free bitmaps structure\n");
210 free(SB_AP_BITMAP(sbi), M_REISERFSMNT);
211 SB_AP_BITMAP(sbi) = NULL;
215 reiserfs_log(LOG_DEBUG, "free super block data\n");
216 free(sbi->s_rs, M_REISERFSMNT);
221 reiserfs_log(LOG_DEBUG, "close device\n");
222 #if defined(si_mountpoint)
223 rmp->rm_devvp->v_rdev->si_mountpoint = NULL;
228 g_wither_geom_close(rmp->rm_cp->geom, ENXIO);
231 vrele(rmp->rm_devvp);
234 reiserfs_log(LOG_DEBUG, "free sbi\n");
235 free(sbi, M_REISERFSMNT);
236 sbi = rmp->rm_reiserfs = NULL;
239 reiserfs_log(LOG_DEBUG, "free rmp\n");
240 free(rmp, M_REISERFSMNT);
246 mp->mnt_flag &= ~MNT_LOCAL;
249 reiserfs_log(LOG_DEBUG, "done\n");
254 * Return the root of a filesystem.
257 reiserfs_root(struct mount *mp, int flags, struct vnode **vpp)
261 struct cpu_key rootkey;
263 rootkey.on_disk_key.k_dir_id = REISERFS_ROOT_PARENT_OBJECTID;
264 rootkey.on_disk_key.k_objectid = REISERFS_ROOT_OBJECTID;
266 error = reiserfs_iget(mp, &rootkey, &vp, curthread);
277 reiserfs_statfs(struct mount *mp, struct statfs *sbp)
279 struct reiserfs_mount *rmp;
280 struct reiserfs_sb_info *sbi;
281 struct reiserfs_super_block *rs;
283 reiserfs_log(LOG_DEBUG, "get private data\n");
284 rmp = VFSTOREISERFS(mp);
285 sbi = rmp->rm_reiserfs;
288 reiserfs_log(LOG_DEBUG, "fill statfs structure\n");
289 sbp->f_bsize = sbi->s_blocksize;
290 sbp->f_iosize = sbp->f_bsize;
291 sbp->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
292 sbp->f_bfree = sb_free_blocks(rs);
293 sbp->f_bavail = sbp->f_bfree;
296 reiserfs_log(LOG_DEBUG, " block size = %ju\n",
297 (intmax_t)sbp->f_bsize);
298 reiserfs_log(LOG_DEBUG, " IO size = %ju\n",
299 (intmax_t)sbp->f_iosize);
300 reiserfs_log(LOG_DEBUG, " block count = %ju\n",
301 (intmax_t)sbp->f_blocks);
302 reiserfs_log(LOG_DEBUG, " free blocks = %ju\n",
303 (intmax_t)sbp->f_bfree);
304 reiserfs_log(LOG_DEBUG, " avail blocks = %ju\n",
305 (intmax_t)sbp->f_bavail);
306 reiserfs_log(LOG_DEBUG, "...done\n");
308 if (sbp != &mp->mnt_stat) {
309 reiserfs_log(LOG_DEBUG, "copying monut point info\n");
310 sbp->f_type = mp->mnt_vfc->vfc_typenum;
311 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
312 (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
313 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
314 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
315 reiserfs_log(LOG_DEBUG, " mount from: %s\n",
317 reiserfs_log(LOG_DEBUG, " mount on: %s\n",
319 reiserfs_log(LOG_DEBUG, "...done\n");
326 * File handle to vnode
328 * Have to be really careful about stale file handles:
329 * - check that the inode key is valid
330 * - call ffs_vget() to get the locked inode
331 * - check for an unallocated inode (i_mode == 0)
332 * - check that the given client host has export rights and return
333 * those rights via. exflagsp and credanonp
336 reiserfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
342 struct reiserfs_node *ip;
343 struct reiserfs_sb_info *sbi;
344 struct thread *td = curthread;
346 rfhp = (struct rfid *)fhp;
347 sbi = VFSTOREISERFS(mp)->rm_reiserfs;
349 /* Check that the key is valid */
350 if (rfhp->rfid_dirid < REISERFS_ROOT_PARENT_OBJECTID &&
351 rfhp->rfid_objectid < REISERFS_ROOT_OBJECTID)
354 reiserfs_log(LOG_DEBUG,
355 "file handle key is (dirid=%d, objectid=%d)\n",
356 rfhp->rfid_dirid, rfhp->rfid_objectid);
357 key.on_disk_key.k_dir_id = rfhp->rfid_dirid;
358 key.on_disk_key.k_objectid = rfhp->rfid_objectid;
360 reiserfs_log(LOG_DEBUG, "read this inode\n");
361 error = reiserfs_iget(mp, &key, &nvp, td);
367 reiserfs_log(LOG_DEBUG, "check validity\n");
369 if (ip->i_mode == 0 || ip->i_generation != rfhp->rfid_gen) {
375 reiserfs_log(LOG_DEBUG, "return it\n");
380 /* -------------------------------------------------------------------
381 * Functions for the journal
382 * -------------------------------------------------------------------*/
385 is_reiserfs_3_5(struct reiserfs_super_block *rs)
388 return (!strncmp(rs->s_v1.s_magic, reiserfs_3_5_magic_string,
389 strlen(reiserfs_3_5_magic_string)));
393 is_reiserfs_3_6(struct reiserfs_super_block *rs)
396 return (!strncmp(rs->s_v1.s_magic, reiserfs_3_6_magic_string,
397 strlen(reiserfs_3_6_magic_string)));
401 is_reiserfs_jr(struct reiserfs_super_block *rs)
404 return (!strncmp(rs->s_v1.s_magic, reiserfs_jr_magic_string,
405 strlen(reiserfs_jr_magic_string)));
409 is_any_reiserfs_magic_string(struct reiserfs_super_block *rs)
412 return ((is_reiserfs_3_5(rs) || is_reiserfs_3_6(rs) ||
413 is_reiserfs_jr(rs)));
416 /* -------------------------------------------------------------------
418 * -------------------------------------------------------------------*/
421 * Common code for mount and mountroot
424 reiserfs_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td)
426 int error, old_format = 0;
427 struct reiserfs_mount *rmp;
428 struct reiserfs_sb_info *sbi;
429 struct reiserfs_super_block *rs;
430 struct cdev *dev = devvp->v_rdev;
432 #if (__FreeBSD_version >= 600000)
433 struct g_consumer *cp;
437 //ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
439 #if (__FreeBSD_version < 600000)
441 * Disallow multiple mounts of the same device.
442 * Disallow mounting of a device that is currently in use
443 * (except for root, which might share swap device for miniroot).
444 * Flush out any old buffers remaining from a previous use.
446 if ((error = vfs_mountedon(devvp)) != 0)
448 if (vcount(devvp) > 1)
451 error = vinvalbuf(devvp, V_SAVE, td->td_ucred, td, 0, 0);
453 VOP_UNLOCK(devvp, 0);
458 * Open the device in read-only, 'cause we don't support write
461 error = VOP_OPEN(devvp, FREAD, FSCRED, td, NULL);
462 VOP_UNLOCK(devvp, 0);
468 error = g_vfs_open(devvp, &cp, "reiserfs", /* read-only */ 0);
471 VOP_UNLOCK(devvp, 0);
475 bo = &devvp->v_bufobj;
477 bo->bo_ops = g_vfs_bufops;
480 if (devvp->v_rdev->si_iosize_max != 0)
481 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
482 if (mp->mnt_iosize_max > MAXPHYS)
483 mp->mnt_iosize_max = MAXPHYS;
488 /* rmp contains any information about this specific mount */
489 rmp = malloc(sizeof *rmp, M_REISERFSMNT, M_WAITOK | M_ZERO);
494 sbi = malloc(sizeof *sbi, M_REISERFSMNT, M_WAITOK | M_ZERO);
499 rmp->rm_reiserfs = sbi;
501 rmp->rm_devvp = devvp;
503 #if (__FreeBSD_version >= 600000)
504 rmp->rm_bo = &devvp->v_bufobj;
508 /* Set default values for options: non-aggressive tails */
509 REISERFS_SB(sbi)->s_mount_opt = (1 << REISERFS_SMALLTAIL);
510 REISERFS_SB(sbi)->s_rd_only = 1;
511 REISERFS_SB(sbi)->s_devvp = devvp;
513 /* Read the super block */
514 if ((error = read_super_block(rmp, REISERFS_OLD_DISK_OFFSET)) == 0) {
515 /* The read process succeeded, it's an old format */
517 } else if ((error = read_super_block(rmp, REISERFS_DISK_OFFSET)) != 0) {
518 reiserfs_log(LOG_ERR, "can not find a ReiserFS filesystem\n");
522 rs = SB_DISK_SUPER_BLOCK(sbi);
525 * Let's do basic sanity check to verify that underlying device is
526 * not smaller than the filesystem. If the check fails then abort and
527 * scream, because bad stuff will happen otherwise.
530 if (s->s_bdev && s->s_bdev->bd_inode &&
531 i_size_read(s->s_bdev->bd_inode) <
532 sb_block_count(rs) * sb_blocksize(rs)) {
533 reiserfs_log(LOG_ERR,
534 "reiserfs: filesystem cannot be mounted because it is "
535 "bigger than the device.\n");
536 reiserfs_log(LOG_ERR, "reiserfs: you may need to run fsck "
537 "rr may be you forgot to reboot after fdisk when it "
544 * XXX This is from the original Linux code, but why affecting 2 values
545 * to the same variable?
547 sbi->s_mount_state = SB_REISERFS_STATE(sbi);
548 sbi->s_mount_state = REISERFS_VALID_FS;
550 if ((error = (old_format ?
551 read_old_bitmaps(rmp) : read_bitmaps(rmp)))) {
552 reiserfs_log(LOG_ERR, "unable to read bitmap\n");
556 /* Make data=ordered the default */
557 if (!reiserfs_data_log(sbi) && !reiserfs_data_ordered(sbi) &&
558 !reiserfs_data_writeback(sbi)) {
559 REISERFS_SB(sbi)->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
562 if (reiserfs_data_log(sbi)) {
563 reiserfs_log(LOG_INFO, "using journaled data mode\n");
564 } else if (reiserfs_data_ordered(sbi)) {
565 reiserfs_log(LOG_INFO, "using ordered data mode\n");
567 reiserfs_log(LOG_INFO, "using writeback data mode\n");
570 /* TODO Not yet supported */
572 if(journal_init(sbi, jdev_name, old_format, commit_max_age)) {
573 reiserfs_log(LOG_ERR, "unable to initialize journal space\n");
576 jinit_done = 1 ; /* once this is set, journal_release must
577 be called if we error out of the mount */
580 if (reread_meta_blocks(sbi)) {
581 reiserfs_log(LOG_ERR,
582 "unable to reread meta blocks after journal init\n");
587 /* Define and initialize hash function */
588 sbi->s_hash_function = hash_function(rmp);
590 if (sbi->s_hash_function == NULL) {
591 reiserfs_log(LOG_ERR, "couldn't determined hash function\n");
596 if (is_reiserfs_3_5(rs) ||
597 (is_reiserfs_jr(rs) && SB_VERSION(sbi) == REISERFS_VERSION_1))
598 bit_set(&(sbi->s_properties), REISERFS_3_5);
600 bit_set(&(sbi->s_properties), REISERFS_3_6);
603 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
604 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
606 mp->mnt_flag |= MNT_LOCAL;
608 #if defined(si_mountpoint)
609 devvp->v_rdev->si_mountpoint = mp;
615 reiserfs_log(LOG_INFO, "*** error during mount ***\n");
617 if (SB_AP_BITMAP(sbi)) {
619 for (i = 0; i < SB_BMAP_NR(sbi); i++) {
620 if (!SB_AP_BITMAP(sbi)[i].bp_data)
622 free(SB_AP_BITMAP(sbi)[i].bp_data, M_REISERFSMNT);
624 free(SB_AP_BITMAP(sbi), M_REISERFSMNT);
628 free(sbi->s_rs, M_REISERFSMNT);
633 #if (__FreeBSD_version < 600000)
634 (void)VOP_CLOSE(devvp, FREAD, NOCRED, td);
639 g_wither_geom_close(cp->geom, ENXIO);
646 free(sbi, M_REISERFSMNT);
648 free(rmp, M_REISERFSMNT);
653 * Read the super block
656 read_super_block(struct reiserfs_mount *rmp, int offset)
660 struct reiserfs_super_block *rs;
661 struct reiserfs_sb_info *sbi;
662 uint16_t fs_blocksize;
664 if (offset == REISERFS_OLD_DISK_OFFSET) {
665 reiserfs_log(LOG_DEBUG,
666 "reiserfs/super: read old format super block\n");
668 reiserfs_log(LOG_DEBUG,
669 "reiserfs/super: read new format super block\n");
672 /* Read the super block */
673 if ((error = bread(rmp->rm_devvp, offset * btodb(REISERFS_BSIZE),
674 REISERFS_BSIZE, NOCRED, &bp)) != 0) {
675 reiserfs_log(LOG_ERR, "can't read device\n");
679 /* Get it from the buffer data */
680 rs = (struct reiserfs_super_block *)bp->b_data;
681 if (!is_any_reiserfs_magic_string(rs)) {
686 fs_blocksize = sb_blocksize(rs);
690 if (fs_blocksize <= 0) {
691 reiserfs_log(LOG_ERR, "unexpected null block size");
695 /* Read the super block (for double check)
696 * We can't read the same blkno with a different size: it causes
697 * panic() if INVARIANTS is set. So we keep REISERFS_BSIZE */
698 if ((error = bread(rmp->rm_devvp,
699 offset * REISERFS_BSIZE / fs_blocksize * btodb(fs_blocksize),
700 REISERFS_BSIZE, NOCRED, &bp)) != 0) {
701 reiserfs_log(LOG_ERR, "can't reread the super block\n");
705 rs = (struct reiserfs_super_block *)bp->b_data;
706 if (sb_blocksize(rs) != fs_blocksize) {
707 reiserfs_log(LOG_ERR, "unexpected block size "
708 "(found=%u, expected=%u)\n",
709 sb_blocksize(rs), fs_blocksize);
714 reiserfs_log(LOG_DEBUG, "magic: `%s'\n", rs->s_v1.s_magic);
715 reiserfs_log(LOG_DEBUG, "label: `%s'\n", rs->s_label);
716 reiserfs_log(LOG_DEBUG, "block size: %6d\n", sb_blocksize(rs));
717 reiserfs_log(LOG_DEBUG, "block count: %6u\n",
718 rs->s_v1.s_block_count);
719 reiserfs_log(LOG_DEBUG, "bitmaps number: %6u\n",
722 if (rs->s_v1.s_root_block == -1) {
724 "reiserfs: Unfinished reiserfsck --rebuild-tree run "
726 "run reiserfsck --rebuild-tree and wait for a "
727 "completion. If that\n"
728 "fails, get newer reiserfsprogs package");
733 sbi = rmp->rm_reiserfs;
734 sbi->s_blocksize = fs_blocksize;
736 for (bits = 9, fs_blocksize >>= 9; fs_blocksize >>= 1; bits++)
738 sbi->s_blocksize_bits = bits;
740 /* Copy the buffer and release it */
741 sbi->s_rs = malloc(sizeof *rs, M_REISERFSMNT, M_WAITOK | M_ZERO);
743 reiserfs_log(LOG_ERR, "can not read the super block\n");
747 bcopy(rs, sbi->s_rs, sizeof(struct reiserfs_super_block));
750 if (is_reiserfs_jr(rs)) {
751 if (sb_version(rs) == REISERFS_VERSION_2)
752 reiserfs_log(LOG_INFO, "found reiserfs format \"3.6\""
753 " with non-standard journal");
754 else if (sb_version(rs) == REISERFS_VERSION_1)
755 reiserfs_log(LOG_INFO, "found reiserfs format \"3.5\""
756 " with non-standard journal");
758 reiserfs_log(LOG_ERR, "found unknown "
759 "format \"%u\" of reiserfs with non-standard magic",
765 * s_version of standard format may contain incorrect
766 * information, so we just look at the magic string
768 reiserfs_log(LOG_INFO,
769 "found reiserfs format \"%s\" with standard journal\n",
770 is_reiserfs_3_5(rs) ? "3.5" : "3.6");
777 * load_bitmap_info_data - Sets up the reiserfs_bitmap_info structure
779 * @sbi - superblock info for this filesystem
780 * @bi - the bitmap info to be loaded. Requires that bi->bp is valid.
782 * This routine counts how many free bits there are, finding the first
783 * zero as a side effect. Could also be implemented as a loop of
784 * test_bit() calls, or a loop of find_first_zero_bit() calls. This
785 * implementation is similar to find_first_zero_bit(), but doesn't
786 * return after it finds the first bit. Should only be called on fs
787 * mount, but should be fairly efficient anyways.
789 * bi->first_zero_hint is considered unset if it == 0, since the bitmap
790 * itself will invariably occupt block 0 represented in the bitmap. The
791 * only exception to this is when free_count also == 0, since there will
792 * be no free blocks at all.
795 load_bitmap_info_data(struct reiserfs_sb_info *sbi,
796 struct reiserfs_bitmap_info *bi)
800 cur = (unsigned long *)bi->bp_data;
801 while ((char *)cur < (bi->bp_data + sbi->s_blocksize)) {
803 * No need to scan if all 0's or all 1's.
804 * Since we're only counting 0's, we can simply ignore
808 if (bi->first_zero_hint == 0) {
809 bi->first_zero_hint =
810 ((char *)cur - bi->bp_data) << 3;
812 bi->free_count += sizeof(unsigned long) * 8;
813 } else if (*cur != ~0L) {
816 for (b = 0; b < sizeof(unsigned long) * 8; b++) {
817 if (!reiserfs_test_le_bit(b, cur)) {
819 if (bi->first_zero_hint == 0)
820 bi->first_zero_hint =
822 bi->bp_data) << 3) + b;
834 read_bitmaps(struct reiserfs_mount *rmp)
837 struct buf *bp = NULL;
838 struct reiserfs_sb_info *sbi = rmp->rm_reiserfs;
840 /* Allocate memory for the table of bitmaps */
842 malloc(sizeof(struct reiserfs_bitmap_info) * SB_BMAP_NR(sbi),
843 M_REISERFSMNT, M_WAITOK | M_ZERO);
844 if (!SB_AP_BITMAP(sbi))
847 /* Read all the bitmaps */
849 bmap_nr = (REISERFS_DISK_OFFSET_IN_BYTES / sbi->s_blocksize + 1) *
850 btodb(sbi->s_blocksize);
851 i < SB_BMAP_NR(sbi); i++, bmap_nr = sbi->s_blocksize * 8 * i) {
852 SB_AP_BITMAP(sbi)[i].bp_data = malloc(sbi->s_blocksize,
853 M_REISERFSMNT, M_WAITOK | M_ZERO);
854 if (!SB_AP_BITMAP(sbi)[i].bp_data)
856 bread(rmp->rm_devvp, bmap_nr, sbi->s_blocksize, NOCRED, &bp);
857 bcopy(bp->b_data, SB_AP_BITMAP(sbi)[i].bp_data,
862 /*if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh))
863 ll_rw_block(READ, 1, &SB_AP_BITMAP(s)[i].bh);*/
866 for (i = 0; i < SB_BMAP_NR(sbi); i++) {
867 /*if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) {
868 reiserfs_warning(s,"sh-2029: reiserfs read_bitmaps: "
869 "bitmap block (#%lu) reading failed",
870 SB_AP_BITMAP(s)[i].bh->b_blocknr);
871 for (i = 0; i < SB_BMAP_NR(s); i++)
872 brelse(SB_AP_BITMAP(s)[i].bh);
873 vfree(SB_AP_BITMAP(s));
874 SB_AP_BITMAP(s) = NULL;
877 load_bitmap_info_data(sbi, SB_AP_BITMAP(sbi) + i);
878 reiserfs_log(LOG_DEBUG,
879 "%d free blocks (starting at block %ld)\n",
880 SB_AP_BITMAP(sbi)[i].free_count,
881 (long)SB_AP_BITMAP(sbi)[i].first_zero_hint);
887 // TODO Not supported
889 read_old_bitmaps(struct reiserfs_mount *rmp)
895 struct reiserfs_sb_info *sbi = rmp->rm_reiserfs;
896 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(sbi);
898 /* First of bitmap blocks */
899 int bmp1 = (REISERFS_OLD_DISK_OFFSET / sbi->s_blocksize) *
900 btodb(sbi->s_blocksize);
902 /* Read true bitmap */
904 malloc(sizeof (struct reiserfs_buffer_info *) * sb_bmap_nr(rs),
905 M_REISERFSMNT, M_WAITOK | M_ZERO);
906 if (!SB_AP_BITMAP(sbi))
909 for (i = 0; i < sb_bmap_nr(rs); i ++) {
910 SB_AP_BITMAP(sbi)[i].bp = getblk(rmp->rm_devvp,
911 (bmp1 + i) * btodb(sbi->s_blocksize), sbi->s_blocksize, 0, 0, 0);
912 if (!SB_AP_BITMAP(sbi)[i].bp)
914 load_bitmap_info_data(sbi, SB_AP_BITMAP(sbi) + i);
921 /* -------------------------------------------------------------------
922 * Hash detection stuff
923 * -------------------------------------------------------------------*/
926 get_root_node(struct reiserfs_mount *rmp, struct reiserfs_node **root)
928 struct reiserfs_node *ip;
929 struct reiserfs_iget_args args;
931 /* Allocate the node structure */
932 reiserfs_log(LOG_DEBUG, "malloc(struct reiserfs_node)\n");
933 ip = malloc(sizeof(struct reiserfs_node),
934 M_REISERFSNODE, M_WAITOK | M_ZERO);
936 /* Fill the structure */
937 reiserfs_log(LOG_DEBUG, "filling *ip\n");
938 ip->i_dev = rmp->rm_dev;
939 ip->i_number = REISERFS_ROOT_OBJECTID;
940 ip->i_ino = REISERFS_ROOT_PARENT_OBJECTID;
941 ip->i_reiserfs = rmp->rm_reiserfs;
944 args.objectid = ip->i_number;
945 args.dirid = ip->i_ino;
946 reiserfs_log(LOG_DEBUG, "call reiserfs_read_locked_inode("
947 "objectid=%d,dirid=%d)\n", args.objectid, args.dirid);
948 reiserfs_read_locked_inode(ip, &args);
950 ip->i_devvp = rmp->rm_devvp;
951 //XXX VREF(ip->i_devvp); Is it necessary ?
958 * If root directory is empty - we set default - Yura's - hash and warn
960 * FIXME: we look for only one name in a directory. If tea and yura both
961 * have the same value - we ask user to send report to the mailing list
963 uint32_t find_hash_out(struct reiserfs_mount *rmp)
967 INITIALIZE_PATH(path);
968 struct reiserfs_node *ip;
969 struct reiserfs_sb_info *sbi;
970 struct reiserfs_dir_entry de;
971 uint32_t hash = DEFAULT_HASH;
973 get_root_node(rmp, &ip);
977 sbi = rmp->rm_reiserfs;
980 uint32_t teahash, r5hash, yurahash;
982 reiserfs_log(LOG_DEBUG, "make_cpu_key\n");
983 make_cpu_key(&key, ip, ~0, TYPE_DIRENTRY, 3);
984 reiserfs_log(LOG_DEBUG, "search_by_entry_key for "
985 "key(objectid=%d,dirid=%d)\n",
986 key.on_disk_key.k_objectid, key.on_disk_key.k_dir_id);
987 retval = search_by_entry_key(sbi, &key, &path, &de);
988 if (retval == IO_ERROR) {
992 if (retval == NAME_NOT_FOUND)
995 reiserfs_log(LOG_DEBUG, "name found\n");
997 set_de_name_and_namelen(&de);
999 if (deh_offset(&(de.de_deh[de.de_entry_num])) == DOT_DOT_OFFSET) {
1000 /* Allow override in this case */
1001 if (reiserfs_rupasov_hash(sbi)) {
1004 reiserfs_log(LOG_DEBUG,
1005 "FS seems to be empty, autodetect "
1006 "is using the default hash");
1010 r5hash = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen));
1011 teahash = GET_HASH_VALUE(keyed_hash(de.de_name,
1013 yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen));
1014 if (((teahash == r5hash) &&
1016 deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash)) ||
1017 ((teahash == yurahash) &&
1020 deh_offset(&(de.de_deh[de.de_entry_num]))))) ||
1021 ((r5hash == yurahash) &&
1024 deh_offset(&(de.de_deh[de.de_entry_num])))))) {
1025 reiserfs_log(LOG_ERR,
1026 "unable to automatically detect hash "
1027 "function. Please mount with -o "
1028 "hash={tea,rupasov,r5}");
1034 deh_offset(&(de.de_deh[de.de_entry_num]))) == yurahash) {
1035 reiserfs_log(LOG_DEBUG, "detected YURA hash\n");
1037 } else if (GET_HASH_VALUE(
1038 deh_offset(&(de.de_deh[de.de_entry_num]))) == teahash) {
1039 reiserfs_log(LOG_DEBUG, "detected TEA hash\n");
1041 } else if (GET_HASH_VALUE(
1042 deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash) {
1043 reiserfs_log(LOG_DEBUG, "detected R5 hash\n");
1046 reiserfs_log(LOG_WARNING, "unrecognised hash function");
1055 /* Finds out which hash names are sorted with */
1057 what_hash(struct reiserfs_mount *rmp)
1060 struct reiserfs_sb_info *sbi = rmp->rm_reiserfs;
1063 code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(sbi));
1066 * reiserfs_hash_detect() == true if any of the hash mount options
1067 * were used. We must check them to make sure the user isn't using a
1070 if (code == UNSET_HASH || reiserfs_hash_detect(sbi))
1071 code = find_hash_out(rmp);
1073 if (code != UNSET_HASH && reiserfs_hash_detect(sbi)) {
1075 * Detection has found the hash, and we must check against
1078 if (reiserfs_rupasov_hash(sbi) && code != YURA_HASH) {
1079 reiserfs_log(LOG_ERR, "error, %s hash detected, "
1080 "unable to force rupasov hash",
1081 reiserfs_hashname(code));
1083 } else if (reiserfs_tea_hash(sbi) && code != TEA_HASH) {
1084 reiserfs_log(LOG_ERR, "error, %s hash detected, "
1085 "unable to force tea hash",
1086 reiserfs_hashname(code));
1088 } else if (reiserfs_r5_hash(sbi) && code != R5_HASH) {
1089 reiserfs_log(LOG_ERR, "error, %s hash detected, "
1090 "unable to force r5 hash",
1091 reiserfs_hashname(code));
1096 * Find_hash_out was not called or could not determine
1099 if (reiserfs_rupasov_hash(sbi)) {
1101 } else if (reiserfs_tea_hash(sbi)) {
1103 } else if (reiserfs_r5_hash(sbi)) {
1108 /* TODO Not supported yet */
1110 /* If we are mounted RW, and we have a new valid hash code, update
1112 if (code != UNSET_HASH &&
1113 !(s->s_flags & MS_RDONLY) &&
1114 code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1115 set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1122 /* Return pointer to appropriate function */
1124 hash_function(struct reiserfs_mount *rmp)
1127 switch (what_hash(rmp)) {
1129 reiserfs_log(LOG_INFO, "using tea hash to sort names\n");
1130 return (keyed_hash);
1132 reiserfs_log(LOG_INFO, "using rupasov hash to sort names\n");
1135 reiserfs_log(LOG_INFO, "using r5 hash to sort names\n");
1142 /* -------------------------------------------------------------------
1144 * -------------------------------------------------------------------*/
1146 static struct vfsops reiser_vfsops = {
1147 .vfs_cmount = reiserfs_cmount,
1148 .vfs_mount = reiserfs_mount,
1149 .vfs_unmount = reiserfs_unmount,
1150 //.vfs_checkexp = reiserfs_checkexp,
1151 //.vfs_extattrctl = reiserfs_extattrctl,
1152 .vfs_fhtovp = reiserfs_fhtovp,
1153 //.vfs_quotactl = reiserfs_quotactl,
1154 .vfs_root = reiserfs_root,
1155 //.vfs_start = reiserfs_start,
1156 .vfs_statfs = reiserfs_statfs,
1157 //.vfs_sync = reiserfs_sync,
1158 //.vfs_vget = reiserfs_vget,
1161 VFS_SET(reiser_vfsops, reiserfs, VFCF_READONLY);