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
);
131 * Someone else has handle exclusively open. Try invalidating instead.
132 * The 'end' argument is inclusive so the rounding is safe.
134 return invalidate_inode_pages2_range(bdev
->bd_inode
->i_mapping
,
135 lstart
>> PAGE_SHIFT
,
139 static void set_init_blocksize(struct block_device
*bdev
)
141 unsigned int bsize
= bdev_logical_block_size(bdev
);
142 loff_t size
= i_size_read(bdev
->bd_inode
);
144 while (bsize
< PAGE_SIZE
) {
149 bdev
->bd_inode
->i_blkbits
= blksize_bits(bsize
);
152 int set_blocksize(struct block_device
*bdev
, int size
)
154 /* Size must be a power of two, and between 512 and PAGE_SIZE */
155 if (size
> PAGE_SIZE
|| size
< 512 || !is_power_of_2(size
))
158 /* Size cannot be smaller than the size supported by the device */
159 if (size
< bdev_logical_block_size(bdev
))
162 /* Don't change the size if it is same as current */
163 if (bdev
->bd_inode
->i_blkbits
!= blksize_bits(size
)) {
165 bdev
->bd_inode
->i_blkbits
= blksize_bits(size
);
171 EXPORT_SYMBOL(set_blocksize
);
173 int sb_set_blocksize(struct super_block
*sb
, int size
)
175 if (set_blocksize(sb
->s_bdev
, size
))
177 /* If we get here, we know size is power of two
178 * and it's value is between 512 and PAGE_SIZE */
179 sb
->s_blocksize
= size
;
180 sb
->s_blocksize_bits
= blksize_bits(size
);
181 return sb
->s_blocksize
;
184 EXPORT_SYMBOL(sb_set_blocksize
);
186 int sb_min_blocksize(struct super_block
*sb
, int size
)
188 int minsize
= bdev_logical_block_size(sb
->s_bdev
);
191 return sb_set_blocksize(sb
, size
);
194 EXPORT_SYMBOL(sb_min_blocksize
);
197 blkdev_get_block(struct inode
*inode
, sector_t iblock
,
198 struct buffer_head
*bh
, int create
)
200 bh
->b_bdev
= I_BDEV(inode
);
201 bh
->b_blocknr
= iblock
;
202 set_buffer_mapped(bh
);
206 static struct inode
*bdev_file_inode(struct file
*file
)
208 return file
->f_mapping
->host
;
211 static unsigned int dio_bio_write_op(struct kiocb
*iocb
)
213 unsigned int op
= REQ_OP_WRITE
| REQ_SYNC
| REQ_IDLE
;
215 /* avoid the need for a I/O completion work item */
216 if (iocb
->ki_flags
& IOCB_DSYNC
)
221 #define DIO_INLINE_BIO_VECS 4
223 static void blkdev_bio_end_io_simple(struct bio
*bio
)
225 struct task_struct
*waiter
= bio
->bi_private
;
227 WRITE_ONCE(bio
->bi_private
, NULL
);
228 blk_wake_io_task(waiter
);
232 __blkdev_direct_IO_simple(struct kiocb
*iocb
, struct iov_iter
*iter
,
233 unsigned int nr_pages
)
235 struct file
*file
= iocb
->ki_filp
;
236 struct block_device
*bdev
= I_BDEV(bdev_file_inode(file
));
237 struct bio_vec inline_vecs
[DIO_INLINE_BIO_VECS
], *vecs
;
238 loff_t pos
= iocb
->ki_pos
;
239 bool should_dirty
= false;
244 if ((pos
| iov_iter_alignment(iter
)) &
245 (bdev_logical_block_size(bdev
) - 1))
248 if (nr_pages
<= DIO_INLINE_BIO_VECS
)
251 vecs
= kmalloc_array(nr_pages
, sizeof(struct bio_vec
),
257 bio_init(&bio
, vecs
, nr_pages
);
258 bio_set_dev(&bio
, bdev
);
259 bio
.bi_iter
.bi_sector
= pos
>> 9;
260 bio
.bi_write_hint
= iocb
->ki_hint
;
261 bio
.bi_private
= current
;
262 bio
.bi_end_io
= blkdev_bio_end_io_simple
;
263 bio
.bi_ioprio
= iocb
->ki_ioprio
;
265 ret
= bio_iov_iter_get_pages(&bio
, iter
);
268 ret
= bio
.bi_iter
.bi_size
;
270 if (iov_iter_rw(iter
) == READ
) {
271 bio
.bi_opf
= REQ_OP_READ
;
272 if (iter_is_iovec(iter
))
275 bio
.bi_opf
= dio_bio_write_op(iocb
);
276 task_io_account_write(ret
);
278 if (iocb
->ki_flags
& IOCB_NOWAIT
)
279 bio
.bi_opf
|= REQ_NOWAIT
;
280 if (iocb
->ki_flags
& IOCB_HIPRI
)
281 bio_set_polled(&bio
, iocb
);
283 qc
= submit_bio(&bio
);
285 set_current_state(TASK_UNINTERRUPTIBLE
);
286 if (!READ_ONCE(bio
.bi_private
))
288 if (!(iocb
->ki_flags
& IOCB_HIPRI
) ||
289 !blk_poll(bdev_get_queue(bdev
), qc
, true))
292 __set_current_state(TASK_RUNNING
);
294 bio_release_pages(&bio
, should_dirty
);
295 if (unlikely(bio
.bi_status
))
296 ret
= blk_status_to_errno(bio
.bi_status
);
299 if (vecs
!= inline_vecs
)
310 struct task_struct
*waiter
;
315 bool should_dirty
: 1;
320 static struct bio_set blkdev_dio_pool
;
322 static int blkdev_iopoll(struct kiocb
*kiocb
, bool wait
)
324 struct block_device
*bdev
= I_BDEV(kiocb
->ki_filp
->f_mapping
->host
);
325 struct request_queue
*q
= bdev_get_queue(bdev
);
327 return blk_poll(q
, READ_ONCE(kiocb
->ki_cookie
), wait
);
330 static void blkdev_bio_end_io(struct bio
*bio
)
332 struct blkdev_dio
*dio
= bio
->bi_private
;
333 bool should_dirty
= dio
->should_dirty
;
335 if (bio
->bi_status
&& !dio
->bio
.bi_status
)
336 dio
->bio
.bi_status
= bio
->bi_status
;
338 if (!dio
->multi_bio
|| atomic_dec_and_test(&dio
->ref
)) {
340 struct kiocb
*iocb
= dio
->iocb
;
343 if (likely(!dio
->bio
.bi_status
)) {
347 ret
= blk_status_to_errno(dio
->bio
.bi_status
);
350 dio
->iocb
->ki_complete(iocb
, ret
, 0);
354 struct task_struct
*waiter
= dio
->waiter
;
356 WRITE_ONCE(dio
->waiter
, NULL
);
357 blk_wake_io_task(waiter
);
362 bio_check_pages_dirty(bio
);
364 bio_release_pages(bio
, false);
369 static ssize_t
__blkdev_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
,
370 unsigned int nr_pages
)
372 struct file
*file
= iocb
->ki_filp
;
373 struct inode
*inode
= bdev_file_inode(file
);
374 struct block_device
*bdev
= I_BDEV(inode
);
375 struct blk_plug plug
;
376 struct blkdev_dio
*dio
;
378 bool is_poll
= (iocb
->ki_flags
& IOCB_HIPRI
) != 0;
379 bool is_read
= (iov_iter_rw(iter
) == READ
), is_sync
;
380 loff_t pos
= iocb
->ki_pos
;
381 blk_qc_t qc
= BLK_QC_T_NONE
;
384 if ((pos
| iov_iter_alignment(iter
)) &
385 (bdev_logical_block_size(bdev
) - 1))
388 bio
= bio_alloc_bioset(GFP_KERNEL
, nr_pages
, &blkdev_dio_pool
);
390 dio
= container_of(bio
, struct blkdev_dio
, bio
);
391 dio
->is_sync
= is_sync
= is_sync_kiocb(iocb
);
393 dio
->waiter
= current
;
400 dio
->multi_bio
= false;
401 dio
->should_dirty
= is_read
&& iter_is_iovec(iter
);
404 * Don't plug for HIPRI/polled IO, as those should go straight
408 blk_start_plug(&plug
);
411 bio_set_dev(bio
, bdev
);
412 bio
->bi_iter
.bi_sector
= pos
>> 9;
413 bio
->bi_write_hint
= iocb
->ki_hint
;
414 bio
->bi_private
= dio
;
415 bio
->bi_end_io
= blkdev_bio_end_io
;
416 bio
->bi_ioprio
= iocb
->ki_ioprio
;
418 ret
= bio_iov_iter_get_pages(bio
, iter
);
420 bio
->bi_status
= BLK_STS_IOERR
;
426 bio
->bi_opf
= REQ_OP_READ
;
427 if (dio
->should_dirty
)
428 bio_set_pages_dirty(bio
);
430 bio
->bi_opf
= dio_bio_write_op(iocb
);
431 task_io_account_write(bio
->bi_iter
.bi_size
);
433 if (iocb
->ki_flags
& IOCB_NOWAIT
)
434 bio
->bi_opf
|= REQ_NOWAIT
;
436 dio
->size
+= bio
->bi_iter
.bi_size
;
437 pos
+= bio
->bi_iter
.bi_size
;
439 nr_pages
= bio_iov_vecs_to_alloc(iter
, BIO_MAX_VECS
);
443 if (iocb
->ki_flags
& IOCB_HIPRI
) {
444 bio_set_polled(bio
, iocb
);
448 qc
= submit_bio(bio
);
451 WRITE_ONCE(iocb
->ki_cookie
, qc
);
455 if (!dio
->multi_bio
) {
457 * AIO needs an extra reference to ensure the dio
458 * structure which is embedded into the first bio
463 dio
->multi_bio
= true;
464 atomic_set(&dio
->ref
, 2);
466 atomic_inc(&dio
->ref
);
470 bio
= bio_alloc(GFP_KERNEL
, nr_pages
);
474 blk_finish_plug(&plug
);
480 set_current_state(TASK_UNINTERRUPTIBLE
);
481 if (!READ_ONCE(dio
->waiter
))
484 if (!(iocb
->ki_flags
& IOCB_HIPRI
) ||
485 !blk_poll(bdev_get_queue(bdev
), qc
, true))
488 __set_current_state(TASK_RUNNING
);
491 ret
= blk_status_to_errno(dio
->bio
.bi_status
);
500 blkdev_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
502 unsigned int nr_pages
;
504 if (!iov_iter_count(iter
))
507 nr_pages
= bio_iov_vecs_to_alloc(iter
, BIO_MAX_VECS
+ 1);
508 if (is_sync_kiocb(iocb
) && nr_pages
<= BIO_MAX_VECS
)
509 return __blkdev_direct_IO_simple(iocb
, iter
, nr_pages
);
511 return __blkdev_direct_IO(iocb
, iter
, bio_max_segs(nr_pages
));
514 static __init
int blkdev_init(void)
516 return bioset_init(&blkdev_dio_pool
, 4, offsetof(struct blkdev_dio
, bio
), BIOSET_NEED_BVECS
);
518 module_init(blkdev_init
);
520 int __sync_blockdev(struct block_device
*bdev
, int wait
)
525 return filemap_flush(bdev
->bd_inode
->i_mapping
);
526 return filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
530 * Write out and wait upon all the dirty data associated with a block
531 * device via its mapping. Does not take the superblock lock.
533 int sync_blockdev(struct block_device
*bdev
)
535 return __sync_blockdev(bdev
, 1);
537 EXPORT_SYMBOL(sync_blockdev
);
540 * Write out and wait upon all dirty data associated with this
541 * device. Filesystem data as well as the underlying block
542 * device. Takes the superblock lock.
544 int fsync_bdev(struct block_device
*bdev
)
546 struct super_block
*sb
= get_super(bdev
);
548 int res
= sync_filesystem(sb
);
552 return sync_blockdev(bdev
);
554 EXPORT_SYMBOL(fsync_bdev
);
557 * freeze_bdev -- lock a filesystem and force it into a consistent state
558 * @bdev: blockdevice to lock
560 * If a superblock is found on this device, we take the s_umount semaphore
561 * on it to make sure nobody unmounts until the snapshot creation is done.
562 * The reference counter (bd_fsfreeze_count) guarantees that only the last
563 * unfreeze process can unfreeze the frozen filesystem actually when multiple
564 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
565 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
568 int freeze_bdev(struct block_device
*bdev
)
570 struct super_block
*sb
;
573 mutex_lock(&bdev
->bd_fsfreeze_mutex
);
574 if (++bdev
->bd_fsfreeze_count
> 1)
577 sb
= get_active_super(bdev
);
580 if (sb
->s_op
->freeze_super
)
581 error
= sb
->s_op
->freeze_super(sb
);
583 error
= freeze_super(sb
);
584 deactivate_super(sb
);
587 bdev
->bd_fsfreeze_count
--;
590 bdev
->bd_fsfreeze_sb
= sb
;
595 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
598 EXPORT_SYMBOL(freeze_bdev
);
601 * thaw_bdev -- unlock filesystem
602 * @bdev: blockdevice to unlock
604 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
606 int thaw_bdev(struct block_device
*bdev
)
608 struct super_block
*sb
;
611 mutex_lock(&bdev
->bd_fsfreeze_mutex
);
612 if (!bdev
->bd_fsfreeze_count
)
616 if (--bdev
->bd_fsfreeze_count
> 0)
619 sb
= bdev
->bd_fsfreeze_sb
;
623 if (sb
->s_op
->thaw_super
)
624 error
= sb
->s_op
->thaw_super(sb
);
626 error
= thaw_super(sb
);
628 bdev
->bd_fsfreeze_count
++;
630 bdev
->bd_fsfreeze_sb
= NULL
;
632 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
635 EXPORT_SYMBOL(thaw_bdev
);
637 static int blkdev_writepage(struct page
*page
, struct writeback_control
*wbc
)
639 return block_write_full_page(page
, blkdev_get_block
, wbc
);
642 static int blkdev_readpage(struct file
* file
, struct page
* page
)
644 return block_read_full_page(page
, blkdev_get_block
);
647 static void blkdev_readahead(struct readahead_control
*rac
)
649 mpage_readahead(rac
, blkdev_get_block
);
652 static int blkdev_write_begin(struct file
*file
, struct address_space
*mapping
,
653 loff_t pos
, unsigned len
, unsigned flags
,
654 struct page
**pagep
, void **fsdata
)
656 return block_write_begin(mapping
, pos
, len
, flags
, pagep
,
660 static int blkdev_write_end(struct file
*file
, struct address_space
*mapping
,
661 loff_t pos
, unsigned len
, unsigned copied
,
662 struct page
*page
, void *fsdata
)
665 ret
= block_write_end(file
, mapping
, pos
, len
, copied
, page
, fsdata
);
675 * for a block special file file_inode(file)->i_size is zero
676 * so we compute the size by hand (just as in block_read/write above)
678 static loff_t
block_llseek(struct file
*file
, loff_t offset
, int whence
)
680 struct inode
*bd_inode
= bdev_file_inode(file
);
683 inode_lock(bd_inode
);
684 retval
= fixed_size_llseek(file
, offset
, whence
, i_size_read(bd_inode
));
685 inode_unlock(bd_inode
);
689 int blkdev_fsync(struct file
*filp
, loff_t start
, loff_t end
, int datasync
)
691 struct inode
*bd_inode
= bdev_file_inode(filp
);
692 struct block_device
*bdev
= I_BDEV(bd_inode
);
695 error
= file_write_and_wait_range(filp
, start
, end
);
700 * There is no need to serialise calls to blkdev_issue_flush with
701 * i_mutex and doing so causes performance issues with concurrent
702 * O_SYNC writers to a block device.
704 error
= blkdev_issue_flush(bdev
);
705 if (error
== -EOPNOTSUPP
)
710 EXPORT_SYMBOL(blkdev_fsync
);
713 * bdev_read_page() - Start reading a page from a block device
714 * @bdev: The device to read the page from
715 * @sector: The offset on the device to read the page to (need not be aligned)
716 * @page: The page to read
718 * On entry, the page should be locked. It will be unlocked when the page
719 * has been read. If the block driver implements rw_page synchronously,
720 * that will be true on exit from this function, but it need not be.
722 * Errors returned by this function are usually "soft", eg out of memory, or
723 * queue full; callers should try a different route to read this page rather
724 * than propagate an error back up the stack.
726 * Return: negative errno if an error occurs, 0 if submission was successful.
728 int bdev_read_page(struct block_device
*bdev
, sector_t sector
,
731 const struct block_device_operations
*ops
= bdev
->bd_disk
->fops
;
732 int result
= -EOPNOTSUPP
;
734 if (!ops
->rw_page
|| bdev_get_integrity(bdev
))
737 result
= blk_queue_enter(bdev
->bd_disk
->queue
, 0);
740 result
= ops
->rw_page(bdev
, sector
+ get_start_sect(bdev
), page
,
742 blk_queue_exit(bdev
->bd_disk
->queue
);
747 * bdev_write_page() - Start writing a page to a block device
748 * @bdev: The device to write the page to
749 * @sector: The offset on the device to write the page to (need not be aligned)
750 * @page: The page to write
751 * @wbc: The writeback_control for the write
753 * On entry, the page should be locked and not currently under writeback.
754 * On exit, if the write started successfully, the page will be unlocked and
755 * under writeback. If the write failed already (eg the driver failed to
756 * queue the page to the device), the page will still be locked. If the
757 * caller is a ->writepage implementation, it will need to unlock the page.
759 * Errors returned by this function are usually "soft", eg out of memory, or
760 * queue full; callers should try a different route to write this page rather
761 * than propagate an error back up the stack.
763 * Return: negative errno if an error occurs, 0 if submission was successful.
765 int bdev_write_page(struct block_device
*bdev
, sector_t sector
,
766 struct page
*page
, struct writeback_control
*wbc
)
769 const struct block_device_operations
*ops
= bdev
->bd_disk
->fops
;
771 if (!ops
->rw_page
|| bdev_get_integrity(bdev
))
773 result
= blk_queue_enter(bdev
->bd_disk
->queue
, 0);
777 set_page_writeback(page
);
778 result
= ops
->rw_page(bdev
, sector
+ get_start_sect(bdev
), page
,
781 end_page_writeback(page
);
783 clean_page_buffers(page
);
786 blk_queue_exit(bdev
->bd_disk
->queue
);
794 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(bdev_lock
);
795 static struct kmem_cache
* bdev_cachep __read_mostly
;
797 static struct inode
*bdev_alloc_inode(struct super_block
*sb
)
799 struct bdev_inode
*ei
= kmem_cache_alloc(bdev_cachep
, GFP_KERNEL
);
803 memset(&ei
->bdev
, 0, sizeof(ei
->bdev
));
804 ei
->bdev
.bd_bdi
= &noop_backing_dev_info
;
805 return &ei
->vfs_inode
;
808 static void bdev_free_inode(struct inode
*inode
)
810 struct block_device
*bdev
= I_BDEV(inode
);
812 free_percpu(bdev
->bd_stats
);
813 kfree(bdev
->bd_meta_info
);
815 kmem_cache_free(bdev_cachep
, BDEV_I(inode
));
818 static void init_once(void *data
)
820 struct bdev_inode
*ei
= data
;
822 inode_init_once(&ei
->vfs_inode
);
825 static void bdev_evict_inode(struct inode
*inode
)
827 struct block_device
*bdev
= &BDEV_I(inode
)->bdev
;
828 truncate_inode_pages_final(&inode
->i_data
);
829 invalidate_inode_buffers(inode
); /* is it needed here? */
831 /* Detach inode from wb early as bdi_put() may free bdi->wb */
832 inode_detach_wb(inode
);
833 if (bdev
->bd_bdi
!= &noop_backing_dev_info
) {
834 bdi_put(bdev
->bd_bdi
);
835 bdev
->bd_bdi
= &noop_backing_dev_info
;
839 static const struct super_operations bdev_sops
= {
840 .statfs
= simple_statfs
,
841 .alloc_inode
= bdev_alloc_inode
,
842 .free_inode
= bdev_free_inode
,
843 .drop_inode
= generic_delete_inode
,
844 .evict_inode
= bdev_evict_inode
,
847 static int bd_init_fs_context(struct fs_context
*fc
)
849 struct pseudo_fs_context
*ctx
= init_pseudo(fc
, BDEVFS_MAGIC
);
852 fc
->s_iflags
|= SB_I_CGROUPWB
;
853 ctx
->ops
= &bdev_sops
;
857 static struct file_system_type bd_type
= {
859 .init_fs_context
= bd_init_fs_context
,
860 .kill_sb
= kill_anon_super
,
863 struct super_block
*blockdev_superblock __read_mostly
;
864 EXPORT_SYMBOL_GPL(blockdev_superblock
);
866 void __init
bdev_cache_init(void)
869 static struct vfsmount
*bd_mnt
;
871 bdev_cachep
= kmem_cache_create("bdev_cache", sizeof(struct bdev_inode
),
872 0, (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
873 SLAB_MEM_SPREAD
|SLAB_ACCOUNT
|SLAB_PANIC
),
875 err
= register_filesystem(&bd_type
);
877 panic("Cannot register bdev pseudo-fs");
878 bd_mnt
= kern_mount(&bd_type
);
880 panic("Cannot create bdev pseudo-fs");
881 blockdev_superblock
= bd_mnt
->mnt_sb
; /* For writeback */
884 struct block_device
*bdev_alloc(struct gendisk
*disk
, u8 partno
)
886 struct block_device
*bdev
;
889 inode
= new_inode(blockdev_superblock
);
892 inode
->i_mode
= S_IFBLK
;
894 inode
->i_data
.a_ops
= &def_blk_aops
;
895 mapping_set_gfp_mask(&inode
->i_data
, GFP_USER
);
897 bdev
= I_BDEV(inode
);
898 mutex_init(&bdev
->bd_mutex
);
899 mutex_init(&bdev
->bd_fsfreeze_mutex
);
900 spin_lock_init(&bdev
->bd_size_lock
);
901 bdev
->bd_disk
= disk
;
902 bdev
->bd_partno
= partno
;
903 bdev
->bd_inode
= inode
;
905 INIT_LIST_HEAD(&bdev
->bd_holder_disks
);
907 bdev
->bd_stats
= alloc_percpu(struct disk_stats
);
908 if (!bdev
->bd_stats
) {
915 void bdev_add(struct block_device
*bdev
, dev_t dev
)
918 bdev
->bd_inode
->i_rdev
= dev
;
919 bdev
->bd_inode
->i_ino
= dev
;
920 insert_inode_hash(bdev
->bd_inode
);
923 static struct block_device
*bdget(dev_t dev
)
927 inode
= ilookup(blockdev_superblock
, dev
);
930 return &BDEV_I(inode
)->bdev
;
934 * bdgrab -- Grab a reference to an already referenced block device
935 * @bdev: Block device to grab a reference to.
937 * Returns the block_device with an additional reference when successful,
938 * or NULL if the inode is already beeing freed.
940 struct block_device
*bdgrab(struct block_device
*bdev
)
942 if (!igrab(bdev
->bd_inode
))
946 EXPORT_SYMBOL(bdgrab
);
948 long nr_blockdev_pages(void)
953 spin_lock(&blockdev_superblock
->s_inode_list_lock
);
954 list_for_each_entry(inode
, &blockdev_superblock
->s_inodes
, i_sb_list
)
955 ret
+= inode
->i_mapping
->nrpages
;
956 spin_unlock(&blockdev_superblock
->s_inode_list_lock
);
961 void bdput(struct block_device
*bdev
)
963 iput(bdev
->bd_inode
);
965 EXPORT_SYMBOL(bdput
);
968 * bd_may_claim - test whether a block device can be claimed
969 * @bdev: block device of interest
970 * @whole: whole block device containing @bdev, may equal @bdev
971 * @holder: holder trying to claim @bdev
973 * Test whether @bdev can be claimed by @holder.
976 * spin_lock(&bdev_lock).
979 * %true if @bdev can be claimed, %false otherwise.
981 static bool bd_may_claim(struct block_device
*bdev
, struct block_device
*whole
,
984 if (bdev
->bd_holder
== holder
)
985 return true; /* already a holder */
986 else if (bdev
->bd_holder
!= NULL
)
987 return false; /* held by someone else */
988 else if (whole
== bdev
)
989 return true; /* is a whole device which isn't held */
991 else if (whole
->bd_holder
== bd_may_claim
)
992 return true; /* is a partition of a device that is being partitioned */
993 else if (whole
->bd_holder
!= NULL
)
994 return false; /* is a partition of a held device */
996 return true; /* is a partition of an un-held device */
1000 * bd_prepare_to_claim - claim a block device
1001 * @bdev: block device of interest
1002 * @holder: holder trying to claim @bdev
1004 * Claim @bdev. This function fails if @bdev is already claimed by another
1005 * holder and waits if another claiming is in progress. return, the caller
1006 * has ownership of bd_claiming and bd_holder[s].
1009 * 0 if @bdev can be claimed, -EBUSY otherwise.
1011 int bd_prepare_to_claim(struct block_device
*bdev
, void *holder
)
1013 struct block_device
*whole
= bdev_whole(bdev
);
1015 if (WARN_ON_ONCE(!holder
))
1018 spin_lock(&bdev_lock
);
1019 /* if someone else claimed, fail */
1020 if (!bd_may_claim(bdev
, whole
, holder
)) {
1021 spin_unlock(&bdev_lock
);
1025 /* if claiming is already in progress, wait for it to finish */
1026 if (whole
->bd_claiming
) {
1027 wait_queue_head_t
*wq
= bit_waitqueue(&whole
->bd_claiming
, 0);
1030 prepare_to_wait(wq
, &wait
, TASK_UNINTERRUPTIBLE
);
1031 spin_unlock(&bdev_lock
);
1033 finish_wait(wq
, &wait
);
1038 whole
->bd_claiming
= holder
;
1039 spin_unlock(&bdev_lock
);
1042 EXPORT_SYMBOL_GPL(bd_prepare_to_claim
); /* only for the loop driver */
1044 static void bd_clear_claiming(struct block_device
*whole
, void *holder
)
1046 lockdep_assert_held(&bdev_lock
);
1047 /* tell others that we're done */
1048 BUG_ON(whole
->bd_claiming
!= holder
);
1049 whole
->bd_claiming
= NULL
;
1050 wake_up_bit(&whole
->bd_claiming
, 0);
1054 * bd_finish_claiming - finish claiming of a block device
1055 * @bdev: block device of interest
1056 * @holder: holder that has claimed @bdev
1058 * Finish exclusive open of a block device. Mark the device as exlusively
1059 * open by the holder and wake up all waiters for exclusive open to finish.
1061 static void bd_finish_claiming(struct block_device
*bdev
, void *holder
)
1063 struct block_device
*whole
= bdev_whole(bdev
);
1065 spin_lock(&bdev_lock
);
1066 BUG_ON(!bd_may_claim(bdev
, whole
, holder
));
1068 * Note that for a whole device bd_holders will be incremented twice,
1069 * and bd_holder will be set to bd_may_claim before being set to holder
1071 whole
->bd_holders
++;
1072 whole
->bd_holder
= bd_may_claim
;
1074 bdev
->bd_holder
= holder
;
1075 bd_clear_claiming(whole
, holder
);
1076 spin_unlock(&bdev_lock
);
1080 * bd_abort_claiming - abort claiming of a block device
1081 * @bdev: block device of interest
1082 * @holder: holder that has claimed @bdev
1084 * Abort claiming of a block device when the exclusive open failed. This can be
1085 * also used when exclusive open is not actually desired and we just needed
1086 * to block other exclusive openers for a while.
1088 void bd_abort_claiming(struct block_device
*bdev
, void *holder
)
1090 spin_lock(&bdev_lock
);
1091 bd_clear_claiming(bdev_whole(bdev
), holder
);
1092 spin_unlock(&bdev_lock
);
1094 EXPORT_SYMBOL(bd_abort_claiming
);
1097 struct bd_holder_disk
{
1098 struct list_head list
;
1099 struct gendisk
*disk
;
1103 static struct bd_holder_disk
*bd_find_holder_disk(struct block_device
*bdev
,
1104 struct gendisk
*disk
)
1106 struct bd_holder_disk
*holder
;
1108 list_for_each_entry(holder
, &bdev
->bd_holder_disks
, list
)
1109 if (holder
->disk
== disk
)
1114 static int add_symlink(struct kobject
*from
, struct kobject
*to
)
1116 return sysfs_create_link(from
, to
, kobject_name(to
));
1119 static void del_symlink(struct kobject
*from
, struct kobject
*to
)
1121 sysfs_remove_link(from
, kobject_name(to
));
1125 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
1126 * @bdev: the claimed slave bdev
1127 * @disk: the holding disk
1129 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1131 * This functions creates the following sysfs symlinks.
1133 * - from "slaves" directory of the holder @disk to the claimed @bdev
1134 * - from "holders" directory of the @bdev to the holder @disk
1136 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
1137 * passed to bd_link_disk_holder(), then:
1139 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
1140 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
1142 * The caller must have claimed @bdev before calling this function and
1143 * ensure that both @bdev and @disk are valid during the creation and
1144 * lifetime of these symlinks.
1150 * 0 on success, -errno on failure.
1152 int bd_link_disk_holder(struct block_device
*bdev
, struct gendisk
*disk
)
1154 struct bd_holder_disk
*holder
;
1157 mutex_lock(&bdev
->bd_mutex
);
1159 WARN_ON_ONCE(!bdev
->bd_holder
);
1161 /* FIXME: remove the following once add_disk() handles errors */
1162 if (WARN_ON(!disk
->slave_dir
|| !bdev
->bd_holder_dir
))
1165 holder
= bd_find_holder_disk(bdev
, disk
);
1171 holder
= kzalloc(sizeof(*holder
), GFP_KERNEL
);
1177 INIT_LIST_HEAD(&holder
->list
);
1178 holder
->disk
= disk
;
1181 ret
= add_symlink(disk
->slave_dir
, bdev_kobj(bdev
));
1185 ret
= add_symlink(bdev
->bd_holder_dir
, &disk_to_dev(disk
)->kobj
);
1189 * bdev could be deleted beneath us which would implicitly destroy
1190 * the holder directory. Hold on to it.
1192 kobject_get(bdev
->bd_holder_dir
);
1194 list_add(&holder
->list
, &bdev
->bd_holder_disks
);
1198 del_symlink(disk
->slave_dir
, bdev_kobj(bdev
));
1202 mutex_unlock(&bdev
->bd_mutex
);
1205 EXPORT_SYMBOL_GPL(bd_link_disk_holder
);
1208 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
1209 * @bdev: the calimed slave bdev
1210 * @disk: the holding disk
1212 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1217 void bd_unlink_disk_holder(struct block_device
*bdev
, struct gendisk
*disk
)
1219 struct bd_holder_disk
*holder
;
1221 mutex_lock(&bdev
->bd_mutex
);
1223 holder
= bd_find_holder_disk(bdev
, disk
);
1225 if (!WARN_ON_ONCE(holder
== NULL
) && !--holder
->refcnt
) {
1226 del_symlink(disk
->slave_dir
, bdev_kobj(bdev
));
1227 del_symlink(bdev
->bd_holder_dir
, &disk_to_dev(disk
)->kobj
);
1228 kobject_put(bdev
->bd_holder_dir
);
1229 list_del_init(&holder
->list
);
1233 mutex_unlock(&bdev
->bd_mutex
);
1235 EXPORT_SYMBOL_GPL(bd_unlink_disk_holder
);
1238 static void __blkdev_put(struct block_device
*bdev
, fmode_t mode
, int for_part
);
1240 int bdev_disk_changed(struct block_device
*bdev
, bool invalidate
)
1242 struct gendisk
*disk
= bdev
->bd_disk
;
1245 lockdep_assert_held(&bdev
->bd_mutex
);
1248 if (bdev
->bd_part_count
)
1250 sync_blockdev(bdev
);
1251 invalidate_bdev(bdev
);
1252 blk_drop_partitions(disk
);
1254 clear_bit(GD_NEED_PART_SCAN
, &disk
->state
);
1257 * Historically we only set the capacity to zero for devices that
1258 * support partitions (independ of actually having partitions created).
1259 * Doing that is rather inconsistent, but changing it broke legacy
1260 * udisks polling for legacy ide-cdrom devices. Use the crude check
1261 * below to get the sane behavior for most device while not breaking
1262 * userspace for this particular setup.
1265 if (disk_part_scan_enabled(disk
) ||
1266 !(disk
->flags
& GENHD_FL_REMOVABLE
))
1267 set_capacity(disk
, 0);
1270 if (get_capacity(disk
)) {
1271 ret
= blk_add_partitions(disk
, bdev
);
1274 } else if (invalidate
) {
1276 * Tell userspace that the media / partition table may have
1279 kobject_uevent(&disk_to_dev(disk
)->kobj
, KOBJ_CHANGE
);
1285 * Only exported for loop and dasd for historic reasons. Don't use in new
1288 EXPORT_SYMBOL_GPL(bdev_disk_changed
);
1293 * mutex_lock(part->bd_mutex)
1294 * mutex_lock_nested(whole->bd_mutex, 1)
1296 static int __blkdev_get(struct block_device
*bdev
, fmode_t mode
)
1298 struct gendisk
*disk
= bdev
->bd_disk
;
1301 if (!bdev
->bd_openers
) {
1302 if (!bdev_is_partition(bdev
)) {
1304 if (disk
->fops
->open
)
1305 ret
= disk
->fops
->open(bdev
, mode
);
1308 set_init_blocksize(bdev
);
1311 * If the device is invalidated, rescan partition
1312 * if open succeeded or failed with -ENOMEDIUM.
1313 * The latter is necessary to prevent ghost
1314 * partitions on a removed medium.
1316 if (test_bit(GD_NEED_PART_SCAN
, &disk
->state
) &&
1317 (!ret
|| ret
== -ENOMEDIUM
))
1318 bdev_disk_changed(bdev
, ret
== -ENOMEDIUM
);
1323 struct block_device
*whole
= bdgrab(disk
->part0
);
1325 mutex_lock_nested(&whole
->bd_mutex
, 1);
1326 ret
= __blkdev_get(whole
, mode
);
1328 mutex_unlock(&whole
->bd_mutex
);
1332 whole
->bd_part_count
++;
1333 mutex_unlock(&whole
->bd_mutex
);
1335 if (!(disk
->flags
& GENHD_FL_UP
) ||
1336 !bdev_nr_sectors(bdev
)) {
1337 __blkdev_put(whole
, mode
, 1);
1341 set_init_blocksize(bdev
);
1344 if (bdev
->bd_bdi
== &noop_backing_dev_info
)
1345 bdev
->bd_bdi
= bdi_get(disk
->queue
->backing_dev_info
);
1347 if (!bdev_is_partition(bdev
)) {
1348 if (bdev
->bd_disk
->fops
->open
)
1349 ret
= bdev
->bd_disk
->fops
->open(bdev
, mode
);
1350 /* the same as first opener case, read comment there */
1351 if (test_bit(GD_NEED_PART_SCAN
, &disk
->state
) &&
1352 (!ret
|| ret
== -ENOMEDIUM
))
1353 bdev_disk_changed(bdev
, ret
== -ENOMEDIUM
);
1362 struct block_device
*blkdev_get_no_open(dev_t dev
)
1364 struct block_device
*bdev
;
1365 struct gendisk
*disk
;
1367 down_read(&bdev_lookup_sem
);
1370 up_read(&bdev_lookup_sem
);
1371 blk_request_module(dev
);
1372 down_read(&bdev_lookup_sem
);
1379 disk
= bdev
->bd_disk
;
1380 if (!kobject_get_unless_zero(&disk_to_dev(disk
)->kobj
))
1382 if ((disk
->flags
& (GENHD_FL_UP
| GENHD_FL_HIDDEN
)) != GENHD_FL_UP
)
1384 if (!try_module_get(bdev
->bd_disk
->fops
->owner
))
1386 up_read(&bdev_lookup_sem
);
1393 up_read(&bdev_lookup_sem
);
1397 void blkdev_put_no_open(struct block_device
*bdev
)
1399 module_put(bdev
->bd_disk
->fops
->owner
);
1400 put_disk(bdev
->bd_disk
);
1405 * blkdev_get_by_dev - open a block device by device number
1406 * @dev: device number of block device to open
1407 * @mode: FMODE_* mask
1408 * @holder: exclusive holder identifier
1410 * Open the block device described by device number @dev. If @mode includes
1411 * %FMODE_EXCL, the block device is opened with exclusive access. Specifying
1412 * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for
1415 * Use this interface ONLY if you really do not have anything better - i.e. when
1416 * you are behind a truly sucky interface and all you are given is a device
1417 * number. Everything else should use blkdev_get_by_path().
1423 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
1425 struct block_device
*blkdev_get_by_dev(dev_t dev
, fmode_t mode
, void *holder
)
1427 bool unblock_events
= true;
1428 struct block_device
*bdev
;
1429 struct gendisk
*disk
;
1432 ret
= devcgroup_check_permission(DEVCG_DEV_BLOCK
,
1433 MAJOR(dev
), MINOR(dev
),
1434 ((mode
& FMODE_READ
) ? DEVCG_ACC_READ
: 0) |
1435 ((mode
& FMODE_WRITE
) ? DEVCG_ACC_WRITE
: 0));
1437 return ERR_PTR(ret
);
1439 bdev
= blkdev_get_no_open(dev
);
1441 return ERR_PTR(-ENXIO
);
1442 disk
= bdev
->bd_disk
;
1444 if (mode
& FMODE_EXCL
) {
1445 ret
= bd_prepare_to_claim(bdev
, holder
);
1450 disk_block_events(disk
);
1452 mutex_lock(&bdev
->bd_mutex
);
1453 ret
=__blkdev_get(bdev
, mode
);
1455 goto abort_claiming
;
1456 if (mode
& FMODE_EXCL
) {
1457 bd_finish_claiming(bdev
, holder
);
1460 * Block event polling for write claims if requested. Any write
1461 * holder makes the write_holder state stick until all are
1462 * released. This is good enough and tracking individual
1463 * writeable reference is too fragile given the way @mode is
1464 * used in blkdev_get/put().
1466 if ((mode
& FMODE_WRITE
) && !bdev
->bd_write_holder
&&
1467 (disk
->flags
& GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE
)) {
1468 bdev
->bd_write_holder
= true;
1469 unblock_events
= false;
1472 mutex_unlock(&bdev
->bd_mutex
);
1475 disk_unblock_events(disk
);
1479 if (mode
& FMODE_EXCL
)
1480 bd_abort_claiming(bdev
, holder
);
1481 mutex_unlock(&bdev
->bd_mutex
);
1482 disk_unblock_events(disk
);
1484 blkdev_put_no_open(bdev
);
1485 return ERR_PTR(ret
);
1487 EXPORT_SYMBOL(blkdev_get_by_dev
);
1490 * blkdev_get_by_path - open a block device by name
1491 * @path: path to the block device to open
1492 * @mode: FMODE_* mask
1493 * @holder: exclusive holder identifier
1495 * Open the block device described by the device file at @path. If @mode
1496 * includes %FMODE_EXCL, the block device is opened with exclusive access.
1497 * Specifying %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may
1498 * nest for the same @holder.
1504 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
1506 struct block_device
*blkdev_get_by_path(const char *path
, fmode_t mode
,
1509 struct block_device
*bdev
;
1513 error
= lookup_bdev(path
, &dev
);
1515 return ERR_PTR(error
);
1517 bdev
= blkdev_get_by_dev(dev
, mode
, holder
);
1518 if (!IS_ERR(bdev
) && (mode
& FMODE_WRITE
) && bdev_read_only(bdev
)) {
1519 blkdev_put(bdev
, mode
);
1520 return ERR_PTR(-EACCES
);
1525 EXPORT_SYMBOL(blkdev_get_by_path
);
1527 static int blkdev_open(struct inode
* inode
, struct file
* filp
)
1529 struct block_device
*bdev
;
1532 * Preserve backwards compatibility and allow large file access
1533 * even if userspace doesn't ask for it explicitly. Some mkfs
1534 * binary needs it. We might want to drop this workaround
1535 * during an unstable branch.
1537 filp
->f_flags
|= O_LARGEFILE
;
1539 filp
->f_mode
|= FMODE_NOWAIT
| FMODE_BUF_RASYNC
;
1541 if (filp
->f_flags
& O_NDELAY
)
1542 filp
->f_mode
|= FMODE_NDELAY
;
1543 if (filp
->f_flags
& O_EXCL
)
1544 filp
->f_mode
|= FMODE_EXCL
;
1545 if ((filp
->f_flags
& O_ACCMODE
) == 3)
1546 filp
->f_mode
|= FMODE_WRITE_IOCTL
;
1548 bdev
= blkdev_get_by_dev(inode
->i_rdev
, filp
->f_mode
, filp
);
1550 return PTR_ERR(bdev
);
1551 filp
->f_mapping
= bdev
->bd_inode
->i_mapping
;
1552 filp
->f_wb_err
= filemap_sample_wb_err(filp
->f_mapping
);
1556 static void __blkdev_put(struct block_device
*bdev
, fmode_t mode
, int for_part
)
1558 struct gendisk
*disk
= bdev
->bd_disk
;
1559 struct block_device
*victim
= NULL
;
1562 * Sync early if it looks like we're the last one. If someone else
1563 * opens the block device between now and the decrement of bd_openers
1564 * then we did a sync that we didn't need to, but that's not the end
1565 * of the world and we want to avoid long (could be several minute)
1566 * syncs while holding the mutex.
1568 if (bdev
->bd_openers
== 1)
1569 sync_blockdev(bdev
);
1571 mutex_lock_nested(&bdev
->bd_mutex
, for_part
);
1573 bdev
->bd_part_count
--;
1575 if (!--bdev
->bd_openers
) {
1576 WARN_ON_ONCE(bdev
->bd_holders
);
1577 sync_blockdev(bdev
);
1579 bdev_write_inode(bdev
);
1580 if (bdev_is_partition(bdev
))
1581 victim
= bdev_whole(bdev
);
1584 if (!bdev_is_partition(bdev
) && disk
->fops
->release
)
1585 disk
->fops
->release(disk
, mode
);
1586 mutex_unlock(&bdev
->bd_mutex
);
1588 __blkdev_put(victim
, mode
, 1);
1593 void blkdev_put(struct block_device
*bdev
, fmode_t mode
)
1595 struct gendisk
*disk
= bdev
->bd_disk
;
1597 mutex_lock(&bdev
->bd_mutex
);
1599 if (mode
& FMODE_EXCL
) {
1600 struct block_device
*whole
= bdev_whole(bdev
);
1604 * Release a claim on the device. The holder fields
1605 * are protected with bdev_lock. bd_mutex is to
1606 * synchronize disk_holder unlinking.
1608 spin_lock(&bdev_lock
);
1610 WARN_ON_ONCE(--bdev
->bd_holders
< 0);
1611 WARN_ON_ONCE(--whole
->bd_holders
< 0);
1613 if ((bdev_free
= !bdev
->bd_holders
))
1614 bdev
->bd_holder
= NULL
;
1615 if (!whole
->bd_holders
)
1616 whole
->bd_holder
= NULL
;
1618 spin_unlock(&bdev_lock
);
1621 * If this was the last claim, remove holder link and
1622 * unblock evpoll if it was a write holder.
1624 if (bdev_free
&& bdev
->bd_write_holder
) {
1625 disk_unblock_events(disk
);
1626 bdev
->bd_write_holder
= false;
1631 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
1632 * event. This is to ensure detection of media removal commanded
1633 * from userland - e.g. eject(1).
1635 disk_flush_events(disk
, DISK_EVENT_MEDIA_CHANGE
);
1636 mutex_unlock(&bdev
->bd_mutex
);
1638 __blkdev_put(bdev
, mode
, 0);
1639 blkdev_put_no_open(bdev
);
1641 EXPORT_SYMBOL(blkdev_put
);
1643 static int blkdev_close(struct inode
* inode
, struct file
* filp
)
1645 struct block_device
*bdev
= I_BDEV(bdev_file_inode(filp
));
1646 blkdev_put(bdev
, filp
->f_mode
);
1650 static long block_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
1652 struct block_device
*bdev
= I_BDEV(bdev_file_inode(file
));
1653 fmode_t mode
= file
->f_mode
;
1656 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1657 * to updated it before every ioctl.
1659 if (file
->f_flags
& O_NDELAY
)
1660 mode
|= FMODE_NDELAY
;
1662 mode
&= ~FMODE_NDELAY
;
1664 return blkdev_ioctl(bdev
, mode
, cmd
, arg
);
1668 * Write data to the block device. Only intended for the block device itself
1669 * and the raw driver which basically is a fake block device.
1671 * Does not take i_mutex for the write and thus is not for general purpose
1674 ssize_t
blkdev_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1676 struct file
*file
= iocb
->ki_filp
;
1677 struct inode
*bd_inode
= bdev_file_inode(file
);
1678 loff_t size
= i_size_read(bd_inode
);
1679 struct blk_plug plug
;
1682 if (bdev_read_only(I_BDEV(bd_inode
)))
1685 if (IS_SWAPFILE(bd_inode
) && !is_hibernate_resume_dev(bd_inode
->i_rdev
))
1688 if (!iov_iter_count(from
))
1691 if (iocb
->ki_pos
>= size
)
1694 if ((iocb
->ki_flags
& (IOCB_NOWAIT
| IOCB_DIRECT
)) == IOCB_NOWAIT
)
1697 iov_iter_truncate(from
, size
- iocb
->ki_pos
);
1699 blk_start_plug(&plug
);
1700 ret
= __generic_file_write_iter(iocb
, from
);
1702 ret
= generic_write_sync(iocb
, ret
);
1703 blk_finish_plug(&plug
);
1706 EXPORT_SYMBOL_GPL(blkdev_write_iter
);
1708 ssize_t
blkdev_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1710 struct file
*file
= iocb
->ki_filp
;
1711 struct inode
*bd_inode
= bdev_file_inode(file
);
1712 loff_t size
= i_size_read(bd_inode
);
1713 loff_t pos
= iocb
->ki_pos
;
1719 iov_iter_truncate(to
, size
);
1720 return generic_file_read_iter(iocb
, to
);
1722 EXPORT_SYMBOL_GPL(blkdev_read_iter
);
1725 * Try to release a page associated with block device when the system
1726 * is under memory pressure.
1728 static int blkdev_releasepage(struct page
*page
, gfp_t wait
)
1730 struct super_block
*super
= BDEV_I(page
->mapping
->host
)->bdev
.bd_super
;
1732 if (super
&& super
->s_op
->bdev_try_to_free_page
)
1733 return super
->s_op
->bdev_try_to_free_page(super
, page
, wait
);
1735 return try_to_free_buffers(page
);
1738 static int blkdev_writepages(struct address_space
*mapping
,
1739 struct writeback_control
*wbc
)
1741 return generic_writepages(mapping
, wbc
);
1744 static const struct address_space_operations def_blk_aops
= {
1745 .readpage
= blkdev_readpage
,
1746 .readahead
= blkdev_readahead
,
1747 .writepage
= blkdev_writepage
,
1748 .write_begin
= blkdev_write_begin
,
1749 .write_end
= blkdev_write_end
,
1750 .writepages
= blkdev_writepages
,
1751 .releasepage
= blkdev_releasepage
,
1752 .direct_IO
= blkdev_direct_IO
,
1753 .migratepage
= buffer_migrate_page_norefs
,
1754 .is_dirty_writeback
= buffer_check_dirty_writeback
,
1757 #define BLKDEV_FALLOC_FL_SUPPORTED \
1758 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
1759 FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
1761 static long blkdev_fallocate(struct file
*file
, int mode
, loff_t start
,
1764 struct block_device
*bdev
= I_BDEV(bdev_file_inode(file
));
1765 loff_t end
= start
+ len
- 1;
1769 /* Fail if we don't recognize the flags. */
1770 if (mode
& ~BLKDEV_FALLOC_FL_SUPPORTED
)
1773 /* Don't go off the end of the device. */
1774 isize
= i_size_read(bdev
->bd_inode
);
1778 if (mode
& FALLOC_FL_KEEP_SIZE
) {
1779 len
= isize
- start
;
1780 end
= start
+ len
- 1;
1786 * Don't allow IO that isn't aligned to logical block size.
1788 if ((start
| len
) & (bdev_logical_block_size(bdev
) - 1))
1791 /* Invalidate the page cache, including dirty pages. */
1792 error
= truncate_bdev_range(bdev
, file
->f_mode
, start
, end
);
1797 case FALLOC_FL_ZERO_RANGE
:
1798 case FALLOC_FL_ZERO_RANGE
| FALLOC_FL_KEEP_SIZE
:
1799 error
= blkdev_issue_zeroout(bdev
, start
>> 9, len
>> 9,
1800 GFP_KERNEL
, BLKDEV_ZERO_NOUNMAP
);
1802 case FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
:
1803 error
= blkdev_issue_zeroout(bdev
, start
>> 9, len
>> 9,
1804 GFP_KERNEL
, BLKDEV_ZERO_NOFALLBACK
);
1806 case FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
| FALLOC_FL_NO_HIDE_STALE
:
1807 error
= blkdev_issue_discard(bdev
, start
>> 9, len
>> 9,
1817 * Invalidate the page cache again; if someone wandered in and dirtied
1818 * a page, we just discard it - userspace has no way of knowing whether
1819 * the write happened before or after discard completing...
1821 return truncate_bdev_range(bdev
, file
->f_mode
, start
, end
);
1824 const struct file_operations def_blk_fops
= {
1825 .open
= blkdev_open
,
1826 .release
= blkdev_close
,
1827 .llseek
= block_llseek
,
1828 .read_iter
= blkdev_read_iter
,
1829 .write_iter
= blkdev_write_iter
,
1830 .iopoll
= blkdev_iopoll
,
1831 .mmap
= generic_file_mmap
,
1832 .fsync
= blkdev_fsync
,
1833 .unlocked_ioctl
= block_ioctl
,
1834 #ifdef CONFIG_COMPAT
1835 .compat_ioctl
= compat_blkdev_ioctl
,
1837 .splice_read
= generic_file_splice_read
,
1838 .splice_write
= iter_file_splice_write
,
1839 .fallocate
= blkdev_fallocate
,
1843 * lookup_bdev - lookup a struct block_device by name
1844 * @pathname: special file representing the block device
1845 * @dev: return value of the block device's dev_t
1847 * Get a reference to the blockdevice at @pathname in the current
1848 * namespace if possible and return it. Return ERR_PTR(error)
1851 int lookup_bdev(const char *pathname
, dev_t
*dev
)
1853 struct inode
*inode
;
1857 if (!pathname
|| !*pathname
)
1860 error
= kern_path(pathname
, LOOKUP_FOLLOW
, &path
);
1864 inode
= d_backing_inode(path
.dentry
);
1866 if (!S_ISBLK(inode
->i_mode
))
1869 if (!may_open_dev(&path
))
1872 *dev
= inode
->i_rdev
;
1878 EXPORT_SYMBOL(lookup_bdev
);
1880 int __invalidate_device(struct block_device
*bdev
, bool kill_dirty
)
1882 struct super_block
*sb
= get_super(bdev
);
1887 * no need to lock the super, get_super holds the
1888 * read mutex so the filesystem cannot go away
1889 * under us (->put_super runs with the write lock
1892 shrink_dcache_sb(sb
);
1893 res
= invalidate_inodes(sb
, kill_dirty
);
1896 invalidate_bdev(bdev
);
1899 EXPORT_SYMBOL(__invalidate_device
);
1901 void iterate_bdevs(void (*func
)(struct block_device
*, void *), void *arg
)
1903 struct inode
*inode
, *old_inode
= NULL
;
1905 spin_lock(&blockdev_superblock
->s_inode_list_lock
);
1906 list_for_each_entry(inode
, &blockdev_superblock
->s_inodes
, i_sb_list
) {
1907 struct address_space
*mapping
= inode
->i_mapping
;
1908 struct block_device
*bdev
;
1910 spin_lock(&inode
->i_lock
);
1911 if (inode
->i_state
& (I_FREEING
|I_WILL_FREE
|I_NEW
) ||
1912 mapping
->nrpages
== 0) {
1913 spin_unlock(&inode
->i_lock
);
1917 spin_unlock(&inode
->i_lock
);
1918 spin_unlock(&blockdev_superblock
->s_inode_list_lock
);
1920 * We hold a reference to 'inode' so it couldn't have been
1921 * removed from s_inodes list while we dropped the
1922 * s_inode_list_lock We cannot iput the inode now as we can
1923 * be holding the last reference and we cannot iput it under
1924 * s_inode_list_lock. So we keep the reference and iput it
1929 bdev
= I_BDEV(inode
);
1931 mutex_lock(&bdev
->bd_mutex
);
1932 if (bdev
->bd_openers
)
1934 mutex_unlock(&bdev
->bd_mutex
);
1936 spin_lock(&blockdev_superblock
->s_inode_list_lock
);
1938 spin_unlock(&blockdev_superblock
->s_inode_list_lock
);