node.c revision 1.25 1 /* $NetBSD: node.c,v 1.25 2007/05/07 17:20:58 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.25 2007/05/07 17:20:58 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_open(struct puffs_cc *pcc, void *opc, int mode,
207 const struct puffs_cred *pcr, pid_t pid)
208 {
209 PSSHFSAUTOVAR(pcc);
210 struct vattr va;
211 struct puffs_node *pn = opc;
212 struct psshfs_node *psn = pn->pn_data;
213
214 if (pn->pn_va.va_type == VDIR)
215 goto out;
216
217 puffs_setback(pcc, PUFFS_SETBACK_INACT_N1);
218 puffs_vattr_null(&va);
219 if (mode & FREAD && psn->fhand_r == NULL) {
220 psbuf_req_str(pb, SSH_FXP_OPEN, reqid, PNPATH(pn));
221 psbuf_put_4(pb, SSH_FXF_READ);
222 psbuf_put_vattr(pb, &va);
223 puffs_framebuf_enqueue_cc(pcc, pb);
224
225 rv = psbuf_expect_handle(pb, &psn->fhand_r, &psn->fhand_r_len);
226 if (rv)
227 goto out;
228 psbuf_recycleout(pb);
229 }
230 if (mode & FWRITE && psn->fhand_w == NULL) {
231 psbuf_req_str(pb, SSH_FXP_OPEN, reqid, PNPATH(pn));
232 psbuf_put_4(pb, SSH_FXF_WRITE);
233 psbuf_put_vattr(pb, &va);
234 puffs_framebuf_enqueue_cc(pcc, pb);
235
236 rv = psbuf_expect_handle(pb, &psn->fhand_w, &psn->fhand_w_len);
237 if (rv)
238 goto out;
239 }
240
241 out:
242 PSSHFSRETURN(rv);
243 }
244
245 int
246 psshfs_node_inactive(struct puffs_cc *pcc, void *opc, pid_t pid, int *refcount)
247 {
248 struct psshfs_ctx *pctx = puffs_cc_getspecific(pcc);
249 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
250 struct puffs_node *pn = opc;
251 struct psshfs_node *psn = pn->pn_data;
252 uint32_t reqid = NEXTREQ(pctx);
253 struct puffs_framebuf *pb1, *pb2;
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 puffs_framebuf_enqueue_justsend(pu, pb1, 1);
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 puffs_framebuf_enqueue_justsend(pu, pb2, 1);
268 free(psn->fhand_w);
269 psn->fhand_w = NULL;
270 }
271
272 *refcount = 1;
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 err;
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 puffs_framebuf_enqueue_cc(pcc, pb);
328
329 rv = psbuf_do_data(pb, buf, &readlen);
330 if (rv == 0)
331 *resid -= readlen;
332
333 err:
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 err;
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 puffs_framebuf_enqueue_cc(pcc, 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 err:
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 puffs_framebuf_enqueue_cc(pcc, 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 puffs_framebuf_enqueue_cc(pcc, pb);
419
420 rv = psbuf_expect_status(pb);
421
422 if (rv == 0)
423 nukenode(pn_targ, pcn->pcn_name, 0);
424
425 out:
426 PSSHFSRETURN(rv);
427 }
428
429 int
430 psshfs_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
431 const struct puffs_cn *pcn, const struct vattr *va)
432 {
433 PSSHFSAUTOVAR(pcc);
434 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
435 struct puffs_node *pn = opc;
436 struct puffs_node *pn_new;
437
438 pn_new = allocnode(pu, pn, pcn->pcn_name, va);
439 if (!pn_new) {
440 rv = ENOMEM;
441 goto out;
442 }
443
444 psbuf_req_str(pb, SSH_FXP_MKDIR, reqid, PCNPATH(pcn));
445 psbuf_put_vattr(pb, va);
446 puffs_framebuf_enqueue_cc(pcc, pb);
447
448 rv = psbuf_expect_status(pb);
449
450 if (rv == 0)
451 *newnode = pn_new;
452 else
453 nukenode(pn_new, pcn->pcn_name, 1);
454
455 out:
456 PSSHFSRETURN(rv);
457 }
458
459 int
460 psshfs_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
461 const struct puffs_cn *pcn)
462 {
463 PSSHFSAUTOVAR(pcc);
464 struct puffs_node *pn_targ = targ;
465
466 psbuf_req_str(pb, SSH_FXP_RMDIR, reqid, PNPATH(pn_targ));
467 puffs_framebuf_enqueue_cc(pcc, pb);
468
469 rv = psbuf_expect_status(pb);
470 if (rv == 0)
471 nukenode(pn_targ, pcn->pcn_name, 0);
472
473 PSSHFSRETURN(rv);
474 }
475
476 int
477 psshfs_node_symlink(struct puffs_cc *pcc, void *opc, void **newnode,
478 const struct puffs_cn *pcn, const struct vattr *va,
479 const char *link_target)
480 {
481 PSSHFSAUTOVAR(pcc);
482 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
483 struct puffs_node *pn = opc;
484 struct puffs_node *pn_new;
485
486 if (pctx->protover < 3) {
487 rv = EOPNOTSUPP;
488 goto out;
489 }
490
491 pn_new = allocnode(pu, pn, pcn->pcn_name, va);
492 if (!pn_new) {
493 rv = ENOMEM;
494 goto out;
495 }
496
497 /*
498 * XXX: ietf says: source, target. openssh says: ietf who?
499 * Let's go with openssh and build quirk tables later if we care
500 */
501 psbuf_req_str(pb, SSH_FXP_SYMLINK, reqid, link_target);
502 psbuf_put_str(pb, PCNPATH(pcn));
503 puffs_framebuf_enqueue_cc(pcc, pb);
504
505 rv = psbuf_expect_status(pb);
506 if (rv == 0)
507 *newnode = pn_new;
508 else
509 nukenode(pn_new, pcn->pcn_name, 1);
510
511 out:
512 PSSHFSRETURN(rv);
513 }
514
515 int
516 psshfs_node_rename(struct puffs_cc *pcc, void *opc, void *src,
517 const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
518 const struct puffs_cn *pcn_targ)
519 {
520 PSSHFSAUTOVAR(pcc);
521 struct puffs_node *pn_sf = src;
522 struct puffs_node *pn_td = targ_dir, *pn_tf = targ;
523 struct psshfs_node *psn_targdir = pn_td->pn_data;
524
525 if (pctx->protover < 2) {
526 rv = EOPNOTSUPP;
527 goto out;
528 }
529
530 if (pn_tf) {
531 /* XXX: no backend implementation for now, so call directly */
532 rv = psshfs_node_remove(pcc, targ_dir, pn_tf, pcn_targ);
533 if (rv)
534 goto out;
535 }
536
537 psbuf_req_str(pb, SSH_FXP_RENAME, reqid, PCNPATH(pcn_src));
538 psbuf_put_str(pb, PCNPATH(pcn_targ));
539 puffs_framebuf_enqueue_cc(pcc, pb);
540
541 rv = psbuf_expect_status(pb);
542 if (rv == 0) {
543 struct psshfs_dir *pd;
544
545 /*
546 * XXX: interfaces didn't quite work with rename..
547 * the song remains the same. go figure .. ;)
548 */
549 nukenode(pn_sf, pcn_src->pcn_name, 0);
550 pd = direnter(pn_td, pcn_targ->pcn_name);
551 pd->entry = pn_sf;
552 puffs_setvattr(&pd->va, &pn_sf->pn_va);
553
554 if (opc != targ_dir) {
555 psn_targdir->childcount++;
556 if (pn_sf->pn_va.va_type == VDIR)
557 pn_td->pn_va.va_nlink++;
558 }
559 }
560
561 out:
562 PSSHFSRETURN(rv);
563 }
564
565 /*
566 * So this file system happened to be written in such a way that
567 * lookup for ".." is hard if we lose the in-memory node. We'd
568 * need to recreate the entire directory structure from the root
569 * node up to the ".." node we're looking up.
570 *
571 * And since our entire fs structure is purely fictional (i.e. it's
572 * only in-memory, not fetchable from the server), the easiest way
573 * to deal with it is to not allow nodes with children to be
574 * reclaimed.
575 *
576 * If a node with children is being attempted to be reclaimed, we
577 * just mark it "reclaimed" but leave it as is until all its children
578 * have been reclaimed. If a lookup for that node is done meanwhile,
579 * it will be found by lookup() and we just remove the "reclaimed"
580 * bit.
581 */
582 int
583 psshfs_node_reclaim(struct puffs_cc *pcc, void *opc, pid_t pid)
584 {
585 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
586 struct puffs_node *pn = opc, *pn_next, *pn_root;
587 struct psshfs_node *psn = pn->pn_data;
588
589 /*
590 * don't reclaim if we have file handle issued, otherwise
591 * we can't do fhtonode
592 */
593 if (psn->hasfh)
594 return 0;
595
596 psn->reclaimed = 1;
597 pn_root = puffs_getroot(pu);
598 for (; pn != pn_root; pn = pn_next) {
599 psn = pn->pn_data;
600 if (psn->reclaimed == 0 || psn->childcount != 0)
601 break;
602
603 pn_next = psn->parent;
604 doreclaim(pn);
605 }
606
607 return 0;
608 }
609