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