ops_cachefs.c revision 174294
1/* 2 * Copyright (c) 1997-2006 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 acknowledgment: 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 * 40 * File: am-utils/amd/ops_cachefs.c 41 * 42 */ 43 44/* 45 * Caching filesystem (Solaris 2.x) 46 */ 47 48#ifdef HAVE_CONFIG_H 49# include <config.h> 50#endif /* HAVE_CONFIG_H */ 51#include <am_defs.h> 52#include <amd.h> 53 54/* forward declarations */ 55static char *cachefs_match(am_opts *fo); 56static int cachefs_init(mntfs *mf); 57static int cachefs_mount(am_node *am, mntfs *mf); 58static int cachefs_umount(am_node *am, mntfs *mf); 59 60 61/* 62 * Ops structure 63 */ 64am_ops cachefs_ops = 65{ 66 "cachefs", 67 cachefs_match, 68 cachefs_init, 69 cachefs_mount, 70 cachefs_umount, 71 amfs_error_lookup_child, 72 amfs_error_mount_child, 73 amfs_error_readdir, 74 0, /* cachefs_readlink */ 75 0, /* cachefs_mounted */ 76 0, /* cachefs_umounted */ 77 amfs_generic_find_srvr, 78 0, /* cachefs_get_wchan */ 79 FS_MKMNT | FS_NOTIMEOUT | FS_UBACKGROUND | FS_AMQINFO, /* nfs_fs_flags */ 80#ifdef HAVE_FS_AUTOFS 81 AUTOFS_CACHEFS_FS_FLAGS, 82#endif /* HAVE_FS_AUTOFS */ 83}; 84 85 86/* 87 * Check that f/s has all needed fields. 88 * Returns: matched string if found, NULL otherwise. 89 */ 90static char * 91cachefs_match(am_opts *fo) 92{ 93 /* sanity check */ 94 if (!fo->opt_rfs || !fo->opt_fs || !fo->opt_cachedir) { 95 plog(XLOG_USER, "cachefs: must specify cachedir, rfs, and fs"); 96 return NULL; 97 } 98 99 dlog("CACHEFS: using cache directory \"%s\"", fo->opt_cachedir); 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_private) { 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 *mntdir, char *backdir, char *cachedir, 132 char *opts, int on_autofs) 133{ 134 cachefs_args_t ca; 135 mntent_t mnt; 136 int flags; 137 char *cp; 138 MTYPE_TYPE type = MOUNT_TYPE_CACHEFS; /* F/S mount type */ 139 140 memset((voidp) &ca, 0, sizeof(ca)); /* Paranoid */ 141 142 /* 143 * Fill in the mount structure 144 */ 145 memset((voidp) &mnt, 0, sizeof(mnt)); 146 mnt.mnt_dir = mntdir; 147 mnt.mnt_fsname = backdir; 148 mnt.mnt_type = MNTTAB_TYPE_CACHEFS; 149 mnt.mnt_opts = opts; 150 151 flags = compute_mount_flags(&mnt); 152#ifdef HAVE_FS_AUTOFS 153 if (on_autofs) 154 flags |= autofs_compute_mount_flags(&mnt); 155#endif /* HAVE_FS_AUTOFS */ 156 157 /* Fill in cachefs mount arguments */ 158 159 /* 160 * XXX: Caveats 161 * (1) cache directory is NOT checked for sanity beforehand, nor is it 162 * purged. Maybe it should be purged first? 163 * (2) cache directory is NOT locked. Should we? 164 */ 165 166 /* mount flags */ 167 ca.cfs_options.opt_flags = CFS_WRITE_AROUND | CFS_ACCESS_BACKFS; 168 /* cache population size */ 169 ca.cfs_options.opt_popsize = DEF_POP_SIZE; /* default: 64K */ 170 /* filegrp size */ 171 ca.cfs_options.opt_fgsize = DEF_FILEGRP_SIZE; /* default: 256 */ 172 173 /* CFS ID for file system (must be unique) */ 174 ca.cfs_fsid = cachedir; 175 176 /* CFS fscdir name */ 177 memset(ca.cfs_cacheid, 0, sizeof(ca.cfs_cacheid)); 178 /* 179 * Append cacheid and mountpoint. 180 * sizeof(cfs_cacheid) should be C_MAX_MOUNT_FSCDIRNAME as per 181 * <sys/fs/cachefs_fs.h> (checked on Solaris 8). 182 */ 183 xsnprintf(ca.cfs_cacheid, sizeof(ca.cfs_cacheid), 184 "%s:%s", ca.cfs_fsid, mntdir); 185 /* convert '/' to '_' (Solaris does that...) */ 186 cp = ca.cfs_cacheid; 187 while ((cp = strpbrk(cp, "/")) != NULL) 188 *cp = '_'; 189 190 /* path for this cache dir */ 191 ca.cfs_cachedir = cachedir; 192 193 /* back filesystem dir */ 194 ca.cfs_backfs = backdir; 195 196 /* same as nfs values (XXX: need to handle these options) */ 197 ca.cfs_acregmin = 0; 198 ca.cfs_acregmax = 0; 199 ca.cfs_acdirmin = 0; 200 ca.cfs_acdirmax = 0; 201 202 /* 203 * Call generic mount routine 204 */ 205 return mount_fs(&mnt, flags, (caddr_t) &ca, 0, type, 0, NULL, mnttab_file_name, on_autofs); 206} 207 208 209static int 210cachefs_mount(am_node *am, mntfs *mf) 211{ 212 int on_autofs = mf->mf_flags & MFF_ON_AUTOFS; 213 int error; 214 215 error = mount_cachefs(mf->mf_mount, 216 mf->mf_fo->opt_rfs, 217 mf->mf_fo->opt_cachedir, 218 mf->mf_mopts, 219 on_autofs); 220 if (error) { 221 errno = error; 222 /* according to Solaris, if errno==ESRCH, "options to not match" */ 223 if (error == ESRCH) 224 plog(XLOG_ERROR, "mount_cachefs: options to no match: %m"); 225 else 226 plog(XLOG_ERROR, "mount_cachefs: %m"); 227 return error; 228 } 229 230 return 0; 231} 232 233 234static int 235cachefs_umount(am_node *am, mntfs *mf) 236{ 237 int unmount_flags = (mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0; 238 int error; 239 240 error = UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags); 241 242 /* 243 * In the case of cachefs, we must fsck the cache directory. Otherwise, 244 * it will remain inconsistent, and the next cachefs mount will fail 245 * with the error "no space left on device" (ENOSPC). 246 * 247 * XXX: this is hacky! use fork/exec/wait instead... 248 */ 249 if (!error) { 250 char *cachedir = NULL; 251 char cmd[128]; 252 253 cachedir = (char *) mf->mf_private; 254 plog(XLOG_INFO, "running fsck on cache directory \"%s\"", cachedir); 255 xsnprintf(cmd, sizeof(cmd), "fsck -F cachefs %s", cachedir); 256 system(cmd); 257 } 258 259 return error; 260} 261