AMD64 - Refactor uio_resid and size_t assumptions.
[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.40 2008/06/19 23:27:39 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 #include <sys/objcache.h>
59
60 #include <vm/vm.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
84 /*
85  * Setattr call. Only allowed for block and character special devices.
86  *
87  * cd9660_setattr(struct vnode *a_vp, struct vattr *a_vap,
88  *                struct ucred *a_cred, struct proc *a_p)
89  */
90 int
91 cd9660_setattr(struct vop_setattr_args *ap)
92 {
93         struct vnode *vp = ap->a_vp;
94         struct vattr *vap = ap->a_vap;
95
96         if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
97             vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
98             vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
99                 return (EROFS);
100         if (vap->va_size != (u_quad_t)VNOVAL) {
101                 switch (vp->v_type) {
102                 case VDIR:
103                         return (EISDIR);
104                 case VLNK:
105                 case VREG:
106                 case VDATABASE:
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)
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_rmajor  = umajor(ip->inode.iso_rdev);
217         vap->va_rminor  = uminor(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 = curthread;
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         return(vop_stdopen(ap));
281 }
282
283 /*
284  * Vnode op for reading.
285  *
286  * cd9660_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
287  *              struct ucred *a_cred)
288  */
289 static int
290 cd9660_read(struct vop_read_args *ap)
291 {
292         struct vnode *vp = ap->a_vp;
293         struct uio *uio = ap->a_uio;
294         struct iso_node *ip = VTOI(vp);
295         struct iso_mnt *imp;
296         struct buf *bp;
297         daddr_t lbn, rablock;
298         off_t raoffset;
299         off_t loffset;
300         off_t diff;
301         int rasize, error = 0;
302         int seqcount;
303         long size, n, on;
304
305         seqcount = ap->a_ioflag >> 16;
306
307         if (uio->uio_resid == 0)
308                 return (0);
309         if (uio->uio_offset < 0)
310                 return (EINVAL);
311         ip->i_flag |= IN_ACCESS;
312         imp = ip->i_mnt;
313         do {
314                 lbn = lblkno(imp, uio->uio_offset);
315                 loffset = lblktooff(imp, lbn);
316                 on = blkoff(imp, uio->uio_offset);
317                 n = szmin((u_int)(imp->logical_block_size - on),
318                           uio->uio_resid);
319                 diff = (off_t)ip->i_size - uio->uio_offset;
320                 if (diff <= 0)
321                         return (0);
322                 if (diff < n)
323                         n = diff;
324                 size = blksize(imp, ip, lbn);
325                 rablock = lbn + 1;
326                 raoffset = lblktooff(imp, rablock);
327                 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
328                         if (raoffset < ip->i_size) {
329                                 error = cluster_read(vp, (off_t)ip->i_size,
330                                                      loffset, size,
331                                                      uio->uio_resid,
332                                                      (ap->a_ioflag >> 16),
333                                                      &bp);
334                         } else {
335                                 error = bread(vp, loffset, size, &bp);
336                         }
337                 } else {
338                         if (seqcount > 1 &&
339                             lblktosize(imp, rablock) < ip->i_size) {
340                                 rasize = blksize(imp, ip, rablock);
341                                 error = breadn(vp, loffset, size, &raoffset,
342                                                &rasize, 1, &bp);
343                         } else
344                                 error = bread(vp, loffset, size, &bp);
345                 }
346                 n = imin(n, size - bp->b_resid);
347                 if (error) {
348                         brelse(bp);
349                         return (error);
350                 }
351
352                 error = uiomove(bp->b_data + on, (int)n, uio);
353                 brelse(bp);
354         } while (error == 0 && uio->uio_resid > 0 && n != 0);
355         return (error);
356 }
357
358 /* struct dirent + enough space for the maximum supported size */
359 struct iso_dirent {
360         struct dirent de;
361         char de_name[_DIRENT_RECLEN(NAME_MAX) - sizeof(struct dirent)];
362 };
363
364 /*
365  * Structure for reading directories
366  */
367 struct isoreaddir {
368         struct iso_dirent saveent;
369         struct iso_dirent assocent;
370         struct iso_dirent current;
371         off_t saveoff;
372         off_t assocoff;
373         off_t curroff;
374         struct uio *uio;
375         off_t uio_off;
376         int eofflag;
377         off_t *cookies;
378         int ncookies;
379 };
380
381 int
382 iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off)
383 {
384         int error;
385
386         dp->d_name[dp->d_namlen] = 0;
387
388         if (idp->uio->uio_resid < _DIRENT_DIRSIZ(dp)) {
389                 idp->eofflag = 0;
390                 return (-1);
391         }
392
393         if (idp->cookies) {
394                 if (idp->ncookies <= 0) {
395                         idp->eofflag = 0;
396                         return (-1);
397                 }
398
399                 *idp->cookies++ = off;
400                 --idp->ncookies;
401         }
402
403         if ((error = uiomove((caddr_t) dp,_DIRENT_DIRSIZ(dp),idp->uio)) != 0)
404                 return (error);
405         idp->uio_off = off;
406         return (0);
407 }
408
409 int
410 iso_shipdir(struct isoreaddir *idp)
411 {
412         struct dirent *dp;
413         int cl, sl, assoc;
414         int error;
415         char *cname, *sname;
416
417         cl = idp->current.de.d_namlen;
418         cname = idp->current.de.d_name;
419 assoc = (cl > 1) && (*cname == ASSOCCHAR);
420         if (assoc) {
421                 cl--;
422                 cname++;
423         }
424
425         dp = &idp->saveent.de;
426         sname = dp->d_name;
427         if (!(sl = dp->d_namlen)) {
428                 dp = &idp->assocent.de;
429                 sname = dp->d_name + 1;
430                 sl = dp->d_namlen - 1;
431         }
432         if (sl > 0) {
433                 if (sl != cl
434                     || bcmp(sname,cname,sl)) {
435                         if (idp->assocent.de.d_namlen) {
436                                 if ((error = iso_uiodir(idp,&idp->assocent.de,idp->assocoff)) != 0)
437                                         return (error);
438                                 idp->assocent.de.d_namlen = 0;
439                         }
440                         if (idp->saveent.de.d_namlen) {
441                                 if ((error = iso_uiodir(idp,&idp->saveent.de,idp->saveoff)) != 0)
442                                         return (error);
443                                 idp->saveent.de.d_namlen = 0;
444                         }
445                 }
446         }
447         if (assoc) {
448                 idp->assocoff = idp->curroff;
449                 bcopy(&idp->current,&idp->assocent,_DIRENT_DIRSIZ(&idp->current.de));
450         } else {
451                 idp->saveoff = idp->curroff;
452                 bcopy(&idp->current,&idp->saveent,_DIRENT_DIRSIZ(&idp->current.de));
453         }
454         return (0);
455 }
456
457 /*
458  * Vnode op for readdir
459  *
460  * cd9660_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
461  *                int *a_eofflag, int *a_ncookies, off_t *a_cookies)
462  */
463 static int
464 cd9660_readdir(struct vop_readdir_args *ap)
465 {
466         struct uio *uio = ap->a_uio;
467         struct isoreaddir *idp;
468         struct vnode *vdp = ap->a_vp;
469         struct iso_node *dp;
470         struct iso_mnt *imp;
471         struct buf *bp = NULL;
472         struct iso_directory_record *ep;
473         int entryoffsetinblock;
474         doff_t endsearch;
475         u_long bmask;
476         int error = 0;
477         int reclen;
478         u_short namelen;
479         int ncookies = 0;
480         off_t *cookies = NULL;
481
482         dp = VTOI(vdp);
483         imp = dp->i_mnt;
484         bmask = imp->im_bmask;
485
486         if ((error = vn_lock(vdp, LK_EXCLUSIVE|LK_RETRY)) != 0)
487                 return (error);
488
489         MALLOC(idp, struct isoreaddir *, sizeof(*idp), M_TEMP, M_WAITOK);
490         idp->saveent.de.d_namlen = idp->assocent.de.d_namlen = 0;
491         /*
492          * XXX
493          * Is it worth trying to figure out the type?
494          */
495         idp->saveent.de.d_type = DT_UNKNOWN;
496         idp->assocent.de.d_type = DT_UNKNOWN;
497         idp->current.de.d_type = DT_UNKNOWN;
498         idp->uio = uio;
499         if (ap->a_ncookies == NULL) {
500                 idp->cookies = NULL;
501         } else {
502                 /*
503                  * Guess the number of cookies needed.  Guess at least
504                  * 1 to avoid a degenerate case in malloc, and cap at
505                  * a reasonable limit.
506                  */
507                 ncookies = uio->uio_resid / 16 + 1;
508                 if (ncookies > 1024)
509                         ncookies = 1024;
510                 MALLOC(cookies, off_t *, ncookies * sizeof(off_t),
511                        M_TEMP, M_WAITOK);
512                 idp->cookies = cookies;
513                 idp->ncookies = ncookies;
514         }
515         idp->eofflag = 1;
516         idp->curroff = uio->uio_offset;
517
518         if ((entryoffsetinblock = idp->curroff & bmask) &&
519             (error = cd9660_devblkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
520                 FREE(idp, M_TEMP);
521                 goto done;
522         }
523         endsearch = dp->i_size;
524
525         while (idp->curroff < endsearch) {
526                 /*
527                  * If offset is on a block boundary,
528                  * read the next directory block.
529                  * Release previous if it exists.
530                  */
531                 if ((idp->curroff & bmask) == 0) {
532                         if (bp != NULL)
533                                 brelse(bp);
534                         if ((error =
535                             cd9660_devblkatoff(vdp, (off_t)idp->curroff, NULL, &bp)) != 0)
536                                 break;
537                         entryoffsetinblock = 0;
538                 }
539                 /*
540                  * Get pointer to next entry.
541                  */
542                 ep = (struct iso_directory_record *)
543                         ((char *)bp->b_data + entryoffsetinblock);
544
545                 reclen = isonum_711(ep->length);
546                 if (reclen == 0) {
547                         /* skip to next block, if any */
548                         idp->curroff =
549                             (idp->curroff & ~bmask) + imp->logical_block_size;
550                         continue;
551                 }
552
553                 if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
554                         error = EINVAL;
555                         /* illegal entry, stop */
556                         break;
557                 }
558
559                 if (entryoffsetinblock + reclen > imp->logical_block_size) {
560                         error = EINVAL;
561                         /* illegal directory, so stop looking */
562                         break;
563                 }
564
565                 idp->current.de.d_namlen = isonum_711(ep->name_len);
566
567                 if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.de.d_namlen) {
568                         error = EINVAL;
569                         /* illegal entry, stop */
570                         break;
571                 }
572
573                 if (isonum_711(ep->flags)&2)
574                         idp->current.de.d_ino = isodirino(ep, imp);
575                 else
576                         idp->current.de.d_ino = bp->b_bio1.bio_offset +
577                                                 entryoffsetinblock;
578
579                 idp->curroff += reclen;
580
581                 switch (imp->iso_ftype) {
582                 case ISO_FTYPE_RRIP:
583                 {
584                         ino_t cur_fileno = idp->current.de.d_ino;       
585                         cd9660_rrip_getname(ep,idp->current.de.d_name, &namelen,
586                                            &cur_fileno,imp);
587                         idp->current.de.d_ino = cur_fileno;
588                         idp->current.de.d_namlen = namelen;
589                         if (idp->current.de.d_namlen)
590                                 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
591                         break;
592                 }
593                 default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
594                         strcpy(idp->current.de.d_name,"..");
595                         if (idp->current.de.d_namlen == 1 && ep->name[0] == 0) {
596                                 idp->current.de.d_namlen = 1;
597                                 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
598                         } else if (idp->current.de.d_namlen == 1 && ep->name[0] == 1) {
599                                 idp->current.de.d_namlen = 2;
600                                 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
601                         } else {
602                                 isofntrans(ep->name,idp->current.de.d_namlen,
603                                            idp->current.de.d_name, &namelen,
604                                            imp->iso_ftype == ISO_FTYPE_9660,
605                                            isonum_711(ep->flags)&4,
606                                            imp->joliet_level);
607                                 idp->current.de.d_namlen = namelen;
608                                 if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
609                                         error = iso_shipdir(idp);
610                                 else
611                                         error = iso_uiodir(idp,&idp->current.de,idp->curroff);
612                         }
613                 }
614                 if (error)
615                         break;
616
617                 entryoffsetinblock += reclen;
618         }
619
620         if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
621                 idp->current.de.d_namlen = 0;
622                 error = iso_shipdir(idp);
623         }
624         if (error < 0)
625                 error = 0;
626
627         if (ap->a_ncookies != NULL) {
628                 if (error)
629                         kfree(cookies, M_TEMP);
630                 else {
631                         /*
632                          * Work out the number of cookies actually used.
633                          */
634                         *ap->a_ncookies = ncookies - idp->ncookies;
635                         *ap->a_cookies = cookies;
636                 }
637         }
638
639         if (bp)
640                 brelse (bp);
641
642         uio->uio_offset = idp->uio_off;
643         *ap->a_eofflag = idp->eofflag;
644
645         FREE(idp, M_TEMP);
646
647 done:
648         vn_unlock(vdp);
649         return (error);
650 }
651
652 /*
653  * Return target name of a symbolic link
654  * Shouldn't we get the parent vnode and read the data from there?
655  * This could eventually result in deadlocks in cd9660_lookup.
656  * But otherwise the block read here is in the block buffer two times.
657  */
658 typedef struct iso_directory_record ISODIR;
659 typedef struct iso_node             ISONODE;
660 typedef struct iso_mnt              ISOMNT;
661 /*
662  * cd9660_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
663  */
664 static int
665 cd9660_readlink(struct vop_readlink_args *ap)
666 {
667         ISONODE *ip;
668         ISODIR  *dirp;
669         ISOMNT  *imp;
670         struct  buf *bp;
671         struct  uio *uio;
672         u_short symlen;
673         int     error;
674         char    *symname;
675
676         ip  = VTOI(ap->a_vp);
677         imp = ip->i_mnt;
678         uio = ap->a_uio;
679
680         if (imp->iso_ftype != ISO_FTYPE_RRIP)
681                 return (EINVAL);
682
683         /*
684          * Get parents directory record block that this inode included.
685          */
686         error = bread(imp->im_devvp,
687                         (off_t)ip->i_number & ~((1 << imp->im_bshift) - 1),
688                       imp->logical_block_size, &bp);
689         if (error) {
690                 brelse(bp);
691                 return (EINVAL);
692         }
693
694         /*
695          * Setup the directory pointer for this inode
696          */
697         dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
698
699         /*
700          * Just make sure, we have a right one....
701          *   1: Check not cross boundary on block
702          */
703         if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
704             > (unsigned)imp->logical_block_size) {
705                 brelse(bp);
706                 return (EINVAL);
707         }
708
709         /*
710          * Now get a buffer
711          * Abuse a namei buffer for now.
712          */
713         if (uio->uio_segflg == UIO_SYSSPACE)
714                 symname = uio->uio_iov->iov_base;
715         else
716                 symname = objcache_get(namei_oc, M_WAITOK);
717         
718         /*
719          * Ok, we just gathering a symbolic name in SL record.
720          */
721         if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
722                 if (uio->uio_segflg != UIO_SYSSPACE)
723                         objcache_put(namei_oc, symname);
724                 brelse(bp);
725                 return (EINVAL);
726         }
727         /*
728          * Don't forget before you leave from home ;-)
729          */
730         brelse(bp);
731
732         /*
733          * return with the symbolic name to caller's.
734          */
735         if (uio->uio_segflg != UIO_SYSSPACE) {
736                 error = uiomove(symname, symlen, uio);
737                 objcache_put(namei_oc, symname);
738                 return (error);
739         }
740         uio->uio_resid -= symlen;
741         uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
742         uio->uio_iov->iov_len -= symlen;
743         return (0);
744 }
745
746 /*
747  * Calculate the logical to physical mapping if not done already,
748  * then call the device strategy routine.
749  *
750  * cd9660_strategy(struct buf *a_vp, struct buf *a_bio)
751  */
752 static int
753 cd9660_strategy(struct vop_strategy_args *ap)
754 {
755         struct bio *bio = ap->a_bio;
756         struct bio *nbio;
757         struct buf *bp = bio->bio_buf;
758         struct vnode *vp = ap->a_vp;
759         struct iso_node *ip;
760         int error;
761
762         ip = VTOI(vp);
763         if (vp->v_type == VBLK || vp->v_type == VCHR)
764                 panic("cd9660_strategy: spec");
765         nbio = push_bio(bio);
766         if (nbio->bio_offset == NOOFFSET) {
767                 error = VOP_BMAP(vp, bio->bio_offset,
768                                  &nbio->bio_offset, NULL, NULL, bp->b_cmd);
769                 if (error) {
770                         bp->b_error = error;
771                         bp->b_flags |= B_ERROR;
772                         /* I/O was never started on nbio, must biodone(bio) */
773                         biodone(bio);
774                         return (error);
775                 }
776                 if (nbio->bio_offset == NOOFFSET)
777                         clrbuf(bp);
778         }
779         if (nbio->bio_offset == NOOFFSET) {
780                 /* I/O was never started on nbio, must biodone(bio) */
781                 biodone(bio);
782                 return (0);
783         }
784         vp = ip->i_devvp;
785         vn_strategy(vp, nbio);
786         return (0);
787 }
788
789 /*
790  * Print out the contents of an inode.
791  *
792  * cd9660_print(struct vnode *a_vp)
793  */
794 static int
795 cd9660_print(struct vop_print_args *ap)
796 {
797         kprintf("tag VT_ISOFS, isofs vnode\n");
798         return (0);
799 }
800
801 /*
802  * Return POSIX pathconf information applicable to cd9660 filesystems.
803  *
804  * cd9660_pathconf(struct vnode *a_vp, int a_name, register_t *a_retval)
805  */
806 static int
807 cd9660_pathconf(struct vop_pathconf_args *ap)
808 {
809         switch (ap->a_name) {
810         case _PC_LINK_MAX:
811                 *ap->a_retval = 1;
812                 return (0);
813         case _PC_NAME_MAX:
814                 if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
815                         *ap->a_retval = NAME_MAX;
816                 else
817                         *ap->a_retval = 37;
818                 return (0);
819         case _PC_PATH_MAX:
820                 *ap->a_retval = PATH_MAX;
821                 return (0);
822         case _PC_PIPE_BUF:
823                 *ap->a_retval = PIPE_BUF;
824                 return (0);
825         case _PC_CHOWN_RESTRICTED:
826                 *ap->a_retval = 1;
827                 return (0);
828         case _PC_NO_TRUNC:
829                 *ap->a_retval = 1;
830                 return (0);
831         default:
832                 return (EINVAL);
833         }
834         /* NOTREACHED */
835 }
836
837 /*
838  * Advisory lock support
839  */
840 static int
841 cd9660_advlock(struct vop_advlock_args *ap)
842 {
843         struct iso_node *ip = VTOI(ap->a_vp);
844         return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
845 }
846
847
848 /*
849  * Global vfs data structures for cd9660
850  */
851 struct vop_ops cd9660_vnode_vops = {
852         .vop_default =          vop_defaultop,
853         .vop_open =             cd9660_open,
854         .vop_access =           cd9660_access,
855         .vop_advlock =          cd9660_advlock,
856         .vop_bmap =             cd9660_bmap,
857         .vop_old_lookup =       cd9660_lookup,
858         .vop_getattr =          cd9660_getattr,
859         .vop_inactive =         cd9660_inactive,
860         .vop_ioctl =            cd9660_ioctl,
861         .vop_pathconf =         cd9660_pathconf,
862         .vop_print =            cd9660_print,
863         .vop_read =             cd9660_read,
864         .vop_readdir =          cd9660_readdir,
865         .vop_readlink =         cd9660_readlink,
866         .vop_reclaim =          cd9660_reclaim,
867         .vop_setattr =          cd9660_setattr,
868         .vop_strategy =         cd9660_strategy,
869         .vop_getpages =         vop_stdgetpages,
870         .vop_putpages =         vop_stdputpages
871 };
872
873 /*
874  * Special device vnode ops
875  */
876 struct vop_ops cd9660_spec_vops = {
877         .vop_default =          vop_defaultop,
878         .vop_read =             vop_stdnoread,
879         .vop_access =           cd9660_access,
880         .vop_getattr =          cd9660_getattr,
881         .vop_inactive =         cd9660_inactive,
882         .vop_print =            cd9660_print,
883         .vop_reclaim =          cd9660_reclaim,
884         .vop_setattr =          cd9660_setattr,
885 };
886
887 struct vop_ops cd9660_fifo_vops = {
888         .vop_default =          fifo_vnoperate,
889         .vop_access =           cd9660_access,
890         .vop_getattr =          cd9660_getattr,
891         .vop_inactive =         cd9660_inactive,
892         .vop_print =            cd9660_print,
893         .vop_reclaim =          cd9660_reclaim,
894         .vop_setattr =          cd9660_setattr,
895 };
896