2
* aufs sample -- ULOOP driver
4
* Copyright (C) 2007 Junjiro Okajima
6
* This program, aufs is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
/* $Id: ulobdev.c,v 1.1 2007/11/26 01:35:40 sfjro Exp $ */
24
#include <linux/uloop.h>
25
#include <sys/ioctl.h>
38
static char *me, *real_bdev;
40
#define Dbg(fmt, args...) printf("%s:%d:" fmt, __func__, __LINE__, ##args)
41
#define DbgErr(e) if (e) Dbg("err %d\n", e)
43
/* ---------------------------------------------------------------------- */
45
static int store(unsigned long long start, int size, void *arg)
48
unsigned long long m, tsize;
51
//Dbg("start %Lu, size %d\n", start, size);
52
assert(start + size <= uloop->cache_size);
55
m = start % uloop->pagesize;
58
tsize = uloop->tgt_size;
59
if (tsize < start + size)
61
src = mmap(NULL, size, PROT_READ, MAP_SHARED, real_fd, start);
62
if (src == MAP_FAILED)
64
dst = mmap(NULL, size, PROT_WRITE, MAP_SHARED, ulo_cache_fd, start);
65
if (dst == MAP_FAILED)
67
memcpy(dst, src, size);
70
err = msync(dst, size, MS_SYNC);
73
err = munmap(dst, size);
75
munmap(src, size); /* ignore */
81
/* ---------------------------------------------------------------------- */
83
static unsigned long long init_size(void)
85
unsigned long long sz;
89
real_fd = open(real_bdev, O_RDONLY);
92
err = ioctl(real_fd, BLKGETSIZE, &i);
99
goto out; /* success */
107
/* ---------------------------------------------------------------------- */
109
static int init(struct ulo_init *init_args)
113
init_args->size = init_size();
114
err = init_args->size;
116
err = ulo_init(init_args);
122
static void usage(void)
127
" /dev/loopN block_device\n",
132
static int parse(int argc, char *argv[], struct ulo_init *init_args)
135
static char bitmap_def[] = "/tmp/123456.bitmap",
136
cache_def[] = "/tmp/123456.cache";
138
while ((opt = getopt(argc, argv, "b:c:")) != -1) {
141
init_args->path[ULO_BITMAP] = optarg;
144
init_args->path[ULO_CACHE] = optarg;
152
if (argc - optind != 2) {
157
init_args->path[ULO_DEV] = argv[optind];
158
real_bdev = argv[optind + 1];
160
if (!init_args->path[ULO_BITMAP]) {
161
snprintf(bitmap_def, sizeof(bitmap_def) - 1, "/tmp/%d.bitmap",
163
init_args->path[ULO_BITMAP] = bitmap_def;
165
if (!init_args->path[ULO_CACHE]) {
166
snprintf(cache_def, sizeof(cache_def) - 1, "/tmp/%d.cache",
168
init_args->path[ULO_CACHE] = cache_def;
171
//Dbg("to %d, %s\n", timeout, real_bdev);
175
/* ---------------------------------------------------------------------- */
177
int main(int argc, char *argv[])
181
struct ulo_init init_args = {
182
.dev_flags = O_RDONLY
183
//.dev_flags = O_RDWR
187
err = parse(argc, argv, &init_args);
189
err = init(&init_args);
193
err = ulo_loop(SIGUSR1, store, NULL);