Whamcloud - gitweb
LU-4606 utils: Mount uses so for backing fstype
[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, 2013, Intel Corporation.
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 "mount_utils.h"
41 #include <stdio.h>
42 #include <errno.h>
43 #include <string.h>
44 #include <config.h>
45 #include <lustre_disk.h>
46 #include <lustre_ver.h>
47 #include <sys/stat.h>
48 #include <sys/utsname.h>
49 #include <dlfcn.h>
50
51 extern char *progname;
52 extern int verbose;
53
54 #define vprint(fmt, arg...) if (verbose > 0) printf(fmt, ##arg)
55 #define verrprint(fmt, arg...) if (verbose >= 0) fprintf(stderr, fmt, ##arg)
56
57 static struct module_backfs_ops *backfs_ops[LDD_MT_LAST];
58
59 void fatal(void)
60 {
61         verbose = 0;
62         fprintf(stderr, "\n%s FATAL: ", progname);
63 }
64
65 int run_command(char *cmd, int cmdsz)
66 {
67         char log[] = "/tmp/run_command_logXXXXXX";
68         int fd = -1, rc;
69
70         if ((cmdsz - strlen(cmd)) < 6) {
71                 fatal();
72                 fprintf(stderr, "Command buffer overflow: %.*s...\n",
73                         cmdsz, cmd);
74                 return ENOMEM;
75         }
76
77         if (verbose > 1) {
78                 printf("cmd: %s\n", cmd);
79         } else {
80                 if ((fd = mkstemp(log)) >= 0) {
81                         close(fd);
82                         strcat(cmd, " >");
83                         strcat(cmd, log);
84                 }
85         }
86         strcat(cmd, " 2>&1");
87
88         /* Can't use popen because we need the rv of the command */
89         rc = system(cmd);
90         if (rc && (fd >= 0)) {
91                 char buf[128];
92                 FILE *fp;
93                 fp = fopen(log, "r");
94                 if (fp) {
95                         while (fgets(buf, sizeof(buf), fp) != NULL) {
96                                 printf("   %s", buf);
97                         }
98                         fclose(fp);
99                 }
100         }
101         if (fd >= 0)
102                 remove(log);
103         return rc;
104 }
105
106 int add_param(char *buf, char *key, char *val)
107 {
108         int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
109         int start = strlen(buf);
110         int keylen = 0;
111
112         if (key)
113                 keylen = strlen(key);
114         if (start + 1 + keylen + strlen(val) >= end) {
115                 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
116                         progname, buf, key ? key : "", val);
117                 return 1;
118         }
119
120         sprintf(buf + start, " %s%s", key ? key : "", val);
121         return 0;
122 }
123
124 int get_param(char *buf, char *key, char **val)
125 {
126         int i, key_len = strlen(key);
127         char *ptr;
128
129         ptr = strstr(buf, key);
130         if (ptr) {
131                 *val = strdup(ptr + key_len);
132                 if (*val == NULL)
133                         return ENOMEM;
134
135                 for (i = 0; i < strlen(*val); i++)
136                         if (((*val)[i] == ' ') || ((*val)[i] == '\0'))
137                                 break;
138
139                 (*val)[i] = '\0';
140                 return 0;
141         }
142
143         return ENOENT;
144 }
145
146 char *strscat(char *dst, char *src, int buflen)
147 {
148         dst[buflen - 1] = 0;
149         if (strlen(dst) + strlen(src) >= buflen) {
150                 fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
151                         "\n", buflen, dst, src);
152                 exit(EOVERFLOW);
153         }
154         return strcat(dst, src);
155 }
156
157 char *strscpy(char *dst, char *src, int buflen)
158 {
159         dst[0] = 0;
160         return strscat(dst, src, buflen);
161 }
162
163 int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
164 {
165         FILE *fp;
166         struct mntent *mnt;
167
168         fp = setmntent(MOUNTED, "r");
169         if (fp == NULL)
170                 return 0;
171
172         while ((mnt = getmntent(fp)) != NULL) {
173                 if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
174                      strcmp(mnt->mnt_fsname, spec2) == 0) &&
175                     (mtpt == NULL || strcmp(mnt->mnt_dir, mtpt) == 0) &&
176                     (type == NULL || strcmp(mnt->mnt_type, type) == 0)) {
177                         endmntent(fp);
178                         return(EEXIST);
179                 }
180         }
181         endmntent(fp);
182
183         return 0;
184 }
185
186 #define PROC_DIR        "/proc/"
187 static int mtab_is_proc(const char *mtab)
188 {
189         char path[16];
190
191         if (readlink(mtab, path, sizeof(path)) < 0)
192                 return 0;
193
194         if (strncmp(path, PROC_DIR, strlen(PROC_DIR)))
195                 return 0;
196
197         return 1;
198 }
199
200 int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
201                 int flags, int freq, int pass)
202 {
203         FILE *fp;
204         struct mntent mnt;
205         int rc = 0;
206
207         /* Don't update mtab if it is linked to any file in /proc direcotry.*/
208         if (mtab_is_proc(MOUNTED))
209                 return 0;
210
211         mnt.mnt_fsname = spec;
212         mnt.mnt_dir = mtpt;
213         mnt.mnt_type = type;
214         mnt.mnt_opts = opts ? opts : "";
215         mnt.mnt_freq = freq;
216         mnt.mnt_passno = pass;
217
218         fp = setmntent(MOUNTED, "a+");
219         if (fp == NULL) {
220                 fprintf(stderr, "%s: setmntent(%s): %s:",
221                         progname, MOUNTED, strerror (errno));
222                 rc = 16;
223         } else {
224                 if ((addmntent(fp, &mnt)) == 1) {
225                         fprintf(stderr, "%s: addmntent: %s:",
226                                 progname, strerror (errno));
227                         rc = 16;
228                 }
229                 endmntent(fp);
230         }
231
232         return rc;
233 }
234
235 /* Search for opt in mntlist, returning true if found.
236  */
237 static int in_mntlist(char *opt, char *mntlist)
238 {
239         char *ml, *mlp, *item, *ctx = NULL;
240
241         if (!(ml = strdup(mntlist))) {
242                 fprintf(stderr, "%s: out of memory\n", progname);
243                 exit(1);
244         }
245         mlp = ml;
246         while ((item = strtok_r(mlp, ",", &ctx))) {
247                 if (!strcmp(opt, item))
248                         break;
249                 mlp = NULL;
250         }
251         free(ml);
252         return (item != NULL);
253 }
254
255 /* Issue a message on stderr for every item in wanted_mountopts that is not
256  * present in mountopts.  The justwarn boolean toggles between error and
257  * warning message.  Return an error count.
258  */
259 int check_mountfsoptions(char *mountopts, char *wanted_mountopts,
260                          int justwarn)
261 {
262         char *ml, *mlp, *item, *ctx = NULL;
263         int errors = 0;
264
265         if (!(ml = strdup(wanted_mountopts))) {
266                 fprintf(stderr, "%s: out of memory\n", progname);
267                 exit(1);
268         }
269         mlp = ml;
270         while ((item = strtok_r(mlp, ",", &ctx))) {
271                 if (!in_mntlist(item, mountopts)) {
272                         fprintf(stderr, "%s: %s mount option `%s' is missing\n",
273                                 progname, justwarn ? "Warning: default"
274                                 : "Error: mandatory", item);
275                         errors++;
276                 }
277                 mlp = NULL;
278         }
279         free(ml);
280         return errors;
281 }
282
283 /* Trim embedded white space, leading and trailing commas from string s.
284  */
285 void trim_mountfsoptions(char *s)
286 {
287         char *p;
288
289         for (p = s; *p; ) {
290                 if (isspace(*p)) {
291                         memmove(p, p + 1, strlen(p + 1) + 1);
292                         continue;
293                 }
294                 p++;
295         }
296
297         while (s[0] == ',')
298                 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
299
300         p = s + strlen(s) - 1;
301         while (p >= s && *p == ',')
302                 *p-- = '\0';
303 }
304
305 /* Setup a file in the first unused loop_device */
306 int loop_setup(struct mkfs_opts *mop)
307 {
308         char loop_base[20];
309         char l_device[64];
310         int i, ret = 0;
311
312         /* Figure out the loop device names */
313         if (!access("/dev/loop0", F_OK | R_OK)) {
314                 strcpy(loop_base, "/dev/loop\0");
315         } else if (!access("/dev/loop/0", F_OK | R_OK)) {
316                 strcpy(loop_base, "/dev/loop/\0");
317         } else {
318                 fprintf(stderr, "%s: can't access loop devices\n", progname);
319                 return EACCES;
320         }
321
322         /* Find unused loop device */
323         for (i = 0; i < MAX_LOOP_DEVICES; i++) {
324                 char cmd[PATH_MAX];
325                 int cmdsz = sizeof(cmd);
326
327                 sprintf(l_device, "%s%d", loop_base, i);
328                 if (access(l_device, F_OK | R_OK))
329                         break;
330                 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
331                 ret = system(cmd);
332
333                 /* losetup gets 1 (ret=256) for non-set-up device */
334                 if (ret) {
335                         /* Set up a loopback device to our file */
336                         snprintf(cmd, cmdsz, "losetup %s %s", l_device,
337                                  mop->mo_device);
338                         ret = run_command(cmd, cmdsz);
339                         if (ret == 256)
340                                 /* someone else picked up this loop device
341                                  * behind our back */
342                                 continue;
343                         if (ret) {
344                                 fprintf(stderr, "%s: error %d on losetup: %s\n",
345                                         progname, ret,
346                                         ret >= 0 ? strerror(ret) : "");
347                                 return ret;
348                         }
349                         strscpy(mop->mo_loopdev, l_device,
350                                 sizeof(mop->mo_loopdev));
351                         return ret;
352                 }
353         }
354
355         fprintf(stderr, "%s: out of loop devices!\n", progname);
356         return EMFILE;
357 }
358
359 int loop_cleanup(struct mkfs_opts *mop)
360 {
361         char cmd[150];
362         int ret = 0;
363
364         if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
365                 int tries;
366
367                 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
368                 for (tries = 0; tries < 3; tries++) {
369                         ret = run_command(cmd, sizeof(cmd));
370                         if (ret == 0)
371                                 break;
372                         sleep(1);
373                 }
374         }
375
376         if (ret != 0)
377                 fprintf(stderr, "cannot cleanup %s: rc = %d\n",
378                         mop->mo_loopdev, ret);
379         return ret;
380 }
381
382 int loop_format(struct mkfs_opts *mop)
383 {
384         int fd;
385
386         if (mop->mo_device_sz == 0) {
387                 fatal();
388                 fprintf(stderr, "loop device requires a --device-size= "
389                         "param\n");
390                 return EINVAL;
391         }
392
393         fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
394         if (fd < 0) {
395                 fatal();
396                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
397                         progname, strerror(errno));
398                 return errno;
399         }
400
401         if (ftruncate(fd, mop->mo_device_sz * 1024) != 0) {
402                 close(fd);
403                 fatal();
404                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
405                         progname, strerror(errno));
406                 return errno;
407         }
408
409         close(fd);
410         return 0;
411 }
412
413 #define DLSYM(prefix, sym, func)                                        \
414         do {                                                            \
415                 char _fname[64];                                        \
416                 snprintf(_fname, sizeof(_fname), "%s_%s", prefix, #func); \
417                 sym->func = (typeof(sym->func))dlsym(sym->dl_handle, _fname); \
418         } while (0)
419
420 /**
421  * Load plugin for a given mount_type from ${pkglibdir}/mount_osd_FSTYPE.so and
422  * return struct of function pointers (will be freed in unloack_backfs_module).
423  *
424  * \param[in] mount_type        Mount type to load module for.
425  * \retval Value of backfs_ops struct
426  * \retval NULL if no module exists
427  */
428 struct module_backfs_ops *load_backfs_module(enum ldd_mount_type mount_type)
429 {
430         void *handle;
431         char *error, filename[512], fsname[512], *name;
432         struct module_backfs_ops *ops;
433
434         /* This deals with duplicate ldd_mount_types resolving to same OSD layer
435          * plugin (e.g. ext3/ldiskfs/ldiskfs2 all being ldiskfs) */
436         strncpy(fsname, mt_type(mount_type), sizeof(fsname));
437         name = fsname + sizeof("osd-") - 1;
438
439         /* change osd- to osd_ */
440         fsname[sizeof("osd-") - 2] = '_';
441
442         snprintf(filename, sizeof(filename), PLUGIN_DIR"/mount_%s.so", fsname);
443
444         handle = dlopen(filename, RTLD_LAZY);
445         if (handle == NULL)
446                 /* Do not clutter up console with missing types */
447                 return NULL;
448
449         ops = malloc(sizeof(*ops));
450         if (ops == NULL) {
451                 dlclose(handle);
452                 return NULL;
453         }
454
455         ops->dl_handle = handle;
456         dlerror(); /* Clear any existing error */
457
458         DLSYM(name, ops, init);
459         DLSYM(name, ops, fini);
460         DLSYM(name, ops, read_ldd);
461         DLSYM(name, ops, write_ldd);
462         DLSYM(name, ops, is_lustre);
463         DLSYM(name, ops, make_lustre);
464         DLSYM(name, ops, prepare_lustre);
465         DLSYM(name, ops, tune_lustre);
466         DLSYM(name, ops, label_lustre);
467         DLSYM(name, ops, enable_quota);
468
469         error = dlerror();
470         if (error != NULL) {
471                 fatal();
472                 fprintf(stderr, "%s\n", error);
473                 dlclose(handle);
474                 free(ops);
475                 return NULL;
476         }
477         return ops;
478 }
479
480 /**
481  * Unload plugin and free backfs_ops structure. Must be called the same number
482  * of times as load_backfs_module is.
483  */
484 void unload_backfs_module(struct module_backfs_ops *ops)
485 {
486         if (ops == NULL)
487                 return;
488
489         dlclose(ops->dl_handle);
490         free(ops);
491 }
492
493 /* Return true if backfs_ops has operations for the given mount_type. */
494 int backfs_mount_type_okay(enum ldd_mount_type mount_type)
495 {
496         if (unlikely(mount_type >= LDD_MT_LAST || mount_type < 0)) {
497                 fatal();
498                 fprintf(stderr, "fs type out of range %d\n", mount_type);
499                 return 0;
500         }
501         if (backfs_ops[mount_type] == NULL) {
502                 fatal();
503                 fprintf(stderr, "unhandled fs type %d '%s'\n",
504                         mount_type, mt_str(mount_type));
505                 return 0;
506         }
507         return 1;
508 }
509
510 /* Write the server config files */
511 int osd_write_ldd(struct mkfs_opts *mop)
512 {
513         struct lustre_disk_data *ldd = &mop->mo_ldd;
514         int ret;
515
516         if (backfs_mount_type_okay(ldd->ldd_mount_type))
517                 ret = backfs_ops[ldd->ldd_mount_type]->write_ldd(mop);
518
519         else
520                 ret = EINVAL;
521
522         return ret;
523 }
524
525 /* Read the server config files */
526 int osd_read_ldd(char *dev, struct lustre_disk_data *ldd)
527 {
528         int ret;
529
530         if (backfs_mount_type_okay(ldd->ldd_mount_type))
531                 ret = backfs_ops[ldd->ldd_mount_type]->read_ldd(dev, ldd);
532
533         else
534                 ret = EINVAL;
535
536         return ret;
537 }
538
539 /* Was this device formatted for Lustre */
540 int osd_is_lustre(char *dev, unsigned *mount_type)
541 {
542         int i;
543
544         vprint("checking for existing Lustre data: ");
545
546         for (i = 0; i < LDD_MT_LAST; ++i) {
547                 if (backfs_ops[i] != NULL &&
548                     backfs_ops[i]->is_lustre(dev, mount_type)) {
549                         vprint("found\n");
550                         return 1;
551                 }
552         }
553
554         vprint("not found\n");
555         return 0;
556 }
557
558 /* Build fs according to type */
559 int osd_make_lustre(struct mkfs_opts *mop)
560 {
561         struct lustre_disk_data *ldd = &mop->mo_ldd;
562         int ret;
563
564         if (backfs_mount_type_okay(ldd->ldd_mount_type))
565                 ret = backfs_ops[ldd->ldd_mount_type]->make_lustre(mop);
566
567         else
568                 ret = EINVAL;
569
570         return ret;
571 }
572
573 int osd_prepare_lustre(struct mkfs_opts *mop,
574                 char *default_mountopts, int default_len,
575                 char *always_mountopts, int always_len)
576 {
577         struct lustre_disk_data *ldd = &mop->mo_ldd;
578         int ret;
579
580         if (backfs_mount_type_okay(ldd->ldd_mount_type))
581                 ret = backfs_ops[ldd->ldd_mount_type]->prepare_lustre(mop,
582                         default_mountopts, default_len,
583                         always_mountopts, always_len);
584
585         else
586                 ret = EINVAL;
587
588         return ret;
589 }
590
591 int osd_tune_lustre(char *dev, struct mount_opts *mop)
592 {
593         struct lustre_disk_data *ldd = &mop->mo_ldd;
594         int ret;
595
596         if (backfs_mount_type_okay(ldd->ldd_mount_type))
597                 ret = backfs_ops[ldd->ldd_mount_type]->tune_lustre(dev, mop);
598
599         else
600                 ret = EINVAL;
601
602         return ret;
603 }
604
605 int osd_label_lustre(struct mount_opts *mop)
606 {
607         struct lustre_disk_data *ldd = &mop->mo_ldd;
608         int ret;
609
610         if (backfs_mount_type_okay(ldd->ldd_mount_type))
611                 ret = backfs_ops[ldd->ldd_mount_type]->label_lustre(mop);
612
613         else
614                 ret = EINVAL;
615
616         return ret;
617 }
618
619 /* Enable quota accounting */
620 int osd_enable_quota(struct mkfs_opts *mop)
621 {
622         struct lustre_disk_data *ldd = &mop->mo_ldd;
623         int ret;
624
625         if (backfs_mount_type_okay(ldd->ldd_mount_type))
626                 ret = backfs_ops[ldd->ldd_mount_type]->enable_quota(mop);
627
628         else
629                 ret = EINVAL;
630
631         return ret;
632 }
633
634 int osd_init(void)
635 {
636         int i, ret = 0;
637
638         for (i = 0; i < LDD_MT_LAST; ++i) {
639                 backfs_ops[i] = load_backfs_module(i);
640                 if (backfs_ops[i] != NULL)
641                         ret = backfs_ops[i]->init();
642                 if (ret)
643                         break;
644         }
645
646         return ret;
647 }
648
649 void osd_fini(void)
650 {
651         int i;
652
653         for (i = 0; i < LDD_MT_LAST; ++i) {
654                 if (backfs_ops[i] != NULL) {
655                         backfs_ops[i]->fini();
656                         unload_backfs_module(backfs_ops[i]);
657                         backfs_ops[i] = NULL;
658                 }
659         }
660 }
661
662 __u64 get_device_size(char* device)
663 {
664         int ret, fd;
665         __u64 size = 0;
666
667         fd = open(device, O_RDONLY);
668         if (fd < 0) {
669                 fprintf(stderr, "%s: cannot open %s: %s\n",
670                         progname, device, strerror(errno));
671                 return 0;
672         }
673
674 #ifdef BLKGETSIZE64
675         /* size in bytes. bz5831 */
676         ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
677 #else
678         {
679                 __u32 lsize = 0;
680                 /* size in blocks */
681                 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
682                 size = (__u64)lsize * 512;
683         }
684 #endif
685         close(fd);
686         if (ret < 0) {
687                 fprintf(stderr, "%s: size ioctl failed: %s\n",
688                         progname, strerror(errno));
689                 return 0;
690         }
691
692         vprint("device size = "LPU64"MB\n", size >> 20);
693         /* return value in KB */
694         return size >> 10;
695 }
696
697 int file_create(char *path, __u64 size)
698 {
699         __u64 size_max;
700         int ret;
701         int fd;
702
703         /*
704          * Since "size" is in KB, the file offset it represents could overflow
705          * off_t.
706          */
707         size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
708         if (size >= size_max) {
709                 fprintf(stderr, "%s: "LPU64" KB: Backing store size must be "
710                         "smaller than "LPU64" KB\n", progname, size, size_max);
711                 return EFBIG;
712         }
713
714         ret = access(path, F_OK);
715         if (ret == 0) {
716                 ret = unlink(path);
717                 if (ret != 0)
718                         return errno;
719         }
720
721         fd = creat(path, S_IRUSR|S_IWUSR);
722         if (fd < 0) {
723                 fatal();
724                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
725                         progname, strerror(errno));
726                 return errno;
727         }
728
729         ret = ftruncate(fd, size * 1024);
730         close(fd);
731         if (ret != 0) {
732                 fatal();
733                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
734                         progname, strerror(errno));
735                 return errno;
736         }
737
738         return 0;
739 }