node.c revision 1.3 1 1.3 pooka /* $NetBSD: node.c,v 1.3 2007/05/04 18:17:34 pooka 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.3 pooka __RCSID("$NetBSD: node.c,v 1.3 2007/05/04 18:17:34 pooka Exp $");
31 1.1 pooka #endif /* !lint */
32 1.1 pooka
33 1.1 pooka #include <assert.h>
34 1.1 pooka #include <errno.h>
35 1.1 pooka #include <puffs.h>
36 1.1 pooka #include <stdio.h>
37 1.1 pooka
38 1.1 pooka #include "ninepuffs.h"
39 1.1 pooka #include "nineproto.h"
40 1.1 pooka
41 1.1 pooka static void *
42 1.1 pooka nodecmp(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
43 1.1 pooka {
44 1.1 pooka struct vattr *vap = &pn->pn_va;
45 1.1 pooka struct qid9p *qid = arg;
46 1.1 pooka
47 1.1 pooka if (vap->va_fileid == qid->qidpath)
48 1.1 pooka return pn;
49 1.1 pooka
50 1.1 pooka return NULL;
51 1.1 pooka }
52 1.1 pooka
53 1.1 pooka int
54 1.1 pooka puffs9p_node_lookup(struct puffs_cc *pcc, void *opc, void **newnode,
55 1.1 pooka enum vtype *newtype, voff_t *newsize, dev_t *newrdev,
56 1.1 pooka const struct puffs_cn *pcn)
57 1.1 pooka {
58 1.1 pooka AUTOVAR(pcc);
59 1.1 pooka struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
60 1.1 pooka struct puffs_node *pn, *pn_dir = opc;
61 1.1 pooka struct p9pnode *p9n_dir = pn_dir->pn_data;
62 1.1 pooka p9ptag_t tfid = NEXTFID(p9p);
63 1.1 pooka struct qid9p newqid;
64 1.1 pooka uint16_t nqid;
65 1.1 pooka
66 1.1 pooka p9pbuf_put_1(pb, P9PROTO_T_WALK);
67 1.1 pooka p9pbuf_put_2(pb, tag);
68 1.1 pooka p9pbuf_put_4(pb, p9n_dir->fid_base);
69 1.1 pooka p9pbuf_put_4(pb, tfid);
70 1.1 pooka p9pbuf_put_2(pb, 1);
71 1.1 pooka p9pbuf_put_str(pb, pcn->pcn_name);
72 1.1 pooka
73 1.1 pooka outbuf_enqueue(p9p, pb, pcc, tag);
74 1.1 pooka puffs_cc_yield(pcc);
75 1.1 pooka
76 1.1 pooka rv = proto_expect_walk_nqids(pb, &nqid);
77 1.1 pooka if (rv) {
78 1.1 pooka rv = ENOENT;
79 1.1 pooka goto out;
80 1.1 pooka }
81 1.1 pooka if (nqid != 1 || !proto_getqid(pb, &newqid)) {
82 1.1 pooka rv = EPROTO;
83 1.1 pooka goto out;
84 1.1 pooka }
85 1.1 pooka
86 1.1 pooka pn = puffs_pn_nodewalk(pu, nodecmp, &newqid);
87 1.1 pooka if (pn == NULL)
88 1.1 pooka pn = newp9pnode_qid(pu, &newqid, tfid);
89 1.1 pooka else
90 1.1 pooka proto_cc_clunkfid(pcc, tfid, 0);
91 1.1 pooka
92 1.1 pooka *newnode = pn;
93 1.1 pooka *newtype = pn->pn_va.va_type;
94 1.1 pooka *newsize = pn->pn_va.va_size;
95 1.1 pooka *newrdev = pn->pn_va.va_rdev;
96 1.1 pooka
97 1.1 pooka out:
98 1.1 pooka RETURN(rv);
99 1.1 pooka }
100 1.1 pooka
101 1.1 pooka /*
102 1.1 pooka * Problem is that 9P doesn't allow seeking into a directory. So we
103 1.1 pooka * maintain a list of active fids for any given directory. They
104 1.1 pooka * start living at the first read and exist either until the directory
105 1.1 pooka * is closed or until they reach the end.
106 1.1 pooka */
107 1.1 pooka int
108 1.1 pooka puffs9p_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
109 1.1 pooka off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
110 1.1 pooka int *eofflag, off_t *cookies, size_t *ncookies)
111 1.1 pooka {
112 1.1 pooka AUTOVAR(pcc);
113 1.1 pooka struct puffs_node *pn = opc;
114 1.1 pooka struct p9pnode *p9n = pn->pn_data;
115 1.1 pooka struct vattr va;
116 1.1 pooka struct dirfid *dfp;
117 1.1 pooka char *name;
118 1.1 pooka uint32_t count;
119 1.1 pooka uint16_t statsize;
120 1.1 pooka
121 1.1 pooka rv = getdfwithoffset(pcc, p9n, *readoff, &dfp);
122 1.1 pooka if (rv)
123 1.1 pooka goto out;
124 1.1 pooka
125 1.1 pooka tag = NEXTTAG(p9p);
126 1.1 pooka p9pbuf_put_1(pb, P9PROTO_T_READ);
127 1.1 pooka p9pbuf_put_2(pb, tag);
128 1.1 pooka p9pbuf_put_4(pb, dfp->fid);
129 1.1 pooka p9pbuf_put_8(pb, *readoff);
130 1.1 pooka p9pbuf_put_4(pb, *reslen); /* XXX */
131 1.1 pooka outbuf_enqueue(p9p, pb, pcc, tag);
132 1.1 pooka
133 1.1 pooka puffs_cc_yield(pcc);
134 1.1 pooka
135 1.1 pooka p9pbuf_get_4(pb, &count);
136 1.1 pooka
137 1.1 pooka /*
138 1.1 pooka * if count is 0, assume we at end-of-dir. dfp is no longer
139 1.1 pooka * useful, so nuke it
140 1.1 pooka */
141 1.1 pooka if (count == 0) {
142 1.1 pooka *eofflag = 1;
143 1.1 pooka releasedf(pcc, dfp);
144 1.1 pooka goto out;
145 1.1 pooka }
146 1.1 pooka
147 1.1 pooka while (count > 0) {
148 1.1 pooka if (!proto_getstat(pb, &va, &name, &statsize)) {
149 1.1 pooka rv = EINVAL;
150 1.1 pooka goto out;
151 1.1 pooka }
152 1.1 pooka
153 1.1 pooka puffs_nextdent(&dent, name, va.va_fileid,
154 1.1 pooka puffs_vtype2dt(va.va_type), reslen);
155 1.1 pooka
156 1.1 pooka count -= statsize;
157 1.1 pooka *readoff += statsize;
158 1.1 pooka dfp->seekoff += statsize;
159 1.1 pooka }
160 1.1 pooka
161 1.1 pooka storedf(p9n, dfp);
162 1.1 pooka
163 1.1 pooka out:
164 1.1 pooka RETURN(rv);
165 1.1 pooka }
166 1.1 pooka
167 1.1 pooka int
168 1.1 pooka puffs9p_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *vap,
169 1.1 pooka const struct puffs_cred *pcr, pid_t pid)
170 1.1 pooka {
171 1.1 pooka AUTOVAR(pcc);
172 1.1 pooka struct puffs_node *pn = opc;
173 1.1 pooka struct p9pnode *p9n = pn->pn_data;
174 1.1 pooka
175 1.1 pooka p9pbuf_put_1(pb, P9PROTO_T_STAT);
176 1.1 pooka p9pbuf_put_2(pb, tag);
177 1.1 pooka p9pbuf_put_4(pb, p9n->fid_base);
178 1.1 pooka outbuf_enqueue(p9p, pb, pcc, tag);
179 1.1 pooka puffs_cc_yield(pcc);
180 1.1 pooka
181 1.1 pooka rv = proto_expect_stat(pb, &pn->pn_va);
182 1.1 pooka if (rv)
183 1.1 pooka goto out;
184 1.1 pooka
185 1.1 pooka memcpy(vap, &pn->pn_va, sizeof(struct vattr));
186 1.1 pooka
187 1.1 pooka out:
188 1.1 pooka RETURN(rv);
189 1.1 pooka }
190 1.1 pooka
191 1.1 pooka int
192 1.1 pooka puffs9p_node_setattr(struct puffs_cc *pcc, void *opc,
193 1.1 pooka const struct vattr *va, const struct puffs_cred *pcr, pid_t pid)
194 1.1 pooka {
195 1.1 pooka AUTOVAR(pcc);
196 1.1 pooka struct puffs_node *pn = opc;
197 1.1 pooka struct p9pnode *p9n = pn->pn_data;
198 1.1 pooka
199 1.1 pooka p9pbuf_put_1(pb, P9PROTO_T_WSTAT);
200 1.1 pooka p9pbuf_put_2(pb, tag);
201 1.1 pooka p9pbuf_put_4(pb, p9n->fid_base);
202 1.1 pooka proto_make_stat(pb, va, NULL);
203 1.1 pooka outbuf_enqueue(p9p, pb, pcc, tag);
204 1.1 pooka puffs_cc_yield(pcc);
205 1.1 pooka
206 1.1 pooka if (pb->type != P9PROTO_R_WSTAT)
207 1.1 pooka rv = EPROTO;
208 1.1 pooka
209 1.1 pooka RETURN(rv);
210 1.1 pooka }
211 1.1 pooka
212 1.1 pooka /*
213 1.1 pooka * Ok, time to get clever. There are two possible cases: we are
214 1.1 pooka * opening a file or we are opening a directory.
215 1.1 pooka *
216 1.1 pooka * If it's a directory, don't bother opening it here, but rather
217 1.1 pooka * wait until readdir, since it's probable we need to be able to
218 1.1 pooka * open a directory there in any case.
219 1.1 pooka *
220 1.2 pooka * If it's a regular file, open it here with whatever credentials
221 1.2 pooka * we happen to have. Let the upper layers of the kernel worry
222 1.2 pooka * about permission control.
223 1.1 pooka *
224 1.2 pooka * XXX: this does not work fully for the mmap case
225 1.1 pooka */
226 1.1 pooka int
227 1.1 pooka puffs9p_node_open(struct puffs_cc *pcc, void *opc, int mode,
228 1.1 pooka const struct puffs_cred *pcr, pid_t pid)
229 1.1 pooka {
230 1.1 pooka struct puffs9p *p9p = puffs_cc_getspecific(pcc);
231 1.1 pooka struct puffs_node *pn = opc;
232 1.1 pooka struct p9pnode *p9n = pn->pn_data;
233 1.1 pooka p9pfid_t nfid;
234 1.1 pooka int error = 0;
235 1.1 pooka
236 1.1 pooka p9n->opencount++;
237 1.2 pooka if (pn->pn_va.va_type != VDIR) {
238 1.2 pooka if (mode & FREAD && p9n->fid_read == P9P_INVALFID) {
239 1.2 pooka nfid = NEXTFID(p9p);
240 1.2 pooka error = proto_cc_open(pcc, p9n->fid_base, nfid,
241 1.2 pooka P9PROTO_OMODE_READ);
242 1.2 pooka if (error)
243 1.2 pooka return error;
244 1.2 pooka p9n->fid_read = nfid;
245 1.2 pooka }
246 1.2 pooka if (mode & FWRITE && p9n->fid_write == P9P_INVALFID) {
247 1.2 pooka nfid = NEXTFID(p9p);
248 1.2 pooka error = proto_cc_open(pcc, p9n->fid_base, nfid,
249 1.2 pooka P9PROTO_OMODE_WRITE);
250 1.2 pooka if (error)
251 1.2 pooka return error;
252 1.2 pooka p9n->fid_write = nfid;
253 1.2 pooka }
254 1.1 pooka }
255 1.1 pooka
256 1.1 pooka return 0;
257 1.1 pooka }
258 1.1 pooka
259 1.1 pooka int
260 1.1 pooka puffs9p_node_close(struct puffs_cc *pcc, void *opc, int flags,
261 1.1 pooka const struct puffs_cred *pcr, pid_t pid)
262 1.1 pooka {
263 1.1 pooka struct puffs_node *pn = opc;
264 1.1 pooka struct p9pnode *p9n = pn->pn_data;
265 1.1 pooka
266 1.2 pooka if (--p9n->opencount == 0) {
267 1.2 pooka if (pn->pn_va.va_type == VDIR) {
268 1.1 pooka nukealldf(pcc, p9n);
269 1.2 pooka } else {
270 1.2 pooka if (p9n->fid_read != P9P_INVALFID) {
271 1.2 pooka proto_cc_clunkfid(pcc, p9n->fid_read, 0);
272 1.2 pooka p9n->fid_read = P9P_INVALFID;
273 1.2 pooka }
274 1.2 pooka if (p9n->fid_write != P9P_INVALFID) {
275 1.2 pooka proto_cc_clunkfid(pcc, p9n->fid_write, 0);
276 1.2 pooka p9n->fid_write = P9P_INVALFID;
277 1.2 pooka }
278 1.2 pooka }
279 1.2 pooka }
280 1.1 pooka
281 1.1 pooka return 0;
282 1.1 pooka }
283 1.1 pooka
284 1.1 pooka int
285 1.1 pooka puffs9p_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
286 1.1 pooka off_t offset, size_t *resid, const struct puffs_cred *pcr,
287 1.1 pooka int ioflag)
288 1.1 pooka {
289 1.1 pooka AUTOVAR(pcc);
290 1.1 pooka struct puffs_node *pn = opc;
291 1.1 pooka struct p9pnode *p9n = pn->pn_data;
292 1.1 pooka uint32_t count;
293 1.1 pooka size_t nread;
294 1.1 pooka
295 1.1 pooka nread = 0;
296 1.1 pooka while (*resid > 0) {
297 1.1 pooka p9pbuf_put_1(pb, P9PROTO_T_READ);
298 1.1 pooka p9pbuf_put_2(pb, tag);
299 1.2 pooka p9pbuf_put_4(pb, p9n->fid_read);
300 1.1 pooka p9pbuf_put_8(pb, offset+nread);
301 1.1 pooka p9pbuf_put_4(pb, MIN((uint32_t)*resid,p9p->maxreq-24));
302 1.1 pooka outbuf_enqueue(p9p, pb, pcc, tag);
303 1.1 pooka puffs_cc_yield(pcc);
304 1.1 pooka
305 1.1 pooka if (pb->type != P9PROTO_R_READ) {
306 1.1 pooka rv = EPROTO;
307 1.1 pooka break;
308 1.1 pooka }
309 1.1 pooka
310 1.1 pooka p9pbuf_get_4(pb, &count);
311 1.1 pooka if (!p9pbuf_read_data(pb, buf + nread, count)) {
312 1.1 pooka rv = EPROTO;
313 1.1 pooka break;
314 1.1 pooka }
315 1.1 pooka
316 1.1 pooka *resid -= count;
317 1.1 pooka nread += count;
318 1.1 pooka
319 1.1 pooka p9pbuf_recycle(pb, P9PB_OUT);
320 1.1 pooka }
321 1.1 pooka
322 1.1 pooka RETURN(rv);
323 1.1 pooka }
324 1.1 pooka
325 1.1 pooka int
326 1.1 pooka puffs9p_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
327 1.1 pooka off_t offset, size_t *resid, const struct puffs_cred *cred,
328 1.1 pooka int ioflag)
329 1.1 pooka {
330 1.1 pooka AUTOVAR(pcc);
331 1.1 pooka struct puffs_node *pn = opc;
332 1.1 pooka struct p9pnode *p9n = pn->pn_data;
333 1.1 pooka uint32_t chunk, count;
334 1.1 pooka size_t nwrite;
335 1.1 pooka
336 1.1 pooka if (ioflag & PUFFS_IO_APPEND)
337 1.1 pooka offset = pn->pn_va.va_size;
338 1.1 pooka
339 1.1 pooka nwrite = 0;
340 1.1 pooka while (*resid > 0) {
341 1.1 pooka chunk = MIN(*resid, p9p->maxreq-32);
342 1.1 pooka
343 1.1 pooka p9pbuf_put_1(pb, P9PROTO_T_WRITE);
344 1.1 pooka p9pbuf_put_2(pb, tag);
345 1.2 pooka p9pbuf_put_4(pb, p9n->fid_write);
346 1.1 pooka p9pbuf_put_8(pb, offset+nwrite);
347 1.1 pooka p9pbuf_put_4(pb, chunk);
348 1.1 pooka p9pbuf_write_data(pb, buf+nwrite, chunk);
349 1.1 pooka outbuf_enqueue(p9p, pb, pcc, tag);
350 1.1 pooka puffs_cc_yield(pcc);
351 1.1 pooka
352 1.1 pooka if (pb->type != P9PROTO_R_WRITE) {
353 1.1 pooka rv = EPROTO;
354 1.1 pooka break;
355 1.1 pooka }
356 1.1 pooka
357 1.1 pooka p9pbuf_get_4(pb, &count);
358 1.1 pooka *resid -= count;
359 1.1 pooka nwrite += count;
360 1.1 pooka
361 1.1 pooka if (count != chunk) {
362 1.1 pooka rv = EPROTO;
363 1.1 pooka break;
364 1.1 pooka }
365 1.1 pooka
366 1.1 pooka p9pbuf_recycle(pb, P9PB_OUT);
367 1.1 pooka }
368 1.1 pooka
369 1.1 pooka RETURN(rv);
370 1.1 pooka }
371 1.1 pooka
372 1.1 pooka static int
373 1.1 pooka nodecreate(struct puffs_cc *pcc, struct puffs_node *pn, void **newnode,
374 1.1 pooka const char *name, const struct vattr *vap, uint32_t dirbit)
375 1.1 pooka {
376 1.1 pooka AUTOVAR(pcc);
377 1.1 pooka struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
378 1.1 pooka struct puffs_node *pn_new;
379 1.1 pooka struct p9pnode *p9n = pn->pn_data;
380 1.1 pooka p9pfid_t nfid = NEXTFID(p9p);
381 1.1 pooka struct qid9p nqid;
382 1.1 pooka int tries = 0;
383 1.1 pooka
384 1.1 pooka again:
385 1.1 pooka if (++tries > 5) {
386 1.1 pooka rv = EPROTO;
387 1.1 pooka goto out;
388 1.1 pooka }
389 1.1 pooka
390 1.1 pooka rv = proto_cc_dupfid(pcc, p9n->fid_base, nfid);
391 1.1 pooka if (rv)
392 1.1 pooka goto out;
393 1.1 pooka
394 1.1 pooka p9pbuf_put_1(pb, P9PROTO_T_CREATE);
395 1.1 pooka p9pbuf_put_2(pb, tag);
396 1.1 pooka p9pbuf_put_4(pb, nfid);
397 1.1 pooka p9pbuf_put_str(pb, name);
398 1.1 pooka p9pbuf_put_4(pb, dirbit | (vap->va_mode & 0777));
399 1.1 pooka p9pbuf_put_1(pb, 0);
400 1.1 pooka outbuf_enqueue(p9p, pb, pcc, tag);
401 1.1 pooka puffs_cc_yield(pcc);
402 1.1 pooka
403 1.1 pooka rv = proto_expect_qid(pb, P9PROTO_R_CREATE, &nqid);
404 1.1 pooka if (rv)
405 1.1 pooka goto out;
406 1.1 pooka
407 1.1 pooka /*
408 1.1 pooka * Now, little problem here: create returns an *open* fid.
409 1.1 pooka * So, clunk it and walk the parent directory to get a fid
410 1.1 pooka * which is not open for I/O yet.
411 1.1 pooka */
412 1.1 pooka proto_cc_clunkfid(pcc, nfid, 0);
413 1.1 pooka nfid = NEXTFID(p9p);
414 1.1 pooka
415 1.1 pooka p9pbuf_recycle(pb, P9PB_OUT);
416 1.1 pooka p9pbuf_put_1(pb, P9PROTO_T_WALK);
417 1.1 pooka p9pbuf_put_2(pb, tag);
418 1.1 pooka p9pbuf_put_4(pb, p9n->fid_base);
419 1.1 pooka p9pbuf_put_4(pb, nfid);
420 1.1 pooka p9pbuf_put_2(pb, 1);
421 1.1 pooka p9pbuf_put_str(pb, name);
422 1.1 pooka
423 1.1 pooka outbuf_enqueue(p9p, pb, pcc, tag);
424 1.1 pooka puffs_cc_yield(pcc);
425 1.1 pooka
426 1.1 pooka /*
427 1.1 pooka * someone removed it already? try again
428 1.1 pooka * note: this is kind of lose/lose
429 1.1 pooka */
430 1.1 pooka if (pb->type != P9PROTO_R_WALK)
431 1.1 pooka goto again;
432 1.1 pooka
433 1.1 pooka pn_new = newp9pnode_va(pu, vap, nfid);
434 1.1 pooka qid2vattr(&pn_new->pn_va, &nqid);
435 1.1 pooka *newnode = pn_new;
436 1.1 pooka
437 1.1 pooka out:
438 1.1 pooka RETURN(rv);
439 1.1 pooka }
440 1.1 pooka
441 1.1 pooka int
442 1.1 pooka puffs9p_node_create(struct puffs_cc *pcc, void *opc, void **newnode,
443 1.1 pooka const struct puffs_cn *pcn, const struct vattr *va)
444 1.1 pooka {
445 1.1 pooka
446 1.1 pooka return nodecreate(pcc, opc, newnode, pcn->pcn_name, va, 0);
447 1.1 pooka }
448 1.1 pooka
449 1.1 pooka int
450 1.1 pooka puffs9p_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
451 1.1 pooka const struct puffs_cn *pcn, const struct vattr *va)
452 1.1 pooka {
453 1.1 pooka
454 1.1 pooka return nodecreate(pcc, opc, newnode, pcn->pcn_name,
455 1.1 pooka va, P9PROTO_CPERM_DIR);
456 1.1 pooka }
457 1.1 pooka
458 1.1 pooka /*
459 1.1 pooka * Need to be a bit clever again: the fid is clunked no matter if
460 1.1 pooka * the remove succeeds or not. Re-getting a fid would be way too
461 1.1 pooka * difficult in case the remove failed for a valid reason (directory
462 1.1 pooka * not empty etcetc.). So walk ourselves another fid to prod the
463 1.1 pooka * ice with.
464 1.1 pooka */
465 1.1 pooka static int
466 1.1 pooka noderemove(struct puffs_cc *pcc, struct p9pnode *p9n)
467 1.1 pooka {
468 1.1 pooka AUTOVAR(pcc);
469 1.1 pooka p9pfid_t testfid = NEXTFID(p9p);
470 1.1 pooka
471 1.1 pooka rv = proto_cc_dupfid(pcc, p9n->fid_base, testfid);
472 1.1 pooka
473 1.1 pooka p9pbuf_put_1(pb, P9PROTO_T_REMOVE);
474 1.1 pooka p9pbuf_put_2(pb, tag);
475 1.1 pooka p9pbuf_put_4(pb, testfid);
476 1.1 pooka outbuf_enqueue(p9p, pb, pcc, tag);
477 1.1 pooka puffs_cc_yield(pcc);
478 1.1 pooka
479 1.1 pooka if (pb->type != P9PROTO_R_REMOVE) {
480 1.1 pooka rv = EPROTO;
481 1.1 pooka } else {
482 1.1 pooka proto_cc_clunkfid(pcc, p9n->fid_base, 0);
483 1.1 pooka p9n->fid_base = P9P_INVALFID;
484 1.1 pooka }
485 1.1 pooka
486 1.1 pooka RETURN(rv);
487 1.1 pooka }
488 1.1 pooka
489 1.1 pooka int
490 1.1 pooka puffs9p_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
491 1.1 pooka const struct puffs_cn *pcn)
492 1.1 pooka {
493 1.1 pooka struct puffs_node *pn = targ;
494 1.1 pooka
495 1.1 pooka if (pn->pn_va.va_type == VDIR)
496 1.1 pooka return EISDIR;
497 1.1 pooka
498 1.1 pooka return noderemove(pcc, pn->pn_data);
499 1.1 pooka }
500 1.1 pooka
501 1.1 pooka int
502 1.1 pooka puffs9p_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
503 1.1 pooka const struct puffs_cn *pcn)
504 1.1 pooka {
505 1.1 pooka struct puffs_node *pn = targ;
506 1.1 pooka
507 1.1 pooka if (pn->pn_va.va_type != VDIR)
508 1.1 pooka return ENOTDIR;
509 1.1 pooka
510 1.1 pooka return noderemove(pcc, pn->pn_data);
511 1.1 pooka }
512 1.1 pooka
513 1.1 pooka /*
514 1.1 pooka * 9P supports renames only for regular files within a directory
515 1.1 pooka * from what I could tell. So just support in-directory renames
516 1.1 pooka * for now.
517 1.1 pooka */
518 1.1 pooka int
519 1.1 pooka puffs9p_node_rename(struct puffs_cc *pcc, void *opc, void *src,
520 1.1 pooka const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
521 1.1 pooka const struct puffs_cn *pcn_targ)
522 1.1 pooka {
523 1.1 pooka AUTOVAR(pcc);
524 1.1 pooka struct puffs_node *pn_src = src;
525 1.1 pooka struct p9pnode *p9n_src = pn_src->pn_data;
526 1.1 pooka
527 1.1 pooka if (opc != targ_dir) {
528 1.1 pooka rv = EOPNOTSUPP;
529 1.1 pooka goto out;
530 1.1 pooka }
531 1.1 pooka
532 1.1 pooka /* 9P doesn't allow to overwrite in rename */
533 1.1 pooka if (targ) {
534 1.1 pooka struct puffs_node *pn_targ = targ;
535 1.1 pooka
536 1.1 pooka rv = noderemove(pcc, pn_targ->pn_data);
537 1.1 pooka if (rv)
538 1.3 pooka goto out;
539 1.1 pooka }
540 1.1 pooka
541 1.1 pooka p9pbuf_put_1(pb, P9PROTO_T_WSTAT);
542 1.1 pooka p9pbuf_put_2(pb, tag);
543 1.1 pooka p9pbuf_put_4(pb, p9n_src->fid_base);
544 1.1 pooka proto_make_stat(pb, NULL, pcn_targ->pcn_name);
545 1.1 pooka outbuf_enqueue(p9p, pb, pcc, tag);
546 1.1 pooka puffs_cc_yield(pcc);
547 1.1 pooka
548 1.1 pooka if (pb->type != P9PROTO_R_WSTAT)
549 1.1 pooka rv = EPROTO;
550 1.1 pooka
551 1.1 pooka out:
552 1.1 pooka RETURN(rv);
553 1.1 pooka }
554 1.1 pooka
555 1.1 pooka /*
556 1.1 pooka * - "here's one"
557 1.1 pooka * - "9P"
558 1.1 pooka * ~ "i'm not dead"
559 1.1 pooka * - "you're not fooling anyone you know, you'll be stone dead in a minute
560 1.1 pooka * - "he says he's not quite dead"
561 1.1 pooka * - "isn't there anything you could do?"
562 1.1 pooka * - *clunk*!
563 1.1 pooka * - "thanks"
564 1.1 pooka */
565 1.1 pooka int
566 1.1 pooka puffs9p_node_reclaim(struct puffs_cc *pcc, void *opc, pid_t pid)
567 1.1 pooka {
568 1.1 pooka #if 0
569 1.1 pooka if (p9n->fid_open != P9P_INVALFID)
570 1.1 pooka proto_cc_clunkfid(pcc, p9n->fid_open, 0);
571 1.1 pooka #endif
572 1.1 pooka
573 1.1 pooka return 0;
574 1.1 pooka }
575