1 /* $NetBSD: cd9660_node.c,v 1.23 2008/02/27 19:43:36 matt Exp $ */
4 * Copyright (c) 1982, 1986, 1989, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley
8 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
9 * Support code is derived from software contributed to Berkeley
10 * by Atsushi Murai (amurai@spec.co.jp).
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)cd9660_node.c 8.8 (Berkeley) 5/22/95
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: cd9660_node.c,v 1.23 2008/02/27 19:43:36 matt Exp $");
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/mount.h>
48 #include <sys/vnode.h>
49 #include <sys/namei.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
55 #include <fs/cd9660/iso.h>
56 #include <fs/cd9660/cd9660_extern.h>
57 #include <fs/cd9660/cd9660_node.h>
58 #include <fs/cd9660/cd9660_mount.h>
59 #include <fs/cd9660/iso_rrip.h>
62 * Structures associated with iso_node caching.
64 LIST_HEAD(ihashhead
, iso_node
) *isohashtbl
;
66 #define INOHASH(device, inum) (((device) + ((inum)>>12)) & isohash)
67 kmutex_t cd9660_ihash_lock
;
68 kmutex_t cd9660_hashlock
;
70 extern int prtactive
; /* 1 => print out reclaim of active vnodes */
72 struct pool cd9660_node_pool
;
74 static u_int
cd9660_chars2ui(const u_char
*, int);
77 * Initialize hash links for inodes and dnodes.
83 malloc_type_attach(M_ISOFSMNT
);
84 pool_init(&cd9660_node_pool
, sizeof(struct iso_node
), 0, 0, 0,
85 "cd9660nopl", &pool_allocator_nointr
, IPL_NONE
);
86 isohashtbl
= hashinit(desiredvnodes
, HASH_LIST
, true, &isohash
);
87 mutex_init(&cd9660_ihash_lock
, MUTEX_DEFAULT
, IPL_NONE
);
88 mutex_init(&cd9660_hashlock
, MUTEX_DEFAULT
, IPL_NONE
);
92 * Reinitialize inode hash table.
99 struct ihashhead
*oldhash1
, *hash1
;
100 u_long oldmask1
, mask1
, val
;
103 hash1
= hashinit(desiredvnodes
, HASH_LIST
, true, &mask1
);
105 mutex_enter(&cd9660_ihash_lock
);
106 oldhash1
= isohashtbl
;
110 for (i
= 0; i
<= oldmask1
; i
++) {
111 while ((ip
= LIST_FIRST(&oldhash1
[i
])) != NULL
) {
112 LIST_REMOVE(ip
, i_hash
);
113 val
= INOHASH(ip
->i_dev
, ip
->i_number
);
114 LIST_INSERT_HEAD(&hash1
[val
], ip
, i_hash
);
117 mutex_exit(&cd9660_ihash_lock
);
118 hashdone(oldhash1
, HASH_LIST
, oldmask1
);
122 * Destroy node pool and hash table.
127 hashdone(isohashtbl
, HASH_LIST
, isohash
);
128 pool_destroy(&cd9660_node_pool
);
129 mutex_destroy(&cd9660_ihash_lock
);
130 mutex_destroy(&cd9660_hashlock
);
131 malloc_type_detach(M_ISOFSMNT
);
135 * Use the device/inum pair to find the incore inode, and return a pointer
136 * to it. If it is in core, but locked, wait for it.
139 cd9660_ihashget(dev_t dev
, ino_t inum
, int flags
)
145 mutex_enter(&cd9660_ihash_lock
);
146 LIST_FOREACH(ip
, &isohashtbl
[INOHASH(dev
, inum
)], i_hash
) {
147 if (inum
== ip
->i_number
&& dev
== ip
->i_dev
) {
150 mutex_exit(&cd9660_ihash_lock
);
152 mutex_enter(&vp
->v_interlock
);
153 mutex_exit(&cd9660_ihash_lock
);
154 if (vget(vp
, flags
| LK_INTERLOCK
))
160 mutex_exit(&cd9660_ihash_lock
);
165 * Insert the inode into the hash table, and return it locked.
167 * ip->i_vnode must be initialized first.
170 cd9660_ihashins(struct iso_node
*ip
)
172 struct ihashhead
*ipp
;
174 KASSERT(mutex_owned(&cd9660_hashlock
));
176 mutex_enter(&cd9660_ihash_lock
);
177 ipp
= &isohashtbl
[INOHASH(ip
->i_dev
, ip
->i_number
)];
178 LIST_INSERT_HEAD(ipp
, ip
, i_hash
);
179 mutex_exit(&cd9660_ihash_lock
);
181 vlockmgr(&ip
->i_vnode
->v_lock
, LK_EXCLUSIVE
);
185 * Remove the inode from the hash table.
188 cd9660_ihashrem(struct iso_node
*ip
)
190 mutex_enter(&cd9660_ihash_lock
);
191 LIST_REMOVE(ip
, i_hash
);
192 mutex_exit(&cd9660_ihash_lock
);
196 * Last reference to an inode, write the inode out and if necessary,
197 * truncate and deallocate the file.
200 cd9660_inactive(void *v
)
202 struct vop_inactive_args
/* {
206 struct vnode
*vp
= ap
->a_vp
;
207 struct iso_node
*ip
= VTOI(vp
);
211 * If we are done with the inode, reclaim it
212 * so that it can be reused immediately.
215 *ap
->a_recycle
= (ip
->inode
.iso_mode
== 0);
221 * Reclaim an inode so that it can be used for other purposes.
224 cd9660_reclaim(void *v
)
226 struct vop_reclaim_args
/* {
230 struct vnode
*vp
= ap
->a_vp
;
231 struct iso_node
*ip
= VTOI(vp
);
233 if (prtactive
&& vp
->v_usecount
> 1)
234 vprint("cd9660_reclaim: pushing active", vp
);
236 * Remove the inode from its hash chain.
240 * Purge old data structures associated with the inode.
247 genfs_node_destroy(vp
);
248 pool_put(&cd9660_node_pool
, vp
->v_data
);
257 cd9660_defattr(struct iso_directory_record
*isodir
, struct iso_node
*inop
,
260 struct buf
*bp2
= NULL
;
262 struct iso_extended_attributes
*ap
= NULL
;
265 if (isonum_711(isodir
->flags
)&2) {
266 inop
->inode
.iso_mode
= S_IFDIR
;
268 * If we return 2, fts() will assume there are no subdirectories
269 * (just links for the path and .), so instead we return 1.
271 inop
->inode
.iso_links
= 1;
273 inop
->inode
.iso_mode
= S_IFREG
;
274 inop
->inode
.iso_links
= 1;
277 && ((imp
= inop
->i_mnt
)->im_flags
& ISOFSMNT_EXTATT
)
278 && (off
= isonum_711(isodir
->ext_attr_length
))) {
279 cd9660_blkatoff(ITOV(inop
), (off_t
)-(off
<< imp
->im_bshift
),
284 ap
= (struct iso_extended_attributes
*)bp
->b_data
;
286 if (isonum_711(ap
->version
) == 1) {
287 if (!(ap
->perm
[1]&0x10))
288 inop
->inode
.iso_mode
|= S_IRUSR
;
289 if (!(ap
->perm
[1]&0x40))
290 inop
->inode
.iso_mode
|= S_IXUSR
;
291 if (!(ap
->perm
[0]&0x01))
292 inop
->inode
.iso_mode
|= S_IRGRP
;
293 if (!(ap
->perm
[0]&0x04))
294 inop
->inode
.iso_mode
|= S_IXGRP
;
295 if (!(ap
->perm
[0]&0x10))
296 inop
->inode
.iso_mode
|= S_IROTH
;
297 if (!(ap
->perm
[0]&0x40))
298 inop
->inode
.iso_mode
|= S_IXOTH
;
299 inop
->inode
.iso_uid
= isonum_723(ap
->owner
); /* what about 0? */
300 inop
->inode
.iso_gid
= isonum_723(ap
->group
); /* what about 0? */
305 inop
->inode
.iso_mode
|=
306 S_IRUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
|S_IROTH
|S_IXOTH
;
307 inop
->inode
.iso_uid
= (uid_t
)0;
308 inop
->inode
.iso_gid
= (gid_t
)0;
318 cd9660_deftstamp(struct iso_directory_record
*isodir
, struct iso_node
*inop
,
321 struct buf
*bp2
= NULL
;
323 struct iso_extended_attributes
*ap
= NULL
;
327 && ((imp
= inop
->i_mnt
)->im_flags
& ISOFSMNT_EXTATT
)
328 && (off
= isonum_711(isodir
->ext_attr_length
))) {
329 cd9660_blkatoff(ITOV(inop
), (off_t
)-(off
<< imp
->im_bshift
),
334 ap
= (struct iso_extended_attributes
*)bp
->b_data
;
336 if (isonum_711(ap
->version
) == 1) {
337 if (!cd9660_tstamp_conv17(ap
->ftime
,&inop
->inode
.iso_atime
))
338 cd9660_tstamp_conv17(ap
->ctime
,&inop
->inode
.iso_atime
);
339 if (!cd9660_tstamp_conv17(ap
->ctime
,&inop
->inode
.iso_ctime
))
340 inop
->inode
.iso_ctime
= inop
->inode
.iso_atime
;
341 if (!cd9660_tstamp_conv17(ap
->mtime
,&inop
->inode
.iso_mtime
))
342 inop
->inode
.iso_mtime
= inop
->inode
.iso_ctime
;
347 cd9660_tstamp_conv7(isodir
->date
,&inop
->inode
.iso_ctime
);
348 inop
->inode
.iso_atime
= inop
->inode
.iso_ctime
;
349 inop
->inode
.iso_mtime
= inop
->inode
.iso_ctime
;
356 cd9660_tstamp_conv7(const u_char
*pi
, struct timespec
*pu
)
359 int y
, m
, d
, hour
, minute
, second
, tz
;
375 /* computes day number relative to Sept. 19th,1989 */
376 /* don't even *THINK* about changing formula. It works! */
377 days
= 367*(y
-1980)-7*(y
+(m
+9)/12)/4-3*((y
+(m
-9)/7)/100+1)/4+275*m
/9+d
-100;
380 * Changed :-) to make it relative to Jan. 1st, 1970
381 * and to disambiguate negative division
383 days
= 367*(y
-1960)-7*(y
+(m
+9)/12)/4-3*((y
+(m
+9)/12-1)/100+1)/4+275*m
/9+d
-239;
385 crtime
= ((((days
* 24) + hour
) * 60 + minute
) * 60) + second
;
387 /* timezone offset is unreliable on some disks */
388 if (-48 <= tz
&& tz
<= 52)
389 crtime
-= tz
* 15 * 60;
397 cd9660_chars2ui(const u_char
*begin
, int len
)
401 for (rc
= 0; --len
>= 0;) {
403 rc
+= *begin
++ - '0';
409 cd9660_tstamp_conv17(const u_char
*pi
, struct timespec
*pu
)
413 /* year:"0001"-"9999" -> -1900 */
414 tbuf
[0] = cd9660_chars2ui(pi
,4) - 1900;
416 /* month: " 1"-"12" -> 1 - 12 */
417 tbuf
[1] = cd9660_chars2ui(pi
+ 4,2);
419 /* day: " 1"-"31" -> 1 - 31 */
420 tbuf
[2] = cd9660_chars2ui(pi
+ 6,2);
422 /* hour: " 0"-"23" -> 0 - 23 */
423 tbuf
[3] = cd9660_chars2ui(pi
+ 8,2);
425 /* minute:" 0"-"59" -> 0 - 59 */
426 tbuf
[4] = cd9660_chars2ui(pi
+ 10,2);
428 /* second:" 0"-"59" -> 0 - 59 */
429 tbuf
[5] = cd9660_chars2ui(pi
+ 12,2);
431 /* difference of GMT */
434 return cd9660_tstamp_conv7(tbuf
,pu
);
438 isodirino(struct iso_directory_record
*isodir
, struct iso_mnt
*imp
)
442 ino
= (isonum_733(isodir
->extent
) + isonum_711(isodir
->ext_attr_length
))