]>
git.saurik.com Git - apple/xnu.git/blob - bsd/isofs/cd9660/cd9660_node.c
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* $NetBSD: cd9660_node.c,v 1.13 1994/12/24 15:30:07 cgd Exp $ */
25 * Copyright (c) 1982, 1986, 1989, 1994
26 * The Regents of the University of California. All rights reserved.
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).
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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.
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
61 * @(#)cd9660_node.c 8.5 (Berkeley) 12/5/94
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
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/mount.h>
77 #include <sys/vnode.h>
78 #include <sys/kernel.h>
79 #include <sys/malloc.h>
82 #include <sys/namei.h>
84 #include <isofs/cd9660/iso.h>
85 #include <isofs/cd9660/cd9660_node.h>
86 #include <isofs/cd9660/iso_rrip.h>
87 #include <isofs/cd9660/cd9660_mount.h>
90 * Structures associated with iso_node caching.
92 struct iso_node
**isohashtbl
;
94 #define INOHASH(device, inum) (((device) + ((inum)>>12)) & isohash)
97 struct iso_node
**idvhashtbl
;
99 #define DNOHASH(device, inum) (((device) + ((inum)>>12)) & idvhash)
102 /* defined in bsd/vfs/vfs_subr.c */
103 extern int prtactive
; /* 1 => print out reclaim of active vnodes */
105 extern u_char isonullname
[];
107 * Initialize hash links for inodes and dnodes.
113 isohashtbl
= hashinit(desiredvnodes
, M_ISOFSMNT
, &isohash
);
115 idvhashtbl
= hashinit(desiredvnodes
/ 8, M_ISOFSMNT
, &idvhash
);
122 * Enter a new node into the device hash list
125 iso_dmap(device
, inum
, create
)
130 register struct iso_dnode
**dpp
, *dp
, *dq
;
132 dpp
= &idvhashtbl
[DNOHASH(device
, inum
)];
133 for (dp
= *dpp
;; dp
= dp
->d_next
) {
136 if (inum
== dp
->i_number
&& device
== dp
->i_dev
)
142 MALLOC(dp
, struct iso_dnode
*, sizeof(struct iso_dnode
), M_CACHE
,
148 dq
->d_prev
= dp
->d_next
;
160 struct iso_dnode
**dpp
, *dp
, *dq
;
162 for (dpp
= idvhashtbl
; dpp
<= idvhashtbl
+ idvhash
; dpp
++) {
163 for (dp
= *dpp
; dp
!= NULL
; dp
= dq
)
165 if (device
== dp
->i_dev
) {
167 dq
->d_prev
= dp
->d_prev
;
177 * Use the device/inum pair to find the incore inode, and return a pointer
178 * to it. If it is in core, but locked, wait for it.
181 cd9660_ihashget(device
, inum
, p
)
186 register struct iso_node
*ip
;
190 for (ip
= isohashtbl
[INOHASH(device
, inum
)];; ip
= ip
->i_next
) {
193 if (inum
== ip
->i_number
&& device
== ip
->i_dev
) {
195 * This is my most dangerous change. I am not waiting for
196 * the inode lock anymore (ufs doesn't, why should we) and
197 * I'm worried because there is not lock on the hashtable,
198 * but there wasn't before so I'll let it go for now.
202 simple_lock(&vp
->v_interlock
);
203 if (!vget(vp
, LK_EXCLUSIVE
| LK_INTERLOCK
| LK_RETRY
, p
))
212 * Insert the inode into the hash table, and return it locked.
218 struct iso_node
**ipp
, *iq
;
219 struct proc
*p
= current_proc();
221 /* lock the inode, then put it on the appropriate hash list */
222 lockmgr(&ip
->i_lock
, LK_EXCLUSIVE
, (struct slock
*)0, p
);
224 ipp
= &isohashtbl
[INOHASH(ip
->i_dev
, ip
->i_number
)];
226 iq
->i_prev
= &ip
->i_next
;
233 * Remove the inode from the hash table.
237 register struct iso_node
*ip
;
239 register struct iso_node
*iq
;
241 if ((iq
= ip
->i_next
))
242 iq
->i_prev
= ip
->i_prev
;
244 #if 1 /* was ifdef DIAGNOSTIC */
251 * Last reference to an inode, write the inode out and if necessary,
252 * truncate and deallocate the file.
256 struct vop_inactive_args
/* {
261 struct vnode
*vp
= ap
->a_vp
;
262 struct proc
*p
= ap
->a_p
;
263 register struct iso_node
*ip
= VTOI(vp
);
266 if (prtactive
&& vp
->v_usecount
!= 0)
267 vprint("cd9660_inactive: pushing active", vp
);
269 * We need to unlock the inode here. If we don't panics or
270 * hangs will ensue. Our callers expect us to take care of this.
276 * If we are done with the inode, reclaim it
277 * so that it can be reused immediately.
279 if (vp
->v_usecount
== 0 && ip
->inode
.iso_mode
== 0)
286 * Reclaim an inode so that it can be used for other purposes.
290 struct vop_reclaim_args
/* {
294 register struct vnode
*vp
= ap
->a_vp
;
295 register struct iso_node
*ip
= VTOI(vp
);
297 if (prtactive
&& vp
->v_usecount
!= 0)
298 vprint("cd9660_reclaim: pushing active", vp
);
300 * Remove the inode from its hash chain.
304 * Purge old data structures associated with the inode.
308 struct vnode
*tvp
= ip
->i_devvp
;
312 if (ip
->i_namep
!= isonullname
)
313 FREE(ip
->i_namep
, M_TEMP
);
314 if (ip
->i_riff
!= NULL
)
315 FREE(ip
->i_riff
, M_TEMP
);
316 FREE_ZONE(vp
->v_data
, sizeof(struct iso_node
), M_ISOFSNODE
);
325 cd9660_defattr(isodir
, inop
, bp
)
326 struct iso_directory_record
*isodir
;
327 struct iso_node
*inop
;
330 struct buf
*bp2
= NULL
;
332 struct iso_extended_attributes
*ap
= NULL
;
335 if ( isonum_711(isodir
->flags
) & directoryBit
) {
336 inop
->inode
.iso_mode
= S_IFDIR
;
338 * If we return 2, fts() will assume there are no subdirectories
339 * (just links for the path and .), so instead we return 1.
341 inop
->inode
.iso_links
= 1;
343 inop
->inode
.iso_mode
= S_IFREG
;
344 inop
->inode
.iso_links
= 1;
347 && ((imp
= inop
->i_mnt
)->im_flags
& ISOFSMNT_EXTATT
)
348 && (off
= isonum_711(isodir
->ext_attr_length
))) {
349 VOP_BLKATOFF(ITOV(inop
), (off_t
)-(off
<< imp
->im_bshift
), NULL
,
354 ap
= (struct iso_extended_attributes
*)bp
->b_data
;
356 if (isonum_711(ap
->version
) == 1) {
357 if (!(ap
->perm
[0]&0x40))
358 inop
->inode
.iso_mode
|= VEXEC
>> 6;
359 if (!(ap
->perm
[0]&0x10))
360 inop
->inode
.iso_mode
|= VREAD
>> 6;
361 if (!(ap
->perm
[0]&4))
362 inop
->inode
.iso_mode
|= VEXEC
>> 3;
363 if (!(ap
->perm
[0]&1))
364 inop
->inode
.iso_mode
|= VREAD
>> 3;
365 if (!(ap
->perm
[1]&0x40))
366 inop
->inode
.iso_mode
|= VEXEC
;
367 if (!(ap
->perm
[1]&0x10))
368 inop
->inode
.iso_mode
|= VREAD
;
369 inop
->inode
.iso_uid
= isonum_723(ap
->owner
); /* what about 0? */
370 inop
->inode
.iso_gid
= isonum_723(ap
->group
); /* what about 0? */
375 inop
->inode
.iso_mode
|= VREAD
|VEXEC
|(VREAD
|VEXEC
)>>3|(VREAD
|VEXEC
)>>6;
376 inop
->inode
.iso_uid
= (uid_t
)0;
377 inop
->inode
.iso_gid
= (gid_t
)0;
387 cd9660_deftstamp(isodir
,inop
,bp
)
388 struct iso_directory_record
*isodir
;
389 struct iso_node
*inop
;
392 struct buf
*bp2
= NULL
;
394 struct iso_extended_attributes
*ap
= NULL
;
398 && ((imp
= inop
->i_mnt
)->im_flags
& ISOFSMNT_EXTATT
)
399 && (off
= isonum_711(isodir
->ext_attr_length
)))
401 VOP_BLKATOFF(ITOV(inop
), (off_t
)-(off
<< imp
->im_bshift
), NULL
, &bp2
);
405 ap
= (struct iso_extended_attributes
*)bp
->b_data
;
407 if (isonum_711(ap
->version
) == 1) {
408 if (!cd9660_tstamp_conv17(ap
->ftime
,&inop
->inode
.iso_atime
))
409 cd9660_tstamp_conv17(ap
->ctime
,&inop
->inode
.iso_atime
);
410 if (!cd9660_tstamp_conv17(ap
->ctime
,&inop
->inode
.iso_ctime
))
411 inop
->inode
.iso_ctime
= inop
->inode
.iso_atime
;
412 if (!cd9660_tstamp_conv17(ap
->mtime
,&inop
->inode
.iso_mtime
))
413 inop
->inode
.iso_mtime
= inop
->inode
.iso_ctime
;
418 cd9660_tstamp_conv7(isodir
->date
,&inop
->inode
.iso_ctime
);
419 inop
->inode
.iso_atime
= inop
->inode
.iso_ctime
;
420 inop
->inode
.iso_mtime
= inop
->inode
.iso_ctime
;
427 cd9660_tstamp_conv7(pi
,pu
)
432 int y
, m
, d
, hour
, minute
, second
, tz
;
448 /* computes day number relative to Sept. 19th,1989 */
449 /* don't even *THINK* about changing formula. It works! */
450 days
= 367*(y
-1980)-7*(y
+(m
+9)/12)/4-3*((y
+(m
-9)/7)/100+1)/4+275*m
/9+d
-100;
453 * Changed :-) to make it relative to Jan. 1st, 1970
454 * and to disambiguate negative division
456 days
= 367*(y
-1960)-7*(y
+(m
+9)/12)/4-3*((y
+(m
+9)/12-1)/100+1)/4+275*m
/9+d
-239;
458 crtime
= ((((days
* 24) + hour
) * 60 + minute
) * 60) + second
;
460 /* timezone offset is unreliable on some disks */
461 if (-48 <= tz
&& tz
<= 52)
462 crtime
-= tz
* 15 * 60;
470 cd9660_chars2ui(begin
,len
)
476 for (rc
= 0; --len
>= 0;) {
478 rc
+= *begin
++ - '0';
484 cd9660_tstamp_conv17(pi
,pu
)
490 /* year:"0001"-"9999" -> -1900 */
491 buf
[0] = cd9660_chars2ui(pi
,4) - 1900;
493 /* month: " 1"-"12" -> 1 - 12 */
494 buf
[1] = cd9660_chars2ui(pi
+ 4,2);
496 /* day: " 1"-"31" -> 1 - 31 */
497 buf
[2] = cd9660_chars2ui(pi
+ 6,2);
499 /* hour: " 0"-"23" -> 0 - 23 */
500 buf
[3] = cd9660_chars2ui(pi
+ 8,2);
502 /* minute:" 0"-"59" -> 0 - 59 */
503 buf
[4] = cd9660_chars2ui(pi
+ 10,2);
505 /* second:" 0"-"59" -> 0 - 59 */
506 buf
[5] = cd9660_chars2ui(pi
+ 12,2);
508 /* difference of GMT */
511 return cd9660_tstamp_conv7(buf
,pu
);
515 isodirino(isodir
, imp
)
516 struct iso_directory_record
*isodir
;
521 ino
= (isonum_733(isodir
->extent
) + isonum_711(isodir
->ext_attr_length
))