Whamcloud - gitweb
LU-1581 utils: move common funcs to mount_utils.c
[fs/lustre-release.git] / lustre / utils / mount_utils.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  * Copyright (c) 2012 Whamcloud, Inc.
30  */
31 /*
32  * This file is part of Lustre, http://www.lustre.org/
33  * Lustre is a trademark of Sun Microsystems, Inc.
34  */
35
36 #if HAVE_CONFIG_H
37 #  include "config.h"
38 #endif /* HAVE_CONFIG_H */
39
40 #include <stdio.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <config.h>
44 #include <lustre_disk.h>
45 #include <lustre_ver.h>
46 #include <sys/stat.h>
47 #include <sys/utsname.h>
48 #include "mount_utils.h"
49
50 extern char *progname;
51 extern int verbose;
52
53 #define vprint(fmt, arg...) if (verbose > 0) printf(fmt, ##arg)
54 #define verrprint(fmt, arg...) if (verbose >= 0) fprintf(stderr, fmt, ##arg)
55
56 void fatal(void)
57 {
58         verbose = 0;
59         fprintf(stderr, "\n%s FATAL: ", progname);
60 }
61
62 int run_command(char *cmd, int cmdsz)
63 {
64         char log[] = "/tmp/run_command_logXXXXXX";
65         int fd = -1, rc;
66
67         if ((cmdsz - strlen(cmd)) < 6) {
68                 fatal();
69                 fprintf(stderr, "Command buffer overflow: %.*s...\n",
70                         cmdsz, cmd);
71                 return ENOMEM;
72         }
73
74         if (verbose > 1) {
75                 printf("cmd: %s\n", cmd);
76         } else {
77                 if ((fd = mkstemp(log)) >= 0) {
78                         close(fd);
79                         strcat(cmd, " >");
80                         strcat(cmd, log);
81                 }
82         }
83         strcat(cmd, " 2>&1");
84
85         /* Can't use popen because we need the rv of the command */
86         rc = system(cmd);
87         if (rc && (fd >= 0)) {
88                 char buf[128];
89                 FILE *fp;
90                 fp = fopen(log, "r");
91                 if (fp) {
92                         while (fgets(buf, sizeof(buf), fp) != NULL) {
93                                 printf("   %s", buf);
94                         }
95                         fclose(fp);
96                 }
97         }
98         if (fd >= 0)
99                 remove(log);
100         return rc;
101 }
102
103 int add_param(char *buf, char *key, char *val)
104 {
105         int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
106         int start = strlen(buf);
107         int keylen = 0;
108
109         if (key)
110                 keylen = strlen(key);
111         if (start + 1 + keylen + strlen(val) >= end) {
112                 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
113                         progname, buf, key ? key : "", val);
114                 return 1;
115         }
116
117         sprintf(buf + start, " %s%s", key ? key : "", val);
118         return 0;
119 }
120
121 int get_param(char *buf, char *key, char **val)
122 {
123         int i, key_len = strlen(key);
124         char *ptr;
125
126         ptr = strstr(buf, key);
127         if (ptr) {
128                 *val = strdup(ptr + key_len);
129                 if (*val == NULL)
130                         return ENOMEM;
131
132                 for (i = 0; i < strlen(*val); i++)
133                         if (((*val)[i] == ' ') || ((*val)[i] == '\0'))
134                                 break;
135
136                 (*val)[i] = '\0';
137                 return 0;
138         }
139
140         return ENOENT;
141 }
142
143 char *strscat(char *dst, char *src, int buflen)
144 {
145         dst[buflen - 1] = 0;
146         if (strlen(dst) + strlen(src) >= buflen) {
147                 fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
148                         "\n", buflen, dst, src);
149                 exit(EOVERFLOW);
150         }
151         return strcat(dst, src);
152 }
153
154 char *strscpy(char *dst, char *src, int buflen)
155 {
156         dst[0] = 0;
157         return strscat(dst, src, buflen);
158 }
159
160 static int in_mntlist(char *opt, char *mntlist)
161 {
162         char *ml, *mlp, *item, *ctx = NULL;
163
164         if (!(ml = strdup(mntlist))) {
165                 fprintf(stderr, "%s: out of memory\n", progname);
166                 exit(1);
167         }
168         mlp = ml;
169         while ((item = strtok_r(mlp, ",", &ctx))) {
170                 if (!strcmp(opt, item))
171                         break;
172                 mlp = NULL;
173         }
174         free(ml);
175         return (item != NULL);
176 }
177
178 /* Issue a message on stderr for every item in wanted_mountopts that is not
179  * present in mountopts.  The justwarn boolean toggles between error and
180  * warning message.  Return an error count.
181  */
182 int check_mountfsoptions(char *mountopts, char *wanted_mountopts,
183                          int justwarn)
184 {
185         char *ml, *mlp, *item, *ctx = NULL;
186         int errors = 0;
187
188         if (!(ml = strdup(wanted_mountopts))) {
189                 fprintf(stderr, "%s: out of memory\n", progname);
190                 exit(1);
191         }
192         mlp = ml;
193         while ((item = strtok_r(mlp, ",", &ctx))) {
194                 if (!in_mntlist(item, mountopts)) {
195                         fprintf(stderr, "%s: %s mount option `%s' is missing\n",
196                                 progname, justwarn ? "Warning: default"
197                                 : "Error: mandatory", item);
198                         errors++;
199                 }
200                 mlp = NULL;
201         }
202         free(ml);
203         return errors;
204 }
205
206 /* Trim embedded white space, leading and trailing commas from string s.
207  */
208 void trim_mountfsoptions(char *s)
209 {
210         char *p;
211
212         for (p = s; *p; ) {
213                 if (isspace(*p)) {
214                         memmove(p, p + 1, strlen(p + 1) + 1);
215                         continue;
216                 }
217                 p++;
218         }
219
220         while (s[0] == ',')
221                 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
222
223         p = s + strlen(s) - 1;
224         while (p >= s && *p == ',')
225                 *p-- = '\0';
226 }
227
228 /* Setup a file in the first unused loop_device */
229 int loop_setup(struct mkfs_opts *mop)
230 {
231         char loop_base[20];
232         char l_device[64];
233         int i, ret = 0;
234
235         /* Figure out the loop device names */
236         if (!access("/dev/loop0", F_OK | R_OK)) {
237                 strcpy(loop_base, "/dev/loop\0");
238         } else if (!access("/dev/loop/0", F_OK | R_OK)) {
239                 strcpy(loop_base, "/dev/loop/\0");
240         } else {
241                 fprintf(stderr, "%s: can't access loop devices\n", progname);
242                 return EACCES;
243         }
244
245         /* Find unused loop device */
246         for (i = 0; i < MAX_LOOP_DEVICES; i++) {
247                 char cmd[PATH_MAX];
248                 int cmdsz = sizeof(cmd);
249
250                 sprintf(l_device, "%s%d", loop_base, i);
251                 if (access(l_device, F_OK | R_OK))
252                         break;
253                 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
254                 ret = system(cmd);
255
256                 /* losetup gets 1 (ret=256) for non-set-up device */
257                 if (ret) {
258                         /* Set up a loopback device to our file */
259                         snprintf(cmd, cmdsz, "losetup %s %s", l_device,
260                                  mop->mo_device);
261                         ret = run_command(cmd, cmdsz);
262                         if (ret == 256)
263                                 /* someone else picked up this loop device
264                                  * behind our back */
265                                 continue;
266                         if (ret) {
267                                 fprintf(stderr, "%s: error %d on losetup: %s\n",
268                                         progname, ret, strerror(ret));
269                                 return ret;
270                         }
271                         strscpy(mop->mo_loopdev, l_device,
272                                 sizeof(mop->mo_loopdev));
273                         return ret;
274                 }
275         }
276
277         fprintf(stderr, "%s: out of loop devices!\n", progname);
278         return EMFILE;
279 }
280
281 int loop_cleanup(struct mkfs_opts *mop)
282 {
283         char cmd[150];
284         int ret = 1;
285         if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
286                 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
287                 ret = run_command(cmd, sizeof(cmd));
288         }
289         return ret;
290 }
291
292 int loop_format(struct mkfs_opts *mop)
293 {
294         int fd;
295
296         if (mop->mo_device_sz == 0) {
297                 fatal();
298                 fprintf(stderr, "loop device requires a --device-size= "
299                         "param\n");
300                 return EINVAL;
301         }
302
303         fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
304         if (fd < 0) {
305                 fatal();
306                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
307                         progname, strerror(errno));
308                 return errno;
309         }
310
311         if (ftruncate(fd, mop->mo_device_sz * 1024) != 0) {
312                 close(fd);
313                 fatal();
314                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
315                         progname, strerror(errno));
316                 return errno;
317         }
318
319         close(fd);
320         return 0;
321 }
322
323 __u64 get_device_size(char* device)
324 {
325         int ret, fd;
326         __u64 size = 0;
327
328         fd = open(device, O_RDONLY);
329         if (fd < 0) {
330                 fprintf(stderr, "%s: cannot open %s: %s\n",
331                         progname, device, strerror(errno));
332                 return 0;
333         }
334
335 #ifdef BLKGETSIZE64
336         /* size in bytes. bz5831 */
337         ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
338 #else
339         {
340                 __u32 lsize = 0;
341                 /* size in blocks */
342                 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
343                 size = (__u64)lsize * 512;
344         }
345 #endif
346         close(fd);
347         if (ret < 0) {
348                 fprintf(stderr, "%s: size ioctl failed: %s\n",
349                         progname, strerror(errno));
350                 return 0;
351         }
352
353         vprint("device size = "LPU64"MB\n", size >> 20);
354         /* return value in KB */
355         return size >> 10;
356 }
357
358 int file_create(char *path, int size)
359 {
360         int ret;
361         int fd;
362
363         ret = access(path, F_OK);
364         if (ret == 0) {
365                 ret = unlink(path);
366                 if (ret != 0)
367                         return errno;
368         }
369
370         fd = creat(path, S_IRUSR|S_IWUSR);
371         if (fd < 0) {
372                 fatal();
373                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
374                         progname, strerror(errno));
375                 return errno;
376         }
377
378         ret = ftruncate(fd, size * 1024);
379         close(fd);
380         if (ret != 0) {
381                 fatal();
382                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
383                         progname, strerror(errno));
384                 return errno;
385         }
386
387         return 0;
388 }