vfs_syscalls_50.c revision 1.21 1 /* $NetBSD: vfs_syscalls_50.c,v 1.21 2019/06/17 14:13:13 pgoyette 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.21 2019/06/17 14:13:13 pgoyette Exp $");
40
41 #if defined(_KERNEL_OPT)
42 #include "opt_compat_netbsd.h"
43 #include "opt_quota.h"
44 #endif
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/namei.h>
49 #include <sys/filedesc.h>
50 #include <sys/kernel.h>
51 #include <sys/file.h>
52 #include <sys/stat.h>
53 #include <sys/socketvar.h>
54 #include <sys/vnode.h>
55 #include <sys/mount.h>
56 #include <sys/proc.h>
57 #include <sys/uio.h>
58 #include <sys/dirent.h>
59 #include <sys/kauth.h>
60 #include <sys/time.h>
61 #include <sys/syscall.h>
62 #include <sys/syscallvar.h>
63 #include <sys/syscallargs.h>
64 #include <sys/vfs_syscalls.h>
65 #ifndef LFS
66 #define LFS
67 #endif
68 #include <sys/syscallargs.h>
69
70 #include <ufs/lfs/lfs_extern.h>
71
72 #ifdef QUOTA
73 #include <sys/quota.h>
74 #include <sys/quotactl.h>
75 #include <ufs/ufs/quota1.h>
76 #endif
77
78 #include <compat/common/compat_util.h>
79 #include <compat/common/compat_mod.h>
80 #include <compat/sys/time.h>
81 #include <compat/sys/stat.h>
82 #include <compat/sys/dirent.h>
83 #include <compat/sys/mount.h>
84
85 static void cvtstat(struct stat30 *, const struct stat *);
86
87 static const struct syscall_package vfs_syscalls_50_syscalls[] = {
88 { SYS_compat_50___stat30, 0, (sy_call_t *)compat_50_sys___stat30 },
89 { SYS_compat_50___fstat30, 0, (sy_call_t *)compat_50_sys___fstat30 },
90 { SYS_compat_50___lstat30, 0, (sy_call_t *)compat_50_sys___lstat30 },
91 { SYS_compat_50___fhstat40, 0, (sy_call_t *)compat_50_sys___fhstat40 },
92 { SYS_compat_50_utimes, 0, (sy_call_t *)compat_50_sys_utimes },
93 { SYS_compat_50_lfs_segwait, 0,
94 (sy_call_t *)compat_50_sys_lfs_segwait } ,
95 { SYS_compat_50_futimes, 0, (sy_call_t *)compat_50_sys_futimes },
96 { SYS_compat_50_lutimes, 0, (sy_call_t *)compat_50_sys_lutimes },
97 { SYS_compat_50_mknod, 0, (sy_call_t *)compat_50_sys_mknod },
98 { SYS_compat_50_quotactl, 0, (sy_call_t *)compat_50_sys_quotactl },
99 { 0, 0, NULL }
100 };
101
102 /*
103 * Convert from a new to an old stat structure.
104 */
105 static void
106 cvtstat(struct stat30 *ost, const struct stat *st)
107 {
108
109 ost->st_dev = st->st_dev;
110 ost->st_ino = st->st_ino;
111 ost->st_mode = st->st_mode;
112 ost->st_nlink = st->st_nlink;
113 ost->st_uid = st->st_uid;
114 ost->st_gid = st->st_gid;
115 ost->st_rdev = st->st_rdev;
116 timespec_to_timespec50(&st->st_atimespec, &ost->st_atimespec);
117 timespec_to_timespec50(&st->st_mtimespec, &ost->st_mtimespec);
118 timespec_to_timespec50(&st->st_ctimespec, &ost->st_ctimespec);
119 timespec_to_timespec50(&st->st_birthtimespec, &ost->st_birthtimespec);
120 ost->st_size = st->st_size;
121 ost->st_blocks = st->st_blocks;
122 ost->st_blksize = st->st_blksize;
123 ost->st_flags = st->st_flags;
124 ost->st_gen = st->st_gen;
125 memset(ost->st_spare, 0, sizeof(ost->st_spare));
126 }
127
128 /*
129 * Get file status; this version follows links.
130 */
131 /* ARGSUSED */
132 int
133 compat_50_sys___stat30(struct lwp *l, const struct compat_50_sys___stat30_args *uap, register_t *retval)
134 {
135 /* {
136 syscallarg(const char *) path;
137 syscallarg(struct stat30 *) ub;
138 } */
139 struct stat sb;
140 struct stat30 osb;
141 int error;
142
143 error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
144 if (error)
145 return error;
146 cvtstat(&osb, &sb);
147 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
148 return error;
149 }
150
151
152 /*
153 * Get file status; this version does not follow links.
154 */
155 /* ARGSUSED */
156 int
157 compat_50_sys___lstat30(struct lwp *l, const struct compat_50_sys___lstat30_args *uap, register_t *retval)
158 {
159 /* {
160 syscallarg(const char *) path;
161 syscallarg(struct stat30 *) ub;
162 } */
163 struct stat sb;
164 struct stat30 osb;
165 int error;
166
167 error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb);
168 if (error)
169 return error;
170 cvtstat(&osb, &sb);
171 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
172 return error;
173 }
174
175 /*
176 * Return status information about a file descriptor.
177 */
178 /* ARGSUSED */
179 int
180 compat_50_sys___fstat30(struct lwp *l, const struct compat_50_sys___fstat30_args *uap, register_t *retval)
181 {
182 /* {
183 syscallarg(int) fd;
184 syscallarg(struct stat30 *) sb;
185 } */
186 struct stat sb;
187 struct stat30 osb;
188 int error;
189
190 error = do_sys_fstat(SCARG(uap, fd), &sb);
191 if (error)
192 return error;
193 cvtstat(&osb, &sb);
194 error = copyout(&osb, SCARG(uap, sb), sizeof (osb));
195 return error;
196 }
197
198 /* ARGSUSED */
199 int
200 compat_50_sys___fhstat40(struct lwp *l, const struct compat_50_sys___fhstat40_args *uap, register_t *retval)
201 {
202 /* {
203 syscallarg(const void *) fhp;
204 syscallarg(size_t) fh_size;
205 syscallarg(struct stat30 *) sb;
206 } */
207 struct stat sb;
208 struct stat30 osb;
209 int error;
210
211 error = do_fhstat(l, SCARG(uap, fhp), SCARG(uap, fh_size), &sb);
212 if (error)
213 return error;
214 cvtstat(&osb, &sb);
215 error = copyout(&osb, SCARG(uap, sb), sizeof (osb));
216 return error;
217 }
218
219 static int
220 compat_50_do_sys_utimes(struct lwp *l, struct vnode *vp, const char *path,
221 int flag, const struct timeval50 *tptr)
222 {
223 struct timeval tv[2], *tvp;
224 struct timeval50 tv50[2];
225 if (tptr) {
226 int error = copyin(tptr, tv50, sizeof(tv50));
227 if (error)
228 return error;
229 timeval50_to_timeval(&tv50[0], &tv[0]);
230 timeval50_to_timeval(&tv50[1], &tv[1]);
231 tvp = tv;
232 } else
233 tvp = NULL;
234 return do_sys_utimes(l, vp, path, flag, tvp, UIO_SYSSPACE);
235 }
236
237 /*
238 * Set the access and modification times given a path name; this
239 * version follows links.
240 */
241 /* ARGSUSED */
242 int
243 compat_50_sys_utimes(struct lwp *l, const struct compat_50_sys_utimes_args *uap,
244 register_t *retval)
245 {
246 /* {
247 syscallarg(const char *) path;
248 syscallarg(const struct timeval50 *) tptr;
249 } */
250
251 return compat_50_do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
252 SCARG(uap, tptr));
253 }
254
255 /*
256 * Set the access and modification times given a file descriptor.
257 */
258 /* ARGSUSED */
259 int
260 compat_50_sys_futimes(struct lwp *l,
261 const struct compat_50_sys_futimes_args *uap, register_t *retval)
262 {
263 /* {
264 syscallarg(int) fd;
265 syscallarg(const struct timeval50 *) tptr;
266 } */
267 int error;
268 struct file *fp;
269
270 /* fd_getvnode() will use the descriptor for us */
271 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
272 return error;
273 error = compat_50_do_sys_utimes(l, fp->f_vnode, NULL, 0,
274 SCARG(uap, tptr));
275 fd_putfile(SCARG(uap, fd));
276 return error;
277 }
278
279 /*
280 * Set the access and modification times given a path name; this
281 * version does not follow links.
282 */
283 int
284 compat_50_sys_lutimes(struct lwp *l,
285 const struct compat_50_sys_lutimes_args *uap, register_t *retval)
286 {
287 /* {
288 syscallarg(const char *) path;
289 syscallarg(const struct timeval50 *) tptr;
290 } */
291
292 return compat_50_do_sys_utimes(l, NULL, SCARG(uap, path), NOFOLLOW,
293 SCARG(uap, tptr));
294 }
295
296 int
297 compat_50_sys_lfs_segwait(struct lwp *l,
298 const struct compat_50_sys_lfs_segwait_args *uap, register_t *retval)
299 {
300 /* {
301 syscallarg(fsid_t *) fsidp;
302 syscallarg(struct timeval50 *) tv;
303 } */
304 #ifdef notyet
305 /* XXX need to check presence of LFS at run-time XXX */
306 struct timeval atv;
307 struct timeval50 atv50;
308 fsid_t fsid;
309 int error;
310
311 /* XXX need we be su to segwait? */
312 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
313 KAUTH_REQ_SYSTEM_LFS_SEGWAIT, NULL, NULL, NULL);
314 if (error)
315 return (error);
316 if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
317 return (error);
318
319 if (SCARG(uap, tv)) {
320 error = copyin(SCARG(uap, tv), &atv50, sizeof(atv50));
321 if (error)
322 return (error);
323 timeval50_to_timeval(&atv50, &atv);
324 if (itimerfix(&atv))
325 return (EINVAL);
326 } else /* NULL or invalid */
327 atv.tv_sec = atv.tv_usec = 0;
328 return lfs_segwait(&fsid, &atv);
329 #else
330 return ENOSYS;
331 #endif
332 }
333
334 int
335 compat_50_sys_mknod(struct lwp *l,
336 const struct compat_50_sys_mknod_args *uap, register_t *retval)
337 {
338 /* {
339 syscallarg(const char *) path;
340 syscallarg(mode_t) mode;
341 syscallarg(uint32_t) dev;
342 } */
343 return do_sys_mknod(l, SCARG(uap, path), SCARG(uap, mode),
344 SCARG(uap, dev), retval, UIO_USERSPACE);
345 }
346
347 #ifdef QUOTA
348 /* ARGSUSED */
349 int
350 compat_50_sys_quotactl(struct lwp *l, const struct compat_50_sys_quotactl_args *uap, register_t *retval)
351 {
352 /* {
353 syscallarg(const char *) path;
354 syscallarg(int) cmd;
355 syscallarg(int) uid;
356 syscallarg(void *) arg;
357 } */
358 struct vnode *vp;
359 struct mount *mp;
360 int q1cmd;
361 int idtype;
362 char *qfile;
363 struct dqblk dqblk;
364 struct quotakey key;
365 struct quotaval blocks, files;
366 struct quotastat qstat;
367 int error;
368
369 error = namei_simple_user(SCARG(uap, path),
370 NSM_FOLLOW_TRYEMULROOT, &vp);
371 if (error != 0)
372 return (error);
373
374 mp = vp->v_mount;
375 q1cmd = SCARG(uap, cmd);
376 idtype = quota_idtype_from_ufs(q1cmd & SUBCMDMASK);
377
378 switch ((q1cmd & ~SUBCMDMASK) >> SUBCMDSHIFT) {
379 case Q_QUOTAON:
380 qfile = PNBUF_GET();
381 error = copyinstr(SCARG(uap, arg), qfile, PATH_MAX, NULL);
382 if (error != 0) {
383 PNBUF_PUT(qfile);
384 break;
385 }
386
387 error = vfs_quotactl_quotaon(mp, idtype, qfile);
388
389 PNBUF_PUT(qfile);
390 break;
391
392 case Q_QUOTAOFF:
393 error = vfs_quotactl_quotaoff(mp, idtype);
394 break;
395
396 case Q_GETQUOTA:
397 key.qk_idtype = idtype;
398 key.qk_id = SCARG(uap, uid);
399
400 key.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
401 error = vfs_quotactl_get(mp, &key, &blocks);
402 if (error) {
403 break;
404 }
405
406 key.qk_objtype = QUOTA_OBJTYPE_FILES;
407 error = vfs_quotactl_get(mp, &key, &files);
408 if (error) {
409 break;
410 }
411
412 quotavals_to_dqblk(&blocks, &files, &dqblk);
413 error = copyout(&dqblk, SCARG(uap, arg), sizeof(dqblk));
414 break;
415
416 case Q_SETQUOTA:
417 error = copyin(SCARG(uap, arg), &dqblk, sizeof(dqblk));
418 if (error) {
419 break;
420 }
421 dqblk_to_quotavals(&dqblk, &blocks, &files);
422
423 key.qk_idtype = idtype;
424 key.qk_id = SCARG(uap, uid);
425
426 key.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
427 error = vfs_quotactl_put(mp, &key, &blocks);
428 if (error) {
429 break;
430 }
431
432 key.qk_objtype = QUOTA_OBJTYPE_FILES;
433 error = vfs_quotactl_put(mp, &key, &files);
434 break;
435
436 case Q_SYNC:
437 /*
438 * not supported but used only to see if quota is supported,
439 * emulate with stat
440 *
441 * XXX should probably be supported
442 */
443 (void)idtype; /* not used */
444
445 error = vfs_quotactl_stat(mp, &qstat);
446 break;
447
448 case Q_SETUSE:
449 default:
450 error = EOPNOTSUPP;
451 break;
452 }
453
454 vrele(vp);
455 return error;
456 }
457 #endif
458
459 int
460 vfs_syscalls_50_init(void)
461 {
462
463 return syscall_establish(NULL, vfs_syscalls_50_syscalls);
464 }
465
466 int
467 vfs_syscalls_50_fini(void)
468 {
469
470 return syscall_disestablish(NULL, vfs_syscalls_50_syscalls);
471 }
472