1 /* $NetBSD: subr.c,v 1.45 2007/12/13 14:59:00 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 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 #ifndef lint 30 __RCSID("$NetBSD: subr.c,v 1.45 2007/12/13 14:59:00 pooka Exp $"); 31 #endif /* !lint */ 32 33 #include <assert.h> 34 #include <err.h> 35 #include <errno.h> 36 #include <puffs.h> 37 #include <stdlib.h> 38 #include <util.h> 39 40 #include "psshfs.h" 41 #include "sftp_proto.h" 42 43 static void 44 freedircache(struct psshfs_dir *base, size_t count) 45 { 46 int i; 47 48 for (i = 0; i < count; i++) { 49 free(base[i].entryname); 50 base[i].entryname = NULL; 51 } 52 53 free(base); 54 } 55 56 #define ENTRYCHUNK 16 57 static void 58 allocdirs(struct psshfs_node *psn) 59 { 60 size_t oldtot = psn->denttot; 61 62 psn->denttot += ENTRYCHUNK; 63 psn->dir = erealloc(psn->dir, 64 psn->denttot * sizeof(struct psshfs_dir)); 65 memset(psn->dir + oldtot, 0, ENTRYCHUNK * sizeof(struct psshfs_dir)); 66 } 67 68 static void 69 setpnva(struct puffs_usermount *pu, struct puffs_node *pn, 70 const struct vattr *vap) 71 { 72 struct psshfs_node *psn = pn->pn_data; 73 74 /* 75 * Check if the file was modified from below us. 76 * If so, invalidate page cache. This is the only 77 * sensible place we can do this in. 78 */ 79 if (pn->pn_va.va_mtime.tv_sec != PUFFS_VNOVAL) 80 if (pn->pn_va.va_mtime.tv_sec != vap->va_mtime.tv_sec 81 && pn->pn_va.va_type == VREG) 82 puffs_inval_pagecache_node(pu, pn); 83 84 puffs_setvattr(&pn->pn_va, vap); 85 psn->attrread = time(NULL); 86 } 87 88 struct psshfs_dir * 89 lookup(struct psshfs_dir *bdir, size_t ndir, const char *name) 90 { 91 struct psshfs_dir *test; 92 int i; 93 94 for (i = 0; i < ndir; i++) { 95 test = &bdir[i]; 96 if (test->valid != 1) 97 continue; 98 if (strcmp(test->entryname, name) == 0) 99 return test; 100 } 101 102 return NULL; 103 } 104 105 static struct psshfs_dir * 106 lookup_by_entry(struct psshfs_dir *bdir, size_t ndir, struct puffs_node *entry) 107 { 108 struct psshfs_dir *test; 109 int i; 110 111 for (i = 0; i < ndir; i++) { 112 test = &bdir[i]; 113 if (test->valid != 1) 114 continue; 115 if (test->entry == entry) 116 return test; 117 } 118 119 return NULL; 120 } 121 122 123 void 124 closehandles(struct puffs_usermount *pu, struct psshfs_node *psn, int which) 125 { 126 struct psshfs_ctx *pctx = puffs_getspecific(pu); 127 struct puffs_framebuf *pb1, *pb2; 128 uint32_t reqid; 129 130 if (psn->fhand_r && (which & HANDLE_READ)) { 131 assert(psn->lazyopen_r == NULL); 132 133 pb1 = psbuf_makeout(); 134 reqid = NEXTREQ(pctx); 135 psbuf_req_data(pb1, SSH_FXP_CLOSE, reqid, 136 psn->fhand_r, psn->fhand_r_len); 137 puffs_framev_enqueue_justsend(pu, pctx->sshfd, pb1, 1, 0); 138 free(psn->fhand_r); 139 psn->fhand_r = NULL; 140 } 141 142 if (psn->fhand_w && (which & HANDLE_WRITE)) { 143 assert(psn->lazyopen_w == NULL); 144 145 pb2 = psbuf_makeout(); 146 reqid = NEXTREQ(pctx); 147 psbuf_req_data(pb2, SSH_FXP_CLOSE, reqid, 148 psn->fhand_w, psn->fhand_w_len); 149 puffs_framev_enqueue_justsend(pu, pctx->sshfd, pb2, 1, 0); 150 free(psn->fhand_w); 151 psn->fhand_w = NULL; 152 } 153 154 psn->stat |= PSN_HANDLECLOSE; 155 } 156 157 void 158 lazyopen_rresp(struct puffs_usermount *pu, struct puffs_framebuf *pb, 159 void *arg, int error) 160 { 161 struct psshfs_node *psn = arg; 162 163 /* XXX: this is not enough */ 164 if (psn->stat & PSN_RECLAIMED) { 165 error = ENOENT; 166 goto moreout; 167 } 168 if (error) 169 goto out; 170 171 error = psbuf_expect_handle(pb, &psn->fhand_r, &psn->fhand_r_len); 172 173 out: 174 psn->lazyopen_err_r = error; 175 psn->lazyopen_r = NULL; 176 if (error) 177 psn->stat &= ~PSN_DOLAZY_R; 178 if (psn->stat & PSN_HANDLECLOSE && (psn->stat & PSN_LAZYWAIT_R) == 0) 179 closehandles(pu, psn, HANDLE_READ); 180 moreout: 181 puffs_framebuf_destroy(pb); 182 } 183 184 void 185 lazyopen_wresp(struct puffs_usermount *pu, struct puffs_framebuf *pb, 186 void *arg, int error) 187 { 188 struct psshfs_node *psn = arg; 189 190 /* XXX: this is not enough */ 191 if (psn->stat & PSN_RECLAIMED) { 192 error = ENOENT; 193 goto moreout; 194 } 195 if (error) 196 goto out; 197 198 error = psbuf_expect_handle(pb, &psn->fhand_w, &psn->fhand_w_len); 199 200 out: 201 psn->lazyopen_err_w = error; 202 psn->lazyopen_w = NULL; 203 if (error) 204 psn->stat &= ~PSN_DOLAZY_W; 205 if (psn->stat & PSN_HANDLECLOSE && (psn->stat & PSN_LAZYWAIT_W) == 0) 206 closehandles(pu, psn, HANDLE_WRITE); 207 moreout: 208 puffs_framebuf_destroy(pb); 209 } 210 211 struct readdirattr { 212 struct psshfs_node *psn; 213 int idx; 214 char entryname[MAXPATHLEN+1]; 215 }; 216 217 int 218 getpathattr(struct puffs_usermount *pu, const char *path, struct vattr *vap) 219 { 220 PSSHFSAUTOVAR(pu); 221 222 psbuf_req_str(pb, SSH_FXP_LSTAT, reqid, path); 223 GETRESPONSE(pb); 224 225 rv = psbuf_expect_attrs(pb, vap); 226 227 out: 228 PSSHFSRETURN(rv); 229 } 230 231 int 232 getnodeattr(struct puffs_usermount *pu, struct puffs_node *pn) 233 { 234 struct psshfs_ctx *pctx = puffs_getspecific(pu); 235 struct psshfs_node *psn = pn->pn_data; 236 struct vattr va; 237 int rv; 238 239 if (!psn->attrread || REFRESHTIMEOUT(pctx, time(NULL)-psn->attrread)) { 240 rv = getpathattr(pu, PNPATH(pn), &va); 241 if (rv) 242 return rv; 243 244 setpnva(pu, pn, &va); 245 } 246 247 return 0; 248 } 249 250 int 251 sftp_readdir(struct puffs_usermount *pu, struct psshfs_ctx *pctx, 252 struct puffs_node *pn) 253 { 254 struct puffs_cc *pcc = puffs_cc_getcc(pu); 255 struct psshfs_node *psn = pn->pn_data; 256 struct psshfs_dir *olddir, *testd; 257 struct puffs_framebuf *pb; 258 uint32_t reqid = NEXTREQ(pctx); 259 uint32_t count, dhandlen; 260 char *dhand = NULL; 261 size_t nent; 262 char *longname = NULL; 263 int idx, rv; 264 265 assert(pn->pn_va.va_type == VDIR); 266 idx = 0; 267 olddir = psn->dir; 268 nent = psn->dentnext; 269 270 if (psn->dir && psn->dentread 271 && !REFRESHTIMEOUT(pctx, time(NULL) - psn->dentread)) 272 return 0; 273 274 if (psn->dentread) { 275 if ((rv = puffs_inval_namecache_dir(pu, pn))) 276 warn("readdir: dcache inval fail %p", pn); 277 } 278 279 pb = psbuf_makeout(); 280 psbuf_req_str(pb, SSH_FXP_OPENDIR, reqid, PNPATH(pn)); 281 if (puffs_framev_enqueue_cc(pcc, pctx->sshfd, pb, 0) == -1) { 282 rv = errno; 283 goto wayout; 284 } 285 rv = psbuf_expect_handle(pb, &dhand, &dhandlen); 286 if (rv) 287 goto wayout; 288 289 /* 290 * Well, the following is O(n^2), so feel free to improve if it 291 * gets too taxing on your system. 292 */ 293 294 /* 295 * note: for the "getattr in batch" to work, this must be before 296 * the attribute-getting. Otherwise times for first entries in 297 * large directories might expire before the directory itself and 298 * result in one-by-one attribute fetching. 299 */ 300 psn->dentread = time(NULL); 301 302 psn->dentnext = 0; 303 psn->denttot = 0; 304 psn->dir = NULL; 305 306 for (;;) { 307 reqid = NEXTREQ(pctx); 308 psbuf_recycleout(pb); 309 psbuf_req_data(pb, SSH_FXP_READDIR, reqid, dhand, dhandlen); 310 GETRESPONSE(pb); 311 312 /* check for EOF */ 313 if (psbuf_get_type(pb) == SSH_FXP_STATUS) { 314 rv = psbuf_expect_status(pb); 315 goto out; 316 } 317 rv = psbuf_expect_name(pb, &count); 318 if (rv) 319 goto out; 320 321 for (; count--; idx++) { 322 if (idx == psn->denttot) 323 allocdirs(psn); 324 if ((rv = psbuf_get_str(pb, 325 &psn->dir[idx].entryname, NULL))) 326 goto out; 327 if ((rv = psbuf_get_str(pb, &longname, NULL)) != 0) 328 goto out; 329 if ((rv = psbuf_get_vattr(pb, &psn->dir[idx].va)) != 0) 330 goto out; 331 if (sscanf(longname, "%*s%d", 332 &psn->dir[idx].va.va_nlink) != 1) { 333 rv = EPROTO; 334 goto out; 335 } 336 free(longname); 337 longname = NULL; 338 339 /* 340 * Check if we already have a psshfs_dir for the 341 * name we are processing. If so, use the old one. 342 * If not, create a new one 343 */ 344 testd = lookup(olddir, nent, psn->dir[idx].entryname); 345 if (testd) { 346 psn->dir[idx].entry = testd->entry; 347 /* 348 * Has entry. Update attributes to what 349 * we just got from the server. 350 */ 351 if (testd->entry) { 352 setpnva(pu, testd->entry, 353 &psn->dir[idx].va); 354 psn->dir[idx].va.va_fileid 355 = testd->entry->pn_va.va_fileid; 356 357 /* 358 * No entry. This can happen in two cases: 359 * 1) the file was created "behind our back" 360 * on the server 361 * 2) we do two readdirs before we instantiate 362 * the node (or run with -t 0). 363 * 364 * Cache attributes from the server in 365 * case we want to instantiate this node 366 * soon. Also preserve the old inode number 367 * which was given when the dirent was created. 368 */ 369 } else { 370 psn->dir[idx].va.va_fileid 371 = testd->va.va_fileid; 372 testd->va = psn->dir[idx].va; 373 } 374 375 /* No previous entry? Initialize this one. */ 376 } else { 377 psn->dir[idx].entry = NULL; 378 psn->dir[idx].va.va_fileid = pctx->nextino++; 379 } 380 psn->dir[idx].attrread = psn->dentread; 381 psn->dir[idx].valid = 1; 382 } 383 } 384 385 out: 386 /* XXX: rv */ 387 psn->dentnext = idx; 388 freedircache(olddir, nent); 389 390 reqid = NEXTREQ(pctx); 391 psbuf_recycleout(pb); 392 psbuf_req_data(pb, SSH_FXP_CLOSE, reqid, dhand, dhandlen); 393 puffs_framev_enqueue_justsend(pu, pctx->sshfd, pb, 1, 0); 394 free(dhand); 395 free(longname); 396 397 return rv; 398 399 wayout: 400 free(dhand); 401 PSSHFSRETURN(rv); 402 } 403 404 struct puffs_node * 405 makenode(struct puffs_usermount *pu, struct puffs_node *parent, 406 struct psshfs_dir *pd, const struct vattr *vap) 407 { 408 struct psshfs_node *psn_parent = parent->pn_data; 409 struct psshfs_node *psn; 410 struct puffs_node *pn; 411 412 psn = emalloc(sizeof(struct psshfs_node)); 413 memset(psn, 0, sizeof(struct psshfs_node)); 414 415 pn = puffs_pn_new(pu, psn); 416 if (!pn) { 417 free(psn); 418 return NULL; 419 } 420 setpnva(pu, pn, &pd->va); 421 setpnva(pu, pn, vap); 422 psn->attrread = pd->attrread; 423 424 pd->entry = pn; 425 psn->parent = parent; 426 psn_parent->childcount++; 427 428 TAILQ_INIT(&psn->pw); 429 430 return pn; 431 } 432 433 struct puffs_node * 434 allocnode(struct puffs_usermount *pu, struct puffs_node *parent, 435 const char *entryname, const struct vattr *vap) 436 { 437 struct psshfs_ctx *pctx = puffs_getspecific(pu); 438 struct psshfs_dir *pd; 439 struct puffs_node *pn; 440 441 pd = direnter(parent, entryname); 442 443 pd->va.va_fileid = pctx->nextino++; 444 if (vap->va_type == VDIR) { 445 pd->va.va_nlink = 2; 446 parent->pn_va.va_nlink++; 447 } else { 448 pd->va.va_nlink = 1; 449 } 450 451 pn = makenode(pu, parent, pd, vap); 452 if (pn) 453 pd->va.va_fileid = pn->pn_va.va_fileid; 454 455 return pn; 456 } 457 458 struct psshfs_dir * 459 direnter(struct puffs_node *parent, const char *entryname) 460 { 461 struct psshfs_node *psn_parent = parent->pn_data; 462 struct psshfs_dir *pd; 463 int i; 464 465 /* create directory entry */ 466 if (psn_parent->denttot == psn_parent->dentnext) 467 allocdirs(psn_parent); 468 469 i = psn_parent->dentnext; 470 pd = &psn_parent->dir[i]; 471 pd->entryname = estrdup(entryname); 472 pd->valid = 1; 473 pd->attrread = 0; 474 puffs_vattr_null(&pd->va); 475 psn_parent->dentnext++; 476 477 return pd; 478 } 479 480 void 481 doreclaim(struct puffs_node *pn) 482 { 483 struct psshfs_node *psn = pn->pn_data; 484 struct psshfs_node *psn_parent; 485 struct psshfs_dir *dent; 486 487 psn_parent = psn->parent->pn_data; 488 psn_parent->childcount--; 489 490 /* 491 * Null out entry from directory. Do not treat a missing entry 492 * as an invariant error, since the node might be removed from 493 * under us, and we might do a readdir before the reclaim resulting 494 * in no directory entry in the parent directory. 495 */ 496 dent = lookup_by_entry(psn_parent->dir, psn_parent->dentnext, pn); 497 if (dent) 498 dent->entry = NULL; 499 500 if (pn->pn_va.va_type == VDIR) { 501 freedircache(psn->dir, psn->dentnext); 502 psn->denttot = psn->dentnext = 0; 503 } 504 if (psn->symlink) 505 free(psn->symlink); 506 507 puffs_pn_put(pn); 508 } 509 510 void 511 nukenode(struct puffs_node *node, const char *entryname, int reclaim) 512 { 513 struct psshfs_node *psn, *psn_parent; 514 struct psshfs_dir *pd; 515 516 psn = node->pn_data; 517 psn_parent = psn->parent->pn_data; 518 pd = lookup(psn_parent->dir, psn_parent->dentnext, entryname); 519 assert(pd != NULL); 520 pd->valid = 0; 521 free(pd->entryname); 522 pd->entryname = NULL; 523 524 if (node->pn_va.va_type == VDIR) 525 psn->parent->pn_va.va_nlink--; 526 527 if (reclaim) 528 doreclaim(node); 529 } 530