]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ffs/ffs_vfsops.c
e91a041f756e30767217f5d0448b1b57c04a8d74
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>
75 #include <sys/quota.h>
77 #include <miscfs/specfs/specdev.h>
79 #include <ufs/ufs/quota.h>
80 #include <ufs/ufs/ufsmount.h>
81 #include <ufs/ufs/inode.h>
82 #include <ufs/ufs/ufs_extern.h>
84 #include <ufs/ffs/fs.h>
85 #include <ufs/ffs/ffs_extern.h>
87 #include <ufs/ufs/ufs_byte_order.h>
88 #include <architecture/byte_order.h>
89 #endif /* REV_ENDIAN_FS */
91 int ffs_sbupdate
__P((struct ufsmount
*, int));
93 struct vfsops ufs_vfsops
= {
108 extern u_long nextgennumber
;
111 * Called by main() when ufs is going to be mounted as root.
115 extern struct vnode
*rootvp
;
118 struct proc
*p
= current_proc(); /* XXX */
119 struct ufsmount
*ump
;
124 * Get vnode for rootdev.
126 if (error
= bdevvp(rootdev
, &rootvp
)) {
127 printf("ffs_mountroot: can't setup bdevvp");
130 if (error
= vfs_rootmountalloc("ufs", "root_device", &mp
)) {
131 vrele(rootvp
); /* release the reference from bdevvp() */
135 /* Must set the MNT_ROOTFS flag before doing the actual mount */
136 mp
->mnt_flag
|= MNT_ROOTFS
;
138 if (error
= ffs_mountfs(rootvp
, mp
, p
)) {
139 mp
->mnt_vfc
->vfc_refcount
--;
141 vrele(rootvp
); /* release the reference from bdevvp() */
142 _FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
145 simple_lock(&mountlist_slock
);
146 CIRCLEQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
147 simple_unlock(&mountlist_slock
);
150 (void) copystr(mp
->mnt_stat
.f_mntonname
, fs
->fs_fsmnt
, MNAMELEN
- 1, 0);
151 (void)ffs_statfs(mp
, &mp
->mnt_stat
, p
);
153 inittodr(fs
->fs_time
);
163 ffs_mount(mp
, path
, data
, ndp
, p
)
164 register struct mount
*mp
;
167 struct nameidata
*ndp
;
171 struct ufs_args args
;
172 struct ufsmount
*ump
;
173 register struct fs
*fs
;
180 if (error
= copyin(data
, (caddr_t
)&args
, sizeof (struct ufs_args
)))
183 * If updating, check whether changing from read-only to
184 * read/write; if there is no device name, that's all we do.
186 if (mp
->mnt_flag
& MNT_UPDATE
) {
189 if (fs
->fs_ronly
== 0 && (mp
->mnt_flag
& MNT_RDONLY
)) {
191 if (mp
->mnt_flag
& MNT_FORCE
)
193 if (error
= ffs_flushfiles(mp
, flags
, p
))
197 if (error
= ffs_sbupdate(ump
, MNT_WAIT
)) {
203 /* save fs_ronly to later use */
204 ronly
= fs
->fs_ronly
;
205 if ((mp
->mnt_flag
& MNT_RELOAD
) || ronly
)
208 (error
= ffs_reload(mp
, ndp
->ni_cnd
.cn_cred
, p
)))
210 /* replace the ronly after load */
211 fs
->fs_ronly
= ronly
;
213 * Do not update the file system if the user was in singleuser
214 * and then tries to mount -uw without fscking
216 if (!fs
->fs_clean
&& ronly
) {
217 printf("WARNING: trying to mount a dirty file system\n");
218 if (issingleuser() && (mp
->mnt_flag
& MNT_ROOTFS
)) {
219 printf("WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",fs
->fs_fsmnt
);
221 * Reset the readonly bit as reload might have
229 if (ronly
&& (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)) {
231 * If upgrade to read-write by non-root, then verify
232 * that user has necessary permissions on the device.
234 if (p
->p_ucred
->cr_uid
!= 0) {
235 devvp
= ump
->um_devvp
;
236 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
237 if (error
= VOP_ACCESS(devvp
, VREAD
| VWRITE
,
239 VOP_UNLOCK(devvp
, 0, p
);
242 VOP_UNLOCK(devvp
, 0, p
);
246 (void) ffs_sbupdate(ump
, MNT_WAIT
);
248 if (args
.fspec
== 0) {
250 * Process export requests.
252 return (vfs_export(mp
, &ump
->um_export
, &args
.export
));
256 * Not an update, or updating the name: look up the name
257 * and verify that it refers to a sensible block device.
259 NDINIT(ndp
, LOOKUP
, FOLLOW
, UIO_USERSPACE
, args
.fspec
, p
);
260 if (error
= namei(ndp
))
264 if (devvp
->v_type
!= VBLK
) {
268 if (major(devvp
->v_rdev
) >= nblkdev
) {
273 * If mount by non-root, then verify that user has necessary
274 * permissions on the device.
276 if (p
->p_ucred
->cr_uid
!= 0) {
278 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0)
279 accessmode
|= VWRITE
;
280 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
281 if (error
= VOP_ACCESS(devvp
, accessmode
, p
->p_ucred
, p
)) {
285 VOP_UNLOCK(devvp
, 0, p
);
287 if ((mp
->mnt_flag
& MNT_UPDATE
) == 0)
288 error
= ffs_mountfs(devvp
, mp
, p
);
290 if (devvp
!= ump
->um_devvp
)
291 error
= EINVAL
; /* needs translation */
301 (void) copyinstr(path
, fs
->fs_fsmnt
, sizeof(fs
->fs_fsmnt
) - 1, &size
);
302 bzero(fs
->fs_fsmnt
+ size
, sizeof(fs
->fs_fsmnt
) - size
);
303 bcopy((caddr_t
)fs
->fs_fsmnt
, (caddr_t
)mp
->mnt_stat
.f_mntonname
,
305 (void) copyinstr(args
.fspec
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
- 1,
307 bzero(mp
->mnt_stat
.f_mntfromname
+ size
, MNAMELEN
- size
);
308 (void)ffs_statfs(mp
, &mp
->mnt_stat
, p
);
313 * Reload all incore data for a filesystem (used after running fsck on
314 * the root filesystem and finding things to fix). The filesystem must
315 * be mounted read-only.
317 * Things to do to update the mount:
318 * 1) invalidate all cached meta-data.
319 * 2) re-read superblock from disk.
320 * 3) re-read summary information from disk.
321 * 4) invalidate all inactive vnodes.
322 * 5) invalidate all cached file data.
323 * 6) re-read inode data for all active vnodes.
325 ffs_reload(mountp
, cred
, p
)
326 register struct mount
*mountp
;
330 register struct vnode
*vp
, *nvp
, *devvp
;
334 struct fs
*fs
, *newfs
;
335 int i
, blks
, size
, error
;
336 u_int64_t maxfilesize
; /* XXX */
339 int rev_endian
= (mountp
->mnt_flag
& MNT_REVEND
);
340 #endif /* REV_ENDIAN_FS */
342 if ((mountp
->mnt_flag
& MNT_RDONLY
) == 0)
345 * Step 1: invalidate all cached meta-data.
347 devvp
= VFSTOUFS(mountp
)->um_devvp
;
348 if (vinvalbuf(devvp
, 0, cred
, p
, 0, 0))
349 panic("ffs_reload: dirty1");
351 * Step 2: re-read superblock from disk.
353 VOP_DEVBLOCKSIZE(devvp
,&size
);
355 if (error
= bread(devvp
, (ufs_daddr_t
)(SBOFF
/size
), SBSIZE
, NOCRED
,&bp
)) {
359 newfs
= (struct fs
*)bp
->b_data
;
362 byte_swap_sbin(newfs
);
364 #endif /* REV_ENDIAN_FS */
365 if (newfs
->fs_magic
!= FS_MAGIC
|| newfs
->fs_bsize
> MAXBSIZE
||
366 newfs
->fs_bsize
< sizeof(struct fs
)) {
369 byte_swap_sbout(newfs
);
370 #endif /* REV_ENDIAN_FS */
373 return (EIO
); /* XXX needs translation */
375 fs
= VFSTOUFS(mountp
)->um_fs
;
377 * Copy pointer fields back into superblock before copying in XXX
378 * new superblock. These should really be in the ufsmount. XXX
379 * Note that important parameters (eg fs_ncg) are unchanged.
381 newfs
->fs_csp
= fs
->fs_csp
;
382 newfs
->fs_maxcluster
= fs
->fs_maxcluster
;
383 bcopy(newfs
, fs
, (u_int
)fs
->fs_sbsize
);
384 if (fs
->fs_sbsize
< SBSIZE
)
385 bp
->b_flags
|= B_INVAL
;
388 byte_swap_sbout(newfs
);
389 #endif /* REV_ENDIAN_FS */
391 mountp
->mnt_maxsymlinklen
= fs
->fs_maxsymlinklen
;
393 maxfilesize
= (u_int64_t
)0x100000000; /* 4GB */
394 if (fs
->fs_maxfilesize
> maxfilesize
) /* XXX */
395 fs
->fs_maxfilesize
= maxfilesize
; /* XXX */
397 * Step 3: re-read summary information from disk.
399 blks
= howmany(fs
->fs_cssize
, fs
->fs_fsize
);
401 for (i
= 0; i
< blks
; i
+= fs
->fs_frag
) {
403 if (i
+ fs
->fs_frag
> blks
)
404 size
= (blks
- i
) * fs
->fs_fsize
;
405 if (error
= bread(devvp
, fsbtodb(fs
, fs
->fs_csaddr
+ i
), size
,
413 byte_swap_ints((int *)bp
->b_data
, size
/ sizeof(int));
415 #endif /* REV_ENDIAN_FS */
416 bcopy(bp
->b_data
, space
, (u_int
)size
);
420 byte_swap_ints((int *)bp
->b_data
, size
/ sizeof(int));
422 #endif /* REV_ENDIAN_FS */
426 * We no longer know anything about clusters per cylinder group.
428 if (fs
->fs_contigsumsize
> 0) {
429 lp
= fs
->fs_maxcluster
;
430 for (i
= 0; i
< fs
->fs_ncg
; i
++)
431 *lp
++ = fs
->fs_contigsumsize
;
435 simple_lock(&mntvnode_slock
);
436 for (vp
= mountp
->mnt_vnodelist
.lh_first
; vp
!= NULL
; vp
= nvp
) {
437 if (vp
->v_mount
!= mountp
) {
438 simple_unlock(&mntvnode_slock
);
441 nvp
= vp
->v_mntvnodes
.le_next
;
443 * Step 4: invalidate all inactive vnodes.
445 if (vrecycle(vp
, &mntvnode_slock
, p
))
448 * Step 5: invalidate all cached file data.
450 simple_lock(&vp
->v_interlock
);
451 simple_unlock(&mntvnode_slock
);
452 if (vget(vp
, LK_EXCLUSIVE
| LK_INTERLOCK
, p
)) {
455 if (vinvalbuf(vp
, 0, cred
, p
, 0, 0))
456 panic("ffs_reload: dirty2");
458 * Step 6: re-read inode data for all active vnodes.
462 bread(devvp
, fsbtodb(fs
, ino_to_fsba(fs
, ip
->i_number
)),
463 (int)fs
->fs_bsize
, NOCRED
, &bp
)) {
470 byte_swap_inode_in(((struct dinode
*)bp
->b_data
+
471 ino_to_fsbo(fs
, ip
->i_number
)), ip
);
473 #endif /* REV_ENDIAN_FS */
474 ip
->i_din
= *((struct dinode
*)bp
->b_data
+
475 ino_to_fsbo(fs
, ip
->i_number
));
478 #endif /* REV_ENDIAN_FS */
481 simple_lock(&mntvnode_slock
);
483 simple_unlock(&mntvnode_slock
);
488 * Common code for mount and mountroot
491 ffs_mountfs(devvp
, mp
, p
)
492 register struct vnode
*devvp
;
496 register struct ufsmount
*ump
;
498 register struct fs
*fs
;
502 int32_t clustersumoff
;
504 int error
, i
, blks
, size
, ronly
;
507 extern struct vnode
*rootvp
;
508 u_int64_t maxfilesize
; /* XXX */
509 u_int dbsize
= DEV_BSIZE
;
512 #endif /* REV_ENDIAN_FS */
514 cred
= p
? p
->p_ucred
: NOCRED
;
516 * Disallow multiple mounts of the same device.
517 * Disallow mounting of a device that is currently in use
518 * (except for root, which might share swap device for miniroot).
519 * Flush out any old buffers remaining from a previous use.
521 if (error
= vfs_mountedon(devvp
))
523 if (vcount(devvp
) > 1 && devvp
!= rootvp
)
525 if (error
= vinvalbuf(devvp
, V_SAVE
, cred
, p
, 0, 0))
528 ronly
= (mp
->mnt_flag
& MNT_RDONLY
) != 0;
529 if (error
= VOP_OPEN(devvp
, ronly
? FREAD
: FREAD
|FWRITE
, FSCRED
, p
))
532 VOP_DEVBLOCKSIZE(devvp
,&size
);
536 if (error
= bread(devvp
, (ufs_daddr_t
)(SBOFF
/size
), SBSIZE
, cred
, &bp
))
538 fs
= (struct fs
*)bp
->b_data
;
540 if (fs
->fs_magic
!= FS_MAGIC
|| fs
->fs_bsize
> MAXBSIZE
||
541 fs
->fs_bsize
< sizeof(struct fs
)) {
542 int magic
= fs
->fs_magic
;
544 byte_swap_ints(&magic
, 1);
545 if (magic
!= FS_MAGIC
) {
550 if (fs
->fs_magic
!= FS_MAGIC
|| fs
->fs_bsize
> MAXBSIZE
||
551 fs
->fs_bsize
< sizeof(struct fs
)) {
553 error
= EINVAL
; /* XXX needs translation */
558 #endif /* REV_ENDIAN_FS */
559 if (fs
->fs_magic
!= FS_MAGIC
|| fs
->fs_bsize
> MAXBSIZE
||
560 fs
->fs_bsize
< sizeof(struct fs
)) {
564 #endif /* REV_ENDIAN_FS */
565 error
= EINVAL
; /* XXX needs translation */
571 * Buffer cache does not handle multiple pages in a buf when
572 * invalidating incore buffer in pageout. There are no locks
573 * in the pageout path. So there is a danger of loosing data when
574 * block allocation happens at the same time a pageout of buddy
575 * page occurs. incore() returns buf with both
576 * pages, this leads vnode-pageout to incorrectly flush of entire.
577 * buf. Till the low level ffs code is modified to deal with these
578 * do not mount any FS more than 4K size.
581 * Can't mount filesystems with a fragment size less than DIRBLKSIZ
584 * Don't mount dirty filesystems, except for the root filesystem
586 if ((fs
->fs_bsize
> PAGE_SIZE
) || (fs
->fs_fsize
< DIRBLKSIZ
) ||
587 ((!(mp
->mnt_flag
& MNT_ROOTFS
)) && (!fs
->fs_clean
))) {
591 #endif /* REV_ENDIAN_FS */
596 /* Let's figure out the devblock size the file system is with */
597 /* the device block size = fragment size / number of sectors per frag */
599 dbsize
= fs
->fs_fsize
/ NSPF(fs
);
601 kprintf("device blocksize computaion failed\n");
603 if (VOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, &dbsize
, FWRITE
, NOCRED
,
605 kprintf("failed to set device blocksize\n");
607 /* force the specfs to reread blocksize from size() */
608 set_fsblocksize(devvp
);
611 /* cache the IO attributes */
612 error
= vfs_init_io_attributes(devvp
, mp
);
614 printf("ffs_mountfs: vfs_init_io_attributes returned %d\n",
619 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
620 if (fs
->fs_postblformat
== FS_42POSTBLFMT
&& !ronly
) {
624 #endif /* REV_ENDIAN_FS */
625 error
= EROFS
; /* needs translation */
629 /* If we are not mounting read only, then check for overlap
630 * condition in cylinder group's free block map.
631 * If overlap exists, then force this into a read only mount
632 * to avoid further corruption. PR#2216969
635 if (error
= bread (devvp
, fsbtodb(fs
, cgtod(fs
, 0)),
636 (int)fs
->fs_cgsize
, NOCRED
, &cgbp
)) {
640 cgp
= (struct cg
*)cgbp
->b_data
;
643 byte_swap_cgin(cgp
,fs
);
644 #endif /* REV_ENDIAN_FS */
645 if (!cg_chkmagic(cgp
)){
648 byte_swap_cgout(cgp
,fs
);
649 #endif /* REV_ENDIAN_FS */
653 if (cgp
->cg_clustersumoff
!= 0) {
654 /* Check for overlap */
655 clustersumoff
= cgp
->cg_freeoff
+
656 howmany(fs
->fs_cpg
* fs
->fs_spc
/ NSPF(fs
), NBBY
);
657 clustersumoff
= roundup(clustersumoff
, sizeof(long));
658 if (cgp
->cg_clustersumoff
< clustersumoff
) {
660 mp
->mnt_flag
|= MNT_RDONLY
;
666 byte_swap_cgout(cgp
,fs
);
667 #endif /* REV_ENDIAN_FS */
671 ump
= _MALLOC(sizeof *ump
, M_UFSMNT
, M_WAITOK
);
672 bzero((caddr_t
)ump
, sizeof *ump
);
673 ump
->um_fs
= _MALLOC((u_long
)fs
->fs_sbsize
, M_UFSMNT
,
675 bcopy(bp
->b_data
, ump
->um_fs
, (u_int
)fs
->fs_sbsize
);
676 if (fs
->fs_sbsize
< SBSIZE
)
677 bp
->b_flags
|= B_INVAL
;
681 #endif /* REV_ENDIAN_FS */
685 fs
->fs_ronly
= ronly
;
686 size
= fs
->fs_cssize
;
687 blks
= howmany(size
, fs
->fs_fsize
);
688 if (fs
->fs_contigsumsize
> 0)
689 size
+= fs
->fs_ncg
* sizeof(int32_t);
690 space
= _MALLOC((u_long
)size
, M_UFSMNT
, M_WAITOK
);
692 for (i
= 0; i
< blks
; i
+= fs
->fs_frag
) {
694 if (i
+ fs
->fs_frag
> blks
)
695 size
= (blks
- i
) * fs
->fs_fsize
;
696 if (error
= bread(devvp
, fsbtodb(fs
, fs
->fs_csaddr
+ i
), size
,
698 _FREE(fs
->fs_csp
, M_UFSMNT
);
701 bcopy(bp
->b_data
, space
, (u_int
)size
);
704 byte_swap_ints((int *) space
, size
/ sizeof(int));
705 #endif /* REV_ENDIAN_FS */
706 space
= (char *)space
+ size
;
710 if (fs
->fs_contigsumsize
> 0) {
711 fs
->fs_maxcluster
= lp
= space
;
712 for (i
= 0; i
< fs
->fs_ncg
; i
++)
713 *lp
++ = fs
->fs_contigsumsize
;
715 mp
->mnt_data
= (qaddr_t
)ump
;
716 mp
->mnt_stat
.f_fsid
.val
[0] = (long)dev
;
717 mp
->mnt_stat
.f_fsid
.val
[1] = mp
->mnt_vfc
->vfc_typenum
;
718 #warning hardcoded max symlen and not "mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;"
719 mp
->mnt_maxsymlinklen
= 60;
722 mp
->mnt_flag
|= MNT_REVEND
;
723 #endif /* REV_ENDIAN_FS */
726 ump
->um_devvp
= devvp
;
727 ump
->um_nindir
= fs
->fs_nindir
;
728 ump
->um_bptrtodb
= fs
->fs_fsbtodb
;
729 ump
->um_seqinc
= fs
->fs_frag
;
730 for (i
= 0; i
< MAXQUOTAS
; i
++)
731 ump
->um_qfiles
[i
].qf_vp
= NULLVP
;
732 devvp
->v_specflags
|= SI_MOUNTEDON
;
734 ump
->um_savedmaxfilesize
= fs
->fs_maxfilesize
; /* XXX */
735 maxfilesize
= (u_int64_t
)0x100000000; /* 4GB */
737 maxfilesize
= (u_int64_t
)0x40000000 * fs
->fs_bsize
- 1; /* XXX */
739 if (fs
->fs_maxfilesize
> maxfilesize
) /* XXX */
740 fs
->fs_maxfilesize
= maxfilesize
; /* XXX */
743 (void) ffs_sbupdate(ump
, MNT_WAIT
);
749 (void)VOP_CLOSE(devvp
, ronly
? FREAD
: FREAD
|FWRITE
, cred
, p
);
751 _FREE(ump
->um_fs
, M_UFSMNT
);
752 _FREE(ump
, M_UFSMNT
);
753 mp
->mnt_data
= (qaddr_t
)0;
759 * Sanity checks for old file systems.
761 * XXX - goes away some day.
768 fs
->fs_npsect
= max(fs
->fs_npsect
, fs
->fs_nsect
); /* XXX */
769 fs
->fs_interleave
= max(fs
->fs_interleave
, 1); /* XXX */
770 if (fs
->fs_postblformat
== FS_42POSTBLFMT
) /* XXX */
771 fs
->fs_nrpos
= 8; /* XXX */
772 if (fs
->fs_inodefmt
< FS_44INODEFMT
) { /* XXX */
773 u_int64_t sizepb
= fs
->fs_bsize
; /* XXX */
775 fs
->fs_maxfilesize
= fs
->fs_bsize
* NDADDR
- 1; /* XXX */
776 for (i
= 0; i
< NIADDR
; i
++) { /* XXX */
777 sizepb
*= NINDIR(fs
); /* XXX */
778 fs
->fs_maxfilesize
+= sizepb
; /* XXX */
780 fs
->fs_qbmask
= ~fs
->fs_bmask
; /* XXX */
781 fs
->fs_qfmask
= ~fs
->fs_fmask
; /* XXX */
787 * unmount system call
790 ffs_unmount(mp
, mntflags
, p
)
795 register struct ufsmount
*ump
;
796 register struct fs
*fs
;
802 if (mntflags
& MNT_FORCE
) {
806 if ( (error
= ffs_flushfiles(mp
, flags
, p
)) && !force
)
810 if (fs
->fs_ronly
== 0) {
812 if (error
= ffs_sbupdate(ump
, MNT_WAIT
)) {
815 /* we can atleast cleanup ; as the media could be WP */
816 /* & during mount, we do not check for write failures */
817 /* FIXME LATER : the Correct fix would be to have */
818 /* mount detect the WP media and downgrade to readonly mount */
819 /* For now, here it is */
824 ump
->um_devvp
->v_specflags
&= ~SI_MOUNTEDON
;
825 error
= VOP_CLOSE(ump
->um_devvp
, fs
->fs_ronly
? FREAD
: FREAD
|FWRITE
,
829 vrele(ump
->um_devvp
);
831 _FREE(fs
->fs_csp
, M_UFSMNT
);
833 _FREE(ump
, M_UFSMNT
);
834 mp
->mnt_data
= (qaddr_t
)0;
836 mp
->mnt_flag
&= ~MNT_REVEND
;
837 #endif /* REV_ENDIAN_FS */
842 * Flush out all the files in a filesystem.
844 ffs_flushfiles(mp
, flags
, p
)
845 register struct mount
*mp
;
849 register struct ufsmount
*ump
;
854 if (mp
->mnt_flag
& MNT_QUOTA
) {
855 if (error
= vflush(mp
, NULLVP
, SKIPSYSTEM
|flags
))
857 for (i
= 0; i
< MAXQUOTAS
; i
++) {
858 if (ump
->um_qfiles
[i
].qf_vp
== NULLVP
)
863 * Here we fall through to vflush again to ensure
864 * that we have gotten rid of all the system vnodes.
868 error
= vflush(mp
, NULLVP
, SKIPSWAP
|flags
);
869 error
= vflush(mp
, NULLVP
, flags
);
874 * Get file system statistics.
877 ffs_statfs(mp
, sbp
, p
)
879 register struct statfs
*sbp
;
882 register struct ufsmount
*ump
;
883 register struct fs
*fs
;
887 if (fs
->fs_magic
!= FS_MAGIC
)
889 sbp
->f_bsize
= fs
->fs_fsize
;
890 sbp
->f_iosize
= fs
->fs_bsize
;
891 sbp
->f_blocks
= fs
->fs_dsize
;
892 sbp
->f_bfree
= fs
->fs_cstotal
.cs_nbfree
* fs
->fs_frag
+
893 fs
->fs_cstotal
.cs_nffree
;
894 sbp
->f_bavail
= freespace(fs
, fs
->fs_minfree
);
895 sbp
->f_files
= fs
->fs_ncg
* fs
->fs_ipg
- ROOTINO
;
896 sbp
->f_ffree
= fs
->fs_cstotal
.cs_nifree
;
897 if (sbp
!= &mp
->mnt_stat
) {
898 sbp
->f_type
= mp
->mnt_vfc
->vfc_typenum
;
899 bcopy((caddr_t
)mp
->mnt_stat
.f_mntonname
,
900 (caddr_t
)&sbp
->f_mntonname
[0], MNAMELEN
);
901 bcopy((caddr_t
)mp
->mnt_stat
.f_mntfromname
,
902 (caddr_t
)&sbp
->f_mntfromname
[0], MNAMELEN
);
908 * Go through the disk queues to initiate sandbagged IO;
909 * go through the inodes to write those that have been modified;
910 * initiate the writing of the super block if it has been modified.
912 * Note: we are always called with the filesystem marked `MPBUSY'.
915 ffs_sync(mp
, waitfor
, cred
, p
)
921 struct vnode
*nvp
, *vp
;
923 struct ufsmount
*ump
= VFSTOUFS(mp
);
925 int error
, allerror
= 0;
928 if (fs
->fs_fmod
!= 0 && fs
->fs_ronly
!= 0) { /* XXX */
929 printf("fs = %s\n", fs
->fs_fsmnt
);
930 panic("update: rofs mod");
933 * Write back each (modified) inode.
935 simple_lock(&mntvnode_slock
);
937 for (vp
= mp
->mnt_vnodelist
.lh_first
;
943 * If the vnode that we are about to sync is no longer
944 * associated with this mount point, start over.
946 if (vp
->v_mount
!= mp
)
948 simple_lock(&vp
->v_interlock
);
949 nvp
= vp
->v_mntvnodes
.le_next
;
951 if ((vp
->v_type
== VNON
) ||
952 ((ip
->i_flag
& (IN_ACCESS
| IN_CHANGE
| IN_MODIFIED
| IN_UPDATE
)) == 0 &&
953 vp
->v_dirtyblkhd
.lh_first
== NULL
&& !(vp
->v_flag
& VHASDIRTY
))) {
954 simple_unlock(&vp
->v_interlock
);
957 simple_unlock(&mntvnode_slock
);
958 error
= vget(vp
, LK_EXCLUSIVE
| LK_NOWAIT
| LK_INTERLOCK
, p
);
960 simple_lock(&mntvnode_slock
);
965 didhold
= ubc_hold(vp
);
966 if (error
= VOP_FSYNC(vp
, cred
, waitfor
, p
))
968 VOP_UNLOCK(vp
, 0, p
);
972 simple_lock(&mntvnode_slock
);
974 simple_unlock(&mntvnode_slock
);
976 * Force stale file system control information to be flushed.
978 if (error
= VOP_FSYNC(ump
->um_devvp
, cred
, waitfor
, p
))
984 * Write back modified superblock.
986 if (fs
->fs_fmod
!= 0) {
988 fs
->fs_time
= time
.tv_sec
;
989 if (error
= ffs_sbupdate(ump
, waitfor
))
996 * Look up a FFS dinode number to find its incore vnode, otherwise read it
997 * in from disk. If it is in core, wait for the lock bit to clear, then
998 * return the inode locked. Detection and handling of mount points must be
999 * done by the calling routine.
1002 ffs_vget(mp
, ino
, vpp
)
1007 struct proc
*p
= current_proc(); /* XXX */
1010 struct ufsmount
*ump
;
1019 /* Check for unmount in progress */
1020 if (mp
->mnt_kern_flag
& MNTK_UNMOUNT
) {
1025 if ((*vpp
= ufs_ihashget(dev
, ino
)) != NULL
) {
1027 UBCINFOCHECK("ffs_vget", vp
);
1030 /* Allocate a new vnode/inode. */
1031 type
= ump
->um_devvp
->v_tag
== VT_MFS
? M_MFSNODE
: M_FFSNODE
; /* XXX */
1032 MALLOC_ZONE(ip
, struct inode
*, sizeof(struct inode
), type
, M_WAITOK
);
1033 bzero((caddr_t
)ip
, sizeof(struct inode
));
1034 lockinit(&ip
->i_lock
, PINOD
, "inode", 0, 0);
1035 /* lock the inode */
1036 lockmgr(&ip
->i_lock
, LK_EXCLUSIVE
, (struct slock
*)0, p
);
1038 ip
->i_fs
= fs
= ump
->um_fs
;
1041 ip
->i_flag
|= IN_ALLOC
;
1043 for (i
= 0; i
< MAXQUOTAS
; i
++)
1044 ip
->i_dquot
[i
] = NODQUOT
;
1048 * MALLOC_ZONE is blocking call. Check for race.
1050 if ((*vpp
= ufs_ihashget(dev
, ino
)) != NULL
) {
1052 FREE_ZONE(ip
, sizeof(struct inode
), type
);
1054 UBCINFOCHECK("ffs_vget", vp
);
1059 * Put it onto its hash chain locked so that other requests for
1060 * this inode will block if they arrive while we are sleeping waiting
1061 * for old data structures to be purged or for the contents of the
1062 * disk portion of this inode to be read.
1066 if (error
= getnewvnode(VT_UFS
, mp
, ffs_vnodeop_p
, &vp
)) {
1068 if (ISSET(ip
->i_flag
, IN_WALLOC
))
1070 FREE_ZONE(ip
, sizeof(struct inode
), type
);
1078 * A vnode is associated with the inode now,
1079 * vget() can deal with the serialization.
1081 CLR(ip
->i_flag
, IN_ALLOC
);
1082 if (ISSET(ip
->i_flag
, IN_WALLOC
))
1085 /* Read in the disk contents for the inode, copy into the inode. */
1086 if (error
= bread(ump
->um_devvp
, fsbtodb(fs
, ino_to_fsba(fs
, ino
)),
1087 (int)fs
->fs_bsize
, NOCRED
, &bp
)) {
1089 * The inode does not contain anything useful, so it would
1090 * be misleading to leave it on its hash chain. With mode
1091 * still zero, it will be unlinked and returned to the free
1100 if (mp
->mnt_flag
& MNT_REVEND
) {
1101 byte_swap_inode_in(((struct dinode
*)bp
->b_data
+ ino_to_fsbo(fs
, ino
)),ip
);
1103 #endif /* REV_ENDIAN_FS */
1104 ip
->i_din
= *((struct dinode
*)bp
->b_data
+ ino_to_fsbo(fs
, ino
));
1107 #endif /* REV_ENDIAN_FS */
1111 * Initialize the vnode from the inode, check for aliases.
1112 * Note that the underlying vnode may have changed.
1114 if (error
= ufs_vinit(mp
, ffs_specop_p
, FFS_FIFOOPS
, &vp
)) {
1120 * Finish inode initialization now that aliasing has been resolved.
1122 ip
->i_devvp
= ump
->um_devvp
;
1125 * Set up a generation number for this inode if it does not
1126 * already have one. This should only happen on old filesystems.
1128 if (ip
->i_gen
== 0) {
1129 if (++nextgennumber
< (u_long
)time
.tv_sec
)
1130 nextgennumber
= time
.tv_sec
;
1131 ip
->i_gen
= nextgennumber
;
1132 if ((vp
->v_mount
->mnt_flag
& MNT_RDONLY
) == 0)
1133 ip
->i_flag
|= IN_MODIFIED
;
1136 * Ensure that uid and gid are correct. This is a temporary
1137 * fix until fsck has been changed to do the update.
1139 if (fs
->fs_inodefmt
< FS_44INODEFMT
) { /* XXX */
1140 ip
->i_uid
= ip
->i_din
.di_ouid
; /* XXX */
1141 ip
->i_gid
= ip
->i_din
.di_ogid
; /* XXX */
1145 if (UBCINFOMISSING(vp
) || UBCINFORECLAIMED(vp
))
1151 * File handle to vnode
1153 * Have to be really careful about stale file handles:
1154 * - check that the inode number is valid
1155 * - call ffs_vget() to get the locked inode
1156 * - check for an unallocated inode (i_mode == 0)
1157 * - check that the given client host has export rights and return
1158 * those rights via. exflagsp and credanonp
1161 ffs_fhtovp(mp
, fhp
, nam
, vpp
, exflagsp
, credanonp
)
1162 register struct mount
*mp
;
1167 struct ucred
**credanonp
;
1169 register struct ufid
*ufhp
;
1172 ufhp
= (struct ufid
*)fhp
;
1173 fs
= VFSTOUFS(mp
)->um_fs
;
1174 if (ufhp
->ufid_ino
< ROOTINO
||
1175 ufhp
->ufid_ino
>= fs
->fs_ncg
* fs
->fs_ipg
)
1177 return (ufs_check_export(mp
, ufhp
, nam
, vpp
, exflagsp
, credanonp
));
1181 * Vnode pointer to File handle
1188 register struct inode
*ip
;
1189 register struct ufid
*ufhp
;
1192 ufhp
= (struct ufid
*)fhp
;
1193 ufhp
->ufid_len
= sizeof(struct ufid
);
1194 ufhp
->ufid_ino
= ip
->i_number
;
1195 ufhp
->ufid_gen
= ip
->i_gen
;
1200 * Initialize the filesystem; just use ufs_init.
1204 struct vfsconf
*vfsp
;
1207 return (ufs_init(vfsp
));
1211 * fast filesystem related variables.
1213 ffs_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
, p
)
1222 extern int doclusterread
, doclusterwrite
, doreallocblks
, doasyncfree
;
1224 /* all sysctl names at this level are terminal */
1226 return (ENOTDIR
); /* overloaded */
1229 case FFS_CLUSTERREAD
:
1230 return (sysctl_int(oldp
, oldlenp
, newp
, newlen
,
1232 case FFS_CLUSTERWRITE
:
1233 return (sysctl_int(oldp
, oldlenp
, newp
, newlen
,
1235 case FFS_REALLOCBLKS
:
1236 return (sysctl_int(oldp
, oldlenp
, newp
, newlen
,
1239 return (sysctl_int(oldp
, oldlenp
, newp
, newlen
, &doasyncfree
));
1241 return (EOPNOTSUPP
);
1247 * Write a superblock and associated information back to disk.
1250 ffs_sbupdate(mp
, waitfor
)
1251 struct ufsmount
*mp
;
1254 register struct fs
*dfs
, *fs
= mp
->um_fs
;
1255 register struct buf
*bp
;
1258 int i
, size
, error
, allerror
= 0;
1261 int rev_endian
=(mp
->um_mountp
->mnt_flag
& MNT_REVEND
);
1262 #endif /* REV_ENDIAN_FS */
1265 * First write back the summary information.
1267 blks
= howmany(fs
->fs_cssize
, fs
->fs_fsize
);
1269 for (i
= 0; i
< blks
; i
+= fs
->fs_frag
) {
1270 size
= fs
->fs_bsize
;
1271 if (i
+ fs
->fs_frag
> blks
)
1272 size
= (blks
- i
) * fs
->fs_fsize
;
1273 bp
= getblk(mp
->um_devvp
, fsbtodb(fs
, fs
->fs_csaddr
+ i
),
1274 size
, 0, 0, BLK_META
);
1275 bcopy(space
, bp
->b_data
, (u_int
)size
);
1278 byte_swap_ints((int *)bp
->b_data
, size
/ sizeof(int));
1280 #endif /* REV_ENDIAN_FS */
1281 space
= (char *)space
+ size
;
1282 if (waitfor
!= MNT_WAIT
)
1284 else if (error
= bwrite(bp
))
1288 * Now write back the superblock itself. If any errors occurred
1289 * up to this point, then fail so that the superblock avoids
1290 * being written out as clean.
1294 VOP_DEVBLOCKSIZE(mp
->um_devvp
,&devBlockSize
);
1295 bp
= getblk(mp
->um_devvp
, (SBOFF
/devBlockSize
), (int)fs
->fs_sbsize
, 0, 0, BLK_META
);
1296 bcopy((caddr_t
)fs
, bp
->b_data
, (u_int
)fs
->fs_sbsize
);
1297 /* Restore compatibility to old file systems. XXX */
1298 dfs
= (struct fs
*)bp
->b_data
; /* XXX */
1299 if (fs
->fs_postblformat
== FS_42POSTBLFMT
) /* XXX */
1300 dfs
->fs_nrpos
= -1; /* XXX */
1303 * Swapping bytes here ; so that in case
1304 * of inode format < FS_44INODEFMT appropriate
1308 byte_swap_sbout((struct fs
*)bp
->b_data
);
1310 #endif /* REV_ENDIAN_FS */
1311 if (fs
->fs_inodefmt
< FS_44INODEFMT
) { /* XXX */
1312 int32_t *lp
, tmp
; /* XXX */
1314 lp
= (int32_t *)&dfs
->fs_qbmask
; /* XXX */
1315 tmp
= lp
[4]; /* XXX */
1316 for (i
= 4; i
> 0; i
--) /* XXX */
1317 lp
[i
] = lp
[i
-1]; /* XXX */
1318 lp
[0] = tmp
; /* XXX */
1321 /* Note that dfs is already swapped so swap the filesize
1325 dfs
->fs_maxfilesize
= NXSwapLongLong(mp
->um_savedmaxfilesize
); /* XXX */
1327 #endif /* REV_ENDIAN_FS */
1328 dfs
->fs_maxfilesize
= mp
->um_savedmaxfilesize
; /* XXX */
1331 #endif /* REV_ENDIAN_FS */
1332 if (waitfor
!= MNT_WAIT
)
1334 else if (error
= bwrite(bp
))