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