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