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