1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)ufs_vfsops.c 8.8 (Berkeley) 5/20/95
37 */
38
39 #include <sys/cdefs.h>
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
ufs_root(struct mount * mp,int flags,struct vnode ** vpp)69 ufs_root(struct mount *mp, int flags, struct vnode **vpp)
70 {
71 struct vnode *nvp;
72 int error;
73
74 error = VFS_VGET(mp, (ino_t)UFS_ROOTINO, flags, &nvp);
75 if (error)
76 return (error);
77 *vpp = nvp;
78 return (0);
79 }
80
81 /*
82 * Do operations associated with quotas
83 */
84 int
ufs_quotactl(struct mount * mp,int cmds,uid_t id,void * arg,bool * mp_busy)85 ufs_quotactl(struct mount *mp, int cmds, uid_t id, void *arg, bool *mp_busy)
86 {
87 #ifndef QUOTA
88 return (EOPNOTSUPP);
89 #else
90 struct thread *td;
91 int cmd, type, error;
92
93 td = curthread;
94 cmd = cmds >> SUBCMDSHIFT;
95 type = cmds & SUBCMDMASK;
96 if (id == -1) {
97 switch (type) {
98 case USRQUOTA:
99 id = td->td_ucred->cr_ruid;
100 break;
101
102 case GRPQUOTA:
103 id = td->td_ucred->cr_rgid;
104 break;
105
106 default:
107 return (EINVAL);
108 }
109 }
110 if ((uint64_t)type >= MAXQUOTAS)
111 return (EINVAL);
112
113 switch (cmd) {
114 case Q_QUOTAON:
115 error = quotaon(td, mp, type, arg, mp_busy);
116 break;
117
118 case Q_QUOTAOFF:
119 vfs_ref(mp);
120 KASSERT(*mp_busy,
121 ("%s called without busied mount", __func__));
122 vn_start_write(NULL, &mp, V_WAIT);
123 vfs_unbusy(mp);
124 *mp_busy = false;
125 error = quotaoff(td, mp, type);
126 vn_finished_write(mp);
127 vfs_rel(mp);
128 break;
129
130 case Q_SETQUOTA32:
131 error = setquota32(td, mp, id, type, arg);
132 break;
133
134 case Q_SETUSE32:
135 error = setuse32(td, mp, id, type, arg);
136 break;
137
138 case Q_GETQUOTA32:
139 error = getquota32(td, mp, id, type, arg);
140 break;
141
142 case Q_SETQUOTA:
143 error = setquota(td, mp, id, type, arg);
144 break;
145
146 case Q_SETUSE:
147 error = setuse(td, mp, id, type, arg);
148 break;
149
150 case Q_GETQUOTA:
151 error = getquota(td, mp, id, type, arg);
152 break;
153
154 case Q_GETQUOTASIZE:
155 error = getquotasize(td, mp, id, type, arg);
156 break;
157
158 case Q_SYNC:
159 error = qsync(mp);
160 break;
161
162 default:
163 error = EINVAL;
164 break;
165 }
166 return (error);
167 #endif
168 }
169
170 /*
171 * Initial UFS filesystems, done only once.
172 */
173 int
ufs_init(struct vfsconf * vfsp)174 ufs_init(struct vfsconf *vfsp)
175 {
176
177 #ifdef QUOTA
178 dqinit();
179 #endif
180 #ifdef UFS_DIRHASH
181 ufsdirhash_init();
182 #endif
183 return (0);
184 }
185
186 /*
187 * Uninitialise UFS filesystems, done before module unload.
188 */
189 int
ufs_uninit(struct vfsconf * vfsp)190 ufs_uninit(struct vfsconf *vfsp)
191 {
192
193 #ifdef QUOTA
194 dquninit();
195 #endif
196 #ifdef UFS_DIRHASH
197 ufsdirhash_uninit();
198 #endif
199 return (0);
200 }
201