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