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