Home | History | Annotate | Line # | Download | only in libpuffs
requests.c revision 1.6
      1 /*	$NetBSD: requests.c,v 1.6 2007/05/09 18:36:52 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the company nor the name of the author may be used to
     15  *    endorse or promote products derived from this software without specific
     16  *    prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 #if !defined(lint)
     33 __RCSID("$NetBSD: requests.c,v 1.6 2007/05/09 18:36:52 pooka Exp $");
     34 #endif /* !lint */
     35 
     36 #include <sys/types.h>
     37 #include <sys/ioctl.h>
     38 #include <sys/queue.h>
     39 
     40 #include <assert.h>
     41 #include <puffs.h>
     42 #include <stdio.h>
     43 #include <stdlib.h>
     44 
     45 #include "puffs_priv.h"
     46 
     47 struct puffs_getreq *
     48 puffs_req_makeget(struct puffs_usermount *pu, size_t buflen, int maxops)
     49 {
     50 	struct puffs_getreq *pgr;
     51 	uint8_t *buf;
     52 
     53 	pgr = malloc(sizeof(struct puffs_getreq));
     54 	if (!pgr)
     55 		return NULL;
     56 
     57 	buf = malloc(buflen);
     58 	if (!buf) {
     59 		free(pgr);
     60 		return NULL;
     61 	}
     62 
     63 	pgr->pgr_phg_orig.phg_buf = buf;
     64 	pgr->pgr_phg_orig.phg_buflen = buflen;
     65 	pgr->pgr_phg_orig.phg_nops = maxops;
     66 
     67 	pgr->pgr_pu = pu;
     68 	pgr->pgr_nppr = 0;
     69 
     70 	return pgr;
     71 }
     72 
     73 int
     74 puffs_req_loadget(struct puffs_getreq *pgr)
     75 {
     76 
     77 	assert(pgr->pgr_nppr == 0);
     78 
     79 	/* reset */
     80 	pgr->pgr_phg = pgr->pgr_phg_orig;
     81 
     82 	if (ioctl(pgr->pgr_pu->pu_kargs.pa_fd,
     83 	    PUFFSGETOP, &pgr->pgr_phg) == -1)
     84 		return -1;
     85 
     86 	pgr->pgr_nextpreq = pgr->pgr_phg.phg_buf;
     87 	pgr->pgr_advance = pgr->pgr_nextpreq->preq_buflen;
     88 
     89 	return 0;
     90 }
     91 
     92 struct puffs_req *
     93 puffs_req_get(struct puffs_getreq *pgr)
     94 {
     95 	struct puffs_req *preq;
     96 
     97 	if (pgr->pgr_phg.phg_nops == 0)
     98 		return NULL;
     99 
    100 	preq = pgr->pgr_nextpreq;
    101 	/*LINTED*/
    102 	pgr->pgr_nextpreq =
    103 	    (struct puffs_req*)((uint8_t*)preq + pgr->pgr_advance);
    104 	pgr->pgr_advance = pgr->pgr_nextpreq->preq_buflen;
    105 	pgr->pgr_phg.phg_nops--;
    106 
    107 	return preq;
    108 }
    109 
    110 int
    111 puffs_req_remainingget(struct puffs_getreq *pgr)
    112 {
    113 
    114 	return pgr->pgr_phg.phg_nops;
    115 }
    116 
    117 void
    118 puffs_req_setmaxget(struct puffs_getreq *pgr, int maxops)
    119 {
    120 
    121 	pgr->pgr_phg.phg_nops = maxops;
    122 	pgr->pgr_phg_orig.phg_nops = maxops;
    123 }
    124 
    125 void
    126 puffs_req_destroyget(struct puffs_getreq *pgr)
    127 {
    128 
    129 	assert(pgr->pgr_nppr == 0);
    130 
    131 	free(pgr->pgr_phg_orig.phg_buf);
    132 	free(pgr);
    133 }
    134 
    135 
    136 struct puffs_putreq *
    137 puffs_req_makeput(struct puffs_usermount *pu)
    138 {
    139 	struct puffs_putreq *ppr;
    140 
    141 	ppr = malloc(sizeof(struct puffs_putreq));
    142 	if (!ppr)
    143 		return NULL;
    144 
    145 	ppr->ppr_php.php_nops = 0;
    146 	TAILQ_INIT(&ppr->ppr_pccq);
    147 
    148 	ppr->ppr_pu = pu;
    149 	ppr->ppr_pgr = NULL;
    150 
    151 	puffs_req_resetput(ppr);
    152 
    153 	return ppr;
    154 }
    155 
    156 void
    157 puffs_req_put(struct puffs_putreq *ppr, struct puffs_req *preq)
    158 {
    159 
    160 	ppr->ppr_php.php_nops++;
    161 
    162 	/* store data */
    163 	*ppr->ppr_buf = preq;
    164 	*ppr->ppr_buflen = preq->preq_buflen;
    165 	*ppr->ppr_id = preq->preq_id;
    166 
    167 	/* and roll forward for next request */
    168 	ppr->ppr_buf = &preq->preq_nextbuf;
    169 	ppr->ppr_buflen = &preq->preq_buflen;
    170 	ppr->ppr_id = &preq->preq_id;
    171 }
    172 
    173 /*
    174  * instead of a direct preq, put a cc onto the push queue
    175  */
    176 void
    177 puffs_req_putcc(struct puffs_putreq *ppr, struct puffs_cc *pcc)
    178 {
    179 
    180 	TAILQ_INSERT_TAIL(&ppr->ppr_pccq, pcc, entries);
    181 	puffs_req_put(ppr, pcc->pcc_preq);
    182 }
    183 
    184 int
    185 puffs_req_putput(struct puffs_putreq *ppr)
    186 {
    187 
    188 	if (ppr->ppr_php.php_nops)
    189 		if (ioctl(ppr->ppr_pu->pu_kargs.pa_fd,
    190 		    PUFFSPUTOP, &ppr->ppr_php) == -1)
    191 			return -1;
    192 
    193 	return 0;
    194 }
    195 
    196 void
    197 puffs_req_resetput(struct puffs_putreq *ppr)
    198 {
    199 	struct puffs_cc *pcc;
    200 
    201 	if (ppr->ppr_pgr != NULL) {
    202 		ppr->ppr_pgr->pgr_nppr--;
    203 		ppr->ppr_pgr = NULL;
    204 	}
    205 
    206 	ppr->ppr_buf = &ppr->ppr_php.php_buf;
    207 	ppr->ppr_buflen = &ppr->ppr_php.php_buflen;
    208 	ppr->ppr_id = &ppr->ppr_php.php_id;
    209 
    210 	while ((pcc = TAILQ_FIRST(&ppr->ppr_pccq)) != NULL) {
    211 		TAILQ_REMOVE(&ppr->ppr_pccq, pcc, entries);
    212 		puffs_cc_destroy(pcc);
    213 	}
    214 }
    215 
    216 void
    217 puffs_req_destroyput(struct puffs_putreq *ppr)
    218 {
    219 
    220 	puffs_req_resetput(ppr);
    221 	free(ppr);
    222 }
    223 
    224 int
    225 puffs_req_handle(struct puffs_getreq *pgr, struct puffs_putreq *ppr, int maxops)
    226 {
    227 	struct puffs_usermount *pu;
    228 	struct puffs_req *preq;
    229 	int pval;
    230 
    231 	assert(pgr->pgr_pu == ppr->ppr_pu);
    232 	pu = pgr->pgr_pu;
    233 
    234 	puffs_req_setmaxget(pgr, maxops);
    235 	if (puffs_req_loadget(pgr) == -1)
    236 		return -1;
    237 
    238 	/* interlink pgr and ppr for diagnostic asserts */
    239 	pgr->pgr_nppr++;
    240 	ppr->ppr_pgr = pgr;
    241 
    242 	pval = 0;
    243 	while ((preq = puffs_req_get(pgr)) != NULL
    244 	    && pu->pu_state != PUFFS_STATE_UNMOUNTED)
    245 		pval = puffs_dopreq(pu, preq, ppr);
    246 
    247 	return pval;
    248 }
    249