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