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