]> git.saurik.com Git - apple/xnu.git/blame - bsd/isofs/cd9660/cd9660_node.c
xnu-517.tar.gz
[apple/xnu.git] / bsd / isofs / cd9660 / cd9660_node.c
CommitLineData
1c79356b 1/*
55e303ae 2 * Copyright (c) 2000-2003 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>
55e303ae 85#include <sys/namei.h>
1c79356b
A
86
87#include <isofs/cd9660/iso.h>
88#include <isofs/cd9660/cd9660_node.h>
89#include <isofs/cd9660/iso_rrip.h>
90#include <isofs/cd9660/cd9660_mount.h>
91
92/*
93 * Structures associated with iso_node caching.
94 */
95struct iso_node **isohashtbl;
96u_long isohash;
97#define INOHASH(device, inum) (((device) + ((inum)>>12)) & isohash)
98
99#ifdef ISODEVMAP
100struct iso_node **idvhashtbl;
101u_long idvhash;
102#define DNOHASH(device, inum) (((device) + ((inum)>>12)) & idvhash)
103#endif
104
55e303ae 105/* defined in bsd/vfs/vfs_subr.c */
1c79356b
A
106extern int prtactive; /* 1 => print out reclaim of active vnodes */
107
1c79356b
A
108extern u_char isonullname[];
109/*
110 * Initialize hash links for inodes and dnodes.
111 */
112int
113cd9660_init()
114{
115
116 isohashtbl = hashinit(desiredvnodes, M_ISOFSMNT, &isohash);
117#ifdef ISODEVMAP
118 idvhashtbl = hashinit(desiredvnodes / 8, M_ISOFSMNT, &idvhash);
119#endif
120 return 0;
121}
122
123#ifdef ISODEVMAP
124/*
125 * Enter a new node into the device hash list
126 */
127struct iso_dnode *
128iso_dmap(device, inum, create)
129 dev_t device;
130 ino_t inum;
131 int create;
132{
133 register struct iso_dnode **dpp, *dp, *dq;
134
135 dpp = &idvhashtbl[DNOHASH(device, inum)];
136 for (dp = *dpp;; dp = dp->d_next) {
137 if (dp == NULL)
138 return (NULL);
139 if (inum == dp->i_number && device == dp->i_dev)
140 return (dp);
141
142 if (!create)
143 return (NULL);
144
145 MALLOC(dp, struct iso_dnode *, sizeof(struct iso_dnode), M_CACHE,
146 M_WAITOK);
147 dp->i_dev = dev;
148 dp->i_number = ino;
149
150 if (dq = *dpp)
151 dq->d_prev = dp->d_next;
152 dp->d_next = dq;
153 dp->d_prev = dpp;
154 *dpp = dp;
155
156 return (dp);
157}
158
159void
160iso_dunmap(device)
161 dev_t device;
162{
163 struct iso_dnode **dpp, *dp, *dq;
164
165 for (dpp = idvhashtbl; dpp <= idvhashtbl + idvhash; dpp++) {
166 for (dp = *dpp; dp != NULL; dp = dq)
167 dq = dp->d_next;
168 if (device == dp->i_dev) {
169 if (dq)
170 dq->d_prev = dp->d_prev;
171 *dp->d_prev = dq;
172 FREE(dp, M_CACHE);
173 }
174 }
175 }
176}
177#endif
178
179/*
180 * Use the device/inum pair to find the incore inode, and return a pointer
181 * to it. If it is in core, but locked, wait for it.
182 */
183struct vnode *
184cd9660_ihashget(device, inum, p)
185 dev_t device;
186 ino_t inum;
187 struct proc *p;
188{
189 register struct iso_node *ip;
190 struct vnode *vp;
191
192 for (;;)
193 for (ip = isohashtbl[INOHASH(device, inum)];; ip = ip->i_next) {
194 if (ip == NULL)
195 return (NULL);
196 if (inum == ip->i_number && device == ip->i_dev) {
197 /*
198 * This is my most dangerous change. I am not waiting for
199 * the inode lock anymore (ufs doesn't, why should we) and
200 * I'm worried because there is not lock on the hashtable,
201 * but there wasn't before so I'll let it go for now.
202 * -- chw --
203 */
204 vp = ITOV(ip);
205 simple_lock(&vp->v_interlock);
206 if (!vget(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY, p))
207 return (vp);
208 break;
209 }
210 }
211 /* NOTREACHED */
212}
213
214/*
215 * Insert the inode into the hash table, and return it locked.
216 */
217void
218cd9660_ihashins(ip)
219 struct iso_node *ip;
220{
221 struct iso_node **ipp, *iq;
222 struct proc *p = current_proc();
223
224 /* lock the inode, then put it on the appropriate hash list */
225 lockmgr(&ip->i_lock, LK_EXCLUSIVE, (struct slock *)0, p);
226
227 ipp = &isohashtbl[INOHASH(ip->i_dev, ip->i_number)];
228 if ((iq = *ipp))
229 iq->i_prev = &ip->i_next;
230 ip->i_next = iq;
231 ip->i_prev = ipp;
232 *ipp = ip;
233 }
234
235/*
236 * Remove the inode from the hash table.
237 */
238void
239cd9660_ihashrem(ip)
240 register struct iso_node *ip;
241{
242 register struct iso_node *iq;
243
244 if ((iq = ip->i_next))
245 iq->i_prev = ip->i_prev;
246 *ip->i_prev = iq;
247#if 1 /* was ifdef DIAGNOSTIC */
248 ip->i_next = NULL;
249 ip->i_prev = NULL;
250#endif
251}
252
253/*
254 * Last reference to an inode, write the inode out and if necessary,
255 * truncate and deallocate the file.
256 */
257int
258cd9660_inactive(ap)
259 struct vop_inactive_args /* {
260 struct vnode *a_vp;
261 struct proc *a_p;
262 } */ *ap;
263{
264 struct vnode *vp = ap->a_vp;
265 struct proc *p = ap->a_p;
266 register struct iso_node *ip = VTOI(vp);
267 int error = 0;
268
269 if (prtactive && vp->v_usecount != 0)
270 vprint("cd9660_inactive: pushing active", vp);
271 /*
272 * We need to unlock the inode here. If we don't panics or
273 * hangs will ensue. Our callers expect us to take care of this.
274 */
275
276 VOP_UNLOCK(vp,0,p);
277
278 /*
279 * If we are done with the inode, reclaim it
280 * so that it can be reused immediately.
281 */
282 if (vp->v_usecount == 0 && ip->inode.iso_mode == 0)
283 vgone(vp);
284
285 return error;
286}
287
288/*
289 * Reclaim an inode so that it can be used for other purposes.
290 */
291int
292cd9660_reclaim(ap)
293 struct vop_reclaim_args /* {
294 struct vnode *a_vp;
295 } */ *ap;
296{
297 register struct vnode *vp = ap->a_vp;
298 register struct iso_node *ip = VTOI(vp);
299
300 if (prtactive && vp->v_usecount != 0)
301 vprint("cd9660_reclaim: pushing active", vp);
302 /*
303 * Remove the inode from its hash chain.
304 */
305 cd9660_ihashrem(ip);
306 /*
307 * Purge old data structures associated with the inode.
308 */
309 cache_purge(vp);
310 if (ip->i_devvp) {
fa4905b1
A
311 struct vnode *tvp = ip->i_devvp;
312 ip->i_devvp = NULL;
313 vrele(tvp);
1c79356b
A
314 }
315 if (ip->i_namep != isonullname)
316 FREE(ip->i_namep, M_TEMP);
55e303ae
A
317 if (ip->i_riff != NULL)
318 FREE(ip->i_riff, M_TEMP);
1c79356b
A
319 FREE_ZONE(vp->v_data, sizeof(struct iso_node), M_ISOFSNODE);
320 vp->v_data = NULL;
321 return (0);
322}
323
324/*
325 * File attributes
326 */
327void
328cd9660_defattr(isodir, inop, bp)
329 struct iso_directory_record *isodir;
330 struct iso_node *inop;
331 struct buf *bp;
332{
333 struct buf *bp2 = NULL;
334 struct iso_mnt *imp;
335 struct iso_extended_attributes *ap = NULL;
336 int off;
337
338 if ( isonum_711(isodir->flags) & directoryBit ) {
339 inop->inode.iso_mode = S_IFDIR;
340 /*
341 * If we return 2, fts() will assume there are no subdirectories
342 * (just links for the path and .), so instead we return 1.
343 */
344 inop->inode.iso_links = 1;
345 } else {
346 inop->inode.iso_mode = S_IFREG;
347 inop->inode.iso_links = 1;
348 }
349 if (!bp
350 && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
351 && (off = isonum_711(isodir->ext_attr_length))) {
352 VOP_BLKATOFF(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL,
353 &bp2);
354 bp = bp2;
355 }
356 if (bp) {
357 ap = (struct iso_extended_attributes *)bp->b_data;
358
359 if (isonum_711(ap->version) == 1) {
360 if (!(ap->perm[0]&0x40))
361 inop->inode.iso_mode |= VEXEC >> 6;
362 if (!(ap->perm[0]&0x10))
363 inop->inode.iso_mode |= VREAD >> 6;
364 if (!(ap->perm[0]&4))
365 inop->inode.iso_mode |= VEXEC >> 3;
366 if (!(ap->perm[0]&1))
367 inop->inode.iso_mode |= VREAD >> 3;
368 if (!(ap->perm[1]&0x40))
369 inop->inode.iso_mode |= VEXEC;
370 if (!(ap->perm[1]&0x10))
371 inop->inode.iso_mode |= VREAD;
372 inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */
373 inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */
374 } else
375 ap = NULL;
376 }
377 if (!ap) {
378 inop->inode.iso_mode |= VREAD|VEXEC|(VREAD|VEXEC)>>3|(VREAD|VEXEC)>>6;
379 inop->inode.iso_uid = (uid_t)0;
380 inop->inode.iso_gid = (gid_t)0;
381 }
382 if (bp2)
383 brelse(bp2);
384}
385
386/*
387 * Time stamps
388 */
389void
390cd9660_deftstamp(isodir,inop,bp)
391 struct iso_directory_record *isodir;
392 struct iso_node *inop;
393 struct buf *bp;
394{
395 struct buf *bp2 = NULL;
396 struct iso_mnt *imp;
397 struct iso_extended_attributes *ap = NULL;
398 int off;
399
400 if (!bp
401 && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
402 && (off = isonum_711(isodir->ext_attr_length)))
403 {
404 VOP_BLKATOFF(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL, &bp2);
405 bp = bp2;
406 }
407 if (bp) {
408 ap = (struct iso_extended_attributes *)bp->b_data;
409
410 if (isonum_711(ap->version) == 1) {
411 if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime))
412 cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime);
413 if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime))
414 inop->inode.iso_ctime = inop->inode.iso_atime;
415 if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime))
416 inop->inode.iso_mtime = inop->inode.iso_ctime;
417 } else
418 ap = NULL;
419 }
420 if (!ap) {
421 cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime);
422 inop->inode.iso_atime = inop->inode.iso_ctime;
423 inop->inode.iso_mtime = inop->inode.iso_ctime;
424 }
425 if (bp2)
426 brelse(bp2);
427}
428
429int
430cd9660_tstamp_conv7(pi,pu)
431 u_char *pi;
432 struct timespec *pu;
433{
434 int crtime, days;
435 int y, m, d, hour, minute, second, tz;
436
437 y = pi[0] + 1900;
438 m = pi[1];
439 d = pi[2];
440 hour = pi[3];
441 minute = pi[4];
442 second = pi[5];
443 tz = pi[6];
444
445 if (y < 1970) {
446 pu->tv_sec = 0;
447 pu->tv_nsec = 0;
448 return 0;
449 } else {
450#ifdef ORIGINAL
451 /* computes day number relative to Sept. 19th,1989 */
452 /* don't even *THINK* about changing formula. It works! */
453 days = 367*(y-1980)-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d-100;
454#else
455 /*
456 * Changed :-) to make it relative to Jan. 1st, 1970
457 * and to disambiguate negative division
458 */
459 days = 367*(y-1960)-7*(y+(m+9)/12)/4-3*((y+(m+9)/12-1)/100+1)/4+275*m/9+d-239;
460#endif
461 crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second;
462
463 /* timezone offset is unreliable on some disks */
464 if (-48 <= tz && tz <= 52)
465 crtime -= tz * 15 * 60;
466 }
467 pu->tv_sec = crtime;
468 pu->tv_nsec = 0;
469 return 1;
470}
471
472static u_int
473cd9660_chars2ui(begin,len)
474 u_char *begin;
475 int len;
476{
477 u_int rc;
478
479 for (rc = 0; --len >= 0;) {
480 rc *= 10;
481 rc += *begin++ - '0';
482 }
483 return rc;
484}
485
486int
487cd9660_tstamp_conv17(pi,pu)
488 u_char *pi;
489 struct timespec *pu;
490{
491 u_char buf[7];
492
493 /* year:"0001"-"9999" -> -1900 */
494 buf[0] = cd9660_chars2ui(pi,4) - 1900;
495
496 /* month: " 1"-"12" -> 1 - 12 */
497 buf[1] = cd9660_chars2ui(pi + 4,2);
498
499 /* day: " 1"-"31" -> 1 - 31 */
500 buf[2] = cd9660_chars2ui(pi + 6,2);
501
502 /* hour: " 0"-"23" -> 0 - 23 */
503 buf[3] = cd9660_chars2ui(pi + 8,2);
504
505 /* minute:" 0"-"59" -> 0 - 59 */
506 buf[4] = cd9660_chars2ui(pi + 10,2);
507
508 /* second:" 0"-"59" -> 0 - 59 */
509 buf[5] = cd9660_chars2ui(pi + 12,2);
510
511 /* difference of GMT */
512 buf[6] = pi[16];
513
514 return cd9660_tstamp_conv7(buf,pu);
515}
516
517ino_t
518isodirino(isodir, imp)
519 struct iso_directory_record *isodir;
520 struct iso_mnt *imp;
521{
522 ino_t ino;
523
524 ino = (isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length))
525 << imp->im_bshift;
526 return (ino);
527}