Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/block_dev.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE |
| 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/init.h> |
| 9 | #include <linux/mm.h> |
| 10 | #include <linux/fcntl.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/kmod.h> |
| 13 | #include <linux/major.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/smp_lock.h> |
| 15 | #include <linux/highmem.h> |
| 16 | #include <linux/blkdev.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/blkpg.h> |
| 19 | #include <linux/buffer_head.h> |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 20 | #include <linux/writeback.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/mpage.h> |
| 22 | #include <linux/mount.h> |
| 23 | #include <linux/uio.h> |
| 24 | #include <linux/namei.h> |
Vignesh Babu BM | 1368c4f | 2007-05-08 00:24:32 -0700 | [diff] [blame] | 25 | #include <linux/log2.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <asm/uaccess.h> |
David Howells | 07f3f05 | 2006-09-30 20:52:18 +0200 | [diff] [blame] | 27 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | struct bdev_inode { |
| 30 | struct block_device bdev; |
| 31 | struct inode vfs_inode; |
| 32 | }; |
| 33 | |
Adrian Bunk | 4c54ac6 | 2008-02-18 13:48:31 +0100 | [diff] [blame^] | 34 | static const struct address_space_operations def_blk_aops; |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | static inline struct bdev_inode *BDEV_I(struct inode *inode) |
| 37 | { |
| 38 | return container_of(inode, struct bdev_inode, vfs_inode); |
| 39 | } |
| 40 | |
| 41 | inline struct block_device *I_BDEV(struct inode *inode) |
| 42 | { |
| 43 | return &BDEV_I(inode)->bdev; |
| 44 | } |
| 45 | |
| 46 | EXPORT_SYMBOL(I_BDEV); |
| 47 | |
| 48 | static sector_t max_block(struct block_device *bdev) |
| 49 | { |
| 50 | sector_t retval = ~((sector_t)0); |
| 51 | loff_t sz = i_size_read(bdev->bd_inode); |
| 52 | |
| 53 | if (sz) { |
| 54 | unsigned int size = block_size(bdev); |
| 55 | unsigned int sizebits = blksize_bits(size); |
| 56 | retval = (sz >> sizebits); |
| 57 | } |
| 58 | return retval; |
| 59 | } |
| 60 | |
Peter Zijlstra | f9a1439 | 2007-05-06 14:49:55 -0700 | [diff] [blame] | 61 | /* Kill _all_ buffers and pagecache , dirty or not.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | static void kill_bdev(struct block_device *bdev) |
| 63 | { |
Peter Zijlstra | f9a1439 | 2007-05-06 14:49:55 -0700 | [diff] [blame] | 64 | if (bdev->bd_inode->i_mapping->nrpages == 0) |
| 65 | return; |
| 66 | invalidate_bh_lrus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | truncate_inode_pages(bdev->bd_inode->i_mapping, 0); |
| 68 | } |
| 69 | |
| 70 | int set_blocksize(struct block_device *bdev, int size) |
| 71 | { |
| 72 | /* Size must be a power of two, and between 512 and PAGE_SIZE */ |
Vignesh Babu BM | 1368c4f | 2007-05-08 00:24:32 -0700 | [diff] [blame] | 73 | if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | return -EINVAL; |
| 75 | |
| 76 | /* Size cannot be smaller than the size supported by the device */ |
| 77 | if (size < bdev_hardsect_size(bdev)) |
| 78 | return -EINVAL; |
| 79 | |
| 80 | /* Don't change the size if it is same as current */ |
| 81 | if (bdev->bd_block_size != size) { |
| 82 | sync_blockdev(bdev); |
| 83 | bdev->bd_block_size = size; |
| 84 | bdev->bd_inode->i_blkbits = blksize_bits(size); |
| 85 | kill_bdev(bdev); |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | EXPORT_SYMBOL(set_blocksize); |
| 91 | |
| 92 | int sb_set_blocksize(struct super_block *sb, int size) |
| 93 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | if (set_blocksize(sb->s_bdev, size)) |
| 95 | return 0; |
| 96 | /* If we get here, we know size is power of two |
| 97 | * and it's value is between 512 and PAGE_SIZE */ |
| 98 | sb->s_blocksize = size; |
Coywolf Qi Hunt | 38885bd | 2006-03-24 03:18:05 -0800 | [diff] [blame] | 99 | sb->s_blocksize_bits = blksize_bits(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | return sb->s_blocksize; |
| 101 | } |
| 102 | |
| 103 | EXPORT_SYMBOL(sb_set_blocksize); |
| 104 | |
| 105 | int sb_min_blocksize(struct super_block *sb, int size) |
| 106 | { |
| 107 | int minsize = bdev_hardsect_size(sb->s_bdev); |
| 108 | if (size < minsize) |
| 109 | size = minsize; |
| 110 | return sb_set_blocksize(sb, size); |
| 111 | } |
| 112 | |
| 113 | EXPORT_SYMBOL(sb_min_blocksize); |
| 114 | |
| 115 | static int |
| 116 | blkdev_get_block(struct inode *inode, sector_t iblock, |
| 117 | struct buffer_head *bh, int create) |
| 118 | { |
| 119 | if (iblock >= max_block(I_BDEV(inode))) { |
| 120 | if (create) |
| 121 | return -EIO; |
| 122 | |
| 123 | /* |
| 124 | * for reads, we're just trying to fill a partial page. |
| 125 | * return a hole, they will have to call get_block again |
| 126 | * before they can fill it, and they will get -EIO at that |
| 127 | * time |
| 128 | */ |
| 129 | return 0; |
| 130 | } |
| 131 | bh->b_bdev = I_BDEV(inode); |
| 132 | bh->b_blocknr = iblock; |
| 133 | set_buffer_mapped(bh); |
| 134 | return 0; |
| 135 | } |
| 136 | |
Andrew Morton | b2e895d | 2007-02-03 01:14:01 -0800 | [diff] [blame] | 137 | static int |
| 138 | blkdev_get_blocks(struct inode *inode, sector_t iblock, |
| 139 | struct buffer_head *bh, int create) |
| 140 | { |
| 141 | sector_t end_block = max_block(I_BDEV(inode)); |
| 142 | unsigned long max_blocks = bh->b_size >> inode->i_blkbits; |
| 143 | |
| 144 | if ((iblock + max_blocks) > end_block) { |
| 145 | max_blocks = end_block - iblock; |
| 146 | if ((long)max_blocks <= 0) { |
| 147 | if (create) |
| 148 | return -EIO; /* write fully beyond EOF */ |
| 149 | /* |
| 150 | * It is a read which is fully beyond EOF. We return |
| 151 | * a !buffer_mapped buffer |
| 152 | */ |
| 153 | max_blocks = 0; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | bh->b_bdev = I_BDEV(inode); |
| 158 | bh->b_blocknr = iblock; |
| 159 | bh->b_size = max_blocks << inode->i_blkbits; |
| 160 | if (max_blocks) |
| 161 | set_buffer_mapped(bh); |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static ssize_t |
| 166 | blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, |
| 167 | loff_t offset, unsigned long nr_segs) |
| 168 | { |
| 169 | struct file *file = iocb->ki_filp; |
| 170 | struct inode *inode = file->f_mapping->host; |
| 171 | |
| 172 | return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode), |
| 173 | iov, offset, nr_segs, blkdev_get_blocks, NULL); |
| 174 | } |
| 175 | |
| 176 | #if 0 |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 177 | static void blk_end_aio(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Chen, Kenneth W | e61c901 | 2006-12-13 00:34:36 -0800 | [diff] [blame] | 179 | struct kiocb *iocb = bio->bi_private; |
| 180 | atomic_t *bio_count = &iocb->ki_bio_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Chen, Kenneth W | e61c901 | 2006-12-13 00:34:36 -0800 | [diff] [blame] | 182 | if (bio_data_dir(bio) == READ) |
| 183 | bio_check_pages_dirty(bio); |
| 184 | else { |
| 185 | bio_release_pages(bio); |
| 186 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Chen, Kenneth W | e61c901 | 2006-12-13 00:34:36 -0800 | [diff] [blame] | 189 | /* iocb->ki_nbytes stores error code from LLDD */ |
| 190 | if (error) |
| 191 | iocb->ki_nbytes = -EIO; |
| 192 | |
| 193 | if (atomic_dec_and_test(bio_count)) { |
Andrew Morton | 790816d | 2007-01-22 20:40:35 -0800 | [diff] [blame] | 194 | if ((long)iocb->ki_nbytes < 0) |
Chen, Kenneth W | e61c901 | 2006-12-13 00:34:36 -0800 | [diff] [blame] | 195 | aio_complete(iocb, iocb->ki_nbytes, 0); |
| 196 | else |
| 197 | aio_complete(iocb, iocb->ki_left, 0); |
| 198 | } |
| 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
Chen, Kenneth W | e61c901 | 2006-12-13 00:34:36 -0800 | [diff] [blame] | 203 | #define VEC_SIZE 16 |
| 204 | struct pvec { |
| 205 | unsigned short nr; |
| 206 | unsigned short idx; |
| 207 | struct page *page[VEC_SIZE]; |
| 208 | }; |
| 209 | |
| 210 | #define PAGES_SPANNED(addr, len) \ |
| 211 | (DIV_ROUND_UP((addr) + (len), PAGE_SIZE) - (addr) / PAGE_SIZE); |
| 212 | |
| 213 | /* |
| 214 | * get page pointer for user addr, we internally cache struct page array for |
| 215 | * (addr, count) range in pvec to avoid frequent call to get_user_pages. If |
| 216 | * internal page list is exhausted, a batch count of up to VEC_SIZE is used |
| 217 | * to get next set of page struct. |
| 218 | */ |
| 219 | static struct page *blk_get_page(unsigned long addr, size_t count, int rw, |
| 220 | struct pvec *pvec) |
| 221 | { |
| 222 | int ret, nr_pages; |
| 223 | if (pvec->idx == pvec->nr) { |
| 224 | nr_pages = PAGES_SPANNED(addr, count); |
| 225 | nr_pages = min(nr_pages, VEC_SIZE); |
| 226 | down_read(¤t->mm->mmap_sem); |
| 227 | ret = get_user_pages(current, current->mm, addr, nr_pages, |
| 228 | rw == READ, 0, pvec->page, NULL); |
| 229 | up_read(¤t->mm->mmap_sem); |
| 230 | if (ret < 0) |
| 231 | return ERR_PTR(ret); |
| 232 | pvec->nr = ret; |
| 233 | pvec->idx = 0; |
| 234 | } |
| 235 | return pvec->page[pvec->idx++]; |
| 236 | } |
| 237 | |
Chen, Kenneth W | cda9205 | 2007-01-22 20:40:43 -0800 | [diff] [blame] | 238 | /* return a page back to pvec array */ |
| 239 | static void blk_unget_page(struct page *page, struct pvec *pvec) |
| 240 | { |
| 241 | pvec->page[--pvec->idx] = page; |
| 242 | } |
| 243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | static ssize_t |
| 245 | blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, |
Chen, Kenneth W | e61c901 | 2006-12-13 00:34:36 -0800 | [diff] [blame] | 246 | loff_t pos, unsigned long nr_segs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
Chen, Kenneth W | e61c901 | 2006-12-13 00:34:36 -0800 | [diff] [blame] | 248 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
| 249 | unsigned blkbits = blksize_bits(bdev_hardsect_size(I_BDEV(inode))); |
| 250 | unsigned blocksize_mask = (1 << blkbits) - 1; |
| 251 | unsigned long seg = 0; /* iov segment iterator */ |
| 252 | unsigned long nvec; /* number of bio vec needed */ |
| 253 | unsigned long cur_off; /* offset into current page */ |
| 254 | unsigned long cur_len; /* I/O len of current page, up to PAGE_SIZE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Chen, Kenneth W | e61c901 | 2006-12-13 00:34:36 -0800 | [diff] [blame] | 256 | unsigned long addr; /* user iovec address */ |
| 257 | size_t count; /* user iovec len */ |
| 258 | size_t nbytes = iocb->ki_nbytes = iocb->ki_left; /* total xfer size */ |
| 259 | loff_t size; /* size of block device */ |
| 260 | struct bio *bio; |
| 261 | atomic_t *bio_count = &iocb->ki_bio_count; |
| 262 | struct page *page; |
| 263 | struct pvec pvec; |
| 264 | |
| 265 | pvec.nr = 0; |
| 266 | pvec.idx = 0; |
| 267 | |
| 268 | if (pos & blocksize_mask) |
| 269 | return -EINVAL; |
| 270 | |
| 271 | size = i_size_read(inode); |
| 272 | if (pos + nbytes > size) { |
| 273 | nbytes = size - pos; |
| 274 | iocb->ki_left = nbytes; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * check first non-zero iov alignment, the remaining |
| 279 | * iov alignment is checked inside bio loop below. |
| 280 | */ |
| 281 | do { |
| 282 | addr = (unsigned long) iov[seg].iov_base; |
| 283 | count = min(iov[seg].iov_len, nbytes); |
| 284 | if (addr & blocksize_mask || count & blocksize_mask) |
| 285 | return -EINVAL; |
| 286 | } while (!count && ++seg < nr_segs); |
| 287 | atomic_set(bio_count, 1); |
| 288 | |
| 289 | while (nbytes) { |
| 290 | /* roughly estimate number of bio vec needed */ |
| 291 | nvec = (nbytes + PAGE_SIZE - 1) / PAGE_SIZE; |
| 292 | nvec = max(nvec, nr_segs - seg); |
| 293 | nvec = min(nvec, (unsigned long) BIO_MAX_PAGES); |
| 294 | |
| 295 | /* bio_alloc should not fail with GFP_KERNEL flag */ |
| 296 | bio = bio_alloc(GFP_KERNEL, nvec); |
| 297 | bio->bi_bdev = I_BDEV(inode); |
| 298 | bio->bi_end_io = blk_end_aio; |
| 299 | bio->bi_private = iocb; |
| 300 | bio->bi_sector = pos >> blkbits; |
| 301 | same_bio: |
| 302 | cur_off = addr & ~PAGE_MASK; |
| 303 | cur_len = PAGE_SIZE - cur_off; |
| 304 | if (count < cur_len) |
| 305 | cur_len = count; |
| 306 | |
| 307 | page = blk_get_page(addr, count, rw, &pvec); |
| 308 | if (unlikely(IS_ERR(page))) |
| 309 | goto backout; |
| 310 | |
| 311 | if (bio_add_page(bio, page, cur_len, cur_off)) { |
| 312 | pos += cur_len; |
| 313 | addr += cur_len; |
| 314 | count -= cur_len; |
| 315 | nbytes -= cur_len; |
| 316 | |
| 317 | if (count) |
| 318 | goto same_bio; |
| 319 | while (++seg < nr_segs) { |
| 320 | addr = (unsigned long) iov[seg].iov_base; |
| 321 | count = iov[seg].iov_len; |
| 322 | if (!count) |
| 323 | continue; |
| 324 | if (unlikely(addr & blocksize_mask || |
| 325 | count & blocksize_mask)) { |
| 326 | page = ERR_PTR(-EINVAL); |
| 327 | goto backout; |
| 328 | } |
| 329 | count = min(count, nbytes); |
| 330 | goto same_bio; |
| 331 | } |
Chen, Kenneth W | cda9205 | 2007-01-22 20:40:43 -0800 | [diff] [blame] | 332 | } else { |
| 333 | blk_unget_page(page, &pvec); |
Chen, Kenneth W | e61c901 | 2006-12-13 00:34:36 -0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* bio is ready, submit it */ |
| 337 | if (rw == READ) |
| 338 | bio_set_pages_dirty(bio); |
| 339 | atomic_inc(bio_count); |
| 340 | submit_bio(rw, bio); |
| 341 | } |
| 342 | |
| 343 | completion: |
| 344 | iocb->ki_left -= nbytes; |
| 345 | nbytes = iocb->ki_left; |
| 346 | iocb->ki_pos += nbytes; |
| 347 | |
| 348 | blk_run_address_space(inode->i_mapping); |
| 349 | if (atomic_dec_and_test(bio_count)) |
| 350 | aio_complete(iocb, nbytes, 0); |
| 351 | |
| 352 | return -EIOCBQUEUED; |
| 353 | |
| 354 | backout: |
| 355 | /* |
| 356 | * back out nbytes count constructed so far for this bio, |
| 357 | * we will throw away current bio. |
| 358 | */ |
| 359 | nbytes += bio->bi_size; |
| 360 | bio_release_pages(bio); |
| 361 | bio_put(bio); |
| 362 | |
| 363 | /* |
| 364 | * if no bio was submmitted, return the error code. |
| 365 | * otherwise, proceed with pending I/O completion. |
| 366 | */ |
| 367 | if (atomic_read(bio_count) == 1) |
| 368 | return PTR_ERR(page); |
| 369 | goto completion; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
Andrew Morton | b2e895d | 2007-02-03 01:14:01 -0800 | [diff] [blame] | 371 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | static int blkdev_writepage(struct page *page, struct writeback_control *wbc) |
| 374 | { |
| 375 | return block_write_full_page(page, blkdev_get_block, wbc); |
| 376 | } |
| 377 | |
| 378 | static int blkdev_readpage(struct file * file, struct page * page) |
| 379 | { |
| 380 | return block_read_full_page(page, blkdev_get_block); |
| 381 | } |
| 382 | |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 383 | static int blkdev_write_begin(struct file *file, struct address_space *mapping, |
| 384 | loff_t pos, unsigned len, unsigned flags, |
| 385 | struct page **pagep, void **fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 387 | *pagep = NULL; |
| 388 | return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, |
| 389 | blkdev_get_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 392 | static int blkdev_write_end(struct file *file, struct address_space *mapping, |
| 393 | loff_t pos, unsigned len, unsigned copied, |
| 394 | struct page *page, void *fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | { |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 396 | int ret; |
| 397 | ret = block_write_end(file, mapping, pos, len, copied, page, fsdata); |
| 398 | |
| 399 | unlock_page(page); |
| 400 | page_cache_release(page); |
| 401 | |
| 402 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /* |
| 406 | * private llseek: |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 407 | * for a block special file file->f_path.dentry->d_inode->i_size is zero |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | * so we compute the size by hand (just as in block_read/write above) |
| 409 | */ |
| 410 | static loff_t block_llseek(struct file *file, loff_t offset, int origin) |
| 411 | { |
| 412 | struct inode *bd_inode = file->f_mapping->host; |
| 413 | loff_t size; |
| 414 | loff_t retval; |
| 415 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 416 | mutex_lock(&bd_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | size = i_size_read(bd_inode); |
| 418 | |
| 419 | switch (origin) { |
| 420 | case 2: |
| 421 | offset += size; |
| 422 | break; |
| 423 | case 1: |
| 424 | offset += file->f_pos; |
| 425 | } |
| 426 | retval = -EINVAL; |
| 427 | if (offset >= 0 && offset <= size) { |
| 428 | if (offset != file->f_pos) { |
| 429 | file->f_pos = offset; |
| 430 | } |
| 431 | retval = offset; |
| 432 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 433 | mutex_unlock(&bd_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | return retval; |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * Filp is never NULL; the only case when ->fsync() is called with |
| 439 | * NULL first argument is nfsd_sync_dir() and that's not a directory. |
| 440 | */ |
| 441 | |
| 442 | static int block_fsync(struct file *filp, struct dentry *dentry, int datasync) |
| 443 | { |
| 444 | return sync_blockdev(I_BDEV(filp->f_mapping->host)); |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * pseudo-fs |
| 449 | */ |
| 450 | |
| 451 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock); |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 452 | static struct kmem_cache * bdev_cachep __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
| 454 | static struct inode *bdev_alloc_inode(struct super_block *sb) |
| 455 | { |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 456 | struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | if (!ei) |
| 458 | return NULL; |
| 459 | return &ei->vfs_inode; |
| 460 | } |
| 461 | |
| 462 | static void bdev_destroy_inode(struct inode *inode) |
| 463 | { |
| 464 | struct bdev_inode *bdi = BDEV_I(inode); |
| 465 | |
| 466 | bdi->bdev.bd_inode_backing_dev_info = NULL; |
| 467 | kmem_cache_free(bdev_cachep, bdi); |
| 468 | } |
| 469 | |
Christoph Lameter | 4ba9b9d | 2007-10-16 23:25:51 -0700 | [diff] [blame] | 470 | static void init_once(struct kmem_cache * cachep, void *foo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
| 472 | struct bdev_inode *ei = (struct bdev_inode *) foo; |
| 473 | struct block_device *bdev = &ei->bdev; |
| 474 | |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 475 | memset(bdev, 0, sizeof(*bdev)); |
| 476 | mutex_init(&bdev->bd_mutex); |
| 477 | sema_init(&bdev->bd_mount_sem, 1); |
| 478 | INIT_LIST_HEAD(&bdev->bd_inodes); |
| 479 | INIT_LIST_HEAD(&bdev->bd_list); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 480 | #ifdef CONFIG_SYSFS |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 481 | INIT_LIST_HEAD(&bdev->bd_holder_list); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 482 | #endif |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 483 | inode_init_once(&ei->vfs_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | static inline void __bd_forget(struct inode *inode) |
| 487 | { |
| 488 | list_del_init(&inode->i_devices); |
| 489 | inode->i_bdev = NULL; |
| 490 | inode->i_mapping = &inode->i_data; |
| 491 | } |
| 492 | |
| 493 | static void bdev_clear_inode(struct inode *inode) |
| 494 | { |
| 495 | struct block_device *bdev = &BDEV_I(inode)->bdev; |
| 496 | struct list_head *p; |
| 497 | spin_lock(&bdev_lock); |
| 498 | while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) { |
| 499 | __bd_forget(list_entry(p, struct inode, i_devices)); |
| 500 | } |
| 501 | list_del_init(&bdev->bd_list); |
| 502 | spin_unlock(&bdev_lock); |
| 503 | } |
| 504 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 505 | static const struct super_operations bdev_sops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | .statfs = simple_statfs, |
| 507 | .alloc_inode = bdev_alloc_inode, |
| 508 | .destroy_inode = bdev_destroy_inode, |
| 509 | .drop_inode = generic_delete_inode, |
| 510 | .clear_inode = bdev_clear_inode, |
| 511 | }; |
| 512 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 513 | static int bd_get_sb(struct file_system_type *fs_type, |
| 514 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | { |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 516 | return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | static struct file_system_type bd_type = { |
| 520 | .name = "bdev", |
| 521 | .get_sb = bd_get_sb, |
| 522 | .kill_sb = kill_anon_super, |
| 523 | }; |
| 524 | |
Eric Dumazet | fa3536c | 2006-03-26 01:37:24 -0800 | [diff] [blame] | 525 | static struct vfsmount *bd_mnt __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | struct super_block *blockdev_superblock; |
| 527 | |
| 528 | void __init bdev_cache_init(void) |
| 529 | { |
| 530 | int err; |
| 531 | bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode), |
Paul Jackson | fffb60f | 2006-03-24 03:16:06 -0800 | [diff] [blame] | 532 | 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
| 533 | SLAB_MEM_SPREAD|SLAB_PANIC), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 534 | init_once); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | err = register_filesystem(&bd_type); |
| 536 | if (err) |
| 537 | panic("Cannot register bdev pseudo-fs"); |
| 538 | bd_mnt = kern_mount(&bd_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | if (IS_ERR(bd_mnt)) |
| 540 | panic("Cannot create bdev pseudo-fs"); |
| 541 | blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */ |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * Most likely _very_ bad one - but then it's hardly critical for small |
| 546 | * /dev and can be fixed when somebody will need really large one. |
| 547 | * Keep in mind that it will be fed through icache hash function too. |
| 548 | */ |
| 549 | static inline unsigned long hash(dev_t dev) |
| 550 | { |
| 551 | return MAJOR(dev)+MINOR(dev); |
| 552 | } |
| 553 | |
| 554 | static int bdev_test(struct inode *inode, void *data) |
| 555 | { |
| 556 | return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data; |
| 557 | } |
| 558 | |
| 559 | static int bdev_set(struct inode *inode, void *data) |
| 560 | { |
| 561 | BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data; |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | static LIST_HEAD(all_bdevs); |
| 566 | |
| 567 | struct block_device *bdget(dev_t dev) |
| 568 | { |
| 569 | struct block_device *bdev; |
| 570 | struct inode *inode; |
| 571 | |
| 572 | inode = iget5_locked(bd_mnt->mnt_sb, hash(dev), |
| 573 | bdev_test, bdev_set, &dev); |
| 574 | |
| 575 | if (!inode) |
| 576 | return NULL; |
| 577 | |
| 578 | bdev = &BDEV_I(inode)->bdev; |
| 579 | |
| 580 | if (inode->i_state & I_NEW) { |
| 581 | bdev->bd_contains = NULL; |
| 582 | bdev->bd_inode = inode; |
| 583 | bdev->bd_block_size = (1 << inode->i_blkbits); |
| 584 | bdev->bd_part_count = 0; |
| 585 | bdev->bd_invalidated = 0; |
| 586 | inode->i_mode = S_IFBLK; |
| 587 | inode->i_rdev = dev; |
| 588 | inode->i_bdev = bdev; |
| 589 | inode->i_data.a_ops = &def_blk_aops; |
| 590 | mapping_set_gfp_mask(&inode->i_data, GFP_USER); |
| 591 | inode->i_data.backing_dev_info = &default_backing_dev_info; |
| 592 | spin_lock(&bdev_lock); |
| 593 | list_add(&bdev->bd_list, &all_bdevs); |
| 594 | spin_unlock(&bdev_lock); |
| 595 | unlock_new_inode(inode); |
| 596 | } |
| 597 | return bdev; |
| 598 | } |
| 599 | |
| 600 | EXPORT_SYMBOL(bdget); |
| 601 | |
| 602 | long nr_blockdev_pages(void) |
| 603 | { |
Matthias Kaehlcke | 203a293 | 2007-07-15 23:40:23 -0700 | [diff] [blame] | 604 | struct block_device *bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | long ret = 0; |
| 606 | spin_lock(&bdev_lock); |
Matthias Kaehlcke | 203a293 | 2007-07-15 23:40:23 -0700 | [diff] [blame] | 607 | list_for_each_entry(bdev, &all_bdevs, bd_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | ret += bdev->bd_inode->i_mapping->nrpages; |
| 609 | } |
| 610 | spin_unlock(&bdev_lock); |
| 611 | return ret; |
| 612 | } |
| 613 | |
| 614 | void bdput(struct block_device *bdev) |
| 615 | { |
| 616 | iput(bdev->bd_inode); |
| 617 | } |
| 618 | |
| 619 | EXPORT_SYMBOL(bdput); |
| 620 | |
| 621 | static struct block_device *bd_acquire(struct inode *inode) |
| 622 | { |
| 623 | struct block_device *bdev; |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | spin_lock(&bdev_lock); |
| 626 | bdev = inode->i_bdev; |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 627 | if (bdev) { |
| 628 | atomic_inc(&bdev->bd_inode->i_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | spin_unlock(&bdev_lock); |
| 630 | return bdev; |
| 631 | } |
| 632 | spin_unlock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | bdev = bdget(inode->i_rdev); |
| 635 | if (bdev) { |
| 636 | spin_lock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 637 | if (!inode->i_bdev) { |
| 638 | /* |
| 639 | * We take an additional bd_inode->i_count for inode, |
| 640 | * and it's released in clear_inode() of inode. |
| 641 | * So, we can access it via ->i_mapping always |
| 642 | * without igrab(). |
| 643 | */ |
| 644 | atomic_inc(&bdev->bd_inode->i_count); |
| 645 | inode->i_bdev = bdev; |
| 646 | inode->i_mapping = bdev->bd_inode->i_mapping; |
| 647 | list_add(&inode->i_devices, &bdev->bd_inodes); |
| 648 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | spin_unlock(&bdev_lock); |
| 650 | } |
| 651 | return bdev; |
| 652 | } |
| 653 | |
| 654 | /* Call when you free inode */ |
| 655 | |
| 656 | void bd_forget(struct inode *inode) |
| 657 | { |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 658 | struct block_device *bdev = NULL; |
| 659 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | spin_lock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 661 | if (inode->i_bdev) { |
| 662 | if (inode->i_sb != blockdev_superblock) |
| 663 | bdev = inode->i_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | __bd_forget(inode); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 665 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | spin_unlock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 667 | |
| 668 | if (bdev) |
| 669 | iput(bdev->bd_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | int bd_claim(struct block_device *bdev, void *holder) |
| 673 | { |
| 674 | int res; |
| 675 | spin_lock(&bdev_lock); |
| 676 | |
| 677 | /* first decide result */ |
| 678 | if (bdev->bd_holder == holder) |
| 679 | res = 0; /* already a holder */ |
| 680 | else if (bdev->bd_holder != NULL) |
| 681 | res = -EBUSY; /* held by someone else */ |
| 682 | else if (bdev->bd_contains == bdev) |
| 683 | res = 0; /* is a whole device which isn't held */ |
| 684 | |
| 685 | else if (bdev->bd_contains->bd_holder == bd_claim) |
| 686 | res = 0; /* is a partition of a device that is being partitioned */ |
| 687 | else if (bdev->bd_contains->bd_holder != NULL) |
| 688 | res = -EBUSY; /* is a partition of a held device */ |
| 689 | else |
| 690 | res = 0; /* is a partition of an un-held device */ |
| 691 | |
| 692 | /* now impose change */ |
| 693 | if (res==0) { |
| 694 | /* note that for a whole device bd_holders |
| 695 | * will be incremented twice, and bd_holder will |
| 696 | * be set to bd_claim before being set to holder |
| 697 | */ |
| 698 | bdev->bd_contains->bd_holders ++; |
| 699 | bdev->bd_contains->bd_holder = bd_claim; |
| 700 | bdev->bd_holders++; |
| 701 | bdev->bd_holder = holder; |
| 702 | } |
| 703 | spin_unlock(&bdev_lock); |
| 704 | return res; |
| 705 | } |
| 706 | |
| 707 | EXPORT_SYMBOL(bd_claim); |
| 708 | |
| 709 | void bd_release(struct block_device *bdev) |
| 710 | { |
| 711 | spin_lock(&bdev_lock); |
| 712 | if (!--bdev->bd_contains->bd_holders) |
| 713 | bdev->bd_contains->bd_holder = NULL; |
| 714 | if (!--bdev->bd_holders) |
| 715 | bdev->bd_holder = NULL; |
| 716 | spin_unlock(&bdev_lock); |
| 717 | } |
| 718 | |
| 719 | EXPORT_SYMBOL(bd_release); |
| 720 | |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 721 | #ifdef CONFIG_SYSFS |
| 722 | /* |
| 723 | * Functions for bd_claim_by_kobject / bd_release_from_kobject |
| 724 | * |
| 725 | * If a kobject is passed to bd_claim_by_kobject() |
| 726 | * and the kobject has a parent directory, |
| 727 | * following symlinks are created: |
| 728 | * o from the kobject to the claimed bdev |
| 729 | * o from "holders" directory of the bdev to the parent of the kobject |
| 730 | * bd_release_from_kobject() removes these symlinks. |
| 731 | * |
| 732 | * Example: |
| 733 | * If /dev/dm-0 maps to /dev/sda, kobject corresponding to |
| 734 | * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then: |
| 735 | * /sys/block/dm-0/slaves/sda --> /sys/block/sda |
| 736 | * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0 |
| 737 | */ |
| 738 | |
| 739 | static struct kobject *bdev_get_kobj(struct block_device *bdev) |
| 740 | { |
| 741 | if (bdev->bd_contains != bdev) |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 742 | return kobject_get(&bdev->bd_part->dev.kobj); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 743 | else |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 744 | return kobject_get(&bdev->bd_disk->dev.kobj); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | static struct kobject *bdev_get_holder(struct block_device *bdev) |
| 748 | { |
| 749 | if (bdev->bd_contains != bdev) |
| 750 | return kobject_get(bdev->bd_part->holder_dir); |
| 751 | else |
| 752 | return kobject_get(bdev->bd_disk->holder_dir); |
| 753 | } |
| 754 | |
Andrew Morton | 4d7dd8fd | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 755 | static int add_symlink(struct kobject *from, struct kobject *to) |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 756 | { |
| 757 | if (!from || !to) |
Andrew Morton | 4d7dd8fd | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 758 | return 0; |
| 759 | return sysfs_create_link(from, to, kobject_name(to)); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | static void del_symlink(struct kobject *from, struct kobject *to) |
| 763 | { |
| 764 | if (!from || !to) |
| 765 | return; |
| 766 | sysfs_remove_link(from, kobject_name(to)); |
| 767 | } |
| 768 | |
| 769 | /* |
| 770 | * 'struct bd_holder' contains pointers to kobjects symlinked by |
| 771 | * bd_claim_by_kobject. |
| 772 | * It's connected to bd_holder_list which is protected by bdev->bd_sem. |
| 773 | */ |
| 774 | struct bd_holder { |
| 775 | struct list_head list; /* chain of holders of the bdev */ |
| 776 | int count; /* references from the holder */ |
| 777 | struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */ |
| 778 | struct kobject *hdev; /* e.g. "/block/dm-0" */ |
| 779 | struct kobject *hdir; /* e.g. "/block/sda/holders" */ |
| 780 | struct kobject *sdev; /* e.g. "/block/sda" */ |
| 781 | }; |
| 782 | |
| 783 | /* |
| 784 | * Get references of related kobjects at once. |
| 785 | * Returns 1 on success. 0 on failure. |
| 786 | * |
| 787 | * Should call bd_holder_release_dirs() after successful use. |
| 788 | */ |
| 789 | static int bd_holder_grab_dirs(struct block_device *bdev, |
| 790 | struct bd_holder *bo) |
| 791 | { |
| 792 | if (!bdev || !bo) |
| 793 | return 0; |
| 794 | |
| 795 | bo->sdir = kobject_get(bo->sdir); |
| 796 | if (!bo->sdir) |
| 797 | return 0; |
| 798 | |
| 799 | bo->hdev = kobject_get(bo->sdir->parent); |
| 800 | if (!bo->hdev) |
| 801 | goto fail_put_sdir; |
| 802 | |
| 803 | bo->sdev = bdev_get_kobj(bdev); |
| 804 | if (!bo->sdev) |
| 805 | goto fail_put_hdev; |
| 806 | |
| 807 | bo->hdir = bdev_get_holder(bdev); |
| 808 | if (!bo->hdir) |
| 809 | goto fail_put_sdev; |
| 810 | |
| 811 | return 1; |
| 812 | |
| 813 | fail_put_sdev: |
| 814 | kobject_put(bo->sdev); |
| 815 | fail_put_hdev: |
| 816 | kobject_put(bo->hdev); |
| 817 | fail_put_sdir: |
| 818 | kobject_put(bo->sdir); |
| 819 | |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | /* Put references of related kobjects at once. */ |
| 824 | static void bd_holder_release_dirs(struct bd_holder *bo) |
| 825 | { |
| 826 | kobject_put(bo->hdir); |
| 827 | kobject_put(bo->sdev); |
| 828 | kobject_put(bo->hdev); |
| 829 | kobject_put(bo->sdir); |
| 830 | } |
| 831 | |
| 832 | static struct bd_holder *alloc_bd_holder(struct kobject *kobj) |
| 833 | { |
| 834 | struct bd_holder *bo; |
| 835 | |
| 836 | bo = kzalloc(sizeof(*bo), GFP_KERNEL); |
| 837 | if (!bo) |
| 838 | return NULL; |
| 839 | |
| 840 | bo->count = 1; |
| 841 | bo->sdir = kobj; |
| 842 | |
| 843 | return bo; |
| 844 | } |
| 845 | |
| 846 | static void free_bd_holder(struct bd_holder *bo) |
| 847 | { |
| 848 | kfree(bo); |
| 849 | } |
| 850 | |
| 851 | /** |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 852 | * find_bd_holder - find matching struct bd_holder from the block device |
| 853 | * |
| 854 | * @bdev: struct block device to be searched |
| 855 | * @bo: target struct bd_holder |
| 856 | * |
| 857 | * Returns matching entry with @bo in @bdev->bd_holder_list. |
| 858 | * If found, increment the reference count and return the pointer. |
| 859 | * If not found, returns NULL. |
| 860 | */ |
Andrew Morton | 36a561d | 2006-10-30 22:07:03 -0800 | [diff] [blame] | 861 | static struct bd_holder *find_bd_holder(struct block_device *bdev, |
| 862 | struct bd_holder *bo) |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 863 | { |
| 864 | struct bd_holder *tmp; |
| 865 | |
| 866 | list_for_each_entry(tmp, &bdev->bd_holder_list, list) |
| 867 | if (tmp->sdir == bo->sdir) { |
| 868 | tmp->count++; |
| 869 | return tmp; |
| 870 | } |
| 871 | |
| 872 | return NULL; |
| 873 | } |
| 874 | |
| 875 | /** |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 876 | * add_bd_holder - create sysfs symlinks for bd_claim() relationship |
| 877 | * |
| 878 | * @bdev: block device to be bd_claimed |
| 879 | * @bo: preallocated and initialized by alloc_bd_holder() |
| 880 | * |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 881 | * Add @bo to @bdev->bd_holder_list, create symlinks. |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 882 | * |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 883 | * Returns 0 if symlinks are created. |
| 884 | * Returns -ve if something fails. |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 885 | */ |
| 886 | static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo) |
| 887 | { |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 888 | int err; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 889 | |
| 890 | if (!bo) |
Andrew Morton | 4d7dd8fd | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 891 | return -EINVAL; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 892 | |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 893 | if (!bd_holder_grab_dirs(bdev, bo)) |
Andrew Morton | 4d7dd8fd | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 894 | return -EBUSY; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 895 | |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 896 | err = add_symlink(bo->sdir, bo->sdev); |
| 897 | if (err) |
| 898 | return err; |
| 899 | |
| 900 | err = add_symlink(bo->hdir, bo->hdev); |
| 901 | if (err) { |
| 902 | del_symlink(bo->sdir, bo->sdev); |
| 903 | return err; |
Andrew Morton | 4d7dd8fd | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 904 | } |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 905 | |
| 906 | list_add_tail(&bo->list, &bdev->bd_holder_list); |
| 907 | return 0; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | /** |
| 911 | * del_bd_holder - delete sysfs symlinks for bd_claim() relationship |
| 912 | * |
| 913 | * @bdev: block device to be bd_claimed |
| 914 | * @kobj: holder's kobject |
| 915 | * |
| 916 | * If there is matching entry with @kobj in @bdev->bd_holder_list |
| 917 | * and no other bd_claim() from the same kobject, |
| 918 | * remove the struct bd_holder from the list, delete symlinks for it. |
| 919 | * |
| 920 | * Returns a pointer to the struct bd_holder when it's removed from the list |
| 921 | * and ready to be freed. |
| 922 | * Returns NULL if matching claim isn't found or there is other bd_claim() |
| 923 | * by the same kobject. |
| 924 | */ |
| 925 | static struct bd_holder *del_bd_holder(struct block_device *bdev, |
| 926 | struct kobject *kobj) |
| 927 | { |
| 928 | struct bd_holder *bo; |
| 929 | |
| 930 | list_for_each_entry(bo, &bdev->bd_holder_list, list) { |
| 931 | if (bo->sdir == kobj) { |
| 932 | bo->count--; |
| 933 | BUG_ON(bo->count < 0); |
| 934 | if (!bo->count) { |
| 935 | list_del(&bo->list); |
| 936 | del_symlink(bo->sdir, bo->sdev); |
| 937 | del_symlink(bo->hdir, bo->hdev); |
| 938 | bd_holder_release_dirs(bo); |
| 939 | return bo; |
| 940 | } |
| 941 | break; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | return NULL; |
| 946 | } |
| 947 | |
| 948 | /** |
| 949 | * bd_claim_by_kobject - bd_claim() with additional kobject signature |
| 950 | * |
| 951 | * @bdev: block device to be claimed |
| 952 | * @holder: holder's signature |
| 953 | * @kobj: holder's kobject |
| 954 | * |
| 955 | * Do bd_claim() and if it succeeds, create sysfs symlinks between |
| 956 | * the bdev and the holder's kobject. |
| 957 | * Use bd_release_from_kobject() when relesing the claimed bdev. |
| 958 | * |
| 959 | * Returns 0 on success. (same as bd_claim()) |
| 960 | * Returns errno on failure. |
| 961 | */ |
| 962 | static int bd_claim_by_kobject(struct block_device *bdev, void *holder, |
| 963 | struct kobject *kobj) |
| 964 | { |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 965 | int err; |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 966 | struct bd_holder *bo, *found; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 967 | |
| 968 | if (!kobj) |
| 969 | return -EINVAL; |
| 970 | |
| 971 | bo = alloc_bd_holder(kobj); |
| 972 | if (!bo) |
| 973 | return -ENOMEM; |
| 974 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 975 | mutex_lock(&bdev->bd_mutex); |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 976 | |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 977 | err = bd_claim(bdev, holder); |
| 978 | if (err) |
Andrew Morton | 4210df2 | 2007-07-15 23:41:28 -0700 | [diff] [blame] | 979 | goto fail; |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 980 | |
| 981 | found = find_bd_holder(bdev, bo); |
| 982 | if (found) |
Andrew Morton | 4210df2 | 2007-07-15 23:41:28 -0700 | [diff] [blame] | 983 | goto fail; |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 984 | |
| 985 | err = add_bd_holder(bdev, bo); |
| 986 | if (err) |
| 987 | bd_release(bdev); |
Andrew Morton | 4210df2 | 2007-07-15 23:41:28 -0700 | [diff] [blame] | 988 | else |
| 989 | bo = NULL; |
| 990 | fail: |
Jun'ichi Nomura | b4cf1b7 | 2006-03-27 01:18:00 -0800 | [diff] [blame] | 991 | mutex_unlock(&bdev->bd_mutex); |
Andrew Morton | 4210df2 | 2007-07-15 23:41:28 -0700 | [diff] [blame] | 992 | free_bd_holder(bo); |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 993 | return err; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /** |
| 997 | * bd_release_from_kobject - bd_release() with additional kobject signature |
| 998 | * |
| 999 | * @bdev: block device to be released |
| 1000 | * @kobj: holder's kobject |
| 1001 | * |
| 1002 | * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject(). |
| 1003 | */ |
| 1004 | static void bd_release_from_kobject(struct block_device *bdev, |
| 1005 | struct kobject *kobj) |
| 1006 | { |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1007 | if (!kobj) |
| 1008 | return; |
| 1009 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1010 | mutex_lock(&bdev->bd_mutex); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1011 | bd_release(bdev); |
Andrew Morton | 4210df2 | 2007-07-15 23:41:28 -0700 | [diff] [blame] | 1012 | free_bd_holder(del_bd_holder(bdev, kobj)); |
Jun'ichi Nomura | b4cf1b7 | 2006-03-27 01:18:00 -0800 | [diff] [blame] | 1013 | mutex_unlock(&bdev->bd_mutex); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | /** |
| 1017 | * bd_claim_by_disk - wrapper function for bd_claim_by_kobject() |
| 1018 | * |
| 1019 | * @bdev: block device to be claimed |
| 1020 | * @holder: holder's signature |
| 1021 | * @disk: holder's gendisk |
| 1022 | * |
| 1023 | * Call bd_claim_by_kobject() with getting @disk->slave_dir. |
| 1024 | */ |
| 1025 | int bd_claim_by_disk(struct block_device *bdev, void *holder, |
| 1026 | struct gendisk *disk) |
| 1027 | { |
| 1028 | return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir)); |
| 1029 | } |
| 1030 | EXPORT_SYMBOL_GPL(bd_claim_by_disk); |
| 1031 | |
| 1032 | /** |
| 1033 | * bd_release_from_disk - wrapper function for bd_release_from_kobject() |
| 1034 | * |
| 1035 | * @bdev: block device to be claimed |
| 1036 | * @disk: holder's gendisk |
| 1037 | * |
| 1038 | * Call bd_release_from_kobject() and put @disk->slave_dir. |
| 1039 | */ |
| 1040 | void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk) |
| 1041 | { |
| 1042 | bd_release_from_kobject(bdev, disk->slave_dir); |
| 1043 | kobject_put(disk->slave_dir); |
| 1044 | } |
| 1045 | EXPORT_SYMBOL_GPL(bd_release_from_disk); |
| 1046 | #endif |
| 1047 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | /* |
| 1049 | * Tries to open block device by device number. Use it ONLY if you |
| 1050 | * really do not have anything better - i.e. when you are behind a |
| 1051 | * truly sucky interface and all you are given is a device number. _Never_ |
| 1052 | * to be used for internal purposes. If you ever need it - reconsider |
| 1053 | * your API. |
| 1054 | */ |
| 1055 | struct block_device *open_by_devnum(dev_t dev, unsigned mode) |
| 1056 | { |
| 1057 | struct block_device *bdev = bdget(dev); |
| 1058 | int err = -ENOMEM; |
| 1059 | int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY; |
| 1060 | if (bdev) |
| 1061 | err = blkdev_get(bdev, mode, flags); |
| 1062 | return err ? ERR_PTR(err) : bdev; |
| 1063 | } |
| 1064 | |
| 1065 | EXPORT_SYMBOL(open_by_devnum); |
| 1066 | |
| 1067 | /* |
| 1068 | * This routine checks whether a removable media has been changed, |
| 1069 | * and invalidates all buffer-cache-entries in that case. This |
| 1070 | * is a relatively slow routine, so we have to try to minimize using |
| 1071 | * it. Thus it is called only upon a 'mount' or 'open'. This |
| 1072 | * is the best way of combining speed and utility, I think. |
| 1073 | * People changing diskettes in the middle of an operation deserve |
| 1074 | * to lose :-) |
| 1075 | */ |
| 1076 | int check_disk_change(struct block_device *bdev) |
| 1077 | { |
| 1078 | struct gendisk *disk = bdev->bd_disk; |
| 1079 | struct block_device_operations * bdops = disk->fops; |
| 1080 | |
| 1081 | if (!bdops->media_changed) |
| 1082 | return 0; |
| 1083 | if (!bdops->media_changed(bdev->bd_disk)) |
| 1084 | return 0; |
| 1085 | |
Christoph Hellwig | 2ef4163 | 2005-05-05 16:15:59 -0700 | [diff] [blame] | 1086 | if (__invalidate_device(bdev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | printk("VFS: busy inodes on changed media.\n"); |
| 1088 | |
| 1089 | if (bdops->revalidate_disk) |
| 1090 | bdops->revalidate_disk(bdev->bd_disk); |
| 1091 | if (bdev->bd_disk->minors > 1) |
| 1092 | bdev->bd_invalidated = 1; |
| 1093 | return 1; |
| 1094 | } |
| 1095 | |
| 1096 | EXPORT_SYMBOL(check_disk_change); |
| 1097 | |
| 1098 | void bd_set_size(struct block_device *bdev, loff_t size) |
| 1099 | { |
| 1100 | unsigned bsize = bdev_hardsect_size(bdev); |
| 1101 | |
| 1102 | bdev->bd_inode->i_size = size; |
| 1103 | while (bsize < PAGE_CACHE_SIZE) { |
| 1104 | if (size & bsize) |
| 1105 | break; |
| 1106 | bsize <<= 1; |
| 1107 | } |
| 1108 | bdev->bd_block_size = bsize; |
| 1109 | bdev->bd_inode->i_blkbits = blksize_bits(bsize); |
| 1110 | } |
| 1111 | EXPORT_SYMBOL(bd_set_size); |
| 1112 | |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1113 | static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags, |
| 1114 | int for_part); |
Peter Zijlstra | c48f70c | 2006-12-08 02:36:18 -0800 | [diff] [blame] | 1115 | static int __blkdev_put(struct block_device *bdev, int for_part); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1116 | |
Peter Zijlstra | 6d740cd | 2007-02-20 13:58:18 -0800 | [diff] [blame] | 1117 | /* |
| 1118 | * bd_mutex locking: |
| 1119 | * |
| 1120 | * mutex_lock(part->bd_mutex) |
| 1121 | * mutex_lock_nested(whole->bd_mutex, 1) |
| 1122 | */ |
| 1123 | |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1124 | static int do_open(struct block_device *bdev, struct file *file, int for_part) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | { |
| 1126 | struct module *owner = NULL; |
| 1127 | struct gendisk *disk; |
| 1128 | int ret = -ENXIO; |
| 1129 | int part; |
| 1130 | |
| 1131 | file->f_mapping = bdev->bd_inode->i_mapping; |
| 1132 | lock_kernel(); |
| 1133 | disk = get_gendisk(bdev->bd_dev, &part); |
| 1134 | if (!disk) { |
| 1135 | unlock_kernel(); |
| 1136 | bdput(bdev); |
| 1137 | return ret; |
| 1138 | } |
| 1139 | owner = disk->fops->owner; |
| 1140 | |
NeilBrown | 6796bf5 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1141 | mutex_lock_nested(&bdev->bd_mutex, for_part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | if (!bdev->bd_openers) { |
| 1143 | bdev->bd_disk = disk; |
| 1144 | bdev->bd_contains = bdev; |
| 1145 | if (!part) { |
| 1146 | struct backing_dev_info *bdi; |
| 1147 | if (disk->fops->open) { |
| 1148 | ret = disk->fops->open(bdev->bd_inode, file); |
| 1149 | if (ret) |
| 1150 | goto out_first; |
| 1151 | } |
| 1152 | if (!bdev->bd_openers) { |
| 1153 | bd_set_size(bdev,(loff_t)get_capacity(disk)<<9); |
| 1154 | bdi = blk_get_backing_dev_info(bdev); |
| 1155 | if (bdi == NULL) |
| 1156 | bdi = &default_backing_dev_info; |
| 1157 | bdev->bd_inode->i_data.backing_dev_info = bdi; |
| 1158 | } |
| 1159 | if (bdev->bd_invalidated) |
| 1160 | rescan_partitions(disk, bdev); |
| 1161 | } else { |
| 1162 | struct hd_struct *p; |
| 1163 | struct block_device *whole; |
| 1164 | whole = bdget_disk(disk, 0); |
| 1165 | ret = -ENOMEM; |
| 1166 | if (!whole) |
| 1167 | goto out_first; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1168 | BUG_ON(for_part); |
| 1169 | ret = __blkdev_get(whole, file->f_mode, file->f_flags, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | if (ret) |
| 1171 | goto out_first; |
| 1172 | bdev->bd_contains = whole; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | p = disk->part[part - 1]; |
| 1174 | bdev->bd_inode->i_data.backing_dev_info = |
| 1175 | whole->bd_inode->i_data.backing_dev_info; |
| 1176 | if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | ret = -ENXIO; |
| 1178 | goto out_first; |
| 1179 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1180 | kobject_get(&p->dev.kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | bdev->bd_part = p; |
| 1182 | bd_set_size(bdev, (loff_t) p->nr_sects << 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | } |
| 1184 | } else { |
| 1185 | put_disk(disk); |
| 1186 | module_put(owner); |
| 1187 | if (bdev->bd_contains == bdev) { |
| 1188 | if (bdev->bd_disk->fops->open) { |
| 1189 | ret = bdev->bd_disk->fops->open(bdev->bd_inode, file); |
| 1190 | if (ret) |
| 1191 | goto out; |
| 1192 | } |
| 1193 | if (bdev->bd_invalidated) |
| 1194 | rescan_partitions(bdev->bd_disk, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | } |
| 1196 | } |
| 1197 | bdev->bd_openers++; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1198 | if (for_part) |
| 1199 | bdev->bd_part_count++; |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1200 | mutex_unlock(&bdev->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | unlock_kernel(); |
| 1202 | return 0; |
| 1203 | |
| 1204 | out_first: |
| 1205 | bdev->bd_disk = NULL; |
| 1206 | bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info; |
| 1207 | if (bdev != bdev->bd_contains) |
Peter Zijlstra | c48f70c | 2006-12-08 02:36:18 -0800 | [diff] [blame] | 1208 | __blkdev_put(bdev->bd_contains, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | bdev->bd_contains = NULL; |
| 1210 | put_disk(disk); |
| 1211 | module_put(owner); |
| 1212 | out: |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1213 | mutex_unlock(&bdev->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | unlock_kernel(); |
| 1215 | if (ret) |
| 1216 | bdput(bdev); |
| 1217 | return ret; |
| 1218 | } |
| 1219 | |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1220 | static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags, |
| 1221 | int for_part) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | { |
| 1223 | /* |
| 1224 | * This crockload is due to bad choice of ->open() type. |
| 1225 | * It will go away. |
| 1226 | * For now, block device ->open() routine must _not_ |
| 1227 | * examine anything in 'inode' argument except ->i_rdev. |
| 1228 | */ |
| 1229 | struct file fake_file = {}; |
| 1230 | struct dentry fake_dentry = {}; |
| 1231 | fake_file.f_mode = mode; |
| 1232 | fake_file.f_flags = flags; |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 1233 | fake_file.f_path.dentry = &fake_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | fake_dentry.d_inode = bdev->bd_inode; |
| 1235 | |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1236 | return do_open(bdev, &fake_file, for_part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1239 | int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags) |
| 1240 | { |
| 1241 | return __blkdev_get(bdev, mode, flags, 0); |
| 1242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | EXPORT_SYMBOL(blkdev_get); |
| 1244 | |
| 1245 | static int blkdev_open(struct inode * inode, struct file * filp) |
| 1246 | { |
| 1247 | struct block_device *bdev; |
| 1248 | int res; |
| 1249 | |
| 1250 | /* |
| 1251 | * Preserve backwards compatibility and allow large file access |
| 1252 | * even if userspace doesn't ask for it explicitly. Some mkfs |
| 1253 | * binary needs it. We might want to drop this workaround |
| 1254 | * during an unstable branch. |
| 1255 | */ |
| 1256 | filp->f_flags |= O_LARGEFILE; |
| 1257 | |
| 1258 | bdev = bd_acquire(inode); |
Pavel Emelianov | 6a2aae0 | 2006-10-28 10:38:33 -0700 | [diff] [blame] | 1259 | if (bdev == NULL) |
| 1260 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1262 | res = do_open(bdev, filp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | if (res) |
| 1264 | return res; |
| 1265 | |
| 1266 | if (!(filp->f_flags & O_EXCL) ) |
| 1267 | return 0; |
| 1268 | |
| 1269 | if (!(res = bd_claim(bdev, filp))) |
| 1270 | return 0; |
| 1271 | |
| 1272 | blkdev_put(bdev); |
| 1273 | return res; |
| 1274 | } |
| 1275 | |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1276 | static int __blkdev_put(struct block_device *bdev, int for_part) |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1277 | { |
| 1278 | int ret = 0; |
| 1279 | struct inode *bd_inode = bdev->bd_inode; |
| 1280 | struct gendisk *disk = bdev->bd_disk; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1281 | struct block_device *victim = NULL; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1282 | |
NeilBrown | 6796bf5 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1283 | mutex_lock_nested(&bdev->bd_mutex, for_part); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1284 | lock_kernel(); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1285 | if (for_part) |
| 1286 | bdev->bd_part_count--; |
| 1287 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1288 | if (!--bdev->bd_openers) { |
| 1289 | sync_blockdev(bdev); |
| 1290 | kill_bdev(bdev); |
| 1291 | } |
| 1292 | if (bdev->bd_contains == bdev) { |
| 1293 | if (disk->fops->release) |
| 1294 | ret = disk->fops->release(bd_inode, NULL); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1295 | } |
| 1296 | if (!bdev->bd_openers) { |
| 1297 | struct module *owner = disk->fops->owner; |
| 1298 | |
| 1299 | put_disk(disk); |
| 1300 | module_put(owner); |
| 1301 | |
| 1302 | if (bdev->bd_contains != bdev) { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1303 | kobject_put(&bdev->bd_part->dev.kobj); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1304 | bdev->bd_part = NULL; |
| 1305 | } |
| 1306 | bdev->bd_disk = NULL; |
| 1307 | bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1308 | if (bdev != bdev->bd_contains) |
| 1309 | victim = bdev->bd_contains; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1310 | bdev->bd_contains = NULL; |
| 1311 | } |
| 1312 | unlock_kernel(); |
| 1313 | mutex_unlock(&bdev->bd_mutex); |
| 1314 | bdput(bdev); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1315 | if (victim) |
| 1316 | __blkdev_put(victim, 1); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1317 | return ret; |
| 1318 | } |
| 1319 | |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1320 | int blkdev_put(struct block_device *bdev) |
| 1321 | { |
| 1322 | return __blkdev_put(bdev, 0); |
| 1323 | } |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1324 | EXPORT_SYMBOL(blkdev_put); |
| 1325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | static int blkdev_close(struct inode * inode, struct file * filp) |
| 1327 | { |
| 1328 | struct block_device *bdev = I_BDEV(filp->f_mapping->host); |
| 1329 | if (bdev->bd_holder == filp) |
| 1330 | bd_release(bdev); |
| 1331 | return blkdev_put(bdev); |
| 1332 | } |
| 1333 | |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 1334 | static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | { |
| 1336 | return blkdev_ioctl(file->f_mapping->host, file, cmd, arg); |
| 1337 | } |
| 1338 | |
Adrian Bunk | 4c54ac6 | 2008-02-18 13:48:31 +0100 | [diff] [blame^] | 1339 | static const struct address_space_operations def_blk_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | .readpage = blkdev_readpage, |
| 1341 | .writepage = blkdev_writepage, |
| 1342 | .sync_page = block_sync_page, |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 1343 | .write_begin = blkdev_write_begin, |
| 1344 | .write_end = blkdev_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | .writepages = generic_writepages, |
| 1346 | .direct_IO = blkdev_direct_IO, |
| 1347 | }; |
| 1348 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1349 | const struct file_operations def_blk_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | .open = blkdev_open, |
| 1351 | .release = blkdev_close, |
| 1352 | .llseek = block_llseek, |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 1353 | .read = do_sync_read, |
| 1354 | .write = do_sync_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | .aio_read = generic_file_aio_read, |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 1356 | .aio_write = generic_file_aio_write_nolock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | .mmap = generic_file_mmap, |
| 1358 | .fsync = block_fsync, |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 1359 | .unlocked_ioctl = block_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | #ifdef CONFIG_COMPAT |
| 1361 | .compat_ioctl = compat_blkdev_ioctl, |
| 1362 | #endif |
Jens Axboe | 7f9c51f | 2006-05-01 19:59:32 +0200 | [diff] [blame] | 1363 | .splice_read = generic_file_splice_read, |
| 1364 | .splice_write = generic_file_splice_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | }; |
| 1366 | |
| 1367 | int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg) |
| 1368 | { |
| 1369 | int res; |
| 1370 | mm_segment_t old_fs = get_fs(); |
| 1371 | set_fs(KERNEL_DS); |
| 1372 | res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg); |
| 1373 | set_fs(old_fs); |
| 1374 | return res; |
| 1375 | } |
| 1376 | |
| 1377 | EXPORT_SYMBOL(ioctl_by_bdev); |
| 1378 | |
| 1379 | /** |
| 1380 | * lookup_bdev - lookup a struct block_device by name |
| 1381 | * |
| 1382 | * @path: special file representing the block device |
| 1383 | * |
| 1384 | * Get a reference to the blockdevice at @path in the current |
| 1385 | * namespace if possible and return it. Return ERR_PTR(error) |
| 1386 | * otherwise. |
| 1387 | */ |
| 1388 | struct block_device *lookup_bdev(const char *path) |
| 1389 | { |
| 1390 | struct block_device *bdev; |
| 1391 | struct inode *inode; |
| 1392 | struct nameidata nd; |
| 1393 | int error; |
| 1394 | |
| 1395 | if (!path || !*path) |
| 1396 | return ERR_PTR(-EINVAL); |
| 1397 | |
| 1398 | error = path_lookup(path, LOOKUP_FOLLOW, &nd); |
| 1399 | if (error) |
| 1400 | return ERR_PTR(error); |
| 1401 | |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1402 | inode = nd.path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | error = -ENOTBLK; |
| 1404 | if (!S_ISBLK(inode->i_mode)) |
| 1405 | goto fail; |
| 1406 | error = -EACCES; |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1407 | if (nd.path.mnt->mnt_flags & MNT_NODEV) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | goto fail; |
| 1409 | error = -ENOMEM; |
| 1410 | bdev = bd_acquire(inode); |
| 1411 | if (!bdev) |
| 1412 | goto fail; |
| 1413 | out: |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 1414 | path_put(&nd.path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | return bdev; |
| 1416 | fail: |
| 1417 | bdev = ERR_PTR(error); |
| 1418 | goto out; |
| 1419 | } |
| 1420 | |
| 1421 | /** |
| 1422 | * open_bdev_excl - open a block device by name and set it up for use |
| 1423 | * |
| 1424 | * @path: special file representing the block device |
| 1425 | * @flags: %MS_RDONLY for opening read-only |
| 1426 | * @holder: owner for exclusion |
| 1427 | * |
| 1428 | * Open the blockdevice described by the special file at @path, claim it |
| 1429 | * for the @holder. |
| 1430 | */ |
| 1431 | struct block_device *open_bdev_excl(const char *path, int flags, void *holder) |
| 1432 | { |
| 1433 | struct block_device *bdev; |
| 1434 | mode_t mode = FMODE_READ; |
| 1435 | int error = 0; |
| 1436 | |
| 1437 | bdev = lookup_bdev(path); |
| 1438 | if (IS_ERR(bdev)) |
| 1439 | return bdev; |
| 1440 | |
| 1441 | if (!(flags & MS_RDONLY)) |
| 1442 | mode |= FMODE_WRITE; |
| 1443 | error = blkdev_get(bdev, mode, 0); |
| 1444 | if (error) |
| 1445 | return ERR_PTR(error); |
| 1446 | error = -EACCES; |
| 1447 | if (!(flags & MS_RDONLY) && bdev_read_only(bdev)) |
| 1448 | goto blkdev_put; |
| 1449 | error = bd_claim(bdev, holder); |
| 1450 | if (error) |
| 1451 | goto blkdev_put; |
| 1452 | |
| 1453 | return bdev; |
| 1454 | |
| 1455 | blkdev_put: |
| 1456 | blkdev_put(bdev); |
| 1457 | return ERR_PTR(error); |
| 1458 | } |
| 1459 | |
| 1460 | EXPORT_SYMBOL(open_bdev_excl); |
| 1461 | |
| 1462 | /** |
| 1463 | * close_bdev_excl - release a blockdevice openen by open_bdev_excl() |
| 1464 | * |
| 1465 | * @bdev: blockdevice to close |
| 1466 | * |
| 1467 | * This is the counterpart to open_bdev_excl(). |
| 1468 | */ |
| 1469 | void close_bdev_excl(struct block_device *bdev) |
| 1470 | { |
| 1471 | bd_release(bdev); |
| 1472 | blkdev_put(bdev); |
| 1473 | } |
| 1474 | |
| 1475 | EXPORT_SYMBOL(close_bdev_excl); |
David Howells | b71e8a4 | 2006-08-29 19:06:11 +0100 | [diff] [blame] | 1476 | |
| 1477 | int __invalidate_device(struct block_device *bdev) |
| 1478 | { |
| 1479 | struct super_block *sb = get_super(bdev); |
| 1480 | int res = 0; |
| 1481 | |
| 1482 | if (sb) { |
| 1483 | /* |
| 1484 | * no need to lock the super, get_super holds the |
| 1485 | * read mutex so the filesystem cannot go away |
| 1486 | * under us (->put_super runs with the write lock |
| 1487 | * hold). |
| 1488 | */ |
| 1489 | shrink_dcache_sb(sb); |
| 1490 | res = invalidate_inodes(sb); |
| 1491 | drop_super(sb); |
| 1492 | } |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 1493 | invalidate_bdev(bdev); |
David Howells | b71e8a4 | 2006-08-29 19:06:11 +0100 | [diff] [blame] | 1494 | return res; |
| 1495 | } |
| 1496 | EXPORT_SYMBOL(__invalidate_device); |