[PATCH] Yet more rio cleaning (1 of 2)
[linux-2.6/kvm.git] / fs / block_dev.c
blob573fc8e0b67a01b5f8ce799768ea241daeace02d
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/config.h>
9 #include <linux/init.h>
10 #include <linux/mm.h>
11 #include <linux/fcntl.h>
12 #include <linux/slab.h>
13 #include <linux/kmod.h>
14 #include <linux/major.h>
15 #include <linux/devfs_fs_kernel.h>
16 #include <linux/smp_lock.h>
17 #include <linux/highmem.h>
18 #include <linux/blkdev.h>
19 #include <linux/module.h>
20 #include <linux/blkpg.h>
21 #include <linux/buffer_head.h>
22 #include <linux/mpage.h>
23 #include <linux/mount.h>
24 #include <linux/uio.h>
25 #include <linux/namei.h>
26 #include <asm/uaccess.h>
28 struct bdev_inode {
29 struct block_device bdev;
30 struct inode vfs_inode;
33 static inline struct bdev_inode *BDEV_I(struct inode *inode)
35 return container_of(inode, struct bdev_inode, vfs_inode);
38 inline struct block_device *I_BDEV(struct inode *inode)
40 return &BDEV_I(inode)->bdev;
43 EXPORT_SYMBOL(I_BDEV);
45 static sector_t max_block(struct block_device *bdev)
47 sector_t retval = ~((sector_t)0);
48 loff_t sz = i_size_read(bdev->bd_inode);
50 if (sz) {
51 unsigned int size = block_size(bdev);
52 unsigned int sizebits = blksize_bits(size);
53 retval = (sz >> sizebits);
55 return retval;
58 /* Kill _all_ buffers, dirty or not.. */
59 static void kill_bdev(struct block_device *bdev)
61 invalidate_bdev(bdev, 1);
62 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
65 int set_blocksize(struct block_device *bdev, int size)
67 /* Size must be a power of two, and between 512 and PAGE_SIZE */
68 if (size > PAGE_SIZE || size < 512 || (size & (size-1)))
69 return -EINVAL;
71 /* Size cannot be smaller than the size supported by the device */
72 if (size < bdev_hardsect_size(bdev))
73 return -EINVAL;
75 /* Don't change the size if it is same as current */
76 if (bdev->bd_block_size != size) {
77 sync_blockdev(bdev);
78 bdev->bd_block_size = size;
79 bdev->bd_inode->i_blkbits = blksize_bits(size);
80 kill_bdev(bdev);
82 return 0;
85 EXPORT_SYMBOL(set_blocksize);
87 int sb_set_blocksize(struct super_block *sb, int size)
89 if (set_blocksize(sb->s_bdev, size))
90 return 0;
91 /* If we get here, we know size is power of two
92 * and it's value is between 512 and PAGE_SIZE */
93 sb->s_blocksize = size;
94 sb->s_blocksize_bits = blksize_bits(size);
95 return sb->s_blocksize;
98 EXPORT_SYMBOL(sb_set_blocksize);
100 int sb_min_blocksize(struct super_block *sb, int size)
102 int minsize = bdev_hardsect_size(sb->s_bdev);
103 if (size < minsize)
104 size = minsize;
105 return sb_set_blocksize(sb, size);
108 EXPORT_SYMBOL(sb_min_blocksize);
110 static int
111 blkdev_get_block(struct inode *inode, sector_t iblock,
112 struct buffer_head *bh, int create)
114 if (iblock >= max_block(I_BDEV(inode))) {
115 if (create)
116 return -EIO;
119 * for reads, we're just trying to fill a partial page.
120 * return a hole, they will have to call get_block again
121 * before they can fill it, and they will get -EIO at that
122 * time
124 return 0;
126 bh->b_bdev = I_BDEV(inode);
127 bh->b_blocknr = iblock;
128 set_buffer_mapped(bh);
129 return 0;
132 static int
133 blkdev_get_blocks(struct inode *inode, sector_t iblock,
134 unsigned long max_blocks, struct buffer_head *bh, int create)
136 sector_t end_block = max_block(I_BDEV(inode));
138 if ((iblock + max_blocks) > end_block) {
139 max_blocks = end_block - iblock;
140 if ((long)max_blocks <= 0) {
141 if (create)
142 return -EIO; /* write fully beyond EOF */
144 * It is a read which is fully beyond EOF. We return
145 * a !buffer_mapped buffer
147 max_blocks = 0;
151 bh->b_bdev = I_BDEV(inode);
152 bh->b_blocknr = iblock;
153 bh->b_size = max_blocks << inode->i_blkbits;
154 if (max_blocks)
155 set_buffer_mapped(bh);
156 return 0;
159 static ssize_t
160 blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
161 loff_t offset, unsigned long nr_segs)
163 struct file *file = iocb->ki_filp;
164 struct inode *inode = file->f_mapping->host;
166 return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode),
167 iov, offset, nr_segs, blkdev_get_blocks, NULL);
170 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
172 return block_write_full_page(page, blkdev_get_block, wbc);
175 static int blkdev_readpage(struct file * file, struct page * page)
177 return block_read_full_page(page, blkdev_get_block);
180 static int blkdev_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
182 return block_prepare_write(page, from, to, blkdev_get_block);
185 static int blkdev_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
187 return block_commit_write(page, from, to);
191 * private llseek:
192 * for a block special file file->f_dentry->d_inode->i_size is zero
193 * so we compute the size by hand (just as in block_read/write above)
195 static loff_t block_llseek(struct file *file, loff_t offset, int origin)
197 struct inode *bd_inode = file->f_mapping->host;
198 loff_t size;
199 loff_t retval;
201 mutex_lock(&bd_inode->i_mutex);
202 size = i_size_read(bd_inode);
204 switch (origin) {
205 case 2:
206 offset += size;
207 break;
208 case 1:
209 offset += file->f_pos;
211 retval = -EINVAL;
212 if (offset >= 0 && offset <= size) {
213 if (offset != file->f_pos) {
214 file->f_pos = offset;
216 retval = offset;
218 mutex_unlock(&bd_inode->i_mutex);
219 return retval;
223 * Filp is never NULL; the only case when ->fsync() is called with
224 * NULL first argument is nfsd_sync_dir() and that's not a directory.
227 static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
229 return sync_blockdev(I_BDEV(filp->f_mapping->host));
233 * pseudo-fs
236 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
237 static kmem_cache_t * bdev_cachep;
239 static struct inode *bdev_alloc_inode(struct super_block *sb)
241 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, SLAB_KERNEL);
242 if (!ei)
243 return NULL;
244 return &ei->vfs_inode;
247 static void bdev_destroy_inode(struct inode *inode)
249 struct bdev_inode *bdi = BDEV_I(inode);
251 bdi->bdev.bd_inode_backing_dev_info = NULL;
252 kmem_cache_free(bdev_cachep, bdi);
255 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
257 struct bdev_inode *ei = (struct bdev_inode *) foo;
258 struct block_device *bdev = &ei->bdev;
260 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
261 SLAB_CTOR_CONSTRUCTOR)
263 memset(bdev, 0, sizeof(*bdev));
264 mutex_init(&bdev->bd_mutex);
265 mutex_init(&bdev->bd_mount_mutex);
266 INIT_LIST_HEAD(&bdev->bd_inodes);
267 INIT_LIST_HEAD(&bdev->bd_list);
268 inode_init_once(&ei->vfs_inode);
272 static inline void __bd_forget(struct inode *inode)
274 list_del_init(&inode->i_devices);
275 inode->i_bdev = NULL;
276 inode->i_mapping = &inode->i_data;
279 static void bdev_clear_inode(struct inode *inode)
281 struct block_device *bdev = &BDEV_I(inode)->bdev;
282 struct list_head *p;
283 spin_lock(&bdev_lock);
284 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
285 __bd_forget(list_entry(p, struct inode, i_devices));
287 list_del_init(&bdev->bd_list);
288 spin_unlock(&bdev_lock);
291 static struct super_operations bdev_sops = {
292 .statfs = simple_statfs,
293 .alloc_inode = bdev_alloc_inode,
294 .destroy_inode = bdev_destroy_inode,
295 .drop_inode = generic_delete_inode,
296 .clear_inode = bdev_clear_inode,
299 static struct super_block *bd_get_sb(struct file_system_type *fs_type,
300 int flags, const char *dev_name, void *data)
302 return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576);
305 static struct file_system_type bd_type = {
306 .name = "bdev",
307 .get_sb = bd_get_sb,
308 .kill_sb = kill_anon_super,
311 static struct vfsmount *bd_mnt;
312 struct super_block *blockdev_superblock;
314 void __init bdev_cache_init(void)
316 int err;
317 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
318 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
319 SLAB_MEM_SPREAD|SLAB_PANIC),
320 init_once, NULL);
321 err = register_filesystem(&bd_type);
322 if (err)
323 panic("Cannot register bdev pseudo-fs");
324 bd_mnt = kern_mount(&bd_type);
325 err = PTR_ERR(bd_mnt);
326 if (IS_ERR(bd_mnt))
327 panic("Cannot create bdev pseudo-fs");
328 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
332 * Most likely _very_ bad one - but then it's hardly critical for small
333 * /dev and can be fixed when somebody will need really large one.
334 * Keep in mind that it will be fed through icache hash function too.
336 static inline unsigned long hash(dev_t dev)
338 return MAJOR(dev)+MINOR(dev);
341 static int bdev_test(struct inode *inode, void *data)
343 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
346 static int bdev_set(struct inode *inode, void *data)
348 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
349 return 0;
352 static LIST_HEAD(all_bdevs);
354 struct block_device *bdget(dev_t dev)
356 struct block_device *bdev;
357 struct inode *inode;
359 inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
360 bdev_test, bdev_set, &dev);
362 if (!inode)
363 return NULL;
365 bdev = &BDEV_I(inode)->bdev;
367 if (inode->i_state & I_NEW) {
368 bdev->bd_contains = NULL;
369 bdev->bd_inode = inode;
370 bdev->bd_block_size = (1 << inode->i_blkbits);
371 bdev->bd_part_count = 0;
372 bdev->bd_invalidated = 0;
373 inode->i_mode = S_IFBLK;
374 inode->i_rdev = dev;
375 inode->i_bdev = bdev;
376 inode->i_data.a_ops = &def_blk_aops;
377 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
378 inode->i_data.backing_dev_info = &default_backing_dev_info;
379 spin_lock(&bdev_lock);
380 list_add(&bdev->bd_list, &all_bdevs);
381 spin_unlock(&bdev_lock);
382 unlock_new_inode(inode);
384 return bdev;
387 EXPORT_SYMBOL(bdget);
389 long nr_blockdev_pages(void)
391 struct list_head *p;
392 long ret = 0;
393 spin_lock(&bdev_lock);
394 list_for_each(p, &all_bdevs) {
395 struct block_device *bdev;
396 bdev = list_entry(p, struct block_device, bd_list);
397 ret += bdev->bd_inode->i_mapping->nrpages;
399 spin_unlock(&bdev_lock);
400 return ret;
403 void bdput(struct block_device *bdev)
405 iput(bdev->bd_inode);
408 EXPORT_SYMBOL(bdput);
410 static struct block_device *bd_acquire(struct inode *inode)
412 struct block_device *bdev;
413 spin_lock(&bdev_lock);
414 bdev = inode->i_bdev;
415 if (bdev && igrab(bdev->bd_inode)) {
416 spin_unlock(&bdev_lock);
417 return bdev;
419 spin_unlock(&bdev_lock);
420 bdev = bdget(inode->i_rdev);
421 if (bdev) {
422 spin_lock(&bdev_lock);
423 if (inode->i_bdev)
424 __bd_forget(inode);
425 inode->i_bdev = bdev;
426 inode->i_mapping = bdev->bd_inode->i_mapping;
427 list_add(&inode->i_devices, &bdev->bd_inodes);
428 spin_unlock(&bdev_lock);
430 return bdev;
433 /* Call when you free inode */
435 void bd_forget(struct inode *inode)
437 spin_lock(&bdev_lock);
438 if (inode->i_bdev)
439 __bd_forget(inode);
440 spin_unlock(&bdev_lock);
443 int bd_claim(struct block_device *bdev, void *holder)
445 int res;
446 spin_lock(&bdev_lock);
448 /* first decide result */
449 if (bdev->bd_holder == holder)
450 res = 0; /* already a holder */
451 else if (bdev->bd_holder != NULL)
452 res = -EBUSY; /* held by someone else */
453 else if (bdev->bd_contains == bdev)
454 res = 0; /* is a whole device which isn't held */
456 else if (bdev->bd_contains->bd_holder == bd_claim)
457 res = 0; /* is a partition of a device that is being partitioned */
458 else if (bdev->bd_contains->bd_holder != NULL)
459 res = -EBUSY; /* is a partition of a held device */
460 else
461 res = 0; /* is a partition of an un-held device */
463 /* now impose change */
464 if (res==0) {
465 /* note that for a whole device bd_holders
466 * will be incremented twice, and bd_holder will
467 * be set to bd_claim before being set to holder
469 bdev->bd_contains->bd_holders ++;
470 bdev->bd_contains->bd_holder = bd_claim;
471 bdev->bd_holders++;
472 bdev->bd_holder = holder;
474 spin_unlock(&bdev_lock);
475 return res;
478 EXPORT_SYMBOL(bd_claim);
480 void bd_release(struct block_device *bdev)
482 spin_lock(&bdev_lock);
483 if (!--bdev->bd_contains->bd_holders)
484 bdev->bd_contains->bd_holder = NULL;
485 if (!--bdev->bd_holders)
486 bdev->bd_holder = NULL;
487 spin_unlock(&bdev_lock);
490 EXPORT_SYMBOL(bd_release);
493 * Tries to open block device by device number. Use it ONLY if you
494 * really do not have anything better - i.e. when you are behind a
495 * truly sucky interface and all you are given is a device number. _Never_
496 * to be used for internal purposes. If you ever need it - reconsider
497 * your API.
499 struct block_device *open_by_devnum(dev_t dev, unsigned mode)
501 struct block_device *bdev = bdget(dev);
502 int err = -ENOMEM;
503 int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
504 if (bdev)
505 err = blkdev_get(bdev, mode, flags);
506 return err ? ERR_PTR(err) : bdev;
509 EXPORT_SYMBOL(open_by_devnum);
512 * This routine checks whether a removable media has been changed,
513 * and invalidates all buffer-cache-entries in that case. This
514 * is a relatively slow routine, so we have to try to minimize using
515 * it. Thus it is called only upon a 'mount' or 'open'. This
516 * is the best way of combining speed and utility, I think.
517 * People changing diskettes in the middle of an operation deserve
518 * to lose :-)
520 int check_disk_change(struct block_device *bdev)
522 struct gendisk *disk = bdev->bd_disk;
523 struct block_device_operations * bdops = disk->fops;
525 if (!bdops->media_changed)
526 return 0;
527 if (!bdops->media_changed(bdev->bd_disk))
528 return 0;
530 if (__invalidate_device(bdev))
531 printk("VFS: busy inodes on changed media.\n");
533 if (bdops->revalidate_disk)
534 bdops->revalidate_disk(bdev->bd_disk);
535 if (bdev->bd_disk->minors > 1)
536 bdev->bd_invalidated = 1;
537 return 1;
540 EXPORT_SYMBOL(check_disk_change);
542 void bd_set_size(struct block_device *bdev, loff_t size)
544 unsigned bsize = bdev_hardsect_size(bdev);
546 bdev->bd_inode->i_size = size;
547 while (bsize < PAGE_CACHE_SIZE) {
548 if (size & bsize)
549 break;
550 bsize <<= 1;
552 bdev->bd_block_size = bsize;
553 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
555 EXPORT_SYMBOL(bd_set_size);
557 static int do_open(struct block_device *bdev, struct file *file)
559 struct module *owner = NULL;
560 struct gendisk *disk;
561 int ret = -ENXIO;
562 int part;
564 file->f_mapping = bdev->bd_inode->i_mapping;
565 lock_kernel();
566 disk = get_gendisk(bdev->bd_dev, &part);
567 if (!disk) {
568 unlock_kernel();
569 bdput(bdev);
570 return ret;
572 owner = disk->fops->owner;
574 mutex_lock(&bdev->bd_mutex);
575 if (!bdev->bd_openers) {
576 bdev->bd_disk = disk;
577 bdev->bd_contains = bdev;
578 if (!part) {
579 struct backing_dev_info *bdi;
580 if (disk->fops->open) {
581 ret = disk->fops->open(bdev->bd_inode, file);
582 if (ret)
583 goto out_first;
585 if (!bdev->bd_openers) {
586 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
587 bdi = blk_get_backing_dev_info(bdev);
588 if (bdi == NULL)
589 bdi = &default_backing_dev_info;
590 bdev->bd_inode->i_data.backing_dev_info = bdi;
592 if (bdev->bd_invalidated)
593 rescan_partitions(disk, bdev);
594 } else {
595 struct hd_struct *p;
596 struct block_device *whole;
597 whole = bdget_disk(disk, 0);
598 ret = -ENOMEM;
599 if (!whole)
600 goto out_first;
601 ret = blkdev_get(whole, file->f_mode, file->f_flags);
602 if (ret)
603 goto out_first;
604 bdev->bd_contains = whole;
605 mutex_lock(&whole->bd_mutex);
606 whole->bd_part_count++;
607 p = disk->part[part - 1];
608 bdev->bd_inode->i_data.backing_dev_info =
609 whole->bd_inode->i_data.backing_dev_info;
610 if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) {
611 whole->bd_part_count--;
612 mutex_unlock(&whole->bd_mutex);
613 ret = -ENXIO;
614 goto out_first;
616 kobject_get(&p->kobj);
617 bdev->bd_part = p;
618 bd_set_size(bdev, (loff_t) p->nr_sects << 9);
619 mutex_unlock(&whole->bd_mutex);
621 } else {
622 put_disk(disk);
623 module_put(owner);
624 if (bdev->bd_contains == bdev) {
625 if (bdev->bd_disk->fops->open) {
626 ret = bdev->bd_disk->fops->open(bdev->bd_inode, file);
627 if (ret)
628 goto out;
630 if (bdev->bd_invalidated)
631 rescan_partitions(bdev->bd_disk, bdev);
632 } else {
633 mutex_lock(&bdev->bd_contains->bd_mutex);
634 bdev->bd_contains->bd_part_count++;
635 mutex_unlock(&bdev->bd_contains->bd_mutex);
638 bdev->bd_openers++;
639 mutex_unlock(&bdev->bd_mutex);
640 unlock_kernel();
641 return 0;
643 out_first:
644 bdev->bd_disk = NULL;
645 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
646 if (bdev != bdev->bd_contains)
647 blkdev_put(bdev->bd_contains);
648 bdev->bd_contains = NULL;
649 put_disk(disk);
650 module_put(owner);
651 out:
652 mutex_unlock(&bdev->bd_mutex);
653 unlock_kernel();
654 if (ret)
655 bdput(bdev);
656 return ret;
659 int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags)
662 * This crockload is due to bad choice of ->open() type.
663 * It will go away.
664 * For now, block device ->open() routine must _not_
665 * examine anything in 'inode' argument except ->i_rdev.
667 struct file fake_file = {};
668 struct dentry fake_dentry = {};
669 fake_file.f_mode = mode;
670 fake_file.f_flags = flags;
671 fake_file.f_dentry = &fake_dentry;
672 fake_dentry.d_inode = bdev->bd_inode;
674 return do_open(bdev, &fake_file);
677 EXPORT_SYMBOL(blkdev_get);
679 static int blkdev_open(struct inode * inode, struct file * filp)
681 struct block_device *bdev;
682 int res;
685 * Preserve backwards compatibility and allow large file access
686 * even if userspace doesn't ask for it explicitly. Some mkfs
687 * binary needs it. We might want to drop this workaround
688 * during an unstable branch.
690 filp->f_flags |= O_LARGEFILE;
692 bdev = bd_acquire(inode);
694 res = do_open(bdev, filp);
695 if (res)
696 return res;
698 if (!(filp->f_flags & O_EXCL) )
699 return 0;
701 if (!(res = bd_claim(bdev, filp)))
702 return 0;
704 blkdev_put(bdev);
705 return res;
708 int blkdev_put(struct block_device *bdev)
710 int ret = 0;
711 struct inode *bd_inode = bdev->bd_inode;
712 struct gendisk *disk = bdev->bd_disk;
714 mutex_lock(&bdev->bd_mutex);
715 lock_kernel();
716 if (!--bdev->bd_openers) {
717 sync_blockdev(bdev);
718 kill_bdev(bdev);
720 if (bdev->bd_contains == bdev) {
721 if (disk->fops->release)
722 ret = disk->fops->release(bd_inode, NULL);
723 } else {
724 mutex_lock(&bdev->bd_contains->bd_mutex);
725 bdev->bd_contains->bd_part_count--;
726 mutex_unlock(&bdev->bd_contains->bd_mutex);
728 if (!bdev->bd_openers) {
729 struct module *owner = disk->fops->owner;
731 put_disk(disk);
732 module_put(owner);
734 if (bdev->bd_contains != bdev) {
735 kobject_put(&bdev->bd_part->kobj);
736 bdev->bd_part = NULL;
738 bdev->bd_disk = NULL;
739 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
740 if (bdev != bdev->bd_contains) {
741 blkdev_put(bdev->bd_contains);
743 bdev->bd_contains = NULL;
745 unlock_kernel();
746 mutex_unlock(&bdev->bd_mutex);
747 bdput(bdev);
748 return ret;
751 EXPORT_SYMBOL(blkdev_put);
753 static int blkdev_close(struct inode * inode, struct file * filp)
755 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
756 if (bdev->bd_holder == filp)
757 bd_release(bdev);
758 return blkdev_put(bdev);
761 static ssize_t blkdev_file_write(struct file *file, const char __user *buf,
762 size_t count, loff_t *ppos)
764 struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
766 return generic_file_write_nolock(file, &local_iov, 1, ppos);
769 static ssize_t blkdev_file_aio_write(struct kiocb *iocb, const char __user *buf,
770 size_t count, loff_t pos)
772 struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
774 return generic_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos);
777 static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
779 return blkdev_ioctl(file->f_mapping->host, file, cmd, arg);
782 struct address_space_operations def_blk_aops = {
783 .readpage = blkdev_readpage,
784 .writepage = blkdev_writepage,
785 .sync_page = block_sync_page,
786 .prepare_write = blkdev_prepare_write,
787 .commit_write = blkdev_commit_write,
788 .writepages = generic_writepages,
789 .direct_IO = blkdev_direct_IO,
792 struct file_operations def_blk_fops = {
793 .open = blkdev_open,
794 .release = blkdev_close,
795 .llseek = block_llseek,
796 .read = generic_file_read,
797 .write = blkdev_file_write,
798 .aio_read = generic_file_aio_read,
799 .aio_write = blkdev_file_aio_write,
800 .mmap = generic_file_mmap,
801 .fsync = block_fsync,
802 .unlocked_ioctl = block_ioctl,
803 #ifdef CONFIG_COMPAT
804 .compat_ioctl = compat_blkdev_ioctl,
805 #endif
806 .readv = generic_file_readv,
807 .writev = generic_file_write_nolock,
808 .sendfile = generic_file_sendfile,
811 int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
813 int res;
814 mm_segment_t old_fs = get_fs();
815 set_fs(KERNEL_DS);
816 res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg);
817 set_fs(old_fs);
818 return res;
821 EXPORT_SYMBOL(ioctl_by_bdev);
824 * lookup_bdev - lookup a struct block_device by name
826 * @path: special file representing the block device
828 * Get a reference to the blockdevice at @path in the current
829 * namespace if possible and return it. Return ERR_PTR(error)
830 * otherwise.
832 struct block_device *lookup_bdev(const char *path)
834 struct block_device *bdev;
835 struct inode *inode;
836 struct nameidata nd;
837 int error;
839 if (!path || !*path)
840 return ERR_PTR(-EINVAL);
842 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
843 if (error)
844 return ERR_PTR(error);
846 inode = nd.dentry->d_inode;
847 error = -ENOTBLK;
848 if (!S_ISBLK(inode->i_mode))
849 goto fail;
850 error = -EACCES;
851 if (nd.mnt->mnt_flags & MNT_NODEV)
852 goto fail;
853 error = -ENOMEM;
854 bdev = bd_acquire(inode);
855 if (!bdev)
856 goto fail;
857 out:
858 path_release(&nd);
859 return bdev;
860 fail:
861 bdev = ERR_PTR(error);
862 goto out;
866 * open_bdev_excl - open a block device by name and set it up for use
868 * @path: special file representing the block device
869 * @flags: %MS_RDONLY for opening read-only
870 * @holder: owner for exclusion
872 * Open the blockdevice described by the special file at @path, claim it
873 * for the @holder.
875 struct block_device *open_bdev_excl(const char *path, int flags, void *holder)
877 struct block_device *bdev;
878 mode_t mode = FMODE_READ;
879 int error = 0;
881 bdev = lookup_bdev(path);
882 if (IS_ERR(bdev))
883 return bdev;
885 if (!(flags & MS_RDONLY))
886 mode |= FMODE_WRITE;
887 error = blkdev_get(bdev, mode, 0);
888 if (error)
889 return ERR_PTR(error);
890 error = -EACCES;
891 if (!(flags & MS_RDONLY) && bdev_read_only(bdev))
892 goto blkdev_put;
893 error = bd_claim(bdev, holder);
894 if (error)
895 goto blkdev_put;
897 return bdev;
899 blkdev_put:
900 blkdev_put(bdev);
901 return ERR_PTR(error);
904 EXPORT_SYMBOL(open_bdev_excl);
907 * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
909 * @bdev: blockdevice to close
911 * This is the counterpart to open_bdev_excl().
913 void close_bdev_excl(struct block_device *bdev)
915 bd_release(bdev);
916 blkdev_put(bdev);
919 EXPORT_SYMBOL(close_bdev_excl);