subr.c revision 1.17 1 /* $NetBSD: subr.c,v 1.17 2007/05/15 14:17:30 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.17 2007/05/15 14:17:30 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 free(longname);
292 goto out;
293 }
294 if (sscanf(longname, "%*s%d",
295 &psn->dir[idx].va.va_nlink) != 1) {
296 rv = EPROTO;
297 goto out;
298 }
299 free(longname);
300
301 testd = lookup(olddir, nent, psn->dir[idx].entryname);
302 if (testd) {
303 psn->dir[idx].entry = testd->entry;
304 psn->dir[idx].va = testd->va;
305 } else {
306 psn->dir[idx].entry = NULL;
307 psn->dir[idx].va.va_fileid = pctx->nextino++;
308 }
309 #ifdef SUPERREADDIR
310 /*
311 * XXX: there's a dangling pointer race here if
312 * the server responds to our queries out-of-order.
313 * fixxxme some day
314 */
315 rv = readdir_getattr(puffs_cc_getusermount(pcc),
316 psn, PNPATH(pn), idx);
317 if (rv)
318 goto out;
319 #endif
320 psn->dir[idx].valid = 1;
321 }
322 }
323
324 out:
325 /* XXX: rv */
326 psn->dentnext = idx;
327 freedircache(olddir, nent);
328
329 reqid = NEXTREQ(pctx);
330 psbuf_recycleout(pb);
331 psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, dhand, dhandlen);
332
333 JUSTSEND(pb);
334 free(dhand);
335 return rv;
336
337 wayout:
338 free(dhand);
339 PSSHFSRETURN(rv);
340 }
341
342 struct puffs_node *
343 makenode(struct puffs_usermount *pu, struct puffs_node *parent,
344 struct psshfs_dir *pd, const struct vattr *vap)
345 {
346 struct psshfs_node *psn_parent = parent->pn_data;
347 struct psshfs_node *psn;
348 struct puffs_node *pn;
349
350 psn = emalloc(sizeof(struct psshfs_node));
351 memset(psn, 0, sizeof(struct psshfs_node));
352
353 pn = puffs_pn_new(pu, psn);
354 if (!pn) {
355 free(psn);
356 return NULL;
357 }
358 puffs_setvattr(&pn->pn_va, &pd->va);
359 psn->attrread = pd->attrread;
360 puffs_setvattr(&pn->pn_va, vap);
361
362 pd->entry = pn;
363 psn->parent = parent;
364 psn_parent->childcount++;
365
366 return pn;
367 }
368
369 struct puffs_node *
370 allocnode(struct puffs_usermount *pu, struct puffs_node *parent,
371 const char *entryname, const struct vattr *vap)
372 {
373 struct psshfs_ctx *pctx = puffs_getspecific(pu);
374 struct psshfs_dir *pd;
375 struct puffs_node *pn;
376
377 pd = direnter(parent, entryname);
378
379 pd->va.va_fileid = pctx->nextino++;
380 if (vap->va_type == VDIR) {
381 pd->va.va_nlink = 2;
382 parent->pn_va.va_nlink++;
383 } else {
384 pd->va.va_nlink = 1;
385 }
386
387 pn = makenode(pu, parent, pd, vap);
388 if (pn)
389 pd->va.va_fileid = pn->pn_va.va_fileid;
390
391 return pn;
392 }
393
394 struct psshfs_dir *
395 direnter(struct puffs_node *parent, const char *entryname)
396 {
397 struct psshfs_node *psn_parent = parent->pn_data;
398 struct psshfs_dir *pd;
399 int i;
400
401 /* create directory entry */
402 if (psn_parent->denttot == psn_parent->dentnext)
403 allocdirs(psn_parent);
404
405 i = psn_parent->dentnext;
406 pd = &psn_parent->dir[i];
407 pd->entryname = estrdup(entryname);
408 pd->valid = 1;
409 pd->attrread = 0;
410 puffs_vattr_null(&pd->va);
411 psn_parent->dentnext++;
412
413 return pd;
414 }
415
416 void
417 doreclaim(struct puffs_node *pn)
418 {
419 struct psshfs_node *psn = pn->pn_data;
420 struct psshfs_node *psn_parent;
421 struct psshfs_dir *dent;
422
423 psn_parent = psn->parent->pn_data;
424 psn_parent->childcount--;
425
426 /*
427 * Null out entry from directory. Do not treat a missing entry
428 * as an invariant error, since the node might be removed from
429 * under us, and we might do a readdir before the reclaim resulting
430 * in no directory entry in the parent directory.
431 */
432 dent = lookup_by_entry(psn_parent->dir, psn_parent->dentnext, pn);
433 if (dent)
434 dent->entry = NULL;
435
436 if (pn->pn_va.va_type == VDIR)
437 freedircache(psn->dir, psn->dentnext);
438
439 puffs_pn_put(pn);
440 }
441
442 void
443 nukenode(struct puffs_node *node, const char *entryname, int reclaim)
444 {
445 struct psshfs_node *psn, *psn_parent;
446 struct psshfs_dir *pd;
447
448 psn = node->pn_data;
449 psn_parent = psn->parent->pn_data;
450 pd = lookup(psn_parent->dir, psn_parent->dentnext, entryname);
451 assert(pd != NULL);
452 pd->valid = 0;
453 free(pd->entryname);
454 pd->entryname = NULL;
455
456 if (node->pn_va.va_type == VDIR)
457 psn->parent->pn_va.va_nlink--;
458
459 if (reclaim)
460 doreclaim(node);
461 }
462