1/* $NetBSD: cd9660_node.c,v 1.29 2011/06/12 03:35:52 rmind Exp $ */ 2 3/*- 4 * Copyright (c) 1982, 1986, 1989, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley 8 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension 9 * Support code is derived from software contributed to Berkeley 10 * by Atsushi Murai (amurai@spec.co.jp). 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)cd9660_node.c 8.8 (Berkeley) 5/22/95 37 */ 38 39#include <sys/cdefs.h> 40__KERNEL_RCSID(0, "$NetBSD: cd9660_node.c,v 1.29 2011/06/12 03:35:52 rmind Exp $"); 41 42#include <sys/param.h> 43#include <sys/systm.h> 44#include <sys/mount.h> 45#include <sys/proc.h> 46#include <sys/file.h> 47#include <sys/buf.h> 48#include <sys/vnode.h> 49#include <sys/namei.h> 50#include <sys/kernel.h> 51#include <sys/malloc.h> 52#include <sys/pool.h> 53#include <sys/stat.h> 54 55#include <fs/cd9660/iso.h> 56#include <fs/cd9660/cd9660_extern.h> 57#include <fs/cd9660/cd9660_node.h> 58#include <fs/cd9660/cd9660_mount.h> 59#include <fs/cd9660/iso_rrip.h> 60 61/* 62 * Structures associated with iso_node caching. 63 */ 64LIST_HEAD(ihashhead, iso_node) *isohashtbl; 65u_long isohash; 66#define INOHASH(device, inum) (((device) + ((inum)>>12)) & isohash) 67kmutex_t cd9660_ihash_lock; 68kmutex_t cd9660_hashlock; 69 70extern int prtactive; /* 1 => print out reclaim of active vnodes */ 71 72struct pool cd9660_node_pool; 73 74static u_int cd9660_chars2ui(const u_char *, int); 75 76/* 77 * Initialize hash links for inodes and dnodes. 78 */ 79void 80cd9660_init(void) 81{ 82 83 malloc_type_attach(M_ISOFSMNT); 84 pool_init(&cd9660_node_pool, sizeof(struct iso_node), 0, 0, 0, 85 "cd9660nopl", &pool_allocator_nointr, IPL_NONE); 86 isohashtbl = hashinit(desiredvnodes, HASH_LIST, true, &isohash); 87 mutex_init(&cd9660_ihash_lock, MUTEX_DEFAULT, IPL_NONE); 88 mutex_init(&cd9660_hashlock, MUTEX_DEFAULT, IPL_NONE); 89} 90 91/* 92 * Reinitialize inode hash table. 93 */ 94 95void 96cd9660_reinit(void) 97{ 98 struct iso_node *ip; 99 struct ihashhead *oldhash1, *hash1; 100 u_long oldmask1, mask1, val; 101 u_int i; 102 103 hash1 = hashinit(desiredvnodes, HASH_LIST, true, &mask1); 104 105 mutex_enter(&cd9660_ihash_lock); 106 oldhash1 = isohashtbl; 107 oldmask1 = isohash; 108 isohashtbl = hash1; 109 isohash = mask1; 110 for (i = 0; i <= oldmask1; i++) { 111 while ((ip = LIST_FIRST(&oldhash1[i])) != NULL) { 112 LIST_REMOVE(ip, i_hash); 113 val = INOHASH(ip->i_dev, ip->i_number); 114 LIST_INSERT_HEAD(&hash1[val], ip, i_hash); 115 } 116 } 117 mutex_exit(&cd9660_ihash_lock); 118 hashdone(oldhash1, HASH_LIST, oldmask1); 119} 120 121/* 122 * Destroy node pool and hash table. 123 */ 124void 125cd9660_done(void) 126{ 127 hashdone(isohashtbl, HASH_LIST, isohash); 128 pool_destroy(&cd9660_node_pool); 129 mutex_destroy(&cd9660_ihash_lock); 130 mutex_destroy(&cd9660_hashlock); 131 malloc_type_detach(M_ISOFSMNT); 132} 133 134/* 135 * Use the device/inum pair to find the incore inode, and return a pointer 136 * to it. If it is in core, but locked, wait for it. 137 */ 138struct vnode * 139cd9660_ihashget(dev_t dev, ino_t inum, int flags) 140{ 141 struct iso_node *ip; 142 struct vnode *vp; 143 144loop: 145 mutex_enter(&cd9660_ihash_lock); 146 LIST_FOREACH(ip, &isohashtbl[INOHASH(dev, inum)], i_hash) { 147 if (inum == ip->i_number && dev == ip->i_dev) { 148 vp = ITOV(ip); 149 if (flags == 0) { 150 mutex_exit(&cd9660_ihash_lock); 151 } else { 152 mutex_enter(vp->v_interlock); 153 mutex_exit(&cd9660_ihash_lock); 154 if (vget(vp, flags)) 155 goto loop; 156 } 157 return (vp); 158 } 159 } 160 mutex_exit(&cd9660_ihash_lock); 161 return (NULL); 162} 163 164/* 165 * Insert the inode into the hash table, and return it locked. 166 * 167 * ip->i_vnode must be initialized first. 168 */ 169void 170cd9660_ihashins(struct iso_node *ip) 171{ 172 struct ihashhead *ipp; 173 174 KASSERT(mutex_owned(&cd9660_hashlock)); 175 176 mutex_enter(&cd9660_ihash_lock); 177 ipp = &isohashtbl[INOHASH(ip->i_dev, ip->i_number)]; 178 LIST_INSERT_HEAD(ipp, ip, i_hash); 179 mutex_exit(&cd9660_ihash_lock); 180 181 VOP_LOCK(ITOV(ip), LK_EXCLUSIVE); 182} 183 184/* 185 * Remove the inode from the hash table. 186 */ 187void 188cd9660_ihashrem(struct iso_node *ip) 189{ 190 mutex_enter(&cd9660_ihash_lock); 191 LIST_REMOVE(ip, i_hash); 192 mutex_exit(&cd9660_ihash_lock); 193} 194 195/* 196 * Last reference to an inode, write the inode out and if necessary, 197 * truncate and deallocate the file. 198 */ 199int 200cd9660_inactive(void *v) 201{ 202 struct vop_inactive_args /* { 203 struct vnode *a_vp; 204 bool *a_recycle; 205 } */ *ap = v; 206 struct vnode *vp = ap->a_vp; 207 struct iso_node *ip = VTOI(vp); 208 int error = 0; 209 210 /* 211 * If we are done with the inode, reclaim it 212 * so that it can be reused immediately. 213 */ 214 ip->i_flag = 0; 215 *ap->a_recycle = (ip->inode.iso_mode == 0); 216 VOP_UNLOCK(vp); 217 return error; 218} 219 220/* 221 * Reclaim an inode so that it can be used for other purposes. 222 */ 223int 224cd9660_reclaim(void *v) 225{ 226 struct vop_reclaim_args /* { 227 struct vnode *a_vp; 228 struct lwp *a_l; 229 } */ *ap = v; 230 struct vnode *vp = ap->a_vp; 231 struct iso_node *ip = VTOI(vp); 232 233 if (prtactive && vp->v_usecount > 1) 234 vprint("cd9660_reclaim: pushing active", vp); 235 /* 236 * Remove the inode from its hash chain. 237 */ 238 cd9660_ihashrem(ip); 239 /* 240 * Purge old data structures associated with the inode. 241 */ 242 if (ip->i_devvp) { 243 vrele(ip->i_devvp); 244 ip->i_devvp = 0; 245 } 246 genfs_node_destroy(vp); 247 pool_put(&cd9660_node_pool, vp->v_data); 248 vp->v_data = NULL; 249 return (0); 250} 251 252/* 253 * File attributes 254 */ 255void 256cd9660_defattr(struct iso_directory_record *isodir, struct iso_node *inop, 257 struct buf *bp) 258{ 259 struct buf *bp2 = NULL; 260 struct iso_mnt *imp; 261 struct iso_extended_attributes *ap = NULL; 262 int off; 263 264 if (isonum_711(isodir->flags)&2) { 265 inop->inode.iso_mode = S_IFDIR; 266 /* 267 * If we return 2, fts() will assume there are no subdirectories 268 * (just links for the path and .), so instead we return 1. 269 */ 270 inop->inode.iso_links = 1; 271 } else { 272 inop->inode.iso_mode = S_IFREG; 273 inop->inode.iso_links = 1; 274 } 275 if (!bp 276 && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT) 277 && (off = isonum_711(isodir->ext_attr_length))) { 278 cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), 279 NULL, &bp2); 280 bp = bp2; 281 } 282 if (bp) { 283 ap = (struct iso_extended_attributes *)bp->b_data; 284 285 if (isonum_711(ap->version) == 1) { 286 if (!(ap->perm[1]&0x10)) 287 inop->inode.iso_mode |= S_IRUSR; 288 if (!(ap->perm[1]&0x40)) 289 inop->inode.iso_mode |= S_IXUSR; 290 if (!(ap->perm[0]&0x01)) 291 inop->inode.iso_mode |= S_IRGRP; 292 if (!(ap->perm[0]&0x04)) 293 inop->inode.iso_mode |= S_IXGRP; 294 if (!(ap->perm[0]&0x10)) 295 inop->inode.iso_mode |= S_IROTH; 296 if (!(ap->perm[0]&0x40)) 297 inop->inode.iso_mode |= S_IXOTH; 298 inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */ 299 inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */ 300 } else 301 ap = NULL; 302 } 303 if (!ap) { 304 inop->inode.iso_mode |= 305 S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; 306 inop->inode.iso_uid = (uid_t)0; 307 inop->inode.iso_gid = (gid_t)0; 308 } 309 if (bp2) 310 brelse(bp2, 0); 311} 312 313/* 314 * Time stamps 315 */ 316void 317cd9660_deftstamp(struct iso_directory_record *isodir, struct iso_node *inop, 318 struct buf *bp) 319{ 320 struct buf *bp2 = NULL; 321 struct iso_mnt *imp; 322 struct iso_extended_attributes *ap = NULL; 323 int off; 324 325 if (!bp 326 && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT) 327 && (off = isonum_711(isodir->ext_attr_length))) { 328 cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), 329 NULL, &bp2); 330 bp = bp2; 331 } 332 if (bp) { 333 ap = (struct iso_extended_attributes *)bp->b_data; 334 335 if (isonum_711(ap->version) == 1) { 336 if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime)) 337 cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime); 338 if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime)) 339 inop->inode.iso_ctime = inop->inode.iso_atime; 340 if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime)) 341 inop->inode.iso_mtime = inop->inode.iso_ctime; 342 } else 343 ap = NULL; 344 } 345 if (!ap) { 346 cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime); 347 inop->inode.iso_atime = inop->inode.iso_ctime; 348 inop->inode.iso_mtime = inop->inode.iso_ctime; 349 } 350 if (bp2) 351 brelse(bp2, 0); 352} 353 354int 355cd9660_tstamp_conv7(const u_char *pi, struct timespec *pu) 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 tz = pi[6]; 367 368 if (y < 1970) { 369 pu->tv_sec = 0; 370 pu->tv_nsec = 0; 371 return 0; 372 } else { 373#ifdef ORIGINAL 374 /* computes day number relative to Sept. 19th,1989 */ 375 /* don't even *THINK* about changing formula. It works! */ 376 days = 367*(y-1980)-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d-100; 377#else 378 /* 379 * Changed :-) to make it relative to Jan. 1st, 1970 380 * and to disambiguate negative division 381 */ 382 days = 367*(y-1960)-7*(y+(m+9)/12)/4-3*((y+(m+9)/12-1)/100+1)/4+275*m/9+d-239; 383#endif 384 crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second; 385 386 /* timezone offset is unreliable on some disks */ 387 if (-48 <= tz && tz <= 52) 388 crtime -= tz * 15 * 60; 389 } 390 pu->tv_sec = crtime; 391 pu->tv_nsec = 0; 392 return 1; 393} 394 395static u_int 396cd9660_chars2ui(const u_char *begin, int len) 397{ 398 u_int rc; 399 400 for (rc = 0; --len >= 0;) { 401 rc *= 10; 402 rc += *begin++ - '0'; 403 } 404 return rc; 405} 406 407int 408cd9660_tstamp_conv17(const u_char *pi, struct timespec *pu) 409{ 410 u_char tbuf[7]; 411 412 /* year:"0001"-"9999" -> -1900 */ 413 tbuf[0] = cd9660_chars2ui(pi,4) - 1900; 414 415 /* month: " 1"-"12" -> 1 - 12 */ 416 tbuf[1] = cd9660_chars2ui(pi + 4,2); 417 418 /* day: " 1"-"31" -> 1 - 31 */ 419 tbuf[2] = cd9660_chars2ui(pi + 6,2); 420 421 /* hour: " 0"-"23" -> 0 - 23 */ 422 tbuf[3] = cd9660_chars2ui(pi + 8,2); 423 424 /* minute:" 0"-"59" -> 0 - 59 */ 425 tbuf[4] = cd9660_chars2ui(pi + 10,2); 426 427 /* second:" 0"-"59" -> 0 - 59 */ 428 tbuf[5] = cd9660_chars2ui(pi + 12,2); 429 430 /* difference of GMT */ 431 tbuf[6] = pi[16]; 432 433 return cd9660_tstamp_conv7(tbuf,pu); 434} 435 436ino_t 437isodirino(struct iso_directory_record *isodir, struct iso_mnt *imp) 438{ 439 ino_t ino; 440 441 /* 442 * Note there is an inverse calculation in 443 * cd9660_vfsops.c:cd9660_vget_internal(): 444 * ip->iso_start = ino >> imp->im_bshift; 445 * and also a calculation of the isodir pointer 446 * from an inode in cd9660_vnops.c:cd9660_readlink() 447 */ 448 ino = ((ino_t)isonum_733(isodir->extent) + 449 isonum_711(isodir->ext_attr_length)) << imp->im_bshift; 450 return ino; 451} 452