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/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>
30 struct block_device bdev
;
31 struct inode vfs_inode
;
34 static const struct address_space_operations def_blk_aops
;
36 static inline struct bdev_inode
*BDEV_I(struct inode
*inode
)
38 return container_of(inode
, struct bdev_inode
, vfs_inode
);
41 inline struct block_device
*I_BDEV(struct inode
*inode
)
43 return &BDEV_I(inode
)->bdev
;
46 EXPORT_SYMBOL(I_BDEV
);
48 static sector_t
max_block(struct block_device
*bdev
)
50 sector_t retval
= ~((sector_t
)0);
51 loff_t sz
= i_size_read(bdev
->bd_inode
);
54 unsigned int size
= block_size(bdev
);
55 unsigned int sizebits
= blksize_bits(size
);
56 retval
= (sz
>> sizebits
);
61 /* Kill _all_ buffers and pagecache , dirty or not.. */
62 static void kill_bdev(struct block_device
*bdev
)
64 if (bdev
->bd_inode
->i_mapping
->nrpages
== 0)
67 truncate_inode_pages(bdev
->bd_inode
->i_mapping
, 0);
70 int set_blocksize(struct block_device
*bdev
, int size
)
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
))
76 /* Size cannot be smaller than the size supported by the device */
77 if (size
< bdev_hardsect_size(bdev
))
80 /* Don't change the size if it is same as current */
81 if (bdev
->bd_block_size
!= size
) {
83 bdev
->bd_block_size
= size
;
84 bdev
->bd_inode
->i_blkbits
= blksize_bits(size
);
90 EXPORT_SYMBOL(set_blocksize
);
92 int sb_set_blocksize(struct super_block
*sb
, int size
)
94 if (set_blocksize(sb
->s_bdev
, size
))
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
;
103 EXPORT_SYMBOL(sb_set_blocksize
);
105 int sb_min_blocksize(struct super_block
*sb
, int size
)
107 int minsize
= bdev_hardsect_size(sb
->s_bdev
);
110 return sb_set_blocksize(sb
, size
);
113 EXPORT_SYMBOL(sb_min_blocksize
);
116 blkdev_get_block(struct inode
*inode
, sector_t iblock
,
117 struct buffer_head
*bh
, int create
)
119 if (iblock
>= max_block(I_BDEV(inode
))) {
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
131 bh
->b_bdev
= I_BDEV(inode
);
132 bh
->b_blocknr
= iblock
;
133 set_buffer_mapped(bh
);
138 blkdev_get_blocks(struct inode
*inode
, sector_t iblock
,
139 struct buffer_head
*bh
, int create
)
141 sector_t end_block
= max_block(I_BDEV(inode
));
142 unsigned long max_blocks
= bh
->b_size
>> inode
->i_blkbits
;
144 if ((iblock
+ max_blocks
) > end_block
) {
145 max_blocks
= end_block
- iblock
;
146 if ((long)max_blocks
<= 0) {
148 return -EIO
; /* write fully beyond EOF */
150 * It is a read which is fully beyond EOF. We return
151 * a !buffer_mapped buffer
157 bh
->b_bdev
= I_BDEV(inode
);
158 bh
->b_blocknr
= iblock
;
159 bh
->b_size
= max_blocks
<< inode
->i_blkbits
;
161 set_buffer_mapped(bh
);
166 blkdev_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
167 loff_t offset
, unsigned long nr_segs
)
169 struct file
*file
= iocb
->ki_filp
;
170 struct inode
*inode
= file
->f_mapping
->host
;
172 return blockdev_direct_IO_no_locking(rw
, iocb
, inode
, I_BDEV(inode
),
173 iov
, offset
, nr_segs
, blkdev_get_blocks
, NULL
);
177 static void blk_end_aio(struct bio
*bio
, int error
)
179 struct kiocb
*iocb
= bio
->bi_private
;
180 atomic_t
*bio_count
= &iocb
->ki_bio_count
;
182 if (bio_data_dir(bio
) == READ
)
183 bio_check_pages_dirty(bio
);
185 bio_release_pages(bio
);
189 /* iocb->ki_nbytes stores error code from LLDD */
191 iocb
->ki_nbytes
= -EIO
;
193 if (atomic_dec_and_test(bio_count
)) {
194 if ((long)iocb
->ki_nbytes
< 0)
195 aio_complete(iocb
, iocb
->ki_nbytes
, 0);
197 aio_complete(iocb
, iocb
->ki_left
, 0);
207 struct page
*page
[VEC_SIZE
];
210 #define PAGES_SPANNED(addr, len) \
211 (DIV_ROUND_UP((addr) + (len), PAGE_SIZE) - (addr) / PAGE_SIZE);
214 * get page pointer for user addr, we internally cache struct page array for
215 * (addr, count) range in pvec to avoid frequent call to get_user_pages. If
216 * internal page list is exhausted, a batch count of up to VEC_SIZE is used
217 * to get next set of page struct.
219 static struct page
*blk_get_page(unsigned long addr
, size_t count
, int rw
,
223 if (pvec
->idx
== pvec
->nr
) {
224 nr_pages
= PAGES_SPANNED(addr
, count
);
225 nr_pages
= min(nr_pages
, VEC_SIZE
);
226 down_read(¤t
->mm
->mmap_sem
);
227 ret
= get_user_pages(current
, current
->mm
, addr
, nr_pages
,
228 rw
== READ
, 0, pvec
->page
, NULL
);
229 up_read(¤t
->mm
->mmap_sem
);
235 return pvec
->page
[pvec
->idx
++];
238 /* return a page back to pvec array */
239 static void blk_unget_page(struct page
*page
, struct pvec
*pvec
)
241 pvec
->page
[--pvec
->idx
] = page
;
245 blkdev_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
246 loff_t pos
, unsigned long nr_segs
)
248 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
249 unsigned blkbits
= blksize_bits(bdev_hardsect_size(I_BDEV(inode
)));
250 unsigned blocksize_mask
= (1 << blkbits
) - 1;
251 unsigned long seg
= 0; /* iov segment iterator */
252 unsigned long nvec
; /* number of bio vec needed */
253 unsigned long cur_off
; /* offset into current page */
254 unsigned long cur_len
; /* I/O len of current page, up to PAGE_SIZE */
256 unsigned long addr
; /* user iovec address */
257 size_t count
; /* user iovec len */
258 size_t nbytes
= iocb
->ki_nbytes
= iocb
->ki_left
; /* total xfer size */
259 loff_t size
; /* size of block device */
261 atomic_t
*bio_count
= &iocb
->ki_bio_count
;
268 if (pos
& blocksize_mask
)
271 size
= i_size_read(inode
);
272 if (pos
+ nbytes
> size
) {
274 iocb
->ki_left
= nbytes
;
278 * check first non-zero iov alignment, the remaining
279 * iov alignment is checked inside bio loop below.
282 addr
= (unsigned long) iov
[seg
].iov_base
;
283 count
= min(iov
[seg
].iov_len
, nbytes
);
284 if (addr
& blocksize_mask
|| count
& blocksize_mask
)
286 } while (!count
&& ++seg
< nr_segs
);
287 atomic_set(bio_count
, 1);
290 /* roughly estimate number of bio vec needed */
291 nvec
= (nbytes
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
292 nvec
= max(nvec
, nr_segs
- seg
);
293 nvec
= min(nvec
, (unsigned long) BIO_MAX_PAGES
);
295 /* bio_alloc should not fail with GFP_KERNEL flag */
296 bio
= bio_alloc(GFP_KERNEL
, nvec
);
297 bio
->bi_bdev
= I_BDEV(inode
);
298 bio
->bi_end_io
= blk_end_aio
;
299 bio
->bi_private
= iocb
;
300 bio
->bi_sector
= pos
>> blkbits
;
302 cur_off
= addr
& ~PAGE_MASK
;
303 cur_len
= PAGE_SIZE
- cur_off
;
307 page
= blk_get_page(addr
, count
, rw
, &pvec
);
308 if (unlikely(IS_ERR(page
)))
311 if (bio_add_page(bio
, page
, cur_len
, cur_off
)) {
319 while (++seg
< nr_segs
) {
320 addr
= (unsigned long) iov
[seg
].iov_base
;
321 count
= iov
[seg
].iov_len
;
324 if (unlikely(addr
& blocksize_mask
||
325 count
& blocksize_mask
)) {
326 page
= ERR_PTR(-EINVAL
);
329 count
= min(count
, nbytes
);
333 blk_unget_page(page
, &pvec
);
336 /* bio is ready, submit it */
338 bio_set_pages_dirty(bio
);
339 atomic_inc(bio_count
);
344 iocb
->ki_left
-= nbytes
;
345 nbytes
= iocb
->ki_left
;
346 iocb
->ki_pos
+= nbytes
;
348 blk_run_address_space(inode
->i_mapping
);
349 if (atomic_dec_and_test(bio_count
))
350 aio_complete(iocb
, nbytes
, 0);
356 * back out nbytes count constructed so far for this bio,
357 * we will throw away current bio.
359 nbytes
+= bio
->bi_size
;
360 bio_release_pages(bio
);
364 * if no bio was submmitted, return the error code.
365 * otherwise, proceed with pending I/O completion.
367 if (atomic_read(bio_count
) == 1)
368 return PTR_ERR(page
);
373 static int blkdev_writepage(struct page
*page
, struct writeback_control
*wbc
)
375 return block_write_full_page(page
, blkdev_get_block
, wbc
);
378 static int blkdev_readpage(struct file
* file
, struct page
* page
)
380 return block_read_full_page(page
, blkdev_get_block
);
383 static int blkdev_write_begin(struct file
*file
, struct address_space
*mapping
,
384 loff_t pos
, unsigned len
, unsigned flags
,
385 struct page
**pagep
, void **fsdata
)
388 return block_write_begin(file
, mapping
, pos
, len
, flags
, pagep
, fsdata
,
392 static int blkdev_write_end(struct file
*file
, struct address_space
*mapping
,
393 loff_t pos
, unsigned len
, unsigned copied
,
394 struct page
*page
, void *fsdata
)
397 ret
= block_write_end(file
, mapping
, pos
, len
, copied
, page
, fsdata
);
400 page_cache_release(page
);
407 * for a block special file file->f_path.dentry->d_inode->i_size is zero
408 * so we compute the size by hand (just as in block_read/write above)
410 static loff_t
block_llseek(struct file
*file
, loff_t offset
, int origin
)
412 struct inode
*bd_inode
= file
->f_mapping
->host
;
416 mutex_lock(&bd_inode
->i_mutex
);
417 size
= i_size_read(bd_inode
);
424 offset
+= file
->f_pos
;
427 if (offset
>= 0 && offset
<= size
) {
428 if (offset
!= file
->f_pos
) {
429 file
->f_pos
= offset
;
433 mutex_unlock(&bd_inode
->i_mutex
);
438 * Filp is never NULL; the only case when ->fsync() is called with
439 * NULL first argument is nfsd_sync_dir() and that's not a directory.
442 static int block_fsync(struct file
*filp
, struct dentry
*dentry
, int datasync
)
444 return sync_blockdev(I_BDEV(filp
->f_mapping
->host
));
451 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(bdev_lock
);
452 static struct kmem_cache
* bdev_cachep __read_mostly
;
454 static struct inode
*bdev_alloc_inode(struct super_block
*sb
)
456 struct bdev_inode
*ei
= kmem_cache_alloc(bdev_cachep
, GFP_KERNEL
);
459 return &ei
->vfs_inode
;
462 static void bdev_destroy_inode(struct inode
*inode
)
464 struct bdev_inode
*bdi
= BDEV_I(inode
);
466 bdi
->bdev
.bd_inode_backing_dev_info
= NULL
;
467 kmem_cache_free(bdev_cachep
, bdi
);
470 static void init_once(struct kmem_cache
* cachep
, void *foo
)
472 struct bdev_inode
*ei
= (struct bdev_inode
*) foo
;
473 struct block_device
*bdev
= &ei
->bdev
;
475 memset(bdev
, 0, sizeof(*bdev
));
476 mutex_init(&bdev
->bd_mutex
);
477 sema_init(&bdev
->bd_mount_sem
, 1);
478 INIT_LIST_HEAD(&bdev
->bd_inodes
);
479 INIT_LIST_HEAD(&bdev
->bd_list
);
481 INIT_LIST_HEAD(&bdev
->bd_holder_list
);
483 inode_init_once(&ei
->vfs_inode
);
486 static inline void __bd_forget(struct inode
*inode
)
488 list_del_init(&inode
->i_devices
);
489 inode
->i_bdev
= NULL
;
490 inode
->i_mapping
= &inode
->i_data
;
493 static void bdev_clear_inode(struct inode
*inode
)
495 struct block_device
*bdev
= &BDEV_I(inode
)->bdev
;
497 spin_lock(&bdev_lock
);
498 while ( (p
= bdev
->bd_inodes
.next
) != &bdev
->bd_inodes
) {
499 __bd_forget(list_entry(p
, struct inode
, i_devices
));
501 list_del_init(&bdev
->bd_list
);
502 spin_unlock(&bdev_lock
);
505 static const struct super_operations bdev_sops
= {
506 .statfs
= simple_statfs
,
507 .alloc_inode
= bdev_alloc_inode
,
508 .destroy_inode
= bdev_destroy_inode
,
509 .drop_inode
= generic_delete_inode
,
510 .clear_inode
= bdev_clear_inode
,
513 static int bd_get_sb(struct file_system_type
*fs_type
,
514 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
516 return get_sb_pseudo(fs_type
, "bdev:", &bdev_sops
, 0x62646576, mnt
);
519 static struct file_system_type bd_type
= {
522 .kill_sb
= kill_anon_super
,
525 static struct vfsmount
*bd_mnt __read_mostly
;
526 struct super_block
*blockdev_superblock
;
528 void __init
bdev_cache_init(void)
531 bdev_cachep
= kmem_cache_create("bdev_cache", sizeof(struct bdev_inode
),
532 0, (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
533 SLAB_MEM_SPREAD
|SLAB_PANIC
),
535 err
= register_filesystem(&bd_type
);
537 panic("Cannot register bdev pseudo-fs");
538 bd_mnt
= kern_mount(&bd_type
);
540 panic("Cannot create bdev pseudo-fs");
541 blockdev_superblock
= bd_mnt
->mnt_sb
; /* For writeback */
545 * Most likely _very_ bad one - but then it's hardly critical for small
546 * /dev and can be fixed when somebody will need really large one.
547 * Keep in mind that it will be fed through icache hash function too.
549 static inline unsigned long hash(dev_t dev
)
551 return MAJOR(dev
)+MINOR(dev
);
554 static int bdev_test(struct inode
*inode
, void *data
)
556 return BDEV_I(inode
)->bdev
.bd_dev
== *(dev_t
*)data
;
559 static int bdev_set(struct inode
*inode
, void *data
)
561 BDEV_I(inode
)->bdev
.bd_dev
= *(dev_t
*)data
;
565 static LIST_HEAD(all_bdevs
);
567 struct block_device
*bdget(dev_t dev
)
569 struct block_device
*bdev
;
572 inode
= iget5_locked(bd_mnt
->mnt_sb
, hash(dev
),
573 bdev_test
, bdev_set
, &dev
);
578 bdev
= &BDEV_I(inode
)->bdev
;
580 if (inode
->i_state
& I_NEW
) {
581 bdev
->bd_contains
= NULL
;
582 bdev
->bd_inode
= inode
;
583 bdev
->bd_block_size
= (1 << inode
->i_blkbits
);
584 bdev
->bd_part_count
= 0;
585 bdev
->bd_invalidated
= 0;
586 inode
->i_mode
= S_IFBLK
;
588 inode
->i_bdev
= bdev
;
589 inode
->i_data
.a_ops
= &def_blk_aops
;
590 mapping_set_gfp_mask(&inode
->i_data
, GFP_USER
);
591 inode
->i_data
.backing_dev_info
= &default_backing_dev_info
;
592 spin_lock(&bdev_lock
);
593 list_add(&bdev
->bd_list
, &all_bdevs
);
594 spin_unlock(&bdev_lock
);
595 unlock_new_inode(inode
);
600 EXPORT_SYMBOL(bdget
);
602 long nr_blockdev_pages(void)
604 struct block_device
*bdev
;
606 spin_lock(&bdev_lock
);
607 list_for_each_entry(bdev
, &all_bdevs
, bd_list
) {
608 ret
+= bdev
->bd_inode
->i_mapping
->nrpages
;
610 spin_unlock(&bdev_lock
);
614 void bdput(struct block_device
*bdev
)
616 iput(bdev
->bd_inode
);
619 EXPORT_SYMBOL(bdput
);
621 static struct block_device
*bd_acquire(struct inode
*inode
)
623 struct block_device
*bdev
;
625 spin_lock(&bdev_lock
);
626 bdev
= inode
->i_bdev
;
628 atomic_inc(&bdev
->bd_inode
->i_count
);
629 spin_unlock(&bdev_lock
);
632 spin_unlock(&bdev_lock
);
634 bdev
= bdget(inode
->i_rdev
);
636 spin_lock(&bdev_lock
);
637 if (!inode
->i_bdev
) {
639 * We take an additional bd_inode->i_count for inode,
640 * and it's released in clear_inode() of inode.
641 * So, we can access it via ->i_mapping always
644 atomic_inc(&bdev
->bd_inode
->i_count
);
645 inode
->i_bdev
= bdev
;
646 inode
->i_mapping
= bdev
->bd_inode
->i_mapping
;
647 list_add(&inode
->i_devices
, &bdev
->bd_inodes
);
649 spin_unlock(&bdev_lock
);
654 /* Call when you free inode */
656 void bd_forget(struct inode
*inode
)
658 struct block_device
*bdev
= NULL
;
660 spin_lock(&bdev_lock
);
662 if (inode
->i_sb
!= blockdev_superblock
)
663 bdev
= inode
->i_bdev
;
666 spin_unlock(&bdev_lock
);
669 iput(bdev
->bd_inode
);
672 int bd_claim(struct block_device
*bdev
, void *holder
)
675 spin_lock(&bdev_lock
);
677 /* first decide result */
678 if (bdev
->bd_holder
== holder
)
679 res
= 0; /* already a holder */
680 else if (bdev
->bd_holder
!= NULL
)
681 res
= -EBUSY
; /* held by someone else */
682 else if (bdev
->bd_contains
== bdev
)
683 res
= 0; /* is a whole device which isn't held */
685 else if (bdev
->bd_contains
->bd_holder
== bd_claim
)
686 res
= 0; /* is a partition of a device that is being partitioned */
687 else if (bdev
->bd_contains
->bd_holder
!= NULL
)
688 res
= -EBUSY
; /* is a partition of a held device */
690 res
= 0; /* is a partition of an un-held device */
692 /* now impose change */
694 /* note that for a whole device bd_holders
695 * will be incremented twice, and bd_holder will
696 * be set to bd_claim before being set to holder
698 bdev
->bd_contains
->bd_holders
++;
699 bdev
->bd_contains
->bd_holder
= bd_claim
;
701 bdev
->bd_holder
= holder
;
703 spin_unlock(&bdev_lock
);
707 EXPORT_SYMBOL(bd_claim
);
709 void bd_release(struct block_device
*bdev
)
711 spin_lock(&bdev_lock
);
712 if (!--bdev
->bd_contains
->bd_holders
)
713 bdev
->bd_contains
->bd_holder
= NULL
;
714 if (!--bdev
->bd_holders
)
715 bdev
->bd_holder
= NULL
;
716 spin_unlock(&bdev_lock
);
719 EXPORT_SYMBOL(bd_release
);
723 * Functions for bd_claim_by_kobject / bd_release_from_kobject
725 * If a kobject is passed to bd_claim_by_kobject()
726 * and the kobject has a parent directory,
727 * following symlinks are created:
728 * o from the kobject to the claimed bdev
729 * o from "holders" directory of the bdev to the parent of the kobject
730 * bd_release_from_kobject() removes these symlinks.
733 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
734 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
735 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
736 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
739 static struct kobject
*bdev_get_kobj(struct block_device
*bdev
)
741 if (bdev
->bd_contains
!= bdev
)
742 return kobject_get(&bdev
->bd_part
->dev
.kobj
);
744 return kobject_get(&bdev
->bd_disk
->dev
.kobj
);
747 static struct kobject
*bdev_get_holder(struct block_device
*bdev
)
749 if (bdev
->bd_contains
!= bdev
)
750 return kobject_get(bdev
->bd_part
->holder_dir
);
752 return kobject_get(bdev
->bd_disk
->holder_dir
);
755 static int add_symlink(struct kobject
*from
, struct kobject
*to
)
759 return sysfs_create_link(from
, to
, kobject_name(to
));
762 static void del_symlink(struct kobject
*from
, struct kobject
*to
)
766 sysfs_remove_link(from
, kobject_name(to
));
770 * 'struct bd_holder' contains pointers to kobjects symlinked by
771 * bd_claim_by_kobject.
772 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
775 struct list_head list
; /* chain of holders of the bdev */
776 int count
; /* references from the holder */
777 struct kobject
*sdir
; /* holder object, e.g. "/block/dm-0/slaves" */
778 struct kobject
*hdev
; /* e.g. "/block/dm-0" */
779 struct kobject
*hdir
; /* e.g. "/block/sda/holders" */
780 struct kobject
*sdev
; /* e.g. "/block/sda" */
784 * Get references of related kobjects at once.
785 * Returns 1 on success. 0 on failure.
787 * Should call bd_holder_release_dirs() after successful use.
789 static int bd_holder_grab_dirs(struct block_device
*bdev
,
790 struct bd_holder
*bo
)
795 bo
->sdir
= kobject_get(bo
->sdir
);
799 bo
->hdev
= kobject_get(bo
->sdir
->parent
);
803 bo
->sdev
= bdev_get_kobj(bdev
);
807 bo
->hdir
= bdev_get_holder(bdev
);
814 kobject_put(bo
->sdev
);
816 kobject_put(bo
->hdev
);
818 kobject_put(bo
->sdir
);
823 /* Put references of related kobjects at once. */
824 static void bd_holder_release_dirs(struct bd_holder
*bo
)
826 kobject_put(bo
->hdir
);
827 kobject_put(bo
->sdev
);
828 kobject_put(bo
->hdev
);
829 kobject_put(bo
->sdir
);
832 static struct bd_holder
*alloc_bd_holder(struct kobject
*kobj
)
834 struct bd_holder
*bo
;
836 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
846 static void free_bd_holder(struct bd_holder
*bo
)
852 * find_bd_holder - find matching struct bd_holder from the block device
854 * @bdev: struct block device to be searched
855 * @bo: target struct bd_holder
857 * Returns matching entry with @bo in @bdev->bd_holder_list.
858 * If found, increment the reference count and return the pointer.
859 * If not found, returns NULL.
861 static struct bd_holder
*find_bd_holder(struct block_device
*bdev
,
862 struct bd_holder
*bo
)
864 struct bd_holder
*tmp
;
866 list_for_each_entry(tmp
, &bdev
->bd_holder_list
, list
)
867 if (tmp
->sdir
== bo
->sdir
) {
876 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
878 * @bdev: block device to be bd_claimed
879 * @bo: preallocated and initialized by alloc_bd_holder()
881 * Add @bo to @bdev->bd_holder_list, create symlinks.
883 * Returns 0 if symlinks are created.
884 * Returns -ve if something fails.
886 static int add_bd_holder(struct block_device
*bdev
, struct bd_holder
*bo
)
893 if (!bd_holder_grab_dirs(bdev
, bo
))
896 err
= add_symlink(bo
->sdir
, bo
->sdev
);
900 err
= add_symlink(bo
->hdir
, bo
->hdev
);
902 del_symlink(bo
->sdir
, bo
->sdev
);
906 list_add_tail(&bo
->list
, &bdev
->bd_holder_list
);
911 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
913 * @bdev: block device to be bd_claimed
914 * @kobj: holder's kobject
916 * If there is matching entry with @kobj in @bdev->bd_holder_list
917 * and no other bd_claim() from the same kobject,
918 * remove the struct bd_holder from the list, delete symlinks for it.
920 * Returns a pointer to the struct bd_holder when it's removed from the list
921 * and ready to be freed.
922 * Returns NULL if matching claim isn't found or there is other bd_claim()
923 * by the same kobject.
925 static struct bd_holder
*del_bd_holder(struct block_device
*bdev
,
926 struct kobject
*kobj
)
928 struct bd_holder
*bo
;
930 list_for_each_entry(bo
, &bdev
->bd_holder_list
, list
) {
931 if (bo
->sdir
== kobj
) {
933 BUG_ON(bo
->count
< 0);
936 del_symlink(bo
->sdir
, bo
->sdev
);
937 del_symlink(bo
->hdir
, bo
->hdev
);
938 bd_holder_release_dirs(bo
);
949 * bd_claim_by_kobject - bd_claim() with additional kobject signature
951 * @bdev: block device to be claimed
952 * @holder: holder's signature
953 * @kobj: holder's kobject
955 * Do bd_claim() and if it succeeds, create sysfs symlinks between
956 * the bdev and the holder's kobject.
957 * Use bd_release_from_kobject() when relesing the claimed bdev.
959 * Returns 0 on success. (same as bd_claim())
960 * Returns errno on failure.
962 static int bd_claim_by_kobject(struct block_device
*bdev
, void *holder
,
963 struct kobject
*kobj
)
966 struct bd_holder
*bo
, *found
;
971 bo
= alloc_bd_holder(kobj
);
975 mutex_lock(&bdev
->bd_mutex
);
977 err
= bd_claim(bdev
, holder
);
981 found
= find_bd_holder(bdev
, bo
);
985 err
= add_bd_holder(bdev
, bo
);
991 mutex_unlock(&bdev
->bd_mutex
);
997 * bd_release_from_kobject - bd_release() with additional kobject signature
999 * @bdev: block device to be released
1000 * @kobj: holder's kobject
1002 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
1004 static void bd_release_from_kobject(struct block_device
*bdev
,
1005 struct kobject
*kobj
)
1010 mutex_lock(&bdev
->bd_mutex
);
1012 free_bd_holder(del_bd_holder(bdev
, kobj
));
1013 mutex_unlock(&bdev
->bd_mutex
);
1017 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
1019 * @bdev: block device to be claimed
1020 * @holder: holder's signature
1021 * @disk: holder's gendisk
1023 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
1025 int bd_claim_by_disk(struct block_device
*bdev
, void *holder
,
1026 struct gendisk
*disk
)
1028 return bd_claim_by_kobject(bdev
, holder
, kobject_get(disk
->slave_dir
));
1030 EXPORT_SYMBOL_GPL(bd_claim_by_disk
);
1033 * bd_release_from_disk - wrapper function for bd_release_from_kobject()
1035 * @bdev: block device to be claimed
1036 * @disk: holder's gendisk
1038 * Call bd_release_from_kobject() and put @disk->slave_dir.
1040 void bd_release_from_disk(struct block_device
*bdev
, struct gendisk
*disk
)
1042 bd_release_from_kobject(bdev
, disk
->slave_dir
);
1043 kobject_put(disk
->slave_dir
);
1045 EXPORT_SYMBOL_GPL(bd_release_from_disk
);
1049 * Tries to open block device by device number. Use it ONLY if you
1050 * really do not have anything better - i.e. when you are behind a
1051 * truly sucky interface and all you are given is a device number. _Never_
1052 * to be used for internal purposes. If you ever need it - reconsider
1055 struct block_device
*open_by_devnum(dev_t dev
, unsigned mode
)
1057 struct block_device
*bdev
= bdget(dev
);
1059 int flags
= mode
& FMODE_WRITE
? O_RDWR
: O_RDONLY
;
1061 err
= blkdev_get(bdev
, mode
, flags
);
1062 return err
? ERR_PTR(err
) : bdev
;
1065 EXPORT_SYMBOL(open_by_devnum
);
1068 * This routine checks whether a removable media has been changed,
1069 * and invalidates all buffer-cache-entries in that case. This
1070 * is a relatively slow routine, so we have to try to minimize using
1071 * it. Thus it is called only upon a 'mount' or 'open'. This
1072 * is the best way of combining speed and utility, I think.
1073 * People changing diskettes in the middle of an operation deserve
1076 int check_disk_change(struct block_device
*bdev
)
1078 struct gendisk
*disk
= bdev
->bd_disk
;
1079 struct block_device_operations
* bdops
= disk
->fops
;
1081 if (!bdops
->media_changed
)
1083 if (!bdops
->media_changed(bdev
->bd_disk
))
1086 if (__invalidate_device(bdev
))
1087 printk("VFS: busy inodes on changed media.\n");
1089 if (bdops
->revalidate_disk
)
1090 bdops
->revalidate_disk(bdev
->bd_disk
);
1091 if (bdev
->bd_disk
->minors
> 1)
1092 bdev
->bd_invalidated
= 1;
1096 EXPORT_SYMBOL(check_disk_change
);
1098 void bd_set_size(struct block_device
*bdev
, loff_t size
)
1100 unsigned bsize
= bdev_hardsect_size(bdev
);
1102 bdev
->bd_inode
->i_size
= size
;
1103 while (bsize
< PAGE_CACHE_SIZE
) {
1108 bdev
->bd_block_size
= bsize
;
1109 bdev
->bd_inode
->i_blkbits
= blksize_bits(bsize
);
1111 EXPORT_SYMBOL(bd_set_size
);
1113 static int __blkdev_get(struct block_device
*bdev
, mode_t mode
, unsigned flags
,
1115 static int __blkdev_put(struct block_device
*bdev
, int for_part
);
1120 * mutex_lock(part->bd_mutex)
1121 * mutex_lock_nested(whole->bd_mutex, 1)
1124 static int do_open(struct block_device
*bdev
, struct file
*file
, int for_part
)
1126 struct module
*owner
= NULL
;
1127 struct gendisk
*disk
;
1131 file
->f_mapping
= bdev
->bd_inode
->i_mapping
;
1133 disk
= get_gendisk(bdev
->bd_dev
, &part
);
1139 owner
= disk
->fops
->owner
;
1141 mutex_lock_nested(&bdev
->bd_mutex
, for_part
);
1142 if (!bdev
->bd_openers
) {
1143 bdev
->bd_disk
= disk
;
1144 bdev
->bd_contains
= bdev
;
1146 struct backing_dev_info
*bdi
;
1147 if (disk
->fops
->open
) {
1148 ret
= disk
->fops
->open(bdev
->bd_inode
, file
);
1152 if (!bdev
->bd_openers
) {
1153 bd_set_size(bdev
,(loff_t
)get_capacity(disk
)<<9);
1154 bdi
= blk_get_backing_dev_info(bdev
);
1156 bdi
= &default_backing_dev_info
;
1157 bdev
->bd_inode
->i_data
.backing_dev_info
= bdi
;
1159 if (bdev
->bd_invalidated
)
1160 rescan_partitions(disk
, bdev
);
1162 struct hd_struct
*p
;
1163 struct block_device
*whole
;
1164 whole
= bdget_disk(disk
, 0);
1169 ret
= __blkdev_get(whole
, file
->f_mode
, file
->f_flags
, 1);
1172 bdev
->bd_contains
= whole
;
1173 p
= disk
->part
[part
- 1];
1174 bdev
->bd_inode
->i_data
.backing_dev_info
=
1175 whole
->bd_inode
->i_data
.backing_dev_info
;
1176 if (!(disk
->flags
& GENHD_FL_UP
) || !p
|| !p
->nr_sects
) {
1180 kobject_get(&p
->dev
.kobj
);
1182 bd_set_size(bdev
, (loff_t
) p
->nr_sects
<< 9);
1187 if (bdev
->bd_contains
== bdev
) {
1188 if (bdev
->bd_disk
->fops
->open
) {
1189 ret
= bdev
->bd_disk
->fops
->open(bdev
->bd_inode
, file
);
1193 if (bdev
->bd_invalidated
)
1194 rescan_partitions(bdev
->bd_disk
, bdev
);
1199 bdev
->bd_part_count
++;
1200 mutex_unlock(&bdev
->bd_mutex
);
1205 bdev
->bd_disk
= NULL
;
1206 bdev
->bd_inode
->i_data
.backing_dev_info
= &default_backing_dev_info
;
1207 if (bdev
!= bdev
->bd_contains
)
1208 __blkdev_put(bdev
->bd_contains
, 1);
1209 bdev
->bd_contains
= NULL
;
1213 mutex_unlock(&bdev
->bd_mutex
);
1220 static int __blkdev_get(struct block_device
*bdev
, mode_t mode
, unsigned flags
,
1224 * This crockload is due to bad choice of ->open() type.
1226 * For now, block device ->open() routine must _not_
1227 * examine anything in 'inode' argument except ->i_rdev.
1229 struct file fake_file
= {};
1230 struct dentry fake_dentry
= {};
1231 fake_file
.f_mode
= mode
;
1232 fake_file
.f_flags
= flags
;
1233 fake_file
.f_path
.dentry
= &fake_dentry
;
1234 fake_dentry
.d_inode
= bdev
->bd_inode
;
1236 return do_open(bdev
, &fake_file
, for_part
);
1239 int blkdev_get(struct block_device
*bdev
, mode_t mode
, unsigned flags
)
1241 return __blkdev_get(bdev
, mode
, flags
, 0);
1243 EXPORT_SYMBOL(blkdev_get
);
1245 static int blkdev_open(struct inode
* inode
, struct file
* filp
)
1247 struct block_device
*bdev
;
1251 * Preserve backwards compatibility and allow large file access
1252 * even if userspace doesn't ask for it explicitly. Some mkfs
1253 * binary needs it. We might want to drop this workaround
1254 * during an unstable branch.
1256 filp
->f_flags
|= O_LARGEFILE
;
1258 bdev
= bd_acquire(inode
);
1262 res
= do_open(bdev
, filp
, 0);
1266 if (!(filp
->f_flags
& O_EXCL
) )
1269 if (!(res
= bd_claim(bdev
, filp
)))
1276 static int __blkdev_put(struct block_device
*bdev
, int for_part
)
1279 struct inode
*bd_inode
= bdev
->bd_inode
;
1280 struct gendisk
*disk
= bdev
->bd_disk
;
1281 struct block_device
*victim
= NULL
;
1283 mutex_lock_nested(&bdev
->bd_mutex
, for_part
);
1286 bdev
->bd_part_count
--;
1288 if (!--bdev
->bd_openers
) {
1289 sync_blockdev(bdev
);
1292 if (bdev
->bd_contains
== bdev
) {
1293 if (disk
->fops
->release
)
1294 ret
= disk
->fops
->release(bd_inode
, NULL
);
1296 if (!bdev
->bd_openers
) {
1297 struct module
*owner
= disk
->fops
->owner
;
1302 if (bdev
->bd_contains
!= bdev
) {
1303 kobject_put(&bdev
->bd_part
->dev
.kobj
);
1304 bdev
->bd_part
= NULL
;
1306 bdev
->bd_disk
= NULL
;
1307 bdev
->bd_inode
->i_data
.backing_dev_info
= &default_backing_dev_info
;
1308 if (bdev
!= bdev
->bd_contains
)
1309 victim
= bdev
->bd_contains
;
1310 bdev
->bd_contains
= NULL
;
1313 mutex_unlock(&bdev
->bd_mutex
);
1316 __blkdev_put(victim
, 1);
1320 int blkdev_put(struct block_device
*bdev
)
1322 return __blkdev_put(bdev
, 0);
1324 EXPORT_SYMBOL(blkdev_put
);
1326 static int blkdev_close(struct inode
* inode
, struct file
* filp
)
1328 struct block_device
*bdev
= I_BDEV(filp
->f_mapping
->host
);
1329 if (bdev
->bd_holder
== filp
)
1331 return blkdev_put(bdev
);
1334 static long block_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
1336 return blkdev_ioctl(file
->f_mapping
->host
, file
, cmd
, arg
);
1339 static const struct address_space_operations def_blk_aops
= {
1340 .readpage
= blkdev_readpage
,
1341 .writepage
= blkdev_writepage
,
1342 .sync_page
= block_sync_page
,
1343 .write_begin
= blkdev_write_begin
,
1344 .write_end
= blkdev_write_end
,
1345 .writepages
= generic_writepages
,
1346 .direct_IO
= blkdev_direct_IO
,
1349 const struct file_operations def_blk_fops
= {
1350 .open
= blkdev_open
,
1351 .release
= blkdev_close
,
1352 .llseek
= block_llseek
,
1353 .read
= do_sync_read
,
1354 .write
= do_sync_write
,
1355 .aio_read
= generic_file_aio_read
,
1356 .aio_write
= generic_file_aio_write_nolock
,
1357 .mmap
= generic_file_mmap
,
1358 .fsync
= block_fsync
,
1359 .unlocked_ioctl
= block_ioctl
,
1360 #ifdef CONFIG_COMPAT
1361 .compat_ioctl
= compat_blkdev_ioctl
,
1363 .splice_read
= generic_file_splice_read
,
1364 .splice_write
= generic_file_splice_write
,
1367 int ioctl_by_bdev(struct block_device
*bdev
, unsigned cmd
, unsigned long arg
)
1370 mm_segment_t old_fs
= get_fs();
1372 res
= blkdev_ioctl(bdev
->bd_inode
, NULL
, cmd
, arg
);
1377 EXPORT_SYMBOL(ioctl_by_bdev
);
1380 * lookup_bdev - lookup a struct block_device by name
1382 * @path: special file representing the block device
1384 * Get a reference to the blockdevice at @path in the current
1385 * namespace if possible and return it. Return ERR_PTR(error)
1388 struct block_device
*lookup_bdev(const char *path
)
1390 struct block_device
*bdev
;
1391 struct inode
*inode
;
1392 struct nameidata nd
;
1395 if (!path
|| !*path
)
1396 return ERR_PTR(-EINVAL
);
1398 error
= path_lookup(path
, LOOKUP_FOLLOW
, &nd
);
1400 return ERR_PTR(error
);
1402 inode
= nd
.path
.dentry
->d_inode
;
1404 if (!S_ISBLK(inode
->i_mode
))
1407 if (nd
.path
.mnt
->mnt_flags
& MNT_NODEV
)
1410 bdev
= bd_acquire(inode
);
1417 bdev
= ERR_PTR(error
);
1422 * open_bdev_excl - open a block device by name and set it up for use
1424 * @path: special file representing the block device
1425 * @flags: %MS_RDONLY for opening read-only
1426 * @holder: owner for exclusion
1428 * Open the blockdevice described by the special file at @path, claim it
1431 struct block_device
*open_bdev_excl(const char *path
, int flags
, void *holder
)
1433 struct block_device
*bdev
;
1434 mode_t mode
= FMODE_READ
;
1437 bdev
= lookup_bdev(path
);
1441 if (!(flags
& MS_RDONLY
))
1442 mode
|= FMODE_WRITE
;
1443 error
= blkdev_get(bdev
, mode
, 0);
1445 return ERR_PTR(error
);
1447 if (!(flags
& MS_RDONLY
) && bdev_read_only(bdev
))
1449 error
= bd_claim(bdev
, holder
);
1457 return ERR_PTR(error
);
1460 EXPORT_SYMBOL(open_bdev_excl
);
1463 * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
1465 * @bdev: blockdevice to close
1467 * This is the counterpart to open_bdev_excl().
1469 void close_bdev_excl(struct block_device
*bdev
)
1475 EXPORT_SYMBOL(close_bdev_excl
);
1477 int __invalidate_device(struct block_device
*bdev
)
1479 struct super_block
*sb
= get_super(bdev
);
1484 * no need to lock the super, get_super holds the
1485 * read mutex so the filesystem cannot go away
1486 * under us (->put_super runs with the write lock
1489 shrink_dcache_sb(sb
);
1490 res
= invalidate_inodes(sb
);
1493 invalidate_bdev(bdev
);
1496 EXPORT_SYMBOL(__invalidate_device
);