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