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