vfs_syscalls_50.c revision 1.9 1 /* $NetBSD: vfs_syscalls_50.c,v 1.9 2011/11/20 21:43:35 dholland 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.9 2011/11/20 21:43:35 dholland 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/malloc.h>
55 #include <sys/kauth.h>
56 #include <sys/time.h>
57 #include <sys/vfs_syscalls.h>
58 #ifndef LFS
59 #define LFS
60 #endif
61 #include <sys/syscallargs.h>
62
63 #include <ufs/lfs/lfs_extern.h>
64
65 #include <sys/quota.h>
66 #include <quota/quotaprop.h>
67 #include <ufs/ufs/quota1.h>
68
69 #include <compat/common/compat_util.h>
70 #include <compat/sys/time.h>
71 #include <compat/sys/stat.h>
72 #include <compat/sys/dirent.h>
73 #include <compat/sys/mount.h>
74
75 static void cvtstat(struct stat30 *, const struct stat *);
76
77 /*
78 * Convert from a new to an old stat structure.
79 */
80 static void
81 cvtstat(struct stat30 *ost, const struct stat *st)
82 {
83
84 ost->st_dev = st->st_dev;
85 ost->st_ino = st->st_ino;
86 ost->st_mode = st->st_mode;
87 ost->st_nlink = st->st_nlink;
88 ost->st_uid = st->st_uid;
89 ost->st_gid = st->st_gid;
90 ost->st_rdev = st->st_rdev;
91 timespec_to_timespec50(&st->st_atimespec, &ost->st_atimespec);
92 timespec_to_timespec50(&st->st_mtimespec, &ost->st_mtimespec);
93 timespec_to_timespec50(&st->st_ctimespec, &ost->st_ctimespec);
94 timespec_to_timespec50(&st->st_birthtimespec, &ost->st_birthtimespec);
95 ost->st_size = st->st_size;
96 ost->st_blocks = st->st_blocks;
97 ost->st_blksize = st->st_blksize;
98 ost->st_flags = st->st_flags;
99 ost->st_gen = st->st_gen;
100 memset(ost->st_spare, 0, sizeof(ost->st_spare));
101 }
102
103 /*
104 * Get file status; this version follows links.
105 */
106 /* ARGSUSED */
107 int
108 compat_50_sys___stat30(struct lwp *l, const struct compat_50_sys___stat30_args *uap, register_t *retval)
109 {
110 /* {
111 syscallarg(const char *) path;
112 syscallarg(struct stat30 *) ub;
113 } */
114 struct stat sb;
115 struct stat30 osb;
116 int error;
117
118 error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
119 if (error)
120 return error;
121 cvtstat(&osb, &sb);
122 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
123 return error;
124 }
125
126
127 /*
128 * Get file status; this version does not follow links.
129 */
130 /* ARGSUSED */
131 int
132 compat_50_sys___lstat30(struct lwp *l, const struct compat_50_sys___lstat30_args *uap, register_t *retval)
133 {
134 /* {
135 syscallarg(const char *) path;
136 syscallarg(struct stat30 *) ub;
137 } */
138 struct stat sb;
139 struct stat30 osb;
140 int error;
141
142 error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb);
143 if (error)
144 return error;
145 cvtstat(&osb, &sb);
146 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
147 return error;
148 }
149
150 /*
151 * Return status information about a file descriptor.
152 */
153 /* ARGSUSED */
154 int
155 compat_50_sys___fstat30(struct lwp *l, const struct compat_50_sys___fstat30_args *uap, register_t *retval)
156 {
157 /* {
158 syscallarg(int) fd;
159 syscallarg(struct stat30 *) sb;
160 } */
161 struct stat sb;
162 struct stat30 osb;
163 int error;
164
165 error = do_sys_fstat(SCARG(uap, fd), &sb);
166 if (error)
167 return error;
168 cvtstat(&osb, &sb);
169 error = copyout(&osb, SCARG(uap, sb), sizeof (osb));
170 return error;
171 }
172
173 /* ARGSUSED */
174 int
175 compat_50_sys___fhstat40(struct lwp *l, const struct compat_50_sys___fhstat40_args *uap, register_t *retval)
176 {
177 /* {
178 syscallarg(const void *) fhp;
179 syscallarg(size_t) fh_size;
180 syscallarg(struct stat30 *) sb;
181 } */
182 struct stat sb;
183 struct stat30 osb;
184 int error;
185
186 error = do_fhstat(l, SCARG(uap, fhp), SCARG(uap, fh_size), &sb);
187 if (error)
188 return error;
189 cvtstat(&osb, &sb);
190 error = copyout(&osb, SCARG(uap, sb), sizeof (osb));
191 return error;
192 }
193
194 static int
195 compat_50_do_sys_utimes(struct lwp *l, struct vnode *vp, const char *path,
196 int flag, const struct timeval50 *tptr)
197 {
198 struct timeval tv[2], *tvp;
199 struct timeval50 tv50[2];
200 if (tptr) {
201 int error = copyin(tptr, tv50, sizeof(tv50));
202 if (error)
203 return error;
204 timeval50_to_timeval(&tv50[0], &tv[0]);
205 timeval50_to_timeval(&tv50[1], &tv[1]);
206 tvp = tv;
207 } else
208 tvp = NULL;
209 return do_sys_utimes(l, vp, path, flag, tvp, UIO_SYSSPACE);
210 }
211
212 /*
213 * Set the access and modification times given a path name; this
214 * version follows links.
215 */
216 /* ARGSUSED */
217 int
218 compat_50_sys_utimes(struct lwp *l, const struct compat_50_sys_utimes_args *uap,
219 register_t *retval)
220 {
221 /* {
222 syscallarg(const char *) path;
223 syscallarg(const struct timeval50 *) tptr;
224 } */
225
226 return compat_50_do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
227 SCARG(uap, tptr));
228 }
229
230 /*
231 * Set the access and modification times given a file descriptor.
232 */
233 /* ARGSUSED */
234 int
235 compat_50_sys_futimes(struct lwp *l,
236 const struct compat_50_sys_futimes_args *uap, register_t *retval)
237 {
238 /* {
239 syscallarg(int) fd;
240 syscallarg(const struct timeval50 *) tptr;
241 } */
242 int error;
243 struct file *fp;
244
245 /* fd_getvnode() will use the descriptor for us */
246 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
247 return error;
248 error = compat_50_do_sys_utimes(l, fp->f_data, NULL, 0,
249 SCARG(uap, tptr));
250 fd_putfile(SCARG(uap, fd));
251 return error;
252 }
253
254 /*
255 * Set the access and modification times given a path name; this
256 * version does not follow links.
257 */
258 int
259 compat_50_sys_lutimes(struct lwp *l,
260 const struct compat_50_sys_lutimes_args *uap, register_t *retval)
261 {
262 /* {
263 syscallarg(const char *) path;
264 syscallarg(const struct timeval50 *) tptr;
265 } */
266
267 return compat_50_do_sys_utimes(l, NULL, SCARG(uap, path), NOFOLLOW,
268 SCARG(uap, tptr));
269 }
270
271 int
272 compat_50_sys_lfs_segwait(struct lwp *l,
273 const struct compat_50_sys_lfs_segwait_args *uap, register_t *retval)
274 {
275 /* {
276 syscallarg(fsid_t *) fsidp;
277 syscallarg(struct timeval50 *) tv;
278 } */
279 #ifdef notyet
280 struct timeval atv;
281 struct timeval50 atv50;
282 fsid_t fsid;
283 int error;
284
285 /* XXX need we be su to segwait? */
286 if ((error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
287 NULL)) != 0)
288 return (error);
289 if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
290 return (error);
291
292 if (SCARG(uap, tv)) {
293 error = copyin(SCARG(uap, tv), &atv50, sizeof(atv50));
294 if (error)
295 return (error);
296 timeval50_to_timeval(&atv50, &atv);
297 if (itimerfix(&atv))
298 return (EINVAL);
299 } else /* NULL or invalid */
300 atv.tv_sec = atv.tv_usec = 0;
301 return lfs_segwait(&fsid, &atv);
302 #else
303 return ENOSYS;
304 #endif
305 }
306
307 int
308 compat_50_sys_mknod(struct lwp *l,
309 const struct compat_50_sys_mknod_args *uap, register_t *retval)
310 {
311 /* {
312 syscallarg(const char *) path;
313 syscallarg(mode_t) mode;
314 syscallarg(uint32_t) dev;
315 } */
316 return do_sys_mknod(l, SCARG(uap, path), SCARG(uap, mode),
317 SCARG(uap, dev), retval, UIO_USERSPACE);
318 }
319
320 /* ARGSUSED */
321 int
322 compat_50_sys_quotactl(struct lwp *l, const struct compat_50_sys_quotactl_args *uap, register_t *retval)
323 {
324 /* {
325 syscallarg(const char *) path;
326 syscallarg(int) cmd;
327 syscallarg(int) uid;
328 syscallarg(void *) arg;
329 } */
330 struct mount *mp;
331 int error;
332 uint8_t error8;
333 struct vnode *vp;
334 int q1cmd = SCARG(uap, cmd);
335 prop_dictionary_t dict, data, cmd;
336 prop_array_t cmds, datas;
337 char *bufpath;
338 struct dqblk dqblk;
339 struct ufs_quota_entry qe[QUOTA_NLIMITS];
340 uint64_t *values[QUOTA_NLIMITS];
341
342 values[QUOTA_LIMIT_BLOCK] = &qe[QUOTA_LIMIT_BLOCK].ufsqe_hardlimit;
343 values[QUOTA_LIMIT_FILE] = &qe[QUOTA_LIMIT_FILE].ufsqe_hardlimit;
344
345 error = namei_simple_user(SCARG(uap, path),
346 NSM_FOLLOW_TRYEMULROOT, &vp);
347 if (error != 0)
348 return (error);
349 error = ENOMEM;
350 mp = vp->v_mount;
351 if ((dict = quota_prop_create()) == NULL)
352 goto out;
353 if ((cmds = prop_array_create()) == NULL)
354 goto out_dict;
355 if ((datas = prop_array_create()) == NULL)
356 goto out_cmds;
357
358 switch((q1cmd & ~SUBCMDMASK) >> SUBCMDSHIFT) {
359 case Q_QUOTAON:
360 data = prop_dictionary_create();
361 if (data == NULL)
362 goto out_datas;
363 bufpath = malloc(PATH_MAX * sizeof(char), M_TEMP, M_WAITOK);
364 if (bufpath == NULL)
365 goto out_data;
366 error = copyinstr(SCARG(uap, arg), bufpath, PATH_MAX, NULL);
367 if (error != 0) {
368 free(bufpath, M_TEMP);
369 goto out_data;
370 }
371 if (!prop_dictionary_set_cstring(data, "quotafile", bufpath))
372 error = ENOMEM;
373 free(bufpath, M_TEMP);
374 if (error)
375 goto out_data;
376 error = ENOMEM;
377 if (!prop_array_add_and_rel(datas, data))
378 goto out_datas;
379 if (!quota_prop_add_command(cmds, "quotaon",
380 ufs_quota_class_names[qtype2ufsclass(q1cmd & SUBCMDMASK)],
381 datas))
382 goto out_cmds;
383 goto do_quotaonoff;
384
385 case Q_QUOTAOFF:
386 error = ENOMEM;
387 if (!quota_prop_add_command(cmds, "quotaoff",
388 ufs_quota_class_names[qtype2ufsclass(q1cmd & SUBCMDMASK)],
389 datas))
390 goto out_cmds;
391 do_quotaonoff:
392 if (!prop_dictionary_set_and_rel(dict, "commands", cmds))
393 goto out_dict;
394 error = VFS_QUOTACTL(mp, dict);
395 if (error)
396 goto out_dict;
397 if ((error = quota_get_cmds(dict, &cmds)) != 0)
398 goto out_dict;
399 cmd = prop_array_get(cmds, 0);
400 if (cmd == NULL) {
401 error = EINVAL;
402 goto out_dict;
403 }
404 if (!prop_dictionary_get_int8(cmd, "return", &error8)) {
405 error = EINVAL;
406 goto out_dict;
407 }
408 error = error8;
409 goto out_dict;
410
411 case Q_GETQUOTA:
412 error = ENOMEM;
413 data = prop_dictionary_create();
414 if (data == NULL)
415 goto out_datas;
416 if (!prop_dictionary_set_uint32(data, "id", SCARG(uap, uid)))
417 goto out_data;
418 if (!prop_array_add_and_rel(datas, data))
419 goto out_datas;
420 if (!quota_prop_add_command(cmds, "get",
421 ufs_quota_class_names[qtype2ufsclass(q1cmd & SUBCMDMASK)],
422 datas))
423 goto out_cmds;
424 if (!prop_dictionary_set_and_rel(dict, "commands", cmds))
425 goto out_dict;
426 error = VFS_QUOTACTL(mp, dict);
427 if (error)
428 goto out_dict;
429 if ((error = quota_get_cmds(dict, &cmds)) != 0)
430 goto out_dict;
431 cmd = prop_array_get(cmds, 0);
432 if (cmd == NULL) {
433 error = EINVAL;
434 goto out_dict;
435 }
436 if (!prop_dictionary_get_int8(cmd, "return", &error8)) {
437 error = EINVAL;
438 goto out_dict;
439 }
440 error = error8;
441 if (error)
442 goto out_dict;
443 datas = prop_dictionary_get(cmd, "data");
444 error = EINVAL;
445 if (datas == NULL)
446 goto out_dict;
447 data = prop_array_get(datas, 0);
448 if (data == NULL)
449 goto out_dict;
450 error = proptoquota64(data, values,
451 ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
452 ufs_quota_limit_names, QUOTA_NLIMITS);
453 if (error)
454 goto out_dict;
455 ufsqe2dqblk(qe, &dqblk);
456 error = copyout(&dqblk, SCARG(uap, arg), sizeof(dqblk));
457 goto out_dict;
458
459 case Q_SETQUOTA:
460 error = copyin(SCARG(uap, arg), &dqblk, sizeof(dqblk));
461 if (error)
462 goto out_datas;
463 dqblk2ufsqe(&dqblk, qe);
464
465 error = ENOMEM;
466 data = quota64toprop(SCARG(uap, uid), 0, values,
467 ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
468 ufs_quota_limit_names, QUOTA_NLIMITS);
469 if (data == NULL)
470 goto out_data;
471 if (!prop_array_add_and_rel(datas, data))
472 goto out_datas;
473 if (!quota_prop_add_command(cmds, "set",
474 ufs_quota_class_names[qtype2ufsclass(q1cmd & SUBCMDMASK)],
475 datas))
476 goto out_cmds;
477 if (!prop_dictionary_set_and_rel(dict, "commands", cmds))
478 goto out_dict;
479 error = VFS_QUOTACTL(mp, dict);
480 if (error)
481 goto out_dict;
482 if ((error = quota_get_cmds(dict, &cmds)) != 0)
483 goto out_dict;
484 cmd = prop_array_get(cmds, 0);
485 if (cmd == NULL) {
486 error = EINVAL;
487 goto out_dict;
488 }
489 if (!prop_dictionary_get_int8(cmd, "return", &error8)) {
490 error = EINVAL;
491 goto out_dict;
492 }
493 error = error8;
494 goto out_dict;
495
496 case Q_SYNC:
497 /*
498 * not supported but used only to see if quota is supported,
499 * emulate with a "get version"
500 */
501 error = ENOMEM;
502 if (!quota_prop_add_command(cmds, "get version",
503 ufs_quota_class_names[qtype2ufsclass(q1cmd & SUBCMDMASK)],
504 datas))
505 goto out_cmds;
506 if (!prop_dictionary_set_and_rel(dict, "commands", cmds))
507 goto out_dict;
508 error = VFS_QUOTACTL(mp, dict);
509 if (error)
510 goto out_dict;
511 if ((error = quota_get_cmds(dict, &cmds)) != 0)
512 goto out_dict;
513 cmd = prop_array_get(cmds, 0);
514 if (cmd == NULL) {
515 error = EINVAL;
516 goto out_dict;
517 }
518 if (!prop_dictionary_get_int8(cmd, "return", &error8)) {
519 error = EINVAL;
520 goto out_dict;
521 }
522 error = error8;
523 goto out_dict;
524
525 case Q_SETUSE:
526 default:
527 error = EOPNOTSUPP;
528 goto out_datas;
529 }
530 out_data:
531 prop_object_release(data);
532 out_datas:
533 prop_object_release(datas);
534 out_cmds:
535 prop_object_release(cmds);
536 out_dict:
537 prop_object_release(dict);
538 out:
539 vrele(vp);
540 return error;
541 }
542