xref: /openbsd-src/sys/isofs/cd9660/cd9660_node.c (revision de8cc8edbc71bd3e3bc7fbffa27ba0e564c37d8b)
1 /*	$OpenBSD: cd9660_node.c,v 1.35 2020/02/27 09:10:31 mpi 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 	register struct iso_node *ip = VTOI(vp);
186 	int error = 0;
187 
188 #ifdef DIAGNOSTIC
189 	if (prtactive && vp->v_usecount != 0)
190 		vprint("cd9660_inactive: pushing active", vp);
191 #endif
192 
193 	ip->i_flag = 0;
194 	VOP_UNLOCK(vp);
195 	/*
196 	 * If we are done with the inode, reclaim it
197 	 * so that it can be reused immediately.
198 	 */
199 	if (ip->inode.iso_mode == 0)
200 		vrecycle(vp, ap->a_p);
201 
202 	return (error);
203 }
204 
205 /*
206  * Reclaim an inode so that it can be used for other purposes.
207  */
208 int
209 cd9660_reclaim(v)
210 	void *v;
211 {
212 	struct vop_reclaim_args *ap = v;
213 	register struct vnode *vp = ap->a_vp;
214 	register struct iso_node *ip = VTOI(vp);
215 
216 #ifdef DIAGNOSTIC
217 	if (prtactive && vp->v_usecount != 0)
218 		vprint("cd9660_reclaim: pushing active", vp);
219 #endif
220 
221 	/*
222 	 * Remove the inode from its hash chain.
223 	 */
224 	cd9660_ihashrem(ip);
225 	/*
226 	 * Purge old data structures associated with the inode.
227 	 */
228 	cache_purge(vp);
229 	if (ip->i_devvp) {
230 		vrele(ip->i_devvp);
231 		ip->i_devvp = 0;
232 	}
233 	free(vp->v_data, M_ISOFSNODE, 0);
234 	vp->v_data = NULL;
235 	return (0);
236 }
237 
238 /*
239  * File attributes
240  */
241 void
242 cd9660_defattr(isodir, inop, bp)
243 	struct iso_directory_record *isodir;
244 	struct iso_node *inop;
245 	struct buf *bp;
246 {
247 	struct buf *bp2 = NULL;
248 	struct iso_mnt *imp;
249 	struct iso_extended_attributes *ap = NULL;
250 	int off;
251 
252 	if (isonum_711(isodir->flags)&2) {
253 		inop->inode.iso_mode = S_IFDIR;
254 		/*
255 		 * If we return 2, fts() will assume there are no subdirectories
256 		 * (just links for the path and .), so instead we return 1.
257 		 */
258 		inop->inode.iso_links = 1;
259 	} else {
260 		inop->inode.iso_mode = S_IFREG;
261 		inop->inode.iso_links = 1;
262 	}
263 	if (!bp
264 	    && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
265 	    && (off = isonum_711(isodir->ext_attr_length))) {
266 		cd9660_bufatoff(inop, (off_t)-(off << imp->im_bshift), NULL,
267 			     &bp2);
268 		bp = bp2;
269 	}
270 	if (bp) {
271 		ap = (struct iso_extended_attributes *)bp->b_data;
272 
273 		if (isonum_711(ap->version) == 1) {
274 			if (!(ap->perm[1]&0x10))
275 				inop->inode.iso_mode |= S_IRUSR;
276 			if (!(ap->perm[1]&0x40))
277 				inop->inode.iso_mode |= S_IXUSR;
278 			if (!(ap->perm[0]&0x01))
279 				inop->inode.iso_mode |= S_IRGRP;
280 			if (!(ap->perm[0]&0x04))
281 				inop->inode.iso_mode |= S_IXGRP;
282 			if (!(ap->perm[0]&0x10))
283 				inop->inode.iso_mode |= S_IROTH;
284 			if (!(ap->perm[0]&0x40))
285 				inop->inode.iso_mode |= S_IXOTH;
286 			inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */
287 			inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */
288 		} else
289 			ap = NULL;
290 	}
291 	if (!ap) {
292 		inop->inode.iso_mode |=
293 		    S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
294 		inop->inode.iso_uid = (uid_t)0;
295 		inop->inode.iso_gid = (gid_t)0;
296 	}
297 	if (bp2)
298 		brelse(bp2);
299 }
300 
301 /*
302  * Time stamps
303  */
304 void
305 cd9660_deftstamp(isodir,inop,bp)
306 	struct iso_directory_record *isodir;
307 	struct iso_node *inop;
308 	struct buf *bp;
309 {
310 	struct buf *bp2 = NULL;
311 	struct iso_mnt *imp;
312 	struct iso_extended_attributes *ap = NULL;
313 	int off;
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 (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime))
327 				cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime);
328 			if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime))
329 				inop->inode.iso_ctime = inop->inode.iso_atime;
330 			if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime))
331 				inop->inode.iso_mtime = inop->inode.iso_ctime;
332 		} else
333 			ap = NULL;
334 	}
335 	if (!ap) {
336 		cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime);
337 		inop->inode.iso_atime = inop->inode.iso_ctime;
338 		inop->inode.iso_mtime = inop->inode.iso_ctime;
339 	}
340 	if (bp2)
341 		brelse(bp2);
342 }
343 
344 int
345 cd9660_tstamp_conv7(pi,pu)
346 	u_char *pi;
347 	struct timespec *pu;
348 {
349 	int crtime, days;
350 	int y, m, d, hour, minute, second;
351 	signed char tz;
352 
353 	y = pi[0] + 1900;
354 	m = pi[1];
355 	d = pi[2];
356 	hour = pi[3];
357 	minute = pi[4];
358 	second = pi[5];
359 	tz = (signed char) pi[6];
360 
361 	if (y < 1970) {
362 		pu->tv_sec  = 0;
363 		pu->tv_nsec = 0;
364 		return (0);
365 	} else {
366 #ifdef	ORIGINAL
367 		/* computes day number relative to Sept. 19th,1989 */
368 		/* don't even *THINK* about changing formula. It works! */
369 		days = 367*(y-1980)-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d-100;
370 #else
371 		/*
372 		 * Changed :-) to make it relative to Jan. 1st, 1970
373 		 * and to disambiguate negative division
374 		 */
375 		days = 367*(y-1960)-7*(y+(m+9)/12)/4-3*((y+(m+9)/12-1)/100+1)/4+275*m/9+d-239;
376 #endif
377 		crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second;
378 
379 		/* timezone offset is unreliable on some disks */
380 		if (-48 <= tz && tz <= 52)
381 			crtime -= tz * 15 * 60;
382 	}
383 	pu->tv_sec  = crtime;
384 	pu->tv_nsec = 0;
385 	return (1);
386 }
387 
388 static u_int
389 cd9660_chars2ui(begin,len)
390 	u_char *begin;
391 	int len;
392 {
393 	u_int rc;
394 
395 	for (rc = 0; --len >= 0;) {
396 		rc *= 10;
397 		rc += *begin++ - '0';
398 	}
399 	return (rc);
400 }
401 
402 int
403 cd9660_tstamp_conv17(pi,pu)
404 	u_char *pi;
405 	struct timespec *pu;
406 {
407 	u_char buf[7];
408 
409 	/* year:"0001"-"9999" -> -1900  */
410 	buf[0] = cd9660_chars2ui(pi,4) - 1900;
411 
412 	/* month: " 1"-"12"      -> 1 - 12 */
413 	buf[1] = cd9660_chars2ui(pi + 4,2);
414 
415 	/* day:   " 1"-"31"      -> 1 - 31 */
416 	buf[2] = cd9660_chars2ui(pi + 6,2);
417 
418 	/* hour:  " 0"-"23"      -> 0 - 23 */
419 	buf[3] = cd9660_chars2ui(pi + 8,2);
420 
421 	/* minute:" 0"-"59"      -> 0 - 59 */
422 	buf[4] = cd9660_chars2ui(pi + 10,2);
423 
424 	/* second:" 0"-"59"      -> 0 - 59 */
425 	buf[5] = cd9660_chars2ui(pi + 12,2);
426 
427 	/* difference of GMT */
428 	buf[6] = pi[16];
429 
430 	return (cd9660_tstamp_conv7(buf,pu));
431 }
432 
433 cdino_t
434 isodirino(isodir, imp)
435 	struct iso_directory_record *isodir;
436 	struct iso_mnt *imp;
437 {
438 	cdino_t ino;
439 
440 	ino = (isonum_733(isodir->extent) +
441 	    isonum_711(isodir->ext_attr_length)) << imp->im_bshift;
442 	return (ino);
443 }
444