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