1 /* $NetBSD: sysvbfs_vnops.c,v 1.60 2016/08/20 12:37:08 hannken Exp $ */ 2 3 /*- 4 * Copyright (c) 2004 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by UCHIYAMA Yasushi. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: sysvbfs_vnops.c,v 1.60 2016/08/20 12:37:08 hannken Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/kernel.h> 37 #include <sys/resource.h> 38 #include <sys/vnode.h> 39 #include <sys/namei.h> 40 #include <sys/dirent.h> 41 #include <sys/malloc.h> 42 #include <sys/lockf.h> 43 #include <sys/unistd.h> 44 #include <sys/fcntl.h> 45 #include <sys/kauth.h> 46 #include <sys/buf.h> 47 48 #include <miscfs/genfs/genfs.h> 49 50 #include <fs/sysvbfs/sysvbfs.h> 51 #include <fs/sysvbfs/bfs.h> 52 53 #ifdef SYSVBFS_VNOPS_DEBUG 54 #define DPRINTF(fmt, args...) printf(fmt, ##args) 55 #else 56 #define DPRINTF(arg...) ((void)0) 57 #endif 58 #define ROUND_SECTOR(x) (((x) + 511) & ~511) 59 60 MALLOC_JUSTDEFINE(M_SYSVBFS_VNODE, "sysvbfs vnode", "sysvbfs vnode structures"); 61 MALLOC_DECLARE(M_BFS); 62 63 static void sysvbfs_file_setsize(struct vnode *, size_t); 64 65 int 66 sysvbfs_lookup(void *arg) 67 { 68 struct vop_lookup_v2_args /* { 69 struct vnode *a_dvp; 70 struct vnode **a_vpp; 71 struct componentname *a_cnp; 72 } */ *a = arg; 73 struct vnode *v = a->a_dvp; 74 struct sysvbfs_node *bnode = v->v_data; 75 struct bfs *bfs = bnode->bmp->bfs; /* my filesystem */ 76 struct vnode *vpp = NULL; 77 struct bfs_dirent *dirent = NULL; 78 struct componentname *cnp = a->a_cnp; 79 int nameiop = cnp->cn_nameiop; 80 const char *name = cnp->cn_nameptr; 81 int namelen = cnp->cn_namelen; 82 int error; 83 84 DPRINTF("%s: %s op=%d %d\n", __func__, name, nameiop, 85 cnp->cn_flags); 86 87 *a->a_vpp = NULL; 88 89 KASSERT((cnp->cn_flags & ISDOTDOT) == 0); 90 91 if ((error = VOP_ACCESS(a->a_dvp, VEXEC, cnp->cn_cred)) != 0) { 92 return error; /* directory permission. */ 93 } 94 95 /* Deny last component write operation on a read-only mount */ 96 if ((cnp->cn_flags & ISLASTCN) && (v->v_mount->mnt_flag & MNT_RDONLY) && 97 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 98 return EROFS; 99 100 if (namelen == 1 && name[0] == '.') { /* "." */ 101 vref(v); 102 *a->a_vpp = v; 103 } else { /* Regular file */ 104 if (!bfs_dirent_lookup_by_name(bfs, cnp->cn_nameptr, 105 &dirent)) { 106 if (nameiop != CREATE && nameiop != RENAME) { 107 DPRINTF("%s: no such a file. (1)\n", 108 __func__); 109 return ENOENT; 110 } 111 if ((error = VOP_ACCESS(v, VWRITE, cnp->cn_cred)) != 0) 112 return error; 113 return EJUSTRETURN; 114 } 115 116 /* Allocate v-node */ 117 if ((error = sysvbfs_vget(v->v_mount, dirent->inode, &vpp)) != 0) { 118 DPRINTF("%s: can't get vnode.\n", __func__); 119 return error; 120 } 121 VOP_UNLOCK(vpp); 122 *a->a_vpp = vpp; 123 } 124 125 return 0; 126 } 127 128 int 129 sysvbfs_create(void *arg) 130 { 131 struct vop_create_v3_args /* { 132 struct vnode *a_dvp; 133 struct vnode **a_vpp; 134 struct componentname *a_cnp; 135 struct vattr *a_vap; 136 } */ *a = arg; 137 struct sysvbfs_node *bnode = a->a_dvp->v_data; 138 struct sysvbfs_mount *bmp = bnode->bmp; 139 struct bfs *bfs = bmp->bfs; 140 struct mount *mp = bmp->mountp; 141 struct bfs_dirent *dirent; 142 struct bfs_fileattr attr; 143 struct vattr *va = a->a_vap; 144 kauth_cred_t cr = a->a_cnp->cn_cred; 145 int err = 0; 146 147 DPRINTF("%s: %s\n", __func__, a->a_cnp->cn_nameptr); 148 KDASSERT(a->a_vap->va_type == VREG); 149 attr.uid = kauth_cred_geteuid(cr); 150 attr.gid = kauth_cred_getegid(cr); 151 attr.mode = va->va_mode; 152 153 if ((err = bfs_file_create(bfs, a->a_cnp->cn_nameptr, 0, 0, &attr)) 154 != 0) { 155 DPRINTF("%s: bfs_file_create failed.\n", __func__); 156 return err; 157 } 158 159 if (!bfs_dirent_lookup_by_name(bfs, a->a_cnp->cn_nameptr, &dirent)) 160 panic("no dirent for created file."); 161 162 if ((err = sysvbfs_vget(mp, dirent->inode, a->a_vpp)) != 0) { 163 DPRINTF("%s: sysvbfs_vget failed.\n", __func__); 164 return err; 165 } 166 bnode = (*a->a_vpp)->v_data; 167 bnode->update_ctime = true; 168 bnode->update_mtime = true; 169 bnode->update_atime = true; 170 VOP_UNLOCK(*a->a_vpp); 171 172 return err; 173 } 174 175 int 176 sysvbfs_open(void *arg) 177 { 178 struct vop_open_args /* { 179 struct vnode *a_vp; 180 int a_mode; 181 kauth_cred_t a_cred; 182 } */ *a = arg; 183 struct vnode *v = a->a_vp; 184 struct sysvbfs_node *bnode = v->v_data; 185 struct bfs_inode *inode = bnode->inode; 186 187 DPRINTF("%s:\n", __func__); 188 KDASSERT(v->v_type == VREG || v->v_type == VDIR); 189 190 bnode->update_atime = true; 191 if ((a->a_mode & FWRITE) && !(a->a_mode & O_APPEND)) { 192 bnode->size = 0; 193 } else { 194 bnode->size = bfs_file_size(inode); 195 } 196 bnode->data_block = inode->start_sector; 197 198 return 0; 199 } 200 201 int 202 sysvbfs_close(void *arg) 203 { 204 struct vop_close_args /* { 205 struct vnodeop_desc *a_desc; 206 struct vnode *a_vp; 207 int a_fflag; 208 kauth_cred_t a_cred; 209 } */ *a = arg; 210 struct vnode *v = a->a_vp; 211 struct sysvbfs_node *bnode = v->v_data; 212 struct bfs_fileattr attr; 213 214 DPRINTF("%s:\n", __func__); 215 216 if (v->v_mount->mnt_flag & MNT_RDONLY) 217 goto out; 218 219 uvm_vnp_setsize(v, bnode->size); 220 221 memset(&attr, 0xff, sizeof attr); /* Set VNOVAL all */ 222 if (bnode->update_atime) 223 attr.atime = time_second; 224 if (bnode->update_ctime) 225 attr.ctime = time_second; 226 if (bnode->update_mtime) 227 attr.mtime = time_second; 228 bfs_inode_set_attr(bnode->bmp->bfs, bnode->inode, &attr); 229 230 VOP_FSYNC(a->a_vp, a->a_cred, FSYNC_WAIT, 0, 0); 231 232 out: 233 return 0; 234 } 235 236 static int 237 sysvbfs_check_possible(struct vnode *vp, struct sysvbfs_node *bnode, 238 mode_t mode) 239 { 240 241 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) 242 return EROFS; 243 244 return 0; 245 } 246 247 static int 248 sysvbfs_check_permitted(struct vnode *vp, struct sysvbfs_node *bnode, 249 mode_t mode, kauth_cred_t cred) 250 { 251 struct bfs_fileattr *attr = &bnode->inode->attr; 252 253 return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode, 254 vp->v_type, attr->mode), vp, NULL, genfs_can_access(vp->v_type, 255 attr->mode, attr->uid, attr->gid, mode, cred)); 256 } 257 258 int 259 sysvbfs_access(void *arg) 260 { 261 struct vop_access_args /* { 262 struct vnode *a_vp; 263 int a_mode; 264 kauth_cred_t a_cred; 265 } */ *ap = arg; 266 struct vnode *vp = ap->a_vp; 267 struct sysvbfs_node *bnode = vp->v_data; 268 int error; 269 270 DPRINTF("%s:\n", __func__); 271 272 error = sysvbfs_check_possible(vp, bnode, ap->a_mode); 273 if (error) 274 return error; 275 276 error = sysvbfs_check_permitted(vp, bnode, ap->a_mode, ap->a_cred); 277 278 return error; 279 } 280 281 int 282 sysvbfs_getattr(void *v) 283 { 284 struct vop_getattr_args /* { 285 struct vnode *a_vp; 286 struct vattr *a_vap; 287 kauth_cred_t a_cred; 288 } */ *ap = v; 289 struct vnode *vp = ap->a_vp; 290 struct sysvbfs_node *bnode = vp->v_data; 291 struct bfs_inode *inode = bnode->inode; 292 struct bfs_fileattr *attr = &inode->attr; 293 struct sysvbfs_mount *bmp = bnode->bmp; 294 struct vattr *vap = ap->a_vap; 295 296 DPRINTF("%s:\n", __func__); 297 298 vap->va_type = vp->v_type; 299 vap->va_mode = attr->mode; 300 vap->va_nlink = attr->nlink; 301 vap->va_uid = attr->uid; 302 vap->va_gid = attr->gid; 303 vap->va_fsid = bmp->devvp->v_rdev; 304 vap->va_fileid = inode->number; 305 vap->va_size = bfs_file_size(inode); 306 vap->va_blocksize = BFS_BSIZE; 307 vap->va_atime.tv_sec = attr->atime; 308 vap->va_mtime.tv_sec = attr->mtime; 309 vap->va_ctime.tv_sec = attr->ctime; 310 vap->va_birthtime.tv_sec = 0; 311 vap->va_gen = 1; 312 vap->va_flags = 0; 313 vap->va_rdev = 0; /* No device file */ 314 vap->va_bytes = vap->va_size; 315 vap->va_filerev = 0; 316 vap->va_vaflags = 0; 317 318 return 0; 319 } 320 321 int 322 sysvbfs_setattr(void *arg) 323 { 324 struct vop_setattr_args /* { 325 struct vnode *a_vp; 326 struct vattr *a_vap; 327 kauth_cred_t a_cred; 328 struct proc *p; 329 } */ *ap = arg; 330 struct vnode *vp = ap->a_vp; 331 struct vattr *vap = ap->a_vap; 332 struct sysvbfs_node *bnode = vp->v_data; 333 struct bfs_inode *inode = bnode->inode; 334 struct bfs_fileattr *attr = &inode->attr; 335 struct bfs *bfs = bnode->bmp->bfs; 336 kauth_cred_t cred = ap->a_cred; 337 int error; 338 339 DPRINTF("%s:\n", __func__); 340 if (vp->v_mount->mnt_flag & MNT_RDONLY) 341 return EROFS; 342 343 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || 344 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || 345 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || 346 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) 347 return EINVAL; 348 349 if (vap->va_flags != VNOVAL) 350 return EOPNOTSUPP; 351 352 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) { 353 uid_t uid = 354 (vap->va_uid != (uid_t)VNOVAL) ? vap->va_uid : attr->uid; 355 gid_t gid = 356 (vap->va_gid != (gid_t)VNOVAL) ? vap->va_gid : attr->gid; 357 error = kauth_authorize_vnode(cred, 358 KAUTH_VNODE_CHANGE_OWNERSHIP, vp, NULL, 359 genfs_can_chown(cred, attr->uid, attr->gid, uid, gid)); 360 if (error) 361 return error; 362 attr->uid = uid; 363 attr->gid = gid; 364 } 365 366 if (vap->va_size != VNOVAL) 367 switch (vp->v_type) { 368 case VDIR: 369 return EISDIR; 370 case VCHR: 371 case VBLK: 372 case VFIFO: 373 break; 374 case VREG: 375 if (vp->v_mount->mnt_flag & MNT_RDONLY) 376 return EROFS; 377 sysvbfs_file_setsize(vp, vap->va_size); 378 break; 379 default: 380 return EOPNOTSUPP; 381 } 382 383 if (vap->va_mode != (mode_t)VNOVAL) { 384 mode_t mode = vap->va_mode; 385 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, 386 vp, NULL, genfs_can_chmod(vp->v_type, cred, attr->uid, 387 attr->gid, mode)); 388 if (error) 389 return error; 390 attr->mode = mode; 391 } 392 393 if ((vap->va_atime.tv_sec != VNOVAL) || 394 (vap->va_mtime.tv_sec != VNOVAL) || 395 (vap->va_ctime.tv_sec != VNOVAL)) { 396 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp, 397 NULL, genfs_can_chtimes(vp, vap->va_vaflags, attr->uid, 398 cred)); 399 if (error) 400 return error; 401 402 if (vap->va_atime.tv_sec != VNOVAL) 403 attr->atime = vap->va_atime.tv_sec; 404 if (vap->va_mtime.tv_sec != VNOVAL) 405 attr->mtime = vap->va_mtime.tv_sec; 406 if (vap->va_ctime.tv_sec != VNOVAL) 407 attr->ctime = vap->va_ctime.tv_sec; 408 } 409 410 bfs_inode_set_attr(bfs, inode, attr); 411 412 return 0; 413 } 414 415 int 416 sysvbfs_read(void *arg) 417 { 418 struct vop_read_args /* { 419 struct vnode *a_vp; 420 struct uio *a_uio; 421 int a_ioflag; 422 kauth_cred_t a_cred; 423 } */ *a = arg; 424 struct vnode *v = a->a_vp; 425 struct uio *uio = a->a_uio; 426 struct sysvbfs_node *bnode = v->v_data; 427 struct bfs_inode *inode = bnode->inode; 428 vsize_t sz, filesz = bfs_file_size(inode); 429 int err, uerr; 430 const int advice = IO_ADV_DECODE(a->a_ioflag); 431 432 DPRINTF("%s: type=%d\n", __func__, v->v_type); 433 switch (v->v_type) { 434 case VREG: 435 break; 436 case VDIR: 437 return EISDIR; 438 default: 439 return EINVAL; 440 } 441 442 err = 0; 443 while (uio->uio_resid > 0) { 444 if ((sz = MIN(filesz - uio->uio_offset, uio->uio_resid)) == 0) 445 break; 446 447 err = ubc_uiomove(&v->v_uobj, uio, sz, advice, 448 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(v)); 449 if (err) 450 break; 451 DPRINTF("%s: read %ldbyte\n", __func__, sz); 452 } 453 454 uerr = sysvbfs_update(v, NULL, NULL, UPDATE_WAIT); 455 if (err == 0) 456 err = uerr; 457 458 return err; 459 } 460 461 int 462 sysvbfs_write(void *arg) 463 { 464 struct vop_write_args /* { 465 struct vnode *a_vp; 466 struct uio *a_uio; 467 int a_ioflag; 468 kauth_cred_t a_cred; 469 } */ *a = arg; 470 struct vnode *v = a->a_vp; 471 struct uio *uio = a->a_uio; 472 int advice = IO_ADV_DECODE(a->a_ioflag); 473 struct sysvbfs_node *bnode = v->v_data; 474 bool extended = false; 475 vsize_t sz; 476 int err = 0; 477 478 if (a->a_vp->v_type != VREG) 479 return EISDIR; 480 481 if (a->a_ioflag & IO_APPEND) 482 uio->uio_offset = bnode->size; 483 484 if (uio->uio_resid == 0) 485 return 0; 486 487 if (bnode->size < uio->uio_offset + uio->uio_resid) { 488 sysvbfs_file_setsize(v, uio->uio_offset + uio->uio_resid); 489 extended = true; 490 } 491 492 while (uio->uio_resid > 0) { 493 sz = uio->uio_resid; 494 err = ubc_uiomove(&v->v_uobj, uio, sz, advice, 495 UBC_WRITE | UBC_UNMAP_FLAG(v)); 496 if (err) 497 break; 498 DPRINTF("%s: write %ldbyte\n", __func__, sz); 499 } 500 if (err) 501 sysvbfs_file_setsize(v, bnode->size - uio->uio_resid); 502 503 VN_KNOTE(v, NOTE_WRITE | (extended ? NOTE_EXTEND : 0)); 504 505 return err; 506 } 507 508 int 509 sysvbfs_remove(void *arg) 510 { 511 struct vop_remove_args /* { 512 struct vnodeop_desc *a_desc; 513 struct vnode * a_dvp; 514 struct vnode * a_vp; 515 struct componentname * a_cnp; 516 } */ *ap = arg; 517 struct vnode *vp = ap->a_vp; 518 struct vnode *dvp = ap->a_dvp; 519 struct sysvbfs_node *bnode = vp->v_data; 520 struct sysvbfs_mount *bmp = bnode->bmp; 521 struct bfs *bfs = bmp->bfs; 522 int err; 523 524 DPRINTF("%s: delete %s\n", __func__, ap->a_cnp->cn_nameptr); 525 526 if (vp->v_type == VDIR) 527 return EPERM; 528 529 if ((err = bfs_file_delete(bfs, ap->a_cnp->cn_nameptr, true)) != 0) 530 DPRINTF("%s: bfs_file_delete failed.\n", __func__); 531 532 VN_KNOTE(ap->a_vp, NOTE_DELETE); 533 VN_KNOTE(ap->a_dvp, NOTE_WRITE); 534 if (dvp == vp) 535 vrele(vp); 536 else 537 vput(vp); 538 vput(dvp); 539 540 if (err == 0) { 541 bnode->removed = 1; 542 } 543 544 return err; 545 } 546 547 int 548 sysvbfs_rename(void *arg) 549 { 550 struct vop_rename_args /* { 551 struct vnode *a_fdvp; from parent-directory v-node 552 struct vnode *a_fvp; from file v-node 553 struct componentname *a_fcnp; 554 struct vnode *a_tdvp; to parent-directory 555 struct vnode *a_tvp; to file v-node 556 struct componentname *a_tcnp; 557 } */ *ap = arg; 558 struct vnode *fvp = ap->a_fvp; 559 struct vnode *fdvp = ap->a_fdvp; 560 struct vnode *tvp = ap->a_tvp; 561 struct vnode *tdvp = ap->a_tdvp; 562 struct sysvbfs_node *bnode = fvp->v_data; 563 struct bfs *bfs = bnode->bmp->bfs; 564 const char *from_name = ap->a_fcnp->cn_nameptr; 565 const char *to_name = ap->a_tcnp->cn_nameptr; 566 int error; 567 568 DPRINTF("%s: %s->%s\n", __func__, from_name, to_name); 569 if ((fvp->v_mount != tdvp->v_mount) || 570 (tvp && (fvp->v_mount != tvp->v_mount))) { 571 error = EXDEV; 572 printf("cross-device link\n"); 573 goto out; 574 } 575 576 KDASSERT(fvp->v_type == VREG); 577 KDASSERT(tvp == NULL ? true : tvp->v_type == VREG); 578 KASSERT(tdvp == fdvp); 579 580 /* 581 * Make sure the source hasn't been removed between lookup 582 * and target directory lock. 583 */ 584 if (bnode->removed) { 585 error = ENOENT; 586 goto out; 587 } 588 589 /* 590 * Remove the target if it exists. 591 */ 592 if (tvp != NULL) { 593 error = bfs_file_delete(bfs, to_name, true); 594 if (error) 595 goto out; 596 } 597 error = bfs_file_rename(bfs, from_name, to_name); 598 out: 599 /* tdvp == tvp probably can't happen with this fs, but safety first */ 600 if (tdvp == tvp) 601 vrele(tdvp); 602 else 603 vput(tdvp); 604 if (tvp) 605 vput(tvp); 606 607 vrele(fdvp); 608 vrele(fvp); 609 610 return 0; 611 } 612 613 int 614 sysvbfs_readdir(void *v) 615 { 616 struct vop_readdir_args /* { 617 struct vnode *a_vp; 618 struct uio *a_uio; 619 kauth_cred_t a_cred; 620 int *a_eofflag; 621 off_t **a_cookies; 622 int *a_ncookies; 623 } */ *ap = v; 624 struct uio *uio = ap->a_uio; 625 struct vnode *vp = ap->a_vp; 626 struct sysvbfs_node *bnode = vp->v_data; 627 struct bfs *bfs = bnode->bmp->bfs; 628 struct dirent *dp; 629 struct bfs_dirent *file; 630 int i, n, error; 631 632 DPRINTF("%s: offset=%" PRId64 " residue=%zu\n", __func__, 633 uio->uio_offset, uio->uio_resid); 634 635 KDASSERT(vp->v_type == VDIR); 636 KDASSERT(uio->uio_offset >= 0); 637 638 dp = malloc(sizeof(struct dirent), M_BFS, M_WAITOK | M_ZERO); 639 640 i = uio->uio_offset / sizeof(struct dirent); 641 n = uio->uio_resid / sizeof(struct dirent); 642 if ((i + n) > bfs->n_dirent) 643 n = bfs->n_dirent - i; 644 645 DPRINTF("%s 1: %d %d %d\n", __func__, i, n, bfs->n_dirent); 646 for (file = &bfs->dirent[i]; n > 0; file++, i++) { 647 if (i == bfs->max_dirent) { 648 DPRINTF("%s: file system inconsistent.\n", 649 __func__); 650 break; 651 } 652 if (file->inode == 0) 653 continue; 654 655 /* ok, we have a live one here */ 656 n--; 657 memset(dp, 0, sizeof(struct dirent)); 658 dp->d_fileno = file->inode; 659 dp->d_type = file->inode == BFS_ROOT_INODE ? DT_DIR : DT_REG; 660 dp->d_namlen = strlen(file->name); 661 strncpy(dp->d_name, file->name, BFS_FILENAME_MAXLEN); 662 dp->d_reclen = sizeof(struct dirent); 663 if ((error = uiomove(dp, dp->d_reclen, uio)) != 0) { 664 DPRINTF("%s: uiomove failed.\n", __func__); 665 free(dp, M_BFS); 666 return error; 667 } 668 } 669 DPRINTF("%s 2: %d %d %d\n", __func__, i, n, bfs->n_dirent); 670 *ap->a_eofflag = (i == bfs->n_dirent); 671 672 free(dp, M_BFS); 673 return 0; 674 } 675 676 int 677 sysvbfs_inactive(void *arg) 678 { 679 struct vop_inactive_args /* { 680 struct vnode *a_vp; 681 bool *a_recycle; 682 } */ *a = arg; 683 struct vnode *v = a->a_vp; 684 struct sysvbfs_node *bnode = v->v_data; 685 686 DPRINTF("%s:\n", __func__); 687 if (bnode->removed) 688 *a->a_recycle = true; 689 else 690 *a->a_recycle = false; 691 VOP_UNLOCK(v); 692 693 return 0; 694 } 695 696 int 697 sysvbfs_reclaim(void *v) 698 { 699 extern struct pool sysvbfs_node_pool; 700 struct vop_reclaim_args /* { 701 struct vnode *a_vp; 702 } */ *ap = v; 703 struct vnode *vp = ap->a_vp; 704 struct sysvbfs_node *bnode = vp->v_data; 705 struct bfs *bfs = bnode->bmp->bfs; 706 707 DPRINTF("%s:\n", __func__); 708 709 if (bnode->removed) { 710 if (bfs_inode_delete(bfs, bnode->inode->number) != 0) 711 DPRINTF("%s: delete inode failed\n", __func__); 712 } 713 genfs_node_destroy(vp); 714 pool_put(&sysvbfs_node_pool, bnode); 715 vp->v_data = NULL; 716 717 return 0; 718 } 719 720 int 721 sysvbfs_bmap(void *arg) 722 { 723 struct vop_bmap_args /* { 724 struct vnode *a_vp; 725 daddr_t a_bn; 726 struct vnode **a_vpp; 727 daddr_t *a_bnp; 728 int *a_runp; 729 } */ *a = arg; 730 struct vnode *v = a->a_vp; 731 struct sysvbfs_node *bnode = v->v_data; 732 struct sysvbfs_mount *bmp = bnode->bmp; 733 struct bfs_inode *inode = bnode->inode; 734 daddr_t blk; 735 736 DPRINTF("%s:\n", __func__); 737 /* BFS algorithm is contiguous allocation */ 738 blk = inode->start_sector + a->a_bn; 739 740 if (blk * BFS_BSIZE > bmp->bfs->data_end) 741 return ENOSPC; 742 743 *a->a_vpp = bmp->devvp; 744 *a->a_runp = 0; 745 DPRINTF("%s: %d + %" PRId64 "\n", __func__, inode->start_sector, 746 a->a_bn); 747 748 *a->a_bnp = blk; 749 750 751 return 0; 752 } 753 754 int 755 sysvbfs_strategy(void *arg) 756 { 757 struct vop_strategy_args /* { 758 struct vnode *a_vp; 759 struct buf *a_bp; 760 } */ *a = arg; 761 struct buf *b = a->a_bp; 762 struct vnode *v = a->a_vp; 763 struct sysvbfs_node *bnode = v->v_data; 764 struct sysvbfs_mount *bmp = bnode->bmp; 765 int error; 766 767 DPRINTF("%s:\n", __func__); 768 KDASSERT(v->v_type == VREG); 769 if (b->b_blkno == b->b_lblkno) { 770 error = VOP_BMAP(v, b->b_lblkno, NULL, &b->b_blkno, NULL); 771 if (error) { 772 b->b_error = error; 773 biodone(b); 774 return error; 775 } 776 if ((long)b->b_blkno == -1) 777 clrbuf(b); 778 } 779 if ((long)b->b_blkno == -1) { 780 biodone(b); 781 return 0; 782 } 783 784 return VOP_STRATEGY(bmp->devvp, b); 785 } 786 787 int 788 sysvbfs_print(void *v) 789 { 790 struct vop_print_args /* { 791 struct vnode *a_vp; 792 } */ *ap = v; 793 struct sysvbfs_node *bnode = ap->a_vp->v_data; 794 795 DPRINTF("%s:\n", __func__); 796 bfs_dump(bnode->bmp->bfs); 797 798 return 0; 799 } 800 801 int 802 sysvbfs_advlock(void *v) 803 { 804 struct vop_advlock_args /* { 805 struct vnode *a_vp; 806 void *a_id; 807 int a_op; 808 struct flock *a_fl; 809 int a_flags; 810 } */ *ap = v; 811 struct sysvbfs_node *bnode = ap->a_vp->v_data; 812 813 DPRINTF("%s: op=%d\n", __func__, ap->a_op); 814 815 return lf_advlock(ap, &bnode->lockf, bfs_file_size(bnode->inode)); 816 } 817 818 int 819 sysvbfs_pathconf(void *v) 820 { 821 struct vop_pathconf_args /* { 822 struct vnode *a_vp; 823 int a_name; 824 register_t *a_retval; 825 } */ *ap = v; 826 int err = 0; 827 828 DPRINTF("%s:\n", __func__); 829 830 switch (ap->a_name) { 831 case _PC_LINK_MAX: 832 *ap->a_retval = 1; 833 break; 834 case _PC_NAME_MAX: 835 *ap->a_retval = BFS_FILENAME_MAXLEN; 836 break; 837 case _PC_PATH_MAX: 838 *ap->a_retval = BFS_FILENAME_MAXLEN; 839 break; 840 case _PC_CHOWN_RESTRICTED: 841 *ap->a_retval = 1; 842 break; 843 case _PC_NO_TRUNC: 844 *ap->a_retval = 0; 845 break; 846 case _PC_SYNC_IO: 847 *ap->a_retval = 1; 848 break; 849 case _PC_FILESIZEBITS: 850 *ap->a_retval = 32; 851 break; 852 default: 853 err = EINVAL; 854 break; 855 } 856 857 return err; 858 } 859 860 int 861 sysvbfs_fsync(void *v) 862 { 863 struct vop_fsync_args /* { 864 struct vnode *a_vp; 865 kauth_cred_t a_cred; 866 int a_flags; 867 off_t offlo; 868 off_t offhi; 869 } */ *ap = v; 870 struct vnode *vp = ap->a_vp; 871 int error, wait; 872 873 if (ap->a_flags & FSYNC_CACHE) { 874 return EOPNOTSUPP; 875 } 876 877 wait = (ap->a_flags & FSYNC_WAIT) != 0; 878 error = vflushbuf(vp, ap->a_flags); 879 if (error == 0 && (ap->a_flags & FSYNC_DATAONLY) == 0) 880 error = sysvbfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0); 881 882 return error; 883 } 884 885 int 886 sysvbfs_update(struct vnode *vp, const struct timespec *acc, 887 const struct timespec *mod, int flags) 888 { 889 struct sysvbfs_node *bnode = vp->v_data; 890 struct bfs_fileattr attr; 891 892 if (vp->v_mount->mnt_flag & MNT_RDONLY) 893 return 0; 894 895 DPRINTF("%s:\n", __func__); 896 memset(&attr, 0xff, sizeof attr); /* Set VNOVAL all */ 897 if (bnode->update_atime) { 898 attr.atime = acc ? acc->tv_sec : time_second; 899 bnode->update_atime = false; 900 } 901 if (bnode->update_ctime) { 902 attr.ctime = time_second; 903 bnode->update_ctime = false; 904 } 905 if (bnode->update_mtime) { 906 attr.mtime = mod ? mod->tv_sec : time_second; 907 bnode->update_mtime = false; 908 } 909 bfs_inode_set_attr(bnode->bmp->bfs, bnode->inode, &attr); 910 911 return 0; 912 } 913 914 static void 915 sysvbfs_file_setsize(struct vnode *v, size_t size) 916 { 917 struct sysvbfs_node *bnode = v->v_data; 918 struct bfs_inode *inode = bnode->inode; 919 920 bnode->size = size; 921 uvm_vnp_setsize(v, bnode->size); 922 inode->end_sector = bnode->data_block + 923 (ROUND_SECTOR(bnode->size) >> DEV_BSHIFT) - 1; 924 inode->eof_offset_byte = bnode->data_block * DEV_BSIZE + 925 bnode->size - 1; 926 bnode->update_mtime = true; 927 } 928