xref: /netbsd-src/lib/libquota/quota_open.c (revision 59b296daa791bbc901611368081fd58de91bfc66)
1*59b296daSdholland /*	$NetBSD: quota_open.c,v 1.7 2012/02/01 05:34:40 dholland Exp $	*/
2bb96a166Sdholland /*-
3bb96a166Sdholland  * Copyright (c) 2011 The NetBSD Foundation, Inc.
4bb96a166Sdholland  * All rights reserved.
5bb96a166Sdholland  *
6bb96a166Sdholland  * This code is derived from software contributed to The NetBSD Foundation
7bb96a166Sdholland  * by David A. Holland.
8bb96a166Sdholland  *
9bb96a166Sdholland  * Redistribution and use in source and binary forms, with or without
10bb96a166Sdholland  * modification, are permitted provided that the following conditions
11bb96a166Sdholland  * are met:
12bb96a166Sdholland  * 1. Redistributions of source code must retain the above copyright
13bb96a166Sdholland  *    notice, this list of conditions and the following disclaimer.
14bb96a166Sdholland  * 2. Redistributions in binary form must reproduce the above copyright
15bb96a166Sdholland  *    notice, this list of conditions and the following disclaimer in the
16bb96a166Sdholland  *    documentation and/or other materials provided with the distribution.
17bb96a166Sdholland  *
18bb96a166Sdholland  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19bb96a166Sdholland  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20bb96a166Sdholland  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21bb96a166Sdholland  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22bb96a166Sdholland  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23bb96a166Sdholland  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24bb96a166Sdholland  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25bb96a166Sdholland  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26bb96a166Sdholland  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27bb96a166Sdholland  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28bb96a166Sdholland  * POSSIBILITY OF SUCH DAMAGE.
29bb96a166Sdholland  */
30bb96a166Sdholland 
31bb96a166Sdholland #include <sys/cdefs.h>
32*59b296daSdholland __RCSID("$NetBSD: quota_open.c,v 1.7 2012/02/01 05:34:40 dholland Exp $");
33bb96a166Sdholland 
342957f1ccSdholland #include <sys/types.h>
352957f1ccSdholland #include <sys/statvfs.h>
368d8965acSdholland #include <stdlib.h>
37bb96a166Sdholland #include <string.h>
388d70e807Sdholland #include <unistd.h>
39bb96a166Sdholland #include <errno.h>
40bb96a166Sdholland 
41bb96a166Sdholland #include <quota.h>
428d8965acSdholland #include "quotapvt.h"
43bb96a166Sdholland 
44bb96a166Sdholland struct quotahandle *
quota_open(const char * path)45bb96a166Sdholland quota_open(const char *path)
46bb96a166Sdholland {
472957f1ccSdholland 
482957f1ccSdholland 	struct statvfs stv;
498d8965acSdholland 	struct quotahandle *qh;
50fa577c93Sdholland 	int mode;
518d8965acSdholland 	int serrno;
528d8965acSdholland 
53fa577c93Sdholland 	/*
54fa577c93Sdholland 	 * Probe order:
55fa577c93Sdholland 	 *
56fa577c93Sdholland 	 *    1. Check for NFS. NFS quota ops don't go to the kernel
57fa577c93Sdholland 	 *    at all but instead do RPCs to the NFS server's
58fa577c93Sdholland 	 *    rpc.rquotad, so it doesn't matter what the kernel
59fa577c93Sdholland 	 *    thinks.
60fa577c93Sdholland 	 *
61fa577c93Sdholland 	 *    2. Check if quotas are enabled in the mount flags. If
62fa577c93Sdholland 	 *    so, we can do quotactl calls.
63fa577c93Sdholland 	 *
64fa577c93Sdholland 	 *    3. Check if the volume is listed in fstab as one of
65fa577c93Sdholland 	 *    the filesystem types supported by quota_oldfiles.c,
66fa577c93Sdholland 	 *    and with the proper mount options to enable quotas.
675f0d2c5fSdholland 	 *
685f0d2c5fSdholland 	 * Note that (as of this writing) the mount options for
695f0d2c5fSdholland 	 * enabling quotas are accepted by mount for *all* filesystem
705f0d2c5fSdholland 	 * types and then ignored -- the kernel mount flag (ST_QUOTA /
715f0d2c5fSdholland 	 * MNT_QUOTA) gets set either by the filesystem based on its
725f0d2c5fSdholland 	 * own criteria, or for old-style quotas, during quotaon. The
735f0d2c5fSdholland 	 * quota filenames specified in fstab are not passed to or
745f0d2c5fSdholland 	 * known by the kernel except via quota_oldfiles.c! This is
755f0d2c5fSdholland 	 * generally gross but not easily fixed.
76fa577c93Sdholland 	 */
77fa577c93Sdholland 
782957f1ccSdholland 	if (statvfs(path, &stv) < 0) {
792957f1ccSdholland 		return NULL;
802957f1ccSdholland 	}
812957f1ccSdholland 
82fa577c93Sdholland 	__quota_oldfiles_load_fstab();
832957f1ccSdholland 
842957f1ccSdholland 	if (!strcmp(stv.f_fstypename, "nfs")) {
85fa577c93Sdholland 		mode = QUOTA_MODE_NFS;
86fa577c93Sdholland 	} else if ((stv.f_flag & ST_QUOTA) != 0) {
87*59b296daSdholland 		mode = QUOTA_MODE_KERNEL;
88fa577c93Sdholland 	} else if (__quota_oldfiles_infstab(stv.f_mntonname)) {
89fa577c93Sdholland 		mode = QUOTA_MODE_OLDFILES;
902957f1ccSdholland 	} else {
912957f1ccSdholland 		errno = EOPNOTSUPP;
922957f1ccSdholland 		return NULL;
932957f1ccSdholland 	}
942957f1ccSdholland 
958d8965acSdholland 	qh = malloc(sizeof(*qh));
968d8965acSdholland 	if (qh == NULL) {
97bb96a166Sdholland 		return NULL;
98bb96a166Sdholland 	}
992957f1ccSdholland 
1002957f1ccSdholland 	/*
1012957f1ccSdholland 	 * Get the mount point from statvfs; this way the passed-in
1022957f1ccSdholland 	 * path can be any path on the volume.
1032957f1ccSdholland 	 */
1042957f1ccSdholland 
1052957f1ccSdholland 	qh->qh_mountpoint = strdup(stv.f_mntonname);
1068d8965acSdholland 	if (qh->qh_mountpoint == NULL) {
1078d8965acSdholland 		serrno = errno;
1088d8965acSdholland 		free(qh);
1098d8965acSdholland 		errno = serrno;
1108d8965acSdholland 		return NULL;
1118d8965acSdholland 	}
1122957f1ccSdholland 
1132957f1ccSdholland 	qh->qh_mountdevice = strdup(stv.f_mntfromname);
1142957f1ccSdholland 	if (qh->qh_mountdevice == NULL) {
1152957f1ccSdholland 		serrno = errno;
1162957f1ccSdholland 		free(qh->qh_mountpoint);
1172957f1ccSdholland 		free(qh);
1182957f1ccSdholland 		errno = serrno;
1192957f1ccSdholland 		return NULL;
1202957f1ccSdholland 	}
1212957f1ccSdholland 
122fa577c93Sdholland 	qh->qh_mode = mode;
1232957f1ccSdholland 
124fa577c93Sdholland 	qh->qh_oldfilesopen = 0;
1258d70e807Sdholland 	qh->qh_userfile = -1;
1268d70e807Sdholland 	qh->qh_groupfile = -1;
1278d70e807Sdholland 
1288d8965acSdholland 	return qh;
1298d8965acSdholland }
130bb96a166Sdholland 
131bb96a166Sdholland const char *
quota_getmountpoint(struct quotahandle * qh)132bb96a166Sdholland quota_getmountpoint(struct quotahandle *qh)
133bb96a166Sdholland {
1348d8965acSdholland 	return qh->qh_mountpoint;
135bb96a166Sdholland }
136bb96a166Sdholland 
137bb96a166Sdholland const char *
quota_getmountdevice(struct quotahandle * qh)138bb96a166Sdholland quota_getmountdevice(struct quotahandle *qh)
139bb96a166Sdholland {
1402957f1ccSdholland 	return qh->qh_mountdevice;
141bb96a166Sdholland }
142bb96a166Sdholland 
143bb96a166Sdholland void
quota_close(struct quotahandle * qh)144bb96a166Sdholland quota_close(struct quotahandle *qh)
145bb96a166Sdholland {
1468d70e807Sdholland 	if (qh->qh_userfile >= 0) {
1478d70e807Sdholland 		close(qh->qh_userfile);
1488d70e807Sdholland 	}
1498d70e807Sdholland 	if (qh->qh_groupfile >= 0) {
1508d70e807Sdholland 		close(qh->qh_groupfile);
1518d70e807Sdholland 	}
1522957f1ccSdholland 	free(qh->qh_mountdevice);
1538d8965acSdholland 	free(qh->qh_mountpoint);
1548d8965acSdholland 	free(qh);
155bb96a166Sdholland }
1565f0d2c5fSdholland 
1575f0d2c5fSdholland int
quota_quotaon(struct quotahandle * qh,int idtype)1585f0d2c5fSdholland quota_quotaon(struct quotahandle *qh, int idtype)
1595f0d2c5fSdholland {
1605f0d2c5fSdholland 	switch (qh->qh_mode) {
1615f0d2c5fSdholland 	    case QUOTA_MODE_NFS:
1625f0d2c5fSdholland 		errno = EOPNOTSUPP;
1635f0d2c5fSdholland 		break;
1645f0d2c5fSdholland 	    case QUOTA_MODE_OLDFILES:
1655f0d2c5fSdholland 		return __quota_oldfiles_quotaon(qh, idtype);
166*59b296daSdholland 	    case QUOTA_MODE_KERNEL:
167*59b296daSdholland 		return __quota_kernel_quotaon(qh, idtype);
1685f0d2c5fSdholland 	    default:
1695f0d2c5fSdholland 		errno = EINVAL;
1705f0d2c5fSdholland 		break;
1715f0d2c5fSdholland 	}
1725f0d2c5fSdholland 	return -1;
1735f0d2c5fSdholland }
1745f0d2c5fSdholland 
1755f0d2c5fSdholland int
quota_quotaoff(struct quotahandle * qh,int idtype)1765f0d2c5fSdholland quota_quotaoff(struct quotahandle *qh, int idtype)
1775f0d2c5fSdholland {
1785f0d2c5fSdholland 	switch (qh->qh_mode) {
1795f0d2c5fSdholland 	    case QUOTA_MODE_NFS:
1805f0d2c5fSdholland 		errno = EOPNOTSUPP;
1815f0d2c5fSdholland 		break;
1825f0d2c5fSdholland 	    case QUOTA_MODE_OLDFILES:
1835f0d2c5fSdholland 		/* can't quotaoff if we haven't quotaon'd */
1845f0d2c5fSdholland 		errno = ENOTCONN;
1855f0d2c5fSdholland 		break;
186*59b296daSdholland 	    case QUOTA_MODE_KERNEL:
187*59b296daSdholland 		return __quota_kernel_quotaoff(qh, idtype);
1885f0d2c5fSdholland 	    default:
1895f0d2c5fSdholland 		errno = EINVAL;
1905f0d2c5fSdholland 		break;
1915f0d2c5fSdholland 	}
1925f0d2c5fSdholland 	return -1;
1935f0d2c5fSdholland }
194