Whamcloud - gitweb
c32c56516ea62ae970824c00dca5e9b611707e6c
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #if HAVE_CONFIG_H
33 #  include "config.h"
34 #endif /* HAVE_CONFIG_H */
35
36 #include <inttypes.h>
37 #include <limits.h>
38 #include <mntent.h>
39 #include <stdio.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <config.h>
44 #include <linux/lustre/lustre_ver.h>
45 #include <sys/mount.h>
46 #include <sys/stat.h>
47 #include <sys/utsname.h>
48 #include <linux/loop.h>
49 #include <sys/types.h>
50 #include <dirent.h>
51 #include <dlfcn.h>
52 #include <linux/lustre/lustre_cfg.h>
53 #include <dirent.h>
54 #include <sys/types.h>
55 #include <sys/xattr.h>
56
57 #ifdef HAVE_GSS
58 #include <keyutils.h>
59 #include <lustre/utils/gss/sk_utils.h>
60 #endif
61
62 #include "mount_utils.h"
63
64 #define vprint(fmt, arg...) if (verbose > 0) printf(fmt, ##arg)
65 #define verrprint(fmt, arg...) if (verbose >= 0) fprintf(stderr, fmt, ##arg)
66
67 #ifdef HAVE_SERVER_SUPPORT
68 static struct module_backfs_ops *backfs_ops[LDD_MT_LAST];
69 #endif
70
71 void fatal(void)
72 {
73         verbose = 0;
74         fprintf(stderr, "\n%s FATAL: ", progname);
75 }
76
77 int run_command(char *cmd, int cmdsz)
78 {
79         char log[] = "/tmp/run_command_logXXXXXX";
80         int fd = -1, rc;
81
82         if ((cmdsz - strlen(cmd)) < 6) {
83                 fatal();
84                 fprintf(stderr, "Command buffer overflow: %.*s...\n",
85                         cmdsz, cmd);
86                 return ENOMEM;
87         }
88
89         if (verbose > 1) {
90                 printf("cmd: %s\n", cmd);
91         } else {
92                 if ((fd = mkstemp(log)) >= 0) {
93                         close(fd);
94                         strcat(cmd, " >");
95                         strcat(cmd, log);
96                 }
97         }
98         strcat(cmd, " 2>&1");
99
100         /* Can't use popen because we need the rv of the command */
101         rc = system(cmd);
102         if (rc && (fd >= 0)) {
103                 char buf[128];
104                 FILE *fp;
105
106                 fp = fopen(log, "r");
107                 if (fp) {
108                         while (fgets(buf, sizeof(buf), fp))
109                                 printf("   %s", buf);
110                         fclose(fp);
111                 }
112         }
113         if (fd >= 0)
114                 remove(log);
115         return rc;
116 }
117
118 #ifdef HAVE_SERVER_SUPPORT
119 int add_param(char *buf, char *key, char *val)
120 {
121         int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
122         int start = strlen(buf);
123         int keylen = 0;
124
125         if (key)
126                 keylen = strlen(key);
127         if (start + 1 + keylen + strlen(val) >= end) {
128                 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
129                         progname, buf, key ? key : "", val);
130                 return 1;
131         }
132
133         sprintf(buf + start, " %s%s", key ? key : "", val);
134         return 0;
135 }
136
137 int get_param(char *buf, char *key, char **val)
138 {
139         int i, key_len = strlen(key);
140         char *ptr;
141
142         ptr = strstr(buf, key);
143         if (ptr) {
144                 *val = strdup(ptr + key_len);
145                 if (!(*val))
146                         return ENOMEM;
147
148                 for (i = 0; i < strlen(*val); i++)
149                         if (((*val)[i] == ' ') || ((*val)[i] == '\0'))
150                                 break;
151
152                 (*val)[i] = '\0';
153                 return 0;
154         }
155
156         return ENOENT;
157 }
158
159 int append_param(char *buf, char *key, char *val, char sep)
160 {
161         int key_len, i, offset, old_val_len;
162         char *ptr = NULL, str[1024];
163
164         if (key)
165                 ptr = strstr(buf, key);
166
167         /* key doesn't exist yet, so just add it */
168         if (!ptr)
169                 return add_param(buf, key, val);
170
171         key_len = strlen(key);
172
173         /* Copy previous values to str */
174         for (i = 0; i < sizeof(str); ++i) {
175                 if ((ptr[i + key_len] == ' ') || (ptr[i + key_len] == '\0'))
176                         break;
177                 str[i] = ptr[i + key_len];
178         }
179         if (i == sizeof(str))
180                 return E2BIG;
181         old_val_len = i;
182
183         offset = old_val_len + key_len;
184
185         /* Move rest of buf to overwrite previous key and value */
186         for (i = 0; ptr[i + offset] != '\0'; ++i)
187                 ptr[i] = ptr[i + offset];
188
189         ptr[i] = '\0';
190
191         snprintf(str + old_val_len, sizeof(str) - old_val_len,
192                  "%c%s", sep, val);
193
194         return add_param(buf, key, str);
195 }
196 #endif
197
198 char *strscat(char *dst, char *src, int buflen)
199 {
200         dst[buflen - 1] = 0;
201         if (strlen(dst) + strlen(src) >= buflen) {
202                 fprintf(stderr,
203                         "string buffer overflow (max %d): '%s' + '%s'\n",
204                         buflen, dst, src);
205                 exit(EOVERFLOW);
206         }
207         return strcat(dst, src);
208 }
209
210 char *strscpy(char *dst, char *src, int buflen)
211 {
212         dst[0] = 0;
213         return strscat(dst, src, buflen);
214 }
215
216 int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
217 {
218         FILE *fp;
219         struct mntent *mnt;
220
221         fp = setmntent(MOUNTED, "r");
222         if (!fp)
223                 return 0;
224
225         while ((mnt = getmntent(fp)) != NULL) {
226                 if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
227                      strcmp(mnt->mnt_fsname, spec2) == 0) &&
228                     (!mtpt || strcmp(mnt->mnt_dir, mtpt) == 0) &&
229                     (!type || strcmp(mnt->mnt_type, type) == 0)) {
230                         endmntent(fp);
231                         return EEXIST;
232                 }
233         }
234         endmntent(fp);
235
236         return 0;
237 }
238
239 #include <sys/vfs.h>
240 #include <linux/magic.h>
241
242 static int mtab_is_proc(const char *mtab)
243 {
244         struct statfs s;
245
246         if (statfs(mtab, &s) < 0)
247                 return 0;
248
249         return (s.f_type == PROC_SUPER_MAGIC);
250 }
251
252 #ifdef HAVE_LIBMOUNT
253
254 # include <libmount/libmount.h>
255
256 /*
257  * The libmount is part of util-linux since 2.18.
258  * We use it to update utab to avoid umount would
259  * blocked in some rare case.
260  */
261 int update_utab_entry(struct mount_opts *mop)
262 {
263         struct libmnt_fs *fs = mnt_new_fs();
264         struct libmnt_update *upd;
265         int rc;
266
267         mnt_fs_set_source(fs, mop->mo_source);
268         mnt_fs_set_target(fs, mop->mo_target);
269         mnt_fs_set_fstype(fs, "lustre");
270         mnt_fs_set_attributes(fs, "lustre");
271
272         upd = mnt_new_update();
273         if (!upd)
274                 return -ENOMEM;
275
276         rc = mnt_update_set_fs(upd, mop->mo_nomtab ? MS_REMOUNT : 0, NULL, fs);
277         if (rc == 1) /* update is unnecessary */
278                 rc = 0;
279         if (rc) {
280                 fprintf(stderr,
281                         "error: failed to save utab entry: rc = %d\n", rc);
282         } else {
283                 rc = mnt_update_table(upd, NULL);
284         }
285
286         mnt_free_update(upd);
287         mnt_free_fs(fs);
288
289         return rc;
290 }
291 #else
292 int update_utab_entry(struct mount_opts *mop)
293 {
294         return 0;
295 }
296 #endif /* HAVE_LIBMOUNT */
297
298 int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
299                       int flags, int freq, int pass)
300 {
301         FILE *fp;
302         struct mntent mnt;
303         int rc = 0;
304
305         /* Don't update mtab if it is linked to any file in /proc direcotry.*/
306         if (mtab_is_proc(MOUNTED))
307                 return 0;
308
309         mnt.mnt_fsname = spec;
310         mnt.mnt_dir = mtpt;
311         mnt.mnt_type = type;
312         mnt.mnt_opts = opts ? opts : "";
313         mnt.mnt_freq = freq;
314         mnt.mnt_passno = pass;
315
316         fp = setmntent(MOUNTED, "a+");
317         if (!fp) {
318                 fprintf(stderr, "%s: setmntent(%s): %s\n",
319                         progname, MOUNTED, strerror(errno));
320                 rc = 16;
321         } else {
322                 if ((addmntent(fp, &mnt)) == 1) {
323                         fprintf(stderr, "%s: addmntent: %s\n",
324                                 progname, strerror(errno));
325                         rc = 16;
326                 }
327                 endmntent(fp);
328         }
329
330         return rc;
331 }
332
333 /* Search for opt in mntlist, returning true if found.
334  */
335 static int in_mntlist(char *opt, char *mntlist)
336 {
337         char *ml, *mlp, *item, *ctx = NULL;
338
339         if (!(ml = strdup(mntlist))) {
340                 fprintf(stderr, "%s: out of memory\n", progname);
341                 exit(1);
342         }
343         mlp = ml;
344         while ((item = strtok_r(mlp, ",", &ctx))) {
345                 if (!strcmp(opt, item))
346                         break;
347                 mlp = NULL;
348         }
349         free(ml);
350         return (item != NULL);
351 }
352
353 /* Issue a message on stderr for every item in wanted_mountopts that is not
354  * present in mountopts.  The justwarn boolean toggles between error and
355  * warning message.  Return an error count.
356  */
357 int check_mountfsoptions(char *mountopts, char *wanted_mountopts)
358 {
359         char *ml, *mlp, *item, *ctx = NULL;
360         int errors = 0;
361
362         if (!(ml = strdup(wanted_mountopts))) {
363                 fprintf(stderr, "%s: out of memory\n", progname);
364                 exit(1);
365         }
366         mlp = ml;
367         while ((item = strtok_r(mlp, ",", &ctx))) {
368                 if (!in_mntlist(item, mountopts)) {
369                         fprintf(stderr, "%s: Error: mandatory mount option"
370                                 " '%s' is missing\n", progname, item);
371                         errors++;
372                 }
373                 mlp = NULL;
374         }
375         free(ml);
376         return errors;
377 }
378
379 /* Trim embedded white space, leading and trailing commas from string s.
380  */
381 void trim_mountfsoptions(char *s)
382 {
383         char *p;
384
385         for (p = s; *p; ) {
386                 if (isspace(*p)) {
387                         memmove(p, p + 1, strlen(p + 1) + 1);
388                         continue;
389                 }
390                 p++;
391         }
392
393         while (s[0] == ',')
394                 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
395
396         p = s + strlen(s) - 1;
397         while (p >= s && *p == ',')
398                 *p-- = '\0';
399 }
400
401 #ifdef HAVE_SERVER_SUPPORT
402 /* Setup a file in the first unused loop_device */
403 int loop_setup(struct mkfs_opts *mop)
404 {
405         char loop_base[20];
406         char l_device[64];
407         int i, ret = 0;
408
409         /* Figure out the loop device names */
410         if (!access("/dev/loop0", F_OK | R_OK) ||
411             !access("/dev/loop-control", F_OK | R_OK)) {
412                 strcpy(loop_base, "/dev/loop\0");
413         } else if (!access("/dev/loop/0", F_OK | R_OK)) {
414                 strcpy(loop_base, "/dev/loop/\0");
415         } else {
416                 fprintf(stderr, "%s: can't access loop devices\n", progname);
417                 return EACCES;
418         }
419
420         /* Find unused loop device */
421         for (i = 0; i < MAX_LOOP_DEVICES; i++) {
422                 char cmd[PATH_MAX];
423                 int cmdsz = sizeof(cmd);
424
425                 ret = open("/dev/loop-control", O_RDWR);
426                 if (ret < 0) {
427                         fprintf(stderr, "%s: can't access loop control\n",
428                                 progname);
429                         return EACCES;
430                 }
431                 /* find or allocate a free loop device to use */
432                 i = ioctl(ret, LOOP_CTL_GET_FREE);
433                 close(ret);
434                 if (i < 0) {
435                         fprintf(stderr, "%s: access loop control error\n",
436                                 progname);
437                         return EACCES;
438                 }
439                 sprintf(l_device, "%s%d", loop_base, i);
440
441                 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
442                 ret = system(cmd);
443
444                 /* losetup gets 1 (ret=256) for non-set-up device */
445                 if (ret) {
446                         /* Set up a loopback device to our file */
447                         snprintf(cmd, cmdsz, "losetup %s %s", l_device,
448                                  mop->mo_device);
449                         ret = run_command(cmd, cmdsz);
450                         if (ret == 256)
451                                 /*
452                                  * someone else picked up this loop device
453                                  * behind our back
454                                  */
455                                 continue;
456                         if (ret) {
457                                 fprintf(stderr, "%s: error %d on losetup: %s\n",
458                                         progname, ret,
459                                         ret >= 0 ? strerror(ret) : "");
460                                 return ret;
461                         }
462                         strscpy(mop->mo_loopdev, l_device,
463                                 sizeof(mop->mo_loopdev));
464                         return ret;
465                 }
466         }
467
468         fprintf(stderr, "%s: out of loop devices!\n", progname);
469         return EMFILE;
470 }
471
472 int loop_cleanup(struct mkfs_opts *mop)
473 {
474         char cmd[150];
475         int ret = 0;
476
477         if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
478                 int tries;
479
480                 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
481                 for (tries = 0; tries < 3; tries++) {
482                         ret = run_command(cmd, sizeof(cmd));
483                         if (ret == 0)
484                                 break;
485                         sleep(1);
486                 }
487         }
488
489         if (ret != 0)
490                 fprintf(stderr, "cannot cleanup %s: rc = %d\n",
491                         mop->mo_loopdev, ret);
492         return ret;
493 }
494
495 int loop_format(struct mkfs_opts *mop)
496 {
497         int fd;
498
499         if (mop->mo_device_kb == 0) {
500                 fatal();
501                 fprintf(stderr,
502                         "loop device requires a --device-size= param\n");
503                 return EINVAL;
504         }
505
506         fd = creat(mop->mo_device, 0600);
507         if (fd < 0) {
508                 fatal();
509                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
510                         progname, strerror(errno));
511                 return errno;
512         }
513
514         if (ftruncate(fd, mop->mo_device_kb * 1024) != 0) {
515                 close(fd);
516                 fatal();
517                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
518                         progname, strerror(errno));
519                 return errno;
520         }
521
522         close(fd);
523         return 0;
524 }
525
526 #ifdef PLUGIN_DIR
527 #define DLSYM(prefix, sym, func)                                        \
528         do {                                                            \
529                 char _fname[64];                                        \
530                 snprintf(_fname, sizeof(_fname), "%s_%s", prefix, #func); \
531                 sym->func = (typeof(sym->func))dlsym(sym->dl_handle, _fname); \
532         } while (0)
533 #endif /* PLUGIN_DIR */
534
535 /**
536  * Load plugin for a given mount_type from ${pkglibdir}/mount_osd_FSTYPE.so and
537  * return struct of function pointers (will be freed in unloack_backfs_module).
538  *
539  * \param[in] mount_type        Mount type to load module for.
540  * \retval Value of backfs_ops struct
541  * \retval NULL if no module exists
542  */
543 struct module_backfs_ops *load_backfs_module(enum ldd_mount_type mount_type)
544 {
545         struct module_backfs_ops *ops;
546 #ifdef PLUGIN_DIR
547         char *error, filename[PATH_MAX], fsname[512], *name;
548         void *handle;
549
550         /*
551          * This deals with duplicate ldd_mount_types resolving to same OSD layer
552          * plugin (e.g. ext3/ldiskfs/ldiskfs2 all being ldiskfs)
553          */
554         strncpy(fsname, mt_type(mount_type), sizeof(fsname));
555         name = fsname + sizeof("osd-") - 1;
556
557         /* change osd- to osd_ */
558         fsname[sizeof("osd-") - 2] = '_';
559
560         snprintf(filename, sizeof(filename), PLUGIN_DIR"/mount_%s.so", fsname);
561
562         handle = dlopen(filename, RTLD_LAZY);
563
564         /*
565          * Check for $LUSTRE environment variable from test-framework.
566          * This allows using locally built modules to be used.
567          */
568         if (!handle) {
569                 char *dirname;
570
571                 dirname = getenv("LUSTRE");
572                 if (dirname) {
573                         snprintf(filename, sizeof(filename),
574                                  "%s/utils/mount_%s.so",
575                                  dirname, fsname);
576                         handle = dlopen(filename, RTLD_LAZY);
577                 }
578         }
579
580         /* Do not clutter up console with missing types */
581         if (!handle)
582                 return NULL;
583
584         ops = malloc(sizeof(*ops));
585         if (!ops) {
586                 dlclose(handle);
587                 return NULL;
588         }
589
590         ops->dl_handle = handle;
591         dlerror(); /* Clear any existing error */
592
593         DLSYM(name, ops, init);
594         DLSYM(name, ops, fini);
595         DLSYM(name, ops, read_ldd);
596         DLSYM(name, ops, write_ldd);
597         DLSYM(name, ops, erase_ldd);
598         DLSYM(name, ops, print_ldd_params);
599         DLSYM(name, ops, is_lustre);
600         DLSYM(name, ops, make_lustre);
601         DLSYM(name, ops, prepare_lustre);
602         DLSYM(name, ops, tune_lustre);
603         DLSYM(name, ops, label_lustre);
604         DLSYM(name, ops, rename_fsname);
605         DLSYM(name, ops, enable_quota);
606
607         error = dlerror();
608         if (error) {
609                 fatal();
610                 fprintf(stderr, "%s\n", error);
611                 dlclose(handle);
612                 free(ops);
613                 return NULL;
614         }
615
616         /* optional methods */
617         DLSYM(name, ops, fix_mountopts);
618 #else
619         switch (mount_type) {
620 #ifdef HAVE_LDISKFS_OSD
621         case LDD_MT_LDISKFS:
622                 ops = &ldiskfs_ops;
623                 break;
624 #endif /* HAVE_LDISKFS_OSD */
625 #ifdef HAVE_ZFS_OSD
626         case LDD_MT_ZFS:
627                 ops = &zfs_ops;
628                 break;
629 #endif /* HAVE_ZFS_OSD */
630         default:
631                 ops = NULL;
632                 break;
633         }
634 #endif
635         return ops;
636 }
637
638 /**
639  * Unload plugin and free backfs_ops structure. Must be called the same number
640  * of times as load_backfs_module is.
641  */
642 void unload_backfs_module(struct module_backfs_ops *ops)
643 {
644 #ifdef PLUGIN_DIR
645         if (!ops)
646                 return;
647
648         dlclose(ops->dl_handle);
649         free(ops);
650 #endif
651 }
652
653 /* Return true if backfs_ops has operations for the given mount_type. */
654 int backfs_mount_type_okay(enum ldd_mount_type mount_type)
655 {
656         if (mount_type >= LDD_MT_LAST || mount_type < 0) {
657                 fatal();
658                 fprintf(stderr, "fs type out of range %d\n", mount_type);
659                 return 0;
660         }
661         if (!backfs_ops[mount_type]) {
662                 fatal();
663                 fprintf(stderr, "unhandled/unloaded fs type %d '%s'\n",
664                         mount_type, mt_str(mount_type));
665                 return 0;
666         }
667         return 1;
668 }
669
670 /* Write the server config files */
671 int osd_write_ldd(struct mkfs_opts *mop)
672 {
673         struct lustre_disk_data *ldd = &mop->mo_ldd;
674         int ret;
675
676         if (backfs_mount_type_okay(ldd->ldd_mount_type))
677                 ret = backfs_ops[ldd->ldd_mount_type]->write_ldd(mop);
678         else
679                 ret = EINVAL;
680
681         return ret;
682 }
683
684 /* Read the server config files */
685 int osd_read_ldd(char *dev, struct lustre_disk_data *ldd)
686 {
687         int ret;
688
689         if (backfs_mount_type_okay(ldd->ldd_mount_type))
690                 ret = backfs_ops[ldd->ldd_mount_type]->read_ldd(dev, ldd);
691         else
692                 ret = EINVAL;
693
694         return ret;
695 }
696
697 /* Erase param from the server config files */
698 int osd_erase_ldd(struct mkfs_opts *mop, char *param)
699 {
700         struct lustre_disk_data *ldd = &mop->mo_ldd;
701         int ret;
702
703         if (backfs_mount_type_okay(ldd->ldd_mount_type))
704                 ret = backfs_ops[ldd->ldd_mount_type]->erase_ldd(mop, param);
705         else
706                 ret = EINVAL;
707
708         return ret;
709 }
710
711 /* Print ldd_params */
712 void osd_print_ldd_params(struct mkfs_opts *mop)
713 {
714         struct lustre_disk_data *ldd = &mop->mo_ldd;
715
716         if (backfs_mount_type_okay(ldd->ldd_mount_type))
717                 backfs_ops[ldd->ldd_mount_type]->print_ldd_params(mop);
718 }
719
720 /* Was this device formatted for Lustre */
721 int osd_is_lustre(char *dev, unsigned int *mount_type)
722 {
723         int i;
724
725         vprint("checking for existing Lustre data: ");
726
727         for (i = 0; i < LDD_MT_LAST; ++i) {
728                 if (backfs_ops[i] &&
729                     backfs_ops[i]->is_lustre(dev, mount_type)) {
730                         vprint("found\n");
731                         return 1;
732                 }
733         }
734
735         vprint("not found\n");
736         return 0;
737 }
738
739 /* Build fs according to type */
740 int osd_make_lustre(struct mkfs_opts *mop)
741 {
742         struct lustre_disk_data *ldd = &mop->mo_ldd;
743         int ret;
744
745         if (backfs_mount_type_okay(ldd->ldd_mount_type))
746                 ret = backfs_ops[ldd->ldd_mount_type]->make_lustre(mop);
747         else
748                 ret = EINVAL;
749
750         return ret;
751 }
752
753 int osd_prepare_lustre(struct mkfs_opts *mop,
754                        char *wanted_mountopts, size_t len)
755 {
756         struct lustre_disk_data *ldd = &mop->mo_ldd;
757         int ret;
758
759         if (backfs_mount_type_okay(ldd->ldd_mount_type))
760                 ret = backfs_ops[ldd->ldd_mount_type]->prepare_lustre(mop,
761                                                         wanted_mountopts, len);
762         else
763                 ret = EINVAL;
764
765         return ret;
766 }
767
768 int osd_fix_mountopts(struct mkfs_opts *mop, char *mountopts, size_t len)
769 {
770         struct lustre_disk_data *ldd = &mop->mo_ldd;
771
772         if (!backfs_mount_type_okay(ldd->ldd_mount_type))
773                 return EINVAL;
774
775         if (!backfs_ops[ldd->ldd_mount_type]->fix_mountopts)
776                 return 0;
777
778         return backfs_ops[ldd->ldd_mount_type]->fix_mountopts(mop, mountopts,
779                                                               len);
780 }
781
782 int osd_tune_lustre(char *dev, struct mount_opts *mop)
783 {
784         struct lustre_disk_data *ldd = &mop->mo_ldd;
785         int ret;
786
787         if (backfs_mount_type_okay(ldd->ldd_mount_type))
788                 ret = backfs_ops[ldd->ldd_mount_type]->tune_lustre(dev, mop);
789         else
790                 ret = EINVAL;
791
792         return ret;
793 }
794
795 int osd_label_lustre(struct mount_opts *mop)
796 {
797         struct lustre_disk_data *ldd = &mop->mo_ldd;
798         int ret;
799
800         if (backfs_mount_type_okay(ldd->ldd_mount_type))
801                 ret = backfs_ops[ldd->ldd_mount_type]->label_lustre(mop);
802         else
803                 ret = EINVAL;
804
805         return ret;
806 }
807
808 /* Rename filesystem fsname */
809 int osd_rename_fsname(struct mkfs_opts *mop, const char *oldname)
810 {
811         struct lustre_disk_data *ldd = &mop->mo_ldd;
812         int ret;
813
814         if (backfs_mount_type_okay(ldd->ldd_mount_type))
815                 ret = backfs_ops[ldd->ldd_mount_type]->rename_fsname(mop,
816                                                                      oldname);
817         else
818                 ret = EINVAL;
819
820         return ret;
821 }
822
823 /* Enable quota accounting */
824 int osd_enable_quota(struct mkfs_opts *mop)
825 {
826         struct lustre_disk_data *ldd = &mop->mo_ldd;
827         int ret;
828
829         if (backfs_mount_type_okay(ldd->ldd_mount_type))
830                 ret = backfs_ops[ldd->ldd_mount_type]->enable_quota(mop);
831         else
832                 ret = EINVAL;
833
834         return ret;
835 }
836
837 int osd_init(void)
838 {
839         int i, rc, ret = EINVAL;
840
841         for (i = 0; i < LDD_MT_LAST; ++i) {
842                 rc = 0;
843                 backfs_ops[i] = load_backfs_module(i);
844                 if (backfs_ops[i]) {
845                         rc = backfs_ops[i]->init();
846                         if (rc != 0) {
847                                 backfs_ops[i]->fini();
848                                 unload_backfs_module(backfs_ops[i]);
849                                 backfs_ops[i] = NULL;
850                         } else {
851                                 ret = 0;
852                         }
853                 }
854         }
855
856         return ret;
857 }
858
859 void osd_fini(void)
860 {
861         int i;
862
863         for (i = 0; i < LDD_MT_LAST; ++i) {
864                 if (backfs_ops[i]) {
865                         backfs_ops[i]->fini();
866                         unload_backfs_module(backfs_ops[i]);
867                         backfs_ops[i] = NULL;
868                 }
869         }
870 }
871
872 __u64 get_device_size(char *device)
873 {
874         int ret, fd;
875         __u64 size = 0;
876
877         fd = open(device, O_RDONLY);
878         if (fd < 0) {
879                 fprintf(stderr, "%s: cannot open %s: %s\n",
880                         progname, device, strerror(errno));
881                 return 0;
882         }
883
884 #ifdef BLKGETSIZE64
885         /* size in bytes. bz5831 */
886         ret = ioctl(fd, BLKGETSIZE64, (void *)&size);
887 #else
888         {
889                 __u32 lsize = 0;
890                 /* size in blocks */
891                 ret = ioctl(fd, BLKGETSIZE, (void *)&lsize);
892                 size = (__u64)lsize * 512;
893         }
894 #endif
895         close(fd);
896         if (ret < 0) {
897                 fprintf(stderr, "%s: size ioctl failed: %s\n",
898                         progname, strerror(errno));
899                 return 0;
900         }
901
902         vprint("device size = %juMB\n", (uintmax_t)(size >> 20));
903         /* return value in KB */
904         return size >> 10;
905 }
906 #endif
907
908 int file_create(char *path, __u64 size)
909 {
910         __u64 size_max;
911         int ret;
912         int fd;
913
914         /*
915          * Since "size" is in KB, the file offset it represents could overflow
916          * off_t.
917          */
918         size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
919         if (size >= size_max) {
920                 fprintf(stderr,
921                         "%s: %ju KB: Backing store size must be smaller than %ju KB\n",
922                         progname, (uintmax_t)size, (uintmax_t)size_max);
923                 return EFBIG;
924         }
925
926         ret = access(path, F_OK);
927         if (ret == 0) {
928                 ret = unlink(path);
929                 if (ret != 0)
930                         return errno;
931         }
932
933         fd = creat(path, 0600);
934         if (fd < 0) {
935                 fatal();
936                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
937                         progname, strerror(errno));
938                 return errno;
939         }
940
941         ret = ftruncate(fd, size * 1024);
942         close(fd);
943         if (ret != 0) {
944                 fatal();
945                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
946                         progname, strerror(errno));
947                 return errno;
948         }
949
950         return 0;
951 }
952
953 #ifdef HAVE_SERVER_SUPPORT
954 struct lustre_cfg_entry {
955         struct list_head lce_list;
956         char             lce_name[0];
957 };
958
959 static struct lustre_cfg_entry *lustre_cfg_entry_init(const char *name)
960 {
961         struct lustre_cfg_entry *lce;
962         int len = strlen(name) + 1;
963
964         lce = malloc(sizeof(*lce) + len);
965         if (lce) {
966                 INIT_LIST_HEAD(&lce->lce_list);
967                 memcpy(lce->lce_name, name, len);
968         }
969
970         return lce;
971 }
972
973 static void lustre_cfg_entry_fini(struct lustre_cfg_entry *lce)
974 {
975         free(lce);
976 }
977
978 int lustre_rename_fsname(struct mkfs_opts *mop, const char *mntpt,
979                          const char *oldname)
980 {
981         struct lustre_disk_data *ldd = &mop->mo_ldd;
982         struct lr_server_data lsd;
983         char filepnm[132];
984         char cfg_dir[128];
985         DIR *dir = NULL;
986         struct dirent64 *dirent;
987         struct lustre_cfg_entry *lce;
988         struct list_head cfg_list;
989         int old_namelen = strlen(oldname);
990         int new_namelen = strlen(ldd->ldd_fsname);
991         int ret;
992         int fd;
993
994         INIT_LIST_HEAD(&cfg_list);
995
996         snprintf(filepnm, sizeof(filepnm), "%s/%s", mntpt, LAST_RCVD);
997         fd = open(filepnm, O_RDWR);
998         if (fd < 0) {
999                 if (errno == ENOENT)
1000                         goto config;
1001
1002                 if (errno != 0)
1003                         ret = errno;
1004                 else
1005                         ret = fd;
1006                 fprintf(stderr, "Unable to open %s: %s\n",
1007                         filepnm, strerror(ret));
1008                 return ret;
1009         }
1010
1011         ret = read(fd, &lsd, sizeof(lsd));
1012         if (ret != sizeof(lsd)) {
1013                 if (errno != 0)
1014                         ret = errno;
1015                 fprintf(stderr, "Unable to read %s: %s\n",
1016                         filepnm, strerror(ret));
1017                 close(fd);
1018                 return ret;
1019         }
1020
1021         ret = lseek(fd, 0, SEEK_SET);
1022         if (ret) {
1023                 if (errno != 0)
1024                         ret = errno;
1025                 fprintf(stderr, "Unable to lseek %s: %s\n",
1026                         filepnm, strerror(ret));
1027                 close(fd);
1028                 return ret;
1029         }
1030
1031         /* replace fsname in lr_server_data::lsd_uuid. */
1032         if (old_namelen > new_namelen)
1033                 memmove(lsd.lsd_uuid + new_namelen,
1034                         lsd.lsd_uuid + old_namelen,
1035                         sizeof(lsd.lsd_uuid) - old_namelen);
1036         else if (old_namelen < new_namelen)
1037                 memmove(lsd.lsd_uuid + new_namelen,
1038                         lsd.lsd_uuid + old_namelen,
1039                         sizeof(lsd.lsd_uuid) - new_namelen);
1040         memcpy(lsd.lsd_uuid, ldd->ldd_fsname, new_namelen);
1041         ret = write(fd, &lsd, sizeof(lsd));
1042         if (ret != sizeof(lsd)) {
1043                 if (errno != 0)
1044                         ret = errno;
1045                 fprintf(stderr, "Unable to write %s: %s\n",
1046                         filepnm, strerror(ret));
1047                 close(fd);
1048                 return ret;
1049         }
1050
1051         close(fd);
1052
1053 config:
1054         snprintf(cfg_dir, sizeof(cfg_dir), "%s/%s", mntpt, MOUNT_CONFIGS_DIR);
1055         dir = opendir(cfg_dir);
1056         if (!dir) {
1057                 if (errno != 0)
1058                         ret = errno;
1059                 else
1060                         ret = EINVAL;
1061                 fprintf(stderr, "Unable to opendir %s: %s\n",
1062                         cfg_dir, strerror(ret));
1063                 return ret;
1064         }
1065
1066         while ((dirent = readdir64(dir)) != NULL) {
1067                 char *ptr;
1068
1069                 if (strlen(dirent->d_name) <= old_namelen)
1070                         continue;
1071
1072                 ptr = strrchr(dirent->d_name, '-');
1073                 if (!ptr || (ptr - dirent->d_name) != old_namelen)
1074                         continue;
1075
1076                 if (strncmp(dirent->d_name, oldname, old_namelen) != 0)
1077                         continue;
1078
1079                 lce = lustre_cfg_entry_init(dirent->d_name);
1080                 if (!lce) {
1081                         if (errno != 0)
1082                                 ret = errno;
1083                         else
1084                                 ret = EINVAL;
1085
1086                         fprintf(stderr, "Fail to init item for %s: %s\n",
1087                                 dirent->d_name, strerror(ret));
1088                         goto out;
1089                 }
1090
1091                 list_add(&lce->lce_list, &cfg_list);
1092         }
1093
1094         closedir(dir);
1095         dir = NULL;
1096         ret = 0;
1097
1098         while (!list_empty(&cfg_list) && ret == 0) {
1099                 lce = list_entry(cfg_list.next, struct lustre_cfg_entry,
1100                                  lce_list);
1101                 list_del(&lce->lce_list);
1102                 snprintf(filepnm, sizeof(filepnm), "%s/%s", cfg_dir,
1103                          lce->lce_name);
1104                 if (IS_MGS(ldd))
1105                         /*
1106                          * Store the new fsname in the XATTR_TARGET_RENAME EA.
1107                          * When the MGS start, it will scan config logs, and
1108                          * for the ones which have the XATTR_TARGET_RENAME EA,
1109                          * it will replace old fsname with the new fsname in
1110                          * the config log by some shared kernel level config
1111                          * logs {fork,erase} functionalities automatically.
1112                          */
1113                         ret = setxattr(filepnm, XATTR_TARGET_RENAME,
1114                                        ldd->ldd_fsname,
1115                                        strlen(ldd->ldd_fsname), 0);
1116                 else
1117                         ret = unlink(filepnm);
1118
1119                 if (ret) {
1120                         if (errno != 0)
1121                                 ret = errno;
1122
1123                         fprintf(stderr, "Fail to %s %s: %s\n",
1124                                 IS_MGS(ldd) ? "setxattr" : "unlink",
1125                                 filepnm, strerror(ret));
1126                 }
1127
1128                 lustre_cfg_entry_fini(lce);
1129         }
1130
1131 out:
1132         if (dir)
1133                 closedir(dir);
1134
1135         while (!list_empty(&cfg_list)) {
1136                 lce = list_entry(cfg_list.next, struct lustre_cfg_entry,
1137                                  lce_list);
1138                 list_del(&lce->lce_list);
1139                 lustre_cfg_entry_fini(lce);
1140         }
1141
1142         return ret;
1143 }
1144 #endif /* HAVE_SERVER_SUPPORT */
1145
1146 #ifdef HAVE_GSS
1147 #ifdef HAVE_OPENSSL_SSK
1148 int load_shared_keys(struct mount_opts *mop)
1149 {
1150         DIR *dir;
1151         struct dirent *dentry;
1152         struct stat sbuf;
1153         char fullpath[PATH_MAX];
1154         char *path = mop->mo_skpath;
1155         int rc;
1156
1157         /* init logging */
1158         sk_init_logging(NULL, 1, 1);
1159
1160         rc = stat(path, &sbuf);
1161         if (rc < 0) {
1162                 fprintf(stderr, "stat() failed for key %s: %s\n", path,
1163                         strerror(errno));
1164                 return -errno;
1165         }
1166
1167         /* Load individual keys or a directory of them */
1168         if (S_ISREG(sbuf.st_mode)) {
1169                 return sk_load_keyfile(path);
1170         } else if (!S_ISDIR(sbuf.st_mode)) {
1171                 fprintf(stderr, "Invalid shared key path: %s\n", path);
1172                 return -ENOKEY;
1173         }
1174
1175         dir = opendir(path);
1176         if (!dir) {
1177                 fprintf(stderr, "Unable to open shared key directory: %s\n",
1178                         path);
1179                 return -ENOENT;
1180         }
1181
1182         /*
1183          * Loop through the files in the directory attempting to load them.
1184          * Any issue with loading the keyfile is treated as an error although
1185          * the loop continues until all files have been attempted.  This will
1186          * allow all errors be reported at once rather then requiring
1187          * incremental corrections to fix each one and try again.
1188          */
1189         while ((dentry = readdir(dir)) != NULL) {
1190                 if (strcmp(".", dentry->d_name) == 0 ||
1191                     strcmp("..", dentry->d_name) == 0)
1192                         continue;
1193
1194                 rc = snprintf(fullpath, PATH_MAX, "%s/%s", path,
1195                               dentry->d_name);
1196                 if (rc >= PATH_MAX) {
1197                         fprintf(stderr, "Path too long for %s/%s\n",
1198                                 path, dentry->d_name);
1199                         rc = -ENAMETOOLONG;
1200                         continue;
1201                 }
1202
1203                 rc = stat(fullpath, &sbuf);
1204                 if (rc < 0) {
1205                         fprintf(stderr, "Unable to stat %s: %s\n", fullpath,
1206                                 strerror(errno));
1207                         rc = -errno;
1208                         continue;
1209                 }
1210
1211                 if (!S_ISREG(sbuf.st_mode))
1212                         continue;
1213
1214                 rc = sk_load_keyfile(fullpath);
1215                 if (rc)
1216                         fprintf(stderr, "Failed to load key %s\n", fullpath);
1217         }
1218         closedir(dir);
1219
1220         return rc;
1221 }
1222 #endif /* HAVE_OPENSSL_SSK */
1223 #endif /* HAVE_GSS */