]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/mount.c
Merge nfsmount.x and mount.x into mount.x
[nfs-utils.git] / utils / mount / mount.c
1 /*
2  * mount.c -- Linux NFS mount
3  *
4  * Copyright (C) 2006 Amit Gud <agud@redhat.com>
5  *
6  * - Basic code and wrapper around mount and umount code of NFS.
7  *   Based on util-linux/mount/mount.c.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  */
20
21 #include "config.h"
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <sys/mount.h>
29 #include <getopt.h>
30 #include <mntent.h>
31
32 #include "fstab.h"
33 #include "xcommon.h"
34 #include "mount_constants.h"
35 #include "nfs_paths.h"
36
37 #include "nfs_mount.h"
38 #include "nfs4_mount.h"
39 #include "nfsumount.h"
40 #include "mount.h"
41
42 char *progname;
43 int nomtab;
44 int verbose;
45 int mounttype;
46
47 static struct option longopts[] = {
48   { "fake", 0, 0, 'f' },
49   { "help", 0, 0, 'h' },
50   { "no-mtab", 0, 0, 'n' },
51   { "read-only", 0, 0, 'r' },
52   { "ro", 0, 0, 'r' },
53   { "verbose", 0, 0, 'v' },
54   { "version", 0, 0, 'V' },
55   { "read-write", 0, 0, 'w' },
56   { "rw", 0, 0, 'w' },
57   { "options", 1, 0, 'o' },
58   { "nfsvers", 1, 0, 't' },
59   { "bind", 0, 0, 128 },
60   { "replace", 0, 0, 129 },
61   { "after", 0, 0, 130 },
62   { "before", 0, 0, 131 },
63   { "over", 0, 0, 132 },
64   { "move", 0, 0, 133 },
65   { "rbind", 0, 0, 135 },
66   { NULL, 0, 0, 0 }
67 };
68
69 /* Map from -o and fstab option strings to the flag argument to mount(2).  */
70 struct opt_map {
71   const char *opt;              /* option name */
72   int  skip;                    /* skip in mtab option string */
73   int  inv;                     /* true if flag value should be inverted */
74   int  mask;                    /* flag mask value */
75 };
76
77 static const struct opt_map opt_map[] = {
78   { "defaults", 0, 0, 0         },      /* default options */
79   { "ro",       1, 0, MS_RDONLY },      /* read-only */
80   { "rw",       1, 1, MS_RDONLY },      /* read-write */
81   { "exec",     0, 1, MS_NOEXEC },      /* permit execution of binaries */
82   { "noexec",   0, 0, MS_NOEXEC },      /* don't execute binaries */
83   { "suid",     0, 1, MS_NOSUID },      /* honor suid executables */
84   { "nosuid",   0, 0, MS_NOSUID },      /* don't honor suid executables */
85   { "dev",      0, 1, MS_NODEV  },      /* interpret device files  */
86   { "nodev",    0, 0, MS_NODEV  },      /* don't interpret devices */
87   { "sync",     0, 0, MS_SYNCHRONOUS},  /* synchronous I/O */
88   { "async",    0, 1, MS_SYNCHRONOUS},  /* asynchronous I/O */
89   { "dirsync",  0, 0, MS_DIRSYNC},      /* synchronous directory modifications */
90   { "remount",  0, 0, MS_REMOUNT},      /* Alter flags of mounted FS */
91   { "bind",     0, 0, MS_BIND   },      /* Remount part of tree elsewhere */
92   { "rbind",    0, 0, MS_BIND|MS_REC }, /* Idem, plus mounted subtrees */
93
94   /* add new options here */
95 #ifdef MS_NOSUB
96   { "sub",      0, 1, MS_NOSUB  },      /* allow submounts */
97   { "nosub",    0, 0, MS_NOSUB  },      /* don't allow submounts */
98 #endif
99 #ifdef MS_SILENT
100   { "quiet",    0, 0, MS_SILENT    },   /* be quiet  */
101   { "loud",     0, 1, MS_SILENT    },   /* print out messages. */
102 #endif
103 #ifdef MS_MANDLOCK
104   { "mand",     0, 0, MS_MANDLOCK },    /* Allow mandatory locks on this FS */
105   { "nomand",   0, 1, MS_MANDLOCK },    /* Forbid mandatory locks on this FS */
106 #endif
107 #ifdef MS_NOATIME
108   { "atime",    0, 1, MS_NOATIME },     /* Update access time */
109   { "noatime",  0, 0, MS_NOATIME },     /* Do not update access time */
110 #endif
111 #ifdef MS_NODIRATIME
112   { "diratime", 0, 1, MS_NODIRATIME },  /* Update dir access times */
113   { "nodiratime", 0, 0, MS_NODIRATIME },/* Do not update dir access times */
114 #endif
115   { NULL,       0, 0, 0         }
116 };
117
118 /* Try to build a canonical options string.  */
119 static char * fix_opts_string (int flags, const char *extra_opts) {
120         const struct opt_map *om;
121         char *new_opts;
122
123         new_opts = xstrdup((flags & MS_RDONLY) ? "ro" : "rw");
124         for (om = opt_map; om->opt != NULL; om++) {
125                 if (om->skip)
126                         continue;
127                 if (om->inv || !om->mask || (flags & om->mask) != om->mask)
128                         continue;
129                 new_opts = xstrconcat3(new_opts, ",", om->opt);
130                 flags &= ~om->mask;
131         }
132         if (extra_opts && *extra_opts) {
133                 new_opts = xstrconcat3(new_opts, ",", extra_opts);
134         }
135         return new_opts;
136 }
137
138
139 int add_mtab(char *fsname, char *mount_point, char *fstype, int flags, char *opts, int freq, int passno)
140 {
141         struct mntent ment;
142         int fd;
143         FILE *mtab;
144
145         ment.mnt_fsname = fsname;
146         ment.mnt_dir = mount_point;
147         ment.mnt_type = fstype;
148         ment.mnt_opts = fix_opts_string(flags, opts);
149         ment.mnt_freq = 0;
150         ment.mnt_passno= 0;
151
152         if ((fd = open(MOUNTED"~", O_RDWR|O_CREAT|O_EXCL, 0600)) == -1) {
153                 fprintf(stderr, "Can't get "MOUNTED"~ lock file");
154                 return 1;
155         }
156         close(fd);
157
158         if ((mtab = setmntent(MOUNTED, "a+")) == NULL) {
159                 fprintf(stderr, "Can't open " MOUNTED);
160                 return 1;
161         }
162
163         if (addmntent(mtab, &ment) == 1) {
164                 fprintf(stderr, "Can't write mount entry");
165                 return 1;
166         }
167
168         if (fchmod(fileno(mtab), 0644) == -1) {
169                 fprintf(stderr, "Can't set perms on " MOUNTED);
170                 return 1;
171         }
172
173         endmntent(mtab);
174
175         if (unlink(MOUNTED"~") == -1) {
176                 fprintf(stderr, "Can't remove "MOUNTED"~");
177                 return 1;
178         }
179
180         return 0;
181 }
182
183 int do_mount_syscall(char *spec, char *node, char *type, int flags, void *data)
184 {
185         return mount(spec, node, type, flags, data);
186 }
187
188 void mount_usage()
189 {
190         printf("usage: %s remotetarget dir [-rvVwfnh] [-t version] [-o nfsoptions]\n", progname);
191         printf("options:\n\t-r\t\tMount file system readonly\n");
192         printf("\t-v\t\tVerbose\n");
193         printf("\t-V\t\tPrint version\n");
194         printf("\t-w\t\tMount file system read-write\n");
195         printf("\t-f\t\tFake mount, don't actually mount\n");
196         printf("\t-n\t\tDo not update /etc/mtab\n");
197         printf("\t-h\t\tPrint this help\n");
198         printf("\tversion\t\tnfs4 - NFS version 4, nfs - older NFS version supported\n");
199         printf("\tnfsoptions\tRefer mount.nfs(8) or nfs(5)\n\n");
200 }
201
202 static inline void
203 parse_opt(const char *opt, int *mask, char *extra_opts, int len) {
204         const struct opt_map *om;
205
206         for (om = opt_map; om->opt != NULL; om++) {
207                 if (!strcmp (opt, om->opt)) {
208                         if (om->inv)
209                                 *mask &= ~om->mask;
210                         else
211                                 *mask |= om->mask;
212                         return;
213                 }
214         }
215
216         len -= strlen(extra_opts);
217
218         if (*extra_opts && --len > 0)
219                 strcat(extra_opts, ",");
220
221         if ((len -= strlen(opt)) > 0)
222                 strcat(extra_opts, opt);
223 }
224
225 /* Take -o options list and compute 4th and 5th args to mount(2).  flags
226    gets the standard options (indicated by bits) and extra_opts all the rest */
227 static void parse_opts (const char *options, int *flags, char **extra_opts)
228 {
229         if (options != NULL) {
230                 char *opts = xstrdup(options);
231                 char *opt;
232                 int len = strlen(opts) + 20;
233
234                 *extra_opts = xmalloc(len);
235                 **extra_opts = '\0';
236
237                 for (opt = strtok(opts, ","); opt; opt = strtok(NULL, ","))
238                         parse_opt(opt, flags, *extra_opts, len);
239
240                 free(opts);
241         }
242
243 }
244
245 static void mount_error(char *node)
246 {
247         switch(errno) {
248                 case ENOTDIR:
249                         printf("%s: mount point %s is not a directory\n", progname, node);
250                         break;
251                 case EBUSY:
252                         printf("%s: %s is already mounted or busy\n", progname, node);
253                         break;
254                 case ENOENT:
255                         printf("%s: mount point %s does not exist\n", progname, node);
256                         break;
257                 default:
258                         printf("%s: %s\n", progname, strerror(errno));
259         }
260 }
261
262 int main(int argc, char *argv[])
263 {
264         int c, flags = 0, nfs_mount_vers = 0, mnt_err = 1, fake = 0;
265         char *spec, *mount_point, *extra_opts = NULL;
266         char *mount_opts = NULL, *p;
267
268         progname = argv[0];
269         if ((p = strrchr(progname, '/')) != NULL)
270                 progname = p+1;
271
272         if (getuid() != 0) {
273                 printf("%s: only root can do that.\n", progname);
274                 exit(1);
275         }
276
277         if(!strncmp(progname, "umount", strlen("umount"))) {
278                 if(argc < 2) {
279                         umount_usage();
280                         exit(1);
281                 }
282                 return(nfsumount(argc, argv));
283         }
284
285         if ((argc < 2)) {
286                 mount_usage();
287                 exit(1);
288         }
289
290         if(argv[1][0] == '-') {
291                 if(argv[1][1] == 'V')
292                         printf("%s ("PACKAGE_STRING")\n", progname);
293                 else
294                         mount_usage();
295                 return 0;
296         }
297
298         while ((c = getopt_long (argc - 2, argv + 2, "rt:vVwfno:h",
299                                 longopts, NULL)) != -1) {
300                 switch (c) {
301                 case 'r':
302                         flags |= MS_RDONLY;
303                         break;
304                 case 't':
305                         nfs_mount_vers = (strncmp(optarg, "nfs4", 4)) ? 0 : 4;
306                         break;
307                 case 'v':
308                         ++verbose;
309                         break;
310                 case 'V':
311                         printf("%s: ("PACKAGE_STRING")\n", progname);
312                         return 0;
313                 case 'w':
314                         flags &= ~MS_RDONLY;
315                         break;
316                 case 'f':
317                         ++fake;
318                         break;
319                 case 'n':
320                         ++nomtab;
321                         break;
322                 case 'o':              /* specify mount options */
323                         if (mount_opts)
324                                 mount_opts = xstrconcat3(mount_opts, ",", optarg);
325                         else
326                                 mount_opts = xstrdup(optarg);
327                         break;
328                 case 128: /* bind */
329                         mounttype = MS_BIND;
330                         break;
331                 case 129: /* replace */
332                         mounttype = MS_REPLACE;
333                         break;
334                 case 130: /* after */
335                         mounttype = MS_AFTER;
336                         break;
337                 case 131: /* before */
338                         mounttype = MS_BEFORE;
339                         break;
340                 case 132: /* over */
341                         mounttype = MS_OVER;
342                         break;
343                 case 133: /* move */
344                         mounttype = MS_MOVE;
345                         break;
346                 case 135: /* rbind */
347                         mounttype = MS_BIND | MS_REC;
348                         break;
349                 case 'h':
350                 default:
351                         mount_usage();
352                         exit(1);
353                 }
354         }
355
356         spec = argv[1];
357         mount_point = canonicalize(argv[2]);
358         
359         parse_opts(mount_opts, &flags, &extra_opts);
360
361         if (!strcmp(progname, "mount.nfs4") || nfs_mount_vers == 4) {
362                 nfs_mount_vers = 4;
363                 mnt_err = nfs4mount(spec, mount_point, &flags, &extra_opts, &mount_opts, 0);
364         }
365         else {
366                 if (!strcmp(progname, "mount.nfs")) {
367                         mnt_err = nfsmount(spec, mount_point, &flags,
368                                         &extra_opts, &mount_opts, &nfs_mount_vers, 0);
369                 }
370         }
371
372         if (!mnt_err && !fake) {
373                 mnt_err = do_mount_syscall(spec, mount_point, nfs_mount_vers == 4 ? "nfs4" : "nfs", flags, mount_opts);
374                 
375                 if(mnt_err) {
376                         mount_error(mount_point);
377                         exit(-1);
378                 }
379
380                 if(!nomtab)
381                         add_mtab(spec, mount_point, nfs_mount_vers == 4 ? "nfs4" : "nfs",
382                                  flags, extra_opts, 0, 0);
383         }
384
385         return 0;
386 }
387