node.c revision 1.17 1 /* $NetBSD: node.c,v 1.17 2007/04/12 20:42:46 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2006 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 * 3. The name of the company nor the name of the author may be used to
15 * endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 #ifndef lint
33 __RCSID("$NetBSD: node.c,v 1.17 2007/04/12 20:42:46 pooka Exp $");
34 #endif /* !lint */
35
36 #include <assert.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40
41 #include "psshfs.h"
42 #include "sftp_proto.h"
43
44 int
45 psshfs_node_lookup(struct puffs_cc *pcc, void *opc, void **newnode,
46 enum vtype *newtype, voff_t *newsize, dev_t *newrdev,
47 const struct puffs_cn *pcn)
48 {
49 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
50 struct psshfs_ctx *pctx = puffs_getspecific(pu);
51 struct puffs_node *pn_dir = opc;
52 struct psshfs_node *psn, *psn_dir = pn_dir->pn_data;
53 struct puffs_node *pn;
54 struct psshfs_dir *pd;
55 struct vattr va;
56 int rv;
57
58 if (PCNISDOTDOT(pcn)) {
59 psn = psn_dir->parent->pn_data;
60 psn->reclaimed = 0;
61
62 *newnode = psn_dir->parent;
63 *newtype = VDIR;
64 return 0;
65 }
66
67 rv = sftp_readdir(pcc, pctx, pn_dir);
68 if (rv) {
69 printf("got error from readdir: %d\n", rv);
70 if (rv != EPERM)
71 return rv;
72
73 /*
74 * Can't read the directory. We still might be
75 * able to find the node with getattr in -r+x dirs
76 */
77 rv = getpathattr(pcc, PCNPATH(pcn), &va);
78 if (rv)
79 return rv;
80
81 /* guess */
82 if (va.va_type == VDIR)
83 va.va_nlink = 2;
84 else
85 va.va_nlink = 1;
86
87 pn = allocnode(pu, pn_dir, pcn->pcn_name, &va);
88 psn = pn->pn_data;
89 psn->attrread = time(NULL);
90 } else {
91 pd = lookup(psn_dir->dir, psn_dir->dentnext, pcn->pcn_name);
92 if (!pd) {
93 return ENOENT;
94 }
95
96 if (pd->entry)
97 pn = pd->entry;
98 else
99 pn = makenode(pu, pn_dir, pd, &pd->va);
100 psn = pn->pn_data;
101 }
102
103 psn->reclaimed = 0;
104
105 *newnode = pn;
106 *newsize = pn->pn_va.va_size;
107 *newtype = pn->pn_va.va_type;
108
109 return 0;
110 }
111
112 int
113 psshfs_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *vap,
114 const struct puffs_cred *pcr, pid_t pid)
115 {
116 struct puffs_node *pn = opc;
117 int rv;
118
119 rv = getnodeattr(pcc, pn);
120 if (rv)
121 return rv;
122
123 memcpy(vap, &pn->pn_va, sizeof(struct vattr));
124
125 return 0;
126 }
127
128 int
129 psshfs_node_setattr(struct puffs_cc *pcc, void *opc,
130 const struct vattr *va, const struct puffs_cred *pcr, pid_t pid)
131 {
132 PSSHFSAUTOVAR(pcc);
133 struct vattr kludgeva;
134 struct puffs_node *pn = opc;
135
136 psbuf_req_str(pb, SSH_FXP_SETSTAT, reqid, PNPATH(pn));
137
138 memcpy(&kludgeva, va, sizeof(struct vattr));
139
140 /* XXX: kludge due to openssh server implementation */
141 if (va->va_atime.tv_sec != PUFFS_VNOVAL
142 && va->va_mtime.tv_sec == PUFFS_VNOVAL) {
143 if (pn->pn_va.va_mtime.tv_sec != PUFFS_VNOVAL)
144 kludgeva.va_mtime.tv_sec = pn->pn_va.va_mtime.tv_sec;
145 else
146 kludgeva.va_mtime.tv_sec = va->va_atime.tv_sec;
147 }
148 if (va->va_mtime.tv_sec != PUFFS_VNOVAL
149 && va->va_atime.tv_sec == PUFFS_VNOVAL) {
150 if (pn->pn_va.va_atime.tv_sec != PUFFS_VNOVAL)
151 kludgeva.va_atime.tv_sec = pn->pn_va.va_atime.tv_sec;
152 else
153 kludgeva.va_atime.tv_sec = va->va_mtime.tv_sec;
154 }
155
156 psbuf_put_vattr(pb, &kludgeva);
157 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
158
159 puffs_cc_yield(pcc);
160
161 rv = psbuf_expect_status(pb);
162 if (rv == 0)
163 puffs_setvattr(&pn->pn_va, &kludgeva);
164
165 PSSHFSRETURN(rv);
166 }
167
168 int
169 psshfs_node_create(struct puffs_cc *pcc, void *opc, void **newnode,
170 const struct puffs_cn *pcn, const struct vattr *va)
171 {
172 PSSHFSAUTOVAR(pcc);
173 struct puffs_node *pn = opc;
174 struct puffs_node *pn_new;
175 char *fhand = NULL;
176 uint32_t fhandlen;
177
178 pn_new = allocnode(pu, pn, pcn->pcn_name, va);
179 if (!pn_new) {
180 rv = ENOMEM;
181 goto out;
182 }
183
184 psbuf_req_str(pb, SSH_FXP_OPEN, reqid, PCNPATH(pcn));
185 psbuf_put_4(pb, SSH_FXF_WRITE | SSH_FXF_CREAT | SSH_FXF_TRUNC);
186 psbuf_put_vattr(pb, va);
187 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
188
189 puffs_cc_yield(pcc);
190
191 rv = psbuf_expect_handle(pb, &fhand, &fhandlen);
192 if (rv)
193 goto out;
194
195 reqid = NEXTREQ(pctx);
196 psbuf_recycle(pb, PSB_OUT);
197 psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, fhand, fhandlen);
198 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
199
200 puffs_cc_yield(pcc);
201 rv = psbuf_expect_status(pb);
202
203 if (rv == 0)
204 *newnode = pn_new;
205 else
206 nukenode(pn_new, pcn->pcn_name, 1);
207
208 out:
209 free(fhand);
210 PSSHFSRETURN(rv);
211 }
212
213 int
214 psshfs_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
215 off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
216 int *eofflag, off_t *cookies, size_t *ncookies)
217 {
218 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
219 struct psshfs_ctx *pctx = puffs_getspecific(pu);
220 struct puffs_node *pn = opc;
221 struct psshfs_node *psn = pn->pn_data;
222 struct psshfs_dir *pd;
223 int i, rv;
224
225 *ncookies = 0;
226 rv = sftp_readdir(pcc, pctx, pn);
227 if (rv)
228 return rv;
229
230 for (i = *readoff; i < psn->dentnext; i++) {
231 pd = &psn->dir[i];
232 if (pd->valid == 0)
233 continue;
234 if (!puffs_nextdent(&dent, pd->entryname,
235 pd->va.va_fileid, puffs_vtype2dt(pd->va.va_type), reslen))
236 break;
237 if (cookies) {
238 *(cookies++) = (off_t)i;
239 (*ncookies)++;
240 }
241 }
242 if (i == psn->dentnext)
243 *eofflag = 1;
244
245 *readoff = i;
246 return 0;
247 }
248
249 int
250 psshfs_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
251 off_t offset, size_t *resid, const struct puffs_cred *pcr,
252 int ioflag)
253 {
254 PSSHFSAUTOVAR(pcc);
255 struct puffs_node *pn = opc;
256 char *fhand = NULL;
257 struct vattr va;
258 uint32_t readlen, fhandlen;
259
260 if (pn->pn_va.va_type == VDIR) {
261 rv = EISDIR;
262 goto err;
263 }
264
265 puffs_vattr_null(&va);
266 psbuf_req_str(pb, SSH_FXP_OPEN, reqid, PNPATH(pn));
267 psbuf_put_4(pb, SSH_FXF_READ);
268 psbuf_put_vattr(pb, &va);
269 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
270
271 puffs_cc_yield(pcc);
272
273 rv = psbuf_expect_handle(pb, &fhand, &fhandlen);
274 if (rv)
275 goto err;
276
277 readlen = *resid;
278 reqid = NEXTREQ(pctx);
279 psbuf_recycle(pb, PSB_OUT);
280 psbuf_req_data(pb, SSH_FXP_READ, reqid, fhand, fhandlen);
281 psbuf_put_8(pb, offset);
282 psbuf_put_4(pb, readlen);
283 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
284
285 puffs_cc_yield(pcc);
286
287 rv = psbuf_do_data(pb, buf, &readlen);
288 if (rv == 0)
289 *resid -= readlen;
290
291 reqid = NEXTREQ(pctx);
292 psbuf_recycle(pb, PSB_OUT);
293 psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, fhand, fhandlen);
294
295 pssh_outbuf_enqueue_nocc(pctx, pb, NULL, NULL, reqid);
296 free(fhand);
297 return 0;
298
299 err:
300 free(fhand);
301 PSSHFSRETURN(rv);
302 }
303
304 /* XXX: we should getattr for size */
305 int
306 psshfs_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
307 off_t offset, size_t *resid, const struct puffs_cred *cred,
308 int ioflag)
309 {
310 PSSHFSAUTOVAR(pcc);
311 struct puffs_node *pn = opc;
312 char *fhand = NULL;
313 struct vattr va, kludgeva1, kludgeva2;
314 uint32_t writelen, oflags, fhandlen;
315
316 if (pn->pn_va.va_type == VDIR) {
317 rv = EISDIR;
318 goto err;
319 }
320
321 /*
322 * XXXX: this is wrong - we shouldn't muck the file permissions
323 * at this stage any more. However, we need this, since currently
324 * we can't tell the sftp server "hey, this data was already
325 * authenticated to UBC, it's ok to let us write this". Yes, it
326 * will fail e.g. if we don't own the file. Tough love.
327 *
328 * TODO-point: Investigate solving this with open filehandles
329 * or something like that.
330 */
331 kludgeva1 = pn->pn_va;
332
333 puffs_vattr_null(&kludgeva2);
334 kludgeva2.va_mode = 0700;
335 rv = psshfs_node_setattr(pcc, opc, &kludgeva2, cred, 0);
336 if (rv)
337 goto err;
338
339 /* XXXcontinuation: ok, file is mode 700 now, we can open it rw */
340
341 oflags = SSH_FXF_WRITE;
342 #if 0
343 /*
344 * At least OpenSSH doesn't appear to support this, so can't
345 * do it the right way.
346 */
347 if (ioflag & PUFFS_IO_APPEND)
348 oflags |= SSH_FXF_APPEND;
349 #endif
350 if (ioflag & PUFFS_IO_APPEND)
351 offset = pn->pn_va.va_size;
352
353 puffs_vattr_null(&va);
354 psbuf_req_str(pb, SSH_FXP_OPEN, reqid, PNPATH(pn));
355 psbuf_put_4(pb, oflags);
356 psbuf_put_vattr(pb, &va);
357 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
358
359 puffs_cc_yield(pcc);
360
361 rv = psbuf_expect_handle(pb, &fhand, &fhandlen);
362 if (rv)
363 goto err;
364
365 /* moreXXX: file is open, revert old creds for crying out loud! */
366 rv = psshfs_node_setattr(pcc, opc, &kludgeva1, cred, 0);
367
368 /* are we screwed a la royal jelly? */
369 if (rv)
370 goto closefile;
371
372 writelen = *resid;
373 reqid = NEXTREQ(pctx);
374 psbuf_recycle(pb, PSB_OUT);
375 psbuf_req_data(pb, SSH_FXP_WRITE, reqid, fhand, fhandlen);
376 psbuf_put_8(pb, offset);
377 psbuf_put_data(pb, buf, writelen);
378 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
379
380 puffs_cc_yield(pcc);
381
382 rv = psbuf_expect_status(pb);
383 if (rv == 0)
384 *resid = 0;
385
386 if (pn->pn_va.va_size < offset + writelen)
387 pn->pn_va.va_size = offset + writelen;
388
389 closefile:
390 reqid = NEXTREQ(pctx);
391 psbuf_recycle(pb, PSB_OUT);
392 psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, fhand, fhandlen);
393
394 pssh_outbuf_enqueue_nocc(pctx, pb, NULL, NULL, reqid);
395 free(fhand);
396 return 0;
397
398 err:
399 free(fhand);
400 PSSHFSRETURN(rv);
401 }
402
403 int
404 psshfs_node_readlink(struct puffs_cc *pcc, void *opc,
405 const struct puffs_cred *cred, char *linkvalue, size_t *linklen)
406 {
407 PSSHFSAUTOVAR(pcc);
408 struct puffs_node *pn = opc;
409 char *linktmp = NULL;
410 uint32_t count;
411
412 if (pctx->protover < 3) {
413 rv = EOPNOTSUPP;
414 goto out;
415 }
416
417 psbuf_req_str(pb, SSH_FXP_READLINK, reqid, PNPATH(pn));
418 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
419
420 puffs_cc_yield(pcc);
421
422 rv = psbuf_expect_name(pb, &count);
423 if (count != 1) {
424 rv = EPROTO;
425 goto out;
426 }
427 rv = psbuf_get_str(pb, &linktmp, (uint32_t *)linklen);
428 if (rv)
429 rv = 0;
430 else {
431 rv = EPROTO;
432 goto out;
433 }
434 (void) memcpy(linkvalue, linktmp, *linklen);
435
436 out:
437 free(linktmp);
438 PSSHFSRETURN(rv);
439 }
440
441 int
442 psshfs_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
443 const struct puffs_cn *pcn)
444 {
445 PSSHFSAUTOVAR(pcc);
446 struct puffs_node *pn_targ = targ;
447
448 if (pn_targ->pn_va.va_type == VDIR) {
449 rv = EPERM;
450 goto out;
451 }
452
453 psbuf_req_str(pb, SSH_FXP_REMOVE, reqid, PNPATH(pn_targ));
454 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
455
456 puffs_cc_yield(pcc);
457
458 rv = psbuf_expect_status(pb);
459
460 if (rv == 0)
461 nukenode(pn_targ, pcn->pcn_name, 0);
462
463 out:
464 PSSHFSRETURN(rv);
465 }
466
467 int
468 psshfs_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
469 const struct puffs_cn *pcn, const struct vattr *va)
470 {
471 PSSHFSAUTOVAR(pcc);
472 struct puffs_node *pn = opc;
473 struct puffs_node *pn_new;
474
475 pn_new = allocnode(pu, pn, pcn->pcn_name, va);
476 if (!pn_new) {
477 rv = ENOMEM;
478 goto out;
479 }
480
481 psbuf_req_str(pb, SSH_FXP_MKDIR, reqid, PCNPATH(pcn));
482 psbuf_put_vattr(pb, va);
483 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
484
485 puffs_cc_yield(pcc);
486
487 rv = psbuf_expect_status(pb);
488
489 if (rv == 0)
490 *newnode = pn_new;
491 else
492 nukenode(pn_new, pcn->pcn_name, 1);
493
494 out:
495 PSSHFSRETURN(rv);
496 }
497
498 int
499 psshfs_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
500 const struct puffs_cn *pcn)
501 {
502 PSSHFSAUTOVAR(pcc);
503 struct puffs_node *pn_targ = targ;
504
505 psbuf_req_str(pb, SSH_FXP_RMDIR, reqid, PNPATH(pn_targ));
506 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
507
508 puffs_cc_yield(pcc);
509
510 rv = psbuf_expect_status(pb);
511 if (rv == 0)
512 nukenode(pn_targ, pcn->pcn_name, 0);
513
514 PSSHFSRETURN(rv);
515 }
516
517 int
518 psshfs_node_symlink(struct puffs_cc *pcc, void *opc, void **newnode,
519 const struct puffs_cn *pcn, const struct vattr *va,
520 const char *link_target)
521 {
522 PSSHFSAUTOVAR(pcc);
523 struct puffs_node *pn = opc;
524 struct puffs_node *pn_new;
525
526 if (pctx->protover < 3) {
527 rv = EOPNOTSUPP;
528 goto out;
529 }
530
531 pn_new = allocnode(pu, pn, pcn->pcn_name, va);
532 if (!pn_new) {
533 rv = ENOMEM;
534 goto out;
535 }
536
537 /*
538 * XXX: ietf says: source, target. openssh says: ietf who?
539 * Let's go with openssh and build quirk tables later if we care
540 */
541 psbuf_req_str(pb, SSH_FXP_SYMLINK, reqid, link_target);
542 psbuf_put_str(pb, PCNPATH(pcn));
543 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
544
545 puffs_cc_yield(pcc);
546
547 rv = psbuf_expect_status(pb);
548 if (rv == 0)
549 *newnode = pn_new;
550 else
551 nukenode(pn_new, pcn->pcn_name, 1);
552
553 out:
554 PSSHFSRETURN(rv);
555 }
556
557 int
558 psshfs_node_rename(struct puffs_cc *pcc, void *opc, void *src,
559 const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
560 const struct puffs_cn *pcn_targ)
561 {
562 PSSHFSAUTOVAR(pcc);
563 struct puffs_node *pn_sf = src;
564 struct puffs_node *pn_td = targ_dir, *pn_tf = targ;
565 struct psshfs_node *psn_targdir = pn_td->pn_data;
566
567 if (pctx->protover < 2) {
568 rv = EOPNOTSUPP;
569 goto out;
570 }
571
572 if (pn_tf) {
573 /* XXX: no backend implementation for now, so call directly */
574 rv = psshfs_node_remove(pcc, targ_dir, pn_tf, pcn_targ);
575 if (rv)
576 goto out;
577 }
578
579 psbuf_req_str(pb, SSH_FXP_RENAME, reqid, PCNPATH(pcn_src));
580 psbuf_put_str(pb, PCNPATH(pcn_targ));
581 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
582
583 puffs_cc_yield(pcc);
584
585 rv = psbuf_expect_status(pb);
586 if (rv == 0) {
587 struct psshfs_dir *pd;
588
589 /*
590 * XXX: interfaces didn't quite work with rename..
591 * the song remains the same. go figure .. ;)
592 */
593 nukenode(pn_sf, pcn_src->pcn_name, 0);
594 pd = direnter(pn_td, pcn_targ->pcn_name);
595 pd->entry = pn_sf;
596 puffs_setvattr(&pd->va, &pn_sf->pn_va);
597
598 if (opc != targ_dir) {
599 psn_targdir->childcount++;
600 if (pn_sf->pn_va.va_type == VDIR)
601 pn_td->pn_va.va_nlink++;
602 }
603 }
604
605 out:
606 PSSHFSRETURN(rv);
607 }
608
609 /*
610 * So this file system happened to be written in such a way that
611 * lookup for ".." is hard if we lose the in-memory node. We'd
612 * need to recreate the entire directory structure from the root
613 * node up to the ".." node we're looking up.
614 *
615 * And since our entire fs structure is purely fictional (i.e. it's
616 * only in-memory, not fetchable from the server), the easiest way
617 * to deal with it is to not allow nodes with children to be
618 * reclaimed.
619 *
620 * If a node with children is being attempted to be reclaimed, we
621 * just mark it "reclaimed" but leave it as is until all its children
622 * have been reclaimed. If a lookup for that node is done meanwhile,
623 * it will be found by lookup() and we just remove the "reclaimed"
624 * bit.
625 */
626 int
627 psshfs_node_reclaim(struct puffs_cc *pcc, void *opc, pid_t pid)
628 {
629 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
630 struct puffs_node *pn = opc, *pn_next, *pn_root;
631 struct psshfs_node *psn = pn->pn_data;
632
633 /*
634 * don't reclaim if we have file handle issued, otherwise
635 * we can't do fhtonode
636 */
637 if (psn->hasfh) {
638 psn->reclaimed = 2;
639 return 0;
640 }
641
642 psn->reclaimed = 1;
643 pn_root = puffs_getroot(pu);
644 for (; pn != pn_root; pn = pn_next) {
645 psn = pn->pn_data;
646 if (psn->reclaimed == 0 || psn->childcount != 0)
647 break;
648
649 pn_next = psn->parent;
650 doreclaim(pn);
651 }
652
653 return 0;
654 }
655