]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ffs/ffs_vfsops.c
7da3b2a3e856a6a4d36a4cbaf1a5244d3c4146c8
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
;
332 u_int64_t maxfilesize
; /* XXX */
335 int rev_endian
= (mountp
->mnt_flag
& MNT_REVEND
);
336 #endif /* REV_ENDIAN_FS */
338 if ((mountp
->mnt_flag
& MNT_RDONLY
) == 0)
341 * Step 1: invalidate all cached meta-data.
343 devvp
= VFSTOUFS(mountp
)->um_devvp
;
344 if (vinvalbuf(devvp
, 0, cred
, p
, 0, 0))
345 panic("ffs_reload: dirty1");
347 * Step 2: re-read superblock from disk.
349 VOP_DEVBLOCKSIZE(devvp
,&size
);
351 if (error
= bread(devvp
, (ufs_daddr_t
)(SBOFF
/size
), SBSIZE
, NOCRED
,&bp
)) {
355 newfs
= (struct fs
*)bp
->b_data
;
358 byte_swap_sbin(newfs
);
360 #endif /* REV_ENDIAN_FS */
361 if (newfs
->fs_magic
!= FS_MAGIC
|| newfs
->fs_bsize
> MAXBSIZE
||
362 newfs
->fs_bsize
< sizeof(struct fs
)) {
365 byte_swap_sbout(newfs
);
366 #endif /* REV_ENDIAN_FS */
369 return (EIO
); /* XXX needs translation */
371 fs
= VFSTOUFS(mountp
)->um_fs
;
373 * Copy pointer fields back into superblock before copying in XXX
374 * new superblock. These should really be in the ufsmount. XXX
375 * Note that important parameters (eg fs_ncg) are unchanged.
377 bcopy(&fs
->fs_csp
[0], &newfs
->fs_csp
[0], sizeof(fs
->fs_csp
));
378 newfs
->fs_maxcluster
= fs
->fs_maxcluster
;
379 bcopy(newfs
, fs
, (u_int
)fs
->fs_sbsize
);
380 if (fs
->fs_sbsize
< SBSIZE
)
381 bp
->b_flags
|= B_INVAL
;
384 byte_swap_sbout(newfs
);
385 #endif /* REV_ENDIAN_FS */
387 mountp
->mnt_maxsymlinklen
= fs
->fs_maxsymlinklen
;
389 maxfilesize
= (u_int64_t
)0x100000000; /* 4GB */
390 if (fs
->fs_maxfilesize
> maxfilesize
) /* XXX */
391 fs
->fs_maxfilesize
= maxfilesize
; /* XXX */
393 * Step 3: re-read summary information from disk.
395 blks
= howmany(fs
->fs_cssize
, fs
->fs_fsize
);
396 space
= fs
->fs_csp
[0];
397 for (i
= 0; i
< blks
; i
+= fs
->fs_frag
) {
399 if (i
+ fs
->fs_frag
> blks
)
400 size
= (blks
- i
) * fs
->fs_fsize
;
401 if (error
= bread(devvp
, fsbtodb(fs
, fs
->fs_csaddr
+ i
), size
,
409 byte_swap_ints((int *)bp
->b_data
, size
/ sizeof(int));
411 #endif /* REV_ENDIAN_FS */
412 bcopy(bp
->b_data
, fs
->fs_csp
[fragstoblks(fs
, i
)], (u_int
)size
);
416 byte_swap_ints((int *)bp
->b_data
, size
/ sizeof(int));
418 #endif /* REV_ENDIAN_FS */
422 * We no longer know anything about clusters per cylinder group.
424 if (fs
->fs_contigsumsize
> 0) {
425 lp
= fs
->fs_maxcluster
;
426 for (i
= 0; i
< fs
->fs_ncg
; i
++)
427 *lp
++ = fs
->fs_contigsumsize
;
431 simple_lock(&mntvnode_slock
);
432 for (vp
= mountp
->mnt_vnodelist
.lh_first
; vp
!= NULL
; vp
= nvp
) {
433 if (vp
->v_mount
!= mountp
) {
434 simple_unlock(&mntvnode_slock
);
437 nvp
= vp
->v_mntvnodes
.le_next
;
439 * Step 4: invalidate all inactive vnodes.
441 if (vrecycle(vp
, &mntvnode_slock
, p
))
444 * Step 5: invalidate all cached file data.
446 simple_lock(&vp
->v_interlock
);
447 simple_unlock(&mntvnode_slock
);
448 if (vget(vp
, LK_EXCLUSIVE
| LK_INTERLOCK
, p
)) {
451 if (vinvalbuf(vp
, 0, cred
, p
, 0, 0))
452 panic("ffs_reload: dirty2");
454 * Step 6: re-read inode data for all active vnodes.
458 bread(devvp
, fsbtodb(fs
, ino_to_fsba(fs
, ip
->i_number
)),
459 (int)fs
->fs_bsize
, NOCRED
, &bp
)) {
466 byte_swap_inode_in(((struct dinode
*)bp
->b_data
+
467 ino_to_fsbo(fs
, ip
->i_number
)), ip
);
469 #endif /* REV_ENDIAN_FS */
470 ip
->i_din
= *((struct dinode
*)bp
->b_data
+
471 ino_to_fsbo(fs
, ip
->i_number
));
474 #endif /* REV_ENDIAN_FS */
477 simple_lock(&mntvnode_slock
);
479 simple_unlock(&mntvnode_slock
);
484 * Common code for mount and mountroot
487 ffs_mountfs(devvp
, mp
, p
)
488 register struct vnode
*devvp
;
492 register struct ufsmount
*ump
;
494 register struct fs
*fs
;
498 int32_t clustersumoff
;
500 int error
, i
, blks
, size
, ronly
;
503 extern struct vnode
*rootvp
;
504 u_int64_t maxfilesize
; /* XXX */
505 u_int dbsize
= DEV_BSIZE
;
508 #endif /* REV_ENDIAN_FS */
510 cred
= p
? p
->p_ucred
: NOCRED
;
512 * Disallow multiple mounts of the same device.
513 * Disallow mounting of a device that is currently in use
514 * (except for root, which might share swap device for miniroot).
515 * Flush out any old buffers remaining from a previous use.
517 if (error
= vfs_mountedon(devvp
))
519 if (vcount(devvp
) > 1 && devvp
!= rootvp
)
521 if (error
= vinvalbuf(devvp
, V_SAVE
, cred
, p
, 0, 0))
524 ronly
= (mp
->mnt_flag
& MNT_RDONLY
) != 0;
525 if (error
= VOP_OPEN(devvp
, ronly
? FREAD
: FREAD
|FWRITE
, FSCRED
, p
))
528 VOP_DEVBLOCKSIZE(devvp
,&size
);
532 if (error
= bread(devvp
, (ufs_daddr_t
)(SBOFF
/size
), SBSIZE
, cred
, &bp
))
534 fs
= (struct fs
*)bp
->b_data
;
536 if (fs
->fs_magic
!= FS_MAGIC
|| fs
->fs_bsize
> MAXBSIZE
||
537 fs
->fs_bsize
< sizeof(struct fs
)) {
538 int magic
= fs
->fs_magic
;
540 byte_swap_ints(&magic
, 1);
541 if (magic
!= FS_MAGIC
) {
546 if (fs
->fs_magic
!= FS_MAGIC
|| fs
->fs_bsize
> MAXBSIZE
||
547 fs
->fs_bsize
< sizeof(struct fs
)) {
549 error
= EINVAL
; /* XXX needs translation */
554 #endif /* REV_ENDIAN_FS */
555 if (fs
->fs_magic
!= FS_MAGIC
|| fs
->fs_bsize
> MAXBSIZE
||
556 fs
->fs_bsize
< sizeof(struct fs
)) {
560 #endif /* REV_ENDIAN_FS */
561 error
= EINVAL
; /* XXX needs translation */
567 * Buffer cache does not handle multiple pages in a buf when
568 * invalidating incore buffer in pageout. There are no locks
569 * in the pageout path. So there is a danger of loosing data when
570 * block allocation happens at the same time a pageout of buddy
571 * page occurs. incore() returns buf with both
572 * pages, this leads vnode-pageout to incorrectly flush of entire.
573 * buf. Till the low level ffs code is modified to deal with these
574 * do not mount any FS more than 4K size.
577 * Can't mount filesystems with a fragment size less than DIRBLKSIZ
580 * Don't mount dirty filesystems, except for the root filesystem
582 if ((fs
->fs_bsize
> PAGE_SIZE
) || (fs
->fs_fsize
< DIRBLKSIZ
) ||
583 ((!(mp
->mnt_flag
& MNT_ROOTFS
)) && (!fs
->fs_clean
))) {
587 #endif /* REV_ENDIAN_FS */
592 /* Let's figure out the devblock size the file system is with */
593 /* the device block size = fragment size / number of sectors per frag */
595 dbsize
= fs
->fs_fsize
/ NSPF(fs
);
597 kprintf("device blocksize computaion failed\n");
599 if (VOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, &dbsize
, FWRITE
, NOCRED
,
601 kprintf("failed to set device blocksize\n");
603 /* force the specfs to reread blocksize from size() */
604 set_fsblocksize(devvp
);
607 /* cache the IO attributes */
608 error
= vfs_init_io_attributes(devvp
, mp
);
610 printf("ffs_mountfs: vfs_init_io_attributes returned %d\n",
615 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
616 if (fs
->fs_postblformat
== FS_42POSTBLFMT
&& !ronly
) {
620 #endif /* REV_ENDIAN_FS */
621 error
= EROFS
; /* needs translation */
625 /* If we are not mounting read only, then check for overlap
626 * condition in cylinder group's free block map.
627 * If overlap exists, then force this into a read only mount
628 * to avoid further corruption. PR#2216969
631 if (error
= bread (devvp
, fsbtodb(fs
, cgtod(fs
, 0)),
632 (int)fs
->fs_cgsize
, NOCRED
, &cgbp
)) {
636 cgp
= (struct cg
*)cgbp
->b_data
;
639 byte_swap_cgin(cgp
,fs
);
640 #endif /* REV_ENDIAN_FS */
641 if (!cg_chkmagic(cgp
)){
644 byte_swap_cgout(cgp
,fs
);
645 #endif /* REV_ENDIAN_FS */
649 if (cgp
->cg_clustersumoff
!= 0) {
650 /* Check for overlap */
651 clustersumoff
= cgp
->cg_freeoff
+
652 howmany(fs
->fs_cpg
* fs
->fs_spc
/ NSPF(fs
), NBBY
);
653 clustersumoff
= roundup(clustersumoff
, sizeof(long));
654 if (cgp
->cg_clustersumoff
< clustersumoff
) {
656 mp
->mnt_flag
|= MNT_RDONLY
;
662 byte_swap_cgout(cgp
,fs
);
663 #endif /* REV_ENDIAN_FS */
667 ump
= _MALLOC(sizeof *ump
, M_UFSMNT
, M_WAITOK
);
668 bzero((caddr_t
)ump
, sizeof *ump
);
669 ump
->um_fs
= _MALLOC((u_long
)fs
->fs_sbsize
, M_UFSMNT
,
671 bcopy(bp
->b_data
, ump
->um_fs
, (u_int
)fs
->fs_sbsize
);
672 if (fs
->fs_sbsize
< SBSIZE
)
673 bp
->b_flags
|= B_INVAL
;
677 #endif /* REV_ENDIAN_FS */
681 fs
->fs_ronly
= ronly
;
682 size
= fs
->fs_cssize
;
683 blks
= howmany(size
, fs
->fs_fsize
);
684 if (fs
->fs_contigsumsize
> 0)
685 size
+= fs
->fs_ncg
* sizeof(int32_t);
686 base
= space
= _MALLOC((u_long
)size
, M_UFSMNT
, M_WAITOK
);
688 for (i
= 0; i
< blks
; i
+= fs
->fs_frag
) {
690 if (i
+ fs
->fs_frag
> blks
)
691 size
= (blks
- i
) * fs
->fs_fsize
;
692 if (error
= bread(devvp
, fsbtodb(fs
, fs
->fs_csaddr
+ i
), size
,
694 _FREE(base
, M_UFSMNT
);
697 bcopy(bp
->b_data
, space
, (u_int
)size
);
700 byte_swap_ints((int *) space
, size
/ sizeof(int));
701 #endif /* REV_ENDIAN_FS */
702 fs
->fs_csp
[fragstoblks(fs
, i
)] = (struct csum
*)space
;
707 if (fs
->fs_contigsumsize
> 0) {
708 fs
->fs_maxcluster
= lp
= (int32_t *)space
;
709 for (i
= 0; i
< fs
->fs_ncg
; i
++)
710 *lp
++ = fs
->fs_contigsumsize
;
712 mp
->mnt_data
= (qaddr_t
)ump
;
713 mp
->mnt_stat
.f_fsid
.val
[0] = (long)dev
;
714 mp
->mnt_stat
.f_fsid
.val
[1] = mp
->mnt_vfc
->vfc_typenum
;
715 #warning hardcoded max symlen and not "mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;"
716 mp
->mnt_maxsymlinklen
= 60;
719 mp
->mnt_flag
|= MNT_REVEND
;
720 #endif /* REV_ENDIAN_FS */
723 ump
->um_devvp
= devvp
;
724 ump
->um_nindir
= fs
->fs_nindir
;
725 ump
->um_bptrtodb
= fs
->fs_fsbtodb
;
726 ump
->um_seqinc
= fs
->fs_frag
;
727 for (i
= 0; i
< MAXQUOTAS
; i
++)
728 ump
->um_quotas
[i
] = NULLVP
;
729 devvp
->v_specflags
|= SI_MOUNTEDON
;
731 ump
->um_savedmaxfilesize
= fs
->fs_maxfilesize
; /* XXX */
732 maxfilesize
= (u_int64_t
)0x100000000; /* 4GB */
734 maxfilesize
= (u_int64_t
)0x40000000 * fs
->fs_bsize
- 1; /* XXX */
736 if (fs
->fs_maxfilesize
> maxfilesize
) /* XXX */
737 fs
->fs_maxfilesize
= maxfilesize
; /* XXX */
740 (void) ffs_sbupdate(ump
, MNT_WAIT
);
746 (void)VOP_CLOSE(devvp
, ronly
? FREAD
: FREAD
|FWRITE
, cred
, p
);
748 _FREE(ump
->um_fs
, M_UFSMNT
);
749 _FREE(ump
, M_UFSMNT
);
750 mp
->mnt_data
= (qaddr_t
)0;
756 * Sanity checks for old file systems.
758 * XXX - goes away some day.
765 fs
->fs_npsect
= max(fs
->fs_npsect
, fs
->fs_nsect
); /* XXX */
766 fs
->fs_interleave
= max(fs
->fs_interleave
, 1); /* XXX */
767 if (fs
->fs_postblformat
== FS_42POSTBLFMT
) /* XXX */
768 fs
->fs_nrpos
= 8; /* XXX */
769 if (fs
->fs_inodefmt
< FS_44INODEFMT
) { /* XXX */
770 u_int64_t sizepb
= fs
->fs_bsize
; /* XXX */
772 fs
->fs_maxfilesize
= fs
->fs_bsize
* NDADDR
- 1; /* XXX */
773 for (i
= 0; i
< NIADDR
; i
++) { /* XXX */
774 sizepb
*= NINDIR(fs
); /* XXX */
775 fs
->fs_maxfilesize
+= sizepb
; /* XXX */
777 fs
->fs_qbmask
= ~fs
->fs_bmask
; /* XXX */
778 fs
->fs_qfmask
= ~fs
->fs_fmask
; /* XXX */
784 * unmount system call
787 ffs_unmount(mp
, mntflags
, p
)
792 register struct ufsmount
*ump
;
793 register struct fs
*fs
;
796 if (mntflags
& MNT_FORCE
)
798 if (error
= ffs_flushfiles(mp
, flags
, p
))
802 if (fs
->fs_ronly
== 0) {
804 if (error
= ffs_sbupdate(ump
, MNT_WAIT
)) {
807 /* we can atleast cleanup ; as the media could be WP */
808 /* & during mount, we do not check for write failures */
809 /* FIXME LATER : the Correct fix would be to have */
810 /* mount detect the WP media and downgrade to readonly mount */
811 /* For now, here it is */
816 ump
->um_devvp
->v_specflags
&= ~SI_MOUNTEDON
;
817 error
= VOP_CLOSE(ump
->um_devvp
, fs
->fs_ronly
? FREAD
: FREAD
|FWRITE
,
819 vrele(ump
->um_devvp
);
821 _FREE(fs
->fs_csp
[0], M_UFSMNT
);
823 _FREE(ump
, M_UFSMNT
);
824 mp
->mnt_data
= (qaddr_t
)0;
826 mp
->mnt_flag
&= ~MNT_REVEND
;
827 #endif /* REV_ENDIAN_FS */
832 * Flush out all the files in a filesystem.
834 ffs_flushfiles(mp
, flags
, p
)
835 register struct mount
*mp
;
839 register struct ufsmount
*ump
;
844 if (mp
->mnt_flag
& MNT_QUOTA
) {
845 if (error
= vflush(mp
, NULLVP
, SKIPSYSTEM
|flags
))
847 for (i
= 0; i
< MAXQUOTAS
; i
++) {
848 if (ump
->um_quotas
[i
] == NULLVP
)
853 * Here we fall through to vflush again to ensure
854 * that we have gotten rid of all the system vnodes.
858 error
= vflush(mp
, NULLVP
, SKIPSWAP
|flags
);
859 error
= vflush(mp
, NULLVP
, flags
);
864 * Get file system statistics.
867 ffs_statfs(mp
, sbp
, p
)
869 register struct statfs
*sbp
;
872 register struct ufsmount
*ump
;
873 register struct fs
*fs
;
877 if (fs
->fs_magic
!= FS_MAGIC
)
879 sbp
->f_bsize
= fs
->fs_fsize
;
880 sbp
->f_iosize
= fs
->fs_bsize
;
881 sbp
->f_blocks
= fs
->fs_dsize
;
882 sbp
->f_bfree
= fs
->fs_cstotal
.cs_nbfree
* fs
->fs_frag
+
883 fs
->fs_cstotal
.cs_nffree
;
884 sbp
->f_bavail
= freespace(fs
, fs
->fs_minfree
);
885 sbp
->f_files
= fs
->fs_ncg
* fs
->fs_ipg
- ROOTINO
;
886 sbp
->f_ffree
= fs
->fs_cstotal
.cs_nifree
;
887 if (sbp
!= &mp
->mnt_stat
) {
888 sbp
->f_type
= mp
->mnt_vfc
->vfc_typenum
;
889 bcopy((caddr_t
)mp
->mnt_stat
.f_mntonname
,
890 (caddr_t
)&sbp
->f_mntonname
[0], MNAMELEN
);
891 bcopy((caddr_t
)mp
->mnt_stat
.f_mntfromname
,
892 (caddr_t
)&sbp
->f_mntfromname
[0], MNAMELEN
);
898 * Go through the disk queues to initiate sandbagged IO;
899 * go through the inodes to write those that have been modified;
900 * initiate the writing of the super block if it has been modified.
902 * Note: we are always called with the filesystem marked `MPBUSY'.
905 ffs_sync(mp
, waitfor
, cred
, p
)
911 struct vnode
*nvp
, *vp
;
913 struct ufsmount
*ump
= VFSTOUFS(mp
);
915 int error
, allerror
= 0;
918 if (fs
->fs_fmod
!= 0 && fs
->fs_ronly
!= 0) { /* XXX */
919 printf("fs = %s\n", fs
->fs_fsmnt
);
920 panic("update: rofs mod");
923 * Write back each (modified) inode.
925 simple_lock(&mntvnode_slock
);
927 for (vp
= mp
->mnt_vnodelist
.lh_first
;
933 * If the vnode that we are about to sync is no longer
934 * associated with this mount point, start over.
936 if (vp
->v_mount
!= mp
)
938 simple_lock(&vp
->v_interlock
);
939 nvp
= vp
->v_mntvnodes
.le_next
;
941 if ((vp
->v_type
== VNON
) ||
942 ((ip
->i_flag
& (IN_ACCESS
| IN_CHANGE
| IN_MODIFIED
| IN_UPDATE
)) == 0 &&
943 vp
->v_dirtyblkhd
.lh_first
== NULL
&& !(vp
->v_flag
& VHASDIRTY
))) {
944 simple_unlock(&vp
->v_interlock
);
947 simple_unlock(&mntvnode_slock
);
948 error
= vget(vp
, LK_EXCLUSIVE
| LK_NOWAIT
| LK_INTERLOCK
, p
);
950 simple_lock(&mntvnode_slock
);
955 didhold
= ubc_hold(vp
);
956 if (error
= VOP_FSYNC(vp
, cred
, waitfor
, p
))
958 VOP_UNLOCK(vp
, 0, p
);
962 simple_lock(&mntvnode_slock
);
964 simple_unlock(&mntvnode_slock
);
966 * Force stale file system control information to be flushed.
968 if (error
= VOP_FSYNC(ump
->um_devvp
, cred
, waitfor
, p
))
974 * Write back modified superblock.
976 if (fs
->fs_fmod
!= 0) {
978 fs
->fs_time
= time
.tv_sec
;
979 if (error
= ffs_sbupdate(ump
, waitfor
))
986 * Look up a FFS dinode number to find its incore vnode, otherwise read it
987 * in from disk. If it is in core, wait for the lock bit to clear, then
988 * return the inode locked. Detection and handling of mount points must be
989 * done by the calling routine.
992 ffs_vget(mp
, ino
, vpp
)
997 struct proc
*p
= current_proc(); /* XXX */
1000 struct ufsmount
*ump
;
1009 /* Check for unmount in progress */
1010 if (mp
->mnt_kern_flag
& MNTK_UNMOUNT
) {
1015 if ((*vpp
= ufs_ihashget(dev
, ino
)) != NULL
) {
1017 UBCINFOCHECK("ffs_vget", vp
);
1020 /* Allocate a new vnode/inode. */
1021 type
= ump
->um_devvp
->v_tag
== VT_MFS
? M_MFSNODE
: M_FFSNODE
; /* XXX */
1022 MALLOC_ZONE(ip
, struct inode
*, sizeof(struct inode
), type
, M_WAITOK
);
1023 bzero((caddr_t
)ip
, sizeof(struct inode
));
1024 lockinit(&ip
->i_lock
, PINOD
, "inode", 0, 0);
1025 /* lock the inode */
1026 lockmgr(&ip
->i_lock
, LK_EXCLUSIVE
, (struct slock
*)0, p
);
1028 ip
->i_fs
= fs
= ump
->um_fs
;
1031 ip
->i_flag
|= IN_ALLOC
;
1033 for (i
= 0; i
< MAXQUOTAS
; i
++)
1034 ip
->i_dquot
[i
] = NODQUOT
;
1038 * MALLOC_ZONE is blocking call. Check for race.
1040 if ((*vpp
= ufs_ihashget(dev
, ino
)) != NULL
) {
1042 FREE_ZONE(ip
, sizeof(struct inode
), type
);
1044 UBCINFOCHECK("ffs_vget", vp
);
1049 * Put it onto its hash chain locked so that other requests for
1050 * this inode will block if they arrive while we are sleeping waiting
1051 * for old data structures to be purged or for the contents of the
1052 * disk portion of this inode to be read.
1056 if (error
= getnewvnode(VT_UFS
, mp
, ffs_vnodeop_p
, &vp
)) {
1058 if (ISSET(ip
->i_flag
, IN_WALLOC
))
1060 FREE_ZONE(ip
, sizeof(struct inode
), type
);
1068 * A vnode is associated with the inode now,
1069 * vget() can deal with the serialization.
1071 CLR(ip
->i_flag
, IN_ALLOC
);
1072 if (ISSET(ip
->i_flag
, IN_WALLOC
))
1075 /* Read in the disk contents for the inode, copy into the inode. */
1076 if (error
= bread(ump
->um_devvp
, fsbtodb(fs
, ino_to_fsba(fs
, ino
)),
1077 (int)fs
->fs_bsize
, NOCRED
, &bp
)) {
1079 * The inode does not contain anything useful, so it would
1080 * be misleading to leave it on its hash chain. With mode
1081 * still zero, it will be unlinked and returned to the free
1090 if (mp
->mnt_flag
& MNT_REVEND
) {
1091 byte_swap_inode_in(((struct dinode
*)bp
->b_data
+ ino_to_fsbo(fs
, ino
)),ip
);
1093 #endif /* REV_ENDIAN_FS */
1094 ip
->i_din
= *((struct dinode
*)bp
->b_data
+ ino_to_fsbo(fs
, ino
));
1097 #endif /* REV_ENDIAN_FS */
1101 * Initialize the vnode from the inode, check for aliases.
1102 * Note that the underlying vnode may have changed.
1104 if (error
= ufs_vinit(mp
, ffs_specop_p
, FFS_FIFOOPS
, &vp
)) {
1110 * Finish inode initialization now that aliasing has been resolved.
1112 ip
->i_devvp
= ump
->um_devvp
;
1115 * Set up a generation number for this inode if it does not
1116 * already have one. This should only happen on old filesystems.
1118 if (ip
->i_gen
== 0) {
1119 if (++nextgennumber
< (u_long
)time
.tv_sec
)
1120 nextgennumber
= time
.tv_sec
;
1121 ip
->i_gen
= nextgennumber
;
1122 if ((vp
->v_mount
->mnt_flag
& MNT_RDONLY
) == 0)
1123 ip
->i_flag
|= IN_MODIFIED
;
1126 * Ensure that uid and gid are correct. This is a temporary
1127 * fix until fsck has been changed to do the update.
1129 if (fs
->fs_inodefmt
< FS_44INODEFMT
) { /* XXX */
1130 ip
->i_uid
= ip
->i_din
.di_ouid
; /* XXX */
1131 ip
->i_gid
= ip
->i_din
.di_ogid
; /* XXX */
1135 if (UBCINFOMISSING(vp
) || UBCINFORECLAIMED(vp
))
1141 * File handle to vnode
1143 * Have to be really careful about stale file handles:
1144 * - check that the inode number is valid
1145 * - call ffs_vget() to get the locked inode
1146 * - check for an unallocated inode (i_mode == 0)
1147 * - check that the given client host has export rights and return
1148 * those rights via. exflagsp and credanonp
1151 ffs_fhtovp(mp
, fhp
, nam
, vpp
, exflagsp
, credanonp
)
1152 register struct mount
*mp
;
1157 struct ucred
**credanonp
;
1159 register struct ufid
*ufhp
;
1162 ufhp
= (struct ufid
*)fhp
;
1163 fs
= VFSTOUFS(mp
)->um_fs
;
1164 if (ufhp
->ufid_ino
< ROOTINO
||
1165 ufhp
->ufid_ino
>= fs
->fs_ncg
* fs
->fs_ipg
)
1167 return (ufs_check_export(mp
, ufhp
, nam
, vpp
, exflagsp
, credanonp
));
1171 * Vnode pointer to File handle
1178 register struct inode
*ip
;
1179 register struct ufid
*ufhp
;
1182 ufhp
= (struct ufid
*)fhp
;
1183 ufhp
->ufid_len
= sizeof(struct ufid
);
1184 ufhp
->ufid_ino
= ip
->i_number
;
1185 ufhp
->ufid_gen
= ip
->i_gen
;
1190 * Initialize the filesystem; just use ufs_init.
1194 struct vfsconf
*vfsp
;
1197 return (ufs_init(vfsp
));
1201 * fast filesystem related variables.
1203 ffs_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
, p
)
1212 extern int doclusterread
, doclusterwrite
, doreallocblks
, doasyncfree
;
1214 /* all sysctl names at this level are terminal */
1216 return (ENOTDIR
); /* overloaded */
1219 case FFS_CLUSTERREAD
:
1220 return (sysctl_int(oldp
, oldlenp
, newp
, newlen
,
1222 case FFS_CLUSTERWRITE
:
1223 return (sysctl_int(oldp
, oldlenp
, newp
, newlen
,
1225 case FFS_REALLOCBLKS
:
1226 return (sysctl_int(oldp
, oldlenp
, newp
, newlen
,
1229 return (sysctl_int(oldp
, oldlenp
, newp
, newlen
, &doasyncfree
));
1231 return (EOPNOTSUPP
);
1237 * Write a superblock and associated information back to disk.
1240 ffs_sbupdate(mp
, waitfor
)
1241 struct ufsmount
*mp
;
1244 register struct fs
*dfs
, *fs
= mp
->um_fs
;
1245 register struct buf
*bp
;
1248 int i
, size
, error
, allerror
= 0;
1251 int rev_endian
=(mp
->um_mountp
->mnt_flag
& MNT_REVEND
);
1252 #endif /* REV_ENDIAN_FS */
1255 * First write back the summary information.
1257 blks
= howmany(fs
->fs_cssize
, fs
->fs_fsize
);
1258 space
= (caddr_t
)fs
->fs_csp
[0];
1259 for (i
= 0; i
< blks
; i
+= fs
->fs_frag
) {
1260 size
= fs
->fs_bsize
;
1261 if (i
+ fs
->fs_frag
> blks
)
1262 size
= (blks
- i
) * fs
->fs_fsize
;
1263 bp
= getblk(mp
->um_devvp
, fsbtodb(fs
, fs
->fs_csaddr
+ i
),
1264 size
, 0, 0, BLK_META
);
1265 bcopy(space
, bp
->b_data
, (u_int
)size
);
1268 byte_swap_ints((int *)bp
->b_data
, size
/ sizeof(int));
1270 #endif /* REV_ENDIAN_FS */
1272 if (waitfor
!= MNT_WAIT
)
1274 else if (error
= bwrite(bp
))
1278 * Now write back the superblock itself. If any errors occurred
1279 * up to this point, then fail so that the superblock avoids
1280 * being written out as clean.
1284 VOP_DEVBLOCKSIZE(mp
->um_devvp
,&devBlockSize
);
1285 bp
= getblk(mp
->um_devvp
, (SBOFF
/devBlockSize
), (int)fs
->fs_sbsize
, 0, 0, BLK_META
);
1286 bcopy((caddr_t
)fs
, bp
->b_data
, (u_int
)fs
->fs_sbsize
);
1287 /* Restore compatibility to old file systems. XXX */
1288 dfs
= (struct fs
*)bp
->b_data
; /* XXX */
1289 if (fs
->fs_postblformat
== FS_42POSTBLFMT
) /* XXX */
1290 dfs
->fs_nrpos
= -1; /* XXX */
1293 * Swapping bytes here ; so that in case
1294 * of inode format < FS_44INODEFMT appropriate
1298 byte_swap_sbout((struct fs
*)bp
->b_data
);
1300 #endif /* REV_ENDIAN_FS */
1301 if (fs
->fs_inodefmt
< FS_44INODEFMT
) { /* XXX */
1302 int32_t *lp
, tmp
; /* XXX */
1304 lp
= (int32_t *)&dfs
->fs_qbmask
; /* XXX */
1305 tmp
= lp
[4]; /* XXX */
1306 for (i
= 4; i
> 0; i
--) /* XXX */
1307 lp
[i
] = lp
[i
-1]; /* XXX */
1308 lp
[0] = tmp
; /* XXX */
1311 /* Note that dfs is already swapped so swap the filesize
1315 dfs
->fs_maxfilesize
= NXSwapLongLong(mp
->um_savedmaxfilesize
); /* XXX */
1317 #endif /* REV_ENDIAN_FS */
1318 dfs
->fs_maxfilesize
= mp
->um_savedmaxfilesize
; /* XXX */
1321 #endif /* REV_ENDIAN_FS */
1322 if (waitfor
!= MNT_WAIT
)
1324 else if (error
= bwrite(bp
))