node.c revision 1.23 1 /* $NetBSD: node.c,v 1.23 2007/05/06 15:30:18 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.23 2007/05/06 15:30:18 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 if (rv != EPERM)
70 return rv;
71
72 /*
73 * Can't read the directory. We still might be
74 * able to find the node with getattr in -r+x dirs
75 */
76 rv = getpathattr(pcc, PCNPATH(pcn), &va);
77 if (rv)
78 return rv;
79
80 /* guess */
81 if (va.va_type == VDIR)
82 va.va_nlink = 2;
83 else
84 va.va_nlink = 1;
85
86 pn = allocnode(pu, pn_dir, pcn->pcn_name, &va);
87 psn = pn->pn_data;
88 psn->attrread = time(NULL);
89 } else {
90 pd = lookup(psn_dir->dir, psn_dir->dentnext, pcn->pcn_name);
91 if (!pd) {
92 return ENOENT;
93 }
94
95 if (pd->entry)
96 pn = pd->entry;
97 else
98 pn = makenode(pu, pn_dir, pd, &pd->va);
99 psn = pn->pn_data;
100 }
101
102 psn->reclaimed = 0;
103
104 *newnode = pn;
105 *newsize = pn->pn_va.va_size;
106 *newtype = pn->pn_va.va_type;
107
108 return 0;
109 }
110
111 int
112 psshfs_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *vap,
113 const struct puffs_cred *pcr, pid_t pid)
114 {
115 struct puffs_node *pn = opc;
116 int rv;
117
118 rv = getnodeattr(pcc, pn);
119 if (rv)
120 return rv;
121
122 memcpy(vap, &pn->pn_va, sizeof(struct vattr));
123
124 return 0;
125 }
126
127 int
128 psshfs_node_setattr(struct puffs_cc *pcc, void *opc,
129 const struct vattr *va, const struct puffs_cred *pcr, pid_t pid)
130 {
131 PSSHFSAUTOVAR(pcc);
132 struct vattr kludgeva;
133 struct puffs_node *pn = opc;
134
135 psbuf_req_str(pb, SSH_FXP_SETSTAT, reqid, PNPATH(pn));
136
137 memcpy(&kludgeva, va, sizeof(struct vattr));
138
139 /* XXX: kludge due to openssh server implementation */
140 if (va->va_atime.tv_sec != PUFFS_VNOVAL
141 && va->va_mtime.tv_sec == PUFFS_VNOVAL) {
142 if (pn->pn_va.va_mtime.tv_sec != PUFFS_VNOVAL)
143 kludgeva.va_mtime.tv_sec = pn->pn_va.va_mtime.tv_sec;
144 else
145 kludgeva.va_mtime.tv_sec = va->va_atime.tv_sec;
146 }
147 if (va->va_mtime.tv_sec != PUFFS_VNOVAL
148 && va->va_atime.tv_sec == PUFFS_VNOVAL) {
149 if (pn->pn_va.va_atime.tv_sec != PUFFS_VNOVAL)
150 kludgeva.va_atime.tv_sec = pn->pn_va.va_atime.tv_sec;
151 else
152 kludgeva.va_atime.tv_sec = va->va_mtime.tv_sec;
153 }
154
155 psbuf_put_vattr(pb, &kludgeva);
156 puffs_framebuf_enqueue_cc(pcc, pb);
157
158 rv = psbuf_expect_status(pb);
159 if (rv == 0)
160 puffs_setvattr(&pn->pn_va, &kludgeva);
161
162 PSSHFSRETURN(rv);
163 }
164
165 int
166 psshfs_node_create(struct puffs_cc *pcc, void *opc, void **newnode,
167 const struct puffs_cn *pcn, const struct vattr *va)
168 {
169 PSSHFSAUTOVAR(pcc);
170 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
171 struct puffs_node *pn = opc;
172 struct puffs_node *pn_new;
173 char *fhand = NULL;
174 uint32_t fhandlen;
175
176 pn_new = allocnode(pu, pn, pcn->pcn_name, va);
177 if (!pn_new) {
178 rv = ENOMEM;
179 goto out;
180 }
181
182 psbuf_req_str(pb, SSH_FXP_OPEN, reqid, PCNPATH(pcn));
183 psbuf_put_4(pb, SSH_FXF_WRITE | SSH_FXF_CREAT | SSH_FXF_TRUNC);
184 psbuf_put_vattr(pb, va);
185 puffs_framebuf_enqueue_cc(pcc, pb);
186
187 rv = psbuf_expect_handle(pb, &fhand, &fhandlen);
188 if (rv == 0)
189 *newnode = pn_new;
190 else
191 goto out;
192
193 reqid = NEXTREQ(pctx);
194 psbuf_recycleout(pb);
195 psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, fhand, fhandlen);
196 puffs_framebuf_enqueue_justsend(pu, pb, 1);
197 free(fhand);
198 return 0;
199
200 out:
201 free(fhand);
202 PSSHFSRETURN(rv);
203 }
204
205 int
206 psshfs_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
207 off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
208 int *eofflag, off_t *cookies, size_t *ncookies)
209 {
210 struct psshfs_ctx *pctx = puffs_cc_getspecific(pcc);
211 struct puffs_node *pn = opc;
212 struct psshfs_node *psn = pn->pn_data;
213 struct psshfs_dir *pd;
214 int i, rv;
215
216 *ncookies = 0;
217 rv = sftp_readdir(pcc, pctx, pn);
218 if (rv)
219 return rv;
220
221 for (i = *readoff; i < psn->dentnext; i++) {
222 pd = &psn->dir[i];
223 if (pd->valid == 0)
224 continue;
225 if (!puffs_nextdent(&dent, pd->entryname,
226 pd->va.va_fileid, puffs_vtype2dt(pd->va.va_type), reslen))
227 break;
228 PUFFS_STORE_DCOOKIE(cookies, ncookies, (off_t)i);
229 }
230 if (i == psn->dentnext)
231 *eofflag = 1;
232
233 *readoff = i;
234 return 0;
235 }
236
237 int
238 psshfs_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
239 off_t offset, size_t *resid, const struct puffs_cred *pcr,
240 int ioflag)
241 {
242 PSSHFSAUTOVAR(pcc);
243 struct puffs_node *pn = opc;
244 char *fhand = NULL;
245 struct vattr va;
246 uint32_t readlen, fhandlen;
247
248 if (pn->pn_va.va_type == VDIR) {
249 rv = EISDIR;
250 goto err;
251 }
252
253 puffs_vattr_null(&va);
254 psbuf_req_str(pb, SSH_FXP_OPEN, reqid, PNPATH(pn));
255 psbuf_put_4(pb, SSH_FXF_READ);
256 psbuf_put_vattr(pb, &va);
257 puffs_framebuf_enqueue_cc(pcc, pb);
258
259 rv = psbuf_expect_handle(pb, &fhand, &fhandlen);
260 if (rv)
261 goto err;
262
263 readlen = *resid;
264 reqid = NEXTREQ(pctx);
265 psbuf_recycleout(pb);
266 psbuf_req_data(pb, SSH_FXP_READ, reqid, fhand, fhandlen);
267 psbuf_put_8(pb, offset);
268 psbuf_put_4(pb, readlen);
269 puffs_framebuf_enqueue_cc(pcc, pb);
270
271 rv = psbuf_do_data(pb, buf, &readlen);
272 if (rv == 0)
273 *resid -= readlen;
274
275 reqid = NEXTREQ(pctx);
276 psbuf_recycleout(pb);
277 psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, fhand, fhandlen);
278
279 puffs_framebuf_enqueue_justsend(puffs_cc_getusermount(pcc), pb, 1);
280 free(fhand);
281 return 0;
282
283 err:
284 free(fhand);
285 PSSHFSRETURN(rv);
286 }
287
288 /* XXX: we should getattr for size */
289 int
290 psshfs_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
291 off_t offset, size_t *resid, const struct puffs_cred *cred,
292 int ioflag)
293 {
294 PSSHFSAUTOVAR(pcc);
295 struct puffs_node *pn = opc;
296 char *fhand = NULL;
297 struct vattr va, kludgeva1, kludgeva2;
298 uint32_t writelen, oflags, fhandlen;
299
300 if (pn->pn_va.va_type == VDIR) {
301 rv = EISDIR;
302 goto err;
303 }
304
305 /*
306 * XXXX: this is wrong - we shouldn't muck the file permissions
307 * at this stage any more. However, we need this, since currently
308 * we can't tell the sftp server "hey, this data was already
309 * authenticated to UBC, it's ok to let us write this". Yes, it
310 * will fail e.g. if we don't own the file. Tough love.
311 *
312 * TODO-point: Investigate solving this with open filehandles
313 * or something like that.
314 */
315 kludgeva1 = pn->pn_va;
316
317 puffs_vattr_null(&kludgeva2);
318 kludgeva2.va_mode = 0700;
319 rv = psshfs_node_setattr(pcc, opc, &kludgeva2, cred, 0);
320 if (rv)
321 goto err;
322
323 /* XXXcontinuation: ok, file is mode 700 now, we can open it rw */
324
325 oflags = SSH_FXF_WRITE;
326 #if 0
327 /*
328 * At least OpenSSH doesn't appear to support this, so can't
329 * do it the right way.
330 */
331 if (ioflag & PUFFS_IO_APPEND)
332 oflags |= SSH_FXF_APPEND;
333 #endif
334 if (ioflag & PUFFS_IO_APPEND)
335 offset = pn->pn_va.va_size;
336
337 puffs_vattr_null(&va);
338 psbuf_req_str(pb, SSH_FXP_OPEN, reqid, PNPATH(pn));
339 psbuf_put_4(pb, oflags);
340 psbuf_put_vattr(pb, &va);
341 puffs_framebuf_enqueue_cc(pcc, pb);
342
343 rv = psbuf_expect_handle(pb, &fhand, &fhandlen);
344 if (rv)
345 goto err;
346
347 /* moreXXX: file is open, revert old creds for crying out loud! */
348 rv = psshfs_node_setattr(pcc, opc, &kludgeva1, cred, 0);
349
350 /* are we screwed a la royal jelly? */
351 if (rv)
352 goto closefile;
353
354 writelen = *resid;
355 reqid = NEXTREQ(pctx);
356 psbuf_recycleout(pb);
357 psbuf_req_data(pb, SSH_FXP_WRITE, reqid, fhand, fhandlen);
358 psbuf_put_8(pb, offset);
359 psbuf_put_data(pb, buf, writelen);
360 puffs_framebuf_enqueue_cc(pcc, pb);
361
362 rv = psbuf_expect_status(pb);
363 if (rv == 0)
364 *resid = 0;
365
366 if (pn->pn_va.va_size < offset + writelen)
367 pn->pn_va.va_size = offset + writelen;
368
369 closefile:
370 reqid = NEXTREQ(pctx);
371 psbuf_recycleout(pb);
372 psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, fhand, fhandlen);
373
374 puffs_framebuf_enqueue_justsend(puffs_cc_getusermount(pcc), pb, 1);
375 free(fhand);
376 return 0;
377
378 err:
379 free(fhand);
380 PSSHFSRETURN(rv);
381 }
382
383 int
384 psshfs_node_readlink(struct puffs_cc *pcc, void *opc,
385 const struct puffs_cred *cred, char *linkvalue, size_t *linklen)
386 {
387 PSSHFSAUTOVAR(pcc);
388 struct puffs_node *pn = opc;
389 char *linktmp = NULL;
390 uint32_t count;
391
392 if (pctx->protover < 3) {
393 rv = EOPNOTSUPP;
394 goto out;
395 }
396
397 psbuf_req_str(pb, SSH_FXP_READLINK, reqid, PNPATH(pn));
398 puffs_framebuf_enqueue_cc(pcc, pb);
399
400 rv = psbuf_expect_name(pb, &count);
401 if (rv)
402 goto out;
403 if (count != 1) {
404 rv = EPROTO;
405 goto out;
406 }
407
408 rv = psbuf_get_str(pb, &linktmp, (uint32_t *)linklen);
409 if (rv)
410 goto out;
411 (void) memcpy(linkvalue, linktmp, *linklen);
412
413 out:
414 free(linktmp);
415 PSSHFSRETURN(rv);
416 }
417
418 int
419 psshfs_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
420 const struct puffs_cn *pcn)
421 {
422 PSSHFSAUTOVAR(pcc);
423 struct puffs_node *pn_targ = targ;
424
425 if (pn_targ->pn_va.va_type == VDIR) {
426 rv = EPERM;
427 goto out;
428 }
429
430 psbuf_req_str(pb, SSH_FXP_REMOVE, reqid, PNPATH(pn_targ));
431 puffs_framebuf_enqueue_cc(pcc, pb);
432
433 rv = psbuf_expect_status(pb);
434
435 if (rv == 0)
436 nukenode(pn_targ, pcn->pcn_name, 0);
437
438 out:
439 PSSHFSRETURN(rv);
440 }
441
442 int
443 psshfs_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
444 const struct puffs_cn *pcn, const struct vattr *va)
445 {
446 PSSHFSAUTOVAR(pcc);
447 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
448 struct puffs_node *pn = opc;
449 struct puffs_node *pn_new;
450
451 pn_new = allocnode(pu, pn, pcn->pcn_name, va);
452 if (!pn_new) {
453 rv = ENOMEM;
454 goto out;
455 }
456
457 psbuf_req_str(pb, SSH_FXP_MKDIR, reqid, PCNPATH(pcn));
458 psbuf_put_vattr(pb, va);
459 puffs_framebuf_enqueue_cc(pcc, pb);
460
461 rv = psbuf_expect_status(pb);
462
463 if (rv == 0)
464 *newnode = pn_new;
465 else
466 nukenode(pn_new, pcn->pcn_name, 1);
467
468 out:
469 PSSHFSRETURN(rv);
470 }
471
472 int
473 psshfs_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
474 const struct puffs_cn *pcn)
475 {
476 PSSHFSAUTOVAR(pcc);
477 struct puffs_node *pn_targ = targ;
478
479 psbuf_req_str(pb, SSH_FXP_RMDIR, reqid, PNPATH(pn_targ));
480 puffs_framebuf_enqueue_cc(pcc, pb);
481
482 rv = psbuf_expect_status(pb);
483 if (rv == 0)
484 nukenode(pn_targ, pcn->pcn_name, 0);
485
486 PSSHFSRETURN(rv);
487 }
488
489 int
490 psshfs_node_symlink(struct puffs_cc *pcc, void *opc, void **newnode,
491 const struct puffs_cn *pcn, const struct vattr *va,
492 const char *link_target)
493 {
494 PSSHFSAUTOVAR(pcc);
495 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
496 struct puffs_node *pn = opc;
497 struct puffs_node *pn_new;
498
499 if (pctx->protover < 3) {
500 rv = EOPNOTSUPP;
501 goto out;
502 }
503
504 pn_new = allocnode(pu, pn, pcn->pcn_name, va);
505 if (!pn_new) {
506 rv = ENOMEM;
507 goto out;
508 }
509
510 /*
511 * XXX: ietf says: source, target. openssh says: ietf who?
512 * Let's go with openssh and build quirk tables later if we care
513 */
514 psbuf_req_str(pb, SSH_FXP_SYMLINK, reqid, link_target);
515 psbuf_put_str(pb, PCNPATH(pcn));
516 puffs_framebuf_enqueue_cc(pcc, pb);
517
518 rv = psbuf_expect_status(pb);
519 if (rv == 0)
520 *newnode = pn_new;
521 else
522 nukenode(pn_new, pcn->pcn_name, 1);
523
524 out:
525 PSSHFSRETURN(rv);
526 }
527
528 int
529 psshfs_node_rename(struct puffs_cc *pcc, void *opc, void *src,
530 const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
531 const struct puffs_cn *pcn_targ)
532 {
533 PSSHFSAUTOVAR(pcc);
534 struct puffs_node *pn_sf = src;
535 struct puffs_node *pn_td = targ_dir, *pn_tf = targ;
536 struct psshfs_node *psn_targdir = pn_td->pn_data;
537
538 if (pctx->protover < 2) {
539 rv = EOPNOTSUPP;
540 goto out;
541 }
542
543 if (pn_tf) {
544 /* XXX: no backend implementation for now, so call directly */
545 rv = psshfs_node_remove(pcc, targ_dir, pn_tf, pcn_targ);
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 puffs_framebuf_enqueue_cc(pcc, 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, pid_t pid)
597 {
598 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
599 struct puffs_node *pn = opc, *pn_next, *pn_root;
600 struct psshfs_node *psn = pn->pn_data;
601
602 /*
603 * don't reclaim if we have file handle issued, otherwise
604 * we can't do fhtonode
605 */
606 if (psn->hasfh)
607 return 0;
608
609 psn->reclaimed = 1;
610 pn_root = puffs_getroot(pu);
611 for (; pn != pn_root; pn = pn_next) {
612 psn = pn->pn_data;
613 if (psn->reclaimed == 0 || psn->childcount != 0)
614 break;
615
616 pn_next = psn->parent;
617 doreclaim(pn);
618 }
619
620 return 0;
621 }
622