Home | History | Annotate | Line # | Download | only in cd9660
cd9660_node.c revision 1.4
      1 /*	$NetBSD: cd9660_node.c,v 1.4 2003/06/29 22:31:08 fvdl 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 (at) blitz.com).  The Rock Ridge Extension
      9  * Support code is derived from software contributed to Berkeley
     10  * by Atsushi Murai (amurai (at) 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. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)cd9660_node.c	8.8 (Berkeley) 5/22/95
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: cd9660_node.c,v 1.4 2003/06/29 22:31:08 fvdl Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/mount.h>
     49 #include <sys/proc.h>
     50 #include <sys/file.h>
     51 #include <sys/buf.h>
     52 #include <sys/vnode.h>
     53 #include <sys/namei.h>
     54 #include <sys/kernel.h>
     55 #include <sys/malloc.h>
     56 #include <sys/pool.h>
     57 #include <sys/stat.h>
     58 
     59 #include <fs/cd9660/iso.h>
     60 #include <fs/cd9660/cd9660_extern.h>
     61 #include <fs/cd9660/cd9660_node.h>
     62 #include <fs/cd9660/cd9660_mount.h>
     63 #include <fs/cd9660/iso_rrip.h>
     64 
     65 /*
     66  * Structures associated with iso_node caching.
     67  */
     68 LIST_HEAD(ihashhead, iso_node) *isohashtbl;
     69 u_long isohash;
     70 #define	INOHASH(device, inum)	(((device) + ((inum)>>12)) & isohash)
     71 struct simplelock cd9660_ihash_slock;
     72 
     73 #ifdef ISODEVMAP
     74 LIST_HEAD(idvhashhead, iso_dnode) *idvhashtbl;
     75 u_long idvhash;
     76 #define	DNOHASH(device, inum)	(((device) + ((inum)>>12)) & idvhash)
     77 #endif
     78 
     79 extern int prtactive;	/* 1 => print out reclaim of active vnodes */
     80 
     81 struct pool cd9660_node_pool;
     82 
     83 static u_int cd9660_chars2ui __P((u_char *, int));
     84 
     85 /*
     86  * Initialize hash links for inodes and dnodes.
     87  */
     88 void
     89 cd9660_init()
     90 {
     91 	isohashtbl = hashinit(desiredvnodes, HASH_LIST, M_ISOFSMNT, M_WAITOK,
     92 	    &isohash);
     93 	simple_lock_init(&cd9660_ihash_slock);
     94 #ifdef ISODEVMAP
     95 	idvhashtbl = hashinit(desiredvnodes / 8, HASH_LIST, M_ISOFSMNT,
     96 	    M_WAITOK, &idvhash);
     97 #endif
     98 	pool_init(&cd9660_node_pool, sizeof(struct iso_node), 0, 0, 0,
     99 	    "cd9660nopl", &pool_allocator_nointr);
    100 }
    101 
    102 /*
    103  * Reinitialize inode hash table.
    104  */
    105 
    106 void
    107 cd9660_reinit()
    108 {
    109 	struct iso_node *ip;
    110 	struct ihashhead *oldhash1, *hash1;
    111 	u_long oldmask1, mask1, val;
    112 #ifdef ISODEVMAP
    113 	struct iso_dnode *dp;
    114 	struct idvhashhead *oldhash2, *hash2;
    115 	u_long oldmask2, mask2;
    116 #endif
    117 	u_int i;
    118 
    119 	hash1 = hashinit(desiredvnodes, HASH_LIST, M_ISOFSMNT, M_WAITOK,
    120 	    &mask1);
    121 #ifdef ISODEVMAP
    122 	hash2 = hashinit(desiredvnodes / 8, HASH_LIST, M_ISOFSMNT, M_WAITOK,
    123 	    &mask2);
    124 #endif
    125 
    126 	simple_lock(&cd9660_ihash_slock);
    127 	oldhash1 = isohashtbl;
    128 	oldmask1 = isohash;
    129 	isohashtbl = hash1;
    130 	isohash = mask1;
    131 #ifdef ISODEVMAP
    132 	oldhash2 = idvhashtbl;
    133 	oldmask2 = idvhash;
    134 	idvhashtbl = hash2;
    135 	idvhash = mask2;
    136 	for (i = 0; i <= oldmask2; i++) {
    137 		while ((dp = LIST_FIRST(&oldhash2[i])) != NULL) {
    138 			LIST_REMOVE(dp, d_hash);
    139 			val = DNOHASH(dp->i_dev, dp->i_number);
    140 			LIST_INSERT_HEAD(&hash2[val], dp, d_hash);
    141 		}
    142 	}
    143 #endif
    144 	for (i = 0; i <= oldmask1; i++) {
    145 		while ((ip = LIST_FIRST(&oldhash1[i])) != NULL) {
    146 			LIST_REMOVE(ip, i_hash);
    147 			val = INOHASH(ip->i_dev, ip->i_number);
    148 			LIST_INSERT_HEAD(&hash1[val], ip, i_hash);
    149 		}
    150 	}
    151 	simple_unlock(&cd9660_ihash_slock);
    152 	hashdone(oldhash1, M_ISOFSMNT);
    153 #ifdef ISODEVMAP
    154 	hashdone(oldhash2, M_ISOFSMNT);
    155 #endif
    156 }
    157 
    158 /*
    159  * Destroy node pool and hash table.
    160  */
    161 void
    162 cd9660_done()
    163 {
    164 	hashdone(isohashtbl, M_ISOFSMNT);
    165 #ifdef ISODEVMAP
    166 	hashdone(idvhashtbl, M_ISOFSMNT);
    167 #endif
    168 	pool_destroy(&cd9660_node_pool);
    169 }
    170 
    171 #ifdef ISODEVMAP
    172 /*
    173  * Enter a new node into the device hash list
    174  */
    175 struct iso_dnode *
    176 iso_dmap(device, inum, create)
    177 	dev_t	device;
    178 	ino_t	inum;
    179 	int	create;
    180 {
    181 	struct iso_dnode *dp;
    182 	struct idvhashhead *hp;
    183 
    184 	hp = &idvhashtbl[DNOHASH(device, inum)];
    185 	LIST_FOREACH(dp, hp, d_hash) {
    186 		if (inum == dp->i_number && device == dp->i_dev)
    187 			return (dp);
    188 	}
    189 
    190 	if (!create)
    191 		return (NULL);
    192 
    193 	MALLOC(dp, struct iso_dnode *, sizeof(struct iso_dnode), M_CACHE,
    194 	       M_WAITOK);
    195 	dp->i_dev = device;
    196 	dp->i_number = inum;
    197 	LIST_INSERT_HEAD(hp, dp, d_hash);
    198 	return (dp);
    199 }
    200 
    201 void
    202 iso_dunmap(device)
    203 	dev_t device;
    204 {
    205 	struct idvhashhead *dpp;
    206 	struct iso_dnode *dp, *dq;
    207 
    208 	for (dpp = idvhashtbl; dpp <= idvhashtbl + idvhash; dpp++) {
    209 		for (dp = LIST_FIRST(dpp); dp != NULL; dp = dq) {
    210 			dq = LIST_NEXT(dp, d_hash);
    211 			if (device == dp->i_dev) {
    212 				LIST_REMOVE(dp, d_hash);
    213 				FREE(dp, M_CACHE);
    214 			}
    215 		}
    216 	}
    217 }
    218 #endif
    219 
    220 /*
    221  * Use the device/inum pair to find the incore inode, and return a pointer
    222  * to it. If it is in core, but locked, wait for it.
    223  */
    224 struct vnode *
    225 cd9660_ihashget(dev, inum)
    226 	dev_t dev;
    227 	ino_t inum;
    228 {
    229 	struct iso_node *ip;
    230 	struct vnode *vp;
    231 
    232 loop:
    233 	simple_lock(&cd9660_ihash_slock);
    234 	LIST_FOREACH(ip, &isohashtbl[INOHASH(dev, inum)], i_hash) {
    235 		if (inum == ip->i_number && dev == ip->i_dev) {
    236 			vp = ITOV(ip);
    237 			simple_lock(&vp->v_interlock);
    238 			simple_unlock(&cd9660_ihash_slock);
    239 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    240 				goto loop;
    241 			return (vp);
    242 		}
    243 	}
    244 	simple_unlock(&cd9660_ihash_slock);
    245 	return (NULL);
    246 }
    247 
    248 /*
    249  * Insert the inode into the hash table, and return it locked.
    250  *
    251  * ip->i_vnode must be initialized first.
    252  */
    253 void
    254 cd9660_ihashins(ip)
    255 	struct iso_node *ip;
    256 {
    257 	struct ihashhead *ipp;
    258 
    259 	simple_lock(&cd9660_ihash_slock);
    260 	ipp = &isohashtbl[INOHASH(ip->i_dev, ip->i_number)];
    261 	LIST_INSERT_HEAD(ipp, ip, i_hash);
    262 	simple_unlock(&cd9660_ihash_slock);
    263 
    264 	lockmgr(&ip->i_vnode->v_lock, LK_EXCLUSIVE, &ip->i_vnode->v_interlock);
    265 }
    266 
    267 /*
    268  * Remove the inode from the hash table.
    269  */
    270 void
    271 cd9660_ihashrem(ip)
    272 	struct iso_node *ip;
    273 {
    274 	simple_lock(&cd9660_ihash_slock);
    275 	LIST_REMOVE(ip, i_hash);
    276 	simple_unlock(&cd9660_ihash_slock);
    277 }
    278 
    279 /*
    280  * Last reference to an inode, write the inode out and if necessary,
    281  * truncate and deallocate the file.
    282  */
    283 int
    284 cd9660_inactive(v)
    285 	void *v;
    286 {
    287 	struct vop_inactive_args /* {
    288 		struct vnode *a_vp;
    289 		struct proc *a_p;
    290 	} */ *ap = v;
    291 	struct vnode *vp = ap->a_vp;
    292 	struct proc *p = ap->a_p;
    293 	struct iso_node *ip = VTOI(vp);
    294 	int error = 0;
    295 
    296 	if (prtactive && vp->v_usecount != 0)
    297 		vprint("cd9660_inactive: pushing active", vp);
    298 
    299 	ip->i_flag = 0;
    300 	VOP_UNLOCK(vp, 0);
    301 	/*
    302 	 * If we are done with the inode, reclaim it
    303 	 * so that it can be reused immediately.
    304 	 */
    305 	if (ip->inode.iso_mode == 0)
    306 		vrecycle(vp, (struct simplelock *)0, p);
    307 	return error;
    308 }
    309 
    310 /*
    311  * Reclaim an inode so that it can be used for other purposes.
    312  */
    313 int
    314 cd9660_reclaim(v)
    315 	void *v;
    316 {
    317 	struct vop_reclaim_args /* {
    318 		struct vnode *a_vp;
    319 		struct proc *a_p;
    320 	} */ *ap = v;
    321 	struct vnode *vp = ap->a_vp;
    322 	struct iso_node *ip = VTOI(vp);
    323 
    324 	if (prtactive && vp->v_usecount != 0)
    325 		vprint("cd9660_reclaim: pushing active", vp);
    326 	/*
    327 	 * Remove the inode from its hash chain.
    328 	 */
    329 	cd9660_ihashrem(ip);
    330 	/*
    331 	 * Purge old data structures associated with the inode.
    332 	 */
    333 	cache_purge(vp);
    334 	if (ip->i_devvp) {
    335 		vrele(ip->i_devvp);
    336 		ip->i_devvp = 0;
    337 	}
    338 	pool_put(&cd9660_node_pool, vp->v_data);
    339 	vp->v_data = NULL;
    340 	return (0);
    341 }
    342 
    343 /*
    344  * File attributes
    345  */
    346 void
    347 cd9660_defattr(isodir, inop, bp)
    348 	struct iso_directory_record *isodir;
    349 	struct iso_node *inop;
    350 	struct buf *bp;
    351 {
    352 	struct buf *bp2 = NULL;
    353 	struct iso_mnt *imp;
    354 	struct iso_extended_attributes *ap = NULL;
    355 	int off;
    356 
    357 	if (isonum_711(isodir->flags)&2) {
    358 		inop->inode.iso_mode = S_IFDIR;
    359 		/*
    360 		 * If we return 2, fts() will assume there are no subdirectories
    361 		 * (just links for the path and .), so instead we return 1.
    362 		 */
    363 		inop->inode.iso_links = 1;
    364 	} else {
    365 		inop->inode.iso_mode = S_IFREG;
    366 		inop->inode.iso_links = 1;
    367 	}
    368 	if (!bp
    369 	    && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
    370 	    && (off = isonum_711(isodir->ext_attr_length))) {
    371 		VOP_BLKATOFF(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL,
    372 			     &bp2);
    373 		bp = bp2;
    374 	}
    375 	if (bp) {
    376 		ap = (struct iso_extended_attributes *)bp->b_data;
    377 
    378 		if (isonum_711(ap->version) == 1) {
    379 			if (!(ap->perm[1]&0x10))
    380 				inop->inode.iso_mode |= S_IRUSR;
    381 			if (!(ap->perm[1]&0x40))
    382 				inop->inode.iso_mode |= S_IXUSR;
    383 			if (!(ap->perm[0]&0x01))
    384 				inop->inode.iso_mode |= S_IRGRP;
    385 			if (!(ap->perm[0]&0x04))
    386 				inop->inode.iso_mode |= S_IXGRP;
    387 			if (!(ap->perm[0]&0x10))
    388 				inop->inode.iso_mode |= S_IROTH;
    389 			if (!(ap->perm[0]&0x40))
    390 				inop->inode.iso_mode |= S_IXOTH;
    391 			inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */
    392 			inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */
    393 		} else
    394 			ap = NULL;
    395 	}
    396 	if (!ap) {
    397 		inop->inode.iso_mode |=
    398 		    S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    399 		inop->inode.iso_uid = (uid_t)0;
    400 		inop->inode.iso_gid = (gid_t)0;
    401 	}
    402 	if (bp2)
    403 		brelse(bp2);
    404 }
    405 
    406 /*
    407  * Time stamps
    408  */
    409 void
    410 cd9660_deftstamp(isodir,inop,bp)
    411 	struct iso_directory_record *isodir;
    412 	struct iso_node *inop;
    413 	struct buf *bp;
    414 {
    415 	struct buf *bp2 = NULL;
    416 	struct iso_mnt *imp;
    417 	struct iso_extended_attributes *ap = NULL;
    418 	int off;
    419 
    420 	if (!bp
    421 	    && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
    422 	    && (off = isonum_711(isodir->ext_attr_length))) {
    423 		VOP_BLKATOFF(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL,
    424 			     &bp2);
    425 		bp = bp2;
    426 	}
    427 	if (bp) {
    428 		ap = (struct iso_extended_attributes *)bp->b_data;
    429 
    430 		if (isonum_711(ap->version) == 1) {
    431 			if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime))
    432 				cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime);
    433 			if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime))
    434 				inop->inode.iso_ctime = inop->inode.iso_atime;
    435 			if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime))
    436 				inop->inode.iso_mtime = inop->inode.iso_ctime;
    437 		} else
    438 			ap = NULL;
    439 	}
    440 	if (!ap) {
    441 		cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime);
    442 		inop->inode.iso_atime = inop->inode.iso_ctime;
    443 		inop->inode.iso_mtime = inop->inode.iso_ctime;
    444 	}
    445 	if (bp2)
    446 		brelse(bp2);
    447 }
    448 
    449 int
    450 cd9660_tstamp_conv7(pi,pu)
    451 	u_char *pi;
    452 	struct timespec *pu;
    453 {
    454 	int crtime, days;
    455 	int y, m, d, hour, minute, second, tz;
    456 
    457 	y = pi[0] + 1900;
    458 	m = pi[1];
    459 	d = pi[2];
    460 	hour = pi[3];
    461 	minute = pi[4];
    462 	second = pi[5];
    463 	tz = pi[6];
    464 
    465 	if (y < 1970) {
    466 		pu->tv_sec  = 0;
    467 		pu->tv_nsec = 0;
    468 		return 0;
    469 	} else {
    470 #ifdef	ORIGINAL
    471 		/* computes day number relative to Sept. 19th,1989 */
    472 		/* don't even *THINK* about changing formula. It works! */
    473 		days = 367*(y-1980)-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d-100;
    474 #else
    475 		/*
    476 		 * Changed :-) to make it relative to Jan. 1st, 1970
    477 		 * and to disambiguate negative division
    478 		 */
    479 		days = 367*(y-1960)-7*(y+(m+9)/12)/4-3*((y+(m+9)/12-1)/100+1)/4+275*m/9+d-239;
    480 #endif
    481 		crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second;
    482 
    483 		/* timezone offset is unreliable on some disks */
    484 		if (-48 <= tz && tz <= 52)
    485 			crtime -= tz * 15 * 60;
    486 	}
    487 	pu->tv_sec  = crtime;
    488 	pu->tv_nsec = 0;
    489 	return 1;
    490 }
    491 
    492 static u_int
    493 cd9660_chars2ui(begin,len)
    494 	u_char *begin;
    495 	int len;
    496 {
    497 	u_int rc;
    498 
    499 	for (rc = 0; --len >= 0;) {
    500 		rc *= 10;
    501 		rc += *begin++ - '0';
    502 	}
    503 	return rc;
    504 }
    505 
    506 int
    507 cd9660_tstamp_conv17(pi,pu)
    508 	u_char *pi;
    509 	struct timespec *pu;
    510 {
    511 	u_char buf[7];
    512 
    513 	/* year:"0001"-"9999" -> -1900  */
    514 	buf[0] = cd9660_chars2ui(pi,4) - 1900;
    515 
    516 	/* month: " 1"-"12"      -> 1 - 12 */
    517 	buf[1] = cd9660_chars2ui(pi + 4,2);
    518 
    519 	/* day:   " 1"-"31"      -> 1 - 31 */
    520 	buf[2] = cd9660_chars2ui(pi + 6,2);
    521 
    522 	/* hour:  " 0"-"23"      -> 0 - 23 */
    523 	buf[3] = cd9660_chars2ui(pi + 8,2);
    524 
    525 	/* minute:" 0"-"59"      -> 0 - 59 */
    526 	buf[4] = cd9660_chars2ui(pi + 10,2);
    527 
    528 	/* second:" 0"-"59"      -> 0 - 59 */
    529 	buf[5] = cd9660_chars2ui(pi + 12,2);
    530 
    531 	/* difference of GMT */
    532 	buf[6] = pi[16];
    533 
    534 	return cd9660_tstamp_conv7(buf,pu);
    535 }
    536 
    537 ino_t
    538 isodirino(isodir, imp)
    539 	struct iso_directory_record *isodir;
    540 	struct iso_mnt *imp;
    541 {
    542 	ino_t ino;
    543 
    544 	ino = (isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length))
    545 	      << imp->im_bshift;
    546 	return (ino);
    547 }
    548