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.59 2008/09/17 21:44:25 dillon Exp $
38 #include "opt_quota.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/nlookup.h>
45 #include <sys/kernel.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
50 #include <sys/fcntl.h>
51 #include <sys/diskslice.h>
52 #include <sys/malloc.h>
57 #include "ufs_extern.h"
60 #include "ffs_extern.h"
63 #include <vm/vm_page.h>
64 #include <vm/vm_zone.h>
66 static MALLOC_DEFINE(M_FFSNODE, "FFS node", "FFS vnode private part");
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 *);
74 static struct vfsops ufs_vfsops = {
75 .vfs_mount = ffs_mount,
76 .vfs_unmount = ffs_unmount,
78 .vfs_quotactl = ufs_quotactl,
79 .vfs_statfs = ffs_statfs,
82 .vfs_fhtovp = ffs_fhtovp,
83 .vfs_checkexp = ufs_check_export,
84 .vfs_vptofh = ffs_vptofh,
86 .vfs_uninit = ufs_uninit
89 VFS_SET(ufs_vfsops, ufs, 0);
90 MODULE_VERSION(ufs, 1);
92 extern struct vop_ops ffs_vnode_vops;
93 extern struct vop_ops ffs_spec_vops;
94 extern struct vop_ops ffs_fifo_vops;
99 * Called when mounting local physical media
103 * mp mount point structure
104 * path NULL (flag for root mount!!!)
106 * p process (user credentials check [statfs])
109 * mp mount point structure
110 * path path to mount point
111 * data pointer to argument struct in user space
112 * p process (user credentials check)
115 * !0 error number (errno.h)
120 * mount point is locked
122 * mount point is locked
125 * A NULL path can be used for a flag since the mount
126 * system call will fail with EFAULT in copyinstr in
127 * nlookup() if it is a genuine NULL from the user.
130 ffs_mount(struct mount *mp, /* mount struct pointer */
131 char *path, /* path to mount point */
132 caddr_t data, /* arguments to FS specific mount */
133 struct ucred *cred) /* process requesting mount */
139 struct ufs_args args;
140 struct ufsmount *ump = 0;
142 int flags, ronly = 0;
144 struct nlookupdata nd;
145 struct vnode *rootvp;
151 * Use NULL path to flag a root mount
156 * Mounting root filesystem
160 if ((error = bdevvp(rootdev, &rootvp))) {
161 kprintf("ffs_mountroot: can't find rootvp\n");
165 if( ( error = ffs_mountfs(rootvp, mp, M_FFSNODE)) != 0) {
166 /* fs specific cleanup (if any)*/
171 goto dostatfs; /* success*/
177 * Mounting non-root filesystem or updating a filesystem
181 /* copy in user arguments*/
182 error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
184 goto error_1; /* can't get arguments*/
187 * If updating, check whether changing from read-only to
188 * read/write; if there is no device name, that's all we do.
190 if (mp->mnt_flag & MNT_UPDATE) {
193 devvp = ump->um_devvp;
195 ronly = fs->fs_ronly; /* MNT_RELOAD might change this */
196 if (ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
198 * Flush any dirty data.
200 VFS_SYNC(mp, MNT_WAIT);
202 * Check for and optionally get rid of files open
206 if (mp->mnt_flag & MNT_FORCE)
208 if (mp->mnt_flag & MNT_SOFTDEP) {
209 error = softdep_flushfiles(mp, flags);
211 error = ffs_flushfiles(mp, flags);
215 if (!error && (mp->mnt_flag & MNT_RELOAD)) {
216 error = ffs_reload(mp, NULL);
221 if (ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
223 * If upgrade to read-write by non-root, then verify
224 * that user has necessary permissions on the device.
226 if (cred->cr_uid != 0) {
227 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
228 if ((error = VOP_EACCESS(devvp, VREAD | VWRITE,
236 fs->fs_flags &= ~FS_UNCLEAN;
237 if (fs->fs_clean == 0) {
238 fs->fs_flags |= FS_UNCLEAN;
239 if (mp->mnt_flag & MNT_FORCE) {
241 "WARNING: %s was not properly dismounted\n",
245 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
252 /* check to see if we need to start softdep */
253 if (fs->fs_flags & FS_DOSOFTDEP) {
254 error = softdep_mount(devvp, mp, fs);
261 * Soft updates is incompatible with "async",
262 * so if we are doing softupdates stop the user
263 * from setting the async flag in an update.
264 * Softdep_mount() clears it in an initial mount
267 if (mp->mnt_flag & MNT_SOFTDEP) {
268 mp->mnt_flag &= ~MNT_ASYNC;
270 /* if not updating name...*/
271 if (args.fspec == 0) {
273 * Process export requests. Jumping to "success"
274 * will return the vfs_export() error code.
276 error = vfs_export(mp, &ump->um_export, &args.export);
282 * Not an update, or updating the name: look up the name
283 * and verify that it refers to a sensible block device.
286 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
288 error = nlookup(&nd);
290 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
295 if (!vn_isdisk(devvp, &error))
299 * If mount by non-root, then verify that user has necessary
300 * permissions on the device.
302 if (cred->cr_uid != 0) {
304 if ((mp->mnt_flag & MNT_RDONLY) == 0)
305 accessmode |= VWRITE;
306 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
307 if ((error = VOP_EACCESS(devvp, accessmode, cred)) != 0) {
314 if (mp->mnt_flag & MNT_UPDATE) {
316 * UPDATE - make sure the resolved vnode represents the same
317 * device. Note that devvp->v_rdev may be NULL since we
318 * haven't opened it, so compare udev instead.
320 * Our current open/writecount state is associated with
321 * um_devvp, so continue using um_devvp and throw away devvp.
323 if (devvp != ump->um_devvp) {
324 if (devvp->v_umajor == ump->um_devvp->v_umajor &&
325 devvp->v_uminor == ump->um_devvp->v_uminor) {
327 devvp = ump->um_devvp;
329 kprintf("cannot update mount, udev does"
330 " not match %08x:%08x vs %08x:%08x\n",
331 devvp->v_umajor, devvp->v_uminor,
332 ump->um_devvp->v_umajor,
333 ump->um_devvp->v_uminor);
334 error = EINVAL; /* needs translation */
340 * Update device name only on success
343 /* Save "mounted from" info for mount point (NULL pad)*/
344 copyinstr( args.fspec,
345 mp->mnt_stat.f_mntfromname,
348 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
357 /* Save "mounted from" info for mount point (NULL pad)*/
358 copyinstr( args.fspec, /* device name*/
359 mp->mnt_stat.f_mntfromname, /* save area*/
360 MNAMELEN - 1, /* max size*/
361 &size); /* real size*/
362 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
364 /* Save "last mounted on" info for mount point (NULL pad)*/
365 bzero(mp->mnt_stat.f_mntonname,
366 sizeof(mp->mnt_stat.f_mntonname));
368 copyinstr(path, mp->mnt_stat.f_mntonname,
369 sizeof(mp->mnt_stat.f_mntonname) - 1,
373 error = ffs_mountfs(devvp, mp, M_FFSNODE);
381 * Initialize FS stat information in mount struct; uses
382 * mp->mnt_stat.f_mntfromname.
384 * This code is common to root and non-root mounts
386 (void)VFS_STATFS(mp, &mp->mnt_stat, cred);
391 error_2: /* error with devvp held*/
393 /* release devvp before failing*/
396 error_1: /* no state to back out*/
399 if (!error && path && (mp->mnt_flag & MNT_UPDATE)) {
400 /* Update clean flag after changing read-onlyness. */
402 if (ronly != fs->fs_ronly) {
403 fs->fs_ronly = ronly;
404 fs->fs_clean = ronly &&
405 (fs->fs_flags & FS_UNCLEAN) == 0 ? 1 : 0;
408 * The device must be re-opened as appropriate or
409 * the device close at unmount time will panic.
411 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
413 VOP_OPEN(devvp, FREAD, FSCRED, NULL);
414 VOP_CLOSE(devvp, FREAD|FWRITE);
416 VOP_OPEN(devvp, FREAD|FWRITE, FSCRED, NULL);
417 VOP_CLOSE(devvp, FREAD);
420 ffs_sbupdate(ump, MNT_WAIT);
427 * Reload all incore data for a filesystem (used after running fsck on
428 * the root filesystem and finding things to fix). The filesystem must
429 * be mounted read-only.
431 * Things to do to update the mount:
432 * 1) invalidate all cached meta-data.
433 * 2) re-read superblock from disk.
434 * 3) re-read summary information from disk.
435 * 4) invalidate all inactive vnodes.
436 * 5) invalidate all cached file data.
437 * 6) re-read inode data for all active vnodes.
440 static int ffs_reload_scan2(struct mount *mp, struct vnode *vp, void *data);
451 ffs_reload(struct mount *mp, struct ucred *cred)
456 struct fs *fs, *newfs;
457 struct partinfo dpart;
459 int i, blks, size, error;
460 struct scaninfo scaninfo;
463 if ((mp->mnt_flag & MNT_RDONLY) == 0)
466 * Step 1: invalidate all cached meta-data.
468 devvp = VFSTOUFS(mp)->um_devvp;
469 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
470 error = vinvalbuf(devvp, 0, 0, 0);
473 panic("ffs_reload: dirty1");
478 * The backing device must be VMIO-capable because we use getblk().
479 * NOTE: the MFS driver now returns a VMIO-enabled descriptor.
481 if (devvp->v_object == NULL)
482 panic("ffs_reload: devvp has no VM object!");
485 * Step 2: re-read superblock from disk.
487 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD,
491 size = dpart.media_blksize;
493 if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0) {
497 newfs = (struct fs *)bp->b_data;
498 if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
499 newfs->fs_bsize < sizeof(struct fs)) {
501 return (EIO); /* XXX needs translation */
503 fs = VFSTOUFS(mp)->um_fs;
505 * Copy pointer fields back into superblock before copying in XXX
506 * new superblock. These should really be in the ufsmount. XXX
507 * Note that important parameters (eg fs_ncg) are unchanged.
509 newfs->fs_csp = fs->fs_csp;
510 newfs->fs_maxcluster = fs->fs_maxcluster;
511 newfs->fs_contigdirs = fs->fs_contigdirs;
512 /* The filesystem is still read-only. */
514 bcopy(newfs, fs, (uint)fs->fs_sbsize);
515 if (fs->fs_sbsize < SBSIZE)
516 bp->b_flags |= B_INVAL;
518 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
520 /* An old fsck may have zeroed these fields, so recheck them. */
521 if (fs->fs_avgfilesize <= 0) /* XXX */
522 fs->fs_avgfilesize = AVFILESIZ; /* XXX */
523 if (fs->fs_avgfpdir <= 0) /* XXX */
524 fs->fs_avgfpdir = AFPDIR; /* XXX */
527 * Step 3: re-read summary information from disk.
529 blks = howmany(fs->fs_cssize, fs->fs_fsize);
531 for (i = 0; i < blks; i += fs->fs_frag) {
533 if (i + fs->fs_frag > blks)
534 size = (blks - i) * fs->fs_fsize;
535 error = bread(devvp, fsbtodoff(fs, fs->fs_csaddr + i), size, &bp);
540 bcopy(bp->b_data, space, (uint)size);
541 space = (char *)space + size;
545 * We no longer know anything about clusters per cylinder group.
547 if (fs->fs_contigsumsize > 0) {
548 lp = fs->fs_maxcluster;
549 for (i = 0; i < fs->fs_ncg; i++)
550 *lp++ = fs->fs_contigsumsize;
555 scaninfo.devvp = devvp;
556 while (error == 0 && scaninfo.rescan) {
558 error = vmntvnodescan(mp, VMSC_GETVX,
559 NULL, ffs_reload_scan2, &scaninfo);
565 ffs_reload_scan2(struct mount *mp, struct vnode *vp, void *data)
567 struct scaninfo *info = data;
578 if (vinvalbuf(vp, 0, 0, 0))
579 panic("ffs_reload: dirty2");
581 * Step 6: re-read inode data for all active vnodes.
584 error = bread(info->devvp,
585 fsbtodoff(info->fs, ino_to_fsba(info->fs, ip->i_number)),
586 (int)info->fs->fs_bsize, &bp);
591 ip->i_din = *((struct ufs1_dinode *)bp->b_data +
592 ino_to_fsbo(info->fs, ip->i_number));
593 ip->i_effnlink = ip->i_nlink;
599 * Common code for mount and mountroot
602 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct malloc_type *mtype)
604 struct ufsmount *ump;
608 struct partinfo dpart;
610 int error, i, blks, size, ronly;
612 uint64_t maxfilesize; /* XXX */
615 * Disallow multiple mounts of the same device.
616 * Disallow mounting of a device that is currently in use
617 * Flush out any old buffers remaining from a previous use.
619 error = vfs_mountedon(devvp);
622 if (vcount(devvp) > 0)
624 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
625 error = vinvalbuf(devvp, V_SAVE, 0, 0);
630 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
631 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
632 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
637 if (dev->si_iosize_max != 0)
638 mp->mnt_iosize_max = dev->si_iosize_max;
639 if (mp->mnt_iosize_max > MAXPHYS)
640 mp->mnt_iosize_max = MAXPHYS;
643 * Filesystem supports native FSMIDs
645 mp->mnt_kern_flag |= MNTK_FSMID;
648 * The backing device must be VMIO-capable because we use getblk().
649 * NOTE: the MFS driver now returns a VMIO-enabled descriptor.
650 * The VOP_OPEN() call above should have associated a VM object
653 if (devvp->v_object == NULL)
654 panic("ffs_reload: devvp has no VM object!");
656 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD,
657 proc0.p_ucred, NULL) != 0) {
660 size = dpart.media_blksize;
665 if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0)
667 fs = (struct fs *)bp->b_data;
668 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
669 fs->fs_bsize < sizeof(struct fs)) {
670 error = EINVAL; /* XXX needs translation */
674 fs->fs_flags &= ~FS_UNCLEAN;
675 if (fs->fs_clean == 0) {
676 fs->fs_flags |= FS_UNCLEAN;
677 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
679 "WARNING: %s was not properly dismounted\n",
683 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
689 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
690 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
691 error = EROFS; /* needs translation */
694 ump = kmalloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
695 ump->um_malloctype = mtype;
696 ump->um_i_effnlink_valid = 1;
697 ump->um_fs = kmalloc((u_long)fs->fs_sbsize, M_UFSMNT,
699 bcopy(bp->b_data, ump->um_fs, (uint)fs->fs_sbsize);
700 if (fs->fs_sbsize < SBSIZE)
701 bp->b_flags |= B_INVAL;
705 fs->fs_ronly = ronly;
706 size = fs->fs_cssize;
707 blks = howmany(size, fs->fs_fsize);
708 if (fs->fs_contigsumsize > 0)
709 size += fs->fs_ncg * sizeof(int32_t);
710 size += fs->fs_ncg * sizeof(uint8_t);
711 space = kmalloc((u_long)size, M_UFSMNT, M_WAITOK);
713 for (i = 0; i < blks; i += fs->fs_frag) {
715 if (i + fs->fs_frag > blks)
716 size = (blks - i) * fs->fs_fsize;
717 if ((error = bread(devvp, fsbtodoff(fs, fs->fs_csaddr + i),
719 kfree(fs->fs_csp, M_UFSMNT);
722 bcopy(bp->b_data, space, (uint)size);
723 space = (char *)space + size;
727 if (fs->fs_contigsumsize > 0) {
728 fs->fs_maxcluster = lp = space;
729 for (i = 0; i < fs->fs_ncg; i++)
730 *lp++ = fs->fs_contigsumsize;
733 size = fs->fs_ncg * sizeof(uint8_t);
734 fs->fs_contigdirs = (uint8_t *)space;
735 bzero(fs->fs_contigdirs, size);
736 /* Compatibility for old filesystems XXX */
737 if (fs->fs_avgfilesize <= 0) /* XXX */
738 fs->fs_avgfilesize = AVFILESIZ; /* XXX */
739 if (fs->fs_avgfpdir <= 0) /* XXX */
740 fs->fs_avgfpdir = AFPDIR; /* XXX */
741 mp->mnt_data = (qaddr_t)ump;
742 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
743 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
744 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
745 vfs_getvfs(&mp->mnt_stat.f_fsid))
747 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
748 mp->mnt_flag |= MNT_LOCAL;
751 ump->um_devvp = devvp;
752 ump->um_nindir = fs->fs_nindir;
753 ump->um_bptrtodb = fs->fs_fsbtodb;
754 ump->um_seqinc = fs->fs_frag;
755 for (i = 0; i < MAXQUOTAS; i++)
756 ump->um_quotas[i] = NULLVP;
757 dev->si_mountpoint = mp;
760 /* restore "last mounted on" here */
761 bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt));
762 ksnprintf(fs->fs_fsmnt, sizeof(fs->fs_fsmnt),
763 "%s", mp->mnt_stat.f_mntonname);
765 if( mp->mnt_flag & MNT_ROOTFS) {
767 * Root mount; update timestamp in mount structure.
768 * this will be used by the common root mount code
769 * to update the system clock.
771 mp->mnt_time = fs->fs_time;
774 ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */
775 maxfilesize = (uint64_t)0x40000000 * fs->fs_bsize - 1; /* XXX */
776 /* Enforce limit caused by vm object backing (32 bits vm_pindex_t). */
777 if (maxfilesize > (uint64_t)0x80000000u * PAGE_SIZE - 1)
778 maxfilesize = (uint64_t)0x80000000u * PAGE_SIZE - 1;
779 if (fs->fs_maxfilesize > maxfilesize) /* XXX */
780 fs->fs_maxfilesize = maxfilesize; /* XXX */
782 if ((fs->fs_flags & FS_DOSOFTDEP) &&
783 (error = softdep_mount(devvp, mp, fs)) != 0) {
784 kfree(fs->fs_csp, M_UFSMNT);
789 (void) ffs_sbupdate(ump, MNT_WAIT);
791 vfs_add_vnodeops(mp, &ffs_vnode_vops, &mp->mnt_vn_norm_ops);
792 vfs_add_vnodeops(mp, &ffs_spec_vops, &mp->mnt_vn_spec_ops);
793 vfs_add_vnodeops(mp, &ffs_fifo_vops, &mp->mnt_vn_fifo_ops);
797 dev->si_mountpoint = NULL;
800 VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE);
802 kfree(ump->um_fs, M_UFSMNT);
803 kfree(ump, M_UFSMNT);
804 mp->mnt_data = (qaddr_t)0;
810 * Sanity checks for old filesystems.
812 * XXX - goes away some day.
815 ffs_oldfscompat(struct fs *fs)
817 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
818 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
819 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
820 fs->fs_nrpos = 8; /* XXX */
821 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
824 uint64_t sizepb = fs->fs_bsize; /* XXX */
826 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
827 for (i = 0; i < NIADDR; i++) { /* XXX */
828 sizepb *= NINDIR(fs); /* XXX */
829 fs->fs_maxfilesize += sizepb; /* XXX */
832 fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
833 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
834 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
840 * unmount system call
843 ffs_unmount(struct mount *mp, int mntflags)
845 struct ufsmount *ump;
850 if (mntflags & MNT_FORCE) {
853 if (mp->mnt_flag & MNT_SOFTDEP) {
854 if ((error = softdep_flushfiles(mp, flags)) != 0)
857 if ((error = ffs_flushfiles(mp, flags)) != 0)
862 if (fs->fs_ronly == 0) {
863 fs->fs_clean = fs->fs_flags & FS_UNCLEAN ? 0 : 1;
864 error = ffs_sbupdate(ump, MNT_WAIT);
870 ump->um_devvp->v_rdev->si_mountpoint = NULL;
872 vinvalbuf(ump->um_devvp, V_SAVE, 0, 0);
873 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE);
875 vrele(ump->um_devvp);
877 kfree(fs->fs_csp, M_UFSMNT);
879 kfree(ump, M_UFSMNT);
880 mp->mnt_data = (qaddr_t)0;
881 mp->mnt_flag &= ~MNT_LOCAL;
886 * Flush out all the files in a filesystem.
889 ffs_flushfiles(struct mount *mp, int flags)
891 struct ufsmount *ump;
896 if (mp->mnt_flag & MNT_QUOTA) {
898 error = vflush(mp, 0, SKIPSYSTEM|flags);
901 /* Find out how many quota files we have open. */
902 for (i = 0; i < MAXQUOTAS; i++) {
903 if (ump->um_quotas[i] == NULLVP)
908 * Here we fall through to vflush again to ensure
909 * that we have gotten rid of all the system vnodes.
914 * Flush all the files.
916 if ((error = vflush(mp, 0, flags)) != 0)
919 * Flush filesystem metadata.
921 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
922 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, 0);
923 vn_unlock(ump->um_devvp);
928 * Get filesystem statistics.
931 ffs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
933 struct ufsmount *ump;
938 if (fs->fs_magic != FS_MAGIC)
940 sbp->f_bsize = fs->fs_fsize;
941 sbp->f_iosize = fs->fs_bsize;
942 sbp->f_blocks = fs->fs_dsize;
943 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
944 fs->fs_cstotal.cs_nffree;
945 sbp->f_bavail = freespace(fs, fs->fs_minfree);
946 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
947 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
948 if (sbp != &mp->mnt_stat) {
949 sbp->f_type = mp->mnt_vfc->vfc_typenum;
950 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
951 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
957 * Go through the disk queues to initiate sandbagged IO;
958 * go through the inodes to write those that have been modified;
959 * initiate the writing of the super block if it has been modified.
961 * Note: we are always called with the filesystem marked `MPBUSY'.
965 static int ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
966 static int ffs_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
969 ffs_sync(struct mount *mp, int waitfor)
971 struct ufsmount *ump = VFSTOUFS(mp);
974 struct scaninfo scaninfo;
977 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
978 kprintf("fs = %s\n", fs->fs_fsmnt);
979 panic("ffs_sync: rofs mod");
983 * Write back each (modified) inode.
985 scaninfo.allerror = 0;
987 scaninfo.waitfor = waitfor;
988 while (scaninfo.rescan) {
990 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
991 ffs_sync_scan1, ffs_sync_scan2, &scaninfo);
995 * Force stale filesystem control information to be flushed.
997 if ((waitfor & MNT_LAZY) == 0) {
998 if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
999 waitfor = MNT_NOWAIT;
1000 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1001 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, 0)) != 0)
1002 scaninfo.allerror = error;
1003 vn_unlock(ump->um_devvp);
1009 * Write back modified superblock.
1011 if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
1012 scaninfo.allerror = error;
1013 return (scaninfo.allerror);
1017 ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
1022 * Depend on the mount list's vnode lock to keep things stable
1023 * enough for a quick test. Since there might be hundreds of
1024 * thousands of vnodes, we cannot afford even a subroutine
1025 * call unless there's a good chance that we have work to do.
1028 /* Restart out whole search if this guy is locked
1029 * or is being reclaimed.
1031 if (vp->v_type == VNON || ((ip->i_flag &
1032 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1033 RB_EMPTY(&vp->v_rbdirty_tree))) {
1040 ffs_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
1042 struct scaninfo *info = data;
1047 * We have to recheck after having obtained the vnode interlock.
1050 if (vp->v_type == VNON || vp->v_type == VBAD ||
1052 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1053 RB_EMPTY(&vp->v_rbdirty_tree))) {
1056 if (vp->v_type != VCHR) {
1057 if ((error = VOP_FSYNC(vp, info->waitfor, 0)) != 0)
1058 info->allerror = error;
1061 * We must reference the vp to prevent it from
1062 * getting ripped out from under ffs_update, since
1063 * we are not holding a vnode lock.
1065 /* ffs_update(vp, waitfor == MNT_WAIT); */
1072 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1073 * in from disk. If it is in core, wait for the lock bit to clear, then
1074 * return the inode locked. Detection and handling of mount points must be
1075 * done by the calling routine.
1079 ffs_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
1083 struct ufsmount *ump;
1092 if ((*vpp = ufs_ihashget(dev, ino)) != NULL) {
1097 * If this MALLOC() is performed after the getnewvnode()
1098 * it might block, leaving a vnode with a NULL v_data to be
1099 * found by ffs_sync() if a sync happens to fire right then,
1100 * which will cause a panic because ffs_sync() blindly
1101 * dereferences vp->v_data (as well it should).
1103 * XXX this may no longer be true since getnewvnode returns a
1104 * VX locked vnode now.
1106 MALLOC(ip, struct inode *, sizeof(struct inode),
1107 ump->um_malloctype, M_WAITOK);
1109 /* Allocate a new vnode/inode. */
1110 error = getnewvnode(VT_UFS, mp, &vp, VLKTIMEOUT, LK_CANRECURSE);
1113 kfree(ip, ump->um_malloctype);
1116 bzero((caddr_t)ip, sizeof(struct inode));
1118 ip->i_fs = fs = ump->um_fs;
1124 for (i = 0; i < MAXQUOTAS; i++)
1125 ip->i_dquot[i] = NODQUOT;
1130 * Insert it into the inode hash table and check for a collision.
1131 * If a collision occurs, throw away the vnode and try again.
1133 if (ufs_ihashins(ip) != 0) {
1134 kprintf("debug: ufs ihashins collision, retrying inode %ld\n",
1135 (long)ip->i_number);
1138 kfree(ip, ump->um_malloctype);
1143 /* Read in the disk contents for the inode, copy into the inode. */
1144 error = bread(ump->um_devvp, fsbtodoff(fs, ino_to_fsba(fs, ino)),
1145 (int)fs->fs_bsize, &bp);
1148 * The inode does not contain anything useful, so it would
1149 * be misleading to leave it on its hash chain. With mode
1150 * still zero, it will be unlinked and returned to the free
1159 ip->i_din = *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
1160 if (DOINGSOFTDEP(vp))
1161 softdep_load_inodeblock(ip);
1163 ip->i_effnlink = ip->i_nlink;
1167 * Initialize the vnode from the inode, check for aliases.
1168 * Note that the underlying vnode may have changed.
1170 error = ufs_vinit(mp, &vp);
1178 * Finish inode initialization now that aliasing has been resolved.
1180 ip->i_devvp = ump->um_devvp;
1183 * Set up a generation number for this inode if it does not
1184 * already have one. This should only happen on old filesystems.
1186 if (ip->i_gen == 0) {
1187 ip->i_gen = krandom() / 2 + 1;
1188 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1189 ip->i_flag |= IN_MODIFIED;
1192 * Ensure that uid and gid are correct. This is a temporary
1193 * fix until fsck has been changed to do the update.
1195 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1196 ip->i_uid = ip->i_din.di_ouid; /* XXX */
1197 ip->i_gid = ip->i_din.di_ogid; /* XXX */
1201 * return a VX locked and refd vnode (VX == same as normal vget()
1202 * vnode so we are ok)
1209 * File handle to vnode
1211 * Have to be really careful about stale file handles:
1212 * - check that the inode number is valid
1213 * - call ffs_vget() to get the locked inode
1214 * - check for an unallocated inode (i_mode == 0)
1215 * - check that the given client host has export rights and return
1216 * those rights via. exflagsp and credanonp
1219 ffs_fhtovp(struct mount *mp, struct vnode *rootvp,
1220 struct fid *fhp, struct vnode **vpp)
1225 ufhp = (struct ufid *)fhp;
1226 fs = VFSTOUFS(mp)->um_fs;
1227 if (ufhp->ufid_ino < ROOTINO ||
1228 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1230 return (ufs_fhtovp(mp, rootvp, ufhp, vpp));
1234 * Vnode pointer to File handle
1238 ffs_vptofh(struct vnode *vp, struct fid *fhp)
1244 ufhp = (struct ufid *)fhp;
1245 ufhp->ufid_len = sizeof(struct ufid);
1246 ufhp->ufid_ino = ip->i_number;
1247 ufhp->ufid_gen = ip->i_gen;
1252 * Initialize the filesystem; just use ufs_init.
1255 ffs_init(struct vfsconf *vfsp)
1257 softdep_initialize();
1258 kmalloc_raise_limit(M_FFSNODE, 0);
1259 return (ufs_init(vfsp));
1263 * Write a superblock and associated information back to disk.
1266 ffs_sbupdate(struct ufsmount *mp, int waitfor)
1268 struct fs *dfs, *fs = mp->um_fs;
1272 int i, size, error, allerror = 0;
1275 * First write back the summary information.
1277 * NOTE: the getblk is relative to the device vnode so bio1
1278 * contains the device block number.
1280 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1282 for (i = 0; i < blks; i += fs->fs_frag) {
1283 size = fs->fs_bsize;
1284 if (i + fs->fs_frag > blks)
1285 size = (blks - i) * fs->fs_fsize;
1286 bp = getblk(mp->um_devvp, fsbtodoff(fs, fs->fs_csaddr + i),
1288 bcopy(space, bp->b_data, (uint)size);
1289 space = (char *)space + size;
1290 if (waitfor != MNT_WAIT)
1292 else if ((error = bwrite(bp)) != 0)
1296 * Now write back the superblock itself. If any errors occurred
1297 * up to this point, then fail so that the superblock avoids
1298 * being written out as clean.
1302 bp = getblk(mp->um_devvp, SBOFF, (int)fs->fs_sbsize, 0, 0);
1304 fs->fs_time = time_second;
1305 bcopy((caddr_t)fs, bp->b_data, (uint)fs->fs_sbsize);
1306 /* Restore compatibility to old filesystems. XXX */
1307 dfs = (struct fs *)bp->b_data; /* XXX */
1308 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
1309 dfs->fs_nrpos = -1; /* XXX */
1310 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1311 int32_t *lp, tmp; /* XXX */
1313 lp = (int32_t *)&dfs->fs_qbmask; /* XXX */
1314 tmp = lp[4]; /* XXX */
1315 for (i = 4; i > 0; i--) /* XXX */
1316 lp[i] = lp[i-1]; /* XXX */
1317 lp[0] = tmp; /* XXX */
1319 dfs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */
1320 if (waitfor != MNT_WAIT)
1322 else if ((error = bwrite(bp)) != 0)