4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
8 #include <linux/config.h>
9 #include <linux/init.h>
11 #include <linux/fcntl.h>
12 #include <linux/slab.h>
13 #include <linux/kmod.h>
14 #include <linux/major.h>
15 #include <linux/smp_lock.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/mpage.h>
22 #include <linux/mount.h>
23 #include <linux/uio.h>
24 #include <linux/namei.h>
25 #include <asm/uaccess.h>
28 struct block_device bdev
;
29 struct inode vfs_inode
;
32 static inline struct bdev_inode
*BDEV_I(struct inode
*inode
)
34 return container_of(inode
, struct bdev_inode
, vfs_inode
);
37 inline struct block_device
*I_BDEV(struct inode
*inode
)
39 return &BDEV_I(inode
)->bdev
;
42 EXPORT_SYMBOL(I_BDEV
);
44 static sector_t
max_block(struct block_device
*bdev
)
46 sector_t retval
= ~((sector_t
)0);
47 loff_t sz
= i_size_read(bdev
->bd_inode
);
50 unsigned int size
= block_size(bdev
);
51 unsigned int sizebits
= blksize_bits(size
);
52 retval
= (sz
>> sizebits
);
57 /* Kill _all_ buffers, dirty or not.. */
58 static void kill_bdev(struct block_device
*bdev
)
60 invalidate_bdev(bdev
, 1);
61 truncate_inode_pages(bdev
->bd_inode
->i_mapping
, 0);
64 int set_blocksize(struct block_device
*bdev
, int size
)
66 /* Size must be a power of two, and between 512 and PAGE_SIZE */
67 if (size
> PAGE_SIZE
|| size
< 512 || (size
& (size
-1)))
70 /* Size cannot be smaller than the size supported by the device */
71 if (size
< bdev_hardsect_size(bdev
))
74 /* Don't change the size if it is same as current */
75 if (bdev
->bd_block_size
!= size
) {
77 bdev
->bd_block_size
= size
;
78 bdev
->bd_inode
->i_blkbits
= blksize_bits(size
);
84 EXPORT_SYMBOL(set_blocksize
);
86 int sb_set_blocksize(struct super_block
*sb
, int size
)
88 if (set_blocksize(sb
->s_bdev
, size
))
90 /* If we get here, we know size is power of two
91 * and it's value is between 512 and PAGE_SIZE */
92 sb
->s_blocksize
= size
;
93 sb
->s_blocksize_bits
= blksize_bits(size
);
94 return sb
->s_blocksize
;
97 EXPORT_SYMBOL(sb_set_blocksize
);
99 int sb_min_blocksize(struct super_block
*sb
, int size
)
101 int minsize
= bdev_hardsect_size(sb
->s_bdev
);
104 return sb_set_blocksize(sb
, size
);
107 EXPORT_SYMBOL(sb_min_blocksize
);
110 blkdev_get_block(struct inode
*inode
, sector_t iblock
,
111 struct buffer_head
*bh
, int create
)
113 if (iblock
>= max_block(I_BDEV(inode
))) {
118 * for reads, we're just trying to fill a partial page.
119 * return a hole, they will have to call get_block again
120 * before they can fill it, and they will get -EIO at that
125 bh
->b_bdev
= I_BDEV(inode
);
126 bh
->b_blocknr
= iblock
;
127 set_buffer_mapped(bh
);
132 blkdev_get_blocks(struct inode
*inode
, sector_t iblock
,
133 struct buffer_head
*bh
, int create
)
135 sector_t end_block
= max_block(I_BDEV(inode
));
136 unsigned long max_blocks
= bh
->b_size
>> inode
->i_blkbits
;
138 if ((iblock
+ max_blocks
) > end_block
) {
139 max_blocks
= end_block
- iblock
;
140 if ((long)max_blocks
<= 0) {
142 return -EIO
; /* write fully beyond EOF */
144 * It is a read which is fully beyond EOF. We return
145 * a !buffer_mapped buffer
151 bh
->b_bdev
= I_BDEV(inode
);
152 bh
->b_blocknr
= iblock
;
153 bh
->b_size
= max_blocks
<< inode
->i_blkbits
;
155 set_buffer_mapped(bh
);
160 blkdev_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
161 loff_t offset
, unsigned long nr_segs
)
163 struct file
*file
= iocb
->ki_filp
;
164 struct inode
*inode
= file
->f_mapping
->host
;
166 return blockdev_direct_IO_no_locking(rw
, iocb
, inode
, I_BDEV(inode
),
167 iov
, offset
, nr_segs
, blkdev_get_blocks
, NULL
);
170 static int blkdev_writepage(struct page
*page
, struct writeback_control
*wbc
)
172 return block_write_full_page(page
, blkdev_get_block
, wbc
);
175 static int blkdev_readpage(struct file
* file
, struct page
* page
)
177 return block_read_full_page(page
, blkdev_get_block
);
180 static int blkdev_prepare_write(struct file
*file
, struct page
*page
, unsigned from
, unsigned to
)
182 return block_prepare_write(page
, from
, to
, blkdev_get_block
);
185 static int blkdev_commit_write(struct file
*file
, struct page
*page
, unsigned from
, unsigned to
)
187 return block_commit_write(page
, from
, to
);
192 * for a block special file file->f_dentry->d_inode->i_size is zero
193 * so we compute the size by hand (just as in block_read/write above)
195 static loff_t
block_llseek(struct file
*file
, loff_t offset
, int origin
)
197 struct inode
*bd_inode
= file
->f_mapping
->host
;
201 mutex_lock(&bd_inode
->i_mutex
);
202 size
= i_size_read(bd_inode
);
209 offset
+= file
->f_pos
;
212 if (offset
>= 0 && offset
<= size
) {
213 if (offset
!= file
->f_pos
) {
214 file
->f_pos
= offset
;
218 mutex_unlock(&bd_inode
->i_mutex
);
223 * Filp is never NULL; the only case when ->fsync() is called with
224 * NULL first argument is nfsd_sync_dir() and that's not a directory.
227 static int block_fsync(struct file
*filp
, struct dentry
*dentry
, int datasync
)
229 return sync_blockdev(I_BDEV(filp
->f_mapping
->host
));
236 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(bdev_lock
);
237 static kmem_cache_t
* bdev_cachep __read_mostly
;
239 static struct inode
*bdev_alloc_inode(struct super_block
*sb
)
241 struct bdev_inode
*ei
= kmem_cache_alloc(bdev_cachep
, SLAB_KERNEL
);
244 return &ei
->vfs_inode
;
247 static void bdev_destroy_inode(struct inode
*inode
)
249 struct bdev_inode
*bdi
= BDEV_I(inode
);
251 bdi
->bdev
.bd_inode_backing_dev_info
= NULL
;
252 kmem_cache_free(bdev_cachep
, bdi
);
255 static void init_once(void * foo
, kmem_cache_t
* cachep
, unsigned long flags
)
257 struct bdev_inode
*ei
= (struct bdev_inode
*) foo
;
258 struct block_device
*bdev
= &ei
->bdev
;
260 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
261 SLAB_CTOR_CONSTRUCTOR
)
263 memset(bdev
, 0, sizeof(*bdev
));
264 mutex_init(&bdev
->bd_mutex
);
265 mutex_init(&bdev
->bd_mount_mutex
);
266 INIT_LIST_HEAD(&bdev
->bd_inodes
);
267 INIT_LIST_HEAD(&bdev
->bd_list
);
269 INIT_LIST_HEAD(&bdev
->bd_holder_list
);
271 inode_init_once(&ei
->vfs_inode
);
275 static inline void __bd_forget(struct inode
*inode
)
277 list_del_init(&inode
->i_devices
);
278 inode
->i_bdev
= NULL
;
279 inode
->i_mapping
= &inode
->i_data
;
282 static void bdev_clear_inode(struct inode
*inode
)
284 struct block_device
*bdev
= &BDEV_I(inode
)->bdev
;
286 spin_lock(&bdev_lock
);
287 while ( (p
= bdev
->bd_inodes
.next
) != &bdev
->bd_inodes
) {
288 __bd_forget(list_entry(p
, struct inode
, i_devices
));
290 list_del_init(&bdev
->bd_list
);
291 spin_unlock(&bdev_lock
);
294 static struct super_operations bdev_sops
= {
295 .statfs
= simple_statfs
,
296 .alloc_inode
= bdev_alloc_inode
,
297 .destroy_inode
= bdev_destroy_inode
,
298 .drop_inode
= generic_delete_inode
,
299 .clear_inode
= bdev_clear_inode
,
302 static int bd_get_sb(struct file_system_type
*fs_type
,
303 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
305 return get_sb_pseudo(fs_type
, "bdev:", &bdev_sops
, 0x62646576, mnt
);
308 static struct file_system_type bd_type
= {
311 .kill_sb
= kill_anon_super
,
314 static struct vfsmount
*bd_mnt __read_mostly
;
315 struct super_block
*blockdev_superblock
;
317 void __init
bdev_cache_init(void)
320 bdev_cachep
= kmem_cache_create("bdev_cache", sizeof(struct bdev_inode
),
321 0, (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
322 SLAB_MEM_SPREAD
|SLAB_PANIC
),
324 err
= register_filesystem(&bd_type
);
326 panic("Cannot register bdev pseudo-fs");
327 bd_mnt
= kern_mount(&bd_type
);
328 err
= PTR_ERR(bd_mnt
);
330 panic("Cannot create bdev pseudo-fs");
331 blockdev_superblock
= bd_mnt
->mnt_sb
; /* For writeback */
335 * Most likely _very_ bad one - but then it's hardly critical for small
336 * /dev and can be fixed when somebody will need really large one.
337 * Keep in mind that it will be fed through icache hash function too.
339 static inline unsigned long hash(dev_t dev
)
341 return MAJOR(dev
)+MINOR(dev
);
344 static int bdev_test(struct inode
*inode
, void *data
)
346 return BDEV_I(inode
)->bdev
.bd_dev
== *(dev_t
*)data
;
349 static int bdev_set(struct inode
*inode
, void *data
)
351 BDEV_I(inode
)->bdev
.bd_dev
= *(dev_t
*)data
;
355 static LIST_HEAD(all_bdevs
);
357 struct block_device
*bdget(dev_t dev
)
359 struct block_device
*bdev
;
362 inode
= iget5_locked(bd_mnt
->mnt_sb
, hash(dev
),
363 bdev_test
, bdev_set
, &dev
);
368 bdev
= &BDEV_I(inode
)->bdev
;
370 if (inode
->i_state
& I_NEW
) {
371 bdev
->bd_contains
= NULL
;
372 bdev
->bd_inode
= inode
;
373 bdev
->bd_block_size
= (1 << inode
->i_blkbits
);
374 bdev
->bd_part_count
= 0;
375 bdev
->bd_invalidated
= 0;
376 inode
->i_mode
= S_IFBLK
;
378 inode
->i_bdev
= bdev
;
379 inode
->i_data
.a_ops
= &def_blk_aops
;
380 mapping_set_gfp_mask(&inode
->i_data
, GFP_USER
);
381 inode
->i_data
.backing_dev_info
= &default_backing_dev_info
;
382 spin_lock(&bdev_lock
);
383 list_add(&bdev
->bd_list
, &all_bdevs
);
384 spin_unlock(&bdev_lock
);
385 unlock_new_inode(inode
);
390 EXPORT_SYMBOL(bdget
);
392 long nr_blockdev_pages(void)
396 spin_lock(&bdev_lock
);
397 list_for_each(p
, &all_bdevs
) {
398 struct block_device
*bdev
;
399 bdev
= list_entry(p
, struct block_device
, bd_list
);
400 ret
+= bdev
->bd_inode
->i_mapping
->nrpages
;
402 spin_unlock(&bdev_lock
);
406 void bdput(struct block_device
*bdev
)
408 iput(bdev
->bd_inode
);
411 EXPORT_SYMBOL(bdput
);
413 static struct block_device
*bd_acquire(struct inode
*inode
)
415 struct block_device
*bdev
;
417 spin_lock(&bdev_lock
);
418 bdev
= inode
->i_bdev
;
420 atomic_inc(&bdev
->bd_inode
->i_count
);
421 spin_unlock(&bdev_lock
);
424 spin_unlock(&bdev_lock
);
426 bdev
= bdget(inode
->i_rdev
);
428 spin_lock(&bdev_lock
);
429 if (!inode
->i_bdev
) {
431 * We take an additional bd_inode->i_count for inode,
432 * and it's released in clear_inode() of inode.
433 * So, we can access it via ->i_mapping always
436 atomic_inc(&bdev
->bd_inode
->i_count
);
437 inode
->i_bdev
= bdev
;
438 inode
->i_mapping
= bdev
->bd_inode
->i_mapping
;
439 list_add(&inode
->i_devices
, &bdev
->bd_inodes
);
441 spin_unlock(&bdev_lock
);
446 /* Call when you free inode */
448 void bd_forget(struct inode
*inode
)
450 struct block_device
*bdev
= NULL
;
452 spin_lock(&bdev_lock
);
454 if (inode
->i_sb
!= blockdev_superblock
)
455 bdev
= inode
->i_bdev
;
458 spin_unlock(&bdev_lock
);
461 iput(bdev
->bd_inode
);
464 int bd_claim(struct block_device
*bdev
, void *holder
)
467 spin_lock(&bdev_lock
);
469 /* first decide result */
470 if (bdev
->bd_holder
== holder
)
471 res
= 0; /* already a holder */
472 else if (bdev
->bd_holder
!= NULL
)
473 res
= -EBUSY
; /* held by someone else */
474 else if (bdev
->bd_contains
== bdev
)
475 res
= 0; /* is a whole device which isn't held */
477 else if (bdev
->bd_contains
->bd_holder
== bd_claim
)
478 res
= 0; /* is a partition of a device that is being partitioned */
479 else if (bdev
->bd_contains
->bd_holder
!= NULL
)
480 res
= -EBUSY
; /* is a partition of a held device */
482 res
= 0; /* is a partition of an un-held device */
484 /* now impose change */
486 /* note that for a whole device bd_holders
487 * will be incremented twice, and bd_holder will
488 * be set to bd_claim before being set to holder
490 bdev
->bd_contains
->bd_holders
++;
491 bdev
->bd_contains
->bd_holder
= bd_claim
;
493 bdev
->bd_holder
= holder
;
495 spin_unlock(&bdev_lock
);
499 EXPORT_SYMBOL(bd_claim
);
501 void bd_release(struct block_device
*bdev
)
503 spin_lock(&bdev_lock
);
504 if (!--bdev
->bd_contains
->bd_holders
)
505 bdev
->bd_contains
->bd_holder
= NULL
;
506 if (!--bdev
->bd_holders
)
507 bdev
->bd_holder
= NULL
;
508 spin_unlock(&bdev_lock
);
511 EXPORT_SYMBOL(bd_release
);
515 * Functions for bd_claim_by_kobject / bd_release_from_kobject
517 * If a kobject is passed to bd_claim_by_kobject()
518 * and the kobject has a parent directory,
519 * following symlinks are created:
520 * o from the kobject to the claimed bdev
521 * o from "holders" directory of the bdev to the parent of the kobject
522 * bd_release_from_kobject() removes these symlinks.
525 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
526 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
527 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
528 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
531 static struct kobject
*bdev_get_kobj(struct block_device
*bdev
)
533 if (bdev
->bd_contains
!= bdev
)
534 return kobject_get(&bdev
->bd_part
->kobj
);
536 return kobject_get(&bdev
->bd_disk
->kobj
);
539 static struct kobject
*bdev_get_holder(struct block_device
*bdev
)
541 if (bdev
->bd_contains
!= bdev
)
542 return kobject_get(bdev
->bd_part
->holder_dir
);
544 return kobject_get(bdev
->bd_disk
->holder_dir
);
547 static void add_symlink(struct kobject
*from
, struct kobject
*to
)
551 sysfs_create_link(from
, to
, kobject_name(to
));
554 static void del_symlink(struct kobject
*from
, struct kobject
*to
)
558 sysfs_remove_link(from
, kobject_name(to
));
562 * 'struct bd_holder' contains pointers to kobjects symlinked by
563 * bd_claim_by_kobject.
564 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
567 struct list_head list
; /* chain of holders of the bdev */
568 int count
; /* references from the holder */
569 struct kobject
*sdir
; /* holder object, e.g. "/block/dm-0/slaves" */
570 struct kobject
*hdev
; /* e.g. "/block/dm-0" */
571 struct kobject
*hdir
; /* e.g. "/block/sda/holders" */
572 struct kobject
*sdev
; /* e.g. "/block/sda" */
576 * Get references of related kobjects at once.
577 * Returns 1 on success. 0 on failure.
579 * Should call bd_holder_release_dirs() after successful use.
581 static int bd_holder_grab_dirs(struct block_device
*bdev
,
582 struct bd_holder
*bo
)
587 bo
->sdir
= kobject_get(bo
->sdir
);
591 bo
->hdev
= kobject_get(bo
->sdir
->parent
);
595 bo
->sdev
= bdev_get_kobj(bdev
);
599 bo
->hdir
= bdev_get_holder(bdev
);
606 kobject_put(bo
->sdev
);
608 kobject_put(bo
->hdev
);
610 kobject_put(bo
->sdir
);
615 /* Put references of related kobjects at once. */
616 static void bd_holder_release_dirs(struct bd_holder
*bo
)
618 kobject_put(bo
->hdir
);
619 kobject_put(bo
->sdev
);
620 kobject_put(bo
->hdev
);
621 kobject_put(bo
->sdir
);
624 static struct bd_holder
*alloc_bd_holder(struct kobject
*kobj
)
626 struct bd_holder
*bo
;
628 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
638 static void free_bd_holder(struct bd_holder
*bo
)
644 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
646 * @bdev: block device to be bd_claimed
647 * @bo: preallocated and initialized by alloc_bd_holder()
649 * If there is no matching entry with @bo in @bdev->bd_holder_list,
650 * add @bo to the list, create symlinks.
652 * Returns 1 if @bo was added to the list.
653 * Returns 0 if @bo wasn't used by any reason and should be freed.
655 static int add_bd_holder(struct block_device
*bdev
, struct bd_holder
*bo
)
657 struct bd_holder
*tmp
;
662 list_for_each_entry(tmp
, &bdev
->bd_holder_list
, list
) {
663 if (tmp
->sdir
== bo
->sdir
) {
669 if (!bd_holder_grab_dirs(bdev
, bo
))
672 add_symlink(bo
->sdir
, bo
->sdev
);
673 add_symlink(bo
->hdir
, bo
->hdev
);
674 list_add_tail(&bo
->list
, &bdev
->bd_holder_list
);
679 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
681 * @bdev: block device to be bd_claimed
682 * @kobj: holder's kobject
684 * If there is matching entry with @kobj in @bdev->bd_holder_list
685 * and no other bd_claim() from the same kobject,
686 * remove the struct bd_holder from the list, delete symlinks for it.
688 * Returns a pointer to the struct bd_holder when it's removed from the list
689 * and ready to be freed.
690 * Returns NULL if matching claim isn't found or there is other bd_claim()
691 * by the same kobject.
693 static struct bd_holder
*del_bd_holder(struct block_device
*bdev
,
694 struct kobject
*kobj
)
696 struct bd_holder
*bo
;
698 list_for_each_entry(bo
, &bdev
->bd_holder_list
, list
) {
699 if (bo
->sdir
== kobj
) {
701 BUG_ON(bo
->count
< 0);
704 del_symlink(bo
->sdir
, bo
->sdev
);
705 del_symlink(bo
->hdir
, bo
->hdev
);
706 bd_holder_release_dirs(bo
);
717 * bd_claim_by_kobject - bd_claim() with additional kobject signature
719 * @bdev: block device to be claimed
720 * @holder: holder's signature
721 * @kobj: holder's kobject
723 * Do bd_claim() and if it succeeds, create sysfs symlinks between
724 * the bdev and the holder's kobject.
725 * Use bd_release_from_kobject() when relesing the claimed bdev.
727 * Returns 0 on success. (same as bd_claim())
728 * Returns errno on failure.
730 static int bd_claim_by_kobject(struct block_device
*bdev
, void *holder
,
731 struct kobject
*kobj
)
734 struct bd_holder
*bo
;
739 bo
= alloc_bd_holder(kobj
);
743 mutex_lock(&bdev
->bd_mutex
);
744 res
= bd_claim(bdev
, holder
);
745 if (res
|| !add_bd_holder(bdev
, bo
))
747 mutex_unlock(&bdev
->bd_mutex
);
753 * bd_release_from_kobject - bd_release() with additional kobject signature
755 * @bdev: block device to be released
756 * @kobj: holder's kobject
758 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
760 static void bd_release_from_kobject(struct block_device
*bdev
,
761 struct kobject
*kobj
)
763 struct bd_holder
*bo
;
768 mutex_lock(&bdev
->bd_mutex
);
770 if ((bo
= del_bd_holder(bdev
, kobj
)))
772 mutex_unlock(&bdev
->bd_mutex
);
776 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
778 * @bdev: block device to be claimed
779 * @holder: holder's signature
780 * @disk: holder's gendisk
782 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
784 int bd_claim_by_disk(struct block_device
*bdev
, void *holder
,
785 struct gendisk
*disk
)
787 return bd_claim_by_kobject(bdev
, holder
, kobject_get(disk
->slave_dir
));
789 EXPORT_SYMBOL_GPL(bd_claim_by_disk
);
792 * bd_release_from_disk - wrapper function for bd_release_from_kobject()
794 * @bdev: block device to be claimed
795 * @disk: holder's gendisk
797 * Call bd_release_from_kobject() and put @disk->slave_dir.
799 void bd_release_from_disk(struct block_device
*bdev
, struct gendisk
*disk
)
801 bd_release_from_kobject(bdev
, disk
->slave_dir
);
802 kobject_put(disk
->slave_dir
);
804 EXPORT_SYMBOL_GPL(bd_release_from_disk
);
808 * Tries to open block device by device number. Use it ONLY if you
809 * really do not have anything better - i.e. when you are behind a
810 * truly sucky interface and all you are given is a device number. _Never_
811 * to be used for internal purposes. If you ever need it - reconsider
814 struct block_device
*open_by_devnum(dev_t dev
, unsigned mode
)
816 struct block_device
*bdev
= bdget(dev
);
818 int flags
= mode
& FMODE_WRITE
? O_RDWR
: O_RDONLY
;
820 err
= blkdev_get(bdev
, mode
, flags
);
821 return err
? ERR_PTR(err
) : bdev
;
824 EXPORT_SYMBOL(open_by_devnum
);
827 * This routine checks whether a removable media has been changed,
828 * and invalidates all buffer-cache-entries in that case. This
829 * is a relatively slow routine, so we have to try to minimize using
830 * it. Thus it is called only upon a 'mount' or 'open'. This
831 * is the best way of combining speed and utility, I think.
832 * People changing diskettes in the middle of an operation deserve
835 int check_disk_change(struct block_device
*bdev
)
837 struct gendisk
*disk
= bdev
->bd_disk
;
838 struct block_device_operations
* bdops
= disk
->fops
;
840 if (!bdops
->media_changed
)
842 if (!bdops
->media_changed(bdev
->bd_disk
))
845 if (__invalidate_device(bdev
))
846 printk("VFS: busy inodes on changed media.\n");
848 if (bdops
->revalidate_disk
)
849 bdops
->revalidate_disk(bdev
->bd_disk
);
850 if (bdev
->bd_disk
->minors
> 1)
851 bdev
->bd_invalidated
= 1;
855 EXPORT_SYMBOL(check_disk_change
);
857 void bd_set_size(struct block_device
*bdev
, loff_t size
)
859 unsigned bsize
= bdev_hardsect_size(bdev
);
861 bdev
->bd_inode
->i_size
= size
;
862 while (bsize
< PAGE_CACHE_SIZE
) {
867 bdev
->bd_block_size
= bsize
;
868 bdev
->bd_inode
->i_blkbits
= blksize_bits(bsize
);
870 EXPORT_SYMBOL(bd_set_size
);
872 static int do_open(struct block_device
*bdev
, struct file
*file
)
874 struct module
*owner
= NULL
;
875 struct gendisk
*disk
;
879 file
->f_mapping
= bdev
->bd_inode
->i_mapping
;
881 disk
= get_gendisk(bdev
->bd_dev
, &part
);
887 owner
= disk
->fops
->owner
;
889 mutex_lock(&bdev
->bd_mutex
);
890 if (!bdev
->bd_openers
) {
891 bdev
->bd_disk
= disk
;
892 bdev
->bd_contains
= bdev
;
894 struct backing_dev_info
*bdi
;
895 if (disk
->fops
->open
) {
896 ret
= disk
->fops
->open(bdev
->bd_inode
, file
);
900 if (!bdev
->bd_openers
) {
901 bd_set_size(bdev
,(loff_t
)get_capacity(disk
)<<9);
902 bdi
= blk_get_backing_dev_info(bdev
);
904 bdi
= &default_backing_dev_info
;
905 bdev
->bd_inode
->i_data
.backing_dev_info
= bdi
;
907 if (bdev
->bd_invalidated
)
908 rescan_partitions(disk
, bdev
);
911 struct block_device
*whole
;
912 whole
= bdget_disk(disk
, 0);
916 ret
= blkdev_get(whole
, file
->f_mode
, file
->f_flags
);
919 bdev
->bd_contains
= whole
;
920 mutex_lock(&whole
->bd_mutex
);
921 whole
->bd_part_count
++;
922 p
= disk
->part
[part
- 1];
923 bdev
->bd_inode
->i_data
.backing_dev_info
=
924 whole
->bd_inode
->i_data
.backing_dev_info
;
925 if (!(disk
->flags
& GENHD_FL_UP
) || !p
|| !p
->nr_sects
) {
926 whole
->bd_part_count
--;
927 mutex_unlock(&whole
->bd_mutex
);
931 kobject_get(&p
->kobj
);
933 bd_set_size(bdev
, (loff_t
) p
->nr_sects
<< 9);
934 mutex_unlock(&whole
->bd_mutex
);
939 if (bdev
->bd_contains
== bdev
) {
940 if (bdev
->bd_disk
->fops
->open
) {
941 ret
= bdev
->bd_disk
->fops
->open(bdev
->bd_inode
, file
);
945 if (bdev
->bd_invalidated
)
946 rescan_partitions(bdev
->bd_disk
, bdev
);
948 mutex_lock(&bdev
->bd_contains
->bd_mutex
);
949 bdev
->bd_contains
->bd_part_count
++;
950 mutex_unlock(&bdev
->bd_contains
->bd_mutex
);
954 mutex_unlock(&bdev
->bd_mutex
);
959 bdev
->bd_disk
= NULL
;
960 bdev
->bd_inode
->i_data
.backing_dev_info
= &default_backing_dev_info
;
961 if (bdev
!= bdev
->bd_contains
)
962 blkdev_put(bdev
->bd_contains
);
963 bdev
->bd_contains
= NULL
;
967 mutex_unlock(&bdev
->bd_mutex
);
974 int blkdev_get(struct block_device
*bdev
, mode_t mode
, unsigned flags
)
977 * This crockload is due to bad choice of ->open() type.
979 * For now, block device ->open() routine must _not_
980 * examine anything in 'inode' argument except ->i_rdev.
982 struct file fake_file
= {};
983 struct dentry fake_dentry
= {};
984 fake_file
.f_mode
= mode
;
985 fake_file
.f_flags
= flags
;
986 fake_file
.f_dentry
= &fake_dentry
;
987 fake_dentry
.d_inode
= bdev
->bd_inode
;
989 return do_open(bdev
, &fake_file
);
992 EXPORT_SYMBOL(blkdev_get
);
994 static int blkdev_open(struct inode
* inode
, struct file
* filp
)
996 struct block_device
*bdev
;
1000 * Preserve backwards compatibility and allow large file access
1001 * even if userspace doesn't ask for it explicitly. Some mkfs
1002 * binary needs it. We might want to drop this workaround
1003 * during an unstable branch.
1005 filp
->f_flags
|= O_LARGEFILE
;
1007 bdev
= bd_acquire(inode
);
1009 res
= do_open(bdev
, filp
);
1013 if (!(filp
->f_flags
& O_EXCL
) )
1016 if (!(res
= bd_claim(bdev
, filp
)))
1023 int blkdev_put(struct block_device
*bdev
)
1026 struct inode
*bd_inode
= bdev
->bd_inode
;
1027 struct gendisk
*disk
= bdev
->bd_disk
;
1029 mutex_lock(&bdev
->bd_mutex
);
1031 if (!--bdev
->bd_openers
) {
1032 sync_blockdev(bdev
);
1035 if (bdev
->bd_contains
== bdev
) {
1036 if (disk
->fops
->release
)
1037 ret
= disk
->fops
->release(bd_inode
, NULL
);
1039 mutex_lock(&bdev
->bd_contains
->bd_mutex
);
1040 bdev
->bd_contains
->bd_part_count
--;
1041 mutex_unlock(&bdev
->bd_contains
->bd_mutex
);
1043 if (!bdev
->bd_openers
) {
1044 struct module
*owner
= disk
->fops
->owner
;
1049 if (bdev
->bd_contains
!= bdev
) {
1050 kobject_put(&bdev
->bd_part
->kobj
);
1051 bdev
->bd_part
= NULL
;
1053 bdev
->bd_disk
= NULL
;
1054 bdev
->bd_inode
->i_data
.backing_dev_info
= &default_backing_dev_info
;
1055 if (bdev
!= bdev
->bd_contains
) {
1056 blkdev_put(bdev
->bd_contains
);
1058 bdev
->bd_contains
= NULL
;
1061 mutex_unlock(&bdev
->bd_mutex
);
1066 EXPORT_SYMBOL(blkdev_put
);
1068 static int blkdev_close(struct inode
* inode
, struct file
* filp
)
1070 struct block_device
*bdev
= I_BDEV(filp
->f_mapping
->host
);
1071 if (bdev
->bd_holder
== filp
)
1073 return blkdev_put(bdev
);
1076 static ssize_t
blkdev_file_write(struct file
*file
, const char __user
*buf
,
1077 size_t count
, loff_t
*ppos
)
1079 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= count
};
1081 return generic_file_write_nolock(file
, &local_iov
, 1, ppos
);
1084 static ssize_t
blkdev_file_aio_write(struct kiocb
*iocb
, const char __user
*buf
,
1085 size_t count
, loff_t pos
)
1087 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= count
};
1089 return generic_file_aio_write_nolock(iocb
, &local_iov
, 1, &iocb
->ki_pos
);
1092 static long block_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
1094 return blkdev_ioctl(file
->f_mapping
->host
, file
, cmd
, arg
);
1097 const struct address_space_operations def_blk_aops
= {
1098 .readpage
= blkdev_readpage
,
1099 .writepage
= blkdev_writepage
,
1100 .sync_page
= block_sync_page
,
1101 .prepare_write
= blkdev_prepare_write
,
1102 .commit_write
= blkdev_commit_write
,
1103 .writepages
= generic_writepages
,
1104 .direct_IO
= blkdev_direct_IO
,
1107 const struct file_operations def_blk_fops
= {
1108 .open
= blkdev_open
,
1109 .release
= blkdev_close
,
1110 .llseek
= block_llseek
,
1111 .read
= generic_file_read
,
1112 .write
= blkdev_file_write
,
1113 .aio_read
= generic_file_aio_read
,
1114 .aio_write
= blkdev_file_aio_write
,
1115 .mmap
= generic_file_mmap
,
1116 .fsync
= block_fsync
,
1117 .unlocked_ioctl
= block_ioctl
,
1118 #ifdef CONFIG_COMPAT
1119 .compat_ioctl
= compat_blkdev_ioctl
,
1121 .readv
= generic_file_readv
,
1122 .writev
= generic_file_write_nolock
,
1123 .sendfile
= generic_file_sendfile
,
1124 .splice_read
= generic_file_splice_read
,
1125 .splice_write
= generic_file_splice_write
,
1128 int ioctl_by_bdev(struct block_device
*bdev
, unsigned cmd
, unsigned long arg
)
1131 mm_segment_t old_fs
= get_fs();
1133 res
= blkdev_ioctl(bdev
->bd_inode
, NULL
, cmd
, arg
);
1138 EXPORT_SYMBOL(ioctl_by_bdev
);
1141 * lookup_bdev - lookup a struct block_device by name
1143 * @path: special file representing the block device
1145 * Get a reference to the blockdevice at @path in the current
1146 * namespace if possible and return it. Return ERR_PTR(error)
1149 struct block_device
*lookup_bdev(const char *path
)
1151 struct block_device
*bdev
;
1152 struct inode
*inode
;
1153 struct nameidata nd
;
1156 if (!path
|| !*path
)
1157 return ERR_PTR(-EINVAL
);
1159 error
= path_lookup(path
, LOOKUP_FOLLOW
, &nd
);
1161 return ERR_PTR(error
);
1163 inode
= nd
.dentry
->d_inode
;
1165 if (!S_ISBLK(inode
->i_mode
))
1168 if (nd
.mnt
->mnt_flags
& MNT_NODEV
)
1171 bdev
= bd_acquire(inode
);
1178 bdev
= ERR_PTR(error
);
1183 * open_bdev_excl - open a block device by name and set it up for use
1185 * @path: special file representing the block device
1186 * @flags: %MS_RDONLY for opening read-only
1187 * @holder: owner for exclusion
1189 * Open the blockdevice described by the special file at @path, claim it
1192 struct block_device
*open_bdev_excl(const char *path
, int flags
, void *holder
)
1194 struct block_device
*bdev
;
1195 mode_t mode
= FMODE_READ
;
1198 bdev
= lookup_bdev(path
);
1202 if (!(flags
& MS_RDONLY
))
1203 mode
|= FMODE_WRITE
;
1204 error
= blkdev_get(bdev
, mode
, 0);
1206 return ERR_PTR(error
);
1208 if (!(flags
& MS_RDONLY
) && bdev_read_only(bdev
))
1210 error
= bd_claim(bdev
, holder
);
1218 return ERR_PTR(error
);
1221 EXPORT_SYMBOL(open_bdev_excl
);
1224 * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
1226 * @bdev: blockdevice to close
1228 * This is the counterpart to open_bdev_excl().
1230 void close_bdev_excl(struct block_device
*bdev
)
1236 EXPORT_SYMBOL(close_bdev_excl
);