Home | History | Annotate | Line # | Download | only in mount_9p
subr.c revision 1.2
      1 /*	$NetBSD: subr.c,v 1.2 2007/05/05 15:49:51 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007  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 #ifndef lint
     30 __RCSID("$NetBSD: subr.c,v 1.2 2007/05/05 15:49:51 pooka Exp $");
     31 #endif /* !lint */
     32 
     33 #include <sys/types.h>
     34 
     35 #include <errno.h>
     36 #include <puffs.h>
     37 #include <stdlib.h>
     38 #include <util.h>
     39 
     40 #include "ninepuffs.h"
     41 #include "nineproto.h"
     42 
     43 void
     44 qid2vattr(struct vattr *vap, const struct qid9p *qid)
     45 {
     46 
     47 	vap->va_fileid = qid->qidpath;
     48 	vap->va_gen = qid->qidvers;
     49 	if (qid->qidtype & P9PROTO_QID_TYPE_DIR)
     50 		vap->va_type = VDIR;
     51 	else
     52 		vap->va_type = VREG;
     53 }
     54 
     55 static struct puffs_node *
     56 makep9pnode(struct puffs_usermount *pu, p9pfid_t fid)
     57 {
     58 	struct p9pnode *p9n;
     59 	struct puffs_node *pn;
     60 
     61 	p9n = emalloc(sizeof(struct p9pnode));
     62 	memset(p9n, 0, sizeof(struct p9pnode));
     63 	p9n->fid_base = fid;
     64 	LIST_INIT(&p9n->dir_openlist);
     65 
     66 	pn = puffs_pn_new(pu, p9n);
     67 	if (pn == NULL)
     68 		abort();
     69 
     70 	return pn;
     71 }
     72 
     73 struct puffs_node *
     74 newp9pnode_va(struct puffs_usermount *pu, const struct vattr *va, p9pfid_t fid)
     75 {
     76 	struct puffs_node *pn;
     77 
     78 	pn = makep9pnode(pu, fid);
     79 	pn->pn_va = *va;
     80 
     81 	return pn;
     82 }
     83 
     84 struct puffs_node *
     85 newp9pnode_qid(struct puffs_usermount *pu, const struct qid9p *qid,
     86 	p9pfid_t fid)
     87 {
     88 	struct puffs_node *pn;
     89 
     90 	pn = makep9pnode(pu, fid);
     91 	puffs_vattr_null(&pn->pn_va);
     92 	qid2vattr(&pn->pn_va, qid);
     93 
     94 	return pn;
     95 }
     96 
     97 /*
     98  * search list of fids, or if none is found, walk a fid for a new one
     99  * and issue dummy readdirs until we get the result we want
    100  */
    101 int
    102 getdfwithoffset(struct puffs_cc *pcc, struct p9pnode *p9n, off_t wantoff,
    103 	struct dirfid **rfid)
    104 {
    105 	struct puffs9p *p9p = puffs_cc_getspecific(pcc);
    106 	struct puffs_framebuf *pb;
    107 	struct dirfid *dfp = NULL;
    108 	p9ptag_t tag = NEXTTAG(p9p);
    109 	off_t curoff, advance;
    110 	uint32_t count;
    111 	int error;
    112 
    113 	LIST_FOREACH(dfp, &p9n->dir_openlist, entries) {
    114 		if (dfp->seekoff == wantoff) {
    115 			LIST_REMOVE(dfp, entries);
    116 			*rfid = dfp;
    117 			return 0;
    118 		}
    119 	}
    120 
    121 	/* didn't get off easy?  damn, do manual labour */
    122 	pb = p9pbuf_makeout();
    123 	dfp = ecalloc(1, sizeof(struct dirfid));
    124 	dfp->fid = NEXTFID(p9p);
    125 	error = proto_cc_open(pcc, p9n->fid_base, dfp->fid, P9PROTO_OMODE_READ);
    126 	if (error)
    127 		goto errout;
    128 
    129 	for (curoff = 0;;) {
    130 		advance = wantoff - curoff;
    131 
    132 		tag = NEXTTAG(p9p);
    133 		p9pbuf_put_1(pb, P9PROTO_T_READ);
    134 		p9pbuf_put_2(pb, tag);
    135 		p9pbuf_put_4(pb, dfp->fid);
    136 		p9pbuf_put_8(pb, 0);
    137 		p9pbuf_put_4(pb, advance);
    138 		puffs_framebuf_enqueue_cc(pcc, pb);
    139 
    140 		if (p9pbuf_get_type(pb) != P9PROTO_R_READ) {
    141 			error = EPROTO;
    142 			goto errout;
    143 		}
    144 
    145 		/*
    146 		 * Check how many bytes we got.  If we got the amount we
    147 		 * wanted, we are at the correct position.  If we got
    148 		 * zero bytes, either the directory doesn't "support" the
    149 		 * seek offset we want (someone has probably inserted an
    150 		 * entry meantime) or we at the end of directory.  Either
    151 		 * way, let the upper layer deal with it.
    152 		 */
    153 		p9pbuf_get_4(pb, &count);
    154 		curoff += count;
    155 		if (count == advance || count == 0)
    156 			break;
    157 
    158 		p9pbuf_recycleout(pb);
    159 	}
    160 
    161 	dfp->seekoff = curoff;
    162 	*rfid = dfp;
    163 	return 0;
    164 
    165  errout:
    166 	puffs_framebuf_destroy(pb);
    167 	free(dfp);
    168 	return error;
    169 }
    170 
    171 void
    172 releasedf(struct puffs_cc *pcc, struct dirfid *dfp)
    173 {
    174 
    175 	proto_cc_clunkfid(pcc, dfp->fid, 0);
    176 	free(dfp);
    177 }
    178 
    179 void
    180 storedf(struct p9pnode *p9n, struct dirfid *dfp)
    181 {
    182 
    183 	LIST_INSERT_HEAD(&p9n->dir_openlist, dfp, entries);
    184 }
    185 
    186 void
    187 nukealldf(struct puffs_cc *pcc, struct p9pnode *p9n)
    188 {
    189 	struct dirfid *dfp, *dfp_next;
    190 
    191 	for (dfp = LIST_FIRST(&p9n->dir_openlist); dfp; dfp = dfp_next) {
    192 		dfp_next = LIST_NEXT(dfp, entries);
    193 		LIST_REMOVE(dfp, entries);
    194 		releasedf(pcc, dfp);
    195 	}
    196 }
    197