]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ffs/ffs_vfsops.c
10c78d5fb8e31730e74558fe569eec4682602a9e
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1989, 1991, 1993, 1994
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
58 #include <rev_endian_fs.h>
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/namei.h>
63 #include <sys/kernel.h>
64 #include <sys/vnode.h>
65 #include <sys/socket.h>
66 #include <sys/mount.h>
71 #include <sys/ioctl.h>
72 #include <sys/errno.h>
73 #include <sys/malloc.h>
76 #include <miscfs/specfs/specdev.h>
78 #include <ufs/ufs/quota.h>
79 #include <ufs/ufs/ufsmount.h>
80 #include <ufs/ufs/inode.h>
81 #include <ufs/ufs/ufs_extern.h>
83 #include <ufs/ffs/fs.h>
84 #include <ufs/ffs/ffs_extern.h>
86 #include <ufs/ufs/ufs_byte_order.h>
87 #include <architecture/byte_order.h>
88 #endif /* REV_ENDIAN_FS */
90 int ffs_sbupdate
__P((struct ufsmount
*, int));
92 struct vfsops ufs_vfsops
= {
107 extern u_long nextgennumber
;
110 * Called by main() when ufs is going to be mounted as root.
114 extern struct vnode
*rootvp
;
117 struct proc
*p
= current_proc(); /* XXX */
118 struct ufsmount
*ump
;
123 * Get vnode for rootdev.
125 if (error
= bdevvp(rootdev
, &rootvp
)) {
126 printf("ffs_mountroot: can't setup bdevvp");
129 if (error
= vfs_rootmountalloc("ufs", "root_device", &mp
))
132 /* Must set the MNT_ROOTFS flag before doing the actual mount */
133 mp
->mnt_flag
|= MNT_ROOTFS
;
135 if (error
= ffs_mountfs(rootvp
, mp
, p
)) {
136 mp
->mnt_vfc
->vfc_refcount
--;
138 _FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
141 simple_lock(&mountlist_slock
);
142 CIRCLEQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
143 simple_unlock(&mountlist_slock
);
146 (void) copystr(mp
->mnt_stat
.f_mntonname
, fs
->fs_fsmnt
, MNAMELEN
- 1, 0);
147 (void)ffs_statfs(mp
, &mp
->mnt_stat
, p
);
149 inittodr(fs
->fs_time
);
159 ffs_mount(mp
, path
, data
, ndp
, p
)
160 register struct mount
*mp
;
163 struct nameidata
*ndp
;
167 struct ufs_args args
;
168 struct ufsmount
*ump
;
169 register struct fs
*fs
;
176 if (error
= copyin(data
, (caddr_t
)&args
, sizeof (struct ufs_args
)))
179 * If updating, check whether changing from read-only to
180 * read/write; if there is no device name, that's all we do.
182 if (mp
->mnt_flag
& MNT_UPDATE
) {
185 if (fs
->fs_ronly
== 0 && (mp
->mnt_flag
& MNT_RDONLY
)) {
187 if (mp
->mnt_flag
& MNT_FORCE
)
189 if (error
= ffs_flushfiles(mp
, flags
, p
))
193 if (error
= ffs_sbupdate(ump
, MNT_WAIT
)) {
199 /* save fs_ronly to later use */
200 ronly
= fs
->fs_ronly
;
201 if ((mp
->mnt_flag
& MNT_RELOAD
) || ronly
)
204 (error
= ffs_reload(mp
, ndp
->ni_cnd
.cn_cred
, p
)))
206 /* replace the ronly after load */
207 fs
->fs_ronly
= ronly
;
209 * Do not update the file system if the user was in singleuser
210 * and then tries to mount -uw without fscking
212 if (!fs
->fs_clean
&& ronly
) {
213 printf("WARNING: trying to mount a dirty file system\n");
214 if (issingleuser() && (mp
->mnt_flag
& MNT_ROOTFS
)) {
215 printf("WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",fs
->fs_fsmnt
);
217 * Reset the readonly bit as reload might have
225 if (ronly
&& (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)) {
227 * If upgrade to read-write by non-root, then verify
228 * that user has necessary permissions on the device.
230 if (p
->p_ucred
->cr_uid
!= 0) {
231 devvp
= ump
->um_devvp
;
232 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
233 if (error
= VOP_ACCESS(devvp
, VREAD
| VWRITE
,
235 VOP_UNLOCK(devvp
, 0, p
);
238 VOP_UNLOCK(devvp
, 0, p
);
242 (void) ffs_sbupdate(ump
, MNT_WAIT
);
244 if (args
.fspec
== 0) {
246 * Process export requests.
248 return (vfs_export(mp
, &ump
->um_export
, &args
.export
));
252 * Not an update, or updating the name: look up the name
253 * and verify that it refers to a sensible block device.
255 NDINIT(ndp
, LOOKUP
, FOLLOW
, UIO_USERSPACE
, args
.fspec
, p
);
256 if (error
= namei(ndp
))
260 if (devvp
->v_type
!= VBLK
) {
264 if (major(devvp
->v_rdev
) >= nblkdev
) {
269 * If mount by non-root, then verify that user has necessary
270 * permissions on the device.
272 if (p
->p_ucred
->cr_uid
!= 0) {
274 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0)
275 accessmode
|= VWRITE
;
276 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
277 if (error
= VOP_ACCESS(devvp
, accessmode
, p
->p_ucred
, p
)) {
281 VOP_UNLOCK(devvp
, 0, p
);
283 if ((mp
->mnt_flag
& MNT_UPDATE
) == 0)
284 error
= ffs_mountfs(devvp
, mp
, p
);
286 if (devvp
!= ump
->um_devvp
)
287 error
= EINVAL
; /* needs translation */
297 (void) copyinstr(path
, fs
->fs_fsmnt
, sizeof(fs
->fs_fsmnt
) - 1, &size
);
298 bzero(fs
->fs_fsmnt
+ size
, sizeof(fs
->fs_fsmnt
) - size
);
299 bcopy((caddr_t
)fs
->fs_fsmnt
, (caddr_t
)mp
->mnt_stat
.f_mntonname
,
301 (void) copyinstr(args
.fspec
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
- 1,
303 bzero(mp
->mnt_stat
.f_mntfromname
+ size
, MNAMELEN
- size
);
304 (void)ffs_statfs(mp
, &mp
->mnt_stat
, p
);
309 * Reload all incore data for a filesystem (used after running fsck on
310 * the root filesystem and finding things to fix). The filesystem must
311 * be mounted read-only.
313 * Things to do to update the mount:
314 * 1) invalidate all cached meta-data.
315 * 2) re-read superblock from disk.
316 * 3) re-read summary information from disk.
317 * 4) invalidate all inactive vnodes.
318 * 5) invalidate all cached file data.
319 * 6) re-read inode data for all active vnodes.
321 ffs_reload(mountp
, cred
, p
)
322 register struct mount
*mountp
;
326 register struct vnode
*vp
, *nvp
, *devvp
;
330 struct fs
*fs
, *newfs
;
331 int i
, blks
, size
, error
;
334 int rev_endian
= (mountp
->mnt_flag
& MNT_REVEND
);
335 #endif /* REV_ENDIAN_FS */
337 if ((mountp
->mnt_flag
& MNT_RDONLY
) == 0)
340 * Step 1: invalidate all cached meta-data.
342 devvp
= VFSTOUFS(mountp
)->um_devvp
;
343 if (vinvalbuf(devvp
, 0, cred
, p
, 0, 0))
344 panic("ffs_reload: dirty1");
346 * Step 2: re-read superblock from disk.
348 VOP_DEVBLOCKSIZE(devvp
,&size
);
350 if (error
= bread(devvp
, (ufs_daddr_t
)(SBOFF
/size
), SBSIZE
, NOCRED
,&bp
)) {
354 newfs
= (struct fs
*)bp
->b_data
;
357 byte_swap_sbin(newfs
);
359 #endif /* REV_ENDIAN_FS */
360 if (newfs
->fs_magic
!= FS_MAGIC
|| newfs
->fs_bsize
> MAXBSIZE
||
361 newfs
->fs_bsize
< sizeof(struct fs
)) {
364 byte_swap_sbout(newfs
);
365 #endif /* REV_ENDIAN_FS */
368 return (EIO
); /* XXX needs translation */
370 fs
= VFSTOUFS(mountp
)->um_fs
;
372 * Copy pointer fields back into superblock before copying in XXX
373 * new superblock. These should really be in the ufsmount. XXX
374 * Note that important parameters (eg fs_ncg) are unchanged.
376 bcopy(&fs
->fs_csp
[0], &newfs
->fs_csp
[0], sizeof(fs
->fs_csp
));
377 newfs
->fs_maxcluster
= fs
->fs_maxcluster
;
378 bcopy(newfs
, fs
, (u_int
)fs
->fs_sbsize
);
379 if (fs
->fs_sbsize
< SBSIZE
)
380 bp
->b_flags
|= B_INVAL
;
383 byte_swap_sbout(newfs
);
384 #endif /* REV_ENDIAN_FS */
386 mountp
->mnt_maxsymlinklen
= fs
->fs_maxsymlinklen
;
389 * Step 3: re-read summary information from disk.
391 blks
= howmany(fs
->fs_cssize
, fs
->fs_fsize
);
392 space
= fs
->fs_csp
[0];
393 for (i
= 0; i
< blks
; i
+= fs
->fs_frag
) {
395 if (i
+ fs
->fs_frag
> blks
)
396 size
= (blks
- i
) * fs
->fs_fsize
;
397 if (error
= bread(devvp
, fsbtodb(fs
, fs
->fs_csaddr
+ i
), size
,
405 byte_swap_ints((int *)bp
->b_data
, size
/ sizeof(int));
407 #endif /* REV_ENDIAN_FS */
408 bcopy(bp
->b_data
, fs
->fs_csp
[fragstoblks(fs
, i
)], (u_int
)size
);
412 byte_swap_ints((int *)bp
->b_data
, size
/ sizeof(int));
414 #endif /* REV_ENDIAN_FS */
418 * We no longer know anything about clusters per cylinder group.
420 if (fs
->fs_contigsumsize
> 0) {
421 lp
= fs
->fs_maxcluster
;
422 for (i
= 0; i
< fs
->fs_ncg
; i
++)
423 *lp
++ = fs
->fs_contigsumsize
;
427 simple_lock(&mntvnode_slock
);
428 for (vp
= mountp
->mnt_vnodelist
.lh_first
; vp
!= NULL
; vp
= nvp
) {
429 if (vp
->v_mount
!= mountp
) {
430 simple_unlock(&mntvnode_slock
);
433 nvp
= vp
->v_mntvnodes
.le_next
;
435 * Step 4: invalidate all inactive vnodes.
437 if (vrecycle(vp
, &mntvnode_slock
, p
))
440 * Step 5: invalidate all cached file data.
442 simple_lock(&vp
->v_interlock
);
443 simple_unlock(&mntvnode_slock
);
444 if (vget(vp
, LK_EXCLUSIVE
| LK_INTERLOCK
, p
)) {
447 if (vinvalbuf(vp
, 0, cred
, p
, 0, 0))
448 panic("ffs_reload: dirty2");
450 * Step 6: re-read inode data for all active vnodes.
454 bread(devvp
, fsbtodb(fs
, ino_to_fsba(fs
, ip
->i_number
)),
455 (int)fs
->fs_bsize
, NOCRED
, &bp
)) {
462 byte_swap_inode_in(((struct dinode
*)bp
->b_data
+
463 ino_to_fsbo(fs
, ip
->i_number
)), ip
);
465 #endif /* REV_ENDIAN_FS */
466 ip
->i_din
= *((struct dinode
*)bp
->b_data
+
467 ino_to_fsbo(fs
, ip
->i_number
));
470 #endif /* REV_ENDIAN_FS */
473 simple_lock(&mntvnode_slock
);
475 simple_unlock(&mntvnode_slock
);
480 * Common code for mount and mountroot
483 ffs_mountfs(devvp
, mp
, p
)
484 register struct vnode
*devvp
;
488 register struct ufsmount
*ump
;
490 register struct fs
*fs
;
494 int32_t clustersumoff
;
496 int error
, i
, blks
, size
, ronly
;
499 extern struct vnode
*rootvp
;
500 u_int64_t maxfilesize
; /* XXX */
501 u_int dbsize
= DEV_BSIZE
;
504 #endif /* REV_ENDIAN_FS */
506 cred
= p
? p
->p_ucred
: NOCRED
;
508 * Disallow multiple mounts of the same device.
509 * Disallow mounting of a device that is currently in use
510 * (except for root, which might share swap device for miniroot).
511 * Flush out any old buffers remaining from a previous use.
513 if (error
= vfs_mountedon(devvp
))
515 if (vcount(devvp
) > 1 && devvp
!= rootvp
)
517 if (error
= vinvalbuf(devvp
, V_SAVE
, cred
, p
, 0, 0))
520 ronly
= (mp
->mnt_flag
& MNT_RDONLY
) != 0;
521 if (error
= VOP_OPEN(devvp
, ronly
? FREAD
: FREAD
|FWRITE
, FSCRED
, p
))
524 VOP_DEVBLOCKSIZE(devvp
,&size
);
528 if (error
= bread(devvp
, (ufs_daddr_t
)(SBOFF
/size
), SBSIZE
, cred
, &bp
))
530 fs
= (struct fs
*)bp
->b_data
;
532 if (fs
->fs_magic
!= FS_MAGIC
|| fs
->fs_bsize
> MAXBSIZE
||
533 fs
->fs_bsize
< sizeof(struct fs
)) {
534 int magic
= fs
->fs_magic
;
536 byte_swap_ints(&magic
, 1);
537 if (magic
!= FS_MAGIC
) {
542 if (fs
->fs_magic
!= FS_MAGIC
|| fs
->fs_bsize
> MAXBSIZE
||
543 fs
->fs_bsize
< sizeof(struct fs
)) {
545 error
= EINVAL
; /* XXX needs translation */
550 #endif /* REV_ENDIAN_FS */
551 if (fs
->fs_magic
!= FS_MAGIC
|| fs
->fs_bsize
> MAXBSIZE
||
552 fs
->fs_bsize
< sizeof(struct fs
)) {
556 #endif /* REV_ENDIAN_FS */
557 error
= EINVAL
; /* XXX needs translation */
563 * Buffer cache does not handle multiple pages in a buf when
564 * invalidating incore buffer in pageout. There are no locks
565 * in the pageout path. So there is a danger of loosing data when
566 * block allocation happens at the same time a pageout of buddy
567 * page occurs. incore() returns buf with both
568 * pages, this leads vnode-pageout to incorrectly flush of entire.
569 * buf. Till the low level ffs code is modified to deal with these
570 * do not mount any FS more than 4K size.
573 * Can't mount filesystems with a fragment size less than DIRBLKSIZ
576 * Don't mount dirty filesystems, except for the root filesystem
578 if ((fs
->fs_bsize
> PAGE_SIZE
) || (fs
->fs_fsize
< DIRBLKSIZ
) ||
579 ((!(mp
->mnt_flag
& MNT_ROOTFS
)) && (!fs
->fs_clean
))) {
583 #endif /* REV_ENDIAN_FS */
588 /* Let's figure out the devblock size the file system is with */
589 /* the device block size = fragment size / number of sectors per frag */
591 dbsize
= fs
->fs_fsize
/ NSPF(fs
);
593 kprintf("device blocksize computaion failed\n");
595 if (VOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, &dbsize
, FWRITE
, NOCRED
,
597 kprintf("failed to set device blocksize\n");
599 /* force the specfs to reread blocksize from size() */
600 set_fsblocksize(devvp
);
604 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
605 if (fs
->fs_postblformat
== FS_42POSTBLFMT
&& !ronly
) {
609 #endif /* REV_ENDIAN_FS */
610 error
= EROFS
; /* needs translation */
614 /* If we are not mounting read only, then check for overlap
615 * condition in cylinder group's free block map.
616 * If overlap exists, then force this into a read only mount
617 * to avoid further corruption. PR#2216969
620 if (error
= bread (devvp
, fsbtodb(fs
, cgtod(fs
, 0)),
621 (int)fs
->fs_cgsize
, NOCRED
, &cgbp
)) {
625 cgp
= (struct cg
*)cgbp
->b_data
;
628 byte_swap_cgin(cgp
,fs
);
629 #endif /* REV_ENDIAN_FS */
630 if (!cg_chkmagic(cgp
)){
633 byte_swap_cgout(cgp
,fs
);
634 #endif /* REV_ENDIAN_FS */
638 if (cgp
->cg_clustersumoff
!= 0) {
639 /* Check for overlap */
640 clustersumoff
= cgp
->cg_freeoff
+
641 howmany(fs
->fs_cpg
* fs
->fs_spc
/ NSPF(fs
), NBBY
);
642 clustersumoff
= roundup(clustersumoff
, sizeof(long));
643 if (cgp
->cg_clustersumoff
< clustersumoff
) {
645 mp
->mnt_flag
|= MNT_RDONLY
;
651 byte_swap_cgout(cgp
,fs
);
652 #endif /* REV_ENDIAN_FS */
656 ump
= _MALLOC(sizeof *ump
, M_UFSMNT
, M_WAITOK
);
657 bzero((caddr_t
)ump
, sizeof *ump
);
658 ump
->um_fs
= _MALLOC((u_long
)fs
->fs_sbsize
, M_UFSMNT
,
660 bcopy(bp
->b_data
, ump
->um_fs
, (u_int
)fs
->fs_sbsize
);
661 if (fs
->fs_sbsize
< SBSIZE
)
662 bp
->b_flags
|= B_INVAL
;
666 #endif /* REV_ENDIAN_FS */
670 fs
->fs_ronly
= ronly
;
671 size
= fs
->fs_cssize
;
672 blks
= howmany(size
, fs
->fs_fsize
);
673 if (fs
->fs_contigsumsize
> 0)
674 size
+= fs
->fs_ncg
* sizeof(int32_t);
675 base
= space
= _MALLOC((u_long
)size
, M_UFSMNT
, M_WAITOK
);
677 for (i
= 0; i
< blks
; i
+= fs
->fs_frag
) {
679 if (i
+ fs
->fs_frag
> blks
)
680 size
= (blks
- i
) * fs
->fs_fsize
;
681 if (error
= bread(devvp
, fsbtodb(fs
, fs
->fs_csaddr
+ i
), size
,
683 _FREE(base
, M_UFSMNT
);
686 bcopy(bp
->b_data
, space
, (u_int
)size
);
689 byte_swap_ints((int *) space
, size
/ sizeof(int));
690 #endif /* REV_ENDIAN_FS */
691 fs
->fs_csp
[fragstoblks(fs
, i
)] = (struct csum
*)space
;
696 if (fs
->fs_contigsumsize
> 0) {
697 fs
->fs_maxcluster
= lp
= (int32_t *)space
;
698 for (i
= 0; i
< fs
->fs_ncg
; i
++)
699 *lp
++ = fs
->fs_contigsumsize
;
701 mp
->mnt_data
= (qaddr_t
)ump
;
702 mp
->mnt_stat
.f_fsid
.val
[0] = (long)dev
;
703 mp
->mnt_stat
.f_fsid
.val
[1] = mp
->mnt_vfc
->vfc_typenum
;
704 #warning hardcoded max symlen and not "mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;"
705 mp
->mnt_maxsymlinklen
= 60;
708 mp
->mnt_flag
|= MNT_REVEND
;
709 #endif /* REV_ENDIAN_FS */
712 ump
->um_devvp
= devvp
;
713 ump
->um_nindir
= fs
->fs_nindir
;
714 ump
->um_bptrtodb
= fs
->fs_fsbtodb
;
715 ump
->um_seqinc
= fs
->fs_frag
;
716 for (i
= 0; i
< MAXQUOTAS
; i
++)
717 ump
->um_quotas
[i
] = NULLVP
;
718 devvp
->v_specflags
|= SI_MOUNTEDON
;
720 ump
->um_savedmaxfilesize
= fs
->fs_maxfilesize
; /* XXX */
721 maxfilesize
= (u_int64_t
)0x100000000; /* 4GB */
723 maxfilesize
= (u_int64_t
)0x40000000 * fs
->fs_bsize
- 1; /* XXX */
725 if (fs
->fs_maxfilesize
> maxfilesize
) /* XXX */
726 fs
->fs_maxfilesize
= maxfilesize
; /* XXX */
729 (void) ffs_sbupdate(ump
, MNT_WAIT
);
735 (void)VOP_CLOSE(devvp
, ronly
? FREAD
: FREAD
|FWRITE
, cred
, p
);
737 _FREE(ump
->um_fs
, M_UFSMNT
);
738 _FREE(ump
, M_UFSMNT
);
739 mp
->mnt_data
= (qaddr_t
)0;
745 * Sanity checks for old file systems.
747 * XXX - goes away some day.
754 fs
->fs_npsect
= max(fs
->fs_npsect
, fs
->fs_nsect
); /* XXX */
755 fs
->fs_interleave
= max(fs
->fs_interleave
, 1); /* XXX */
756 if (fs
->fs_postblformat
== FS_42POSTBLFMT
) /* XXX */
757 fs
->fs_nrpos
= 8; /* XXX */
758 if (fs
->fs_inodefmt
< FS_44INODEFMT
) { /* XXX */
759 u_int64_t sizepb
= fs
->fs_bsize
; /* XXX */
761 fs
->fs_maxfilesize
= fs
->fs_bsize
* NDADDR
- 1; /* XXX */
762 for (i
= 0; i
< NIADDR
; i
++) { /* XXX */
763 sizepb
*= NINDIR(fs
); /* XXX */
764 fs
->fs_maxfilesize
+= sizepb
; /* XXX */
766 fs
->fs_qbmask
= ~fs
->fs_bmask
; /* XXX */
767 fs
->fs_qfmask
= ~fs
->fs_fmask
; /* XXX */
773 * unmount system call
776 ffs_unmount(mp
, mntflags
, p
)
781 register struct ufsmount
*ump
;
782 register struct fs
*fs
;
785 if (mntflags
& MNT_FORCE
)
787 if (error
= ffs_flushfiles(mp
, flags
, p
))
791 if (fs
->fs_ronly
== 0) {
793 if (error
= ffs_sbupdate(ump
, MNT_WAIT
)) {
796 /* we can atleast cleanup ; as the media could be WP */
797 /* & during mount, we do not check for write failures */
798 /* FIXME LATER : the Correct fix would be to have */
799 /* mount detect the WP media and downgrade to readonly mount */
800 /* For now, here it is */
805 ump
->um_devvp
->v_specflags
&= ~SI_MOUNTEDON
;
806 error
= VOP_CLOSE(ump
->um_devvp
, fs
->fs_ronly
? FREAD
: FREAD
|FWRITE
,
808 vrele(ump
->um_devvp
);
810 _FREE(fs
->fs_csp
[0], M_UFSMNT
);
812 _FREE(ump
, M_UFSMNT
);
813 mp
->mnt_data
= (qaddr_t
)0;
815 mp
->mnt_flag
&= ~MNT_REVEND
;
816 #endif /* REV_ENDIAN_FS */
821 * Flush out all the files in a filesystem.
823 ffs_flushfiles(mp
, flags
, p
)
824 register struct mount
*mp
;
828 register struct ufsmount
*ump
;
833 if (mp
->mnt_flag
& MNT_QUOTA
) {
834 if (error
= vflush(mp
, NULLVP
, SKIPSYSTEM
|flags
))
836 for (i
= 0; i
< MAXQUOTAS
; i
++) {
837 if (ump
->um_quotas
[i
] == NULLVP
)
842 * Here we fall through to vflush again to ensure
843 * that we have gotten rid of all the system vnodes.
847 error
= vflush(mp
, NULLVP
, SKIPSWAP
|flags
);
848 error
= vflush(mp
, NULLVP
, flags
);
853 * Get file system statistics.
856 ffs_statfs(mp
, sbp
, p
)
858 register struct statfs
*sbp
;
861 register struct ufsmount
*ump
;
862 register struct fs
*fs
;
866 if (fs
->fs_magic
!= FS_MAGIC
)
868 sbp
->f_bsize
= fs
->fs_fsize
;
869 sbp
->f_iosize
= fs
->fs_bsize
;
870 sbp
->f_blocks
= fs
->fs_dsize
;
871 sbp
->f_bfree
= fs
->fs_cstotal
.cs_nbfree
* fs
->fs_frag
+
872 fs
->fs_cstotal
.cs_nffree
;
873 sbp
->f_bavail
= freespace(fs
, fs
->fs_minfree
);
874 sbp
->f_files
= fs
->fs_ncg
* fs
->fs_ipg
- ROOTINO
;
875 sbp
->f_ffree
= fs
->fs_cstotal
.cs_nifree
;
876 if (sbp
!= &mp
->mnt_stat
) {
877 sbp
->f_type
= mp
->mnt_vfc
->vfc_typenum
;
878 bcopy((caddr_t
)mp
->mnt_stat
.f_mntonname
,
879 (caddr_t
)&sbp
->f_mntonname
[0], MNAMELEN
);
880 bcopy((caddr_t
)mp
->mnt_stat
.f_mntfromname
,
881 (caddr_t
)&sbp
->f_mntfromname
[0], MNAMELEN
);
887 * Go through the disk queues to initiate sandbagged IO;
888 * go through the inodes to write those that have been modified;
889 * initiate the writing of the super block if it has been modified.
891 * Note: we are always called with the filesystem marked `MPBUSY'.
894 ffs_sync(mp
, waitfor
, cred
, p
)
900 struct vnode
*nvp
, *vp
;
902 struct ufsmount
*ump
= VFSTOUFS(mp
);
904 int error
, allerror
= 0;
907 if (fs
->fs_fmod
!= 0 && fs
->fs_ronly
!= 0) { /* XXX */
908 printf("fs = %s\n", fs
->fs_fsmnt
);
909 panic("update: rofs mod");
912 * Write back each (modified) inode.
914 simple_lock(&mntvnode_slock
);
916 for (vp
= mp
->mnt_vnodelist
.lh_first
;
920 * If the vnode that we are about to sync is no longer
921 * associated with this mount point, start over.
923 if (vp
->v_mount
!= mp
)
925 simple_lock(&vp
->v_interlock
);
926 nvp
= vp
->v_mntvnodes
.le_next
;
928 if ((vp
->v_type
== VNON
) ||
929 ((ip
->i_flag
& (IN_ACCESS
| IN_CHANGE
| IN_MODIFIED
| IN_UPDATE
)) == 0 &&
930 vp
->v_dirtyblkhd
.lh_first
== NULL
&& !(vp
->v_flag
& VHASDIRTY
))) {
931 simple_unlock(&vp
->v_interlock
);
934 simple_unlock(&mntvnode_slock
);
935 error
= vget(vp
, LK_EXCLUSIVE
| LK_NOWAIT
| LK_INTERLOCK
, p
);
937 simple_lock(&mntvnode_slock
);
942 if (error
= VOP_FSYNC(vp
, cred
, waitfor
, p
))
944 VOP_UNLOCK(vp
, 0, p
);
946 simple_lock(&mntvnode_slock
);
948 simple_unlock(&mntvnode_slock
);
950 * Force stale file system control information to be flushed.
952 if (error
= VOP_FSYNC(ump
->um_devvp
, cred
, waitfor
, p
))
958 * Write back modified superblock.
960 if (fs
->fs_fmod
!= 0) {
962 fs
->fs_time
= time
.tv_sec
;
963 if (error
= ffs_sbupdate(ump
, waitfor
))
970 * Look up a FFS dinode number to find its incore vnode, otherwise read it
971 * in from disk. If it is in core, wait for the lock bit to clear, then
972 * return the inode locked. Detection and handling of mount points must be
973 * done by the calling routine.
976 ffs_vget(mp
, ino
, vpp
)
981 struct proc
*p
= current_proc(); /* XXX */
984 struct ufsmount
*ump
;
993 /* Check for unmount in progress */
994 if (mp
->mnt_kern_flag
& MNTK_UNMOUNT
) {
999 if ((*vpp
= ufs_ihashget(dev
, ino
)) != NULL
) {
1001 UBCINFOCHECK("ffs_vget", vp
);
1004 /* Allocate a new vnode/inode. */
1005 type
= ump
->um_devvp
->v_tag
== VT_MFS
? M_MFSNODE
: M_FFSNODE
; /* XXX */
1006 MALLOC_ZONE(ip
, struct inode
*, sizeof(struct inode
), type
, M_WAITOK
);
1007 if (error
= getnewvnode(VT_UFS
, mp
, ffs_vnodeop_p
, &vp
)) {
1008 FREE_ZONE(ip
, sizeof(struct inode
), type
);
1012 bzero((caddr_t
)ip
, sizeof(struct inode
));
1013 lockinit(&ip
->i_lock
, PINOD
, "inode", 0, 0);
1016 ip
->i_fs
= fs
= ump
->um_fs
;
1020 for (i
= 0; i
< MAXQUOTAS
; i
++)
1021 ip
->i_dquot
[i
] = NODQUOT
;
1024 * Put it onto its hash chain and lock it so that other requests for
1025 * this inode will block if they arrive while we are sleeping waiting
1026 * for old data structures to be purged or for the contents of the
1027 * disk portion of this inode to be read.
1031 /* Read in the disk contents for the inode, copy into the inode. */
1032 if (error
= bread(ump
->um_devvp
, fsbtodb(fs
, ino_to_fsba(fs
, ino
)),
1033 (int)fs
->fs_bsize
, NOCRED
, &bp
)) {
1035 * The inode does not contain anything useful, so it would
1036 * be misleading to leave it on its hash chain. With mode
1037 * still zero, it will be unlinked and returned to the free
1046 if (mp
->mnt_flag
& MNT_REVEND
) {
1047 byte_swap_inode_in(((struct dinode
*)bp
->b_data
+ ino_to_fsbo(fs
, ino
)),ip
);
1049 #endif /* REV_ENDIAN_FS */
1050 ip
->i_din
= *((struct dinode
*)bp
->b_data
+ ino_to_fsbo(fs
, ino
));
1053 #endif /* REV_ENDIAN_FS */
1057 * Initialize the vnode from the inode, check for aliases.
1058 * Note that the underlying vnode may have changed.
1060 if (error
= ufs_vinit(mp
, ffs_specop_p
, FFS_FIFOOPS
, &vp
)) {
1066 * Finish inode initialization now that aliasing has been resolved.
1068 ip
->i_devvp
= ump
->um_devvp
;
1071 * Set up a generation number for this inode if it does not
1072 * already have one. This should only happen on old filesystems.
1074 if (ip
->i_gen
== 0) {
1075 if (++nextgennumber
< (u_long
)time
.tv_sec
)
1076 nextgennumber
= time
.tv_sec
;
1077 ip
->i_gen
= nextgennumber
;
1078 if ((vp
->v_mount
->mnt_flag
& MNT_RDONLY
) == 0)
1079 ip
->i_flag
|= IN_MODIFIED
;
1082 * Ensure that uid and gid are correct. This is a temporary
1083 * fix until fsck has been changed to do the update.
1085 if (fs
->fs_inodefmt
< FS_44INODEFMT
) { /* XXX */
1086 ip
->i_uid
= ip
->i_din
.di_ouid
; /* XXX */
1087 ip
->i_gid
= ip
->i_din
.di_ogid
; /* XXX */
1091 if (UBCINFOMISSING(vp
) || UBCINFORECLAIMED(vp
))
1097 * File handle to vnode
1099 * Have to be really careful about stale file handles:
1100 * - check that the inode number is valid
1101 * - call ffs_vget() to get the locked inode
1102 * - check for an unallocated inode (i_mode == 0)
1103 * - check that the given client host has export rights and return
1104 * those rights via. exflagsp and credanonp
1107 ffs_fhtovp(mp
, fhp
, nam
, vpp
, exflagsp
, credanonp
)
1108 register struct mount
*mp
;
1113 struct ucred
**credanonp
;
1115 register struct ufid
*ufhp
;
1118 ufhp
= (struct ufid
*)fhp
;
1119 fs
= VFSTOUFS(mp
)->um_fs
;
1120 if (ufhp
->ufid_ino
< ROOTINO
||
1121 ufhp
->ufid_ino
>= fs
->fs_ncg
* fs
->fs_ipg
)
1123 return (ufs_check_export(mp
, ufhp
, nam
, vpp
, exflagsp
, credanonp
));
1127 * Vnode pointer to File handle
1134 register struct inode
*ip
;
1135 register struct ufid
*ufhp
;
1138 ufhp
= (struct ufid
*)fhp
;
1139 ufhp
->ufid_len
= sizeof(struct ufid
);
1140 ufhp
->ufid_ino
= ip
->i_number
;
1141 ufhp
->ufid_gen
= ip
->i_gen
;
1146 * Initialize the filesystem; just use ufs_init.
1150 struct vfsconf
*vfsp
;
1153 return (ufs_init(vfsp
));
1157 * fast filesystem related variables.
1159 ffs_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
, p
)
1168 extern int doclusterread
, doclusterwrite
, doreallocblks
, doasyncfree
;
1170 /* all sysctl names at this level are terminal */
1172 return (ENOTDIR
); /* overloaded */
1175 case FFS_CLUSTERREAD
:
1176 return (sysctl_int(oldp
, oldlenp
, newp
, newlen
,
1178 case FFS_CLUSTERWRITE
:
1179 return (sysctl_int(oldp
, oldlenp
, newp
, newlen
,
1181 case FFS_REALLOCBLKS
:
1182 return (sysctl_int(oldp
, oldlenp
, newp
, newlen
,
1185 return (sysctl_int(oldp
, oldlenp
, newp
, newlen
, &doasyncfree
));
1187 return (EOPNOTSUPP
);
1193 * Write a superblock and associated information back to disk.
1196 ffs_sbupdate(mp
, waitfor
)
1197 struct ufsmount
*mp
;
1200 register struct fs
*dfs
, *fs
= mp
->um_fs
;
1201 register struct buf
*bp
;
1204 int i
, size
, error
, allerror
= 0;
1207 int rev_endian
=(mp
->um_mountp
->mnt_flag
& MNT_REVEND
);
1208 #endif /* REV_ENDIAN_FS */
1211 * First write back the summary information.
1213 blks
= howmany(fs
->fs_cssize
, fs
->fs_fsize
);
1214 space
= (caddr_t
)fs
->fs_csp
[0];
1215 for (i
= 0; i
< blks
; i
+= fs
->fs_frag
) {
1216 size
= fs
->fs_bsize
;
1217 if (i
+ fs
->fs_frag
> blks
)
1218 size
= (blks
- i
) * fs
->fs_fsize
;
1219 bp
= getblk(mp
->um_devvp
, fsbtodb(fs
, fs
->fs_csaddr
+ i
),
1220 size
, 0, 0, BLK_META
);
1221 bcopy(space
, bp
->b_data
, (u_int
)size
);
1224 byte_swap_ints((int *)bp
->b_data
, size
/ sizeof(int));
1226 #endif /* REV_ENDIAN_FS */
1228 if (waitfor
!= MNT_WAIT
)
1230 else if (error
= bwrite(bp
))
1234 * Now write back the superblock itself. If any errors occurred
1235 * up to this point, then fail so that the superblock avoids
1236 * being written out as clean.
1240 VOP_DEVBLOCKSIZE(mp
->um_devvp
,&devBlockSize
);
1241 bp
= getblk(mp
->um_devvp
, (SBOFF
/devBlockSize
), (int)fs
->fs_sbsize
, 0, 0, BLK_META
);
1242 bcopy((caddr_t
)fs
, bp
->b_data
, (u_int
)fs
->fs_sbsize
);
1243 /* Restore compatibility to old file systems. XXX */
1244 dfs
= (struct fs
*)bp
->b_data
; /* XXX */
1245 if (fs
->fs_postblformat
== FS_42POSTBLFMT
) /* XXX */
1246 dfs
->fs_nrpos
= -1; /* XXX */
1249 * Swapping bytes here ; so that in case
1250 * of inode format < FS_44INODEFMT appropriate
1254 byte_swap_sbout((struct fs
*)bp
->b_data
);
1256 #endif /* REV_ENDIAN_FS */
1257 if (fs
->fs_inodefmt
< FS_44INODEFMT
) { /* XXX */
1258 int32_t *lp
, tmp
; /* XXX */
1260 lp
= (int32_t *)&dfs
->fs_qbmask
; /* XXX */
1261 tmp
= lp
[4]; /* XXX */
1262 for (i
= 4; i
> 0; i
--) /* XXX */
1263 lp
[i
] = lp
[i
-1]; /* XXX */
1264 lp
[0] = tmp
; /* XXX */
1267 /* Note that dfs is already swapped so swap the filesize
1271 dfs
->fs_maxfilesize
= NXSwapLongLong(mp
->um_savedmaxfilesize
); /* XXX */
1273 #endif /* REV_ENDIAN_FS */
1274 dfs
->fs_maxfilesize
= mp
->um_savedmaxfilesize
; /* XXX */
1277 #endif /* REV_ENDIAN_FS */
1278 if (waitfor
!= MNT_WAIT
)
1280 else if (error
= bwrite(bp
))