quota_open.c revision 1.4 1 1.4 dholland /* $NetBSD: quota_open.c,v 1.4 2012/01/09 15:41:58 dholland Exp $ */
2 1.1 dholland /*-
3 1.1 dholland * Copyright (c) 2011 The NetBSD Foundation, Inc.
4 1.1 dholland * All rights reserved.
5 1.1 dholland *
6 1.1 dholland * This code is derived from software contributed to The NetBSD Foundation
7 1.1 dholland * by David A. Holland.
8 1.1 dholland *
9 1.1 dholland * Redistribution and use in source and binary forms, with or without
10 1.1 dholland * modification, are permitted provided that the following conditions
11 1.1 dholland * are met:
12 1.1 dholland * 1. Redistributions of source code must retain the above copyright
13 1.1 dholland * notice, this list of conditions and the following disclaimer.
14 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 dholland * notice, this list of conditions and the following disclaimer in the
16 1.1 dholland * documentation and/or other materials provided with the distribution.
17 1.1 dholland *
18 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 dholland * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 dholland * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 dholland * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 dholland * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 dholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 dholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 dholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 dholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 dholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 dholland * POSSIBILITY OF SUCH DAMAGE.
29 1.1 dholland */
30 1.1 dholland
31 1.1 dholland #include <sys/cdefs.h>
32 1.4 dholland __RCSID("$NetBSD: quota_open.c,v 1.4 2012/01/09 15:41:58 dholland Exp $");
33 1.1 dholland
34 1.3 dholland #include <sys/types.h>
35 1.3 dholland #include <sys/statvfs.h>
36 1.2 dholland #include <stdlib.h>
37 1.1 dholland #include <string.h>
38 1.4 dholland #include <unistd.h>
39 1.1 dholland #include <errno.h>
40 1.1 dholland
41 1.1 dholland #include <quota.h>
42 1.2 dholland #include "quotapvt.h"
43 1.1 dholland
44 1.1 dholland struct quotahandle *
45 1.1 dholland quota_open(const char *path)
46 1.1 dholland {
47 1.3 dholland
48 1.3 dholland struct statvfs stv;
49 1.2 dholland struct quotahandle *qh;
50 1.3 dholland int isnfs;
51 1.2 dholland int serrno;
52 1.2 dholland
53 1.3 dholland if (statvfs(path, &stv) < 0) {
54 1.3 dholland return NULL;
55 1.3 dholland }
56 1.3 dholland
57 1.3 dholland /*
58 1.3 dholland * We need to know up front if the volume is NFS. If so, we
59 1.3 dholland * don't go to the kernel at all but instead do RPCs to the
60 1.3 dholland * NFS server's rpc.rquotad. Therefore, don't check the
61 1.3 dholland * mount flags for quota support or do anything else that
62 1.3 dholland * reaches the kernel.
63 1.3 dholland */
64 1.3 dholland
65 1.3 dholland if (!strcmp(stv.f_fstypename, "nfs")) {
66 1.3 dholland isnfs = 1;
67 1.3 dholland } else {
68 1.3 dholland isnfs = 0;
69 1.3 dholland if ((stv.f_flag & ST_QUOTA) == 0) {
70 1.3 dholland /* XXX: correct errno? */
71 1.3 dholland errno = EOPNOTSUPP;
72 1.3 dholland return NULL;
73 1.3 dholland }
74 1.3 dholland }
75 1.3 dholland
76 1.2 dholland qh = malloc(sizeof(*qh));
77 1.2 dholland if (qh == NULL) {
78 1.2 dholland return NULL;
79 1.2 dholland }
80 1.3 dholland
81 1.3 dholland /*
82 1.3 dholland * Get the mount point from statvfs; this way the passed-in
83 1.3 dholland * path can be any path on the volume.
84 1.3 dholland */
85 1.3 dholland
86 1.3 dholland qh->qh_mountpoint = strdup(stv.f_mntonname);
87 1.2 dholland if (qh->qh_mountpoint == NULL) {
88 1.2 dholland serrno = errno;
89 1.2 dholland free(qh);
90 1.2 dholland errno = serrno;
91 1.2 dholland return NULL;
92 1.2 dholland }
93 1.3 dholland
94 1.3 dholland qh->qh_mountdevice = strdup(stv.f_mntfromname);
95 1.3 dholland if (qh->qh_mountdevice == NULL) {
96 1.3 dholland serrno = errno;
97 1.3 dholland free(qh->qh_mountpoint);
98 1.3 dholland free(qh);
99 1.3 dholland errno = serrno;
100 1.3 dholland return NULL;
101 1.3 dholland }
102 1.3 dholland
103 1.3 dholland qh->qh_isnfs = isnfs;
104 1.3 dholland
105 1.4 dholland qh->qh_hasoldfiles = 0;
106 1.4 dholland qh->qh_userfile = -1;
107 1.4 dholland qh->qh_groupfile = -1;
108 1.4 dholland
109 1.2 dholland return qh;
110 1.1 dholland }
111 1.1 dholland
112 1.1 dholland const char *
113 1.1 dholland quota_getmountpoint(struct quotahandle *qh)
114 1.1 dholland {
115 1.2 dholland return qh->qh_mountpoint;
116 1.1 dholland }
117 1.1 dholland
118 1.1 dholland const char *
119 1.1 dholland quota_getmountdevice(struct quotahandle *qh)
120 1.1 dholland {
121 1.3 dholland return qh->qh_mountdevice;
122 1.1 dholland }
123 1.1 dholland
124 1.1 dholland void
125 1.1 dholland quota_close(struct quotahandle *qh)
126 1.1 dholland {
127 1.4 dholland if (qh->qh_userfile >= 0) {
128 1.4 dholland close(qh->qh_userfile);
129 1.4 dholland }
130 1.4 dholland if (qh->qh_groupfile >= 0) {
131 1.4 dholland close(qh->qh_groupfile);
132 1.4 dholland }
133 1.3 dholland free(qh->qh_mountdevice);
134 1.2 dholland free(qh->qh_mountpoint);
135 1.2 dholland free(qh);
136 1.1 dholland }
137