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;
29 static vfs_vptofh_t reiserfs_vptofh;
31 static int reiserfs_mountfs(struct vnode *devvp, struct mount *mp,
33 static void load_bitmap_info_data(struct reiserfs_sb_info *sbi,
34 struct reiserfs_bitmap_info *bi);
35 static int read_bitmaps(struct reiserfs_mount *rmp);
36 static int read_old_bitmaps(struct reiserfs_mount *rmp);
37 static int read_super_block(struct reiserfs_mount *rmp, int offset);
38 static hashf_t hash_function(struct reiserfs_mount *rmp);
40 static int get_root_node(struct reiserfs_mount *rmp,
41 struct reiserfs_node **root);
42 uint32_t find_hash_out(struct reiserfs_mount *rmp);
44 MALLOC_DEFINE(M_REISERFSMNT, "ReiserFS mount", "ReiserFS mount structure");
45 MALLOC_DEFINE(M_REISERFSPATH, "ReiserFS path", "ReiserFS path structure");
46 MALLOC_DEFINE(M_REISERFSNODE, "ReiserFS node", "ReiserFS vnode private part");
48 /* -------------------------------------------------------------------
50 * -------------------------------------------------------------------*/
53 reiserfs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
55 struct reiserfs_args args;
58 error = copyin(data, &args, sizeof(args));
62 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
63 ma = mount_arg(ma, "export", &args.export, sizeof args.export);
65 error = kernel_mount(ma, flags);
74 reiserfs_mount(struct mount *mp, struct thread *td)
81 struct vfsoptlist *opts;
82 struct reiserfs_mount *rmp;
83 struct reiserfs_sb_info *sbi;
84 struct nameidata nd, *ndp = &nd;
86 if (!(mp->mnt_flag & MNT_RDONLY))
89 /* Get the new options passed to mount */
90 opts = mp->mnt_optnew;
92 /* `fspath' contains the mount point (eg. /mnt/linux); REQUIRED */
93 vfs_getopt(opts, "fspath", (void **)&path, NULL);
94 reiserfs_log(LOG_INFO, "mount point is `%s'\n", path);
96 /* `from' contains the device name (eg. /dev/ad0s1); REQUIRED */
98 error = vfs_getopt(opts, "from", (void **)&fspec, &len);
99 if (!error && fspec[len - 1] != '\0')
101 reiserfs_log(LOG_INFO, "device is `%s'\n", fspec);
103 /* Handle MNT_UPDATE (mp->mnt_flag) */
104 if (mp->mnt_flag & MNT_UPDATE) {
105 /* For now, only NFS export is supported. */
106 if (vfs_flagopt(opts, "export", NULL, 0))
110 /* Not an update, or updating the name: look up the name
111 * and verify that it refers to a sensible disk device. */
115 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
116 if ((error = namei(ndp)) != 0)
118 NDFREE(ndp, NDF_ONLY_PNBUF);
121 if (!vn_isdisk(devvp, &error)) {
126 /* If mount by non-root, then verify that user has necessary
127 * permissions on the device. */
130 if ((mp->mnt_flag & MNT_RDONLY) == 0)
131 accessmode |= VWRITE;
132 if ((error = VOP_ACCESS(devvp,
133 accessmode, td->td_ucred, td)) != 0) {
139 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
140 error = reiserfs_mountfs(devvp, mp, td);
142 /* TODO Handle MNT_UPDATE */
152 rmp = VFSTOREISERFS(mp);
153 sbi = rmp->rm_reiserfs;
156 * Note that this strncpy() is ok because of a check at the start
157 * of reiserfs_mount().
159 reiserfs_log(LOG_DEBUG, "prepare statfs data\n");
160 (void)copystr(fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
161 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
162 (void)reiserfs_statfs(mp, &mp->mnt_stat, td);
164 reiserfs_log(LOG_DEBUG, "done\n");
169 * Unmount system call
172 reiserfs_unmount(struct mount *mp, int mntflags, struct thread *td)
174 int error, flags = 0;
175 struct reiserfs_mount *rmp;
176 struct reiserfs_sb_info *sbi;
178 reiserfs_log(LOG_DEBUG, "get private data\n");
179 rmp = VFSTOREISERFS(mp);
180 sbi = rmp->rm_reiserfs;
182 /* Flangs handling */
183 reiserfs_log(LOG_DEBUG, "handle mntflags\n");
184 if (mntflags & MNT_FORCE)
187 /* Flush files -> vflush */
188 reiserfs_log(LOG_DEBUG, "flush vnodes\n");
189 if ((error = vflush(mp, 0, flags, td)))
192 /* XXX Super block update */
195 if (SB_AP_BITMAP(sbi)) {
197 reiserfs_log(LOG_DEBUG,
198 "release bitmap buffers (total: %d)\n",
200 for (i = 0; i < SB_BMAP_NR(sbi); i++) {
201 if (SB_AP_BITMAP(sbi)[i].bp_data) {
202 free(SB_AP_BITMAP(sbi)[i].bp_data,
204 SB_AP_BITMAP(sbi)[i].bp_data = NULL;
208 reiserfs_log(LOG_DEBUG, "free bitmaps structure\n");
209 free(SB_AP_BITMAP(sbi), M_REISERFSMNT);
210 SB_AP_BITMAP(sbi) = NULL;
214 reiserfs_log(LOG_DEBUG, "free super block data\n");
215 free(sbi->s_rs, M_REISERFSMNT);
220 reiserfs_log(LOG_DEBUG, "close device\n");
221 #if defined(si_mountpoint)
222 rmp->rm_devvp->v_rdev->si_mountpoint = NULL;
227 g_wither_geom_close(rmp->rm_cp->geom, ENXIO);
230 vrele(rmp->rm_devvp);
233 reiserfs_log(LOG_DEBUG, "free sbi\n");
234 free(sbi, M_REISERFSMNT);
235 sbi = rmp->rm_reiserfs = NULL;
238 reiserfs_log(LOG_DEBUG, "free rmp\n");
239 free(rmp, M_REISERFSMNT);
243 mp->mnt_data = (qaddr_t)0;
245 mp->mnt_flag &= ~MNT_LOCAL;
248 reiserfs_log(LOG_DEBUG, "done\n");
253 * Return the root of a filesystem.
256 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, td);
277 reiserfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
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");
381 * Vnode pointer to File handle
384 reiserfs_vptofh(struct vnode *vp, struct fid *fhp)
387 struct reiserfs_node *ip;
390 reiserfs_log(LOG_DEBUG,
391 "fill *fhp with inode (dirid=%d, objectid=%d)\n",
392 ip->i_ino, ip->i_number);
394 rfhp = (struct rfid *)fhp;
395 rfhp->rfid_len = sizeof(struct rfid);
396 rfhp->rfid_dirid = ip->i_ino;
397 rfhp->rfid_objectid = ip->i_number;
398 rfhp->rfid_gen = ip->i_generation;
400 reiserfs_log(LOG_DEBUG, "return it\n");
404 /* -------------------------------------------------------------------
405 * Functions for the journal
406 * -------------------------------------------------------------------*/
409 is_reiserfs_3_5(struct reiserfs_super_block *rs)
412 return (!strncmp(rs->s_v1.s_magic, reiserfs_3_5_magic_string,
413 strlen(reiserfs_3_5_magic_string)));
417 is_reiserfs_3_6(struct reiserfs_super_block *rs)
420 return (!strncmp(rs->s_v1.s_magic, reiserfs_3_6_magic_string,
421 strlen(reiserfs_3_6_magic_string)));
425 is_reiserfs_jr(struct reiserfs_super_block *rs)
428 return (!strncmp(rs->s_v1.s_magic, reiserfs_jr_magic_string,
429 strlen(reiserfs_jr_magic_string)));
433 is_any_reiserfs_magic_string(struct reiserfs_super_block *rs)
436 return ((is_reiserfs_3_5(rs) || is_reiserfs_3_6(rs) ||
437 is_reiserfs_jr(rs)));
440 /* -------------------------------------------------------------------
442 * -------------------------------------------------------------------*/
445 * Common code for mount and mountroot
448 reiserfs_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td)
450 int error, old_format = 0;
451 struct reiserfs_mount *rmp;
452 struct reiserfs_sb_info *sbi;
453 struct reiserfs_super_block *rs;
454 struct cdev *dev = devvp->v_rdev;
456 #if (__FreeBSD_version >= 600000)
457 struct g_consumer *cp;
461 //ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
463 #if (__FreeBSD_version < 600000)
465 * Disallow multiple mounts of the same device.
466 * Disallow mounting of a device that is currently in use
467 * (except for root, which might share swap device for miniroot).
468 * Flush out any old buffers remaining from a previous use.
470 if ((error = vfs_mountedon(devvp)) != 0)
472 if (vcount(devvp) > 1)
475 error = vinvalbuf(devvp, V_SAVE, td->td_ucred, td, 0, 0);
477 VOP_UNLOCK(devvp, 0, td);
482 * Open the device in read-only, 'cause we don't support write
485 error = VOP_OPEN(devvp, FREAD, FSCRED, td, -1);
486 VOP_UNLOCK(devvp, 0, td);
492 error = g_vfs_open(devvp, &cp, "reiserfs", /* read-only */ 0);
495 VOP_UNLOCK(devvp, 0, td);
499 bo = &devvp->v_bufobj;
501 bo->bo_ops = g_vfs_bufops;
504 if (devvp->v_rdev->si_iosize_max != 0)
505 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
506 if (mp->mnt_iosize_max > MAXPHYS)
507 mp->mnt_iosize_max = MAXPHYS;
512 /* rmp contains any information about this specific mount */
513 rmp = malloc(sizeof *rmp, M_REISERFSMNT, M_WAITOK | M_ZERO);
518 sbi = malloc(sizeof *sbi, M_REISERFSMNT, M_WAITOK | M_ZERO);
523 rmp->rm_reiserfs = sbi;
525 rmp->rm_devvp = devvp;
527 #if (__FreeBSD_version >= 600000)
528 rmp->rm_bo = &devvp->v_bufobj;
532 /* Set default values for options: non-aggressive tails */
533 REISERFS_SB(sbi)->s_mount_opt = (1 << REISERFS_SMALLTAIL);
534 REISERFS_SB(sbi)->s_rd_only = 1;
535 REISERFS_SB(sbi)->s_devvp = devvp;
537 /* Read the super block */
538 if ((error = read_super_block(rmp, REISERFS_OLD_DISK_OFFSET)) == 0) {
539 /* The read process succeeded, it's an old format */
541 } else if ((error = read_super_block(rmp, REISERFS_DISK_OFFSET)) != 0) {
542 reiserfs_log(LOG_ERR, "can not find a ReiserFS filesystem\n");
546 rs = SB_DISK_SUPER_BLOCK(sbi);
549 * Let's do basic sanity check to verify that underlying device is
550 * not smaller than the filesystem. If the check fails then abort and
551 * scream, because bad stuff will happen otherwise.
554 if (s->s_bdev && s->s_bdev->bd_inode &&
555 i_size_read(s->s_bdev->bd_inode) <
556 sb_block_count(rs) * sb_blocksize(rs)) {
557 reiserfs_log(LOG_ERR,
558 "reiserfs: filesystem cannot be mounted because it is "
559 "bigger than the device.\n");
560 reiserfs_log(LOG_ERR, "reiserfs: you may need to run fsck "
561 "rr may be you forgot to reboot after fdisk when it "
568 * XXX This is from the original Linux code, but why affecting 2 values
569 * to the same variable?
571 sbi->s_mount_state = SB_REISERFS_STATE(sbi);
572 sbi->s_mount_state = REISERFS_VALID_FS;
574 if ((error = (old_format ?
575 read_old_bitmaps(rmp) : read_bitmaps(rmp)))) {
576 reiserfs_log(LOG_ERR, "unable to read bitmap\n");
580 /* Make data=ordered the default */
581 if (!reiserfs_data_log(sbi) && !reiserfs_data_ordered(sbi) &&
582 !reiserfs_data_writeback(sbi)) {
583 REISERFS_SB(sbi)->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
586 if (reiserfs_data_log(sbi)) {
587 reiserfs_log(LOG_INFO, "using journaled data mode\n");
588 } else if (reiserfs_data_ordered(sbi)) {
589 reiserfs_log(LOG_INFO, "using ordered data mode\n");
591 reiserfs_log(LOG_INFO, "using writeback data mode\n");
594 /* TODO Not yet supported */
596 if(journal_init(sbi, jdev_name, old_format, commit_max_age)) {
597 reiserfs_log(LOG_ERR, "unable to initialize journal space\n");
600 jinit_done = 1 ; /* once this is set, journal_release must
601 be called if we error out of the mount */
604 if (reread_meta_blocks(sbi)) {
605 reiserfs_log(LOG_ERR,
606 "unable to reread meta blocks after journal init\n");
611 /* Define and initialize hash function */
612 sbi->s_hash_function = hash_function(rmp);
614 if (sbi->s_hash_function == NULL) {
615 reiserfs_log(LOG_ERR, "couldn't determined hash function\n");
620 if (is_reiserfs_3_5(rs) ||
621 (is_reiserfs_jr(rs) && SB_VERSION(sbi) == REISERFS_VERSION_1))
622 bit_set(&(sbi->s_properties), REISERFS_3_5);
624 bit_set(&(sbi->s_properties), REISERFS_3_6);
626 mp->mnt_data = (qaddr_t)rmp;
627 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
628 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
630 mp->mnt_flag |= MNT_LOCAL;
632 #if defined(si_mountpoint)
633 devvp->v_rdev->si_mountpoint = mp;
639 reiserfs_log(LOG_INFO, "*** error during mount ***\n");
641 if (SB_AP_BITMAP(sbi)) {
643 for (i = 0; i < SB_BMAP_NR(sbi); i++) {
644 if (!SB_AP_BITMAP(sbi)[i].bp_data)
646 free(SB_AP_BITMAP(sbi)[i].bp_data, M_REISERFSMNT);
648 free(SB_AP_BITMAP(sbi), M_REISERFSMNT);
652 free(sbi->s_rs, M_REISERFSMNT);
657 #if (__FreeBSD_version < 600000)
658 (void)VOP_CLOSE(devvp, FREAD, NOCRED, td);
663 g_wither_geom_close(cp->geom, ENXIO);
670 free(sbi, M_REISERFSMNT);
672 free(rmp, M_REISERFSMNT);
677 * Read the super block
680 read_super_block(struct reiserfs_mount *rmp, int offset)
684 struct reiserfs_super_block *rs;
685 struct reiserfs_sb_info *sbi;
686 uint16_t fs_blocksize;
688 if (offset == REISERFS_OLD_DISK_OFFSET) {
689 reiserfs_log(LOG_DEBUG,
690 "reiserfs/super: read old format super block\n");
692 reiserfs_log(LOG_DEBUG,
693 "reiserfs/super: read new format super block\n");
696 /* Read the super block */
697 if ((error = bread(rmp->rm_devvp, offset * btodb(REISERFS_BSIZE),
698 REISERFS_BSIZE, NOCRED, &bp)) != 0) {
699 reiserfs_log(LOG_ERR, "can't read device\n");
703 /* Get it from the buffer data */
704 rs = (struct reiserfs_super_block *)bp->b_data;
705 if (!is_any_reiserfs_magic_string(rs)) {
710 fs_blocksize = sb_blocksize(rs);
714 if (fs_blocksize <= 0) {
715 reiserfs_log(LOG_ERR, "unexpected null block size");
719 /* Read the super block (for double check)
720 * We can't read the same blkno with a different size: it causes
721 * panic() if INVARIANTS is set. So we keep REISERFS_BSIZE */
722 if ((error = bread(rmp->rm_devvp,
723 offset * REISERFS_BSIZE / fs_blocksize * btodb(fs_blocksize),
724 REISERFS_BSIZE, NOCRED, &bp)) != 0) {
725 reiserfs_log(LOG_ERR, "can't reread the super block\n");
729 rs = (struct reiserfs_super_block *)bp->b_data;
730 if (sb_blocksize(rs) != fs_blocksize) {
731 reiserfs_log(LOG_ERR, "unexpected block size "
732 "(found=%u, expected=%u)\n",
733 sb_blocksize(rs), fs_blocksize);
738 reiserfs_log(LOG_DEBUG, "magic: `%s'\n", rs->s_v1.s_magic);
739 reiserfs_log(LOG_DEBUG, "label: `%s'\n", rs->s_label);
740 reiserfs_log(LOG_DEBUG, "block size: %6d\n", sb_blocksize(rs));
741 reiserfs_log(LOG_DEBUG, "block count: %6u\n",
742 rs->s_v1.s_block_count);
743 reiserfs_log(LOG_DEBUG, "bitmaps number: %6u\n",
746 if (rs->s_v1.s_root_block == -1) {
748 "reiserfs: Unfinished reiserfsck --rebuild-tree run "
750 "run reiserfsck --rebuild-tree and wait for a "
751 "completion. If that\n"
752 "fails, get newer reiserfsprogs package");
757 sbi = rmp->rm_reiserfs;
758 sbi->s_blocksize = fs_blocksize;
760 for (bits = 9, fs_blocksize >>= 9; fs_blocksize >>= 1; bits++)
762 sbi->s_blocksize_bits = bits;
764 /* Copy the buffer and release it */
765 sbi->s_rs = malloc(sizeof *rs, M_REISERFSMNT, M_WAITOK | M_ZERO);
767 reiserfs_log(LOG_ERR, "can not read the super block\n");
771 bcopy(rs, sbi->s_rs, sizeof(struct reiserfs_super_block));
774 if (is_reiserfs_jr(rs)) {
775 if (sb_version(rs) == REISERFS_VERSION_2)
776 reiserfs_log(LOG_INFO, "found reiserfs format \"3.6\""
777 " with non-standard journal");
778 else if (sb_version(rs) == REISERFS_VERSION_1)
779 reiserfs_log(LOG_INFO, "found reiserfs format \"3.5\""
780 " with non-standard journal");
782 reiserfs_log(LOG_ERR, "found unknown "
783 "format \"%u\" of reiserfs with non-standard magic",
789 * s_version of standard format may contain incorrect
790 * information, so we just look at the magic string
792 reiserfs_log(LOG_INFO,
793 "found reiserfs format \"%s\" with standard journal\n",
794 is_reiserfs_3_5(rs) ? "3.5" : "3.6");
801 * load_bitmap_info_data - Sets up the reiserfs_bitmap_info structure
803 * @sbi - superblock info for this filesystem
804 * @bi - the bitmap info to be loaded. Requires that bi->bp is valid.
806 * This routine counts how many free bits there are, finding the first
807 * zero as a side effect. Could also be implemented as a loop of
808 * test_bit() calls, or a loop of find_first_zero_bit() calls. This
809 * implementation is similar to find_first_zero_bit(), but doesn't
810 * return after it finds the first bit. Should only be called on fs
811 * mount, but should be fairly efficient anyways.
813 * bi->first_zero_hint is considered unset if it == 0, since the bitmap
814 * itself will invariably occupt block 0 represented in the bitmap. The
815 * only exception to this is when free_count also == 0, since there will
816 * be no free blocks at all.
819 load_bitmap_info_data(struct reiserfs_sb_info *sbi,
820 struct reiserfs_bitmap_info *bi)
824 cur = (unsigned long *)bi->bp_data;
825 while ((char *)cur < (bi->bp_data + sbi->s_blocksize)) {
827 * No need to scan if all 0's or all 1's.
828 * Since we're only counting 0's, we can simply ignore
832 if (bi->first_zero_hint == 0) {
833 bi->first_zero_hint =
834 ((char *)cur - bi->bp_data) << 3;
836 bi->free_count += sizeof(unsigned long) * 8;
837 } else if (*cur != ~0L) {
840 for (b = 0; b < sizeof(unsigned long) * 8; b++) {
841 if (!reiserfs_test_le_bit(b, cur)) {
843 if (bi->first_zero_hint == 0)
844 bi->first_zero_hint =
846 bi->bp_data) << 3) + b;
858 read_bitmaps(struct reiserfs_mount *rmp)
861 struct buf *bp = NULL;
862 struct reiserfs_sb_info *sbi = rmp->rm_reiserfs;
864 /* Allocate memory for the table of bitmaps */
866 malloc(sizeof(struct reiserfs_bitmap_info) * SB_BMAP_NR(sbi),
867 M_REISERFSMNT, M_WAITOK | M_ZERO);
868 if (!SB_AP_BITMAP(sbi))
871 /* Read all the bitmaps */
873 bmap_nr = (REISERFS_DISK_OFFSET_IN_BYTES / sbi->s_blocksize + 1) *
874 btodb(sbi->s_blocksize);
875 i < SB_BMAP_NR(sbi); i++, bmap_nr = sbi->s_blocksize * 8 * i) {
876 SB_AP_BITMAP(sbi)[i].bp_data = malloc(sbi->s_blocksize,
877 M_REISERFSMNT, M_WAITOK | M_ZERO);
878 if (!SB_AP_BITMAP(sbi)[i].bp_data)
880 bread(rmp->rm_devvp, bmap_nr, sbi->s_blocksize, NOCRED, &bp);
881 bcopy(bp->b_data, SB_AP_BITMAP(sbi)[i].bp_data,
886 /*if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh))
887 ll_rw_block(READ, 1, &SB_AP_BITMAP(s)[i].bh);*/
890 for (i = 0; i < SB_BMAP_NR(sbi); i++) {
891 /*if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) {
892 reiserfs_warning(s,"sh-2029: reiserfs read_bitmaps: "
893 "bitmap block (#%lu) reading failed",
894 SB_AP_BITMAP(s)[i].bh->b_blocknr);
895 for (i = 0; i < SB_BMAP_NR(s); i++)
896 brelse(SB_AP_BITMAP(s)[i].bh);
897 vfree(SB_AP_BITMAP(s));
898 SB_AP_BITMAP(s) = NULL;
901 load_bitmap_info_data(sbi, SB_AP_BITMAP(sbi) + i);
902 reiserfs_log(LOG_DEBUG,
903 "%d free blocks (starting at block %ld)\n",
904 SB_AP_BITMAP(sbi)[i].free_count,
905 (long)SB_AP_BITMAP(sbi)[i].first_zero_hint);
911 // TODO Not supported
913 read_old_bitmaps(struct reiserfs_mount *rmp)
919 struct reiserfs_sb_info *sbi = rmp->rm_reiserfs;
920 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(sbi);
922 /* First of bitmap blocks */
923 int bmp1 = (REISERFS_OLD_DISK_OFFSET / sbi->s_blocksize) *
924 btodb(sbi->s_blocksize);
926 /* Read true bitmap */
928 malloc(sizeof (struct reiserfs_buffer_info *) * sb_bmap_nr(rs),
929 M_REISERFSMNT, M_WAITOK | M_ZERO);
930 if (!SB_AP_BITMAP(sbi))
933 for (i = 0; i < sb_bmap_nr(rs); i ++) {
934 SB_AP_BITMAP(sbi)[i].bp = getblk(rmp->rm_devvp,
935 (bmp1 + i) * btodb(sbi->s_blocksize), sbi->s_blocksize, 0, 0, 0);
936 if (!SB_AP_BITMAP(sbi)[i].bp)
938 load_bitmap_info_data(sbi, SB_AP_BITMAP(sbi) + i);
945 /* -------------------------------------------------------------------
946 * Hash detection stuff
947 * -------------------------------------------------------------------*/
950 get_root_node(struct reiserfs_mount *rmp, struct reiserfs_node **root)
952 struct reiserfs_node *ip;
953 struct reiserfs_iget_args args;
955 /* Allocate the node structure */
956 reiserfs_log(LOG_DEBUG, "malloc(struct reiserfs_node)\n");
957 MALLOC(ip, struct reiserfs_node *, sizeof(struct reiserfs_node),
958 M_REISERFSNODE, M_WAITOK | M_ZERO);
960 /* Fill the structure */
961 reiserfs_log(LOG_DEBUG, "filling *ip\n");
962 ip->i_dev = rmp->rm_dev;
963 ip->i_number = REISERFS_ROOT_OBJECTID;
964 ip->i_ino = REISERFS_ROOT_PARENT_OBJECTID;
965 ip->i_reiserfs = rmp->rm_reiserfs;
968 args.objectid = ip->i_number;
969 args.dirid = ip->i_ino;
970 reiserfs_log(LOG_DEBUG, "call reiserfs_read_locked_inode("
971 "objectid=%d,dirid=%d)\n", args.objectid, args.dirid);
972 reiserfs_read_locked_inode(ip, &args);
974 ip->i_devvp = rmp->rm_devvp;
975 //XXX VREF(ip->i_devvp); Is it necessary ?
982 * If root directory is empty - we set default - Yura's - hash and warn
984 * FIXME: we look for only one name in a directory. If tea and yura both
985 * have the same value - we ask user to send report to the mailing list
987 uint32_t find_hash_out(struct reiserfs_mount *rmp)
991 INITIALIZE_PATH(path);
992 struct reiserfs_node *ip;
993 struct reiserfs_sb_info *sbi;
994 struct reiserfs_dir_entry de;
995 uint32_t hash = DEFAULT_HASH;
997 get_root_node(rmp, &ip);
1001 sbi = rmp->rm_reiserfs;
1004 uint32_t teahash, r5hash, yurahash;
1006 reiserfs_log(LOG_DEBUG, "make_cpu_key\n");
1007 make_cpu_key(&key, ip, ~0, TYPE_DIRENTRY, 3);
1008 reiserfs_log(LOG_DEBUG, "search_by_entry_key for "
1009 "key(objectid=%d,dirid=%d)\n",
1010 key.on_disk_key.k_objectid, key.on_disk_key.k_dir_id);
1011 retval = search_by_entry_key(sbi, &key, &path, &de);
1012 if (retval == IO_ERROR) {
1014 return (UNSET_HASH);
1016 if (retval == NAME_NOT_FOUND)
1019 reiserfs_log(LOG_DEBUG, "name found\n");
1021 set_de_name_and_namelen(&de);
1023 if (deh_offset(&(de.de_deh[de.de_entry_num])) == DOT_DOT_OFFSET) {
1024 /* Allow override in this case */
1025 if (reiserfs_rupasov_hash(sbi)) {
1028 reiserfs_log(LOG_DEBUG,
1029 "FS seems to be empty, autodetect "
1030 "is using the default hash");
1034 r5hash = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen));
1035 teahash = GET_HASH_VALUE(keyed_hash(de.de_name,
1037 yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen));
1038 if (((teahash == r5hash) &&
1040 deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash)) ||
1041 ((teahash == yurahash) &&
1044 deh_offset(&(de.de_deh[de.de_entry_num]))))) ||
1045 ((r5hash == yurahash) &&
1048 deh_offset(&(de.de_deh[de.de_entry_num])))))) {
1049 reiserfs_log(LOG_ERR,
1050 "unable to automatically detect hash "
1051 "function. Please mount with -o "
1052 "hash={tea,rupasov,r5}");
1058 deh_offset(&(de.de_deh[de.de_entry_num]))) == yurahash) {
1059 reiserfs_log(LOG_DEBUG, "detected YURA hash\n");
1061 } else if (GET_HASH_VALUE(
1062 deh_offset(&(de.de_deh[de.de_entry_num]))) == teahash) {
1063 reiserfs_log(LOG_DEBUG, "detected TEA hash\n");
1065 } else if (GET_HASH_VALUE(
1066 deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash) {
1067 reiserfs_log(LOG_DEBUG, "detected R5 hash\n");
1070 reiserfs_log(LOG_WARNING, "unrecognised hash function");
1079 /* Finds out which hash names are sorted with */
1081 what_hash(struct reiserfs_mount *rmp)
1084 struct reiserfs_sb_info *sbi = rmp->rm_reiserfs;
1087 code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(sbi));
1090 * reiserfs_hash_detect() == true if any of the hash mount options
1091 * were used. We must check them to make sure the user isn't using a
1094 if (code == UNSET_HASH || reiserfs_hash_detect(sbi))
1095 code = find_hash_out(rmp);
1097 if (code != UNSET_HASH && reiserfs_hash_detect(sbi)) {
1099 * Detection has found the hash, and we must check against
1102 if (reiserfs_rupasov_hash(sbi) && code != YURA_HASH) {
1103 reiserfs_log(LOG_ERR, "error, %s hash detected, "
1104 "unable to force rupasov hash",
1105 reiserfs_hashname(code));
1107 } else if (reiserfs_tea_hash(sbi) && code != TEA_HASH) {
1108 reiserfs_log(LOG_ERR, "error, %s hash detected, "
1109 "unable to force tea hash",
1110 reiserfs_hashname(code));
1112 } else if (reiserfs_r5_hash(sbi) && code != R5_HASH) {
1113 reiserfs_log(LOG_ERR, "error, %s hash detected, "
1114 "unable to force r5 hash",
1115 reiserfs_hashname(code));
1120 * Find_hash_out was not called or could not determine
1123 if (reiserfs_rupasov_hash(sbi)) {
1125 } else if (reiserfs_tea_hash(sbi)) {
1127 } else if (reiserfs_r5_hash(sbi)) {
1132 /* TODO Not supported yet */
1134 /* If we are mounted RW, and we have a new valid hash code, update
1136 if (code != UNSET_HASH &&
1137 !(s->s_flags & MS_RDONLY) &&
1138 code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1139 set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1146 /* Return pointer to appropriate function */
1148 hash_function(struct reiserfs_mount *rmp)
1151 switch (what_hash(rmp)) {
1153 reiserfs_log(LOG_INFO, "using tea hash to sort names\n");
1154 return (keyed_hash);
1156 reiserfs_log(LOG_INFO, "using rupasov hash to sort names\n");
1159 reiserfs_log(LOG_INFO, "using r5 hash to sort names\n");
1166 /* -------------------------------------------------------------------
1168 * -------------------------------------------------------------------*/
1170 static struct vfsops reiser_vfsops = {
1171 .vfs_cmount = reiserfs_cmount,
1172 .vfs_mount = reiserfs_mount,
1173 .vfs_unmount = reiserfs_unmount,
1174 //.vfs_checkexp = reiserfs_checkexp,
1175 //.vfs_extattrctl = reiserfs_extattrctl,
1176 .vfs_fhtovp = reiserfs_fhtovp,
1177 //.vfs_quotactl = reiserfs_quotactl,
1178 .vfs_root = reiserfs_root,
1179 //.vfs_start = reiserfs_start,
1180 .vfs_statfs = reiserfs_statfs,
1181 //.vfs_sync = reiserfs_sync,
1182 //.vfs_vget = reiserfs_vget,
1183 .vfs_vptofh = reiserfs_vptofh,
1186 VFS_SET(reiser_vfsops, reiserfs, VFCF_READONLY);