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