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
37 #include <sys/cdefs.h>
38 #include "opt_quota.h"
39 #include "opt_ufs.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mount.h>
47 #include <sys/proc.h>
48 #include <sys/socket.h>
49 #include <sys/vnode.h>
50
51 #include <ufs/ufs/extattr.h>
52 #include <ufs/ufs/quota.h>
53 #include <ufs/ufs/inode.h>
54 #include <ufs/ufs/ufsmount.h>
55 #include <ufs/ufs/ufs_extern.h>
56 #ifdef UFS_DIRHASH
57 #include <ufs/ufs/dir.h>
58 #include <ufs/ufs/dirhash.h>
59 #endif
60
61 MALLOC_DEFINE(M_UFSMNT, "ufs_mount", "UFS mount structure");
62
63 /*
64 * Return the root of a filesystem.
65 */
66 int
ufs_root(struct mount * mp,int flags,struct vnode ** vpp)67 ufs_root(struct mount *mp, int flags, struct vnode **vpp)
68 {
69 struct vnode *nvp;
70 int error;
71
72 error = VFS_VGET(mp, (ino_t)UFS_ROOTINO, flags, &nvp);
73 if (error)
74 return (error);
75 *vpp = nvp;
76 return (0);
77 }
78
79 /*
80 * Do operations associated with quotas
81 */
82 int
ufs_quotactl(struct mount * mp,int cmds,uid_t id,void * arg,bool * mp_busy)83 ufs_quotactl(struct mount *mp, int cmds, uid_t id, void *arg, bool *mp_busy)
84 {
85 #ifndef QUOTA
86 return (EOPNOTSUPP);
87 #else
88 struct thread *td;
89 int cmd, type, error;
90
91 td = curthread;
92 cmd = cmds >> SUBCMDSHIFT;
93 type = cmds & SUBCMDMASK;
94 if (id == -1) {
95 switch (type) {
96 case USRQUOTA:
97 id = td->td_ucred->cr_ruid;
98 break;
99
100 case GRPQUOTA:
101 id = td->td_ucred->cr_rgid;
102 break;
103
104 default:
105 return (EINVAL);
106 }
107 }
108 if ((uint64_t)type >= MAXQUOTAS)
109 return (EINVAL);
110
111 switch (cmd) {
112 case Q_QUOTAON:
113 error = quotaon(td, mp, type, arg, mp_busy);
114 break;
115
116 case Q_QUOTAOFF:
117 vfs_ref(mp);
118 KASSERT(*mp_busy,
119 ("%s called without busied mount", __func__));
120 vn_start_write(NULL, &mp, V_WAIT);
121 vfs_unbusy(mp);
122 *mp_busy = false;
123 error = quotaoff(td, mp, type);
124 vn_finished_write(mp);
125 vfs_rel(mp);
126 break;
127
128 case Q_SETQUOTA32:
129 error = setquota32(td, mp, id, type, arg);
130 break;
131
132 case Q_SETUSE32:
133 error = setuse32(td, mp, id, type, arg);
134 break;
135
136 case Q_GETQUOTA32:
137 error = getquota32(td, mp, id, type, arg);
138 break;
139
140 case Q_SETQUOTA:
141 error = setquota(td, mp, id, type, arg);
142 break;
143
144 case Q_SETUSE:
145 error = setuse(td, mp, id, type, arg);
146 break;
147
148 case Q_GETQUOTA:
149 error = getquota(td, mp, id, type, arg);
150 break;
151
152 case Q_GETQUOTASIZE:
153 error = getquotasize(td, mp, id, type, arg);
154 break;
155
156 case Q_SYNC:
157 error = qsync(mp);
158 break;
159
160 default:
161 error = EINVAL;
162 break;
163 }
164 return (error);
165 #endif
166 }
167
168 /*
169 * Initial UFS filesystems, done only once.
170 */
171 int
ufs_init(struct vfsconf * vfsp)172 ufs_init(struct vfsconf *vfsp)
173 {
174
175 #ifdef QUOTA
176 dqinit();
177 #endif
178 #ifdef UFS_DIRHASH
179 ufsdirhash_init();
180 #endif
181 return (0);
182 }
183
184 /*
185 * Uninitialise UFS filesystems, done before module unload.
186 */
187 int
ufs_uninit(struct vfsconf * vfsp)188 ufs_uninit(struct vfsconf *vfsp)
189 {
190
191 #ifdef QUOTA
192 dquninit();
193 #endif
194 #ifdef UFS_DIRHASH
195 ufsdirhash_uninit();
196 #endif
197 return (0);
198 }
199