/
ufs_vfsops.c
342 lines (291 loc) · 7.1 KB
/
ufs_vfsops.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* $NetBSD: ufs_vfsops.c,v 1.61 2023/02/22 21:49:45 riastradh Exp $ */
/*
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)ufs_vfsops.c 8.8 (Berkeley) 5/20/95
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ufs_vfsops.c,v 1.61 2023/02/22 21:49:45 riastradh Exp $");
#if defined(_KERNEL_OPT)
#include "opt_ffs.h"
#include "opt_quota.h"
#include "opt_wapbl.h"
#endif
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/proc.h>
#include <sys/buf.h>
#include <sys/module.h>
#include <sys/vnode.h>
#include <sys/kmem.h>
#include <sys/kauth.h>
#include <miscfs/specfs/specdev.h>
#include <sys/quotactl.h>
#include <ufs/ufs/quota.h>
#include <ufs/ufs/inode.h>
#include <ufs/ufs/ufsmount.h>
#include <ufs/ufs/ufs_extern.h>
#ifdef UFS_DIRHASH
#include <ufs/ufs/dirhash.h>
#endif
/* how many times ufs_init() was called */
static int ufs_initcount = 0;
pool_cache_t ufs_direct_cache;
/*
* Make a filesystem operational.
* Nothing to do at the moment.
*/
/* ARGSUSED */
int
ufs_start(struct mount *mp, int flags)
{
return (0);
}
/*
* Return the root of a filesystem.
*/
int
ufs_root(struct mount *mp, int lktype, struct vnode **vpp)
{
struct vnode *nvp;
int error;
if ((error = VFS_VGET(mp, (ino_t)UFS_ROOTINO, lktype, &nvp)) != 0)
return (error);
*vpp = nvp;
return (0);
}
/*
* Look up and return a vnode/inode pair by inode number.
*/
int
ufs_vget(struct mount *mp, ino_t ino, int lktype, struct vnode **vpp)
{
int error;
error = vcache_get(mp, &ino, sizeof(ino), vpp);
if (error)
return error;
error = vn_lock(*vpp, lktype);
if (error) {
vrele(*vpp);
*vpp = NULL;
return error;
}
return 0;
}
/*
* Do operations associated with quotas
*/
int
ufs_quotactl(struct mount *mp, struct quotactl_args *args)
{
#if !defined(QUOTA) && !defined(QUOTA2)
(void) mp;
(void) args;
return (EOPNOTSUPP);
#else
struct lwp *l = curlwp;
int error;
/* Mark the mount busy, as we're passing it to kauth(9). */
error = vfs_busy(mp);
if (error) {
return (error);
}
mutex_enter(mp->mnt_updating);
error = quota_handle_cmd(mp, l, args);
mutex_exit(mp->mnt_updating);
vfs_unbusy(mp);
return (error);
#endif
}
#if 0
switch (cmd) {
case Q_SYNC:
break;
case Q_GETQUOTA:
/* The user can always query about his own quota. */
if (uid == kauth_cred_getuid(l->l_cred))
break;
error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, KAUTH_ARG(uid), NULL);
break;
case Q_QUOTAON:
case Q_QUOTAOFF:
error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
KAUTH_REQ_SYSTEM_FS_QUOTA_ONOFF, mp, NULL, NULL);
break;
case Q_SETQUOTA:
case Q_SETUSE:
error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
KAUTH_REQ_SYSTEM_FS_QUOTA_MANAGE, mp, KAUTH_ARG(uid), NULL);
break;
default:
error = EINVAL;
break;
}
type = cmds & SUBCMDMASK;
if (!error) {
/* Only check if there was no error above. */
if ((u_int)type >= MAXQUOTAS)
error = EINVAL;
}
if (error) {
vfs_unbusy(mp);
return (error);
}
mutex_enter(mp->mnt_updating);
switch (cmd) {
case Q_QUOTAON:
error = quotaon(l, mp, type, arg);
break;
case Q_QUOTAOFF:
error = quotaoff(l, mp, type);
break;
case Q_SETQUOTA:
error = setquota(mp, uid, type, arg);
break;
case Q_SETUSE:
error = setuse(mp, uid, type, arg);
break;
case Q_GETQUOTA:
error = getquota(mp, uid, type, arg);
break;
case Q_SYNC:
error = qsync(mp);
break;
default:
error = EINVAL;
}
mutex_exit(mp->mnt_updating);
vfs_unbusy(mp);
return (error);
#endif
/*
* This is the generic part of fhtovp called after the underlying
* filesystem has validated the file handle.
*/
int
ufs_fhtovp(struct mount *mp, struct ufid *ufhp, int lktype, struct vnode **vpp)
{
struct vnode *nvp;
struct inode *ip;
int error;
if ((error = VFS_VGET(mp, ufhp->ufid_ino, lktype, &nvp)) != 0) {
if (error == ENOENT)
error = ESTALE;
*vpp = NULLVP;
return (error);
}
ip = VTOI(nvp);
KASSERT(ip != NULL);
if (ip->i_mode == 0 || ip->i_gen != ufhp->ufid_gen ||
((ip->i_mode & IFMT) == IFDIR && ip->i_size == 0)) {
vput(nvp);
*vpp = NULLVP;
return (ESTALE);
}
*vpp = nvp;
return (0);
}
/*
* Initialize UFS filesystems, done only once.
*/
void
ufs_init(void)
{
if (ufs_initcount++ > 0)
return;
ufs_direct_cache = pool_cache_init(sizeof(struct direct), 0, 0, 0,
"ufsdir", NULL, IPL_NONE, NULL, NULL, NULL);
#if defined(QUOTA) || defined(QUOTA2)
dqinit();
#endif
#ifdef UFS_DIRHASH
ufsdirhash_init();
#endif
#ifdef UFS_EXTATTR
ufs_extattr_init();
#endif
}
void
ufs_reinit(void)
{
#if defined(QUOTA) || defined(QUOTA2)
dqreinit();
#endif
}
/*
* Free UFS filesystem resources, done only once.
*/
void
ufs_done(void)
{
if (--ufs_initcount > 0)
return;
#if defined(QUOTA) || defined(QUOTA2)
dqdone();
#endif
pool_cache_destroy(ufs_direct_cache);
#ifdef UFS_DIRHASH
ufsdirhash_done();
#endif
#ifdef UFS_EXTATTR
ufs_extattr_done();
#endif
}
/*
* module interface
*/
#ifdef WAPBL
MODULE(MODULE_CLASS_MISC, ufs, "wapbl");
#else
MODULE(MODULE_CLASS_MISC, ufs, NULL);
#endif
static int
ufs_modcmd(modcmd_t cmd, void *arg)
{
int error;
switch (cmd) {
case MODULE_CMD_INIT:
ufs_init();
error = 0;
break;
case MODULE_CMD_FINI:
ufs_done();
error = 0;
break;
default:
error = ENOTTY;
break;
}
return error;
}