Home | History | Annotate | Line # | Download | only in libpuffs
requests.c revision 1.9.6.2
      1 /*	$NetBSD: requests.c,v 1.9.6.2 2007/07/19 12:52:29 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.9.6.2 2007/07/19 12:52:29 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_fd, PUFFSGETOP, &pgr->pgr_phg) == -1)
     80 		return -1;
     81 
     82 	pgr->pgr_nextpreq = pgr->pgr_phg.phg_buf;
     83 	pgr->pgr_advance = pgr->pgr_nextpreq->preq_buflen;
     84 
     85 	return 0;
     86 }
     87 
     88 struct puffs_req *
     89 puffs_req_get(struct puffs_getreq *pgr)
     90 {
     91 	struct puffs_req *preq;
     92 
     93 	if (pgr->pgr_phg.phg_nops == 0)
     94 		return NULL;
     95 
     96 	preq = pgr->pgr_nextpreq;
     97 	/*LINTED*/
     98 	pgr->pgr_nextpreq =
     99 	    (struct puffs_req*)((uint8_t*)preq + pgr->pgr_advance);
    100 	pgr->pgr_advance = pgr->pgr_nextpreq->preq_buflen;
    101 	pgr->pgr_phg.phg_nops--;
    102 
    103 	return preq;
    104 }
    105 
    106 int
    107 puffs_req_remainingget(struct puffs_getreq *pgr)
    108 {
    109 
    110 	return pgr->pgr_phg.phg_nops;
    111 }
    112 
    113 void
    114 puffs_req_setmaxget(struct puffs_getreq *pgr, int maxops)
    115 {
    116 
    117 	pgr->pgr_phg.phg_nops = maxops;
    118 	pgr->pgr_phg_orig.phg_nops = maxops;
    119 }
    120 
    121 void
    122 puffs_req_destroyget(struct puffs_getreq *pgr)
    123 {
    124 
    125 	assert(pgr->pgr_nppr == 0);
    126 
    127 	free(pgr->pgr_phg_orig.phg_buf);
    128 	free(pgr);
    129 }
    130 
    131 
    132 struct puffs_putreq *
    133 puffs_req_makeput(struct puffs_usermount *pu)
    134 {
    135 	struct puffs_putreq *ppr;
    136 
    137 	ppr = malloc(sizeof(struct puffs_putreq));
    138 	if (!ppr)
    139 		return NULL;
    140 
    141 	ppr->ppr_php.php_nops = 0;
    142 	TAILQ_INIT(&ppr->ppr_pccq);
    143 
    144 	ppr->ppr_pu = pu;
    145 	ppr->ppr_pgr = NULL;
    146 
    147 	puffs_req_resetput(ppr);
    148 
    149 	return ppr;
    150 }
    151 
    152 void
    153 puffs_req_put(struct puffs_putreq *ppr, struct puffs_req *preq)
    154 {
    155 
    156 	ppr->ppr_php.php_nops++;
    157 
    158 	/* store data */
    159 	*ppr->ppr_buf = preq;
    160 	*ppr->ppr_buflen = preq->preq_buflen;
    161 	*ppr->ppr_id = preq->preq_id;
    162 
    163 	/* and roll forward for next request */
    164 	ppr->ppr_buf = &preq->preq_nextbuf;
    165 	ppr->ppr_buflen = &preq->preq_buflen;
    166 	ppr->ppr_id = &preq->preq_id;
    167 }
    168 
    169 /*
    170  * instead of a direct preq, put a cc onto the push queue
    171  */
    172 void
    173 puffs_req_putcc(struct puffs_putreq *ppr, struct puffs_cc *pcc)
    174 {
    175 
    176 	TAILQ_INSERT_TAIL(&ppr->ppr_pccq, pcc, entries);
    177 	puffs_req_put(ppr, pcc->pcc_preq);
    178 }
    179 
    180 int
    181 puffs_req_putput(struct puffs_putreq *ppr)
    182 {
    183 
    184 	if (ppr->ppr_php.php_nops)
    185 		if (ioctl(ppr->ppr_pu->pu_fd, PUFFSPUTOP, &ppr->ppr_php) == -1)
    186 			return -1;
    187 
    188 	return 0;
    189 }
    190 
    191 void
    192 puffs_req_resetput(struct puffs_putreq *ppr)
    193 {
    194 	struct puffs_cc *pcc;
    195 
    196 	if (ppr->ppr_pgr != NULL) {
    197 		ppr->ppr_pgr->pgr_nppr--;
    198 		ppr->ppr_pgr = NULL;
    199 	}
    200 
    201 	ppr->ppr_buf = &ppr->ppr_php.php_buf;
    202 	ppr->ppr_buflen = &ppr->ppr_php.php_buflen;
    203 	ppr->ppr_id = &ppr->ppr_php.php_id;
    204 
    205 	while ((pcc = TAILQ_FIRST(&ppr->ppr_pccq)) != NULL) {
    206 		TAILQ_REMOVE(&ppr->ppr_pccq, pcc, entries);
    207 		puffs_cc_destroy(pcc);
    208 	}
    209 }
    210 
    211 void
    212 puffs_req_destroyput(struct puffs_putreq *ppr)
    213 {
    214 
    215 	puffs_req_resetput(ppr);
    216 	free(ppr);
    217 }
    218 
    219 int
    220 puffs_req_handle(struct puffs_getreq *pgr, struct puffs_putreq *ppr, int maxops)
    221 {
    222 	struct puffs_usermount *pu;
    223 	struct puffs_req *preq;
    224 	int pval;
    225 
    226 	assert(pgr->pgr_pu == ppr->ppr_pu);
    227 	pu = pgr->pgr_pu;
    228 
    229 	puffs_req_setmaxget(pgr, maxops);
    230 	if (puffs_req_loadget(pgr) == -1)
    231 		return -1;
    232 
    233 	/* interlink pgr and ppr for diagnostic asserts */
    234 	pgr->pgr_nppr++;
    235 	ppr->ppr_pgr = pgr;
    236 
    237 	pval = 0;
    238 	while ((preq = puffs_req_get(pgr)) != NULL
    239 	    && puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED)
    240 		pval = puffs_dopreq(pu, preq, ppr);
    241 
    242 	return pval;
    243 }
    244