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 | 07f3f05c | 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); |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 288 | /* Initialize mutex for freeze. */ |
| 289 | mutex_init(&bdev->bd_fsfreeze_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static inline void __bd_forget(struct inode *inode) |
| 293 | { |
| 294 | list_del_init(&inode->i_devices); |
| 295 | inode->i_bdev = NULL; |
| 296 | inode->i_mapping = &inode->i_data; |
| 297 | } |
| 298 | |
| 299 | static void bdev_clear_inode(struct inode *inode) |
| 300 | { |
| 301 | struct block_device *bdev = &BDEV_I(inode)->bdev; |
| 302 | struct list_head *p; |
| 303 | spin_lock(&bdev_lock); |
| 304 | while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) { |
| 305 | __bd_forget(list_entry(p, struct inode, i_devices)); |
| 306 | } |
| 307 | list_del_init(&bdev->bd_list); |
| 308 | spin_unlock(&bdev_lock); |
| 309 | } |
| 310 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 311 | static const struct super_operations bdev_sops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | .statfs = simple_statfs, |
| 313 | .alloc_inode = bdev_alloc_inode, |
| 314 | .destroy_inode = bdev_destroy_inode, |
| 315 | .drop_inode = generic_delete_inode, |
| 316 | .clear_inode = bdev_clear_inode, |
| 317 | }; |
| 318 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 319 | static int bd_get_sb(struct file_system_type *fs_type, |
| 320 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 322 | return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static struct file_system_type bd_type = { |
| 326 | .name = "bdev", |
| 327 | .get_sb = bd_get_sb, |
| 328 | .kill_sb = kill_anon_super, |
| 329 | }; |
| 330 | |
Denis ChengRq | c2acf7b | 2008-12-01 14:34:56 -0800 | [diff] [blame] | 331 | struct super_block *blockdev_superblock __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
| 333 | void __init bdev_cache_init(void) |
| 334 | { |
| 335 | int err; |
Denis ChengRq | c2acf7b | 2008-12-01 14:34:56 -0800 | [diff] [blame] | 336 | struct vfsmount *bd_mnt; |
| 337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode), |
Paul Jackson | fffb60f | 2006-03-24 03:16:06 -0800 | [diff] [blame] | 339 | 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
| 340 | SLAB_MEM_SPREAD|SLAB_PANIC), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 341 | init_once); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | err = register_filesystem(&bd_type); |
| 343 | if (err) |
| 344 | panic("Cannot register bdev pseudo-fs"); |
| 345 | bd_mnt = kern_mount(&bd_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | if (IS_ERR(bd_mnt)) |
| 347 | panic("Cannot create bdev pseudo-fs"); |
| 348 | blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */ |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Most likely _very_ bad one - but then it's hardly critical for small |
| 353 | * /dev and can be fixed when somebody will need really large one. |
| 354 | * Keep in mind that it will be fed through icache hash function too. |
| 355 | */ |
| 356 | static inline unsigned long hash(dev_t dev) |
| 357 | { |
| 358 | return MAJOR(dev)+MINOR(dev); |
| 359 | } |
| 360 | |
| 361 | static int bdev_test(struct inode *inode, void *data) |
| 362 | { |
| 363 | return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data; |
| 364 | } |
| 365 | |
| 366 | static int bdev_set(struct inode *inode, void *data) |
| 367 | { |
| 368 | BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data; |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static LIST_HEAD(all_bdevs); |
| 373 | |
| 374 | struct block_device *bdget(dev_t dev) |
| 375 | { |
| 376 | struct block_device *bdev; |
| 377 | struct inode *inode; |
| 378 | |
Denis ChengRq | c2acf7b | 2008-12-01 14:34:56 -0800 | [diff] [blame] | 379 | inode = iget5_locked(blockdev_superblock, hash(dev), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | bdev_test, bdev_set, &dev); |
| 381 | |
| 382 | if (!inode) |
| 383 | return NULL; |
| 384 | |
| 385 | bdev = &BDEV_I(inode)->bdev; |
| 386 | |
| 387 | if (inode->i_state & I_NEW) { |
| 388 | bdev->bd_contains = NULL; |
| 389 | bdev->bd_inode = inode; |
| 390 | bdev->bd_block_size = (1 << inode->i_blkbits); |
| 391 | bdev->bd_part_count = 0; |
| 392 | bdev->bd_invalidated = 0; |
| 393 | inode->i_mode = S_IFBLK; |
| 394 | inode->i_rdev = dev; |
| 395 | inode->i_bdev = bdev; |
| 396 | inode->i_data.a_ops = &def_blk_aops; |
| 397 | mapping_set_gfp_mask(&inode->i_data, GFP_USER); |
| 398 | inode->i_data.backing_dev_info = &default_backing_dev_info; |
| 399 | spin_lock(&bdev_lock); |
| 400 | list_add(&bdev->bd_list, &all_bdevs); |
| 401 | spin_unlock(&bdev_lock); |
| 402 | unlock_new_inode(inode); |
| 403 | } |
| 404 | return bdev; |
| 405 | } |
| 406 | |
| 407 | EXPORT_SYMBOL(bdget); |
| 408 | |
| 409 | long nr_blockdev_pages(void) |
| 410 | { |
Matthias Kaehlcke | 203a293 | 2007-07-15 23:40:23 -0700 | [diff] [blame] | 411 | struct block_device *bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | long ret = 0; |
| 413 | spin_lock(&bdev_lock); |
Matthias Kaehlcke | 203a293 | 2007-07-15 23:40:23 -0700 | [diff] [blame] | 414 | list_for_each_entry(bdev, &all_bdevs, bd_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | ret += bdev->bd_inode->i_mapping->nrpages; |
| 416 | } |
| 417 | spin_unlock(&bdev_lock); |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | void bdput(struct block_device *bdev) |
| 422 | { |
| 423 | iput(bdev->bd_inode); |
| 424 | } |
| 425 | |
| 426 | EXPORT_SYMBOL(bdput); |
| 427 | |
| 428 | static struct block_device *bd_acquire(struct inode *inode) |
| 429 | { |
| 430 | struct block_device *bdev; |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | spin_lock(&bdev_lock); |
| 433 | bdev = inode->i_bdev; |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 434 | if (bdev) { |
| 435 | atomic_inc(&bdev->bd_inode->i_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | spin_unlock(&bdev_lock); |
| 437 | return bdev; |
| 438 | } |
| 439 | spin_unlock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | bdev = bdget(inode->i_rdev); |
| 442 | if (bdev) { |
| 443 | spin_lock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 444 | if (!inode->i_bdev) { |
| 445 | /* |
| 446 | * We take an additional bd_inode->i_count for inode, |
| 447 | * and it's released in clear_inode() of inode. |
| 448 | * So, we can access it via ->i_mapping always |
| 449 | * without igrab(). |
| 450 | */ |
| 451 | atomic_inc(&bdev->bd_inode->i_count); |
| 452 | inode->i_bdev = bdev; |
| 453 | inode->i_mapping = bdev->bd_inode->i_mapping; |
| 454 | list_add(&inode->i_devices, &bdev->bd_inodes); |
| 455 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | spin_unlock(&bdev_lock); |
| 457 | } |
| 458 | return bdev; |
| 459 | } |
| 460 | |
| 461 | /* Call when you free inode */ |
| 462 | |
| 463 | void bd_forget(struct inode *inode) |
| 464 | { |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 465 | struct block_device *bdev = NULL; |
| 466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | spin_lock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 468 | if (inode->i_bdev) { |
Denis ChengRq | c2acf7b | 2008-12-01 14:34:56 -0800 | [diff] [blame] | 469 | if (!sb_is_blkdev_sb(inode->i_sb)) |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 470 | bdev = inode->i_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | __bd_forget(inode); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 472 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | spin_unlock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 474 | |
| 475 | if (bdev) |
| 476 | iput(bdev->bd_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | int bd_claim(struct block_device *bdev, void *holder) |
| 480 | { |
| 481 | int res; |
| 482 | spin_lock(&bdev_lock); |
| 483 | |
| 484 | /* first decide result */ |
| 485 | if (bdev->bd_holder == holder) |
| 486 | res = 0; /* already a holder */ |
| 487 | else if (bdev->bd_holder != NULL) |
| 488 | res = -EBUSY; /* held by someone else */ |
| 489 | else if (bdev->bd_contains == bdev) |
| 490 | res = 0; /* is a whole device which isn't held */ |
| 491 | |
| 492 | else if (bdev->bd_contains->bd_holder == bd_claim) |
| 493 | res = 0; /* is a partition of a device that is being partitioned */ |
| 494 | else if (bdev->bd_contains->bd_holder != NULL) |
| 495 | res = -EBUSY; /* is a partition of a held device */ |
| 496 | else |
| 497 | res = 0; /* is a partition of an un-held device */ |
| 498 | |
| 499 | /* now impose change */ |
| 500 | if (res==0) { |
| 501 | /* note that for a whole device bd_holders |
| 502 | * will be incremented twice, and bd_holder will |
| 503 | * be set to bd_claim before being set to holder |
| 504 | */ |
| 505 | bdev->bd_contains->bd_holders ++; |
| 506 | bdev->bd_contains->bd_holder = bd_claim; |
| 507 | bdev->bd_holders++; |
| 508 | bdev->bd_holder = holder; |
| 509 | } |
| 510 | spin_unlock(&bdev_lock); |
| 511 | return res; |
| 512 | } |
| 513 | |
| 514 | EXPORT_SYMBOL(bd_claim); |
| 515 | |
| 516 | void bd_release(struct block_device *bdev) |
| 517 | { |
| 518 | spin_lock(&bdev_lock); |
| 519 | if (!--bdev->bd_contains->bd_holders) |
| 520 | bdev->bd_contains->bd_holder = NULL; |
| 521 | if (!--bdev->bd_holders) |
| 522 | bdev->bd_holder = NULL; |
| 523 | spin_unlock(&bdev_lock); |
| 524 | } |
| 525 | |
| 526 | EXPORT_SYMBOL(bd_release); |
| 527 | |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 528 | #ifdef CONFIG_SYSFS |
| 529 | /* |
| 530 | * Functions for bd_claim_by_kobject / bd_release_from_kobject |
| 531 | * |
| 532 | * If a kobject is passed to bd_claim_by_kobject() |
| 533 | * and the kobject has a parent directory, |
| 534 | * following symlinks are created: |
| 535 | * o from the kobject to the claimed bdev |
| 536 | * o from "holders" directory of the bdev to the parent of the kobject |
| 537 | * bd_release_from_kobject() removes these symlinks. |
| 538 | * |
| 539 | * Example: |
| 540 | * If /dev/dm-0 maps to /dev/sda, kobject corresponding to |
| 541 | * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then: |
| 542 | * /sys/block/dm-0/slaves/sda --> /sys/block/sda |
| 543 | * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0 |
| 544 | */ |
| 545 | |
Andrew Morton | 4d7dd8fd | 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 | 4d7dd8fd | 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 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 594 | bo->sdev = kobject_get(&part_to_dev(bdev->bd_part)->kobj); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 595 | if (!bo->sdev) |
| 596 | goto fail_put_hdev; |
| 597 | |
Tejun Heo | 4c46501 | 2008-08-25 19:56:11 +0900 | [diff] [blame] | 598 | bo->hdir = kobject_get(bdev->bd_part->holder_dir); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 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 | /** |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 643 | * find_bd_holder - find matching struct bd_holder from the block device |
| 644 | * |
| 645 | * @bdev: struct block device to be searched |
| 646 | * @bo: target struct bd_holder |
| 647 | * |
| 648 | * Returns matching entry with @bo in @bdev->bd_holder_list. |
| 649 | * If found, increment the reference count and return the pointer. |
| 650 | * If not found, returns NULL. |
| 651 | */ |
Andrew Morton | 36a561d | 2006-10-30 22:07:03 -0800 | [diff] [blame] | 652 | static struct bd_holder *find_bd_holder(struct block_device *bdev, |
| 653 | struct bd_holder *bo) |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 654 | { |
| 655 | struct bd_holder *tmp; |
| 656 | |
| 657 | list_for_each_entry(tmp, &bdev->bd_holder_list, list) |
| 658 | if (tmp->sdir == bo->sdir) { |
| 659 | tmp->count++; |
| 660 | return tmp; |
| 661 | } |
| 662 | |
| 663 | return NULL; |
| 664 | } |
| 665 | |
| 666 | /** |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 667 | * add_bd_holder - create sysfs symlinks for bd_claim() relationship |
| 668 | * |
| 669 | * @bdev: block device to be bd_claimed |
| 670 | * @bo: preallocated and initialized by alloc_bd_holder() |
| 671 | * |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 672 | * Add @bo to @bdev->bd_holder_list, create symlinks. |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 673 | * |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 674 | * Returns 0 if symlinks are created. |
| 675 | * Returns -ve if something fails. |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 676 | */ |
| 677 | static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo) |
| 678 | { |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 679 | int err; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 680 | |
| 681 | if (!bo) |
Andrew Morton | 4d7dd8fd | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 682 | return -EINVAL; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 683 | |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 684 | if (!bd_holder_grab_dirs(bdev, bo)) |
Andrew Morton | 4d7dd8fd | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 685 | return -EBUSY; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 686 | |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 687 | err = add_symlink(bo->sdir, bo->sdev); |
| 688 | if (err) |
| 689 | return err; |
| 690 | |
| 691 | err = add_symlink(bo->hdir, bo->hdev); |
| 692 | if (err) { |
| 693 | del_symlink(bo->sdir, bo->sdev); |
| 694 | return err; |
Andrew Morton | 4d7dd8fd | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 695 | } |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 696 | |
| 697 | list_add_tail(&bo->list, &bdev->bd_holder_list); |
| 698 | return 0; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | /** |
| 702 | * del_bd_holder - delete sysfs symlinks for bd_claim() relationship |
| 703 | * |
| 704 | * @bdev: block device to be bd_claimed |
| 705 | * @kobj: holder's kobject |
| 706 | * |
| 707 | * If there is matching entry with @kobj in @bdev->bd_holder_list |
| 708 | * and no other bd_claim() from the same kobject, |
| 709 | * remove the struct bd_holder from the list, delete symlinks for it. |
| 710 | * |
| 711 | * Returns a pointer to the struct bd_holder when it's removed from the list |
| 712 | * and ready to be freed. |
| 713 | * Returns NULL if matching claim isn't found or there is other bd_claim() |
| 714 | * by the same kobject. |
| 715 | */ |
| 716 | static struct bd_holder *del_bd_holder(struct block_device *bdev, |
| 717 | struct kobject *kobj) |
| 718 | { |
| 719 | struct bd_holder *bo; |
| 720 | |
| 721 | list_for_each_entry(bo, &bdev->bd_holder_list, list) { |
| 722 | if (bo->sdir == kobj) { |
| 723 | bo->count--; |
| 724 | BUG_ON(bo->count < 0); |
| 725 | if (!bo->count) { |
| 726 | list_del(&bo->list); |
| 727 | del_symlink(bo->sdir, bo->sdev); |
| 728 | del_symlink(bo->hdir, bo->hdev); |
| 729 | bd_holder_release_dirs(bo); |
| 730 | return bo; |
| 731 | } |
| 732 | break; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | return NULL; |
| 737 | } |
| 738 | |
| 739 | /** |
| 740 | * bd_claim_by_kobject - bd_claim() with additional kobject signature |
| 741 | * |
| 742 | * @bdev: block device to be claimed |
| 743 | * @holder: holder's signature |
| 744 | * @kobj: holder's kobject |
| 745 | * |
| 746 | * Do bd_claim() and if it succeeds, create sysfs symlinks between |
| 747 | * the bdev and the holder's kobject. |
| 748 | * Use bd_release_from_kobject() when relesing the claimed bdev. |
| 749 | * |
| 750 | * Returns 0 on success. (same as bd_claim()) |
| 751 | * Returns errno on failure. |
| 752 | */ |
| 753 | static int bd_claim_by_kobject(struct block_device *bdev, void *holder, |
| 754 | struct kobject *kobj) |
| 755 | { |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 756 | int err; |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 757 | struct bd_holder *bo, *found; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 758 | |
| 759 | if (!kobj) |
| 760 | return -EINVAL; |
| 761 | |
| 762 | bo = alloc_bd_holder(kobj); |
| 763 | if (!bo) |
| 764 | return -ENOMEM; |
| 765 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 766 | mutex_lock(&bdev->bd_mutex); |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 767 | |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 768 | err = bd_claim(bdev, holder); |
| 769 | if (err) |
Andrew Morton | 4210df2 | 2007-07-15 23:41:28 -0700 | [diff] [blame] | 770 | goto fail; |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 771 | |
| 772 | found = find_bd_holder(bdev, bo); |
| 773 | if (found) |
Andrew Morton | 4210df2 | 2007-07-15 23:41:28 -0700 | [diff] [blame] | 774 | goto fail; |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 775 | |
| 776 | err = add_bd_holder(bdev, bo); |
| 777 | if (err) |
| 778 | bd_release(bdev); |
Andrew Morton | 4210df2 | 2007-07-15 23:41:28 -0700 | [diff] [blame] | 779 | else |
| 780 | bo = NULL; |
| 781 | fail: |
Jun'ichi Nomura | b4cf1b7 | 2006-03-27 01:18:00 -0800 | [diff] [blame] | 782 | mutex_unlock(&bdev->bd_mutex); |
Andrew Morton | 4210df2 | 2007-07-15 23:41:28 -0700 | [diff] [blame] | 783 | free_bd_holder(bo); |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 784 | return err; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /** |
| 788 | * bd_release_from_kobject - bd_release() with additional kobject signature |
| 789 | * |
| 790 | * @bdev: block device to be released |
| 791 | * @kobj: holder's kobject |
| 792 | * |
| 793 | * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject(). |
| 794 | */ |
| 795 | static void bd_release_from_kobject(struct block_device *bdev, |
| 796 | struct kobject *kobj) |
| 797 | { |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 798 | if (!kobj) |
| 799 | return; |
| 800 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 801 | mutex_lock(&bdev->bd_mutex); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 802 | bd_release(bdev); |
Andrew Morton | 4210df2 | 2007-07-15 23:41:28 -0700 | [diff] [blame] | 803 | free_bd_holder(del_bd_holder(bdev, kobj)); |
Jun'ichi Nomura | b4cf1b7 | 2006-03-27 01:18:00 -0800 | [diff] [blame] | 804 | mutex_unlock(&bdev->bd_mutex); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | /** |
| 808 | * bd_claim_by_disk - wrapper function for bd_claim_by_kobject() |
| 809 | * |
| 810 | * @bdev: block device to be claimed |
| 811 | * @holder: holder's signature |
| 812 | * @disk: holder's gendisk |
| 813 | * |
| 814 | * Call bd_claim_by_kobject() with getting @disk->slave_dir. |
| 815 | */ |
| 816 | int bd_claim_by_disk(struct block_device *bdev, void *holder, |
| 817 | struct gendisk *disk) |
| 818 | { |
| 819 | return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir)); |
| 820 | } |
| 821 | EXPORT_SYMBOL_GPL(bd_claim_by_disk); |
| 822 | |
| 823 | /** |
| 824 | * bd_release_from_disk - wrapper function for bd_release_from_kobject() |
| 825 | * |
| 826 | * @bdev: block device to be claimed |
| 827 | * @disk: holder's gendisk |
| 828 | * |
| 829 | * Call bd_release_from_kobject() and put @disk->slave_dir. |
| 830 | */ |
| 831 | void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk) |
| 832 | { |
| 833 | bd_release_from_kobject(bdev, disk->slave_dir); |
| 834 | kobject_put(disk->slave_dir); |
| 835 | } |
| 836 | EXPORT_SYMBOL_GPL(bd_release_from_disk); |
| 837 | #endif |
| 838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | /* |
| 840 | * Tries to open block device by device number. Use it ONLY if you |
| 841 | * really do not have anything better - i.e. when you are behind a |
| 842 | * truly sucky interface and all you are given is a device number. _Never_ |
| 843 | * to be used for internal purposes. If you ever need it - reconsider |
| 844 | * your API. |
| 845 | */ |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 846 | struct block_device *open_by_devnum(dev_t dev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | { |
| 848 | struct block_device *bdev = bdget(dev); |
| 849 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | if (bdev) |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 851 | err = blkdev_get(bdev, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | return err ? ERR_PTR(err) : bdev; |
| 853 | } |
| 854 | |
| 855 | EXPORT_SYMBOL(open_by_devnum); |
| 856 | |
Andrew Patterson | 0c002c2 | 2008-09-04 14:27:20 -0600 | [diff] [blame] | 857 | /** |
Andrew Patterson | 56ade44 | 2008-09-04 14:27:40 -0600 | [diff] [blame] | 858 | * flush_disk - invalidates all buffer-cache entries on a disk |
| 859 | * |
| 860 | * @bdev: struct block device to be flushed |
| 861 | * |
| 862 | * Invalidates all buffer-cache entries on a disk. It should be called |
| 863 | * when a disk has been changed -- either by a media change or online |
| 864 | * resize. |
| 865 | */ |
| 866 | static void flush_disk(struct block_device *bdev) |
| 867 | { |
| 868 | if (__invalidate_device(bdev)) { |
| 869 | char name[BDEVNAME_SIZE] = ""; |
| 870 | |
| 871 | if (bdev->bd_disk) |
| 872 | disk_name(bdev->bd_disk, 0, name); |
| 873 | printk(KERN_WARNING "VFS: busy inodes on changed media or " |
| 874 | "resized disk %s\n", name); |
| 875 | } |
| 876 | |
| 877 | if (!bdev->bd_disk) |
| 878 | return; |
| 879 | if (disk_partitionable(bdev->bd_disk)) |
| 880 | bdev->bd_invalidated = 1; |
| 881 | } |
| 882 | |
| 883 | /** |
Randy Dunlap | 57d1b53 | 2008-10-09 10:42:38 +0200 | [diff] [blame] | 884 | * 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] | 885 | * @disk: struct gendisk to check |
| 886 | * @bdev: struct bdev to adjust. |
| 887 | * |
| 888 | * This routine checks to see if the bdev size does not match the disk size |
| 889 | * and adjusts it if it differs. |
| 890 | */ |
| 891 | void check_disk_size_change(struct gendisk *disk, struct block_device *bdev) |
| 892 | { |
| 893 | loff_t disk_size, bdev_size; |
| 894 | |
| 895 | disk_size = (loff_t)get_capacity(disk) << 9; |
| 896 | bdev_size = i_size_read(bdev->bd_inode); |
| 897 | if (disk_size != bdev_size) { |
| 898 | char name[BDEVNAME_SIZE]; |
| 899 | |
| 900 | disk_name(disk, 0, name); |
| 901 | printk(KERN_INFO |
| 902 | "%s: detected capacity change from %lld to %lld\n", |
| 903 | name, bdev_size, disk_size); |
| 904 | i_size_write(bdev->bd_inode, disk_size); |
Andrew Patterson | 608aeef | 2008-09-04 14:27:45 -0600 | [diff] [blame] | 905 | flush_disk(bdev); |
Andrew Patterson | c3279d1 | 2008-09-04 14:27:25 -0600 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | EXPORT_SYMBOL(check_disk_size_change); |
| 909 | |
| 910 | /** |
Randy Dunlap | 57d1b53 | 2008-10-09 10:42:38 +0200 | [diff] [blame] | 911 | * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back |
Andrew Patterson | 0c002c2 | 2008-09-04 14:27:20 -0600 | [diff] [blame] | 912 | * @disk: struct gendisk to be revalidated |
| 913 | * |
| 914 | * This routine is a wrapper for lower-level driver's revalidate_disk |
| 915 | * call-backs. It is used to do common pre and post operations needed |
| 916 | * for all revalidate_disk operations. |
| 917 | */ |
| 918 | int revalidate_disk(struct gendisk *disk) |
| 919 | { |
Andrew Patterson | c3279d1 | 2008-09-04 14:27:25 -0600 | [diff] [blame] | 920 | struct block_device *bdev; |
Andrew Patterson | 0c002c2 | 2008-09-04 14:27:20 -0600 | [diff] [blame] | 921 | int ret = 0; |
| 922 | |
| 923 | if (disk->fops->revalidate_disk) |
| 924 | ret = disk->fops->revalidate_disk(disk); |
| 925 | |
Andrew Patterson | c3279d1 | 2008-09-04 14:27:25 -0600 | [diff] [blame] | 926 | bdev = bdget_disk(disk, 0); |
| 927 | if (!bdev) |
| 928 | return ret; |
| 929 | |
| 930 | mutex_lock(&bdev->bd_mutex); |
| 931 | check_disk_size_change(disk, bdev); |
| 932 | mutex_unlock(&bdev->bd_mutex); |
| 933 | bdput(bdev); |
Andrew Patterson | 0c002c2 | 2008-09-04 14:27:20 -0600 | [diff] [blame] | 934 | return ret; |
| 935 | } |
| 936 | EXPORT_SYMBOL(revalidate_disk); |
| 937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | /* |
| 939 | * This routine checks whether a removable media has been changed, |
| 940 | * and invalidates all buffer-cache-entries in that case. This |
| 941 | * is a relatively slow routine, so we have to try to minimize using |
| 942 | * it. Thus it is called only upon a 'mount' or 'open'. This |
| 943 | * is the best way of combining speed and utility, I think. |
| 944 | * People changing diskettes in the middle of an operation deserve |
| 945 | * to lose :-) |
| 946 | */ |
| 947 | int check_disk_change(struct block_device *bdev) |
| 948 | { |
| 949 | struct gendisk *disk = bdev->bd_disk; |
| 950 | struct block_device_operations * bdops = disk->fops; |
| 951 | |
| 952 | if (!bdops->media_changed) |
| 953 | return 0; |
| 954 | if (!bdops->media_changed(bdev->bd_disk)) |
| 955 | return 0; |
| 956 | |
Andrew Patterson | 56ade44 | 2008-09-04 14:27:40 -0600 | [diff] [blame] | 957 | flush_disk(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | if (bdops->revalidate_disk) |
| 959 | bdops->revalidate_disk(bdev->bd_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | return 1; |
| 961 | } |
| 962 | |
| 963 | EXPORT_SYMBOL(check_disk_change); |
| 964 | |
| 965 | void bd_set_size(struct block_device *bdev, loff_t size) |
| 966 | { |
| 967 | unsigned bsize = bdev_hardsect_size(bdev); |
| 968 | |
| 969 | bdev->bd_inode->i_size = size; |
| 970 | while (bsize < PAGE_CACHE_SIZE) { |
| 971 | if (size & bsize) |
| 972 | break; |
| 973 | bsize <<= 1; |
| 974 | } |
| 975 | bdev->bd_block_size = bsize; |
| 976 | bdev->bd_inode->i_blkbits = blksize_bits(bsize); |
| 977 | } |
| 978 | EXPORT_SYMBOL(bd_set_size); |
| 979 | |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 980 | static int __blkdev_put(struct block_device *bdev, fmode_t mode, 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 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 989 | static int __blkdev_get(struct block_device *bdev, fmode_t mode, 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; |
Pavel Emelyanov | 7db9cfd | 2008-06-05 22:46:27 -0700 | [diff] [blame] | 992 | int ret; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 993 | int partno; |
Al Viro | fe6e9c1 | 2008-06-23 08:30:55 -0400 | [diff] [blame] | 994 | int perm = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 996 | if (mode & FMODE_READ) |
Al Viro | fe6e9c1 | 2008-06-23 08:30:55 -0400 | [diff] [blame] | 997 | perm |= MAY_READ; |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 998 | if (mode & FMODE_WRITE) |
Al Viro | fe6e9c1 | 2008-06-23 08:30:55 -0400 | [diff] [blame] | 999 | perm |= MAY_WRITE; |
| 1000 | /* |
| 1001 | * hooks: /n/, see "layering violations". |
| 1002 | */ |
| 1003 | ret = devcgroup_inode_permission(bdev->bd_inode, perm); |
Al Viro | 8266602 | 2008-08-01 05:32:04 -0400 | [diff] [blame] | 1004 | if (ret != 0) { |
| 1005 | bdput(bdev); |
Pavel Emelyanov | 7db9cfd | 2008-06-05 22:46:27 -0700 | [diff] [blame] | 1006 | return ret; |
Al Viro | 8266602 | 2008-08-01 05:32:04 -0400 | [diff] [blame] | 1007 | } |
Pavel Emelyanov | 7db9cfd | 2008-06-05 22:46:27 -0700 | [diff] [blame] | 1008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | lock_kernel(); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 1010 | restart: |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1011 | |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1012 | ret = -ENXIO; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 1013 | disk = get_gendisk(bdev->bd_dev, &partno); |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1014 | if (!disk) |
| 1015 | goto out_unlock_kernel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | |
NeilBrown | 6796bf5 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1017 | mutex_lock_nested(&bdev->bd_mutex, for_part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | if (!bdev->bd_openers) { |
| 1019 | bdev->bd_disk = disk; |
| 1020 | bdev->bd_contains = bdev; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 1021 | if (!partno) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | struct backing_dev_info *bdi; |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1023 | |
| 1024 | ret = -ENXIO; |
| 1025 | bdev->bd_part = disk_get_part(disk, partno); |
| 1026 | if (!bdev->bd_part) |
| 1027 | goto out_clear; |
| 1028 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | if (disk->fops->open) { |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1030 | ret = disk->fops->open(bdev, mode); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 1031 | if (ret == -ERESTARTSYS) { |
| 1032 | /* Lost a race with 'disk' being |
| 1033 | * deleted, try again. |
| 1034 | * See md.c |
| 1035 | */ |
| 1036 | disk_put_part(bdev->bd_part); |
| 1037 | bdev->bd_part = NULL; |
| 1038 | module_put(disk->fops->owner); |
| 1039 | put_disk(disk); |
| 1040 | bdev->bd_disk = NULL; |
| 1041 | mutex_unlock(&bdev->bd_mutex); |
| 1042 | goto restart; |
| 1043 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | if (ret) |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1045 | goto out_clear; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | } |
| 1047 | if (!bdev->bd_openers) { |
| 1048 | bd_set_size(bdev,(loff_t)get_capacity(disk)<<9); |
| 1049 | bdi = blk_get_backing_dev_info(bdev); |
| 1050 | if (bdi == NULL) |
| 1051 | bdi = &default_backing_dev_info; |
| 1052 | bdev->bd_inode->i_data.backing_dev_info = bdi; |
| 1053 | } |
| 1054 | if (bdev->bd_invalidated) |
| 1055 | rescan_partitions(disk, bdev); |
| 1056 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | struct block_device *whole; |
| 1058 | whole = bdget_disk(disk, 0); |
| 1059 | ret = -ENOMEM; |
| 1060 | if (!whole) |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1061 | goto out_clear; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1062 | BUG_ON(for_part); |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1063 | ret = __blkdev_get(whole, mode, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | if (ret) |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1065 | goto out_clear; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | bdev->bd_contains = whole; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | bdev->bd_inode->i_data.backing_dev_info = |
| 1068 | whole->bd_inode->i_data.backing_dev_info; |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1069 | bdev->bd_part = disk_get_part(disk, partno); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 1070 | if (!(disk->flags & GENHD_FL_UP) || |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1071 | !bdev->bd_part || !bdev->bd_part->nr_sects) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | ret = -ENXIO; |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1073 | goto out_clear; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | } |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1075 | bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | } |
| 1077 | } else { |
| 1078 | put_disk(disk); |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1079 | module_put(disk->fops->owner); |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1080 | disk = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | if (bdev->bd_contains == bdev) { |
| 1082 | if (bdev->bd_disk->fops->open) { |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1083 | ret = bdev->bd_disk->fops->open(bdev, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | if (ret) |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1085 | goto out_unlock_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | } |
| 1087 | if (bdev->bd_invalidated) |
| 1088 | rescan_partitions(bdev->bd_disk, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | bdev->bd_openers++; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1092 | if (for_part) |
| 1093 | bdev->bd_part_count++; |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1094 | mutex_unlock(&bdev->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | unlock_kernel(); |
| 1096 | return 0; |
| 1097 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1098 | out_clear: |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1099 | disk_put_part(bdev->bd_part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | bdev->bd_disk = NULL; |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1101 | bdev->bd_part = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info; |
| 1103 | if (bdev != bdev->bd_contains) |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1104 | __blkdev_put(bdev->bd_contains, mode, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | bdev->bd_contains = NULL; |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1106 | out_unlock_bdev: |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1107 | mutex_unlock(&bdev->bd_mutex); |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1108 | out_unlock_kernel: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | unlock_kernel(); |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1110 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1111 | if (disk) |
| 1112 | module_put(disk->fops->owner); |
| 1113 | put_disk(disk); |
| 1114 | bdput(bdev); |
| 1115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | return ret; |
| 1117 | } |
| 1118 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1119 | int blkdev_get(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | { |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1121 | return __blkdev_get(bdev, mode, 0); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1122 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | EXPORT_SYMBOL(blkdev_get); |
| 1124 | |
| 1125 | static int blkdev_open(struct inode * inode, struct file * filp) |
| 1126 | { |
| 1127 | struct block_device *bdev; |
| 1128 | int res; |
| 1129 | |
| 1130 | /* |
| 1131 | * Preserve backwards compatibility and allow large file access |
| 1132 | * even if userspace doesn't ask for it explicitly. Some mkfs |
| 1133 | * binary needs it. We might want to drop this workaround |
| 1134 | * during an unstable branch. |
| 1135 | */ |
| 1136 | filp->f_flags |= O_LARGEFILE; |
| 1137 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1138 | if (filp->f_flags & O_NDELAY) |
| 1139 | filp->f_mode |= FMODE_NDELAY; |
| 1140 | if (filp->f_flags & O_EXCL) |
| 1141 | filp->f_mode |= FMODE_EXCL; |
| 1142 | if ((filp->f_flags & O_ACCMODE) == 3) |
| 1143 | filp->f_mode |= FMODE_WRITE_IOCTL; |
| 1144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1149 | filp->f_mapping = bdev->bd_inode->i_mapping; |
| 1150 | |
| 1151 | res = blkdev_get(bdev, filp->f_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | if (res) |
| 1153 | return res; |
| 1154 | |
Christoph Hellwig | ebbefc0 | 2008-11-05 14:54:41 +0100 | [diff] [blame] | 1155 | if (filp->f_mode & FMODE_EXCL) { |
| 1156 | res = bd_claim(bdev, filp); |
| 1157 | if (res) |
| 1158 | goto out_blkdev_put; |
| 1159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | |
Christoph Hellwig | ebbefc0 | 2008-11-05 14:54:41 +0100 | [diff] [blame] | 1161 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | |
Christoph Hellwig | ebbefc0 | 2008-11-05 14:54:41 +0100 | [diff] [blame] | 1163 | out_blkdev_put: |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1164 | blkdev_put(bdev, filp->f_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | return res; |
| 1166 | } |
| 1167 | |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1168 | static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part) |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1169 | { |
| 1170 | int ret = 0; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1171 | struct gendisk *disk = bdev->bd_disk; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1172 | struct block_device *victim = NULL; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1173 | |
NeilBrown | 6796bf5 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1174 | mutex_lock_nested(&bdev->bd_mutex, for_part); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1175 | lock_kernel(); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1176 | if (for_part) |
| 1177 | bdev->bd_part_count--; |
| 1178 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1179 | if (!--bdev->bd_openers) { |
| 1180 | sync_blockdev(bdev); |
| 1181 | kill_bdev(bdev); |
| 1182 | } |
| 1183 | if (bdev->bd_contains == bdev) { |
| 1184 | if (disk->fops->release) |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1185 | ret = disk->fops->release(disk, mode); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1186 | } |
| 1187 | if (!bdev->bd_openers) { |
| 1188 | struct module *owner = disk->fops->owner; |
| 1189 | |
| 1190 | put_disk(disk); |
| 1191 | module_put(owner); |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1192 | disk_put_part(bdev->bd_part); |
| 1193 | bdev->bd_part = NULL; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1194 | bdev->bd_disk = NULL; |
| 1195 | bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1196 | if (bdev != bdev->bd_contains) |
| 1197 | victim = bdev->bd_contains; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1198 | bdev->bd_contains = NULL; |
| 1199 | } |
| 1200 | unlock_kernel(); |
| 1201 | mutex_unlock(&bdev->bd_mutex); |
| 1202 | bdput(bdev); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1203 | if (victim) |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1204 | __blkdev_put(victim, mode, 1); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1205 | return ret; |
| 1206 | } |
| 1207 | |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1208 | int blkdev_put(struct block_device *bdev, fmode_t mode) |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1209 | { |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1210 | return __blkdev_put(bdev, mode, 0); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1211 | } |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1212 | EXPORT_SYMBOL(blkdev_put); |
| 1213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | static int blkdev_close(struct inode * inode, struct file * filp) |
| 1215 | { |
| 1216 | struct block_device *bdev = I_BDEV(filp->f_mapping->host); |
| 1217 | if (bdev->bd_holder == filp) |
| 1218 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1219 | return blkdev_put(bdev, filp->f_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 1222 | static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | { |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1224 | struct block_device *bdev = I_BDEV(file->f_mapping->host); |
| 1225 | fmode_t mode = file->f_mode; |
Christoph Hellwig | fd4ce1a | 2008-11-05 14:58:42 +0100 | [diff] [blame] | 1226 | |
| 1227 | /* |
| 1228 | * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have |
| 1229 | * to updated it before every ioctl. |
| 1230 | */ |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1231 | if (file->f_flags & O_NDELAY) |
Christoph Hellwig | fd4ce1a | 2008-11-05 14:58:42 +0100 | [diff] [blame] | 1232 | mode |= FMODE_NDELAY; |
| 1233 | else |
| 1234 | mode &= ~FMODE_NDELAY; |
| 1235 | |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1236 | return blkdev_ioctl(bdev, mode, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Theodore Ts'o | 87d8fe1 | 2009-01-03 09:47:09 -0500 | [diff] [blame] | 1239 | /* |
| 1240 | * Try to release a page associated with block device when the system |
| 1241 | * is under memory pressure. |
| 1242 | */ |
| 1243 | static int blkdev_releasepage(struct page *page, gfp_t wait) |
| 1244 | { |
| 1245 | struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super; |
| 1246 | |
| 1247 | if (super && super->s_op->bdev_try_to_free_page) |
| 1248 | return super->s_op->bdev_try_to_free_page(super, page, wait); |
| 1249 | |
| 1250 | return try_to_free_buffers(page); |
| 1251 | } |
| 1252 | |
Adrian Bunk | 4c54ac6 | 2008-02-18 13:48:31 +0100 | [diff] [blame] | 1253 | static const struct address_space_operations def_blk_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | .readpage = blkdev_readpage, |
| 1255 | .writepage = blkdev_writepage, |
| 1256 | .sync_page = block_sync_page, |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 1257 | .write_begin = blkdev_write_begin, |
| 1258 | .write_end = blkdev_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | .writepages = generic_writepages, |
Theodore Ts'o | 87d8fe1 | 2009-01-03 09:47:09 -0500 | [diff] [blame] | 1260 | .releasepage = blkdev_releasepage, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | .direct_IO = blkdev_direct_IO, |
| 1262 | }; |
| 1263 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1264 | const struct file_operations def_blk_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | .open = blkdev_open, |
| 1266 | .release = blkdev_close, |
| 1267 | .llseek = block_llseek, |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 1268 | .read = do_sync_read, |
| 1269 | .write = do_sync_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | .aio_read = generic_file_aio_read, |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 1271 | .aio_write = generic_file_aio_write_nolock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | .mmap = generic_file_mmap, |
| 1273 | .fsync = block_fsync, |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 1274 | .unlocked_ioctl = block_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | #ifdef CONFIG_COMPAT |
| 1276 | .compat_ioctl = compat_blkdev_ioctl, |
| 1277 | #endif |
Jens Axboe | 7f9c51f | 2006-05-01 19:59:32 +0200 | [diff] [blame] | 1278 | .splice_read = generic_file_splice_read, |
| 1279 | .splice_write = generic_file_splice_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | }; |
| 1281 | |
| 1282 | int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg) |
| 1283 | { |
| 1284 | int res; |
| 1285 | mm_segment_t old_fs = get_fs(); |
| 1286 | set_fs(KERNEL_DS); |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1287 | res = blkdev_ioctl(bdev, 0, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | set_fs(old_fs); |
| 1289 | return res; |
| 1290 | } |
| 1291 | |
| 1292 | EXPORT_SYMBOL(ioctl_by_bdev); |
| 1293 | |
| 1294 | /** |
| 1295 | * lookup_bdev - lookup a struct block_device by name |
Randy Dunlap | 94e2959 | 2009-01-06 14:41:15 -0800 | [diff] [blame] | 1296 | * @pathname: special file representing the block device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | * |
Randy Dunlap | 57d1b53 | 2008-10-09 10:42:38 +0200 | [diff] [blame] | 1298 | * Get a reference to the blockdevice at @pathname in the current |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | * namespace if possible and return it. Return ERR_PTR(error) |
| 1300 | * otherwise. |
| 1301 | */ |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1302 | struct block_device *lookup_bdev(const char *pathname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | { |
| 1304 | struct block_device *bdev; |
| 1305 | struct inode *inode; |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1306 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | int error; |
| 1308 | |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1309 | if (!pathname || !*pathname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | return ERR_PTR(-EINVAL); |
| 1311 | |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1312 | error = kern_path(pathname, LOOKUP_FOLLOW, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | if (error) |
| 1314 | return ERR_PTR(error); |
| 1315 | |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1316 | inode = path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | error = -ENOTBLK; |
| 1318 | if (!S_ISBLK(inode->i_mode)) |
| 1319 | goto fail; |
| 1320 | error = -EACCES; |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1321 | if (path.mnt->mnt_flags & MNT_NODEV) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | goto fail; |
| 1323 | error = -ENOMEM; |
| 1324 | bdev = bd_acquire(inode); |
| 1325 | if (!bdev) |
| 1326 | goto fail; |
| 1327 | out: |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1328 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | return bdev; |
| 1330 | fail: |
| 1331 | bdev = ERR_PTR(error); |
| 1332 | goto out; |
| 1333 | } |
Al Viro | d5686b4 | 2008-08-01 05:00:11 -0400 | [diff] [blame] | 1334 | EXPORT_SYMBOL(lookup_bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | |
| 1336 | /** |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1337 | * open_bdev_exclusive - open a block device by name and set it up for use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | * |
| 1339 | * @path: special file representing the block device |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1340 | * @mode: FMODE_... combination to pass be used |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | * @holder: owner for exclusion |
| 1342 | * |
| 1343 | * Open the blockdevice described by the special file at @path, claim it |
| 1344 | * for the @holder. |
| 1345 | */ |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1346 | struct block_device *open_bdev_exclusive(const char *path, fmode_t mode, void *holder) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | { |
| 1348 | struct block_device *bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | int error = 0; |
| 1350 | |
| 1351 | bdev = lookup_bdev(path); |
| 1352 | if (IS_ERR(bdev)) |
| 1353 | return bdev; |
| 1354 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1355 | error = blkdev_get(bdev, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | if (error) |
| 1357 | return ERR_PTR(error); |
| 1358 | error = -EACCES; |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1359 | if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | goto blkdev_put; |
| 1361 | error = bd_claim(bdev, holder); |
| 1362 | if (error) |
| 1363 | goto blkdev_put; |
| 1364 | |
| 1365 | return bdev; |
| 1366 | |
| 1367 | blkdev_put: |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1368 | blkdev_put(bdev, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | return ERR_PTR(error); |
| 1370 | } |
| 1371 | |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1372 | EXPORT_SYMBOL(open_bdev_exclusive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | |
| 1374 | /** |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1375 | * close_bdev_exclusive - close a blockdevice opened by open_bdev_exclusive() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | * |
| 1377 | * @bdev: blockdevice to close |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1378 | * @mode: mode, must match that used to open. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | * |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1380 | * This is the counterpart to open_bdev_exclusive(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | */ |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1382 | void close_bdev_exclusive(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | { |
| 1384 | bd_release(bdev); |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1385 | blkdev_put(bdev, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | } |
| 1387 | |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1388 | EXPORT_SYMBOL(close_bdev_exclusive); |
David Howells | b71e8a4 | 2006-08-29 19:06:11 +0100 | [diff] [blame] | 1389 | |
| 1390 | int __invalidate_device(struct block_device *bdev) |
| 1391 | { |
| 1392 | struct super_block *sb = get_super(bdev); |
| 1393 | int res = 0; |
| 1394 | |
| 1395 | if (sb) { |
| 1396 | /* |
| 1397 | * no need to lock the super, get_super holds the |
| 1398 | * read mutex so the filesystem cannot go away |
| 1399 | * under us (->put_super runs with the write lock |
| 1400 | * hold). |
| 1401 | */ |
| 1402 | shrink_dcache_sb(sb); |
| 1403 | res = invalidate_inodes(sb); |
| 1404 | drop_super(sb); |
| 1405 | } |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 1406 | invalidate_bdev(bdev); |
David Howells | b71e8a4 | 2006-08-29 19:06:11 +0100 | [diff] [blame] | 1407 | return res; |
| 1408 | } |
| 1409 | EXPORT_SYMBOL(__invalidate_device); |