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