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