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 <<<<<<< HEAD
:fs
/block_dev
.c
36 static const struct address_space_operations def_blk_aops
;
38 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/block_dev
.c
39 static inline struct bdev_inode
*BDEV_I(struct inode
*inode
)
41 return container_of(inode
, struct bdev_inode
, vfs_inode
);
44 inline struct block_device
*I_BDEV(struct inode
*inode
)
46 return &BDEV_I(inode
)->bdev
;
49 EXPORT_SYMBOL(I_BDEV
);
51 static sector_t
max_block(struct block_device
*bdev
)
53 sector_t retval
= ~((sector_t
)0);
54 loff_t sz
= i_size_read(bdev
->bd_inode
);
57 unsigned int size
= block_size(bdev
);
58 unsigned int sizebits
= blksize_bits(size
);
59 retval
= (sz
>> sizebits
);
64 /* Kill _all_ buffers and pagecache , dirty or not.. */
65 static void kill_bdev(struct block_device
*bdev
)
67 if (bdev
->bd_inode
->i_mapping
->nrpages
== 0)
70 truncate_inode_pages(bdev
->bd_inode
->i_mapping
, 0);
73 int set_blocksize(struct block_device
*bdev
, int size
)
75 /* Size must be a power of two, and between 512 and PAGE_SIZE */
76 if (size
> PAGE_SIZE
|| size
< 512 || !is_power_of_2(size
))
79 /* Size cannot be smaller than the size supported by the device */
80 if (size
< bdev_hardsect_size(bdev
))
83 /* Don't change the size if it is same as current */
84 if (bdev
->bd_block_size
!= size
) {
86 bdev
->bd_block_size
= size
;
87 bdev
->bd_inode
->i_blkbits
= blksize_bits(size
);
93 EXPORT_SYMBOL(set_blocksize
);
95 int sb_set_blocksize(struct super_block
*sb
, int size
)
97 if (set_blocksize(sb
->s_bdev
, size
))
99 /* If we get here, we know size is power of two
100 * and it's value is between 512 and PAGE_SIZE */
101 sb
->s_blocksize
= size
;
102 sb
->s_blocksize_bits
= blksize_bits(size
);
103 return sb
->s_blocksize
;
106 EXPORT_SYMBOL(sb_set_blocksize
);
108 int sb_min_blocksize(struct super_block
*sb
, int size
)
110 int minsize
= bdev_hardsect_size(sb
->s_bdev
);
113 return sb_set_blocksize(sb
, size
);
116 EXPORT_SYMBOL(sb_min_blocksize
);
119 blkdev_get_block(struct inode
*inode
, sector_t iblock
,
120 struct buffer_head
*bh
, int create
)
122 if (iblock
>= max_block(I_BDEV(inode
))) {
127 * for reads, we're just trying to fill a partial page.
128 * return a hole, they will have to call get_block again
129 * before they can fill it, and they will get -EIO at that
134 bh
->b_bdev
= I_BDEV(inode
);
135 bh
->b_blocknr
= iblock
;
136 set_buffer_mapped(bh
);
141 blkdev_get_blocks(struct inode
*inode
, sector_t iblock
,
142 struct buffer_head
*bh
, int create
)
144 sector_t end_block
= max_block(I_BDEV(inode
));
145 unsigned long max_blocks
= bh
->b_size
>> inode
->i_blkbits
;
147 if ((iblock
+ max_blocks
) > end_block
) {
148 max_blocks
= end_block
- iblock
;
149 if ((long)max_blocks
<= 0) {
151 return -EIO
; /* write fully beyond EOF */
153 * It is a read which is fully beyond EOF. We return
154 * a !buffer_mapped buffer
160 bh
->b_bdev
= I_BDEV(inode
);
161 bh
->b_blocknr
= iblock
;
162 bh
->b_size
= max_blocks
<< inode
->i_blkbits
;
164 set_buffer_mapped(bh
);
169 blkdev_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
170 loff_t offset
, unsigned long nr_segs
)
172 struct file
*file
= iocb
->ki_filp
;
173 struct inode
*inode
= file
->f_mapping
->host
;
175 return blockdev_direct_IO_no_locking(rw
, iocb
, inode
, I_BDEV(inode
),
176 iov
, offset
, nr_segs
, blkdev_get_blocks
, NULL
);
179 <<<<<<< HEAD
:fs
/block_dev
.c
181 static void blk_end_aio(struct bio
*bio
, int error
)
183 struct kiocb
*iocb
= bio
->bi_private
;
184 atomic_t
*bio_count
= &iocb
->ki_bio_count
;
186 if (bio_data_dir(bio
) == READ
)
187 bio_check_pages_dirty(bio
);
189 bio_release_pages(bio
);
193 /* iocb->ki_nbytes stores error code from LLDD */
195 iocb
->ki_nbytes
= -EIO
;
197 if (atomic_dec_and_test(bio_count
)) {
198 if ((long)iocb
->ki_nbytes
< 0)
199 aio_complete(iocb
, iocb
->ki_nbytes
, 0);
201 aio_complete(iocb
, iocb
->ki_left
, 0);
211 struct page
*page
[VEC_SIZE
];
214 #define PAGES_SPANNED(addr, len) \
215 (DIV_ROUND_UP((addr) + (len), PAGE_SIZE) - (addr) / PAGE_SIZE);
218 * get page pointer for user addr, we internally cache struct page array for
219 * (addr, count) range in pvec to avoid frequent call to get_user_pages. If
220 * internal page list is exhausted, a batch count of up to VEC_SIZE is used
221 * to get next set of page struct.
223 static struct page
*blk_get_page(unsigned long addr
, size_t count
, int rw
,
227 if (pvec
->idx
== pvec
->nr
) {
228 nr_pages
= PAGES_SPANNED(addr
, count
);
229 nr_pages
= min(nr_pages
, VEC_SIZE
);
230 down_read(¤t
->mm
->mmap_sem
);
231 ret
= get_user_pages(current
, current
->mm
, addr
, nr_pages
,
232 rw
== READ
, 0, pvec
->page
, NULL
);
233 up_read(¤t
->mm
->mmap_sem
);
239 return pvec
->page
[pvec
->idx
++];
242 /* return a page back to pvec array */
243 static void blk_unget_page(struct page
*page
, struct pvec
*pvec
)
245 pvec
->page
[--pvec
->idx
] = page
;
249 blkdev_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
250 loff_t pos
, unsigned long nr_segs
)
252 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
253 unsigned blkbits
= blksize_bits(bdev_hardsect_size(I_BDEV(inode
)));
254 unsigned blocksize_mask
= (1 << blkbits
) - 1;
255 unsigned long seg
= 0; /* iov segment iterator */
256 unsigned long nvec
; /* number of bio vec needed */
257 unsigned long cur_off
; /* offset into current page */
258 unsigned long cur_len
; /* I/O len of current page, up to PAGE_SIZE */
260 unsigned long addr
; /* user iovec address */
261 size_t count
; /* user iovec len */
262 size_t nbytes
= iocb
->ki_nbytes
= iocb
->ki_left
; /* total xfer size */
263 loff_t size
; /* size of block device */
265 atomic_t
*bio_count
= &iocb
->ki_bio_count
;
272 if (pos
& blocksize_mask
)
275 size
= i_size_read(inode
);
276 if (pos
+ nbytes
> size
) {
278 iocb
->ki_left
= nbytes
;
282 * check first non-zero iov alignment, the remaining
283 * iov alignment is checked inside bio loop below.
286 addr
= (unsigned long) iov
[seg
].iov_base
;
287 count
= min(iov
[seg
].iov_len
, nbytes
);
288 if (addr
& blocksize_mask
|| count
& blocksize_mask
)
290 } while (!count
&& ++seg
< nr_segs
);
291 atomic_set(bio_count
, 1);
294 /* roughly estimate number of bio vec needed */
295 nvec
= (nbytes
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
296 nvec
= max(nvec
, nr_segs
- seg
);
297 nvec
= min(nvec
, (unsigned long) BIO_MAX_PAGES
);
299 /* bio_alloc should not fail with GFP_KERNEL flag */
300 bio
= bio_alloc(GFP_KERNEL
, nvec
);
301 bio
->bi_bdev
= I_BDEV(inode
);
302 bio
->bi_end_io
= blk_end_aio
;
303 bio
->bi_private
= iocb
;
304 bio
->bi_sector
= pos
>> blkbits
;
306 cur_off
= addr
& ~PAGE_MASK
;
307 cur_len
= PAGE_SIZE
- cur_off
;
311 page
= blk_get_page(addr
, count
, rw
, &pvec
);
312 if (unlikely(IS_ERR(page
)))
315 if (bio_add_page(bio
, page
, cur_len
, cur_off
)) {
323 while (++seg
< nr_segs
) {
324 addr
= (unsigned long) iov
[seg
].iov_base
;
325 count
= iov
[seg
].iov_len
;
328 if (unlikely(addr
& blocksize_mask
||
329 count
& blocksize_mask
)) {
330 page
= ERR_PTR(-EINVAL
);
333 count
= min(count
, nbytes
);
337 blk_unget_page(page
, &pvec
);
340 /* bio is ready, submit it */
342 bio_set_pages_dirty(bio
);
343 atomic_inc(bio_count
);
348 iocb
->ki_left
-= nbytes
;
349 nbytes
= iocb
->ki_left
;
350 iocb
->ki_pos
+= nbytes
;
352 blk_run_address_space(inode
->i_mapping
);
353 if (atomic_dec_and_test(bio_count
))
354 aio_complete(iocb
, nbytes
, 0);
360 * back out nbytes count constructed so far for this bio,
361 * we will throw away current bio.
363 nbytes
+= bio
->bi_size
;
364 bio_release_pages(bio
);
368 * if no bio was submmitted, return the error code.
369 * otherwise, proceed with pending I/O completion.
371 if (atomic_read(bio_count
) == 1)
372 return PTR_ERR(page
);
378 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/block_dev
.c
379 static int blkdev_writepage(struct page
*page
, struct writeback_control
*wbc
)
381 return block_write_full_page(page
, blkdev_get_block
, wbc
);
384 static int blkdev_readpage(struct file
* file
, struct page
* page
)
386 return block_read_full_page(page
, blkdev_get_block
);
389 static int blkdev_write_begin(struct file
*file
, struct address_space
*mapping
,
390 loff_t pos
, unsigned len
, unsigned flags
,
391 struct page
**pagep
, void **fsdata
)
394 return block_write_begin(file
, mapping
, pos
, len
, flags
, pagep
, fsdata
,
398 static int blkdev_write_end(struct file
*file
, struct address_space
*mapping
,
399 loff_t pos
, unsigned len
, unsigned copied
,
400 struct page
*page
, void *fsdata
)
403 ret
= block_write_end(file
, mapping
, pos
, len
, copied
, page
, fsdata
);
406 page_cache_release(page
);
413 * for a block special file file->f_path.dentry->d_inode->i_size is zero
414 * so we compute the size by hand (just as in block_read/write above)
416 static loff_t
block_llseek(struct file
*file
, loff_t offset
, int origin
)
418 struct inode
*bd_inode
= file
->f_mapping
->host
;
422 mutex_lock(&bd_inode
->i_mutex
);
423 size
= i_size_read(bd_inode
);
430 offset
+= file
->f_pos
;
433 if (offset
>= 0 && offset
<= size
) {
434 if (offset
!= file
->f_pos
) {
435 file
->f_pos
= offset
;
439 mutex_unlock(&bd_inode
->i_mutex
);
444 * Filp is never NULL; the only case when ->fsync() is called with
445 * NULL first argument is nfsd_sync_dir() and that's not a directory.
448 static int block_fsync(struct file
*filp
, struct dentry
*dentry
, int datasync
)
450 return sync_blockdev(I_BDEV(filp
->f_mapping
->host
));
457 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(bdev_lock
);
458 static struct kmem_cache
* bdev_cachep __read_mostly
;
460 static struct inode
*bdev_alloc_inode(struct super_block
*sb
)
462 struct bdev_inode
*ei
= kmem_cache_alloc(bdev_cachep
, GFP_KERNEL
);
465 return &ei
->vfs_inode
;
468 static void bdev_destroy_inode(struct inode
*inode
)
470 struct bdev_inode
*bdi
= BDEV_I(inode
);
472 bdi
->bdev
.bd_inode_backing_dev_info
= NULL
;
473 kmem_cache_free(bdev_cachep
, bdi
);
476 static void init_once(struct kmem_cache
* cachep
, void *foo
)
478 struct bdev_inode
*ei
= (struct bdev_inode
*) foo
;
479 struct block_device
*bdev
= &ei
->bdev
;
481 memset(bdev
, 0, sizeof(*bdev
));
482 mutex_init(&bdev
->bd_mutex
);
483 sema_init(&bdev
->bd_mount_sem
, 1);
484 INIT_LIST_HEAD(&bdev
->bd_inodes
);
485 INIT_LIST_HEAD(&bdev
->bd_list
);
487 INIT_LIST_HEAD(&bdev
->bd_holder_list
);
489 inode_init_once(&ei
->vfs_inode
);
492 static inline void __bd_forget(struct inode
*inode
)
494 list_del_init(&inode
->i_devices
);
495 inode
->i_bdev
= NULL
;
496 inode
->i_mapping
= &inode
->i_data
;
499 static void bdev_clear_inode(struct inode
*inode
)
501 struct block_device
*bdev
= &BDEV_I(inode
)->bdev
;
503 spin_lock(&bdev_lock
);
504 while ( (p
= bdev
->bd_inodes
.next
) != &bdev
->bd_inodes
) {
505 __bd_forget(list_entry(p
, struct inode
, i_devices
));
507 list_del_init(&bdev
->bd_list
);
508 spin_unlock(&bdev_lock
);
511 static const struct super_operations bdev_sops
= {
512 .statfs
= simple_statfs
,
513 .alloc_inode
= bdev_alloc_inode
,
514 .destroy_inode
= bdev_destroy_inode
,
515 .drop_inode
= generic_delete_inode
,
516 .clear_inode
= bdev_clear_inode
,
519 static int bd_get_sb(struct file_system_type
*fs_type
,
520 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
522 return get_sb_pseudo(fs_type
, "bdev:", &bdev_sops
, 0x62646576, mnt
);
525 static struct file_system_type bd_type
= {
528 .kill_sb
= kill_anon_super
,
531 static struct vfsmount
*bd_mnt __read_mostly
;
532 struct super_block
*blockdev_superblock
;
534 void __init
bdev_cache_init(void)
537 bdev_cachep
= kmem_cache_create("bdev_cache", sizeof(struct bdev_inode
),
538 0, (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
539 SLAB_MEM_SPREAD
|SLAB_PANIC
),
541 err
= register_filesystem(&bd_type
);
543 panic("Cannot register bdev pseudo-fs");
544 bd_mnt
= kern_mount(&bd_type
);
546 panic("Cannot create bdev pseudo-fs");
547 blockdev_superblock
= bd_mnt
->mnt_sb
; /* For writeback */
551 * Most likely _very_ bad one - but then it's hardly critical for small
552 * /dev and can be fixed when somebody will need really large one.
553 * Keep in mind that it will be fed through icache hash function too.
555 static inline unsigned long hash(dev_t dev
)
557 return MAJOR(dev
)+MINOR(dev
);
560 static int bdev_test(struct inode
*inode
, void *data
)
562 return BDEV_I(inode
)->bdev
.bd_dev
== *(dev_t
*)data
;
565 static int bdev_set(struct inode
*inode
, void *data
)
567 BDEV_I(inode
)->bdev
.bd_dev
= *(dev_t
*)data
;
571 static LIST_HEAD(all_bdevs
);
573 struct block_device
*bdget(dev_t dev
)
575 struct block_device
*bdev
;
578 inode
= iget5_locked(bd_mnt
->mnt_sb
, hash(dev
),
579 bdev_test
, bdev_set
, &dev
);
584 bdev
= &BDEV_I(inode
)->bdev
;
586 if (inode
->i_state
& I_NEW
) {
587 bdev
->bd_contains
= NULL
;
588 bdev
->bd_inode
= inode
;
589 bdev
->bd_block_size
= (1 << inode
->i_blkbits
);
590 bdev
->bd_part_count
= 0;
591 bdev
->bd_invalidated
= 0;
592 inode
->i_mode
= S_IFBLK
;
594 inode
->i_bdev
= bdev
;
595 inode
->i_data
.a_ops
= &def_blk_aops
;
596 mapping_set_gfp_mask(&inode
->i_data
, GFP_USER
);
597 inode
->i_data
.backing_dev_info
= &default_backing_dev_info
;
598 spin_lock(&bdev_lock
);
599 list_add(&bdev
->bd_list
, &all_bdevs
);
600 spin_unlock(&bdev_lock
);
601 unlock_new_inode(inode
);
606 EXPORT_SYMBOL(bdget
);
608 long nr_blockdev_pages(void)
610 struct block_device
*bdev
;
612 spin_lock(&bdev_lock
);
613 list_for_each_entry(bdev
, &all_bdevs
, bd_list
) {
614 ret
+= bdev
->bd_inode
->i_mapping
->nrpages
;
616 spin_unlock(&bdev_lock
);
620 void bdput(struct block_device
*bdev
)
622 iput(bdev
->bd_inode
);
625 EXPORT_SYMBOL(bdput
);
627 static struct block_device
*bd_acquire(struct inode
*inode
)
629 struct block_device
*bdev
;
631 spin_lock(&bdev_lock
);
632 bdev
= inode
->i_bdev
;
634 atomic_inc(&bdev
->bd_inode
->i_count
);
635 spin_unlock(&bdev_lock
);
638 spin_unlock(&bdev_lock
);
640 bdev
= bdget(inode
->i_rdev
);
642 spin_lock(&bdev_lock
);
643 if (!inode
->i_bdev
) {
645 * We take an additional bd_inode->i_count for inode,
646 * and it's released in clear_inode() of inode.
647 * So, we can access it via ->i_mapping always
650 atomic_inc(&bdev
->bd_inode
->i_count
);
651 inode
->i_bdev
= bdev
;
652 inode
->i_mapping
= bdev
->bd_inode
->i_mapping
;
653 list_add(&inode
->i_devices
, &bdev
->bd_inodes
);
655 spin_unlock(&bdev_lock
);
660 /* Call when you free inode */
662 void bd_forget(struct inode
*inode
)
664 struct block_device
*bdev
= NULL
;
666 spin_lock(&bdev_lock
);
668 if (inode
->i_sb
!= blockdev_superblock
)
669 bdev
= inode
->i_bdev
;
672 spin_unlock(&bdev_lock
);
675 iput(bdev
->bd_inode
);
678 int bd_claim(struct block_device
*bdev
, void *holder
)
681 spin_lock(&bdev_lock
);
683 /* first decide result */
684 if (bdev
->bd_holder
== holder
)
685 res
= 0; /* already a holder */
686 else if (bdev
->bd_holder
!= NULL
)
687 res
= -EBUSY
; /* held by someone else */
688 else if (bdev
->bd_contains
== bdev
)
689 res
= 0; /* is a whole device which isn't held */
691 else if (bdev
->bd_contains
->bd_holder
== bd_claim
)
692 res
= 0; /* is a partition of a device that is being partitioned */
693 else if (bdev
->bd_contains
->bd_holder
!= NULL
)
694 res
= -EBUSY
; /* is a partition of a held device */
696 res
= 0; /* is a partition of an un-held device */
698 /* now impose change */
700 /* note that for a whole device bd_holders
701 * will be incremented twice, and bd_holder will
702 * be set to bd_claim before being set to holder
704 bdev
->bd_contains
->bd_holders
++;
705 bdev
->bd_contains
->bd_holder
= bd_claim
;
707 bdev
->bd_holder
= holder
;
709 spin_unlock(&bdev_lock
);
713 EXPORT_SYMBOL(bd_claim
);
715 void bd_release(struct block_device
*bdev
)
717 spin_lock(&bdev_lock
);
718 if (!--bdev
->bd_contains
->bd_holders
)
719 bdev
->bd_contains
->bd_holder
= NULL
;
720 if (!--bdev
->bd_holders
)
721 bdev
->bd_holder
= NULL
;
722 spin_unlock(&bdev_lock
);
725 EXPORT_SYMBOL(bd_release
);
729 * Functions for bd_claim_by_kobject / bd_release_from_kobject
731 * If a kobject is passed to bd_claim_by_kobject()
732 * and the kobject has a parent directory,
733 * following symlinks are created:
734 * o from the kobject to the claimed bdev
735 * o from "holders" directory of the bdev to the parent of the kobject
736 * bd_release_from_kobject() removes these symlinks.
739 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
740 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
741 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
742 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
745 static struct kobject
*bdev_get_kobj(struct block_device
*bdev
)
747 if (bdev
->bd_contains
!= bdev
)
748 return kobject_get(&bdev
->bd_part
->dev
.kobj
);
750 return kobject_get(&bdev
->bd_disk
->dev
.kobj
);
753 static struct kobject
*bdev_get_holder(struct block_device
*bdev
)
755 if (bdev
->bd_contains
!= bdev
)
756 return kobject_get(bdev
->bd_part
->holder_dir
);
758 return kobject_get(bdev
->bd_disk
->holder_dir
);
761 static int add_symlink(struct kobject
*from
, struct kobject
*to
)
765 return sysfs_create_link(from
, to
, kobject_name(to
));
768 static void del_symlink(struct kobject
*from
, struct kobject
*to
)
772 sysfs_remove_link(from
, kobject_name(to
));
776 * 'struct bd_holder' contains pointers to kobjects symlinked by
777 * bd_claim_by_kobject.
778 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
781 struct list_head list
; /* chain of holders of the bdev */
782 int count
; /* references from the holder */
783 struct kobject
*sdir
; /* holder object, e.g. "/block/dm-0/slaves" */
784 struct kobject
*hdev
; /* e.g. "/block/dm-0" */
785 struct kobject
*hdir
; /* e.g. "/block/sda/holders" */
786 struct kobject
*sdev
; /* e.g. "/block/sda" */
790 * Get references of related kobjects at once.
791 * Returns 1 on success. 0 on failure.
793 * Should call bd_holder_release_dirs() after successful use.
795 static int bd_holder_grab_dirs(struct block_device
*bdev
,
796 struct bd_holder
*bo
)
801 bo
->sdir
= kobject_get(bo
->sdir
);
805 bo
->hdev
= kobject_get(bo
->sdir
->parent
);
809 bo
->sdev
= bdev_get_kobj(bdev
);
813 bo
->hdir
= bdev_get_holder(bdev
);
820 kobject_put(bo
->sdev
);
822 kobject_put(bo
->hdev
);
824 kobject_put(bo
->sdir
);
829 /* Put references of related kobjects at once. */
830 static void bd_holder_release_dirs(struct bd_holder
*bo
)
832 kobject_put(bo
->hdir
);
833 kobject_put(bo
->sdev
);
834 kobject_put(bo
->hdev
);
835 kobject_put(bo
->sdir
);
838 static struct bd_holder
*alloc_bd_holder(struct kobject
*kobj
)
840 struct bd_holder
*bo
;
842 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
852 static void free_bd_holder(struct bd_holder
*bo
)
858 * find_bd_holder - find matching struct bd_holder from the block device
860 * @bdev: struct block device to be searched
861 * @bo: target struct bd_holder
863 * Returns matching entry with @bo in @bdev->bd_holder_list.
864 * If found, increment the reference count and return the pointer.
865 * If not found, returns NULL.
867 static struct bd_holder
*find_bd_holder(struct block_device
*bdev
,
868 struct bd_holder
*bo
)
870 struct bd_holder
*tmp
;
872 list_for_each_entry(tmp
, &bdev
->bd_holder_list
, list
)
873 if (tmp
->sdir
== bo
->sdir
) {
882 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
884 * @bdev: block device to be bd_claimed
885 * @bo: preallocated and initialized by alloc_bd_holder()
887 * Add @bo to @bdev->bd_holder_list, create symlinks.
889 * Returns 0 if symlinks are created.
890 * Returns -ve if something fails.
892 static int add_bd_holder(struct block_device
*bdev
, struct bd_holder
*bo
)
899 if (!bd_holder_grab_dirs(bdev
, bo
))
902 err
= add_symlink(bo
->sdir
, bo
->sdev
);
906 err
= add_symlink(bo
->hdir
, bo
->hdev
);
908 del_symlink(bo
->sdir
, bo
->sdev
);
912 list_add_tail(&bo
->list
, &bdev
->bd_holder_list
);
917 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
919 * @bdev: block device to be bd_claimed
920 * @kobj: holder's kobject
922 * If there is matching entry with @kobj in @bdev->bd_holder_list
923 * and no other bd_claim() from the same kobject,
924 * remove the struct bd_holder from the list, delete symlinks for it.
926 * Returns a pointer to the struct bd_holder when it's removed from the list
927 * and ready to be freed.
928 * Returns NULL if matching claim isn't found or there is other bd_claim()
929 * by the same kobject.
931 static struct bd_holder
*del_bd_holder(struct block_device
*bdev
,
932 struct kobject
*kobj
)
934 struct bd_holder
*bo
;
936 list_for_each_entry(bo
, &bdev
->bd_holder_list
, list
) {
937 if (bo
->sdir
== kobj
) {
939 BUG_ON(bo
->count
< 0);
942 del_symlink(bo
->sdir
, bo
->sdev
);
943 del_symlink(bo
->hdir
, bo
->hdev
);
944 bd_holder_release_dirs(bo
);
955 * bd_claim_by_kobject - bd_claim() with additional kobject signature
957 * @bdev: block device to be claimed
958 * @holder: holder's signature
959 * @kobj: holder's kobject
961 * Do bd_claim() and if it succeeds, create sysfs symlinks between
962 * the bdev and the holder's kobject.
963 * Use bd_release_from_kobject() when relesing the claimed bdev.
965 * Returns 0 on success. (same as bd_claim())
966 * Returns errno on failure.
968 static int bd_claim_by_kobject(struct block_device
*bdev
, void *holder
,
969 struct kobject
*kobj
)
972 struct bd_holder
*bo
, *found
;
977 bo
= alloc_bd_holder(kobj
);
981 mutex_lock(&bdev
->bd_mutex
);
983 err
= bd_claim(bdev
, holder
);
987 found
= find_bd_holder(bdev
, bo
);
991 err
= add_bd_holder(bdev
, bo
);
997 mutex_unlock(&bdev
->bd_mutex
);
1003 * bd_release_from_kobject - bd_release() with additional kobject signature
1005 * @bdev: block device to be released
1006 * @kobj: holder's kobject
1008 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
1010 static void bd_release_from_kobject(struct block_device
*bdev
,
1011 struct kobject
*kobj
)
1016 mutex_lock(&bdev
->bd_mutex
);
1018 free_bd_holder(del_bd_holder(bdev
, kobj
));
1019 mutex_unlock(&bdev
->bd_mutex
);
1023 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
1025 * @bdev: block device to be claimed
1026 * @holder: holder's signature
1027 * @disk: holder's gendisk
1029 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
1031 int bd_claim_by_disk(struct block_device
*bdev
, void *holder
,
1032 struct gendisk
*disk
)
1034 return bd_claim_by_kobject(bdev
, holder
, kobject_get(disk
->slave_dir
));
1036 EXPORT_SYMBOL_GPL(bd_claim_by_disk
);
1039 * bd_release_from_disk - wrapper function for bd_release_from_kobject()
1041 * @bdev: block device to be claimed
1042 * @disk: holder's gendisk
1044 * Call bd_release_from_kobject() and put @disk->slave_dir.
1046 void bd_release_from_disk(struct block_device
*bdev
, struct gendisk
*disk
)
1048 bd_release_from_kobject(bdev
, disk
->slave_dir
);
1049 kobject_put(disk
->slave_dir
);
1051 EXPORT_SYMBOL_GPL(bd_release_from_disk
);
1055 * Tries to open block device by device number. Use it ONLY if you
1056 * really do not have anything better - i.e. when you are behind a
1057 * truly sucky interface and all you are given is a device number. _Never_
1058 * to be used for internal purposes. If you ever need it - reconsider
1061 struct block_device
*open_by_devnum(dev_t dev
, unsigned mode
)
1063 struct block_device
*bdev
= bdget(dev
);
1065 int flags
= mode
& FMODE_WRITE
? O_RDWR
: O_RDONLY
;
1067 err
= blkdev_get(bdev
, mode
, flags
);
1068 return err
? ERR_PTR(err
) : bdev
;
1071 EXPORT_SYMBOL(open_by_devnum
);
1074 * This routine checks whether a removable media has been changed,
1075 * and invalidates all buffer-cache-entries in that case. This
1076 * is a relatively slow routine, so we have to try to minimize using
1077 * it. Thus it is called only upon a 'mount' or 'open'. This
1078 * is the best way of combining speed and utility, I think.
1079 * People changing diskettes in the middle of an operation deserve
1082 int check_disk_change(struct block_device
*bdev
)
1084 struct gendisk
*disk
= bdev
->bd_disk
;
1085 struct block_device_operations
* bdops
= disk
->fops
;
1087 if (!bdops
->media_changed
)
1089 if (!bdops
->media_changed(bdev
->bd_disk
))
1092 if (__invalidate_device(bdev
))
1093 printk("VFS: busy inodes on changed media.\n");
1095 if (bdops
->revalidate_disk
)
1096 bdops
->revalidate_disk(bdev
->bd_disk
);
1097 if (bdev
->bd_disk
->minors
> 1)
1098 bdev
->bd_invalidated
= 1;
1102 EXPORT_SYMBOL(check_disk_change
);
1104 void bd_set_size(struct block_device
*bdev
, loff_t size
)
1106 unsigned bsize
= bdev_hardsect_size(bdev
);
1108 bdev
->bd_inode
->i_size
= size
;
1109 while (bsize
< PAGE_CACHE_SIZE
) {
1114 bdev
->bd_block_size
= bsize
;
1115 bdev
->bd_inode
->i_blkbits
= blksize_bits(bsize
);
1117 EXPORT_SYMBOL(bd_set_size
);
1119 static int __blkdev_get(struct block_device
*bdev
, mode_t mode
, unsigned flags
,
1121 static int __blkdev_put(struct block_device
*bdev
, int for_part
);
1126 * mutex_lock(part->bd_mutex)
1127 * mutex_lock_nested(whole->bd_mutex, 1)
1130 static int do_open(struct block_device
*bdev
, struct file
*file
, int for_part
)
1132 struct module
*owner
= NULL
;
1133 struct gendisk
*disk
;
1137 file
->f_mapping
= bdev
->bd_inode
->i_mapping
;
1139 disk
= get_gendisk(bdev
->bd_dev
, &part
);
1145 owner
= disk
->fops
->owner
;
1147 mutex_lock_nested(&bdev
->bd_mutex
, for_part
);
1148 if (!bdev
->bd_openers
) {
1149 bdev
->bd_disk
= disk
;
1150 bdev
->bd_contains
= bdev
;
1152 struct backing_dev_info
*bdi
;
1153 if (disk
->fops
->open
) {
1154 ret
= disk
->fops
->open(bdev
->bd_inode
, file
);
1158 if (!bdev
->bd_openers
) {
1159 bd_set_size(bdev
,(loff_t
)get_capacity(disk
)<<9);
1160 bdi
= blk_get_backing_dev_info(bdev
);
1162 bdi
= &default_backing_dev_info
;
1163 bdev
->bd_inode
->i_data
.backing_dev_info
= bdi
;
1165 if (bdev
->bd_invalidated
)
1166 rescan_partitions(disk
, bdev
);
1168 struct hd_struct
*p
;
1169 struct block_device
*whole
;
1170 whole
= bdget_disk(disk
, 0);
1175 ret
= __blkdev_get(whole
, file
->f_mode
, file
->f_flags
, 1);
1178 bdev
->bd_contains
= whole
;
1179 p
= disk
->part
[part
- 1];
1180 bdev
->bd_inode
->i_data
.backing_dev_info
=
1181 whole
->bd_inode
->i_data
.backing_dev_info
;
1182 if (!(disk
->flags
& GENHD_FL_UP
) || !p
|| !p
->nr_sects
) {
1186 kobject_get(&p
->dev
.kobj
);
1188 bd_set_size(bdev
, (loff_t
) p
->nr_sects
<< 9);
1193 if (bdev
->bd_contains
== bdev
) {
1194 if (bdev
->bd_disk
->fops
->open
) {
1195 ret
= bdev
->bd_disk
->fops
->open(bdev
->bd_inode
, file
);
1199 if (bdev
->bd_invalidated
)
1200 rescan_partitions(bdev
->bd_disk
, bdev
);
1205 bdev
->bd_part_count
++;
1206 mutex_unlock(&bdev
->bd_mutex
);
1211 bdev
->bd_disk
= NULL
;
1212 bdev
->bd_inode
->i_data
.backing_dev_info
= &default_backing_dev_info
;
1213 if (bdev
!= bdev
->bd_contains
)
1214 __blkdev_put(bdev
->bd_contains
, 1);
1215 bdev
->bd_contains
= NULL
;
1219 mutex_unlock(&bdev
->bd_mutex
);
1226 static int __blkdev_get(struct block_device
*bdev
, mode_t mode
, unsigned flags
,
1230 * This crockload is due to bad choice of ->open() type.
1232 * For now, block device ->open() routine must _not_
1233 * examine anything in 'inode' argument except ->i_rdev.
1235 struct file fake_file
= {};
1236 struct dentry fake_dentry
= {};
1237 fake_file
.f_mode
= mode
;
1238 fake_file
.f_flags
= flags
;
1239 fake_file
.f_path
.dentry
= &fake_dentry
;
1240 fake_dentry
.d_inode
= bdev
->bd_inode
;
1242 return do_open(bdev
, &fake_file
, for_part
);
1245 int blkdev_get(struct block_device
*bdev
, mode_t mode
, unsigned flags
)
1247 return __blkdev_get(bdev
, mode
, flags
, 0);
1249 EXPORT_SYMBOL(blkdev_get
);
1251 static int blkdev_open(struct inode
* inode
, struct file
* filp
)
1253 struct block_device
*bdev
;
1257 * Preserve backwards compatibility and allow large file access
1258 * even if userspace doesn't ask for it explicitly. Some mkfs
1259 * binary needs it. We might want to drop this workaround
1260 * during an unstable branch.
1262 filp
->f_flags
|= O_LARGEFILE
;
1264 bdev
= bd_acquire(inode
);
1268 res
= do_open(bdev
, filp
, 0);
1272 if (!(filp
->f_flags
& O_EXCL
) )
1275 if (!(res
= bd_claim(bdev
, filp
)))
1282 static int __blkdev_put(struct block_device
*bdev
, int for_part
)
1285 struct inode
*bd_inode
= bdev
->bd_inode
;
1286 struct gendisk
*disk
= bdev
->bd_disk
;
1287 struct block_device
*victim
= NULL
;
1289 mutex_lock_nested(&bdev
->bd_mutex
, for_part
);
1292 bdev
->bd_part_count
--;
1294 if (!--bdev
->bd_openers
) {
1295 sync_blockdev(bdev
);
1298 if (bdev
->bd_contains
== bdev
) {
1299 if (disk
->fops
->release
)
1300 ret
= disk
->fops
->release(bd_inode
, NULL
);
1302 if (!bdev
->bd_openers
) {
1303 struct module
*owner
= disk
->fops
->owner
;
1308 if (bdev
->bd_contains
!= bdev
) {
1309 kobject_put(&bdev
->bd_part
->dev
.kobj
);
1310 bdev
->bd_part
= NULL
;
1312 bdev
->bd_disk
= NULL
;
1313 bdev
->bd_inode
->i_data
.backing_dev_info
= &default_backing_dev_info
;
1314 if (bdev
!= bdev
->bd_contains
)
1315 victim
= bdev
->bd_contains
;
1316 bdev
->bd_contains
= NULL
;
1319 mutex_unlock(&bdev
->bd_mutex
);
1322 __blkdev_put(victim
, 1);
1326 int blkdev_put(struct block_device
*bdev
)
1328 return __blkdev_put(bdev
, 0);
1330 EXPORT_SYMBOL(blkdev_put
);
1332 static int blkdev_close(struct inode
* inode
, struct file
* filp
)
1334 struct block_device
*bdev
= I_BDEV(filp
->f_mapping
->host
);
1335 if (bdev
->bd_holder
== filp
)
1337 return blkdev_put(bdev
);
1340 static long block_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
1342 return blkdev_ioctl(file
->f_mapping
->host
, file
, cmd
, arg
);
1345 <<<<<<< HEAD
:fs
/block_dev
.c
1346 const struct address_space_operations def_blk_aops
= {
1348 static const struct address_space_operations def_blk_aops
= {
1349 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/block_dev
.c
1350 .readpage
= blkdev_readpage
,
1351 .writepage
= blkdev_writepage
,
1352 .sync_page
= block_sync_page
,
1353 .write_begin
= blkdev_write_begin
,
1354 .write_end
= blkdev_write_end
,
1355 .writepages
= generic_writepages
,
1356 .direct_IO
= blkdev_direct_IO
,
1359 const struct file_operations def_blk_fops
= {
1360 .open
= blkdev_open
,
1361 .release
= blkdev_close
,
1362 .llseek
= block_llseek
,
1363 .read
= do_sync_read
,
1364 .write
= do_sync_write
,
1365 .aio_read
= generic_file_aio_read
,
1366 .aio_write
= generic_file_aio_write_nolock
,
1367 .mmap
= generic_file_mmap
,
1368 .fsync
= block_fsync
,
1369 .unlocked_ioctl
= block_ioctl
,
1370 #ifdef CONFIG_COMPAT
1371 .compat_ioctl
= compat_blkdev_ioctl
,
1373 .splice_read
= generic_file_splice_read
,
1374 .splice_write
= generic_file_splice_write
,
1377 int ioctl_by_bdev(struct block_device
*bdev
, unsigned cmd
, unsigned long arg
)
1380 mm_segment_t old_fs
= get_fs();
1382 res
= blkdev_ioctl(bdev
->bd_inode
, NULL
, cmd
, arg
);
1387 EXPORT_SYMBOL(ioctl_by_bdev
);
1390 * lookup_bdev - lookup a struct block_device by name
1392 * @path: special file representing the block device
1394 * Get a reference to the blockdevice at @path in the current
1395 * namespace if possible and return it. Return ERR_PTR(error)
1398 struct block_device
*lookup_bdev(const char *path
)
1400 struct block_device
*bdev
;
1401 struct inode
*inode
;
1402 struct nameidata nd
;
1405 if (!path
|| !*path
)
1406 return ERR_PTR(-EINVAL
);
1408 error
= path_lookup(path
, LOOKUP_FOLLOW
, &nd
);
1410 return ERR_PTR(error
);
1412 inode
= nd
.path
.dentry
->d_inode
;
1414 if (!S_ISBLK(inode
->i_mode
))
1417 if (nd
.path
.mnt
->mnt_flags
& MNT_NODEV
)
1420 bdev
= bd_acquire(inode
);
1427 bdev
= ERR_PTR(error
);
1432 * open_bdev_excl - open a block device by name and set it up for use
1434 * @path: special file representing the block device
1435 * @flags: %MS_RDONLY for opening read-only
1436 * @holder: owner for exclusion
1438 * Open the blockdevice described by the special file at @path, claim it
1441 struct block_device
*open_bdev_excl(const char *path
, int flags
, void *holder
)
1443 struct block_device
*bdev
;
1444 mode_t mode
= FMODE_READ
;
1447 bdev
= lookup_bdev(path
);
1451 if (!(flags
& MS_RDONLY
))
1452 mode
|= FMODE_WRITE
;
1453 error
= blkdev_get(bdev
, mode
, 0);
1455 return ERR_PTR(error
);
1457 if (!(flags
& MS_RDONLY
) && bdev_read_only(bdev
))
1459 error
= bd_claim(bdev
, holder
);
1467 return ERR_PTR(error
);
1470 EXPORT_SYMBOL(open_bdev_excl
);
1473 * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
1475 * @bdev: blockdevice to close
1477 * This is the counterpart to open_bdev_excl().
1479 void close_bdev_excl(struct block_device
*bdev
)
1485 EXPORT_SYMBOL(close_bdev_excl
);
1487 int __invalidate_device(struct block_device
*bdev
)
1489 struct super_block
*sb
= get_super(bdev
);
1494 * no need to lock the super, get_super holds the
1495 * read mutex so the filesystem cannot go away
1496 * under us (->put_super runs with the write lock
1499 shrink_dcache_sb(sb
);
1500 res
= invalidate_inodes(sb
);
1503 invalidate_bdev(bdev
);
1506 EXPORT_SYMBOL(__invalidate_device
);