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