node.c revision 1.35 1 /* $NetBSD: node.c,v 1.35 2007/07/19 10:14:53 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 *
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.35 2007/07/19 10:14:53 pooka Exp $");
31 #endif /* !lint */
32
33 #include <assert.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 #include "psshfs.h"
39 #include "sftp_proto.h"
40
41 int
42 psshfs_node_lookup(struct puffs_cc *pcc, void *opc, struct puffs_newinfo *pni,
43 const struct puffs_cn *pcn)
44 {
45 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
46 struct psshfs_ctx *pctx = puffs_getspecific(pu);
47 struct puffs_node *pn_dir = opc;
48 struct psshfs_node *psn, *psn_dir = pn_dir->pn_data;
49 struct puffs_node *pn;
50 struct psshfs_dir *pd;
51 struct vattr va;
52 int rv;
53
54 if (PCNISDOTDOT(pcn)) {
55 psn = psn_dir->parent->pn_data;
56 psn->stat &= ~PSN_RECLAIMED;
57
58 puffs_newinfo_setcookie(pni, psn_dir->parent);
59 puffs_newinfo_setvtype(pni, VDIR);
60 return 0;
61 }
62
63 rv = sftp_readdir(pcc, pctx, pn_dir);
64 if (rv) {
65 if (rv != EPERM)
66 return rv;
67
68 /*
69 * Can't read the directory. We still might be
70 * able to find the node with getattr in -r+x dirs
71 */
72 rv = getpathattr(pcc, PCNPATH(pcn), &va);
73 if (rv)
74 return rv;
75
76 /* guess */
77 if (va.va_type == VDIR)
78 va.va_nlink = 2;
79 else
80 va.va_nlink = 1;
81
82 pn = allocnode(pu, pn_dir, pcn->pcn_name, &va);
83 psn = pn->pn_data;
84 psn->attrread = time(NULL);
85 } else {
86 pd = lookup(psn_dir->dir, psn_dir->dentnext, pcn->pcn_name);
87 if (!pd) {
88 return ENOENT;
89 }
90
91 if (pd->entry)
92 pn = pd->entry;
93 else
94 pn = makenode(pu, pn_dir, pd, &pd->va);
95 psn = pn->pn_data;
96 }
97
98 psn->stat &= ~PSN_RECLAIMED;
99
100 puffs_newinfo_setcookie(pni, pn);
101 puffs_newinfo_setvtype(pni, pn->pn_va.va_type);
102 puffs_newinfo_setsize(pni, pn->pn_va.va_size);
103
104 return 0;
105 }
106
107 int
108 psshfs_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *vap,
109 const struct puffs_cred *pcr, const struct puffs_cid *pcid)
110 {
111 struct puffs_node *pn = opc;
112 int rv;
113
114 rv = getnodeattr(pcc, pn);
115 if (rv)
116 return rv;
117
118 memcpy(vap, &pn->pn_va, sizeof(struct vattr));
119
120 return 0;
121 }
122
123 int
124 psshfs_node_setattr(struct puffs_cc *pcc, void *opc,
125 const struct vattr *va, const struct puffs_cred *pcr,
126 const struct puffs_cid *pcid)
127 {
128 PSSHFSAUTOVAR(pcc);
129 struct vattr kludgeva;
130 struct puffs_node *pn = opc;
131
132 psbuf_req_str(pb, SSH_FXP_SETSTAT, reqid, PNPATH(pn));
133
134 memcpy(&kludgeva, va, sizeof(struct vattr));
135
136 /* XXX: kludge due to openssh server implementation */
137 if (va->va_atime.tv_sec != PUFFS_VNOVAL
138 && va->va_mtime.tv_sec == PUFFS_VNOVAL) {
139 if (pn->pn_va.va_mtime.tv_sec != PUFFS_VNOVAL)
140 kludgeva.va_mtime.tv_sec = pn->pn_va.va_mtime.tv_sec;
141 else
142 kludgeva.va_mtime.tv_sec = va->va_atime.tv_sec;
143 }
144 if (va->va_mtime.tv_sec != PUFFS_VNOVAL
145 && va->va_atime.tv_sec == PUFFS_VNOVAL) {
146 if (pn->pn_va.va_atime.tv_sec != PUFFS_VNOVAL)
147 kludgeva.va_atime.tv_sec = pn->pn_va.va_atime.tv_sec;
148 else
149 kludgeva.va_atime.tv_sec = va->va_mtime.tv_sec;
150 }
151
152 psbuf_put_vattr(pb, &kludgeva);
153 GETRESPONSE(pb);
154
155 rv = psbuf_expect_status(pb);
156 if (rv == 0)
157 puffs_setvattr(&pn->pn_va, &kludgeva);
158
159 out:
160 PSSHFSRETURN(rv);
161 }
162
163 int
164 psshfs_node_create(struct puffs_cc *pcc, void *opc, struct puffs_newinfo *pni,
165 const struct puffs_cn *pcn, const struct vattr *va)
166 {
167 PSSHFSAUTOVAR(pcc);
168 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
169 struct puffs_node *pn = opc;
170 struct puffs_node *pn_new;
171 char *fhand = NULL;
172 uint32_t fhandlen;
173
174 pn_new = allocnode(pu, pn, pcn->pcn_name, va);
175 if (!pn_new) {
176 rv = ENOMEM;
177 goto out;
178 }
179
180 psbuf_req_str(pb, SSH_FXP_OPEN, reqid, PCNPATH(pcn));
181 psbuf_put_4(pb, SSH_FXF_WRITE | SSH_FXF_CREAT | SSH_FXF_TRUNC);
182 psbuf_put_vattr(pb, va);
183 GETRESPONSE(pb);
184
185 rv = psbuf_expect_handle(pb, &fhand, &fhandlen);
186 if (rv == 0)
187 puffs_newinfo_setcookie(pni, pn_new);
188 else
189 goto out;
190
191 reqid = NEXTREQ(pctx);
192 psbuf_recycleout(pb);
193 psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, fhand, fhandlen);
194 JUSTSEND(pb);
195 free(fhand);
196 return 0;
197
198 out:
199 free(fhand);
200 PSSHFSRETURN(rv);
201 }
202
203 int
204 psshfs_node_open(struct puffs_cc *pcc, void *opc, int mode,
205 const struct puffs_cred *pcr, const struct puffs_cid *pcid)
206 {
207 PSSHFSAUTOVAR(pcc);
208 struct vattr va;
209 struct puffs_node *pn = opc;
210 struct psshfs_node *psn = pn->pn_data;
211
212 if (pn->pn_va.va_type == VDIR)
213 goto out;
214
215 puffs_setback(pcc, PUFFS_SETBACK_INACT_N1);
216 puffs_vattr_null(&va);
217 if (mode & FREAD && psn->fhand_r == NULL) {
218 psbuf_req_str(pb, SSH_FXP_OPEN, reqid, PNPATH(pn));
219 psbuf_put_4(pb, SSH_FXF_READ);
220 psbuf_put_vattr(pb, &va);
221 GETRESPONSE(pb);
222
223 rv = psbuf_expect_handle(pb, &psn->fhand_r, &psn->fhand_r_len);
224 if (rv)
225 goto out;
226 psbuf_recycleout(pb);
227 }
228 if (mode & FWRITE && psn->fhand_w == NULL) {
229 psbuf_req_str(pb, SSH_FXP_OPEN, reqid, PNPATH(pn));
230 psbuf_put_4(pb, SSH_FXF_WRITE);
231 psbuf_put_vattr(pb, &va);
232 GETRESPONSE(pb);
233
234 rv = psbuf_expect_handle(pb, &psn->fhand_w, &psn->fhand_w_len);
235 if (rv)
236 goto out;
237 }
238
239 out:
240 PSSHFSRETURN(rv);
241 }
242
243 int
244 psshfs_node_inactive(struct puffs_cc *pcc, void *opc,
245 const struct puffs_cid *pcid)
246 {
247 struct psshfs_ctx *pctx = puffs_cc_getspecific(pcc);
248 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
249 struct puffs_node *pn = opc;
250 struct psshfs_node *psn = pn->pn_data;
251 uint32_t reqid = NEXTREQ(pctx);
252 struct puffs_framebuf *pb1, *pb2;
253 int rv;
254
255 if (psn->fhand_r) {
256 pb1 = psbuf_makeout();
257 psbuf_req_data(pb1, SSH_FXP_CLOSE, reqid,
258 psn->fhand_r, psn->fhand_r_len);
259 JUSTSEND(pb1);
260 free(psn->fhand_r);
261 psn->fhand_r = NULL;
262 }
263 if (psn->fhand_w) {
264 pb2 = psbuf_makeout();
265 psbuf_req_data(pb2, SSH_FXP_CLOSE, reqid,
266 psn->fhand_w, psn->fhand_w_len);
267 JUSTSEND(pb2);
268 free(psn->fhand_w);
269 psn->fhand_w = NULL;
270 }
271
272 out:
273 return 0;
274 }
275
276 int
277 psshfs_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
278 off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
279 int *eofflag, off_t *cookies, size_t *ncookies)
280 {
281 struct psshfs_ctx *pctx = puffs_cc_getspecific(pcc);
282 struct puffs_node *pn = opc;
283 struct psshfs_node *psn = pn->pn_data;
284 struct psshfs_dir *pd;
285 int i, rv;
286
287 *ncookies = 0;
288 rv = sftp_readdir(pcc, pctx, pn);
289 if (rv)
290 return rv;
291
292 /* find next dirent */
293 for (i = *readoff;;i++) {
294 if (i == psn->dentnext)
295 goto out;
296 pd = &psn->dir[i];
297 if (pd->valid)
298 break;
299 }
300
301 for (;;) {
302 *readoff = i;
303 if (!puffs_nextdent(&dent, pd->entryname,
304 pd->va.va_fileid, puffs_vtype2dt(pd->va.va_type), reslen))
305 return 0;
306
307 /* find next entry, store possible nfs key */
308 do {
309 if (++i == psn->dentnext)
310 goto out;
311 pd = &psn->dir[i];
312 } while (pd->valid == 0);
313 PUFFS_STORE_DCOOKIE(cookies, ncookies, (off_t)i);
314 }
315
316 out:
317 if (i == psn->dentnext)
318 *eofflag = 1;
319
320 *readoff = i;
321 return 0;
322 }
323
324 int
325 psshfs_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
326 off_t offset, size_t *resid, const struct puffs_cred *pcr,
327 int ioflag)
328 {
329 PSSHFSAUTOVAR(pcc);
330 struct puffs_node *pn = opc;
331 struct psshfs_node *psn = pn->pn_data;
332 uint32_t readlen;
333
334 if (pn->pn_va.va_type == VDIR) {
335 rv = EISDIR;
336 goto out;
337 }
338
339 readlen = *resid;
340 psbuf_req_data(pb, SSH_FXP_READ, reqid, psn->fhand_r, psn->fhand_r_len);
341 psbuf_put_8(pb, offset);
342 psbuf_put_4(pb, readlen);
343 GETRESPONSE(pb);
344
345 rv = psbuf_do_data(pb, buf, &readlen);
346 if (rv == 0)
347 *resid -= readlen;
348
349 out:
350 PSSHFSRETURN(rv);
351 }
352
353 /* XXX: we should getattr for size */
354 int
355 psshfs_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
356 off_t offset, size_t *resid, const struct puffs_cred *cred,
357 int ioflag)
358 {
359 PSSHFSAUTOVAR(pcc);
360 struct puffs_node *pn = opc;
361 struct psshfs_node *psn = pn->pn_data;
362 uint32_t writelen;
363
364 if (pn->pn_va.va_type == VDIR) {
365 rv = EISDIR;
366 goto out;
367 }
368
369 writelen = *resid;
370 psbuf_req_data(pb, SSH_FXP_WRITE, reqid, psn->fhand_w,psn->fhand_w_len);
371 psbuf_put_8(pb, offset);
372 psbuf_put_data(pb, buf, writelen);
373 GETRESPONSE(pb);
374
375 rv = psbuf_expect_status(pb);
376 if (rv == 0)
377 *resid = 0;
378
379 if (pn->pn_va.va_size < offset + writelen)
380 pn->pn_va.va_size = offset + writelen;
381
382 out:
383 PSSHFSRETURN(rv);
384 }
385
386 int
387 psshfs_node_readlink(struct puffs_cc *pcc, void *opc,
388 const struct puffs_cred *cred, char *linkvalue, size_t *linklen)
389 {
390 PSSHFSAUTOVAR(pcc);
391 struct puffs_node *pn = opc;
392 char *linktmp = NULL;
393 uint32_t count;
394
395 if (pctx->protover < 3) {
396 rv = EOPNOTSUPP;
397 goto out;
398 }
399
400 psbuf_req_str(pb, SSH_FXP_READLINK, reqid, PNPATH(pn));
401 GETRESPONSE(pb);
402
403 rv = psbuf_expect_name(pb, &count);
404 if (rv)
405 goto out;
406 if (count != 1) {
407 rv = EPROTO;
408 goto out;
409 }
410
411 rv = psbuf_get_str(pb, &linktmp, (uint32_t *)linklen);
412 if (rv)
413 goto out;
414 (void) memcpy(linkvalue, linktmp, *linklen);
415
416 out:
417 free(linktmp);
418 PSSHFSRETURN(rv);
419 }
420
421 static int
422 doremove(struct puffs_cc *pcc, struct puffs_node *pn_dir,
423 struct puffs_node *pn, const char *name)
424 {
425 PSSHFSAUTOVAR(pcc);
426 int op;
427
428 if (pn->pn_va.va_type == VDIR)
429 op = SSH_FXP_RMDIR;
430 else
431 op = SSH_FXP_REMOVE;
432
433 psbuf_req_str(pb, op, reqid, PNPATH(pn));
434 GETRESPONSE(pb);
435
436 rv = psbuf_expect_status(pb);
437 if (rv == 0)
438 nukenode(pn, name, 0);
439
440 out:
441 PSSHFSRETURN(rv);
442 }
443
444 int
445 psshfs_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
446 const struct puffs_cn *pcn)
447 {
448 struct puffs_node *pn_targ = targ;
449 int rv;
450
451 assert(pn_targ->pn_va.va_type != VDIR);
452
453 rv = doremove(pcc, opc, targ, pcn->pcn_name);
454 if (rv == 0)
455 puffs_setback(pcc, PUFFS_SETBACK_NOREF_N2);
456
457 return rv;
458 }
459
460 int
461 psshfs_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
462 const struct puffs_cn *pcn)
463 {
464 struct puffs_node *pn_targ = targ;
465 int rv;
466
467 assert(pn_targ->pn_va.va_type == VDIR);
468
469 rv = doremove(pcc, opc, targ, pcn->pcn_name);
470 if (rv == 0)
471 puffs_setback(pcc, PUFFS_SETBACK_NOREF_N2);
472
473 return rv;
474 }
475
476 int
477 psshfs_node_mkdir(struct puffs_cc *pcc, void *opc, struct puffs_newinfo *pni,
478 const struct puffs_cn *pcn, const struct vattr *va)
479 {
480 PSSHFSAUTOVAR(pcc);
481 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
482 struct puffs_node *pn = opc;
483 struct puffs_node *pn_new;
484
485 pn_new = allocnode(pu, pn, pcn->pcn_name, va);
486 if (!pn_new) {
487 rv = ENOMEM;
488 goto out;
489 }
490
491 psbuf_req_str(pb, SSH_FXP_MKDIR, reqid, PCNPATH(pcn));
492 psbuf_put_vattr(pb, va);
493 GETRESPONSE(pb);
494
495 rv = psbuf_expect_status(pb);
496
497 if (rv == 0)
498 puffs_newinfo_setcookie(pni, pn_new);
499 else
500 nukenode(pn_new, pcn->pcn_name, 1);
501
502 out:
503 PSSHFSRETURN(rv);
504 }
505
506 int
507 psshfs_node_symlink(struct puffs_cc *pcc, void *opc, struct puffs_newinfo *pni,
508 const struct puffs_cn *pcn, const struct vattr *va,
509 const char *link_target)
510 {
511 PSSHFSAUTOVAR(pcc);
512 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
513 struct puffs_node *pn = opc;
514 struct puffs_node *pn_new;
515
516 if (pctx->protover < 3) {
517 rv = EOPNOTSUPP;
518 goto out;
519 }
520
521 pn_new = allocnode(pu, pn, pcn->pcn_name, va);
522 if (!pn_new) {
523 rv = ENOMEM;
524 goto out;
525 }
526
527 /*
528 * XXX: ietf says: source, target. openssh says: ietf who?
529 * Let's go with openssh and build quirk tables later if we care
530 */
531 psbuf_req_str(pb, SSH_FXP_SYMLINK, reqid, link_target);
532 psbuf_put_str(pb, PCNPATH(pcn));
533 GETRESPONSE(pb);
534
535 rv = psbuf_expect_status(pb);
536 if (rv == 0)
537 puffs_newinfo_setcookie(pni, pn_new);
538 else
539 nukenode(pn_new, pcn->pcn_name, 1);
540
541 out:
542 PSSHFSRETURN(rv);
543 }
544
545 int
546 psshfs_node_rename(struct puffs_cc *pcc, void *opc, void *src,
547 const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
548 const struct puffs_cn *pcn_targ)
549 {
550 PSSHFSAUTOVAR(pcc);
551 struct puffs_node *pn_sf = src;
552 struct puffs_node *pn_td = targ_dir, *pn_tf = targ;
553 struct psshfs_node *psn_targdir = pn_td->pn_data;
554
555 if (pctx->protover < 2) {
556 rv = EOPNOTSUPP;
557 goto out;
558 }
559
560 if (pn_tf) {
561 rv = doremove(pcc, targ_dir, pn_tf, pcn_targ->pcn_name);
562 if (rv)
563 goto out;
564 }
565
566 psbuf_req_str(pb, SSH_FXP_RENAME, reqid, PCNPATH(pcn_src));
567 psbuf_put_str(pb, PCNPATH(pcn_targ));
568 GETRESPONSE(pb);
569
570 rv = psbuf_expect_status(pb);
571 if (rv == 0) {
572 struct psshfs_dir *pd;
573
574 /*
575 * XXX: interfaces didn't quite work with rename..
576 * the song remains the same. go figure .. ;)
577 */
578 nukenode(pn_sf, pcn_src->pcn_name, 0);
579 pd = direnter(pn_td, pcn_targ->pcn_name);
580 pd->entry = pn_sf;
581 puffs_setvattr(&pd->va, &pn_sf->pn_va);
582
583 if (opc != targ_dir) {
584 psn_targdir->childcount++;
585 if (pn_sf->pn_va.va_type == VDIR)
586 pn_td->pn_va.va_nlink++;
587 }
588 }
589
590 out:
591 PSSHFSRETURN(rv);
592 }
593
594 /*
595 * So this file system happened to be written in such a way that
596 * lookup for ".." is hard if we lose the in-memory node. We'd
597 * need to recreate the entire directory structure from the root
598 * node up to the ".." node we're looking up.
599 *
600 * And since our entire fs structure is purely fictional (i.e. it's
601 * only in-memory, not fetchable from the server), the easiest way
602 * to deal with it is to not allow nodes with children to be
603 * reclaimed.
604 *
605 * If a node with children is being attempted to be reclaimed, we
606 * just mark it "reclaimed" but leave it as is until all its children
607 * have been reclaimed. If a lookup for that node is done meanwhile,
608 * it will be found by lookup() and we just remove the "reclaimed"
609 * bit.
610 */
611 int
612 psshfs_node_reclaim(struct puffs_cc *pcc, void *opc,
613 const struct puffs_cid *pcid)
614 {
615 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
616 struct puffs_node *pn = opc, *pn_next, *pn_root;
617 struct psshfs_node *psn = pn->pn_data;
618
619 /*
620 * don't reclaim if we have file handle issued, otherwise
621 * we can't do fhtonode
622 */
623 if (psn->stat & PSN_HASFH)
624 return 0;
625
626 psn->stat |= PSN_RECLAIMED;
627 pn_root = puffs_getroot(pu);
628 for (; pn != pn_root; pn = pn_next) {
629 psn = pn->pn_data;
630 if ((psn->stat & PSN_RECLAIMED) == 0 || psn->childcount != 0)
631 break;
632
633 pn_next = psn->parent;
634 doreclaim(pn);
635 }
636
637 return 0;
638 }
639