6
6
#include <stdio.h>
7
7
#include <string.h>
8
8
#include <stdlib.h>
9
+ #include <sys/file.h>
9
10
#include <sys/stat.h>
10
11
#include <string.h>
11
12
#include <errno.h>
@@ -24,6 +25,7 @@ struct lklfuse {
24
25
const char * log ;
25
26
const char * type ;
26
27
const char * opts ;
28
+ const char * lock ;
27
29
struct lkl_disk disk ;
28
30
int disk_id ;
29
31
int part ;
@@ -46,6 +48,7 @@ static struct fuse_opt lklfuse_opts[] = {
46
48
LKLFUSE_OPT ("mb=%d" , mb , 0 ),
47
49
LKLFUSE_OPT ("opts=%s" , opts , 0 ),
48
50
LKLFUSE_OPT ("part=%d" , part , 0 ),
51
+ LKLFUSE_OPT ("lock=%s" , lock , 0 ),
49
52
FUSE_OPT_KEY ("-h" , KEY_HELP ),
50
53
FUSE_OPT_KEY ("--help" , KEY_HELP ),
51
54
FUSE_OPT_KEY ("-V" , KEY_VERSION ),
@@ -58,7 +61,7 @@ static struct fuse_opt lklfuse_opts[] = {
58
61
static void usage (void )
59
62
{
60
63
printf (
61
- "usage: lklfuse file mountpoint [options]\n"
64
+ "usage: lklfuse block-device mountpoint [options]\n"
62
65
"\n"
63
66
"general options:\n"
64
67
" -o opt,[opt...] mount options\n"
@@ -70,7 +73,8 @@ static void usage(void)
70
73
" -o type=fstype filesystem type\n"
71
74
" -o mb=memory amount of memory to allocate in MB (default: 64)\n"
72
75
" -o part=parition partition to mount\n"
73
- " -o ro open file read-only\n"
76
+ " -o ro open block-device read-only\n"
77
+ " -o lock=FILE only mount after taking an exclusive lock on FILE\n"
74
78
" -o opts=options mount options (use \\ to escape , and =)\n"
75
79
);
76
80
}
@@ -791,7 +795,7 @@ int main(int argc, char **argv)
791
795
struct fuse_cmdline_opts cli_opts ;
792
796
struct fuse * fuse ;
793
797
struct stat st ;
794
- int ret ;
798
+ int ret , lockfd = -1 ;
795
799
796
800
if (fuse_opt_parse (& args , & lklfuse , lklfuse_opts , lklfuse_opt_proc ))
797
801
return 1 ;
@@ -801,6 +805,23 @@ int main(int argc, char **argv)
801
805
return 1 ;
802
806
}
803
807
808
+ if (lklfuse .lock ) {
809
+ lockfd = open (lklfuse .lock , O_RDWR | O_CREAT , 0644 );
810
+ if (lockfd < 0 ) {
811
+ fprintf (stderr , "failed to open %s: %s\n" ,
812
+ lklfuse .lock , strerror (errno ));
813
+ return 1 ;
814
+ }
815
+
816
+ ret = flock (lockfd , LOCK_EX | LOCK_NB );
817
+ if (ret < 0 ) {
818
+ fprintf (stderr , "unable to exclusively lock %s: %s\n" ,
819
+ lklfuse .lock , strerror (errno ));
820
+ return 2 ;
821
+ }
822
+ /* lock dropped when lockfd is closed on program exit */
823
+ }
824
+
804
825
if (fuse_parse_cmdline (& args , & cli_opts ))
805
826
return 1 ;
806
827
0 commit comments