Home | History | Annotate | Line # | Download | only in mount_9p
      1  1.8    uwe /*	$NetBSD: subr.c,v 1.8 2020/05/26 19:38:14 uwe Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*
      4  1.1  pooka  * Copyright (c) 2007  Antti Kantee.  All Rights Reserved.
      5  1.1  pooka  *
      6  1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7  1.1  pooka  * modification, are permitted provided that the following conditions
      8  1.1  pooka  * are met:
      9  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14  1.1  pooka  *
     15  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  pooka  * SUCH DAMAGE.
     26  1.1  pooka  */
     27  1.1  pooka 
     28  1.1  pooka #include <sys/cdefs.h>
     29  1.1  pooka #ifndef lint
     30  1.8    uwe __RCSID("$NetBSD: subr.c,v 1.8 2020/05/26 19:38:14 uwe Exp $");
     31  1.1  pooka #endif /* !lint */
     32  1.1  pooka 
     33  1.1  pooka #include <sys/types.h>
     34  1.1  pooka 
     35  1.1  pooka #include <errno.h>
     36  1.1  pooka #include <puffs.h>
     37  1.1  pooka #include <stdlib.h>
     38  1.1  pooka #include <util.h>
     39  1.1  pooka 
     40  1.1  pooka #include "ninepuffs.h"
     41  1.1  pooka #include "nineproto.h"
     42  1.1  pooka 
     43  1.1  pooka void
     44  1.1  pooka qid2vattr(struct vattr *vap, const struct qid9p *qid)
     45  1.1  pooka {
     46  1.1  pooka 
     47  1.1  pooka 	vap->va_fileid = qid->qidpath;
     48  1.1  pooka 	vap->va_gen = qid->qidvers;
     49  1.1  pooka 	if (qid->qidtype & P9PROTO_QID_TYPE_DIR)
     50  1.1  pooka 		vap->va_type = VDIR;
     51  1.1  pooka 	else
     52  1.1  pooka 		vap->va_type = VREG;
     53  1.1  pooka }
     54  1.1  pooka 
     55  1.1  pooka static struct puffs_node *
     56  1.1  pooka makep9pnode(struct puffs_usermount *pu, p9pfid_t fid)
     57  1.1  pooka {
     58  1.1  pooka 	struct p9pnode *p9n;
     59  1.1  pooka 	struct puffs_node *pn;
     60  1.1  pooka 
     61  1.1  pooka 	p9n = emalloc(sizeof(struct p9pnode));
     62  1.1  pooka 	memset(p9n, 0, sizeof(struct p9pnode));
     63  1.1  pooka 	p9n->fid_base = fid;
     64  1.1  pooka 	LIST_INIT(&p9n->dir_openlist);
     65  1.1  pooka 
     66  1.1  pooka 	pn = puffs_pn_new(pu, p9n);
     67  1.1  pooka 	if (pn == NULL)
     68  1.1  pooka 		abort();
     69  1.1  pooka 
     70  1.1  pooka 	return pn;
     71  1.1  pooka }
     72  1.1  pooka 
     73  1.1  pooka struct puffs_node *
     74  1.1  pooka newp9pnode_va(struct puffs_usermount *pu, const struct vattr *va, p9pfid_t fid)
     75  1.1  pooka {
     76  1.1  pooka 	struct puffs_node *pn;
     77  1.1  pooka 
     78  1.1  pooka 	pn = makep9pnode(pu, fid);
     79  1.1  pooka 	pn->pn_va = *va;
     80  1.1  pooka 
     81  1.1  pooka 	return pn;
     82  1.1  pooka }
     83  1.1  pooka 
     84  1.1  pooka struct puffs_node *
     85  1.1  pooka newp9pnode_qid(struct puffs_usermount *pu, const struct qid9p *qid,
     86  1.1  pooka 	p9pfid_t fid)
     87  1.1  pooka {
     88  1.1  pooka 	struct puffs_node *pn;
     89  1.1  pooka 
     90  1.1  pooka 	pn = makep9pnode(pu, fid);
     91  1.1  pooka 	puffs_vattr_null(&pn->pn_va);
     92  1.1  pooka 	qid2vattr(&pn->pn_va, qid);
     93  1.1  pooka 
     94  1.1  pooka 	return pn;
     95  1.1  pooka }
     96  1.1  pooka 
     97  1.1  pooka /*
     98  1.1  pooka  * search list of fids, or if none is found, walk a fid for a new one
     99  1.1  pooka  * and issue dummy readdirs until we get the result we want
    100  1.1  pooka  */
    101  1.1  pooka int
    102  1.6  pooka getdfwithoffset(struct puffs_usermount *pu, struct p9pnode *p9n, off_t wantoff,
    103  1.1  pooka 	struct dirfid **rfid)
    104  1.1  pooka {
    105  1.6  pooka 	struct puffs_cc *pcc = puffs_cc_getcc(pu);
    106  1.6  pooka 	struct puffs9p *p9p = puffs_getspecific(pu);
    107  1.1  pooka 	struct dirfid *dfp = NULL;
    108  1.5  pooka 	int rv;
    109  1.1  pooka 
    110  1.1  pooka 	LIST_FOREACH(dfp, &p9n->dir_openlist, entries) {
    111  1.1  pooka 		if (dfp->seekoff == wantoff) {
    112  1.1  pooka 			LIST_REMOVE(dfp, entries);
    113  1.1  pooka 			*rfid = dfp;
    114  1.1  pooka 			return 0;
    115  1.1  pooka 		}
    116  1.1  pooka 	}
    117  1.1  pooka 
    118  1.1  pooka 	/* didn't get off easy?  damn, do manual labour */
    119  1.1  pooka 	dfp = ecalloc(1, sizeof(struct dirfid));
    120  1.1  pooka 	dfp->fid = NEXTFID(p9p);
    121  1.6  pooka 	rv = proto_cc_open(pu, p9n->fid_base, dfp->fid, P9PROTO_OMODE_READ);
    122  1.5  pooka 	if (rv)
    123  1.5  pooka 		goto out;
    124  1.1  pooka 
    125  1.8    uwe 	off_t curoff = 0;
    126  1.8    uwe 	if (wantoff != 0) {
    127  1.8    uwe 		struct puffs_framebuf *pb = p9pbuf_makeout();
    128  1.8    uwe 		for (;;) {
    129  1.8    uwe 			off_t advance = wantoff - curoff;
    130  1.8    uwe 
    131  1.8    uwe 			p9ptag_t tag = NEXTTAG(p9p);
    132  1.8    uwe 			p9pbuf_put_1(pb, P9PROTO_T_READ);
    133  1.8    uwe 			p9pbuf_put_2(pb, tag);
    134  1.8    uwe 			p9pbuf_put_4(pb, dfp->fid);
    135  1.8    uwe 			p9pbuf_put_8(pb, curoff);
    136  1.8    uwe 			p9pbuf_put_4(pb, advance);
    137  1.8    uwe 			GETRESPONSE(pb);
    138  1.8    uwe 
    139  1.8    uwe 			if (p9pbuf_get_type(pb) != P9PROTO_R_READ) {
    140  1.8    uwe 				rv = proto_handle_rerror(pu, pb);
    141  1.8    uwe 				puffs_framebuf_destroy(pb);
    142  1.8    uwe 				goto out;
    143  1.8    uwe 			}
    144  1.8    uwe 
    145  1.8    uwe 			/*
    146  1.8    uwe 			 * Check how many bytes we got.  If we got the
    147  1.8    uwe 			 * amount we wanted, we are at the correct position.
    148  1.8    uwe 			 * If we got zero bytes, either the directory
    149  1.8    uwe 			 * doesn't "support" the seek offset we want
    150  1.8    uwe 			 * (someone has probably inserted an entry
    151  1.8    uwe 			 * meantime) or we at the end of directory.
    152  1.8    uwe 			 * Either way, let the upper layer deal with it.
    153  1.8    uwe 			 */
    154  1.8    uwe 			uint32_t count;
    155  1.8    uwe 			p9pbuf_get_4(pb, &count);
    156  1.8    uwe 			curoff += count;
    157  1.8    uwe 			if (count == advance || count == 0)
    158  1.8    uwe 				break;
    159  1.1  pooka 
    160  1.8    uwe 			p9pbuf_recycleout(pb);
    161  1.1  pooka 		}
    162  1.8    uwe 		puffs_framebuf_destroy(pb);
    163  1.1  pooka 	}
    164  1.1  pooka 
    165  1.1  pooka 	dfp->seekoff = curoff;
    166  1.1  pooka 	*rfid = dfp;
    167  1.1  pooka 	return 0;
    168  1.1  pooka 
    169  1.5  pooka  out:
    170  1.1  pooka 	free(dfp);
    171  1.5  pooka 	return rv;
    172  1.1  pooka }
    173  1.1  pooka 
    174  1.1  pooka void
    175  1.6  pooka releasedf(struct puffs_usermount *pu, struct dirfid *dfp)
    176  1.1  pooka {
    177  1.1  pooka 
    178  1.6  pooka 	proto_cc_clunkfid(pu, dfp->fid, 0);
    179  1.1  pooka 	free(dfp);
    180  1.1  pooka }
    181  1.1  pooka 
    182  1.1  pooka void
    183  1.1  pooka storedf(struct p9pnode *p9n, struct dirfid *dfp)
    184  1.1  pooka {
    185  1.1  pooka 
    186  1.1  pooka 	LIST_INSERT_HEAD(&p9n->dir_openlist, dfp, entries);
    187  1.1  pooka }
    188  1.1  pooka 
    189  1.1  pooka void
    190  1.6  pooka nukealldf(struct puffs_usermount *pu, struct p9pnode *p9n)
    191  1.1  pooka {
    192  1.4  pooka 	struct dirfid *dfp;
    193  1.1  pooka 
    194  1.4  pooka 	while ((dfp = LIST_FIRST(&p9n->dir_openlist)) != NULL) {
    195  1.1  pooka 		LIST_REMOVE(dfp, entries);
    196  1.6  pooka 		releasedf(pu, dfp);
    197  1.1  pooka 	}
    198  1.1  pooka }
    199