mount_linux.c revision 1.1.1.1 1 1.1 christos /* $NetBSD: mount_linux.c,v 1.1.1.1 2008/09/19 20:07:17 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 1997-2007 Erez Zadok
5 1.1 christos * Copyright (c) 1990 Jan-Simon Pendry
6 1.1 christos * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
7 1.1 christos * Copyright (c) 1990 The Regents of the University of California.
8 1.1 christos * All rights reserved.
9 1.1 christos *
10 1.1 christos * This code is derived from software contributed to Berkeley by
11 1.1 christos * Jan-Simon Pendry at Imperial College, London.
12 1.1 christos *
13 1.1 christos * Redistribution and use in source and binary forms, with or without
14 1.1 christos * modification, are permitted provided that the following conditions
15 1.1 christos * are met:
16 1.1 christos * 1. Redistributions of source code must retain the above copyright
17 1.1 christos * notice, this list of conditions and the following disclaimer.
18 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
19 1.1 christos * notice, this list of conditions and the following disclaimer in the
20 1.1 christos * documentation and/or other materials provided with the distribution.
21 1.1 christos * 3. All advertising materials mentioning features or use of this software
22 1.1 christos * must display the following acknowledgment:
23 1.1 christos * This product includes software developed by the University of
24 1.1 christos * California, Berkeley and its contributors.
25 1.1 christos * 4. Neither the name of the University nor the names of its contributors
26 1.1 christos * may be used to endorse or promote products derived from this software
27 1.1 christos * without specific prior written permission.
28 1.1 christos *
29 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 1.1 christos * SUCH DAMAGE.
40 1.1 christos *
41 1.1 christos *
42 1.1 christos * File: am-utils/conf/mount/mount_linux.c
43 1.1 christos */
44 1.1 christos
45 1.1 christos /*
46 1.1 christos * Linux mount helper.
47 1.1 christos */
48 1.1 christos
49 1.1 christos #ifdef HAVE_CONFIG_H
50 1.1 christos # include <config.h>
51 1.1 christos #endif /* HAVE_CONFIG_H */
52 1.1 christos #include <am_defs.h>
53 1.1 christos #include <amu.h>
54 1.1 christos
55 1.1 christos
56 1.1 christos #ifndef MOUNT_TYPE_UFS
57 1.1 christos /*
58 1.1 christos * Autoconf didn't find any disk-based f/s on this system,
59 1.1 christos * So provide some default definition for this file to compile.
60 1.1 christos */
61 1.1 christos # define MOUNT_TYPE_UFS "no_disk_fs"
62 1.1 christos #endif /* not MOUNT_TYPE_UFS */
63 1.1 christos
64 1.1 christos struct opt_map {
65 1.1 christos const char *opt; /* option name */
66 1.1 christos int inv; /* true if flag value should be inverted */
67 1.1 christos int mask; /* flag mask value */
68 1.1 christos };
69 1.1 christos
70 1.1 christos const struct opt_map opt_map[] =
71 1.1 christos {
72 1.1 christos {"defaults", 0, 0},
73 1.1 christos {MNTTAB_OPT_RO, 0, MNT2_GEN_OPT_RDONLY},
74 1.1 christos {MNTTAB_OPT_RW, 1, MNT2_GEN_OPT_RDONLY},
75 1.1 christos {MNTTAB_OPT_EXEC, 1, MNT2_GEN_OPT_NOEXEC},
76 1.1 christos {MNTTAB_OPT_NOEXEC, 0, MNT2_GEN_OPT_NOEXEC},
77 1.1 christos {MNTTAB_OPT_SUID, 1, MNT2_GEN_OPT_NOSUID},
78 1.1 christos {MNTTAB_OPT_NOSUID, 0, MNT2_GEN_OPT_NOSUID},
79 1.1 christos #ifdef MNT2_GEN_OPT_NODEV
80 1.1 christos {MNTTAB_OPT_NODEV, 0, MNT2_GEN_OPT_NODEV},
81 1.1 christos #endif /* MNT2_GEN_OPT_NODEV */
82 1.1 christos #ifdef MNT2_GEN_OPT_SYNC
83 1.1 christos {MNTTAB_OPT_SYNC, 0, MNT2_GEN_OPT_SYNC},
84 1.1 christos {MNTTAB_OPT_ASYNC, 1, MNT2_GEN_OPT_SYNC},
85 1.1 christos #endif /* MNT2_GEN_OPT_SYNC */
86 1.1 christos #ifdef MNT2_GEN_OPT_NOSUB
87 1.1 christos {MNTTAB_OPT_SUB, 1, MNT2_GEN_OPT_NOSUB},
88 1.1 christos {MNTTAB_OPT_NOSUB, 0, MNT2_GEN_OPT_NOSUB},
89 1.1 christos #endif /* MNT2_GEN_OPT_NOSUB */
90 1.1 christos {NULL, 0, 0}
91 1.1 christos };
92 1.1 christos
93 1.1 christos struct fs_opts {
94 1.1 christos const char *opt;
95 1.1 christos int type; /* XXX: Ion, what is this for? */
96 1.1 christos };
97 1.1 christos
98 1.1 christos const struct fs_opts iso_opts[] = {
99 1.1 christos { "map", 0 },
100 1.1 christos { "norock", 0 },
101 1.1 christos { "cruft", 0 },
102 1.1 christos { "unhide", 0 },
103 1.1 christos { "conv", 1 },
104 1.1 christos { "block", 1 },
105 1.1 christos { "mode", 1 },
106 1.1 christos { "gid", 1 },
107 1.1 christos { "uid", 1 },
108 1.1 christos { NULL, 0 }
109 1.1 christos };
110 1.1 christos
111 1.1 christos const struct fs_opts dos_opts[] = {
112 1.1 christos { "check", 1 },
113 1.1 christos { "conv", 1 },
114 1.1 christos { "uid", 1 },
115 1.1 christos { "gid", 1 },
116 1.1 christos { "umask", 1 },
117 1.1 christos { "debug", 0 },
118 1.1 christos { "fat", 1 },
119 1.1 christos { "quiet", 0 },
120 1.1 christos { "blocksize",1 },
121 1.1 christos { NULL, 0 }
122 1.1 christos };
123 1.1 christos
124 1.1 christos const struct fs_opts autofs_opts[] = {
125 1.1 christos { "fd", 1 },
126 1.1 christos { "pgrp", 1 },
127 1.1 christos { "minproto", 1 },
128 1.1 christos { "maxproto", 1 },
129 1.1 christos { NULL, 0 }
130 1.1 christos };
131 1.1 christos
132 1.1 christos const struct fs_opts null_opts[] = {
133 1.1 christos { NULL, 0 }
134 1.1 christos };
135 1.1 christos
136 1.1 christos
137 1.1 christos /*
138 1.1 christos * New parser for linux-specific mounts.
139 1.1 christos * Should now handle fs-type specific mount-options correctly.
140 1.1 christos * Currently implemented: msdos, iso9660.
141 1.1 christos */
142 1.1 christos static char *
143 1.1 christos parse_opts(char *type, const char *optstr, int *flags, char **xopts, int *noauto)
144 1.1 christos {
145 1.1 christos const struct opt_map *std_opts;
146 1.1 christos const struct fs_opts *dev_opts;
147 1.1 christos char *opt, *topts, *xoptstr;
148 1.1 christos size_t l;
149 1.1 christos
150 1.1 christos if (optstr == NULL)
151 1.1 christos return NULL;
152 1.1 christos
153 1.1 christos xoptstr = strdup(optstr); /* because strtok is destructive below */
154 1.1 christos
155 1.1 christos *noauto = 0;
156 1.1 christos l = strlen(optstr) + 2;
157 1.1 christos *xopts = (char *) xmalloc(l);
158 1.1 christos topts = (char *) xmalloc(l);
159 1.1 christos *topts = '\0';
160 1.1 christos **xopts = '\0';
161 1.1 christos
162 1.1 christos for (opt = strtok(xoptstr, ","); opt; opt = strtok(NULL, ",")) {
163 1.1 christos /*
164 1.1 christos * First, parse standard options
165 1.1 christos */
166 1.1 christos std_opts = opt_map;
167 1.1 christos while (std_opts->opt &&
168 1.1 christos !NSTREQ(std_opts->opt, opt, strlen(std_opts->opt)))
169 1.1 christos ++std_opts;
170 1.1 christos if (!(*noauto = STREQ(opt, MNTTAB_OPT_NOAUTO)) || std_opts->opt) {
171 1.1 christos xstrlcat(topts, opt, l);
172 1.1 christos xstrlcat(topts, ",", l);
173 1.1 christos if (std_opts->inv)
174 1.1 christos *flags &= ~std_opts->mask;
175 1.1 christos else
176 1.1 christos *flags |= std_opts->mask;
177 1.1 christos }
178 1.1 christos /*
179 1.1 christos * Next, select which fs-type is to be used
180 1.1 christos * and parse the fs-specific options
181 1.1 christos */
182 1.1 christos #ifdef MOUNT_TYPE_AUTOFS
183 1.1 christos if (STREQ(type, MOUNT_TYPE_AUTOFS)) {
184 1.1 christos dev_opts = autofs_opts;
185 1.1 christos goto do_opts;
186 1.1 christos }
187 1.1 christos #endif /* MOUNT_TYPE_AUTOFS */
188 1.1 christos #ifdef MOUNT_TYPE_PCFS
189 1.1 christos if (STREQ(type, MOUNT_TYPE_PCFS)) {
190 1.1 christos dev_opts = dos_opts;
191 1.1 christos goto do_opts;
192 1.1 christos }
193 1.1 christos #endif /* MOUNT_TYPE_PCFS */
194 1.1 christos #ifdef MOUNT_TYPE_CDFS
195 1.1 christos if (STREQ(type, MOUNT_TYPE_CDFS)) {
196 1.1 christos dev_opts = iso_opts;
197 1.1 christos goto do_opts;
198 1.1 christos }
199 1.1 christos #endif /* MOUNT_TYPE_CDFS */
200 1.1 christos #ifdef MOUNT_TYPE_LOFS
201 1.1 christos if (STREQ(type, MOUNT_TYPE_LOFS)) {
202 1.1 christos dev_opts = null_opts;
203 1.1 christos goto do_opts;
204 1.1 christos }
205 1.1 christos #endif /* MOUNT_TYPE_LOFS */
206 1.1 christos plog(XLOG_FATAL, "linux mount: unknown fs-type: %s\n", type);
207 1.1 christos XFREE(xoptstr);
208 1.1 christos XFREE(*xopts);
209 1.1 christos XFREE(topts);
210 1.1 christos return NULL;
211 1.1 christos
212 1.1 christos do_opts:
213 1.1 christos while (dev_opts->opt &&
214 1.1 christos (!NSTREQ(dev_opts->opt, opt, strlen(dev_opts->opt)))) {
215 1.1 christos ++dev_opts;
216 1.1 christos }
217 1.1 christos if (dev_opts->opt && *xopts) {
218 1.1 christos xstrlcat(*xopts, opt, l);
219 1.1 christos xstrlcat(*xopts, ",", l);
220 1.1 christos }
221 1.1 christos }
222 1.1 christos /*
223 1.1 christos * All other options are discarded
224 1.1 christos */
225 1.1 christos if (strlen(*xopts))
226 1.1 christos *(*xopts + strlen(*xopts)-1) = '\0';
227 1.1 christos if (strlen(topts))
228 1.1 christos topts[strlen(topts)-1] = '\0';
229 1.1 christos XFREE(xoptstr);
230 1.1 christos return topts;
231 1.1 christos }
232 1.1 christos
233 1.1 christos
234 1.1 christos /*
235 1.1 christos * Returns combined linux kernel version number. For a kernel numbered
236 1.1 christos * x.y.z, returns x*65535+y*256+z.
237 1.1 christos */
238 1.1 christos int
239 1.1 christos linux_version_code(void)
240 1.1 christos {
241 1.1 christos struct utsname my_utsname;
242 1.1 christos static int release = 0;
243 1.1 christos
244 1.1 christos if ( 0 == release && 0 == uname(&my_utsname)) {
245 1.1 christos release = 65536 * atoi(strtok(my_utsname.release, "."))
246 1.1 christos + 256 * atoi(strtok(NULL, "."))
247 1.1 christos + atoi(strtok(NULL, "."));
248 1.1 christos }
249 1.1 christos return release;
250 1.1 christos }
251 1.1 christos
252 1.1 christos
253 1.1 christos int
254 1.1 christos do_mount_linux(MTYPE_TYPE type, mntent_t *mnt, int flags, caddr_t data)
255 1.1 christos {
256 1.1 christos if (amuDebug(D_FULL)) {
257 1.1 christos plog(XLOG_DEBUG, "do_mount_linux: fsname %s\n", mnt->mnt_fsname);
258 1.1 christos plog(XLOG_DEBUG, "do_mount_linux: type (mntent) %s\n", mnt->mnt_type);
259 1.1 christos plog(XLOG_DEBUG, "do_mount_linux: opts %s\n", mnt->mnt_opts);
260 1.1 christos plog(XLOG_DEBUG, "do_mount_linux: dir %s\n", mnt->mnt_dir);
261 1.1 christos }
262 1.1 christos
263 1.1 christos /*
264 1.1 christos * If we have an nfs mount, the 5th argument to system mount() must be the
265 1.1 christos * nfs_mount_data structure, otherwise it is the return from parse_opts()
266 1.1 christos */
267 1.1 christos return mount(mnt->mnt_fsname,
268 1.1 christos mnt->mnt_dir,
269 1.1 christos type,
270 1.1 christos MS_MGC_VAL | flags,
271 1.1 christos data);
272 1.1 christos }
273 1.1 christos
274 1.1 christos
275 1.1 christos int
276 1.1 christos mount_linux_nfs(MTYPE_TYPE type, mntent_t *mnt, int flags, caddr_t data)
277 1.1 christos {
278 1.1 christos nfs_args_t *mnt_data = (nfs_args_t *) data;
279 1.1 christos int errorcode;
280 1.1 christos
281 1.1 christos /* Fake some values for linux */
282 1.1 christos mnt_data->version = NFS_MOUNT_VERSION;
283 1.1 christos if (!mnt_data->timeo) {
284 1.1 christos #ifdef MNT2_NFS_OPT_TCP
285 1.1 christos if (mnt_data->flags & MNT2_NFS_OPT_TCP)
286 1.1 christos mnt_data->timeo = 600;
287 1.1 christos else
288 1.1 christos #endif /* MNT2_NFS_OPT_TCP */
289 1.1 christos mnt_data->timeo = 7;
290 1.1 christos }
291 1.1 christos if (!mnt_data->retrans)
292 1.1 christos mnt_data->retrans = 3;
293 1.1 christos
294 1.1 christos #ifdef MNT2_NFS_OPT_NOAC
295 1.1 christos if (!(mnt_data->flags & MNT2_NFS_OPT_NOAC)) {
296 1.1 christos if (!mnt_data->acregmin)
297 1.1 christos mnt_data->acregmin = 3;
298 1.1 christos if (!mnt_data->acregmax)
299 1.1 christos mnt_data->acregmax = 60;
300 1.1 christos if (!mnt_data->acdirmin)
301 1.1 christos mnt_data->acdirmin = 30;
302 1.1 christos if (!mnt_data->acdirmax)
303 1.1 christos mnt_data->acdirmax = 60;
304 1.1 christos }
305 1.1 christos #endif /* MNT2_NFS_OPT_NOAC */
306 1.1 christos
307 1.1 christos /*
308 1.1 christos * in nfs structure implementation version 4, the old
309 1.1 christos * filehandle field was renamed "old_root" and left as 3rd field,
310 1.1 christos * while a new field called "root" was added to the end of the
311 1.1 christos * structure. Both of them however need a copy of the file handle
312 1.1 christos * for NFSv2 mounts.
313 1.1 christos */
314 1.1 christos #ifdef MNT2_NFS_OPT_VER3
315 1.1 christos if (mnt_data->flags & MNT2_NFS_OPT_VER3)
316 1.1 christos memset(mnt_data->old_root.data, 0, FHSIZE);
317 1.1 christos else
318 1.1 christos #endif /* MNT2_NFS_OPT_VER3 */
319 1.1 christos memcpy(mnt_data->old_root.data, mnt_data->root.data, FHSIZE);
320 1.1 christos
321 1.1 christos #ifdef HAVE_NFS_ARGS_T_BSIZE
322 1.1 christos /* linux mount version 3 */
323 1.1 christos mnt_data->bsize = 0; /* let the kernel decide */
324 1.1 christos #endif /* HAVE_NFS_ARGS_T_BSIZE */
325 1.1 christos
326 1.1 christos #ifdef HAVE_NFS_ARGS_T_NAMLEN
327 1.1 christos /* linux mount version 2 */
328 1.1 christos mnt_data->namlen = NAME_MAX; /* 256 bytes */
329 1.1 christos #endif /* HAVE_NFS_ARGS_T_NAMELEN */
330 1.1 christos
331 1.1 christos mnt_data->fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
332 1.1 christos if (mnt_data->fd < 0) {
333 1.1 christos plog(XLOG_ERROR, "Can't create socket for kernel");
334 1.1 christos return 1;
335 1.1 christos }
336 1.1 christos if (bindresvport(mnt_data->fd, NULL) < 0) {
337 1.1 christos plog(XLOG_ERROR, "Can't bind to reserved port");
338 1.1 christos errorcode = 1;
339 1.1 christos goto out;
340 1.1 christos }
341 1.1 christos /*
342 1.1 christos * connect() the socket for kernels 1.3.10 and below
343 1.1 christos * only to avoid problems with multihomed hosts.
344 1.1 christos */
345 1.1 christos if (linux_version_code() <= 0x01030a) {
346 1.1 christos int ret = connect(mnt_data->fd,
347 1.1 christos (struct sockaddr *) &mnt_data->addr,
348 1.1 christos sizeof(mnt_data->addr));
349 1.1 christos if (ret < 0) {
350 1.1 christos plog(XLOG_ERROR, "Can't connect socket for kernel");
351 1.1 christos errorcode = 1;
352 1.1 christos goto out;
353 1.1 christos }
354 1.1 christos }
355 1.1 christos if (amuDebug(D_FULL)) {
356 1.1 christos plog(XLOG_DEBUG, "mount_linux_nfs: type %s\n", type);
357 1.1 christos plog(XLOG_DEBUG, "mount_linux_nfs: version %d\n", mnt_data->version);
358 1.1 christos plog(XLOG_DEBUG, "mount_linux_nfs: fd %d\n", mnt_data->fd);
359 1.1 christos plog(XLOG_DEBUG, "mount_linux_nfs: hostname %s\n",
360 1.1 christos inet_ntoa(mnt_data->addr.sin_addr));
361 1.1 christos plog(XLOG_DEBUG, "mount_linux_nfs: port %d\n",
362 1.1 christos htons(mnt_data->addr.sin_port));
363 1.1 christos }
364 1.1 christos if (amuDebug(D_TRACE)) {
365 1.1 christos plog(XLOG_DEBUG, "mount_linux_nfs: Generic mount flags 0x%x", MS_MGC_VAL | flags);
366 1.1 christos plog(XLOG_DEBUG, "mount_linux_nfs: updated nfs_args...");
367 1.1 christos print_nfs_args(mnt_data, 0);
368 1.1 christos }
369 1.1 christos
370 1.1 christos errorcode = do_mount_linux(type, mnt, flags, data);
371 1.1 christos
372 1.1 christos out:
373 1.1 christos /*
374 1.1 christos * If we failed, (i.e. errorcode != 0), then close the socket
375 1.1 christos * if it is open.
376 1.1 christos */
377 1.1 christos if (errorcode && mnt_data->fd != -1) {
378 1.1 christos /* save errno, may be clobbered by close() call! */
379 1.1 christos int save_errno = errno;
380 1.1 christos close(mnt_data->fd);
381 1.1 christos errno = save_errno;
382 1.1 christos }
383 1.1 christos return errorcode;
384 1.1 christos }
385 1.1 christos
386 1.1 christos
387 1.1 christos int
388 1.1 christos mount_linux_nonfs(MTYPE_TYPE type, mntent_t *mnt, int flags, caddr_t data)
389 1.1 christos {
390 1.1 christos char *extra_opts = NULL;
391 1.1 christos char *tmp_opts = NULL;
392 1.1 christos char *sub_type = NULL;
393 1.1 christos char *loopdev = NULL;
394 1.1 christos int noauto = 0;
395 1.1 christos int errorcode;
396 1.1 christos
397 1.1 christos sub_type = hasmnteq(mnt, "type");
398 1.1 christos if (sub_type) {
399 1.1 christos sub_type = strdup(sub_type);
400 1.1 christos if (sub_type) { /* the strdup malloc might have failed */
401 1.1 christos type = strpbrk(sub_type, ",:;\n\t");
402 1.1 christos if (type == NULL)
403 1.1 christos type = MOUNT_TYPE_UFS;
404 1.1 christos else {
405 1.1 christos *type = '\0';
406 1.1 christos type = sub_type;
407 1.1 christos }
408 1.1 christos } else {
409 1.1 christos plog(XLOG_ERROR, "strdup returned null in mount_linux_nonfs");
410 1.1 christos }
411 1.1 christos }
412 1.1 christos
413 1.1 christos if (!hasmntopt(mnt, "type"))
414 1.1 christos mnt->mnt_type = type;
415 1.1 christos
416 1.1 christos tmp_opts = parse_opts(type, mnt->mnt_opts, &flags, &extra_opts, &noauto);
417 1.1 christos
418 1.1 christos #ifdef MOUNT_TYPE_LOFS
419 1.1 christos if (STREQ(type, MOUNT_TYPE_LOFS)) {
420 1.1 christos # ifndef MNT2_GEN_OPT_BIND
421 1.1 christos size_t l;
422 1.1 christos /* this is basically a hack to support fist lofs */
423 1.1 christos XFREE(extra_opts);
424 1.1 christos l = strlen(mnt->mnt_fsname) + sizeof("dir=") + 1;
425 1.1 christos extra_opts = (char *) xmalloc(l);
426 1.1 christos xsnprintf(extra_opts, l, sizeof(extra_opts), "dir=%s", mnt->mnt_fsname);
427 1.1 christos # else /* MNT2_GEN_OPT_BIND */
428 1.1 christos /* use bind mounts for lofs */
429 1.1 christos flags |= MNT2_GEN_OPT_BIND;
430 1.1 christos # endif /* MNT2_GEN_OPT_BIND */
431 1.1 christos errorcode = do_mount_linux(type, mnt, flags, extra_opts);
432 1.1 christos } else /* end of "if type is LOFS" */
433 1.1 christos #endif /* MOUNT_TYPE_LOFS */
434 1.1 christos
435 1.1 christos {
436 1.1 christos #ifdef HAVE_LOOP_DEVICE
437 1.1 christos /*
438 1.1 christos * If the mounted "device" is actually a regular file,
439 1.1 christos # try to attach a loop device to it.
440 1.1 christos */
441 1.1 christos struct stat buf;
442 1.1 christos char *old_fsname = NULL;
443 1.1 christos if (stat(mnt->mnt_fsname, &buf) == 0 &&
444 1.1 christos S_ISREG(buf.st_mode)) {
445 1.1 christos if ((loopdev = setup_loop_device(mnt->mnt_fsname)) != NULL) {
446 1.1 christos char *str;
447 1.1 christos size_t l;
448 1.1 christos
449 1.1 christos plog(XLOG_INFO, "setup loop device %s over %s OK", loopdev, mnt->mnt_fsname);
450 1.1 christos old_fsname = mnt->mnt_fsname;
451 1.1 christos mnt->mnt_fsname = loopdev;
452 1.1 christos /* XXX: hack, append loop=/dev/loopX to mnttab opts */
453 1.1 christos l = strlen(mnt->mnt_opts) + 7 + strlen(loopdev);
454 1.1 christos str = (char *) xmalloc(l);
455 1.1 christos if (str) {
456 1.1 christos xsnprintf(str, l, "%s,loop=%s", mnt->mnt_opts, loopdev);
457 1.1 christos XFREE(mnt->mnt_opts);
458 1.1 christos mnt->mnt_opts = str;
459 1.1 christos }
460 1.1 christos } else {
461 1.1 christos plog(XLOG_ERROR, "failed to set up a loop device: %m");
462 1.1 christos errorcode = 1;
463 1.1 christos goto out;
464 1.1 christos }
465 1.1 christos }
466 1.1 christos #endif /* HAVE_LOOP_DEVICE */
467 1.1 christos
468 1.1 christos errorcode = do_mount_linux(type, mnt, flags, extra_opts);
469 1.1 christos
470 1.1 christos #ifdef HAVE_LOOP_DEVICE
471 1.1 christos /* if mount failed and we used a loop device, then undo it */
472 1.1 christos if (errorcode != 0 && loopdev != NULL) {
473 1.1 christos if (delete_loop_device(loopdev) < 0)
474 1.1 christos plog(XLOG_WARNING, "mount() failed to release loop device %s: %m", loopdev);
475 1.1 christos else
476 1.1 christos plog(XLOG_INFO, "mount() released loop device %s OK", loopdev);
477 1.1 christos }
478 1.1 christos if (old_fsname)
479 1.1 christos mnt->mnt_fsname = old_fsname;
480 1.1 christos #endif /* HAVE_LOOP_DEVICE */
481 1.1 christos }
482 1.1 christos
483 1.1 christos /*
484 1.1 christos * Free all allocated space and return errorcode.
485 1.1 christos */
486 1.1 christos out:
487 1.1 christos if (loopdev)
488 1.1 christos XFREE(loopdev);
489 1.1 christos if (extra_opts != NULL)
490 1.1 christos XFREE(extra_opts);
491 1.1 christos if (tmp_opts != NULL)
492 1.1 christos XFREE(tmp_opts);
493 1.1 christos if (sub_type != NULL)
494 1.1 christos XFREE(sub_type);
495 1.1 christos return errorcode;
496 1.1 christos }
497 1.1 christos
498 1.1 christos
499 1.1 christos int
500 1.1 christos mount_linux(MTYPE_TYPE type, mntent_t *mnt, int flags, caddr_t data)
501 1.1 christos {
502 1.1 christos int errorcode;
503 1.1 christos
504 1.1 christos if (mnt->mnt_opts && STREQ(mnt->mnt_opts, "defaults"))
505 1.1 christos mnt->mnt_opts = NULL;
506 1.1 christos
507 1.1 christos if (type == NULL)
508 1.1 christos type = index(mnt->mnt_fsname, ':') ? MOUNT_TYPE_NFS : MOUNT_TYPE_UFS;
509 1.1 christos
510 1.1 christos if (STREQ(type, MOUNT_TYPE_NFS))
511 1.1 christos errorcode = mount_linux_nfs(type, mnt, flags, data);
512 1.1 christos else /* non-NFS mounts */
513 1.1 christos errorcode = mount_linux_nonfs(type, mnt, flags, data);
514 1.1 christos
515 1.1 christos return errorcode;
516 1.1 christos }
517 1.1 christos
518 1.1 christos
519 1.1 christos /****************************************************************************/
520 1.1 christos /*
521 1.1 christos * NFS error numbers and Linux errno's are two different things! Linux is
522 1.1 christos * `worse' than other OSes in the respect that it loudly complains about
523 1.1 christos * undefined NFS return value ("bad NFS return value.."). So we should
524 1.1 christos * translate ANY possible Linux errno to their NFS equivalent. Just, there
525 1.1 christos * aren't much NFS numbers, so most go to EINVAL or EIO. The mapping below
526 1.1 christos * should fit at least for Linux/i386 and Linux/68k. I haven't checked
527 1.1 christos * other architectures yet.
528 1.1 christos */
529 1.1 christos
530 1.1 christos #define NE_PERM 1
531 1.1 christos #define NE_NOENT 2
532 1.1 christos #define NE_IO 5
533 1.1 christos #define NE_NXIO 6
534 1.1 christos #define NE_AGAIN 11
535 1.1 christos #define NE_ACCES 13
536 1.1 christos #define NE_EXIST 17
537 1.1 christos #define NE_NODEV 19
538 1.1 christos #define NE_NOTDIR 20
539 1.1 christos #define NE_ISDIR 21
540 1.1 christos #define NE_INVAL 22
541 1.1 christos #define NE_FBIG 27
542 1.1 christos #define NE_NOSPC 28
543 1.1 christos #define NE_ROFS 30
544 1.1 christos #define NE_OPNOTSUPP 45
545 1.1 christos #define NE_NAMETOOLONG 63
546 1.1 christos #define NE_NOTEMPTY 66
547 1.1 christos #define NE_DQUOT 69
548 1.1 christos #define NE_STALE 70
549 1.1 christos #define NE_REMOTE 71
550 1.1 christos
551 1.1 christos #define NFS_LOMAP 0
552 1.1 christos #define NFS_HIMAP 122
553 1.1 christos
554 1.1 christos /*
555 1.1 christos * The errno's below are correct for Linux/i386. One day, somebody
556 1.1 christos * with lots of energy ought to verify them against the other ports...
557 1.1 christos */
558 1.1 christos static int nfs_errormap[] = {
559 1.1 christos 0, /* success(0) */
560 1.1 christos NE_PERM, /* EPERM (1) */
561 1.1 christos NE_NOENT, /* ENOENT (2) */
562 1.1 christos NE_INVAL, /* ESRCH (3) */
563 1.1 christos NE_IO, /* EINTR (4) */
564 1.1 christos NE_IO, /* EIO (5) */
565 1.1 christos NE_NXIO, /* ENXIO (6) */
566 1.1 christos NE_INVAL, /* E2BIG (7) */
567 1.1 christos NE_INVAL, /* ENOEXEC (8) */
568 1.1 christos NE_INVAL, /* EBADF (9) */
569 1.1 christos NE_IO, /* ECHILD (10) */
570 1.1 christos NE_AGAIN, /* EAGAIN (11) */
571 1.1 christos NE_IO, /* ENOMEM (12) */
572 1.1 christos NE_ACCES, /* EACCES (13) */
573 1.1 christos NE_INVAL, /* EFAULT (14) */
574 1.1 christos NE_INVAL, /* ENOTBLK (15) */
575 1.1 christos NE_IO, /* EBUSY (16) */
576 1.1 christos NE_EXIST, /* EEXIST (17) */
577 1.1 christos NE_INVAL, /* EXDEV (18) */
578 1.1 christos NE_NODEV, /* ENODEV (19) */
579 1.1 christos NE_NOTDIR, /* ENOTDIR (20) */
580 1.1 christos NE_ISDIR, /* EISDIR (21) */
581 1.1 christos NE_INVAL, /* EINVAL (22) */
582 1.1 christos NE_IO, /* ENFILE (23) */
583 1.1 christos NE_IO, /* EMFILE (24) */
584 1.1 christos NE_INVAL, /* ENOTTY (25) */
585 1.1 christos NE_ACCES, /* ETXTBSY (26) */
586 1.1 christos NE_FBIG, /* EFBIG (27) */
587 1.1 christos NE_NOSPC, /* ENOSPC (28) */
588 1.1 christos NE_INVAL, /* ESPIPE (29) */
589 1.1 christos NE_ROFS, /* EROFS (30) */
590 1.1 christos NE_INVAL, /* EMLINK (31) */
591 1.1 christos NE_INVAL, /* EPIPE (32) */
592 1.1 christos NE_INVAL, /* EDOM (33) */
593 1.1 christos NE_INVAL, /* ERANGE (34) */
594 1.1 christos NE_INVAL, /* EDEADLK (35) */
595 1.1 christos NE_NAMETOOLONG, /* ENAMETOOLONG (36) */
596 1.1 christos NE_INVAL, /* ENOLCK (37) */
597 1.1 christos NE_INVAL, /* ENOSYS (38) */
598 1.1 christos NE_NOTEMPTY, /* ENOTEMPTY (39) */
599 1.1 christos NE_INVAL, /* ELOOP (40) */
600 1.1 christos NE_INVAL, /* unused (41) */
601 1.1 christos NE_INVAL, /* ENOMSG (42) */
602 1.1 christos NE_INVAL, /* EIDRM (43) */
603 1.1 christos NE_INVAL, /* ECHRNG (44) */
604 1.1 christos NE_INVAL, /* EL2NSYNC (45) */
605 1.1 christos NE_INVAL, /* EL3HLT (46) */
606 1.1 christos NE_INVAL, /* EL3RST (47) */
607 1.1 christos NE_INVAL, /* ELNRNG (48) */
608 1.1 christos NE_INVAL, /* EUNATCH (49) */
609 1.1 christos NE_INVAL, /* ENOCSI (50) */
610 1.1 christos NE_INVAL, /* EL2HLT (51) */
611 1.1 christos NE_INVAL, /* EBADE (52) */
612 1.1 christos NE_INVAL, /* EBADR (53) */
613 1.1 christos NE_INVAL, /* EXFULL (54) */
614 1.1 christos NE_INVAL, /* ENOANO (55) */
615 1.1 christos NE_INVAL, /* EBADRQC (56) */
616 1.1 christos NE_INVAL, /* EBADSLT (57) */
617 1.1 christos NE_INVAL, /* unused (58) */
618 1.1 christos NE_INVAL, /* EBFONT (59) */
619 1.1 christos NE_INVAL, /* ENOSTR (60) */
620 1.1 christos NE_INVAL, /* ENODATA (61) */
621 1.1 christos NE_INVAL, /* ETIME (62) */
622 1.1 christos NE_INVAL, /* ENOSR (63) */
623 1.1 christos NE_INVAL, /* ENONET (64) */
624 1.1 christos NE_INVAL, /* ENOPKG (65) */
625 1.1 christos NE_INVAL, /* EREMOTE (66) */
626 1.1 christos NE_INVAL, /* ENOLINK (67) */
627 1.1 christos NE_INVAL, /* EADV (68) */
628 1.1 christos NE_INVAL, /* ESRMNT (69) */
629 1.1 christos NE_IO, /* ECOMM (70) */
630 1.1 christos NE_IO, /* EPROTO (71) */
631 1.1 christos NE_IO, /* EMULTIHOP (72) */
632 1.1 christos NE_IO, /* EDOTDOT (73) */
633 1.1 christos NE_INVAL, /* EBADMSG (74) */
634 1.1 christos NE_INVAL, /* EOVERFLOW (75) */
635 1.1 christos NE_INVAL, /* ENOTUNIQ (76) */
636 1.1 christos NE_INVAL, /* EBADFD (77) */
637 1.1 christos NE_IO, /* EREMCHG (78) */
638 1.1 christos NE_IO, /* ELIBACC (79) */
639 1.1 christos NE_IO, /* ELIBBAD (80) */
640 1.1 christos NE_IO, /* ELIBSCN (81) */
641 1.1 christos NE_IO, /* ELIBMAX (82) */
642 1.1 christos NE_IO, /* ELIBEXEC (83) */
643 1.1 christos NE_INVAL, /* EILSEQ (84) */
644 1.1 christos NE_INVAL, /* ERESTART (85) */
645 1.1 christos NE_INVAL, /* ESTRPIPE (86) */
646 1.1 christos NE_INVAL, /* EUSERS (87) */
647 1.1 christos NE_INVAL, /* ENOTSOCK (88) */
648 1.1 christos NE_INVAL, /* EDESTADDRREQ (89) */
649 1.1 christos NE_INVAL, /* EMSGSIZE (90) */
650 1.1 christos NE_INVAL, /* EPROTOTYPE (91) */
651 1.1 christos NE_INVAL, /* ENOPROTOOPT (92) */
652 1.1 christos NE_INVAL, /* EPROTONOSUPPORT (93) */
653 1.1 christos NE_INVAL, /* ESOCKTNOSUPPORT (94) */
654 1.1 christos NE_INVAL, /* EOPNOTSUPP (95) */
655 1.1 christos NE_INVAL, /* EPFNOSUPPORT (96) */
656 1.1 christos NE_INVAL, /* EAFNOSUPPORT (97) */
657 1.1 christos NE_INVAL, /* EADDRINUSE (98) */
658 1.1 christos NE_INVAL, /* EADDRNOTAVAIL (99) */
659 1.1 christos NE_IO, /* ENETDOWN (100) */
660 1.1 christos NE_IO, /* ENETUNREACH (101) */
661 1.1 christos NE_IO, /* ENETRESET (102) */
662 1.1 christos NE_IO, /* ECONNABORTED (103) */
663 1.1 christos NE_IO, /* ECONNRESET (104) */
664 1.1 christos NE_IO, /* ENOBUFS (105) */
665 1.1 christos NE_IO, /* EISCONN (106) */
666 1.1 christos NE_IO, /* ENOTCONN (107) */
667 1.1 christos NE_IO, /* ESHUTDOWN (108) */
668 1.1 christos NE_IO, /* ETOOMANYREFS (109) */
669 1.1 christos NE_IO, /* ETIMEDOUT (110) */
670 1.1 christos NE_IO, /* ECONNREFUSED (111) */
671 1.1 christos NE_IO, /* EHOSTDOWN (112) */
672 1.1 christos NE_IO, /* EHOSTUNREACH (113) */
673 1.1 christos NE_IO, /* EALREADY (114) */
674 1.1 christos NE_IO, /* EINPROGRESS (115) */
675 1.1 christos NE_STALE, /* ESTALE (116) */
676 1.1 christos NE_IO, /* EUCLEAN (117) */
677 1.1 christos NE_INVAL, /* ENOTNAM (118) */
678 1.1 christos NE_INVAL, /* ENAVAIL (119) */
679 1.1 christos NE_INVAL, /* EISNAM (120) */
680 1.1 christos NE_IO, /* EREMOTEIO (121) */
681 1.1 christos NE_DQUOT, /* EDQUOT (122) */
682 1.1 christos };
683 1.1 christos
684 1.1 christos
685 1.1 christos int
686 1.1 christos linux_nfs_error(int e)
687 1.1 christos {
688 1.1 christos int ret = (nfsstat) NE_IO;
689 1.1 christos
690 1.1 christos if (e < NFS_LOMAP || e > NFS_HIMAP)
691 1.1 christos ret = (nfsstat) NE_IO;
692 1.1 christos else
693 1.1 christos ret = nfs_errormap[e - NFS_LOMAP];
694 1.1 christos dlog("linux_nfs_error: map error %d to NFS error %d", e, ret);
695 1.1 christos return (nfsstat) ret;
696 1.1 christos }
697 1.1 christos
698 1.1 christos
699 1.1 christos #ifdef HAVE_LOOP_DEVICE
700 1.1 christos /****************************************************************************/
701 1.1 christos /*** LOOP DEVICE SUPPORT ***/
702 1.1 christos /*** Loop Device setup code taken from mount-2.11g-5.src.rpm, which was ***/
703 1.1 christos /*** originally written bt Ted T'so and others. ***/
704 1.1 christos /****************************************************************************/
705 1.1 christos
706 1.1 christos #define PROC_DEVICES "/proc/devices"
707 1.1 christos
708 1.1 christos #if not_used_yet
709 1.1 christos static int
710 1.1 christos show_loop(char *device)
711 1.1 christos {
712 1.1 christos struct loop_info loopinfo;
713 1.1 christos int fd;
714 1.1 christos
715 1.1 christos if ((fd = open(device, O_RDONLY)) < 0) {
716 1.1 christos dlog("loop: can't open device %s: %m", device);
717 1.1 christos return -2;
718 1.1 christos }
719 1.1 christos if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) < 0) {
720 1.1 christos dlog("loop: can't get info on device %s: %m", device);
721 1.1 christos close(fd);
722 1.1 christos return -1;
723 1.1 christos }
724 1.1 christos dlog("show_loop: %s: [%04x]:%ld (%s)",
725 1.1 christos device, loopinfo.lo_device, loopinfo.lo_inode,
726 1.1 christos loopinfo.lo_name);
727 1.1 christos
728 1.1 christos close(fd);
729 1.1 christos
730 1.1 christos return 0;
731 1.1 christos }
732 1.1 christos
733 1.1 christos
734 1.1 christos static int
735 1.1 christos is_loop_device(const char *device)
736 1.1 christos {
737 1.1 christos struct stat statbuf;
738 1.1 christos int loopmajor = 7;
739 1.1 christos
740 1.1 christos return (loopmajor && stat(device, &statbuf) == 0 &&
741 1.1 christos S_ISBLK(statbuf.st_mode) &&
742 1.1 christos (statbuf.st_rdev>>8) == loopmajor);
743 1.1 christos }
744 1.1 christos #endif /* not_used_yet */
745 1.1 christos
746 1.1 christos
747 1.1 christos /*
748 1.1 christos * Just creating a device, say in /tmp, is probably a bad idea - people
749 1.1 christos * might have problems with backup or so. So, we just try /dev/loop[0-7].
750 1.1 christos */
751 1.1 christos static char *
752 1.1 christos find_unused_loop_device(void)
753 1.1 christos {
754 1.1 christos char dev[20];
755 1.1 christos char *loop_formats[] = { "/dev/loop%d", "/dev/loop/%d" };
756 1.1 christos int i, j, fd, somedev = 0, someloop = 0, loop_known = 0;
757 1.1 christos struct stat statbuf;
758 1.1 christos struct loop_info loopinfo;
759 1.1 christos FILE *procdev;
760 1.1 christos
761 1.1 christos #define LOOP_FMT_SIZE(a) (sizeof(a)/sizeof(a[0]))
762 1.1 christos for (j = 0; j < (int) LOOP_FMT_SIZE(loop_formats); j++) {
763 1.1 christos for (i = 0; i < 256; i++) {
764 1.1 christos xsnprintf(dev, sizeof(dev), loop_formats[j], i);
765 1.1 christos if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
766 1.1 christos somedev++;
767 1.1 christos fd = open(dev, O_RDONLY);
768 1.1 christos if (fd >= 0) {
769 1.1 christos if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == 0)
770 1.1 christos someloop++; /* in use */
771 1.1 christos else if (errno == ENXIO) {
772 1.1 christos close(fd);
773 1.1 christos return strdup(dev); /* probably free */
774 1.1 christos }
775 1.1 christos close(fd);
776 1.1 christos }
777 1.1 christos continue; /* continue trying as long as devices exist */
778 1.1 christos }
779 1.1 christos break;
780 1.1 christos }
781 1.1 christos }
782 1.1 christos
783 1.1 christos /* Nothing found. Why not? */
784 1.1 christos if ((procdev = fopen(PROC_DEVICES, "r")) != NULL) {
785 1.1 christos char line[100];
786 1.1 christos while (fgets(line, sizeof(line), procdev))
787 1.1 christos if (strstr(line, " loop\n")) {
788 1.1 christos loop_known = 1;
789 1.1 christos break;
790 1.1 christos }
791 1.1 christos fclose(procdev);
792 1.1 christos if (!loop_known)
793 1.1 christos loop_known = -1;
794 1.1 christos }
795 1.1 christos
796 1.1 christos if (!somedev) {
797 1.1 christos dlog("Could not find any device /dev/loop#");
798 1.1 christos } else if (!someloop) {
799 1.1 christos if (loop_known == 1) {
800 1.1 christos dlog("Could not find any loop device.");
801 1.1 christos dlog("...Maybe /dev/loop# has a wrong major number?");
802 1.1 christos }
803 1.1 christos else if (loop_known == -1) {
804 1.1 christos dlog("Could not find any loop device, and, according to %s,", PROC_DEVICES);
805 1.1 christos dlog("...this kernel does not know about the loop device.");
806 1.1 christos dlog("... (If so, then recompile or `insmod loop.o'.)");
807 1.1 christos } else {
808 1.1 christos dlog("Could not find any loop device. Maybe this kernel does not know,");
809 1.1 christos dlog("...about the loop device (then recompile or `insmod loop.o'), or");
810 1.1 christos dlog("...maybe /dev/loop# has the wrong major number?");
811 1.1 christos }
812 1.1 christos } else {
813 1.1 christos dlog("Could not find any free loop device!");
814 1.1 christos }
815 1.1 christos return NULL;
816 1.1 christos }
817 1.1 christos
818 1.1 christos
819 1.1 christos /* returns 0 if OK, -1 otherwise */
820 1.1 christos char *
821 1.1 christos setup_loop_device(const char *file)
822 1.1 christos {
823 1.1 christos struct loop_info loopinfo;
824 1.1 christos int fd, ffd, mode, err = -1;
825 1.1 christos char *device = find_unused_loop_device();
826 1.1 christos
827 1.1 christos if (!device) {
828 1.1 christos dlog("no unused loop device");
829 1.1 christos goto out;
830 1.1 christos }
831 1.1 christos
832 1.1 christos mode = O_RDWR | O_LARGEFILE;
833 1.1 christos if ((ffd = open(file, mode)) < 0) {
834 1.1 christos if (errno == EROFS) {
835 1.1 christos mode = O_RDONLY | O_LARGEFILE;
836 1.1 christos ffd = open(file, mode);
837 1.1 christos }
838 1.1 christos if (ffd < 0) {
839 1.1 christos dlog("%s: %m", file);
840 1.1 christos goto out;
841 1.1 christos }
842 1.1 christos }
843 1.1 christos if ((fd = open(device, mode)) < 0) {
844 1.1 christos dlog("%s: %m", device);
845 1.1 christos goto out_close;
846 1.1 christos }
847 1.1 christos
848 1.1 christos memset(&loopinfo, 0, sizeof(loopinfo));
849 1.1 christos xstrlcpy(loopinfo.lo_name, file, LO_NAME_SIZE);
850 1.1 christos loopinfo.lo_offset = 0;
851 1.1 christos
852 1.1 christos if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
853 1.1 christos dlog("ioctl: LOOP_SET_FD: %m");
854 1.1 christos goto out_close_all;
855 1.1 christos }
856 1.1 christos if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
857 1.1 christos (void) ioctl(fd, LOOP_CLR_FD, 0);
858 1.1 christos dlog("ioctl: LOOP_SET_STATUS: %m");
859 1.1 christos goto out_close_all;
860 1.1 christos }
861 1.1 christos
862 1.1 christos /* if gets here, all is OK */
863 1.1 christos err = 0;
864 1.1 christos
865 1.1 christos out_close_all:
866 1.1 christos close(fd);
867 1.1 christos out_close:
868 1.1 christos close(ffd);
869 1.1 christos out:
870 1.1 christos
871 1.1 christos if (err) {
872 1.1 christos XFREE(device);
873 1.1 christos return NULL;
874 1.1 christos } else {
875 1.1 christos dlog("setup_loop_device(%s,%s): success", device, file);
876 1.1 christos return device;
877 1.1 christos }
878 1.1 christos }
879 1.1 christos
880 1.1 christos
881 1.1 christos int
882 1.1 christos delete_loop_device(const char *device)
883 1.1 christos {
884 1.1 christos int fd;
885 1.1 christos
886 1.1 christos if ((fd = open(device, O_RDONLY)) < 0) {
887 1.1 christos dlog("delete_loop_device: can't delete device %s: %m", device);
888 1.1 christos return -1;
889 1.1 christos }
890 1.1 christos if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
891 1.1 christos dlog("ioctl: LOOP_CLR_FD: %m");
892 1.1 christos return -1;
893 1.1 christos }
894 1.1 christos close(fd);
895 1.1 christos dlog("delete_loop_device(%s): success", device);
896 1.1 christos return 0;
897 1.1 christos }
898 1.1 christos #endif /* HAVE_LOOP_DEVICE */
899 1.1 christos
900 1.1 christos
901 1.1 christos /****************************************************************************/
902