1 /* $NetBSD: udf_vnops.c,v 1.6 2006/06/12 00:20:21 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Reinoud Zandijk 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed for the 18 * NetBSD Project. See http://www.NetBSD.org/ for 19 * information about NetBSD. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 */ 35 36 37 #include <sys/cdefs.h> 38 #ifndef lint 39 __RCSID("$NetBSD: udf_vnops.c,v 1.6 2006/06/12 00:20:21 christos Exp $"); 40 #endif /* not lint */ 41 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/namei.h> 46 #include <sys/resourcevar.h> /* defines plimit structure in proc struct */ 47 #include <sys/kernel.h> 48 #include <sys/file.h> /* define FWRITE ... */ 49 #include <sys/stat.h> 50 #include <sys/buf.h> 51 #include <sys/proc.h> 52 #include <sys/mount.h> 53 #include <sys/vnode.h> 54 #include <sys/signalvar.h> 55 #include <sys/malloc.h> 56 #include <sys/dirent.h> 57 #include <sys/lockf.h> 58 59 #include <miscfs/genfs/genfs.h> 60 #include <uvm/uvm_extern.h> 61 62 #include <fs/udf/ecma167-udf.h> 63 #include <fs/udf/udf_mount.h> 64 #include "udf.h" 65 #include "udf_subr.h" 66 #include "udf_bswap.h" 67 68 69 #define VTOI(vnode) ((struct udf_node *) vnode->v_data) 70 71 72 /* externs */ 73 extern int prtactive; 74 75 76 /* implementations of vnode functions; table follows at end */ 77 /* --------------------------------------------------------------------- */ 78 79 int 80 udf_inactive(void *v) 81 { 82 struct vop_inactive_args /* { 83 struct vnode *a_vp; 84 struct proc *a_p; 85 } */ *ap = v; 86 struct vnode *vp = ap->a_vp; 87 88 if (prtactive && vp->v_usecount != 0) 89 vprint("udf_inactive(): pushing active", vp); 90 91 VOP_UNLOCK(vp, 0); 92 93 DPRINTF(LOCKING, ("udf_inactive called for node %p\n", VTOI(vp))); 94 95 /* 96 * Optionally flush metadata to disc. If the file has not been 97 * referenced anymore in a directory we ought to free up the resources 98 * on disc if applicable. 99 */ 100 101 return 0; 102 } 103 104 /* --------------------------------------------------------------------- */ 105 106 int 107 udf_reclaim(void *v) 108 { 109 struct vop_reclaim_args /* { 110 struct vnode *a_vp; 111 } */ *ap = v; 112 struct vnode *vp = ap->a_vp; 113 struct udf_node *node = VTOI(vp); 114 115 if (prtactive && vp->v_usecount != 0) 116 vprint("udf_reclaim(): pushing active", vp); 117 118 /* purge old data from namei */ 119 cache_purge(vp); 120 121 DPRINTF(LOCKING, ("udf_reclaim called for node %p\n", node)); 122 /* dispose all node knowledge */ 123 udf_dispose_node(node); 124 125 return 0; 126 } 127 128 /* --------------------------------------------------------------------- */ 129 130 int 131 udf_read(void *v) 132 { 133 struct vop_read_args /* { 134 struct vnode *a_vp; 135 struct uio *a_uio; 136 int a_ioflag; 137 kauth_cred_t a_cred; 138 } */ *ap = v; 139 struct vnode *vp = ap->a_vp; 140 struct uio *uio = ap->a_uio; 141 int ioflag = ap->a_ioflag; 142 struct uvm_object *uobj; 143 struct udf_node *udf_node = VTOI(vp); 144 struct file_entry *fe; 145 struct extfile_entry *efe; 146 uint64_t file_size; 147 vsize_t len; 148 void *win; 149 int error; 150 int flags; 151 152 /* XXX how to deal with xtended attributes (files) */ 153 154 DPRINTF(READ, ("udf_read called\n")); 155 156 /* can this happen? some filingsystems have this check */ 157 if (uio->uio_offset < 0) 158 return EINVAL; 159 160 assert(udf_node); 161 assert(udf_node->fe || udf_node->efe); 162 163 /* TODO set access time */ 164 165 /* get directory filesize */ 166 if (udf_node->fe) { 167 fe = udf_node->fe; 168 file_size = udf_rw64(fe->inf_len); 169 } else { 170 assert(udf_node->efe); 171 efe = udf_node->efe; 172 file_size = udf_rw64(efe->inf_len); 173 } 174 175 if (vp->v_type == VDIR) { 176 /* protect against rogue programs reading raw directories */ 177 if ((ioflag & IO_ALTSEMANTICS) == 0) 178 return EISDIR; 179 } 180 if (vp->v_type == VREG || vp->v_type == VDIR) { 181 const int advice = IO_ADV_DECODE(ap->a_ioflag); 182 183 /* read contents using buffercache */ 184 uobj = &vp->v_uobj; 185 flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0; 186 error = 0; 187 while (uio->uio_resid > 0) { 188 /* reached end? */ 189 if (file_size <= uio->uio_offset) 190 break; 191 192 /* maximise length to file extremity */ 193 len = MIN(file_size - uio->uio_offset, uio->uio_resid); 194 if (len == 0) 195 break; 196 197 /* ubc, here we come, prepare to trap */ 198 win = ubc_alloc(uobj, uio->uio_offset, &len, 199 advice, UBC_READ); 200 error = uiomove(win, len, uio); 201 ubc_release(win, flags); 202 if (error) 203 break; 204 } 205 /* TODO note access time */ 206 return error; 207 } 208 209 return EINVAL; 210 } 211 212 /* --------------------------------------------------------------------- */ 213 214 int 215 udf_write(void *v) 216 { 217 struct vop_write_args /* { 218 struct vnode *a_vp; 219 struct uio *a_uio; 220 int a_ioflag; 221 kauth_cred_t a_cred; 222 } */ *ap = v; 223 224 DPRINTF(NOTIMPL, ("udf_write called\n")); 225 ap = ap; /* shut up gcc */ 226 227 /* TODO implement writing */ 228 return EROFS; 229 } 230 231 232 /* --------------------------------------------------------------------- */ 233 234 /* 235 * `Special' bmap functionality that translates all incomming requests to 236 * translate to vop_strategy() calls with the same blocknumbers effectively 237 * not translating at all. 238 */ 239 240 int 241 udf_trivial_bmap(void *v) 242 { 243 struct vop_bmap_args /* { 244 struct vnode *a_vp; 245 daddr_t a_bn; 246 struct vnode **a_vpp; 247 daddr_t *a_bnp; 248 int *a_runp; 249 } */ *ap = v; 250 struct vnode *vp = ap->a_vp; /* our node */ 251 struct vnode **vpp = ap->a_vpp; /* return node */ 252 daddr_t *bnp = ap->a_bnp; /* translated */ 253 daddr_t bn = ap->a_bn; /* origional */ 254 int *runp = ap->a_runp; 255 struct udf_node *udf_node = VTOI(vp); 256 uint32_t lb_size; 257 258 /* get logical block size */ 259 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size); 260 261 /* could return `-1' to indicate holes/zeros */ 262 /* translate 1:1 */ 263 *bnp = bn; 264 265 /* set the vnode to read the data from with strategy on itself */ 266 if (vpp) 267 *vpp = vp; 268 269 /* set runlength of maximum block size */ 270 if (runp) 271 *runp = MAXPHYS / lb_size; /* or with -1 ? */ 272 273 /* return success */ 274 return 0; 275 } 276 277 /* --------------------------------------------------------------------- */ 278 279 int 280 udf_strategy(void *v) 281 { 282 struct vop_strategy_args /* { 283 struct vnode *a_vp; 284 struct buf *a_bp; 285 } */ *ap = v; 286 struct vnode *vp = ap->a_vp; 287 struct buf *bp = ap->a_bp; 288 struct udf_node *udf_node = VTOI(vp); 289 uint32_t lb_size, from, sectors; 290 int error; 291 292 DPRINTF(STRATEGY, ("udf_strategy called\n")); 293 294 /* check if we ought to be here */ 295 if (vp->v_type == VBLK || vp->v_type == VCHR) 296 panic("udf_strategy: spec"); 297 298 /* only filebuffers ought to be read by this, no descriptors */ 299 assert(bp->b_blkno >= 0); 300 301 /* get sector size */ 302 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size); 303 304 /* calculate sector to start from */ 305 from = bp->b_blkno; 306 307 /* calculate length to fetch/store in sectors */ 308 sectors = bp->b_bcount / lb_size; 309 assert(bp->b_bcount > 0); 310 311 /* NEVER assume later that this buffer is already translated */ 312 /* bp->b_lblkno = bp->b_blkno; */ 313 314 DPRINTF(STRATEGY, ("\tread vp %p buf %p (blk no %"PRIu64")" 315 ", sector %d for %d sectors\n", 316 vp, bp, bp->b_blkno, from, sectors)); 317 318 /* check assertions: we OUGHT to allways get multiples of this */ 319 assert(sectors * lb_size == bp->b_bcount); 320 321 /* determine mode */ 322 error = 0; 323 if (bp->b_flags & B_READ) { 324 /* read buffer from the udf_node, translate vtop on the way*/ 325 udf_read_filebuf(udf_node, bp); 326 return bp->b_error; 327 } 328 329 printf("udf_strategy: can't write yet\n"); 330 return ENOTSUP; 331 } 332 333 /* --------------------------------------------------------------------- */ 334 335 int 336 udf_readdir(void *v) 337 { 338 struct vop_readdir_args /* { 339 struct vnode *a_vp; 340 struct uio *a_uio; 341 kauth_cred_t a_cred; 342 int *a_eofflag; 343 off_t **a_cookies; 344 int *a_ncookies; 345 } */ *ap = v; 346 struct uio *uio = ap->a_uio; 347 struct vnode *vp = ap->a_vp; 348 struct udf_node *udf_node = VTOI(vp); 349 struct file_entry *fe; 350 struct extfile_entry *efe; 351 struct fileid_desc *fid; 352 struct dirent dirent; 353 uint64_t file_size, diroffset, transoffset; 354 uint32_t lb_size; 355 int error; 356 357 DPRINTF(READDIR, ("udf_readdir called\n")); 358 359 /* This operation only makes sense on directory nodes. */ 360 if (vp->v_type != VDIR) 361 return ENOTDIR; 362 363 /* get directory filesize */ 364 if (udf_node->fe) { 365 fe = udf_node->fe; 366 file_size = udf_rw64(fe->inf_len); 367 } else { 368 assert(udf_node->efe); 369 efe = udf_node->efe; 370 file_size = udf_rw64(efe->inf_len); 371 } 372 373 /* 374 * Add `.' pseudo entry if at offset zero since its not in the fid 375 * stream 376 */ 377 if (uio->uio_offset == 0) { 378 DPRINTF(READDIR, ("\t'.' inserted\n")); 379 memset(&dirent, 0, sizeof(struct dirent)); 380 strcpy(dirent.d_name, "."); 381 dirent.d_fileno = udf_calchash(&udf_node->loc); 382 dirent.d_type = DT_DIR; 383 dirent.d_namlen = strlen(dirent.d_name); 384 dirent.d_reclen = _DIRENT_SIZE(&dirent); 385 uiomove(&dirent, _DIRENT_SIZE(&dirent), uio); 386 387 /* mark with magic value that we have done the dummy */ 388 uio->uio_offset = UDF_DIRCOOKIE_DOT; 389 } 390 391 /* we are called just as long as we keep on pushing data in */ 392 error = 0; 393 if ((uio->uio_offset < file_size) && 394 (uio->uio_resid >= sizeof(struct dirent))) { 395 /* allocate temporary space for fid */ 396 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size); 397 fid = malloc(lb_size, M_TEMP, M_WAITOK); 398 399 if (uio->uio_offset == UDF_DIRCOOKIE_DOT) 400 uio->uio_offset = 0; 401 402 diroffset = uio->uio_offset; 403 transoffset = diroffset; 404 while (diroffset < file_size) { 405 DPRINTF(READDIR, ("\tread in fid stream\n")); 406 /* transfer a new fid/dirent */ 407 error = udf_read_fid_stream(vp, &diroffset, 408 fid, &dirent); 409 DPRINTFIF(READDIR, error, ("read error in read fid " 410 "stream : %d\n", error)); 411 if (error) 412 break; 413 414 /* 415 * If there isn't enough space in the uio to return a 416 * whole dirent, break off read 417 */ 418 if (uio->uio_resid < _DIRENT_SIZE(&dirent)) 419 break; 420 421 /* remember the last entry we transfered */ 422 transoffset = diroffset; 423 424 /* skip deleted entries */ 425 if (fid->file_char & UDF_FILE_CHAR_DEL) 426 continue; 427 428 /* skip not visible files */ 429 if (fid->file_char & UDF_FILE_CHAR_VIS) 430 continue; 431 432 /* copy dirent to the caller */ 433 DPRINTF(READDIR, ("\tread dirent `%s', type %d\n", 434 dirent.d_name, dirent.d_type)); 435 uiomove(&dirent, _DIRENT_SIZE(&dirent), uio); 436 } 437 438 /* pass on last transfered offset */ 439 uio->uio_offset = transoffset; 440 free(fid, M_TEMP); 441 } 442 443 if (ap->a_eofflag) 444 *ap->a_eofflag = (uio->uio_offset == file_size); 445 446 #ifdef DEBUG 447 if (udf_verbose & UDF_DEBUG_READDIR) { 448 printf("returning offset %d\n", (uint32_t) uio->uio_offset); 449 if (ap->a_eofflag) 450 printf("returning EOF ? %d\n", *ap->a_eofflag); 451 if (error) 452 printf("readdir returning error %d\n", error); 453 } 454 #endif 455 456 return error; 457 } 458 459 /* --------------------------------------------------------------------- */ 460 461 int 462 udf_lookup(void *v) 463 { 464 struct vop_lookup_args /* { 465 struct vnode *a_dvp; 466 struct vnode **a_vpp; 467 struct componentname *a_cnp; 468 } */ *ap = v; 469 struct vnode *dvp = ap->a_dvp; 470 struct vnode **vpp = ap->a_vpp; 471 struct componentname *cnp = ap->a_cnp; 472 struct udf_node *dir_node, *res_node; 473 struct udf_mount *ump; 474 struct long_ad icb_loc; 475 const char *name; 476 int namelen, nameiop, islastcn, lock_parent, mounted_ro; 477 int vnodetp; 478 int error, found; 479 480 dir_node = VTOI(dvp); 481 ump = dir_node->ump; 482 *vpp = NULL; 483 484 DPRINTF(LOOKUP, ("udf_lookup called\n")); 485 486 /* simplify/clarification flags */ 487 nameiop = cnp->cn_nameiop; 488 islastcn = cnp->cn_flags & ISLASTCN; 489 lock_parent = cnp->cn_flags & LOCKPARENT; 490 mounted_ro = dvp->v_mount->mnt_flag & MNT_RDONLY; 491 492 /* check exec/dirread permissions first */ 493 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_lwp); 494 if (error) 495 return error; 496 497 DPRINTF(LOOKUP, ("\taccess ok\n")); 498 499 /* 500 * If requesting a modify on the last path element on a read-only 501 * filingsystem, reject lookup; XXX why is this repeated in every FS ? 502 */ 503 if (islastcn && mounted_ro && (nameiop == DELETE || nameiop == RENAME)) 504 return EROFS; 505 506 DPRINTF(LOOKUP, ("\tlooking up cnp->cn_nameptr '%s'\n", 507 cnp->cn_nameptr)); 508 /* look in the nami cache; returns 0 on success!! */ 509 error = cache_lookup(dvp, vpp, cnp); 510 if (error >= 0) 511 return error; 512 513 DPRINTF(LOOKUP, ("\tNOT found in cache\n")); 514 515 /* 516 * Obviously, the file is not (anymore) in the namecache, we have to 517 * search for it. There are three basic cases: '.', '..' and others. 518 * 519 * Following the guidelines of VOP_LOOKUP manpage and tmpfs. 520 */ 521 error = 0; 522 if ((cnp->cn_namelen == 1) && (cnp->cn_nameptr[0] == '.')) { 523 DPRINTF(LOOKUP, ("\tlookup '.'\n")); 524 /* special case 1 '.' */ 525 VREF(dvp); 526 *vpp = dvp; 527 /* done */ 528 } else if (cnp->cn_flags & ISDOTDOT) { 529 /* special case 2 '..' */ 530 DPRINTF(LOOKUP, ("\tlookup '..'\n")); 531 532 /* first unlock parent */ 533 VOP_UNLOCK(dvp, 0); 534 cnp->cn_flags |= PDIRUNLOCK; 535 536 /* get our node */ 537 name = ".."; 538 namelen = 2; 539 found = udf_lookup_name_in_dir(dvp, name, namelen, &icb_loc); 540 if (!found) 541 error = ENOENT; 542 543 if (!error) { 544 DPRINTF(LOOKUP, ("\tfound '..'\n")); 545 /* try to create/reuse the node */ 546 error = udf_get_node(ump, &icb_loc, &res_node); 547 548 if (!error) { 549 DPRINTF(LOOKUP, ("\tnode retrieved/created OK\n")); 550 *vpp = res_node->vnode; 551 } 552 } 553 554 /* see if we're requested to lock the parent */ 555 if (lock_parent && islastcn) { 556 if (vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY) == 0) 557 cnp->cn_flags &= ~PDIRUNLOCK; 558 } 559 /* done */ 560 } else { 561 DPRINTF(LOOKUP, ("\tlookup file\n")); 562 /* all other files */ 563 /* lookup filename in the directory; location icb_loc */ 564 name = cnp->cn_nameptr; 565 namelen = cnp->cn_namelen; 566 found = udf_lookup_name_in_dir(dvp, name, namelen, &icb_loc); 567 if (!found) { 568 DPRINTF(LOOKUP, ("\tNOT found\n")); 569 /* 570 * UGH, didn't find name. If we're creating or naming 571 * on the last name this is OK and we ought to return 572 * EJUSTRETURN if its allowed to be created. 573 */ 574 error = ENOENT; 575 if (islastcn && 576 (nameiop == CREATE || nameiop == RENAME)) 577 error = 0; 578 if (!error) { 579 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, 580 cnp->cn_lwp); 581 if (!error) { 582 /* keep the component name */ 583 cnp->cn_flags |= SAVENAME; 584 error = EJUSTRETURN; 585 } 586 } 587 /* done */ 588 } else { 589 /* try to create/reuse the node */ 590 error = udf_get_node(ump, &icb_loc, &res_node); 591 if (!error) { 592 /* 593 * If we are not at the last path component 594 * and found a non-directory or non-link entry 595 * (which may itself be pointing to a 596 * directory), raise an error. 597 */ 598 vnodetp = res_node->vnode->v_type; 599 if ((vnodetp != VDIR) && (vnodetp != VLNK)) { 600 if (!islastcn) 601 error = ENOTDIR; 602 } 603 604 } 605 if (!error) { 606 /* 607 * If LOCKPARENT or ISLASTCN is not set, dvp 608 * is returned unlocked on a successful 609 * lookup. 610 */ 611 *vpp = res_node->vnode; 612 if (!lock_parent || !islastcn) 613 VOP_UNLOCK(dvp, 0); 614 } 615 /* done */ 616 } 617 /* done */ 618 } 619 620 /* 621 * Store result in the cache if requested. If we are creating a file, 622 * the file might not be found and thus putting it into the namecache 623 * might be seen as negative caching. 624 */ 625 if (cnp->cn_flags & MAKEENTRY) 626 cache_enter(dvp, *vpp, cnp); 627 628 DPRINTFIF(LOOKUP, error, ("udf_lookup returing error %d\n", error)); 629 630 return error; 631 } 632 633 /* --------------------------------------------------------------------- */ 634 635 int 636 udf_getattr(void *v) 637 { 638 struct vop_getattr_args /* { 639 struct vnode *a_vp; 640 struct vattr *a_vap; 641 kauth_cred_t a_cred; 642 struct proc *a_p; 643 } */ *ap = v; 644 struct vnode *vp = ap->a_vp; 645 struct udf_node *udf_node = VTOI(vp); 646 struct udf_mount *ump = udf_node->ump; 647 struct vattr *vap = ap->a_vap; 648 struct file_entry *fe; 649 struct extfile_entry *efe; 650 struct timestamp *atime, *mtime, *attrtime; 651 uint32_t nlink; 652 uint64_t filesize, blkssize; 653 uid_t uid; 654 gid_t gid; 655 656 DPRINTF(CALL, ("udf_getattr called\n")); 657 658 /* get descriptor information */ 659 if (udf_node->fe) { 660 fe = udf_node->fe; 661 nlink = udf_rw16(fe->link_cnt); 662 uid = (uid_t)udf_rw32(fe->uid); 663 gid = (gid_t)udf_rw32(fe->gid); 664 filesize = udf_rw64(fe->inf_len); 665 blkssize = udf_rw64(fe->logblks_rec); 666 atime = &fe->atime; 667 mtime = &fe->mtime; 668 attrtime = &fe->attrtime; 669 } else { 670 assert(udf_node->efe); 671 efe = udf_node->efe; 672 nlink = udf_rw16(efe->link_cnt); 673 uid = (uid_t)udf_rw32(efe->uid); 674 gid = (gid_t)udf_rw32(efe->gid); 675 filesize = udf_rw64(efe->inf_len); /* XXX or obj_size? */ 676 blkssize = udf_rw64(efe->logblks_rec); 677 atime = &efe->atime; 678 mtime = &efe->mtime; 679 attrtime = &efe->attrtime; 680 } 681 682 /* do the uid/gid translation game */ 683 if ((uid == (uid_t) -1) && (gid == (gid_t) -1)) { 684 uid = ump->mount_args.anon_uid; 685 gid = ump->mount_args.anon_gid; 686 } 687 688 /* fill in struct vattr with values from the node */ 689 VATTR_NULL(vap); 690 vap->va_type = vp->v_type; 691 vap->va_mode = udf_getaccessmode(udf_node); 692 vap->va_nlink = nlink; 693 vap->va_uid = uid; 694 vap->va_gid = gid; 695 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0]; 696 vap->va_fileid = udf_calchash(&udf_node->loc); /* inode hash XXX */ 697 vap->va_size = filesize; 698 vap->va_blocksize = udf_node->ump->discinfo.sector_size; /* wise? */ 699 700 /* 701 * BUG-ALERT: UDF doesn't count '.' as an entry, so we'll have to add 702 * 1 to the link count if its a directory we're requested attributes 703 * of. 704 */ 705 if (vap->va_type == VDIR) 706 vap->va_nlink++; 707 708 /* access times */ 709 udf_timestamp_to_timespec(ump, atime, &vap->va_atime); 710 udf_timestamp_to_timespec(ump, mtime, &vap->va_mtime); 711 udf_timestamp_to_timespec(ump, attrtime, &vap->va_ctime); 712 713 vap->va_gen = 1; /* no multiple generations yes (!?) */ 714 vap->va_flags = 0; /* no flags */ 715 vap->va_rdev = udf_node->rdev; 716 vap->va_bytes = udf_node->ump->discinfo.sector_size * blkssize; 717 vap->va_filerev = 1; /* TODO file revision numbers? */ 718 vap->va_vaflags = 0; /* TODO which va_vaflags? */ 719 720 return 0; 721 } 722 723 /* --------------------------------------------------------------------- */ 724 725 int 726 udf_setattr(void *v) 727 { 728 struct vop_setattr_args /* { 729 struct vnode *a_vp; 730 struct vattr *a_vap; 731 kauth_cred_t a_cred; 732 struct proc *a_p; 733 } */ *ap = v; 734 struct vnode *vp = ap->a_vp; 735 struct udf_node *udf_node = VTOI(vp); 736 struct udf_mount *ump = udf_node->ump; 737 struct vattr *vap = ap->a_vap; 738 uid_t uid, nobody_uid; 739 gid_t gid, nobody_gid; 740 741 /* shut up gcc for now */ 742 ap = ap; 743 vp = vp; 744 uid = 0; /* XXX gcc */ 745 gid = 0; /* XXX gcc */ 746 udf_node = udf_node; 747 748 DPRINTF(NOTIMPL, ("udf_setattr called\n")); 749 750 /* 751 * BUG-ALERT: UDF doesn't count '.' as an entry, so we'll have to 752 * subtract 1 to the link count if its a directory we're setting 753 * attributes on. See getattr. 754 */ 755 if (vap->va_type == VDIR) 756 vap->va_nlink--; 757 758 /* do the uid/gid translation game */ 759 nobody_uid = ump->mount_args.nobody_uid; 760 nobody_gid = ump->mount_args.nobody_gid; 761 if ((uid == nobody_uid) && (gid == nobody_gid)) { 762 uid = (uid_t) -1; 763 gid = (gid_t) -1; 764 } 765 766 /* TODO implement setattr!! NOT IMPLEMENTED yet */ 767 return 0; 768 } 769 770 /* --------------------------------------------------------------------- */ 771 772 /* 773 * Return POSIX pathconf information for UDF file systems. 774 */ 775 int 776 udf_pathconf(void *v) 777 { 778 struct vop_pathconf_args /* { 779 struct vnode *a_vp; 780 int a_name; 781 register_t *a_retval; 782 } */ *ap = v; 783 struct vnode *vp = ap->a_vp; 784 struct udf_node *udf_node = VTOI(vp); 785 uint32_t bits; 786 787 DPRINTF(CALL, ("udf_pathconf called\n")); 788 789 switch (ap->a_name) { 790 case _PC_LINK_MAX: 791 *ap->a_retval = (1<<16)-1; /* 16 bits */ 792 return 0; 793 case _PC_NAME_MAX: 794 *ap->a_retval = NAME_MAX; 795 return 0; 796 case _PC_PATH_MAX: 797 *ap->a_retval = PATH_MAX; 798 return 0; 799 case _PC_PIPE_BUF: 800 *ap->a_retval = PIPE_BUF; 801 return 0; 802 case _PC_CHOWN_RESTRICTED: 803 *ap->a_retval = 1; 804 return 0; 805 case _PC_NO_TRUNC: 806 *ap->a_retval = 1; 807 return 0; 808 case _PC_SYNC_IO: 809 *ap->a_retval = 0; /* synchronised is off for performance */ 810 return 0; 811 case _PC_FILESIZEBITS: 812 bits = 32; 813 if (udf_node) 814 bits = 32 * vp->v_mount->mnt_dev_bshift; 815 *ap->a_retval = bits; 816 return 0; 817 } 818 819 return EINVAL; 820 } 821 822 823 /* --------------------------------------------------------------------- */ 824 825 int 826 udf_open(void *v) 827 { 828 struct vop_open_args /* { 829 struct vnode *a_vp; 830 int a_mode; 831 kauth_cred_t a_cred; 832 struct proc *a_p; 833 } */ *ap; 834 835 DPRINTF(CALL, ("udf_open called\n")); 836 ap = 0; /* XXX gcc */ 837 838 return 0; 839 } 840 841 842 /* --------------------------------------------------------------------- */ 843 844 int 845 udf_close(void *v) 846 { 847 struct vop_close_args /* { 848 struct vnode *a_vp; 849 int a_fflag; 850 kauth_cred_t a_cred; 851 struct proc *a_p; 852 } */ *ap = v; 853 struct vnode *vp = ap->a_vp; 854 struct udf_node *udf_node = VTOI(vp); 855 856 DPRINTF(CALL, ("udf_close called\n")); 857 udf_node = udf_node; /* shut up gcc */ 858 859 simple_lock(&vp->v_interlock); 860 if (vp->v_usecount > 1) { 861 /* TODO update times */ 862 } 863 simple_unlock(&vp->v_interlock); 864 865 return 0; 866 } 867 868 869 /* --------------------------------------------------------------------- */ 870 871 int 872 udf_access(void *v) 873 { 874 struct vop_access_args /* { 875 struct vnode *a_vp; 876 int a_mode; 877 kauth_cred_t a_cred; 878 struct proc *a_p; 879 } */ *ap = v; 880 struct vnode *vp = ap->a_vp; 881 mode_t mode = ap->a_mode; 882 kauth_cred_t cred = ap->a_cred; 883 struct udf_node *udf_node = VTOI(vp); 884 struct file_entry *fe; 885 struct extfile_entry *efe; 886 mode_t node_mode; 887 uid_t uid; 888 gid_t gid; 889 890 DPRINTF(CALL, ("udf_access called\n")); 891 892 /* get access mode to compare to */ 893 node_mode = udf_getaccessmode(udf_node); 894 895 /* get uid/gid pair */ 896 if (udf_node->fe) { 897 fe = udf_node->fe; 898 uid = (uid_t)udf_rw32(fe->uid); 899 gid = (gid_t)udf_rw32(fe->gid); 900 } else { 901 assert(udf_node->efe); 902 efe = udf_node->efe; 903 uid = (uid_t)udf_rw32(efe->uid); 904 gid = (gid_t)udf_rw32(efe->gid); 905 } 906 907 /* check if we are allowed to write */ 908 switch (vp->v_type) { 909 case VDIR: 910 case VLNK: 911 case VREG: 912 /* 913 * normal nodes: check if we're on a read-only mounted 914 * filingsystem and bomb out if we're trying to write. 915 */ 916 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) 917 return EROFS; 918 break; 919 case VBLK: 920 case VCHR: 921 case VSOCK: 922 case VFIFO: 923 /* 924 * special nodes: even on read-only mounted filingsystems 925 * these are allowed to be written to if permissions allow. 926 */ 927 break; 928 default: 929 /* no idea what this is */ 930 return EINVAL; 931 } 932 933 /* TODO support for chflags checking i.e. IMMUTABLE flag */ 934 935 /* ask the generic vaccess to advice on security */ 936 return vaccess(vp->v_type, node_mode, uid, gid, mode, cred); 937 } 938 939 /* --------------------------------------------------------------------- */ 940 941 int 942 udf_create(void *v) 943 { 944 struct vop_create_args /* { 945 struct vnode *a_dvp; 946 struct vnode **a_vpp; 947 struct componentname *a_cnp; 948 struct vattr *a_vap; 949 } */ *ap = v; 950 struct componentname *cnp = ap->a_cnp; 951 952 DPRINTF(NOTIMPL, ("udf_create called\n")); 953 954 /* error out */ 955 PNBUF_PUT(cnp->cn_pnbuf); 956 vput(ap->a_dvp); 957 return ENOTSUP; 958 } 959 960 /* --------------------------------------------------------------------- */ 961 962 int 963 udf_mknod(void *v) 964 { 965 struct vop_mknod_args /* { 966 struct vnode *a_dvp; 967 struct vnode **a_vpp; 968 struct componentname *a_cnp; 969 struct vattr *a_vap; 970 } */ *ap = v; 971 972 DPRINTF(NOTIMPL, ("udf_mknod called\n")); 973 974 /* error out */ 975 PNBUF_PUT(ap->a_cnp->cn_pnbuf); 976 vput(ap->a_dvp); 977 return ENOTSUP; 978 } 979 980 /* --------------------------------------------------------------------- */ 981 982 int 983 udf_remove(void *v) 984 { 985 struct vop_remove_args /* { 986 struct vnode *a_dvp; 987 struct vnode *a_vp; 988 struct componentname *a_cnp; 989 } */ *ap = v; 990 struct vnode *dvp = ap->a_dvp; 991 struct vnode *vp = ap->a_vp; 992 993 DPRINTF(NOTIMPL, ("udf_remove called\n")); 994 995 /* error out */ 996 vput(dvp); 997 vput(vp); 998 999 return ENOTSUP; 1000 } 1001 1002 /* --------------------------------------------------------------------- */ 1003 1004 int 1005 udf_link(void *v) 1006 { 1007 struct vop_link_args /* { 1008 struct vnode *a_dvp; 1009 struct vnode *a_vp; 1010 struct componentname *a_cnp; 1011 } */ *ap = v; 1012 struct vnode *dvp = ap->a_dvp; 1013 struct vnode *vp = ap->a_vp; 1014 struct componentname *cnp = ap->a_cnp; 1015 1016 DPRINTF(NOTIMPL, ("udf_link called\n")); 1017 1018 /* error out */ 1019 /* XXX or just VOP_ABORTOP(dvp, a_cnp); ? */ 1020 if (VOP_ISLOCKED(vp)) 1021 VOP_UNLOCK(vp, 0); 1022 PNBUF_PUT(cnp->cn_pnbuf); 1023 vput(dvp); 1024 1025 return ENOTSUP; 1026 } 1027 1028 /* --------------------------------------------------------------------- */ 1029 1030 int 1031 udf_symlink(void *v) 1032 { 1033 struct vop_symlink_args /* { 1034 struct vnode *a_dvp; 1035 struct vnode **a_vpp; 1036 struct componentname *a_cnp; 1037 struct vattr *a_vap; 1038 char *a_target; 1039 } */ *ap = v; 1040 struct vnode *dvp = ap->a_dvp; 1041 struct componentname *cnp = ap->a_cnp; 1042 1043 DPRINTF(NOTIMPL, ("udf_symlink called\n")); 1044 1045 /* error out */ 1046 VOP_ABORTOP(dvp, cnp); 1047 vput(dvp); 1048 1049 return ENOTSUP; 1050 } 1051 1052 /* --------------------------------------------------------------------- */ 1053 1054 int 1055 udf_readlink(void *v) 1056 { 1057 struct vop_readlink_args /* { 1058 struct vnode *a_vp; 1059 struct uio *a_uio; 1060 kauth_cred_t a_cred; 1061 } */ *ap = v; 1062 #ifdef notyet 1063 struct vnode *vp = ap->a_vp; 1064 struct uio *uio = ap->a_uio; 1065 kauth_cred_t cred = ap->a_cred; 1066 #endif 1067 1068 ap = ap; /* shut up gcc */ 1069 1070 DPRINTF(NOTIMPL, ("udf_readlink called\n")); 1071 1072 /* TODO read `file' contents and process pathcomponents into a path */ 1073 return ENOTSUP; 1074 } 1075 1076 /* --------------------------------------------------------------------- */ 1077 1078 int 1079 udf_rename(void *v) 1080 { 1081 struct vop_rename_args /* { 1082 struct vnode *a_fdvp; 1083 struct vnode *a_fvp; 1084 struct componentname *a_fcnp; 1085 struct vnode *a_tdvp; 1086 struct vnode *a_tvp; 1087 struct componentname *a_tcnp; 1088 } */ *ap = v; 1089 struct vnode *tvp = ap->a_tvp; 1090 struct vnode *tdvp = ap->a_tdvp; 1091 struct vnode *fvp = ap->a_fvp; 1092 struct vnode *fdvp = ap->a_fdvp; 1093 1094 DPRINTF(NOTIMPL, ("udf_rename called\n")); 1095 1096 /* error out */ 1097 if (tdvp == tvp) 1098 vrele(tdvp); 1099 else 1100 vput(tdvp); 1101 if (tvp != NULL) 1102 vput(tvp); 1103 1104 /* release source nodes. */ 1105 vrele(fdvp); 1106 vrele(fvp); 1107 1108 return ENOTSUP; 1109 } 1110 1111 /* --------------------------------------------------------------------- */ 1112 1113 int 1114 udf_mkdir(void *v) 1115 { 1116 struct vop_mkdir_args /* { 1117 struct vnode *a_dvp; 1118 struct vnode **a_vpp; 1119 struct componentname *a_cnp; 1120 struct vattr *a_vap; 1121 } */ *ap = v; 1122 struct vnode *dvp = ap->a_dvp; 1123 struct componentname *cnp = ap->a_cnp; 1124 1125 DPRINTF(NOTIMPL, ("udf_mkdir called\n")); 1126 1127 /* error out */ 1128 PNBUF_PUT(cnp->cn_pnbuf); 1129 vput(dvp); 1130 1131 return ENOTSUP; 1132 } 1133 1134 /* --------------------------------------------------------------------- */ 1135 1136 int 1137 udf_rmdir(void *v) 1138 { 1139 struct vop_rmdir_args /* { 1140 struct vnode *a_dvp; 1141 struct vnode *a_vp; 1142 struct componentname *a_cnp; 1143 } */ *ap = v; 1144 struct vnode *vp = ap->a_vp; 1145 struct vnode *dvp = ap->a_dvp; 1146 struct componentname *cnp = ap->a_cnp; 1147 int error; 1148 1149 cnp = cnp; /* shut up gcc */ 1150 1151 DPRINTF(NOTIMPL, ("udf_rmdir called\n")); 1152 1153 error = ENOTSUP; 1154 /* error out */ 1155 if (error != 0) { 1156 vput(dvp); 1157 vput(vp); 1158 } 1159 1160 return error; 1161 } 1162 1163 /* --------------------------------------------------------------------- */ 1164 1165 int 1166 udf_fsync(void *v) 1167 { 1168 struct vop_fsync_args /* { 1169 struct vnode *a_vp; 1170 kauth_cred_t a_cred; 1171 int a_flags; 1172 off_t offlo; 1173 off_t offhi; 1174 struct proc *a_p; 1175 } */ *ap = v; 1176 struct vnode *vp = ap->a_vp; 1177 1178 DPRINTF(NOTIMPL, ("udf_fsync called\n")); 1179 1180 vp = vp; /* shut up gcc */ 1181 1182 return 0; 1183 } 1184 1185 /* --------------------------------------------------------------------- */ 1186 1187 int 1188 udf_advlock(void *v) 1189 { 1190 struct vop_advlock_args /* { 1191 struct vnode *a_vp; 1192 void *a_id; 1193 int a_op; 1194 struct flock *a_fl; 1195 int a_flags; 1196 } */ *ap = v; 1197 struct vnode *vp = ap->a_vp; 1198 struct udf_node *udf_node = VTOI(vp); 1199 struct file_entry *fe; 1200 struct extfile_entry *efe; 1201 uint64_t file_size; 1202 1203 DPRINTF(LOCKING, ("udf_advlock called\n")); 1204 1205 /* get directory filesize */ 1206 if (udf_node->fe) { 1207 fe = udf_node->fe; 1208 file_size = udf_rw64(fe->inf_len); 1209 } else { 1210 assert(udf_node->efe); 1211 efe = udf_node->efe; 1212 file_size = udf_rw64(efe->inf_len); 1213 } 1214 1215 return lf_advlock(ap, &udf_node->lockf, file_size); 1216 } 1217 1218 /* --------------------------------------------------------------------- */ 1219 1220 1221 /* Global vfs vnode data structures for udfs */ 1222 int (**udf_vnodeop_p) __P((void *)); 1223 1224 const struct vnodeopv_entry_desc udf_vnodeop_entries[] = { 1225 { &vop_default_desc, vn_default_error }, 1226 { &vop_lookup_desc, udf_lookup }, /* lookup */ 1227 { &vop_create_desc, udf_create }, /* create */ /* TODO */ 1228 { &vop_mknod_desc, udf_mknod }, /* mknod */ /* TODO */ 1229 { &vop_open_desc, udf_open }, /* open */ 1230 { &vop_close_desc, udf_close }, /* close */ 1231 { &vop_access_desc, udf_access }, /* access */ 1232 { &vop_getattr_desc, udf_getattr }, /* getattr */ 1233 { &vop_setattr_desc, udf_setattr }, /* setattr */ /* TODO */ 1234 { &vop_read_desc, udf_read }, /* read */ 1235 { &vop_write_desc, udf_write }, /* write */ /* WRITE */ 1236 { &vop_lease_desc, genfs_lease_check }, /* lease */ /* TODO? */ 1237 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */ /* TODO? */ 1238 { &vop_ioctl_desc, genfs_enoioctl }, /* ioctl */ /* TODO? */ 1239 { &vop_poll_desc, genfs_poll }, /* poll */ /* TODO/OK? */ 1240 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */ /* ? */ 1241 { &vop_revoke_desc, genfs_revoke }, /* revoke */ /* TODO? */ 1242 { &vop_mmap_desc, genfs_mmap }, /* mmap */ /* OK? */ 1243 { &vop_fsync_desc, udf_fsync }, /* fsync */ /* TODO */ 1244 { &vop_seek_desc, genfs_seek }, /* seek */ 1245 { &vop_remove_desc, udf_remove }, /* remove */ /* TODO */ 1246 { &vop_link_desc, udf_link }, /* link */ /* TODO */ 1247 { &vop_rename_desc, udf_rename }, /* rename */ /* TODO */ 1248 { &vop_mkdir_desc, udf_mkdir }, /* mkdir */ /* TODO */ 1249 { &vop_rmdir_desc, udf_rmdir }, /* rmdir */ /* TODO */ 1250 { &vop_symlink_desc, udf_symlink }, /* symlink */ /* TODO */ 1251 { &vop_readdir_desc, udf_readdir }, /* readdir */ 1252 { &vop_readlink_desc, udf_readlink }, /* readlink */ /* TEST ME */ 1253 { &vop_abortop_desc, genfs_abortop }, /* abortop */ /* TODO/OK? */ 1254 { &vop_inactive_desc, udf_inactive }, /* inactive */ 1255 { &vop_reclaim_desc, udf_reclaim }, /* reclaim */ 1256 { &vop_lock_desc, genfs_lock }, /* lock */ 1257 { &vop_unlock_desc, genfs_unlock }, /* unlock */ 1258 { &vop_bmap_desc, udf_trivial_bmap }, /* bmap */ /* 1:1 bmap */ 1259 { &vop_strategy_desc, udf_strategy }, /* strategy */ 1260 /* { &vop_print_desc, udf_print }, */ /* print */ 1261 { &vop_islocked_desc, genfs_islocked }, /* islocked */ 1262 { &vop_pathconf_desc, udf_pathconf }, /* pathconf */ 1263 { &vop_advlock_desc, udf_advlock }, /* advlock */ /* TEST ME */ 1264 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */ /* ->strategy */ 1265 { &vop_getpages_desc, genfs_getpages }, /* getpages */ 1266 { &vop_putpages_desc, genfs_putpages }, /* putpages */ 1267 { NULL, NULL } 1268 }; 1269 1270 1271 const struct vnodeopv_desc udf_vnodeop_opv_desc = { 1272 &udf_vnodeop_p, udf_vnodeop_entries 1273 }; 1274 1275