cd9660_node.c revision 1.8 1 /* $NetBSD: cd9660_node.c,v 1.8 2004/05/20 05:39:34 atatat Exp $ */
2
3 /*-
4 * Copyright (c) 1982, 1986, 1989, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley
8 * by Pace Willisson (pace (at) blitz.com). The Rock Ridge Extension
9 * Support code is derived from software contributed to Berkeley
10 * by Atsushi Murai (amurai (at) spec.co.jp).
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
23 *
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
34 * SUCH DAMAGE.
35 *
36 * @(#)cd9660_node.c 8.8 (Berkeley) 5/22/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: cd9660_node.c,v 1.8 2004/05/20 05:39:34 atatat Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/mount.h>
45 #include <sys/proc.h>
46 #include <sys/file.h>
47 #include <sys/buf.h>
48 #include <sys/vnode.h>
49 #include <sys/namei.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/pool.h>
53 #include <sys/stat.h>
54
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>
60
61 /*
62 * Structures associated with iso_node caching.
63 */
64 LIST_HEAD(ihashhead, iso_node) *isohashtbl;
65 u_long isohash;
66 #define INOHASH(device, inum) (((device) + ((inum)>>12)) & isohash)
67 struct simplelock cd9660_ihash_slock;
68
69 #ifdef ISODEVMAP
70 LIST_HEAD(idvhashhead, iso_dnode) *idvhashtbl;
71 u_long idvhash;
72 #define DNOHASH(device, inum) (((device) + ((inum)>>12)) & idvhash)
73 #endif
74
75 extern int prtactive; /* 1 => print out reclaim of active vnodes */
76
77 POOL_INIT(cd9660_node_pool, sizeof(struct iso_node), 0, 0, 0, "cd9660nopl",
78 &pool_allocator_nointr);
79
80 static u_int cd9660_chars2ui __P((u_char *, int));
81
82 /*
83 * Initialize hash links for inodes and dnodes.
84 */
85 void
86 cd9660_init()
87 {
88 #ifdef _LKM
89 malloc_type_attach(M_ISOFSMNT);
90 pool_init(&cd9660_node_pool, sizeof(struct iso_node), 0, 0, 0,
91 "cd9660nopl", &pool_allocator_nointr);
92 #endif
93 isohashtbl = hashinit(desiredvnodes, HASH_LIST, M_ISOFSMNT, M_WAITOK,
94 &isohash);
95 simple_lock_init(&cd9660_ihash_slock);
96 #ifdef ISODEVMAP
97 idvhashtbl = hashinit(desiredvnodes / 8, HASH_LIST, M_ISOFSMNT,
98 M_WAITOK, &idvhash);
99 #endif
100 }
101
102 /*
103 * Reinitialize inode hash table.
104 */
105
106 void
107 cd9660_reinit()
108 {
109 struct iso_node *ip;
110 struct ihashhead *oldhash1, *hash1;
111 u_long oldmask1, mask1, val;
112 #ifdef ISODEVMAP
113 struct iso_dnode *dp;
114 struct idvhashhead *oldhash2, *hash2;
115 u_long oldmask2, mask2;
116 #endif
117 u_int i;
118
119 hash1 = hashinit(desiredvnodes, HASH_LIST, M_ISOFSMNT, M_WAITOK,
120 &mask1);
121 #ifdef ISODEVMAP
122 hash2 = hashinit(desiredvnodes / 8, HASH_LIST, M_ISOFSMNT, M_WAITOK,
123 &mask2);
124 #endif
125
126 simple_lock(&cd9660_ihash_slock);
127 oldhash1 = isohashtbl;
128 oldmask1 = isohash;
129 isohashtbl = hash1;
130 isohash = mask1;
131 #ifdef ISODEVMAP
132 oldhash2 = idvhashtbl;
133 oldmask2 = idvhash;
134 idvhashtbl = hash2;
135 idvhash = mask2;
136 for (i = 0; i <= oldmask2; i++) {
137 while ((dp = LIST_FIRST(&oldhash2[i])) != NULL) {
138 LIST_REMOVE(dp, d_hash);
139 val = DNOHASH(dp->i_dev, dp->i_number);
140 LIST_INSERT_HEAD(&hash2[val], dp, d_hash);
141 }
142 }
143 #endif
144 for (i = 0; i <= oldmask1; i++) {
145 while ((ip = LIST_FIRST(&oldhash1[i])) != NULL) {
146 LIST_REMOVE(ip, i_hash);
147 val = INOHASH(ip->i_dev, ip->i_number);
148 LIST_INSERT_HEAD(&hash1[val], ip, i_hash);
149 }
150 }
151 simple_unlock(&cd9660_ihash_slock);
152 hashdone(oldhash1, M_ISOFSMNT);
153 #ifdef ISODEVMAP
154 hashdone(oldhash2, M_ISOFSMNT);
155 #endif
156 }
157
158 /*
159 * Destroy node pool and hash table.
160 */
161 void
162 cd9660_done()
163 {
164 hashdone(isohashtbl, M_ISOFSMNT);
165 #ifdef ISODEVMAP
166 hashdone(idvhashtbl, M_ISOFSMNT);
167 #endif
168 #ifdef _LKM
169 pool_destroy(&cd9660_node_pool);
170 malloc_type_detach(M_ISOFSMNT);
171 #endif
172 }
173
174 #ifdef ISODEVMAP
175 /*
176 * Enter a new node into the device hash list
177 */
178 struct iso_dnode *
179 iso_dmap(device, inum, create)
180 dev_t device;
181 ino_t inum;
182 int create;
183 {
184 struct iso_dnode *dp;
185 struct idvhashhead *hp;
186
187 hp = &idvhashtbl[DNOHASH(device, inum)];
188 LIST_FOREACH(dp, hp, d_hash) {
189 if (inum == dp->i_number && device == dp->i_dev)
190 return (dp);
191 }
192
193 if (!create)
194 return (NULL);
195
196 MALLOC(dp, struct iso_dnode *, sizeof(struct iso_dnode), M_CACHE,
197 M_WAITOK);
198 dp->i_dev = device;
199 dp->i_number = inum;
200 LIST_INSERT_HEAD(hp, dp, d_hash);
201 return (dp);
202 }
203
204 void
205 iso_dunmap(device)
206 dev_t device;
207 {
208 struct idvhashhead *dpp;
209 struct iso_dnode *dp, *dq;
210
211 for (dpp = idvhashtbl; dpp <= idvhashtbl + idvhash; dpp++) {
212 for (dp = LIST_FIRST(dpp); dp != NULL; dp = dq) {
213 dq = LIST_NEXT(dp, d_hash);
214 if (device == dp->i_dev) {
215 LIST_REMOVE(dp, d_hash);
216 FREE(dp, M_CACHE);
217 }
218 }
219 }
220 }
221 #endif
222
223 /*
224 * Use the device/inum pair to find the incore inode, and return a pointer
225 * to it. If it is in core, but locked, wait for it.
226 */
227 struct vnode *
228 cd9660_ihashget(dev, inum)
229 dev_t dev;
230 ino_t inum;
231 {
232 struct iso_node *ip;
233 struct vnode *vp;
234
235 loop:
236 simple_lock(&cd9660_ihash_slock);
237 LIST_FOREACH(ip, &isohashtbl[INOHASH(dev, inum)], i_hash) {
238 if (inum == ip->i_number && dev == ip->i_dev) {
239 vp = ITOV(ip);
240 simple_lock(&vp->v_interlock);
241 simple_unlock(&cd9660_ihash_slock);
242 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
243 goto loop;
244 return (vp);
245 }
246 }
247 simple_unlock(&cd9660_ihash_slock);
248 return (NULL);
249 }
250
251 /*
252 * Insert the inode into the hash table, and return it locked.
253 *
254 * ip->i_vnode must be initialized first.
255 */
256 void
257 cd9660_ihashins(ip)
258 struct iso_node *ip;
259 {
260 struct ihashhead *ipp;
261
262 simple_lock(&cd9660_ihash_slock);
263 ipp = &isohashtbl[INOHASH(ip->i_dev, ip->i_number)];
264 LIST_INSERT_HEAD(ipp, ip, i_hash);
265 simple_unlock(&cd9660_ihash_slock);
266
267 lockmgr(&ip->i_vnode->v_lock, LK_EXCLUSIVE, &ip->i_vnode->v_interlock);
268 }
269
270 /*
271 * Remove the inode from the hash table.
272 */
273 void
274 cd9660_ihashrem(ip)
275 struct iso_node *ip;
276 {
277 simple_lock(&cd9660_ihash_slock);
278 LIST_REMOVE(ip, i_hash);
279 simple_unlock(&cd9660_ihash_slock);
280 }
281
282 /*
283 * Last reference to an inode, write the inode out and if necessary,
284 * truncate and deallocate the file.
285 */
286 int
287 cd9660_inactive(v)
288 void *v;
289 {
290 struct vop_inactive_args /* {
291 struct vnode *a_vp;
292 struct proc *a_p;
293 } */ *ap = v;
294 struct vnode *vp = ap->a_vp;
295 struct proc *p = ap->a_p;
296 struct iso_node *ip = VTOI(vp);
297 int error = 0;
298
299 if (prtactive && vp->v_usecount != 0)
300 vprint("cd9660_inactive: pushing active", vp);
301
302 ip->i_flag = 0;
303 VOP_UNLOCK(vp, 0);
304 /*
305 * If we are done with the inode, reclaim it
306 * so that it can be reused immediately.
307 */
308 if (ip->inode.iso_mode == 0)
309 vrecycle(vp, (struct simplelock *)0, p);
310 return error;
311 }
312
313 /*
314 * Reclaim an inode so that it can be used for other purposes.
315 */
316 int
317 cd9660_reclaim(v)
318 void *v;
319 {
320 struct vop_reclaim_args /* {
321 struct vnode *a_vp;
322 struct proc *a_p;
323 } */ *ap = v;
324 struct vnode *vp = ap->a_vp;
325 struct iso_node *ip = VTOI(vp);
326
327 if (prtactive && vp->v_usecount != 0)
328 vprint("cd9660_reclaim: pushing active", vp);
329 /*
330 * Remove the inode from its hash chain.
331 */
332 cd9660_ihashrem(ip);
333 /*
334 * Purge old data structures associated with the inode.
335 */
336 cache_purge(vp);
337 if (ip->i_devvp) {
338 vrele(ip->i_devvp);
339 ip->i_devvp = 0;
340 }
341 pool_put(&cd9660_node_pool, vp->v_data);
342 vp->v_data = NULL;
343 return (0);
344 }
345
346 /*
347 * File attributes
348 */
349 void
350 cd9660_defattr(isodir, inop, bp)
351 struct iso_directory_record *isodir;
352 struct iso_node *inop;
353 struct buf *bp;
354 {
355 struct buf *bp2 = NULL;
356 struct iso_mnt *imp;
357 struct iso_extended_attributes *ap = NULL;
358 int off;
359
360 if (isonum_711(isodir->flags)&2) {
361 inop->inode.iso_mode = S_IFDIR;
362 /*
363 * If we return 2, fts() will assume there are no subdirectories
364 * (just links for the path and .), so instead we return 1.
365 */
366 inop->inode.iso_links = 1;
367 } else {
368 inop->inode.iso_mode = S_IFREG;
369 inop->inode.iso_links = 1;
370 }
371 if (!bp
372 && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
373 && (off = isonum_711(isodir->ext_attr_length))) {
374 VOP_BLKATOFF(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL,
375 &bp2);
376 bp = bp2;
377 }
378 if (bp) {
379 ap = (struct iso_extended_attributes *)bp->b_data;
380
381 if (isonum_711(ap->version) == 1) {
382 if (!(ap->perm[1]&0x10))
383 inop->inode.iso_mode |= S_IRUSR;
384 if (!(ap->perm[1]&0x40))
385 inop->inode.iso_mode |= S_IXUSR;
386 if (!(ap->perm[0]&0x01))
387 inop->inode.iso_mode |= S_IRGRP;
388 if (!(ap->perm[0]&0x04))
389 inop->inode.iso_mode |= S_IXGRP;
390 if (!(ap->perm[0]&0x10))
391 inop->inode.iso_mode |= S_IROTH;
392 if (!(ap->perm[0]&0x40))
393 inop->inode.iso_mode |= S_IXOTH;
394 inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */
395 inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */
396 } else
397 ap = NULL;
398 }
399 if (!ap) {
400 inop->inode.iso_mode |=
401 S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
402 inop->inode.iso_uid = (uid_t)0;
403 inop->inode.iso_gid = (gid_t)0;
404 }
405 if (bp2)
406 brelse(bp2);
407 }
408
409 /*
410 * Time stamps
411 */
412 void
413 cd9660_deftstamp(isodir,inop,bp)
414 struct iso_directory_record *isodir;
415 struct iso_node *inop;
416 struct buf *bp;
417 {
418 struct buf *bp2 = NULL;
419 struct iso_mnt *imp;
420 struct iso_extended_attributes *ap = NULL;
421 int off;
422
423 if (!bp
424 && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
425 && (off = isonum_711(isodir->ext_attr_length))) {
426 VOP_BLKATOFF(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL,
427 &bp2);
428 bp = bp2;
429 }
430 if (bp) {
431 ap = (struct iso_extended_attributes *)bp->b_data;
432
433 if (isonum_711(ap->version) == 1) {
434 if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime))
435 cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime);
436 if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime))
437 inop->inode.iso_ctime = inop->inode.iso_atime;
438 if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime))
439 inop->inode.iso_mtime = inop->inode.iso_ctime;
440 } else
441 ap = NULL;
442 }
443 if (!ap) {
444 cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime);
445 inop->inode.iso_atime = inop->inode.iso_ctime;
446 inop->inode.iso_mtime = inop->inode.iso_ctime;
447 }
448 if (bp2)
449 brelse(bp2);
450 }
451
452 int
453 cd9660_tstamp_conv7(pi,pu)
454 u_char *pi;
455 struct timespec *pu;
456 {
457 int crtime, days;
458 int y, m, d, hour, minute, second, tz;
459
460 y = pi[0] + 1900;
461 m = pi[1];
462 d = pi[2];
463 hour = pi[3];
464 minute = pi[4];
465 second = pi[5];
466 tz = pi[6];
467
468 if (y < 1970) {
469 pu->tv_sec = 0;
470 pu->tv_nsec = 0;
471 return 0;
472 } else {
473 #ifdef ORIGINAL
474 /* computes day number relative to Sept. 19th,1989 */
475 /* don't even *THINK* about changing formula. It works! */
476 days = 367*(y-1980)-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d-100;
477 #else
478 /*
479 * Changed :-) to make it relative to Jan. 1st, 1970
480 * and to disambiguate negative division
481 */
482 days = 367*(y-1960)-7*(y+(m+9)/12)/4-3*((y+(m+9)/12-1)/100+1)/4+275*m/9+d-239;
483 #endif
484 crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second;
485
486 /* timezone offset is unreliable on some disks */
487 if (-48 <= tz && tz <= 52)
488 crtime -= tz * 15 * 60;
489 }
490 pu->tv_sec = crtime;
491 pu->tv_nsec = 0;
492 return 1;
493 }
494
495 static u_int
496 cd9660_chars2ui(begin,len)
497 u_char *begin;
498 int len;
499 {
500 u_int rc;
501
502 for (rc = 0; --len >= 0;) {
503 rc *= 10;
504 rc += *begin++ - '0';
505 }
506 return rc;
507 }
508
509 int
510 cd9660_tstamp_conv17(pi,pu)
511 u_char *pi;
512 struct timespec *pu;
513 {
514 u_char buf[7];
515
516 /* year:"0001"-"9999" -> -1900 */
517 buf[0] = cd9660_chars2ui(pi,4) - 1900;
518
519 /* month: " 1"-"12" -> 1 - 12 */
520 buf[1] = cd9660_chars2ui(pi + 4,2);
521
522 /* day: " 1"-"31" -> 1 - 31 */
523 buf[2] = cd9660_chars2ui(pi + 6,2);
524
525 /* hour: " 0"-"23" -> 0 - 23 */
526 buf[3] = cd9660_chars2ui(pi + 8,2);
527
528 /* minute:" 0"-"59" -> 0 - 59 */
529 buf[4] = cd9660_chars2ui(pi + 10,2);
530
531 /* second:" 0"-"59" -> 0 - 59 */
532 buf[5] = cd9660_chars2ui(pi + 12,2);
533
534 /* difference of GMT */
535 buf[6] = pi[16];
536
537 return cd9660_tstamp_conv7(buf,pu);
538 }
539
540 ino_t
541 isodirino(isodir, imp)
542 struct iso_directory_record *isodir;
543 struct iso_mnt *imp;
544 {
545 ino_t ino;
546
547 ino = (isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length))
548 << imp->im_bshift;
549 return (ino);
550 }
551