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