1 /*- 2 * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 /* udf_vnops.c */ 30 /* Take care of the vnode side of things */ 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/namei.h> 35 #include <sys/kernel.h> 36 #include <sys/malloc.h> 37 #include <sys/stat.h> 38 #include <sys/bio.h> 39 #include <sys/buf.h> 40 #include <sys/mount.h> 41 #include <sys/vnode.h> 42 #include <sys/dirent.h> 43 #include <sys/queue.h> 44 #include <sys/unistd.h> 45 46 #include <vm/uma.h> 47 48 #include <fs/udf/ecma167-udf.h> 49 #include <fs/udf/osta.h> 50 #include <fs/udf/udf.h> 51 52 static int udf_access(struct vop_access_args *); 53 static int udf_getattr(struct vop_getattr_args *); 54 static int udf_ioctl(struct vop_ioctl_args *); 55 static int udf_pathconf(struct vop_pathconf_args *); 56 static int udf_read(struct vop_read_args *); 57 static int udf_readdir(struct vop_readdir_args *); 58 static int udf_readlink(struct vop_readlink_args *ap); 59 static int udf_strategy(struct vop_strategy_args *); 60 static int udf_bmap(struct vop_bmap_args *); 61 static int udf_lookup(struct vop_cachedlookup_args *); 62 static int udf_reclaim(struct vop_reclaim_args *); 63 static void udf_dumpblock(void *, int) __unused; 64 static int udf_readatoffset(struct udf_node *, int *, int, struct buf **, uint8_t **); 65 static int udf_bmap_internal(struct udf_node *, uint32_t, daddr_t *, uint32_t *); 66 67 vop_t **udf_vnodeop_p; 68 static struct vnodeopv_entry_desc udf_vnodeop_entries[] = { 69 { &vop_default_desc, (vop_t *) vop_defaultop }, 70 { &vop_access_desc, (vop_t *) udf_access }, 71 { &vop_bmap_desc, (vop_t *) udf_bmap }, 72 { &vop_cachedlookup_desc, (vop_t *) udf_lookup }, 73 { &vop_getattr_desc, (vop_t *) udf_getattr }, 74 { &vop_ioctl_desc, (vop_t *) udf_ioctl }, 75 { &vop_lookup_desc, (vop_t *) vfs_cache_lookup }, 76 { &vop_pathconf_desc, (vop_t *) udf_pathconf }, 77 { &vop_read_desc, (vop_t *) udf_read }, 78 { &vop_readdir_desc, (vop_t *) udf_readdir }, 79 { &vop_readlink_desc, (vop_t *) udf_readlink }, 80 { &vop_reclaim_desc, (vop_t *) udf_reclaim }, 81 { &vop_strategy_desc, (vop_t *) udf_strategy }, 82 { NULL, NULL } 83 }; 84 static struct vnodeopv_desc udf_vnodeop_opv_desc = 85 { &udf_vnodeop_p, udf_vnodeop_entries }; 86 VNODEOP_SET(udf_vnodeop_opv_desc); 87 88 MALLOC_DEFINE(M_UDFFID, "UDF FID", "UDF FileId structure"); 89 MALLOC_DEFINE(M_UDFDS, "UDF DS", "UDF Dirstream structure"); 90 91 #define UDF_INVALID_BMAP -1 92 93 /* Look up a udf_node based on the ino_t passed in and return it's vnode */ 94 int 95 udf_hashlookup(struct udf_mnt *udfmp, ino_t id, int flags, struct vnode **vpp) 96 { 97 struct udf_node *node; 98 int error; 99 100 *vpp = NULL; 101 102 loop: 103 mtx_lock(&udfmp->hash_mtx); 104 TAILQ_FOREACH(node, &udfmp->udf_tqh, tq) { 105 if (node->hash_id == id) { 106 VI_LOCK(node->i_vnode); 107 mtx_unlock(&udfmp->hash_mtx); 108 error = vget(node->i_vnode, flags | LK_INTERLOCK, 109 curthread); 110 if (error == ENOENT) 111 goto loop; 112 if (error) 113 return (error); 114 *vpp = node->i_vnode; 115 return (0); 116 } 117 } 118 119 mtx_unlock(&udfmp->hash_mtx); 120 return (0); 121 } 122 123 int 124 udf_hashins(struct udf_node *node) 125 { 126 struct udf_mnt *udfmp; 127 128 udfmp = node->udfmp; 129 130 vn_lock(node->i_vnode, LK_EXCLUSIVE | LK_RETRY, curthread); 131 mtx_lock(&udfmp->hash_mtx); 132 TAILQ_INSERT_TAIL(&udfmp->udf_tqh, node, tq); 133 mtx_unlock(&udfmp->hash_mtx); 134 135 return (0); 136 } 137 138 int 139 udf_hashrem(struct udf_node *node) 140 { 141 struct udf_mnt *udfmp; 142 143 udfmp = node->udfmp; 144 145 mtx_lock(&udfmp->hash_mtx); 146 TAILQ_REMOVE(&udfmp->udf_tqh, node, tq); 147 mtx_unlock(&udfmp->hash_mtx); 148 149 return (0); 150 } 151 152 int 153 udf_allocv(struct mount *mp, struct vnode **vpp, struct thread *td) 154 { 155 int error; 156 struct vnode *vp; 157 158 error = getnewvnode("udf", mp, udf_vnodeop_p, &vp); 159 if (error) { 160 printf("udf_allocv: failed to allocate new vnode\n"); 161 return (error); 162 } 163 164 *vpp = vp; 165 return (0); 166 } 167 168 /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */ 169 static mode_t 170 udf_permtomode(struct udf_node *node) 171 { 172 uint32_t perm; 173 uint32_t flags; 174 mode_t mode; 175 176 perm = node->fentry->perm; 177 flags = node->fentry->icbtag.flags; 178 179 mode = perm & UDF_FENTRY_PERM_USER_MASK; 180 mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2); 181 mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4); 182 mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4); 183 mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6); 184 mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8); 185 186 return (mode); 187 } 188 189 static int 190 udf_access(struct vop_access_args *a) 191 { 192 struct vnode *vp; 193 struct udf_node *node; 194 mode_t a_mode, mode; 195 196 vp = a->a_vp; 197 node = VTON(vp); 198 a_mode = a->a_mode; 199 200 if (a_mode & VWRITE) { 201 switch (vp->v_type) { 202 case VDIR: 203 case VLNK: 204 case VREG: 205 return (EROFS); 206 /* NOT REACHED */ 207 default: 208 break; 209 } 210 } 211 212 mode = udf_permtomode(node); 213 214 return (vaccess(vp->v_type, mode, node->fentry->uid, node->fentry->gid, 215 a_mode, a->a_cred, NULL)); 216 } 217 218 static int mon_lens[2][12] = { 219 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 220 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} 221 }; 222 223 static int 224 udf_isaleapyear(int year) 225 { 226 int i; 227 228 i = (year % 4) ? 0 : 1; 229 i &= (year % 100) ? 1 : 0; 230 i |= (year % 400) ? 0 : 1; 231 232 return i; 233 } 234 235 /* 236 * XXX This is just a rough hack. Daylight savings isn't calculated and tv_nsec 237 * is ignored. 238 * Timezone calculation compliments of Julian Elischer <julian@elischer.org>. 239 */ 240 static void 241 udf_timetotimespec(struct timestamp *time, struct timespec *t) 242 { 243 int i, lpyear, daysinyear; 244 union { 245 uint16_t u_tz_offset; 246 int16_t s_tz_offset; 247 } tz; 248 249 t->tv_nsec = 0; 250 251 /* DirectCD seems to like using bogus year values */ 252 if (time->year < 1970) { 253 t->tv_sec = 0; 254 return; 255 } 256 257 /* Calculate the time and day */ 258 t->tv_sec = time->second; 259 t->tv_sec += time->minute * 60; 260 t->tv_sec += time->hour * 3600; 261 t->tv_sec += time->day * 3600 * 24; 262 263 /* Calclulate the month */ 264 lpyear = udf_isaleapyear(time->year); 265 for (i = 1; i < time->month; i++) 266 t->tv_sec += mon_lens[lpyear][i] * 3600 * 24; 267 268 /* Speed up the calculation */ 269 if (time->year > 1979) 270 t->tv_sec += 315532800; 271 if (time->year > 1989) 272 t->tv_sec += 315619200; 273 if (time->year > 1999) 274 t->tv_sec += 315532800; 275 for (i = 2000; i < time->year; i++) { 276 daysinyear = udf_isaleapyear(i) + 365 ; 277 t->tv_sec += daysinyear * 3600 * 24; 278 } 279 280 /* 281 * Calculate the time zone. The timezone is 12 bit signed 2's 282 * compliment, so we gotta do some extra magic to handle it right. 283 */ 284 tz.u_tz_offset = time->type_tz; 285 tz.u_tz_offset &= 0x0fff; 286 if (tz.u_tz_offset & 0x0800) 287 tz.u_tz_offset |= 0xf000; /* extend the sign to 16 bits */ 288 if ((time->type_tz & 0x1000) && (tz.s_tz_offset != -2047)) 289 t->tv_sec -= tz.s_tz_offset * 60; 290 291 return; 292 } 293 294 static int 295 udf_getattr(struct vop_getattr_args *a) 296 { 297 struct vnode *vp; 298 struct udf_node *node; 299 struct vattr *vap; 300 struct file_entry *fentry; 301 struct timespec ts; 302 303 ts.tv_sec = 0; 304 305 vp = a->a_vp; 306 vap = a->a_vap; 307 node = VTON(vp); 308 fentry = node->fentry; 309 310 vap->va_fsid = dev2udev(node->i_dev); 311 vap->va_fileid = node->hash_id; 312 vap->va_mode = udf_permtomode(node); 313 vap->va_nlink = fentry->link_cnt; 314 /* 315 * XXX The spec says that -1 is valid for uid/gid and indicates an 316 * invalid uid/gid. How should this be represented? 317 */ 318 vap->va_uid = (fentry->uid == -1) ? 0 : fentry->uid; 319 vap->va_gid = (fentry->gid == -1) ? 0 : fentry->gid; 320 udf_timetotimespec(&fentry->atime, &vap->va_atime); 321 udf_timetotimespec(&fentry->mtime, &vap->va_mtime); 322 vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */ 323 vap->va_rdev = 0; /* XXX */ 324 if (vp->v_type & VDIR) { 325 /* 326 * Directories that are recorded within their ICB will show 327 * as having 0 blocks recorded. Since tradition dictates 328 * that directories consume at least one logical block, 329 * make it appear so. 330 */ 331 if (fentry->logblks_rec != 0) { 332 vap->va_size = fentry->logblks_rec * node->udfmp->bsize; 333 } else { 334 vap->va_size = node->udfmp->bsize; 335 } 336 } else { 337 vap->va_size = fentry->inf_len; 338 } 339 vap->va_flags = 0; 340 vap->va_gen = 1; 341 vap->va_blocksize = node->udfmp->bsize; 342 vap->va_bytes = fentry->inf_len; 343 vap->va_type = vp->v_type; 344 vap->va_filerev = 0; /* XXX */ 345 return (0); 346 } 347 348 /* 349 * File specific ioctls. DeCSS candidate? 350 */ 351 static int 352 udf_ioctl(struct vop_ioctl_args *a) 353 { 354 printf("%s called\n", __FUNCTION__); 355 return (ENOTTY); 356 } 357 358 /* 359 * I'm not sure that this has much value in a read-only filesystem, but 360 * cd9660 has it too. 361 */ 362 static int 363 udf_pathconf(struct vop_pathconf_args *a) 364 { 365 366 switch (a->a_name) { 367 case _PC_LINK_MAX: 368 *a->a_retval = 65535; 369 return (0); 370 case _PC_NAME_MAX: 371 *a->a_retval = NAME_MAX; 372 return (0); 373 case _PC_PATH_MAX: 374 *a->a_retval = PATH_MAX; 375 return (0); 376 case _PC_NO_TRUNC: 377 *a->a_retval = 1; 378 return (0); 379 default: 380 return (EINVAL); 381 } 382 } 383 384 static int 385 udf_read(struct vop_read_args *a) 386 { 387 struct vnode *vp = a->a_vp; 388 struct uio *uio = a->a_uio; 389 struct udf_node *node = VTON(vp); 390 struct buf *bp; 391 uint8_t *data; 392 int error = 0; 393 int size, fsize, offset; 394 395 if (uio->uio_offset < 0) 396 return (EINVAL); 397 398 fsize = node->fentry->inf_len; 399 400 while (uio->uio_offset < fsize && uio->uio_resid > 0) { 401 offset = uio->uio_offset; 402 size = uio->uio_resid; 403 error = udf_readatoffset(node, &size, offset, &bp, &data); 404 if (error) 405 return (error); 406 error = uiomove(data, size, uio); 407 if (bp != NULL) 408 brelse(bp); 409 if (error) 410 break; 411 }; 412 413 return (error); 414 } 415 416 /* Convienience routine to dump a block in hex */ 417 static void 418 udf_dumpblock(void *data, int len) 419 { 420 int i, j; 421 422 for (i = 0; i < len; i++) { 423 printf("\noffset= %d: ", i); 424 for (j = 0; j < 8; j++) { 425 if (i + j == len) 426 break; 427 printf("0x%02x ", (uint8_t)((uint8_t*)(data))[i + j]); 428 } 429 i += j - 1; 430 } 431 printf("\n"); 432 } 433 434 /* 435 * Call the OSTA routines to translate the name from a CS0 dstring to a 436 * 16-bit Unicode String. Hooks need to be placed in here to translate from 437 * Unicode to the encoding that the kernel/user expects. For now, compact 438 * the encoding to 8 bits if possible. Return the length of the translated 439 * string. 440 * XXX This horribly pessimizes the 8bit case 441 */ 442 static int 443 udf_transname(char *cs0string, char *destname, int len) 444 { 445 unicode_t *transname; 446 int i, unilen = 0; 447 448 /* allocate a buffer big enough to hold an 8->16 bit expansion */ 449 transname = uma_zalloc(udf_zone_trans, M_WAITOK); 450 451 if ((unilen = udf_UncompressUnicode(len, cs0string, transname)) == -1) { 452 printf("udf: Unicode translation failed\n"); 453 uma_zfree(udf_zone_trans, transname); 454 return 0; 455 } 456 457 /* At this point, the name is in 16-bit Unicode. Compact it down 458 * to 8-bit 459 */ 460 for (i = 0; i < unilen ; i++) { 461 if (transname[i] & 0xff00) { 462 destname[i] = '.'; /* Fudge the 16bit chars */ 463 } else { 464 destname[i] = transname[i] & 0xff; 465 } 466 } 467 468 destname[unilen] = 0; 469 uma_zfree(udf_zone_trans, transname); 470 471 return unilen; 472 } 473 474 /* 475 * Compare a CS0 dstring with a name passed in from the VFS layer. Return 476 * 0 on a successful match, nonzero therwise. Unicode work may need to be done 477 * here also. 478 */ 479 static int 480 udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen) 481 { 482 char *transname; 483 int error = 0; 484 485 /* This is overkill, but not worth creating a new zone */ 486 transname = uma_zalloc(udf_zone_trans, M_WAITOK); 487 488 cs0len = udf_transname(cs0string, transname, cs0len); 489 490 /* Easy check. If they aren't the same length, they aren't equal */ 491 if ((cs0len == 0) || (cs0len != cmplen)) 492 error = -1; 493 else 494 error = bcmp(transname, cmpname, cmplen); 495 496 uma_zfree(udf_zone_trans, transname); 497 return (error); 498 } 499 500 struct udf_uiodir { 501 struct dirent *dirent; 502 u_long *cookies; 503 int ncookies; 504 int acookies; 505 int eofflag; 506 }; 507 508 static int 509 udf_uiodir(struct udf_uiodir *uiodir, int de_size, struct uio *uio, long cookie) 510 { 511 if (uiodir->cookies != NULL) { 512 if (++uiodir->acookies > uiodir->ncookies) { 513 uiodir->eofflag = 0; 514 return (-1); 515 } 516 *uiodir->cookies++ = cookie; 517 } 518 519 if (uio->uio_resid < de_size) { 520 uiodir->eofflag = 0; 521 return (-1); 522 } 523 524 return (uiomove(uiodir->dirent, de_size, uio)); 525 } 526 527 static struct udf_dirstream * 528 udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp) 529 { 530 struct udf_dirstream *ds; 531 532 ds = uma_zalloc(udf_zone_ds, M_WAITOK | M_ZERO); 533 534 ds->node = node; 535 ds->offset = offset; 536 ds->udfmp = udfmp; 537 ds->fsize = fsize; 538 539 return (ds); 540 } 541 542 static struct fileid_desc * 543 udf_getfid(struct udf_dirstream *ds) 544 { 545 struct fileid_desc *fid; 546 int error, frag_size = 0, total_fid_size; 547 548 /* End of directory? */ 549 if (ds->offset + ds->off >= ds->fsize) { 550 ds->error = 0; 551 return (NULL); 552 } 553 554 /* Grab the first extent of the directory */ 555 if (ds->off == 0) { 556 ds->size = 0; 557 error = udf_readatoffset(ds->node, &ds->size, ds->offset, 558 &ds->bp, &ds->data); 559 if (error) { 560 ds->error = error; 561 return (NULL); 562 } 563 } 564 565 /* 566 * Clean up from a previous fragmented FID. 567 * XXX Is this the right place for this? 568 */ 569 if (ds->fid_fragment && ds->buf != NULL) { 570 ds->fid_fragment = 0; 571 FREE(ds->buf, M_UDFFID); 572 } 573 574 fid = (struct fileid_desc*)&ds->data[ds->off]; 575 576 /* 577 * Check to see if the fid is fragmented. The first test 578 * ensures that we don't wander off the end of the buffer 579 * looking for the l_iu and l_fi fields. 580 */ 581 if (ds->off + UDF_FID_SIZE > ds->size || 582 ds->off + fid->l_iu + fid->l_fi + UDF_FID_SIZE > ds->size) { 583 584 /* Copy what we have of the fid into a buffer */ 585 frag_size = ds->size - ds->off; 586 if (frag_size >= ds->udfmp->bsize) { 587 printf("udf: invalid FID fragment\n"); 588 ds->error = EINVAL; 589 return (NULL); 590 } 591 592 /* 593 * File ID descriptors can only be at most one 594 * logical sector in size. 595 */ 596 MALLOC(ds->buf, uint8_t*, ds->udfmp->bsize, M_UDFFID, 597 M_WAITOK | M_ZERO); 598 bcopy(fid, ds->buf, frag_size); 599 600 /* Reduce all of the casting magic */ 601 fid = (struct fileid_desc*)ds->buf; 602 603 if (ds->bp != NULL) 604 brelse(ds->bp); 605 606 /* Fetch the next allocation */ 607 ds->offset += ds->size; 608 ds->size = 0; 609 error = udf_readatoffset(ds->node, &ds->size, ds->offset, 610 &ds->bp, &ds->data); 611 if (error) { 612 ds->error = error; 613 return (NULL); 614 } 615 616 /* 617 * If the fragment was so small that we didn't get 618 * the l_iu and l_fi fields, copy those in. 619 */ 620 if (frag_size < UDF_FID_SIZE) 621 bcopy(ds->data, &ds->buf[frag_size], 622 UDF_FID_SIZE - frag_size); 623 624 /* 625 * Now that we have enough of the fid to work with, 626 * copy in the rest of the fid from the new 627 * allocation. 628 */ 629 total_fid_size = UDF_FID_SIZE + fid->l_iu + fid->l_fi; 630 if (total_fid_size > ds->udfmp->bsize) { 631 printf("udf: invalid FID\n"); 632 ds->error = EIO; 633 return (NULL); 634 } 635 bcopy(ds->data, &ds->buf[frag_size], 636 total_fid_size - frag_size); 637 638 ds->fid_fragment = 1; 639 } else { 640 total_fid_size = fid->l_iu + fid->l_fi + UDF_FID_SIZE; 641 } 642 643 /* 644 * Update the offset. Align on a 4 byte boundary because the 645 * UDF spec says so. 646 */ 647 ds->this_off = ds->off; 648 if (!ds->fid_fragment) { 649 ds->off += (total_fid_size + 3) & ~0x03; 650 } else { 651 ds->off = (total_fid_size - frag_size + 3) & ~0x03; 652 } 653 654 return (fid); 655 } 656 657 static void 658 udf_closedir(struct udf_dirstream *ds) 659 { 660 661 if (ds->bp != NULL) 662 brelse(ds->bp); 663 664 if (ds->fid_fragment && ds->buf != NULL) 665 FREE(ds->buf, M_UDFFID); 666 667 uma_zfree(udf_zone_ds, ds); 668 } 669 670 static int 671 udf_readdir(struct vop_readdir_args *a) 672 { 673 struct vnode *vp; 674 struct uio *uio; 675 struct dirent dir; 676 struct udf_node *node; 677 struct fileid_desc *fid; 678 struct udf_uiodir uiodir; 679 struct udf_dirstream *ds; 680 u_long *cookies = NULL; 681 int ncookies; 682 int error = 0; 683 684 vp = a->a_vp; 685 uio = a->a_uio; 686 node = VTON(vp); 687 uiodir.eofflag = 1; 688 689 if (a->a_ncookies != NULL) { 690 /* 691 * Guess how many entries are needed. If we run out, this 692 * function will be called again and thing will pick up were 693 * it left off. 694 */ 695 ncookies = uio->uio_resid / 8; 696 MALLOC(cookies, u_long *, sizeof(u_long) * ncookies, 697 M_TEMP, M_WAITOK); 698 if (cookies == NULL) 699 return (ENOMEM); 700 uiodir.ncookies = ncookies; 701 uiodir.cookies = cookies; 702 uiodir.acookies = 0; 703 } else { 704 uiodir.cookies = NULL; 705 } 706 707 /* 708 * Iterate through the file id descriptors. Give the parent dir 709 * entry special attention. 710 */ 711 ds = udf_opendir(node, uio->uio_offset, node->fentry->inf_len, 712 node->udfmp); 713 714 while ((fid = udf_getfid(ds)) != NULL) { 715 716 /* XXX Should we return an error on a bad fid? */ 717 if (udf_checktag(&fid->tag, TAGID_FID)) { 718 printf("Invalid FID tag\n"); 719 udf_dumpblock(fid, UDF_FID_SIZE); 720 error = EIO; 721 break; 722 } 723 724 /* Is this a deleted file? */ 725 if (fid->file_char & UDF_FILE_CHAR_DEL) 726 continue; 727 728 if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) { 729 /* Do up the '.' and '..' entries. Dummy values are 730 * used for the cookies since the offset here is 731 * usually zero, and NFS doesn't like that value 732 */ 733 dir.d_fileno = node->hash_id; 734 dir.d_type = DT_DIR; 735 dir.d_name[0] = '.'; 736 dir.d_namlen = 1; 737 dir.d_reclen = GENERIC_DIRSIZ(&dir); 738 uiodir.dirent = &dir; 739 error = udf_uiodir(&uiodir, dir.d_reclen, uio, 1); 740 if (error) 741 break; 742 743 dir.d_fileno = udf_getid(&fid->icb); 744 dir.d_type = DT_DIR; 745 dir.d_name[0] = '.'; 746 dir.d_name[1] = '.'; 747 dir.d_namlen = 2; 748 dir.d_reclen = GENERIC_DIRSIZ(&dir); 749 uiodir.dirent = &dir; 750 error = udf_uiodir(&uiodir, dir.d_reclen, uio, 2); 751 } else { 752 dir.d_namlen = udf_transname(&fid->data[fid->l_iu], 753 &dir.d_name[0], fid->l_fi); 754 dir.d_fileno = udf_getid(&fid->icb); 755 dir.d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ? 756 DT_DIR : DT_UNKNOWN; 757 dir.d_reclen = GENERIC_DIRSIZ(&dir); 758 uiodir.dirent = &dir; 759 error = udf_uiodir(&uiodir, dir.d_reclen, uio, 760 ds->this_off); 761 } 762 if (error) { 763 printf("uiomove returned %d\n", error); 764 break; 765 } 766 767 } 768 769 /* tell the calling layer whether we need to be called again */ 770 *a->a_eofflag = uiodir.eofflag; 771 uio->uio_offset = ds->offset + ds->off; 772 773 if (!error) 774 error = ds->error; 775 776 udf_closedir(ds); 777 778 if (a->a_ncookies != NULL) { 779 if (error) 780 FREE(cookies, M_TEMP); 781 else { 782 *a->a_ncookies = uiodir.acookies; 783 *a->a_cookies = cookies; 784 } 785 } 786 787 return (error); 788 } 789 790 /* Are there any implementations out there that do soft-links? */ 791 static int 792 udf_readlink(struct vop_readlink_args *ap) 793 { 794 printf("%s called\n", __FUNCTION__); 795 return (EOPNOTSUPP); 796 } 797 798 static int 799 udf_strategy(struct vop_strategy_args *a) 800 { 801 struct buf *bp; 802 struct vnode *vp; 803 struct udf_node *node; 804 int maxsize; 805 806 bp = a->a_bp; 807 vp = bp->b_vp; 808 node = VTON(vp); 809 810 /* cd9660 has this test reversed, but it seems more logical this way */ 811 if (bp->b_blkno != bp->b_lblkno) { 812 /* 813 * Files that are embedded in the fentry don't translate well 814 * to a block number. Reject. 815 */ 816 if (udf_bmap_internal(node, bp->b_lblkno * node->udfmp->bsize, 817 &bp->b_lblkno, &maxsize)) { 818 clrbuf(bp); 819 bp->b_blkno = -1; 820 } 821 } 822 if ((long)bp->b_blkno == -1) { 823 bufdone(bp); 824 return (0); 825 } 826 vp = node->i_devvp; 827 bp->b_dev = vp->v_rdev; 828 VOP_SPECSTRATEGY(vp, bp); 829 return (0); 830 } 831 832 static int 833 udf_bmap(struct vop_bmap_args *a) 834 { 835 struct udf_node *node; 836 uint32_t max_size; 837 daddr_t lsector; 838 int error; 839 840 node = VTON(a->a_vp); 841 842 if (a->a_vpp != NULL) 843 *a->a_vpp = node->i_devvp; 844 if (a->a_bnp == NULL) 845 return (0); 846 if (a->a_runb) 847 *a->a_runb = 0; 848 849 error = udf_bmap_internal(node, a->a_bn * node->udfmp->bsize, &lsector, 850 &max_size); 851 if (error) 852 return (error); 853 854 /* Translate logical to physical sector number */ 855 *a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT); 856 857 /* Punt on read-ahead for now */ 858 if (a->a_runp) 859 *a->a_runp = 0; 860 861 return (0); 862 } 863 864 /* 865 * The all powerful VOP_LOOKUP(). 866 */ 867 static int 868 udf_lookup(struct vop_cachedlookup_args *a) 869 { 870 struct vnode *dvp; 871 struct vnode *tdp = NULL; 872 struct vnode **vpp = a->a_vpp; 873 struct udf_node *node; 874 struct udf_mnt *udfmp; 875 struct fileid_desc *fid = NULL; 876 struct udf_dirstream *ds; 877 struct thread *td; 878 u_long nameiop; 879 u_long flags; 880 char *nameptr; 881 long namelen; 882 ino_t id = 0; 883 int offset, error = 0; 884 int numdirpasses, fsize; 885 886 dvp = a->a_dvp; 887 node = VTON(dvp); 888 udfmp = node->udfmp; 889 nameiop = a->a_cnp->cn_nameiop; 890 flags = a->a_cnp->cn_flags; 891 nameptr = a->a_cnp->cn_nameptr; 892 namelen = a->a_cnp->cn_namelen; 893 fsize = node->fentry->inf_len; 894 td = a->a_cnp->cn_thread; 895 896 /* 897 * If this is a LOOKUP and we've already partially searched through 898 * the directory, pick up where we left off and flag that the 899 * directory may need to be searched twice. For a full description, 900 * see /sys/isofs/cd9660/cd9660_lookup.c:cd9660_lookup() 901 */ 902 if (nameiop != LOOKUP || node->diroff == 0 || node->diroff > fsize) { 903 offset = 0; 904 numdirpasses = 1; 905 } else { 906 offset = node->diroff; 907 numdirpasses = 2; 908 nchstats.ncs_2passes++; 909 } 910 911 lookloop: 912 ds = udf_opendir(node, offset, fsize, udfmp); 913 914 while ((fid = udf_getfid(ds)) != NULL) { 915 916 /* XXX Should we return an error on a bad fid? */ 917 if (udf_checktag(&fid->tag, TAGID_FID)) { 918 printf("udf_lookup: Invalid tag\n"); 919 error = EIO; 920 break; 921 } 922 923 /* Is this a deleted file? */ 924 if (fid->file_char & UDF_FILE_CHAR_DEL) 925 continue; 926 927 if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) { 928 if (flags & ISDOTDOT) { 929 id = udf_getid(&fid->icb); 930 break; 931 } 932 } else { 933 if (!(udf_cmpname(&fid->data[fid->l_iu], 934 nameptr, fid->l_fi, namelen))) { 935 id = udf_getid(&fid->icb); 936 break; 937 } 938 } 939 } 940 941 if (!error) 942 error = ds->error; 943 944 /* XXX Bail out here? */ 945 if (error) { 946 udf_closedir(ds); 947 return (error); 948 } 949 950 /* Did we have a match? */ 951 if (id) { 952 error = udf_vget(udfmp->im_mountp, id, LK_EXCLUSIVE, &tdp); 953 if (!error) { 954 /* 955 * Remember where this entry was if it's the final 956 * component. 957 */ 958 if ((flags & ISLASTCN) && nameiop == LOOKUP) 959 node->diroff = ds->offset + ds->off; 960 if (numdirpasses == 2) 961 nchstats.ncs_pass2++; 962 if (!(flags & LOCKPARENT) || !(flags & ISLASTCN)) { 963 a->a_cnp->cn_flags |= PDIRUNLOCK; 964 VOP_UNLOCK(dvp, 0, td); 965 } 966 967 *vpp = tdp; 968 969 /* Put this entry in the cache */ 970 if (flags & MAKEENTRY) 971 cache_enter(dvp, *vpp, a->a_cnp); 972 } 973 } else { 974 /* Name wasn't found on this pass. Do another pass? */ 975 if (numdirpasses == 2) { 976 numdirpasses--; 977 offset = 0; 978 udf_closedir(ds); 979 goto lookloop; 980 } 981 982 /* Enter name into cache as non-existant */ 983 if (flags & MAKEENTRY) 984 cache_enter(dvp, *vpp, a->a_cnp); 985 986 if ((flags & ISLASTCN) && 987 (nameiop == CREATE || nameiop == RENAME)) { 988 error = EROFS; 989 } else { 990 error = ENOENT; 991 } 992 } 993 994 udf_closedir(ds); 995 return (error); 996 } 997 998 static int 999 udf_reclaim(struct vop_reclaim_args *a) 1000 { 1001 struct vnode *vp; 1002 struct udf_node *unode; 1003 1004 vp = a->a_vp; 1005 unode = VTON(vp); 1006 1007 cache_purge(vp); 1008 if (unode != NULL) { 1009 udf_hashrem(unode); 1010 if (unode->i_devvp) { 1011 vrele(unode->i_devvp); 1012 unode->i_devvp = 0; 1013 } 1014 1015 if (unode->fentry != NULL) 1016 FREE(unode->fentry, M_UDFFENTRY); 1017 uma_zfree(udf_zone_node, unode); 1018 vp->v_data = NULL; 1019 } 1020 1021 return (0); 1022 } 1023 1024 /* 1025 * Read the block and then set the data pointer to correspond with the 1026 * offset passed in. Only read in at most 'size' bytes, and then set 'size' 1027 * to the number of bytes pointed to. If 'size' is zero, try to read in a 1028 * whole extent. 1029 * XXX 'size' is limited to the logical block size for now due to problems 1030 * with udf_read() 1031 */ 1032 static int 1033 udf_readatoffset(struct udf_node *node, int *size, int offset, struct buf **bp, uint8_t **data) 1034 { 1035 struct udf_mnt *udfmp; 1036 struct file_entry *fentry = NULL; 1037 struct buf *bp1; 1038 uint32_t max_size; 1039 daddr_t sector; 1040 int error; 1041 1042 udfmp = node->udfmp; 1043 1044 error = udf_bmap_internal(node, offset, §or, &max_size); 1045 if (error == UDF_INVALID_BMAP) { 1046 /* 1047 * This error means that the file *data* is stored in the 1048 * allocation descriptor field of the file entry. 1049 */ 1050 fentry = node->fentry; 1051 *data = &fentry->data[fentry->l_ea]; 1052 *size = fentry->l_ad; 1053 *bp = NULL; 1054 return (0); 1055 } else if (error != 0) { 1056 return (error); 1057 } 1058 1059 /* Adjust the size so that it is within range */ 1060 if (*size == 0 || *size > max_size) 1061 *size = max_size; 1062 *size = min(*size, MAXBSIZE); 1063 1064 if ((error = udf_readlblks(udfmp, sector, *size, bp))) { 1065 printf("udf_readlblks returned %d\n", error); 1066 return (error); 1067 } 1068 1069 bp1 = *bp; 1070 *data = (uint8_t *)&bp1->b_data[offset % udfmp->bsize]; 1071 return (0); 1072 } 1073 1074 /* 1075 * Translate a file offset into a logical block and then into a physical 1076 * block. 1077 */ 1078 static int 1079 udf_bmap_internal(struct udf_node *node, uint32_t offset, daddr_t *sector, uint32_t *max_size) 1080 { 1081 struct udf_mnt *udfmp; 1082 struct file_entry *fentry; 1083 void *icb; 1084 struct icb_tag *tag; 1085 uint32_t icblen = 0; 1086 daddr_t lsector; 1087 int ad_offset, ad_num = 0; 1088 int i, p_offset; 1089 1090 udfmp = node->udfmp; 1091 fentry = node->fentry; 1092 tag = &fentry->icbtag; 1093 1094 switch (tag->strat_type) { 1095 case 4: 1096 break; 1097 1098 case 4096: 1099 printf("Cannot deal with strategy4096 yet!\n"); 1100 return (ENODEV); 1101 1102 default: 1103 printf("Unknown strategy type %d\n", tag->strat_type); 1104 return (ENODEV); 1105 } 1106 1107 switch (tag->flags & 0x7) { 1108 case 0: 1109 /* 1110 * The allocation descriptor field is filled with short_ad's. 1111 * If the offset is beyond the current extent, look for the 1112 * next extent. 1113 */ 1114 do { 1115 offset -= icblen; 1116 ad_offset = sizeof(struct short_ad) * ad_num; 1117 if (ad_offset > fentry->l_ad) { 1118 printf("File offset out of bounds\n"); 1119 return (EINVAL); 1120 } 1121 icb = GETICB(long_ad, fentry, fentry->l_ea + ad_offset); 1122 icblen = GETICBLEN(short_ad, icb); 1123 ad_num++; 1124 } while(offset >= icblen); 1125 1126 lsector = (offset >> udfmp->bshift) + 1127 ((struct short_ad *)(icb))->pos; 1128 1129 *max_size = GETICBLEN(short_ad, icb) - offset; 1130 1131 break; 1132 case 1: 1133 /* 1134 * The allocation descriptor field is filled with long_ad's 1135 * If the offset is beyond the current extent, look for the 1136 * next extent. 1137 */ 1138 do { 1139 offset -= icblen; 1140 ad_offset = sizeof(struct long_ad) * ad_num; 1141 if (ad_offset > fentry->l_ad) { 1142 printf("File offset out of bounds\n"); 1143 return (EINVAL); 1144 } 1145 icb = GETICB(long_ad, fentry, fentry->l_ea + ad_offset); 1146 icblen = GETICBLEN(long_ad, icb); 1147 ad_num++; 1148 } while(offset >= icblen); 1149 1150 lsector = (offset >> udfmp->bshift) + 1151 ((struct long_ad *)(icb))->loc.lb_num; 1152 1153 *max_size = GETICBLEN(long_ad, icb) - offset; 1154 1155 break; 1156 case 3: 1157 /* 1158 * This type means that the file *data* is stored in the 1159 * allocation descriptor field of the file entry. 1160 */ 1161 *max_size = 0; 1162 *sector = node->hash_id + udfmp->part_start; 1163 1164 return (UDF_INVALID_BMAP); 1165 case 2: 1166 /* DirectCD does not use extended_ad's */ 1167 default: 1168 printf("Unsupported allocation descriptor %d\n", 1169 tag->flags & 0x7); 1170 return (ENODEV); 1171 } 1172 1173 *sector = lsector + udfmp->part_start; 1174 1175 /* 1176 * Check the sparing table. Each entry represents the beginning of 1177 * a packet. 1178 */ 1179 if (udfmp->s_table != NULL) { 1180 for (i = 0; i< udfmp->s_table_entries; i++) { 1181 p_offset = lsector - udfmp->s_table->entries[i].org; 1182 if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) { 1183 *sector = udfmp->s_table->entries[i].map + 1184 p_offset; 1185 break; 1186 } 1187 } 1188 } 1189 1190 return (0); 1191 } 1192