subr.c revision 1.11 1 1.11 pooka /* $NetBSD: subr.c,v 1.11 2007/03/13 18:00:34 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2006 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Redistribution and use in source and binary forms, with or without
7 1.1 pooka * modification, are permitted provided that the following conditions
8 1.1 pooka * are met:
9 1.1 pooka * 1. Redistributions of source code must retain the above copyright
10 1.1 pooka * notice, this list of conditions and the following disclaimer.
11 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer in the
13 1.1 pooka * documentation and/or other materials provided with the distribution.
14 1.1 pooka * 3. The name of the company nor the name of the author may be used to
15 1.1 pooka * endorse or promote products derived from this software without specific
16 1.1 pooka * prior written permission.
17 1.1 pooka *
18 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 pooka * SUCH DAMAGE.
29 1.1 pooka */
30 1.1 pooka
31 1.1 pooka #include <sys/cdefs.h>
32 1.1 pooka #ifndef lint
33 1.11 pooka __RCSID("$NetBSD: subr.c,v 1.11 2007/03/13 18:00:34 pooka Exp $");
34 1.1 pooka #endif /* !lint */
35 1.1 pooka
36 1.1 pooka #include <assert.h>
37 1.1 pooka #include <errno.h>
38 1.1 pooka #include <puffs.h>
39 1.1 pooka #include <stdlib.h>
40 1.1 pooka #include <util.h>
41 1.1 pooka
42 1.1 pooka #include "psshfs.h"
43 1.1 pooka #include "sftp_proto.h"
44 1.1 pooka
45 1.1 pooka static void
46 1.1 pooka freedircache(struct psshfs_dir *base, size_t count)
47 1.1 pooka {
48 1.1 pooka int i;
49 1.1 pooka
50 1.1 pooka for (i = 0; i < count; i++) {
51 1.1 pooka free(base[i].entryname);
52 1.1 pooka base[i].entryname = NULL;
53 1.1 pooka }
54 1.1 pooka
55 1.1 pooka free(base);
56 1.1 pooka }
57 1.1 pooka
58 1.1 pooka #define ENTRYCHUNK 16
59 1.1 pooka static void
60 1.1 pooka allocdirs(struct psshfs_node *psn)
61 1.1 pooka {
62 1.1 pooka size_t oldtot = psn->denttot;
63 1.1 pooka
64 1.1 pooka psn->denttot += ENTRYCHUNK;
65 1.1 pooka psn->dir = erealloc(psn->dir,
66 1.1 pooka psn->denttot * sizeof(struct psshfs_dir));
67 1.1 pooka memset(psn->dir + oldtot, 0, ENTRYCHUNK * sizeof(struct psshfs_dir));
68 1.1 pooka }
69 1.1 pooka
70 1.1 pooka struct psshfs_dir *
71 1.9 pooka lookup(struct psshfs_dir *bdir, size_t ndir, const char *name)
72 1.1 pooka {
73 1.9 pooka struct psshfs_dir *test;
74 1.1 pooka int i;
75 1.1 pooka
76 1.1 pooka for (i = 0; i < ndir; i++) {
77 1.9 pooka test = &bdir[i];
78 1.9 pooka if (test->valid != 1)
79 1.1 pooka continue;
80 1.9 pooka if (strcmp(test->entryname, name) == 0)
81 1.9 pooka return test;
82 1.9 pooka }
83 1.9 pooka
84 1.9 pooka return NULL;
85 1.9 pooka }
86 1.9 pooka
87 1.9 pooka static struct psshfs_dir *
88 1.9 pooka lookup_by_entry(struct psshfs_dir *bdir, size_t ndir, struct puffs_node *entry)
89 1.9 pooka {
90 1.9 pooka struct psshfs_dir *test;
91 1.9 pooka int i;
92 1.9 pooka
93 1.9 pooka for (i = 0; i < ndir; i++) {
94 1.9 pooka test = &bdir[i];
95 1.9 pooka if (test->valid != 1)
96 1.9 pooka continue;
97 1.9 pooka if (test->entry == entry)
98 1.9 pooka return test;
99 1.1 pooka }
100 1.1 pooka
101 1.1 pooka return NULL;
102 1.1 pooka }
103 1.1 pooka
104 1.8 pooka #ifdef SUPERREADDIR
105 1.8 pooka struct readdirattr {
106 1.8 pooka struct psshfs_node *psn;
107 1.8 pooka int idx;
108 1.8 pooka char entryname[MAXPATHLEN+1];
109 1.8 pooka };
110 1.8 pooka
111 1.8 pooka static void
112 1.8 pooka readdir_getattr_resp(struct psshfs_ctx *pctx, struct psbuf *pb, void *arg)
113 1.8 pooka {
114 1.8 pooka struct readdirattr *rda = arg;
115 1.8 pooka struct psshfs_node *psn = rda->psn;
116 1.8 pooka struct psshfs_dir *pdir;
117 1.8 pooka struct vattr va;
118 1.8 pooka
119 1.8 pooka pdir = lookup(psn->dir, psn->denttot, rda->entryname);
120 1.8 pooka if (!pdir)
121 1.8 pooka goto out;
122 1.8 pooka
123 1.8 pooka if (psbuf_expect_attrs(pb, &va))
124 1.8 pooka goto out;
125 1.8 pooka
126 1.8 pooka if (pdir->entry) {
127 1.8 pooka struct psshfs_node *psn_targ;
128 1.8 pooka psn_targ = pdir->entry->pn_data;
129 1.8 pooka
130 1.8 pooka puffs_setvattr(&pdir->entry->pn_va, &va);
131 1.8 pooka psn_targ->attrread = time(NULL);
132 1.8 pooka } else {
133 1.8 pooka puffs_setvattr(&pdir->va, &va);
134 1.8 pooka pdir->attrread = time(NULL);
135 1.8 pooka }
136 1.8 pooka
137 1.8 pooka out:
138 1.8 pooka free(rda);
139 1.8 pooka psbuf_destroy(pb);
140 1.8 pooka }
141 1.8 pooka
142 1.8 pooka static void
143 1.8 pooka readdir_getattr(struct psshfs_ctx *pctx, struct psshfs_node *psn,
144 1.8 pooka const char *basepath, int idx)
145 1.8 pooka {
146 1.8 pooka char path[MAXPATHLEN+1];
147 1.8 pooka struct psshfs_dir *pdir = psn->dir;
148 1.8 pooka struct psbuf *pb;
149 1.8 pooka struct readdirattr *rda;
150 1.8 pooka const char *entryname = pdir[idx].entryname;
151 1.8 pooka uint32_t reqid = NEXTREQ(pctx);
152 1.8 pooka
153 1.8 pooka rda = emalloc(sizeof(struct readdirattr));
154 1.8 pooka rda->psn = psn;
155 1.8 pooka rda->idx = idx;
156 1.8 pooka strlcpy(rda->entryname, entryname, sizeof(rda->entryname));
157 1.8 pooka
158 1.8 pooka strcpy(path, basepath);
159 1.8 pooka strcat(path, "/");
160 1.8 pooka strlcat(path, entryname, sizeof(path));
161 1.8 pooka
162 1.8 pooka pb = psbuf_make(PSB_OUT);
163 1.8 pooka psbuf_req_str(pb, SSH_FXP_LSTAT, reqid, path);
164 1.8 pooka pssh_outbuf_enqueue_nocc(pctx, pb, readdir_getattr_resp, rda, reqid);
165 1.8 pooka }
166 1.8 pooka #endif
167 1.8 pooka
168 1.1 pooka int
169 1.1 pooka sftp_readdir(struct puffs_cc *pcc, struct psshfs_ctx *pctx,
170 1.1 pooka struct puffs_node *pn)
171 1.1 pooka {
172 1.1 pooka struct psshfs_node *psn = pn->pn_data;
173 1.1 pooka struct psshfs_dir *olddir, *testd;
174 1.1 pooka struct psbuf *pb;
175 1.1 pooka uint32_t reqid = NEXTREQ(pctx);
176 1.10 pooka uint32_t count, dhandlen;
177 1.1 pooka char *dhand = NULL;
178 1.10 pooka size_t nent;
179 1.1 pooka char *longname;
180 1.1 pooka int idx, rv;
181 1.1 pooka
182 1.1 pooka assert(pn->pn_va.va_type == VDIR);
183 1.1 pooka
184 1.1 pooka if (psn->dir && (time(NULL) - psn->dentread) < PSSHFS_REFRESHIVAL)
185 1.1 pooka return 0;
186 1.1 pooka
187 1.5 pooka puffs_inval_namecache_dir(puffs_cc_getusermount(pcc), pn);
188 1.5 pooka
189 1.1 pooka pb = psbuf_make(PSB_OUT);
190 1.7 pooka psbuf_req_str(pb, SSH_FXP_OPENDIR, reqid, PNPATH(pn));
191 1.1 pooka pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
192 1.1 pooka
193 1.1 pooka puffs_cc_yield(pcc);
194 1.1 pooka
195 1.1 pooka rv = psbuf_expect_handle(pb, &dhand, &dhandlen);
196 1.1 pooka if (rv)
197 1.1 pooka goto wayout;
198 1.1 pooka
199 1.1 pooka /*
200 1.1 pooka * Well, the following is O(n^2), so feel free to improve if it
201 1.1 pooka * gets too taxing on your system.
202 1.1 pooka */
203 1.1 pooka olddir = psn->dir;
204 1.1 pooka nent = psn->dentnext;
205 1.1 pooka
206 1.8 pooka /*
207 1.8 pooka * note: for the "getattr in batch" to work, this must be before
208 1.8 pooka * the attribute-getting. Otherwise times for first entries in
209 1.8 pooka * large directories might expire before the directory itself and
210 1.8 pooka * result in one-by-one attribute fetching.
211 1.8 pooka */
212 1.8 pooka psn->dentread = time(NULL);
213 1.8 pooka
214 1.1 pooka psn->dentnext = 0;
215 1.1 pooka psn->denttot = 0;
216 1.1 pooka psn->dir = NULL;
217 1.1 pooka idx = 0;
218 1.1 pooka
219 1.1 pooka for (;;) {
220 1.1 pooka reqid = NEXTREQ(pctx);
221 1.1 pooka psbuf_recycle(pb, PSB_OUT);
222 1.1 pooka psbuf_req_data(pb, SSH_FXP_READDIR, reqid, dhand, dhandlen);
223 1.1 pooka pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
224 1.1 pooka
225 1.1 pooka puffs_cc_yield(pcc);
226 1.1 pooka
227 1.1 pooka /* check for EOF */
228 1.1 pooka if (pb->type == SSH_FXP_STATUS) {
229 1.1 pooka rv = psbuf_expect_status(pb);
230 1.1 pooka goto out;
231 1.1 pooka }
232 1.1 pooka rv = psbuf_expect_name(pb, &count);
233 1.1 pooka if (rv)
234 1.1 pooka goto out;
235 1.1 pooka
236 1.1 pooka for (; count--; idx++) {
237 1.1 pooka if (idx == psn->denttot)
238 1.1 pooka allocdirs(psn);
239 1.1 pooka if (!psbuf_get_str(pb, &psn->dir[idx].entryname,
240 1.1 pooka NULL)) {
241 1.1 pooka rv = EPROTO;
242 1.1 pooka goto out;
243 1.1 pooka }
244 1.1 pooka if (!psbuf_get_str(pb, &longname, NULL)) {
245 1.1 pooka rv = EPROTO;
246 1.1 pooka goto out;
247 1.1 pooka }
248 1.1 pooka if (!psbuf_get_vattr(pb, &psn->dir[idx].va)) {
249 1.1 pooka rv = EPROTO;
250 1.1 pooka goto out;
251 1.1 pooka }
252 1.1 pooka if (sscanf(longname, "%*s%d",
253 1.1 pooka &psn->dir[idx].va.va_nlink) != 1) {
254 1.1 pooka rv = EPROTO;
255 1.1 pooka goto out;
256 1.1 pooka }
257 1.1 pooka free(longname);
258 1.1 pooka
259 1.1 pooka testd = lookup(olddir, nent, psn->dir[idx].entryname);
260 1.1 pooka if (testd) {
261 1.1 pooka psn->dir[idx].entry = testd->entry;
262 1.8 pooka psn->dir[idx].va = testd->va;
263 1.1 pooka } else {
264 1.1 pooka psn->dir[idx].entry = NULL;
265 1.1 pooka psn->dir[idx].va.va_fileid = pctx->nextino++;
266 1.1 pooka }
267 1.8 pooka #ifdef SUPERREADDIR
268 1.8 pooka /*
269 1.8 pooka * XXX: there's a dangling pointer race here if
270 1.8 pooka * the server responds to our queries out-of-order.
271 1.8 pooka * fixxxme some day
272 1.8 pooka */
273 1.8 pooka readdir_getattr(pctx, psn, PNPATH(pn), idx);
274 1.8 pooka #endif
275 1.1 pooka psn->dir[idx].valid = 1;
276 1.1 pooka }
277 1.1 pooka }
278 1.1 pooka
279 1.1 pooka out:
280 1.1 pooka /* XXX: rv */
281 1.1 pooka psn->dentnext = idx;
282 1.1 pooka freedircache(olddir, nent);
283 1.1 pooka
284 1.1 pooka reqid = NEXTREQ(pctx);
285 1.1 pooka psbuf_recycle(pb, PSB_OUT);
286 1.1 pooka psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, dhand, dhandlen);
287 1.1 pooka
288 1.11 pooka pssh_outbuf_enqueue_nocc(pctx, pb, NULL, NULL, reqid);
289 1.11 pooka free(dhand);
290 1.11 pooka return 0;
291 1.1 pooka
292 1.1 pooka wayout:
293 1.1 pooka free(dhand);
294 1.11 pooka PSSHFSRETURN(rv);
295 1.1 pooka }
296 1.1 pooka
297 1.1 pooka struct puffs_node *
298 1.1 pooka makenode(struct puffs_usermount *pu, struct puffs_node *parent,
299 1.1 pooka struct psshfs_dir *pd, const struct vattr *vap)
300 1.1 pooka {
301 1.1 pooka struct psshfs_node *psn_parent = parent->pn_data;
302 1.1 pooka struct psshfs_node *psn;
303 1.1 pooka struct puffs_node *pn;
304 1.1 pooka
305 1.1 pooka psn = emalloc(sizeof(struct psshfs_node));
306 1.1 pooka memset(psn, 0, sizeof(struct psshfs_node));
307 1.1 pooka
308 1.1 pooka pn = puffs_pn_new(pu, psn);
309 1.1 pooka if (!pn) {
310 1.1 pooka free(psn);
311 1.1 pooka return NULL;
312 1.1 pooka }
313 1.1 pooka puffs_setvattr(&pn->pn_va, &pd->va);
314 1.8 pooka psn->attrread = pd->attrread;
315 1.1 pooka puffs_setvattr(&pn->pn_va, vap);
316 1.1 pooka
317 1.1 pooka pd->entry = pn;
318 1.1 pooka psn->parent = parent;
319 1.1 pooka psn_parent->childcount++;
320 1.1 pooka
321 1.1 pooka return pn;
322 1.1 pooka }
323 1.1 pooka
324 1.1 pooka struct puffs_node *
325 1.1 pooka allocnode(struct puffs_usermount *pu, struct puffs_node *parent,
326 1.1 pooka const char *entryname, const struct vattr *vap)
327 1.1 pooka {
328 1.1 pooka struct psshfs_ctx *pctx = pu->pu_privdata;
329 1.1 pooka struct psshfs_dir *pd;
330 1.3 pooka struct puffs_node *pn;
331 1.1 pooka
332 1.1 pooka pd = direnter(parent, entryname);
333 1.1 pooka
334 1.1 pooka pd->va.va_fileid = pctx->nextino++;
335 1.2 pooka if (vap->va_type == VDIR) {
336 1.1 pooka pd->va.va_nlink = 2;
337 1.2 pooka parent->pn_va.va_nlink++;
338 1.2 pooka } else {
339 1.1 pooka pd->va.va_nlink = 1;
340 1.2 pooka }
341 1.1 pooka
342 1.3 pooka pn = makenode(pu, parent, pd, vap);
343 1.3 pooka if (pn)
344 1.3 pooka pd->va.va_fileid = pn->pn_va.va_fileid;
345 1.3 pooka
346 1.3 pooka return pn;
347 1.1 pooka }
348 1.1 pooka
349 1.1 pooka struct psshfs_dir *
350 1.1 pooka direnter(struct puffs_node *parent, const char *entryname)
351 1.1 pooka {
352 1.1 pooka struct psshfs_node *psn_parent = parent->pn_data;
353 1.1 pooka struct psshfs_dir *pd;
354 1.1 pooka int i;
355 1.1 pooka
356 1.1 pooka /* create directory entry */
357 1.1 pooka if (psn_parent->denttot == psn_parent->dentnext)
358 1.1 pooka allocdirs(psn_parent);
359 1.1 pooka
360 1.1 pooka i = psn_parent->dentnext;
361 1.1 pooka pd = &psn_parent->dir[i];
362 1.1 pooka pd->entryname = estrdup(entryname);
363 1.1 pooka pd->valid = 1;
364 1.8 pooka pd->attrread = 0;
365 1.4 pooka puffs_vattr_null(&pd->va);
366 1.1 pooka psn_parent->dentnext++;
367 1.1 pooka
368 1.1 pooka return pd;
369 1.1 pooka }
370 1.1 pooka
371 1.1 pooka void
372 1.9 pooka doreclaim(struct puffs_node *pn)
373 1.9 pooka {
374 1.9 pooka struct psshfs_node *psn = pn->pn_data;
375 1.9 pooka struct psshfs_node *psn_parent;
376 1.9 pooka struct psshfs_dir *dent;
377 1.9 pooka
378 1.9 pooka psn_parent = psn->parent->pn_data;
379 1.9 pooka psn_parent->childcount--;
380 1.9 pooka
381 1.11 pooka /*
382 1.11 pooka * Null out entry from directory. Do not treat a missing entry
383 1.11 pooka * as an invariant error, since the node might be removed from
384 1.11 pooka * under us, and we might do a readdir before the reclaim resulting
385 1.11 pooka * in no directory entry in the parent directory.
386 1.11 pooka */
387 1.9 pooka dent = lookup_by_entry(psn_parent->dir, psn_parent->dentnext, pn);
388 1.11 pooka if (dent)
389 1.11 pooka dent->entry = NULL;
390 1.9 pooka
391 1.9 pooka if (pn->pn_va.va_type == VDIR)
392 1.9 pooka freedircache(psn->dir, psn->dentnext);
393 1.9 pooka
394 1.9 pooka puffs_pn_put(pn);
395 1.9 pooka }
396 1.9 pooka
397 1.9 pooka void
398 1.9 pooka nukenode(struct puffs_node *node, const char *entryname, int reclaim)
399 1.1 pooka {
400 1.1 pooka struct psshfs_node *psn, *psn_parent;
401 1.1 pooka struct psshfs_dir *pd;
402 1.1 pooka
403 1.1 pooka psn = node->pn_data;
404 1.1 pooka psn_parent = psn->parent->pn_data;
405 1.1 pooka pd = lookup(psn_parent->dir, psn_parent->dentnext, entryname);
406 1.1 pooka assert(pd != NULL);
407 1.1 pooka pd->valid = 0;
408 1.1 pooka free(pd->entryname);
409 1.1 pooka pd->entryname = NULL;
410 1.1 pooka
411 1.9 pooka if (node->pn_va.va_type == VDIR)
412 1.1 pooka psn->parent->pn_va.va_nlink--;
413 1.6 pooka
414 1.9 pooka if (reclaim)
415 1.9 pooka doreclaim(node);
416 1.1 pooka }
417