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