~ubuntu-branches/ubuntu/vivid/aufs/vivid

« back to all changes in this revision

Viewing changes to sample/uloop/ulobdev.c

  • Committer: Bazaar Package Importer
  • Author(s): Julian Andres Klode
  • Date: 2007-12-15 23:32:51 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20071215233251-2vgs2lmg8mai5d9e
Tags: 0+20071211-1ubuntu1
* Merge from debian unstable (LP: #175705), remaining changes:
  - Fix for Ubuntu Kernels (updated)
* patches/01_vserver.dpatch: Removed
* patches/06_ubuntu.dpatch: Added (update of ubuntu patch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * aufs sample -- ULOOP driver
 
3
 *
 
4
 * Copyright (C) 2007 Junjiro Okajima
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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
 
19
 */
 
20
 
 
21
/* $Id: ulobdev.c,v 1.1 2007/11/26 01:35:40 sfjro Exp $ */
 
22
 
 
23
#include <linux/fs.h>
 
24
#include <linux/uloop.h>
 
25
#include <sys/ioctl.h>
 
26
#include <sys/mman.h>
 
27
#include <assert.h>
 
28
#include <errno.h>
 
29
#include <fcntl.h>
 
30
#include <getopt.h>
 
31
#include <signal.h>
 
32
#include <stdio.h>
 
33
#include <stdlib.h>
 
34
#include <string.h>
 
35
#include <unistd.h>
 
36
 
 
37
static int real_fd;
 
38
static char *me, *real_bdev;
 
39
 
 
40
#define Dbg(fmt, args...) printf("%s:%d:" fmt, __func__, __LINE__, ##args)
 
41
#define DbgErr(e) if (e) Dbg("err %d\n", e)
 
42
 
 
43
/* ---------------------------------------------------------------------- */
 
44
 
 
45
static int store(unsigned long long start, int size, void *arg)
 
46
{
 
47
        int err;
 
48
        unsigned long long m, tsize;
 
49
        char *src, *dst;
 
50
 
 
51
        //Dbg("start %Lu, size %d\n", start, size);
 
52
        assert(start + size <= uloop->cache_size);
 
53
 
 
54
        err = -1;
 
55
        m = start % uloop->pagesize;
 
56
        start -= m;
 
57
        size += m;
 
58
        tsize = uloop->tgt_size;
 
59
        if (tsize < start + size)
 
60
                size = tsize - start;
 
61
        src = mmap(NULL, size, PROT_READ, MAP_SHARED, real_fd, start);
 
62
        if (src == MAP_FAILED)
 
63
                goto out;
 
64
        dst = mmap(NULL, size, PROT_WRITE, MAP_SHARED, ulo_cache_fd, start);
 
65
        if (dst == MAP_FAILED)
 
66
                goto out_src;
 
67
        memcpy(dst, src, size);
 
68
 
 
69
#if 0
 
70
        err = msync(dst, size, MS_SYNC);
 
71
        DbgErr(err);
 
72
#endif
 
73
        err = munmap(dst, size);
 
74
 out_src:
 
75
        munmap(src, size); /* ignore */
 
76
 out:
 
77
        DbgErr(err);
 
78
        return err;
 
79
}
 
80
 
 
81
/* ---------------------------------------------------------------------- */
 
82
 
 
83
static unsigned long long init_size(void)
 
84
{
 
85
        unsigned long long sz;
 
86
        int err, i;
 
87
 
 
88
        sz = -1;
 
89
        real_fd = open(real_bdev, O_RDONLY);
 
90
        if (real_fd < 0)
 
91
                goto out_err;
 
92
        err = ioctl(real_fd, BLKGETSIZE, &i);
 
93
        if (err) {
 
94
                close(real_fd);
 
95
                goto out_err;
 
96
        }
 
97
        sz = i;
 
98
        sz *= 512;
 
99
        goto out; /* success */
 
100
 
 
101
 out_err:
 
102
        me = real_bdev;
 
103
 out:
 
104
        return sz;
 
105
}
 
106
 
 
107
/* ---------------------------------------------------------------------- */
 
108
 
 
109
static int init(struct ulo_init *init_args)
 
110
{
 
111
        int err;
 
112
 
 
113
        init_args->size = init_size();
 
114
        err = init_args->size;
 
115
        if (err != -1)
 
116
                err = ulo_init(init_args);
 
117
 
 
118
        me = real_bdev;
 
119
        return err;
 
120
}
 
121
 
 
122
static void usage(void)
 
123
{
 
124
        fprintf(stderr, "%s"
 
125
                " [-b bitmap]"
 
126
                " [-c cache]"
 
127
                " /dev/loopN block_device\n",
 
128
                me);
 
129
        exit(EINVAL);
 
130
}
 
131
 
 
132
static int parse(int argc, char *argv[], struct ulo_init *init_args)
 
133
{
 
134
        int opt;
 
135
        static char bitmap_def[] = "/tmp/123456.bitmap",
 
136
                cache_def[] = "/tmp/123456.cache";
 
137
 
 
138
        while ((opt = getopt(argc, argv, "b:c:")) != -1) {
 
139
                switch (opt) {
 
140
                case 'b':
 
141
                        init_args->path[ULO_BITMAP] = optarg;
 
142
                        break;
 
143
                case 'c':
 
144
                        init_args->path[ULO_CACHE] = optarg;
 
145
                        break;
 
146
                default:
 
147
                        usage();
 
148
                        break;
 
149
                }
 
150
        }
 
151
 
 
152
        if (argc - optind != 2) {
 
153
                usage();
 
154
                return EINVAL;
 
155
        }
 
156
 
 
157
        init_args->path[ULO_DEV] = argv[optind];
 
158
        real_bdev = argv[optind + 1];
 
159
 
 
160
        if (!init_args->path[ULO_BITMAP]) {
 
161
                snprintf(bitmap_def, sizeof(bitmap_def) - 1, "/tmp/%d.bitmap",
 
162
                         getpid());
 
163
                init_args->path[ULO_BITMAP] = bitmap_def;
 
164
        }
 
165
        if (!init_args->path[ULO_CACHE]) {
 
166
                snprintf(cache_def, sizeof(cache_def) - 1, "/tmp/%d.cache",
 
167
                         getpid());
 
168
                init_args->path[ULO_CACHE] = cache_def;
 
169
        }
 
170
 
 
171
        //Dbg("to %d, %s\n", timeout, real_bdev);
 
172
        return 0;
 
173
}
 
174
 
 
175
/* ---------------------------------------------------------------------- */
 
176
 
 
177
int main(int argc, char *argv[])
 
178
{
 
179
        int err;
 
180
        pid_t pid;
 
181
        struct ulo_init init_args = {
 
182
                .dev_flags = O_RDONLY
 
183
                //.dev_flags = O_RDWR
 
184
        };
 
185
 
 
186
        me = argv[0];
 
187
        err = parse(argc, argv, &init_args);
 
188
        if (!err)
 
189
                err = init(&init_args);
 
190
        if (!err) {
 
191
                pid = fork();
 
192
                if (!pid)
 
193
                        err = ulo_loop(SIGUSR1, store, NULL);
 
194
                else if (pid > 0)
 
195
                        sleep(1);
 
196
        }
 
197
 
 
198
        if (err && me)
 
199
                perror(me);
 
200
        return err;
 
201
}