subr.c revision 1.11 1 /* $NetBSD: subr.c,v 1.11 2007/03/13 18:00:34 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.11 2007/03/13 18:00:34 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 psshfs_ctx *pctx, struct psbuf *pb, void *arg)
113 {
114 struct readdirattr *rda = arg;
115 struct psshfs_node *psn = rda->psn;
116 struct psshfs_dir *pdir;
117 struct vattr va;
118
119 pdir = lookup(psn->dir, psn->denttot, rda->entryname);
120 if (!pdir)
121 goto out;
122
123 if (psbuf_expect_attrs(pb, &va))
124 goto out;
125
126 if (pdir->entry) {
127 struct psshfs_node *psn_targ;
128 psn_targ = pdir->entry->pn_data;
129
130 puffs_setvattr(&pdir->entry->pn_va, &va);
131 psn_targ->attrread = time(NULL);
132 } else {
133 puffs_setvattr(&pdir->va, &va);
134 pdir->attrread = time(NULL);
135 }
136
137 out:
138 free(rda);
139 psbuf_destroy(pb);
140 }
141
142 static void
143 readdir_getattr(struct psshfs_ctx *pctx, struct psshfs_node *psn,
144 const char *basepath, int idx)
145 {
146 char path[MAXPATHLEN+1];
147 struct psshfs_dir *pdir = psn->dir;
148 struct psbuf *pb;
149 struct readdirattr *rda;
150 const char *entryname = pdir[idx].entryname;
151 uint32_t reqid = NEXTREQ(pctx);
152
153 rda = emalloc(sizeof(struct readdirattr));
154 rda->psn = psn;
155 rda->idx = idx;
156 strlcpy(rda->entryname, entryname, sizeof(rda->entryname));
157
158 strcpy(path, basepath);
159 strcat(path, "/");
160 strlcat(path, entryname, sizeof(path));
161
162 pb = psbuf_make(PSB_OUT);
163 psbuf_req_str(pb, SSH_FXP_LSTAT, reqid, path);
164 pssh_outbuf_enqueue_nocc(pctx, pb, readdir_getattr_resp, rda, reqid);
165 }
166 #endif
167
168 int
169 sftp_readdir(struct puffs_cc *pcc, struct psshfs_ctx *pctx,
170 struct puffs_node *pn)
171 {
172 struct psshfs_node *psn = pn->pn_data;
173 struct psshfs_dir *olddir, *testd;
174 struct psbuf *pb;
175 uint32_t reqid = NEXTREQ(pctx);
176 uint32_t count, dhandlen;
177 char *dhand = NULL;
178 size_t nent;
179 char *longname;
180 int idx, rv;
181
182 assert(pn->pn_va.va_type == VDIR);
183
184 if (psn->dir && (time(NULL) - psn->dentread) < PSSHFS_REFRESHIVAL)
185 return 0;
186
187 puffs_inval_namecache_dir(puffs_cc_getusermount(pcc), pn);
188
189 pb = psbuf_make(PSB_OUT);
190 psbuf_req_str(pb, SSH_FXP_OPENDIR, reqid, PNPATH(pn));
191 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
192
193 puffs_cc_yield(pcc);
194
195 rv = psbuf_expect_handle(pb, &dhand, &dhandlen);
196 if (rv)
197 goto wayout;
198
199 /*
200 * Well, the following is O(n^2), so feel free to improve if it
201 * gets too taxing on your system.
202 */
203 olddir = psn->dir;
204 nent = psn->dentnext;
205
206 /*
207 * note: for the "getattr in batch" to work, this must be before
208 * the attribute-getting. Otherwise times for first entries in
209 * large directories might expire before the directory itself and
210 * result in one-by-one attribute fetching.
211 */
212 psn->dentread = time(NULL);
213
214 psn->dentnext = 0;
215 psn->denttot = 0;
216 psn->dir = NULL;
217 idx = 0;
218
219 for (;;) {
220 reqid = NEXTREQ(pctx);
221 psbuf_recycle(pb, PSB_OUT);
222 psbuf_req_data(pb, SSH_FXP_READDIR, reqid, dhand, dhandlen);
223 pssh_outbuf_enqueue(pctx, pb, pcc, reqid);
224
225 puffs_cc_yield(pcc);
226
227 /* check for EOF */
228 if (pb->type == SSH_FXP_STATUS) {
229 rv = psbuf_expect_status(pb);
230 goto out;
231 }
232 rv = psbuf_expect_name(pb, &count);
233 if (rv)
234 goto out;
235
236 for (; count--; idx++) {
237 if (idx == psn->denttot)
238 allocdirs(psn);
239 if (!psbuf_get_str(pb, &psn->dir[idx].entryname,
240 NULL)) {
241 rv = EPROTO;
242 goto out;
243 }
244 if (!psbuf_get_str(pb, &longname, NULL)) {
245 rv = EPROTO;
246 goto out;
247 }
248 if (!psbuf_get_vattr(pb, &psn->dir[idx].va)) {
249 rv = EPROTO;
250 goto out;
251 }
252 if (sscanf(longname, "%*s%d",
253 &psn->dir[idx].va.va_nlink) != 1) {
254 rv = EPROTO;
255 goto out;
256 }
257 free(longname);
258
259 testd = lookup(olddir, nent, psn->dir[idx].entryname);
260 if (testd) {
261 psn->dir[idx].entry = testd->entry;
262 psn->dir[idx].va = testd->va;
263 } else {
264 psn->dir[idx].entry = NULL;
265 psn->dir[idx].va.va_fileid = pctx->nextino++;
266 }
267 #ifdef SUPERREADDIR
268 /*
269 * XXX: there's a dangling pointer race here if
270 * the server responds to our queries out-of-order.
271 * fixxxme some day
272 */
273 readdir_getattr(pctx, psn, PNPATH(pn), idx);
274 #endif
275 psn->dir[idx].valid = 1;
276 }
277 }
278
279 out:
280 /* XXX: rv */
281 psn->dentnext = idx;
282 freedircache(olddir, nent);
283
284 reqid = NEXTREQ(pctx);
285 psbuf_recycle(pb, PSB_OUT);
286 psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, dhand, dhandlen);
287
288 pssh_outbuf_enqueue_nocc(pctx, pb, NULL, NULL, reqid);
289 free(dhand);
290 return 0;
291
292 wayout:
293 free(dhand);
294 PSSHFSRETURN(rv);
295 }
296
297 struct puffs_node *
298 makenode(struct puffs_usermount *pu, struct puffs_node *parent,
299 struct psshfs_dir *pd, const struct vattr *vap)
300 {
301 struct psshfs_node *psn_parent = parent->pn_data;
302 struct psshfs_node *psn;
303 struct puffs_node *pn;
304
305 psn = emalloc(sizeof(struct psshfs_node));
306 memset(psn, 0, sizeof(struct psshfs_node));
307
308 pn = puffs_pn_new(pu, psn);
309 if (!pn) {
310 free(psn);
311 return NULL;
312 }
313 puffs_setvattr(&pn->pn_va, &pd->va);
314 psn->attrread = pd->attrread;
315 puffs_setvattr(&pn->pn_va, vap);
316
317 pd->entry = pn;
318 psn->parent = parent;
319 psn_parent->childcount++;
320
321 return pn;
322 }
323
324 struct puffs_node *
325 allocnode(struct puffs_usermount *pu, struct puffs_node *parent,
326 const char *entryname, const struct vattr *vap)
327 {
328 struct psshfs_ctx *pctx = pu->pu_privdata;
329 struct psshfs_dir *pd;
330 struct puffs_node *pn;
331
332 pd = direnter(parent, entryname);
333
334 pd->va.va_fileid = pctx->nextino++;
335 if (vap->va_type == VDIR) {
336 pd->va.va_nlink = 2;
337 parent->pn_va.va_nlink++;
338 } else {
339 pd->va.va_nlink = 1;
340 }
341
342 pn = makenode(pu, parent, pd, vap);
343 if (pn)
344 pd->va.va_fileid = pn->pn_va.va_fileid;
345
346 return pn;
347 }
348
349 struct psshfs_dir *
350 direnter(struct puffs_node *parent, const char *entryname)
351 {
352 struct psshfs_node *psn_parent = parent->pn_data;
353 struct psshfs_dir *pd;
354 int i;
355
356 /* create directory entry */
357 if (psn_parent->denttot == psn_parent->dentnext)
358 allocdirs(psn_parent);
359
360 i = psn_parent->dentnext;
361 pd = &psn_parent->dir[i];
362 pd->entryname = estrdup(entryname);
363 pd->valid = 1;
364 pd->attrread = 0;
365 puffs_vattr_null(&pd->va);
366 psn_parent->dentnext++;
367
368 return pd;
369 }
370
371 void
372 doreclaim(struct puffs_node *pn)
373 {
374 struct psshfs_node *psn = pn->pn_data;
375 struct psshfs_node *psn_parent;
376 struct psshfs_dir *dent;
377
378 psn_parent = psn->parent->pn_data;
379 psn_parent->childcount--;
380
381 /*
382 * Null out entry from directory. Do not treat a missing entry
383 * as an invariant error, since the node might be removed from
384 * under us, and we might do a readdir before the reclaim resulting
385 * in no directory entry in the parent directory.
386 */
387 dent = lookup_by_entry(psn_parent->dir, psn_parent->dentnext, pn);
388 if (dent)
389 dent->entry = NULL;
390
391 if (pn->pn_va.va_type == VDIR)
392 freedircache(psn->dir, psn->dentnext);
393
394 puffs_pn_put(pn);
395 }
396
397 void
398 nukenode(struct puffs_node *node, const char *entryname, int reclaim)
399 {
400 struct psshfs_node *psn, *psn_parent;
401 struct psshfs_dir *pd;
402
403 psn = node->pn_data;
404 psn_parent = psn->parent->pn_data;
405 pd = lookup(psn_parent->dir, psn_parent->dentnext, entryname);
406 assert(pd != NULL);
407 pd->valid = 0;
408 free(pd->entryname);
409 pd->entryname = NULL;
410
411 if (node->pn_va.va_type == VDIR)
412 psn->parent->pn_va.va_nlink--;
413
414 if (reclaim)
415 doreclaim(node);
416 }
417