quota_open.c revision 1.7.4.2 1 1.7.4.2 yamt /* $NetBSD: quota_open.c,v 1.7.4.2 2012/04/17 00:05:32 yamt Exp $ */
2 1.7.4.2 yamt /*-
3 1.7.4.2 yamt * Copyright (c) 2011 The NetBSD Foundation, Inc.
4 1.7.4.2 yamt * All rights reserved.
5 1.7.4.2 yamt *
6 1.7.4.2 yamt * This code is derived from software contributed to The NetBSD Foundation
7 1.7.4.2 yamt * by David A. Holland.
8 1.7.4.2 yamt *
9 1.7.4.2 yamt * Redistribution and use in source and binary forms, with or without
10 1.7.4.2 yamt * modification, are permitted provided that the following conditions
11 1.7.4.2 yamt * are met:
12 1.7.4.2 yamt * 1. Redistributions of source code must retain the above copyright
13 1.7.4.2 yamt * notice, this list of conditions and the following disclaimer.
14 1.7.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
15 1.7.4.2 yamt * notice, this list of conditions and the following disclaimer in the
16 1.7.4.2 yamt * documentation and/or other materials provided with the distribution.
17 1.7.4.2 yamt *
18 1.7.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.7.4.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.7.4.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.7.4.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.7.4.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.7.4.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.7.4.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.7.4.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.7.4.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.7.4.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.7.4.2 yamt * POSSIBILITY OF SUCH DAMAGE.
29 1.7.4.2 yamt */
30 1.7.4.2 yamt
31 1.7.4.2 yamt #include <sys/cdefs.h>
32 1.7.4.2 yamt __RCSID("$NetBSD: quota_open.c,v 1.7.4.2 2012/04/17 00:05:32 yamt Exp $");
33 1.7.4.2 yamt
34 1.7.4.2 yamt #include <sys/types.h>
35 1.7.4.2 yamt #include <sys/statvfs.h>
36 1.7.4.2 yamt #include <stdlib.h>
37 1.7.4.2 yamt #include <string.h>
38 1.7.4.2 yamt #include <unistd.h>
39 1.7.4.2 yamt #include <errno.h>
40 1.7.4.2 yamt
41 1.7.4.2 yamt #include <quota.h>
42 1.7.4.2 yamt #include "quotapvt.h"
43 1.7.4.2 yamt
44 1.7.4.2 yamt struct quotahandle *
45 1.7.4.2 yamt quota_open(const char *path)
46 1.7.4.2 yamt {
47 1.7.4.2 yamt
48 1.7.4.2 yamt struct statvfs stv;
49 1.7.4.2 yamt struct quotahandle *qh;
50 1.7.4.2 yamt int mode;
51 1.7.4.2 yamt int serrno;
52 1.7.4.2 yamt
53 1.7.4.2 yamt /*
54 1.7.4.2 yamt * Probe order:
55 1.7.4.2 yamt *
56 1.7.4.2 yamt * 1. Check for NFS. NFS quota ops don't go to the kernel
57 1.7.4.2 yamt * at all but instead do RPCs to the NFS server's
58 1.7.4.2 yamt * rpc.rquotad, so it doesn't matter what the kernel
59 1.7.4.2 yamt * thinks.
60 1.7.4.2 yamt *
61 1.7.4.2 yamt * 2. Check if quotas are enabled in the mount flags. If
62 1.7.4.2 yamt * so, we can do quotactl calls.
63 1.7.4.2 yamt *
64 1.7.4.2 yamt * 3. Check if the volume is listed in fstab as one of
65 1.7.4.2 yamt * the filesystem types supported by quota_oldfiles.c,
66 1.7.4.2 yamt * and with the proper mount options to enable quotas.
67 1.7.4.2 yamt *
68 1.7.4.2 yamt * Note that (as of this writing) the mount options for
69 1.7.4.2 yamt * enabling quotas are accepted by mount for *all* filesystem
70 1.7.4.2 yamt * types and then ignored -- the kernel mount flag (ST_QUOTA /
71 1.7.4.2 yamt * MNT_QUOTA) gets set either by the filesystem based on its
72 1.7.4.2 yamt * own criteria, or for old-style quotas, during quotaon. The
73 1.7.4.2 yamt * quota filenames specified in fstab are not passed to or
74 1.7.4.2 yamt * known by the kernel except via quota_oldfiles.c! This is
75 1.7.4.2 yamt * generally gross but not easily fixed.
76 1.7.4.2 yamt */
77 1.7.4.2 yamt
78 1.7.4.2 yamt if (statvfs(path, &stv) < 0) {
79 1.7.4.2 yamt return NULL;
80 1.7.4.2 yamt }
81 1.7.4.2 yamt
82 1.7.4.2 yamt __quota_oldfiles_load_fstab();
83 1.7.4.2 yamt
84 1.7.4.2 yamt if (!strcmp(stv.f_fstypename, "nfs")) {
85 1.7.4.2 yamt mode = QUOTA_MODE_NFS;
86 1.7.4.2 yamt } else if ((stv.f_flag & ST_QUOTA) != 0) {
87 1.7.4.2 yamt mode = QUOTA_MODE_KERNEL;
88 1.7.4.2 yamt } else if (__quota_oldfiles_infstab(stv.f_mntonname)) {
89 1.7.4.2 yamt mode = QUOTA_MODE_OLDFILES;
90 1.7.4.2 yamt } else {
91 1.7.4.2 yamt errno = EOPNOTSUPP;
92 1.7.4.2 yamt return NULL;
93 1.7.4.2 yamt }
94 1.7.4.2 yamt
95 1.7.4.2 yamt qh = malloc(sizeof(*qh));
96 1.7.4.2 yamt if (qh == NULL) {
97 1.7.4.2 yamt return NULL;
98 1.7.4.2 yamt }
99 1.7.4.2 yamt
100 1.7.4.2 yamt /*
101 1.7.4.2 yamt * Get the mount point from statvfs; this way the passed-in
102 1.7.4.2 yamt * path can be any path on the volume.
103 1.7.4.2 yamt */
104 1.7.4.2 yamt
105 1.7.4.2 yamt qh->qh_mountpoint = strdup(stv.f_mntonname);
106 1.7.4.2 yamt if (qh->qh_mountpoint == NULL) {
107 1.7.4.2 yamt serrno = errno;
108 1.7.4.2 yamt free(qh);
109 1.7.4.2 yamt errno = serrno;
110 1.7.4.2 yamt return NULL;
111 1.7.4.2 yamt }
112 1.7.4.2 yamt
113 1.7.4.2 yamt qh->qh_mountdevice = strdup(stv.f_mntfromname);
114 1.7.4.2 yamt if (qh->qh_mountdevice == NULL) {
115 1.7.4.2 yamt serrno = errno;
116 1.7.4.2 yamt free(qh->qh_mountpoint);
117 1.7.4.2 yamt free(qh);
118 1.7.4.2 yamt errno = serrno;
119 1.7.4.2 yamt return NULL;
120 1.7.4.2 yamt }
121 1.7.4.2 yamt
122 1.7.4.2 yamt qh->qh_mode = mode;
123 1.7.4.2 yamt
124 1.7.4.2 yamt qh->qh_oldfilesopen = 0;
125 1.7.4.2 yamt qh->qh_userfile = -1;
126 1.7.4.2 yamt qh->qh_groupfile = -1;
127 1.7.4.2 yamt
128 1.7.4.2 yamt return qh;
129 1.7.4.2 yamt }
130 1.7.4.2 yamt
131 1.7.4.2 yamt const char *
132 1.7.4.2 yamt quota_getmountpoint(struct quotahandle *qh)
133 1.7.4.2 yamt {
134 1.7.4.2 yamt return qh->qh_mountpoint;
135 1.7.4.2 yamt }
136 1.7.4.2 yamt
137 1.7.4.2 yamt const char *
138 1.7.4.2 yamt quota_getmountdevice(struct quotahandle *qh)
139 1.7.4.2 yamt {
140 1.7.4.2 yamt return qh->qh_mountdevice;
141 1.7.4.2 yamt }
142 1.7.4.2 yamt
143 1.7.4.2 yamt void
144 1.7.4.2 yamt quota_close(struct quotahandle *qh)
145 1.7.4.2 yamt {
146 1.7.4.2 yamt if (qh->qh_userfile >= 0) {
147 1.7.4.2 yamt close(qh->qh_userfile);
148 1.7.4.2 yamt }
149 1.7.4.2 yamt if (qh->qh_groupfile >= 0) {
150 1.7.4.2 yamt close(qh->qh_groupfile);
151 1.7.4.2 yamt }
152 1.7.4.2 yamt free(qh->qh_mountdevice);
153 1.7.4.2 yamt free(qh->qh_mountpoint);
154 1.7.4.2 yamt free(qh);
155 1.7.4.2 yamt }
156 1.7.4.2 yamt
157 1.7.4.2 yamt int
158 1.7.4.2 yamt quota_quotaon(struct quotahandle *qh, int idtype)
159 1.7.4.2 yamt {
160 1.7.4.2 yamt switch (qh->qh_mode) {
161 1.7.4.2 yamt case QUOTA_MODE_NFS:
162 1.7.4.2 yamt errno = EOPNOTSUPP;
163 1.7.4.2 yamt break;
164 1.7.4.2 yamt case QUOTA_MODE_OLDFILES:
165 1.7.4.2 yamt return __quota_oldfiles_quotaon(qh, idtype);
166 1.7.4.2 yamt case QUOTA_MODE_KERNEL:
167 1.7.4.2 yamt return __quota_kernel_quotaon(qh, idtype);
168 1.7.4.2 yamt default:
169 1.7.4.2 yamt errno = EINVAL;
170 1.7.4.2 yamt break;
171 1.7.4.2 yamt }
172 1.7.4.2 yamt return -1;
173 1.7.4.2 yamt }
174 1.7.4.2 yamt
175 1.7.4.2 yamt int
176 1.7.4.2 yamt quota_quotaoff(struct quotahandle *qh, int idtype)
177 1.7.4.2 yamt {
178 1.7.4.2 yamt switch (qh->qh_mode) {
179 1.7.4.2 yamt case QUOTA_MODE_NFS:
180 1.7.4.2 yamt errno = EOPNOTSUPP;
181 1.7.4.2 yamt break;
182 1.7.4.2 yamt case QUOTA_MODE_OLDFILES:
183 1.7.4.2 yamt /* can't quotaoff if we haven't quotaon'd */
184 1.7.4.2 yamt errno = ENOTCONN;
185 1.7.4.2 yamt break;
186 1.7.4.2 yamt case QUOTA_MODE_KERNEL:
187 1.7.4.2 yamt return __quota_kernel_quotaoff(qh, idtype);
188 1.7.4.2 yamt default:
189 1.7.4.2 yamt errno = EINVAL;
190 1.7.4.2 yamt break;
191 1.7.4.2 yamt }
192 1.7.4.2 yamt return -1;
193 1.7.4.2 yamt }
194