Merge tag 'armsoc-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[deliverable/linux.git] / fs / block_dev.c
CommitLineData
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>
7db9cfd3 14#include <linux/device_cgroup.h>
1da177e4
LT
15#include <linux/highmem.h>
16#include <linux/blkdev.h>
66114cad 17#include <linux/backing-dev.h>
1da177e4
LT
18#include <linux/module.h>
19#include <linux/blkpg.h>
b502bd11 20#include <linux/magic.h>
1da177e4 21#include <linux/buffer_head.h>
ff01bb48 22#include <linux/swap.h>
585d3bc0 23#include <linux/pagevec.h>
811d736f 24#include <linux/writeback.h>
1da177e4
LT
25#include <linux/mpage.h>
26#include <linux/mount.h>
27#include <linux/uio.h>
28#include <linux/namei.h>
1368c4f2 29#include <linux/log2.h>
ff01bb48 30#include <linux/cleancache.h>
c94c2acf 31#include <linux/dax.h>
acc93d30 32#include <linux/badblocks.h>
1da177e4 33#include <asm/uaccess.h>
07f3f05c 34#include "internal.h"
1da177e4
LT
35
36struct bdev_inode {
37 struct block_device bdev;
38 struct inode vfs_inode;
39};
40
4c54ac62
AB
41static const struct address_space_operations def_blk_aops;
42
1da177e4
LT
43static inline struct bdev_inode *BDEV_I(struct inode *inode)
44{
45 return container_of(inode, struct bdev_inode, vfs_inode);
46}
47
ff5053f6 48struct block_device *I_BDEV(struct inode *inode)
1da177e4
LT
49{
50 return &BDEV_I(inode)->bdev;
51}
1da177e4
LT
52EXPORT_SYMBOL(I_BDEV);
53
dbd3ca50 54static void bdev_write_inode(struct block_device *bdev)
564f00f6 55{
dbd3ca50
VG
56 struct inode *inode = bdev->bd_inode;
57 int ret;
58
564f00f6
CH
59 spin_lock(&inode->i_lock);
60 while (inode->i_state & I_DIRTY) {
61 spin_unlock(&inode->i_lock);
dbd3ca50
VG
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 }
564f00f6
CH
69 spin_lock(&inode->i_lock);
70 }
71 spin_unlock(&inode->i_lock);
72}
73
f9a14399 74/* Kill _all_ buffers and pagecache , dirty or not.. */
ff01bb48 75void kill_bdev(struct block_device *bdev)
1da177e4 76{
ff01bb48
AV
77 struct address_space *mapping = bdev->bd_inode->i_mapping;
78
f9fe48be 79 if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
f9a14399 80 return;
ff01bb48 81
f9a14399 82 invalidate_bh_lrus();
ff01bb48 83 truncate_inode_pages(mapping, 0);
1da177e4 84}
ff01bb48
AV
85EXPORT_SYMBOL(kill_bdev);
86
87/* Invalidate clean unused buffers and pagecache. */
88void 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 */
3167760f 101 cleancache_invalidate_inode(mapping);
ff01bb48
AV
102}
103EXPORT_SYMBOL(invalidate_bdev);
1da177e4
LT
104
105int set_blocksize(struct block_device *bdev, int size)
106{
107 /* Size must be a power of two, and between 512 and PAGE_SIZE */
1368c4f2 108 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
1da177e4
LT
109 return -EINVAL;
110
111 /* Size cannot be smaller than the size supported by the device */
e1defc4f 112 if (size < bdev_logical_block_size(bdev))
1da177e4
LT
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
125EXPORT_SYMBOL(set_blocksize);
126
127int sb_set_blocksize(struct super_block *sb, int size)
128{
1da177e4
LT
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;
38885bd4 134 sb->s_blocksize_bits = blksize_bits(size);
1da177e4
LT
135 return sb->s_blocksize;
136}
137
138EXPORT_SYMBOL(sb_set_blocksize);
139
140int sb_min_blocksize(struct super_block *sb, int size)
141{
e1defc4f 142 int minsize = bdev_logical_block_size(sb->s_bdev);
1da177e4
LT
143 if (size < minsize)
144 size = minsize;
145 return sb_set_blocksize(sb, size);
146}
147
148EXPORT_SYMBOL(sb_min_blocksize);
149
150static int
151blkdev_get_block(struct inode *inode, sector_t iblock,
152 struct buffer_head *bh, int create)
153{
1da177e4
LT
154 bh->b_bdev = I_BDEV(inode);
155 bh->b_blocknr = iblock;
156 set_buffer_mapped(bh);
157 return 0;
158}
159
4ebb16ca
DW
160static struct inode *bdev_file_inode(struct file *file)
161{
162 return file->f_mapping->host;
163}
164
b2e895db 165static ssize_t
c8b8e32d 166blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
b2e895db
AM
167{
168 struct file *file = iocb->ki_filp;
4ebb16ca 169 struct inode *inode = bdev_file_inode(file);
b2e895db 170
bbab37dd 171 if (IS_DAX(inode))
c8b8e32d 172 return dax_do_io(iocb, inode, iter, blkdev_get_block,
bbab37dd 173 NULL, DIO_SKIP_DIO_COUNT);
c8b8e32d 174 return __blockdev_direct_IO(iocb, inode, I_BDEV(inode), iter,
fe0f07d0
JA
175 blkdev_get_block, NULL, NULL,
176 DIO_SKIP_DIO_COUNT);
b2e895db
AM
177}
178
5cee5815
JK
179int __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
585d3bc0
NP
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 */
192int sync_blockdev(struct block_device *bdev)
193{
5cee5815 194 return __sync_blockdev(bdev, 1);
585d3bc0
NP
195}
196EXPORT_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 */
203int fsync_bdev(struct block_device *bdev)
204{
205 struct super_block *sb = get_super(bdev);
206 if (sb) {
60b0680f 207 int res = sync_filesystem(sb);
585d3bc0
NP
208 drop_super(sb);
209 return res;
210 }
211 return sync_blockdev(bdev);
212}
47e4491b 213EXPORT_SYMBOL(fsync_bdev);
585d3bc0
NP
214
215/**
216 * freeze_bdev -- lock a filesystem and force it into a consistent state
217 * @bdev: blockdevice to lock
218 *
585d3bc0
NP
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 */
227struct 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);
4504230a
CH
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 */
585d3bc0 239 sb = get_super(bdev);
4504230a
CH
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;
48b6bca6
BM
248 if (sb->s_op->freeze_super)
249 error = sb->s_op->freeze_super(sb);
250 else
251 error = freeze_super(sb);
18e9e510
JB
252 if (error) {
253 deactivate_super(sb);
254 bdev->bd_fsfreeze_count--;
585d3bc0 255 mutex_unlock(&bdev->bd_fsfreeze_mutex);
18e9e510 256 return ERR_PTR(error);
585d3bc0 257 }
18e9e510 258 deactivate_super(sb);
4504230a 259 out:
585d3bc0
NP
260 sync_blockdev(bdev);
261 mutex_unlock(&bdev->bd_fsfreeze_mutex);
4fadd7bb 262 return sb; /* thaw_bdev releases s->s_umount */
585d3bc0
NP
263}
264EXPORT_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 */
273int thaw_bdev(struct block_device *bdev, struct super_block *sb)
274{
4504230a 275 int error = -EINVAL;
585d3bc0
NP
276
277 mutex_lock(&bdev->bd_fsfreeze_mutex);
4504230a 278 if (!bdev->bd_fsfreeze_count)
18e9e510 279 goto out;
4504230a
CH
280
281 error = 0;
282 if (--bdev->bd_fsfreeze_count > 0)
18e9e510 283 goto out;
4504230a
CH
284
285 if (!sb)
18e9e510 286 goto out;
4504230a 287
48b6bca6
BM
288 if (sb->s_op->thaw_super)
289 error = sb->s_op->thaw_super(sb);
290 else
291 error = thaw_super(sb);
18e9e510
JB
292 if (error) {
293 bdev->bd_fsfreeze_count++;
294 mutex_unlock(&bdev->bd_fsfreeze_mutex);
295 return error;
296 }
297out:
585d3bc0
NP
298 mutex_unlock(&bdev->bd_fsfreeze_mutex);
299 return 0;
300}
301EXPORT_SYMBOL(thaw_bdev);
302
1da177e4
LT
303static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
304{
305 return block_write_full_page(page, blkdev_get_block, wbc);
306}
307
308static int blkdev_readpage(struct file * file, struct page * page)
309{
310 return block_read_full_page(page, blkdev_get_block);
311}
312
447f05bb
AM
313static 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
6272b5a5
NP
319static 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)
1da177e4 322{
155130a4
CH
323 return block_write_begin(mapping, pos, len, flags, pagep,
324 blkdev_get_block);
1da177e4
LT
325}
326
6272b5a5
NP
327static 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)
1da177e4 330{
6272b5a5
NP
331 int ret;
332 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
333
334 unlock_page(page);
09cbfeaf 335 put_page(page);
6272b5a5
NP
336
337 return ret;
1da177e4
LT
338}
339
340/*
341 * private llseek:
496ad9aa 342 * for a block special file file_inode(file)->i_size is zero
1da177e4
LT
343 * so we compute the size by hand (just as in block_read/write above)
344 */
965c8e59 345static loff_t block_llseek(struct file *file, loff_t offset, int whence)
1da177e4 346{
4ebb16ca 347 struct inode *bd_inode = bdev_file_inode(file);
1da177e4
LT
348 loff_t retval;
349
5955102c 350 inode_lock(bd_inode);
5d48f3a2 351 retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
5955102c 352 inode_unlock(bd_inode);
1da177e4
LT
353 return retval;
354}
355
02c24a82 356int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
1da177e4 357{
4ebb16ca 358 struct inode *bd_inode = bdev_file_inode(filp);
b8af67e2 359 struct block_device *bdev = I_BDEV(bd_inode);
ab0a9735 360 int error;
da5aa861
RW
361
362 error = filemap_write_and_wait_range(filp->f_mapping, start, end);
363 if (error)
364 return error;
ab0a9735 365
b8af67e2
AB
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 */
dd3932ed 371 error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
ab0a9735
CH
372 if (error == -EOPNOTSUPP)
373 error = 0;
b8af67e2 374
ab0a9735 375 return error;
1da177e4 376}
b1dd3b28 377EXPORT_SYMBOL(blkdev_fsync);
1da177e4 378
47a191fd
MW
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 */
395int 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;
2e6edc95
DW
399 int result = -EOPNOTSUPP;
400
f68eb1e7 401 if (!ops->rw_page || bdev_get_integrity(bdev))
2e6edc95
DW
402 return result;
403
6f3b0e8b 404 result = blk_queue_enter(bdev->bd_queue, false);
2e6edc95
DW
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;
47a191fd
MW
410}
411EXPORT_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 */
432int 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;
2e6edc95 438
f68eb1e7 439 if (!ops->rw_page || bdev_get_integrity(bdev))
47a191fd 440 return -EOPNOTSUPP;
6f3b0e8b 441 result = blk_queue_enter(bdev->bd_queue, false);
2e6edc95
DW
442 if (result)
443 return result;
444
47a191fd
MW
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);
2e6edc95 451 blk_queue_exit(bdev->bd_queue);
47a191fd
MW
452 return result;
453}
454EXPORT_SYMBOL_GPL(bdev_write_page);
455
dd22f551
MW
456/**
457 * bdev_direct_access() - Get the address for directly-accessibly memory
458 * @bdev: The device containing the memory
b2e0d162 459 * @dax: control and output parameters for ->direct_access
dd22f551
MW
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 */
b2e0d162 470long bdev_direct_access(struct block_device *bdev, struct blk_dax_ctl *dax)
dd22f551 471{
b2e0d162
DW
472 sector_t sector = dax->sector;
473 long avail, size = dax->size;
dd22f551
MW
474 const struct block_device_operations *ops = bdev->bd_disk->fops;
475
43c3dd08
MW
476 /*
477 * The device driver is allowed to sleep, in order to make the
478 * memory directly accessible.
479 */
480 might_sleep();
481
dd22f551
MW
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;
b2e0d162 492 avail = ops->direct_access(bdev, sector, &dax->addr, &dax->pfn);
dd22f551
MW
493 if (!avail)
494 return -ERANGE;
fe683ada
DW
495 if (avail > 0 && avail & ~PAGE_MASK)
496 return -ENXIO;
dd22f551
MW
497 return min(avail, size);
498}
499EXPORT_SYMBOL_GPL(bdev_direct_access);
500
1da177e4
LT
501/*
502 * pseudo-fs
503 */
504
505static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
e18b890b 506static struct kmem_cache * bdev_cachep __read_mostly;
1da177e4
LT
507
508static struct inode *bdev_alloc_inode(struct super_block *sb)
509{
e94b1766 510 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
1da177e4
LT
511 if (!ei)
512 return NULL;
513 return &ei->vfs_inode;
514}
515
fa0d7e3d 516static void bdev_i_callback(struct rcu_head *head)
1da177e4 517{
fa0d7e3d 518 struct inode *inode = container_of(head, struct inode, i_rcu);
1da177e4
LT
519 struct bdev_inode *bdi = BDEV_I(inode);
520
1da177e4
LT
521 kmem_cache_free(bdev_cachep, bdi);
522}
523
fa0d7e3d
NP
524static void bdev_destroy_inode(struct inode *inode)
525{
526 call_rcu(&inode->i_rcu, bdev_i_callback);
527}
528
51cc5068 529static void init_once(void *foo)
1da177e4
LT
530{
531 struct bdev_inode *ei = (struct bdev_inode *) foo;
532 struct block_device *bdev = &ei->bdev;
533
a35afb83
CL
534 memset(bdev, 0, sizeof(*bdev));
535 mutex_init(&bdev->bd_mutex);
a35afb83
CL
536 INIT_LIST_HEAD(&bdev->bd_inodes);
537 INIT_LIST_HEAD(&bdev->bd_list);
49731baa
TH
538#ifdef CONFIG_SYSFS
539 INIT_LIST_HEAD(&bdev->bd_holder_disks);
540#endif
a35afb83 541 inode_init_once(&ei->vfs_inode);
fcccf502
TS
542 /* Initialize mutex for freeze. */
543 mutex_init(&bdev->bd_fsfreeze_mutex);
1da177e4
LT
544}
545
546static 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
b57922d9 553static void bdev_evict_inode(struct inode *inode)
1da177e4
LT
554{
555 struct block_device *bdev = &BDEV_I(inode)->bdev;
556 struct list_head *p;
91b0abe3 557 truncate_inode_pages_final(&inode->i_data);
b57922d9 558 invalidate_inode_buffers(inode); /* is it needed here? */
dbd5768f 559 clear_inode(inode);
1da177e4
LT
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
ee9b6d61 568static const struct super_operations bdev_sops = {
1da177e4
LT
569 .statfs = simple_statfs,
570 .alloc_inode = bdev_alloc_inode,
571 .destroy_inode = bdev_destroy_inode,
572 .drop_inode = generic_delete_inode,
b57922d9 573 .evict_inode = bdev_evict_inode,
1da177e4
LT
574};
575
51139ada
AV
576static struct dentry *bd_mount(struct file_system_type *fs_type,
577 int flags, const char *dev_name, void *data)
1da177e4 578{
3684aa70
SL
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;
1da177e4
LT
584}
585
586static struct file_system_type bd_type = {
587 .name = "bdev",
51139ada 588 .mount = bd_mount,
1da177e4
LT
589 .kill_sb = kill_anon_super,
590};
591
a212b105
TH
592struct super_block *blockdev_superblock __read_mostly;
593EXPORT_SYMBOL_GPL(blockdev_superblock);
1da177e4
LT
594
595void __init bdev_cache_init(void)
596{
597 int err;
ace8577a 598 static struct vfsmount *bd_mnt;
c2acf7b9 599
1da177e4 600 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
fffb60f9 601 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
5d097056 602 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
20c2df83 603 init_once);
1da177e4
LT
604 err = register_filesystem(&bd_type);
605 if (err)
606 panic("Cannot register bdev pseudo-fs");
607 bd_mnt = kern_mount(&bd_type);
1da177e4
LT
608 if (IS_ERR(bd_mnt))
609 panic("Cannot create bdev pseudo-fs");
ace8577a 610 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
1da177e4
LT
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 */
618static inline unsigned long hash(dev_t dev)
619{
620 return MAJOR(dev)+MINOR(dev);
621}
622
623static int bdev_test(struct inode *inode, void *data)
624{
625 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
626}
627
628static int bdev_set(struct inode *inode, void *data)
629{
630 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
631 return 0;
632}
633
634static LIST_HEAD(all_bdevs);
635
636struct block_device *bdget(dev_t dev)
637{
638 struct block_device *bdev;
639 struct inode *inode;
640
c2acf7b9 641 inode = iget5_locked(blockdev_superblock, hash(dev),
1da177e4
LT
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;
782b94cd 651 bdev->bd_super = NULL;
1da177e4
LT
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);
1da177e4
LT
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
669EXPORT_SYMBOL(bdget);
670
dddac6a7
AJ
671/**
672 * bdgrab -- Grab a reference to an already referenced block device
673 * @bdev: Block device to grab a reference to.
674 */
675struct block_device *bdgrab(struct block_device *bdev)
676{
7de9c6ee 677 ihold(bdev->bd_inode);
dddac6a7
AJ
678 return bdev;
679}
c1681bf8 680EXPORT_SYMBOL(bdgrab);
dddac6a7 681
1da177e4
LT
682long nr_blockdev_pages(void)
683{
203a2935 684 struct block_device *bdev;
1da177e4
LT
685 long ret = 0;
686 spin_lock(&bdev_lock);
203a2935 687 list_for_each_entry(bdev, &all_bdevs, bd_list) {
1da177e4
LT
688 ret += bdev->bd_inode->i_mapping->nrpages;
689 }
690 spin_unlock(&bdev_lock);
691 return ret;
692}
693
694void bdput(struct block_device *bdev)
695{
696 iput(bdev->bd_inode);
697}
698
699EXPORT_SYMBOL(bdput);
700
701static struct block_device *bd_acquire(struct inode *inode)
702{
703 struct block_device *bdev;
09d967c6 704
1da177e4
LT
705 spin_lock(&bdev_lock);
706 bdev = inode->i_bdev;
09d967c6 707 if (bdev) {
ed8a9d2c 708 bdgrab(bdev);
1da177e4
LT
709 spin_unlock(&bdev_lock);
710 return bdev;
711 }
712 spin_unlock(&bdev_lock);
09d967c6 713
1da177e4
LT
714 bdev = bdget(inode->i_rdev);
715 if (bdev) {
716 spin_lock(&bdev_lock);
09d967c6
OH
717 if (!inode->i_bdev) {
718 /*
7de9c6ee 719 * We take an additional reference to bd_inode,
09d967c6
OH
720 * and it's released in clear_inode() of inode.
721 * So, we can access it via ->i_mapping always
722 * without igrab().
723 */
ed8a9d2c 724 bdgrab(bdev);
09d967c6
OH
725 inode->i_bdev = bdev;
726 inode->i_mapping = bdev->bd_inode->i_mapping;
727 list_add(&inode->i_devices, &bdev->bd_inodes);
728 }
1da177e4
LT
729 spin_unlock(&bdev_lock);
730 }
731 return bdev;
732}
733
734/* Call when you free inode */
735
736void bd_forget(struct inode *inode)
737{
09d967c6
OH
738 struct block_device *bdev = NULL;
739
1da177e4 740 spin_lock(&bdev_lock);
b4ea2eaa
YH
741 if (!sb_is_blkdev_sb(inode->i_sb))
742 bdev = inode->i_bdev;
743 __bd_forget(inode);
1da177e4 744 spin_unlock(&bdev_lock);
09d967c6
OH
745
746 if (bdev)
ed8a9d2c 747 bdput(bdev);
1da177e4
LT
748}
749
1a3cbbc5
TH
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 *
25985edc 756 * Test whether @bdev can be claimed by @holder.
1a3cbbc5
TH
757 *
758 * CONTEXT:
759 * spin_lock(&bdev_lock).
760 *
761 * RETURNS:
762 * %true if @bdev can be claimed, %false otherwise.
763 */
764static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
765 void *holder)
1da177e4 766{
1da177e4 767 if (bdev->bd_holder == holder)
1a3cbbc5 768 return true; /* already a holder */
1da177e4 769 else if (bdev->bd_holder != NULL)
1a3cbbc5 770 return false; /* held by someone else */
1da177e4 771 else if (bdev->bd_contains == bdev)
1a3cbbc5 772 return true; /* is a whole device which isn't held */
1da177e4 773
e525fd89 774 else if (whole->bd_holder == bd_may_claim)
1a3cbbc5
TH
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 */
1da177e4 778 else
1a3cbbc5
TH
779 return true; /* is a partition of an un-held device */
780}
781
6b4517a7
TH
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 */
800static int bd_prepare_to_claim(struct block_device *bdev,
801 struct block_device *whole, void *holder)
802{
803retry:
804 /* if someone else claimed, fail */
805 if (!bd_may_claim(bdev, whole, holder))
806 return -EBUSY;
807
e75aa858
TH
808 /* if claiming is already in progress, wait for it to finish */
809 if (whole->bd_claiming) {
6b4517a7
TH
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
b0018361
NP
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.
6b4517a7
TH
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 */
848static 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
d4c208b8
TH
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
cf342570 878 module_put(disk->fops->owner);
6b4517a7
TH
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
641dc636 898#ifdef CONFIG_SYSFS
49731baa
TH
899struct bd_holder_disk {
900 struct list_head list;
901 struct gendisk *disk;
902 int refcnt;
903};
904
905static 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
4d7dd8fd 916static int add_symlink(struct kobject *from, struct kobject *to)
641dc636 917{
4d7dd8fd 918 return sysfs_create_link(from, to, kobject_name(to));
641dc636
JN
919}
920
921static void del_symlink(struct kobject *from, struct kobject *to)
922{
641dc636
JN
923 sysfs_remove_link(from, kobject_name(to));
924}
925
df6c0cd9 926/**
e09b457b
TH
927 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
928 * @bdev: the claimed slave bdev
929 * @disk: the holding disk
df6c0cd9 930 *
49731baa
TH
931 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
932 *
e09b457b 933 * This functions creates the following sysfs symlinks.
641dc636 934 *
e09b457b
TH
935 * - from "slaves" directory of the holder @disk to the claimed @bdev
936 * - from "holders" directory of the @bdev to the holder @disk
641dc636 937 *
e09b457b
TH
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:
641dc636 940 *
e09b457b
TH
941 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
942 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
641dc636 943 *
e09b457b
TH
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.
641dc636 947 *
e09b457b
TH
948 * CONTEXT:
949 * Might sleep.
641dc636 950 *
e09b457b
TH
951 * RETURNS:
952 * 0 on success, -errno on failure.
641dc636 953 */
e09b457b 954int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
641dc636 955{
49731baa 956 struct bd_holder_disk *holder;
e09b457b 957 int ret = 0;
641dc636 958
2e7b651d 959 mutex_lock(&bdev->bd_mutex);
df6c0cd9 960
49731baa 961 WARN_ON_ONCE(!bdev->bd_holder);
4e91672c 962
e09b457b
TH
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;
4e91672c 966
49731baa
TH
967 holder = bd_find_holder_disk(bdev, disk);
968 if (holder) {
969 holder->refcnt++;
e09b457b 970 goto out_unlock;
49731baa 971 }
641dc636 972
49731baa
TH
973 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
974 if (!holder) {
975 ret = -ENOMEM;
e09b457b
TH
976 goto out_unlock;
977 }
641dc636 978
49731baa
TH
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;
e7407d16
TH
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);
49731baa
TH
995
996 list_add(&holder->list, &bdev->bd_holder_disks);
997 goto out_unlock;
998
999out_del:
1000 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1001out_free:
1002 kfree(holder);
e09b457b 1003out_unlock:
b4cf1b72 1004 mutex_unlock(&bdev->bd_mutex);
e09b457b 1005 return ret;
641dc636 1006}
e09b457b 1007EXPORT_SYMBOL_GPL(bd_link_disk_holder);
641dc636 1008
49731baa
TH
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 */
1019void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
641dc636 1020{
49731baa 1021 struct bd_holder_disk *holder;
641dc636 1022
49731baa 1023 mutex_lock(&bdev->bd_mutex);
641dc636 1024
49731baa
TH
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);
e7407d16 1031 kobject_put(bdev->bd_part->holder_dir);
49731baa
TH
1032 list_del_init(&holder->list);
1033 kfree(holder);
1034 }
1035
1036 mutex_unlock(&bdev->bd_mutex);
1da177e4 1037}
49731baa 1038EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
641dc636 1039#endif
1da177e4 1040
56ade44b
AP
1041/**
1042 * flush_disk - invalidates all buffer-cache entries on a disk
1043 *
1044 * @bdev: struct block device to be flushed
e6eb5ce1 1045 * @kill_dirty: flag to guide handling of dirty inodes
56ade44b
AP
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 */
93b270f7 1051static void flush_disk(struct block_device *bdev, bool kill_dirty)
56ade44b 1052{
93b270f7 1053 if (__invalidate_device(bdev, kill_dirty)) {
56ade44b 1054 printk(KERN_WARNING "VFS: busy inodes on changed media or "
424081f3
DM
1055 "resized disk %s\n",
1056 bdev->bd_disk ? bdev->bd_disk->disk_name : "");
56ade44b
AP
1057 }
1058
1059 if (!bdev->bd_disk)
1060 return;
d27769ec 1061 if (disk_part_scan_enabled(bdev->bd_disk))
56ade44b
AP
1062 bdev->bd_invalidated = 1;
1063}
1064
c3279d14 1065/**
57d1b536 1066 * check_disk_size_change - checks for disk size change and adjusts bdev size.
c3279d14
AP
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 */
1073void 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) {
c3279d14
AP
1080 printk(KERN_INFO
1081 "%s: detected capacity change from %lld to %lld\n",
424081f3 1082 disk->disk_name, bdev_size, disk_size);
c3279d14 1083 i_size_write(bdev->bd_inode, disk_size);
93b270f7 1084 flush_disk(bdev, false);
c3279d14
AP
1085 }
1086}
1087EXPORT_SYMBOL(check_disk_size_change);
1088
0c002c2f 1089/**
57d1b536 1090 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
0c002c2f
AP
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 */
1097int revalidate_disk(struct gendisk *disk)
1098{
c3279d14 1099 struct block_device *bdev;
0c002c2f
AP
1100 int ret = 0;
1101
1102 if (disk->fops->revalidate_disk)
1103 ret = disk->fops->revalidate_disk(disk);
25520d55 1104 blk_integrity_revalidate(disk);
c3279d14
AP
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);
7630b661 1111 bdev->bd_invalidated = 0;
c3279d14
AP
1112 mutex_unlock(&bdev->bd_mutex);
1113 bdput(bdev);
0c002c2f
AP
1114 return ret;
1115}
1116EXPORT_SYMBOL(revalidate_disk);
1117
1da177e4
LT
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 */
1127int check_disk_change(struct block_device *bdev)
1128{
1129 struct gendisk *disk = bdev->bd_disk;
83d5cde4 1130 const struct block_device_operations *bdops = disk->fops;
77ea887e 1131 unsigned int events;
1da177e4 1132
77ea887e
TH
1133 events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
1134 DISK_EVENT_EJECT_REQUEST);
1135 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1da177e4
LT
1136 return 0;
1137
93b270f7 1138 flush_disk(bdev, true);
1da177e4
LT
1139 if (bdops->revalidate_disk)
1140 bdops->revalidate_disk(bdev->bd_disk);
1da177e4
LT
1141 return 1;
1142}
1143
1144EXPORT_SYMBOL(check_disk_change);
1145
1146void bd_set_size(struct block_device *bdev, loff_t size)
1147{
e1defc4f 1148 unsigned bsize = bdev_logical_block_size(bdev);
1da177e4 1149
5955102c 1150 inode_lock(bdev->bd_inode);
d646a02a 1151 i_size_write(bdev->bd_inode, size);
5955102c 1152 inode_unlock(bdev->bd_inode);
09cbfeaf 1153 while (bsize < PAGE_SIZE) {
1da177e4
LT
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}
1161EXPORT_SYMBOL(bd_set_size);
1162
acc93d30
DW
1163static 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
4385bab1 1190static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
37be4124 1191
6d740cd5
PZ
1192/*
1193 * bd_mutex locking:
1194 *
1195 * mutex_lock(part->bd_mutex)
1196 * mutex_lock_nested(whole->bd_mutex, 1)
1197 */
1198
572c4892 1199static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
1da177e4 1200{
1da177e4 1201 struct gendisk *disk;
523e1d39 1202 struct module *owner;
7db9cfd3 1203 int ret;
cf771cb5 1204 int partno;
fe6e9c1f
AV
1205 int perm = 0;
1206
572c4892 1207 if (mode & FMODE_READ)
fe6e9c1f 1208 perm |= MAY_READ;
572c4892 1209 if (mode & FMODE_WRITE)
fe6e9c1f
AV
1210 perm |= MAY_WRITE;
1211 /*
1212 * hooks: /n/, see "layering violations".
1213 */
b7300b78
CW
1214 if (!for_part) {
1215 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1216 if (ret != 0) {
1217 bdput(bdev);
1218 return ret;
1219 }
82666020 1220 }
7db9cfd3 1221
d3374825 1222 restart:
0762b8bd 1223
89f97496 1224 ret = -ENXIO;
cf771cb5 1225 disk = get_gendisk(bdev->bd_dev, &partno);
0762b8bd 1226 if (!disk)
6e9624b8 1227 goto out;
523e1d39 1228 owner = disk->fops->owner;
1da177e4 1229
69e02c59 1230 disk_block_events(disk);
6796bf54 1231 mutex_lock_nested(&bdev->bd_mutex, for_part);
1da177e4
LT
1232 if (!bdev->bd_openers) {
1233 bdev->bd_disk = disk;
87192a2a 1234 bdev->bd_queue = disk->queue;
1da177e4 1235 bdev->bd_contains = bdev;
03cdadb0
DW
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
cf771cb5 1241 if (!partno) {
89f97496
TH
1242 ret = -ENXIO;
1243 bdev->bd_part = disk_get_part(disk, partno);
1244 if (!bdev->bd_part)
1245 goto out_clear;
1246
1196f8b8 1247 ret = 0;
1da177e4 1248 if (disk->fops->open) {
572c4892 1249 ret = disk->fops->open(bdev, mode);
d3374825
N
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;
d3374825 1257 bdev->bd_disk = NULL;
87192a2a 1258 bdev->bd_queue = NULL;
d3374825 1259 mutex_unlock(&bdev->bd_mutex);
69e02c59 1260 disk_unblock_events(disk);
69e02c59 1261 put_disk(disk);
523e1d39 1262 module_put(owner);
d3374825
N
1263 goto restart;
1264 }
1da177e4 1265 }
7e69723f 1266
5a023cdb 1267 if (!ret) {
7e69723f 1268 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
5a023cdb
DW
1269 if (!blkdev_dax_capable(bdev))
1270 bdev->bd_inode->i_flags &= ~S_DAX;
1271 }
7e69723f 1272
1196f8b8
TH
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 */
fe316bf2
JN
1279 if (bdev->bd_invalidated) {
1280 if (!ret)
1281 rescan_partitions(disk, bdev);
1282 else if (ret == -ENOMEDIUM)
1283 invalidate_partitions(disk, bdev);
1284 }
5a023cdb 1285
1196f8b8
TH
1286 if (ret)
1287 goto out_clear;
1da177e4 1288 } else {
1da177e4
LT
1289 struct block_device *whole;
1290 whole = bdget_disk(disk, 0);
1291 ret = -ENOMEM;
1292 if (!whole)
0762b8bd 1293 goto out_clear;
37be4124 1294 BUG_ON(for_part);
572c4892 1295 ret = __blkdev_get(whole, mode, 1);
1da177e4 1296 if (ret)
0762b8bd 1297 goto out_clear;
1da177e4 1298 bdev->bd_contains = whole;
89f97496 1299 bdev->bd_part = disk_get_part(disk, partno);
e71bf0d0 1300 if (!(disk->flags & GENHD_FL_UP) ||
89f97496 1301 !bdev->bd_part || !bdev->bd_part->nr_sects) {
1da177e4 1302 ret = -ENXIO;
0762b8bd 1303 goto out_clear;
1da177e4 1304 }
89f97496 1305 bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
5a023cdb 1306 if (!blkdev_dax_capable(bdev))
f0b2e563 1307 bdev->bd_inode->i_flags &= ~S_DAX;
1da177e4
LT
1308 }
1309 } else {
1da177e4 1310 if (bdev->bd_contains == bdev) {
1196f8b8
TH
1311 ret = 0;
1312 if (bdev->bd_disk->fops->open)
572c4892 1313 ret = bdev->bd_disk->fops->open(bdev, mode);
1196f8b8 1314 /* the same as first opener case, read comment there */
fe316bf2
JN
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 }
1196f8b8
TH
1321 if (ret)
1322 goto out_unlock_bdev;
1da177e4 1323 }
69e02c59 1324 /* only one opener holds refs to the module and disk */
69e02c59 1325 put_disk(disk);
523e1d39 1326 module_put(owner);
1da177e4
LT
1327 }
1328 bdev->bd_openers++;
37be4124
N
1329 if (for_part)
1330 bdev->bd_part_count++;
c039e313 1331 mutex_unlock(&bdev->bd_mutex);
69e02c59 1332 disk_unblock_events(disk);
1da177e4
LT
1333 return 0;
1334
0762b8bd 1335 out_clear:
89f97496 1336 disk_put_part(bdev->bd_part);
1da177e4 1337 bdev->bd_disk = NULL;
0762b8bd 1338 bdev->bd_part = NULL;
87192a2a 1339 bdev->bd_queue = NULL;
1da177e4 1340 if (bdev != bdev->bd_contains)
572c4892 1341 __blkdev_put(bdev->bd_contains, mode, 1);
1da177e4 1342 bdev->bd_contains = NULL;
0762b8bd 1343 out_unlock_bdev:
c039e313 1344 mutex_unlock(&bdev->bd_mutex);
69e02c59 1345 disk_unblock_events(disk);
0762b8bd 1346 put_disk(disk);
523e1d39 1347 module_put(owner);
4345caba 1348 out:
0762b8bd
TH
1349 bdput(bdev);
1350
1da177e4
LT
1351 return ret;
1352}
1353
d4d77629
TH
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 */
e525fd89 1373int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
1da177e4 1374{
e525fd89
TH
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) {
d4dc210f
TH
1391 struct gendisk *disk = whole->bd_disk;
1392
6a027eff 1393 /* finish claiming */
77ea887e 1394 mutex_lock(&bdev->bd_mutex);
6a027eff
TH
1395 spin_lock(&bdev_lock);
1396
77ea887e 1397 if (!res) {
6a027eff
TH
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);
77ea887e
TH
1417
1418 /*
d4dc210f
TH
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().
77ea887e 1424 */
4c49ff3f
TH
1425 if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder &&
1426 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
77ea887e 1427 bdev->bd_write_holder = true;
d4dc210f 1428 disk_block_events(disk);
77ea887e
TH
1429 }
1430
1431 mutex_unlock(&bdev->bd_mutex);
6a027eff 1432 bdput(whole);
e525fd89
TH
1433 }
1434
1435 return res;
37be4124 1436}
1da177e4
LT
1437EXPORT_SYMBOL(blkdev_get);
1438
d4d77629
TH
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 */
1456struct 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
e51900f7
CE
1470 if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
1471 blkdev_put(bdev, mode);
1472 return ERR_PTR(-EACCES);
1473 }
1474
d4d77629
TH
1475 return bdev;
1476}
1477EXPORT_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 */
1501struct 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}
1516EXPORT_SYMBOL(blkdev_get_by_dev);
1517
1da177e4
LT
1518static int blkdev_open(struct inode * inode, struct file * filp)
1519{
1520 struct block_device *bdev;
1da177e4
LT
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
572c4892
AV
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
1da177e4 1537 bdev = bd_acquire(inode);
6a2aae06
PE
1538 if (bdev == NULL)
1539 return -ENOMEM;
1da177e4 1540
572c4892
AV
1541 filp->f_mapping = bdev->bd_inode->i_mapping;
1542
e525fd89 1543 return blkdev_get(bdev, filp->f_mode, filp);
1da177e4
LT
1544}
1545
4385bab1 1546static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
2e7b651d 1547{
2e7b651d 1548 struct gendisk *disk = bdev->bd_disk;
37be4124 1549 struct block_device *victim = NULL;
2e7b651d 1550
6796bf54 1551 mutex_lock_nested(&bdev->bd_mutex, for_part);
37be4124
N
1552 if (for_part)
1553 bdev->bd_part_count--;
1554
2e7b651d 1555 if (!--bdev->bd_openers) {
6a027eff 1556 WARN_ON_ONCE(bdev->bd_holders);
2e7b651d
PZ
1557 sync_blockdev(bdev);
1558 kill_bdev(bdev);
43d1c0eb
ID
1559
1560 bdev_write_inode(bdev);
564f00f6 1561 /*
43d1c0eb
ID
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.
94007751 1565 */
43d1c0eb 1566 inode_detach_wb(bdev->bd_inode);
2e7b651d
PZ
1567 }
1568 if (bdev->bd_contains == bdev) {
1569 if (disk->fops->release)
db2a144b 1570 disk->fops->release(disk, mode);
2e7b651d
PZ
1571 }
1572 if (!bdev->bd_openers) {
1573 struct module *owner = disk->fops->owner;
1574
0762b8bd
TH
1575 disk_put_part(bdev->bd_part);
1576 bdev->bd_part = NULL;
2e7b651d 1577 bdev->bd_disk = NULL;
37be4124
N
1578 if (bdev != bdev->bd_contains)
1579 victim = bdev->bd_contains;
2e7b651d 1580 bdev->bd_contains = NULL;
523e1d39
TH
1581
1582 put_disk(disk);
1583 module_put(owner);
2e7b651d 1584 }
2e7b651d
PZ
1585 mutex_unlock(&bdev->bd_mutex);
1586 bdput(bdev);
37be4124 1587 if (victim)
9a1c3542 1588 __blkdev_put(victim, mode, 1);
2e7b651d
PZ
1589}
1590
4385bab1 1591void blkdev_put(struct block_device *bdev, fmode_t mode)
37be4124 1592{
85ef06d1
TH
1593 mutex_lock(&bdev->bd_mutex);
1594
e525fd89 1595 if (mode & FMODE_EXCL) {
6a027eff
TH
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 */
6a027eff
TH
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
77ea887e
TH
1616 /*
1617 * If this was the last claim, remove holder link and
1618 * unblock evpoll if it was a write holder.
1619 */
85ef06d1
TH
1620 if (bdev_free && bdev->bd_write_holder) {
1621 disk_unblock_events(bdev->bd_disk);
1622 bdev->bd_write_holder = false;
77ea887e 1623 }
6936217c 1624 }
77ea887e 1625
85ef06d1
TH
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
4385bab1 1635 __blkdev_put(bdev, mode, 0);
37be4124 1636}
2e7b651d
PZ
1637EXPORT_SYMBOL(blkdev_put);
1638
1da177e4
LT
1639static int blkdev_close(struct inode * inode, struct file * filp)
1640{
4ebb16ca 1641 struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
4385bab1
AV
1642 blkdev_put(bdev, filp->f_mode);
1643 return 0;
1da177e4
LT
1644}
1645
bb93e3a5 1646static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1da177e4 1647{
4ebb16ca 1648 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
56b26add 1649 fmode_t mode = file->f_mode;
fd4ce1ac
CH
1650
1651 /*
1652 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1653 * to updated it before every ioctl.
1654 */
56b26add 1655 if (file->f_flags & O_NDELAY)
fd4ce1ac
CH
1656 mode |= FMODE_NDELAY;
1657 else
1658 mode &= ~FMODE_NDELAY;
1659
56b26add 1660 return blkdev_ioctl(bdev, mode, cmd, arg);
1da177e4
LT
1661}
1662
eef99380
CH
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 */
1456c0a8 1670ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
eef99380
CH
1671{
1672 struct file *file = iocb->ki_filp;
4ebb16ca 1673 struct inode *bd_inode = bdev_file_inode(file);
7ec7b94a 1674 loff_t size = i_size_read(bd_inode);
53362a05 1675 struct blk_plug plug;
eef99380 1676 ssize_t ret;
5f380c7f 1677
7ec7b94a
AV
1678 if (bdev_read_only(I_BDEV(bd_inode)))
1679 return -EPERM;
5f380c7f 1680
7ec7b94a 1681 if (!iov_iter_count(from))
5f380c7f
AV
1682 return 0;
1683
7ec7b94a
AV
1684 if (iocb->ki_pos >= size)
1685 return -ENOSPC;
1686
1687 iov_iter_truncate(from, size - iocb->ki_pos);
eef99380 1688
53362a05 1689 blk_start_plug(&plug);
1456c0a8 1690 ret = __generic_file_write_iter(iocb, from);
e2592217
CH
1691 if (ret > 0)
1692 ret = generic_write_sync(iocb, ret);
53362a05 1693 blk_finish_plug(&plug);
eef99380
CH
1694 return ret;
1695}
1456c0a8 1696EXPORT_SYMBOL_GPL(blkdev_write_iter);
eef99380 1697
b2de525f 1698ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
684c9aae
LT
1699{
1700 struct file *file = iocb->ki_filp;
4ebb16ca 1701 struct inode *bd_inode = bdev_file_inode(file);
684c9aae 1702 loff_t size = i_size_read(bd_inode);
a886038b 1703 loff_t pos = iocb->ki_pos;
684c9aae
LT
1704
1705 if (pos >= size)
1706 return 0;
1707
1708 size -= pos;
a886038b
AV
1709 iov_iter_truncate(to, size);
1710 return generic_file_read_iter(iocb, to);
684c9aae 1711}
b2de525f 1712EXPORT_SYMBOL_GPL(blkdev_read_iter);
684c9aae 1713
87d8fe1e
TT
1714/*
1715 * Try to release a page associated with block device when the system
1716 * is under memory pressure.
1717 */
1718static 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
7f6d5b52
RZ
1728static 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
4c54ac62 1739static const struct address_space_operations def_blk_aops = {
1da177e4 1740 .readpage = blkdev_readpage,
447f05bb 1741 .readpages = blkdev_readpages,
1da177e4 1742 .writepage = blkdev_writepage,
6272b5a5
NP
1743 .write_begin = blkdev_write_begin,
1744 .write_end = blkdev_write_end,
7f6d5b52 1745 .writepages = blkdev_writepages,
87d8fe1e 1746 .releasepage = blkdev_releasepage,
1da177e4 1747 .direct_IO = blkdev_direct_IO,
b4597226 1748 .is_dirty_writeback = buffer_check_dirty_writeback,
1da177e4
LT
1749};
1750
4b6f5d20 1751const struct file_operations def_blk_fops = {
1da177e4
LT
1752 .open = blkdev_open,
1753 .release = blkdev_close,
1754 .llseek = block_llseek,
a886038b 1755 .read_iter = blkdev_read_iter,
1456c0a8 1756 .write_iter = blkdev_write_iter,
acc93d30 1757 .mmap = generic_file_mmap,
b1dd3b28 1758 .fsync = blkdev_fsync,
bb93e3a5 1759 .unlocked_ioctl = block_ioctl,
1da177e4
LT
1760#ifdef CONFIG_COMPAT
1761 .compat_ioctl = compat_blkdev_ioctl,
1762#endif
1e8b3332 1763 .splice_read = generic_file_splice_read,
8d020765 1764 .splice_write = iter_file_splice_write,
1da177e4
LT
1765};
1766
1767int 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);
56b26add 1772 res = blkdev_ioctl(bdev, 0, cmd, arg);
1da177e4
LT
1773 set_fs(old_fs);
1774 return res;
1775}
1776
1777EXPORT_SYMBOL(ioctl_by_bdev);
1778
1779/**
1780 * lookup_bdev - lookup a struct block_device by name
94e2959e 1781 * @pathname: special file representing the block device
1da177e4 1782 *
57d1b536 1783 * Get a reference to the blockdevice at @pathname in the current
1da177e4
LT
1784 * namespace if possible and return it. Return ERR_PTR(error)
1785 * otherwise.
1786 */
421748ec 1787struct block_device *lookup_bdev(const char *pathname)
1da177e4
LT
1788{
1789 struct block_device *bdev;
1790 struct inode *inode;
421748ec 1791 struct path path;
1da177e4
LT
1792 int error;
1793
421748ec 1794 if (!pathname || !*pathname)
1da177e4
LT
1795 return ERR_PTR(-EINVAL);
1796
421748ec 1797 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
1da177e4
LT
1798 if (error)
1799 return ERR_PTR(error);
1800
bb668734 1801 inode = d_backing_inode(path.dentry);
1da177e4
LT
1802 error = -ENOTBLK;
1803 if (!S_ISBLK(inode->i_mode))
1804 goto fail;
1805 error = -EACCES;
421748ec 1806 if (path.mnt->mnt_flags & MNT_NODEV)
1da177e4
LT
1807 goto fail;
1808 error = -ENOMEM;
1809 bdev = bd_acquire(inode);
1810 if (!bdev)
1811 goto fail;
1812out:
421748ec 1813 path_put(&path);
1da177e4
LT
1814 return bdev;
1815fail:
1816 bdev = ERR_PTR(error);
1817 goto out;
1818}
d5686b44 1819EXPORT_SYMBOL(lookup_bdev);
1da177e4 1820
93b270f7 1821int __invalidate_device(struct block_device *bdev, bool kill_dirty)
b71e8a4c
DH
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);
93b270f7 1834 res = invalidate_inodes(sb, kill_dirty);
b71e8a4c
DH
1835 drop_super(sb);
1836 }
f98393a6 1837 invalidate_bdev(bdev);
b71e8a4c
DH
1838 return res;
1839}
1840EXPORT_SYMBOL(__invalidate_device);
5c0d6b60
JK
1841
1842void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
1843{
1844 struct inode *inode, *old_inode = NULL;
1845
74278da9 1846 spin_lock(&blockdev_superblock->s_inode_list_lock);
5c0d6b60
JK
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);
74278da9 1858 spin_unlock(&blockdev_superblock->s_inode_list_lock);
5c0d6b60
JK
1859 /*
1860 * We hold a reference to 'inode' so it couldn't have been
1861 * removed from s_inodes list while we dropped the
74278da9 1862 * s_inode_list_lock We cannot iput the inode now as we can
5c0d6b60 1863 * be holding the last reference and we cannot iput it under
74278da9 1864 * s_inode_list_lock. So we keep the reference and iput it
5c0d6b60
JK
1865 * later.
1866 */
1867 iput(old_inode);
1868 old_inode = inode;
1869
1870 func(I_BDEV(inode), arg);
1871
74278da9 1872 spin_lock(&blockdev_superblock->s_inode_list_lock);
5c0d6b60 1873 }
74278da9 1874 spin_unlock(&blockdev_superblock->s_inode_list_lock);
5c0d6b60
JK
1875 iput(old_inode);
1876}
This page took 0.905037 seconds and 5 git commands to generate.