1 /* $NetBSD: udf_vnops.c,v 1.38 2009/03/20 23:06:52 reinoud Exp $ */ 2 3 /* 4 * Copyright (c) 2006, 2008 Reinoud Zandijk 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 * Generic parts are derived from software contributed to The NetBSD Foundation 28 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code 29 * 2005 program. 30 * 31 */ 32 33 #include <sys/cdefs.h> 34 #ifndef lint 35 __KERNEL_RCSID(0, "$NetBSD: udf_vnops.c,v 1.38 2009/03/20 23:06:52 reinoud Exp $"); 36 #endif /* not lint */ 37 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/namei.h> 42 #include <sys/resourcevar.h> /* defines plimit structure in proc struct */ 43 #include <sys/kernel.h> 44 #include <sys/file.h> /* define FWRITE ... */ 45 #include <sys/stat.h> 46 #include <sys/buf.h> 47 #include <sys/proc.h> 48 #include <sys/mount.h> 49 #include <sys/vnode.h> 50 #include <sys/signalvar.h> 51 #include <sys/malloc.h> 52 #include <sys/dirent.h> 53 #include <sys/lockf.h> 54 #include <sys/kauth.h> 55 56 #include <miscfs/genfs/genfs.h> 57 #include <uvm/uvm_extern.h> 58 59 #include <fs/udf/ecma167-udf.h> 60 #include <fs/udf/udf_mount.h> 61 #include "udf.h" 62 #include "udf_subr.h" 63 #include "udf_bswap.h" 64 65 66 #define VTOI(vnode) ((struct udf_node *) (vnode)->v_data) 67 68 69 /* externs */ 70 extern int prtactive; 71 72 /* implementations of vnode functions; table follows at end */ 73 /* --------------------------------------------------------------------- */ 74 75 int 76 udf_inactive(void *v) 77 { 78 struct vop_inactive_args /* { 79 struct vnode *a_vp; 80 bool *a_recycle; 81 } */ *ap = v; 82 struct vnode *vp = ap->a_vp; 83 struct udf_node *udf_node = VTOI(vp); 84 int refcnt; 85 86 DPRINTF(NODE, ("udf_inactive called for udf_node %p\n", VTOI(vp))); 87 88 if (udf_node == NULL) { 89 DPRINTF(NODE, ("udf_inactive: inactive NULL UDF node\n")); 90 VOP_UNLOCK(vp, 0); 91 return 0; 92 } 93 94 /* 95 * Optionally flush metadata to disc. If the file has not been 96 * referenced anymore in a directory we ought to free up the resources 97 * on disc if applicable. 98 */ 99 if (udf_node->fe) { 100 refcnt = udf_rw16(udf_node->fe->link_cnt); 101 } else { 102 assert(udf_node->efe); 103 refcnt = udf_rw16(udf_node->efe->link_cnt); 104 } 105 106 if ((refcnt == 0) && (vp->v_vflag & VV_SYSTEM)) 107 DPRINTF(VOLUMES, ("UDF_INACTIVE deleting VV_SYSTEM\n")); 108 109 *ap->a_recycle = false; 110 if ((refcnt == 0) && ((vp->v_vflag & VV_SYSTEM) == 0)) { 111 /* remove this file's allocation */ 112 DPRINTF(NODE, ("udf_inactive deleting unlinked file\n")); 113 *ap->a_recycle = true; 114 udf_delete_node(udf_node); 115 VOP_UNLOCK(vp, 0); 116 vrecycle(vp, NULL, curlwp); 117 return 0; 118 } 119 120 /* write out its node */ 121 if (udf_node->i_flags & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) 122 udf_update(vp, NULL, NULL, NULL, 0); 123 VOP_UNLOCK(vp, 0); 124 125 return 0; 126 } 127 128 /* --------------------------------------------------------------------- */ 129 130 int udf_sync(struct mount *mp, int waitfor, kauth_cred_t cred, struct lwp *lwp); 131 132 int 133 udf_reclaim(void *v) 134 { 135 struct vop_reclaim_args /* { 136 struct vnode *a_vp; 137 } */ *ap = v; 138 struct vnode *vp = ap->a_vp; 139 struct udf_node *udf_node = VTOI(vp); 140 141 DPRINTF(NODE, ("udf_reclaim called for node %p\n", udf_node)); 142 if (prtactive && vp->v_usecount > 1) 143 vprint("udf_reclaim(): pushing active", vp); 144 145 if (udf_node == NULL) { 146 DPRINTF(NODE, ("udf_reclaim(): null udfnode\n")); 147 return 0; 148 } 149 150 /* update note for closure */ 151 udf_update(vp, NULL, NULL, NULL, UPDATE_CLOSE); 152 153 /* async check to see if all node descriptors are written out */ 154 while ((volatile int) udf_node->outstanding_nodedscr > 0) { 155 vprint("udf_reclaim(): waiting for writeout\n", vp); 156 tsleep(&udf_node->outstanding_nodedscr, PRIBIO, "recl wait", hz/8); 157 } 158 159 /* purge old data from namei */ 160 cache_purge(vp); 161 162 /* dispose all node knowledge */ 163 udf_dispose_node(udf_node); 164 165 return 0; 166 } 167 168 /* --------------------------------------------------------------------- */ 169 170 int 171 udf_read(void *v) 172 { 173 struct vop_read_args /* { 174 struct vnode *a_vp; 175 struct uio *a_uio; 176 int a_ioflag; 177 kauth_cred_t a_cred; 178 } */ *ap = v; 179 struct vnode *vp = ap->a_vp; 180 struct uio *uio = ap->a_uio; 181 int ioflag = ap->a_ioflag; 182 int advice = IO_ADV_DECODE(ap->a_ioflag); 183 struct uvm_object *uobj; 184 struct udf_node *udf_node = VTOI(vp); 185 struct file_entry *fe; 186 struct extfile_entry *efe; 187 uint64_t file_size; 188 vsize_t len; 189 int error; 190 191 /* 192 * XXX reading from extended attributes not yet implemented. FreeBSD 193 * has it in mind to forward the IO_EXT read call to the 194 * VOP_READEXTATTR(). 195 */ 196 197 DPRINTF(READ, ("udf_read called\n")); 198 199 /* can this happen? some filingsystems have this check */ 200 if (uio->uio_offset < 0) 201 return EINVAL; 202 if (uio->uio_resid == 0) 203 return 0; 204 205 /* protect against rogue programs reading raw directories and links */ 206 if ((ioflag & IO_ALTSEMANTICS) == 0) { 207 if (vp->v_type == VDIR) 208 return EISDIR; 209 /* all but regular files just give EINVAL */ 210 if (vp->v_type != VREG) 211 return EINVAL; 212 } 213 214 assert(udf_node); 215 assert(udf_node->fe || udf_node->efe); 216 217 /* get file/directory filesize */ 218 if (udf_node->fe) { 219 fe = udf_node->fe; 220 file_size = udf_rw64(fe->inf_len); 221 } else { 222 assert(udf_node->efe); 223 efe = udf_node->efe; 224 file_size = udf_rw64(efe->inf_len); 225 } 226 227 /* read contents using buffercache */ 228 uobj = &vp->v_uobj; 229 error = 0; 230 while (uio->uio_resid > 0) { 231 /* reached end? */ 232 if (file_size <= uio->uio_offset) 233 break; 234 235 /* maximise length to file extremity */ 236 len = MIN(file_size - uio->uio_offset, uio->uio_resid); 237 if (len == 0) 238 break; 239 240 /* ubc, here we come, prepare to trap */ 241 error = ubc_uiomove(uobj, uio, len, advice, 242 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp)); 243 if (error) 244 break; 245 } 246 247 /* note access time unless not requested */ 248 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) { 249 udf_node->i_flags |= IN_ACCESS; 250 if ((ioflag & IO_SYNC) == IO_SYNC) 251 error = udf_update(vp, NULL, NULL, NULL, UPDATE_WAIT); 252 } 253 254 return error; 255 } 256 257 /* --------------------------------------------------------------------- */ 258 259 int 260 udf_write(void *v) 261 { 262 struct vop_write_args /* { 263 struct vnode *a_vp; 264 struct uio *a_uio; 265 int a_ioflag; 266 kauth_cred_t a_cred; 267 } */ *ap = v; 268 struct vnode *vp = ap->a_vp; 269 struct uio *uio = ap->a_uio; 270 int ioflag = ap->a_ioflag; 271 int advice = IO_ADV_DECODE(ap->a_ioflag); 272 struct uvm_object *uobj; 273 struct udf_node *udf_node = VTOI(vp); 274 struct file_entry *fe; 275 struct extfile_entry *efe; 276 uint64_t file_size, old_size, 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 euid, uid; 939 gid_t egid, gid; 940 int issuperuser, ismember; 941 int error; 942 943 #ifdef notyet 944 /* TODO get vaflags from the extended attributes? */ 945 /* Immutable or append-only files cannot be modified, either. */ 946 if (udf_node->flags & (IMMUTABLE | APPEND)) 947 return EPERM; 948 #endif 949 950 if (vp->v_mount->mnt_flag & MNT_RDONLY) 951 return EROFS; 952 953 /* retrieve old values */ 954 udf_getownership(udf_node, &uid, &gid); 955 956 /* only one could be specified */ 957 if (new_uid == VNOVAL) 958 new_uid = uid; 959 if (new_gid == VNOVAL) 960 new_gid = gid; 961 962 /* check if we can fit it in an 32 bits */ 963 if ((uid_t) ((uint32_t) uid) != uid) 964 return EINVAL; 965 if ((gid_t) ((uint32_t) gid) != gid) 966 return EINVAL; 967 968 /* 969 * If we don't own the file, are trying to change the owner of the 970 * file, or are not a member of the target group, the caller's 971 * credentials must imply super-user privilege or the call fails. 972 */ 973 974 /* check permissions */ 975 euid = kauth_cred_geteuid(cred); 976 egid = kauth_cred_getegid(cred); 977 if ((error = kauth_cred_ismember_gid(cred, new_gid, &ismember))) 978 return error; 979 error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL); 980 issuperuser = (error == 0); 981 982 if (!issuperuser) { 983 if ((new_uid != uid) || (euid != uid)) 984 return EPERM; 985 if ((new_gid != gid) && !(egid == new_gid || ismember)) 986 return EPERM; 987 } 988 989 /* change the ownership */ 990 udf_setownership(udf_node, new_uid, new_gid); 991 992 /* mark node changed */ 993 udf_node->i_flags |= IN_CHANGE; 994 995 return 0; 996 } 997 998 999 static int 1000 udf_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred) 1001 { 1002 struct udf_node *udf_node = VTOI(vp); 1003 uid_t euid, uid; 1004 gid_t egid, gid; 1005 int issuperuser, ismember; 1006 int error; 1007 1008 #ifdef notyet 1009 /* TODO get vaflags from the extended attributes? */ 1010 /* Immutable or append-only files cannot be modified, either. */ 1011 if (udf_node->flags & (IMMUTABLE | APPEND)) 1012 return EPERM; 1013 #endif 1014 1015 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1016 return EROFS; 1017 1018 /* retrieve uid/gid values */ 1019 udf_getownership(udf_node, &uid, &gid); 1020 1021 /* check permissions */ 1022 euid = kauth_cred_geteuid(cred); 1023 egid = kauth_cred_getegid(cred); 1024 if ((error = kauth_cred_ismember_gid(cred, gid, &ismember))) 1025 return error; 1026 error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL); 1027 issuperuser = (error == 0); 1028 1029 if ((euid != uid) && !issuperuser) 1030 return EPERM; 1031 if (euid != 0) { 1032 if (vp->v_type != VDIR && (mode & S_ISTXT)) 1033 return EFTYPE; 1034 1035 if ((!ismember) && (mode & S_ISGID)) 1036 return EPERM; 1037 } 1038 1039 /* change mode */ 1040 udf_setaccessmode(udf_node, mode); 1041 1042 /* mark node changed */ 1043 udf_node->i_flags |= IN_CHANGE; 1044 1045 return 0; 1046 } 1047 1048 1049 /* exported */ 1050 int 1051 udf_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred) 1052 { 1053 struct udf_node *udf_node = VTOI(vp); 1054 int error, extended; 1055 1056 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1057 return EROFS; 1058 1059 /* Decide whether this is a valid operation based on the file type. */ 1060 switch (vp->v_type) { 1061 case VDIR: 1062 return EISDIR; 1063 case VREG: 1064 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1065 return EROFS; 1066 break; 1067 case VBLK: 1068 /* FALLTHROUGH */ 1069 case VCHR: 1070 /* FALLTHROUGH */ 1071 case VFIFO: 1072 /* Allow modifications of special files even if in the file 1073 * system is mounted read-only (we are not modifying the 1074 * files themselves, but the objects they represent). */ 1075 return 0; 1076 default: 1077 /* Anything else is unsupported. */ 1078 return EOPNOTSUPP; 1079 } 1080 1081 #if notyet 1082 /* TODO get vaflags from the extended attributes? */ 1083 /* Immutable or append-only files cannot be modified, either. */ 1084 if (node->flags & (IMMUTABLE | APPEND)) 1085 return EPERM; 1086 #endif 1087 1088 /* resize file to the requested size */ 1089 error = udf_resize_node(udf_node, newsize, &extended); 1090 1091 if (error == 0) { 1092 /* mark change */ 1093 udf_node->i_flags |= IN_CHANGE | IN_MODIFY; 1094 VN_KNOTE(vp, NOTE_ATTRIB | (extended ? NOTE_EXTEND : 0)); 1095 udf_update(vp, NULL, NULL, NULL, 0); 1096 } 1097 1098 return error; 1099 } 1100 1101 1102 static int 1103 udf_chflags(struct vnode *vp, mode_t mode, kauth_cred_t cred) 1104 { 1105 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1106 return EROFS; 1107 1108 /* XXX we can't do this yet XXX */ 1109 1110 return EINVAL; 1111 } 1112 1113 1114 static int 1115 udf_chtimes(struct vnode *vp, 1116 struct timespec *atime, struct timespec *mtime, 1117 struct timespec *birthtime, int setattrflags, 1118 kauth_cred_t cred) 1119 { 1120 struct udf_node *udf_node = VTOI(vp); 1121 uid_t euid, uid; 1122 gid_t gid; 1123 int issuperuser; 1124 int error; 1125 1126 #ifdef notyet 1127 /* TODO get vaflags from the extended attributes? */ 1128 /* Immutable or append-only files cannot be modified, either. */ 1129 if (udf_node->flags & (IMMUTABLE | APPEND)) 1130 return EPERM; 1131 #endif 1132 1133 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1134 return EROFS; 1135 1136 /* retrieve uid/gid values */ 1137 udf_getownership(udf_node, &uid, &gid); 1138 1139 /* check permissions */ 1140 euid = kauth_cred_geteuid(cred); 1141 error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL); 1142 issuperuser = (error == 0); 1143 1144 if (!issuperuser) { 1145 if (euid != uid) 1146 return EPERM; 1147 if ((setattrflags & VA_UTIMES_NULL) == 0) { 1148 error = VOP_ACCESS(vp, VWRITE, cred); 1149 if (error) 1150 return error; 1151 } 1152 } 1153 1154 /* update node flags depending on what times are passed */ 1155 if (atime->tv_sec != VNOVAL) 1156 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) 1157 udf_node->i_flags |= IN_ACCESS; 1158 if ((mtime->tv_sec != VNOVAL) || (birthtime->tv_sec != VNOVAL)) 1159 udf_node->i_flags |= IN_CHANGE | IN_UPDATE; 1160 1161 return udf_update(vp, atime, mtime, birthtime, 0); 1162 } 1163 1164 1165 int 1166 udf_setattr(void *v) 1167 { 1168 struct vop_setattr_args /* { 1169 struct vnode *a_vp; 1170 struct vattr *a_vap; 1171 kauth_cred_t a_cred; 1172 struct lwp *a_l; 1173 } */ *ap = v; 1174 struct vnode *vp = ap->a_vp; 1175 /* struct udf_node *udf_node = VTOI(vp); */ 1176 /* struct udf_mount *ump = udf_node->ump; */ 1177 kauth_cred_t cred = ap->a_cred; 1178 struct vattr *vap = ap->a_vap; 1179 int error; 1180 1181 DPRINTF(CALL, ("udf_setattr called\n")); 1182 1183 /* Abort if any unsettable attribute is given. */ 1184 error = 0; 1185 if (vap->va_type != VNON || 1186 vap->va_nlink != VNOVAL || 1187 vap->va_fsid != VNOVAL || 1188 vap->va_fileid != VNOVAL || 1189 vap->va_blocksize != VNOVAL || 1190 #ifdef notyet 1191 /* checks are debated */ 1192 vap->va_ctime.tv_sec != VNOVAL || 1193 vap->va_ctime.tv_nsec != VNOVAL || 1194 vap->va_birthtime.tv_sec != VNOVAL || 1195 vap->va_birthtime.tv_nsec != VNOVAL || 1196 #endif 1197 vap->va_gen != VNOVAL || 1198 vap->va_rdev != VNOVAL || 1199 vap->va_bytes != VNOVAL) 1200 error = EINVAL; 1201 1202 DPRINTF(ATTR, ("setattr changing:\n")); 1203 if (error == 0 && (vap->va_flags != VNOVAL)) { 1204 DPRINTF(ATTR, ("\tchflags\n")); 1205 error = udf_chflags(vp, vap->va_flags, cred); 1206 } 1207 1208 if (error == 0 && (vap->va_size != VNOVAL)) { 1209 DPRINTF(ATTR, ("\tchsize\n")); 1210 error = udf_chsize(vp, vap->va_size, cred); 1211 } 1212 1213 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL)) { 1214 DPRINTF(ATTR, ("\tchown\n")); 1215 error = udf_chown(vp, vap->va_uid, vap->va_gid, cred); 1216 } 1217 1218 if (error == 0 && (vap->va_mode != VNOVAL)) { 1219 DPRINTF(ATTR, ("\tchmod\n")); 1220 error = udf_chmod(vp, vap->va_mode, cred); 1221 } 1222 1223 if (error == 0 && 1224 ((vap->va_atime.tv_sec != VNOVAL && 1225 vap->va_atime.tv_nsec != VNOVAL) || 1226 (vap->va_mtime.tv_sec != VNOVAL && 1227 vap->va_mtime.tv_nsec != VNOVAL)) 1228 ) { 1229 DPRINTF(ATTR, ("\tchtimes\n")); 1230 error = udf_chtimes(vp, &vap->va_atime, &vap->va_mtime, 1231 &vap->va_birthtime, vap->va_vaflags, cred); 1232 } 1233 VN_KNOTE(vp, NOTE_ATTRIB); 1234 1235 return error; 1236 } 1237 1238 /* --------------------------------------------------------------------- */ 1239 1240 /* 1241 * Return POSIX pathconf information for UDF file systems. 1242 */ 1243 int 1244 udf_pathconf(void *v) 1245 { 1246 struct vop_pathconf_args /* { 1247 struct vnode *a_vp; 1248 int a_name; 1249 register_t *a_retval; 1250 } */ *ap = v; 1251 uint32_t bits; 1252 1253 DPRINTF(CALL, ("udf_pathconf called\n")); 1254 1255 switch (ap->a_name) { 1256 case _PC_LINK_MAX: 1257 *ap->a_retval = (1<<16)-1; /* 16 bits */ 1258 return 0; 1259 case _PC_NAME_MAX: 1260 *ap->a_retval = NAME_MAX; 1261 return 0; 1262 case _PC_PATH_MAX: 1263 *ap->a_retval = PATH_MAX; 1264 return 0; 1265 case _PC_PIPE_BUF: 1266 *ap->a_retval = PIPE_BUF; 1267 return 0; 1268 case _PC_CHOWN_RESTRICTED: 1269 *ap->a_retval = 1; 1270 return 0; 1271 case _PC_NO_TRUNC: 1272 *ap->a_retval = 1; 1273 return 0; 1274 case _PC_SYNC_IO: 1275 *ap->a_retval = 0; /* synchronised is off for performance */ 1276 return 0; 1277 case _PC_FILESIZEBITS: 1278 /* 64 bit file offsets -> 2+floor(2log(2^64-1)) = 2 + 63 = 65 */ 1279 bits = 64; /* XXX ought to deliver 65 */ 1280 #if 0 1281 if (udf_node) 1282 bits = 64 * vp->v_mount->mnt_dev_bshift; 1283 #endif 1284 *ap->a_retval = bits; 1285 return 0; 1286 } 1287 1288 return EINVAL; 1289 } 1290 1291 1292 /* --------------------------------------------------------------------- */ 1293 1294 int 1295 udf_open(void *v) 1296 { 1297 struct vop_open_args /* { 1298 struct vnode *a_vp; 1299 int a_mode; 1300 kauth_cred_t a_cred; 1301 struct proc *a_p; 1302 } */ *ap = v; 1303 int flags; 1304 1305 DPRINTF(CALL, ("udf_open called\n")); 1306 1307 /* 1308 * Files marked append-only must be opened for appending. 1309 * TODO: get chflags(2) flags from extened attribute. 1310 */ 1311 flags = 0; 1312 if ((flags & APPEND) && (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) 1313 return (EPERM); 1314 1315 return 0; 1316 } 1317 1318 1319 /* --------------------------------------------------------------------- */ 1320 1321 int 1322 udf_close(void *v) 1323 { 1324 struct vop_close_args /* { 1325 struct vnode *a_vp; 1326 int a_fflag; 1327 kauth_cred_t a_cred; 1328 struct proc *a_p; 1329 } */ *ap = v; 1330 struct vnode *vp = ap->a_vp; 1331 struct udf_node *udf_node = VTOI(vp); 1332 1333 DPRINTF(CALL, ("udf_close called\n")); 1334 udf_node = udf_node; /* shut up gcc */ 1335 1336 mutex_enter(&vp->v_interlock); 1337 if (vp->v_usecount > 1) 1338 udf_itimes(udf_node, NULL, NULL, NULL); 1339 mutex_exit(&vp->v_interlock); 1340 1341 return 0; 1342 } 1343 1344 1345 /* --------------------------------------------------------------------- */ 1346 1347 int 1348 udf_access(void *v) 1349 { 1350 struct vop_access_args /* { 1351 struct vnode *a_vp; 1352 int a_mode; 1353 kauth_cred_t a_cred; 1354 struct proc *a_p; 1355 } */ *ap = v; 1356 struct vnode *vp = ap->a_vp; 1357 mode_t mode = ap->a_mode; 1358 kauth_cred_t cred = ap->a_cred; 1359 /* struct udf_node *udf_node = VTOI(vp); */ 1360 struct vattr vap; 1361 int flags; 1362 int error; 1363 1364 DPRINTF(CALL, ("udf_access called\n")); 1365 1366 error = VOP_GETATTR(vp, &vap, NULL); 1367 if (error) 1368 return error; 1369 1370 /* check if we are allowed to write */ 1371 switch (vap.va_type) { 1372 case VDIR: 1373 case VLNK: 1374 case VREG: 1375 /* 1376 * normal nodes: check if we're on a read-only mounted 1377 * filingsystem and bomb out if we're trying to write. 1378 */ 1379 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) 1380 return EROFS; 1381 break; 1382 case VBLK: 1383 case VCHR: 1384 case VSOCK: 1385 case VFIFO: 1386 /* 1387 * special nodes: even on read-only mounted filingsystems 1388 * these are allowed to be written to if permissions allow. 1389 */ 1390 break; 1391 default: 1392 /* no idea what this is */ 1393 return EINVAL; 1394 } 1395 1396 /* noone may write immutable files */ 1397 /* TODO: get chflags(2) flags from extened attribute. */ 1398 flags = 0; 1399 if ((mode & VWRITE) && (flags & IMMUTABLE)) 1400 return EPERM; 1401 1402 /* ask the generic vaccess to advice on security */ 1403 return vaccess(vp->v_type, 1404 vap.va_mode, vap.va_uid, vap.va_gid, 1405 mode, cred); 1406 } 1407 1408 /* --------------------------------------------------------------------- */ 1409 1410 int 1411 udf_create(void *v) 1412 { 1413 struct vop_create_args /* { 1414 struct vnode *a_dvp; 1415 struct vnode **a_vpp; 1416 struct componentname *a_cnp; 1417 struct vattr *a_vap; 1418 } */ *ap = v; 1419 struct vnode *dvp = ap->a_dvp; 1420 struct vnode **vpp = ap->a_vpp; 1421 struct vattr *vap = ap->a_vap; 1422 struct componentname *cnp = ap->a_cnp; 1423 int error; 1424 1425 DPRINTF(CALL, ("udf_create called\n")); 1426 error = udf_create_node(dvp, vpp, vap, cnp); 1427 1428 if (error || !(cnp->cn_flags & SAVESTART)) 1429 PNBUF_PUT(cnp->cn_pnbuf); 1430 vput(dvp); 1431 return error; 1432 } 1433 1434 /* --------------------------------------------------------------------- */ 1435 1436 int 1437 udf_mknod(void *v) 1438 { 1439 struct vop_mknod_args /* { 1440 struct vnode *a_dvp; 1441 struct vnode **a_vpp; 1442 struct componentname *a_cnp; 1443 struct vattr *a_vap; 1444 } */ *ap = v; 1445 struct vnode *dvp = ap->a_dvp; 1446 struct vnode **vpp = ap->a_vpp; 1447 struct vattr *vap = ap->a_vap; 1448 struct componentname *cnp = ap->a_cnp; 1449 int error; 1450 1451 DPRINTF(CALL, ("udf_mknod called\n")); 1452 error = udf_create_node(dvp, vpp, vap, cnp); 1453 1454 if (error || !(cnp->cn_flags & SAVESTART)) 1455 PNBUF_PUT(cnp->cn_pnbuf); 1456 vput(dvp); 1457 return error; 1458 } 1459 1460 /* --------------------------------------------------------------------- */ 1461 1462 int 1463 udf_mkdir(void *v) 1464 { 1465 struct vop_mkdir_args /* { 1466 struct vnode *a_dvp; 1467 struct vnode **a_vpp; 1468 struct componentname *a_cnp; 1469 struct vattr *a_vap; 1470 } */ *ap = v; 1471 struct vnode *dvp = ap->a_dvp; 1472 struct vnode **vpp = ap->a_vpp; 1473 struct vattr *vap = ap->a_vap; 1474 struct componentname *cnp = ap->a_cnp; 1475 int error; 1476 1477 DPRINTF(CALL, ("udf_mkdir called\n")); 1478 error = udf_create_node(dvp, vpp, vap, cnp); 1479 1480 if (error || !(cnp->cn_flags & SAVESTART)) 1481 PNBUF_PUT(cnp->cn_pnbuf); 1482 vput(dvp); 1483 return error; 1484 } 1485 1486 /* --------------------------------------------------------------------- */ 1487 1488 static int 1489 udf_do_link(struct vnode *dvp, struct vnode *vp, struct componentname *cnp) 1490 { 1491 struct udf_node *udf_node, *dir_node; 1492 struct vattr vap; 1493 int error; 1494 1495 DPRINTF(CALL, ("udf_link called\n")); 1496 error = 0; 1497 1498 /* some quick checks */ 1499 if (vp->v_type == VDIR) 1500 return EPERM; /* can't link a directory */ 1501 if (dvp->v_mount != vp->v_mount) 1502 return EXDEV; /* can't link across devices */ 1503 if (dvp == vp) 1504 return EPERM; /* can't be the same */ 1505 1506 /* lock node */ 1507 error = vn_lock(vp, LK_EXCLUSIVE); 1508 if (error) 1509 return error; 1510 1511 /* get attributes */ 1512 dir_node = VTOI(dvp); 1513 udf_node = VTOI(vp); 1514 1515 error = VOP_GETATTR(vp, &vap, FSCRED); 1516 if (error) 1517 return error; 1518 1519 /* check link count overflow */ 1520 if (vap.va_nlink >= (1<<16)-1) /* uint16_t */ 1521 return EMLINK; 1522 1523 return udf_dir_attach(dir_node->ump, dir_node, udf_node, &vap, cnp); 1524 } 1525 1526 int 1527 udf_link(void *v) 1528 { 1529 struct vop_link_args /* { 1530 struct vnode *a_dvp; 1531 struct vnode *a_vp; 1532 struct componentname *a_cnp; 1533 } */ *ap = v; 1534 struct vnode *dvp = ap->a_dvp; 1535 struct vnode *vp = ap->a_vp; 1536 struct componentname *cnp = ap->a_cnp; 1537 int error; 1538 1539 error = udf_do_link(dvp, vp, cnp); 1540 if (error) 1541 VOP_ABORTOP(dvp, cnp); 1542 1543 if ((vp != dvp) && (VOP_ISLOCKED(vp) == LK_EXCLUSIVE)) 1544 VOP_UNLOCK(vp, 0); 1545 1546 VN_KNOTE(vp, NOTE_LINK); 1547 VN_KNOTE(dvp, NOTE_WRITE); 1548 vput(dvp); 1549 1550 return error; 1551 } 1552 1553 /* --------------------------------------------------------------------- */ 1554 1555 static int 1556 udf_do_symlink(struct udf_node *udf_node, char *target) 1557 { 1558 struct pathcomp pathcomp; 1559 uint8_t *pathbuf, *pathpos, *compnamepos; 1560 char *mntonname; 1561 int pathlen, len, compnamelen, mntonnamelen; 1562 int error; 1563 1564 /* process `target' to an UDF structure */ 1565 pathbuf = malloc(UDF_SYMLINKBUFLEN, M_UDFTEMP, M_WAITOK); 1566 pathpos = pathbuf; 1567 pathlen = 0; 1568 1569 if (*target == '/') { 1570 /* symlink starts from the root */ 1571 len = UDF_PATH_COMP_SIZE; 1572 memset(&pathcomp, 0, len); 1573 pathcomp.type = UDF_PATH_COMP_ROOT; 1574 1575 /* check if its mount-point relative! */ 1576 mntonname = udf_node->ump->vfs_mountp->mnt_stat.f_mntonname; 1577 mntonnamelen = strlen(mntonname); 1578 if (strlen(target) >= mntonnamelen) { 1579 if (strncmp(target, mntonname, mntonnamelen) == 0) { 1580 pathcomp.type = UDF_PATH_COMP_MOUNTROOT; 1581 target += mntonnamelen; 1582 } 1583 } else { 1584 target++; 1585 } 1586 1587 memcpy(pathpos, &pathcomp, len); 1588 pathpos += len; 1589 pathlen += len; 1590 } 1591 1592 error = 0; 1593 while (*target) { 1594 /* ignore multiple '/' */ 1595 while (*target == '/') { 1596 target++; 1597 } 1598 if (!*target) 1599 break; 1600 1601 /* extract component name */ 1602 compnamelen = 0; 1603 compnamepos = target; 1604 while ((*target) && (*target != '/')) { 1605 target++; 1606 compnamelen++; 1607 } 1608 1609 /* just trunc if too long ?? (security issue) */ 1610 if (compnamelen >= 127) { 1611 error = ENAMETOOLONG; 1612 break; 1613 } 1614 1615 /* convert unix name to UDF name */ 1616 len = sizeof(struct pathcomp); 1617 memset(&pathcomp, 0, len); 1618 pathcomp.type = UDF_PATH_COMP_NAME; 1619 len = UDF_PATH_COMP_SIZE; 1620 1621 if ((compnamelen == 2) && (strncmp(compnamepos, "..", 2) == 0)) 1622 pathcomp.type = UDF_PATH_COMP_PARENTDIR; 1623 if ((compnamelen == 1) && (*compnamepos == '.')) 1624 pathcomp.type = UDF_PATH_COMP_CURDIR; 1625 1626 if (pathcomp.type == UDF_PATH_COMP_NAME) { 1627 unix_to_udf_name( 1628 (char *) &pathcomp.ident, &pathcomp.l_ci, 1629 compnamepos, compnamelen, 1630 &udf_node->ump->logical_vol->desc_charset); 1631 len = UDF_PATH_COMP_SIZE + pathcomp.l_ci; 1632 } 1633 1634 if (pathlen + len >= UDF_SYMLINKBUFLEN) { 1635 error = ENAMETOOLONG; 1636 break; 1637 } 1638 1639 memcpy(pathpos, &pathcomp, len); 1640 pathpos += len; 1641 pathlen += len; 1642 } 1643 1644 if (error) { 1645 /* aparently too big */ 1646 free(pathbuf, M_UDFTEMP); 1647 return error; 1648 } 1649 1650 error = udf_grow_node(udf_node, pathlen); 1651 if (error) { 1652 /* failed to pregrow node */ 1653 free(pathbuf, M_UDFTEMP); 1654 return error; 1655 } 1656 1657 /* write out structure on the new file */ 1658 error = vn_rdwr(UIO_WRITE, udf_node->vnode, 1659 pathbuf, pathlen, 0, 1660 UIO_SYSSPACE, IO_NODELOCKED | IO_ALTSEMANTICS, 1661 FSCRED, NULL, NULL); 1662 1663 /* return status of symlink contents writeout */ 1664 free(pathbuf, M_UDFTEMP); 1665 return error; 1666 } 1667 1668 1669 int 1670 udf_symlink(void *v) 1671 { 1672 struct vop_symlink_args /* { 1673 struct vnode *a_dvp; 1674 struct vnode **a_vpp; 1675 struct componentname *a_cnp; 1676 struct vattr *a_vap; 1677 char *a_target; 1678 } */ *ap = v; 1679 struct vnode *dvp = ap->a_dvp; 1680 struct vnode **vpp = ap->a_vpp; 1681 struct vattr *vap = ap->a_vap; 1682 struct componentname *cnp = ap->a_cnp; 1683 struct udf_node *dir_node; 1684 struct udf_node *udf_node; 1685 int error; 1686 1687 DPRINTF(CALL, ("udf_symlink called\n")); 1688 DPRINTF(CALL, ("\tlinking to `%s`\n", ap->a_target)); 1689 error = udf_create_node(dvp, vpp, vap, cnp); 1690 KASSERT(((error == 0) && (*vpp != NULL)) || ((error && (*vpp == NULL)))); 1691 if (!error) { 1692 dir_node = VTOI(dvp); 1693 udf_node = VTOI(*vpp); 1694 KASSERT(udf_node); 1695 error = udf_do_symlink(udf_node, ap->a_target); 1696 if (error) { 1697 /* remove node */ 1698 udf_shrink_node(udf_node, 0); 1699 udf_dir_detach(udf_node->ump, dir_node, udf_node, cnp); 1700 } 1701 } 1702 if (error || !(cnp->cn_flags & SAVESTART)) 1703 PNBUF_PUT(cnp->cn_pnbuf); 1704 vput(dvp); 1705 return error; 1706 } 1707 1708 /* --------------------------------------------------------------------- */ 1709 1710 int 1711 udf_readlink(void *v) 1712 { 1713 struct vop_readlink_args /* { 1714 struct vnode *a_vp; 1715 struct uio *a_uio; 1716 kauth_cred_t a_cred; 1717 } */ *ap = v; 1718 struct vnode *vp = ap->a_vp; 1719 struct uio *uio = ap->a_uio; 1720 kauth_cred_t cred = ap->a_cred; 1721 struct udf_node *udf_node; 1722 struct pathcomp pathcomp; 1723 struct vattr vattr; 1724 uint8_t *pathbuf, *targetbuf, *tmpname; 1725 uint8_t *pathpos, *targetpos; 1726 char *mntonname; 1727 int pathlen, targetlen, namelen, mntonnamelen, len, l_ci; 1728 int first, error; 1729 1730 DPRINTF(CALL, ("udf_readlink called\n")); 1731 1732 udf_node = VTOI(vp); 1733 error = VOP_GETATTR(vp, &vattr, cred); 1734 if (error) 1735 return error; 1736 1737 /* claim temporary buffers for translation */ 1738 pathbuf = malloc(UDF_SYMLINKBUFLEN, M_UDFTEMP, M_WAITOK); 1739 targetbuf = malloc(PATH_MAX+1, M_UDFTEMP, M_WAITOK); 1740 tmpname = malloc(PATH_MAX+1, M_UDFTEMP, M_WAITOK); 1741 memset(pathbuf, 0, UDF_SYMLINKBUFLEN); 1742 memset(targetbuf, 0, PATH_MAX); 1743 1744 /* read contents of file in our temporary buffer */ 1745 error = vn_rdwr(UIO_READ, udf_node->vnode, 1746 pathbuf, vattr.va_size, 0, 1747 UIO_SYSSPACE, IO_NODELOCKED | IO_ALTSEMANTICS, 1748 FSCRED, NULL, NULL); 1749 if (error) { 1750 /* failed to read in symlink contents */ 1751 free(pathbuf, M_UDFTEMP); 1752 free(targetbuf, M_UDFTEMP); 1753 free(tmpname, M_UDFTEMP); 1754 return error; 1755 } 1756 1757 /* convert to a unix path */ 1758 pathpos = pathbuf; 1759 pathlen = 0; 1760 targetpos = targetbuf; 1761 targetlen = PATH_MAX; 1762 mntonname = udf_node->ump->vfs_mountp->mnt_stat.f_mntonname; 1763 mntonnamelen = strlen(mntonname); 1764 1765 error = 0; 1766 first = 1; 1767 while (vattr.va_size - pathlen >= UDF_PATH_COMP_SIZE) { 1768 len = UDF_PATH_COMP_SIZE; 1769 memcpy(&pathcomp, pathpos, len); 1770 l_ci = pathcomp.l_ci; 1771 switch (pathcomp.type) { 1772 case UDF_PATH_COMP_ROOT : 1773 /* XXX should check for l_ci; bugcompatible now */ 1774 if ((targetlen < 1) || !first) { 1775 error = EINVAL; 1776 break; 1777 } 1778 *targetpos++ = '/'; targetlen--; 1779 break; 1780 case UDF_PATH_COMP_MOUNTROOT : 1781 /* XXX what should it be if l_ci > 0 ? [4/48.16.1.2] */ 1782 if (l_ci || (targetlen < mntonnamelen+1) || !first) { 1783 error = EINVAL; 1784 break; 1785 } 1786 memcpy(targetpos, mntonname, mntonnamelen); 1787 targetpos += mntonnamelen; targetlen -= mntonnamelen; 1788 if (vattr.va_size-pathlen > UDF_PATH_COMP_SIZE+l_ci) { 1789 /* more follows, so must be directory */ 1790 *targetpos++ = '/'; targetlen--; 1791 } 1792 break; 1793 case UDF_PATH_COMP_PARENTDIR : 1794 /* XXX should check for l_ci; bugcompatible now */ 1795 if (targetlen < 3) { 1796 error = EINVAL; 1797 break; 1798 } 1799 *targetpos++ = '.'; targetlen--; 1800 *targetpos++ = '.'; targetlen--; 1801 *targetpos++ = '/'; targetlen--; 1802 break; 1803 case UDF_PATH_COMP_CURDIR : 1804 /* XXX should check for l_ci; bugcompatible now */ 1805 if (targetlen < 2) { 1806 error = EINVAL; 1807 break; 1808 } 1809 *targetpos++ = '.'; targetlen--; 1810 *targetpos++ = '/'; targetlen--; 1811 break; 1812 case UDF_PATH_COMP_NAME : 1813 if (l_ci == 0) { 1814 error = EINVAL; 1815 break; 1816 } 1817 memset(tmpname, 0, PATH_MAX); 1818 memcpy(&pathcomp, pathpos, len + l_ci); 1819 udf_to_unix_name(tmpname, MAXPATHLEN, 1820 pathcomp.ident, l_ci, 1821 &udf_node->ump->logical_vol->desc_charset); 1822 namelen = strlen(tmpname); 1823 if (targetlen < namelen + 1) { 1824 error = EINVAL; 1825 break; 1826 } 1827 memcpy(targetpos, tmpname, namelen); 1828 targetpos += namelen; targetlen -= namelen; 1829 if (vattr.va_size-pathlen > UDF_PATH_COMP_SIZE+l_ci) { 1830 /* more follows, so must be directory */ 1831 *targetpos++ = '/'; targetlen--; 1832 } 1833 break; 1834 default : 1835 error = EINVAL; 1836 break; 1837 } 1838 first = 0; 1839 if (error) 1840 break; 1841 pathpos += UDF_PATH_COMP_SIZE + l_ci; 1842 pathlen += UDF_PATH_COMP_SIZE + l_ci; 1843 1844 } 1845 /* all processed? */ 1846 if (vattr.va_size - pathlen > 0) 1847 error = EINVAL; 1848 1849 /* uiomove() to destination */ 1850 if (!error) 1851 uiomove(targetbuf, PATH_MAX - targetlen, uio); 1852 1853 free(pathbuf, M_UDFTEMP); 1854 free(targetbuf, M_UDFTEMP); 1855 free(tmpname, M_UDFTEMP); 1856 1857 return error; 1858 } 1859 1860 /* --------------------------------------------------------------------- */ 1861 1862 /* note: i tried to follow the logics of the tmpfs rename code */ 1863 int 1864 udf_rename(void *v) 1865 { 1866 struct vop_rename_args /* { 1867 struct vnode *a_fdvp; 1868 struct vnode *a_fvp; 1869 struct componentname *a_fcnp; 1870 struct vnode *a_tdvp; 1871 struct vnode *a_tvp; 1872 struct componentname *a_tcnp; 1873 } */ *ap = v; 1874 struct vnode *tvp = ap->a_tvp; 1875 struct vnode *tdvp = ap->a_tdvp; 1876 struct vnode *fvp = ap->a_fvp; 1877 struct vnode *fdvp = ap->a_fdvp; 1878 struct componentname *tcnp = ap->a_tcnp; 1879 struct componentname *fcnp = ap->a_fcnp; 1880 struct udf_node *fnode, *fdnode, *tnode, *tdnode; 1881 struct vattr fvap, tvap; 1882 int error; 1883 1884 DPRINTF(CALL, ("udf_rename called\n")); 1885 1886 /* disallow cross-device renames */ 1887 if (fvp->v_mount != tdvp->v_mount || 1888 (tvp != NULL && fvp->v_mount != tvp->v_mount)) { 1889 error = EXDEV; 1890 goto out_unlocked; 1891 } 1892 1893 fnode = VTOI(fvp); 1894 fdnode = VTOI(fdvp); 1895 tnode = (tvp == NULL) ? NULL : VTOI(tvp); 1896 tdnode = VTOI(tdvp); 1897 1898 /* lock our source dir */ 1899 if (fdnode != tdnode) { 1900 error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY); 1901 if (error != 0) 1902 goto out_unlocked; 1903 } 1904 1905 /* get info about the node to be moved */ 1906 error = VOP_GETATTR(fvp, &fvap, FSCRED); 1907 KASSERT(error == 0); 1908 1909 /* check when to delete the old already existing entry */ 1910 if (tvp) { 1911 /* get info about the node to be moved to */ 1912 error = VOP_GETATTR(fvp, &tvap, FSCRED); 1913 KASSERT(error == 0); 1914 1915 /* if both dirs, make sure the destination is empty */ 1916 if (fvp->v_type == VDIR && tvp->v_type == VDIR) { 1917 if (tvap.va_nlink > 2) { 1918 error = ENOTEMPTY; 1919 goto out; 1920 } 1921 } 1922 /* if moving dir, make sure destination is dir too */ 1923 if (fvp->v_type == VDIR && tvp->v_type != VDIR) { 1924 error = ENOTDIR; 1925 goto out; 1926 } 1927 /* if we're moving a non-directory, make sure dest is no dir */ 1928 if (fvp->v_type != VDIR && tvp->v_type == VDIR) { 1929 error = EISDIR; 1930 goto out; 1931 } 1932 } 1933 1934 /* dont allow renaming directories acros directory for now */ 1935 if (fdnode != tdnode) { 1936 if (fvp->v_type == VDIR) { 1937 error = EINVAL; 1938 goto out; 1939 } 1940 } 1941 1942 /* remove existing entry if present */ 1943 if (tvp) 1944 udf_dir_detach(tdnode->ump, tdnode, tnode, tcnp); 1945 1946 /* create new directory entry for the node */ 1947 error = udf_dir_attach(tdnode->ump, tdnode, fnode, &fvap, tcnp); 1948 if (error) 1949 goto out; 1950 1951 /* unlink old directory entry for the node, if failing, unattach new */ 1952 error = udf_dir_detach(tdnode->ump, fdnode, fnode, fcnp); 1953 if (error) 1954 udf_dir_detach(tdnode->ump, tdnode, fnode, tcnp); 1955 1956 out: 1957 if (fdnode != tdnode) 1958 VOP_UNLOCK(fdvp, 0); 1959 1960 out_unlocked: 1961 VOP_ABORTOP(tdvp, tcnp); 1962 if (tdvp == tvp) 1963 vrele(tdvp); 1964 else 1965 vput(tdvp); 1966 if (tvp) 1967 vput(tvp); 1968 VOP_ABORTOP(fdvp, fcnp); 1969 1970 /* release source nodes. */ 1971 vrele(fdvp); 1972 vrele(fvp); 1973 1974 return error; 1975 } 1976 1977 /* --------------------------------------------------------------------- */ 1978 1979 int 1980 udf_remove(void *v) 1981 { 1982 struct vop_remove_args /* { 1983 struct vnode *a_dvp; 1984 struct vnode *a_vp; 1985 struct componentname *a_cnp; 1986 } */ *ap = v; 1987 struct vnode *dvp = ap->a_dvp; 1988 struct vnode *vp = ap->a_vp; 1989 struct componentname *cnp = ap->a_cnp; 1990 struct udf_node *dir_node = VTOI(dvp);; 1991 struct udf_node *udf_node = VTOI(vp); 1992 struct udf_mount *ump = dir_node->ump; 1993 int error; 1994 1995 DPRINTF(CALL, ("udf_remove called\n")); 1996 if (vp->v_type != VDIR) { 1997 error = udf_dir_detach(ump, dir_node, udf_node, cnp); 1998 DPRINTFIF(NODE, error, ("\tgot error removing file\n")); 1999 } else { 2000 DPRINTF(NODE, ("\tis a directory: perm. denied\n")); 2001 error = EPERM; 2002 } 2003 2004 if (error == 0) { 2005 VN_KNOTE(vp, NOTE_DELETE); 2006 VN_KNOTE(dvp, NOTE_WRITE); 2007 } 2008 2009 if (dvp == vp) 2010 vrele(vp); 2011 else 2012 vput(vp); 2013 vput(dvp); 2014 2015 return error; 2016 } 2017 2018 /* --------------------------------------------------------------------- */ 2019 2020 int 2021 udf_rmdir(void *v) 2022 { 2023 struct vop_rmdir_args /* { 2024 struct vnode *a_dvp; 2025 struct vnode *a_vp; 2026 struct componentname *a_cnp; 2027 } */ *ap = v; 2028 struct vnode *vp = ap->a_vp; 2029 struct vnode *dvp = ap->a_dvp; 2030 struct componentname *cnp = ap->a_cnp; 2031 struct udf_node *dir_node = VTOI(dvp);; 2032 struct udf_node *udf_node = VTOI(vp); 2033 struct udf_mount *ump = dir_node->ump; 2034 int refcnt, error; 2035 2036 DPRINTF(NOTIMPL, ("udf_rmdir called\n")); 2037 2038 /* don't allow '.' to be deleted */ 2039 if (dir_node == udf_node) { 2040 vrele(dvp); 2041 vput(vp); 2042 return EINVAL; 2043 } 2044 2045 /* check to see if the directory is empty */ 2046 error = 0; 2047 if (dir_node->fe) { 2048 refcnt = udf_rw16(udf_node->fe->link_cnt); 2049 } else { 2050 refcnt = udf_rw16(udf_node->efe->link_cnt); 2051 } 2052 if (refcnt > 1) { 2053 /* NOT empty */ 2054 vput(dvp); 2055 vput(vp); 2056 return ENOTEMPTY; 2057 } 2058 2059 /* detach the node from the directory */ 2060 error = udf_dir_detach(ump, dir_node, udf_node, cnp); 2061 if (error == 0) { 2062 cache_purge(vp); 2063 // cache_purge(dvp); /* XXX from msdosfs, why? */ 2064 VN_KNOTE(vp, NOTE_DELETE); 2065 } 2066 DPRINTFIF(NODE, error, ("\tgot error removing file\n")); 2067 2068 /* unput the nodes and exit */ 2069 vput(dvp); 2070 vput(vp); 2071 2072 return error; 2073 } 2074 2075 /* --------------------------------------------------------------------- */ 2076 2077 int 2078 udf_fsync(void *v) 2079 { 2080 struct vop_fsync_args /* { 2081 struct vnode *a_vp; 2082 kauth_cred_t a_cred; 2083 int a_flags; 2084 off_t offlo; 2085 off_t offhi; 2086 struct proc *a_p; 2087 } */ *ap = v; 2088 struct vnode *vp = ap->a_vp; 2089 struct udf_node *udf_node = VTOI(vp); 2090 int error, flags, wait; 2091 2092 DPRINTF(SYNC, ("udf_fsync called on %p : %s, %s\n", 2093 udf_node, 2094 (ap->a_flags & FSYNC_WAIT) ? "wait":"no wait", 2095 (ap->a_flags & FSYNC_DATAONLY) ? "data_only":"complete")); 2096 2097 /* flush data and wait for it when requested */ 2098 wait = (ap->a_flags & FSYNC_WAIT) ? UPDATE_WAIT : 0; 2099 vflushbuf(vp, wait); 2100 2101 if (udf_node == NULL) { 2102 printf("udf_fsync() called on NULL udf_node!\n"); 2103 return 0; 2104 } 2105 if (vp->v_tag != VT_UDF) { 2106 printf("udf_fsync() called on node not tagged as UDF node!\n"); 2107 return 0; 2108 } 2109 2110 /* set our times */ 2111 udf_itimes(udf_node, NULL, NULL, NULL); 2112 2113 /* if called when mounted readonly, never write back */ 2114 if (vp->v_mount->mnt_flag & MNT_RDONLY) 2115 return 0; 2116 2117 /* if only data is requested, return */ 2118 if (ap->a_flags & FSYNC_DATAONLY) 2119 return 0; 2120 2121 /* check if the node is dirty 'enough'*/ 2122 flags = udf_node->i_flags & (IN_MODIFIED | IN_ACCESSED); 2123 if (flags == 0) 2124 return 0; 2125 2126 /* if we don't have to wait, check for IO pending */ 2127 if (!wait) { 2128 if (vp->v_numoutput > 0) { 2129 DPRINTF(SYNC, ("udf_fsync %p, rejecting on v_numoutput\n", udf_node)); 2130 return 0; 2131 } 2132 if (udf_node->outstanding_bufs > 0) { 2133 DPRINTF(SYNC, ("udf_fsync %p, rejecting on outstanding_bufs\n", udf_node)); 2134 return 0; 2135 } 2136 if (udf_node->outstanding_nodedscr > 0) { 2137 DPRINTF(SYNC, ("udf_fsync %p, rejecting on outstanding_nodedscr\n", udf_node)); 2138 return 0; 2139 } 2140 } 2141 2142 /* wait until vp->v_numoutput reaches zero i.e. is finished */ 2143 if (wait) { 2144 DPRINTF(SYNC, ("udf_fsync %p, waiting\n", udf_node)); 2145 mutex_enter(&vp->v_interlock); 2146 while (vp->v_numoutput) { 2147 DPRINTF(SYNC, ("udf_fsync %p, v_numoutput %d\n", udf_node, vp->v_numoutput)); 2148 cv_timedwait(&vp->v_cv, &vp->v_interlock, hz/8); 2149 } 2150 mutex_exit(&vp->v_interlock); 2151 DPRINTF(SYNC, ("udf_fsync %p, fin wait\n", udf_node)); 2152 } 2153 2154 /* write out node and wait for it if requested */ 2155 DPRINTF(SYNC, ("udf_fsync %p, writeout node\n", udf_node)); 2156 error = udf_writeout_node(udf_node, wait); 2157 if (error) 2158 return error; 2159 2160 /* TODO/XXX if ap->a_flags & FSYNC_CACHE, we ought to do a disc sync */ 2161 2162 return 0; 2163 } 2164 2165 /* --------------------------------------------------------------------- */ 2166 2167 int 2168 udf_advlock(void *v) 2169 { 2170 struct vop_advlock_args /* { 2171 struct vnode *a_vp; 2172 void *a_id; 2173 int a_op; 2174 struct flock *a_fl; 2175 int a_flags; 2176 } */ *ap = v; 2177 struct vnode *vp = ap->a_vp; 2178 struct udf_node *udf_node = VTOI(vp); 2179 struct file_entry *fe; 2180 struct extfile_entry *efe; 2181 uint64_t file_size; 2182 2183 DPRINTF(LOCKING, ("udf_advlock called\n")); 2184 2185 /* get directory filesize */ 2186 if (udf_node->fe) { 2187 fe = udf_node->fe; 2188 file_size = udf_rw64(fe->inf_len); 2189 } else { 2190 assert(udf_node->efe); 2191 efe = udf_node->efe; 2192 file_size = udf_rw64(efe->inf_len); 2193 } 2194 2195 return lf_advlock(ap, &udf_node->lockf, file_size); 2196 } 2197 2198 /* --------------------------------------------------------------------- */ 2199 2200 2201 /* Global vfs vnode data structures for udfs */ 2202 int (**udf_vnodeop_p)(void *); 2203 2204 const struct vnodeopv_entry_desc udf_vnodeop_entries[] = { 2205 { &vop_default_desc, vn_default_error }, 2206 { &vop_lookup_desc, udf_lookup }, /* lookup */ 2207 { &vop_create_desc, udf_create }, /* create */ 2208 { &vop_mknod_desc, udf_mknod }, /* mknod */ /* TODO */ 2209 { &vop_open_desc, udf_open }, /* open */ 2210 { &vop_close_desc, udf_close }, /* close */ 2211 { &vop_access_desc, udf_access }, /* access */ 2212 { &vop_getattr_desc, udf_getattr }, /* getattr */ 2213 { &vop_setattr_desc, udf_setattr }, /* setattr */ /* TODO chflags */ 2214 { &vop_read_desc, udf_read }, /* read */ 2215 { &vop_write_desc, udf_write }, /* write */ /* WRITE */ 2216 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */ /* TODO? */ 2217 { &vop_ioctl_desc, genfs_enoioctl }, /* ioctl */ /* TODO? */ 2218 { &vop_poll_desc, genfs_poll }, /* poll */ /* TODO/OK? */ 2219 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */ /* ? */ 2220 { &vop_revoke_desc, genfs_revoke }, /* revoke */ /* TODO? */ 2221 { &vop_mmap_desc, genfs_mmap }, /* mmap */ /* OK? */ 2222 { &vop_fsync_desc, udf_fsync }, /* fsync */ 2223 { &vop_seek_desc, genfs_seek }, /* seek */ 2224 { &vop_remove_desc, udf_remove }, /* remove */ 2225 { &vop_link_desc, udf_link }, /* link */ /* TODO */ 2226 { &vop_rename_desc, udf_rename }, /* rename */ /* TODO */ 2227 { &vop_mkdir_desc, udf_mkdir }, /* mkdir */ 2228 { &vop_rmdir_desc, udf_rmdir }, /* rmdir */ 2229 { &vop_symlink_desc, udf_symlink }, /* symlink */ /* TODO */ 2230 { &vop_readdir_desc, udf_readdir }, /* readdir */ 2231 { &vop_readlink_desc, udf_readlink }, /* readlink */ /* TEST ME */ 2232 { &vop_abortop_desc, genfs_abortop }, /* abortop */ /* TODO/OK? */ 2233 { &vop_inactive_desc, udf_inactive }, /* inactive */ 2234 { &vop_reclaim_desc, udf_reclaim }, /* reclaim */ 2235 { &vop_lock_desc, genfs_lock }, /* lock */ 2236 { &vop_unlock_desc, genfs_unlock }, /* unlock */ 2237 { &vop_bmap_desc, udf_trivial_bmap }, /* bmap */ /* 1:1 bmap */ 2238 { &vop_strategy_desc, udf_vfsstrategy },/* strategy */ 2239 /* { &vop_print_desc, udf_print }, */ /* print */ 2240 { &vop_islocked_desc, genfs_islocked }, /* islocked */ 2241 { &vop_pathconf_desc, udf_pathconf }, /* pathconf */ 2242 { &vop_advlock_desc, udf_advlock }, /* advlock */ /* TEST ME */ 2243 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */ /* ->strategy */ 2244 { &vop_getpages_desc, genfs_getpages }, /* getpages */ 2245 { &vop_putpages_desc, genfs_putpages }, /* putpages */ 2246 { NULL, NULL } 2247 }; 2248 2249 2250 const struct vnodeopv_desc udf_vnodeop_opv_desc = { 2251 &udf_vnodeop_p, udf_vnodeop_entries 2252 }; 2253 2254