quota_cursor.c revision 1.3 1 1.3 dholland /* $NetBSD: quota_cursor.c,v 1.3 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.3 dholland __RCSID("$NetBSD: quota_cursor.c,v 1.3 2012/01/09 15:41:58 dholland Exp $");
33 1.1 dholland
34 1.1 dholland #include <stdlib.h>
35 1.1 dholland #include <errno.h>
36 1.1 dholland
37 1.1 dholland #include <quota.h>
38 1.2 dholland #include "quotapvt.h"
39 1.1 dholland
40 1.1 dholland struct quotacursor *
41 1.1 dholland quota_opencursor(struct quotahandle *qh)
42 1.1 dholland {
43 1.2 dholland struct quotacursor *qc;
44 1.3 dholland int8_t version;
45 1.2 dholland int serrno;
46 1.2 dholland
47 1.2 dholland if (qh->qh_isnfs) {
48 1.2 dholland errno = EOPNOTSUPP;
49 1.2 dholland return NULL;
50 1.2 dholland }
51 1.2 dholland
52 1.3 dholland if (__quota_proplib_getversion(qh, &version)) {
53 1.3 dholland return NULL;
54 1.3 dholland }
55 1.3 dholland
56 1.3 dholland /*
57 1.3 dholland * For the time being at least the version 1 kernel code
58 1.3 dholland * cannot do cursors.
59 1.3 dholland */
60 1.3 dholland if (version == 1 && !qh->qh_hasoldfiles) {
61 1.3 dholland if (__quota_oldfiles_initialize(qh)) {
62 1.3 dholland return NULL;
63 1.3 dholland }
64 1.3 dholland }
65 1.3 dholland
66 1.2 dholland qc = malloc(sizeof(*qc));
67 1.2 dholland if (qc == NULL) {
68 1.2 dholland return NULL;
69 1.2 dholland }
70 1.2 dholland
71 1.2 dholland qc->qc_qh = qh;
72 1.2 dholland
73 1.3 dholland if (version == 1) {
74 1.3 dholland qc->qc_type = QC_OLDFILES;
75 1.3 dholland qc->u.qc_oldfiles = __quota_oldfiles_cursor_create(qh);
76 1.3 dholland if (qc->u.qc_oldfiles == NULL) {
77 1.3 dholland serrno = errno;
78 1.3 dholland free(qc);
79 1.3 dholland errno = serrno;
80 1.3 dholland return NULL;
81 1.3 dholland }
82 1.3 dholland } else {
83 1.3 dholland qc->qc_type = QC_PROPLIB;
84 1.3 dholland qc->u.qc_proplib = __quota_proplib_cursor_create();
85 1.3 dholland if (qc->u.qc_proplib == NULL) {
86 1.3 dholland serrno = errno;
87 1.3 dholland free(qc);
88 1.3 dholland errno = serrno;
89 1.3 dholland return NULL;
90 1.3 dholland }
91 1.2 dholland }
92 1.2 dholland return qc;
93 1.1 dholland }
94 1.1 dholland
95 1.1 dholland void
96 1.1 dholland quotacursor_close(struct quotacursor *qc)
97 1.1 dholland {
98 1.3 dholland switch (qc->qc_type) {
99 1.3 dholland case QC_PROPLIB:
100 1.3 dholland __quota_proplib_cursor_destroy(qc->u.qc_proplib);
101 1.3 dholland break;
102 1.3 dholland case QC_OLDFILES:
103 1.3 dholland __quota_oldfiles_cursor_destroy(qc->u.qc_oldfiles);
104 1.3 dholland break;
105 1.3 dholland }
106 1.2 dholland free(qc);
107 1.1 dholland }
108 1.1 dholland
109 1.1 dholland int
110 1.1 dholland quotacursor_skipidtype(struct quotacursor *qc, unsigned idtype)
111 1.1 dholland {
112 1.3 dholland switch (qc->qc_type) {
113 1.3 dholland case QC_PROPLIB:
114 1.3 dholland return __quota_proplib_cursor_skipidtype(qc->u.qc_proplib,
115 1.3 dholland idtype);
116 1.3 dholland case QC_OLDFILES:
117 1.3 dholland return __quota_oldfiles_cursor_skipidtype(qc->u.qc_oldfiles,
118 1.3 dholland idtype);
119 1.3 dholland }
120 1.3 dholland errno = EINVAL;
121 1.3 dholland return -1;
122 1.1 dholland }
123 1.1 dholland
124 1.1 dholland int
125 1.1 dholland quotacursor_get(struct quotacursor *qc,
126 1.1 dholland struct quotakey *qk_ret, struct quotaval *qv_ret)
127 1.1 dholland {
128 1.3 dholland switch (qc->qc_type) {
129 1.3 dholland case QC_PROPLIB:
130 1.3 dholland return __quota_proplib_cursor_get(qc->qc_qh, qc->u.qc_proplib,
131 1.3 dholland qk_ret, qv_ret);
132 1.3 dholland case QC_OLDFILES:
133 1.3 dholland return __quota_oldfiles_cursor_get(qc->qc_qh,
134 1.3 dholland qc->u.qc_oldfiles,
135 1.3 dholland qk_ret, qv_ret);
136 1.3 dholland }
137 1.3 dholland errno = EINVAL;
138 1.3 dholland return -1;
139 1.1 dholland }
140 1.1 dholland
141 1.1 dholland int
142 1.1 dholland quotacursor_getn(struct quotacursor *qc,
143 1.1 dholland struct quotakey *keys, struct quotaval *vals,
144 1.1 dholland unsigned maxnum)
145 1.1 dholland {
146 1.3 dholland switch (qc->qc_type) {
147 1.3 dholland case QC_PROPLIB:
148 1.3 dholland return __quota_proplib_cursor_getn(qc->qc_qh, qc->u.qc_proplib,
149 1.3 dholland keys, vals, maxnum);
150 1.3 dholland case QC_OLDFILES:
151 1.3 dholland return __quota_oldfiles_cursor_getn(qc->qc_qh,
152 1.3 dholland qc->u.qc_oldfiles,
153 1.3 dholland keys, vals, maxnum);
154 1.3 dholland }
155 1.3 dholland errno = EINVAL;
156 1.3 dholland return -1;
157 1.1 dholland }
158 1.1 dholland
159 1.1 dholland int
160 1.1 dholland quotacursor_atend(struct quotacursor *qc)
161 1.1 dholland {
162 1.3 dholland switch (qc->qc_type) {
163 1.3 dholland case QC_PROPLIB:
164 1.3 dholland return __quota_proplib_cursor_atend(qc->qc_qh,
165 1.3 dholland qc->u.qc_proplib);
166 1.3 dholland case QC_OLDFILES:
167 1.3 dholland return __quota_oldfiles_cursor_atend(qc->u.qc_oldfiles);
168 1.3 dholland }
169 1.3 dholland errno = EINVAL;
170 1.3 dholland return -1;
171 1.1 dholland }
172 1.1 dholland
173 1.1 dholland int
174 1.1 dholland quotacursor_rewind(struct quotacursor *qc)
175 1.1 dholland {
176 1.3 dholland switch (qc->qc_type) {
177 1.3 dholland case QC_PROPLIB:
178 1.3 dholland return __quota_proplib_cursor_rewind(qc->u.qc_proplib);
179 1.3 dholland case QC_OLDFILES:
180 1.3 dholland return __quota_oldfiles_cursor_rewind(qc->u.qc_oldfiles);
181 1.3 dholland }
182 1.3 dholland errno = EINVAL;
183 1.3 dholland return -1;
184 1.1 dholland }
185