2 * Copyright (c) 1989, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.30 2005/02/02 21:34:19 joerg Exp $
38 #include "opt_quota.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/nlookup.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
49 #include <sys/fcntl.h>
50 #include <sys/disklabel.h>
51 #include <sys/malloc.h>
56 #include "ufs_extern.h"
59 #include "ffs_extern.h"
62 #include <vm/vm_page.h>
63 #include <vm/vm_zone.h>
65 static MALLOC_DEFINE(M_FFSNODE, "FFS node", "FFS vnode private part");
67 static int ffs_sbupdate (struct ufsmount *, int);
68 static int ffs_reload (struct mount *,struct ucred *,struct thread *);
69 static int ffs_oldfscompat (struct fs *);
70 static int ffs_mount (struct mount *, char *, caddr_t, struct thread *);
71 static int ffs_init (struct vfsconf *);
73 static struct vfsops ufs_vfsops = {
90 VFS_SET(ufs_vfsops, ufs, 0);
92 extern struct vnodeopv_entry_desc ffs_vnodeop_entries[];
93 extern struct vnodeopv_entry_desc ffs_specop_entries[];
94 extern struct vnodeopv_entry_desc ffs_fifoop_entries[];
100 * Called when mounting local physical media
104 * mp mount point structure
105 * path NULL (flag for root mount!!!)
107 * p process (user credentials check [statfs])
110 * mp mount point structure
111 * path path to mount point
112 * data pointer to argument struct in user space
113 * p process (user credentials check)
116 * !0 error number (errno.h)
121 * mount point is locked
123 * mount point is locked
126 * A NULL path can be used for a flag since the mount
127 * system call will fail with EFAULT in copyinstr in
128 * nlookup() if it is a genuine NULL from the user.
131 ffs_mount(struct mount *mp, /* mount struct pointer */
132 char *path, /* path to mount point */
133 caddr_t data, /* arguments to FS specific mount */
134 struct thread *td) /* process requesting mount */
140 struct ufs_args args;
141 struct ufsmount *ump = 0;
143 int flags, ronly = 0;
146 struct nlookupdata nd;
147 struct vnode *rootvp;
149 KKASSERT(td->td_proc);
150 cred = td->td_proc->p_ucred;
154 * Use NULL path to flag a root mount
159 * Mounting root filesystem
163 if ((error = bdevvp(rootdev, &rootvp))) {
164 printf("ffs_mountroot: can't find rootvp\n");
168 if( ( error = ffs_mountfs(rootvp, mp, td, M_FFSNODE)) != 0) {
169 /* fs specific cleanup (if any)*/
173 goto dostatfs; /* success*/
179 * Mounting non-root filesystem or updating a filesystem
183 /* copy in user arguments*/
184 error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
186 goto error_1; /* can't get arguments*/
189 * If updating, check whether changing from read-only to
190 * read/write; if there is no device name, that's all we do.
192 if (mp->mnt_flag & MNT_UPDATE) {
195 devvp = ump->um_devvp;
197 ronly = fs->fs_ronly; /* MNT_RELOAD might change this */
198 if (ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
200 * Flush any dirty data.
202 VFS_SYNC(mp, MNT_WAIT, td);
204 * Check for and optionally get rid of files open
208 if (mp->mnt_flag & MNT_FORCE)
210 if (mp->mnt_flag & MNT_SOFTDEP) {
211 error = softdep_flushfiles(mp, flags, td);
213 error = ffs_flushfiles(mp, flags, td);
217 if (!error && (mp->mnt_flag & MNT_RELOAD))
218 error = ffs_reload(mp, NULL, td);
222 if (ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
224 * If upgrade to read-write by non-root, then verify
225 * that user has necessary permissions on the device.
227 if (cred->cr_uid != 0) {
228 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
229 if ((error = VOP_ACCESS(devvp, VREAD | VWRITE,
231 VOP_UNLOCK(devvp, 0, td);
234 VOP_UNLOCK(devvp, 0, td);
237 fs->fs_flags &= ~FS_UNCLEAN;
238 if (fs->fs_clean == 0) {
239 fs->fs_flags |= FS_UNCLEAN;
240 if (mp->mnt_flag & MNT_FORCE) {
242 "WARNING: %s was not properly dismounted\n",
246 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
253 /* check to see if we need to start softdep */
254 if (fs->fs_flags & FS_DOSOFTDEP) {
255 error = softdep_mount(devvp, mp, fs);
263 * Soft updates is incompatible with "async",
264 * so if we are doing softupdates stop the user
265 * from setting the async flag in an update.
266 * Softdep_mount() clears it in an initial mount
269 if (mp->mnt_flag & MNT_SOFTDEP) {
270 mp->mnt_flag &= ~MNT_ASYNC;
272 /* if not updating name...*/
273 if (args.fspec == 0) {
275 * Process export requests. Jumping to "success"
276 * will return the vfs_export() error code.
278 error = vfs_export(mp, &ump->um_export, &args.export);
284 * Not an update, or updating the name: look up the name
285 * and verify that it refers to a sensible block device.
288 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
290 error = nlookup(&nd);
292 error = cache_vref(nd.nl_ncp, nd.nl_cred, &devvp);
297 if (!vn_isdisk(devvp, &error))
301 * If mount by non-root, then verify that user has necessary
302 * permissions on the device.
304 if (cred->cr_uid != 0) {
306 if ((mp->mnt_flag & MNT_RDONLY) == 0)
307 accessmode |= VWRITE;
308 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
309 if ((error = VOP_ACCESS(devvp, accessmode, cred, td)) != 0) {
313 VOP_UNLOCK(devvp, 0, td);
316 if (mp->mnt_flag & MNT_UPDATE) {
320 * If it's not the same vnode, or at least the same device
321 * then it's not correct. NOTE: devvp->v_rdev may be NULL
322 * since we haven't opened it, so we compare udev instead.
325 if (devvp != ump->um_devvp) {
326 if (devvp->v_udev == ump->um_devvp->v_udev) {
329 printf("cannot update mount, udev does"
330 " not match %08x vs %08x\n",
331 devvp->v_udev, ump->um_devvp->v_udev);
332 error = EINVAL; /* needs translation */
338 * Update device name only on success
341 /* Save "mounted from" info for mount point (NULL pad)*/
342 copyinstr( args.fspec,
343 mp->mnt_stat.f_mntfromname,
346 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
355 /* Save "mounted from" info for mount point (NULL pad)*/
356 copyinstr( args.fspec, /* device name*/
357 mp->mnt_stat.f_mntfromname, /* save area*/
358 MNAMELEN - 1, /* max size*/
359 &size); /* real size*/
360 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
362 error = ffs_mountfs(devvp, mp, td, M_FFSNODE);
370 * Initialize FS stat information in mount struct; uses
371 * mp->mnt_stat.f_mntfromname.
373 * This code is common to root and non-root mounts
375 (void)VFS_STATFS(mp, &mp->mnt_stat, td);
380 error_2: /* error with devvp held*/
382 /* release devvp before failing*/
385 error_1: /* no state to back out*/
388 if (!error && path && (mp->mnt_flag & MNT_UPDATE)) {
389 /* Update clean flag after changing read-onlyness. */
391 if (ronly != fs->fs_ronly) {
392 fs->fs_ronly = ronly;
393 fs->fs_clean = ronly &&
394 (fs->fs_flags & FS_UNCLEAN) == 0 ? 1 : 0;
395 ffs_sbupdate(ump, MNT_WAIT);
402 * Reload all incore data for a filesystem (used after running fsck on
403 * the root filesystem and finding things to fix). The filesystem must
404 * be mounted read-only.
406 * Things to do to update the mount:
407 * 1) invalidate all cached meta-data.
408 * 2) re-read superblock from disk.
409 * 3) re-read summary information from disk.
410 * 4) invalidate all inactive vnodes.
411 * 5) invalidate all cached file data.
412 * 6) re-read inode data for all active vnodes.
415 static int ffs_reload_scan2(struct mount *mp, struct vnode *vp, void *data);
427 ffs_reload(struct mount *mp, struct ucred *cred, struct thread *td)
432 struct fs *fs, *newfs;
433 struct partinfo dpart;
435 int i, blks, size, error;
436 struct scaninfo scaninfo;
439 if ((mp->mnt_flag & MNT_RDONLY) == 0)
442 * Step 1: invalidate all cached meta-data.
444 devvp = VFSTOUFS(mp)->um_devvp;
445 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
446 error = vinvalbuf(devvp, 0, td, 0, 0);
447 VOP_UNLOCK(devvp, 0, td);
449 panic("ffs_reload: dirty1");
453 * Only VMIO the backing device if the backing device is a real
454 * block device. See ffs_mountmfs() for more details.
456 if (devvp->v_tag != VT_MFS && vn_isdisk(devvp, NULL)) {
457 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
458 vfs_object_create(devvp, td);
459 VOP_UNLOCK(devvp, 0, td);
463 * Step 2: re-read superblock from disk.
465 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, td) != 0)
468 size = dpart.disklab->d_secsize;
469 if ((error = bread(devvp, (ufs_daddr_t)(SBOFF/size), SBSIZE, &bp)) != 0)
474 newfs = (struct fs *)bp->b_data;
475 if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
476 newfs->fs_bsize < sizeof(struct fs)) {
478 return (EIO); /* XXX needs translation */
480 fs = VFSTOUFS(mp)->um_fs;
482 * Copy pointer fields back into superblock before copying in XXX
483 * new superblock. These should really be in the ufsmount. XXX
484 * Note that important parameters (eg fs_ncg) are unchanged.
486 newfs->fs_csp = fs->fs_csp;
487 newfs->fs_maxcluster = fs->fs_maxcluster;
488 newfs->fs_contigdirs = fs->fs_contigdirs;
489 /* The filesystem is still read-only. */
491 bcopy(newfs, fs, (uint)fs->fs_sbsize);
492 if (fs->fs_sbsize < SBSIZE)
493 bp->b_flags |= B_INVAL;
495 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
497 /* An old fsck may have zeroed these fields, so recheck them. */
498 if (fs->fs_avgfilesize <= 0) /* XXX */
499 fs->fs_avgfilesize = AVFILESIZ; /* XXX */
500 if (fs->fs_avgfpdir <= 0) /* XXX */
501 fs->fs_avgfpdir = AFPDIR; /* XXX */
504 * Step 3: re-read summary information from disk.
506 blks = howmany(fs->fs_cssize, fs->fs_fsize);
508 for (i = 0; i < blks; i += fs->fs_frag) {
510 if (i + fs->fs_frag > blks)
511 size = (blks - i) * fs->fs_fsize;
512 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, &bp);
517 bcopy(bp->b_data, space, (uint)size);
518 space = (char *)space + size;
522 * We no longer know anything about clusters per cylinder group.
524 if (fs->fs_contigsumsize > 0) {
525 lp = fs->fs_maxcluster;
526 for (i = 0; i < fs->fs_ncg; i++)
527 *lp++ = fs->fs_contigsumsize;
532 scaninfo.devvp = devvp;
534 while (error == 0 && scaninfo.rescan) {
536 error = vmntvnodescan(mp, VMSC_GETVX,
537 NULL, ffs_reload_scan2, &scaninfo);
543 ffs_reload_scan2(struct mount *mp, struct vnode *vp, void *data)
545 struct scaninfo *info = data;
553 if (vrecycle(vp, info->td))
556 if (vinvalbuf(vp, 0, info->td, 0, 0))
557 panic("ffs_reload: dirty2");
559 * Step 6: re-read inode data for all active vnodes.
562 error = bread(info->devvp,
563 fsbtodb(info->fs, ino_to_fsba(info->fs, ip->i_number)),
564 (int)info->fs->fs_bsize, &bp);
569 ip->i_din = *((struct dinode *)bp->b_data +
570 ino_to_fsbo(info->fs, ip->i_number));
571 ip->i_effnlink = ip->i_nlink;
577 * Common code for mount and mountroot
580 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td,
581 struct malloc_type *malloctype)
583 struct ufsmount *ump;
587 struct partinfo dpart;
589 int error, i, blks, size, ronly;
591 uint64_t maxfilesize; /* XXX */
595 * Disallow multiple mounts of the same device.
596 * Disallow mounting of a device that is currently in use
597 * Flush out any old buffers remaining from a previous use.
599 error = vfs_mountedon(devvp);
602 if (count_udev(devvp->v_udev) > 0)
604 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
605 error = vinvalbuf(devvp, V_SAVE, td, 0, 0);
606 VOP_UNLOCK(devvp, 0, td);
610 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
611 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
612 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL, td);
613 VOP_UNLOCK(devvp, 0, td);
617 if (dev->si_iosize_max != 0)
618 mp->mnt_iosize_max = dev->si_iosize_max;
619 if (mp->mnt_iosize_max > MAXPHYS)
620 mp->mnt_iosize_max = MAXPHYS;
623 * Only VMIO the backing device if the backing device is a real
624 * block device. This excludes the original MFS implementation.
625 * Note that it is optional that the backing device be VMIOed. This
626 * increases the opportunity for metadata caching.
628 * This call must be made after the VOP_OPEN.
630 if (devvp->v_tag != VT_MFS && vn_isdisk(devvp, NULL)) {
631 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
632 vfs_object_create(devvp, td);
633 VOP_UNLOCK(devvp, 0, td);
636 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, proc0.p_ucred, td) != 0)
639 size = dpart.disklab->d_secsize;
643 if ((error = bread(devvp, SBLOCK, SBSIZE, &bp)) != 0)
645 fs = (struct fs *)bp->b_data;
646 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
647 fs->fs_bsize < sizeof(struct fs)) {
648 error = EINVAL; /* XXX needs translation */
652 fs->fs_flags &= ~FS_UNCLEAN;
653 if (fs->fs_clean == 0) {
654 fs->fs_flags |= FS_UNCLEAN;
655 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
657 "WARNING: %s was not properly dismounted\n",
661 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
667 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
668 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
669 error = EROFS; /* needs translation */
672 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
673 bzero((caddr_t)ump, sizeof *ump);
674 ump->um_malloctype = malloctype;
675 ump->um_i_effnlink_valid = 1;
676 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT,
678 ump->um_blkatoff = ffs_blkatoff;
679 ump->um_truncate = ffs_truncate;
680 ump->um_update = ffs_update;
681 ump->um_valloc = ffs_valloc;
682 ump->um_vfree = ffs_vfree;
683 bcopy(bp->b_data, ump->um_fs, (uint)fs->fs_sbsize);
684 if (fs->fs_sbsize < SBSIZE)
685 bp->b_flags |= B_INVAL;
689 fs->fs_ronly = ronly;
690 size = fs->fs_cssize;
691 blks = howmany(size, fs->fs_fsize);
692 if (fs->fs_contigsumsize > 0)
693 size += fs->fs_ncg * sizeof(int32_t);
694 size += fs->fs_ncg * sizeof(uint8_t);
695 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
697 for (i = 0; i < blks; i += fs->fs_frag) {
699 if (i + fs->fs_frag > blks)
700 size = (blks - i) * fs->fs_fsize;
701 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
703 free(fs->fs_csp, M_UFSMNT);
706 bcopy(bp->b_data, space, (uint)size);
707 space = (char *)space + size;
711 if (fs->fs_contigsumsize > 0) {
712 fs->fs_maxcluster = lp = space;
713 for (i = 0; i < fs->fs_ncg; i++)
714 *lp++ = fs->fs_contigsumsize;
717 size = fs->fs_ncg * sizeof(uint8_t);
718 fs->fs_contigdirs = (uint8_t *)space;
719 bzero(fs->fs_contigdirs, size);
720 /* Compatibility for old filesystems XXX */
721 if (fs->fs_avgfilesize <= 0) /* XXX */
722 fs->fs_avgfilesize = AVFILESIZ; /* XXX */
723 if (fs->fs_avgfpdir <= 0) /* XXX */
724 fs->fs_avgfpdir = AFPDIR; /* XXX */
725 mp->mnt_data = (qaddr_t)ump;
726 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
727 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
728 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
729 vfs_getvfs(&mp->mnt_stat.f_fsid))
731 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
732 mp->mnt_flag |= MNT_LOCAL;
735 ump->um_devvp = devvp;
736 ump->um_nindir = fs->fs_nindir;
737 ump->um_bptrtodb = fs->fs_fsbtodb;
738 ump->um_seqinc = fs->fs_frag;
739 for (i = 0; i < MAXQUOTAS; i++)
740 ump->um_quotas[i] = NULLVP;
741 dev->si_mountpoint = mp;
744 if( mp->mnt_flag & MNT_ROOTFS) {
746 * Root mount; update timestamp in mount structure.
747 * this will be used by the common root mount code
748 * to update the system clock.
750 mp->mnt_time = fs->fs_time;
753 ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */
754 maxfilesize = (uint64_t)0x40000000 * fs->fs_bsize - 1; /* XXX */
755 /* Enforce limit caused by vm object backing (32 bits vm_pindex_t). */
756 if (maxfilesize > (uint64_t)0x80000000u * PAGE_SIZE - 1)
757 maxfilesize = (uint64_t)0x80000000u * PAGE_SIZE - 1;
758 if (fs->fs_maxfilesize > maxfilesize) /* XXX */
759 fs->fs_maxfilesize = maxfilesize; /* XXX */
761 if ((fs->fs_flags & FS_DOSOFTDEP) &&
762 (error = softdep_mount(devvp, mp, fs)) != 0) {
763 free(fs->fs_csp, M_UFSMNT);
768 (void) ffs_sbupdate(ump, MNT_WAIT);
770 vfs_add_vnodeops(mp, &mp->mnt_vn_norm_ops, ffs_vnodeop_entries);
771 vfs_add_vnodeops(mp, &mp->mnt_vn_spec_ops, ffs_specop_entries);
772 vfs_add_vnodeops(mp, &mp->mnt_vn_fifo_ops, ffs_fifoop_entries);
776 dev->si_mountpoint = NULL;
779 VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, td);
781 free(ump->um_fs, M_UFSMNT);
783 mp->mnt_data = (qaddr_t)0;
789 * Sanity checks for old filesystems.
791 * XXX - goes away some day.
794 ffs_oldfscompat(struct fs *fs)
796 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
797 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
798 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
799 fs->fs_nrpos = 8; /* XXX */
800 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
803 uint64_t sizepb = fs->fs_bsize; /* XXX */
805 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
806 for (i = 0; i < NIADDR; i++) { /* XXX */
807 sizepb *= NINDIR(fs); /* XXX */
808 fs->fs_maxfilesize += sizepb; /* XXX */
811 fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
812 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
813 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
819 * unmount system call
822 ffs_unmount(struct mount *mp, int mntflags, struct thread *td)
824 struct ufsmount *ump;
829 if (mntflags & MNT_FORCE) {
832 if (mp->mnt_flag & MNT_SOFTDEP) {
833 if ((error = softdep_flushfiles(mp, flags, td)) != 0)
836 if ((error = ffs_flushfiles(mp, flags, td)) != 0)
841 if (fs->fs_ronly == 0) {
842 fs->fs_clean = fs->fs_flags & FS_UNCLEAN ? 0 : 1;
843 error = ffs_sbupdate(ump, MNT_WAIT);
849 ump->um_devvp->v_rdev->si_mountpoint = NULL;
851 vinvalbuf(ump->um_devvp, V_SAVE, td, 0, 0);
852 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE, td);
854 vrele(ump->um_devvp);
856 free(fs->fs_csp, M_UFSMNT);
859 mp->mnt_data = (qaddr_t)0;
860 mp->mnt_flag &= ~MNT_LOCAL;
865 * Flush out all the files in a filesystem.
868 ffs_flushfiles(struct mount *mp, int flags, struct thread *td)
870 struct ufsmount *ump;
875 if (mp->mnt_flag & MNT_QUOTA) {
877 error = vflush(mp, 0, SKIPSYSTEM|flags);
880 /* Find out how many quota files we have open. */
881 for (i = 0; i < MAXQUOTAS; i++) {
882 if (ump->um_quotas[i] == NULLVP)
887 * Here we fall through to vflush again to ensure
888 * that we have gotten rid of all the system vnodes.
893 * Flush all the files.
895 if ((error = vflush(mp, 0, flags)) != 0)
898 * Flush filesystem metadata.
900 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, td);
901 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
902 VOP_UNLOCK(ump->um_devvp, 0, td);
907 * Get filesystem statistics.
910 ffs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
912 struct ufsmount *ump;
917 if (fs->fs_magic != FS_MAGIC)
919 sbp->f_bsize = fs->fs_fsize;
920 sbp->f_iosize = fs->fs_bsize;
921 sbp->f_blocks = fs->fs_dsize;
922 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
923 fs->fs_cstotal.cs_nffree;
924 sbp->f_bavail = freespace(fs, fs->fs_minfree);
925 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
926 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
927 if (sbp != &mp->mnt_stat) {
928 sbp->f_type = mp->mnt_vfc->vfc_typenum;
929 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
930 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
936 * Go through the disk queues to initiate sandbagged IO;
937 * go through the inodes to write those that have been modified;
938 * initiate the writing of the super block if it has been modified.
940 * Note: we are always called with the filesystem marked `MPBUSY'.
944 static int ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
945 static int ffs_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
948 ffs_sync(struct mount *mp, int waitfor, struct thread *td)
950 struct ufsmount *ump = VFSTOUFS(mp);
953 struct scaninfo scaninfo;
956 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
957 printf("fs = %s\n", fs->fs_fsmnt);
958 panic("ffs_sync: rofs mod");
962 * Write back each (modified) inode.
964 scaninfo.allerror = 0;
966 scaninfo.waitfor = waitfor;
967 while (scaninfo.rescan) {
969 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
970 ffs_sync_scan1, ffs_sync_scan2, &scaninfo);
974 * Force stale filesystem control information to be flushed.
976 if (waitfor != MNT_LAZY) {
977 if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
978 waitfor = MNT_NOWAIT;
979 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, td);
980 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0)
981 scaninfo.allerror = error;
982 VOP_UNLOCK(ump->um_devvp, 0, td);
988 * Write back modified superblock.
990 if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
991 scaninfo.allerror = error;
992 return (scaninfo.allerror);
996 ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
1001 * Depend on the mount list's vnode lock to keep things stable
1002 * enough for a quick test. Since there might be hundreds of
1003 * thousands of vnodes, we cannot afford even a subroutine
1004 * call unless there's a good chance that we have work to do.
1007 /* Restart out whole search if this guy is locked
1008 * or is being reclaimed.
1010 if (vp->v_type == VNON || ((ip->i_flag &
1011 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1012 TAILQ_EMPTY(&vp->v_dirtyblkhd))) {
1019 ffs_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
1021 struct scaninfo *info = data;
1022 thread_t td = curthread; /* XXX */
1027 * We have to recheck after having obtained the vnode interlock.
1030 if (vp->v_type == VNON || ((ip->i_flag &
1031 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1032 TAILQ_EMPTY(&vp->v_dirtyblkhd))) {
1035 if (vp->v_type != VCHR) {
1036 if ((error = VOP_FSYNC(vp, info->waitfor, td)) != 0)
1037 info->allerror = error;
1040 * We must reference the vp to prevent it from
1041 * getting ripped out from under UFS_UPDATE, since
1042 * we are not holding a vnode lock.
1044 /* UFS_UPDATE(vp, waitfor == MNT_WAIT); */
1051 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1052 * in from disk. If it is in core, wait for the lock bit to clear, then
1053 * return the inode locked. Detection and handling of mount points must be
1054 * done by the calling routine.
1058 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1062 struct ufsmount *ump;
1071 if ((*vpp = ufs_ihashget(dev, ino)) != NULL) {
1076 * If this MALLOC() is performed after the getnewvnode()
1077 * it might block, leaving a vnode with a NULL v_data to be
1078 * found by ffs_sync() if a sync happens to fire right then,
1079 * which will cause a panic because ffs_sync() blindly
1080 * dereferences vp->v_data (as well it should).
1082 * XXX this may no longer be true since getnewvnode returns a
1083 * VX locked vnode now.
1085 MALLOC(ip, struct inode *, sizeof(struct inode),
1086 ump->um_malloctype, M_WAITOK);
1088 /* Allocate a new vnode/inode. */
1089 error = getnewvnode(VT_UFS, mp, &vp, VLKTIMEOUT, LK_CANRECURSE);
1092 free(ip, ump->um_malloctype);
1095 bzero((caddr_t)ip, sizeof(struct inode));
1097 ip->i_fs = fs = ump->um_fs;
1103 for (i = 0; i < MAXQUOTAS; i++)
1104 ip->i_dquot[i] = NODQUOT;
1109 * Insert it into the inode hash table and check for a collision.
1110 * If a collision occurs, throw away the vnode and try again.
1112 if (ufs_ihashins(ip) != 0) {
1113 printf("debug: ufs ihashins collision, retrying inode %ld\n",
1114 (long)ip->i_number);
1116 free(ip, ump->um_malloctype);
1121 /* Read in the disk contents for the inode, copy into the inode. */
1122 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1123 (int)fs->fs_bsize, &bp);
1126 * The inode does not contain anything useful, so it would
1127 * be misleading to leave it on its hash chain. With mode
1128 * still zero, it will be unlinked and returned to the free
1136 ip->i_din = *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ino));
1137 if (DOINGSOFTDEP(vp))
1138 softdep_load_inodeblock(ip);
1140 ip->i_effnlink = ip->i_nlink;
1144 * Initialize the vnode from the inode, check for aliases.
1145 * Note that the underlying vnode may have changed.
1147 error = ufs_vinit(mp, &vp);
1154 * Finish inode initialization now that aliasing has been resolved.
1156 ip->i_devvp = ump->um_devvp;
1159 * Set up a generation number for this inode if it does not
1160 * already have one. This should only happen on old filesystems.
1162 if (ip->i_gen == 0) {
1163 ip->i_gen = random() / 2 + 1;
1164 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1165 ip->i_flag |= IN_MODIFIED;
1168 * Ensure that uid and gid are correct. This is a temporary
1169 * fix until fsck has been changed to do the update.
1171 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1172 ip->i_uid = ip->i_din.di_ouid; /* XXX */
1173 ip->i_gid = ip->i_din.di_ogid; /* XXX */
1177 * return a VX locked and refd vnode (VX == same as normal vget()
1178 * vnode so we are ok)
1185 * File handle to vnode
1187 * Have to be really careful about stale file handles:
1188 * - check that the inode number is valid
1189 * - call ffs_vget() to get the locked inode
1190 * - check for an unallocated inode (i_mode == 0)
1191 * - check that the given client host has export rights and return
1192 * those rights via. exflagsp and credanonp
1195 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1200 ufhp = (struct ufid *)fhp;
1201 fs = VFSTOUFS(mp)->um_fs;
1202 if (ufhp->ufid_ino < ROOTINO ||
1203 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1205 return (ufs_fhtovp(mp, ufhp, vpp));
1209 * Vnode pointer to File handle
1213 ffs_vptofh(struct vnode *vp, struct fid *fhp)
1219 ufhp = (struct ufid *)fhp;
1220 ufhp->ufid_len = sizeof(struct ufid);
1221 ufhp->ufid_ino = ip->i_number;
1222 ufhp->ufid_gen = ip->i_gen;
1227 * Initialize the filesystem; just use ufs_init.
1230 ffs_init(struct vfsconf *vfsp)
1232 softdep_initialize();
1233 return (ufs_init(vfsp));
1237 * Write a superblock and associated information back to disk.
1240 ffs_sbupdate(struct ufsmount *mp, int waitfor)
1242 struct fs *dfs, *fs = mp->um_fs;
1246 int i, size, error, allerror = 0;
1249 * First write back the summary information.
1251 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1253 for (i = 0; i < blks; i += fs->fs_frag) {
1254 size = fs->fs_bsize;
1255 if (i + fs->fs_frag > blks)
1256 size = (blks - i) * fs->fs_fsize;
1257 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1259 bcopy(space, bp->b_data, (uint)size);
1260 space = (char *)space + size;
1261 if (waitfor != MNT_WAIT)
1263 else if ((error = bwrite(bp)) != 0)
1267 * Now write back the superblock itself. If any errors occurred
1268 * up to this point, then fail so that the superblock avoids
1269 * being written out as clean.
1273 bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize, 0, 0);
1275 fs->fs_time = time_second;
1276 bcopy((caddr_t)fs, bp->b_data, (uint)fs->fs_sbsize);
1277 /* Restore compatibility to old filesystems. XXX */
1278 dfs = (struct fs *)bp->b_data; /* XXX */
1279 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
1280 dfs->fs_nrpos = -1; /* XXX */
1281 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1282 int32_t *lp, tmp; /* XXX */
1284 lp = (int32_t *)&dfs->fs_qbmask; /* XXX */
1285 tmp = lp[4]; /* XXX */
1286 for (i = 4; i > 0; i--) /* XXX */
1287 lp[i] = lp[i-1]; /* XXX */
1288 lp[0] = tmp; /* XXX */
1290 dfs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */
1291 if (waitfor != MNT_WAIT)
1293 else if ((error = bwrite(bp)) != 0)