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