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