1 /* $NetBSD: subr.c,v 1.5 2007/05/16 09:57:21 pooka Exp $ */
4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: subr.c,v 1.5 2007/05/16 09:57:21 pooka Exp $");
33 #include <sys/types.h>
40 #include "ninepuffs.h"
41 #include "nineproto.h"
44 qid2vattr(struct vattr
*vap
, const struct qid9p
*qid
)
47 vap
->va_fileid
= qid
->qidpath
;
48 vap
->va_gen
= qid
->qidvers
;
49 if (qid
->qidtype
& P9PROTO_QID_TYPE_DIR
)
55 static struct puffs_node
*
56 makep9pnode(struct puffs_usermount
*pu
, p9pfid_t fid
)
59 struct puffs_node
*pn
;
61 p9n
= emalloc(sizeof(struct p9pnode
));
62 memset(p9n
, 0, sizeof(struct p9pnode
));
64 LIST_INIT(&p9n
->dir_openlist
);
66 pn
= puffs_pn_new(pu
, p9n
);
74 newp9pnode_va(struct puffs_usermount
*pu
, const struct vattr
*va
, p9pfid_t fid
)
76 struct puffs_node
*pn
;
78 pn
= makep9pnode(pu
, fid
);
85 newp9pnode_qid(struct puffs_usermount
*pu
, const struct qid9p
*qid
,
88 struct puffs_node
*pn
;
90 pn
= makep9pnode(pu
, fid
);
91 puffs_vattr_null(&pn
->pn_va
);
92 qid2vattr(&pn
->pn_va
, qid
);
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
102 getdfwithoffset(struct puffs_usermount
*pu
, struct p9pnode
*p9n
, off_t wantoff
,
103 struct dirfid
**rfid
)
105 struct puffs_cc
*pcc
= puffs_cc_getcc(pu
);
106 struct puffs9p
*p9p
= puffs_getspecific(pu
);
107 struct puffs_framebuf
*pb
;
108 struct dirfid
*dfp
= NULL
;
109 p9ptag_t tag
= NEXTTAG(p9p
);
110 off_t curoff
, advance
;
114 LIST_FOREACH(dfp
, &p9n
->dir_openlist
, entries
) {
115 if (dfp
->seekoff
== wantoff
) {
116 LIST_REMOVE(dfp
, entries
);
122 /* didn't get off easy? damn, do manual labour */
123 pb
= p9pbuf_makeout();
124 dfp
= ecalloc(1, sizeof(struct dirfid
));
125 dfp
->fid
= NEXTFID(p9p
);
126 rv
= proto_cc_open(pu
, p9n
->fid_base
, dfp
->fid
, P9PROTO_OMODE_READ
);
131 advance
= wantoff
- curoff
;
134 p9pbuf_put_1(pb
, P9PROTO_T_READ
);
135 p9pbuf_put_2(pb
, tag
);
136 p9pbuf_put_4(pb
, dfp
->fid
);
138 p9pbuf_put_4(pb
, advance
);
141 if (p9pbuf_get_type(pb
) != P9PROTO_R_READ
) {
147 * Check how many bytes we got. If we got the amount we
148 * wanted, we are at the correct position. If we got
149 * zero bytes, either the directory doesn't "support" the
150 * seek offset we want (someone has probably inserted an
151 * entry meantime) or we at the end of directory. Either
152 * way, let the upper layer deal with it.
154 p9pbuf_get_4(pb
, &count
);
156 if (count
== advance
|| count
== 0)
159 p9pbuf_recycleout(pb
);
161 puffs_framebuf_destroy(pb
);
163 dfp
->seekoff
= curoff
;
168 puffs_framebuf_destroy(pb
);
174 releasedf(struct puffs_usermount
*pu
, struct dirfid
*dfp
)
177 proto_cc_clunkfid(pu
, dfp
->fid
, 0);
182 storedf(struct p9pnode
*p9n
, struct dirfid
*dfp
)
185 LIST_INSERT_HEAD(&p9n
->dir_openlist
, dfp
, entries
);
189 nukealldf(struct puffs_usermount
*pu
, struct p9pnode
*p9n
)
193 while ((dfp
= LIST_FIRST(&p9n
->dir_openlist
)) != NULL
) {
194 LIST_REMOVE(dfp
, entries
);