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