]>
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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
23 * @APPLE_LICENSE_HEADER_END@
25 /* $NetBSD: cd9660_node.c,v 1.13 1994/12/24 15:30:07 cgd Exp $ */
28 * Copyright (c) 1982, 1986, 1989, 1994
29 * The Regents of the University of California. All rights reserved.
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).
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
64 * @(#)cd9660_node.c 8.5 (Berkeley) 12/5/94
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
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/mount.h>
80 #include <sys/vnode.h>
81 #include <sys/kernel.h>
82 #include <sys/malloc.h>
85 #include <sys/namei.h>
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>
93 * Structures associated with iso_node caching.
95 struct iso_node
**isohashtbl
;
97 #define INOHASH(device, inum) (((device) + ((inum)>>12)) & isohash)
100 struct iso_node
**idvhashtbl
;
102 #define DNOHASH(device, inum) (((device) + ((inum)>>12)) & idvhash)
105 /* defined in bsd/vfs/vfs_subr.c */
106 extern int prtactive
; /* 1 => print out reclaim of active vnodes */
108 extern u_char isonullname
[];
110 * Initialize hash links for inodes and dnodes.
116 isohashtbl
= hashinit(desiredvnodes
, M_ISOFSMNT
, &isohash
);
118 idvhashtbl
= hashinit(desiredvnodes
/ 8, M_ISOFSMNT
, &idvhash
);
125 * Enter a new node into the device hash list
128 iso_dmap(device
, inum
, create
)
133 register struct iso_dnode
**dpp
, *dp
, *dq
;
135 dpp
= &idvhashtbl
[DNOHASH(device
, inum
)];
136 for (dp
= *dpp
;; dp
= dp
->d_next
) {
139 if (inum
== dp
->i_number
&& device
== dp
->i_dev
)
145 MALLOC(dp
, struct iso_dnode
*, sizeof(struct iso_dnode
), M_CACHE
,
151 dq
->d_prev
= dp
->d_next
;
163 struct iso_dnode
**dpp
, *dp
, *dq
;
165 for (dpp
= idvhashtbl
; dpp
<= idvhashtbl
+ idvhash
; dpp
++) {
166 for (dp
= *dpp
; dp
!= NULL
; dp
= dq
)
168 if (device
== dp
->i_dev
) {
170 dq
->d_prev
= dp
->d_prev
;
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.
184 cd9660_ihashget(device
, inum
, p
)
189 register struct iso_node
*ip
;
193 for (ip
= isohashtbl
[INOHASH(device
, inum
)];; ip
= ip
->i_next
) {
196 if (inum
== ip
->i_number
&& device
== ip
->i_dev
) {
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.
205 simple_lock(&vp
->v_interlock
);
206 if (!vget(vp
, LK_EXCLUSIVE
| LK_INTERLOCK
| LK_RETRY
, p
))
215 * Insert the inode into the hash table, and return it locked.
221 struct iso_node
**ipp
, *iq
;
222 struct proc
*p
= current_proc();
224 /* lock the inode, then put it on the appropriate hash list */
225 lockmgr(&ip
->i_lock
, LK_EXCLUSIVE
, (struct slock
*)0, p
);
227 ipp
= &isohashtbl
[INOHASH(ip
->i_dev
, ip
->i_number
)];
229 iq
->i_prev
= &ip
->i_next
;
236 * Remove the inode from the hash table.
240 register struct iso_node
*ip
;
242 register struct iso_node
*iq
;
244 if ((iq
= ip
->i_next
))
245 iq
->i_prev
= ip
->i_prev
;
247 #if 1 /* was ifdef DIAGNOSTIC */
254 * Last reference to an inode, write the inode out and if necessary,
255 * truncate and deallocate the file.
259 struct vop_inactive_args
/* {
264 struct vnode
*vp
= ap
->a_vp
;
265 struct proc
*p
= ap
->a_p
;
266 register struct iso_node
*ip
= VTOI(vp
);
269 if (prtactive
&& vp
->v_usecount
!= 0)
270 vprint("cd9660_inactive: pushing active", vp
);
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.
279 * If we are done with the inode, reclaim it
280 * so that it can be reused immediately.
282 if (vp
->v_usecount
== 0 && ip
->inode
.iso_mode
== 0)
289 * Reclaim an inode so that it can be used for other purposes.
293 struct vop_reclaim_args
/* {
297 register struct vnode
*vp
= ap
->a_vp
;
298 register struct iso_node
*ip
= VTOI(vp
);
300 if (prtactive
&& vp
->v_usecount
!= 0)
301 vprint("cd9660_reclaim: pushing active", vp
);
303 * Remove the inode from its hash chain.
307 * Purge old data structures associated with the inode.
311 struct vnode
*tvp
= ip
->i_devvp
;
315 if (ip
->i_namep
!= isonullname
)
316 FREE(ip
->i_namep
, M_TEMP
);
317 if (ip
->i_riff
!= NULL
)
318 FREE(ip
->i_riff
, M_TEMP
);
319 FREE_ZONE(vp
->v_data
, sizeof(struct iso_node
), M_ISOFSNODE
);
328 cd9660_defattr(isodir
, inop
, bp
)
329 struct iso_directory_record
*isodir
;
330 struct iso_node
*inop
;
333 struct buf
*bp2
= NULL
;
335 struct iso_extended_attributes
*ap
= NULL
;
338 if ( isonum_711(isodir
->flags
) & directoryBit
) {
339 inop
->inode
.iso_mode
= S_IFDIR
;
341 * If we return 2, fts() will assume there are no subdirectories
342 * (just links for the path and .), so instead we return 1.
344 inop
->inode
.iso_links
= 1;
346 inop
->inode
.iso_mode
= S_IFREG
;
347 inop
->inode
.iso_links
= 1;
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
,
357 ap
= (struct iso_extended_attributes
*)bp
->b_data
;
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? */
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;
390 cd9660_deftstamp(isodir
,inop
,bp
)
391 struct iso_directory_record
*isodir
;
392 struct iso_node
*inop
;
395 struct buf
*bp2
= NULL
;
397 struct iso_extended_attributes
*ap
= NULL
;
401 && ((imp
= inop
->i_mnt
)->im_flags
& ISOFSMNT_EXTATT
)
402 && (off
= isonum_711(isodir
->ext_attr_length
)))
404 VOP_BLKATOFF(ITOV(inop
), (off_t
)-(off
<< imp
->im_bshift
), NULL
, &bp2
);
408 ap
= (struct iso_extended_attributes
*)bp
->b_data
;
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
;
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
;
430 cd9660_tstamp_conv7(pi
,pu
)
435 int y
, m
, d
, hour
, minute
, second
, tz
;
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;
456 * Changed :-) to make it relative to Jan. 1st, 1970
457 * and to disambiguate negative division
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;
461 crtime
= ((((days
* 24) + hour
) * 60 + minute
) * 60) + second
;
463 /* timezone offset is unreliable on some disks */
464 if (-48 <= tz
&& tz
<= 52)
465 crtime
-= tz
* 15 * 60;
473 cd9660_chars2ui(begin
,len
)
479 for (rc
= 0; --len
>= 0;) {
481 rc
+= *begin
++ - '0';
487 cd9660_tstamp_conv17(pi
,pu
)
493 /* year:"0001"-"9999" -> -1900 */
494 buf
[0] = cd9660_chars2ui(pi
,4) - 1900;
496 /* month: " 1"-"12" -> 1 - 12 */
497 buf
[1] = cd9660_chars2ui(pi
+ 4,2);
499 /* day: " 1"-"31" -> 1 - 31 */
500 buf
[2] = cd9660_chars2ui(pi
+ 6,2);
502 /* hour: " 0"-"23" -> 0 - 23 */
503 buf
[3] = cd9660_chars2ui(pi
+ 8,2);
505 /* minute:" 0"-"59" -> 0 - 59 */
506 buf
[4] = cd9660_chars2ui(pi
+ 10,2);
508 /* second:" 0"-"59" -> 0 - 59 */
509 buf
[5] = cd9660_chars2ui(pi
+ 12,2);
511 /* difference of GMT */
514 return cd9660_tstamp_conv7(buf
,pu
);
518 isodirino(isodir
, imp
)
519 struct iso_directory_record
*isodir
;
524 ino
= (isonum_733(isodir
->extent
) + isonum_711(isodir
->ext_attr_length
))