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