kernel - fine-grained namecache and partial vnode MPSAFE work
[dragonfly.git] / sys / vfs / isofs / cd9660 / cd9660_vfsops.c
CommitLineData
984263bc
MD
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_vfsops.c 8.18 (Berkeley) 5/22/95
39 * $FreeBSD: src/sys/isofs/cd9660/cd9660_vfsops.c,v 1.74.2.7 2002/04/08 09:39:29 bde Exp $
67863d04 40 * $DragonFly: src/sys/vfs/isofs/cd9660/cd9660_vfsops.c,v 1.46 2008/09/17 21:44:23 dillon Exp $
984263bc
MD
41 */
42
43#include <sys/param.h>
44#include <sys/systm.h>
984263bc 45#include <sys/proc.h>
895c1f85 46#include <sys/priv.h>
fad57d0e 47#include <sys/nlookup.h>
984263bc
MD
48#include <sys/kernel.h>
49#include <sys/vnode.h>
50#include <sys/mount.h>
51#include <sys/buf.h>
52#include <sys/cdio.h>
53#include <sys/conf.h>
54#include <sys/fcntl.h>
55#include <sys/malloc.h>
56#include <sys/stat.h>
57#include <sys/syslog.h>
60c47c5e 58#include <sys/iconv.h>
984263bc
MD
59
60#include <vm/vm_zone.h>
61
1f2de5d4
MD
62#include "iso.h"
63#include "iso_rrip.h"
64#include "cd9660_node.h"
65#include "cd9660_mount.h"
984263bc 66
66a1ddf5
MD
67extern struct vop_ops cd9660_vnode_vops;
68extern struct vop_ops cd9660_spec_vops;
69extern struct vop_ops cd9660_fifo_vops;
0961aa92 70
984263bc
MD
71MALLOC_DEFINE(M_ISOFSMNT, "ISOFS mount", "ISOFS mount structure");
72MALLOC_DEFINE(M_ISOFSNODE, "ISOFS node", "ISOFS vnode private part");
73
60c47c5e
AP
74struct iconv_functions *cd9660_iconv = NULL;
75
acde96db
MD
76static int cd9660_mount (struct mount *, char *, caddr_t, struct ucred *);
77static int cd9660_unmount (struct mount *, int);
a6ee311a 78static int cd9660_root (struct mount *, struct vnode **);
acde96db 79static int cd9660_statfs (struct mount *, struct statfs *, struct ucred *);
b9b0a6d0 80static int cd9660_vget (struct mount *, struct vnode *, ino_t, struct vnode **);
67863d04
MD
81static int cd9660_fhtovp (struct mount *, struct vnode *rootvp,
82 struct fid *, struct vnode **);
a6ee311a
RG
83static int cd9660_checkexp (struct mount *, struct sockaddr *,
84 int *, struct ucred **);
85static int cd9660_vptofh (struct vnode *, struct fid *);
984263bc
MD
86
87static struct vfsops cd9660_vfsops = {
43c45e8f
HP
88 .vfs_mount = cd9660_mount,
89 .vfs_unmount = cd9660_unmount,
90 .vfs_root = cd9660_root,
91 .vfs_statfs = cd9660_statfs,
92 .vfs_sync = vfs_stdsync,
93 .vfs_vget = cd9660_vget,
94 .vfs_fhtovp = cd9660_fhtovp,
95 .vfs_checkexp = cd9660_checkexp,
96 .vfs_vptofh = cd9660_vptofh,
97 .vfs_init = cd9660_init,
98 .vfs_uninit = cd9660_uninit,
984263bc
MD
99};
100VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY);
101MODULE_VERSION(cd9660, 1);
102
103
104/*
105 * Called by vfs_mountroot when iso is going to be mounted as root.
106 */
107
b13267a5 108static int iso_get_ssector (cdev_t dev);
a6ee311a 109static int iso_mountfs (struct vnode *devvp, struct mount *mp,
acde96db 110 struct iso_args *argp);
984263bc
MD
111
112/*
113 * Try to find the start of the last data track on this CD-ROM. This
114 * is used to mount the last session of a multi-session CD. Bail out
115 * and return 0 if we fail, this is always a safe bet.
116 */
117static int
b13267a5 118iso_get_ssector(cdev_t dev)
984263bc
MD
119{
120 struct ioc_toc_header h;
121 struct ioc_read_toc_single_entry t;
122 int i;
984263bc 123
87baaf0c
MD
124 if (dev_dioctl(dev, CDIOREADTOCHEADER, (caddr_t)&h, FREAD,
125 proc0.p_ucred, NULL) != 0)
984263bc
MD
126 return 0;
127
128 for (i = h.ending_track; i >= 0; i--) {
129 t.address_format = CD_LBA_FORMAT;
130 t.track = i;
87baaf0c
MD
131 if (dev_dioctl(dev, CDIOREADTOCENTRY, (caddr_t)&t, FREAD,
132 proc0.p_ucred, NULL) != 0) {
984263bc 133 return 0;
335dda38 134 }
984263bc
MD
135 if ((t.entry.control & 4) != 0)
136 /* found a data track */
137 break;
138 }
139
140 if (i < 0)
141 return 0;
142
143 return ntohl(t.entry.addr.lba);
144}
145
984263bc 146static int
acde96db 147iso_mountroot(struct mount *mp)
984263bc
MD
148{
149 struct iso_args args;
21739618 150 struct vnode *rootvp;
984263bc
MD
151 int error;
152
153 if ((error = bdevvp(rootdev, &rootvp))) {
086c1d7e 154 kprintf("iso_mountroot: can't find rootvp\n");
984263bc
MD
155 return (error);
156 }
157 args.flags = ISOFSMNT_ROOT;
158
ca466bae 159 vn_lock(rootvp, LK_EXCLUSIVE | LK_RETRY);
87de5057 160 error = VOP_OPEN(rootvp, FREAD, FSCRED, NULL);
a11aaa81 161 vn_unlock(rootvp);
984263bc
MD
162 if (error)
163 return (error);
164
acde96db 165 args.ssector = iso_get_ssector(rootdev);
984263bc 166
87de5057 167 VOP_CLOSE(rootvp, FREAD);
984263bc
MD
168
169 if (bootverbose)
086c1d7e 170 kprintf("iso_mountroot(): using session at block %d\n",
984263bc 171 args.ssector);
acde96db 172 if ((error = iso_mountfs(rootvp, mp, &args)) != 0)
984263bc
MD
173 return (error);
174
acde96db 175 cd9660_statfs(mp, &mp->mnt_stat, proc0.p_ucred);
984263bc
MD
176 return (0);
177}
178
179/*
180 * VFS Operations.
181 *
182 * mount system call
183 */
184static int
acde96db 185cd9660_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
984263bc
MD
186{
187 struct vnode *devvp;
188 struct iso_args args;
189 size_t size;
190 int error;
191 mode_t accessmode;
192 struct iso_mnt *imp = 0;
fad57d0e 193 struct nlookupdata nd;
984263bc 194
83760fab 195 if ((mp->mnt_flag & (MNT_ROOTFS|MNT_UPDATE)) == MNT_ROOTFS) {
acde96db 196 return (iso_mountroot(mp));
984263bc
MD
197 }
198 if ((error = copyin(data, (caddr_t)&args, sizeof (struct iso_args))))
199 return (error);
200
201 if ((mp->mnt_flag & MNT_RDONLY) == 0)
202 return (EROFS);
203
204 /*
205 * If updating, check whether changing from read-only to
206 * read/write; if there is no device name, that's all we do.
207 */
208 if (mp->mnt_flag & MNT_UPDATE) {
209 imp = VFSTOISOFS(mp);
210 if (args.fspec == 0)
211 return (vfs_export(mp, &imp->im_export, &args.export));
212 }
213 /*
214 * Not an update, or updating the name: look up the name
215 * and verify that it refers to a sensible block device.
216 */
fad57d0e
MD
217 devvp = NULL;
218 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
219 if (error == 0)
220 error = nlookup(&nd);
221 if (error == 0)
28623bf9 222 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
fad57d0e
MD
223 nlookup_done(&nd);
224 if (error)
984263bc 225 return (error);
984263bc
MD
226
227 if (!vn_isdisk(devvp, &error)) {
228 vrele(devvp);
229 return (error);
230 }
231
232 /*
233 * Verify that user has necessary permissions on the device,
234 * or has superuser abilities
235 */
236 accessmode = VREAD;
ca466bae 237 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
cb66845a 238 error = VOP_EACCESS(devvp, accessmode, cred);
984263bc 239 if (error)
895c1f85 240 error = priv_check_cred(cred, PRIV_ROOT, 0);
984263bc
MD
241 if (error) {
242 vput(devvp);
243 return (error);
244 }
a11aaa81 245 vn_unlock(devvp);
984263bc
MD
246
247 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
acde96db 248 error = iso_mountfs(devvp, mp, &args);
984263bc
MD
249 } else {
250 if (devvp != imp->im_devvp)
251 error = EINVAL; /* needs translation */
252 else
253 vrele(devvp);
254 }
255 if (error) {
256 vrele(devvp);
257 return error;
258 }
259 imp = VFSTOISOFS(mp);
b1ce5639 260 copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
984263bc
MD
261 &size);
262 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
acde96db 263 cd9660_statfs(mp, &mp->mnt_stat, cred);
984263bc
MD
264 return 0;
265}
266
267/*
268 * Common code for mount and mountroot
269 */
270static int
acde96db 271iso_mountfs(struct vnode *devvp, struct mount *mp, struct iso_args *argp)
3fa8192b 272{
460af697 273 struct iso_mnt *isomp = NULL;
984263bc
MD
274 struct buf *bp = NULL;
275 struct buf *pribp = NULL, *supbp = NULL;
b13267a5 276 cdev_t dev;
984263bc
MD
277 int error = EINVAL;
278 int needclose = 0;
279 int high_sierra = 0;
280 int iso_bsize;
281 int iso_blknum;
282 int joliet_level;
283 struct iso_volume_descriptor *vdp = 0;
284 struct iso_primary_descriptor *pri = NULL;
285 struct iso_sierra_primary_descriptor *pri_sierra = NULL;
286 struct iso_supplementary_descriptor *sup = NULL;
287 struct iso_directory_record *rootp;
288 int logical_block_size;
60c47c5e
AP
289 char cs_local[ICONV_CSNMAXLEN];
290 char cs_disk[ICONV_CSNMAXLEN];
984263bc
MD
291
292 if (!(mp->mnt_flag & MNT_RDONLY))
293 return EROFS;
294
295 /*
296 * Disallow multiple mounts of the same device.
297 * Disallow mounting of a device that is currently in use
984263bc
MD
298 * Flush out any old buffers remaining from a previous use.
299 */
300 if ((error = vfs_mountedon(devvp)))
301 return error;
8be7edad 302 if (vcount(devvp) > 0)
984263bc 303 return EBUSY;
87de5057 304 if ((error = vinvalbuf(devvp, V_SAVE, 0, 0)))
984263bc
MD
305 return (error);
306
ca466bae 307 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
87de5057 308 error = VOP_OPEN(devvp, FREAD, FSCRED, NULL);
a11aaa81 309 vn_unlock(devvp);
984263bc
MD
310 if (error)
311 return error;
e4c9c0c8
MD
312 dev = devvp->v_rdev;
313 if (dev->si_iosize_max != 0)
314 mp->mnt_iosize_max = dev->si_iosize_max;
984263bc
MD
315 if (mp->mnt_iosize_max > MAXPHYS)
316 mp->mnt_iosize_max = MAXPHYS;
317
318 needclose = 1;
319
320 /* This is the "logical sector size". The standard says this
321 * should be 2048 or the physical sector size on the device,
322 * whichever is greater. For now, we'll just use a constant.
323 */
324 iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
325
326 joliet_level = 0;
327 for (iso_blknum = 16 + argp->ssector;
328 iso_blknum < 100 + argp->ssector;
329 iso_blknum++) {
54078292 330 if ((error = bread(devvp, (off_t)iso_blknum * iso_bsize,
3b568787 331 iso_bsize, &bp)) != 0)
984263bc
MD
332 goto out;
333
334 vdp = (struct iso_volume_descriptor *)bp->b_data;
335 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
336 if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
337 sizeof vdp->id) != 0) {
338 error = EINVAL;
339 goto out;
340 } else
341 high_sierra = 1;
342 }
343 switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
344 case ISO_VD_PRIMARY:
345 if (pribp == NULL) {
346 pribp = bp;
347 bp = NULL;
348 pri = (struct iso_primary_descriptor *)vdp;
349 pri_sierra =
350 (struct iso_sierra_primary_descriptor *)vdp;
351 }
352 break;
353
354 case ISO_VD_SUPPLEMENTARY:
355 if (supbp == NULL) {
356 supbp = bp;
357 bp = NULL;
358 sup = (struct iso_supplementary_descriptor *)vdp;
359
360 if (!(argp->flags & ISOFSMNT_NOJOLIET)) {
361 if (bcmp(sup->escape, "%/@", 3) == 0)
362 joliet_level = 1;
363 if (bcmp(sup->escape, "%/C", 3) == 0)
364 joliet_level = 2;
365 if (bcmp(sup->escape, "%/E", 3) == 0)
366 joliet_level = 3;
367
368 if ((isonum_711 (sup->flags) & 1) &&
369 (argp->flags & ISOFSMNT_BROKENJOLIET) == 0)
370 joliet_level = 0;
371 }
372 }
373 break;
374
375 case ISO_VD_END:
376 goto vd_end;
377
378 default:
379 break;
380 }
381 if (bp) {
382 brelse(bp);
383 bp = NULL;
384 }
385 }
386 vd_end:
387 if (bp) {
388 brelse(bp);
389 bp = NULL;
390 }
391
392 if (pri == NULL) {
393 error = EINVAL;
394 goto out;
395 }
396
397 logical_block_size =
398 isonum_723 (high_sierra?
399 pri_sierra->logical_block_size:
400 pri->logical_block_size);
401
402 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
403 || (logical_block_size & (logical_block_size - 1)) != 0) {
404 error = EINVAL;
405 goto out;
406 }
407
408 rootp = (struct iso_directory_record *)
409 (high_sierra?
410 pri_sierra->root_directory_record:
411 pri->root_directory_record);
412
efda3bd0 413 isomp = kmalloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK | M_ZERO);
984263bc
MD
414 isomp->logical_block_size = logical_block_size;
415 isomp->volume_space_size =
416 isonum_733 (high_sierra?
417 pri_sierra->volume_space_size:
418 pri->volume_space_size);
419 isomp->joliet_level = 0;
420 /*
421 * Since an ISO9660 multi-session CD can also access previous
422 * sessions, we have to include them into the space consider-
423 * ations. This doesn't yield a very accurate number since
424 * parts of the old sessions might be inaccessible now, but we
425 * can't do much better. This is also important for the NFS
426 * filehandle validation.
427 */
428 isomp->volume_space_size += argp->ssector;
429 bcopy (rootp, isomp->root, sizeof isomp->root);
430 isomp->root_extent = isonum_733 (rootp->extent);
431 isomp->root_size = isonum_733 (rootp->size);
432
433 isomp->im_bmask = logical_block_size - 1;
434 isomp->im_bshift = ffs(logical_block_size) - 1;
435
436 pribp->b_flags |= B_AGE;
437 brelse(pribp);
438 pribp = NULL;
439
440 mp->mnt_data = (qaddr_t)isomp;
441 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
442 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
443 mp->mnt_maxsymlinklen = 0;
444 mp->mnt_flag |= MNT_LOCAL;
445 isomp->im_mountp = mp;
446 isomp->im_dev = dev;
447 isomp->im_devvp = devvp;
448
e4c9c0c8 449 dev->si_mountpoint = mp;
984263bc
MD
450
451 /* Check the Rock Ridge Extention support */
452 if (!(argp->flags & ISOFSMNT_NORRIP)) {
453 if ((error = bread(isomp->im_devvp,
54078292 454 lblktooff(isomp, isomp->root_extent + isonum_711(rootp->ext_attr_length)),
3b568787 455 isomp->logical_block_size, &bp)) != 0)
984263bc
MD
456 goto out;
457
458 rootp = (struct iso_directory_record *)bp->b_data;
459
460 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
461 argp->flags |= ISOFSMNT_NORRIP;
462 } else {
463 argp->flags &= ~ISOFSMNT_GENS;
464 }
465
466 /*
467 * The contents are valid,
468 * but they will get reread as part of another vnode, so...
469 */
470 bp->b_flags |= B_AGE;
471 brelse(bp);
472 bp = NULL;
473 }
474 isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
60c47c5e
AP
475 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET |
476 ISOFSMNT_KICONV);
477 if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
478 bcopy(argp->cs_local, cs_local, sizeof(cs_local));
479 bcopy(argp->cs_disk, cs_disk, sizeof(cs_disk));
480 cd9660_iconv->open(cs_local, cs_disk, &isomp->im_d2l);
481 cd9660_iconv->open(cs_disk, cs_local, &isomp->im_l2d);
482 } else {
483 isomp->im_d2l = NULL;
484 isomp->im_l2d = NULL;
485 }
984263bc
MD
486
487 if (high_sierra) {
488 /* this effectively ignores all the mount flags */
489 log(LOG_INFO, "cd9660: High Sierra Format\n");
490 isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
0961aa92 491 } else {
984263bc
MD
492 switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
493 default:
494 isomp->iso_ftype = ISO_FTYPE_DEFAULT;
495 break;
496 case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
497 isomp->iso_ftype = ISO_FTYPE_9660;
498 break;
499 case 0:
500 log(LOG_INFO, "cd9660: RockRidge Extension\n");
501 isomp->iso_ftype = ISO_FTYPE_RRIP;
502 break;
503 }
0961aa92 504 }
984263bc
MD
505
506 /* Decide whether to use the Joliet descriptor */
507
508 if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
509 log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n", joliet_level);
510 rootp = (struct iso_directory_record *)
511 sup->root_directory_record;
512 bcopy (rootp, isomp->root, sizeof isomp->root);
513 isomp->root_extent = isonum_733 (rootp->extent);
514 isomp->root_size = isonum_733 (rootp->size);
515 isomp->joliet_level = joliet_level;
516 supbp->b_flags |= B_AGE;
517 }
518
519 if (supbp) {
520 brelse(supbp);
521 supbp = NULL;
522 }
523
66a1ddf5
MD
524 vfs_add_vnodeops(mp, &cd9660_vnode_vops, &mp->mnt_vn_norm_ops);
525 vfs_add_vnodeops(mp, &cd9660_spec_vops, &mp->mnt_vn_spec_ops);
526 vfs_add_vnodeops(mp, &cd9660_fifo_vops, &mp->mnt_vn_fifo_ops);
0961aa92 527
984263bc
MD
528 return 0;
529out:
e4c9c0c8 530 dev->si_mountpoint = NULL;
984263bc
MD
531 if (bp)
532 brelse(bp);
533 if (pribp)
534 brelse(pribp);
535 if (supbp)
536 brelse(supbp);
537 if (needclose)
87de5057 538 VOP_CLOSE(devvp, FREAD);
984263bc 539 if (isomp) {
efda3bd0 540 kfree((caddr_t)isomp, M_ISOFSMNT);
984263bc
MD
541 mp->mnt_data = (qaddr_t)0;
542 }
543 return error;
544}
545
546/*
547 * unmount system call
548 */
549static int
acde96db 550cd9660_unmount(struct mount *mp, int mntflags)
984263bc 551{
3a1b0fdc 552 struct iso_mnt *isomp;
984263bc
MD
553 int error, flags = 0;
554
555 if (mntflags & MNT_FORCE)
556 flags |= FORCECLOSE;
557#if 0
558 mntflushbuf(mp, 0);
559 if (mntinvalbuf(mp))
560 return EBUSY;
561#endif
562 if ((error = vflush(mp, 0, flags)))
563 return (error);
564
565 isomp = VFSTOISOFS(mp);
566
60c47c5e
AP
567 if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
568 if (isomp->im_d2l)
569 cd9660_iconv->close(isomp->im_d2l);
570 if (isomp->im_l2d)
571 cd9660_iconv->close(isomp->im_l2d);
572 }
573
e4c9c0c8 574 isomp->im_devvp->v_rdev->si_mountpoint = NULL;
87de5057 575 error = VOP_CLOSE(isomp->im_devvp, FREAD);
984263bc 576 vrele(isomp->im_devvp);
efda3bd0 577 kfree((caddr_t)isomp, M_ISOFSMNT);
984263bc
MD
578 mp->mnt_data = (qaddr_t)0;
579 mp->mnt_flag &= ~MNT_LOCAL;
580 return (error);
581}
582
583/*
584 * Return root of a filesystem
585 */
586static int
3fa8192b 587cd9660_root(struct mount *mp, struct vnode **vpp)
984263bc
MD
588{
589 struct iso_mnt *imp = VFSTOISOFS(mp);
590 struct iso_directory_record *dp =
591 (struct iso_directory_record *)imp->root;
592 ino_t ino = isodirino(dp, imp);
593
594 /*
595 * With RRIP we must use the `.' entry of the root directory.
596 * Simply tell vget, that it's a relocated directory.
597 */
598 return (cd9660_vget_internal(mp, ino, vpp,
599 imp->iso_ftype == ISO_FTYPE_RRIP, dp));
600}
601
602/*
603 * Get file system statistics.
604 */
605int
acde96db 606cd9660_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
984263bc 607{
dadab5e9 608 struct iso_mnt *isomp;
984263bc
MD
609
610 isomp = VFSTOISOFS(mp);
611
612 sbp->f_bsize = isomp->logical_block_size;
613 sbp->f_iosize = sbp->f_bsize; /* XXX */
614 sbp->f_blocks = isomp->volume_space_size;
615 sbp->f_bfree = 0; /* total free blocks */
616 sbp->f_bavail = 0; /* blocks free for non superuser */
617 sbp->f_files = 0; /* total files */
618 sbp->f_ffree = 0; /* free file nodes */
619 if (sbp != &mp->mnt_stat) {
620 sbp->f_type = mp->mnt_vfc->vfc_typenum;
984263bc
MD
621 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
622 }
623 return 0;
624}
625
626/*
627 * File handle to vnode
628 *
629 * Have to be really careful about stale file handles:
630 * - check that the inode number is in range
631 * - call iget() to get the locked inode
632 * - check for an unallocated inode (i_mode == 0)
633 * - check that the generation number matches
634 */
635
636struct ifid {
637 ushort ifid_len;
638 ushort ifid_pad;
639 int ifid_ino;
640 long ifid_start;
641};
642
643/* ARGSUSED */
644int
67863d04
MD
645cd9660_fhtovp(struct mount *mp, struct vnode *rootvp,
646 struct fid *fhp, struct vnode **vpp)
984263bc
MD
647{
648 struct ifid *ifhp = (struct ifid *)fhp;
3a1b0fdc 649 struct iso_node *ip;
984263bc
MD
650 struct vnode *nvp;
651 int error;
652
653#ifdef ISOFS_DBG
086c1d7e 654 kprintf("fhtovp: ino %d, start %ld\n",
984263bc
MD
655 ifhp->ifid_ino, ifhp->ifid_start);
656#endif
657
b9b0a6d0 658 if ((error = VFS_VGET(mp, NULL, ifhp->ifid_ino, &nvp)) != 0) {
984263bc
MD
659 *vpp = NULLVP;
660 return (error);
661 }
662 ip = VTOI(nvp);
663 if (ip->inode.iso_mode == 0) {
664 vput(nvp);
665 *vpp = NULLVP;
666 return (ESTALE);
667 }
668 *vpp = nvp;
669 return (0);
670}
671
672int
3fa8192b
CP
673cd9660_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp,
674 struct ucred **credanonp)
984263bc 675{
3a1b0fdc
RG
676 struct netcred *np;
677 struct iso_mnt *imp;
984263bc
MD
678
679 imp = VFSTOISOFS(mp);
680
681 /*
682 * Get the export permission structure for this <mp, client> tuple.
683 */
684 np = vfs_export_lookup(mp, &imp->im_export, nam);
685 if (np == NULL)
686 return (EACCES);
687
688 *exflagsp = np->netc_exflags;
689 *credanonp = &np->netc_anon;
690 return (0);
691}
692
693int
b9b0a6d0 694cd9660_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
984263bc
MD
695{
696
697 /*
698 * XXXX
699 * It would be nice if we didn't always set the `relocated' flag
700 * and force the extra read, but I don't want to think about fixing
701 * that right now.
702 */
703 return (cd9660_vget_internal(mp, ino, vpp,
704#if 0
705 VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
706#else
707 0,
708#endif
60233e58 709 NULL));
984263bc
MD
710}
711
712int
3fa8192b
CP
713cd9660_vget_internal(struct mount *mp, ino_t ino, struct vnode **vpp,
714 int relocated, struct iso_directory_record *isodir)
984263bc
MD
715{
716 struct iso_mnt *imp;
717 struct iso_node *ip;
718 struct buf *bp;
719 struct vnode *vp;
b13267a5 720 cdev_t dev;
984263bc
MD
721 int error;
722
723 imp = VFSTOISOFS(mp);
724 dev = imp->im_dev;
3446c007 725again:
984263bc
MD
726 if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP)
727 return (0);
728
729 /* Allocate a new vnode/iso_node. */
6ddb7618 730 error = getnewvnode(VT_ISOFS, mp, &vp, 0, 0);
3446c007 731 if (error) {
984263bc
MD
732 *vpp = NULLVP;
733 return (error);
734 }
735 MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
e7b4468c 736 M_WAITOK | M_ZERO);
984263bc
MD
737 ip->i_vnode = vp;
738 ip->i_dev = dev;
739 ip->i_number = ino;
740
741 /*
3446c007
MD
742 * Insert it into the inode hash table and check for a collision.
743 * If a collision occurs, throw away the vnode and try again.
984263bc 744 */
3446c007 745 if (cd9660_ihashins(ip) != 0) {
086c1d7e 746 kprintf("debug: cd9660 ihashins collision, retrying\n");
5fd012e0 747 vx_put(vp);
efda3bd0 748 kfree(ip, M_ISOFSNODE);
3446c007
MD
749 goto again;
750 }
751 vp->v_data = ip;
984263bc 752
899483f5 753 if (isodir == NULL) {
984263bc
MD
754 int lbn, off;
755
756 lbn = lblkno(imp, ino);
757 if (lbn >= imp->volume_space_size) {
5fd012e0 758 vx_put(vp);
086c1d7e 759 kprintf("fhtovp: lbn exceed volume space %d\n", lbn);
984263bc
MD
760 return (ESTALE);
761 }
762
763 off = blkoff(imp, ino);
764 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
5fd012e0 765 vx_put(vp);
086c1d7e 766 kprintf("fhtovp: crosses block boundary %d\n",
984263bc
MD
767 off + ISO_DIRECTORY_RECORD_SIZE);
768 return (ESTALE);
769 }
770
771 error = bread(imp->im_devvp,
54078292 772 lblktooff(imp, lbn),
3b568787 773 imp->logical_block_size, &bp);
984263bc 774 if (error) {
5fd012e0 775 vx_put(vp);
984263bc 776 brelse(bp);
086c1d7e 777 kprintf("fhtovp: bread error %d\n",error);
984263bc
MD
778 return (error);
779 }
780 isodir = (struct iso_directory_record *)(bp->b_data + off);
781
782 if (off + isonum_711(isodir->length) >
783 imp->logical_block_size) {
5fd012e0 784 vx_put(vp);
899483f5 785 if (bp != NULL)
984263bc 786 brelse(bp);
086c1d7e 787 kprintf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
984263bc
MD
788 off +isonum_711(isodir->length), off,
789 isonum_711(isodir->length));
790 return (ESTALE);
791 }
792
793#if 0
794 if (isonum_733(isodir->extent) +
795 isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
899483f5 796 if (bp != NULL)
984263bc 797 brelse(bp);
086c1d7e 798 kprintf("fhtovp: file start miss %d vs %d\n",
984263bc
MD
799 isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
800 ifhp->ifid_start);
801 return (ESTALE);
802 }
803#endif
804 } else
805 bp = 0;
806
807 ip->i_mnt = imp;
808 ip->i_devvp = imp->im_devvp;
597aea93 809 vref(ip->i_devvp);
984263bc
MD
810
811 if (relocated) {
812 /*
813 * On relocated directories we must
814 * read the `.' entry out of a dir.
815 */
816 ip->iso_start = ino >> imp->im_bshift;
899483f5 817 if (bp != NULL)
984263bc 818 brelse(bp);
899483f5 819 if ((error = cd9660_devblkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
5fd012e0 820 vx_put(vp);
984263bc
MD
821 return (error);
822 }
823 isodir = (struct iso_directory_record *)bp->b_data;
824 }
825
826 ip->iso_extent = isonum_733(isodir->extent);
827 ip->i_size = isonum_733(isodir->size);
828 ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
57f7b636 829
984263bc
MD
830 /*
831 * Setup time stamp, attribute
832 */
833 vp->v_type = VNON;
834 switch (imp->iso_ftype) {
835 default: /* ISO_FTYPE_9660 */
836 {
837 struct buf *bp2;
838 int off;
839 if ((imp->im_flags & ISOFSMNT_EXTATT)
840 && (off = isonum_711(isodir->ext_attr_length)))
899483f5 841 cd9660_devblkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
984263bc
MD
842 &bp2);
843 else
844 bp2 = NULL;
845 cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
846 cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
847 if (bp2)
848 brelse(bp2);
849 break;
850 }
851 case ISO_FTYPE_RRIP:
852 cd9660_rrip_analyze(isodir, ip, imp);
853 break;
854 }
855
899483f5 856 if (bp != NULL)
984263bc
MD
857 brelse(bp);
858
859 /*
860 * Initialize the associated vnode
861 */
57f7b636
MD
862 vp->v_type = IFTOVT(ip->inode.iso_mode);
863
864 switch (vp->v_type) {
984263bc 865 case VFIFO:
6ddb7618 866 vp->v_ops = &mp->mnt_vn_fifo_ops;
984263bc
MD
867 break;
868 case VCHR:
869 case VBLK:
6ddb7618 870 vp->v_ops = &mp->mnt_vn_spec_ops;
0e9b9130
MD
871 addaliasu(vp, umajor(ip->inode.iso_rdev),
872 uminor(ip->inode.iso_rdev));
984263bc 873 break;
1c843a13
MD
874 case VREG:
875 case VDIR:
876 vinitvmio(vp, ip->i_size);
877 break;
984263bc
MD
878 default:
879 break;
880 }
881
882 if (ip->iso_extent == imp->root_extent)
2247fe02 883 vsetflags(vp, VROOT);
984263bc
MD
884
885 /*
5fd012e0 886 * Return the locked and refd vp
984263bc 887 */
984263bc
MD
888 *vpp = vp;
889 return (0);
890}
891
892/*
893 * Vnode pointer to File handle
894 */
895/* ARGSUSED */
896int
3fa8192b 897cd9660_vptofh(struct vnode *vp, struct fid *fhp)
984263bc 898{
3a1b0fdc
RG
899 struct iso_node *ip = VTOI(vp);
900 struct ifid *ifhp;
984263bc
MD
901
902 ifhp = (struct ifid *)fhp;
903 ifhp->ifid_len = sizeof(struct ifid);
904
905 ifhp->ifid_ino = ip->i_number;
906 ifhp->ifid_start = ip->iso_start;
907
908#ifdef ISOFS_DBG
086c1d7e 909 kprintf("vptofh: ino %d, start %ld\n",
984263bc
MD
910 ifhp->ifid_ino,ifhp->ifid_start);
911#endif
912 return 0;
913}