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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 26 | struct bdev_inode { |
| 27 | struct block_device bdev; |
| 28 | struct inode vfs_inode; |
| 29 | }; |
| 30 | |
| 31 | static inline struct bdev_inode *BDEV_I(struct inode *inode) |
| 32 | { |
| 33 | return container_of(inode, struct bdev_inode, vfs_inode); |
| 34 | } |
| 35 | |
| 36 | inline struct block_device *I_BDEV(struct inode *inode) |
| 37 | { |
| 38 | return &BDEV_I(inode)->bdev; |
| 39 | } |
| 40 | |
| 41 | EXPORT_SYMBOL(I_BDEV); |
| 42 | |
| 43 | static 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 | /* Kill _all_ buffers, dirty or not.. */ |
| 57 | static 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 | |
| 63 | int set_blocksize(struct block_device *bdev, int size) |
| 64 | { |
| 65 | /* Size must be a power of two, and between 512 and PAGE_SIZE */ |
| 66 | if (size > PAGE_SIZE || size < 512 || (size & (size-1))) |
| 67 | return -EINVAL; |
| 68 | |
| 69 | /* Size cannot be smaller than the size supported by the device */ |
| 70 | if (size < bdev_hardsect_size(bdev)) |
| 71 | return -EINVAL; |
| 72 | |
| 73 | /* Don't change the size if it is same as current */ |
| 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 | |
| 83 | EXPORT_SYMBOL(set_blocksize); |
| 84 | |
| 85 | int sb_set_blocksize(struct super_block *sb, int size) |
| 86 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | if (set_blocksize(sb->s_bdev, size)) |
| 88 | return 0; |
| 89 | /* If we get here, we know size is power of two |
| 90 | * and it's value is between 512 and PAGE_SIZE */ |
| 91 | sb->s_blocksize = size; |
Coywolf Qi Hunt | 38885bd | 2006-03-24 03:18:05 -0800 | [diff] [blame] | 92 | sb->s_blocksize_bits = blksize_bits(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | return sb->s_blocksize; |
| 94 | } |
| 95 | |
| 96 | EXPORT_SYMBOL(sb_set_blocksize); |
| 97 | |
| 98 | int 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 | |
| 106 | EXPORT_SYMBOL(sb_min_blocksize); |
| 107 | |
| 108 | static int |
| 109 | blkdev_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 | * for reads, we're just trying to fill a partial page. |
| 118 | * return a hole, they will have to call get_block again |
| 119 | * before they can fill it, and they will get -EIO at that |
| 120 | * time |
| 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 | |
| 130 | static int |
| 131 | blkdev_get_blocks(struct inode *inode, sector_t iblock, |
Badari Pulavarty | 1d8fa7a | 2006-03-26 01:38:02 -0800 | [diff] [blame] | 132 | struct buffer_head *bh, int create) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
| 134 | sector_t end_block = max_block(I_BDEV(inode)); |
Badari Pulavarty | 1d8fa7a | 2006-03-26 01:38:02 -0800 | [diff] [blame] | 135 | unsigned long max_blocks = bh->b_size >> inode->i_blkbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; /* write fully beyond EOF */ |
| 142 | /* |
| 143 | * It is a read which is fully beyond EOF. We return |
| 144 | * a !buffer_mapped buffer |
| 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 | |
| 158 | static ssize_t |
| 159 | blkdev_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 | |
| 169 | static int blkdev_writepage(struct page *page, struct writeback_control *wbc) |
| 170 | { |
| 171 | return block_write_full_page(page, blkdev_get_block, wbc); |
| 172 | } |
| 173 | |
| 174 | static int blkdev_readpage(struct file * file, struct page * page) |
| 175 | { |
| 176 | return block_read_full_page(page, blkdev_get_block); |
| 177 | } |
| 178 | |
| 179 | static 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 | |
| 184 | static 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 | * private llseek: |
| 191 | * for a block special file file->f_dentry->d_inode->i_size is zero |
| 192 | * so we compute the size by hand (just as in block_read/write above) |
| 193 | */ |
| 194 | static 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 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 200 | mutex_lock(&bd_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 217 | mutex_unlock(&bd_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | return retval; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Filp is never NULL; the only case when ->fsync() is called with |
| 223 | * NULL first argument is nfsd_sync_dir() and that's not a directory. |
| 224 | */ |
| 225 | |
| 226 | static 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 | * pseudo-fs |
| 233 | */ |
| 234 | |
| 235 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock); |
Eric Dumazet | fa3536c | 2006-03-26 01:37:24 -0800 | [diff] [blame] | 236 | static kmem_cache_t * bdev_cachep __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
| 238 | static 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 | |
| 246 | static 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 | |
| 254 | static 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)); |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 263 | mutex_init(&bdev->bd_mutex); |
| 264 | mutex_init(&bdev->bd_mount_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | INIT_LIST_HEAD(&bdev->bd_inodes); |
| 266 | INIT_LIST_HEAD(&bdev->bd_list); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 267 | #ifdef CONFIG_SYSFS |
| 268 | INIT_LIST_HEAD(&bdev->bd_holder_list); |
| 269 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | inode_init_once(&ei->vfs_inode); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | static 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 | |
| 281 | static 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 | |
| 293 | static 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 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 301 | static int bd_get_sb(struct file_system_type *fs_type, |
| 302 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | { |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 304 | return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static struct file_system_type bd_type = { |
| 308 | .name = "bdev", |
| 309 | .get_sb = bd_get_sb, |
| 310 | .kill_sb = kill_anon_super, |
| 311 | }; |
| 312 | |
Eric Dumazet | fa3536c | 2006-03-26 01:37:24 -0800 | [diff] [blame] | 313 | static struct vfsmount *bd_mnt __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | struct super_block *blockdev_superblock; |
| 315 | |
| 316 | void __init bdev_cache_init(void) |
| 317 | { |
| 318 | int err; |
| 319 | bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode), |
Paul Jackson | fffb60f | 2006-03-24 03:16:06 -0800 | [diff] [blame] | 320 | 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
| 321 | SLAB_MEM_SPREAD|SLAB_PANIC), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; /* For writeback */ |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Most likely _very_ bad one - but then it's hardly critical for small |
| 335 | * /dev and can be fixed when somebody will need really large one. |
| 336 | * Keep in mind that it will be fed through icache hash function too. |
| 337 | */ |
| 338 | static inline unsigned long hash(dev_t dev) |
| 339 | { |
| 340 | return MAJOR(dev)+MINOR(dev); |
| 341 | } |
| 342 | |
| 343 | static int bdev_test(struct inode *inode, void *data) |
| 344 | { |
| 345 | return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data; |
| 346 | } |
| 347 | |
| 348 | static int bdev_set(struct inode *inode, void *data) |
| 349 | { |
| 350 | BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data; |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static LIST_HEAD(all_bdevs); |
| 355 | |
| 356 | struct 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 | |
| 389 | EXPORT_SYMBOL(bdget); |
| 390 | |
| 391 | long 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 | |
| 405 | void bdput(struct block_device *bdev) |
| 406 | { |
| 407 | iput(bdev->bd_inode); |
| 408 | } |
| 409 | |
| 410 | EXPORT_SYMBOL(bdput); |
| 411 | |
| 412 | static struct block_device *bd_acquire(struct inode *inode) |
| 413 | { |
| 414 | struct block_device *bdev; |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | spin_lock(&bdev_lock); |
| 417 | bdev = inode->i_bdev; |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 418 | if (bdev) { |
| 419 | atomic_inc(&bdev->bd_inode->i_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | spin_unlock(&bdev_lock); |
| 421 | return bdev; |
| 422 | } |
| 423 | spin_unlock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | bdev = bdget(inode->i_rdev); |
| 426 | if (bdev) { |
| 427 | spin_lock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 428 | if (!inode->i_bdev) { |
| 429 | /* |
| 430 | * We take an additional bd_inode->i_count for inode, |
| 431 | * and it's released in clear_inode() of inode. |
| 432 | * So, we can access it via ->i_mapping always |
| 433 | * without igrab(). |
| 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | spin_unlock(&bdev_lock); |
| 441 | } |
| 442 | return bdev; |
| 443 | } |
| 444 | |
| 445 | /* Call when you free inode */ |
| 446 | |
| 447 | void bd_forget(struct inode *inode) |
| 448 | { |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 449 | struct block_device *bdev = NULL; |
| 450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | spin_lock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 452 | if (inode->i_bdev) { |
| 453 | if (inode->i_sb != blockdev_superblock) |
| 454 | bdev = inode->i_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | __bd_forget(inode); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 456 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | spin_unlock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 458 | |
| 459 | if (bdev) |
| 460 | iput(bdev->bd_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | int bd_claim(struct block_device *bdev, void *holder) |
| 464 | { |
| 465 | int res; |
| 466 | spin_lock(&bdev_lock); |
| 467 | |
| 468 | /* first decide result */ |
| 469 | if (bdev->bd_holder == holder) |
| 470 | res = 0; /* already a holder */ |
| 471 | else if (bdev->bd_holder != NULL) |
| 472 | res = -EBUSY; /* held by someone else */ |
| 473 | else if (bdev->bd_contains == bdev) |
| 474 | res = 0; /* is a whole device which isn't held */ |
| 475 | |
| 476 | else if (bdev->bd_contains->bd_holder == bd_claim) |
| 477 | res = 0; /* is a partition of a device that is being partitioned */ |
| 478 | else if (bdev->bd_contains->bd_holder != NULL) |
| 479 | res = -EBUSY; /* is a partition of a held device */ |
| 480 | else |
| 481 | res = 0; /* is a partition of an un-held device */ |
| 482 | |
| 483 | /* now impose change */ |
| 484 | if (res==0) { |
| 485 | /* note that for a whole device bd_holders |
| 486 | * will be incremented twice, and bd_holder will |
| 487 | * be set to bd_claim before being set to holder |
| 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 | |
| 498 | EXPORT_SYMBOL(bd_claim); |
| 499 | |
| 500 | void 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 | |
| 510 | EXPORT_SYMBOL(bd_release); |
| 511 | |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 512 | #ifdef CONFIG_SYSFS |
| 513 | /* |
| 514 | * Functions for bd_claim_by_kobject / bd_release_from_kobject |
| 515 | * |
| 516 | * If a kobject is passed to bd_claim_by_kobject() |
| 517 | * and the kobject has a parent directory, |
| 518 | * following symlinks are created: |
| 519 | * o from the kobject to the claimed bdev |
| 520 | * o from "holders" directory of the bdev to the parent of the kobject |
| 521 | * bd_release_from_kobject() removes these symlinks. |
| 522 | * |
| 523 | * Example: |
| 524 | * If /dev/dm-0 maps to /dev/sda, kobject corresponding to |
| 525 | * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then: |
| 526 | * /sys/block/dm-0/slaves/sda --> /sys/block/sda |
| 527 | * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0 |
| 528 | */ |
| 529 | |
| 530 | static 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 | |
| 538 | static 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 | |
Andrew Morton | 4d7dd8f | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 546 | static int add_symlink(struct kobject *from, struct kobject *to) |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 547 | { |
| 548 | if (!from || !to) |
Andrew Morton | 4d7dd8f | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 549 | return 0; |
| 550 | return sysfs_create_link(from, to, kobject_name(to)); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | static 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 | * 'struct bd_holder' contains pointers to kobjects symlinked by |
| 562 | * bd_claim_by_kobject. |
| 563 | * It's connected to bd_holder_list which is protected by bdev->bd_sem. |
| 564 | */ |
| 565 | struct bd_holder { |
| 566 | struct list_head list; /* chain of holders of the bdev */ |
| 567 | int count; /* references from the holder */ |
| 568 | struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */ |
| 569 | struct kobject *hdev; /* e.g. "/block/dm-0" */ |
| 570 | struct kobject *hdir; /* e.g. "/block/sda/holders" */ |
| 571 | struct kobject *sdev; /* e.g. "/block/sda" */ |
| 572 | }; |
| 573 | |
| 574 | /* |
| 575 | * Get references of related kobjects at once. |
| 576 | * Returns 1 on success. 0 on failure. |
| 577 | * |
| 578 | * Should call bd_holder_release_dirs() after successful use. |
| 579 | */ |
| 580 | static 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 | |
| 604 | fail_put_sdev: |
| 605 | kobject_put(bo->sdev); |
| 606 | fail_put_hdev: |
| 607 | kobject_put(bo->hdev); |
| 608 | fail_put_sdir: |
| 609 | kobject_put(bo->sdir); |
| 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | /* Put references of related kobjects at once. */ |
| 615 | static 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 | |
| 623 | static 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 | |
| 637 | static void free_bd_holder(struct bd_holder *bo) |
| 638 | { |
| 639 | kfree(bo); |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * add_bd_holder - create sysfs symlinks for bd_claim() relationship |
| 644 | * |
| 645 | * @bdev: block device to be bd_claimed |
| 646 | * @bo: preallocated and initialized by alloc_bd_holder() |
| 647 | * |
| 648 | * If there is no matching entry with @bo in @bdev->bd_holder_list, |
| 649 | * add @bo to the list, create symlinks. |
| 650 | * |
Andrew Morton | 4d7dd8f | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 651 | * Returns 0 if symlinks are created or already there. |
| 652 | * Returns -ve if something fails and @bo can be freed. |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 653 | */ |
| 654 | static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo) |
| 655 | { |
| 656 | struct bd_holder *tmp; |
Andrew Morton | 4d7dd8f | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 657 | int ret; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 658 | |
| 659 | if (!bo) |
Andrew Morton | 4d7dd8f | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 660 | return -EINVAL; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 661 | |
| 662 | list_for_each_entry(tmp, &bdev->bd_holder_list, list) { |
| 663 | if (tmp->sdir == bo->sdir) { |
| 664 | tmp->count++; |
Andrew Morton | 4d7dd8f | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 665 | /* We've already done what we need to do here. */ |
| 666 | free_bd_holder(bo); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 667 | return 0; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | if (!bd_holder_grab_dirs(bdev, bo)) |
Andrew Morton | 4d7dd8f | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 672 | return -EBUSY; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 673 | |
Andrew Morton | 4d7dd8f | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 674 | ret = add_symlink(bo->sdir, bo->sdev); |
| 675 | if (ret == 0) { |
| 676 | ret = add_symlink(bo->hdir, bo->hdev); |
| 677 | if (ret) |
| 678 | del_symlink(bo->sdir, bo->sdev); |
| 679 | } |
| 680 | if (ret == 0) |
| 681 | list_add_tail(&bo->list, &bdev->bd_holder_list); |
| 682 | return ret; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /** |
| 686 | * del_bd_holder - delete sysfs symlinks for bd_claim() relationship |
| 687 | * |
| 688 | * @bdev: block device to be bd_claimed |
| 689 | * @kobj: holder's kobject |
| 690 | * |
| 691 | * If there is matching entry with @kobj in @bdev->bd_holder_list |
| 692 | * and no other bd_claim() from the same kobject, |
| 693 | * remove the struct bd_holder from the list, delete symlinks for it. |
| 694 | * |
| 695 | * Returns a pointer to the struct bd_holder when it's removed from the list |
| 696 | * and ready to be freed. |
| 697 | * Returns NULL if matching claim isn't found or there is other bd_claim() |
| 698 | * by the same kobject. |
| 699 | */ |
| 700 | static struct bd_holder *del_bd_holder(struct block_device *bdev, |
| 701 | struct kobject *kobj) |
| 702 | { |
| 703 | struct bd_holder *bo; |
| 704 | |
| 705 | list_for_each_entry(bo, &bdev->bd_holder_list, list) { |
| 706 | if (bo->sdir == kobj) { |
| 707 | bo->count--; |
| 708 | BUG_ON(bo->count < 0); |
| 709 | if (!bo->count) { |
| 710 | list_del(&bo->list); |
| 711 | del_symlink(bo->sdir, bo->sdev); |
| 712 | del_symlink(bo->hdir, bo->hdev); |
| 713 | bd_holder_release_dirs(bo); |
| 714 | return bo; |
| 715 | } |
| 716 | break; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | return NULL; |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * bd_claim_by_kobject - bd_claim() with additional kobject signature |
| 725 | * |
| 726 | * @bdev: block device to be claimed |
| 727 | * @holder: holder's signature |
| 728 | * @kobj: holder's kobject |
| 729 | * |
| 730 | * Do bd_claim() and if it succeeds, create sysfs symlinks between |
| 731 | * the bdev and the holder's kobject. |
| 732 | * Use bd_release_from_kobject() when relesing the claimed bdev. |
| 733 | * |
| 734 | * Returns 0 on success. (same as bd_claim()) |
| 735 | * Returns errno on failure. |
| 736 | */ |
| 737 | static int bd_claim_by_kobject(struct block_device *bdev, void *holder, |
| 738 | struct kobject *kobj) |
| 739 | { |
| 740 | int res; |
| 741 | struct bd_holder *bo; |
| 742 | |
| 743 | if (!kobj) |
| 744 | return -EINVAL; |
| 745 | |
| 746 | bo = alloc_bd_holder(kobj); |
| 747 | if (!bo) |
| 748 | return -ENOMEM; |
| 749 | |
Ingo Molnar | 663d440 | 2006-07-03 00:25:33 -0700 | [diff] [blame] | 750 | mutex_lock_nested(&bdev->bd_mutex, BD_MUTEX_PARTITION); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 751 | res = bd_claim(bdev, holder); |
Andrew Morton | 4d7dd8f | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 752 | if (res == 0) |
| 753 | res = add_bd_holder(bdev, bo); |
| 754 | if (res) |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 755 | free_bd_holder(bo); |
Jun'ichi Nomura | b4cf1b7 | 2006-03-27 01:18:00 -0800 | [diff] [blame] | 756 | mutex_unlock(&bdev->bd_mutex); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 757 | |
| 758 | return res; |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * bd_release_from_kobject - bd_release() with additional kobject signature |
| 763 | * |
| 764 | * @bdev: block device to be released |
| 765 | * @kobj: holder's kobject |
| 766 | * |
| 767 | * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject(). |
| 768 | */ |
| 769 | static void bd_release_from_kobject(struct block_device *bdev, |
| 770 | struct kobject *kobj) |
| 771 | { |
| 772 | struct bd_holder *bo; |
| 773 | |
| 774 | if (!kobj) |
| 775 | return; |
| 776 | |
Ingo Molnar | 663d440 | 2006-07-03 00:25:33 -0700 | [diff] [blame] | 777 | mutex_lock_nested(&bdev->bd_mutex, BD_MUTEX_PARTITION); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 778 | bd_release(bdev); |
| 779 | if ((bo = del_bd_holder(bdev, kobj))) |
| 780 | free_bd_holder(bo); |
Jun'ichi Nomura | b4cf1b7 | 2006-03-27 01:18:00 -0800 | [diff] [blame] | 781 | mutex_unlock(&bdev->bd_mutex); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | /** |
| 785 | * bd_claim_by_disk - wrapper function for bd_claim_by_kobject() |
| 786 | * |
| 787 | * @bdev: block device to be claimed |
| 788 | * @holder: holder's signature |
| 789 | * @disk: holder's gendisk |
| 790 | * |
| 791 | * Call bd_claim_by_kobject() with getting @disk->slave_dir. |
| 792 | */ |
| 793 | int bd_claim_by_disk(struct block_device *bdev, void *holder, |
| 794 | struct gendisk *disk) |
| 795 | { |
| 796 | return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir)); |
| 797 | } |
| 798 | EXPORT_SYMBOL_GPL(bd_claim_by_disk); |
| 799 | |
| 800 | /** |
| 801 | * bd_release_from_disk - wrapper function for bd_release_from_kobject() |
| 802 | * |
| 803 | * @bdev: block device to be claimed |
| 804 | * @disk: holder's gendisk |
| 805 | * |
| 806 | * Call bd_release_from_kobject() and put @disk->slave_dir. |
| 807 | */ |
| 808 | void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk) |
| 809 | { |
| 810 | bd_release_from_kobject(bdev, disk->slave_dir); |
| 811 | kobject_put(disk->slave_dir); |
| 812 | } |
| 813 | EXPORT_SYMBOL_GPL(bd_release_from_disk); |
| 814 | #endif |
| 815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | /* |
| 817 | * Tries to open block device by device number. Use it ONLY if you |
| 818 | * really do not have anything better - i.e. when you are behind a |
| 819 | * truly sucky interface and all you are given is a device number. _Never_ |
| 820 | * to be used for internal purposes. If you ever need it - reconsider |
| 821 | * your API. |
| 822 | */ |
| 823 | struct block_device *open_by_devnum(dev_t dev, unsigned mode) |
| 824 | { |
| 825 | struct block_device *bdev = bdget(dev); |
| 826 | int err = -ENOMEM; |
| 827 | int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY; |
| 828 | if (bdev) |
| 829 | err = blkdev_get(bdev, mode, flags); |
| 830 | return err ? ERR_PTR(err) : bdev; |
| 831 | } |
| 832 | |
| 833 | EXPORT_SYMBOL(open_by_devnum); |
| 834 | |
Ingo Molnar | 663d440 | 2006-07-03 00:25:33 -0700 | [diff] [blame] | 835 | static int |
| 836 | blkdev_get_partition(struct block_device *bdev, mode_t mode, unsigned flags); |
| 837 | |
| 838 | struct block_device *open_partition_by_devnum(dev_t dev, unsigned mode) |
| 839 | { |
| 840 | struct block_device *bdev = bdget(dev); |
| 841 | int err = -ENOMEM; |
| 842 | int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY; |
| 843 | if (bdev) |
| 844 | err = blkdev_get_partition(bdev, mode, flags); |
| 845 | return err ? ERR_PTR(err) : bdev; |
| 846 | } |
| 847 | |
| 848 | EXPORT_SYMBOL(open_partition_by_devnum); |
| 849 | |
| 850 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | /* |
| 852 | * This routine checks whether a removable media has been changed, |
| 853 | * and invalidates all buffer-cache-entries in that case. This |
| 854 | * is a relatively slow routine, so we have to try to minimize using |
| 855 | * it. Thus it is called only upon a 'mount' or 'open'. This |
| 856 | * is the best way of combining speed and utility, I think. |
| 857 | * People changing diskettes in the middle of an operation deserve |
| 858 | * to lose :-) |
| 859 | */ |
| 860 | int check_disk_change(struct block_device *bdev) |
| 861 | { |
| 862 | struct gendisk *disk = bdev->bd_disk; |
| 863 | struct block_device_operations * bdops = disk->fops; |
| 864 | |
| 865 | if (!bdops->media_changed) |
| 866 | return 0; |
| 867 | if (!bdops->media_changed(bdev->bd_disk)) |
| 868 | return 0; |
| 869 | |
Christoph Hellwig | 2ef4163 | 2005-05-05 16:15:59 -0700 | [diff] [blame] | 870 | if (__invalidate_device(bdev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | printk("VFS: busy inodes on changed media.\n"); |
| 872 | |
| 873 | if (bdops->revalidate_disk) |
| 874 | bdops->revalidate_disk(bdev->bd_disk); |
| 875 | if (bdev->bd_disk->minors > 1) |
| 876 | bdev->bd_invalidated = 1; |
| 877 | return 1; |
| 878 | } |
| 879 | |
| 880 | EXPORT_SYMBOL(check_disk_change); |
| 881 | |
| 882 | void bd_set_size(struct block_device *bdev, loff_t size) |
| 883 | { |
| 884 | unsigned bsize = bdev_hardsect_size(bdev); |
| 885 | |
| 886 | bdev->bd_inode->i_size = size; |
| 887 | while (bsize < PAGE_CACHE_SIZE) { |
| 888 | if (size & bsize) |
| 889 | break; |
| 890 | bsize <<= 1; |
| 891 | } |
| 892 | bdev->bd_block_size = bsize; |
| 893 | bdev->bd_inode->i_blkbits = blksize_bits(bsize); |
| 894 | } |
| 895 | EXPORT_SYMBOL(bd_set_size); |
| 896 | |
Peter Zijlstra | 6946bd6 | 2006-08-27 01:23:31 -0700 | [diff] [blame] | 897 | static int __blkdev_put(struct block_device *bdev, unsigned int subclass) |
| 898 | { |
| 899 | int ret = 0; |
| 900 | struct inode *bd_inode = bdev->bd_inode; |
| 901 | struct gendisk *disk = bdev->bd_disk; |
| 902 | |
| 903 | mutex_lock_nested(&bdev->bd_mutex, subclass); |
| 904 | lock_kernel(); |
| 905 | if (!--bdev->bd_openers) { |
| 906 | sync_blockdev(bdev); |
| 907 | kill_bdev(bdev); |
| 908 | } |
| 909 | if (bdev->bd_contains == bdev) { |
| 910 | if (disk->fops->release) |
| 911 | ret = disk->fops->release(bd_inode, NULL); |
| 912 | } else { |
| 913 | mutex_lock_nested(&bdev->bd_contains->bd_mutex, |
| 914 | subclass + 1); |
| 915 | bdev->bd_contains->bd_part_count--; |
| 916 | mutex_unlock(&bdev->bd_contains->bd_mutex); |
| 917 | } |
| 918 | if (!bdev->bd_openers) { |
| 919 | struct module *owner = disk->fops->owner; |
| 920 | |
| 921 | put_disk(disk); |
| 922 | module_put(owner); |
| 923 | |
| 924 | if (bdev->bd_contains != bdev) { |
| 925 | kobject_put(&bdev->bd_part->kobj); |
| 926 | bdev->bd_part = NULL; |
| 927 | } |
| 928 | bdev->bd_disk = NULL; |
| 929 | bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info; |
| 930 | if (bdev != bdev->bd_contains) |
| 931 | __blkdev_put(bdev->bd_contains, subclass + 1); |
| 932 | bdev->bd_contains = NULL; |
| 933 | } |
| 934 | unlock_kernel(); |
| 935 | mutex_unlock(&bdev->bd_mutex); |
| 936 | bdput(bdev); |
| 937 | return ret; |
| 938 | } |
| 939 | |
| 940 | int blkdev_put(struct block_device *bdev) |
| 941 | { |
| 942 | return __blkdev_put(bdev, BD_MUTEX_NORMAL); |
| 943 | } |
| 944 | EXPORT_SYMBOL(blkdev_put); |
| 945 | |
| 946 | int blkdev_put_partition(struct block_device *bdev) |
| 947 | { |
| 948 | return __blkdev_put(bdev, BD_MUTEX_PARTITION); |
| 949 | } |
| 950 | EXPORT_SYMBOL(blkdev_put_partition); |
| 951 | |
Ingo Molnar | 663d440 | 2006-07-03 00:25:33 -0700 | [diff] [blame] | 952 | static int |
| 953 | blkdev_get_whole(struct block_device *bdev, mode_t mode, unsigned flags); |
| 954 | |
| 955 | static int |
| 956 | do_open(struct block_device *bdev, struct file *file, unsigned int subclass) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | { |
| 958 | struct module *owner = NULL; |
| 959 | struct gendisk *disk; |
| 960 | int ret = -ENXIO; |
| 961 | int part; |
| 962 | |
| 963 | file->f_mapping = bdev->bd_inode->i_mapping; |
| 964 | lock_kernel(); |
| 965 | disk = get_gendisk(bdev->bd_dev, &part); |
| 966 | if (!disk) { |
| 967 | unlock_kernel(); |
| 968 | bdput(bdev); |
| 969 | return ret; |
| 970 | } |
| 971 | owner = disk->fops->owner; |
| 972 | |
Ingo Molnar | 663d440 | 2006-07-03 00:25:33 -0700 | [diff] [blame] | 973 | mutex_lock_nested(&bdev->bd_mutex, subclass); |
| 974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | if (!bdev->bd_openers) { |
| 976 | bdev->bd_disk = disk; |
| 977 | bdev->bd_contains = bdev; |
| 978 | if (!part) { |
| 979 | struct backing_dev_info *bdi; |
| 980 | if (disk->fops->open) { |
| 981 | ret = disk->fops->open(bdev->bd_inode, file); |
| 982 | if (ret) |
| 983 | goto out_first; |
| 984 | } |
| 985 | if (!bdev->bd_openers) { |
| 986 | bd_set_size(bdev,(loff_t)get_capacity(disk)<<9); |
| 987 | bdi = blk_get_backing_dev_info(bdev); |
| 988 | if (bdi == NULL) |
| 989 | bdi = &default_backing_dev_info; |
| 990 | bdev->bd_inode->i_data.backing_dev_info = bdi; |
| 991 | } |
| 992 | if (bdev->bd_invalidated) |
| 993 | rescan_partitions(disk, bdev); |
| 994 | } else { |
| 995 | struct hd_struct *p; |
| 996 | struct block_device *whole; |
| 997 | whole = bdget_disk(disk, 0); |
| 998 | ret = -ENOMEM; |
| 999 | if (!whole) |
| 1000 | goto out_first; |
Ingo Molnar | 663d440 | 2006-07-03 00:25:33 -0700 | [diff] [blame] | 1001 | ret = blkdev_get_whole(whole, file->f_mode, file->f_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | if (ret) |
| 1003 | goto out_first; |
| 1004 | bdev->bd_contains = whole; |
Ingo Molnar | 663d440 | 2006-07-03 00:25:33 -0700 | [diff] [blame] | 1005 | mutex_lock_nested(&whole->bd_mutex, BD_MUTEX_WHOLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | whole->bd_part_count++; |
| 1007 | p = disk->part[part - 1]; |
| 1008 | bdev->bd_inode->i_data.backing_dev_info = |
| 1009 | whole->bd_inode->i_data.backing_dev_info; |
| 1010 | if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) { |
| 1011 | whole->bd_part_count--; |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1012 | mutex_unlock(&whole->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | ret = -ENXIO; |
| 1014 | goto out_first; |
| 1015 | } |
| 1016 | kobject_get(&p->kobj); |
| 1017 | bdev->bd_part = p; |
| 1018 | bd_set_size(bdev, (loff_t) p->nr_sects << 9); |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1019 | mutex_unlock(&whole->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | } |
| 1021 | } else { |
| 1022 | put_disk(disk); |
| 1023 | module_put(owner); |
| 1024 | if (bdev->bd_contains == bdev) { |
| 1025 | if (bdev->bd_disk->fops->open) { |
| 1026 | ret = bdev->bd_disk->fops->open(bdev->bd_inode, file); |
| 1027 | if (ret) |
| 1028 | goto out; |
| 1029 | } |
| 1030 | if (bdev->bd_invalidated) |
| 1031 | rescan_partitions(bdev->bd_disk, bdev); |
| 1032 | } else { |
Ingo Molnar | 663d440 | 2006-07-03 00:25:33 -0700 | [diff] [blame] | 1033 | mutex_lock_nested(&bdev->bd_contains->bd_mutex, |
Jason Baron | 87d7c8a | 2006-09-29 02:01:01 -0700 | [diff] [blame^] | 1034 | BD_MUTEX_WHOLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | bdev->bd_contains->bd_part_count++; |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1036 | mutex_unlock(&bdev->bd_contains->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | bdev->bd_openers++; |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1040 | mutex_unlock(&bdev->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | unlock_kernel(); |
| 1042 | return 0; |
| 1043 | |
| 1044 | out_first: |
| 1045 | bdev->bd_disk = NULL; |
| 1046 | bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info; |
| 1047 | if (bdev != bdev->bd_contains) |
Peter Zijlstra | 6946bd6 | 2006-08-27 01:23:31 -0700 | [diff] [blame] | 1048 | __blkdev_put(bdev->bd_contains, BD_MUTEX_WHOLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | bdev->bd_contains = NULL; |
| 1050 | put_disk(disk); |
| 1051 | module_put(owner); |
| 1052 | out: |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1053 | mutex_unlock(&bdev->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | unlock_kernel(); |
| 1055 | if (ret) |
| 1056 | bdput(bdev); |
| 1057 | return ret; |
| 1058 | } |
| 1059 | |
| 1060 | int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags) |
| 1061 | { |
| 1062 | /* |
| 1063 | * This crockload is due to bad choice of ->open() type. |
| 1064 | * It will go away. |
| 1065 | * For now, block device ->open() routine must _not_ |
| 1066 | * examine anything in 'inode' argument except ->i_rdev. |
| 1067 | */ |
| 1068 | struct file fake_file = {}; |
| 1069 | struct dentry fake_dentry = {}; |
| 1070 | fake_file.f_mode = mode; |
| 1071 | fake_file.f_flags = flags; |
| 1072 | fake_file.f_dentry = &fake_dentry; |
| 1073 | fake_dentry.d_inode = bdev->bd_inode; |
| 1074 | |
Ingo Molnar | 663d440 | 2006-07-03 00:25:33 -0700 | [diff] [blame] | 1075 | return do_open(bdev, &fake_file, BD_MUTEX_NORMAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | EXPORT_SYMBOL(blkdev_get); |
| 1079 | |
Ingo Molnar | 663d440 | 2006-07-03 00:25:33 -0700 | [diff] [blame] | 1080 | static int |
| 1081 | blkdev_get_whole(struct block_device *bdev, mode_t mode, unsigned flags) |
| 1082 | { |
| 1083 | /* |
| 1084 | * This crockload is due to bad choice of ->open() type. |
| 1085 | * It will go away. |
| 1086 | * For now, block device ->open() routine must _not_ |
| 1087 | * examine anything in 'inode' argument except ->i_rdev. |
| 1088 | */ |
| 1089 | struct file fake_file = {}; |
| 1090 | struct dentry fake_dentry = {}; |
| 1091 | fake_file.f_mode = mode; |
| 1092 | fake_file.f_flags = flags; |
| 1093 | fake_file.f_dentry = &fake_dentry; |
| 1094 | fake_dentry.d_inode = bdev->bd_inode; |
| 1095 | |
| 1096 | return do_open(bdev, &fake_file, BD_MUTEX_WHOLE); |
| 1097 | } |
| 1098 | |
| 1099 | static int |
| 1100 | blkdev_get_partition(struct block_device *bdev, mode_t mode, unsigned flags) |
| 1101 | { |
| 1102 | /* |
| 1103 | * This crockload is due to bad choice of ->open() type. |
| 1104 | * It will go away. |
| 1105 | * For now, block device ->open() routine must _not_ |
| 1106 | * examine anything in 'inode' argument except ->i_rdev. |
| 1107 | */ |
| 1108 | struct file fake_file = {}; |
| 1109 | struct dentry fake_dentry = {}; |
| 1110 | fake_file.f_mode = mode; |
| 1111 | fake_file.f_flags = flags; |
| 1112 | fake_file.f_dentry = &fake_dentry; |
| 1113 | fake_dentry.d_inode = bdev->bd_inode; |
| 1114 | |
| 1115 | return do_open(bdev, &fake_file, BD_MUTEX_PARTITION); |
| 1116 | } |
| 1117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | static int blkdev_open(struct inode * inode, struct file * filp) |
| 1119 | { |
| 1120 | struct block_device *bdev; |
| 1121 | int res; |
| 1122 | |
| 1123 | /* |
| 1124 | * Preserve backwards compatibility and allow large file access |
| 1125 | * even if userspace doesn't ask for it explicitly. Some mkfs |
| 1126 | * binary needs it. We might want to drop this workaround |
| 1127 | * during an unstable branch. |
| 1128 | */ |
| 1129 | filp->f_flags |= O_LARGEFILE; |
| 1130 | |
| 1131 | bdev = bd_acquire(inode); |
| 1132 | |
Ingo Molnar | 663d440 | 2006-07-03 00:25:33 -0700 | [diff] [blame] | 1133 | res = do_open(bdev, filp, BD_MUTEX_NORMAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | if (res) |
| 1135 | return res; |
| 1136 | |
| 1137 | if (!(filp->f_flags & O_EXCL) ) |
| 1138 | return 0; |
| 1139 | |
| 1140 | if (!(res = bd_claim(bdev, filp))) |
| 1141 | return 0; |
| 1142 | |
| 1143 | blkdev_put(bdev); |
| 1144 | return res; |
| 1145 | } |
| 1146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | static int blkdev_close(struct inode * inode, struct file * filp) |
| 1148 | { |
| 1149 | struct block_device *bdev = I_BDEV(filp->f_mapping->host); |
| 1150 | if (bdev->bd_holder == filp) |
| 1151 | bd_release(bdev); |
| 1152 | return blkdev_put(bdev); |
| 1153 | } |
| 1154 | |
| 1155 | static ssize_t blkdev_file_write(struct file *file, const char __user *buf, |
| 1156 | size_t count, loff_t *ppos) |
| 1157 | { |
| 1158 | struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count }; |
| 1159 | |
| 1160 | return generic_file_write_nolock(file, &local_iov, 1, ppos); |
| 1161 | } |
| 1162 | |
| 1163 | static ssize_t blkdev_file_aio_write(struct kiocb *iocb, const char __user *buf, |
| 1164 | size_t count, loff_t pos) |
| 1165 | { |
| 1166 | struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count }; |
| 1167 | |
| 1168 | return generic_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos); |
| 1169 | } |
| 1170 | |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 1171 | static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | { |
| 1173 | return blkdev_ioctl(file->f_mapping->host, file, cmd, arg); |
| 1174 | } |
| 1175 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 1176 | const struct address_space_operations def_blk_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | .readpage = blkdev_readpage, |
| 1178 | .writepage = blkdev_writepage, |
| 1179 | .sync_page = block_sync_page, |
| 1180 | .prepare_write = blkdev_prepare_write, |
| 1181 | .commit_write = blkdev_commit_write, |
| 1182 | .writepages = generic_writepages, |
| 1183 | .direct_IO = blkdev_direct_IO, |
| 1184 | }; |
| 1185 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1186 | const struct file_operations def_blk_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | .open = blkdev_open, |
| 1188 | .release = blkdev_close, |
| 1189 | .llseek = block_llseek, |
| 1190 | .read = generic_file_read, |
| 1191 | .write = blkdev_file_write, |
| 1192 | .aio_read = generic_file_aio_read, |
| 1193 | .aio_write = blkdev_file_aio_write, |
| 1194 | .mmap = generic_file_mmap, |
| 1195 | .fsync = block_fsync, |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 1196 | .unlocked_ioctl = block_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | #ifdef CONFIG_COMPAT |
| 1198 | .compat_ioctl = compat_blkdev_ioctl, |
| 1199 | #endif |
| 1200 | .readv = generic_file_readv, |
| 1201 | .writev = generic_file_write_nolock, |
| 1202 | .sendfile = generic_file_sendfile, |
Jens Axboe | 7f9c51f | 2006-05-01 19:59:32 +0200 | [diff] [blame] | 1203 | .splice_read = generic_file_splice_read, |
| 1204 | .splice_write = generic_file_splice_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | }; |
| 1206 | |
| 1207 | int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg) |
| 1208 | { |
| 1209 | int res; |
| 1210 | mm_segment_t old_fs = get_fs(); |
| 1211 | set_fs(KERNEL_DS); |
| 1212 | res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg); |
| 1213 | set_fs(old_fs); |
| 1214 | return res; |
| 1215 | } |
| 1216 | |
| 1217 | EXPORT_SYMBOL(ioctl_by_bdev); |
| 1218 | |
| 1219 | /** |
| 1220 | * lookup_bdev - lookup a struct block_device by name |
| 1221 | * |
| 1222 | * @path: special file representing the block device |
| 1223 | * |
| 1224 | * Get a reference to the blockdevice at @path in the current |
| 1225 | * namespace if possible and return it. Return ERR_PTR(error) |
| 1226 | * otherwise. |
| 1227 | */ |
| 1228 | struct block_device *lookup_bdev(const char *path) |
| 1229 | { |
| 1230 | struct block_device *bdev; |
| 1231 | struct inode *inode; |
| 1232 | struct nameidata nd; |
| 1233 | int error; |
| 1234 | |
| 1235 | if (!path || !*path) |
| 1236 | return ERR_PTR(-EINVAL); |
| 1237 | |
| 1238 | error = path_lookup(path, LOOKUP_FOLLOW, &nd); |
| 1239 | if (error) |
| 1240 | return ERR_PTR(error); |
| 1241 | |
| 1242 | inode = nd.dentry->d_inode; |
| 1243 | error = -ENOTBLK; |
| 1244 | if (!S_ISBLK(inode->i_mode)) |
| 1245 | goto fail; |
| 1246 | error = -EACCES; |
| 1247 | if (nd.mnt->mnt_flags & MNT_NODEV) |
| 1248 | goto fail; |
| 1249 | error = -ENOMEM; |
| 1250 | bdev = bd_acquire(inode); |
| 1251 | if (!bdev) |
| 1252 | goto fail; |
| 1253 | out: |
| 1254 | path_release(&nd); |
| 1255 | return bdev; |
| 1256 | fail: |
| 1257 | bdev = ERR_PTR(error); |
| 1258 | goto out; |
| 1259 | } |
| 1260 | |
| 1261 | /** |
| 1262 | * open_bdev_excl - open a block device by name and set it up for use |
| 1263 | * |
| 1264 | * @path: special file representing the block device |
| 1265 | * @flags: %MS_RDONLY for opening read-only |
| 1266 | * @holder: owner for exclusion |
| 1267 | * |
| 1268 | * Open the blockdevice described by the special file at @path, claim it |
| 1269 | * for the @holder. |
| 1270 | */ |
| 1271 | struct block_device *open_bdev_excl(const char *path, int flags, void *holder) |
| 1272 | { |
| 1273 | struct block_device *bdev; |
| 1274 | mode_t mode = FMODE_READ; |
| 1275 | int error = 0; |
| 1276 | |
| 1277 | bdev = lookup_bdev(path); |
| 1278 | if (IS_ERR(bdev)) |
| 1279 | return bdev; |
| 1280 | |
| 1281 | if (!(flags & MS_RDONLY)) |
| 1282 | mode |= FMODE_WRITE; |
| 1283 | error = blkdev_get(bdev, mode, 0); |
| 1284 | if (error) |
| 1285 | return ERR_PTR(error); |
| 1286 | error = -EACCES; |
| 1287 | if (!(flags & MS_RDONLY) && bdev_read_only(bdev)) |
| 1288 | goto blkdev_put; |
| 1289 | error = bd_claim(bdev, holder); |
| 1290 | if (error) |
| 1291 | goto blkdev_put; |
| 1292 | |
| 1293 | return bdev; |
| 1294 | |
| 1295 | blkdev_put: |
| 1296 | blkdev_put(bdev); |
| 1297 | return ERR_PTR(error); |
| 1298 | } |
| 1299 | |
| 1300 | EXPORT_SYMBOL(open_bdev_excl); |
| 1301 | |
| 1302 | /** |
| 1303 | * close_bdev_excl - release a blockdevice openen by open_bdev_excl() |
| 1304 | * |
| 1305 | * @bdev: blockdevice to close |
| 1306 | * |
| 1307 | * This is the counterpart to open_bdev_excl(). |
| 1308 | */ |
| 1309 | void close_bdev_excl(struct block_device *bdev) |
| 1310 | { |
| 1311 | bd_release(bdev); |
| 1312 | blkdev_put(bdev); |
| 1313 | } |
| 1314 | |
| 1315 | EXPORT_SYMBOL(close_bdev_excl); |