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