1 /* $NetBSD: udf_vnops.c,v 1.34 2008/12/16 14:28:34 reinoud Exp $ */ 2 3 /* 4 * Copyright (c) 2006, 2008 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 * Generic parts are derived from software contributed to The NetBSD Foundation 28 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code 29 * 2005 program. 30 * 31 */ 32 33 #include <sys/cdefs.h> 34 #ifndef lint 35 __KERNEL_RCSID(0, "$NetBSD: udf_vnops.c,v 1.34 2008/12/16 14:28:34 reinoud Exp $"); 36 #endif /* not lint */ 37 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/namei.h> 42 #include <sys/resourcevar.h> /* defines plimit structure in proc struct */ 43 #include <sys/kernel.h> 44 #include <sys/file.h> /* define FWRITE ... */ 45 #include <sys/stat.h> 46 #include <sys/buf.h> 47 #include <sys/proc.h> 48 #include <sys/mount.h> 49 #include <sys/vnode.h> 50 #include <sys/signalvar.h> 51 #include <sys/malloc.h> 52 #include <sys/dirent.h> 53 #include <sys/lockf.h> 54 #include <sys/kauth.h> 55 56 #include <miscfs/genfs/genfs.h> 57 #include <uvm/uvm_extern.h> 58 59 #include <fs/udf/ecma167-udf.h> 60 #include <fs/udf/udf_mount.h> 61 #include "udf.h" 62 #include "udf_subr.h" 63 #include "udf_bswap.h" 64 65 66 #define VTOI(vnode) ((struct udf_node *) (vnode)->v_data) 67 68 69 /* externs */ 70 extern int prtactive; 71 72 /* implementations of vnode functions; table follows at end */ 73 /* --------------------------------------------------------------------- */ 74 75 int 76 udf_inactive(void *v) 77 { 78 struct vop_inactive_args /* { 79 struct vnode *a_vp; 80 bool *a_recycle; 81 } */ *ap = v; 82 struct vnode *vp = ap->a_vp; 83 struct udf_node *udf_node = VTOI(vp); 84 int refcnt; 85 86 DPRINTF(NODE, ("udf_inactive called for udf_node %p\n", VTOI(vp))); 87 88 if (udf_node == NULL) { 89 DPRINTF(NODE, ("udf_inactive: inactive NULL UDF node\n")); 90 VOP_UNLOCK(vp, 0); 91 return 0; 92 } 93 94 /* 95 * Optionally flush metadata to disc. If the file has not been 96 * referenced anymore in a directory we ought to free up the resources 97 * on disc if applicable. 98 */ 99 if (udf_node->fe) { 100 refcnt = udf_rw16(udf_node->fe->link_cnt); 101 } else { 102 assert(udf_node->efe); 103 refcnt = udf_rw16(udf_node->efe->link_cnt); 104 } 105 106 if ((refcnt == 0) && (vp->v_vflag & VV_SYSTEM)) 107 DPRINTF(VOLUMES, ("UDF_INACTIVE deleting VV_SYSTEM\n")); 108 109 *ap->a_recycle = false; 110 if ((refcnt == 0) && ((vp->v_vflag & VV_SYSTEM) == 0)) { 111 /* remove this file's allocation */ 112 DPRINTF(NODE, ("udf_inactive deleting unlinked file\n")); 113 *ap->a_recycle = true; 114 udf_delete_node(udf_node); 115 VOP_UNLOCK(vp, 0); 116 vrecycle(vp, NULL, curlwp); 117 return 0; 118 } 119 120 /* write out its node */ 121 if (udf_node->i_flags & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) 122 udf_update(vp, NULL, NULL, NULL, 0); 123 VOP_UNLOCK(vp, 0); 124 125 return 0; 126 } 127 128 /* --------------------------------------------------------------------- */ 129 130 int udf_sync(struct mount *mp, int waitfor, kauth_cred_t cred, struct lwp *lwp); 131 132 int 133 udf_reclaim(void *v) 134 { 135 struct vop_reclaim_args /* { 136 struct vnode *a_vp; 137 } */ *ap = v; 138 struct vnode *vp = ap->a_vp; 139 struct udf_node *udf_node = VTOI(vp); 140 141 DPRINTF(NODE, ("udf_reclaim called for node %p\n", udf_node)); 142 if (prtactive && vp->v_usecount > 1) 143 vprint("udf_reclaim(): pushing active", vp); 144 145 if (udf_node == NULL) { 146 DPRINTF(NODE, ("udf_reclaim(): null udfnode\n")); 147 return 0; 148 } 149 150 /* update note for closure */ 151 udf_update(vp, NULL, NULL, NULL, UPDATE_CLOSE); 152 153 /* async check to see if all node descriptors are written out */ 154 while ((volatile int) udf_node->outstanding_nodedscr > 0) { 155 vprint("udf_reclaim(): waiting for writeout\n", vp); 156 tsleep(&udf_node->outstanding_nodedscr, PRIBIO, "recl wait", hz/8); 157 } 158 159 /* purge old data from namei */ 160 cache_purge(vp); 161 162 /* dispose all node knowledge */ 163 udf_dispose_node(udf_node); 164 165 return 0; 166 } 167 168 /* --------------------------------------------------------------------- */ 169 170 int 171 udf_read(void *v) 172 { 173 struct vop_read_args /* { 174 struct vnode *a_vp; 175 struct uio *a_uio; 176 int a_ioflag; 177 kauth_cred_t a_cred; 178 } */ *ap = v; 179 struct vnode *vp = ap->a_vp; 180 struct uio *uio = ap->a_uio; 181 int ioflag = ap->a_ioflag; 182 int advice = IO_ADV_DECODE(ap->a_ioflag); 183 struct uvm_object *uobj; 184 struct udf_node *udf_node = VTOI(vp); 185 struct file_entry *fe; 186 struct extfile_entry *efe; 187 uint64_t file_size; 188 vsize_t len; 189 int error; 190 191 /* 192 * XXX reading from extended attributes not yet implemented. FreeBSD 193 * has it in mind to forward the IO_EXT read call to the 194 * VOP_READEXTATTR(). 195 */ 196 197 DPRINTF(READ, ("udf_read called\n")); 198 199 /* can this happen? some filingsystems have this check */ 200 if (uio->uio_offset < 0) 201 return EINVAL; 202 if (uio->uio_resid == 0) 203 return 0; 204 205 /* protect against rogue programs reading raw directories and links */ 206 if ((ioflag & IO_ALTSEMANTICS) == 0) { 207 if (vp->v_type == VDIR) 208 return EISDIR; 209 /* all but regular files just give EINVAL */ 210 if (vp->v_type != VREG) 211 return EINVAL; 212 } 213 214 assert(udf_node); 215 assert(udf_node->fe || udf_node->efe); 216 217 /* get file/directory filesize */ 218 if (udf_node->fe) { 219 fe = udf_node->fe; 220 file_size = udf_rw64(fe->inf_len); 221 } else { 222 assert(udf_node->efe); 223 efe = udf_node->efe; 224 file_size = udf_rw64(efe->inf_len); 225 } 226 227 /* read contents using buffercache */ 228 uobj = &vp->v_uobj; 229 error = 0; 230 while (uio->uio_resid > 0) { 231 /* reached end? */ 232 if (file_size <= uio->uio_offset) 233 break; 234 235 /* maximise length to file extremity */ 236 len = MIN(file_size - uio->uio_offset, uio->uio_resid); 237 if (len == 0) 238 break; 239 240 /* ubc, here we come, prepare to trap */ 241 error = ubc_uiomove(uobj, uio, len, advice, 242 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp)); 243 if (error) 244 break; 245 } 246 247 /* note access time unless not requested */ 248 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) { 249 udf_node->i_flags |= IN_ACCESS; 250 if ((ioflag & IO_SYNC) == IO_SYNC) 251 error = udf_update(vp, NULL, NULL, NULL, UPDATE_WAIT); 252 } 253 254 return error; 255 } 256 257 /* --------------------------------------------------------------------- */ 258 259 int 260 udf_write(void *v) 261 { 262 struct vop_write_args /* { 263 struct vnode *a_vp; 264 struct uio *a_uio; 265 int a_ioflag; 266 kauth_cred_t a_cred; 267 } */ *ap = v; 268 struct vnode *vp = ap->a_vp; 269 struct uio *uio = ap->a_uio; 270 int ioflag = ap->a_ioflag; 271 int advice = IO_ADV_DECODE(ap->a_ioflag); 272 struct uvm_object *uobj; 273 struct udf_node *udf_node = VTOI(vp); 274 struct file_entry *fe; 275 struct extfile_entry *efe; 276 uint64_t file_size, old_size; 277 vsize_t len; 278 int error; 279 int resid, extended; 280 281 /* 282 * XXX writing to extended attributes not yet implemented. FreeBSD has 283 * it in mind to forward the IO_EXT read call to the 284 * VOP_READEXTATTR(). 285 */ 286 287 DPRINTF(WRITE, ("udf_write called\n")); 288 289 /* can this happen? some filingsystems have this check */ 290 if (uio->uio_offset < 0) 291 return EINVAL; 292 if (uio->uio_resid == 0) 293 return 0; 294 295 /* protect against rogue programs writing raw directories or links */ 296 if ((ioflag & IO_ALTSEMANTICS) == 0) { 297 if (vp->v_type == VDIR) 298 return EISDIR; 299 /* all but regular files just give EINVAL for now */ 300 if (vp->v_type != VREG) 301 return EINVAL; 302 } 303 304 assert(udf_node); 305 assert(udf_node->fe || udf_node->efe); 306 307 /* get file/directory filesize */ 308 if (udf_node->fe) { 309 fe = udf_node->fe; 310 file_size = udf_rw64(fe->inf_len); 311 } else { 312 assert(udf_node->efe); 313 efe = udf_node->efe; 314 file_size = udf_rw64(efe->inf_len); 315 } 316 old_size = file_size; 317 318 /* if explicitly asked to append, uio_offset can be wrong? */ 319 if (ioflag & IO_APPEND) 320 uio->uio_offset = file_size; 321 322 extended = (uio->uio_offset + uio->uio_resid > file_size); 323 if (extended) { 324 DPRINTF(WRITE, ("extending file from %"PRIu64" to %"PRIu64"\n", 325 file_size, uio->uio_offset + uio->uio_resid)); 326 error = udf_grow_node(udf_node, uio->uio_offset + uio->uio_resid); 327 if (error) 328 return error; 329 file_size = uio->uio_offset + uio->uio_resid; 330 } 331 332 /* write contents using buffercache */ 333 uobj = &vp->v_uobj; 334 resid = uio->uio_resid; 335 error = 0; 336 337 uvm_vnp_setwritesize(vp, file_size); 338 while (uio->uio_resid > 0) { 339 /* maximise length to file extremity */ 340 len = MIN(file_size - uio->uio_offset, uio->uio_resid); 341 if (len == 0) 342 break; 343 344 /* ubc, here we come, prepare to trap */ 345 error = ubc_uiomove(uobj, uio, len, advice, 346 UBC_WRITE | UBC_UNMAP_FLAG(vp)); 347 if (error) 348 break; 349 } 350 uvm_vnp_setsize(vp, file_size); 351 352 /* mark node changed and request update */ 353 udf_node->i_flags |= IN_CHANGE | IN_UPDATE; 354 355 /* 356 * XXX TODO FFS has code here to reset setuid & setgid when we're not 357 * the superuser as a precaution against tampering. 358 */ 359 360 /* if we wrote a thing, note write action on vnode */ 361 if (resid > uio->uio_resid) 362 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0)); 363 364 if (error) { 365 /* bring back file size to its former size */ 366 /* take notice of its errors? */ 367 (void) udf_chsize(vp, (u_quad_t) old_size, NOCRED); 368 369 /* roll back uio */ 370 uio->uio_offset -= resid - uio->uio_resid; 371 uio->uio_resid = resid; 372 } else { 373 /* if we write and we're synchronous, update node */ 374 if ((resid > uio->uio_resid) && ((ioflag & IO_SYNC) == IO_SYNC)) 375 error = udf_update(vp, NULL, NULL, NULL, UPDATE_WAIT); 376 } 377 378 return error; 379 } 380 381 382 /* --------------------------------------------------------------------- */ 383 384 /* 385 * `Special' bmap functionality that translates all incomming requests to 386 * translate to vop_strategy() calls with the same blocknumbers effectively 387 * not translating at all. 388 */ 389 390 int 391 udf_trivial_bmap(void *v) 392 { 393 struct vop_bmap_args /* { 394 struct vnode *a_vp; 395 daddr_t a_bn; 396 struct vnode **a_vpp; 397 daddr_t *a_bnp; 398 int *a_runp; 399 } */ *ap = v; 400 struct vnode *vp = ap->a_vp; /* our node */ 401 struct vnode **vpp = ap->a_vpp; /* return node */ 402 daddr_t *bnp = ap->a_bnp; /* translated */ 403 daddr_t bn = ap->a_bn; /* origional */ 404 int *runp = ap->a_runp; 405 struct udf_node *udf_node = VTOI(vp); 406 uint32_t lb_size; 407 408 /* get logical block size */ 409 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size); 410 411 /* could return `-1' to indicate holes/zeros */ 412 /* translate 1:1 */ 413 *bnp = bn; 414 415 /* set the vnode to read the data from with strategy on itself */ 416 if (vpp) 417 *vpp = vp; 418 419 /* set runlength of maximum block size */ 420 if (runp) 421 *runp = MAXPHYS / lb_size; /* or with -1 ? */ 422 423 /* return success */ 424 return 0; 425 } 426 427 /* --------------------------------------------------------------------- */ 428 429 int 430 udf_vfsstrategy(void *v) 431 { 432 struct vop_strategy_args /* { 433 struct vnode *a_vp; 434 struct buf *a_bp; 435 } */ *ap = v; 436 struct vnode *vp = ap->a_vp; 437 struct buf *bp = ap->a_bp; 438 struct udf_node *udf_node = VTOI(vp); 439 uint32_t lb_size, from, sectors; 440 int error; 441 442 DPRINTF(STRATEGY, ("udf_strategy called\n")); 443 444 /* check if we ought to be here */ 445 if (vp->v_type == VBLK || vp->v_type == VCHR) 446 panic("udf_strategy: spec"); 447 448 /* only filebuffers ought to be read/write by this, no descriptors */ 449 assert(bp->b_blkno >= 0); 450 451 /* get sector size */ 452 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size); 453 454 /* calculate sector to start from */ 455 from = bp->b_blkno; 456 457 /* calculate length to fetch/store in sectors */ 458 sectors = bp->b_bcount / lb_size; 459 assert(bp->b_bcount > 0); 460 461 /* NEVER assume later that this buffer is already translated */ 462 /* bp->b_lblkno = bp->b_blkno; */ 463 464 /* check assertions: we OUGHT to always get multiples of this */ 465 assert(sectors * lb_size == bp->b_bcount); 466 467 /* issue buffer */ 468 error = 0; 469 if (bp->b_flags & B_READ) { 470 DPRINTF(STRATEGY, ("\tread vp %p buf %p (blk no %"PRIu64")" 471 ", sector %d for %d sectors\n", 472 vp, bp, bp->b_blkno, from, sectors)); 473 474 /* read buffer from the udf_node, translate vtop on the way*/ 475 udf_read_filebuf(udf_node, bp); 476 } else { 477 DPRINTF(STRATEGY, ("\twrite vp %p buf %p (blk no %"PRIu64")" 478 ", sector %d for %d sectors\n", 479 vp, bp, bp->b_blkno, from, sectors)); 480 481 /* write buffer to the udf_node, translate vtop on the way*/ 482 udf_write_filebuf(udf_node, bp); 483 } 484 485 return bp->b_error; 486 } 487 488 /* --------------------------------------------------------------------- */ 489 490 int 491 udf_readdir(void *v) 492 { 493 struct vop_readdir_args /* { 494 struct vnode *a_vp; 495 struct uio *a_uio; 496 kauth_cred_t a_cred; 497 int *a_eofflag; 498 off_t **a_cookies; 499 int *a_ncookies; 500 } */ *ap = v; 501 struct uio *uio = ap->a_uio; 502 struct vnode *vp = ap->a_vp; 503 struct udf_node *udf_node = VTOI(vp); 504 struct file_entry *fe; 505 struct extfile_entry *efe; 506 struct fileid_desc *fid; 507 struct dirent *dirent; 508 uint64_t file_size, diroffset, transoffset; 509 uint32_t lb_size; 510 int error; 511 512 DPRINTF(READDIR, ("udf_readdir called\n")); 513 514 /* This operation only makes sense on directory nodes. */ 515 if (vp->v_type != VDIR) 516 return ENOTDIR; 517 518 /* get directory filesize */ 519 if (udf_node->fe) { 520 fe = udf_node->fe; 521 file_size = udf_rw64(fe->inf_len); 522 } else { 523 assert(udf_node->efe); 524 efe = udf_node->efe; 525 file_size = udf_rw64(efe->inf_len); 526 } 527 528 dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK | M_ZERO); 529 530 /* 531 * Add `.' pseudo entry if at offset zero since its not in the fid 532 * stream 533 */ 534 if (uio->uio_offset == 0) { 535 DPRINTF(READDIR, ("\t'.' inserted\n")); 536 strcpy(dirent->d_name, "."); 537 dirent->d_fileno = udf_calchash(&udf_node->loc); 538 dirent->d_type = DT_DIR; 539 dirent->d_namlen = strlen(dirent->d_name); 540 dirent->d_reclen = _DIRENT_SIZE(dirent); 541 uiomove(dirent, _DIRENT_SIZE(dirent), uio); 542 543 /* mark with magic value that we have done the dummy */ 544 uio->uio_offset = UDF_DIRCOOKIE_DOT; 545 } 546 547 /* we are called just as long as we keep on pushing data in */ 548 error = 0; 549 if (uio->uio_offset < file_size) { 550 /* allocate temporary space for fid */ 551 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size); 552 fid = malloc(lb_size, M_UDFTEMP, M_WAITOK); 553 554 if (uio->uio_offset == UDF_DIRCOOKIE_DOT) 555 uio->uio_offset = 0; 556 557 diroffset = uio->uio_offset; 558 transoffset = diroffset; 559 while (diroffset < file_size) { 560 DPRINTF(READDIR, ("\tread in fid stream\n")); 561 /* transfer a new fid/dirent */ 562 error = udf_read_fid_stream(vp, &diroffset, 563 fid, dirent); 564 DPRINTFIF(READDIR, error, ("read error in read fid " 565 "stream : %d\n", error)); 566 if (error) 567 break; 568 569 /* 570 * If there isn't enough space in the uio to return a 571 * whole dirent, break off read 572 */ 573 if (uio->uio_resid < _DIRENT_SIZE(dirent)) 574 break; 575 576 /* remember the last entry we transfered */ 577 transoffset = diroffset; 578 579 /* skip deleted entries */ 580 if (fid->file_char & UDF_FILE_CHAR_DEL) 581 continue; 582 583 /* skip not visible files */ 584 if (fid->file_char & UDF_FILE_CHAR_VIS) 585 continue; 586 587 /* copy dirent to the caller */ 588 DPRINTF(READDIR, ("\tread dirent `%s', type %d\n", 589 dirent->d_name, dirent->d_type)); 590 uiomove(dirent, _DIRENT_SIZE(dirent), uio); 591 } 592 593 /* pass on last transfered offset */ 594 uio->uio_offset = transoffset; 595 free(fid, M_UDFTEMP); 596 } 597 598 if (ap->a_eofflag) 599 *ap->a_eofflag = (uio->uio_offset >= file_size); 600 601 #ifdef DEBUG 602 if (udf_verbose & UDF_DEBUG_READDIR) { 603 printf("returning offset %d\n", (uint32_t) uio->uio_offset); 604 if (ap->a_eofflag) 605 printf("returning EOF ? %d\n", *ap->a_eofflag); 606 if (error) 607 printf("readdir returning error %d\n", error); 608 } 609 #endif 610 611 free(dirent, M_UDFTEMP); 612 return error; 613 } 614 615 /* --------------------------------------------------------------------- */ 616 617 int 618 udf_lookup(void *v) 619 { 620 struct vop_lookup_args /* { 621 struct vnode *a_dvp; 622 struct vnode **a_vpp; 623 struct componentname *a_cnp; 624 } */ *ap = v; 625 struct vnode *dvp = ap->a_dvp; 626 struct vnode **vpp = ap->a_vpp; 627 struct componentname *cnp = ap->a_cnp; 628 struct udf_node *dir_node, *res_node; 629 struct udf_mount *ump; 630 struct long_ad icb_loc; 631 const char *name; 632 int namelen, nameiop, islastcn, mounted_ro; 633 int vnodetp; 634 int error, found; 635 636 dir_node = VTOI(dvp); 637 ump = dir_node->ump; 638 *vpp = NULL; 639 640 DPRINTF(LOOKUP, ("udf_lookup called\n")); 641 642 /* simplify/clarification flags */ 643 nameiop = cnp->cn_nameiop; 644 islastcn = cnp->cn_flags & ISLASTCN; 645 mounted_ro = dvp->v_mount->mnt_flag & MNT_RDONLY; 646 647 /* check exec/dirread permissions first */ 648 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred); 649 if (error) 650 return error; 651 652 DPRINTF(LOOKUP, ("\taccess ok\n")); 653 654 /* 655 * If requesting a modify on the last path element on a read-only 656 * filingsystem, reject lookup; XXX why is this repeated in every FS ? 657 */ 658 if (islastcn && mounted_ro && (nameiop == DELETE || nameiop == RENAME)) 659 return EROFS; 660 661 DPRINTF(LOOKUP, ("\tlooking up cnp->cn_nameptr '%s'\n", 662 cnp->cn_nameptr)); 663 /* look in the nami cache; returns 0 on success!! */ 664 error = cache_lookup(dvp, vpp, cnp); 665 if (error >= 0) 666 return error; 667 668 DPRINTF(LOOKUP, ("\tNOT found in cache\n")); 669 670 /* 671 * Obviously, the file is not (anymore) in the namecache, we have to 672 * search for it. There are three basic cases: '.', '..' and others. 673 * 674 * Following the guidelines of VOP_LOOKUP manpage and tmpfs. 675 */ 676 error = 0; 677 if ((cnp->cn_namelen == 1) && (cnp->cn_nameptr[0] == '.')) { 678 DPRINTF(LOOKUP, ("\tlookup '.'\n")); 679 /* special case 1 '.' */ 680 VREF(dvp); 681 *vpp = dvp; 682 /* done */ 683 } else if (cnp->cn_flags & ISDOTDOT) { 684 /* special case 2 '..' */ 685 DPRINTF(LOOKUP, ("\tlookup '..'\n")); 686 687 /* get our node */ 688 name = ".."; 689 namelen = 2; 690 error = udf_lookup_name_in_dir(dvp, name, namelen, 691 &icb_loc, &found); 692 if (error) 693 goto out; 694 if (!found) 695 error = ENOENT; 696 697 /* first unlock parent */ 698 VOP_UNLOCK(dvp, 0); 699 700 if (error == 0) { 701 DPRINTF(LOOKUP, ("\tfound '..'\n")); 702 /* try to create/reuse the node */ 703 error = udf_get_node(ump, &icb_loc, &res_node); 704 705 if (!error) { 706 DPRINTF(LOOKUP, 707 ("\tnode retrieved/created OK\n")); 708 *vpp = res_node->vnode; 709 } 710 } 711 712 /* try to relock parent */ 713 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 714 } else { 715 DPRINTF(LOOKUP, ("\tlookup file\n")); 716 /* all other files */ 717 /* lookup filename in the directory; location icb_loc */ 718 name = cnp->cn_nameptr; 719 namelen = cnp->cn_namelen; 720 error = udf_lookup_name_in_dir(dvp, name, namelen, 721 &icb_loc, &found); 722 if (error) 723 goto out; 724 if (!found) { 725 DPRINTF(LOOKUP, ("\tNOT found\n")); 726 /* 727 * UGH, didn't find name. If we're creating or 728 * renaming on the last name this is OK and we ought 729 * to return EJUSTRETURN if its allowed to be created. 730 */ 731 error = ENOENT; 732 if (islastcn && 733 (nameiop == CREATE || nameiop == RENAME)) 734 error = 0; 735 if (!error) { 736 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred); 737 if (!error) { 738 /* keep the component name */ 739 cnp->cn_flags |= SAVENAME; 740 error = EJUSTRETURN; 741 } 742 } 743 /* done */ 744 } else { 745 /* try to create/reuse the node */ 746 error = udf_get_node(ump, &icb_loc, &res_node); 747 if (!error) { 748 /* 749 * If we are not at the last path component 750 * and found a non-directory or non-link entry 751 * (which may itself be pointing to a 752 * directory), raise an error. 753 */ 754 vnodetp = res_node->vnode->v_type; 755 if ((vnodetp != VDIR) && (vnodetp != VLNK)) { 756 if (!islastcn) 757 error = ENOTDIR; 758 } 759 760 } 761 if (!error) { 762 *vpp = res_node->vnode; 763 } 764 } 765 } 766 767 out: 768 /* 769 * Store result in the cache if requested. If we are creating a file, 770 * the file might not be found and thus putting it into the namecache 771 * might be seen as negative caching. 772 */ 773 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE) 774 cache_enter(dvp, *vpp, cnp); 775 776 DPRINTFIF(LOOKUP, error, ("udf_lookup returing error %d\n", error)); 777 778 return error; 779 } 780 781 /* --------------------------------------------------------------------- */ 782 783 int 784 udf_getattr(void *v) 785 { 786 struct vop_getattr_args /* { 787 struct vnode *a_vp; 788 struct vattr *a_vap; 789 kauth_cred_t a_cred; 790 struct lwp *a_l; 791 } */ *ap = v; 792 struct vnode *vp = ap->a_vp; 793 struct udf_node *udf_node = VTOI(vp); 794 struct udf_mount *ump = udf_node->ump; 795 struct file_entry *fe = udf_node->fe; 796 struct extfile_entry *efe = udf_node->efe; 797 struct filetimes_extattr_entry *ft_extattr; 798 struct device_extattr_entry *devattr; 799 struct vattr *vap = ap->a_vap; 800 struct timestamp *atime, *mtime, *attrtime, *creatime; 801 uint64_t filesize, blkssize; 802 uint32_t nlink; 803 uint32_t offset, a_l; 804 uint8_t *filedata; 805 uid_t uid; 806 gid_t gid; 807 int error; 808 809 DPRINTF(CALL, ("udf_getattr called\n")); 810 811 /* update times before we returning values */ 812 udf_itimes(udf_node, NULL, NULL, NULL); 813 814 /* get descriptor information */ 815 if (fe) { 816 nlink = udf_rw16(fe->link_cnt); 817 uid = (uid_t)udf_rw32(fe->uid); 818 gid = (gid_t)udf_rw32(fe->gid); 819 filesize = udf_rw64(fe->inf_len); 820 blkssize = udf_rw64(fe->logblks_rec); 821 atime = &fe->atime; 822 mtime = &fe->mtime; 823 attrtime = &fe->attrtime; 824 filedata = fe->data; 825 826 /* initial guess */ 827 creatime = mtime; 828 829 /* check our extended attribute if present */ 830 error = udf_extattr_search_intern(udf_node, 831 UDF_FILETIMES_ATTR_NO, "", &offset, &a_l); 832 if (!error) { 833 ft_extattr = (struct filetimes_extattr_entry *) 834 (filedata + offset); 835 if (ft_extattr->existence & UDF_FILETIMES_FILE_CREATION) 836 creatime = &ft_extattr->times[0]; 837 } 838 } else { 839 assert(udf_node->efe); 840 nlink = udf_rw16(efe->link_cnt); 841 uid = (uid_t)udf_rw32(efe->uid); 842 gid = (gid_t)udf_rw32(efe->gid); 843 filesize = udf_rw64(efe->inf_len); /* XXX or obj_size? */ 844 blkssize = udf_rw64(efe->logblks_rec); 845 atime = &efe->atime; 846 mtime = &efe->mtime; 847 attrtime = &efe->attrtime; 848 creatime = &efe->ctime; 849 filedata = efe->data; 850 } 851 852 /* do the uid/gid translation game */ 853 if (uid == (uid_t) -1) 854 uid = ump->mount_args.anon_uid; 855 if (gid == (gid_t) -1) 856 gid = ump->mount_args.anon_gid; 857 858 /* fill in struct vattr with values from the node */ 859 VATTR_NULL(vap); 860 vap->va_type = vp->v_type; 861 vap->va_mode = udf_getaccessmode(udf_node); 862 vap->va_nlink = nlink; 863 vap->va_uid = uid; 864 vap->va_gid = gid; 865 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0]; 866 vap->va_fileid = udf_calchash(&udf_node->loc); /* inode hash XXX */ 867 vap->va_size = filesize; 868 vap->va_blocksize = udf_node->ump->discinfo.sector_size; /* wise? */ 869 870 /* 871 * BUG-ALERT: UDF doesn't count '.' as an entry, so we'll have to add 872 * 1 to the link count if its a directory we're requested attributes 873 * of. 874 */ 875 if (vap->va_type == VDIR) 876 vap->va_nlink++; 877 878 /* access times */ 879 udf_timestamp_to_timespec(ump, atime, &vap->va_atime); 880 udf_timestamp_to_timespec(ump, mtime, &vap->va_mtime); 881 udf_timestamp_to_timespec(ump, attrtime, &vap->va_ctime); 882 udf_timestamp_to_timespec(ump, creatime, &vap->va_birthtime); 883 884 vap->va_gen = 1; /* no multiple generations yes (!?) */ 885 vap->va_flags = 0; /* no flags */ 886 vap->va_bytes = blkssize * udf_node->ump->discinfo.sector_size; 887 vap->va_filerev = 1; /* TODO file revision numbers? */ 888 vap->va_vaflags = 0; 889 /* TODO get vaflags from the extended attributes? */ 890 891 if ((vap->va_type == VBLK) || (vap->va_type == VCHR)) { 892 error = udf_extattr_search_intern(udf_node, 893 UDF_DEVICESPEC_ATTR_NO, "", 894 &offset, &a_l); 895 /* if error, deny access */ 896 if (error || (filedata == NULL)) { 897 vap->va_mode = 0; /* or v_type = VNON? */ 898 } else { 899 devattr = (struct device_extattr_entry *) 900 filedata + offset; 901 vap->va_rdev = makedev( 902 udf_rw32(devattr->major), 903 udf_rw32(devattr->minor) 904 ); 905 /* TODO we could check the implementator */ 906 } 907 } 908 909 return 0; 910 } 911 912 /* --------------------------------------------------------------------- */ 913 914 static int 915 udf_chown(struct vnode *vp, uid_t new_uid, gid_t new_gid, 916 kauth_cred_t cred) 917 { 918 struct udf_node *udf_node = VTOI(vp); 919 uid_t euid, uid; 920 gid_t egid, gid; 921 int issuperuser, ismember; 922 int error; 923 924 #ifdef notyet 925 /* TODO get vaflags from the extended attributes? */ 926 /* Immutable or append-only files cannot be modified, either. */ 927 if (udf_node->flags & (IMMUTABLE | APPEND)) 928 return EPERM; 929 #endif 930 931 if (vp->v_mount->mnt_flag & MNT_RDONLY) 932 return EROFS; 933 934 /* retrieve old values */ 935 udf_getownership(udf_node, &uid, &gid); 936 937 /* only one could be specified */ 938 if (new_uid == VNOVAL) 939 new_uid = uid; 940 if (new_gid == VNOVAL) 941 new_gid = gid; 942 943 /* check if we can fit it in an 32 bits */ 944 if ((uid_t) ((uint32_t) uid) != uid) 945 return EINVAL; 946 if ((gid_t) ((uint32_t) gid) != gid) 947 return EINVAL; 948 949 /* 950 * If we don't own the file, are trying to change the owner of the 951 * file, or are not a member of the target group, the caller's 952 * credentials must imply super-user privilege or the call fails. 953 */ 954 955 /* check permissions */ 956 euid = kauth_cred_geteuid(cred); 957 egid = kauth_cred_getegid(cred); 958 if ((error = kauth_cred_ismember_gid(cred, new_gid, &ismember))) 959 return error; 960 error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL); 961 issuperuser = (error == 0); 962 963 if (!issuperuser) { 964 if ((new_uid != uid) || (euid != uid)) 965 return EPERM; 966 if ((new_gid != gid) && !(egid == new_gid || ismember)) 967 return EPERM; 968 } 969 970 /* change the ownership */ 971 udf_setownership(udf_node, new_uid, new_gid); 972 973 /* mark node changed */ 974 udf_node->i_flags |= IN_CHANGE; 975 976 return 0; 977 } 978 979 980 static int 981 udf_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred) 982 { 983 struct udf_node *udf_node = VTOI(vp); 984 uid_t euid, uid; 985 gid_t egid, gid; 986 int issuperuser, ismember; 987 int error; 988 989 #ifdef notyet 990 /* TODO get vaflags from the extended attributes? */ 991 /* Immutable or append-only files cannot be modified, either. */ 992 if (udf_node->flags & (IMMUTABLE | APPEND)) 993 return EPERM; 994 #endif 995 996 if (vp->v_mount->mnt_flag & MNT_RDONLY) 997 return EROFS; 998 999 /* retrieve uid/gid values */ 1000 udf_getownership(udf_node, &uid, &gid); 1001 1002 /* check permissions */ 1003 euid = kauth_cred_geteuid(cred); 1004 egid = kauth_cred_getegid(cred); 1005 if ((error = kauth_cred_ismember_gid(cred, gid, &ismember))) 1006 return error; 1007 error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL); 1008 issuperuser = (error == 0); 1009 1010 if ((euid != uid) && !issuperuser) 1011 return EPERM; 1012 if (euid != 0) { 1013 if (vp->v_type != VDIR && (mode & S_ISTXT)) 1014 return EFTYPE; 1015 1016 if ((!ismember) && (mode & S_ISGID)) 1017 return EPERM; 1018 } 1019 1020 /* change mode */ 1021 udf_setaccessmode(udf_node, mode); 1022 1023 /* mark node changed */ 1024 udf_node->i_flags |= IN_CHANGE; 1025 1026 return 0; 1027 } 1028 1029 1030 /* exported */ 1031 int 1032 udf_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred) 1033 { 1034 struct udf_node *udf_node = VTOI(vp); 1035 int error, extended; 1036 1037 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1038 return EROFS; 1039 1040 /* Decide whether this is a valid operation based on the file type. */ 1041 switch (vp->v_type) { 1042 case VDIR: 1043 return EISDIR; 1044 case VREG: 1045 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1046 return EROFS; 1047 break; 1048 case VBLK: 1049 /* FALLTHROUGH */ 1050 case VCHR: 1051 /* FALLTHROUGH */ 1052 case VFIFO: 1053 /* Allow modifications of special files even if in the file 1054 * system is mounted read-only (we are not modifying the 1055 * files themselves, but the objects they represent). */ 1056 return 0; 1057 default: 1058 /* Anything else is unsupported. */ 1059 return EOPNOTSUPP; 1060 } 1061 1062 #if notyet 1063 /* TODO get vaflags from the extended attributes? */ 1064 /* Immutable or append-only files cannot be modified, either. */ 1065 if (node->flags & (IMMUTABLE | APPEND)) 1066 return EPERM; 1067 #endif 1068 1069 /* resize file to the requested size */ 1070 error = udf_resize_node(udf_node, newsize, &extended); 1071 1072 if (error == 0) { 1073 /* mark change */ 1074 udf_node->i_flags |= IN_CHANGE | IN_MODIFY; 1075 VN_KNOTE(vp, NOTE_ATTRIB | (extended ? NOTE_EXTEND : 0)); 1076 udf_update(vp, NULL, NULL, NULL, 0); 1077 } 1078 1079 return error; 1080 } 1081 1082 1083 static int 1084 udf_chflags(struct vnode *vp, mode_t mode, kauth_cred_t cred) 1085 { 1086 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1087 return EROFS; 1088 1089 /* XXX we can't do this yet XXX */ 1090 1091 return EINVAL; 1092 } 1093 1094 1095 static int 1096 udf_chtimes(struct vnode *vp, 1097 struct timespec *atime, struct timespec *mtime, 1098 struct timespec *birthtime, int setattrflags, 1099 kauth_cred_t cred) 1100 { 1101 struct udf_node *udf_node = VTOI(vp); 1102 uid_t euid, uid; 1103 gid_t gid; 1104 int issuperuser; 1105 int error; 1106 1107 #ifdef notyet 1108 /* TODO get vaflags from the extended attributes? */ 1109 /* Immutable or append-only files cannot be modified, either. */ 1110 if (udf_node->flags & (IMMUTABLE | APPEND)) 1111 return EPERM; 1112 #endif 1113 1114 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1115 return EROFS; 1116 1117 /* retrieve uid/gid values */ 1118 udf_getownership(udf_node, &uid, &gid); 1119 1120 /* check permissions */ 1121 euid = kauth_cred_geteuid(cred); 1122 error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL); 1123 issuperuser = (error == 0); 1124 1125 if (!issuperuser) { 1126 if (euid != uid) 1127 return EPERM; 1128 if ((setattrflags & VA_UTIMES_NULL) == 0) { 1129 error = VOP_ACCESS(vp, VWRITE, cred); 1130 if (error) 1131 return error; 1132 } 1133 } 1134 1135 /* update node flags depending on what times are passed */ 1136 if (atime->tv_sec != VNOVAL) 1137 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) 1138 udf_node->i_flags |= IN_ACCESS; 1139 if ((mtime->tv_sec != VNOVAL) || (birthtime->tv_sec != VNOVAL)) 1140 udf_node->i_flags |= IN_CHANGE | IN_UPDATE; 1141 1142 return udf_update(vp, atime, mtime, birthtime, 0); 1143 } 1144 1145 1146 int 1147 udf_setattr(void *v) 1148 { 1149 struct vop_setattr_args /* { 1150 struct vnode *a_vp; 1151 struct vattr *a_vap; 1152 kauth_cred_t a_cred; 1153 struct lwp *a_l; 1154 } */ *ap = v; 1155 struct vnode *vp = ap->a_vp; 1156 /* struct udf_node *udf_node = VTOI(vp); */ 1157 /* struct udf_mount *ump = udf_node->ump; */ 1158 kauth_cred_t cred = ap->a_cred; 1159 struct vattr *vap = ap->a_vap; 1160 int error; 1161 1162 DPRINTF(CALL, ("udf_setattr called\n")); 1163 1164 /* Abort if any unsettable attribute is given. */ 1165 error = 0; 1166 if (vap->va_type != VNON || 1167 vap->va_nlink != VNOVAL || 1168 vap->va_fsid != VNOVAL || 1169 vap->va_fileid != VNOVAL || 1170 vap->va_blocksize != VNOVAL || 1171 #ifdef notyet 1172 /* checks are debated */ 1173 vap->va_ctime.tv_sec != VNOVAL || 1174 vap->va_ctime.tv_nsec != VNOVAL || 1175 vap->va_birthtime.tv_sec != VNOVAL || 1176 vap->va_birthtime.tv_nsec != VNOVAL || 1177 #endif 1178 vap->va_gen != VNOVAL || 1179 vap->va_rdev != VNOVAL || 1180 vap->va_bytes != VNOVAL) 1181 error = EINVAL; 1182 1183 DPRINTF(ATTR, ("setattr changing:\n")); 1184 if (error == 0 && (vap->va_flags != VNOVAL)) { 1185 DPRINTF(ATTR, ("\tchflags\n")); 1186 error = udf_chflags(vp, vap->va_flags, cred); 1187 } 1188 1189 if (error == 0 && (vap->va_size != VNOVAL)) { 1190 DPRINTF(ATTR, ("\tchsize\n")); 1191 error = udf_chsize(vp, vap->va_size, cred); 1192 } 1193 1194 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL)) { 1195 DPRINTF(ATTR, ("\tchown\n")); 1196 error = udf_chown(vp, vap->va_uid, vap->va_gid, cred); 1197 } 1198 1199 if (error == 0 && (vap->va_mode != VNOVAL)) { 1200 DPRINTF(ATTR, ("\tchmod\n")); 1201 error = udf_chmod(vp, vap->va_mode, cred); 1202 } 1203 1204 if (error == 0 && 1205 ((vap->va_atime.tv_sec != VNOVAL && 1206 vap->va_atime.tv_nsec != VNOVAL) || 1207 (vap->va_mtime.tv_sec != VNOVAL && 1208 vap->va_mtime.tv_nsec != VNOVAL)) 1209 ) { 1210 DPRINTF(ATTR, ("\tchtimes\n")); 1211 error = udf_chtimes(vp, &vap->va_atime, &vap->va_mtime, 1212 &vap->va_birthtime, vap->va_vaflags, cred); 1213 } 1214 VN_KNOTE(vp, NOTE_ATTRIB); 1215 1216 return error; 1217 } 1218 1219 /* --------------------------------------------------------------------- */ 1220 1221 /* 1222 * Return POSIX pathconf information for UDF file systems. 1223 */ 1224 int 1225 udf_pathconf(void *v) 1226 { 1227 struct vop_pathconf_args /* { 1228 struct vnode *a_vp; 1229 int a_name; 1230 register_t *a_retval; 1231 } */ *ap = v; 1232 uint32_t bits; 1233 1234 DPRINTF(CALL, ("udf_pathconf called\n")); 1235 1236 switch (ap->a_name) { 1237 case _PC_LINK_MAX: 1238 *ap->a_retval = (1<<16)-1; /* 16 bits */ 1239 return 0; 1240 case _PC_NAME_MAX: 1241 *ap->a_retval = NAME_MAX; 1242 return 0; 1243 case _PC_PATH_MAX: 1244 *ap->a_retval = PATH_MAX; 1245 return 0; 1246 case _PC_PIPE_BUF: 1247 *ap->a_retval = PIPE_BUF; 1248 return 0; 1249 case _PC_CHOWN_RESTRICTED: 1250 *ap->a_retval = 1; 1251 return 0; 1252 case _PC_NO_TRUNC: 1253 *ap->a_retval = 1; 1254 return 0; 1255 case _PC_SYNC_IO: 1256 *ap->a_retval = 0; /* synchronised is off for performance */ 1257 return 0; 1258 case _PC_FILESIZEBITS: 1259 /* 64 bit file offsets -> 2+floor(2log(2^64-1)) = 2 + 63 = 65 */ 1260 bits = 64; /* XXX ought to deliver 65 */ 1261 #if 0 1262 if (udf_node) 1263 bits = 64 * vp->v_mount->mnt_dev_bshift; 1264 #endif 1265 *ap->a_retval = bits; 1266 return 0; 1267 } 1268 1269 return EINVAL; 1270 } 1271 1272 1273 /* --------------------------------------------------------------------- */ 1274 1275 int 1276 udf_open(void *v) 1277 { 1278 struct vop_open_args /* { 1279 struct vnode *a_vp; 1280 int a_mode; 1281 kauth_cred_t a_cred; 1282 struct proc *a_p; 1283 } */ *ap = v; 1284 int flags; 1285 1286 DPRINTF(CALL, ("udf_open called\n")); 1287 1288 /* 1289 * Files marked append-only must be opened for appending. 1290 * TODO: get chflags(2) flags from extened attribute. 1291 */ 1292 flags = 0; 1293 if ((flags & APPEND) && (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) 1294 return (EPERM); 1295 1296 return 0; 1297 } 1298 1299 1300 /* --------------------------------------------------------------------- */ 1301 1302 int 1303 udf_close(void *v) 1304 { 1305 struct vop_close_args /* { 1306 struct vnode *a_vp; 1307 int a_fflag; 1308 kauth_cred_t a_cred; 1309 struct proc *a_p; 1310 } */ *ap = v; 1311 struct vnode *vp = ap->a_vp; 1312 struct udf_node *udf_node = VTOI(vp); 1313 1314 DPRINTF(CALL, ("udf_close called\n")); 1315 udf_node = udf_node; /* shut up gcc */ 1316 1317 mutex_enter(&vp->v_interlock); 1318 if (vp->v_usecount > 1) 1319 udf_itimes(udf_node, NULL, NULL, NULL); 1320 mutex_exit(&vp->v_interlock); 1321 1322 return 0; 1323 } 1324 1325 1326 /* --------------------------------------------------------------------- */ 1327 1328 int 1329 udf_access(void *v) 1330 { 1331 struct vop_access_args /* { 1332 struct vnode *a_vp; 1333 int a_mode; 1334 kauth_cred_t a_cred; 1335 struct proc *a_p; 1336 } */ *ap = v; 1337 struct vnode *vp = ap->a_vp; 1338 mode_t mode = ap->a_mode; 1339 kauth_cred_t cred = ap->a_cred; 1340 /* struct udf_node *udf_node = VTOI(vp); */ 1341 struct vattr vap; 1342 int flags; 1343 int error; 1344 1345 DPRINTF(CALL, ("udf_access called\n")); 1346 1347 error = VOP_GETATTR(vp, &vap, NULL); 1348 if (error) 1349 return error; 1350 1351 /* check if we are allowed to write */ 1352 switch (vap.va_type) { 1353 case VDIR: 1354 case VLNK: 1355 case VREG: 1356 /* 1357 * normal nodes: check if we're on a read-only mounted 1358 * filingsystem and bomb out if we're trying to write. 1359 */ 1360 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) 1361 return EROFS; 1362 break; 1363 case VBLK: 1364 case VCHR: 1365 case VSOCK: 1366 case VFIFO: 1367 /* 1368 * special nodes: even on read-only mounted filingsystems 1369 * these are allowed to be written to if permissions allow. 1370 */ 1371 break; 1372 default: 1373 /* no idea what this is */ 1374 return EINVAL; 1375 } 1376 1377 /* noone may write immutable files */ 1378 /* TODO: get chflags(2) flags from extened attribute. */ 1379 flags = 0; 1380 if ((mode & VWRITE) && (flags & IMMUTABLE)) 1381 return EPERM; 1382 1383 /* ask the generic vaccess to advice on security */ 1384 return vaccess(vp->v_type, 1385 vap.va_mode, vap.va_uid, vap.va_gid, 1386 mode, cred); 1387 } 1388 1389 /* --------------------------------------------------------------------- */ 1390 1391 int 1392 udf_create(void *v) 1393 { 1394 struct vop_create_args /* { 1395 struct vnode *a_dvp; 1396 struct vnode **a_vpp; 1397 struct componentname *a_cnp; 1398 struct vattr *a_vap; 1399 } */ *ap = v; 1400 struct vnode *dvp = ap->a_dvp; 1401 struct vnode **vpp = ap->a_vpp; 1402 struct vattr *vap = ap->a_vap; 1403 struct componentname *cnp = ap->a_cnp; 1404 int error; 1405 1406 DPRINTF(CALL, ("udf_create called\n")); 1407 error = udf_create_node(dvp, vpp, vap, cnp); 1408 1409 if (error || !(cnp->cn_flags & SAVESTART)) 1410 PNBUF_PUT(cnp->cn_pnbuf); 1411 vput(dvp); 1412 return error; 1413 } 1414 1415 /* --------------------------------------------------------------------- */ 1416 1417 int 1418 udf_mknod(void *v) 1419 { 1420 struct vop_mknod_args /* { 1421 struct vnode *a_dvp; 1422 struct vnode **a_vpp; 1423 struct componentname *a_cnp; 1424 struct vattr *a_vap; 1425 } */ *ap = v; 1426 struct vnode *dvp = ap->a_dvp; 1427 struct vnode **vpp = ap->a_vpp; 1428 struct vattr *vap = ap->a_vap; 1429 struct componentname *cnp = ap->a_cnp; 1430 int error; 1431 1432 DPRINTF(CALL, ("udf_mknod called\n")); 1433 error = udf_create_node(dvp, vpp, vap, cnp); 1434 1435 if (error || !(cnp->cn_flags & SAVESTART)) 1436 PNBUF_PUT(cnp->cn_pnbuf); 1437 vput(dvp); 1438 return error; 1439 } 1440 1441 /* --------------------------------------------------------------------- */ 1442 1443 int 1444 udf_mkdir(void *v) 1445 { 1446 struct vop_mkdir_args /* { 1447 struct vnode *a_dvp; 1448 struct vnode **a_vpp; 1449 struct componentname *a_cnp; 1450 struct vattr *a_vap; 1451 } */ *ap = v; 1452 struct vnode *dvp = ap->a_dvp; 1453 struct vnode **vpp = ap->a_vpp; 1454 struct vattr *vap = ap->a_vap; 1455 struct componentname *cnp = ap->a_cnp; 1456 int error; 1457 1458 DPRINTF(CALL, ("udf_mkdir called\n")); 1459 error = udf_create_node(dvp, vpp, vap, cnp); 1460 1461 if (error || !(cnp->cn_flags & SAVESTART)) 1462 PNBUF_PUT(cnp->cn_pnbuf); 1463 vput(dvp); 1464 return error; 1465 } 1466 1467 /* --------------------------------------------------------------------- */ 1468 1469 static int 1470 udf_do_link(struct vnode *dvp, struct vnode *vp, struct componentname *cnp) 1471 { 1472 struct udf_node *udf_node, *dir_node; 1473 struct vattr vap; 1474 int error; 1475 1476 DPRINTF(CALL, ("udf_link called\n")); 1477 error = 0; 1478 1479 /* some quick checks */ 1480 if (vp->v_type == VDIR) 1481 return EPERM; /* can't link a directory */ 1482 if (dvp->v_mount != vp->v_mount) 1483 return EXDEV; /* can't link across devices */ 1484 if (dvp == vp) 1485 return EPERM; /* can't be the same */ 1486 1487 /* lock node */ 1488 error = vn_lock(vp, LK_EXCLUSIVE); 1489 if (error) 1490 return error; 1491 1492 /* get attributes */ 1493 dir_node = VTOI(dvp); 1494 udf_node = VTOI(vp); 1495 1496 error = VOP_GETATTR(vp, &vap, FSCRED); 1497 if (error) 1498 return error; 1499 1500 /* check link count overflow */ 1501 if (vap.va_nlink >= (1<<16)-1) /* uint16_t */ 1502 return EMLINK; 1503 1504 return udf_dir_attach(dir_node->ump, dir_node, udf_node, &vap, cnp); 1505 } 1506 1507 int 1508 udf_link(void *v) 1509 { 1510 struct vop_link_args /* { 1511 struct vnode *a_dvp; 1512 struct vnode *a_vp; 1513 struct componentname *a_cnp; 1514 } */ *ap = v; 1515 struct vnode *dvp = ap->a_dvp; 1516 struct vnode *vp = ap->a_vp; 1517 struct componentname *cnp = ap->a_cnp; 1518 int error; 1519 1520 error = udf_do_link(dvp, vp, cnp); 1521 if (error) 1522 VOP_ABORTOP(dvp, cnp); 1523 1524 if ((vp != dvp) && (VOP_ISLOCKED(vp) == LK_EXCLUSIVE)) 1525 VOP_UNLOCK(vp, 0); 1526 1527 VN_KNOTE(vp, NOTE_LINK); 1528 VN_KNOTE(dvp, NOTE_WRITE); 1529 vput(dvp); 1530 1531 return error; 1532 } 1533 1534 /* --------------------------------------------------------------------- */ 1535 1536 static int 1537 udf_do_symlink(struct udf_node *udf_node, char *target) 1538 { 1539 struct pathcomp pathcomp; 1540 uint8_t *pathbuf, *pathpos, *compnamepos; 1541 char *mntonname; 1542 int pathlen, len, compnamelen, mntonnamelen; 1543 int error; 1544 1545 /* process `target' to an UDF structure */ 1546 pathbuf = malloc(UDF_SYMLINKBUFLEN, M_UDFTEMP, M_WAITOK); 1547 pathpos = pathbuf; 1548 pathlen = 0; 1549 1550 if (*target == '/') { 1551 /* symlink starts from the root */ 1552 len = UDF_PATH_COMP_SIZE; 1553 memset(&pathcomp, 0, len); 1554 pathcomp.type = UDF_PATH_COMP_ROOT; 1555 1556 /* check if its mount-point relative! */ 1557 mntonname = udf_node->ump->vfs_mountp->mnt_stat.f_mntonname; 1558 mntonnamelen = strlen(mntonname); 1559 if (strlen(target) >= mntonnamelen) { 1560 if (strncmp(target, mntonname, mntonnamelen) == 0) { 1561 pathcomp.type = UDF_PATH_COMP_MOUNTROOT; 1562 target += mntonnamelen; 1563 } 1564 } else { 1565 target++; 1566 } 1567 1568 memcpy(pathpos, &pathcomp, len); 1569 pathpos += len; 1570 pathlen += len; 1571 } 1572 1573 error = 0; 1574 while (*target) { 1575 /* ignore multiple '/' */ 1576 while (*target == '/') { 1577 target++; 1578 } 1579 if (!*target) 1580 break; 1581 1582 /* extract component name */ 1583 compnamelen = 0; 1584 compnamepos = target; 1585 while ((*target) && (*target != '/')) { 1586 target++; 1587 compnamelen++; 1588 } 1589 1590 /* just trunc if too long ?? (security issue) */ 1591 if (compnamelen >= 127) { 1592 error = ENAMETOOLONG; 1593 break; 1594 } 1595 1596 /* convert unix name to UDF name */ 1597 len = sizeof(struct pathcomp); 1598 memset(&pathcomp, 0, len); 1599 pathcomp.type = UDF_PATH_COMP_NAME; 1600 len = UDF_PATH_COMP_SIZE; 1601 1602 if ((compnamelen == 2) && (strncmp(compnamepos, "..", 2) == 0)) 1603 pathcomp.type = UDF_PATH_COMP_PARENTDIR; 1604 if ((compnamelen == 1) && (*compnamepos == '.')) 1605 pathcomp.type = UDF_PATH_COMP_CURDIR; 1606 1607 if (pathcomp.type == UDF_PATH_COMP_NAME) { 1608 unix_to_udf_name( 1609 (char *) &pathcomp.ident, &pathcomp.l_ci, 1610 compnamepos, compnamelen, 1611 &udf_node->ump->logical_vol->desc_charset); 1612 len = UDF_PATH_COMP_SIZE + pathcomp.l_ci; 1613 } 1614 1615 if (pathlen + len >= UDF_SYMLINKBUFLEN) { 1616 error = ENAMETOOLONG; 1617 break; 1618 } 1619 1620 memcpy(pathpos, &pathcomp, len); 1621 pathpos += len; 1622 pathlen += len; 1623 } 1624 1625 if (error) { 1626 /* aparently too big */ 1627 free(pathbuf, M_UDFTEMP); 1628 return error; 1629 } 1630 1631 error = udf_grow_node(udf_node, pathlen); 1632 if (error) { 1633 /* failed to pregrow node */ 1634 free(pathbuf, M_UDFTEMP); 1635 return error; 1636 } 1637 1638 /* write out structure on the new file */ 1639 error = vn_rdwr(UIO_WRITE, udf_node->vnode, 1640 pathbuf, pathlen, 0, 1641 UIO_SYSSPACE, IO_NODELOCKED | IO_ALTSEMANTICS, 1642 FSCRED, NULL, NULL); 1643 if (error) { 1644 /* failed to write out symlink contents */ 1645 free(pathbuf, M_UDFTEMP); 1646 return error; 1647 } 1648 1649 return 0; 1650 } 1651 1652 1653 int 1654 udf_symlink(void *v) 1655 { 1656 struct vop_symlink_args /* { 1657 struct vnode *a_dvp; 1658 struct vnode **a_vpp; 1659 struct componentname *a_cnp; 1660 struct vattr *a_vap; 1661 char *a_target; 1662 } */ *ap = v; 1663 struct vnode *dvp = ap->a_dvp; 1664 struct vnode **vpp = ap->a_vpp; 1665 struct vattr *vap = ap->a_vap; 1666 struct componentname *cnp = ap->a_cnp; 1667 struct udf_node *dir_node; 1668 struct udf_node *udf_node; 1669 int error; 1670 1671 DPRINTF(CALL, ("udf_symlink called\n")); 1672 DPRINTF(CALL, ("\tlinking to `%s`\n", ap->a_target)); 1673 error = udf_create_node(dvp, vpp, vap, cnp); 1674 KASSERT(((error == 0) && (*vpp != NULL)) || ((error && (*vpp == NULL)))); 1675 if (!error) { 1676 dir_node = VTOI(dvp); 1677 udf_node = VTOI(*vpp); 1678 KASSERT(udf_node); 1679 error = udf_do_symlink(udf_node, ap->a_target); 1680 if (error) { 1681 /* remove node */ 1682 udf_shrink_node(udf_node, 0); 1683 udf_dir_detach(udf_node->ump, dir_node, udf_node, cnp); 1684 } 1685 } 1686 if (error || !(cnp->cn_flags & SAVESTART)) 1687 PNBUF_PUT(cnp->cn_pnbuf); 1688 vput(dvp); 1689 return error; 1690 } 1691 1692 /* --------------------------------------------------------------------- */ 1693 1694 int 1695 udf_readlink(void *v) 1696 { 1697 struct vop_readlink_args /* { 1698 struct vnode *a_vp; 1699 struct uio *a_uio; 1700 kauth_cred_t a_cred; 1701 } */ *ap = v; 1702 struct vnode *vp = ap->a_vp; 1703 struct uio *uio = ap->a_uio; 1704 kauth_cred_t cred = ap->a_cred; 1705 struct udf_node *udf_node; 1706 struct pathcomp pathcomp; 1707 struct vattr vattr; 1708 uint8_t *pathbuf, *targetbuf, *tmpname; 1709 uint8_t *pathpos, *targetpos; 1710 char *mntonname; 1711 int pathlen, targetlen, namelen, mntonnamelen, len, l_ci; 1712 int first, error; 1713 1714 DPRINTF(CALL, ("udf_readlink called\n")); 1715 1716 udf_node = VTOI(vp); 1717 error = VOP_GETATTR(vp, &vattr, cred); 1718 if (error) 1719 return error; 1720 1721 /* claim temporary buffers for translation */ 1722 pathbuf = malloc(UDF_SYMLINKBUFLEN, M_UDFTEMP, M_WAITOK); 1723 targetbuf = malloc(PATH_MAX+1, M_UDFTEMP, M_WAITOK); 1724 tmpname = malloc(PATH_MAX+1, M_UDFTEMP, M_WAITOK); 1725 memset(pathbuf, 0, UDF_SYMLINKBUFLEN); 1726 memset(targetbuf, 0, PATH_MAX); 1727 1728 /* read contents of file in our temporary buffer */ 1729 error = vn_rdwr(UIO_READ, udf_node->vnode, 1730 pathbuf, vattr.va_size, 0, 1731 UIO_SYSSPACE, IO_NODELOCKED | IO_ALTSEMANTICS, 1732 FSCRED, NULL, NULL); 1733 if (error) { 1734 /* failed to read in symlink contents */ 1735 free(pathbuf, M_UDFTEMP); 1736 free(targetbuf, M_UDFTEMP); 1737 free(tmpname, M_UDFTEMP); 1738 return error; 1739 } 1740 1741 /* convert to a unix path */ 1742 pathpos = pathbuf; 1743 pathlen = 0; 1744 targetpos = targetbuf; 1745 targetlen = PATH_MAX; 1746 mntonname = udf_node->ump->vfs_mountp->mnt_stat.f_mntonname; 1747 mntonnamelen = strlen(mntonname); 1748 1749 error = 0; 1750 first = 1; 1751 while (vattr.va_size - pathlen >= UDF_PATH_COMP_SIZE) { 1752 len = UDF_PATH_COMP_SIZE; 1753 memcpy(&pathcomp, pathpos, len); 1754 l_ci = pathcomp.l_ci; 1755 switch (pathcomp.type) { 1756 case UDF_PATH_COMP_ROOT : 1757 /* XXX should check for l_ci; bugcompatible now */ 1758 if ((targetlen < 1) || !first) { 1759 error = EINVAL; 1760 break; 1761 } 1762 *targetpos++ = '/'; targetlen--; 1763 break; 1764 case UDF_PATH_COMP_MOUNTROOT : 1765 /* XXX what should it be if l_ci > 0 ? [4/48.16.1.2] */ 1766 if (l_ci || (targetlen < mntonnamelen+1) || !first) { 1767 error = EINVAL; 1768 break; 1769 } 1770 memcpy(targetpos, mntonname, mntonnamelen); 1771 targetpos += mntonnamelen; targetlen -= mntonnamelen; 1772 if (vattr.va_size-pathlen > UDF_PATH_COMP_SIZE+l_ci) { 1773 /* more follows, so must be directory */ 1774 *targetpos++ = '/'; targetlen--; 1775 } 1776 break; 1777 case UDF_PATH_COMP_PARENTDIR : 1778 /* XXX should check for l_ci; bugcompatible now */ 1779 if (targetlen < 3) { 1780 error = EINVAL; 1781 break; 1782 } 1783 *targetpos++ = '.'; targetlen--; 1784 *targetpos++ = '.'; targetlen--; 1785 *targetpos++ = '/'; targetlen--; 1786 break; 1787 case UDF_PATH_COMP_CURDIR : 1788 /* XXX should check for l_ci; bugcompatible now */ 1789 if (targetlen < 2) { 1790 error = EINVAL; 1791 break; 1792 } 1793 *targetpos++ = '.'; targetlen--; 1794 *targetpos++ = '/'; targetlen--; 1795 break; 1796 case UDF_PATH_COMP_NAME : 1797 if (l_ci == 0) { 1798 error = EINVAL; 1799 break; 1800 } 1801 memset(tmpname, 0, PATH_MAX); 1802 memcpy(&pathcomp, pathpos, len + l_ci); 1803 udf_to_unix_name(tmpname, MAXPATHLEN, 1804 pathcomp.ident, l_ci, 1805 &udf_node->ump->logical_vol->desc_charset); 1806 namelen = strlen(tmpname); 1807 if (targetlen < namelen + 1) { 1808 error = EINVAL; 1809 break; 1810 } 1811 memcpy(targetpos, tmpname, namelen); 1812 targetpos += namelen; targetlen -= namelen; 1813 if (vattr.va_size-pathlen > UDF_PATH_COMP_SIZE+l_ci) { 1814 /* more follows, so must be directory */ 1815 *targetpos++ = '/'; targetlen--; 1816 } 1817 break; 1818 default : 1819 error = EINVAL; 1820 break; 1821 } 1822 first = 0; 1823 if (error) 1824 break; 1825 pathpos += UDF_PATH_COMP_SIZE + l_ci; 1826 pathlen += UDF_PATH_COMP_SIZE + l_ci; 1827 1828 } 1829 /* all processed? */ 1830 if (vattr.va_size - pathlen > 0) 1831 error = EINVAL; 1832 1833 /* uiomove() to destination */ 1834 if (!error) 1835 uiomove(targetbuf, PATH_MAX - targetlen, uio); 1836 1837 free(pathbuf, M_UDFTEMP); 1838 free(targetbuf, M_UDFTEMP); 1839 free(tmpname, M_UDFTEMP); 1840 1841 return error; 1842 } 1843 1844 /* --------------------------------------------------------------------- */ 1845 1846 /* note: i tried to follow the logics of the tmpfs rename code */ 1847 int 1848 udf_rename(void *v) 1849 { 1850 struct vop_rename_args /* { 1851 struct vnode *a_fdvp; 1852 struct vnode *a_fvp; 1853 struct componentname *a_fcnp; 1854 struct vnode *a_tdvp; 1855 struct vnode *a_tvp; 1856 struct componentname *a_tcnp; 1857 } */ *ap = v; 1858 struct vnode *tvp = ap->a_tvp; 1859 struct vnode *tdvp = ap->a_tdvp; 1860 struct vnode *fvp = ap->a_fvp; 1861 struct vnode *fdvp = ap->a_fdvp; 1862 struct componentname *tcnp = ap->a_tcnp; 1863 struct componentname *fcnp = ap->a_fcnp; 1864 struct udf_node *fnode, *fdnode, *tnode, *tdnode; 1865 struct vattr fvap, tvap; 1866 int error; 1867 1868 DPRINTF(CALL, ("udf_rename called\n")); 1869 1870 /* disallow cross-device renames */ 1871 if (fvp->v_mount != tdvp->v_mount || 1872 (tvp != NULL && fvp->v_mount != tvp->v_mount)) { 1873 error = EXDEV; 1874 goto out_unlocked; 1875 } 1876 1877 fnode = VTOI(fvp); 1878 fdnode = VTOI(fdvp); 1879 tnode = (tvp == NULL) ? NULL : VTOI(tvp); 1880 tdnode = VTOI(tdvp); 1881 1882 /* lock our source dir */ 1883 if (fdnode != tdnode) { 1884 error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY); 1885 if (error != 0) 1886 goto out_unlocked; 1887 } 1888 1889 /* get info about the node to be moved */ 1890 error = VOP_GETATTR(fvp, &fvap, FSCRED); 1891 KASSERT(error == 0); 1892 1893 /* check when to delete the old already existing entry */ 1894 if (tvp) { 1895 /* get info about the node to be moved to */ 1896 error = VOP_GETATTR(fvp, &tvap, FSCRED); 1897 KASSERT(error == 0); 1898 1899 /* if both dirs, make sure the destination is empty */ 1900 if (fvp->v_type == VDIR && tvp->v_type == VDIR) { 1901 if (tvap.va_nlink > 2) { 1902 error = ENOTEMPTY; 1903 goto out; 1904 } 1905 } 1906 /* if moving dir, make sure destination is dir too */ 1907 if (fvp->v_type == VDIR && tvp->v_type != VDIR) { 1908 error = ENOTDIR; 1909 goto out; 1910 } 1911 /* if we're moving a non-directory, make sure dest is no dir */ 1912 if (fvp->v_type != VDIR && tvp->v_type == VDIR) { 1913 error = EISDIR; 1914 goto out; 1915 } 1916 } 1917 1918 /* dont allow renaming directories acros directory for now */ 1919 if (fdnode != tdnode) { 1920 if (fvp->v_type == VDIR) { 1921 error = EINVAL; 1922 goto out; 1923 } 1924 } 1925 1926 /* remove existing entry if present */ 1927 if (tvp) 1928 udf_dir_detach(tdnode->ump, tdnode, tnode, tcnp); 1929 1930 /* create new directory entry for the node */ 1931 error = udf_dir_attach(tdnode->ump, tdnode, fnode, &fvap, tcnp); 1932 if (error) 1933 goto out; 1934 1935 /* unlink old directory entry for the node, if failing, unattach new */ 1936 error = udf_dir_detach(tdnode->ump, fdnode, fnode, fcnp); 1937 if (error) 1938 udf_dir_detach(tdnode->ump, tdnode, fnode, tcnp); 1939 1940 out: 1941 if (fdnode != tdnode) 1942 VOP_UNLOCK(fdvp, 0); 1943 1944 out_unlocked: 1945 VOP_ABORTOP(tdvp, tcnp); 1946 if (tdvp == tvp) 1947 vrele(tdvp); 1948 else 1949 vput(tdvp); 1950 if (tvp) 1951 vput(tvp); 1952 VOP_ABORTOP(fdvp, fcnp); 1953 1954 /* release source nodes. */ 1955 vrele(fdvp); 1956 vrele(fvp); 1957 1958 return error; 1959 } 1960 1961 /* --------------------------------------------------------------------- */ 1962 1963 int 1964 udf_remove(void *v) 1965 { 1966 struct vop_remove_args /* { 1967 struct vnode *a_dvp; 1968 struct vnode *a_vp; 1969 struct componentname *a_cnp; 1970 } */ *ap = v; 1971 struct vnode *dvp = ap->a_dvp; 1972 struct vnode *vp = ap->a_vp; 1973 struct componentname *cnp = ap->a_cnp; 1974 struct udf_node *dir_node = VTOI(dvp);; 1975 struct udf_node *udf_node = VTOI(vp); 1976 struct udf_mount *ump = dir_node->ump; 1977 int error; 1978 1979 DPRINTF(CALL, ("udf_remove called\n")); 1980 if (vp->v_type != VDIR) { 1981 error = udf_dir_detach(ump, dir_node, udf_node, cnp); 1982 DPRINTFIF(NODE, error, ("\tgot error removing file\n")); 1983 } else { 1984 DPRINTF(NODE, ("\tis a directory: perm. denied\n")); 1985 error = EPERM; 1986 } 1987 1988 if (error == 0) { 1989 VN_KNOTE(vp, NOTE_DELETE); 1990 VN_KNOTE(dvp, NOTE_WRITE); 1991 } 1992 1993 if (dvp == vp) 1994 vrele(vp); 1995 else 1996 vput(vp); 1997 vput(dvp); 1998 1999 return error; 2000 } 2001 2002 /* --------------------------------------------------------------------- */ 2003 2004 int 2005 udf_rmdir(void *v) 2006 { 2007 struct vop_rmdir_args /* { 2008 struct vnode *a_dvp; 2009 struct vnode *a_vp; 2010 struct componentname *a_cnp; 2011 } */ *ap = v; 2012 struct vnode *vp = ap->a_vp; 2013 struct vnode *dvp = ap->a_dvp; 2014 struct componentname *cnp = ap->a_cnp; 2015 struct udf_node *dir_node = VTOI(dvp);; 2016 struct udf_node *udf_node = VTOI(vp); 2017 struct udf_mount *ump = dir_node->ump; 2018 int refcnt, error; 2019 2020 DPRINTF(NOTIMPL, ("udf_rmdir called\n")); 2021 2022 /* don't allow '.' to be deleted */ 2023 if (dir_node == udf_node) { 2024 vrele(dvp); 2025 vput(vp); 2026 return EINVAL; 2027 } 2028 2029 /* check to see if the directory is empty */ 2030 error = 0; 2031 if (dir_node->fe) { 2032 refcnt = udf_rw16(udf_node->fe->link_cnt); 2033 } else { 2034 refcnt = udf_rw16(udf_node->efe->link_cnt); 2035 } 2036 if (refcnt > 1) { 2037 /* NOT empty */ 2038 vput(dvp); 2039 vput(vp); 2040 return ENOTEMPTY; 2041 } 2042 2043 /* detach the node from the directory */ 2044 error = udf_dir_detach(ump, dir_node, udf_node, cnp); 2045 if (error == 0) { 2046 cache_purge(vp); 2047 // cache_purge(dvp); /* XXX from msdosfs, why? */ 2048 VN_KNOTE(vp, NOTE_DELETE); 2049 } 2050 DPRINTFIF(NODE, error, ("\tgot error removing file\n")); 2051 2052 /* unput the nodes and exit */ 2053 vput(dvp); 2054 vput(vp); 2055 2056 return error; 2057 } 2058 2059 /* --------------------------------------------------------------------- */ 2060 2061 int 2062 udf_fsync(void *v) 2063 { 2064 struct vop_fsync_args /* { 2065 struct vnode *a_vp; 2066 kauth_cred_t a_cred; 2067 int a_flags; 2068 off_t offlo; 2069 off_t offhi; 2070 struct proc *a_p; 2071 } */ *ap = v; 2072 struct vnode *vp = ap->a_vp; 2073 struct udf_node *udf_node = VTOI(vp); 2074 int error, flags, wait; 2075 2076 DPRINTF(SYNC, ("udf_fsync called on %p : %s, %s\n", 2077 udf_node, 2078 (ap->a_flags & FSYNC_WAIT) ? "wait":"no wait", 2079 (ap->a_flags & FSYNC_DATAONLY) ? "data_only":"complete")); 2080 2081 /* flush data and wait for it when requested */ 2082 wait = (ap->a_flags & FSYNC_WAIT) ? UPDATE_WAIT : 0; 2083 vflushbuf(vp, wait); 2084 2085 if (udf_node == NULL) { 2086 printf("udf_fsync() called on NULL udf_node!\n"); 2087 return 0; 2088 } 2089 if (vp->v_tag != VT_UDF) { 2090 printf("udf_fsync() called on node not tagged as UDF node!\n"); 2091 return 0; 2092 } 2093 2094 /* set our times */ 2095 udf_itimes(udf_node, NULL, NULL, NULL); 2096 2097 /* if called when mounted readonly, never write back */ 2098 if (vp->v_mount->mnt_flag & MNT_RDONLY) 2099 return 0; 2100 2101 /* if only data is requested, return */ 2102 if (ap->a_flags & FSYNC_DATAONLY) 2103 return 0; 2104 2105 /* check if the node is dirty 'enough'*/ 2106 flags = udf_node->i_flags & (IN_MODIFIED | IN_ACCESSED); 2107 if (flags == 0) 2108 return 0; 2109 2110 /* if we don't have to wait, check for IO pending */ 2111 if (!wait) { 2112 if (vp->v_numoutput > 0) { 2113 DPRINTF(SYNC, ("udf_fsync %p, rejecting on v_numoutput\n", udf_node)); 2114 return 0; 2115 } 2116 if (udf_node->outstanding_bufs > 0) { 2117 DPRINTF(SYNC, ("udf_fsync %p, rejecting on outstanding_bufs\n", udf_node)); 2118 return 0; 2119 } 2120 if (udf_node->outstanding_nodedscr > 0) { 2121 DPRINTF(SYNC, ("udf_fsync %p, rejecting on outstanding_nodedscr\n", udf_node)); 2122 return 0; 2123 } 2124 } 2125 2126 /* wait until vp->v_numoutput reaches zero i.e. is finished */ 2127 if (wait) { 2128 DPRINTF(SYNC, ("udf_fsync %p, waiting\n", udf_node)); 2129 mutex_enter(&vp->v_interlock); 2130 while (vp->v_numoutput) { 2131 DPRINTF(SYNC, ("udf_fsync %p, v_numoutput %d\n", udf_node, vp->v_numoutput)); 2132 cv_timedwait(&vp->v_cv, &vp->v_interlock, hz/8); 2133 } 2134 mutex_exit(&vp->v_interlock); 2135 DPRINTF(SYNC, ("udf_fsync %p, fin wait\n", udf_node)); 2136 } 2137 2138 /* write out node and wait for it if requested */ 2139 DPRINTF(SYNC, ("udf_fsync %p, writeout node\n", udf_node)); 2140 error = udf_writeout_node(udf_node, wait); 2141 if (error) 2142 return error; 2143 2144 /* TODO/XXX if ap->a_flags & FSYNC_CACHE, we ought to do a disc sync */ 2145 2146 return 0; 2147 } 2148 2149 /* --------------------------------------------------------------------- */ 2150 2151 int 2152 udf_advlock(void *v) 2153 { 2154 struct vop_advlock_args /* { 2155 struct vnode *a_vp; 2156 void *a_id; 2157 int a_op; 2158 struct flock *a_fl; 2159 int a_flags; 2160 } */ *ap = v; 2161 struct vnode *vp = ap->a_vp; 2162 struct udf_node *udf_node = VTOI(vp); 2163 struct file_entry *fe; 2164 struct extfile_entry *efe; 2165 uint64_t file_size; 2166 2167 DPRINTF(LOCKING, ("udf_advlock called\n")); 2168 2169 /* get directory filesize */ 2170 if (udf_node->fe) { 2171 fe = udf_node->fe; 2172 file_size = udf_rw64(fe->inf_len); 2173 } else { 2174 assert(udf_node->efe); 2175 efe = udf_node->efe; 2176 file_size = udf_rw64(efe->inf_len); 2177 } 2178 2179 return lf_advlock(ap, &udf_node->lockf, file_size); 2180 } 2181 2182 /* --------------------------------------------------------------------- */ 2183 2184 2185 /* Global vfs vnode data structures for udfs */ 2186 int (**udf_vnodeop_p) __P((void *)); 2187 2188 const struct vnodeopv_entry_desc udf_vnodeop_entries[] = { 2189 { &vop_default_desc, vn_default_error }, 2190 { &vop_lookup_desc, udf_lookup }, /* lookup */ 2191 { &vop_create_desc, udf_create }, /* create */ 2192 { &vop_mknod_desc, udf_mknod }, /* mknod */ /* TODO */ 2193 { &vop_open_desc, udf_open }, /* open */ 2194 { &vop_close_desc, udf_close }, /* close */ 2195 { &vop_access_desc, udf_access }, /* access */ 2196 { &vop_getattr_desc, udf_getattr }, /* getattr */ 2197 { &vop_setattr_desc, udf_setattr }, /* setattr */ /* TODO chflags */ 2198 { &vop_read_desc, udf_read }, /* read */ 2199 { &vop_write_desc, udf_write }, /* write */ /* WRITE */ 2200 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */ /* TODO? */ 2201 { &vop_ioctl_desc, genfs_enoioctl }, /* ioctl */ /* TODO? */ 2202 { &vop_poll_desc, genfs_poll }, /* poll */ /* TODO/OK? */ 2203 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */ /* ? */ 2204 { &vop_revoke_desc, genfs_revoke }, /* revoke */ /* TODO? */ 2205 { &vop_mmap_desc, genfs_mmap }, /* mmap */ /* OK? */ 2206 { &vop_fsync_desc, udf_fsync }, /* fsync */ 2207 { &vop_seek_desc, genfs_seek }, /* seek */ 2208 { &vop_remove_desc, udf_remove }, /* remove */ 2209 { &vop_link_desc, udf_link }, /* link */ /* TODO */ 2210 { &vop_rename_desc, udf_rename }, /* rename */ /* TODO */ 2211 { &vop_mkdir_desc, udf_mkdir }, /* mkdir */ 2212 { &vop_rmdir_desc, udf_rmdir }, /* rmdir */ 2213 { &vop_symlink_desc, udf_symlink }, /* symlink */ /* TODO */ 2214 { &vop_readdir_desc, udf_readdir }, /* readdir */ 2215 { &vop_readlink_desc, udf_readlink }, /* readlink */ /* TEST ME */ 2216 { &vop_abortop_desc, genfs_abortop }, /* abortop */ /* TODO/OK? */ 2217 { &vop_inactive_desc, udf_inactive }, /* inactive */ 2218 { &vop_reclaim_desc, udf_reclaim }, /* reclaim */ 2219 { &vop_lock_desc, genfs_lock }, /* lock */ 2220 { &vop_unlock_desc, genfs_unlock }, /* unlock */ 2221 { &vop_bmap_desc, udf_trivial_bmap }, /* bmap */ /* 1:1 bmap */ 2222 { &vop_strategy_desc, udf_vfsstrategy },/* strategy */ 2223 /* { &vop_print_desc, udf_print }, */ /* print */ 2224 { &vop_islocked_desc, genfs_islocked }, /* islocked */ 2225 { &vop_pathconf_desc, udf_pathconf }, /* pathconf */ 2226 { &vop_advlock_desc, udf_advlock }, /* advlock */ /* TEST ME */ 2227 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */ /* ->strategy */ 2228 { &vop_getpages_desc, genfs_getpages }, /* getpages */ 2229 { &vop_putpages_desc, genfs_putpages }, /* putpages */ 2230 { NULL, NULL } 2231 }; 2232 2233 2234 const struct vnodeopv_desc udf_vnodeop_opv_desc = { 2235 &udf_vnodeop_p, udf_vnodeop_entries 2236 }; 2237 2238