Home | History | Annotate | Line # | Download | only in cd9660
cd9660_node.c revision 1.37
      1 /*	$NetBSD: cd9660_node.c,v 1.37 2017/05/26 14:21:00 riastradh 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.37 2017/05/26 14:21:00 riastradh 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/pool.h>
     52 #include <sys/stat.h>
     53 
     54 #include <fs/cd9660/iso.h>
     55 #include <fs/cd9660/cd9660_extern.h>
     56 #include <fs/cd9660/cd9660_node.h>
     57 #include <fs/cd9660/cd9660_mount.h>
     58 #include <fs/cd9660/iso_rrip.h>
     59 
     60 extern int prtactive;	/* 1 => print out reclaim of active vnodes */
     61 
     62 struct pool cd9660_node_pool;
     63 
     64 static u_int cd9660_chars2ui(const u_char *, int);
     65 
     66 /*
     67  * Initialize pool for nodes.
     68  */
     69 void
     70 cd9660_init(void)
     71 {
     72 
     73 	malloc_type_attach(M_ISOFSMNT);
     74 	pool_init(&cd9660_node_pool, sizeof(struct iso_node), 0, 0, 0,
     75 	    "cd9660nopl", &pool_allocator_nointr, IPL_NONE);
     76 }
     77 
     78 /*
     79  * Reinitialize.
     80  */
     81 
     82 void
     83 cd9660_reinit(void)
     84 {
     85 
     86 }
     87 
     88 /*
     89  * Destroy node pool.
     90  */
     91 void
     92 cd9660_done(void)
     93 {
     94 	pool_destroy(&cd9660_node_pool);
     95 	malloc_type_detach(M_ISOFSMNT);
     96 }
     97 
     98 /*
     99  * Last reference to an inode, write the inode out and if necessary,
    100  * truncate and deallocate the file.
    101  */
    102 int
    103 cd9660_inactive(void *v)
    104 {
    105 	struct vop_inactive_v2_args /* {
    106 		struct vnode *a_vp;
    107 		bool *a_recycle;
    108 	} */ *ap = v;
    109 	struct vnode *vp = ap->a_vp;
    110 	struct iso_node *ip = VTOI(vp);
    111 	int error = 0;
    112 
    113 	/*
    114 	 * If we are done with the inode, reclaim it
    115 	 * so that it can be reused immediately.
    116 	 */
    117 	ip->i_flag = 0;
    118 	*ap->a_recycle = (ip->inode.iso_mode == 0);
    119 	return error;
    120 }
    121 
    122 /*
    123  * Reclaim an inode so that it can be used for other purposes.
    124  */
    125 int
    126 cd9660_reclaim(void *v)
    127 {
    128 	struct vop_reclaim_v2_args /* {
    129 		struct vnode *a_vp;
    130 		struct lwp *a_l;
    131 	} */ *ap = v;
    132 	struct vnode *vp = ap->a_vp;
    133 
    134 	VOP_UNLOCK(vp);
    135 
    136 	if (prtactive && vp->v_usecount > 1)
    137 		vprint("cd9660_reclaim: pushing active", vp);
    138 	/*
    139 	 * Purge old data structures associated with the inode.
    140 	 */
    141 	genfs_node_destroy(vp);
    142 	pool_put(&cd9660_node_pool, vp->v_data);
    143 	vp->v_data = NULL;
    144 	return (0);
    145 }
    146 
    147 /*
    148  * File attributes
    149  */
    150 void
    151 cd9660_defattr(struct iso_directory_record *isodir, struct iso_node *inop,
    152 	struct buf *bp)
    153 {
    154 	struct buf *bp2 = NULL;
    155 	struct iso_mnt *imp;
    156 	struct iso_extended_attributes *ap = NULL;
    157 	int off;
    158 
    159 	if (isonum_711(isodir->flags)&2) {
    160 		inop->inode.iso_mode = S_IFDIR;
    161 		/*
    162 		 * If we return 2, fts() will assume there are no subdirectories
    163 		 * (just links for the path and .), so instead we return 1.
    164 		 */
    165 		inop->inode.iso_links = 1;
    166 	} else {
    167 		inop->inode.iso_mode = S_IFREG;
    168 		inop->inode.iso_links = 1;
    169 	}
    170 	if (!bp
    171 	    && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
    172 	    && (off = isonum_711(isodir->ext_attr_length))) {
    173 		cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift),
    174 		    NULL, &bp2);
    175 		bp = bp2;
    176 	}
    177 	if (bp) {
    178 		ap = (struct iso_extended_attributes *)bp->b_data;
    179 
    180 		if (isonum_711(ap->version) == 1) {
    181 			if (!(ap->perm[1]&0x10))
    182 				inop->inode.iso_mode |= S_IRUSR;
    183 			if (!(ap->perm[1]&0x40))
    184 				inop->inode.iso_mode |= S_IXUSR;
    185 			if (!(ap->perm[0]&0x01))
    186 				inop->inode.iso_mode |= S_IRGRP;
    187 			if (!(ap->perm[0]&0x04))
    188 				inop->inode.iso_mode |= S_IXGRP;
    189 			if (!(ap->perm[0]&0x10))
    190 				inop->inode.iso_mode |= S_IROTH;
    191 			if (!(ap->perm[0]&0x40))
    192 				inop->inode.iso_mode |= S_IXOTH;
    193 			inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */
    194 			inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */
    195 		} else
    196 			ap = NULL;
    197 	}
    198 	if (!ap) {
    199 		inop->inode.iso_mode |=
    200 		    S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    201 		inop->inode.iso_uid = (uid_t)0;
    202 		inop->inode.iso_gid = (gid_t)0;
    203 	}
    204 	if (bp2)
    205 		brelse(bp2, 0);
    206 }
    207 
    208 /*
    209  * Time stamps
    210  */
    211 void
    212 cd9660_deftstamp(struct iso_directory_record *isodir, struct iso_node *inop,
    213 	struct buf *bp)
    214 {
    215 	struct buf *bp2 = NULL;
    216 	struct iso_mnt *imp;
    217 	struct iso_extended_attributes *ap = NULL;
    218 	int off;
    219 
    220 	if (!bp
    221 	    && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
    222 	    && (off = isonum_711(isodir->ext_attr_length))) {
    223 		cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift),
    224 		    NULL, &bp2);
    225 		bp = bp2;
    226 	}
    227 	if (bp) {
    228 		ap = (struct iso_extended_attributes *)bp->b_data;
    229 
    230 		if (isonum_711(ap->version) == 1) {
    231 			if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime))
    232 				cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime);
    233 			if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime))
    234 				inop->inode.iso_ctime = inop->inode.iso_atime;
    235 			if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime))
    236 				inop->inode.iso_mtime = inop->inode.iso_ctime;
    237 		} else
    238 			ap = NULL;
    239 	}
    240 	if (!ap) {
    241 		cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime);
    242 		inop->inode.iso_atime = inop->inode.iso_ctime;
    243 		inop->inode.iso_mtime = inop->inode.iso_ctime;
    244 	}
    245 	if (bp2)
    246 		brelse(bp2, 0);
    247 }
    248 
    249 int
    250 cd9660_tstamp_conv7(const u_char *pi, struct timespec *pu)
    251 {
    252 	int crtime, days;
    253 	int y, m, d, hour, minute, second, tz;
    254 
    255 	y = pi[0] + 1900;
    256 	m = pi[1];
    257 	d = pi[2];
    258 	hour = pi[3];
    259 	minute = pi[4];
    260 	second = pi[5];
    261 	tz = pi[6];
    262 
    263 	if (y < 1970) {
    264 		pu->tv_sec  = 0;
    265 		pu->tv_nsec = 0;
    266 		return 0;
    267 	} else {
    268 #ifdef	ORIGINAL
    269 		/* computes day number relative to Sept. 19th,1989 */
    270 		/* don't even *THINK* about changing formula. It works! */
    271 		days = 367*(y-1980)-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d-100;
    272 #else
    273 		/*
    274 		 * Changed :-) to make it relative to Jan. 1st, 1970
    275 		 * and to disambiguate negative division
    276 		 */
    277 		days = 367*(y-1960)-7*(y+(m+9)/12)/4-3*((y+(m+9)/12-1)/100+1)/4+275*m/9+d-239;
    278 #endif
    279 		crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second;
    280 
    281 		/* timezone offset is unreliable on some disks */
    282 		if (-48 <= tz && tz <= 52)
    283 			crtime -= tz * 15 * 60;
    284 	}
    285 	pu->tv_sec  = crtime;
    286 	pu->tv_nsec = 0;
    287 	return 1;
    288 }
    289 
    290 static u_int
    291 cd9660_chars2ui(const u_char *begin, int len)
    292 {
    293 	u_int rc;
    294 
    295 	for (rc = 0; --len >= 0;) {
    296 		rc *= 10;
    297 		rc += *begin++ - '0';
    298 	}
    299 	return rc;
    300 }
    301 
    302 int
    303 cd9660_tstamp_conv17(const u_char *pi, struct timespec *pu)
    304 {
    305 	u_char tbuf[7];
    306 
    307 	/* year:"0001"-"9999" -> -1900  */
    308 	tbuf[0] = cd9660_chars2ui(pi,4) - 1900;
    309 
    310 	/* month: " 1"-"12"      -> 1 - 12 */
    311 	tbuf[1] = cd9660_chars2ui(pi + 4,2);
    312 
    313 	/* day:   " 1"-"31"      -> 1 - 31 */
    314 	tbuf[2] = cd9660_chars2ui(pi + 6,2);
    315 
    316 	/* hour:  " 0"-"23"      -> 0 - 23 */
    317 	tbuf[3] = cd9660_chars2ui(pi + 8,2);
    318 
    319 	/* minute:" 0"-"59"      -> 0 - 59 */
    320 	tbuf[4] = cd9660_chars2ui(pi + 10,2);
    321 
    322 	/* second:" 0"-"59"      -> 0 - 59 */
    323 	tbuf[5] = cd9660_chars2ui(pi + 12,2);
    324 
    325 	/* difference of GMT */
    326 	tbuf[6] = pi[16];
    327 
    328 	return cd9660_tstamp_conv7(tbuf,pu);
    329 }
    330 
    331 ino_t
    332 isodirino(struct iso_directory_record *isodir, struct iso_mnt *imp)
    333 {
    334 	ino_t ino;
    335 
    336 	/*
    337 	 * Note there is an inverse calculation in
    338 	 * cd9660_vfsops.c:cd9660_loadvnode():
    339 	 *   ip->iso_start = ino >> imp->im_bshift;
    340 	 * and also a calculation of the isodir pointer
    341 	 * from an inode in cd9660_vnops.c:cd9660_readlink()
    342 	 */
    343 	ino = ((ino_t)isonum_733(isodir->extent) +
    344 		isonum_711(isodir->ext_attr_length)) << imp->im_bshift;
    345 	return ino;
    346 }
    347