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