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