node.c revision 1.33 1 /* $NetBSD: node.c,v 1.33 2007/07/02 10:26:50 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.33 2007/07/02 10:26:50 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 int
406 psshfs_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
407 const struct puffs_cn *pcn)
408 {
409 PSSHFSAUTOVAR(pcc);
410 struct puffs_node *pn_targ = targ;
411
412 if (pn_targ->pn_va.va_type == VDIR) {
413 rv = EPERM;
414 goto out;
415 }
416
417 psbuf_req_str(pb, SSH_FXP_REMOVE, reqid, PNPATH(pn_targ));
418 GETRESPONSE(pb);
419
420 rv = psbuf_expect_status(pb);
421
422 if (rv == 0) {
423 nukenode(pn_targ, pcn->pcn_name, 0);
424 puffs_setback(pcc, PUFFS_SETBACK_NOREF_N2);
425 }
426
427 out:
428 PSSHFSRETURN(rv);
429 }
430
431 int
432 psshfs_node_mkdir(struct puffs_cc *pcc, void *opc, struct puffs_newinfo *pni,
433 const struct puffs_cn *pcn, const struct vattr *va)
434 {
435 PSSHFSAUTOVAR(pcc);
436 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
437 struct puffs_node *pn = opc;
438 struct puffs_node *pn_new;
439
440 pn_new = allocnode(pu, pn, pcn->pcn_name, va);
441 if (!pn_new) {
442 rv = ENOMEM;
443 goto out;
444 }
445
446 psbuf_req_str(pb, SSH_FXP_MKDIR, reqid, PCNPATH(pcn));
447 psbuf_put_vattr(pb, va);
448 GETRESPONSE(pb);
449
450 rv = psbuf_expect_status(pb);
451
452 if (rv == 0)
453 puffs_newinfo_setcookie(pni, pn_new);
454 else
455 nukenode(pn_new, pcn->pcn_name, 1);
456
457 out:
458 PSSHFSRETURN(rv);
459 }
460
461 int
462 psshfs_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
463 const struct puffs_cn *pcn)
464 {
465 PSSHFSAUTOVAR(pcc);
466 struct puffs_node *pn_targ = targ;
467
468 psbuf_req_str(pb, SSH_FXP_RMDIR, reqid, PNPATH(pn_targ));
469 GETRESPONSE(pb);
470
471 rv = psbuf_expect_status(pb);
472 if (rv == 0) {
473 nukenode(pn_targ, pcn->pcn_name, 0);
474 puffs_setback(pcc, PUFFS_SETBACK_NOREF_N2);
475 }
476
477 out:
478 PSSHFSRETURN(rv);
479 }
480
481 int
482 psshfs_node_symlink(struct puffs_cc *pcc, void *opc, struct puffs_newinfo *pni,
483 const struct puffs_cn *pcn, const struct vattr *va,
484 const char *link_target)
485 {
486 PSSHFSAUTOVAR(pcc);
487 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
488 struct puffs_node *pn = opc;
489 struct puffs_node *pn_new;
490
491 if (pctx->protover < 3) {
492 rv = EOPNOTSUPP;
493 goto out;
494 }
495
496 pn_new = allocnode(pu, pn, pcn->pcn_name, va);
497 if (!pn_new) {
498 rv = ENOMEM;
499 goto out;
500 }
501
502 /*
503 * XXX: ietf says: source, target. openssh says: ietf who?
504 * Let's go with openssh and build quirk tables later if we care
505 */
506 psbuf_req_str(pb, SSH_FXP_SYMLINK, reqid, link_target);
507 psbuf_put_str(pb, PCNPATH(pcn));
508 GETRESPONSE(pb);
509
510 rv = psbuf_expect_status(pb);
511 if (rv == 0)
512 puffs_newinfo_setcookie(pni, pn_new);
513 else
514 nukenode(pn_new, pcn->pcn_name, 1);
515
516 out:
517 PSSHFSRETURN(rv);
518 }
519
520 int
521 psshfs_node_rename(struct puffs_cc *pcc, void *opc, void *src,
522 const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
523 const struct puffs_cn *pcn_targ)
524 {
525 PSSHFSAUTOVAR(pcc);
526 struct puffs_node *pn_sf = src;
527 struct puffs_node *pn_td = targ_dir, *pn_tf = targ;
528 struct psshfs_node *psn_targdir = pn_td->pn_data;
529
530 if (pctx->protover < 2) {
531 rv = EOPNOTSUPP;
532 goto out;
533 }
534
535 if (pn_tf) {
536 /* XXX: no backend implementation for now, so call directly */
537 rv = psshfs_node_remove(pcc, targ_dir, pn_tf, pcn_targ);
538 if (rv)
539 goto out;
540 }
541
542 psbuf_req_str(pb, SSH_FXP_RENAME, reqid, PCNPATH(pcn_src));
543 psbuf_put_str(pb, PCNPATH(pcn_targ));
544 GETRESPONSE(pb);
545
546 rv = psbuf_expect_status(pb);
547 if (rv == 0) {
548 struct psshfs_dir *pd;
549
550 /*
551 * XXX: interfaces didn't quite work with rename..
552 * the song remains the same. go figure .. ;)
553 */
554 nukenode(pn_sf, pcn_src->pcn_name, 0);
555 pd = direnter(pn_td, pcn_targ->pcn_name);
556 pd->entry = pn_sf;
557 puffs_setvattr(&pd->va, &pn_sf->pn_va);
558
559 if (opc != targ_dir) {
560 psn_targdir->childcount++;
561 if (pn_sf->pn_va.va_type == VDIR)
562 pn_td->pn_va.va_nlink++;
563 }
564 }
565
566 out:
567 PSSHFSRETURN(rv);
568 }
569
570 /*
571 * So this file system happened to be written in such a way that
572 * lookup for ".." is hard if we lose the in-memory node. We'd
573 * need to recreate the entire directory structure from the root
574 * node up to the ".." node we're looking up.
575 *
576 * And since our entire fs structure is purely fictional (i.e. it's
577 * only in-memory, not fetchable from the server), the easiest way
578 * to deal with it is to not allow nodes with children to be
579 * reclaimed.
580 *
581 * If a node with children is being attempted to be reclaimed, we
582 * just mark it "reclaimed" but leave it as is until all its children
583 * have been reclaimed. If a lookup for that node is done meanwhile,
584 * it will be found by lookup() and we just remove the "reclaimed"
585 * bit.
586 */
587 int
588 psshfs_node_reclaim(struct puffs_cc *pcc, void *opc,
589 const struct puffs_cid *pcid)
590 {
591 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
592 struct puffs_node *pn = opc, *pn_next, *pn_root;
593 struct psshfs_node *psn = pn->pn_data;
594
595 /*
596 * don't reclaim if we have file handle issued, otherwise
597 * we can't do fhtonode
598 */
599 if (psn->stat & PSN_HASFH)
600 return 0;
601
602 psn->stat |= PSN_RECLAIMED;
603 pn_root = puffs_getroot(pu);
604 for (; pn != pn_root; pn = pn_next) {
605 psn = pn->pn_data;
606 if ((psn->stat & PSN_RECLAIMED) == 0 || psn->childcount != 0)
607 break;
608
609 pn_next = psn->parent;
610 doreclaim(pn);
611 }
612
613 return 0;
614 }
615