1 /* $NetBSD: cd9660_node.c,v 1.5 2003/08/07 16:31:34 agc Exp $ */ 2 3 /*- 4 * Copyright (c) 1982, 1986, 1989, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley 8 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension 9 * Support code is derived from software contributed to Berkeley 10 * by Atsushi Murai (amurai@spec.co.jp). 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)cd9660_node.c 8.8 (Berkeley) 5/22/95 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: cd9660_node.c,v 1.5 2003/08/07 16:31:34 agc Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/mount.h> 45 #include <sys/proc.h> 46 #include <sys/file.h> 47 #include <sys/buf.h> 48 #include <sys/vnode.h> 49 #include <sys/namei.h> 50 #include <sys/kernel.h> 51 #include <sys/malloc.h> 52 #include <sys/pool.h> 53 #include <sys/stat.h> 54 55 #include <fs/cd9660/iso.h> 56 #include <fs/cd9660/cd9660_extern.h> 57 #include <fs/cd9660/cd9660_node.h> 58 #include <fs/cd9660/cd9660_mount.h> 59 #include <fs/cd9660/iso_rrip.h> 60 61 /* 62 * Structures associated with iso_node caching. 63 */ 64 LIST_HEAD(ihashhead, iso_node) *isohashtbl; 65 u_long isohash; 66 #define INOHASH(device, inum) (((device) + ((inum)>>12)) & isohash) 67 struct simplelock cd9660_ihash_slock; 68 69 #ifdef ISODEVMAP 70 LIST_HEAD(idvhashhead, iso_dnode) *idvhashtbl; 71 u_long idvhash; 72 #define DNOHASH(device, inum) (((device) + ((inum)>>12)) & idvhash) 73 #endif 74 75 extern int prtactive; /* 1 => print out reclaim of active vnodes */ 76 77 struct pool cd9660_node_pool; 78 79 static u_int cd9660_chars2ui __P((u_char *, int)); 80 81 /* 82 * Initialize hash links for inodes and dnodes. 83 */ 84 void 85 cd9660_init() 86 { 87 isohashtbl = hashinit(desiredvnodes, HASH_LIST, M_ISOFSMNT, M_WAITOK, 88 &isohash); 89 simple_lock_init(&cd9660_ihash_slock); 90 #ifdef ISODEVMAP 91 idvhashtbl = hashinit(desiredvnodes / 8, HASH_LIST, M_ISOFSMNT, 92 M_WAITOK, &idvhash); 93 #endif 94 pool_init(&cd9660_node_pool, sizeof(struct iso_node), 0, 0, 0, 95 "cd9660nopl", &pool_allocator_nointr); 96 } 97 98 /* 99 * Reinitialize inode hash table. 100 */ 101 102 void 103 cd9660_reinit() 104 { 105 struct iso_node *ip; 106 struct ihashhead *oldhash1, *hash1; 107 u_long oldmask1, mask1, val; 108 #ifdef ISODEVMAP 109 struct iso_dnode *dp; 110 struct idvhashhead *oldhash2, *hash2; 111 u_long oldmask2, mask2; 112 #endif 113 u_int i; 114 115 hash1 = hashinit(desiredvnodes, HASH_LIST, M_ISOFSMNT, M_WAITOK, 116 &mask1); 117 #ifdef ISODEVMAP 118 hash2 = hashinit(desiredvnodes / 8, HASH_LIST, M_ISOFSMNT, M_WAITOK, 119 &mask2); 120 #endif 121 122 simple_lock(&cd9660_ihash_slock); 123 oldhash1 = isohashtbl; 124 oldmask1 = isohash; 125 isohashtbl = hash1; 126 isohash = mask1; 127 #ifdef ISODEVMAP 128 oldhash2 = idvhashtbl; 129 oldmask2 = idvhash; 130 idvhashtbl = hash2; 131 idvhash = mask2; 132 for (i = 0; i <= oldmask2; i++) { 133 while ((dp = LIST_FIRST(&oldhash2[i])) != NULL) { 134 LIST_REMOVE(dp, d_hash); 135 val = DNOHASH(dp->i_dev, dp->i_number); 136 LIST_INSERT_HEAD(&hash2[val], dp, d_hash); 137 } 138 } 139 #endif 140 for (i = 0; i <= oldmask1; i++) { 141 while ((ip = LIST_FIRST(&oldhash1[i])) != NULL) { 142 LIST_REMOVE(ip, i_hash); 143 val = INOHASH(ip->i_dev, ip->i_number); 144 LIST_INSERT_HEAD(&hash1[val], ip, i_hash); 145 } 146 } 147 simple_unlock(&cd9660_ihash_slock); 148 hashdone(oldhash1, M_ISOFSMNT); 149 #ifdef ISODEVMAP 150 hashdone(oldhash2, M_ISOFSMNT); 151 #endif 152 } 153 154 /* 155 * Destroy node pool and hash table. 156 */ 157 void 158 cd9660_done() 159 { 160 hashdone(isohashtbl, M_ISOFSMNT); 161 #ifdef ISODEVMAP 162 hashdone(idvhashtbl, M_ISOFSMNT); 163 #endif 164 pool_destroy(&cd9660_node_pool); 165 } 166 167 #ifdef ISODEVMAP 168 /* 169 * Enter a new node into the device hash list 170 */ 171 struct iso_dnode * 172 iso_dmap(device, inum, create) 173 dev_t device; 174 ino_t inum; 175 int create; 176 { 177 struct iso_dnode *dp; 178 struct idvhashhead *hp; 179 180 hp = &idvhashtbl[DNOHASH(device, inum)]; 181 LIST_FOREACH(dp, hp, d_hash) { 182 if (inum == dp->i_number && device == dp->i_dev) 183 return (dp); 184 } 185 186 if (!create) 187 return (NULL); 188 189 MALLOC(dp, struct iso_dnode *, sizeof(struct iso_dnode), M_CACHE, 190 M_WAITOK); 191 dp->i_dev = device; 192 dp->i_number = inum; 193 LIST_INSERT_HEAD(hp, dp, d_hash); 194 return (dp); 195 } 196 197 void 198 iso_dunmap(device) 199 dev_t device; 200 { 201 struct idvhashhead *dpp; 202 struct iso_dnode *dp, *dq; 203 204 for (dpp = idvhashtbl; dpp <= idvhashtbl + idvhash; dpp++) { 205 for (dp = LIST_FIRST(dpp); dp != NULL; dp = dq) { 206 dq = LIST_NEXT(dp, d_hash); 207 if (device == dp->i_dev) { 208 LIST_REMOVE(dp, d_hash); 209 FREE(dp, M_CACHE); 210 } 211 } 212 } 213 } 214 #endif 215 216 /* 217 * Use the device/inum pair to find the incore inode, and return a pointer 218 * to it. If it is in core, but locked, wait for it. 219 */ 220 struct vnode * 221 cd9660_ihashget(dev, inum) 222 dev_t dev; 223 ino_t inum; 224 { 225 struct iso_node *ip; 226 struct vnode *vp; 227 228 loop: 229 simple_lock(&cd9660_ihash_slock); 230 LIST_FOREACH(ip, &isohashtbl[INOHASH(dev, inum)], i_hash) { 231 if (inum == ip->i_number && dev == ip->i_dev) { 232 vp = ITOV(ip); 233 simple_lock(&vp->v_interlock); 234 simple_unlock(&cd9660_ihash_slock); 235 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) 236 goto loop; 237 return (vp); 238 } 239 } 240 simple_unlock(&cd9660_ihash_slock); 241 return (NULL); 242 } 243 244 /* 245 * Insert the inode into the hash table, and return it locked. 246 * 247 * ip->i_vnode must be initialized first. 248 */ 249 void 250 cd9660_ihashins(ip) 251 struct iso_node *ip; 252 { 253 struct ihashhead *ipp; 254 255 simple_lock(&cd9660_ihash_slock); 256 ipp = &isohashtbl[INOHASH(ip->i_dev, ip->i_number)]; 257 LIST_INSERT_HEAD(ipp, ip, i_hash); 258 simple_unlock(&cd9660_ihash_slock); 259 260 lockmgr(&ip->i_vnode->v_lock, LK_EXCLUSIVE, &ip->i_vnode->v_interlock); 261 } 262 263 /* 264 * Remove the inode from the hash table. 265 */ 266 void 267 cd9660_ihashrem(ip) 268 struct iso_node *ip; 269 { 270 simple_lock(&cd9660_ihash_slock); 271 LIST_REMOVE(ip, i_hash); 272 simple_unlock(&cd9660_ihash_slock); 273 } 274 275 /* 276 * Last reference to an inode, write the inode out and if necessary, 277 * truncate and deallocate the file. 278 */ 279 int 280 cd9660_inactive(v) 281 void *v; 282 { 283 struct vop_inactive_args /* { 284 struct vnode *a_vp; 285 struct proc *a_p; 286 } */ *ap = v; 287 struct vnode *vp = ap->a_vp; 288 struct proc *p = ap->a_p; 289 struct iso_node *ip = VTOI(vp); 290 int error = 0; 291 292 if (prtactive && vp->v_usecount != 0) 293 vprint("cd9660_inactive: pushing active", vp); 294 295 ip->i_flag = 0; 296 VOP_UNLOCK(vp, 0); 297 /* 298 * If we are done with the inode, reclaim it 299 * so that it can be reused immediately. 300 */ 301 if (ip->inode.iso_mode == 0) 302 vrecycle(vp, (struct simplelock *)0, p); 303 return error; 304 } 305 306 /* 307 * Reclaim an inode so that it can be used for other purposes. 308 */ 309 int 310 cd9660_reclaim(v) 311 void *v; 312 { 313 struct vop_reclaim_args /* { 314 struct vnode *a_vp; 315 struct proc *a_p; 316 } */ *ap = v; 317 struct vnode *vp = ap->a_vp; 318 struct iso_node *ip = VTOI(vp); 319 320 if (prtactive && vp->v_usecount != 0) 321 vprint("cd9660_reclaim: pushing active", vp); 322 /* 323 * Remove the inode from its hash chain. 324 */ 325 cd9660_ihashrem(ip); 326 /* 327 * Purge old data structures associated with the inode. 328 */ 329 cache_purge(vp); 330 if (ip->i_devvp) { 331 vrele(ip->i_devvp); 332 ip->i_devvp = 0; 333 } 334 pool_put(&cd9660_node_pool, vp->v_data); 335 vp->v_data = NULL; 336 return (0); 337 } 338 339 /* 340 * File attributes 341 */ 342 void 343 cd9660_defattr(isodir, inop, bp) 344 struct iso_directory_record *isodir; 345 struct iso_node *inop; 346 struct buf *bp; 347 { 348 struct buf *bp2 = NULL; 349 struct iso_mnt *imp; 350 struct iso_extended_attributes *ap = NULL; 351 int off; 352 353 if (isonum_711(isodir->flags)&2) { 354 inop->inode.iso_mode = S_IFDIR; 355 /* 356 * If we return 2, fts() will assume there are no subdirectories 357 * (just links for the path and .), so instead we return 1. 358 */ 359 inop->inode.iso_links = 1; 360 } else { 361 inop->inode.iso_mode = S_IFREG; 362 inop->inode.iso_links = 1; 363 } 364 if (!bp 365 && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT) 366 && (off = isonum_711(isodir->ext_attr_length))) { 367 VOP_BLKATOFF(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL, 368 &bp2); 369 bp = bp2; 370 } 371 if (bp) { 372 ap = (struct iso_extended_attributes *)bp->b_data; 373 374 if (isonum_711(ap->version) == 1) { 375 if (!(ap->perm[1]&0x10)) 376 inop->inode.iso_mode |= S_IRUSR; 377 if (!(ap->perm[1]&0x40)) 378 inop->inode.iso_mode |= S_IXUSR; 379 if (!(ap->perm[0]&0x01)) 380 inop->inode.iso_mode |= S_IRGRP; 381 if (!(ap->perm[0]&0x04)) 382 inop->inode.iso_mode |= S_IXGRP; 383 if (!(ap->perm[0]&0x10)) 384 inop->inode.iso_mode |= S_IROTH; 385 if (!(ap->perm[0]&0x40)) 386 inop->inode.iso_mode |= S_IXOTH; 387 inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */ 388 inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */ 389 } else 390 ap = NULL; 391 } 392 if (!ap) { 393 inop->inode.iso_mode |= 394 S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; 395 inop->inode.iso_uid = (uid_t)0; 396 inop->inode.iso_gid = (gid_t)0; 397 } 398 if (bp2) 399 brelse(bp2); 400 } 401 402 /* 403 * Time stamps 404 */ 405 void 406 cd9660_deftstamp(isodir,inop,bp) 407 struct iso_directory_record *isodir; 408 struct iso_node *inop; 409 struct buf *bp; 410 { 411 struct buf *bp2 = NULL; 412 struct iso_mnt *imp; 413 struct iso_extended_attributes *ap = NULL; 414 int off; 415 416 if (!bp 417 && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT) 418 && (off = isonum_711(isodir->ext_attr_length))) { 419 VOP_BLKATOFF(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL, 420 &bp2); 421 bp = bp2; 422 } 423 if (bp) { 424 ap = (struct iso_extended_attributes *)bp->b_data; 425 426 if (isonum_711(ap->version) == 1) { 427 if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime)) 428 cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime); 429 if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime)) 430 inop->inode.iso_ctime = inop->inode.iso_atime; 431 if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime)) 432 inop->inode.iso_mtime = inop->inode.iso_ctime; 433 } else 434 ap = NULL; 435 } 436 if (!ap) { 437 cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime); 438 inop->inode.iso_atime = inop->inode.iso_ctime; 439 inop->inode.iso_mtime = inop->inode.iso_ctime; 440 } 441 if (bp2) 442 brelse(bp2); 443 } 444 445 int 446 cd9660_tstamp_conv7(pi,pu) 447 u_char *pi; 448 struct timespec *pu; 449 { 450 int crtime, days; 451 int y, m, d, hour, minute, second, tz; 452 453 y = pi[0] + 1900; 454 m = pi[1]; 455 d = pi[2]; 456 hour = pi[3]; 457 minute = pi[4]; 458 second = pi[5]; 459 tz = pi[6]; 460 461 if (y < 1970) { 462 pu->tv_sec = 0; 463 pu->tv_nsec = 0; 464 return 0; 465 } else { 466 #ifdef ORIGINAL 467 /* computes day number relative to Sept. 19th,1989 */ 468 /* don't even *THINK* about changing formula. It works! */ 469 days = 367*(y-1980)-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d-100; 470 #else 471 /* 472 * Changed :-) to make it relative to Jan. 1st, 1970 473 * and to disambiguate negative division 474 */ 475 days = 367*(y-1960)-7*(y+(m+9)/12)/4-3*((y+(m+9)/12-1)/100+1)/4+275*m/9+d-239; 476 #endif 477 crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second; 478 479 /* timezone offset is unreliable on some disks */ 480 if (-48 <= tz && tz <= 52) 481 crtime -= tz * 15 * 60; 482 } 483 pu->tv_sec = crtime; 484 pu->tv_nsec = 0; 485 return 1; 486 } 487 488 static u_int 489 cd9660_chars2ui(begin,len) 490 u_char *begin; 491 int len; 492 { 493 u_int rc; 494 495 for (rc = 0; --len >= 0;) { 496 rc *= 10; 497 rc += *begin++ - '0'; 498 } 499 return rc; 500 } 501 502 int 503 cd9660_tstamp_conv17(pi,pu) 504 u_char *pi; 505 struct timespec *pu; 506 { 507 u_char buf[7]; 508 509 /* year:"0001"-"9999" -> -1900 */ 510 buf[0] = cd9660_chars2ui(pi,4) - 1900; 511 512 /* month: " 1"-"12" -> 1 - 12 */ 513 buf[1] = cd9660_chars2ui(pi + 4,2); 514 515 /* day: " 1"-"31" -> 1 - 31 */ 516 buf[2] = cd9660_chars2ui(pi + 6,2); 517 518 /* hour: " 0"-"23" -> 0 - 23 */ 519 buf[3] = cd9660_chars2ui(pi + 8,2); 520 521 /* minute:" 0"-"59" -> 0 - 59 */ 522 buf[4] = cd9660_chars2ui(pi + 10,2); 523 524 /* second:" 0"-"59" -> 0 - 59 */ 525 buf[5] = cd9660_chars2ui(pi + 12,2); 526 527 /* difference of GMT */ 528 buf[6] = pi[16]; 529 530 return cd9660_tstamp_conv7(buf,pu); 531 } 532 533 ino_t 534 isodirino(isodir, imp) 535 struct iso_directory_record *isodir; 536 struct iso_mnt *imp; 537 { 538 ino_t ino; 539 540 ino = (isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length)) 541 << imp->im_bshift; 542 return (ino); 543 } 544