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