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