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