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