include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[linux-flexiantxendom0-3.2.10.git] / fs / logfs / dev_bdev.c
1 /*
2  * fs/logfs/dev_bdev.c  - Device access methods for block devices
3  *
4  * As should be obvious for Linux kernel code, license is GPLv2
5  *
6  * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
7  */
8 #include "logfs.h"
9 #include <linux/bio.h>
10 #include <linux/blkdev.h>
11 #include <linux/buffer_head.h>
12 #include <linux/gfp.h>
13
14 #define PAGE_OFS(ofs) ((ofs) & (PAGE_SIZE-1))
15
16 static void request_complete(struct bio *bio, int err)
17 {
18         complete((struct completion *)bio->bi_private);
19 }
20
21 static int sync_request(struct page *page, struct block_device *bdev, int rw)
22 {
23         struct bio bio;
24         struct bio_vec bio_vec;
25         struct completion complete;
26
27         bio_init(&bio);
28         bio.bi_io_vec = &bio_vec;
29         bio_vec.bv_page = page;
30         bio_vec.bv_len = PAGE_SIZE;
31         bio_vec.bv_offset = 0;
32         bio.bi_vcnt = 1;
33         bio.bi_idx = 0;
34         bio.bi_size = PAGE_SIZE;
35         bio.bi_bdev = bdev;
36         bio.bi_sector = page->index * (PAGE_SIZE >> 9);
37         init_completion(&complete);
38         bio.bi_private = &complete;
39         bio.bi_end_io = request_complete;
40
41         submit_bio(rw, &bio);
42         generic_unplug_device(bdev_get_queue(bdev));
43         wait_for_completion(&complete);
44         return test_bit(BIO_UPTODATE, &bio.bi_flags) ? 0 : -EIO;
45 }
46
47 static int bdev_readpage(void *_sb, struct page *page)
48 {
49         struct super_block *sb = _sb;
50         struct block_device *bdev = logfs_super(sb)->s_bdev;
51         int err;
52
53         err = sync_request(page, bdev, READ);
54         if (err) {
55                 ClearPageUptodate(page);
56                 SetPageError(page);
57         } else {
58                 SetPageUptodate(page);
59                 ClearPageError(page);
60         }
61         unlock_page(page);
62         return err;
63 }
64
65 static DECLARE_WAIT_QUEUE_HEAD(wq);
66
67 static void writeseg_end_io(struct bio *bio, int err)
68 {
69         const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
70         struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
71         struct super_block *sb = bio->bi_private;
72         struct logfs_super *super = logfs_super(sb);
73         struct page *page;
74
75         BUG_ON(!uptodate); /* FIXME: Retry io or write elsewhere */
76         BUG_ON(err);
77         BUG_ON(bio->bi_vcnt == 0);
78         do {
79                 page = bvec->bv_page;
80                 if (--bvec >= bio->bi_io_vec)
81                         prefetchw(&bvec->bv_page->flags);
82
83                 end_page_writeback(page);
84         } while (bvec >= bio->bi_io_vec);
85         bio_put(bio);
86         if (atomic_dec_and_test(&super->s_pending_writes))
87                 wake_up(&wq);
88 }
89
90 static int __bdev_writeseg(struct super_block *sb, u64 ofs, pgoff_t index,
91                 size_t nr_pages)
92 {
93         struct logfs_super *super = logfs_super(sb);
94         struct address_space *mapping = super->s_mapping_inode->i_mapping;
95         struct bio *bio;
96         struct page *page;
97         struct request_queue *q = bdev_get_queue(sb->s_bdev);
98         unsigned int max_pages = queue_max_hw_sectors(q) >> (PAGE_SHIFT - 9);
99         int i;
100
101         bio = bio_alloc(GFP_NOFS, max_pages);
102         BUG_ON(!bio); /* FIXME: handle this */
103
104         for (i = 0; i < nr_pages; i++) {
105                 if (i >= max_pages) {
106                         /* Block layer cannot split bios :( */
107                         bio->bi_vcnt = i;
108                         bio->bi_idx = 0;
109                         bio->bi_size = i * PAGE_SIZE;
110                         bio->bi_bdev = super->s_bdev;
111                         bio->bi_sector = ofs >> 9;
112                         bio->bi_private = sb;
113                         bio->bi_end_io = writeseg_end_io;
114                         atomic_inc(&super->s_pending_writes);
115                         submit_bio(WRITE, bio);
116
117                         ofs += i * PAGE_SIZE;
118                         index += i;
119                         nr_pages -= i;
120                         i = 0;
121
122                         bio = bio_alloc(GFP_NOFS, max_pages);
123                         BUG_ON(!bio);
124                 }
125                 page = find_lock_page(mapping, index + i);
126                 BUG_ON(!page);
127                 bio->bi_io_vec[i].bv_page = page;
128                 bio->bi_io_vec[i].bv_len = PAGE_SIZE;
129                 bio->bi_io_vec[i].bv_offset = 0;
130
131                 BUG_ON(PageWriteback(page));
132                 set_page_writeback(page);
133                 unlock_page(page);
134         }
135         bio->bi_vcnt = nr_pages;
136         bio->bi_idx = 0;
137         bio->bi_size = nr_pages * PAGE_SIZE;
138         bio->bi_bdev = super->s_bdev;
139         bio->bi_sector = ofs >> 9;
140         bio->bi_private = sb;
141         bio->bi_end_io = writeseg_end_io;
142         atomic_inc(&super->s_pending_writes);
143         submit_bio(WRITE, bio);
144         return 0;
145 }
146
147 static void bdev_writeseg(struct super_block *sb, u64 ofs, size_t len)
148 {
149         struct logfs_super *super = logfs_super(sb);
150         int head;
151
152         BUG_ON(super->s_flags & LOGFS_SB_FLAG_RO);
153
154         if (len == 0) {
155                 /* This can happen when the object fit perfectly into a
156                  * segment, the segment gets written per sync and subsequently
157                  * closed.
158                  */
159                 return;
160         }
161         head = ofs & (PAGE_SIZE - 1);
162         if (head) {
163                 ofs -= head;
164                 len += head;
165         }
166         len = PAGE_ALIGN(len);
167         __bdev_writeseg(sb, ofs, ofs >> PAGE_SHIFT, len >> PAGE_SHIFT);
168         generic_unplug_device(bdev_get_queue(logfs_super(sb)->s_bdev));
169 }
170
171
172 static void erase_end_io(struct bio *bio, int err) 
173
174         const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); 
175         struct super_block *sb = bio->bi_private; 
176         struct logfs_super *super = logfs_super(sb); 
177
178         BUG_ON(!uptodate); /* FIXME: Retry io or write elsewhere */ 
179         BUG_ON(err); 
180         BUG_ON(bio->bi_vcnt == 0); 
181         bio_put(bio); 
182         if (atomic_dec_and_test(&super->s_pending_writes))
183                 wake_up(&wq); 
184
185
186 static int do_erase(struct super_block *sb, u64 ofs, pgoff_t index,
187                 size_t nr_pages)
188 {
189         struct logfs_super *super = logfs_super(sb);
190         struct bio *bio;
191         struct request_queue *q = bdev_get_queue(sb->s_bdev);
192         unsigned int max_pages = queue_max_hw_sectors(q) >> (PAGE_SHIFT - 9);
193         int i;
194
195         bio = bio_alloc(GFP_NOFS, max_pages);
196         BUG_ON(!bio); /* FIXME: handle this */
197
198         for (i = 0; i < nr_pages; i++) {
199                 if (i >= max_pages) {
200                         /* Block layer cannot split bios :( */
201                         bio->bi_vcnt = i;
202                         bio->bi_idx = 0;
203                         bio->bi_size = i * PAGE_SIZE;
204                         bio->bi_bdev = super->s_bdev;
205                         bio->bi_sector = ofs >> 9;
206                         bio->bi_private = sb;
207                         bio->bi_end_io = erase_end_io;
208                         atomic_inc(&super->s_pending_writes);
209                         submit_bio(WRITE, bio);
210
211                         ofs += i * PAGE_SIZE;
212                         index += i;
213                         nr_pages -= i;
214                         i = 0;
215
216                         bio = bio_alloc(GFP_NOFS, max_pages);
217                         BUG_ON(!bio);
218                 }
219                 bio->bi_io_vec[i].bv_page = super->s_erase_page;
220                 bio->bi_io_vec[i].bv_len = PAGE_SIZE;
221                 bio->bi_io_vec[i].bv_offset = 0;
222         }
223         bio->bi_vcnt = nr_pages;
224         bio->bi_idx = 0;
225         bio->bi_size = nr_pages * PAGE_SIZE;
226         bio->bi_bdev = super->s_bdev;
227         bio->bi_sector = ofs >> 9;
228         bio->bi_private = sb;
229         bio->bi_end_io = erase_end_io;
230         atomic_inc(&super->s_pending_writes);
231         submit_bio(WRITE, bio);
232         return 0;
233 }
234
235 static int bdev_erase(struct super_block *sb, loff_t to, size_t len,
236                 int ensure_write)
237 {
238         struct logfs_super *super = logfs_super(sb);
239
240         BUG_ON(to & (PAGE_SIZE - 1));
241         BUG_ON(len & (PAGE_SIZE - 1));
242
243         if (super->s_flags & LOGFS_SB_FLAG_RO)
244                 return -EROFS;
245
246         if (ensure_write) {
247                 /*
248                  * Object store doesn't care whether erases happen or not.
249                  * But for the journal they are required.  Otherwise a scan
250                  * can find an old commit entry and assume it is the current
251                  * one, travelling back in time.
252                  */
253                 do_erase(sb, to, to >> PAGE_SHIFT, len >> PAGE_SHIFT);
254         }
255
256         return 0;
257 }
258
259 static void bdev_sync(struct super_block *sb)
260 {
261         struct logfs_super *super = logfs_super(sb);
262
263         wait_event(wq, atomic_read(&super->s_pending_writes) == 0);
264 }
265
266 static struct page *bdev_find_first_sb(struct super_block *sb, u64 *ofs)
267 {
268         struct logfs_super *super = logfs_super(sb);
269         struct address_space *mapping = super->s_mapping_inode->i_mapping;
270         filler_t *filler = bdev_readpage;
271
272         *ofs = 0;
273         return read_cache_page(mapping, 0, filler, sb);
274 }
275
276 static struct page *bdev_find_last_sb(struct super_block *sb, u64 *ofs)
277 {
278         struct logfs_super *super = logfs_super(sb);
279         struct address_space *mapping = super->s_mapping_inode->i_mapping;
280         filler_t *filler = bdev_readpage;
281         u64 pos = (super->s_bdev->bd_inode->i_size & ~0xfffULL) - 0x1000;
282         pgoff_t index = pos >> PAGE_SHIFT;
283
284         *ofs = pos;
285         return read_cache_page(mapping, index, filler, sb);
286 }
287
288 static int bdev_write_sb(struct super_block *sb, struct page *page)
289 {
290         struct block_device *bdev = logfs_super(sb)->s_bdev;
291
292         /* Nothing special to do for block devices. */
293         return sync_request(page, bdev, WRITE);
294 }
295
296 static void bdev_put_device(struct super_block *sb)
297 {
298         close_bdev_exclusive(logfs_super(sb)->s_bdev, FMODE_READ|FMODE_WRITE);
299 }
300
301 static const struct logfs_device_ops bd_devops = {
302         .find_first_sb  = bdev_find_first_sb,
303         .find_last_sb   = bdev_find_last_sb,
304         .write_sb       = bdev_write_sb,
305         .readpage       = bdev_readpage,
306         .writeseg       = bdev_writeseg,
307         .erase          = bdev_erase,
308         .sync           = bdev_sync,
309         .put_device     = bdev_put_device,
310 };
311
312 int logfs_get_sb_bdev(struct file_system_type *type, int flags,
313                 const char *devname, struct vfsmount *mnt)
314 {
315         struct block_device *bdev;
316
317         bdev = open_bdev_exclusive(devname, FMODE_READ|FMODE_WRITE, type);
318         if (IS_ERR(bdev))
319                 return PTR_ERR(bdev);
320
321         if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
322                 int mtdnr = MINOR(bdev->bd_dev);
323                 close_bdev_exclusive(bdev, FMODE_READ|FMODE_WRITE);
324                 return logfs_get_sb_mtd(type, flags, mtdnr, mnt);
325         }
326
327         return logfs_get_sb_device(type, flags, NULL, bdev, &bd_devops, mnt);
328 }