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