1/* $NetBSD: v7fs_vnops.c,v 1.7 2012/01/27 12:22:02 njoly Exp $ */ 2 3/*- 4 * Copyright (c) 2004, 2011 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: v7fs_vnops.c,v 1.7 2012/01/27 12:22:02 njoly Exp $"); 34#if defined _KERNEL_OPT 35#include "opt_v7fs.h" 36#endif 37 38#include <sys/param.h> 39#include <sys/kernel.h> 40#include <sys/resource.h> 41#include <sys/vnode.h> 42#include <sys/namei.h> 43#include <sys/dirent.h> 44#include <sys/malloc.h> 45#include <sys/lockf.h> 46#include <sys/unistd.h> 47#include <sys/fcntl.h> 48#include <sys/kauth.h> 49#include <sys/buf.h> 50#include <sys/stat.h> /*APPEND */ 51#include <miscfs/genfs/genfs.h> 52 53#include <fs/v7fs/v7fs.h> 54#include <fs/v7fs/v7fs_impl.h> 55#include <fs/v7fs/v7fs_inode.h> 56#include <fs/v7fs/v7fs_dirent.h> 57#include <fs/v7fs/v7fs_file.h> 58#include <fs/v7fs/v7fs_datablock.h> 59#include <fs/v7fs/v7fs_extern.h> 60 61#ifdef V7FS_VNOPS_DEBUG 62#define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args) 63#else 64#define DPRINTF(arg...) ((void)0) 65#endif 66 67MALLOC_JUSTDEFINE(M_V7FS_VNODE, "v7fs vnode", "v7fs vnode structures"); 68MALLOC_DECLARE(M_V7FS); 69 70int v7fs_vnode_reload(struct mount *, struct vnode *); 71 72static v7fs_mode_t vtype_to_v7fs_mode(enum vtype); 73static uint8_t v7fs_mode_to_d_type(v7fs_mode_t); 74 75static v7fs_mode_t 76vtype_to_v7fs_mode(enum vtype type) 77{ 78 /* Convert Vnode types to V7FS types (sys/vnode.h)*/ 79 v7fs_mode_t table[] = { 0, V7FS_IFREG, V7FS_IFDIR, V7FS_IFBLK, 80 V7FS_IFCHR, V7FSBSD_IFLNK, V7FSBSD_IFSOCK, 81 V7FSBSD_IFFIFO }; 82 return table[type]; 83} 84 85static uint8_t 86v7fs_mode_to_d_type(v7fs_mode_t mode) 87{ 88 /* Convert V7FS types to dirent d_type (sys/dirent.h)*/ 89 90 return (mode & V7FS_IFMT) >> 12; 91} 92 93int 94v7fs_lookup(void *v) 95{ 96 struct vop_lookup_args /* { 97 struct vnode *a_dvp; 98 struct vnode **a_vpp; 99 struct componentname *a_cnp; 100 } */ *a = v; 101 struct vnode *dvp = a->a_dvp; 102 struct v7fs_node *parent_node = dvp->v_data; 103 struct v7fs_inode *parent = &parent_node->inode; 104 struct v7fs_self *fs = parent_node->v7fsmount->core;/* my filesystem */ 105 struct vnode *vpp; 106 struct componentname *cnp = a->a_cnp; 107 int nameiop = cnp->cn_nameiop; 108 const char *name = cnp->cn_nameptr; 109 int namelen = cnp->cn_namelen; 110 int flags = cnp->cn_flags; 111 bool isdotdot = flags & ISDOTDOT; 112 bool islastcn = flags & ISLASTCN; 113 v7fs_ino_t ino; 114 int error; 115#ifdef V7FS_VNOPS_DEBUG 116 const char *opname[] = { "LOOKUP", "CREATE", "DELETE", "RENAME" }; 117#endif 118 DPRINTF("'%s' op=%s flags=%d parent=%d %o %dbyte\n", name, 119 opname[nameiop], cnp->cn_flags, parent->inode_number, parent->mode, 120 parent->filesize); 121 122 *a->a_vpp = 0; 123 124 /* Check directory permission for search */ 125 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred))) { 126 DPRINTF("***perm.\n"); 127 return error; 128 } 129 130 /* Deny last component write operation on a read-only mount */ 131 if (islastcn && (dvp->v_mount->mnt_flag & MNT_RDONLY) && 132 (nameiop == DELETE || nameiop == RENAME)) { 133 DPRINTF("***ROFS.\n"); 134 return EROFS; 135 } 136 137 /* "." */ 138 if (namelen == 1 && name[0] == '.') { 139 if ((nameiop == RENAME) && islastcn) { 140 return EISDIR; /* t_vnops rename_dir(3) */ 141 } 142 vref(dvp); /* v_usecount++ */ 143 *a->a_vpp = dvp; 144 DPRINTF("done.(.)\n"); 145 return 0; 146 } 147 148 /* ".." and reguler file. */ 149 if ((error = v7fs_file_lookup_by_name(fs, parent, name, &ino))) { 150 /* Not found. Tell this entry be able to allocate. */ 151 if (((nameiop == CREATE) || (nameiop == RENAME)) && islastcn) { 152 /* Check directory permission to allocate. */ 153 if ((error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred))) { 154 DPRINTF("access denied. (%s)\n", name); 155 return error; 156 } 157 DPRINTF("EJUSTRETURN op=%d (%s)\n", nameiop, name); 158 return EJUSTRETURN; 159 } 160 DPRINTF("lastcn=%d\n", flags & ISLASTCN); 161 return error; 162 } 163 164 if ((nameiop == DELETE) && islastcn) { 165 if ((error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred))) { 166 DPRINTF("access denied. (%s)\n", name); 167 return error; 168 } 169 } 170 171 /* Entry found. Allocate v-node */ 172 // Check permissions? 173 vpp = 0; 174 if (isdotdot) { 175 VOP_UNLOCK(dvp); /* preserve reference count. (not vput) */ 176 } 177 DPRINTF("enter vget\n"); 178 if ((error = v7fs_vget(dvp->v_mount, ino, &vpp))) { 179 DPRINTF("***can't get vnode.\n"); 180 return error; 181 } 182 DPRINTF("exit vget\n"); 183 if (isdotdot) { 184 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 185 } 186 *a->a_vpp = vpp; 187 DPRINTF("done.(%s)\n", name); 188 189 return 0; 190} 191 192int 193v7fs_create(void *v) 194{ 195 struct vop_create_args /* { 196 struct vnode *a_dvp; 197 struct vnode **a_vpp; 198 struct componentname *a_cnp; 199 struct vattr *a_vap; 200 } */ *a = v; 201 struct v7fs_node *parent_node = a->a_dvp->v_data; 202 struct v7fs_mount *v7fsmount = parent_node->v7fsmount; 203 struct v7fs_self *fs = v7fsmount->core; 204 struct mount *mp = v7fsmount->mountp; 205 struct v7fs_fileattr attr; 206 struct vattr *va = a->a_vap; 207 kauth_cred_t cr = a->a_cnp->cn_cred; 208 v7fs_ino_t ino; 209 int error = 0; 210 211 DPRINTF("%s parent#%d\n", a->a_cnp->cn_nameptr, 212 parent_node->inode.inode_number); 213 KDASSERT((va->va_type == VREG) || (va->va_type == VSOCK)); 214 215 memset(&attr, 0, sizeof(attr)); 216 attr.uid = kauth_cred_geteuid(cr); 217 attr.gid = kauth_cred_getegid(cr); 218 attr.mode = va->va_mode | vtype_to_v7fs_mode (va->va_type); 219 attr.device = 0; 220 221 /* Allocate disk entry. and register its entry to parent directory. */ 222 if ((error = v7fs_file_allocate(fs, &parent_node->inode, 223 a->a_cnp->cn_nameptr, &attr, &ino))) { 224 DPRINTF("v7fs_file_allocate failed.\n"); 225 goto unlock_exit; 226 } 227 /* Sync dirent size change. */ 228 uvm_vnp_setsize(a->a_dvp, v7fs_inode_filesize(&parent_node->inode)); 229 230 /* Get myself vnode. */ 231 *a->a_vpp = 0; 232 if ((error = v7fs_vget(mp, ino, a->a_vpp))) { 233 DPRINTF("v7fs_vget failed.\n"); 234 goto unlock_exit; 235 } 236 237 /* Scheduling update time. real update by v7fs_update */ 238 struct v7fs_node *newnode = (*a->a_vpp)->v_data; 239 newnode->update_ctime = true; 240 newnode->update_mtime = true; 241 newnode->update_atime = true; 242 DPRINTF("allocated %s->#%d\n", a->a_cnp->cn_nameptr, ino); 243 244unlock_exit: 245 /* unlock parent directory */ 246 vput(a->a_dvp); /* locked at v7fs_lookup(); */ 247 248 return error; 249} 250 251int 252v7fs_mknod(void *v) 253{ 254 struct vop_mknod_args /* { 255 struct vnode *a_dvp; 256 struct vnode **a_vpp; 257 struct componentname *a_cnp; 258 struct vattr *a_vap; 259 } */ *a = v; 260 struct componentname *cnp = a->a_cnp; 261 kauth_cred_t cr = cnp->cn_cred; 262 struct vnode *dvp = a->a_dvp; 263 struct vattr *va = a->a_vap; 264 struct v7fs_node *parent_node = dvp->v_data; 265 struct v7fs_mount *v7fsmount = parent_node->v7fsmount; 266 struct v7fs_self *fs = v7fsmount->core; 267 struct mount *mp = v7fsmount->mountp; 268 struct v7fs_fileattr attr; 269 270 v7fs_ino_t ino; 271 int error = 0; 272 273 DPRINTF("%s %06o %lx %d\n", cnp->cn_nameptr, va->va_mode, 274 (long)va->va_rdev, va->va_type); 275 memset(&attr, 0, sizeof(attr)); 276 attr.uid = kauth_cred_geteuid(cr); 277 attr.gid = kauth_cred_getegid(cr); 278 attr.mode = va->va_mode | vtype_to_v7fs_mode(va->va_type); 279 attr.device = va->va_rdev; 280 281 if ((error = v7fs_file_allocate(fs, &parent_node->inode, 282 cnp->cn_nameptr, &attr, &ino))) 283 goto unlock_exit; 284 /* Sync dirent size change. */ 285 uvm_vnp_setsize(dvp, v7fs_inode_filesize(&parent_node->inode)); 286 287 if ((error = v7fs_vget(mp, ino, a->a_vpp))) { 288 DPRINTF("can't get vnode.\n"); 289 goto unlock_exit; 290 } 291 struct v7fs_node *newnode = (*a->a_vpp)->v_data; 292 newnode->update_ctime = true; 293 newnode->update_mtime = true; 294 newnode->update_atime = true; 295 296unlock_exit: 297 vput(dvp); 298 299 return error; 300} 301 302int 303v7fs_open(void *v) 304{ 305 struct vop_open_args /* { 306 struct vnode *a_vp; 307 int a_mode; 308 kauth_cred_t a_cred; 309 } */ *a = v; 310 311 struct vnode *vp = a->a_vp; 312 struct v7fs_node *v7node = vp->v_data; 313 struct v7fs_inode *inode = &v7node->inode; 314 315 DPRINTF("inode %d\n", inode->inode_number); 316 /* Append mode file pointer is managed by kernel. */ 317 if (inode->append_mode && 318 ((a->a_mode & (FWRITE | O_APPEND)) == FWRITE)) { 319 DPRINTF("file is already opened by append mode.\n"); 320 return EPERM; 321 } 322 323 return 0; 324} 325 326int 327v7fs_close(void *v) 328{ 329 struct vop_close_args /* { 330 struct vnodeop_desc *a_desc; 331 struct vnode *a_vp; 332 int a_fflag; 333 kauth_cred_t a_cred; 334 } */ *a = v; 335 struct vnode *vp = a->a_vp; 336#ifdef V7FS_VNOPS_DEBUG 337 struct v7fs_node *v7node = vp->v_data; 338 struct v7fs_inode *inode = &v7node->inode; 339#endif 340 DPRINTF("#%d (i)%dbyte (v)%zubyte\n", inode->inode_number, 341 v7fs_inode_filesize(inode), vp->v_size); 342 343 /* Update timestamp */ 344 v7fs_update(vp, 0, 0, UPDATE_WAIT); 345 346 return 0; 347} 348 349static int 350v7fs_check_possible(struct vnode *vp, struct v7fs_node *v7node, 351 mode_t mode) 352{ 353 354 if (!(mode & VWRITE)) 355 return 0; 356 357 switch (vp->v_type) { 358 default: 359 /* special file is always writable. */ 360 return 0; 361 case VDIR: 362 case VLNK: 363 case VREG: 364 break; 365 } 366 367 return vp->v_mount->mnt_flag & MNT_RDONLY ? EROFS : 0; 368} 369 370static int 371v7fs_check_permitted(struct vnode *vp, struct v7fs_node *v7node, 372 mode_t mode, kauth_cred_t cred) 373{ 374 375 struct v7fs_inode *inode = &v7node->inode; 376 377 return genfs_can_access(vp->v_type, inode->mode, inode->uid, inode->gid, 378 mode, cred); 379} 380 381int 382v7fs_access(void *v) 383{ 384 struct vop_access_args /* { 385 struct vnode *a_vp; 386 int a_mode; 387 kauth_cred_t a_cred; 388 } */ *ap = v; 389 struct vnode *vp = ap->a_vp; 390 struct v7fs_node *v7node = vp->v_data; 391 int error; 392 393 error = v7fs_check_possible(vp, v7node, ap->a_mode); 394 if (error) 395 return error; 396 397 error = v7fs_check_permitted(vp, v7node, ap->a_mode, ap->a_cred); 398 399 return error; 400} 401 402int 403v7fs_getattr(void *v) 404{ 405 struct vop_getattr_args /* { 406 struct vnode *a_vp; 407 struct vattr *a_vap; 408 kauth_cred_t a_cred; 409 } */ *ap = v; 410 struct vnode *vp = ap->a_vp; 411 struct v7fs_node *v7node = vp->v_data; 412 struct v7fs_inode *inode = &v7node->inode; 413 struct v7fs_mount *v7fsmount = v7node->v7fsmount; 414 struct vattr *vap = ap->a_vap; 415 416 DPRINTF("\n"); 417 vap->va_type = vp->v_type; 418 vap->va_mode = inode->mode; 419 vap->va_nlink = inode->nlink; 420 vap->va_uid = inode->uid; 421 vap->va_gid = inode->gid; 422 vap->va_fsid = v7fsmount->devvp->v_rdev; 423 vap->va_fileid = inode->inode_number; 424 vap->va_size = vp->v_size; 425 vap->va_atime.tv_sec = inode->atime; 426 vap->va_mtime.tv_sec = inode->mtime; 427 vap->va_ctime.tv_sec = inode->ctime; 428 vap->va_birthtime.tv_sec = 0; 429 vap->va_gen = 1; 430 vap->va_flags = 0; 431 vap->va_rdev = inode->device; 432 vap->va_bytes = vap->va_size; /* No sparse support. */ 433 vap->va_filerev = 0; 434 vap->va_vaflags = 0; 435 /* PAGE_SIZE is larger than sizeof(struct dirent). OK. 436 getcwd_scandir()@vfs_getcwd.c */ 437 vap->va_blocksize = PAGE_SIZE; 438 439 return 0; 440} 441 442int 443v7fs_setattr(void *v) 444{ 445 struct vop_setattr_args /* { 446 struct vnode *a_vp; 447 struct vattr *a_vap; 448 kauth_cred_t a_cred; 449 struct proc *p; 450 } */ *ap = v; 451 struct vnode *vp = ap->a_vp; 452 struct vattr *vap = ap->a_vap; 453 struct v7fs_node *v7node = vp->v_data; 454 struct v7fs_self *fs = v7node->v7fsmount->core; 455 struct v7fs_inode *inode = &v7node->inode; 456 kauth_cred_t cred = ap->a_cred; 457 struct timespec *acc, *mod; 458 int error = 0; 459 acc = mod = NULL; 460 461 DPRINTF("\n"); 462 463 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 464 switch (vp->v_type) { 465 default: 466 /* special file is always writable. */ 467 break; 468 case VDIR: 469 case VLNK: 470 case VREG: 471 DPRINTF("read-only mount\n"); 472 return EROFS; 473 } 474 } 475 476 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || 477 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || 478 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || 479 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { 480 DPRINTF("invalid request\n"); 481 return EINVAL; 482 } 483 /* File pointer mode. */ 484 if ((vap->va_flags != VNOVAL) && (vap->va_flags & SF_APPEND)) { 485 DPRINTF("Set append-mode.\n"); 486 inode->append_mode = true; 487 } 488 489 /* File size change. */ 490 if ((vap->va_size != VNOVAL) && (vp->v_type == VREG)) { 491 error = v7fs_datablock_size_change(fs, vap->va_size, inode); 492 if (error == 0) 493 uvm_vnp_setsize(vp, vap->va_size); 494 } 495 uid_t uid = inode->uid; 496 gid_t gid = inode->gid; 497 498 if (vap->va_uid != (uid_t)VNOVAL) { 499 uid = vap->va_uid; 500 error = kauth_authorize_vnode(cred, 501 KAUTH_VNODE_CHANGE_OWNERSHIP, vp, NULL, 502 genfs_can_chown(vp, cred, inode->uid, inode->gid, uid, 503 gid)); 504 if (error) 505 return error; 506 inode->uid = uid; 507 } 508 if (vap->va_gid != (uid_t)VNOVAL) { 509 gid = vap->va_gid; 510 error = kauth_authorize_vnode(cred, 511 KAUTH_VNODE_CHANGE_OWNERSHIP, vp, NULL, 512 genfs_can_chown(vp, cred, inode->uid, inode->gid, uid, 513 gid)); 514 if (error) 515 return error; 516 inode->gid = gid; 517 } 518 if (vap->va_mode != (mode_t)VNOVAL) { 519 mode_t mode = vap->va_mode; 520 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, 521 vp, NULL, genfs_can_chmod(vp, cred, inode->uid, inode->gid, 522 mode)); 523 if (error) { 524 return error; 525 } 526 v7fs_inode_chmod(inode, mode); 527 } 528 if (vap->va_atime.tv_sec != VNOVAL) { 529 acc = &vap->va_atime; 530 } 531 if (vap->va_mtime.tv_sec != VNOVAL) { 532 mod = &vap->va_mtime; 533 v7node->update_mtime = true; 534 } 535 if (vap->va_ctime.tv_sec != VNOVAL) { 536 v7node->update_ctime = true; 537 } 538 539 v7node->update_atime = true; 540 v7fs_update(vp, acc, mod, 0); 541 542 return error; 543} 544 545int 546v7fs_read(void *v) 547{ 548 struct vop_read_args /* { 549 struct vnode *a_vp; 550 struct uio *a_uio; 551 int a_ioflag; 552 kauth_cred_t a_cred; 553 } */ *a = v; 554 struct vnode *vp = a->a_vp; 555 struct uio *uio = a->a_uio; 556 struct v7fs_node *v7node = vp->v_data; 557 struct v7fs_inode *inode = &v7node->inode; 558 vsize_t sz, filesz = v7fs_inode_filesize(inode); 559 const int advice = IO_ADV_DECODE(a->a_ioflag); 560 int error = 0; 561 562 DPRINTF("type=%d inode=%d\n", vp->v_type, v7node->inode.inode_number); 563 564 while (uio->uio_resid > 0) { 565 if ((sz = MIN(filesz - uio->uio_offset, uio->uio_resid)) == 0) 566 break; 567 568 error = ubc_uiomove(&vp->v_uobj, uio, sz, advice, UBC_READ | 569 UBC_PARTIALOK | UBC_UNMAP_FLAG(v)); 570 if (error) { 571 break; 572 } 573 DPRINTF("read %zubyte\n", sz); 574 } 575 v7node->update_atime = true; 576 577 return error; 578} 579 580int 581v7fs_write(void *v) 582{ 583 struct vop_write_args /* { 584 struct vnode *a_vp; 585 struct uio *a_uio; 586 int a_ioflag; 587 kauth_cred_t a_cred; 588 } */ *a = v; 589 struct vnode *vp = a->a_vp; 590 struct uio *uio = a->a_uio; 591 int advice = IO_ADV_DECODE(a->a_ioflag); 592 struct v7fs_node *v7node = vp->v_data; 593 struct v7fs_inode *inode = &v7node->inode; 594 struct v7fs_self *fs = v7node->v7fsmount->core; 595 vsize_t sz; 596 int error = 0; 597 598 if (uio->uio_resid == 0) 599 return 0; 600 601 sz = v7fs_inode_filesize(inode); 602 DPRINTF("(i)%ld (v)%zu ofs=%zu + res=%zu = %zu\n", sz, vp->v_size, 603 uio->uio_offset, uio->uio_resid, uio->uio_offset + uio->uio_resid); 604 605 /* Append mode file offset is managed by kernel. */ 606 if (a->a_ioflag & IO_APPEND) 607 uio->uio_offset = sz; 608 609 /* If write region is over filesize, expand. */ 610 size_t newsize= uio->uio_offset + uio->uio_resid; 611 ssize_t expand = newsize - sz; 612 if (expand > 0) { 613 if ((error = v7fs_datablock_expand(fs, inode, expand))) 614 return error; 615 uvm_vnp_setsize(vp, newsize); 616 } 617 618 while (uio->uio_resid > 0) { 619 sz = uio->uio_resid; 620 if ((error = ubc_uiomove(&vp->v_uobj, uio, sz, advice, 621 UBC_WRITE | UBC_UNMAP_FLAG(v)))) 622 break; 623 DPRINTF("write %zubyte\n", sz); 624 } 625 v7node->update_mtime = true; 626 627 return error; 628} 629 630int 631v7fs_fsync(void *v) 632{ 633 struct vop_fsync_args /* { 634 struct vnode *a_vp; 635 kauth_cred_t a_cred; 636 int a_flags; 637 off_t offlo; 638 off_t offhi; 639 } */ *a = v; 640 struct vnode *vp = a->a_vp; 641 int error, wait; 642 643 DPRINTF("%p\n", a->a_vp); 644 if (a->a_flags & FSYNC_CACHE) { 645 return EOPNOTSUPP; 646 } 647 648 wait = (a->a_flags & FSYNC_WAIT); 649 error = vflushbuf(vp, a->a_flags); 650 651 if (error == 0 && (a->a_flags & FSYNC_DATAONLY) == 0) 652 error = v7fs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0); 653 654 return error; 655} 656 657int 658v7fs_remove(void *v) 659{ 660 struct vop_remove_args /* { 661 struct vnodeop_desc *a_desc; 662 struct vnode * a_dvp; 663 struct vnode * a_vp; 664 struct componentname * a_cnp; 665 } */ *a = v; 666 struct v7fs_node *parent_node = a->a_dvp->v_data; 667 struct v7fs_mount *v7fsmount = parent_node->v7fsmount; 668 struct vnode *vp = a->a_vp; 669 struct v7fs_inode *inode = &((struct v7fs_node *)vp->v_data)->inode; 670 struct vnode *dvp = a->a_dvp; 671 struct v7fs_self *fs = v7fsmount->core; 672 bool remove; 673 int error = 0; 674 675 DPRINTF("delete %s\n", a->a_cnp->cn_nameptr); 676 677 if (vp->v_type == VDIR) { 678 error = EPERM; 679 goto out; 680 } 681 682 remove = v7fs_inode_nlink(inode) == 1; 683 if (remove) 684 uvm_vnp_setsize(vp, 0); 685 686 if ((error = v7fs_file_deallocate(fs, &parent_node->inode, 687 a->a_cnp->cn_nameptr))) { 688 DPRINTF("v7fs_file_delete failed.\n"); 689 goto out; 690 } 691 /* Sync dirent size change. */ 692 uvm_vnp_setsize(dvp, v7fs_inode_filesize(&parent_node->inode)); 693 /* This inode is no longer used. -> v7fs_inactive */ 694 if (remove) 695 memset(inode, 0, sizeof(*inode)); 696 697out: 698 if (dvp == vp) 699 vrele(vp); /* v_usecount-- of unlocked vp */ 700 else 701 vput(vp); /* unlock vp and then v_usecount-- */ 702 vput(dvp); 703 704 return error; 705} 706 707int 708v7fs_link(void *v) 709{ 710 struct vop_link_args /* { 711 struct vnode *a_dvp; 712 struct vnode *a_vp; 713 struct componentname *a_cnp; 714 } */ *a = v; 715 struct vnode *dvp = a->a_dvp; 716 struct vnode *vp = a->a_vp; 717 struct v7fs_node *parent_node = dvp->v_data; 718 struct v7fs_node *node = vp->v_data; 719 struct v7fs_inode *parent = &parent_node->inode; 720 struct v7fs_inode *p = &node->inode; 721 struct v7fs_self *fs = node->v7fsmount->core; 722 struct componentname *cnp = a->a_cnp; 723 int error = 0; 724 725 DPRINTF("%p\n", vp); 726 /* Lock soruce file */ 727 if ((error = vn_lock(vp, LK_EXCLUSIVE))) { 728 DPRINTF("lock failed. %p\n", vp); 729 VOP_ABORTOP(dvp, cnp); 730 goto unlock; 731 } 732 error = v7fs_file_link(fs, parent, p, cnp->cn_nameptr); 733 /* Sync dirent size change. */ 734 uvm_vnp_setsize(dvp, v7fs_inode_filesize(&parent_node->inode)); 735 736 VOP_UNLOCK(vp); 737unlock: 738 vput(dvp); 739 740 return error; 741} 742 743int 744v7fs_rename(void *v) 745{ 746 struct vop_rename_args /* { 747 struct vnode *a_fdvp; from parent-directory 748 struct vnode *a_fvp; from file 749 struct componentname *a_fcnp; 750 struct vnode *a_tdvp; to parent-directory 751 struct vnode *a_tvp; to file 752 struct componentname *a_tcnp; 753 } */ *a = v; 754 struct vnode *fvp = a->a_fvp; 755 struct vnode *tvp = a->a_tvp; 756 struct vnode *fdvp = a->a_fdvp; 757 struct vnode *tdvp = a->a_tdvp; 758 struct v7fs_node *parent_from = fdvp->v_data; 759 struct v7fs_node *parent_to = tdvp->v_data; 760 struct v7fs_node *v7node = fvp->v_data; 761 struct v7fs_self *fs = v7node->v7fsmount->core; 762 const char *from_name = a->a_fcnp->cn_nameptr; 763 const char *to_name = a->a_tcnp->cn_nameptr; 764 int error; 765 766 DPRINTF("%s->%s %p %p\n", from_name, to_name, fvp, tvp); 767 768 if ((fvp->v_mount != tdvp->v_mount) || 769 (tvp && (fvp->v_mount != tvp->v_mount))) { 770 error = EXDEV; 771 DPRINTF("cross-device link\n"); 772 goto out; 773 } 774 // XXXsource file lock? 775 error = v7fs_file_rename(fs, &parent_from->inode, from_name, 776 &parent_to->inode, to_name); 777 /* 'to file' inode may be changed. (hard-linked and it is cached.) 778 t_vnops rename_reg_nodir */ 779 if (tvp) { 780 v7fs_vnode_reload(parent_from->v7fsmount->mountp, tvp); 781 } 782 /* Sync dirent size change. */ 783 uvm_vnp_setsize(tdvp, v7fs_inode_filesize(&parent_to->inode)); 784 uvm_vnp_setsize(fdvp, v7fs_inode_filesize(&parent_from->inode)); 785out: 786 if (tvp) 787 vput(tvp); /* locked on entry */ 788 if (tdvp == tvp) 789 vrele(tdvp); 790 else 791 vput(tdvp); 792 vrele(fdvp); 793 vrele(fvp); 794 795 return error; 796} 797 798int 799v7fs_mkdir(void *v) 800{ 801 struct vop_mkdir_args /* { 802 struct vnode *a_dvp; 803 struct vnode **a_vpp; 804 struct componentname *a_cnp; 805 struct vattr *a_vap; 806 } */ *a = v; 807 struct componentname *cnp = a->a_cnp; 808 kauth_cred_t cr = cnp->cn_cred; 809 struct vnode *dvp = a->a_dvp; 810 struct vattr *va = a->a_vap; 811 struct v7fs_node *parent_node = dvp->v_data; 812 struct v7fs_mount *v7fsmount = parent_node->v7fsmount; 813 struct v7fs_self *fs = v7fsmount->core; 814 struct v7fs_fileattr attr; 815 struct mount *mp = v7fsmount->mountp; 816 v7fs_ino_t ino; 817 int error = 0; 818 819 DPRINTF("\n"); 820 memset(&attr, 0, sizeof(attr)); 821 attr.uid = kauth_cred_geteuid(cr); 822 attr.gid = kauth_cred_getegid(cr); 823 attr.mode = va->va_mode | vtype_to_v7fs_mode(va->va_type); 824 825 if ((error = v7fs_file_allocate(fs, &parent_node->inode, 826 cnp->cn_nameptr, &attr, &ino))) 827 goto unlock_exit; 828 /* Sync dirent size change. */ 829 uvm_vnp_setsize(dvp, v7fs_inode_filesize(&parent_node->inode)); 830 831 if ((error = v7fs_vget(mp, ino, a->a_vpp))) { 832 DPRINTF("can't get vnode.\n"); 833 } 834 struct v7fs_node *newnode = (*a->a_vpp)->v_data; 835 newnode->update_ctime = true; 836 newnode->update_mtime = true; 837 newnode->update_atime = true; 838 839unlock_exit: 840 vput(dvp); 841 842 return error; 843} 844 845int 846v7fs_rmdir(void *v) 847{ 848 struct vop_rmdir_args /* { 849 struct vnode *a_dvp; 850 struct vnode *a_vp; 851 struct componentname *a_cnp; 852 } */ *a = v; 853 struct vnode *vp = a->a_vp; 854 struct vnode *dvp = a->a_dvp; 855 struct v7fs_node *parent_node = dvp->v_data; 856 struct v7fs_mount *v7fsmount = parent_node->v7fsmount; 857 struct v7fs_inode *inode = &((struct v7fs_node *)vp->v_data)->inode; 858 struct v7fs_self *fs = v7fsmount->core; 859 int error = 0; 860 861 DPRINTF("delete %s\n", a->a_cnp->cn_nameptr); 862 863 KDASSERT(vp->v_type == VDIR); 864 865 if ((error = v7fs_file_deallocate(fs, &parent_node->inode, 866 a->a_cnp->cn_nameptr))) { 867 DPRINTF("v7fs_directory_deallocate failed.\n"); 868 goto out; 869 } 870 uvm_vnp_setsize(vp, 0); 871 /* Sync dirent size change. */ 872 uvm_vnp_setsize(dvp, v7fs_inode_filesize(&parent_node->inode)); 873 /* This inode is no longer used. -> v7fs_inactive */ 874 memset(inode, 0, sizeof(*inode)); 875out: 876 vput(vp); 877 vput(dvp); 878 879 return error; 880} 881 882struct v7fs_readdir_arg { 883 struct dirent *dp; 884 struct uio *uio; 885 int start; 886 int end; 887 int cnt; 888}; 889static int readdir_subr(struct v7fs_self *, void *, v7fs_daddr_t, size_t); 890 891int 892readdir_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk, size_t sz) 893{ 894 struct v7fs_readdir_arg *p = (struct v7fs_readdir_arg *)ctx; 895 struct v7fs_dirent *dir; 896 struct dirent *dp = p->dp; 897 struct v7fs_inode inode; 898 char filename[V7FS_NAME_MAX + 1]; 899 int i, n; 900 int error = 0; 901 void *buf; 902 903 if (!(buf = scratch_read(fs, blk))) 904 return EIO; 905 dir = (struct v7fs_dirent *)buf; 906 907 n = sz / sizeof(*dir); 908 909 for (i = 0; (i < n) && (p->cnt < p->end); i++, dir++, p->cnt++) { 910 if (p->cnt < p->start) 911 continue; 912 913 if ((error = v7fs_inode_load(fs, &inode, dir->inode_number))) 914 break; 915 916 v7fs_dirent_filename(filename, dir->name); 917 918 DPRINTF("inode=%d name=%s %s\n", dir->inode_number, filename, 919 v7fs_inode_isdir(&inode) ? "DIR" : "FILE"); 920 memset(dp, 0, sizeof(*dp)); 921 dp->d_fileno = dir->inode_number; 922 dp->d_type = v7fs_mode_to_d_type(inode.mode); 923 dp->d_namlen = strlen(filename); 924 strcpy(dp->d_name, filename); 925 dp->d_reclen = sizeof(*dp); 926 if ((error = uiomove(dp, dp->d_reclen, p->uio))) { 927 DPRINTF("uiomove failed.\n"); 928 break; 929 } 930 } 931 scratch_free(fs, buf); 932 933 if (p->cnt == p->end) 934 return V7FS_ITERATOR_BREAK; 935 936 return error; 937} 938 939int 940v7fs_readdir(void *v) 941{ 942 struct vop_readdir_args /* { 943 struct vnode *a_vp; 944 struct uio *a_uio; 945 kauth_cred_t a_cred; 946 int *a_eofflag; 947 off_t **a_cookies; 948 int *a_ncookies; 949 } */ *a = v; 950 struct uio *uio = a->a_uio; 951 struct vnode *vp = a->a_vp; 952 struct v7fs_node *v7node = vp->v_data; 953 struct v7fs_inode *inode = &v7node->inode; 954 struct v7fs_self *fs = v7node->v7fsmount->core; 955 struct dirent *dp; 956 int error; 957 958 DPRINTF("offset=%zu residue=%zu\n", uio->uio_offset, uio->uio_resid); 959 960 KDASSERT(vp->v_type == VDIR); 961 KDASSERT(uio->uio_offset >= 0); 962 KDASSERT(v7fs_inode_isdir(inode)); 963 964 struct v7fs_readdir_arg arg; 965 arg.start = uio->uio_offset / sizeof(*dp); 966 arg.end = arg.start + uio->uio_resid / sizeof(*dp); 967 if (arg.start == arg.end) {/* user buffer has not enuf space. */ 968 DPRINTF("uio buffer too small\n"); 969 return ENOMEM; 970 } 971 dp = malloc(sizeof(*dp), M_V7FS, M_WAITOK | M_ZERO); 972 arg.cnt = 0; 973 arg.dp = dp; 974 arg.uio = uio; 975 976 *a->a_eofflag = false; 977 error = v7fs_datablock_foreach(fs, inode, readdir_subr, &arg); 978 if (error == V7FS_ITERATOR_END) { 979 *a->a_eofflag = true; 980 } 981 if (error < 0) 982 error = 0; 983 984 free(dp, M_V7FS); 985 986 return error; 987} 988 989int 990v7fs_inactive(void *v) 991{ 992 struct vop_inactive_args /* { 993 struct vnode *a_vp; 994 bool *a_recycle; 995 } */ *a = v; 996 struct vnode *vp = a->a_vp; 997 struct v7fs_node *v7node = vp->v_data; 998 struct v7fs_inode *inode = &v7node->inode; 999 1000 DPRINTF("%p #%d\n", vp, inode->inode_number); 1001 if (v7fs_inode_allocated(inode)) { 1002 v7fs_update(vp, 0, 0, UPDATE_WAIT); 1003 *a->a_recycle = false; 1004 } else { 1005 *a->a_recycle = true; 1006 } 1007 1008 VOP_UNLOCK(vp); 1009 1010 return 0; 1011} 1012 1013int 1014v7fs_reclaim(void *v) 1015{ 1016 /*This vnode is no longer referenced by kernel. */ 1017 extern struct pool v7fs_node_pool; 1018 struct vop_reclaim_args /* { 1019 struct vnode *a_vp; 1020 } */ *a = v; 1021 struct vnode *vp = a->a_vp; 1022 struct v7fs_node *v7node = vp->v_data; 1023 1024 DPRINTF("%p #%d\n", vp, v7node->inode.inode_number); 1025 mutex_enter(&mntvnode_lock); 1026 LIST_REMOVE(v7node, link); 1027 mutex_exit(&mntvnode_lock); 1028 genfs_node_destroy(vp); 1029 pool_put(&v7fs_node_pool, v7node); 1030 vp->v_data = NULL; 1031 1032 return 0; 1033} 1034 1035int 1036v7fs_bmap(void *v) 1037{ 1038 struct vop_bmap_args /* { 1039 struct vnode *a_vp; 1040 daddr_t a_bn; 1041 struct vnode **a_vpp; 1042 daddr_t *a_bnp; 1043 int *a_runp; 1044 } */ *a = v; 1045 struct vnode *vp = a->a_vp; 1046 struct v7fs_node *v7node = vp->v_data; 1047 struct v7fs_mount *v7fsmount = v7node->v7fsmount; 1048 struct v7fs_self *fs = v7node->v7fsmount->core; 1049 struct v7fs_inode *inode = &v7node->inode; 1050 int error = 0; 1051 1052 DPRINTF("inode=%d offset=%zu %p\n", inode->inode_number, a->a_bn, vp); 1053 DPRINTF("filesize: %d\n", inode->filesize); 1054 if (!a->a_bnp) 1055 return 0; 1056 1057 v7fs_daddr_t blk; 1058 if (!(blk = v7fs_datablock_last(fs, inode, 1059 (a->a_bn + 1) << V7FS_BSHIFT))) { 1060 /* +1 converts block # to file offset. */ 1061 return ENOSPC; 1062 } 1063 1064 *a->a_bnp = blk; 1065 1066 if (a->a_vpp) 1067 *a->a_vpp = v7fsmount->devvp; 1068 if (a->a_runp) 1069 *a->a_runp = 0; /*XXX TODO */ 1070 1071 DPRINTF("%d %zu->%zu status=%d\n", inode->inode_number, a->a_bn, 1072 *a->a_bnp, error); 1073 1074 return error; 1075} 1076 1077int 1078v7fs_strategy(void *v) 1079{ 1080 struct vop_strategy_args /* { 1081 struct vnode *a_vp; 1082 struct buf *a_bp; 1083 } */ *a = v; 1084 struct buf *b = a->a_bp; 1085 struct vnode *vp = a->a_vp; 1086 struct v7fs_node *v7node = vp->v_data; 1087 struct v7fs_mount *v7fsmount = v7node->v7fsmount; 1088 int error; 1089 1090 DPRINTF("%p\n", vp); 1091 KDASSERT(vp->v_type == VREG); 1092 if (b->b_blkno == b->b_lblkno) { 1093 error = VOP_BMAP(vp, b->b_lblkno, NULL, &b->b_blkno, NULL); 1094 if (error) { 1095 b->b_error = error; 1096 biodone(b); 1097 return error; 1098 } 1099 if ((long)b->b_blkno == -1) 1100 clrbuf(b); 1101 } 1102 if ((long)b->b_blkno == -1) { 1103 biodone(b); 1104 return 0; 1105 } 1106 1107 return VOP_STRATEGY(v7fsmount->devvp, b); 1108} 1109 1110int 1111v7fs_print(void *v) 1112{ 1113 struct vop_print_args /* { 1114 struct vnode *a_vp; 1115 } */ *a = v; 1116 struct v7fs_node *v7node = a->a_vp->v_data; 1117 1118 v7fs_inode_dump(&v7node->inode); 1119 1120 return 0; 1121} 1122 1123int 1124v7fs_advlock(void *v) 1125{ 1126 struct vop_advlock_args /* { 1127 struct vnode *a_vp; 1128 void *a_id; 1129 int a_op; 1130 struct flock *a_fl; 1131 int a_flags; 1132 } */ *a = v; 1133 struct v7fs_node *v7node = a->a_vp->v_data; 1134 1135 DPRINTF("op=%d\n", a->a_op); 1136 1137 return lf_advlock(a, &v7node->lockf, 1138 v7fs_inode_filesize(&v7node->inode)); 1139} 1140 1141int 1142v7fs_pathconf(void *v) 1143{ 1144 struct vop_pathconf_args /* { 1145 struct vnode *a_vp; 1146 int a_name; 1147 register_t *a_retval; 1148 } */ *a = v; 1149 int err = 0; 1150 1151 DPRINTF("%p\n", a->a_vp); 1152 1153 switch (a->a_name) { 1154 case _PC_LINK_MAX: 1155 *a->a_retval = V7FS_LINK_MAX; 1156 break; 1157 case _PC_NAME_MAX: 1158 *a->a_retval = V7FS_NAME_MAX; 1159 break; 1160 case _PC_PATH_MAX: 1161 *a->a_retval = V7FS_PATH_MAX; 1162 break; 1163 case _PC_CHOWN_RESTRICTED: 1164 *a->a_retval = 1; 1165 break; 1166 case _PC_NO_TRUNC: 1167 *a->a_retval = 0; 1168 break; 1169 case _PC_SYNC_IO: 1170 *a->a_retval = 1; 1171 break; 1172 case _PC_FILESIZEBITS: 1173 *a->a_retval = 30; /* ~1G */ 1174 break; 1175 case _PC_SYMLINK_MAX: 1176 *a->a_retval = V7FSBSD_MAXSYMLINKLEN; 1177 break; 1178 case _PC_2_SYMLINKS: 1179 *a->a_retval = 1; 1180 break; 1181 default: 1182 err = EINVAL; 1183 break; 1184 } 1185 1186 return err; 1187} 1188 1189int 1190v7fs_update(struct vnode *vp, const struct timespec *acc, 1191 const struct timespec *mod, int flags) 1192{ 1193 struct v7fs_node *v7node = vp->v_data; 1194 struct v7fs_inode *inode = &v7node->inode; 1195 struct v7fs_self *fs = v7node->v7fsmount->core; 1196 bool update = false; 1197 1198 DPRINTF("%p %zu %d\n", vp, vp->v_size, v7fs_inode_filesize(inode)); 1199 KDASSERT(vp->v_size == v7fs_inode_filesize(inode)); 1200 1201 if (v7node->update_atime) { 1202 inode->atime = acc ? acc->tv_sec : time_second; 1203 v7node->update_atime = false; 1204 update = true; 1205 } 1206 if (v7node->update_ctime) { 1207 inode->ctime = time_second; 1208 v7node->update_ctime = false; 1209 update = true; 1210 } 1211 if (v7node->update_mtime) { 1212 inode->mtime = mod ? mod->tv_sec : time_second; 1213 v7node->update_mtime = false; 1214 update = true; 1215 } 1216 1217 if (update) 1218 v7fs_inode_writeback(fs, inode); 1219 1220 return 0; 1221} 1222 1223int 1224v7fs_symlink(void *v) 1225{ 1226 struct vop_symlink_args /* { 1227 struct vnode *a_dvp; 1228 struct vnode **a_vpp; 1229 struct componentname *a_cnp; 1230 struct vattr *a_vap; 1231 char *a_target; 1232 } */ *a = v; 1233 struct v7fs_node *parent_node = a->a_dvp->v_data; 1234 struct v7fs_mount *v7fsmount = parent_node->v7fsmount; 1235 struct v7fs_self *fs = v7fsmount->core; 1236 struct vattr *va = a->a_vap; 1237 kauth_cred_t cr = a->a_cnp->cn_cred; 1238 struct componentname *cnp = a->a_cnp; 1239 struct v7fs_fileattr attr; 1240 v7fs_ino_t ino; 1241 const char *from = a->a_target; 1242 const char *to = cnp->cn_nameptr; 1243 size_t len = strlen(from) + 1; 1244 int error = 0; 1245 1246 if (len > V7FS_BSIZE) { /* limited to 512byte pathname */ 1247 DPRINTF("too long pathname."); 1248 return ENAMETOOLONG; 1249 } 1250 1251 memset(&attr, 0, sizeof(attr)); 1252 attr.uid = kauth_cred_geteuid(cr); 1253 attr.gid = kauth_cred_getegid(cr); 1254 attr.mode = va->va_mode | vtype_to_v7fs_mode(va->va_type); 1255 1256 if ((error = v7fs_file_allocate 1257 (fs, &parent_node->inode, to, &attr, &ino))) { 1258 goto unlock_exit; 1259 } 1260 /* Sync dirent size change. */ 1261 uvm_vnp_setsize(a->a_dvp, v7fs_inode_filesize(&parent_node->inode)); 1262 1263 /* Get myself vnode. */ 1264 if ((error = v7fs_vget(v7fsmount->mountp, ino, a->a_vpp))) { 1265 DPRINTF("can't get vnode.\n"); 1266 } 1267 1268 struct v7fs_node *newnode = (*a->a_vpp)->v_data; 1269 struct v7fs_inode *p = &newnode->inode; 1270 v7fs_file_symlink(fs, p, from); 1271 uvm_vnp_setsize(*a->a_vpp, v7fs_inode_filesize(p)); 1272 1273 newnode->update_ctime = true; 1274 newnode->update_mtime = true; 1275 newnode->update_atime = true; 1276unlock_exit: 1277 /* unlock parent directory */ 1278 vput(a->a_dvp); 1279 1280 return error; 1281} 1282 1283int 1284v7fs_readlink(void *v) 1285{ 1286 struct vop_readlink_args /* { 1287 struct vnode *a_vp; 1288 struct uio *a_uio; 1289 kauth_cred_t a_cred; 1290 } */ *a = v; 1291 struct uio *uio = a->a_uio; 1292 struct vnode *vp = a->a_vp; 1293 struct v7fs_node *v7node = vp->v_data; 1294 struct v7fs_inode *inode = &v7node->inode; 1295 struct v7fs_self *fs = v7node->v7fsmount->core; 1296 int error = 0; 1297 1298 KDASSERT(vp->v_type == VLNK); 1299 KDASSERT(uio->uio_offset >= 0); 1300 KDASSERT(v7fs_inode_islnk(inode)); 1301 1302 v7fs_daddr_t blk = inode->addr[0]; 1303 void *buf; 1304 if (!(buf = scratch_read(fs, blk))) { 1305 error = EIO; 1306 goto error_exit; 1307 } 1308 1309 if ((error = uiomove(buf, strlen(buf), uio))) { 1310 DPRINTF("uiomove failed.\n"); 1311 } 1312 scratch_free(fs, buf); 1313 1314error_exit: 1315 return error; 1316} 1317