]> git.saurik.com Git - apple/xnu.git/blame - bsd/isofs/cd9660/cd9660_node.c
xnu-344.23.tar.gz
[apple/xnu.git] / bsd / isofs / cd9660 / cd9660_node.c
CommitLineData
1c79356b 1/*
fa4905b1 2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
de355530
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
1c79356b 11 *
de355530
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
de355530
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/* $NetBSD: cd9660_node.c,v 1.13 1994/12/24 15:30:07 cgd Exp $ */
23
24/*-
25 * Copyright (c) 1982, 1986, 1989, 1994
26 * The Regents of the University of California. All rights reserved.
27 *
28 * This code is derived from software contributed to Berkeley
29 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
30 * Support code is derived from software contributed to Berkeley
31 * by Atsushi Murai (amurai@spec.co.jp).
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)cd9660_node.c 8.5 (Berkeley) 12/5/94
62
63
64
65 * HISTORY
66 * 22-Jan-98 radar 1669467 - ISO 9660 CD support - jwc
67 * 17-Feb-98 radar 1669467 - changed lock protocols to use the lock manager - chw
68
69 */
70
71#include <sys/param.h>
72#include <sys/systm.h>
73#include <sys/mount.h>
74#include <sys/proc.h>
75#include <sys/file.h>
76#include <sys/buf.h>
77#include <sys/vnode.h>
78#include <sys/kernel.h>
79#include <sys/malloc.h>
80#include <sys/stat.h>
81#include <sys/lock.h>
82
83#include <isofs/cd9660/iso.h>
84#include <isofs/cd9660/cd9660_node.h>
85#include <isofs/cd9660/iso_rrip.h>
86#include <isofs/cd9660/cd9660_mount.h>
87
88/*
89 * Structures associated with iso_node caching.
90 */
91struct iso_node **isohashtbl;
92u_long isohash;
93#define INOHASH(device, inum) (((device) + ((inum)>>12)) & isohash)
94
95#ifdef ISODEVMAP
96struct iso_node **idvhashtbl;
97u_long idvhash;
98#define DNOHASH(device, inum) (((device) + ((inum)>>12)) & idvhash)
99#endif
100
101/* defined in bsd/ufs/ufs/ufs_inode.c */
102extern int prtactive; /* 1 => print out reclaim of active vnodes */
103
104extern void cache_purge (struct vnode *vp);
105
106extern u_char isonullname[];
107/*
108 * Initialize hash links for inodes and dnodes.
109 */
110int
111cd9660_init()
112{
113
114 isohashtbl = hashinit(desiredvnodes, M_ISOFSMNT, &isohash);
115#ifdef ISODEVMAP
116 idvhashtbl = hashinit(desiredvnodes / 8, M_ISOFSMNT, &idvhash);
117#endif
118 return 0;
119}
120
121#ifdef ISODEVMAP
122/*
123 * Enter a new node into the device hash list
124 */
125struct iso_dnode *
126iso_dmap(device, inum, create)
127 dev_t device;
128 ino_t inum;
129 int create;
130{
131 register struct iso_dnode **dpp, *dp, *dq;
132
133 dpp = &idvhashtbl[DNOHASH(device, inum)];
134 for (dp = *dpp;; dp = dp->d_next) {
135 if (dp == NULL)
136 return (NULL);
137 if (inum == dp->i_number && device == dp->i_dev)
138 return (dp);
139
140 if (!create)
141 return (NULL);
142
143 MALLOC(dp, struct iso_dnode *, sizeof(struct iso_dnode), M_CACHE,
144 M_WAITOK);
145 dp->i_dev = dev;
146 dp->i_number = ino;
147
148 if (dq = *dpp)
149 dq->d_prev = dp->d_next;
150 dp->d_next = dq;
151 dp->d_prev = dpp;
152 *dpp = dp;
153
154 return (dp);
155}
156
157void
158iso_dunmap(device)
159 dev_t device;
160{
161 struct iso_dnode **dpp, *dp, *dq;
162
163 for (dpp = idvhashtbl; dpp <= idvhashtbl + idvhash; dpp++) {
164 for (dp = *dpp; dp != NULL; dp = dq)
165 dq = dp->d_next;
166 if (device == dp->i_dev) {
167 if (dq)
168 dq->d_prev = dp->d_prev;
169 *dp->d_prev = dq;
170 FREE(dp, M_CACHE);
171 }
172 }
173 }
174}
175#endif
176
177/*
178 * Use the device/inum pair to find the incore inode, and return a pointer
179 * to it. If it is in core, but locked, wait for it.
180 */
181struct vnode *
182cd9660_ihashget(device, inum, p)
183 dev_t device;
184 ino_t inum;
185 struct proc *p;
186{
187 register struct iso_node *ip;
188 struct vnode *vp;
189
190 for (;;)
191 for (ip = isohashtbl[INOHASH(device, inum)];; ip = ip->i_next) {
192 if (ip == NULL)
193 return (NULL);
194 if (inum == ip->i_number && device == ip->i_dev) {
195 /*
196 * This is my most dangerous change. I am not waiting for
197 * the inode lock anymore (ufs doesn't, why should we) and
198 * I'm worried because there is not lock on the hashtable,
199 * but there wasn't before so I'll let it go for now.
200 * -- chw --
201 */
202 vp = ITOV(ip);
203 simple_lock(&vp->v_interlock);
204 if (!vget(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY, p))
205 return (vp);
206 break;
207 }
208 }
209 /* NOTREACHED */
210}
211
212/*
213 * Insert the inode into the hash table, and return it locked.
214 */
215void
216cd9660_ihashins(ip)
217 struct iso_node *ip;
218{
219 struct iso_node **ipp, *iq;
220 struct proc *p = current_proc();
221
222 /* lock the inode, then put it on the appropriate hash list */
223 lockmgr(&ip->i_lock, LK_EXCLUSIVE, (struct slock *)0, p);
224
225 ipp = &isohashtbl[INOHASH(ip->i_dev, ip->i_number)];
226 if ((iq = *ipp))
227 iq->i_prev = &ip->i_next;
228 ip->i_next = iq;
229 ip->i_prev = ipp;
230 *ipp = ip;
231 }
232
233/*
234 * Remove the inode from the hash table.
235 */
236void
237cd9660_ihashrem(ip)
238 register struct iso_node *ip;
239{
240 register struct iso_node *iq;
241
242 if ((iq = ip->i_next))
243 iq->i_prev = ip->i_prev;
244 *ip->i_prev = iq;
245#if 1 /* was ifdef DIAGNOSTIC */
246 ip->i_next = NULL;
247 ip->i_prev = NULL;
248#endif
249}
250
251/*
252 * Last reference to an inode, write the inode out and if necessary,
253 * truncate and deallocate the file.
254 */
255int
256cd9660_inactive(ap)
257 struct vop_inactive_args /* {
258 struct vnode *a_vp;
259 struct proc *a_p;
260 } */ *ap;
261{
262 struct vnode *vp = ap->a_vp;
263 struct proc *p = ap->a_p;
264 register struct iso_node *ip = VTOI(vp);
265 int error = 0;
266
267 if (prtactive && vp->v_usecount != 0)
268 vprint("cd9660_inactive: pushing active", vp);
269 /*
270 * We need to unlock the inode here. If we don't panics or
271 * hangs will ensue. Our callers expect us to take care of this.
272 */
273
274 VOP_UNLOCK(vp,0,p);
275
276 /*
277 * If we are done with the inode, reclaim it
278 * so that it can be reused immediately.
279 */
280 if (vp->v_usecount == 0 && ip->inode.iso_mode == 0)
281 vgone(vp);
282
283 return error;
284}
285
286/*
287 * Reclaim an inode so that it can be used for other purposes.
288 */
289int
290cd9660_reclaim(ap)
291 struct vop_reclaim_args /* {
292 struct vnode *a_vp;
293 } */ *ap;
294{
295 register struct vnode *vp = ap->a_vp;
296 register struct iso_node *ip = VTOI(vp);
297
298 if (prtactive && vp->v_usecount != 0)
299 vprint("cd9660_reclaim: pushing active", vp);
300 /*
301 * Remove the inode from its hash chain.
302 */
303 cd9660_ihashrem(ip);
304 /*
305 * Purge old data structures associated with the inode.
306 */
307 cache_purge(vp);
308 if (ip->i_devvp) {
fa4905b1
A
309 struct vnode *tvp = ip->i_devvp;
310 ip->i_devvp = NULL;
311 vrele(tvp);
1c79356b
A
312 }
313 if (ip->i_namep != isonullname)
314 FREE(ip->i_namep, M_TEMP);
315 FREE_ZONE(vp->v_data, sizeof(struct iso_node), M_ISOFSNODE);
316 vp->v_data = NULL;
317 return (0);
318}
319
320/*
321 * File attributes
322 */
323void
324cd9660_defattr(isodir, inop, bp)
325 struct iso_directory_record *isodir;
326 struct iso_node *inop;
327 struct buf *bp;
328{
329 struct buf *bp2 = NULL;
330 struct iso_mnt *imp;
331 struct iso_extended_attributes *ap = NULL;
332 int off;
333
334 if ( isonum_711(isodir->flags) & directoryBit ) {
335 inop->inode.iso_mode = S_IFDIR;
336 /*
337 * If we return 2, fts() will assume there are no subdirectories
338 * (just links for the path and .), so instead we return 1.
339 */
340 inop->inode.iso_links = 1;
341 } else {
342 inop->inode.iso_mode = S_IFREG;
343 inop->inode.iso_links = 1;
344 }
345 if (!bp
346 && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
347 && (off = isonum_711(isodir->ext_attr_length))) {
348 VOP_BLKATOFF(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL,
349 &bp2);
350 bp = bp2;
351 }
352 if (bp) {
353 ap = (struct iso_extended_attributes *)bp->b_data;
354
355 if (isonum_711(ap->version) == 1) {
356 if (!(ap->perm[0]&0x40))
357 inop->inode.iso_mode |= VEXEC >> 6;
358 if (!(ap->perm[0]&0x10))
359 inop->inode.iso_mode |= VREAD >> 6;
360 if (!(ap->perm[0]&4))
361 inop->inode.iso_mode |= VEXEC >> 3;
362 if (!(ap->perm[0]&1))
363 inop->inode.iso_mode |= VREAD >> 3;
364 if (!(ap->perm[1]&0x40))
365 inop->inode.iso_mode |= VEXEC;
366 if (!(ap->perm[1]&0x10))
367 inop->inode.iso_mode |= VREAD;
368 inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */
369 inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */
370 } else
371 ap = NULL;
372 }
373 if (!ap) {
374 inop->inode.iso_mode |= VREAD|VEXEC|(VREAD|VEXEC)>>3|(VREAD|VEXEC)>>6;
375 inop->inode.iso_uid = (uid_t)0;
376 inop->inode.iso_gid = (gid_t)0;
377 }
378 if (bp2)
379 brelse(bp2);
380}
381
382/*
383 * Time stamps
384 */
385void
386cd9660_deftstamp(isodir,inop,bp)
387 struct iso_directory_record *isodir;
388 struct iso_node *inop;
389 struct buf *bp;
390{
391 struct buf *bp2 = NULL;
392 struct iso_mnt *imp;
393 struct iso_extended_attributes *ap = NULL;
394 int off;
395
396 if (!bp
397 && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
398 && (off = isonum_711(isodir->ext_attr_length)))
399 {
400 VOP_BLKATOFF(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL, &bp2);
401 bp = bp2;
402 }
403 if (bp) {
404 ap = (struct iso_extended_attributes *)bp->b_data;
405
406 if (isonum_711(ap->version) == 1) {
407 if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime))
408 cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime);
409 if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime))
410 inop->inode.iso_ctime = inop->inode.iso_atime;
411 if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime))
412 inop->inode.iso_mtime = inop->inode.iso_ctime;
413 } else
414 ap = NULL;
415 }
416 if (!ap) {
417 cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime);
418 inop->inode.iso_atime = inop->inode.iso_ctime;
419 inop->inode.iso_mtime = inop->inode.iso_ctime;
420 }
421 if (bp2)
422 brelse(bp2);
423}
424
425int
426cd9660_tstamp_conv7(pi,pu)
427 u_char *pi;
428 struct timespec *pu;
429{
430 int crtime, days;
431 int y, m, d, hour, minute, second, tz;
432
433 y = pi[0] + 1900;
434 m = pi[1];
435 d = pi[2];
436 hour = pi[3];
437 minute = pi[4];
438 second = pi[5];
439 tz = pi[6];
440
441 if (y < 1970) {
442 pu->tv_sec = 0;
443 pu->tv_nsec = 0;
444 return 0;
445 } else {
446#ifdef ORIGINAL
447 /* computes day number relative to Sept. 19th,1989 */
448 /* don't even *THINK* about changing formula. It works! */
449 days = 367*(y-1980)-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d-100;
450#else
451 /*
452 * Changed :-) to make it relative to Jan. 1st, 1970
453 * and to disambiguate negative division
454 */
455 days = 367*(y-1960)-7*(y+(m+9)/12)/4-3*((y+(m+9)/12-1)/100+1)/4+275*m/9+d-239;
456#endif
457 crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second;
458
459 /* timezone offset is unreliable on some disks */
460 if (-48 <= tz && tz <= 52)
461 crtime -= tz * 15 * 60;
462 }
463 pu->tv_sec = crtime;
464 pu->tv_nsec = 0;
465 return 1;
466}
467
468static u_int
469cd9660_chars2ui(begin,len)
470 u_char *begin;
471 int len;
472{
473 u_int rc;
474
475 for (rc = 0; --len >= 0;) {
476 rc *= 10;
477 rc += *begin++ - '0';
478 }
479 return rc;
480}
481
482int
483cd9660_tstamp_conv17(pi,pu)
484 u_char *pi;
485 struct timespec *pu;
486{
487 u_char buf[7];
488
489 /* year:"0001"-"9999" -> -1900 */
490 buf[0] = cd9660_chars2ui(pi,4) - 1900;
491
492 /* month: " 1"-"12" -> 1 - 12 */
493 buf[1] = cd9660_chars2ui(pi + 4,2);
494
495 /* day: " 1"-"31" -> 1 - 31 */
496 buf[2] = cd9660_chars2ui(pi + 6,2);
497
498 /* hour: " 0"-"23" -> 0 - 23 */
499 buf[3] = cd9660_chars2ui(pi + 8,2);
500
501 /* minute:" 0"-"59" -> 0 - 59 */
502 buf[4] = cd9660_chars2ui(pi + 10,2);
503
504 /* second:" 0"-"59" -> 0 - 59 */
505 buf[5] = cd9660_chars2ui(pi + 12,2);
506
507 /* difference of GMT */
508 buf[6] = pi[16];
509
510 return cd9660_tstamp_conv7(buf,pu);
511}
512
513ino_t
514isodirino(isodir, imp)
515 struct iso_directory_record *isodir;
516 struct iso_mnt *imp;
517{
518 ino_t ino;
519
520 ino = (isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length))
521 << imp->im_bshift;
522 return (ino);
523}