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