1 /* 2 * Copyright (c) 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)ufs_vfsops.c 8.8 (Berkeley) 5/20/95 39 * $FreeBSD: src/sys/ufs/ufs/ufs_vfsops.c,v 1.17.2.3 2001/10/14 19:08:16 iedowse Exp $ 40 * $DragonFly: src/sys/vfs/ufs/ufs_vfsops.c,v 1.11 2005/07/26 15:43:36 hmp Exp $ 41 */ 42 43 #include "opt_quota.h" 44 45 #include <sys/param.h> 46 #include <sys/kernel.h> 47 #include <sys/mount.h> 48 #include <sys/proc.h> 49 #include <sys/malloc.h> 50 #include <sys/vnode.h> 51 52 #include "quota.h" 53 #include "inode.h" 54 #include "ufsmount.h" 55 #include "ufs_extern.h" 56 57 MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure"); 58 /* 59 * Return the root of a filesystem. 60 */ 61 int 62 ufs_root(struct mount *mp, struct vnode **vpp) 63 { 64 struct vnode *nvp; 65 int error; 66 67 error = VFS_VGET(mp, (ino_t)ROOTINO, &nvp); 68 if (error) 69 return (error); 70 *vpp = nvp; 71 return (0); 72 } 73 74 /* 75 * Do operations associated with quotas 76 */ 77 int 78 ufs_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t arg, 79 struct thread *td) 80 { 81 #ifndef QUOTA 82 return (EOPNOTSUPP); 83 #else 84 struct proc *p = td->td_proc; 85 int cmd, type, error; 86 87 if (p == NULL) 88 return (EOPNOTSUPP); 89 90 type = cmds & SUBCMDMASK; 91 cmd = cmds >> SUBCMDSHIFT; 92 93 if (uid == -1) { 94 switch(type) { 95 case USRQUOTA: 96 uid = p->p_ucred->cr_ruid; 97 break; 98 case GRPQUOTA: 99 uid = p->p_ucred->cr_rgid; 100 break; 101 default: 102 return (EINVAL); 103 } 104 } 105 106 switch (cmd) { 107 case Q_SYNC: 108 break; 109 case Q_GETQUOTA: 110 if (uid == p->p_ucred->cr_ruid) 111 break; 112 /* fall through */ 113 default: 114 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0) 115 return (error); 116 } 117 118 type = cmds & SUBCMDMASK; 119 if ((uint)type >= MAXQUOTAS) 120 return (EINVAL); 121 if (vfs_busy(mp, LK_NOWAIT, td)) 122 return (0); 123 124 switch (cmd) { 125 126 case Q_QUOTAON: 127 error = quotaon(td, mp, type, arg); 128 break; 129 130 case Q_QUOTAOFF: 131 error = quotaoff(td, mp, type); 132 break; 133 134 case Q_SETQUOTA: 135 error = setquota(mp, uid, type, arg); 136 break; 137 138 case Q_SETUSE: 139 error = setuse(mp, uid, type, arg); 140 break; 141 142 case Q_GETQUOTA: 143 error = getquota(mp, uid, type, arg); 144 break; 145 146 case Q_SYNC: 147 error = qsync(mp); 148 break; 149 150 default: 151 error = EINVAL; 152 break; 153 } 154 vfs_unbusy(mp, td); 155 return (error); 156 #endif 157 } 158 159 /* 160 * Initial UFS filesystems, done only once. 161 */ 162 int 163 ufs_init(struct vfsconf *vfsp) 164 { 165 static int done; 166 167 if (done) 168 return (0); 169 done = 1; 170 ufs_ihashinit(); 171 #ifdef QUOTA 172 dqinit(); 173 #endif 174 return (0); 175 } 176 177 /* 178 * This is the generic part of fhtovp called after the underlying 179 * filesystem has validated the file handle. 180 * 181 * Call the VFS_CHECKEXP beforehand to verify access. 182 */ 183 int 184 ufs_fhtovp(struct mount *mp, struct ufid *ufhp, struct vnode **vpp) 185 { 186 struct inode *ip; 187 struct vnode *nvp; 188 int error; 189 190 error = VFS_VGET(mp, ufhp->ufid_ino, &nvp); 191 if (error) { 192 *vpp = NULLVP; 193 return (error); 194 } 195 ip = VTOI(nvp); 196 if (ip->i_mode == 0 || 197 ip->i_gen != ufhp->ufid_gen || 198 (VFSTOUFS(mp)->um_i_effnlink_valid ? ip->i_effnlink : 199 ip->i_nlink) <= 0) { 200 vput(nvp); 201 *vpp = NULLVP; 202 return (ESTALE); 203 } 204 *vpp = nvp; 205 return (0); 206 } 207 208 209 /* 210 * This is the generic part of fhtovp called after the underlying 211 * filesystem has validated the file handle. 212 * 213 * Verify that a host should have access to a filesystem. 214 */ 215 int 216 ufs_check_export(struct mount *mp, struct sockaddr *nam, int *exflagsp, 217 struct ucred **credanonp) 218 { 219 struct netcred *np; 220 struct ufsmount *ump;; 221 222 ump = VFSTOUFS(mp); 223 /* 224 * Get the export permission structure for this <mp, client> tuple. 225 */ 226 np = vfs_export_lookup(mp, &ump->um_export, nam); 227 if (np == NULL) 228 return (EACCES); 229 230 *exflagsp = np->netc_exflags; 231 *credanonp = &np->netc_anon; 232 return (0); 233 } 234