ext2_vfsops.c revision 143663
1139778Simp/*- 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 */ 7139778Simp/*- 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 * 4. Neither the name of the University nor the names of its contributors 2012115Sdyson * may be used to endorse or promote products derived from this software 2112115Sdyson * without specific prior written permission. 2212115Sdyson * 2312115Sdyson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2412115Sdyson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2512115Sdyson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2612115Sdyson * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2712115Sdyson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2812115Sdyson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2912115Sdyson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3012115Sdyson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3112115Sdyson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3212115Sdyson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3312115Sdyson * SUCH DAMAGE. 3412115Sdyson * 3512115Sdyson * @(#)ffs_vfsops.c 8.8 (Berkeley) 4/18/94 3693016Sbde * $FreeBSD: head/sys/gnu/fs/ext2fs/ext2_vfsops.c 143663 2005-03-15 20:00:03Z phk $ 3712115Sdyson */ 3812115Sdyson 3912115Sdyson#include <sys/param.h> 4012115Sdyson#include <sys/systm.h> 4112115Sdyson#include <sys/namei.h> 4212115Sdyson#include <sys/proc.h> 4312115Sdyson#include <sys/kernel.h> 4412115Sdyson#include <sys/vnode.h> 4512115Sdyson#include <sys/mount.h> 4660041Sphk#include <sys/bio.h> 4712115Sdyson#include <sys/buf.h> 4829906Skato#include <sys/conf.h> 4924131Sbde#include <sys/fcntl.h> 5012115Sdyson#include <sys/malloc.h> 5112115Sdyson#include <sys/stat.h> 5271576Sjasone#include <sys/mutex.h> 5312115Sdyson 54137039Sphk#include <geom/geom.h> 55137039Sphk#include <geom/geom_vfs.h> 56137039Sphk 5796749Siedowse#include <gnu/ext2fs/ext2_mount.h> 5896749Siedowse#include <gnu/ext2fs/inode.h> 5912115Sdyson 6012115Sdyson#include <gnu/ext2fs/fs.h> 6112115Sdyson#include <gnu/ext2fs/ext2_extern.h> 6212115Sdyson#include <gnu/ext2fs/ext2_fs.h> 6312115Sdyson#include <gnu/ext2fs/ext2_fs_sb.h> 6412115Sdyson 6592728Salfredstatic int ext2_flushfiles(struct mount *mp, int flags, struct thread *td); 6692728Salfredstatic int ext2_mountfs(struct vnode *, struct mount *, struct thread *); 67140736Sphkstatic int ext2_reload(struct mount *mp, struct thread *td); 6896749Siedowsestatic int ext2_sbupdate(struct ext2mount *, int); 6912115Sdyson 70116271Sphkstatic vfs_unmount_t ext2_unmount; 71116271Sphkstatic vfs_root_t ext2_root; 72116271Sphkstatic vfs_statfs_t ext2_statfs; 73116271Sphkstatic vfs_sync_t ext2_sync; 74116271Sphkstatic vfs_vget_t ext2_vget; 75116271Sphkstatic vfs_fhtovp_t ext2_fhtovp; 76116271Sphkstatic vfs_vptofh_t ext2_vptofh; 77132902Sphkstatic vfs_mount_t ext2_mount; 78116271Sphk 7996749SiedowseMALLOC_DEFINE(M_EXT2NODE, "EXT2 node", "EXT2 vnode private part"); 8096749Siedowsestatic MALLOC_DEFINE(M_EXT2MNT, "EXT2 mount", "EXT2 mount structure"); 8130280Sphk 8212911Sphkstatic struct vfsops ext2fs_vfsops = { 83116271Sphk .vfs_fhtovp = ext2_fhtovp, 84132902Sphk .vfs_mount = ext2_mount, 85116271Sphk .vfs_root = ext2_root, /* root inode via vget */ 86116271Sphk .vfs_statfs = ext2_statfs, 87116271Sphk .vfs_sync = ext2_sync, 88116271Sphk .vfs_unmount = ext2_unmount, 89116271Sphk .vfs_vget = ext2_vget, 90116271Sphk .vfs_vptofh = ext2_vptofh, 9112115Sdyson}; 9212115Sdyson 9338909SbdeVFS_SET(ext2fs_vfsops, ext2fs, 0); 94137039Sphk 9512115Sdyson#define bsd_malloc malloc 9612115Sdyson#define bsd_free free 9712115Sdyson 98130585Sphkstatic int ext2_check_sb_compat(struct ext2_super_block *es, struct cdev *dev, 9993014Sbde int ronly); 10092728Salfredstatic int compute_sb_data(struct vnode * devvp, 10193014Sbde struct ext2_super_block * es, struct ext2_sb_info * fs); 10216322Sgpalmer 103138493Sphkstatic const char *ext2_opts[] = { "from", "export" }; 10412115Sdyson/* 10512115Sdyson * VFS Operations. 10612115Sdyson * 10712115Sdyson * mount system call 10812115Sdyson */ 10912911Sphkstatic int 110132902Sphkext2_mount(mp, td) 11196752Siedowse struct mount *mp; 11283366Sjulian struct thread *td; 11312115Sdyson{ 11497255Smux struct export_args *export; 11597255Smux struct vfsoptlist *opts; 11612115Sdyson struct vnode *devvp; 11796749Siedowse struct ext2mount *ump = 0; 11896752Siedowse struct ext2_sb_info *fs; 11997255Smux char *path, *fspec; 12097255Smux int error, flags, len; 12139028Sbde mode_t accessmode; 122132902Sphk struct nameidata nd, *ndp = &nd; 12312115Sdyson 12497255Smux opts = mp->mnt_optnew; 12597255Smux 126138493Sphk if (vfs_filteropt(opts, ext2_opts)) 127138493Sphk return (EINVAL); 128138493Sphk 12997255Smux vfs_getopt(opts, "fspath", (void **)&path, NULL); 13073286Sadrian /* Double-check the length of path.. */ 13173286Sadrian if (strlen(path) >= MAXMNTLEN - 1) 13273286Sadrian return (ENAMETOOLONG); 13397255Smux 13497255Smux fspec = NULL; 13597255Smux error = vfs_getopt(opts, "from", (void **)&fspec, &len); 13697255Smux if (!error && fspec[len - 1] != '\0') 13797255Smux return (EINVAL); 13897255Smux 13912115Sdyson /* 14012115Sdyson * If updating, check whether changing from read-only to 14112115Sdyson * read/write; if there is no device name, that's all we do. 14212115Sdyson */ 14312115Sdyson if (mp->mnt_flag & MNT_UPDATE) { 14496749Siedowse ump = VFSTOEXT2(mp); 14512115Sdyson fs = ump->um_e2fs; 14612115Sdyson error = 0; 147138493Sphk if (fs->s_rd_only == 0 && 148138493Sphk vfs_flagopt(opts, "ro", NULL, 0)) { 149140048Sphk error = VFS_SYNC(mp, MNT_WAIT, td); 150137039Sphk if (error) 151137039Sphk return (error); 15212115Sdyson flags = WRITECLOSE; 15312115Sdyson if (mp->mnt_flag & MNT_FORCE) 15412115Sdyson flags |= FORCECLOSE; 15583366Sjulian if (vfs_busy(mp, LK_NOWAIT, 0, td)) 15612115Sdyson return (EBUSY); 15783366Sjulian error = ext2_flushfiles(mp, flags, td); 15883366Sjulian vfs_unbusy(mp, td); 15939670Sbde if (!error && fs->s_wasvalid) { 16039670Sbde fs->s_es->s_state |= EXT2_VALID_FS; 16139670Sbde ext2_sbupdate(ump, MNT_WAIT); 16239670Sbde } 16339670Sbde fs->s_rd_only = 1; 164138493Sphk vfs_flagopt(opts, "ro", &mp->mnt_flag, MNT_RDONLY); 165137039Sphk DROP_GIANT(); 166137039Sphk g_topology_lock(); 167137039Sphk g_access(ump->um_cp, 0, -1, 0); 168137039Sphk g_topology_unlock(); 169137039Sphk PICKUP_GIANT(); 17012115Sdyson } 17112115Sdyson if (!error && (mp->mnt_flag & MNT_RELOAD)) 172140736Sphk error = ext2_reload(mp, td); 17312115Sdyson if (error) 17412115Sdyson return (error); 17557839Sbde devvp = ump->um_devvp; 176138493Sphk if (fs->s_rd_only && !vfs_flagopt(opts, "ro", NULL, 0)) { 177138493Sphk if (ext2_check_sb_compat(fs->s_es, devvp->v_rdev, 0)) 178138493Sphk return (EPERM); 17939028Sbde /* 18039028Sbde * If upgrade to read-write by non-root, then verify 18139028Sbde * that user has necessary permissions on the device. 18239028Sbde */ 18393593Sjhb if (suser(td)) { 18483366Sjulian vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 18543301Sdillon if ((error = VOP_ACCESS(devvp, VREAD | VWRITE, 18691406Sjhb td->td_ucred, td)) != 0) { 18783366Sjulian VOP_UNLOCK(devvp, 0, td); 18839028Sbde return (error); 18939028Sbde } 19083366Sjulian VOP_UNLOCK(devvp, 0, td); 19139028Sbde } 192137039Sphk DROP_GIANT(); 193137039Sphk g_topology_lock(); 194137039Sphk error = g_access(ump->um_cp, 0, 1, 0); 195137039Sphk g_topology_unlock(); 196137039Sphk PICKUP_GIANT(); 197137039Sphk if (error) 198137039Sphk return (error); 19939028Sbde 20039670Sbde if ((fs->s_es->s_state & EXT2_VALID_FS) == 0 || 20139670Sbde (fs->s_es->s_state & EXT2_ERROR_FS)) { 20239670Sbde if (mp->mnt_flag & MNT_FORCE) { 20339670Sbde printf( 20439670Sbde"WARNING: %s was not properly dismounted\n", 20539670Sbde fs->fs_fsmnt); 20639670Sbde } else { 20739670Sbde printf( 20839670Sbde"WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n", 20939670Sbde fs->fs_fsmnt); 21039670Sbde return (EPERM); 21139670Sbde } 21239670Sbde } 21312115Sdyson fs->s_es->s_state &= ~EXT2_VALID_FS; 21412115Sdyson ext2_sbupdate(ump, MNT_WAIT); 21539670Sbde fs->s_rd_only = 0; 216138493Sphk mp->mnt_flag &= ~MNT_RDONLY; 21712115Sdyson } 21897255Smux if (fspec == NULL) { 21997255Smux error = vfs_getopt(opts, "export", (void **)&export, 22097255Smux &len); 22197255Smux if (error || len != sizeof(struct export_args)) 22297255Smux return (EINVAL); 22397255Smux /* Process export requests. */ 22497255Smux return (vfs_export(mp, export)); 22512115Sdyson } 22612115Sdyson } 22712115Sdyson /* 22812115Sdyson * Not an update, or updating the name: look up the name 229125786Sbde * and verify that it refers to a sensible disk device. 23012115Sdyson */ 23197255Smux if (fspec == NULL) 23297255Smux return (EINVAL); 23397255Smux NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, td); 23443301Sdillon if ((error = namei(ndp)) != 0) 23512115Sdyson return (error); 23654655Seivind NDFREE(ndp, NDF_ONLY_PNBUF); 23712115Sdyson devvp = ndp->ni_vp; 23812115Sdyson 23955756Sphk if (!vn_isdisk(devvp, &error)) { 24012115Sdyson vrele(devvp); 24155756Sphk return (error); 24212115Sdyson } 24339028Sbde 24439028Sbde /* 24539028Sbde * If mount by non-root, then verify that user has necessary 24639028Sbde * permissions on the device. 24739028Sbde */ 24893593Sjhb if (suser(td)) { 24939028Sbde accessmode = VREAD; 25039028Sbde if ((mp->mnt_flag & MNT_RDONLY) == 0) 25139028Sbde accessmode |= VWRITE; 25283366Sjulian vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 25391406Sjhb if ((error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td)) != 0) { 25439028Sbde vput(devvp); 25539028Sbde return (error); 25639028Sbde } 25783366Sjulian VOP_UNLOCK(devvp, 0, td); 25839028Sbde } 25939028Sbde 26029888Skato if ((mp->mnt_flag & MNT_UPDATE) == 0) { 26183366Sjulian error = ext2_mountfs(devvp, mp, td); 26229888Skato } else { 26312115Sdyson if (devvp != ump->um_devvp) 26412115Sdyson error = EINVAL; /* needs translation */ 26512115Sdyson else 26612115Sdyson vrele(devvp); 26712115Sdyson } 26812115Sdyson if (error) { 26912115Sdyson vrele(devvp); 27012115Sdyson return (error); 27112115Sdyson } 27296749Siedowse ump = VFSTOEXT2(mp); 27312115Sdyson fs = ump->um_e2fs; 27473286Sadrian /* 27573286Sadrian * Note that this strncpy() is ok because of a check at the start 27673286Sadrian * of ext2_mount(). 27773286Sadrian */ 27873286Sadrian strncpy(fs->fs_fsmnt, path, MAXMNTLEN); 27973286Sadrian fs->fs_fsmnt[MAXMNTLEN - 1] = '\0'; 280138493Sphk vfs_mountedfrom(mp, fspec); 28112115Sdyson return (0); 28212115Sdyson} 28312115Sdyson 28412115Sdyson/* 28512115Sdyson * checks that the data in the descriptor blocks make sense 28612115Sdyson * this is taken from ext2/super.c 28712115Sdyson */ 28812115Sdysonstatic int ext2_check_descriptors (struct ext2_sb_info * sb) 28912115Sdyson{ 29012115Sdyson int i; 29112115Sdyson int desc_block = 0; 29212115Sdyson unsigned long block = sb->s_es->s_first_data_block; 29312115Sdyson struct ext2_group_desc * gdp = NULL; 29412115Sdyson 29512115Sdyson /* ext2_debug ("Checking group descriptors"); */ 29612115Sdyson 29712115Sdyson for (i = 0; i < sb->s_groups_count; i++) 29812115Sdyson { 29912115Sdyson /* examine next descriptor block */ 30012115Sdyson if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0) 30112115Sdyson gdp = (struct ext2_group_desc *) 30212115Sdyson sb->s_group_desc[desc_block++]->b_data; 30312115Sdyson if (gdp->bg_block_bitmap < block || 30412115Sdyson gdp->bg_block_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb)) 30512115Sdyson { 30612115Sdyson printf ("ext2_check_descriptors: " 30712115Sdyson "Block bitmap for group %d" 30839671Sbde " not in group (block %lu)!\n", 30912115Sdyson i, (unsigned long) gdp->bg_block_bitmap); 31012115Sdyson return 0; 31112115Sdyson } 31212115Sdyson if (gdp->bg_inode_bitmap < block || 31312115Sdyson gdp->bg_inode_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb)) 31412115Sdyson { 31512115Sdyson printf ("ext2_check_descriptors: " 31612115Sdyson "Inode bitmap for group %d" 31739671Sbde " not in group (block %lu)!\n", 31812115Sdyson i, (unsigned long) gdp->bg_inode_bitmap); 31912115Sdyson return 0; 32012115Sdyson } 32112115Sdyson if (gdp->bg_inode_table < block || 32212115Sdyson gdp->bg_inode_table + sb->s_itb_per_group >= 32312115Sdyson block + EXT2_BLOCKS_PER_GROUP(sb)) 32412115Sdyson { 32512115Sdyson printf ("ext2_check_descriptors: " 32612115Sdyson "Inode table for group %d" 32739671Sbde " not in group (block %lu)!\n", 32812115Sdyson i, (unsigned long) gdp->bg_inode_table); 32912115Sdyson return 0; 33012115Sdyson } 33112115Sdyson block += EXT2_BLOCKS_PER_GROUP(sb); 33212115Sdyson gdp++; 33312115Sdyson } 33412115Sdyson return 1; 33512115Sdyson} 33612115Sdyson 33755313Sbdestatic int 33855313Sbdeext2_check_sb_compat(es, dev, ronly) 33955313Sbde struct ext2_super_block *es; 340130585Sphk struct cdev *dev; 34155313Sbde int ronly; 34255313Sbde{ 34355313Sbde 34455313Sbde if (es->s_magic != EXT2_SUPER_MAGIC) { 34555313Sbde printf("ext2fs: %s: wrong magic number %#x (expected %#x)\n", 34655313Sbde devtoname(dev), es->s_magic, EXT2_SUPER_MAGIC); 34755313Sbde return (1); 34855313Sbde } 34955313Sbde if (es->s_rev_level > EXT2_GOOD_OLD_REV) { 35055313Sbde if (es->s_feature_incompat & ~EXT2_FEATURE_INCOMPAT_SUPP) { 35155313Sbde printf( 35255313Sbde"WARNING: mount of %s denied due to unsupported optional features\n", 35355313Sbde devtoname(dev)); 35455313Sbde return (1); 35555313Sbde } 35655313Sbde if (!ronly && 35755313Sbde (es->s_feature_ro_compat & ~EXT2_FEATURE_RO_COMPAT_SUPP)) { 35855313Sbde printf( 35955313Sbde"WARNING: R/W mount of %s denied due to unsupported optional features\n", 36055313Sbde devtoname(dev)); 36155313Sbde return (1); 36255313Sbde } 36355313Sbde } 36455313Sbde return (0); 36555313Sbde} 36655313Sbde 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 *), 425111119Simp M_EXT2MNT, 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]); 44096749Siedowse bsd_free(fs->s_group_desc, M_EXT2MNT); 44112115Sdyson printf("EXT2-fs: unable to read group descriptors (%d)\n", error); 44212115Sdyson return EIO; 44312115Sdyson } 44427881Sdyson LCK_BUF(fs->s_group_desc[i]) 44512115Sdyson } 44612115Sdyson if(!ext2_check_descriptors(fs)) { 44712115Sdyson for (j = 0; j < db_count; j++) 44827881Sdyson ULCK_BUF(fs->s_group_desc[j]) 44996749Siedowse bsd_free(fs->s_group_desc, M_EXT2MNT); 45012115Sdyson printf("EXT2-fs: (ext2_check_descriptors failure) " 45112115Sdyson "unable to read group descriptors\n"); 45212115Sdyson return EIO; 45312115Sdyson } 45412115Sdyson 45512115Sdyson for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) { 45612115Sdyson fs->s_inode_bitmap_number[i] = 0; 45712115Sdyson fs->s_inode_bitmap[i] = NULL; 45812115Sdyson fs->s_block_bitmap_number[i] = 0; 45912115Sdyson fs->s_block_bitmap[i] = NULL; 46012115Sdyson } 46112115Sdyson fs->s_loaded_inode_bitmaps = 0; 46212115Sdyson fs->s_loaded_block_bitmaps = 0; 463125991Stjr if (es->s_rev_level == EXT2_GOOD_OLD_REV || (es->s_feature_ro_compat & 464125991Stjr EXT2_FEATURE_RO_COMPAT_LARGE_FILE) == 0) 465125991Stjr fs->fs_maxfilesize = 0x7fffffff; 466125991Stjr else 467125991Stjr fs->fs_maxfilesize = 0x7fffffffffffffff; 46812115Sdyson return 0; 46912115Sdyson} 47012115Sdyson 47112115Sdyson/* 47212115Sdyson * Reload all incore data for a filesystem (used after running fsck on 47312115Sdyson * the root filesystem and finding things to fix). The filesystem must 47412115Sdyson * be mounted read-only. 47512115Sdyson * 47612115Sdyson * Things to do to update the mount: 47712115Sdyson * 1) invalidate all cached meta-data. 47812115Sdyson * 2) re-read superblock from disk. 47912115Sdyson * 3) re-read summary information from disk. 48012115Sdyson * 4) invalidate all inactive vnodes. 48112115Sdyson * 5) invalidate all cached file data. 48212115Sdyson * 6) re-read inode data for all active vnodes. 48312115Sdyson */ 48412911Sphkstatic int 485140736Sphkext2_reload(struct mount *mp, struct thread *td) 48612115Sdyson{ 48796752Siedowse struct vnode *vp, *nvp, *devvp; 48812115Sdyson struct inode *ip; 48912115Sdyson struct buf *bp; 49012115Sdyson struct ext2_super_block * es; 49112115Sdyson struct ext2_sb_info *fs; 49212147Sdyson int error; 49312115Sdyson 494122114Sbde if ((mp->mnt_flag & MNT_RDONLY) == 0) 49512115Sdyson return (EINVAL); 49612115Sdyson /* 49712115Sdyson * Step 1: invalidate all cached meta-data. 49812115Sdyson */ 499122114Sbde devvp = VFSTOEXT2(mp)->um_devvp; 500125786Sbde vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 501140220Sphk if (vinvalbuf(devvp, 0, td, 0, 0) != 0) 50212115Sdyson panic("ext2_reload: dirty1"); 503125786Sbde VOP_UNLOCK(devvp, 0, td); 504125786Sbde 50512115Sdyson /* 50612115Sdyson * Step 2: re-read superblock from disk. 50712115Sdyson * constants have been adjusted for ext2 50812115Sdyson */ 50943301Sdillon if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0) 51012115Sdyson return (error); 51112115Sdyson es = (struct ext2_super_block *)bp->b_data; 51255313Sbde if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) { 51312115Sdyson brelse(bp); 51412115Sdyson return (EIO); /* XXX needs translation */ 51512115Sdyson } 516122114Sbde fs = VFSTOEXT2(mp)->um_e2fs; 51712115Sdyson bcopy(bp->b_data, fs->s_es, sizeof(struct ext2_super_block)); 51812115Sdyson 51943301Sdillon if((error = compute_sb_data(devvp, es, fs)) != 0) { 52012115Sdyson brelse(bp); 52112115Sdyson return error; 52212115Sdyson } 52312115Sdyson#ifdef UNKLAR 52412115Sdyson if (fs->fs_sbsize < SBSIZE) 52512115Sdyson bp->b_flags |= B_INVAL; 52612115Sdyson#endif 52712115Sdyson brelse(bp); 52812115Sdyson 52912115Sdysonloop: 530122091Skan MNT_ILOCK(mp); 531131551Sphk MNT_VNODE_FOREACH(vp, mp, nvp) { 532120751Sjeff VI_LOCK(vp); 533143509Sjeff if (vp->v_iflag & VI_DOOMED) { 534120783Sjeff VI_UNLOCK(vp); 535120783Sjeff continue; 536120783Sjeff } 537122091Skan MNT_IUNLOCK(mp); 53812115Sdyson /* 539143509Sjeff * Step 4: invalidate all cached file data. 54012115Sdyson */ 54183366Sjulian if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) { 54212115Sdyson goto loop; 54339678Sbde } 544140220Sphk if (vinvalbuf(vp, 0, td, 0, 0)) 54512115Sdyson panic("ext2_reload: dirty2"); 54612115Sdyson /* 547143509Sjeff * Step 5: re-read inode data for all active vnodes. 54812115Sdyson */ 54912115Sdyson ip = VTOI(vp); 55039678Sbde error = 55112115Sdyson bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 55239678Sbde (int)fs->s_blocksize, NOCRED, &bp); 55339678Sbde if (error) { 554121925Skan VOP_UNLOCK(vp, 0, td); 555121925Skan vrele(vp); 55612115Sdyson return (error); 55712115Sdyson } 55896749Siedowse ext2_ei2i((struct ext2_inode *) ((char *)bp->b_data + 55996749Siedowse EXT2_INODE_SIZE * ino_to_fsbo(fs, ip->i_number)), ip); 56012115Sdyson brelse(bp); 561121925Skan VOP_UNLOCK(vp, 0, td); 562121925Skan vrele(vp); 563122091Skan MNT_ILOCK(mp); 56412115Sdyson } 565122091Skan MNT_IUNLOCK(mp); 56612115Sdyson return (0); 56712115Sdyson} 56812115Sdyson 56912115Sdyson/* 57012115Sdyson * Common code for mount and mountroot 57112115Sdyson */ 57212911Sphkstatic int 57383366Sjulianext2_mountfs(devvp, mp, td) 57496752Siedowse struct vnode *devvp; 57512115Sdyson struct mount *mp; 57683366Sjulian struct thread *td; 57712115Sdyson{ 57896752Siedowse struct ext2mount *ump; 57912115Sdyson struct buf *bp; 58096752Siedowse struct ext2_sb_info *fs; 58112115Sdyson struct ext2_super_block * es; 582130585Sphk struct cdev *dev = devvp->v_rdev; 583137039Sphk struct g_consumer *cp; 584137039Sphk struct bufobj *bo; 58596749Siedowse int error; 58612115Sdyson int ronly; 58712115Sdyson 588138493Sphk ronly = vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0); 589137039Sphk /* XXX: use VOP_ACESS to check FS perms */ 590137039Sphk DROP_GIANT(); 591137039Sphk g_topology_lock(); 592137039Sphk error = g_vfs_open(devvp, &cp, "ext2fs", ronly ? 0 : 1); 593137039Sphk g_topology_unlock(); 594137039Sphk PICKUP_GIANT(); 59583366Sjulian VOP_UNLOCK(devvp, 0, td); 59653059Sphk if (error) 59712115Sdyson return (error); 598137039Sphk bo = &devvp->v_bufobj; 599137039Sphk bo->bo_private = cp; 600137039Sphk bo->bo_ops = g_vfs_bufops; 60193430Sbde if (devvp->v_rdev->si_iosize_max != 0) 60293430Sbde mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max; 60393430Sbde if (mp->mnt_iosize_max > MAXPHYS) 60493430Sbde mp->mnt_iosize_max = MAXPHYS; 60593430Sbde 60612115Sdyson bp = NULL; 60712115Sdyson ump = NULL; 60843301Sdillon if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0) 60912115Sdyson goto out; 61012115Sdyson es = (struct ext2_super_block *)bp->b_data; 61155313Sbde if (ext2_check_sb_compat(es, dev, ronly) != 0) { 61212115Sdyson error = EINVAL; /* XXX needs translation */ 61312115Sdyson goto out; 61412115Sdyson } 61539670Sbde if ((es->s_state & EXT2_VALID_FS) == 0 || 61639670Sbde (es->s_state & EXT2_ERROR_FS)) { 61739670Sbde if (ronly || (mp->mnt_flag & MNT_FORCE)) { 61839670Sbde printf( 61939670Sbde"WARNING: Filesystem was not properly dismounted\n"); 62039670Sbde } else { 62139670Sbde printf( 62239670Sbde"WARNING: R/W mount denied. Filesystem is not clean - run fsck\n"); 62339670Sbde error = EPERM; 62439670Sbde goto out; 62539670Sbde } 62639670Sbde } 627111119Simp ump = bsd_malloc(sizeof *ump, M_EXT2MNT, M_WAITOK); 62812115Sdyson bzero((caddr_t)ump, sizeof *ump); 62912115Sdyson /* I don't know whether this is the right strategy. Note that 630108533Sschweikh we dynamically allocate both an ext2_sb_info and an ext2_super_block 63112115Sdyson while Linux keeps the super block in a locked buffer 63212115Sdyson */ 63312115Sdyson ump->um_e2fs = bsd_malloc(sizeof(struct ext2_sb_info), 634111119Simp M_EXT2MNT, M_WAITOK); 63512115Sdyson ump->um_e2fs->s_es = bsd_malloc(sizeof(struct ext2_super_block), 636111119Simp M_EXT2MNT, M_WAITOK); 63712115Sdyson bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block)); 63839671Sbde if ((error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs))) 63939671Sbde goto out; 64039671Sbde /* 64139671Sbde * We don't free the group descriptors allocated by compute_sb_data() 64239671Sbde * until ext2_unmount(). This is OK since the mount will succeed. 64339671Sbde */ 64412115Sdyson brelse(bp); 64512115Sdyson bp = NULL; 64612115Sdyson fs = ump->um_e2fs; 64712115Sdyson fs->s_rd_only = ronly; /* ronly is set according to mnt_flags */ 64812115Sdyson /* if the fs is not mounted read-only, make sure the super block is 64912115Sdyson always written back on a sync() 65012115Sdyson */ 65139670Sbde fs->s_wasvalid = fs->s_es->s_state & EXT2_VALID_FS ? 1 : 0; 65212115Sdyson if (ronly == 0) { 65312115Sdyson fs->s_dirt = 1; /* mark it modified */ 65412115Sdyson fs->s_es->s_state &= ~EXT2_VALID_FS; /* set fs invalid */ 65512115Sdyson } 65612115Sdyson mp->mnt_data = (qaddr_t)ump; 65750256Sbde mp->mnt_stat.f_fsid.val[0] = dev2udev(dev); 65838909Sbde mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 65912115Sdyson mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN; 66012115Sdyson mp->mnt_flag |= MNT_LOCAL; 66112115Sdyson ump->um_mountp = mp; 66212115Sdyson ump->um_dev = dev; 66312115Sdyson ump->um_devvp = devvp; 664137320Sphk ump->um_bo = &devvp->v_bufobj; 665137320Sphk ump->um_cp = cp; 66696749Siedowse /* setting those two parameters allowed us to use 66712115Sdyson ufs_bmap w/o changse ! 66812115Sdyson */ 66912115Sdyson ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs); 67012115Sdyson ump->um_bptrtodb = fs->s_es->s_log_block_size + 1; 67112115Sdyson ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs); 67234430Seivind if (ronly == 0) 67334430Seivind ext2_sbupdate(ump, MNT_WAIT); 67412115Sdyson return (0); 67512115Sdysonout: 67612115Sdyson if (bp) 67712115Sdyson brelse(bp); 678137039Sphk if (cp != NULL) { 679137039Sphk DROP_GIANT(); 680137039Sphk g_topology_lock(); 681140822Sphk g_vfs_close(cp, td); 682137039Sphk g_topology_unlock(); 683137039Sphk PICKUP_GIANT(); 684137039Sphk } 68512115Sdyson if (ump) { 68696749Siedowse bsd_free(ump->um_e2fs->s_es, M_EXT2MNT); 68796749Siedowse bsd_free(ump->um_e2fs, M_EXT2MNT); 68896749Siedowse bsd_free(ump, M_EXT2MNT); 68912115Sdyson mp->mnt_data = (qaddr_t)0; 69012115Sdyson } 69112115Sdyson return (error); 69212115Sdyson} 69312115Sdyson 69412115Sdyson/* 69512115Sdyson * unmount system call 69612115Sdyson */ 69712911Sphkstatic int 69883366Sjulianext2_unmount(mp, mntflags, td) 69912115Sdyson struct mount *mp; 70012115Sdyson int mntflags; 70183366Sjulian struct thread *td; 70212115Sdyson{ 70396752Siedowse struct ext2mount *ump; 70496752Siedowse struct ext2_sb_info *fs; 70512115Sdyson int error, flags, ronly, i; 70612115Sdyson 70712115Sdyson flags = 0; 70812115Sdyson if (mntflags & MNT_FORCE) { 70912115Sdyson if (mp->mnt_flag & MNT_ROOTFS) 71012115Sdyson return (EINVAL); 71112115Sdyson flags |= FORCECLOSE; 71212115Sdyson } 71383366Sjulian if ((error = ext2_flushfiles(mp, flags, td)) != 0) 71412115Sdyson return (error); 71596749Siedowse ump = VFSTOEXT2(mp); 71612115Sdyson fs = ump->um_e2fs; 71712115Sdyson ronly = fs->s_rd_only; 71839670Sbde if (ronly == 0) { 71939670Sbde if (fs->s_wasvalid) 72039670Sbde fs->s_es->s_state |= EXT2_VALID_FS; 72112115Sdyson ext2_sbupdate(ump, MNT_WAIT); 72212115Sdyson } 72327881Sdyson 72412115Sdyson /* release buffers containing group descriptors */ 72512115Sdyson for(i = 0; i < fs->s_db_per_group; i++) 72627881Sdyson ULCK_BUF(fs->s_group_desc[i]) 72796749Siedowse bsd_free(fs->s_group_desc, M_EXT2MNT); 72827881Sdyson 72912115Sdyson /* release cached inode/block bitmaps */ 73012115Sdyson for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) 73112115Sdyson if (fs->s_inode_bitmap[i]) 73227881Sdyson ULCK_BUF(fs->s_inode_bitmap[i]) 73327881Sdyson 73412115Sdyson for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) 73512115Sdyson if (fs->s_block_bitmap[i]) 73627881Sdyson ULCK_BUF(fs->s_block_bitmap[i]) 73712115Sdyson 738137039Sphk DROP_GIANT(); 739137039Sphk g_topology_lock(); 740140822Sphk g_vfs_close(ump->um_cp, td); 741137039Sphk g_topology_unlock(); 742137039Sphk PICKUP_GIANT(); 74312115Sdyson vrele(ump->um_devvp); 74496749Siedowse bsd_free(fs->s_es, M_EXT2MNT); 74596749Siedowse bsd_free(fs, M_EXT2MNT); 74696749Siedowse bsd_free(ump, M_EXT2MNT); 74712115Sdyson mp->mnt_data = (qaddr_t)0; 74812115Sdyson mp->mnt_flag &= ~MNT_LOCAL; 74912115Sdyson return (error); 75012115Sdyson} 75112115Sdyson 75212115Sdyson/* 75312115Sdyson * Flush out all the files in a filesystem. 75412115Sdyson */ 75512911Sphkstatic int 75683366Sjulianext2_flushfiles(mp, flags, td) 75796752Siedowse struct mount *mp; 75812115Sdyson int flags; 75983366Sjulian struct thread *td; 76012115Sdyson{ 76112147Sdyson int error; 76212115Sdyson 763132023Salfred error = vflush(mp, 0, flags, td); 76412115Sdyson return (error); 76512115Sdyson} 76612115Sdyson 76712115Sdyson/* 76812115Sdyson * Get file system statistics. 76912115Sdyson * taken from ext2/super.c ext2_statfs 77012115Sdyson */ 77112911Sphkstatic int 77283366Sjulianext2_statfs(mp, sbp, td) 77312115Sdyson struct mount *mp; 77496752Siedowse struct statfs *sbp; 77583366Sjulian struct thread *td; 77612115Sdyson{ 77712115Sdyson unsigned long overhead; 77896752Siedowse struct ext2mount *ump; 77996752Siedowse struct ext2_sb_info *fs; 78096752Siedowse struct ext2_super_block *es; 78168291Sbde int i, nsb; 78212115Sdyson 78396749Siedowse ump = VFSTOEXT2(mp); 78412115Sdyson fs = ump->um_e2fs; 78512115Sdyson es = fs->s_es; 78612115Sdyson 78712115Sdyson if (es->s_magic != EXT2_SUPER_MAGIC) 78812115Sdyson panic("ext2_statfs - magic number spoiled"); 78912115Sdyson 79012115Sdyson /* 79112115Sdyson * Compute the overhead (FS structures) 79212115Sdyson */ 79368291Sbde if (es->s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) { 79468291Sbde nsb = 0; 79568291Sbde for (i = 0 ; i < fs->s_groups_count; i++) 79668291Sbde if (ext2_group_sparse(i)) 79768291Sbde nsb++; 79868291Sbde } else 79968291Sbde nsb = fs->s_groups_count; 80012115Sdyson overhead = es->s_first_data_block + 80168291Sbde /* Superblocks and block group descriptors: */ 80268291Sbde nsb * (1 + fs->s_db_per_group) + 80368291Sbde /* Inode bitmap, block bitmap, and inode table: */ 80468291Sbde fs->s_groups_count * (1 + 1 + fs->s_itb_per_group); 80512115Sdyson 80612115Sdyson sbp->f_bsize = EXT2_FRAG_SIZE(fs); 80712115Sdyson sbp->f_iosize = EXT2_BLOCK_SIZE(fs); 80812115Sdyson sbp->f_blocks = es->s_blocks_count - overhead; 80912115Sdyson sbp->f_bfree = es->s_free_blocks_count; 81012115Sdyson sbp->f_bavail = sbp->f_bfree - es->s_r_blocks_count; 81112115Sdyson sbp->f_files = es->s_inodes_count; 81212115Sdyson sbp->f_ffree = es->s_free_inodes_count; 81312115Sdyson return (0); 81412115Sdyson} 81512115Sdyson 81612115Sdyson/* 81712115Sdyson * Go through the disk queues to initiate sandbagged IO; 81812115Sdyson * go through the inodes to write those that have been modified; 81912115Sdyson * initiate the writing of the super block if it has been modified. 82012115Sdyson * 82112115Sdyson * Note: we are always called with the filesystem marked `MPBUSY'. 82212115Sdyson */ 82312911Sphkstatic int 824140048Sphkext2_sync(mp, waitfor, td) 82512115Sdyson struct mount *mp; 82612115Sdyson int waitfor; 82783366Sjulian struct thread *td; 82812115Sdyson{ 82939678Sbde struct vnode *nvp, *vp; 83039678Sbde struct inode *ip; 83196749Siedowse struct ext2mount *ump = VFSTOEXT2(mp); 83239678Sbde struct ext2_sb_info *fs; 83312115Sdyson int error, allerror = 0; 83412115Sdyson 83512115Sdyson fs = ump->um_e2fs; 83639678Sbde if (fs->s_dirt != 0 && fs->s_rd_only != 0) { /* XXX */ 83739678Sbde printf("fs = %s\n", fs->fs_fsmnt); 83839678Sbde panic("ext2_sync: rofs mod"); 83912115Sdyson } 84012115Sdyson /* 84112115Sdyson * Write back each (modified) inode. 84212115Sdyson */ 843122091Skan MNT_ILOCK(mp); 84412115Sdysonloop: 845131551Sphk MNT_VNODE_FOREACH(vp, mp, nvp) { 846120751Sjeff VI_LOCK(vp); 847143509Sjeff if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED)) { 848120783Sjeff VI_UNLOCK(vp); 849120783Sjeff continue; 850120783Sjeff } 851122091Skan MNT_IUNLOCK(mp); 85212115Sdyson ip = VTOI(vp); 853137008Sphk if ((ip->i_flag & 85412115Sdyson (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 && 855136943Sphk (vp->v_bufobj.bo_dirty.bv_cnt == 0 || 856137008Sphk waitfor == MNT_LAZY)) { 857103938Sjeff VI_UNLOCK(vp); 858122091Skan MNT_ILOCK(mp); 85912115Sdyson continue; 86039678Sbde } 86183366Sjulian error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td); 86239678Sbde if (error) { 863122091Skan MNT_ILOCK(mp); 86439678Sbde if (error == ENOENT) 86539678Sbde goto loop; 86639678Sbde continue; 86739678Sbde } 868140048Sphk if ((error = VOP_FSYNC(vp, waitfor, td)) != 0) 86912115Sdyson allerror = error; 870121874Skan VOP_UNLOCK(vp, 0, td); 871121874Skan vrele(vp); 872122091Skan MNT_ILOCK(mp); 87312115Sdyson } 874122091Skan MNT_IUNLOCK(mp); 87512115Sdyson /* 87612115Sdyson * Force stale file system control information to be flushed. 87712115Sdyson */ 87839678Sbde if (waitfor != MNT_LAZY) { 87983366Sjulian vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, td); 880140048Sphk if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0) 88139678Sbde allerror = error; 88283366Sjulian VOP_UNLOCK(ump->um_devvp, 0, td); 88339678Sbde } 88439678Sbde /* 88539678Sbde * Write back modified superblock. 88639678Sbde */ 88739678Sbde if (fs->s_dirt != 0) { 88839678Sbde fs->s_dirt = 0; 88939678Sbde fs->s_es->s_wtime = time_second; 89039678Sbde if ((error = ext2_sbupdate(ump, waitfor)) != 0) 89139678Sbde allerror = error; 89239678Sbde } 89312115Sdyson return (allerror); 89412115Sdyson} 89512115Sdyson 89612115Sdyson/* 897108533Sschweikh * Look up an EXT2FS dinode number to find its incore vnode, otherwise read it 89812115Sdyson * in from disk. If it is in core, wait for the lock bit to clear, then 89912115Sdyson * return the inode locked. Detection and handling of mount points must be 90012115Sdyson * done by the calling routine. 90112115Sdyson */ 90212911Sphkstatic int 90392462Smckusickext2_vget(mp, ino, flags, vpp) 90412115Sdyson struct mount *mp; 90512115Sdyson ino_t ino; 90692462Smckusick int flags; 90712115Sdyson struct vnode **vpp; 90812115Sdyson{ 90996752Siedowse struct ext2_sb_info *fs; 91096752Siedowse struct inode *ip; 91196749Siedowse struct ext2mount *ump; 91212115Sdyson struct buf *bp; 91312115Sdyson struct vnode *vp; 914130585Sphk struct cdev *dev; 91530280Sphk int i, error; 91612115Sdyson int used_blocks; 91712115Sdyson 918143578Sphk error = vfs_hash_get(mp, ino, flags, curthread, vpp); 919143619Sphk if (error || *vpp != NULL) 92092462Smckusick return (error); 92112115Sdyson 922143578Sphk ump = VFSTOEXT2(mp); 923143578Sphk dev = ump->um_dev; 92412406Sdyson 92536102Sbde /* 92636102Sbde * If this MALLOC() is performed after the getnewvnode() 92736102Sbde * it might block, leaving a vnode with a NULL v_data to be 92836102Sbde * found by ext2_sync() if a sync happens to fire right then, 92936102Sbde * which will cause a panic because ext2_sync() blindly 93036102Sbde * dereferences vp->v_data (as well it should). 93136102Sbde */ 932143578Sphk ip = malloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK | M_ZERO); 93336102Sbde 93412115Sdyson /* Allocate a new vnode/inode. */ 935138290Sphk if ((error = getnewvnode("ext2fs", mp, &ext2_vnodeops, &vp)) != 0) { 93612115Sdyson *vpp = NULL; 937143578Sphk free(ip, M_EXT2NODE); 93812115Sdyson return (error); 93912115Sdyson } 94012115Sdyson vp->v_data = ip; 94112115Sdyson ip->i_vnode = vp; 94212115Sdyson ip->i_e2fs = fs = ump->um_e2fs; 94312115Sdyson ip->i_dev = dev; 94412115Sdyson ip->i_number = ino; 945143578Sphk 946143619Sphk error = vfs_hash_insert(vp, ino, flags, curthread, vpp); 947143663Sphk if (error || *vpp != NULL) 948143578Sphk return (error); 94912406Sdyson 95012115Sdyson /* Read in the disk contents for the inode, copy into the inode. */ 95112115Sdyson#if 0 95212115Sdysonprintf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino))); 95312115Sdyson#endif 95443301Sdillon if ((error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)), 95543301Sdillon (int)fs->s_blocksize, NOCRED, &bp)) != 0) { 95612115Sdyson /* 95712115Sdyson * The inode does not contain anything useful, so it would 95812115Sdyson * be misleading to leave it on its hash chain. With mode 95912115Sdyson * still zero, it will be unlinked and returned to the free 96012115Sdyson * list by vput(). 96112115Sdyson */ 96212115Sdyson vput(vp); 96312115Sdyson brelse(bp); 96412115Sdyson *vpp = NULL; 96512115Sdyson return (error); 96612115Sdyson } 96712115Sdyson /* convert ext2 inode to dinode */ 96896749Siedowse ext2_ei2i((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE * 96996749Siedowse ino_to_fsbo(fs, ino)), ip); 97012115Sdyson ip->i_block_group = ino_to_cg(fs, ino); 97112115Sdyson ip->i_next_alloc_block = 0; 97212115Sdyson ip->i_next_alloc_goal = 0; 97312115Sdyson ip->i_prealloc_count = 0; 97412115Sdyson ip->i_prealloc_block = 0; 97512115Sdyson /* now we want to make sure that block pointers for unused 97612115Sdyson blocks are zeroed out - ext2_balloc depends on this 97712115Sdyson although for regular files and directories only 97812115Sdyson */ 97912115Sdyson if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) { 98012115Sdyson used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize; 98112115Sdyson for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++) 98212115Sdyson ip->i_db[i] = 0; 98312115Sdyson } 98412115Sdyson/* 98512115Sdyson ext2_print_inode(ip); 98612115Sdyson*/ 98712115Sdyson brelse(bp); 98812115Sdyson 98912115Sdyson /* 99012115Sdyson * Initialize the vnode from the inode, check for aliases. 99112115Sdyson * Note that the underlying vnode may have changed. 99212115Sdyson */ 993138290Sphk if ((error = ext2_vinit(mp, &ext2_fifoops, &vp)) != 0) { 99412115Sdyson vput(vp); 99512115Sdyson *vpp = NULL; 99612115Sdyson return (error); 99712115Sdyson } 99812115Sdyson /* 99912115Sdyson * Finish inode initialization now that aliasing has been resolved. 100012115Sdyson */ 100112115Sdyson ip->i_devvp = ump->um_devvp; 100212115Sdyson VREF(ip->i_devvp); 100312115Sdyson /* 100412115Sdyson * Set up a generation number for this inode if it does not 100512115Sdyson * already have one. This should only happen on old filesystems. 100612115Sdyson */ 100712115Sdyson if (ip->i_gen == 0) { 100831485Sbde ip->i_gen = random() / 2 + 1; 100912115Sdyson if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) 101012115Sdyson ip->i_flag |= IN_MODIFIED; 101112115Sdyson } 101212115Sdyson *vpp = vp; 101312115Sdyson return (0); 101412115Sdyson} 101512115Sdyson 101612115Sdyson/* 101712115Sdyson * File handle to vnode 101812115Sdyson * 101912115Sdyson * Have to be really careful about stale file handles: 102012115Sdyson * - check that the inode number is valid 102112115Sdyson * - call ext2_vget() to get the locked inode 102212115Sdyson * - check for an unallocated inode (i_mode == 0) 102312115Sdyson * - check that the given client host has export rights and return 102412115Sdyson * those rights via. exflagsp and credanonp 102512115Sdyson */ 102612911Sphkstatic int 102751138Salfredext2_fhtovp(mp, fhp, vpp) 102896752Siedowse struct mount *mp; 102912115Sdyson struct fid *fhp; 103012115Sdyson struct vnode **vpp; 103112115Sdyson{ 103296749Siedowse struct inode *ip; 103396752Siedowse struct ufid *ufhp; 103496749Siedowse struct vnode *nvp; 103512115Sdyson struct ext2_sb_info *fs; 103696749Siedowse int error; 103712115Sdyson 103812115Sdyson ufhp = (struct ufid *)fhp; 103996749Siedowse fs = VFSTOEXT2(mp)->um_e2fs; 104012115Sdyson if (ufhp->ufid_ino < ROOTINO || 104196880Siedowse ufhp->ufid_ino > fs->s_groups_count * fs->s_es->s_inodes_per_group) 104212115Sdyson return (ESTALE); 104396749Siedowse 104496749Siedowse error = VFS_VGET(mp, ufhp->ufid_ino, LK_EXCLUSIVE, &nvp); 104596749Siedowse if (error) { 104696749Siedowse *vpp = NULLVP; 104796749Siedowse return (error); 104896749Siedowse } 104996749Siedowse ip = VTOI(nvp); 105096749Siedowse if (ip->i_mode == 0 || 105196749Siedowse ip->i_gen != ufhp->ufid_gen || ip->i_nlink <= 0) { 105296749Siedowse vput(nvp); 105396749Siedowse *vpp = NULLVP; 105496749Siedowse return (ESTALE); 105596749Siedowse } 105696749Siedowse *vpp = nvp; 1057140768Sphk vnode_create_vobject(*vpp, 0, curthread); 105896749Siedowse return (0); 105912115Sdyson} 106012115Sdyson 106112115Sdyson/* 106212115Sdyson * Vnode pointer to File handle 106312115Sdyson */ 106412115Sdyson/* ARGSUSED */ 106512911Sphkstatic int 106612115Sdysonext2_vptofh(vp, fhp) 106712115Sdyson struct vnode *vp; 106812115Sdyson struct fid *fhp; 106912115Sdyson{ 107096752Siedowse struct inode *ip; 107196752Siedowse struct ufid *ufhp; 107212115Sdyson 107312115Sdyson ip = VTOI(vp); 107412115Sdyson ufhp = (struct ufid *)fhp; 107512115Sdyson ufhp->ufid_len = sizeof(struct ufid); 107612115Sdyson ufhp->ufid_ino = ip->i_number; 107712115Sdyson ufhp->ufid_gen = ip->i_gen; 107812115Sdyson return (0); 107912115Sdyson} 108012115Sdyson 108112115Sdyson/* 108212115Sdyson * Write a superblock and associated information back to disk. 108312115Sdyson */ 108412911Sphkstatic int 108512115Sdysonext2_sbupdate(mp, waitfor) 108696749Siedowse struct ext2mount *mp; 108712115Sdyson int waitfor; 108812115Sdyson{ 108996752Siedowse struct ext2_sb_info *fs = mp->um_e2fs; 109096752Siedowse struct ext2_super_block *es = fs->s_es; 109196752Siedowse struct buf *bp; 109241591Sarchie int error = 0; 109312115Sdyson/* 109412115Sdysonprintf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no"); 109512115Sdyson*/ 1096111856Sjeff bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0, 0); 109712115Sdyson bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block)); 109812115Sdyson if (waitfor == MNT_WAIT) 109912115Sdyson error = bwrite(bp); 110012115Sdyson else 110112115Sdyson bawrite(bp); 110212115Sdyson 110327881Sdyson /* 110427881Sdyson * The buffers for group descriptors, inode bitmaps and block bitmaps 110527881Sdyson * are not busy at this point and are (hopefully) written by the 110627881Sdyson * usual sync mechanism. No need to write them here 110712115Sdyson */ 110812115Sdyson 110912115Sdyson return (error); 111012115Sdyson} 111196749Siedowse 111296749Siedowse/* 111396749Siedowse * Return the root of a filesystem. 111496749Siedowse */ 111596749Siedowsestatic int 1116132023Salfredext2_root(mp, vpp, td) 111796749Siedowse struct mount *mp; 111896749Siedowse struct vnode **vpp; 1119132023Salfred struct thread *td; 112096749Siedowse{ 112196749Siedowse struct vnode *nvp; 112296749Siedowse int error; 112396749Siedowse 112496749Siedowse error = VFS_VGET(mp, (ino_t)ROOTINO, LK_EXCLUSIVE, &nvp); 112596749Siedowse if (error) 112696749Siedowse return (error); 112796749Siedowse *vpp = nvp; 112896749Siedowse return (0); 112996749Siedowse} 1130