4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
8 #include <linux/init.h>
10 #include <linux/fcntl.h>
11 #include <linux/slab.h>
12 #include <linux/kmod.h>
13 #include <linux/major.h>
14 #include <linux/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/uio.h>
28 #include <linux/namei.h>
29 #include <linux/log2.h>
30 #include <linux/cleancache.h>
31 #include <linux/dax.h>
32 #include <linux/badblocks.h>
33 #include <asm/uaccess.h>
37 struct block_device bdev
;
38 struct inode vfs_inode
;
41 static const struct address_space_operations def_blk_aops
;
43 static inline struct bdev_inode
*BDEV_I(struct inode
*inode
)
45 return container_of(inode
, struct bdev_inode
, vfs_inode
);
48 struct block_device
*I_BDEV(struct inode
*inode
)
50 return &BDEV_I(inode
)->bdev
;
52 EXPORT_SYMBOL(I_BDEV
);
54 static void bdev_write_inode(struct block_device
*bdev
)
56 struct inode
*inode
= bdev
->bd_inode
;
59 spin_lock(&inode
->i_lock
);
60 while (inode
->i_state
& I_DIRTY
) {
61 spin_unlock(&inode
->i_lock
);
62 ret
= write_inode_now(inode
, true);
64 char name
[BDEVNAME_SIZE
];
65 pr_warn_ratelimited("VFS: Dirty inode writeback failed "
66 "for block device %s (err=%d).\n",
67 bdevname(bdev
, name
), ret
);
69 spin_lock(&inode
->i_lock
);
71 spin_unlock(&inode
->i_lock
);
74 /* Kill _all_ buffers and pagecache , dirty or not.. */
75 void kill_bdev(struct block_device
*bdev
)
77 struct address_space
*mapping
= bdev
->bd_inode
->i_mapping
;
79 if (mapping
->nrpages
== 0 && mapping
->nrexceptional
== 0)
83 truncate_inode_pages(mapping
, 0);
85 EXPORT_SYMBOL(kill_bdev
);
87 /* Invalidate clean unused buffers and pagecache. */
88 void invalidate_bdev(struct block_device
*bdev
)
90 struct address_space
*mapping
= bdev
->bd_inode
->i_mapping
;
92 if (mapping
->nrpages
== 0)
96 lru_add_drain_all(); /* make sure all lru add caches are flushed */
97 invalidate_mapping_pages(mapping
, 0, -1);
98 /* 99% of the time, we don't need to flush the cleancache on the bdev.
99 * But, for the strange corners, lets be cautious
101 cleancache_invalidate_inode(mapping
);
103 EXPORT_SYMBOL(invalidate_bdev
);
105 int set_blocksize(struct block_device
*bdev
, int size
)
107 /* Size must be a power of two, and between 512 and PAGE_SIZE */
108 if (size
> PAGE_SIZE
|| size
< 512 || !is_power_of_2(size
))
111 /* Size cannot be smaller than the size supported by the device */
112 if (size
< bdev_logical_block_size(bdev
))
115 /* Don't change the size if it is same as current */
116 if (bdev
->bd_block_size
!= size
) {
118 bdev
->bd_block_size
= size
;
119 bdev
->bd_inode
->i_blkbits
= blksize_bits(size
);
125 EXPORT_SYMBOL(set_blocksize
);
127 int sb_set_blocksize(struct super_block
*sb
, int size
)
129 if (set_blocksize(sb
->s_bdev
, size
))
131 /* If we get here, we know size is power of two
132 * and it's value is between 512 and PAGE_SIZE */
133 sb
->s_blocksize
= size
;
134 sb
->s_blocksize_bits
= blksize_bits(size
);
135 return sb
->s_blocksize
;
138 EXPORT_SYMBOL(sb_set_blocksize
);
140 int sb_min_blocksize(struct super_block
*sb
, int size
)
142 int minsize
= bdev_logical_block_size(sb
->s_bdev
);
145 return sb_set_blocksize(sb
, size
);
148 EXPORT_SYMBOL(sb_min_blocksize
);
151 blkdev_get_block(struct inode
*inode
, sector_t iblock
,
152 struct buffer_head
*bh
, int create
)
154 bh
->b_bdev
= I_BDEV(inode
);
155 bh
->b_blocknr
= iblock
;
156 set_buffer_mapped(bh
);
160 static struct inode
*bdev_file_inode(struct file
*file
)
162 return file
->f_mapping
->host
;
166 blkdev_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
168 struct file
*file
= iocb
->ki_filp
;
169 struct inode
*inode
= bdev_file_inode(file
);
172 return dax_do_io(iocb
, inode
, iter
, blkdev_get_block
,
173 NULL
, DIO_SKIP_DIO_COUNT
);
174 return __blockdev_direct_IO(iocb
, inode
, I_BDEV(inode
), iter
,
175 blkdev_get_block
, NULL
, NULL
,
179 int __sync_blockdev(struct block_device
*bdev
, int wait
)
184 return filemap_flush(bdev
->bd_inode
->i_mapping
);
185 return filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
189 * Write out and wait upon all the dirty data associated with a block
190 * device via its mapping. Does not take the superblock lock.
192 int sync_blockdev(struct block_device
*bdev
)
194 return __sync_blockdev(bdev
, 1);
196 EXPORT_SYMBOL(sync_blockdev
);
199 * Write out and wait upon all dirty data associated with this
200 * device. Filesystem data as well as the underlying block
201 * device. Takes the superblock lock.
203 int fsync_bdev(struct block_device
*bdev
)
205 struct super_block
*sb
= get_super(bdev
);
207 int res
= sync_filesystem(sb
);
211 return sync_blockdev(bdev
);
213 EXPORT_SYMBOL(fsync_bdev
);
216 * freeze_bdev -- lock a filesystem and force it into a consistent state
217 * @bdev: blockdevice to lock
219 * If a superblock is found on this device, we take the s_umount semaphore
220 * on it to make sure nobody unmounts until the snapshot creation is done.
221 * The reference counter (bd_fsfreeze_count) guarantees that only the last
222 * unfreeze process can unfreeze the frozen filesystem actually when multiple
223 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
224 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
227 struct super_block
*freeze_bdev(struct block_device
*bdev
)
229 struct super_block
*sb
;
232 mutex_lock(&bdev
->bd_fsfreeze_mutex
);
233 if (++bdev
->bd_fsfreeze_count
> 1) {
235 * We don't even need to grab a reference - the first call
236 * to freeze_bdev grab an active reference and only the last
237 * thaw_bdev drops it.
239 sb
= get_super(bdev
);
241 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
245 sb
= get_active_super(bdev
);
248 if (sb
->s_op
->freeze_super
)
249 error
= sb
->s_op
->freeze_super(sb
);
251 error
= freeze_super(sb
);
253 deactivate_super(sb
);
254 bdev
->bd_fsfreeze_count
--;
255 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
256 return ERR_PTR(error
);
258 deactivate_super(sb
);
261 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
262 return sb
; /* thaw_bdev releases s->s_umount */
264 EXPORT_SYMBOL(freeze_bdev
);
267 * thaw_bdev -- unlock filesystem
268 * @bdev: blockdevice to unlock
269 * @sb: associated superblock
271 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
273 int thaw_bdev(struct block_device
*bdev
, struct super_block
*sb
)
277 mutex_lock(&bdev
->bd_fsfreeze_mutex
);
278 if (!bdev
->bd_fsfreeze_count
)
282 if (--bdev
->bd_fsfreeze_count
> 0)
288 if (sb
->s_op
->thaw_super
)
289 error
= sb
->s_op
->thaw_super(sb
);
291 error
= thaw_super(sb
);
293 bdev
->bd_fsfreeze_count
++;
294 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
298 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
301 EXPORT_SYMBOL(thaw_bdev
);
303 static int blkdev_writepage(struct page
*page
, struct writeback_control
*wbc
)
305 return block_write_full_page(page
, blkdev_get_block
, wbc
);
308 static int blkdev_readpage(struct file
* file
, struct page
* page
)
310 return block_read_full_page(page
, blkdev_get_block
);
313 static int blkdev_readpages(struct file
*file
, struct address_space
*mapping
,
314 struct list_head
*pages
, unsigned nr_pages
)
316 return mpage_readpages(mapping
, pages
, nr_pages
, blkdev_get_block
);
319 static int blkdev_write_begin(struct file
*file
, struct address_space
*mapping
,
320 loff_t pos
, unsigned len
, unsigned flags
,
321 struct page
**pagep
, void **fsdata
)
323 return block_write_begin(mapping
, pos
, len
, flags
, pagep
,
327 static int blkdev_write_end(struct file
*file
, struct address_space
*mapping
,
328 loff_t pos
, unsigned len
, unsigned copied
,
329 struct page
*page
, void *fsdata
)
332 ret
= block_write_end(file
, mapping
, pos
, len
, copied
, page
, fsdata
);
342 * for a block special file file_inode(file)->i_size is zero
343 * so we compute the size by hand (just as in block_read/write above)
345 static loff_t
block_llseek(struct file
*file
, loff_t offset
, int whence
)
347 struct inode
*bd_inode
= bdev_file_inode(file
);
350 inode_lock(bd_inode
);
351 retval
= fixed_size_llseek(file
, offset
, whence
, i_size_read(bd_inode
));
352 inode_unlock(bd_inode
);
356 int blkdev_fsync(struct file
*filp
, loff_t start
, loff_t end
, int datasync
)
358 struct inode
*bd_inode
= bdev_file_inode(filp
);
359 struct block_device
*bdev
= I_BDEV(bd_inode
);
362 error
= filemap_write_and_wait_range(filp
->f_mapping
, start
, end
);
367 * There is no need to serialise calls to blkdev_issue_flush with
368 * i_mutex and doing so causes performance issues with concurrent
369 * O_SYNC writers to a block device.
371 error
= blkdev_issue_flush(bdev
, GFP_KERNEL
, NULL
);
372 if (error
== -EOPNOTSUPP
)
377 EXPORT_SYMBOL(blkdev_fsync
);
380 * bdev_read_page() - Start reading a page from a block device
381 * @bdev: The device to read the page from
382 * @sector: The offset on the device to read the page to (need not be aligned)
383 * @page: The page to read
385 * On entry, the page should be locked. It will be unlocked when the page
386 * has been read. If the block driver implements rw_page synchronously,
387 * that will be true on exit from this function, but it need not be.
389 * Errors returned by this function are usually "soft", eg out of memory, or
390 * queue full; callers should try a different route to read this page rather
391 * than propagate an error back up the stack.
393 * Return: negative errno if an error occurs, 0 if submission was successful.
395 int bdev_read_page(struct block_device
*bdev
, sector_t sector
,
398 const struct block_device_operations
*ops
= bdev
->bd_disk
->fops
;
399 int result
= -EOPNOTSUPP
;
401 if (!ops
->rw_page
|| bdev_get_integrity(bdev
))
404 result
= blk_queue_enter(bdev
->bd_queue
, false);
407 result
= ops
->rw_page(bdev
, sector
+ get_start_sect(bdev
), page
, READ
);
408 blk_queue_exit(bdev
->bd_queue
);
411 EXPORT_SYMBOL_GPL(bdev_read_page
);
414 * bdev_write_page() - Start writing a page to a block device
415 * @bdev: The device to write the page to
416 * @sector: The offset on the device to write the page to (need not be aligned)
417 * @page: The page to write
418 * @wbc: The writeback_control for the write
420 * On entry, the page should be locked and not currently under writeback.
421 * On exit, if the write started successfully, the page will be unlocked and
422 * under writeback. If the write failed already (eg the driver failed to
423 * queue the page to the device), the page will still be locked. If the
424 * caller is a ->writepage implementation, it will need to unlock the page.
426 * Errors returned by this function are usually "soft", eg out of memory, or
427 * queue full; callers should try a different route to write this page rather
428 * than propagate an error back up the stack.
430 * Return: negative errno if an error occurs, 0 if submission was successful.
432 int bdev_write_page(struct block_device
*bdev
, sector_t sector
,
433 struct page
*page
, struct writeback_control
*wbc
)
436 int rw
= (wbc
->sync_mode
== WB_SYNC_ALL
) ? WRITE_SYNC
: WRITE
;
437 const struct block_device_operations
*ops
= bdev
->bd_disk
->fops
;
439 if (!ops
->rw_page
|| bdev_get_integrity(bdev
))
441 result
= blk_queue_enter(bdev
->bd_queue
, false);
445 set_page_writeback(page
);
446 result
= ops
->rw_page(bdev
, sector
+ get_start_sect(bdev
), page
, rw
);
448 end_page_writeback(page
);
451 blk_queue_exit(bdev
->bd_queue
);
454 EXPORT_SYMBOL_GPL(bdev_write_page
);
457 * bdev_direct_access() - Get the address for directly-accessibly memory
458 * @bdev: The device containing the memory
459 * @dax: control and output parameters for ->direct_access
461 * If a block device is made up of directly addressable memory, this function
462 * will tell the caller the PFN and the address of the memory. The address
463 * may be directly dereferenced within the kernel without the need to call
464 * ioremap(), kmap() or similar. The PFN is suitable for inserting into
467 * Return: negative errno if an error occurs, otherwise the number of bytes
468 * accessible at this address.
470 long bdev_direct_access(struct block_device
*bdev
, struct blk_dax_ctl
*dax
)
472 sector_t sector
= dax
->sector
;
473 long avail
, size
= dax
->size
;
474 const struct block_device_operations
*ops
= bdev
->bd_disk
->fops
;
477 * The device driver is allowed to sleep, in order to make the
478 * memory directly accessible.
484 if (!ops
->direct_access
)
486 if ((sector
+ DIV_ROUND_UP(size
, 512)) >
487 part_nr_sects_read(bdev
->bd_part
))
489 sector
+= get_start_sect(bdev
);
490 if (sector
% (PAGE_SIZE
/ 512))
492 avail
= ops
->direct_access(bdev
, sector
, &dax
->addr
, &dax
->pfn
);
495 if (avail
> 0 && avail
& ~PAGE_MASK
)
497 return min(avail
, size
);
499 EXPORT_SYMBOL_GPL(bdev_direct_access
);
505 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(bdev_lock
);
506 static struct kmem_cache
* bdev_cachep __read_mostly
;
508 static struct inode
*bdev_alloc_inode(struct super_block
*sb
)
510 struct bdev_inode
*ei
= kmem_cache_alloc(bdev_cachep
, GFP_KERNEL
);
513 return &ei
->vfs_inode
;
516 static void bdev_i_callback(struct rcu_head
*head
)
518 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
519 struct bdev_inode
*bdi
= BDEV_I(inode
);
521 kmem_cache_free(bdev_cachep
, bdi
);
524 static void bdev_destroy_inode(struct inode
*inode
)
526 call_rcu(&inode
->i_rcu
, bdev_i_callback
);
529 static void init_once(void *foo
)
531 struct bdev_inode
*ei
= (struct bdev_inode
*) foo
;
532 struct block_device
*bdev
= &ei
->bdev
;
534 memset(bdev
, 0, sizeof(*bdev
));
535 mutex_init(&bdev
->bd_mutex
);
536 INIT_LIST_HEAD(&bdev
->bd_inodes
);
537 INIT_LIST_HEAD(&bdev
->bd_list
);
539 INIT_LIST_HEAD(&bdev
->bd_holder_disks
);
541 inode_init_once(&ei
->vfs_inode
);
542 /* Initialize mutex for freeze. */
543 mutex_init(&bdev
->bd_fsfreeze_mutex
);
546 static inline void __bd_forget(struct inode
*inode
)
548 list_del_init(&inode
->i_devices
);
549 inode
->i_bdev
= NULL
;
550 inode
->i_mapping
= &inode
->i_data
;
553 static void bdev_evict_inode(struct inode
*inode
)
555 struct block_device
*bdev
= &BDEV_I(inode
)->bdev
;
557 truncate_inode_pages_final(&inode
->i_data
);
558 invalidate_inode_buffers(inode
); /* is it needed here? */
560 spin_lock(&bdev_lock
);
561 while ( (p
= bdev
->bd_inodes
.next
) != &bdev
->bd_inodes
) {
562 __bd_forget(list_entry(p
, struct inode
, i_devices
));
564 list_del_init(&bdev
->bd_list
);
565 spin_unlock(&bdev_lock
);
568 static const struct super_operations bdev_sops
= {
569 .statfs
= simple_statfs
,
570 .alloc_inode
= bdev_alloc_inode
,
571 .destroy_inode
= bdev_destroy_inode
,
572 .drop_inode
= generic_delete_inode
,
573 .evict_inode
= bdev_evict_inode
,
576 static struct dentry
*bd_mount(struct file_system_type
*fs_type
,
577 int flags
, const char *dev_name
, void *data
)
580 dent
= mount_pseudo(fs_type
, "bdev:", &bdev_sops
, NULL
, BDEVFS_MAGIC
);
582 dent
->d_sb
->s_iflags
|= SB_I_CGROUPWB
;
586 static struct file_system_type bd_type
= {
589 .kill_sb
= kill_anon_super
,
592 struct super_block
*blockdev_superblock __read_mostly
;
593 EXPORT_SYMBOL_GPL(blockdev_superblock
);
595 void __init
bdev_cache_init(void)
598 static struct vfsmount
*bd_mnt
;
600 bdev_cachep
= kmem_cache_create("bdev_cache", sizeof(struct bdev_inode
),
601 0, (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
602 SLAB_MEM_SPREAD
|SLAB_ACCOUNT
|SLAB_PANIC
),
604 err
= register_filesystem(&bd_type
);
606 panic("Cannot register bdev pseudo-fs");
607 bd_mnt
= kern_mount(&bd_type
);
609 panic("Cannot create bdev pseudo-fs");
610 blockdev_superblock
= bd_mnt
->mnt_sb
; /* For writeback */
614 * Most likely _very_ bad one - but then it's hardly critical for small
615 * /dev and can be fixed when somebody will need really large one.
616 * Keep in mind that it will be fed through icache hash function too.
618 static inline unsigned long hash(dev_t dev
)
620 return MAJOR(dev
)+MINOR(dev
);
623 static int bdev_test(struct inode
*inode
, void *data
)
625 return BDEV_I(inode
)->bdev
.bd_dev
== *(dev_t
*)data
;
628 static int bdev_set(struct inode
*inode
, void *data
)
630 BDEV_I(inode
)->bdev
.bd_dev
= *(dev_t
*)data
;
634 static LIST_HEAD(all_bdevs
);
636 struct block_device
*bdget(dev_t dev
)
638 struct block_device
*bdev
;
641 inode
= iget5_locked(blockdev_superblock
, hash(dev
),
642 bdev_test
, bdev_set
, &dev
);
647 bdev
= &BDEV_I(inode
)->bdev
;
649 if (inode
->i_state
& I_NEW
) {
650 bdev
->bd_contains
= NULL
;
651 bdev
->bd_super
= NULL
;
652 bdev
->bd_inode
= inode
;
653 bdev
->bd_block_size
= (1 << inode
->i_blkbits
);
654 bdev
->bd_part_count
= 0;
655 bdev
->bd_invalidated
= 0;
656 inode
->i_mode
= S_IFBLK
;
658 inode
->i_bdev
= bdev
;
659 inode
->i_data
.a_ops
= &def_blk_aops
;
660 mapping_set_gfp_mask(&inode
->i_data
, GFP_USER
);
661 spin_lock(&bdev_lock
);
662 list_add(&bdev
->bd_list
, &all_bdevs
);
663 spin_unlock(&bdev_lock
);
664 unlock_new_inode(inode
);
669 EXPORT_SYMBOL(bdget
);
672 * bdgrab -- Grab a reference to an already referenced block device
673 * @bdev: Block device to grab a reference to.
675 struct block_device
*bdgrab(struct block_device
*bdev
)
677 ihold(bdev
->bd_inode
);
680 EXPORT_SYMBOL(bdgrab
);
682 long nr_blockdev_pages(void)
684 struct block_device
*bdev
;
686 spin_lock(&bdev_lock
);
687 list_for_each_entry(bdev
, &all_bdevs
, bd_list
) {
688 ret
+= bdev
->bd_inode
->i_mapping
->nrpages
;
690 spin_unlock(&bdev_lock
);
694 void bdput(struct block_device
*bdev
)
696 iput(bdev
->bd_inode
);
699 EXPORT_SYMBOL(bdput
);
701 static struct block_device
*bd_acquire(struct inode
*inode
)
703 struct block_device
*bdev
;
705 spin_lock(&bdev_lock
);
706 bdev
= inode
->i_bdev
;
709 spin_unlock(&bdev_lock
);
712 spin_unlock(&bdev_lock
);
714 bdev
= bdget(inode
->i_rdev
);
716 spin_lock(&bdev_lock
);
717 if (!inode
->i_bdev
) {
719 * We take an additional reference to bd_inode,
720 * and it's released in clear_inode() of inode.
721 * So, we can access it via ->i_mapping always
725 inode
->i_bdev
= bdev
;
726 inode
->i_mapping
= bdev
->bd_inode
->i_mapping
;
727 list_add(&inode
->i_devices
, &bdev
->bd_inodes
);
729 spin_unlock(&bdev_lock
);
734 /* Call when you free inode */
736 void bd_forget(struct inode
*inode
)
738 struct block_device
*bdev
= NULL
;
740 spin_lock(&bdev_lock
);
741 if (!sb_is_blkdev_sb(inode
->i_sb
))
742 bdev
= inode
->i_bdev
;
744 spin_unlock(&bdev_lock
);
751 * bd_may_claim - test whether a block device can be claimed
752 * @bdev: block device of interest
753 * @whole: whole block device containing @bdev, may equal @bdev
754 * @holder: holder trying to claim @bdev
756 * Test whether @bdev can be claimed by @holder.
759 * spin_lock(&bdev_lock).
762 * %true if @bdev can be claimed, %false otherwise.
764 static bool bd_may_claim(struct block_device
*bdev
, struct block_device
*whole
,
767 if (bdev
->bd_holder
== holder
)
768 return true; /* already a holder */
769 else if (bdev
->bd_holder
!= NULL
)
770 return false; /* held by someone else */
771 else if (bdev
->bd_contains
== bdev
)
772 return true; /* is a whole device which isn't held */
774 else if (whole
->bd_holder
== bd_may_claim
)
775 return true; /* is a partition of a device that is being partitioned */
776 else if (whole
->bd_holder
!= NULL
)
777 return false; /* is a partition of a held device */
779 return true; /* is a partition of an un-held device */
783 * bd_prepare_to_claim - prepare to claim a block device
784 * @bdev: block device of interest
785 * @whole: the whole device containing @bdev, may equal @bdev
786 * @holder: holder trying to claim @bdev
788 * Prepare to claim @bdev. This function fails if @bdev is already
789 * claimed by another holder and waits if another claiming is in
790 * progress. This function doesn't actually claim. On successful
791 * return, the caller has ownership of bd_claiming and bd_holder[s].
794 * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
798 * 0 if @bdev can be claimed, -EBUSY otherwise.
800 static int bd_prepare_to_claim(struct block_device
*bdev
,
801 struct block_device
*whole
, void *holder
)
804 /* if someone else claimed, fail */
805 if (!bd_may_claim(bdev
, whole
, holder
))
808 /* if claiming is already in progress, wait for it to finish */
809 if (whole
->bd_claiming
) {
810 wait_queue_head_t
*wq
= bit_waitqueue(&whole
->bd_claiming
, 0);
813 prepare_to_wait(wq
, &wait
, TASK_UNINTERRUPTIBLE
);
814 spin_unlock(&bdev_lock
);
816 finish_wait(wq
, &wait
);
817 spin_lock(&bdev_lock
);
826 * bd_start_claiming - start claiming a block device
827 * @bdev: block device of interest
828 * @holder: holder trying to claim @bdev
830 * @bdev is about to be opened exclusively. Check @bdev can be opened
831 * exclusively and mark that an exclusive open is in progress. Each
832 * successful call to this function must be matched with a call to
833 * either bd_finish_claiming() or bd_abort_claiming() (which do not
836 * This function is used to gain exclusive access to the block device
837 * without actually causing other exclusive open attempts to fail. It
838 * should be used when the open sequence itself requires exclusive
839 * access but may subsequently fail.
845 * Pointer to the block device containing @bdev on success, ERR_PTR()
848 static struct block_device
*bd_start_claiming(struct block_device
*bdev
,
851 struct gendisk
*disk
;
852 struct block_device
*whole
;
858 * @bdev might not have been initialized properly yet, look up
859 * and grab the outer block device the hard way.
861 disk
= get_gendisk(bdev
->bd_dev
, &partno
);
863 return ERR_PTR(-ENXIO
);
866 * Normally, @bdev should equal what's returned from bdget_disk()
867 * if partno is 0; however, some drivers (floppy) use multiple
868 * bdev's for the same physical device and @bdev may be one of the
869 * aliases. Keep @bdev if partno is 0. This means claimer
870 * tracking is broken for those devices but it has always been that
874 whole
= bdget_disk(disk
, 0);
876 whole
= bdgrab(bdev
);
878 module_put(disk
->fops
->owner
);
881 return ERR_PTR(-ENOMEM
);
883 /* prepare to claim, if successful, mark claiming in progress */
884 spin_lock(&bdev_lock
);
886 err
= bd_prepare_to_claim(bdev
, whole
, holder
);
888 whole
->bd_claiming
= holder
;
889 spin_unlock(&bdev_lock
);
892 spin_unlock(&bdev_lock
);
899 struct bd_holder_disk
{
900 struct list_head list
;
901 struct gendisk
*disk
;
905 static struct bd_holder_disk
*bd_find_holder_disk(struct block_device
*bdev
,
906 struct gendisk
*disk
)
908 struct bd_holder_disk
*holder
;
910 list_for_each_entry(holder
, &bdev
->bd_holder_disks
, list
)
911 if (holder
->disk
== disk
)
916 static int add_symlink(struct kobject
*from
, struct kobject
*to
)
918 return sysfs_create_link(from
, to
, kobject_name(to
));
921 static void del_symlink(struct kobject
*from
, struct kobject
*to
)
923 sysfs_remove_link(from
, kobject_name(to
));
927 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
928 * @bdev: the claimed slave bdev
929 * @disk: the holding disk
931 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
933 * This functions creates the following sysfs symlinks.
935 * - from "slaves" directory of the holder @disk to the claimed @bdev
936 * - from "holders" directory of the @bdev to the holder @disk
938 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
939 * passed to bd_link_disk_holder(), then:
941 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
942 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
944 * The caller must have claimed @bdev before calling this function and
945 * ensure that both @bdev and @disk are valid during the creation and
946 * lifetime of these symlinks.
952 * 0 on success, -errno on failure.
954 int bd_link_disk_holder(struct block_device
*bdev
, struct gendisk
*disk
)
956 struct bd_holder_disk
*holder
;
959 mutex_lock(&bdev
->bd_mutex
);
961 WARN_ON_ONCE(!bdev
->bd_holder
);
963 /* FIXME: remove the following once add_disk() handles errors */
964 if (WARN_ON(!disk
->slave_dir
|| !bdev
->bd_part
->holder_dir
))
967 holder
= bd_find_holder_disk(bdev
, disk
);
973 holder
= kzalloc(sizeof(*holder
), GFP_KERNEL
);
979 INIT_LIST_HEAD(&holder
->list
);
983 ret
= add_symlink(disk
->slave_dir
, &part_to_dev(bdev
->bd_part
)->kobj
);
987 ret
= add_symlink(bdev
->bd_part
->holder_dir
, &disk_to_dev(disk
)->kobj
);
991 * bdev could be deleted beneath us which would implicitly destroy
992 * the holder directory. Hold on to it.
994 kobject_get(bdev
->bd_part
->holder_dir
);
996 list_add(&holder
->list
, &bdev
->bd_holder_disks
);
1000 del_symlink(disk
->slave_dir
, &part_to_dev(bdev
->bd_part
)->kobj
);
1004 mutex_unlock(&bdev
->bd_mutex
);
1007 EXPORT_SYMBOL_GPL(bd_link_disk_holder
);
1010 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
1011 * @bdev: the calimed slave bdev
1012 * @disk: the holding disk
1014 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1019 void bd_unlink_disk_holder(struct block_device
*bdev
, struct gendisk
*disk
)
1021 struct bd_holder_disk
*holder
;
1023 mutex_lock(&bdev
->bd_mutex
);
1025 holder
= bd_find_holder_disk(bdev
, disk
);
1027 if (!WARN_ON_ONCE(holder
== NULL
) && !--holder
->refcnt
) {
1028 del_symlink(disk
->slave_dir
, &part_to_dev(bdev
->bd_part
)->kobj
);
1029 del_symlink(bdev
->bd_part
->holder_dir
,
1030 &disk_to_dev(disk
)->kobj
);
1031 kobject_put(bdev
->bd_part
->holder_dir
);
1032 list_del_init(&holder
->list
);
1036 mutex_unlock(&bdev
->bd_mutex
);
1038 EXPORT_SYMBOL_GPL(bd_unlink_disk_holder
);
1042 * flush_disk - invalidates all buffer-cache entries on a disk
1044 * @bdev: struct block device to be flushed
1045 * @kill_dirty: flag to guide handling of dirty inodes
1047 * Invalidates all buffer-cache entries on a disk. It should be called
1048 * when a disk has been changed -- either by a media change or online
1051 static void flush_disk(struct block_device
*bdev
, bool kill_dirty
)
1053 if (__invalidate_device(bdev
, kill_dirty
)) {
1054 printk(KERN_WARNING
"VFS: busy inodes on changed media or "
1055 "resized disk %s\n",
1056 bdev
->bd_disk
? bdev
->bd_disk
->disk_name
: "");
1061 if (disk_part_scan_enabled(bdev
->bd_disk
))
1062 bdev
->bd_invalidated
= 1;
1066 * check_disk_size_change - checks for disk size change and adjusts bdev size.
1067 * @disk: struct gendisk to check
1068 * @bdev: struct bdev to adjust.
1070 * This routine checks to see if the bdev size does not match the disk size
1071 * and adjusts it if it differs.
1073 void check_disk_size_change(struct gendisk
*disk
, struct block_device
*bdev
)
1075 loff_t disk_size
, bdev_size
;
1077 disk_size
= (loff_t
)get_capacity(disk
) << 9;
1078 bdev_size
= i_size_read(bdev
->bd_inode
);
1079 if (disk_size
!= bdev_size
) {
1081 "%s: detected capacity change from %lld to %lld\n",
1082 disk
->disk_name
, bdev_size
, disk_size
);
1083 i_size_write(bdev
->bd_inode
, disk_size
);
1084 flush_disk(bdev
, false);
1087 EXPORT_SYMBOL(check_disk_size_change
);
1090 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
1091 * @disk: struct gendisk to be revalidated
1093 * This routine is a wrapper for lower-level driver's revalidate_disk
1094 * call-backs. It is used to do common pre and post operations needed
1095 * for all revalidate_disk operations.
1097 int revalidate_disk(struct gendisk
*disk
)
1099 struct block_device
*bdev
;
1102 if (disk
->fops
->revalidate_disk
)
1103 ret
= disk
->fops
->revalidate_disk(disk
);
1104 blk_integrity_revalidate(disk
);
1105 bdev
= bdget_disk(disk
, 0);
1109 mutex_lock(&bdev
->bd_mutex
);
1110 check_disk_size_change(disk
, bdev
);
1111 bdev
->bd_invalidated
= 0;
1112 mutex_unlock(&bdev
->bd_mutex
);
1116 EXPORT_SYMBOL(revalidate_disk
);
1119 * This routine checks whether a removable media has been changed,
1120 * and invalidates all buffer-cache-entries in that case. This
1121 * is a relatively slow routine, so we have to try to minimize using
1122 * it. Thus it is called only upon a 'mount' or 'open'. This
1123 * is the best way of combining speed and utility, I think.
1124 * People changing diskettes in the middle of an operation deserve
1127 int check_disk_change(struct block_device
*bdev
)
1129 struct gendisk
*disk
= bdev
->bd_disk
;
1130 const struct block_device_operations
*bdops
= disk
->fops
;
1131 unsigned int events
;
1133 events
= disk_clear_events(disk
, DISK_EVENT_MEDIA_CHANGE
|
1134 DISK_EVENT_EJECT_REQUEST
);
1135 if (!(events
& DISK_EVENT_MEDIA_CHANGE
))
1138 flush_disk(bdev
, true);
1139 if (bdops
->revalidate_disk
)
1140 bdops
->revalidate_disk(bdev
->bd_disk
);
1144 EXPORT_SYMBOL(check_disk_change
);
1146 void bd_set_size(struct block_device
*bdev
, loff_t size
)
1148 unsigned bsize
= bdev_logical_block_size(bdev
);
1150 inode_lock(bdev
->bd_inode
);
1151 i_size_write(bdev
->bd_inode
, size
);
1152 inode_unlock(bdev
->bd_inode
);
1153 while (bsize
< PAGE_SIZE
) {
1158 bdev
->bd_block_size
= bsize
;
1159 bdev
->bd_inode
->i_blkbits
= blksize_bits(bsize
);
1161 EXPORT_SYMBOL(bd_set_size
);
1163 static bool blkdev_dax_capable(struct block_device
*bdev
)
1165 struct gendisk
*disk
= bdev
->bd_disk
;
1167 if (!disk
->fops
->direct_access
|| !IS_ENABLED(CONFIG_FS_DAX
))
1171 * If the partition is not aligned on a page boundary, we can't
1174 if ((bdev
->bd_part
->start_sect
% (PAGE_SIZE
/ 512))
1175 || (bdev
->bd_part
->nr_sects
% (PAGE_SIZE
/ 512)))
1179 * If the device has known bad blocks, force all I/O through the
1180 * driver / page cache.
1182 * TODO: support finer grained dax error handling
1184 if (disk
->bb
&& disk
->bb
->count
)
1190 static void __blkdev_put(struct block_device
*bdev
, fmode_t mode
, int for_part
);
1195 * mutex_lock(part->bd_mutex)
1196 * mutex_lock_nested(whole->bd_mutex, 1)
1199 static int __blkdev_get(struct block_device
*bdev
, fmode_t mode
, int for_part
)
1201 struct gendisk
*disk
;
1202 struct module
*owner
;
1207 if (mode
& FMODE_READ
)
1209 if (mode
& FMODE_WRITE
)
1212 * hooks: /n/, see "layering violations".
1215 ret
= devcgroup_inode_permission(bdev
->bd_inode
, perm
);
1225 disk
= get_gendisk(bdev
->bd_dev
, &partno
);
1228 owner
= disk
->fops
->owner
;
1230 disk_block_events(disk
);
1231 mutex_lock_nested(&bdev
->bd_mutex
, for_part
);
1232 if (!bdev
->bd_openers
) {
1233 bdev
->bd_disk
= disk
;
1234 bdev
->bd_queue
= disk
->queue
;
1235 bdev
->bd_contains
= bdev
;
1236 if (IS_ENABLED(CONFIG_BLK_DEV_DAX
) && disk
->fops
->direct_access
)
1237 bdev
->bd_inode
->i_flags
= S_DAX
;
1239 bdev
->bd_inode
->i_flags
= 0;
1243 bdev
->bd_part
= disk_get_part(disk
, partno
);
1248 if (disk
->fops
->open
) {
1249 ret
= disk
->fops
->open(bdev
, mode
);
1250 if (ret
== -ERESTARTSYS
) {
1251 /* Lost a race with 'disk' being
1252 * deleted, try again.
1255 disk_put_part(bdev
->bd_part
);
1256 bdev
->bd_part
= NULL
;
1257 bdev
->bd_disk
= NULL
;
1258 bdev
->bd_queue
= NULL
;
1259 mutex_unlock(&bdev
->bd_mutex
);
1260 disk_unblock_events(disk
);
1268 bd_set_size(bdev
,(loff_t
)get_capacity(disk
)<<9);
1269 if (!blkdev_dax_capable(bdev
))
1270 bdev
->bd_inode
->i_flags
&= ~S_DAX
;
1274 * If the device is invalidated, rescan partition
1275 * if open succeeded or failed with -ENOMEDIUM.
1276 * The latter is necessary to prevent ghost
1277 * partitions on a removed medium.
1279 if (bdev
->bd_invalidated
) {
1281 rescan_partitions(disk
, bdev
);
1282 else if (ret
== -ENOMEDIUM
)
1283 invalidate_partitions(disk
, bdev
);
1289 struct block_device
*whole
;
1290 whole
= bdget_disk(disk
, 0);
1295 ret
= __blkdev_get(whole
, mode
, 1);
1298 bdev
->bd_contains
= whole
;
1299 bdev
->bd_part
= disk_get_part(disk
, partno
);
1300 if (!(disk
->flags
& GENHD_FL_UP
) ||
1301 !bdev
->bd_part
|| !bdev
->bd_part
->nr_sects
) {
1305 bd_set_size(bdev
, (loff_t
)bdev
->bd_part
->nr_sects
<< 9);
1306 if (!blkdev_dax_capable(bdev
))
1307 bdev
->bd_inode
->i_flags
&= ~S_DAX
;
1310 if (bdev
->bd_contains
== bdev
) {
1312 if (bdev
->bd_disk
->fops
->open
)
1313 ret
= bdev
->bd_disk
->fops
->open(bdev
, mode
);
1314 /* the same as first opener case, read comment there */
1315 if (bdev
->bd_invalidated
) {
1317 rescan_partitions(bdev
->bd_disk
, bdev
);
1318 else if (ret
== -ENOMEDIUM
)
1319 invalidate_partitions(bdev
->bd_disk
, bdev
);
1322 goto out_unlock_bdev
;
1324 /* only one opener holds refs to the module and disk */
1330 bdev
->bd_part_count
++;
1331 mutex_unlock(&bdev
->bd_mutex
);
1332 disk_unblock_events(disk
);
1336 disk_put_part(bdev
->bd_part
);
1337 bdev
->bd_disk
= NULL
;
1338 bdev
->bd_part
= NULL
;
1339 bdev
->bd_queue
= NULL
;
1340 if (bdev
!= bdev
->bd_contains
)
1341 __blkdev_put(bdev
->bd_contains
, mode
, 1);
1342 bdev
->bd_contains
= NULL
;
1344 mutex_unlock(&bdev
->bd_mutex
);
1345 disk_unblock_events(disk
);
1355 * blkdev_get - open a block device
1356 * @bdev: block_device to open
1357 * @mode: FMODE_* mask
1358 * @holder: exclusive holder identifier
1360 * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
1361 * open with exclusive access. Specifying %FMODE_EXCL with %NULL
1362 * @holder is invalid. Exclusive opens may nest for the same @holder.
1364 * On success, the reference count of @bdev is unchanged. On failure,
1371 * 0 on success, -errno on failure.
1373 int blkdev_get(struct block_device
*bdev
, fmode_t mode
, void *holder
)
1375 struct block_device
*whole
= NULL
;
1378 WARN_ON_ONCE((mode
& FMODE_EXCL
) && !holder
);
1380 if ((mode
& FMODE_EXCL
) && holder
) {
1381 whole
= bd_start_claiming(bdev
, holder
);
1382 if (IS_ERR(whole
)) {
1384 return PTR_ERR(whole
);
1388 res
= __blkdev_get(bdev
, mode
, 0);
1391 struct gendisk
*disk
= whole
->bd_disk
;
1393 /* finish claiming */
1394 mutex_lock(&bdev
->bd_mutex
);
1395 spin_lock(&bdev_lock
);
1398 BUG_ON(!bd_may_claim(bdev
, whole
, holder
));
1400 * Note that for a whole device bd_holders
1401 * will be incremented twice, and bd_holder
1402 * will be set to bd_may_claim before being
1405 whole
->bd_holders
++;
1406 whole
->bd_holder
= bd_may_claim
;
1408 bdev
->bd_holder
= holder
;
1411 /* tell others that we're done */
1412 BUG_ON(whole
->bd_claiming
!= holder
);
1413 whole
->bd_claiming
= NULL
;
1414 wake_up_bit(&whole
->bd_claiming
, 0);
1416 spin_unlock(&bdev_lock
);
1419 * Block event polling for write claims if requested. Any
1420 * write holder makes the write_holder state stick until
1421 * all are released. This is good enough and tracking
1422 * individual writeable reference is too fragile given the
1423 * way @mode is used in blkdev_get/put().
1425 if (!res
&& (mode
& FMODE_WRITE
) && !bdev
->bd_write_holder
&&
1426 (disk
->flags
& GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE
)) {
1427 bdev
->bd_write_holder
= true;
1428 disk_block_events(disk
);
1431 mutex_unlock(&bdev
->bd_mutex
);
1437 EXPORT_SYMBOL(blkdev_get
);
1440 * blkdev_get_by_path - open a block device by name
1441 * @path: path to the block device to open
1442 * @mode: FMODE_* mask
1443 * @holder: exclusive holder identifier
1445 * Open the blockdevice described by the device file at @path. @mode
1446 * and @holder are identical to blkdev_get().
1448 * On success, the returned block_device has reference count of one.
1454 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1456 struct block_device
*blkdev_get_by_path(const char *path
, fmode_t mode
,
1459 struct block_device
*bdev
;
1462 bdev
= lookup_bdev(path
);
1466 err
= blkdev_get(bdev
, mode
, holder
);
1468 return ERR_PTR(err
);
1470 if ((mode
& FMODE_WRITE
) && bdev_read_only(bdev
)) {
1471 blkdev_put(bdev
, mode
);
1472 return ERR_PTR(-EACCES
);
1477 EXPORT_SYMBOL(blkdev_get_by_path
);
1480 * blkdev_get_by_dev - open a block device by device number
1481 * @dev: device number of block device to open
1482 * @mode: FMODE_* mask
1483 * @holder: exclusive holder identifier
1485 * Open the blockdevice described by device number @dev. @mode and
1486 * @holder are identical to blkdev_get().
1488 * Use it ONLY if you really do not have anything better - i.e. when
1489 * you are behind a truly sucky interface and all you are given is a
1490 * device number. _Never_ to be used for internal purposes. If you
1491 * ever need it - reconsider your API.
1493 * On success, the returned block_device has reference count of one.
1499 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1501 struct block_device
*blkdev_get_by_dev(dev_t dev
, fmode_t mode
, void *holder
)
1503 struct block_device
*bdev
;
1508 return ERR_PTR(-ENOMEM
);
1510 err
= blkdev_get(bdev
, mode
, holder
);
1512 return ERR_PTR(err
);
1516 EXPORT_SYMBOL(blkdev_get_by_dev
);
1518 static int blkdev_open(struct inode
* inode
, struct file
* filp
)
1520 struct block_device
*bdev
;
1523 * Preserve backwards compatibility and allow large file access
1524 * even if userspace doesn't ask for it explicitly. Some mkfs
1525 * binary needs it. We might want to drop this workaround
1526 * during an unstable branch.
1528 filp
->f_flags
|= O_LARGEFILE
;
1530 if (filp
->f_flags
& O_NDELAY
)
1531 filp
->f_mode
|= FMODE_NDELAY
;
1532 if (filp
->f_flags
& O_EXCL
)
1533 filp
->f_mode
|= FMODE_EXCL
;
1534 if ((filp
->f_flags
& O_ACCMODE
) == 3)
1535 filp
->f_mode
|= FMODE_WRITE_IOCTL
;
1537 bdev
= bd_acquire(inode
);
1541 filp
->f_mapping
= bdev
->bd_inode
->i_mapping
;
1543 return blkdev_get(bdev
, filp
->f_mode
, filp
);
1546 static void __blkdev_put(struct block_device
*bdev
, fmode_t mode
, int for_part
)
1548 struct gendisk
*disk
= bdev
->bd_disk
;
1549 struct block_device
*victim
= NULL
;
1551 mutex_lock_nested(&bdev
->bd_mutex
, for_part
);
1553 bdev
->bd_part_count
--;
1555 if (!--bdev
->bd_openers
) {
1556 WARN_ON_ONCE(bdev
->bd_holders
);
1557 sync_blockdev(bdev
);
1560 bdev_write_inode(bdev
);
1562 * Detaching bdev inode from its wb in __destroy_inode()
1563 * is too late: the queue which embeds its bdi (along with
1564 * root wb) can be gone as soon as we put_disk() below.
1566 inode_detach_wb(bdev
->bd_inode
);
1568 if (bdev
->bd_contains
== bdev
) {
1569 if (disk
->fops
->release
)
1570 disk
->fops
->release(disk
, mode
);
1572 if (!bdev
->bd_openers
) {
1573 struct module
*owner
= disk
->fops
->owner
;
1575 disk_put_part(bdev
->bd_part
);
1576 bdev
->bd_part
= NULL
;
1577 bdev
->bd_disk
= NULL
;
1578 if (bdev
!= bdev
->bd_contains
)
1579 victim
= bdev
->bd_contains
;
1580 bdev
->bd_contains
= NULL
;
1585 mutex_unlock(&bdev
->bd_mutex
);
1588 __blkdev_put(victim
, mode
, 1);
1591 void blkdev_put(struct block_device
*bdev
, fmode_t mode
)
1593 mutex_lock(&bdev
->bd_mutex
);
1595 if (mode
& FMODE_EXCL
) {
1599 * Release a claim on the device. The holder fields
1600 * are protected with bdev_lock. bd_mutex is to
1601 * synchronize disk_holder unlinking.
1603 spin_lock(&bdev_lock
);
1605 WARN_ON_ONCE(--bdev
->bd_holders
< 0);
1606 WARN_ON_ONCE(--bdev
->bd_contains
->bd_holders
< 0);
1608 /* bd_contains might point to self, check in a separate step */
1609 if ((bdev_free
= !bdev
->bd_holders
))
1610 bdev
->bd_holder
= NULL
;
1611 if (!bdev
->bd_contains
->bd_holders
)
1612 bdev
->bd_contains
->bd_holder
= NULL
;
1614 spin_unlock(&bdev_lock
);
1617 * If this was the last claim, remove holder link and
1618 * unblock evpoll if it was a write holder.
1620 if (bdev_free
&& bdev
->bd_write_holder
) {
1621 disk_unblock_events(bdev
->bd_disk
);
1622 bdev
->bd_write_holder
= false;
1627 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
1628 * event. This is to ensure detection of media removal commanded
1629 * from userland - e.g. eject(1).
1631 disk_flush_events(bdev
->bd_disk
, DISK_EVENT_MEDIA_CHANGE
);
1633 mutex_unlock(&bdev
->bd_mutex
);
1635 __blkdev_put(bdev
, mode
, 0);
1637 EXPORT_SYMBOL(blkdev_put
);
1639 static int blkdev_close(struct inode
* inode
, struct file
* filp
)
1641 struct block_device
*bdev
= I_BDEV(bdev_file_inode(filp
));
1642 blkdev_put(bdev
, filp
->f_mode
);
1646 static long block_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
1648 struct block_device
*bdev
= I_BDEV(bdev_file_inode(file
));
1649 fmode_t mode
= file
->f_mode
;
1652 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1653 * to updated it before every ioctl.
1655 if (file
->f_flags
& O_NDELAY
)
1656 mode
|= FMODE_NDELAY
;
1658 mode
&= ~FMODE_NDELAY
;
1660 return blkdev_ioctl(bdev
, mode
, cmd
, arg
);
1664 * Write data to the block device. Only intended for the block device itself
1665 * and the raw driver which basically is a fake block device.
1667 * Does not take i_mutex for the write and thus is not for general purpose
1670 ssize_t
blkdev_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1672 struct file
*file
= iocb
->ki_filp
;
1673 struct inode
*bd_inode
= bdev_file_inode(file
);
1674 loff_t size
= i_size_read(bd_inode
);
1675 struct blk_plug plug
;
1678 if (bdev_read_only(I_BDEV(bd_inode
)))
1681 if (!iov_iter_count(from
))
1684 if (iocb
->ki_pos
>= size
)
1687 iov_iter_truncate(from
, size
- iocb
->ki_pos
);
1689 blk_start_plug(&plug
);
1690 ret
= __generic_file_write_iter(iocb
, from
);
1692 ret
= generic_write_sync(iocb
, ret
);
1693 blk_finish_plug(&plug
);
1696 EXPORT_SYMBOL_GPL(blkdev_write_iter
);
1698 ssize_t
blkdev_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1700 struct file
*file
= iocb
->ki_filp
;
1701 struct inode
*bd_inode
= bdev_file_inode(file
);
1702 loff_t size
= i_size_read(bd_inode
);
1703 loff_t pos
= iocb
->ki_pos
;
1709 iov_iter_truncate(to
, size
);
1710 return generic_file_read_iter(iocb
, to
);
1712 EXPORT_SYMBOL_GPL(blkdev_read_iter
);
1715 * Try to release a page associated with block device when the system
1716 * is under memory pressure.
1718 static int blkdev_releasepage(struct page
*page
, gfp_t wait
)
1720 struct super_block
*super
= BDEV_I(page
->mapping
->host
)->bdev
.bd_super
;
1722 if (super
&& super
->s_op
->bdev_try_to_free_page
)
1723 return super
->s_op
->bdev_try_to_free_page(super
, page
, wait
);
1725 return try_to_free_buffers(page
);
1728 static int blkdev_writepages(struct address_space
*mapping
,
1729 struct writeback_control
*wbc
)
1731 if (dax_mapping(mapping
)) {
1732 struct block_device
*bdev
= I_BDEV(mapping
->host
);
1734 return dax_writeback_mapping_range(mapping
, bdev
, wbc
);
1736 return generic_writepages(mapping
, wbc
);
1739 static const struct address_space_operations def_blk_aops
= {
1740 .readpage
= blkdev_readpage
,
1741 .readpages
= blkdev_readpages
,
1742 .writepage
= blkdev_writepage
,
1743 .write_begin
= blkdev_write_begin
,
1744 .write_end
= blkdev_write_end
,
1745 .writepages
= blkdev_writepages
,
1746 .releasepage
= blkdev_releasepage
,
1747 .direct_IO
= blkdev_direct_IO
,
1748 .is_dirty_writeback
= buffer_check_dirty_writeback
,
1751 const struct file_operations def_blk_fops
= {
1752 .open
= blkdev_open
,
1753 .release
= blkdev_close
,
1754 .llseek
= block_llseek
,
1755 .read_iter
= blkdev_read_iter
,
1756 .write_iter
= blkdev_write_iter
,
1757 .mmap
= generic_file_mmap
,
1758 .fsync
= blkdev_fsync
,
1759 .unlocked_ioctl
= block_ioctl
,
1760 #ifdef CONFIG_COMPAT
1761 .compat_ioctl
= compat_blkdev_ioctl
,
1763 .splice_read
= generic_file_splice_read
,
1764 .splice_write
= iter_file_splice_write
,
1767 int ioctl_by_bdev(struct block_device
*bdev
, unsigned cmd
, unsigned long arg
)
1770 mm_segment_t old_fs
= get_fs();
1772 res
= blkdev_ioctl(bdev
, 0, cmd
, arg
);
1777 EXPORT_SYMBOL(ioctl_by_bdev
);
1780 * lookup_bdev - lookup a struct block_device by name
1781 * @pathname: special file representing the block device
1783 * Get a reference to the blockdevice at @pathname in the current
1784 * namespace if possible and return it. Return ERR_PTR(error)
1787 struct block_device
*lookup_bdev(const char *pathname
)
1789 struct block_device
*bdev
;
1790 struct inode
*inode
;
1794 if (!pathname
|| !*pathname
)
1795 return ERR_PTR(-EINVAL
);
1797 error
= kern_path(pathname
, LOOKUP_FOLLOW
, &path
);
1799 return ERR_PTR(error
);
1801 inode
= d_backing_inode(path
.dentry
);
1803 if (!S_ISBLK(inode
->i_mode
))
1806 if (path
.mnt
->mnt_flags
& MNT_NODEV
)
1809 bdev
= bd_acquire(inode
);
1816 bdev
= ERR_PTR(error
);
1819 EXPORT_SYMBOL(lookup_bdev
);
1821 int __invalidate_device(struct block_device
*bdev
, bool kill_dirty
)
1823 struct super_block
*sb
= get_super(bdev
);
1828 * no need to lock the super, get_super holds the
1829 * read mutex so the filesystem cannot go away
1830 * under us (->put_super runs with the write lock
1833 shrink_dcache_sb(sb
);
1834 res
= invalidate_inodes(sb
, kill_dirty
);
1837 invalidate_bdev(bdev
);
1840 EXPORT_SYMBOL(__invalidate_device
);
1842 void iterate_bdevs(void (*func
)(struct block_device
*, void *), void *arg
)
1844 struct inode
*inode
, *old_inode
= NULL
;
1846 spin_lock(&blockdev_superblock
->s_inode_list_lock
);
1847 list_for_each_entry(inode
, &blockdev_superblock
->s_inodes
, i_sb_list
) {
1848 struct address_space
*mapping
= inode
->i_mapping
;
1850 spin_lock(&inode
->i_lock
);
1851 if (inode
->i_state
& (I_FREEING
|I_WILL_FREE
|I_NEW
) ||
1852 mapping
->nrpages
== 0) {
1853 spin_unlock(&inode
->i_lock
);
1857 spin_unlock(&inode
->i_lock
);
1858 spin_unlock(&blockdev_superblock
->s_inode_list_lock
);
1860 * We hold a reference to 'inode' so it couldn't have been
1861 * removed from s_inodes list while we dropped the
1862 * s_inode_list_lock We cannot iput the inode now as we can
1863 * be holding the last reference and we cannot iput it under
1864 * s_inode_list_lock. So we keep the reference and iput it
1870 func(I_BDEV(inode
), arg
);
1872 spin_lock(&blockdev_superblock
->s_inode_list_lock
);
1874 spin_unlock(&blockdev_superblock
->s_inode_list_lock
);