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