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