ext2_vfsops.c revision 40651
1/* 2 * modified for EXT2FS support in Lites 1.1 3 * 4 * Aug 1995, Godmar Back (gback@cs.utah.edu) 5 * University of Utah, Department of Computer Science 6 */ 7/* 8 * Copyright (c) 1989, 1991, 1993, 1994 9 * The Regents of the University of California. All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * @(#)ffs_vfsops.c 8.8 (Berkeley) 4/18/94 40 */ 41 42#include "opt_quota.h" 43 44#include <sys/param.h> 45#include <sys/systm.h> 46#include <sys/namei.h> 47#include <sys/proc.h> 48#include <sys/kernel.h> 49#include <sys/vnode.h> 50#include <sys/mount.h> 51#include <sys/buf.h> 52#include <sys/conf.h> 53#include <sys/fcntl.h> 54#include <sys/disklabel.h> 55#include <sys/malloc.h> 56#include <sys/stat.h> 57 58#include <miscfs/specfs/specdev.h> 59 60#include <ufs/ufs/quota.h> 61#include <ufs/ufs/ufsmount.h> 62#include <ufs/ufs/inode.h> 63#include <ufs/ufs/ufs_extern.h> 64 65#include <gnu/ext2fs/fs.h> 66#include <gnu/ext2fs/ext2_extern.h> 67#include <gnu/ext2fs/ext2_fs.h> 68#include <gnu/ext2fs/ext2_fs_sb.h> 69 70static int ext2_fhtovp __P((struct mount *, struct fid *, struct sockaddr *, 71 struct vnode **, int *, struct ucred **)); 72static int ext2_flushfiles __P((struct mount *mp, int flags, struct proc *p)); 73static int ext2_mount __P((struct mount *, 74 char *, caddr_t, struct nameidata *, struct proc *)); 75static int ext2_mountfs __P((struct vnode *, struct mount *, struct proc *)); 76static int ext2_reload __P((struct mount *mountp, struct ucred *cred, 77 struct proc *p)); 78static int ext2_sbupdate __P((struct ufsmount *, int)); 79static int ext2_statfs __P((struct mount *, struct statfs *, struct proc *)); 80static int ext2_sync __P((struct mount *, int, struct ucred *, struct proc *)); 81static int ext2_unmount __P((struct mount *, int, struct proc *)); 82static int ext2_vget __P((struct mount *, ino_t, struct vnode **)); 83static int ext2_vptofh __P((struct vnode *, struct fid *)); 84 85static MALLOC_DEFINE(M_EXT2NODE, "EXT2 node", "EXT2 vnode private part"); 86 87static struct vfsops ext2fs_vfsops = { 88 ext2_mount, 89 ufs_start, /* empty function */ 90 ext2_unmount, 91 ufs_root, /* root inode via vget */ 92 ufs_quotactl, /* does operations associated with quotas */ 93 ext2_statfs, 94 ext2_sync, 95 ext2_vget, 96 ext2_fhtovp, 97 ext2_vptofh, 98 ext2_init, 99}; 100 101VFS_SET(ext2fs_vfsops, ext2fs, 0); 102#define bsd_malloc malloc 103#define bsd_free free 104 105static int ext2fs_inode_hash_lock; 106 107static int compute_sb_data __P((struct vnode * devvp, 108 struct ext2_super_block * es, 109 struct ext2_sb_info * fs)); 110 111#ifdef notyet 112static int ext2_mountroot __P((void)); 113 114/* 115 * Called by main() when ext2fs is going to be mounted as root. 116 * 117 * Name is updated by mount(8) after booting. 118 */ 119#define ROOTNAME "root_device" 120 121static int 122ext2_mountroot() 123{ 124 register struct ext2_sb_info *fs; 125 register struct mount *mp; 126 struct proc *p = curproc; 127 struct ufsmount *ump; 128 u_int size; 129 int error; 130 131 if ((error = bdevvp(rootdev, &rootvp))) { 132 printf("ext2_mountroot: can't find rootvp"); 133 return (error); 134 } 135 mp = bsd_malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK); 136 bzero((char *)mp, (u_long)sizeof(struct mount)); 137 mp->mnt_op = &ext2fs_vfsops; 138 mp->mnt_flag = MNT_RDONLY; 139 if (bdevsw[major(rootdev)]->d_flags & D_NOCLUSTERR) 140 mp->mnt_flag |= MNT_NOCLUSTERR; 141 if (bdevsw[major(rootdev)]->d_flags & D_NOCLUSTERW) 142 mp->mnt_flag |= MNT_NOCLUSTERW; 143 if (error = ext2_mountfs(rootvp, mp, p)) { 144 bsd_free(mp, M_MOUNT); 145 return (error); 146 } 147 if (error = vfs_lock(mp)) { 148 (void)ext2_unmount(mp, 0, p); 149 bsd_free(mp, M_MOUNT); 150 return (error); 151 } 152 CIRCLEQ_INSERT_HEAD(&mountlist, mp, mnt_list); 153 mp->mnt_flag |= MNT_ROOTFS; 154 mp->mnt_vnodecovered = NULLVP; 155 ump = VFSTOUFS(mp); 156 fs = ump->um_e2fs; 157 bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt)); 158 fs->fs_fsmnt[0] = '/'; 159 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname, 160 MNAMELEN); 161 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 162 &size); 163 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 164 (void)ext2_statfs(mp, &mp->mnt_stat, p); 165 vfs_unlock(mp); 166 inittodr(fs->s_es->s_wtime); /* this helps to set the time */ 167 return (0); 168} 169#endif 170 171/* 172 * VFS Operations. 173 * 174 * mount system call 175 */ 176static int 177ext2_mount(mp, path, data, ndp, p) 178 register struct mount *mp; 179 char *path; 180 caddr_t data; /* this is actually a (struct ufs_args *) */ 181 struct nameidata *ndp; 182 struct proc *p; 183{ 184 struct vnode *devvp; 185 struct ufs_args args; 186 struct ufsmount *ump = 0; 187 register struct ext2_sb_info *fs; 188 u_int size; 189 int error, flags; 190 mode_t accessmode; 191 192 if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args))) 193 return (error); 194 /* 195 * If updating, check whether changing from read-only to 196 * read/write; if there is no device name, that's all we do. 197 * Disallow clearing MNT_NOCLUSTERR and MNT_NOCLUSTERW flags, 198 * if block device requests. 199 */ 200 if (mp->mnt_flag & MNT_UPDATE) { 201 ump = VFSTOUFS(mp); 202 fs = ump->um_e2fs; 203 error = 0; 204 if (bdevsw[major(ump->um_dev)]->d_flags & D_NOCLUSTERR) 205 mp->mnt_flag |= MNT_NOCLUSTERR; 206 if (bdevsw[major(ump->um_dev)]->d_flags & D_NOCLUSTERW) 207 mp->mnt_flag |= MNT_NOCLUSTERW; 208 if (fs->s_rd_only == 0 && (mp->mnt_flag & MNT_RDONLY)) { 209 flags = WRITECLOSE; 210 if (mp->mnt_flag & MNT_FORCE) 211 flags |= FORCECLOSE; 212 if (vfs_busy(mp, LK_NOWAIT, 0, p)) 213 return (EBUSY); 214 error = ext2_flushfiles(mp, flags, p); 215 vfs_unbusy(mp, p); 216 if (!error && fs->s_wasvalid) { 217 fs->s_es->s_state |= EXT2_VALID_FS; 218 ext2_sbupdate(ump, MNT_WAIT); 219 } 220 fs->s_rd_only = 1; 221 } 222 if (!error && (mp->mnt_flag & MNT_RELOAD)) 223 error = ext2_reload(mp, ndp->ni_cnd.cn_cred, p); 224 if (error) 225 return (error); 226 if (fs->s_rd_only && (mp->mnt_kern_flag & MNTK_WANTRDWR)) { 227 /* 228 * If upgrade to read-write by non-root, then verify 229 * that user has necessary permissions on the device. 230 */ 231 if (p->p_ucred->cr_uid != 0) { 232 devvp = ump->um_devvp; 233 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 234 if (error = VOP_ACCESS(devvp, VREAD | VWRITE, 235 p->p_ucred, p)) { 236 VOP_UNLOCK(devvp, 0, p); 237 return (error); 238 } 239 VOP_UNLOCK(devvp, 0, p); 240 } 241 242 if ((fs->s_es->s_state & EXT2_VALID_FS) == 0 || 243 (fs->s_es->s_state & EXT2_ERROR_FS)) { 244 if (mp->mnt_flag & MNT_FORCE) { 245 printf( 246"WARNING: %s was not properly dismounted\n", 247 fs->fs_fsmnt); 248 } else { 249 printf( 250"WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n", 251 fs->fs_fsmnt); 252 return (EPERM); 253 } 254 } 255 fs->s_es->s_state &= ~EXT2_VALID_FS; 256 ext2_sbupdate(ump, MNT_WAIT); 257 fs->s_rd_only = 0; 258 } 259 if (args.fspec == 0) { 260 /* 261 * Process export requests. 262 */ 263 return (vfs_export(mp, &ump->um_export, &args.export)); 264 } 265 } 266 /* 267 * Not an update, or updating the name: look up the name 268 * and verify that it refers to a sensible block device. 269 */ 270 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); 271 if (error = namei(ndp)) 272 return (error); 273 devvp = ndp->ni_vp; 274 275 if (devvp->v_type != VBLK) { 276 vrele(devvp); 277 return (ENOTBLK); 278 } 279 if (major(devvp->v_rdev) >= nblkdev || 280 bdevsw[major(devvp->v_rdev)] == NULL) { 281 vrele(devvp); 282 return (ENXIO); 283 } 284 285 /* 286 * If mount by non-root, then verify that user has necessary 287 * permissions on the device. 288 */ 289 if (p->p_ucred->cr_uid != 0) { 290 accessmode = VREAD; 291 if ((mp->mnt_flag & MNT_RDONLY) == 0) 292 accessmode |= VWRITE; 293 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 294 if (error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p)) { 295 vput(devvp); 296 return (error); 297 } 298 VOP_UNLOCK(devvp, 0, p); 299 } 300 301 if ((mp->mnt_flag & MNT_UPDATE) == 0) { 302 if (bdevsw[major(devvp->v_rdev)]->d_flags & D_NOCLUSTERR) 303 mp->mnt_flag |= MNT_NOCLUSTERR; 304 if (bdevsw[major(devvp->v_rdev)]->d_flags & D_NOCLUSTERW) 305 mp->mnt_flag |= MNT_NOCLUSTERW; 306 error = ext2_mountfs(devvp, mp, p); 307 } else { 308 if (devvp != ump->um_devvp) 309 error = EINVAL; /* needs translation */ 310 else 311 vrele(devvp); 312 } 313 if (error) { 314 vrele(devvp); 315 return (error); 316 } 317 ump = VFSTOUFS(mp); 318 fs = ump->um_e2fs; 319 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size); 320 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size); 321 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname, 322 MNAMELEN); 323 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 324 &size); 325 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 326 (void)ext2_statfs(mp, &mp->mnt_stat, p); 327 return (0); 328} 329 330/* 331 * checks that the data in the descriptor blocks make sense 332 * this is taken from ext2/super.c 333 */ 334static int ext2_check_descriptors (struct ext2_sb_info * sb) 335{ 336 int i; 337 int desc_block = 0; 338 unsigned long block = sb->s_es->s_first_data_block; 339 struct ext2_group_desc * gdp = NULL; 340 341 /* ext2_debug ("Checking group descriptors"); */ 342 343 for (i = 0; i < sb->s_groups_count; i++) 344 { 345 /* examine next descriptor block */ 346 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0) 347 gdp = (struct ext2_group_desc *) 348 sb->s_group_desc[desc_block++]->b_data; 349 if (gdp->bg_block_bitmap < block || 350 gdp->bg_block_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb)) 351 { 352 printf ("ext2_check_descriptors: " 353 "Block bitmap for group %d" 354 " not in group (block %lu)!\n", 355 i, (unsigned long) gdp->bg_block_bitmap); 356 return 0; 357 } 358 if (gdp->bg_inode_bitmap < block || 359 gdp->bg_inode_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb)) 360 { 361 printf ("ext2_check_descriptors: " 362 "Inode bitmap for group %d" 363 " not in group (block %lu)!\n", 364 i, (unsigned long) gdp->bg_inode_bitmap); 365 return 0; 366 } 367 if (gdp->bg_inode_table < block || 368 gdp->bg_inode_table + sb->s_itb_per_group >= 369 block + EXT2_BLOCKS_PER_GROUP(sb)) 370 { 371 printf ("ext2_check_descriptors: " 372 "Inode table for group %d" 373 " not in group (block %lu)!\n", 374 i, (unsigned long) gdp->bg_inode_table); 375 return 0; 376 } 377 block += EXT2_BLOCKS_PER_GROUP(sb); 378 gdp++; 379 } 380 return 1; 381} 382 383/* 384 * this computes the fields of the ext2_sb_info structure from the 385 * data in the ext2_super_block structure read in 386 */ 387static int compute_sb_data(devvp, es, fs) 388 struct vnode * devvp; 389 struct ext2_super_block * es; 390 struct ext2_sb_info * fs; 391{ 392 int db_count, error; 393 int i, j; 394 int logic_sb_block = 1; /* XXX for now */ 395 396#if 1 397#define V(v) 398#else 399#define V(v) printf(#v"= %d\n", fs->v); 400#endif 401 402 fs->s_blocksize = EXT2_MIN_BLOCK_SIZE << es->s_log_block_size; 403 V(s_blocksize) 404 fs->s_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->s_log_block_size; 405 V(s_bshift) 406 fs->s_fsbtodb = es->s_log_block_size + 1; 407 V(s_fsbtodb) 408 fs->s_qbmask = fs->s_blocksize - 1; 409 V(s_bmask) 410 fs->s_blocksize_bits = EXT2_BLOCK_SIZE_BITS(es); 411 V(s_blocksize_bits) 412 fs->s_frag_size = EXT2_MIN_FRAG_SIZE << es->s_log_frag_size; 413 V(s_frag_size) 414 if (fs->s_frag_size) 415 fs->s_frags_per_block = fs->s_blocksize / fs->s_frag_size; 416 V(s_frags_per_block) 417 fs->s_blocks_per_group = es->s_blocks_per_group; 418 V(s_blocks_per_group) 419 fs->s_frags_per_group = es->s_frags_per_group; 420 V(s_frags_per_group) 421 fs->s_inodes_per_group = es->s_inodes_per_group; 422 V(s_inodes_per_group) 423 fs->s_inodes_per_block = fs->s_blocksize / EXT2_INODE_SIZE; 424 V(s_inodes_per_block) 425 fs->s_itb_per_group = fs->s_inodes_per_group /fs->s_inodes_per_block; 426 V(s_itb_per_group) 427 fs->s_desc_per_block = fs->s_blocksize / sizeof (struct ext2_group_desc); 428 V(s_desc_per_block) 429 /* s_resuid / s_resgid ? */ 430 fs->s_groups_count = (es->s_blocks_count - 431 es->s_first_data_block + 432 EXT2_BLOCKS_PER_GROUP(fs) - 1) / 433 EXT2_BLOCKS_PER_GROUP(fs); 434 V(s_groups_count) 435 db_count = (fs->s_groups_count + EXT2_DESC_PER_BLOCK(fs) - 1) / 436 EXT2_DESC_PER_BLOCK(fs); 437 fs->s_db_per_group = db_count; 438 V(s_db_per_group) 439 440 fs->s_group_desc = bsd_malloc(db_count * sizeof (struct buf *), 441 M_UFSMNT, M_WAITOK); 442 443 /* adjust logic_sb_block */ 444 if(fs->s_blocksize > SBSIZE) 445 /* Godmar thinks: if the blocksize is greater than 1024, then 446 the superblock is logically part of block zero. 447 */ 448 logic_sb_block = 0; 449 450 for (i = 0; i < db_count; i++) { 451 error = bread(devvp , fsbtodb(fs, logic_sb_block + i + 1), 452 fs->s_blocksize, NOCRED, &fs->s_group_desc[i]); 453 if(error) { 454 for (j = 0; j < i; j++) 455 brelse(fs->s_group_desc[j]); 456 bsd_free(fs->s_group_desc, M_UFSMNT); 457 printf("EXT2-fs: unable to read group descriptors (%d)\n", error); 458 return EIO; 459 } 460 /* Set the B_LOCKED flag on the buffer, then brelse() it */ 461 LCK_BUF(fs->s_group_desc[i]) 462 } 463 if(!ext2_check_descriptors(fs)) { 464 for (j = 0; j < db_count; j++) 465 ULCK_BUF(fs->s_group_desc[j]) 466 bsd_free(fs->s_group_desc, M_UFSMNT); 467 printf("EXT2-fs: (ext2_check_descriptors failure) " 468 "unable to read group descriptors\n"); 469 return EIO; 470 } 471 472 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) { 473 fs->s_inode_bitmap_number[i] = 0; 474 fs->s_inode_bitmap[i] = NULL; 475 fs->s_block_bitmap_number[i] = 0; 476 fs->s_block_bitmap[i] = NULL; 477 } 478 fs->s_loaded_inode_bitmaps = 0; 479 fs->s_loaded_block_bitmaps = 0; 480 return 0; 481} 482 483/* 484 * Reload all incore data for a filesystem (used after running fsck on 485 * the root filesystem and finding things to fix). The filesystem must 486 * be mounted read-only. 487 * 488 * Things to do to update the mount: 489 * 1) invalidate all cached meta-data. 490 * 2) re-read superblock from disk. 491 * 3) re-read summary information from disk. 492 * 4) invalidate all inactive vnodes. 493 * 5) invalidate all cached file data. 494 * 6) re-read inode data for all active vnodes. 495 */ 496static int 497ext2_reload(mountp, cred, p) 498 register struct mount *mountp; 499 struct ucred *cred; 500 struct proc *p; 501{ 502 register struct vnode *vp, *nvp, *devvp; 503 struct inode *ip; 504 struct buf *bp; 505 struct ext2_super_block * es; 506 struct ext2_sb_info *fs; 507 int error; 508 509 if ((mountp->mnt_flag & MNT_RDONLY) == 0) 510 return (EINVAL); 511 /* 512 * Step 1: invalidate all cached meta-data. 513 */ 514 devvp = VFSTOUFS(mountp)->um_devvp; 515 if (vinvalbuf(devvp, 0, cred, p, 0, 0)) 516 panic("ext2_reload: dirty1"); 517 /* 518 * Step 2: re-read superblock from disk. 519 * constants have been adjusted for ext2 520 */ 521 if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) 522 return (error); 523 es = (struct ext2_super_block *)bp->b_data; 524 if (es->s_magic != EXT2_SUPER_MAGIC) { 525 if(es->s_magic == EXT2_PRE_02B_MAGIC) 526 printf("This filesystem bears the magic number of a pre " 527 "0.2b version of ext2. This is not supported by " 528 "Lites.\n"); 529 else 530 printf("Wrong magic number: %x (expected %x for ext2 fs\n", 531 es->s_magic, EXT2_SUPER_MAGIC); 532 brelse(bp); 533 return (EIO); /* XXX needs translation */ 534 } 535 fs = VFSTOUFS(mountp)->um_e2fs; 536 bcopy(bp->b_data, fs->s_es, sizeof(struct ext2_super_block)); 537 538 if(error = compute_sb_data(devvp, es, fs)) { 539 brelse(bp); 540 return error; 541 } 542#ifdef UNKLAR 543 if (fs->fs_sbsize < SBSIZE) 544 bp->b_flags |= B_INVAL; 545#endif 546 brelse(bp); 547 548loop: 549 simple_lock(&mntvnode_slock); 550 for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { 551 if (vp->v_mount != mountp) { 552 simple_unlock(&mntvnode_slock); 553 goto loop; 554 } 555 nvp = vp->v_mntvnodes.le_next; 556 /* 557 * Step 4: invalidate all inactive vnodes. 558 */ 559 if (vrecycle(vp, &mntvnode_slock, p)) 560 goto loop; 561 /* 562 * Step 5: invalidate all cached file data. 563 */ 564 simple_lock(&vp->v_interlock); 565 simple_unlock(&mntvnode_slock); 566 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) { 567 goto loop; 568 } 569 if (vinvalbuf(vp, 0, cred, p, 0, 0)) 570 panic("ext2_reload: dirty2"); 571 /* 572 * Step 6: re-read inode data for all active vnodes. 573 */ 574 ip = VTOI(vp); 575 error = 576 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 577 (int)fs->s_blocksize, NOCRED, &bp); 578 if (error) { 579 vput(vp); 580 return (error); 581 } 582 ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + 583 EXT2_INODE_SIZE * ino_to_fsbo(fs, ip->i_number)), 584 &ip->i_din); 585 brelse(bp); 586 vput(vp); 587 simple_lock(&mntvnode_slock); 588 } 589 simple_unlock(&mntvnode_slock); 590 return (0); 591} 592 593/* 594 * Common code for mount and mountroot 595 */ 596static int 597ext2_mountfs(devvp, mp, p) 598 register struct vnode *devvp; 599 struct mount *mp; 600 struct proc *p; 601{ 602 register struct ufsmount *ump; 603 struct buf *bp; 604 register struct ext2_sb_info *fs; 605 struct ext2_super_block * es; 606 dev_t dev = devvp->v_rdev; 607 struct partinfo dpart; 608 int havepart = 0; 609 int error, i, size; 610 int ronly; 611 612 /* 613 * Disallow multiple mounts of the same device. 614 * Disallow mounting of a device that is currently in use 615 * (except for root, which might share swap device for miniroot). 616 * Flush out any old buffers remaining from a previous use. 617 */ 618 if (error = vfs_mountedon(devvp)) 619 return (error); 620 if (vcount(devvp) > 1 && devvp != rootvp) 621 return (EBUSY); 622 if (error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0)) 623 return (error); 624#ifdef READONLY 625/* turn on this to force it to be read-only */ 626 mp->mnt_flag |= MNT_RDONLY; 627#endif 628 629 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 630 if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p)) 631 return (error); 632 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0) 633 size = DEV_BSIZE; 634 else { 635 havepart = 1; 636 size = dpart.disklab->d_secsize; 637 } 638 639 bp = NULL; 640 ump = NULL; 641 if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) 642 goto out; 643 es = (struct ext2_super_block *)bp->b_data; 644 if (es->s_magic != EXT2_SUPER_MAGIC) { 645 if(es->s_magic == EXT2_PRE_02B_MAGIC) 646 printf("This filesystem bears the magic number of a pre " 647 "0.2b version of ext2. This is not supported by " 648 "Lites.\n"); 649 else 650 printf("Wrong magic number: %x (expected %x for EXT2FS)\n", 651 es->s_magic, EXT2_SUPER_MAGIC); 652 error = EINVAL; /* XXX needs translation */ 653 goto out; 654 } 655 if ((es->s_state & EXT2_VALID_FS) == 0 || 656 (es->s_state & EXT2_ERROR_FS)) { 657 if (ronly || (mp->mnt_flag & MNT_FORCE)) { 658 printf( 659"WARNING: Filesystem was not properly dismounted\n"); 660 } else { 661 printf( 662"WARNING: R/W mount denied. Filesystem is not clean - run fsck\n"); 663 error = EPERM; 664 goto out; 665 } 666 } 667 ump = bsd_malloc(sizeof *ump, M_UFSMNT, M_WAITOK); 668 bzero((caddr_t)ump, sizeof *ump); 669 ump->um_malloctype = M_EXT2NODE; 670 ump->um_blkatoff = ext2_blkatoff; 671 ump->um_truncate = ext2_truncate; 672 ump->um_update = ext2_update; 673 ump->um_valloc = ext2_valloc; 674 ump->um_vfree = ext2_vfree; 675 /* I don't know whether this is the right strategy. Note that 676 we dynamically allocate both a ext2_sb_info and a ext2_super_block 677 while Linux keeps the super block in a locked buffer 678 */ 679 ump->um_e2fs = bsd_malloc(sizeof(struct ext2_sb_info), 680 M_UFSMNT, M_WAITOK); 681 ump->um_e2fs->s_es = bsd_malloc(sizeof(struct ext2_super_block), 682 M_UFSMNT, M_WAITOK); 683 bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block)); 684 if ((error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs))) 685 goto out; 686 /* 687 * We don't free the group descriptors allocated by compute_sb_data() 688 * until ext2_unmount(). This is OK since the mount will succeed. 689 */ 690 brelse(bp); 691 bp = NULL; 692 fs = ump->um_e2fs; 693 fs->s_rd_only = ronly; /* ronly is set according to mnt_flags */ 694 /* if the fs is not mounted read-only, make sure the super block is 695 always written back on a sync() 696 */ 697 fs->s_wasvalid = fs->s_es->s_state & EXT2_VALID_FS ? 1 : 0; 698 if (ronly == 0) { 699 fs->s_dirt = 1; /* mark it modified */ 700 fs->s_es->s_state &= ~EXT2_VALID_FS; /* set fs invalid */ 701 } 702 mp->mnt_data = (qaddr_t)ump; 703 mp->mnt_stat.f_fsid.val[0] = (long)dev; 704 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 705 mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN; 706 mp->mnt_flag |= MNT_LOCAL; 707 ump->um_mountp = mp; 708 ump->um_dev = dev; 709 ump->um_devvp = devvp; 710 /* setting those two parameters allows us to use 711 ufs_bmap w/o changse ! 712 */ 713 ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs); 714 ump->um_bptrtodb = fs->s_es->s_log_block_size + 1; 715 ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs); 716 for (i = 0; i < MAXQUOTAS; i++) 717 ump->um_quotas[i] = NULLVP; 718 devvp->v_specmountpoint = mp; 719 if (ronly == 0) 720 ext2_sbupdate(ump, MNT_WAIT); 721 return (0); 722out: 723 if (bp) 724 brelse(bp); 725 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p); 726 if (ump) { 727 bsd_free(ump->um_e2fs->s_es, M_UFSMNT); 728 bsd_free(ump->um_e2fs, M_UFSMNT); 729 bsd_free(ump, M_UFSMNT); 730 mp->mnt_data = (qaddr_t)0; 731 } 732 return (error); 733} 734 735/* 736 * unmount system call 737 */ 738static int 739ext2_unmount(mp, mntflags, p) 740 struct mount *mp; 741 int mntflags; 742 struct proc *p; 743{ 744 register struct ufsmount *ump; 745 register struct ext2_sb_info *fs; 746 int error, flags, ronly, i; 747 748 flags = 0; 749 if (mntflags & MNT_FORCE) { 750 if (mp->mnt_flag & MNT_ROOTFS) 751 return (EINVAL); 752 flags |= FORCECLOSE; 753 } 754 if (error = ext2_flushfiles(mp, flags, p)) 755 return (error); 756 ump = VFSTOUFS(mp); 757 fs = ump->um_e2fs; 758 ronly = fs->s_rd_only; 759 if (ronly == 0) { 760 if (fs->s_wasvalid) 761 fs->s_es->s_state |= EXT2_VALID_FS; 762 ext2_sbupdate(ump, MNT_WAIT); 763 } 764 765 /* release buffers containing group descriptors */ 766 for(i = 0; i < fs->s_db_per_group; i++) 767 ULCK_BUF(fs->s_group_desc[i]) 768 bsd_free(fs->s_group_desc, M_UFSMNT); 769 770 /* release cached inode/block bitmaps */ 771 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) 772 if (fs->s_inode_bitmap[i]) 773 ULCK_BUF(fs->s_inode_bitmap[i]) 774 775 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) 776 if (fs->s_block_bitmap[i]) 777 ULCK_BUF(fs->s_block_bitmap[i]) 778 779 ump->um_devvp->v_specmountpoint = NULL; 780 error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE, 781 NOCRED, p); 782 vrele(ump->um_devvp); 783 bsd_free(fs->s_es, M_UFSMNT); 784 bsd_free(fs, M_UFSMNT); 785 bsd_free(ump, M_UFSMNT); 786 mp->mnt_data = (qaddr_t)0; 787 mp->mnt_flag &= ~MNT_LOCAL; 788 return (error); 789} 790 791/* 792 * Flush out all the files in a filesystem. 793 */ 794static int 795ext2_flushfiles(mp, flags, p) 796 register struct mount *mp; 797 int flags; 798 struct proc *p; 799{ 800 register struct ufsmount *ump; 801 int error; 802#if QUOTA 803 int i; 804#endif 805 806 ump = VFSTOUFS(mp); 807#if QUOTA 808 if (mp->mnt_flag & MNT_QUOTA) { 809 if (error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) 810 return (error); 811 for (i = 0; i < MAXQUOTAS; i++) { 812 if (ump->um_quotas[i] == NULLVP) 813 continue; 814 quotaoff(p, mp, i); 815 } 816 /* 817 * Here we fall through to vflush again to ensure 818 * that we have gotten rid of all the system vnodes. 819 */ 820 } 821#endif 822 error = vflush(mp, NULLVP, flags); 823 return (error); 824} 825 826/* 827 * Get file system statistics. 828 * taken from ext2/super.c ext2_statfs 829 */ 830static int 831ext2_statfs(mp, sbp, p) 832 struct mount *mp; 833 register struct statfs *sbp; 834 struct proc *p; 835{ 836 unsigned long overhead; 837 unsigned long overhead_per_group; 838 839 register struct ufsmount *ump; 840 register struct ext2_sb_info *fs; 841 register struct ext2_super_block *es; 842 843 ump = VFSTOUFS(mp); 844 fs = ump->um_e2fs; 845 es = fs->s_es; 846 847 if (es->s_magic != EXT2_SUPER_MAGIC) 848 panic("ext2_statfs - magic number spoiled"); 849 850 /* 851 * Compute the overhead (FS structures) 852 */ 853 overhead_per_group = 1 /* super block */ + 854 fs->s_db_per_group + 855 1 /* block bitmap */ + 856 1 /* inode bitmap */ + 857 fs->s_itb_per_group; 858 overhead = es->s_first_data_block + 859 fs->s_groups_count * overhead_per_group; 860 861 sbp->f_bsize = EXT2_FRAG_SIZE(fs); 862 sbp->f_iosize = EXT2_BLOCK_SIZE(fs); 863 sbp->f_blocks = es->s_blocks_count - overhead; 864 sbp->f_bfree = es->s_free_blocks_count; 865 sbp->f_bavail = sbp->f_bfree - es->s_r_blocks_count; 866 sbp->f_files = es->s_inodes_count; 867 sbp->f_ffree = es->s_free_inodes_count; 868 if (sbp != &mp->mnt_stat) { 869 sbp->f_type = mp->mnt_vfc->vfc_typenum; 870 bcopy((caddr_t)mp->mnt_stat.f_mntonname, 871 (caddr_t)&sbp->f_mntonname[0], MNAMELEN); 872 bcopy((caddr_t)mp->mnt_stat.f_mntfromname, 873 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); 874 } 875 return (0); 876} 877 878/* 879 * Go through the disk queues to initiate sandbagged IO; 880 * go through the inodes to write those that have been modified; 881 * initiate the writing of the super block if it has been modified. 882 * 883 * Note: we are always called with the filesystem marked `MPBUSY'. 884 */ 885static int 886ext2_sync(mp, waitfor, cred, p) 887 struct mount *mp; 888 int waitfor; 889 struct ucred *cred; 890 struct proc *p; 891{ 892 struct vnode *nvp, *vp; 893 struct inode *ip; 894 struct ufsmount *ump = VFSTOUFS(mp); 895 struct ext2_sb_info *fs; 896 struct timeval tv; 897 int error, allerror = 0; 898 899 fs = ump->um_e2fs; 900 if (fs->s_dirt != 0 && fs->s_rd_only != 0) { /* XXX */ 901 printf("fs = %s\n", fs->fs_fsmnt); 902 panic("ext2_sync: rofs mod"); 903 } 904 /* 905 * Write back each (modified) inode. 906 */ 907 simple_lock(&mntvnode_slock); 908loop: 909 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { 910 /* 911 * If the vnode that we are about to sync is no longer 912 * associated with this mount point, start over. 913 */ 914 if (vp->v_mount != mp) 915 goto loop; 916 simple_lock(&vp->v_interlock); 917 nvp = vp->v_mntvnodes.le_next; 918 ip = VTOI(vp); 919 if (vp->v_type == VNON || 920 (ip->i_flag & 921 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 && 922 (vp->v_dirtyblkhd.lh_first == NULL || 923 waitfor == MNT_LAZY)) { 924 simple_unlock(&vp->v_interlock); 925 continue; 926 } 927 simple_unlock(&mntvnode_slock); 928 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p); 929 if (error) { 930 simple_lock(&mntvnode_slock); 931 if (error == ENOENT) 932 goto loop; 933 continue; 934 } 935 if (error = VOP_FSYNC(vp, cred, waitfor, p)) 936 allerror = error; 937 VOP_UNLOCK(vp, 0, p); 938 vrele(vp); 939 simple_lock(&mntvnode_slock); 940 } 941 simple_unlock(&mntvnode_slock); 942 /* 943 * Force stale file system control information to be flushed. 944 */ 945 if (waitfor != MNT_LAZY) { 946 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p); 947 if ((error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p)) != 0) 948 allerror = error; 949 VOP_UNLOCK(ump->um_devvp, 0, p); 950 } 951#if QUOTA 952 qsync(mp); 953#endif 954 /* 955 * Write back modified superblock. 956 */ 957 if (fs->s_dirt != 0) { 958 fs->s_dirt = 0; 959 fs->s_es->s_wtime = time_second; 960 if ((error = ext2_sbupdate(ump, waitfor)) != 0) 961 allerror = error; 962 } 963 return (allerror); 964} 965 966/* 967 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it 968 * in from disk. If it is in core, wait for the lock bit to clear, then 969 * return the inode locked. Detection and handling of mount points must be 970 * done by the calling routine. 971 */ 972static int 973ext2_vget(mp, ino, vpp) 974 struct mount *mp; 975 ino_t ino; 976 struct vnode **vpp; 977{ 978 register struct ext2_sb_info *fs; 979 register struct inode *ip; 980 struct ufsmount *ump; 981 struct buf *bp; 982 struct vnode *vp; 983 dev_t dev; 984 int i, error; 985 int used_blocks; 986 987 ump = VFSTOUFS(mp); 988 dev = ump->um_dev; 989restart: 990 if ((*vpp = ufs_ihashget(dev, ino)) != NULL) 991 return (0); 992 993 /* 994 * Lock out the creation of new entries in the FFS hash table in 995 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate 996 * may occur! 997 */ 998 if (ext2fs_inode_hash_lock) { 999 while (ext2fs_inode_hash_lock) { 1000 ext2fs_inode_hash_lock = -1; 1001 tsleep(&ext2fs_inode_hash_lock, PVM, "e2vget", 0); 1002 } 1003 goto restart; 1004 } 1005 ext2fs_inode_hash_lock = 1; 1006 1007 /* 1008 * If this MALLOC() is performed after the getnewvnode() 1009 * it might block, leaving a vnode with a NULL v_data to be 1010 * found by ext2_sync() if a sync happens to fire right then, 1011 * which will cause a panic because ext2_sync() blindly 1012 * dereferences vp->v_data (as well it should). 1013 */ 1014 MALLOC(ip, struct inode *, sizeof(struct inode), M_EXT2NODE, M_WAITOK); 1015 1016 /* Allocate a new vnode/inode. */ 1017 if (error = getnewvnode(VT_UFS, mp, ext2_vnodeop_p, &vp)) { 1018 if (ext2fs_inode_hash_lock < 0) 1019 wakeup(&ext2fs_inode_hash_lock); 1020 ext2fs_inode_hash_lock = 0; 1021 *vpp = NULL; 1022 FREE(ip, M_EXT2NODE); 1023 return (error); 1024 } 1025 bzero((caddr_t)ip, sizeof(struct inode)); 1026 lockinit(&ip->i_lock, PINOD, "ext2in", 0, 0); 1027 vp->v_data = ip; 1028 ip->i_vnode = vp; 1029 ip->i_e2fs = fs = ump->um_e2fs; 1030 ip->i_dev = dev; 1031 ip->i_number = ino; 1032#if QUOTA 1033 for (i = 0; i < MAXQUOTAS; i++) 1034 ip->i_dquot[i] = NODQUOT; 1035#endif 1036 /* 1037 * Put it onto its hash chain and lock it so that other requests for 1038 * this inode will block if they arrive while we are sleeping waiting 1039 * for old data structures to be purged or for the contents of the 1040 * disk portion of this inode to be read. 1041 */ 1042 ufs_ihashins(ip); 1043 1044 if (ext2fs_inode_hash_lock < 0) 1045 wakeup(&ext2fs_inode_hash_lock); 1046 ext2fs_inode_hash_lock = 0; 1047 1048 /* Read in the disk contents for the inode, copy into the inode. */ 1049#if 0 1050printf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino))); 1051#endif 1052 if (error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)), 1053 (int)fs->s_blocksize, NOCRED, &bp)) { 1054 /* 1055 * The inode does not contain anything useful, so it would 1056 * be misleading to leave it on its hash chain. With mode 1057 * still zero, it will be unlinked and returned to the free 1058 * list by vput(). 1059 */ 1060 vput(vp); 1061 brelse(bp); 1062 *vpp = NULL; 1063 return (error); 1064 } 1065 /* convert ext2 inode to dinode */ 1066 ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE * 1067 ino_to_fsbo(fs, ino)), &ip->i_din); 1068 ip->i_block_group = ino_to_cg(fs, ino); 1069 ip->i_next_alloc_block = 0; 1070 ip->i_next_alloc_goal = 0; 1071 ip->i_prealloc_count = 0; 1072 ip->i_prealloc_block = 0; 1073 /* now we want to make sure that block pointers for unused 1074 blocks are zeroed out - ext2_balloc depends on this 1075 although for regular files and directories only 1076 */ 1077 if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) { 1078 used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize; 1079 for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++) 1080 ip->i_db[i] = 0; 1081 } 1082/* 1083 ext2_print_inode(ip); 1084*/ 1085 brelse(bp); 1086 1087 /* 1088 * Initialize the vnode from the inode, check for aliases. 1089 * Note that the underlying vnode may have changed. 1090 */ 1091 if (error = ufs_vinit(mp, ext2_specop_p, ext2_fifoop_p, &vp)) { 1092 vput(vp); 1093 *vpp = NULL; 1094 return (error); 1095 } 1096 /* 1097 * Finish inode initialization now that aliasing has been resolved. 1098 */ 1099 ip->i_devvp = ump->um_devvp; 1100 VREF(ip->i_devvp); 1101 /* 1102 * Set up a generation number for this inode if it does not 1103 * already have one. This should only happen on old filesystems. 1104 */ 1105 if (ip->i_gen == 0) { 1106 ip->i_gen = random() / 2 + 1; 1107 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) 1108 ip->i_flag |= IN_MODIFIED; 1109 } 1110 *vpp = vp; 1111 return (0); 1112} 1113 1114/* 1115 * File handle to vnode 1116 * 1117 * Have to be really careful about stale file handles: 1118 * - check that the inode number is valid 1119 * - call ext2_vget() to get the locked inode 1120 * - check for an unallocated inode (i_mode == 0) 1121 * - check that the given client host has export rights and return 1122 * those rights via. exflagsp and credanonp 1123 */ 1124static int 1125ext2_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp) 1126 register struct mount *mp; 1127 struct fid *fhp; 1128 struct sockaddr *nam; 1129 struct vnode **vpp; 1130 int *exflagsp; 1131 struct ucred **credanonp; 1132{ 1133 register struct ufid *ufhp; 1134 struct ext2_sb_info *fs; 1135 1136 ufhp = (struct ufid *)fhp; 1137 fs = VFSTOUFS(mp)->um_e2fs; 1138 if (ufhp->ufid_ino < ROOTINO || 1139 ufhp->ufid_ino >= fs->s_groups_count * fs->s_es->s_inodes_per_group) 1140 return (ESTALE); 1141 return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp)); 1142} 1143 1144/* 1145 * Vnode pointer to File handle 1146 */ 1147/* ARGSUSED */ 1148static int 1149ext2_vptofh(vp, fhp) 1150 struct vnode *vp; 1151 struct fid *fhp; 1152{ 1153 register struct inode *ip; 1154 register struct ufid *ufhp; 1155 1156 ip = VTOI(vp); 1157 ufhp = (struct ufid *)fhp; 1158 ufhp->ufid_len = sizeof(struct ufid); 1159 ufhp->ufid_ino = ip->i_number; 1160 ufhp->ufid_gen = ip->i_gen; 1161 return (0); 1162} 1163 1164/* 1165 * Write a superblock and associated information back to disk. 1166 */ 1167static int 1168ext2_sbupdate(mp, waitfor) 1169 struct ufsmount *mp; 1170 int waitfor; 1171{ 1172 register struct ext2_sb_info *fs = mp->um_e2fs; 1173 register struct ext2_super_block *es = fs->s_es; 1174 register struct buf *bp; 1175 int i, error = 0; 1176/* 1177printf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no"); 1178*/ 1179 bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0); 1180 bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block)); 1181 if (waitfor == MNT_WAIT) 1182 error = bwrite(bp); 1183 else 1184 bawrite(bp); 1185 1186 /* 1187 * The buffers for group descriptors, inode bitmaps and block bitmaps 1188 * are not busy at this point and are (hopefully) written by the 1189 * usual sync mechanism. No need to write them here 1190 */ 1191 1192 return (error); 1193} 1194