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