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