1/*	$NetBSD: ops_cdfs.c,v 1.3 2009/03/20 20:30:52 christos Exp $	*/
2
3/*
4 * Copyright (c) 1997-2009 Erez Zadok
5 * Copyright (c) 1990 Jan-Simon Pendry
6 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
7 * Copyright (c) 1990 The Regents of the University of California.
8 * All rights reserved.
9 *
10 * This code is derived from software contributed to Berkeley by
11 * Jan-Simon Pendry at Imperial College, London.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 *    must display the following acknowledgment:
23 *      This product includes software developed by the University of
24 *      California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 *    may be used to endorse or promote products derived from this software
27 *    without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 *
42 * File: am-utils/amd/ops_cdfs.c
43 *
44 */
45
46/*
47 * High Sierra (CD-ROM) file system
48 */
49
50#ifdef HAVE_CONFIG_H
51# include <config.h>
52#endif /* HAVE_CONFIG_H */
53#include <am_defs.h>
54#include <amd.h>
55
56/* forward declarations */
57static char *cdfs_match(am_opts *fo);
58static int cdfs_mount(am_node *am, mntfs *mf);
59static int cdfs_umount(am_node *am, mntfs *mf);
60
61/*
62 * Ops structure
63 */
64am_ops cdfs_ops =
65{
66  "cdfs",
67  cdfs_match,
68  0,				/* cdfs_init */
69  cdfs_mount,
70  cdfs_umount,
71  amfs_error_lookup_child,
72  amfs_error_mount_child,
73  amfs_error_readdir,
74  0,				/* cdfs_readlink */
75  0,				/* cdfs_mounted */
76  0,				/* cdfs_umounted */
77  amfs_generic_find_srvr,
78  0,				/* cdfs_get_wchan */
79  FS_MKMNT | FS_UBACKGROUND | FS_AMQINFO,	/* nfs_fs_flags */
80#ifdef HAVE_FS_AUTOFS
81  AUTOFS_CDFS_FS_FLAGS,
82#endif /* HAVE_FS_AUTOFS */
83};
84
85
86/*
87 * CDFS needs remote filesystem.
88 */
89static char *
90cdfs_match(am_opts *fo)
91{
92  if (!fo->opt_dev) {
93    plog(XLOG_USER, "cdfs: no source device specified");
94    return 0;
95  }
96  dlog("CDFS: mounting device \"%s\" on \"%s\"",
97       fo->opt_dev, fo->opt_fs);
98
99  /*
100   * Determine magic cookie to put in mtab
101   */
102  return strdup(fo->opt_dev);
103}
104
105
106static int
107mount_cdfs(char *mntdir, char *fs_name, char *opts, int on_autofs)
108{
109  cdfs_args_t cdfs_args;
110  mntent_t mnt;
111  int genflags, cdfs_flags, retval;
112
113  /*
114   * Figure out the name of the file system type.
115   */
116  MTYPE_TYPE type = MOUNT_TYPE_CDFS;
117
118  memset((voidp) &cdfs_args, 0, sizeof(cdfs_args)); /* Paranoid */
119  cdfs_flags = 0;
120
121  /*
122   * Fill in the mount structure
123   */
124  memset((voidp) &mnt, 0, sizeof(mnt));
125  mnt.mnt_dir = mntdir;
126  mnt.mnt_fsname = fs_name;
127  mnt.mnt_type = MNTTAB_TYPE_CDFS;
128  mnt.mnt_opts = opts;
129
130#if defined(MNT2_CDFS_OPT_DEFPERM) && defined(MNTTAB_OPT_DEFPERM)
131  if (amu_hasmntopt(&mnt, MNTTAB_OPT_DEFPERM))
132# ifdef MNT2_CDFS_OPT_DEFPERM
133    cdfs_flags |= MNT2_CDFS_OPT_DEFPERM;
134# else /* not MNT2_CDFS_OPT_DEFPERM */
135    cdfs_flags &= ~MNT2_CDFS_OPT_NODEFPERM;
136# endif /* not MNT2_CDFS_OPT_DEFPERM */
137#endif /* defined(MNT2_CDFS_OPT_DEFPERM) && defined(MNTTAB_OPT_DEFPERM) */
138
139#if defined(MNT2_CDFS_OPT_NODEFPERM) && defined(MNTTAB_OPT_NODEFPERM)
140  if (amu_hasmntopt(&mnt, MNTTAB_OPT_NODEFPERM))
141    cdfs_flags |= MNT2_CDFS_OPT_NODEFPERM;
142#endif /* MNTTAB_OPT_NODEFPERM */
143
144#if defined(MNT2_CDFS_OPT_NOVERSION) && defined(MNTTAB_OPT_NOVERSION)
145  if (amu_hasmntopt(&mnt, MNTTAB_OPT_NOVERSION))
146    cdfs_flags |= MNT2_CDFS_OPT_NOVERSION;
147#endif /* defined(MNT2_CDFS_OPT_NOVERSION) && defined(MNTTAB_OPT_NOVERSION) */
148
149#if defined(MNT2_CDFS_OPT_RRIP) && defined(MNTTAB_OPT_RRIP)
150  if (amu_hasmntopt(&mnt, MNTTAB_OPT_RRIP))
151    cdfs_flags |= MNT2_CDFS_OPT_RRIP;
152#endif /* defined(MNT2_CDFS_OPT_RRIP) && defined(MNTTAB_OPT_RRIP) */
153
154#if defined(MNT2_CDFS_OPT_NORRIP) && defined(MNTTAB_OPT_NORRIP)
155  if (amu_hasmntopt(&mnt, MNTTAB_OPT_NORRIP))
156    cdfs_flags |= MNT2_CDFS_OPT_NORRIP;
157#endif /* defined(MNT2_CDFS_OPT_NORRIP) && defined(MNTTAB_OPT_NORRIP) */
158
159#if defined(MNT2_CDFS_OPT_GENS) && defined(MNTTAB_OPT_GENS)
160  if (amu_hasmntopt(&mnt, MNTTAB_OPT_GENS))
161    cdfs_flags |= MNT2_CDFS_OPT_GENS;
162#endif /* defined(MNT2_CDFS_OPT_GENS) && defined(MNTTAB_OPT_GENS) */
163
164#if defined(MNT2_CDFS_OPT_EXTATT) && defined(MNTTAB_OPT_EXTATT)
165  if (amu_hasmntopt(&mnt, MNTTAB_OPT_EXTATT))
166    cdfs_flags |= MNT2_CDFS_OPT_EXTATT;
167#endif /* defined(MNT2_CDFS_OPT_EXTATT) && defined(MNTTAB_OPT_EXTATT) */
168
169#if defined(MNT2_CDFS_OPT_NOCASETRANS) && defined(MNTTAB_OPT_NOCASETRANS)
170  if (amu_hasmntopt(&mnt, MNTTAB_OPT_NOCASETRANS))
171    cdfs_flags |= MNT2_CDFS_OPT_NOCASETRANS;
172#endif /* defined(MNT2_CDFS_OPT_NOCASETRANS) && defined(MNTTAB_OPT_NOCASETRANS) */
173
174#if defined(MNT2_CDFS_OPT_NOJOLIET) && defined(MNTTAB_OPT_NOJOLIET)
175  if (amu_hasmntopt(&mnt, MNTTAB_OPT_NOJOLIET))
176    cdfs_flags |= MNT2_CDFS_OPT_NOJOLIET;
177#endif /* defined(MNT2_CDFS_OPT_NOJOLIET) && defined(MNTTAB_OPT_NOJOLIET) */
178
179#if defined(MNT2_CDFS_OPT_RRCASEINS) && defined(MNTTAB_OPT_RRCASEINS)
180  if (amu_hasmntopt(&mnt, MNTTAB_OPT_RRCASEINS))
181    cdfs_flags |= MNT2_CDFS_OPT_RRCASEINS;
182#endif /* defined(MNT2_CDFS_OPT_RRCASEINS) && defined(MNTTAB_OPT_RRCASEINS) */
183
184  genflags = compute_mount_flags(&mnt);
185#ifdef HAVE_FS_AUTOFS
186  if (on_autofs)
187    genflags |= autofs_compute_mount_flags(&mnt);
188#endif /* HAVE_FS_AUTOFS */
189
190#ifdef HAVE_CDFS_ARGS_T_FLAGS
191  cdfs_args.flags = cdfs_flags;
192#endif /* HAVE_CDFS_ARGS_T_FLAGS */
193
194#ifdef HAVE_CDFS_ARGS_T_ISO_FLAGS
195  cdfs_args.iso_flags = genflags | cdfs_flags;
196#endif /* HAVE_CDFS_ARGS_T_ISO_FLAGS */
197
198#ifdef HAVE_CDFS_ARGS_T_ISO_PGTHRESH
199  cdfs_args.iso_pgthresh = hasmntval(&mnt, MNTTAB_OPT_PGTHRESH);
200#endif /* HAVE_CDFS_ARGS_T_ISO_PGTHRESH */
201
202#ifdef HAVE_CDFS_ARGS_T_NORRIP
203  /* XXX: need to provide norrip mount opt */
204  cdfs_args.norrip = 0;		/* use Rock-Ridge Protocol extensions */
205#endif /* HAVE_CDFS_ARGS_T_NORRIP */
206
207#ifdef HAVE_CDFS_ARGS_T_SSECTOR
208  /* XXX: need to provide ssector mount option */
209  cdfs_args.ssector = 0;	/* use 1st session on disk */
210#endif /* HAVE_CDFS_ARGS_T_SSECTOR */
211
212#ifdef HAVE_CDFS_ARGS_T_FSPEC
213  cdfs_args.fspec = fs_name;
214#endif /* HAVE_CDFS_ARGS_T_FSPEC */
215
216  /*
217   * Call generic mount routine
218   */
219  retval = mount_fs(&mnt, genflags, (caddr_t) &cdfs_args, 0, type, 0, NULL, mnttab_file_name, on_autofs);
220
221  return retval;
222}
223
224
225static int
226cdfs_mount(am_node *am, mntfs *mf)
227{
228  int on_autofs = mf->mf_flags & MFF_ON_AUTOFS;
229  int error;
230
231  error = mount_cdfs(mf->mf_mount, mf->mf_info, mf->mf_mopts, on_autofs);
232  if (error) {
233    errno = error;
234    plog(XLOG_ERROR, "mount_cdfs: %m");
235    return error;
236  }
237  return 0;
238}
239
240
241static int
242cdfs_umount(am_node *am, mntfs *mf)
243{
244  int unmount_flags = (mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0;
245
246  return UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags);
247}
248