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