vfs_syscalls_50.c revision 1.18.12.1 1 /* $NetBSD: vfs_syscalls_50.c,v 1.18.12.1 2021/08/15 10:03:46 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
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 NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_50.c,v 1.18.12.1 2021/08/15 10:03:46 martin Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/namei.h>
44 #include <sys/filedesc.h>
45 #include <sys/kernel.h>
46 #include <sys/file.h>
47 #include <sys/stat.h>
48 #include <sys/socketvar.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
51 #include <sys/proc.h>
52 #include <sys/uio.h>
53 #include <sys/dirent.h>
54 #include <sys/kauth.h>
55 #include <sys/time.h>
56 #include <sys/vfs_syscalls.h>
57 #ifndef LFS
58 #define LFS
59 #endif
60 #include <sys/syscallargs.h>
61
62 #include <ufs/lfs/lfs_extern.h>
63
64 #include <sys/quota.h>
65 #include <sys/quotactl.h>
66 #include <ufs/ufs/quota1.h>
67
68 #include <compat/common/compat_util.h>
69 #include <compat/sys/time.h>
70 #include <compat/sys/stat.h>
71 #include <compat/sys/dirent.h>
72 #include <compat/sys/mount.h>
73
74 /*
75 * Convert from a new to an old stat structure.
76 */
77 static void
78 cvtstat(struct stat30 *ost, const struct stat *st)
79 {
80
81 /* Handle any padding. */
82 memset(ost, 0, sizeof(*ost));
83 ost->st_dev = st->st_dev;
84 ost->st_ino = st->st_ino;
85 ost->st_mode = st->st_mode;
86 ost->st_nlink = st->st_nlink;
87 ost->st_uid = st->st_uid;
88 ost->st_gid = st->st_gid;
89 ost->st_rdev = st->st_rdev;
90 timespec_to_timespec50(&st->st_atimespec, &ost->st_atimespec);
91 timespec_to_timespec50(&st->st_mtimespec, &ost->st_mtimespec);
92 timespec_to_timespec50(&st->st_ctimespec, &ost->st_ctimespec);
93 timespec_to_timespec50(&st->st_birthtimespec, &ost->st_birthtimespec);
94 ost->st_size = st->st_size;
95 ost->st_blocks = st->st_blocks;
96 ost->st_blksize = st->st_blksize;
97 ost->st_flags = st->st_flags;
98 ost->st_gen = st->st_gen;
99 memset(ost->st_spare, 0, sizeof(ost->st_spare));
100 }
101
102 /*
103 * Get file status; this version follows links.
104 */
105 /* ARGSUSED */
106 int
107 compat_50_sys___stat30(struct lwp *l, const struct compat_50_sys___stat30_args *uap, register_t *retval)
108 {
109 /* {
110 syscallarg(const char *) path;
111 syscallarg(struct stat30 *) ub;
112 } */
113 struct stat sb;
114 struct stat30 osb;
115 int error;
116
117 error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
118 if (error)
119 return error;
120 cvtstat(&osb, &sb);
121 return copyout(&osb, SCARG(uap, ub), sizeof(osb));
122 }
123
124
125 /*
126 * Get file status; this version does not follow links.
127 */
128 /* ARGSUSED */
129 int
130 compat_50_sys___lstat30(struct lwp *l, const struct compat_50_sys___lstat30_args *uap, register_t *retval)
131 {
132 /* {
133 syscallarg(const char *) path;
134 syscallarg(struct stat30 *) ub;
135 } */
136 struct stat sb;
137 struct stat30 osb;
138 int error;
139
140 error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb);
141 if (error)
142 return error;
143 cvtstat(&osb, &sb);
144 return copyout(&osb, SCARG(uap, ub), sizeof(osb));
145 }
146
147 /*
148 * Return status information about a file descriptor.
149 */
150 /* ARGSUSED */
151 int
152 compat_50_sys___fstat30(struct lwp *l, const struct compat_50_sys___fstat30_args *uap, register_t *retval)
153 {
154 /* {
155 syscallarg(int) fd;
156 syscallarg(struct stat30 *) sb;
157 } */
158 struct stat sb;
159 struct stat30 osb;
160 int error;
161
162 error = do_sys_fstat(SCARG(uap, fd), &sb);
163 if (error)
164 return error;
165 cvtstat(&osb, &sb);
166 return copyout(&osb, SCARG(uap, sb), sizeof(osb));
167 }
168
169 /* ARGSUSED */
170 int
171 compat_50_sys___fhstat40(struct lwp *l, const struct compat_50_sys___fhstat40_args *uap, register_t *retval)
172 {
173 /* {
174 syscallarg(const void *) fhp;
175 syscallarg(size_t) fh_size;
176 syscallarg(struct stat30 *) sb;
177 } */
178 struct stat sb;
179 struct stat30 osb;
180 int error;
181
182 error = do_fhstat(l, SCARG(uap, fhp), SCARG(uap, fh_size), &sb);
183 if (error)
184 return error;
185 cvtstat(&osb, &sb);
186 return copyout(&osb, SCARG(uap, sb), sizeof(osb));
187 }
188
189 static int
190 compat_50_do_sys_utimes(struct lwp *l, struct vnode *vp, const char *path,
191 int flag, const struct timeval50 *tptr)
192 {
193 struct timeval tv[2], *tvp;
194 struct timeval50 tv50[2];
195 if (tptr) {
196 int error = copyin(tptr, tv50, sizeof(tv50));
197 if (error)
198 return error;
199 timeval50_to_timeval(&tv50[0], &tv[0]);
200 timeval50_to_timeval(&tv50[1], &tv[1]);
201 tvp = tv;
202 } else
203 tvp = NULL;
204 return do_sys_utimes(l, vp, path, flag, tvp, UIO_SYSSPACE);
205 }
206
207 /*
208 * Set the access and modification times given a path name; this
209 * version follows links.
210 */
211 /* ARGSUSED */
212 int
213 compat_50_sys_utimes(struct lwp *l, const struct compat_50_sys_utimes_args *uap,
214 register_t *retval)
215 {
216 /* {
217 syscallarg(const char *) path;
218 syscallarg(const struct timeval50 *) tptr;
219 } */
220
221 return compat_50_do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
222 SCARG(uap, tptr));
223 }
224
225 /*
226 * Set the access and modification times given a file descriptor.
227 */
228 /* ARGSUSED */
229 int
230 compat_50_sys_futimes(struct lwp *l,
231 const struct compat_50_sys_futimes_args *uap, register_t *retval)
232 {
233 /* {
234 syscallarg(int) fd;
235 syscallarg(const struct timeval50 *) tptr;
236 } */
237 int error;
238 struct file *fp;
239
240 /* fd_getvnode() will use the descriptor for us */
241 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
242 return error;
243 error = compat_50_do_sys_utimes(l, fp->f_vnode, NULL, 0,
244 SCARG(uap, tptr));
245 fd_putfile(SCARG(uap, fd));
246 return error;
247 }
248
249 /*
250 * Set the access and modification times given a path name; this
251 * version does not follow links.
252 */
253 int
254 compat_50_sys_lutimes(struct lwp *l,
255 const struct compat_50_sys_lutimes_args *uap, register_t *retval)
256 {
257 /* {
258 syscallarg(const char *) path;
259 syscallarg(const struct timeval50 *) tptr;
260 } */
261
262 return compat_50_do_sys_utimes(l, NULL, SCARG(uap, path), NOFOLLOW,
263 SCARG(uap, tptr));
264 }
265
266 int
267 compat_50_sys_lfs_segwait(struct lwp *l,
268 const struct compat_50_sys_lfs_segwait_args *uap, register_t *retval)
269 {
270 /* {
271 syscallarg(fsid_t *) fsidp;
272 syscallarg(struct timeval50 *) tv;
273 } */
274 #ifdef notyet
275 struct timeval atv;
276 struct timeval50 atv50;
277 fsid_t fsid;
278 int error;
279
280 /* XXX need we be su to segwait? */
281 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
282 KAUTH_REQ_SYSTEM_LFS_SEGWAIT, NULL, NULL, NULL);
283 if (error)
284 return (error);
285 if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
286 return (error);
287
288 if (SCARG(uap, tv)) {
289 error = copyin(SCARG(uap, tv), &atv50, sizeof(atv50));
290 if (error)
291 return (error);
292 timeval50_to_timeval(&atv50, &atv);
293 if (itimerfix(&atv))
294 return (EINVAL);
295 } else /* NULL or invalid */
296 atv.tv_sec = atv.tv_usec = 0;
297 return lfs_segwait(&fsid, &atv);
298 #else
299 return ENOSYS;
300 #endif
301 }
302
303 int
304 compat_50_sys_mknod(struct lwp *l,
305 const struct compat_50_sys_mknod_args *uap, register_t *retval)
306 {
307 /* {
308 syscallarg(const char *) path;
309 syscallarg(mode_t) mode;
310 syscallarg(uint32_t) dev;
311 } */
312 return do_sys_mknod(l, SCARG(uap, path), SCARG(uap, mode),
313 SCARG(uap, dev), retval, UIO_USERSPACE);
314 }
315
316 /* ARGSUSED */
317 int
318 compat_50_sys_quotactl(struct lwp *l, const struct compat_50_sys_quotactl_args *uap, register_t *retval)
319 {
320 /* {
321 syscallarg(const char *) path;
322 syscallarg(int) cmd;
323 syscallarg(int) uid;
324 syscallarg(void *) arg;
325 } */
326 struct vnode *vp;
327 struct mount *mp;
328 int q1cmd;
329 int idtype;
330 char *qfile;
331 struct dqblk dqblk;
332 struct quotakey key;
333 struct quotaval blocks, files;
334 struct quotastat qstat;
335 int error;
336
337 error = namei_simple_user(SCARG(uap, path),
338 NSM_FOLLOW_TRYEMULROOT, &vp);
339 if (error != 0)
340 return (error);
341
342 mp = vp->v_mount;
343 q1cmd = SCARG(uap, cmd);
344 idtype = quota_idtype_from_ufs(q1cmd & SUBCMDMASK);
345
346 switch ((q1cmd & ~SUBCMDMASK) >> SUBCMDSHIFT) {
347 case Q_QUOTAON:
348 qfile = PNBUF_GET();
349 error = copyinstr(SCARG(uap, arg), qfile, PATH_MAX, NULL);
350 if (error != 0) {
351 PNBUF_PUT(qfile);
352 break;
353 }
354
355 error = vfs_quotactl_quotaon(mp, idtype, qfile);
356
357 PNBUF_PUT(qfile);
358 break;
359
360 case Q_QUOTAOFF:
361 error = vfs_quotactl_quotaoff(mp, idtype);
362 break;
363
364 case Q_GETQUOTA:
365 key.qk_idtype = idtype;
366 key.qk_id = SCARG(uap, uid);
367
368 key.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
369 error = vfs_quotactl_get(mp, &key, &blocks);
370 if (error) {
371 break;
372 }
373
374 key.qk_objtype = QUOTA_OBJTYPE_FILES;
375 error = vfs_quotactl_get(mp, &key, &files);
376 if (error) {
377 break;
378 }
379
380 quotavals_to_dqblk(&blocks, &files, &dqblk);
381 error = copyout(&dqblk, SCARG(uap, arg), sizeof(dqblk));
382 break;
383
384 case Q_SETQUOTA:
385 error = copyin(SCARG(uap, arg), &dqblk, sizeof(dqblk));
386 if (error) {
387 break;
388 }
389 dqblk_to_quotavals(&dqblk, &blocks, &files);
390
391 key.qk_idtype = idtype;
392 key.qk_id = SCARG(uap, uid);
393
394 key.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
395 error = vfs_quotactl_put(mp, &key, &blocks);
396 if (error) {
397 break;
398 }
399
400 key.qk_objtype = QUOTA_OBJTYPE_FILES;
401 error = vfs_quotactl_put(mp, &key, &files);
402 break;
403
404 case Q_SYNC:
405 /*
406 * not supported but used only to see if quota is supported,
407 * emulate with stat
408 *
409 * XXX should probably be supported
410 */
411 (void)idtype; /* not used */
412
413 error = vfs_quotactl_stat(mp, &qstat);
414 break;
415
416 case Q_SETUSE:
417 default:
418 error = EOPNOTSUPP;
419 break;
420 }
421
422 vrele(vp);
423 return error;
424 }
425