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