Whamcloud - gitweb
f7a62ed64c5e189894cac2696d5d1d2dc4ffeedb
[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 #include <dlfcn.h>
51
52 extern char *progname;
53 extern int verbose;
54
55 #define vprint(fmt, arg...) if (verbose > 0) printf(fmt, ##arg)
56 #define verrprint(fmt, arg...) if (verbose >= 0) fprintf(stderr, fmt, ##arg)
57
58 static struct module_backfs_ops *backfs_ops[LDD_MT_LAST];
59
60 void fatal(void)
61 {
62         verbose = 0;
63         fprintf(stderr, "\n%s FATAL: ", progname);
64 }
65
66 int run_command(char *cmd, int cmdsz)
67 {
68         char log[] = "/tmp/run_command_logXXXXXX";
69         int fd = -1, rc;
70
71         if ((cmdsz - strlen(cmd)) < 6) {
72                 fatal();
73                 fprintf(stderr, "Command buffer overflow: %.*s...\n",
74                         cmdsz, cmd);
75                 return ENOMEM;
76         }
77
78         if (verbose > 1) {
79                 printf("cmd: %s\n", cmd);
80         } else {
81                 if ((fd = mkstemp(log)) >= 0) {
82                         close(fd);
83                         strcat(cmd, " >");
84                         strcat(cmd, log);
85                 }
86         }
87         strcat(cmd, " 2>&1");
88
89         /* Can't use popen because we need the rv of the command */
90         rc = system(cmd);
91         if (rc && (fd >= 0)) {
92                 char buf[128];
93                 FILE *fp;
94                 fp = fopen(log, "r");
95                 if (fp) {
96                         while (fgets(buf, sizeof(buf), fp) != NULL) {
97                                 printf("   %s", buf);
98                         }
99                         fclose(fp);
100                 }
101         }
102         if (fd >= 0)
103                 remove(log);
104         return rc;
105 }
106
107 int add_param(char *buf, char *key, char *val)
108 {
109         int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
110         int start = strlen(buf);
111         int keylen = 0;
112
113         if (key)
114                 keylen = strlen(key);
115         if (start + 1 + keylen + strlen(val) >= end) {
116                 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
117                         progname, buf, key ? key : "", val);
118                 return 1;
119         }
120
121         sprintf(buf + start, " %s%s", key ? key : "", val);
122         return 0;
123 }
124
125 int get_param(char *buf, char *key, char **val)
126 {
127         int i, key_len = strlen(key);
128         char *ptr;
129
130         ptr = strstr(buf, key);
131         if (ptr) {
132                 *val = strdup(ptr + key_len);
133                 if (*val == NULL)
134                         return ENOMEM;
135
136                 for (i = 0; i < strlen(*val); i++)
137                         if (((*val)[i] == ' ') || ((*val)[i] == '\0'))
138                                 break;
139
140                 (*val)[i] = '\0';
141                 return 0;
142         }
143
144         return ENOENT;
145 }
146
147 int append_param(char *buf, char *key, char *val, char sep)
148 {
149         int key_len, i, offset, old_val_len;
150         char *ptr = NULL, str[1024];
151
152         if (key)
153                 ptr = strstr(buf, key);
154
155         /* key doesn't exist yet, so just add it */
156         if (ptr == NULL)
157                 return add_param(buf, key, val);
158
159         key_len = strlen(key);
160
161         /* Copy previous values to str */
162         for (i = 0; i < sizeof(str); ++i) {
163                 if ((ptr[i+key_len] == ' ') || (ptr[i+key_len] == '\0'))
164                         break;
165                 str[i] = ptr[i+key_len];
166         }
167         if (i == sizeof(str))
168                 return E2BIG;
169         old_val_len = i;
170
171         offset = old_val_len+key_len;
172
173         /* Move rest of buf to overwrite previous key and value */
174         for (i = 0; ptr[i+offset] != '\0'; ++i)
175                 ptr[i] = ptr[i+offset];
176
177         ptr[i] = '\0';
178
179         snprintf(str+old_val_len, sizeof(str)-old_val_len, "%c%s", sep, val);
180
181         return add_param(buf, key, str);
182 }
183
184 char *strscat(char *dst, char *src, int buflen)
185 {
186         dst[buflen - 1] = 0;
187         if (strlen(dst) + strlen(src) >= buflen) {
188                 fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
189                         "\n", buflen, dst, src);
190                 exit(EOVERFLOW);
191         }
192         return strcat(dst, src);
193 }
194
195 char *strscpy(char *dst, char *src, int buflen)
196 {
197         dst[0] = 0;
198         return strscat(dst, src, buflen);
199 }
200
201 int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
202 {
203         FILE *fp;
204         struct mntent *mnt;
205
206         fp = setmntent(MOUNTED, "r");
207         if (fp == NULL)
208                 return 0;
209
210         while ((mnt = getmntent(fp)) != NULL) {
211                 if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
212                      strcmp(mnt->mnt_fsname, spec2) == 0) &&
213                     (mtpt == NULL || strcmp(mnt->mnt_dir, mtpt) == 0) &&
214                     (type == NULL || strcmp(mnt->mnt_type, type) == 0)) {
215                         endmntent(fp);
216                         return(EEXIST);
217                 }
218         }
219         endmntent(fp);
220
221         return 0;
222 }
223
224 #define PROC_DIR        "/proc/"
225 static int mtab_is_proc(const char *mtab)
226 {
227         char path[16];
228
229         if (readlink(mtab, path, sizeof(path)) < 0)
230                 return 0;
231
232         if (strncmp(path, PROC_DIR, strlen(PROC_DIR)))
233                 return 0;
234
235         return 1;
236 }
237
238 int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
239                 int flags, int freq, int pass)
240 {
241         FILE *fp;
242         struct mntent mnt;
243         int rc = 0;
244
245         /* Don't update mtab if it is linked to any file in /proc direcotry.*/
246         if (mtab_is_proc(MOUNTED))
247                 return 0;
248
249         mnt.mnt_fsname = spec;
250         mnt.mnt_dir = mtpt;
251         mnt.mnt_type = type;
252         mnt.mnt_opts = opts ? opts : "";
253         mnt.mnt_freq = freq;
254         mnt.mnt_passno = pass;
255
256         fp = setmntent(MOUNTED, "a+");
257         if (fp == NULL) {
258                 fprintf(stderr, "%s: setmntent(%s): %s:",
259                         progname, MOUNTED, strerror (errno));
260                 rc = 16;
261         } else {
262                 if ((addmntent(fp, &mnt)) == 1) {
263                         fprintf(stderr, "%s: addmntent: %s:",
264                                 progname, strerror (errno));
265                         rc = 16;
266                 }
267                 endmntent(fp);
268         }
269
270         return rc;
271 }
272
273 /* Search for opt in mntlist, returning true if found.
274  */
275 static int in_mntlist(char *opt, char *mntlist)
276 {
277         char *ml, *mlp, *item, *ctx = NULL;
278
279         if (!(ml = strdup(mntlist))) {
280                 fprintf(stderr, "%s: out of memory\n", progname);
281                 exit(1);
282         }
283         mlp = ml;
284         while ((item = strtok_r(mlp, ",", &ctx))) {
285                 if (!strcmp(opt, item))
286                         break;
287                 mlp = NULL;
288         }
289         free(ml);
290         return (item != NULL);
291 }
292
293 /* Issue a message on stderr for every item in wanted_mountopts that is not
294  * present in mountopts.  The justwarn boolean toggles between error and
295  * warning message.  Return an error count.
296  */
297 int check_mountfsoptions(char *mountopts, char *wanted_mountopts,
298                          int justwarn)
299 {
300         char *ml, *mlp, *item, *ctx = NULL;
301         int errors = 0;
302
303         if (!(ml = strdup(wanted_mountopts))) {
304                 fprintf(stderr, "%s: out of memory\n", progname);
305                 exit(1);
306         }
307         mlp = ml;
308         while ((item = strtok_r(mlp, ",", &ctx))) {
309                 if (!in_mntlist(item, mountopts)) {
310                         fprintf(stderr, "%s: %s mount option `%s' is missing\n",
311                                 progname, justwarn ? "Warning: default"
312                                 : "Error: mandatory", item);
313                         errors++;
314                 }
315                 mlp = NULL;
316         }
317         free(ml);
318         return errors;
319 }
320
321 /* Trim embedded white space, leading and trailing commas from string s.
322  */
323 void trim_mountfsoptions(char *s)
324 {
325         char *p;
326
327         for (p = s; *p; ) {
328                 if (isspace(*p)) {
329                         memmove(p, p + 1, strlen(p + 1) + 1);
330                         continue;
331                 }
332                 p++;
333         }
334
335         while (s[0] == ',')
336                 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
337
338         p = s + strlen(s) - 1;
339         while (p >= s && *p == ',')
340                 *p-- = '\0';
341 }
342
343 /* Setup a file in the first unused loop_device */
344 int loop_setup(struct mkfs_opts *mop)
345 {
346         char loop_base[20];
347         char l_device[64];
348         int i, ret = 0;
349
350         /* Figure out the loop device names */
351         if (!access("/dev/loop0", F_OK | R_OK) ||
352             !access("/dev/loop-control", F_OK | R_OK)) {
353                 strcpy(loop_base, "/dev/loop\0");
354         } else if (!access("/dev/loop/0", F_OK | R_OK)) {
355                 strcpy(loop_base, "/dev/loop/\0");
356         } else {
357                 fprintf(stderr, "%s: can't access loop devices\n", progname);
358                 return EACCES;
359         }
360
361         /* Find unused loop device */
362         for (i = 0; i < MAX_LOOP_DEVICES; i++) {
363                 char cmd[PATH_MAX];
364                 int cmdsz = sizeof(cmd);
365
366 #ifdef LOOP_CTL_GET_FREE
367                 ret = open("/dev/loop-control", O_RDWR);
368                 if (ret < 0) {
369                         fprintf(stderr, "%s: can't access loop control\n", progname);
370                         return EACCES;
371                 }
372                 /* find or allocate a free loop device to use */
373                 i = ioctl(ret, LOOP_CTL_GET_FREE);
374                 if (i < 0) {
375                         fprintf(stderr, "%s: access loop control error\n", progname);
376                         return EACCES;
377                 }
378                 sprintf(l_device, "%s%d", loop_base, i);
379 #else
380                 sprintf(l_device, "%s%d", loop_base, i);
381                 if (access(l_device, F_OK | R_OK))
382                         break;
383 #endif
384                 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
385                 ret = system(cmd);
386
387                 /* losetup gets 1 (ret=256) for non-set-up device */
388                 if (ret) {
389                         /* Set up a loopback device to our file */
390                         snprintf(cmd, cmdsz, "losetup %s %s", l_device,
391                                  mop->mo_device);
392                         ret = run_command(cmd, cmdsz);
393                         if (ret == 256)
394                                 /* someone else picked up this loop device
395                                  * behind our back */
396                                 continue;
397                         if (ret) {
398                                 fprintf(stderr, "%s: error %d on losetup: %s\n",
399                                         progname, ret,
400                                         ret >= 0 ? strerror(ret) : "");
401                                 return ret;
402                         }
403                         strscpy(mop->mo_loopdev, l_device,
404                                 sizeof(mop->mo_loopdev));
405                         return ret;
406                 }
407         }
408
409         fprintf(stderr, "%s: out of loop devices!\n", progname);
410         return EMFILE;
411 }
412
413 int loop_cleanup(struct mkfs_opts *mop)
414 {
415         char cmd[150];
416         int ret = 0;
417
418         if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
419                 int tries;
420
421                 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
422                 for (tries = 0; tries < 3; tries++) {
423                         ret = run_command(cmd, sizeof(cmd));
424                         if (ret == 0)
425                                 break;
426                         sleep(1);
427                 }
428         }
429
430         if (ret != 0)
431                 fprintf(stderr, "cannot cleanup %s: rc = %d\n",
432                         mop->mo_loopdev, ret);
433         return ret;
434 }
435
436 int loop_format(struct mkfs_opts *mop)
437 {
438         int fd;
439
440         if (mop->mo_device_kb == 0) {
441                 fatal();
442                 fprintf(stderr, "loop device requires a --device-size= "
443                         "param\n");
444                 return EINVAL;
445         }
446
447         fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
448         if (fd < 0) {
449                 fatal();
450                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
451                         progname, strerror(errno));
452                 return errno;
453         }
454
455         if (ftruncate(fd, mop->mo_device_kb * 1024) != 0) {
456                 close(fd);
457                 fatal();
458                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
459                         progname, strerror(errno));
460                 return errno;
461         }
462
463         close(fd);
464         return 0;
465 }
466
467 #define DLSYM(prefix, sym, func)                                        \
468         do {                                                            \
469                 char _fname[64];                                        \
470                 snprintf(_fname, sizeof(_fname), "%s_%s", prefix, #func); \
471                 sym->func = (typeof(sym->func))dlsym(sym->dl_handle, _fname); \
472         } while (0)
473
474 /**
475  * Load plugin for a given mount_type from ${pkglibdir}/mount_osd_FSTYPE.so and
476  * return struct of function pointers (will be freed in unloack_backfs_module).
477  *
478  * \param[in] mount_type        Mount type to load module for.
479  * \retval Value of backfs_ops struct
480  * \retval NULL if no module exists
481  */
482 struct module_backfs_ops *load_backfs_module(enum ldd_mount_type mount_type)
483 {
484         void *handle;
485         char *error, filename[512], fsname[512], *name;
486         struct module_backfs_ops *ops;
487
488         /* This deals with duplicate ldd_mount_types resolving to same OSD layer
489          * plugin (e.g. ext3/ldiskfs/ldiskfs2 all being ldiskfs) */
490         strlcpy(fsname, mt_type(mount_type), sizeof(fsname));
491         name = fsname + sizeof("osd-") - 1;
492
493         /* change osd- to osd_ */
494         fsname[sizeof("osd-") - 2] = '_';
495
496         snprintf(filename, sizeof(filename), PLUGIN_DIR"/mount_%s.so", fsname);
497
498         handle = dlopen(filename, RTLD_LAZY);
499
500         /* Check for $LUSTRE environment variable from test-framework.
501          * This allows using locally built modules to be used.
502          */
503         if (handle == NULL) {
504                 char *dirname;
505                 dirname = getenv("LUSTRE");
506                 if (dirname) {
507                         snprintf(filename, sizeof(filename),
508                                  "%s/utils/.libs/mount_%s.so",
509                                  dirname, fsname);
510                         handle = dlopen(filename, RTLD_LAZY);
511                 }
512         }
513
514         /* Do not clutter up console with missing types */
515         if (handle == NULL)
516                 return NULL;
517
518         ops = malloc(sizeof(*ops));
519         if (ops == NULL) {
520                 dlclose(handle);
521                 return NULL;
522         }
523
524         ops->dl_handle = handle;
525         dlerror(); /* Clear any existing error */
526
527         DLSYM(name, ops, init);
528         DLSYM(name, ops, fini);
529         DLSYM(name, ops, read_ldd);
530         DLSYM(name, ops, write_ldd);
531         DLSYM(name, ops, is_lustre);
532         DLSYM(name, ops, make_lustre);
533         DLSYM(name, ops, prepare_lustre);
534         DLSYM(name, ops, tune_lustre);
535         DLSYM(name, ops, label_lustre);
536         DLSYM(name, ops, enable_quota);
537
538         error = dlerror();
539         if (error != NULL) {
540                 fatal();
541                 fprintf(stderr, "%s\n", error);
542                 dlclose(handle);
543                 free(ops);
544                 return NULL;
545         }
546         return ops;
547 }
548
549 /**
550  * Unload plugin and free backfs_ops structure. Must be called the same number
551  * of times as load_backfs_module is.
552  */
553 void unload_backfs_module(struct module_backfs_ops *ops)
554 {
555         if (ops == NULL)
556                 return;
557
558         dlclose(ops->dl_handle);
559         free(ops);
560 }
561
562 /* Return true if backfs_ops has operations for the given mount_type. */
563 int backfs_mount_type_okay(enum ldd_mount_type mount_type)
564 {
565         if (unlikely(mount_type >= LDD_MT_LAST || mount_type < 0)) {
566                 fatal();
567                 fprintf(stderr, "fs type out of range %d\n", mount_type);
568                 return 0;
569         }
570         if (backfs_ops[mount_type] == NULL) {
571                 fatal();
572                 fprintf(stderr, "unhandled fs type %d '%s'\n",
573                         mount_type, mt_str(mount_type));
574                 return 0;
575         }
576         return 1;
577 }
578
579 /* Write the server config files */
580 int osd_write_ldd(struct mkfs_opts *mop)
581 {
582         struct lustre_disk_data *ldd = &mop->mo_ldd;
583         int ret;
584
585         if (backfs_mount_type_okay(ldd->ldd_mount_type))
586                 ret = backfs_ops[ldd->ldd_mount_type]->write_ldd(mop);
587
588         else
589                 ret = EINVAL;
590
591         return ret;
592 }
593
594 /* Read the server config files */
595 int osd_read_ldd(char *dev, struct lustre_disk_data *ldd)
596 {
597         int ret;
598
599         if (backfs_mount_type_okay(ldd->ldd_mount_type))
600                 ret = backfs_ops[ldd->ldd_mount_type]->read_ldd(dev, ldd);
601
602         else
603                 ret = EINVAL;
604
605         return ret;
606 }
607
608 /* Was this device formatted for Lustre */
609 int osd_is_lustre(char *dev, unsigned *mount_type)
610 {
611         int i;
612
613         vprint("checking for existing Lustre data: ");
614
615         for (i = 0; i < LDD_MT_LAST; ++i) {
616                 if (backfs_ops[i] != NULL &&
617                     backfs_ops[i]->is_lustre(dev, mount_type)) {
618                         vprint("found\n");
619                         return 1;
620                 }
621         }
622
623         vprint("not found\n");
624         return 0;
625 }
626
627 /* Build fs according to type */
628 int osd_make_lustre(struct mkfs_opts *mop)
629 {
630         struct lustre_disk_data *ldd = &mop->mo_ldd;
631         int ret;
632
633         if (backfs_mount_type_okay(ldd->ldd_mount_type))
634                 ret = backfs_ops[ldd->ldd_mount_type]->make_lustre(mop);
635
636         else
637                 ret = EINVAL;
638
639         return ret;
640 }
641
642 int osd_prepare_lustre(struct mkfs_opts *mop,
643                 char *default_mountopts, int default_len,
644                 char *always_mountopts, int always_len)
645 {
646         struct lustre_disk_data *ldd = &mop->mo_ldd;
647         int ret;
648
649         if (backfs_mount_type_okay(ldd->ldd_mount_type))
650                 ret = backfs_ops[ldd->ldd_mount_type]->prepare_lustre(mop,
651                         default_mountopts, default_len,
652                         always_mountopts, always_len);
653
654         else
655                 ret = EINVAL;
656
657         return ret;
658 }
659
660 int osd_tune_lustre(char *dev, struct mount_opts *mop)
661 {
662         struct lustre_disk_data *ldd = &mop->mo_ldd;
663         int ret;
664
665         if (backfs_mount_type_okay(ldd->ldd_mount_type))
666                 ret = backfs_ops[ldd->ldd_mount_type]->tune_lustre(dev, mop);
667
668         else
669                 ret = EINVAL;
670
671         return ret;
672 }
673
674 int osd_label_lustre(struct mount_opts *mop)
675 {
676         struct lustre_disk_data *ldd = &mop->mo_ldd;
677         int ret;
678
679         if (backfs_mount_type_okay(ldd->ldd_mount_type))
680                 ret = backfs_ops[ldd->ldd_mount_type]->label_lustre(mop);
681
682         else
683                 ret = EINVAL;
684
685         return ret;
686 }
687
688 /* Enable quota accounting */
689 int osd_enable_quota(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]->enable_quota(mop);
696
697         else
698                 ret = EINVAL;
699
700         return ret;
701 }
702
703 int osd_init(void)
704 {
705         int i, ret = 0;
706
707         for (i = 0; i < LDD_MT_LAST; ++i) {
708                 backfs_ops[i] = load_backfs_module(i);
709                 if (backfs_ops[i] != NULL)
710                         ret = backfs_ops[i]->init();
711                 if (ret)
712                         break;
713         }
714
715         return ret;
716 }
717
718 void osd_fini(void)
719 {
720         int i;
721
722         for (i = 0; i < LDD_MT_LAST; ++i) {
723                 if (backfs_ops[i] != NULL) {
724                         backfs_ops[i]->fini();
725                         unload_backfs_module(backfs_ops[i]);
726                         backfs_ops[i] = NULL;
727                 }
728         }
729 }
730
731 __u64 get_device_size(char* device)
732 {
733         int ret, fd;
734         __u64 size = 0;
735
736         fd = open(device, O_RDONLY);
737         if (fd < 0) {
738                 fprintf(stderr, "%s: cannot open %s: %s\n",
739                         progname, device, strerror(errno));
740                 return 0;
741         }
742
743 #ifdef BLKGETSIZE64
744         /* size in bytes. bz5831 */
745         ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
746 #else
747         {
748                 __u32 lsize = 0;
749                 /* size in blocks */
750                 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
751                 size = (__u64)lsize * 512;
752         }
753 #endif
754         close(fd);
755         if (ret < 0) {
756                 fprintf(stderr, "%s: size ioctl failed: %s\n",
757                         progname, strerror(errno));
758                 return 0;
759         }
760
761         vprint("device size = "LPU64"MB\n", size >> 20);
762         /* return value in KB */
763         return size >> 10;
764 }
765
766 int file_create(char *path, __u64 size)
767 {
768         __u64 size_max;
769         int ret;
770         int fd;
771
772         /*
773          * Since "size" is in KB, the file offset it represents could overflow
774          * off_t.
775          */
776         size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
777         if (size >= size_max) {
778                 fprintf(stderr, "%s: "LPU64" KB: Backing store size must be "
779                         "smaller than "LPU64" KB\n", progname, size, size_max);
780                 return EFBIG;
781         }
782
783         ret = access(path, F_OK);
784         if (ret == 0) {
785                 ret = unlink(path);
786                 if (ret != 0)
787                         return errno;
788         }
789
790         fd = creat(path, S_IRUSR|S_IWUSR);
791         if (fd < 0) {
792                 fatal();
793                 fprintf(stderr, "%s: Unable to create backing store: %s\n",
794                         progname, strerror(errno));
795                 return errno;
796         }
797
798         ret = ftruncate(fd, size * 1024);
799         close(fd);
800         if (ret != 0) {
801                 fatal();
802                 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
803                         progname, strerror(errno));
804                 return errno;
805         }
806
807         return 0;
808 }