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