Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/smp_lock.h> |
| 16 | #include <linux/highmem.h> |
| 17 | #include <linux/blkdev.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/blkpg.h> |
| 20 | #include <linux/buffer_head.h> |
| 21 | #include <linux/mpage.h> |
| 22 | #include <linux/mount.h> |
| 23 | #include <linux/uio.h> |
| 24 | #include <linux/namei.h> |
| 25 | #include <asm/uaccess.h> |
| 26 | |
| 27 | struct bdev_inode { |
| 28 | struct block_device bdev; |
| 29 | struct inode vfs_inode; |
| 30 | }; |
| 31 | |
| 32 | static inline struct bdev_inode *BDEV_I(struct inode *inode) |
| 33 | { |
| 34 | return container_of(inode, struct bdev_inode, vfs_inode); |
| 35 | } |
| 36 | |
| 37 | inline struct block_device *I_BDEV(struct inode *inode) |
| 38 | { |
| 39 | return &BDEV_I(inode)->bdev; |
| 40 | } |
| 41 | |
| 42 | EXPORT_SYMBOL(I_BDEV); |
| 43 | |
| 44 | static sector_t max_block(struct block_device *bdev) |
| 45 | { |
| 46 | sector_t retval = ~((sector_t)0); |
| 47 | loff_t sz = i_size_read(bdev->bd_inode); |
| 48 | |
| 49 | if (sz) { |
| 50 | unsigned int size = block_size(bdev); |
| 51 | unsigned int sizebits = blksize_bits(size); |
| 52 | retval = (sz >> sizebits); |
| 53 | } |
| 54 | return retval; |
| 55 | } |
| 56 | |
| 57 | /* Kill _all_ buffers, dirty or not.. */ |
| 58 | static void kill_bdev(struct block_device *bdev) |
| 59 | { |
| 60 | invalidate_bdev(bdev, 1); |
| 61 | truncate_inode_pages(bdev->bd_inode->i_mapping, 0); |
| 62 | } |
| 63 | |
| 64 | int set_blocksize(struct block_device *bdev, int size) |
| 65 | { |
| 66 | /* Size must be a power of two, and between 512 and PAGE_SIZE */ |
| 67 | if (size > PAGE_SIZE || size < 512 || (size & (size-1))) |
| 68 | return -EINVAL; |
| 69 | |
| 70 | /* Size cannot be smaller than the size supported by the device */ |
| 71 | if (size < bdev_hardsect_size(bdev)) |
| 72 | return -EINVAL; |
| 73 | |
| 74 | /* Don't change the size if it is same as current */ |
| 75 | if (bdev->bd_block_size != size) { |
| 76 | sync_blockdev(bdev); |
| 77 | bdev->bd_block_size = size; |
| 78 | bdev->bd_inode->i_blkbits = blksize_bits(size); |
| 79 | kill_bdev(bdev); |
| 80 | } |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | EXPORT_SYMBOL(set_blocksize); |
| 85 | |
| 86 | int sb_set_blocksize(struct super_block *sb, int size) |
| 87 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | if (set_blocksize(sb->s_bdev, size)) |
| 89 | return 0; |
| 90 | /* If we get here, we know size is power of two |
| 91 | * and it's value is between 512 and PAGE_SIZE */ |
| 92 | sb->s_blocksize = size; |
Coywolf Qi Hunt | 38885bd | 2006-03-24 03:18:05 -0800 | [diff] [blame] | 93 | sb->s_blocksize_bits = blksize_bits(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | return sb->s_blocksize; |
| 95 | } |
| 96 | |
| 97 | EXPORT_SYMBOL(sb_set_blocksize); |
| 98 | |
| 99 | int sb_min_blocksize(struct super_block *sb, int size) |
| 100 | { |
| 101 | int minsize = bdev_hardsect_size(sb->s_bdev); |
| 102 | if (size < minsize) |
| 103 | size = minsize; |
| 104 | return sb_set_blocksize(sb, size); |
| 105 | } |
| 106 | |
| 107 | EXPORT_SYMBOL(sb_min_blocksize); |
| 108 | |
| 109 | static int |
| 110 | blkdev_get_block(struct inode *inode, sector_t iblock, |
| 111 | struct buffer_head *bh, int create) |
| 112 | { |
| 113 | if (iblock >= max_block(I_BDEV(inode))) { |
| 114 | if (create) |
| 115 | return -EIO; |
| 116 | |
| 117 | /* |
| 118 | * for reads, we're just trying to fill a partial page. |
| 119 | * return a hole, they will have to call get_block again |
| 120 | * before they can fill it, and they will get -EIO at that |
| 121 | * time |
| 122 | */ |
| 123 | return 0; |
| 124 | } |
| 125 | bh->b_bdev = I_BDEV(inode); |
| 126 | bh->b_blocknr = iblock; |
| 127 | set_buffer_mapped(bh); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static int |
| 132 | blkdev_get_blocks(struct inode *inode, sector_t iblock, |
Badari Pulavarty | 1d8fa7a | 2006-03-26 01:38:02 -0800 | [diff] [blame] | 133 | struct buffer_head *bh, int create) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
| 135 | sector_t end_block = max_block(I_BDEV(inode)); |
Badari Pulavarty | 1d8fa7a | 2006-03-26 01:38:02 -0800 | [diff] [blame] | 136 | unsigned long max_blocks = bh->b_size >> inode->i_blkbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 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 */ |
| 143 | /* |
| 144 | * It is a read which is fully beyond EOF. We return |
| 145 | * a !buffer_mapped buffer |
| 146 | */ |
| 147 | max_blocks = 0; |
| 148 | } |
| 149 | } |
| 150 | |
| 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; |
| 157 | } |
| 158 | |
| 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) |
| 162 | { |
| 163 | struct file *file = iocb->ki_filp; |
| 164 | struct inode *inode = file->f_mapping->host; |
| 165 | |
| 166 | return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode), |
| 167 | iov, offset, nr_segs, blkdev_get_blocks, NULL); |
| 168 | } |
| 169 | |
| 170 | static int blkdev_writepage(struct page *page, struct writeback_control *wbc) |
| 171 | { |
| 172 | return block_write_full_page(page, blkdev_get_block, wbc); |
| 173 | } |
| 174 | |
| 175 | static int blkdev_readpage(struct file * file, struct page * page) |
| 176 | { |
| 177 | return block_read_full_page(page, blkdev_get_block); |
| 178 | } |
| 179 | |
| 180 | static int blkdev_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to) |
| 181 | { |
| 182 | return block_prepare_write(page, from, to, blkdev_get_block); |
| 183 | } |
| 184 | |
| 185 | static int blkdev_commit_write(struct file *file, struct page *page, unsigned from, unsigned to) |
| 186 | { |
| 187 | return block_commit_write(page, from, to); |
| 188 | } |
| 189 | |
| 190 | /* |
| 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) |
| 194 | */ |
| 195 | static loff_t block_llseek(struct file *file, loff_t offset, int origin) |
| 196 | { |
| 197 | struct inode *bd_inode = file->f_mapping->host; |
| 198 | loff_t size; |
| 199 | loff_t retval; |
| 200 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 201 | mutex_lock(&bd_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | size = i_size_read(bd_inode); |
| 203 | |
| 204 | switch (origin) { |
| 205 | case 2: |
| 206 | offset += size; |
| 207 | break; |
| 208 | case 1: |
| 209 | offset += file->f_pos; |
| 210 | } |
| 211 | retval = -EINVAL; |
| 212 | if (offset >= 0 && offset <= size) { |
| 213 | if (offset != file->f_pos) { |
| 214 | file->f_pos = offset; |
| 215 | } |
| 216 | retval = offset; |
| 217 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 218 | mutex_unlock(&bd_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | return retval; |
| 220 | } |
| 221 | |
| 222 | /* |
| 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. |
| 225 | */ |
| 226 | |
| 227 | static int block_fsync(struct file *filp, struct dentry *dentry, int datasync) |
| 228 | { |
| 229 | return sync_blockdev(I_BDEV(filp->f_mapping->host)); |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * pseudo-fs |
| 234 | */ |
| 235 | |
| 236 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock); |
Eric Dumazet | fa3536c | 2006-03-26 01:37:24 -0800 | [diff] [blame] | 237 | static kmem_cache_t * bdev_cachep __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
| 239 | static struct inode *bdev_alloc_inode(struct super_block *sb) |
| 240 | { |
| 241 | struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, SLAB_KERNEL); |
| 242 | if (!ei) |
| 243 | return NULL; |
| 244 | return &ei->vfs_inode; |
| 245 | } |
| 246 | |
| 247 | static void bdev_destroy_inode(struct inode *inode) |
| 248 | { |
| 249 | struct bdev_inode *bdi = BDEV_I(inode); |
| 250 | |
| 251 | bdi->bdev.bd_inode_backing_dev_info = NULL; |
| 252 | kmem_cache_free(bdev_cachep, bdi); |
| 253 | } |
| 254 | |
| 255 | static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags) |
| 256 | { |
| 257 | struct bdev_inode *ei = (struct bdev_inode *) foo; |
| 258 | struct block_device *bdev = &ei->bdev; |
| 259 | |
| 260 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == |
| 261 | SLAB_CTOR_CONSTRUCTOR) |
| 262 | { |
| 263 | memset(bdev, 0, sizeof(*bdev)); |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 264 | mutex_init(&bdev->bd_mutex); |
| 265 | mutex_init(&bdev->bd_mount_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | INIT_LIST_HEAD(&bdev->bd_inodes); |
| 267 | INIT_LIST_HEAD(&bdev->bd_list); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 268 | #ifdef CONFIG_SYSFS |
| 269 | INIT_LIST_HEAD(&bdev->bd_holder_list); |
| 270 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | inode_init_once(&ei->vfs_inode); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | static inline void __bd_forget(struct inode *inode) |
| 276 | { |
| 277 | list_del_init(&inode->i_devices); |
| 278 | inode->i_bdev = NULL; |
| 279 | inode->i_mapping = &inode->i_data; |
| 280 | } |
| 281 | |
| 282 | static void bdev_clear_inode(struct inode *inode) |
| 283 | { |
| 284 | struct block_device *bdev = &BDEV_I(inode)->bdev; |
| 285 | struct list_head *p; |
| 286 | spin_lock(&bdev_lock); |
| 287 | while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) { |
| 288 | __bd_forget(list_entry(p, struct inode, i_devices)); |
| 289 | } |
| 290 | list_del_init(&bdev->bd_list); |
| 291 | spin_unlock(&bdev_lock); |
| 292 | } |
| 293 | |
| 294 | static struct super_operations bdev_sops = { |
| 295 | .statfs = simple_statfs, |
| 296 | .alloc_inode = bdev_alloc_inode, |
| 297 | .destroy_inode = bdev_destroy_inode, |
| 298 | .drop_inode = generic_delete_inode, |
| 299 | .clear_inode = bdev_clear_inode, |
| 300 | }; |
| 301 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 302 | static int bd_get_sb(struct file_system_type *fs_type, |
| 303 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 305 | return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static struct file_system_type bd_type = { |
| 309 | .name = "bdev", |
| 310 | .get_sb = bd_get_sb, |
| 311 | .kill_sb = kill_anon_super, |
| 312 | }; |
| 313 | |
Eric Dumazet | fa3536c | 2006-03-26 01:37:24 -0800 | [diff] [blame] | 314 | static struct vfsmount *bd_mnt __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | struct super_block *blockdev_superblock; |
| 316 | |
| 317 | void __init bdev_cache_init(void) |
| 318 | { |
| 319 | int err; |
| 320 | bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode), |
Paul Jackson | fffb60f | 2006-03-24 03:16:06 -0800 | [diff] [blame] | 321 | 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
| 322 | SLAB_MEM_SPREAD|SLAB_PANIC), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | init_once, NULL); |
| 324 | err = register_filesystem(&bd_type); |
| 325 | if (err) |
| 326 | panic("Cannot register bdev pseudo-fs"); |
| 327 | bd_mnt = kern_mount(&bd_type); |
| 328 | err = PTR_ERR(bd_mnt); |
| 329 | if (IS_ERR(bd_mnt)) |
| 330 | panic("Cannot create bdev pseudo-fs"); |
| 331 | blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */ |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Most likely _very_ bad one - but then it's hardly critical for small |
| 336 | * /dev and can be fixed when somebody will need really large one. |
| 337 | * Keep in mind that it will be fed through icache hash function too. |
| 338 | */ |
| 339 | static inline unsigned long hash(dev_t dev) |
| 340 | { |
| 341 | return MAJOR(dev)+MINOR(dev); |
| 342 | } |
| 343 | |
| 344 | static int bdev_test(struct inode *inode, void *data) |
| 345 | { |
| 346 | return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data; |
| 347 | } |
| 348 | |
| 349 | static int bdev_set(struct inode *inode, void *data) |
| 350 | { |
| 351 | BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data; |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static LIST_HEAD(all_bdevs); |
| 356 | |
| 357 | struct block_device *bdget(dev_t dev) |
| 358 | { |
| 359 | struct block_device *bdev; |
| 360 | struct inode *inode; |
| 361 | |
| 362 | inode = iget5_locked(bd_mnt->mnt_sb, hash(dev), |
| 363 | bdev_test, bdev_set, &dev); |
| 364 | |
| 365 | if (!inode) |
| 366 | return NULL; |
| 367 | |
| 368 | bdev = &BDEV_I(inode)->bdev; |
| 369 | |
| 370 | if (inode->i_state & I_NEW) { |
| 371 | bdev->bd_contains = NULL; |
| 372 | bdev->bd_inode = inode; |
| 373 | bdev->bd_block_size = (1 << inode->i_blkbits); |
| 374 | bdev->bd_part_count = 0; |
| 375 | bdev->bd_invalidated = 0; |
| 376 | inode->i_mode = S_IFBLK; |
| 377 | inode->i_rdev = dev; |
| 378 | inode->i_bdev = bdev; |
| 379 | inode->i_data.a_ops = &def_blk_aops; |
| 380 | mapping_set_gfp_mask(&inode->i_data, GFP_USER); |
| 381 | inode->i_data.backing_dev_info = &default_backing_dev_info; |
| 382 | spin_lock(&bdev_lock); |
| 383 | list_add(&bdev->bd_list, &all_bdevs); |
| 384 | spin_unlock(&bdev_lock); |
| 385 | unlock_new_inode(inode); |
| 386 | } |
| 387 | return bdev; |
| 388 | } |
| 389 | |
| 390 | EXPORT_SYMBOL(bdget); |
| 391 | |
| 392 | long nr_blockdev_pages(void) |
| 393 | { |
| 394 | struct list_head *p; |
| 395 | long ret = 0; |
| 396 | spin_lock(&bdev_lock); |
| 397 | list_for_each(p, &all_bdevs) { |
| 398 | struct block_device *bdev; |
| 399 | bdev = list_entry(p, struct block_device, bd_list); |
| 400 | ret += bdev->bd_inode->i_mapping->nrpages; |
| 401 | } |
| 402 | spin_unlock(&bdev_lock); |
| 403 | return ret; |
| 404 | } |
| 405 | |
| 406 | void bdput(struct block_device *bdev) |
| 407 | { |
| 408 | iput(bdev->bd_inode); |
| 409 | } |
| 410 | |
| 411 | EXPORT_SYMBOL(bdput); |
| 412 | |
| 413 | static struct block_device *bd_acquire(struct inode *inode) |
| 414 | { |
| 415 | struct block_device *bdev; |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | spin_lock(&bdev_lock); |
| 418 | bdev = inode->i_bdev; |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 419 | if (bdev) { |
| 420 | atomic_inc(&bdev->bd_inode->i_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | spin_unlock(&bdev_lock); |
| 422 | return bdev; |
| 423 | } |
| 424 | spin_unlock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | bdev = bdget(inode->i_rdev); |
| 427 | if (bdev) { |
| 428 | spin_lock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 429 | if (!inode->i_bdev) { |
| 430 | /* |
| 431 | * We take an additional bd_inode->i_count for inode, |
| 432 | * and it's released in clear_inode() of inode. |
| 433 | * So, we can access it via ->i_mapping always |
| 434 | * without igrab(). |
| 435 | */ |
| 436 | atomic_inc(&bdev->bd_inode->i_count); |
| 437 | inode->i_bdev = bdev; |
| 438 | inode->i_mapping = bdev->bd_inode->i_mapping; |
| 439 | list_add(&inode->i_devices, &bdev->bd_inodes); |
| 440 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | spin_unlock(&bdev_lock); |
| 442 | } |
| 443 | return bdev; |
| 444 | } |
| 445 | |
| 446 | /* Call when you free inode */ |
| 447 | |
| 448 | void bd_forget(struct inode *inode) |
| 449 | { |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 450 | struct block_device *bdev = NULL; |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | spin_lock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 453 | if (inode->i_bdev) { |
| 454 | if (inode->i_sb != blockdev_superblock) |
| 455 | bdev = inode->i_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | __bd_forget(inode); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 457 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | spin_unlock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 459 | |
| 460 | if (bdev) |
| 461 | iput(bdev->bd_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | int bd_claim(struct block_device *bdev, void *holder) |
| 465 | { |
| 466 | int res; |
| 467 | spin_lock(&bdev_lock); |
| 468 | |
| 469 | /* first decide result */ |
| 470 | if (bdev->bd_holder == holder) |
| 471 | res = 0; /* already a holder */ |
| 472 | else if (bdev->bd_holder != NULL) |
| 473 | res = -EBUSY; /* held by someone else */ |
| 474 | else if (bdev->bd_contains == bdev) |
| 475 | res = 0; /* is a whole device which isn't held */ |
| 476 | |
| 477 | else if (bdev->bd_contains->bd_holder == bd_claim) |
| 478 | res = 0; /* is a partition of a device that is being partitioned */ |
| 479 | else if (bdev->bd_contains->bd_holder != NULL) |
| 480 | res = -EBUSY; /* is a partition of a held device */ |
| 481 | else |
| 482 | res = 0; /* is a partition of an un-held device */ |
| 483 | |
| 484 | /* now impose change */ |
| 485 | if (res==0) { |
| 486 | /* note that for a whole device bd_holders |
| 487 | * will be incremented twice, and bd_holder will |
| 488 | * be set to bd_claim before being set to holder |
| 489 | */ |
| 490 | bdev->bd_contains->bd_holders ++; |
| 491 | bdev->bd_contains->bd_holder = bd_claim; |
| 492 | bdev->bd_holders++; |
| 493 | bdev->bd_holder = holder; |
| 494 | } |
| 495 | spin_unlock(&bdev_lock); |
| 496 | return res; |
| 497 | } |
| 498 | |
| 499 | EXPORT_SYMBOL(bd_claim); |
| 500 | |
| 501 | void bd_release(struct block_device *bdev) |
| 502 | { |
| 503 | spin_lock(&bdev_lock); |
| 504 | if (!--bdev->bd_contains->bd_holders) |
| 505 | bdev->bd_contains->bd_holder = NULL; |
| 506 | if (!--bdev->bd_holders) |
| 507 | bdev->bd_holder = NULL; |
| 508 | spin_unlock(&bdev_lock); |
| 509 | } |
| 510 | |
| 511 | EXPORT_SYMBOL(bd_release); |
| 512 | |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 513 | #ifdef CONFIG_SYSFS |
| 514 | /* |
| 515 | * Functions for bd_claim_by_kobject / bd_release_from_kobject |
| 516 | * |
| 517 | * If a kobject is passed to bd_claim_by_kobject() |
| 518 | * and the kobject has a parent directory, |
| 519 | * following symlinks are created: |
| 520 | * o from the kobject to the claimed bdev |
| 521 | * o from "holders" directory of the bdev to the parent of the kobject |
| 522 | * bd_release_from_kobject() removes these symlinks. |
| 523 | * |
| 524 | * Example: |
| 525 | * If /dev/dm-0 maps to /dev/sda, kobject corresponding to |
| 526 | * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then: |
| 527 | * /sys/block/dm-0/slaves/sda --> /sys/block/sda |
| 528 | * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0 |
| 529 | */ |
| 530 | |
| 531 | static struct kobject *bdev_get_kobj(struct block_device *bdev) |
| 532 | { |
| 533 | if (bdev->bd_contains != bdev) |
| 534 | return kobject_get(&bdev->bd_part->kobj); |
| 535 | else |
| 536 | return kobject_get(&bdev->bd_disk->kobj); |
| 537 | } |
| 538 | |
| 539 | static struct kobject *bdev_get_holder(struct block_device *bdev) |
| 540 | { |
| 541 | if (bdev->bd_contains != bdev) |
| 542 | return kobject_get(bdev->bd_part->holder_dir); |
| 543 | else |
| 544 | return kobject_get(bdev->bd_disk->holder_dir); |
| 545 | } |
| 546 | |
| 547 | static void add_symlink(struct kobject *from, struct kobject *to) |
| 548 | { |
| 549 | if (!from || !to) |
| 550 | return; |
| 551 | sysfs_create_link(from, to, kobject_name(to)); |
| 552 | } |
| 553 | |
| 554 | static void del_symlink(struct kobject *from, struct kobject *to) |
| 555 | { |
| 556 | if (!from || !to) |
| 557 | return; |
| 558 | sysfs_remove_link(from, kobject_name(to)); |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * 'struct bd_holder' contains pointers to kobjects symlinked by |
| 563 | * bd_claim_by_kobject. |
| 564 | * It's connected to bd_holder_list which is protected by bdev->bd_sem. |
| 565 | */ |
| 566 | struct bd_holder { |
| 567 | struct list_head list; /* chain of holders of the bdev */ |
| 568 | int count; /* references from the holder */ |
| 569 | struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */ |
| 570 | struct kobject *hdev; /* e.g. "/block/dm-0" */ |
| 571 | struct kobject *hdir; /* e.g. "/block/sda/holders" */ |
| 572 | struct kobject *sdev; /* e.g. "/block/sda" */ |
| 573 | }; |
| 574 | |
| 575 | /* |
| 576 | * Get references of related kobjects at once. |
| 577 | * Returns 1 on success. 0 on failure. |
| 578 | * |
| 579 | * Should call bd_holder_release_dirs() after successful use. |
| 580 | */ |
| 581 | static int bd_holder_grab_dirs(struct block_device *bdev, |
| 582 | struct bd_holder *bo) |
| 583 | { |
| 584 | if (!bdev || !bo) |
| 585 | return 0; |
| 586 | |
| 587 | bo->sdir = kobject_get(bo->sdir); |
| 588 | if (!bo->sdir) |
| 589 | return 0; |
| 590 | |
| 591 | bo->hdev = kobject_get(bo->sdir->parent); |
| 592 | if (!bo->hdev) |
| 593 | goto fail_put_sdir; |
| 594 | |
| 595 | bo->sdev = bdev_get_kobj(bdev); |
| 596 | if (!bo->sdev) |
| 597 | goto fail_put_hdev; |
| 598 | |
| 599 | bo->hdir = bdev_get_holder(bdev); |
| 600 | if (!bo->hdir) |
| 601 | goto fail_put_sdev; |
| 602 | |
| 603 | return 1; |
| 604 | |
| 605 | fail_put_sdev: |
| 606 | kobject_put(bo->sdev); |
| 607 | fail_put_hdev: |
| 608 | kobject_put(bo->hdev); |
| 609 | fail_put_sdir: |
| 610 | kobject_put(bo->sdir); |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | /* Put references of related kobjects at once. */ |
| 616 | static void bd_holder_release_dirs(struct bd_holder *bo) |
| 617 | { |
| 618 | kobject_put(bo->hdir); |
| 619 | kobject_put(bo->sdev); |
| 620 | kobject_put(bo->hdev); |
| 621 | kobject_put(bo->sdir); |
| 622 | } |
| 623 | |
| 624 | static struct bd_holder *alloc_bd_holder(struct kobject *kobj) |
| 625 | { |
| 626 | struct bd_holder *bo; |
| 627 | |
| 628 | bo = kzalloc(sizeof(*bo), GFP_KERNEL); |
| 629 | if (!bo) |
| 630 | return NULL; |
| 631 | |
| 632 | bo->count = 1; |
| 633 | bo->sdir = kobj; |
| 634 | |
| 635 | return bo; |
| 636 | } |
| 637 | |
| 638 | static void free_bd_holder(struct bd_holder *bo) |
| 639 | { |
| 640 | kfree(bo); |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * add_bd_holder - create sysfs symlinks for bd_claim() relationship |
| 645 | * |
| 646 | * @bdev: block device to be bd_claimed |
| 647 | * @bo: preallocated and initialized by alloc_bd_holder() |
| 648 | * |
| 649 | * If there is no matching entry with @bo in @bdev->bd_holder_list, |
| 650 | * add @bo to the list, create symlinks. |
| 651 | * |
| 652 | * Returns 1 if @bo was added to the list. |
| 653 | * Returns 0 if @bo wasn't used by any reason and should be freed. |
| 654 | */ |
| 655 | static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo) |
| 656 | { |
| 657 | struct bd_holder *tmp; |
| 658 | |
| 659 | if (!bo) |
| 660 | return 0; |
| 661 | |
| 662 | list_for_each_entry(tmp, &bdev->bd_holder_list, list) { |
| 663 | if (tmp->sdir == bo->sdir) { |
| 664 | tmp->count++; |
| 665 | return 0; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | if (!bd_holder_grab_dirs(bdev, bo)) |
| 670 | return 0; |
| 671 | |
| 672 | add_symlink(bo->sdir, bo->sdev); |
| 673 | add_symlink(bo->hdir, bo->hdev); |
| 674 | list_add_tail(&bo->list, &bdev->bd_holder_list); |
| 675 | return 1; |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * del_bd_holder - delete sysfs symlinks for bd_claim() relationship |
| 680 | * |
| 681 | * @bdev: block device to be bd_claimed |
| 682 | * @kobj: holder's kobject |
| 683 | * |
| 684 | * If there is matching entry with @kobj in @bdev->bd_holder_list |
| 685 | * and no other bd_claim() from the same kobject, |
| 686 | * remove the struct bd_holder from the list, delete symlinks for it. |
| 687 | * |
| 688 | * Returns a pointer to the struct bd_holder when it's removed from the list |
| 689 | * and ready to be freed. |
| 690 | * Returns NULL if matching claim isn't found or there is other bd_claim() |
| 691 | * by the same kobject. |
| 692 | */ |
| 693 | static struct bd_holder *del_bd_holder(struct block_device *bdev, |
| 694 | struct kobject *kobj) |
| 695 | { |
| 696 | struct bd_holder *bo; |
| 697 | |
| 698 | list_for_each_entry(bo, &bdev->bd_holder_list, list) { |
| 699 | if (bo->sdir == kobj) { |
| 700 | bo->count--; |
| 701 | BUG_ON(bo->count < 0); |
| 702 | if (!bo->count) { |
| 703 | list_del(&bo->list); |
| 704 | del_symlink(bo->sdir, bo->sdev); |
| 705 | del_symlink(bo->hdir, bo->hdev); |
| 706 | bd_holder_release_dirs(bo); |
| 707 | return bo; |
| 708 | } |
| 709 | break; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | return NULL; |
| 714 | } |
| 715 | |
| 716 | /** |
| 717 | * bd_claim_by_kobject - bd_claim() with additional kobject signature |
| 718 | * |
| 719 | * @bdev: block device to be claimed |
| 720 | * @holder: holder's signature |
| 721 | * @kobj: holder's kobject |
| 722 | * |
| 723 | * Do bd_claim() and if it succeeds, create sysfs symlinks between |
| 724 | * the bdev and the holder's kobject. |
| 725 | * Use bd_release_from_kobject() when relesing the claimed bdev. |
| 726 | * |
| 727 | * Returns 0 on success. (same as bd_claim()) |
| 728 | * Returns errno on failure. |
| 729 | */ |
| 730 | static int bd_claim_by_kobject(struct block_device *bdev, void *holder, |
| 731 | struct kobject *kobj) |
| 732 | { |
| 733 | int res; |
| 734 | struct bd_holder *bo; |
| 735 | |
| 736 | if (!kobj) |
| 737 | return -EINVAL; |
| 738 | |
| 739 | bo = alloc_bd_holder(kobj); |
| 740 | if (!bo) |
| 741 | return -ENOMEM; |
| 742 | |
Jun'ichi Nomura | b4cf1b7 | 2006-03-27 01:18:00 -0800 | [diff] [blame] | 743 | mutex_lock(&bdev->bd_mutex); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 744 | res = bd_claim(bdev, holder); |
| 745 | if (res || !add_bd_holder(bdev, bo)) |
| 746 | free_bd_holder(bo); |
Jun'ichi Nomura | b4cf1b7 | 2006-03-27 01:18:00 -0800 | [diff] [blame] | 747 | mutex_unlock(&bdev->bd_mutex); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 748 | |
| 749 | return res; |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * bd_release_from_kobject - bd_release() with additional kobject signature |
| 754 | * |
| 755 | * @bdev: block device to be released |
| 756 | * @kobj: holder's kobject |
| 757 | * |
| 758 | * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject(). |
| 759 | */ |
| 760 | static void bd_release_from_kobject(struct block_device *bdev, |
| 761 | struct kobject *kobj) |
| 762 | { |
| 763 | struct bd_holder *bo; |
| 764 | |
| 765 | if (!kobj) |
| 766 | return; |
| 767 | |
Jun'ichi Nomura | b4cf1b7 | 2006-03-27 01:18:00 -0800 | [diff] [blame] | 768 | mutex_lock(&bdev->bd_mutex); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 769 | bd_release(bdev); |
| 770 | if ((bo = del_bd_holder(bdev, kobj))) |
| 771 | free_bd_holder(bo); |
Jun'ichi Nomura | b4cf1b7 | 2006-03-27 01:18:00 -0800 | [diff] [blame] | 772 | mutex_unlock(&bdev->bd_mutex); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | /** |
| 776 | * bd_claim_by_disk - wrapper function for bd_claim_by_kobject() |
| 777 | * |
| 778 | * @bdev: block device to be claimed |
| 779 | * @holder: holder's signature |
| 780 | * @disk: holder's gendisk |
| 781 | * |
| 782 | * Call bd_claim_by_kobject() with getting @disk->slave_dir. |
| 783 | */ |
| 784 | int bd_claim_by_disk(struct block_device *bdev, void *holder, |
| 785 | struct gendisk *disk) |
| 786 | { |
| 787 | return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir)); |
| 788 | } |
| 789 | EXPORT_SYMBOL_GPL(bd_claim_by_disk); |
| 790 | |
| 791 | /** |
| 792 | * bd_release_from_disk - wrapper function for bd_release_from_kobject() |
| 793 | * |
| 794 | * @bdev: block device to be claimed |
| 795 | * @disk: holder's gendisk |
| 796 | * |
| 797 | * Call bd_release_from_kobject() and put @disk->slave_dir. |
| 798 | */ |
| 799 | void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk) |
| 800 | { |
| 801 | bd_release_from_kobject(bdev, disk->slave_dir); |
| 802 | kobject_put(disk->slave_dir); |
| 803 | } |
| 804 | EXPORT_SYMBOL_GPL(bd_release_from_disk); |
| 805 | #endif |
| 806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | /* |
| 808 | * Tries to open block device by device number. Use it ONLY if you |
| 809 | * really do not have anything better - i.e. when you are behind a |
| 810 | * truly sucky interface and all you are given is a device number. _Never_ |
| 811 | * to be used for internal purposes. If you ever need it - reconsider |
| 812 | * your API. |
| 813 | */ |
| 814 | struct block_device *open_by_devnum(dev_t dev, unsigned mode) |
| 815 | { |
| 816 | struct block_device *bdev = bdget(dev); |
| 817 | int err = -ENOMEM; |
| 818 | int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY; |
| 819 | if (bdev) |
| 820 | err = blkdev_get(bdev, mode, flags); |
| 821 | return err ? ERR_PTR(err) : bdev; |
| 822 | } |
| 823 | |
| 824 | EXPORT_SYMBOL(open_by_devnum); |
| 825 | |
| 826 | /* |
| 827 | * This routine checks whether a removable media has been changed, |
| 828 | * and invalidates all buffer-cache-entries in that case. This |
| 829 | * is a relatively slow routine, so we have to try to minimize using |
| 830 | * it. Thus it is called only upon a 'mount' or 'open'. This |
| 831 | * is the best way of combining speed and utility, I think. |
| 832 | * People changing diskettes in the middle of an operation deserve |
| 833 | * to lose :-) |
| 834 | */ |
| 835 | int check_disk_change(struct block_device *bdev) |
| 836 | { |
| 837 | struct gendisk *disk = bdev->bd_disk; |
| 838 | struct block_device_operations * bdops = disk->fops; |
| 839 | |
| 840 | if (!bdops->media_changed) |
| 841 | return 0; |
| 842 | if (!bdops->media_changed(bdev->bd_disk)) |
| 843 | return 0; |
| 844 | |
Christoph Hellwig | 2ef4163 | 2005-05-05 16:15:59 -0700 | [diff] [blame] | 845 | if (__invalidate_device(bdev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | printk("VFS: busy inodes on changed media.\n"); |
| 847 | |
| 848 | if (bdops->revalidate_disk) |
| 849 | bdops->revalidate_disk(bdev->bd_disk); |
| 850 | if (bdev->bd_disk->minors > 1) |
| 851 | bdev->bd_invalidated = 1; |
| 852 | return 1; |
| 853 | } |
| 854 | |
| 855 | EXPORT_SYMBOL(check_disk_change); |
| 856 | |
| 857 | void bd_set_size(struct block_device *bdev, loff_t size) |
| 858 | { |
| 859 | unsigned bsize = bdev_hardsect_size(bdev); |
| 860 | |
| 861 | bdev->bd_inode->i_size = size; |
| 862 | while (bsize < PAGE_CACHE_SIZE) { |
| 863 | if (size & bsize) |
| 864 | break; |
| 865 | bsize <<= 1; |
| 866 | } |
| 867 | bdev->bd_block_size = bsize; |
| 868 | bdev->bd_inode->i_blkbits = blksize_bits(bsize); |
| 869 | } |
| 870 | EXPORT_SYMBOL(bd_set_size); |
| 871 | |
| 872 | static int do_open(struct block_device *bdev, struct file *file) |
| 873 | { |
| 874 | struct module *owner = NULL; |
| 875 | struct gendisk *disk; |
| 876 | int ret = -ENXIO; |
| 877 | int part; |
| 878 | |
| 879 | file->f_mapping = bdev->bd_inode->i_mapping; |
| 880 | lock_kernel(); |
| 881 | disk = get_gendisk(bdev->bd_dev, &part); |
| 882 | if (!disk) { |
| 883 | unlock_kernel(); |
| 884 | bdput(bdev); |
| 885 | return ret; |
| 886 | } |
| 887 | owner = disk->fops->owner; |
| 888 | |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 889 | mutex_lock(&bdev->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | if (!bdev->bd_openers) { |
| 891 | bdev->bd_disk = disk; |
| 892 | bdev->bd_contains = bdev; |
| 893 | if (!part) { |
| 894 | struct backing_dev_info *bdi; |
| 895 | if (disk->fops->open) { |
| 896 | ret = disk->fops->open(bdev->bd_inode, file); |
| 897 | if (ret) |
| 898 | goto out_first; |
| 899 | } |
| 900 | if (!bdev->bd_openers) { |
| 901 | bd_set_size(bdev,(loff_t)get_capacity(disk)<<9); |
| 902 | bdi = blk_get_backing_dev_info(bdev); |
| 903 | if (bdi == NULL) |
| 904 | bdi = &default_backing_dev_info; |
| 905 | bdev->bd_inode->i_data.backing_dev_info = bdi; |
| 906 | } |
| 907 | if (bdev->bd_invalidated) |
| 908 | rescan_partitions(disk, bdev); |
| 909 | } else { |
| 910 | struct hd_struct *p; |
| 911 | struct block_device *whole; |
| 912 | whole = bdget_disk(disk, 0); |
| 913 | ret = -ENOMEM; |
| 914 | if (!whole) |
| 915 | goto out_first; |
| 916 | ret = blkdev_get(whole, file->f_mode, file->f_flags); |
| 917 | if (ret) |
| 918 | goto out_first; |
| 919 | bdev->bd_contains = whole; |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 920 | mutex_lock(&whole->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | whole->bd_part_count++; |
| 922 | p = disk->part[part - 1]; |
| 923 | bdev->bd_inode->i_data.backing_dev_info = |
| 924 | whole->bd_inode->i_data.backing_dev_info; |
| 925 | if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) { |
| 926 | whole->bd_part_count--; |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 927 | mutex_unlock(&whole->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 934 | mutex_unlock(&whole->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } |
| 936 | } else { |
| 937 | put_disk(disk); |
| 938 | module_put(owner); |
| 939 | if (bdev->bd_contains == bdev) { |
| 940 | if (bdev->bd_disk->fops->open) { |
| 941 | ret = bdev->bd_disk->fops->open(bdev->bd_inode, file); |
| 942 | if (ret) |
| 943 | goto out; |
| 944 | } |
| 945 | if (bdev->bd_invalidated) |
| 946 | rescan_partitions(bdev->bd_disk, bdev); |
| 947 | } else { |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 948 | mutex_lock(&bdev->bd_contains->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | bdev->bd_contains->bd_part_count++; |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 950 | mutex_unlock(&bdev->bd_contains->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | bdev->bd_openers++; |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 954 | mutex_unlock(&bdev->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | unlock_kernel(); |
| 956 | return 0; |
| 957 | |
| 958 | out_first: |
| 959 | bdev->bd_disk = NULL; |
| 960 | bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info; |
| 961 | if (bdev != bdev->bd_contains) |
| 962 | blkdev_put(bdev->bd_contains); |
| 963 | bdev->bd_contains = NULL; |
| 964 | put_disk(disk); |
| 965 | module_put(owner); |
| 966 | out: |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 967 | mutex_unlock(&bdev->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | unlock_kernel(); |
| 969 | if (ret) |
| 970 | bdput(bdev); |
| 971 | return ret; |
| 972 | } |
| 973 | |
| 974 | int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags) |
| 975 | { |
| 976 | /* |
| 977 | * This crockload is due to bad choice of ->open() type. |
| 978 | * It will go away. |
| 979 | * For now, block device ->open() routine must _not_ |
| 980 | * examine anything in 'inode' argument except ->i_rdev. |
| 981 | */ |
| 982 | struct file fake_file = {}; |
| 983 | struct dentry fake_dentry = {}; |
| 984 | fake_file.f_mode = mode; |
| 985 | fake_file.f_flags = flags; |
| 986 | fake_file.f_dentry = &fake_dentry; |
| 987 | fake_dentry.d_inode = bdev->bd_inode; |
| 988 | |
| 989 | return do_open(bdev, &fake_file); |
| 990 | } |
| 991 | |
| 992 | EXPORT_SYMBOL(blkdev_get); |
| 993 | |
| 994 | static int blkdev_open(struct inode * inode, struct file * filp) |
| 995 | { |
| 996 | struct block_device *bdev; |
| 997 | int res; |
| 998 | |
| 999 | /* |
| 1000 | * Preserve backwards compatibility and allow large file access |
| 1001 | * even if userspace doesn't ask for it explicitly. Some mkfs |
| 1002 | * binary needs it. We might want to drop this workaround |
| 1003 | * during an unstable branch. |
| 1004 | */ |
| 1005 | filp->f_flags |= O_LARGEFILE; |
| 1006 | |
| 1007 | bdev = bd_acquire(inode); |
| 1008 | |
| 1009 | res = do_open(bdev, filp); |
| 1010 | if (res) |
| 1011 | return res; |
| 1012 | |
| 1013 | if (!(filp->f_flags & O_EXCL) ) |
| 1014 | return 0; |
| 1015 | |
| 1016 | if (!(res = bd_claim(bdev, filp))) |
| 1017 | return 0; |
| 1018 | |
| 1019 | blkdev_put(bdev); |
| 1020 | return res; |
| 1021 | } |
| 1022 | |
| 1023 | int blkdev_put(struct block_device *bdev) |
| 1024 | { |
| 1025 | int ret = 0; |
| 1026 | struct inode *bd_inode = bdev->bd_inode; |
| 1027 | struct gendisk *disk = bdev->bd_disk; |
| 1028 | |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1029 | mutex_lock(&bdev->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | lock_kernel(); |
| 1031 | if (!--bdev->bd_openers) { |
| 1032 | sync_blockdev(bdev); |
| 1033 | kill_bdev(bdev); |
| 1034 | } |
| 1035 | if (bdev->bd_contains == bdev) { |
| 1036 | if (disk->fops->release) |
| 1037 | ret = disk->fops->release(bd_inode, NULL); |
| 1038 | } else { |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1039 | mutex_lock(&bdev->bd_contains->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | bdev->bd_contains->bd_part_count--; |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1041 | mutex_unlock(&bdev->bd_contains->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | } |
| 1043 | if (!bdev->bd_openers) { |
| 1044 | struct module *owner = disk->fops->owner; |
| 1045 | |
| 1046 | put_disk(disk); |
| 1047 | module_put(owner); |
| 1048 | |
| 1049 | if (bdev->bd_contains != bdev) { |
| 1050 | kobject_put(&bdev->bd_part->kobj); |
| 1051 | bdev->bd_part = NULL; |
| 1052 | } |
| 1053 | bdev->bd_disk = NULL; |
| 1054 | bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info; |
| 1055 | if (bdev != bdev->bd_contains) { |
| 1056 | blkdev_put(bdev->bd_contains); |
| 1057 | } |
| 1058 | bdev->bd_contains = NULL; |
| 1059 | } |
| 1060 | unlock_kernel(); |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1061 | mutex_unlock(&bdev->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | bdput(bdev); |
| 1063 | return ret; |
| 1064 | } |
| 1065 | |
| 1066 | EXPORT_SYMBOL(blkdev_put); |
| 1067 | |
| 1068 | static int blkdev_close(struct inode * inode, struct file * filp) |
| 1069 | { |
| 1070 | struct block_device *bdev = I_BDEV(filp->f_mapping->host); |
| 1071 | if (bdev->bd_holder == filp) |
| 1072 | bd_release(bdev); |
| 1073 | return blkdev_put(bdev); |
| 1074 | } |
| 1075 | |
| 1076 | static ssize_t blkdev_file_write(struct file *file, const char __user *buf, |
| 1077 | size_t count, loff_t *ppos) |
| 1078 | { |
| 1079 | struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count }; |
| 1080 | |
| 1081 | return generic_file_write_nolock(file, &local_iov, 1, ppos); |
| 1082 | } |
| 1083 | |
| 1084 | static ssize_t blkdev_file_aio_write(struct kiocb *iocb, const char __user *buf, |
| 1085 | size_t count, loff_t pos) |
| 1086 | { |
| 1087 | struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count }; |
| 1088 | |
| 1089 | return generic_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos); |
| 1090 | } |
| 1091 | |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 1092 | static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | { |
| 1094 | return blkdev_ioctl(file->f_mapping->host, file, cmd, arg); |
| 1095 | } |
| 1096 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 1097 | const struct address_space_operations def_blk_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | .readpage = blkdev_readpage, |
| 1099 | .writepage = blkdev_writepage, |
| 1100 | .sync_page = block_sync_page, |
| 1101 | .prepare_write = blkdev_prepare_write, |
| 1102 | .commit_write = blkdev_commit_write, |
| 1103 | .writepages = generic_writepages, |
| 1104 | .direct_IO = blkdev_direct_IO, |
| 1105 | }; |
| 1106 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1107 | const struct file_operations def_blk_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | .open = blkdev_open, |
| 1109 | .release = blkdev_close, |
| 1110 | .llseek = block_llseek, |
| 1111 | .read = generic_file_read, |
| 1112 | .write = blkdev_file_write, |
| 1113 | .aio_read = generic_file_aio_read, |
| 1114 | .aio_write = blkdev_file_aio_write, |
| 1115 | .mmap = generic_file_mmap, |
| 1116 | .fsync = block_fsync, |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 1117 | .unlocked_ioctl = block_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | #ifdef CONFIG_COMPAT |
| 1119 | .compat_ioctl = compat_blkdev_ioctl, |
| 1120 | #endif |
| 1121 | .readv = generic_file_readv, |
| 1122 | .writev = generic_file_write_nolock, |
| 1123 | .sendfile = generic_file_sendfile, |
Jens Axboe | 7f9c51f | 2006-05-01 19:59:32 +0200 | [diff] [blame] | 1124 | .splice_read = generic_file_splice_read, |
| 1125 | .splice_write = generic_file_splice_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | }; |
| 1127 | |
| 1128 | int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg) |
| 1129 | { |
| 1130 | int res; |
| 1131 | mm_segment_t old_fs = get_fs(); |
| 1132 | set_fs(KERNEL_DS); |
| 1133 | res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg); |
| 1134 | set_fs(old_fs); |
| 1135 | return res; |
| 1136 | } |
| 1137 | |
| 1138 | EXPORT_SYMBOL(ioctl_by_bdev); |
| 1139 | |
| 1140 | /** |
| 1141 | * lookup_bdev - lookup a struct block_device by name |
| 1142 | * |
| 1143 | * @path: special file representing the block device |
| 1144 | * |
| 1145 | * Get a reference to the blockdevice at @path in the current |
| 1146 | * namespace if possible and return it. Return ERR_PTR(error) |
| 1147 | * otherwise. |
| 1148 | */ |
| 1149 | struct block_device *lookup_bdev(const char *path) |
| 1150 | { |
| 1151 | struct block_device *bdev; |
| 1152 | struct inode *inode; |
| 1153 | struct nameidata nd; |
| 1154 | int error; |
| 1155 | |
| 1156 | if (!path || !*path) |
| 1157 | return ERR_PTR(-EINVAL); |
| 1158 | |
| 1159 | error = path_lookup(path, LOOKUP_FOLLOW, &nd); |
| 1160 | if (error) |
| 1161 | return ERR_PTR(error); |
| 1162 | |
| 1163 | inode = nd.dentry->d_inode; |
| 1164 | error = -ENOTBLK; |
| 1165 | if (!S_ISBLK(inode->i_mode)) |
| 1166 | goto fail; |
| 1167 | error = -EACCES; |
| 1168 | if (nd.mnt->mnt_flags & MNT_NODEV) |
| 1169 | goto fail; |
| 1170 | error = -ENOMEM; |
| 1171 | bdev = bd_acquire(inode); |
| 1172 | if (!bdev) |
| 1173 | goto fail; |
| 1174 | out: |
| 1175 | path_release(&nd); |
| 1176 | return bdev; |
| 1177 | fail: |
| 1178 | bdev = ERR_PTR(error); |
| 1179 | goto out; |
| 1180 | } |
| 1181 | |
| 1182 | /** |
| 1183 | * open_bdev_excl - open a block device by name and set it up for use |
| 1184 | * |
| 1185 | * @path: special file representing the block device |
| 1186 | * @flags: %MS_RDONLY for opening read-only |
| 1187 | * @holder: owner for exclusion |
| 1188 | * |
| 1189 | * Open the blockdevice described by the special file at @path, claim it |
| 1190 | * for the @holder. |
| 1191 | */ |
| 1192 | struct block_device *open_bdev_excl(const char *path, int flags, void *holder) |
| 1193 | { |
| 1194 | struct block_device *bdev; |
| 1195 | mode_t mode = FMODE_READ; |
| 1196 | int error = 0; |
| 1197 | |
| 1198 | bdev = lookup_bdev(path); |
| 1199 | if (IS_ERR(bdev)) |
| 1200 | return bdev; |
| 1201 | |
| 1202 | if (!(flags & MS_RDONLY)) |
| 1203 | mode |= FMODE_WRITE; |
| 1204 | error = blkdev_get(bdev, mode, 0); |
| 1205 | if (error) |
| 1206 | return ERR_PTR(error); |
| 1207 | error = -EACCES; |
| 1208 | if (!(flags & MS_RDONLY) && bdev_read_only(bdev)) |
| 1209 | goto blkdev_put; |
| 1210 | error = bd_claim(bdev, holder); |
| 1211 | if (error) |
| 1212 | goto blkdev_put; |
| 1213 | |
| 1214 | return bdev; |
| 1215 | |
| 1216 | blkdev_put: |
| 1217 | blkdev_put(bdev); |
| 1218 | return ERR_PTR(error); |
| 1219 | } |
| 1220 | |
| 1221 | EXPORT_SYMBOL(open_bdev_excl); |
| 1222 | |
| 1223 | /** |
| 1224 | * close_bdev_excl - release a blockdevice openen by open_bdev_excl() |
| 1225 | * |
| 1226 | * @bdev: blockdevice to close |
| 1227 | * |
| 1228 | * This is the counterpart to open_bdev_excl(). |
| 1229 | */ |
| 1230 | void close_bdev_excl(struct block_device *bdev) |
| 1231 | { |
| 1232 | bd_release(bdev); |
| 1233 | blkdev_put(bdev); |
| 1234 | } |
| 1235 | |
| 1236 | EXPORT_SYMBOL(close_bdev_excl); |