1 /* $OpenBSD: cd9660_node.c,v 1.31 2016/06/19 11:54:33 natano Exp $ */ 2 /* $NetBSD: cd9660_node.c,v 1.17 1997/05/05 07:13:57 mycroft Exp $ */ 3 4 /*- 5 * Copyright (c) 1982, 1986, 1989, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley 9 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension 10 * Support code is derived from software contributed to Berkeley 11 * by Atsushi Murai (amurai@spec.co.jp). 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)cd9660_node.c 8.5 (Berkeley) 12/5/94 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/mount.h> 43 #include <sys/file.h> 44 #include <sys/buf.h> 45 #include <sys/vnode.h> 46 #include <sys/lock.h> 47 #include <sys/namei.h> 48 #include <sys/kernel.h> 49 #include <sys/malloc.h> 50 #include <sys/stat.h> 51 52 #include <crypto/siphash.h> 53 54 #include <isofs/cd9660/iso.h> 55 #include <isofs/cd9660/cd9660_extern.h> 56 #include <isofs/cd9660/cd9660_node.h> 57 58 /* 59 * Structures associated with iso_node caching. 60 */ 61 u_int cd9660_isohash(dev_t, cdino_t); 62 63 struct iso_node **isohashtbl; 64 u_long isohash; 65 SIPHASH_KEY isohashkey; 66 #define INOHASH(device, inum) cd9660_isohash((device), (inum)) 67 68 extern int prtactive; /* 1 => print out reclaim of active vnodes */ 69 70 static u_int cd9660_chars2ui(u_char *, int); 71 72 /* 73 * Initialize hash links for inodes and dnodes. 74 */ 75 int 76 cd9660_init(vfsp) 77 struct vfsconf *vfsp; 78 { 79 80 isohashtbl = hashinit(initialvnodes, M_ISOFSMNT, M_WAITOK, &isohash); 81 arc4random_buf(&isohashkey, sizeof(isohashkey)); 82 return (0); 83 } 84 85 u_int 86 cd9660_isohash(dev_t device, cdino_t inum) 87 { 88 SIPHASH_CTX ctx; 89 90 SipHash24_Init(&ctx, &isohashkey); 91 SipHash24_Update(&ctx, &device, sizeof(device)); 92 SipHash24_Update(&ctx, &inum, sizeof(inum)); 93 return (SipHash24_End(&ctx) & isohash); 94 } 95 96 /* 97 * Use the device/inum pair to find the incore inode, and return a pointer 98 * to it. If it is in core, but locked, wait for it. 99 */ 100 struct vnode * 101 cd9660_ihashget(dev, inum) 102 dev_t dev; 103 cdino_t inum; 104 { 105 struct proc *p = curproc; /* XXX */ 106 struct iso_node *ip; 107 struct vnode *vp; 108 109 loop: 110 /* XXX locking lock hash list? */ 111 for (ip = isohashtbl[INOHASH(dev, inum)]; ip; ip = ip->i_next) { 112 if (inum == ip->i_number && dev == ip->i_dev) { 113 vp = ITOV(ip); 114 /* XXX locking unlock hash list? */ 115 if (vget(vp, LK_EXCLUSIVE, p)) 116 goto loop; 117 return (vp); 118 } 119 } 120 /* XXX locking unlock hash list? */ 121 return (NULL); 122 } 123 124 /* 125 * Insert the inode into the hash table, and return it locked. 126 */ 127 int 128 cd9660_ihashins(ip) 129 struct iso_node *ip; 130 { 131 struct iso_node **ipp, *iq; 132 133 /* XXX locking lock hash list? */ 134 ipp = &isohashtbl[INOHASH(ip->i_dev, ip->i_number)]; 135 136 for (iq = *ipp; iq; iq = iq->i_next) { 137 if (iq->i_dev == ip->i_dev && 138 iq->i_number == ip->i_number) 139 return (EEXIST); 140 } 141 142 if ((iq = *ipp) != NULL) 143 iq->i_prev = &ip->i_next; 144 ip->i_next = iq; 145 ip->i_prev = ipp; 146 *ipp = ip; 147 /* XXX locking unlock hash list? */ 148 149 rrw_enter(&ip->i_lock, RW_WRITE); 150 151 return (0); 152 } 153 154 /* 155 * Remove the inode from the hash table. 156 */ 157 void 158 cd9660_ihashrem(ip) 159 register struct iso_node *ip; 160 { 161 register struct iso_node *iq; 162 163 if (ip->i_prev == NULL) 164 return; 165 166 /* XXX locking lock hash list? */ 167 if ((iq = ip->i_next) != NULL) 168 iq->i_prev = ip->i_prev; 169 *ip->i_prev = iq; 170 #ifdef DIAGNOSTIC 171 ip->i_next = NULL; 172 ip->i_prev = NULL; 173 #endif 174 /* XXX locking unlock hash list? */ 175 } 176 177 /* 178 * Last reference to an inode, write the inode out and if necessary, 179 * truncate and deallocate the file. 180 */ 181 int 182 cd9660_inactive(v) 183 void *v; 184 { 185 struct vop_inactive_args *ap = v; 186 struct vnode *vp = ap->a_vp; 187 struct proc *p = ap->a_p; 188 register struct iso_node *ip = VTOI(vp); 189 int error = 0; 190 191 #ifdef DIAGNOSTIC 192 if (prtactive && vp->v_usecount != 0) 193 vprint("cd9660_inactive: pushing active", vp); 194 #endif 195 196 ip->i_flag = 0; 197 VOP_UNLOCK(vp, p); 198 /* 199 * If we are done with the inode, reclaim it 200 * so that it can be reused immediately. 201 */ 202 if (ip->inode.iso_mode == 0) 203 vrecycle(vp, p); 204 205 return (error); 206 } 207 208 /* 209 * Reclaim an inode so that it can be used for other purposes. 210 */ 211 int 212 cd9660_reclaim(v) 213 void *v; 214 { 215 struct vop_reclaim_args *ap = v; 216 register struct vnode *vp = ap->a_vp; 217 register struct iso_node *ip = VTOI(vp); 218 219 #ifdef DIAGNOSTIC 220 if (prtactive && vp->v_usecount != 0) 221 vprint("cd9660_reclaim: pushing active", vp); 222 #endif 223 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, 0); 237 vp->v_data = NULL; 238 return (0); 239 } 240 241 /* 242 * File attributes 243 */ 244 void 245 cd9660_defattr(isodir, inop, bp) 246 struct iso_directory_record *isodir; 247 struct iso_node *inop; 248 struct buf *bp; 249 { 250 struct buf *bp2 = NULL; 251 struct iso_mnt *imp; 252 struct iso_extended_attributes *ap = NULL; 253 int off; 254 255 if (isonum_711(isodir->flags)&2) { 256 inop->inode.iso_mode = S_IFDIR; 257 /* 258 * If we return 2, fts() will assume there are no subdirectories 259 * (just links for the path and .), so instead we return 1. 260 */ 261 inop->inode.iso_links = 1; 262 } else { 263 inop->inode.iso_mode = S_IFREG; 264 inop->inode.iso_links = 1; 265 } 266 if (!bp 267 && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT) 268 && (off = isonum_711(isodir->ext_attr_length))) { 269 cd9660_bufatoff(inop, (off_t)-(off << imp->im_bshift), NULL, 270 &bp2); 271 bp = bp2; 272 } 273 if (bp) { 274 ap = (struct iso_extended_attributes *)bp->b_data; 275 276 if (isonum_711(ap->version) == 1) { 277 if (!(ap->perm[1]&0x10)) 278 inop->inode.iso_mode |= S_IRUSR; 279 if (!(ap->perm[1]&0x40)) 280 inop->inode.iso_mode |= S_IXUSR; 281 if (!(ap->perm[0]&0x01)) 282 inop->inode.iso_mode |= S_IRGRP; 283 if (!(ap->perm[0]&0x04)) 284 inop->inode.iso_mode |= S_IXGRP; 285 if (!(ap->perm[0]&0x10)) 286 inop->inode.iso_mode |= S_IROTH; 287 if (!(ap->perm[0]&0x40)) 288 inop->inode.iso_mode |= S_IXOTH; 289 inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */ 290 inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */ 291 } else 292 ap = NULL; 293 } 294 if (!ap) { 295 inop->inode.iso_mode |= 296 S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; 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(isodir,inop,bp) 309 struct iso_directory_record *isodir; 310 struct iso_node *inop; 311 struct buf *bp; 312 { 313 struct buf *bp2 = NULL; 314 struct iso_mnt *imp; 315 struct iso_extended_attributes *ap = NULL; 316 int off; 317 318 if (!bp 319 && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT) 320 && (off = isonum_711(isodir->ext_attr_length))) { 321 cd9660_bufatoff(inop, (off_t)-(off << imp->im_bshift), NULL, 322 &bp2); 323 bp = bp2; 324 } 325 if (bp) { 326 ap = (struct iso_extended_attributes *)bp->b_data; 327 328 if (isonum_711(ap->version) == 1) { 329 if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime)) 330 cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime); 331 if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime)) 332 inop->inode.iso_ctime = inop->inode.iso_atime; 333 if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime)) 334 inop->inode.iso_mtime = inop->inode.iso_ctime; 335 } else 336 ap = NULL; 337 } 338 if (!ap) { 339 cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime); 340 inop->inode.iso_atime = inop->inode.iso_ctime; 341 inop->inode.iso_mtime = inop->inode.iso_ctime; 342 } 343 if (bp2) 344 brelse(bp2); 345 } 346 347 int 348 cd9660_tstamp_conv7(pi,pu) 349 u_char *pi; 350 struct timespec *pu; 351 { 352 int crtime, days; 353 int y, m, d, hour, minute, second; 354 signed char tz; 355 356 y = pi[0] + 1900; 357 m = pi[1]; 358 d = pi[2]; 359 hour = pi[3]; 360 minute = pi[4]; 361 second = pi[5]; 362 tz = (signed char) pi[6]; 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(begin,len) 393 u_char *begin; 394 int len; 395 { 396 u_int rc; 397 398 for (rc = 0; --len >= 0;) { 399 rc *= 10; 400 rc += *begin++ - '0'; 401 } 402 return (rc); 403 } 404 405 int 406 cd9660_tstamp_conv17(pi,pu) 407 u_char *pi; 408 struct timespec *pu; 409 { 410 u_char buf[7]; 411 412 /* year:"0001"-"9999" -> -1900 */ 413 buf[0] = cd9660_chars2ui(pi,4) - 1900; 414 415 /* month: " 1"-"12" -> 1 - 12 */ 416 buf[1] = cd9660_chars2ui(pi + 4,2); 417 418 /* day: " 1"-"31" -> 1 - 31 */ 419 buf[2] = cd9660_chars2ui(pi + 6,2); 420 421 /* hour: " 0"-"23" -> 0 - 23 */ 422 buf[3] = cd9660_chars2ui(pi + 8,2); 423 424 /* minute:" 0"-"59" -> 0 - 59 */ 425 buf[4] = cd9660_chars2ui(pi + 10,2); 426 427 /* second:" 0"-"59" -> 0 - 59 */ 428 buf[5] = cd9660_chars2ui(pi + 12,2); 429 430 /* difference of GMT */ 431 buf[6] = pi[16]; 432 433 return (cd9660_tstamp_conv7(buf,pu)); 434 } 435 436 cdino_t 437 isodirino(isodir, imp) 438 struct iso_directory_record *isodir; 439 struct iso_mnt *imp; 440 { 441 cdino_t ino; 442 443 ino = (isonum_733(isodir->extent) + 444 isonum_711(isodir->ext_attr_length)) << imp->im_bshift; 445 return (ino); 446 } 447