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