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