ops_cachefs.c revision 267654
1190207Srpaulo/* 2127668Sbms * Copyright (c) 1997-2006 Erez Zadok 3127668Sbms * Copyright (c) 1990 Jan-Simon Pendry 4127668Sbms * Copyright (c) 1990 Imperial College of Science, Technology & Medicine 5127668Sbms * Copyright (c) 1990 The Regents of the University of California. 6127668Sbms * All rights reserved. 7127668Sbms * 8127668Sbms * This code is derived from software contributed to Berkeley by 9127668Sbms * Jan-Simon Pendry at Imperial College, London. 10127668Sbms * 11127668Sbms * Redistribution and use in source and binary forms, with or without 12127668Sbms * modification, are permitted provided that the following conditions 13127668Sbms * are met: 14127668Sbms * 1. Redistributions of source code must retain the above copyright 15127668Sbms * notice, this list of conditions and the following disclaimer. 16127668Sbms * 2. Redistributions in binary form must reproduce the above copyright 17127668Sbms * notice, this list of conditions and the following disclaimer in the 18127668Sbms * documentation and/or other materials provided with the distribution. 19127668Sbms * 3. All advertising materials mentioning features or use of this software 20127668Sbms * must display the following acknowledgment: 21127668Sbms * This product includes software developed by the University of 22127668Sbms * California, Berkeley and its contributors. 23127668Sbms * 4. Neither the name of the University nor the names of its contributors 24127668Sbms * may be used to endorse or promote products derived from this software 25127668Sbms * without specific prior written permission. 26127668Sbms * 27127668Sbms * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28127668Sbms * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29127668Sbms * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30127668Sbms * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31127668Sbms * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32127668Sbms * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33127668Sbms * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34127668Sbms * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35127668Sbms * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36127668Sbms * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37127668Sbms * SUCH DAMAGE. 38127668Sbms * 39127668Sbms * 40127668Sbms * File: am-utils/amd/ops_cachefs.c 41127668Sbms * 42127668Sbms */ 43127668Sbms 44127668Sbms/* 45127668Sbms * Caching filesystem (Solaris 2.x) 46127668Sbms */ 47127668Sbms 48127668Sbms#ifdef HAVE_CONFIG_H 49127668Sbms# include <config.h> 50127668Sbms#endif /* HAVE_CONFIG_H */ 51127668Sbms#include <am_defs.h> 52127668Sbms#include <amd.h> 53127668Sbms 54127668Sbms/* forward declarations */ 55127668Sbmsstatic char *cachefs_match(am_opts *fo); 56127668Sbmsstatic int cachefs_init(mntfs *mf); 57127668Sbmsstatic int cachefs_mount(am_node *am, mntfs *mf); 58127668Sbmsstatic int cachefs_umount(am_node *am, mntfs *mf); 59127668Sbms 60127668Sbms 61127668Sbms/* 62127668Sbms * Ops structure 63127668Sbms */ 64127668Sbmsam_ops cachefs_ops = 65127668Sbms{ 66127668Sbms "cachefs", 67127668Sbms cachefs_match, 68127668Sbms cachefs_init, 69127668Sbms cachefs_mount, 70127668Sbms cachefs_umount, 71127668Sbms amfs_error_lookup_child, 72127668Sbms amfs_error_mount_child, 73127668Sbms amfs_error_readdir, 74127668Sbms 0, /* cachefs_readlink */ 75127668Sbms 0, /* cachefs_mounted */ 76127668Sbms 0, /* cachefs_umounted */ 77127668Sbms amfs_generic_find_srvr, 78127668Sbms 0, /* cachefs_get_wchan */ 79127668Sbms FS_MKMNT | FS_NOTIMEOUT | FS_UBACKGROUND | FS_AMQINFO, /* nfs_fs_flags */ 80127668Sbms#ifdef HAVE_FS_AUTOFS 81127668Sbms AUTOFS_CACHEFS_FS_FLAGS, 82127668Sbms#endif /* HAVE_FS_AUTOFS */ 83127668Sbms}; 84127668Sbms 85127668Sbms 86127668Sbms/* 87127668Sbms * Check that f/s has all needed fields. 88127668Sbms * Returns: matched string if found, NULL otherwise. 89127668Sbms */ 90127668Sbmsstatic char * 91127668Sbmscachefs_match(am_opts *fo) 92127668Sbms{ 93127668Sbms /* sanity check */ 94127668Sbms if (!fo->opt_rfs || !fo->opt_fs || !fo->opt_cachedir) { 95127668Sbms plog(XLOG_USER, "cachefs: must specify cachedir, rfs, and fs"); 96127668Sbms return NULL; 97127668Sbms } 98127668Sbms 99127668Sbms dlog("CACHEFS: using cache directory \"%s\"", fo->opt_cachedir); 100127668Sbms 101127668Sbms /* determine magic cookie to put in mtab */ 102127668Sbms return strdup(fo->opt_cachedir); 103127668Sbms} 104127668Sbms 105127668Sbms 106127668Sbms/* 107127668Sbms * Initialize. 108127668Sbms * Returns: 0 if OK, non-zero (errno) if failed. 109127668Sbms */ 110127668Sbmsstatic int 111127668Sbmscachefs_init(mntfs *mf) 112127668Sbms{ 113127668Sbms /* 114127668Sbms * Save cache directory name 115127668Sbms */ 116127668Sbms if (!mf->mf_private) { 117127668Sbms mf->mf_private = (voidp) strdup(mf->mf_fo->opt_cachedir); 118127668Sbms mf->mf_prfree = (void (*)(voidp)) free; 119127668Sbms } 120127668Sbms 121127668Sbms return 0; 122127668Sbms} 123127668Sbms 124127668Sbms 125127668Sbms/* 126127668Sbms * mntpt is the mount point ($fs) [XXX: was 'dir'] 127127668Sbms * backdir is the mounted pathname ($rfs) [XXX: was 'fs_name'] 128127668Sbms * cachedir is the cache directory ($cachedir) 129127668Sbms */ 130127668Sbmsstatic int 131127668Sbmsmount_cachefs(char *mntdir, char *backdir, char *cachedir, 132127668Sbms char *opts, int on_autofs) 133127668Sbms{ 134127668Sbms cachefs_args_t ca; 135127668Sbms mntent_t mnt; 136127668Sbms int flags; 137127668Sbms char *cp; 138127668Sbms MTYPE_TYPE type = MOUNT_TYPE_CACHEFS; /* F/S mount type */ 139127668Sbms 140127668Sbms memset((voidp) &ca, 0, sizeof(ca)); /* Paranoid */ 141127668Sbms 142127668Sbms /* 143127668Sbms * Fill in the mount structure 144127668Sbms */ 145127668Sbms memset((voidp) &mnt, 0, sizeof(mnt)); 146127668Sbms mnt.mnt_dir = mntdir; 147127668Sbms mnt.mnt_fsname = backdir; 148127668Sbms mnt.mnt_type = MNTTAB_TYPE_CACHEFS; 149127668Sbms mnt.mnt_opts = opts; 150127668Sbms 151127668Sbms flags = compute_mount_flags(&mnt); 152127668Sbms#ifdef HAVE_FS_AUTOFS 153127668Sbms if (on_autofs) 154127668Sbms flags |= autofs_compute_mount_flags(&mnt); 155127668Sbms#endif /* HAVE_FS_AUTOFS */ 156127668Sbms 157127668Sbms /* Fill in cachefs mount arguments */ 158127668Sbms 159127668Sbms /* 160127668Sbms * XXX: Caveats 161127668Sbms * (1) cache directory is NOT checked for sanity beforehand, nor is it 162127668Sbms * purged. Maybe it should be purged first? 163127668Sbms * (2) cache directory is NOT locked. Should we? 164127668Sbms */ 165127668Sbms 166127668Sbms /* mount flags */ 167127668Sbms ca.cfs_options.opt_flags = CFS_WRITE_AROUND | CFS_ACCESS_BACKFS; 168127668Sbms /* cache population size */ 169127668Sbms ca.cfs_options.opt_popsize = DEF_POP_SIZE; /* default: 64K */ 170127668Sbms /* filegrp size */ 171127668Sbms ca.cfs_options.opt_fgsize = DEF_FILEGRP_SIZE; /* default: 256 */ 172127668Sbms 173127668Sbms /* CFS ID for file system (must be unique) */ 174127668Sbms ca.cfs_fsid = cachedir; 175127668Sbms 176127668Sbms /* CFS fscdir name */ 177127668Sbms memset(ca.cfs_cacheid, 0, sizeof(ca.cfs_cacheid)); 178127668Sbms /* 179127668Sbms * Append cacheid and mountpoint. 180127668Sbms * sizeof(cfs_cacheid) should be C_MAX_MOUNT_FSCDIRNAME as per 181127668Sbms * <sys/fs/cachefs_fs.h> (checked on Solaris 8). 182127668Sbms */ 183127668Sbms xsnprintf(ca.cfs_cacheid, sizeof(ca.cfs_cacheid), 184127668Sbms "%s:%s", ca.cfs_fsid, mntdir); 185127668Sbms /* convert '/' to '_' (Solaris does that...) */ 186127668Sbms cp = ca.cfs_cacheid; 187127668Sbms while ((cp = strpbrk(cp, "/")) != NULL) 188127668Sbms *cp = '_'; 189127668Sbms 190127668Sbms /* 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