1 /* $NetBSD: subr.c,v 1.8 2020/05/26 19:38:14 uwe 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.8 2020/05/26 19:38:14 uwe 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_usermount *pu, struct p9pnode *p9n, off_t wantoff, 103 struct dirfid **rfid) 104 { 105 struct puffs_cc *pcc = puffs_cc_getcc(pu); 106 struct puffs9p *p9p = puffs_getspecific(pu); 107 struct dirfid *dfp = NULL; 108 int rv; 109 110 LIST_FOREACH(dfp, &p9n->dir_openlist, entries) { 111 if (dfp->seekoff == wantoff) { 112 LIST_REMOVE(dfp, entries); 113 *rfid = dfp; 114 return 0; 115 } 116 } 117 118 /* didn't get off easy? damn, do manual labour */ 119 dfp = ecalloc(1, sizeof(struct dirfid)); 120 dfp->fid = NEXTFID(p9p); 121 rv = proto_cc_open(pu, p9n->fid_base, dfp->fid, P9PROTO_OMODE_READ); 122 if (rv) 123 goto out; 124 125 off_t curoff = 0; 126 if (wantoff != 0) { 127 struct puffs_framebuf *pb = p9pbuf_makeout(); 128 for (;;) { 129 off_t advance = wantoff - curoff; 130 131 p9ptag_t tag = NEXTTAG(p9p); 132 p9pbuf_put_1(pb, P9PROTO_T_READ); 133 p9pbuf_put_2(pb, tag); 134 p9pbuf_put_4(pb, dfp->fid); 135 p9pbuf_put_8(pb, curoff); 136 p9pbuf_put_4(pb, advance); 137 GETRESPONSE(pb); 138 139 if (p9pbuf_get_type(pb) != P9PROTO_R_READ) { 140 rv = proto_handle_rerror(pu, pb); 141 puffs_framebuf_destroy(pb); 142 goto out; 143 } 144 145 /* 146 * Check how many bytes we got. If we got the 147 * amount we wanted, we are at the correct position. 148 * If we got zero bytes, either the directory 149 * doesn't "support" the seek offset we want 150 * (someone has probably inserted an entry 151 * meantime) or we at the end of directory. 152 * Either way, let the upper layer deal with it. 153 */ 154 uint32_t count; 155 p9pbuf_get_4(pb, &count); 156 curoff += count; 157 if (count == advance || count == 0) 158 break; 159 160 p9pbuf_recycleout(pb); 161 } 162 puffs_framebuf_destroy(pb); 163 } 164 165 dfp->seekoff = curoff; 166 *rfid = dfp; 167 return 0; 168 169 out: 170 free(dfp); 171 return rv; 172 } 173 174 void 175 releasedf(struct puffs_usermount *pu, struct dirfid *dfp) 176 { 177 178 proto_cc_clunkfid(pu, dfp->fid, 0); 179 free(dfp); 180 } 181 182 void 183 storedf(struct p9pnode *p9n, struct dirfid *dfp) 184 { 185 186 LIST_INSERT_HEAD(&p9n->dir_openlist, dfp, entries); 187 } 188 189 void 190 nukealldf(struct puffs_usermount *pu, struct p9pnode *p9n) 191 { 192 struct dirfid *dfp; 193 194 while ((dfp = LIST_FIRST(&p9n->dir_openlist)) != NULL) { 195 LIST_REMOVE(dfp, entries); 196 releasedf(pu, dfp); 197 } 198 } 199