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