subr.c revision 1.31 1 /* $NetBSD: subr.c,v 1.31 2007/11/08 16:40:15 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: subr.c,v 1.31 2007/11/08 16:40:15 pooka Exp $");
31 #endif /* !lint */
32
33 #include <assert.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <puffs.h>
37 #include <stdlib.h>
38 #include <util.h>
39
40 #include "psshfs.h"
41 #include "sftp_proto.h"
42
43 static void
44 freedircache(struct psshfs_dir *base, size_t count)
45 {
46 int i;
47
48 for (i = 0; i < count; i++) {
49 free(base[i].entryname);
50 base[i].entryname = NULL;
51 }
52
53 free(base);
54 }
55
56 #define ENTRYCHUNK 16
57 static void
58 allocdirs(struct psshfs_node *psn)
59 {
60 size_t oldtot = psn->denttot;
61
62 psn->denttot += ENTRYCHUNK;
63 psn->dir = erealloc(psn->dir,
64 psn->denttot * sizeof(struct psshfs_dir));
65 memset(psn->dir + oldtot, 0, ENTRYCHUNK * sizeof(struct psshfs_dir));
66
67 psn->da = erealloc(psn->da, psn->denttot * sizeof(struct delayattr));
68 }
69
70 struct psshfs_dir *
71 lookup(struct psshfs_dir *bdir, size_t ndir, const char *name)
72 {
73 struct psshfs_dir *test;
74 int i;
75
76 for (i = 0; i < ndir; i++) {
77 test = &bdir[i];
78 if (test->valid != 1)
79 continue;
80 if (strcmp(test->entryname, name) == 0)
81 return test;
82 }
83
84 return NULL;
85 }
86
87 static struct psshfs_dir *
88 lookup_by_entry(struct psshfs_dir *bdir, size_t ndir, struct puffs_node *entry)
89 {
90 struct psshfs_dir *test;
91 int i;
92
93 for (i = 0; i < ndir; i++) {
94 test = &bdir[i];
95 if (test->valid != 1)
96 continue;
97 if (test->entry == entry)
98 return test;
99 }
100
101 return NULL;
102 }
103
104 struct readdirattr {
105 struct psshfs_node *psn;
106 int idx;
107 char entryname[MAXPATHLEN+1];
108 };
109
110 static void
111 readdir_getattr_resp(struct puffs_usermount *pu,
112 struct puffs_framebuf *pb, void *arg, int error)
113 {
114 struct readdirattr *rda = arg;
115 struct psshfs_node *psn = rda->psn;
116 struct psshfs_node *psn_targ = NULL;
117 struct psshfs_dir *pdir = NULL;
118 struct vattr va;
119
120 /* XXX: this is not enough */
121 if (psn->stat & PSN_RECLAIMED)
122 goto out;
123
124 if (error)
125 goto out;
126
127 pdir = lookup(psn->dir, psn->denttot, rda->entryname);
128 if (!pdir)
129 goto out;
130
131 if (psbuf_expect_attrs(pb, &va))
132 goto out;
133
134 if (pdir->entry) {
135 psn_targ = pdir->entry->pn_data;
136
137 puffs_setvattr(&pdir->entry->pn_va, &va);
138 psn_targ->attrread = time(NULL);
139 } else {
140 puffs_setvattr(&pdir->va, &va);
141 pdir->attrread = time(NULL);
142 }
143
144 out:
145 if (psn_targ) {
146 psn_targ->getattr_pb = NULL;
147 assert(pdir->getattr_pb == NULL);
148 } else if (pdir) {
149 pdir->getattr_pb = NULL;
150 }
151
152 free(rda);
153 puffs_framebuf_destroy(pb);
154 }
155
156 static void
157 readdir_getattr(struct puffs_usermount *pu, struct psshfs_node *psn,
158 const char *basepath, int idx)
159 {
160 char path[MAXPATHLEN+1];
161 struct psshfs_ctx *pctx = puffs_getspecific(pu);
162 struct psshfs_dir *pdir = psn->dir;
163 struct puffs_framebuf *pb;
164 struct readdirattr *rda;
165 const char *entryname = pdir[idx].entryname;
166 uint32_t reqid = NEXTREQ(pctx);
167
168 rda = emalloc(sizeof(struct readdirattr));
169 rda->psn = psn;
170 rda->idx = idx;
171 strlcpy(rda->entryname, entryname, sizeof(rda->entryname));
172
173 strcpy(path, basepath);
174 strcat(path, "/");
175 strlcat(path, entryname, sizeof(path));
176
177 pb = psbuf_makeout();
178 psbuf_req_str(pb, SSH_FXP_LSTAT, reqid, path);
179 psn->da[psn->nextda].pufbuf = pb;
180 psn->da[psn->nextda].rda = rda;
181 psn->nextda++;
182 }
183
184 static void
185 readdir_getattr_send(struct puffs_usermount *pu, struct psshfs_node *psn)
186 {
187 struct psshfs_ctx *pctx = puffs_getspecific(pu);
188 struct psshfs_dir *pdir = psn->dir;
189 struct psshfs_node *psn_targ;
190 struct readdirattr *rda;
191 size_t i;
192 int rv = 0;
193
194 for (i = 0; i < psn->nextda; i++) {
195 rda = psn->da[i].rda;
196 if (pdir[rda->idx].entry) {
197 psn_targ = pdir[rda->idx].entry->pn_data;
198 psn_targ->getattr_pb = psn->da[i].pufbuf;
199 } else {
200 pdir[rda->idx].getattr_pb = psn->da[i].pufbuf;
201 }
202 SENDCB(psn->da[i].pufbuf, readdir_getattr_resp, rda);
203 }
204
205 out:
206 return;
207 }
208
209 int
210 getpathattr(struct puffs_cc *pcc, const char *path, struct vattr *vap)
211 {
212 PSSHFSAUTOVAR(pcc);
213
214 psbuf_req_str(pb, SSH_FXP_LSTAT, reqid, path);
215 GETRESPONSE(pb);
216
217 rv = psbuf_expect_attrs(pb, vap);
218
219 out:
220 PSSHFSRETURN(rv);
221 }
222
223 int
224 getnodeattr(struct puffs_cc *pcc, struct puffs_node *pn)
225 {
226 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
227 struct psshfs_ctx *pctx = puffs_getspecific(pu);
228 struct psshfs_node *psn = pn->pn_data;
229 struct vattr va;
230 int rv, dohardway;
231
232 if (!psn->attrread || REFRESHTIMEOUT(pctx, time(NULL)-psn->attrread)) {
233 dohardway = 1;
234 if (psn->getattr_pb) {
235 rv=puffs_framev_framebuf_ccpromote(psn->getattr_pb,pcc);
236 if (rv == 0) {
237 rv = psbuf_expect_attrs(psn->getattr_pb, &va);
238 puffs_framebuf_destroy(psn->getattr_pb);
239 psn->getattr_pb = NULL;
240 if (rv == 0)
241 dohardway = 0;
242 }
243 }
244
245 if (dohardway) {
246 rv = getpathattr(pcc, PNPATH(pn), &va);
247 if (rv)
248 return rv;
249 }
250
251 /*
252 * Check if the file was modified from below us.
253 * If so, invalidate page cache. This is the only
254 * sensible place we can do this in.
255 */
256 if (psn->attrread)
257 if (pn->pn_va.va_mtime.tv_sec != va.va_mtime.tv_sec)
258 puffs_inval_pagecache_node(pu, pn);
259
260 puffs_setvattr(&pn->pn_va, &va);
261 psn->attrread = time(NULL);
262 }
263
264 return 0;
265 }
266
267 int
268 sftp_readdir(struct puffs_cc *pcc, struct psshfs_ctx *pctx,
269 struct puffs_node *pn)
270 {
271 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
272 struct psshfs_node *psn = pn->pn_data;
273 struct psshfs_dir *olddir, *testd;
274 struct puffs_framebuf *pb;
275 uint32_t reqid = NEXTREQ(pctx);
276 uint32_t count, dhandlen;
277 char *dhand = NULL;
278 size_t nent;
279 char *longname = NULL;
280 int idx, rv;
281
282 assert(pn->pn_va.va_type == VDIR);
283 idx = 0;
284 olddir = psn->dir;
285 nent = psn->dentnext;
286
287 if (psn->dir && !REFRESHTIMEOUT(pctx, time(NULL) - psn->dentread))
288 return 0;
289
290 if ((rv = puffs_inval_namecache_dir(pu, pn)))
291 warn("readdir: dcache inval fail %p", pn);
292
293 pb = psbuf_makeout();
294 psbuf_req_str(pb, SSH_FXP_OPENDIR, reqid, PNPATH(pn));
295 if (puffs_framev_enqueue_cc(pcc, pctx->sshfd, pb, 0) == -1) {
296 rv = errno;
297 goto wayout;
298 }
299 rv = psbuf_expect_handle(pb, &dhand, &dhandlen);
300 if (rv)
301 goto wayout;
302
303 /*
304 * Well, the following is O(n^2), so feel free to improve if it
305 * gets too taxing on your system.
306 */
307
308 /*
309 * note: for the "getattr in batch" to work, this must be before
310 * the attribute-getting. Otherwise times for first entries in
311 * large directories might expire before the directory itself and
312 * result in one-by-one attribute fetching.
313 */
314 psn->dentread = time(NULL);
315
316 psn->dentnext = 0;
317 psn->denttot = 0;
318 psn->dir = NULL;
319 psn->nextda = 0;
320
321 for (;;) {
322 reqid = NEXTREQ(pctx);
323 psbuf_recycleout(pb);
324 psbuf_req_data(pb, SSH_FXP_READDIR, reqid, dhand, dhandlen);
325 GETRESPONSE(pb);
326
327 /* check for EOF */
328 if (psbuf_get_type(pb) == SSH_FXP_STATUS) {
329 rv = psbuf_expect_status(pb);
330 goto out;
331 }
332 rv = psbuf_expect_name(pb, &count);
333 if (rv)
334 goto out;
335
336 for (; count--; idx++) {
337 if (idx == psn->denttot)
338 allocdirs(psn);
339 if ((rv = psbuf_get_str(pb,
340 &psn->dir[idx].entryname, NULL)))
341 goto out;
342 if ((rv = psbuf_get_str(pb, &longname, NULL)) != 0)
343 goto out;
344 if ((rv = psbuf_get_vattr(pb, &psn->dir[idx].va)) != 0)
345 goto out;
346 if (sscanf(longname, "%*s%d",
347 &psn->dir[idx].va.va_nlink) != 1) {
348 rv = EPROTO;
349 goto out;
350 }
351 free(longname);
352 longname = NULL;
353
354 testd = lookup(olddir, nent, psn->dir[idx].entryname);
355 if (testd) {
356 psn->dir[idx].entry = testd->entry;
357 psn->dir[idx].va = testd->va;
358 } else {
359 psn->dir[idx].entry = NULL;
360 psn->dir[idx].va.va_fileid = pctx->nextino++;
361 }
362 /*
363 * XXX: there's a dangling pointer race here if
364 * the server responds to our queries out-of-order.
365 * fixxxme some day
366 */
367 readdir_getattr(puffs_cc_getusermount(pcc),
368 psn, PNPATH(pn), idx);
369
370 psn->dir[idx].valid = 1;
371 }
372 }
373
374 out:
375 /* fire off getattr requests */
376 readdir_getattr_send(pu, psn);
377
378 /* XXX: rv */
379 psn->dentnext = idx;
380 freedircache(olddir, nent);
381
382 reqid = NEXTREQ(pctx);
383 psbuf_recycleout(pb);
384 psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, dhand, dhandlen);
385 puffs_framev_enqueue_justsend(pu, pctx->sshfd, pb, 1, 0);
386 free(dhand);
387 free(longname);
388
389 return rv;
390
391 wayout:
392 free(dhand);
393 PSSHFSRETURN(rv);
394 }
395
396 struct puffs_node *
397 makenode(struct puffs_usermount *pu, struct puffs_node *parent,
398 struct psshfs_dir *pd, const struct vattr *vap)
399 {
400 struct psshfs_node *psn_parent = parent->pn_data;
401 struct psshfs_node *psn;
402 struct puffs_node *pn;
403
404 psn = emalloc(sizeof(struct psshfs_node));
405 memset(psn, 0, sizeof(struct psshfs_node));
406
407 pn = puffs_pn_new(pu, psn);
408 if (!pn) {
409 free(psn);
410 return NULL;
411 }
412 puffs_setvattr(&pn->pn_va, &pd->va);
413 psn->attrread = pd->attrread;
414 puffs_setvattr(&pn->pn_va, vap);
415
416 pd->entry = pn;
417 psn->parent = parent;
418 psn_parent->childcount++;
419
420 TAILQ_INIT(&psn->pw);
421
422 if (pd->getattr_pb) {
423 psn->getattr_pb = pd->getattr_pb;
424 pd->getattr_pb = NULL;
425 }
426
427 return pn;
428 }
429
430 struct puffs_node *
431 allocnode(struct puffs_usermount *pu, struct puffs_node *parent,
432 const char *entryname, const struct vattr *vap)
433 {
434 struct psshfs_ctx *pctx = puffs_getspecific(pu);
435 struct psshfs_dir *pd;
436 struct puffs_node *pn;
437
438 pd = direnter(parent, entryname);
439
440 pd->va.va_fileid = pctx->nextino++;
441 if (vap->va_type == VDIR) {
442 pd->va.va_nlink = 2;
443 parent->pn_va.va_nlink++;
444 } else {
445 pd->va.va_nlink = 1;
446 }
447
448 pn = makenode(pu, parent, pd, vap);
449 if (pn)
450 pd->va.va_fileid = pn->pn_va.va_fileid;
451
452 return pn;
453 }
454
455 struct psshfs_dir *
456 direnter(struct puffs_node *parent, const char *entryname)
457 {
458 struct psshfs_node *psn_parent = parent->pn_data;
459 struct psshfs_dir *pd;
460 int i;
461
462 /* create directory entry */
463 if (psn_parent->denttot == psn_parent->dentnext)
464 allocdirs(psn_parent);
465
466 i = psn_parent->dentnext;
467 pd = &psn_parent->dir[i];
468 pd->entryname = estrdup(entryname);
469 pd->valid = 1;
470 pd->attrread = 0;
471 puffs_vattr_null(&pd->va);
472 psn_parent->dentnext++;
473
474 return pd;
475 }
476
477 void
478 doreclaim(struct puffs_node *pn)
479 {
480 struct psshfs_node *psn = pn->pn_data;
481 struct psshfs_node *psn_parent;
482 struct psshfs_dir *dent;
483
484 psn_parent = psn->parent->pn_data;
485 psn_parent->childcount--;
486
487 /*
488 * Null out entry from directory. Do not treat a missing entry
489 * as an invariant error, since the node might be removed from
490 * under us, and we might do a readdir before the reclaim resulting
491 * in no directory entry in the parent directory.
492 */
493 dent = lookup_by_entry(psn_parent->dir, psn_parent->dentnext, pn);
494 if (dent)
495 dent->entry = NULL;
496
497 if (pn->pn_va.va_type == VDIR) {
498 freedircache(psn->dir, psn->dentnext);
499 psn->denttot = psn->dentnext = 0;
500 free(psn->da);
501 }
502
503 puffs_pn_put(pn);
504 }
505
506 void
507 nukenode(struct puffs_node *node, const char *entryname, int reclaim)
508 {
509 struct psshfs_node *psn, *psn_parent;
510 struct psshfs_dir *pd;
511
512 psn = node->pn_data;
513 psn_parent = psn->parent->pn_data;
514 pd = lookup(psn_parent->dir, psn_parent->dentnext, entryname);
515 assert(pd != NULL);
516 pd->valid = 0;
517 free(pd->entryname);
518 pd->entryname = NULL;
519
520 if (node->pn_va.va_type == VDIR)
521 psn->parent->pn_va.va_nlink--;
522
523 if (reclaim)
524 doreclaim(node);
525 }
526