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.24 1998/02/06 12:13:20 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 static struct simplelock cd9660_ihash_slock; 62 63 static void cd9660_ihashrem __P((struct iso_node *)); 64 static unsigned cd9660_chars2ui __P((unsigned char *begin, int len)); 65 66 /* 67 * Initialize hash links for inodes and dnodes. 68 */ 69 int 70 cd9660_init(vfsp) 71 struct vfsconf *vfsp; 72 { 73 74 isohashtbl = hashinit(desiredvnodes, M_ISOFSMNT, &isohash); 75 simple_lock_init(&cd9660_ihash_slock); 76 return (0); 77 } 78 79 80 /* 81 * Use the device/inum pair to find the incore inode, and return a pointer 82 * to it. If it is in core, but locked, wait for it. 83 */ 84 struct vnode * 85 cd9660_ihashget(dev, inum) 86 dev_t dev; 87 ino_t inum; 88 { 89 struct proc *p = curproc; /* XXX */ 90 struct iso_node *ip; 91 struct vnode *vp; 92 93 loop: 94 simple_lock(&cd9660_ihash_slock); 95 for (ip = isohashtbl[INOHASH(dev, inum)]; ip; ip = ip->i_next) { 96 if (inum == ip->i_number && dev == ip->i_dev) { 97 vp = ITOV(ip); 98 simple_lock(&vp->v_interlock); 99 simple_unlock(&cd9660_ihash_slock); 100 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) 101 goto loop; 102 return (vp); 103 } 104 } 105 simple_unlock(&cd9660_ihash_slock); 106 return (NULL); 107 } 108 109 /* 110 * Insert the inode into the hash table, and return it locked. 111 */ 112 void 113 cd9660_ihashins(ip) 114 struct iso_node *ip; 115 { 116 struct proc *p = curproc; /* XXX */ 117 struct iso_node **ipp, *iq; 118 119 simple_lock(&cd9660_ihash_slock); 120 ipp = &isohashtbl[INOHASH(ip->i_dev, ip->i_number)]; 121 if (iq = *ipp) 122 iq->i_prev = &ip->i_next; 123 ip->i_next = iq; 124 ip->i_prev = ipp; 125 *ipp = ip; 126 simple_unlock(&cd9660_ihash_slock); 127 128 lockmgr(&ip->i_lock, LK_EXCLUSIVE, (struct simplelock *)0, p); 129 } 130 131 /* 132 * Remove the inode from the hash table. 133 */ 134 static void 135 cd9660_ihashrem(ip) 136 register struct iso_node *ip; 137 { 138 register struct iso_node *iq; 139 140 simple_lock(&cd9660_ihash_slock); 141 if (iq = ip->i_next) 142 iq->i_prev = ip->i_prev; 143 *ip->i_prev = iq; 144 #ifdef DIAGNOSTIC 145 ip->i_next = NULL; 146 ip->i_prev = NULL; 147 #endif 148 simple_unlock(&cd9660_ihash_slock); 149 } 150 151 /* 152 * Last reference to an inode, write the inode out and if necessary, 153 * truncate and deallocate the file. 154 */ 155 int 156 cd9660_inactive(ap) 157 struct vop_inactive_args /* { 158 struct vnode *a_vp; 159 struct proc *a_p; 160 } */ *ap; 161 { 162 struct vnode *vp = ap->a_vp; 163 struct proc *p = ap->a_p; 164 register struct iso_node *ip = VTOI(vp); 165 int error = 0; 166 167 if (prtactive && vp->v_usecount != 0) 168 vprint("cd9660_inactive: pushing active", vp); 169 170 ip->i_flag = 0; 171 VOP_UNLOCK(vp, 0, p); 172 /* 173 * If we are done with the inode, reclaim it 174 * so that it can be reused immediately. 175 */ 176 if (ip->inode.iso_mode == 0) 177 vrecycle(vp, (struct simplelock *)0, p); 178 return error; 179 } 180 181 /* 182 * Reclaim an inode so that it can be used for other purposes. 183 */ 184 int 185 cd9660_reclaim(ap) 186 struct vop_reclaim_args /* { 187 struct vnode *a_vp; 188 struct proc *a_p; 189 } */ *ap; 190 { 191 register struct vnode *vp = ap->a_vp; 192 register struct iso_node *ip = VTOI(vp); 193 194 if (prtactive && vp->v_usecount != 0) 195 vprint("cd9660_reclaim: pushing active", vp); 196 /* 197 * Remove the inode from its hash chain. 198 */ 199 cd9660_ihashrem(ip); 200 /* 201 * Purge old data structures associated with the inode. 202 */ 203 cache_purge(vp); 204 if (ip->i_devvp) { 205 vrele(ip->i_devvp); 206 ip->i_devvp = 0; 207 } 208 FREE(vp->v_data, M_ISOFSNODE); 209 vp->v_data = NULL; 210 return (0); 211 } 212 213 /* 214 * File attributes 215 */ 216 void 217 cd9660_defattr(isodir, inop, bp, ftype) 218 struct iso_directory_record *isodir; 219 struct iso_node *inop; 220 struct buf *bp; 221 enum ISO_FTYPE ftype; 222 { 223 struct buf *bp2 = NULL; 224 struct iso_mnt *imp; 225 struct iso_extended_attributes *ap = NULL; 226 int off; 227 228 /* high sierra does not have timezone data, flag is one byte ahead */ 229 if (isonum_711(ftype == ISO_FTYPE_HIGH_SIERRA? 230 &isodir->date[6]: isodir->flags)&2) { 231 inop->inode.iso_mode = S_IFDIR; 232 /* 233 * If we return 2, fts() will assume there are no subdirectories 234 * (just links for the path and .), so instead we return 1. 235 */ 236 inop->inode.iso_links = 1; 237 } else { 238 inop->inode.iso_mode = S_IFREG; 239 inop->inode.iso_links = 1; 240 } 241 if (!bp 242 && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT) 243 && (off = isonum_711(isodir->ext_attr_length))) { 244 cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL, 245 &bp2); 246 bp = bp2; 247 } 248 if (bp) { 249 ap = (struct iso_extended_attributes *)bp->b_data; 250 251 if (isonum_711(ap->version) == 1) { 252 if (!(ap->perm[0]&0x40)) 253 inop->inode.iso_mode |= VEXEC >> 6; 254 if (!(ap->perm[0]&0x10)) 255 inop->inode.iso_mode |= VREAD >> 6; 256 if (!(ap->perm[0]&4)) 257 inop->inode.iso_mode |= VEXEC >> 3; 258 if (!(ap->perm[0]&1)) 259 inop->inode.iso_mode |= VREAD >> 3; 260 if (!(ap->perm[1]&0x40)) 261 inop->inode.iso_mode |= VEXEC; 262 if (!(ap->perm[1]&0x10)) 263 inop->inode.iso_mode |= VREAD; 264 inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */ 265 inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */ 266 } else 267 ap = NULL; 268 } 269 if (!ap) { 270 inop->inode.iso_mode |= VREAD|VEXEC|(VREAD|VEXEC)>>3|(VREAD|VEXEC)>>6; 271 inop->inode.iso_uid = (uid_t)0; 272 inop->inode.iso_gid = (gid_t)0; 273 } 274 if (bp2) 275 brelse(bp2); 276 } 277 278 /* 279 * Time stamps 280 */ 281 void 282 cd9660_deftstamp(isodir,inop,bp,ftype) 283 struct iso_directory_record *isodir; 284 struct iso_node *inop; 285 struct buf *bp; 286 enum ISO_FTYPE ftype; 287 { 288 struct buf *bp2 = NULL; 289 struct iso_mnt *imp; 290 struct iso_extended_attributes *ap = NULL; 291 int off; 292 293 if (!bp 294 && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT) 295 && (off = isonum_711(isodir->ext_attr_length))) { 296 cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL, 297 &bp2); 298 bp = bp2; 299 } 300 if (bp) { 301 ap = (struct iso_extended_attributes *)bp->b_data; 302 303 if (ftype != ISO_FTYPE_HIGH_SIERRA 304 && isonum_711(ap->version) == 1) { 305 if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime)) 306 cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime); 307 if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime)) 308 inop->inode.iso_ctime = inop->inode.iso_atime; 309 if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime)) 310 inop->inode.iso_mtime = inop->inode.iso_ctime; 311 } else 312 ap = NULL; 313 } 314 if (!ap) { 315 cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime,ftype); 316 inop->inode.iso_atime = inop->inode.iso_ctime; 317 inop->inode.iso_mtime = inop->inode.iso_ctime; 318 } 319 if (bp2) 320 brelse(bp2); 321 } 322 323 int 324 cd9660_tstamp_conv7(pi,pu,ftype) 325 u_char *pi; 326 struct timespec *pu; 327 enum ISO_FTYPE ftype; 328 { 329 int crtime, days; 330 int y, m, d, hour, minute, second, tz; 331 332 y = pi[0] + 1900; 333 m = pi[1]; 334 d = pi[2]; 335 hour = pi[3]; 336 minute = pi[4]; 337 second = pi[5]; 338 if(ftype != ISO_FTYPE_HIGH_SIERRA) 339 tz = pi[6]; 340 else 341 /* original high sierra misses timezone data */ 342 tz = 0; 343 344 if (y < 1970) { 345 pu->tv_sec = 0; 346 pu->tv_nsec = 0; 347 return 0; 348 } else { 349 #ifdef ORIGINAL 350 /* computes day number relative to Sept. 19th,1989 */ 351 /* don't even *THINK* about changing formula. It works! */ 352 days = 367*(y-1980)-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d-100; 353 #else 354 /* 355 * Changed :-) to make it relative to Jan. 1st, 1970 356 * and to disambiguate negative division 357 */ 358 days = 367*(y-1960)-7*(y+(m+9)/12)/4-3*((y+(m+9)/12-1)/100+1)/4+275*m/9+d-239; 359 #endif 360 crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second; 361 362 /* timezone offset is unreliable on some disks */ 363 if (-48 <= tz && tz <= 52) 364 crtime -= tz * 15 * 60; 365 } 366 pu->tv_sec = crtime; 367 pu->tv_nsec = 0; 368 return 1; 369 } 370 371 static u_int 372 cd9660_chars2ui(begin,len) 373 u_char *begin; 374 int len; 375 { 376 u_int rc; 377 378 for (rc = 0; --len >= 0;) { 379 rc *= 10; 380 rc += *begin++ - '0'; 381 } 382 return rc; 383 } 384 385 int 386 cd9660_tstamp_conv17(pi,pu) 387 u_char *pi; 388 struct timespec *pu; 389 { 390 u_char buf[7]; 391 392 /* year:"0001"-"9999" -> -1900 */ 393 buf[0] = cd9660_chars2ui(pi,4) - 1900; 394 395 /* month: " 1"-"12" -> 1 - 12 */ 396 buf[1] = cd9660_chars2ui(pi + 4,2); 397 398 /* day: " 1"-"31" -> 1 - 31 */ 399 buf[2] = cd9660_chars2ui(pi + 6,2); 400 401 /* hour: " 0"-"23" -> 0 - 23 */ 402 buf[3] = cd9660_chars2ui(pi + 8,2); 403 404 /* minute:" 0"-"59" -> 0 - 59 */ 405 buf[4] = cd9660_chars2ui(pi + 10,2); 406 407 /* second:" 0"-"59" -> 0 - 59 */ 408 buf[5] = cd9660_chars2ui(pi + 12,2); 409 410 /* difference of GMT */ 411 buf[6] = pi[16]; 412 413 return cd9660_tstamp_conv7(buf, pu, ISO_FTYPE_DEFAULT); 414 } 415 416 ino_t 417 isodirino(isodir, imp) 418 struct iso_directory_record *isodir; 419 struct iso_mnt *imp; 420 { 421 ino_t ino; 422 423 ino = (isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length)) 424 << imp->im_bshift; 425 return (ino); 426 } 427