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.11 2004/08/28 19:02:15 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(struct vfsconf *vfsp) 74 { 75 isohash = 16; 76 while (isohash < desiredvnodes) 77 isohash <<= 1; 78 isohashtbl = malloc(sizeof(void *) * isohash, 79 M_ISOFSMNT, M_WAITOK|M_ZERO); 80 --isohash; 81 lwkt_token_init(&cd9660_ihash_token); 82 return (0); 83 } 84 85 int 86 cd9660_uninit(struct vfsconf *vfsp) 87 { 88 89 if (isohashtbl != NULL) 90 free(isohashtbl, M_ISOFSMNT); 91 return (0); 92 } 93 94 95 /* 96 * Use the device/inum pair to find the incore inode, and return a pointer 97 * to it. If it is in core, but locked, wait for it. 98 */ 99 struct vnode * 100 cd9660_ihashget(dev_t dev, 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, return 0 on success, non-zero 138 * if the inode has already been found to be in the hash table. 139 */ 140 int 141 cd9660_ihashins(struct iso_node *ip) 142 { 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 while ((iq = *ipp) != NULL) { 149 if (iq->i_dev == ip->i_dev && iq->i_number == ip->i_number) { 150 lwkt_reltoken(&ilock); 151 return(EBUSY); 152 } 153 ipp = &iq->i_next; 154 } 155 ip->i_next = NULL; 156 *ipp = ip; 157 lwkt_reltoken(&ilock); 158 return(0); 159 } 160 161 /* 162 * Remove the inode from the hash table. 163 */ 164 static void 165 cd9660_ihashrem(struct iso_node *ip) 166 { 167 struct iso_node **ipp, *iq; 168 lwkt_tokref ilock; 169 170 lwkt_gettoken(&ilock, &cd9660_ihash_token); 171 ipp = &isohashtbl[INOHASH(ip->i_dev, ip->i_number)]; 172 while ((iq = *ipp) != NULL) { 173 if (ip == iq) 174 break; 175 ipp = &iq->i_next; 176 } 177 KKASSERT(ip == iq); 178 *ipp = ip->i_next; 179 ip->i_next = NULL; 180 lwkt_reltoken(&ilock); 181 } 182 183 /* 184 * Last reference to an inode, write the inode out and if necessary, 185 * truncate and deallocate the file. 186 * 187 * cd9660_inactive(struct vnode *a_vp, struct thread *a_td) 188 */ 189 int 190 cd9660_inactive(struct vop_inactive_args *ap) 191 { 192 struct vnode *vp = ap->a_vp; 193 struct thread *td = ap->a_td; 194 struct iso_node *ip = VTOI(vp); 195 int error = 0; 196 197 if (prtactive && vp->v_usecount != 0) 198 vprint("cd9660_inactive: pushing active", vp); 199 200 if (ip) 201 ip->i_flag = 0; 202 VOP_UNLOCK(vp, NULL, 0, td); 203 /* 204 * If we are done with the inode, reclaim it 205 * so that it can be reused immediately. 206 */ 207 if (ip == NULL || ip->inode.iso_mode == 0) 208 vrecycle(vp, NULL, td); 209 return error; 210 } 211 212 /* 213 * Reclaim an inode so that it can be used for other purposes. 214 * 215 * cd9660_reclaim(struct vnode *a_vp, struct proc *a_p) 216 */ 217 int 218 cd9660_reclaim(struct vop_reclaim_args *ap) 219 { 220 struct vnode *vp = ap->a_vp; 221 struct iso_node *ip = VTOI(vp); 222 223 if (prtactive && vp->v_usecount != 0) 224 vprint("cd9660_reclaim: pushing active", vp); 225 /* 226 * Remove the inode from its hash chain and purge namecache 227 * data associated with the vnode. 228 */ 229 cache_purge(vp); 230 vp->v_data = NULL; 231 if (ip) { 232 cd9660_ihashrem(ip); 233 if (ip->i_devvp) { 234 vrele(ip->i_devvp); 235 ip->i_devvp = 0; 236 } 237 free(ip, M_ISOFSNODE); 238 } 239 return (0); 240 } 241 242 /* 243 * File attributes 244 */ 245 void 246 cd9660_defattr(struct iso_directory_record *isodir, struct iso_node *inop, 247 struct buf *bp, enum ISO_FTYPE ftype) 248 { 249 struct buf *bp2 = NULL; 250 struct iso_mnt *imp; 251 struct iso_extended_attributes *ap = NULL; 252 int off; 253 254 /* high sierra does not have timezone data, flag is one byte ahead */ 255 if (isonum_711(ftype == ISO_FTYPE_HIGH_SIERRA? 256 &isodir->date[6]: isodir->flags)&2) { 257 inop->inode.iso_mode = S_IFDIR; 258 /* 259 * If we return 2, fts() will assume there are no subdirectories 260 * (just links for the path and .), so instead we return 1. 261 */ 262 inop->inode.iso_links = 1; 263 } else { 264 inop->inode.iso_mode = S_IFREG; 265 inop->inode.iso_links = 1; 266 } 267 if (!bp 268 && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT) 269 && (off = isonum_711(isodir->ext_attr_length))) { 270 cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL, 271 &bp2); 272 bp = bp2; 273 } 274 if (bp) { 275 ap = (struct iso_extended_attributes *)bp->b_data; 276 277 if (isonum_711(ap->version) == 1) { 278 if (!(ap->perm[0]&0x40)) 279 inop->inode.iso_mode |= VEXEC >> 6; 280 if (!(ap->perm[0]&0x10)) 281 inop->inode.iso_mode |= VREAD >> 6; 282 if (!(ap->perm[0]&4)) 283 inop->inode.iso_mode |= VEXEC >> 3; 284 if (!(ap->perm[0]&1)) 285 inop->inode.iso_mode |= VREAD >> 3; 286 if (!(ap->perm[1]&0x40)) 287 inop->inode.iso_mode |= VEXEC; 288 if (!(ap->perm[1]&0x10)) 289 inop->inode.iso_mode |= VREAD; 290 inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */ 291 inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */ 292 } else 293 ap = NULL; 294 } 295 if (!ap) { 296 inop->inode.iso_mode |= VREAD|VEXEC|(VREAD|VEXEC)>>3|(VREAD|VEXEC)>>6; 297 inop->inode.iso_uid = (uid_t)0; 298 inop->inode.iso_gid = (gid_t)0; 299 } 300 if (bp2) 301 brelse(bp2); 302 } 303 304 /* 305 * Time stamps 306 */ 307 void 308 cd9660_deftstamp(struct iso_directory_record *isodir, struct iso_node *inop, 309 struct buf *bp, 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(u_char *pi, struct timespec *pu, enum ISO_FTYPE ftype) 348 { 349 int crtime, days; 350 int y, m, d, hour, minute, second, tz; 351 352 y = pi[0] + 1900; 353 m = pi[1]; 354 d = pi[2]; 355 hour = pi[3]; 356 minute = pi[4]; 357 second = pi[5]; 358 if(ftype != ISO_FTYPE_HIGH_SIERRA) 359 tz = pi[6]; 360 else 361 /* original high sierra misses timezone data */ 362 tz = 0; 363 364 if (y < 1970) { 365 pu->tv_sec = 0; 366 pu->tv_nsec = 0; 367 return 0; 368 } else { 369 #ifdef ORIGINAL 370 /* computes day number relative to Sept. 19th,1989 */ 371 /* don't even *THINK* about changing formula. It works! */ 372 days = 367*(y-1980)-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d-100; 373 #else 374 /* 375 * Changed :-) to make it relative to Jan. 1st, 1970 376 * and to disambiguate negative division 377 */ 378 days = 367*(y-1960)-7*(y+(m+9)/12)/4-3*((y+(m+9)/12-1)/100+1)/4+275*m/9+d-239; 379 #endif 380 crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second; 381 382 /* timezone offset is unreliable on some disks */ 383 if (-48 <= tz && tz <= 52) 384 crtime -= tz * 15 * 60; 385 } 386 pu->tv_sec = crtime; 387 pu->tv_nsec = 0; 388 return 1; 389 } 390 391 static u_int 392 cd9660_chars2ui(u_char *begin, int len) 393 { 394 u_int rc; 395 396 for (rc = 0; --len >= 0;) { 397 rc *= 10; 398 rc += *begin++ - '0'; 399 } 400 return rc; 401 } 402 403 int 404 cd9660_tstamp_conv17(u_char *pi, struct timespec *pu) 405 { 406 u_char buf[7]; 407 408 /* year:"0001"-"9999" -> -1900 */ 409 buf[0] = cd9660_chars2ui(pi,4) - 1900; 410 411 /* month: " 1"-"12" -> 1 - 12 */ 412 buf[1] = cd9660_chars2ui(pi + 4,2); 413 414 /* day: " 1"-"31" -> 1 - 31 */ 415 buf[2] = cd9660_chars2ui(pi + 6,2); 416 417 /* hour: " 0"-"23" -> 0 - 23 */ 418 buf[3] = cd9660_chars2ui(pi + 8,2); 419 420 /* minute:" 0"-"59" -> 0 - 59 */ 421 buf[4] = cd9660_chars2ui(pi + 10,2); 422 423 /* second:" 0"-"59" -> 0 - 59 */ 424 buf[5] = cd9660_chars2ui(pi + 12,2); 425 426 /* difference of GMT */ 427 buf[6] = pi[16]; 428 429 return cd9660_tstamp_conv7(buf, pu, ISO_FTYPE_DEFAULT); 430 } 431 432 ino_t 433 isodirino(struct iso_directory_record *isodir, struct iso_mnt *imp) 434 { 435 ino_t ino; 436 437 ino = (isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length)) 438 << imp->im_bshift; 439 return (ino); 440 } 441