Whamcloud - gitweb
cdc1ed6b96f9af3191cfae23d0d0b6acdd724bb9
[fs/lustre-release.git] / lustre / utils / mount_utils.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  * Copyright (c) 2012, 2013, Intel Corporation.
30  */
31 /*
32  * This file is part of Lustre, http://www.lustre.org/
33  * Lustre is a trademark of Sun Microsystems, Inc.
34  */
35
36 #if HAVE_CONFIG_H
37 #  include "config.h"
38 #endif /* HAVE_CONFIG_H */
39
40 #include "mount_utils.h"
41 #include <stdio.h>
42 #include <errno.h>
43 #include <string.h>
44 #include <config.h>
45 #include <lustre_disk.h>
46 #include <lustre_ver.h>
47 #include <sys/stat.h>
48 #include <sys/utsname.h>
49 #include <linux/loop.h>
50
51 extern char *progname;
52 extern int verbose;
53
54 #define vprint(fmt, arg...) if (verbose > 0) printf(fmt, ##arg)
55 #define verrprint(fmt, arg...) if (verbose >= 0) fprintf(stderr, fmt, ##arg)
56
57 void fatal(void)
58 {
59         verbose = 0;
60         fprintf(stderr, "\n%s FATAL: ", progname);
61 }
62
63 int run_command(char *cmd, int cmdsz)
64 {
65         char log[] = "/tmp/run_command_logXXXXXX";
66         int fd = -1, rc;
67
68         if ((cmdsz - strlen(cmd)) < 6) {
69                 fatal();
70                 fprintf(stderr, "Command buffer overflow: %.*s...\n",
71                         cmdsz, cmd);
72                 return ENOMEM;
73         }
74
75         if (verbose > 1) {
76                 printf("cmd: %s\n", cmd);
77         } else {
78                 if ((fd = mkstemp(log)) >= 0) {
79                         close(fd);
80                         strcat(cmd, " >");
81                         strcat(cmd, log);
82                 }
83         }
84         strcat(cmd, " 2>&1");
85
86         /* Can't use popen because we need the rv of the command */
87         rc = system(cmd);
88         if (rc && (fd >= 0)) {
89                 char buf[128];
90                 FILE *fp;
91                 fp = fopen(log, "r");
92                 if (fp) {
93                         while (fgets(buf, sizeof(buf), fp) != NULL) {
94                                 printf("   %s", buf);
95                         }
96                         fclose(fp);
97                 }
98         }
99         if (fd >= 0)
100                 remove(log);
101         return rc;
102 }
103
104 int add_param(char *buf, char *key, char *val)
105 {
106         char *sub_val = NULL;
107         int   buflen = strlen(buf);
108         int   end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
109         int   start = 0;
110         int   keylen = 0;
111
112         if (key != NULL)
113                 keylen = strlen(key);
114
115         start = buflen;
116         while ((sub_val = strsep(&val, ",")) != NULL) {
117                 if (*sub_val == 0)
118                         continue;
119
120                 if (start + 1 + keylen + strlen(sub_val) >= end) {
121                         fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
122                                 progname, buf, key != NULL ? key : "", sub_val);
123                         buf[buflen] = '\0';
124                         return 1;
125                 }
126
127                 sprintf(buf + start, " %s%s", key != NULL ? key : "", sub_val);
128                 start = strlen(buf);
129         }
130
131         return 0;
132 }
133
134 int get_param(char *buf, char *key, char **val)
135 {
136         int i, key_len = strlen(key);
137         char *ptr;
138
139         ptr = strstr(buf, key);
140         if (ptr) {
141                 *val = strdup(ptr + key_len);
142                 if (*val == NULL)
143                         return ENOMEM;
144
145                 for (i = 0; i < strlen(*val); i++)
146                         if (((*val)[i] == ' ') || ((*val)[i] == '\0'))
147                                 break;
148
149                 (*val)[i] = '\0';
150                 return 0;
151         }
152
153         return ENOENT;
154 }
155
156 char *strscat(char *dst, char *src, int buflen)
157 {
158         dst[buflen - 1] = 0;
159         if (strlen(dst) + strlen(src) >= buflen) {
160                 fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
161                         "\n", buflen, dst, src);
162                 exit(EOVERFLOW);
163         }
164         return strcat(dst, src);
165 }
166
167 char *strscpy(char *dst, char *src, int buflen)
168 {
169         dst[0] = 0;
170         return strscat(dst, src, buflen);
171 }
172
173 static int check_losetup(char *device, char *file_path)
174 {
175         struct loop_info64 loop_info;
176         int fd;
177         int rc;
178
179         fd = open(device, O_RDONLY);
180         if (fd < 0)
181                 return errno;
182
183         rc = ioctl(fd, LOOP_GET_STATUS64, (void *)&loop_info);
184         close(fd);
185         if (rc < 0)
186                 return errno;
187
188         return strcmp(file_path, (char *)(loop_info.lo_file_name));
189 }
190
191 int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
192 {
193         FILE *fp;
194         struct mntent *mnt;
195         char lo_dev[] = "/dev/loop";
196         int lo_dev_len = strlen(lo_dev);
197         int ret = 0;
198
199         fp = setmntent(MOUNTED, "r");
200         if (fp == NULL)
201                 return 0;
202
203         while ((mnt = getmntent(fp)) != NULL) {
204                 char *fsname = mnt->mnt_fsname;
205
206                 if ((strcmp(fsname, spec1) == 0 ||
207                      strcmp(fsname, spec2) == 0) &&
208                     (mtpt == NULL || strcmp(mnt->mnt_dir, mtpt) == 0) &&
209                     (type == NULL || strcmp(mnt->mnt_type, type) == 0)) {
210                         ret = EEXIST;
211                         break;
212                 }
213                 /* Check if the loop device is already mounted */
214                 if (strncmp(fsname, lo_dev, lo_dev_len) != 0)
215                         continue;
216                 if (check_losetup(fsname, spec1) == 0 ||
217                     check_losetup(fsname, spec2) == 0) {
218                         ret = EEXIST;
219                         break;
220                 }
221         }
222         endmntent(fp);
223
224         return ret;
225 }
226
227 #define PROC_DIR        "/proc/"
228 static int mtab_is_proc(const char *mtab)
229 {
230         char path[16];
231
232         if (readlink(mtab, path, sizeof(path)) < 0)
233                 return 0;
234
235         if (strncmp(path, PROC_DIR, strlen(PROC_DIR)))
236                 return 0;
237
238         return 1;
239 }
240
241 int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
242                 int flags, int freq, int pass)
243 {
244         FILE *fp;
245         struct mntent mnt;
246         int rc = 0;
247
248         /* Don't update mtab if it is linked to any file in /proc direcotry.*/
249         if (mtab_is_proc(MOUNTED))
250                 return 0;
251
252         mnt.mnt_fsname = spec;
253         mnt.mnt_dir = mtpt;
254         mnt.mnt_type = type;
255         mnt.mnt_opts = opts ? opts : "";
256         mnt.mnt_freq = freq;
257         mnt.mnt_passno = pass;
258
259         fp = setmntent(MOUNTED, "a+");
260         if (fp == NULL) {
261                 fprintf(stderr, "%s: setmntent(%s): %s:",
262                         progname, MOUNTED, strerror (errno));
263                 rc = 16;
264         } else {
265                 if ((addmntent(fp, &mnt)) == 1) {
266                         fprintf(stderr, "%s: addmntent: %s:",
267                                 progname, strerror (errno));
268                         rc = 16;
269                 }
270                 endmntent(fp);
271         }
272
273         return rc;
274 }
275
276 /* Search for opt in mntlist, returning true if found.
277  */
278 static int in_mntlist(char *opt, char *mntlist)
279 {
280         char *ml, *mlp, *item, *ctx = NULL;
281
282         if (!(ml = strdup(mntlist))) {
283                 fprintf(stderr, "%s: out of memory\n", progname);
284                 exit(1);
285         }
286         mlp = ml;
287         while ((item = strtok_r(mlp, ",", &ctx))) {
288                 if (!strcmp(opt, item))
289                         break;
290                 mlp = NULL;
291         }
292         free(ml);
293         return (item != NULL);
294 }
295
296 /* Issue a message on stderr for every item in wanted_mountopts that is not
297  * present in mountopts.  The justwarn boolean toggles between error and
298  * warning message.  Return an error count.
299  */
300 int check_mountfsoptions(char *mountopts, char *wanted_mountopts,
301                          int justwarn)
302 {
303         char *ml, *mlp, *item, *ctx = NULL;
304         int errors = 0;
305
306         if (!(ml = strdup(wanted_mountopts))) {
307                 fprintf(stderr, "%s: out of memory\n", progname);
308                 exit(1);
309         }
310         mlp = ml;
311         while ((item = strtok_r(mlp, ",", &ctx))) {
312                 if (!in_mntlist(item, mountopts)) {
313                         fprintf(stderr, "%s: %s mount option `%s' is missing\n",
314                                 progname, justwarn ? "Warning: default"
315                                 : "Error: mandatory", item);
316                         errors++;
317                 }
318                 mlp = NULL;
319         }
320         free(ml);
321         return errors;
322 }
323
324 /* Trim embedded white space, leading and trailing commas from string s.
325  */
326 void trim_mountfsoptions(char *s)
327 {
328         char *p;
329
330         for (p = s; *p; ) {
331                 if (isspace(*p)) {
332                         memmove(p, p + 1, strlen(p + 1) + 1);
333                         continue;
334                 }
335                 p++;
336         }
337
338         while (s[0] == ',')
339                 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
340
341         p = s + strlen(s) - 1;
342         while (p >= s && *p == ',')
343                 *p-- = '\0';
344 }
345
346 /* Setup a file in the first unused loop_device */
347 int loop_setup(struct mkfs_opts *mop)
348 {
349         char loop_base[20];
350         char l_device[64];
351         int i, ret = 0;
352
353         /* Figure out the loop device names */
354         if (!access("/dev/loop0", F_OK | R_OK)) {
355                 strcpy(loop_base, "/dev/loop\0");
356         } else if (!access("/dev/loop/0", F_OK | R_OK)) {
357                 strcpy(loop_base, "/dev/loop/\0");
358         } else {
359                 fprintf(stderr, "%s: can't access loop devices\n", progname);
360                 return EACCES;
361         }
362
363         /* Find unused loop device */
364         for (i = 0; i < MAX_LOOP_DEVICES; i++) {
365                 char cmd[PATH_MAX];
366                 int cmdsz = sizeof(cmd);
367
368                 sprintf(l_device, "%s%d", loop_base, i);
369                 if (access(l_device, F_OK | R_OK))
370                         break;
371                 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
372                 ret = system(cmd);
373
374                 /* losetup gets 1 (ret=256) for non-set-up device */
375                 if (ret) {
376                         /* Set up a loopback device to our file */
377                         snprintf(cmd, cmdsz, "losetup %s %s", l_device,
378                                  mop->mo_device);
379                         ret = run_command(cmd, cmdsz);
380                         if (ret == 256)
381                                 /* someone else picked up this loop device
382                                  * behind our back */
383                                 continue;
384                         if (ret) {
385                                 fprintf(stderr, "%s: error %d on losetup: %s\n",
386                                         progname, ret,
387                                         ret >= 0 ? strerror(ret) : "");
388                                 return ret;
389                         }
390                         strscpy(mop->mo_loopdev, l_device,
391                                 sizeof(mop->mo_loopdev));
392                         return ret;
393                 }
394         }
395
396         fprintf(stderr, "%s: out of loop devices!\n", progname);
397         return EMFILE;
398 }
399
400 int loop_cleanup(struct mkfs_opts *mop)
401 {
402         char cmd[150];
403         int ret = 1;
404         if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
405                 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
406                 ret = run_command(cmd, sizeof(cmd));
407         }
408         return ret;
409 }
410
411 int loop_format(struct mkfs_opts *mop)
412 {
413         int fd;
414
415         if (mop->mo_device_sz == 0) {
416                 fatal();
417                 fprintf(stderr, "loop device requires a --device-size= "
418                         "param\n");
419                 return EINVAL;
420         }
421
422         fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
423         if (fd < 0) {
424                 fatal();
425                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
426                         progname, strerror(errno));
427                 return errno;
428         }
429
430         if (ftruncate(fd, mop->mo_device_sz * 1024) != 0) {
431                 close(fd);
432                 fatal();
433                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
434                         progname, strerror(errno));
435                 return errno;
436         }
437
438         close(fd);
439         return 0;
440 }
441
442 /* Write the server config files */
443 int osd_write_ldd(struct mkfs_opts *mop)
444 {
445         struct lustre_disk_data *ldd = &mop->mo_ldd;
446         int ret;
447
448         switch (ldd->ldd_mount_type) {
449 #ifdef HAVE_LDISKFS_OSD
450         case LDD_MT_LDISKFS:
451         case LDD_MT_LDISKFS2:
452                 ret = ldiskfs_write_ldd(mop);
453                 break;
454 #endif /* HAVE_LDISKFS_OSD */
455 #ifdef HAVE_ZFS_OSD
456         case LDD_MT_ZFS:
457                 ret = zfs_write_ldd(mop);
458                 break;
459 #endif /* HAVE_ZFS_OSD */
460         default:
461                 fatal();
462                 fprintf(stderr, "unknown fs type %d '%s'\n",
463                         ldd->ldd_mount_type, MT_STR(ldd));
464                 ret = EINVAL;
465                 break;
466         }
467
468         return ret;
469 }
470
471 /* Read the server config files */
472 int osd_read_ldd(char *dev, struct lustre_disk_data *ldd)
473 {
474         int ret;
475
476         switch (ldd->ldd_mount_type) {
477 #ifdef HAVE_LDISKFS_OSD
478         case LDD_MT_LDISKFS:
479         case LDD_MT_LDISKFS2:
480                 ret = ldiskfs_read_ldd(dev, ldd);
481                 break;
482 #endif /* HAVE_LDISKFS_OSD */
483 #ifdef HAVE_ZFS_OSD
484         case LDD_MT_ZFS:
485                 ret = zfs_read_ldd(dev, ldd);
486                 break;
487 #endif /* HAVE_ZFS_OSD */
488         default:
489                 fatal();
490                 fprintf(stderr, "unknown fs type %d '%s'\n",
491                         ldd->ldd_mount_type, MT_STR(ldd));
492                 ret = EINVAL;
493                 break;
494         }
495
496         return ret;
497 }
498
499 /* Was this device formatted for Lustre */
500 int osd_is_lustre(char *dev, unsigned *mount_type)
501 {
502         vprint("checking for existing Lustre data: ");
503
504 #ifdef HAVE_LDISKFS_OSD
505         if (ldiskfs_is_lustre(dev, mount_type)) {
506                 vprint("found\n");
507                 return 1;
508         }
509 #endif /* HAVE_LDISKFS_OSD */
510 #ifdef HAVE_ZFS_OSD
511         if (zfs_is_lustre(dev, mount_type)) {
512                 vprint("found\n");
513                 return 1;
514         }
515 #endif /* HAVE_ZFS_OSD */
516
517         vprint("not found\n");
518         return 0;
519 }
520
521 /* Build fs according to type */
522 int osd_make_lustre(struct mkfs_opts *mop)
523 {
524         struct lustre_disk_data *ldd = &mop->mo_ldd;
525         int ret;
526
527         switch (ldd->ldd_mount_type) {
528 #ifdef HAVE_LDISKFS_OSD
529         case LDD_MT_LDISKFS:
530         case LDD_MT_LDISKFS2:
531                 ret = ldiskfs_make_lustre(mop);
532                 break;
533 #endif /* HAVE_LDISKFS_OSD */
534 #ifdef HAVE_ZFS_OSD
535         case LDD_MT_ZFS:
536                 ret = zfs_make_lustre(mop);
537                 break;
538 #endif /* HAVE_ZFS_OSD */
539         default:
540                 fatal();
541                 fprintf(stderr, "unknown fs type %d '%s'\n",
542                         ldd->ldd_mount_type, MT_STR(ldd));
543                 ret = EINVAL;
544                 break;
545         }
546
547         return ret;
548 }
549
550 int osd_prepare_lustre(struct mkfs_opts *mop,
551                 char *default_mountopts, int default_len,
552                 char *always_mountopts, int always_len)
553 {
554         struct lustre_disk_data *ldd = &mop->mo_ldd;
555         int ret;
556
557         switch (ldd->ldd_mount_type) {
558 #ifdef HAVE_LDISKFS_OSD
559         case LDD_MT_LDISKFS:
560         case LDD_MT_LDISKFS2:
561                 ret = ldiskfs_prepare_lustre(mop,
562                                              default_mountopts, default_len,
563                                              always_mountopts, always_len);
564                 break;
565 #endif /* HAVE_LDISKFS_OSD */
566 #ifdef HAVE_ZFS_OSD
567         case LDD_MT_ZFS:
568                 ret = zfs_prepare_lustre(mop,
569                                          default_mountopts, default_len,
570                                          always_mountopts, always_len);
571                 break;
572 #endif /* HAVE_ZFS_OSD */
573         default:
574                 fatal();
575                 fprintf(stderr, "unknown fs type %d '%s'\n",
576                         ldd->ldd_mount_type, MT_STR(ldd));
577                 ret = EINVAL;
578                 break;
579         }
580
581         return ret;
582 }
583
584 int osd_tune_lustre(char *dev, struct mount_opts *mop)
585 {
586         struct lustre_disk_data *ldd = &mop->mo_ldd;
587         int ret;
588
589         switch (ldd->ldd_mount_type) {
590 #ifdef HAVE_LDISKFS_OSD
591         case LDD_MT_LDISKFS:
592         case LDD_MT_LDISKFS2:
593                 ret = ldiskfs_tune_lustre(dev, mop);
594                 break;
595 #endif /* HAVE_LDISKFS_OSD */
596 #ifdef HAVE_ZFS_OSD
597         case LDD_MT_ZFS:
598                 ret = zfs_tune_lustre(dev, mop);
599                 break;
600 #endif /* HAVE_ZFS_OSD */
601         default:
602                 fatal();
603                 fprintf(stderr, "unknown fs type %d '%s'\n",
604                                 ldd->ldd_mount_type, MT_STR(ldd));
605                 ret = EINVAL;
606                 break;
607         }
608
609         return ret;
610 }
611
612 int osd_label_lustre(struct mount_opts *mop)
613 {
614         struct lustre_disk_data *ldd = &mop->mo_ldd;
615         int ret;
616
617         switch (ldd->ldd_mount_type) {
618 #ifdef HAVE_LDISKFS_OSD
619         case LDD_MT_LDISKFS:
620         case LDD_MT_LDISKFS2:
621                 ret = ldiskfs_label_lustre(mop);
622                 break;
623 #endif /* HAVE_LDISKFS_OSD */
624 #ifdef HAVE_ZFS_OSD
625         case LDD_MT_ZFS:
626                 ret = zfs_label_lustre(mop);
627                 break;
628 #endif /* HAVE_ZFS_OSD */
629         default:
630                 fatal();
631                 fprintf(stderr, "unknown fs type %d '%s'\n",
632                         ldd->ldd_mount_type, MT_STR(ldd));
633                 ret = EINVAL;
634                 break;
635         }
636
637         return ret;
638 }
639
640 /* Enable quota accounting */
641 int osd_enable_quota(struct mkfs_opts *mop)
642 {
643         struct lustre_disk_data *ldd = &mop->mo_ldd;
644         int ret;
645
646         switch (ldd->ldd_mount_type) {
647 #ifdef HAVE_LDISKFS_OSD
648         case LDD_MT_EXT3:
649         case LDD_MT_LDISKFS:
650         case LDD_MT_LDISKFS2:
651                 ret = ldiskfs_enable_quota(mop);
652                 break;
653 #endif /* HAVE_LDISKFS_OSD */
654 #ifdef HAVE_ZFS_OSD
655         case LDD_MT_ZFS:
656                 fprintf(stderr, "this option is only valid for ldiskfs\n");
657                 ret = EINVAL;
658                 break;
659 #endif /* HAVE_ZFS_OSD */
660         default:
661                 fatal();
662                 fprintf(stderr, "unknown fs type %d '%s'\n",
663                         ldd->ldd_mount_type, MT_STR(ldd));
664                 ret = EINVAL;
665                 break;
666         }
667
668         return ret;
669 }
670
671 int osd_init(void)
672 {
673         int ret = 0;
674
675 #ifdef HAVE_LDISKFS_OSD
676         ret = ldiskfs_init();
677         if (ret)
678                 return ret;
679 #endif /* HAVE_LDISKFS_OSD */
680 #ifdef HAVE_ZFS_OSD
681         ret = zfs_init();
682         /* we want to be able to set up a ldiskfs-based filesystem w/o
683          * the ZFS modules installed, see ORI-425 */
684         if (ret)
685                 ret = 0;
686 #endif /* HAVE_ZFS_OSD */
687
688         return ret;
689 }
690
691 void osd_fini(void)
692 {
693 #ifdef HAVE_LDISKFS_OSD
694         ldiskfs_fini();
695 #endif /* HAVE_LDISKFS_OSD */
696 #ifdef HAVE_ZFS_OSD
697         zfs_fini();
698 #endif /* HAVE_ZFS_OSD */
699 }
700
701 __u64 get_device_size(char* device)
702 {
703         int ret, fd;
704         __u64 size = 0;
705
706         fd = open(device, O_RDONLY);
707         if (fd < 0) {
708                 fprintf(stderr, "%s: cannot open %s: %s\n",
709                         progname, device, strerror(errno));
710                 return 0;
711         }
712
713 #ifdef BLKGETSIZE64
714         /* size in bytes. bz5831 */
715         ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
716 #else
717         {
718                 __u32 lsize = 0;
719                 /* size in blocks */
720                 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
721                 size = (__u64)lsize * 512;
722         }
723 #endif
724         close(fd);
725         if (ret < 0) {
726                 fprintf(stderr, "%s: size ioctl failed: %s\n",
727                         progname, strerror(errno));
728                 return 0;
729         }
730
731         vprint("device size = "LPU64"MB\n", size >> 20);
732         /* return value in KB */
733         return size >> 10;
734 }
735
736 int file_create(char *path, __u64 size)
737 {
738         __u64 size_max;
739         int ret;
740         int fd;
741
742         /*
743          * Since "size" is in KB, the file offset it represents could overflow
744          * off_t.
745          */
746         size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
747         if (size >= size_max) {
748                 fprintf(stderr, "%s: "LPU64" KB: Backing store size must be "
749                         "smaller than "LPU64" KB\n", progname, size, size_max);
750                 return EFBIG;
751         }
752
753         ret = access(path, F_OK);
754         if (ret == 0) {
755                 ret = unlink(path);
756                 if (ret != 0)
757                         return errno;
758         }
759
760         fd = creat(path, S_IRUSR|S_IWUSR);
761         if (fd < 0) {
762                 fatal();
763                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
764                         progname, strerror(errno));
765                 return errno;
766         }
767
768         ret = ftruncate(fd, size * 1024);
769         close(fd);
770         if (ret != 0) {
771                 fatal();
772                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
773                         progname, strerror(errno));
774                 return errno;
775         }
776
777         return 0;
778 }
779
780 /**
781  * Try to get the real path to the device, in case it is a
782  * symbolic link or dm device for instance
783  */
784 int get_realpath(char *path, char **device)
785 {
786         FILE *f;
787         size_t sz;
788         char *ptr;
789         char name[256];
790         char real_path[PATH_MAX] = {'\0'};
791
792         if (realpath(path, real_path) == NULL)
793                 return errno;
794
795         ptr = strrchr(real_path, '/');
796         if (ptr && strncmp(ptr, "/dm-", 4) == 0 && isdigit(*(ptr + 4))) {
797                 snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptr+1);
798                 f = fopen(path, "r");
799                 if (f != NULL) {
800                         /* read "<name>\n" from sysfs */
801                         if (fgets(name, sizeof(name), f) != NULL) {
802                                 sz = strlen(name);
803                                 if (sz > 1) {
804                                         name[sz - 1] = '\0';
805                                         snprintf(real_path, sizeof(real_path),
806                                                  "/dev/mapper/%s", name);
807                                 }
808                         }
809                         fclose(f);
810                 }
811         }
812         *device = strdup(real_path);
813
814         return 0;
815 }