1 1.6 dholland /* $NetBSD: quota_cursor.c,v 1.6 2012/02/01 05:46:46 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.6 dholland __RCSID("$NetBSD: quota_cursor.c,v 1.6 2012/02/01 05:46:46 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.5 dholland unsigned restrictions; 45 1.2 dholland int serrno; 46 1.2 dholland 47 1.4 dholland switch (qh->qh_mode) { 48 1.4 dholland case QUOTA_MODE_NFS: 49 1.2 dholland errno = EOPNOTSUPP; 50 1.2 dholland return NULL; 51 1.2 dholland 52 1.5 dholland case QUOTA_MODE_OLDFILES: 53 1.5 dholland restrictions = QUOTA_RESTRICT_NEEDSQUOTACHECK; 54 1.4 dholland break; 55 1.4 dholland 56 1.5 dholland case QUOTA_MODE_KERNEL: 57 1.5 dholland restrictions = __quota_kernel_getrestrictions(qh); 58 1.4 dholland break; 59 1.4 dholland 60 1.4 dholland default: 61 1.4 dholland errno = EINVAL; 62 1.5 dholland return NULL; 63 1.3 dholland } 64 1.3 dholland 65 1.3 dholland /* 66 1.3 dholland * For the time being at least the version 1 kernel code 67 1.3 dholland * cannot do cursors. 68 1.3 dholland */ 69 1.5 dholland if ((restrictions & QUOTA_RESTRICT_NEEDSQUOTACHECK) != 0 && 70 1.5 dholland !qh->qh_oldfilesopen) { 71 1.3 dholland if (__quota_oldfiles_initialize(qh)) { 72 1.3 dholland return NULL; 73 1.3 dholland } 74 1.3 dholland } 75 1.3 dholland 76 1.2 dholland qc = malloc(sizeof(*qc)); 77 1.2 dholland if (qc == NULL) { 78 1.2 dholland return NULL; 79 1.2 dholland } 80 1.2 dholland 81 1.2 dholland qc->qc_qh = qh; 82 1.2 dholland 83 1.5 dholland if ((restrictions & QUOTA_RESTRICT_NEEDSQUOTACHECK) != 0) { 84 1.3 dholland qc->qc_type = QC_OLDFILES; 85 1.3 dholland qc->u.qc_oldfiles = __quota_oldfiles_cursor_create(qh); 86 1.3 dholland if (qc->u.qc_oldfiles == NULL) { 87 1.3 dholland serrno = errno; 88 1.3 dholland free(qc); 89 1.3 dholland errno = serrno; 90 1.3 dholland return NULL; 91 1.3 dholland } 92 1.3 dholland } else { 93 1.5 dholland qc->qc_type = QC_KERNEL; 94 1.5 dholland qc->u.qc_kernel = __quota_kernel_cursor_create(qh); 95 1.5 dholland if (qc->u.qc_kernel == NULL) { 96 1.3 dholland serrno = errno; 97 1.3 dholland free(qc); 98 1.3 dholland errno = serrno; 99 1.3 dholland return NULL; 100 1.3 dholland } 101 1.2 dholland } 102 1.2 dholland return qc; 103 1.1 dholland } 104 1.1 dholland 105 1.1 dholland void 106 1.1 dholland quotacursor_close(struct quotacursor *qc) 107 1.1 dholland { 108 1.3 dholland switch (qc->qc_type) { 109 1.3 dholland case QC_OLDFILES: 110 1.3 dholland __quota_oldfiles_cursor_destroy(qc->u.qc_oldfiles); 111 1.3 dholland break; 112 1.5 dholland case QC_KERNEL: 113 1.5 dholland __quota_kernel_cursor_destroy(qc->qc_qh, qc->u.qc_kernel); 114 1.5 dholland break; 115 1.3 dholland } 116 1.2 dholland free(qc); 117 1.1 dholland } 118 1.1 dholland 119 1.1 dholland int 120 1.6 dholland quotacursor_skipidtype(struct quotacursor *qc, int idtype) 121 1.1 dholland { 122 1.3 dholland switch (qc->qc_type) { 123 1.3 dholland case QC_OLDFILES: 124 1.3 dholland return __quota_oldfiles_cursor_skipidtype(qc->u.qc_oldfiles, 125 1.3 dholland idtype); 126 1.5 dholland case QC_KERNEL: 127 1.5 dholland return __quota_kernel_cursor_skipidtype(qc->qc_qh, 128 1.5 dholland qc->u.qc_kernel, 129 1.5 dholland idtype); 130 1.3 dholland } 131 1.3 dholland errno = EINVAL; 132 1.3 dholland return -1; 133 1.1 dholland } 134 1.1 dholland 135 1.1 dholland int 136 1.1 dholland quotacursor_get(struct quotacursor *qc, 137 1.1 dholland struct quotakey *qk_ret, struct quotaval *qv_ret) 138 1.1 dholland { 139 1.3 dholland switch (qc->qc_type) { 140 1.3 dholland case QC_OLDFILES: 141 1.3 dholland return __quota_oldfiles_cursor_get(qc->qc_qh, 142 1.3 dholland qc->u.qc_oldfiles, 143 1.3 dholland qk_ret, qv_ret); 144 1.5 dholland case QC_KERNEL: 145 1.5 dholland return __quota_kernel_cursor_get(qc->qc_qh, qc->u.qc_kernel, 146 1.5 dholland qk_ret, qv_ret); 147 1.3 dholland } 148 1.3 dholland errno = EINVAL; 149 1.3 dholland return -1; 150 1.1 dholland } 151 1.1 dholland 152 1.1 dholland int 153 1.1 dholland quotacursor_getn(struct quotacursor *qc, 154 1.1 dholland struct quotakey *keys, struct quotaval *vals, 155 1.1 dholland unsigned maxnum) 156 1.1 dholland { 157 1.3 dholland switch (qc->qc_type) { 158 1.3 dholland case QC_OLDFILES: 159 1.3 dholland return __quota_oldfiles_cursor_getn(qc->qc_qh, 160 1.3 dholland qc->u.qc_oldfiles, 161 1.3 dholland keys, vals, maxnum); 162 1.5 dholland case QC_KERNEL: 163 1.5 dholland return __quota_kernel_cursor_getn(qc->qc_qh, qc->u.qc_kernel, 164 1.5 dholland keys, vals, maxnum); 165 1.3 dholland } 166 1.3 dholland errno = EINVAL; 167 1.3 dholland return -1; 168 1.1 dholland } 169 1.1 dholland 170 1.1 dholland int 171 1.1 dholland quotacursor_atend(struct quotacursor *qc) 172 1.1 dholland { 173 1.3 dholland switch (qc->qc_type) { 174 1.3 dholland case QC_OLDFILES: 175 1.3 dholland return __quota_oldfiles_cursor_atend(qc->u.qc_oldfiles); 176 1.5 dholland 177 1.5 dholland case QC_KERNEL: 178 1.5 dholland return __quota_kernel_cursor_atend(qc->qc_qh, qc->u.qc_kernel); 179 1.3 dholland } 180 1.3 dholland errno = EINVAL; 181 1.3 dholland return -1; 182 1.1 dholland } 183 1.1 dholland 184 1.1 dholland int 185 1.1 dholland quotacursor_rewind(struct quotacursor *qc) 186 1.1 dholland { 187 1.3 dholland switch (qc->qc_type) { 188 1.3 dholland case QC_OLDFILES: 189 1.3 dholland return __quota_oldfiles_cursor_rewind(qc->u.qc_oldfiles); 190 1.5 dholland case QC_KERNEL: 191 1.5 dholland return __quota_kernel_cursor_rewind(qc->qc_qh,qc->u.qc_kernel); 192 1.3 dholland } 193 1.3 dholland errno = EINVAL; 194 1.3 dholland return -1; 195 1.1 dholland } 196