1*d111e83dSdholland /* $NetBSD: compat_50_quota.c,v 1.4 2022/09/21 07:15:24 dholland Exp $ */
209a5fcafSpgoyette
309a5fcafSpgoyette /*-
409a5fcafSpgoyette * Copyright (c) 2008 The NetBSD Foundation, Inc.
509a5fcafSpgoyette * All rights reserved.
609a5fcafSpgoyette *
709a5fcafSpgoyette * This code is derived from software contributed to The NetBSD Foundation
809a5fcafSpgoyette * by Christos Zoulas.
909a5fcafSpgoyette *
1009a5fcafSpgoyette * Redistribution and use in source and binary forms, with or without
1109a5fcafSpgoyette * modification, are permitted provided that the following conditions
1209a5fcafSpgoyette * are met:
1309a5fcafSpgoyette * 1. Redistributions of source code must retain the above copyright
1409a5fcafSpgoyette * notice, this list of conditions and the following disclaimer.
1509a5fcafSpgoyette * 2. Redistributions in binary form must reproduce the above copyright
1609a5fcafSpgoyette * notice, this list of conditions and the following disclaimer in the
1709a5fcafSpgoyette * documentation and/or other materials provided with the distribution.
1809a5fcafSpgoyette *
1909a5fcafSpgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2009a5fcafSpgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2109a5fcafSpgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2209a5fcafSpgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2309a5fcafSpgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2409a5fcafSpgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2509a5fcafSpgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2609a5fcafSpgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2709a5fcafSpgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2809a5fcafSpgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2909a5fcafSpgoyette * POSSIBILITY OF SUCH DAMAGE.
3009a5fcafSpgoyette */
3109a5fcafSpgoyette #include <sys/cdefs.h>
32*d111e83dSdholland __KERNEL_RCSID(0, "$NetBSD: compat_50_quota.c,v 1.4 2022/09/21 07:15:24 dholland Exp $");
3309a5fcafSpgoyette
3409a5fcafSpgoyette #if defined(_KERNEL_OPT)
3509a5fcafSpgoyette #include "opt_compat_netbsd.h"
3609a5fcafSpgoyette #endif
3709a5fcafSpgoyette
3809a5fcafSpgoyette #include <sys/module.h>
3909a5fcafSpgoyette #include <sys/namei.h>
4009a5fcafSpgoyette #include <sys/param.h>
4109a5fcafSpgoyette #include <sys/quota.h>
4209a5fcafSpgoyette #include <sys/quotactl.h>
4309a5fcafSpgoyette #include <sys/systm.h>
4409a5fcafSpgoyette #include <sys/syscall.h>
4509a5fcafSpgoyette #include <sys/syscallvar.h>
4609a5fcafSpgoyette #include <sys/syscallargs.h>
4709a5fcafSpgoyette #include <sys/vfs_syscalls.h>
4809a5fcafSpgoyette #include <sys/vnode.h>
4909a5fcafSpgoyette
5009a5fcafSpgoyette #include <ufs/ufs/quota1.h>
5109a5fcafSpgoyette
5209a5fcafSpgoyette static const struct syscall_package vfs_syscalls_50_quota_syscalls[] = {
5309a5fcafSpgoyette { SYS_compat_50_quotactl, 0, (sy_call_t *)compat_50_sys_quotactl },
5409a5fcafSpgoyette { 0, 0, NULL }
5509a5fcafSpgoyette };
5609a5fcafSpgoyette
5709a5fcafSpgoyette /* ARGSUSED */
5809a5fcafSpgoyette int
compat_50_sys_quotactl(struct lwp * l,const struct compat_50_sys_quotactl_args * uap,register_t * retval)5909a5fcafSpgoyette compat_50_sys_quotactl(struct lwp *l, const struct compat_50_sys_quotactl_args *uap, register_t *retval)
6009a5fcafSpgoyette {
6109a5fcafSpgoyette /* {
6209a5fcafSpgoyette syscallarg(const char *) path;
6309a5fcafSpgoyette syscallarg(int) cmd;
6409a5fcafSpgoyette syscallarg(int) uid;
6509a5fcafSpgoyette syscallarg(void *) arg;
6609a5fcafSpgoyette } */
6709a5fcafSpgoyette struct vnode *vp;
6809a5fcafSpgoyette struct mount *mp;
6909a5fcafSpgoyette int q1cmd;
7009a5fcafSpgoyette int idtype;
7109a5fcafSpgoyette char *qfile;
7209a5fcafSpgoyette struct dqblk dqblk;
7309a5fcafSpgoyette struct quotakey key;
7409a5fcafSpgoyette struct quotaval blocks, files;
7509a5fcafSpgoyette struct quotastat qstat;
7609a5fcafSpgoyette int error;
7709a5fcafSpgoyette
7809a5fcafSpgoyette error = namei_simple_user(SCARG(uap, path),
7909a5fcafSpgoyette NSM_FOLLOW_TRYEMULROOT, &vp);
8009a5fcafSpgoyette if (error != 0)
8109a5fcafSpgoyette return (error);
8209a5fcafSpgoyette
8309a5fcafSpgoyette mp = vp->v_mount;
8409a5fcafSpgoyette q1cmd = SCARG(uap, cmd);
8509a5fcafSpgoyette idtype = quota_idtype_from_ufs(q1cmd & SUBCMDMASK);
86*d111e83dSdholland if (idtype == -1) {
87*d111e83dSdholland return EINVAL;
88*d111e83dSdholland }
8909a5fcafSpgoyette
9009a5fcafSpgoyette switch ((q1cmd & ~SUBCMDMASK) >> SUBCMDSHIFT) {
9109a5fcafSpgoyette case Q_QUOTAON:
9209a5fcafSpgoyette qfile = PNBUF_GET();
9309a5fcafSpgoyette error = copyinstr(SCARG(uap, arg), qfile, PATH_MAX, NULL);
9409a5fcafSpgoyette if (error != 0) {
9509a5fcafSpgoyette PNBUF_PUT(qfile);
9609a5fcafSpgoyette break;
9709a5fcafSpgoyette }
9809a5fcafSpgoyette
9909a5fcafSpgoyette error = vfs_quotactl_quotaon(mp, idtype, qfile);
10009a5fcafSpgoyette
10109a5fcafSpgoyette PNBUF_PUT(qfile);
10209a5fcafSpgoyette break;
10309a5fcafSpgoyette
10409a5fcafSpgoyette case Q_QUOTAOFF:
10509a5fcafSpgoyette error = vfs_quotactl_quotaoff(mp, idtype);
10609a5fcafSpgoyette break;
10709a5fcafSpgoyette
10809a5fcafSpgoyette case Q_GETQUOTA:
10909a5fcafSpgoyette key.qk_idtype = idtype;
11009a5fcafSpgoyette key.qk_id = SCARG(uap, uid);
11109a5fcafSpgoyette
11209a5fcafSpgoyette key.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
11309a5fcafSpgoyette error = vfs_quotactl_get(mp, &key, &blocks);
11409a5fcafSpgoyette if (error) {
11509a5fcafSpgoyette break;
11609a5fcafSpgoyette }
11709a5fcafSpgoyette
11809a5fcafSpgoyette key.qk_objtype = QUOTA_OBJTYPE_FILES;
11909a5fcafSpgoyette error = vfs_quotactl_get(mp, &key, &files);
12009a5fcafSpgoyette if (error) {
12109a5fcafSpgoyette break;
12209a5fcafSpgoyette }
12309a5fcafSpgoyette
12409a5fcafSpgoyette quotavals_to_dqblk(&blocks, &files, &dqblk);
12509a5fcafSpgoyette error = copyout(&dqblk, SCARG(uap, arg), sizeof(dqblk));
12609a5fcafSpgoyette break;
12709a5fcafSpgoyette
12809a5fcafSpgoyette case Q_SETQUOTA:
12909a5fcafSpgoyette error = copyin(SCARG(uap, arg), &dqblk, sizeof(dqblk));
13009a5fcafSpgoyette if (error) {
13109a5fcafSpgoyette break;
13209a5fcafSpgoyette }
13309a5fcafSpgoyette dqblk_to_quotavals(&dqblk, &blocks, &files);
13409a5fcafSpgoyette
13509a5fcafSpgoyette key.qk_idtype = idtype;
13609a5fcafSpgoyette key.qk_id = SCARG(uap, uid);
13709a5fcafSpgoyette
13809a5fcafSpgoyette key.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
13909a5fcafSpgoyette error = vfs_quotactl_put(mp, &key, &blocks);
14009a5fcafSpgoyette if (error) {
14109a5fcafSpgoyette break;
14209a5fcafSpgoyette }
14309a5fcafSpgoyette
14409a5fcafSpgoyette key.qk_objtype = QUOTA_OBJTYPE_FILES;
14509a5fcafSpgoyette error = vfs_quotactl_put(mp, &key, &files);
14609a5fcafSpgoyette break;
14709a5fcafSpgoyette
14809a5fcafSpgoyette case Q_SYNC:
14909a5fcafSpgoyette /*
15009a5fcafSpgoyette * not supported but used only to see if quota is supported,
15109a5fcafSpgoyette * emulate with stat
15209a5fcafSpgoyette *
15309a5fcafSpgoyette * XXX should probably be supported
15409a5fcafSpgoyette */
15509a5fcafSpgoyette (void)idtype; /* not used */
15609a5fcafSpgoyette
15709a5fcafSpgoyette error = vfs_quotactl_stat(mp, &qstat);
15809a5fcafSpgoyette break;
15909a5fcafSpgoyette
16009a5fcafSpgoyette case Q_SETUSE:
16109a5fcafSpgoyette default:
16209a5fcafSpgoyette error = EOPNOTSUPP;
16309a5fcafSpgoyette break;
16409a5fcafSpgoyette }
16509a5fcafSpgoyette
16609a5fcafSpgoyette vrele(vp);
16709a5fcafSpgoyette return error;
16809a5fcafSpgoyette }
16909a5fcafSpgoyette
1703d2be0a6Spgoyette MODULE(MODULE_CLASS_EXEC, compat_50_quota, "compat_50,ufs");
17109a5fcafSpgoyette
17209a5fcafSpgoyette static int
compat_50_quota_modcmd(modcmd_t cmd,void * arg)17309a5fcafSpgoyette compat_50_quota_modcmd(modcmd_t cmd, void *arg)
17409a5fcafSpgoyette {
17509a5fcafSpgoyette
17609a5fcafSpgoyette switch (cmd) {
17709a5fcafSpgoyette case MODULE_CMD_INIT:
17809a5fcafSpgoyette return syscall_establish(NULL, vfs_syscalls_50_quota_syscalls);
17909a5fcafSpgoyette case MODULE_CMD_FINI:
18009a5fcafSpgoyette return syscall_disestablish(NULL, vfs_syscalls_50_quota_syscalls);
18109a5fcafSpgoyette default:
18209a5fcafSpgoyette return ENOTTY;
18309a5fcafSpgoyette }
18409a5fcafSpgoyette }
185