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