thinkpad_acpi: Correct !CONFIG_THINKPAD_ACPI_VIDEO warning
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / block_dev.c
blob59277ba8d2903fd8214278f0bc2243c7d282e6e1
1 /*
2 * linux/fs/block_dev.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
6 */
8 #include <linux/init.h>
9 #include <linux/mm.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/module.h>
18 #include <linux/blkpg.h>
19 #include <linux/buffer_head.h>
20 #include <linux/pagevec.h>
21 #include <linux/writeback.h>
22 #include <linux/mpage.h>
23 #include <linux/mount.h>
24 #include <linux/uio.h>
25 #include <linux/namei.h>
26 #include <linux/log2.h>
27 #include <linux/kmemleak.h>
28 #include <asm/uaccess.h>
29 #include "internal.h"
31 struct bdev_inode {
32 struct block_device bdev;
33 struct inode vfs_inode;
36 static const struct address_space_operations def_blk_aops;
38 static inline struct bdev_inode *BDEV_I(struct inode *inode)
40 return container_of(inode, struct bdev_inode, vfs_inode);
43 inline struct block_device *I_BDEV(struct inode *inode)
45 return &BDEV_I(inode)->bdev;
48 EXPORT_SYMBOL(I_BDEV);
51 * move the inode from it's current bdi to the a new bdi. if the inode is dirty
52 * we need to move it onto the dirty list of @dst so that the inode is always
53 * on the right list.
55 static void bdev_inode_switch_bdi(struct inode *inode,
56 struct backing_dev_info *dst)
58 spin_lock(&inode_lock);
59 inode->i_data.backing_dev_info = dst;
60 if (inode->i_state & I_DIRTY)
61 list_move(&inode->i_wb_list, &dst->wb.b_dirty);
62 spin_unlock(&inode_lock);
65 static sector_t max_block(struct block_device *bdev)
67 sector_t retval = ~((sector_t)0);
68 loff_t sz = i_size_read(bdev->bd_inode);
70 if (sz) {
71 unsigned int size = block_size(bdev);
72 unsigned int sizebits = blksize_bits(size);
73 retval = (sz >> sizebits);
75 return retval;
78 /* Kill _all_ buffers and pagecache , dirty or not.. */
79 static void kill_bdev(struct block_device *bdev)
81 if (bdev->bd_inode->i_mapping->nrpages == 0)
82 return;
83 invalidate_bh_lrus();
84 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
87 int set_blocksize(struct block_device *bdev, int size)
89 /* Size must be a power of two, and between 512 and PAGE_SIZE */
90 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
91 return -EINVAL;
93 /* Size cannot be smaller than the size supported by the device */
94 if (size < bdev_logical_block_size(bdev))
95 return -EINVAL;
97 /* Don't change the size if it is same as current */
98 if (bdev->bd_block_size != size) {
99 sync_blockdev(bdev);
100 bdev->bd_block_size = size;
101 bdev->bd_inode->i_blkbits = blksize_bits(size);
102 kill_bdev(bdev);
104 return 0;
107 EXPORT_SYMBOL(set_blocksize);
109 int sb_set_blocksize(struct super_block *sb, int size)
111 if (set_blocksize(sb->s_bdev, size))
112 return 0;
113 /* If we get here, we know size is power of two
114 * and it's value is between 512 and PAGE_SIZE */
115 sb->s_blocksize = size;
116 sb->s_blocksize_bits = blksize_bits(size);
117 return sb->s_blocksize;
120 EXPORT_SYMBOL(sb_set_blocksize);
122 int sb_min_blocksize(struct super_block *sb, int size)
124 int minsize = bdev_logical_block_size(sb->s_bdev);
125 if (size < minsize)
126 size = minsize;
127 return sb_set_blocksize(sb, size);
130 EXPORT_SYMBOL(sb_min_blocksize);
132 static int
133 blkdev_get_block(struct inode *inode, sector_t iblock,
134 struct buffer_head *bh, int create)
136 if (iblock >= max_block(I_BDEV(inode))) {
137 if (create)
138 return -EIO;
141 * for reads, we're just trying to fill a partial page.
142 * return a hole, they will have to call get_block again
143 * before they can fill it, and they will get -EIO at that
144 * time
146 return 0;
148 bh->b_bdev = I_BDEV(inode);
149 bh->b_blocknr = iblock;
150 set_buffer_mapped(bh);
151 return 0;
154 static int
155 blkdev_get_blocks(struct inode *inode, sector_t iblock,
156 struct buffer_head *bh, int create)
158 sector_t end_block = max_block(I_BDEV(inode));
159 unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
161 if ((iblock + max_blocks) > end_block) {
162 max_blocks = end_block - iblock;
163 if ((long)max_blocks <= 0) {
164 if (create)
165 return -EIO; /* write fully beyond EOF */
167 * It is a read which is fully beyond EOF. We return
168 * a !buffer_mapped buffer
170 max_blocks = 0;
174 bh->b_bdev = I_BDEV(inode);
175 bh->b_blocknr = iblock;
176 bh->b_size = max_blocks << inode->i_blkbits;
177 if (max_blocks)
178 set_buffer_mapped(bh);
179 return 0;
182 static ssize_t
183 blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
184 loff_t offset, unsigned long nr_segs)
186 struct file *file = iocb->ki_filp;
187 struct inode *inode = file->f_mapping->host;
189 return __blockdev_direct_IO(rw, iocb, inode, I_BDEV(inode), iov, offset,
190 nr_segs, blkdev_get_blocks, NULL, NULL, 0);
193 int __sync_blockdev(struct block_device *bdev, int wait)
195 if (!bdev)
196 return 0;
197 if (!wait)
198 return filemap_flush(bdev->bd_inode->i_mapping);
199 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
203 * Write out and wait upon all the dirty data associated with a block
204 * device via its mapping. Does not take the superblock lock.
206 int sync_blockdev(struct block_device *bdev)
208 return __sync_blockdev(bdev, 1);
210 EXPORT_SYMBOL(sync_blockdev);
213 * Write out and wait upon all dirty data associated with this
214 * device. Filesystem data as well as the underlying block
215 * device. Takes the superblock lock.
217 int fsync_bdev(struct block_device *bdev)
219 struct super_block *sb = get_super(bdev);
220 if (sb) {
221 int res = sync_filesystem(sb);
222 drop_super(sb);
223 return res;
225 return sync_blockdev(bdev);
227 EXPORT_SYMBOL(fsync_bdev);
230 * freeze_bdev -- lock a filesystem and force it into a consistent state
231 * @bdev: blockdevice to lock
233 * If a superblock is found on this device, we take the s_umount semaphore
234 * on it to make sure nobody unmounts until the snapshot creation is done.
235 * The reference counter (bd_fsfreeze_count) guarantees that only the last
236 * unfreeze process can unfreeze the frozen filesystem actually when multiple
237 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
238 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
239 * actually.
241 struct super_block *freeze_bdev(struct block_device *bdev)
243 struct super_block *sb;
244 int error = 0;
246 mutex_lock(&bdev->bd_fsfreeze_mutex);
247 if (++bdev->bd_fsfreeze_count > 1) {
249 * We don't even need to grab a reference - the first call
250 * to freeze_bdev grab an active reference and only the last
251 * thaw_bdev drops it.
253 sb = get_super(bdev);
254 drop_super(sb);
255 mutex_unlock(&bdev->bd_fsfreeze_mutex);
256 return sb;
259 sb = get_active_super(bdev);
260 if (!sb)
261 goto out;
262 error = freeze_super(sb);
263 if (error) {
264 deactivate_super(sb);
265 bdev->bd_fsfreeze_count--;
266 mutex_unlock(&bdev->bd_fsfreeze_mutex);
267 return ERR_PTR(error);
269 deactivate_super(sb);
270 out:
271 sync_blockdev(bdev);
272 mutex_unlock(&bdev->bd_fsfreeze_mutex);
273 return sb; /* thaw_bdev releases s->s_umount */
275 EXPORT_SYMBOL(freeze_bdev);
278 * thaw_bdev -- unlock filesystem
279 * @bdev: blockdevice to unlock
280 * @sb: associated superblock
282 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
284 int thaw_bdev(struct block_device *bdev, struct super_block *sb)
286 int error = -EINVAL;
288 mutex_lock(&bdev->bd_fsfreeze_mutex);
289 if (!bdev->bd_fsfreeze_count)
290 goto out;
292 error = 0;
293 if (--bdev->bd_fsfreeze_count > 0)
294 goto out;
296 if (!sb)
297 goto out;
299 error = thaw_super(sb);
300 if (error) {
301 bdev->bd_fsfreeze_count++;
302 mutex_unlock(&bdev->bd_fsfreeze_mutex);
303 return error;
305 out:
306 mutex_unlock(&bdev->bd_fsfreeze_mutex);
307 return 0;
309 EXPORT_SYMBOL(thaw_bdev);
311 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
313 return block_write_full_page(page, blkdev_get_block, wbc);
316 static int blkdev_readpage(struct file * file, struct page * page)
318 return block_read_full_page(page, blkdev_get_block);
321 static int blkdev_write_begin(struct file *file, struct address_space *mapping,
322 loff_t pos, unsigned len, unsigned flags,
323 struct page **pagep, void **fsdata)
325 return block_write_begin(mapping, pos, len, flags, pagep,
326 blkdev_get_block);
329 static int blkdev_write_end(struct file *file, struct address_space *mapping,
330 loff_t pos, unsigned len, unsigned copied,
331 struct page *page, void *fsdata)
333 int ret;
334 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
336 unlock_page(page);
337 page_cache_release(page);
339 return ret;
343 * private llseek:
344 * for a block special file file->f_path.dentry->d_inode->i_size is zero
345 * so we compute the size by hand (just as in block_read/write above)
347 static loff_t block_llseek(struct file *file, loff_t offset, int origin)
349 struct inode *bd_inode = file->f_mapping->host;
350 loff_t size;
351 loff_t retval;
353 mutex_lock(&bd_inode->i_mutex);
354 size = i_size_read(bd_inode);
356 switch (origin) {
357 case 2:
358 offset += size;
359 break;
360 case 1:
361 offset += file->f_pos;
363 retval = -EINVAL;
364 if (offset >= 0 && offset <= size) {
365 if (offset != file->f_pos) {
366 file->f_pos = offset;
368 retval = offset;
370 mutex_unlock(&bd_inode->i_mutex);
371 return retval;
374 int blkdev_fsync(struct file *filp, int datasync)
376 struct inode *bd_inode = filp->f_mapping->host;
377 struct block_device *bdev = I_BDEV(bd_inode);
378 int error;
381 * There is no need to serialise calls to blkdev_issue_flush with
382 * i_mutex and doing so causes performance issues with concurrent
383 * O_SYNC writers to a block device.
385 mutex_unlock(&bd_inode->i_mutex);
387 error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
388 if (error == -EOPNOTSUPP)
389 error = 0;
391 mutex_lock(&bd_inode->i_mutex);
393 return error;
395 EXPORT_SYMBOL(blkdev_fsync);
398 * pseudo-fs
401 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
402 static struct kmem_cache * bdev_cachep __read_mostly;
404 static struct inode *bdev_alloc_inode(struct super_block *sb)
406 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
407 if (!ei)
408 return NULL;
409 return &ei->vfs_inode;
412 static void bdev_i_callback(struct rcu_head *head)
414 struct inode *inode = container_of(head, struct inode, i_rcu);
415 struct bdev_inode *bdi = BDEV_I(inode);
417 INIT_LIST_HEAD(&inode->i_dentry);
418 kmem_cache_free(bdev_cachep, bdi);
421 static void bdev_destroy_inode(struct inode *inode)
423 call_rcu(&inode->i_rcu, bdev_i_callback);
426 static void init_once(void *foo)
428 struct bdev_inode *ei = (struct bdev_inode *) foo;
429 struct block_device *bdev = &ei->bdev;
431 memset(bdev, 0, sizeof(*bdev));
432 mutex_init(&bdev->bd_mutex);
433 INIT_LIST_HEAD(&bdev->bd_inodes);
434 INIT_LIST_HEAD(&bdev->bd_list);
435 #ifdef CONFIG_SYSFS
436 INIT_LIST_HEAD(&bdev->bd_holder_disks);
437 #endif
438 inode_init_once(&ei->vfs_inode);
439 /* Initialize mutex for freeze. */
440 mutex_init(&bdev->bd_fsfreeze_mutex);
443 static inline void __bd_forget(struct inode *inode)
445 list_del_init(&inode->i_devices);
446 inode->i_bdev = NULL;
447 inode->i_mapping = &inode->i_data;
450 static void bdev_evict_inode(struct inode *inode)
452 struct block_device *bdev = &BDEV_I(inode)->bdev;
453 struct list_head *p;
454 truncate_inode_pages(&inode->i_data, 0);
455 invalidate_inode_buffers(inode); /* is it needed here? */
456 end_writeback(inode);
457 spin_lock(&bdev_lock);
458 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
459 __bd_forget(list_entry(p, struct inode, i_devices));
461 list_del_init(&bdev->bd_list);
462 spin_unlock(&bdev_lock);
465 static const struct super_operations bdev_sops = {
466 .statfs = simple_statfs,
467 .alloc_inode = bdev_alloc_inode,
468 .destroy_inode = bdev_destroy_inode,
469 .drop_inode = generic_delete_inode,
470 .evict_inode = bdev_evict_inode,
473 static struct dentry *bd_mount(struct file_system_type *fs_type,
474 int flags, const char *dev_name, void *data)
476 return mount_pseudo(fs_type, "bdev:", &bdev_sops, NULL, 0x62646576);
479 static struct file_system_type bd_type = {
480 .name = "bdev",
481 .mount = bd_mount,
482 .kill_sb = kill_anon_super,
485 struct super_block *blockdev_superblock __read_mostly;
487 void __init bdev_cache_init(void)
489 int err;
490 struct vfsmount *bd_mnt;
492 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
493 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
494 SLAB_MEM_SPREAD|SLAB_PANIC),
495 init_once);
496 err = register_filesystem(&bd_type);
497 if (err)
498 panic("Cannot register bdev pseudo-fs");
499 bd_mnt = kern_mount(&bd_type);
500 if (IS_ERR(bd_mnt))
501 panic("Cannot create bdev pseudo-fs");
503 * This vfsmount structure is only used to obtain the
504 * blockdev_superblock, so tell kmemleak not to report it.
506 kmemleak_not_leak(bd_mnt);
507 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
511 * Most likely _very_ bad one - but then it's hardly critical for small
512 * /dev and can be fixed when somebody will need really large one.
513 * Keep in mind that it will be fed through icache hash function too.
515 static inline unsigned long hash(dev_t dev)
517 return MAJOR(dev)+MINOR(dev);
520 static int bdev_test(struct inode *inode, void *data)
522 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
525 static int bdev_set(struct inode *inode, void *data)
527 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
528 return 0;
531 static LIST_HEAD(all_bdevs);
533 struct block_device *bdget(dev_t dev)
535 struct block_device *bdev;
536 struct inode *inode;
538 inode = iget5_locked(blockdev_superblock, hash(dev),
539 bdev_test, bdev_set, &dev);
541 if (!inode)
542 return NULL;
544 bdev = &BDEV_I(inode)->bdev;
546 if (inode->i_state & I_NEW) {
547 bdev->bd_contains = NULL;
548 bdev->bd_inode = inode;
549 bdev->bd_block_size = (1 << inode->i_blkbits);
550 bdev->bd_part_count = 0;
551 bdev->bd_invalidated = 0;
552 inode->i_mode = S_IFBLK;
553 inode->i_rdev = dev;
554 inode->i_bdev = bdev;
555 inode->i_data.a_ops = &def_blk_aops;
556 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
557 inode->i_data.backing_dev_info = &default_backing_dev_info;
558 spin_lock(&bdev_lock);
559 list_add(&bdev->bd_list, &all_bdevs);
560 spin_unlock(&bdev_lock);
561 unlock_new_inode(inode);
563 return bdev;
566 EXPORT_SYMBOL(bdget);
569 * bdgrab -- Grab a reference to an already referenced block device
570 * @bdev: Block device to grab a reference to.
572 struct block_device *bdgrab(struct block_device *bdev)
574 ihold(bdev->bd_inode);
575 return bdev;
578 long nr_blockdev_pages(void)
580 struct block_device *bdev;
581 long ret = 0;
582 spin_lock(&bdev_lock);
583 list_for_each_entry(bdev, &all_bdevs, bd_list) {
584 ret += bdev->bd_inode->i_mapping->nrpages;
586 spin_unlock(&bdev_lock);
587 return ret;
590 void bdput(struct block_device *bdev)
592 iput(bdev->bd_inode);
595 EXPORT_SYMBOL(bdput);
597 static struct block_device *bd_acquire(struct inode *inode)
599 struct block_device *bdev;
601 spin_lock(&bdev_lock);
602 bdev = inode->i_bdev;
603 if (bdev) {
604 ihold(bdev->bd_inode);
605 spin_unlock(&bdev_lock);
606 return bdev;
608 spin_unlock(&bdev_lock);
610 bdev = bdget(inode->i_rdev);
611 if (bdev) {
612 spin_lock(&bdev_lock);
613 if (!inode->i_bdev) {
615 * We take an additional reference to bd_inode,
616 * and it's released in clear_inode() of inode.
617 * So, we can access it via ->i_mapping always
618 * without igrab().
620 ihold(bdev->bd_inode);
621 inode->i_bdev = bdev;
622 inode->i_mapping = bdev->bd_inode->i_mapping;
623 list_add(&inode->i_devices, &bdev->bd_inodes);
625 spin_unlock(&bdev_lock);
627 return bdev;
630 /* Call when you free inode */
632 void bd_forget(struct inode *inode)
634 struct block_device *bdev = NULL;
636 spin_lock(&bdev_lock);
637 if (inode->i_bdev) {
638 if (!sb_is_blkdev_sb(inode->i_sb))
639 bdev = inode->i_bdev;
640 __bd_forget(inode);
642 spin_unlock(&bdev_lock);
644 if (bdev)
645 iput(bdev->bd_inode);
649 * bd_may_claim - test whether a block device can be claimed
650 * @bdev: block device of interest
651 * @whole: whole block device containing @bdev, may equal @bdev
652 * @holder: holder trying to claim @bdev
654 * Test whther @bdev can be claimed by @holder.
656 * CONTEXT:
657 * spin_lock(&bdev_lock).
659 * RETURNS:
660 * %true if @bdev can be claimed, %false otherwise.
662 static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
663 void *holder)
665 if (bdev->bd_holder == holder)
666 return true; /* already a holder */
667 else if (bdev->bd_holder != NULL)
668 return false; /* held by someone else */
669 else if (bdev->bd_contains == bdev)
670 return true; /* is a whole device which isn't held */
672 else if (whole->bd_holder == bd_may_claim)
673 return true; /* is a partition of a device that is being partitioned */
674 else if (whole->bd_holder != NULL)
675 return false; /* is a partition of a held device */
676 else
677 return true; /* is a partition of an un-held device */
681 * bd_prepare_to_claim - prepare to claim a block device
682 * @bdev: block device of interest
683 * @whole: the whole device containing @bdev, may equal @bdev
684 * @holder: holder trying to claim @bdev
686 * Prepare to claim @bdev. This function fails if @bdev is already
687 * claimed by another holder and waits if another claiming is in
688 * progress. This function doesn't actually claim. On successful
689 * return, the caller has ownership of bd_claiming and bd_holder[s].
691 * CONTEXT:
692 * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
693 * it multiple times.
695 * RETURNS:
696 * 0 if @bdev can be claimed, -EBUSY otherwise.
698 static int bd_prepare_to_claim(struct block_device *bdev,
699 struct block_device *whole, void *holder)
701 retry:
702 /* if someone else claimed, fail */
703 if (!bd_may_claim(bdev, whole, holder))
704 return -EBUSY;
706 /* if claiming is already in progress, wait for it to finish */
707 if (whole->bd_claiming) {
708 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
709 DEFINE_WAIT(wait);
711 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
712 spin_unlock(&bdev_lock);
713 schedule();
714 finish_wait(wq, &wait);
715 spin_lock(&bdev_lock);
716 goto retry;
719 /* yay, all mine */
720 return 0;
724 * bd_start_claiming - start claiming a block device
725 * @bdev: block device of interest
726 * @holder: holder trying to claim @bdev
728 * @bdev is about to be opened exclusively. Check @bdev can be opened
729 * exclusively and mark that an exclusive open is in progress. Each
730 * successful call to this function must be matched with a call to
731 * either bd_finish_claiming() or bd_abort_claiming() (which do not
732 * fail).
734 * This function is used to gain exclusive access to the block device
735 * without actually causing other exclusive open attempts to fail. It
736 * should be used when the open sequence itself requires exclusive
737 * access but may subsequently fail.
739 * CONTEXT:
740 * Might sleep.
742 * RETURNS:
743 * Pointer to the block device containing @bdev on success, ERR_PTR()
744 * value on failure.
746 static struct block_device *bd_start_claiming(struct block_device *bdev,
747 void *holder)
749 struct gendisk *disk;
750 struct block_device *whole;
751 int partno, err;
753 might_sleep();
756 * @bdev might not have been initialized properly yet, look up
757 * and grab the outer block device the hard way.
759 disk = get_gendisk(bdev->bd_dev, &partno);
760 if (!disk)
761 return ERR_PTR(-ENXIO);
763 whole = bdget_disk(disk, 0);
764 module_put(disk->fops->owner);
765 put_disk(disk);
766 if (!whole)
767 return ERR_PTR(-ENOMEM);
769 /* prepare to claim, if successful, mark claiming in progress */
770 spin_lock(&bdev_lock);
772 err = bd_prepare_to_claim(bdev, whole, holder);
773 if (err == 0) {
774 whole->bd_claiming = holder;
775 spin_unlock(&bdev_lock);
776 return whole;
777 } else {
778 spin_unlock(&bdev_lock);
779 bdput(whole);
780 return ERR_PTR(err);
784 #ifdef CONFIG_SYSFS
785 struct bd_holder_disk {
786 struct list_head list;
787 struct gendisk *disk;
788 int refcnt;
791 static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
792 struct gendisk *disk)
794 struct bd_holder_disk *holder;
796 list_for_each_entry(holder, &bdev->bd_holder_disks, list)
797 if (holder->disk == disk)
798 return holder;
799 return NULL;
802 static int add_symlink(struct kobject *from, struct kobject *to)
804 return sysfs_create_link(from, to, kobject_name(to));
807 static void del_symlink(struct kobject *from, struct kobject *to)
809 sysfs_remove_link(from, kobject_name(to));
813 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
814 * @bdev: the claimed slave bdev
815 * @disk: the holding disk
817 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
819 * This functions creates the following sysfs symlinks.
821 * - from "slaves" directory of the holder @disk to the claimed @bdev
822 * - from "holders" directory of the @bdev to the holder @disk
824 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
825 * passed to bd_link_disk_holder(), then:
827 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
828 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
830 * The caller must have claimed @bdev before calling this function and
831 * ensure that both @bdev and @disk are valid during the creation and
832 * lifetime of these symlinks.
834 * CONTEXT:
835 * Might sleep.
837 * RETURNS:
838 * 0 on success, -errno on failure.
840 int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
842 struct bd_holder_disk *holder;
843 int ret = 0;
845 mutex_lock(&bdev->bd_mutex);
847 WARN_ON_ONCE(!bdev->bd_holder);
849 /* FIXME: remove the following once add_disk() handles errors */
850 if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
851 goto out_unlock;
853 holder = bd_find_holder_disk(bdev, disk);
854 if (holder) {
855 holder->refcnt++;
856 goto out_unlock;
859 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
860 if (!holder) {
861 ret = -ENOMEM;
862 goto out_unlock;
865 INIT_LIST_HEAD(&holder->list);
866 holder->disk = disk;
867 holder->refcnt = 1;
869 ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
870 if (ret)
871 goto out_free;
873 ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
874 if (ret)
875 goto out_del;
877 * bdev could be deleted beneath us which would implicitly destroy
878 * the holder directory. Hold on to it.
880 kobject_get(bdev->bd_part->holder_dir);
882 list_add(&holder->list, &bdev->bd_holder_disks);
883 goto out_unlock;
885 out_del:
886 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
887 out_free:
888 kfree(holder);
889 out_unlock:
890 mutex_unlock(&bdev->bd_mutex);
891 return ret;
893 EXPORT_SYMBOL_GPL(bd_link_disk_holder);
896 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
897 * @bdev: the calimed slave bdev
898 * @disk: the holding disk
900 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
902 * CONTEXT:
903 * Might sleep.
905 void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
907 struct bd_holder_disk *holder;
909 mutex_lock(&bdev->bd_mutex);
911 holder = bd_find_holder_disk(bdev, disk);
913 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
914 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
915 del_symlink(bdev->bd_part->holder_dir,
916 &disk_to_dev(disk)->kobj);
917 kobject_put(bdev->bd_part->holder_dir);
918 list_del_init(&holder->list);
919 kfree(holder);
922 mutex_unlock(&bdev->bd_mutex);
924 EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
925 #endif
928 * flush_disk - invalidates all buffer-cache entries on a disk
930 * @bdev: struct block device to be flushed
931 * @kill_dirty: flag to guide handling of dirty inodes
933 * Invalidates all buffer-cache entries on a disk. It should be called
934 * when a disk has been changed -- either by a media change or online
935 * resize.
937 static void flush_disk(struct block_device *bdev, bool kill_dirty)
939 if (__invalidate_device(bdev, kill_dirty)) {
940 char name[BDEVNAME_SIZE] = "";
942 if (bdev->bd_disk)
943 disk_name(bdev->bd_disk, 0, name);
944 printk(KERN_WARNING "VFS: busy inodes on changed media or "
945 "resized disk %s\n", name);
948 if (!bdev->bd_disk)
949 return;
950 if (disk_partitionable(bdev->bd_disk))
951 bdev->bd_invalidated = 1;
955 * check_disk_size_change - checks for disk size change and adjusts bdev size.
956 * @disk: struct gendisk to check
957 * @bdev: struct bdev to adjust.
959 * This routine checks to see if the bdev size does not match the disk size
960 * and adjusts it if it differs.
962 void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
964 loff_t disk_size, bdev_size;
966 disk_size = (loff_t)get_capacity(disk) << 9;
967 bdev_size = i_size_read(bdev->bd_inode);
968 if (disk_size != bdev_size) {
969 char name[BDEVNAME_SIZE];
971 disk_name(disk, 0, name);
972 printk(KERN_INFO
973 "%s: detected capacity change from %lld to %lld\n",
974 name, bdev_size, disk_size);
975 i_size_write(bdev->bd_inode, disk_size);
976 flush_disk(bdev, false);
979 EXPORT_SYMBOL(check_disk_size_change);
982 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
983 * @disk: struct gendisk to be revalidated
985 * This routine is a wrapper for lower-level driver's revalidate_disk
986 * call-backs. It is used to do common pre and post operations needed
987 * for all revalidate_disk operations.
989 int revalidate_disk(struct gendisk *disk)
991 struct block_device *bdev;
992 int ret = 0;
994 if (disk->fops->revalidate_disk)
995 ret = disk->fops->revalidate_disk(disk);
997 bdev = bdget_disk(disk, 0);
998 if (!bdev)
999 return ret;
1001 mutex_lock(&bdev->bd_mutex);
1002 check_disk_size_change(disk, bdev);
1003 mutex_unlock(&bdev->bd_mutex);
1004 bdput(bdev);
1005 return ret;
1007 EXPORT_SYMBOL(revalidate_disk);
1010 * This routine checks whether a removable media has been changed,
1011 * and invalidates all buffer-cache-entries in that case. This
1012 * is a relatively slow routine, so we have to try to minimize using
1013 * it. Thus it is called only upon a 'mount' or 'open'. This
1014 * is the best way of combining speed and utility, I think.
1015 * People changing diskettes in the middle of an operation deserve
1016 * to lose :-)
1018 int check_disk_change(struct block_device *bdev)
1020 struct gendisk *disk = bdev->bd_disk;
1021 const struct block_device_operations *bdops = disk->fops;
1022 unsigned int events;
1024 events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
1025 DISK_EVENT_EJECT_REQUEST);
1026 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1027 return 0;
1029 flush_disk(bdev, true);
1030 if (bdops->revalidate_disk)
1031 bdops->revalidate_disk(bdev->bd_disk);
1032 return 1;
1035 EXPORT_SYMBOL(check_disk_change);
1037 void bd_set_size(struct block_device *bdev, loff_t size)
1039 unsigned bsize = bdev_logical_block_size(bdev);
1041 bdev->bd_inode->i_size = size;
1042 while (bsize < PAGE_CACHE_SIZE) {
1043 if (size & bsize)
1044 break;
1045 bsize <<= 1;
1047 bdev->bd_block_size = bsize;
1048 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
1050 EXPORT_SYMBOL(bd_set_size);
1052 static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
1055 * bd_mutex locking:
1057 * mutex_lock(part->bd_mutex)
1058 * mutex_lock_nested(whole->bd_mutex, 1)
1061 static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
1063 struct gendisk *disk;
1064 int ret;
1065 int partno;
1066 int perm = 0;
1068 if (mode & FMODE_READ)
1069 perm |= MAY_READ;
1070 if (mode & FMODE_WRITE)
1071 perm |= MAY_WRITE;
1073 * hooks: /n/, see "layering violations".
1075 if (!for_part) {
1076 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1077 if (ret != 0) {
1078 bdput(bdev);
1079 return ret;
1083 restart:
1085 ret = -ENXIO;
1086 disk = get_gendisk(bdev->bd_dev, &partno);
1087 if (!disk)
1088 goto out;
1090 mutex_lock_nested(&bdev->bd_mutex, for_part);
1091 if (!bdev->bd_openers) {
1092 bdev->bd_disk = disk;
1093 bdev->bd_contains = bdev;
1094 if (!partno) {
1095 struct backing_dev_info *bdi;
1097 ret = -ENXIO;
1098 bdev->bd_part = disk_get_part(disk, partno);
1099 if (!bdev->bd_part)
1100 goto out_clear;
1102 ret = 0;
1103 if (disk->fops->open) {
1104 ret = disk->fops->open(bdev, mode);
1105 if (ret == -ERESTARTSYS) {
1106 /* Lost a race with 'disk' being
1107 * deleted, try again.
1108 * See md.c
1110 disk_put_part(bdev->bd_part);
1111 bdev->bd_part = NULL;
1112 module_put(disk->fops->owner);
1113 put_disk(disk);
1114 bdev->bd_disk = NULL;
1115 mutex_unlock(&bdev->bd_mutex);
1116 goto restart;
1120 if (!ret && !bdev->bd_openers) {
1121 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1122 bdi = blk_get_backing_dev_info(bdev);
1123 if (bdi == NULL)
1124 bdi = &default_backing_dev_info;
1125 bdev_inode_switch_bdi(bdev->bd_inode, bdi);
1129 * If the device is invalidated, rescan partition
1130 * if open succeeded or failed with -ENOMEDIUM.
1131 * The latter is necessary to prevent ghost
1132 * partitions on a removed medium.
1134 if (bdev->bd_invalidated && (!ret || ret == -ENOMEDIUM))
1135 rescan_partitions(disk, bdev);
1136 if (ret)
1137 goto out_clear;
1138 } else {
1139 struct block_device *whole;
1140 whole = bdget_disk(disk, 0);
1141 ret = -ENOMEM;
1142 if (!whole)
1143 goto out_clear;
1144 BUG_ON(for_part);
1145 ret = __blkdev_get(whole, mode, 1);
1146 if (ret)
1147 goto out_clear;
1148 bdev->bd_contains = whole;
1149 bdev_inode_switch_bdi(bdev->bd_inode,
1150 whole->bd_inode->i_data.backing_dev_info);
1151 bdev->bd_part = disk_get_part(disk, partno);
1152 if (!(disk->flags & GENHD_FL_UP) ||
1153 !bdev->bd_part || !bdev->bd_part->nr_sects) {
1154 ret = -ENXIO;
1155 goto out_clear;
1157 bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
1159 } else {
1160 module_put(disk->fops->owner);
1161 put_disk(disk);
1162 disk = NULL;
1163 if (bdev->bd_contains == bdev) {
1164 ret = 0;
1165 if (bdev->bd_disk->fops->open)
1166 ret = bdev->bd_disk->fops->open(bdev, mode);
1167 /* the same as first opener case, read comment there */
1168 if (bdev->bd_invalidated && (!ret || ret == -ENOMEDIUM))
1169 rescan_partitions(bdev->bd_disk, bdev);
1170 if (ret)
1171 goto out_unlock_bdev;
1174 bdev->bd_openers++;
1175 if (for_part)
1176 bdev->bd_part_count++;
1177 mutex_unlock(&bdev->bd_mutex);
1178 return 0;
1180 out_clear:
1181 disk_put_part(bdev->bd_part);
1182 bdev->bd_disk = NULL;
1183 bdev->bd_part = NULL;
1184 bdev_inode_switch_bdi(bdev->bd_inode, &default_backing_dev_info);
1185 if (bdev != bdev->bd_contains)
1186 __blkdev_put(bdev->bd_contains, mode, 1);
1187 bdev->bd_contains = NULL;
1188 out_unlock_bdev:
1189 mutex_unlock(&bdev->bd_mutex);
1190 out:
1191 if (disk)
1192 module_put(disk->fops->owner);
1193 put_disk(disk);
1194 bdput(bdev);
1196 return ret;
1200 * blkdev_get - open a block device
1201 * @bdev: block_device to open
1202 * @mode: FMODE_* mask
1203 * @holder: exclusive holder identifier
1205 * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
1206 * open with exclusive access. Specifying %FMODE_EXCL with %NULL
1207 * @holder is invalid. Exclusive opens may nest for the same @holder.
1209 * On success, the reference count of @bdev is unchanged. On failure,
1210 * @bdev is put.
1212 * CONTEXT:
1213 * Might sleep.
1215 * RETURNS:
1216 * 0 on success, -errno on failure.
1218 int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
1220 struct block_device *whole = NULL;
1221 int res;
1223 WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
1225 if ((mode & FMODE_EXCL) && holder) {
1226 whole = bd_start_claiming(bdev, holder);
1227 if (IS_ERR(whole)) {
1228 bdput(bdev);
1229 return PTR_ERR(whole);
1233 res = __blkdev_get(bdev, mode, 0);
1235 if (whole) {
1236 struct gendisk *disk = whole->bd_disk;
1238 /* finish claiming */
1239 mutex_lock(&bdev->bd_mutex);
1240 spin_lock(&bdev_lock);
1242 if (!res) {
1243 BUG_ON(!bd_may_claim(bdev, whole, holder));
1245 * Note that for a whole device bd_holders
1246 * will be incremented twice, and bd_holder
1247 * will be set to bd_may_claim before being
1248 * set to holder
1250 whole->bd_holders++;
1251 whole->bd_holder = bd_may_claim;
1252 bdev->bd_holders++;
1253 bdev->bd_holder = holder;
1256 /* tell others that we're done */
1257 BUG_ON(whole->bd_claiming != holder);
1258 whole->bd_claiming = NULL;
1259 wake_up_bit(&whole->bd_claiming, 0);
1261 spin_unlock(&bdev_lock);
1264 * Block event polling for write claims if requested. Any
1265 * write holder makes the write_holder state stick until
1266 * all are released. This is good enough and tracking
1267 * individual writeable reference is too fragile given the
1268 * way @mode is used in blkdev_get/put().
1270 if ((disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE) &&
1271 !res && (mode & FMODE_WRITE) && !bdev->bd_write_holder) {
1272 bdev->bd_write_holder = true;
1273 disk_block_events(disk);
1276 mutex_unlock(&bdev->bd_mutex);
1277 bdput(whole);
1280 return res;
1282 EXPORT_SYMBOL(blkdev_get);
1285 * blkdev_get_by_path - open a block device by name
1286 * @path: path to the block device to open
1287 * @mode: FMODE_* mask
1288 * @holder: exclusive holder identifier
1290 * Open the blockdevice described by the device file at @path. @mode
1291 * and @holder are identical to blkdev_get().
1293 * On success, the returned block_device has reference count of one.
1295 * CONTEXT:
1296 * Might sleep.
1298 * RETURNS:
1299 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1301 struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1302 void *holder)
1304 struct block_device *bdev;
1305 int err;
1307 bdev = lookup_bdev(path);
1308 if (IS_ERR(bdev))
1309 return bdev;
1311 err = blkdev_get(bdev, mode, holder);
1312 if (err)
1313 return ERR_PTR(err);
1315 if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
1316 blkdev_put(bdev, mode);
1317 return ERR_PTR(-EACCES);
1320 return bdev;
1322 EXPORT_SYMBOL(blkdev_get_by_path);
1325 * blkdev_get_by_dev - open a block device by device number
1326 * @dev: device number of block device to open
1327 * @mode: FMODE_* mask
1328 * @holder: exclusive holder identifier
1330 * Open the blockdevice described by device number @dev. @mode and
1331 * @holder are identical to blkdev_get().
1333 * Use it ONLY if you really do not have anything better - i.e. when
1334 * you are behind a truly sucky interface and all you are given is a
1335 * device number. _Never_ to be used for internal purposes. If you
1336 * ever need it - reconsider your API.
1338 * On success, the returned block_device has reference count of one.
1340 * CONTEXT:
1341 * Might sleep.
1343 * RETURNS:
1344 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1346 struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
1348 struct block_device *bdev;
1349 int err;
1351 bdev = bdget(dev);
1352 if (!bdev)
1353 return ERR_PTR(-ENOMEM);
1355 err = blkdev_get(bdev, mode, holder);
1356 if (err)
1357 return ERR_PTR(err);
1359 return bdev;
1361 EXPORT_SYMBOL(blkdev_get_by_dev);
1363 static int blkdev_open(struct inode * inode, struct file * filp)
1365 struct block_device *bdev;
1368 * Preserve backwards compatibility and allow large file access
1369 * even if userspace doesn't ask for it explicitly. Some mkfs
1370 * binary needs it. We might want to drop this workaround
1371 * during an unstable branch.
1373 filp->f_flags |= O_LARGEFILE;
1375 if (filp->f_flags & O_NDELAY)
1376 filp->f_mode |= FMODE_NDELAY;
1377 if (filp->f_flags & O_EXCL)
1378 filp->f_mode |= FMODE_EXCL;
1379 if ((filp->f_flags & O_ACCMODE) == 3)
1380 filp->f_mode |= FMODE_WRITE_IOCTL;
1382 bdev = bd_acquire(inode);
1383 if (bdev == NULL)
1384 return -ENOMEM;
1386 filp->f_mapping = bdev->bd_inode->i_mapping;
1388 return blkdev_get(bdev, filp->f_mode, filp);
1391 static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
1393 int ret = 0;
1394 struct gendisk *disk = bdev->bd_disk;
1395 struct block_device *victim = NULL;
1397 mutex_lock_nested(&bdev->bd_mutex, for_part);
1398 if (for_part)
1399 bdev->bd_part_count--;
1401 if (!--bdev->bd_openers) {
1402 WARN_ON_ONCE(bdev->bd_holders);
1403 sync_blockdev(bdev);
1404 kill_bdev(bdev);
1406 if (bdev->bd_contains == bdev) {
1407 if (disk->fops->release)
1408 ret = disk->fops->release(disk, mode);
1410 if (!bdev->bd_openers) {
1411 struct module *owner = disk->fops->owner;
1413 put_disk(disk);
1414 module_put(owner);
1415 disk_put_part(bdev->bd_part);
1416 bdev->bd_part = NULL;
1417 bdev->bd_disk = NULL;
1418 bdev_inode_switch_bdi(bdev->bd_inode,
1419 &default_backing_dev_info);
1420 if (bdev != bdev->bd_contains)
1421 victim = bdev->bd_contains;
1422 bdev->bd_contains = NULL;
1424 mutex_unlock(&bdev->bd_mutex);
1425 bdput(bdev);
1426 if (victim)
1427 __blkdev_put(victim, mode, 1);
1428 return ret;
1431 int blkdev_put(struct block_device *bdev, fmode_t mode)
1433 if (mode & FMODE_EXCL) {
1434 bool bdev_free;
1437 * Release a claim on the device. The holder fields
1438 * are protected with bdev_lock. bd_mutex is to
1439 * synchronize disk_holder unlinking.
1441 mutex_lock(&bdev->bd_mutex);
1442 spin_lock(&bdev_lock);
1444 WARN_ON_ONCE(--bdev->bd_holders < 0);
1445 WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
1447 /* bd_contains might point to self, check in a separate step */
1448 if ((bdev_free = !bdev->bd_holders))
1449 bdev->bd_holder = NULL;
1450 if (!bdev->bd_contains->bd_holders)
1451 bdev->bd_contains->bd_holder = NULL;
1453 spin_unlock(&bdev_lock);
1456 * If this was the last claim, remove holder link and
1457 * unblock evpoll if it was a write holder.
1459 if (bdev_free) {
1460 if (bdev->bd_write_holder) {
1461 disk_unblock_events(bdev->bd_disk);
1462 bdev->bd_write_holder = false;
1463 } else
1464 disk_check_events(bdev->bd_disk);
1467 mutex_unlock(&bdev->bd_mutex);
1468 } else
1469 disk_check_events(bdev->bd_disk);
1471 return __blkdev_put(bdev, mode, 0);
1473 EXPORT_SYMBOL(blkdev_put);
1475 static int blkdev_close(struct inode * inode, struct file * filp)
1477 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1479 return blkdev_put(bdev, filp->f_mode);
1482 static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1484 struct block_device *bdev = I_BDEV(file->f_mapping->host);
1485 fmode_t mode = file->f_mode;
1488 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1489 * to updated it before every ioctl.
1491 if (file->f_flags & O_NDELAY)
1492 mode |= FMODE_NDELAY;
1493 else
1494 mode &= ~FMODE_NDELAY;
1496 return blkdev_ioctl(bdev, mode, cmd, arg);
1500 * Write data to the block device. Only intended for the block device itself
1501 * and the raw driver which basically is a fake block device.
1503 * Does not take i_mutex for the write and thus is not for general purpose
1504 * use.
1506 ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov,
1507 unsigned long nr_segs, loff_t pos)
1509 struct file *file = iocb->ki_filp;
1510 ssize_t ret;
1512 BUG_ON(iocb->ki_pos != pos);
1514 ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
1515 if (ret > 0 || ret == -EIOCBQUEUED) {
1516 ssize_t err;
1518 err = generic_write_sync(file, pos, ret);
1519 if (err < 0 && ret > 0)
1520 ret = err;
1522 return ret;
1524 EXPORT_SYMBOL_GPL(blkdev_aio_write);
1527 * Try to release a page associated with block device when the system
1528 * is under memory pressure.
1530 static int blkdev_releasepage(struct page *page, gfp_t wait)
1532 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1534 if (super && super->s_op->bdev_try_to_free_page)
1535 return super->s_op->bdev_try_to_free_page(super, page, wait);
1537 return try_to_free_buffers(page);
1540 static const struct address_space_operations def_blk_aops = {
1541 .readpage = blkdev_readpage,
1542 .writepage = blkdev_writepage,
1543 .sync_page = block_sync_page,
1544 .write_begin = blkdev_write_begin,
1545 .write_end = blkdev_write_end,
1546 .writepages = generic_writepages,
1547 .releasepage = blkdev_releasepage,
1548 .direct_IO = blkdev_direct_IO,
1551 const struct file_operations def_blk_fops = {
1552 .open = blkdev_open,
1553 .release = blkdev_close,
1554 .llseek = block_llseek,
1555 .read = do_sync_read,
1556 .write = do_sync_write,
1557 .aio_read = generic_file_aio_read,
1558 .aio_write = blkdev_aio_write,
1559 .mmap = generic_file_mmap,
1560 .fsync = blkdev_fsync,
1561 .unlocked_ioctl = block_ioctl,
1562 #ifdef CONFIG_COMPAT
1563 .compat_ioctl = compat_blkdev_ioctl,
1564 #endif
1565 .splice_read = generic_file_splice_read,
1566 .splice_write = generic_file_splice_write,
1569 int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1571 int res;
1572 mm_segment_t old_fs = get_fs();
1573 set_fs(KERNEL_DS);
1574 res = blkdev_ioctl(bdev, 0, cmd, arg);
1575 set_fs(old_fs);
1576 return res;
1579 EXPORT_SYMBOL(ioctl_by_bdev);
1582 * lookup_bdev - lookup a struct block_device by name
1583 * @pathname: special file representing the block device
1585 * Get a reference to the blockdevice at @pathname in the current
1586 * namespace if possible and return it. Return ERR_PTR(error)
1587 * otherwise.
1589 struct block_device *lookup_bdev(const char *pathname)
1591 struct block_device *bdev;
1592 struct inode *inode;
1593 struct path path;
1594 int error;
1596 if (!pathname || !*pathname)
1597 return ERR_PTR(-EINVAL);
1599 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
1600 if (error)
1601 return ERR_PTR(error);
1603 inode = path.dentry->d_inode;
1604 error = -ENOTBLK;
1605 if (!S_ISBLK(inode->i_mode))
1606 goto fail;
1607 error = -EACCES;
1608 if (path.mnt->mnt_flags & MNT_NODEV)
1609 goto fail;
1610 error = -ENOMEM;
1611 bdev = bd_acquire(inode);
1612 if (!bdev)
1613 goto fail;
1614 out:
1615 path_put(&path);
1616 return bdev;
1617 fail:
1618 bdev = ERR_PTR(error);
1619 goto out;
1621 EXPORT_SYMBOL(lookup_bdev);
1623 int __invalidate_device(struct block_device *bdev, bool kill_dirty)
1625 struct super_block *sb = get_super(bdev);
1626 int res = 0;
1628 if (sb) {
1630 * no need to lock the super, get_super holds the
1631 * read mutex so the filesystem cannot go away
1632 * under us (->put_super runs with the write lock
1633 * hold).
1635 shrink_dcache_sb(sb);
1636 res = invalidate_inodes(sb, kill_dirty);
1637 drop_super(sb);
1639 invalidate_bdev(bdev);
1640 return res;
1642 EXPORT_SYMBOL(__invalidate_device);