xref: /netbsd-src/external/bsd/am-utils/dist/amd/ops_ext.c (revision 8bae5d409deb915cf7c8f0539fae22ff2cb8a313)
1 /*	$NetBSD: ops_ext.c,v 1.1.1.1 2015/01/17 16:34:15 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1997-2014 Erez Zadok
5  * Copyright (c) 1990 Jan-Simon Pendry
6  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
7  * Copyright (c) 1990 The Regents of the University of California.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * Jan-Simon Pendry at Imperial College, London.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *
38  * File: am-utils/amd/ops_ext.c
39  *
40  */
41 
42 /*
43  * Irix UN*X file system: EXT (Extended File System)
44  */
45 
46 #ifdef HAVE_CONFIG_H
47 # include <config.h>
48 #endif /* HAVE_CONFIG_H */
49 #include <am_defs.h>
50 #include <amd.h>
51 
52 /* forward declarations */
53 static char *ext_match(am_opts *fo);
54 static int ext2_mount(am_node *am, mntfs *mf);
55 static int ext3_mount(am_node *am, mntfs *mf);
56 static int ext4_mount(am_node *am, mntfs *mf);
57 static int ext_umount(am_node *am, mntfs *mf);
58 
59 /*
60  * Ops structure
61  */
62 am_ops ext2_ops =
63 {
64   "ext2",
65   ext_match,
66   0,				/* ext_init */
67   ext2_mount,
68   ext_umount,
69   amfs_error_lookup_child,
70   amfs_error_mount_child,
71   amfs_error_readdir,
72   0,				/* ext_readlink */
73   0,				/* ext_mounted */
74   0,				/* ext_umounted */
75   amfs_generic_find_srvr,
76   0,				/* ext_get_wchan */
77   FS_MKMNT | FS_NOTIMEOUT | FS_UBACKGROUND | FS_AMQINFO, /* nfs_fs_flags */
78 #ifdef HAVE_FS_AUTOFS
79   AUTOFS_EXT_FS_FLAGS,
80 #endif /* HAVE_FS_AUTOFS */
81 };
82 
83 am_ops ext3_ops =
84 {
85   "ext3",
86   ext_match,
87   0,				/* ext_init */
88   ext3_mount,
89   ext_umount,
90   amfs_error_lookup_child,
91   amfs_error_mount_child,
92   amfs_error_readdir,
93   0,				/* ext_readlink */
94   0,				/* ext_mounted */
95   0,				/* ext_umounted */
96   amfs_generic_find_srvr,
97   0,				/* ext_get_wchan */
98   FS_MKMNT | FS_NOTIMEOUT | FS_UBACKGROUND | FS_AMQINFO, /* nfs_fs_flags */
99 #ifdef HAVE_FS_AUTOFS
100   AUTOFS_EXT_FS_FLAGS,
101 #endif /* HAVE_FS_AUTOFS */
102 };
103 
104 am_ops ext4_ops =
105 {
106   "ext4",
107   ext_match,
108   0,				/* ext_init */
109   ext4_mount,
110   ext_umount,
111   amfs_error_lookup_child,
112   amfs_error_mount_child,
113   amfs_error_readdir,
114   0,				/* ext_readlink */
115   0,				/* ext_mounted */
116   0,				/* ext_umounted */
117   amfs_generic_find_srvr,
118   0,				/* ext_get_wchan */
119   FS_MKMNT | FS_NOTIMEOUT | FS_UBACKGROUND | FS_AMQINFO, /* nfs_fs_flags */
120 #ifdef HAVE_FS_AUTOFS
121   AUTOFS_EXT_FS_FLAGS,
122 #endif /* HAVE_FS_AUTOFS */
123 };
124 
125 /*
126  * EXT needs local filesystem and device.
127  */
128 static char *
ext_match(am_opts * fo)129 ext_match(am_opts *fo)
130 {
131 
132   if (!fo->opt_dev) {
133     plog(XLOG_USER, "ext: no device specified");
134     return 0;
135   }
136 
137   dlog("EXT: mounting device \"%s\" on \"%s\"", fo->opt_dev, fo->opt_fs);
138 
139   /*
140    * Determine magic cookie to put in mtab
141    */
142   return xstrdup(fo->opt_dev);
143 }
144 
145 
146 static int
mount_ext(char * mntdir,char * fs_name,char * opts,int on_autofs,char * mount_type,const char * mnttab_type)147 mount_ext(char *mntdir, char *fs_name, char *opts, int on_autofs, char *
148     mount_type, const char *mnttab_type)
149 {
150   ext_args_t ext_args;
151   mntent_t mnt;
152   int flags;
153 
154   /*
155    * Figure out the name of the file system type.
156    */
157   MTYPE_TYPE type = mount_type;
158 
159   memset((voidp) &ext_args, 0, sizeof(ext_args)); /* Paranoid */
160 
161   /*
162    * Fill in the mount structure
163    */
164   memset((voidp) &mnt, 0, sizeof(mnt));
165   mnt.mnt_dir = mntdir;
166   mnt.mnt_fsname = fs_name;
167   mnt.mnt_type = mnttab_type;
168   mnt.mnt_opts = opts;
169 
170   flags = compute_mount_flags(&mnt);
171 #ifdef HAVE_FS_AUTOFS
172   if (on_autofs)
173     flags |= autofs_compute_mount_flags(&mnt);
174 #endif /* HAVE_FS_AUTOFS */
175 
176   /*
177    * Call generic mount routine
178    */
179   return mount_fs(&mnt, flags, (caddr_t) &ext_args, 0, type, 0, NULL, mnttab_file_name, on_autofs);
180 }
181 
182 
183 static int
ext_mount(am_node * am,mntfs * mf,char * mount_type,const char * mnttab_type)184 ext_mount(am_node *am, mntfs *mf, char *mount_type,
185   const char *mnttab_type)
186 {
187   int on_autofs = mf->mf_flags & MFF_ON_AUTOFS;
188   int error;
189 
190   error = mount_ext(mf->mf_mount, mf->mf_info, mf->mf_mopts, on_autofs,
191       mount_type, mnttab_type);
192   if (error) {
193     errno = error;
194     plog(XLOG_ERROR, "mount_ext: %m");
195     return error;
196   }
197 
198   return 0;
199 }
200 
201 static int
ext2_mount(am_node * am,mntfs * mf)202 ext2_mount(am_node *am, mntfs *mf)
203 {
204   return ext_mount(am, mf, MOUNT_TYPE_EXT2, MNTTAB_TYPE_EXT2);
205 }
206 
207 static int
ext3_mount(am_node * am,mntfs * mf)208 ext3_mount(am_node *am, mntfs *mf)
209 {
210   return ext_mount(am, mf, MOUNT_TYPE_EXT3, MNTTAB_TYPE_EXT3);
211 }
212 
213 static int
ext4_mount(am_node * am,mntfs * mf)214 ext4_mount(am_node *am, mntfs *mf)
215 {
216   return ext_mount(am, mf, MOUNT_TYPE_EXT4, MNTTAB_TYPE_EXT4);
217 }
218 
219 static int
ext_umount(am_node * am,mntfs * mf)220 ext_umount(am_node *am, mntfs *mf)
221 {
222   int unmount_flags = (mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0;
223 
224   return UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags);
225 }
226