1
2
3
4
5
6
7
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/smp_lock.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/mpage.h>
21#include <linux/mount.h>
22#include <linux/uio.h>
23#include <linux/namei.h>
24#include <asm/uaccess.h>
25
26struct bdev_inode {
27 struct block_device bdev;
28 struct inode vfs_inode;
29};
30
31static inline struct bdev_inode *BDEV_I(struct inode *inode)
32{
33 return container_of(inode, struct bdev_inode, vfs_inode);
34}
35
36inline struct block_device *I_BDEV(struct inode *inode)
37{
38 return &BDEV_I(inode)->bdev;
39}
40
41EXPORT_SYMBOL(I_BDEV);
42
43static sector_t max_block(struct block_device *bdev)
44{
45 sector_t retval = ~((sector_t)0);
46 loff_t sz = i_size_read(bdev->bd_inode);
47
48 if (sz) {
49 unsigned int size = block_size(bdev);
50 unsigned int sizebits = blksize_bits(size);
51 retval = (sz >> sizebits);
52 }
53 return retval;
54}
55
56
57static void kill_bdev(struct block_device *bdev)
58{
59 invalidate_bdev(bdev, 1);
60 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
61}
62
63int set_blocksize(struct block_device *bdev, int size)
64{
65
66 if (size > PAGE_SIZE || size < 512 || (size & (size-1)))
67 return -EINVAL;
68
69
70 if (size < bdev_hardsect_size(bdev))
71 return -EINVAL;
72
73
74 if (bdev->bd_block_size != size) {
75 sync_blockdev(bdev);
76 bdev->bd_block_size = size;
77 bdev->bd_inode->i_blkbits = blksize_bits(size);
78 kill_bdev(bdev);
79 }
80 return 0;
81}
82
83EXPORT_SYMBOL(set_blocksize);
84
85int sb_set_blocksize(struct super_block *sb, int size)
86{
87 if (set_blocksize(sb->s_bdev, size))
88 return 0;
89
90
91 sb->s_blocksize = size;
92 sb->s_blocksize_bits = blksize_bits(size);
93 return sb->s_blocksize;
94}
95
96EXPORT_SYMBOL(sb_set_blocksize);
97
98int sb_min_blocksize(struct super_block *sb, int size)
99{
100 int minsize = bdev_hardsect_size(sb->s_bdev);
101 if (size < minsize)
102 size = minsize;
103 return sb_set_blocksize(sb, size);
104}
105
106EXPORT_SYMBOL(sb_min_blocksize);
107
108static int
109blkdev_get_block(struct inode *inode, sector_t iblock,
110 struct buffer_head *bh, int create)
111{
112 if (iblock >= max_block(I_BDEV(inode))) {
113 if (create)
114 return -EIO;
115
116
117
118
119
120
121
122 return 0;
123 }
124 bh->b_bdev = I_BDEV(inode);
125 bh->b_blocknr = iblock;
126 set_buffer_mapped(bh);
127 return 0;
128}
129
130static int
131blkdev_get_blocks(struct inode *inode, sector_t iblock,
132 struct buffer_head *bh, int create)
133{
134 sector_t end_block = max_block(I_BDEV(inode));
135 unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
136
137 if ((iblock + max_blocks) > end_block) {
138 max_blocks = end_block - iblock;
139 if ((long)max_blocks <= 0) {
140 if (create)
141 return -EIO;
142
143
144
145
146 max_blocks = 0;
147 }
148 }
149
150 bh->b_bdev = I_BDEV(inode);
151 bh->b_blocknr = iblock;
152 bh->b_size = max_blocks << inode->i_blkbits;
153 if (max_blocks)
154 set_buffer_mapped(bh);
155 return 0;
156}
157
158static ssize_t
159blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
160 loff_t offset, unsigned long nr_segs)
161{
162 struct file *file = iocb->ki_filp;
163 struct inode *inode = file->f_mapping->host;
164
165 return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode),
166 iov, offset, nr_segs, blkdev_get_blocks, NULL);
167}
168
169static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
170{
171 return block_write_full_page(page, blkdev_get_block, wbc);
172}
173
174static int blkdev_readpage(struct file * file, struct page * page)
175{
176 return block_read_full_page(page, blkdev_get_block);
177}
178
179static int blkdev_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
180{
181 return block_prepare_write(page, from, to, blkdev_get_block);
182}
183
184static int blkdev_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
185{
186 return block_commit_write(page, from, to);
187}
188
189
190
191
192
193
194static loff_t block_llseek(struct file *file, loff_t offset, int origin)
195{
196 struct inode *bd_inode = file->f_mapping->host;
197 loff_t size;
198 loff_t retval;
199
200 mutex_lock(&bd_inode->i_mutex);
201 size = i_size_read(bd_inode);
202
203 switch (origin) {
204 case 2:
205 offset += size;
206 break;
207 case 1:
208 offset += file->f_pos;
209 }
210 retval = -EINVAL;
211 if (offset >= 0 && offset <= size) {
212 if (offset != file->f_pos) {
213 file->f_pos = offset;
214 }
215 retval = offset;
216 }
217 mutex_unlock(&bd_inode->i_mutex);
218 return retval;
219}
220
221
222
223
224
225
226static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
227{
228 return sync_blockdev(I_BDEV(filp->f_mapping->host));
229}
230
231
232
233
234
235static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
236static kmem_cache_t * bdev_cachep __read_mostly;
237
238static struct inode *bdev_alloc_inode(struct super_block *sb)
239{
240 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, SLAB_KERNEL);
241 if (!ei)
242 return NULL;
243 return &ei->vfs_inode;
244}
245
246static void bdev_destroy_inode(struct inode *inode)
247{
248 struct bdev_inode *bdi = BDEV_I(inode);
249
250 bdi->bdev.bd_inode_backing_dev_info = NULL;
251 kmem_cache_free(bdev_cachep, bdi);
252}
253
254static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
255{
256 struct bdev_inode *ei = (struct bdev_inode *) foo;
257 struct block_device *bdev = &ei->bdev;
258
259 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
260 SLAB_CTOR_CONSTRUCTOR)
261 {
262 memset(bdev, 0, sizeof(*bdev));
263 mutex_init(&bdev->bd_mutex);
264 sema_init(&bdev->bd_mount_sem, 1);
265 INIT_LIST_HEAD(&bdev->bd_inodes);
266 INIT_LIST_HEAD(&bdev->bd_list);
267#ifdef CONFIG_SYSFS
268 INIT_LIST_HEAD(&bdev->bd_holder_list);
269#endif
270 inode_init_once(&ei->vfs_inode);
271 }
272}
273
274static inline void __bd_forget(struct inode *inode)
275{
276 list_del_init(&inode->i_devices);
277 inode->i_bdev = NULL;
278 inode->i_mapping = &inode->i_data;
279}
280
281static void bdev_clear_inode(struct inode *inode)
282{
283 struct block_device *bdev = &BDEV_I(inode)->bdev;
284 struct list_head *p;
285 spin_lock(&bdev_lock);
286 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
287 __bd_forget(list_entry(p, struct inode, i_devices));
288 }
289 list_del_init(&bdev->bd_list);
290 spin_unlock(&bdev_lock);
291}
292
293static struct super_operations bdev_sops = {
294 .statfs = simple_statfs,
295 .alloc_inode = bdev_alloc_inode,
296 .destroy_inode = bdev_destroy_inode,
297 .drop_inode = generic_delete_inode,
298 .clear_inode = bdev_clear_inode,
299};
300
301static int bd_get_sb(struct file_system_type *fs_type,
302 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
303{
304 return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
305}
306
307static struct file_system_type bd_type = {
308 .name = "bdev",
309 .get_sb = bd_get_sb,
310 .kill_sb = kill_anon_super,
311};
312
313static struct vfsmount *bd_mnt __read_mostly;
314struct super_block *blockdev_superblock;
315
316void __init bdev_cache_init(void)
317{
318 int err;
319 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
320 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
321 SLAB_MEM_SPREAD|SLAB_PANIC),
322 init_once, NULL);
323 err = register_filesystem(&bd_type);
324 if (err)
325 panic("Cannot register bdev pseudo-fs");
326 bd_mnt = kern_mount(&bd_type);
327 err = PTR_ERR(bd_mnt);
328 if (IS_ERR(bd_mnt))
329 panic("Cannot create bdev pseudo-fs");
330 blockdev_superblock = bd_mnt->mnt_sb;
331}
332
333
334
335
336
337
338static inline unsigned long hash(dev_t dev)
339{
340 return MAJOR(dev)+MINOR(dev);
341}
342
343static int bdev_test(struct inode *inode, void *data)
344{
345 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
346}
347
348static int bdev_set(struct inode *inode, void *data)
349{
350 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
351 return 0;
352}
353
354static LIST_HEAD(all_bdevs);
355
356struct block_device *bdget(dev_t dev)
357{
358 struct block_device *bdev;
359 struct inode *inode;
360
361 inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
362 bdev_test, bdev_set, &dev);
363
364 if (!inode)
365 return NULL;
366
367 bdev = &BDEV_I(inode)->bdev;
368
369 if (inode->i_state & I_NEW) {
370 bdev->bd_contains = NULL;
371 bdev->bd_inode = inode;
372 bdev->bd_block_size = (1 << inode->i_blkbits);
373 bdev->bd_part_count = 0;
374 bdev->bd_invalidated = 0;
375 inode->i_mode = S_IFBLK;
376 inode->i_rdev = dev;
377 inode->i_bdev = bdev;
378 inode->i_data.a_ops = &def_blk_aops;
379 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
380 inode->i_data.backing_dev_info = &default_backing_dev_info;
381 spin_lock(&bdev_lock);
382 list_add(&bdev->bd_list, &all_bdevs);
383 spin_unlock(&bdev_lock);
384 unlock_new_inode(inode);
385 }
386 return bdev;
387}
388
389EXPORT_SYMBOL(bdget);
390
391long nr_blockdev_pages(void)
392{
393 struct list_head *p;
394 long ret = 0;
395 spin_lock(&bdev_lock);
396 list_for_each(p, &all_bdevs) {
397 struct block_device *bdev;
398 bdev = list_entry(p, struct block_device, bd_list);
399 ret += bdev->bd_inode->i_mapping->nrpages;
400 }
401 spin_unlock(&bdev_lock);
402 return ret;
403}
404
405void bdput(struct block_device *bdev)
406{
407 iput(bdev->bd_inode);
408}
409
410EXPORT_SYMBOL(bdput);
411
412static struct block_device *bd_acquire(struct inode *inode)
413{
414 struct block_device *bdev;
415
416 spin_lock(&bdev_lock);
417 bdev = inode->i_bdev;
418 if (bdev) {
419 atomic_inc(&bdev->bd_inode->i_count);
420 spin_unlock(&bdev_lock);
421 return bdev;
422 }
423 spin_unlock(&bdev_lock);
424
425 bdev = bdget(inode->i_rdev);
426 if (bdev) {
427 spin_lock(&bdev_lock);
428 if (!inode->i_bdev) {
429
430
431
432
433
434
435 atomic_inc(&bdev->bd_inode->i_count);
436 inode->i_bdev = bdev;
437 inode->i_mapping = bdev->bd_inode->i_mapping;
438 list_add(&inode->i_devices, &bdev->bd_inodes);
439 }
440 spin_unlock(&bdev_lock);
441 }
442 return bdev;
443}
444
445
446
447void bd_forget(struct inode *inode)
448{
449 struct block_device *bdev = NULL;
450
451 spin_lock(&bdev_lock);
452 if (inode->i_bdev) {
453 if (inode->i_sb != blockdev_superblock)
454 bdev = inode->i_bdev;
455 __bd_forget(inode);
456 }
457 spin_unlock(&bdev_lock);
458
459 if (bdev)
460 iput(bdev->bd_inode);
461}
462
463int bd_claim(struct block_device *bdev, void *holder)
464{
465 int res;
466 spin_lock(&bdev_lock);
467
468
469 if (bdev->bd_holder == holder)
470 res = 0;
471 else if (bdev->bd_holder != NULL)
472 res = -EBUSY;
473 else if (bdev->bd_contains == bdev)
474 res = 0;
475
476 else if (bdev->bd_contains->bd_holder == bd_claim)
477 res = 0;
478 else if (bdev->bd_contains->bd_holder != NULL)
479 res = -EBUSY;
480 else
481 res = 0;
482
483
484 if (res==0) {
485
486
487
488
489 bdev->bd_contains->bd_holders ++;
490 bdev->bd_contains->bd_holder = bd_claim;
491 bdev->bd_holders++;
492 bdev->bd_holder = holder;
493 }
494 spin_unlock(&bdev_lock);
495 return res;
496}
497
498EXPORT_SYMBOL(bd_claim);
499
500void bd_release(struct block_device *bdev)
501{
502 spin_lock(&bdev_lock);
503 if (!--bdev->bd_contains->bd_holders)
504 bdev->bd_contains->bd_holder = NULL;
505 if (!--bdev->bd_holders)
506 bdev->bd_holder = NULL;
507 spin_unlock(&bdev_lock);
508}
509
510EXPORT_SYMBOL(bd_release);
511
512#ifdef CONFIG_SYSFS
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530static struct kobject *bdev_get_kobj(struct block_device *bdev)
531{
532 if (bdev->bd_contains != bdev)
533 return kobject_get(&bdev->bd_part->kobj);
534 else
535 return kobject_get(&bdev->bd_disk->kobj);
536}
537
538static struct kobject *bdev_get_holder(struct block_device *bdev)
539{
540 if (bdev->bd_contains != bdev)
541 return kobject_get(bdev->bd_part->holder_dir);
542 else
543 return kobject_get(bdev->bd_disk->holder_dir);
544}
545
546static void add_symlink(struct kobject *from, struct kobject *to)
547{
548 if (!from || !to)
549 return;
550 sysfs_create_link(from, to, kobject_name(to));
551}
552
553static void del_symlink(struct kobject *from, struct kobject *to)
554{
555 if (!from || !to)
556 return;
557 sysfs_remove_link(from, kobject_name(to));
558}
559
560
561
562
563
564
565struct bd_holder {
566 struct list_head list;
567 int count;
568 struct kobject *sdir;
569 struct kobject *hdev;
570 struct kobject *hdir;
571 struct kobject *sdev;
572};
573
574
575
576
577
578
579
580static int bd_holder_grab_dirs(struct block_device *bdev,
581 struct bd_holder *bo)
582{
583 if (!bdev || !bo)
584 return 0;
585
586 bo->sdir = kobject_get(bo->sdir);
587 if (!bo->sdir)
588 return 0;
589
590 bo->hdev = kobject_get(bo->sdir->parent);
591 if (!bo->hdev)
592 goto fail_put_sdir;
593
594 bo->sdev = bdev_get_kobj(bdev);
595 if (!bo->sdev)
596 goto fail_put_hdev;
597
598 bo->hdir = bdev_get_holder(bdev);
599 if (!bo->hdir)
600 goto fail_put_sdev;
601
602 return 1;
603
604fail_put_sdev:
605 kobject_put(bo->sdev);
606fail_put_hdev:
607 kobject_put(bo->hdev);
608fail_put_sdir:
609 kobject_put(bo->sdir);
610
611 return 0;
612}
613
614
615static void bd_holder_release_dirs(struct bd_holder *bo)
616{
617 kobject_put(bo->hdir);
618 kobject_put(bo->sdev);
619 kobject_put(bo->hdev);
620 kobject_put(bo->sdir);
621}
622
623static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
624{
625 struct bd_holder *bo;
626
627 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
628 if (!bo)
629 return NULL;
630
631 bo->count = 1;
632 bo->sdir = kobj;
633
634 return bo;
635}
636
637static void free_bd_holder(struct bd_holder *bo)
638{
639 kfree(bo);
640}
641
642
643
644
645
646
647
648
649
650
651
652
653
654static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
655{
656 struct bd_holder *tmp;
657
658 if (!bo)
659 return 0;
660
661 list_for_each_entry(tmp, &bdev->bd_holder_list, list) {
662 if (tmp->sdir == bo->sdir) {
663 tmp->count++;
664 return 0;
665 }
666 }
667
668 if (!bd_holder_grab_dirs(bdev, bo))
669 return 0;
670
671 add_symlink(bo->sdir, bo->sdev);
672 add_symlink(bo->hdir, bo->hdev);
673 list_add_tail(&bo->list, &bdev->bd_holder_list);
674 return 1;
675}
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692static struct bd_holder *del_bd_holder(struct block_device *bdev,
693 struct kobject *kobj)
694{
695 struct bd_holder *bo;
696
697 list_for_each_entry(bo, &bdev->bd_holder_list, list) {
698 if (bo->sdir == kobj) {
699 bo->count--;
700 BUG_ON(bo->count < 0);
701 if (!bo->count) {
702 list_del(&bo->list);
703 del_symlink(bo->sdir, bo->sdev);
704 del_symlink(bo->hdir, bo->hdev);
705 bd_holder_release_dirs(bo);
706 return bo;
707 }
708 break;
709 }
710 }
711
712 return NULL;
713}
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
730 struct kobject *kobj)
731{
732 int res;
733 struct bd_holder *bo;
734
735 if (!kobj)
736 return -EINVAL;
737
738 bo = alloc_bd_holder(kobj);
739 if (!bo)
740 return -ENOMEM;
741
742 mutex_lock(&bdev->bd_mutex);
743 res = bd_claim(bdev, holder);
744 if (res || !add_bd_holder(bdev, bo))
745 free_bd_holder(bo);
746 mutex_unlock(&bdev->bd_mutex);
747
748 return res;
749}
750
751
752
753
754
755
756
757
758
759static void bd_release_from_kobject(struct block_device *bdev,
760 struct kobject *kobj)
761{
762 struct bd_holder *bo;
763
764 if (!kobj)
765 return;
766
767 mutex_lock(&bdev->bd_mutex);
768 bd_release(bdev);
769 if ((bo = del_bd_holder(bdev, kobj)))
770 free_bd_holder(bo);
771 mutex_unlock(&bdev->bd_mutex);
772}
773
774
775
776
777
778
779
780
781
782
783int bd_claim_by_disk(struct block_device *bdev, void *holder,
784 struct gendisk *disk)
785{
786 return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
787}
788EXPORT_SYMBOL_GPL(bd_claim_by_disk);
789
790
791
792
793
794
795
796
797
798void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
799{
800 bd_release_from_kobject(bdev, disk->slave_dir);
801 kobject_put(disk->slave_dir);
802}
803EXPORT_SYMBOL_GPL(bd_release_from_disk);
804#endif
805
806
807
808
809
810
811
812
813struct block_device *open_by_devnum(dev_t dev, unsigned mode)
814{
815 struct block_device *bdev = bdget(dev);
816 int err = -ENOMEM;
817 int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
818 if (bdev)
819 err = blkdev_get(bdev, mode, flags);
820 return err ? ERR_PTR(err) : bdev;
821}
822
823EXPORT_SYMBOL(open_by_devnum);
824
825
826
827
828
829
830
831
832
833
834int check_disk_change(struct block_device *bdev)
835{
836 struct gendisk *disk = bdev->bd_disk;
837 struct block_device_operations * bdops = disk->fops;
838
839 if (!bdops->media_changed)
840 return 0;
841 if (!bdops->media_changed(bdev->bd_disk))
842 return 0;
843
844 if (__invalidate_device(bdev))
845 printk("VFS: busy inodes on changed media.\n");
846
847 if (bdops->revalidate_disk)
848 bdops->revalidate_disk(bdev->bd_disk);
849 if (bdev->bd_disk->minors > 1)
850 bdev->bd_invalidated = 1;
851 return 1;
852}
853
854EXPORT_SYMBOL(check_disk_change);
855
856void bd_set_size(struct block_device *bdev, loff_t size)
857{
858 unsigned bsize = bdev_hardsect_size(bdev);
859
860 bdev->bd_inode->i_size = size;
861 while (bsize < PAGE_CACHE_SIZE) {
862 if (size & bsize)
863 break;
864 bsize <<= 1;
865 }
866 bdev->bd_block_size = bsize;
867 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
868}
869EXPORT_SYMBOL(bd_set_size);
870
871static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags,
872 int for_part);
873static int __blkdev_put(struct block_device *bdev, int for_part);
874
875static int do_open(struct block_device *bdev, struct file *file, int for_part)
876{
877 struct module *owner = NULL;
878 struct gendisk *disk;
879 int ret = -ENXIO;
880 int part;
881
882 file->f_mapping = bdev->bd_inode->i_mapping;
883 lock_kernel();
884 disk = get_gendisk(bdev->bd_dev, &part);
885 if (!disk) {
886 unlock_kernel();
887 bdput(bdev);
888 return ret;
889 }
890 owner = disk->fops->owner;
891
892 mutex_lock_nested(&bdev->bd_mutex, for_part);
893 if (!bdev->bd_openers) {
894 bdev->bd_disk = disk;
895 bdev->bd_contains = bdev;
896 if (!part) {
897 struct backing_dev_info *bdi;
898 if (disk->fops->open) {
899 ret = disk->fops->open(bdev->bd_inode, file);
900 if (ret)
901 goto out_first;
902 }
903 if (!bdev->bd_openers) {
904 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
905 bdi = blk_get_backing_dev_info(bdev);
906 if (bdi == NULL)
907 bdi = &default_backing_dev_info;
908 bdev->bd_inode->i_data.backing_dev_info = bdi;
909 }
910 if (bdev->bd_invalidated)
911 rescan_partitions(disk, bdev);
912 } else {
913 struct hd_struct *p;
914 struct block_device *whole;
915 whole = bdget_disk(disk, 0);
916 ret = -ENOMEM;
917 if (!whole)
918 goto out_first;
919 BUG_ON(for_part);
920 ret = __blkdev_get(whole, file->f_mode, file->f_flags, 1);
921 if (ret)
922 goto out_first;
923 bdev->bd_contains = whole;
924 p = disk->part[part - 1];
925 bdev->bd_inode->i_data.backing_dev_info =
926 whole->bd_inode->i_data.backing_dev_info;
927 if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) {
928 ret = -ENXIO;
929 goto out_first;
930 }
931 kobject_get(&p->kobj);
932 bdev->bd_part = p;
933 bd_set_size(bdev, (loff_t) p->nr_sects << 9);
934 }
935 } else {
936 put_disk(disk);
937 module_put(owner);
938 if (bdev->bd_contains == bdev) {
939 if (bdev->bd_disk->fops->open) {
940 ret = bdev->bd_disk->fops->open(bdev->bd_inode, file);
941 if (ret)
942 goto out;
943 }
944 if (bdev->bd_invalidated)
945 rescan_partitions(bdev->bd_disk, bdev);
946 }
947 }
948 bdev->bd_openers++;
949 if (for_part)
950 bdev->bd_part_count++;
951 mutex_unlock(&bdev->bd_mutex);
952 unlock_kernel();
953 return 0;
954
955out_first:
956 bdev->bd_disk = NULL;
957 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
958 if (bdev != bdev->bd_contains)
959 __blkdev_put(bdev->bd_contains, 1);
960 bdev->bd_contains = NULL;
961 put_disk(disk);
962 module_put(owner);
963out:
964 mutex_unlock(&bdev->bd_mutex);
965 unlock_kernel();
966 if (ret)
967 bdput(bdev);
968 return ret;
969}
970
971static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags,
972 int for_part)
973{
974
975
976
977
978
979
980 struct file fake_file = {};
981 struct dentry fake_dentry = {};
982 fake_file.f_mode = mode;
983 fake_file.f_flags = flags;
984 fake_file.f_dentry = &fake_dentry;
985 fake_dentry.d_inode = bdev->bd_inode;
986
987 return do_open(bdev, &fake_file, for_part);
988}
989
990int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags)
991{
992 return __blkdev_get(bdev, mode, flags, 0);
993}
994EXPORT_SYMBOL(blkdev_get);
995
996static int blkdev_open(struct inode * inode, struct file * filp)
997{
998 struct block_device *bdev;
999 int res;
1000
1001
1002
1003
1004
1005
1006
1007 filp->f_flags |= O_LARGEFILE;
1008
1009 bdev = bd_acquire(inode);
1010
1011 res = do_open(bdev, filp, 0);
1012 if (res)
1013 return res;
1014
1015 if (!(filp->f_flags & O_EXCL) )
1016 return 0;
1017
1018 if (!(res = bd_claim(bdev, filp)))
1019 return 0;
1020
1021 blkdev_put(bdev);
1022 return res;
1023}
1024
1025static int __blkdev_put(struct block_device *bdev, int for_part)
1026{
1027 int ret = 0;
1028 struct inode *bd_inode = bdev->bd_inode;
1029 struct gendisk *disk = bdev->bd_disk;
1030 struct block_device *victim = NULL;
1031
1032 mutex_lock_nested(&bdev->bd_mutex, for_part);
1033 lock_kernel();
1034 if (for_part)
1035 bdev->bd_part_count--;
1036
1037 if (!--bdev->bd_openers) {
1038 sync_blockdev(bdev);
1039 kill_bdev(bdev);
1040 }
1041 if (bdev->bd_contains == bdev) {
1042 if (disk->fops->release)
1043 ret = disk->fops->release(bd_inode, NULL);
1044 }
1045 if (!bdev->bd_openers) {
1046 struct module *owner = disk->fops->owner;
1047
1048 put_disk(disk);
1049 module_put(owner);
1050
1051 if (bdev->bd_contains != bdev) {
1052 kobject_put(&bdev->bd_part->kobj);
1053 bdev->bd_part = NULL;
1054 }
1055 bdev->bd_disk = NULL;
1056 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1057 if (bdev != bdev->bd_contains)
1058 victim = bdev->bd_contains;
1059 bdev->bd_contains = NULL;
1060 }
1061 unlock_kernel();
1062 mutex_unlock(&bdev->bd_mutex);
1063 bdput(bdev);
1064 if (victim)
1065 __blkdev_put(victim, 1);
1066 return ret;
1067}
1068
1069int blkdev_put(struct block_device *bdev)
1070{
1071 return __blkdev_put(bdev, 0);
1072}
1073EXPORT_SYMBOL(blkdev_put);
1074
1075static int blkdev_close(struct inode * inode, struct file * filp)
1076{
1077 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1078 if (bdev->bd_holder == filp)
1079 bd_release(bdev);
1080 return blkdev_put(bdev);
1081}
1082
1083static ssize_t blkdev_file_write(struct file *file, const char __user *buf,
1084 size_t count, loff_t *ppos)
1085{
1086 struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
1087
1088 return generic_file_write_nolock(file, &local_iov, 1, ppos);
1089}
1090
1091static ssize_t blkdev_file_aio_write(struct kiocb *iocb, const char __user *buf,
1092 size_t count, loff_t pos)
1093{
1094 struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
1095
1096 return generic_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos);
1097}
1098
1099static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1100{
1101 return blkdev_ioctl(file->f_mapping->host, file, cmd, arg);
1102}
1103
1104const struct address_space_operations def_blk_aops = {
1105 .readpage = blkdev_readpage,
1106 .writepage = blkdev_writepage,
1107 .sync_page = block_sync_page,
1108 .prepare_write = blkdev_prepare_write,
1109 .commit_write = blkdev_commit_write,
1110 .writepages = generic_writepages,
1111 .direct_IO = blkdev_direct_IO,
1112};
1113
1114const struct file_operations def_blk_fops = {
1115 .open = blkdev_open,
1116 .release = blkdev_close,
1117 .llseek = block_llseek,
1118 .read = generic_file_read,
1119 .write = blkdev_file_write,
1120 .aio_read = generic_file_aio_read,
1121 .aio_write = blkdev_file_aio_write,
1122 .mmap = generic_file_mmap,
1123 .fsync = block_fsync,
1124 .unlocked_ioctl = block_ioctl,
1125#ifdef CONFIG_COMPAT
1126 .compat_ioctl = compat_blkdev_ioctl,
1127#endif
1128 .readv = generic_file_readv,
1129 .writev = generic_file_write_nolock,
1130 .sendfile = generic_file_sendfile,
1131 .splice_read = generic_file_splice_read,
1132 .splice_write = generic_file_splice_write,
1133};
1134
1135int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1136{
1137 int res;
1138 mm_segment_t old_fs = get_fs();
1139 set_fs(KERNEL_DS);
1140 res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg);
1141 set_fs(old_fs);
1142 return res;
1143}
1144
1145EXPORT_SYMBOL(ioctl_by_bdev);
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156struct block_device *lookup_bdev(const char *path)
1157{
1158 struct block_device *bdev;
1159 struct inode *inode;
1160 struct nameidata nd;
1161 int error;
1162
1163 if (!path || !*path)
1164 return ERR_PTR(-EINVAL);
1165
1166 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1167 if (error)
1168 return ERR_PTR(error);
1169
1170 inode = nd.dentry->d_inode;
1171 error = -ENOTBLK;
1172 if (!S_ISBLK(inode->i_mode))
1173 goto fail;
1174 error = -EACCES;
1175 if (nd.mnt->mnt_flags & MNT_NODEV)
1176 goto fail;
1177 error = -ENOMEM;
1178 bdev = bd_acquire(inode);
1179 if (!bdev)
1180 goto fail;
1181out:
1182 path_release(&nd);
1183 return bdev;
1184fail:
1185 bdev = ERR_PTR(error);
1186 goto out;
1187}
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199struct block_device *open_bdev_excl(const char *path, int flags, void *holder)
1200{
1201 struct block_device *bdev;
1202 mode_t mode = FMODE_READ;
1203 int error = 0;
1204
1205 bdev = lookup_bdev(path);
1206 if (IS_ERR(bdev))
1207 return bdev;
1208
1209 if (!(flags & MS_RDONLY))
1210 mode |= FMODE_WRITE;
1211 error = blkdev_get(bdev, mode, 0);
1212 if (error)
1213 return ERR_PTR(error);
1214 error = -EACCES;
1215 if (!(flags & MS_RDONLY) && bdev_read_only(bdev))
1216 goto blkdev_put;
1217 error = bd_claim(bdev, holder);
1218 if (error)
1219 goto blkdev_put;
1220
1221 return bdev;
1222
1223blkdev_put:
1224 blkdev_put(bdev);
1225 return ERR_PTR(error);
1226}
1227
1228EXPORT_SYMBOL(open_bdev_excl);
1229
1230
1231
1232
1233
1234
1235
1236
1237void close_bdev_excl(struct block_device *bdev)
1238{
1239 bd_release(bdev);
1240 blkdev_put(bdev);
1241}
1242
1243EXPORT_SYMBOL(close_bdev_excl);
1244