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