4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
8 #include <linux/init.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>
31 struct block_device bdev
;
32 struct inode vfs_inode
;
35 static const struct address_space_operations def_blk_aops
;
37 static inline struct bdev_inode
*BDEV_I(struct inode
*inode
)
39 return container_of(inode
, struct bdev_inode
, vfs_inode
);
42 inline struct block_device
*I_BDEV(struct inode
*inode
)
44 return &BDEV_I(inode
)->bdev
;
47 EXPORT_SYMBOL(I_BDEV
);
49 static sector_t
max_block(struct block_device
*bdev
)
51 sector_t retval
= ~((sector_t
)0);
52 loff_t sz
= i_size_read(bdev
->bd_inode
);
55 unsigned int size
= block_size(bdev
);
56 unsigned int sizebits
= blksize_bits(size
);
57 retval
= (sz
>> sizebits
);
62 /* Kill _all_ buffers and pagecache , dirty or not.. */
63 static void kill_bdev(struct block_device
*bdev
)
65 if (bdev
->bd_inode
->i_mapping
->nrpages
== 0)
68 truncate_inode_pages(bdev
->bd_inode
->i_mapping
, 0);
71 int set_blocksize(struct block_device
*bdev
, int size
)
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
))
77 /* Size cannot be smaller than the size supported by the device */
78 if (size
< bdev_hardsect_size(bdev
))
81 /* Don't change the size if it is same as current */
82 if (bdev
->bd_block_size
!= size
) {
84 bdev
->bd_block_size
= size
;
85 bdev
->bd_inode
->i_blkbits
= blksize_bits(size
);
91 EXPORT_SYMBOL(set_blocksize
);
93 int sb_set_blocksize(struct super_block
*sb
, int size
)
95 if (set_blocksize(sb
->s_bdev
, size
))
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
;
104 EXPORT_SYMBOL(sb_set_blocksize
);
106 int sb_min_blocksize(struct super_block
*sb
, int size
)
108 int minsize
= bdev_hardsect_size(sb
->s_bdev
);
111 return sb_set_blocksize(sb
, size
);
114 EXPORT_SYMBOL(sb_min_blocksize
);
117 blkdev_get_block(struct inode
*inode
, sector_t iblock
,
118 struct buffer_head
*bh
, int create
)
120 if (iblock
>= max_block(I_BDEV(inode
))) {
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
132 bh
->b_bdev
= I_BDEV(inode
);
133 bh
->b_blocknr
= iblock
;
134 set_buffer_mapped(bh
);
139 blkdev_get_blocks(struct inode
*inode
, sector_t iblock
,
140 struct buffer_head
*bh
, int create
)
142 sector_t end_block
= max_block(I_BDEV(inode
));
143 unsigned long max_blocks
= bh
->b_size
>> inode
->i_blkbits
;
145 if ((iblock
+ max_blocks
) > end_block
) {
146 max_blocks
= end_block
- iblock
;
147 if ((long)max_blocks
<= 0) {
149 return -EIO
; /* write fully beyond EOF */
151 * It is a read which is fully beyond EOF. We return
152 * a !buffer_mapped buffer
158 bh
->b_bdev
= I_BDEV(inode
);
159 bh
->b_blocknr
= iblock
;
160 bh
->b_size
= max_blocks
<< inode
->i_blkbits
;
162 set_buffer_mapped(bh
);
167 blkdev_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
168 loff_t offset
, unsigned long nr_segs
)
170 struct file
*file
= iocb
->ki_filp
;
171 struct inode
*inode
= file
->f_mapping
->host
;
173 return blockdev_direct_IO_no_locking(rw
, iocb
, inode
, I_BDEV(inode
),
174 iov
, offset
, nr_segs
, blkdev_get_blocks
, NULL
);
177 static int blkdev_writepage(struct page
*page
, struct writeback_control
*wbc
)
179 return block_write_full_page(page
, blkdev_get_block
, wbc
);
182 static int blkdev_readpage(struct file
* file
, struct page
* page
)
184 return block_read_full_page(page
, blkdev_get_block
);
187 static 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
)
192 return block_write_begin(file
, mapping
, pos
, len
, flags
, pagep
, fsdata
,
196 static 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
)
201 ret
= block_write_end(file
, mapping
, pos
, len
, copied
, page
, fsdata
);
204 page_cache_release(page
);
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)
214 static loff_t
block_llseek(struct file
*file
, loff_t offset
, int origin
)
216 struct inode
*bd_inode
= file
->f_mapping
->host
;
220 mutex_lock(&bd_inode
->i_mutex
);
221 size
= i_size_read(bd_inode
);
228 offset
+= file
->f_pos
;
231 if (offset
>= 0 && offset
<= size
) {
232 if (offset
!= file
->f_pos
) {
233 file
->f_pos
= offset
;
237 mutex_unlock(&bd_inode
->i_mutex
);
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.
246 static int block_fsync(struct file
*filp
, struct dentry
*dentry
, int datasync
)
248 return sync_blockdev(I_BDEV(filp
->f_mapping
->host
));
255 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(bdev_lock
);
256 static struct kmem_cache
* bdev_cachep __read_mostly
;
258 static struct inode
*bdev_alloc_inode(struct super_block
*sb
)
260 struct bdev_inode
*ei
= kmem_cache_alloc(bdev_cachep
, GFP_KERNEL
);
263 return &ei
->vfs_inode
;
266 static void bdev_destroy_inode(struct inode
*inode
)
268 struct bdev_inode
*bdi
= BDEV_I(inode
);
270 bdi
->bdev
.bd_inode_backing_dev_info
= NULL
;
271 kmem_cache_free(bdev_cachep
, bdi
);
274 static void init_once(void *foo
)
276 struct bdev_inode
*ei
= (struct bdev_inode
*) foo
;
277 struct block_device
*bdev
= &ei
->bdev
;
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
);
285 INIT_LIST_HEAD(&bdev
->bd_holder_list
);
287 inode_init_once(&ei
->vfs_inode
);
290 static inline void __bd_forget(struct inode
*inode
)
292 list_del_init(&inode
->i_devices
);
293 inode
->i_bdev
= NULL
;
294 inode
->i_mapping
= &inode
->i_data
;
297 static void bdev_clear_inode(struct inode
*inode
)
299 struct block_device
*bdev
= &BDEV_I(inode
)->bdev
;
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
));
305 list_del_init(&bdev
->bd_list
);
306 spin_unlock(&bdev_lock
);
309 static 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
,
317 static int bd_get_sb(struct file_system_type
*fs_type
,
318 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
320 return get_sb_pseudo(fs_type
, "bdev:", &bdev_sops
, 0x62646576, mnt
);
323 static struct file_system_type bd_type
= {
326 .kill_sb
= kill_anon_super
,
329 struct super_block
*blockdev_superblock __read_mostly
;
331 void __init
bdev_cache_init(void)
334 struct vfsmount
*bd_mnt
;
336 bdev_cachep
= kmem_cache_create("bdev_cache", sizeof(struct bdev_inode
),
337 0, (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
338 SLAB_MEM_SPREAD
|SLAB_PANIC
),
340 err
= register_filesystem(&bd_type
);
342 panic("Cannot register bdev pseudo-fs");
343 bd_mnt
= kern_mount(&bd_type
);
345 panic("Cannot create bdev pseudo-fs");
346 blockdev_superblock
= bd_mnt
->mnt_sb
; /* For writeback */
350 * Most likely _very_ bad one - but then it's hardly critical for small
351 * /dev and can be fixed when somebody will need really large one.
352 * Keep in mind that it will be fed through icache hash function too.
354 static inline unsigned long hash(dev_t dev
)
356 return MAJOR(dev
)+MINOR(dev
);
359 static int bdev_test(struct inode
*inode
, void *data
)
361 return BDEV_I(inode
)->bdev
.bd_dev
== *(dev_t
*)data
;
364 static int bdev_set(struct inode
*inode
, void *data
)
366 BDEV_I(inode
)->bdev
.bd_dev
= *(dev_t
*)data
;
370 static LIST_HEAD(all_bdevs
);
372 struct block_device
*bdget(dev_t dev
)
374 struct block_device
*bdev
;
377 inode
= iget5_locked(blockdev_superblock
, hash(dev
),
378 bdev_test
, bdev_set
, &dev
);
383 bdev
= &BDEV_I(inode
)->bdev
;
385 if (inode
->i_state
& I_NEW
) {
386 bdev
->bd_contains
= NULL
;
387 bdev
->bd_inode
= inode
;
388 bdev
->bd_block_size
= (1 << inode
->i_blkbits
);
389 bdev
->bd_part_count
= 0;
390 bdev
->bd_invalidated
= 0;
391 inode
->i_mode
= S_IFBLK
;
393 inode
->i_bdev
= bdev
;
394 inode
->i_data
.a_ops
= &def_blk_aops
;
395 mapping_set_gfp_mask(&inode
->i_data
, GFP_USER
);
396 inode
->i_data
.backing_dev_info
= &default_backing_dev_info
;
397 spin_lock(&bdev_lock
);
398 list_add(&bdev
->bd_list
, &all_bdevs
);
399 spin_unlock(&bdev_lock
);
400 unlock_new_inode(inode
);
405 EXPORT_SYMBOL(bdget
);
407 long nr_blockdev_pages(void)
409 struct block_device
*bdev
;
411 spin_lock(&bdev_lock
);
412 list_for_each_entry(bdev
, &all_bdevs
, bd_list
) {
413 ret
+= bdev
->bd_inode
->i_mapping
->nrpages
;
415 spin_unlock(&bdev_lock
);
419 void bdput(struct block_device
*bdev
)
421 iput(bdev
->bd_inode
);
424 EXPORT_SYMBOL(bdput
);
426 static struct block_device
*bd_acquire(struct inode
*inode
)
428 struct block_device
*bdev
;
430 spin_lock(&bdev_lock
);
431 bdev
= inode
->i_bdev
;
433 atomic_inc(&bdev
->bd_inode
->i_count
);
434 spin_unlock(&bdev_lock
);
437 spin_unlock(&bdev_lock
);
439 bdev
= bdget(inode
->i_rdev
);
441 spin_lock(&bdev_lock
);
442 if (!inode
->i_bdev
) {
444 * We take an additional bd_inode->i_count for inode,
445 * and it's released in clear_inode() of inode.
446 * So, we can access it via ->i_mapping always
449 atomic_inc(&bdev
->bd_inode
->i_count
);
450 inode
->i_bdev
= bdev
;
451 inode
->i_mapping
= bdev
->bd_inode
->i_mapping
;
452 list_add(&inode
->i_devices
, &bdev
->bd_inodes
);
454 spin_unlock(&bdev_lock
);
459 /* Call when you free inode */
461 void bd_forget(struct inode
*inode
)
463 struct block_device
*bdev
= NULL
;
465 spin_lock(&bdev_lock
);
467 if (!sb_is_blkdev_sb(inode
->i_sb
))
468 bdev
= inode
->i_bdev
;
471 spin_unlock(&bdev_lock
);
474 iput(bdev
->bd_inode
);
477 int bd_claim(struct block_device
*bdev
, void *holder
)
480 spin_lock(&bdev_lock
);
482 /* first decide result */
483 if (bdev
->bd_holder
== holder
)
484 res
= 0; /* already a holder */
485 else if (bdev
->bd_holder
!= NULL
)
486 res
= -EBUSY
; /* held by someone else */
487 else if (bdev
->bd_contains
== bdev
)
488 res
= 0; /* is a whole device which isn't held */
490 else if (bdev
->bd_contains
->bd_holder
== bd_claim
)
491 res
= 0; /* is a partition of a device that is being partitioned */
492 else if (bdev
->bd_contains
->bd_holder
!= NULL
)
493 res
= -EBUSY
; /* is a partition of a held device */
495 res
= 0; /* is a partition of an un-held device */
497 /* now impose change */
499 /* note that for a whole device bd_holders
500 * will be incremented twice, and bd_holder will
501 * be set to bd_claim before being set to holder
503 bdev
->bd_contains
->bd_holders
++;
504 bdev
->bd_contains
->bd_holder
= bd_claim
;
506 bdev
->bd_holder
= holder
;
508 spin_unlock(&bdev_lock
);
512 EXPORT_SYMBOL(bd_claim
);
514 void bd_release(struct block_device
*bdev
)
516 spin_lock(&bdev_lock
);
517 if (!--bdev
->bd_contains
->bd_holders
)
518 bdev
->bd_contains
->bd_holder
= NULL
;
519 if (!--bdev
->bd_holders
)
520 bdev
->bd_holder
= NULL
;
521 spin_unlock(&bdev_lock
);
524 EXPORT_SYMBOL(bd_release
);
528 * Functions for bd_claim_by_kobject / bd_release_from_kobject
530 * If a kobject is passed to bd_claim_by_kobject()
531 * and the kobject has a parent directory,
532 * following symlinks are created:
533 * o from the kobject to the claimed bdev
534 * o from "holders" directory of the bdev to the parent of the kobject
535 * bd_release_from_kobject() removes these symlinks.
538 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
539 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
540 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
541 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
544 static int add_symlink(struct kobject
*from
, struct kobject
*to
)
548 return sysfs_create_link(from
, to
, kobject_name(to
));
551 static void del_symlink(struct kobject
*from
, struct kobject
*to
)
555 sysfs_remove_link(from
, kobject_name(to
));
559 * 'struct bd_holder' contains pointers to kobjects symlinked by
560 * bd_claim_by_kobject.
561 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
564 struct list_head list
; /* chain of holders of the bdev */
565 int count
; /* references from the holder */
566 struct kobject
*sdir
; /* holder object, e.g. "/block/dm-0/slaves" */
567 struct kobject
*hdev
; /* e.g. "/block/dm-0" */
568 struct kobject
*hdir
; /* e.g. "/block/sda/holders" */
569 struct kobject
*sdev
; /* e.g. "/block/sda" */
573 * Get references of related kobjects at once.
574 * Returns 1 on success. 0 on failure.
576 * Should call bd_holder_release_dirs() after successful use.
578 static int bd_holder_grab_dirs(struct block_device
*bdev
,
579 struct bd_holder
*bo
)
584 bo
->sdir
= kobject_get(bo
->sdir
);
588 bo
->hdev
= kobject_get(bo
->sdir
->parent
);
592 bo
->sdev
= kobject_get(&part_to_dev(bdev
->bd_part
)->kobj
);
596 bo
->hdir
= kobject_get(bdev
->bd_part
->holder_dir
);
603 kobject_put(bo
->sdev
);
605 kobject_put(bo
->hdev
);
607 kobject_put(bo
->sdir
);
612 /* Put references of related kobjects at once. */
613 static void bd_holder_release_dirs(struct bd_holder
*bo
)
615 kobject_put(bo
->hdir
);
616 kobject_put(bo
->sdev
);
617 kobject_put(bo
->hdev
);
618 kobject_put(bo
->sdir
);
621 static struct bd_holder
*alloc_bd_holder(struct kobject
*kobj
)
623 struct bd_holder
*bo
;
625 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
635 static void free_bd_holder(struct bd_holder
*bo
)
641 * find_bd_holder - find matching struct bd_holder from the block device
643 * @bdev: struct block device to be searched
644 * @bo: target struct bd_holder
646 * Returns matching entry with @bo in @bdev->bd_holder_list.
647 * If found, increment the reference count and return the pointer.
648 * If not found, returns NULL.
650 static struct bd_holder
*find_bd_holder(struct block_device
*bdev
,
651 struct bd_holder
*bo
)
653 struct bd_holder
*tmp
;
655 list_for_each_entry(tmp
, &bdev
->bd_holder_list
, list
)
656 if (tmp
->sdir
== bo
->sdir
) {
665 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
667 * @bdev: block device to be bd_claimed
668 * @bo: preallocated and initialized by alloc_bd_holder()
670 * Add @bo to @bdev->bd_holder_list, create symlinks.
672 * Returns 0 if symlinks are created.
673 * Returns -ve if something fails.
675 static int add_bd_holder(struct block_device
*bdev
, struct bd_holder
*bo
)
682 if (!bd_holder_grab_dirs(bdev
, bo
))
685 err
= add_symlink(bo
->sdir
, bo
->sdev
);
689 err
= add_symlink(bo
->hdir
, bo
->hdev
);
691 del_symlink(bo
->sdir
, bo
->sdev
);
695 list_add_tail(&bo
->list
, &bdev
->bd_holder_list
);
700 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
702 * @bdev: block device to be bd_claimed
703 * @kobj: holder's kobject
705 * If there is matching entry with @kobj in @bdev->bd_holder_list
706 * and no other bd_claim() from the same kobject,
707 * remove the struct bd_holder from the list, delete symlinks for it.
709 * Returns a pointer to the struct bd_holder when it's removed from the list
710 * and ready to be freed.
711 * Returns NULL if matching claim isn't found or there is other bd_claim()
712 * by the same kobject.
714 static struct bd_holder
*del_bd_holder(struct block_device
*bdev
,
715 struct kobject
*kobj
)
717 struct bd_holder
*bo
;
719 list_for_each_entry(bo
, &bdev
->bd_holder_list
, list
) {
720 if (bo
->sdir
== kobj
) {
722 BUG_ON(bo
->count
< 0);
725 del_symlink(bo
->sdir
, bo
->sdev
);
726 del_symlink(bo
->hdir
, bo
->hdev
);
727 bd_holder_release_dirs(bo
);
738 * bd_claim_by_kobject - bd_claim() with additional kobject signature
740 * @bdev: block device to be claimed
741 * @holder: holder's signature
742 * @kobj: holder's kobject
744 * Do bd_claim() and if it succeeds, create sysfs symlinks between
745 * the bdev and the holder's kobject.
746 * Use bd_release_from_kobject() when relesing the claimed bdev.
748 * Returns 0 on success. (same as bd_claim())
749 * Returns errno on failure.
751 static int bd_claim_by_kobject(struct block_device
*bdev
, void *holder
,
752 struct kobject
*kobj
)
755 struct bd_holder
*bo
, *found
;
760 bo
= alloc_bd_holder(kobj
);
764 mutex_lock(&bdev
->bd_mutex
);
766 err
= bd_claim(bdev
, holder
);
770 found
= find_bd_holder(bdev
, bo
);
774 err
= add_bd_holder(bdev
, bo
);
780 mutex_unlock(&bdev
->bd_mutex
);
786 * bd_release_from_kobject - bd_release() with additional kobject signature
788 * @bdev: block device to be released
789 * @kobj: holder's kobject
791 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
793 static void bd_release_from_kobject(struct block_device
*bdev
,
794 struct kobject
*kobj
)
799 mutex_lock(&bdev
->bd_mutex
);
801 free_bd_holder(del_bd_holder(bdev
, kobj
));
802 mutex_unlock(&bdev
->bd_mutex
);
806 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
808 * @bdev: block device to be claimed
809 * @holder: holder's signature
810 * @disk: holder's gendisk
812 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
814 int bd_claim_by_disk(struct block_device
*bdev
, void *holder
,
815 struct gendisk
*disk
)
817 return bd_claim_by_kobject(bdev
, holder
, kobject_get(disk
->slave_dir
));
819 EXPORT_SYMBOL_GPL(bd_claim_by_disk
);
822 * bd_release_from_disk - wrapper function for bd_release_from_kobject()
824 * @bdev: block device to be claimed
825 * @disk: holder's gendisk
827 * Call bd_release_from_kobject() and put @disk->slave_dir.
829 void bd_release_from_disk(struct block_device
*bdev
, struct gendisk
*disk
)
831 bd_release_from_kobject(bdev
, disk
->slave_dir
);
832 kobject_put(disk
->slave_dir
);
834 EXPORT_SYMBOL_GPL(bd_release_from_disk
);
838 * Tries to open block device by device number. Use it ONLY if you
839 * really do not have anything better - i.e. when you are behind a
840 * truly sucky interface and all you are given is a device number. _Never_
841 * to be used for internal purposes. If you ever need it - reconsider
844 struct block_device
*open_by_devnum(dev_t dev
, fmode_t mode
)
846 struct block_device
*bdev
= bdget(dev
);
849 err
= blkdev_get(bdev
, mode
);
850 return err
? ERR_PTR(err
) : bdev
;
853 EXPORT_SYMBOL(open_by_devnum
);
856 * flush_disk - invalidates all buffer-cache entries on a disk
858 * @bdev: struct block device to be flushed
860 * Invalidates all buffer-cache entries on a disk. It should be called
861 * when a disk has been changed -- either by a media change or online
864 static void flush_disk(struct block_device
*bdev
)
866 if (__invalidate_device(bdev
)) {
867 char name
[BDEVNAME_SIZE
] = "";
870 disk_name(bdev
->bd_disk
, 0, name
);
871 printk(KERN_WARNING
"VFS: busy inodes on changed media or "
872 "resized disk %s\n", name
);
877 if (disk_partitionable(bdev
->bd_disk
))
878 bdev
->bd_invalidated
= 1;
882 * check_disk_size_change - checks for disk size change and adjusts bdev size.
883 * @disk: struct gendisk to check
884 * @bdev: struct bdev to adjust.
886 * This routine checks to see if the bdev size does not match the disk size
887 * and adjusts it if it differs.
889 void check_disk_size_change(struct gendisk
*disk
, struct block_device
*bdev
)
891 loff_t disk_size
, bdev_size
;
893 disk_size
= (loff_t
)get_capacity(disk
) << 9;
894 bdev_size
= i_size_read(bdev
->bd_inode
);
895 if (disk_size
!= bdev_size
) {
896 char name
[BDEVNAME_SIZE
];
898 disk_name(disk
, 0, name
);
900 "%s: detected capacity change from %lld to %lld\n",
901 name
, bdev_size
, disk_size
);
902 i_size_write(bdev
->bd_inode
, disk_size
);
906 EXPORT_SYMBOL(check_disk_size_change
);
909 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
910 * @disk: struct gendisk to be revalidated
912 * This routine is a wrapper for lower-level driver's revalidate_disk
913 * call-backs. It is used to do common pre and post operations needed
914 * for all revalidate_disk operations.
916 int revalidate_disk(struct gendisk
*disk
)
918 struct block_device
*bdev
;
921 if (disk
->fops
->revalidate_disk
)
922 ret
= disk
->fops
->revalidate_disk(disk
);
924 bdev
= bdget_disk(disk
, 0);
928 mutex_lock(&bdev
->bd_mutex
);
929 check_disk_size_change(disk
, bdev
);
930 mutex_unlock(&bdev
->bd_mutex
);
934 EXPORT_SYMBOL(revalidate_disk
);
937 * This routine checks whether a removable media has been changed,
938 * and invalidates all buffer-cache-entries in that case. This
939 * is a relatively slow routine, so we have to try to minimize using
940 * it. Thus it is called only upon a 'mount' or 'open'. This
941 * is the best way of combining speed and utility, I think.
942 * People changing diskettes in the middle of an operation deserve
945 int check_disk_change(struct block_device
*bdev
)
947 struct gendisk
*disk
= bdev
->bd_disk
;
948 struct block_device_operations
* bdops
= disk
->fops
;
950 if (!bdops
->media_changed
)
952 if (!bdops
->media_changed(bdev
->bd_disk
))
956 if (bdops
->revalidate_disk
)
957 bdops
->revalidate_disk(bdev
->bd_disk
);
961 EXPORT_SYMBOL(check_disk_change
);
963 void bd_set_size(struct block_device
*bdev
, loff_t size
)
965 unsigned bsize
= bdev_hardsect_size(bdev
);
967 bdev
->bd_inode
->i_size
= size
;
968 while (bsize
< PAGE_CACHE_SIZE
) {
973 bdev
->bd_block_size
= bsize
;
974 bdev
->bd_inode
->i_blkbits
= blksize_bits(bsize
);
976 EXPORT_SYMBOL(bd_set_size
);
978 static int __blkdev_put(struct block_device
*bdev
, fmode_t mode
, int for_part
);
983 * mutex_lock(part->bd_mutex)
984 * mutex_lock_nested(whole->bd_mutex, 1)
987 static int __blkdev_get(struct block_device
*bdev
, fmode_t mode
, int for_part
)
989 struct gendisk
*disk
;
994 if (mode
& FMODE_READ
)
996 if (mode
& FMODE_WRITE
)
999 * hooks: /n/, see "layering violations".
1001 ret
= devcgroup_inode_permission(bdev
->bd_inode
, perm
);
1010 disk
= get_gendisk(bdev
->bd_dev
, &partno
);
1012 goto out_unlock_kernel
;
1014 mutex_lock_nested(&bdev
->bd_mutex
, for_part
);
1015 if (!bdev
->bd_openers
) {
1016 bdev
->bd_disk
= disk
;
1017 bdev
->bd_contains
= bdev
;
1019 struct backing_dev_info
*bdi
;
1022 bdev
->bd_part
= disk_get_part(disk
, partno
);
1026 if (disk
->fops
->open
) {
1027 ret
= disk
->fops
->open(bdev
, mode
);
1031 if (!bdev
->bd_openers
) {
1032 bd_set_size(bdev
,(loff_t
)get_capacity(disk
)<<9);
1033 bdi
= blk_get_backing_dev_info(bdev
);
1035 bdi
= &default_backing_dev_info
;
1036 bdev
->bd_inode
->i_data
.backing_dev_info
= bdi
;
1038 if (bdev
->bd_invalidated
)
1039 rescan_partitions(disk
, bdev
);
1041 struct block_device
*whole
;
1042 whole
= bdget_disk(disk
, 0);
1047 ret
= __blkdev_get(whole
, mode
, 1);
1050 bdev
->bd_contains
= whole
;
1051 bdev
->bd_inode
->i_data
.backing_dev_info
=
1052 whole
->bd_inode
->i_data
.backing_dev_info
;
1053 bdev
->bd_part
= disk_get_part(disk
, partno
);
1054 if (!(disk
->flags
& GENHD_FL_UP
) ||
1055 !bdev
->bd_part
|| !bdev
->bd_part
->nr_sects
) {
1059 bd_set_size(bdev
, (loff_t
)bdev
->bd_part
->nr_sects
<< 9);
1063 module_put(disk
->fops
->owner
);
1065 if (bdev
->bd_contains
== bdev
) {
1066 if (bdev
->bd_disk
->fops
->open
) {
1067 ret
= bdev
->bd_disk
->fops
->open(bdev
, mode
);
1069 goto out_unlock_bdev
;
1071 if (bdev
->bd_invalidated
)
1072 rescan_partitions(bdev
->bd_disk
, bdev
);
1077 bdev
->bd_part_count
++;
1078 mutex_unlock(&bdev
->bd_mutex
);
1083 disk_put_part(bdev
->bd_part
);
1084 bdev
->bd_disk
= NULL
;
1085 bdev
->bd_part
= NULL
;
1086 bdev
->bd_inode
->i_data
.backing_dev_info
= &default_backing_dev_info
;
1087 if (bdev
!= bdev
->bd_contains
)
1088 __blkdev_put(bdev
->bd_contains
, mode
, 1);
1089 bdev
->bd_contains
= NULL
;
1091 mutex_unlock(&bdev
->bd_mutex
);
1096 module_put(disk
->fops
->owner
);
1103 int blkdev_get(struct block_device
*bdev
, fmode_t mode
)
1105 return __blkdev_get(bdev
, mode
, 0);
1107 EXPORT_SYMBOL(blkdev_get
);
1109 static int blkdev_open(struct inode
* inode
, struct file
* filp
)
1111 struct block_device
*bdev
;
1115 * Preserve backwards compatibility and allow large file access
1116 * even if userspace doesn't ask for it explicitly. Some mkfs
1117 * binary needs it. We might want to drop this workaround
1118 * during an unstable branch.
1120 filp
->f_flags
|= O_LARGEFILE
;
1122 if (filp
->f_flags
& O_NDELAY
)
1123 filp
->f_mode
|= FMODE_NDELAY
;
1124 if (filp
->f_flags
& O_EXCL
)
1125 filp
->f_mode
|= FMODE_EXCL
;
1126 if ((filp
->f_flags
& O_ACCMODE
) == 3)
1127 filp
->f_mode
|= FMODE_WRITE_IOCTL
;
1129 bdev
= bd_acquire(inode
);
1133 filp
->f_mapping
= bdev
->bd_inode
->i_mapping
;
1135 res
= blkdev_get(bdev
, filp
->f_mode
);
1139 if (filp
->f_mode
& FMODE_EXCL
) {
1140 res
= bd_claim(bdev
, filp
);
1142 goto out_blkdev_put
;
1148 blkdev_put(bdev
, filp
->f_mode
);
1152 static int __blkdev_put(struct block_device
*bdev
, fmode_t mode
, int for_part
)
1155 struct gendisk
*disk
= bdev
->bd_disk
;
1156 struct block_device
*victim
= NULL
;
1158 mutex_lock_nested(&bdev
->bd_mutex
, for_part
);
1161 bdev
->bd_part_count
--;
1163 if (!--bdev
->bd_openers
) {
1164 sync_blockdev(bdev
);
1167 if (bdev
->bd_contains
== bdev
) {
1168 if (disk
->fops
->release
)
1169 ret
= disk
->fops
->release(disk
, mode
);
1171 if (!bdev
->bd_openers
) {
1172 struct module
*owner
= disk
->fops
->owner
;
1176 disk_put_part(bdev
->bd_part
);
1177 bdev
->bd_part
= NULL
;
1178 bdev
->bd_disk
= NULL
;
1179 bdev
->bd_inode
->i_data
.backing_dev_info
= &default_backing_dev_info
;
1180 if (bdev
!= bdev
->bd_contains
)
1181 victim
= bdev
->bd_contains
;
1182 bdev
->bd_contains
= NULL
;
1185 mutex_unlock(&bdev
->bd_mutex
);
1188 __blkdev_put(victim
, mode
, 1);
1192 int blkdev_put(struct block_device
*bdev
, fmode_t mode
)
1194 return __blkdev_put(bdev
, mode
, 0);
1196 EXPORT_SYMBOL(blkdev_put
);
1198 static int blkdev_close(struct inode
* inode
, struct file
* filp
)
1200 struct block_device
*bdev
= I_BDEV(filp
->f_mapping
->host
);
1201 if (bdev
->bd_holder
== filp
)
1203 return blkdev_put(bdev
, filp
->f_mode
);
1206 static long block_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
1208 struct block_device
*bdev
= I_BDEV(file
->f_mapping
->host
);
1209 fmode_t mode
= file
->f_mode
;
1212 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1213 * to updated it before every ioctl.
1215 if (file
->f_flags
& O_NDELAY
)
1216 mode
|= FMODE_NDELAY
;
1218 mode
&= ~FMODE_NDELAY
;
1220 return blkdev_ioctl(bdev
, mode
, cmd
, arg
);
1223 static const struct address_space_operations def_blk_aops
= {
1224 .readpage
= blkdev_readpage
,
1225 .writepage
= blkdev_writepage
,
1226 .sync_page
= block_sync_page
,
1227 .write_begin
= blkdev_write_begin
,
1228 .write_end
= blkdev_write_end
,
1229 .writepages
= generic_writepages
,
1230 .direct_IO
= blkdev_direct_IO
,
1233 const struct file_operations def_blk_fops
= {
1234 .open
= blkdev_open
,
1235 .release
= blkdev_close
,
1236 .llseek
= block_llseek
,
1237 .read
= do_sync_read
,
1238 .write
= do_sync_write
,
1239 .aio_read
= generic_file_aio_read
,
1240 .aio_write
= generic_file_aio_write_nolock
,
1241 .mmap
= generic_file_mmap
,
1242 .fsync
= block_fsync
,
1243 .unlocked_ioctl
= block_ioctl
,
1244 #ifdef CONFIG_COMPAT
1245 .compat_ioctl
= compat_blkdev_ioctl
,
1247 .splice_read
= generic_file_splice_read
,
1248 .splice_write
= generic_file_splice_write
,
1251 int ioctl_by_bdev(struct block_device
*bdev
, unsigned cmd
, unsigned long arg
)
1254 mm_segment_t old_fs
= get_fs();
1256 res
= blkdev_ioctl(bdev
, 0, cmd
, arg
);
1261 EXPORT_SYMBOL(ioctl_by_bdev
);
1264 * lookup_bdev - lookup a struct block_device by name
1265 * @pathname: special file representing the block device
1267 * Get a reference to the blockdevice at @pathname in the current
1268 * namespace if possible and return it. Return ERR_PTR(error)
1271 struct block_device
*lookup_bdev(const char *pathname
)
1273 struct block_device
*bdev
;
1274 struct inode
*inode
;
1278 if (!pathname
|| !*pathname
)
1279 return ERR_PTR(-EINVAL
);
1281 error
= kern_path(pathname
, LOOKUP_FOLLOW
, &path
);
1283 return ERR_PTR(error
);
1285 inode
= path
.dentry
->d_inode
;
1287 if (!S_ISBLK(inode
->i_mode
))
1290 if (path
.mnt
->mnt_flags
& MNT_NODEV
)
1293 bdev
= bd_acquire(inode
);
1300 bdev
= ERR_PTR(error
);
1303 EXPORT_SYMBOL(lookup_bdev
);
1306 * open_bdev_exclusive - open a block device by name and set it up for use
1308 * @path: special file representing the block device
1309 * @mode: FMODE_... combination to pass be used
1310 * @holder: owner for exclusion
1312 * Open the blockdevice described by the special file at @path, claim it
1315 struct block_device
*open_bdev_exclusive(const char *path
, fmode_t mode
, void *holder
)
1317 struct block_device
*bdev
;
1320 bdev
= lookup_bdev(path
);
1324 error
= blkdev_get(bdev
, mode
);
1326 return ERR_PTR(error
);
1328 if ((mode
& FMODE_WRITE
) && bdev_read_only(bdev
))
1330 error
= bd_claim(bdev
, holder
);
1337 blkdev_put(bdev
, mode
);
1338 return ERR_PTR(error
);
1341 EXPORT_SYMBOL(open_bdev_exclusive
);
1344 * close_bdev_exclusive - close a blockdevice opened by open_bdev_exclusive()
1346 * @bdev: blockdevice to close
1347 * @mode: mode, must match that used to open.
1349 * This is the counterpart to open_bdev_exclusive().
1351 void close_bdev_exclusive(struct block_device
*bdev
, fmode_t mode
)
1354 blkdev_put(bdev
, mode
);
1357 EXPORT_SYMBOL(close_bdev_exclusive
);
1359 int __invalidate_device(struct block_device
*bdev
)
1361 struct super_block
*sb
= get_super(bdev
);
1366 * no need to lock the super, get_super holds the
1367 * read mutex so the filesystem cannot go away
1368 * under us (->put_super runs with the write lock
1371 shrink_dcache_sb(sb
);
1372 res
= invalidate_inodes(sb
);
1375 invalidate_bdev(bdev
);
1378 EXPORT_SYMBOL(__invalidate_device
);