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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
30 * $FreeBSD: src/sys/ufs/ffs/ffs_vfsops.c,v 1.117.2.10 2002/06/23 22:34:52 iedowse Exp $
33 #include "opt_quota.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/nlookup.h>
40 #include <sys/kernel.h>
41 #include <sys/vnode.h>
42 #include <sys/mount.h>
45 #include <sys/fcntl.h>
46 #include <sys/diskslice.h>
47 #include <sys/malloc.h>
52 #include "ufs_extern.h"
55 #include "ffs_extern.h"
58 #include <vm/vm_page.h>
59 #include <vm/vm_zone.h>
63 static MALLOC_DEFINE(M_FFSNODE
, "FFS node", "FFS vnode private part");
65 static int ffs_sbupdate (struct ufsmount
*, int);
66 static int ffs_reload (struct mount
*, struct ucred
*);
67 static int ffs_oldfscompat (struct fs
*);
68 static int ffs_mount (struct mount
*, char *, caddr_t
, struct ucred
*);
69 static int ffs_init (struct vfsconf
*);
71 static struct vfsops ufs_vfsops
= {
73 .vfs_mount
= ffs_mount
,
74 .vfs_unmount
= ffs_unmount
,
76 .vfs_quotactl
= ufs_quotactl
,
77 .vfs_statfs
= ffs_statfs
,
80 .vfs_fhtovp
= ffs_fhtovp
,
81 .vfs_checkexp
= ufs_check_export
,
82 .vfs_vptofh
= ffs_vptofh
,
86 VFS_SET(ufs_vfsops
, ufs
, 0);
87 MODULE_VERSION(ufs
, 1);
89 extern struct vop_ops ffs_vnode_vops
;
90 extern struct vop_ops ffs_spec_vops
;
91 extern struct vop_ops ffs_fifo_vops
;
96 * Called when mounting local physical media
100 * mp mount point structure
101 * path NULL (flag for root mount!!!)
103 * p process (user credentials check [statfs])
106 * mp mount point structure
107 * path path to mount point
108 * data pointer to argument struct in user space
109 * p process (user credentials check)
112 * !0 error number (errno.h)
117 * mount point is locked
119 * mount point is locked
122 * A NULL path can be used for a flag since the mount
123 * system call will fail with EFAULT in copyinstr in
124 * nlookup() if it is a genuine NULL from the user.
127 ffs_mount(struct mount
*mp
, /* mount struct pointer */
128 char *path
, /* path to mount point */
129 caddr_t data
, /* arguments to FS specific mount */
130 struct ucred
*cred
) /* process requesting mount */
136 struct ufs_args args
;
137 struct ufsmount
*ump
= NULL
;
139 int flags
, ronly
= 0;
141 struct nlookupdata nd
;
142 struct vnode
*rootvp
;
148 * Use NULL path to flag a root mount
153 * Mounting root filesystem
157 if ((error
= bdevvp(rootdev
, &rootvp
))) {
158 kprintf("ffs_mountroot: can't find rootvp\n");
162 if( ( error
= ffs_mountfs(rootvp
, mp
, M_FFSNODE
)) != 0) {
163 /* fs specific cleanup (if any)*/
168 goto dostatfs
; /* success*/
174 * Mounting non-root filesystem or updating a filesystem
178 /* copy in user arguments*/
179 error
= copyin(data
, (caddr_t
)&args
, sizeof (struct ufs_args
));
181 goto error_1
; /* can't get arguments*/
184 * If updating, check whether changing from read-only to
185 * read/write; if there is no device name, that's all we do.
187 if (mp
->mnt_flag
& MNT_UPDATE
) {
190 devvp
= ump
->um_devvp
;
192 ronly
= fs
->fs_ronly
; /* MNT_RELOAD might change this */
193 if (ronly
== 0 && (mp
->mnt_flag
& MNT_RDONLY
)) {
195 * Flush any dirty data.
197 VFS_SYNC(mp
, MNT_WAIT
);
199 * Check for and optionally get rid of files open
203 if (mp
->mnt_flag
& MNT_FORCE
)
205 if (mp
->mnt_flag
& MNT_SOFTDEP
) {
206 error
= softdep_flushfiles(mp
, flags
);
208 error
= ffs_flushfiles(mp
, flags
);
212 if (!error
&& (mp
->mnt_flag
& MNT_RELOAD
)) {
213 error
= ffs_reload(mp
, NULL
);
218 if (ronly
&& (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)) {
220 * If upgrade to read-write by non-root, then verify
221 * that user has necessary permissions on the device.
223 if (cred
->cr_uid
!= 0) {
224 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
225 if ((error
= VOP_EACCESS(devvp
, VREAD
| VWRITE
,
233 fs
->fs_flags
&= ~FS_UNCLEAN
;
234 if (fs
->fs_clean
== 0) {
235 fs
->fs_flags
|= FS_UNCLEAN
;
236 if (mp
->mnt_flag
& MNT_FORCE
) {
238 "WARNING: %s was not properly dismounted\n",
242 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
249 /* check to see if we need to start softdep */
250 if (fs
->fs_flags
& FS_DOSOFTDEP
) {
251 error
= softdep_mount(devvp
, mp
, fs
);
258 * Soft updates is incompatible with "async",
259 * so if we are doing softupdates stop the user
260 * from setting the async flag in an update.
261 * Softdep_mount() clears it in an initial mount
264 if (mp
->mnt_flag
& MNT_SOFTDEP
) {
265 mp
->mnt_flag
&= ~MNT_ASYNC
;
267 /* if not updating name...*/
268 if (args
.fspec
== NULL
) {
270 * Process export requests. Jumping to "success"
271 * will return the vfs_export() error code.
273 error
= vfs_export(mp
, &ump
->um_export
, &args
.export
);
279 * Not an update, or updating the name: look up the name
280 * and verify that it refers to a sensible block device.
283 error
= nlookup_init(&nd
, args
.fspec
, UIO_USERSPACE
, NLC_FOLLOW
);
285 error
= nlookup(&nd
);
287 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &devvp
);
292 if (!vn_isdisk(devvp
, &error
))
296 * If mount by non-root, then verify that user has necessary
297 * permissions on the device.
299 if (cred
->cr_uid
!= 0) {
301 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0)
302 accessmode
|= VWRITE
;
303 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
304 if ((error
= VOP_EACCESS(devvp
, accessmode
, cred
)) != 0) {
311 if (mp
->mnt_flag
& MNT_UPDATE
) {
313 * UPDATE - make sure the resolved vnode represents the same
314 * device. Since devfs, devvp->v_rdev can be used directly as
315 * it is always associated as long as the vnode exists.
317 * Our current open/writecount state is associated with
318 * um_devvp, so continue using um_devvp and throw away devvp.
320 if (devvp
!= ump
->um_devvp
) {
321 if (devvp
->v_rdev
== ump
->um_devvp
->v_rdev
) {
323 devvp
= ump
->um_devvp
;
325 kprintf("cannot update mount, v_rdev does"
326 " not match (%p vs %p)\n",
327 devvp
->v_rdev
, ump
->um_devvp
->v_rdev
);
328 error
= EINVAL
; /* needs translation */
334 * Update device name only on success
337 /* Save "mounted from" info for mount point (NULL pad)*/
338 copyinstr( args
.fspec
,
339 mp
->mnt_stat
.f_mntfromname
,
342 bzero( mp
->mnt_stat
.f_mntfromname
+ size
, MNAMELEN
- size
);
351 /* Save "mounted from" info for mount point (NULL pad)*/
352 copyinstr( args
.fspec
, /* device name*/
353 mp
->mnt_stat
.f_mntfromname
, /* save area*/
354 MNAMELEN
- 1, /* max size*/
355 &size
); /* real size*/
356 bzero( mp
->mnt_stat
.f_mntfromname
+ size
, MNAMELEN
- size
);
358 /* Save "last mounted on" info for mount point (NULL pad)*/
359 bzero(mp
->mnt_stat
.f_mntonname
,
360 sizeof(mp
->mnt_stat
.f_mntonname
));
362 copyinstr(path
, mp
->mnt_stat
.f_mntonname
,
363 sizeof(mp
->mnt_stat
.f_mntonname
) - 1,
365 } else { /* Root mount */
366 mp
->mnt_stat
.f_mntonname
[0] = '/';
369 error
= ffs_mountfs(devvp
, mp
, M_FFSNODE
);
377 * Initialize FS stat information in mount struct; uses
378 * mp->mnt_stat.f_mntfromname.
380 * This code is common to root and non-root mounts
382 (void)VFS_STATFS(mp
, &mp
->mnt_stat
, cred
);
387 error_2
: /* error with devvp held*/
389 /* release devvp before failing*/
392 error_1
: /* no state to back out*/
395 if (!error
&& path
&& (mp
->mnt_flag
& MNT_UPDATE
)) {
396 /* Update clean flag after changing read-onlyness. */
398 if (ronly
!= fs
->fs_ronly
) {
399 fs
->fs_ronly
= ronly
;
400 fs
->fs_clean
= ronly
&&
401 (fs
->fs_flags
& FS_UNCLEAN
) == 0 ? 1 : 0;
404 * The device must be re-opened as appropriate or
405 * the device close at unmount time will panic.
407 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
409 VOP_OPEN(devvp
, FREAD
, FSCRED
, NULL
);
410 VOP_CLOSE(devvp
, FREAD
|FWRITE
, NULL
);
412 VOP_OPEN(devvp
, FREAD
|FWRITE
, FSCRED
, NULL
);
413 VOP_CLOSE(devvp
, FREAD
, NULL
);
416 ffs_sbupdate(ump
, MNT_WAIT
);
423 * Reload all incore data for a filesystem (used after running fsck on
424 * the root filesystem and finding things to fix). The filesystem must
425 * be mounted read-only.
427 * Things to do to update the mount:
428 * 1) invalidate all cached meta-data.
429 * 2) re-read superblock from disk.
430 * 3) re-read summary information from disk.
431 * 4) invalidate all inactive vnodes.
432 * 5) invalidate all cached file data.
433 * 6) re-read inode data for all active vnodes.
436 static int ffs_reload_scan2(struct mount
*mp
, struct vnode
*vp
, void *data
);
447 ffs_reload(struct mount
*mp
, struct ucred
*cred
)
452 struct fs
*fs
, *newfs
;
453 int i
, blks
, size
, error
;
454 struct scaninfo scaninfo
;
457 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0)
460 * Step 1: invalidate all cached meta-data.
462 devvp
= VFSTOUFS(mp
)->um_devvp
;
463 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
464 error
= vinvalbuf(devvp
, 0, 0, 0);
467 panic("ffs_reload: dirty1");
470 * The backing device must be VMIO-capable because we use getblk().
471 * NOTE: the MFS driver now returns a VMIO-enabled descriptor.
473 if (devvp
->v_object
== NULL
)
474 panic("ffs_reload: devvp has no VM object!");
477 * Step 2: re-read superblock from disk.
479 if ((error
= bread(devvp
, SBOFF
, SBSIZE
, &bp
)) != 0) {
483 newfs
= (struct fs
*)bp
->b_data
;
484 if (newfs
->fs_magic
!= FS_MAGIC
|| newfs
->fs_bsize
> MAXBSIZE
||
485 newfs
->fs_bsize
< sizeof(struct fs
)) {
487 return (EIO
); /* XXX needs translation */
489 fs
= VFSTOUFS(mp
)->um_fs
;
491 * Copy pointer fields back into superblock before copying in XXX
492 * new superblock. These should really be in the ufsmount. XXX
493 * Note that important parameters (eg fs_ncg) are unchanged.
495 newfs
->fs_csp
= fs
->fs_csp
;
496 newfs
->fs_maxcluster
= fs
->fs_maxcluster
;
497 newfs
->fs_contigdirs
= fs
->fs_contigdirs
;
498 /* The filesystem is still read-only. */
500 bcopy(newfs
, fs
, (uint
)fs
->fs_sbsize
);
501 if (fs
->fs_sbsize
< SBSIZE
)
502 bp
->b_flags
|= B_INVAL
;
504 mp
->mnt_maxsymlinklen
= fs
->fs_maxsymlinklen
;
506 /* An old fsck may have zeroed these fields, so recheck them. */
507 if (fs
->fs_avgfilesize
<= 0) /* XXX */
508 fs
->fs_avgfilesize
= AVFILESIZ
; /* XXX */
509 if (fs
->fs_avgfpdir
<= 0) /* XXX */
510 fs
->fs_avgfpdir
= AFPDIR
; /* XXX */
513 * Step 3: re-read summary information from disk.
515 blks
= howmany(fs
->fs_cssize
, fs
->fs_fsize
);
517 for (i
= 0; i
< blks
; i
+= fs
->fs_frag
) {
519 if (i
+ fs
->fs_frag
> blks
)
520 size
= (blks
- i
) * fs
->fs_fsize
;
521 error
= bread(devvp
, fsbtodoff(fs
, fs
->fs_csaddr
+ i
), size
, &bp
);
526 bcopy(bp
->b_data
, space
, (uint
)size
);
527 space
= (char *)space
+ size
;
531 * We no longer know anything about clusters per cylinder group.
533 if (fs
->fs_contigsumsize
> 0) {
534 lp
= fs
->fs_maxcluster
;
535 for (i
= 0; i
< fs
->fs_ncg
; i
++)
536 *lp
++ = fs
->fs_contigsumsize
;
541 scaninfo
.devvp
= devvp
;
542 while (error
== 0 && scaninfo
.rescan
) {
544 error
= vmntvnodescan(mp
, VMSC_GETVX
,
545 NULL
, ffs_reload_scan2
, &scaninfo
);
551 ffs_reload_scan2(struct mount
*mp
, struct vnode
*vp
, void *data
)
553 struct scaninfo
*info
= data
;
564 if (vinvalbuf(vp
, 0, 0, 0))
565 panic("ffs_reload: dirty2");
567 * Step 6: re-read inode data for all active vnodes.
570 error
= bread(info
->devvp
,
571 fsbtodoff(info
->fs
, ino_to_fsba(info
->fs
, ip
->i_number
)),
572 (int)info
->fs
->fs_bsize
, &bp
);
577 ip
->i_din
= *((struct ufs1_dinode
*)bp
->b_data
+
578 ino_to_fsbo(info
->fs
, ip
->i_number
));
579 ip
->i_effnlink
= ip
->i_nlink
;
585 * Common code for mount and mountroot
588 ffs_mountfs(struct vnode
*devvp
, struct mount
*mp
, struct malloc_type
*mtype
)
591 struct ufsmount
*ump
;
596 int error
, i
, blks
, size
, ronly
;
598 uint64_t maxfilesize
; /* XXX */
601 * Disallow multiple mounts of the same device.
602 * Disallow mounting of a device that is currently in use
603 * Flush out any old buffers remaining from a previous use.
605 error
= vfs_mountedon(devvp
);
608 if (vcount(devvp
) > 0)
610 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
611 error
= vinvalbuf(devvp
, V_SAVE
, 0, 0);
616 ronly
= (mp
->mnt_flag
& MNT_RDONLY
) != 0;
617 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
618 error
= VOP_OPEN(devvp
, ronly
? FREAD
: FREAD
|FWRITE
, FSCRED
, NULL
);
623 if (dev
->si_iosize_max
!= 0)
624 mp
->mnt_iosize_max
= dev
->si_iosize_max
;
625 if (mp
->mnt_iosize_max
> MAXPHYS
)
626 mp
->mnt_iosize_max
= MAXPHYS
;
629 * The backing device must be VMIO-capable because we use getblk().
630 * NOTE: the MFS driver now returns a VMIO-enabled descriptor.
631 * The VOP_OPEN() call above should have associated a VM object
634 if (devvp
->v_object
== NULL
)
635 panic("ffs_reload: devvp has no VM object!");
639 if ((error
= bread(devvp
, SBOFF
, SBSIZE
, &bp
)) != 0)
641 fs
= (struct fs
*)bp
->b_data
;
642 if (fs
->fs_magic
!= FS_MAGIC
|| fs
->fs_bsize
> MAXBSIZE
||
643 fs
->fs_bsize
< sizeof(struct fs
)) {
644 error
= EINVAL
; /* XXX needs translation */
648 fs
->fs_flags
&= ~FS_UNCLEAN
;
649 if (fs
->fs_clean
== 0) {
650 fs
->fs_flags
|= FS_UNCLEAN
;
651 if (ronly
|| (mp
->mnt_flag
& MNT_FORCE
)) {
653 "WARNING: %s was not properly dismounted\n",
657 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
663 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
664 if (fs
->fs_postblformat
== FS_42POSTBLFMT
&& !ronly
) {
665 error
= EROFS
; /* needs translation */
668 ump
= kmalloc(sizeof *ump
, M_UFSMNT
, M_WAITOK
| M_ZERO
);
669 ump
->um_malloctype
= mtype
;
670 ump
->um_i_effnlink_valid
= 1;
671 ump
->um_fs
= kmalloc((u_long
)fs
->fs_sbsize
, M_UFSMNT
,
673 bcopy(bp
->b_data
, ump
->um_fs
, (uint
)fs
->fs_sbsize
);
674 if (fs
->fs_sbsize
< SBSIZE
)
675 bp
->b_flags
|= B_INVAL
;
679 fs
->fs_ronly
= ronly
;
680 size
= fs
->fs_cssize
;
681 blks
= howmany(size
, fs
->fs_fsize
);
682 if (fs
->fs_contigsumsize
> 0)
683 size
+= fs
->fs_ncg
* sizeof(int32_t);
684 size
+= fs
->fs_ncg
* sizeof(uint8_t);
685 space
= kmalloc((u_long
)size
, M_UFSMNT
, M_WAITOK
);
687 for (i
= 0; i
< blks
; i
+= fs
->fs_frag
) {
689 if (i
+ fs
->fs_frag
> blks
)
690 size
= (blks
- i
) * fs
->fs_fsize
;
691 if ((error
= bread(devvp
, fsbtodoff(fs
, fs
->fs_csaddr
+ i
),
693 kfree(fs
->fs_csp
, M_UFSMNT
);
696 bcopy(bp
->b_data
, space
, (uint
)size
);
697 space
= (char *)space
+ size
;
701 if (fs
->fs_contigsumsize
> 0) {
702 fs
->fs_maxcluster
= lp
= space
;
703 for (i
= 0; i
< fs
->fs_ncg
; i
++)
704 *lp
++ = fs
->fs_contigsumsize
;
707 size
= fs
->fs_ncg
* sizeof(uint8_t);
708 fs
->fs_contigdirs
= (uint8_t *)space
;
709 bzero(fs
->fs_contigdirs
, size
);
710 /* Compatibility for old filesystems XXX */
711 if (fs
->fs_avgfilesize
<= 0) /* XXX */
712 fs
->fs_avgfilesize
= AVFILESIZ
; /* XXX */
713 if (fs
->fs_avgfpdir
<= 0) /* XXX */
714 fs
->fs_avgfpdir
= AFPDIR
; /* XXX */
715 mp
->mnt_data
= (qaddr_t
)ump
;
716 mp
->mnt_stat
.f_fsid
.val
[0] = fs
->fs_id
[0];
717 mp
->mnt_stat
.f_fsid
.val
[1] = fs
->fs_id
[1];
718 if (fs
->fs_id
[0] == 0 || fs
->fs_id
[1] == 0) {
720 } else if ((mptmp
= vfs_getvfs(&mp
->mnt_stat
.f_fsid
)) != NULL
) {
724 mp
->mnt_maxsymlinklen
= fs
->fs_maxsymlinklen
;
725 mp
->mnt_flag
|= MNT_LOCAL
;
728 ump
->um_devvp
= devvp
;
729 ump
->um_nindir
= fs
->fs_nindir
;
730 ump
->um_bptrtodb
= fs
->fs_fsbtodb
;
731 ump
->um_seqinc
= fs
->fs_frag
;
732 for (i
= 0; i
< MAXQUOTAS
; i
++)
733 ump
->um_quotas
[i
] = NULLVP
;
734 dev
->si_mountpoint
= mp
;
737 /* restore "last mounted on" here */
738 bzero(fs
->fs_fsmnt
, sizeof(fs
->fs_fsmnt
));
739 ksnprintf(fs
->fs_fsmnt
, sizeof(fs
->fs_fsmnt
),
740 "%s", mp
->mnt_stat
.f_mntonname
);
742 if( mp
->mnt_flag
& MNT_ROOTFS
) {
744 * Root mount; update timestamp in mount structure.
745 * this will be used by the common root mount code
746 * to update the system clock.
748 mp
->mnt_time
= fs
->fs_time
;
752 ump
->um_savedmaxfilesize
= fs
->fs_maxfilesize
; /* XXX */
753 maxfilesize
= (uint64_t)0x40000000 * fs
->fs_bsize
- 1; /* XXX */
754 /* Enforce limit caused by vm object backing (32 bits vm_pindex_t). */
755 if (maxfilesize
> (uint64_t)0x80000000u
* PAGE_SIZE
- 1)
756 maxfilesize
= (uint64_t)0x80000000u
* PAGE_SIZE
- 1;
757 if (fs
->fs_maxfilesize
> maxfilesize
) /* XXX */
758 fs
->fs_maxfilesize
= maxfilesize
; /* XXX */
760 if ((fs
->fs_flags
& FS_DOSOFTDEP
) &&
761 (error
= softdep_mount(devvp
, mp
, fs
)) != 0) {
762 kfree(fs
->fs_csp
, M_UFSMNT
);
767 (void) ffs_sbupdate(ump
, MNT_WAIT
);
769 vfs_add_vnodeops(mp
, &ffs_vnode_vops
, &mp
->mnt_vn_norm_ops
);
770 vfs_add_vnodeops(mp
, &ffs_spec_vops
, &mp
->mnt_vn_spec_ops
);
771 vfs_add_vnodeops(mp
, &ffs_fifo_vops
, &mp
->mnt_vn_fifo_ops
);
775 dev
->si_mountpoint
= NULL
;
778 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
779 VOP_CLOSE(devvp
, ronly
? FREAD
: FREAD
|FWRITE
, NULL
);
782 ufs_ihashuninit(ump
);
783 kfree(ump
->um_fs
, M_UFSMNT
);
784 kfree(ump
, M_UFSMNT
);
785 mp
->mnt_data
= (qaddr_t
)0;
791 * Sanity checks for old filesystems.
793 * XXX - goes away some day.
796 ffs_oldfscompat(struct fs
*fs
)
798 fs
->fs_npsect
= max(fs
->fs_npsect
, fs
->fs_nsect
); /* XXX */
799 fs
->fs_interleave
= max(fs
->fs_interleave
, 1); /* XXX */
800 if (fs
->fs_postblformat
== FS_42POSTBLFMT
) /* XXX */
801 fs
->fs_nrpos
= 8; /* XXX */
802 if (fs
->fs_inodefmt
< FS_44INODEFMT
) { /* XXX */
805 uint64_t sizepb
= fs
->fs_bsize
; /* XXX */
807 fs
->fs_maxfilesize
= fs
->fs_bsize
* UFS_NDADDR
- 1; /* XXX */
808 for (i
= 0; i
< UFS_NIADDR
; i
++) { /* XXX */
809 sizepb
*= NINDIR(fs
); /* XXX */
810 fs
->fs_maxfilesize
+= sizepb
; /* XXX */
813 fs
->fs_maxfilesize
= (u_quad_t
) 1LL << 39;
814 fs
->fs_qbmask
= ~fs
->fs_bmask
; /* XXX */
815 fs
->fs_qfmask
= ~fs
->fs_fmask
; /* XXX */
821 * unmount system call
824 ffs_unmount(struct mount
*mp
, int mntflags
)
826 struct ufsmount
*ump
;
831 if (mntflags
& MNT_FORCE
) {
834 if (mp
->mnt_flag
& MNT_SOFTDEP
) {
835 if ((error
= softdep_flushfiles(mp
, flags
)) != 0)
838 if ((error
= ffs_flushfiles(mp
, flags
)) != 0)
843 if (fs
->fs_ronly
== 0) {
844 fs
->fs_clean
= fs
->fs_flags
& FS_UNCLEAN
? 0 : 1;
845 error
= ffs_sbupdate(ump
, MNT_WAIT
);
851 ump
->um_devvp
->v_rdev
->si_mountpoint
= NULL
;
853 vn_lock(ump
->um_devvp
, LK_EXCLUSIVE
| LK_RETRY
);
854 vinvalbuf(ump
->um_devvp
, V_SAVE
, 0, 0);
855 error
= VOP_CLOSE(ump
->um_devvp
,
856 fs
->fs_ronly
? FREAD
: FREAD
|FWRITE
,
858 vn_unlock(ump
->um_devvp
);
860 vrele(ump
->um_devvp
);
862 ufs_ihashuninit(ump
);
863 kfree(fs
->fs_csp
, M_UFSMNT
);
865 kfree(ump
, M_UFSMNT
);
866 mp
->mnt_data
= (qaddr_t
)0;
867 mp
->mnt_flag
&= ~MNT_LOCAL
;
872 * Flush out all the files in a filesystem.
875 ffs_flushfiles(struct mount
*mp
, int flags
)
877 struct ufsmount
*ump
;
882 if (mp
->mnt_flag
& MNT_QUOTA
) {
884 error
= vflush(mp
, 0, SKIPSYSTEM
|flags
);
887 /* Find out how many quota files we have open. */
888 for (i
= 0; i
< MAXQUOTAS
; i
++) {
889 if (ump
->um_quotas
[i
] == NULLVP
)
894 * Here we fall through to vflush again to ensure
895 * that we have gotten rid of all the system vnodes.
900 * Flush all the files.
902 if ((error
= vflush(mp
, 0, flags
)) != 0)
905 * Flush filesystem metadata.
907 vn_lock(ump
->um_devvp
, LK_EXCLUSIVE
| LK_RETRY
);
908 error
= VOP_FSYNC(ump
->um_devvp
, MNT_WAIT
, 0);
909 vn_unlock(ump
->um_devvp
);
914 * Get filesystem statistics.
917 ffs_statfs(struct mount
*mp
, struct statfs
*sbp
, struct ucred
*cred
)
919 struct ufsmount
*ump
;
924 if (fs
->fs_magic
!= FS_MAGIC
)
926 sbp
->f_bsize
= fs
->fs_fsize
;
927 sbp
->f_iosize
= fs
->fs_bsize
;
928 sbp
->f_blocks
= fs
->fs_dsize
;
929 sbp
->f_bfree
= fs
->fs_cstotal
.cs_nbfree
* fs
->fs_frag
+
930 fs
->fs_cstotal
.cs_nffree
;
931 sbp
->f_bavail
= freespace(fs
, fs
->fs_minfree
);
932 sbp
->f_files
= fs
->fs_ncg
* fs
->fs_ipg
- UFS_ROOTINO
;
933 sbp
->f_ffree
= fs
->fs_cstotal
.cs_nifree
;
934 if (sbp
!= &mp
->mnt_stat
) {
935 sbp
->f_type
= mp
->mnt_vfc
->vfc_typenum
;
936 bcopy((caddr_t
)mp
->mnt_stat
.f_mntfromname
,
937 (caddr_t
)&sbp
->f_mntfromname
[0], MNAMELEN
);
943 * Go through the disk queues to initiate sandbagged IO;
944 * go through the inodes to write those that have been modified;
945 * initiate the writing of the super block if it has been modified.
947 * Note: we are always called with the filesystem marked `MPBUSY'.
951 static int ffs_sync_scan1(struct mount
*mp
, struct vnode
*vp
, void *data
);
952 static int ffs_sync_scan2(struct mount
*mp
, struct vnode
*vp
, void *data
);
955 ffs_sync(struct mount
*mp
, int waitfor
)
957 struct ufsmount
*ump
= VFSTOUFS(mp
);
960 struct scaninfo scaninfo
;
963 if (fs
->fs_fmod
!= 0 && fs
->fs_ronly
!= 0) { /* XXX */
964 kprintf("fs = %s\n", fs
->fs_fsmnt
);
965 panic("ffs_sync: rofs mod");
969 * Write back each (modified) inode.
971 scaninfo
.allerror
= 0;
973 scaninfo
.waitfor
= waitfor
;
974 while (scaninfo
.rescan
) {
976 vmntvnodescan(mp
, VMSC_GETVP
|VMSC_NOWAIT
,
977 ffs_sync_scan1
, ffs_sync_scan2
, &scaninfo
);
981 * Force stale filesystem control information to be flushed.
983 if ((waitfor
& MNT_LAZY
) == 0) {
984 if (ump
->um_mountp
->mnt_flag
& MNT_SOFTDEP
)
985 waitfor
= MNT_NOWAIT
;
986 vn_lock(ump
->um_devvp
, LK_EXCLUSIVE
| LK_RETRY
);
987 if ((error
= VOP_FSYNC(ump
->um_devvp
, waitfor
, 0)) != 0)
988 scaninfo
.allerror
= error
;
989 vn_unlock(ump
->um_devvp
);
995 * Write back modified superblock.
997 if (fs
->fs_fmod
!= 0 && (error
= ffs_sbupdate(ump
, waitfor
)) != 0)
998 scaninfo
.allerror
= error
;
999 return (scaninfo
.allerror
);
1003 ffs_sync_scan1(struct mount
*mp
, struct vnode
*vp
, void *data
)
1008 * Depend on the mount list's vnode lock to keep things stable
1009 * enough for a quick test. Since there might be hundreds of
1010 * thousands of vnodes, we cannot afford even a subroutine
1011 * call unless there's a good chance that we have work to do.
1014 /* Restart out whole search if this guy is locked
1015 * or is being reclaimed.
1017 if (vp
->v_type
== VNON
|| (ip
== NULL
) || ((ip
->i_flag
&
1018 (IN_ACCESS
| IN_CHANGE
| IN_MODIFIED
| IN_UPDATE
)) == 0 &&
1019 RB_EMPTY(&vp
->v_rbdirty_tree
))) {
1026 ffs_sync_scan2(struct mount
*mp
, struct vnode
*vp
, void *data
)
1028 struct scaninfo
*info
= data
;
1033 * We have to recheck after having obtained the vnode interlock.
1036 if (vp
->v_type
== VNON
|| vp
->v_type
== VBAD
||
1038 (IN_ACCESS
| IN_CHANGE
| IN_MODIFIED
| IN_UPDATE
)) == 0 &&
1039 RB_EMPTY(&vp
->v_rbdirty_tree
))) {
1042 if (vp
->v_type
!= VCHR
) {
1043 if ((error
= VOP_FSYNC(vp
, info
->waitfor
, 0)) != 0)
1044 info
->allerror
= error
;
1047 * We must reference the vp to prevent it from
1048 * getting ripped out from under ffs_update, since
1049 * we are not holding a vnode lock.
1051 /* ffs_update(vp, waitfor == MNT_WAIT); */
1058 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1059 * in from disk. If it is in core, wait for the lock bit to clear, then
1060 * return the inode locked. Detection and handling of mount points must be
1061 * done by the calling routine.
1065 ffs_vget(struct mount
*mp
, struct vnode
*dvp
, ino_t ino
, struct vnode
**vpp
)
1069 struct ufsmount
*ump
;
1079 if ((*vpp
= ufs_ihashget(ump
, dev
, ino
)) != NULL
) {
1084 * If this MALLOC() is performed after the getnewvnode()
1085 * it might block, leaving a vnode with a NULL v_data to be
1086 * found by ffs_sync() if a sync happens to fire right then,
1087 * which will cause a panic because ffs_sync() blindly
1088 * dereferences vp->v_data (as well it should).
1090 * XXX this may no longer be true since getnewvnode returns a
1091 * VX locked vnode now.
1093 ip
= kmalloc(sizeof(struct inode
), ump
->um_malloctype
,
1096 /* Allocate a new vnode/inode. */
1097 error
= getnewvnode(VT_UFS
, mp
, &vp
, VLKTIMEOUT
, LK_CANRECURSE
);
1100 kfree(ip
, ump
->um_malloctype
);
1104 ip
->i_fs
= fs
= ump
->um_fs
;
1110 for (i
= 0; i
< MAXQUOTAS
; i
++)
1111 ip
->i_dquot
[i
] = NODQUOT
;
1116 * Insert it into the inode hash table.
1118 if (ufs_ihashins(ump
, ip
) != 0) {
1119 kprintf("debug: ufs ihashins collision, retrying inode %ld\n",
1120 (long) ip
->i_number
);
1124 kfree(ip
, ump
->um_malloctype
);
1129 /* Read in the disk contents for the inode, copy into the inode. */
1130 error
= bread(ump
->um_devvp
, fsbtodoff(fs
, ino_to_fsba(fs
, ino
)),
1131 (int)fs
->fs_bsize
, &bp
);
1134 * The inode does not contain anything useful, so it would
1135 * be misleading to leave it on its hash chain. With mode
1136 * still zero, it will be unlinked and returned to the free
1145 ip
->i_din
= *((struct ufs1_dinode
*)bp
->b_data
+ ino_to_fsbo(fs
, ino
));
1146 if (DOINGSOFTDEP(vp
))
1147 softdep_load_inodeblock(ip
);
1149 ip
->i_effnlink
= ip
->i_nlink
;
1153 * Initialize the vnode from the inode, check for aliases.
1154 * Note that the underlying vnode may have changed.
1156 error
= ufs_vinit(mp
, &vp
);
1164 * Finish inode initialization now that aliasing has been resolved.
1166 ip
->i_devvp
= ump
->um_devvp
;
1169 * Set up a generation number for this inode if it does not
1170 * already have one. This should only happen on old filesystems.
1172 if (ip
->i_gen
== 0) {
1173 ip
->i_gen
= krandom() / 2 + 1;
1174 if ((vp
->v_mount
->mnt_flag
& MNT_RDONLY
) == 0)
1175 ip
->i_flag
|= IN_MODIFIED
;
1178 * Ensure that uid and gid are correct. This is a temporary
1179 * fix until fsck has been changed to do the update.
1181 if (fs
->fs_inodefmt
< FS_44INODEFMT
) { /* XXX */
1182 ip
->i_uid
= ip
->i_din
.di_ouid
; /* XXX */
1183 ip
->i_gid
= ip
->i_din
.di_ogid
; /* XXX */
1187 * return a VX locked and refd vnode (VX == same as normal vget()
1188 * vnode so we are ok)
1197 * File handle to vnode
1199 * Have to be really careful about stale file handles:
1200 * - check that the inode number is valid
1201 * - call ffs_vget() to get the locked inode
1202 * - check for an unallocated inode (i_mode == 0)
1203 * - check that the given client host has export rights and return
1204 * those rights via. exflagsp and credanonp
1207 ffs_fhtovp(struct mount
*mp
, struct vnode
*rootvp
,
1208 struct fid
*fhp
, struct vnode
**vpp
)
1213 ufhp
= (struct ufid
*)fhp
;
1214 fs
= VFSTOUFS(mp
)->um_fs
;
1215 if (ufhp
->ufid_ino
< UFS_ROOTINO
||
1216 ufhp
->ufid_ino
>= fs
->fs_ncg
* fs
->fs_ipg
)
1218 return (ufs_fhtovp(mp
, rootvp
, ufhp
, vpp
));
1222 * Vnode pointer to File handle
1226 ffs_vptofh(struct vnode
*vp
, struct fid
*fhp
)
1232 ufhp
= (struct ufid
*)fhp
;
1233 ufhp
->ufid_len
= sizeof(struct ufid
);
1234 ufhp
->ufid_ino
= ip
->i_number
;
1235 ufhp
->ufid_gen
= ip
->i_gen
;
1240 * Initialize the filesystem; just use ufs_init.
1243 ffs_init(struct vfsconf
*vfsp
)
1245 softdep_initialize();
1246 kmalloc_raise_limit(M_FFSNODE
, 0);
1247 return (ufs_init(vfsp
));
1251 * Write a superblock and associated information back to disk.
1254 ffs_sbupdate(struct ufsmount
*mp
, int waitfor
)
1256 struct fs
*dfs
, *fs
= mp
->um_fs
;
1260 int i
, size
, error
, allerror
= 0;
1263 * First write back the summary information.
1265 * NOTE: the getblk is relative to the device vnode so bio1
1266 * contains the device block number.
1268 blks
= howmany(fs
->fs_cssize
, fs
->fs_fsize
);
1270 for (i
= 0; i
< blks
; i
+= fs
->fs_frag
) {
1271 size
= fs
->fs_bsize
;
1272 if (i
+ fs
->fs_frag
> blks
)
1273 size
= (blks
- i
) * fs
->fs_fsize
;
1274 bp
= getblk(mp
->um_devvp
, fsbtodoff(fs
, fs
->fs_csaddr
+ i
),
1276 bcopy(space
, bp
->b_data
, (uint
)size
);
1277 space
= (char *)space
+ size
;
1278 if (waitfor
!= MNT_WAIT
)
1280 else if ((error
= bwrite(bp
)) != 0)
1284 * Now write back the superblock itself. If any errors occurred
1285 * up to this point, then fail so that the superblock avoids
1286 * being written out as clean.
1290 bp
= getblk(mp
->um_devvp
, SBOFF
, (int)fs
->fs_sbsize
, 0, 0);
1292 fs
->fs_time
= time_second
;
1293 bcopy((caddr_t
)fs
, bp
->b_data
, (uint
)fs
->fs_sbsize
);
1294 /* Restore compatibility to old filesystems. XXX */
1295 dfs
= (struct fs
*)bp
->b_data
; /* XXX */
1296 if (fs
->fs_postblformat
== FS_42POSTBLFMT
) /* XXX */
1297 dfs
->fs_nrpos
= -1; /* XXX */
1298 if (fs
->fs_inodefmt
< FS_44INODEFMT
) { /* XXX */
1299 int32_t *lp
, tmp
; /* XXX */
1301 lp
= (int32_t *)&dfs
->fs_qbmask
; /* XXX */
1302 tmp
= lp
[4]; /* XXX */
1303 for (i
= 4; i
> 0; i
--) /* XXX */
1304 lp
[i
] = lp
[i
-1]; /* XXX */
1305 lp
[0] = tmp
; /* XXX */
1307 dfs
->fs_maxfilesize
= mp
->um_savedmaxfilesize
; /* XXX */
1308 if (waitfor
!= MNT_WAIT
)
1310 else if ((error
= bwrite(bp
)) != 0)