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