xref: /openbsd-src/sys/isofs/cd9660/cd9660_node.c (revision 8500990981f885cbe5e6a4958549cacc238b5ae6)
1 /*	$OpenBSD: cd9660_node.c,v 1.14 2003/06/02 23:28:05 millert Exp $	*/
2 /*	$NetBSD: cd9660_node.c,v 1.17 1997/05/05 07:13:57 mycroft Exp $	*/
3 
4 /*-
5  * Copyright (c) 1982, 1986, 1989, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley
9  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
10  * Support code is derived from software contributed to Berkeley
11  * by Atsushi Murai (amurai@spec.co.jp).
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)cd9660_node.c	8.5 (Berkeley) 12/5/94
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/mount.h>
43 #include <sys/proc.h>
44 #include <sys/file.h>
45 #include <sys/buf.h>
46 #include <sys/vnode.h>
47 #include <sys/namei.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/stat.h>
51 
52 #include <isofs/cd9660/iso.h>
53 #include <isofs/cd9660/cd9660_extern.h>
54 #include <isofs/cd9660/cd9660_node.h>
55 #include <isofs/cd9660/iso_rrip.h>
56 
57 /*
58  * Structures associated with iso_node caching.
59  */
60 struct iso_node **isohashtbl;
61 u_long isohash;
62 #define	INOHASH(device, inum)	(((device) + ((inum)>>12)) & isohash)
63 struct simplelock cd9660_ihash_slock;
64 
65 #ifdef ISODEVMAP
66 struct iso_node **idvhashtbl;
67 u_long idvhash;
68 #define	DNOHASH(device, inum)	(((device) + ((inum)>>12)) & idvhash)
69 #endif
70 
71 extern int prtactive;	/* 1 => print out reclaim of active vnodes */
72 
73 static u_int cd9660_chars2ui(u_char *, int);
74 
75 /*
76  * Initialize hash links for inodes and dnodes.
77  */
78 int
79 cd9660_init(vfsp)
80 	struct vfsconf *vfsp;
81 {
82 
83 	isohashtbl = hashinit(desiredvnodes, M_ISOFSMNT, M_WAITOK, &isohash);
84 	simple_lock_init(&cd9660_ihash_slock);
85 #ifdef ISODEVMAP
86 	idvhashtbl = hashinit(desiredvnodes / 8, M_ISOFSMNT, M_WAITOK, &idvhash);
87 #endif
88 	return (0);
89 }
90 
91 #ifdef ISODEVMAP
92 /*
93  * Enter a new node into the device hash list
94  */
95 struct iso_dnode *
96 iso_dmap(device, inum, create)
97 	dev_t	device;
98 	ino_t	inum;
99 	int	create;
100 {
101 	register struct iso_dnode **dpp, *dp, *dq;
102 
103 	dpp = &idvhashtbl[DNOHASH(device, inum)];
104 	for (dp = *dpp;; dp = dp->d_next) {
105 		if (dp == NULL)
106 			return (NULL);
107 		if (inum == dp->i_number && device == dp->i_dev)
108 			return (dp);
109 	}
110 
111 	if (!create)
112 		return (NULL);
113 
114 	MALLOC(dp, struct iso_dnode *, sizeof(struct iso_dnode), M_CACHE,
115 	       M_WAITOK);
116 	dp->i_dev = dev;
117 	dp->i_number = ino;
118 
119 	if (dq = *dpp)
120 		dq->d_prev = dp->d_next;
121 	dp->d_next = dq;
122 	dp->d_prev = dpp;
123 	*dpp = dp;
124 
125 	return (dp);
126 }
127 
128 void
129 iso_dunmap(device)
130 	dev_t device;
131 {
132 	struct iso_dnode **dpp, *dp, *dq;
133 
134 	for (dpp = idvhashtbl; dpp <= idvhashtbl + idvhash; dpp++) {
135 		for (dp = *dpp; dp != NULL; dp = dq) {
136 			dq = dp->d_next;
137 			if (device == dp->i_dev) {
138 				if (dq)
139 					dq->d_prev = dp->d_prev;
140 				*dp->d_prev = dq;
141 				FREE(dp, M_CACHE);
142 			}
143 		}
144 	}
145 }
146 #endif
147 
148 /*
149  * Use the device/inum pair to find the incore inode, and return a pointer
150  * to it. If it is in core, but locked, wait for it.
151  */
152 struct vnode *
153 cd9660_ihashget(dev, inum)
154 	dev_t dev;
155 	ino_t inum;
156 {
157 	struct proc *p = curproc;               /* XXX */
158 	struct iso_node *ip;
159 	struct vnode *vp;
160 
161 loop:
162        simple_lock(&cd9660_ihash_slock);
163        for (ip = isohashtbl[INOHASH(dev, inum)]; ip; ip = ip->i_next) {
164                if (inum == ip->i_number && dev == ip->i_dev) {
165                        vp = ITOV(ip);
166                        simple_lock(&vp->v_interlock);
167                        simple_unlock(&cd9660_ihash_slock);
168                        if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p))
169                                goto loop;
170                        return (vp);
171 	       }
172        }
173        simple_unlock(&cd9660_ihash_slock);
174        return (NULL);
175 }
176 
177 /*
178  * Insert the inode into the hash table, and return it locked.
179  */
180 int
181 cd9660_ihashins(ip)
182 	struct iso_node *ip;
183 {
184 	struct proc *p = curproc;
185 	struct iso_node **ipp, *iq;
186 
187 	simple_lock(&cd9660_ihash_slock);
188 	ipp = &isohashtbl[INOHASH(ip->i_dev, ip->i_number)];
189 
190 	for (iq = *ipp; iq; iq = iq->i_next) {
191 		if (iq->i_dev == ip->i_dev &&
192 		    iq->i_number == ip->i_number)
193 			return (EEXIST);
194 	}
195 
196 	if ((iq = *ipp) != NULL)
197 		iq->i_prev = &ip->i_next;
198 	ip->i_next = iq;
199 	ip->i_prev = ipp;
200 	*ipp = ip;
201 	simple_unlock(&cd9660_ihash_slock);
202 
203 	lockmgr(&ip->i_lock, LK_EXCLUSIVE, 0, p);
204 	return (0);
205 }
206 
207 /*
208  * Remove the inode from the hash table.
209  */
210 void
211 cd9660_ihashrem(ip)
212 	register struct iso_node *ip;
213 {
214 	register struct iso_node *iq;
215 
216 	if (ip->i_prev == NULL)
217 		return;
218 
219 	simple_lock(&cd9660_ihash_slock);
220 	if ((iq = ip->i_next) != NULL)
221 		iq->i_prev = ip->i_prev;
222 	*ip->i_prev = iq;
223 #ifdef DIAGNOSTIC
224 	ip->i_next = NULL;
225 	ip->i_prev = NULL;
226 #endif
227 	simple_unlock(&cd9660_ihash_slock);
228 }
229 
230 /*
231  * Last reference to an inode, write the inode out and if necessary,
232  * truncate and deallocate the file.
233  */
234 int
235 cd9660_inactive(v)
236 	void *v;
237 {
238 	struct vop_inactive_args /* {
239 		struct vnode *a_vp;
240 		struct proc *a_p;
241 	} */ *ap = v;
242 	struct vnode *vp = ap->a_vp;
243 	struct proc *p = ap->a_p;
244 	register struct iso_node *ip = VTOI(vp);
245 	int error = 0;
246 
247 	if (prtactive && vp->v_usecount != 0)
248 		vprint("cd9660_inactive: pushing active", vp);
249 
250 	ip->i_flag = 0;
251 	VOP_UNLOCK(vp, 0, p);
252 	/*
253 	 * If we are done with the inode, reclaim it
254 	 * so that it can be reused immediately.
255 	 */
256 	if (ip->inode.iso_mode == 0)
257 		vrecycle(vp, (struct simplelock *)0, p);
258 
259 	return (error);
260 }
261 
262 /*
263  * Reclaim an inode so that it can be used for other purposes.
264  */
265 int
266 cd9660_reclaim(v)
267 	void *v;
268 {
269 	struct vop_reclaim_args /* {
270 		struct vnode *a_vp;
271 	} */ *ap = v;
272 	register struct vnode *vp = ap->a_vp;
273 	register struct iso_node *ip = VTOI(vp);
274 
275 	if (prtactive && vp->v_usecount != 0)
276 		vprint("cd9660_reclaim: pushing active", vp);
277 	/*
278 	 * Remove the inode from its hash chain.
279 	 */
280 	cd9660_ihashrem(ip);
281 	/*
282 	 * Purge old data structures associated with the inode.
283 	 */
284 	cache_purge(vp);
285 	if (ip->i_devvp) {
286 		vrele(ip->i_devvp);
287 		ip->i_devvp = 0;
288 	}
289 	FREE(vp->v_data, M_ISOFSNODE);
290 	vp->v_data = NULL;
291 	return (0);
292 }
293 
294 /*
295  * File attributes
296  */
297 void
298 cd9660_defattr(isodir, inop, bp)
299 	struct iso_directory_record *isodir;
300 	struct iso_node *inop;
301 	struct buf *bp;
302 {
303 	struct buf *bp2 = NULL;
304 	struct iso_mnt *imp;
305 	struct iso_extended_attributes *ap = NULL;
306 	int off;
307 
308 	if (isonum_711(isodir->flags)&2) {
309 		inop->inode.iso_mode = S_IFDIR;
310 		/*
311 		 * If we return 2, fts() will assume there are no subdirectories
312 		 * (just links for the path and .), so instead we return 1.
313 		 */
314 		inop->inode.iso_links = 1;
315 	} else {
316 		inop->inode.iso_mode = S_IFREG;
317 		inop->inode.iso_links = 1;
318 	}
319 	if (!bp
320 	    && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
321 	    && (off = isonum_711(isodir->ext_attr_length))) {
322 		cd9660_bufatoff(inop, (off_t)-(off << imp->im_bshift), NULL,
323 			     &bp2);
324 		bp = bp2;
325 	}
326 	if (bp) {
327 		ap = (struct iso_extended_attributes *)bp->b_data;
328 
329 		if (isonum_711(ap->version) == 1) {
330 			if (!(ap->perm[1]&0x10))
331 				inop->inode.iso_mode |= S_IRUSR;
332 			if (!(ap->perm[1]&0x40))
333 				inop->inode.iso_mode |= S_IXUSR;
334 			if (!(ap->perm[0]&0x01))
335 				inop->inode.iso_mode |= S_IRGRP;
336 			if (!(ap->perm[0]&0x04))
337 				inop->inode.iso_mode |= S_IXGRP;
338 			if (!(ap->perm[0]&0x10))
339 				inop->inode.iso_mode |= S_IROTH;
340 			if (!(ap->perm[0]&0x40))
341 				inop->inode.iso_mode |= S_IXOTH;
342 			inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */
343 			inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */
344 		} else
345 			ap = NULL;
346 	}
347 	if (!ap) {
348 		inop->inode.iso_mode |=
349 		    S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
350 		inop->inode.iso_uid = (uid_t)0;
351 		inop->inode.iso_gid = (gid_t)0;
352 	}
353 	if (bp2)
354 		brelse(bp2);
355 }
356 
357 /*
358  * Time stamps
359  */
360 void
361 cd9660_deftstamp(isodir,inop,bp)
362 	struct iso_directory_record *isodir;
363 	struct iso_node *inop;
364 	struct buf *bp;
365 {
366 	struct buf *bp2 = NULL;
367 	struct iso_mnt *imp;
368 	struct iso_extended_attributes *ap = NULL;
369 	int off;
370 
371 	if (!bp
372 	    && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
373 	    && (off = isonum_711(isodir->ext_attr_length))) {
374 		cd9660_bufatoff(inop, (off_t)-(off << imp->im_bshift), NULL,
375 			     &bp2);
376 		bp = bp2;
377 	}
378 	if (bp) {
379 		ap = (struct iso_extended_attributes *)bp->b_data;
380 
381 		if (isonum_711(ap->version) == 1) {
382 			if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime))
383 				cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime);
384 			if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime))
385 				inop->inode.iso_ctime = inop->inode.iso_atime;
386 			if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime))
387 				inop->inode.iso_mtime = inop->inode.iso_ctime;
388 		} else
389 			ap = NULL;
390 	}
391 	if (!ap) {
392 		cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime);
393 		inop->inode.iso_atime = inop->inode.iso_ctime;
394 		inop->inode.iso_mtime = inop->inode.iso_ctime;
395 	}
396 	if (bp2)
397 		brelse(bp2);
398 }
399 
400 int
401 cd9660_tstamp_conv7(pi,pu)
402 	u_char *pi;
403 	struct timespec *pu;
404 {
405 	int crtime, days;
406 	int y, m, d, hour, minute, second;
407 	signed char tz;
408 
409 	y = pi[0] + 1900;
410 	m = pi[1];
411 	d = pi[2];
412 	hour = pi[3];
413 	minute = pi[4];
414 	second = pi[5];
415 	tz = (signed char) pi[6];
416 
417 	if (y < 1970) {
418 		pu->tv_sec  = 0;
419 		pu->tv_nsec = 0;
420 		return (0);
421 	} else {
422 #ifdef	ORIGINAL
423 		/* computes day number relative to Sept. 19th,1989 */
424 		/* don't even *THINK* about changing formula. It works! */
425 		days = 367*(y-1980)-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d-100;
426 #else
427 		/*
428 		 * Changed :-) to make it relative to Jan. 1st, 1970
429 		 * and to disambiguate negative division
430 		 */
431 		days = 367*(y-1960)-7*(y+(m+9)/12)/4-3*((y+(m+9)/12-1)/100+1)/4+275*m/9+d-239;
432 #endif
433 		crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second;
434 
435 		/* timezone offset is unreliable on some disks */
436 		if (-48 <= tz && tz <= 52)
437 			crtime -= tz * 15 * 60;
438 	}
439 	pu->tv_sec  = crtime;
440 	pu->tv_nsec = 0;
441 	return (1);
442 }
443 
444 static u_int
445 cd9660_chars2ui(begin,len)
446 	u_char *begin;
447 	int len;
448 {
449 	u_int rc;
450 
451 	for (rc = 0; --len >= 0;) {
452 		rc *= 10;
453 		rc += *begin++ - '0';
454 	}
455 	return (rc);
456 }
457 
458 int
459 cd9660_tstamp_conv17(pi,pu)
460 	u_char *pi;
461 	struct timespec *pu;
462 {
463 	u_char buf[7];
464 
465 	/* year:"0001"-"9999" -> -1900  */
466 	buf[0] = cd9660_chars2ui(pi,4) - 1900;
467 
468 	/* month: " 1"-"12"      -> 1 - 12 */
469 	buf[1] = cd9660_chars2ui(pi + 4,2);
470 
471 	/* day:   " 1"-"31"      -> 1 - 31 */
472 	buf[2] = cd9660_chars2ui(pi + 6,2);
473 
474 	/* hour:  " 0"-"23"      -> 0 - 23 */
475 	buf[3] = cd9660_chars2ui(pi + 8,2);
476 
477 	/* minute:" 0"-"59"      -> 0 - 59 */
478 	buf[4] = cd9660_chars2ui(pi + 10,2);
479 
480 	/* second:" 0"-"59"      -> 0 - 59 */
481 	buf[5] = cd9660_chars2ui(pi + 12,2);
482 
483 	/* difference of GMT */
484 	buf[6] = pi[16];
485 
486 	return (cd9660_tstamp_conv7(buf,pu));
487 }
488 
489 ino_t
490 isodirino(isodir, imp)
491 	struct iso_directory_record *isodir;
492 	struct iso_mnt *imp;
493 {
494 	ino_t ino;
495 
496 	ino = (isonum_733(isodir->extent) +
497 	    isonum_711(isodir->ext_attr_length)) << imp->im_bshift;
498 	return (ino);
499 }
500