ext2_vfsops.c revision 71576
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 4051138Salfred * $FreeBSD: head/sys/gnu/fs/ext2fs/ext2_vfsops.c 71576 2001-01-24 12:35:55Z jasone $ 4112115Sdyson */ 4212115Sdyson 4313260Swollman#include "opt_quota.h" 4412115Sdyson 4512115Sdyson#include <sys/param.h> 4612115Sdyson#include <sys/systm.h> 4712115Sdyson#include <sys/namei.h> 4812115Sdyson#include <sys/proc.h> 4912115Sdyson#include <sys/kernel.h> 5012115Sdyson#include <sys/vnode.h> 5112115Sdyson#include <sys/mount.h> 5260041Sphk#include <sys/bio.h> 5312115Sdyson#include <sys/buf.h> 5429906Skato#include <sys/conf.h> 5524131Sbde#include <sys/fcntl.h> 5612115Sdyson#include <sys/disklabel.h> 5712115Sdyson#include <sys/malloc.h> 5812115Sdyson#include <sys/stat.h> 5971576Sjasone#include <sys/mutex.h> 6012115Sdyson 6159259Srwatson#include <ufs/ufs/extattr.h> 6212115Sdyson#include <ufs/ufs/quota.h> 6312115Sdyson#include <ufs/ufs/ufsmount.h> 6412115Sdyson#include <ufs/ufs/inode.h> 6512115Sdyson#include <ufs/ufs/ufs_extern.h> 6612115Sdyson 6754655Seivind 6812115Sdyson#include <gnu/ext2fs/fs.h> 6912115Sdyson#include <gnu/ext2fs/ext2_extern.h> 7012115Sdyson#include <gnu/ext2fs/ext2_fs.h> 7112115Sdyson#include <gnu/ext2fs/ext2_fs_sb.h> 7212115Sdyson 7351138Salfredstatic int ext2_fhtovp __P((struct mount *, struct fid *, struct vnode **)); 7412911Sphkstatic int ext2_flushfiles __P((struct mount *mp, int flags, struct proc *p)); 7512911Sphkstatic int ext2_mount __P((struct mount *, 7612911Sphk char *, caddr_t, struct nameidata *, struct proc *)); 7712911Sphkstatic int ext2_mountfs __P((struct vnode *, struct mount *, struct proc *)); 7812911Sphkstatic int ext2_reload __P((struct mount *mountp, struct ucred *cred, 7912911Sphk struct proc *p)); 8012911Sphkstatic int ext2_sbupdate __P((struct ufsmount *, int)); 8112911Sphkstatic int ext2_statfs __P((struct mount *, struct statfs *, struct proc *)); 8212911Sphkstatic int ext2_sync __P((struct mount *, int, struct ucred *, struct proc *)); 8312911Sphkstatic int ext2_unmount __P((struct mount *, int, struct proc *)); 8412911Sphkstatic int ext2_vget __P((struct mount *, ino_t, struct vnode **)); 8512911Sphkstatic int ext2_vptofh __P((struct vnode *, struct fid *)); 8612115Sdyson 8731315Sbdestatic MALLOC_DEFINE(M_EXT2NODE, "EXT2 node", "EXT2 vnode private part"); 8830280Sphk 8912911Sphkstatic struct vfsops ext2fs_vfsops = { 9012115Sdyson ext2_mount, 9112115Sdyson ufs_start, /* empty function */ 9212115Sdyson ext2_unmount, 9312115Sdyson ufs_root, /* root inode via vget */ 9412115Sdyson ufs_quotactl, /* does operations associated with quotas */ 9512115Sdyson ext2_statfs, 9612115Sdyson ext2_sync, 9712115Sdyson ext2_vget, 9812115Sdyson ext2_fhtovp, 9951138Salfred ufs_check_export, 10012115Sdyson ext2_vptofh, 10112115Sdyson ext2_init, 10254803Srwatson vfs_stduninit, 10354803Srwatson vfs_stdextattrctl, 10412115Sdyson}; 10512115Sdyson 10638909SbdeVFS_SET(ext2fs_vfsops, ext2fs, 0); 10712115Sdyson#define bsd_malloc malloc 10812115Sdyson#define bsd_free free 10912115Sdyson 11012911Sphkstatic int ext2fs_inode_hash_lock; 11112115Sdyson 11255313Sbdestatic int ext2_check_sb_compat __P((struct ext2_super_block *es, 11355313Sbde dev_t dev, int ronly)); 11416322Sgpalmerstatic int compute_sb_data __P((struct vnode * devvp, 11516322Sgpalmer struct ext2_super_block * es, 11616322Sgpalmer struct ext2_sb_info * fs)); 11716322Sgpalmer 11816322Sgpalmer#ifdef notyet 11916322Sgpalmerstatic int ext2_mountroot __P((void)); 12016322Sgpalmer 12112115Sdyson/* 12216322Sgpalmer * Called by main() when ext2fs is going to be mounted as root. 12312115Sdyson * 12412115Sdyson * Name is updated by mount(8) after booting. 12512115Sdyson */ 12612115Sdyson#define ROOTNAME "root_device" 12712115Sdyson 12812911Sphkstatic int 12912115Sdysonext2_mountroot() 13012115Sdyson{ 13112115Sdyson register struct ext2_sb_info *fs; 13212115Sdyson register struct mount *mp; 13312115Sdyson struct proc *p = curproc; 13412115Sdyson struct ufsmount *ump; 13512115Sdyson u_int size; 13612115Sdyson int error; 13712115Sdyson 13829208Sbde if ((error = bdevvp(rootdev, &rootvp))) { 13952782Smsmith printf("ext2_mountroot: can't find rootvp\n"); 14029208Sbde return (error); 14129208Sbde } 14212115Sdyson mp = bsd_malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK); 14312115Sdyson bzero((char *)mp, (u_long)sizeof(struct mount)); 14412115Sdyson mp->mnt_op = &ext2fs_vfsops; 14512115Sdyson mp->mnt_flag = MNT_RDONLY; 14612115Sdyson if (error = ext2_mountfs(rootvp, mp, p)) { 14712115Sdyson bsd_free(mp, M_MOUNT); 14812115Sdyson return (error); 14912115Sdyson } 15012115Sdyson if (error = vfs_lock(mp)) { 15112115Sdyson (void)ext2_unmount(mp, 0, p); 15212115Sdyson bsd_free(mp, M_MOUNT); 15312115Sdyson return (error); 15412115Sdyson } 15553452Sphk TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list); 15612115Sdyson mp->mnt_flag |= MNT_ROOTFS; 15712115Sdyson mp->mnt_vnodecovered = NULLVP; 15812115Sdyson ump = VFSTOUFS(mp); 15912115Sdyson fs = ump->um_e2fs; 16012115Sdyson bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt)); 16112115Sdyson fs->fs_fsmnt[0] = '/'; 16212115Sdyson bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname, 16312115Sdyson MNAMELEN); 16412115Sdyson (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 16512115Sdyson &size); 16612115Sdyson bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 16712115Sdyson (void)ext2_statfs(mp, &mp->mnt_stat, p); 16812115Sdyson vfs_unlock(mp); 16912115Sdyson inittodr(fs->s_es->s_wtime); /* this helps to set the time */ 17012115Sdyson return (0); 17112115Sdyson} 17216322Sgpalmer#endif 17312115Sdyson 17412115Sdyson/* 17512115Sdyson * VFS Operations. 17612115Sdyson * 17712115Sdyson * mount system call 17812115Sdyson */ 17912911Sphkstatic int 18012115Sdysonext2_mount(mp, path, data, ndp, p) 18112115Sdyson register struct mount *mp; 18212115Sdyson char *path; 18312115Sdyson caddr_t data; /* this is actually a (struct ufs_args *) */ 18412115Sdyson struct nameidata *ndp; 18512115Sdyson struct proc *p; 18612115Sdyson{ 18712115Sdyson struct vnode *devvp; 18812115Sdyson struct ufs_args args; 18912115Sdyson struct ufsmount *ump = 0; 19071483Sjhb struct ucred *uc; 19112115Sdyson register struct ext2_sb_info *fs; 19269806Smjacob size_t size; 19312115Sdyson int error, flags; 19439028Sbde mode_t accessmode; 19512115Sdyson 19643301Sdillon if ((error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args))) != 0) 19712115Sdyson return (error); 19812115Sdyson /* 19912115Sdyson * If updating, check whether changing from read-only to 20012115Sdyson * read/write; if there is no device name, that's all we do. 20112115Sdyson */ 20212115Sdyson if (mp->mnt_flag & MNT_UPDATE) { 20312115Sdyson ump = VFSTOUFS(mp); 20412115Sdyson fs = ump->um_e2fs; 20512115Sdyson error = 0; 20612115Sdyson if (fs->s_rd_only == 0 && (mp->mnt_flag & MNT_RDONLY)) { 20712115Sdyson flags = WRITECLOSE; 20812115Sdyson if (mp->mnt_flag & MNT_FORCE) 20912115Sdyson flags |= FORCECLOSE; 21022521Sdyson if (vfs_busy(mp, LK_NOWAIT, 0, p)) 21112115Sdyson return (EBUSY); 21212115Sdyson error = ext2_flushfiles(mp, flags, p); 21322521Sdyson vfs_unbusy(mp, p); 21439670Sbde if (!error && fs->s_wasvalid) { 21539670Sbde fs->s_es->s_state |= EXT2_VALID_FS; 21639670Sbde ext2_sbupdate(ump, MNT_WAIT); 21739670Sbde } 21839670Sbde fs->s_rd_only = 1; 21912115Sdyson } 22012115Sdyson if (!error && (mp->mnt_flag & MNT_RELOAD)) 22112115Sdyson error = ext2_reload(mp, ndp->ni_cnd.cn_cred, p); 22212115Sdyson if (error) 22312115Sdyson return (error); 22457839Sbde devvp = ump->um_devvp; 22557839Sbde if (ext2_check_sb_compat(fs->s_es, devvp->v_rdev, 22657839Sbde (mp->mnt_kern_flag & MNTK_WANTRDWR) == 0) != 0) 22757839Sbde return (EPERM); 22839028Sbde if (fs->s_rd_only && (mp->mnt_kern_flag & MNTK_WANTRDWR)) { 22939028Sbde /* 23039028Sbde * If upgrade to read-write by non-root, then verify 23139028Sbde * that user has necessary permissions on the device. 23239028Sbde */ 23371483Sjhb if (suser(p)) { 23439028Sbde vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 23571483Sjhb PROC_LOCK(p); 23671483Sjhb uc = p->p_ucred; 23771483Sjhb crhold(uc); 23871483Sjhb PROC_UNLOCK(p); 23943301Sdillon if ((error = VOP_ACCESS(devvp, VREAD | VWRITE, 24071483Sjhb uc, p)) != 0) { 24171483Sjhb crfree(uc); 24239028Sbde VOP_UNLOCK(devvp, 0, p); 24339028Sbde return (error); 24439028Sbde } 24571483Sjhb crfree(uc); 24639028Sbde VOP_UNLOCK(devvp, 0, p); 24739028Sbde } 24839028Sbde 24939670Sbde if ((fs->s_es->s_state & EXT2_VALID_FS) == 0 || 25039670Sbde (fs->s_es->s_state & EXT2_ERROR_FS)) { 25139670Sbde if (mp->mnt_flag & MNT_FORCE) { 25239670Sbde printf( 25339670Sbde"WARNING: %s was not properly dismounted\n", 25439670Sbde fs->fs_fsmnt); 25539670Sbde } else { 25639670Sbde printf( 25739670Sbde"WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n", 25839670Sbde fs->fs_fsmnt); 25939670Sbde return (EPERM); 26039670Sbde } 26139670Sbde } 26212115Sdyson fs->s_es->s_state &= ~EXT2_VALID_FS; 26312115Sdyson ext2_sbupdate(ump, MNT_WAIT); 26439670Sbde fs->s_rd_only = 0; 26512115Sdyson } 26612115Sdyson if (args.fspec == 0) { 26712115Sdyson /* 26812115Sdyson * Process export requests. 26912115Sdyson */ 27012115Sdyson return (vfs_export(mp, &ump->um_export, &args.export)); 27112115Sdyson } 27212115Sdyson } 27312115Sdyson /* 27412115Sdyson * Not an update, or updating the name: look up the name 27512115Sdyson * and verify that it refers to a sensible block device. 27612115Sdyson */ 27712115Sdyson NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); 27843301Sdillon if ((error = namei(ndp)) != 0) 27912115Sdyson return (error); 28054655Seivind NDFREE(ndp, NDF_ONLY_PNBUF); 28112115Sdyson devvp = ndp->ni_vp; 28212115Sdyson 28355756Sphk if (!vn_isdisk(devvp, &error)) { 28412115Sdyson vrele(devvp); 28555756Sphk return (error); 28612115Sdyson } 28739028Sbde 28839028Sbde /* 28939028Sbde * If mount by non-root, then verify that user has necessary 29039028Sbde * permissions on the device. 29139028Sbde */ 29271483Sjhb if (suser(p)) { 29339028Sbde accessmode = VREAD; 29439028Sbde if ((mp->mnt_flag & MNT_RDONLY) == 0) 29539028Sbde accessmode |= VWRITE; 29639028Sbde vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 29771483Sjhb PROC_LOCK(p); 29871483Sjhb uc = p->p_ucred; 29971483Sjhb crhold(uc); 30071483Sjhb PROC_UNLOCK(p); 30171483Sjhb if ((error = VOP_ACCESS(devvp, accessmode, uc, p)) != 0) { 30271483Sjhb crfree(uc); 30339028Sbde vput(devvp); 30439028Sbde return (error); 30539028Sbde } 30671483Sjhb crfree(uc); 30739028Sbde VOP_UNLOCK(devvp, 0, p); 30839028Sbde } 30939028Sbde 31029888Skato if ((mp->mnt_flag & MNT_UPDATE) == 0) { 31112115Sdyson error = ext2_mountfs(devvp, mp, p); 31229888Skato } else { 31312115Sdyson if (devvp != ump->um_devvp) 31412115Sdyson error = EINVAL; /* needs translation */ 31512115Sdyson else 31612115Sdyson vrele(devvp); 31712115Sdyson } 31812115Sdyson if (error) { 31912115Sdyson vrele(devvp); 32012115Sdyson return (error); 32112115Sdyson } 32212115Sdyson ump = VFSTOUFS(mp); 32312115Sdyson fs = ump->um_e2fs; 32412115Sdyson (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size); 32512115Sdyson bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size); 32612115Sdyson bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname, 32712115Sdyson MNAMELEN); 32812115Sdyson (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 32912115Sdyson &size); 33012115Sdyson bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 33112115Sdyson (void)ext2_statfs(mp, &mp->mnt_stat, p); 33212115Sdyson return (0); 33312115Sdyson} 33412115Sdyson 33512115Sdyson/* 33612115Sdyson * checks that the data in the descriptor blocks make sense 33712115Sdyson * this is taken from ext2/super.c 33812115Sdyson */ 33912115Sdysonstatic int ext2_check_descriptors (struct ext2_sb_info * sb) 34012115Sdyson{ 34112115Sdyson int i; 34212115Sdyson int desc_block = 0; 34312115Sdyson unsigned long block = sb->s_es->s_first_data_block; 34412115Sdyson struct ext2_group_desc * gdp = NULL; 34512115Sdyson 34612115Sdyson /* ext2_debug ("Checking group descriptors"); */ 34712115Sdyson 34812115Sdyson for (i = 0; i < sb->s_groups_count; i++) 34912115Sdyson { 35012115Sdyson /* examine next descriptor block */ 35112115Sdyson if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0) 35212115Sdyson gdp = (struct ext2_group_desc *) 35312115Sdyson sb->s_group_desc[desc_block++]->b_data; 35412115Sdyson if (gdp->bg_block_bitmap < block || 35512115Sdyson gdp->bg_block_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb)) 35612115Sdyson { 35712115Sdyson printf ("ext2_check_descriptors: " 35812115Sdyson "Block bitmap for group %d" 35939671Sbde " not in group (block %lu)!\n", 36012115Sdyson i, (unsigned long) gdp->bg_block_bitmap); 36112115Sdyson return 0; 36212115Sdyson } 36312115Sdyson if (gdp->bg_inode_bitmap < block || 36412115Sdyson gdp->bg_inode_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb)) 36512115Sdyson { 36612115Sdyson printf ("ext2_check_descriptors: " 36712115Sdyson "Inode bitmap for group %d" 36839671Sbde " not in group (block %lu)!\n", 36912115Sdyson i, (unsigned long) gdp->bg_inode_bitmap); 37012115Sdyson return 0; 37112115Sdyson } 37212115Sdyson if (gdp->bg_inode_table < block || 37312115Sdyson gdp->bg_inode_table + sb->s_itb_per_group >= 37412115Sdyson block + EXT2_BLOCKS_PER_GROUP(sb)) 37512115Sdyson { 37612115Sdyson printf ("ext2_check_descriptors: " 37712115Sdyson "Inode table for group %d" 37839671Sbde " not in group (block %lu)!\n", 37912115Sdyson i, (unsigned long) gdp->bg_inode_table); 38012115Sdyson return 0; 38112115Sdyson } 38212115Sdyson block += EXT2_BLOCKS_PER_GROUP(sb); 38312115Sdyson gdp++; 38412115Sdyson } 38512115Sdyson return 1; 38612115Sdyson} 38712115Sdyson 38855313Sbdestatic int 38955313Sbdeext2_check_sb_compat(es, dev, ronly) 39055313Sbde struct ext2_super_block *es; 39155313Sbde dev_t dev; 39255313Sbde int ronly; 39355313Sbde{ 39455313Sbde 39555313Sbde if (es->s_magic != EXT2_SUPER_MAGIC) { 39655313Sbde printf("ext2fs: %s: wrong magic number %#x (expected %#x)\n", 39755313Sbde devtoname(dev), es->s_magic, EXT2_SUPER_MAGIC); 39855313Sbde return (1); 39955313Sbde } 40055313Sbde if (es->s_rev_level > EXT2_GOOD_OLD_REV) { 40155313Sbde if (es->s_feature_incompat & ~EXT2_FEATURE_INCOMPAT_SUPP) { 40255313Sbde printf( 40355313Sbde"WARNING: mount of %s denied due to unsupported optional features\n", 40455313Sbde devtoname(dev)); 40555313Sbde return (1); 40655313Sbde } 40755313Sbde if (!ronly && 40855313Sbde (es->s_feature_ro_compat & ~EXT2_FEATURE_RO_COMPAT_SUPP)) { 40955313Sbde printf( 41055313Sbde"WARNING: R/W mount of %s denied due to unsupported optional features\n", 41155313Sbde devtoname(dev)); 41255313Sbde return (1); 41355313Sbde } 41455313Sbde } 41555313Sbde return (0); 41655313Sbde} 41755313Sbde 41812115Sdyson/* 41912115Sdyson * this computes the fields of the ext2_sb_info structure from the 42012115Sdyson * data in the ext2_super_block structure read in 42112115Sdyson */ 42212115Sdysonstatic int compute_sb_data(devvp, es, fs) 42312115Sdyson struct vnode * devvp; 42412115Sdyson struct ext2_super_block * es; 42512115Sdyson struct ext2_sb_info * fs; 42612115Sdyson{ 42712115Sdyson int db_count, error; 42812115Sdyson int i, j; 42912115Sdyson int logic_sb_block = 1; /* XXX for now */ 43012115Sdyson 43112115Sdyson#if 1 43212115Sdyson#define V(v) 43312115Sdyson#else 43412115Sdyson#define V(v) printf(#v"= %d\n", fs->v); 43512115Sdyson#endif 43612115Sdyson 43712115Sdyson fs->s_blocksize = EXT2_MIN_BLOCK_SIZE << es->s_log_block_size; 43812115Sdyson V(s_blocksize) 43912115Sdyson fs->s_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->s_log_block_size; 44012115Sdyson V(s_bshift) 44112115Sdyson fs->s_fsbtodb = es->s_log_block_size + 1; 44212115Sdyson V(s_fsbtodb) 44312115Sdyson fs->s_qbmask = fs->s_blocksize - 1; 44412115Sdyson V(s_bmask) 44512115Sdyson fs->s_blocksize_bits = EXT2_BLOCK_SIZE_BITS(es); 44612115Sdyson V(s_blocksize_bits) 44712115Sdyson fs->s_frag_size = EXT2_MIN_FRAG_SIZE << es->s_log_frag_size; 44812115Sdyson V(s_frag_size) 44912115Sdyson if (fs->s_frag_size) 45012115Sdyson fs->s_frags_per_block = fs->s_blocksize / fs->s_frag_size; 45112115Sdyson V(s_frags_per_block) 45212115Sdyson fs->s_blocks_per_group = es->s_blocks_per_group; 45312115Sdyson V(s_blocks_per_group) 45412115Sdyson fs->s_frags_per_group = es->s_frags_per_group; 45512115Sdyson V(s_frags_per_group) 45612115Sdyson fs->s_inodes_per_group = es->s_inodes_per_group; 45712115Sdyson V(s_inodes_per_group) 45812115Sdyson fs->s_inodes_per_block = fs->s_blocksize / EXT2_INODE_SIZE; 45912115Sdyson V(s_inodes_per_block) 46012115Sdyson fs->s_itb_per_group = fs->s_inodes_per_group /fs->s_inodes_per_block; 46112115Sdyson V(s_itb_per_group) 46212115Sdyson fs->s_desc_per_block = fs->s_blocksize / sizeof (struct ext2_group_desc); 46312115Sdyson V(s_desc_per_block) 46412115Sdyson /* s_resuid / s_resgid ? */ 46512115Sdyson fs->s_groups_count = (es->s_blocks_count - 46612115Sdyson es->s_first_data_block + 46712115Sdyson EXT2_BLOCKS_PER_GROUP(fs) - 1) / 46812115Sdyson EXT2_BLOCKS_PER_GROUP(fs); 46912115Sdyson V(s_groups_count) 47012115Sdyson db_count = (fs->s_groups_count + EXT2_DESC_PER_BLOCK(fs) - 1) / 47112115Sdyson EXT2_DESC_PER_BLOCK(fs); 47212115Sdyson fs->s_db_per_group = db_count; 47312115Sdyson V(s_db_per_group) 47412115Sdyson 47512115Sdyson fs->s_group_desc = bsd_malloc(db_count * sizeof (struct buf *), 47612115Sdyson M_UFSMNT, M_WAITOK); 47712115Sdyson 47812115Sdyson /* adjust logic_sb_block */ 47912115Sdyson if(fs->s_blocksize > SBSIZE) 48012115Sdyson /* Godmar thinks: if the blocksize is greater than 1024, then 48112115Sdyson the superblock is logically part of block zero. 48212115Sdyson */ 48312115Sdyson logic_sb_block = 0; 48412115Sdyson 48512115Sdyson for (i = 0; i < db_count; i++) { 48612115Sdyson error = bread(devvp , fsbtodb(fs, logic_sb_block + i + 1), 48712115Sdyson fs->s_blocksize, NOCRED, &fs->s_group_desc[i]); 48812115Sdyson if(error) { 48912115Sdyson for (j = 0; j < i; j++) 49012115Sdyson brelse(fs->s_group_desc[j]); 49112115Sdyson bsd_free(fs->s_group_desc, M_UFSMNT); 49212115Sdyson printf("EXT2-fs: unable to read group descriptors (%d)\n", error); 49312115Sdyson return EIO; 49412115Sdyson } 49527881Sdyson /* Set the B_LOCKED flag on the buffer, then brelse() it */ 49627881Sdyson LCK_BUF(fs->s_group_desc[i]) 49712115Sdyson } 49812115Sdyson if(!ext2_check_descriptors(fs)) { 49912115Sdyson for (j = 0; j < db_count; j++) 50027881Sdyson ULCK_BUF(fs->s_group_desc[j]) 50112115Sdyson bsd_free(fs->s_group_desc, M_UFSMNT); 50212115Sdyson printf("EXT2-fs: (ext2_check_descriptors failure) " 50312115Sdyson "unable to read group descriptors\n"); 50412115Sdyson return EIO; 50512115Sdyson } 50612115Sdyson 50712115Sdyson for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) { 50812115Sdyson fs->s_inode_bitmap_number[i] = 0; 50912115Sdyson fs->s_inode_bitmap[i] = NULL; 51012115Sdyson fs->s_block_bitmap_number[i] = 0; 51112115Sdyson fs->s_block_bitmap[i] = NULL; 51212115Sdyson } 51312115Sdyson fs->s_loaded_inode_bitmaps = 0; 51412115Sdyson fs->s_loaded_block_bitmaps = 0; 51512115Sdyson return 0; 51612115Sdyson} 51712115Sdyson 51812115Sdyson/* 51912115Sdyson * Reload all incore data for a filesystem (used after running fsck on 52012115Sdyson * the root filesystem and finding things to fix). The filesystem must 52112115Sdyson * be mounted read-only. 52212115Sdyson * 52312115Sdyson * Things to do to update the mount: 52412115Sdyson * 1) invalidate all cached meta-data. 52512115Sdyson * 2) re-read superblock from disk. 52612115Sdyson * 3) re-read summary information from disk. 52712115Sdyson * 4) invalidate all inactive vnodes. 52812115Sdyson * 5) invalidate all cached file data. 52912115Sdyson * 6) re-read inode data for all active vnodes. 53012115Sdyson */ 53112911Sphkstatic int 53212115Sdysonext2_reload(mountp, cred, p) 53312115Sdyson register struct mount *mountp; 53412115Sdyson struct ucred *cred; 53512115Sdyson struct proc *p; 53612115Sdyson{ 53712115Sdyson register struct vnode *vp, *nvp, *devvp; 53812115Sdyson struct inode *ip; 53912115Sdyson struct buf *bp; 54012115Sdyson struct ext2_super_block * es; 54112115Sdyson struct ext2_sb_info *fs; 54212147Sdyson int error; 54312115Sdyson 54412115Sdyson if ((mountp->mnt_flag & MNT_RDONLY) == 0) 54512115Sdyson return (EINVAL); 54612115Sdyson /* 54712115Sdyson * Step 1: invalidate all cached meta-data. 54812115Sdyson */ 54912115Sdyson devvp = VFSTOUFS(mountp)->um_devvp; 55012115Sdyson if (vinvalbuf(devvp, 0, cred, p, 0, 0)) 55112115Sdyson panic("ext2_reload: dirty1"); 55212115Sdyson /* 55312115Sdyson * Step 2: re-read superblock from disk. 55412115Sdyson * constants have been adjusted for ext2 55512115Sdyson */ 55643301Sdillon if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0) 55712115Sdyson return (error); 55812115Sdyson es = (struct ext2_super_block *)bp->b_data; 55955313Sbde if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) { 56012115Sdyson brelse(bp); 56112115Sdyson return (EIO); /* XXX needs translation */ 56212115Sdyson } 56312115Sdyson fs = VFSTOUFS(mountp)->um_e2fs; 56412115Sdyson bcopy(bp->b_data, fs->s_es, sizeof(struct ext2_super_block)); 56512115Sdyson 56643301Sdillon if((error = compute_sb_data(devvp, es, fs)) != 0) { 56712115Sdyson brelse(bp); 56812115Sdyson return error; 56912115Sdyson } 57012115Sdyson#ifdef UNKLAR 57112115Sdyson if (fs->fs_sbsize < SBSIZE) 57212115Sdyson bp->b_flags |= B_INVAL; 57312115Sdyson#endif 57412115Sdyson brelse(bp); 57512115Sdyson 57612115Sdysonloop: 57771576Sjasone mtx_enter(&mntvnode_mtx, MTX_DEF); 57812115Sdyson for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { 57939678Sbde if (vp->v_mount != mountp) { 58071576Sjasone mtx_exit(&mntvnode_mtx, MTX_DEF); 58139678Sbde goto loop; 58239678Sbde } 58312115Sdyson nvp = vp->v_mntvnodes.le_next; 58412115Sdyson /* 58512115Sdyson * Step 4: invalidate all inactive vnodes. 58612115Sdyson */ 58771576Sjasone if (vrecycle(vp, &mntvnode_mtx, p)) 58839678Sbde goto loop; 58912115Sdyson /* 59012115Sdyson * Step 5: invalidate all cached file data. 59112115Sdyson */ 59266615Sjasone mtx_enter(&vp->v_interlock, MTX_DEF); 59371576Sjasone mtx_exit(&mntvnode_mtx, MTX_DEF); 59439678Sbde if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) { 59512115Sdyson goto loop; 59639678Sbde } 59712115Sdyson if (vinvalbuf(vp, 0, cred, p, 0, 0)) 59812115Sdyson panic("ext2_reload: dirty2"); 59912115Sdyson /* 60012115Sdyson * Step 6: re-read inode data for all active vnodes. 60112115Sdyson */ 60212115Sdyson ip = VTOI(vp); 60339678Sbde error = 60412115Sdyson bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 60539678Sbde (int)fs->s_blocksize, NOCRED, &bp); 60639678Sbde if (error) { 60712115Sdyson vput(vp); 60812115Sdyson return (error); 60912115Sdyson } 61012115Sdyson ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + 61139678Sbde EXT2_INODE_SIZE * ino_to_fsbo(fs, ip->i_number)), 61239678Sbde &ip->i_din); 61312115Sdyson brelse(bp); 61412115Sdyson vput(vp); 61571576Sjasone mtx_enter(&mntvnode_mtx, MTX_DEF); 61612115Sdyson } 61771576Sjasone mtx_exit(&mntvnode_mtx, MTX_DEF); 61812115Sdyson return (0); 61912115Sdyson} 62012115Sdyson 62112115Sdyson/* 62212115Sdyson * Common code for mount and mountroot 62312115Sdyson */ 62412911Sphkstatic int 62512115Sdysonext2_mountfs(devvp, mp, p) 62612115Sdyson register struct vnode *devvp; 62712115Sdyson struct mount *mp; 62812115Sdyson struct proc *p; 62912115Sdyson{ 63012115Sdyson register struct ufsmount *ump; 63112115Sdyson struct buf *bp; 63271483Sjhb struct ucred *uc; 63312115Sdyson register struct ext2_sb_info *fs; 63412115Sdyson struct ext2_super_block * es; 63512115Sdyson dev_t dev = devvp->v_rdev; 63612115Sdyson struct partinfo dpart; 63712115Sdyson int havepart = 0; 63812115Sdyson int error, i, size; 63912115Sdyson int ronly; 64012115Sdyson 64112115Sdyson /* 64212115Sdyson * Disallow multiple mounts of the same device. 64312115Sdyson * Disallow mounting of a device that is currently in use 64412115Sdyson * (except for root, which might share swap device for miniroot). 64512115Sdyson * Flush out any old buffers remaining from a previous use. 64612115Sdyson */ 64743301Sdillon if ((error = vfs_mountedon(devvp)) != 0) 64812115Sdyson return (error); 64912115Sdyson if (vcount(devvp) > 1 && devvp != rootvp) 65012115Sdyson return (EBUSY); 65171483Sjhb PROC_LOCK(p); 65271483Sjhb uc = p->p_ucred; 65371483Sjhb crhold(uc); 65471483Sjhb PROC_UNLOCK(p); 65571483Sjhb if ((error = vinvalbuf(devvp, V_SAVE, uc, p, 0, 0)) != 0) { 65671483Sjhb crfree(uc); 65712115Sdyson return (error); 65871483Sjhb } 65971483Sjhb crfree(uc); 66012115Sdyson#ifdef READONLY 66112115Sdyson/* turn on this to force it to be read-only */ 66212115Sdyson mp->mnt_flag |= MNT_RDONLY; 66312115Sdyson#endif 66412115Sdyson 66512115Sdyson ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 66653059Sphk vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 66753059Sphk error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p); 66853059Sphk VOP_UNLOCK(devvp, 0, p); 66953059Sphk if (error) 67012115Sdyson return (error); 67112115Sdyson if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0) 67212115Sdyson size = DEV_BSIZE; 67312115Sdyson else { 67412115Sdyson havepart = 1; 67512115Sdyson size = dpart.disklab->d_secsize; 67612115Sdyson } 67712115Sdyson 67812115Sdyson bp = NULL; 67912115Sdyson ump = NULL; 68043301Sdillon if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0) 68112115Sdyson goto out; 68212115Sdyson es = (struct ext2_super_block *)bp->b_data; 68355313Sbde if (ext2_check_sb_compat(es, dev, ronly) != 0) { 68412115Sdyson error = EINVAL; /* XXX needs translation */ 68512115Sdyson goto out; 68612115Sdyson } 68739670Sbde if ((es->s_state & EXT2_VALID_FS) == 0 || 68839670Sbde (es->s_state & EXT2_ERROR_FS)) { 68939670Sbde if (ronly || (mp->mnt_flag & MNT_FORCE)) { 69039670Sbde printf( 69139670Sbde"WARNING: Filesystem was not properly dismounted\n"); 69239670Sbde } else { 69339670Sbde printf( 69439670Sbde"WARNING: R/W mount denied. Filesystem is not clean - run fsck\n"); 69539670Sbde error = EPERM; 69639670Sbde goto out; 69739670Sbde } 69839670Sbde } 69912115Sdyson ump = bsd_malloc(sizeof *ump, M_UFSMNT, M_WAITOK); 70012115Sdyson bzero((caddr_t)ump, sizeof *ump); 70130280Sphk ump->um_malloctype = M_EXT2NODE; 70230474Sphk ump->um_blkatoff = ext2_blkatoff; 70330474Sphk ump->um_truncate = ext2_truncate; 70430492Sphk ump->um_update = ext2_update; 70530474Sphk ump->um_valloc = ext2_valloc; 70630474Sphk ump->um_vfree = ext2_vfree; 70712115Sdyson /* I don't know whether this is the right strategy. Note that 70812115Sdyson we dynamically allocate both a ext2_sb_info and a ext2_super_block 70912115Sdyson while Linux keeps the super block in a locked buffer 71012115Sdyson */ 71112115Sdyson ump->um_e2fs = bsd_malloc(sizeof(struct ext2_sb_info), 71212115Sdyson M_UFSMNT, M_WAITOK); 71312115Sdyson ump->um_e2fs->s_es = bsd_malloc(sizeof(struct ext2_super_block), 71412115Sdyson M_UFSMNT, M_WAITOK); 71512115Sdyson bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block)); 71639671Sbde if ((error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs))) 71739671Sbde goto out; 71839671Sbde /* 71939671Sbde * We don't free the group descriptors allocated by compute_sb_data() 72039671Sbde * until ext2_unmount(). This is OK since the mount will succeed. 72139671Sbde */ 72212115Sdyson brelse(bp); 72312115Sdyson bp = NULL; 72412115Sdyson fs = ump->um_e2fs; 72512115Sdyson fs->s_rd_only = ronly; /* ronly is set according to mnt_flags */ 72612115Sdyson /* if the fs is not mounted read-only, make sure the super block is 72712115Sdyson always written back on a sync() 72812115Sdyson */ 72939670Sbde fs->s_wasvalid = fs->s_es->s_state & EXT2_VALID_FS ? 1 : 0; 73012115Sdyson if (ronly == 0) { 73112115Sdyson fs->s_dirt = 1; /* mark it modified */ 73212115Sdyson fs->s_es->s_state &= ~EXT2_VALID_FS; /* set fs invalid */ 73312115Sdyson } 73412115Sdyson mp->mnt_data = (qaddr_t)ump; 73550256Sbde mp->mnt_stat.f_fsid.val[0] = dev2udev(dev); 73638909Sbde mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 73712115Sdyson mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN; 73812115Sdyson mp->mnt_flag |= MNT_LOCAL; 73912115Sdyson ump->um_mountp = mp; 74012115Sdyson ump->um_dev = dev; 74112115Sdyson ump->um_devvp = devvp; 74212115Sdyson /* setting those two parameters allows us to use 74312115Sdyson ufs_bmap w/o changse ! 74412115Sdyson */ 74512115Sdyson ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs); 74612115Sdyson ump->um_bptrtodb = fs->s_es->s_log_block_size + 1; 74712115Sdyson ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs); 74812115Sdyson for (i = 0; i < MAXQUOTAS; i++) 74912115Sdyson ump->um_quotas[i] = NULLVP; 75066886Seivind devvp->v_rdev->si_mountpoint = mp; 75134430Seivind if (ronly == 0) 75234430Seivind ext2_sbupdate(ump, MNT_WAIT); 75312115Sdyson return (0); 75412115Sdysonout: 75512115Sdyson if (bp) 75612115Sdyson brelse(bp); 75712115Sdyson (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p); 75812115Sdyson if (ump) { 75939671Sbde bsd_free(ump->um_e2fs->s_es, M_UFSMNT); 76039671Sbde bsd_free(ump->um_e2fs, M_UFSMNT); 76112115Sdyson bsd_free(ump, M_UFSMNT); 76212115Sdyson mp->mnt_data = (qaddr_t)0; 76312115Sdyson } 76412115Sdyson return (error); 76512115Sdyson} 76612115Sdyson 76712115Sdyson/* 76812115Sdyson * unmount system call 76912115Sdyson */ 77012911Sphkstatic int 77112115Sdysonext2_unmount(mp, mntflags, p) 77212115Sdyson struct mount *mp; 77312115Sdyson int mntflags; 77412115Sdyson struct proc *p; 77512115Sdyson{ 77612115Sdyson register struct ufsmount *ump; 77712115Sdyson register struct ext2_sb_info *fs; 77812115Sdyson int error, flags, ronly, i; 77912115Sdyson 78012115Sdyson flags = 0; 78112115Sdyson if (mntflags & MNT_FORCE) { 78212115Sdyson if (mp->mnt_flag & MNT_ROOTFS) 78312115Sdyson return (EINVAL); 78412115Sdyson flags |= FORCECLOSE; 78512115Sdyson } 78643301Sdillon if ((error = ext2_flushfiles(mp, flags, p)) != 0) 78712115Sdyson return (error); 78812115Sdyson ump = VFSTOUFS(mp); 78912115Sdyson fs = ump->um_e2fs; 79012115Sdyson ronly = fs->s_rd_only; 79139670Sbde if (ronly == 0) { 79239670Sbde if (fs->s_wasvalid) 79339670Sbde fs->s_es->s_state |= EXT2_VALID_FS; 79412115Sdyson ext2_sbupdate(ump, MNT_WAIT); 79512115Sdyson } 79627881Sdyson 79712115Sdyson /* release buffers containing group descriptors */ 79812115Sdyson for(i = 0; i < fs->s_db_per_group; i++) 79927881Sdyson ULCK_BUF(fs->s_group_desc[i]) 80039671Sbde bsd_free(fs->s_group_desc, M_UFSMNT); 80127881Sdyson 80212115Sdyson /* release cached inode/block bitmaps */ 80312115Sdyson for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) 80412115Sdyson if (fs->s_inode_bitmap[i]) 80527881Sdyson ULCK_BUF(fs->s_inode_bitmap[i]) 80627881Sdyson 80712115Sdyson for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) 80812115Sdyson if (fs->s_block_bitmap[i]) 80927881Sdyson ULCK_BUF(fs->s_block_bitmap[i]) 81012115Sdyson 81166886Seivind ump->um_devvp->v_rdev->si_mountpoint = NULL; 81212115Sdyson error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE, 81312115Sdyson NOCRED, p); 81412115Sdyson vrele(ump->um_devvp); 81512115Sdyson bsd_free(fs->s_es, M_UFSMNT); 81612115Sdyson bsd_free(fs, M_UFSMNT); 81712115Sdyson bsd_free(ump, M_UFSMNT); 81812115Sdyson mp->mnt_data = (qaddr_t)0; 81912115Sdyson mp->mnt_flag &= ~MNT_LOCAL; 82012115Sdyson return (error); 82112115Sdyson} 82212115Sdyson 82312115Sdyson/* 82412115Sdyson * Flush out all the files in a filesystem. 82512115Sdyson */ 82612911Sphkstatic int 82712115Sdysonext2_flushfiles(mp, flags, p) 82812115Sdyson register struct mount *mp; 82912115Sdyson int flags; 83012115Sdyson struct proc *p; 83112115Sdyson{ 83212115Sdyson register struct ufsmount *ump; 83312147Sdyson int error; 83412746Sbde#if QUOTA 83512746Sbde int i; 83612746Sbde#endif 83712115Sdyson 83812115Sdyson ump = VFSTOUFS(mp); 83912115Sdyson#if QUOTA 84012115Sdyson if (mp->mnt_flag & MNT_QUOTA) { 84143301Sdillon if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0) 84212115Sdyson return (error); 84312115Sdyson for (i = 0; i < MAXQUOTAS; i++) { 84412115Sdyson if (ump->um_quotas[i] == NULLVP) 84512115Sdyson continue; 84612115Sdyson quotaoff(p, mp, i); 84712115Sdyson } 84812115Sdyson /* 84912115Sdyson * Here we fall through to vflush again to ensure 85012115Sdyson * that we have gotten rid of all the system vnodes. 85112115Sdyson */ 85212115Sdyson } 85312115Sdyson#endif 85412115Sdyson error = vflush(mp, NULLVP, flags); 85512115Sdyson return (error); 85612115Sdyson} 85712115Sdyson 85812115Sdyson/* 85912115Sdyson * Get file system statistics. 86012115Sdyson * taken from ext2/super.c ext2_statfs 86112115Sdyson */ 86212911Sphkstatic int 86312115Sdysonext2_statfs(mp, sbp, p) 86412115Sdyson struct mount *mp; 86512115Sdyson register struct statfs *sbp; 86612115Sdyson struct proc *p; 86712115Sdyson{ 86812115Sdyson unsigned long overhead; 86912115Sdyson register struct ufsmount *ump; 87012115Sdyson register struct ext2_sb_info *fs; 87112115Sdyson register struct ext2_super_block *es; 87268291Sbde int i, nsb; 87312115Sdyson 87412115Sdyson ump = VFSTOUFS(mp); 87512115Sdyson fs = ump->um_e2fs; 87612115Sdyson es = fs->s_es; 87712115Sdyson 87812115Sdyson if (es->s_magic != EXT2_SUPER_MAGIC) 87912115Sdyson panic("ext2_statfs - magic number spoiled"); 88012115Sdyson 88112115Sdyson /* 88212115Sdyson * Compute the overhead (FS structures) 88312115Sdyson */ 88468291Sbde if (es->s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) { 88568291Sbde nsb = 0; 88668291Sbde for (i = 0 ; i < fs->s_groups_count; i++) 88768291Sbde if (ext2_group_sparse(i)) 88868291Sbde nsb++; 88968291Sbde } else 89068291Sbde nsb = fs->s_groups_count; 89112115Sdyson overhead = es->s_first_data_block + 89268291Sbde /* Superblocks and block group descriptors: */ 89368291Sbde nsb * (1 + fs->s_db_per_group) + 89468291Sbde /* Inode bitmap, block bitmap, and inode table: */ 89568291Sbde fs->s_groups_count * (1 + 1 + fs->s_itb_per_group); 89612115Sdyson 89712115Sdyson sbp->f_bsize = EXT2_FRAG_SIZE(fs); 89812115Sdyson sbp->f_iosize = EXT2_BLOCK_SIZE(fs); 89912115Sdyson sbp->f_blocks = es->s_blocks_count - overhead; 90012115Sdyson sbp->f_bfree = es->s_free_blocks_count; 90112115Sdyson sbp->f_bavail = sbp->f_bfree - es->s_r_blocks_count; 90212115Sdyson sbp->f_files = es->s_inodes_count; 90312115Sdyson sbp->f_ffree = es->s_free_inodes_count; 90412115Sdyson if (sbp != &mp->mnt_stat) { 90538909Sbde sbp->f_type = mp->mnt_vfc->vfc_typenum; 90612115Sdyson bcopy((caddr_t)mp->mnt_stat.f_mntonname, 90712115Sdyson (caddr_t)&sbp->f_mntonname[0], MNAMELEN); 90812115Sdyson bcopy((caddr_t)mp->mnt_stat.f_mntfromname, 90912115Sdyson (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); 91012115Sdyson } 91112115Sdyson return (0); 91212115Sdyson} 91312115Sdyson 91412115Sdyson/* 91512115Sdyson * Go through the disk queues to initiate sandbagged IO; 91612115Sdyson * go through the inodes to write those that have been modified; 91712115Sdyson * initiate the writing of the super block if it has been modified. 91812115Sdyson * 91912115Sdyson * Note: we are always called with the filesystem marked `MPBUSY'. 92012115Sdyson */ 92112911Sphkstatic int 92212115Sdysonext2_sync(mp, waitfor, cred, p) 92312115Sdyson struct mount *mp; 92412115Sdyson int waitfor; 92512115Sdyson struct ucred *cred; 92612115Sdyson struct proc *p; 92712115Sdyson{ 92839678Sbde struct vnode *nvp, *vp; 92939678Sbde struct inode *ip; 93039678Sbde struct ufsmount *ump = VFSTOUFS(mp); 93139678Sbde struct ext2_sb_info *fs; 93212115Sdyson int error, allerror = 0; 93312115Sdyson 93412115Sdyson fs = ump->um_e2fs; 93539678Sbde if (fs->s_dirt != 0 && fs->s_rd_only != 0) { /* XXX */ 93639678Sbde printf("fs = %s\n", fs->fs_fsmnt); 93739678Sbde panic("ext2_sync: rofs mod"); 93812115Sdyson } 93912115Sdyson /* 94012115Sdyson * Write back each (modified) inode. 94112115Sdyson */ 94271576Sjasone mtx_enter(&mntvnode_mtx, MTX_DEF); 94312115Sdysonloop: 94439678Sbde for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { 94512115Sdyson /* 94612115Sdyson * If the vnode that we are about to sync is no longer 94712115Sdyson * associated with this mount point, start over. 94812115Sdyson */ 94912115Sdyson if (vp->v_mount != mp) 95012115Sdyson goto loop; 95166615Sjasone mtx_enter(&vp->v_interlock, MTX_DEF); 95239678Sbde nvp = vp->v_mntvnodes.le_next; 95312115Sdyson ip = VTOI(vp); 95439678Sbde if (vp->v_type == VNON || 95543309Sdillon ((ip->i_flag & 95612115Sdyson (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 && 95743395Sbde (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) { 95866615Sjasone mtx_exit(&vp->v_interlock, MTX_DEF); 95912115Sdyson continue; 96039678Sbde } 96171576Sjasone mtx_exit(&mntvnode_mtx, MTX_DEF); 96239678Sbde error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p); 96339678Sbde if (error) { 96471576Sjasone mtx_enter(&mntvnode_mtx, MTX_DEF); 96539678Sbde if (error == ENOENT) 96639678Sbde goto loop; 96739678Sbde continue; 96839678Sbde } 96943301Sdillon if ((error = VOP_FSYNC(vp, cred, waitfor, p)) != 0) 97012115Sdyson allerror = error; 97139678Sbde VOP_UNLOCK(vp, 0, p); 97239678Sbde vrele(vp); 97371576Sjasone mtx_enter(&mntvnode_mtx, MTX_DEF); 97412115Sdyson } 97571576Sjasone mtx_exit(&mntvnode_mtx, MTX_DEF); 97612115Sdyson /* 97712115Sdyson * Force stale file system control information to be flushed. 97812115Sdyson */ 97939678Sbde if (waitfor != MNT_LAZY) { 98039678Sbde vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p); 98139678Sbde if ((error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p)) != 0) 98239678Sbde allerror = error; 98339678Sbde VOP_UNLOCK(ump->um_devvp, 0, p); 98439678Sbde } 98512115Sdyson#if QUOTA 98612115Sdyson qsync(mp); 98712115Sdyson#endif 98839678Sbde /* 98939678Sbde * Write back modified superblock. 99039678Sbde */ 99139678Sbde if (fs->s_dirt != 0) { 99239678Sbde fs->s_dirt = 0; 99339678Sbde fs->s_es->s_wtime = time_second; 99439678Sbde if ((error = ext2_sbupdate(ump, waitfor)) != 0) 99539678Sbde allerror = error; 99639678Sbde } 99712115Sdyson return (allerror); 99812115Sdyson} 99912115Sdyson 100012115Sdyson/* 100112115Sdyson * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it 100212115Sdyson * in from disk. If it is in core, wait for the lock bit to clear, then 100312115Sdyson * return the inode locked. Detection and handling of mount points must be 100412115Sdyson * done by the calling routine. 100512115Sdyson */ 100612911Sphkstatic int 100712115Sdysonext2_vget(mp, ino, vpp) 100812115Sdyson struct mount *mp; 100912115Sdyson ino_t ino; 101012115Sdyson struct vnode **vpp; 101112115Sdyson{ 101212115Sdyson register struct ext2_sb_info *fs; 101312115Sdyson register struct inode *ip; 101412115Sdyson struct ufsmount *ump; 101512115Sdyson struct buf *bp; 101612115Sdyson struct vnode *vp; 101712115Sdyson dev_t dev; 101830280Sphk int i, error; 101912115Sdyson int used_blocks; 102012115Sdyson 102112115Sdyson ump = VFSTOUFS(mp); 102212115Sdyson dev = ump->um_dev; 102312406Sdysonrestart: 102412115Sdyson if ((*vpp = ufs_ihashget(dev, ino)) != NULL) 102512115Sdyson return (0); 102612115Sdyson 102712406Sdyson /* 102812406Sdyson * Lock out the creation of new entries in the FFS hash table in 102912406Sdyson * case getnewvnode() or MALLOC() blocks, otherwise a duplicate 103012406Sdyson * may occur! 103112406Sdyson */ 103212406Sdyson if (ext2fs_inode_hash_lock) { 103312406Sdyson while (ext2fs_inode_hash_lock) { 103412406Sdyson ext2fs_inode_hash_lock = -1; 103536102Sbde tsleep(&ext2fs_inode_hash_lock, PVM, "e2vget", 0); 103612406Sdyson } 103712406Sdyson goto restart; 103812406Sdyson } 103912406Sdyson ext2fs_inode_hash_lock = 1; 104012406Sdyson 104136102Sbde /* 104236102Sbde * If this MALLOC() is performed after the getnewvnode() 104336102Sbde * it might block, leaving a vnode with a NULL v_data to be 104436102Sbde * found by ext2_sync() if a sync happens to fire right then, 104536102Sbde * which will cause a panic because ext2_sync() blindly 104636102Sbde * dereferences vp->v_data (as well it should). 104736102Sbde */ 104836102Sbde MALLOC(ip, struct inode *, sizeof(struct inode), M_EXT2NODE, M_WAITOK); 104936102Sbde 105012115Sdyson /* Allocate a new vnode/inode. */ 105143301Sdillon if ((error = getnewvnode(VT_UFS, mp, ext2_vnodeop_p, &vp)) != 0) { 105236102Sbde if (ext2fs_inode_hash_lock < 0) 105336102Sbde wakeup(&ext2fs_inode_hash_lock); 105436102Sbde ext2fs_inode_hash_lock = 0; 105512115Sdyson *vpp = NULL; 105636102Sbde FREE(ip, M_EXT2NODE); 105712115Sdyson return (error); 105812115Sdyson } 105912115Sdyson bzero((caddr_t)ip, sizeof(struct inode)); 106066378Sbp lockinit(&vp->v_lock, PINOD, "ext2in", 0, 0); 106112115Sdyson vp->v_data = ip; 106212115Sdyson ip->i_vnode = vp; 106312115Sdyson ip->i_e2fs = fs = ump->um_e2fs; 106412115Sdyson ip->i_dev = dev; 106512115Sdyson ip->i_number = ino; 106612115Sdyson#if QUOTA 106712115Sdyson for (i = 0; i < MAXQUOTAS; i++) 106812115Sdyson ip->i_dquot[i] = NODQUOT; 106912115Sdyson#endif 107012115Sdyson /* 107112115Sdyson * Put it onto its hash chain and lock it so that other requests for 107212115Sdyson * this inode will block if they arrive while we are sleeping waiting 107312115Sdyson * for old data structures to be purged or for the contents of the 107412115Sdyson * disk portion of this inode to be read. 107512115Sdyson */ 107612115Sdyson ufs_ihashins(ip); 107712115Sdyson 107812406Sdyson if (ext2fs_inode_hash_lock < 0) 107912406Sdyson wakeup(&ext2fs_inode_hash_lock); 108012406Sdyson ext2fs_inode_hash_lock = 0; 108112406Sdyson 108212115Sdyson /* Read in the disk contents for the inode, copy into the inode. */ 108312115Sdyson#if 0 108412115Sdysonprintf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino))); 108512115Sdyson#endif 108643301Sdillon if ((error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)), 108743301Sdillon (int)fs->s_blocksize, NOCRED, &bp)) != 0) { 108812115Sdyson /* 108912115Sdyson * The inode does not contain anything useful, so it would 109012115Sdyson * be misleading to leave it on its hash chain. With mode 109112115Sdyson * still zero, it will be unlinked and returned to the free 109212115Sdyson * list by vput(). 109312115Sdyson */ 109412115Sdyson vput(vp); 109512115Sdyson brelse(bp); 109612115Sdyson *vpp = NULL; 109712115Sdyson return (error); 109812115Sdyson } 109912115Sdyson /* convert ext2 inode to dinode */ 110012115Sdyson ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE * 110112115Sdyson ino_to_fsbo(fs, ino)), &ip->i_din); 110212115Sdyson ip->i_block_group = ino_to_cg(fs, ino); 110312115Sdyson ip->i_next_alloc_block = 0; 110412115Sdyson ip->i_next_alloc_goal = 0; 110512115Sdyson ip->i_prealloc_count = 0; 110612115Sdyson ip->i_prealloc_block = 0; 110712115Sdyson /* now we want to make sure that block pointers for unused 110812115Sdyson blocks are zeroed out - ext2_balloc depends on this 110912115Sdyson although for regular files and directories only 111012115Sdyson */ 111112115Sdyson if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) { 111212115Sdyson used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize; 111312115Sdyson for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++) 111412115Sdyson ip->i_db[i] = 0; 111512115Sdyson } 111612115Sdyson/* 111712115Sdyson ext2_print_inode(ip); 111812115Sdyson*/ 111912115Sdyson brelse(bp); 112012115Sdyson 112112115Sdyson /* 112212115Sdyson * Initialize the vnode from the inode, check for aliases. 112312115Sdyson * Note that the underlying vnode may have changed. 112412115Sdyson */ 112543301Sdillon if ((error = ufs_vinit(mp, ext2_specop_p, ext2_fifoop_p, &vp)) != 0) { 112612115Sdyson vput(vp); 112712115Sdyson *vpp = NULL; 112812115Sdyson return (error); 112912115Sdyson } 113012115Sdyson /* 113112115Sdyson * Finish inode initialization now that aliasing has been resolved. 113212115Sdyson */ 113312115Sdyson ip->i_devvp = ump->um_devvp; 113412115Sdyson VREF(ip->i_devvp); 113512115Sdyson /* 113612115Sdyson * Set up a generation number for this inode if it does not 113712115Sdyson * already have one. This should only happen on old filesystems. 113812115Sdyson */ 113912115Sdyson if (ip->i_gen == 0) { 114031485Sbde ip->i_gen = random() / 2 + 1; 114112115Sdyson if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) 114212115Sdyson ip->i_flag |= IN_MODIFIED; 114312115Sdyson } 114412115Sdyson *vpp = vp; 114512115Sdyson return (0); 114612115Sdyson} 114712115Sdyson 114812115Sdyson/* 114912115Sdyson * File handle to vnode 115012115Sdyson * 115112115Sdyson * Have to be really careful about stale file handles: 115212115Sdyson * - check that the inode number is valid 115312115Sdyson * - call ext2_vget() to get the locked inode 115412115Sdyson * - check for an unallocated inode (i_mode == 0) 115512115Sdyson * - check that the given client host has export rights and return 115612115Sdyson * those rights via. exflagsp and credanonp 115712115Sdyson */ 115812911Sphkstatic int 115951138Salfredext2_fhtovp(mp, fhp, vpp) 116012115Sdyson register struct mount *mp; 116112115Sdyson struct fid *fhp; 116212115Sdyson struct vnode **vpp; 116312115Sdyson{ 116412115Sdyson register struct ufid *ufhp; 116512115Sdyson struct ext2_sb_info *fs; 116612115Sdyson 116712115Sdyson ufhp = (struct ufid *)fhp; 116812115Sdyson fs = VFSTOUFS(mp)->um_e2fs; 116912115Sdyson if (ufhp->ufid_ino < ROOTINO || 117012115Sdyson ufhp->ufid_ino >= fs->s_groups_count * fs->s_es->s_inodes_per_group) 117112115Sdyson return (ESTALE); 117251138Salfred return (ufs_fhtovp(mp, ufhp, vpp)); 117312115Sdyson} 117412115Sdyson 117512115Sdyson/* 117612115Sdyson * Vnode pointer to File handle 117712115Sdyson */ 117812115Sdyson/* ARGSUSED */ 117912911Sphkstatic int 118012115Sdysonext2_vptofh(vp, fhp) 118112115Sdyson struct vnode *vp; 118212115Sdyson struct fid *fhp; 118312115Sdyson{ 118412115Sdyson register struct inode *ip; 118512115Sdyson register struct ufid *ufhp; 118612115Sdyson 118712115Sdyson ip = VTOI(vp); 118812115Sdyson ufhp = (struct ufid *)fhp; 118912115Sdyson ufhp->ufid_len = sizeof(struct ufid); 119012115Sdyson ufhp->ufid_ino = ip->i_number; 119112115Sdyson ufhp->ufid_gen = ip->i_gen; 119212115Sdyson return (0); 119312115Sdyson} 119412115Sdyson 119512115Sdyson/* 119612115Sdyson * Write a superblock and associated information back to disk. 119712115Sdyson */ 119812911Sphkstatic int 119912115Sdysonext2_sbupdate(mp, waitfor) 120012115Sdyson struct ufsmount *mp; 120112115Sdyson int waitfor; 120212115Sdyson{ 120312115Sdyson register struct ext2_sb_info *fs = mp->um_e2fs; 120412115Sdyson register struct ext2_super_block *es = fs->s_es; 120512115Sdyson register struct buf *bp; 120641591Sarchie int error = 0; 120712115Sdyson/* 120812115Sdysonprintf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no"); 120912115Sdyson*/ 121012115Sdyson bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0); 121112115Sdyson bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block)); 121212115Sdyson if (waitfor == MNT_WAIT) 121312115Sdyson error = bwrite(bp); 121412115Sdyson else 121512115Sdyson bawrite(bp); 121612115Sdyson 121727881Sdyson /* 121827881Sdyson * The buffers for group descriptors, inode bitmaps and block bitmaps 121927881Sdyson * are not busy at this point and are (hopefully) written by the 122027881Sdyson * usual sync mechanism. No need to write them here 122112115Sdyson */ 122212115Sdyson 122312115Sdyson return (error); 122412115Sdyson} 1225