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