]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/amd/amd/ops_cachefs.c
MFC r308493, r308619: Update amd from am-utils 6.1.5 to 6.2.
[FreeBSD/stable/10.git] / contrib / amd / amd / ops_cachefs.c
1 /*
2  * Copyright (c) 1997-2014 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. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *
36  * File: am-utils/amd/ops_cachefs.c
37  *
38  */
39
40 /*
41  * Caching filesystem (Solaris 2.x)
42  */
43
44 #ifdef HAVE_CONFIG_H
45 # include <config.h>
46 #endif /* HAVE_CONFIG_H */
47 #include <am_defs.h>
48 #include <amd.h>
49
50 /* forward declarations */
51 static char *cachefs_match(am_opts *fo);
52 static int cachefs_init(mntfs *mf);
53 static int cachefs_mount(am_node *am, mntfs *mf);
54 static int cachefs_umount(am_node *am, mntfs *mf);
55
56
57 /*
58  * Ops structure
59  */
60 am_ops cachefs_ops =
61 {
62   "cachefs",
63   cachefs_match,
64   cachefs_init,
65   cachefs_mount,
66   cachefs_umount,
67   amfs_error_lookup_child,
68   amfs_error_mount_child,
69   amfs_error_readdir,
70   0,                            /* cachefs_readlink */
71   0,                            /* cachefs_mounted */
72   0,                            /* cachefs_umounted */
73   amfs_generic_find_srvr,
74   0,                            /* cachefs_get_wchan */
75   FS_MKMNT | FS_NOTIMEOUT | FS_UBACKGROUND | FS_AMQINFO, /* nfs_fs_flags */
76 #ifdef HAVE_FS_AUTOFS
77   AUTOFS_CACHEFS_FS_FLAGS,
78 #endif /* HAVE_FS_AUTOFS */
79 };
80
81
82 /*
83  * Check that f/s has all needed fields.
84  * Returns: matched string if found, NULL otherwise.
85  */
86 static char *
87 cachefs_match(am_opts *fo)
88 {
89   /* sanity check */
90   if (!fo->opt_rfs || !fo->opt_fs || !fo->opt_cachedir) {
91     plog(XLOG_USER, "cachefs: must specify cachedir, rfs, and fs");
92     return NULL;
93   }
94
95   dlog("CACHEFS: using cache directory \"%s\"", fo->opt_cachedir);
96
97   /* determine magic cookie to put in mtab */
98   return xstrdup(fo->opt_cachedir);
99 }
100
101
102 /*
103  * Initialize.
104  * Returns: 0 if OK, non-zero (errno) if failed.
105  */
106 static int
107 cachefs_init(mntfs *mf)
108 {
109   /*
110    * Save cache directory name
111    */
112   if (!mf->mf_private) {
113     mf->mf_private = (voidp) xstrdup(mf->mf_fo->opt_cachedir);
114     mf->mf_prfree = (void (*)(voidp)) free;
115   }
116
117   return 0;
118 }
119
120
121 /*
122  * mntpt is the mount point ($fs) [XXX: was 'dir']
123  * backdir is the mounted pathname ($rfs) [XXX: was 'fs_name']
124  * cachedir is the cache directory ($cachedir)
125  */
126 static int
127 mount_cachefs(char *mntdir, char *backdir, char *cachedir,
128               char *opts, int on_autofs)
129 {
130   cachefs_args_t ca;
131   mntent_t mnt;
132   int flags;
133   char *cp;
134   MTYPE_TYPE type = MOUNT_TYPE_CACHEFS; /* F/S mount type */
135
136   memset((voidp) &ca, 0, sizeof(ca)); /* Paranoid */
137
138   /*
139    * Fill in the mount structure
140    */
141   memset((voidp) &mnt, 0, sizeof(mnt));
142   mnt.mnt_dir = mntdir;
143   mnt.mnt_fsname = backdir;
144   mnt.mnt_type = MNTTAB_TYPE_CACHEFS;
145   mnt.mnt_opts = opts;
146
147   flags = compute_mount_flags(&mnt);
148 #ifdef HAVE_FS_AUTOFS
149   if (on_autofs)
150     flags |= autofs_compute_mount_flags(&mnt);
151 #endif /* HAVE_FS_AUTOFS */
152
153   /* Fill in cachefs mount arguments */
154
155   /*
156    * XXX: Caveats
157    * (1) cache directory is NOT checked for sanity beforehand, nor is it
158    * purged.  Maybe it should be purged first?
159    * (2) cache directory is NOT locked.  Should we?
160    */
161
162   /* mount flags */
163   ca.cfs_options.opt_flags = CFS_WRITE_AROUND | CFS_ACCESS_BACKFS;
164   /* cache population size */
165   ca.cfs_options.opt_popsize = DEF_POP_SIZE; /* default: 64K */
166   /* filegrp size */
167   ca.cfs_options.opt_fgsize = DEF_FILEGRP_SIZE; /* default: 256 */
168
169   /* CFS ID for file system (must be unique) */
170   ca.cfs_fsid = cachedir;
171
172   /* CFS fscdir name */
173   memset(ca.cfs_cacheid, 0, sizeof(ca.cfs_cacheid));
174   /*
175    * Append cacheid and mountpoint.
176    * sizeof(cfs_cacheid) should be C_MAX_MOUNT_FSCDIRNAME as per
177    * <sys/fs/cachefs_fs.h> (checked on Solaris 8).
178    */
179   xsnprintf(ca.cfs_cacheid, sizeof(ca.cfs_cacheid),
180             "%s:%s", ca.cfs_fsid, mntdir);
181   /* convert '/' to '_' (Solaris does that...) */
182   cp = ca.cfs_cacheid;
183   while ((cp = strpbrk(cp, "/")) != NULL)
184     *cp = '_';
185
186   /* path for this cache dir */
187   ca.cfs_cachedir = cachedir;
188
189   /* back filesystem dir */
190   ca.cfs_backfs = backdir;
191
192   /* same as nfs values (XXX: need to handle these options) */
193   ca.cfs_acregmin = 0;
194   ca.cfs_acregmax = 0;
195   ca.cfs_acdirmin = 0;
196   ca.cfs_acdirmax = 0;
197
198   /*
199    * Call generic mount routine
200    */
201   return mount_fs(&mnt, flags, (caddr_t) &ca, 0, type, 0, NULL, mnttab_file_name, on_autofs);
202 }
203
204
205 static int
206 cachefs_mount(am_node *am, mntfs *mf)
207 {
208   int on_autofs = mf->mf_flags & MFF_ON_AUTOFS;
209   int error;
210
211   error = mount_cachefs(mf->mf_mount,
212                         mf->mf_fo->opt_rfs,
213                         mf->mf_fo->opt_cachedir,
214                         mf->mf_mopts,
215                         on_autofs);
216   if (error) {
217     errno = error;
218     /* according to Solaris, if errno==ESRCH, "options to not match" */
219     if (error == ESRCH)
220       plog(XLOG_ERROR, "mount_cachefs: options to no match: %m");
221     else
222       plog(XLOG_ERROR, "mount_cachefs: %m");
223     return error;
224   }
225
226   return 0;
227 }
228
229
230 static int
231 cachefs_umount(am_node *am, mntfs *mf)
232 {
233   int unmount_flags = (mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0;
234   int error;
235
236   error = UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags);
237
238   /*
239    * In the case of cachefs, we must fsck the cache directory.  Otherwise,
240    * it will remain inconsistent, and the next cachefs mount will fail
241    * with the error "no space left on device" (ENOSPC).
242    *
243    * XXX: this is hacky! use fork/exec/wait instead...
244    */
245   if (!error) {
246     char *cachedir = NULL;
247     char cmd[128];
248
249     cachedir = (char *) mf->mf_private;
250     plog(XLOG_INFO, "running fsck on cache directory \"%s\"", cachedir);
251     xsnprintf(cmd, sizeof(cmd), "fsck -F cachefs %s", cachedir);
252     system(cmd);
253   }
254
255   return error;
256 }