Merge from vendor branch GCC:
[dragonfly.git] / sys / vfs / isofs / cd9660 / cd9660_vnops.c
1 /*-
2  * Copyright (c) 1994
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_vnops.c      8.19 (Berkeley) 5/27/95
39  * $FreeBSD: src/sys/isofs/cd9660/cd9660_vnops.c,v 1.62 1999/12/15 23:01:51 eivind Exp $
40  * $DragonFly: src/sys/vfs/isofs/cd9660/cd9660_vnops.c,v 1.8 2004/04/12 23:18:55 cpressey Exp $
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/namei.h>
47 #include <sys/kernel.h>
48 #include <sys/stat.h>
49 #include <sys/buf.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <vfs/fifofs/fifo.h>
53 #include <sys/malloc.h>
54 #include <sys/dirent.h>
55 #include <sys/unistd.h>
56 #include <sys/filio.h>
57
58 #include <vm/vm.h>
59 #include <vm/vm_zone.h>
60 #include <vm/vnode_pager.h>
61
62 #include "iso.h"
63 #include "cd9660_node.h"
64 #include "iso_rrip.h"
65
66 static int cd9660_setattr (struct vop_setattr_args *);
67 static int cd9660_access (struct vop_access_args *);
68 static int cd9660_getattr (struct vop_getattr_args *);
69 static int cd9660_ioctl (struct vop_ioctl_args *);
70 static int cd9660_pathconf (struct vop_pathconf_args *);
71 static int cd9660_read (struct vop_read_args *);
72 struct isoreaddir;
73 static int iso_uiodir (struct isoreaddir *idp, struct dirent *dp,
74                            off_t off);
75 static int iso_shipdir (struct isoreaddir *idp);
76 static int cd9660_readdir (struct vop_readdir_args *);
77 static int cd9660_readlink (struct vop_readlink_args *ap);
78 static int cd9660_strategy (struct vop_strategy_args *);
79 static int cd9660_print (struct vop_print_args *);
80 static int cd9660_getpages (struct vop_getpages_args *);
81 static int cd9660_putpages (struct vop_putpages_args *);
82
83 /*
84  * Setattr call. Only allowed for block and character special devices.
85  *
86  * cd9660_setattr(struct vnodeop_desc *a_desc, struct vnode *a_vp,
87  *                struct vattr *a_vap, struct ucred *a_cred, struct proc *a_p)
88  */
89 int
90 cd9660_setattr(struct vop_setattr_args *ap)
91 {
92         struct vnode *vp = ap->a_vp;
93         struct vattr *vap = ap->a_vap;
94
95         if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
96             vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
97             vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
98                 return (EROFS);
99         if (vap->va_size != (u_quad_t)VNOVAL) {
100                 switch (vp->v_type) {
101                 case VDIR:
102                         return (EISDIR);
103                 case VLNK:
104                 case VREG:
105                         return (EROFS);
106                 case VCHR:
107                 case VBLK:
108                 case VSOCK:
109                 case VFIFO:
110                 case VNON:
111                 case VBAD:
112                         return (0);
113                 }
114         }
115         return (0);
116 }
117
118 /*
119  * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
120  * The mode is shifted to select the owner/group/other fields. The
121  * super user is granted all permissions.
122  *
123  * cd9660_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
124  *               struct proc *a_p)
125  */
126 /* ARGSUSED */
127 static int
128 cd9660_access(struct vop_access_args *ap)
129 {
130         struct vnode *vp = ap->a_vp;
131         struct iso_node *ip = VTOI(vp);
132         struct ucred *cred = ap->a_cred;
133         mode_t mask, mode = ap->a_mode;
134         gid_t *gp;
135         int i;
136
137         /*
138          * Disallow write attempts unless the file is a socket,
139          * fifo, or a block or character device resident on the
140          * file system.
141          */
142         if (mode & VWRITE) {
143                 switch (vp->v_type) {
144                 case VDIR:
145                 case VLNK:
146                 case VREG:
147                         return (EROFS);
148                         /* NOT REACHED */
149                 default:
150                         break;
151                 }
152         }
153
154         /* User id 0 always gets access. */
155         if (cred->cr_uid == 0)
156                 return (0);
157
158         mask = 0;
159
160         /* Otherwise, check the owner. */
161         if (cred->cr_uid == ip->inode.iso_uid) {
162                 if (mode & VEXEC)
163                         mask |= S_IXUSR;
164                 if (mode & VREAD)
165                         mask |= S_IRUSR;
166                 if (mode & VWRITE)
167                         mask |= S_IWUSR;
168                 return ((ip->inode.iso_mode & mask) == mask ? 0 : EACCES);
169         }
170
171         /* Otherwise, check the groups. */
172         for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
173                 if (ip->inode.iso_gid == *gp) {
174                         if (mode & VEXEC)
175                                 mask |= S_IXGRP;
176                         if (mode & VREAD)
177                                 mask |= S_IRGRP;
178                         if (mode & VWRITE)
179                                 mask |= S_IWGRP;
180                         return ((ip->inode.iso_mode & mask) == mask ?
181                             0 : EACCES);
182                 }
183
184         /* Otherwise, check everyone else. */
185         if (mode & VEXEC)
186                 mask |= S_IXOTH;
187         if (mode & VREAD)
188                 mask |= S_IROTH;
189         if (mode & VWRITE)
190                 mask |= S_IWOTH;
191         return ((ip->inode.iso_mode & mask) == mask ? 0 : EACCES);
192 }
193
194 /*
195  * cd9660_getattr(struct vnode *a_vp, struct vattr *a_vap, struct thread *a_td)
196  */
197 static int
198 cd9660_getattr(struct vop_getattr_args *ap)
199 {
200         struct vnode *vp = ap->a_vp;
201         struct vattr *vap = ap->a_vap;
202         struct iso_node *ip = VTOI(vp);
203
204         vap->va_fsid    = dev2udev(ip->i_dev);
205         vap->va_fileid  = ip->i_number;
206
207         vap->va_mode    = ip->inode.iso_mode;
208         vap->va_nlink   = ip->inode.iso_links;
209         vap->va_uid     = ip->inode.iso_uid;
210         vap->va_gid     = ip->inode.iso_gid;
211         vap->va_atime   = ip->inode.iso_atime;
212         vap->va_mtime   = ip->inode.iso_mtime;
213         vap->va_ctime   = ip->inode.iso_ctime;
214         vap->va_rdev    = ip->inode.iso_rdev;
215
216         vap->va_size    = (u_quad_t) ip->i_size;
217         if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
218                 struct vop_readlink_args rdlnk;
219                 struct iovec aiov;
220                 struct uio auio;
221                 char *cp;
222
223                 MALLOC(cp, char *, MAXPATHLEN, M_TEMP, M_WAITOK);
224                 aiov.iov_base = cp;
225                 aiov.iov_len = MAXPATHLEN;
226                 auio.uio_iov = &aiov;
227                 auio.uio_iovcnt = 1;
228                 auio.uio_offset = 0;
229                 auio.uio_rw = UIO_READ;
230                 auio.uio_segflg = UIO_SYSSPACE;
231                 auio.uio_td = ap->a_td;
232                 auio.uio_resid = MAXPATHLEN;
233                 rdlnk.a_uio = &auio;
234                 rdlnk.a_vp = ap->a_vp;
235                 rdlnk.a_cred = proc0.p_ucred; /* use root cred */
236                 if (cd9660_readlink(&rdlnk) == 0)
237                         vap->va_size = MAXPATHLEN - auio.uio_resid;
238                 FREE(cp, M_TEMP);
239         }
240         vap->va_flags   = 0;
241         vap->va_gen = 1;
242         vap->va_blocksize = ip->i_mnt->logical_block_size;
243         vap->va_bytes   = (u_quad_t) ip->i_size;
244         vap->va_type    = vp->v_type;
245         vap->va_filerev = 0;
246         return (0);
247 }
248
249 /*
250  * Vnode op for ioctl.
251  *
252  * cd9660_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
253  *              int a_fflag, struct ucred *a_cred, struct proc *a_p)
254  */
255 static int
256 cd9660_ioctl(struct vop_ioctl_args *ap)
257 {
258         struct vnode *vp = ap->a_vp;
259         struct iso_node *ip = VTOI(vp);
260
261         switch (ap->a_command) {
262
263         case FIOGETLBA:
264                 *(int *)(ap->a_data) = ip->iso_start;
265                 return 0;
266         default:
267                 return (ENOTTY);
268         }
269 }
270
271 /*
272  * Vnode op for reading.
273  *
274  * cd9660_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
275  *              struct ucred *a_cred)
276  */
277 static int
278 cd9660_read(struct vop_read_args *ap)
279 {
280         struct vnode *vp = ap->a_vp;
281         struct uio *uio = ap->a_uio;
282         struct iso_node *ip = VTOI(vp);
283         struct iso_mnt *imp;
284         struct buf *bp;
285         daddr_t lbn, rablock;
286         off_t diff;
287         int rasize, error = 0;
288         int seqcount;
289         long size, n, on;
290
291         seqcount = ap->a_ioflag >> 16;
292
293         if (uio->uio_resid == 0)
294                 return (0);
295         if (uio->uio_offset < 0)
296                 return (EINVAL);
297         ip->i_flag |= IN_ACCESS;
298         imp = ip->i_mnt;
299         do {
300                 lbn = lblkno(imp, uio->uio_offset);
301                 on = blkoff(imp, uio->uio_offset);
302                 n = min((u_int)(imp->logical_block_size - on),
303                         uio->uio_resid);
304                 diff = (off_t)ip->i_size - uio->uio_offset;
305                 if (diff <= 0)
306                         return (0);
307                 if (diff < n)
308                         n = diff;
309                 size = blksize(imp, ip, lbn);
310                 rablock = lbn + 1;
311                 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
312                         if (lblktosize(imp, rablock) < ip->i_size)
313                                 error = cluster_read(vp, (off_t)ip->i_size,
314                                          lbn, size, uio->uio_resid,
315                                          (ap->a_ioflag >> 16), &bp);
316                         else
317                                 error = bread(vp, lbn, size, &bp);
318                 } else {
319                         if (seqcount > 1 &&
320                             lblktosize(imp, rablock) < ip->i_size) {
321                                 rasize = blksize(imp, ip, rablock);
322                                 error = breadn(vp, lbn, size, &rablock,
323                                                &rasize, 1, &bp);
324                         } else
325                                 error = bread(vp, lbn, size, &bp);
326                 }
327                 n = min(n, size - bp->b_resid);
328                 if (error) {
329                         brelse(bp);
330                         return (error);
331                 }
332
333                 error = uiomove(bp->b_data + on, (int)n, uio);
334                 brelse(bp);
335         } while (error == 0 && uio->uio_resid > 0 && n != 0);
336         return (error);
337 }
338
339 /*
340  * Structure for reading directories
341  */
342 struct isoreaddir {
343         struct dirent saveent;
344         struct dirent assocent;
345         struct dirent current;
346         off_t saveoff;
347         off_t assocoff;
348         off_t curroff;
349         struct uio *uio;
350         off_t uio_off;
351         int eofflag;
352         u_long *cookies;
353         int ncookies;
354 };
355
356 int
357 iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off)
358 {
359         int error;
360
361         dp->d_name[dp->d_namlen] = 0;
362         dp->d_reclen = GENERIC_DIRSIZ(dp);
363
364         if (idp->uio->uio_resid < dp->d_reclen) {
365                 idp->eofflag = 0;
366                 return (-1);
367         }
368
369         if (idp->cookies) {
370                 if (idp->ncookies <= 0) {
371                         idp->eofflag = 0;
372                         return (-1);
373                 }
374
375                 *idp->cookies++ = off;
376                 --idp->ncookies;
377         }
378
379         if ((error = uiomove((caddr_t) dp,dp->d_reclen,idp->uio)) != 0)
380                 return (error);
381         idp->uio_off = off;
382         return (0);
383 }
384
385 int
386 iso_shipdir(struct isoreaddir *idp)
387 {
388         struct dirent *dp;
389         int cl, sl, assoc;
390         int error;
391         char *cname, *sname;
392
393         cl = idp->current.d_namlen;
394         cname = idp->current.d_name;
395 assoc = (cl > 1) && (*cname == ASSOCCHAR);
396         if (assoc) {
397                 cl--;
398                 cname++;
399         }
400
401         dp = &idp->saveent;
402         sname = dp->d_name;
403         if (!(sl = dp->d_namlen)) {
404                 dp = &idp->assocent;
405                 sname = dp->d_name + 1;
406                 sl = dp->d_namlen - 1;
407         }
408         if (sl > 0) {
409                 if (sl != cl
410                     || bcmp(sname,cname,sl)) {
411                         if (idp->assocent.d_namlen) {
412                                 if ((error = iso_uiodir(idp,&idp->assocent,idp->assocoff)) != 0)
413                                         return (error);
414                                 idp->assocent.d_namlen = 0;
415                         }
416                         if (idp->saveent.d_namlen) {
417                                 if ((error = iso_uiodir(idp,&idp->saveent,idp->saveoff)) != 0)
418                                         return (error);
419                                 idp->saveent.d_namlen = 0;
420                         }
421                 }
422         }
423         idp->current.d_reclen = GENERIC_DIRSIZ(&idp->current);
424         if (assoc) {
425                 idp->assocoff = idp->curroff;
426                 bcopy(&idp->current,&idp->assocent,idp->current.d_reclen);
427         } else {
428                 idp->saveoff = idp->curroff;
429                 bcopy(&idp->current,&idp->saveent,idp->current.d_reclen);
430         }
431         return (0);
432 }
433
434 /*
435  * Vnode op for readdir
436  *
437  * cd9660_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
438  *                int *a_eofflag, int *a_ncookies, u_long *a_cookies)
439  */
440 static int
441 cd9660_readdir(struct vop_readdir_args *ap)
442 {
443         struct uio *uio = ap->a_uio;
444         struct isoreaddir *idp;
445         struct vnode *vdp = ap->a_vp;
446         struct iso_node *dp;
447         struct iso_mnt *imp;
448         struct buf *bp = NULL;
449         struct iso_directory_record *ep;
450         int entryoffsetinblock;
451         doff_t endsearch;
452         u_long bmask;
453         int error = 0;
454         int reclen;
455         u_short namelen;
456         int ncookies = 0;
457         u_long *cookies = NULL;
458
459         dp = VTOI(vdp);
460         imp = dp->i_mnt;
461         bmask = imp->im_bmask;
462
463         MALLOC(idp, struct isoreaddir *, sizeof(*idp), M_TEMP, M_WAITOK);
464         idp->saveent.d_namlen = idp->assocent.d_namlen = 0;
465         /*
466          * XXX
467          * Is it worth trying to figure out the type?
468          */
469         idp->saveent.d_type = idp->assocent.d_type = idp->current.d_type =
470             DT_UNKNOWN;
471         idp->uio = uio;
472         if (ap->a_ncookies == NULL) {
473                 idp->cookies = NULL;
474         } else {
475                 /*
476                  * Guess the number of cookies needed.
477                  */
478                 ncookies = uio->uio_resid / 16;
479                 MALLOC(cookies, u_long *, ncookies * sizeof(u_int), M_TEMP,
480                     M_WAITOK);
481                 idp->cookies = cookies;
482                 idp->ncookies = ncookies;
483         }
484         idp->eofflag = 1;
485         idp->curroff = uio->uio_offset;
486
487         if ((entryoffsetinblock = idp->curroff & bmask) &&
488             (error = cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
489                 FREE(idp, M_TEMP);
490                 return (error);
491         }
492         endsearch = dp->i_size;
493
494         while (idp->curroff < endsearch) {
495                 /*
496                  * If offset is on a block boundary,
497                  * read the next directory block.
498                  * Release previous if it exists.
499                  */
500                 if ((idp->curroff & bmask) == 0) {
501                         if (bp != NULL)
502                                 brelse(bp);
503                         if ((error =
504                             cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp)) != 0)
505                                 break;
506                         entryoffsetinblock = 0;
507                 }
508                 /*
509                  * Get pointer to next entry.
510                  */
511                 ep = (struct iso_directory_record *)
512                         ((char *)bp->b_data + entryoffsetinblock);
513
514                 reclen = isonum_711(ep->length);
515                 if (reclen == 0) {
516                         /* skip to next block, if any */
517                         idp->curroff =
518                             (idp->curroff & ~bmask) + imp->logical_block_size;
519                         continue;
520                 }
521
522                 if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
523                         error = EINVAL;
524                         /* illegal entry, stop */
525                         break;
526                 }
527
528                 if (entryoffsetinblock + reclen > imp->logical_block_size) {
529                         error = EINVAL;
530                         /* illegal directory, so stop looking */
531                         break;
532                 }
533
534                 idp->current.d_namlen = isonum_711(ep->name_len);
535
536                 if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.d_namlen) {
537                         error = EINVAL;
538                         /* illegal entry, stop */
539                         break;
540                 }
541
542                 if (isonum_711(ep->flags)&2)
543                         idp->current.d_fileno = isodirino(ep, imp);
544                 else
545                         idp->current.d_fileno = dbtob(bp->b_blkno) +
546                                 entryoffsetinblock;
547
548                 idp->curroff += reclen;
549
550                 switch (imp->iso_ftype) {
551                 case ISO_FTYPE_RRIP:
552                         cd9660_rrip_getname(ep,idp->current.d_name, &namelen,
553                                            &idp->current.d_fileno,imp);
554                         idp->current.d_namlen = (u_char)namelen;
555                         if (idp->current.d_namlen)
556                                 error = iso_uiodir(idp,&idp->current,idp->curroff);
557                         break;
558                 default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
559                         strcpy(idp->current.d_name,"..");
560                         if (idp->current.d_namlen == 1 && ep->name[0] == 0) {
561                                 idp->current.d_namlen = 1;
562                                 error = iso_uiodir(idp,&idp->current,idp->curroff);
563                         } else if (idp->current.d_namlen == 1 && ep->name[0] == 1) {
564                                 idp->current.d_namlen = 2;
565                                 error = iso_uiodir(idp,&idp->current,idp->curroff);
566                         } else {
567                                 isofntrans(ep->name,idp->current.d_namlen,
568                                            idp->current.d_name, &namelen,
569                                            imp->iso_ftype == ISO_FTYPE_9660,
570                                            isonum_711(ep->flags)&4,
571                                            imp->joliet_level);
572                                 idp->current.d_namlen = (u_char)namelen;
573                                 if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
574                                         error = iso_shipdir(idp);
575                                 else
576                                         error = iso_uiodir(idp,&idp->current,idp->curroff);
577                         }
578                 }
579                 if (error)
580                         break;
581
582                 entryoffsetinblock += reclen;
583         }
584
585         if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
586                 idp->current.d_namlen = 0;
587                 error = iso_shipdir(idp);
588         }
589         if (error < 0)
590                 error = 0;
591
592         if (ap->a_ncookies != NULL) {
593                 if (error)
594                         free(cookies, M_TEMP);
595                 else {
596                         /*
597                          * Work out the number of cookies actually used.
598                          */
599                         *ap->a_ncookies = ncookies - idp->ncookies;
600                         *ap->a_cookies = cookies;
601                 }
602         }
603
604         if (bp)
605                 brelse (bp);
606
607         uio->uio_offset = idp->uio_off;
608         *ap->a_eofflag = idp->eofflag;
609
610         FREE(idp, M_TEMP);
611
612         return (error);
613 }
614
615 /*
616  * Return target name of a symbolic link
617  * Shouldn't we get the parent vnode and read the data from there?
618  * This could eventually result in deadlocks in cd9660_lookup.
619  * But otherwise the block read here is in the block buffer two times.
620  */
621 typedef struct iso_directory_record ISODIR;
622 typedef struct iso_node             ISONODE;
623 typedef struct iso_mnt              ISOMNT;
624 /*
625  * cd9660_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
626  */
627 static int
628 cd9660_readlink(struct vop_readlink_args *ap)
629 {
630         ISONODE *ip;
631         ISODIR  *dirp;
632         ISOMNT  *imp;
633         struct  buf *bp;
634         struct  uio *uio;
635         u_short symlen;
636         int     error;
637         char    *symname;
638
639         ip  = VTOI(ap->a_vp);
640         imp = ip->i_mnt;
641         uio = ap->a_uio;
642
643         if (imp->iso_ftype != ISO_FTYPE_RRIP)
644                 return (EINVAL);
645
646         /*
647          * Get parents directory record block that this inode included.
648          */
649         error = bread(imp->im_devvp,
650                       (ip->i_number >> imp->im_bshift) <<
651                       (imp->im_bshift - DEV_BSHIFT),
652                       imp->logical_block_size, &bp);
653         if (error) {
654                 brelse(bp);
655                 return (EINVAL);
656         }
657
658         /*
659          * Setup the directory pointer for this inode
660          */
661         dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
662
663         /*
664          * Just make sure, we have a right one....
665          *   1: Check not cross boundary on block
666          */
667         if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
668             > (unsigned)imp->logical_block_size) {
669                 brelse(bp);
670                 return (EINVAL);
671         }
672
673         /*
674          * Now get a buffer
675          * Abuse a namei buffer for now.
676          */
677         if (uio->uio_segflg == UIO_SYSSPACE)
678                 symname = uio->uio_iov->iov_base;
679         else
680                 symname = zalloc(namei_zone);
681         
682         /*
683          * Ok, we just gathering a symbolic name in SL record.
684          */
685         if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
686                 if (uio->uio_segflg != UIO_SYSSPACE)
687                         zfree(namei_zone, symname);
688                 brelse(bp);
689                 return (EINVAL);
690         }
691         /*
692          * Don't forget before you leave from home ;-)
693          */
694         brelse(bp);
695
696         /*
697          * return with the symbolic name to caller's.
698          */
699         if (uio->uio_segflg != UIO_SYSSPACE) {
700                 error = uiomove(symname, symlen, uio);
701                 zfree(namei_zone, symname);
702                 return (error);
703         }
704         uio->uio_resid -= symlen;
705         uio->uio_iov->iov_base += symlen;
706         uio->uio_iov->iov_len -= symlen;
707         return (0);
708 }
709
710 /*
711  * Calculate the logical to physical mapping if not done already,
712  * then call the device strategy routine.
713  *
714  * cd9660_strategy(struct buf *a_vp, struct buf *a_bp)
715  */
716 static int
717 cd9660_strategy(struct vop_strategy_args *ap)
718 {
719         struct buf *bp = ap->a_bp;
720         struct vnode *vp = bp->b_vp;
721         struct iso_node *ip;
722         int error;
723
724         ip = VTOI(vp);
725         if (vp->v_type == VBLK || vp->v_type == VCHR)
726                 panic("cd9660_strategy: spec");
727         if (bp->b_blkno == bp->b_lblkno) {
728                 if ((error =
729                     VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL))) {
730                         bp->b_error = error;
731                         bp->b_flags |= B_ERROR;
732                         biodone(bp);
733                         return (error);
734                 }
735                 if ((long)bp->b_blkno == -1)
736                         clrbuf(bp);
737         }
738         if ((long)bp->b_blkno == -1) {
739                 biodone(bp);
740                 return (0);
741         }
742         vp = ip->i_devvp;
743         bp->b_dev = vp->v_rdev;
744         VOP_STRATEGY(vp, bp);
745         return (0);
746 }
747
748 /*
749  * Print out the contents of an inode.
750  *
751  * cd9660_print(struct vnode *a_vp)
752  */
753 static int
754 cd9660_print(struct vop_print_args *ap)
755 {
756         printf("tag VT_ISOFS, isofs vnode\n");
757         return (0);
758 }
759
760 /*
761  * Return POSIX pathconf information applicable to cd9660 filesystems.
762  *
763  * cd9660_pathconf(struct vnode *a_vp, int a_name, register_t *a_retval)
764  */
765 static int
766 cd9660_pathconf(struct vop_pathconf_args *ap)
767 {
768         switch (ap->a_name) {
769         case _PC_LINK_MAX:
770                 *ap->a_retval = 1;
771                 return (0);
772         case _PC_NAME_MAX:
773                 if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
774                         *ap->a_retval = NAME_MAX;
775                 else
776                         *ap->a_retval = 37;
777                 return (0);
778         case _PC_PATH_MAX:
779                 *ap->a_retval = PATH_MAX;
780                 return (0);
781         case _PC_PIPE_BUF:
782                 *ap->a_retval = PIPE_BUF;
783                 return (0);
784         case _PC_CHOWN_RESTRICTED:
785                 *ap->a_retval = 1;
786                 return (0);
787         case _PC_NO_TRUNC:
788                 *ap->a_retval = 1;
789                 return (0);
790         default:
791                 return (EINVAL);
792         }
793         /* NOTREACHED */
794 }
795
796 /*
797  * get page routine
798  *
799  * XXX By default, wimp out... note that a_offset is ignored (and always
800  * XXX has been).
801  */
802 int
803 cd9660_getpages(struct vop_getpages_args *ap)
804 {
805         return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
806                 ap->a_reqpage);
807 }
808
809 /*
810  * put page routine
811  *
812  * XXX By default, wimp out... note that a_offset is ignored (and always
813  * XXX has been).
814  */
815 int
816 cd9660_putpages(struct vop_putpages_args *ap)
817 {
818         return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
819                 ap->a_sync, ap->a_rtvals);
820 }
821
822 /*
823  * Global vfs data structures for cd9660
824  */
825 vop_t **cd9660_vnodeop_p;
826 static struct vnodeopv_entry_desc cd9660_vnodeop_entries[] = {
827         { &vop_default_desc,            (vop_t *) vop_defaultop },
828         { &vop_access_desc,             (vop_t *) cd9660_access },
829         { &vop_bmap_desc,               (vop_t *) cd9660_bmap },
830         { &vop_cachedlookup_desc,       (vop_t *) cd9660_lookup },
831         { &vop_getattr_desc,            (vop_t *) cd9660_getattr },
832         { &vop_inactive_desc,           (vop_t *) cd9660_inactive },
833         { &vop_ioctl_desc,              (vop_t *) cd9660_ioctl },
834         { &vop_islocked_desc,           (vop_t *) vop_stdislocked },
835         { &vop_lock_desc,               (vop_t *) vop_stdlock },
836         { &vop_lookup_desc,             (vop_t *) vfs_cache_lookup },
837         { &vop_pathconf_desc,           (vop_t *) cd9660_pathconf },
838         { &vop_print_desc,              (vop_t *) cd9660_print },
839         { &vop_read_desc,               (vop_t *) cd9660_read },
840         { &vop_readdir_desc,            (vop_t *) cd9660_readdir },
841         { &vop_readlink_desc,           (vop_t *) cd9660_readlink },
842         { &vop_reclaim_desc,            (vop_t *) cd9660_reclaim },
843         { &vop_setattr_desc,            (vop_t *) cd9660_setattr },
844         { &vop_strategy_desc,           (vop_t *) cd9660_strategy },
845         { &vop_unlock_desc,             (vop_t *) vop_stdunlock },
846         { &vop_getpages_desc,           (vop_t *) cd9660_getpages },
847         { &vop_putpages_desc,           (vop_t *) cd9660_putpages },
848         { NULL, NULL }
849 };
850 static struct vnodeopv_desc cd9660_vnodeop_opv_desc =
851         { &cd9660_vnodeop_p, cd9660_vnodeop_entries };
852 VNODEOP_SET(cd9660_vnodeop_opv_desc);
853
854 /*
855  * Special device vnode ops
856  */
857 vop_t **cd9660_specop_p;
858 static struct vnodeopv_entry_desc cd9660_specop_entries[] = {
859         { &vop_default_desc,            (vop_t *) spec_vnoperate },
860         { &vop_access_desc,             (vop_t *) cd9660_access },
861         { &vop_getattr_desc,            (vop_t *) cd9660_getattr },
862         { &vop_inactive_desc,           (vop_t *) cd9660_inactive },
863         { &vop_islocked_desc,           (vop_t *) vop_stdislocked },
864         { &vop_lock_desc,               (vop_t *) vop_stdlock },
865         { &vop_print_desc,              (vop_t *) cd9660_print },
866         { &vop_reclaim_desc,            (vop_t *) cd9660_reclaim },
867         { &vop_setattr_desc,            (vop_t *) cd9660_setattr },
868         { &vop_unlock_desc,             (vop_t *) vop_stdunlock },
869         { NULL, NULL }
870 };
871 static struct vnodeopv_desc cd9660_specop_opv_desc =
872         { &cd9660_specop_p, cd9660_specop_entries };
873 VNODEOP_SET(cd9660_specop_opv_desc);
874
875 vop_t **cd9660_fifoop_p;
876 static struct vnodeopv_entry_desc cd9660_fifoop_entries[] = {
877         { &vop_default_desc,            (vop_t *) fifo_vnoperate },
878         { &vop_access_desc,             (vop_t *) cd9660_access },
879         { &vop_getattr_desc,            (vop_t *) cd9660_getattr },
880         { &vop_inactive_desc,           (vop_t *) cd9660_inactive },
881         { &vop_islocked_desc,           (vop_t *) vop_stdislocked },
882         { &vop_lock_desc,               (vop_t *) vop_stdlock },
883         { &vop_print_desc,              (vop_t *) cd9660_print },
884         { &vop_reclaim_desc,            (vop_t *) cd9660_reclaim },
885         { &vop_setattr_desc,            (vop_t *) cd9660_setattr },
886         { &vop_unlock_desc,             (vop_t *) vop_stdunlock },
887         { NULL, NULL }
888 };
889 static struct vnodeopv_desc cd9660_fifoop_opv_desc =
890         { &cd9660_fifoop_p, cd9660_fifoop_entries };
891
892 VNODEOP_SET(cd9660_fifoop_opv_desc);