node.c revision 1.5 1 /* $NetBSD: node.c,v 1.5 2007/05/06 21:58:24 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.5 2007/05/06 21:58:24 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 if (pn->pn_va.va_type != VDIR) {
231 if (mode & FREAD && p9n->fid_read == P9P_INVALFID) {
232 nfid = NEXTFID(p9p);
233 error = proto_cc_open(pcc, p9n->fid_base, nfid,
234 P9PROTO_OMODE_READ);
235 if (error)
236 return error;
237 p9n->fid_read = nfid;
238 }
239 if (mode & FWRITE && p9n->fid_write == P9P_INVALFID) {
240 nfid = NEXTFID(p9p);
241 error = proto_cc_open(pcc, p9n->fid_base, nfid,
242 P9PROTO_OMODE_WRITE);
243 if (error)
244 return error;
245 p9n->fid_write = nfid;
246 }
247 }
248
249 return 0;
250 }
251
252 int
253 puffs9p_node_inactive(struct puffs_cc *pcc, void *opc, pid_t pid,
254 int *refcount)
255 {
256 struct puffs_node *pn = opc;
257 struct p9pnode *p9n = pn->pn_data;
258
259 if (pn->pn_va.va_type == VDIR) {
260 nukealldf(pcc, p9n);
261 } else {
262 if (p9n->fid_read != P9P_INVALFID) {
263 proto_cc_clunkfid(pcc, p9n->fid_read, 0);
264 p9n->fid_read = P9P_INVALFID;
265 }
266 if (p9n->fid_write != P9P_INVALFID) {
267 proto_cc_clunkfid(pcc, p9n->fid_write, 0);
268 p9n->fid_write = P9P_INVALFID;
269 }
270 }
271
272 *refcount = 1;
273 return 0;
274 }
275
276 int
277 puffs9p_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
278 off_t offset, size_t *resid, const struct puffs_cred *pcr,
279 int ioflag)
280 {
281 AUTOVAR(pcc);
282 struct puffs_node *pn = opc;
283 struct p9pnode *p9n = pn->pn_data;
284 uint32_t count;
285 size_t nread;
286
287 nread = 0;
288 while (*resid > 0) {
289 p9pbuf_put_1(pb, P9PROTO_T_READ);
290 p9pbuf_put_2(pb, tag);
291 p9pbuf_put_4(pb, p9n->fid_read);
292 p9pbuf_put_8(pb, offset+nread);
293 p9pbuf_put_4(pb, MIN((uint32_t)*resid,p9p->maxreq-24));
294 puffs_framebuf_enqueue_cc(pcc, pb);
295
296 if (p9pbuf_get_type(pb) != P9PROTO_R_READ) {
297 rv = EPROTO;
298 break;
299 }
300
301 p9pbuf_get_4(pb, &count);
302 if ((rv = p9pbuf_read_data(pb, buf + nread, count)))
303 break;
304
305 *resid -= count;
306 nread += count;
307
308 p9pbuf_recycleout(pb);
309 }
310
311 RETURN(rv);
312 }
313
314 int
315 puffs9p_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
316 off_t offset, size_t *resid, const struct puffs_cred *cred,
317 int ioflag)
318 {
319 AUTOVAR(pcc);
320 struct puffs_node *pn = opc;
321 struct p9pnode *p9n = pn->pn_data;
322 uint32_t chunk, count;
323 size_t nwrite;
324
325 if (ioflag & PUFFS_IO_APPEND)
326 offset = pn->pn_va.va_size;
327
328 nwrite = 0;
329 while (*resid > 0) {
330 chunk = MIN(*resid, p9p->maxreq-32);
331
332 p9pbuf_put_1(pb, P9PROTO_T_WRITE);
333 p9pbuf_put_2(pb, tag);
334 p9pbuf_put_4(pb, p9n->fid_write);
335 p9pbuf_put_8(pb, offset+nwrite);
336 p9pbuf_put_4(pb, chunk);
337 p9pbuf_write_data(pb, buf+nwrite, chunk);
338 puffs_framebuf_enqueue_cc(pcc, pb);
339
340 if (p9pbuf_get_type(pb) != P9PROTO_R_WRITE) {
341 rv = EPROTO;
342 break;
343 }
344
345 p9pbuf_get_4(pb, &count);
346 *resid -= count;
347 nwrite += count;
348
349 if (count != chunk) {
350 rv = EPROTO;
351 break;
352 }
353
354 p9pbuf_recycleout(pb);
355 }
356
357 RETURN(rv);
358 }
359
360 static int
361 nodecreate(struct puffs_cc *pcc, struct puffs_node *pn, void **newnode,
362 const char *name, const struct vattr *vap, uint32_t dirbit)
363 {
364 AUTOVAR(pcc);
365 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
366 struct puffs_node *pn_new;
367 struct p9pnode *p9n = pn->pn_data;
368 p9pfid_t nfid = NEXTFID(p9p);
369 struct qid9p nqid;
370 int tries = 0;
371
372 again:
373 if (++tries > 5) {
374 rv = EPROTO;
375 goto out;
376 }
377
378 rv = proto_cc_dupfid(pcc, p9n->fid_base, nfid);
379 if (rv)
380 goto out;
381
382 p9pbuf_put_1(pb, P9PROTO_T_CREATE);
383 p9pbuf_put_2(pb, tag);
384 p9pbuf_put_4(pb, nfid);
385 p9pbuf_put_str(pb, name);
386 p9pbuf_put_4(pb, dirbit | (vap->va_mode & 0777));
387 p9pbuf_put_1(pb, 0);
388 puffs_framebuf_enqueue_cc(pcc, pb);
389
390 rv = proto_expect_qid(pb, P9PROTO_R_CREATE, &nqid);
391 if (rv)
392 goto out;
393
394 /*
395 * Now, little problem here: create returns an *open* fid.
396 * So, clunk it and walk the parent directory to get a fid
397 * which is not open for I/O yet.
398 */
399 proto_cc_clunkfid(pcc, nfid, 0);
400 nfid = NEXTFID(p9p);
401
402 p9pbuf_recycleout(pb);
403 p9pbuf_put_1(pb, P9PROTO_T_WALK);
404 p9pbuf_put_2(pb, tag);
405 p9pbuf_put_4(pb, p9n->fid_base);
406 p9pbuf_put_4(pb, nfid);
407 p9pbuf_put_2(pb, 1);
408 p9pbuf_put_str(pb, name);
409 puffs_framebuf_enqueue_cc(pcc, pb);
410
411 /*
412 * someone removed it already? try again
413 * note: this is kind of lose/lose
414 */
415 if (p9pbuf_get_type(pb) != P9PROTO_R_WALK)
416 goto again;
417
418 pn_new = newp9pnode_va(pu, vap, nfid);
419 qid2vattr(&pn_new->pn_va, &nqid);
420 *newnode = pn_new;
421
422 out:
423 RETURN(rv);
424 }
425
426 int
427 puffs9p_node_create(struct puffs_cc *pcc, void *opc, void **newnode,
428 const struct puffs_cn *pcn, const struct vattr *va)
429 {
430
431 return nodecreate(pcc, opc, newnode, pcn->pcn_name, va, 0);
432 }
433
434 int
435 puffs9p_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
436 const struct puffs_cn *pcn, const struct vattr *va)
437 {
438
439 return nodecreate(pcc, opc, newnode, pcn->pcn_name,
440 va, P9PROTO_CPERM_DIR);
441 }
442
443 /*
444 * Need to be a bit clever again: the fid is clunked no matter if
445 * the remove succeeds or not. Re-getting a fid would be way too
446 * difficult in case the remove failed for a valid reason (directory
447 * not empty etcetc.). So walk ourselves another fid to prod the
448 * ice with.
449 */
450 static int
451 noderemove(struct puffs_cc *pcc, struct p9pnode *p9n)
452 {
453 AUTOVAR(pcc);
454 p9pfid_t testfid = NEXTFID(p9p);
455
456 rv = proto_cc_dupfid(pcc, p9n->fid_base, testfid);
457
458 p9pbuf_put_1(pb, P9PROTO_T_REMOVE);
459 p9pbuf_put_2(pb, tag);
460 p9pbuf_put_4(pb, testfid);
461 puffs_framebuf_enqueue_cc(pcc, pb);
462
463 if (p9pbuf_get_type(pb) != P9PROTO_R_REMOVE) {
464 rv = EPROTO;
465 } else {
466 proto_cc_clunkfid(pcc, p9n->fid_base, 0);
467 p9n->fid_base = P9P_INVALFID;
468 }
469
470 RETURN(rv);
471 }
472
473 int
474 puffs9p_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
475 const struct puffs_cn *pcn)
476 {
477 struct puffs_node *pn = targ;
478
479 if (pn->pn_va.va_type == VDIR)
480 return EISDIR;
481
482 return noderemove(pcc, pn->pn_data);
483 }
484
485 int
486 puffs9p_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
487 const struct puffs_cn *pcn)
488 {
489 struct puffs_node *pn = targ;
490
491 if (pn->pn_va.va_type != VDIR)
492 return ENOTDIR;
493
494 return noderemove(pcc, pn->pn_data);
495 }
496
497 /*
498 * 9P supports renames only for regular files within a directory
499 * from what I could tell. So just support in-directory renames
500 * for now.
501 */
502 int
503 puffs9p_node_rename(struct puffs_cc *pcc, void *opc, void *src,
504 const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
505 const struct puffs_cn *pcn_targ)
506 {
507 AUTOVAR(pcc);
508 struct puffs_node *pn_src = src;
509 struct p9pnode *p9n_src = pn_src->pn_data;
510
511 if (opc != targ_dir) {
512 rv = EOPNOTSUPP;
513 goto out;
514 }
515
516 /* 9P doesn't allow to overwrite in rename */
517 if (targ) {
518 struct puffs_node *pn_targ = targ;
519
520 rv = noderemove(pcc, pn_targ->pn_data);
521 if (rv)
522 goto out;
523 }
524
525 p9pbuf_put_1(pb, P9PROTO_T_WSTAT);
526 p9pbuf_put_2(pb, tag);
527 p9pbuf_put_4(pb, p9n_src->fid_base);
528 proto_make_stat(pb, NULL, pcn_targ->pcn_name);
529 puffs_framebuf_enqueue_cc(pcc, pb);
530
531 if (p9pbuf_get_type(pb) != P9PROTO_R_WSTAT)
532 rv = EPROTO;
533
534 out:
535 RETURN(rv);
536 }
537
538 /*
539 * - "here's one"
540 * - "9P"
541 * ~ "i'm not dead"
542 * - "you're not fooling anyone you know, you'll be stone dead in a minute
543 * - "he says he's not quite dead"
544 * - "isn't there anything you could do?"
545 * - *clunk*!
546 * - "thanks"
547 */
548 int
549 puffs9p_node_reclaim(struct puffs_cc *pcc, void *opc, pid_t pid)
550 {
551 #if 0
552 if (p9n->fid_open != P9P_INVALFID)
553 proto_cc_clunkfid(pcc, p9n->fid_open, 0);
554 #endif
555
556 return 0;
557 }
558