Merge tag 'libnvdimm-for-4.7' of git://git.kernel.org/pub/scm/linux/kernel/git/nvdimm...
[deliverable/linux.git] / fs / block_dev.c
1 /*
2 * linux/fs/block_dev.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
6 */
7
8 #include <linux/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>
14 #include <linux/device_cgroup.h>
15 #include <linux/highmem.h>
16 #include <linux/blkdev.h>
17 #include <linux/backing-dev.h>
18 #include <linux/module.h>
19 #include <linux/blkpg.h>
20 #include <linux/magic.h>
21 #include <linux/buffer_head.h>
22 #include <linux/swap.h>
23 #include <linux/pagevec.h>
24 #include <linux/writeback.h>
25 #include <linux/mpage.h>
26 #include <linux/mount.h>
27 #include <linux/uio.h>
28 #include <linux/namei.h>
29 #include <linux/log2.h>
30 #include <linux/cleancache.h>
31 #include <linux/dax.h>
32 #include <linux/badblocks.h>
33 #include <asm/uaccess.h>
34 #include "internal.h"
35
36 struct bdev_inode {
37 struct block_device bdev;
38 struct inode vfs_inode;
39 };
40
41 static const struct address_space_operations def_blk_aops;
42
43 static inline struct bdev_inode *BDEV_I(struct inode *inode)
44 {
45 return container_of(inode, struct bdev_inode, vfs_inode);
46 }
47
48 struct block_device *I_BDEV(struct inode *inode)
49 {
50 return &BDEV_I(inode)->bdev;
51 }
52 EXPORT_SYMBOL(I_BDEV);
53
54 static void bdev_write_inode(struct block_device *bdev)
55 {
56 struct inode *inode = bdev->bd_inode;
57 int ret;
58
59 spin_lock(&inode->i_lock);
60 while (inode->i_state & I_DIRTY) {
61 spin_unlock(&inode->i_lock);
62 ret = write_inode_now(inode, true);
63 if (ret) {
64 char name[BDEVNAME_SIZE];
65 pr_warn_ratelimited("VFS: Dirty inode writeback failed "
66 "for block device %s (err=%d).\n",
67 bdevname(bdev, name), ret);
68 }
69 spin_lock(&inode->i_lock);
70 }
71 spin_unlock(&inode->i_lock);
72 }
73
74 /* Kill _all_ buffers and pagecache , dirty or not.. */
75 void kill_bdev(struct block_device *bdev)
76 {
77 struct address_space *mapping = bdev->bd_inode->i_mapping;
78
79 if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
80 return;
81
82 invalidate_bh_lrus();
83 truncate_inode_pages(mapping, 0);
84 }
85 EXPORT_SYMBOL(kill_bdev);
86
87 /* Invalidate clean unused buffers and pagecache. */
88 void invalidate_bdev(struct block_device *bdev)
89 {
90 struct address_space *mapping = bdev->bd_inode->i_mapping;
91
92 if (mapping->nrpages == 0)
93 return;
94
95 invalidate_bh_lrus();
96 lru_add_drain_all(); /* make sure all lru add caches are flushed */
97 invalidate_mapping_pages(mapping, 0, -1);
98 /* 99% of the time, we don't need to flush the cleancache on the bdev.
99 * But, for the strange corners, lets be cautious
100 */
101 cleancache_invalidate_inode(mapping);
102 }
103 EXPORT_SYMBOL(invalidate_bdev);
104
105 int set_blocksize(struct block_device *bdev, int size)
106 {
107 /* Size must be a power of two, and between 512 and PAGE_SIZE */
108 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
109 return -EINVAL;
110
111 /* Size cannot be smaller than the size supported by the device */
112 if (size < bdev_logical_block_size(bdev))
113 return -EINVAL;
114
115 /* Don't change the size if it is same as current */
116 if (bdev->bd_block_size != size) {
117 sync_blockdev(bdev);
118 bdev->bd_block_size = size;
119 bdev->bd_inode->i_blkbits = blksize_bits(size);
120 kill_bdev(bdev);
121 }
122 return 0;
123 }
124
125 EXPORT_SYMBOL(set_blocksize);
126
127 int sb_set_blocksize(struct super_block *sb, int size)
128 {
129 if (set_blocksize(sb->s_bdev, size))
130 return 0;
131 /* If we get here, we know size is power of two
132 * and it's value is between 512 and PAGE_SIZE */
133 sb->s_blocksize = size;
134 sb->s_blocksize_bits = blksize_bits(size);
135 return sb->s_blocksize;
136 }
137
138 EXPORT_SYMBOL(sb_set_blocksize);
139
140 int sb_min_blocksize(struct super_block *sb, int size)
141 {
142 int minsize = bdev_logical_block_size(sb->s_bdev);
143 if (size < minsize)
144 size = minsize;
145 return sb_set_blocksize(sb, size);
146 }
147
148 EXPORT_SYMBOL(sb_min_blocksize);
149
150 static int
151 blkdev_get_block(struct inode *inode, sector_t iblock,
152 struct buffer_head *bh, int create)
153 {
154 bh->b_bdev = I_BDEV(inode);
155 bh->b_blocknr = iblock;
156 set_buffer_mapped(bh);
157 return 0;
158 }
159
160 static struct inode *bdev_file_inode(struct file *file)
161 {
162 return file->f_mapping->host;
163 }
164
165 static ssize_t
166 blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
167 {
168 struct file *file = iocb->ki_filp;
169 struct inode *inode = bdev_file_inode(file);
170
171 if (IS_DAX(inode))
172 return dax_do_io(iocb, inode, iter, blkdev_get_block,
173 NULL, DIO_SKIP_DIO_COUNT);
174 return __blockdev_direct_IO(iocb, inode, I_BDEV(inode), iter,
175 blkdev_get_block, NULL, NULL,
176 DIO_SKIP_DIO_COUNT);
177 }
178
179 int __sync_blockdev(struct block_device *bdev, int wait)
180 {
181 if (!bdev)
182 return 0;
183 if (!wait)
184 return filemap_flush(bdev->bd_inode->i_mapping);
185 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
186 }
187
188 /*
189 * Write out and wait upon all the dirty data associated with a block
190 * device via its mapping. Does not take the superblock lock.
191 */
192 int sync_blockdev(struct block_device *bdev)
193 {
194 return __sync_blockdev(bdev, 1);
195 }
196 EXPORT_SYMBOL(sync_blockdev);
197
198 /*
199 * Write out and wait upon all dirty data associated with this
200 * device. Filesystem data as well as the underlying block
201 * device. Takes the superblock lock.
202 */
203 int fsync_bdev(struct block_device *bdev)
204 {
205 struct super_block *sb = get_super(bdev);
206 if (sb) {
207 int res = sync_filesystem(sb);
208 drop_super(sb);
209 return res;
210 }
211 return sync_blockdev(bdev);
212 }
213 EXPORT_SYMBOL(fsync_bdev);
214
215 /**
216 * freeze_bdev -- lock a filesystem and force it into a consistent state
217 * @bdev: blockdevice to lock
218 *
219 * If a superblock is found on this device, we take the s_umount semaphore
220 * on it to make sure nobody unmounts until the snapshot creation is done.
221 * The reference counter (bd_fsfreeze_count) guarantees that only the last
222 * unfreeze process can unfreeze the frozen filesystem actually when multiple
223 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
224 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
225 * actually.
226 */
227 struct super_block *freeze_bdev(struct block_device *bdev)
228 {
229 struct super_block *sb;
230 int error = 0;
231
232 mutex_lock(&bdev->bd_fsfreeze_mutex);
233 if (++bdev->bd_fsfreeze_count > 1) {
234 /*
235 * We don't even need to grab a reference - the first call
236 * to freeze_bdev grab an active reference and only the last
237 * thaw_bdev drops it.
238 */
239 sb = get_super(bdev);
240 drop_super(sb);
241 mutex_unlock(&bdev->bd_fsfreeze_mutex);
242 return sb;
243 }
244
245 sb = get_active_super(bdev);
246 if (!sb)
247 goto out;
248 if (sb->s_op->freeze_super)
249 error = sb->s_op->freeze_super(sb);
250 else
251 error = freeze_super(sb);
252 if (error) {
253 deactivate_super(sb);
254 bdev->bd_fsfreeze_count--;
255 mutex_unlock(&bdev->bd_fsfreeze_mutex);
256 return ERR_PTR(error);
257 }
258 deactivate_super(sb);
259 out:
260 sync_blockdev(bdev);
261 mutex_unlock(&bdev->bd_fsfreeze_mutex);
262 return sb; /* thaw_bdev releases s->s_umount */
263 }
264 EXPORT_SYMBOL(freeze_bdev);
265
266 /**
267 * thaw_bdev -- unlock filesystem
268 * @bdev: blockdevice to unlock
269 * @sb: associated superblock
270 *
271 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
272 */
273 int thaw_bdev(struct block_device *bdev, struct super_block *sb)
274 {
275 int error = -EINVAL;
276
277 mutex_lock(&bdev->bd_fsfreeze_mutex);
278 if (!bdev->bd_fsfreeze_count)
279 goto out;
280
281 error = 0;
282 if (--bdev->bd_fsfreeze_count > 0)
283 goto out;
284
285 if (!sb)
286 goto out;
287
288 if (sb->s_op->thaw_super)
289 error = sb->s_op->thaw_super(sb);
290 else
291 error = thaw_super(sb);
292 if (error) {
293 bdev->bd_fsfreeze_count++;
294 mutex_unlock(&bdev->bd_fsfreeze_mutex);
295 return error;
296 }
297 out:
298 mutex_unlock(&bdev->bd_fsfreeze_mutex);
299 return 0;
300 }
301 EXPORT_SYMBOL(thaw_bdev);
302
303 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
304 {
305 return block_write_full_page(page, blkdev_get_block, wbc);
306 }
307
308 static int blkdev_readpage(struct file * file, struct page * page)
309 {
310 return block_read_full_page(page, blkdev_get_block);
311 }
312
313 static int blkdev_readpages(struct file *file, struct address_space *mapping,
314 struct list_head *pages, unsigned nr_pages)
315 {
316 return mpage_readpages(mapping, pages, nr_pages, blkdev_get_block);
317 }
318
319 static int blkdev_write_begin(struct file *file, struct address_space *mapping,
320 loff_t pos, unsigned len, unsigned flags,
321 struct page **pagep, void **fsdata)
322 {
323 return block_write_begin(mapping, pos, len, flags, pagep,
324 blkdev_get_block);
325 }
326
327 static int blkdev_write_end(struct file *file, struct address_space *mapping,
328 loff_t pos, unsigned len, unsigned copied,
329 struct page *page, void *fsdata)
330 {
331 int ret;
332 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
333
334 unlock_page(page);
335 put_page(page);
336
337 return ret;
338 }
339
340 /*
341 * private llseek:
342 * for a block special file file_inode(file)->i_size is zero
343 * so we compute the size by hand (just as in block_read/write above)
344 */
345 static loff_t block_llseek(struct file *file, loff_t offset, int whence)
346 {
347 struct inode *bd_inode = bdev_file_inode(file);
348 loff_t retval;
349
350 inode_lock(bd_inode);
351 retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
352 inode_unlock(bd_inode);
353 return retval;
354 }
355
356 int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
357 {
358 struct inode *bd_inode = bdev_file_inode(filp);
359 struct block_device *bdev = I_BDEV(bd_inode);
360 int error;
361
362 error = filemap_write_and_wait_range(filp->f_mapping, start, end);
363 if (error)
364 return error;
365
366 /*
367 * There is no need to serialise calls to blkdev_issue_flush with
368 * i_mutex and doing so causes performance issues with concurrent
369 * O_SYNC writers to a block device.
370 */
371 error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
372 if (error == -EOPNOTSUPP)
373 error = 0;
374
375 return error;
376 }
377 EXPORT_SYMBOL(blkdev_fsync);
378
379 /**
380 * bdev_read_page() - Start reading a page from a block device
381 * @bdev: The device to read the page from
382 * @sector: The offset on the device to read the page to (need not be aligned)
383 * @page: The page to read
384 *
385 * On entry, the page should be locked. It will be unlocked when the page
386 * has been read. If the block driver implements rw_page synchronously,
387 * that will be true on exit from this function, but it need not be.
388 *
389 * Errors returned by this function are usually "soft", eg out of memory, or
390 * queue full; callers should try a different route to read this page rather
391 * than propagate an error back up the stack.
392 *
393 * Return: negative errno if an error occurs, 0 if submission was successful.
394 */
395 int bdev_read_page(struct block_device *bdev, sector_t sector,
396 struct page *page)
397 {
398 const struct block_device_operations *ops = bdev->bd_disk->fops;
399 int result = -EOPNOTSUPP;
400
401 if (!ops->rw_page || bdev_get_integrity(bdev))
402 return result;
403
404 result = blk_queue_enter(bdev->bd_queue, false);
405 if (result)
406 return result;
407 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, READ);
408 blk_queue_exit(bdev->bd_queue);
409 return result;
410 }
411 EXPORT_SYMBOL_GPL(bdev_read_page);
412
413 /**
414 * bdev_write_page() - Start writing a page to a block device
415 * @bdev: The device to write the page to
416 * @sector: The offset on the device to write the page to (need not be aligned)
417 * @page: The page to write
418 * @wbc: The writeback_control for the write
419 *
420 * On entry, the page should be locked and not currently under writeback.
421 * On exit, if the write started successfully, the page will be unlocked and
422 * under writeback. If the write failed already (eg the driver failed to
423 * queue the page to the device), the page will still be locked. If the
424 * caller is a ->writepage implementation, it will need to unlock the page.
425 *
426 * Errors returned by this function are usually "soft", eg out of memory, or
427 * queue full; callers should try a different route to write this page rather
428 * than propagate an error back up the stack.
429 *
430 * Return: negative errno if an error occurs, 0 if submission was successful.
431 */
432 int bdev_write_page(struct block_device *bdev, sector_t sector,
433 struct page *page, struct writeback_control *wbc)
434 {
435 int result;
436 int rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE;
437 const struct block_device_operations *ops = bdev->bd_disk->fops;
438
439 if (!ops->rw_page || bdev_get_integrity(bdev))
440 return -EOPNOTSUPP;
441 result = blk_queue_enter(bdev->bd_queue, false);
442 if (result)
443 return result;
444
445 set_page_writeback(page);
446 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, rw);
447 if (result)
448 end_page_writeback(page);
449 else
450 unlock_page(page);
451 blk_queue_exit(bdev->bd_queue);
452 return result;
453 }
454 EXPORT_SYMBOL_GPL(bdev_write_page);
455
456 /**
457 * bdev_direct_access() - Get the address for directly-accessibly memory
458 * @bdev: The device containing the memory
459 * @dax: control and output parameters for ->direct_access
460 *
461 * If a block device is made up of directly addressable memory, this function
462 * will tell the caller the PFN and the address of the memory. The address
463 * may be directly dereferenced within the kernel without the need to call
464 * ioremap(), kmap() or similar. The PFN is suitable for inserting into
465 * page tables.
466 *
467 * Return: negative errno if an error occurs, otherwise the number of bytes
468 * accessible at this address.
469 */
470 long bdev_direct_access(struct block_device *bdev, struct blk_dax_ctl *dax)
471 {
472 sector_t sector = dax->sector;
473 long avail, size = dax->size;
474 const struct block_device_operations *ops = bdev->bd_disk->fops;
475
476 /*
477 * The device driver is allowed to sleep, in order to make the
478 * memory directly accessible.
479 */
480 might_sleep();
481
482 if (size < 0)
483 return size;
484 if (!ops->direct_access)
485 return -EOPNOTSUPP;
486 if ((sector + DIV_ROUND_UP(size, 512)) >
487 part_nr_sects_read(bdev->bd_part))
488 return -ERANGE;
489 sector += get_start_sect(bdev);
490 if (sector % (PAGE_SIZE / 512))
491 return -EINVAL;
492 avail = ops->direct_access(bdev, sector, &dax->addr, &dax->pfn);
493 if (!avail)
494 return -ERANGE;
495 if (avail > 0 && avail & ~PAGE_MASK)
496 return -ENXIO;
497 return min(avail, size);
498 }
499 EXPORT_SYMBOL_GPL(bdev_direct_access);
500
501 /*
502 * pseudo-fs
503 */
504
505 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
506 static struct kmem_cache * bdev_cachep __read_mostly;
507
508 static struct inode *bdev_alloc_inode(struct super_block *sb)
509 {
510 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
511 if (!ei)
512 return NULL;
513 return &ei->vfs_inode;
514 }
515
516 static void bdev_i_callback(struct rcu_head *head)
517 {
518 struct inode *inode = container_of(head, struct inode, i_rcu);
519 struct bdev_inode *bdi = BDEV_I(inode);
520
521 kmem_cache_free(bdev_cachep, bdi);
522 }
523
524 static void bdev_destroy_inode(struct inode *inode)
525 {
526 call_rcu(&inode->i_rcu, bdev_i_callback);
527 }
528
529 static void init_once(void *foo)
530 {
531 struct bdev_inode *ei = (struct bdev_inode *) foo;
532 struct block_device *bdev = &ei->bdev;
533
534 memset(bdev, 0, sizeof(*bdev));
535 mutex_init(&bdev->bd_mutex);
536 INIT_LIST_HEAD(&bdev->bd_inodes);
537 INIT_LIST_HEAD(&bdev->bd_list);
538 #ifdef CONFIG_SYSFS
539 INIT_LIST_HEAD(&bdev->bd_holder_disks);
540 #endif
541 inode_init_once(&ei->vfs_inode);
542 /* Initialize mutex for freeze. */
543 mutex_init(&bdev->bd_fsfreeze_mutex);
544 }
545
546 static inline void __bd_forget(struct inode *inode)
547 {
548 list_del_init(&inode->i_devices);
549 inode->i_bdev = NULL;
550 inode->i_mapping = &inode->i_data;
551 }
552
553 static void bdev_evict_inode(struct inode *inode)
554 {
555 struct block_device *bdev = &BDEV_I(inode)->bdev;
556 struct list_head *p;
557 truncate_inode_pages_final(&inode->i_data);
558 invalidate_inode_buffers(inode); /* is it needed here? */
559 clear_inode(inode);
560 spin_lock(&bdev_lock);
561 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
562 __bd_forget(list_entry(p, struct inode, i_devices));
563 }
564 list_del_init(&bdev->bd_list);
565 spin_unlock(&bdev_lock);
566 }
567
568 static const struct super_operations bdev_sops = {
569 .statfs = simple_statfs,
570 .alloc_inode = bdev_alloc_inode,
571 .destroy_inode = bdev_destroy_inode,
572 .drop_inode = generic_delete_inode,
573 .evict_inode = bdev_evict_inode,
574 };
575
576 static struct dentry *bd_mount(struct file_system_type *fs_type,
577 int flags, const char *dev_name, void *data)
578 {
579 struct dentry *dent;
580 dent = mount_pseudo(fs_type, "bdev:", &bdev_sops, NULL, BDEVFS_MAGIC);
581 if (dent)
582 dent->d_sb->s_iflags |= SB_I_CGROUPWB;
583 return dent;
584 }
585
586 static struct file_system_type bd_type = {
587 .name = "bdev",
588 .mount = bd_mount,
589 .kill_sb = kill_anon_super,
590 };
591
592 struct super_block *blockdev_superblock __read_mostly;
593 EXPORT_SYMBOL_GPL(blockdev_superblock);
594
595 void __init bdev_cache_init(void)
596 {
597 int err;
598 static struct vfsmount *bd_mnt;
599
600 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
601 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
602 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
603 init_once);
604 err = register_filesystem(&bd_type);
605 if (err)
606 panic("Cannot register bdev pseudo-fs");
607 bd_mnt = kern_mount(&bd_type);
608 if (IS_ERR(bd_mnt))
609 panic("Cannot create bdev pseudo-fs");
610 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
611 }
612
613 /*
614 * Most likely _very_ bad one - but then it's hardly critical for small
615 * /dev and can be fixed when somebody will need really large one.
616 * Keep in mind that it will be fed through icache hash function too.
617 */
618 static inline unsigned long hash(dev_t dev)
619 {
620 return MAJOR(dev)+MINOR(dev);
621 }
622
623 static int bdev_test(struct inode *inode, void *data)
624 {
625 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
626 }
627
628 static int bdev_set(struct inode *inode, void *data)
629 {
630 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
631 return 0;
632 }
633
634 static LIST_HEAD(all_bdevs);
635
636 struct block_device *bdget(dev_t dev)
637 {
638 struct block_device *bdev;
639 struct inode *inode;
640
641 inode = iget5_locked(blockdev_superblock, hash(dev),
642 bdev_test, bdev_set, &dev);
643
644 if (!inode)
645 return NULL;
646
647 bdev = &BDEV_I(inode)->bdev;
648
649 if (inode->i_state & I_NEW) {
650 bdev->bd_contains = NULL;
651 bdev->bd_super = NULL;
652 bdev->bd_inode = inode;
653 bdev->bd_block_size = (1 << inode->i_blkbits);
654 bdev->bd_part_count = 0;
655 bdev->bd_invalidated = 0;
656 inode->i_mode = S_IFBLK;
657 inode->i_rdev = dev;
658 inode->i_bdev = bdev;
659 inode->i_data.a_ops = &def_blk_aops;
660 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
661 spin_lock(&bdev_lock);
662 list_add(&bdev->bd_list, &all_bdevs);
663 spin_unlock(&bdev_lock);
664 unlock_new_inode(inode);
665 }
666 return bdev;
667 }
668
669 EXPORT_SYMBOL(bdget);
670
671 /**
672 * bdgrab -- Grab a reference to an already referenced block device
673 * @bdev: Block device to grab a reference to.
674 */
675 struct block_device *bdgrab(struct block_device *bdev)
676 {
677 ihold(bdev->bd_inode);
678 return bdev;
679 }
680 EXPORT_SYMBOL(bdgrab);
681
682 long nr_blockdev_pages(void)
683 {
684 struct block_device *bdev;
685 long ret = 0;
686 spin_lock(&bdev_lock);
687 list_for_each_entry(bdev, &all_bdevs, bd_list) {
688 ret += bdev->bd_inode->i_mapping->nrpages;
689 }
690 spin_unlock(&bdev_lock);
691 return ret;
692 }
693
694 void bdput(struct block_device *bdev)
695 {
696 iput(bdev->bd_inode);
697 }
698
699 EXPORT_SYMBOL(bdput);
700
701 static struct block_device *bd_acquire(struct inode *inode)
702 {
703 struct block_device *bdev;
704
705 spin_lock(&bdev_lock);
706 bdev = inode->i_bdev;
707 if (bdev) {
708 bdgrab(bdev);
709 spin_unlock(&bdev_lock);
710 return bdev;
711 }
712 spin_unlock(&bdev_lock);
713
714 bdev = bdget(inode->i_rdev);
715 if (bdev) {
716 spin_lock(&bdev_lock);
717 if (!inode->i_bdev) {
718 /*
719 * We take an additional reference to bd_inode,
720 * and it's released in clear_inode() of inode.
721 * So, we can access it via ->i_mapping always
722 * without igrab().
723 */
724 bdgrab(bdev);
725 inode->i_bdev = bdev;
726 inode->i_mapping = bdev->bd_inode->i_mapping;
727 list_add(&inode->i_devices, &bdev->bd_inodes);
728 }
729 spin_unlock(&bdev_lock);
730 }
731 return bdev;
732 }
733
734 /* Call when you free inode */
735
736 void bd_forget(struct inode *inode)
737 {
738 struct block_device *bdev = NULL;
739
740 spin_lock(&bdev_lock);
741 if (!sb_is_blkdev_sb(inode->i_sb))
742 bdev = inode->i_bdev;
743 __bd_forget(inode);
744 spin_unlock(&bdev_lock);
745
746 if (bdev)
747 bdput(bdev);
748 }
749
750 /**
751 * bd_may_claim - test whether a block device can be claimed
752 * @bdev: block device of interest
753 * @whole: whole block device containing @bdev, may equal @bdev
754 * @holder: holder trying to claim @bdev
755 *
756 * Test whether @bdev can be claimed by @holder.
757 *
758 * CONTEXT:
759 * spin_lock(&bdev_lock).
760 *
761 * RETURNS:
762 * %true if @bdev can be claimed, %false otherwise.
763 */
764 static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
765 void *holder)
766 {
767 if (bdev->bd_holder == holder)
768 return true; /* already a holder */
769 else if (bdev->bd_holder != NULL)
770 return false; /* held by someone else */
771 else if (bdev->bd_contains == bdev)
772 return true; /* is a whole device which isn't held */
773
774 else if (whole->bd_holder == bd_may_claim)
775 return true; /* is a partition of a device that is being partitioned */
776 else if (whole->bd_holder != NULL)
777 return false; /* is a partition of a held device */
778 else
779 return true; /* is a partition of an un-held device */
780 }
781
782 /**
783 * bd_prepare_to_claim - prepare to claim a block device
784 * @bdev: block device of interest
785 * @whole: the whole device containing @bdev, may equal @bdev
786 * @holder: holder trying to claim @bdev
787 *
788 * Prepare to claim @bdev. This function fails if @bdev is already
789 * claimed by another holder and waits if another claiming is in
790 * progress. This function doesn't actually claim. On successful
791 * return, the caller has ownership of bd_claiming and bd_holder[s].
792 *
793 * CONTEXT:
794 * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
795 * it multiple times.
796 *
797 * RETURNS:
798 * 0 if @bdev can be claimed, -EBUSY otherwise.
799 */
800 static int bd_prepare_to_claim(struct block_device *bdev,
801 struct block_device *whole, void *holder)
802 {
803 retry:
804 /* if someone else claimed, fail */
805 if (!bd_may_claim(bdev, whole, holder))
806 return -EBUSY;
807
808 /* if claiming is already in progress, wait for it to finish */
809 if (whole->bd_claiming) {
810 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
811 DEFINE_WAIT(wait);
812
813 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
814 spin_unlock(&bdev_lock);
815 schedule();
816 finish_wait(wq, &wait);
817 spin_lock(&bdev_lock);
818 goto retry;
819 }
820
821 /* yay, all mine */
822 return 0;
823 }
824
825 /**
826 * bd_start_claiming - start claiming a block device
827 * @bdev: block device of interest
828 * @holder: holder trying to claim @bdev
829 *
830 * @bdev is about to be opened exclusively. Check @bdev can be opened
831 * exclusively and mark that an exclusive open is in progress. Each
832 * successful call to this function must be matched with a call to
833 * either bd_finish_claiming() or bd_abort_claiming() (which do not
834 * fail).
835 *
836 * This function is used to gain exclusive access to the block device
837 * without actually causing other exclusive open attempts to fail. It
838 * should be used when the open sequence itself requires exclusive
839 * access but may subsequently fail.
840 *
841 * CONTEXT:
842 * Might sleep.
843 *
844 * RETURNS:
845 * Pointer to the block device containing @bdev on success, ERR_PTR()
846 * value on failure.
847 */
848 static struct block_device *bd_start_claiming(struct block_device *bdev,
849 void *holder)
850 {
851 struct gendisk *disk;
852 struct block_device *whole;
853 int partno, err;
854
855 might_sleep();
856
857 /*
858 * @bdev might not have been initialized properly yet, look up
859 * and grab the outer block device the hard way.
860 */
861 disk = get_gendisk(bdev->bd_dev, &partno);
862 if (!disk)
863 return ERR_PTR(-ENXIO);
864
865 /*
866 * Normally, @bdev should equal what's returned from bdget_disk()
867 * if partno is 0; however, some drivers (floppy) use multiple
868 * bdev's for the same physical device and @bdev may be one of the
869 * aliases. Keep @bdev if partno is 0. This means claimer
870 * tracking is broken for those devices but it has always been that
871 * way.
872 */
873 if (partno)
874 whole = bdget_disk(disk, 0);
875 else
876 whole = bdgrab(bdev);
877
878 module_put(disk->fops->owner);
879 put_disk(disk);
880 if (!whole)
881 return ERR_PTR(-ENOMEM);
882
883 /* prepare to claim, if successful, mark claiming in progress */
884 spin_lock(&bdev_lock);
885
886 err = bd_prepare_to_claim(bdev, whole, holder);
887 if (err == 0) {
888 whole->bd_claiming = holder;
889 spin_unlock(&bdev_lock);
890 return whole;
891 } else {
892 spin_unlock(&bdev_lock);
893 bdput(whole);
894 return ERR_PTR(err);
895 }
896 }
897
898 #ifdef CONFIG_SYSFS
899 struct bd_holder_disk {
900 struct list_head list;
901 struct gendisk *disk;
902 int refcnt;
903 };
904
905 static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
906 struct gendisk *disk)
907 {
908 struct bd_holder_disk *holder;
909
910 list_for_each_entry(holder, &bdev->bd_holder_disks, list)
911 if (holder->disk == disk)
912 return holder;
913 return NULL;
914 }
915
916 static int add_symlink(struct kobject *from, struct kobject *to)
917 {
918 return sysfs_create_link(from, to, kobject_name(to));
919 }
920
921 static void del_symlink(struct kobject *from, struct kobject *to)
922 {
923 sysfs_remove_link(from, kobject_name(to));
924 }
925
926 /**
927 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
928 * @bdev: the claimed slave bdev
929 * @disk: the holding disk
930 *
931 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
932 *
933 * This functions creates the following sysfs symlinks.
934 *
935 * - from "slaves" directory of the holder @disk to the claimed @bdev
936 * - from "holders" directory of the @bdev to the holder @disk
937 *
938 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
939 * passed to bd_link_disk_holder(), then:
940 *
941 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
942 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
943 *
944 * The caller must have claimed @bdev before calling this function and
945 * ensure that both @bdev and @disk are valid during the creation and
946 * lifetime of these symlinks.
947 *
948 * CONTEXT:
949 * Might sleep.
950 *
951 * RETURNS:
952 * 0 on success, -errno on failure.
953 */
954 int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
955 {
956 struct bd_holder_disk *holder;
957 int ret = 0;
958
959 mutex_lock(&bdev->bd_mutex);
960
961 WARN_ON_ONCE(!bdev->bd_holder);
962
963 /* FIXME: remove the following once add_disk() handles errors */
964 if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
965 goto out_unlock;
966
967 holder = bd_find_holder_disk(bdev, disk);
968 if (holder) {
969 holder->refcnt++;
970 goto out_unlock;
971 }
972
973 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
974 if (!holder) {
975 ret = -ENOMEM;
976 goto out_unlock;
977 }
978
979 INIT_LIST_HEAD(&holder->list);
980 holder->disk = disk;
981 holder->refcnt = 1;
982
983 ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
984 if (ret)
985 goto out_free;
986
987 ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
988 if (ret)
989 goto out_del;
990 /*
991 * bdev could be deleted beneath us which would implicitly destroy
992 * the holder directory. Hold on to it.
993 */
994 kobject_get(bdev->bd_part->holder_dir);
995
996 list_add(&holder->list, &bdev->bd_holder_disks);
997 goto out_unlock;
998
999 out_del:
1000 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1001 out_free:
1002 kfree(holder);
1003 out_unlock:
1004 mutex_unlock(&bdev->bd_mutex);
1005 return ret;
1006 }
1007 EXPORT_SYMBOL_GPL(bd_link_disk_holder);
1008
1009 /**
1010 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
1011 * @bdev: the calimed slave bdev
1012 * @disk: the holding disk
1013 *
1014 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1015 *
1016 * CONTEXT:
1017 * Might sleep.
1018 */
1019 void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
1020 {
1021 struct bd_holder_disk *holder;
1022
1023 mutex_lock(&bdev->bd_mutex);
1024
1025 holder = bd_find_holder_disk(bdev, disk);
1026
1027 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
1028 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1029 del_symlink(bdev->bd_part->holder_dir,
1030 &disk_to_dev(disk)->kobj);
1031 kobject_put(bdev->bd_part->holder_dir);
1032 list_del_init(&holder->list);
1033 kfree(holder);
1034 }
1035
1036 mutex_unlock(&bdev->bd_mutex);
1037 }
1038 EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
1039 #endif
1040
1041 /**
1042 * flush_disk - invalidates all buffer-cache entries on a disk
1043 *
1044 * @bdev: struct block device to be flushed
1045 * @kill_dirty: flag to guide handling of dirty inodes
1046 *
1047 * Invalidates all buffer-cache entries on a disk. It should be called
1048 * when a disk has been changed -- either by a media change or online
1049 * resize.
1050 */
1051 static void flush_disk(struct block_device *bdev, bool kill_dirty)
1052 {
1053 if (__invalidate_device(bdev, kill_dirty)) {
1054 printk(KERN_WARNING "VFS: busy inodes on changed media or "
1055 "resized disk %s\n",
1056 bdev->bd_disk ? bdev->bd_disk->disk_name : "");
1057 }
1058
1059 if (!bdev->bd_disk)
1060 return;
1061 if (disk_part_scan_enabled(bdev->bd_disk))
1062 bdev->bd_invalidated = 1;
1063 }
1064
1065 /**
1066 * check_disk_size_change - checks for disk size change and adjusts bdev size.
1067 * @disk: struct gendisk to check
1068 * @bdev: struct bdev to adjust.
1069 *
1070 * This routine checks to see if the bdev size does not match the disk size
1071 * and adjusts it if it differs.
1072 */
1073 void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
1074 {
1075 loff_t disk_size, bdev_size;
1076
1077 disk_size = (loff_t)get_capacity(disk) << 9;
1078 bdev_size = i_size_read(bdev->bd_inode);
1079 if (disk_size != bdev_size) {
1080 printk(KERN_INFO
1081 "%s: detected capacity change from %lld to %lld\n",
1082 disk->disk_name, bdev_size, disk_size);
1083 i_size_write(bdev->bd_inode, disk_size);
1084 flush_disk(bdev, false);
1085 }
1086 }
1087 EXPORT_SYMBOL(check_disk_size_change);
1088
1089 /**
1090 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
1091 * @disk: struct gendisk to be revalidated
1092 *
1093 * This routine is a wrapper for lower-level driver's revalidate_disk
1094 * call-backs. It is used to do common pre and post operations needed
1095 * for all revalidate_disk operations.
1096 */
1097 int revalidate_disk(struct gendisk *disk)
1098 {
1099 struct block_device *bdev;
1100 int ret = 0;
1101
1102 if (disk->fops->revalidate_disk)
1103 ret = disk->fops->revalidate_disk(disk);
1104 blk_integrity_revalidate(disk);
1105 bdev = bdget_disk(disk, 0);
1106 if (!bdev)
1107 return ret;
1108
1109 mutex_lock(&bdev->bd_mutex);
1110 check_disk_size_change(disk, bdev);
1111 bdev->bd_invalidated = 0;
1112 mutex_unlock(&bdev->bd_mutex);
1113 bdput(bdev);
1114 return ret;
1115 }
1116 EXPORT_SYMBOL(revalidate_disk);
1117
1118 /*
1119 * This routine checks whether a removable media has been changed,
1120 * and invalidates all buffer-cache-entries in that case. This
1121 * is a relatively slow routine, so we have to try to minimize using
1122 * it. Thus it is called only upon a 'mount' or 'open'. This
1123 * is the best way of combining speed and utility, I think.
1124 * People changing diskettes in the middle of an operation deserve
1125 * to lose :-)
1126 */
1127 int check_disk_change(struct block_device *bdev)
1128 {
1129 struct gendisk *disk = bdev->bd_disk;
1130 const struct block_device_operations *bdops = disk->fops;
1131 unsigned int events;
1132
1133 events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
1134 DISK_EVENT_EJECT_REQUEST);
1135 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1136 return 0;
1137
1138 flush_disk(bdev, true);
1139 if (bdops->revalidate_disk)
1140 bdops->revalidate_disk(bdev->bd_disk);
1141 return 1;
1142 }
1143
1144 EXPORT_SYMBOL(check_disk_change);
1145
1146 void bd_set_size(struct block_device *bdev, loff_t size)
1147 {
1148 unsigned bsize = bdev_logical_block_size(bdev);
1149
1150 inode_lock(bdev->bd_inode);
1151 i_size_write(bdev->bd_inode, size);
1152 inode_unlock(bdev->bd_inode);
1153 while (bsize < PAGE_SIZE) {
1154 if (size & bsize)
1155 break;
1156 bsize <<= 1;
1157 }
1158 bdev->bd_block_size = bsize;
1159 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
1160 }
1161 EXPORT_SYMBOL(bd_set_size);
1162
1163 static bool blkdev_dax_capable(struct block_device *bdev)
1164 {
1165 struct gendisk *disk = bdev->bd_disk;
1166
1167 if (!disk->fops->direct_access || !IS_ENABLED(CONFIG_FS_DAX))
1168 return false;
1169
1170 /*
1171 * If the partition is not aligned on a page boundary, we can't
1172 * do dax I/O to it.
1173 */
1174 if ((bdev->bd_part->start_sect % (PAGE_SIZE / 512))
1175 || (bdev->bd_part->nr_sects % (PAGE_SIZE / 512)))
1176 return false;
1177
1178 /*
1179 * If the device has known bad blocks, force all I/O through the
1180 * driver / page cache.
1181 *
1182 * TODO: support finer grained dax error handling
1183 */
1184 if (disk->bb && disk->bb->count)
1185 return false;
1186
1187 return true;
1188 }
1189
1190 static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
1191
1192 /*
1193 * bd_mutex locking:
1194 *
1195 * mutex_lock(part->bd_mutex)
1196 * mutex_lock_nested(whole->bd_mutex, 1)
1197 */
1198
1199 static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
1200 {
1201 struct gendisk *disk;
1202 struct module *owner;
1203 int ret;
1204 int partno;
1205 int perm = 0;
1206
1207 if (mode & FMODE_READ)
1208 perm |= MAY_READ;
1209 if (mode & FMODE_WRITE)
1210 perm |= MAY_WRITE;
1211 /*
1212 * hooks: /n/, see "layering violations".
1213 */
1214 if (!for_part) {
1215 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1216 if (ret != 0) {
1217 bdput(bdev);
1218 return ret;
1219 }
1220 }
1221
1222 restart:
1223
1224 ret = -ENXIO;
1225 disk = get_gendisk(bdev->bd_dev, &partno);
1226 if (!disk)
1227 goto out;
1228 owner = disk->fops->owner;
1229
1230 disk_block_events(disk);
1231 mutex_lock_nested(&bdev->bd_mutex, for_part);
1232 if (!bdev->bd_openers) {
1233 bdev->bd_disk = disk;
1234 bdev->bd_queue = disk->queue;
1235 bdev->bd_contains = bdev;
1236 if (IS_ENABLED(CONFIG_BLK_DEV_DAX) && disk->fops->direct_access)
1237 bdev->bd_inode->i_flags = S_DAX;
1238 else
1239 bdev->bd_inode->i_flags = 0;
1240
1241 if (!partno) {
1242 ret = -ENXIO;
1243 bdev->bd_part = disk_get_part(disk, partno);
1244 if (!bdev->bd_part)
1245 goto out_clear;
1246
1247 ret = 0;
1248 if (disk->fops->open) {
1249 ret = disk->fops->open(bdev, mode);
1250 if (ret == -ERESTARTSYS) {
1251 /* Lost a race with 'disk' being
1252 * deleted, try again.
1253 * See md.c
1254 */
1255 disk_put_part(bdev->bd_part);
1256 bdev->bd_part = NULL;
1257 bdev->bd_disk = NULL;
1258 bdev->bd_queue = NULL;
1259 mutex_unlock(&bdev->bd_mutex);
1260 disk_unblock_events(disk);
1261 put_disk(disk);
1262 module_put(owner);
1263 goto restart;
1264 }
1265 }
1266
1267 if (!ret) {
1268 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1269 if (!blkdev_dax_capable(bdev))
1270 bdev->bd_inode->i_flags &= ~S_DAX;
1271 }
1272
1273 /*
1274 * If the device is invalidated, rescan partition
1275 * if open succeeded or failed with -ENOMEDIUM.
1276 * The latter is necessary to prevent ghost
1277 * partitions on a removed medium.
1278 */
1279 if (bdev->bd_invalidated) {
1280 if (!ret)
1281 rescan_partitions(disk, bdev);
1282 else if (ret == -ENOMEDIUM)
1283 invalidate_partitions(disk, bdev);
1284 }
1285
1286 if (ret)
1287 goto out_clear;
1288 } else {
1289 struct block_device *whole;
1290 whole = bdget_disk(disk, 0);
1291 ret = -ENOMEM;
1292 if (!whole)
1293 goto out_clear;
1294 BUG_ON(for_part);
1295 ret = __blkdev_get(whole, mode, 1);
1296 if (ret)
1297 goto out_clear;
1298 bdev->bd_contains = whole;
1299 bdev->bd_part = disk_get_part(disk, partno);
1300 if (!(disk->flags & GENHD_FL_UP) ||
1301 !bdev->bd_part || !bdev->bd_part->nr_sects) {
1302 ret = -ENXIO;
1303 goto out_clear;
1304 }
1305 bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
1306 if (!blkdev_dax_capable(bdev))
1307 bdev->bd_inode->i_flags &= ~S_DAX;
1308 }
1309 } else {
1310 if (bdev->bd_contains == bdev) {
1311 ret = 0;
1312 if (bdev->bd_disk->fops->open)
1313 ret = bdev->bd_disk->fops->open(bdev, mode);
1314 /* the same as first opener case, read comment there */
1315 if (bdev->bd_invalidated) {
1316 if (!ret)
1317 rescan_partitions(bdev->bd_disk, bdev);
1318 else if (ret == -ENOMEDIUM)
1319 invalidate_partitions(bdev->bd_disk, bdev);
1320 }
1321 if (ret)
1322 goto out_unlock_bdev;
1323 }
1324 /* only one opener holds refs to the module and disk */
1325 put_disk(disk);
1326 module_put(owner);
1327 }
1328 bdev->bd_openers++;
1329 if (for_part)
1330 bdev->bd_part_count++;
1331 mutex_unlock(&bdev->bd_mutex);
1332 disk_unblock_events(disk);
1333 return 0;
1334
1335 out_clear:
1336 disk_put_part(bdev->bd_part);
1337 bdev->bd_disk = NULL;
1338 bdev->bd_part = NULL;
1339 bdev->bd_queue = NULL;
1340 if (bdev != bdev->bd_contains)
1341 __blkdev_put(bdev->bd_contains, mode, 1);
1342 bdev->bd_contains = NULL;
1343 out_unlock_bdev:
1344 mutex_unlock(&bdev->bd_mutex);
1345 disk_unblock_events(disk);
1346 put_disk(disk);
1347 module_put(owner);
1348 out:
1349 bdput(bdev);
1350
1351 return ret;
1352 }
1353
1354 /**
1355 * blkdev_get - open a block device
1356 * @bdev: block_device to open
1357 * @mode: FMODE_* mask
1358 * @holder: exclusive holder identifier
1359 *
1360 * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
1361 * open with exclusive access. Specifying %FMODE_EXCL with %NULL
1362 * @holder is invalid. Exclusive opens may nest for the same @holder.
1363 *
1364 * On success, the reference count of @bdev is unchanged. On failure,
1365 * @bdev is put.
1366 *
1367 * CONTEXT:
1368 * Might sleep.
1369 *
1370 * RETURNS:
1371 * 0 on success, -errno on failure.
1372 */
1373 int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
1374 {
1375 struct block_device *whole = NULL;
1376 int res;
1377
1378 WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
1379
1380 if ((mode & FMODE_EXCL) && holder) {
1381 whole = bd_start_claiming(bdev, holder);
1382 if (IS_ERR(whole)) {
1383 bdput(bdev);
1384 return PTR_ERR(whole);
1385 }
1386 }
1387
1388 res = __blkdev_get(bdev, mode, 0);
1389
1390 if (whole) {
1391 struct gendisk *disk = whole->bd_disk;
1392
1393 /* finish claiming */
1394 mutex_lock(&bdev->bd_mutex);
1395 spin_lock(&bdev_lock);
1396
1397 if (!res) {
1398 BUG_ON(!bd_may_claim(bdev, whole, holder));
1399 /*
1400 * Note that for a whole device bd_holders
1401 * will be incremented twice, and bd_holder
1402 * will be set to bd_may_claim before being
1403 * set to holder
1404 */
1405 whole->bd_holders++;
1406 whole->bd_holder = bd_may_claim;
1407 bdev->bd_holders++;
1408 bdev->bd_holder = holder;
1409 }
1410
1411 /* tell others that we're done */
1412 BUG_ON(whole->bd_claiming != holder);
1413 whole->bd_claiming = NULL;
1414 wake_up_bit(&whole->bd_claiming, 0);
1415
1416 spin_unlock(&bdev_lock);
1417
1418 /*
1419 * Block event polling for write claims if requested. Any
1420 * write holder makes the write_holder state stick until
1421 * all are released. This is good enough and tracking
1422 * individual writeable reference is too fragile given the
1423 * way @mode is used in blkdev_get/put().
1424 */
1425 if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder &&
1426 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
1427 bdev->bd_write_holder = true;
1428 disk_block_events(disk);
1429 }
1430
1431 mutex_unlock(&bdev->bd_mutex);
1432 bdput(whole);
1433 }
1434
1435 return res;
1436 }
1437 EXPORT_SYMBOL(blkdev_get);
1438
1439 /**
1440 * blkdev_get_by_path - open a block device by name
1441 * @path: path to the block device to open
1442 * @mode: FMODE_* mask
1443 * @holder: exclusive holder identifier
1444 *
1445 * Open the blockdevice described by the device file at @path. @mode
1446 * and @holder are identical to blkdev_get().
1447 *
1448 * On success, the returned block_device has reference count of one.
1449 *
1450 * CONTEXT:
1451 * Might sleep.
1452 *
1453 * RETURNS:
1454 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1455 */
1456 struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1457 void *holder)
1458 {
1459 struct block_device *bdev;
1460 int err;
1461
1462 bdev = lookup_bdev(path);
1463 if (IS_ERR(bdev))
1464 return bdev;
1465
1466 err = blkdev_get(bdev, mode, holder);
1467 if (err)
1468 return ERR_PTR(err);
1469
1470 if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
1471 blkdev_put(bdev, mode);
1472 return ERR_PTR(-EACCES);
1473 }
1474
1475 return bdev;
1476 }
1477 EXPORT_SYMBOL(blkdev_get_by_path);
1478
1479 /**
1480 * blkdev_get_by_dev - open a block device by device number
1481 * @dev: device number of block device to open
1482 * @mode: FMODE_* mask
1483 * @holder: exclusive holder identifier
1484 *
1485 * Open the blockdevice described by device number @dev. @mode and
1486 * @holder are identical to blkdev_get().
1487 *
1488 * Use it ONLY if you really do not have anything better - i.e. when
1489 * you are behind a truly sucky interface and all you are given is a
1490 * device number. _Never_ to be used for internal purposes. If you
1491 * ever need it - reconsider your API.
1492 *
1493 * On success, the returned block_device has reference count of one.
1494 *
1495 * CONTEXT:
1496 * Might sleep.
1497 *
1498 * RETURNS:
1499 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1500 */
1501 struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
1502 {
1503 struct block_device *bdev;
1504 int err;
1505
1506 bdev = bdget(dev);
1507 if (!bdev)
1508 return ERR_PTR(-ENOMEM);
1509
1510 err = blkdev_get(bdev, mode, holder);
1511 if (err)
1512 return ERR_PTR(err);
1513
1514 return bdev;
1515 }
1516 EXPORT_SYMBOL(blkdev_get_by_dev);
1517
1518 static int blkdev_open(struct inode * inode, struct file * filp)
1519 {
1520 struct block_device *bdev;
1521
1522 /*
1523 * Preserve backwards compatibility and allow large file access
1524 * even if userspace doesn't ask for it explicitly. Some mkfs
1525 * binary needs it. We might want to drop this workaround
1526 * during an unstable branch.
1527 */
1528 filp->f_flags |= O_LARGEFILE;
1529
1530 if (filp->f_flags & O_NDELAY)
1531 filp->f_mode |= FMODE_NDELAY;
1532 if (filp->f_flags & O_EXCL)
1533 filp->f_mode |= FMODE_EXCL;
1534 if ((filp->f_flags & O_ACCMODE) == 3)
1535 filp->f_mode |= FMODE_WRITE_IOCTL;
1536
1537 bdev = bd_acquire(inode);
1538 if (bdev == NULL)
1539 return -ENOMEM;
1540
1541 filp->f_mapping = bdev->bd_inode->i_mapping;
1542
1543 return blkdev_get(bdev, filp->f_mode, filp);
1544 }
1545
1546 static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
1547 {
1548 struct gendisk *disk = bdev->bd_disk;
1549 struct block_device *victim = NULL;
1550
1551 mutex_lock_nested(&bdev->bd_mutex, for_part);
1552 if (for_part)
1553 bdev->bd_part_count--;
1554
1555 if (!--bdev->bd_openers) {
1556 WARN_ON_ONCE(bdev->bd_holders);
1557 sync_blockdev(bdev);
1558 kill_bdev(bdev);
1559
1560 bdev_write_inode(bdev);
1561 /*
1562 * Detaching bdev inode from its wb in __destroy_inode()
1563 * is too late: the queue which embeds its bdi (along with
1564 * root wb) can be gone as soon as we put_disk() below.
1565 */
1566 inode_detach_wb(bdev->bd_inode);
1567 }
1568 if (bdev->bd_contains == bdev) {
1569 if (disk->fops->release)
1570 disk->fops->release(disk, mode);
1571 }
1572 if (!bdev->bd_openers) {
1573 struct module *owner = disk->fops->owner;
1574
1575 disk_put_part(bdev->bd_part);
1576 bdev->bd_part = NULL;
1577 bdev->bd_disk = NULL;
1578 if (bdev != bdev->bd_contains)
1579 victim = bdev->bd_contains;
1580 bdev->bd_contains = NULL;
1581
1582 put_disk(disk);
1583 module_put(owner);
1584 }
1585 mutex_unlock(&bdev->bd_mutex);
1586 bdput(bdev);
1587 if (victim)
1588 __blkdev_put(victim, mode, 1);
1589 }
1590
1591 void blkdev_put(struct block_device *bdev, fmode_t mode)
1592 {
1593 mutex_lock(&bdev->bd_mutex);
1594
1595 if (mode & FMODE_EXCL) {
1596 bool bdev_free;
1597
1598 /*
1599 * Release a claim on the device. The holder fields
1600 * are protected with bdev_lock. bd_mutex is to
1601 * synchronize disk_holder unlinking.
1602 */
1603 spin_lock(&bdev_lock);
1604
1605 WARN_ON_ONCE(--bdev->bd_holders < 0);
1606 WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
1607
1608 /* bd_contains might point to self, check in a separate step */
1609 if ((bdev_free = !bdev->bd_holders))
1610 bdev->bd_holder = NULL;
1611 if (!bdev->bd_contains->bd_holders)
1612 bdev->bd_contains->bd_holder = NULL;
1613
1614 spin_unlock(&bdev_lock);
1615
1616 /*
1617 * If this was the last claim, remove holder link and
1618 * unblock evpoll if it was a write holder.
1619 */
1620 if (bdev_free && bdev->bd_write_holder) {
1621 disk_unblock_events(bdev->bd_disk);
1622 bdev->bd_write_holder = false;
1623 }
1624 }
1625
1626 /*
1627 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
1628 * event. This is to ensure detection of media removal commanded
1629 * from userland - e.g. eject(1).
1630 */
1631 disk_flush_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE);
1632
1633 mutex_unlock(&bdev->bd_mutex);
1634
1635 __blkdev_put(bdev, mode, 0);
1636 }
1637 EXPORT_SYMBOL(blkdev_put);
1638
1639 static int blkdev_close(struct inode * inode, struct file * filp)
1640 {
1641 struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
1642 blkdev_put(bdev, filp->f_mode);
1643 return 0;
1644 }
1645
1646 static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1647 {
1648 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
1649 fmode_t mode = file->f_mode;
1650
1651 /*
1652 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1653 * to updated it before every ioctl.
1654 */
1655 if (file->f_flags & O_NDELAY)
1656 mode |= FMODE_NDELAY;
1657 else
1658 mode &= ~FMODE_NDELAY;
1659
1660 return blkdev_ioctl(bdev, mode, cmd, arg);
1661 }
1662
1663 /*
1664 * Write data to the block device. Only intended for the block device itself
1665 * and the raw driver which basically is a fake block device.
1666 *
1667 * Does not take i_mutex for the write and thus is not for general purpose
1668 * use.
1669 */
1670 ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
1671 {
1672 struct file *file = iocb->ki_filp;
1673 struct inode *bd_inode = bdev_file_inode(file);
1674 loff_t size = i_size_read(bd_inode);
1675 struct blk_plug plug;
1676 ssize_t ret;
1677
1678 if (bdev_read_only(I_BDEV(bd_inode)))
1679 return -EPERM;
1680
1681 if (!iov_iter_count(from))
1682 return 0;
1683
1684 if (iocb->ki_pos >= size)
1685 return -ENOSPC;
1686
1687 iov_iter_truncate(from, size - iocb->ki_pos);
1688
1689 blk_start_plug(&plug);
1690 ret = __generic_file_write_iter(iocb, from);
1691 if (ret > 0)
1692 ret = generic_write_sync(iocb, ret);
1693 blk_finish_plug(&plug);
1694 return ret;
1695 }
1696 EXPORT_SYMBOL_GPL(blkdev_write_iter);
1697
1698 ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
1699 {
1700 struct file *file = iocb->ki_filp;
1701 struct inode *bd_inode = bdev_file_inode(file);
1702 loff_t size = i_size_read(bd_inode);
1703 loff_t pos = iocb->ki_pos;
1704
1705 if (pos >= size)
1706 return 0;
1707
1708 size -= pos;
1709 iov_iter_truncate(to, size);
1710 return generic_file_read_iter(iocb, to);
1711 }
1712 EXPORT_SYMBOL_GPL(blkdev_read_iter);
1713
1714 /*
1715 * Try to release a page associated with block device when the system
1716 * is under memory pressure.
1717 */
1718 static int blkdev_releasepage(struct page *page, gfp_t wait)
1719 {
1720 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1721
1722 if (super && super->s_op->bdev_try_to_free_page)
1723 return super->s_op->bdev_try_to_free_page(super, page, wait);
1724
1725 return try_to_free_buffers(page);
1726 }
1727
1728 static int blkdev_writepages(struct address_space *mapping,
1729 struct writeback_control *wbc)
1730 {
1731 if (dax_mapping(mapping)) {
1732 struct block_device *bdev = I_BDEV(mapping->host);
1733
1734 return dax_writeback_mapping_range(mapping, bdev, wbc);
1735 }
1736 return generic_writepages(mapping, wbc);
1737 }
1738
1739 static const struct address_space_operations def_blk_aops = {
1740 .readpage = blkdev_readpage,
1741 .readpages = blkdev_readpages,
1742 .writepage = blkdev_writepage,
1743 .write_begin = blkdev_write_begin,
1744 .write_end = blkdev_write_end,
1745 .writepages = blkdev_writepages,
1746 .releasepage = blkdev_releasepage,
1747 .direct_IO = blkdev_direct_IO,
1748 .is_dirty_writeback = buffer_check_dirty_writeback,
1749 };
1750
1751 const struct file_operations def_blk_fops = {
1752 .open = blkdev_open,
1753 .release = blkdev_close,
1754 .llseek = block_llseek,
1755 .read_iter = blkdev_read_iter,
1756 .write_iter = blkdev_write_iter,
1757 .mmap = generic_file_mmap,
1758 .fsync = blkdev_fsync,
1759 .unlocked_ioctl = block_ioctl,
1760 #ifdef CONFIG_COMPAT
1761 .compat_ioctl = compat_blkdev_ioctl,
1762 #endif
1763 .splice_read = generic_file_splice_read,
1764 .splice_write = iter_file_splice_write,
1765 };
1766
1767 int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1768 {
1769 int res;
1770 mm_segment_t old_fs = get_fs();
1771 set_fs(KERNEL_DS);
1772 res = blkdev_ioctl(bdev, 0, cmd, arg);
1773 set_fs(old_fs);
1774 return res;
1775 }
1776
1777 EXPORT_SYMBOL(ioctl_by_bdev);
1778
1779 /**
1780 * lookup_bdev - lookup a struct block_device by name
1781 * @pathname: special file representing the block device
1782 *
1783 * Get a reference to the blockdevice at @pathname in the current
1784 * namespace if possible and return it. Return ERR_PTR(error)
1785 * otherwise.
1786 */
1787 struct block_device *lookup_bdev(const char *pathname)
1788 {
1789 struct block_device *bdev;
1790 struct inode *inode;
1791 struct path path;
1792 int error;
1793
1794 if (!pathname || !*pathname)
1795 return ERR_PTR(-EINVAL);
1796
1797 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
1798 if (error)
1799 return ERR_PTR(error);
1800
1801 inode = d_backing_inode(path.dentry);
1802 error = -ENOTBLK;
1803 if (!S_ISBLK(inode->i_mode))
1804 goto fail;
1805 error = -EACCES;
1806 if (path.mnt->mnt_flags & MNT_NODEV)
1807 goto fail;
1808 error = -ENOMEM;
1809 bdev = bd_acquire(inode);
1810 if (!bdev)
1811 goto fail;
1812 out:
1813 path_put(&path);
1814 return bdev;
1815 fail:
1816 bdev = ERR_PTR(error);
1817 goto out;
1818 }
1819 EXPORT_SYMBOL(lookup_bdev);
1820
1821 int __invalidate_device(struct block_device *bdev, bool kill_dirty)
1822 {
1823 struct super_block *sb = get_super(bdev);
1824 int res = 0;
1825
1826 if (sb) {
1827 /*
1828 * no need to lock the super, get_super holds the
1829 * read mutex so the filesystem cannot go away
1830 * under us (->put_super runs with the write lock
1831 * hold).
1832 */
1833 shrink_dcache_sb(sb);
1834 res = invalidate_inodes(sb, kill_dirty);
1835 drop_super(sb);
1836 }
1837 invalidate_bdev(bdev);
1838 return res;
1839 }
1840 EXPORT_SYMBOL(__invalidate_device);
1841
1842 void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
1843 {
1844 struct inode *inode, *old_inode = NULL;
1845
1846 spin_lock(&blockdev_superblock->s_inode_list_lock);
1847 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
1848 struct address_space *mapping = inode->i_mapping;
1849
1850 spin_lock(&inode->i_lock);
1851 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
1852 mapping->nrpages == 0) {
1853 spin_unlock(&inode->i_lock);
1854 continue;
1855 }
1856 __iget(inode);
1857 spin_unlock(&inode->i_lock);
1858 spin_unlock(&blockdev_superblock->s_inode_list_lock);
1859 /*
1860 * We hold a reference to 'inode' so it couldn't have been
1861 * removed from s_inodes list while we dropped the
1862 * s_inode_list_lock We cannot iput the inode now as we can
1863 * be holding the last reference and we cannot iput it under
1864 * s_inode_list_lock. So we keep the reference and iput it
1865 * later.
1866 */
1867 iput(old_inode);
1868 old_inode = inode;
1869
1870 func(I_BDEV(inode), arg);
1871
1872 spin_lock(&blockdev_superblock->s_inode_list_lock);
1873 }
1874 spin_unlock(&blockdev_superblock->s_inode_list_lock);
1875 iput(old_inode);
1876 }
This page took 0.117226 seconds and 5 git commands to generate.