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