ops_cachefs.c revision 38494
1/*
2 * Copyright (c) 1997-1998 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *      This product includes software developed by the University of
22 *      California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 *      %W% (Berkeley) %G%
40 *
41 * $Id: ops_cachefs.c,v 5.2.2.3 1992/08/02 10:42:21 jsp Exp $
42 *
43 */
44
45/*
46 * Caching filesystem (Solaris 2.x)
47 */
48
49#ifdef HAVE_CONFIG_H
50# include <config.h>
51#endif /* HAVE_CONFIG_H */
52#include <am_defs.h>
53#include <amd.h>
54
55/* forward declarations */
56static char *cachefs_match(am_opts *fo);
57static int cachefs_init(mntfs *mf);
58static int cachefs_fmount(mntfs *mf);
59static int cachefs_fumount(mntfs *mf);
60
61
62/*
63 * Ops structure
64 */
65am_ops cachefs_ops =
66{
67  "cachefs",
68  cachefs_match,
69  cachefs_init,
70  amfs_auto_fmount,
71  cachefs_fmount,
72  amfs_auto_fumount,
73  cachefs_fumount,
74  amfs_error_lookuppn,
75  amfs_error_readdir,
76  0,				/* cachefs_readlink */
77  0,				/* post-mount actions */
78  0,				/* post-umount actions */
79  find_amfs_auto_srvr,
80  FS_MKMNT | FS_NOTIMEOUT | FS_UBACKGROUND | FS_AMQINFO
81};
82
83
84/*
85 * Check that f/s has all needed fields.
86 * Returns: matched string if found, NULL otherwise.
87 */
88static char *
89cachefs_match(am_opts *fo)
90{
91  /* sanity check */
92  if (!fo->opt_rfs || !fo->opt_fs || !fo->opt_cachedir) {
93    plog(XLOG_USER, "cachefs: must specify cachedir, rfs, and fs");
94    return NULL;
95  }
96
97#ifdef DEBUG
98  dlog("CACHEFS: using cache directory \"%s\"", fo->opt_cachedir);
99#endif /* DEBUG */
100
101  /* determine magic cookie to put in mtab */
102  return strdup(fo->opt_cachedir);
103}
104
105
106/*
107 * Initialize.
108 * Returns: 0 if OK, non-zero (errno) if failed.
109 */
110static int
111cachefs_init(mntfs *mf)
112{
113  /*
114   * Save cache directory name
115   */
116  if (mf->mf_refc == 1) {
117    mf->mf_private = (voidp) strdup(mf->mf_fo->opt_cachedir);
118    mf->mf_prfree = (void (*)(voidp)) free;
119  }
120
121  return 0;
122}
123
124
125/*
126 * mntpt is the mount point ($fs) [XXX: was 'dir']
127 * backdir is the mounted pathname ($rfs) [XXX: was 'fs_name']
128 * cachedir is the cache directory ($cachedir)
129 */
130static int
131mount_cachefs(char *mntpt, char *backdir, char *cachedir, char *opts)
132{
133  cachefs_args_t ca;
134  mntent_t mnt;
135  int flags;
136  char *cp;
137  MTYPE_TYPE type = MOUNT_TYPE_CACHEFS;	/* F/S mount type */
138
139  memset((voidp) &ca, 0, sizeof(ca)); /* Paranoid */
140
141  /*
142   * Fill in the mount structure
143   */
144  memset((voidp) &mnt, 0, sizeof(mnt));
145  mnt.mnt_dir = mntpt;
146  mnt.mnt_fsname = backdir;
147  mnt.mnt_type = MNTTAB_TYPE_CACHEFS;
148  mnt.mnt_opts = opts;
149
150  flags = compute_mount_flags(&mnt);
151
152  /* Fill in cachefs mount arguments */
153
154  /*
155   * XXX: Caveats
156   * (1) cache directory is NOT checked for sanity beforehand, nor is it
157   * purged.  Maybe it should be purged first?
158   * (2) cache directory is NOT locked.  Should we?
159   */
160
161  /* mount flags */
162  ca.cfs_options.opt_flags = CFS_WRITE_AROUND | CFS_ACCESS_BACKFS;
163  /* cache population size */
164  ca.cfs_options.opt_popsize = DEF_POP_SIZE; /* default: 64K */
165  /* filegrp size */
166  ca.cfs_options.opt_fgsize = DEF_FILEGRP_SIZE; /* default: 256 */
167
168  /* CFS ID for file system (must be unique) */
169  ca.cfs_fsid = cachedir;
170
171  /* CFS fscdir name */
172  memset(ca.cfs_cacheid, 0, sizeof(ca.cfs_cacheid));
173  /* append cacheid and mountpoint */
174  sprintf(ca.cfs_cacheid, "%s:%s", ca.cfs_fsid, mntpt);
175  /* convert '/' to '_' (Solaris does that...) */
176  cp = ca.cfs_cacheid;
177  while ((cp = strpbrk(cp, "/")) != NULL)
178    *cp = '_';
179
180  /* path for this cache dir */
181  ca.cfs_cachedir = cachedir;
182
183  /* back filesystem dir */
184  ca.cfs_backfs = backdir;
185
186  /* same as nfs values (XXX: need to handle these options) */
187  ca.cfs_acregmin = 0;
188  ca.cfs_acregmax = 0;
189  ca.cfs_acdirmin = 0;
190  ca.cfs_acdirmax = 0;
191
192  /*
193   * Call generic mount routine
194   */
195  return mount_fs(&mnt, flags, (caddr_t) &ca, 0, type, 0, NULL, mnttab_file_name);
196}
197
198
199static int
200cachefs_fmount(mntfs *mf)
201{
202  int error;
203
204  error = mount_cachefs(mf->mf_mount,
205			mf->mf_fo->opt_rfs,
206			mf->mf_fo->opt_cachedir,
207			mf->mf_mopts);
208  if (error) {
209    errno = error;
210    /* according to Solaris, if errno==ESRCH, "options to not match" */
211    if (error == ESRCH)
212      plog(XLOG_ERROR, "mount_cachefs: options to no match: %m");
213    else
214      plog(XLOG_ERROR, "mount_cachefs: %m");
215    return error;
216  }
217
218  return 0;
219}
220
221
222static int
223cachefs_fumount(mntfs *mf)
224{
225  int error;
226
227  error = UMOUNT_FS(mf->mf_mount, mnttab_file_name);
228
229  /*
230   * In the case of cachefs, we must fsck the cache directory.  Otherwise,
231   * it will remain inconsistent, and the next cachefs mount will fail
232   * with the error "no space left on device" (ENOSPC).
233   *
234   * XXX: this is hacky! use fork/exec/wait instead...
235   */
236  if (!error) {
237    char *cachedir = NULL;
238    char cmd[128];
239
240    cachedir = (char *) mf->mf_private;
241    plog(XLOG_INFO, "running fsck on cache directory \"%s\"", cachedir);
242    sprintf(cmd, "fsck -F cachefs %s", cachedir);
243    system(cmd);
244  }
245
246  return error;
247}
248