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