reiserfs_vfsops.c revision 149720
1/*- 2 * Copyright 2000 Hans Reiser 3 * See README for licensing and copyright details 4 * 5 * Ported to FreeBSD by Jean-S�bastien P�dron <jspedron@club-internet.fr> 6 * 7 * $FreeBSD: head/sys/gnu/fs/reiserfs/reiserfs_vfsops.c 149720 2005-09-02 15:27:23Z ssouhlal $ 8 */ 9 10#include <gnu/fs/reiserfs/reiserfs_fs.h> 11 12const char reiserfs_3_5_magic_string[] = REISERFS_SUPER_MAGIC_STRING; 13const char reiserfs_3_6_magic_string[] = REISER2FS_SUPER_MAGIC_STRING; 14const char reiserfs_jr_magic_string[] = REISER2FS_JR_SUPER_MAGIC_STRING; 15 16/* 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. 20 */ 21int reiserfs_default_io_size = 128 * 1024; 22 23static vfs_cmount_t reiserfs_cmount; 24static vfs_fhtovp_t reiserfs_fhtovp; 25static vfs_mount_t reiserfs_mount; 26static vfs_root_t reiserfs_root; 27static vfs_statfs_t reiserfs_statfs; 28static vfs_unmount_t reiserfs_unmount; 29static vfs_vptofh_t reiserfs_vptofh; 30 31static int reiserfs_mountfs(struct vnode *devvp, struct mount *mp, 32 struct thread *td); 33static void load_bitmap_info_data(struct reiserfs_sb_info *sbi, 34 struct reiserfs_bitmap_info *bi); 35static int read_bitmaps(struct reiserfs_mount *rmp); 36static int read_old_bitmaps(struct reiserfs_mount *rmp); 37static int read_super_block(struct reiserfs_mount *rmp, int offset); 38static hashf_t hash_function(struct reiserfs_mount *rmp); 39 40static int get_root_node(struct reiserfs_mount *rmp, 41 struct reiserfs_node **root); 42uint32_t find_hash_out(struct reiserfs_mount *rmp); 43 44MALLOC_DEFINE(M_REISERFSMNT, "ReiserFS mount", "ReiserFS mount structure"); 45MALLOC_DEFINE(M_REISERFSPATH, "ReiserFS path", "ReiserFS path structure"); 46MALLOC_DEFINE(M_REISERFSNODE, "ReiserFS node", "ReiserFS vnode private part"); 47 48/* ------------------------------------------------------------------- 49 * VFS operations 50 * -------------------------------------------------------------------*/ 51 52static int 53reiserfs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td) 54{ 55 struct reiserfs_args args; 56 int error; 57 58 error = copyin(data, &args, sizeof(args)); 59 if (error) 60 return (error); 61 62 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN); 63 ma = mount_arg(ma, "export", &args.export, sizeof args.export); 64 65 error = kernel_mount(ma, flags); 66 67 return (error); 68} 69 70/* 71 * Mount system call 72 */ 73static int 74reiserfs_mount(struct mount *mp, struct thread *td) 75{ 76 size_t size; 77 int error, len; 78 mode_t accessmode; 79 char *path, *fspec; 80 struct vnode *devvp; 81 struct vfsoptlist *opts; 82 struct export_args *export; 83 struct reiserfs_mount *rmp; 84 struct reiserfs_sb_info *sbi; 85 struct nameidata nd, *ndp = &nd; 86 87 if (!(mp->mnt_flag & MNT_RDONLY)) 88 return EROFS; 89 90 /* Get the new options passed to mount */ 91 opts = mp->mnt_optnew; 92 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); 96 97 /* `from' contains the device name (eg. /dev/ad0s1); REQUIRED */ 98 fspec = NULL; 99 error = vfs_getopt(opts, "from", (void **)&fspec, &len); 100 if (!error && fspec[len - 1] != '\0') 101 return (EINVAL); 102 reiserfs_log(LOG_INFO, "device is `%s'\n", fspec); 103 104 /* Handle MNT_UPDATE (mp->mnt_flag) */ 105 if (mp->mnt_flag & MNT_UPDATE) { 106 /* For now, only NFS export is supported. */ 107 error = vfs_getopt(opts, "export", (void **)&export, &len); 108 if (error == 0 && len == sizeof(*export) && export->ex_flags) 109 return (vfs_export(mp, export)); 110 } 111 112 /* Not an update, or updating the name: look up the name 113 * and verify that it refers to a sensible disk device. */ 114 if (fspec == NULL) 115 return (EINVAL); 116 117 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td); 118 if ((error = namei(ndp)) != 0) 119 return (error); 120 NDFREE(ndp, NDF_ONLY_PNBUF); 121 devvp = ndp->ni_vp; 122 123 if (!vn_isdisk(devvp, &error)) { 124 vput(devvp); 125 return (error); 126 } 127 128 /* If mount by non-root, then verify that user has necessary 129 * permissions on the device. */ 130 if (suser(td)) { 131 accessmode = VREAD; 132 if ((mp->mnt_flag & MNT_RDONLY) == 0) 133 accessmode |= VWRITE; 134 if ((error = VOP_ACCESS(devvp, 135 accessmode, td->td_ucred, td)) != 0) { 136 vput(devvp); 137 return (error); 138 } 139 } 140 141 if ((mp->mnt_flag & MNT_UPDATE) == 0) { 142 error = reiserfs_mountfs(devvp, mp, td); 143 } else { 144 /* TODO Handle MNT_UPDATE */ 145 error = (EOPNOTSUPP); 146 } 147 148 if (error) { 149 vrele(devvp); 150 return (error); 151 } 152 153 rmp = VFSTOREISERFS(mp); 154 sbi = rmp->rm_reiserfs; 155 156 /* 157 * Note that this strncpy() is ok because of a check at the start 158 * of reiserfs_mount(). 159 */ 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, td); 164 165 reiserfs_log(LOG_DEBUG, "done\n"); 166 return (0); 167} 168 169/* 170 * Unmount system call 171 */ 172static int 173reiserfs_unmount(struct mount *mp, int mntflags, struct thread *td) 174{ 175 int error, flags = 0; 176 struct reiserfs_mount *rmp; 177 struct reiserfs_sb_info *sbi; 178 179 reiserfs_log(LOG_DEBUG, "get private data\n"); 180 rmp = VFSTOREISERFS(mp); 181 sbi = rmp->rm_reiserfs; 182 183 /* Flangs handling */ 184 reiserfs_log(LOG_DEBUG, "handle mntflags\n"); 185 if (mntflags & MNT_FORCE) 186 flags |= FORCECLOSE; 187 188 /* Flush files -> vflush */ 189 reiserfs_log(LOG_DEBUG, "flush vnodes\n"); 190 if ((error = vflush(mp, 0, flags, td))) 191 return (error); 192 193 /* XXX Super block update */ 194 195 if (sbi) { 196 if (SB_AP_BITMAP(sbi)) { 197 int i; 198 reiserfs_log(LOG_DEBUG, 199 "release bitmap buffers (total: %d)\n", 200 SB_BMAP_NR(sbi)); 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, 204 M_REISERFSMNT); 205 SB_AP_BITMAP(sbi)[i].bp_data = NULL; 206 } 207 } 208 209 reiserfs_log(LOG_DEBUG, "free bitmaps structure\n"); 210 free(SB_AP_BITMAP(sbi), M_REISERFSMNT); 211 SB_AP_BITMAP(sbi) = NULL; 212 } 213 214 if (sbi->s_rs) { 215 reiserfs_log(LOG_DEBUG, "free super block data\n"); 216 free(sbi->s_rs, M_REISERFSMNT); 217 sbi->s_rs = NULL; 218 } 219 } 220 221 reiserfs_log(LOG_DEBUG, "close device\n"); 222#if defined(si_mountpoint) 223 rmp->rm_devvp->v_rdev->si_mountpoint = NULL; 224#endif 225 226 DROP_GIANT(); 227 g_topology_lock(); 228 g_wither_geom_close(rmp->rm_cp->geom, ENXIO); 229 g_topology_unlock(); 230 PICKUP_GIANT(); 231 vrele(rmp->rm_devvp); 232 233 if (sbi) { 234 reiserfs_log(LOG_DEBUG, "free sbi\n"); 235 free(sbi, M_REISERFSMNT); 236 sbi = rmp->rm_reiserfs = NULL; 237 } 238 if (rmp) { 239 reiserfs_log(LOG_DEBUG, "free rmp\n"); 240 free(rmp, M_REISERFSMNT); 241 rmp = NULL; 242 } 243 244 mp->mnt_data = (qaddr_t)0; 245 mp->mnt_flag &= ~MNT_LOCAL; 246 247 reiserfs_log(LOG_DEBUG, "done\n"); 248 return (error); 249} 250 251/* 252 * Return the root of a filesystem. 253 */ 254static int 255reiserfs_root(struct mount *mp, int flags, struct vnode **vpp, 256 struct thread *td) 257{ 258 int error; 259 struct vnode *vp; 260 struct cpu_key rootkey; 261 262 rootkey.on_disk_key.k_dir_id = REISERFS_ROOT_PARENT_OBJECTID; 263 rootkey.on_disk_key.k_objectid = REISERFS_ROOT_OBJECTID; 264 265 error = reiserfs_iget(mp, &rootkey, &vp, td); 266 267 if (error == 0) 268 *vpp = vp; 269 return (error); 270} 271 272/* 273 * The statfs syscall 274 */ 275static int 276reiserfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 277{ 278 struct reiserfs_mount *rmp; 279 struct reiserfs_sb_info *sbi; 280 struct reiserfs_super_block *rs; 281 282 reiserfs_log(LOG_DEBUG, "get private data\n"); 283 rmp = VFSTOREISERFS(mp); 284 sbi = rmp->rm_reiserfs; 285 rs = sbi->s_rs; 286 287 reiserfs_log(LOG_DEBUG, "fill statfs structure\n"); 288 sbp->f_bsize = sbi->s_blocksize; 289 sbp->f_iosize = sbp->f_bsize; 290 sbp->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1; 291 sbp->f_bfree = sb_free_blocks(rs); 292 sbp->f_bavail = sbp->f_bfree; 293 sbp->f_files = 0; 294 sbp->f_ffree = 0; 295 reiserfs_log(LOG_DEBUG, " block size = %ju\n", 296 (intmax_t)sbp->f_bsize); 297 reiserfs_log(LOG_DEBUG, " IO size = %ju\n", 298 (intmax_t)sbp->f_iosize); 299 reiserfs_log(LOG_DEBUG, " block count = %ju\n", 300 (intmax_t)sbp->f_blocks); 301 reiserfs_log(LOG_DEBUG, " free blocks = %ju\n", 302 (intmax_t)sbp->f_bfree); 303 reiserfs_log(LOG_DEBUG, " avail blocks = %ju\n", 304 (intmax_t)sbp->f_bavail); 305 reiserfs_log(LOG_DEBUG, "...done\n"); 306 307 if (sbp != &mp->mnt_stat) { 308 reiserfs_log(LOG_DEBUG, "copying monut point info\n"); 309 sbp->f_type = mp->mnt_vfc->vfc_typenum; 310 bcopy((caddr_t)mp->mnt_stat.f_mntonname, 311 (caddr_t)&sbp->f_mntonname[0], MNAMELEN); 312 bcopy((caddr_t)mp->mnt_stat.f_mntfromname, 313 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); 314 reiserfs_log(LOG_DEBUG, " mount from: %s\n", 315 sbp->f_mntfromname); 316 reiserfs_log(LOG_DEBUG, " mount on: %s\n", 317 sbp->f_mntonname); 318 reiserfs_log(LOG_DEBUG, "...done\n"); 319 } 320 321 return (0); 322} 323 324/* 325 * File handle to vnode 326 * 327 * Have to be really careful about stale file handles: 328 * - check that the inode key is valid 329 * - call ffs_vget() to get the locked inode 330 * - check for an unallocated inode (i_mode == 0) 331 * - check that the given client host has export rights and return 332 * those rights via. exflagsp and credanonp 333 */ 334static int 335reiserfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 336{ 337 int error; 338 struct rfid *rfhp; 339 struct vnode *nvp; 340 struct cpu_key key; 341 struct reiserfs_node *ip; 342 struct reiserfs_sb_info *sbi; 343 struct thread *td = curthread; 344 345 rfhp = (struct rfid *)fhp; 346 sbi = VFSTOREISERFS(mp)->rm_reiserfs; 347 348 /* Check that the key is valid */ 349 if (rfhp->rfid_dirid < REISERFS_ROOT_PARENT_OBJECTID && 350 rfhp->rfid_objectid < REISERFS_ROOT_OBJECTID) 351 return (ESTALE); 352 353 reiserfs_log(LOG_DEBUG, 354 "file handle key is (dirid=%d, objectid=%d)\n", 355 rfhp->rfid_dirid, rfhp->rfid_objectid); 356 key.on_disk_key.k_dir_id = rfhp->rfid_dirid; 357 key.on_disk_key.k_objectid = rfhp->rfid_objectid; 358 359 reiserfs_log(LOG_DEBUG, "read this inode\n"); 360 error = reiserfs_iget(mp, &key, &nvp, td); 361 if (error) { 362 *vpp = NULLVP; 363 return (error); 364 } 365 366 reiserfs_log(LOG_DEBUG, "check validity\n"); 367 ip = VTOI(nvp); 368 if (ip->i_mode == 0 || ip->i_generation != rfhp->rfid_gen) { 369 vput(nvp); 370 *vpp = NULLVP; 371 return (ESTALE); 372 } 373 374 reiserfs_log(LOG_DEBUG, "return it\n"); 375 *vpp = nvp; 376 return (0); 377} 378 379/* 380 * Vnode pointer to File handle 381 */ 382static int 383reiserfs_vptofh(struct vnode *vp, struct fid *fhp) 384{ 385 struct rfid *rfhp; 386 struct reiserfs_node *ip; 387 388 ip = VTOI(vp); 389 reiserfs_log(LOG_DEBUG, 390 "fill *fhp with inode (dirid=%d, objectid=%d)\n", 391 ip->i_ino, ip->i_number); 392 393 rfhp = (struct rfid *)fhp; 394 rfhp->rfid_len = sizeof(struct rfid); 395 rfhp->rfid_dirid = ip->i_ino; 396 rfhp->rfid_objectid = ip->i_number; 397 rfhp->rfid_gen = ip->i_generation; 398 399 reiserfs_log(LOG_DEBUG, "return it\n"); 400 return (0); 401} 402 403/* ------------------------------------------------------------------- 404 * Functions for the journal 405 * -------------------------------------------------------------------*/ 406 407int 408is_reiserfs_3_5(struct reiserfs_super_block *rs) 409{ 410 411 return (!strncmp(rs->s_v1.s_magic, reiserfs_3_5_magic_string, 412 strlen(reiserfs_3_5_magic_string))); 413} 414 415int 416is_reiserfs_3_6(struct reiserfs_super_block *rs) 417{ 418 419 return (!strncmp(rs->s_v1.s_magic, reiserfs_3_6_magic_string, 420 strlen(reiserfs_3_6_magic_string))); 421} 422 423int 424is_reiserfs_jr(struct reiserfs_super_block *rs) 425{ 426 427 return (!strncmp(rs->s_v1.s_magic, reiserfs_jr_magic_string, 428 strlen(reiserfs_jr_magic_string))); 429} 430 431static int 432is_any_reiserfs_magic_string(struct reiserfs_super_block *rs) 433{ 434 435 return ((is_reiserfs_3_5(rs) || is_reiserfs_3_6(rs) || 436 is_reiserfs_jr(rs))); 437} 438 439/* ------------------------------------------------------------------- 440 * Internal functions 441 * -------------------------------------------------------------------*/ 442 443/* 444 * Common code for mount and mountroot 445 */ 446static int 447reiserfs_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td) 448{ 449 int error, old_format = 0; 450 struct reiserfs_mount *rmp; 451 struct reiserfs_sb_info *sbi; 452 struct reiserfs_super_block *rs; 453 struct cdev *dev = devvp->v_rdev; 454 455#if (__FreeBSD_version >= 600000) 456 struct g_consumer *cp; 457 struct bufobj *bo; 458#endif 459 460 //ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 461 462#if (__FreeBSD_version < 600000) 463 /* 464 * Disallow multiple mounts of the same device. 465 * Disallow mounting of a device that is currently in use 466 * (except for root, which might share swap device for miniroot). 467 * Flush out any old buffers remaining from a previous use. 468 */ 469 if ((error = vfs_mountedon(devvp)) != 0) 470 return (error); 471 if (vcount(devvp) > 1) 472 return (EBUSY); 473 474 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 475 error = vinvalbuf(devvp, V_SAVE, td->td_ucred, td, 0, 0); 476 if (error) { 477 VOP_UNLOCK(devvp, 0, td); 478 return (error); 479 } 480 481 /* 482 * Open the device in read-only, 'cause we don't support write 483 * for now 484 */ 485 error = VOP_OPEN(devvp, FREAD, FSCRED, td, -1); 486 VOP_UNLOCK(devvp, 0, td); 487 if (error) 488 return (error); 489#else 490 DROP_GIANT(); 491 g_topology_lock(); 492 error = g_vfs_open(devvp, &cp, "reiserfs", /* read-only */ 0); 493 g_topology_unlock(); 494 PICKUP_GIANT(); 495 VOP_UNLOCK(devvp, 0, td); 496 if (error) 497 return (error); 498 499 bo = &devvp->v_bufobj; 500 bo->bo_private = cp; 501 bo->bo_ops = g_vfs_bufops; 502#endif 503 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; 508 509 rmp = NULL; 510 sbi = NULL; 511 512 /* rmp contains any information about this specific mount */ 513 rmp = malloc(sizeof *rmp, M_REISERFSMNT, M_WAITOK | M_ZERO); 514 if (!rmp) { 515 error = (ENOMEM); 516 goto out; 517 } 518 sbi = malloc(sizeof *sbi, M_REISERFSMNT, M_WAITOK | M_ZERO); 519 if (!sbi) { 520 error = (ENOMEM); 521 goto out; 522 } 523 rmp->rm_reiserfs = sbi; 524 rmp->rm_mountp = mp; 525 rmp->rm_devvp = devvp; 526 rmp->rm_dev = dev; 527#if (__FreeBSD_version >= 600000) 528 rmp->rm_bo = &devvp->v_bufobj; 529 rmp->rm_cp = cp; 530#endif 531 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; 536 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 */ 540 old_format = 1; 541 } else if ((error = read_super_block(rmp, REISERFS_DISK_OFFSET)) != 0) { 542 reiserfs_log(LOG_ERR, "can not find a ReiserFS filesystem\n"); 543 goto out; 544 } 545 546 rs = SB_DISK_SUPER_BLOCK(sbi); 547 548 /* 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. 552 */ 553#if 0 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 " 562 "told you to.\n"); 563 goto out; 564 } 565#endif 566 567 /* 568 * XXX This is from the original Linux code, but why affecting 2 values 569 * to the same variable? 570 */ 571 sbi->s_mount_state = SB_REISERFS_STATE(sbi); 572 sbi->s_mount_state = REISERFS_VALID_FS; 573 574 if ((error = (old_format ? 575 read_old_bitmaps(rmp) : read_bitmaps(rmp)))) { 576 reiserfs_log(LOG_ERR, "unable to read bitmap\n"); 577 goto out; 578 } 579 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); 584 } 585 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"); 590 } else { 591 reiserfs_log(LOG_INFO, "using writeback data mode\n"); 592 } 593 594 /* TODO Not yet supported */ 595#if 0 596 if(journal_init(sbi, jdev_name, old_format, commit_max_age)) { 597 reiserfs_log(LOG_ERR, "unable to initialize journal space\n"); 598 goto out; 599 } else { 600 jinit_done = 1 ; /* once this is set, journal_release must 601 be called if we error out of the mount */ 602 } 603 604 if (reread_meta_blocks(sbi)) { 605 reiserfs_log(LOG_ERR, 606 "unable to reread meta blocks after journal init\n"); 607 goto out; 608 } 609#endif 610 611 /* Define and initialize hash function */ 612 sbi->s_hash_function = hash_function(rmp); 613 614 if (sbi->s_hash_function == NULL) { 615 reiserfs_log(LOG_ERR, "couldn't determined hash function\n"); 616 error = (EINVAL); 617 goto out; 618 } 619 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); 623 else 624 bit_set(&(sbi->s_properties), REISERFS_3_6); 625 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; 629 mp->mnt_flag |= MNT_LOCAL; 630#if defined(si_mountpoint) 631 devvp->v_rdev->si_mountpoint = mp; 632#endif 633 634 return (0); 635 636out: 637 reiserfs_log(LOG_INFO, "*** error during mount ***\n"); 638 if (sbi) { 639 if (SB_AP_BITMAP(sbi)) { 640 int i; 641 for (i = 0; i < SB_BMAP_NR(sbi); i++) { 642 if (!SB_AP_BITMAP(sbi)[i].bp_data) 643 break; 644 free(SB_AP_BITMAP(sbi)[i].bp_data, M_REISERFSMNT); 645 } 646 free(SB_AP_BITMAP(sbi), M_REISERFSMNT); 647 } 648 649 if (sbi->s_rs) { 650 free(sbi->s_rs, M_REISERFSMNT); 651 sbi->s_rs = NULL; 652 } 653 } 654 655#if (__FreeBSD_version < 600000) 656 (void)VOP_CLOSE(devvp, FREAD, NOCRED, td); 657#else 658 if (cp != NULL) { 659 DROP_GIANT(); 660 g_topology_lock(); 661 g_wither_geom_close(cp->geom, ENXIO); 662 g_topology_unlock(); 663 PICKUP_GIANT(); 664 } 665#endif 666 667 if (sbi) 668 free(sbi, M_REISERFSMNT); 669 if (rmp) 670 free(rmp, M_REISERFSMNT); 671 return (error); 672} 673 674/* 675 * Read the super block 676 */ 677static int 678read_super_block(struct reiserfs_mount *rmp, int offset) 679{ 680 struct buf *bp; 681 int error, bits; 682 struct reiserfs_super_block *rs; 683 struct reiserfs_sb_info *sbi; 684 uint16_t fs_blocksize; 685 686 if (offset == REISERFS_OLD_DISK_OFFSET) { 687 reiserfs_log(LOG_DEBUG, 688 "reiserfs/super: read old format super block\n"); 689 } else { 690 reiserfs_log(LOG_DEBUG, 691 "reiserfs/super: read new format super block\n"); 692 } 693 694 /* Read the super block */ 695 if ((error = bread(rmp->rm_devvp, offset * btodb(REISERFS_BSIZE), 696 REISERFS_BSIZE, NOCRED, &bp)) != 0) { 697 reiserfs_log(LOG_ERR, "can't read device\n"); 698 return (error); 699 } 700 701 /* Get it from the buffer data */ 702 rs = (struct reiserfs_super_block *)bp->b_data; 703 if (!is_any_reiserfs_magic_string(rs)) { 704 brelse(bp); 705 return (EINVAL); 706 } 707 708 fs_blocksize = sb_blocksize(rs); 709 brelse(bp); 710 bp = NULL; 711 712 if (fs_blocksize <= 0) { 713 reiserfs_log(LOG_ERR, "unexpected null block size"); 714 return (EINVAL); 715 } 716 717 /* Read the super block (for double check) 718 * We can't read the same blkno with a different size: it causes 719 * panic() if INVARIANTS is set. So we keep REISERFS_BSIZE */ 720 if ((error = bread(rmp->rm_devvp, 721 offset * REISERFS_BSIZE / fs_blocksize * btodb(fs_blocksize), 722 REISERFS_BSIZE, NOCRED, &bp)) != 0) { 723 reiserfs_log(LOG_ERR, "can't reread the super block\n"); 724 return (error); 725 } 726 727 rs = (struct reiserfs_super_block *)bp->b_data; 728 if (sb_blocksize(rs) != fs_blocksize) { 729 reiserfs_log(LOG_ERR, "unexpected block size " 730 "(found=%u, expected=%u)\n", 731 sb_blocksize(rs), fs_blocksize); 732 brelse(bp); 733 return (EINVAL); 734 } 735 736 reiserfs_log(LOG_DEBUG, "magic: `%s'\n", rs->s_v1.s_magic); 737 reiserfs_log(LOG_DEBUG, "label: `%s'\n", rs->s_label); 738 reiserfs_log(LOG_DEBUG, "block size: %6d\n", sb_blocksize(rs)); 739 reiserfs_log(LOG_DEBUG, "block count: %6u\n", 740 rs->s_v1.s_block_count); 741 reiserfs_log(LOG_DEBUG, "bitmaps number: %6u\n", 742 rs->s_v1.s_bmap_nr); 743 744 if (rs->s_v1.s_root_block == -1) { 745 log(LOG_ERR, 746 "reiserfs: Unfinished reiserfsck --rebuild-tree run " 747 "detected. Please\n" 748 "run reiserfsck --rebuild-tree and wait for a " 749 "completion. If that\n" 750 "fails, get newer reiserfsprogs package"); 751 brelse(bp); 752 return (EINVAL); 753 } 754 755 sbi = rmp->rm_reiserfs; 756 sbi->s_blocksize = fs_blocksize; 757 758 for (bits = 9, fs_blocksize >>= 9; fs_blocksize >>= 1; bits++) 759 ; 760 sbi->s_blocksize_bits = bits; 761 762 /* Copy the buffer and release it */ 763 sbi->s_rs = malloc(sizeof *rs, M_REISERFSMNT, M_WAITOK | M_ZERO); 764 if (!sbi->s_rs) { 765 reiserfs_log(LOG_ERR, "can not read the super block\n"); 766 brelse(bp); 767 return (ENOMEM); 768 } 769 bcopy(rs, sbi->s_rs, sizeof(struct reiserfs_super_block)); 770 brelse(bp); 771 772 if (is_reiserfs_jr(rs)) { 773 if (sb_version(rs) == REISERFS_VERSION_2) 774 reiserfs_log(LOG_INFO, "found reiserfs format \"3.6\"" 775 " with non-standard journal"); 776 else if (sb_version(rs) == REISERFS_VERSION_1) 777 reiserfs_log(LOG_INFO, "found reiserfs format \"3.5\"" 778 " with non-standard journal"); 779 else { 780 reiserfs_log(LOG_ERR, "found unknown " 781 "format \"%u\" of reiserfs with non-standard magic", 782 sb_version(rs)); 783 return (EINVAL); 784 } 785 } else { 786 /* 787 * s_version of standard format may contain incorrect 788 * information, so we just look at the magic string 789 */ 790 reiserfs_log(LOG_INFO, 791 "found reiserfs format \"%s\" with standard journal\n", 792 is_reiserfs_3_5(rs) ? "3.5" : "3.6"); 793 } 794 795 return (0); 796} 797 798/* 799 * load_bitmap_info_data - Sets up the reiserfs_bitmap_info structure 800 * from disk. 801 * @sbi - superblock info for this filesystem 802 * @bi - the bitmap info to be loaded. Requires that bi->bp is valid. 803 * 804 * This routine counts how many free bits there are, finding the first 805 * zero as a side effect. Could also be implemented as a loop of 806 * test_bit() calls, or a loop of find_first_zero_bit() calls. This 807 * implementation is similar to find_first_zero_bit(), but doesn't 808 * return after it finds the first bit. Should only be called on fs 809 * mount, but should be fairly efficient anyways. 810 * 811 * bi->first_zero_hint is considered unset if it == 0, since the bitmap 812 * itself will invariably occupt block 0 represented in the bitmap. The 813 * only exception to this is when free_count also == 0, since there will 814 * be no free blocks at all. 815 */ 816static void 817load_bitmap_info_data(struct reiserfs_sb_info *sbi, 818 struct reiserfs_bitmap_info *bi) 819{ 820 unsigned long *cur; 821 822 cur = (unsigned long *)bi->bp_data; 823 while ((char *)cur < (bi->bp_data + sbi->s_blocksize)) { 824 /* 825 * No need to scan if all 0's or all 1's. 826 * Since we're only counting 0's, we can simply ignore 827 * all 1's 828 */ 829 if (*cur == 0) { 830 if (bi->first_zero_hint == 0) { 831 bi->first_zero_hint = 832 ((char *)cur - bi->bp_data) << 3; 833 } 834 bi->free_count += sizeof(unsigned long) * 8; 835 } else if (*cur != ~0L) { 836 int b; 837 838 for (b = 0; b < sizeof(unsigned long) * 8; b++) { 839 if (!reiserfs_test_le_bit(b, cur)) { 840 bi->free_count++; 841 if (bi->first_zero_hint == 0) 842 bi->first_zero_hint = 843 (((char *)cur - 844 bi->bp_data) << 3) + b; 845 } 846 } 847 } 848 cur++; 849 } 850} 851 852/* 853 * Read the bitmaps 854 */ 855static int 856read_bitmaps(struct reiserfs_mount *rmp) 857{ 858 int i, bmap_nr; 859 struct buf *bp = NULL; 860 struct reiserfs_sb_info *sbi = rmp->rm_reiserfs; 861 862 /* Allocate memory for the table of bitmaps */ 863 SB_AP_BITMAP(sbi) = 864 malloc(sizeof(struct reiserfs_bitmap_info) * SB_BMAP_NR(sbi), 865 M_REISERFSMNT, M_WAITOK | M_ZERO); 866 if (!SB_AP_BITMAP(sbi)) 867 return (ENOMEM); 868 869 /* Read all the bitmaps */ 870 for (i = 0, 871 bmap_nr = (REISERFS_DISK_OFFSET_IN_BYTES / sbi->s_blocksize + 1) * 872 btodb(sbi->s_blocksize); 873 i < SB_BMAP_NR(sbi); i++, bmap_nr = sbi->s_blocksize * 8 * i) { 874 SB_AP_BITMAP(sbi)[i].bp_data = malloc(sbi->s_blocksize, 875 M_REISERFSMNT, M_WAITOK | M_ZERO); 876 if (!SB_AP_BITMAP(sbi)[i].bp_data) 877 return (ENOMEM); 878 bread(rmp->rm_devvp, bmap_nr, sbi->s_blocksize, NOCRED, &bp); 879 bcopy(bp->b_data, SB_AP_BITMAP(sbi)[i].bp_data, 880 sbi->s_blocksize); 881 brelse(bp); 882 bp = NULL; 883 884 /*if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) 885 ll_rw_block(READ, 1, &SB_AP_BITMAP(s)[i].bh);*/ 886 } 887 888 for (i = 0; i < SB_BMAP_NR(sbi); i++) { 889 /*if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) { 890 reiserfs_warning(s,"sh-2029: reiserfs read_bitmaps: " 891 "bitmap block (#%lu) reading failed", 892 SB_AP_BITMAP(s)[i].bh->b_blocknr); 893 for (i = 0; i < SB_BMAP_NR(s); i++) 894 brelse(SB_AP_BITMAP(s)[i].bh); 895 vfree(SB_AP_BITMAP(s)); 896 SB_AP_BITMAP(s) = NULL; 897 return 1; 898 }*/ 899 load_bitmap_info_data(sbi, SB_AP_BITMAP(sbi) + i); 900 reiserfs_log(LOG_DEBUG, 901 "%d free blocks (starting at block %ld)\n", 902 SB_AP_BITMAP(sbi)[i].free_count, 903 (long)SB_AP_BITMAP(sbi)[i].first_zero_hint); 904 } 905 906 return (0); 907} 908 909// TODO Not supported 910static int 911read_old_bitmaps(struct reiserfs_mount *rmp) 912{ 913 914 return (EOPNOTSUPP); 915#if 0 916 int i; 917 struct reiserfs_sb_info *sbi = rmp->rm_reiserfs; 918 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(sbi); 919 920 /* First of bitmap blocks */ 921 int bmp1 = (REISERFS_OLD_DISK_OFFSET / sbi->s_blocksize) * 922 btodb(sbi->s_blocksize); 923 924 /* Read true bitmap */ 925 SB_AP_BITMAP(sbi) = 926 malloc(sizeof (struct reiserfs_buffer_info *) * sb_bmap_nr(rs), 927 M_REISERFSMNT, M_WAITOK | M_ZERO); 928 if (!SB_AP_BITMAP(sbi)) 929 return 1; 930 931 for (i = 0; i < sb_bmap_nr(rs); i ++) { 932 SB_AP_BITMAP(sbi)[i].bp = getblk(rmp->rm_devvp, 933 (bmp1 + i) * btodb(sbi->s_blocksize), sbi->s_blocksize, 0, 0, 0); 934 if (!SB_AP_BITMAP(sbi)[i].bp) 935 return 1; 936 load_bitmap_info_data(sbi, SB_AP_BITMAP(sbi) + i); 937 } 938 939 return 0; 940#endif 941} 942 943/* ------------------------------------------------------------------- 944 * Hash detection stuff 945 * -------------------------------------------------------------------*/ 946 947static int 948get_root_node(struct reiserfs_mount *rmp, struct reiserfs_node **root) 949{ 950 struct reiserfs_node *ip; 951 struct reiserfs_iget_args args; 952 953 /* Allocate the node structure */ 954 reiserfs_log(LOG_DEBUG, "malloc(struct reiserfs_node)\n"); 955 MALLOC(ip, struct reiserfs_node *, sizeof(struct reiserfs_node), 956 M_REISERFSNODE, M_WAITOK | M_ZERO); 957 958 /* Fill the structure */ 959 reiserfs_log(LOG_DEBUG, "filling *ip\n"); 960 ip->i_dev = rmp->rm_dev; 961 ip->i_number = REISERFS_ROOT_OBJECTID; 962 ip->i_ino = REISERFS_ROOT_PARENT_OBJECTID; 963 ip->i_reiserfs = rmp->rm_reiserfs; 964 965 /* Read the inode */ 966 args.objectid = ip->i_number; 967 args.dirid = ip->i_ino; 968 reiserfs_log(LOG_DEBUG, "call reiserfs_read_locked_inode(" 969 "objectid=%d,dirid=%d)\n", args.objectid, args.dirid); 970 reiserfs_read_locked_inode(ip, &args); 971 972 ip->i_devvp = rmp->rm_devvp; 973 //XXX VREF(ip->i_devvp); Is it necessary ? 974 975 *root = ip; 976 return (0); 977} 978 979/* 980 * If root directory is empty - we set default - Yura's - hash and warn 981 * about it. 982 * FIXME: we look for only one name in a directory. If tea and yura both 983 * have the same value - we ask user to send report to the mailing list 984 */ 985uint32_t find_hash_out(struct reiserfs_mount *rmp) 986{ 987 int retval; 988 struct cpu_key key; 989 INITIALIZE_PATH(path); 990 struct reiserfs_node *ip; 991 struct reiserfs_sb_info *sbi; 992 struct reiserfs_dir_entry de; 993 uint32_t hash = DEFAULT_HASH; 994 995 get_root_node(rmp, &ip); 996 if (!ip) 997 return (UNSET_HASH); 998 999 sbi = rmp->rm_reiserfs; 1000 1001 do { 1002 uint32_t teahash, r5hash, yurahash; 1003 1004 reiserfs_log(LOG_DEBUG, "make_cpu_key\n"); 1005 make_cpu_key(&key, ip, ~0, TYPE_DIRENTRY, 3); 1006 reiserfs_log(LOG_DEBUG, "search_by_entry_key for " 1007 "key(objectid=%d,dirid=%d)\n", 1008 key.on_disk_key.k_objectid, key.on_disk_key.k_dir_id); 1009 retval = search_by_entry_key(sbi, &key, &path, &de); 1010 if (retval == IO_ERROR) { 1011 pathrelse(&path); 1012 return (UNSET_HASH); 1013 } 1014 if (retval == NAME_NOT_FOUND) 1015 de.de_entry_num--; 1016 1017 reiserfs_log(LOG_DEBUG, "name found\n"); 1018 1019 set_de_name_and_namelen(&de); 1020 1021 if (deh_offset(&(de.de_deh[de.de_entry_num])) == DOT_DOT_OFFSET) { 1022 /* Allow override in this case */ 1023 if (reiserfs_rupasov_hash(sbi)) { 1024 hash = YURA_HASH; 1025 } 1026 reiserfs_log(LOG_DEBUG, 1027 "FS seems to be empty, autodetect " 1028 "is using the default hash"); 1029 break; 1030 } 1031 1032 r5hash = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen)); 1033 teahash = GET_HASH_VALUE(keyed_hash(de.de_name, 1034 de.de_namelen)); 1035 yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen)); 1036 if (((teahash == r5hash) && 1037 (GET_HASH_VALUE( 1038 deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash)) || 1039 ((teahash == yurahash) && 1040 (yurahash == 1041 GET_HASH_VALUE( 1042 deh_offset(&(de.de_deh[de.de_entry_num]))))) || 1043 ((r5hash == yurahash) && 1044 (yurahash == 1045 GET_HASH_VALUE( 1046 deh_offset(&(de.de_deh[de.de_entry_num])))))) { 1047 reiserfs_log(LOG_ERR, 1048 "unable to automatically detect hash " 1049 "function. Please mount with -o " 1050 "hash={tea,rupasov,r5}"); 1051 hash = UNSET_HASH; 1052 break; 1053 } 1054 1055 if (GET_HASH_VALUE( 1056 deh_offset(&(de.de_deh[de.de_entry_num]))) == yurahash) { 1057 reiserfs_log(LOG_DEBUG, "detected YURA hash\n"); 1058 hash = YURA_HASH; 1059 } else if (GET_HASH_VALUE( 1060 deh_offset(&(de.de_deh[de.de_entry_num]))) == teahash) { 1061 reiserfs_log(LOG_DEBUG, "detected TEA hash\n"); 1062 hash = TEA_HASH; 1063 } else if (GET_HASH_VALUE( 1064 deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash) { 1065 reiserfs_log(LOG_DEBUG, "detected R5 hash\n"); 1066 hash = R5_HASH; 1067 } else { 1068 reiserfs_log(LOG_WARNING, "unrecognised hash function"); 1069 hash = UNSET_HASH; 1070 } 1071 } while (0); 1072 1073 pathrelse(&path); 1074 return (hash); 1075} 1076 1077/* Finds out which hash names are sorted with */ 1078static int 1079what_hash(struct reiserfs_mount *rmp) 1080{ 1081 uint32_t code; 1082 struct reiserfs_sb_info *sbi = rmp->rm_reiserfs; 1083 1084 find_hash_out(rmp); 1085 code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(sbi)); 1086 1087 /* 1088 * reiserfs_hash_detect() == true if any of the hash mount options 1089 * were used. We must check them to make sure the user isn't using a 1090 * bad hash value 1091 */ 1092 if (code == UNSET_HASH || reiserfs_hash_detect(sbi)) 1093 code = find_hash_out(rmp); 1094 1095 if (code != UNSET_HASH && reiserfs_hash_detect(sbi)) { 1096 /* 1097 * Detection has found the hash, and we must check against 1098 * the mount options 1099 */ 1100 if (reiserfs_rupasov_hash(sbi) && code != YURA_HASH) { 1101 reiserfs_log(LOG_ERR, "error, %s hash detected, " 1102 "unable to force rupasov hash", 1103 reiserfs_hashname(code)); 1104 code = UNSET_HASH; 1105 } else if (reiserfs_tea_hash(sbi) && code != TEA_HASH) { 1106 reiserfs_log(LOG_ERR, "error, %s hash detected, " 1107 "unable to force tea hash", 1108 reiserfs_hashname(code)); 1109 code = UNSET_HASH; 1110 } else if (reiserfs_r5_hash(sbi) && code != R5_HASH) { 1111 reiserfs_log(LOG_ERR, "error, %s hash detected, " 1112 "unable to force r5 hash", 1113 reiserfs_hashname(code)); 1114 code = UNSET_HASH; 1115 } 1116 } else { 1117 /* 1118 * Find_hash_out was not called or could not determine 1119 * the hash 1120 */ 1121 if (reiserfs_rupasov_hash(sbi)) { 1122 code = YURA_HASH; 1123 } else if (reiserfs_tea_hash(sbi)) { 1124 code = TEA_HASH; 1125 } else if (reiserfs_r5_hash(sbi)) { 1126 code = R5_HASH; 1127 } 1128 } 1129 1130 /* TODO Not supported yet */ 1131#if 0 1132 /* If we are mounted RW, and we have a new valid hash code, update 1133 * the super */ 1134 if (code != UNSET_HASH && 1135 !(s->s_flags & MS_RDONLY) && 1136 code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) { 1137 set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code); 1138 } 1139#endif 1140 1141 return (code); 1142} 1143 1144/* Return pointer to appropriate function */ 1145static hashf_t 1146hash_function(struct reiserfs_mount *rmp) 1147{ 1148 1149 switch (what_hash(rmp)) { 1150 case TEA_HASH: 1151 reiserfs_log(LOG_INFO, "using tea hash to sort names\n"); 1152 return (keyed_hash); 1153 case YURA_HASH: 1154 reiserfs_log(LOG_INFO, "using rupasov hash to sort names\n"); 1155 return (yura_hash); 1156 case R5_HASH: 1157 reiserfs_log(LOG_INFO, "using r5 hash to sort names\n"); 1158 return (r5_hash); 1159 } 1160 1161 return (NULL); 1162} 1163 1164/* ------------------------------------------------------------------- 1165 * VFS registration 1166 * -------------------------------------------------------------------*/ 1167 1168static struct vfsops reiser_vfsops = { 1169 .vfs_cmount = reiserfs_cmount, 1170 .vfs_mount = reiserfs_mount, 1171 .vfs_unmount = reiserfs_unmount, 1172 //.vfs_checkexp = reiserfs_checkexp, 1173 //.vfs_extattrctl = reiserfs_extattrctl, 1174 .vfs_fhtovp = reiserfs_fhtovp, 1175 //.vfs_quotactl = reiserfs_quotactl, 1176 .vfs_root = reiserfs_root, 1177 //.vfs_start = reiserfs_start, 1178 .vfs_statfs = reiserfs_statfs, 1179 //.vfs_sync = reiserfs_sync, 1180 //.vfs_vget = reiserfs_vget, 1181 .vfs_vptofh = reiserfs_vptofh, 1182}; 1183 1184VFS_SET(reiser_vfsops, reiserfs, VFCF_READONLY); 1185