xref: /freebsd/sys/ufs/ufs/ufs_vfsops.c (revision 5b9c547c)
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  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)ufs_vfsops.c	8.8 (Berkeley) 5/20/95
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "opt_quota.h"
41 #include "opt_ufs.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/lock.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/proc.h>
50 #include <sys/socket.h>
51 #include <sys/vnode.h>
52 
53 #include <ufs/ufs/extattr.h>
54 #include <ufs/ufs/quota.h>
55 #include <ufs/ufs/inode.h>
56 #include <ufs/ufs/ufsmount.h>
57 #include <ufs/ufs/ufs_extern.h>
58 #ifdef UFS_DIRHASH
59 #include <ufs/ufs/dir.h>
60 #include <ufs/ufs/dirhash.h>
61 #endif
62 
63 MALLOC_DEFINE(M_UFSMNT, "ufs_mount", "UFS mount structure");
64 
65 /*
66  * Return the root of a filesystem.
67  */
68 int
69 ufs_root(mp, flags, vpp)
70 	struct mount *mp;
71 	int flags;
72 	struct vnode **vpp;
73 {
74 	struct vnode *nvp;
75 	int error;
76 
77 	error = VFS_VGET(mp, (ino_t)ROOTINO, flags, &nvp);
78 	if (error)
79 		return (error);
80 	*vpp = nvp;
81 	return (0);
82 }
83 
84 /*
85  * Do operations associated with quotas
86  */
87 int
88 ufs_quotactl(mp, cmds, id, arg)
89 	struct mount *mp;
90 	int cmds;
91 	uid_t id;
92 	void *arg;
93 {
94 #ifndef QUOTA
95 	if ((cmds >> SUBCMDSHIFT) == Q_QUOTAON)
96 		vfs_unbusy(mp);
97 
98 	return (EOPNOTSUPP);
99 #else
100 	struct thread *td;
101 	int cmd, type, error;
102 
103 	td = curthread;
104 	cmd = cmds >> SUBCMDSHIFT;
105 	type = cmds & SUBCMDMASK;
106 	if (id == -1) {
107 		switch (type) {
108 
109 		case USRQUOTA:
110 			id = td->td_ucred->cr_ruid;
111 			break;
112 
113 		case GRPQUOTA:
114 			id = td->td_ucred->cr_rgid;
115 			break;
116 
117 		default:
118 			if (cmd == Q_QUOTAON)
119 				vfs_unbusy(mp);
120 			return (EINVAL);
121 		}
122 	}
123 	if ((u_int)type >= MAXQUOTAS) {
124 		if (cmd == Q_QUOTAON)
125 			vfs_unbusy(mp);
126 		return (EINVAL);
127 	}
128 
129 	switch (cmd) {
130 	case Q_QUOTAON:
131 		error = quotaon(td, mp, type, arg);
132 		break;
133 
134 	case Q_QUOTAOFF:
135 		error = quotaoff(td, mp, type);
136 		break;
137 
138 	case Q_SETQUOTA32:
139 		error = setquota32(td, mp, id, type, arg);
140 		break;
141 
142 	case Q_SETUSE32:
143 		error = setuse32(td, mp, id, type, arg);
144 		break;
145 
146 	case Q_GETQUOTA32:
147 		error = getquota32(td, mp, id, type, arg);
148 		break;
149 
150 	case Q_SETQUOTA:
151 		error = setquota(td, mp, id, type, arg);
152 		break;
153 
154 	case Q_SETUSE:
155 		error = setuse(td, mp, id, type, arg);
156 		break;
157 
158 	case Q_GETQUOTA:
159 		error = getquota(td, mp, id, type, arg);
160 		break;
161 
162 	case Q_GETQUOTASIZE:
163 		error = getquotasize(td, mp, id, type, arg);
164 		break;
165 
166 	case Q_SYNC:
167 		error = qsync(mp);
168 		break;
169 
170 	default:
171 		error = EINVAL;
172 		break;
173 	}
174 	return (error);
175 #endif
176 }
177 
178 /*
179  * Initial UFS filesystems, done only once.
180  */
181 int
182 ufs_init(vfsp)
183 	struct vfsconf *vfsp;
184 {
185 
186 #ifdef QUOTA
187 	dqinit();
188 #endif
189 #ifdef UFS_DIRHASH
190 	ufsdirhash_init();
191 #endif
192 	return (0);
193 }
194 
195 /*
196  * Uninitialise UFS filesystems, done before module unload.
197  */
198 int
199 ufs_uninit(vfsp)
200 	struct vfsconf *vfsp;
201 {
202 
203 #ifdef QUOTA
204 	dquninit();
205 #endif
206 #ifdef UFS_DIRHASH
207 	ufsdirhash_uninit();
208 #endif
209 	return (0);
210 }
211 
212 /*
213  * This is the generic part of fhtovp called after the underlying
214  * filesystem has validated the file handle.
215  *
216  * Call the VFS_CHECKEXP beforehand to verify access.
217  */
218 int
219 ufs_fhtovp(mp, ufhp, flags, vpp)
220 	struct mount *mp;
221 	struct ufid *ufhp;
222 	int flags;
223 	struct vnode **vpp;
224 {
225 	struct inode *ip;
226 	struct vnode *nvp;
227 	int error;
228 
229 	error = VFS_VGET(mp, ufhp->ufid_ino, flags, &nvp);
230 	if (error) {
231 		*vpp = NULLVP;
232 		return (error);
233 	}
234 	ip = VTOI(nvp);
235 	if (ip->i_mode == 0 || ip->i_gen != ufhp->ufid_gen ||
236 	    ip->i_effnlink <= 0) {
237 		vput(nvp);
238 		*vpp = NULLVP;
239 		return (ESTALE);
240 	}
241 	*vpp = nvp;
242 	vnode_create_vobject(*vpp, DIP(ip, i_size), curthread);
243 	return (0);
244 }
245