| 1 | /* |
| 2 | * Copyright (c) 1989, 1991, 1993, 1994 |
| 3 | * The Regents of the University of California. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. All advertising materials mentioning features or use of this software |
| 14 | * must display the following acknowledgement: |
| 15 | * This product includes software developed by the University of |
| 16 | * California, Berkeley and its contributors. |
| 17 | * 4. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | * |
| 33 | * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95 |
| 34 | * $FreeBSD: src/sys/ufs/ffs/ffs_vfsops.c,v 1.117.2.10 2002/06/23 22:34:52 iedowse Exp $ |
| 35 | * $DragonFly: src/sys/vfs/ufs/ffs_vfsops.c,v 1.59 2008/09/17 21:44:25 dillon Exp $ |
| 36 | */ |
| 37 | |
| 38 | #include "opt_quota.h" |
| 39 | |
| 40 | #include <sys/disk.h> |
| 41 | #include <sys/param.h> |
| 42 | #include <sys/systm.h> |
| 43 | #include <sys/proc.h> |
| 44 | #include <sys/nlookup.h> |
| 45 | #include <sys/kernel.h> |
| 46 | #include <sys/vnode.h> |
| 47 | #include <sys/mount.h> |
| 48 | #include <sys/buf.h> |
| 49 | #include <sys/conf.h> |
| 50 | #include <sys/fcntl.h> |
| 51 | #include <sys/diskslice.h> |
| 52 | #include <sys/malloc.h> |
| 53 | |
| 54 | #include "quota.h" |
| 55 | #include "ufsmount.h" |
| 56 | #include "inode.h" |
| 57 | #include "ufs_extern.h" |
| 58 | |
| 59 | #include "fs.h" |
| 60 | #include "ffs_extern.h" |
| 61 | |
| 62 | #include <vm/vm.h> |
| 63 | #include <vm/vm_page.h> |
| 64 | #include <vm/vm_zone.h> |
| 65 | |
| 66 | static MALLOC_DEFINE(M_FFSNODE, "FFS node", "FFS vnode private part"); |
| 67 | |
| 68 | static int ffs_sbupdate (struct ufsmount *, int); |
| 69 | static int ffs_reload (struct mount *, struct ucred *); |
| 70 | static int ffs_oldfscompat (struct fs *); |
| 71 | static int ffs_mount (struct mount *, char *, caddr_t, struct ucred *); |
| 72 | static int ffs_init (struct vfsconf *); |
| 73 | |
| 74 | static struct vfsops ufs_vfsops = { |
| 75 | .vfs_mount = ffs_mount, |
| 76 | .vfs_unmount = ffs_unmount, |
| 77 | .vfs_root = ufs_root, |
| 78 | .vfs_quotactl = ufs_quotactl, |
| 79 | .vfs_statfs = ffs_statfs, |
| 80 | .vfs_sync = ffs_sync, |
| 81 | .vfs_vget = ffs_vget, |
| 82 | .vfs_fhtovp = ffs_fhtovp, |
| 83 | .vfs_checkexp = ufs_check_export, |
| 84 | .vfs_vptofh = ffs_vptofh, |
| 85 | .vfs_init = ffs_init, |
| 86 | .vfs_uninit = ufs_uninit |
| 87 | }; |
| 88 | |
| 89 | VFS_SET(ufs_vfsops, ufs, 0); |
| 90 | |
| 91 | extern struct vop_ops ffs_vnode_vops; |
| 92 | extern struct vop_ops ffs_spec_vops; |
| 93 | extern struct vop_ops ffs_fifo_vops; |
| 94 | |
| 95 | /* |
| 96 | * ffs_mount |
| 97 | * |
| 98 | * Called when mounting local physical media |
| 99 | * |
| 100 | * PARAMETERS: |
| 101 | * mountroot |
| 102 | * mp mount point structure |
| 103 | * path NULL (flag for root mount!!!) |
| 104 | * data <unused> |
| 105 | * p process (user credentials check [statfs]) |
| 106 | * |
| 107 | * mount |
| 108 | * mp mount point structure |
| 109 | * path path to mount point |
| 110 | * data pointer to argument struct in user space |
| 111 | * p process (user credentials check) |
| 112 | * |
| 113 | * RETURNS: 0 Success |
| 114 | * !0 error number (errno.h) |
| 115 | * |
| 116 | * LOCK STATE: |
| 117 | * |
| 118 | * ENTRY |
| 119 | * mount point is locked |
| 120 | * EXIT |
| 121 | * mount point is locked |
| 122 | * |
| 123 | * NOTES: |
| 124 | * A NULL path can be used for a flag since the mount |
| 125 | * system call will fail with EFAULT in copyinstr in |
| 126 | * nlookup() if it is a genuine NULL from the user. |
| 127 | */ |
| 128 | static int |
| 129 | ffs_mount(struct mount *mp, /* mount struct pointer */ |
| 130 | char *path, /* path to mount point */ |
| 131 | caddr_t data, /* arguments to FS specific mount */ |
| 132 | struct ucred *cred) /* process requesting mount */ |
| 133 | { |
| 134 | size_t size; |
| 135 | int error; |
| 136 | struct vnode *devvp; |
| 137 | |
| 138 | struct ufs_args args; |
| 139 | struct ufsmount *ump = 0; |
| 140 | struct fs *fs; |
| 141 | int flags, ronly = 0; |
| 142 | mode_t accessmode; |
| 143 | struct nlookupdata nd; |
| 144 | struct vnode *rootvp; |
| 145 | |
| 146 | devvp = NULL; |
| 147 | error = 0; |
| 148 | |
| 149 | /* |
| 150 | * Use NULL path to flag a root mount |
| 151 | */ |
| 152 | if (path == NULL) { |
| 153 | /* |
| 154 | *** |
| 155 | * Mounting root filesystem |
| 156 | *** |
| 157 | */ |
| 158 | |
| 159 | if ((error = bdevvp(rootdev, &rootvp))) { |
| 160 | kprintf("ffs_mountroot: can't find rootvp\n"); |
| 161 | return (error); |
| 162 | } |
| 163 | |
| 164 | if( ( error = ffs_mountfs(rootvp, mp, M_FFSNODE)) != 0) { |
| 165 | /* fs specific cleanup (if any)*/ |
| 166 | goto error_1; |
| 167 | } |
| 168 | devvp = rootvp; |
| 169 | |
| 170 | goto dostatfs; /* success*/ |
| 171 | |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | *** |
| 176 | * Mounting non-root filesystem or updating a filesystem |
| 177 | *** |
| 178 | */ |
| 179 | |
| 180 | /* copy in user arguments*/ |
| 181 | error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args)); |
| 182 | if (error) |
| 183 | goto error_1; /* can't get arguments*/ |
| 184 | |
| 185 | /* |
| 186 | * If updating, check whether changing from read-only to |
| 187 | * read/write; if there is no device name, that's all we do. |
| 188 | */ |
| 189 | if (mp->mnt_flag & MNT_UPDATE) { |
| 190 | ump = VFSTOUFS(mp); |
| 191 | fs = ump->um_fs; |
| 192 | devvp = ump->um_devvp; |
| 193 | error = 0; |
| 194 | ronly = fs->fs_ronly; /* MNT_RELOAD might change this */ |
| 195 | if (ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) { |
| 196 | /* |
| 197 | * Flush any dirty data. |
| 198 | */ |
| 199 | VFS_SYNC(mp, MNT_WAIT); |
| 200 | /* |
| 201 | * Check for and optionally get rid of files open |
| 202 | * for writing. |
| 203 | */ |
| 204 | flags = WRITECLOSE; |
| 205 | if (mp->mnt_flag & MNT_FORCE) |
| 206 | flags |= FORCECLOSE; |
| 207 | if (mp->mnt_flag & MNT_SOFTDEP) { |
| 208 | error = softdep_flushfiles(mp, flags); |
| 209 | } else { |
| 210 | error = ffs_flushfiles(mp, flags); |
| 211 | } |
| 212 | ronly = 1; |
| 213 | } |
| 214 | if (!error && (mp->mnt_flag & MNT_RELOAD)) { |
| 215 | error = ffs_reload(mp, NULL); |
| 216 | } |
| 217 | if (error) { |
| 218 | goto error_1; |
| 219 | } |
| 220 | if (ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) { |
| 221 | /* |
| 222 | * If upgrade to read-write by non-root, then verify |
| 223 | * that user has necessary permissions on the device. |
| 224 | */ |
| 225 | if (cred->cr_uid != 0) { |
| 226 | vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); |
| 227 | if ((error = VOP_EACCESS(devvp, VREAD | VWRITE, |
| 228 | cred)) != 0) { |
| 229 | vn_unlock(devvp); |
| 230 | return (error); |
| 231 | } |
| 232 | vn_unlock(devvp); |
| 233 | } |
| 234 | |
| 235 | fs->fs_flags &= ~FS_UNCLEAN; |
| 236 | if (fs->fs_clean == 0) { |
| 237 | fs->fs_flags |= FS_UNCLEAN; |
| 238 | if (mp->mnt_flag & MNT_FORCE) { |
| 239 | kprintf( |
| 240 | "WARNING: %s was not properly dismounted\n", |
| 241 | fs->fs_fsmnt); |
| 242 | } else { |
| 243 | kprintf( |
| 244 | "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n", |
| 245 | fs->fs_fsmnt); |
| 246 | error = EPERM; |
| 247 | goto error_1; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /* check to see if we need to start softdep */ |
| 252 | if (fs->fs_flags & FS_DOSOFTDEP) { |
| 253 | error = softdep_mount(devvp, mp, fs); |
| 254 | if (error) |
| 255 | goto error_1; |
| 256 | } |
| 257 | ronly = 0; |
| 258 | } |
| 259 | /* |
| 260 | * Soft updates is incompatible with "async", |
| 261 | * so if we are doing softupdates stop the user |
| 262 | * from setting the async flag in an update. |
| 263 | * Softdep_mount() clears it in an initial mount |
| 264 | * or ro->rw remount. |
| 265 | */ |
| 266 | if (mp->mnt_flag & MNT_SOFTDEP) { |
| 267 | mp->mnt_flag &= ~MNT_ASYNC; |
| 268 | } |
| 269 | /* if not updating name...*/ |
| 270 | if (args.fspec == 0) { |
| 271 | /* |
| 272 | * Process export requests. Jumping to "success" |
| 273 | * will return the vfs_export() error code. |
| 274 | */ |
| 275 | error = vfs_export(mp, &ump->um_export, &args.export); |
| 276 | goto success; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * Not an update, or updating the name: look up the name |
| 282 | * and verify that it refers to a sensible block device. |
| 283 | */ |
| 284 | devvp = NULL; |
| 285 | error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW); |
| 286 | if (error == 0) |
| 287 | error = nlookup(&nd); |
| 288 | if (error == 0) |
| 289 | error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp); |
| 290 | nlookup_done(&nd); |
| 291 | if (error) |
| 292 | goto error_1; |
| 293 | |
| 294 | if (!vn_isdisk(devvp, &error)) |
| 295 | goto error_2; |
| 296 | |
| 297 | /* |
| 298 | * If mount by non-root, then verify that user has necessary |
| 299 | * permissions on the device. |
| 300 | */ |
| 301 | if (cred->cr_uid != 0) { |
| 302 | accessmode = VREAD; |
| 303 | if ((mp->mnt_flag & MNT_RDONLY) == 0) |
| 304 | accessmode |= VWRITE; |
| 305 | vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); |
| 306 | if ((error = VOP_EACCESS(devvp, accessmode, cred)) != 0) { |
| 307 | vput(devvp); |
| 308 | return (error); |
| 309 | } |
| 310 | vn_unlock(devvp); |
| 311 | } |
| 312 | |
| 313 | if (mp->mnt_flag & MNT_UPDATE) { |
| 314 | /* |
| 315 | * UPDATE - make sure the resolved vnode represents the same |
| 316 | * device. Note that devvp->v_rdev may be NULL since we |
| 317 | * haven't opened it, so compare udev instead. |
| 318 | * |
| 319 | * Our current open/writecount state is associated with |
| 320 | * um_devvp, so continue using um_devvp and throw away devvp. |
| 321 | */ |
| 322 | if (devvp != ump->um_devvp) { |
| 323 | if (devvp->v_umajor == ump->um_devvp->v_umajor && |
| 324 | devvp->v_uminor == ump->um_devvp->v_uminor) { |
| 325 | vrele(devvp); |
| 326 | devvp = ump->um_devvp; |
| 327 | } else { |
| 328 | kprintf("cannot update mount, udev does" |
| 329 | " not match %08x:%08x vs %08x:%08x\n", |
| 330 | devvp->v_umajor, devvp->v_uminor, |
| 331 | ump->um_devvp->v_umajor, |
| 332 | ump->um_devvp->v_uminor); |
| 333 | error = EINVAL; /* needs translation */ |
| 334 | } |
| 335 | } else { |
| 336 | vrele(devvp); |
| 337 | } |
| 338 | /* |
| 339 | * Update device name only on success |
| 340 | */ |
| 341 | if (!error) { |
| 342 | /* Save "mounted from" info for mount point (NULL pad)*/ |
| 343 | copyinstr( args.fspec, |
| 344 | mp->mnt_stat.f_mntfromname, |
| 345 | MNAMELEN - 1, |
| 346 | &size); |
| 347 | bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); |
| 348 | } |
| 349 | } else { |
| 350 | /* |
| 351 | ******************** |
| 352 | * NEW MOUNT |
| 353 | ******************** |
| 354 | */ |
| 355 | |
| 356 | /* Save "mounted from" info for mount point (NULL pad)*/ |
| 357 | copyinstr( args.fspec, /* device name*/ |
| 358 | mp->mnt_stat.f_mntfromname, /* save area*/ |
| 359 | MNAMELEN - 1, /* max size*/ |
| 360 | &size); /* real size*/ |
| 361 | bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); |
| 362 | |
| 363 | /* Save "last mounted on" info for mount point (NULL pad)*/ |
| 364 | bzero(mp->mnt_stat.f_mntonname, |
| 365 | sizeof(mp->mnt_stat.f_mntonname)); |
| 366 | if (path) { |
| 367 | copyinstr(path, mp->mnt_stat.f_mntonname, |
| 368 | sizeof(mp->mnt_stat.f_mntonname) - 1, |
| 369 | &size); |
| 370 | } |
| 371 | |
| 372 | error = ffs_mountfs(devvp, mp, M_FFSNODE); |
| 373 | } |
| 374 | if (error) { |
| 375 | goto error_2; |
| 376 | } |
| 377 | |
| 378 | dostatfs: |
| 379 | /* |
| 380 | * Initialize FS stat information in mount struct; uses |
| 381 | * mp->mnt_stat.f_mntfromname. |
| 382 | * |
| 383 | * This code is common to root and non-root mounts |
| 384 | */ |
| 385 | (void)VFS_STATFS(mp, &mp->mnt_stat, cred); |
| 386 | |
| 387 | goto success; |
| 388 | |
| 389 | |
| 390 | error_2: /* error with devvp held*/ |
| 391 | |
| 392 | /* release devvp before failing*/ |
| 393 | vrele(devvp); |
| 394 | |
| 395 | error_1: /* no state to back out*/ |
| 396 | |
| 397 | success: |
| 398 | if (!error && path && (mp->mnt_flag & MNT_UPDATE)) { |
| 399 | /* Update clean flag after changing read-onlyness. */ |
| 400 | fs = ump->um_fs; |
| 401 | if (ronly != fs->fs_ronly) { |
| 402 | fs->fs_ronly = ronly; |
| 403 | fs->fs_clean = ronly && |
| 404 | (fs->fs_flags & FS_UNCLEAN) == 0 ? 1 : 0; |
| 405 | |
| 406 | /* |
| 407 | * The device must be re-opened as appropriate or |
| 408 | * the device close at unmount time will panic. |
| 409 | */ |
| 410 | vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); |
| 411 | if (ronly) { |
| 412 | VOP_OPEN(devvp, FREAD, FSCRED, NULL); |
| 413 | VOP_CLOSE(devvp, FREAD|FWRITE); |
| 414 | } else { |
| 415 | VOP_OPEN(devvp, FREAD|FWRITE, FSCRED, NULL); |
| 416 | VOP_CLOSE(devvp, FREAD); |
| 417 | } |
| 418 | vn_unlock(devvp); |
| 419 | ffs_sbupdate(ump, MNT_WAIT); |
| 420 | } |
| 421 | } |
| 422 | return (error); |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | * Reload all incore data for a filesystem (used after running fsck on |
| 427 | * the root filesystem and finding things to fix). The filesystem must |
| 428 | * be mounted read-only. |
| 429 | * |
| 430 | * Things to do to update the mount: |
| 431 | * 1) invalidate all cached meta-data. |
| 432 | * 2) re-read superblock from disk. |
| 433 | * 3) re-read summary information from disk. |
| 434 | * 4) invalidate all inactive vnodes. |
| 435 | * 5) invalidate all cached file data. |
| 436 | * 6) re-read inode data for all active vnodes. |
| 437 | */ |
| 438 | |
| 439 | static int ffs_reload_scan2(struct mount *mp, struct vnode *vp, void *data); |
| 440 | |
| 441 | struct scaninfo { |
| 442 | int rescan; |
| 443 | struct fs *fs; |
| 444 | struct vnode *devvp; |
| 445 | int waitfor; |
| 446 | int allerror; |
| 447 | }; |
| 448 | |
| 449 | static int |
| 450 | ffs_reload(struct mount *mp, struct ucred *cred) |
| 451 | { |
| 452 | struct vnode *devvp; |
| 453 | void *space; |
| 454 | struct buf *bp; |
| 455 | struct fs *fs, *newfs; |
| 456 | struct partinfo dpart; |
| 457 | cdev_t dev; |
| 458 | int i, blks, size, error; |
| 459 | struct scaninfo scaninfo; |
| 460 | int32_t *lp; |
| 461 | |
| 462 | if ((mp->mnt_flag & MNT_RDONLY) == 0) |
| 463 | return (EINVAL); |
| 464 | /* |
| 465 | * Step 1: invalidate all cached meta-data. |
| 466 | */ |
| 467 | devvp = VFSTOUFS(mp)->um_devvp; |
| 468 | vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); |
| 469 | error = vinvalbuf(devvp, 0, 0, 0); |
| 470 | vn_unlock(devvp); |
| 471 | if (error) |
| 472 | panic("ffs_reload: dirty1"); |
| 473 | |
| 474 | dev = devvp->v_rdev; |
| 475 | |
| 476 | /* |
| 477 | * The backing device must be VMIO-capable because we use getblk(). |
| 478 | * NOTE: the MFS driver now returns a VMIO-enabled descriptor. |
| 479 | */ |
| 480 | if (devvp->v_object == NULL) |
| 481 | panic("ffs_reload: devvp has no VM object!"); |
| 482 | |
| 483 | /* |
| 484 | * Step 2: re-read superblock from disk. |
| 485 | */ |
| 486 | if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, |
| 487 | cred, NULL) != 0) { |
| 488 | size = DEV_BSIZE; |
| 489 | } else { |
| 490 | size = dpart.media_blksize; |
| 491 | } |
| 492 | if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0) { |
| 493 | brelse(bp); |
| 494 | return (error); |
| 495 | } |
| 496 | newfs = (struct fs *)bp->b_data; |
| 497 | if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE || |
| 498 | newfs->fs_bsize < sizeof(struct fs)) { |
| 499 | brelse(bp); |
| 500 | return (EIO); /* XXX needs translation */ |
| 501 | } |
| 502 | fs = VFSTOUFS(mp)->um_fs; |
| 503 | /* |
| 504 | * Copy pointer fields back into superblock before copying in XXX |
| 505 | * new superblock. These should really be in the ufsmount. XXX |
| 506 | * Note that important parameters (eg fs_ncg) are unchanged. |
| 507 | */ |
| 508 | newfs->fs_csp = fs->fs_csp; |
| 509 | newfs->fs_maxcluster = fs->fs_maxcluster; |
| 510 | newfs->fs_contigdirs = fs->fs_contigdirs; |
| 511 | /* The filesystem is still read-only. */ |
| 512 | newfs->fs_ronly = 1; |
| 513 | bcopy(newfs, fs, (uint)fs->fs_sbsize); |
| 514 | if (fs->fs_sbsize < SBSIZE) |
| 515 | bp->b_flags |= B_INVAL; |
| 516 | brelse(bp); |
| 517 | mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen; |
| 518 | ffs_oldfscompat(fs); |
| 519 | /* An old fsck may have zeroed these fields, so recheck them. */ |
| 520 | if (fs->fs_avgfilesize <= 0) /* XXX */ |
| 521 | fs->fs_avgfilesize = AVFILESIZ; /* XXX */ |
| 522 | if (fs->fs_avgfpdir <= 0) /* XXX */ |
| 523 | fs->fs_avgfpdir = AFPDIR; /* XXX */ |
| 524 | |
| 525 | /* |
| 526 | * Step 3: re-read summary information from disk. |
| 527 | */ |
| 528 | blks = howmany(fs->fs_cssize, fs->fs_fsize); |
| 529 | space = fs->fs_csp; |
| 530 | for (i = 0; i < blks; i += fs->fs_frag) { |
| 531 | size = fs->fs_bsize; |
| 532 | if (i + fs->fs_frag > blks) |
| 533 | size = (blks - i) * fs->fs_fsize; |
| 534 | error = bread(devvp, fsbtodoff(fs, fs->fs_csaddr + i), size, &bp); |
| 535 | if (error) { |
| 536 | brelse(bp); |
| 537 | return (error); |
| 538 | } |
| 539 | bcopy(bp->b_data, space, (uint)size); |
| 540 | space = (char *)space + size; |
| 541 | brelse(bp); |
| 542 | } |
| 543 | /* |
| 544 | * We no longer know anything about clusters per cylinder group. |
| 545 | */ |
| 546 | if (fs->fs_contigsumsize > 0) { |
| 547 | lp = fs->fs_maxcluster; |
| 548 | for (i = 0; i < fs->fs_ncg; i++) |
| 549 | *lp++ = fs->fs_contigsumsize; |
| 550 | } |
| 551 | |
| 552 | scaninfo.rescan = 0; |
| 553 | scaninfo.fs = fs; |
| 554 | scaninfo.devvp = devvp; |
| 555 | while (error == 0 && scaninfo.rescan) { |
| 556 | scaninfo.rescan = 0; |
| 557 | error = vmntvnodescan(mp, VMSC_GETVX, |
| 558 | NULL, ffs_reload_scan2, &scaninfo); |
| 559 | } |
| 560 | return(error); |
| 561 | } |
| 562 | |
| 563 | static int |
| 564 | ffs_reload_scan2(struct mount *mp, struct vnode *vp, void *data) |
| 565 | { |
| 566 | struct scaninfo *info = data; |
| 567 | struct inode *ip; |
| 568 | struct buf *bp; |
| 569 | int error; |
| 570 | |
| 571 | /* |
| 572 | * Try to recycle |
| 573 | */ |
| 574 | if (vrecycle(vp)) |
| 575 | return(0); |
| 576 | |
| 577 | if (vinvalbuf(vp, 0, 0, 0)) |
| 578 | panic("ffs_reload: dirty2"); |
| 579 | /* |
| 580 | * Step 6: re-read inode data for all active vnodes. |
| 581 | */ |
| 582 | ip = VTOI(vp); |
| 583 | error = bread(info->devvp, |
| 584 | fsbtodoff(info->fs, ino_to_fsba(info->fs, ip->i_number)), |
| 585 | (int)info->fs->fs_bsize, &bp); |
| 586 | if (error) { |
| 587 | brelse(bp); |
| 588 | return (error); |
| 589 | } |
| 590 | ip->i_din = *((struct ufs1_dinode *)bp->b_data + |
| 591 | ino_to_fsbo(info->fs, ip->i_number)); |
| 592 | ip->i_effnlink = ip->i_nlink; |
| 593 | brelse(bp); |
| 594 | return(0); |
| 595 | } |
| 596 | |
| 597 | /* |
| 598 | * Common code for mount and mountroot |
| 599 | */ |
| 600 | int |
| 601 | ffs_mountfs(struct vnode *devvp, struct mount *mp, struct malloc_type *mtype) |
| 602 | { |
| 603 | struct ufsmount *ump; |
| 604 | struct buf *bp; |
| 605 | struct fs *fs; |
| 606 | cdev_t dev; |
| 607 | struct partinfo dpart; |
| 608 | void *space; |
| 609 | int error, i, blks, size, ronly; |
| 610 | int32_t *lp; |
| 611 | uint64_t maxfilesize; /* XXX */ |
| 612 | |
| 613 | /* |
| 614 | * Disallow multiple mounts of the same device. |
| 615 | * Disallow mounting of a device that is currently in use |
| 616 | * Flush out any old buffers remaining from a previous use. |
| 617 | */ |
| 618 | error = vfs_mountedon(devvp); |
| 619 | if (error) |
| 620 | return (error); |
| 621 | if (vcount(devvp) > 0) |
| 622 | return (EBUSY); |
| 623 | vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); |
| 624 | error = vinvalbuf(devvp, V_SAVE, 0, 0); |
| 625 | vn_unlock(devvp); |
| 626 | if (error) |
| 627 | return (error); |
| 628 | |
| 629 | ronly = (mp->mnt_flag & MNT_RDONLY) != 0; |
| 630 | vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); |
| 631 | error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL); |
| 632 | vn_unlock(devvp); |
| 633 | if (error) |
| 634 | return (error); |
| 635 | dev = devvp->v_rdev; |
| 636 | if (dev->si_iosize_max != 0) |
| 637 | mp->mnt_iosize_max = dev->si_iosize_max; |
| 638 | if (mp->mnt_iosize_max > MAXPHYS) |
| 639 | mp->mnt_iosize_max = MAXPHYS; |
| 640 | |
| 641 | /* |
| 642 | * Filesystem supports native FSMIDs |
| 643 | */ |
| 644 | mp->mnt_kern_flag |= MNTK_FSMID; |
| 645 | |
| 646 | /* |
| 647 | * The backing device must be VMIO-capable because we use getblk(). |
| 648 | * NOTE: the MFS driver now returns a VMIO-enabled descriptor. |
| 649 | * The VOP_OPEN() call above should have associated a VM object |
| 650 | * with devvp. |
| 651 | */ |
| 652 | if (devvp->v_object == NULL) |
| 653 | panic("ffs_reload: devvp has no VM object!"); |
| 654 | |
| 655 | if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, |
| 656 | proc0.p_ucred, NULL) != 0) { |
| 657 | size = DEV_BSIZE; |
| 658 | } else { |
| 659 | size = dpart.media_blksize; |
| 660 | } |
| 661 | |
| 662 | bp = NULL; |
| 663 | ump = NULL; |
| 664 | if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0) |
| 665 | goto out; |
| 666 | fs = (struct fs *)bp->b_data; |
| 667 | if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE || |
| 668 | fs->fs_bsize < sizeof(struct fs)) { |
| 669 | error = EINVAL; /* XXX needs translation */ |
| 670 | goto out; |
| 671 | } |
| 672 | fs->fs_fmod = 0; |
| 673 | fs->fs_flags &= ~FS_UNCLEAN; |
| 674 | if (fs->fs_clean == 0) { |
| 675 | fs->fs_flags |= FS_UNCLEAN; |
| 676 | if (ronly || (mp->mnt_flag & MNT_FORCE)) { |
| 677 | kprintf( |
| 678 | "WARNING: %s was not properly dismounted\n", |
| 679 | fs->fs_fsmnt); |
| 680 | } else { |
| 681 | kprintf( |
| 682 | "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n", |
| 683 | fs->fs_fsmnt); |
| 684 | error = EPERM; |
| 685 | goto out; |
| 686 | } |
| 687 | } |
| 688 | /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */ |
| 689 | if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) { |
| 690 | error = EROFS; /* needs translation */ |
| 691 | goto out; |
| 692 | } |
| 693 | ump = kmalloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO); |
| 694 | ump->um_malloctype = mtype; |
| 695 | ump->um_i_effnlink_valid = 1; |
| 696 | ump->um_fs = kmalloc((u_long)fs->fs_sbsize, M_UFSMNT, |
| 697 | M_WAITOK); |
| 698 | bcopy(bp->b_data, ump->um_fs, (uint)fs->fs_sbsize); |
| 699 | if (fs->fs_sbsize < SBSIZE) |
| 700 | bp->b_flags |= B_INVAL; |
| 701 | brelse(bp); |
| 702 | bp = NULL; |
| 703 | fs = ump->um_fs; |
| 704 | fs->fs_ronly = ronly; |
| 705 | size = fs->fs_cssize; |
| 706 | blks = howmany(size, fs->fs_fsize); |
| 707 | if (fs->fs_contigsumsize > 0) |
| 708 | size += fs->fs_ncg * sizeof(int32_t); |
| 709 | size += fs->fs_ncg * sizeof(uint8_t); |
| 710 | space = kmalloc((u_long)size, M_UFSMNT, M_WAITOK); |
| 711 | fs->fs_csp = space; |
| 712 | for (i = 0; i < blks; i += fs->fs_frag) { |
| 713 | size = fs->fs_bsize; |
| 714 | if (i + fs->fs_frag > blks) |
| 715 | size = (blks - i) * fs->fs_fsize; |
| 716 | if ((error = bread(devvp, fsbtodoff(fs, fs->fs_csaddr + i), |
| 717 | size, &bp)) != 0) { |
| 718 | kfree(fs->fs_csp, M_UFSMNT); |
| 719 | goto out; |
| 720 | } |
| 721 | bcopy(bp->b_data, space, (uint)size); |
| 722 | space = (char *)space + size; |
| 723 | brelse(bp); |
| 724 | bp = NULL; |
| 725 | } |
| 726 | if (fs->fs_contigsumsize > 0) { |
| 727 | fs->fs_maxcluster = lp = space; |
| 728 | for (i = 0; i < fs->fs_ncg; i++) |
| 729 | *lp++ = fs->fs_contigsumsize; |
| 730 | space = lp; |
| 731 | } |
| 732 | size = fs->fs_ncg * sizeof(uint8_t); |
| 733 | fs->fs_contigdirs = (uint8_t *)space; |
| 734 | bzero(fs->fs_contigdirs, size); |
| 735 | /* Compatibility for old filesystems XXX */ |
| 736 | if (fs->fs_avgfilesize <= 0) /* XXX */ |
| 737 | fs->fs_avgfilesize = AVFILESIZ; /* XXX */ |
| 738 | if (fs->fs_avgfpdir <= 0) /* XXX */ |
| 739 | fs->fs_avgfpdir = AFPDIR; /* XXX */ |
| 740 | mp->mnt_data = (qaddr_t)ump; |
| 741 | mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0]; |
| 742 | mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1]; |
| 743 | if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 || |
| 744 | vfs_getvfs(&mp->mnt_stat.f_fsid)) |
| 745 | vfs_getnewfsid(mp); |
| 746 | mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen; |
| 747 | mp->mnt_flag |= MNT_LOCAL; |
| 748 | ump->um_mountp = mp; |
| 749 | ump->um_dev = dev; |
| 750 | ump->um_devvp = devvp; |
| 751 | ump->um_nindir = fs->fs_nindir; |
| 752 | ump->um_bptrtodb = fs->fs_fsbtodb; |
| 753 | ump->um_seqinc = fs->fs_frag; |
| 754 | for (i = 0; i < MAXQUOTAS; i++) |
| 755 | ump->um_quotas[i] = NULLVP; |
| 756 | dev->si_mountpoint = mp; |
| 757 | ffs_oldfscompat(fs); |
| 758 | |
| 759 | /* restore "last mounted on" here */ |
| 760 | bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt)); |
| 761 | ksnprintf(fs->fs_fsmnt, sizeof(fs->fs_fsmnt), |
| 762 | "%s", mp->mnt_stat.f_mntonname); |
| 763 | |
| 764 | if( mp->mnt_flag & MNT_ROOTFS) { |
| 765 | /* |
| 766 | * Root mount; update timestamp in mount structure. |
| 767 | * this will be used by the common root mount code |
| 768 | * to update the system clock. |
| 769 | */ |
| 770 | mp->mnt_time = fs->fs_time; |
| 771 | } |
| 772 | |
| 773 | ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */ |
| 774 | maxfilesize = (uint64_t)0x40000000 * fs->fs_bsize - 1; /* XXX */ |
| 775 | /* Enforce limit caused by vm object backing (32 bits vm_pindex_t). */ |
| 776 | if (maxfilesize > (uint64_t)0x80000000u * PAGE_SIZE - 1) |
| 777 | maxfilesize = (uint64_t)0x80000000u * PAGE_SIZE - 1; |
| 778 | if (fs->fs_maxfilesize > maxfilesize) /* XXX */ |
| 779 | fs->fs_maxfilesize = maxfilesize; /* XXX */ |
| 780 | if (ronly == 0) { |
| 781 | if ((fs->fs_flags & FS_DOSOFTDEP) && |
| 782 | (error = softdep_mount(devvp, mp, fs)) != 0) { |
| 783 | kfree(fs->fs_csp, M_UFSMNT); |
| 784 | goto out; |
| 785 | } |
| 786 | fs->fs_fmod = 1; |
| 787 | fs->fs_clean = 0; |
| 788 | (void) ffs_sbupdate(ump, MNT_WAIT); |
| 789 | } |
| 790 | vfs_add_vnodeops(mp, &ffs_vnode_vops, &mp->mnt_vn_norm_ops); |
| 791 | vfs_add_vnodeops(mp, &ffs_spec_vops, &mp->mnt_vn_spec_ops); |
| 792 | vfs_add_vnodeops(mp, &ffs_fifo_vops, &mp->mnt_vn_fifo_ops); |
| 793 | |
| 794 | return (0); |
| 795 | out: |
| 796 | dev->si_mountpoint = NULL; |
| 797 | if (bp) |
| 798 | brelse(bp); |
| 799 | VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE); |
| 800 | if (ump) { |
| 801 | kfree(ump->um_fs, M_UFSMNT); |
| 802 | kfree(ump, M_UFSMNT); |
| 803 | mp->mnt_data = (qaddr_t)0; |
| 804 | } |
| 805 | return (error); |
| 806 | } |
| 807 | |
| 808 | /* |
| 809 | * Sanity checks for old filesystems. |
| 810 | * |
| 811 | * XXX - goes away some day. |
| 812 | */ |
| 813 | static int |
| 814 | ffs_oldfscompat(struct fs *fs) |
| 815 | { |
| 816 | fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */ |
| 817 | fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */ |
| 818 | if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */ |
| 819 | fs->fs_nrpos = 8; /* XXX */ |
| 820 | if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */ |
| 821 | #if 0 |
| 822 | int i; /* XXX */ |
| 823 | uint64_t sizepb = fs->fs_bsize; /* XXX */ |
| 824 | /* XXX */ |
| 825 | fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */ |
| 826 | for (i = 0; i < NIADDR; i++) { /* XXX */ |
| 827 | sizepb *= NINDIR(fs); /* XXX */ |
| 828 | fs->fs_maxfilesize += sizepb; /* XXX */ |
| 829 | } /* XXX */ |
| 830 | #endif |
| 831 | fs->fs_maxfilesize = (u_quad_t) 1LL << 39; |
| 832 | fs->fs_qbmask = ~fs->fs_bmask; /* XXX */ |
| 833 | fs->fs_qfmask = ~fs->fs_fmask; /* XXX */ |
| 834 | } /* XXX */ |
| 835 | return (0); |
| 836 | } |
| 837 | |
| 838 | /* |
| 839 | * unmount system call |
| 840 | */ |
| 841 | int |
| 842 | ffs_unmount(struct mount *mp, int mntflags) |
| 843 | { |
| 844 | struct ufsmount *ump; |
| 845 | struct fs *fs; |
| 846 | int error, flags; |
| 847 | |
| 848 | flags = 0; |
| 849 | if (mntflags & MNT_FORCE) { |
| 850 | flags |= FORCECLOSE; |
| 851 | } |
| 852 | if (mp->mnt_flag & MNT_SOFTDEP) { |
| 853 | if ((error = softdep_flushfiles(mp, flags)) != 0) |
| 854 | return (error); |
| 855 | } else { |
| 856 | if ((error = ffs_flushfiles(mp, flags)) != 0) |
| 857 | return (error); |
| 858 | } |
| 859 | ump = VFSTOUFS(mp); |
| 860 | fs = ump->um_fs; |
| 861 | if (fs->fs_ronly == 0) { |
| 862 | fs->fs_clean = fs->fs_flags & FS_UNCLEAN ? 0 : 1; |
| 863 | error = ffs_sbupdate(ump, MNT_WAIT); |
| 864 | if (error) { |
| 865 | fs->fs_clean = 0; |
| 866 | return (error); |
| 867 | } |
| 868 | } |
| 869 | ump->um_devvp->v_rdev->si_mountpoint = NULL; |
| 870 | |
| 871 | vinvalbuf(ump->um_devvp, V_SAVE, 0, 0); |
| 872 | error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE); |
| 873 | |
| 874 | vrele(ump->um_devvp); |
| 875 | |
| 876 | kfree(fs->fs_csp, M_UFSMNT); |
| 877 | kfree(fs, M_UFSMNT); |
| 878 | kfree(ump, M_UFSMNT); |
| 879 | mp->mnt_data = (qaddr_t)0; |
| 880 | mp->mnt_flag &= ~MNT_LOCAL; |
| 881 | return (error); |
| 882 | } |
| 883 | |
| 884 | /* |
| 885 | * Flush out all the files in a filesystem. |
| 886 | */ |
| 887 | int |
| 888 | ffs_flushfiles(struct mount *mp, int flags) |
| 889 | { |
| 890 | struct ufsmount *ump; |
| 891 | int error; |
| 892 | |
| 893 | ump = VFSTOUFS(mp); |
| 894 | #ifdef QUOTA |
| 895 | if (mp->mnt_flag & MNT_QUOTA) { |
| 896 | int i; |
| 897 | error = vflush(mp, 0, SKIPSYSTEM|flags); |
| 898 | if (error) |
| 899 | return (error); |
| 900 | /* Find out how many quota files we have open. */ |
| 901 | for (i = 0; i < MAXQUOTAS; i++) { |
| 902 | if (ump->um_quotas[i] == NULLVP) |
| 903 | continue; |
| 904 | ufs_quotaoff(mp, i); |
| 905 | } |
| 906 | /* |
| 907 | * Here we fall through to vflush again to ensure |
| 908 | * that we have gotten rid of all the system vnodes. |
| 909 | */ |
| 910 | } |
| 911 | #endif |
| 912 | /* |
| 913 | * Flush all the files. |
| 914 | */ |
| 915 | if ((error = vflush(mp, 0, flags)) != 0) |
| 916 | return (error); |
| 917 | /* |
| 918 | * Flush filesystem metadata. |
| 919 | */ |
| 920 | vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY); |
| 921 | error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, 0); |
| 922 | vn_unlock(ump->um_devvp); |
| 923 | return (error); |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * Get filesystem statistics. |
| 928 | */ |
| 929 | int |
| 930 | ffs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) |
| 931 | { |
| 932 | struct ufsmount *ump; |
| 933 | struct fs *fs; |
| 934 | |
| 935 | ump = VFSTOUFS(mp); |
| 936 | fs = ump->um_fs; |
| 937 | if (fs->fs_magic != FS_MAGIC) |
| 938 | panic("ffs_statfs"); |
| 939 | sbp->f_bsize = fs->fs_fsize; |
| 940 | sbp->f_iosize = fs->fs_bsize; |
| 941 | sbp->f_blocks = fs->fs_dsize; |
| 942 | sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag + |
| 943 | fs->fs_cstotal.cs_nffree; |
| 944 | sbp->f_bavail = freespace(fs, fs->fs_minfree); |
| 945 | sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO; |
| 946 | sbp->f_ffree = fs->fs_cstotal.cs_nifree; |
| 947 | if (sbp != &mp->mnt_stat) { |
| 948 | sbp->f_type = mp->mnt_vfc->vfc_typenum; |
| 949 | bcopy((caddr_t)mp->mnt_stat.f_mntfromname, |
| 950 | (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); |
| 951 | } |
| 952 | return (0); |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | * Go through the disk queues to initiate sandbagged IO; |
| 957 | * go through the inodes to write those that have been modified; |
| 958 | * initiate the writing of the super block if it has been modified. |
| 959 | * |
| 960 | * Note: we are always called with the filesystem marked `MPBUSY'. |
| 961 | */ |
| 962 | |
| 963 | |
| 964 | static int ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data); |
| 965 | static int ffs_sync_scan2(struct mount *mp, struct vnode *vp, void *data); |
| 966 | |
| 967 | int |
| 968 | ffs_sync(struct mount *mp, int waitfor) |
| 969 | { |
| 970 | struct ufsmount *ump = VFSTOUFS(mp); |
| 971 | struct fs *fs; |
| 972 | int error; |
| 973 | struct scaninfo scaninfo; |
| 974 | |
| 975 | fs = ump->um_fs; |
| 976 | if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */ |
| 977 | kprintf("fs = %s\n", fs->fs_fsmnt); |
| 978 | panic("ffs_sync: rofs mod"); |
| 979 | } |
| 980 | |
| 981 | /* |
| 982 | * Write back each (modified) inode. |
| 983 | */ |
| 984 | scaninfo.allerror = 0; |
| 985 | scaninfo.rescan = 1; |
| 986 | scaninfo.waitfor = waitfor; |
| 987 | while (scaninfo.rescan) { |
| 988 | scaninfo.rescan = 0; |
| 989 | vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT, |
| 990 | ffs_sync_scan1, ffs_sync_scan2, &scaninfo); |
| 991 | } |
| 992 | |
| 993 | /* |
| 994 | * Force stale filesystem control information to be flushed. |
| 995 | */ |
| 996 | if ((waitfor & MNT_LAZY) == 0) { |
| 997 | if (ump->um_mountp->mnt_flag & MNT_SOFTDEP) |
| 998 | waitfor = MNT_NOWAIT; |
| 999 | vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY); |
| 1000 | if ((error = VOP_FSYNC(ump->um_devvp, waitfor, 0)) != 0) |
| 1001 | scaninfo.allerror = error; |
| 1002 | vn_unlock(ump->um_devvp); |
| 1003 | } |
| 1004 | #ifdef QUOTA |
| 1005 | ufs_qsync(mp); |
| 1006 | #endif |
| 1007 | /* |
| 1008 | * Write back modified superblock. |
| 1009 | */ |
| 1010 | if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0) |
| 1011 | scaninfo.allerror = error; |
| 1012 | return (scaninfo.allerror); |
| 1013 | } |
| 1014 | |
| 1015 | static int |
| 1016 | ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data) |
| 1017 | { |
| 1018 | struct inode *ip; |
| 1019 | |
| 1020 | /* |
| 1021 | * Depend on the mount list's vnode lock to keep things stable |
| 1022 | * enough for a quick test. Since there might be hundreds of |
| 1023 | * thousands of vnodes, we cannot afford even a subroutine |
| 1024 | * call unless there's a good chance that we have work to do. |
| 1025 | */ |
| 1026 | ip = VTOI(vp); |
| 1027 | /* Restart out whole search if this guy is locked |
| 1028 | * or is being reclaimed. |
| 1029 | */ |
| 1030 | if (vp->v_type == VNON || ((ip->i_flag & |
| 1031 | (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 && |
| 1032 | RB_EMPTY(&vp->v_rbdirty_tree))) { |
| 1033 | return(-1); |
| 1034 | } |
| 1035 | return(0); |
| 1036 | } |
| 1037 | |
| 1038 | static int |
| 1039 | ffs_sync_scan2(struct mount *mp, struct vnode *vp, void *data) |
| 1040 | { |
| 1041 | struct scaninfo *info = data; |
| 1042 | struct inode *ip; |
| 1043 | int error; |
| 1044 | |
| 1045 | /* |
| 1046 | * We have to recheck after having obtained the vnode interlock. |
| 1047 | */ |
| 1048 | ip = VTOI(vp); |
| 1049 | if (vp->v_type == VNON || vp->v_type == VBAD || |
| 1050 | ((ip->i_flag & |
| 1051 | (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 && |
| 1052 | RB_EMPTY(&vp->v_rbdirty_tree))) { |
| 1053 | return(0); |
| 1054 | } |
| 1055 | if (vp->v_type != VCHR) { |
| 1056 | if ((error = VOP_FSYNC(vp, info->waitfor, 0)) != 0) |
| 1057 | info->allerror = error; |
| 1058 | } else { |
| 1059 | /* |
| 1060 | * We must reference the vp to prevent it from |
| 1061 | * getting ripped out from under ffs_update, since |
| 1062 | * we are not holding a vnode lock. |
| 1063 | */ |
| 1064 | /* ffs_update(vp, waitfor == MNT_WAIT); */ |
| 1065 | ffs_update(vp, 0); |
| 1066 | } |
| 1067 | return(0); |
| 1068 | } |
| 1069 | |
| 1070 | /* |
| 1071 | * Look up a FFS dinode number to find its incore vnode, otherwise read it |
| 1072 | * in from disk. If it is in core, wait for the lock bit to clear, then |
| 1073 | * return the inode locked. Detection and handling of mount points must be |
| 1074 | * done by the calling routine. |
| 1075 | */ |
| 1076 | |
| 1077 | int |
| 1078 | ffs_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp) |
| 1079 | { |
| 1080 | struct fs *fs; |
| 1081 | struct inode *ip; |
| 1082 | struct ufsmount *ump; |
| 1083 | struct buf *bp; |
| 1084 | struct vnode *vp; |
| 1085 | cdev_t dev; |
| 1086 | int error; |
| 1087 | |
| 1088 | ump = VFSTOUFS(mp); |
| 1089 | dev = ump->um_dev; |
| 1090 | restart: |
| 1091 | if ((*vpp = ufs_ihashget(dev, ino)) != NULL) { |
| 1092 | return (0); |
| 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * If this MALLOC() is performed after the getnewvnode() |
| 1097 | * it might block, leaving a vnode with a NULL v_data to be |
| 1098 | * found by ffs_sync() if a sync happens to fire right then, |
| 1099 | * which will cause a panic because ffs_sync() blindly |
| 1100 | * dereferences vp->v_data (as well it should). |
| 1101 | * |
| 1102 | * XXX this may no longer be true since getnewvnode returns a |
| 1103 | * VX locked vnode now. |
| 1104 | */ |
| 1105 | MALLOC(ip, struct inode *, sizeof(struct inode), |
| 1106 | ump->um_malloctype, M_WAITOK); |
| 1107 | |
| 1108 | /* Allocate a new vnode/inode. */ |
| 1109 | error = getnewvnode(VT_UFS, mp, &vp, VLKTIMEOUT, LK_CANRECURSE); |
| 1110 | if (error) { |
| 1111 | *vpp = NULL; |
| 1112 | kfree(ip, ump->um_malloctype); |
| 1113 | return (error); |
| 1114 | } |
| 1115 | bzero((caddr_t)ip, sizeof(struct inode)); |
| 1116 | ip->i_vnode = vp; |
| 1117 | ip->i_fs = fs = ump->um_fs; |
| 1118 | ip->i_dev = dev; |
| 1119 | ip->i_number = ino; |
| 1120 | #ifdef QUOTA |
| 1121 | { |
| 1122 | int i; |
| 1123 | for (i = 0; i < MAXQUOTAS; i++) |
| 1124 | ip->i_dquot[i] = NODQUOT; |
| 1125 | } |
| 1126 | #endif |
| 1127 | |
| 1128 | /* |
| 1129 | * Insert it into the inode hash table and check for a collision. |
| 1130 | * If a collision occurs, throw away the vnode and try again. |
| 1131 | */ |
| 1132 | if (ufs_ihashins(ip) != 0) { |
| 1133 | kprintf("debug: ufs ihashins collision, retrying inode %ld\n", |
| 1134 | (long)ip->i_number); |
| 1135 | vp->v_type = VBAD; |
| 1136 | vx_put(vp); |
| 1137 | kfree(ip, ump->um_malloctype); |
| 1138 | goto restart; |
| 1139 | } |
| 1140 | vp->v_data = ip; |
| 1141 | |
| 1142 | /* Read in the disk contents for the inode, copy into the inode. */ |
| 1143 | error = bread(ump->um_devvp, fsbtodoff(fs, ino_to_fsba(fs, ino)), |
| 1144 | (int)fs->fs_bsize, &bp); |
| 1145 | if (error) { |
| 1146 | /* |
| 1147 | * The inode does not contain anything useful, so it would |
| 1148 | * be misleading to leave it on its hash chain. With mode |
| 1149 | * still zero, it will be unlinked and returned to the free |
| 1150 | * list by vput(). |
| 1151 | */ |
| 1152 | vp->v_type = VBAD; |
| 1153 | brelse(bp); |
| 1154 | vx_put(vp); |
| 1155 | *vpp = NULL; |
| 1156 | return (error); |
| 1157 | } |
| 1158 | ip->i_din = *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino)); |
| 1159 | if (DOINGSOFTDEP(vp)) |
| 1160 | softdep_load_inodeblock(ip); |
| 1161 | else |
| 1162 | ip->i_effnlink = ip->i_nlink; |
| 1163 | bqrelse(bp); |
| 1164 | |
| 1165 | /* |
| 1166 | * Initialize the vnode from the inode, check for aliases. |
| 1167 | * Note that the underlying vnode may have changed. |
| 1168 | */ |
| 1169 | error = ufs_vinit(mp, &vp); |
| 1170 | if (error) { |
| 1171 | vp->v_type = VBAD; |
| 1172 | vx_put(vp); |
| 1173 | *vpp = NULL; |
| 1174 | return (error); |
| 1175 | } |
| 1176 | /* |
| 1177 | * Finish inode initialization now that aliasing has been resolved. |
| 1178 | */ |
| 1179 | ip->i_devvp = ump->um_devvp; |
| 1180 | vref(ip->i_devvp); |
| 1181 | /* |
| 1182 | * Set up a generation number for this inode if it does not |
| 1183 | * already have one. This should only happen on old filesystems. |
| 1184 | */ |
| 1185 | if (ip->i_gen == 0) { |
| 1186 | ip->i_gen = krandom() / 2 + 1; |
| 1187 | if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) |
| 1188 | ip->i_flag |= IN_MODIFIED; |
| 1189 | } |
| 1190 | /* |
| 1191 | * Ensure that uid and gid are correct. This is a temporary |
| 1192 | * fix until fsck has been changed to do the update. |
| 1193 | */ |
| 1194 | if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */ |
| 1195 | ip->i_uid = ip->i_din.di_ouid; /* XXX */ |
| 1196 | ip->i_gid = ip->i_din.di_ogid; /* XXX */ |
| 1197 | } /* XXX */ |
| 1198 | |
| 1199 | /* |
| 1200 | * return a VX locked and refd vnode (VX == same as normal vget() |
| 1201 | * vnode so we are ok) |
| 1202 | */ |
| 1203 | *vpp = vp; |
| 1204 | return (0); |
| 1205 | } |
| 1206 | |
| 1207 | /* |
| 1208 | * File handle to vnode |
| 1209 | * |
| 1210 | * Have to be really careful about stale file handles: |
| 1211 | * - check that the inode number is valid |
| 1212 | * - call ffs_vget() to get the locked inode |
| 1213 | * - check for an unallocated inode (i_mode == 0) |
| 1214 | * - check that the given client host has export rights and return |
| 1215 | * those rights via. exflagsp and credanonp |
| 1216 | */ |
| 1217 | int |
| 1218 | ffs_fhtovp(struct mount *mp, struct vnode *rootvp, |
| 1219 | struct fid *fhp, struct vnode **vpp) |
| 1220 | { |
| 1221 | struct ufid *ufhp; |
| 1222 | struct fs *fs; |
| 1223 | |
| 1224 | ufhp = (struct ufid *)fhp; |
| 1225 | fs = VFSTOUFS(mp)->um_fs; |
| 1226 | if (ufhp->ufid_ino < ROOTINO || |
| 1227 | ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg) |
| 1228 | return (ESTALE); |
| 1229 | return (ufs_fhtovp(mp, rootvp, ufhp, vpp)); |
| 1230 | } |
| 1231 | |
| 1232 | /* |
| 1233 | * Vnode pointer to File handle |
| 1234 | */ |
| 1235 | /* ARGSUSED */ |
| 1236 | int |
| 1237 | ffs_vptofh(struct vnode *vp, struct fid *fhp) |
| 1238 | { |
| 1239 | struct inode *ip; |
| 1240 | struct ufid *ufhp; |
| 1241 | |
| 1242 | ip = VTOI(vp); |
| 1243 | ufhp = (struct ufid *)fhp; |
| 1244 | ufhp->ufid_len = sizeof(struct ufid); |
| 1245 | ufhp->ufid_ino = ip->i_number; |
| 1246 | ufhp->ufid_gen = ip->i_gen; |
| 1247 | return (0); |
| 1248 | } |
| 1249 | |
| 1250 | /* |
| 1251 | * Initialize the filesystem; just use ufs_init. |
| 1252 | */ |
| 1253 | static int |
| 1254 | ffs_init(struct vfsconf *vfsp) |
| 1255 | { |
| 1256 | softdep_initialize(); |
| 1257 | kmalloc_raise_limit(M_FFSNODE, 0); |
| 1258 | return (ufs_init(vfsp)); |
| 1259 | } |
| 1260 | |
| 1261 | /* |
| 1262 | * Write a superblock and associated information back to disk. |
| 1263 | */ |
| 1264 | static int |
| 1265 | ffs_sbupdate(struct ufsmount *mp, int waitfor) |
| 1266 | { |
| 1267 | struct fs *dfs, *fs = mp->um_fs; |
| 1268 | struct buf *bp; |
| 1269 | int blks; |
| 1270 | void *space; |
| 1271 | int i, size, error, allerror = 0; |
| 1272 | |
| 1273 | /* |
| 1274 | * First write back the summary information. |
| 1275 | * |
| 1276 | * NOTE: the getblk is relative to the device vnode so bio1 |
| 1277 | * contains the device block number. |
| 1278 | */ |
| 1279 | blks = howmany(fs->fs_cssize, fs->fs_fsize); |
| 1280 | space = fs->fs_csp; |
| 1281 | for (i = 0; i < blks; i += fs->fs_frag) { |
| 1282 | size = fs->fs_bsize; |
| 1283 | if (i + fs->fs_frag > blks) |
| 1284 | size = (blks - i) * fs->fs_fsize; |
| 1285 | bp = getblk(mp->um_devvp, fsbtodoff(fs, fs->fs_csaddr + i), |
| 1286 | size, 0, 0); |
| 1287 | bcopy(space, bp->b_data, (uint)size); |
| 1288 | space = (char *)space + size; |
| 1289 | if (waitfor != MNT_WAIT) |
| 1290 | bawrite(bp); |
| 1291 | else if ((error = bwrite(bp)) != 0) |
| 1292 | allerror = error; |
| 1293 | } |
| 1294 | /* |
| 1295 | * Now write back the superblock itself. If any errors occurred |
| 1296 | * up to this point, then fail so that the superblock avoids |
| 1297 | * being written out as clean. |
| 1298 | */ |
| 1299 | if (allerror) |
| 1300 | return (allerror); |
| 1301 | bp = getblk(mp->um_devvp, SBOFF, (int)fs->fs_sbsize, 0, 0); |
| 1302 | fs->fs_fmod = 0; |
| 1303 | fs->fs_time = time_second; |
| 1304 | bcopy((caddr_t)fs, bp->b_data, (uint)fs->fs_sbsize); |
| 1305 | /* Restore compatibility to old filesystems. XXX */ |
| 1306 | dfs = (struct fs *)bp->b_data; /* XXX */ |
| 1307 | if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */ |
| 1308 | dfs->fs_nrpos = -1; /* XXX */ |
| 1309 | if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */ |
| 1310 | int32_t *lp, tmp; /* XXX */ |
| 1311 | /* XXX */ |
| 1312 | lp = (int32_t *)&dfs->fs_qbmask; /* XXX */ |
| 1313 | tmp = lp[4]; /* XXX */ |
| 1314 | for (i = 4; i > 0; i--) /* XXX */ |
| 1315 | lp[i] = lp[i-1]; /* XXX */ |
| 1316 | lp[0] = tmp; /* XXX */ |
| 1317 | } /* XXX */ |
| 1318 | dfs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */ |
| 1319 | if (waitfor != MNT_WAIT) |
| 1320 | bawrite(bp); |
| 1321 | else if ((error = bwrite(bp)) != 0) |
| 1322 | allerror = error; |
| 1323 | return (allerror); |
| 1324 | } |