subr.c revision 1.26 1 /* $NetBSD: subr.c,v 1.26 2007/08/23 15:19:40 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.26 2007/08/23 15:19:40 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_node *psn = pn->pn_data;
228 struct vattr va;
229 int rv, dohardway;
230
231 if ((time(NULL) - psn->attrread) >= PSSHFS_REFRESHIVAL) {
232 dohardway = 1;
233 if (psn->getattr_pb) {
234 rv=puffs_framev_framebuf_ccpromote(psn->getattr_pb,pcc);
235 if (rv == 0) {
236 rv = psbuf_expect_attrs(psn->getattr_pb, &va);
237 puffs_framebuf_destroy(psn->getattr_pb);
238 psn->getattr_pb = NULL;
239 if (rv == 0)
240 dohardway = 0;
241 }
242 }
243
244 if (dohardway) {
245 rv = getpathattr(pcc, PNPATH(pn), &va);
246 if (rv)
247 return rv;
248 }
249
250 /*
251 * Check if the file was modified from below us.
252 * If so, invalidate page cache. This is the only
253 * sensible place we can do this in.
254 */
255 if (psn->attrread)
256 if (pn->pn_va.va_mtime.tv_sec != va.va_mtime.tv_sec)
257 puffs_inval_pagecache_node(pu, pn);
258
259 puffs_setvattr(&pn->pn_va, &va);
260 psn->attrread = time(NULL);
261 }
262
263 return 0;
264 }
265
266 int
267 sftp_readdir(struct puffs_cc *pcc, struct psshfs_ctx *pctx,
268 struct puffs_node *pn)
269 {
270 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
271 struct psshfs_node *psn = pn->pn_data;
272 struct psshfs_dir *olddir, *testd;
273 struct puffs_framebuf *pb;
274 uint32_t reqid = NEXTREQ(pctx);
275 uint32_t count, dhandlen;
276 char *dhand = NULL;
277 size_t nent;
278 char *longname;
279 int idx, rv;
280
281 assert(pn->pn_va.va_type == VDIR);
282 idx = 0;
283 olddir = psn->dir;
284 nent = psn->dentnext;
285
286 if (psn->dir && (time(NULL) - psn->dentread) < PSSHFS_REFRESHIVAL)
287 return 0;
288
289 if ((rv = puffs_inval_namecache_dir(pu, pn)))
290 warn("readdir: dcache inval fail %p", pn);
291
292 pb = psbuf_makeout();
293 psbuf_req_str(pb, SSH_FXP_OPENDIR, reqid, PNPATH(pn));
294 GETRESPONSE(pb);
295
296 rv = psbuf_expect_handle(pb, &dhand, &dhandlen);
297 if (rv)
298 goto wayout;
299
300 /*
301 * Well, the following is O(n^2), so feel free to improve if it
302 * gets too taxing on your system.
303 */
304
305 /*
306 * note: for the "getattr in batch" to work, this must be before
307 * the attribute-getting. Otherwise times for first entries in
308 * large directories might expire before the directory itself and
309 * result in one-by-one attribute fetching.
310 */
311 psn->dentread = time(NULL);
312
313 psn->dentnext = 0;
314 psn->denttot = 0;
315 psn->dir = NULL;
316 psn->nextda = 0;
317
318 for (;;) {
319 reqid = NEXTREQ(pctx);
320 psbuf_recycleout(pb);
321 psbuf_req_data(pb, SSH_FXP_READDIR, reqid, dhand, dhandlen);
322 GETRESPONSE(pb);
323
324 /* check for EOF */
325 if (psbuf_get_type(pb) == SSH_FXP_STATUS) {
326 rv = psbuf_expect_status(pb);
327 goto out;
328 }
329 rv = psbuf_expect_name(pb, &count);
330 if (rv)
331 goto out;
332
333 for (; count--; idx++) {
334 if (idx == psn->denttot)
335 allocdirs(psn);
336 if ((rv = psbuf_get_str(pb,
337 &psn->dir[idx].entryname, NULL)))
338 goto out;
339 if ((rv = psbuf_get_str(pb, &longname, NULL)))
340 goto out;
341 if ((rv = psbuf_get_vattr(pb, &psn->dir[idx].va))) {
342 free(longname);
343 goto out;
344 }
345 if (sscanf(longname, "%*s%d",
346 &psn->dir[idx].va.va_nlink) != 1) {
347 rv = EPROTO;
348 goto out;
349 }
350 free(longname);
351
352 testd = lookup(olddir, nent, psn->dir[idx].entryname);
353 if (testd) {
354 psn->dir[idx].entry = testd->entry;
355 psn->dir[idx].va = testd->va;
356 } else {
357 psn->dir[idx].entry = NULL;
358 psn->dir[idx].va.va_fileid = pctx->nextino++;
359 }
360 /*
361 * XXX: there's a dangling pointer race here if
362 * the server responds to our queries out-of-order.
363 * fixxxme some day
364 */
365 readdir_getattr(puffs_cc_getusermount(pcc),
366 psn, PNPATH(pn), idx);
367
368 psn->dir[idx].valid = 1;
369 }
370 }
371
372 out:
373 /* fire off getattr requests */
374 readdir_getattr_send(pu, psn);
375
376 /* XXX: rv */
377 psn->dentnext = idx;
378 freedircache(olddir, nent);
379
380 reqid = NEXTREQ(pctx);
381 psbuf_recycleout(pb);
382 psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, dhand, dhandlen);
383 JUSTSEND(pb);
384 free(dhand);
385
386 return rv;
387
388 wayout:
389 free(dhand);
390 PSSHFSRETURN(rv);
391 }
392
393 struct puffs_node *
394 makenode(struct puffs_usermount *pu, struct puffs_node *parent,
395 struct psshfs_dir *pd, const struct vattr *vap)
396 {
397 struct psshfs_node *psn_parent = parent->pn_data;
398 struct psshfs_node *psn;
399 struct puffs_node *pn;
400
401 psn = emalloc(sizeof(struct psshfs_node));
402 memset(psn, 0, sizeof(struct psshfs_node));
403
404 pn = puffs_pn_new(pu, psn);
405 if (!pn) {
406 free(psn);
407 return NULL;
408 }
409 puffs_setvattr(&pn->pn_va, &pd->va);
410 psn->attrread = pd->attrread;
411 puffs_setvattr(&pn->pn_va, vap);
412
413 pd->entry = pn;
414 psn->parent = parent;
415 psn_parent->childcount++;
416
417 LIST_INIT(&psn->dw);
418
419 if (pd->getattr_pb) {
420 psn->getattr_pb = pd->getattr_pb;
421 pd->getattr_pb = NULL;
422 }
423
424 return pn;
425 }
426
427 struct puffs_node *
428 allocnode(struct puffs_usermount *pu, struct puffs_node *parent,
429 const char *entryname, const struct vattr *vap)
430 {
431 struct psshfs_ctx *pctx = puffs_getspecific(pu);
432 struct psshfs_dir *pd;
433 struct puffs_node *pn;
434
435 pd = direnter(parent, entryname);
436
437 pd->va.va_fileid = pctx->nextino++;
438 if (vap->va_type == VDIR) {
439 pd->va.va_nlink = 2;
440 parent->pn_va.va_nlink++;
441 } else {
442 pd->va.va_nlink = 1;
443 }
444
445 pn = makenode(pu, parent, pd, vap);
446 if (pn)
447 pd->va.va_fileid = pn->pn_va.va_fileid;
448
449 return pn;
450 }
451
452 struct psshfs_dir *
453 direnter(struct puffs_node *parent, const char *entryname)
454 {
455 struct psshfs_node *psn_parent = parent->pn_data;
456 struct psshfs_dir *pd;
457 int i;
458
459 /* create directory entry */
460 if (psn_parent->denttot == psn_parent->dentnext)
461 allocdirs(psn_parent);
462
463 i = psn_parent->dentnext;
464 pd = &psn_parent->dir[i];
465 pd->entryname = estrdup(entryname);
466 pd->valid = 1;
467 pd->attrread = 0;
468 puffs_vattr_null(&pd->va);
469 psn_parent->dentnext++;
470
471 return pd;
472 }
473
474 void
475 doreclaim(struct puffs_node *pn)
476 {
477 struct psshfs_node *psn = pn->pn_data;
478 struct psshfs_node *psn_parent;
479 struct psshfs_dir *dent;
480
481 psn_parent = psn->parent->pn_data;
482 psn_parent->childcount--;
483
484 /*
485 * Null out entry from directory. Do not treat a missing entry
486 * as an invariant error, since the node might be removed from
487 * under us, and we might do a readdir before the reclaim resulting
488 * in no directory entry in the parent directory.
489 */
490 dent = lookup_by_entry(psn_parent->dir, psn_parent->dentnext, pn);
491 if (dent)
492 dent->entry = NULL;
493
494 if (pn->pn_va.va_type == VDIR) {
495 freedircache(psn->dir, psn->dentnext);
496 psn->denttot = psn->dentnext = 0;
497 free(psn->da);
498 }
499
500 puffs_pn_put(pn);
501 }
502
503 void
504 nukenode(struct puffs_node *node, const char *entryname, int reclaim)
505 {
506 struct psshfs_node *psn, *psn_parent;
507 struct psshfs_dir *pd;
508
509 psn = node->pn_data;
510 psn_parent = psn->parent->pn_data;
511 pd = lookup(psn_parent->dir, psn_parent->dentnext, entryname);
512 assert(pd != NULL);
513 pd->valid = 0;
514 free(pd->entryname);
515 pd->entryname = NULL;
516
517 if (node->pn_va.va_type == VDIR)
518 psn->parent->pn_va.va_nlink--;
519
520 if (reclaim)
521 doreclaim(node);
522 }
523