node.c revision 1.34 1 /* $NetBSD: node.c,v 1.34 2007/07/16 09:36:06 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.34 2007/07/16 09:36:06 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 for (i = *readoff; i < psn->dentnext; i++) {
293 pd = &psn->dir[i];
294 if (pd->valid == 0)
295 continue;
296 if (!puffs_nextdent(&dent, pd->entryname,
297 pd->va.va_fileid, puffs_vtype2dt(pd->va.va_type), reslen))
298 break;
299 PUFFS_STORE_DCOOKIE(cookies, ncookies, (off_t)i);
300 }
301 if (i == psn->dentnext)
302 *eofflag = 1;
303
304 *readoff = i;
305 return 0;
306 }
307
308 int
309 psshfs_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
310 off_t offset, size_t *resid, const struct puffs_cred *pcr,
311 int ioflag)
312 {
313 PSSHFSAUTOVAR(pcc);
314 struct puffs_node *pn = opc;
315 struct psshfs_node *psn = pn->pn_data;
316 uint32_t readlen;
317
318 if (pn->pn_va.va_type == VDIR) {
319 rv = EISDIR;
320 goto out;
321 }
322
323 readlen = *resid;
324 psbuf_req_data(pb, SSH_FXP_READ, reqid, psn->fhand_r, psn->fhand_r_len);
325 psbuf_put_8(pb, offset);
326 psbuf_put_4(pb, readlen);
327 GETRESPONSE(pb);
328
329 rv = psbuf_do_data(pb, buf, &readlen);
330 if (rv == 0)
331 *resid -= readlen;
332
333 out:
334 PSSHFSRETURN(rv);
335 }
336
337 /* XXX: we should getattr for size */
338 int
339 psshfs_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
340 off_t offset, size_t *resid, const struct puffs_cred *cred,
341 int ioflag)
342 {
343 PSSHFSAUTOVAR(pcc);
344 struct puffs_node *pn = opc;
345 struct psshfs_node *psn = pn->pn_data;
346 uint32_t writelen;
347
348 if (pn->pn_va.va_type == VDIR) {
349 rv = EISDIR;
350 goto out;
351 }
352
353 writelen = *resid;
354 psbuf_req_data(pb, SSH_FXP_WRITE, reqid, psn->fhand_w,psn->fhand_w_len);
355 psbuf_put_8(pb, offset);
356 psbuf_put_data(pb, buf, writelen);
357 GETRESPONSE(pb);
358
359 rv = psbuf_expect_status(pb);
360 if (rv == 0)
361 *resid = 0;
362
363 if (pn->pn_va.va_size < offset + writelen)
364 pn->pn_va.va_size = offset + writelen;
365
366 out:
367 PSSHFSRETURN(rv);
368 }
369
370 int
371 psshfs_node_readlink(struct puffs_cc *pcc, void *opc,
372 const struct puffs_cred *cred, char *linkvalue, size_t *linklen)
373 {
374 PSSHFSAUTOVAR(pcc);
375 struct puffs_node *pn = opc;
376 char *linktmp = NULL;
377 uint32_t count;
378
379 if (pctx->protover < 3) {
380 rv = EOPNOTSUPP;
381 goto out;
382 }
383
384 psbuf_req_str(pb, SSH_FXP_READLINK, reqid, PNPATH(pn));
385 GETRESPONSE(pb);
386
387 rv = psbuf_expect_name(pb, &count);
388 if (rv)
389 goto out;
390 if (count != 1) {
391 rv = EPROTO;
392 goto out;
393 }
394
395 rv = psbuf_get_str(pb, &linktmp, (uint32_t *)linklen);
396 if (rv)
397 goto out;
398 (void) memcpy(linkvalue, linktmp, *linklen);
399
400 out:
401 free(linktmp);
402 PSSHFSRETURN(rv);
403 }
404
405 static int
406 doremove(struct puffs_cc *pcc, struct puffs_node *pn_dir,
407 struct puffs_node *pn, const char *name)
408 {
409 PSSHFSAUTOVAR(pcc);
410 int op;
411
412 if (pn->pn_va.va_type == VDIR)
413 op = SSH_FXP_RMDIR;
414 else
415 op = SSH_FXP_REMOVE;
416
417 psbuf_req_str(pb, op, reqid, PNPATH(pn));
418 GETRESPONSE(pb);
419
420 rv = psbuf_expect_status(pb);
421 if (rv == 0)
422 nukenode(pn, name, 0);
423
424 out:
425 PSSHFSRETURN(rv);
426 }
427
428 int
429 psshfs_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
430 const struct puffs_cn *pcn)
431 {
432 struct puffs_node *pn_targ = targ;
433 int rv;
434
435 assert(pn_targ->pn_va.va_type != VDIR);
436
437 rv = doremove(pcc, opc, targ, pcn->pcn_name);
438 if (rv == 0)
439 puffs_setback(pcc, PUFFS_SETBACK_NOREF_N2);
440
441 return rv;
442 }
443
444 int
445 psshfs_node_rmdir(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_mkdir(struct puffs_cc *pcc, void *opc, struct puffs_newinfo *pni,
462 const struct puffs_cn *pcn, const struct vattr *va)
463 {
464 PSSHFSAUTOVAR(pcc);
465 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
466 struct puffs_node *pn = opc;
467 struct puffs_node *pn_new;
468
469 pn_new = allocnode(pu, pn, pcn->pcn_name, va);
470 if (!pn_new) {
471 rv = ENOMEM;
472 goto out;
473 }
474
475 psbuf_req_str(pb, SSH_FXP_MKDIR, reqid, PCNPATH(pcn));
476 psbuf_put_vattr(pb, va);
477 GETRESPONSE(pb);
478
479 rv = psbuf_expect_status(pb);
480
481 if (rv == 0)
482 puffs_newinfo_setcookie(pni, pn_new);
483 else
484 nukenode(pn_new, pcn->pcn_name, 1);
485
486 out:
487 PSSHFSRETURN(rv);
488 }
489
490 int
491 psshfs_node_symlink(struct puffs_cc *pcc, void *opc, struct puffs_newinfo *pni,
492 const struct puffs_cn *pcn, const struct vattr *va,
493 const char *link_target)
494 {
495 PSSHFSAUTOVAR(pcc);
496 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
497 struct puffs_node *pn = opc;
498 struct puffs_node *pn_new;
499
500 if (pctx->protover < 3) {
501 rv = EOPNOTSUPP;
502 goto out;
503 }
504
505 pn_new = allocnode(pu, pn, pcn->pcn_name, va);
506 if (!pn_new) {
507 rv = ENOMEM;
508 goto out;
509 }
510
511 /*
512 * XXX: ietf says: source, target. openssh says: ietf who?
513 * Let's go with openssh and build quirk tables later if we care
514 */
515 psbuf_req_str(pb, SSH_FXP_SYMLINK, reqid, link_target);
516 psbuf_put_str(pb, PCNPATH(pcn));
517 GETRESPONSE(pb);
518
519 rv = psbuf_expect_status(pb);
520 if (rv == 0)
521 puffs_newinfo_setcookie(pni, pn_new);
522 else
523 nukenode(pn_new, pcn->pcn_name, 1);
524
525 out:
526 PSSHFSRETURN(rv);
527 }
528
529 int
530 psshfs_node_rename(struct puffs_cc *pcc, void *opc, void *src,
531 const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
532 const struct puffs_cn *pcn_targ)
533 {
534 PSSHFSAUTOVAR(pcc);
535 struct puffs_node *pn_sf = src;
536 struct puffs_node *pn_td = targ_dir, *pn_tf = targ;
537 struct psshfs_node *psn_targdir = pn_td->pn_data;
538
539 if (pctx->protover < 2) {
540 rv = EOPNOTSUPP;
541 goto out;
542 }
543
544 if (pn_tf) {
545 rv = doremove(pcc, targ_dir, pn_tf, pcn_targ->pcn_name);
546 if (rv)
547 goto out;
548 }
549
550 psbuf_req_str(pb, SSH_FXP_RENAME, reqid, PCNPATH(pcn_src));
551 psbuf_put_str(pb, PCNPATH(pcn_targ));
552 GETRESPONSE(pb);
553
554 rv = psbuf_expect_status(pb);
555 if (rv == 0) {
556 struct psshfs_dir *pd;
557
558 /*
559 * XXX: interfaces didn't quite work with rename..
560 * the song remains the same. go figure .. ;)
561 */
562 nukenode(pn_sf, pcn_src->pcn_name, 0);
563 pd = direnter(pn_td, pcn_targ->pcn_name);
564 pd->entry = pn_sf;
565 puffs_setvattr(&pd->va, &pn_sf->pn_va);
566
567 if (opc != targ_dir) {
568 psn_targdir->childcount++;
569 if (pn_sf->pn_va.va_type == VDIR)
570 pn_td->pn_va.va_nlink++;
571 }
572 }
573
574 out:
575 PSSHFSRETURN(rv);
576 }
577
578 /*
579 * So this file system happened to be written in such a way that
580 * lookup for ".." is hard if we lose the in-memory node. We'd
581 * need to recreate the entire directory structure from the root
582 * node up to the ".." node we're looking up.
583 *
584 * And since our entire fs structure is purely fictional (i.e. it's
585 * only in-memory, not fetchable from the server), the easiest way
586 * to deal with it is to not allow nodes with children to be
587 * reclaimed.
588 *
589 * If a node with children is being attempted to be reclaimed, we
590 * just mark it "reclaimed" but leave it as is until all its children
591 * have been reclaimed. If a lookup for that node is done meanwhile,
592 * it will be found by lookup() and we just remove the "reclaimed"
593 * bit.
594 */
595 int
596 psshfs_node_reclaim(struct puffs_cc *pcc, void *opc,
597 const struct puffs_cid *pcid)
598 {
599 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
600 struct puffs_node *pn = opc, *pn_next, *pn_root;
601 struct psshfs_node *psn = pn->pn_data;
602
603 /*
604 * don't reclaim if we have file handle issued, otherwise
605 * we can't do fhtonode
606 */
607 if (psn->stat & PSN_HASFH)
608 return 0;
609
610 psn->stat |= PSN_RECLAIMED;
611 pn_root = puffs_getroot(pu);
612 for (; pn != pn_root; pn = pn_next) {
613 psn = pn->pn_data;
614 if ((psn->stat & PSN_RECLAIMED) == 0 || psn->childcount != 0)
615 break;
616
617 pn_next = psn->parent;
618 doreclaim(pn);
619 }
620
621 return 0;
622 }
623