xref: /dragonfly/sys/vfs/ufs/ufs_vfsops.c (revision 984263bc)
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  */
41 
42 #include "opt_quota.h"
43 
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/mount.h>
47 #include <sys/proc.h>
48 #include <sys/malloc.h>
49 #include <sys/vnode.h>
50 
51 #include <ufs/ufs/quota.h>
52 #include <ufs/ufs/inode.h>
53 #include <ufs/ufs/ufsmount.h>
54 #include <ufs/ufs/ufs_extern.h>
55 
56 MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
57 /*
58  * Make a filesystem operational.
59  * Nothing to do at the moment.
60  */
61 /* ARGSUSED */
62 int
63 ufs_start(mp, flags, p)
64 	struct mount *mp;
65 	int flags;
66 	struct proc *p;
67 {
68 
69 	return (0);
70 }
71 
72 /*
73  * Return the root of a filesystem.
74  */
75 int
76 ufs_root(mp, vpp)
77 	struct mount *mp;
78 	struct vnode **vpp;
79 {
80 	struct vnode *nvp;
81 	int error;
82 
83 	error = VFS_VGET(mp, (ino_t)ROOTINO, &nvp);
84 	if (error)
85 		return (error);
86 	*vpp = nvp;
87 	return (0);
88 }
89 
90 /*
91  * Do operations associated with quotas
92  */
93 int
94 ufs_quotactl(mp, cmds, uid, arg, p)
95 	struct mount *mp;
96 	int cmds;
97 	uid_t uid;
98 	caddr_t arg;
99 	struct proc *p;
100 {
101 #ifndef QUOTA
102 	return (EOPNOTSUPP);
103 #else
104 	int cmd, type, error;
105 
106 	if (uid == -1)
107 		uid = p->p_cred->p_ruid;
108 	cmd = cmds >> SUBCMDSHIFT;
109 
110 	switch (cmd) {
111 	case Q_SYNC:
112 		break;
113 	case Q_GETQUOTA:
114 		if (uid == p->p_cred->p_ruid)
115 			break;
116 		/* fall through */
117 	default:
118 		if ((error = suser_xxx(0, p, PRISON_ROOT)) != 0)
119 			return (error);
120 	}
121 
122 	type = cmds & SUBCMDMASK;
123 	if ((u_int)type >= MAXQUOTAS)
124 		return (EINVAL);
125 	if (vfs_busy(mp, LK_NOWAIT, 0, p))
126 		return (0);
127 
128 	switch (cmd) {
129 
130 	case Q_QUOTAON:
131 		error = quotaon(p, mp, type, arg);
132 		break;
133 
134 	case Q_QUOTAOFF:
135 		error = quotaoff(p, mp, type);
136 		break;
137 
138 	case Q_SETQUOTA:
139 		error = setquota(mp, uid, type, arg);
140 		break;
141 
142 	case Q_SETUSE:
143 		error = setuse(mp, uid, type, arg);
144 		break;
145 
146 	case Q_GETQUOTA:
147 		error = getquota(mp, uid, type, arg);
148 		break;
149 
150 	case Q_SYNC:
151 		error = qsync(mp);
152 		break;
153 
154 	default:
155 		error = EINVAL;
156 		break;
157 	}
158 	vfs_unbusy(mp, p);
159 	return (error);
160 #endif
161 }
162 
163 /*
164  * Initial UFS filesystems, done only once.
165  */
166 int
167 ufs_init(vfsp)
168 	struct vfsconf *vfsp;
169 {
170 	static int done;
171 
172 	if (done)
173 		return (0);
174 	done = 1;
175 	ufs_ihashinit();
176 #ifdef QUOTA
177 	dqinit();
178 #endif
179 	return (0);
180 }
181 
182 /*
183  * This is the generic part of fhtovp called after the underlying
184  * filesystem has validated the file handle.
185  *
186  * Call the VFS_CHECKEXP beforehand to verify access.
187  */
188 int
189 ufs_fhtovp(mp, ufhp, vpp)
190 	register struct mount *mp;
191 	struct ufid *ufhp;
192 	struct vnode **vpp;
193 {
194 	register struct inode *ip;
195 	struct vnode *nvp;
196 	int error;
197 
198 	error = VFS_VGET(mp, ufhp->ufid_ino, &nvp);
199 	if (error) {
200 		*vpp = NULLVP;
201 		return (error);
202 	}
203 	ip = VTOI(nvp);
204 	if (ip->i_mode == 0 ||
205 	    ip->i_gen != ufhp->ufid_gen ||
206 	    (VFSTOUFS(mp)->um_i_effnlink_valid ? ip->i_effnlink :
207 	    ip->i_nlink) <= 0) {
208 		vput(nvp);
209 		*vpp = NULLVP;
210 		return (ESTALE);
211 	}
212 	*vpp = nvp;
213 	return (0);
214 }
215 
216 
217 /*
218  * This is the generic part of fhtovp called after the underlying
219  * filesystem has validated the file handle.
220  *
221  * Verify that a host should have access to a filesystem.
222  */
223 int
224 ufs_check_export(mp, nam, exflagsp, credanonp)
225 	register struct mount *mp;
226 	struct sockaddr *nam;
227 	int *exflagsp;
228 	struct ucred **credanonp;
229 {
230 	register struct netcred *np;
231 	register struct ufsmount *ump;;
232 
233 	ump = VFSTOUFS(mp);
234 	/*
235 	 * Get the export permission structure for this <mp, client> tuple.
236 	 */
237 	np = vfs_export_lookup(mp, &ump->um_export, nam);
238 	if (np == NULL)
239 		return (EACCES);
240 
241 	*exflagsp = np->netc_exflags;
242 	*credanonp = &np->netc_anon;
243 	return (0);
244 }
245