Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0-only
2 : /*
3 : * Copyright (C) 1991, 1992 Linus Torvalds
4 : * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
5 : * Copyright (C) 2016 - 2020 Christoph Hellwig
6 : */
7 : #include <linux/init.h>
8 : #include <linux/mm.h>
9 : #include <linux/blkdev.h>
10 : #include <linux/buffer_head.h>
11 : #include <linux/mpage.h>
12 : #include <linux/uio.h>
13 : #include <linux/namei.h>
14 : #include <linux/task_io_accounting_ops.h>
15 : #include <linux/falloc.h>
16 : #include <linux/suspend.h>
17 : #include <linux/fs.h>
18 : #include <linux/module.h>
19 : #include "blk.h"
20 :
21 : static inline struct inode *bdev_file_inode(struct file *file)
22 : {
23 0 : return file->f_mapping->host;
24 : }
25 :
26 0 : static int blkdev_get_block(struct inode *inode, sector_t iblock,
27 : struct buffer_head *bh, int create)
28 : {
29 0 : bh->b_bdev = I_BDEV(inode);
30 0 : bh->b_blocknr = iblock;
31 0 : set_buffer_mapped(bh);
32 0 : return 0;
33 : }
34 :
35 : static unsigned int dio_bio_write_op(struct kiocb *iocb)
36 : {
37 0 : unsigned int op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
38 :
39 : /* avoid the need for a I/O completion work item */
40 0 : if (iocb->ki_flags & IOCB_DSYNC)
41 0 : op |= REQ_FUA;
42 : return op;
43 : }
44 :
45 : #define DIO_INLINE_BIO_VECS 4
46 :
47 0 : static void blkdev_bio_end_io_simple(struct bio *bio)
48 : {
49 0 : struct task_struct *waiter = bio->bi_private;
50 :
51 0 : WRITE_ONCE(bio->bi_private, NULL);
52 0 : blk_wake_io_task(waiter);
53 0 : }
54 :
55 0 : static ssize_t __blkdev_direct_IO_simple(struct kiocb *iocb,
56 : struct iov_iter *iter, unsigned int nr_pages)
57 : {
58 0 : struct block_device *bdev = iocb->ki_filp->private_data;
59 : struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs;
60 0 : loff_t pos = iocb->ki_pos;
61 0 : bool should_dirty = false;
62 : struct bio bio;
63 : ssize_t ret;
64 :
65 0 : if ((pos | iov_iter_alignment(iter)) &
66 0 : (bdev_logical_block_size(bdev) - 1))
67 : return -EINVAL;
68 :
69 0 : if (nr_pages <= DIO_INLINE_BIO_VECS)
70 : vecs = inline_vecs;
71 : else {
72 0 : vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec),
73 : GFP_KERNEL);
74 0 : if (!vecs)
75 : return -ENOMEM;
76 : }
77 :
78 0 : if (iov_iter_rw(iter) == READ) {
79 0 : bio_init(&bio, bdev, vecs, nr_pages, REQ_OP_READ);
80 0 : if (iter_is_iovec(iter))
81 0 : should_dirty = true;
82 : } else {
83 0 : bio_init(&bio, bdev, vecs, nr_pages, dio_bio_write_op(iocb));
84 : }
85 0 : bio.bi_iter.bi_sector = pos >> SECTOR_SHIFT;
86 0 : bio.bi_private = current;
87 0 : bio.bi_end_io = blkdev_bio_end_io_simple;
88 0 : bio.bi_ioprio = iocb->ki_ioprio;
89 :
90 0 : ret = bio_iov_iter_get_pages(&bio, iter);
91 0 : if (unlikely(ret))
92 : goto out;
93 0 : ret = bio.bi_iter.bi_size;
94 :
95 0 : if (iov_iter_rw(iter) == WRITE)
96 : task_io_account_write(ret);
97 :
98 0 : if (iocb->ki_flags & IOCB_NOWAIT)
99 0 : bio.bi_opf |= REQ_NOWAIT;
100 0 : if (iocb->ki_flags & IOCB_HIPRI)
101 : bio_set_polled(&bio, iocb);
102 :
103 0 : submit_bio(&bio);
104 : for (;;) {
105 0 : set_current_state(TASK_UNINTERRUPTIBLE);
106 0 : if (!READ_ONCE(bio.bi_private))
107 : break;
108 0 : if (!(iocb->ki_flags & IOCB_HIPRI) || !bio_poll(&bio, NULL, 0))
109 0 : blk_io_schedule();
110 : }
111 0 : __set_current_state(TASK_RUNNING);
112 :
113 0 : bio_release_pages(&bio, should_dirty);
114 0 : if (unlikely(bio.bi_status))
115 0 : ret = blk_status_to_errno(bio.bi_status);
116 :
117 : out:
118 0 : if (vecs != inline_vecs)
119 0 : kfree(vecs);
120 :
121 0 : bio_uninit(&bio);
122 :
123 0 : return ret;
124 : }
125 :
126 : enum {
127 : DIO_SHOULD_DIRTY = 1,
128 : DIO_IS_SYNC = 2,
129 : };
130 :
131 : struct blkdev_dio {
132 : union {
133 : struct kiocb *iocb;
134 : struct task_struct *waiter;
135 : };
136 : size_t size;
137 : atomic_t ref;
138 : unsigned int flags;
139 : struct bio bio ____cacheline_aligned_in_smp;
140 : };
141 :
142 : static struct bio_set blkdev_dio_pool;
143 :
144 0 : static void blkdev_bio_end_io(struct bio *bio)
145 : {
146 0 : struct blkdev_dio *dio = bio->bi_private;
147 0 : bool should_dirty = dio->flags & DIO_SHOULD_DIRTY;
148 :
149 0 : if (bio->bi_status && !dio->bio.bi_status)
150 0 : dio->bio.bi_status = bio->bi_status;
151 :
152 0 : if (atomic_dec_and_test(&dio->ref)) {
153 0 : if (!(dio->flags & DIO_IS_SYNC)) {
154 0 : struct kiocb *iocb = dio->iocb;
155 : ssize_t ret;
156 :
157 0 : WRITE_ONCE(iocb->private, NULL);
158 :
159 0 : if (likely(!dio->bio.bi_status)) {
160 0 : ret = dio->size;
161 0 : iocb->ki_pos += ret;
162 : } else {
163 0 : ret = blk_status_to_errno(dio->bio.bi_status);
164 : }
165 :
166 0 : dio->iocb->ki_complete(iocb, ret);
167 0 : bio_put(&dio->bio);
168 : } else {
169 0 : struct task_struct *waiter = dio->waiter;
170 :
171 0 : WRITE_ONCE(dio->waiter, NULL);
172 : blk_wake_io_task(waiter);
173 : }
174 : }
175 :
176 0 : if (should_dirty) {
177 0 : bio_check_pages_dirty(bio);
178 : } else {
179 0 : bio_release_pages(bio, false);
180 0 : bio_put(bio);
181 : }
182 0 : }
183 :
184 0 : static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
185 : unsigned int nr_pages)
186 : {
187 0 : struct block_device *bdev = iocb->ki_filp->private_data;
188 : struct blk_plug plug;
189 : struct blkdev_dio *dio;
190 : struct bio *bio;
191 0 : bool is_read = (iov_iter_rw(iter) == READ), is_sync;
192 0 : unsigned int opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb);
193 0 : loff_t pos = iocb->ki_pos;
194 0 : int ret = 0;
195 :
196 0 : if ((pos | iov_iter_alignment(iter)) &
197 0 : (bdev_logical_block_size(bdev) - 1))
198 : return -EINVAL;
199 :
200 0 : bio = bio_alloc_kiocb(iocb, bdev, nr_pages, opf, &blkdev_dio_pool);
201 :
202 0 : dio = container_of(bio, struct blkdev_dio, bio);
203 0 : atomic_set(&dio->ref, 1);
204 : /*
205 : * Grab an extra reference to ensure the dio structure which is embedded
206 : * into the first bio stays around.
207 : */
208 0 : bio_get(bio);
209 :
210 0 : is_sync = is_sync_kiocb(iocb);
211 0 : if (is_sync) {
212 0 : dio->flags = DIO_IS_SYNC;
213 0 : dio->waiter = current;
214 : } else {
215 0 : dio->flags = 0;
216 0 : dio->iocb = iocb;
217 : }
218 :
219 0 : dio->size = 0;
220 0 : if (is_read && iter_is_iovec(iter))
221 0 : dio->flags |= DIO_SHOULD_DIRTY;
222 :
223 0 : blk_start_plug(&plug);
224 :
225 : for (;;) {
226 0 : bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT;
227 0 : bio->bi_private = dio;
228 0 : bio->bi_end_io = blkdev_bio_end_io;
229 0 : bio->bi_ioprio = iocb->ki_ioprio;
230 :
231 0 : ret = bio_iov_iter_get_pages(bio, iter);
232 0 : if (unlikely(ret)) {
233 0 : bio->bi_status = BLK_STS_IOERR;
234 0 : bio_endio(bio);
235 0 : break;
236 : }
237 :
238 0 : if (is_read) {
239 0 : if (dio->flags & DIO_SHOULD_DIRTY)
240 0 : bio_set_pages_dirty(bio);
241 : } else {
242 : task_io_account_write(bio->bi_iter.bi_size);
243 : }
244 0 : if (iocb->ki_flags & IOCB_NOWAIT)
245 0 : bio->bi_opf |= REQ_NOWAIT;
246 :
247 0 : dio->size += bio->bi_iter.bi_size;
248 0 : pos += bio->bi_iter.bi_size;
249 :
250 0 : nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS);
251 0 : if (!nr_pages) {
252 0 : submit_bio(bio);
253 0 : break;
254 : }
255 0 : atomic_inc(&dio->ref);
256 0 : submit_bio(bio);
257 0 : bio = bio_alloc(bdev, nr_pages, opf, GFP_KERNEL);
258 : }
259 :
260 0 : blk_finish_plug(&plug);
261 :
262 0 : if (!is_sync)
263 : return -EIOCBQUEUED;
264 :
265 : for (;;) {
266 0 : set_current_state(TASK_UNINTERRUPTIBLE);
267 0 : if (!READ_ONCE(dio->waiter))
268 : break;
269 0 : blk_io_schedule();
270 : }
271 0 : __set_current_state(TASK_RUNNING);
272 :
273 0 : if (!ret)
274 0 : ret = blk_status_to_errno(dio->bio.bi_status);
275 0 : if (likely(!ret))
276 0 : ret = dio->size;
277 :
278 0 : bio_put(&dio->bio);
279 0 : return ret;
280 : }
281 :
282 0 : static void blkdev_bio_end_io_async(struct bio *bio)
283 : {
284 0 : struct blkdev_dio *dio = container_of(bio, struct blkdev_dio, bio);
285 0 : struct kiocb *iocb = dio->iocb;
286 : ssize_t ret;
287 :
288 0 : WRITE_ONCE(iocb->private, NULL);
289 :
290 0 : if (likely(!bio->bi_status)) {
291 0 : ret = dio->size;
292 0 : iocb->ki_pos += ret;
293 : } else {
294 0 : ret = blk_status_to_errno(bio->bi_status);
295 : }
296 :
297 0 : iocb->ki_complete(iocb, ret);
298 :
299 0 : if (dio->flags & DIO_SHOULD_DIRTY) {
300 0 : bio_check_pages_dirty(bio);
301 : } else {
302 0 : bio_release_pages(bio, false);
303 0 : bio_put(bio);
304 : }
305 0 : }
306 :
307 0 : static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb,
308 : struct iov_iter *iter,
309 : unsigned int nr_pages)
310 : {
311 0 : struct block_device *bdev = iocb->ki_filp->private_data;
312 0 : bool is_read = iov_iter_rw(iter) == READ;
313 0 : unsigned int opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb);
314 : struct blkdev_dio *dio;
315 : struct bio *bio;
316 0 : loff_t pos = iocb->ki_pos;
317 0 : int ret = 0;
318 :
319 0 : if ((pos | iov_iter_alignment(iter)) &
320 0 : (bdev_logical_block_size(bdev) - 1))
321 : return -EINVAL;
322 :
323 0 : bio = bio_alloc_kiocb(iocb, bdev, nr_pages, opf, &blkdev_dio_pool);
324 0 : dio = container_of(bio, struct blkdev_dio, bio);
325 0 : dio->flags = 0;
326 0 : dio->iocb = iocb;
327 0 : bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT;
328 0 : bio->bi_end_io = blkdev_bio_end_io_async;
329 0 : bio->bi_ioprio = iocb->ki_ioprio;
330 :
331 0 : if (iov_iter_is_bvec(iter)) {
332 : /*
333 : * Users don't rely on the iterator being in any particular
334 : * state for async I/O returning -EIOCBQUEUED, hence we can
335 : * avoid expensive iov_iter_advance(). Bypass
336 : * bio_iov_iter_get_pages() and set the bvec directly.
337 : */
338 0 : bio_iov_bvec_set(bio, iter);
339 : } else {
340 0 : ret = bio_iov_iter_get_pages(bio, iter);
341 0 : if (unlikely(ret)) {
342 0 : bio_put(bio);
343 0 : return ret;
344 : }
345 : }
346 0 : dio->size = bio->bi_iter.bi_size;
347 :
348 0 : if (is_read) {
349 0 : if (iter_is_iovec(iter)) {
350 0 : dio->flags |= DIO_SHOULD_DIRTY;
351 0 : bio_set_pages_dirty(bio);
352 : }
353 : } else {
354 : task_io_account_write(bio->bi_iter.bi_size);
355 : }
356 :
357 0 : if (iocb->ki_flags & IOCB_HIPRI) {
358 0 : bio->bi_opf |= REQ_POLLED | REQ_NOWAIT;
359 0 : submit_bio(bio);
360 0 : WRITE_ONCE(iocb->private, bio);
361 : } else {
362 0 : if (iocb->ki_flags & IOCB_NOWAIT)
363 0 : bio->bi_opf |= REQ_NOWAIT;
364 0 : submit_bio(bio);
365 : }
366 : return -EIOCBQUEUED;
367 : }
368 :
369 0 : static ssize_t blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
370 : {
371 : unsigned int nr_pages;
372 :
373 0 : if (!iov_iter_count(iter))
374 : return 0;
375 :
376 0 : nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS + 1);
377 0 : if (likely(nr_pages <= BIO_MAX_VECS)) {
378 0 : if (is_sync_kiocb(iocb))
379 0 : return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
380 0 : return __blkdev_direct_IO_async(iocb, iter, nr_pages);
381 : }
382 0 : return __blkdev_direct_IO(iocb, iter, bio_max_segs(nr_pages));
383 : }
384 :
385 0 : static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
386 : {
387 0 : return block_write_full_page(page, blkdev_get_block, wbc);
388 : }
389 :
390 0 : static int blkdev_readpage(struct file * file, struct page * page)
391 : {
392 0 : return block_read_full_page(page, blkdev_get_block);
393 : }
394 :
395 0 : static void blkdev_readahead(struct readahead_control *rac)
396 : {
397 0 : mpage_readahead(rac, blkdev_get_block);
398 0 : }
399 :
400 0 : static int blkdev_write_begin(struct file *file, struct address_space *mapping,
401 : loff_t pos, unsigned len, unsigned flags, struct page **pagep,
402 : void **fsdata)
403 : {
404 0 : return block_write_begin(mapping, pos, len, flags, pagep,
405 : blkdev_get_block);
406 : }
407 :
408 0 : static int blkdev_write_end(struct file *file, struct address_space *mapping,
409 : loff_t pos, unsigned len, unsigned copied, struct page *page,
410 : void *fsdata)
411 : {
412 : int ret;
413 0 : ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
414 :
415 0 : unlock_page(page);
416 0 : put_page(page);
417 :
418 0 : return ret;
419 : }
420 :
421 0 : static int blkdev_writepages(struct address_space *mapping,
422 : struct writeback_control *wbc)
423 : {
424 0 : return generic_writepages(mapping, wbc);
425 : }
426 :
427 : const struct address_space_operations def_blk_aops = {
428 : .dirty_folio = block_dirty_folio,
429 : .invalidate_folio = block_invalidate_folio,
430 : .readpage = blkdev_readpage,
431 : .readahead = blkdev_readahead,
432 : .writepage = blkdev_writepage,
433 : .write_begin = blkdev_write_begin,
434 : .write_end = blkdev_write_end,
435 : .writepages = blkdev_writepages,
436 : .direct_IO = blkdev_direct_IO,
437 : .migratepage = buffer_migrate_page_norefs,
438 : .is_dirty_writeback = buffer_check_dirty_writeback,
439 : };
440 :
441 : /*
442 : * for a block special file file_inode(file)->i_size is zero
443 : * so we compute the size by hand (just as in block_read/write above)
444 : */
445 0 : static loff_t blkdev_llseek(struct file *file, loff_t offset, int whence)
446 : {
447 0 : struct inode *bd_inode = bdev_file_inode(file);
448 : loff_t retval;
449 :
450 0 : inode_lock(bd_inode);
451 0 : retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
452 0 : inode_unlock(bd_inode);
453 0 : return retval;
454 : }
455 :
456 0 : static int blkdev_fsync(struct file *filp, loff_t start, loff_t end,
457 : int datasync)
458 : {
459 0 : struct block_device *bdev = filp->private_data;
460 : int error;
461 :
462 0 : error = file_write_and_wait_range(filp, start, end);
463 0 : if (error)
464 : return error;
465 :
466 : /*
467 : * There is no need to serialise calls to blkdev_issue_flush with
468 : * i_mutex and doing so causes performance issues with concurrent
469 : * O_SYNC writers to a block device.
470 : */
471 0 : error = blkdev_issue_flush(bdev);
472 0 : if (error == -EOPNOTSUPP)
473 0 : error = 0;
474 :
475 : return error;
476 : }
477 :
478 0 : static int blkdev_open(struct inode *inode, struct file *filp)
479 : {
480 : struct block_device *bdev;
481 :
482 : /*
483 : * Preserve backwards compatibility and allow large file access
484 : * even if userspace doesn't ask for it explicitly. Some mkfs
485 : * binary needs it. We might want to drop this workaround
486 : * during an unstable branch.
487 : */
488 0 : filp->f_flags |= O_LARGEFILE;
489 0 : filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
490 :
491 0 : if (filp->f_flags & O_NDELAY)
492 0 : filp->f_mode |= FMODE_NDELAY;
493 0 : if (filp->f_flags & O_EXCL)
494 0 : filp->f_mode |= FMODE_EXCL;
495 0 : if ((filp->f_flags & O_ACCMODE) == 3)
496 0 : filp->f_mode |= FMODE_WRITE_IOCTL;
497 :
498 0 : bdev = blkdev_get_by_dev(inode->i_rdev, filp->f_mode, filp);
499 0 : if (IS_ERR(bdev))
500 0 : return PTR_ERR(bdev);
501 :
502 0 : filp->private_data = bdev;
503 0 : filp->f_mapping = bdev->bd_inode->i_mapping;
504 0 : filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
505 0 : return 0;
506 : }
507 :
508 0 : static int blkdev_close(struct inode *inode, struct file *filp)
509 : {
510 0 : struct block_device *bdev = filp->private_data;
511 :
512 0 : blkdev_put(bdev, filp->f_mode);
513 0 : return 0;
514 : }
515 :
516 : /*
517 : * Write data to the block device. Only intended for the block device itself
518 : * and the raw driver which basically is a fake block device.
519 : *
520 : * Does not take i_mutex for the write and thus is not for general purpose
521 : * use.
522 : */
523 0 : static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
524 : {
525 0 : struct block_device *bdev = iocb->ki_filp->private_data;
526 0 : struct inode *bd_inode = bdev->bd_inode;
527 0 : loff_t size = bdev_nr_bytes(bdev);
528 : struct blk_plug plug;
529 0 : size_t shorted = 0;
530 : ssize_t ret;
531 :
532 0 : if (bdev_read_only(bdev))
533 : return -EPERM;
534 :
535 0 : if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev))
536 : return -ETXTBSY;
537 :
538 0 : if (!iov_iter_count(from))
539 : return 0;
540 :
541 0 : if (iocb->ki_pos >= size)
542 : return -ENOSPC;
543 :
544 0 : if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
545 : return -EOPNOTSUPP;
546 :
547 0 : size -= iocb->ki_pos;
548 0 : if (iov_iter_count(from) > size) {
549 0 : shorted = iov_iter_count(from) - size;
550 0 : iov_iter_truncate(from, size);
551 : }
552 :
553 0 : blk_start_plug(&plug);
554 0 : ret = __generic_file_write_iter(iocb, from);
555 0 : if (ret > 0)
556 0 : ret = generic_write_sync(iocb, ret);
557 0 : iov_iter_reexpand(from, iov_iter_count(from) + shorted);
558 0 : blk_finish_plug(&plug);
559 0 : return ret;
560 : }
561 :
562 0 : static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
563 : {
564 0 : struct block_device *bdev = iocb->ki_filp->private_data;
565 0 : loff_t size = bdev_nr_bytes(bdev);
566 0 : loff_t pos = iocb->ki_pos;
567 0 : size_t shorted = 0;
568 0 : ssize_t ret = 0;
569 : size_t count;
570 :
571 0 : if (unlikely(pos + iov_iter_count(to) > size)) {
572 0 : if (pos >= size)
573 : return 0;
574 0 : size -= pos;
575 0 : shorted = iov_iter_count(to) - size;
576 0 : iov_iter_truncate(to, size);
577 : }
578 :
579 0 : count = iov_iter_count(to);
580 0 : if (!count)
581 : goto reexpand; /* skip atime */
582 :
583 0 : if (iocb->ki_flags & IOCB_DIRECT) {
584 0 : struct address_space *mapping = iocb->ki_filp->f_mapping;
585 :
586 0 : if (iocb->ki_flags & IOCB_NOWAIT) {
587 0 : if (filemap_range_needs_writeback(mapping, pos,
588 0 : pos + count - 1)) {
589 : ret = -EAGAIN;
590 : goto reexpand;
591 : }
592 : } else {
593 0 : ret = filemap_write_and_wait_range(mapping, pos,
594 0 : pos + count - 1);
595 0 : if (ret < 0)
596 : goto reexpand;
597 : }
598 :
599 0 : file_accessed(iocb->ki_filp);
600 :
601 0 : ret = blkdev_direct_IO(iocb, to);
602 0 : if (ret >= 0) {
603 0 : iocb->ki_pos += ret;
604 0 : count -= ret;
605 : }
606 0 : iov_iter_revert(to, count - iov_iter_count(to));
607 0 : if (ret < 0 || !count)
608 : goto reexpand;
609 : }
610 :
611 0 : ret = filemap_read(iocb, to, ret);
612 :
613 : reexpand:
614 0 : if (unlikely(shorted))
615 0 : iov_iter_reexpand(to, iov_iter_count(to) + shorted);
616 : return ret;
617 : }
618 :
619 : #define BLKDEV_FALLOC_FL_SUPPORTED \
620 : (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
621 : FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
622 :
623 0 : static long blkdev_fallocate(struct file *file, int mode, loff_t start,
624 : loff_t len)
625 : {
626 0 : struct inode *inode = bdev_file_inode(file);
627 0 : struct block_device *bdev = I_BDEV(inode);
628 0 : loff_t end = start + len - 1;
629 : loff_t isize;
630 : int error;
631 :
632 : /* Fail if we don't recognize the flags. */
633 0 : if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
634 : return -EOPNOTSUPP;
635 :
636 : /* Don't go off the end of the device. */
637 0 : isize = bdev_nr_bytes(bdev);
638 0 : if (start >= isize)
639 : return -EINVAL;
640 0 : if (end >= isize) {
641 0 : if (mode & FALLOC_FL_KEEP_SIZE) {
642 0 : len = isize - start;
643 0 : end = start + len - 1;
644 : } else
645 : return -EINVAL;
646 : }
647 :
648 : /*
649 : * Don't allow IO that isn't aligned to logical block size.
650 : */
651 0 : if ((start | len) & (bdev_logical_block_size(bdev) - 1))
652 : return -EINVAL;
653 :
654 0 : filemap_invalidate_lock(inode->i_mapping);
655 :
656 : /* Invalidate the page cache, including dirty pages. */
657 0 : error = truncate_bdev_range(bdev, file->f_mode, start, end);
658 0 : if (error)
659 : goto fail;
660 :
661 0 : switch (mode) {
662 : case FALLOC_FL_ZERO_RANGE:
663 : case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
664 0 : error = blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT,
665 0 : len >> SECTOR_SHIFT, GFP_KERNEL,
666 : BLKDEV_ZERO_NOUNMAP);
667 0 : break;
668 : case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
669 0 : error = blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT,
670 0 : len >> SECTOR_SHIFT, GFP_KERNEL,
671 : BLKDEV_ZERO_NOFALLBACK);
672 0 : break;
673 : case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
674 0 : error = blkdev_issue_discard(bdev, start >> SECTOR_SHIFT,
675 0 : len >> SECTOR_SHIFT, GFP_KERNEL, 0);
676 0 : break;
677 : default:
678 : error = -EOPNOTSUPP;
679 : }
680 :
681 : fail:
682 0 : filemap_invalidate_unlock(inode->i_mapping);
683 0 : return error;
684 : }
685 :
686 : const struct file_operations def_blk_fops = {
687 : .open = blkdev_open,
688 : .release = blkdev_close,
689 : .llseek = blkdev_llseek,
690 : .read_iter = blkdev_read_iter,
691 : .write_iter = blkdev_write_iter,
692 : .iopoll = iocb_bio_iopoll,
693 : .mmap = generic_file_mmap,
694 : .fsync = blkdev_fsync,
695 : .unlocked_ioctl = blkdev_ioctl,
696 : #ifdef CONFIG_COMPAT
697 : .compat_ioctl = compat_blkdev_ioctl,
698 : #endif
699 : .splice_read = generic_file_splice_read,
700 : .splice_write = iter_file_splice_write,
701 : .fallocate = blkdev_fallocate,
702 : };
703 :
704 1 : static __init int blkdev_init(void)
705 : {
706 1 : return bioset_init(&blkdev_dio_pool, 4,
707 : offsetof(struct blkdev_dio, bio),
708 : BIOSET_NEED_BVECS|BIOSET_PERCPU_CACHE);
709 : }
710 : module_init(blkdev_init);
|