138494Sobrien/*
2310490Scy * Copyright (c) 1997-2014 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.
19310490Scy * 3. Neither the name of the University nor the names of its contributors
2038494Sobrien *    may be used to endorse or promote products derived from this software
2138494Sobrien *    without specific prior written permission.
2238494Sobrien *
2338494Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2438494Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2538494Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2638494Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2738494Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2838494Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2938494Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3038494Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3138494Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3238494Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3338494Sobrien * SUCH DAMAGE.
3438494Sobrien *
3538494Sobrien *
36174294Sobrien * File: am-utils/amd/ops_tmpfs.c
3738494Sobrien *
3838494Sobrien */
3938494Sobrien
4038494Sobrien/*
4138494Sobrien * TMPFS file system (combines RAM-fs and swap-fs)
4238494Sobrien */
4338494Sobrien
4438494Sobrien#ifdef HAVE_CONFIG_H
4538494Sobrien# include <config.h>
4638494Sobrien#endif /* HAVE_CONFIG_H */
4738494Sobrien#include <am_defs.h>
4838494Sobrien#include <amd.h>
4938494Sobrien
50310490Scy/* forward declarations */
51310490Scystatic char *tmpfs_match(am_opts *fo);
52310490Scystatic int tmpfs_mount(am_node *am, mntfs *mf);
53310490Scystatic int tmpfs_umount(am_node *am, mntfs *mf);
54310490Scy
55310490Scy/*
56310490Scy * Ops structure
57310490Scy */
58310490Scyam_ops tmpfs_ops =
59310490Scy{
60310490Scy  "tmpfs",
61310490Scy  tmpfs_match,
62310490Scy  0,				/* tmpfs_init */
63310490Scy  tmpfs_mount,
64310490Scy  tmpfs_umount,
65310490Scy  amfs_error_lookup_child,
66310490Scy  amfs_error_mount_child,
67310490Scy  amfs_error_readdir,
68310490Scy  0,				/* tmpfs_readlink */
69310490Scy  0,				/* tmpfs_mounted */
70310490Scy  0,				/* tmpfs_umounted */
71310490Scy  amfs_generic_find_srvr,
72310490Scy  0,				/* tmpfs_get_wchan */
73310490Scy  FS_MKMNT | FS_NOTIMEOUT | FS_UBACKGROUND | FS_AMQINFO, /* nfs_fs_flags */
74310490Scy#if defined(HAVE_FS_AUTOFS) && defined(AUTOFS_TMPFS_FS_FLAGS)
75310490Scy  AUTOFS_TMPFS_FS_FLAGS,
76310490Scy#endif /* HAVE_FS_AUTOFS */
77310490Scy};
78310490Scy
79310490Scy
80310490Scy/*
81310490Scy * EFS needs local filesystem and device.
82310490Scy */
83310490Scystatic char *
84310490Scytmpfs_match(am_opts *fo)
85310490Scy{
86310490Scy
87310490Scy  if (!fo->opt_dev) {
88310490Scy    plog(XLOG_USER, "tmpfs: no device specified");
89310490Scy    return 0;
90310490Scy  }
91310490Scy
92310490Scy  dlog("EFS: mounting device \"%s\" on \"%s\"", fo->opt_dev, fo->opt_fs);
93310490Scy
94310490Scy  /*
95310490Scy   * Determine magic cookie to put in mtab
96310490Scy   */
97310490Scy  return xstrdup(fo->opt_dev);
98310490Scy}
99310490Scy
100310490Scy
101310490Scystatic int
102310490Scymount_tmpfs(char *mntdir, char *fs_name, char *opts, int on_autofs)
103310490Scy{
104310490Scy  tmpfs_args_t tmpfs_args;
105310490Scy  mntent_t mnt;
106310490Scy  int flags;
107310490Scy  const char *p;
108310490Scy
109310490Scy  /*
110310490Scy   * Figure out the name of the file system type.
111310490Scy   */
112310490Scy  MTYPE_TYPE type = MOUNT_TYPE_TMPFS;
113310490Scy
114310490Scy  p = NULL;
115310490Scy  memset((voidp) &tmpfs_args, 0, sizeof(tmpfs_args)); /* Paranoid */
116310490Scy
117310490Scy  /*
118310490Scy   * Fill in the mount structure
119310490Scy   */
120310490Scy  memset((voidp) &mnt, 0, sizeof(mnt));
121310490Scy  mnt.mnt_dir = mntdir;
122310490Scy  mnt.mnt_fsname = fs_name;
123310490Scy  mnt.mnt_type = MNTTAB_TYPE_TMPFS;
124310490Scy  mnt.mnt_opts = opts;
125310490Scy
126310490Scy  flags = compute_mount_flags(&mnt);
127310490Scy#ifdef HAVE_FS_AUTOFS
128310490Scy  if (on_autofs)
129310490Scy    flags |= autofs_compute_mount_flags(&mnt);
130310490Scy#endif /* HAVE_FS_AUTOFS */
131310490Scy
132310490Scy#if defined(HAVE_TMPFS_ARGS_T_TA_VERSION) && defined(TMPFS_ARGS_VERSION)
133310490Scy  tmpfs_args.ta_version = TMPFS_ARGS_VERSION;
134310490Scy#endif /* HAVE_TMPFS_ARGS_T_TA_VERSION && TMPFS_ARGS_VERSION */
135310490Scy#ifdef HAVE_TMPFS_ARGS_T_TA_NODES_MAX
136310490Scy  if ((p = amu_hasmntopt(&mnt, "nodes")) == NULL)
137310490Scy	p = "1000000";
138310490Scy  tmpfs_args.ta_nodes_max = atoi(p);
139310490Scy#endif /* HAVE_TMPFS_ARGS_T_TA_SIZE_MAX */
140310490Scy#ifdef HAVE_TMPFS_ARGS_T_TA_SIZE_MAX
141310490Scy  if ((p = amu_hasmntopt(&mnt, "size")) == NULL)
142310490Scy	p = "10000000";
143310490Scy  tmpfs_args.ta_size_max = atoi(p);
144310490Scy#endif /* HAVE_TMPFS_ARGS_T_TA_SIZE_MAX */
145310490Scy#ifdef HAVE_TMPFS_ARGS_T_TA_ROOT_UID
146310490Scy  if ((p = amu_hasmntopt(&mnt, "uid")) == NULL)
147310490Scy	p = "0";
148310490Scy  tmpfs_args.ta_root_uid = atoi(p);
149310490Scy#endif /* HAVE_TMPFS_ARGS_T_TA_ROOT_UID */
150310490Scy#ifdef HAVE_TMPFS_ARGS_T_TA_ROOT_GID
151310490Scy  if ((p = amu_hasmntopt(&mnt, "gid")) == NULL)
152310490Scy	p = "0";
153310490Scy  tmpfs_args.ta_root_gid = atoi(p);
154310490Scy#endif /* HAVE_TMPFS_ARGS_T_TA_ROOT_GID */
155310490Scy#ifdef HAVE_TMPFS_ARGS_T_TA_ROOT_MODE
156310490Scy  if ((p = amu_hasmntopt(&mnt, "mode")) == NULL)
157310490Scy	p = "01777";
158310490Scy  tmpfs_args.ta_root_mode = strtol(p, NULL, 8);
159310490Scy#endif /* HAVE_TMPFS_ARGS_T_TA_ROOT_MODE */
160310490Scy
161310490Scy  /*
162310490Scy   * Call generic mount routine
163310490Scy   */
164310490Scy  return mount_fs(&mnt, flags, (caddr_t) &tmpfs_args, 0, type, 0, NULL, mnttab_file_name, on_autofs);
165310490Scy}
166310490Scy
167310490Scy
168310490Scystatic int
169310490Scytmpfs_mount(am_node *am, mntfs *mf)
170310490Scy{
171310490Scy  int on_autofs = mf->mf_flags & MFF_ON_AUTOFS;
172310490Scy  int error;
173310490Scy
174310490Scy  error = mount_tmpfs(mf->mf_mount, mf->mf_info, mf->mf_mopts, on_autofs);
175310490Scy  if (error) {
176310490Scy    errno = error;
177310490Scy    plog(XLOG_ERROR, "mount_tmpfs: %m");
178310490Scy    return error;
179310490Scy  }
180310490Scy
181310490Scy  return 0;
182310490Scy}
183310490Scy
184310490Scy
185310490Scystatic int
186310490Scytmpfs_umount(am_node *am, mntfs *mf)
187310490Scy{
188310490Scy  int unmount_flags = (mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0;
189310490Scy
190310490Scy  return UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags);
191310490Scy}
192310490Scy
193