ext2_vfsops.c revision 39028
112115Sdyson/* 212115Sdyson * modified for EXT2FS support in Lites 1.1 312115Sdyson * 412115Sdyson * Aug 1995, Godmar Back (gback@cs.utah.edu) 512115Sdyson * University of Utah, Department of Computer Science 612115Sdyson */ 712115Sdyson/* 812115Sdyson * Copyright (c) 1989, 1991, 1993, 1994 912115Sdyson * The Regents of the University of California. All rights reserved. 1012115Sdyson * 1112115Sdyson * Redistribution and use in source and binary forms, with or without 1212115Sdyson * modification, are permitted provided that the following conditions 1312115Sdyson * are met: 1412115Sdyson * 1. Redistributions of source code must retain the above copyright 1512115Sdyson * notice, this list of conditions and the following disclaimer. 1612115Sdyson * 2. Redistributions in binary form must reproduce the above copyright 1712115Sdyson * notice, this list of conditions and the following disclaimer in the 1812115Sdyson * documentation and/or other materials provided with the distribution. 1912115Sdyson * 3. All advertising materials mentioning features or use of this software 2012115Sdyson * must display the following acknowledgement: 2112115Sdyson * This product includes software developed by the University of 2212115Sdyson * California, Berkeley and its contributors. 2312115Sdyson * 4. Neither the name of the University nor the names of its contributors 2412115Sdyson * may be used to endorse or promote products derived from this software 2512115Sdyson * without specific prior written permission. 2612115Sdyson * 2712115Sdyson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2812115Sdyson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2912115Sdyson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3012115Sdyson * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3112115Sdyson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3212115Sdyson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3312115Sdyson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3412115Sdyson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3512115Sdyson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3612115Sdyson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3712115Sdyson * SUCH DAMAGE. 3812115Sdyson * 3912115Sdyson * @(#)ffs_vfsops.c 8.8 (Berkeley) 4/18/94 4012115Sdyson */ 4112115Sdyson 4213260Swollman#include "opt_quota.h" 4312115Sdyson 4412115Sdyson#include <sys/param.h> 4512115Sdyson#include <sys/systm.h> 4612115Sdyson#include <sys/namei.h> 4712115Sdyson#include <sys/proc.h> 4812115Sdyson#include <sys/kernel.h> 4912115Sdyson#include <sys/vnode.h> 5012115Sdyson#include <sys/mount.h> 5112115Sdyson#include <sys/buf.h> 5229906Skato#include <sys/conf.h> 5324131Sbde#include <sys/fcntl.h> 5412115Sdyson#include <sys/disklabel.h> 5512115Sdyson#include <sys/malloc.h> 5612115Sdyson#include <sys/stat.h> 5712115Sdyson 5812115Sdyson#include <miscfs/specfs/specdev.h> 5912115Sdyson 6012115Sdyson#include <ufs/ufs/quota.h> 6112115Sdyson#include <ufs/ufs/ufsmount.h> 6212115Sdyson#include <ufs/ufs/inode.h> 6312115Sdyson#include <ufs/ufs/ufs_extern.h> 6412115Sdyson 6512115Sdyson#include <gnu/ext2fs/fs.h> 6612115Sdyson#include <gnu/ext2fs/ext2_extern.h> 6712115Sdyson#include <gnu/ext2fs/ext2_fs.h> 6812115Sdyson#include <gnu/ext2fs/ext2_fs_sb.h> 6912115Sdyson 7028270Swollmanstatic int ext2_fhtovp __P((struct mount *, struct fid *, struct sockaddr *, 7112911Sphk struct vnode **, int *, struct ucred **)); 7212911Sphkstatic int ext2_flushfiles __P((struct mount *mp, int flags, struct proc *p)); 7312911Sphkstatic int ext2_mount __P((struct mount *, 7412911Sphk char *, caddr_t, struct nameidata *, struct proc *)); 7512911Sphkstatic int ext2_mountfs __P((struct vnode *, struct mount *, struct proc *)); 7612911Sphkstatic int ext2_reload __P((struct mount *mountp, struct ucred *cred, 7712911Sphk struct proc *p)); 7812911Sphkstatic int ext2_sbupdate __P((struct ufsmount *, int)); 7912911Sphkstatic int ext2_statfs __P((struct mount *, struct statfs *, struct proc *)); 8012911Sphkstatic int ext2_sync __P((struct mount *, int, struct ucred *, struct proc *)); 8112911Sphkstatic int ext2_unmount __P((struct mount *, int, struct proc *)); 8212911Sphkstatic int ext2_vget __P((struct mount *, ino_t, struct vnode **)); 8312911Sphkstatic int ext2_vptofh __P((struct vnode *, struct fid *)); 8412115Sdyson 8531315Sbdestatic MALLOC_DEFINE(M_EXT2NODE, "EXT2 node", "EXT2 vnode private part"); 8630280Sphk 8712911Sphkstatic struct vfsops ext2fs_vfsops = { 8812115Sdyson ext2_mount, 8912115Sdyson ufs_start, /* empty function */ 9012115Sdyson ext2_unmount, 9112115Sdyson ufs_root, /* root inode via vget */ 9212115Sdyson ufs_quotactl, /* does operations associated with quotas */ 9312115Sdyson ext2_statfs, 9412115Sdyson ext2_sync, 9512115Sdyson ext2_vget, 9612115Sdyson ext2_fhtovp, 9712115Sdyson ext2_vptofh, 9812115Sdyson ext2_init, 9912115Sdyson}; 10012115Sdyson 10138909SbdeVFS_SET(ext2fs_vfsops, ext2fs, 0); 10212115Sdyson#define bsd_malloc malloc 10312115Sdyson#define bsd_free free 10412115Sdyson 10512911Sphkstatic int ext2fs_inode_hash_lock; 10612115Sdyson 10716322Sgpalmerstatic int compute_sb_data __P((struct vnode * devvp, 10816322Sgpalmer struct ext2_super_block * es, 10916322Sgpalmer struct ext2_sb_info * fs)); 11016322Sgpalmer 11116322Sgpalmer#ifdef notyet 11216322Sgpalmerstatic int ext2_mountroot __P((void)); 11316322Sgpalmer 11412115Sdyson/* 11516322Sgpalmer * Called by main() when ext2fs is going to be mounted as root. 11612115Sdyson * 11712115Sdyson * Name is updated by mount(8) after booting. 11812115Sdyson */ 11912115Sdyson#define ROOTNAME "root_device" 12012115Sdyson 12112911Sphkstatic int 12212115Sdysonext2_mountroot() 12312115Sdyson{ 12412115Sdyson register struct ext2_sb_info *fs; 12512115Sdyson register struct mount *mp; 12612115Sdyson struct proc *p = curproc; 12712115Sdyson struct ufsmount *ump; 12812115Sdyson u_int size; 12912115Sdyson int error; 13012115Sdyson 13129208Sbde if ((error = bdevvp(rootdev, &rootvp))) { 13229208Sbde printf("ext2_mountroot: can't find rootvp"); 13329208Sbde return (error); 13429208Sbde } 13512115Sdyson mp = bsd_malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK); 13612115Sdyson bzero((char *)mp, (u_long)sizeof(struct mount)); 13712115Sdyson mp->mnt_op = &ext2fs_vfsops; 13812115Sdyson mp->mnt_flag = MNT_RDONLY; 13929888Skato if (bdevsw[major(rootdev)]->d_flags & D_NOCLUSTERR) 14029888Skato mp->mnt_flag |= MNT_NOCLUSTERR; 14129888Skato if (bdevsw[major(rootdev)]->d_flags & D_NOCLUSTERW) 14229888Skato mp->mnt_flag |= MNT_NOCLUSTERW; 14312115Sdyson if (error = ext2_mountfs(rootvp, mp, p)) { 14412115Sdyson bsd_free(mp, M_MOUNT); 14512115Sdyson return (error); 14612115Sdyson } 14712115Sdyson if (error = vfs_lock(mp)) { 14812115Sdyson (void)ext2_unmount(mp, 0, p); 14912115Sdyson bsd_free(mp, M_MOUNT); 15012115Sdyson return (error); 15112115Sdyson } 15230469Sjulian CIRCLEQ_INSERT_HEAD(&mountlist, mp, mnt_list); 15312115Sdyson mp->mnt_flag |= MNT_ROOTFS; 15412115Sdyson mp->mnt_vnodecovered = NULLVP; 15512115Sdyson ump = VFSTOUFS(mp); 15612115Sdyson fs = ump->um_e2fs; 15712115Sdyson bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt)); 15812115Sdyson fs->fs_fsmnt[0] = '/'; 15912115Sdyson bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname, 16012115Sdyson MNAMELEN); 16112115Sdyson (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 16212115Sdyson &size); 16312115Sdyson bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 16412115Sdyson (void)ext2_statfs(mp, &mp->mnt_stat, p); 16512115Sdyson vfs_unlock(mp); 16612115Sdyson inittodr(fs->s_es->s_wtime); /* this helps to set the time */ 16712115Sdyson return (0); 16812115Sdyson} 16916322Sgpalmer#endif 17012115Sdyson 17112115Sdyson/* 17212115Sdyson * VFS Operations. 17312115Sdyson * 17412115Sdyson * mount system call 17512115Sdyson */ 17612911Sphkstatic int 17712115Sdysonext2_mount(mp, path, data, ndp, p) 17812115Sdyson register struct mount *mp; 17912115Sdyson char *path; 18012115Sdyson caddr_t data; /* this is actually a (struct ufs_args *) */ 18112115Sdyson struct nameidata *ndp; 18212115Sdyson struct proc *p; 18312115Sdyson{ 18412115Sdyson struct vnode *devvp; 18512115Sdyson struct ufs_args args; 18612115Sdyson struct ufsmount *ump = 0; 18712115Sdyson register struct ext2_sb_info *fs; 18812115Sdyson u_int size; 18912115Sdyson int error, flags; 19039028Sbde mode_t accessmode; 19112115Sdyson 19212115Sdyson if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args))) 19312115Sdyson return (error); 19412115Sdyson /* 19512115Sdyson * If updating, check whether changing from read-only to 19612115Sdyson * read/write; if there is no device name, that's all we do. 19729888Skato * Disallow clearing MNT_NOCLUSTERR and MNT_NOCLUSTERW flags, 19829888Skato * if block device requests. 19912115Sdyson */ 20012115Sdyson if (mp->mnt_flag & MNT_UPDATE) { 20112115Sdyson ump = VFSTOUFS(mp); 20212115Sdyson fs = ump->um_e2fs; 20312115Sdyson error = 0; 20429888Skato if (bdevsw[major(ump->um_dev)]->d_flags & D_NOCLUSTERR) 20529888Skato mp->mnt_flag |= MNT_NOCLUSTERR; 20629888Skato if (bdevsw[major(ump->um_dev)]->d_flags & D_NOCLUSTERW) 20729888Skato mp->mnt_flag |= MNT_NOCLUSTERW; 20812115Sdyson if (fs->s_rd_only == 0 && (mp->mnt_flag & MNT_RDONLY)) { 20912115Sdyson flags = WRITECLOSE; 21012115Sdyson if (mp->mnt_flag & MNT_FORCE) 21112115Sdyson flags |= FORCECLOSE; 21222521Sdyson if (vfs_busy(mp, LK_NOWAIT, 0, p)) 21312115Sdyson return (EBUSY); 21412115Sdyson error = ext2_flushfiles(mp, flags, p); 21522521Sdyson vfs_unbusy(mp, p); 21612115Sdyson } 21712115Sdyson if (!error && (mp->mnt_flag & MNT_RELOAD)) 21812115Sdyson error = ext2_reload(mp, ndp->ni_cnd.cn_cred, p); 21912115Sdyson if (error) 22012115Sdyson return (error); 22139028Sbde if (fs->s_rd_only && (mp->mnt_kern_flag & MNTK_WANTRDWR)) { 22239028Sbde /* 22339028Sbde * If upgrade to read-write by non-root, then verify 22439028Sbde * that user has necessary permissions on the device. 22539028Sbde */ 22639028Sbde if (p->p_ucred->cr_uid != 0) { 22739028Sbde devvp = ump->um_devvp; 22839028Sbde vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 22939028Sbde if (error = VOP_ACCESS(devvp, VREAD | VWRITE, 23039028Sbde p->p_ucred, p)) { 23139028Sbde VOP_UNLOCK(devvp, 0, p); 23239028Sbde return (error); 23339028Sbde } 23439028Sbde VOP_UNLOCK(devvp, 0, p); 23539028Sbde } 23639028Sbde 23712115Sdyson fs->s_rd_only = 0; 23839028Sbde } 23912115Sdyson if (fs->s_rd_only == 0) { 24012115Sdyson /* don't say it's clean */ 24112115Sdyson fs->s_es->s_state &= ~EXT2_VALID_FS; 24212115Sdyson ext2_sbupdate(ump, MNT_WAIT); 24312115Sdyson } 24412115Sdyson if (args.fspec == 0) { 24512115Sdyson /* 24612115Sdyson * Process export requests. 24712115Sdyson */ 24812115Sdyson return (vfs_export(mp, &ump->um_export, &args.export)); 24912115Sdyson } 25012115Sdyson } 25112115Sdyson /* 25212115Sdyson * Not an update, or updating the name: look up the name 25312115Sdyson * and verify that it refers to a sensible block device. 25412115Sdyson */ 25512115Sdyson NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); 25612115Sdyson if (error = namei(ndp)) 25712115Sdyson return (error); 25812115Sdyson devvp = ndp->ni_vp; 25912115Sdyson 26012115Sdyson if (devvp->v_type != VBLK) { 26112115Sdyson vrele(devvp); 26212115Sdyson return (ENOTBLK); 26312115Sdyson } 26412115Sdyson if (major(devvp->v_rdev) >= nblkdev) { 26512115Sdyson vrele(devvp); 26612115Sdyson return (ENXIO); 26712115Sdyson } 26839028Sbde 26939028Sbde /* 27039028Sbde * If mount by non-root, then verify that user has necessary 27139028Sbde * permissions on the device. 27239028Sbde */ 27339028Sbde if (p->p_ucred->cr_uid != 0) { 27439028Sbde accessmode = VREAD; 27539028Sbde if ((mp->mnt_flag & MNT_RDONLY) == 0) 27639028Sbde accessmode |= VWRITE; 27739028Sbde vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 27839028Sbde if (error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p)) { 27939028Sbde vput(devvp); 28039028Sbde return (error); 28139028Sbde } 28239028Sbde VOP_UNLOCK(devvp, 0, p); 28339028Sbde } 28439028Sbde 28529888Skato if ((mp->mnt_flag & MNT_UPDATE) == 0) { 28629888Skato if (bdevsw[major(devvp->v_rdev)]->d_flags & D_NOCLUSTERR) 28729888Skato mp->mnt_flag |= MNT_NOCLUSTERR; 28829888Skato if (bdevsw[major(devvp->v_rdev)]->d_flags & D_NOCLUSTERW) 28929888Skato mp->mnt_flag |= MNT_NOCLUSTERW; 29012115Sdyson error = ext2_mountfs(devvp, mp, p); 29129888Skato } else { 29212115Sdyson if (devvp != ump->um_devvp) 29312115Sdyson error = EINVAL; /* needs translation */ 29412115Sdyson else 29512115Sdyson vrele(devvp); 29612115Sdyson } 29712115Sdyson if (error) { 29812115Sdyson vrele(devvp); 29912115Sdyson return (error); 30012115Sdyson } 30112115Sdyson ump = VFSTOUFS(mp); 30212115Sdyson fs = ump->um_e2fs; 30312115Sdyson (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size); 30412115Sdyson bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size); 30512115Sdyson bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname, 30612115Sdyson MNAMELEN); 30712115Sdyson (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 30812115Sdyson &size); 30912115Sdyson bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 31012115Sdyson (void)ext2_statfs(mp, &mp->mnt_stat, p); 31112115Sdyson return (0); 31212115Sdyson} 31312115Sdyson 31412115Sdyson/* 31512115Sdyson * checks that the data in the descriptor blocks make sense 31612115Sdyson * this is taken from ext2/super.c 31712115Sdyson */ 31812115Sdysonstatic int ext2_check_descriptors (struct ext2_sb_info * sb) 31912115Sdyson{ 32012115Sdyson int i; 32112115Sdyson int desc_block = 0; 32212115Sdyson unsigned long block = sb->s_es->s_first_data_block; 32312115Sdyson struct ext2_group_desc * gdp = NULL; 32412115Sdyson 32512115Sdyson /* ext2_debug ("Checking group descriptors"); */ 32612115Sdyson 32712115Sdyson for (i = 0; i < sb->s_groups_count; i++) 32812115Sdyson { 32912115Sdyson /* examine next descriptor block */ 33012115Sdyson if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0) 33112115Sdyson gdp = (struct ext2_group_desc *) 33212115Sdyson sb->s_group_desc[desc_block++]->b_data; 33312115Sdyson if (gdp->bg_block_bitmap < block || 33412115Sdyson gdp->bg_block_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb)) 33512115Sdyson { 33612115Sdyson printf ("ext2_check_descriptors: " 33712115Sdyson "Block bitmap for group %d" 33812115Sdyson " not in group (block %lu)!", 33912115Sdyson i, (unsigned long) gdp->bg_block_bitmap); 34012115Sdyson return 0; 34112115Sdyson } 34212115Sdyson if (gdp->bg_inode_bitmap < block || 34312115Sdyson gdp->bg_inode_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb)) 34412115Sdyson { 34512115Sdyson printf ("ext2_check_descriptors: " 34612115Sdyson "Inode bitmap for group %d" 34712115Sdyson " not in group (block %lu)!", 34812115Sdyson i, (unsigned long) gdp->bg_inode_bitmap); 34912115Sdyson return 0; 35012115Sdyson } 35112115Sdyson if (gdp->bg_inode_table < block || 35212115Sdyson gdp->bg_inode_table + sb->s_itb_per_group >= 35312115Sdyson block + EXT2_BLOCKS_PER_GROUP(sb)) 35412115Sdyson { 35512115Sdyson printf ("ext2_check_descriptors: " 35612115Sdyson "Inode table for group %d" 35712115Sdyson " not in group (block %lu)!", 35812115Sdyson i, (unsigned long) gdp->bg_inode_table); 35912115Sdyson return 0; 36012115Sdyson } 36112115Sdyson block += EXT2_BLOCKS_PER_GROUP(sb); 36212115Sdyson gdp++; 36312115Sdyson } 36412115Sdyson return 1; 36512115Sdyson} 36612115Sdyson 36712115Sdyson/* 36812115Sdyson * this computes the fields of the ext2_sb_info structure from the 36912115Sdyson * data in the ext2_super_block structure read in 37012115Sdyson */ 37112115Sdysonstatic int compute_sb_data(devvp, es, fs) 37212115Sdyson struct vnode * devvp; 37312115Sdyson struct ext2_super_block * es; 37412115Sdyson struct ext2_sb_info * fs; 37512115Sdyson{ 37612115Sdyson int db_count, error; 37712115Sdyson int i, j; 37812115Sdyson int logic_sb_block = 1; /* XXX for now */ 37912115Sdyson 38012115Sdyson#if 1 38112115Sdyson#define V(v) 38212115Sdyson#else 38312115Sdyson#define V(v) printf(#v"= %d\n", fs->v); 38412115Sdyson#endif 38512115Sdyson 38612115Sdyson fs->s_blocksize = EXT2_MIN_BLOCK_SIZE << es->s_log_block_size; 38712115Sdyson V(s_blocksize) 38812115Sdyson fs->s_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->s_log_block_size; 38912115Sdyson V(s_bshift) 39012115Sdyson fs->s_fsbtodb = es->s_log_block_size + 1; 39112115Sdyson V(s_fsbtodb) 39212115Sdyson fs->s_qbmask = fs->s_blocksize - 1; 39312115Sdyson V(s_bmask) 39412115Sdyson fs->s_blocksize_bits = EXT2_BLOCK_SIZE_BITS(es); 39512115Sdyson V(s_blocksize_bits) 39612115Sdyson fs->s_frag_size = EXT2_MIN_FRAG_SIZE << es->s_log_frag_size; 39712115Sdyson V(s_frag_size) 39812115Sdyson if (fs->s_frag_size) 39912115Sdyson fs->s_frags_per_block = fs->s_blocksize / fs->s_frag_size; 40012115Sdyson V(s_frags_per_block) 40112115Sdyson fs->s_blocks_per_group = es->s_blocks_per_group; 40212115Sdyson V(s_blocks_per_group) 40312115Sdyson fs->s_frags_per_group = es->s_frags_per_group; 40412115Sdyson V(s_frags_per_group) 40512115Sdyson fs->s_inodes_per_group = es->s_inodes_per_group; 40612115Sdyson V(s_inodes_per_group) 40712115Sdyson fs->s_inodes_per_block = fs->s_blocksize / EXT2_INODE_SIZE; 40812115Sdyson V(s_inodes_per_block) 40912115Sdyson fs->s_itb_per_group = fs->s_inodes_per_group /fs->s_inodes_per_block; 41012115Sdyson V(s_itb_per_group) 41112115Sdyson fs->s_desc_per_block = fs->s_blocksize / sizeof (struct ext2_group_desc); 41212115Sdyson V(s_desc_per_block) 41312115Sdyson /* s_resuid / s_resgid ? */ 41412115Sdyson fs->s_groups_count = (es->s_blocks_count - 41512115Sdyson es->s_first_data_block + 41612115Sdyson EXT2_BLOCKS_PER_GROUP(fs) - 1) / 41712115Sdyson EXT2_BLOCKS_PER_GROUP(fs); 41812115Sdyson V(s_groups_count) 41912115Sdyson db_count = (fs->s_groups_count + EXT2_DESC_PER_BLOCK(fs) - 1) / 42012115Sdyson EXT2_DESC_PER_BLOCK(fs); 42112115Sdyson fs->s_db_per_group = db_count; 42212115Sdyson V(s_db_per_group) 42312115Sdyson 42412115Sdyson fs->s_group_desc = bsd_malloc(db_count * sizeof (struct buf *), 42512115Sdyson M_UFSMNT, M_WAITOK); 42612115Sdyson 42712115Sdyson /* adjust logic_sb_block */ 42812115Sdyson if(fs->s_blocksize > SBSIZE) 42912115Sdyson /* Godmar thinks: if the blocksize is greater than 1024, then 43012115Sdyson the superblock is logically part of block zero. 43112115Sdyson */ 43212115Sdyson logic_sb_block = 0; 43312115Sdyson 43412115Sdyson for (i = 0; i < db_count; i++) { 43512115Sdyson error = bread(devvp , fsbtodb(fs, logic_sb_block + i + 1), 43612115Sdyson fs->s_blocksize, NOCRED, &fs->s_group_desc[i]); 43712115Sdyson if(error) { 43812115Sdyson for (j = 0; j < i; j++) 43912115Sdyson brelse(fs->s_group_desc[j]); 44012115Sdyson bsd_free(fs->s_group_desc, M_UFSMNT); 44112115Sdyson printf("EXT2-fs: unable to read group descriptors (%d)\n", error); 44212115Sdyson return EIO; 44312115Sdyson } 44427881Sdyson /* Set the B_LOCKED flag on the buffer, then brelse() it */ 44527881Sdyson LCK_BUF(fs->s_group_desc[i]) 44612115Sdyson } 44712115Sdyson if(!ext2_check_descriptors(fs)) { 44812115Sdyson for (j = 0; j < db_count; j++) 44927881Sdyson ULCK_BUF(fs->s_group_desc[j]) 45012115Sdyson bsd_free(fs->s_group_desc, M_UFSMNT); 45112115Sdyson printf("EXT2-fs: (ext2_check_descriptors failure) " 45212115Sdyson "unable to read group descriptors\n"); 45312115Sdyson return EIO; 45412115Sdyson } 45512115Sdyson 45612115Sdyson for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) { 45712115Sdyson fs->s_inode_bitmap_number[i] = 0; 45812115Sdyson fs->s_inode_bitmap[i] = NULL; 45912115Sdyson fs->s_block_bitmap_number[i] = 0; 46012115Sdyson fs->s_block_bitmap[i] = NULL; 46112115Sdyson } 46212115Sdyson fs->s_loaded_inode_bitmaps = 0; 46312115Sdyson fs->s_loaded_block_bitmaps = 0; 46412115Sdyson return 0; 46512115Sdyson} 46612115Sdyson 46712115Sdyson/* 46812115Sdyson * Reload all incore data for a filesystem (used after running fsck on 46912115Sdyson * the root filesystem and finding things to fix). The filesystem must 47012115Sdyson * be mounted read-only. 47112115Sdyson * 47212115Sdyson * Things to do to update the mount: 47312115Sdyson * 1) invalidate all cached meta-data. 47412115Sdyson * 2) re-read superblock from disk. 47512115Sdyson * 3) re-read summary information from disk. 47612115Sdyson * 4) invalidate all inactive vnodes. 47712115Sdyson * 5) invalidate all cached file data. 47812115Sdyson * 6) re-read inode data for all active vnodes. 47912115Sdyson */ 48012911Sphkstatic int 48112115Sdysonext2_reload(mountp, cred, p) 48212115Sdyson register struct mount *mountp; 48312115Sdyson struct ucred *cred; 48412115Sdyson struct proc *p; 48512115Sdyson{ 48612115Sdyson register struct vnode *vp, *nvp, *devvp; 48712115Sdyson struct inode *ip; 48812115Sdyson struct buf *bp; 48912115Sdyson struct ext2_super_block * es; 49012115Sdyson struct ext2_sb_info *fs; 49112147Sdyson int error; 49212115Sdyson 49312115Sdyson if ((mountp->mnt_flag & MNT_RDONLY) == 0) 49412115Sdyson return (EINVAL); 49512115Sdyson /* 49612115Sdyson * Step 1: invalidate all cached meta-data. 49712115Sdyson */ 49812115Sdyson devvp = VFSTOUFS(mountp)->um_devvp; 49912115Sdyson if (vinvalbuf(devvp, 0, cred, p, 0, 0)) 50012115Sdyson panic("ext2_reload: dirty1"); 50112115Sdyson /* 50212115Sdyson * Step 2: re-read superblock from disk. 50312115Sdyson * constants have been adjusted for ext2 50412115Sdyson */ 50512115Sdyson if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) 50612115Sdyson return (error); 50712115Sdyson es = (struct ext2_super_block *)bp->b_data; 50812115Sdyson if (es->s_magic != EXT2_SUPER_MAGIC) { 50912115Sdyson if(es->s_magic == EXT2_PRE_02B_MAGIC) 51012115Sdyson printf("This filesystem bears the magic number of a pre " 51112115Sdyson "0.2b version of ext2. This is not supported by " 51212115Sdyson "Lites.\n"); 51312115Sdyson else 51412115Sdyson printf("Wrong magic number: %x (expected %x for ext2 fs\n", 51512115Sdyson es->s_magic, EXT2_SUPER_MAGIC); 51612115Sdyson brelse(bp); 51712115Sdyson return (EIO); /* XXX needs translation */ 51812115Sdyson } 51912115Sdyson fs = VFSTOUFS(mountp)->um_e2fs; 52012115Sdyson bcopy(bp->b_data, fs->s_es, sizeof(struct ext2_super_block)); 52112115Sdyson 52212115Sdyson if(error = compute_sb_data(devvp, es, fs)) { 52312115Sdyson brelse(bp); 52412115Sdyson return error; 52512115Sdyson } 52612115Sdyson#ifdef UNKLAR 52712115Sdyson if (fs->fs_sbsize < SBSIZE) 52812115Sdyson bp->b_flags |= B_INVAL; 52912115Sdyson#endif 53012115Sdyson brelse(bp); 53112115Sdyson 53212115Sdysonloop: 53312115Sdyson for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { 53412115Sdyson nvp = vp->v_mntvnodes.le_next; 53512115Sdyson /* 53612115Sdyson * Step 4: invalidate all inactive vnodes. 53712115Sdyson */ 53812115Sdyson if (vp->v_usecount == 0) { 53912115Sdyson vgone(vp); 54012115Sdyson continue; 54112115Sdyson } 54212115Sdyson /* 54312115Sdyson * Step 5: invalidate all cached file data. 54412115Sdyson */ 54522521Sdyson if (vget(vp, LK_EXCLUSIVE, p)) 54612115Sdyson goto loop; 54712115Sdyson if (vinvalbuf(vp, 0, cred, p, 0, 0)) 54812115Sdyson panic("ext2_reload: dirty2"); 54912115Sdyson /* 55012115Sdyson * Step 6: re-read inode data for all active vnodes. 55112115Sdyson */ 55212115Sdyson ip = VTOI(vp); 55312115Sdyson if (error = 55412115Sdyson bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 55512115Sdyson (int)fs->s_blocksize, NOCRED, &bp)) { 55612115Sdyson vput(vp); 55712115Sdyson return (error); 55812115Sdyson } 55912115Sdyson ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + 56012115Sdyson EXT2_INODE_SIZE * ino_to_fsbo(fs, ip->i_number)), 56112115Sdyson &ip->i_din); 56212115Sdyson brelse(bp); 56312115Sdyson vput(vp); 56412115Sdyson if (vp->v_mount != mountp) 56512115Sdyson goto loop; 56612115Sdyson } 56712115Sdyson return (0); 56812115Sdyson} 56912115Sdyson 57012115Sdyson/* 57112115Sdyson * Common code for mount and mountroot 57212115Sdyson */ 57312911Sphkstatic int 57412115Sdysonext2_mountfs(devvp, mp, p) 57512115Sdyson register struct vnode *devvp; 57612115Sdyson struct mount *mp; 57712115Sdyson struct proc *p; 57812115Sdyson{ 57912115Sdyson register struct ufsmount *ump; 58012115Sdyson struct buf *bp; 58112115Sdyson register struct ext2_sb_info *fs; 58212115Sdyson struct ext2_super_block * es; 58312115Sdyson dev_t dev = devvp->v_rdev; 58412115Sdyson struct partinfo dpart; 58512115Sdyson int havepart = 0; 58612115Sdyson int error, i, size; 58712115Sdyson int ronly; 58812115Sdyson 58912115Sdyson /* 59012115Sdyson * Disallow multiple mounts of the same device. 59112115Sdyson * Disallow mounting of a device that is currently in use 59212115Sdyson * (except for root, which might share swap device for miniroot). 59312115Sdyson * Flush out any old buffers remaining from a previous use. 59412115Sdyson */ 59512115Sdyson if (error = vfs_mountedon(devvp)) 59612115Sdyson return (error); 59712115Sdyson if (vcount(devvp) > 1 && devvp != rootvp) 59812115Sdyson return (EBUSY); 59912115Sdyson if (error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0)) 60012115Sdyson return (error); 60112115Sdyson#ifdef READONLY 60212115Sdyson/* turn on this to force it to be read-only */ 60312115Sdyson mp->mnt_flag |= MNT_RDONLY; 60412115Sdyson#endif 60512115Sdyson 60612115Sdyson ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 60712115Sdyson if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p)) 60812115Sdyson return (error); 60912115Sdyson if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0) 61012115Sdyson size = DEV_BSIZE; 61112115Sdyson else { 61212115Sdyson havepart = 1; 61312115Sdyson size = dpart.disklab->d_secsize; 61412115Sdyson } 61512115Sdyson 61612115Sdyson bp = NULL; 61712115Sdyson ump = NULL; 61812115Sdyson if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) 61912115Sdyson goto out; 62012115Sdyson es = (struct ext2_super_block *)bp->b_data; 62112115Sdyson if (es->s_magic != EXT2_SUPER_MAGIC) { 62212115Sdyson if(es->s_magic == EXT2_PRE_02B_MAGIC) 62312115Sdyson printf("This filesystem bears the magic number of a pre " 62412115Sdyson "0.2b version of ext2. This is not supported by " 62512115Sdyson "Lites.\n"); 62612115Sdyson else 62712115Sdyson printf("Wrong magic number: %x (expected %x for EXT2FS)\n", 62812115Sdyson es->s_magic, EXT2_SUPER_MAGIC); 62912115Sdyson error = EINVAL; /* XXX needs translation */ 63012115Sdyson goto out; 63112115Sdyson } 63212115Sdyson ump = bsd_malloc(sizeof *ump, M_UFSMNT, M_WAITOK); 63312115Sdyson bzero((caddr_t)ump, sizeof *ump); 63430280Sphk ump->um_malloctype = M_EXT2NODE; 63530474Sphk ump->um_blkatoff = ext2_blkatoff; 63630474Sphk ump->um_truncate = ext2_truncate; 63730492Sphk ump->um_update = ext2_update; 63830474Sphk ump->um_valloc = ext2_valloc; 63930474Sphk ump->um_vfree = ext2_vfree; 64012115Sdyson /* I don't know whether this is the right strategy. Note that 64112115Sdyson we dynamically allocate both a ext2_sb_info and a ext2_super_block 64212115Sdyson while Linux keeps the super block in a locked buffer 64312115Sdyson */ 64412115Sdyson ump->um_e2fs = bsd_malloc(sizeof(struct ext2_sb_info), 64512115Sdyson M_UFSMNT, M_WAITOK); 64612115Sdyson ump->um_e2fs->s_es = bsd_malloc(sizeof(struct ext2_super_block), 64712115Sdyson M_UFSMNT, M_WAITOK); 64812115Sdyson bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block)); 64912115Sdyson if(error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs)) { 65012115Sdyson brelse(bp); 65112115Sdyson return error; 65212115Sdyson } 65312115Sdyson brelse(bp); 65412115Sdyson bp = NULL; 65512115Sdyson fs = ump->um_e2fs; 65612115Sdyson fs->s_rd_only = ronly; /* ronly is set according to mnt_flags */ 65712115Sdyson if (!(fs->s_es->s_state & EXT2_VALID_FS)) { 65812115Sdyson printf("WARNING: %s was not properly dismounted\n", 65912115Sdyson fs->fs_fsmnt); 66012115Sdyson } 66112115Sdyson /* if the fs is not mounted read-only, make sure the super block is 66212115Sdyson always written back on a sync() 66312115Sdyson */ 66412115Sdyson if (ronly == 0) { 66512115Sdyson fs->s_dirt = 1; /* mark it modified */ 66612115Sdyson fs->s_es->s_state &= ~EXT2_VALID_FS; /* set fs invalid */ 66712115Sdyson } 66812115Sdyson mp->mnt_data = (qaddr_t)ump; 66912115Sdyson mp->mnt_stat.f_fsid.val[0] = (long)dev; 67038909Sbde mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 67112115Sdyson mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN; 67212115Sdyson mp->mnt_flag |= MNT_LOCAL; 67312115Sdyson ump->um_mountp = mp; 67412115Sdyson ump->um_dev = dev; 67512115Sdyson ump->um_devvp = devvp; 67612115Sdyson /* setting those two parameters allows us to use 67712115Sdyson ufs_bmap w/o changse ! 67812115Sdyson */ 67912115Sdyson ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs); 68012115Sdyson ump->um_bptrtodb = fs->s_es->s_log_block_size + 1; 68112115Sdyson ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs); 68212115Sdyson for (i = 0; i < MAXQUOTAS; i++) 68312115Sdyson ump->um_quotas[i] = NULLVP; 68434430Seivind devvp->v_specmountpoint = mp; 68534430Seivind if (ronly == 0) 68634430Seivind ext2_sbupdate(ump, MNT_WAIT); 68712115Sdyson return (0); 68812115Sdysonout: 68912115Sdyson if (bp) 69012115Sdyson brelse(bp); 69112115Sdyson (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p); 69212115Sdyson if (ump) { 69312115Sdyson bsd_free(ump->um_fs, M_UFSMNT); 69412115Sdyson bsd_free(ump, M_UFSMNT); 69512115Sdyson mp->mnt_data = (qaddr_t)0; 69612115Sdyson } 69712115Sdyson return (error); 69812115Sdyson} 69912115Sdyson 70012115Sdyson/* 70112115Sdyson * unmount system call 70212115Sdyson */ 70312911Sphkstatic int 70412115Sdysonext2_unmount(mp, mntflags, p) 70512115Sdyson struct mount *mp; 70612115Sdyson int mntflags; 70712115Sdyson struct proc *p; 70812115Sdyson{ 70912115Sdyson register struct ufsmount *ump; 71012115Sdyson register struct ext2_sb_info *fs; 71112115Sdyson int error, flags, ronly, i; 71212115Sdyson 71312115Sdyson flags = 0; 71412115Sdyson if (mntflags & MNT_FORCE) { 71512115Sdyson if (mp->mnt_flag & MNT_ROOTFS) 71612115Sdyson return (EINVAL); 71712115Sdyson flags |= FORCECLOSE; 71812115Sdyson } 71912115Sdyson if (error = ext2_flushfiles(mp, flags, p)) 72012115Sdyson return (error); 72112115Sdyson ump = VFSTOUFS(mp); 72212115Sdyson fs = ump->um_e2fs; 72312115Sdyson ronly = fs->s_rd_only; 72412115Sdyson if (!ronly) { 72512115Sdyson fs->s_es->s_state |= EXT2_VALID_FS; /* was fs_clean = 1 */ 72612115Sdyson ext2_sbupdate(ump, MNT_WAIT); 72712115Sdyson } 72827881Sdyson 72912115Sdyson /* release buffers containing group descriptors */ 73012115Sdyson for(i = 0; i < fs->s_db_per_group; i++) 73127881Sdyson ULCK_BUF(fs->s_group_desc[i]) 73227881Sdyson 73312115Sdyson /* release cached inode/block bitmaps */ 73412115Sdyson for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) 73512115Sdyson if (fs->s_inode_bitmap[i]) 73627881Sdyson ULCK_BUF(fs->s_inode_bitmap[i]) 73727881Sdyson 73812115Sdyson for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) 73912115Sdyson if (fs->s_block_bitmap[i]) 74027881Sdyson ULCK_BUF(fs->s_block_bitmap[i]) 74112115Sdyson 74234430Seivind ump->um_devvp->v_specmountpoint = NULL; 74312115Sdyson error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE, 74412115Sdyson NOCRED, p); 74512115Sdyson vrele(ump->um_devvp); 74612115Sdyson bsd_free(fs->s_es, M_UFSMNT); 74712115Sdyson bsd_free(fs, M_UFSMNT); 74812115Sdyson bsd_free(ump, M_UFSMNT); 74912115Sdyson mp->mnt_data = (qaddr_t)0; 75012115Sdyson mp->mnt_flag &= ~MNT_LOCAL; 75112115Sdyson return (error); 75212115Sdyson} 75312115Sdyson 75412115Sdyson/* 75512115Sdyson * Flush out all the files in a filesystem. 75612115Sdyson */ 75712911Sphkstatic int 75812115Sdysonext2_flushfiles(mp, flags, p) 75912115Sdyson register struct mount *mp; 76012115Sdyson int flags; 76112115Sdyson struct proc *p; 76212115Sdyson{ 76312115Sdyson register struct ufsmount *ump; 76412147Sdyson int error; 76512746Sbde#if QUOTA 76612746Sbde int i; 76712746Sbde#endif 76812115Sdyson 76912115Sdyson ump = VFSTOUFS(mp); 77012115Sdyson#if QUOTA 77112115Sdyson if (mp->mnt_flag & MNT_QUOTA) { 77212115Sdyson if (error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) 77312115Sdyson return (error); 77412115Sdyson for (i = 0; i < MAXQUOTAS; i++) { 77512115Sdyson if (ump->um_quotas[i] == NULLVP) 77612115Sdyson continue; 77712115Sdyson quotaoff(p, mp, i); 77812115Sdyson } 77912115Sdyson /* 78012115Sdyson * Here we fall through to vflush again to ensure 78112115Sdyson * that we have gotten rid of all the system vnodes. 78212115Sdyson */ 78312115Sdyson } 78412115Sdyson#endif 78512115Sdyson error = vflush(mp, NULLVP, flags); 78612115Sdyson return (error); 78712115Sdyson} 78812115Sdyson 78912115Sdyson/* 79012115Sdyson * Get file system statistics. 79112115Sdyson * taken from ext2/super.c ext2_statfs 79212115Sdyson */ 79312911Sphkstatic int 79412115Sdysonext2_statfs(mp, sbp, p) 79512115Sdyson struct mount *mp; 79612115Sdyson register struct statfs *sbp; 79712115Sdyson struct proc *p; 79812115Sdyson{ 79912115Sdyson unsigned long overhead; 80012115Sdyson unsigned long overhead_per_group; 80112115Sdyson 80212115Sdyson register struct ufsmount *ump; 80312115Sdyson register struct ext2_sb_info *fs; 80412115Sdyson register struct ext2_super_block *es; 80512115Sdyson 80612115Sdyson ump = VFSTOUFS(mp); 80712115Sdyson fs = ump->um_e2fs; 80812115Sdyson es = fs->s_es; 80912115Sdyson 81012115Sdyson if (es->s_magic != EXT2_SUPER_MAGIC) 81112115Sdyson panic("ext2_statfs - magic number spoiled"); 81212115Sdyson 81312115Sdyson /* 81412115Sdyson * Compute the overhead (FS structures) 81512115Sdyson */ 81612115Sdyson overhead_per_group = 1 /* super block */ + 81712115Sdyson fs->s_db_per_group + 81812115Sdyson 1 /* block bitmap */ + 81912115Sdyson 1 /* inode bitmap */ + 82012115Sdyson fs->s_itb_per_group; 82112115Sdyson overhead = es->s_first_data_block + 82212115Sdyson fs->s_groups_count * overhead_per_group; 82312115Sdyson 82412115Sdyson sbp->f_bsize = EXT2_FRAG_SIZE(fs); 82512115Sdyson sbp->f_iosize = EXT2_BLOCK_SIZE(fs); 82612115Sdyson sbp->f_blocks = es->s_blocks_count - overhead; 82712115Sdyson sbp->f_bfree = es->s_free_blocks_count; 82812115Sdyson sbp->f_bavail = sbp->f_bfree - es->s_r_blocks_count; 82912115Sdyson sbp->f_files = es->s_inodes_count; 83012115Sdyson sbp->f_ffree = es->s_free_inodes_count; 83112115Sdyson if (sbp != &mp->mnt_stat) { 83238909Sbde sbp->f_type = mp->mnt_vfc->vfc_typenum; 83312115Sdyson bcopy((caddr_t)mp->mnt_stat.f_mntonname, 83412115Sdyson (caddr_t)&sbp->f_mntonname[0], MNAMELEN); 83512115Sdyson bcopy((caddr_t)mp->mnt_stat.f_mntfromname, 83612115Sdyson (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); 83712115Sdyson } 83812115Sdyson return (0); 83912115Sdyson} 84012115Sdyson 84112115Sdyson/* 84212115Sdyson * Go through the disk queues to initiate sandbagged IO; 84312115Sdyson * go through the inodes to write those that have been modified; 84412115Sdyson * initiate the writing of the super block if it has been modified. 84512115Sdyson * 84612115Sdyson * Note: we are always called with the filesystem marked `MPBUSY'. 84712115Sdyson */ 84812911Sphkstatic int 84912115Sdysonext2_sync(mp, waitfor, cred, p) 85012115Sdyson struct mount *mp; 85112115Sdyson int waitfor; 85212115Sdyson struct ucred *cred; 85312115Sdyson struct proc *p; 85412115Sdyson{ 85512115Sdyson register struct vnode *vp; 85612115Sdyson register struct inode *ip; 85712115Sdyson register struct ufsmount *ump = VFSTOUFS(mp); 85812115Sdyson register struct ext2_sb_info *fs; 85912115Sdyson int error, allerror = 0; 86012115Sdyson 86112115Sdyson fs = ump->um_e2fs; 86212115Sdyson /* 86312115Sdyson * Write back modified superblock. 86412115Sdyson * Consistency check that the superblock 86512115Sdyson * is still in the buffer cache. 86612115Sdyson */ 86712115Sdyson if (fs->s_dirt) { 86812115Sdyson if (fs->s_rd_only != 0) { /* XXX */ 86912115Sdyson printf("fs = %s\n", fs->fs_fsmnt); 87012115Sdyson panic("update: rofs mod"); 87112115Sdyson } 87212115Sdyson fs->s_dirt = 0; 87334961Sphk fs->s_es->s_wtime = time_second; 87412115Sdyson allerror = ext2_sbupdate(ump, waitfor); 87512115Sdyson } 87612115Sdyson /* 87712115Sdyson * Write back each (modified) inode. 87812115Sdyson */ 87912115Sdysonloop: 88012115Sdyson for (vp = mp->mnt_vnodelist.lh_first; 88112115Sdyson vp != NULL; 88212115Sdyson vp = vp->v_mntvnodes.le_next) { 88312115Sdyson /* 88412115Sdyson * If the vnode that we are about to sync is no longer 88512115Sdyson * associated with this mount point, start over. 88612115Sdyson */ 88712115Sdyson if (vp->v_mount != mp) 88812115Sdyson goto loop; 88912115Sdyson if (VOP_ISLOCKED(vp)) 89012115Sdyson continue; 89137966Sjulian if (vp->v_type == VNON) /* XXX why is this needed? (it is) */ 89237966Sjulian continue; 89312115Sdyson ip = VTOI(vp); 89412115Sdyson if ((ip->i_flag & 89512115Sdyson (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 && 89612115Sdyson vp->v_dirtyblkhd.lh_first == NULL) 89712115Sdyson continue; 89822521Sdyson if (vget(vp, LK_EXCLUSIVE, p)) 89912115Sdyson goto loop; 90012115Sdyson if (error = VOP_FSYNC(vp, cred, waitfor, p)) 90112115Sdyson allerror = error; 90212115Sdyson vput(vp); 90312115Sdyson } 90412115Sdyson /* 90512115Sdyson * Force stale file system control information to be flushed. 90612115Sdyson */ 90712115Sdyson if (error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p)) 90812115Sdyson allerror = error; 90912115Sdyson#if QUOTA 91012115Sdyson qsync(mp); 91112115Sdyson#endif 91212115Sdyson return (allerror); 91312115Sdyson} 91412115Sdyson 91512115Sdyson/* 91612115Sdyson * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it 91712115Sdyson * in from disk. If it is in core, wait for the lock bit to clear, then 91812115Sdyson * return the inode locked. Detection and handling of mount points must be 91912115Sdyson * done by the calling routine. 92012115Sdyson */ 92112911Sphkstatic int 92212115Sdysonext2_vget(mp, ino, vpp) 92312115Sdyson struct mount *mp; 92412115Sdyson ino_t ino; 92512115Sdyson struct vnode **vpp; 92612115Sdyson{ 92712115Sdyson register struct ext2_sb_info *fs; 92812115Sdyson register struct inode *ip; 92912115Sdyson struct ufsmount *ump; 93012115Sdyson struct buf *bp; 93112115Sdyson struct vnode *vp; 93212115Sdyson dev_t dev; 93330280Sphk int i, error; 93412115Sdyson int used_blocks; 93512115Sdyson 93612115Sdyson ump = VFSTOUFS(mp); 93712115Sdyson dev = ump->um_dev; 93812406Sdysonrestart: 93912115Sdyson if ((*vpp = ufs_ihashget(dev, ino)) != NULL) 94012115Sdyson return (0); 94112115Sdyson 94212406Sdyson /* 94312406Sdyson * Lock out the creation of new entries in the FFS hash table in 94412406Sdyson * case getnewvnode() or MALLOC() blocks, otherwise a duplicate 94512406Sdyson * may occur! 94612406Sdyson */ 94712406Sdyson if (ext2fs_inode_hash_lock) { 94812406Sdyson while (ext2fs_inode_hash_lock) { 94912406Sdyson ext2fs_inode_hash_lock = -1; 95036102Sbde tsleep(&ext2fs_inode_hash_lock, PVM, "e2vget", 0); 95112406Sdyson } 95212406Sdyson goto restart; 95312406Sdyson } 95412406Sdyson ext2fs_inode_hash_lock = 1; 95512406Sdyson 95636102Sbde /* 95736102Sbde * If this MALLOC() is performed after the getnewvnode() 95836102Sbde * it might block, leaving a vnode with a NULL v_data to be 95936102Sbde * found by ext2_sync() if a sync happens to fire right then, 96036102Sbde * which will cause a panic because ext2_sync() blindly 96136102Sbde * dereferences vp->v_data (as well it should). 96236102Sbde */ 96336102Sbde MALLOC(ip, struct inode *, sizeof(struct inode), M_EXT2NODE, M_WAITOK); 96436102Sbde 96512115Sdyson /* Allocate a new vnode/inode. */ 96612115Sdyson if (error = getnewvnode(VT_UFS, mp, ext2_vnodeop_p, &vp)) { 96736102Sbde if (ext2fs_inode_hash_lock < 0) 96836102Sbde wakeup(&ext2fs_inode_hash_lock); 96936102Sbde ext2fs_inode_hash_lock = 0; 97012115Sdyson *vpp = NULL; 97136102Sbde FREE(ip, M_EXT2NODE); 97212115Sdyson return (error); 97312115Sdyson } 97412115Sdyson bzero((caddr_t)ip, sizeof(struct inode)); 97538998Sbde lockinit(&ip->i_lock, PINOD, "ext2in", 0, 0); 97612115Sdyson vp->v_data = ip; 97712115Sdyson ip->i_vnode = vp; 97812115Sdyson ip->i_e2fs = fs = ump->um_e2fs; 97912115Sdyson ip->i_dev = dev; 98012115Sdyson ip->i_number = ino; 98112115Sdyson#if QUOTA 98212115Sdyson for (i = 0; i < MAXQUOTAS; i++) 98312115Sdyson ip->i_dquot[i] = NODQUOT; 98412115Sdyson#endif 98512115Sdyson /* 98612115Sdyson * Put it onto its hash chain and lock it so that other requests for 98712115Sdyson * this inode will block if they arrive while we are sleeping waiting 98812115Sdyson * for old data structures to be purged or for the contents of the 98912115Sdyson * disk portion of this inode to be read. 99012115Sdyson */ 99112115Sdyson ufs_ihashins(ip); 99212115Sdyson 99312406Sdyson if (ext2fs_inode_hash_lock < 0) 99412406Sdyson wakeup(&ext2fs_inode_hash_lock); 99512406Sdyson ext2fs_inode_hash_lock = 0; 99612406Sdyson 99712115Sdyson /* Read in the disk contents for the inode, copy into the inode. */ 99812115Sdyson#if 0 99912115Sdysonprintf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino))); 100012115Sdyson#endif 100112115Sdyson if (error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)), 100212115Sdyson (int)fs->s_blocksize, NOCRED, &bp)) { 100312115Sdyson /* 100412115Sdyson * The inode does not contain anything useful, so it would 100512115Sdyson * be misleading to leave it on its hash chain. With mode 100612115Sdyson * still zero, it will be unlinked and returned to the free 100712115Sdyson * list by vput(). 100812115Sdyson */ 100912115Sdyson vput(vp); 101012115Sdyson brelse(bp); 101112115Sdyson *vpp = NULL; 101212115Sdyson return (error); 101312115Sdyson } 101412115Sdyson /* convert ext2 inode to dinode */ 101512115Sdyson ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE * 101612115Sdyson ino_to_fsbo(fs, ino)), &ip->i_din); 101712115Sdyson ip->i_block_group = ino_to_cg(fs, ino); 101812115Sdyson ip->i_next_alloc_block = 0; 101912115Sdyson ip->i_next_alloc_goal = 0; 102012115Sdyson ip->i_prealloc_count = 0; 102112115Sdyson ip->i_prealloc_block = 0; 102212115Sdyson /* now we want to make sure that block pointers for unused 102312115Sdyson blocks are zeroed out - ext2_balloc depends on this 102412115Sdyson although for regular files and directories only 102512115Sdyson */ 102612115Sdyson if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) { 102712115Sdyson used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize; 102812115Sdyson for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++) 102912115Sdyson ip->i_db[i] = 0; 103012115Sdyson } 103112115Sdyson/* 103212115Sdyson ext2_print_inode(ip); 103312115Sdyson*/ 103412115Sdyson brelse(bp); 103512115Sdyson 103612115Sdyson /* 103712115Sdyson * Initialize the vnode from the inode, check for aliases. 103812115Sdyson * Note that the underlying vnode may have changed. 103912115Sdyson */ 104014249Sbde if (error = ufs_vinit(mp, ext2_specop_p, ext2_fifoop_p, &vp)) { 104112115Sdyson vput(vp); 104212115Sdyson *vpp = NULL; 104312115Sdyson return (error); 104412115Sdyson } 104512115Sdyson /* 104612115Sdyson * Finish inode initialization now that aliasing has been resolved. 104712115Sdyson */ 104812115Sdyson ip->i_devvp = ump->um_devvp; 104912115Sdyson VREF(ip->i_devvp); 105012115Sdyson /* 105112115Sdyson * Set up a generation number for this inode if it does not 105212115Sdyson * already have one. This should only happen on old filesystems. 105312115Sdyson */ 105412115Sdyson if (ip->i_gen == 0) { 105531485Sbde ip->i_gen = random() / 2 + 1; 105612115Sdyson if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) 105712115Sdyson ip->i_flag |= IN_MODIFIED; 105812115Sdyson } 105912115Sdyson *vpp = vp; 106012115Sdyson return (0); 106112115Sdyson} 106212115Sdyson 106312115Sdyson/* 106412115Sdyson * File handle to vnode 106512115Sdyson * 106612115Sdyson * Have to be really careful about stale file handles: 106712115Sdyson * - check that the inode number is valid 106812115Sdyson * - call ext2_vget() to get the locked inode 106912115Sdyson * - check for an unallocated inode (i_mode == 0) 107012115Sdyson * - check that the given client host has export rights and return 107112115Sdyson * those rights via. exflagsp and credanonp 107212115Sdyson */ 107312911Sphkstatic int 107412115Sdysonext2_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp) 107512115Sdyson register struct mount *mp; 107612115Sdyson struct fid *fhp; 107728270Swollman struct sockaddr *nam; 107812115Sdyson struct vnode **vpp; 107912115Sdyson int *exflagsp; 108012115Sdyson struct ucred **credanonp; 108112115Sdyson{ 108212115Sdyson register struct ufid *ufhp; 108312115Sdyson struct ext2_sb_info *fs; 108412115Sdyson 108512115Sdyson ufhp = (struct ufid *)fhp; 108612115Sdyson fs = VFSTOUFS(mp)->um_e2fs; 108712115Sdyson if (ufhp->ufid_ino < ROOTINO || 108812115Sdyson ufhp->ufid_ino >= fs->s_groups_count * fs->s_es->s_inodes_per_group) 108912115Sdyson return (ESTALE); 109012115Sdyson return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp)); 109112115Sdyson} 109212115Sdyson 109312115Sdyson/* 109412115Sdyson * Vnode pointer to File handle 109512115Sdyson */ 109612115Sdyson/* ARGSUSED */ 109712911Sphkstatic int 109812115Sdysonext2_vptofh(vp, fhp) 109912115Sdyson struct vnode *vp; 110012115Sdyson struct fid *fhp; 110112115Sdyson{ 110212115Sdyson register struct inode *ip; 110312115Sdyson register struct ufid *ufhp; 110412115Sdyson 110512115Sdyson ip = VTOI(vp); 110612115Sdyson ufhp = (struct ufid *)fhp; 110712115Sdyson ufhp->ufid_len = sizeof(struct ufid); 110812115Sdyson ufhp->ufid_ino = ip->i_number; 110912115Sdyson ufhp->ufid_gen = ip->i_gen; 111012115Sdyson return (0); 111112115Sdyson} 111212115Sdyson 111312115Sdyson/* 111412115Sdyson * Write a superblock and associated information back to disk. 111512115Sdyson */ 111612911Sphkstatic int 111712115Sdysonext2_sbupdate(mp, waitfor) 111812115Sdyson struct ufsmount *mp; 111912115Sdyson int waitfor; 112012115Sdyson{ 112112115Sdyson register struct ext2_sb_info *fs = mp->um_e2fs; 112212115Sdyson register struct ext2_super_block *es = fs->s_es; 112312115Sdyson register struct buf *bp; 112412147Sdyson int i, error = 0; 112512115Sdyson/* 112612115Sdysonprintf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no"); 112712115Sdyson*/ 112812115Sdyson bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0); 112912115Sdyson bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block)); 113012115Sdyson if (waitfor == MNT_WAIT) 113112115Sdyson error = bwrite(bp); 113212115Sdyson else 113312115Sdyson bawrite(bp); 113412115Sdyson 113527881Sdyson /* 113627881Sdyson * The buffers for group descriptors, inode bitmaps and block bitmaps 113727881Sdyson * are not busy at this point and are (hopefully) written by the 113827881Sdyson * usual sync mechanism. No need to write them here 113912115Sdyson */ 114012115Sdyson 114112115Sdyson return (error); 114212115Sdyson} 1143