1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Copyright (C) 2016 - 2020 Christoph Hellwig
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/device_cgroup.h>
15 #include <linux/highmem.h>
16 #include <linux/blkdev.h>
17 #include <linux/backing-dev.h>
18 #include <linux/module.h>
19 #include <linux/blkpg.h>
20 #include <linux/magic.h>
21 #include <linux/buffer_head.h>
22 #include <linux/swap.h>
23 #include <linux/pagevec.h>
24 #include <linux/writeback.h>
25 #include <linux/mpage.h>
26 #include <linux/mount.h>
27 #include <linux/pseudo_fs.h>
28 #include <linux/uio.h>
29 #include <linux/namei.h>
30 #include <linux/log2.h>
31 #include <linux/cleancache.h>
32 #include <linux/task_io_accounting_ops.h>
33 #include <linux/falloc.h>
34 #include <linux/part_stat.h>
35 #include <linux/uaccess.h>
36 #include <linux/suspend.h>
40 struct block_device bdev
;
41 struct inode vfs_inode
;
44 static const struct address_space_operations def_blk_aops
;
46 static inline struct bdev_inode
*BDEV_I(struct inode
*inode
)
48 return container_of(inode
, struct bdev_inode
, vfs_inode
);
51 struct block_device
*I_BDEV(struct inode
*inode
)
53 return &BDEV_I(inode
)->bdev
;
55 EXPORT_SYMBOL(I_BDEV
);
57 static void bdev_write_inode(struct block_device
*bdev
)
59 struct inode
*inode
= bdev
->bd_inode
;
62 spin_lock(&inode
->i_lock
);
63 while (inode
->i_state
& I_DIRTY
) {
64 spin_unlock(&inode
->i_lock
);
65 ret
= write_inode_now(inode
, true);
67 char name
[BDEVNAME_SIZE
];
68 pr_warn_ratelimited("VFS: Dirty inode writeback failed "
69 "for block device %s (err=%d).\n",
70 bdevname(bdev
, name
), ret
);
72 spin_lock(&inode
->i_lock
);
74 spin_unlock(&inode
->i_lock
);
77 /* Kill _all_ buffers and pagecache , dirty or not.. */
78 static void kill_bdev(struct block_device
*bdev
)
80 struct address_space
*mapping
= bdev
->bd_inode
->i_mapping
;
82 if (mapping
->nrpages
== 0 && mapping
->nrexceptional
== 0)
86 truncate_inode_pages(mapping
, 0);
89 /* Invalidate clean unused buffers and pagecache. */
90 void invalidate_bdev(struct block_device
*bdev
)
92 struct address_space
*mapping
= bdev
->bd_inode
->i_mapping
;
94 if (mapping
->nrpages
) {
96 lru_add_drain_all(); /* make sure all lru add caches are flushed */
97 invalidate_mapping_pages(mapping
, 0, -1);
99 /* 99% of the time, we don't need to flush the cleancache on the bdev.
100 * But, for the strange corners, lets be cautious
102 cleancache_invalidate_inode(mapping
);
104 EXPORT_SYMBOL(invalidate_bdev
);
107 * Drop all buffers & page cache for given bdev range. This function bails
108 * with error if bdev has other exclusive owner (such as filesystem).
110 int truncate_bdev_range(struct block_device
*bdev
, fmode_t mode
,
111 loff_t lstart
, loff_t lend
)
114 * If we don't hold exclusive handle for the device, upgrade to it
115 * while we discard the buffer cache to avoid discarding buffers
116 * under live filesystem.
118 if (!(mode
& FMODE_EXCL
)) {
119 int err
= bd_prepare_to_claim(bdev
, truncate_bdev_range
);
124 truncate_inode_pages_range(bdev
->bd_inode
->i_mapping
, lstart
, lend
);
125 if (!(mode
& FMODE_EXCL
))
126 bd_abort_claiming(bdev
, truncate_bdev_range
);
129 EXPORT_SYMBOL(truncate_bdev_range
);
131 static void set_init_blocksize(struct block_device
*bdev
)
133 bdev
->bd_inode
->i_blkbits
= blksize_bits(bdev_logical_block_size(bdev
));
136 int set_blocksize(struct block_device
*bdev
, int size
)
138 /* Size must be a power of two, and between 512 and PAGE_SIZE */
139 if (size
> PAGE_SIZE
|| size
< 512 || !is_power_of_2(size
))
142 /* Size cannot be smaller than the size supported by the device */
143 if (size
< bdev_logical_block_size(bdev
))
146 /* Don't change the size if it is same as current */
147 if (bdev
->bd_inode
->i_blkbits
!= blksize_bits(size
)) {
149 bdev
->bd_inode
->i_blkbits
= blksize_bits(size
);
155 EXPORT_SYMBOL(set_blocksize
);
157 int sb_set_blocksize(struct super_block
*sb
, int size
)
159 if (set_blocksize(sb
->s_bdev
, size
))
161 /* If we get here, we know size is power of two
162 * and it's value is between 512 and PAGE_SIZE */
163 sb
->s_blocksize
= size
;
164 sb
->s_blocksize_bits
= blksize_bits(size
);
165 return sb
->s_blocksize
;
168 EXPORT_SYMBOL(sb_set_blocksize
);
170 int sb_min_blocksize(struct super_block
*sb
, int size
)
172 int minsize
= bdev_logical_block_size(sb
->s_bdev
);
175 return sb_set_blocksize(sb
, size
);
178 EXPORT_SYMBOL(sb_min_blocksize
);
181 blkdev_get_block(struct inode
*inode
, sector_t iblock
,
182 struct buffer_head
*bh
, int create
)
184 bh
->b_bdev
= I_BDEV(inode
);
185 bh
->b_blocknr
= iblock
;
186 set_buffer_mapped(bh
);
190 static struct inode
*bdev_file_inode(struct file
*file
)
192 return file
->f_mapping
->host
;
195 static unsigned int dio_bio_write_op(struct kiocb
*iocb
)
197 unsigned int op
= REQ_OP_WRITE
| REQ_SYNC
| REQ_IDLE
;
199 /* avoid the need for a I/O completion work item */
200 if (iocb
->ki_flags
& IOCB_DSYNC
)
205 #define DIO_INLINE_BIO_VECS 4
207 static void blkdev_bio_end_io_simple(struct bio
*bio
)
209 struct task_struct
*waiter
= bio
->bi_private
;
211 WRITE_ONCE(bio
->bi_private
, NULL
);
212 blk_wake_io_task(waiter
);
216 __blkdev_direct_IO_simple(struct kiocb
*iocb
, struct iov_iter
*iter
,
219 struct file
*file
= iocb
->ki_filp
;
220 struct block_device
*bdev
= I_BDEV(bdev_file_inode(file
));
221 struct bio_vec inline_vecs
[DIO_INLINE_BIO_VECS
], *vecs
;
222 loff_t pos
= iocb
->ki_pos
;
223 bool should_dirty
= false;
228 if ((pos
| iov_iter_alignment(iter
)) &
229 (bdev_logical_block_size(bdev
) - 1))
232 if (nr_pages
<= DIO_INLINE_BIO_VECS
)
235 vecs
= kmalloc_array(nr_pages
, sizeof(struct bio_vec
),
241 bio_init(&bio
, vecs
, nr_pages
);
242 bio_set_dev(&bio
, bdev
);
243 bio
.bi_iter
.bi_sector
= pos
>> 9;
244 bio
.bi_write_hint
= iocb
->ki_hint
;
245 bio
.bi_private
= current
;
246 bio
.bi_end_io
= blkdev_bio_end_io_simple
;
247 bio
.bi_ioprio
= iocb
->ki_ioprio
;
249 ret
= bio_iov_iter_get_pages(&bio
, iter
);
252 ret
= bio
.bi_iter
.bi_size
;
254 if (iov_iter_rw(iter
) == READ
) {
255 bio
.bi_opf
= REQ_OP_READ
;
256 if (iter_is_iovec(iter
))
259 bio
.bi_opf
= dio_bio_write_op(iocb
);
260 task_io_account_write(ret
);
262 if (iocb
->ki_flags
& IOCB_HIPRI
)
263 bio_set_polled(&bio
, iocb
);
265 qc
= submit_bio(&bio
);
267 set_current_state(TASK_UNINTERRUPTIBLE
);
268 if (!READ_ONCE(bio
.bi_private
))
270 if (!(iocb
->ki_flags
& IOCB_HIPRI
) ||
271 !blk_poll(bdev_get_queue(bdev
), qc
, true))
274 __set_current_state(TASK_RUNNING
);
276 bio_release_pages(&bio
, should_dirty
);
277 if (unlikely(bio
.bi_status
))
278 ret
= blk_status_to_errno(bio
.bi_status
);
281 if (vecs
!= inline_vecs
)
292 struct task_struct
*waiter
;
297 bool should_dirty
: 1;
302 static struct bio_set blkdev_dio_pool
;
304 static int blkdev_iopoll(struct kiocb
*kiocb
, bool wait
)
306 struct block_device
*bdev
= I_BDEV(kiocb
->ki_filp
->f_mapping
->host
);
307 struct request_queue
*q
= bdev_get_queue(bdev
);
309 return blk_poll(q
, READ_ONCE(kiocb
->ki_cookie
), wait
);
312 static void blkdev_bio_end_io(struct bio
*bio
)
314 struct blkdev_dio
*dio
= bio
->bi_private
;
315 bool should_dirty
= dio
->should_dirty
;
317 if (bio
->bi_status
&& !dio
->bio
.bi_status
)
318 dio
->bio
.bi_status
= bio
->bi_status
;
320 if (!dio
->multi_bio
|| atomic_dec_and_test(&dio
->ref
)) {
322 struct kiocb
*iocb
= dio
->iocb
;
325 if (likely(!dio
->bio
.bi_status
)) {
329 ret
= blk_status_to_errno(dio
->bio
.bi_status
);
332 dio
->iocb
->ki_complete(iocb
, ret
, 0);
336 struct task_struct
*waiter
= dio
->waiter
;
338 WRITE_ONCE(dio
->waiter
, NULL
);
339 blk_wake_io_task(waiter
);
344 bio_check_pages_dirty(bio
);
346 bio_release_pages(bio
, false);
352 __blkdev_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
, int nr_pages
)
354 struct file
*file
= iocb
->ki_filp
;
355 struct inode
*inode
= bdev_file_inode(file
);
356 struct block_device
*bdev
= I_BDEV(inode
);
357 struct blk_plug plug
;
358 struct blkdev_dio
*dio
;
360 bool is_poll
= (iocb
->ki_flags
& IOCB_HIPRI
) != 0;
361 bool is_read
= (iov_iter_rw(iter
) == READ
), is_sync
;
362 loff_t pos
= iocb
->ki_pos
;
363 blk_qc_t qc
= BLK_QC_T_NONE
;
366 if ((pos
| iov_iter_alignment(iter
)) &
367 (bdev_logical_block_size(bdev
) - 1))
370 bio
= bio_alloc_bioset(GFP_KERNEL
, nr_pages
, &blkdev_dio_pool
);
372 dio
= container_of(bio
, struct blkdev_dio
, bio
);
373 dio
->is_sync
= is_sync
= is_sync_kiocb(iocb
);
375 dio
->waiter
= current
;
382 dio
->multi_bio
= false;
383 dio
->should_dirty
= is_read
&& iter_is_iovec(iter
);
386 * Don't plug for HIPRI/polled IO, as those should go straight
390 blk_start_plug(&plug
);
393 bio_set_dev(bio
, bdev
);
394 bio
->bi_iter
.bi_sector
= pos
>> 9;
395 bio
->bi_write_hint
= iocb
->ki_hint
;
396 bio
->bi_private
= dio
;
397 bio
->bi_end_io
= blkdev_bio_end_io
;
398 bio
->bi_ioprio
= iocb
->ki_ioprio
;
400 ret
= bio_iov_iter_get_pages(bio
, iter
);
402 bio
->bi_status
= BLK_STS_IOERR
;
408 bio
->bi_opf
= REQ_OP_READ
;
409 if (dio
->should_dirty
)
410 bio_set_pages_dirty(bio
);
412 bio
->bi_opf
= dio_bio_write_op(iocb
);
413 task_io_account_write(bio
->bi_iter
.bi_size
);
416 dio
->size
+= bio
->bi_iter
.bi_size
;
417 pos
+= bio
->bi_iter
.bi_size
;
419 nr_pages
= iov_iter_npages(iter
, BIO_MAX_PAGES
);
423 if (iocb
->ki_flags
& IOCB_HIPRI
) {
424 bio_set_polled(bio
, iocb
);
428 qc
= submit_bio(bio
);
431 WRITE_ONCE(iocb
->ki_cookie
, qc
);
435 if (!dio
->multi_bio
) {
437 * AIO needs an extra reference to ensure the dio
438 * structure which is embedded into the first bio
443 dio
->multi_bio
= true;
444 atomic_set(&dio
->ref
, 2);
446 atomic_inc(&dio
->ref
);
450 bio
= bio_alloc(GFP_KERNEL
, nr_pages
);
454 blk_finish_plug(&plug
);
460 set_current_state(TASK_UNINTERRUPTIBLE
);
461 if (!READ_ONCE(dio
->waiter
))
464 if (!(iocb
->ki_flags
& IOCB_HIPRI
) ||
465 !blk_poll(bdev_get_queue(bdev
), qc
, true))
468 __set_current_state(TASK_RUNNING
);
471 ret
= blk_status_to_errno(dio
->bio
.bi_status
);
480 blkdev_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
484 nr_pages
= iov_iter_npages(iter
, BIO_MAX_PAGES
+ 1);
487 if (is_sync_kiocb(iocb
) && nr_pages
<= BIO_MAX_PAGES
)
488 return __blkdev_direct_IO_simple(iocb
, iter
, nr_pages
);
490 return __blkdev_direct_IO(iocb
, iter
, min(nr_pages
, BIO_MAX_PAGES
));
493 static __init
int blkdev_init(void)
495 return bioset_init(&blkdev_dio_pool
, 4, offsetof(struct blkdev_dio
, bio
), BIOSET_NEED_BVECS
);
497 module_init(blkdev_init
);
499 int __sync_blockdev(struct block_device
*bdev
, int wait
)
504 return filemap_flush(bdev
->bd_inode
->i_mapping
);
505 return filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
509 * Write out and wait upon all the dirty data associated with a block
510 * device via its mapping. Does not take the superblock lock.
512 int sync_blockdev(struct block_device
*bdev
)
514 return __sync_blockdev(bdev
, 1);
516 EXPORT_SYMBOL(sync_blockdev
);
519 * Write out and wait upon all dirty data associated with this
520 * device. Filesystem data as well as the underlying block
521 * device. Takes the superblock lock.
523 int fsync_bdev(struct block_device
*bdev
)
525 struct super_block
*sb
= get_super(bdev
);
527 int res
= sync_filesystem(sb
);
531 return sync_blockdev(bdev
);
533 EXPORT_SYMBOL(fsync_bdev
);
536 * freeze_bdev -- lock a filesystem and force it into a consistent state
537 * @bdev: blockdevice to lock
539 * If a superblock is found on this device, we take the s_umount semaphore
540 * on it to make sure nobody unmounts until the snapshot creation is done.
541 * The reference counter (bd_fsfreeze_count) guarantees that only the last
542 * unfreeze process can unfreeze the frozen filesystem actually when multiple
543 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
544 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
547 int freeze_bdev(struct block_device
*bdev
)
549 struct super_block
*sb
;
552 mutex_lock(&bdev
->bd_fsfreeze_mutex
);
553 if (++bdev
->bd_fsfreeze_count
> 1)
556 sb
= get_active_super(bdev
);
559 if (sb
->s_op
->freeze_super
)
560 error
= sb
->s_op
->freeze_super(sb
);
562 error
= freeze_super(sb
);
563 deactivate_super(sb
);
566 bdev
->bd_fsfreeze_count
--;
569 bdev
->bd_fsfreeze_sb
= sb
;
574 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
577 EXPORT_SYMBOL(freeze_bdev
);
580 * thaw_bdev -- unlock filesystem
581 * @bdev: blockdevice to unlock
583 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
585 int thaw_bdev(struct block_device
*bdev
)
587 struct super_block
*sb
;
590 mutex_lock(&bdev
->bd_fsfreeze_mutex
);
591 if (!bdev
->bd_fsfreeze_count
)
595 if (--bdev
->bd_fsfreeze_count
> 0)
598 sb
= bdev
->bd_fsfreeze_sb
;
602 if (sb
->s_op
->thaw_super
)
603 error
= sb
->s_op
->thaw_super(sb
);
605 error
= thaw_super(sb
);
607 bdev
->bd_fsfreeze_count
++;
609 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
612 EXPORT_SYMBOL(thaw_bdev
);
614 static int blkdev_writepage(struct page
*page
, struct writeback_control
*wbc
)
616 return block_write_full_page(page
, blkdev_get_block
, wbc
);
619 static int blkdev_readpage(struct file
* file
, struct page
* page
)
621 return block_read_full_page(page
, blkdev_get_block
);
624 static void blkdev_readahead(struct readahead_control
*rac
)
626 mpage_readahead(rac
, blkdev_get_block
);
629 static int blkdev_write_begin(struct file
*file
, struct address_space
*mapping
,
630 loff_t pos
, unsigned len
, unsigned flags
,
631 struct page
**pagep
, void **fsdata
)
633 return block_write_begin(mapping
, pos
, len
, flags
, pagep
,
637 static int blkdev_write_end(struct file
*file
, struct address_space
*mapping
,
638 loff_t pos
, unsigned len
, unsigned copied
,
639 struct page
*page
, void *fsdata
)
642 ret
= block_write_end(file
, mapping
, pos
, len
, copied
, page
, fsdata
);
652 * for a block special file file_inode(file)->i_size is zero
653 * so we compute the size by hand (just as in block_read/write above)
655 static loff_t
block_llseek(struct file
*file
, loff_t offset
, int whence
)
657 struct inode
*bd_inode
= bdev_file_inode(file
);
660 inode_lock(bd_inode
);
661 retval
= fixed_size_llseek(file
, offset
, whence
, i_size_read(bd_inode
));
662 inode_unlock(bd_inode
);
666 int blkdev_fsync(struct file
*filp
, loff_t start
, loff_t end
, int datasync
)
668 struct inode
*bd_inode
= bdev_file_inode(filp
);
669 struct block_device
*bdev
= I_BDEV(bd_inode
);
672 error
= file_write_and_wait_range(filp
, start
, end
);
677 * There is no need to serialise calls to blkdev_issue_flush with
678 * i_mutex and doing so causes performance issues with concurrent
679 * O_SYNC writers to a block device.
681 error
= blkdev_issue_flush(bdev
, GFP_KERNEL
);
682 if (error
== -EOPNOTSUPP
)
687 EXPORT_SYMBOL(blkdev_fsync
);
690 * bdev_read_page() - Start reading a page from a block device
691 * @bdev: The device to read the page from
692 * @sector: The offset on the device to read the page to (need not be aligned)
693 * @page: The page to read
695 * On entry, the page should be locked. It will be unlocked when the page
696 * has been read. If the block driver implements rw_page synchronously,
697 * that will be true on exit from this function, but it need not be.
699 * Errors returned by this function are usually "soft", eg out of memory, or
700 * queue full; callers should try a different route to read this page rather
701 * than propagate an error back up the stack.
703 * Return: negative errno if an error occurs, 0 if submission was successful.
705 int bdev_read_page(struct block_device
*bdev
, sector_t sector
,
708 const struct block_device_operations
*ops
= bdev
->bd_disk
->fops
;
709 int result
= -EOPNOTSUPP
;
711 if (!ops
->rw_page
|| bdev_get_integrity(bdev
))
714 result
= blk_queue_enter(bdev
->bd_disk
->queue
, 0);
717 result
= ops
->rw_page(bdev
, sector
+ get_start_sect(bdev
), page
,
719 blk_queue_exit(bdev
->bd_disk
->queue
);
724 * bdev_write_page() - Start writing a page to a block device
725 * @bdev: The device to write the page to
726 * @sector: The offset on the device to write the page to (need not be aligned)
727 * @page: The page to write
728 * @wbc: The writeback_control for the write
730 * On entry, the page should be locked and not currently under writeback.
731 * On exit, if the write started successfully, the page will be unlocked and
732 * under writeback. If the write failed already (eg the driver failed to
733 * queue the page to the device), the page will still be locked. If the
734 * caller is a ->writepage implementation, it will need to unlock the page.
736 * Errors returned by this function are usually "soft", eg out of memory, or
737 * queue full; callers should try a different route to write this page rather
738 * than propagate an error back up the stack.
740 * Return: negative errno if an error occurs, 0 if submission was successful.
742 int bdev_write_page(struct block_device
*bdev
, sector_t sector
,
743 struct page
*page
, struct writeback_control
*wbc
)
746 const struct block_device_operations
*ops
= bdev
->bd_disk
->fops
;
748 if (!ops
->rw_page
|| bdev_get_integrity(bdev
))
750 result
= blk_queue_enter(bdev
->bd_disk
->queue
, 0);
754 set_page_writeback(page
);
755 result
= ops
->rw_page(bdev
, sector
+ get_start_sect(bdev
), page
,
758 end_page_writeback(page
);
760 clean_page_buffers(page
);
763 blk_queue_exit(bdev
->bd_disk
->queue
);
771 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(bdev_lock
);
772 static struct kmem_cache
* bdev_cachep __read_mostly
;
774 static struct inode
*bdev_alloc_inode(struct super_block
*sb
)
776 struct bdev_inode
*ei
= kmem_cache_alloc(bdev_cachep
, GFP_KERNEL
);
779 return &ei
->vfs_inode
;
782 static void bdev_free_inode(struct inode
*inode
)
784 struct block_device
*bdev
= I_BDEV(inode
);
786 free_percpu(bdev
->bd_stats
);
787 kfree(bdev
->bd_meta_info
);
789 kmem_cache_free(bdev_cachep
, BDEV_I(inode
));
792 static void init_once(void *data
)
794 struct bdev_inode
*ei
= data
;
796 inode_init_once(&ei
->vfs_inode
);
799 static void bdev_evict_inode(struct inode
*inode
)
801 struct block_device
*bdev
= &BDEV_I(inode
)->bdev
;
802 truncate_inode_pages_final(&inode
->i_data
);
803 invalidate_inode_buffers(inode
); /* is it needed here? */
805 /* Detach inode from wb early as bdi_put() may free bdi->wb */
806 inode_detach_wb(inode
);
807 if (bdev
->bd_bdi
!= &noop_backing_dev_info
) {
808 bdi_put(bdev
->bd_bdi
);
809 bdev
->bd_bdi
= &noop_backing_dev_info
;
813 static const struct super_operations bdev_sops
= {
814 .statfs
= simple_statfs
,
815 .alloc_inode
= bdev_alloc_inode
,
816 .free_inode
= bdev_free_inode
,
817 .drop_inode
= generic_delete_inode
,
818 .evict_inode
= bdev_evict_inode
,
821 static int bd_init_fs_context(struct fs_context
*fc
)
823 struct pseudo_fs_context
*ctx
= init_pseudo(fc
, BDEVFS_MAGIC
);
826 fc
->s_iflags
|= SB_I_CGROUPWB
;
827 ctx
->ops
= &bdev_sops
;
831 static struct file_system_type bd_type
= {
833 .init_fs_context
= bd_init_fs_context
,
834 .kill_sb
= kill_anon_super
,
837 struct super_block
*blockdev_superblock __read_mostly
;
838 EXPORT_SYMBOL_GPL(blockdev_superblock
);
840 void __init
bdev_cache_init(void)
843 static struct vfsmount
*bd_mnt
;
845 bdev_cachep
= kmem_cache_create("bdev_cache", sizeof(struct bdev_inode
),
846 0, (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
847 SLAB_MEM_SPREAD
|SLAB_ACCOUNT
|SLAB_PANIC
),
849 err
= register_filesystem(&bd_type
);
851 panic("Cannot register bdev pseudo-fs");
852 bd_mnt
= kern_mount(&bd_type
);
854 panic("Cannot create bdev pseudo-fs");
855 blockdev_superblock
= bd_mnt
->mnt_sb
; /* For writeback */
858 struct block_device
*bdev_alloc(struct gendisk
*disk
, u8 partno
)
860 struct block_device
*bdev
;
863 inode
= new_inode(blockdev_superblock
);
866 inode
->i_mode
= S_IFBLK
;
868 inode
->i_data
.a_ops
= &def_blk_aops
;
869 mapping_set_gfp_mask(&inode
->i_data
, GFP_USER
);
871 bdev
= I_BDEV(inode
);
872 memset(bdev
, 0, sizeof(*bdev
));
873 mutex_init(&bdev
->bd_mutex
);
874 mutex_init(&bdev
->bd_fsfreeze_mutex
);
875 spin_lock_init(&bdev
->bd_size_lock
);
876 bdev
->bd_disk
= disk
;
877 bdev
->bd_partno
= partno
;
878 bdev
->bd_inode
= inode
;
879 bdev
->bd_bdi
= &noop_backing_dev_info
;
881 INIT_LIST_HEAD(&bdev
->bd_holder_disks
);
883 bdev
->bd_stats
= alloc_percpu(struct disk_stats
);
884 if (!bdev
->bd_stats
) {
891 void bdev_add(struct block_device
*bdev
, dev_t dev
)
894 bdev
->bd_inode
->i_rdev
= dev
;
895 bdev
->bd_inode
->i_ino
= dev
;
896 insert_inode_hash(bdev
->bd_inode
);
899 static struct block_device
*bdget(dev_t dev
)
903 inode
= ilookup(blockdev_superblock
, dev
);
906 return &BDEV_I(inode
)->bdev
;
910 * bdgrab -- Grab a reference to an already referenced block device
911 * @bdev: Block device to grab a reference to.
913 * Returns the block_device with an additional reference when successful,
914 * or NULL if the inode is already beeing freed.
916 struct block_device
*bdgrab(struct block_device
*bdev
)
918 if (!igrab(bdev
->bd_inode
))
922 EXPORT_SYMBOL(bdgrab
);
924 long nr_blockdev_pages(void)
929 spin_lock(&blockdev_superblock
->s_inode_list_lock
);
930 list_for_each_entry(inode
, &blockdev_superblock
->s_inodes
, i_sb_list
)
931 ret
+= inode
->i_mapping
->nrpages
;
932 spin_unlock(&blockdev_superblock
->s_inode_list_lock
);
937 void bdput(struct block_device
*bdev
)
939 iput(bdev
->bd_inode
);
941 EXPORT_SYMBOL(bdput
);
944 * bd_may_claim - test whether a block device can be claimed
945 * @bdev: block device of interest
946 * @whole: whole block device containing @bdev, may equal @bdev
947 * @holder: holder trying to claim @bdev
949 * Test whether @bdev can be claimed by @holder.
952 * spin_lock(&bdev_lock).
955 * %true if @bdev can be claimed, %false otherwise.
957 static bool bd_may_claim(struct block_device
*bdev
, struct block_device
*whole
,
960 if (bdev
->bd_holder
== holder
)
961 return true; /* already a holder */
962 else if (bdev
->bd_holder
!= NULL
)
963 return false; /* held by someone else */
964 else if (whole
== bdev
)
965 return true; /* is a whole device which isn't held */
967 else if (whole
->bd_holder
== bd_may_claim
)
968 return true; /* is a partition of a device that is being partitioned */
969 else if (whole
->bd_holder
!= NULL
)
970 return false; /* is a partition of a held device */
972 return true; /* is a partition of an un-held device */
976 * bd_prepare_to_claim - claim a block device
977 * @bdev: block device of interest
978 * @holder: holder trying to claim @bdev
980 * Claim @bdev. This function fails if @bdev is already claimed by another
981 * holder and waits if another claiming is in progress. return, the caller
982 * has ownership of bd_claiming and bd_holder[s].
985 * 0 if @bdev can be claimed, -EBUSY otherwise.
987 int bd_prepare_to_claim(struct block_device
*bdev
, void *holder
)
989 struct block_device
*whole
= bdev_whole(bdev
);
991 if (WARN_ON_ONCE(!holder
))
994 spin_lock(&bdev_lock
);
995 /* if someone else claimed, fail */
996 if (!bd_may_claim(bdev
, whole
, holder
)) {
997 spin_unlock(&bdev_lock
);
1001 /* if claiming is already in progress, wait for it to finish */
1002 if (whole
->bd_claiming
) {
1003 wait_queue_head_t
*wq
= bit_waitqueue(&whole
->bd_claiming
, 0);
1006 prepare_to_wait(wq
, &wait
, TASK_UNINTERRUPTIBLE
);
1007 spin_unlock(&bdev_lock
);
1009 finish_wait(wq
, &wait
);
1014 whole
->bd_claiming
= holder
;
1015 spin_unlock(&bdev_lock
);
1018 EXPORT_SYMBOL_GPL(bd_prepare_to_claim
); /* only for the loop driver */
1020 static void bd_clear_claiming(struct block_device
*whole
, void *holder
)
1022 lockdep_assert_held(&bdev_lock
);
1023 /* tell others that we're done */
1024 BUG_ON(whole
->bd_claiming
!= holder
);
1025 whole
->bd_claiming
= NULL
;
1026 wake_up_bit(&whole
->bd_claiming
, 0);
1030 * bd_finish_claiming - finish claiming of a block device
1031 * @bdev: block device of interest
1032 * @holder: holder that has claimed @bdev
1034 * Finish exclusive open of a block device. Mark the device as exlusively
1035 * open by the holder and wake up all waiters for exclusive open to finish.
1037 static void bd_finish_claiming(struct block_device
*bdev
, void *holder
)
1039 struct block_device
*whole
= bdev_whole(bdev
);
1041 spin_lock(&bdev_lock
);
1042 BUG_ON(!bd_may_claim(bdev
, whole
, holder
));
1044 * Note that for a whole device bd_holders will be incremented twice,
1045 * and bd_holder will be set to bd_may_claim before being set to holder
1047 whole
->bd_holders
++;
1048 whole
->bd_holder
= bd_may_claim
;
1050 bdev
->bd_holder
= holder
;
1051 bd_clear_claiming(whole
, holder
);
1052 spin_unlock(&bdev_lock
);
1056 * bd_abort_claiming - abort claiming of a block device
1057 * @bdev: block device of interest
1058 * @holder: holder that has claimed @bdev
1060 * Abort claiming of a block device when the exclusive open failed. This can be
1061 * also used when exclusive open is not actually desired and we just needed
1062 * to block other exclusive openers for a while.
1064 void bd_abort_claiming(struct block_device
*bdev
, void *holder
)
1066 spin_lock(&bdev_lock
);
1067 bd_clear_claiming(bdev_whole(bdev
), holder
);
1068 spin_unlock(&bdev_lock
);
1070 EXPORT_SYMBOL(bd_abort_claiming
);
1073 struct bd_holder_disk
{
1074 struct list_head list
;
1075 struct gendisk
*disk
;
1079 static struct bd_holder_disk
*bd_find_holder_disk(struct block_device
*bdev
,
1080 struct gendisk
*disk
)
1082 struct bd_holder_disk
*holder
;
1084 list_for_each_entry(holder
, &bdev
->bd_holder_disks
, list
)
1085 if (holder
->disk
== disk
)
1090 static int add_symlink(struct kobject
*from
, struct kobject
*to
)
1092 return sysfs_create_link(from
, to
, kobject_name(to
));
1095 static void del_symlink(struct kobject
*from
, struct kobject
*to
)
1097 sysfs_remove_link(from
, kobject_name(to
));
1101 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
1102 * @bdev: the claimed slave bdev
1103 * @disk: the holding disk
1105 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1107 * This functions creates the following sysfs symlinks.
1109 * - from "slaves" directory of the holder @disk to the claimed @bdev
1110 * - from "holders" directory of the @bdev to the holder @disk
1112 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
1113 * passed to bd_link_disk_holder(), then:
1115 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
1116 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
1118 * The caller must have claimed @bdev before calling this function and
1119 * ensure that both @bdev and @disk are valid during the creation and
1120 * lifetime of these symlinks.
1126 * 0 on success, -errno on failure.
1128 int bd_link_disk_holder(struct block_device
*bdev
, struct gendisk
*disk
)
1130 struct bd_holder_disk
*holder
;
1133 mutex_lock(&bdev
->bd_mutex
);
1135 WARN_ON_ONCE(!bdev
->bd_holder
);
1137 /* FIXME: remove the following once add_disk() handles errors */
1138 if (WARN_ON(!disk
->slave_dir
|| !bdev
->bd_holder_dir
))
1141 holder
= bd_find_holder_disk(bdev
, disk
);
1147 holder
= kzalloc(sizeof(*holder
), GFP_KERNEL
);
1153 INIT_LIST_HEAD(&holder
->list
);
1154 holder
->disk
= disk
;
1157 ret
= add_symlink(disk
->slave_dir
, bdev_kobj(bdev
));
1161 ret
= add_symlink(bdev
->bd_holder_dir
, &disk_to_dev(disk
)->kobj
);
1165 * bdev could be deleted beneath us which would implicitly destroy
1166 * the holder directory. Hold on to it.
1168 kobject_get(bdev
->bd_holder_dir
);
1170 list_add(&holder
->list
, &bdev
->bd_holder_disks
);
1174 del_symlink(disk
->slave_dir
, bdev_kobj(bdev
));
1178 mutex_unlock(&bdev
->bd_mutex
);
1181 EXPORT_SYMBOL_GPL(bd_link_disk_holder
);
1184 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
1185 * @bdev: the calimed slave bdev
1186 * @disk: the holding disk
1188 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1193 void bd_unlink_disk_holder(struct block_device
*bdev
, struct gendisk
*disk
)
1195 struct bd_holder_disk
*holder
;
1197 mutex_lock(&bdev
->bd_mutex
);
1199 holder
= bd_find_holder_disk(bdev
, disk
);
1201 if (!WARN_ON_ONCE(holder
== NULL
) && !--holder
->refcnt
) {
1202 del_symlink(disk
->slave_dir
, bdev_kobj(bdev
));
1203 del_symlink(bdev
->bd_holder_dir
, &disk_to_dev(disk
)->kobj
);
1204 kobject_put(bdev
->bd_holder_dir
);
1205 list_del_init(&holder
->list
);
1209 mutex_unlock(&bdev
->bd_mutex
);
1211 EXPORT_SYMBOL_GPL(bd_unlink_disk_holder
);
1214 static void __blkdev_put(struct block_device
*bdev
, fmode_t mode
, int for_part
);
1216 int bdev_disk_changed(struct block_device
*bdev
, bool invalidate
)
1218 struct gendisk
*disk
= bdev
->bd_disk
;
1221 lockdep_assert_held(&bdev
->bd_mutex
);
1223 clear_bit(GD_NEED_PART_SCAN
, &bdev
->bd_disk
->state
);
1226 ret
= blk_drop_partitions(bdev
);
1231 * Historically we only set the capacity to zero for devices that
1232 * support partitions (independ of actually having partitions created).
1233 * Doing that is rather inconsistent, but changing it broke legacy
1234 * udisks polling for legacy ide-cdrom devices. Use the crude check
1235 * below to get the sane behavior for most device while not breaking
1236 * userspace for this particular setup.
1239 if (disk_part_scan_enabled(disk
) ||
1240 !(disk
->flags
& GENHD_FL_REMOVABLE
))
1241 set_capacity(disk
, 0);
1243 if (disk
->fops
->revalidate_disk
)
1244 disk
->fops
->revalidate_disk(disk
);
1247 if (get_capacity(disk
)) {
1248 ret
= blk_add_partitions(disk
, bdev
);
1251 } else if (invalidate
) {
1253 * Tell userspace that the media / partition table may have
1256 kobject_uevent(&disk_to_dev(disk
)->kobj
, KOBJ_CHANGE
);
1262 * Only exported for for loop and dasd for historic reasons. Don't use in new
1265 EXPORT_SYMBOL_GPL(bdev_disk_changed
);
1270 * mutex_lock(part->bd_mutex)
1271 * mutex_lock_nested(whole->bd_mutex, 1)
1273 static int __blkdev_get(struct block_device
*bdev
, fmode_t mode
)
1275 struct gendisk
*disk
= bdev
->bd_disk
;
1278 if (!bdev
->bd_openers
) {
1279 if (!bdev_is_partition(bdev
)) {
1281 if (disk
->fops
->open
)
1282 ret
= disk
->fops
->open(bdev
, mode
);
1285 set_init_blocksize(bdev
);
1288 * If the device is invalidated, rescan partition
1289 * if open succeeded or failed with -ENOMEDIUM.
1290 * The latter is necessary to prevent ghost
1291 * partitions on a removed medium.
1293 if (test_bit(GD_NEED_PART_SCAN
, &disk
->state
) &&
1294 (!ret
|| ret
== -ENOMEDIUM
))
1295 bdev_disk_changed(bdev
, ret
== -ENOMEDIUM
);
1300 struct block_device
*whole
= bdgrab(disk
->part0
);
1302 mutex_lock_nested(&whole
->bd_mutex
, 1);
1303 ret
= __blkdev_get(whole
, mode
);
1305 mutex_unlock(&whole
->bd_mutex
);
1309 whole
->bd_part_count
++;
1310 mutex_unlock(&whole
->bd_mutex
);
1312 if (!(disk
->flags
& GENHD_FL_UP
) ||
1313 !bdev_nr_sectors(bdev
)) {
1314 __blkdev_put(whole
, mode
, 1);
1318 set_init_blocksize(bdev
);
1321 if (bdev
->bd_bdi
== &noop_backing_dev_info
)
1322 bdev
->bd_bdi
= bdi_get(disk
->queue
->backing_dev_info
);
1324 if (!bdev_is_partition(bdev
)) {
1325 if (bdev
->bd_disk
->fops
->open
)
1326 ret
= bdev
->bd_disk
->fops
->open(bdev
, mode
);
1327 /* the same as first opener case, read comment there */
1328 if (test_bit(GD_NEED_PART_SCAN
, &disk
->state
) &&
1329 (!ret
|| ret
== -ENOMEDIUM
))
1330 bdev_disk_changed(bdev
, ret
== -ENOMEDIUM
);
1339 struct block_device
*blkdev_get_no_open(dev_t dev
)
1341 struct block_device
*bdev
;
1342 struct gendisk
*disk
;
1344 down_read(&bdev_lookup_sem
);
1347 up_read(&bdev_lookup_sem
);
1348 blk_request_module(dev
);
1349 down_read(&bdev_lookup_sem
);
1356 disk
= bdev
->bd_disk
;
1357 if (!kobject_get_unless_zero(&disk_to_dev(disk
)->kobj
))
1359 if ((disk
->flags
& (GENHD_FL_UP
| GENHD_FL_HIDDEN
)) != GENHD_FL_UP
)
1361 if (!try_module_get(bdev
->bd_disk
->fops
->owner
))
1363 up_read(&bdev_lookup_sem
);
1370 up_read(&bdev_lookup_sem
);
1374 void blkdev_put_no_open(struct block_device
*bdev
)
1376 module_put(bdev
->bd_disk
->fops
->owner
);
1377 put_disk(bdev
->bd_disk
);
1382 * blkdev_get_by_dev - open a block device by device number
1383 * @dev: device number of block device to open
1384 * @mode: FMODE_* mask
1385 * @holder: exclusive holder identifier
1387 * Open the block device described by device number @dev. If @mode includes
1388 * %FMODE_EXCL, the block device is opened with exclusive access. Specifying
1389 * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for
1392 * Use this interface ONLY if you really do not have anything better - i.e. when
1393 * you are behind a truly sucky interface and all you are given is a device
1394 * number. Everything else should use blkdev_get_by_path().
1400 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
1402 struct block_device
*blkdev_get_by_dev(dev_t dev
, fmode_t mode
, void *holder
)
1404 bool unblock_events
= true;
1405 struct block_device
*bdev
;
1406 struct gendisk
*disk
;
1409 ret
= devcgroup_check_permission(DEVCG_DEV_BLOCK
,
1410 MAJOR(dev
), MINOR(dev
),
1411 ((mode
& FMODE_READ
) ? DEVCG_ACC_READ
: 0) |
1412 ((mode
& FMODE_WRITE
) ? DEVCG_ACC_WRITE
: 0));
1414 return ERR_PTR(ret
);
1417 * If we lost a race with 'disk' being deleted, try again. See md.c.
1420 bdev
= blkdev_get_no_open(dev
);
1422 return ERR_PTR(-ENXIO
);
1423 disk
= bdev
->bd_disk
;
1425 if (mode
& FMODE_EXCL
) {
1426 ret
= bd_prepare_to_claim(bdev
, holder
);
1431 disk_block_events(disk
);
1433 mutex_lock(&bdev
->bd_mutex
);
1434 ret
=__blkdev_get(bdev
, mode
);
1436 goto abort_claiming
;
1437 if (mode
& FMODE_EXCL
) {
1438 bd_finish_claiming(bdev
, holder
);
1441 * Block event polling for write claims if requested. Any write
1442 * holder makes the write_holder state stick until all are
1443 * released. This is good enough and tracking individual
1444 * writeable reference is too fragile given the way @mode is
1445 * used in blkdev_get/put().
1447 if ((mode
& FMODE_WRITE
) && !bdev
->bd_write_holder
&&
1448 (disk
->flags
& GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE
)) {
1449 bdev
->bd_write_holder
= true;
1450 unblock_events
= false;
1453 mutex_unlock(&bdev
->bd_mutex
);
1456 disk_unblock_events(disk
);
1460 if (mode
& FMODE_EXCL
)
1461 bd_abort_claiming(bdev
, holder
);
1462 mutex_unlock(&bdev
->bd_mutex
);
1463 disk_unblock_events(disk
);
1465 blkdev_put_no_open(bdev
);
1466 if (ret
== -ERESTARTSYS
)
1468 return ERR_PTR(ret
);
1470 EXPORT_SYMBOL(blkdev_get_by_dev
);
1473 * blkdev_get_by_path - open a block device by name
1474 * @path: path to the block device to open
1475 * @mode: FMODE_* mask
1476 * @holder: exclusive holder identifier
1478 * Open the block device described by the device file at @path. If @mode
1479 * includes %FMODE_EXCL, the block device is opened with exclusive access.
1480 * Specifying %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may
1481 * nest for the same @holder.
1487 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
1489 struct block_device
*blkdev_get_by_path(const char *path
, fmode_t mode
,
1492 struct block_device
*bdev
;
1496 error
= lookup_bdev(path
, &dev
);
1498 return ERR_PTR(error
);
1500 bdev
= blkdev_get_by_dev(dev
, mode
, holder
);
1501 if (!IS_ERR(bdev
) && (mode
& FMODE_WRITE
) && bdev_read_only(bdev
)) {
1502 blkdev_put(bdev
, mode
);
1503 return ERR_PTR(-EACCES
);
1508 EXPORT_SYMBOL(blkdev_get_by_path
);
1510 static int blkdev_open(struct inode
* inode
, struct file
* filp
)
1512 struct block_device
*bdev
;
1515 * Preserve backwards compatibility and allow large file access
1516 * even if userspace doesn't ask for it explicitly. Some mkfs
1517 * binary needs it. We might want to drop this workaround
1518 * during an unstable branch.
1520 filp
->f_flags
|= O_LARGEFILE
;
1522 filp
->f_mode
|= FMODE_NOWAIT
| FMODE_BUF_RASYNC
;
1524 if (filp
->f_flags
& O_NDELAY
)
1525 filp
->f_mode
|= FMODE_NDELAY
;
1526 if (filp
->f_flags
& O_EXCL
)
1527 filp
->f_mode
|= FMODE_EXCL
;
1528 if ((filp
->f_flags
& O_ACCMODE
) == 3)
1529 filp
->f_mode
|= FMODE_WRITE_IOCTL
;
1531 bdev
= blkdev_get_by_dev(inode
->i_rdev
, filp
->f_mode
, filp
);
1533 return PTR_ERR(bdev
);
1534 filp
->f_mapping
= bdev
->bd_inode
->i_mapping
;
1535 filp
->f_wb_err
= filemap_sample_wb_err(filp
->f_mapping
);
1539 static void __blkdev_put(struct block_device
*bdev
, fmode_t mode
, int for_part
)
1541 struct gendisk
*disk
= bdev
->bd_disk
;
1542 struct block_device
*victim
= NULL
;
1545 * Sync early if it looks like we're the last one. If someone else
1546 * opens the block device between now and the decrement of bd_openers
1547 * then we did a sync that we didn't need to, but that's not the end
1548 * of the world and we want to avoid long (could be several minute)
1549 * syncs while holding the mutex.
1551 if (bdev
->bd_openers
== 1)
1552 sync_blockdev(bdev
);
1554 mutex_lock_nested(&bdev
->bd_mutex
, for_part
);
1556 bdev
->bd_part_count
--;
1558 if (!--bdev
->bd_openers
) {
1559 WARN_ON_ONCE(bdev
->bd_holders
);
1560 sync_blockdev(bdev
);
1562 bdev_write_inode(bdev
);
1563 if (bdev_is_partition(bdev
))
1564 victim
= bdev_whole(bdev
);
1567 if (!bdev_is_partition(bdev
) && disk
->fops
->release
)
1568 disk
->fops
->release(disk
, mode
);
1569 mutex_unlock(&bdev
->bd_mutex
);
1571 __blkdev_put(victim
, mode
, 1);
1576 void blkdev_put(struct block_device
*bdev
, fmode_t mode
)
1578 struct gendisk
*disk
= bdev
->bd_disk
;
1580 mutex_lock(&bdev
->bd_mutex
);
1582 if (mode
& FMODE_EXCL
) {
1583 struct block_device
*whole
= bdev_whole(bdev
);
1587 * Release a claim on the device. The holder fields
1588 * are protected with bdev_lock. bd_mutex is to
1589 * synchronize disk_holder unlinking.
1591 spin_lock(&bdev_lock
);
1593 WARN_ON_ONCE(--bdev
->bd_holders
< 0);
1594 WARN_ON_ONCE(--whole
->bd_holders
< 0);
1596 if ((bdev_free
= !bdev
->bd_holders
))
1597 bdev
->bd_holder
= NULL
;
1598 if (!whole
->bd_holders
)
1599 whole
->bd_holder
= NULL
;
1601 spin_unlock(&bdev_lock
);
1604 * If this was the last claim, remove holder link and
1605 * unblock evpoll if it was a write holder.
1607 if (bdev_free
&& bdev
->bd_write_holder
) {
1608 disk_unblock_events(disk
);
1609 bdev
->bd_write_holder
= false;
1614 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
1615 * event. This is to ensure detection of media removal commanded
1616 * from userland - e.g. eject(1).
1618 disk_flush_events(disk
, DISK_EVENT_MEDIA_CHANGE
);
1619 mutex_unlock(&bdev
->bd_mutex
);
1621 __blkdev_put(bdev
, mode
, 0);
1622 blkdev_put_no_open(bdev
);
1624 EXPORT_SYMBOL(blkdev_put
);
1626 static int blkdev_close(struct inode
* inode
, struct file
* filp
)
1628 struct block_device
*bdev
= I_BDEV(bdev_file_inode(filp
));
1629 blkdev_put(bdev
, filp
->f_mode
);
1633 static long block_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
1635 struct block_device
*bdev
= I_BDEV(bdev_file_inode(file
));
1636 fmode_t mode
= file
->f_mode
;
1639 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1640 * to updated it before every ioctl.
1642 if (file
->f_flags
& O_NDELAY
)
1643 mode
|= FMODE_NDELAY
;
1645 mode
&= ~FMODE_NDELAY
;
1647 return blkdev_ioctl(bdev
, mode
, cmd
, arg
);
1651 * Write data to the block device. Only intended for the block device itself
1652 * and the raw driver which basically is a fake block device.
1654 * Does not take i_mutex for the write and thus is not for general purpose
1657 ssize_t
blkdev_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1659 struct file
*file
= iocb
->ki_filp
;
1660 struct inode
*bd_inode
= bdev_file_inode(file
);
1661 loff_t size
= i_size_read(bd_inode
);
1662 struct blk_plug plug
;
1665 if (bdev_read_only(I_BDEV(bd_inode
)))
1668 if (IS_SWAPFILE(bd_inode
) && !is_hibernate_resume_dev(bd_inode
->i_rdev
))
1671 if (!iov_iter_count(from
))
1674 if (iocb
->ki_pos
>= size
)
1677 if ((iocb
->ki_flags
& (IOCB_NOWAIT
| IOCB_DIRECT
)) == IOCB_NOWAIT
)
1680 iov_iter_truncate(from
, size
- iocb
->ki_pos
);
1682 blk_start_plug(&plug
);
1683 ret
= __generic_file_write_iter(iocb
, from
);
1685 ret
= generic_write_sync(iocb
, ret
);
1686 blk_finish_plug(&plug
);
1689 EXPORT_SYMBOL_GPL(blkdev_write_iter
);
1691 ssize_t
blkdev_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1693 struct file
*file
= iocb
->ki_filp
;
1694 struct inode
*bd_inode
= bdev_file_inode(file
);
1695 loff_t size
= i_size_read(bd_inode
);
1696 loff_t pos
= iocb
->ki_pos
;
1702 iov_iter_truncate(to
, size
);
1703 return generic_file_read_iter(iocb
, to
);
1705 EXPORT_SYMBOL_GPL(blkdev_read_iter
);
1708 * Try to release a page associated with block device when the system
1709 * is under memory pressure.
1711 static int blkdev_releasepage(struct page
*page
, gfp_t wait
)
1713 struct super_block
*super
= BDEV_I(page
->mapping
->host
)->bdev
.bd_super
;
1715 if (super
&& super
->s_op
->bdev_try_to_free_page
)
1716 return super
->s_op
->bdev_try_to_free_page(super
, page
, wait
);
1718 return try_to_free_buffers(page
);
1721 static int blkdev_writepages(struct address_space
*mapping
,
1722 struct writeback_control
*wbc
)
1724 return generic_writepages(mapping
, wbc
);
1727 static const struct address_space_operations def_blk_aops
= {
1728 .readpage
= blkdev_readpage
,
1729 .readahead
= blkdev_readahead
,
1730 .writepage
= blkdev_writepage
,
1731 .write_begin
= blkdev_write_begin
,
1732 .write_end
= blkdev_write_end
,
1733 .writepages
= blkdev_writepages
,
1734 .releasepage
= blkdev_releasepage
,
1735 .direct_IO
= blkdev_direct_IO
,
1736 .migratepage
= buffer_migrate_page_norefs
,
1737 .is_dirty_writeback
= buffer_check_dirty_writeback
,
1740 #define BLKDEV_FALLOC_FL_SUPPORTED \
1741 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
1742 FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
1744 static long blkdev_fallocate(struct file
*file
, int mode
, loff_t start
,
1747 struct block_device
*bdev
= I_BDEV(bdev_file_inode(file
));
1748 loff_t end
= start
+ len
- 1;
1752 /* Fail if we don't recognize the flags. */
1753 if (mode
& ~BLKDEV_FALLOC_FL_SUPPORTED
)
1756 /* Don't go off the end of the device. */
1757 isize
= i_size_read(bdev
->bd_inode
);
1761 if (mode
& FALLOC_FL_KEEP_SIZE
) {
1762 len
= isize
- start
;
1763 end
= start
+ len
- 1;
1769 * Don't allow IO that isn't aligned to logical block size.
1771 if ((start
| len
) & (bdev_logical_block_size(bdev
) - 1))
1774 /* Invalidate the page cache, including dirty pages. */
1775 error
= truncate_bdev_range(bdev
, file
->f_mode
, start
, end
);
1780 case FALLOC_FL_ZERO_RANGE
:
1781 case FALLOC_FL_ZERO_RANGE
| FALLOC_FL_KEEP_SIZE
:
1782 error
= blkdev_issue_zeroout(bdev
, start
>> 9, len
>> 9,
1783 GFP_KERNEL
, BLKDEV_ZERO_NOUNMAP
);
1785 case FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
:
1786 error
= blkdev_issue_zeroout(bdev
, start
>> 9, len
>> 9,
1787 GFP_KERNEL
, BLKDEV_ZERO_NOFALLBACK
);
1789 case FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
| FALLOC_FL_NO_HIDE_STALE
:
1790 error
= blkdev_issue_discard(bdev
, start
>> 9, len
>> 9,
1800 * Invalidate again; if someone wandered in and dirtied a page,
1801 * the caller will be given -EBUSY. The third argument is
1802 * inclusive, so the rounding here is safe.
1804 return invalidate_inode_pages2_range(bdev
->bd_inode
->i_mapping
,
1805 start
>> PAGE_SHIFT
,
1809 const struct file_operations def_blk_fops
= {
1810 .open
= blkdev_open
,
1811 .release
= blkdev_close
,
1812 .llseek
= block_llseek
,
1813 .read_iter
= blkdev_read_iter
,
1814 .write_iter
= blkdev_write_iter
,
1815 .iopoll
= blkdev_iopoll
,
1816 .mmap
= generic_file_mmap
,
1817 .fsync
= blkdev_fsync
,
1818 .unlocked_ioctl
= block_ioctl
,
1819 #ifdef CONFIG_COMPAT
1820 .compat_ioctl
= compat_blkdev_ioctl
,
1822 .splice_read
= generic_file_splice_read
,
1823 .splice_write
= iter_file_splice_write
,
1824 .fallocate
= blkdev_fallocate
,
1828 * lookup_bdev - lookup a struct block_device by name
1829 * @pathname: special file representing the block device
1830 * @dev: return value of the block device's dev_t
1832 * Get a reference to the blockdevice at @pathname in the current
1833 * namespace if possible and return it. Return ERR_PTR(error)
1836 int lookup_bdev(const char *pathname
, dev_t
*dev
)
1838 struct inode
*inode
;
1842 if (!pathname
|| !*pathname
)
1845 error
= kern_path(pathname
, LOOKUP_FOLLOW
, &path
);
1849 inode
= d_backing_inode(path
.dentry
);
1851 if (!S_ISBLK(inode
->i_mode
))
1854 if (!may_open_dev(&path
))
1857 *dev
= inode
->i_rdev
;
1863 EXPORT_SYMBOL(lookup_bdev
);
1865 int __invalidate_device(struct block_device
*bdev
, bool kill_dirty
)
1867 struct super_block
*sb
= get_super(bdev
);
1872 * no need to lock the super, get_super holds the
1873 * read mutex so the filesystem cannot go away
1874 * under us (->put_super runs with the write lock
1877 shrink_dcache_sb(sb
);
1878 res
= invalidate_inodes(sb
, kill_dirty
);
1881 invalidate_bdev(bdev
);
1884 EXPORT_SYMBOL(__invalidate_device
);
1886 void iterate_bdevs(void (*func
)(struct block_device
*, void *), void *arg
)
1888 struct inode
*inode
, *old_inode
= NULL
;
1890 spin_lock(&blockdev_superblock
->s_inode_list_lock
);
1891 list_for_each_entry(inode
, &blockdev_superblock
->s_inodes
, i_sb_list
) {
1892 struct address_space
*mapping
= inode
->i_mapping
;
1893 struct block_device
*bdev
;
1895 spin_lock(&inode
->i_lock
);
1896 if (inode
->i_state
& (I_FREEING
|I_WILL_FREE
|I_NEW
) ||
1897 mapping
->nrpages
== 0) {
1898 spin_unlock(&inode
->i_lock
);
1902 spin_unlock(&inode
->i_lock
);
1903 spin_unlock(&blockdev_superblock
->s_inode_list_lock
);
1905 * We hold a reference to 'inode' so it couldn't have been
1906 * removed from s_inodes list while we dropped the
1907 * s_inode_list_lock We cannot iput the inode now as we can
1908 * be holding the last reference and we cannot iput it under
1909 * s_inode_list_lock. So we keep the reference and iput it
1914 bdev
= I_BDEV(inode
);
1916 mutex_lock(&bdev
->bd_mutex
);
1917 if (bdev
->bd_openers
)
1919 mutex_unlock(&bdev
->bd_mutex
);
1921 spin_lock(&blockdev_superblock
->s_inode_list_lock
);
1923 spin_unlock(&blockdev_superblock
->s_inode_list_lock
);