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