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