blob: 5983d42df015d2ff32983ddc46e8ae194c3e1063 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/block_dev.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
6 */
7
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>
27
28struct bdev_inode {
29 struct block_device bdev;
30 struct inode vfs_inode;
31};
32
33static inline struct bdev_inode *BDEV_I(struct inode *inode)
34{
35 return container_of(inode, struct bdev_inode, vfs_inode);
36}
37
38inline struct block_device *I_BDEV(struct inode *inode)
39{
40 return &BDEV_I(inode)->bdev;
41}
42
43EXPORT_SYMBOL(I_BDEV);
44
45static sector_t max_block(struct block_device *bdev)
46{
47 sector_t retval = ~((sector_t)0);
48 loff_t sz = i_size_read(bdev->bd_inode);
49
50 if (sz) {
51 unsigned int size = block_size(bdev);
52 unsigned int sizebits = blksize_bits(size);
53 retval = (sz >> sizebits);
54 }
55 return retval;
56}
57
58/* Kill _all_ buffers, dirty or not.. */
59static void kill_bdev(struct block_device *bdev)
60{
61 invalidate_bdev(bdev, 1);
62 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
63}
64
65int set_blocksize(struct block_device *bdev, int size)
66{
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;
70
71 /* Size cannot be smaller than the size supported by the device */
72 if (size < bdev_hardsect_size(bdev))
73 return -EINVAL;
74
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);
81 }
82 return 0;
83}
84
85EXPORT_SYMBOL(set_blocksize);
86
87int sb_set_blocksize(struct super_block *sb, int size)
88{
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 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;
Coywolf Qi Hunt38885bd2006-03-24 03:18:05 -080094 sb->s_blocksize_bits = blksize_bits(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return sb->s_blocksize;
96}
97
98EXPORT_SYMBOL(sb_set_blocksize);
99
100int sb_min_blocksize(struct super_block *sb, int size)
101{
102 int minsize = bdev_hardsect_size(sb->s_bdev);
103 if (size < minsize)
104 size = minsize;
105 return sb_set_blocksize(sb, size);
106}
107
108EXPORT_SYMBOL(sb_min_blocksize);
109
110static int
111blkdev_get_block(struct inode *inode, sector_t iblock,
112 struct buffer_head *bh, int create)
113{
114 if (iblock >= max_block(I_BDEV(inode))) {
115 if (create)
116 return -EIO;
117
118 /*
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
123 */
124 return 0;
125 }
126 bh->b_bdev = I_BDEV(inode);
127 bh->b_blocknr = iblock;
128 set_buffer_mapped(bh);
129 return 0;
130}
131
132static int
133blkdev_get_blocks(struct inode *inode, sector_t iblock,
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -0800134 struct buffer_head *bh, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 sector_t end_block = max_block(I_BDEV(inode));
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -0800137 unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 if ((iblock + max_blocks) > end_block) {
140 max_blocks = end_block - iblock;
141 if ((long)max_blocks <= 0) {
142 if (create)
143 return -EIO; /* write fully beyond EOF */
144 /*
145 * It is a read which is fully beyond EOF. We return
146 * a !buffer_mapped buffer
147 */
148 max_blocks = 0;
149 }
150 }
151
152 bh->b_bdev = I_BDEV(inode);
153 bh->b_blocknr = iblock;
154 bh->b_size = max_blocks << inode->i_blkbits;
155 if (max_blocks)
156 set_buffer_mapped(bh);
157 return 0;
158}
159
160static ssize_t
161blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
162 loff_t offset, unsigned long nr_segs)
163{
164 struct file *file = iocb->ki_filp;
165 struct inode *inode = file->f_mapping->host;
166
167 return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode),
168 iov, offset, nr_segs, blkdev_get_blocks, NULL);
169}
170
171static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
172{
173 return block_write_full_page(page, blkdev_get_block, wbc);
174}
175
176static int blkdev_readpage(struct file * file, struct page * page)
177{
178 return block_read_full_page(page, blkdev_get_block);
179}
180
181static int blkdev_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
182{
183 return block_prepare_write(page, from, to, blkdev_get_block);
184}
185
186static int blkdev_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
187{
188 return block_commit_write(page, from, to);
189}
190
191/*
192 * private llseek:
193 * for a block special file file->f_dentry->d_inode->i_size is zero
194 * so we compute the size by hand (just as in block_read/write above)
195 */
196static loff_t block_llseek(struct file *file, loff_t offset, int origin)
197{
198 struct inode *bd_inode = file->f_mapping->host;
199 loff_t size;
200 loff_t retval;
201
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800202 mutex_lock(&bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 size = i_size_read(bd_inode);
204
205 switch (origin) {
206 case 2:
207 offset += size;
208 break;
209 case 1:
210 offset += file->f_pos;
211 }
212 retval = -EINVAL;
213 if (offset >= 0 && offset <= size) {
214 if (offset != file->f_pos) {
215 file->f_pos = offset;
216 }
217 retval = offset;
218 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800219 mutex_unlock(&bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return retval;
221}
222
223/*
224 * Filp is never NULL; the only case when ->fsync() is called with
225 * NULL first argument is nfsd_sync_dir() and that's not a directory.
226 */
227
228static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
229{
230 return sync_blockdev(I_BDEV(filp->f_mapping->host));
231}
232
233/*
234 * pseudo-fs
235 */
236
237static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800238static kmem_cache_t * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240static struct inode *bdev_alloc_inode(struct super_block *sb)
241{
242 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, SLAB_KERNEL);
243 if (!ei)
244 return NULL;
245 return &ei->vfs_inode;
246}
247
248static void bdev_destroy_inode(struct inode *inode)
249{
250 struct bdev_inode *bdi = BDEV_I(inode);
251
252 bdi->bdev.bd_inode_backing_dev_info = NULL;
253 kmem_cache_free(bdev_cachep, bdi);
254}
255
256static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
257{
258 struct bdev_inode *ei = (struct bdev_inode *) foo;
259 struct block_device *bdev = &ei->bdev;
260
261 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
262 SLAB_CTOR_CONSTRUCTOR)
263 {
264 memset(bdev, 0, sizeof(*bdev));
Arjan van de Venc039e312006-03-23 03:00:28 -0800265 mutex_init(&bdev->bd_mutex);
266 mutex_init(&bdev->bd_mount_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 INIT_LIST_HEAD(&bdev->bd_inodes);
268 INIT_LIST_HEAD(&bdev->bd_list);
269 inode_init_once(&ei->vfs_inode);
270 }
271}
272
273static inline void __bd_forget(struct inode *inode)
274{
275 list_del_init(&inode->i_devices);
276 inode->i_bdev = NULL;
277 inode->i_mapping = &inode->i_data;
278}
279
280static void bdev_clear_inode(struct inode *inode)
281{
282 struct block_device *bdev = &BDEV_I(inode)->bdev;
283 struct list_head *p;
284 spin_lock(&bdev_lock);
285 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
286 __bd_forget(list_entry(p, struct inode, i_devices));
287 }
288 list_del_init(&bdev->bd_list);
289 spin_unlock(&bdev_lock);
290}
291
292static struct super_operations bdev_sops = {
293 .statfs = simple_statfs,
294 .alloc_inode = bdev_alloc_inode,
295 .destroy_inode = bdev_destroy_inode,
296 .drop_inode = generic_delete_inode,
297 .clear_inode = bdev_clear_inode,
298};
299
300static struct super_block *bd_get_sb(struct file_system_type *fs_type,
301 int flags, const char *dev_name, void *data)
302{
303 return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576);
304}
305
306static struct file_system_type bd_type = {
307 .name = "bdev",
308 .get_sb = bd_get_sb,
309 .kill_sb = kill_anon_super,
310};
311
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800312static struct vfsmount *bd_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313struct super_block *blockdev_superblock;
314
315void __init bdev_cache_init(void)
316{
317 int err;
318 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800319 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
320 SLAB_MEM_SPREAD|SLAB_PANIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 init_once, NULL);
322 err = register_filesystem(&bd_type);
323 if (err)
324 panic("Cannot register bdev pseudo-fs");
325 bd_mnt = kern_mount(&bd_type);
326 err = PTR_ERR(bd_mnt);
327 if (IS_ERR(bd_mnt))
328 panic("Cannot create bdev pseudo-fs");
329 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
330}
331
332/*
333 * Most likely _very_ bad one - but then it's hardly critical for small
334 * /dev and can be fixed when somebody will need really large one.
335 * Keep in mind that it will be fed through icache hash function too.
336 */
337static inline unsigned long hash(dev_t dev)
338{
339 return MAJOR(dev)+MINOR(dev);
340}
341
342static int bdev_test(struct inode *inode, void *data)
343{
344 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
345}
346
347static int bdev_set(struct inode *inode, void *data)
348{
349 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
350 return 0;
351}
352
353static LIST_HEAD(all_bdevs);
354
355struct block_device *bdget(dev_t dev)
356{
357 struct block_device *bdev;
358 struct inode *inode;
359
360 inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
361 bdev_test, bdev_set, &dev);
362
363 if (!inode)
364 return NULL;
365
366 bdev = &BDEV_I(inode)->bdev;
367
368 if (inode->i_state & I_NEW) {
369 bdev->bd_contains = NULL;
370 bdev->bd_inode = inode;
371 bdev->bd_block_size = (1 << inode->i_blkbits);
372 bdev->bd_part_count = 0;
373 bdev->bd_invalidated = 0;
374 inode->i_mode = S_IFBLK;
375 inode->i_rdev = dev;
376 inode->i_bdev = bdev;
377 inode->i_data.a_ops = &def_blk_aops;
378 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
379 inode->i_data.backing_dev_info = &default_backing_dev_info;
380 spin_lock(&bdev_lock);
381 list_add(&bdev->bd_list, &all_bdevs);
382 spin_unlock(&bdev_lock);
383 unlock_new_inode(inode);
384 }
385 return bdev;
386}
387
388EXPORT_SYMBOL(bdget);
389
390long nr_blockdev_pages(void)
391{
392 struct list_head *p;
393 long ret = 0;
394 spin_lock(&bdev_lock);
395 list_for_each(p, &all_bdevs) {
396 struct block_device *bdev;
397 bdev = list_entry(p, struct block_device, bd_list);
398 ret += bdev->bd_inode->i_mapping->nrpages;
399 }
400 spin_unlock(&bdev_lock);
401 return ret;
402}
403
404void bdput(struct block_device *bdev)
405{
406 iput(bdev->bd_inode);
407}
408
409EXPORT_SYMBOL(bdput);
410
411static struct block_device *bd_acquire(struct inode *inode)
412{
413 struct block_device *bdev;
414 spin_lock(&bdev_lock);
415 bdev = inode->i_bdev;
416 if (bdev && igrab(bdev->bd_inode)) {
417 spin_unlock(&bdev_lock);
418 return bdev;
419 }
420 spin_unlock(&bdev_lock);
421 bdev = bdget(inode->i_rdev);
422 if (bdev) {
423 spin_lock(&bdev_lock);
424 if (inode->i_bdev)
425 __bd_forget(inode);
426 inode->i_bdev = bdev;
427 inode->i_mapping = bdev->bd_inode->i_mapping;
428 list_add(&inode->i_devices, &bdev->bd_inodes);
429 spin_unlock(&bdev_lock);
430 }
431 return bdev;
432}
433
434/* Call when you free inode */
435
436void bd_forget(struct inode *inode)
437{
438 spin_lock(&bdev_lock);
439 if (inode->i_bdev)
440 __bd_forget(inode);
441 spin_unlock(&bdev_lock);
442}
443
444int bd_claim(struct block_device *bdev, void *holder)
445{
446 int res;
447 spin_lock(&bdev_lock);
448
449 /* first decide result */
450 if (bdev->bd_holder == holder)
451 res = 0; /* already a holder */
452 else if (bdev->bd_holder != NULL)
453 res = -EBUSY; /* held by someone else */
454 else if (bdev->bd_contains == bdev)
455 res = 0; /* is a whole device which isn't held */
456
457 else if (bdev->bd_contains->bd_holder == bd_claim)
458 res = 0; /* is a partition of a device that is being partitioned */
459 else if (bdev->bd_contains->bd_holder != NULL)
460 res = -EBUSY; /* is a partition of a held device */
461 else
462 res = 0; /* is a partition of an un-held device */
463
464 /* now impose change */
465 if (res==0) {
466 /* note that for a whole device bd_holders
467 * will be incremented twice, and bd_holder will
468 * be set to bd_claim before being set to holder
469 */
470 bdev->bd_contains->bd_holders ++;
471 bdev->bd_contains->bd_holder = bd_claim;
472 bdev->bd_holders++;
473 bdev->bd_holder = holder;
474 }
475 spin_unlock(&bdev_lock);
476 return res;
477}
478
479EXPORT_SYMBOL(bd_claim);
480
481void bd_release(struct block_device *bdev)
482{
483 spin_lock(&bdev_lock);
484 if (!--bdev->bd_contains->bd_holders)
485 bdev->bd_contains->bd_holder = NULL;
486 if (!--bdev->bd_holders)
487 bdev->bd_holder = NULL;
488 spin_unlock(&bdev_lock);
489}
490
491EXPORT_SYMBOL(bd_release);
492
493/*
494 * Tries to open block device by device number. Use it ONLY if you
495 * really do not have anything better - i.e. when you are behind a
496 * truly sucky interface and all you are given is a device number. _Never_
497 * to be used for internal purposes. If you ever need it - reconsider
498 * your API.
499 */
500struct block_device *open_by_devnum(dev_t dev, unsigned mode)
501{
502 struct block_device *bdev = bdget(dev);
503 int err = -ENOMEM;
504 int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
505 if (bdev)
506 err = blkdev_get(bdev, mode, flags);
507 return err ? ERR_PTR(err) : bdev;
508}
509
510EXPORT_SYMBOL(open_by_devnum);
511
512/*
513 * This routine checks whether a removable media has been changed,
514 * and invalidates all buffer-cache-entries in that case. This
515 * is a relatively slow routine, so we have to try to minimize using
516 * it. Thus it is called only upon a 'mount' or 'open'. This
517 * is the best way of combining speed and utility, I think.
518 * People changing diskettes in the middle of an operation deserve
519 * to lose :-)
520 */
521int check_disk_change(struct block_device *bdev)
522{
523 struct gendisk *disk = bdev->bd_disk;
524 struct block_device_operations * bdops = disk->fops;
525
526 if (!bdops->media_changed)
527 return 0;
528 if (!bdops->media_changed(bdev->bd_disk))
529 return 0;
530
Christoph Hellwig2ef41632005-05-05 16:15:59 -0700531 if (__invalidate_device(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 printk("VFS: busy inodes on changed media.\n");
533
534 if (bdops->revalidate_disk)
535 bdops->revalidate_disk(bdev->bd_disk);
536 if (bdev->bd_disk->minors > 1)
537 bdev->bd_invalidated = 1;
538 return 1;
539}
540
541EXPORT_SYMBOL(check_disk_change);
542
543void bd_set_size(struct block_device *bdev, loff_t size)
544{
545 unsigned bsize = bdev_hardsect_size(bdev);
546
547 bdev->bd_inode->i_size = size;
548 while (bsize < PAGE_CACHE_SIZE) {
549 if (size & bsize)
550 break;
551 bsize <<= 1;
552 }
553 bdev->bd_block_size = bsize;
554 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
555}
556EXPORT_SYMBOL(bd_set_size);
557
558static int do_open(struct block_device *bdev, struct file *file)
559{
560 struct module *owner = NULL;
561 struct gendisk *disk;
562 int ret = -ENXIO;
563 int part;
564
565 file->f_mapping = bdev->bd_inode->i_mapping;
566 lock_kernel();
567 disk = get_gendisk(bdev->bd_dev, &part);
568 if (!disk) {
569 unlock_kernel();
570 bdput(bdev);
571 return ret;
572 }
573 owner = disk->fops->owner;
574
Arjan van de Venc039e312006-03-23 03:00:28 -0800575 mutex_lock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (!bdev->bd_openers) {
577 bdev->bd_disk = disk;
578 bdev->bd_contains = bdev;
579 if (!part) {
580 struct backing_dev_info *bdi;
581 if (disk->fops->open) {
582 ret = disk->fops->open(bdev->bd_inode, file);
583 if (ret)
584 goto out_first;
585 }
586 if (!bdev->bd_openers) {
587 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
588 bdi = blk_get_backing_dev_info(bdev);
589 if (bdi == NULL)
590 bdi = &default_backing_dev_info;
591 bdev->bd_inode->i_data.backing_dev_info = bdi;
592 }
593 if (bdev->bd_invalidated)
594 rescan_partitions(disk, bdev);
595 } else {
596 struct hd_struct *p;
597 struct block_device *whole;
598 whole = bdget_disk(disk, 0);
599 ret = -ENOMEM;
600 if (!whole)
601 goto out_first;
602 ret = blkdev_get(whole, file->f_mode, file->f_flags);
603 if (ret)
604 goto out_first;
605 bdev->bd_contains = whole;
Arjan van de Venc039e312006-03-23 03:00:28 -0800606 mutex_lock(&whole->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 whole->bd_part_count++;
608 p = disk->part[part - 1];
609 bdev->bd_inode->i_data.backing_dev_info =
610 whole->bd_inode->i_data.backing_dev_info;
611 if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) {
612 whole->bd_part_count--;
Arjan van de Venc039e312006-03-23 03:00:28 -0800613 mutex_unlock(&whole->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 ret = -ENXIO;
615 goto out_first;
616 }
617 kobject_get(&p->kobj);
618 bdev->bd_part = p;
619 bd_set_size(bdev, (loff_t) p->nr_sects << 9);
Arjan van de Venc039e312006-03-23 03:00:28 -0800620 mutex_unlock(&whole->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622 } else {
623 put_disk(disk);
624 module_put(owner);
625 if (bdev->bd_contains == bdev) {
626 if (bdev->bd_disk->fops->open) {
627 ret = bdev->bd_disk->fops->open(bdev->bd_inode, file);
628 if (ret)
629 goto out;
630 }
631 if (bdev->bd_invalidated)
632 rescan_partitions(bdev->bd_disk, bdev);
633 } else {
Arjan van de Venc039e312006-03-23 03:00:28 -0800634 mutex_lock(&bdev->bd_contains->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 bdev->bd_contains->bd_part_count++;
Arjan van de Venc039e312006-03-23 03:00:28 -0800636 mutex_unlock(&bdev->bd_contains->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638 }
639 bdev->bd_openers++;
Arjan van de Venc039e312006-03-23 03:00:28 -0800640 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 unlock_kernel();
642 return 0;
643
644out_first:
645 bdev->bd_disk = NULL;
646 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
647 if (bdev != bdev->bd_contains)
648 blkdev_put(bdev->bd_contains);
649 bdev->bd_contains = NULL;
650 put_disk(disk);
651 module_put(owner);
652out:
Arjan van de Venc039e312006-03-23 03:00:28 -0800653 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 unlock_kernel();
655 if (ret)
656 bdput(bdev);
657 return ret;
658}
659
660int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags)
661{
662 /*
663 * This crockload is due to bad choice of ->open() type.
664 * It will go away.
665 * For now, block device ->open() routine must _not_
666 * examine anything in 'inode' argument except ->i_rdev.
667 */
668 struct file fake_file = {};
669 struct dentry fake_dentry = {};
670 fake_file.f_mode = mode;
671 fake_file.f_flags = flags;
672 fake_file.f_dentry = &fake_dentry;
673 fake_dentry.d_inode = bdev->bd_inode;
674
675 return do_open(bdev, &fake_file);
676}
677
678EXPORT_SYMBOL(blkdev_get);
679
680static int blkdev_open(struct inode * inode, struct file * filp)
681{
682 struct block_device *bdev;
683 int res;
684
685 /*
686 * Preserve backwards compatibility and allow large file access
687 * even if userspace doesn't ask for it explicitly. Some mkfs
688 * binary needs it. We might want to drop this workaround
689 * during an unstable branch.
690 */
691 filp->f_flags |= O_LARGEFILE;
692
693 bdev = bd_acquire(inode);
694
695 res = do_open(bdev, filp);
696 if (res)
697 return res;
698
699 if (!(filp->f_flags & O_EXCL) )
700 return 0;
701
702 if (!(res = bd_claim(bdev, filp)))
703 return 0;
704
705 blkdev_put(bdev);
706 return res;
707}
708
709int blkdev_put(struct block_device *bdev)
710{
711 int ret = 0;
712 struct inode *bd_inode = bdev->bd_inode;
713 struct gendisk *disk = bdev->bd_disk;
714
Arjan van de Venc039e312006-03-23 03:00:28 -0800715 mutex_lock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 lock_kernel();
717 if (!--bdev->bd_openers) {
718 sync_blockdev(bdev);
719 kill_bdev(bdev);
720 }
721 if (bdev->bd_contains == bdev) {
722 if (disk->fops->release)
723 ret = disk->fops->release(bd_inode, NULL);
724 } else {
Arjan van de Venc039e312006-03-23 03:00:28 -0800725 mutex_lock(&bdev->bd_contains->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 bdev->bd_contains->bd_part_count--;
Arjan van de Venc039e312006-03-23 03:00:28 -0800727 mutex_unlock(&bdev->bd_contains->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729 if (!bdev->bd_openers) {
730 struct module *owner = disk->fops->owner;
731
732 put_disk(disk);
733 module_put(owner);
734
735 if (bdev->bd_contains != bdev) {
736 kobject_put(&bdev->bd_part->kobj);
737 bdev->bd_part = NULL;
738 }
739 bdev->bd_disk = NULL;
740 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
741 if (bdev != bdev->bd_contains) {
742 blkdev_put(bdev->bd_contains);
743 }
744 bdev->bd_contains = NULL;
745 }
746 unlock_kernel();
Arjan van de Venc039e312006-03-23 03:00:28 -0800747 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 bdput(bdev);
749 return ret;
750}
751
752EXPORT_SYMBOL(blkdev_put);
753
754static int blkdev_close(struct inode * inode, struct file * filp)
755{
756 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
757 if (bdev->bd_holder == filp)
758 bd_release(bdev);
759 return blkdev_put(bdev);
760}
761
762static ssize_t blkdev_file_write(struct file *file, const char __user *buf,
763 size_t count, loff_t *ppos)
764{
765 struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
766
767 return generic_file_write_nolock(file, &local_iov, 1, ppos);
768}
769
770static ssize_t blkdev_file_aio_write(struct kiocb *iocb, const char __user *buf,
771 size_t count, loff_t pos)
772{
773 struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
774
775 return generic_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos);
776}
777
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700778static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 return blkdev_ioctl(file->f_mapping->host, file, cmd, arg);
781}
782
783struct address_space_operations def_blk_aops = {
784 .readpage = blkdev_readpage,
785 .writepage = blkdev_writepage,
786 .sync_page = block_sync_page,
787 .prepare_write = blkdev_prepare_write,
788 .commit_write = blkdev_commit_write,
789 .writepages = generic_writepages,
790 .direct_IO = blkdev_direct_IO,
791};
792
793struct file_operations def_blk_fops = {
794 .open = blkdev_open,
795 .release = blkdev_close,
796 .llseek = block_llseek,
797 .read = generic_file_read,
798 .write = blkdev_file_write,
799 .aio_read = generic_file_aio_read,
800 .aio_write = blkdev_file_aio_write,
801 .mmap = generic_file_mmap,
802 .fsync = block_fsync,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -0700803 .unlocked_ioctl = block_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804#ifdef CONFIG_COMPAT
805 .compat_ioctl = compat_blkdev_ioctl,
806#endif
807 .readv = generic_file_readv,
808 .writev = generic_file_write_nolock,
809 .sendfile = generic_file_sendfile,
810};
811
812int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
813{
814 int res;
815 mm_segment_t old_fs = get_fs();
816 set_fs(KERNEL_DS);
817 res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg);
818 set_fs(old_fs);
819 return res;
820}
821
822EXPORT_SYMBOL(ioctl_by_bdev);
823
824/**
825 * lookup_bdev - lookup a struct block_device by name
826 *
827 * @path: special file representing the block device
828 *
829 * Get a reference to the blockdevice at @path in the current
830 * namespace if possible and return it. Return ERR_PTR(error)
831 * otherwise.
832 */
833struct block_device *lookup_bdev(const char *path)
834{
835 struct block_device *bdev;
836 struct inode *inode;
837 struct nameidata nd;
838 int error;
839
840 if (!path || !*path)
841 return ERR_PTR(-EINVAL);
842
843 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
844 if (error)
845 return ERR_PTR(error);
846
847 inode = nd.dentry->d_inode;
848 error = -ENOTBLK;
849 if (!S_ISBLK(inode->i_mode))
850 goto fail;
851 error = -EACCES;
852 if (nd.mnt->mnt_flags & MNT_NODEV)
853 goto fail;
854 error = -ENOMEM;
855 bdev = bd_acquire(inode);
856 if (!bdev)
857 goto fail;
858out:
859 path_release(&nd);
860 return bdev;
861fail:
862 bdev = ERR_PTR(error);
863 goto out;
864}
865
866/**
867 * open_bdev_excl - open a block device by name and set it up for use
868 *
869 * @path: special file representing the block device
870 * @flags: %MS_RDONLY for opening read-only
871 * @holder: owner for exclusion
872 *
873 * Open the blockdevice described by the special file at @path, claim it
874 * for the @holder.
875 */
876struct block_device *open_bdev_excl(const char *path, int flags, void *holder)
877{
878 struct block_device *bdev;
879 mode_t mode = FMODE_READ;
880 int error = 0;
881
882 bdev = lookup_bdev(path);
883 if (IS_ERR(bdev))
884 return bdev;
885
886 if (!(flags & MS_RDONLY))
887 mode |= FMODE_WRITE;
888 error = blkdev_get(bdev, mode, 0);
889 if (error)
890 return ERR_PTR(error);
891 error = -EACCES;
892 if (!(flags & MS_RDONLY) && bdev_read_only(bdev))
893 goto blkdev_put;
894 error = bd_claim(bdev, holder);
895 if (error)
896 goto blkdev_put;
897
898 return bdev;
899
900blkdev_put:
901 blkdev_put(bdev);
902 return ERR_PTR(error);
903}
904
905EXPORT_SYMBOL(open_bdev_excl);
906
907/**
908 * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
909 *
910 * @bdev: blockdevice to close
911 *
912 * This is the counterpart to open_bdev_excl().
913 */
914void close_bdev_excl(struct block_device *bdev)
915{
916 bd_release(bdev);
917 blkdev_put(bdev);
918}
919
920EXPORT_SYMBOL(close_bdev_excl);