xref: /original-bsd/sys/isofs/cd9660/cd9660_node.c (revision deff14a8)
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley
6  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
7  * Support code is derived from software contributed to Berkeley
8  * by Atsushi Murai (amurai@spec.co.jp).
9  *
10  * %sccs.include.redist.c%
11  *
12  *	@(#)cd9660_node.c	8.4 (Berkeley) 07/13/94
13  */
14 
15 #include <sys/param.h>
16 #include <sys/systm.h>
17 #include <sys/mount.h>
18 #include <sys/proc.h>
19 #include <sys/file.h>
20 #include <sys/buf.h>
21 #include <sys/vnode.h>
22 #include <sys/kernel.h>
23 #include <sys/malloc.h>
24 #include <sys/stat.h>
25 
26 #include <isofs/cd9660/iso.h>
27 #include <isofs/cd9660/cd9660_node.h>
28 #include <isofs/cd9660/iso_rrip.h>
29 
30 /*
31  * Structures associated with iso_node caching.
32  */
33 struct iso_node **isohashtbl;
34 u_long isohash;
35 #define	INOHASH(device, inum)	(((device) + ((inum)>>12)) & isohash)
36 
37 #ifdef ISODEVMAP
38 struct iso_node **idvhashtbl;
39 u_long idvhash;
40 #define	DNOHASH(device, inum)	(((device) + ((inum)>>12)) & idvhash)
41 #endif
42 
43 int prtactive;	/* 1 => print out reclaim of active vnodes */
44 
45 /*
46  * Initialize hash links for inodes and dnodes.
47  */
48 cd9660_init()
49 {
50 
51 	isohashtbl = hashinit(desiredvnodes, M_ISOFSMNT, &isohash);
52 #ifdef ISODEVMAP
53 	idvhashtbl = hashinit(desiredvnodes / 8, M_ISOFSMNT, &idvhash);
54 #endif
55 }
56 
57 #ifdef ISODEVMAP
58 /*
59  * Enter a new node into the device hash list
60  */
61 struct iso_dnode *
62 iso_dmap(device, inum, create)
63 	dev_t	device;
64 	ino_t	inum;
65 	int	create;
66 {
67 	register struct iso_dnode **dpp, *dp, *dq;
68 
69 	dpp = &idvhashtbl[DNOHASH(device, inum)];
70 	for (dp = *dpp;; dp = dp->d_next) {
71 		if (dp == NULL)
72 			return (NULL);
73 		if (inum == dp->i_number && device == dp->i_dev)
74 			return (dp);
75 
76 	if (!create)
77 		return (NULL);
78 
79 	MALLOC(dp, struct iso_dnode *, sizeof(struct iso_dnode), M_CACHE,
80 	       M_WAITOK);
81 	dp->i_dev = dev;
82 	dp->i_number = ino;
83 
84 	if (dq = *dpp)
85 		dq->d_prev = dp->d_next;
86 	dp->d_next = dq;
87 	dp->d_prev = dpp;
88 	*dpp = dp;
89 
90 	return (dp);
91 }
92 
93 void
94 iso_dunmap(device)
95 	dev_t device;
96 {
97 	struct iso_dnode **dpp, *dp, *dq;
98 
99 	for (dpp = idvhashtbl; dpp <= idvhashtbl + idvhash; dpp++) {
100 		for (dp = *dpp; dp != NULL; dp = dq)
101 			dq = dp->d_next;
102 			if (device == dp->i_dev) {
103 				if (dq)
104 					dq->d_prev = dp->d_prev;
105 				*dp->d_prev = dq;
106 				FREE(dp, M_CACHE);
107 			}
108 		}
109 	}
110 }
111 #endif
112 
113 /*
114  * Use the device/inum pair to find the incore inode, and return a pointer
115  * to it. If it is in core, but locked, wait for it.
116  */
117 struct vnode *
118 cd9660_ihashget(device, inum)
119 	dev_t device;
120 	ino_t inum;
121 {
122 	register struct iso_node *ip;
123 	struct vnode *vp;
124 
125 	for (;;)
126 		for (ip = isohashtbl[INOHASH(device, inum)];; ip = ip->i_next) {
127 			if (ip == NULL)
128 				return (NULL);
129 			if (inum == ip->i_number && device == ip->i_dev) {
130 				if (ip->i_flag & IN_LOCKED) {
131 					ip->i_flag |= IN_WANTED;
132 					sleep(ip, PINOD);
133 					break;
134 				}
135 				vp = ITOV(ip);
136 				if (!vget(vp, 1))
137 					return (vp);
138 				break;
139 			}
140 		}
141 	/* NOTREACHED */
142 }
143 
144 /*
145  * Insert the inode into the hash table, and return it locked.
146  */
147 void
148 cd9660_ihashins(ip)
149 	struct iso_node *ip;
150 {
151 	struct iso_node **ipp, *iq;
152 
153 	ipp = &isohashtbl[INOHASH(ip->i_dev, ip->i_number)];
154 	if (iq = *ipp)
155 		iq->i_prev = &ip->i_next;
156 	ip->i_next = iq;
157 	ip->i_prev = ipp;
158 	*ipp = ip;
159 	if (ip->i_flag & IN_LOCKED)
160 		panic("cd9660_ihashins: already locked");
161 	if (curproc)
162 		ip->i_lockholder = curproc->p_pid;
163 	else
164 		ip->i_lockholder = -1;
165 	ip->i_flag |= IN_LOCKED;
166 }
167 
168 /*
169  * Remove the inode from the hash table.
170  */
171 void
172 cd9660_ihashrem(ip)
173 	register struct iso_node *ip;
174 {
175 	register struct iso_node *iq;
176 
177 	if (iq = ip->i_next)
178 		iq->i_prev = ip->i_prev;
179 	*ip->i_prev = iq;
180 #ifdef DIAGNOSTIC
181 	ip->i_next = NULL;
182 	ip->i_prev = NULL;
183 #endif
184 }
185 
186 /*
187  * Last reference to an inode, write the inode out and if necessary,
188  * truncate and deallocate the file.
189  */
190 int
191 cd9660_inactive(ap)
192 	struct vop_inactive_args /* {
193 		struct vnode *a_vp;
194 	} */ *ap;
195 {
196 	struct vnode *vp = ap->a_vp;
197 	register struct iso_node *ip = VTOI(vp);
198 	int mode, error = 0;
199 
200 	if (prtactive && vp->v_usecount != 0)
201 		vprint("cd9660_inactive: pushing active", vp);
202 
203 	ip->i_flag = 0;
204 	/*
205 	 * If we are done with the inode, reclaim it
206 	 * so that it can be reused immediately.
207 	 */
208 	if (vp->v_usecount == 0 && ip->inode.iso_mode == 0)
209 		vgone(vp);
210 	return error;
211 }
212 
213 /*
214  * Reclaim an inode so that it can be used for other purposes.
215  */
216 int
217 cd9660_reclaim(ap)
218 	struct vop_reclaim_args /* {
219 		struct vnode *a_vp;
220 	} */ *ap;
221 {
222 	register struct vnode *vp = ap->a_vp;
223 	register struct iso_node *ip = VTOI(vp);
224 	int i;
225 
226 	if (prtactive && vp->v_usecount != 0)
227 		vprint("cd9660_reclaim: pushing active", vp);
228 	/*
229 	 * Remove the inode from its hash chain.
230 	 */
231 	cd9660_ihashrem(ip);
232 	/*
233 	 * Purge old data structures associated with the inode.
234 	 */
235 	cache_purge(vp);
236 	if (ip->i_devvp) {
237 		vrele(ip->i_devvp);
238 		ip->i_devvp = 0;
239 	}
240 	FREE(vp->v_data, M_ISOFSNODE);
241 	vp->v_data = NULL;
242 	return (0);
243 }
244 
245 /*
246  * File attributes
247  */
248 void
249 cd9660_defattr(isodir,inop,bp)
250 	struct iso_directory_record *isodir;
251 	struct iso_node *inop;
252 	struct buf *bp;
253 {
254 	struct buf *bp2 = NULL;
255 	struct iso_mnt *imp;
256 	struct iso_extended_attributes *ap = NULL;
257 	int off;
258 
259 	if (isonum_711(isodir->flags)&2) {
260 		inop->inode.iso_mode = S_IFDIR;
261 		/*
262 		 * If we return 2, fts() will assume there are no subdirectories
263 		 * (just links for the path and .), so instead we return 1.
264 		 */
265 		inop->inode.iso_links = 1;
266 	} else {
267 		inop->inode.iso_mode = S_IFREG;
268 		inop->inode.iso_links = 1;
269 	}
270 	if (!bp
271 	    && ((imp = inop->i_mnt)->im_flags&ISOFSMNT_EXTATT)
272 	    && (off = isonum_711(isodir->ext_attr_length))) {
273 		iso_blkatoff(inop,-off * imp->logical_block_size,&bp2);
274 		bp = bp2;
275 	}
276 	if (bp) {
277 		ap = (struct iso_extended_attributes *)bp->b_un.b_addr;
278 
279 		if (isonum_711(ap->version) == 1) {
280 			if (!(ap->perm[0]&0x40))
281 				inop->inode.iso_mode |= VEXEC >> 6;
282 			if (!(ap->perm[0]&0x10))
283 				inop->inode.iso_mode |= VREAD >> 6;
284 			if (!(ap->perm[0]&4))
285 				inop->inode.iso_mode |= VEXEC >> 3;
286 			if (!(ap->perm[0]&1))
287 				inop->inode.iso_mode |= VREAD >> 3;
288 			if (!(ap->perm[1]&0x40))
289 				inop->inode.iso_mode |= VEXEC;
290 			if (!(ap->perm[1]&0x10))
291 				inop->inode.iso_mode |= VREAD;
292 			inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */
293 			inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */
294 		} else
295 			ap = NULL;
296 	}
297 	if (!ap) {
298 		inop->inode.iso_mode |= VREAD|VEXEC|(VREAD|VEXEC)>>3|(VREAD|VEXEC)>>6;
299 		inop->inode.iso_uid = (uid_t)0;
300 		inop->inode.iso_gid = (gid_t)0;
301 	}
302 	if (bp2)
303 		brelse(bp2);
304 }
305 
306 /*
307  * Time stamps
308  */
309 void
310 cd9660_deftstamp(isodir,inop,bp)
311 	struct iso_directory_record *isodir;
312 	struct iso_node *inop;
313 	struct buf *bp;
314 {
315 	struct buf *bp2 = NULL;
316 	struct iso_mnt *imp;
317 	struct iso_extended_attributes *ap = NULL;
318 	int off;
319 
320 	if (!bp
321 	    && ((imp = inop->i_mnt)->im_flags&ISOFSMNT_EXTATT)
322 	    && (off = isonum_711(isodir->ext_attr_length))) {
323 		iso_blkatoff(inop,-off * imp->logical_block_size,&bp2);
324 		bp = bp2;
325 	}
326 	if (bp) {
327 		ap = (struct iso_extended_attributes *)bp->b_un.b_addr;
328 
329 		if (isonum_711(ap->version) == 1) {
330 			if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime))
331 				cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime);
332 			if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime))
333 				inop->inode.iso_ctime = inop->inode.iso_atime;
334 			if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime))
335 				inop->inode.iso_mtime = inop->inode.iso_ctime;
336 		} else
337 			ap = NULL;
338 	}
339 	if (!ap) {
340 		cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime);
341 		inop->inode.iso_atime = inop->inode.iso_ctime;
342 		inop->inode.iso_mtime = inop->inode.iso_ctime;
343 	}
344 	if (bp2)
345 		brelse(bp2);
346 }
347 
348 int
349 cd9660_tstamp_conv7(pi,pu)
350 char *pi;
351 struct timeval *pu;
352 {
353 	int i;
354 	int crtime, days;
355 	int y, m, d, hour, minute, second, tz;
356 
357 	y = pi[0] + 1900;
358 	m = pi[1];
359 	d = pi[2];
360 	hour = pi[3];
361 	minute = pi[4];
362 	second = pi[5];
363 	tz = pi[6];
364 
365 	if (y < 1970) {
366 		pu->tv_sec  = 0;
367 		pu->tv_usec = 0;
368 		return 0;
369 	} else {
370 #ifdef	ORIGINAL
371 		/* computes day number relative to Sept. 19th,1989 */
372 		/* don't even *THINK* about changing formula. It works! */
373 		days = 367*(y-1980)-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d-100;
374 #else
375 		/*
376 		 * Changed :-) to make it relative to Jan. 1st, 1970
377 		 * and to disambiguate negative division
378 		 */
379 		days = 367*(y-1960)-7*(y+(m+9)/12)/4-3*((y+(m+9)/12-1)/100+1)/4+275*m/9+d-239;
380 #endif
381 		crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second;
382 
383 		/* timezone offset is unreliable on some disks */
384 		if (-48 <= tz && tz <= 52)
385 			crtime -= tz * 15 * 60;
386 	}
387 	pu->tv_sec  = crtime;
388 	pu->tv_usec = 0;
389 	return 1;
390 }
391 
392 static unsigned
393 cd9660_chars2ui(begin,len)
394 	unsigned char *begin;
395 	int len;
396 {
397 	unsigned rc;
398 
399 	for (rc = 0; --len >= 0;) {
400 		rc *= 10;
401 		rc += *begin++ - '0';
402 	}
403 	return rc;
404 }
405 
406 int
407 cd9660_tstamp_conv17(pi,pu)
408 	unsigned char *pi;
409 	struct timeval *pu;
410 {
411 	unsigned char buf[7];
412 
413 	/* year:"0001"-"9999" -> -1900  */
414 	buf[0] = cd9660_chars2ui(pi,4) - 1900;
415 
416 	/* month: " 1"-"12"      -> 1 - 12 */
417 	buf[1] = cd9660_chars2ui(pi + 4,2);
418 
419 	/* day:   " 1"-"31"      -> 1 - 31 */
420 	buf[2] = cd9660_chars2ui(pi + 6,2);
421 
422 	/* hour:  " 0"-"23"      -> 0 - 23 */
423 	buf[3] = cd9660_chars2ui(pi + 8,2);
424 
425 	/* minute:" 0"-"59"      -> 0 - 59 */
426 	buf[4] = cd9660_chars2ui(pi + 10,2);
427 
428 	/* second:" 0"-"59"      -> 0 - 59 */
429 	buf[5] = cd9660_chars2ui(pi + 12,2);
430 
431 	/* difference of GMT */
432 	buf[6] = pi[16];
433 
434 	return cd9660_tstamp_conv7(buf,pu);
435 }
436 
437 void
438 isodirino(inump,isodir,imp)
439 	ino_t *inump;
440 	struct iso_directory_record *isodir;
441 	struct iso_mnt *imp;
442 {
443 	*inump = (isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length))
444 		 * imp->logical_block_size;
445 }
446