138494Sobrien/* 2174294Sobrien * Copyright (c) 1997-2006 Erez Zadok 338494Sobrien * Copyright (c) 1990 Jan-Simon Pendry 438494Sobrien * Copyright (c) 1990 Imperial College of Science, Technology & Medicine 538494Sobrien * Copyright (c) 1990 The Regents of the University of California. 638494Sobrien * All rights reserved. 738494Sobrien * 838494Sobrien * This code is derived from software contributed to Berkeley by 938494Sobrien * Jan-Simon Pendry at Imperial College, London. 1038494Sobrien * 1138494Sobrien * Redistribution and use in source and binary forms, with or without 1238494Sobrien * modification, are permitted provided that the following conditions 1338494Sobrien * are met: 1438494Sobrien * 1. Redistributions of source code must retain the above copyright 1538494Sobrien * notice, this list of conditions and the following disclaimer. 1638494Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1738494Sobrien * notice, this list of conditions and the following disclaimer in the 1838494Sobrien * documentation and/or other materials provided with the distribution. 1938494Sobrien * 3. All advertising materials mentioning features or use of this software 2042629Sobrien * must display the following acknowledgment: 2138494Sobrien * This product includes software developed by the University of 2238494Sobrien * California, Berkeley and its contributors. 2338494Sobrien * 4. Neither the name of the University nor the names of its contributors 2438494Sobrien * may be used to endorse or promote products derived from this software 2538494Sobrien * without specific prior written permission. 2638494Sobrien * 2738494Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2838494Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2938494Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3038494Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3138494Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3238494Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3338494Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3438494Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3538494Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3638494Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3738494Sobrien * SUCH DAMAGE. 3838494Sobrien * 3938494Sobrien * 40174294Sobrien * File: am-utils/amd/ops_cachefs.c 4138494Sobrien * 4238494Sobrien */ 4338494Sobrien 4438494Sobrien/* 4538494Sobrien * Caching filesystem (Solaris 2.x) 4638494Sobrien */ 4738494Sobrien 4838494Sobrien#ifdef HAVE_CONFIG_H 4938494Sobrien# include <config.h> 5038494Sobrien#endif /* HAVE_CONFIG_H */ 5138494Sobrien#include <am_defs.h> 5238494Sobrien#include <amd.h> 5338494Sobrien 5438494Sobrien/* forward declarations */ 5538494Sobrienstatic char *cachefs_match(am_opts *fo); 5638494Sobrienstatic int cachefs_init(mntfs *mf); 57174294Sobrienstatic int cachefs_mount(am_node *am, mntfs *mf); 58174294Sobrienstatic int cachefs_umount(am_node *am, mntfs *mf); 5938494Sobrien 6038494Sobrien 6138494Sobrien/* 6238494Sobrien * Ops structure 6338494Sobrien */ 6438494Sobrienam_ops cachefs_ops = 6538494Sobrien{ 6638494Sobrien "cachefs", 6738494Sobrien cachefs_match, 6838494Sobrien cachefs_init, 69174294Sobrien cachefs_mount, 70174294Sobrien cachefs_umount, 71174294Sobrien amfs_error_lookup_child, 72174294Sobrien amfs_error_mount_child, 7338494Sobrien amfs_error_readdir, 7438494Sobrien 0, /* cachefs_readlink */ 75174294Sobrien 0, /* cachefs_mounted */ 76174294Sobrien 0, /* cachefs_umounted */ 77174294Sobrien amfs_generic_find_srvr, 78174294Sobrien 0, /* cachefs_get_wchan */ 79174294Sobrien FS_MKMNT | FS_NOTIMEOUT | FS_UBACKGROUND | FS_AMQINFO, /* nfs_fs_flags */ 80174294Sobrien#ifdef HAVE_FS_AUTOFS 81174294Sobrien AUTOFS_CACHEFS_FS_FLAGS, 82174294Sobrien#endif /* HAVE_FS_AUTOFS */ 8338494Sobrien}; 8438494Sobrien 8538494Sobrien 8638494Sobrien/* 8738494Sobrien * Check that f/s has all needed fields. 8838494Sobrien * Returns: matched string if found, NULL otherwise. 8938494Sobrien */ 9038494Sobrienstatic char * 9138494Sobriencachefs_match(am_opts *fo) 9238494Sobrien{ 9338494Sobrien /* sanity check */ 9438494Sobrien if (!fo->opt_rfs || !fo->opt_fs || !fo->opt_cachedir) { 9538494Sobrien plog(XLOG_USER, "cachefs: must specify cachedir, rfs, and fs"); 9638494Sobrien return NULL; 9738494Sobrien } 9838494Sobrien 9938494Sobrien dlog("CACHEFS: using cache directory \"%s\"", fo->opt_cachedir); 10038494Sobrien 10138494Sobrien /* determine magic cookie to put in mtab */ 10238494Sobrien return strdup(fo->opt_cachedir); 10338494Sobrien} 10438494Sobrien 10538494Sobrien 10638494Sobrien/* 10738494Sobrien * Initialize. 10838494Sobrien * Returns: 0 if OK, non-zero (errno) if failed. 10938494Sobrien */ 11038494Sobrienstatic int 11138494Sobriencachefs_init(mntfs *mf) 11238494Sobrien{ 11338494Sobrien /* 11438494Sobrien * Save cache directory name 11538494Sobrien */ 116174294Sobrien if (!mf->mf_private) { 11738494Sobrien mf->mf_private = (voidp) strdup(mf->mf_fo->opt_cachedir); 11838494Sobrien mf->mf_prfree = (void (*)(voidp)) free; 11938494Sobrien } 12038494Sobrien 12138494Sobrien return 0; 12238494Sobrien} 12338494Sobrien 12438494Sobrien 12538494Sobrien/* 12638494Sobrien * mntpt is the mount point ($fs) [XXX: was 'dir'] 12738494Sobrien * backdir is the mounted pathname ($rfs) [XXX: was 'fs_name'] 12838494Sobrien * cachedir is the cache directory ($cachedir) 12938494Sobrien */ 13038494Sobrienstatic int 131174294Sobrienmount_cachefs(char *mntdir, char *backdir, char *cachedir, 132174294Sobrien char *opts, int on_autofs) 13338494Sobrien{ 13438494Sobrien cachefs_args_t ca; 13538494Sobrien mntent_t mnt; 13638494Sobrien int flags; 13738494Sobrien char *cp; 13838494Sobrien MTYPE_TYPE type = MOUNT_TYPE_CACHEFS; /* F/S mount type */ 13938494Sobrien 14038494Sobrien memset((voidp) &ca, 0, sizeof(ca)); /* Paranoid */ 14138494Sobrien 14238494Sobrien /* 14338494Sobrien * Fill in the mount structure 14438494Sobrien */ 14538494Sobrien memset((voidp) &mnt, 0, sizeof(mnt)); 146174294Sobrien mnt.mnt_dir = mntdir; 14738494Sobrien mnt.mnt_fsname = backdir; 14838494Sobrien mnt.mnt_type = MNTTAB_TYPE_CACHEFS; 14938494Sobrien mnt.mnt_opts = opts; 15038494Sobrien 15138494Sobrien flags = compute_mount_flags(&mnt); 152174294Sobrien#ifdef HAVE_FS_AUTOFS 153174294Sobrien if (on_autofs) 154174294Sobrien flags |= autofs_compute_mount_flags(&mnt); 155174294Sobrien#endif /* HAVE_FS_AUTOFS */ 15638494Sobrien 15738494Sobrien /* Fill in cachefs mount arguments */ 15838494Sobrien 15938494Sobrien /* 16038494Sobrien * XXX: Caveats 16138494Sobrien * (1) cache directory is NOT checked for sanity beforehand, nor is it 16238494Sobrien * purged. Maybe it should be purged first? 16338494Sobrien * (2) cache directory is NOT locked. Should we? 16438494Sobrien */ 16538494Sobrien 16638494Sobrien /* mount flags */ 16738494Sobrien ca.cfs_options.opt_flags = CFS_WRITE_AROUND | CFS_ACCESS_BACKFS; 16838494Sobrien /* cache population size */ 16938494Sobrien ca.cfs_options.opt_popsize = DEF_POP_SIZE; /* default: 64K */ 17038494Sobrien /* filegrp size */ 17138494Sobrien ca.cfs_options.opt_fgsize = DEF_FILEGRP_SIZE; /* default: 256 */ 17238494Sobrien 17338494Sobrien /* CFS ID for file system (must be unique) */ 17438494Sobrien ca.cfs_fsid = cachedir; 17538494Sobrien 17638494Sobrien /* CFS fscdir name */ 17738494Sobrien memset(ca.cfs_cacheid, 0, sizeof(ca.cfs_cacheid)); 178174294Sobrien /* 179174294Sobrien * Append cacheid and mountpoint. 180174294Sobrien * sizeof(cfs_cacheid) should be C_MAX_MOUNT_FSCDIRNAME as per 181174294Sobrien * <sys/fs/cachefs_fs.h> (checked on Solaris 8). 182174294Sobrien */ 183174294Sobrien xsnprintf(ca.cfs_cacheid, sizeof(ca.cfs_cacheid), 184174294Sobrien "%s:%s", ca.cfs_fsid, mntdir); 18538494Sobrien /* convert '/' to '_' (Solaris does that...) */ 18638494Sobrien cp = ca.cfs_cacheid; 18738494Sobrien while ((cp = strpbrk(cp, "/")) != NULL) 18838494Sobrien *cp = '_'; 18938494Sobrien 19038494Sobrien /* path for this cache dir */ 19138494Sobrien ca.cfs_cachedir = cachedir; 19238494Sobrien 19338494Sobrien /* back filesystem dir */ 19438494Sobrien ca.cfs_backfs = backdir; 19538494Sobrien 19638494Sobrien /* same as nfs values (XXX: need to handle these options) */ 19738494Sobrien ca.cfs_acregmin = 0; 19838494Sobrien ca.cfs_acregmax = 0; 19938494Sobrien ca.cfs_acdirmin = 0; 20038494Sobrien ca.cfs_acdirmax = 0; 20138494Sobrien 20238494Sobrien /* 20338494Sobrien * Call generic mount routine 20438494Sobrien */ 205174294Sobrien return mount_fs(&mnt, flags, (caddr_t) &ca, 0, type, 0, NULL, mnttab_file_name, on_autofs); 20638494Sobrien} 20738494Sobrien 20838494Sobrien 20938494Sobrienstatic int 210174294Sobriencachefs_mount(am_node *am, mntfs *mf) 21138494Sobrien{ 212174294Sobrien int on_autofs = mf->mf_flags & MFF_ON_AUTOFS; 21338494Sobrien int error; 21438494Sobrien 21538494Sobrien error = mount_cachefs(mf->mf_mount, 21638494Sobrien mf->mf_fo->opt_rfs, 21738494Sobrien mf->mf_fo->opt_cachedir, 218174294Sobrien mf->mf_mopts, 219174294Sobrien on_autofs); 22038494Sobrien if (error) { 22138494Sobrien errno = error; 22238494Sobrien /* according to Solaris, if errno==ESRCH, "options to not match" */ 22338494Sobrien if (error == ESRCH) 22438494Sobrien plog(XLOG_ERROR, "mount_cachefs: options to no match: %m"); 22538494Sobrien else 22638494Sobrien plog(XLOG_ERROR, "mount_cachefs: %m"); 22738494Sobrien return error; 22838494Sobrien } 22938494Sobrien 23038494Sobrien return 0; 23138494Sobrien} 23238494Sobrien 23338494Sobrien 23438494Sobrienstatic int 235174294Sobriencachefs_umount(am_node *am, mntfs *mf) 23638494Sobrien{ 237174294Sobrien int unmount_flags = (mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0; 23838494Sobrien int error; 23938494Sobrien 240174294Sobrien error = UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags); 24138494Sobrien 24238494Sobrien /* 24338494Sobrien * In the case of cachefs, we must fsck the cache directory. Otherwise, 24438494Sobrien * it will remain inconsistent, and the next cachefs mount will fail 24538494Sobrien * with the error "no space left on device" (ENOSPC). 24638494Sobrien * 24738494Sobrien * XXX: this is hacky! use fork/exec/wait instead... 24838494Sobrien */ 24938494Sobrien if (!error) { 25038494Sobrien char *cachedir = NULL; 25138494Sobrien char cmd[128]; 25238494Sobrien 25338494Sobrien cachedir = (char *) mf->mf_private; 25438494Sobrien plog(XLOG_INFO, "running fsck on cache directory \"%s\"", cachedir); 255174294Sobrien xsnprintf(cmd, sizeof(cmd), "fsck -F cachefs %s", cachedir); 25638494Sobrien system(cmd); 25738494Sobrien } 25838494Sobrien 25938494Sobrien return error; 26038494Sobrien} 261