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