c247ba35fea834cd87b42ffeff72bbca297386d2
[dragonfly.git] / sys / vfs / isofs / cd9660 / cd9660_node.c
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1994, 1995
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley
6  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
7  * Support code is derived from software contributed to Berkeley
8  * by Atsushi Murai (amurai@spec.co.jp).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)cd9660_node.c       8.2 (Berkeley) 1/23/94
39  * $FreeBSD: src/sys/isofs/cd9660/cd9660_node.c,v 1.29.2.1 2000/07/08 14:35:56 bp Exp $
40  * $DragonFly: src/sys/vfs/isofs/cd9660/cd9660_node.c,v 1.13 2004/10/12 19:20:58 dillon Exp $
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mount.h>
46 #include <sys/proc.h>
47 #include <sys/buf.h>
48 #include <sys/vnode.h>
49 #include <sys/malloc.h>
50 #include <sys/stat.h>
51
52 #include "iso.h"
53 #include "cd9660_node.h"
54 #include "cd9660_mount.h"
55
56 /*
57  * Structures associated with iso_node caching.
58  */
59 static struct iso_node **isohashtbl;
60 static u_long isohash;
61 #define INOHASH(device, inum)   ((minor(device) + ((inum)>>12)) & isohash)
62 #ifndef NULL_SIMPLELOCKS
63 static struct lwkt_token cd9660_ihash_token;
64 #endif
65
66 static void cd9660_ihashrem (struct iso_node *);
67 static unsigned cd9660_chars2ui (unsigned char *begin, int len);
68
69 /*
70  * Initialize hash links for inodes and dnodes.
71  */
72 int
73 cd9660_init(struct vfsconf *vfsp)
74 {
75         isohash = 16;
76         while (isohash < desiredvnodes)
77                 isohash <<= 1;
78         isohashtbl = malloc(sizeof(void *) * isohash,
79                             M_ISOFSMNT, M_WAITOK|M_ZERO);
80         --isohash;
81         lwkt_token_init(&cd9660_ihash_token);
82         return (0);
83 }
84
85 int
86 cd9660_uninit(struct vfsconf *vfsp)
87 {
88
89         if (isohashtbl != NULL)
90                 free(isohashtbl, M_ISOFSMNT);
91         return (0);
92 }
93
94
95 /*
96  * Use the device/inum pair to find the incore inode, and return a pointer
97  * to it. If it is in core, but locked, wait for it.
98  */
99 struct vnode *
100 cd9660_ihashget(dev_t dev, ino_t inum)
101 {
102         struct thread *td = curthread;          /* XXX */
103         struct iso_node *ip;
104         lwkt_tokref ilock;
105         struct vnode *vp;
106
107         lwkt_gettoken(&ilock, &cd9660_ihash_token);
108 loop:
109         for (ip = isohashtbl[INOHASH(dev, inum)]; ip; ip = ip->i_next) {
110                 if (inum != ip->i_number || dev != ip->i_dev)
111                         continue;
112                 vp = ITOV(ip);
113                 if (vget(vp, LK_EXCLUSIVE, td))
114                         goto loop;
115                 /*
116                  * We must check to see if the inode has been ripped
117                  * out from under us after blocking.
118                  */   
119                 for (ip = isohashtbl[INOHASH(dev, inum)]; ip; ip = ip->i_next) {
120                         if (inum == ip->i_number && dev == ip->i_dev)
121                                 break;
122                 }
123                 if (ip == NULL || ITOV(ip) != vp) {
124                         goto loop;
125                 }
126                 lwkt_reltoken(&ilock);
127                 return (vp);
128         }
129         lwkt_reltoken(&ilock);
130         return (NULL);
131 }
132
133 /*
134  * Insert the inode into the hash table, return 0 on success, non-zero
135  * if the inode has already been found to be in the hash table.
136  */
137 int
138 cd9660_ihashins(struct iso_node *ip)
139 {
140         struct iso_node **ipp, *iq;
141         lwkt_tokref ilock;
142
143         lwkt_gettoken(&ilock, &cd9660_ihash_token);
144         ipp = &isohashtbl[INOHASH(ip->i_dev, ip->i_number)];
145         while ((iq = *ipp) != NULL) {
146                 if (iq->i_dev == ip->i_dev && iq->i_number == ip->i_number) {
147                         lwkt_reltoken(&ilock);
148                         return(EBUSY);
149                 }
150                 ipp = &iq->i_next;
151         }
152         ip->i_next = NULL;
153         *ipp = ip;
154         lwkt_reltoken(&ilock);
155         return(0);
156 }
157
158 /*
159  * Remove the inode from the hash table.
160  */
161 static void
162 cd9660_ihashrem(struct iso_node *ip)
163 {
164         struct iso_node **ipp, *iq;
165         lwkt_tokref ilock;
166
167         lwkt_gettoken(&ilock, &cd9660_ihash_token);
168         ipp = &isohashtbl[INOHASH(ip->i_dev, ip->i_number)];
169         while ((iq = *ipp) != NULL) {
170                 if (ip == iq)
171                         break;
172                 ipp = &iq->i_next;
173         }
174         KKASSERT(ip == iq);
175         *ipp = ip->i_next;
176         ip->i_next = NULL;
177         lwkt_reltoken(&ilock);
178 }
179
180 /*
181  * Last reference to an inode, write the inode out and if necessary,
182  * truncate and deallocate the file.
183  *
184  * cd9660_inactive(struct vnode *a_vp, struct thread *a_td)
185  */
186 int
187 cd9660_inactive(struct vop_inactive_args *ap)
188 {
189         struct vnode *vp = ap->a_vp;
190         struct thread *td = ap->a_td;
191         struct iso_node *ip = VTOI(vp);
192         int error = 0;
193
194         if (prtactive && vp->v_usecount != 0)
195                 vprint("cd9660_inactive: pushing active", vp);
196
197         if (ip)
198                 ip->i_flag = 0;
199         /*
200          * If we are done with the inode, reclaim it
201          * so that it can be reused immediately.
202          */
203         if (ip == NULL || ip->inode.iso_mode == 0)
204                 vrecycle(vp, td);
205         return error;
206 }
207
208 /*
209  * Reclaim an inode so that it can be used for other purposes.
210  *
211  * cd9660_reclaim(struct vnode *a_vp, struct proc *a_p)
212  */
213 int
214 cd9660_reclaim(struct vop_reclaim_args *ap)
215 {
216         struct vnode *vp = ap->a_vp;
217         struct iso_node *ip = VTOI(vp);
218
219         if (prtactive && vp->v_usecount != 0)
220                 vprint("cd9660_reclaim: pushing active", vp);
221         /*
222          * Remove the inode from its hash chain.
223          */
224         vp->v_data = NULL;
225         if (ip) {
226                 cd9660_ihashrem(ip);
227                 if (ip->i_devvp) {
228                         vrele(ip->i_devvp);
229                         ip->i_devvp = 0;
230                 }
231                 free(ip, M_ISOFSNODE);
232         }
233         return (0);
234 }
235
236 /*
237  * File attributes
238  */
239 void
240 cd9660_defattr(struct iso_directory_record *isodir, struct iso_node *inop,
241                struct buf *bp, enum ISO_FTYPE ftype)
242 {
243         struct buf *bp2 = NULL;
244         struct iso_mnt *imp;
245         struct iso_extended_attributes *ap = NULL;
246         int off;
247
248         /* high sierra does not have timezone data, flag is one byte ahead */
249         if (isonum_711(ftype == ISO_FTYPE_HIGH_SIERRA?
250                        &isodir->date[6]: isodir->flags)&2) {
251                 inop->inode.iso_mode = S_IFDIR;
252                 /*
253                  * If we return 2, fts() will assume there are no subdirectories
254                  * (just links for the path and .), so instead we return 1.
255                  */
256                 inop->inode.iso_links = 1;
257         } else {
258                 inop->inode.iso_mode = S_IFREG;
259                 inop->inode.iso_links = 1;
260         }
261         if (!bp
262             && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
263             && (off = isonum_711(isodir->ext_attr_length))) {
264                 cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL,
265                              &bp2);
266                 bp = bp2;
267         }
268         if (bp) {
269                 ap = (struct iso_extended_attributes *)bp->b_data;
270                 
271                 if (isonum_711(ap->version) == 1) {
272                         if (!(ap->perm[0]&0x40))
273                                 inop->inode.iso_mode |= VEXEC >> 6;
274                         if (!(ap->perm[0]&0x10))
275                                 inop->inode.iso_mode |= VREAD >> 6;
276                         if (!(ap->perm[0]&4))
277                                 inop->inode.iso_mode |= VEXEC >> 3;
278                         if (!(ap->perm[0]&1))
279                                 inop->inode.iso_mode |= VREAD >> 3;
280                         if (!(ap->perm[1]&0x40))
281                                 inop->inode.iso_mode |= VEXEC;
282                         if (!(ap->perm[1]&0x10))
283                                 inop->inode.iso_mode |= VREAD;
284                         inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */
285                         inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */
286                 } else
287                         ap = NULL;
288         }
289         if (!ap) {
290                 inop->inode.iso_mode |= VREAD|VEXEC|(VREAD|VEXEC)>>3|(VREAD|VEXEC)>>6;
291                 inop->inode.iso_uid = (uid_t)0;
292                 inop->inode.iso_gid = (gid_t)0;
293         }
294         if (bp2)
295                 brelse(bp2);
296 }
297
298 /*
299  * Time stamps
300  */
301 void
302 cd9660_deftstamp(struct iso_directory_record *isodir, struct iso_node *inop,
303                  struct buf *bp, enum ISO_FTYPE ftype)
304 {
305         struct buf *bp2 = NULL;
306         struct iso_mnt *imp;
307         struct iso_extended_attributes *ap = NULL;
308         int off;
309
310         if (!bp
311             && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
312             && (off = isonum_711(isodir->ext_attr_length))) {
313                 cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL,
314                              &bp2);
315                 bp = bp2;
316         }
317         if (bp) {
318                 ap = (struct iso_extended_attributes *)bp->b_data;
319                 
320                 if (ftype != ISO_FTYPE_HIGH_SIERRA
321                     && isonum_711(ap->version) == 1) {
322                         if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime))
323                                 cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime);
324                         if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime))
325                                 inop->inode.iso_ctime = inop->inode.iso_atime;
326                         if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime))
327                                 inop->inode.iso_mtime = inop->inode.iso_ctime;
328                 } else
329                         ap = NULL;
330         }
331         if (!ap) {
332                 cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime,ftype);
333                 inop->inode.iso_atime = inop->inode.iso_ctime;
334                 inop->inode.iso_mtime = inop->inode.iso_ctime;
335         }
336         if (bp2)
337                 brelse(bp2);
338 }
339
340 int
341 cd9660_tstamp_conv7(u_char *pi, struct timespec *pu, enum ISO_FTYPE ftype)
342 {
343         int crtime, days;
344         int y, m, d, hour, minute, second, tz;
345
346         y = pi[0] + 1900;
347         m = pi[1];
348         d = pi[2];
349         hour = pi[3];
350         minute = pi[4];
351         second = pi[5];
352         if(ftype != ISO_FTYPE_HIGH_SIERRA)
353                 tz = pi[6];
354         else
355                 /* original high sierra misses timezone data */
356                 tz = 0;
357
358         if (y < 1970) {
359                 pu->tv_sec  = 0;
360                 pu->tv_nsec = 0;
361                 return 0;
362         } else {
363 #ifdef  ORIGINAL
364                 /* computes day number relative to Sept. 19th,1989 */
365                 /* don't even *THINK* about changing formula. It works! */
366                 days = 367*(y-1980)-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d-100;
367 #else
368                 /*
369                  * Changed :-) to make it relative to Jan. 1st, 1970
370                  * and to disambiguate negative division
371                  */
372                 days = 367*(y-1960)-7*(y+(m+9)/12)/4-3*((y+(m+9)/12-1)/100+1)/4+275*m/9+d-239;
373 #endif
374                 crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second;
375
376                 /* timezone offset is unreliable on some disks */
377                 if (-48 <= tz && tz <= 52)
378                         crtime -= tz * 15 * 60;
379         }
380         pu->tv_sec  = crtime;
381         pu->tv_nsec = 0;
382         return 1;
383 }
384
385 static u_int
386 cd9660_chars2ui(u_char *begin, int len)
387 {
388         u_int rc;
389         
390         for (rc = 0; --len >= 0;) {
391                 rc *= 10;
392                 rc += *begin++ - '0';
393         }
394         return rc;
395 }
396
397 int
398 cd9660_tstamp_conv17(u_char *pi, struct timespec *pu)
399 {
400         u_char buf[7];
401         
402         /* year:"0001"-"9999" -> -1900  */
403         buf[0] = cd9660_chars2ui(pi,4) - 1900;
404
405         /* month: " 1"-"12"   -> 1 - 12 */
406         buf[1] = cd9660_chars2ui(pi + 4,2);
407
408         /* day:   " 1"-"31"   -> 1 - 31 */
409         buf[2] = cd9660_chars2ui(pi + 6,2);
410
411         /* hour:  " 0"-"23"   -> 0 - 23 */
412         buf[3] = cd9660_chars2ui(pi + 8,2);
413
414         /* minute:" 0"-"59"   -> 0 - 59 */
415         buf[4] = cd9660_chars2ui(pi + 10,2);
416
417         /* second:" 0"-"59"   -> 0 - 59 */
418         buf[5] = cd9660_chars2ui(pi + 12,2);
419
420         /* difference of GMT */
421         buf[6] = pi[16];
422
423         return cd9660_tstamp_conv7(buf, pu, ISO_FTYPE_DEFAULT);
424 }
425
426 ino_t
427 isodirino(struct iso_directory_record *isodir, struct iso_mnt *imp)
428 {
429         ino_t ino;
430
431         ino = (isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length))
432               << imp->im_bshift;
433         return (ino);
434 }