| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: ukfs.c,v 1.23 2009/04/06 03:27:39 pooka Exp $ */ | | 1 | /* $NetBSD: ukfs.c,v 1.24 2009/04/26 22:23:01 pooka Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 2007, 2008 Antti Kantee. All Rights Reserved. | | 4 | * Copyright (c) 2007, 2008 Antti Kantee. All Rights Reserved. |
5 | * | | 5 | * |
6 | * Development of this software was supported by the | | 6 | * Development of this software was supported by the |
7 | * Finnish Cultural Foundation. | | 7 | * Finnish Cultural Foundation. |
8 | * | | 8 | * |
9 | * Redistribution and use in source and binary forms, with or without | | 9 | * Redistribution and use in source and binary forms, with or without |
10 | * modification, are permitted provided that the following conditions | | 10 | * modification, are permitted provided that the following conditions |
11 | * are met: | | 11 | * are met: |
12 | * 1. Redistributions of source code must retain the above copyright | | 12 | * 1. Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. | | 13 | * notice, this list of conditions and the following disclaimer. |
14 | * 2. Redistributions in binary form must reproduce the above copyright | | 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| @@ -288,84 +288,161 @@ ukfs_release(struct ukfs *fs, int flags) | | | @@ -288,84 +288,161 @@ ukfs_release(struct ukfs *fs, int flags) |
288 | } | | 288 | } |
289 | free(fs); | | 289 | free(fs); |
290 | } | | 290 | } |
291 | | | 291 | |
292 | #define STDCALL(ukfs, thecall) \ | | 292 | #define STDCALL(ukfs, thecall) \ |
293 | int rv = 0; \ | | 293 | int rv = 0; \ |
294 | \ | | 294 | \ |
295 | precall(ukfs); \ | | 295 | precall(ukfs); \ |
296 | rv = thecall; \ | | 296 | rv = thecall; \ |
297 | postcall(ukfs); \ | | 297 | postcall(ukfs); \ |
298 | return rv; | | 298 | return rv; |
299 | | | 299 | |
300 | int | | 300 | int |
301 | ukfs_getdents(struct ukfs *ukfs, const char *dirname, off_t *off, | | 301 | ukfs_opendir(struct ukfs *ukfs, const char *dirname, struct ukfs_dircookie **c) |
302 | uint8_t *buf, size_t bufsize) | | | |
303 | { | | 302 | { |
304 | struct uio *uio; | | | |
305 | struct vnode *vp; | | 303 | struct vnode *vp; |
306 | size_t resid; | | 304 | int rv; |
307 | kauth_cred_t cred; | | | |
308 | int rv, eofflag; | | | |
309 | | | 305 | |
310 | precall(ukfs); | | 306 | precall(ukfs); |
311 | rv = rump_namei(RUMP_NAMEI_LOOKUP, RUMP_NAMEI_LOCKLEAF, dirname, | | 307 | rv = rump_namei(RUMP_NAMEI_LOOKUP, RUMP_NAMEI_LOCKLEAF, dirname, |
312 | NULL, &vp, NULL); | | 308 | NULL, &vp, NULL); |
313 | postcall(ukfs); | | 309 | postcall(ukfs); |
314 | if (rv) | | 310 | |
315 | goto out; | | 311 | if (rv == 0) { |
316 | | | 312 | RUMP_VOP_UNLOCK(vp, 0); |
| | | 313 | } else { |
| | | 314 | errno = rv; |
| | | 315 | rv = -1; |
| | | 316 | } |
| | | 317 | |
| | | 318 | /*LINTED*/ |
| | | 319 | *c = (struct ukfs_dircookie *)vp; |
| | | 320 | return rv; |
| | | 321 | } |
| | | 322 | |
| | | 323 | static int |
| | | 324 | getmydents(struct vnode *vp, off_t *off, uint8_t *buf, size_t bufsize) |
| | | 325 | { |
| | | 326 | struct uio *uio; |
| | | 327 | size_t resid; |
| | | 328 | int rv, eofflag; |
| | | 329 | kauth_cred_t cred; |
| | | 330 | |
317 | uio = rump_uio_setup(buf, bufsize, *off, RUMPUIO_READ); | | 331 | uio = rump_uio_setup(buf, bufsize, *off, RUMPUIO_READ); |
318 | cred = rump_cred_suserget(); | | 332 | cred = rump_cred_suserget(); |
319 | rv = RUMP_VOP_READDIR(vp, uio, cred, &eofflag, NULL, NULL); | | 333 | rv = RUMP_VOP_READDIR(vp, uio, cred, &eofflag, NULL, NULL); |
320 | rump_cred_suserput(cred); | | 334 | rump_cred_suserput(cred); |
321 | RUMP_VOP_UNLOCK(vp, 0); | | 335 | RUMP_VOP_UNLOCK(vp, 0); |
322 | *off = rump_uio_getoff(uio); | | 336 | *off = rump_uio_getoff(uio); |
323 | resid = rump_uio_free(uio); | | 337 | resid = rump_uio_free(uio); |
324 | rump_vp_rele(vp); | | | |
325 | | | 338 | |
326 | out: | | | |
327 | if (rv) { | | 339 | if (rv) { |
328 | errno = rv; | | 340 | errno = rv; |
329 | return -1; | | 341 | return -1; |
330 | } | | 342 | } |
331 | | | 343 | |
332 | /* LINTED: not totally correct return type, but follows syscall */ | | 344 | /* LINTED: not totally correct return type, but follows syscall */ |
333 | return bufsize - resid; | | 345 | return bufsize - resid; |
334 | } | | 346 | } |
335 | | | 347 | |
| | | 348 | /*ARGSUSED*/ |
| | | 349 | int |
| | | 350 | ukfs_getdents_cookie(struct ukfs *ukfs, struct ukfs_dircookie *c, off_t *off, |
| | | 351 | uint8_t *buf, size_t bufsize) |
| | | 352 | { |
| | | 353 | /*LINTED*/ |
| | | 354 | struct vnode *vp = (struct vnode *)c; |
| | | 355 | |
| | | 356 | RUMP_VOP_LOCK(vp, RUMP_LK_SHARED); |
| | | 357 | return getmydents(vp, off, buf, bufsize); |
| | | 358 | } |
| | | 359 | |
| | | 360 | int |
| | | 361 | ukfs_getdents(struct ukfs *ukfs, const char *dirname, off_t *off, |
| | | 362 | uint8_t *buf, size_t bufsize) |
| | | 363 | { |
| | | 364 | struct vnode *vp; |
| | | 365 | int rv; |
| | | 366 | |
| | | 367 | precall(ukfs); |
| | | 368 | rv = rump_namei(RUMP_NAMEI_LOOKUP, RUMP_NAMEI_LOCKLEAF, dirname, |
| | | 369 | NULL, &vp, NULL); |
| | | 370 | postcall(ukfs); |
| | | 371 | if (rv) { |
| | | 372 | errno = rv; |
| | | 373 | return -1; |
| | | 374 | } |
| | | 375 | |
| | | 376 | rv = getmydents(vp, off, buf, bufsize); |
| | | 377 | rump_vp_rele(vp); |
| | | 378 | return rv; |
| | | 379 | } |
| | | 380 | |
| | | 381 | /*ARGSUSED*/ |
| | | 382 | int |
| | | 383 | ukfs_closedir(struct ukfs *ukfs, struct ukfs_dircookie *c) |
| | | 384 | { |
| | | 385 | |
| | | 386 | /*LINTED*/ |
| | | 387 | rump_vp_rele((struct vnode *)c); |
| | | 388 | return 0; |
| | | 389 | } |
| | | 390 | |
| | | 391 | int |
| | | 392 | ukfs_open(struct ukfs *ukfs, const char *filename, int flags) |
| | | 393 | { |
| | | 394 | int fd; |
| | | 395 | |
| | | 396 | precall(ukfs); |
| | | 397 | fd = rump_sys_open(filename, flags, 0); |
| | | 398 | postcall(ukfs); |
| | | 399 | if (fd == -1) |
| | | 400 | return -1; |
| | | 401 | |
| | | 402 | return fd; |
| | | 403 | } |
| | | 404 | |
336 | ssize_t | | 405 | ssize_t |
337 | ukfs_read(struct ukfs *ukfs, const char *filename, off_t off, | | 406 | ukfs_read(struct ukfs *ukfs, const char *filename, off_t off, |
338 | uint8_t *buf, size_t bufsize) | | 407 | uint8_t *buf, size_t bufsize) |
339 | { | | 408 | { |
340 | int fd; | | 409 | int fd; |
341 | ssize_t xfer = -1; /* XXXgcc */ | | 410 | ssize_t xfer = -1; /* XXXgcc */ |
342 | | | 411 | |
343 | precall(ukfs); | | 412 | precall(ukfs); |
344 | fd = rump_sys_open(filename, RUMP_O_RDONLY, 0); | | 413 | fd = rump_sys_open(filename, RUMP_O_RDONLY, 0); |
345 | if (fd == -1) | | 414 | if (fd == -1) |
346 | goto out; | | 415 | goto out; |
347 | | | 416 | |
348 | xfer = rump_sys_pread(fd, buf, bufsize, 0, off); | | 417 | xfer = rump_sys_pread(fd, buf, bufsize, 0, off); |
349 | rump_sys_close(fd); | | 418 | rump_sys_close(fd); |
350 | | | 419 | |
351 | out: | | 420 | out: |
352 | postcall(ukfs); | | 421 | postcall(ukfs); |
353 | if (fd == -1) { | | 422 | if (fd == -1) { |
354 | return -1; | | 423 | return -1; |
355 | } | | 424 | } |
356 | return xfer; | | 425 | return xfer; |
357 | } | | 426 | } |
358 | | | 427 | |
| | | 428 | /*ARGSUSED*/ |
| | | 429 | ssize_t |
| | | 430 | ukfs_read_fd(struct ukfs *ukfs, int fd, off_t off, uint8_t *buf, size_t buflen) |
| | | 431 | { |
| | | 432 | |
| | | 433 | return rump_sys_pread(fd, buf, buflen, 0, off); |
| | | 434 | } |
| | | 435 | |
359 | ssize_t | | 436 | ssize_t |
360 | ukfs_write(struct ukfs *ukfs, const char *filename, off_t off, | | 437 | ukfs_write(struct ukfs *ukfs, const char *filename, off_t off, |
361 | uint8_t *buf, size_t bufsize) | | 438 | uint8_t *buf, size_t bufsize) |
362 | { | | 439 | { |
363 | int fd; | | 440 | int fd; |
364 | ssize_t xfer = -1; /* XXXgcc */ | | 441 | ssize_t xfer = -1; /* XXXgcc */ |
365 | | | 442 | |
366 | precall(ukfs); | | 443 | precall(ukfs); |
367 | fd = rump_sys_open(filename, RUMP_O_WRONLY, 0); | | 444 | fd = rump_sys_open(filename, RUMP_O_WRONLY, 0); |
368 | if (fd == -1) | | 445 | if (fd == -1) |
369 | goto out; | | 446 | goto out; |
370 | | | 447 | |
371 | /* write and commit */ | | 448 | /* write and commit */ |
| @@ -373,26 +450,49 @@ ukfs_write(struct ukfs *ukfs, const char | | | @@ -373,26 +450,49 @@ ukfs_write(struct ukfs *ukfs, const char |
373 | if (xfer > 0) | | 450 | if (xfer > 0) |
374 | rump_sys_fsync(fd); | | 451 | rump_sys_fsync(fd); |
375 | | | 452 | |
376 | rump_sys_close(fd); | | 453 | rump_sys_close(fd); |
377 | | | 454 | |
378 | out: | | 455 | out: |
379 | postcall(ukfs); | | 456 | postcall(ukfs); |
380 | if (fd == -1) { | | 457 | if (fd == -1) { |
381 | return -1; | | 458 | return -1; |
382 | } | | 459 | } |
383 | return xfer; | | 460 | return xfer; |
384 | } | | 461 | } |
385 | | | 462 | |
| | | 463 | /*ARGSUSED*/ |
| | | 464 | ssize_t |
| | | 465 | ukfs_write_fd(struct ukfs *ukfs, int fd, off_t off, uint8_t *buf, size_t buflen, |
| | | 466 | int dosync) |
| | | 467 | { |
| | | 468 | ssize_t xfer; |
| | | 469 | |
| | | 470 | xfer = rump_sys_pwrite(fd, buf, buflen, 0, off); |
| | | 471 | if (xfer > 0 && dosync) |
| | | 472 | rump_sys_fsync(fd); |
| | | 473 | |
| | | 474 | return xfer; |
| | | 475 | } |
| | | 476 | |
| | | 477 | /*ARGSUSED*/ |
| | | 478 | int |
| | | 479 | ukfs_close(struct ukfs *ukfs, int fd) |
| | | 480 | { |
| | | 481 | |
| | | 482 | rump_sys_close(fd); |
| | | 483 | return 0; |
| | | 484 | } |
| | | 485 | |
386 | int | | 486 | int |
387 | ukfs_create(struct ukfs *ukfs, const char *filename, mode_t mode) | | 487 | ukfs_create(struct ukfs *ukfs, const char *filename, mode_t mode) |
388 | { | | 488 | { |
389 | int fd; | | 489 | int fd; |
390 | | | 490 | |
391 | precall(ukfs); | | 491 | precall(ukfs); |
392 | fd = rump_sys_open(filename, RUMP_O_WRONLY | RUMP_O_CREAT, mode); | | 492 | fd = rump_sys_open(filename, RUMP_O_WRONLY | RUMP_O_CREAT, mode); |
393 | if (fd == -1) | | 493 | if (fd == -1) |
394 | return -1; | | 494 | return -1; |
395 | rump_sys_close(fd); | | 495 | rump_sys_close(fd); |
396 | | | 496 | |
397 | postcall(ukfs); | | 497 | postcall(ukfs); |
398 | return 0; | | 498 | return 0; |