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