1 /*- 2 * Copyright (c) 1982, 1986, 1989, 1994 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 * %sccs.include.redist.c% 11 * 12 * @(#)cd9660_node.c 8.5 (Berkeley) 12/05/94 13 */ 14 15 #include <sys/param.h> 16 #include <sys/systm.h> 17 #include <sys/mount.h> 18 #include <sys/proc.h> 19 #include <sys/file.h> 20 #include <sys/buf.h> 21 #include <sys/vnode.h> 22 #include <sys/kernel.h> 23 #include <sys/malloc.h> 24 #include <sys/stat.h> 25 26 #include <isofs/cd9660/iso.h> 27 #include <isofs/cd9660/cd9660_node.h> 28 #include <isofs/cd9660/iso_rrip.h> 29 30 /* 31 * Structures associated with iso_node caching. 32 */ 33 struct iso_node **isohashtbl; 34 u_long isohash; 35 #define INOHASH(device, inum) (((device) + ((inum)>>12)) & isohash) 36 37 #ifdef ISODEVMAP 38 struct iso_node **idvhashtbl; 39 u_long idvhash; 40 #define DNOHASH(device, inum) (((device) + ((inum)>>12)) & idvhash) 41 #endif 42 43 int prtactive; /* 1 => print out reclaim of active vnodes */ 44 45 /* 46 * Initialize hash links for inodes and dnodes. 47 */ 48 cd9660_init() 49 { 50 51 isohashtbl = hashinit(desiredvnodes, M_ISOFSMNT, &isohash); 52 #ifdef ISODEVMAP 53 idvhashtbl = hashinit(desiredvnodes / 8, M_ISOFSMNT, &idvhash); 54 #endif 55 } 56 57 #ifdef ISODEVMAP 58 /* 59 * Enter a new node into the device hash list 60 */ 61 struct iso_dnode * 62 iso_dmap(device, inum, create) 63 dev_t device; 64 ino_t inum; 65 int create; 66 { 67 register struct iso_dnode **dpp, *dp, *dq; 68 69 dpp = &idvhashtbl[DNOHASH(device, inum)]; 70 for (dp = *dpp;; dp = dp->d_next) { 71 if (dp == NULL) 72 return (NULL); 73 if (inum == dp->i_number && device == dp->i_dev) 74 return (dp); 75 76 if (!create) 77 return (NULL); 78 79 MALLOC(dp, struct iso_dnode *, sizeof(struct iso_dnode), M_CACHE, 80 M_WAITOK); 81 dp->i_dev = dev; 82 dp->i_number = ino; 83 84 if (dq = *dpp) 85 dq->d_prev = dp->d_next; 86 dp->d_next = dq; 87 dp->d_prev = dpp; 88 *dpp = dp; 89 90 return (dp); 91 } 92 93 void 94 iso_dunmap(device) 95 dev_t device; 96 { 97 struct iso_dnode **dpp, *dp, *dq; 98 99 for (dpp = idvhashtbl; dpp <= idvhashtbl + idvhash; dpp++) { 100 for (dp = *dpp; dp != NULL; dp = dq) 101 dq = dp->d_next; 102 if (device == dp->i_dev) { 103 if (dq) 104 dq->d_prev = dp->d_prev; 105 *dp->d_prev = dq; 106 FREE(dp, M_CACHE); 107 } 108 } 109 } 110 } 111 #endif 112 113 /* 114 * Use the device/inum pair to find the incore inode, and return a pointer 115 * to it. If it is in core, but locked, wait for it. 116 */ 117 struct vnode * 118 cd9660_ihashget(device, inum) 119 dev_t device; 120 ino_t inum; 121 { 122 register struct iso_node *ip; 123 struct vnode *vp; 124 125 for (;;) 126 for (ip = isohashtbl[INOHASH(device, inum)];; ip = ip->i_next) { 127 if (ip == NULL) 128 return (NULL); 129 if (inum == ip->i_number && device == ip->i_dev) { 130 if (ip->i_flag & IN_LOCKED) { 131 ip->i_flag |= IN_WANTED; 132 sleep(ip, PINOD); 133 break; 134 } 135 vp = ITOV(ip); 136 if (!vget(vp, 1)) 137 return (vp); 138 break; 139 } 140 } 141 /* NOTREACHED */ 142 } 143 144 /* 145 * Insert the inode into the hash table, and return it locked. 146 */ 147 void 148 cd9660_ihashins(ip) 149 struct iso_node *ip; 150 { 151 struct iso_node **ipp, *iq; 152 153 ipp = &isohashtbl[INOHASH(ip->i_dev, ip->i_number)]; 154 if (iq = *ipp) 155 iq->i_prev = &ip->i_next; 156 ip->i_next = iq; 157 ip->i_prev = ipp; 158 *ipp = ip; 159 if (ip->i_flag & IN_LOCKED) 160 panic("cd9660_ihashins: already locked"); 161 if (curproc) 162 ip->i_lockholder = curproc->p_pid; 163 else 164 ip->i_lockholder = -1; 165 ip->i_flag |= IN_LOCKED; 166 } 167 168 /* 169 * Remove the inode from the hash table. 170 */ 171 void 172 cd9660_ihashrem(ip) 173 register struct iso_node *ip; 174 { 175 register struct iso_node *iq; 176 177 if (iq = ip->i_next) 178 iq->i_prev = ip->i_prev; 179 *ip->i_prev = iq; 180 #ifdef DIAGNOSTIC 181 ip->i_next = NULL; 182 ip->i_prev = NULL; 183 #endif 184 } 185 186 /* 187 * Last reference to an inode, write the inode out and if necessary, 188 * truncate and deallocate the file. 189 */ 190 int 191 cd9660_inactive(ap) 192 struct vop_inactive_args /* { 193 struct vnode *a_vp; 194 } */ *ap; 195 { 196 struct vnode *vp = ap->a_vp; 197 register struct iso_node *ip = VTOI(vp); 198 int mode, error = 0; 199 200 if (prtactive && vp->v_usecount != 0) 201 vprint("cd9660_inactive: pushing active", vp); 202 203 ip->i_flag = 0; 204 /* 205 * If we are done with the inode, reclaim it 206 * so that it can be reused immediately. 207 */ 208 if (vp->v_usecount == 0 && ip->inode.iso_mode == 0) 209 vgone(vp); 210 return error; 211 } 212 213 /* 214 * Reclaim an inode so that it can be used for other purposes. 215 */ 216 int 217 cd9660_reclaim(ap) 218 struct vop_reclaim_args /* { 219 struct vnode *a_vp; 220 } */ *ap; 221 { 222 register struct vnode *vp = ap->a_vp; 223 register struct iso_node *ip = VTOI(vp); 224 int i; 225 226 if (prtactive && vp->v_usecount != 0) 227 vprint("cd9660_reclaim: pushing active", vp); 228 /* 229 * Remove the inode from its hash chain. 230 */ 231 cd9660_ihashrem(ip); 232 /* 233 * Purge old data structures associated with the inode. 234 */ 235 cache_purge(vp); 236 if (ip->i_devvp) { 237 vrele(ip->i_devvp); 238 ip->i_devvp = 0; 239 } 240 FREE(vp->v_data, M_ISOFSNODE); 241 vp->v_data = NULL; 242 return (0); 243 } 244 245 /* 246 * File attributes 247 */ 248 void 249 cd9660_defattr(isodir, inop, bp) 250 struct iso_directory_record *isodir; 251 struct iso_node *inop; 252 struct buf *bp; 253 { 254 struct buf *bp2 = NULL; 255 struct iso_mnt *imp; 256 struct iso_extended_attributes *ap = NULL; 257 int off; 258 259 if (isonum_711(isodir->flags)&2) { 260 inop->inode.iso_mode = S_IFDIR; 261 /* 262 * If we return 2, fts() will assume there are no subdirectories 263 * (just links for the path and .), so instead we return 1. 264 */ 265 inop->inode.iso_links = 1; 266 } else { 267 inop->inode.iso_mode = S_IFREG; 268 inop->inode.iso_links = 1; 269 } 270 if (!bp 271 && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT) 272 && (off = isonum_711(isodir->ext_attr_length))) { 273 VOP_BLKATOFF(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL, 274 &bp2); 275 bp = bp2; 276 } 277 if (bp) { 278 ap = (struct iso_extended_attributes *)bp->b_data; 279 280 if (isonum_711(ap->version) == 1) { 281 if (!(ap->perm[0]&0x40)) 282 inop->inode.iso_mode |= VEXEC >> 6; 283 if (!(ap->perm[0]&0x10)) 284 inop->inode.iso_mode |= VREAD >> 6; 285 if (!(ap->perm[0]&4)) 286 inop->inode.iso_mode |= VEXEC >> 3; 287 if (!(ap->perm[0]&1)) 288 inop->inode.iso_mode |= VREAD >> 3; 289 if (!(ap->perm[1]&0x40)) 290 inop->inode.iso_mode |= VEXEC; 291 if (!(ap->perm[1]&0x10)) 292 inop->inode.iso_mode |= VREAD; 293 inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */ 294 inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */ 295 } else 296 ap = NULL; 297 } 298 if (!ap) { 299 inop->inode.iso_mode |= VREAD|VEXEC|(VREAD|VEXEC)>>3|(VREAD|VEXEC)>>6; 300 inop->inode.iso_uid = (uid_t)0; 301 inop->inode.iso_gid = (gid_t)0; 302 } 303 if (bp2) 304 brelse(bp2); 305 } 306 307 /* 308 * Time stamps 309 */ 310 void 311 cd9660_deftstamp(isodir,inop,bp) 312 struct iso_directory_record *isodir; 313 struct iso_node *inop; 314 struct buf *bp; 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 VOP_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 (isonum_711(ap->version) == 1) { 332 if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime)) 333 cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime); 334 if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime)) 335 inop->inode.iso_ctime = inop->inode.iso_atime; 336 if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime)) 337 inop->inode.iso_mtime = inop->inode.iso_ctime; 338 } else 339 ap = NULL; 340 } 341 if (!ap) { 342 cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime); 343 inop->inode.iso_atime = inop->inode.iso_ctime; 344 inop->inode.iso_mtime = inop->inode.iso_ctime; 345 } 346 if (bp2) 347 brelse(bp2); 348 } 349 350 int 351 cd9660_tstamp_conv7(pi,pu) 352 u_char *pi; 353 struct timespec *pu; 354 { 355 int i; 356 int crtime, days; 357 int y, m, d, hour, minute, second, tz; 358 359 y = pi[0] + 1900; 360 m = pi[1]; 361 d = pi[2]; 362 hour = pi[3]; 363 minute = pi[4]; 364 second = pi[5]; 365 tz = pi[6]; 366 367 if (y < 1970) { 368 pu->ts_sec = 0; 369 pu->ts_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->ts_sec = crtime; 390 pu->ts_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); 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