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