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