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